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)
258 return NULL; // Return ERR_MYBAD
262 iface->Type = 0; // Unset type
263 iface->Address = iface->Name + nameLen + 1; // Address
266 iface->Node.ImplPtr = iface;
267 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
268 iface->Node.Size = -1;
269 iface->Node.NumACLs = 1;
270 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
271 iface->Node.ReadDir = IPStack_Iface_ReadDir;
272 iface->Node.FindDir = IPStack_Iface_FindDir;
273 iface->Node.IOCtl = IPStack_Iface_IOCtl;
274 iface->Node.MkNod = NULL;
275 iface->Node.Link = NULL;
276 iface->Node.Relink = NULL;
277 iface->Node.Close = NULL;
280 iface->TimeoutDelay = DEFAULT_TIMEOUT;
282 // Get adapter handle
283 iface->Adapter = IPStack_GetAdapter(Device);
284 if( !iface->Adapter ) {
287 return NULL; // Return ERR_YOUFAIL
290 // Delay setting ImplInt until after the adapter is opened
291 // Keeps things simple
292 iface->Node.ImplInt = giIP_NextIfaceId++;
293 sprintf(iface->Name, "%i", iface->Node.ImplInt);
296 SHORTLOCK( &glIP_Interfaces );
297 if( gIP_Interfaces ) {
298 gIP_Interfaces_Last->Next = iface;
299 gIP_Interfaces_Last = iface;
302 gIP_Interfaces = iface;
303 gIP_Interfaces_Last = iface;
305 SHORTREL( &glIP_Interfaces );
307 // gIP_DriverInfo.RootNode.Size ++;
315 * \brief Adds a file to the socket list
317 int IPStack_AddFile(tSocketFile *File)
319 Log_Log("IPStack", "Added file '%s'", File->Name);
320 File->Next = gIP_FileTemplates;
321 gIP_FileTemplates = File;
329 * \brief Read from an interface's directory
331 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
333 tSocketFile *file = gIP_FileTemplates;
334 while(Pos-- && file) {
338 if(!file) return NULL;
340 return strdup(file->Name);
344 * \brief Gets a named node from an interface directory
346 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
348 tSocketFile *file = gIP_FileTemplates;
350 // Get file definition
351 for(;file;file = file->Next)
353 if( strcmp(file->Name, Name) == 0 ) break;
355 if(!file) return NULL;
358 return file->Init(Node->ImplPtr);
362 * \brief Names for interface IOCtl Calls
364 static const char *casIOCtls_Iface[] = {
367 "get_address", "set_address",
374 * \brief Handles IOCtls for the IPStack interfaces
376 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
379 tInterface *iface = (tInterface*)Node->ImplPtr;
380 ENTER("pNode iID pData", Node, ID, Data);
384 // --- Standard IOCtls (0-3) ---
386 LEAVE('i', DRV_TYPE_MISC);
387 return DRV_TYPE_MISC;
389 case DRV_IOCTL_IDENT:
390 tmp = ModUtil_SetIdent(Data, STR(IDENT));
394 case DRV_IOCTL_VERSION:
398 case DRV_IOCTL_LOOKUP:
399 tmp = ModUtil_LookupString( (char**)casIOCtls_Iface, (char*)Data );
405 * - Get/Set the interface type
412 if( Threads_GetUID() != 0 ) {
413 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
417 if( !CheckMem( Data, sizeof(int) ) ) {
418 LOG("Invalid pointer %p", Data);
424 iface->Type = *(int*)Data;
425 size = IPStack_GetAddressSize(iface->Type);
426 // Check it's actually valid
427 if( iface->Type != 0 && size == 0 ) {
434 memset(iface->Address, 0, size);
436 LEAVE('i', iface->Type);
441 * - Get the interface's address
444 size = IPStack_GetAddressSize(iface->Type);
445 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
446 memcpy( Data, iface->Address, size );
452 * - Set the interface's address
455 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
457 size = IPStack_GetAddressSize(iface->Type);
458 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
459 // TODO: Protect against trashing
460 memcpy( iface->Address, Data, size );
466 * - Get/Set the bits in the address subnet
469 // Do we want to set the value?
472 // Are we root? (TODO: Check Owner/Group)
473 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
474 // Is the memory valid
475 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
478 if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 )
482 iface->SubnetBits = *(int*)Data;
484 LEAVE('i', iface->SubnetBits);
485 return iface->SubnetBits;
489 * - Gets the name of the attached device
492 if( iface->Adapter == NULL )
495 LEAVE_RET('i', iface->Adapter->DeviceLen);
496 if( !CheckMem( Data, iface->Adapter->DeviceLen+1 ) )
498 strcpy( Data, iface->Adapter->Device );
499 return iface->Adapter->DeviceLen;
503 * - Send an ICMP Echo
512 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
513 tmp = IPv4_Ping(iface, *(tIPv4*)Data);
530 * \fn tAdapter *IPStack_GetAdapter(const char *Path)
531 * \brief Gets/opens an adapter given the path
533 tAdapter *IPStack_GetAdapter(const char *Path)
538 ENTER("sPath", Path);
540 // Check for loopback
541 if( strcmp(Path, "LOOPBACK") == 0 )
543 // Initialise if required
544 if( gIP_LoopAdapter.DeviceFD == 0 )
546 dev = &gIP_LoopAdapter;
551 dev->DeviceFD = VFS_Open( "/Devices/fifo/anon", VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
552 if( dev->DeviceFD == -1 ) {
553 Log_Warning("IPStack", "Unable to open FIFO '/Devices/fifo/anon' for loopback");
557 dev->MacAddr.B[0] = 'A';
558 dev->MacAddr.B[1] = 'c';
559 dev->MacAddr.B[2] = 'e';
560 dev->MacAddr.B[3] = 's';
561 dev->MacAddr.B[4] = 's';
562 dev->MacAddr.B[5] = '2';
565 Link_WatchDevice( dev );
567 LEAVE('p', &gIP_LoopAdapter);
568 return &gIP_LoopAdapter;
571 Mutex_Acquire( &glIP_Adapters );
573 // Check if this adapter is already open
574 for( dev = gIP_Adapters; dev; dev = dev->Next )
576 if( strcmp(dev->Device, Path) == 0 ) {
578 Mutex_Release( &glIP_Adapters );
584 // Ok, so let's open it
585 dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
587 Mutex_Release( &glIP_Adapters );
593 strcpy( dev->Device, Path );
595 dev->DeviceLen = strlen(Path);
598 dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
599 if( dev->DeviceFD == -1 ) {
601 Mutex_Release( &glIP_Adapters );
606 // Check that it is a network interface
607 tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
608 LOG("Device type = %i", tmp);
609 if( tmp != DRV_TYPE_NETWORK ) {
610 Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
611 VFS_Close( dev->DeviceFD );
613 Mutex_Release( &glIP_Adapters );
619 VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
622 dev->Next = gIP_Adapters;
625 Mutex_Release( &glIP_Adapters );
628 Link_WatchDevice( dev );