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 void UDP_Initialise();
21 extern void TCP_Initialise();
22 extern int IPv4_Initialise();
23 extern int IPv4_Ping(tInterface *Iface, tIPv4 Addr);
24 extern int IPv6_Initialise();
25 //extern int IPv6_Ping(tInterface *Iface, tIPv6 Addr);
28 int IPStack_Install(char **Arguments);
29 int IPStack_IOCtlRoot(tVFS_Node *Node, int ID, void *Data);
30 char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos);
31 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name);
32 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data);
34 int IPStack_AddInterface(char *Device);
35 tAdapter *IPStack_GetAdapter(char *Path);
36 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos);
37 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name);
38 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data);
41 MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
42 tDevFS_Driver gIP_DriverInfo = {
45 .Size = 0, // Number of interfaces
47 .ACLs = &gVFS_ACL_EveryoneRX,
48 .Flags = VFS_FFLAG_DIRECTORY,
49 .ReadDir = IPStack_Root_ReadDir,
50 .FindDir = IPStack_Root_FindDir,
51 .IOCtl = IPStack_Root_IOCtl
54 tShortSpinlock glIP_Interfaces;
55 //! Loopback (127.0.0.0/8, ::1) Pseudo-Interface
56 tInterface gIP_LoopInterface = {
58 ImplPtr: &gIP_LoopInterface,
59 Flags: VFS_FFLAG_DIRECTORY,
62 ACLs: &gVFS_ACL_EveryoneRX,
63 ReadDir: IPStack_Iface_ReadDir,
64 FindDir: IPStack_Iface_FindDir,
65 IOCtl: IPStack_Iface_IOCtl
70 tInterface *gIP_Interfaces = NULL;
71 tInterface *gIP_Interfaces_Last = NULL;
72 int giIP_NextIfaceId = 1;
74 tAdapter *gIP_Adapters = NULL;
75 tSocketFile *gIP_FileTemplates;
79 * \fn int IPStack_Install(char **Arguments)
80 * \brief Intialise the relevant parts of the stack and register with DevFS
82 int IPStack_Install(char **Arguments)
86 // Layer 3 - Network Layer Protocols
90 // Layer 4 - Transport Layer Protocols
96 // Parse module arguments
97 for( i = 0; Arguments[i]; i++ )
100 // Define interfaces by <Device>,<Type>,<HexStreamAddress>,<Bits>
102 // - <Device> is the device path (E.g. /Devices/ne2k/0)
103 // - <Type> is a number (e.g. 4) or symbol (e.g. AF_INET4)
104 // - <HexStreamAddress> is a condensed hexadecimal stream (in big endian)
105 // (E.g. 0A000201 for 10.0.2.1 IPv4)
106 // - <Bits> is the number of subnet bits (E.g. 24 for an IPv4 Class C)
110 gIP_LoopInterface.Adapter = IPStack_GetAdapter("/Devices/fifo/anon");
112 DevFS_AddDevice( &gIP_DriverInfo );
114 return MODULE_ERR_OK;
118 * \brief Adds a file to the socket list
120 int IPStack_AddFile(tSocketFile *File)
122 Log_Log("IPStack", "Added file '%s'", File->Name);
123 File->Next = gIP_FileTemplates;
124 gIP_FileTemplates = File;
129 * \brief Read from the IP Stack's Device Directory
131 char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos)
135 ENTER("pNode iPos", Node, Pos);
140 return strdup("routes");
149 for( iface = gIP_Interfaces; iface && Pos--; iface = iface->Next ) ;
151 // Did we run off the end?
159 Log_Warning("IPStack", "IPStack_Root_ReadDir - malloc error");
165 Pos = iface->Node.ImplInt;
171 name[0] = '0' + Pos/10;
172 name[1] = '0' + Pos%10;
176 name[0] = '0' + Pos/100;
177 name[1] = '0' + (Pos/10)%10;
178 name[2] = '0' + Pos%10;
183 // Return the pre-generated name
188 * \brief Get the node of an interface
190 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name)
195 ENTER("pNode sName", Node, Name);
198 if( strcmp(Name, "routes") == 0 ) {
199 return &gIP_RouteNode;
203 if( strcmp(Name, "lo") == 0 ) {
204 return &gIP_LoopInterface.Node;
208 while('0' <= Name[i] && Name[i] <= '9')
211 num += Name[i] - '0';
214 if(Name[i] != '\0') {
219 for( iface = gIP_Interfaces; iface; iface = iface->Next )
221 if( (int)iface->Node.ImplInt == num )
223 LEAVE('p', &iface->Node);
231 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
233 * \brief Handles IOCtls for the IPStack root
235 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data)
238 ENTER("pNode iID pData", Node, ID, Data);
242 // --- Standard IOCtls (0-3) ---
244 LEAVE('i', DRV_TYPE_MISC);
245 return DRV_TYPE_MISC;
247 case DRV_IOCTL_IDENT:
248 tmp = ModUtil_SetIdent(Data, "IPStack");
252 case DRV_IOCTL_VERSION:
256 case DRV_IOCTL_LOOKUP:
257 tmp = ModUtil_LookupString( (char**)casIOCtls_Root, (char*)Data );
263 * - Adds a new IP interface and binds it to a device
266 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
267 if( !CheckString( Data ) ) LEAVE_RET('i', -1);
270 tmp = IPStack_AddInterface(Data, name);
279 * \brief Read from an interface's directory
281 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
283 tSocketFile *file = gIP_FileTemplates;
284 while(Pos-- && file) {
288 if(!file) return NULL;
290 return strdup(file->Name);
294 * \brief Gets a named node from an interface directory
296 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
298 tSocketFile *file = gIP_FileTemplates;
300 // Get file definition
301 for(;file;file = file->Next)
303 if( strcmp(file->Name, Name) == 0 ) break;
305 if(!file) return NULL;
308 return file->Init(Node->ImplPtr);
312 * \brief Names for interface IOCtl Calls
314 static const char *casIOCtls_Iface[] = {
317 "get_address", "set_address",
319 "get_gateway", "set_gateway",
325 * \brief Handles IOCtls for the IPStack interfaces
327 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
330 tInterface *iface = (tInterface*)Node->ImplPtr;
331 ENTER("pNode iID pData", Node, ID, Data);
335 // --- Standard IOCtls (0-3) ---
337 LEAVE('i', DRV_TYPE_MISC);
338 return DRV_TYPE_MISC;
340 case DRV_IOCTL_IDENT:
341 tmp = ModUtil_SetIdent(Data, STR(IDENT));
345 case DRV_IOCTL_VERSION:
349 case DRV_IOCTL_LOOKUP:
350 tmp = ModUtil_LookupString( (char**)casIOCtls_Iface, (char*)Data );
356 * - Get/Set the interface type
363 if( Threads_GetUID() != 0 ) {
364 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
368 if( !CheckMem( Data, sizeof(int) ) ) {
369 LOG("Invalid pointer %p", Data);
373 switch( *(int*)Data )
377 memset(&iface->IP6, 0, sizeof(tIPv6)); // Clear address
381 memset(&iface->IP4, 0, sizeof(tIPv4));
385 memset(&iface->IP6, 0, sizeof(tIPv6));
392 LEAVE('i', iface->Type);
397 * - Get the interface's address
402 case 0: LEAVE_RET('i', 1);
404 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
405 memcpy( Data, &iface->IP4.Address, sizeof(tIPv4) );
408 if( !CheckMem( Data, sizeof(tIPv6) ) ) LEAVE_RET('i', -1);
409 memcpy( Data, &iface->IP6.Address, sizeof(tIPv6) );
416 * - Get the interface's address
419 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
422 case 0: LEAVE_RET('i', 1);
424 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
425 iface->Type = 0; // One very hacky mutex/trash protector
426 memcpy( &iface->IP4.Address, Data, sizeof(tIPv4) );
430 if( !CheckMem( Data, sizeof(tIPv6) ) ) LEAVE_RET('i', -1);
432 memcpy( &iface->IP6.Address, Data, sizeof(tIPv6) );
440 * - Get/Set the bits in the address subnet
446 switch( iface->Type )
448 case 4: LEAVE_RET('i', iface->IP4.SubnetBits);
449 case 6: LEAVE_RET('i', iface->IP6.SubnetBits);
450 default: LEAVE_RET('i', 0);
455 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
456 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
458 // Check and set the subnet bits
459 switch( iface->Type )
462 if( *(int*)Data < 0 || *(int*)Data > 31 ) LEAVE_RET('i', -1);
463 iface->IP4.SubnetBits = *(int*)Data;
464 LEAVE_RET('i', iface->IP4.SubnetBits);
466 if( *(int*)Data < 0 || *(int*)Data > 127 ) LEAVE_RET('i', -1);
467 iface->IP6.SubnetBits = *(int*)Data;
468 LEAVE_RET('i', iface->IP6.SubnetBits);
478 * - Get the interface's IPv4 gateway
486 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
487 memcpy( Data, &iface->IP4.Gateway, sizeof(tIPv4) );
497 * - Get/Set the interface's IPv4 gateway
500 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
507 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
508 iface->Type = 0; // One very hacky mutex/trash protector
509 memcpy( &iface->IP4.Gateway, Data, sizeof(tIPv4) );
520 * - Gets the name of the attached device
524 LEAVE_RET('i', iface->Adapter->DeviceLen);
525 if( !CheckMem( Data, iface->Adapter->DeviceLen+1 ) )
527 strcpy( Data, iface->Adapter->Device );
528 return iface->Adapter->DeviceLen;
532 * - Send an ICMP Echo
541 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
542 tmp = IPv4_Ping(iface, *(tIPv4*)Data);
559 * \fn int IPStack_AddInterface(char *Device)
560 * \brief Adds an interface to the list
562 int IPStack_AddInterface(const char *Device, const char *Name)
567 ENTER("sDevice", Device);
569 card = IPStack_GetAdapter(Device);
571 iface = malloc(sizeof(tInterface) + strlen(Name));
574 return -2; // Return ERR_MYBAD
578 iface->Type = 0; // Unset type
581 iface->Node.ImplPtr = iface;
582 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
583 iface->Node.Size = -1;
584 iface->Node.NumACLs = 1;
585 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
586 iface->Node.ReadDir = IPStack_Iface_ReadDir;
587 iface->Node.FindDir = IPStack_Iface_FindDir;
588 iface->Node.IOCtl = IPStack_Iface_IOCtl;
589 iface->Node.MkNod = NULL;
590 iface->Node.Link = NULL;
591 iface->Node.Relink = NULL;
592 iface->Node.Close = NULL;
595 iface->TimeoutDelay = DEFAULT_TIMEOUT;
597 // Get adapter handle
598 iface->Adapter = IPStack_GetAdapter(Device);
599 if( !iface->Adapter ) {
602 return -1; // Return ERR_YOUFAIL
605 // Delay setting ImplInt until after the adapter is opened
606 // Keeps things simple
607 iface->Node.ImplInt = giIP_NextIfaceId++;
610 SHORTLOCK( &glIP_Interfaces );
611 if( gIP_Interfaces ) {
612 gIP_Interfaces_Last->Next = iface;
613 gIP_Interfaces_Last = iface;
616 gIP_Interfaces = iface;
617 gIP_Interfaces_Last = iface;
619 SHORTREL( &glIP_Interfaces );
621 gIP_DriverInfo.RootNode.Size ++;
624 LEAVE('i', iface->Node.ImplInt);
625 return iface->Node.ImplInt;
629 * \fn tAdapter *IPStack_GetAdapter(char *Path)
630 * \brief Gets/opens an adapter given the path
632 tAdapter *IPStack_GetAdapter(char *Path)
637 ENTER("sPath", Path);
639 Mutex_Acquire( &glIP_Adapters );
641 // Check if this adapter is already open
642 for( dev = gIP_Adapters; dev; dev = dev->Next )
644 if( strcmp(dev->Device, Path) == 0 ) {
646 Mutex_Release( &glIP_Adapters );
652 // Ok, so let's open it
653 dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
655 Mutex_Release( &glIP_Adapters );
661 strcpy( dev->Device, Path );
663 dev->DeviceLen = strlen(Path);
666 dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
667 if( dev->DeviceFD == -1 ) {
669 Mutex_Release( &glIP_Adapters );
674 // Check that it is a network interface
675 tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
676 LOG("Device type = %i", tmp);
677 if( tmp != DRV_TYPE_NETWORK ) {
678 Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
679 VFS_Close( dev->DeviceFD );
681 Mutex_Release( &glIP_Adapters );
687 VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
690 dev->Next = gIP_Adapters;
693 Mutex_Release( &glIP_Adapters );
696 Link_WatchDevice( dev );
703 * \brief Gets the size (in bytes) of a specified form of address
705 int IPStack_GetAddressSize(int AddressType)
713 return sizeof(tIPv4);
715 return sizeof(tIPv6);