6 #define VERSION VER2(0,10)
9 #include <api_drv_common.h>
10 #include <api_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 tVFS_NodeType gIP_InterfaceNodeType = {
36 .ReadDir = IPStack_Iface_ReadDir,
37 .FindDir = IPStack_Iface_FindDir,
38 .IOCtl = IPStack_Iface_IOCtl
40 //! Loopback (127.0.0.0/8, ::1) Pseudo-Interface
41 tInterface gIP_LoopInterface = {
43 .ImplPtr = &gIP_LoopInterface,
44 .Flags = VFS_FFLAG_DIRECTORY,
47 .ACLs = &gVFS_ACL_EveryoneRX,
48 .Type = &gIP_InterfaceNodeType
53 tShortSpinlock glIP_Interfaces;
54 tInterface *gIP_Interfaces = NULL;
55 tInterface *gIP_Interfaces_Last = NULL;
57 tSocketFile *gIP_FileTemplates;
59 tAdapter gIP_LoopAdapter = {
64 tAdapter *gIP_Adapters = NULL;
65 int giIP_NextIfaceId = 1;
70 * \brief Read from the IP Stack's Device Directory
72 char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos)
76 ENTER("pNode iPos", Node, Pos);
81 return strdup("routes");
90 for( iface = gIP_Interfaces; iface && Pos--; iface = iface->Next ) ;
92 // Did we run off the end?
100 Log_Warning("IPStack", "IPStack_Root_ReadDir - malloc error");
106 Pos = iface->Node.ImplInt;
112 name[0] = '0' + Pos/10;
113 name[1] = '0' + Pos%10;
117 name[0] = '0' + Pos/100;
118 name[1] = '0' + (Pos/10)%10;
119 name[2] = '0' + Pos%10;
124 // Return the pre-generated name
129 * \brief Get the node of an interface
131 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name)
138 ENTER("pNode sName", Node, Name);
141 if( strcmp(Name, "routes") == 0 ) {
142 return &gIP_RouteNode;
146 if( strcmp(Name, "lo") == 0 ) {
147 return &gIP_LoopInterface.Node;
150 for( iface = gIP_Interfaces; iface; iface = iface->Next )
152 if( strcmp(iface->Name, Name) == 0 )
154 LEAVE('p', &iface->Node);
163 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
165 * \brief Handles IOCtls for the IPStack root
167 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data)
170 ENTER("pNode iID pData", Node, ID, Data);
174 // --- Standard IOCtls (0-3) ---
175 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Root)
179 * - Adds a new IP interface and binds it to a device
182 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
183 if( !CheckString( Data ) ) LEAVE_RET('i', -1);
186 tInterface *iface = IPStack_AddInterface(Data, name);
187 if(iface == NULL) LEAVE_RET('i', -1);
188 tmp = iface->Node.ImplInt;
197 * \fn tInterface *IPStack_AddInterface(char *Device)
198 * \brief Adds an interface to the list
200 tInterface *IPStack_AddInterface(const char *Device, const char *Name)
206 ENTER("sDevice", Device);
208 card = IPStack_GetAdapter(Device);
210 Log_Debug("IPStack", "Unable to open card '%s'", Device);
212 return NULL; // ERR_YOURBAD
215 nameLen = sprintf(NULL, "%i", giIP_NextIfaceId);
220 + IPStack_GetAddressSize(-1)*3 // Address, Route->Network, Route->NextHop
223 Log_Warning("IPStack", "AddInterface - malloc() failed");
225 return NULL; // Return ERR_MYBAD
229 iface->Type = 0; // Unset type
230 iface->Address = iface->Name + nameLen + 1; // Address
231 iface->Route.Network = iface->Address + IPStack_GetAddressSize(-1);
232 iface->Route.NextHop = iface->Route.Network + IPStack_GetAddressSize(-1);
235 iface->Node.ImplPtr = iface;
236 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
237 iface->Node.Size = -1;
238 iface->Node.NumACLs = 1;
239 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
240 iface->Node.Type = &gIP_InterfaceNodeType;
243 iface->TimeoutDelay = DEFAULT_TIMEOUT;
245 // Get adapter handle
246 iface->Adapter = card;
248 // Delay setting ImplInt until after the adapter is opened
249 // Keeps things simple
250 iface->Node.ImplInt = giIP_NextIfaceId++;
251 sprintf(iface->Name, "%i", (int)iface->Node.ImplInt);
254 SHORTLOCK( &glIP_Interfaces );
255 if( gIP_Interfaces ) {
256 gIP_Interfaces_Last->Next = iface;
257 gIP_Interfaces_Last = iface;
260 gIP_Interfaces = iface;
261 gIP_Interfaces_Last = iface;
263 SHORTREL( &glIP_Interfaces );
265 // gIP_DriverInfo.RootNode.Size ++;
273 * \brief Adds a file to the socket list
275 int IPStack_AddFile(tSocketFile *File)
277 Log_Log("IPStack", "Added file '%s'", File->Name);
278 File->Next = gIP_FileTemplates;
279 gIP_FileTemplates = File;
287 * \brief Read from an interface's directory
289 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
291 tSocketFile *file = gIP_FileTemplates;
292 while(Pos-- && file) {
296 if(!file) return NULL;
298 return strdup(file->Name);
302 * \brief Gets a named node from an interface directory
304 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
306 tSocketFile *file = gIP_FileTemplates;
308 // Get file definition
309 for(;file;file = file->Next)
311 if( strcmp(file->Name, Name) == 0 ) break;
313 if(!file) return NULL;
316 return file->Init(Node->ImplPtr);
320 * \brief Names for interface IOCtl Calls
322 static const char *casIOCtls_Iface[] = {
325 "get_address", "set_address",
332 * \brief Handles IOCtls for the IPStack interfaces
334 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
337 tInterface *iface = (tInterface*)Node->ImplPtr;
338 ENTER("pNode iID pData", Node, ID, Data);
342 // --- Standard IOCtls (0-3) ---
343 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Iface)
347 * - Get/Set the interface type
354 if( Threads_GetUID() != 0 ) {
355 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
359 if( !CheckMem( Data, sizeof(int) ) ) {
360 LOG("Invalid pointer %p", Data);
366 iface->Type = *(int*)Data;
367 size = IPStack_GetAddressSize(iface->Type);
368 // Check it's actually valid
369 if( iface->Type != 0 && size == 0 ) {
376 memset(iface->Address, 0, size);
378 LEAVE('i', iface->Type);
383 * - Get the interface's address
386 size = IPStack_GetAddressSize(iface->Type);
387 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
388 memcpy( Data, iface->Address, size );
394 * - Set the interface's address
397 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
399 size = IPStack_GetAddressSize(iface->Type);
400 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
401 // TODO: Protect against trashing
402 memcpy( iface->Address, Data, size );
408 * - Get/Set the bits in the address subnet
411 // Do we want to set the value?
414 // Are we root? (TODO: Check Owner/Group)
415 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
416 // Is the memory valid
417 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
420 if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 )
424 iface->SubnetBits = *(int*)Data;
426 LEAVE('i', iface->SubnetBits);
427 return iface->SubnetBits;
431 * - Gets the name of the attached device
434 if( iface->Adapter == NULL )
437 LEAVE_RET('i', iface->Adapter->DeviceLen);
438 if( !CheckMem( Data, iface->Adapter->DeviceLen+1 ) )
440 strcpy( Data, iface->Adapter->Device );
441 return iface->Adapter->DeviceLen;
445 * - Send an ICMP Echo
454 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
455 tmp = IPv4_Ping(iface, *(tIPv4*)Data);
472 * \fn tAdapter *IPStack_GetAdapter(const char *Path)
473 * \brief Gets/opens an adapter given the path
475 tAdapter *IPStack_GetAdapter(const char *Path)
480 ENTER("sPath", Path);
482 // Check for loopback
483 if( strcmp(Path, "LOOPBACK") == 0 )
485 // Initialise if required
486 if( gIP_LoopAdapter.DeviceFD == 0 )
488 dev = &gIP_LoopAdapter;
493 dev->DeviceFD = VFS_Open( "/Devices/fifo/anon", VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
494 if( dev->DeviceFD == -1 ) {
495 Log_Warning("IPStack", "Unable to open FIFO '/Devices/fifo/anon' for loopback");
499 dev->MacAddr.B[0] = 'A';
500 dev->MacAddr.B[1] = 'c';
501 dev->MacAddr.B[2] = 'e';
502 dev->MacAddr.B[3] = 's';
503 dev->MacAddr.B[4] = 's';
504 dev->MacAddr.B[5] = '2';
507 Link_WatchDevice( dev );
509 LEAVE('p', &gIP_LoopAdapter);
510 return &gIP_LoopAdapter;
513 Mutex_Acquire( &glIP_Adapters );
515 // Check if this adapter is already open
516 for( dev = gIP_Adapters; dev; dev = dev->Next )
518 if( strcmp(dev->Device, Path) == 0 ) {
520 Mutex_Release( &glIP_Adapters );
526 // Ok, so let's open it
527 dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
529 Log_Warning("IPStack", "GetAdapter - malloc() failed");
530 Mutex_Release( &glIP_Adapters );
536 strcpy( dev->Device, Path );
538 dev->DeviceLen = strlen(Path);
541 dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
542 if( dev->DeviceFD == -1 ) {
544 Mutex_Release( &glIP_Adapters );
549 // Check that it is a network interface
550 tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
551 LOG("Device type = %i", tmp);
552 if( tmp != DRV_TYPE_NETWORK ) {
553 Log_Warning("IPStack", "IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
554 VFS_Close( dev->DeviceFD );
556 Mutex_Release( &glIP_Adapters );
562 VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
565 dev->Next = gIP_Adapters;
568 Mutex_Release( &glIP_Adapters );
571 Link_WatchDevice( dev );