6 #define VERSION VER2(0,10)
8 #include <tpl_drv_common.h>
13 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Filename);
16 typedef struct sRoute {
21 tInterface *Interface; // Interface for this route
22 int AddressType; // 0: Invalid, 4: IPv4, 6: IPv4
23 void *Network; // Network - Pointer to tIPv4/tIPv6/... at end of structure
24 int SubnetBits; // Number of bits in \a Network that are valid
25 void *NextHop; // Next Hop address - Pointer to tIPv4/tIPv6/... at end of structure
26 int Metric; // Route priority
31 char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos);
32 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name);
33 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data);
34 int IPStack_Route_Create(const char *InterfaceName);
35 tRoute *IPStack_FindRoute(int AddressType, void *Address);
36 // - Individual Routes
37 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data);
40 int giIP_NextRouteId = 1;
42 tRoute *gIP_RoutesEnd;
43 tVFS_Node gIP_RouteNode = {
44 Flags: VFS_FFLAG_DIRECTORY,
47 ACLs: &gVFS_ACL_EveryoneRX,
48 ReadDir: IPStack_RouteDir_ReadDir,
49 FindDir: IPStack_RouteDir_FindDir,
50 IOCtl: IPStack_RouteDir_IOCtl
55 * \brief ReadDir for the /Devices/ip/routes/ directory
57 char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos)
61 for(rt = gIP_Routes; rt && Pos --; rt = rt->Next);
68 int len = sprintf(NULL, "%i", rt->Node.Inode);
70 sprintf(buf, "%i", rt->Node.Inode);
76 * \brief FindDir for the /Devices/ip/routes/ directory
78 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name)
83 // Zero is invalid, sorry :)
84 if( !num ) return NULL;
86 // The list is inherently sorted, so we can do a quick search
87 for(rt = gIP_Routes; rt && rt->Node.Inode < num; rt = rt->Next);
88 // Fast fail on larger number
89 if( rt->Node.Inode > num )
96 * \brief Names for the route list IOCtl Calls
98 static const char *casIOCtls_RouteDir[] = {
100 "add_route", // Add a route - char *InterfaceName
101 "locate_route", // Find the best route for an address - struct {int Type, char Address[]} *
106 * \brief IOCtl for /Devices/ip/routes/
108 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
113 // --- Standard IOCtls (0-3) ---
115 LEAVE('i', DRV_TYPE_MISC);
116 return DRV_TYPE_MISC;
118 case DRV_IOCTL_IDENT:
119 tmp = ModUtil_SetIdent(Data, STR(IDENT));
123 case DRV_IOCTL_VERSION:
127 case DRV_IOCTL_LOOKUP:
128 tmp = ModUtil_LookupString( (char**)casIOCtls_RouteDir, (char*)Data );
133 if( !CheckString(Data) ) return -1;
134 return IPStack_Route_Create(Data);
136 case 5: // Locate Route
144 if( !CheckMem(Data, sizeof(int)) )
146 if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
149 rt = IPStack_FindRoute(data->Type, data->Addr);
154 return rt->Node.Inode;
162 * \brief Create a new route entry
163 * \param InterfaceName Name of the interface using this route
165 int IPStack_Route_Create(const char *InterfaceName)
172 // Note: Oh man! This is such a hack
174 tVFS_Node *node = IPStack_Root_FindDir(NULL, InterfaceName);
175 if( !node ) return 0;
176 iface = node->ImplPtr;
179 // Get the size of the specified address type
180 size = IPStack_GetAddressSize(iface->Type);
186 rt = calloc(1, sizeof(tRoute) + size*2 );
189 rt->Node.ImplPtr = rt;
190 rt->Node.Inode = giIP_NextRouteId ++;
192 rt->Node.NumACLs = 1,
193 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
194 rt->Node.IOCtl = IPStack_Route_IOCtl;
197 rt->AddressType = iface->Type;
198 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
200 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
201 rt->Interface = iface;
204 if( gIP_RoutesEnd ) {
205 gIP_RoutesEnd->Next = rt;
209 gIP_Routes = gIP_RoutesEnd = rt;
212 return rt->Node.Inode;
217 tRoute *IPStack_FindRoute(int AddressType, void *Address)
222 for( rt = gIP_Routes; rt; rt = rt->Next )
224 if( rt->AddressType != AddressType ) continue;
226 if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
230 // More direct routes are preferred
231 if( best->SubnetBits > rt->SubnetBits ) continue;
232 // If equally direct, choose the best metric
233 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) continue;
243 * \brief Names for route IOCtl Calls
245 static const char *casIOCtls_Route[] = {
247 "get_network", // Get network - (void *Data), returns boolean success
248 "set_network", // Set network - (void *Data), returns boolean success
249 "get_nexthop", // Get next hop - (void *Data), returns boolean success
250 "set_nexthop", // Set next hop - (void *Data), returns boolean success
251 "getset_subnetbits", // Get/Set subnet bits - (int *Bits), returns current value
252 "getset_metric", // Get/Set metric - (int *Metric), returns current value
253 "get_interface", // Get interface name - (char *Name), returns name length, NULL OK
258 * \brief IOCtl for /Devices/ip/routes/#
260 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
264 tRoute *rt = Node->ImplPtr;
265 int addrSize = IPStack_GetAddressSize(rt->AddressType);
269 // --- Standard IOCtls (0-3) ---
271 LEAVE('i', DRV_TYPE_MISC);
272 return DRV_TYPE_MISC;
274 case DRV_IOCTL_IDENT:
275 tmp = ModUtil_SetIdent(Data, STR(IDENT));
279 case DRV_IOCTL_VERSION:
283 case DRV_IOCTL_LOOKUP:
284 tmp = ModUtil_LookupString( (char**)casIOCtls_Route, (char*)Data );
290 if( !CheckMem(Data, addrSize) ) return -1;
291 memcpy(Data, rt->Network, addrSize);
295 if( !CheckMem(Data, addrSize) ) return -1;
296 memcpy(rt->Network, Data, addrSize);
301 if( !CheckMem(Data, addrSize) ) return -1;
302 memcpy(Data, rt->NextHop, addrSize);
306 if( !CheckMem(Data, addrSize) ) return -1;
307 memcpy(rt->NextHop, Data, addrSize);
310 // Get/Set Subnet Bits
313 if( !CheckMem(Data, sizeof(int)) ) return -1;
314 if( *iData < 0 || *iData > addrSize*8 )
316 rt->SubnetBits = *iData;
318 return rt->SubnetBits;
323 if( !CheckMem(Data, sizeof(int)) ) return -1;
324 if( *iData < 0 ) return -1;
329 // Get interface name
332 if( !CheckMem(Data, strlen(rt->Interface->Name) + 1) )
334 strcpy(Data, rt->Interface->Name);
336 return strlen(rt->Interface->Name);