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, 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 Pos = iface->Node.ImplInt;
105 snprintf(Dest, FILENAME_MAX, "%i", Pos);
112 * \brief Get the node of an interface
114 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name)
121 ENTER("pNode sName", Node, Name);
124 if( strcmp(Name, "routes") == 0 ) {
125 LEAVE('p', &gIP_RouteNode);
126 return &gIP_RouteNode;
130 if( strcmp(Name, "adapters") == 0 ) {
131 LEAVE('p', &gIP_AdaptersNode);
132 return &gIP_AdaptersNode;
136 if( strcmp(Name, "lo") == 0 ) {
137 LEAVE('p', &gIP_LoopInterface.Node);
138 return &gIP_LoopInterface.Node;
141 for( iface = gIP_Interfaces; iface; iface = iface->Next )
143 if( strcmp(iface->Name, Name) == 0 )
145 LEAVE('p', &iface->Node);
154 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
156 * \brief Handles IOCtls for the IPStack root
158 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data)
161 ENTER("pNode iID pData", Node, ID, Data);
165 // --- Standard IOCtls (0-3) ---
166 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Root)
170 * - Adds a new IP interface and binds it to a device
173 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
174 if( !CheckString( Data ) ) LEAVE_RET('i', -1);
175 LOG("New interface for '%s'", Data);
178 tInterface *iface = IPStack_AddInterface(Data, name);
179 if(iface == NULL) LEAVE_RET('i', -1);
180 tmp = iface->Node.ImplInt;
189 * \fn tInterface *IPStack_AddInterface(char *Device)
190 * \brief Adds an interface to the list
192 tInterface *IPStack_AddInterface(const char *Device, const char *Name)
198 ENTER("sDevice", Device);
200 card = Adapter_GetByName(Device);
202 Log_Debug("IPStack", "Unable to open card '%s'", Device);
204 return NULL; // ERR_YOURBAD
207 nameLen = sprintf(NULL, "%i", giIP_NextIfaceId);
212 + IPStack_GetAddressSize(-1)*3 // Address, Route->Network, Route->NextHop
215 Log_Warning("IPStack", "AddInterface - malloc() failed");
217 return NULL; // Return ERR_MYBAD
221 iface->Type = 0; // Unset type
222 iface->Address = iface->Name + nameLen + 1; // Address
223 memset(&iface->Route, 0, sizeof(iface->Route));
224 iface->Route.Network = iface->Address + IPStack_GetAddressSize(-1);
225 iface->Route.NextHop = iface->Route.Network + IPStack_GetAddressSize(-1);
228 iface->Node.ImplPtr = iface;
229 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
230 iface->Node.Size = -1;
231 iface->Node.NumACLs = 1;
232 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
233 iface->Node.Type = &gIP_InterfaceNodeType;
236 iface->TimeoutDelay = DEFAULT_TIMEOUT;
238 // Get adapter handle
239 iface->Adapter = card;
241 // Delay setting ImplInt until after the adapter is opened
242 // Keeps things simple
243 iface->Node.ImplInt = giIP_NextIfaceId++;
244 sprintf(iface->Name, "%i", (int)iface->Node.ImplInt);
247 SHORTLOCK( &glIP_Interfaces );
248 if( gIP_Interfaces ) {
249 gIP_Interfaces_Last->Next = iface;
250 gIP_Interfaces_Last = iface;
253 gIP_Interfaces = iface;
254 gIP_Interfaces_Last = iface;
256 SHORTREL( &glIP_Interfaces );
258 // gIP_DriverInfo.RootNode.Size ++;
266 * \brief Adds a file to the socket list
268 int IPStack_AddFile(tSocketFile *File)
270 Log_Log("IPStack", "Added file '%s'", File->Name);
271 File->Next = gIP_FileTemplates;
272 gIP_FileTemplates = File;
280 * \brief Read from an interface's directory
282 int IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
284 tSocketFile *file = gIP_FileTemplates;
285 while(Pos-- && file) {
292 strncpy(Dest, file->Name, FILENAME_MAX);
297 * \brief Gets a named node from an interface directory
299 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
301 tSocketFile *file = gIP_FileTemplates;
303 // Get file definition
304 for(;file;file = file->Next)
306 if( strcmp(file->Name, Name) == 0 ) break;
308 if(!file) return NULL;
311 return file->Init(Node->ImplPtr);
315 * \brief Names for interface IOCtl Calls
317 static const char *casIOCtls_Iface[] = {
320 "get_address", "set_address",
327 * \brief Handles IOCtls for the IPStack interfaces
329 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
332 tInterface *iface = (tInterface*)Node->ImplPtr;
333 ENTER("pNode iID pData", Node, ID, Data);
337 // --- Standard IOCtls (0-3) ---
338 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Iface)
342 * - Get/Set the interface type
349 if( Threads_GetUID() != 0 ) {
350 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
354 if( !CheckMem( Data, sizeof(int) ) ) {
355 LOG("Invalid pointer %p", Data);
361 iface->Type = *(int*)Data;
362 LOG("Interface type set to %i", iface->Type);
363 size = IPStack_GetAddressSize(iface->Type);
364 // Check it's actually valid
365 if( iface->Type != 0 && size == 0 ) {
372 memset(iface->Address, 0, size);
374 LEAVE('i', iface->Type);
379 * - Get the interface's address
382 size = IPStack_GetAddressSize(iface->Type);
383 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
384 memcpy( Data, iface->Address, size );
390 * - Set the interface's address
393 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
395 size = IPStack_GetAddressSize(iface->Type);
396 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
397 // TODO: Protect against trashing
398 LOG("Interface address set to '%s'", IPStack_PrintAddress(iface->Type, Data));
399 memcpy( iface->Address, Data, size );
404 * - Get/Set the bits in the address subnet
407 // Do we want to set the value?
410 // Are we root? (TODO: Check Owner/Group)
411 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
412 // Is the memory valid
413 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
416 if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 )
418 LOG("Set subnet bits to %i", *(int*)Data);
420 iface->SubnetBits = *(int*)Data;
422 LEAVE_RET('i', iface->SubnetBits);
426 * - Gets the name of the attached device
429 if( iface->Adapter == NULL )
431 char *name = Adapter_GetName(iface->Adapter);
432 int len = strlen(name);
434 if( !CheckMem( Data, len+1 ) ) {
438 strcpy( Data, name );
445 * - Send an ICMP Echo
454 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
455 tmp = IPv4_Ping(iface, *(tIPv4*)Data);