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

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