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);
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);
95 Mutex_Acquire( &glARP_Cache4 );
96 for( i = 0; i < giARP_Cache4Space; i++ )
98 if(gaARP_Cache4[i].IP.L != Address.L) continue;
100 // Check if the entry needs to be refreshed
101 if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE ) break;
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]
110 return gaARP_Cache4[i].MAC;
112 Mutex_Release( &glARP_Cache4 );
114 lastID = giARP_LastUpdateID;
117 Log_Log("ARP4", "Asking for address %i.%i.%i.%i",
118 Address.B[0], Address.B[1], Address.B[2], Address.B[3]
120 req.HWType = htons(0x0001); // Ethernet
121 req.Type = htons(0x0800);
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;
131 Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
133 timeout = now() + Interface->TimeoutDelay;
138 while(lastID == giARP_LastUpdateID && now() < timeout)
141 if( now() >= timeout ) break; // Timeout
143 lastID = giARP_LastUpdateID;
145 Mutex_Acquire( &glARP_Cache4 );
146 for( i = 0; i < giARP_Cache4Space; i++ )
148 if(gaARP_Cache4[i].IP.L != Address.L) continue;
150 Mutex_Release( &glARP_Cache4 );
151 return gaARP_Cache4[i].MAC;
153 Mutex_Release( &glARP_Cache4 );
156 tMacAddr ret = {{0,0,0,0,0,0}};
162 * \brief Updates the ARP Cache entry for an IPv4 Address
164 void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
170 // Find an entry for the IP address in the cache
171 Mutex_Acquire(&glARP_Cache4);
172 for( i = giARP_Cache4Space; i--; )
174 if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
177 if( gaARP_Cache4[i].IP.L == SWAddr.L ) break;
178 if( gaARP_Cache4[i].LastUpdate == 0 && free == -1 ) free = i;
180 // If there was no match, we need to make one
186 gaARP_Cache4[i].IP = SWAddr;
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],
195 gaARP_Cache4[i].MAC = HWAddr;
196 gaARP_Cache4[i].LastUpdate = now();
197 giARP_LastUpdateID ++;
198 Mutex_Release(&glARP_Cache4);
203 * \brief Updates the ARP Cache entry for an IPv6 Address
205 void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
211 // Find an entry for the MAC address in the cache
212 Mutex_Acquire(&glARP_Cache6);
213 for( i = giARP_Cache6Space; i--; )
215 if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
218 if( MAC_EQU(gaARP_Cache6[i].MAC, HWAddr) ) break;
219 if( gaARP_Cache6[i].LastUpdate == 0 && free == -1 ) free = i;
221 // If there was no match, we need to make one
227 gaARP_Cache6[i].MAC = HWAddr;
230 gaARP_Cache6[i].IP = SWAddr;
231 gaARP_Cache6[i].LastUpdate = now();
232 giARP_LastUpdateID ++;
233 Mutex_Release(&glARP_Cache6);
238 * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
239 * \brief Called when an ARP packet is recieved
241 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
243 tArpRequest4 *req4 = Buffer;
245 tArpRequest6 *req6 = Buffer;
249 // Sanity Check Packet
250 if( Length < (int)sizeof(tArpRequest4) ) {
251 Log_Log("ARP", "Recieved undersized packet");
254 if( ntohs(req4->Type) != 0x0800 ) {
255 Log_Log("ARP", "Recieved a packet with a bad type (0x%x)", ntohs(req4->Type));
258 if( req4->HWSize != 6 ) {
259 Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
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]
275 switch( ntohs(req4->Request) )
277 case 1: // You want my IP?
278 // Check what type of IP it is
279 switch( req4->SWSize )
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],
285 req4->SourceIP.B[0], req4->SourceIP.B[1],
286 req4->SourceIP.B[2], req4->SourceIP.B[3]);
287 Log_Debug("ARP", " from MAC %02x:%02x:%02x:%02x:%02x:%02x",
288 req4->SourceMac.B[0], req4->SourceMac.B[1],
289 req4->SourceMac.B[2], req4->SourceMac.B[3],
290 req4->SourceMac.B[4], req4->SourceMac.B[5]);
291 iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
294 ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
296 req4->DestIP = req4->SourceIP;
297 req4->DestMac = req4->SourceMac;
298 req4->SourceIP = *(tIPv4*)iface->Address;;
299 req4->SourceMac = Adapter->MacAddr;
300 req4->Request = htons(2);
301 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
302 req4->SourceMac.B[0], req4->SourceMac.B[1],
303 req4->SourceMac.B[2], req4->SourceMac.B[3],
304 req4->SourceMac.B[4], req4->SourceMac.B[5]);
305 Link_SendPacket(Adapter, 0x0806, req4->DestMac, sizeof(tArpRequest4), req4);
310 if( Length < (int)sizeof(tArpRequest6) ) {
311 Log_Log("ARP", "Recieved undersized packet (IPv6)");
314 Log_Debug("ARP", "ARP Request IPv6 Address %08x:%08x:%08x:%08x",
315 ntohl(req6->DestIP.L[0]), ntohl(req6->DestIP.L[1]),
316 ntohl(req6->DestIP.L[2]), ntohl(req6->DestIP.L[3])
318 iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
321 req6->DestIP = req6->SourceIP;
322 req6->DestMac = req6->SourceMac;
323 req6->SourceIP = *(tIPv6*)iface->Address;
324 req6->SourceMac = Adapter->MacAddr;
325 req6->Request = htons(2);
326 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
327 req4->SourceMac.B[0], req4->SourceMac.B[1],
328 req4->SourceMac.B[2], req4->SourceMac.B[3],
329 req4->SourceMac.B[4], req4->SourceMac.B[5]);
330 Link_SendPacket(Adapter, 0x0806, req6->DestMac, sizeof(tArpRequest6), req6);
335 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
341 case 2: // Ooh! A response!
342 // Check what type of IP it is
343 switch( req4->SWSize )
346 ARP_UpdateCache4( req4->SourceIP, From );
350 if( Length < (int)sizeof(tArpRequest6) ) {
351 Log_Debug("ARP", "Recieved undersized packet (IPv6)");
354 ARP_UpdateCache6( req6->SourceIP, From );
358 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
365 Log_Warning("ARP", "Unknown Request ID %i", ntohs(req4->Request));