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_LoopAdapter = {
61 tAdapter *gIP_Adapters = NULL;
62 int giIP_NextIfaceId = 1;
67 * \brief Read from the IP Stack's Device Directory
69 char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos)
73 ENTER("pNode iPos", Node, Pos);
78 return strdup("routes");
87 for( iface = gIP_Interfaces; iface && Pos--; iface = iface->Next ) ;
89 // Did we run off the end?
97 Log_Warning("IPStack", "IPStack_Root_ReadDir - malloc error");
103 Pos = iface->Node.ImplInt;
109 name[0] = '0' + Pos/10;
110 name[1] = '0' + Pos%10;
114 name[0] = '0' + Pos/100;
115 name[1] = '0' + (Pos/10)%10;
116 name[2] = '0' + Pos%10;
121 // Return the pre-generated name
126 * \brief Get the node of an interface
128 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name)
135 ENTER("pNode sName", Node, Name);
138 if( strcmp(Name, "routes") == 0 ) {
139 return &gIP_RouteNode;
143 if( strcmp(Name, "lo") == 0 ) {
144 return &gIP_LoopInterface.Node;
149 while('0' <= Name[i] && Name[i] <= '9')
152 num += Name[i] - '0';
155 if(Name[i] != '\0') {
160 for( iface = gIP_Interfaces; iface; iface = iface->Next )
162 if( (int)iface->Node.ImplInt == num )
164 LEAVE('p', &iface->Node);
169 for( iface = gIP_Interfaces; iface; iface = iface->Next )
171 if( strcmp(iface->Name, Name) == 0 )
173 LEAVE('p', &iface->Node);
183 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
185 * \brief Handles IOCtls for the IPStack root
187 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data)
190 ENTER("pNode iID pData", Node, ID, Data);
194 // --- Standard IOCtls (0-3) ---
196 LEAVE('i', DRV_TYPE_MISC);
197 return DRV_TYPE_MISC;
199 case DRV_IOCTL_IDENT:
200 tmp = ModUtil_SetIdent(Data, "IPStack");
204 case DRV_IOCTL_VERSION:
208 case DRV_IOCTL_LOOKUP:
209 tmp = ModUtil_LookupString( (char**)casIOCtls_Root, (char*)Data );
215 * - Adds a new IP interface and binds it to a device
218 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
219 if( !CheckString( Data ) ) LEAVE_RET('i', -1);
222 tmp = IPStack_AddInterface(Data, name);
231 * \fn int IPStack_AddInterface(char *Device)
232 * \brief Adds an interface to the list
234 int IPStack_AddInterface(const char *Device, const char *Name)
240 ENTER("sDevice", Device);
242 card = IPStack_GetAdapter(Device);
245 return -1; // ERR_YOURBAD
248 nameLen = sprintf(NULL, "%i", giIP_NextIfaceId);
253 + IPStack_GetAddressSize(-1)
257 return -2; // Return ERR_MYBAD
261 iface->Type = 0; // Unset type
262 iface->Address = iface->Name + nameLen + 1; // Address
265 iface->Node.ImplPtr = iface;
266 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
267 iface->Node.Size = -1;
268 iface->Node.NumACLs = 1;
269 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
270 iface->Node.ReadDir = IPStack_Iface_ReadDir;
271 iface->Node.FindDir = IPStack_Iface_FindDir;
272 iface->Node.IOCtl = IPStack_Iface_IOCtl;
273 iface->Node.MkNod = NULL;
274 iface->Node.Link = NULL;
275 iface->Node.Relink = NULL;
276 iface->Node.Close = NULL;
279 iface->TimeoutDelay = DEFAULT_TIMEOUT;
281 // Get adapter handle
282 iface->Adapter = IPStack_GetAdapter(Device);
283 if( !iface->Adapter ) {
286 return -1; // Return ERR_YOUFAIL
289 // Delay setting ImplInt until after the adapter is opened
290 // Keeps things simple
291 iface->Node.ImplInt = giIP_NextIfaceId++;
292 sprintf(iface->Name, "%i", iface->Node.ImplInt);
295 SHORTLOCK( &glIP_Interfaces );
296 if( gIP_Interfaces ) {
297 gIP_Interfaces_Last->Next = iface;
298 gIP_Interfaces_Last = iface;
301 gIP_Interfaces = iface;
302 gIP_Interfaces_Last = iface;
304 SHORTREL( &glIP_Interfaces );
306 // gIP_DriverInfo.RootNode.Size ++;
309 LEAVE('i', iface->Node.ImplInt);
310 return iface->Node.ImplInt;
314 * \brief Adds a file to the socket list
316 int IPStack_AddFile(tSocketFile *File)
318 Log_Log("IPStack", "Added file '%s'", File->Name);
319 File->Next = gIP_FileTemplates;
320 gIP_FileTemplates = File;
328 * \brief Read from an interface's directory
330 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
332 tSocketFile *file = gIP_FileTemplates;
333 while(Pos-- && file) {
337 if(!file) return NULL;
339 return strdup(file->Name);
343 * \brief Gets a named node from an interface directory
345 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
347 tSocketFile *file = gIP_FileTemplates;
349 // Get file definition
350 for(;file;file = file->Next)
352 if( strcmp(file->Name, Name) == 0 ) break;
354 if(!file) return NULL;
357 return file->Init(Node->ImplPtr);
361 * \brief Names for interface IOCtl Calls
363 static const char *casIOCtls_Iface[] = {
366 "get_address", "set_address",
373 * \brief Handles IOCtls for the IPStack interfaces
375 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
378 tInterface *iface = (tInterface*)Node->ImplPtr;
379 ENTER("pNode iID pData", Node, ID, Data);
383 // --- Standard IOCtls (0-3) ---
385 LEAVE('i', DRV_TYPE_MISC);
386 return DRV_TYPE_MISC;
388 case DRV_IOCTL_IDENT:
389 tmp = ModUtil_SetIdent(Data, STR(IDENT));
393 case DRV_IOCTL_VERSION:
397 case DRV_IOCTL_LOOKUP:
398 tmp = ModUtil_LookupString( (char**)casIOCtls_Iface, (char*)Data );
404 * - Get/Set the interface type
411 if( Threads_GetUID() != 0 ) {
412 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
416 if( !CheckMem( Data, sizeof(int) ) ) {
417 LOG("Invalid pointer %p", Data);
423 iface->Type = *(int*)Data;
424 size = IPStack_GetAddressSize(iface->Type);
425 // Check it's actually valid
426 if( iface->Type != 0 && size == 0 ) {
433 memset(iface->Address, 0, size);
435 LEAVE('i', iface->Type);
440 * - Get the interface's address
443 size = IPStack_GetAddressSize(iface->Type);
444 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
445 memcpy( Data, iface->Address, size );
451 * - Set the interface's address
454 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
456 size = IPStack_GetAddressSize(iface->Type);
457 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
458 // TODO: Protect against trashing
459 memcpy( iface->Address, Data, size );
465 * - Get/Set the bits in the address subnet
468 // Do we want to set the value?
471 // Are we root? (TODO: Check Owner/Group)
472 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
473 // Is the memory valid
474 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
477 if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 )
481 iface->SubnetBits = *(int*)Data;
483 LEAVE('i', iface->SubnetBits);
484 return iface->SubnetBits;
488 * - Gets the name of the attached device
491 if( iface->Adapter == NULL )
494 LEAVE_RET('i', iface->Adapter->DeviceLen);
495 if( !CheckMem( Data, iface->Adapter->DeviceLen+1 ) )
497 strcpy( Data, iface->Adapter->Device );
498 return iface->Adapter->DeviceLen;
502 * - Send an ICMP Echo
511 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
512 tmp = IPv4_Ping(iface, *(tIPv4*)Data);
529 * \fn tAdapter *IPStack_GetAdapter(const char *Path)
530 * \brief Gets/opens an adapter given the path
532 tAdapter *IPStack_GetAdapter(const char *Path)
537 ENTER("sPath", Path);
539 // Check for loopback
540 if( strcmp(Path, "LOOPBACK") == 0 )
542 // Initialise if required
543 if( gIP_LoopAdapter.DeviceFD == 0 )
545 dev = &gIP_LoopAdapter;
550 dev->DeviceFD = VFS_Open( "/Devices/fifo/anon", VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
551 if( dev->DeviceFD == -1 ) {
552 Log_Warning("IPStack", "Unable to open FIFO '/Devices/fifo/anon' for loopback");
556 dev->MacAddr.B[0] = 'A';
557 dev->MacAddr.B[1] = 'c';
558 dev->MacAddr.B[2] = 'e';
559 dev->MacAddr.B[3] = 's';
560 dev->MacAddr.B[4] = 's';
561 dev->MacAddr.B[5] = '2';
564 Link_WatchDevice( dev );
566 LEAVE('p', &gIP_LoopAdapter);
567 return &gIP_LoopAdapter;
570 Mutex_Acquire( &glIP_Adapters );
572 // Check if this adapter is already open
573 for( dev = gIP_Adapters; dev; dev = dev->Next )
575 if( strcmp(dev->Device, Path) == 0 ) {
577 Mutex_Release( &glIP_Adapters );
583 // Ok, so let's open it
584 dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
586 Mutex_Release( &glIP_Adapters );
592 strcpy( dev->Device, Path );
594 dev->DeviceLen = strlen(Path);
597 dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
598 if( dev->DeviceFD == -1 ) {
600 Mutex_Release( &glIP_Adapters );
605 // Check that it is a network interface
606 tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
607 LOG("Device type = %i", tmp);
608 if( tmp != DRV_TYPE_NETWORK ) {
609 Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
610 VFS_Close( dev->DeviceFD );
612 Mutex_Release( &glIP_Adapters );
618 VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
621 dev->Next = gIP_Adapters;
624 Mutex_Release( &glIP_Adapters );
627 Link_WatchDevice( dev );