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

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