Updating IPStack to have firewall hooks (well, the start of them)
[tpg/acess2.git] / Modules / IPStack / ipv4.c
1 /*
2  * Acess2 IP Stack
3  * - IPv4 Protcol Handling
4  */
5 #include "ipstack.h"
6 #include "link.h"
7 #include "ipv4.h"
8 #include "firewall.h"
9
10 #define DEFAULT_TTL     32
11
12 // === IMPORTS ===
13 extern tInterface       *gIP_Interfaces;
14 extern void     ICMP_Initialise();
15 extern  int     ICMP_Ping(tInterface *Interface, tIPv4 Addr);
16 extern tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address);
17
18 // === PROTOTYPES ===
19  int    IPv4_Initialise();
20  int    IPv4_RegisterCallback(int ID, tIPCallback Callback);
21 void    IPv4_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer);
22 tInterface      *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
23 Uint32  IPv4_Netmask(int FixedBits);
24 Uint16  IPv4_Checksum(const void *Buf, int Size);
25  int    IPv4_Ping(tInterface *Iface, tIPv4 Addr);
26
27 // === GLOBALS ===
28 tIPCallback     gaIPv4_Callbacks[256];
29
30 // === CODE ===
31 /**
32  * \fn int IPv4_Initialise()
33  */
34 int IPv4_Initialise()
35 {
36         ICMP_Initialise();
37         Link_RegisterType(IPV4_ETHERNET_ID, IPv4_int_GetPacket);
38         return 1;
39 }
40
41 /**
42  * \fn int IPv4_RegisterCallback( int ID, tIPCallback Callback )
43  * \brief Registers a callback
44  */
45 int IPv4_RegisterCallback(int ID, tIPCallback Callback)
46 {
47         if( ID < 0 || ID > 255 )        return 0;
48         if( gaIPv4_Callbacks[ID] )      return 0;
49         gaIPv4_Callbacks[ID] = Callback;
50         return 1;
51 }
52
53 /**
54  * \brief Creates and sends an IPv4 Packet
55  * \param Iface Interface
56  * \param Address       Destination IP
57  * \param Protocol      Protocol ID
58  * \param ID    Some random ID number
59  * \param Length        Data Length
60  * \param Data  Packet Data
61  */
62 int IPv4_SendPacket(tInterface *Iface, tIPv4 Address, int Protocol, int ID, int Length, const void *Data)
63 {
64         tMacAddr        to = ARP_Resolve4(Iface, Address);
65          int    bufSize = sizeof(tIPv4Header) + Length;
66         char    buf[bufSize];
67         tIPv4Header     *hdr = (void*)buf;
68          int    ret;
69         
70         // TODO: OUTPUT Firewall rule go here
71         ret = IPTablesV4_TestChain("OUTPUT",
72                 &Iface->IP4.Address, &Address,
73                 Protocol, 0,
74                 Length, Data);
75         
76         if(ret != 0)
77         {
78                 // Just drop it
79                 return 0;
80         }
81         
82         memcpy(&hdr->Options[0], Data, Length);
83         hdr->Version = 4;
84         hdr->HeaderLength = sizeof(tIPv4Header)/4;
85         hdr->DiffServices = 0;  // TODO: Check
86         
87         hdr->Reserved = 0;
88         hdr->DontFragment = 0;
89         hdr->MoreFragments = 0;
90         hdr->FragOffLow = 0;
91         hdr->FragOffHi = 0;
92         
93         hdr->TotalLength = htons( bufSize );
94         hdr->Identifcation = htons( ID );       // TODO: Check
95         hdr->TTL = DEFAULT_TTL;
96         hdr->Protocol = Protocol;
97         hdr->HeaderChecksum = 0;        // Will be set later
98         hdr->Source = Iface->IP4.Address;
99         hdr->Destination = Address;
100         hdr->HeaderChecksum = IPv4_Checksum(hdr, sizeof(tIPv4Header));
101         
102         Log_Log("IPv4", "Sending packet to %i.%i.%i.%i",
103                 Address.B[0], Address.B[1], Address.B[2], Address.B[3]);
104         Link_SendPacket(Iface->Adapter, IPV4_ETHERNET_ID, to, bufSize, buf);
105         return 1;
106 }
107
108 /**
109  * \fn void IPv4_int_GetPacket(tInterface *Adapter, tMacAddr From, int Length, void *Buffer)
110  * \brief Process an IPv4 Packet
111  */
112 void IPv4_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
113 {
114         tIPv4Header     *hdr = Buffer;
115         tInterface      *iface;
116         Uint8   *data;
117          int    dataLength;
118          int    ret;
119          
120         if(Length < sizeof(tIPv4Header))        return;
121         
122         #if 0
123         //Log_Log("IPv4", "Version = %i", hdr->Version);
124         //Log_Log("IPv4", "HeaderLength = %i", hdr->HeaderLength);
125         //Log_Log("IPv4", "DiffServices = %i", hdr->DiffServices);
126         Log_Log("IPv4", "TotalLength = %i", ntohs(hdr->TotalLength) );
127         //Log_Log("IPv4", "Identifcation = %i", ntohs(hdr->Identifcation) );
128         //Log_Log("IPv4", "TTL = %i", hdr->TTL );
129         Log_Log("IPv4", "Protocol = %i", hdr->Protocol );
130         //Log_Log("IPv4", "HeaderChecksum = 0x%x", ntohs(hdr->HeaderChecksum) );
131         Log_Log("IPv4", "Source = %i.%i.%i.%i",
132                 hdr->Source.B[0], hdr->Source.B[1], hdr->Source.B[2], hdr->Source.B[3] );
133         Log_Log("IPv4", "Destination = %i.%i.%i.%i",
134                 hdr->Destination.B[0], hdr->Destination.B[1],
135                 hdr->Destination.B[2], hdr->Destination.B[3] );
136         #endif  
137
138         // Check that the version IS IPv4
139         if(hdr->Version != 4) {
140                 Log_Log("IPv4", "hdr->Version(%i) != 4", hdr->Version);
141                 return;
142         }
143         
144         // Check Header checksum
145         //TODO
146         
147         // Check Packet length
148         if( ntohs(hdr->TotalLength) > Length) {
149                 Log_Log("IPv4", "hdr->TotalLength(%i) > Length(%i)", ntohs(hdr->TotalLength), Length);
150                 return;
151         }
152         
153         
154         // TODO: Handle packet fragmentation
155         
156         // Get Data and Data Length
157         dataLength = ntohs(hdr->TotalLength) - sizeof(tIPv4Header);
158         data = &hdr->Options[0];
159         
160         // Get Interface (allowing broadcasts)
161         iface = IPv4_GetInterface(Adapter, hdr->Destination, 1);
162         
163         // TODO: INPUT Firewall Rule
164         if( iface ) {
165                 ret = IPTablesV4_TestChain("INPUT",
166                         &hdr->Source, &hdr->Destination,
167                         hdr->Protocol, 0,
168                         dataLength, data
169                         );
170         }
171         else {
172                 ret = IPTablesV4_TestChain("FORWARD",
173                         &hdr->Source, &hdr->Destination,
174                         hdr->Protocol, 0,
175                         dataLength, data
176                         );
177         }
178         switch(ret)
179         {
180         // 0 - Allow
181         case 0: break;
182         // 1 - Silent Drop
183         case 1: return ;
184         // Unknown, silent drop
185         default:
186                 return ;
187         }
188         
189         // Routing
190         if(!iface)
191         {
192                 Log_Log("IPv4", "Route the packet");
193                 
194                 // TODO: Parse Routing tables and determine where to send it
195                 
196                 return ;
197         }
198         
199         // Send it on
200         if( gaIPv4_Callbacks[hdr->Protocol] )
201                 gaIPv4_Callbacks[hdr->Protocol] (iface, &hdr->Source, dataLength, data);
202         else
203                 Log_Log("IPv4", "Unknown Protocol %i", hdr->Protocol);
204 }
205
206 /**
207  * \fn tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address)
208  * \brief Searches an adapter for a matching address
209  */
210 tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast)
211 {
212         tInterface      *iface = NULL;
213         Uint32  netmask;
214         Uint32  addr, this;
215         
216         addr = ntohl( Address.L );
217         
218         for( iface = gIP_Interfaces; iface; iface = iface->Next)
219         {
220                 if( iface->Adapter != Adapter ) continue;
221                 if( iface->Type != 4 )  continue;
222                 if( IP4_EQU(Address, iface->IP4.Address) )
223                         return iface;
224                 
225                 if( !Broadcast )        continue;
226                 
227                 this = ntohl( iface->IP4.Address.L );
228                 
229                 // Check for broadcast
230                 netmask = IPv4_Netmask(iface->IP4.SubnetBits);
231                 
232                 if( (addr & netmask) == (this & netmask)
233                  && (addr & ~netmask) == (0xFFFFFFFF & ~netmask) )
234                         return iface;
235         }
236         return NULL;
237 }
238
239 /**
240  * \brief Convert a network prefix to a netmask
241  * 
242  * For example /24 will become 255.255.255.0
243  */
244 Uint32 IPv4_Netmask(int FixedBits)
245 {
246         Uint32  ret = 0xFFFFFFFF;
247         ret >>= (32-FixedBits);
248         ret <<= (32-FixedBits);
249         // Returns a native endian netmask
250         return ret;
251 }
252
253 /**
254  * \brief Calculate the IPv4 Checksum
255  */
256 Uint16 IPv4_Checksum(const void *Buf, int Size)
257 {
258         Uint16  sum = 0;
259         const Uint16    *arr = Buf;
260          int    i;
261         
262         Size = (Size + 1) >> 1; // 16-bit word count
263         for(i = 0; i < Size; i++ )
264         {
265                 if((int)sum + arr[i] > 0xFFFF)
266                         sum ++; // Simulate 1's complement
267                 sum += arr[i];
268         }
269         return ~sum ;
270 }
271
272 /**
273  * \brief Sends an ICMP Echo and waits for a reply
274  */
275 int IPv4_Ping(tInterface *Iface, tIPv4 Addr)
276 {
277         return ICMP_Ping(Iface, Addr);
278 }

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