3 * - Stack Initialisation
6 #define VERSION VER2(0,10)
11 #include <tpl_drv_common.h>
12 #include <tpl_drv_network.h>
15 //! Default timeout value, 30 seconds
16 #define DEFAULT_TIMEOUT (30*1000)
19 extern int ARP_Initialise();
20 extern int IPv4_Initialise();
21 extern int IPv4_Ping(tInterface *Iface, tIPv4 Addr);
24 int IPStack_Install(char **Arguments);
25 int IPStack_IOCtlRoot(tVFS_Node *Node, int ID, void *Data);
26 char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos);
27 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, char *Name);
28 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data);
29 int IPStack_AddInterface(char *Device);
30 tAdapter *IPStack_GetAdapter(char *Path);
31 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos);
32 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, char *Name);
33 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data);
36 MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
37 tDevFS_Driver gIP_DriverInfo = {
40 .Size = 0, // Number of interfaces
42 .ACLs = &gVFS_ACL_EveryoneRX,
43 .Flags = VFS_FFLAG_DIRECTORY,
44 .ReadDir = IPStack_Root_ReadDir,
45 .FindDir = IPStack_Root_FindDir,
46 .IOCtl = IPStack_Root_IOCtl
49 tSpinlock glIP_Interfaces = 0;
50 tInterface *gIP_Interfaces = NULL;
51 tInterface *gIP_Interfaces_Last = NULL;
52 int giIP_NextIfaceId = 1;
53 tSpinlock glIP_Adapters = 0;
54 tAdapter *gIP_Adapters = NULL;
55 tSocketFile *gIP_FileTemplates;
59 * \fn int IPStack_Install(char **Arguments)
60 * \brief Intialise the relevant parts of the stack and register with DevFS
62 int IPStack_Install(char **Arguments)
72 // Parse module arguments
73 for( i = 0; Arguments[i]; i++ )
75 //if(strcmp(Arguments[i], "Device") == '=') {
81 DevFS_AddDevice( &gIP_DriverInfo );
87 * \brief Adds a file to the socket list
89 int IPStack_AddFile(tSocketFile *File)
91 File->Next = gIP_FileTemplates;
92 gIP_FileTemplates = File;
97 * \brief Read from the IP Stack's Device Directory
99 char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos)
103 ENTER("pNode iPos", Node, Pos);
106 for( iface = gIP_Interfaces; iface && Pos--; iface = iface->Next ) ;
108 // Did we run off the end?
119 Pos = iface->Node.ImplInt;
125 name[2] = '0' + Pos/10;
126 name[3] = '0' + Pos%10;
130 name[2] = '0' + Pos/100;
131 name[3] = '0' + (Pos/10)%10;
132 name[4] = '0' + Pos%10;
137 // Return the pre-generated name
142 * \brief Get the node of an interface
144 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, char *Name)
149 ENTER("pNode sName", Node, Name);
151 if(Name[0] != 'i' || Name[1] != 'p') {
157 while('0' <= Name[i] && Name[i] <= '9')
160 num += Name[i] - '0';
163 if(Name[i] != '\0') {
168 for( iface = gIP_Interfaces; iface; iface = iface->Next )
170 if( iface->Node.ImplInt == num )
172 LEAVE('p', &iface->Node);
180 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
182 * \brief Handles IOCtls for the IPStack root
184 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data)
187 ENTER("pNode iID pData", Node, ID, Data);
191 // --- Standard IOCtls (0-3) ---
193 LEAVE('i', DRV_TYPE_MISC);
194 return DRV_TYPE_MISC;
196 case DRV_IOCTL_IDENT:
197 tmp = ModUtil_SetIdent(Data, "IPStack");
201 case DRV_IOCTL_VERSION:
205 case DRV_IOCTL_LOOKUP:
206 tmp = ModUtil_LookupString( (char**)casIOCtls_Root, (char*)Data );
212 * - Adds a new IP interface and binds it to a device
215 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
216 if( !CheckString( Data ) ) LEAVE_RET('i', -1);
217 tmp = IPStack_AddInterface(Data);
225 * \brief Read from an interface's directory
227 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
229 tSocketFile *file = gIP_FileTemplates;
230 while(Pos-- && file) file = file->Next;
232 if(!file) return NULL;
234 return strdup(file->Name);
238 * \brief Gets a named node from an interface directory
240 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, char *Name)
242 tSocketFile *file = gIP_FileTemplates;
244 // Get file definition
245 for(;file;file = file->Next)
247 if( strcmp(file->Name, Name) == 0 ) break;
249 if(!file) return NULL;
252 return file->Init(Node->ImplPtr);
256 * \brief Names for interface IOCtl Calls
258 static const char *casIOCtls_Iface[] = {
261 "get_address", "set_address",
263 "get_gateway", "set_gateway",
268 * \brief Handles IOCtls for the IPStack interfaces
270 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
273 tInterface *iface = (tInterface*)Node->ImplPtr;
274 ENTER("pNode iID pData", Node, ID, Data);
278 // --- Standard IOCtls (0-3) ---
280 LEAVE('i', DRV_TYPE_MISC);
281 return DRV_TYPE_MISC;
283 case DRV_IOCTL_IDENT:
284 tmp = ModUtil_SetIdent(Data, STR(IDENT));
288 case DRV_IOCTL_VERSION:
292 case DRV_IOCTL_LOOKUP:
293 tmp = ModUtil_LookupString( (char**)casIOCtls_Iface, (char*)Data );
299 * - Get/Set the interface type
306 if( Threads_GetUID() != 0 ) {
307 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
311 if( !CheckMem( Data, sizeof(int) ) ) {
312 LOG("Invalid pointer %p", Data);
316 switch( *(int*)Data )
320 memset(&iface->IP6, 0, sizeof(tIPv6)); // Clear address
324 memset(&iface->IP4, 0, sizeof(tIPv4));
328 memset(&iface->IP6, 0, sizeof(tIPv6));
335 LEAVE('i', iface->Type);
340 * - Get the interface's address
345 case 0: LEAVE_RET('i', 1);
347 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
348 memcpy( Data, &iface->IP4.Address, sizeof(tIPv4) );
351 if( !CheckMem( Data, sizeof(tIPv6) ) ) LEAVE_RET('i', -1);
352 memcpy( Data, &iface->IP6.Address, sizeof(tIPv6) );
359 * - Get the interface's address
362 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
365 case 0: LEAVE_RET('i', 1);
367 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
368 iface->Type = 0; // One very hacky mutex/trash protector
369 memcpy( &iface->IP4.Address, Data, sizeof(tIPv4) );
373 if( !CheckMem( Data, sizeof(tIPv6) ) ) LEAVE_RET('i', -1);
375 memcpy( &iface->IP6.Address, Data, sizeof(tIPv6) );
383 * - Get/Set the bits in the address subnet
389 switch( iface->Type )
391 case 4: LEAVE_RET('i', iface->IP4.SubnetBits);
392 case 6: LEAVE_RET('i', iface->IP6.SubnetBits);
393 default: LEAVE_RET('i', 0);
398 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
399 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
401 // Check and set the subnet bits
402 switch( iface->Type )
405 if( *(int*)Data < 0 || *(int*)Data > 31 ) LEAVE_RET('i', -1);
406 iface->IP4.SubnetBits = *(int*)Data;
407 LEAVE_RET('i', iface->IP4.SubnetBits);
409 if( *(int*)Data < 0 || *(int*)Data > 127 ) LEAVE_RET('i', -1);
410 iface->IP6.SubnetBits = *(int*)Data;
411 LEAVE_RET('i', iface->IP6.SubnetBits);
421 * - Get the interface's IPv4 gateway
429 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
430 memcpy( Data, &iface->IP4.Gateway, sizeof(tIPv4) );
440 * - Get/Set the interface's IPv4 gateway
443 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
450 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
451 iface->Type = 0; // One very hacky mutex/trash protector
452 memcpy( &iface->IP4.Gateway, Data, sizeof(tIPv4) );
463 * - Send an ICMP Echo
472 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
473 tmp = IPv4_Ping(iface, *(tIPv4*)Data);
490 * \fn int IPStack_AddInterface(char *Device)
491 * \brief Adds an interface to the list
493 int IPStack_AddInterface(char *Device)
497 ENTER("sDevice", Device);
499 iface = malloc(sizeof(tInterface));
502 return -2; // Return ERR_MYBAD
506 iface->Type = 0; // Unset type
509 iface->Node.ImplPtr = iface;
510 iface->Node.ImplInt = giIP_NextIfaceId++;
511 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
512 iface->Node.Size = 0;
513 iface->Node.NumACLs = 1;
514 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
515 iface->Node.ReadDir = IPStack_Iface_ReadDir;
516 iface->Node.FindDir = IPStack_Iface_FindDir;
517 iface->Node.IOCtl = IPStack_Iface_IOCtl;
520 iface->TimeoutDelay = DEFAULT_TIMEOUT;
522 // Get adapter handle
523 iface->Adapter = IPStack_GetAdapter(Device);
524 if( !iface->Adapter ) {
527 return -1; // Return ERR_YOUFAIL
531 LOCK( &glIP_Interfaces );
532 if( gIP_Interfaces ) {
533 gIP_Interfaces_Last->Next = iface;
534 gIP_Interfaces_Last = iface;
537 gIP_Interfaces = iface;
538 gIP_Interfaces_Last = iface;
540 RELEASE( &glIP_Interfaces );
542 gIP_DriverInfo.RootNode.Size ++;
545 LEAVE('i', iface->Node.ImplInt);
546 return iface->Node.ImplInt;
550 * \fn tAdapter *IPStack_GetAdapter(char *Path)
551 * \brief Gets/opens an adapter given the path
553 tAdapter *IPStack_GetAdapter(char *Path)
558 ENTER("sPath", Path);
560 LOCK( &glIP_Adapters );
562 // Check if this adapter is already open
563 for( dev = gIP_Adapters; dev; dev = dev->Next )
565 if( strcmp(dev->Device, Path) == 0 ) {
567 RELEASE( &glIP_Adapters );
573 // Ok, so let's open it
574 dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
576 RELEASE( &glIP_Adapters );
582 strcpy( dev->Device, Path );
586 dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
587 if( dev->DeviceFD == -1 ) {
589 RELEASE( &glIP_Adapters );
594 // Check that it is a network interface
595 tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
596 LOG("Device type = %i", tmp);
597 if( tmp != DRV_TYPE_NETWORK ) {
598 Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
599 VFS_Close( dev->DeviceFD );
601 RELEASE( &glIP_Adapters );
607 VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
610 dev->Next = gIP_Adapters;
613 RELEASE( &glIP_Adapters );
616 Link_WatchDevice( dev );