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)
99 // --- Standard IOCtls (0-3) ---
101 LEAVE('i', DRV_TYPE_MISC);
102 return DRV_TYPE_MISC;
104 case DRV_IOCTL_IDENT:
105 tmp = ModUtil_SetIdent(Data, STR(IDENT));
109 case DRV_IOCTL_VERSION:
113 case DRV_IOCTL_LOOKUP:
114 tmp = ModUtil_LookupString( (char**)casIOCtls_RouteDir, (char*)Data );
119 if( !CheckString(Data) ) return -1;
120 return IPStack_Route_Create(Data);
122 case 5: // Locate Route
130 if( !CheckMem(Data, sizeof(int)) )
132 if( !CheckMem(Data, sizeof(int) + IPStack_GetAddressSize(data->Type)) )
135 rt = IPStack_FindRoute(data->Type, NULL, data->Addr);
140 return rt->Node.Inode;
148 * \brief Create a new route entry
149 * \param InterfaceName Name of the interface using this route
151 int IPStack_Route_Create(const char *InterfaceName)
158 // Note: Oh man! This is such a hack
160 tVFS_Node *node = IPStack_Root_FindDir(NULL, InterfaceName);
161 if( !node ) return 0;
162 iface = node->ImplPtr;
165 // Get the size of the specified address type
166 size = IPStack_GetAddressSize(iface->Type);
172 rt = calloc(1, sizeof(tRoute) + size*2 );
175 rt->Node.ImplPtr = rt;
176 rt->Node.Inode = giIP_NextRouteId ++;
178 rt->Node.NumACLs = 1,
179 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
180 rt->Node.IOCtl = IPStack_Route_IOCtl;
183 rt->AddressType = iface->Type;
184 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
186 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
187 rt->Interface = iface;
190 if( gIP_RoutesEnd ) {
191 gIP_RoutesEnd->Next = rt;
195 gIP_Routes = gIP_RoutesEnd = rt;
198 return rt->Node.Inode;
203 tRoute *IPStack_FindRoute(int AddressType, tInterface *Interface, void *Address)
208 if( Interface && AddressType != Interface->Type ) return NULL;
210 for( rt = gIP_Routes; rt; rt = rt->Next )
213 if( Interface && rt->Interface != Interface ) continue;
214 // Check address type
215 if( rt->AddressType != AddressType ) continue;
217 // Check if the address matches
218 if( !IPStack_CompareAddress(AddressType, rt->Network, Address, rt->SubnetBits) )
222 // More direct routes are preferred
223 if( best->SubnetBits > rt->SubnetBits ) continue;
224 // If equally direct, choose the best metric
225 if( best->SubnetBits == rt->SubnetBits && best->Metric < rt->Metric ) continue;
235 * \brief Names for route IOCtl Calls
237 static const char *casIOCtls_Route[] = {
239 "get_network", // Get network - (void *Data), returns boolean success
240 "set_network", // Set network - (void *Data), returns boolean success
241 "get_nexthop", // Get next hop - (void *Data), returns boolean success
242 "set_nexthop", // Set next hop - (void *Data), returns boolean success
243 "getset_subnetbits", // Get/Set subnet bits - (int *Bits), returns current value
244 "getset_metric", // Get/Set metric - (int *Metric), returns current value
245 "get_interface", // Get interface name - (char *Name), returns name length, NULL OK
250 * \brief IOCtl for /Devices/ip/routes/#
252 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
256 tRoute *rt = Node->ImplPtr;
257 int addrSize = IPStack_GetAddressSize(rt->AddressType);
261 // --- Standard IOCtls (0-3) ---
263 LEAVE('i', DRV_TYPE_MISC);
264 return DRV_TYPE_MISC;
266 case DRV_IOCTL_IDENT:
267 tmp = ModUtil_SetIdent(Data, STR(IDENT));
271 case DRV_IOCTL_VERSION:
275 case DRV_IOCTL_LOOKUP:
276 tmp = ModUtil_LookupString( (char**)casIOCtls_Route, (char*)Data );
282 if( !CheckMem(Data, addrSize) ) return -1;
283 memcpy(Data, rt->Network, addrSize);
287 if( !CheckMem(Data, addrSize) ) return -1;
288 memcpy(rt->Network, Data, addrSize);
293 if( !CheckMem(Data, addrSize) ) return -1;
294 memcpy(Data, rt->NextHop, addrSize);
298 if( !CheckMem(Data, addrSize) ) return -1;
299 memcpy(rt->NextHop, Data, addrSize);
302 // Get/Set Subnet Bits
305 if( !CheckMem(Data, sizeof(int)) ) return -1;
306 if( *iData < 0 || *iData > addrSize*8 )
308 rt->SubnetBits = *iData;
310 return rt->SubnetBits;
315 if( !CheckMem(Data, sizeof(int)) ) return -1;
316 if( *iData < 0 ) return -1;
321 // Get interface name
324 if( !CheckMem(Data, strlen(rt->Interface->Name) + 1) )
326 strcpy(Data, rt->Interface->Name);
328 return strlen(rt->Interface->Name);