6 #define VERSION VER2(0,10)
9 #include <api_drv_common.h>
10 #include "include/adapters.h"
13 //! Default timeout value, 5 seconds
14 #define DEFAULT_TIMEOUT (5*1000)
17 extern int IPv4_Ping(tInterface *Iface, tIPv4 Addr);
18 //extern int IPv6_Ping(tInterface *Iface, tIPv6 Addr);
19 extern tVFS_Node gIP_RouteNode;
20 extern tVFS_Node gIP_AdaptersNode;
23 char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos);
24 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name);
25 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data);
27 int IPStack_AddFile(tSocketFile *File);
28 tInterface *IPStack_AddInterface(const char *Device, const char *Name);
30 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos);
31 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name);
32 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data);
35 tVFS_NodeType gIP_InterfaceNodeType = {
36 .ReadDir = IPStack_Iface_ReadDir,
37 .FindDir = IPStack_Iface_FindDir,
38 .IOCtl = IPStack_Iface_IOCtl
40 //! Loopback (127.0.0.0/8, ::1) Pseudo-Interface
41 tInterface gIP_LoopInterface = {
43 .ImplPtr = &gIP_LoopInterface,
44 .Flags = VFS_FFLAG_DIRECTORY,
47 .ACLs = &gVFS_ACL_EveryoneRX,
48 .Type = &gIP_InterfaceNodeType
53 tShortSpinlock glIP_Interfaces;
54 tInterface *gIP_Interfaces = NULL;
55 tInterface *gIP_Interfaces_Last = NULL;
56 int giIP_NextIfaceId = 1;
58 tSocketFile *gIP_FileTemplates;
63 * \brief Read from the IP Stack's Device Directory
65 char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos)
69 ENTER("pNode iPos", Node, Pos);
75 return strdup("routes");
79 LEAVE('s', "adapters");
80 return strdup("adapters");
90 for( iface = gIP_Interfaces; iface && Pos--; iface = iface->Next ) ;
92 // Did we run off the end?
100 Log_Warning("IPStack", "IPStack_Root_ReadDir - malloc error");
106 Pos = iface->Node.ImplInt;
112 name[0] = '0' + Pos/10;
113 name[1] = '0' + Pos%10;
117 name[0] = '0' + Pos/100;
118 name[1] = '0' + (Pos/10)%10;
119 name[2] = '0' + Pos%10;
124 // Return the pre-generated name
129 * \brief Get the node of an interface
131 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name)
138 ENTER("pNode sName", Node, Name);
141 if( strcmp(Name, "routes") == 0 ) {
142 LEAVE('p', &gIP_RouteNode);
143 return &gIP_RouteNode;
147 if( strcmp(Name, "adapters") == 0 ) {
148 LEAVE('p', &gIP_AdaptersNode);
149 return &gIP_AdaptersNode;
153 if( strcmp(Name, "lo") == 0 ) {
154 LEAVE('p', &gIP_LoopInterface.Node);
155 return &gIP_LoopInterface.Node;
158 for( iface = gIP_Interfaces; iface; iface = iface->Next )
160 if( strcmp(iface->Name, Name) == 0 )
162 LEAVE('p', &iface->Node);
171 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
173 * \brief Handles IOCtls for the IPStack root
175 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data)
178 ENTER("pNode iID pData", Node, ID, Data);
182 // --- Standard IOCtls (0-3) ---
183 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Root)
187 * - Adds a new IP interface and binds it to a device
190 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
191 if( !CheckString( Data ) ) LEAVE_RET('i', -1);
192 LOG("New interface for '%s'", Data);
195 tInterface *iface = IPStack_AddInterface(Data, name);
196 if(iface == NULL) LEAVE_RET('i', -1);
197 tmp = iface->Node.ImplInt;
206 * \fn tInterface *IPStack_AddInterface(char *Device)
207 * \brief Adds an interface to the list
209 tInterface *IPStack_AddInterface(const char *Device, const char *Name)
215 ENTER("sDevice", Device);
217 card = Adapter_GetByName(Device);
219 Log_Debug("IPStack", "Unable to open card '%s'", Device);
221 return NULL; // ERR_YOURBAD
224 nameLen = sprintf(NULL, "%i", giIP_NextIfaceId);
229 + IPStack_GetAddressSize(-1)*3 // Address, Route->Network, Route->NextHop
232 Log_Warning("IPStack", "AddInterface - malloc() failed");
234 return NULL; // Return ERR_MYBAD
238 iface->Type = 0; // Unset type
239 iface->Address = iface->Name + nameLen + 1; // Address
240 memset(&iface->Route, 0, sizeof(iface->Route));
241 iface->Route.Network = iface->Address + IPStack_GetAddressSize(-1);
242 iface->Route.NextHop = iface->Route.Network + IPStack_GetAddressSize(-1);
245 iface->Node.ImplPtr = iface;
246 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
247 iface->Node.Size = -1;
248 iface->Node.NumACLs = 1;
249 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
250 iface->Node.Type = &gIP_InterfaceNodeType;
253 iface->TimeoutDelay = DEFAULT_TIMEOUT;
255 // Get adapter handle
256 iface->Adapter = card;
258 // Delay setting ImplInt until after the adapter is opened
259 // Keeps things simple
260 iface->Node.ImplInt = giIP_NextIfaceId++;
261 sprintf(iface->Name, "%i", (int)iface->Node.ImplInt);
264 SHORTLOCK( &glIP_Interfaces );
265 if( gIP_Interfaces ) {
266 gIP_Interfaces_Last->Next = iface;
267 gIP_Interfaces_Last = iface;
270 gIP_Interfaces = iface;
271 gIP_Interfaces_Last = iface;
273 SHORTREL( &glIP_Interfaces );
275 // gIP_DriverInfo.RootNode.Size ++;
283 * \brief Adds a file to the socket list
285 int IPStack_AddFile(tSocketFile *File)
287 Log_Log("IPStack", "Added file '%s'", File->Name);
288 File->Next = gIP_FileTemplates;
289 gIP_FileTemplates = File;
297 * \brief Read from an interface's directory
299 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
301 tSocketFile *file = gIP_FileTemplates;
302 while(Pos-- && file) {
306 if(!file) return NULL;
308 return strdup(file->Name);
312 * \brief Gets a named node from an interface directory
314 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
316 tSocketFile *file = gIP_FileTemplates;
318 // Get file definition
319 for(;file;file = file->Next)
321 if( strcmp(file->Name, Name) == 0 ) break;
323 if(!file) return NULL;
326 return file->Init(Node->ImplPtr);
330 * \brief Names for interface IOCtl Calls
332 static const char *casIOCtls_Iface[] = {
335 "get_address", "set_address",
342 * \brief Handles IOCtls for the IPStack interfaces
344 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
347 tInterface *iface = (tInterface*)Node->ImplPtr;
348 ENTER("pNode iID pData", Node, ID, Data);
352 // --- Standard IOCtls (0-3) ---
353 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Iface)
357 * - Get/Set the interface type
364 if( Threads_GetUID() != 0 ) {
365 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
369 if( !CheckMem( Data, sizeof(int) ) ) {
370 LOG("Invalid pointer %p", Data);
376 iface->Type = *(int*)Data;
377 LOG("Interface type set to %i", iface->Type);
378 size = IPStack_GetAddressSize(iface->Type);
379 // Check it's actually valid
380 if( iface->Type != 0 && size == 0 ) {
387 memset(iface->Address, 0, size);
389 LEAVE('i', iface->Type);
394 * - Get the interface's address
397 size = IPStack_GetAddressSize(iface->Type);
398 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
399 memcpy( Data, iface->Address, size );
405 * - Set the interface's address
408 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
410 size = IPStack_GetAddressSize(iface->Type);
411 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
412 // TODO: Protect against trashing
413 LOG("Interface address set to '%s'", IPStack_PrintAddress(iface->Type, Data));
414 memcpy( iface->Address, Data, size );
419 * - Get/Set the bits in the address subnet
422 // Do we want to set the value?
425 // Are we root? (TODO: Check Owner/Group)
426 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
427 // Is the memory valid
428 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
431 if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 )
433 LOG("Set subnet bits to %i", *(int*)Data);
435 iface->SubnetBits = *(int*)Data;
437 LEAVE_RET('i', iface->SubnetBits);
441 * - Gets the name of the attached device
444 if( iface->Adapter == NULL )
446 char *name = Adapter_GetName(iface->Adapter);
447 int len = strlen(name);
449 if( !CheckMem( Data, len+1 ) ) {
453 strcpy( Data, name );
460 * - Send an ICMP Echo
469 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
470 tmp = IPv4_Ping(iface, *(tIPv4*)Data);