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 *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address);
31 // - Individual Routes
32 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data);
35 int giIP_NextRouteId = 1;
37 tRoute *gIP_RoutesEnd;
38 tVFS_NodeType gIP_RouteNodeType = {
39 .IOCtl = IPStack_Route_IOCtl
41 tVFS_NodeType gIP_RouteDirNodeType = {
42 .ReadDir = IPStack_RouteDir_ReadDir,
43 .FindDir = IPStack_RouteDir_FindDir,
44 .MkNod = IPStack_RouteDir_MkNod,
45 .Relink = IPStack_RouteDir_Relink,
46 .IOCtl = IPStack_RouteDir_IOCtl
48 tVFS_Node gIP_RouteNode = {
49 .Flags = VFS_FFLAG_DIRECTORY,
52 .ACLs = &gVFS_ACL_EveryoneRX,
53 .Type = &gIP_RouteDirNodeType
58 * \brief ReadDir for the /Devices/ip/routes/ directory
60 char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos)
64 for(rt = gIP_Routes; rt && Pos --; rt = rt->Next);
65 if( !rt ) return NULL;
68 int addrlen = IPStack_GetAddressSize(rt->AddressType);
69 int len = sprintf(NULL, "%i::%i:%i", rt->AddressType, rt->SubnetBits, rt->Metric) + addrlen*2;
72 ofs = sprintf(buf, "%i:", rt->AddressType);
73 ofs += Hex(buf+ofs, addrlen, rt->Network);
74 sprintf(buf+ofs, ":%i:%i", rt->SubnetBits, rt->Metric);
80 * \brief FindDir for the /Devices/ip/routes/ directory
82 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name)
84 // Interpret the name as <type>:<addr>, returning the interface for
85 // needed to access that address.
86 // E.g. '@4:0A02000A' - 10.2.0.10
87 // Hm... It could do with a way to have a better address type representation
94 ofs += ParseInt(Name+ofs, &type);
95 int addrSize = IPStack_GetAddressSize(type);
96 Uint8 addrData[addrSize];
99 if( Name[ofs] != ':' ) return NULL;
102 // Error if the size is invalid
103 if( strlen(Name+ofs) != addrSize*2 )
107 // - Error if the address data is not fully hex
108 if( UnHex(addrData, addrSize, Name + ofs) != addrSize )
112 rt = IPStack_FindRoute(type, NULL, addrData);
117 // Return the interface node
118 // - Sure it's hijacking it from inteface.c's area, but it's
120 return &rt->Interface->Node;
127 else if( Name[0] == '#' )
131 ofs = ParseInt(Name+ofs, &num);
132 if( ofs == 1 ) return NULL;
133 if( Name[ofs] != '\0' ) return NULL;
134 if( num < 0) return NULL;
136 for( tRoute *rt = gIP_Routes; rt; rt = rt->Next )
138 if( rt->Node.Inode > num ) return NULL;
139 if( rt->Node.Inode == num ) return &rt->Node;
145 int type = _Route_ParseRouteName(Name, NULL, NULL, NULL);
146 if( type <= 0 ) return NULL;
148 int addrSize = IPStack_GetAddressSize(type);
149 Uint8 addrData[addrSize];
150 int subnet_bits, metric;
152 _Route_ParseRouteName(Name, addrData, &subnet_bits, &metric);
154 tRoute *rt = _Route_FindExactRoute(type, addrData, subnet_bits, metric);
155 if(rt) return &rt->Node;
161 * \brief Create a new route node
163 int IPStack_RouteDir_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
165 if( Flags ) return -EINVAL;
166 if( Threads_GetUID() != 0 ) return -EACCES;
168 int type = _Route_ParseRouteName(Name, NULL, NULL, NULL);
169 if( type <= 0 ) return -EINVAL;
171 int size = IPStack_GetAddressSize(type);
172 Uint8 addrdata[size];
175 _Route_ParseRouteName(Name, addrdata, &subnet, &metric);
177 // Check for duplicates
178 if( _Route_FindExactRoute(type, addrdata, subnet, metric) )
181 IPStack_Route_Create(type, addrdata, subnet, metric);
187 * \brief Rename / Delete a route
189 int IPStack_RouteDir_Relink(tVFS_Node *Node, const char *OldName, const char *NewName)
193 if( Threads_GetUID() != 0 ) return -EACCES;
195 // Get the original route entry
197 int type = _Route_ParseRouteName(OldName, NULL, NULL, NULL);
198 if(type <= 0) return -EINVAL;
199 Uint8 addr[IPStack_GetAddressSize(type)];
201 _Route_ParseRouteName(OldName, addr, &subnet, &metric);
203 rt = _Route_FindExactRoute(type, addr, subnet, metric);
206 if( NewName == NULL )
210 for(tRoute *r = gIP_Routes; r && r != rt; prev = r, r = r->Next);
213 prev->Next = rt->Next;
215 gIP_Routes = rt->Next;
221 int type = _Route_ParseRouteName(NewName, NULL, NULL, NULL);
222 if(type <= 0) return -EINVAL;
223 Uint8 addr[IPStack_GetAddressSize(type)];
225 _Route_ParseRouteName(NewName, addr, &subnet, &metric);
232 tRoute *_Route_FindExactRoute(int Type, void *Network, int Subnet, int Metric)
234 for(tRoute *rt = gIP_Routes; rt; rt = rt->Next)
236 if( rt->AddressType != Type ) continue;
237 if( rt->Metric != Metric ) continue;
238 if( rt->SubnetBits != Subnet ) continue;
239 if( IPStack_CompareAddress(Type, rt->Network, Network, -1) == 0 )
246 int _Route_ParseRouteName(const char *Name, void *Addr, int *SubnetBits, int *Metric)
251 ENTER("sName pAddr pSubnetBits pMetric", Name, Addr, SubnetBits, Metric);
253 ilen = ParseInt(Name, &type);
255 LOG("Type failed to parse");
260 if(Name[ofs] != ':') LEAVE_RET('i', -1);
263 addrlen = IPStack_GetAddressSize(type);
266 if( UnHex(Addr, addrlen, Name + ofs) != addrlen )
271 if(Name[ofs] != ':') LEAVE_RET('i', -1);
274 ilen = ParseInt(Name+ofs, SubnetBits);
276 LOG("Subnet failed to parse");
281 if(Name[ofs] != ':') LEAVE_RET('i', -1);
284 ilen = ParseInt(Name+ofs, Metric);
286 LOG("Metric failed to parse");
291 if(Name[ofs] != '\0') LEAVE_RET('i', -1);
298 * \brief Names for the route list IOCtl Calls
300 static const char *casIOCtls_RouteDir[] = {
302 "locate_route", // Find the best route for an address - struct {int Type, char Address[]} *
307 * \brief IOCtl for /Devices/ip/routes/
309 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
312 ENTER("pNode iID pData", Node, ID, Data);
315 // --- Standard IOCtls (0-3) ---
316 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_RouteDir)
318 case 4: // Locate Route
325 if( !CheckMem(Data, sizeof(int)) )
327 if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
330 Log_Debug("IPStack", "Route_RouteDir_IOCtl - FindRoute %i, %s",
331 data->Type, IPStack_PrintAddress(data->Type, data->Addr) );
332 rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
337 LEAVE('i', rt->Node.Inode);
338 return rt->Node.Inode;
347 * \brief Create a new route entry
348 * \param InterfaceName Name of the interface using this route
350 tRoute *IPStack_Route_Create(int AddrType, void *Network, int SubnetBits, int Metric)
355 // Get the size of the specified address type
356 size = IPStack_GetAddressSize(AddrType);
362 rt = calloc(1, sizeof(tRoute) + size*2 );
365 rt->Node.ImplPtr = rt;
366 rt->Node.Inode = giIP_NextRouteId ++;
368 rt->Node.NumACLs = 1,
369 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
370 rt->Node.Type = &gIP_RouteNodeType;
373 rt->AddressType = AddrType;
374 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
375 rt->SubnetBits = SubnetBits;
376 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
377 rt->Interface = NULL;
379 memcpy(rt->Network, Network, size);
380 memset(rt->NextHop, 0, size);
382 // Clear non-fixed bits
384 Uint8 *data = rt->Network;
387 if( SubnetBits % 8 ) {
388 data[i] &= ~((1 << (8 - SubnetBits % 8)) - 1);
391 memset(data + i, 0, size - i);
396 if( gIP_RoutesEnd ) {
397 gIP_RoutesEnd->Next = rt;
401 gIP_Routes = gIP_RoutesEnd = rt;
404 // Log_Log("IPStack", "Route entry for '%s' created", InterfaceName);
410 * \brief Add and fill a route
412 tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric)
420 tmp = IPStack_Root_FindDir(NULL, Interface);
421 if(!tmp) return NULL;
422 iface = tmp->ImplPtr;
423 if(tmp->Type->Close) tmp->Type->Close(tmp);
426 rt = IPStack_Route_Create(iface->Type, Network, SubnetBits, Metric);
427 if( !rt ) return NULL;
429 addrSize = IPStack_GetAddressSize(iface->Type);
430 rt->Interface = iface;
433 memcpy(rt->NextHop, NextHop, addrSize);
440 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
447 ENTER("iAddressType pInterface sAddress",
448 AddressType, Interface, IPStack_PrintAddress(AddressType, Address));
450 if( Interface && AddressType != Interface->Type ) {
451 LOG("Interface->Type (%i) != AddressType", Interface->Type);
457 addrSize = IPStack_GetAddressSize(AddressType);
459 // Check against explicit routes
460 for( rt = gIP_Routes; rt; rt = rt->Next )
463 if( Interface && rt->Interface != Interface ) continue;
464 // Check address type
465 if( rt->AddressType != AddressType ) continue;
467 LOG("Checking network %s/%i", IPStack_PrintAddress(AddressType, rt->Network), rt->SubnetBits);
469 // Check if the address matches
470 if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
474 // More direct routes are preferred
475 if( best->SubnetBits > rt->SubnetBits ) {
476 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
479 // If equally direct, choose the best metric
480 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
481 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
489 // Check against implicit routes
490 if( !best && !Interface )
492 for( iface = gIP_Interfaces; iface; iface = iface->Next )
494 if( Interface && iface != Interface ) continue;
495 if( iface->Type != AddressType ) continue;
498 // Check if the address matches
499 if( !IPStack_CompareAddress(AddressType, iface->Address, Address, iface->SubnetBits) )
503 // More direct routes are preferred
504 if( best->SubnetBits > rt->SubnetBits ) {
505 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
508 // If equally direct, choose the best metric
509 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
510 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
516 memcpy(rt->Network, iface->Address, addrSize);
517 memset(rt->NextHop, 0, addrSize);
518 rt->Metric = DEFAUTL_METRIC;
519 rt->SubnetBits = iface->SubnetBits;
524 if( !best && Interface )
526 rt = &Interface->Route;
527 // Make sure route is up to date
528 memcpy(rt->Network, Interface->Address, addrSize);
529 memset(rt->NextHop, 0, addrSize);
530 rt->Metric = DEFAUTL_METRIC;
531 rt->SubnetBits = Interface->SubnetBits;
533 if( IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
544 * \brief Names for route IOCtl Calls
546 static const char *casIOCtls_Route[] = {
548 "get_nexthop", // Get next hop - (void *Data), returns boolean success
549 "set_nexthop", // Set next hop - (void *Data), returns boolean success
550 "get_interface", // Get interface name - (char *Name), returns name length, NULL OK
551 "set_interface", // Set interface - (const char *Name)
556 * \brief IOCtl for /Devices/ip/routes/#
558 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
560 tRoute *rt = Node->ImplPtr;
561 int addrSize = IPStack_GetAddressSize(rt->AddressType);
565 // --- Standard IOCtls (0-3) ---
566 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_Route)
570 if( !CheckMem(Data, addrSize) ) return -1;
571 memcpy(Data, rt->NextHop, addrSize);
575 if( Threads_GetUID() != 0 ) return -1;
576 if( !CheckMem(Data, addrSize) ) return -1;
577 memcpy(rt->NextHop, Data, addrSize);
580 // Get interface name
582 if( !rt->Interface ) {
583 if(Data && !CheckMem(Data, 1) )
590 if( !CheckMem(Data, strlen(rt->Interface->Name) + 1) )
592 strcpy(Data, rt->Interface->Name);
594 return strlen(rt->Interface->Name);
595 // Set interface name
597 if( Threads_GetUID() != 0 )
599 if( !CheckString(Data) )
605 tmp = IPStack_Root_FindDir(NULL, Data);
608 iface = tmp->ImplPtr;
609 if(tmp->Type->Close) tmp->Type->Close(tmp);
611 if( iface->Type != rt->AddressType )
614 // TODO: Other checks?
616 rt->Interface = iface;