3 * - Address Resolution Protocol
4 * - Part of the IPv4 protocol
10 #include "ipv4.h" // For IPv4_Netmask
13 #define ARP_CACHE_SIZE 64
14 #define ARP_MAX_AGE (60*60*1000) // 1Hr
17 extern tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
19 extern tInterface *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast);
24 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address);
25 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer);
34 int giARP_Cache4Space;
43 int giARP_Cache6Space;
46 volatile int giARP_LastUpdateID = 0;
50 * \fn int ARP_Initialise()
51 * \brief Initalise the ARP section
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;
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;
65 Link_RegisterType(0x0806, ARP_int_GetPacket);
70 * \brief Resolves a MAC address from an IPv4 address
72 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
76 struct sArpRequest4 req;
79 ENTER("pInterface xAddress", Interface, Address);
81 // Check for broadcast
86 return cMAC_BROADCAST;
89 // Check routing tables if not on this subnet
90 if( IPStack_CompareAddress(4, &Address, Interface->Address, Interface->SubnetBits) == 0 )
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 )
97 // Recursion: see /Recursion/
98 LOG("Recursing with %s", IPStack_PrintAddress(4, route->NextHop));
100 return ARP_Resolve4(Interface, *(tIPv4*)route->NextHop);
102 // No route, fall though
107 // Check for broadcast
108 netmask = IPv4_Netmask(Interface->SubnetBits);
109 if( (Address.L & ~netmask) == (0xFFFFFFFF & ~netmask) )
111 LOG("Local Broadcast");
113 return cMAC_BROADCAST;
118 Mutex_Acquire( &glARP_Cache4 );
119 for( i = 0; i < giARP_Cache4Space; i++ )
121 if(gaARP_Cache4[i].IP.L != Address.L) continue;
123 // Check if the entry needs to be refreshed
124 if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE ) break;
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]
133 return gaARP_Cache4[i].MAC;
135 Mutex_Release( &glARP_Cache4 );
137 lastID = giARP_LastUpdateID;
140 Log_Log("ARP4", "Asking for address %i.%i.%i.%i",
141 Address.B[0], Address.B[1], Address.B[2], Address.B[3]
143 req.HWType = htons(0x0001); // Ethernet
144 req.Type = htons(0x0800);
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;
154 Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
156 timeout = now() + Interface->TimeoutDelay;
161 while(lastID == giARP_LastUpdateID && now() < timeout) {
165 if( now() >= timeout ) {
166 Log_Log("ARP4", "Timeout");
170 lastID = giARP_LastUpdateID;
172 Mutex_Acquire( &glARP_Cache4 );
173 for( i = 0; i < giARP_Cache4Space; i++ )
175 if(gaARP_Cache4[i].IP.L != Address.L) continue;
177 Mutex_Release( &glARP_Cache4 );
178 Log_Debug("ARP4", "Return %02x:%02x:%02x:%02x:%02x:%02x",
179 gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1],
180 gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3],
181 gaARP_Cache4[i].MAC.B[4], gaARP_Cache4[i].MAC.B[5]);
182 return gaARP_Cache4[i].MAC;
184 Mutex_Release( &glARP_Cache4 );
187 tMacAddr ret = {{0,0,0,0,0,0}};
193 * \brief Updates the ARP Cache entry for an IPv4 Address
195 void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
201 // Find an entry for the IP address in the cache
202 Mutex_Acquire(&glARP_Cache4);
203 for( i = giARP_Cache4Space; i--; )
205 if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
208 if( gaARP_Cache4[i].IP.L == SWAddr.L ) break;
209 if( gaARP_Cache4[i].LastUpdate == 0 && free == -1 ) free = i;
211 // If there was no match, we need to make one
219 Log_Log("ARP4", "Caching %i.%i.%i.%i (%02x:%02x:%02x:%02x:%02x:%02x) in %i",
220 SWAddr.B[0], SWAddr.B[1], SWAddr.B[2], SWAddr.B[3],
221 HWAddr.B[0], HWAddr.B[1], HWAddr.B[2], HWAddr.B[3], HWAddr.B[4], HWAddr.B[5],
225 gaARP_Cache4[i].IP = SWAddr;
226 gaARP_Cache4[i].MAC = HWAddr;
227 gaARP_Cache4[i].LastUpdate = now();
228 giARP_LastUpdateID ++;
229 Mutex_Release(&glARP_Cache4);
234 * \brief Updates the ARP Cache entry for an IPv6 Address
236 void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
242 // Find an entry for the MAC address in the cache
243 Mutex_Acquire(&glARP_Cache6);
244 for( i = giARP_Cache6Space; i--; )
246 if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
249 if( MAC_EQU(gaARP_Cache6[i].MAC, HWAddr) ) break;
250 if( gaARP_Cache6[i].LastUpdate == 0 && free == -1 ) free = i;
252 // If there was no match, we need to make one
258 gaARP_Cache6[i].MAC = HWAddr;
261 gaARP_Cache6[i].IP = SWAddr;
262 gaARP_Cache6[i].LastUpdate = now();
263 giARP_LastUpdateID ++;
264 Mutex_Release(&glARP_Cache6);
269 * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
270 * \brief Called when an ARP packet is recieved
272 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
274 tArpRequest4 *req4 = Buffer;
276 tArpRequest6 *req6 = Buffer;
280 // Sanity Check Packet
281 if( Length < (int)sizeof(tArpRequest4) ) {
282 Log_Log("ARP", "Recieved undersized packet");
285 if( ntohs(req4->Type) != 0x0800 ) {
286 Log_Log("ARP", "Recieved a packet with a bad type (0x%x)", ntohs(req4->Type));
289 if( req4->HWSize != 6 ) {
290 Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
293 #if ARP_DETECT_SPOOFS
294 if( !MAC_EQU(req4->SourceMac, From) ) {
295 Log_Log("ARP", "ARP spoofing detected "
296 "(%02x%02x:%02x%02x:%02x%02x != %02x%02x:%02x%02x:%02x%02x)",
297 req4->SourceMac.B[0], req4->SourceMac.B[1], req4->SourceMac.B[2],
298 req4->SourceMac.B[3], req4->SourceMac.B[4], req4->SourceMac.B[5],
299 From.B[0], From.B[1], From.B[2],
300 From.B[3], From.B[4], From.B[5]
306 switch( ntohs(req4->Request) )
308 case 1: // You want my IP?
309 // Check what type of IP it is
310 switch( req4->SWSize )
313 Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
314 req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
316 req4->SourceIP.B[0], req4->SourceIP.B[1],
317 req4->SourceIP.B[2], req4->SourceIP.B[3]);
318 Log_Debug("ARP", " from MAC %02x:%02x:%02x:%02x:%02x:%02x",
319 req4->SourceMac.B[0], req4->SourceMac.B[1],
320 req4->SourceMac.B[2], req4->SourceMac.B[3],
321 req4->SourceMac.B[4], req4->SourceMac.B[5]);
322 iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
325 ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
327 req4->DestIP = req4->SourceIP;
328 req4->DestMac = req4->SourceMac;
329 req4->SourceIP = *(tIPv4*)iface->Address;;
330 req4->SourceMac = Adapter->MacAddr;
331 req4->Request = htons(2);
332 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
333 req4->SourceMac.B[0], req4->SourceMac.B[1],
334 req4->SourceMac.B[2], req4->SourceMac.B[3],
335 req4->SourceMac.B[4], req4->SourceMac.B[5]);
336 Link_SendPacket(Adapter, 0x0806, req4->DestMac, sizeof(tArpRequest4), req4);
341 if( Length < (int)sizeof(tArpRequest6) ) {
342 Log_Log("ARP", "Recieved undersized packet (IPv6)");
345 Log_Debug("ARP", "ARP Request IPv6 Address %08x:%08x:%08x:%08x",
346 ntohl(req6->DestIP.L[0]), ntohl(req6->DestIP.L[1]),
347 ntohl(req6->DestIP.L[2]), ntohl(req6->DestIP.L[3])
349 iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
352 req6->DestIP = req6->SourceIP;
353 req6->DestMac = req6->SourceMac;
354 req6->SourceIP = *(tIPv6*)iface->Address;
355 req6->SourceMac = Adapter->MacAddr;
356 req6->Request = htons(2);
357 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
358 req4->SourceMac.B[0], req4->SourceMac.B[1],
359 req4->SourceMac.B[2], req4->SourceMac.B[3],
360 req4->SourceMac.B[4], req4->SourceMac.B[5]);
361 Link_SendPacket(Adapter, 0x0806, req6->DestMac, sizeof(tArpRequest6), req6);
366 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
372 case 2: // Ooh! A response!
373 // Check what type of IP it is
374 switch( req4->SWSize )
377 ARP_UpdateCache4( req4->SourceIP, From );
381 if( Length < (int)sizeof(tArpRequest6) ) {
382 Log_Debug("ARP", "Recieved undersized packet (IPv6)");
385 ARP_UpdateCache6( req6->SourceIP, From );
389 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
396 Log_Warning("ARP", "Unknown Request ID %i", ntohs(req4->Request));