blob: 7765046d4d53fd005f419067e0bda9bc79cf3c91 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/**
2 * @file
3 * Address Resolution Protocol module for IP over Ethernet
4 *
5 * Functionally, ARP is divided into two parts. The first maps an IP address
6 * to a physical address when sending a packet, and the second part answers
7 * requests from other machines for our physical address.
8 *
9 * This implementation complies with RFC 826 (Ethernet ARP). It supports
10 * Gratuitious ARP from RFC3220 (IP Mobility Support for IPv4) section 4.6
11 * if an interface calls etharp_query(our_netif, its_ip_addr, NULL) upon
12 * address change.
13 */
14
15/*
16 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
17 * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv>
18 * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands.
19 * All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without modification,
22 * are permitted provided that the following conditions are met:
23 *
24 * 1. Redistributions of source code must retain the above copyright notice,
25 * this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright notice,
27 * this list of conditions and the following disclaimer in the documentation
28 * and/or other materials provided with the distribution.
29 * 3. The name of the author may not be used to endorse or promote products
30 * derived from this software without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
35 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
37 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
41 * OF SUCH DAMAGE.
42 *
43 * This file is part of the lwIP TCP/IP stack.
44 *
45 */
46
47#include "lwip/opt.h"
48#include "lwip/inet.h"
49#include "netif/etharp.h"
50#include "lwip/ip.h"
51#include "lwip/stats.h"
52
53/* ARP needs to inform DHCP of any ARP replies? */
54#if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
55# include "lwip/dhcp.h"
56#endif
57
58/** the time an ARP entry stays valid after its last update,
59 * (240 * 5) seconds = 20 minutes.
60 */
61#define ARP_MAXAGE 240
62/** the time an ARP entry stays pending after first request,
63 * (2 * 5) seconds = 10 seconds.
64 *
65 * @internal Keep this number at least 2, otherwise it might
66 * run out instantly if the timeout occurs directly after a request.
67 */
68#define ARP_MAXPENDING 2
69
70#define HWTYPE_ETHERNET 1
71
72/** ARP message types */
73#define ARP_REQUEST 1
74#define ARP_REPLY 2
75
76#define ARPH_HWLEN(hdr) (ntohs((hdr)->_hwlen_protolen) >> 8)
77#define ARPH_PROTOLEN(hdr) (ntohs((hdr)->_hwlen_protolen) & 0xff)
78
79#define ARPH_HWLEN_SET(hdr, len) (hdr)->_hwlen_protolen = htons(ARPH_PROTOLEN(hdr) | ((len) << 8))
80#define ARPH_PROTOLEN_SET(hdr, len) (hdr)->_hwlen_protolen = htons((len) | (ARPH_HWLEN(hdr) << 8))
81
82enum etharp_state {
83 ETHARP_STATE_EMPTY,
84 ETHARP_STATE_PENDING,
85 ETHARP_STATE_STABLE,
86 /** @internal transitional state used in etharp_tmr() for convenience*/
87 ETHARP_STATE_EXPIRED
88};
89
90struct etharp_entry {
91#if ARP_QUEUEING
92 /**
93 * Pointer to queue of pending outgoing packets on this ARP entry.
94 */
95 struct pbuf *p;
96#endif
97 struct ip_addr ipaddr;
98 struct eth_addr ethaddr;
99 enum etharp_state state;
100 u8_t ctime;
101};
102
103static const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
104static struct etharp_entry arp_table[ARP_TABLE_SIZE];
105
106/**
107 * Try hard to create a new entry - we want the IP address to appear in
108 * the cache (even if this means removing an active entry or so). */
109#define ETHARP_TRY_HARD 1
110
111static s8_t find_entry(struct ip_addr *ipaddr, u8_t flags);
112static err_t update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u8_t flags);
113/**
114 * Initializes ARP module.
115 */
116void
117etharp_init(void)
118{
119 u8_t i;
120 /* clear ARP entries */
121 for(i = 0; i < ARP_TABLE_SIZE; ++i) {
122 arp_table[i].state = ETHARP_STATE_EMPTY;
123#if ARP_QUEUEING
124 arp_table[i].p = NULL;
125#endif
126 arp_table[i].ctime = 0;
127 }
128}
129
130/**
131 * Clears expired entries in the ARP table.
132 *
133 * This function should be called every ETHARP_TMR_INTERVAL microseconds (5 seconds),
134 * in order to expire entries in the ARP table.
135 */
136void
137etharp_tmr(void)
138{
139 u8_t i;
140
141 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n"));
142 /* remove expired entries from the ARP table */
143 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
144 arp_table[i].ctime++;
145 /* stable entry? */
146 if ((arp_table[i].state == ETHARP_STATE_STABLE) &&
147 /* entry has become old? */
148 (arp_table[i].ctime >= ARP_MAXAGE)) {
149 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired stable entry %"U16_F".\n", (u16_t)i));
150 arp_table[i].state = ETHARP_STATE_EXPIRED;
151 /* pending entry? */
152 } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
153 /* entry unresolved/pending for too long? */
154 if (arp_table[i].ctime >= ARP_MAXPENDING) {
155 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired pending entry %"U16_F".\n", (u16_t)i));
156 arp_table[i].state = ETHARP_STATE_EXPIRED;
157#if ARP_QUEUEING
158 } else if (arp_table[i].p != NULL) {
159 /* resend an ARP query here */
160#endif
161 }
162 }
163 /* clean up entries that have just been expired */
164 if (arp_table[i].state == ETHARP_STATE_EXPIRED) {
165#if ARP_QUEUEING
166 /* and empty packet queue */
167 if (arp_table[i].p != NULL) {
168 /* remove all queued packets */
169 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].p)));
170 pbuf_free(arp_table[i].p);
171 arp_table[i].p = NULL;
172 }
173#endif
174 /* recycle entry for re-use */
175 arp_table[i].state = ETHARP_STATE_EMPTY;
176 }
177 }
178}
179
180/**
181 * Search the ARP table for a matching or new entry.
182 *
183 * If an IP address is given, return a pending or stable ARP entry that matches
184 * the address. If no match is found, create a new entry with this address set,
185 * but in state ETHARP_EMPTY. The caller must check and possibly change the
186 * state of the returned entry.
187 *
188 * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
189 *
190 * In all cases, attempt to create new entries from an empty entry. If no
191 * empty entries are available and ETHARP_TRY_HARD flag is set, recycle
192 * old entries. Heuristic choose the least important entry for recycling.
193 *
194 * @param ipaddr IP address to find in ARP cache, or to add if not found.
195 * @param flags
196 * - ETHARP_TRY_HARD: Try hard to create a entry by allowing recycling of
197 * active (stable or pending) entries.
198 *
199 * @return The ARP entry index that matched or is created, ERR_MEM if no
200 * entry is found or could be recycled.
201 */
202static s8_t find_entry(struct ip_addr *ipaddr, u8_t flags)
203{
204 s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
205 s8_t empty = ARP_TABLE_SIZE;
206 u8_t i = 0, age_pending = 0, age_stable = 0;
207#if ARP_QUEUEING
208 /* oldest entry with packets on queue */
209 s8_t old_queue = ARP_TABLE_SIZE;
210 /* its age */
211 u8_t age_queue = 0;
212#endif
213
214 /**
215 * a) do a search through the cache, remember candidates
216 * b) select candidate entry
217 * c) create new entry
218 */
219
220 /* a) in a single search sweep, do all of this
221 * 1) remember the first empty entry (if any)
222 * 2) remember the oldest stable entry (if any)
223 * 3) remember the oldest pending entry without queued packets (if any)
224 * 4) remember the oldest pending entry with queued packets (if any)
225 * 5) search for a matching IP entry, either pending or stable
226 * until 5 matches, or all entries are searched for.
227 */
228
229 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
230 /* no empty entry found yet and now we do find one? */
231 if ((empty == ARP_TABLE_SIZE) && (arp_table[i].state == ETHARP_STATE_EMPTY)) {
232 LWIP_DEBUGF(ETHARP_DEBUG, ("find_entry: found empty entry %"U16_F"\n", (u16_t)i));
233 /* remember first empty entry */
234 empty = i;
235 }
236 /* pending entry? */
237 else if (arp_table[i].state == ETHARP_STATE_PENDING) {
238 /* if given, does IP address match IP address in ARP entry? */
239 if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) {
240 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("find_entry: found matching pending entry %"U16_F"\n", (u16_t)i));
241 /* found exact IP address match, simply bail out */
242 return i;
243#if ARP_QUEUEING
244 /* pending with queued packets? */
245 } else if (arp_table[i].p != NULL) {
246 if (arp_table[i].ctime >= age_queue) {
247 old_queue = i;
248 age_queue = arp_table[i].ctime;
249 }
250#endif
251 /* pending without queued packets? */
252 } else {
253 if (arp_table[i].ctime >= age_pending) {
254 old_pending = i;
255 age_pending = arp_table[i].ctime;
256 }
257 }
258 }
259 /* stable entry? */
260 else if (arp_table[i].state == ETHARP_STATE_STABLE) {
261 /* if given, does IP address match IP address in ARP entry? */
262 if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) {
263 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("find_entry: found matching stable entry %"U16_F"\n", (u16_t)i));
264 /* found exact IP address match, simply bail out */
265 return i;
266 /* remember entry with oldest stable entry in oldest, its age in maxtime */
267 } else if (arp_table[i].ctime >= age_stable) {
268 old_stable = i;
269 age_stable = arp_table[i].ctime;
270 }
271 }
272 }
273 /* { we have no match } => try to create a new entry */
274
275 /* no empty entry found and not allowed to recycle? */
276 if ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_TRY_HARD) == 0))
277 {
278 return (s8_t)ERR_MEM;
279 }
280
281 /* b) choose the least destructive entry to recycle:
282 * 1) empty entry
283 * 2) oldest stable entry
284 * 3) oldest pending entry without queued packets
285 * 4) oldest pending entry without queued packets
286 *
287 * { ETHARP_TRY_HARD is set at this point }
288 */
289
290 /* 1) empty entry available? */
291 if (empty < ARP_TABLE_SIZE) {
292 i = empty;
293 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("find_entry: selecting empty entry %"U16_F"\n", (u16_t)i));
294 }
295 /* 2) found recyclable stable entry? */
296 else if (old_stable < ARP_TABLE_SIZE) {
297 /* recycle oldest stable*/
298 i = old_stable;
299 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("find_entry: selecting oldest stable entry %"U16_F"\n", (u16_t)i));
300#if ARP_QUEUEING
301 /* no queued packets should exist on stable entries */
302 LWIP_ASSERT("arp_table[i].p == NULL", arp_table[i].p == NULL);
303#endif
304 /* 3) found recyclable pending entry without queued packets? */
305 } else if (old_pending < ARP_TABLE_SIZE) {
306 /* recycle oldest pending */
307 i = old_pending;
308 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("find_entry: selecting oldest pending entry %"U16_F" (without queue)\n", (u16_t)i));
309#if ARP_QUEUEING
310 /* 4) found recyclable pending entry with queued packets? */
311 } else if (old_queue < ARP_TABLE_SIZE) {
312 /* recycle oldest pending */
313 i = old_queue;
314 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("find_entry: selecting oldest pending entry %"U16_F", freeing packet queue %p\n", (u16_t)i, (void *)(arp_table[i].p)));
315 pbuf_free(arp_table[i].p);
316 arp_table[i].p = NULL;
317#endif
318 /* no empty or recyclable entries found */
319 } else {
320 return (s8_t)ERR_MEM;
321 }
322
323 /* { empty or recyclable entry found } */
324 LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
325
326 /* recycle entry (no-op for an already empty entry) */
327 arp_table[i].state = ETHARP_STATE_EMPTY;
328
329 /* IP address given? */
330 if (ipaddr != NULL) {
331 /* set IP address */
332 ip_addr_set(&arp_table[i].ipaddr, ipaddr);
333 }
334 arp_table[i].ctime = 0;
335 return (err_t)i;
336}
337
338/**
339 * Update (or insert) a IP/MAC address pair in the ARP cache.
340 *
341 * If a pending entry is resolved, any queued packets will be sent
342 * at this point.
343 *
344 * @param ipaddr IP address of the inserted ARP entry.
345 * @param ethaddr Ethernet address of the inserted ARP entry.
346 * @param flags Defines behaviour:
347 * - ETHARP_TRY_HARD Allows ARP to insert this as a new item. If not specified,
348 * only existing ARP entries will be updated.
349 *
350 * @return
351 * - ERR_OK Succesfully updated ARP cache.
352 * - ERR_MEM If we could not add a new ARP entry when ETHARP_TRY_HARD was set.
353 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
354 *
355 * @see pbuf_free()
356 */
357static err_t
358update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u8_t flags)
359{
360 s8_t i, k;
361 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE | 3, ("update_arp_entry()\n"));
362 LWIP_ASSERT("netif->hwaddr_len != 0", netif->hwaddr_len != 0);
363 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n",
364 ip4_addr1(ipaddr), ip4_addr2(ipaddr), ip4_addr3(ipaddr), ip4_addr4(ipaddr),
365 ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
366 ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]));
367 /* non-unicast address? */
368 if (ip_addr_isany(ipaddr) ||
369 ip_addr_isbroadcast(ipaddr, netif) ||
370 ip_addr_ismulticast(ipaddr)) {
371 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
372 return ERR_ARG;
373 }
374 /* find or create ARP entry */
375 i = find_entry(ipaddr, flags);
376 /* bail out if no entry could be found */
377 if (i < 0) return (err_t)i;
378
379 /* mark it stable */
380 arp_table[i].state = ETHARP_STATE_STABLE;
381
382 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: updating stable entry %"S16_F"\n", (s16_t)i));
383 /* update address */
384 for (k = 0; k < netif->hwaddr_len; ++k) {
385 arp_table[i].ethaddr.addr[k] = ethaddr->addr[k];
386 }
387 /* reset time stamp */
388 arp_table[i].ctime = 0;
389/* this is where we will send out queued packets! */
390#if ARP_QUEUEING
391 while (arp_table[i].p != NULL) {
392 /* get the first packet on the queue */
393 struct pbuf *p = arp_table[i].p;
394 /* Ethernet header */
395 struct eth_hdr *ethhdr = p->payload;
396 /* remember (and reference) remainder of queue */
397 /* note: this will also terminate the p pbuf chain */
398 arp_table[i].p = pbuf_dequeue(p);
399 /* fill-in Ethernet header */
400 for (k = 0; k < netif->hwaddr_len; ++k) {
401 ethhdr->dest.addr[k] = ethaddr->addr[k];
402 ethhdr->src.addr[k] = netif->hwaddr[k];
403 }
404 ethhdr->type = htons(ETHTYPE_IP);
405 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: sending queued IP packet %p.\n", (void *)p));
406 /* send the queued IP packet */
407 netif->linkoutput(netif, p);
408 /* free the queued IP packet */
409 pbuf_free(p);
410 }
411#endif
412 return ERR_OK;
413}
414
415/**
416 * Updates the ARP table using the given IP packet.
417 *
418 * Uses the incoming IP packet's source address to update the
419 * ARP cache for the local network. The function does not alter
420 * or free the packet. This function must be called before the
421 * packet p is passed to the IP layer.
422 *
423 * @param netif The lwIP network interface on which the IP packet pbuf arrived.
424 * @param pbuf The IP packet that arrived on netif.
425 *
426 * @return NULL
427 *
428 * @see pbuf_free()
429 */
430void
431etharp_ip_input(struct netif *netif, struct pbuf *p)
432{
433 struct ethip_hdr *hdr;
434 LWIP_ASSERT("netif != NULL", netif != NULL);
435 /* Only insert an entry if the source IP address of the
436 incoming IP packet comes from a host on the local network. */
437 hdr = p->payload;
438 /* source is not on the local network? */
439 if (!ip_addr_netcmp(&(hdr->ip.src), &(netif->ip_addr), &(netif->netmask))) {
440 /* do nothing */
441 return;
442 }
443
444 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_ip_input: updating ETHARP table.\n"));
445 /* update ARP table */
446 /* @todo We could use ETHARP_TRY_HARD if we think we are going to talk
447 * back soon (for example, if the destination IP address is ours. */
448 update_arp_entry(netif, &(hdr->ip.src), &(hdr->eth.src), 0);
449}
450
451
452/**
453 * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache
454 * send out queued IP packets. Updates cache with snooped address pairs.
455 *
456 * Should be called for incoming ARP packets. The pbuf in the argument
457 * is freed by this function.
458 *
459 * @param netif The lwIP network interface on which the ARP packet pbuf arrived.
460 * @param pbuf The ARP packet that arrived on netif. Is freed by this function.
461 * @param ethaddr Ethernet address of netif.
462 *
463 * @return NULL
464 *
465 * @see pbuf_free()
466 */
467void
468etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p)
469{
470 struct etharp_hdr *hdr;
471 /* these are aligned properly, whereas the ARP header fields might not be */
472 struct ip_addr sipaddr, dipaddr;
473 u8_t i;
474 u8_t for_us;
475
476 LWIP_ASSERT("netif != NULL", netif != NULL);
477
478 /* drop short ARP packets */
479 if (p->tot_len < sizeof(struct etharp_hdr)) {
480 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE | 1, ("etharp_arp_input: packet dropped, too short (%"S16_F"/%"S16_F")\n", p->tot_len, sizeof(struct etharp_hdr)));
481 pbuf_free(p);
482 return;
483 }
484
485 hdr = p->payload;
486
487 /* get aligned copies of addresses */
488 *(struct ip_addr2 *)&sipaddr = hdr->sipaddr;
489 *(struct ip_addr2 *)&dipaddr = hdr->dipaddr;
490
491 /* this interface is not configured? */
492 if (netif->ip_addr.addr == 0) {
493 for_us = 0;
494 } else {
495 /* ARP packet directed to us? */
496 for_us = ip_addr_cmp(&dipaddr, &(netif->ip_addr));
497 }
498
499 /* ARP message directed to us? */
500 if (for_us) {
501 /* add IP address in ARP cache; assume requester wants to talk to us.
502 * can result in directly sending the queued packets for this host. */
503 update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), ETHARP_TRY_HARD);
504 /* ARP message not directed to us? */
505 } else {
506 /* update the source IP address in the cache, if present */
507 update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), 0);
508 }
509
510 /* now act on the message itself */
511 switch (htons(hdr->opcode)) {
512 /* ARP request? */
513 case ARP_REQUEST:
514 /* ARP request. If it asked for our address, we send out a
515 * reply. In any case, we time-stamp any existing ARP entry,
516 * and possiby send out an IP packet that was queued on it. */
517
518 LWIP_DEBUGF (ETHARP_DEBUG | DBG_TRACE, ("etharp_arp_input: incoming ARP request\n"));
519 /* ARP request for our address? */
520 if (for_us) {
521
522 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_arp_input: replying to ARP request for our IP address\n"));
523 /* re-use pbuf to send ARP reply */
524 hdr->opcode = htons(ARP_REPLY);
525
526 hdr->dipaddr = hdr->sipaddr;
527 hdr->sipaddr = *(struct ip_addr2 *)&netif->ip_addr;
528
529 for(i = 0; i < netif->hwaddr_len; ++i) {
530 hdr->dhwaddr.addr[i] = hdr->shwaddr.addr[i];
531 hdr->shwaddr.addr[i] = ethaddr->addr[i];
532 hdr->ethhdr.dest.addr[i] = hdr->dhwaddr.addr[i];
533 hdr->ethhdr.src.addr[i] = ethaddr->addr[i];
534 }
535
536 hdr->hwtype = htons(HWTYPE_ETHERNET);
537 ARPH_HWLEN_SET(hdr, netif->hwaddr_len);
538
539 hdr->proto = htons(ETHTYPE_IP);
540 ARPH_PROTOLEN_SET(hdr, sizeof(struct ip_addr));
541
542 hdr->ethhdr.type = htons(ETHTYPE_ARP);
543 /* return ARP reply */
544 netif->linkoutput(netif, p);
545 /* we are not configured? */
546 } else if (netif->ip_addr.addr == 0) {
547 /* { for_us == 0 and netif->ip_addr.addr == 0 } */
548 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n"));
549 /* request was not directed to us */
550 } else {
551 /* { for_us == 0 and netif->ip_addr.addr != 0 } */
552 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_arp_input: ARP request was not for us.\n"));
553 }
554 break;
555 case ARP_REPLY:
556 /* ARP reply. We already updated the ARP cache earlier. */
557 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_arp_input: incoming ARP reply\n"));
558#if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
559 /* DHCP wants to know about ARP replies from any host with an
560 * IP address also offered to us by the DHCP server. We do not
561 * want to take a duplicate IP address on a single network.
562 * @todo How should we handle redundant (fail-over) interfaces?
563 * */
564 dhcp_arp_reply(netif, &sipaddr);
565#endif
566 break;
567 default:
568 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_arp_input: ARP unknown opcode type %"S16_F"\n", htons(hdr->opcode)));
569 break;
570 }
571 /* free ARP packet */
572 pbuf_free(p);
573}
574
575/**
576 * Resolve and fill-in Ethernet address header for outgoing packet.
577 *
578 * For IP multicast and broadcast, corresponding Ethernet addresses
579 * are selected and the packet is transmitted on the link.
580 *
581 * For unicast addresses, the packet is submitted to etharp_query(). In
582 * case the IP address is outside the local network, the IP address of
583 * the gateway is used.
584 *
585 * @param netif The lwIP network interface which the IP packet will be sent on.
586 * @param ipaddr The IP address of the packet destination.
587 * @param pbuf The pbuf(s) containing the IP packet to be sent.
588 *
589 * @return
590 * - ERR_RTE No route to destination (no gateway to external networks),
591 * or the return type of either etharp_query() or netif->linkoutput().
592 */
593err_t
594etharp_output(struct netif *netif, struct ip_addr *ipaddr, struct pbuf *q)
595{
596 struct eth_addr *dest, *srcaddr, mcastaddr;
597 struct eth_hdr *ethhdr;
598 u8_t i;
599
600 /* make room for Ethernet header - should not fail */
601 if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) {
602 /* bail out */
603 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE | 2, ("etharp_output: could not allocate room for header.\n"));
604 LINK_STATS_INC(link.lenerr);
605 return ERR_BUF;
606 }
607
608 /* assume unresolved Ethernet address */
609 dest = NULL;
610 /* Determine on destination hardware address. Broadcasts and multicasts
611 * are special, other IP addresses are looked up in the ARP table. */
612
613 /* broadcast destination IP address? */
614 if (ip_addr_isbroadcast(ipaddr, netif)) {
615 /* broadcast on Ethernet also */
616 dest = (struct eth_addr *)&ethbroadcast;
617 /* multicast destination IP address? */
618 } else if (ip_addr_ismulticast(ipaddr)) {
619 /* Hash IP multicast address to MAC address.*/
620 mcastaddr.addr[0] = 0x01;
621 mcastaddr.addr[1] = 0x00;
622 mcastaddr.addr[2] = 0x5e;
623 mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
624 mcastaddr.addr[4] = ip4_addr3(ipaddr);
625 mcastaddr.addr[5] = ip4_addr4(ipaddr);
626 /* destination Ethernet address is multicast */
627 dest = &mcastaddr;
628 /* unicast destination IP address? */
629 } else {
630 /* outside local network? */
631 if (!ip_addr_netcmp(ipaddr, &(netif->ip_addr), &(netif->netmask))) {
632 /* interface has default gateway? */
633 if (netif->gw.addr != 0) {
634 /* send to hardware address of default gateway IP address */
635 ipaddr = &(netif->gw);
636 /* no default gateway available */
637 } else {
638 /* no route to destination error (default gateway missing) */
639 return ERR_RTE;
640 }
641 }
642 /* queue on destination Ethernet address belonging to ipaddr */
643 return etharp_query(netif, ipaddr, q);
644 }
645
646 /* continuation for multicast/broadcast destinations */
647 /* obtain source Ethernet address of the given interface */
648 srcaddr = (struct eth_addr *)netif->hwaddr;
649 ethhdr = q->payload;
650 for (i = 0; i < netif->hwaddr_len; i++) {
651 ethhdr->dest.addr[i] = dest->addr[i];
652 ethhdr->src.addr[i] = srcaddr->addr[i];
653 }
654 ethhdr->type = htons(ETHTYPE_IP);
655 /* send packet directly on the link */
656 return netif->linkoutput(netif, q);
657}
658
659/**
660 * Send an ARP request for the given IP address and/or queue a packet.
661 *
662 * If the IP address was not yet in the cache, a pending ARP cache entry
663 * is added and an ARP request is sent for the given address. The packet
664 * is queued on this entry.
665 *
666 * If the IP address was already pending in the cache, a new ARP request
667 * is sent for the given address. The packet is queued on this entry.
668 *
669 * If the IP address was already stable in the cache, and a packet is
670 * given, it is directly sent and no ARP request is sent out.
671 *
672 * If the IP address was already stable in the cache, and no packet is
673 * given, an ARP request is sent out.
674 *
675 * @param netif The lwIP network interface on which ipaddr
676 * must be queried for.
677 * @param ipaddr The IP address to be resolved.
678 * @param q If non-NULL, a pbuf that must be delivered to the IP address.
679 * q is not freed by this function.
680 *
681 * @return
682 * - ERR_BUF Could not make room for Ethernet header.
683 * - ERR_MEM Hardware address unknown, and no more ARP entries available
684 * to query for address or queue the packet.
685 * - ERR_MEM Could not queue packet due to memory shortage.
686 * - ERR_RTE No route to destination (no gateway to external networks).
687 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
688 *
689 */
690err_t etharp_query(struct netif *netif, struct ip_addr *ipaddr, struct pbuf *q)
691{
692 struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
693 err_t result = ERR_MEM;
694 s8_t i; /* ARP entry index */
695 u8_t k; /* Ethernet address octet index */
696
697 /* non-unicast address? */
698 if (ip_addr_isbroadcast(ipaddr, netif) ||
699 ip_addr_ismulticast(ipaddr) ||
700 ip_addr_isany(ipaddr)) {
701 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
702 return ERR_ARG;
703 }
704
705 /* find entry in ARP cache, ask to create entry if queueing packet */
706 i = find_entry(ipaddr, ETHARP_TRY_HARD);
707
708 /* could not find or create entry? */
709 if (i < 0)
710 {
711 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
712 if (q) LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: packet dropped\n"));
713 return (err_t)i;
714 }
715
716 /* mark a fresh entry as pending (we just sent a request) */
717 if (arp_table[i].state == ETHARP_STATE_EMPTY) {
718 arp_table[i].state = ETHARP_STATE_PENDING;
719 }
720
721 /* { i is either a STABLE or (new or existing) PENDING entry } */
722 LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
723 ((arp_table[i].state == ETHARP_STATE_PENDING) ||
724 (arp_table[i].state == ETHARP_STATE_STABLE)));
725
726 /* do we have a pending entry? or an implicit query request? */
727 if ((arp_table[i].state == ETHARP_STATE_PENDING) || (q == NULL)) {
728 /* try to resolve it; send out ARP request */
729 result = etharp_request(netif, ipaddr);
730 }
731
732 /* packet given? */
733 if (q != NULL) {
734 /* stable entry? */
735 if (arp_table[i].state == ETHARP_STATE_STABLE) {
736 /* we have a valid IP->Ethernet address mapping,
737 * fill in the Ethernet header for the outgoing packet */
738 struct eth_hdr *ethhdr = q->payload;
739 for(k = 0; k < netif->hwaddr_len; k++) {
740 ethhdr->dest.addr[k] = arp_table[i].ethaddr.addr[k];
741 ethhdr->src.addr[k] = srcaddr->addr[k];
742 }
743 ethhdr->type = htons(ETHTYPE_IP);
744 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: sending packet %p\n", (void *)q));
745 /* send the packet */
746 result = netif->linkoutput(netif, q);
747 /* pending entry? (either just created or already pending */
748 } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
749#if ARP_QUEUEING /* queue the given q packet */
750 struct pbuf *p;
751 /* copy any PBUF_REF referenced payloads into PBUF_RAM */
752 /* (the caller of lwIP assumes the referenced payload can be
753 * freed after it returns from the lwIP call that brought us here) */
754 p = pbuf_take(q);
755 /* packet could be taken over? */
756 if (p != NULL) {
757 /* queue packet ... */
758 if (arp_table[i].p == NULL) {
759 /* ... in the empty queue */
760 pbuf_ref(p);
761 arp_table[i].p = p;
762#if 0 /* multi-packet-queueing disabled, see bug #11400 */
763 } else {
764 /* ... at tail of non-empty queue */
765 pbuf_queue(arp_table[i].p, p);
766#endif
767 }
768 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
769 result = ERR_OK;
770 } else {
771 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q));
772 /* { result == ERR_MEM } through initialization */
773 }
774#else /* ARP_QUEUEING == 0 */
775 /* q && state == PENDING && ARP_QUEUEING == 0 => result = ERR_MEM */
776 /* { result == ERR_MEM } through initialization */
777 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: Ethernet destination address unknown, queueing disabled, packet %p dropped\n", (void *)q));
778#endif
779 }
780 }
781 return result;
782}
783
784err_t etharp_request(struct netif *netif, struct ip_addr *ipaddr)
785{
786 struct pbuf *p;
787 struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
788 err_t result = ERR_OK;
789 u8_t k; /* ARP entry index */
790
791 /* allocate a pbuf for the outgoing ARP request packet */
792 p = pbuf_alloc(PBUF_LINK, sizeof(struct etharp_hdr), PBUF_RAM);
793 /* could allocate a pbuf for an ARP request? */
794 if (p != NULL) {
795 struct etharp_hdr *hdr = p->payload;
796 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_request: sending ARP request.\n"));
797 hdr->opcode = htons(ARP_REQUEST);
798 for (k = 0; k < netif->hwaddr_len; k++)
799 {
800 hdr->shwaddr.addr[k] = srcaddr->addr[k];
801 /* the hardware address is what we ask for, in
802 * a request it is a don't-care value, we use zeroes */
803 hdr->dhwaddr.addr[k] = 0x00;
804 }
805 hdr->dipaddr = *(struct ip_addr2 *)ipaddr;
806 hdr->sipaddr = *(struct ip_addr2 *)&netif->ip_addr;
807
808 hdr->hwtype = htons(HWTYPE_ETHERNET);
809 ARPH_HWLEN_SET(hdr, netif->hwaddr_len);
810
811 hdr->proto = htons(ETHTYPE_IP);
812 ARPH_PROTOLEN_SET(hdr, sizeof(struct ip_addr));
813 for (k = 0; k < netif->hwaddr_len; ++k)
814 {
815 /* broadcast to all network interfaces on the local network */
816 hdr->ethhdr.dest.addr[k] = 0xff;
817 hdr->ethhdr.src.addr[k] = srcaddr->addr[k];
818 }
819 hdr->ethhdr.type = htons(ETHTYPE_ARP);
820 /* send ARP query */
821 result = netif->linkoutput(netif, p);
822 /* free ARP query packet */
823 pbuf_free(p);
824 p = NULL;
825 /* could not allocate pbuf for ARP request */
826 } else {
827 result = ERR_MEM;
828 LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE | 2, ("etharp_request: could not allocate pbuf for ARP request.\n"));
829 }
830 return result;
831}