Modules/VIARhineII - Working on a driver, doesn't do anything yet
[tpg/acess2.git] / KernelLand / Modules / IPStack / main.c
1 /*
2  * Acess2 IP Stack
3  * - Stack Initialisation
4  */
5 #define DEBUG   0
6 #define VERSION VER2(0,10)
7 #include "ipstack.h"
8 #include "link.h"
9 #include <modules.h>
10 #include <fs_devfs.h>
11
12 // === IMPORTS ===
13 extern int      ARP_Initialise();
14 extern void     UDP_Initialise();
15 extern void     TCP_Initialise();
16 extern int      IPv4_Initialise();
17 extern int      IPv6_Initialise();
18
19 extern tAdapter *IPStack_GetAdapter(const char *Path);
20 extern char     *IPStack_Root_ReadDir(tVFS_Node *Node, int Pos);
21 extern tVFS_Node        *IPStack_Root_FindDir(tVFS_Node *Node, const char *Name);
22 extern int      IPStack_Root_IOCtl(tVFS_Node *Node, int ID, void *Data);
23 extern tInterface       gIP_LoopInterface;
24 extern tInterface       *IPStack_AddInterface(const char *Device, const char *Name);
25 extern tRoute   *IPStack_AddRoute(const char *Interface, void *Network, int SubnetBits, void *NextHop, int Metric);
26
27 // === PROTOTYPES ===
28  int    IPStack_Install(char **Arguments);
29  int    IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits);
30
31 // === GLOBALS ===
32 MODULE_DEFINE(0, VERSION, IPStack, IPStack_Install, NULL, NULL);
33 tVFS_NodeType   gIP_RootNodeType = {
34         .ReadDir = IPStack_Root_ReadDir,
35         .FindDir = IPStack_Root_FindDir,
36         .IOCtl = IPStack_Root_IOCtl
37 };
38 tDevFS_Driver   gIP_DriverInfo = {
39         NULL, "ip",
40         {
41         .Size = -1,     // Number of interfaces
42         .NumACLs = 1,
43         .ACLs = &gVFS_ACL_EveryoneRX,
44         .Flags = VFS_FFLAG_DIRECTORY,
45         .Type = &gIP_RootNodeType
46         }
47 };
48
49 // === CODE ===
50 /**
51  * \fn int IPStack_Install(char **Arguments)
52  * \brief Intialise the relevant parts of the stack and register with DevFS
53  */
54 int IPStack_Install(char **Arguments)
55 {
56          int    i = 0;
57         
58         // Layer 3 - Network Layer Protocols
59         ARP_Initialise();
60         IPv4_Initialise();
61         IPv6_Initialise();
62         // Layer 4 - Transport Layer Protocols
63         TCP_Initialise();
64         UDP_Initialise();
65         
66         if(Arguments)
67         {
68                 // Parse module arguments
69                 for( i = 0; Arguments[i]; i++ )
70                 {
71                         // TODO:
72                         // Define interfaces by <Device>:<Type>:<HexStreamAddress>:<Bits>
73                         // Where:
74                         // - <Device> is the device path (E.g. /Devices/ne2k/0)
75                         // - <Type> is a number (e.g. 4) or symbol (e.g. AF_INET4)
76                         // - <HexStreamAddress> is a condensed hexadecimal stream (in big endian)
77                         //      (E.g. 0A000201 for 10.0.2.1 IPv4)
78                         // - <Bits> is the number of subnet bits (E.g. 24 for an IPv4 Class C)
79                         // Example: /Devices/ne2k/0:4:0A00020A:24
80                         //  would define an interface with the address 10.0.2.10/24
81                         if( Arguments[i][0] == '/' ) {
82                                 // Define Interface
83                                 char    *dev, *type, *addr, *bits;
84                                 
85                                 // Read definition
86                                 dev = Arguments[i];
87                                 type = strchr(dev, ':');
88                                 if( !type ) {
89                                         Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
90                                         continue;
91                                 }
92                                 *type = '\0';   type ++;
93                                 
94                                 addr = strchr(type, ':');
95                                 if( !addr ) {
96                                         Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
97                                         continue;
98                                 }
99                                 *addr = '\0';   addr ++;
100                                 
101                                 bits = strchr(addr, ':');
102                                 if( !bits ) {
103                                         Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
104                                         continue;
105                                 }
106                                 *bits = '\0';   bits ++;
107                                 
108                                 // Define interface
109                                 {
110                                          int    iType = atoi(type);
111                                          int    size = IPStack_GetAddressSize(iType);
112                                         Uint8   addrData[size];
113                                          int    iBits = atoi(bits);
114                                         
115                                         UnHex(addrData, size, addr);
116                                         
117                                         tInterface      *iface = IPStack_AddInterface(dev, "");
118                                         if( !iface ) {
119                                                 Log_Warning("IPStack", "Unable to add interface on '%s'", dev);
120                                                 continue ;
121                                         }
122                                         iface->Type = iType;
123                                         memcpy(iface->Address, addrData, size);
124                                         iface->SubnetBits = iBits;
125                                         
126                                         // Route for addrData/iBits, no next hop, default metric
127                                         IPStack_AddRoute(iface->Name, iface->Address, iBits, NULL, 0);
128
129                                         Log_Notice("IPStack", "Boot interface %s/%i on %s",
130                                                 IPStack_PrintAddress(iType, addrData), iBits,
131                                                 dev);
132                                 }
133                                 
134                                 continue;
135                         }
136                         
137                         // I could also define routes using <Interface>:<HexStreamNetwork>:<Bits>[:<HexStreamGateway>]
138                         // Example: 1:00000000:0:0A000201
139                         if( '0' <= Arguments[i][0] && Arguments[i][0] <= '9' )
140                         {
141                                 // Define Interface
142                                 char    *ifaceName, *network, *bits, *gateway;
143                                 
144                                 // Read definition
145                                 ifaceName = Arguments[i];
146                                 
147                                 network = strchr(ifaceName, ':');
148                                 if( !network ) {
149                                         Log_Warning("IPStack", "<iface>:<HexStreamNetwork>:<Bits>:<HexStreamGateway>");
150                                         continue;
151                                 }
152                                 *network = '\0';        network ++;
153                                 
154                                 bits = strchr(network, ':');
155                                 if( !bits ) {
156                                         Log_Warning("IPStack", "<Device>:<Type>:<HexStreamAddress>:<Bits>");
157                                         continue;
158                                 }
159                                 *bits = '\0';   bits ++;
160                                 
161                                 gateway = strchr(bits, ':');
162                                 if( gateway ) {
163                                         *gateway = '\0';        gateway ++;
164                                 }
165                                 
166                                 // Define route
167                                 {
168                                         tVFS_Node       *node = IPStack_Root_FindDir(NULL, ifaceName);
169                                         if( !node ) {
170                                                 Log_Warning("IPStack", "Unknown interface '%s' in arg %i", ifaceName, i);
171                                                 continue ;
172                                         }
173                                         tInterface      *iface = node->ImplPtr;
174                                         
175                                          int    size = IPStack_GetAddressSize(iface->Type);
176                                         Uint8   netData[size];
177                                         Uint8   gwData[size];
178                                          int    iBits = atoi(bits);
179                                         
180                                         UnHex(netData, size, network);
181                                         if( gateway )
182                                                 UnHex(gwData, size, gateway);
183                                         else
184                                                 memset(gwData, 0, size);
185                                         
186                                         IPStack_AddRoute(ifaceName, netData, iBits, gwData, 30);
187                                 }
188                                 
189                                 continue;
190                         }
191                 }
192         }
193         
194         // Initialise loopback interface
195         gIP_LoopInterface.Adapter = IPStack_GetAdapter("LOOPBACK");
196         
197         DevFS_AddDevice( &gIP_DriverInfo );
198         
199         return MODULE_ERR_OK;
200 }
201
202 /**
203  * \brief Gets the size (in bytes) of a specified form of address
204  */
205 int IPStack_GetAddressSize(int AddressType)
206 {
207         switch(AddressType)
208         {
209         case -1:        // -1 = maximum
210                 return sizeof(tIPv6);
211         
212         case AF_NULL:
213                 return 0;
214         
215         case AF_INET4:
216                 return sizeof(tIPv4);
217         case AF_INET6:
218                 return sizeof(tIPv6);
219                 
220         default:
221                 return 0;
222         }
223 }
224
225 /**
226  * \brief Compare two IP Addresses masked by CheckBits
227  */
228 int IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits)
229 {
230          int    size = IPStack_GetAddressSize(AddressType);
231         Uint8   mask;
232         const Uint8     *addr1 = Address1, *addr2 = Address2;
233         
234         // Sanity check size
235         if( CheckBits < 0 )     CheckBits = size*8;
236         if( CheckBits > size*8 )        CheckBits = size*8;
237         
238         if( CheckBits == 0 )    return 1;       // /0 matches anything
239         
240         // Check first bits/8 bytes
241         if( memcmp(Address1, Address2, CheckBits/8) != 0 )      return 0;
242         
243         // Check if the mask is a multiple of 8
244         if( CheckBits % 8 == 0 )        return 1;
245         
246         // Check last bits
247         mask = 0xFF << (8 - (CheckBits % 8));
248         if( (addr1[CheckBits/8] & mask) == (addr2[CheckBits/8] & mask) )
249                 return 1;
250         
251         return 0;
252 }
253
254 const char *IPStack_PrintAddress(int AddressType, const void *Address)
255 {
256         switch( AddressType )
257         {
258         case 4: {
259                 static char     ret[4*3+3+1];
260                 const Uint8     *addr = Address;
261                 sprintf(ret, "%i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
262                 return ret;
263                 }
264         
265         case 6: {       // TODO: address compression
266                 static char     ret[8*4+7+1];
267                 const Uint16    *addr = Address;
268                 sprintf(ret, "%x:%x:%x:%x:%x:%x:%x:%x",
269                         ntohs(addr[0]), ntohs(addr[1]), ntohs(addr[2]), ntohs(addr[3]),
270                         ntohs(addr[4]), ntohs(addr[5]), ntohs(addr[6]), ntohs(addr[7])
271                         );
272                 return ret;
273                 }
274         
275         default:
276                 return "";
277         }
278 }

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