More fixups to ARP and UDP (udp still buggy)
[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 tMacAddr        ARP_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         struct sArpRequest4     req;
68         
69         ENTER("pInterface xAddress", Interface, Address);
70         
71         LOCK( &glARP_Cache4 );
72         for( i = 0; i < giARP_Cache4Space; i++ )
73         {
74                 if(gaARP_Cache4[i].IP.L != Address.L)   continue;
75                 
76                 // Check if the entry needs to be refreshed
77                 if( now() - gaARP_Cache4[i].LastUpdate > ARP_MAX_AGE )  break;
78                 
79                 RELEASE( &glARP_Cache4 );
80                 LOG("Return %x:%x:%x:%x:%x:%x",
81                         gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1],
82                         gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3],
83                         gaARP_Cache4[i].MAC.B[4], gaARP_Cache4[i].MAC.B[5]
84                         );
85                 LEAVE('-');
86                 return gaARP_Cache4[i].MAC;
87         }
88         RELEASE( &glARP_Cache4 );
89         
90         lastID = giARP_LastUpdateID;
91         
92         // Create request
93         Log("[ARP4 ] Asking for address %i.%i.%i.%i",
94                 Address.B[0], Address.B[1], Address.B[2], Address.B[3]
95                 );
96         req.HWType = htons(1);  // Ethernet
97         req.Type   = htons(0x0800);
98         req.HWSize = 6;
99         req.SWSize = 4;
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;
105         
106         // Send Request
107         Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
108         
109         // Wait for a reply
110         for(;;)
111         {
112                 while(lastID == giARP_LastUpdateID)     Threads_Yield();
113                 lastID = giARP_LastUpdateID;
114                 
115                 LOCK( &glARP_Cache4 );
116                 for( i = 0; i < giARP_Cache4Space; i++ )
117                 {
118                         if(gaARP_Cache4[i].IP.L != Address.L)   continue;
119                         
120                         RELEASE( &glARP_Cache4 );
121                         return gaARP_Cache4[i].MAC;
122                 }
123                 RELEASE( &glARP_Cache4 );
124         }
125 }
126
127 /**
128  * \brief Updates the ARP Cache entry for an IPv4 Address
129  */
130 void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
131 {
132          int    i;
133          int    free = -1;
134          int    oldest = 0;
135         
136         // Find an entry for the IP address in the cache
137         LOCK(&glARP_Cache4);
138         for( i = giARP_Cache4Space; i--; )
139         {
140                 if(gaARP_Cache4[oldest].LastUpdate > gaARP_Cache4[i].LastUpdate) {
141                         oldest = i;
142                 }
143                 if( gaARP_Cache4[i].IP.L == SWAddr.L )  break;
144                 if( gaARP_Cache4[i].LastUpdate == 0 && free == -1 )     free = i;
145         }
146         // If there was no match, we need to make one
147         if(i == -1) {
148                 if(free != -1)
149                         i = free;
150                 else
151                         i = oldest;
152                 gaARP_Cache4[i].IP = SWAddr;
153         }
154         
155         Log("[ARP  ] Caching %i.%i.%i.%i (%02x:%02x:%02x:%02x:%02x:%02x) in %i",
156                 SWAddr.B[0], SWAddr.B[1], SWAddr.B[2], SWAddr.B[3],
157                 HWAddr.B[0], HWAddr.B[1], HWAddr.B[2], HWAddr.B[3], HWAddr.B[4], HWAddr.B[5],
158                 i
159                 );
160                 
161         gaARP_Cache4[i].MAC = HWAddr;
162         gaARP_Cache4[i].LastUpdate = now();
163         giARP_LastUpdateID ++;
164         RELEASE(&glARP_Cache4);
165 }
166
167 /**
168  * \brief Updates the ARP Cache entry for an IPv6 Address
169  */
170 void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
171 {
172          int    i;
173          int    free = -1;
174          int    oldest = 0;
175         
176         // Find an entry for the MAC address in the cache
177         LOCK(&glARP_Cache6);
178         for( i = giARP_Cache6Space; i--; )
179         {
180                 if(gaARP_Cache6[oldest].LastUpdate > gaARP_Cache6[i].LastUpdate) {
181                         oldest = i;
182                 }
183                 if( MAC_EQU(gaARP_Cache6[i].MAC, HWAddr) )      break;
184                 if( gaARP_Cache6[i].LastUpdate == 0 && free == -1 )     free = i;
185         }
186         // If there was no match, we need to make one
187         if(i == -1) {
188                 if(free != -1)
189                         i = free;
190                 else
191                         i = oldest;
192                 gaARP_Cache6[i].MAC = HWAddr;
193         }
194         
195         gaARP_Cache6[i].IP = SWAddr;
196         gaARP_Cache6[i].LastUpdate = now();
197         giARP_LastUpdateID ++;
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         default:
317                 Warning("[ARP  ] Unknown Request ID %i", ntohs(req4->Request));
318                 break;
319         }
320 }

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