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_IOCtl(tVFS_Node *Node, int ID, void *Data);
24 tRoute *IPStack_Route_Create(const char *InterfaceName);
25 tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric);
26 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address);
27 // - Individual Routes
28 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data);
31 int giIP_NextRouteId = 1;
33 tRoute *gIP_RoutesEnd;
34 tVFS_NodeType gIP_RouteNodeType = {
35 .IOCtl = IPStack_Route_IOCtl
37 tVFS_NodeType gIP_RouteDirNodeType = {
38 .ReadDir = IPStack_RouteDir_ReadDir,
39 .FindDir = IPStack_RouteDir_FindDir,
40 .IOCtl = IPStack_RouteDir_IOCtl
42 tVFS_Node gIP_RouteNode = {
43 .Flags = VFS_FFLAG_DIRECTORY,
46 .ACLs = &gVFS_ACL_EveryoneRX,
47 .Type = &gIP_RouteDirNodeType
52 * \brief ReadDir for the /Devices/ip/routes/ directory
54 char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos)
58 for(rt = gIP_Routes; rt && Pos --; rt = rt->Next);
65 int len = sprintf(NULL, "%i", (int)rt->Node.Inode);
67 sprintf(buf, "%i", (int)rt->Node.Inode);
73 * \brief FindDir for the /Devices/ip/routes/ directory
75 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name)
80 // Zero is invalid, sorry :)
81 if( !num ) return NULL;
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
87 if( Name[1] == ':' ) // TODO: Allow variable length type codes
89 int addrSize = IPStack_GetAddressSize(num);
90 Uint8 addrData[addrSize];
92 // Errof if the size is invalid
93 if( strlen(Name) != 2 + addrSize*2 )
97 // - Error if the address data is not fully hex
98 if( UnHex(addrData, addrSize, Name + 2) != addrSize )
102 rt = IPStack_FindRoute(num, NULL, addrData);
105 // Return the interface node
106 // - Sure it's hijacking it from inteface.c's area, but it's
108 return &rt->Interface->Node;
111 // The list is inherently sorted, so we can do a quick search
112 for(rt = gIP_Routes; rt && rt->Node.Inode < num; rt = rt->Next);
114 // Fast fail end of list / larger number
115 if( !rt || rt->Node.Inode > num )
122 * \brief Names for the route list IOCtl Calls
124 static const char *casIOCtls_RouteDir[] = {
126 "add_route", // Add a route - char *InterfaceName
127 "locate_route", // Find the best route for an address - struct {int Type, char Address[]} *
132 * \brief IOCtl for /Devices/ip/routes/
134 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
138 ENTER("pNode iID pData", Node, ID, Data);
141 // --- Standard IOCtls (0-3) ---
142 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_RouteDir)
145 if( !CheckString(Data) ) LEAVE_RET('i', -1);
146 rt = IPStack_Route_Create(Data);
150 tmp = rt->Node.Inode;
154 case 5: // Locate Route
161 if( !CheckMem(Data, sizeof(int)) )
163 if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
166 Log_Debug("IPStack", "Route_RouteDir_IOCtl - FindRoute %i, %s",
167 data->Type, IPStack_PrintAddress(data->Type, data->Addr) );
168 rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
173 LEAVE('i', rt->Node.Inode);
174 return rt->Node.Inode;
183 * \brief Create a new route entry
184 * \param InterfaceName Name of the interface using this route
186 tRoute *IPStack_Route_Create(const char *InterfaceName)
193 // Note: Oh man! This is such a hack
195 tVFS_Node *node = IPStack_Root_FindDir(NULL, InterfaceName);
197 Log_Debug("IPStack", "IPStack_Route_Create - Unknown interface '%s'\n", InterfaceName);
200 iface = node->ImplPtr;
201 if(node->Type->Close) node->Type->Close(node);
204 // Get the size of the specified address type
205 size = IPStack_GetAddressSize(iface->Type);
211 rt = calloc(1, sizeof(tRoute) + size*2 );
214 rt->Node.ImplPtr = rt;
215 rt->Node.Inode = giIP_NextRouteId ++;
217 rt->Node.NumACLs = 1,
218 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
219 rt->Node.Type = &gIP_RouteNodeType;
222 rt->AddressType = iface->Type;
223 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
225 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
226 rt->Interface = iface;
227 rt->Metric = DEFAUTL_METRIC;
228 memset(rt->Network, 0, size);
229 memset(rt->NextHop, 0, size);
232 if( gIP_RoutesEnd ) {
233 gIP_RoutesEnd->Next = rt;
237 gIP_Routes = gIP_RoutesEnd = rt;
240 Log_Log("IPStack", "Route entry for '%s' created", InterfaceName);
246 * \brief Add and fill a route
248 tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric)
250 tRoute *rt = IPStack_Route_Create(Interface);
253 if( !rt ) return NULL;
255 addrSize = IPStack_GetAddressSize(rt->Interface->Type);
257 memcpy(rt->Network, Network, addrSize);
259 memcpy(rt->NextHop, NextHop, addrSize);
260 rt->SubnetBits = SubnetBits;
269 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
276 ENTER("iAddressType pInterface sAddress",
277 AddressType, Interface, IPStack_PrintAddress(AddressType, Address));
279 if( Interface && AddressType != Interface->Type ) {
280 LOG("Interface->Type (%i) != AddressType", Interface->Type);
286 addrSize = IPStack_GetAddressSize(AddressType);
288 // Check against explicit routes
289 for( rt = gIP_Routes; rt; rt = rt->Next )
292 if( Interface && rt->Interface != Interface ) continue;
293 // Check address type
294 if( rt->AddressType != AddressType ) continue;
296 LOG("Checking network %s/%i", IPStack_PrintAddress(AddressType, rt->Network), rt->SubnetBits);
298 // Check if the address matches
299 if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
303 // More direct routes are preferred
304 if( best->SubnetBits > rt->SubnetBits ) {
305 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
308 // If equally direct, choose the best metric
309 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
310 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
318 // Check against implicit routes
319 if( !best && !Interface )
321 for( iface = gIP_Interfaces; iface; iface = iface->Next )
323 if( Interface && iface != Interface ) continue;
324 if( iface->Type != AddressType ) continue;
327 // Check if the address matches
328 if( !IPStack_CompareAddress(AddressType, iface->Address, Address, iface->SubnetBits) )
332 // More direct routes are preferred
333 if( best->SubnetBits > rt->SubnetBits ) {
334 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
337 // If equally direct, choose the best metric
338 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
339 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
345 memcpy(rt->Network, iface->Address, addrSize);
346 memset(rt->NextHop, 0, addrSize);
347 rt->Metric = DEFAUTL_METRIC;
348 rt->SubnetBits = iface->SubnetBits;
353 if( !best && Interface )
355 rt = &Interface->Route;
356 // Make sure route is up to date
357 memcpy(rt->Network, Interface->Address, addrSize);
358 memset(rt->NextHop, 0, addrSize);
359 rt->Metric = DEFAUTL_METRIC;
360 rt->SubnetBits = Interface->SubnetBits;
362 if( IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
373 * \brief Names for route IOCtl Calls
375 static const char *casIOCtls_Route[] = {
377 "get_type", // Get address type - (void), returns integer type
378 "get_network", // Get network - (void *Data), returns boolean success
379 "set_network", // Set network - (void *Data), returns boolean success
380 "get_nexthop", // Get next hop - (void *Data), returns boolean success
381 "set_nexthop", // Set next hop - (void *Data), returns boolean success
382 "getset_subnetbits", // Get/Set subnet bits - (int *Bits), returns current value
383 "getset_metric", // Get/Set metric - (int *Metric), returns current value
384 "get_interface", // Get interface name - (char *Name), returns name length, NULL OK
389 * \brief IOCtl for /Devices/ip/routes/#
391 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
394 tRoute *rt = Node->ImplPtr;
395 int addrSize = IPStack_GetAddressSize(rt->AddressType);
399 // --- Standard IOCtls (0-3) ---
400 BASE_IOCTLS(DRV_TYPE_MISC, STR(IDENT), VERSION, casIOCtls_Route)
404 return rt->AddressType;
408 if( !CheckMem(Data, addrSize) ) return -1;
409 memcpy(Data, rt->Network, addrSize);
413 if( !CheckMem(Data, addrSize) ) return -1;
414 memcpy(rt->Network, Data, addrSize);
419 if( !CheckMem(Data, addrSize) ) return -1;
420 memcpy(Data, rt->NextHop, addrSize);
424 if( !CheckMem(Data, addrSize) ) return -1;
425 memcpy(rt->NextHop, Data, addrSize);
428 // Get/Set Subnet Bits
431 if( !CheckMem(Data, sizeof(int)) ) return -1;
432 if( *iData < 0 || *iData > addrSize*8 )
434 rt->SubnetBits = *iData;
436 return rt->SubnetBits;
441 if( !CheckMem(Data, sizeof(int)) ) return -1;
442 if( *iData < 0 ) return -1;
447 // Get interface name
450 if( !CheckMem(Data, strlen(rt->Interface->Name) + 1) )
452 strcpy(Data, rt->Interface->Name);
454 return strlen(rt->Interface->Name);