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_Relink(tVFS_Node *Node, const char *OldName, const char *NewName);
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 .Relink = IPStack_RouteDir_Relink,
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_Relink(tVFS_Node *Node, const char *OldName, const char *NewName)
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);
215 if( NewName == NULL )
219 for(tRoute *r = gIP_Routes; r && r != rt; prev = r, r = r->Next);
222 prev->Next = rt->Next;
224 gIP_Routes = rt->Next;
230 int type = _Route_ParseRouteName(NewName, NULL, NULL, NULL);
231 if(type <= 0) return -EINVAL;
232 Uint8 addr[IPStack_GetAddressSize(type)];
234 _Route_ParseRouteName(NewName, addr, &subnet, &metric);
241 tRoute *_Route_FindExactRoute(int Type, void *Network, int Subnet, int Metric)
243 for(tRoute *rt = gIP_Routes; rt; rt = rt->Next)
245 if( rt->AddressType != Type ) continue;
246 if( rt->Metric != Metric ) continue;
247 if( rt->SubnetBits != Subnet ) continue;
248 if( IPStack_CompareAddress(Type, rt->Network, Network, -1) == 0 )
255 int _Route_ParseRouteName(const char *Name, void *Addr, int *SubnetBits, int *Metric)
260 ENTER("sName pAddr pSubnetBits pMetric", Name, Addr, SubnetBits, Metric);
262 ilen = ParseInt(Name, &type);
264 LOG("Type failed to parse");
269 if(Name[ofs] != ':') LEAVE_RET('i', -1);
272 addrlen = IPStack_GetAddressSize(type);
275 if( UnHex(Addr, addrlen, Name + ofs) != addrlen )
280 if(Name[ofs] != ':') LEAVE_RET('i', -1);
283 ilen = ParseInt(Name+ofs, SubnetBits);
285 LOG("Subnet failed to parse");
290 if(Name[ofs] != ':') LEAVE_RET('i', -1);
293 ilen = ParseInt(Name+ofs, Metric);
295 LOG("Metric failed to parse");
300 if(Name[ofs] != '\0') LEAVE_RET('i', -1);
307 * \brief Names for the route list IOCtl Calls
309 static const char *casIOCtls_RouteDir[] = {
311 "locate_route", // Find the best route for an address - struct {int Type, char Address[]} *
316 * \brief IOCtl for /Devices/ip/routes/
318 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
321 ENTER("pNode iID pData", Node, ID, Data);
324 // --- Standard IOCtls (0-3) ---
325 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_RouteDir)
327 case 4: // Locate Route
334 if( !CheckMem(Data, sizeof(int)) )
336 if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
339 Log_Debug("IPStack", "Route_RouteDir_IOCtl - FindRoute %i, %s",
340 data->Type, IPStack_PrintAddress(data->Type, data->Addr) );
341 rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
346 LEAVE('i', rt->Node.Inode);
347 return rt->Node.Inode;
356 * \brief Create a new route entry
357 * \param InterfaceName Name of the interface using this route
359 tRoute *IPStack_Route_Create(int AddrType, void *Network, int SubnetBits, int Metric)
364 // Get the size of the specified address type
365 size = IPStack_GetAddressSize(AddrType);
371 rt = calloc(1, sizeof(tRoute) + size*2 );
374 rt->Node.ImplPtr = rt;
375 rt->Node.Inode = giIP_NextRouteId ++;
377 rt->Node.NumACLs = 1,
378 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
379 rt->Node.Type = &gIP_RouteNodeType;
382 rt->AddressType = AddrType;
383 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
384 rt->SubnetBits = SubnetBits;
385 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
386 rt->Interface = NULL;
388 memcpy(rt->Network, Network, size);
389 memset(rt->NextHop, 0, size);
391 // Clear non-fixed bits
393 Uint8 *data = rt->Network;
396 if( SubnetBits % 8 ) {
397 data[i] &= ~((1 << (8 - SubnetBits % 8)) - 1);
400 memset(data + i, 0, size - i);
405 if( gIP_RoutesEnd ) {
406 gIP_RoutesEnd->Next = rt;
410 gIP_Routes = gIP_RoutesEnd = rt;
413 // Log_Log("IPStack", "Route entry for '%s' created", InterfaceName);
419 * \brief Add and fill a route
421 tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric)
429 tmp = IPStack_Root_FindDir(NULL, Interface);
430 if(!tmp) return NULL;
431 iface = tmp->ImplPtr;
432 if(tmp->Type->Close) tmp->Type->Close(tmp);
435 rt = IPStack_Route_Create(iface->Type, Network, SubnetBits, Metric);
436 if( !rt ) return NULL;
438 addrSize = IPStack_GetAddressSize(iface->Type);
439 rt->Interface = iface;
442 memcpy(rt->NextHop, NextHop, addrSize);
448 * \brief Locates what interface should be used to get directly to an address
450 tRoute *_Route_FindInterfaceRoute(int AddressType, void *Address)
452 tRoute *best = NULL, *rt;
453 int addrSize = IPStack_GetAddressSize(AddressType);
454 for( tInterface *iface = gIP_Interfaces; iface; iface = iface->Next )
456 if( iface->Type != AddressType ) continue;
458 // Check if the address matches
459 if( !IPStack_CompareAddress(AddressType, iface->Address, Address, iface->SubnetBits) )
465 memcpy(rt->Network, iface->Address, addrSize);
466 memset(rt->NextHop, 0, addrSize);
467 rt->Metric = DEFAUTL_METRIC;
468 rt->SubnetBits = iface->SubnetBits;
469 rt->Interface = iface;
473 // More direct routes are preferred
474 if( best->SubnetBits > rt->SubnetBits ) {
475 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
478 // If equally direct, choose the best metric
479 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
480 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
493 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
499 ENTER("iAddressType pInterface sAddress",
500 AddressType, Interface, IPStack_PrintAddress(AddressType, Address));
502 if( Interface && AddressType != Interface->Type ) {
503 LOG("Interface->Type (%i) != AddressType", Interface->Type);
509 addrSize = IPStack_GetAddressSize(AddressType);
511 // Check against explicit routes
512 for( rt = gIP_Routes; rt; rt = rt->Next )
515 if( Interface && rt->Interface && rt->Interface != Interface ) continue;
516 // Check address type
517 if( rt->AddressType != AddressType ) continue;
519 LOG("Checking network %s/%i", IPStack_PrintAddress(AddressType, rt->Network), rt->SubnetBits);
521 // Check if the address matches
522 if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
526 // More direct routes are preferred
527 if( best->SubnetBits > rt->SubnetBits ) {
528 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
531 // If equally direct, choose the best metric
532 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
533 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
541 // Check against implicit routes
542 if( !best && !Interface )
544 best = _Route_FindInterfaceRoute(AddressType, Address);
546 if( !best && Interface )
548 rt = &Interface->Route;
549 // Make sure route is up to date
550 memcpy(rt->Network, Interface->Address, addrSize);
551 memset(rt->NextHop, 0, addrSize);
552 rt->Metric = DEFAUTL_METRIC;
553 rt->SubnetBits = Interface->SubnetBits;
555 if( IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
566 * \brief Names for route IOCtl Calls
568 static const char *casIOCtls_Route[] = {
570 "get_nexthop", // Get next hop - (void *Data), returns boolean success
571 "set_nexthop", // Set next hop - (void *Data), returns boolean success
572 "get_interface", // Get interface name - (char *Name), returns name length, NULL OK
573 "set_interface", // Set interface - (const char *Name)
578 * \brief IOCtl for /Devices/ip/routes/#
580 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
582 tRoute *rt = Node->ImplPtr;
583 int addrSize = IPStack_GetAddressSize(rt->AddressType);
585 ENTER("pNode iID pData", Node, ID, Data);
589 // --- Standard IOCtls (0-3) ---
590 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_Route)
594 if( !CheckMem(Data, addrSize) ) LEAVE_RET('i', -1);
595 memcpy(Data, rt->NextHop, addrSize);
599 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
600 if( !CheckMem(Data, addrSize) ) LEAVE_RET('i', -1);
601 memcpy(rt->NextHop, Data, addrSize);
604 // Get interface name
606 if( !rt->Interface ) {
607 if(Data && !CheckMem(Data, 1) )
614 if( !CheckMem(Data, strlen(rt->Interface->Name) + 1) )
616 strcpy(Data, rt->Interface->Name);
618 LEAVE_RET('i', strlen(rt->Interface->Name));
619 // Set interface name
621 if( Threads_GetUID() != 0 )
623 if( !CheckString(Data) )
629 tmp = IPStack_Root_FindDir(NULL, Data);
632 iface = tmp->ImplPtr;
633 if(tmp->Type->Close) tmp->Type->Close(tmp);
635 if( iface->Type != rt->AddressType )
638 // TODO: Other checks?
640 rt->Interface = iface;