3 * - Address Resolution Protocol
11 #define ARP_CACHE_SIZE 64
12 #define ARP_MAX_AGE (60*60*1000) // 1Hr
15 extern tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
17 extern tInterface *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast);
22 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address);
23 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer);
32 int giARP_Cache4Space;
41 int giARP_Cache6Space;
44 int giARP_LastUpdateID = 0;
48 * \fn int ARP_Initialise()
49 * \brief Initalise the ARP section
53 gaARP_Cache4 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
54 memset( gaARP_Cache4, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
55 giARP_Cache4Space = ARP_CACHE_SIZE;
58 gaARP_Cache6 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
59 memset( gaARP_Cache6, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
60 giARP_Cache6Space = ARP_CACHE_SIZE;
63 Link_RegisterType(0x0806, ARP_int_GetPacket);
68 * \brief Resolves a MAC address from an IPv4 address
70 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
74 struct sArpRequest4 req;
76 ENTER("pInterface xAddress", Interface, Address);
78 Mutex_Acquire( &glARP_Cache4 );
79 for( i = 0; i < giARP_Cache4Space; i++ )
81 if(gaARP_Cache4[i].IP.L != Address.L) continue;
83 // Check if the entry needs to be refreshed
84 if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE ) break;
86 Mutex_Release( &glARP_Cache4 );
87 LOG("Return %x:%x:%x:%x:%x:%x",
88 gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1],
89 gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3],
90 gaARP_Cache4[i].MAC.B[4], gaARP_Cache4[i].MAC.B[5]
93 return gaARP_Cache4[i].MAC;
95 Mutex_Release( &glARP_Cache4 );
97 lastID = giARP_LastUpdateID;
100 Log_Log("ARP4", "Asking for address %i.%i.%i.%i",
101 Address.B[0], Address.B[1], Address.B[2], Address.B[3]
103 req.HWType = htons(0x0001); // Ethernet
104 req.Type = htons(0x0800);
107 req.Request = htons(1);
108 req.SourceMac = Interface->Adapter->MacAddr;
109 req.SourceIP = Interface->IP4.Address;
110 req.DestMac = cMAC_BROADCAST;
111 req.DestIP = Address;
114 Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
119 while(lastID == giARP_LastUpdateID) Threads_Yield();
120 lastID = giARP_LastUpdateID;
122 Mutex_Acquire( &glARP_Cache4 );
123 for( i = 0; i < giARP_Cache4Space; i++ )
125 if(gaARP_Cache4[i].IP.L != Address.L) continue;
127 Mutex_Release( &glARP_Cache4 );
128 return gaARP_Cache4[i].MAC;
130 Mutex_Release( &glARP_Cache4 );
135 * \brief Updates the ARP Cache entry for an IPv4 Address
137 void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
143 // Find an entry for the IP address in the cache
144 Mutex_Acquire(&glARP_Cache4);
145 for( i = giARP_Cache4Space; i--; )
147 if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
150 if( gaARP_Cache4[i].IP.L == SWAddr.L ) break;
151 if( gaARP_Cache4[i].LastUpdate == 0 && free == -1 ) free = i;
153 // If there was no match, we need to make one
159 gaARP_Cache4[i].IP = SWAddr;
162 Log_Log("ARP4", "Caching %i.%i.%i.%i (%02x:%02x:%02x:%02x:%02x:%02x) in %i",
163 SWAddr.B[0], SWAddr.B[1], SWAddr.B[2], SWAddr.B[3],
164 HWAddr.B[0], HWAddr.B[1], HWAddr.B[2], HWAddr.B[3], HWAddr.B[4], HWAddr.B[5],
168 gaARP_Cache4[i].MAC = HWAddr;
169 gaARP_Cache4[i].LastUpdate = now();
170 giARP_LastUpdateID ++;
171 Mutex_Release(&glARP_Cache4);
176 * \brief Updates the ARP Cache entry for an IPv6 Address
178 void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
184 // Find an entry for the MAC address in the cache
185 Mutex_Acquire(&glARP_Cache6);
186 for( i = giARP_Cache6Space; i--; )
188 if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
191 if( MAC_EQU(gaARP_Cache6[i].MAC, HWAddr) ) break;
192 if( gaARP_Cache6[i].LastUpdate == 0 && free == -1 ) free = i;
194 // If there was no match, we need to make one
200 gaARP_Cache6[i].MAC = HWAddr;
203 gaARP_Cache6[i].IP = SWAddr;
204 gaARP_Cache6[i].LastUpdate = now();
205 giARP_LastUpdateID ++;
206 Mutex_Release(&glARP_Cache6);
211 * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
212 * \brief Called when an ARP packet is recieved
214 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
216 tArpRequest4 *req4 = Buffer;
218 tArpRequest6 *req6 = Buffer;
222 // Sanity Check Packet
223 if( Length < (int)sizeof(tArpRequest4) ) {
224 Log_Log("ARP", "Recieved undersized packet");
227 if( ntohs(req4->Type) != 0x0800 ) {
228 Log_Log("ARP", "Recieved a packet with a bad type (0x%x)", ntohs(req4->Type));
231 if( req4->HWSize != 6 ) {
232 Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
235 #if ARP_DETECT_SPOOFS
236 if( !MAC_EQU(req4->SourceMac, From) ) {
237 Log_Log("ARP", "ARP spoofing detected "
238 "(%02x%02x:%02x%02x:%02x%02x != %02x%02x:%02x%02x:%02x%02x)",
239 req4->SourceMac.B[0], req4->SourceMac.B[1], req4->SourceMac.B[2],
240 req4->SourceMac.B[3], req4->SourceMac.B[4], req4->SourceMac.B[5],
241 From.B[0], From.B[1], From.B[2],
242 From.B[3], From.B[4], From.B[5]
248 Log_Debug("ARP", "Request ID %i", ntohs(req4->Request));
250 switch( ntohs(req4->Request) )
252 case 1: // You want my IP?
253 Log_Debug("ARP", "ARP Request Address class %i", req4->SWSize);
254 // Check what type of IP it is
255 switch( req4->SWSize )
258 Log_Debug("ARP", "From MAC %02x:%02x:%02x:%02x:%02x:%02x",
259 req4->SourceMac.B[0], req4->SourceMac.B[1],
260 req4->SourceMac.B[2], req4->SourceMac.B[3],
261 req4->SourceMac.B[4], req4->SourceMac.B[5]);
262 //Log_Debug("ARP", "to MAC %02x:%02x:%02x:%02x:%02x:%02x",
263 // req4->DestMac.B[0], req4->DestMac.B[1],
264 // req4->DestMac.B[2], req4->DestMac.B[3],
265 // req4->DestMac.B[4], req4->DestMac.B[5]);
266 Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
267 req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
269 req4->SourceIP.B[0], req4->SourceIP.B[1],
270 req4->SourceIP.B[2], req4->SourceIP.B[3]);
271 iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
274 Log_Debug("ARP", "Caching sender's IP Address");
275 ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
277 req4->DestIP = req4->SourceIP;
278 req4->DestMac = req4->SourceMac;
279 req4->SourceIP = iface->IP4.Address;
280 req4->SourceMac = Adapter->MacAddr;
281 req4->Request = htons(2);
282 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
283 req4->SourceMac.B[0], req4->SourceMac.B[1],
284 req4->SourceMac.B[2], req4->SourceMac.B[3],
285 req4->SourceMac.B[4], req4->SourceMac.B[5]);
286 Link_SendPacket(Adapter, 0x0806, req4->DestMac, sizeof(tArpRequest4), req4);
291 if( Length < (int)sizeof(tArpRequest6) ) {
292 Log_Log("ARP", "Recieved undersized packet (IPv6)");
295 Log_Debug("ARP", "ARP Request IPv6 Address %08x:%08x:%08x:%08x",
296 ntohl(req6->DestIP.L[0]), ntohl(req6->DestIP.L[1]),
297 ntohl(req6->DestIP.L[2]), ntohl(req6->DestIP.L[3])
299 iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
302 req6->DestIP = req6->SourceIP;
303 req6->DestMac = req6->SourceMac;
304 req6->SourceIP = iface->IP6.Address;
305 req6->SourceMac = Adapter->MacAddr;
306 req6->Request = htons(2);
307 Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
308 req4->SourceMac.B[0], req4->SourceMac.B[1],
309 req4->SourceMac.B[2], req4->SourceMac.B[3],
310 req4->SourceMac.B[4], req4->SourceMac.B[5]);
311 Link_SendPacket(Adapter, 0x0806, req6->DestMac, sizeof(tArpRequest6), req6);
316 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
322 case 2: // Ooh! A response!
323 // Check what type of IP it is
324 switch( req4->SWSize )
327 ARP_UpdateCache4( req4->SourceIP, From );
331 if( Length < (int)sizeof(tArpRequest6) ) {
332 Log_Debug("ARP", "Recieved undersized packet (IPv6)");
335 ARP_UpdateCache6( req6->SourceIP, From );
339 Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
346 Log_Warning("ARP", "Unknown Request ID %i", ntohs(req4->Request));