IPStack / ifconfig - Routing changes
[tpg/acess2.git] / Modules / IPStack / interface.c
1 /*
2  * Acess2 IP Stack
3  * - Interface Control
4  */
5 #define DEBUG   0
6 #define VERSION VER2(0,10)
7 #include "ipstack.h"
8 #include "link.h"
9 #include <tpl_drv_common.h>
10 #include <tpl_drv_network.h>
11
12 // === CONSTANTS ===
13 //! Default timeout value, 30 seconds
14 #define DEFAULT_TIMEOUT (30*1000)
15
16 // === IMPORTS ===
17 extern int      IPv4_Ping(tInterface *Iface, tIPv4 Addr);
18 //extern int    IPv6_Ping(tInterface *Iface, tIPv6 Addr);
19 extern tVFS_Node        gIP_RouteNode;
20
21 // === PROTOTYPES ===
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);
25
26  int    IPStack_AddFile(tSocketFile *File);
27 tInterface      *IPStack_AddInterface(const char *Device, const char *Name);
28 tAdapter        *IPStack_GetAdapter(const char *Path);
29
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);
33
34 // === GLOBALS ===
35 //! Loopback (127.0.0.0/8, ::1) Pseudo-Interface
36 tInterface      gIP_LoopInterface = {
37         Node: {
38                 ImplPtr: &gIP_LoopInterface,
39                 Flags: VFS_FFLAG_DIRECTORY,
40                 Size: -1,
41                 NumACLs: 1,
42                 ACLs: &gVFS_ACL_EveryoneRX,
43                 ReadDir: IPStack_Iface_ReadDir,
44                 FindDir: IPStack_Iface_FindDir,
45                 IOCtl: IPStack_Iface_IOCtl
46         },
47         Adapter: NULL,
48         Type: 0
49 };
50 tShortSpinlock  glIP_Interfaces;
51 tInterface      *gIP_Interfaces = NULL;
52 tInterface      *gIP_Interfaces_Last = NULL;
53
54 tSocketFile     *gIP_FileTemplates;
55
56 tAdapter        gIP_LoopAdapter = {
57         DeviceLen: 8,
58         Device: "LOOPBACK"
59         };
60 tMutex  glIP_Adapters;
61 tAdapter        *gIP_Adapters = NULL;
62  int    giIP_NextIfaceId = 1;
63
64 // === CODE ===
65
66 /**
67  * \brief Read from the IP Stack's Device Directory
68  */
69 char *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos)
70 {
71         tInterface      *iface;
72         char    *name;
73         ENTER("pNode iPos", Node, Pos);
74         
75
76         // Routing Subdir
77         if( Pos == 0 ) {
78                 return strdup("routes");
79         }
80         // Pseudo Interfaces
81         if( Pos == 1 ) {
82                 return strdup("lo");
83         }
84         Pos -= 2;
85         
86         // Traverse the list
87         for( iface = gIP_Interfaces; iface && Pos--; iface = iface->Next ) ;
88         
89         // Did we run off the end?
90         if(!iface) {
91                 LEAVE('n');
92                 return NULL;
93         }
94         
95         name = malloc(4);
96         if(!name) {
97                 Log_Warning("IPStack", "IPStack_Root_ReadDir - malloc error");
98                 LEAVE('n');
99                 return NULL;
100         }
101         
102         // Create the name
103         Pos = iface->Node.ImplInt;
104         if(Pos < 10) {
105                 name[0] = '0' + Pos;
106                 name[1] = '\0';
107         }
108         else if(Pos < 100) {
109                 name[0] = '0' + Pos/10;
110                 name[1] = '0' + Pos%10;
111                 name[2] = '\0';
112         }
113         else {
114                 name[0] = '0' + Pos/100;
115                 name[1] = '0' + (Pos/10)%10;
116                 name[2] = '0' + Pos%10;
117                 name[3] = '\0';
118         }
119         
120         LEAVE('s', name);
121         // Return the pre-generated name
122         return name;
123 }
124
125 /**
126  * \brief Get the node of an interface
127  */
128 tVFS_Node *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name)
129 {
130         #if 0
131          int    i, num;
132         #endif
133         tInterface      *iface;
134         
135         ENTER("pNode sName", Node, Name);
136         
137         // Routing subdir
138         if( strcmp(Name, "routes") == 0 ) {
139                 return &gIP_RouteNode;
140         }
141         
142         // Loopback
143         if( strcmp(Name, "lo") == 0 ) {
144                 return &gIP_LoopInterface.Node;
145         }
146         
147         #if 0
148         i = 0;  num = 0;
149         while('0' <= Name[i] && Name[i] <= '9')
150         {
151                 num *= 10;
152                 num += Name[i] - '0';
153                 i ++;
154         }
155         if(Name[i] != '\0') {
156                 LEAVE('n');
157                 return NULL;
158         }
159         
160         for( iface = gIP_Interfaces; iface; iface = iface->Next )
161         {
162                 if( (int)iface->Node.ImplInt == num )
163                 {
164                         LEAVE('p', &iface->Node);
165                         return &iface->Node;
166                 }
167         }
168         #else
169         for( iface = gIP_Interfaces; iface; iface = iface->Next )
170         {
171                 if( strcmp(iface->Name, Name) == 0 )
172                 {
173                         LEAVE('p', &iface->Node);
174                         return &iface->Node;
175                 }
176         }
177         #endif
178         
179         LEAVE('p', NULL);
180         return NULL;
181 }
182
183 static const char *casIOCtls_Root[] = { DRV_IOCTLNAMES, "add_interface", NULL };
184 /**
185  * \brief Handles IOCtls for the IPStack root
186  */
187 int IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data)
188 {
189          int    tmp;
190         ENTER("pNode iID pData", Node, ID, Data);
191         
192         switch(ID)
193         {
194         // --- Standard IOCtls (0-3) ---
195         case DRV_IOCTL_TYPE:
196                 LEAVE('i', DRV_TYPE_MISC);
197                 return DRV_TYPE_MISC;
198         
199         case DRV_IOCTL_IDENT:
200                 tmp = ModUtil_SetIdent(Data, "IPStack");
201                 LEAVE('i', 1);
202                 return 1;
203         
204         case DRV_IOCTL_VERSION:
205                 LEAVE('x', VERSION);
206                 return VERSION;
207         
208         case DRV_IOCTL_LOOKUP:
209                 tmp = ModUtil_LookupString( (char**)casIOCtls_Root, (char*)Data );
210                 LEAVE('i', tmp);
211                 return tmp;
212                 
213                 /*
214                  * add_interface
215                  * - Adds a new IP interface and binds it to a device
216                  */
217         case 4:
218                 if( Threads_GetUID() != 0 )     LEAVE_RET('i', -1);
219                 if( !CheckString( Data ) )      LEAVE_RET('i', -1);
220                 {
221                         char    name[4] = "";
222                         tInterface      *iface = IPStack_AddInterface(Data, name);
223                         tmp = iface->Node.ImplInt;
224                 }
225                 LEAVE_RET('i', tmp);
226         }
227         LEAVE('i', 0);
228         return 0;
229 }
230
231 /**
232  * \fn tInterface *IPStack_AddInterface(char *Device)
233  * \brief Adds an interface to the list
234  */
235 tInterface *IPStack_AddInterface(const char *Device, const char *Name)
236 {
237         tInterface      *iface;
238         tAdapter        *card;
239          int    nameLen;
240         
241         ENTER("sDevice", Device);
242         
243         card = IPStack_GetAdapter(Device);
244         if( !card ) {
245                 LEAVE('n');
246                 return NULL;    // ERR_YOURBAD
247         }
248         
249         nameLen = sprintf(NULL, "%i", giIP_NextIfaceId);
250         
251         iface = malloc(
252                 sizeof(tInterface)
253                 + nameLen + 1
254                 + IPStack_GetAddressSize(-1)*3  // Address, Route->Network, Route->NextHop
255                 );
256         if(!iface) {
257                 LEAVE('n');
258                 return NULL;    // Return ERR_MYBAD
259         }
260         
261         iface->Next = NULL;
262         iface->Type = 0;        // Unset type
263         iface->Address = iface->Name + nameLen + 1;     // Address
264         iface->Route.Network = iface->Address + IPStack_GetAddressSize(-1);
265         iface->Route.NextHop = iface->Route.Network + IPStack_GetAddressSize(-1);
266         
267         // Create Node
268         iface->Node.ImplPtr = iface;
269         iface->Node.Flags = VFS_FFLAG_DIRECTORY;
270         iface->Node.Size = -1;
271         iface->Node.NumACLs = 1;
272         iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
273         iface->Node.ReadDir = IPStack_Iface_ReadDir;
274         iface->Node.FindDir = IPStack_Iface_FindDir;
275         iface->Node.IOCtl = IPStack_Iface_IOCtl;
276         iface->Node.MkNod = NULL;
277         iface->Node.Link = NULL;
278         iface->Node.Relink = NULL;
279         iface->Node.Close = NULL;
280         
281         // Set Defaults
282         iface->TimeoutDelay = DEFAULT_TIMEOUT;
283         
284         // Get adapter handle
285         iface->Adapter = IPStack_GetAdapter(Device);
286         if( !iface->Adapter ) {
287                 free( iface );
288                 LEAVE('n');
289                 return NULL;    // Return ERR_YOUFAIL
290         }
291         
292         // Delay setting ImplInt until after the adapter is opened
293         // Keeps things simple
294         iface->Node.ImplInt = giIP_NextIfaceId++;
295         sprintf(iface->Name, "%i", iface->Node.ImplInt);
296         
297         // Append to list
298         SHORTLOCK( &glIP_Interfaces );
299         if( gIP_Interfaces ) {
300                 gIP_Interfaces_Last->Next = iface;
301                 gIP_Interfaces_Last = iface;
302         }
303         else {
304                 gIP_Interfaces = iface;
305                 gIP_Interfaces_Last = iface;
306         }
307         SHORTREL( &glIP_Interfaces );
308
309 //      gIP_DriverInfo.RootNode.Size ++;
310         
311         // Success!
312         LEAVE('p', iface);
313         return iface;
314 }
315
316 /**
317  * \brief Adds a file to the socket list
318  */
319 int IPStack_AddFile(tSocketFile *File)
320 {
321         Log_Log("IPStack", "Added file '%s'", File->Name);
322         File->Next = gIP_FileTemplates;
323         gIP_FileTemplates = File;
324         return 0;
325 }
326
327 // ---
328 // VFS Functions
329 // ---
330 /**
331  * \brief Read from an interface's directory
332  */
333 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
334 {
335         tSocketFile     *file = gIP_FileTemplates;
336         while(Pos-- && file) {
337                 file = file->Next;
338         }
339         
340         if(!file)       return NULL;
341         
342         return strdup(file->Name);
343 }
344
345 /**
346  * \brief Gets a named node from an interface directory
347  */
348 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
349 {
350         tSocketFile     *file = gIP_FileTemplates;
351         
352         // Get file definition
353         for(;file;file = file->Next)
354         {
355                 if( strcmp(file->Name, Name) == 0 )     break;
356         }
357         if(!file)       return NULL;
358         
359         // Pass the buck!
360         return file->Init(Node->ImplPtr);
361 }
362
363 /**
364  * \brief Names for interface IOCtl Calls
365  */
366 static const char *casIOCtls_Iface[] = {
367         DRV_IOCTLNAMES,
368         "getset_type",
369         "get_address", "set_address",
370         "getset_subnet",
371         "get_device",
372         "ping",
373         NULL
374         };
375 /**
376  * \brief Handles IOCtls for the IPStack interfaces
377  */
378 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
379 {
380          int    tmp, size;
381         tInterface      *iface = (tInterface*)Node->ImplPtr;
382         ENTER("pNode iID pData", Node, ID, Data);
383         
384         switch(ID)
385         {
386         // --- Standard IOCtls (0-3) ---
387         case DRV_IOCTL_TYPE:
388                 LEAVE('i', DRV_TYPE_MISC);
389                 return DRV_TYPE_MISC;
390         
391         case DRV_IOCTL_IDENT:
392                 tmp = ModUtil_SetIdent(Data, STR(IDENT));
393                 LEAVE('i', 1);
394                 return 1;
395         
396         case DRV_IOCTL_VERSION:
397                 LEAVE('x', VERSION);
398                 return VERSION;
399         
400         case DRV_IOCTL_LOOKUP:
401                 tmp = ModUtil_LookupString( (char**)casIOCtls_Iface, (char*)Data );
402                 LEAVE('i', tmp);
403                 return tmp;
404         
405         /*
406          * getset_type
407          * - Get/Set the interface type
408          */
409         case 4:
410                 // Set Type?
411                 if( Data )
412                 {
413                         // Ok, it's set type
414                         if( Threads_GetUID() != 0 ) {
415                                 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
416                                 LEAVE('i', -1);
417                                 return -1;
418                         }
419                         if( !CheckMem( Data, sizeof(int) ) ) {
420                                 LOG("Invalid pointer %p", Data);
421                                 LEAVE('i', -1);
422                                 return -1;
423                         }
424                         
425                         // Set type
426                         iface->Type = *(int*)Data;
427                         size = IPStack_GetAddressSize(iface->Type);
428                         // Check it's actually valid
429                         if( iface->Type != 0 && size == 0 ) {
430                                 iface->Type = 0;
431                                 LEAVE('i', -1);
432                                 return -1;
433                         }
434                         
435                         // Clear address
436                         memset(iface->Address, 0, size);
437                 }
438                 LEAVE('i', iface->Type);
439                 return iface->Type;
440         
441         /*
442          * get_address
443          * - Get the interface's address
444          */
445         case 5:
446                 size = IPStack_GetAddressSize(iface->Type);
447                 if( !CheckMem( Data, size ) )   LEAVE_RET('i', -1);
448                 memcpy( Data, iface->Address, size );
449                 LEAVE('i', 1);
450                 return 1;
451         
452         /*
453          * set_address
454          * - Set the interface's address
455          */
456         case 6:
457                 if( Threads_GetUID() != 0 )     LEAVE_RET('i', -1);
458                 
459                 size = IPStack_GetAddressSize(iface->Type);
460                 if( !CheckMem( Data, size ) )   LEAVE_RET('i', -1);
461                 // TODO: Protect against trashing
462                 memcpy( iface->Address, Data, size );
463                 LEAVE('i', 1);
464                 return 1;
465         
466         /*
467          * getset_subnet
468          * - Get/Set the bits in the address subnet
469          */
470         case 7:
471                 // Do we want to set the value?
472                 if( Data )
473                 {
474                         // Are we root? (TODO: Check Owner/Group)
475                         if( Threads_GetUID() != 0 )     LEAVE_RET('i', -1);
476                         // Is the memory valid
477                         if( !CheckMem(Data, sizeof(int)) )      LEAVE_RET('i', -1);
478                         
479                         // Is the mask sane?
480                         if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 )
481                                 LEAVE_RET('i', -1);
482                         
483                         // Ok, set it
484                         iface->SubnetBits = *(int*)Data;
485                 }
486                 LEAVE('i', iface->SubnetBits);
487                 return iface->SubnetBits;
488         
489         /*
490          * get_device
491          * - Gets the name of the attached device
492          */
493         case 8:
494                 if( iface->Adapter == NULL )
495                         LEAVE_RET('i', 0);
496                 if( Data == NULL )
497                         LEAVE_RET('i', iface->Adapter->DeviceLen);
498                 if( !CheckMem( Data, iface->Adapter->DeviceLen+1 ) )
499                         LEAVE_RET('i', -1);
500                 strcpy( Data, iface->Adapter->Device );
501                 return iface->Adapter->DeviceLen;
502         
503         /*
504          * ping
505          * - Send an ICMP Echo
506          */
507         case 9:
508                 switch(iface->Type)
509                 {
510                 case 0:
511                         LEAVE_RET('i', 1);
512                 
513                 case 4:
514                         if( !CheckMem( Data, sizeof(tIPv4) ) )  LEAVE_RET('i', -1);
515                         tmp = IPv4_Ping(iface, *(tIPv4*)Data);
516                         LEAVE('i', tmp);
517                         return tmp;
518                         
519                 case 6:
520                         LEAVE_RET('i', 1);
521                 }
522                 break;
523         
524         }
525         
526         LEAVE('i', 0);
527         return 0;
528 }
529
530 // --- Internal ---
531 /**
532  * \fn tAdapter *IPStack_GetAdapter(const char *Path)
533  * \brief Gets/opens an adapter given the path
534  */
535 tAdapter *IPStack_GetAdapter(const char *Path)
536 {
537         tAdapter        *dev;
538          int    tmp;
539         
540         ENTER("sPath", Path);
541         
542         // Check for loopback
543         if( strcmp(Path, "LOOPBACK") == 0 )
544         {
545                 // Initialise if required
546                 if( gIP_LoopAdapter.DeviceFD == 0 )
547                 {
548                         dev = &gIP_LoopAdapter;
549                         
550                         dev->NRef = 1;
551                         dev->DeviceLen = 8;
552                         
553                         dev->DeviceFD = VFS_Open( "/Devices/fifo/anon", VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
554                         if( dev->DeviceFD == -1 ) {
555                                 Log_Warning("IPStack", "Unable to open FIFO '/Devices/fifo/anon' for loopback");
556                                 return NULL;
557                         }
558                         
559                         dev->MacAddr.B[0] = 'A';
560                         dev->MacAddr.B[1] = 'c';
561                         dev->MacAddr.B[2] = 'e';
562                         dev->MacAddr.B[3] = 's';
563                         dev->MacAddr.B[4] = 's';
564                         dev->MacAddr.B[5] = '2';
565                         
566                         // Start watcher
567                         Link_WatchDevice( dev );
568                 }
569                 LEAVE('p', &gIP_LoopAdapter);
570                 return &gIP_LoopAdapter;
571         }
572         
573         Mutex_Acquire( &glIP_Adapters );
574         
575         // Check if this adapter is already open
576         for( dev = gIP_Adapters; dev; dev = dev->Next )
577         {
578                 if( strcmp(dev->Device, Path) == 0 ) {
579                         dev->NRef ++;
580                         Mutex_Release( &glIP_Adapters );
581                         LEAVE('p', dev);
582                         return dev;
583                 }
584         }
585         
586         // Ok, so let's open it
587         dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
588         if(!dev) {
589                 Mutex_Release( &glIP_Adapters );
590                 LEAVE('n');
591                 return NULL;
592         }
593         
594         // Fill Structure
595         strcpy( dev->Device, Path );
596         dev->NRef = 1;
597         dev->DeviceLen = strlen(Path);
598         
599         // Open Device
600         dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
601         if( dev->DeviceFD == -1 ) {
602                 free( dev );
603                 Mutex_Release( &glIP_Adapters );
604                 LEAVE('n');
605                 return NULL;
606         }
607         
608         // Check that it is a network interface
609         tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
610         LOG("Device type = %i", tmp);
611         if( tmp != DRV_TYPE_NETWORK ) {
612                 Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
613                 VFS_Close( dev->DeviceFD );
614                 free( dev );
615                 Mutex_Release( &glIP_Adapters );
616                 LEAVE('n');
617                 return NULL;
618         }
619         
620         // Get MAC Address
621         VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
622         
623         // Add to list
624         dev->Next = gIP_Adapters;
625         gIP_Adapters = dev;
626         
627         Mutex_Release( &glIP_Adapters );
628         
629         // Start watcher
630         Link_WatchDevice( dev );
631         
632         LEAVE('p', dev);
633         return dev;
634 }

UCC git Repository :: git.ucc.asn.au