3 * - Address Resolution Protocol
4 * - Part of the IPv4 protocol
12 #define ARP_CACHE_SIZE 64
13 #define ARP_MAX_AGE (60*60*1000) // 1Hr
16 extern tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
18 extern tInterface *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast);
23 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address);
24 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer);
33 int giARP_Cache4Space;
42 int giARP_Cache6Space;
45 int giARP_LastUpdateID = 0;
49 * \fn int ARP_Initialise()
50 * \brief Initalise the ARP section
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;
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;
64 Link_RegisterType(0x0806, ARP_int_GetPacket);
69 * \brief Resolves a MAC address from an IPv4 address
71 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
75 struct sArpRequest4 req;
78 ENTER("pInterface xAddress", Interface, Address);
80 // Check routing tables if not on this subnet
81 if( IPStack_CompareAddress(4, &Address, Interface->Address, Interface->SubnetBits) == 0 )
83 tRoute *route = IPStack_FindRoute(4, Interface, &Address);
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( route && ((tIPv4*)route->NextHop)->L != 0 )
88 // Recursion: see /Recursion/
89 return ARP_Resolve4(Interface, *(tIPv4*)route->NextHop);
94 Mutex_Acquire( &glARP_Cache4 );
95 for( i = 0; i < giARP_Cache4Space; i++ )
97 if(gaARP_Cache4[i].IP.L != Address.L) continue;
99 // Check if the entry needs to be refreshed
100 if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE ) break;
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]
109 return gaARP_Cache4[i].MAC;
111 Mutex_Release( &glARP_Cache4 );
113 lastID = giARP_LastUpdateID;
116 Log_Log("ARP4", "Asking for address %i.%i.%i.%i",
117 Address.B[0], Address.B[1], Address.B[2], Address.B[3]
119 req.HWType = htons(0x0001); // Ethernet
120 req.Type = htons(0x0800);
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;
130 Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
132 timeout = now() + Interface->TimeoutDelay;
137 while(lastID == giARP_LastUpdateID && now() < timeout) {
138 // Log_Debug("ARP", "timeout = %lli", timeout);
142 if( now() >= timeout ) break; // Timeout
144 lastID = giARP_LastUpdateID;
146 Mutex_Acquire( &glARP_Cache4 );
147 for( i = 0; i < giARP_Cache4Space; i++ )
149 if(gaARP_Cache4[i].IP.L != Address.L) continue;
151 Mutex_Release( &glARP_Cache4 );
152 return gaARP_Cache4[i].MAC;
154 Mutex_Release( &glARP_Cache4 );
157 tMacAddr ret = {{0,0,0,0,0,0}};
163 * \brief Updates the ARP Cache entry for an IPv4 Address
165 void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
171 // Find an entry for the IP address in the cache
172 Mutex_Acquire(&glARP_Cache4);
173 for( i = giARP_Cache4Space; i--; )
175 if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
178 if( gaARP_Cache4[i].IP.L == SWAddr.L ) break;
179 if( gaARP_Cache4[i].LastUpdate == 0 && free == -1 ) free = i;
181 // If there was no match, we need to make one
187 gaARP_Cache4[i].IP = SWAddr;
190 Log_Log("ARP4", "Caching %i.%i.%i.%i (%02x:%02x:%02x:%02x:%02x:%02x) in %i",
191 SWAddr.B[0], SWAddr.B[1], SWAddr.B[2], SWAddr.B[3],
192 HWAddr.B[0], HWAddr.B[1], HWAddr.B[2], HWAddr.B[3], HWAddr.B[4], HWAddr.B[5],
196 gaARP_Cache4[i].MAC = HWAddr;
197 gaARP_Cache4[i].LastUpdate = now();
198 giARP_LastUpdateID ++;
199 Mutex_Release(&glARP_Cache4);
204 * \brief Updates the ARP Cache entry for an IPv6 Address
206 void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
212 // Find an entry for the MAC address in the cache
213 Mutex_Acquire(&glARP_Cache6);
214 for( i = giARP_Cache6Space; i--; )
216 if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
219 if( MAC_EQU(gaARP_Cache6[i].MAC, HWAddr) ) break;
220 if( gaARP_Cache6[i].LastUpdate == 0 && free == -1 ) free = i;
222 // If there was no match, we need to make one
228 gaARP_Cache6[i].MAC = HWAddr;
231 gaARP_Cache6[i].IP = SWAddr;
232 gaARP_Cache6[i].LastUpdate = now();
233 giARP_LastUpdateID ++;
234 Mutex_Release(&glARP_Cache6);
239 * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
240 * \brief Called when an ARP packet is recieved
242 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
244 tArpRequest4 *req4 = Buffer;
246 tArpRequest6 *req6 = Buffer;
250 // Sanity Check Packet
251 if( Length < (int)sizeof(tArpRequest4) ) {
252 Log_Log("ARP", "Recieved undersized packet");
255 if( ntohs(req4->Type) != 0x0800 ) {
256 Log_Log("ARP", "Recieved a packet with a bad type (0x%x)", ntohs(req4->Type));
259 if( req4->HWSize != 6 ) {
260 Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
263 #if ARP_DETECT_SPOOFS
264 if( !MAC_EQU(req4->SourceMac, From) ) {
265 Log_Log("ARP", "ARP spoofing detected "
266 "(%02x%02x:%02x%02x:%02x%02x != %02x%02x:%02x%02x:%02x%02x)",
267 req4->SourceMac.B[0], req4->SourceMac.B[1], req4->SourceMac.B[2],
268 req4->SourceMac.B[3], req4->SourceMac.B[4], req4->SourceMac.B[5],
269 From.B[0], From.B[1], From.B[2],
270 From.B[3], From.B[4], From.B[5]
276 switch( ntohs(req4->Request) )
278 case 1: // You want my IP?
279 // Check what type of IP it is
280 switch( req4->SWSize )
283 Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
284 req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
286 req4->SourceIP.B[0], req4->SourceIP.B[1],
287 req4->SourceIP.B[2], req4->SourceIP.B[3]);
288 Log_Debug("ARP", " from MAC %02x:%02x:%02x:%02x:%02x:%02x",
289 req4->SourceMac.B[0], req4->SourceMac.B[1],
290 req4->SourceMac.B[2], req4->SourceMac.B[3],
291 req4->SourceMac.B[4], req4->SourceMac.B[5]);
292 iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
295 ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
297 req4->DestIP = req4->SourceIP;
298 req4->DestMac = req4->SourceMac;
299 req4->SourceIP = *(tIPv4*)iface->Address;;
300 req4->SourceMac = Adapter->MacAddr;
301 req4->Request = htons(2);
302 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
303 req4->SourceMac.B[0], req4->SourceMac.B[1],
304 req4->SourceMac.B[2], req4->SourceMac.B[3],
305 req4->SourceMac.B[4], req4->SourceMac.B[5]);
306 Link_SendPacket(Adapter, 0x0806, req4->DestMac, sizeof(tArpRequest4), req4);
311 if( Length < (int)sizeof(tArpRequest6) ) {
312 Log_Log("ARP", "Recieved undersized packet (IPv6)");
315 Log_Debug("ARP", "ARP Request IPv6 Address %08x:%08x:%08x:%08x",
316 ntohl(req6->DestIP.L[0]), ntohl(req6->DestIP.L[1]),
317 ntohl(req6->DestIP.L[2]), ntohl(req6->DestIP.L[3])
319 iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
322 req6->DestIP = req6->SourceIP;
323 req6->DestMac = req6->SourceMac;
324 req6->SourceIP = *(tIPv6*)iface->Address;
325 req6->SourceMac = Adapter->MacAddr;
326 req6->Request = htons(2);
327 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
328 req4->SourceMac.B[0], req4->SourceMac.B[1],
329 req4->SourceMac.B[2], req4->SourceMac.B[3],
330 req4->SourceMac.B[4], req4->SourceMac.B[5]);
331 Link_SendPacket(Adapter, 0x0806, req6->DestMac, sizeof(tArpRequest6), req6);
336 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
342 case 2: // Ooh! A response!
343 // Check what type of IP it is
344 switch( req4->SWSize )
347 ARP_UpdateCache4( req4->SourceIP, From );
351 if( Length < (int)sizeof(tArpRequest6) ) {
352 Log_Debug("ARP", "Recieved undersized packet (IPv6)");
355 ARP_UpdateCache6( req6->SourceIP, From );
359 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
366 Log_Warning("ARP", "Unknown Request ID %i", ntohs(req4->Request));