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_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 char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos)
65 for(rt = gIP_Routes; rt && Pos --; rt = rt->Next);
66 if( !rt ) return NULL;
69 int addrlen = IPStack_GetAddressSize(rt->AddressType);
70 int len = sprintf(NULL, "%i::%i:%i", rt->AddressType, rt->SubnetBits, rt->Metric) + addrlen*2;
73 ofs = sprintf(buf, "%i:", rt->AddressType);
74 ofs += Hex(buf+ofs, addrlen, rt->Network);
75 sprintf(buf+ofs, ":%i:%i", rt->SubnetBits, rt->Metric);
81 * \brief FindDir for the /Devices/ip/routes/ directory
83 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name)
85 // Interpret the name as <type>:<addr>, returning the interface for
86 // needed to access that address.
87 // E.g. '@4:0A02000A' - 10.2.0.10
88 // Hm... It could do with a way to have a better address type representation
95 ofs += ParseInt(Name+ofs, &type);
96 int addrSize = IPStack_GetAddressSize(type);
97 Uint8 addrData[addrSize];
100 if( Name[ofs] != ':' ) return NULL;
103 // Error if the size is invalid
104 if( strlen(Name+ofs) != addrSize*2 )
108 // - Error if the address data is not fully hex
109 if( UnHex(addrData, addrSize, Name + ofs) != addrSize )
113 rt = IPStack_FindRoute(type, NULL, addrData);
118 LOG("Why does this route not have a node? trying to find an iface for the next hop");
120 rt = _Route_FindInterfaceRoute(type, rt->NextHop);
122 Log_Notice("Cannot find route to next hop '%s'",
123 IPStack_PrintAddress(type, rt->NextHop));
127 if( !rt->Interface ) {
128 Log_Notice("Routes", "No interface for route %p, what the?", rt);
131 // Return the interface node
132 // - Sure it's hijacking it from inteface.c's area, but it's
134 return &rt->Interface->Node;
136 else if( Name[0] == '#' )
140 ofs = ParseInt(Name+ofs, &num);
141 if( ofs == 1 ) return NULL;
142 if( Name[ofs] != '\0' ) return NULL;
143 if( num < 0) return NULL;
145 for( tRoute *rt = gIP_Routes; rt; rt = rt->Next )
147 if( rt->Node.Inode > num ) return NULL;
148 if( rt->Node.Inode == num ) return &rt->Node;
154 int type = _Route_ParseRouteName(Name, NULL, NULL, NULL);
155 if( type <= 0 ) return NULL;
157 int addrSize = IPStack_GetAddressSize(type);
158 Uint8 addrData[addrSize];
159 int subnet_bits, metric;
161 _Route_ParseRouteName(Name, addrData, &subnet_bits, &metric);
163 tRoute *rt = _Route_FindExactRoute(type, addrData, subnet_bits, metric);
164 if(rt) return &rt->Node;
170 * \brief Create a new route node
172 int IPStack_RouteDir_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
174 if( Flags ) return -EINVAL;
175 if( Threads_GetUID() != 0 ) return -EACCES;
177 int type = _Route_ParseRouteName(Name, NULL, NULL, NULL);
178 if( type <= 0 ) return -EINVAL;
180 int size = IPStack_GetAddressSize(type);
181 Uint8 addrdata[size];
184 _Route_ParseRouteName(Name, addrdata, &subnet, &metric);
186 // Check for duplicates
187 if( _Route_FindExactRoute(type, addrdata, subnet, metric) )
190 IPStack_Route_Create(type, addrdata, subnet, metric);
196 * \brief Rename / Delete a route
198 int IPStack_RouteDir_Unlink(tVFS_Node *Node, const char *OldName)
202 if( Threads_GetUID() != 0 ) return -EACCES;
204 // Get the original route entry
206 int type = _Route_ParseRouteName(OldName, NULL, NULL, NULL);
207 if(type <= 0) return -EINVAL;
208 Uint8 addr[IPStack_GetAddressSize(type)];
210 _Route_ParseRouteName(OldName, addr, &subnet, &metric);
212 rt = _Route_FindExactRoute(type, addr, subnet, metric);
217 for(tRoute *r = gIP_Routes; r && r != rt; prev = r, r = r->Next);
220 prev->Next = rt->Next;
222 gIP_Routes = rt->Next;
227 tRoute *_Route_FindExactRoute(int Type, void *Network, int Subnet, int Metric)
229 for(tRoute *rt = gIP_Routes; rt; rt = rt->Next)
231 if( rt->AddressType != Type ) continue;
232 if( rt->Metric != Metric ) continue;
233 if( rt->SubnetBits != Subnet ) continue;
234 if( IPStack_CompareAddress(Type, rt->Network, Network, -1) == 0 )
241 int _Route_ParseRouteName(const char *Name, void *Addr, int *SubnetBits, int *Metric)
246 ENTER("sName pAddr pSubnetBits pMetric", Name, Addr, SubnetBits, Metric);
248 ilen = ParseInt(Name, &type);
250 LOG("Type failed to parse");
255 if(Name[ofs] != ':') LEAVE_RET('i', -1);
258 addrlen = IPStack_GetAddressSize(type);
261 if( UnHex(Addr, addrlen, Name + ofs) != addrlen )
266 if(Name[ofs] != ':') LEAVE_RET('i', -1);
269 ilen = ParseInt(Name+ofs, SubnetBits);
271 LOG("Subnet failed to parse");
276 if(Name[ofs] != ':') LEAVE_RET('i', -1);
279 ilen = ParseInt(Name+ofs, Metric);
281 LOG("Metric failed to parse");
286 if(Name[ofs] != '\0') LEAVE_RET('i', -1);
293 * \brief Names for the route list IOCtl Calls
295 static const char *casIOCtls_RouteDir[] = {
297 "locate_route", // Find the best route for an address - struct {int Type, char Address[]} *
302 * \brief IOCtl for /Devices/ip/routes/
304 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
307 ENTER("pNode iID pData", Node, ID, Data);
310 // --- Standard IOCtls (0-3) ---
311 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_RouteDir)
313 case 4: // Locate Route
320 if( !CheckMem(Data, sizeof(int)) )
322 if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
325 Log_Debug("IPStack", "Route_RouteDir_IOCtl - FindRoute %i, %s",
326 data->Type, IPStack_PrintAddress(data->Type, data->Addr) );
327 rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
332 LEAVE('i', rt->Node.Inode);
333 return rt->Node.Inode;
342 * \brief Create a new route entry
343 * \param InterfaceName Name of the interface using this route
345 tRoute *IPStack_Route_Create(int AddrType, void *Network, int SubnetBits, int Metric)
350 // Get the size of the specified address type
351 size = IPStack_GetAddressSize(AddrType);
357 rt = calloc(1, sizeof(tRoute) + size*2 );
360 rt->Node.ImplPtr = rt;
361 rt->Node.Inode = giIP_NextRouteId ++;
363 rt->Node.NumACLs = 1,
364 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
365 rt->Node.Type = &gIP_RouteNodeType;
368 rt->AddressType = AddrType;
369 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
370 rt->SubnetBits = SubnetBits;
371 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
372 rt->Interface = NULL;
374 memcpy(rt->Network, Network, size);
375 memset(rt->NextHop, 0, size);
377 // Clear non-fixed bits
379 Uint8 *data = rt->Network;
382 if( SubnetBits % 8 ) {
383 data[i] &= ~((1 << (8 - SubnetBits % 8)) - 1);
386 memset(data + i, 0, size - i);
391 if( gIP_RoutesEnd ) {
392 gIP_RoutesEnd->Next = rt;
396 gIP_Routes = gIP_RoutesEnd = rt;
399 // Log_Log("IPStack", "Route entry for '%s' created", InterfaceName);
405 * \brief Add and fill a route
407 tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric)
415 tmp = IPStack_Root_FindDir(NULL, Interface);
416 if(!tmp) return NULL;
417 iface = tmp->ImplPtr;
418 if(tmp->Type->Close) tmp->Type->Close(tmp);
421 rt = IPStack_Route_Create(iface->Type, Network, SubnetBits, Metric);
422 if( !rt ) return NULL;
424 addrSize = IPStack_GetAddressSize(iface->Type);
425 rt->Interface = iface;
428 memcpy(rt->NextHop, NextHop, addrSize);
434 * \brief Locates what interface should be used to get directly to an address
436 tRoute *_Route_FindInterfaceRoute(int AddressType, void *Address)
438 tRoute *best = NULL, *rt;
439 int addrSize = IPStack_GetAddressSize(AddressType);
440 for( tInterface *iface = gIP_Interfaces; iface; iface = iface->Next )
442 if( iface->Type != AddressType ) continue;
444 // Check if the address matches
445 if( !IPStack_CompareAddress(AddressType, iface->Address, Address, iface->SubnetBits) )
451 memcpy(rt->Network, iface->Address, addrSize);
452 memset(rt->NextHop, 0, addrSize);
453 rt->Metric = DEFAUTL_METRIC;
454 rt->SubnetBits = iface->SubnetBits;
455 rt->Interface = iface;
459 // More direct routes are preferred
460 if( best->SubnetBits > rt->SubnetBits ) {
461 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
464 // If equally direct, choose the best metric
465 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
466 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
479 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
485 ENTER("iAddressType pInterface sAddress",
486 AddressType, Interface, IPStack_PrintAddress(AddressType, Address));
488 if( Interface && AddressType != Interface->Type ) {
489 LOG("Interface->Type (%i) != AddressType", Interface->Type);
495 addrSize = IPStack_GetAddressSize(AddressType);
497 // Check against explicit routes
498 for( rt = gIP_Routes; rt; rt = rt->Next )
501 if( Interface && rt->Interface && rt->Interface != Interface ) continue;
502 // Check address type
503 if( rt->AddressType != AddressType ) continue;
505 LOG("Checking network %s/%i", IPStack_PrintAddress(AddressType, rt->Network), rt->SubnetBits);
507 // Check if the address matches
508 if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
512 // More direct routes are preferred
513 if( best->SubnetBits > rt->SubnetBits ) {
514 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
517 // If equally direct, choose the best metric
518 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
519 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
527 // Check against implicit routes
528 if( !best && !Interface )
530 best = _Route_FindInterfaceRoute(AddressType, Address);
532 if( !best && Interface )
534 rt = &Interface->Route;
535 // Make sure route is up to date
536 memcpy(rt->Network, Interface->Address, addrSize);
537 memset(rt->NextHop, 0, addrSize);
538 rt->Metric = DEFAUTL_METRIC;
539 rt->SubnetBits = Interface->SubnetBits;
541 if( IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
552 * \brief Names for route IOCtl Calls
554 static const char *casIOCtls_Route[] = {
556 "get_nexthop", // Get next hop - (void *Data), returns boolean success
557 "set_nexthop", // Set next hop - (void *Data), returns boolean success
558 "get_interface", // Get interface name - (char *Name), returns name length, NULL OK
559 "set_interface", // Set interface - (const char *Name)
564 * \brief IOCtl for /Devices/ip/routes/#
566 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
568 tRoute *rt = Node->ImplPtr;
569 int addrSize = IPStack_GetAddressSize(rt->AddressType);
571 ENTER("pNode iID pData", Node, ID, Data);
575 // --- Standard IOCtls (0-3) ---
576 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_Route)
580 if( !CheckMem(Data, addrSize) ) LEAVE_RET('i', -1);
581 memcpy(Data, rt->NextHop, addrSize);
585 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
586 if( !CheckMem(Data, addrSize) ) LEAVE_RET('i', -1);
587 memcpy(rt->NextHop, Data, addrSize);
590 // Get interface name
592 if( !rt->Interface ) {
593 if(Data && !CheckMem(Data, 1) )
600 if( !CheckMem(Data, strlen(rt->Interface->Name) + 1) )
602 strcpy(Data, rt->Interface->Name);
604 LEAVE_RET('i', strlen(rt->Interface->Name));
605 // Set interface name
607 if( Threads_GetUID() != 0 )
609 if( !CheckString(Data) )
615 tmp = IPStack_Root_FindDir(NULL, Data);
618 iface = tmp->ImplPtr;
619 if(tmp->Type->Close) tmp->Type->Close(tmp);
621 if( iface->Type != rt->AddressType )
624 // TODO: Other checks?
626 rt->Interface = iface;