6 #define VERSION VER2(0,10)
8 #include <api_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", (int)rt->Node.Inode);
61 sprintf(buf, "%i", (int)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 // Interpret the name as <type>:<addr>, returning the interface for
78 // needed to access that address.
79 // E.g. '4:0A02000A' - 10.2.0.10
80 // Hm... It could do with a way to have a better address type representation
81 if( Name[1] == ':' ) // TODO: Allow variable length type codes
83 int addrSize = IPStack_GetAddressSize(num);
84 Uint8 addrData[addrSize];
86 // Errof if the size is invalid
87 if( strlen(Name) != 2 + addrSize*2 )
91 // - Error if the address data is not fully hex
92 if( UnHex(addrData, addrSize, Name + 2) != addrSize )
96 rt = IPStack_FindRoute(num, NULL, addrData);
99 // Return the interface node
100 // - Sure it's hijacking it from inteface.c's area, but it's
102 return &rt->Interface->Node;
105 // The list is inherently sorted, so we can do a quick search
106 for(rt = gIP_Routes; rt && rt->Node.Inode < num; rt = rt->Next);
108 // Fast fail end of list / larger number
109 if( !rt || rt->Node.Inode > num )
116 * \brief Names for the route list IOCtl Calls
118 static const char *casIOCtls_RouteDir[] = {
120 "add_route", // Add a route - char *InterfaceName
121 "locate_route", // Find the best route for an address - struct {int Type, char Address[]} *
126 * \brief IOCtl for /Devices/ip/routes/
128 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
132 ENTER("pNode iID pData", Node, ID, Data);
135 // --- Standard IOCtls (0-3) ---
136 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_RouteDir)
139 if( !CheckString(Data) ) LEAVE_RET('i', -1);
140 rt = IPStack_Route_Create(Data);
144 tmp = rt->Node.Inode;
148 case 5: // Locate Route
155 if( !CheckMem(Data, sizeof(int)) )
157 if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
160 Log_Debug("IPStack", "Route_RouteDir_IOCtl - FindRoute %i, %s",
161 data->Type, IPStack_PrintAddress(data->Type, data->Addr) );
162 rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
167 LEAVE('i', rt->Node.Inode);
168 return rt->Node.Inode;
177 * \brief Create a new route entry
178 * \param InterfaceName Name of the interface using this route
180 tRoute *IPStack_Route_Create(const char *InterfaceName)
187 // Note: Oh man! This is such a hack
189 tVFS_Node *node = IPStack_Root_FindDir(NULL, InterfaceName);
191 Log_Debug("IPStack", "IPStack_Route_Create - Unknown interface '%s'\n", InterfaceName);
194 iface = node->ImplPtr;
195 if(node->Close) node->Close(node);
198 // Get the size of the specified address type
199 size = IPStack_GetAddressSize(iface->Type);
205 rt = calloc(1, sizeof(tRoute) + size*2 );
208 rt->Node.ImplPtr = rt;
209 rt->Node.Inode = giIP_NextRouteId ++;
211 rt->Node.NumACLs = 1,
212 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
213 rt->Node.IOCtl = IPStack_Route_IOCtl;
216 rt->AddressType = iface->Type;
217 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
219 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
220 rt->Interface = iface;
221 rt->Metric = DEFAUTL_METRIC;
222 memset(rt->Network, 0, size);
223 memset(rt->NextHop, 0, size);
226 if( gIP_RoutesEnd ) {
227 gIP_RoutesEnd->Next = rt;
231 gIP_Routes = gIP_RoutesEnd = rt;
234 Log_Log("IPStack", "Route entry for '%s' created", InterfaceName);
240 * \brief Add and fill a route
242 tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric)
244 tRoute *rt = IPStack_Route_Create(Interface);
247 if( !rt ) return NULL;
249 addrSize = IPStack_GetAddressSize(rt->Interface->Type);
251 memcpy(rt->Network, Network, addrSize);
253 memcpy(rt->NextHop, NextHop, addrSize);
254 rt->SubnetBits = SubnetBits;
263 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
270 ENTER("iAddressType pInterface sAddress",
271 AddressType, Interface, IPStack_PrintAddress(AddressType, Address));
273 if( Interface && AddressType != Interface->Type ) {
274 LOG("Interface->Type (%i) != AddressType", Interface->Type);
280 addrSize = IPStack_GetAddressSize(AddressType);
282 // Check against explicit routes
283 for( rt = gIP_Routes; rt; rt = rt->Next )
286 if( Interface && rt->Interface != Interface ) continue;
287 // Check address type
288 if( rt->AddressType != AddressType ) continue;
290 LOG("Checking network %s/%i", IPStack_PrintAddress(AddressType, rt->Network), rt->SubnetBits);
292 // Check if the address matches
293 if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->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);
312 // Check against implicit routes
313 if( !best && !Interface )
315 for( iface = gIP_Interfaces; iface; iface = iface->Next )
317 if( Interface && iface != Interface ) continue;
318 if( iface->Type != AddressType ) continue;
321 // Check if the address matches
322 if( !IPStack_CompareAddress(AddressType, iface->Address, Address, iface->SubnetBits) )
326 // More direct routes are preferred
327 if( best->SubnetBits > rt->SubnetBits ) {
328 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
331 // If equally direct, choose the best metric
332 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
333 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
339 memcpy(rt->Network, iface->Address, addrSize);
340 memset(rt->NextHop, 0, addrSize);
341 rt->Metric = DEFAUTL_METRIC;
342 rt->SubnetBits = iface->SubnetBits;
347 if( !best && Interface )
349 rt = &Interface->Route;
350 // Make sure route is up to date
351 memcpy(rt->Network, Interface->Address, addrSize);
352 memset(rt->NextHop, 0, addrSize);
353 rt->Metric = DEFAUTL_METRIC;
354 rt->SubnetBits = Interface->SubnetBits;
356 if( IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
367 * \brief Names for route IOCtl Calls
369 static const char *casIOCtls_Route[] = {
371 "get_type", // Get address type - (void), returns integer type
372 "get_network", // Get network - (void *Data), returns boolean success
373 "set_network", // Set network - (void *Data), returns boolean success
374 "get_nexthop", // Get next hop - (void *Data), returns boolean success
375 "set_nexthop", // Set next hop - (void *Data), returns boolean success
376 "getset_subnetbits", // Get/Set subnet bits - (int *Bits), returns current value
377 "getset_metric", // Get/Set metric - (int *Metric), returns current value
378 "get_interface", // Get interface name - (char *Name), returns name length, NULL OK
383 * \brief IOCtl for /Devices/ip/routes/#
385 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
388 tRoute *rt = Node->ImplPtr;
389 int addrSize = IPStack_GetAddressSize(rt->AddressType);
393 // --- Standard IOCtls (0-3) ---
394 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_Route)
398 return rt->AddressType;
402 if( !CheckMem(Data, addrSize) ) return -1;
403 memcpy(Data, rt->Network, addrSize);
407 if( !CheckMem(Data, addrSize) ) return -1;
408 memcpy(rt->Network, Data, addrSize);
413 if( !CheckMem(Data, addrSize) ) return -1;
414 memcpy(Data, rt->NextHop, addrSize);
418 if( !CheckMem(Data, addrSize) ) return -1;
419 memcpy(rt->NextHop, Data, addrSize);
422 // Get/Set Subnet Bits
425 if( !CheckMem(Data, sizeof(int)) ) return -1;
426 if( *iData < 0 || *iData > addrSize*8 )
428 rt->SubnetBits = *iData;
430 return rt->SubnetBits;
435 if( !CheckMem(Data, sizeof(int)) ) return -1;
436 if( *iData < 0 ) return -1;
441 // Get interface name
444 if( !CheckMem(Data, strlen(rt->Interface->Name) + 1) )
446 strcpy(Data, rt->Interface->Name);
448 return strlen(rt->Interface->Name);