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, Uint Flags);
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, const void *Address);
31 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, const 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, Uint Flags)
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 void *nextrt = _Route_FindInterfaceRoute(type, rt->NextHop);
120 Log_Notice("Cannot find route to next hop '%s'",
121 IPStack_PrintAddress(type, rt->NextHop));
126 if( !rt->Interface ) {
127 Log_Notice("Routes", "No interface for route %p, what the?", rt);
130 // Return the interface node
131 // - Sure it's hijacking it from inteface.c's area, but it's
133 return &rt->Interface->Node;
135 else if( Name[0] == '#' )
139 ofs = ParseInt(Name+ofs, &num);
140 if( ofs == 1 ) return NULL;
141 if( Name[ofs] != '\0' ) return NULL;
142 if( num < 0) return NULL;
144 for( tRoute *rt = gIP_Routes; rt; rt = rt->Next )
146 if( rt->Node.Inode > num ) return NULL;
147 if( rt->Node.Inode == num ) return &rt->Node;
153 int type = _Route_ParseRouteName(Name, NULL, NULL, NULL);
154 if( type <= 0 ) return NULL;
156 int addrSize = IPStack_GetAddressSize(type);
157 Uint8 addrData[addrSize];
158 int subnet_bits, metric;
160 _Route_ParseRouteName(Name, addrData, &subnet_bits, &metric);
162 tRoute *rt = _Route_FindExactRoute(type, addrData, subnet_bits, metric);
163 if(rt) return &rt->Node;
169 * \brief Create a new route node
171 tVFS_Node *IPStack_RouteDir_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
177 if( Threads_GetUID() != 0 ) {
182 int type = _Route_ParseRouteName(Name, NULL, NULL, NULL);
188 int size = IPStack_GetAddressSize(type);
189 Uint8 addrdata[size];
192 _Route_ParseRouteName(Name, addrdata, &subnet, &metric);
194 // Check for duplicates
195 if( _Route_FindExactRoute(type, addrdata, subnet, metric) ) {
200 tRoute *rt = IPStack_Route_Create(type, addrdata, subnet, metric);
201 rt->Node.ReferenceCount ++;
207 * \brief Rename / Delete a route
209 int IPStack_RouteDir_Unlink(tVFS_Node *Node, const char *OldName)
213 if( Threads_GetUID() != 0 ) return -EACCES;
215 // Get the original route entry
217 int type = _Route_ParseRouteName(OldName, NULL, NULL, NULL);
218 if(type <= 0) return -EINVAL;
219 Uint8 addr[IPStack_GetAddressSize(type)];
221 _Route_ParseRouteName(OldName, addr, &subnet, &metric);
223 rt = _Route_FindExactRoute(type, addr, subnet, metric);
228 for(tRoute *r = gIP_Routes; r && r != rt; prev = r, r = r->Next);
231 prev->Next = rt->Next;
233 gIP_Routes = rt->Next;
238 tRoute *_Route_FindExactRoute(int Type, void *Network, int Subnet, int Metric)
240 for(tRoute *rt = gIP_Routes; rt; rt = rt->Next)
242 if( rt->AddressType != Type ) continue;
243 if( rt->Metric != Metric ) continue;
244 if( rt->SubnetBits != Subnet ) continue;
245 if( IPStack_CompareAddress(Type, rt->Network, Network, -1) == 0 )
252 int _Route_ParseRouteName(const char *Name, void *Addr, int *SubnetBits, int *Metric)
257 ENTER("sName pAddr pSubnetBits pMetric", Name, Addr, SubnetBits, Metric);
259 ilen = ParseInt(Name, &type);
261 LOG("Type failed to parse");
266 if(Name[ofs] != ':') LEAVE_RET('i', -1);
269 addrlen = IPStack_GetAddressSize(type);
272 if( UnHex(Addr, addrlen, Name + ofs) != addrlen )
277 if(Name[ofs] != ':') LEAVE_RET('i', -1);
280 ilen = ParseInt(Name+ofs, SubnetBits);
282 LOG("Subnet failed to parse");
287 if(Name[ofs] != ':') LEAVE_RET('i', -1);
290 ilen = ParseInt(Name+ofs, Metric);
292 LOG("Metric failed to parse");
297 if(Name[ofs] != '\0') LEAVE_RET('i', -1);
304 * \brief Names for the route list IOCtl Calls
306 static const char *casIOCtls_RouteDir[] = {
308 "locate_route", // Find the best route for an address - struct {int Type, char Address[]} *
313 * \brief IOCtl for /Devices/ip/routes/
315 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
318 ENTER("pNode iID pData", Node, ID, Data);
321 // --- Standard IOCtls (0-3) ---
322 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_RouteDir)
324 case 4: // Locate Route
331 if( !CheckMem(Data, sizeof(int)) )
333 if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
336 Log_Debug("IPStack", "Route_RouteDir_IOCtl - FindRoute %i, %s",
337 data->Type, IPStack_PrintAddress(data->Type, data->Addr) );
338 rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
343 LEAVE('i', rt->Node.Inode);
344 return rt->Node.Inode;
353 * \brief Create a new route entry
354 * \param InterfaceName Name of the interface using this route
356 tRoute *IPStack_Route_Create(int AddrType, void *Network, int SubnetBits, int Metric)
361 // Get the size of the specified address type
362 size = IPStack_GetAddressSize(AddrType);
368 rt = calloc(1, sizeof(tRoute) + size*2 );
371 rt->Node.ImplPtr = rt;
372 rt->Node.Inode = giIP_NextRouteId ++;
374 rt->Node.NumACLs = 1,
375 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
376 rt->Node.Type = &gIP_RouteNodeType;
379 rt->AddressType = AddrType;
380 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
381 rt->SubnetBits = SubnetBits;
382 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
383 rt->Interface = NULL;
385 memcpy(rt->Network, Network, size);
386 memset(rt->NextHop, 0, size);
388 // Clear non-fixed bits
390 Uint8 *data = rt->Network;
393 if( SubnetBits % 8 ) {
394 data[i] &= ~((1 << (8 - SubnetBits % 8)) - 1);
397 memset(data + i, 0, size - i);
402 if( gIP_RoutesEnd ) {
403 gIP_RoutesEnd->Next = rt;
407 gIP_Routes = gIP_RoutesEnd = rt;
410 // Log_Log("IPStack", "Route entry for '%s' created", InterfaceName);
416 * \brief Add and fill a route
418 tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric)
426 tmp = IPStack_Root_FindDir(NULL, Interface);
427 if(!tmp) return NULL;
428 iface = tmp->ImplPtr;
429 if(tmp->Type->Close) tmp->Type->Close(tmp);
432 rt = IPStack_Route_Create(iface->Type, Network, SubnetBits, Metric);
433 if( !rt ) return NULL;
435 addrSize = IPStack_GetAddressSize(iface->Type);
436 rt->Interface = iface;
439 memcpy(rt->NextHop, NextHop, addrSize);
445 * \brief Locates what interface should be used to get directly to an address
447 tRoute *_Route_FindInterfaceRoute(int AddressType, const void *Address)
449 tRoute *best = NULL, *rt;
450 int addrSize = IPStack_GetAddressSize(AddressType);
451 for( tInterface *iface = gIP_Interfaces; iface; iface = iface->Next )
453 if( iface->Type != AddressType ) continue;
455 // Check if the address matches
456 if( !IPStack_CompareAddress(AddressType, iface->Address, Address, iface->SubnetBits) )
462 memcpy(rt->Network, iface->Address, addrSize);
463 memset(rt->NextHop, 0, addrSize);
464 rt->Metric = DEFAUTL_METRIC;
465 rt->SubnetBits = iface->SubnetBits;
466 rt->Interface = iface;
470 // More direct routes are preferred
471 if( best->SubnetBits > rt->SubnetBits ) {
472 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
475 // If equally direct, choose the best metric
476 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
477 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
490 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, const void *Address)
496 ENTER("iAddressType pInterface sAddress",
497 AddressType, Interface, IPStack_PrintAddress(AddressType, Address));
499 if( Interface && AddressType != Interface->Type ) {
500 LOG("Interface->Type (%i) != AddressType", Interface->Type);
506 addrSize = IPStack_GetAddressSize(AddressType);
508 // Check against explicit routes
509 for( rt = gIP_Routes; rt; rt = rt->Next )
512 if( Interface && rt->Interface && rt->Interface != Interface ) continue;
513 // Check address type
514 if( rt->AddressType != AddressType ) continue;
516 LOG("Checking network %s/%i", IPStack_PrintAddress(AddressType, rt->Network), rt->SubnetBits);
518 // Check if the address matches
519 if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
523 // More direct routes are preferred
524 if( best->SubnetBits > rt->SubnetBits ) {
525 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
528 // If equally direct, choose the best metric
529 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
530 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
538 // Check against implicit routes
539 if( !best && !Interface )
541 best = _Route_FindInterfaceRoute(AddressType, Address);
543 if( !best && Interface )
545 rt = &Interface->Route;
546 // Make sure route is up to date
547 memcpy(rt->Network, Interface->Address, addrSize);
548 memset(rt->NextHop, 0, addrSize);
549 rt->Metric = DEFAUTL_METRIC;
550 rt->SubnetBits = Interface->SubnetBits;
552 if( IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
563 * \brief Names for route IOCtl Calls
565 static const char *casIOCtls_Route[] = {
567 "get_nexthop", // Get next hop - (void *Data), returns boolean success
568 "set_nexthop", // Set next hop - (void *Data), returns boolean success
569 "get_interface", // Get interface name - (char *Name), returns name length, NULL OK
570 "set_interface", // Set interface - (const char *Name)
575 * \brief IOCtl for /Devices/ip/routes/#
577 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
579 tRoute *rt = Node->ImplPtr;
580 int addrSize = IPStack_GetAddressSize(rt->AddressType);
582 ENTER("pNode iID pData", Node, ID, Data);
586 // --- Standard IOCtls (0-3) ---
587 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_Route)
591 if( !CheckMem(Data, addrSize) ) LEAVE_RET('i', -1);
592 memcpy(Data, rt->NextHop, addrSize);
596 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
597 if( !CheckMem(Data, addrSize) ) LEAVE_RET('i', -1);
598 memcpy(rt->NextHop, Data, addrSize);
601 // Get interface name
603 if( !rt->Interface ) {
604 if(Data && !CheckMem(Data, 1) )
611 if( !CheckMem(Data, strlen(rt->Interface->Name) + 1) )
613 strcpy(Data, rt->Interface->Name);
615 LEAVE_RET('i', strlen(rt->Interface->Name));
616 // Set interface name
618 if( Threads_GetUID() != 0 )
620 if( !CheckString(Data) )
626 tmp = IPStack_Root_FindDir(NULL, Data);
629 iface = tmp->ImplPtr;
630 if(tmp->Type->Close) tmp->Type->Close(tmp);
632 if( iface->Type != rt->AddressType )
635 // TODO: Other checks?
637 rt->Interface = iface;