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_ReadDir(tVFS_Node *Node, int Pos);
27 tVFS_Node *IPStack_FindDir(tVFS_Node *Node, char *Name);
28 int IPStack_IOCtl(tVFS_Node *Node, int ID, void *Data);
29 int IPStack_AddInterface(char *Device);
30 tAdapter *IPStack_GetAdapter(char *Path);
33 MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
34 tDevFS_Driver gIP_DriverInfo = {
37 .Size = 0, // Number of interfaces
39 .ACLs = &gVFS_ACL_EveryoneRX,
40 .Flags = VFS_FFLAG_DIRECTORY,
41 .ReadDir = IPStack_ReadDir,
42 .FindDir = IPStack_FindDir,
43 .IOCtl = IPStack_IOCtlRoot
46 tSpinlock glIP_Interfaces = 0;
47 tInterface *gIP_Interfaces = NULL;
48 tInterface *gIP_Interfaces_Last = NULL;
49 int giIP_NextIfaceId = 1;
50 tSpinlock glIP_Adapters = 0;
51 tAdapter *gIP_Adapters = NULL;
55 * \fn int IPStack_Install(char **Arguments)
56 * \brief Intialise the relevant parts of the stack and register with DevFS
58 int IPStack_Install(char **Arguments)
68 // Parse module arguments
69 for( i = 0; Arguments[i]; i++ )
71 //if(strcmp(Arguments[i], "Device") == '=') {
77 DevFS_AddDevice( &gIP_DriverInfo );
83 * \brief Read from the IP Stack's Device Directory
85 char *IPStack_ReadDir(tVFS_Node *Node, int Pos)
89 ENTER("pNode iPos", Node, Pos);
92 for( iface = gIP_Interfaces; iface && Pos--; iface = iface->Next ) ;
94 // Did we run off the end?
105 Pos = iface->Node.ImplInt;
111 name[2] = '0' + Pos/10;
112 name[3] = '0' + Pos%10;
116 name[2] = '0' + Pos/100;
117 name[3] = '0' + (Pos/10)%10;
118 name[4] = '0' + Pos%10;
123 // Return the pre-generated name
128 * \brief Get the node of an interface
130 tVFS_Node *IPStack_FindDir(tVFS_Node *Node, char *Name)
135 ENTER("pNode sName", Node, Name);
137 if(Name[0] != 'i' || Name[1] != 'p') {
143 while('0' <= Name[i] && Name[i] <= '9')
146 num += Name[i] - '0';
149 if(Name[i] != '\0') {
154 for( iface = gIP_Interfaces; iface; iface = iface->Next )
156 if( iface->Node.ImplInt == num )
158 LEAVE('p', &iface->Node);
166 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
168 * \brief Handles IOCtls for the IPStack root
170 int IPStack_IOCtlRoot(tVFS_Node *Node, int ID, void *Data)
173 ENTER("pNode iID pData", Node, ID, Data);
177 // --- Standard IOCtls (0-3) ---
179 LEAVE('i', DRV_TYPE_MISC);
180 return DRV_TYPE_MISC;
182 case DRV_IOCTL_IDENT:
183 tmp = ModUtil_SetIdent(Data, "IPStack");
187 case DRV_IOCTL_VERSION:
191 case DRV_IOCTL_LOOKUP:
192 tmp = ModUtil_LookupString( (char**)casIOCtls_Root, (char*)Data );
198 * - Adds a new IP interface and binds it to a device
201 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
202 if( !CheckString( Data ) ) LEAVE_RET('i', -1);
203 tmp = IPStack_AddInterface(Data);
210 static const char *casIOCtls_Iface[] = {
213 "get_address", "set_address",
215 "get_gateway", "set_gateway",
220 * \brief Handles IOCtls for the IPStack interfaces
222 int IPStack_IOCtl(tVFS_Node *Node, int ID, void *Data)
225 tInterface *iface = (tInterface*)Node->ImplPtr;
226 ENTER("pNode iID pData", Node, ID, Data);
230 // --- Standard IOCtls (0-3) ---
232 LEAVE('i', DRV_TYPE_MISC);
233 return DRV_TYPE_MISC;
235 case DRV_IOCTL_IDENT:
236 tmp = ModUtil_SetIdent(Data, STR(IDENT));
240 case DRV_IOCTL_VERSION:
244 case DRV_IOCTL_LOOKUP:
245 tmp = ModUtil_LookupString( (char**)casIOCtls_Iface, (char*)Data );
251 * - Get/Set the interface type
258 if( Threads_GetUID() != 0 ) {
259 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
263 if( !CheckMem( Data, sizeof(int) ) ) {
264 LOG("Invalid pointer %p", Data);
268 switch( *(int*)Data )
272 memset(&iface->IP6, 0, sizeof(tIPv6)); // Clear address
276 memset(&iface->IP4, 0, sizeof(tIPv4));
280 memset(&iface->IP6, 0, sizeof(tIPv6));
287 LEAVE('i', iface->Type);
292 * - Get the interface's address
297 case 0: LEAVE_RET('i', 1);
299 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
300 memcpy( Data, &iface->IP4.Address, sizeof(tIPv4) );
303 if( !CheckMem( Data, sizeof(tIPv6) ) ) LEAVE_RET('i', -1);
304 memcpy( Data, &iface->IP6.Address, sizeof(tIPv6) );
311 * - Get the interface's address
314 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
317 case 0: LEAVE_RET('i', 1);
319 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
320 iface->Type = 0; // One very hacky mutex/trash protector
321 memcpy( &iface->IP4.Address, Data, sizeof(tIPv4) );
325 if( !CheckMem( Data, sizeof(tIPv6) ) ) LEAVE_RET('i', -1);
327 memcpy( &iface->IP6.Address, Data, sizeof(tIPv6) );
335 * - Get/Set the bits in the address subnet
341 switch( iface->Type )
343 case 4: LEAVE_RET('i', iface->IP4.SubnetBits);
344 case 6: LEAVE_RET('i', iface->IP6.SubnetBits);
345 default: LEAVE_RET('i', 0);
350 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
351 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
353 // Check and set the subnet bits
354 switch( iface->Type )
357 if( *(int*)Data < 0 || *(int*)Data > 31 ) LEAVE_RET('i', -1);
358 iface->IP4.SubnetBits = *(int*)Data;
359 LEAVE_RET('i', iface->IP4.SubnetBits);
361 if( *(int*)Data < 0 || *(int*)Data > 127 ) LEAVE_RET('i', -1);
362 iface->IP6.SubnetBits = *(int*)Data;
363 LEAVE_RET('i', iface->IP6.SubnetBits);
373 * - Get the interface's IPv4 gateway
381 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
382 memcpy( Data, &iface->IP4.Gateway, sizeof(tIPv4) );
392 * - Get/Set the interface's IPv4 gateway
395 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
402 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
403 iface->Type = 0; // One very hacky mutex/trash protector
404 memcpy( &iface->IP4.Gateway, Data, sizeof(tIPv4) );
415 * - Send an ICMP Echo
424 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
425 tmp = IPv4_Ping(iface, *(tIPv4*)Data);
442 * \fn int IPStack_AddInterface(char *Device)
443 * \brief Adds an interface to the list
445 int IPStack_AddInterface(char *Device)
449 ENTER("sDevice", Device);
451 iface = malloc(sizeof(tInterface));
454 return -2; // Return ERR_MYBAD
458 iface->Type = 0; // Unset type
461 iface->Node.ImplPtr = iface;
462 iface->Node.ImplInt = giIP_NextIfaceId++;
463 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
464 iface->Node.Size = 0;
465 iface->Node.NumACLs = 1;
466 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
467 iface->Node.ReadDir = NULL;
468 iface->Node.FindDir = NULL;
469 iface->Node.IOCtl = IPStack_IOCtl;
472 iface->TimeoutDelay = DEFAULT_TIMEOUT;
474 // Get adapter handle
475 iface->Adapter = IPStack_GetAdapter(Device);
476 if( !iface->Adapter ) {
479 return -1; // Return ERR_YOUFAIL
483 LOCK( &glIP_Interfaces );
484 if( gIP_Interfaces ) {
485 gIP_Interfaces_Last->Next = iface;
486 gIP_Interfaces_Last = iface;
489 gIP_Interfaces = iface;
490 gIP_Interfaces_Last = iface;
492 RELEASE( &glIP_Interfaces );
494 gIP_DriverInfo.RootNode.Size ++;
497 LEAVE('i', iface->Node.ImplInt);
498 return iface->Node.ImplInt;
502 * \fn tAdapter *IPStack_GetAdapter(char *Path)
503 * \brief Gets/opens an adapter given the path
505 tAdapter *IPStack_GetAdapter(char *Path)
510 ENTER("sPath", Path);
512 LOCK( &glIP_Adapters );
514 // Check if this adapter is already open
515 for( dev = gIP_Adapters; dev; dev = dev->Next )
517 if( strcmp(dev->Device, Path) == 0 ) {
519 RELEASE( &glIP_Adapters );
525 // Ok, so let's open it
526 dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
528 RELEASE( &glIP_Adapters );
534 strcpy( dev->Device, Path );
538 dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
539 if( dev->DeviceFD == -1 ) {
541 RELEASE( &glIP_Adapters );
546 // Check that it is a network interface
547 tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
548 LOG("Device type = %i", tmp);
549 if( tmp != DRV_TYPE_NETWORK ) {
550 Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
551 VFS_Close( dev->DeviceFD );
553 RELEASE( &glIP_Adapters );
559 VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
562 dev->Next = gIP_Adapters;
565 RELEASE( &glIP_Adapters );
568 Link_WatchDevice( dev );