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);
82 return strdup("routes");
92 for( iface = gIP_Interfaces; iface && Pos--; iface = iface->Next ) ;
94 // Did we run off the end?
102 Log_Warning("IPStack", "IPStack_Root_ReadDir - malloc error");
108 Pos = iface->Node.ImplInt;
114 name[0] = '0' + Pos/10;
115 name[1] = '0' + Pos%10;
119 name[0] = '0' + Pos/100;
120 name[1] = '0' + (Pos/10)%10;
121 name[2] = '0' + Pos%10;
126 // Return the pre-generated name
131 * \brief Get the node of an interface
133 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name)
140 ENTER("pNode sName", Node, Name);
143 if( strcmp(Name, "routes") == 0 ) {
144 LEAVE('p', &gIP_RouteNode);
145 return &gIP_RouteNode;
149 if( strcmp(Name, "lo") == 0 ) {
150 LEAVE('p', &gIP_LoopInterface.Node);
151 return &gIP_LoopInterface.Node;
154 for( iface = gIP_Interfaces; iface; iface = iface->Next )
156 if( strcmp(iface->Name, Name) == 0 )
158 LEAVE('p', &iface->Node);
167 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
169 * \brief Handles IOCtls for the IPStack root
171 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data)
174 ENTER("pNode iID pData", Node, ID, Data);
178 // --- Standard IOCtls (0-3) ---
179 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Root)
183 * - Adds a new IP interface and binds it to a device
186 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
187 if( !CheckString( Data ) ) LEAVE_RET('i', -1);
188 LOG("New interface for '%s'", Data);
191 tInterface *iface = IPStack_AddInterface(Data, name);
192 if(iface == NULL) LEAVE_RET('i', -1);
193 tmp = iface->Node.ImplInt;
202 * \fn tInterface *IPStack_AddInterface(char *Device)
203 * \brief Adds an interface to the list
205 tInterface *IPStack_AddInterface(const char *Device, const char *Name)
211 ENTER("sDevice", Device);
213 card = IPStack_GetAdapter(Device);
215 Log_Debug("IPStack", "Unable to open card '%s'", Device);
217 return NULL; // ERR_YOURBAD
220 nameLen = sprintf(NULL, "%i", giIP_NextIfaceId);
225 + IPStack_GetAddressSize(-1)*3 // Address, Route->Network, Route->NextHop
228 Log_Warning("IPStack", "AddInterface - malloc() failed");
230 return NULL; // Return ERR_MYBAD
234 iface->Type = 0; // Unset type
235 iface->Address = iface->Name + nameLen + 1; // Address
236 iface->Route.Network = iface->Address + IPStack_GetAddressSize(-1);
237 iface->Route.NextHop = iface->Route.Network + IPStack_GetAddressSize(-1);
240 iface->Node.ImplPtr = iface;
241 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
242 iface->Node.Size = -1;
243 iface->Node.NumACLs = 1;
244 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
245 iface->Node.Type = &gIP_InterfaceNodeType;
248 iface->TimeoutDelay = DEFAULT_TIMEOUT;
250 // Get adapter handle
251 iface->Adapter = card;
253 // Delay setting ImplInt until after the adapter is opened
254 // Keeps things simple
255 iface->Node.ImplInt = giIP_NextIfaceId++;
256 sprintf(iface->Name, "%i", (int)iface->Node.ImplInt);
259 SHORTLOCK( &glIP_Interfaces );
260 if( gIP_Interfaces ) {
261 gIP_Interfaces_Last->Next = iface;
262 gIP_Interfaces_Last = iface;
265 gIP_Interfaces = iface;
266 gIP_Interfaces_Last = iface;
268 SHORTREL( &glIP_Interfaces );
270 // gIP_DriverInfo.RootNode.Size ++;
278 * \brief Adds a file to the socket list
280 int IPStack_AddFile(tSocketFile *File)
282 Log_Log("IPStack", "Added file '%s'", File->Name);
283 File->Next = gIP_FileTemplates;
284 gIP_FileTemplates = File;
292 * \brief Read from an interface's directory
294 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
296 tSocketFile *file = gIP_FileTemplates;
297 while(Pos-- && file) {
301 if(!file) return NULL;
303 return strdup(file->Name);
307 * \brief Gets a named node from an interface directory
309 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
311 tSocketFile *file = gIP_FileTemplates;
313 // Get file definition
314 for(;file;file = file->Next)
316 if( strcmp(file->Name, Name) == 0 ) break;
318 if(!file) return NULL;
321 return file->Init(Node->ImplPtr);
325 * \brief Names for interface IOCtl Calls
327 static const char *casIOCtls_Iface[] = {
330 "get_address", "set_address",
337 * \brief Handles IOCtls for the IPStack interfaces
339 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
342 tInterface *iface = (tInterface*)Node->ImplPtr;
343 ENTER("pNode iID pData", Node, ID, Data);
347 // --- Standard IOCtls (0-3) ---
348 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Iface)
352 * - Get/Set the interface type
359 if( Threads_GetUID() != 0 ) {
360 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
364 if( !CheckMem( Data, sizeof(int) ) ) {
365 LOG("Invalid pointer %p", Data);
371 iface->Type = *(int*)Data;
372 LOG("Interface type set to %i", iface->Type);
373 size = IPStack_GetAddressSize(iface->Type);
374 // Check it's actually valid
375 if( iface->Type != 0 && size == 0 ) {
382 memset(iface->Address, 0, size);
384 LEAVE('i', iface->Type);
389 * - Get the interface's address
392 size = IPStack_GetAddressSize(iface->Type);
393 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
394 memcpy( Data, iface->Address, size );
400 * - Set the interface's address
403 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
405 size = IPStack_GetAddressSize(iface->Type);
406 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
407 // TODO: Protect against trashing
408 LOG("Interface address set to '%s'", IPStack_PrintAddress(iface->Type, Data));
409 memcpy( iface->Address, Data, size );
414 * - Get/Set the bits in the address subnet
417 // Do we want to set the value?
420 // Are we root? (TODO: Check Owner/Group)
421 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
422 // Is the memory valid
423 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
426 if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 )
428 LOG("Set subnet bits to %i", *(int*)Data);
430 iface->SubnetBits = *(int*)Data;
432 LEAVE_RET('i', iface->SubnetBits);
436 * - Gets the name of the attached device
439 if( iface->Adapter == NULL )
442 LEAVE_RET('i', iface->Adapter->DeviceLen);
443 if( !CheckMem( Data, iface->Adapter->DeviceLen+1 ) )
445 strcpy( Data, iface->Adapter->Device );
446 LEAVE_RET('i', iface->Adapter->DeviceLen);
450 * - Send an ICMP Echo
459 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
460 tmp = IPv4_Ping(iface, *(tIPv4*)Data);
476 * \fn tAdapter *IPStack_GetAdapter(const char *Path)
477 * \brief Gets/opens an adapter given the path
479 tAdapter *IPStack_GetAdapter(const char *Path)
484 ENTER("sPath", Path);
486 // Check for loopback
487 if( strcmp(Path, "LOOPBACK") == 0 )
489 // Initialise if required
490 if( gIP_LoopAdapter.DeviceFD == 0 )
492 dev = &gIP_LoopAdapter;
497 dev->DeviceFD = VFS_Open( "/Devices/fifo/anon", VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
498 if( dev->DeviceFD == -1 ) {
499 Log_Warning("IPStack", "Unable to open FIFO '/Devices/fifo/anon' for loopback");
503 dev->MacAddr.B[0] = 'A';
504 dev->MacAddr.B[1] = 'c';
505 dev->MacAddr.B[2] = 'e';
506 dev->MacAddr.B[3] = 's';
507 dev->MacAddr.B[4] = 's';
508 dev->MacAddr.B[5] = '2';
511 Link_WatchDevice( dev );
513 LEAVE('p', &gIP_LoopAdapter);
514 return &gIP_LoopAdapter;
517 Mutex_Acquire( &glIP_Adapters );
519 // Check if this adapter is already open
520 for( dev = gIP_Adapters; dev; dev = dev->Next )
522 if( strcmp(dev->Device, Path) == 0 ) {
524 Mutex_Release( &glIP_Adapters );
530 // Ok, so let's open it
531 dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
533 Log_Warning("IPStack", "GetAdapter - malloc() failed");
534 Mutex_Release( &glIP_Adapters );
540 strcpy( dev->Device, Path );
542 dev->DeviceLen = strlen(Path);
545 dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
546 if( dev->DeviceFD == -1 ) {
548 Mutex_Release( &glIP_Adapters );
553 // Check that it is a network interface
554 tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
555 LOG("Device type = %i", tmp);
556 if( tmp != DRV_TYPE_NETWORK ) {
557 Log_Warning("IPStack", "IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
558 VFS_Close( dev->DeviceFD );
560 Mutex_Release( &glIP_Adapters );
566 VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
569 dev->Next = gIP_Adapters;
572 Mutex_Release( &glIP_Adapters );
575 Link_WatchDevice( dev );