Fiddling with ARP to handle routing
[tpg/acess2.git] / Modules / IPStack / arp.c
1 /*
2  * Acess2 IP Stack
3  * - Address Resolution Protocol
4  */
5 #define DEBUG   0
6 #include "ipstack.h"
7 #include "arp.h"
8 #include "link.h"
9
10 #define ARPv6   1
11 #define ARP_CACHE_SIZE  64
12 #define ARP_MAX_AGE             (60*60*1000)    // 1Hr
13
14 // === IMPORTS ===
15 extern tInterface       *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
16 #if ARPv6
17 extern tInterface       *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast);
18 #endif
19
20 // === PROTOTYPES ===
21  int    ARP_Initialise();
22 tMacAddr        ARP_Resolve4(tInterface *Interface, tIPv4 Address);
23 void    ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer);
24
25 // === GLOBALS ===
26 struct sARP_Cache4 {
27         tIPv4   IP;
28         tMacAddr        MAC;
29         Sint64  LastUpdate;
30         Sint64  LastUsed;
31 }       *gaARP_Cache4;
32  int    giARP_Cache4Space;
33 tMutex  glARP_Cache4;
34 #if ARPv6
35 struct sARP_Cache6 {
36         tIPv6   IP;
37         tMacAddr        MAC;
38         Sint64  LastUpdate;
39         Sint64  LastUsed;
40 }       *gaARP_Cache6;
41  int    giARP_Cache6Space;
42 tMutex  glARP_Cache6;
43 #endif
44  int    giARP_LastUpdateID = 0;
45
46 // === CODE ===
47 /**
48  * \fn int ARP_Initialise()
49  * \brief Initalise the ARP section
50  */
51 int ARP_Initialise()
52 {
53         gaARP_Cache4 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
54         memset( gaARP_Cache4, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
55         giARP_Cache4Space = ARP_CACHE_SIZE;
56         
57         #if ARPv6
58         gaARP_Cache6 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
59         memset( gaARP_Cache6, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
60         giARP_Cache6Space = ARP_CACHE_SIZE;
61         #endif
62         
63         Link_RegisterType(0x0806, ARP_int_GetPacket);
64         return 1;
65 }
66
67 /**
68  * \brief Resolves a MAC address from an IPv4 address
69  */
70 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
71 {
72          int    lastID;
73          int    i;
74         struct sArpRequest4     req;
75         
76         ENTER("pInterface xAddress", Interface, Address);
77         
78         Mutex_Acquire( &glARP_Cache4 );
79         for( i = 0; i < giARP_Cache4Space; i++ )
80         {
81                 if(gaARP_Cache4[i].IP.L != Address.L)   continue;
82                 
83                 // Check if the entry needs to be refreshed
84                 if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE )  break;
85                 
86                 Mutex_Release( &glARP_Cache4 );
87                 LOG("Return %x:%x:%x:%x:%x:%x",
88                         gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1],
89                         gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3],
90                         gaARP_Cache4[i].MAC.B[4], gaARP_Cache4[i].MAC.B[5]
91                         );
92                 LEAVE('-');
93                 return gaARP_Cache4[i].MAC;
94         }
95         Mutex_Release( &glARP_Cache4 );
96         
97         lastID = giARP_LastUpdateID;
98         
99         // Create request
100         Log_Log("ARP4", "Asking for address %i.%i.%i.%i",
101                 Address.B[0], Address.B[1], Address.B[2], Address.B[3]
102                 );
103         req.HWType = htons(0x0001);     // Ethernet
104         req.Type   = htons(0x0800);
105         req.HWSize = 6;
106         req.SWSize = 4;
107         req.Request = htons(1);
108         req.SourceMac = Interface->Adapter->MacAddr;
109         req.SourceIP = Interface->IP4.Address;
110         req.DestMac = cMAC_BROADCAST;
111         req.DestIP = Address;
112         
113         // Send Request
114         Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
115         
116         // Wait for a reply
117         for(;;)
118         {
119                 while(lastID == giARP_LastUpdateID)     Threads_Yield();
120                 lastID = giARP_LastUpdateID;
121                 
122                 Mutex_Acquire( &glARP_Cache4 );
123                 for( i = 0; i < giARP_Cache4Space; i++ )
124                 {
125                         if(gaARP_Cache4[i].IP.L != Address.L)   continue;
126                         
127                         Mutex_Release( &glARP_Cache4 );
128                         return gaARP_Cache4[i].MAC;
129                 }
130                 Mutex_Release( &glARP_Cache4 );
131         }
132 }
133
134 /**
135  * \brief Updates the ARP Cache entry for an IPv4 Address
136  */
137 void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
138 {
139          int    i;
140          int    free = -1;
141          int    oldest = 0;
142         
143         // Find an entry for the IP address in the cache
144         Mutex_Acquire(&glARP_Cache4);
145         for( i = giARP_Cache4Space; i--; )
146         {
147                 if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
148                         oldest = i;
149                 }
150                 if( gaARP_Cache4[i].IP.L == SWAddr.L )  break;
151                 if( gaARP_Cache4[i].LastUpdate == 0 && free == -1 )     free = i;
152         }
153         // If there was no match, we need to make one
154         if(i == -1) {
155                 if(free != -1)
156                         i = free;
157                 else
158                         i = oldest;
159                 gaARP_Cache4[i].IP = SWAddr;
160         }
161         
162         Log_Log("ARP4", "Caching %i.%i.%i.%i (%02x:%02x:%02x:%02x:%02x:%02x) in %i",
163                 SWAddr.B[0], SWAddr.B[1], SWAddr.B[2], SWAddr.B[3],
164                 HWAddr.B[0], HWAddr.B[1], HWAddr.B[2], HWAddr.B[3], HWAddr.B[4], HWAddr.B[5],
165                 i
166                 );
167                 
168         gaARP_Cache4[i].MAC = HWAddr;
169         gaARP_Cache4[i].LastUpdate = now();
170         giARP_LastUpdateID ++;
171         Mutex_Release(&glARP_Cache4);
172 }
173
174 #if ARPv6
175 /**
176  * \brief Updates the ARP Cache entry for an IPv6 Address
177  */
178 void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
179 {
180          int    i;
181          int    free = -1;
182          int    oldest = 0;
183         
184         // Find an entry for the MAC address in the cache
185         Mutex_Acquire(&glARP_Cache6);
186         for( i = giARP_Cache6Space; i--; )
187         {
188                 if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
189                         oldest = i;
190                 }
191                 if( MAC_EQU(gaARP_Cache6[i].MAC, HWAddr) )      break;
192                 if( gaARP_Cache6[i].LastUpdate == 0 && free == -1 )     free = i;
193         }
194         // If there was no match, we need to make one
195         if(i == -1) {
196                 if(free != -1)
197                         i = free;
198                 else
199                         i = oldest;
200                 gaARP_Cache6[i].MAC = HWAddr;
201         }
202         
203         gaARP_Cache6[i].IP = SWAddr;
204         gaARP_Cache6[i].LastUpdate = now();
205         giARP_LastUpdateID ++;
206         Mutex_Release(&glARP_Cache6);
207 }
208 #endif
209
210 /**
211  * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
212  * \brief Called when an ARP packet is recieved
213  */
214 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
215 {
216         tArpRequest4    *req4 = Buffer;
217         #if ARPv6
218         tArpRequest6    *req6 = Buffer;
219         #endif
220         tInterface      *iface;
221         
222         // Sanity Check Packet
223         if( Length < (int)sizeof(tArpRequest4) ) {
224                 Log_Log("ARP", "Recieved undersized packet");
225                 return ;
226         }
227         if( ntohs(req4->Type) != 0x0800 ) {
228                 Log_Log("ARP", "Recieved a packet with a bad type (0x%x)", ntohs(req4->Type));
229                 return ;
230         }
231         if( req4->HWSize != 6 ) {
232                 Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
233                 return;
234         }
235         #if ARP_DETECT_SPOOFS
236         if( !MAC_EQU(req4->SourceMac, From) ) {
237                 Log_Log("ARP", "ARP spoofing detected "
238                         "(%02x%02x:%02x%02x:%02x%02x != %02x%02x:%02x%02x:%02x%02x)",
239                         req4->SourceMac.B[0], req4->SourceMac.B[1], req4->SourceMac.B[2],
240                         req4->SourceMac.B[3], req4->SourceMac.B[4], req4->SourceMac.B[5],
241                         From.B[0], From.B[1], From.B[2],
242                         From.B[3], From.B[4], From.B[5]
243                         );
244                 return;
245         }
246         #endif
247         
248         Log_Debug("ARP", "Request ID %i", ntohs(req4->Request));
249         
250         switch( ntohs(req4->Request) )
251         {
252         case 1: // You want my IP?
253                 Log_Debug("ARP", "ARP Request Address class %i", req4->SWSize);
254                 // Check what type of IP it is
255                 switch( req4->SWSize )
256                 {
257                 case 4:
258                         Log_Debug("ARP", "From MAC %02x:%02x:%02x:%02x:%02x:%02x",
259                                 req4->SourceMac.B[0], req4->SourceMac.B[1],
260                                 req4->SourceMac.B[2], req4->SourceMac.B[3],
261                                 req4->SourceMac.B[4], req4->SourceMac.B[5]);
262                         //Log_Debug("ARP", "to MAC %02x:%02x:%02x:%02x:%02x:%02x",
263                         //      req4->DestMac.B[0], req4->DestMac.B[1],
264                         //      req4->DestMac.B[2], req4->DestMac.B[3],
265                         //      req4->DestMac.B[4], req4->DestMac.B[5]);
266                         Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
267                                 req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
268                                 req4->DestIP.B[3],
269                                 req4->SourceIP.B[0], req4->SourceIP.B[1],
270                                 req4->SourceIP.B[2], req4->SourceIP.B[3]);
271                         iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
272                         if( iface )
273                         {
274                                 Log_Debug("ARP", "Caching sender's IP Address");
275                                 ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
276                                 
277                                 req4->DestIP = req4->SourceIP;
278                                 req4->DestMac = req4->SourceMac;
279                                 req4->SourceIP = iface->IP4.Address;
280                                 req4->SourceMac = Adapter->MacAddr;
281                                 req4->Request = htons(2);
282                                 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
283                                         req4->SourceMac.B[0], req4->SourceMac.B[1],
284                                         req4->SourceMac.B[2], req4->SourceMac.B[3],
285                                         req4->SourceMac.B[4], req4->SourceMac.B[5]);
286                                 Link_SendPacket(Adapter, 0x0806, req4->DestMac, sizeof(tArpRequest4), req4);
287                         }
288                         break;
289                 #if ARPv6
290                 case 6:
291                         if( Length < (int)sizeof(tArpRequest6) ) {
292                                 Log_Log("ARP", "Recieved undersized packet (IPv6)");
293                                 return ;
294                         }
295                         Log_Debug("ARP", "ARP Request IPv6 Address %08x:%08x:%08x:%08x",
296                                 ntohl(req6->DestIP.L[0]), ntohl(req6->DestIP.L[1]),
297                                 ntohl(req6->DestIP.L[2]), ntohl(req6->DestIP.L[3])
298                                 );
299                         iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
300                         if( iface )
301                         {
302                                 req6->DestIP = req6->SourceIP;
303                                 req6->DestMac = req6->SourceMac;
304                                 req6->SourceIP = iface->IP6.Address;
305                                 req6->SourceMac = Adapter->MacAddr;
306                                 req6->Request = htons(2);
307                                 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
308                                         req4->SourceMac.B[0], req4->SourceMac.B[1],
309                                         req4->SourceMac.B[2], req4->SourceMac.B[3],
310                                         req4->SourceMac.B[4], req4->SourceMac.B[5]);
311                                 Link_SendPacket(Adapter, 0x0806, req6->DestMac, sizeof(tArpRequest6), req6);
312                         }
313                         break;
314                 #endif
315                 default:
316                         Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
317                         return ;
318                 }
319                 
320                 break;
321         
322         case 2: // Ooh! A response!             
323                 // Check what type of IP it is
324                 switch( req4->SWSize )
325                 {
326                 case 4:
327                         ARP_UpdateCache4( req4->SourceIP, From );
328                         break;
329                 #if ARPv6
330                 case 6:
331                         if( Length < (int)sizeof(tArpRequest6) ) {
332                                 Log_Debug("ARP", "Recieved undersized packet (IPv6)");
333                                 return ;
334                         }
335                         ARP_UpdateCache6( req6->SourceIP, From );
336                         break;
337                 #endif
338                 default:
339                         Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
340                         return ;
341                 }
342                 
343                 break;
344         
345         default:
346                 Log_Warning("ARP", "Unknown Request ID %i", ntohs(req4->Request));
347                 break;
348         }
349 }

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