Multiple IPStack Related changes (and other bugfixes)
[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_AddInterface(const char *Device, const char *Name);
27  int    IPStack_AddFile(tSocketFile *File);
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                         tmp = IPStack_AddInterface(Data, name);
223                 }
224                 LEAVE_RET('i', tmp);
225         }
226         LEAVE('i', 0);
227         return 0;
228 }
229
230 /**
231  * \fn int IPStack_AddInterface(char *Device)
232  * \brief Adds an interface to the list
233  */
234 int IPStack_AddInterface(const char *Device, const char *Name)
235 {
236         tInterface      *iface;
237         tAdapter        *card;
238          int    nameLen;
239         
240         ENTER("sDevice", Device);
241         
242         card = IPStack_GetAdapter(Device);
243         if( !card ) {
244                 LEAVE('i', -1);
245                 return -1;      // ERR_YOURBAD
246         }
247         
248         nameLen = sprintf(NULL, "%i", giIP_NextIfaceId);
249         
250         iface = malloc(
251                 sizeof(tInterface)
252                 + nameLen + 1
253                 + IPStack_GetAddressSize(-1)
254                 );
255         if(!iface) {
256                 LEAVE('i', -2);
257                 return -2;      // Return ERR_MYBAD
258         }
259         
260         iface->Next = NULL;
261         iface->Type = 0;        // Unset type
262         iface->Address = iface->Name + nameLen + 1;     // Address
263         
264         // Create Node
265         iface->Node.ImplPtr = iface;
266         iface->Node.Flags = VFS_FFLAG_DIRECTORY;
267         iface->Node.Size = -1;
268         iface->Node.NumACLs = 1;
269         iface->Node.ACLs = &gVFS_ACL_EveryoneRX;
270         iface->Node.ReadDir = IPStack_Iface_ReadDir;
271         iface->Node.FindDir = IPStack_Iface_FindDir;
272         iface->Node.IOCtl = IPStack_Iface_IOCtl;
273         iface->Node.MkNod = NULL;
274         iface->Node.Link = NULL;
275         iface->Node.Relink = NULL;
276         iface->Node.Close = NULL;
277         
278         // Set Defaults
279         iface->TimeoutDelay = DEFAULT_TIMEOUT;
280         
281         // Get adapter handle
282         iface->Adapter = IPStack_GetAdapter(Device);
283         if( !iface->Adapter ) {
284                 free( iface );
285                 LEAVE('i', -1);
286                 return -1;      // Return ERR_YOUFAIL
287         }
288         
289         // Delay setting ImplInt until after the adapter is opened
290         // Keeps things simple
291         iface->Node.ImplInt = giIP_NextIfaceId++;
292         sprintf(iface->Name, "%i", iface->Node.ImplInt);
293         
294         // Append to list
295         SHORTLOCK( &glIP_Interfaces );
296         if( gIP_Interfaces ) {
297                 gIP_Interfaces_Last->Next = iface;
298                 gIP_Interfaces_Last = iface;
299         }
300         else {
301                 gIP_Interfaces = iface;
302                 gIP_Interfaces_Last = iface;
303         }
304         SHORTREL( &glIP_Interfaces );
305
306 //      gIP_DriverInfo.RootNode.Size ++;
307         
308         // Success!
309         LEAVE('i', iface->Node.ImplInt);
310         return iface->Node.ImplInt;
311 }
312
313 /**
314  * \brief Adds a file to the socket list
315  */
316 int IPStack_AddFile(tSocketFile *File)
317 {
318         Log_Log("IPStack", "Added file '%s'", File->Name);
319         File->Next = gIP_FileTemplates;
320         gIP_FileTemplates = File;
321         return 0;
322 }
323
324 // ---
325 // VFS Functions
326 // ---
327 /**
328  * \brief Read from an interface's directory
329  */
330 char *IPStack_Iface_ReadDir(tVFS_Node *Node, int Pos)
331 {
332         tSocketFile     *file = gIP_FileTemplates;
333         while(Pos-- && file) {
334                 file = file->Next;
335         }
336         
337         if(!file)       return NULL;
338         
339         return strdup(file->Name);
340 }
341
342 /**
343  * \brief Gets a named node from an interface directory
344  */
345 tVFS_Node *IPStack_Iface_FindDir(tVFS_Node *Node, const char *Name)
346 {
347         tSocketFile     *file = gIP_FileTemplates;
348         
349         // Get file definition
350         for(;file;file = file->Next)
351         {
352                 if( strcmp(file->Name, Name) == 0 )     break;
353         }
354         if(!file)       return NULL;
355         
356         // Pass the buck!
357         return file->Init(Node->ImplPtr);
358 }
359
360 /**
361  * \brief Names for interface IOCtl Calls
362  */
363 static const char *casIOCtls_Iface[] = {
364         DRV_IOCTLNAMES,
365         "getset_type",
366         "get_address", "set_address",
367         "getset_subnet",
368         "get_device",
369         "ping",
370         NULL
371         };
372 /**
373  * \brief Handles IOCtls for the IPStack interfaces
374  */
375 int IPStack_Iface_IOCtl(tVFS_Node *Node, int ID, void *Data)
376 {
377          int    tmp, size;
378         tInterface      *iface = (tInterface*)Node->ImplPtr;
379         ENTER("pNode iID pData", Node, ID, Data);
380         
381         switch(ID)
382         {
383         // --- Standard IOCtls (0-3) ---
384         case DRV_IOCTL_TYPE:
385                 LEAVE('i', DRV_TYPE_MISC);
386                 return DRV_TYPE_MISC;
387         
388         case DRV_IOCTL_IDENT:
389                 tmp = ModUtil_SetIdent(Data, STR(IDENT));
390                 LEAVE('i', 1);
391                 return 1;
392         
393         case DRV_IOCTL_VERSION:
394                 LEAVE('x', VERSION);
395                 return VERSION;
396         
397         case DRV_IOCTL_LOOKUP:
398                 tmp = ModUtil_LookupString( (char**)casIOCtls_Iface, (char*)Data );
399                 LEAVE('i', tmp);
400                 return tmp;
401         
402         /*
403          * getset_type
404          * - Get/Set the interface type
405          */
406         case 4:
407                 // Set Type?
408                 if( Data )
409                 {
410                         // Ok, it's set type
411                         if( Threads_GetUID() != 0 ) {
412                                 LOG("Attempt by non-root to alter an interface (%i)", Threads_GetUID());
413                                 LEAVE('i', -1);
414                                 return -1;
415                         }
416                         if( !CheckMem( Data, sizeof(int) ) ) {
417                                 LOG("Invalid pointer %p", Data);
418                                 LEAVE('i', -1);
419                                 return -1;
420                         }
421                         
422                         // Set type
423                         iface->Type = *(int*)Data;
424                         size = IPStack_GetAddressSize(iface->Type);
425                         // Check it's actually valid
426                         if( iface->Type != 0 && size == 0 ) {
427                                 iface->Type = 0;
428                                 LEAVE('i', -1);
429                                 return -1;
430                         }
431                         
432                         // Clear address
433                         memset(iface->Address, 0, size);
434                 }
435                 LEAVE('i', iface->Type);
436                 return iface->Type;
437         
438         /*
439          * get_address
440          * - Get the interface's address
441          */
442         case 5:
443                 size = IPStack_GetAddressSize(iface->Type);
444                 if( !CheckMem( Data, size ) )   LEAVE_RET('i', -1);
445                 memcpy( Data, iface->Address, size );
446                 LEAVE('i', 1);
447                 return 1;
448         
449         /*
450          * set_address
451          * - Set the interface's address
452          */
453         case 6:
454                 if( Threads_GetUID() != 0 )     LEAVE_RET('i', -1);
455                 
456                 size = IPStack_GetAddressSize(iface->Type);
457                 if( !CheckMem( Data, size ) )   LEAVE_RET('i', -1);
458                 // TODO: Protect against trashing
459                 memcpy( iface->Address, Data, size );
460                 LEAVE('i', 1);
461                 return 1;
462         
463         /*
464          * getset_subnet
465          * - Get/Set the bits in the address subnet
466          */
467         case 7:
468                 // Do we want to set the value?
469                 if( Data )
470                 {
471                         // Are we root? (TODO: Check Owner/Group)
472                         if( Threads_GetUID() != 0 )     LEAVE_RET('i', -1);
473                         // Is the memory valid
474                         if( !CheckMem(Data, sizeof(int)) )      LEAVE_RET('i', -1);
475                         
476                         // Is the mask sane?
477                         if( *(int*)Data < 0 || *(int*)Data > IPStack_GetAddressSize(iface->Type)*8-1 )
478                                 LEAVE_RET('i', -1);
479                         
480                         // Ok, set it
481                         iface->SubnetBits = *(int*)Data;
482                 }
483                 LEAVE('i', iface->SubnetBits);
484                 return iface->SubnetBits;
485         
486         /*
487          * get_device
488          * - Gets the name of the attached device
489          */
490         case 8:
491                 if( iface->Adapter == NULL )
492                         LEAVE_RET('i', 0);
493                 if( Data == NULL )
494                         LEAVE_RET('i', iface->Adapter->DeviceLen);
495                 if( !CheckMem( Data, iface->Adapter->DeviceLen+1 ) )
496                         LEAVE_RET('i', -1);
497                 strcpy( Data, iface->Adapter->Device );
498                 return iface->Adapter->DeviceLen;
499         
500         /*
501          * ping
502          * - Send an ICMP Echo
503          */
504         case 9:
505                 switch(iface->Type)
506                 {
507                 case 0:
508                         LEAVE_RET('i', 1);
509                 
510                 case 4:
511                         if( !CheckMem( Data, sizeof(tIPv4) ) )  LEAVE_RET('i', -1);
512                         tmp = IPv4_Ping(iface, *(tIPv4*)Data);
513                         LEAVE('i', tmp);
514                         return tmp;
515                         
516                 case 6:
517                         LEAVE_RET('i', 1);
518                 }
519                 break;
520         
521         }
522         
523         LEAVE('i', 0);
524         return 0;
525 }
526
527 // --- Internal ---
528 /**
529  * \fn tAdapter *IPStack_GetAdapter(const char *Path)
530  * \brief Gets/opens an adapter given the path
531  */
532 tAdapter *IPStack_GetAdapter(const char *Path)
533 {
534         tAdapter        *dev;
535          int    tmp;
536         
537         ENTER("sPath", Path);
538         
539         // Check for loopback
540         if( strcmp(Path, "LOOPBACK") == 0 )
541         {
542                 // Initialise if required
543                 if( gIP_LoopAdapter.DeviceFD == 0 )
544                 {
545                         dev = &gIP_LoopAdapter;
546                         
547                         dev->NRef = 1;
548                         dev->DeviceLen = 8;
549                         
550                         dev->DeviceFD = VFS_Open( "/Devices/fifo/anon", VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
551                         if( dev->DeviceFD == -1 ) {
552                                 Log_Warning("IPStack", "Unable to open FIFO '/Devices/fifo/anon' for loopback");
553                                 return NULL;
554                         }
555                         
556                         dev->MacAddr.B[0] = 'A';
557                         dev->MacAddr.B[1] = 'c';
558                         dev->MacAddr.B[2] = 'e';
559                         dev->MacAddr.B[3] = 's';
560                         dev->MacAddr.B[4] = 's';
561                         dev->MacAddr.B[5] = '2';
562                         
563                         // Start watcher
564                         Link_WatchDevice( dev );
565                 }
566                 LEAVE('p', &gIP_LoopAdapter);
567                 return &gIP_LoopAdapter;
568         }
569         
570         Mutex_Acquire( &glIP_Adapters );
571         
572         // Check if this adapter is already open
573         for( dev = gIP_Adapters; dev; dev = dev->Next )
574         {
575                 if( strcmp(dev->Device, Path) == 0 ) {
576                         dev->NRef ++;
577                         Mutex_Release( &glIP_Adapters );
578                         LEAVE('p', dev);
579                         return dev;
580                 }
581         }
582         
583         // Ok, so let's open it
584         dev = malloc( sizeof(tAdapter) + strlen(Path) + 1 );
585         if(!dev) {
586                 Mutex_Release( &glIP_Adapters );
587                 LEAVE('n');
588                 return NULL;
589         }
590         
591         // Fill Structure
592         strcpy( dev->Device, Path );
593         dev->NRef = 1;
594         dev->DeviceLen = strlen(Path);
595         
596         // Open Device
597         dev->DeviceFD = VFS_Open( dev->Device, VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE );
598         if( dev->DeviceFD == -1 ) {
599                 free( dev );
600                 Mutex_Release( &glIP_Adapters );
601                 LEAVE('n');
602                 return NULL;
603         }
604         
605         // Check that it is a network interface
606         tmp = VFS_IOCtl(dev->DeviceFD, 0, NULL);
607         LOG("Device type = %i", tmp);
608         if( tmp != DRV_TYPE_NETWORK ) {
609                 Warning("IPStack_GetAdapter: '%s' is not a network interface", dev->Device);
610                 VFS_Close( dev->DeviceFD );
611                 free( dev );
612                 Mutex_Release( &glIP_Adapters );
613                 LEAVE('n');
614                 return NULL;
615         }
616         
617         // Get MAC Address
618         VFS_IOCtl(dev->DeviceFD, NET_IOCTL_GETMAC, &dev->MacAddr);
619         
620         // Add to list
621         dev->Next = gIP_Adapters;
622         gIP_Adapters = dev;
623         
624         Mutex_Release( &glIP_Adapters );
625         
626         // Start watcher
627         Link_WatchDevice( dev );
628         
629         LEAVE('p', dev);
630         return dev;
631 }

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