1 | /**
|
---|
2 | * \addtogroup uip
|
---|
3 | * @{
|
---|
4 | */
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * \defgroup uiparp uIP Address Resolution Protocol
|
---|
8 | * @{
|
---|
9 | *
|
---|
10 | * The Address Resolution Protocol ARP is used for mapping between IP
|
---|
11 | * addresses and link level addresses such as the Ethernet MAC
|
---|
12 | * addresses. ARP uses broadcast queries to ask for the link level
|
---|
13 | * address of a known IP address and the host which is configured with
|
---|
14 | * the IP address for which the query was meant, will respond with its
|
---|
15 | * link level address.
|
---|
16 | *
|
---|
17 | * \note This ARP implementation only supports Ethernet.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * \file
|
---|
22 | * Implementation of the ARP Address Resolution Protocol.
|
---|
23 | * \author Adam Dunkels <adam@dunkels.com>
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*
|
---|
28 | * Copyright (c) 2001-2003, Adam Dunkels.
|
---|
29 | * All rights reserved.
|
---|
30 | *
|
---|
31 | * Redistribution and use in source and binary forms, with or without
|
---|
32 | * modification, are permitted provided that the following conditions
|
---|
33 | * are met:
|
---|
34 | * 1. Redistributions of source code must retain the above copyright
|
---|
35 | * notice, this list of conditions and the following disclaimer.
|
---|
36 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
37 | * notice, this list of conditions and the following disclaimer in the
|
---|
38 | * documentation and/or other materials provided with the distribution.
|
---|
39 | * 3. The name of the author may not be used to endorse or promote
|
---|
40 | * products derived from this software without specific prior
|
---|
41 | * written permission.
|
---|
42 | *
|
---|
43 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
---|
44 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
45 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
---|
47 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
---|
49 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
50 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
---|
51 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
52 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
53 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
54 | *
|
---|
55 | * This file is part of the uIP TCP/IP stack.
|
---|
56 | *
|
---|
57 | * $Id: uip_arp.c 108 2015-06-11 09:15:46Z coas-nagasima $
|
---|
58 | *
|
---|
59 | */
|
---|
60 |
|
---|
61 |
|
---|
62 | #include "uip_arp.h"
|
---|
63 |
|
---|
64 | #include <string.h>
|
---|
65 |
|
---|
66 | #ifdef __RX
|
---|
67 | #pragma pack
|
---|
68 | #elif _MSC_VER
|
---|
69 | #pragma pack(push, 1)
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | struct arp_hdr {
|
---|
73 | struct uip_eth_hdr ethhdr;
|
---|
74 | u16_t hwtype;
|
---|
75 | u16_t protocol;
|
---|
76 | u8_t hwlen;
|
---|
77 | u8_t protolen;
|
---|
78 | u16_t opcode;
|
---|
79 | struct uip_eth_addr shwaddr;
|
---|
80 | u16_t sipaddr[2];
|
---|
81 | struct uip_eth_addr dhwaddr;
|
---|
82 | u16_t dipaddr[2];
|
---|
83 | };
|
---|
84 |
|
---|
85 | struct ethip_hdr {
|
---|
86 | struct uip_eth_hdr ethhdr;
|
---|
87 | /* IP header. */
|
---|
88 | u8_t vhl,
|
---|
89 | tos,
|
---|
90 | len[2],
|
---|
91 | ipid[2],
|
---|
92 | ipoffset[2],
|
---|
93 | ttl,
|
---|
94 | proto;
|
---|
95 | u16_t ipchksum;
|
---|
96 | u16_t srcipaddr[2],
|
---|
97 | destipaddr[2];
|
---|
98 | };
|
---|
99 |
|
---|
100 | #ifdef __RX
|
---|
101 | #pragma unpack
|
---|
102 | #elif _MSC_VER
|
---|
103 | #pragma pack(pop)
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | #define ARP_REQUEST 1
|
---|
107 | #define ARP_REPLY 2
|
---|
108 |
|
---|
109 | #define ARP_HWTYPE_ETH 1
|
---|
110 |
|
---|
111 | struct arp_entry {
|
---|
112 | u16_t ipaddr[2];
|
---|
113 | struct uip_eth_addr ethaddr;
|
---|
114 | u8_t time;
|
---|
115 | };
|
---|
116 |
|
---|
117 | static const struct uip_eth_addr broadcast_ethaddr =
|
---|
118 | {{0xff,0xff,0xff,0xff,0xff,0xff}};
|
---|
119 | static const u16_t broadcast_ipaddr[2] = {0xffff,0xffff};
|
---|
120 | static const u16_t multicast_ipaddr[2] = { 0x00e0, 0x0000 };
|
---|
121 | static const u16_t multicast_mask[2] = { 0x00f0, 0x0000 };
|
---|
122 |
|
---|
123 | static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
|
---|
124 | static u16_t ipaddr[2];
|
---|
125 | static u8_t i, c;
|
---|
126 |
|
---|
127 | static u8_t arptime;
|
---|
128 | static u8_t tmpage;
|
---|
129 |
|
---|
130 | #define BUF ((struct arp_hdr *)&uip_buf[0])
|
---|
131 | #define IPBUF ((struct ethip_hdr *)&uip_buf[0])
|
---|
132 | /*-----------------------------------------------------------------------------------*/
|
---|
133 | /**
|
---|
134 | * Initialize the ARP module.
|
---|
135 | *
|
---|
136 | */
|
---|
137 | /*-----------------------------------------------------------------------------------*/
|
---|
138 | void
|
---|
139 | uip_arp_init(void)
|
---|
140 | {
|
---|
141 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
|
---|
142 | memset(arp_table[i].ipaddr, 0, 4);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | /*-----------------------------------------------------------------------------------*/
|
---|
146 | /**
|
---|
147 | * Periodic ARP processing function.
|
---|
148 | *
|
---|
149 | * This function performs periodic timer processing in the ARP module
|
---|
150 | * and should be called at regular intervals. The recommended interval
|
---|
151 | * is 10 seconds between the calls.
|
---|
152 | *
|
---|
153 | */
|
---|
154 | /*-----------------------------------------------------------------------------------*/
|
---|
155 | void
|
---|
156 | uip_arp_timer(void)
|
---|
157 | {
|
---|
158 | struct arp_entry *tabptr;
|
---|
159 |
|
---|
160 | ++arptime;
|
---|
161 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
|
---|
162 | tabptr = &arp_table[i];
|
---|
163 | if((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 &&
|
---|
164 | arptime - tabptr->time >= UIP_ARP_MAXAGE) {
|
---|
165 | memset(tabptr->ipaddr, 0, 4);
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | }
|
---|
170 | /*-----------------------------------------------------------------------------------*/
|
---|
171 | static void
|
---|
172 | uip_arp_update(u16_t *ipaddr, struct uip_eth_addr *ethaddr)
|
---|
173 | {
|
---|
174 | register struct arp_entry *tabptr = NULL;
|
---|
175 | /* Walk through the ARP mapping table and try to find an entry to
|
---|
176 | update. If none is found, the IP -> MAC address mapping is
|
---|
177 | inserted in the ARP table. */
|
---|
178 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
|
---|
179 |
|
---|
180 | tabptr = &arp_table[i];
|
---|
181 | /* Only check those entries that are actually in use. */
|
---|
182 | if(tabptr->ipaddr[0] != 0 &&
|
---|
183 | tabptr->ipaddr[1] != 0) {
|
---|
184 |
|
---|
185 | /* Check if the source IP address of the incoming packet matches
|
---|
186 | the IP address in this ARP table entry. */
|
---|
187 | if(ipaddr[0] == tabptr->ipaddr[0] &&
|
---|
188 | ipaddr[1] == tabptr->ipaddr[1]) {
|
---|
189 |
|
---|
190 | /* An old entry found, update this and return. */
|
---|
191 | memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
|
---|
192 | tabptr->time = arptime;
|
---|
193 |
|
---|
194 | return;
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | /* If we get here, no existing ARP table entry was found, so we
|
---|
200 | create one. */
|
---|
201 |
|
---|
202 | /* First, we try to find an unused entry in the ARP table. */
|
---|
203 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
|
---|
204 | tabptr = &arp_table[i];
|
---|
205 | if(tabptr->ipaddr[0] == 0 &&
|
---|
206 | tabptr->ipaddr[1] == 0) {
|
---|
207 | break;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | /* If no unused entry is found, we try to find the oldest entry and
|
---|
212 | throw it away. */
|
---|
213 | if(i == UIP_ARPTAB_SIZE) {
|
---|
214 | tmpage = 0;
|
---|
215 | c = 0;
|
---|
216 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
|
---|
217 | tabptr = &arp_table[i];
|
---|
218 | if(arptime - tabptr->time > tmpage) {
|
---|
219 | tmpage = arptime - tabptr->time;
|
---|
220 | c = i;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | i = c;
|
---|
224 | tabptr = &arp_table[i];
|
---|
225 | }
|
---|
226 |
|
---|
227 | /* Now, i is the ARP table entry which we will fill with the new
|
---|
228 | information. */
|
---|
229 | memcpy(tabptr->ipaddr, ipaddr, 4);
|
---|
230 | memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
|
---|
231 | tabptr->time = arptime;
|
---|
232 | }
|
---|
233 | /*-----------------------------------------------------------------------------------*/
|
---|
234 | /**
|
---|
235 | * ARP processing for incoming IP packets
|
---|
236 | *
|
---|
237 | * This function should be called by the device driver when an IP
|
---|
238 | * packet has been received. The function will check if the address is
|
---|
239 | * in the ARP cache, and if so the ARP cache entry will be
|
---|
240 | * refreshed. If no ARP cache entry was found, a new one is created.
|
---|
241 | *
|
---|
242 | * This function expects an IP packet with a prepended Ethernet header
|
---|
243 | * in the uip_buf[] buffer, and the length of the packet in the global
|
---|
244 | * variable uip_len.
|
---|
245 | */
|
---|
246 | /*-----------------------------------------------------------------------------------*/
|
---|
247 | #if 0
|
---|
248 | void
|
---|
249 | uip_arp_ipin(void)
|
---|
250 | {
|
---|
251 | uip_len -= sizeof(struct uip_eth_hdr);
|
---|
252 |
|
---|
253 | /* Only insert/update an entry if the source IP address of the
|
---|
254 | incoming IP packet comes from a host on the local network. */
|
---|
255 | if((IPBUF->srcipaddr[0] & uip_netmask[0]) !=
|
---|
256 | (uip_hostaddr[0] & uip_netmask[0])) {
|
---|
257 | return;
|
---|
258 | }
|
---|
259 | if((IPBUF->srcipaddr[1] & uip_netmask[1]) !=
|
---|
260 | (uip_hostaddr[1] & uip_netmask[1])) {
|
---|
261 | return;
|
---|
262 | }
|
---|
263 | uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src));
|
---|
264 |
|
---|
265 | return;
|
---|
266 | }
|
---|
267 | #endif /* 0 */
|
---|
268 | /*-----------------------------------------------------------------------------------*/
|
---|
269 | /**
|
---|
270 | * ARP processing for incoming ARP packets.
|
---|
271 | *
|
---|
272 | * This function should be called by the device driver when an ARP
|
---|
273 | * packet has been received. The function will act differently
|
---|
274 | * depending on the ARP packet type: if it is a reply for a request
|
---|
275 | * that we previously sent out, the ARP cache will be filled in with
|
---|
276 | * the values from the ARP reply. If the incoming ARP packet is an ARP
|
---|
277 | * request for our IP address, an ARP reply packet is created and put
|
---|
278 | * into the uip_buf[] buffer.
|
---|
279 | *
|
---|
280 | * When the function returns, the value of the global variable uip_len
|
---|
281 | * indicates whether the device driver should send out a packet or
|
---|
282 | * not. If uip_len is zero, no packet should be sent. If uip_len is
|
---|
283 | * non-zero, it contains the length of the outbound packet that is
|
---|
284 | * present in the uip_buf[] buffer.
|
---|
285 | *
|
---|
286 | * This function expects an ARP packet with a prepended Ethernet
|
---|
287 | * header in the uip_buf[] buffer, and the length of the packet in the
|
---|
288 | * global variable uip_len.
|
---|
289 | */
|
---|
290 | /*-----------------------------------------------------------------------------------*/
|
---|
291 | void
|
---|
292 | uip_arp_arpin(void)
|
---|
293 | {
|
---|
294 |
|
---|
295 | if(uip_len < sizeof(struct arp_hdr)) {
|
---|
296 | uip_len = 0;
|
---|
297 | return;
|
---|
298 | }
|
---|
299 | uip_len = 0;
|
---|
300 |
|
---|
301 | switch(BUF->opcode) {
|
---|
302 | case HTONS(ARP_REQUEST):
|
---|
303 | /* ARP request. If it asked for our address, we send out a
|
---|
304 | reply. */
|
---|
305 | if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) {
|
---|
306 | /* First, we register the one who made the request in our ARP
|
---|
307 | table, since it is likely that we will do more communication
|
---|
308 | with this host in the future. */
|
---|
309 | uip_arp_update(BUF->sipaddr, &BUF->shwaddr);
|
---|
310 |
|
---|
311 | /* The reply opcode is 2. */
|
---|
312 | BUF->opcode = HTONS(2);
|
---|
313 |
|
---|
314 | memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6);
|
---|
315 | memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
|
---|
316 | memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
|
---|
317 | memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6);
|
---|
318 |
|
---|
319 | BUF->dipaddr[0] = BUF->sipaddr[0];
|
---|
320 | BUF->dipaddr[1] = BUF->sipaddr[1];
|
---|
321 | BUF->sipaddr[0] = uip_hostaddr[0];
|
---|
322 | BUF->sipaddr[1] = uip_hostaddr[1];
|
---|
323 |
|
---|
324 | BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
|
---|
325 | uip_len = sizeof(struct arp_hdr);
|
---|
326 | }
|
---|
327 | break;
|
---|
328 | case HTONS(ARP_REPLY):
|
---|
329 | /* ARP reply. We insert or update the ARP table if it was meant
|
---|
330 | for us. */
|
---|
331 | if(uip_ipaddr_cmp(BUF->dipaddr, uip_hostaddr)) {
|
---|
332 | uip_arp_update(BUF->sipaddr, &BUF->shwaddr);
|
---|
333 | }
|
---|
334 | break;
|
---|
335 | }
|
---|
336 |
|
---|
337 | return;
|
---|
338 | }
|
---|
339 | /*-----------------------------------------------------------------------------------*/
|
---|
340 | /**
|
---|
341 | * Prepend Ethernet header to an outbound IP packet and see if we need
|
---|
342 | * to send out an ARP request.
|
---|
343 | *
|
---|
344 | * This function should be called before sending out an IP packet. The
|
---|
345 | * function checks the destination IP address of the IP packet to see
|
---|
346 | * what Ethernet MAC address that should be used as a destination MAC
|
---|
347 | * address on the Ethernet.
|
---|
348 | *
|
---|
349 | * If the destination IP address is in the local network (determined
|
---|
350 | * by logical ANDing of netmask and our IP address), the function
|
---|
351 | * checks the ARP cache to see if an entry for the destination IP
|
---|
352 | * address is found. If so, an Ethernet header is prepended and the
|
---|
353 | * function returns. If no ARP cache entry is found for the
|
---|
354 | * destination IP address, the packet in the uip_buf[] is replaced by
|
---|
355 | * an ARP request packet for the IP address. The IP packet is dropped
|
---|
356 | * and it is assumed that they higher level protocols (e.g., TCP)
|
---|
357 | * eventually will retransmit the dropped packet.
|
---|
358 | *
|
---|
359 | * If the destination IP address is not on the local network, the IP
|
---|
360 | * address of the default router is used instead.
|
---|
361 | *
|
---|
362 | * When the function returns, a packet is present in the uip_buf[]
|
---|
363 | * buffer, and the length of the packet is in the global variable
|
---|
364 | * uip_len.
|
---|
365 | */
|
---|
366 | /*-----------------------------------------------------------------------------------*/
|
---|
367 | void
|
---|
368 | uip_arp_out(void)
|
---|
369 | {
|
---|
370 | struct arp_entry *tabptr = NULL;
|
---|
371 |
|
---|
372 | /* Find the destination IP address in the ARP table and construct
|
---|
373 | the Ethernet header. If the destination IP addres isn't on the
|
---|
374 | local network, we use the default router's IP address instead.
|
---|
375 |
|
---|
376 | If not ARP table entry is found, we overwrite the original IP
|
---|
377 | packet with an ARP request for the IP address. */
|
---|
378 |
|
---|
379 | /* First check if destination is a local broadcast. */
|
---|
380 | if(uip_ipaddr_cmp(IPBUF->destipaddr, broadcast_ipaddr)) {
|
---|
381 | memcpy(IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6);
|
---|
382 | }
|
---|
383 | else if(uip_ipaddr_maskcmp(IPBUF->destipaddr, multicast_ipaddr, multicast_mask)) {
|
---|
384 | IPBUF->ethhdr.dest.addr[0] = 0x01;
|
---|
385 | IPBUF->ethhdr.dest.addr[1] = 0x00;
|
---|
386 | IPBUF->ethhdr.dest.addr[2] = 0x5E;
|
---|
387 | IPBUF->ethhdr.dest.addr[3] = uip_ipaddr2(IPBUF->destipaddr) & 0x7F;
|
---|
388 | IPBUF->ethhdr.dest.addr[4] = uip_ipaddr3(IPBUF->destipaddr);
|
---|
389 | IPBUF->ethhdr.dest.addr[5] = uip_ipaddr4(IPBUF->destipaddr);
|
---|
390 | } else {
|
---|
391 | /* Check if the destination address is on the local network. */
|
---|
392 | if(!uip_ipaddr_maskcmp(IPBUF->destipaddr, uip_hostaddr, uip_netmask)) {
|
---|
393 | /* Destination address was not on the local network, so we need to
|
---|
394 | use the default router's IP address instead of the destination
|
---|
395 | address when determining the MAC address. */
|
---|
396 | uip_ipaddr_copy(ipaddr, uip_draddr);
|
---|
397 | } else {
|
---|
398 | /* Else, we use the destination IP address. */
|
---|
399 | uip_ipaddr_copy(ipaddr, IPBUF->destipaddr);
|
---|
400 | }
|
---|
401 |
|
---|
402 | for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
|
---|
403 | tabptr = &arp_table[i];
|
---|
404 | if(uip_ipaddr_cmp(ipaddr, tabptr->ipaddr)) {
|
---|
405 | break;
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | if(i == UIP_ARPTAB_SIZE) {
|
---|
410 | /* The destination address was not in our ARP table, so we
|
---|
411 | overwrite the IP packet with an ARP request. */
|
---|
412 |
|
---|
413 | memset(BUF->ethhdr.dest.addr, 0xff, 6);
|
---|
414 | memset(BUF->dhwaddr.addr, 0x00, 6);
|
---|
415 | memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
|
---|
416 | memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
|
---|
417 |
|
---|
418 | uip_ipaddr_copy(BUF->dipaddr, ipaddr);
|
---|
419 | uip_ipaddr_copy(BUF->sipaddr, uip_hostaddr);
|
---|
420 | BUF->opcode = HTONS(ARP_REQUEST); /* ARP request. */
|
---|
421 | BUF->hwtype = HTONS(ARP_HWTYPE_ETH);
|
---|
422 | BUF->protocol = HTONS(UIP_ETHTYPE_IP);
|
---|
423 | BUF->hwlen = 6;
|
---|
424 | BUF->protolen = 4;
|
---|
425 | BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
|
---|
426 |
|
---|
427 | uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN];
|
---|
428 |
|
---|
429 | uip_len = sizeof(struct arp_hdr);
|
---|
430 | return;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /* Build an ethernet header. */
|
---|
434 | memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6);
|
---|
435 | }
|
---|
436 | memcpy(IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
|
---|
437 |
|
---|
438 | IPBUF->ethhdr.type = HTONS(UIP_ETHTYPE_IP);
|
---|
439 |
|
---|
440 | uip_len += sizeof(struct uip_eth_hdr);
|
---|
441 | }
|
---|
442 | /*-----------------------------------------------------------------------------------*/
|
---|
443 |
|
---|
444 | /** @} */
|
---|
445 | /** @} */
|
---|