6 #define VERSION VER2(0,10)
8 #include <tpl_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_Node gIP_RouteNode = {
35 Flags: VFS_FFLAG_DIRECTORY,
38 ACLs: &gVFS_ACL_EveryoneRX,
39 ReadDir: IPStack_RouteDir_ReadDir,
40 FindDir: IPStack_RouteDir_FindDir,
41 IOCtl: IPStack_RouteDir_IOCtl
46 * \brief ReadDir for the /Devices/ip/routes/ directory
48 char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos)
52 for(rt = gIP_Routes; rt && Pos --; rt = rt->Next);
59 int len = sprintf(NULL, "%i", rt->Node.Inode);
61 sprintf(buf, "%i", rt->Node.Inode);
67 * \brief FindDir for the /Devices/ip/routes/ directory
69 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name)
74 // Zero is invalid, sorry :)
75 if( !num ) return NULL;
77 // The list is inherently sorted, so we can do a quick search
78 for(rt = gIP_Routes; rt && rt->Node.Inode < num; rt = rt->Next);
79 // Fast fail on larger number
80 if( rt->Node.Inode > num )
87 * \brief Names for the route list IOCtl Calls
89 static const char *casIOCtls_RouteDir[] = {
91 "add_route", // Add a route - char *InterfaceName
92 "locate_route", // Find the best route for an address - struct {int Type, char Address[]} *
97 * \brief IOCtl for /Devices/ip/routes/
99 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
103 ENTER("pNode iID pData", Node, ID, Data);
106 // --- Standard IOCtls (0-3) ---
108 LEAVE('i', DRV_TYPE_MISC);
109 return DRV_TYPE_MISC;
111 case DRV_IOCTL_IDENT:
112 tmp = ModUtil_SetIdent(Data, STR(IDENT));
116 case DRV_IOCTL_VERSION:
120 case DRV_IOCTL_LOOKUP:
121 tmp = ModUtil_LookupString( (char**)casIOCtls_RouteDir, (char*)Data );
126 if( !CheckString(Data) ) LEAVE_RET('i', -1);
127 rt = IPStack_Route_Create(Data);
131 tmp = rt->Node.Inode;
135 case 5: // Locate Route
142 if( !CheckMem(Data, sizeof(int)) )
144 if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
147 Log_Debug("IPStack", "Route_RouteDir_IOCtl - FindRoute %i, %s",
148 data->Type, IPStack_PrintAddress(data->Type, data->Addr) );
149 rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
154 LEAVE('i', rt->Node.Inode);
155 return rt->Node.Inode;
164 * \brief Create a new route entry
165 * \param InterfaceName Name of the interface using this route
167 tRoute *IPStack_Route_Create(const char *InterfaceName)
174 // Note: Oh man! This is such a hack
176 tVFS_Node *node = IPStack_Root_FindDir(NULL, InterfaceName);
178 Log_Debug("IPStack", "IPStack_Route_Create - Unknown interface '%s'\n", InterfaceName);
181 iface = node->ImplPtr;
182 if(node->Close) node->Close(node);
185 // Get the size of the specified address type
186 size = IPStack_GetAddressSize(iface->Type);
192 rt = calloc(1, sizeof(tRoute) + size*2 );
195 rt->Node.ImplPtr = rt;
196 rt->Node.Inode = giIP_NextRouteId ++;
198 rt->Node.NumACLs = 1,
199 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
200 rt->Node.IOCtl = IPStack_Route_IOCtl;
203 rt->AddressType = iface->Type;
204 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
206 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
207 rt->Interface = iface;
208 rt->Metric = DEFAUTL_METRIC;
209 memset(rt->Network, 0, size);
210 memset(rt->NextHop, 0, size);
213 if( gIP_RoutesEnd ) {
214 gIP_RoutesEnd->Next = rt;
218 gIP_Routes = gIP_RoutesEnd = rt;
221 Log_Log("IPStack", "Route entry for '%s' created", InterfaceName);
227 * \brief Add and fill a route
229 tRoute *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric)
231 tRoute *rt = IPStack_Route_Create(Interface);
234 if( !rt ) return NULL;
236 addrSize = IPStack_GetAddressSize(rt->Interface->Type);
238 memcpy(rt->Network, Network, addrSize);
240 memcpy(rt->NextHop, NextHop, addrSize);
241 rt->SubnetBits = SubnetBits;
250 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
255 ENTER("iAddressType pInterface sAddress",
256 AddressType, Interface, IPStack_PrintAddress(AddressType, Address));
258 if( Interface && AddressType != Interface->Type ) {
259 LOG("Interface->Type (%i) != AddressType", Interface->Type);
264 for( rt = gIP_Routes; rt; rt = rt->Next )
267 if( Interface && rt->Interface != Interface ) continue;
268 // Check address type
269 if( rt->AddressType != AddressType ) continue;
271 LOG("Checking network %s/%i", IPStack_PrintAddress(AddressType, rt->Network), rt->SubnetBits);
273 // Check if the address matches
274 if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
278 // More direct routes are preferred
279 if( best->SubnetBits > rt->SubnetBits ) {
280 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
283 // If equally direct, choose the best metric
284 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
285 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
298 * \brief Names for route IOCtl Calls
300 static const char *casIOCtls_Route[] = {
302 "get_type", // Get address type - (void), returns integer type
303 "get_network", // Get network - (void *Data), returns boolean success
304 "set_network", // Set network - (void *Data), returns boolean success
305 "get_nexthop", // Get next hop - (void *Data), returns boolean success
306 "set_nexthop", // Set next hop - (void *Data), returns boolean success
307 "getset_subnetbits", // Get/Set subnet bits - (int *Bits), returns current value
308 "getset_metric", // Get/Set metric - (int *Metric), returns current value
309 "get_interface", // Get interface name - (char *Name), returns name length, NULL OK
314 * \brief IOCtl for /Devices/ip/routes/#
316 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
320 tRoute *rt = Node->ImplPtr;
321 int addrSize = IPStack_GetAddressSize(rt->AddressType);
325 // --- Standard IOCtls (0-3) ---
327 LEAVE('i', DRV_TYPE_MISC);
328 return DRV_TYPE_MISC;
330 case DRV_IOCTL_IDENT:
331 tmp = ModUtil_SetIdent(Data, STR(IDENT));
335 case DRV_IOCTL_VERSION:
339 case DRV_IOCTL_LOOKUP:
340 tmp = ModUtil_LookupString( (char**)casIOCtls_Route, (char*)Data );
346 return rt->AddressType;
350 if( !CheckMem(Data, addrSize) ) return -1;
351 memcpy(Data, rt->Network, addrSize);
355 if( !CheckMem(Data, addrSize) ) return -1;
356 memcpy(rt->Network, Data, addrSize);
361 if( !CheckMem(Data, addrSize) ) return -1;
362 memcpy(Data, rt->NextHop, addrSize);
366 if( !CheckMem(Data, addrSize) ) return -1;
367 memcpy(rt->NextHop, Data, addrSize);
370 // Get/Set Subnet Bits
373 if( !CheckMem(Data, sizeof(int)) ) return -1;
374 if( *iData < 0 || *iData > addrSize*8 )
376 rt->SubnetBits = *iData;
378 return rt->SubnetBits;
383 if( !CheckMem(Data, sizeof(int)) ) return -1;
384 if( *iData < 0 ) return -1;
389 // Get interface name
392 if( !CheckMem(Data, strlen(rt->Interface->Name) + 1) )
394 strcpy(Data, rt->Interface->Name);
396 return strlen(rt->Interface->Name);