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)
130 ENTER("pNode sName", Node, Name);
133 if( strcmp(Name, "routes") == 0 ) {
134 return &gIP_RouteNode;
138 if( strcmp(Name, "lo") == 0 ) {
139 return &gIP_LoopInterface.Node;
144 while('0' <= Name[i] && Name[i] <= '9')
147 num += Name[i] - '0';
150 if(Name[i] != '\0') {
155 for( iface = gIP_Interfaces; iface; iface = iface->Next )
157 if( (int)iface->Node.ImplInt == num )
159 LEAVE('p', &iface->Node);
164 for( iface = gIP_Interfaces; iface; iface = iface->Next )
166 if( strcmp(iface->Name, Name) == 0 )
168 LEAVE('p', &iface->Node);
178 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
180 * \brief Handles IOCtls for the IPStack root
182 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data)
185 ENTER("pNode iID pData", Node, ID, Data);
189 // --- Standard IOCtls (0-3) ---
191 LEAVE('i', DRV_TYPE_MISC);
192 return DRV_TYPE_MISC;
194 case DRV_IOCTL_IDENT:
195 tmp = ModUtil_SetIdent(Data, "IPStack");
199 case DRV_IOCTL_VERSION:
203 case DRV_IOCTL_LOOKUP:
204 tmp = ModUtil_LookupString( (char**)casIOCtls_Root, (char*)Data );
210 * - Adds a new IP interface and binds it to a device
213 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
214 if( !CheckString( Data ) ) LEAVE_RET('i', -1);
217 tmp = IPStack_AddInterface(Data, name);
226 * \fn int IPStack_AddInterface(char *Device)
227 * \brief Adds an interface to the list
229 int IPStack_AddInterface(const char *Device, const char *Name)
235 ENTER("sDevice", Device);
237 card = IPStack_GetAdapter(Device);
240 return -1; // ERR_YOURBAD
243 nameLen = sprintf(NULL, "%i", giIP_NextIfaceId);
248 + IPStack_GetAddressSize(-1)
252 return -2; // Return ERR_MYBAD
256 iface->Type = 0; // Unset type
257 iface->Address = iface->Name + nameLen + 1; // Address
260 iface->Node.ImplPtr = iface;
261 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
262 iface->Node.Size = -1;
263 iface->Node.NumACLs = 1;
264 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
265 iface->Node.ReadDir = IPStack_Iface_ReadDir;
266 iface->Node.FindDir = IPStack_Iface_FindDir;
267 iface->Node.IOCtl = IPStack_Iface_IOCtl;
268 iface->Node.MkNod = NULL;
269 iface->Node.Link = NULL;
270 iface->Node.Relink = NULL;
271 iface->Node.Close = NULL;
274 iface->TimeoutDelay = DEFAULT_TIMEOUT;
276 // Get adapter handle
277 iface->Adapter = IPStack_GetAdapter(Device);
278 if( !iface->Adapter ) {
281 return -1; // Return ERR_YOUFAIL
284 // Delay setting ImplInt until after the adapter is opened
285 // Keeps things simple
286 iface->Node.ImplInt = giIP_NextIfaceId++;
287 sprintf(iface->Name, "%i", iface->Node.ImplInt);
290 SHORTLOCK( &glIP_Interfaces );
291 if( gIP_Interfaces ) {
292 gIP_Interfaces_Last->Next = iface;
293 gIP_Interfaces_Last = iface;
296 gIP_Interfaces = iface;
297 gIP_Interfaces_Last = iface;
299 SHORTREL( &glIP_Interfaces );
301 // gIP_DriverInfo.RootNode.Size ++;
304 LEAVE('i', iface->Node.ImplInt);
305 return iface->Node.ImplInt;
309 * \brief Adds a file to the socket list
311 int IPStack_AddFile(tSocketFile *File)
313 Log_Log("IPStack", "Added file '%s'", File->Name);
314 File->Next = gIP_FileTemplates;
315 gIP_FileTemplates = File;
323 * \brief Read from an interface's directory
325 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
327 tSocketFile *file = gIP_FileTemplates;
328 while(Pos-- && file) {
332 if(!file) return NULL;
334 return strdup(file->Name);
338 * \brief Gets a named node from an interface directory
340 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
342 tSocketFile *file = gIP_FileTemplates;
344 // Get file definition
345 for(;file;file = file->Next)
347 if( strcmp(file->Name, Name) == 0 ) break;
349 if(!file) return NULL;
352 return file->Init(Node->ImplPtr);
356 * \brief Names for interface IOCtl Calls
358 static const char *casIOCtls_Iface[] = {
361 "get_address", "set_address",
368 * \brief Handles IOCtls for the IPStack interfaces
370 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
373 tInterface *iface = (tInterface*)Node->ImplPtr;
374 ENTER("pNode iID pData", Node, ID, Data);
378 // --- Standard IOCtls (0-3) ---
380 LEAVE('i', DRV_TYPE_MISC);
381 return DRV_TYPE_MISC;
383 case DRV_IOCTL_IDENT:
384 tmp = ModUtil_SetIdent(Data, STR(IDENT));
388 case DRV_IOCTL_VERSION:
392 case DRV_IOCTL_LOOKUP:
393 tmp = ModUtil_LookupString( (char**)casIOCtls_Iface, (char*)Data );
399 * - Get/Set the interface type
406 if( Threads_GetUID() != 0 ) {
407 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
411 if( !CheckMem( Data, sizeof(int) ) ) {
412 LOG("Invalid pointer %p", Data);
418 iface->Type = *(int*)Data;
419 size = IPStack_GetAddressSize(iface->Type);
420 // Check it's actually valid
421 if( iface->Type != 0 && size == 0 ) {
428 memset(iface->Address, 0, size);
430 LEAVE('i', iface->Type);
435 * - Get the interface's address
438 size = IPStack_GetAddressSize(iface->Type);
439 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
440 memcpy( Data, iface->Address, size );
446 * - Set the interface's address
449 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
451 size = IPStack_GetAddressSize(iface->Type);
452 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
453 // TODO: Protect against trashing
454 memcpy( iface->Address, Data, size );
460 * - Get/Set the bits in the address subnet
463 // Do we want to set the value?
466 // Are we root? (TODO: Check Owner/Group)
467 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
468 // Is the memory valid
469 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
472 if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 )
476 iface->SubnetBits = *(int*)Data;
478 LEAVE('i', iface->SubnetBits);
479 return iface->SubnetBits;
483 * - Gets the name of the attached device
486 if( iface->Adapter == NULL )
489 LEAVE_RET('i', iface->Adapter->DeviceLen);
490 if( !CheckMem( Data, iface->Adapter->DeviceLen+1 ) )
492 strcpy( Data, iface->Adapter->Device );
493 return iface->Adapter->DeviceLen;
497 * - Send an ICMP Echo
506 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
507 tmp = IPv4_Ping(iface, *(tIPv4*)Data);
524 * \fn tAdapter *IPStack_GetAdapter(const char *Path)
525 * \brief Gets/opens an adapter given the path
527 tAdapter *IPStack_GetAdapter(const char *Path)
532 ENTER("sPath", Path);
534 Mutex_Acquire( &glIP_Adapters );
536 // Check if this adapter is already open
537 for( dev = gIP_Adapters; dev; dev = dev->Next )
539 if( strcmp(dev->Device, Path) == 0 ) {
541 Mutex_Release( &glIP_Adapters );
547 // Ok, so let's open it
548 dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
550 Mutex_Release( &glIP_Adapters );
556 strcpy( dev->Device, Path );
558 dev->DeviceLen = strlen(Path);
561 dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
562 if( dev->DeviceFD == -1 ) {
564 Mutex_Release( &glIP_Adapters );
569 // Check that it is a network interface
570 tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
571 LOG("Device type = %i", tmp);
572 if( tmp != DRV_TYPE_NETWORK ) {
573 Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
574 VFS_Close( dev->DeviceFD );
576 Mutex_Release( &glIP_Adapters );
582 VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
585 dev->Next = gIP_Adapters;
588 Mutex_Release( &glIP_Adapters );
591 Link_WatchDevice( dev );