3 * - Address Resolution Protocol
9 #define ARP_CACHE_SIZE 64
10 #define ARP_MAX_AGE (60*60*1000) // 1Hr
13 extern tInterface *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
14 extern tInterface *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast);
18 int ARP_int_Resolve4(tInterface *Interface, tIPv4 Address);
19 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer);
30 int giARP_LastUpdateID = 0;
31 tSpinlock glARP_Cache;
35 * \fn int ARP_Initialise()
36 * \brief Initalise the ARP section
40 gaARP_Cache = malloc( ARP_CACHE_SIZE * sizeof(*gaARP_Cache) );
41 memset( gaARP_Cache, 0, ARP_CACHE_SIZE * sizeof(*gaARP_Cache) );
42 giARP_CacheSpace = ARP_CACHE_SIZE;
44 Link_RegisterType(0x0806, ARP_int_GetPacket);
49 * \brief Resolves a MAC address from an IPv4 address
51 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
57 for( i = 0; i < giARP_CacheSpace; i++ )
59 if(gaARP_Cache[i].IP4.L != Address.L) continue;
61 // Check if the entry needs to be refreshed
62 if( now() - gaARP_Cache[i].LastUpdate > ARP_MAX_AGE ) break;
64 RELEASE( &glARP_Cache );
65 return gaARP_Cache[i].MAC;
67 RELEASE( &glARP_Cache );
69 lastID = giARP_LastUpdateID;
70 ARP_int_Resolve4(Interface, Address);
73 while(lastID == giARP_LastUpdateID) Threads_Yield();
76 for( i = 0; i < giARP_CacheSpace; i++ )
78 if(gaARP_Cache[i].IP4.L != Address.L) continue;
80 RELEASE( &glARP_Cache );
81 return gaARP_Cache[i].MAC;
83 RELEASE( &glARP_Cache );
88 * \fn int ARP_int_Resolve4(tInterface *Interface, tIPv4 Address)
89 * \brief Request the network to resolve an IPv4 Address
90 * \return Boolean Success
92 int ARP_int_Resolve4(tInterface *Interface, tIPv4 Address)
94 struct sArpRequest4 req;
96 req.HWType = htons(0x100); // Ethernet
97 req.Type = htons(0x0800);
100 req.Request = htons(1);
101 req.SourceMac = Interface->Adapter->MacAddr;
102 req.SourceIP = Interface->IP4.Address;
103 req.DestMac = cMAC_BROADCAST;
104 req.DestIP = Address;
106 Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
112 * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
113 * \brief Called when an ARP packet is recieved
115 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
119 tArpRequest4 *req4 = Buffer;
120 tArpRequest6 *req6 = Buffer;
123 // Sanity Check Packet
124 if( Length < sizeof(tArpRequest4) ) {
125 Log("[ARP ] Recieved undersized packet");
128 if( ntohs(req4->Type) != 0x0800 ) {
129 Log("[ARP ] Recieved a packet with a bad type 0x%x", ntohs(req4->Type));
132 if( req4->HWSize != 6 ) {
133 Log("[ARP ] Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
136 if( !MAC_EQU(req4->SourceMac, From) ) {
137 Log("[ARP ] ARP spoofing detected", req4->HWSize);
141 Log("[ARP ] Request ID %i", ntohs(req4->Request));
143 switch( ntohs(req4->Request) )
145 case 1: // You want my IP?
146 Log("[ARP ] ARP Request Address class %i", req4->SWSize);
147 // Check what type of IP it is
148 switch( req4->SWSize )
151 Log("[ARP ] From MAC %02x:%02x:%02x:%02x:%02x:%02x",
152 req4->SourceMac.B[0], req4->SourceMac.B[1],
153 req4->SourceMac.B[2], req4->SourceMac.B[3],
154 req4->SourceMac.B[4], req4->SourceMac.B[5]);
155 Log("[ARP ] to MAC %02x:%02x:%02x:%02x:%02x:%02x",
156 req4->DestMac.B[0], req4->DestMac.B[1],
157 req4->DestMac.B[2], req4->DestMac.B[3],
158 req4->DestMac.B[4], req4->DestMac.B[5]);
159 Log("[ARP ] ARP Request IPv4 Address %i.%i.%i.%i",
160 req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
162 Log("[ARP ] from %i.%i.%i.%i",
163 req4->SourceIP.B[0], req4->SourceIP.B[1],
164 req4->SourceIP.B[2], req4->SourceIP.B[3]);
165 iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
168 req4->DestIP = req4->SourceIP;
169 req4->DestMac = req4->SourceMac;
170 req4->SourceIP = iface->IP4.Address;
171 req4->SourceMac = Adapter->MacAddr;
172 req4->Request = htons(2);
173 Log("[ARP ] Hey, That's us!");
174 Log("[ARP ] Sending back %02x:%02x:%02x:%02x:%02x:%02x",
175 req4->SourceMac.B[0], req4->SourceMac.B[1],
176 req4->SourceMac.B[2], req4->SourceMac.B[3],
177 req4->SourceMac.B[4], req4->SourceMac.B[5]);
178 Link_SendPacket(Adapter, 0x0806, req4->DestMac, sizeof(tArpRequest4), req4);
182 if( Length < sizeof(tArpRequest6) ) {
183 Log("[ARP ] Recieved undersized packet (IPv6)");
186 iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
189 req6->DestIP = req6->SourceIP;
190 req6->DestMac = req6->SourceMac;
191 req6->SourceIP = iface->IP6.Address;
192 req6->SourceMac = Adapter->MacAddr;
193 req6->Request = htons(2);
194 Link_SendPacket(Adapter, 0x0806, req6->DestMac, sizeof(tArpRequest6), req6);
198 Log("[ARP ] Unknown Protocol Address size (%i)", req4->SWSize);
204 case 2: // Ooh! A response!
205 // Find an entry for the MAC address in the cache
207 for( i = giARP_CacheSpace; i--; )
209 if(gaARP_Cache[oldest].LastUpdate > gaARP_Cache[i].LastUpdate) {
212 if( MAC_EQU(gaARP_Cache[i].MAC, From) ) break;
213 if( gaARP_Cache[i].LastUpdate == 0 && free==-1 ) free = i;
222 // Check what type of IP it is
223 switch( req4->SWSize )
226 gaARP_Cache[i].IP4 = req4->SourceIP;
229 if( Length < sizeof(tArpRequest6) ) {
230 Log("[ARP ] Recieved undersized packet (IPv6)");
233 gaARP_Cache[i].IP6 = req6->SourceIP;
236 Log("[ARP ] Unknown Protocol Address size (%i)", req4->SWSize);
237 RELEASE(&glARP_Cache);
241 gaARP_Cache[i].LastUpdate = now();
242 RELEASE(&glARP_Cache);