Kernel - Update VFS API to use off_t/size_t instead of Uint64
[tpg/acess2.git] / KernelLand / Modules / IPStack / arp.c
1 /*
2  * Acess2 IP Stack
3  * - Address Resolution Protocol
4  * - Part of the IPv4 protocol
5  */
6 #define DEBUG   0
7 #include "ipstack.h"
8 #include "arp.h"
9 #include "link.h"
10 #include "ipv4.h"       // For IPv4_Netmask
11
12 #define ARPv6   0
13 #define ARP_CACHE_SIZE  64
14 #define ARP_MAX_AGE             (60*60*1000)    // 1Hr
15
16 // === IMPORTS ===
17 extern tInterface       *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
18 #if ARPv6
19 extern tInterface       *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast);
20 #endif
21
22 // === PROTOTYPES ===
23  int    ARP_Initialise();
24 tMacAddr        ARP_Resolve4(tInterface *Interface, tIPv4 Address);
25 void    ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer);
26
27 // === GLOBALS ===
28 struct sARP_Cache4 {
29         tIPv4   IP;
30         tMacAddr        MAC;
31         Sint64  LastUpdate;
32         Sint64  LastUsed;
33 }       *gaARP_Cache4;
34  int    giARP_Cache4Space;
35 tMutex  glARP_Cache4;
36 #if ARPv6
37 struct sARP_Cache6 {
38         tIPv6   IP;
39         tMacAddr        MAC;
40         Sint64  LastUpdate;
41         Sint64  LastUsed;
42 }       *gaARP_Cache6;
43  int    giARP_Cache6Space;
44 tMutex  glARP_Cache6;
45 #endif
46 volatile int    giARP_LastUpdateID = 0;
47
48 // === CODE ===
49 /**
50  * \fn int ARP_Initialise()
51  * \brief Initalise the ARP section
52  */
53 int ARP_Initialise()
54 {
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;
58         
59         #if ARPv6
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;
63         #endif
64         
65         Link_RegisterType(0x0806, ARP_int_GetPacket);
66         return 1;
67 }
68
69 /**
70  * \brief Resolves a MAC address from an IPv4 address
71  */
72 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
73 {
74          int    lastID;
75          int    i;
76         struct sArpRequest4     req;
77         Sint64  timeout;
78         
79         ENTER("pInterface xAddress", Interface, Address);
80         
81         // Check for broadcast
82         if( Address.L == -1 )
83         {
84                 LOG("Broadcast");
85                 LEAVE('-');
86                 return cMAC_BROADCAST;
87         }
88
89         // Check routing tables if not on this subnet
90         if( IPStack_CompareAddress(4, &Address, Interface->Address, Interface->SubnetBits) == 0 )
91         {
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 )
96                 {
97                         // Recursion: see /Recursion/
98                         LOG("Recursing with %s", IPStack_PrintAddress(4, route->NextHop));
99                         LEAVE('-');
100                         return ARP_Resolve4(Interface, *(tIPv4*)route->NextHop);
101                 }
102                 // No route, fall though
103         }
104         else
105         {
106                 Uint32  netmask;
107                 // Check for broadcast
108                 netmask = IPv4_Netmask(Interface->SubnetBits);
109                 if( (Address.L & ~netmask) == (0xFFFFFFFF & ~netmask) )
110                 {
111                         LOG("Local Broadcast");
112                         LEAVE('-');
113                         return cMAC_BROADCAST;
114                 }
115         }
116         
117         // Check ARP Cache
118         Mutex_Acquire( &glARP_Cache4 );
119         for( i = 0; i < giARP_Cache4Space; i++ )
120         {
121                 if(gaARP_Cache4[i].IP.L != Address.L)   continue;
122                 
123                 // Check if the entry needs to be refreshed
124                 if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE )  break;
125                 
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]
131                         );
132                 LEAVE('-');
133                 return gaARP_Cache4[i].MAC;
134         }
135         Mutex_Release( &glARP_Cache4 );
136         
137         lastID = giARP_LastUpdateID;
138         
139         // Create request
140         Log_Log("ARP4", "Asking for address %i.%i.%i.%i",
141                 Address.B[0], Address.B[1], Address.B[2], Address.B[3]
142                 );
143         req.HWType = htons(0x0001);     // Ethernet
144         req.Type   = htons(0x0800);
145         req.HWSize = 6;
146         req.SWSize = 4;
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;
152         
153         // Send Request
154         Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
155         
156         timeout = now() + Interface->TimeoutDelay;
157         
158         // Wait for a reply
159         for(;;)
160         {
161                 while(lastID == giARP_LastUpdateID && now() < timeout) {
162                         Threads_Yield();
163                 }
164                 
165                 if( now() >= timeout ) {
166                         Log_Log("ARP4", "Timeout");
167                         break;  // Timeout
168                 }
169                 
170                 lastID = giARP_LastUpdateID;
171                 
172                 Mutex_Acquire( &glARP_Cache4 );
173                 for( i = 0; i < giARP_Cache4Space; i++ )
174                 {
175                         if(gaARP_Cache4[i].IP.L != Address.L)   continue;
176                         
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;
183                 }
184                 Mutex_Release( &glARP_Cache4 );
185         }
186         {
187                 tMacAddr        ret = {{0,0,0,0,0,0}};
188                 return ret;
189         }
190 }
191
192 /**
193  * \brief Updates the ARP Cache entry for an IPv4 Address
194  */
195 void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
196 {
197          int    i;
198          int    free = -1;
199          int    oldest = 0;
200         
201         // Find an entry for the IP address in the cache
202         Mutex_Acquire(&glARP_Cache4);
203         for( i = giARP_Cache4Space; i--; )
204         {
205                 if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
206                         oldest = i;
207                 }
208                 if( gaARP_Cache4[i].IP.L == SWAddr.L )  break;
209                 if( gaARP_Cache4[i].LastUpdate == 0 && free == -1 )     free = i;
210         }
211         // If there was no match, we need to make one
212         if(i == -1) {
213                 if(free != -1)
214                         i = free;
215                 else
216                         i = oldest;
217         }
218         
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],
222                 i
223                 );
224                 
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);
230 }
231
232 #if ARPv6
233 /**
234  * \brief Updates the ARP Cache entry for an IPv6 Address
235  */
236 void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
237 {
238          int    i;
239          int    free = -1;
240          int    oldest = 0;
241         
242         // Find an entry for the MAC address in the cache
243         Mutex_Acquire(&glARP_Cache6);
244         for( i = giARP_Cache6Space; i--; )
245         {
246                 if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
247                         oldest = i;
248                 }
249                 if( MAC_EQU(gaARP_Cache6[i].MAC, HWAddr) )      break;
250                 if( gaARP_Cache6[i].LastUpdate == 0 && free == -1 )     free = i;
251         }
252         // If there was no match, we need to make one
253         if(i == -1) {
254                 if(free != -1)
255                         i = free;
256                 else
257                         i = oldest;
258                 gaARP_Cache6[i].MAC = HWAddr;
259         }
260         
261         gaARP_Cache6[i].IP = SWAddr;
262         gaARP_Cache6[i].LastUpdate = now();
263         giARP_LastUpdateID ++;
264         Mutex_Release(&glARP_Cache6);
265 }
266 #endif
267
268 /**
269  * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
270  * \brief Called when an ARP packet is recieved
271  */
272 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
273 {
274         tArpRequest4    *req4 = Buffer;
275         #if ARPv6
276         tArpRequest6    *req6 = Buffer;
277         #endif
278         tInterface      *iface;
279         
280         // Sanity Check Packet
281         if( Length < (int)sizeof(tArpRequest4) ) {
282                 Log_Log("ARP", "Recieved undersized packet");
283                 return ;
284         }
285         if( ntohs(req4->Type) != 0x0800 ) {
286                 Log_Log("ARP", "Recieved a packet with a bad type (0x%x)", ntohs(req4->Type));
287                 return ;
288         }
289         if( req4->HWSize != 6 ) {
290                 Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
291                 return;
292         }
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]
301                         );
302                 return;
303         }
304         #endif
305         
306         switch( ntohs(req4->Request) )
307         {
308         case 1: // You want my IP?
309                 // Check what type of IP it is
310                 switch( req4->SWSize )
311                 {
312                 case 4:
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],
315                                 req4->DestIP.B[3],
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);
323                         if( iface )
324                         {
325                                 ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
326                                 
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);
337                         }
338                         break;
339                 #if ARPv6
340                 case 6:
341                         if( Length < (int)sizeof(tArpRequest6) ) {
342                                 Log_Log("ARP", "Recieved undersized packet (IPv6)");
343                                 return ;
344                         }
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])
348                                 );
349                         iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
350                         if( iface )
351                         {
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);
362                         }
363                         break;
364                 #endif
365                 default:
366                         Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
367                         return ;
368                 }
369                 
370                 break;
371         
372         case 2: // Ooh! A response!             
373                 // Check what type of IP it is
374                 switch( req4->SWSize )
375                 {
376                 case 4:
377                         ARP_UpdateCache4( req4->SourceIP, From );
378                         break;
379                 #if ARPv6
380                 case 6:
381                         if( Length < (int)sizeof(tArpRequest6) ) {
382                                 Log_Debug("ARP", "Recieved undersized packet (IPv6)");
383                                 return ;
384                         }
385                         ARP_UpdateCache6( req6->SourceIP, From );
386                         break;
387                 #endif
388                 default:
389                         Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
390                         return ;
391                 }
392                 
393                 break;
394         
395         default:
396                 Log_Warning("ARP", "Unknown Request ID %i", ntohs(req4->Request));
397                 break;
398         }
399 }

UCC git Repository :: git.ucc.asn.au