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

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