6 #define VERSION VER2(0,10)
8 #include <tpl_drv_common.h>
12 #define DEFAUTL_METRIC 30
15 extern tInterface *gIP_Interfaces;
16 extern tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Filename);
20 char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos);
21 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name);
22 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data);
24 tRoute *IPStack_Route_Create(const char *InterfaceName);
25 tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric);
26 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address);
27 // - Individual Routes
28 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data);
31 int giIP_NextRouteId = 1;
33 tRoute *gIP_RoutesEnd;
34 tVFS_Node gIP_RouteNode = {
35 Flags: VFS_FFLAG_DIRECTORY,
38 ACLs: &gVFS_ACL_EveryoneRX,
39 ReadDir: IPStack_RouteDir_ReadDir,
40 FindDir: IPStack_RouteDir_FindDir,
41 IOCtl: IPStack_RouteDir_IOCtl
46 * \brief ReadDir for the /Devices/ip/routes/ directory
48 char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos)
52 for(rt = gIP_Routes; rt && Pos --; rt = rt->Next);
59 int len = sprintf(NULL, "%i", rt->Node.Inode);
61 sprintf(buf, "%i", rt->Node.Inode);
67 * \brief FindDir for the /Devices/ip/routes/ directory
69 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name)
74 // Zero is invalid, sorry :)
75 if( !num ) return NULL;
77 // The list is inherently sorted, so we can do a quick search
78 for(rt = gIP_Routes; rt && rt->Node.Inode < num; rt = rt->Next);
79 // Fast fail on larger number
80 if( rt->Node.Inode > num )
87 * \brief Names for the route list IOCtl Calls
89 static const char *casIOCtls_RouteDir[] = {
91 "add_route", // Add a route - char *InterfaceName
92 "locate_route", // Find the best route for an address - struct {int Type, char Address[]} *
97 * \brief IOCtl for /Devices/ip/routes/
99 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
103 ENTER("pNode iID pData", Node, ID, Data);
106 // --- Standard IOCtls (0-3) ---
107 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_RouteDir)
110 if( !CheckString(Data) ) LEAVE_RET('i', -1);
111 rt = IPStack_Route_Create(Data);
115 tmp = rt->Node.Inode;
119 case 5: // Locate Route
126 if( !CheckMem(Data, sizeof(int)) )
128 if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
131 Log_Debug("IPStack", "Route_RouteDir_IOCtl - FindRoute %i, %s",
132 data->Type, IPStack_PrintAddress(data->Type, data->Addr) );
133 rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
138 LEAVE('i', rt->Node.Inode);
139 return rt->Node.Inode;
148 * \brief Create a new route entry
149 * \param InterfaceName Name of the interface using this route
151 tRoute *IPStack_Route_Create(const char *InterfaceName)
158 // Note: Oh man! This is such a hack
160 tVFS_Node *node = IPStack_Root_FindDir(NULL, InterfaceName);
162 Log_Debug("IPStack", "IPStack_Route_Create - Unknown interface '%s'\n", InterfaceName);
165 iface = node->ImplPtr;
166 if(node->Close) node->Close(node);
169 // Get the size of the specified address type
170 size = IPStack_GetAddressSize(iface->Type);
176 rt = calloc(1, sizeof(tRoute) + size*2 );
179 rt->Node.ImplPtr = rt;
180 rt->Node.Inode = giIP_NextRouteId ++;
182 rt->Node.NumACLs = 1,
183 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
184 rt->Node.IOCtl = IPStack_Route_IOCtl;
187 rt->AddressType = iface->Type;
188 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
190 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
191 rt->Interface = iface;
192 rt->Metric = DEFAUTL_METRIC;
193 memset(rt->Network, 0, size);
194 memset(rt->NextHop, 0, size);
197 if( gIP_RoutesEnd ) {
198 gIP_RoutesEnd->Next = rt;
202 gIP_Routes = gIP_RoutesEnd = rt;
205 Log_Log("IPStack", "Route entry for '%s' created", InterfaceName);
211 * \brief Add and fill a route
213 tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric)
215 tRoute *rt = IPStack_Route_Create(Interface);
218 if( !rt ) return NULL;
220 addrSize = IPStack_GetAddressSize(rt->Interface->Type);
222 memcpy(rt->Network, Network, addrSize);
224 memcpy(rt->NextHop, NextHop, addrSize);
225 rt->SubnetBits = SubnetBits;
234 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
241 ENTER("iAddressType pInterface sAddress",
242 AddressType, Interface, IPStack_PrintAddress(AddressType, Address));
244 if( Interface && AddressType != Interface->Type ) {
245 LOG("Interface->Type (%i) != AddressType", Interface->Type);
251 addrSize = IPStack_GetAddressSize(AddressType);
253 // Check against explicit routes
254 for( rt = gIP_Routes; rt; rt = rt->Next )
257 if( Interface && rt->Interface != Interface ) continue;
258 // Check address type
259 if( rt->AddressType != AddressType ) continue;
261 LOG("Checking network %s/%i", IPStack_PrintAddress(AddressType, rt->Network), rt->SubnetBits);
263 // Check if the address matches
264 if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
268 // More direct routes are preferred
269 if( best->SubnetBits > rt->SubnetBits ) {
270 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
273 // If equally direct, choose the best metric
274 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
275 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
283 // Check against implicit routes
284 if( !best && !Interface )
286 for( iface = gIP_Interfaces; iface; iface = iface->Next )
288 if( Interface && iface != Interface ) continue;
289 if( iface->Type != AddressType ) continue;
292 // Check if the address matches
293 if( !IPStack_CompareAddress(AddressType, iface->Address, Address, iface->SubnetBits) )
297 // More direct routes are preferred
298 if( best->SubnetBits > rt->SubnetBits ) {
299 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
302 // If equally direct, choose the best metric
303 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
304 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
310 memcpy(rt->Network, iface->Address, addrSize);
311 memset(rt->NextHop, 0, addrSize);
312 rt->Metric = DEFAUTL_METRIC;
313 rt->SubnetBits = iface->SubnetBits;
318 if( !best && Interface )
320 rt = &Interface->Route;
321 // Make sure route is up to date
322 memcpy(rt->Network, iface->Address, addrSize);
323 memset(rt->NextHop, 0, addrSize);
324 rt->Metric = DEFAUTL_METRIC;
325 rt->SubnetBits = iface->SubnetBits;
327 if( IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
338 * \brief Names for route IOCtl Calls
340 static const char *casIOCtls_Route[] = {
342 "get_type", // Get address type - (void), returns integer type
343 "get_network", // Get network - (void *Data), returns boolean success
344 "set_network", // Set network - (void *Data), returns boolean success
345 "get_nexthop", // Get next hop - (void *Data), returns boolean success
346 "set_nexthop", // Set next hop - (void *Data), returns boolean success
347 "getset_subnetbits", // Get/Set subnet bits - (int *Bits), returns current value
348 "getset_metric", // Get/Set metric - (int *Metric), returns current value
349 "get_interface", // Get interface name - (char *Name), returns name length, NULL OK
354 * \brief IOCtl for /Devices/ip/routes/#
356 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
359 tRoute *rt = Node->ImplPtr;
360 int addrSize = IPStack_GetAddressSize(rt->AddressType);
364 // --- Standard IOCtls (0-3) ---
365 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_Route)
369 return rt->AddressType;
373 if( !CheckMem(Data, addrSize) ) return -1;
374 memcpy(Data, rt->Network, addrSize);
378 if( !CheckMem(Data, addrSize) ) return -1;
379 memcpy(rt->Network, Data, addrSize);
384 if( !CheckMem(Data, addrSize) ) return -1;
385 memcpy(Data, rt->NextHop, addrSize);
389 if( !CheckMem(Data, addrSize) ) return -1;
390 memcpy(rt->NextHop, Data, addrSize);
393 // Get/Set Subnet Bits
396 if( !CheckMem(Data, sizeof(int)) ) return -1;
397 if( *iData < 0 || *iData > addrSize*8 )
399 rt->SubnetBits = *iData;
401 return rt->SubnetBits;
406 if( !CheckMem(Data, sizeof(int)) ) return -1;
407 if( *iData < 0 ) return -1;
412 // Get interface name
415 if( !CheckMem(Data, strlen(rt->Interface->Name) + 1) )
417 strcpy(Data, rt->Interface->Name);
419 return strlen(rt->Interface->Name);