More work on the IP Stack, now responds to pings
[tpg/acess2.git] / Modules / IPStack / arp.c
1 /*
2  * Acess2 IP Stack
3  * - Address Resolution Protocol
4  */
5 #define DEBUG   0
6 #include "ipstack.h"
7 #include "arp.h"
8 #include "link.h"
9
10 #define ARP_CACHE_SIZE  64
11 #define ARP_MAX_AGE             (60*60*1000)    // 1Hr
12
13 // === IMPORTS ===
14 extern tInterface       *IPv4_GetInterface(tAdapter *Adapter, tIPv4 Address, int Broadcast);
15 extern tInterface       *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast);
16
17 // === PROTOTYPES ===
18  int    ARP_Initialise();
19  int    ARP_int_Resolve4(tInterface *Interface, tIPv4 Address);
20 void    ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer);
21
22 // === GLOBALS ===
23 struct sARP_Cache4 {
24         tIPv4   IP;
25         tMacAddr        MAC;
26         Sint64  LastUpdate;
27         Sint64  LastUsed;
28 }       *gaARP_Cache4;
29  int    giARP_Cache4Space;
30 tSpinlock       glARP_Cache4;
31 struct sARP_Cache6 {
32         tIPv6   IP;
33         tMacAddr        MAC;
34         Sint64  LastUpdate;
35         Sint64  LastUsed;
36 }       *gaARP_Cache6;
37  int    giARP_Cache6Space;
38 tSpinlock       glARP_Cache6;
39  int    giARP_LastUpdateID = 0;
40
41 // === CODE ===
42 /**
43  * \fn int ARP_Initialise()
44  * \brief Initalise the ARP section
45  */
46 int ARP_Initialise()
47 {
48         gaARP_Cache4 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
49         memset( gaARP_Cache4, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache4) );
50         giARP_Cache4Space = ARP_CACHE_SIZE;
51         
52         gaARP_Cache6 = malloc( ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
53         memset( gaARP_Cache6, 0, ARP_CACHE_SIZE * sizeof(struct sARP_Cache6) );
54         giARP_Cache6Space = ARP_CACHE_SIZE;
55         
56         Link_RegisterType(0x0806, ARP_int_GetPacket);
57         return 1;
58 }
59
60 /**
61  * \brief Resolves a MAC address from an IPv4 address
62  */
63 tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
64 {
65          int    lastID;
66          int    i;
67         
68         ENTER("pInterface xAddress", Interface, Address);
69         
70         LOCK( &glARP_Cache4 );
71         for( i = 0; i < giARP_Cache4Space; i++ )
72         {
73                 if(gaARP_Cache4[i].IP.L != Address.L)   continue;
74                 
75                 // Check if the entry needs to be refreshed
76                 if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE )  break;
77                 
78                 RELEASE( &glARP_Cache4 );
79                 LOG("Return %x:%x:%x:%x:%x:%x",
80                         gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1],
81                         gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3],
82                         gaARP_Cache4[i].MAC.B[4], gaARP_Cache4[i].MAC.B[5]
83                         );
84                 LEAVE('-');
85                 return gaARP_Cache4[i].MAC;
86         }
87         RELEASE( &glARP_Cache4 );
88         
89         lastID = giARP_LastUpdateID;
90         ARP_int_Resolve4(Interface, Address);
91         for(;;)
92         {
93                 while(lastID == giARP_LastUpdateID)     Threads_Yield();
94                 lastID = giARP_LastUpdateID;
95                 
96                 LOCK( &glARP_Cache4 );
97                 for( i = 0; i < giARP_Cache4Space; i++ )
98                 {
99                         if(gaARP_Cache4[i].IP.L != Address.L)   continue;
100                         
101                         RELEASE( &glARP_Cache4 );
102                         return gaARP_Cache4[i].MAC;
103                 }
104                 RELEASE( &glARP_Cache4 );
105         }
106 }
107
108 /**
109  * \fn int ARP_int_Resolve4(tInterface *Interface, tIPv4 Address)
110  * \brief Request the network to resolve an IPv4 Address
111  * \return Boolean Success
112  */
113 int ARP_int_Resolve4(tInterface *Interface, tIPv4 Address)
114 {
115         struct sArpRequest4     req;
116         
117         Log("[ARP4 ] Asking for address %i.%i.%i.%i",
118                 Address.B[0], Address.B[1], Address.B[2], Address.B[3]
119                 );
120         req.HWType = htons(0x100);      // Ethernet
121         req.Type   = htons(0x0800);
122         req.HWSize = 6;
123         req.SWSize = 4;
124         req.Request = htons(1);
125         req.SourceMac = Interface->Adapter->MacAddr;
126         req.SourceIP = Interface->IP4.Address;
127         req.DestMac = cMAC_BROADCAST;
128         req.DestIP = Address;
129         
130         Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
131         
132         return 0;
133 }
134
135 /**
136  * \brief Updates the ARP Cache entry for an IPv4 Address
137  */
138 void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
139 {
140          int    i;
141          int    free = -1;
142          int    oldest = 0;
143         
144         // Find an entry for the IP address in the cache
145         LOCK(&glARP_Cache4);
146         for( i = giARP_Cache4Space; i--; )
147         {
148                 if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
149                         oldest = i;
150                 }
151                 if( gaARP_Cache4[i].IP.L == SWAddr.L )  break;
152                 if( gaARP_Cache4[i].LastUpdate == 0 && free == -1 )     free = i;
153         }
154         // If there was no match, we need to make one
155         if(i == -1) {
156                 if(free != -1)
157                         i = free;
158                 else
159                         i = oldest;
160                 gaARP_Cache4[i].IP = SWAddr;
161         }
162         
163         gaARP_Cache4[i].MAC = HWAddr;
164         gaARP_Cache4[i].LastUpdate = now();
165         RELEASE(&glARP_Cache4);
166 }
167
168 /**
169  * \brief Updates the ARP Cache entry for an IPv6 Address
170  */
171 void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
172 {
173          int    i;
174          int    free = -1;
175          int    oldest = 0;
176         
177         // Find an entry for the MAC address in the cache
178         LOCK(&glARP_Cache6);
179         for( i = giARP_Cache6Space; i--; )
180         {
181                 if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
182                         oldest = i;
183                 }
184                 if( MAC_EQU(gaARP_Cache6[i].MAC, HWAddr) )      break;
185                 if( gaARP_Cache6[i].LastUpdate == 0 && free == -1 )     free = i;
186         }
187         // If there was no match, we need to make one
188         if(i == -1) {
189                 if(free != -1)
190                         i = free;
191                 else
192                         i = oldest;
193                 gaARP_Cache6[i].MAC = HWAddr;
194         }
195         
196         gaARP_Cache6[i].IP = SWAddr;
197         gaARP_Cache6[i].LastUpdate = now();
198         RELEASE(&glARP_Cache6);
199 }
200
201 /**
202  * \fn void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
203  * \brief Called when an ARP packet is recieved
204  */
205 void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer)
206 {
207         tArpRequest4    *req4 = Buffer;
208         tArpRequest6    *req6 = Buffer;
209         tInterface      *iface;
210         
211         // Sanity Check Packet
212         if( Length < sizeof(tArpRequest4) ) {
213                 Log("[ARP  ] Recieved undersized packet");
214                 return ;
215         }
216         if( ntohs(req4->Type) != 0x0800 ) {
217                 Log("[ARP  ] Recieved a packet with a bad type 0x%x", ntohs(req4->Type));
218                 return ;
219         }
220         if( req4->HWSize != 6 ) {
221                 Log("[ARP  ] Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
222                 return;
223         }
224         if( !MAC_EQU(req4->SourceMac, From) ) {
225                 Log("[ARP  ] ARP spoofing detected", req4->HWSize);
226                 return;
227         }
228         
229         Log("[ARP  ] Request ID %i", ntohs(req4->Request));
230         
231         switch( ntohs(req4->Request) )
232         {
233         case 1: // You want my IP?
234                 Log("[ARP  ] ARP Request Address class %i", req4->SWSize);
235                 // Check what type of IP it is
236                 switch( req4->SWSize )
237                 {
238                 case 4:
239                         Log("[ARP  ] From MAC %02x:%02x:%02x:%02x:%02x:%02x",
240                                 req4->SourceMac.B[0], req4->SourceMac.B[1],
241                                 req4->SourceMac.B[2], req4->SourceMac.B[3],
242                                 req4->SourceMac.B[4], req4->SourceMac.B[5]);
243                         Log("[ARP  ] to MAC %02x:%02x:%02x:%02x:%02x:%02x",
244                                 req4->DestMac.B[0], req4->DestMac.B[1],
245                                 req4->DestMac.B[2], req4->DestMac.B[3],
246                                 req4->DestMac.B[4], req4->DestMac.B[5]);
247                         Log("[ARP  ] ARP Request IPv4 Address %i.%i.%i.%i",
248                                 req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
249                                 req4->DestIP.B[3]);
250                         Log("[ARP  ] from %i.%i.%i.%i",
251                                 req4->SourceIP.B[0], req4->SourceIP.B[1],
252                                 req4->SourceIP.B[2], req4->SourceIP.B[3]);
253                         iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
254                         if( iface )
255                         {
256                                 Log("[ARP  ] Caching sender's IP Address");
257                                 ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
258                                 
259                                 req4->DestIP = req4->SourceIP;
260                                 req4->DestMac = req4->SourceMac;
261                                 req4->SourceIP = iface->IP4.Address;
262                                 req4->SourceMac = Adapter->MacAddr;
263                                 req4->Request = htons(2);
264                                 Log("[ARP  ] Hey, That's us!");
265                                 Log("[ARP  ] Sending back %02x:%02x:%02x:%02x:%02x:%02x",
266                                         req4->SourceMac.B[0], req4->SourceMac.B[1],
267                                         req4->SourceMac.B[2], req4->SourceMac.B[3],
268                                         req4->SourceMac.B[4], req4->SourceMac.B[5]);
269                                 Link_SendPacket(Adapter, 0x0806, req4->DestMac, sizeof(tArpRequest4), req4);
270                         }
271                         break;
272                 case 6:
273                         if( Length < sizeof(tArpRequest6) ) {
274                                 Log("[ARP  ] Recieved undersized packet (IPv6)");
275                                 return ;
276                         }
277                         iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
278                         if( iface )
279                         {
280                                 req6->DestIP = req6->SourceIP;
281                                 req6->DestMac = req6->SourceMac;
282                                 req6->SourceIP = iface->IP6.Address;
283                                 req6->SourceMac = Adapter->MacAddr;
284                                 req6->Request = htons(2);
285                                 Link_SendPacket(Adapter, 0x0806, req6->DestMac, sizeof(tArpRequest6), req6);
286                         }
287                         break;
288                 default:
289                         Log("[ARP  ] Unknown Protocol Address size (%i)", req4->SWSize);
290                         return ;
291                 }
292                 
293                 break;
294         
295         case 2: // Ooh! A response!             
296                 // Check what type of IP it is
297                 switch( req4->SWSize )
298                 {
299                 case 4:
300                         ARP_UpdateCache4( req4->SourceIP, From );
301                         break;
302                 case 6:
303                         if( Length < sizeof(tArpRequest6) ) {
304                                 Log("[ARP  ] Recieved undersized packet (IPv6)");
305                                 return ;
306                         }
307                         ARP_UpdateCache6( req6->SourceIP, From );
308                         break;
309                 default:
310                         Log("[ARP  ] Unknown Protocol Address size (%i)", req4->SWSize);
311                         return ;
312                 }
313                 
314                 break;
315         }
316 }

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