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 // - Individual Routes
36 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data);
39 int giIP_NextRouteId = 1;
41 tRoute *gIP_RoutesEnd;
42 tVFS_Node gIP_RouteNode = {
43 Flags: VFS_FFLAG_DIRECTORY,
46 ACLs: &gVFS_ACL_EveryoneRX,
47 ReadDir: IPStack_RouteDir_ReadDir,
48 FindDir: IPStack_RouteDir_FindDir,
49 IOCtl: IPStack_RouteDir_IOCtl
54 * \brief ReadDir for the /Devices/ip/routes/ directory
56 char *IPStack_RouteDir_ReadDir(tVFS_Node *Node, int Pos)
60 for(rt = gIP_Routes; rt && Pos --; rt = rt->Next);
67 int len = sprintf(NULL, "%i", rt->Node.Inode);
69 sprintf(buf, "%i", rt->Node.Inode);
75 * \brief FindDir for the /Devices/ip/routes/ directory
77 tVFS_Node *IPStack_RouteDir_FindDir(tVFS_Node *Node, const char *Name)
82 // Zero is invalid, sorry :)
83 if( !num ) return NULL;
85 // The list is inherently sorted, so we can do a quick search
86 for(rt = gIP_Routes; rt && rt->Node.Inode < num; rt = rt->Next);
87 // Fast fail on larger number
88 if( rt->Node.Inode > num )
95 * \brief Names for the route list IOCtl Calls
97 static const char *casIOCtls_RouteDir[] = {
99 "add_route", // Add a route - char *InterfaceName
104 * \brief IOCtl for /Devices/ip/routes/
106 int IPStack_RouteDir_IOCtl(tVFS_Node *Node, int ID, void *Data)
111 // --- Standard IOCtls (0-3) ---
113 LEAVE('i', DRV_TYPE_MISC);
114 return DRV_TYPE_MISC;
116 case DRV_IOCTL_IDENT:
117 tmp = ModUtil_SetIdent(Data, STR(IDENT));
121 case DRV_IOCTL_VERSION:
125 case DRV_IOCTL_LOOKUP:
126 tmp = ModUtil_LookupString( (char**)casIOCtls_RouteDir, (char*)Data );
131 if( !CheckString(Data) ) return -1;
132 return IPStack_Route_Create(Data);
138 * \brief Create a new route entry
139 * \param InterfaceName Name of the interface using this route
141 int IPStack_Route_Create(const char *InterfaceName)
148 // Note: Oh man! This is such a hack
150 tVFS_Node *node = IPStack_Root_FindDir(NULL, InterfaceName);
151 if( !node ) return 0;
152 iface = node->ImplPtr;
155 // Get the size of the specified address type
156 size = IPStack_GetAddressSize(iface->Type);
162 rt = calloc(1, sizeof(tRoute) + size*2 );
165 rt->Node.ImplPtr = rt;
166 rt->Node.Inode = giIP_NextRouteId ++;
168 rt->Node.NumACLs = 1,
169 rt->Node.ACLs = &gVFS_ACL_EveryoneRO;
170 rt->Node.IOCtl = IPStack_Route_IOCtl;
173 rt->AddressType = iface->Type;
174 rt->Network = (void *)( (tVAddr)rt + sizeof(tRoute) );
176 rt->NextHop = (void *)( (tVAddr)rt + sizeof(tRoute) + size );
177 rt->Interface = iface;
180 if( gIP_RoutesEnd ) {
181 gIP_RoutesEnd->Next = rt;
185 gIP_Routes = gIP_RoutesEnd = rt;
188 return rt->Node.Inode;
192 * \brief Names for route IOCtl Calls
194 static const char *casIOCtls_Route[] = {
196 "get_network", // Get network - (void *Data)
197 "set_network", // Set network - (void *Data)
198 "get_nexthop", // Get next hop - (void *Data)
199 "set_nexthop", // Set next hop - (void *Data)
200 "getset_subnetbits", // Get/Set subnet bits - (int *Bits)
201 "getset_metric", // Get/Set metric - (int *Metric)
206 * \brief IOCtl for /Devices/ip/routes/#
208 int IPStack_Route_IOCtl(tVFS_Node *Node, int ID, void *Data)
212 tRoute *rt = Node->ImplPtr;
213 int addrSize = IPStack_GetAddressSize(rt->AddressType);
217 // --- Standard IOCtls (0-3) ---
219 LEAVE('i', DRV_TYPE_MISC);
220 return DRV_TYPE_MISC;
222 case DRV_IOCTL_IDENT:
223 tmp = ModUtil_SetIdent(Data, STR(IDENT));
227 case DRV_IOCTL_VERSION:
231 case DRV_IOCTL_LOOKUP:
232 tmp = ModUtil_LookupString( (char**)casIOCtls_Route, (char*)Data );
238 if( !CheckMem(Data, addrSize) ) return -1;
239 memcpy(Data, rt->Network, addrSize);
243 if( !CheckMem(Data, addrSize) ) return -1;
244 memcpy(rt->Network, Data, addrSize);
249 if( !CheckMem(Data, addrSize) ) return -1;
250 memcpy(Data, rt->NextHop, addrSize);
254 if( !CheckMem(Data, addrSize) ) return -1;
255 memcpy(rt->NextHop, Data, addrSize);
258 // Get/Set Subnet Bits
261 if( !CheckMem(Data, sizeof(int)) ) return -1;
262 if( *iData < 0 || *iData > addrSize*8 )
264 rt->SubnetBits = *iData;
266 return rt->SubnetBits;
271 if( !CheckMem(Data, sizeof(int)) ) return -1;
272 if( *iData < 0 ) return -1;