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 int IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX]);
21 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name);
22 tVFS_Node *IPStack_RouteDir_MkNod(tVFS_Node *Node, const char *Name, Uint Flags);
23 int IPStack_RouteDir_Unlink(tVFS_Node *Node, const char *OldName);
24 tRoute *_Route_FindExactRoute(int Type, void *Network, int Subnet, int Metric);
25 int _Route_ParseRouteName(const char *Name, void *Addr, int *SubnetBits, int *Metric);
26 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data);
28 tRoute *IPStack_Route_Create(int AddrType, void *Network, int SubnetBits, int Metric);
29 tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric);
30 tRoute *_Route_FindInterfaceRoute(int AddressType, void *Address);
31 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address);
32 // - Individual Routes
33 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data);
36 int giIP_NextRouteId = 1;
38 tRoute *gIP_RoutesEnd;
39 tVFS_NodeType gIP_RouteNodeType = {
40 .IOCtl = IPStack_Route_IOCtl
42 tVFS_NodeType gIP_RouteDirNodeType = {
43 .ReadDir = IPStack_RouteDir_ReadDir,
44 .FindDir = IPStack_RouteDir_FindDir,
45 .MkNod = IPStack_RouteDir_MkNod,
46 .Unlink = IPStack_RouteDir_Unlink,
47 .IOCtl = IPStack_RouteDir_IOCtl
49 tVFS_Node gIP_RouteNode = {
50 .Flags = VFS_FFLAG_DIRECTORY,
53 .ACLs = &gVFS_ACL_EveryoneRX,
54 .Type = &gIP_RouteDirNodeType
59 * \brief ReadDir for the /Devices/ip/routes/ directory
61 int IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
65 for(rt = gIP_Routes; rt && Pos --; rt = rt->Next);
66 if( !rt ) return -EINVAL;
69 int addrlen = IPStack_GetAddressSize(rt->AddressType);
71 ofs = sprintf(Dest, "%i:", rt->AddressType);
72 ofs += Hex(Dest+ofs, addrlen, rt->Network);
73 sprintf(Dest+ofs, ":%i:%i", rt->SubnetBits, rt->Metric);
79 * \brief FindDir for the /Devices/ip/routes/ directory
81 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name)
83 // Interpret the name as <type>:<addr>, returning the interface for
84 // needed to access that address.
85 // E.g. '@4:0A02000A' - 10.2.0.10
86 // Hm... It could do with a way to have a better address type representation
93 ofs += ParseInt(Name+ofs, &type);
94 int addrSize = IPStack_GetAddressSize(type);
95 Uint8 addrData[addrSize];
98 if( Name[ofs] != ':' ) return NULL;
101 // Error if the size is invalid
102 if( strlen(Name+ofs) != addrSize*2 )
106 // - Error if the address data is not fully hex
107 if( UnHex(addrData, addrSize, Name + ofs) != addrSize )
111 rt = IPStack_FindRoute(type, NULL, addrData);
116 LOG("Why does this route not have a node? trying to find an iface for the next hop");
118 rt = _Route_FindInterfaceRoute(type, rt->NextHop);
120 Log_Notice("Cannot find route to next hop '%s'",
121 IPStack_PrintAddress(type, rt->NextHop));
125 if( !rt->Interface ) {
126 Log_Notice("Routes", "No interface for route %p, what the?", rt);
129 // Return the interface node
130 // - Sure it's hijacking it from inteface.c's area, but it's
132 return &rt->Interface->Node;
134 else if( Name[0] == '#' )
138 ofs = ParseInt(Name+ofs, &num);
139 if( ofs == 1 ) return NULL;
140 if( Name[ofs] != '\0' ) return NULL;
141 if( num < 0) return NULL;
143 for( tRoute *rt = gIP_Routes; rt; rt = rt->Next )
145 if( rt->Node.Inode > num ) return NULL;
146 if( rt->Node.Inode == num ) return &rt->Node;
152 int type = _Route_ParseRouteName(Name, NULL, NULL, NULL);
153 if( type <= 0 ) return NULL;
155 int addrSize = IPStack_GetAddressSize(type);
156 Uint8 addrData[addrSize];
157 int subnet_bits, metric;
159 _Route_ParseRouteName(Name, addrData, &subnet_bits, &metric);
161 tRoute *rt = _Route_FindExactRoute(type, addrData, subnet_bits, metric);
162 if(rt) return &rt->Node;
168 * \brief Create a new route node
170 tVFS_Node *IPStack_RouteDir_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
176 if( Threads_GetUID() != 0 ) {
181 int type = _Route_ParseRouteName(Name, NULL, NULL, NULL);
187 int size = IPStack_GetAddressSize(type);
188 Uint8 addrdata[size];
191 _Route_ParseRouteName(Name, addrdata, &subnet, &metric);
193 // Check for duplicates
194 if( _Route_FindExactRoute(type, addrdata, subnet, metric) ) {
199 tRoute *rt = IPStack_Route_Create(type, addrdata, subnet, metric);
200 rt->Node.ReferenceCount ++;
206 * \brief Rename / Delete a route
208 int IPStack_RouteDir_Unlink(tVFS_Node *Node, const char *OldName)
212 if( Threads_GetUID() != 0 ) return -EACCES;
214 // Get the original route entry
216 int type = _Route_ParseRouteName(OldName, NULL, NULL, NULL);
217 if(type <= 0) return -EINVAL;
218 Uint8 addr[IPStack_GetAddressSize(type)];
220 _Route_ParseRouteName(OldName, addr, &subnet, &metric);
222 rt = _Route_FindExactRoute(type, addr, subnet, metric);
227 for(tRoute *r = gIP_Routes; r && r != rt; prev = r, r = r->Next);
230 prev->Next = rt->Next;
232 gIP_Routes = rt->Next;
237 tRoute *_Route_FindExactRoute(int Type, void *Network, int Subnet, int Metric)
239 for(tRoute *rt = gIP_Routes; rt; rt = rt->Next)
241 if( rt->AddressType != Type ) continue;
242 if( rt->Metric != Metric ) continue;
243 if( rt->SubnetBits != Subnet ) continue;
244 if( IPStack_CompareAddress(Type, rt->Network, Network, -1) == 0 )
251 int _Route_ParseRouteName(const char *Name, void *Addr, int *SubnetBits, int *Metric)
256 ENTER("sName pAddr pSubnetBits pMetric", Name, Addr, SubnetBits, Metric);
258 ilen = ParseInt(Name, &type);
260 LOG("Type failed to parse");
265 if(Name[ofs] != ':') LEAVE_RET('i', -1);
268 addrlen = IPStack_GetAddressSize(type);
271 if( UnHex(Addr, addrlen, Name + ofs) != addrlen )
276 if(Name[ofs] != ':') LEAVE_RET('i', -1);
279 ilen = ParseInt(Name+ofs, SubnetBits);
281 LOG("Subnet failed to parse");
286 if(Name[ofs] != ':') LEAVE_RET('i', -1);
289 ilen = ParseInt(Name+ofs, Metric);
291 LOG("Metric failed to parse");
296 if(Name[ofs] != '\0') LEAVE_RET('i', -1);
303 * \brief Names for the route list IOCtl Calls
305 static const char *casIOCtls_RouteDir[] = {
307 "locate_route", // Find the best route for an address - struct {int Type, char Address[]} *
312 * \brief IOCtl for /Devices/ip/routes/
314 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
317 ENTER("pNode iID pData", Node, ID, Data);
320 // --- Standard IOCtls (0-3) ---
321 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_RouteDir)
323 case 4: // Locate Route
330 if( !CheckMem(Data, sizeof(int)) )
332 if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
335 Log_Debug("IPStack", "Route_RouteDir_IOCtl - FindRoute %i, %s",
336 data->Type, IPStack_PrintAddress(data->Type, data->Addr) );
337 rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
342 LEAVE('i', rt->Node.Inode);
343 return rt->Node.Inode;
352 * \brief Create a new route entry
353 * \param InterfaceName Name of the interface using this route
355 tRoute *IPStack_Route_Create(int AddrType, void *Network, int SubnetBits, int Metric)
360 // Get the size of the specified address type
361 size = IPStack_GetAddressSize(AddrType);
367 rt = calloc(1, sizeof(tRoute) + size*2 );
370 rt->Node.ImplPtr = rt;
371 rt->Node.Inode = giIP_NextRouteId ++;
373 rt->Node.NumACLs = 1,
374 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
375 rt->Node.Type = &gIP_RouteNodeType;
378 rt->AddressType = AddrType;
379 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
380 rt->SubnetBits = SubnetBits;
381 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
382 rt->Interface = NULL;
384 memcpy(rt->Network, Network, size);
385 memset(rt->NextHop, 0, size);
387 // Clear non-fixed bits
389 Uint8 *data = rt->Network;
392 if( SubnetBits % 8 ) {
393 data[i] &= ~((1 << (8 - SubnetBits % 8)) - 1);
396 memset(data + i, 0, size - i);
401 if( gIP_RoutesEnd ) {
402 gIP_RoutesEnd->Next = rt;
406 gIP_Routes = gIP_RoutesEnd = rt;
409 // Log_Log("IPStack", "Route entry for '%s' created", InterfaceName);
415 * \brief Add and fill a route
417 tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric)
425 tmp = IPStack_Root_FindDir(NULL, Interface);
426 if(!tmp) return NULL;
427 iface = tmp->ImplPtr;
428 if(tmp->Type->Close) tmp->Type->Close(tmp);
431 rt = IPStack_Route_Create(iface->Type, Network, SubnetBits, Metric);
432 if( !rt ) return NULL;
434 addrSize = IPStack_GetAddressSize(iface->Type);
435 rt->Interface = iface;
438 memcpy(rt->NextHop, NextHop, addrSize);
444 * \brief Locates what interface should be used to get directly to an address
446 tRoute *_Route_FindInterfaceRoute(int AddressType, void *Address)
448 tRoute *best = NULL, *rt;
449 int addrSize = IPStack_GetAddressSize(AddressType);
450 for( tInterface *iface = gIP_Interfaces; iface; iface = iface->Next )
452 if( iface->Type != AddressType ) continue;
454 // Check if the address matches
455 if( !IPStack_CompareAddress(AddressType, iface->Address, Address, iface->SubnetBits) )
461 memcpy(rt->Network, iface->Address, addrSize);
462 memset(rt->NextHop, 0, addrSize);
463 rt->Metric = DEFAUTL_METRIC;
464 rt->SubnetBits = iface->SubnetBits;
465 rt->Interface = iface;
469 // More direct routes are preferred
470 if( best->SubnetBits > rt->SubnetBits ) {
471 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
474 // If equally direct, choose the best metric
475 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
476 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
489 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
495 ENTER("iAddressType pInterface sAddress",
496 AddressType, Interface, IPStack_PrintAddress(AddressType, Address));
498 if( Interface && AddressType != Interface->Type ) {
499 LOG("Interface->Type (%i) != AddressType", Interface->Type);
505 addrSize = IPStack_GetAddressSize(AddressType);
507 // Check against explicit routes
508 for( rt = gIP_Routes; rt; rt = rt->Next )
511 if( Interface && rt->Interface && rt->Interface != Interface ) continue;
512 // Check address type
513 if( rt->AddressType != AddressType ) continue;
515 LOG("Checking network %s/%i", IPStack_PrintAddress(AddressType, rt->Network), rt->SubnetBits);
517 // Check if the address matches
518 if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
522 // More direct routes are preferred
523 if( best->SubnetBits > rt->SubnetBits ) {
524 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
527 // If equally direct, choose the best metric
528 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
529 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
537 // Check against implicit routes
538 if( !best && !Interface )
540 best = _Route_FindInterfaceRoute(AddressType, Address);
542 if( !best && Interface )
544 rt = &Interface->Route;
545 // Make sure route is up to date
546 memcpy(rt->Network, Interface->Address, addrSize);
547 memset(rt->NextHop, 0, addrSize);
548 rt->Metric = DEFAUTL_METRIC;
549 rt->SubnetBits = Interface->SubnetBits;
551 if( IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
562 * \brief Names for route IOCtl Calls
564 static const char *casIOCtls_Route[] = {
566 "get_nexthop", // Get next hop - (void *Data), returns boolean success
567 "set_nexthop", // Set next hop - (void *Data), returns boolean success
568 "get_interface", // Get interface name - (char *Name), returns name length, NULL OK
569 "set_interface", // Set interface - (const char *Name)
574 * \brief IOCtl for /Devices/ip/routes/#
576 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
578 tRoute *rt = Node->ImplPtr;
579 int addrSize = IPStack_GetAddressSize(rt->AddressType);
581 ENTER("pNode iID pData", Node, ID, Data);
585 // --- Standard IOCtls (0-3) ---
586 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_Route)
590 if( !CheckMem(Data, addrSize) ) LEAVE_RET('i', -1);
591 memcpy(Data, rt->NextHop, addrSize);
595 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
596 if( !CheckMem(Data, addrSize) ) LEAVE_RET('i', -1);
597 memcpy(rt->NextHop, Data, addrSize);
600 // Get interface name
602 if( !rt->Interface ) {
603 if(Data && !CheckMem(Data, 1) )
610 if( !CheckMem(Data, strlen(rt->Interface->Name) + 1) )
612 strcpy(Data, rt->Interface->Name);
614 LEAVE_RET('i', strlen(rt->Interface->Name));
615 // Set interface name
617 if( Threads_GetUID() != 0 )
619 if( !CheckString(Data) )
625 tmp = IPStack_Root_FindDir(NULL, Data);
628 iface = tmp->ImplPtr;
629 if(tmp->Type->Close) tmp->Type->Close(tmp);
631 if( iface->Type != rt->AddressType )
634 // TODO: Other checks?
636 rt->Interface = iface;