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 //! 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;
147 for( iface = gIP_Interfaces; iface; iface = iface->Next )
149 if( strcmp(iface->Name, Name) == 0 )
151 LEAVE('p', &iface->Node);
160 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
162 * \brief Handles IOCtls for the IPStack root
164 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data)
167 ENTER("pNode iID pData", Node, ID, Data);
171 // --- Standard IOCtls (0-3) ---
172 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Root)
176 * - Adds a new IP interface and binds it to a device
179 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
180 if( !CheckString( Data ) ) LEAVE_RET('i', -1);
183 tInterface *iface = IPStack_AddInterface(Data, name);
184 if(iface == NULL) LEAVE_RET('i', -1);
185 tmp = iface->Node.ImplInt;
194 * \fn tInterface *IPStack_AddInterface(char *Device)
195 * \brief Adds an interface to the list
197 tInterface *IPStack_AddInterface(const char *Device, const char *Name)
203 ENTER("sDevice", Device);
205 card = IPStack_GetAdapter(Device);
207 Log_Debug("IPStack", "Unable to open card '%s'", Device);
209 return NULL; // ERR_YOURBAD
212 nameLen = sprintf(NULL, "%i", giIP_NextIfaceId);
217 + IPStack_GetAddressSize(-1)*3 // Address, Route->Network, Route->NextHop
220 Log_Warning("IPStack", "AddInterface - malloc() failed");
222 return NULL; // Return ERR_MYBAD
226 iface->Type = 0; // Unset type
227 iface->Address = iface->Name + nameLen + 1; // Address
228 iface->Route.Network = iface->Address + IPStack_GetAddressSize(-1);
229 iface->Route.NextHop = iface->Route.Network + IPStack_GetAddressSize(-1);
232 iface->Node.ImplPtr = iface;
233 iface->Node.Flags = VFS_FFLAG_DIRECTORY;
234 iface->Node.Size = -1;
235 iface->Node.NumACLs = 1;
236 iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
237 iface->Node.ReadDir = IPStack_Iface_ReadDir;
238 iface->Node.FindDir = IPStack_Iface_FindDir;
239 iface->Node.IOCtl = IPStack_Iface_IOCtl;
240 iface->Node.MkNod = NULL;
241 iface->Node.Link = NULL;
242 iface->Node.Relink = NULL;
243 iface->Node.Close = NULL;
246 iface->TimeoutDelay = DEFAULT_TIMEOUT;
248 // Get adapter handle
249 iface->Adapter = card;
251 // Delay setting ImplInt until after the adapter is opened
252 // Keeps things simple
253 iface->Node.ImplInt = giIP_NextIfaceId++;
254 sprintf(iface->Name, "%i", (int)iface->Node.ImplInt);
257 SHORTLOCK( &glIP_Interfaces );
258 if( gIP_Interfaces ) {
259 gIP_Interfaces_Last->Next = iface;
260 gIP_Interfaces_Last = iface;
263 gIP_Interfaces = iface;
264 gIP_Interfaces_Last = iface;
266 SHORTREL( &glIP_Interfaces );
268 // gIP_DriverInfo.RootNode.Size ++;
276 * \brief Adds a file to the socket list
278 int IPStack_AddFile(tSocketFile *File)
280 Log_Log("IPStack", "Added file '%s'", File->Name);
281 File->Next = gIP_FileTemplates;
282 gIP_FileTemplates = File;
290 * \brief Read from an interface's directory
292 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
294 tSocketFile *file = gIP_FileTemplates;
295 while(Pos-- && file) {
299 if(!file) return NULL;
301 return strdup(file->Name);
305 * \brief Gets a named node from an interface directory
307 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
309 tSocketFile *file = gIP_FileTemplates;
311 // Get file definition
312 for(;file;file = file->Next)
314 if( strcmp(file->Name, Name) == 0 ) break;
316 if(!file) return NULL;
319 return file->Init(Node->ImplPtr);
323 * \brief Names for interface IOCtl Calls
325 static const char *casIOCtls_Iface[] = {
328 "get_address", "set_address",
335 * \brief Handles IOCtls for the IPStack interfaces
337 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
340 tInterface *iface = (tInterface*)Node->ImplPtr;
341 ENTER("pNode iID pData", Node, ID, Data);
345 // --- Standard IOCtls (0-3) ---
346 BASE_IOCTLS(DRV_TYPE_MISC, "IPStack", VERSION, casIOCtls_Iface)
350 * - Get/Set the interface type
357 if( Threads_GetUID() != 0 ) {
358 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
362 if( !CheckMem( Data, sizeof(int) ) ) {
363 LOG("Invalid pointer %p", Data);
369 iface->Type = *(int*)Data;
370 size = IPStack_GetAddressSize(iface->Type);
371 // Check it's actually valid
372 if( iface->Type != 0 && size == 0 ) {
379 memset(iface->Address, 0, size);
381 LEAVE('i', iface->Type);
386 * - Get the interface's address
389 size = IPStack_GetAddressSize(iface->Type);
390 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
391 memcpy( Data, iface->Address, size );
397 * - Set the interface's address
400 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
402 size = IPStack_GetAddressSize(iface->Type);
403 if( !CheckMem( Data, size ) ) LEAVE_RET('i', -1);
404 // TODO: Protect against trashing
405 memcpy( iface->Address, Data, size );
411 * - Get/Set the bits in the address subnet
414 // Do we want to set the value?
417 // Are we root? (TODO: Check Owner/Group)
418 if( Threads_GetUID() != 0 ) LEAVE_RET('i', -1);
419 // Is the memory valid
420 if( !CheckMem(Data, sizeof(int)) ) LEAVE_RET('i', -1);
423 if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 )
427 iface->SubnetBits = *(int*)Data;
429 LEAVE('i', iface->SubnetBits);
430 return iface->SubnetBits;
434 * - Gets the name of the attached device
437 if( iface->Adapter == NULL )
440 LEAVE_RET('i', iface->Adapter->DeviceLen);
441 if( !CheckMem( Data, iface->Adapter->DeviceLen+1 ) )
443 strcpy( Data, iface->Adapter->Device );
444 return iface->Adapter->DeviceLen;
448 * - Send an ICMP Echo
457 if( !CheckMem( Data, sizeof(tIPv4) ) ) LEAVE_RET('i', -1);
458 tmp = IPv4_Ping(iface, *(tIPv4*)Data);
475 * \fn tAdapter *IPStack_GetAdapter(const char *Path)
476 * \brief Gets/opens an adapter given the path
478 tAdapter *IPStack_GetAdapter(const char *Path)
483 ENTER("sPath", Path);
485 // Check for loopback
486 if( strcmp(Path, "LOOPBACK") == 0 )
488 // Initialise if required
489 if( gIP_LoopAdapter.DeviceFD == 0 )
491 dev = &gIP_LoopAdapter;
496 dev->DeviceFD = VFS_Open( "/Devices/fifo/anon", VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
497 if( dev->DeviceFD == -1 ) {
498 Log_Warning("IPStack", "Unable to open FIFO '/Devices/fifo/anon' for loopback");
502 dev->MacAddr.B[0] = 'A';
503 dev->MacAddr.B[1] = 'c';
504 dev->MacAddr.B[2] = 'e';
505 dev->MacAddr.B[3] = 's';
506 dev->MacAddr.B[4] = 's';
507 dev->MacAddr.B[5] = '2';
510 Link_WatchDevice( dev );
512 LEAVE('p', &gIP_LoopAdapter);
513 return &gIP_LoopAdapter;
516 Mutex_Acquire( &glIP_Adapters );
518 // Check if this adapter is already open
519 for( dev = gIP_Adapters; dev; dev = dev->Next )
521 if( strcmp(dev->Device, Path) == 0 ) {
523 Mutex_Release( &glIP_Adapters );
529 // Ok, so let's open it
530 dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
532 Log_Warning("IPStack", "GetAdapter - malloc() failed");
533 Mutex_Release( &glIP_Adapters );
539 strcpy( dev->Device, Path );
541 dev->DeviceLen = strlen(Path);
544 dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
545 if( dev->DeviceFD == -1 ) {
547 Mutex_Release( &glIP_Adapters );
552 // Check that it is a network interface
553 tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
554 LOG("Device type = %i", tmp);
555 if( tmp != DRV_TYPE_NETWORK ) {
556 Log_Warning("IPStack", "IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
557 VFS_Close( dev->DeviceFD );
559 Mutex_Release( &glIP_Adapters );
565 VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
568 dev->Next = gIP_Adapters;
571 Mutex_Release( &glIP_Adapters );
574 Link_WatchDevice( dev );