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_AddFile(tSocketFile *File);
27 tInterface *IPStack_AddInterface(const char *Device, const char *Name);
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 tInterface *iface = IPStack_AddInterface(Data, name);
223 tmp = iface->Node.ImplInt;
232 * \fn tInterface *IPStack_AddInterface(char *Device)
233 * \brief Adds an interface to the list
235 tInterface *IPStack_AddInterface(const char *Device, const char *Name)
241 ENTER("sDevice", Device);
243 card = IPStack_GetAdapter(Device);
246 return NULL; // ERR_YOURBAD
249 nameLen = sprintf(NULL, "%i", giIP_NextIfaceId);
254 + IPStack_GetAddressSize(-1)*3 // Address, Route->Network, Route->NextHop
258 return NULL; // Return ERR_MYBAD
262 iface->Type = 0; // Unset type
263 iface->Address = iface->Name + nameLen + 1; // Address
264 iface->Route.Network = iface->Address + IPStack_GetAddressSize(-1);
265 iface->Route.NextHop = iface->Route.Network + IPStack_GetAddressSize(-1);
268 iface->Node.ImplPtr = iface;
269 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
270 iface->Node.Size = -1;
271 iface->Node.NumACLs = 1;
272 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
273 iface->Node.ReadDir = IPStack_Iface_ReadDir;
274 iface->Node.FindDir = IPStack_Iface_FindDir;
275 iface->Node.IOCtl = IPStack_Iface_IOCtl;
276 iface->Node.MkNod = NULL;
277 iface->Node.Link = NULL;
278 iface->Node.Relink = NULL;
279 iface->Node.Close = NULL;
282 iface->TimeoutDelay = DEFAULT_TIMEOUT;
284 // Get adapter handle
285 iface->Adapter = IPStack_GetAdapter(Device);
286 if( !iface->Adapter ) {
289 return NULL; // Return ERR_YOUFAIL
292 // Delay setting ImplInt until after the adapter is opened
293 // Keeps things simple
294 iface->Node.ImplInt = giIP_NextIfaceId++;
295 sprintf(iface->Name, "%i", iface->Node.ImplInt);
298 SHORTLOCK( &glIP_Interfaces );
299 if( gIP_Interfaces ) {
300 gIP_Interfaces_Last->Next = iface;
301 gIP_Interfaces_Last = iface;
304 gIP_Interfaces = iface;
305 gIP_Interfaces_Last = iface;
307 SHORTREL( &glIP_Interfaces );
309 // gIP_DriverInfo.RootNode.Size ++;
317 * \brief Adds a file to the socket list
319 int IPStack_AddFile(tSocketFile *File)
321 Log_Log("IPStack", "Added file '%s'", File->Name);
322 File->Next = gIP_FileTemplates;
323 gIP_FileTemplates = File;
331 * \brief Read from an interface's directory
333 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
335 tSocketFile *file = gIP_FileTemplates;
336 while(Pos-- && file) {
340 if(!file) return NULL;
342 return strdup(file->Name);
346 * \brief Gets a named node from an interface directory
348 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
350 tSocketFile *file = gIP_FileTemplates;
352 // Get file definition
353 for(;file;file = file->Next)
355 if( strcmp(file->Name, Name) == 0 ) break;
357 if(!file) return NULL;
360 return file->Init(Node->ImplPtr);
364 * \brief Names for interface IOCtl Calls
366 static const char *casIOCtls_Iface[] = {
369 "get_address", "set_address",
376 * \brief Handles IOCtls for the IPStack interfaces
378 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
381 tInterface *iface = (tInterface*)Node->ImplPtr;
382 ENTER("pNode iID pData", Node, ID, Data);
386 // --- Standard IOCtls (0-3) ---
388 LEAVE('i', DRV_TYPE_MISC);
389 return DRV_TYPE_MISC;
391 case DRV_IOCTL_IDENT:
392 tmp = ModUtil_SetIdent(Data, STR(IDENT));
396 case DRV_IOCTL_VERSION:
400 case DRV_IOCTL_LOOKUP:
401 tmp = ModUtil_LookupString( (char**)casIOCtls_Iface, (char*)Data );
407 * - Get/Set the interface type
414 if( Threads_GetUID() != 0 ) {
415 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
419 if( !CheckMem( Data, sizeof(int) ) ) {
420 LOG("Invalid pointer %p", Data);
426 iface->Type = *(int*)Data;
427 size = IPStack_GetAddressSize(iface->Type);
428 // Check it's actually valid
429 if( iface->Type != 0 && size == 0 ) {
436 memset(iface->Address, 0, size);
438 LEAVE('i', iface->Type);
443 * - Get the interface's address
446 size = IPStack_GetAddressSize(iface->Type);
447 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
448 memcpy( Data, iface->Address, size );
454 * - Set the interface's address
457 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
459 size = IPStack_GetAddressSize(iface->Type);
460 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
461 // TODO: Protect against trashing
462 memcpy( iface->Address, Data, size );
468 * - Get/Set the bits in the address subnet
471 // Do we want to set the value?
474 // Are we root? (TODO: Check Owner/Group)
475 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
476 // Is the memory valid
477 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
480 if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 )
484 iface->SubnetBits = *(int*)Data;
486 LEAVE('i', iface->SubnetBits);
487 return iface->SubnetBits;
491 * - Gets the name of the attached device
494 if( iface->Adapter == NULL )
497 LEAVE_RET('i', iface->Adapter->DeviceLen);
498 if( !CheckMem( Data, iface->Adapter->DeviceLen+1 ) )
500 strcpy( Data, iface->Adapter->Device );
501 return iface->Adapter->DeviceLen;
505 * - Send an ICMP Echo
514 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
515 tmp = IPv4_Ping(iface, *(tIPv4*)Data);
532 * \fn tAdapter *IPStack_GetAdapter(const char *Path)
533 * \brief Gets/opens an adapter given the path
535 tAdapter *IPStack_GetAdapter(const char *Path)
540 ENTER("sPath", Path);
542 // Check for loopback
543 if( strcmp(Path, "LOOPBACK") == 0 )
545 // Initialise if required
546 if( gIP_LoopAdapter.DeviceFD == 0 )
548 dev = &gIP_LoopAdapter;
553 dev->DeviceFD = VFS_Open( "/Devices/fifo/anon", VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
554 if( dev->DeviceFD == -1 ) {
555 Log_Warning("IPStack", "Unable to open FIFO '/Devices/fifo/anon' for loopback");
559 dev->MacAddr.B[0] = 'A';
560 dev->MacAddr.B[1] = 'c';
561 dev->MacAddr.B[2] = 'e';
562 dev->MacAddr.B[3] = 's';
563 dev->MacAddr.B[4] = 's';
564 dev->MacAddr.B[5] = '2';
567 Link_WatchDevice( dev );
569 LEAVE('p', &gIP_LoopAdapter);
570 return &gIP_LoopAdapter;
573 Mutex_Acquire( &glIP_Adapters );
575 // Check if this adapter is already open
576 for( dev = gIP_Adapters; dev; dev = dev->Next )
578 if( strcmp(dev->Device, Path) == 0 ) {
580 Mutex_Release( &glIP_Adapters );
586 // Ok, so let's open it
587 dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
589 Mutex_Release( &glIP_Adapters );
595 strcpy( dev->Device, Path );
597 dev->DeviceLen = strlen(Path);
600 dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
601 if( dev->DeviceFD == -1 ) {
603 Mutex_Release( &glIP_Adapters );
608 // Check that it is a network interface
609 tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
610 LOG("Device type = %i", tmp);
611 if( tmp != DRV_TYPE_NETWORK ) {
612 Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
613 VFS_Close( dev->DeviceFD );
615 Mutex_Release( &glIP_Adapters );
621 VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
624 dev->Next = gIP_Adapters;
627 Mutex_Release( &glIP_Adapters );
630 Link_WatchDevice( dev );