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

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