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

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