6 #define VERSION VER2(0,10)
8 #include <tpl_drv_common.h>
13 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Filename);
17 char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos);
18 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name);
19 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data);
20 int IPStack_Route_Create(const char *InterfaceName);
21 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address);
22 // - Individual Routes
23 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data);
26 int giIP_NextRouteId = 1;
28 tRoute *gIP_RoutesEnd;
29 tVFS_Node gIP_RouteNode = {
30 Flags: VFS_FFLAG_DIRECTORY,
33 ACLs: &gVFS_ACL_EveryoneRX,
34 ReadDir: IPStack_RouteDir_ReadDir,
35 FindDir: IPStack_RouteDir_FindDir,
36 IOCtl: IPStack_RouteDir_IOCtl
41 * \brief ReadDir for the /Devices/ip/routes/ directory
43 char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos)
47 for(rt = gIP_Routes; rt && Pos --; rt = rt->Next);
54 int len = sprintf(NULL, "%i", rt->Node.Inode);
56 sprintf(buf, "%i", rt->Node.Inode);
62 * \brief FindDir for the /Devices/ip/routes/ directory
64 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name)
69 // Zero is invalid, sorry :)
70 if( !num ) return NULL;
72 // The list is inherently sorted, so we can do a quick search
73 for(rt = gIP_Routes; rt && rt->Node.Inode < num; rt = rt->Next);
74 // Fast fail on larger number
75 if( rt->Node.Inode > num )
82 * \brief Names for the route list IOCtl Calls
84 static const char *casIOCtls_RouteDir[] = {
86 "add_route", // Add a route - char *InterfaceName
87 "locate_route", // Find the best route for an address - struct {int Type, char Address[]} *
92 * \brief IOCtl for /Devices/ip/routes/
94 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
97 ENTER("pNode iID pData", Node, ID, Data);
100 // --- Standard IOCtls (0-3) ---
102 LEAVE('i', DRV_TYPE_MISC);
103 return DRV_TYPE_MISC;
105 case DRV_IOCTL_IDENT:
106 tmp = ModUtil_SetIdent(Data, STR(IDENT));
110 case DRV_IOCTL_VERSION:
114 case DRV_IOCTL_LOOKUP:
115 tmp = ModUtil_LookupString( (char**)casIOCtls_RouteDir, (char*)Data );
120 if( !CheckString(Data) ) LEAVE_RET('i', -1);
121 tmp = IPStack_Route_Create(Data);
125 case 5: // Locate Route
133 if( !CheckMem(Data, sizeof(int)) )
135 if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
138 Log_Debug("IPStack", "Route_RouteDir_IOCtl - FindRoute %i, %s\n",
139 data->Type, IPStack_PrintAddress(data->Type, data->Addr) );
140 rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
145 LEAVE('i', rt->Node.Inode);
146 return rt->Node.Inode;
155 * \brief Create a new route entry
156 * \param InterfaceName Name of the interface using this route
158 int IPStack_Route_Create(const char *InterfaceName)
165 // Note: Oh man! This is such a hack
167 tVFS_Node *node = IPStack_Root_FindDir(NULL, InterfaceName);
169 Log_Debug("IPStack", "IPStack_Route_Create - Unknown interface '%s'\n", InterfaceName);
172 iface = node->ImplPtr;
175 // Get the size of the specified address type
176 size = IPStack_GetAddressSize(iface->Type);
182 rt = calloc(1, sizeof(tRoute) + size*2 );
185 rt->Node.ImplPtr = rt;
186 rt->Node.Inode = giIP_NextRouteId ++;
188 rt->Node.NumACLs = 1,
189 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
190 rt->Node.IOCtl = IPStack_Route_IOCtl;
193 rt->AddressType = iface->Type;
194 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
196 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
197 rt->Interface = iface;
200 if( gIP_RoutesEnd ) {
201 gIP_RoutesEnd->Next = rt;
205 gIP_Routes = gIP_RoutesEnd = rt;
208 Log_Log("IPStack", "Route entry for '%s' created\n", InterfaceName);
210 return rt->Node.Inode;
215 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
220 ENTER("iAddressType pInterface sAddress",
221 AddressType, Interface, IPStack_PrintAddress(AddressType, Address));
223 if( Interface && AddressType != Interface->Type ) {
224 LOG("Interface->Type (%i) != AddressType", Interface->Type);
229 for( rt = gIP_Routes; rt; rt = rt->Next )
232 if( Interface && rt->Interface != Interface ) continue;
233 // Check address type
234 if( rt->AddressType != AddressType ) continue;
236 LOG("Checking network %s/%i", IPStack_PrintAddress(AddressType, rt->Network), rt->SubnetBits);
238 // Check if the address matches
239 if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
243 // More direct routes are preferred
244 if( best->SubnetBits > rt->SubnetBits ) {
245 LOG("Skipped - less direct (%i < %i)", rt->SubnetBits, best->SubnetBits);
248 // If equally direct, choose the best metric
249 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) {
250 LOG("Skipped - higher metric (%i > %i)", rt->Metric, best->Metric);
263 * \brief Names for route IOCtl Calls
265 static const char *casIOCtls_Route[] = {
267 "get_type", // Get address type - (void), returns integer type
268 "get_network", // Get network - (void *Data), returns boolean success
269 "set_network", // Set network - (void *Data), returns boolean success
270 "get_nexthop", // Get next hop - (void *Data), returns boolean success
271 "set_nexthop", // Set next hop - (void *Data), returns boolean success
272 "getset_subnetbits", // Get/Set subnet bits - (int *Bits), returns current value
273 "getset_metric", // Get/Set metric - (int *Metric), returns current value
274 "get_interface", // Get interface name - (char *Name), returns name length, NULL OK
279 * \brief IOCtl for /Devices/ip/routes/#
281 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
285 tRoute *rt = Node->ImplPtr;
286 int addrSize = IPStack_GetAddressSize(rt->AddressType);
290 // --- Standard IOCtls (0-3) ---
292 LEAVE('i', DRV_TYPE_MISC);
293 return DRV_TYPE_MISC;
295 case DRV_IOCTL_IDENT:
296 tmp = ModUtil_SetIdent(Data, STR(IDENT));
300 case DRV_IOCTL_VERSION:
304 case DRV_IOCTL_LOOKUP:
305 tmp = ModUtil_LookupString( (char**)casIOCtls_Route, (char*)Data );
311 return rt->AddressType;
315 if( !CheckMem(Data, addrSize) ) return -1;
316 memcpy(Data, rt->Network, addrSize);
320 if( !CheckMem(Data, addrSize) ) return -1;
321 memcpy(rt->Network, Data, addrSize);
326 if( !CheckMem(Data, addrSize) ) return -1;
327 memcpy(Data, rt->NextHop, addrSize);
331 if( !CheckMem(Data, addrSize) ) return -1;
332 memcpy(rt->NextHop, Data, addrSize);
335 // Get/Set Subnet Bits
338 if( !CheckMem(Data, sizeof(int)) ) return -1;
339 if( *iData < 0 || *iData > addrSize*8 )
341 rt->SubnetBits = *iData;
343 return rt->SubnetBits;
348 if( !CheckMem(Data, sizeof(int)) ) return -1;
349 if( *iData < 0 ) return -1;
354 // Get interface name
357 if( !CheckMem(Data, strlen(rt->Interface->Name) + 1) )
359 strcpy(Data, rt->Interface->Name);
361 return strlen(rt->Interface->Name);