6 #define VERSION VER2(0,10)
9 #include <tpl_drv_common.h>
10 #include <tpl_drv_network.h>
13 //! Default timeout value, 30 seconds
14 #define DEFAULT_TIMEOUT (30*1000)
17 extern int IPv4_Ping(tInterface *Iface, tIPv4 Addr);
18 //extern int IPv6_Ping(tInterface *Iface, tIPv6 Addr);
19 extern tVFS_Node gIP_RouteNode;
22 char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos);
23 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name);
24 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data);
26 int IPStack_AddInterface(const char *Device, const char *Name);
27 int IPStack_AddFile(tSocketFile *File);
28 tAdapter *IPStack_GetAdapter(const char *Path);
30 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos);
31 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name);
32 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data);
35 //! Loopback (127.0.0.0/8, ::1) Pseudo-Interface
36 tInterface gIP_LoopInterface = {
38 ImplPtr: &gIP_LoopInterface,
39 Flags: VFS_FFLAG_DIRECTORY,
42 ACLs: &gVFS_ACL_EveryoneRX,
43 ReadDir: IPStack_Iface_ReadDir,
44 FindDir: IPStack_Iface_FindDir,
45 IOCtl: IPStack_Iface_IOCtl
50 tShortSpinlock glIP_Interfaces;
51 tInterface *gIP_Interfaces = NULL;
52 tInterface *gIP_Interfaces_Last = NULL;
54 tSocketFile *gIP_FileTemplates;
56 tAdapter *gIP_Adapters = NULL;
57 int giIP_NextIfaceId = 1;
62 * \brief Read from the IP Stack's Device Directory
64 char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos)
68 ENTER("pNode iPos", Node, Pos);
73 return strdup("routes");
82 for( iface = gIP_Interfaces; iface && Pos--; iface = iface->Next ) ;
84 // Did we run off the end?
92 Log_Warning("IPStack", "IPStack_Root_ReadDir - malloc error");
98 Pos = iface->Node.ImplInt;
104 name[0] = '0' + Pos/10;
105 name[1] = '0' + Pos%10;
109 name[0] = '0' + Pos/100;
110 name[1] = '0' + (Pos/10)%10;
111 name[2] = '0' + Pos%10;
116 // Return the pre-generated name
121 * \brief Get the node of an interface
123 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name)
128 ENTER("pNode sName", Node, Name);
131 if( strcmp(Name, "routes") == 0 ) {
132 return &gIP_RouteNode;
136 if( strcmp(Name, "lo") == 0 ) {
137 return &gIP_LoopInterface.Node;
141 while('0' <= Name[i] && Name[i] <= '9')
144 num += Name[i] - '0';
147 if(Name[i] != '\0') {
152 for( iface = gIP_Interfaces; iface; iface = iface->Next )
154 if( (int)iface->Node.ImplInt == num )
156 LEAVE('p', &iface->Node);
164 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
166 * \brief Handles IOCtls for the IPStack root
168 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data)
171 ENTER("pNode iID pData", Node, ID, Data);
175 // --- Standard IOCtls (0-3) ---
177 LEAVE('i', DRV_TYPE_MISC);
178 return DRV_TYPE_MISC;
180 case DRV_IOCTL_IDENT:
181 tmp = ModUtil_SetIdent(Data, "IPStack");
185 case DRV_IOCTL_VERSION:
189 case DRV_IOCTL_LOOKUP:
190 tmp = ModUtil_LookupString( (char**)casIOCtls_Root, (char*)Data );
196 * - Adds a new IP interface and binds it to a device
199 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
200 if( !CheckString( Data ) ) LEAVE_RET('i', -1);
203 tmp = IPStack_AddInterface(Data, name);
212 * \fn int IPStack_AddInterface(char *Device)
213 * \brief Adds an interface to the list
215 int IPStack_AddInterface(const char *Device, const char *Name)
220 ENTER("sDevice", Device);
222 card = IPStack_GetAdapter(Device);
224 iface = malloc(sizeof(tInterface) + strlen(Name));
227 return -2; // Return ERR_MYBAD
231 iface->Type = 0; // Unset type
234 iface->Node.ImplPtr = iface;
235 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
236 iface->Node.Size = -1;
237 iface->Node.NumACLs = 1;
238 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
239 iface->Node.ReadDir = IPStack_Iface_ReadDir;
240 iface->Node.FindDir = IPStack_Iface_FindDir;
241 iface->Node.IOCtl = IPStack_Iface_IOCtl;
242 iface->Node.MkNod = NULL;
243 iface->Node.Link = NULL;
244 iface->Node.Relink = NULL;
245 iface->Node.Close = NULL;
248 iface->TimeoutDelay = DEFAULT_TIMEOUT;
250 // Get adapter handle
251 iface->Adapter = IPStack_GetAdapter(Device);
252 if( !iface->Adapter ) {
255 return -1; // Return ERR_YOUFAIL
258 // Delay setting ImplInt until after the adapter is opened
259 // Keeps things simple
260 iface->Node.ImplInt = giIP_NextIfaceId++;
263 SHORTLOCK( &glIP_Interfaces );
264 if( gIP_Interfaces ) {
265 gIP_Interfaces_Last->Next = iface;
266 gIP_Interfaces_Last = iface;
269 gIP_Interfaces = iface;
270 gIP_Interfaces_Last = iface;
272 SHORTREL( &glIP_Interfaces );
274 // gIP_DriverInfo.RootNode.Size ++;
277 LEAVE('i', iface->Node.ImplInt);
278 return iface->Node.ImplInt;
282 * \brief Adds a file to the socket list
284 int IPStack_AddFile(tSocketFile *File)
286 Log_Log("IPStack", "Added file '%s'", File->Name);
287 File->Next = gIP_FileTemplates;
288 gIP_FileTemplates = File;
296 * \brief Read from an interface's directory
298 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
300 tSocketFile *file = gIP_FileTemplates;
301 while(Pos-- && file) {
305 if(!file) return NULL;
307 return strdup(file->Name);
311 * \brief Gets a named node from an interface directory
313 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
315 tSocketFile *file = gIP_FileTemplates;
317 // Get file definition
318 for(;file;file = file->Next)
320 if( strcmp(file->Name, Name) == 0 ) break;
322 if(!file) return NULL;
325 return file->Init(Node->ImplPtr);
329 * \brief Names for interface IOCtl Calls
331 static const char *casIOCtls_Iface[] = {
334 "get_address", "set_address",
336 "get_gateway", "set_gateway",
342 * \brief Handles IOCtls for the IPStack interfaces
344 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
347 tInterface *iface = (tInterface*)Node->ImplPtr;
348 ENTER("pNode iID pData", Node, ID, Data);
352 // --- Standard IOCtls (0-3) ---
354 LEAVE('i', DRV_TYPE_MISC);
355 return DRV_TYPE_MISC;
357 case DRV_IOCTL_IDENT:
358 tmp = ModUtil_SetIdent(Data, STR(IDENT));
362 case DRV_IOCTL_VERSION:
366 case DRV_IOCTL_LOOKUP:
367 tmp = ModUtil_LookupString( (char**)casIOCtls_Iface, (char*)Data );
373 * - Get/Set the interface type
380 if( Threads_GetUID() != 0 ) {
381 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
385 if( !CheckMem( Data, sizeof(int) ) ) {
386 LOG("Invalid pointer %p", Data);
390 switch( *(int*)Data )
394 memset(&iface->IP6, 0, sizeof(tIPv6)); // Clear address
398 memset(&iface->IP4, 0, sizeof(tIPv4));
402 memset(&iface->IP6, 0, sizeof(tIPv6));
409 LEAVE('i', iface->Type);
414 * - Get the interface's address
419 case 0: LEAVE_RET('i', 1);
421 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
422 memcpy( Data, &iface->IP4.Address, sizeof(tIPv4) );
425 if( !CheckMem( Data, sizeof(tIPv6) ) ) LEAVE_RET('i', -1);
426 memcpy( Data, &iface->IP6.Address, sizeof(tIPv6) );
433 * - Get the interface's address
436 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
439 case 0: LEAVE_RET('i', 1);
441 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
442 iface->Type = 0; // One very hacky mutex/trash protector
443 memcpy( &iface->IP4.Address, Data, sizeof(tIPv4) );
447 if( !CheckMem( Data, sizeof(tIPv6) ) ) LEAVE_RET('i', -1);
449 memcpy( &iface->IP6.Address, Data, sizeof(tIPv6) );
457 * - Get/Set the bits in the address subnet
463 switch( iface->Type )
465 case 4: LEAVE_RET('i', iface->IP4.SubnetBits);
466 case 6: LEAVE_RET('i', iface->IP6.SubnetBits);
467 default: LEAVE_RET('i', 0);
472 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
473 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
475 // Check and set the subnet bits
476 switch( iface->Type )
479 if( *(int*)Data < 0 || *(int*)Data > 31 ) LEAVE_RET('i', -1);
480 iface->IP4.SubnetBits = *(int*)Data;
481 LEAVE_RET('i', iface->IP4.SubnetBits);
483 if( *(int*)Data < 0 || *(int*)Data > 127 ) LEAVE_RET('i', -1);
484 iface->IP6.SubnetBits = *(int*)Data;
485 LEAVE_RET('i', iface->IP6.SubnetBits);
495 * - Get the interface's IPv4 gateway
503 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
504 memcpy( Data, &iface->IP4.Gateway, sizeof(tIPv4) );
514 * - Get/Set the interface's IPv4 gateway
517 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
524 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
525 iface->Type = 0; // One very hacky mutex/trash protector
526 memcpy( &iface->IP4.Gateway, Data, sizeof(tIPv4) );
537 * - Gets the name of the attached device
541 LEAVE_RET('i', iface->Adapter->DeviceLen);
542 if( !CheckMem( Data, iface->Adapter->DeviceLen+1 ) )
544 strcpy( Data, iface->Adapter->Device );
545 return iface->Adapter->DeviceLen;
549 * - Send an ICMP Echo
558 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
559 tmp = IPv4_Ping(iface, *(tIPv4*)Data);
576 * \fn tAdapter *IPStack_GetAdapter(const char *Path)
577 * \brief Gets/opens an adapter given the path
579 tAdapter *IPStack_GetAdapter(const char *Path)
584 ENTER("sPath", Path);
586 Mutex_Acquire( &glIP_Adapters );
588 // Check if this adapter is already open
589 for( dev = gIP_Adapters; dev; dev = dev->Next )
591 if( strcmp(dev->Device, Path) == 0 ) {
593 Mutex_Release( &glIP_Adapters );
599 // Ok, so let's open it
600 dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
602 Mutex_Release( &glIP_Adapters );
608 strcpy( dev->Device, Path );
610 dev->DeviceLen = strlen(Path);
613 dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
614 if( dev->DeviceFD == -1 ) {
616 Mutex_Release( &glIP_Adapters );
621 // Check that it is a network interface
622 tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
623 LOG("Device type = %i", tmp);
624 if( tmp != DRV_TYPE_NETWORK ) {
625 Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
626 VFS_Close( dev->DeviceFD );
628 Mutex_Release( &glIP_Adapters );
634 VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
637 dev->Next = gIP_Adapters;
640 Mutex_Release( &glIP_Adapters );
643 Link_WatchDevice( dev );