General fixes
[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  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         
77         ENTER("pInterface xAddress", Interface, Address);
78         
79         // Check routing tables if not on this subnet
80         if( IPStack_CompareAddress(4, &Address, Interface->Address, Interface->SubnetBits) == 0 )
81         {
82                 tRoute  *route = IPStack_FindRoute(4, Interface, &Address);
83                 if( route ) {
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( ((tIPv4*)route->NextHop)->L != 0 ) {
87                                 // Recursion: see /Recursion/
88                                 return ARP_Resolve4(Interface, *(tIPv4*)route->NextHop);
89                         }
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         // Wait for a reply
133         for(;;)
134         {
135                 while(lastID == giARP_LastUpdateID)     Threads_Yield();
136                 lastID = giARP_LastUpdateID;
137                 
138                 Mutex_Acquire( &glARP_Cache4 );
139                 for( i = 0; i < giARP_Cache4Space; i++ )
140                 {
141                         if(gaARP_Cache4[i].IP.L != Address.L)   continue;
142                         
143                         Mutex_Release( &glARP_Cache4 );
144                         return gaARP_Cache4[i].MAC;
145                 }
146                 Mutex_Release( &glARP_Cache4 );
147         }
148 }
149
150 /**
151  * \brief Updates the ARP Cache entry for an IPv4 Address
152  */
153 void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
154 {
155          int    i;
156          int    free = -1;
157          int    oldest = 0;
158         
159         // Find an entry for the IP address in the cache
160         Mutex_Acquire(&glARP_Cache4);
161         for( i = giARP_Cache4Space; i--; )
162         {
163                 if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
164                         oldest = i;
165                 }
166                 if( gaARP_Cache4[i].IP.L == SWAddr.L )  break;
167                 if( gaARP_Cache4[i].LastUpdate == 0 && free == -1 )     free = i;
168         }
169         // If there was no match, we need to make one
170         if(i == -1) {
171                 if(free != -1)
172                         i = free;
173                 else
174                         i = oldest;
175                 gaARP_Cache4[i].IP = SWAddr;
176         }
177         
178         Log_Log("ARP4", "Caching %i.%i.%i.%i (%02x:%02x:%02x:%02x:%02x:%02x) in %i",
179                 SWAddr.B[0], SWAddr.B[1], SWAddr.B[2], SWAddr.B[3],
180                 HWAddr.B[0], HWAddr.B[1], HWAddr.B[2], HWAddr.B[3], HWAddr.B[4], HWAddr.B[5],
181                 i
182                 );
183                 
184         gaARP_Cache4[i].MAC = HWAddr;
185         gaARP_Cache4[i].LastUpdate = now();
186         giARP_LastUpdateID ++;
187         Mutex_Release(&glARP_Cache4);
188 }
189
190 #if ARPv6
191 /**
192  * \brief Updates the ARP Cache entry for an IPv6 Address
193  */
194 void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
195 {
196          int    i;
197          int    free = -1;
198          int    oldest = 0;
199         
200         // Find an entry for the MAC address in the cache
201         Mutex_Acquire(&glARP_Cache6);
202         for( i = giARP_Cache6Space; i--; )
203         {
204                 if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
205                         oldest = i;
206                 }
207                 if( MAC_EQU(gaARP_Cache6[i].MAC, HWAddr) )      break;
208                 if( gaARP_Cache6[i].LastUpdate == 0 && free == -1 )     free = i;
209         }
210         // If there was no match, we need to make one
211         if(i == -1) {
212                 if(free != -1)
213                         i = free;
214                 else
215                         i = oldest;
216                 gaARP_Cache6[i].MAC = HWAddr;
217         }
218         
219         gaARP_Cache6[i].IP = SWAddr;
220         gaARP_Cache6[i].LastUpdate = now();
221         giARP_LastUpdateID ++;
222         Mutex_Release(&glARP_Cache6);
223 }
224 #endif
225
226 /**
227  * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
228  * \brief Called when an ARP packet is recieved
229  */
230 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
231 {
232         tArpRequest4    *req4 = Buffer;
233         #if ARPv6
234         tArpRequest6    *req6 = Buffer;
235         #endif
236         tInterface      *iface;
237         
238         // Sanity Check Packet
239         if( Length < (int)sizeof(tArpRequest4) ) {
240                 Log_Log("ARP", "Recieved undersized packet");
241                 return ;
242         }
243         if( ntohs(req4->Type) != 0x0800 ) {
244                 Log_Log("ARP", "Recieved a packet with a bad type (0x%x)", ntohs(req4->Type));
245                 return ;
246         }
247         if( req4->HWSize != 6 ) {
248                 Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
249                 return;
250         }
251         #if ARP_DETECT_SPOOFS
252         if( !MAC_EQU(req4->SourceMac, From) ) {
253                 Log_Log("ARP", "ARP spoofing detected "
254                         "(%02x%02x:%02x%02x:%02x%02x != %02x%02x:%02x%02x:%02x%02x)",
255                         req4->SourceMac.B[0], req4->SourceMac.B[1], req4->SourceMac.B[2],
256                         req4->SourceMac.B[3], req4->SourceMac.B[4], req4->SourceMac.B[5],
257                         From.B[0], From.B[1], From.B[2],
258                         From.B[3], From.B[4], From.B[5]
259                         );
260                 return;
261         }
262         #endif
263         
264         Log_Debug("ARP", "Request ID %i", ntohs(req4->Request));
265         
266         switch( ntohs(req4->Request) )
267         {
268         case 1: // You want my IP?
269                 Log_Debug("ARP", "ARP Request Address class %i", req4->SWSize);
270                 // Check what type of IP it is
271                 switch( req4->SWSize )
272                 {
273                 case 4:
274                         Log_Debug("ARP", "From MAC %02x:%02x:%02x:%02x:%02x:%02x",
275                                 req4->SourceMac.B[0], req4->SourceMac.B[1],
276                                 req4->SourceMac.B[2], req4->SourceMac.B[3],
277                                 req4->SourceMac.B[4], req4->SourceMac.B[5]);
278                         //Log_Debug("ARP", "to MAC %02x:%02x:%02x:%02x:%02x:%02x",
279                         //      req4->DestMac.B[0], req4->DestMac.B[1],
280                         //      req4->DestMac.B[2], req4->DestMac.B[3],
281                         //      req4->DestMac.B[4], req4->DestMac.B[5]);
282                         Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
283                                 req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
284                                 req4->DestIP.B[3],
285                                 req4->SourceIP.B[0], req4->SourceIP.B[1],
286                                 req4->SourceIP.B[2], req4->SourceIP.B[3]);
287                         iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
288                         if( iface )
289                         {
290                                 Log_Debug("ARP", "Caching sender's IP Address");
291                                 ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
292                                 
293                                 req4->DestIP = req4->SourceIP;
294                                 req4->DestMac = req4->SourceMac;
295                                 req4->SourceIP = *(tIPv4*)iface->Address;;
296                                 req4->SourceMac = Adapter->MacAddr;
297                                 req4->Request = htons(2);
298                                 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
299                                         req4->SourceMac.B[0], req4->SourceMac.B[1],
300                                         req4->SourceMac.B[2], req4->SourceMac.B[3],
301                                         req4->SourceMac.B[4], req4->SourceMac.B[5]);
302                                 Link_SendPacket(Adapter, 0x0806, req4->DestMac, sizeof(tArpRequest4), req4);
303                         }
304                         break;
305                 #if ARPv6
306                 case 6:
307                         if( Length < (int)sizeof(tArpRequest6) ) {
308                                 Log_Log("ARP", "Recieved undersized packet (IPv6)");
309                                 return ;
310                         }
311                         Log_Debug("ARP", "ARP Request IPv6 Address %08x:%08x:%08x:%08x",
312                                 ntohl(req6->DestIP.L[0]), ntohl(req6->DestIP.L[1]),
313                                 ntohl(req6->DestIP.L[2]), ntohl(req6->DestIP.L[3])
314                                 );
315                         iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
316                         if( iface )
317                         {
318                                 req6->DestIP = req6->SourceIP;
319                                 req6->DestMac = req6->SourceMac;
320                                 req6->SourceIP = *(tIPv6*)iface->Address;
321                                 req6->SourceMac = Adapter->MacAddr;
322                                 req6->Request = htons(2);
323                                 Log_Debug("ARP", "Sending back us (%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                                 Link_SendPacket(Adapter, 0x0806, req6->DestMac, sizeof(tArpRequest6), req6);
328                         }
329                         break;
330                 #endif
331                 default:
332                         Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
333                         return ;
334                 }
335                 
336                 break;
337         
338         case 2: // Ooh! A response!             
339                 // Check what type of IP it is
340                 switch( req4->SWSize )
341                 {
342                 case 4:
343                         ARP_UpdateCache4( req4->SourceIP, From );
344                         break;
345                 #if ARPv6
346                 case 6:
347                         if( Length < (int)sizeof(tArpRequest6) ) {
348                                 Log_Debug("ARP", "Recieved undersized packet (IPv6)");
349                                 return ;
350                         }
351                         ARP_UpdateCache6( req6->SourceIP, From );
352                         break;
353                 #endif
354                 default:
355                         Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
356                         return ;
357                 }
358                 
359                 break;
360         
361         default:
362                 Log_Warning("ARP", "Unknown Request ID %i", ntohs(req4->Request));
363                 break;
364         }
365 }

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