6 #define VERSION VER2(0,10)
9 #include <api_drv_common.h>
10 #include "include/adapters.h"
11 #include "interface.h"
14 //! Default timeout value, 5 seconds
15 #define DEFAULT_TIMEOUT (5*1000)
18 extern int IPv4_Ping(tInterface *Iface, tIPv4 Addr);
19 //extern int IPv6_Ping(tInterface *Iface, tIPv6 Addr);
20 extern tVFS_Node gIP_RouteNode;
21 extern tVFS_Node gIP_AdaptersNode;
24 int IPStack_Root_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX]);
25 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name);
26 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data);
28 int IPStack_AddFile(tSocketFile *File);
29 tInterface *IPStack_AddInterface(const char *Device, int Type, const char *Name);
31 int IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX]);
32 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name);
33 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data);
36 tVFS_NodeType gIP_RootNodeType = {
37 .ReadDir = IPStack_Root_ReadDir,
38 .FindDir = IPStack_Root_FindDir,
39 .IOCtl = IPStack_Root_IOCtl
41 tVFS_NodeType gIP_InterfaceNodeType = {
42 .ReadDir = IPStack_Iface_ReadDir,
43 .FindDir = IPStack_Iface_FindDir,
44 .IOCtl = IPStack_Iface_IOCtl
46 //! Loopback (127.0.0.0/8, ::1) Pseudo-Interface
47 tInterface gIP_LoopInterface = {
49 .ImplPtr = &gIP_LoopInterface,
50 .Flags = VFS_FFLAG_DIRECTORY,
53 .ACLs = &gVFS_ACL_EveryoneRX,
54 .Type = &gIP_InterfaceNodeType
59 tShortSpinlock glIP_Interfaces;
60 tInterface *gIP_Interfaces = NULL;
61 tInterface *gIP_Interfaces_Last = NULL;
62 int giIP_NextIfaceId = 1;
64 tSocketFile *gIP_FileTemplates;
69 * \brief Read from the IP Stack's Device Directory
71 int IPStack_Root_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
74 ENTER("pNode iPos", Node, Pos);
79 strcpy(Dest, "routes");
84 strcpy(Dest, "adapters");
95 for( iface = gIP_Interfaces; iface && Pos--; iface = iface->Next ) ;
97 // Did we run off the end?
99 LEAVE('i', -EINTERNAL);
104 strncpy(Dest, iface->Name, FILENAME_MAX);
111 * \brief Get the node of an interface
113 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name)
120 ENTER("pNode sName", Node, Name);
123 if( strcmp(Name, "routes") == 0 ) {
124 LEAVE('p', &gIP_RouteNode);
125 return &gIP_RouteNode;
129 if( strcmp(Name, "adapters") == 0 ) {
130 LEAVE('p', &gIP_AdaptersNode);
131 return &gIP_AdaptersNode;
135 if( strcmp(Name, "lo") == 0 ) {
136 LEAVE('p', &gIP_LoopInterface.Node);
137 return &gIP_LoopInterface.Node;
140 for( iface = gIP_Interfaces; iface; iface = iface->Next )
142 if( strcmp(iface->Name, Name) == 0 )
144 LEAVE('p', &iface->Node);
153 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
155 * \brief Handles IOCtls for the IPStack root
157 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data)
160 ENTER("pNode iID pData", Node, ID, Data);
164 // --- Standard IOCtls (0-3) ---
165 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Root)
169 * - Adds a new interface and binds it to a device
178 if( Threads_GetUID() != 0 )
180 if( !CheckMem(ifinfo, sizeof(*ifinfo)) )
182 if( !MM_IsUser(ifinfo->Device) || !CheckString( ifinfo->Device ) )
184 if( !MM_IsUser(ifinfo->Name) || !CheckString( ifinfo->Name ) )
187 LOG("New interface of type %i for '%s' named '%s'",
188 ifinfo->Type, ifinfo->Device, ifinfo->Name);
189 tInterface *iface = IPStack_AddInterface(ifinfo->Device, ifinfo->Type, ifinfo->Name);
190 if(iface == NULL) LEAVE_RET('i', -1);
192 tmp = iface->Node.ImplInt;
201 * \fn tInterface *IPStack_AddInterface(char *Device)
202 * \brief Adds an interface to the list
204 tInterface *IPStack_AddInterface(const char *Device, int Type, const char *Name)
208 int nameLen, addrsize;
210 ENTER("sDevice", Device);
212 // Check address type
213 addrsize = IPStack_GetAddressSize(Type);
214 if( addrsize == -1 ) {
215 Log_Debug("IPStack", "Bad address type %i", Type);
220 card = Adapter_GetByName(Device);
222 Log_Debug("IPStack", "Unable to open card '%s'", Device);
224 return NULL; // ERR_YOURBAD
230 nameLen = strlen(Name);
234 nameLen = snprintf(NULL, 0, "%i", giIP_NextIfaceId);
240 + addrsize*3 // Address, Route->Network, Route->NextHop
243 Log_Warning("IPStack", "AddInterface - malloc() failed");
246 return NULL; // Return ERR_MYBAD
250 iface->Type = Type; // Unset type
251 iface->Address = iface->Name + nameLen + 1; // Address
252 memset(iface->Address, 0, addrsize);
253 memset(&iface->Route, 0, sizeof(iface->Route));
254 iface->Route.Network = iface->Address + addrsize;
255 memset(iface->Route.Network, 0, addrsize);
256 iface->Route.NextHop = iface->Route.Network + addrsize;
257 memset(iface->Route.NextHop, 0, addrsize);
260 iface->Node.ImplPtr = iface;
261 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
262 iface->Node.Size = -1;
263 iface->Node.NumACLs = 1;
264 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
265 iface->Node.Type = &gIP_InterfaceNodeType;
268 iface->TimeoutDelay = DEFAULT_TIMEOUT;
270 // Get adapter handle
271 iface->Adapter = card;
276 iface->Node.ImplInt = 0;
277 strcpy(iface->Name, Name);
281 iface->Node.ImplInt = giIP_NextIfaceId++;
282 sprintf(iface->Name, "%i", (int)iface->Node.ImplInt);
286 SHORTLOCK( &glIP_Interfaces );
287 if( gIP_Interfaces ) {
288 gIP_Interfaces_Last->Next = iface;
289 gIP_Interfaces_Last = iface;
292 gIP_Interfaces = iface;
293 gIP_Interfaces_Last = iface;
295 SHORTREL( &glIP_Interfaces );
303 * \brief Adds a file to the socket list
305 int IPStack_AddFile(tSocketFile *File)
307 Log_Log("IPStack", "Added file '%s'", File->Name);
308 File->Next = gIP_FileTemplates;
309 gIP_FileTemplates = File;
317 * \brief Read from an interface's directory
319 int IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
321 tSocketFile *file = gIP_FileTemplates;
322 while(Pos-- && file) {
329 strncpy(Dest, file->Name, FILENAME_MAX);
334 * \brief Gets a named node from an interface directory
336 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
338 tSocketFile *file = gIP_FileTemplates;
340 // Get file definition
341 for(;file;file = file->Next)
343 if( strcmp(file->Name, Name) == 0 ) break;
345 if(!file) return NULL;
348 return file->Init(Node->ImplPtr);
352 * \brief Names for interface IOCtl Calls
354 static const char *casIOCtls_Iface[] = {
357 "get_address", "set_address",
364 * \brief Handles IOCtls for the IPStack interfaces
366 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
369 tInterface *iface = (tInterface*)Node->ImplPtr;
370 ENTER("pNode iID pData", Node, ID, Data);
374 // --- Standard IOCtls (0-3) ---
375 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Iface)
379 * - Get/Set the interface type
387 if( Threads_GetUID() != 0 ) {
388 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
392 if( !CheckMem( Data, sizeof(int) ) ) {
393 LOG("Invalid pointer %p", Data);
399 iface->Type = *(int*)Data;
400 LOG("Interface type set to %i", iface->Type);
401 size = IPStack_GetAddressSize(iface->Type);
402 // Check it's actually valid
403 if( iface->Type != 0 && size == 0 ) {
410 memset(iface->Address, 0, size);
412 Log_Notice("IPStack", "Interface ioctl(settype) no longer usable");
414 LEAVE('i', iface->Type);
419 * - Get the interface's address
422 size = IPStack_GetAddressSize(iface->Type);
423 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
424 memcpy( Data, iface->Address, size );
430 * - Set the interface's address
433 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
435 size = IPStack_GetAddressSize(iface->Type);
436 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
437 // TODO: Protect against trashing
438 LOG("Interface address set to '%s'", IPStack_PrintAddress(iface->Type, Data));
439 memcpy( iface->Address, Data, size );
444 * - Get/Set the bits in the address subnet
447 // Do we want to set the value?
450 // Are we root? (TODO: Check Owner/Group)
451 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
452 // Is the memory valid
453 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
456 if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 )
458 LOG("Set subnet bits to %i", *(int*)Data);
460 iface->SubnetBits = *(int*)Data;
462 LEAVE_RET('i', iface->SubnetBits);
466 * - Gets the name of the attached device
469 if( iface->Adapter == NULL )
471 char *name = Adapter_GetName(iface->Adapter);
472 int len = strlen(name);
474 if( !CheckMem( Data, len+1 ) ) {
478 strcpy( Data, name );
485 * - Send an ICMP Echo
494 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
495 tmp = IPv4_Ping(iface, *(tIPv4*)Data);