1 /**
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 * Gratuitous ARP from RFC3220 (IP Mobility Support for IPv4) section 4.6
11 * if an interface calls etharp_gratuitous(our_netif) upon address change.
12 */
13
14 /*
15 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
16 * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv>
17 * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands.
18 * All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without modification,
21 * are permitted provided that the following conditions are met:
22 *
23 * 1. Redistributions of source code must retain the above copyright notice,
24 * this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials provided with the distribution.
28 * 3. The name of the author may not be used to endorse or promote products
29 * derived from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
32 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
34 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
36 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
40 * OF SUCH DAMAGE.
41 *
42 * This file is part of the lwIP TCP/IP stack.
43 *
44 */
45
46 #include "lwip/opt.h"
47
48 #if LWIP_IPV4 && LWIP_ARP /* don't build if not configured for use in lwipopts.h */
49
50 #include "lwip/etharp.h"
51 #include "lwip/stats.h"
52 #include "lwip/snmp.h"
53 #include "lwip/dhcp.h"
54 #include "lwip/autoip.h"
55 #include "lwip/acd.h"
56 #include "lwip/prot/iana.h"
57 #include "netif/ethernet.h"
58
59 #include <string.h>
60
61 #ifdef LWIP_HOOK_FILENAME
62 #include LWIP_HOOK_FILENAME
63 #endif
64
65 /** Re-request a used ARP entry 1 minute before it would expire to prevent
66 * breaking a steadily used connection because the ARP entry timed out. */
67 #define ARP_AGE_REREQUEST_USED_UNICAST (ARP_MAXAGE - 30)
68 #define ARP_AGE_REREQUEST_USED_BROADCAST (ARP_MAXAGE - 15)
69
70 /** the time an ARP entry stays pending after first request,
71 * for ARP_TMR_INTERVAL = 1000, this is
72 * 10 seconds.
73 *
74 * @internal Keep this number at least 2, otherwise it might
75 * run out instantly if the timeout occurs directly after a request.
76 */
77 #define ARP_MAXPENDING 5
78
79 /** ARP states */
80 enum etharp_state {
81 ETHARP_STATE_EMPTY = 0,
82 ETHARP_STATE_PENDING,
83 ETHARP_STATE_STABLE,
84 ETHARP_STATE_STABLE_REREQUESTING_1,
85 ETHARP_STATE_STABLE_REREQUESTING_2
86 #if ETHARP_SUPPORT_STATIC_ENTRIES
87 , ETHARP_STATE_STATIC
88 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
89 };
90
91 struct etharp_entry {
92 #if ARP_QUEUEING
93 /** Pointer to queue of pending outgoing packets on this ARP entry. */
94 struct etharp_q_entry *q;
95 #else /* ARP_QUEUEING */
96 /** Pointer to a single pending outgoing packet on this ARP entry. */
97 struct pbuf *q;
98 #endif /* ARP_QUEUEING */
99 ip4_addr_t ipaddr;
100 struct netif *netif;
101 struct eth_addr ethaddr;
102 u16_t ctime;
103 u8_t state;
104 };
105
106 static struct etharp_entry arp_table[ARP_TABLE_SIZE];
107
108 #if !LWIP_NETIF_HWADDRHINT
109 static netif_addr_idx_t etharp_cached_entry;
110 #endif /* !LWIP_NETIF_HWADDRHINT */
111
112 /** Try hard to create a new entry - we want the IP address to appear in
113 the cache (even if this means removing an active entry or so). */
114 #define ETHARP_FLAG_TRY_HARD 1
115 #define ETHARP_FLAG_FIND_ONLY 2
116 #if ETHARP_SUPPORT_STATIC_ENTRIES
117 #define ETHARP_FLAG_STATIC_ENTRY 4
118 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
119
120 #if LWIP_NETIF_HWADDRHINT
121 #define ETHARP_SET_ADDRHINT(netif, addrhint) do { if (((netif) != NULL) && ((netif)->hints != NULL)) { \
122 (netif)->hints->addr_hint = (addrhint); }} while(0)
123 #else /* LWIP_NETIF_HWADDRHINT */
124 #define ETHARP_SET_ADDRHINT(netif, addrhint) (etharp_cached_entry = (addrhint))
125 #endif /* LWIP_NETIF_HWADDRHINT */
126
127
128 /* Check for maximum ARP_TABLE_SIZE */
129 #if (ARP_TABLE_SIZE > NETIF_ADDR_IDX_MAX)
130 #error "ARP_TABLE_SIZE must fit in an s16_t, you have to reduce it in your lwipopts.h"
131 #endif
132
133
134 static err_t etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr *hw_dst_addr);
135 static err_t etharp_raw(struct netif *netif,
136 const struct eth_addr *ethsrc_addr, const struct eth_addr *ethdst_addr,
137 const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr,
138 const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr,
139 const u16_t opcode);
140
141 #if ARP_QUEUEING
142 /**
143 * Free a complete queue of etharp entries
144 *
145 * @param q a queue of etharp_q_entry's to free
146 */
147 static void
free_etharp_q(struct etharp_q_entry * q)148 free_etharp_q(struct etharp_q_entry *q)
149 {
150 struct etharp_q_entry *r;
151 LWIP_ASSERT("q != NULL", q != NULL);
152 while (q) {
153 r = q;
154 q = q->next;
155 LWIP_ASSERT("r->p != NULL", (r->p != NULL));
156 pbuf_free(r->p);
157 memp_free(MEMP_ARP_QUEUE, r);
158 }
159 }
160 #else /* ARP_QUEUEING */
161
162 /** Compatibility define: free the queued pbuf */
163 #define free_etharp_q(q) pbuf_free(q)
164
165 #endif /* ARP_QUEUEING */
166
167 /** Clean up ARP table entries */
168 static void
etharp_free_entry(int i)169 etharp_free_entry(int i)
170 {
171 /* remove from SNMP ARP index tree */
172 mib2_remove_arp_entry(arp_table[i].netif, &arp_table[i].ipaddr);
173 /* and empty packet queue */
174 if (arp_table[i].q != NULL) {
175 /* remove all queued packets */
176 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_free_entry: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q)));
177 free_etharp_q(arp_table[i].q);
178 arp_table[i].q = NULL;
179 }
180 /* recycle entry for re-use */
181 arp_table[i].state = ETHARP_STATE_EMPTY;
182 #ifdef LWIP_DEBUG
183 /* for debugging, clean out the complete entry */
184 arp_table[i].ctime = 0;
185 arp_table[i].netif = NULL;
186 ip4_addr_set_zero(&arp_table[i].ipaddr);
187 arp_table[i].ethaddr = ethzero;
188 #endif /* LWIP_DEBUG */
189 }
190
191 #if LWIP_LOWPOWER
192 #include "lwip/lowpower.h"
193 u32_t
etharp_tmr_tick(void)194 etharp_tmr_tick(void)
195 {
196 s32_t i;
197 u32_t tick = 0;
198 u32_t time;
199
200 for (i = 0; i < ARP_TABLE_SIZE; i++) {
201 u8_t state = arp_table[i].state;
202 if ((state != ETHARP_STATE_EMPTY)
203 #if ETHARP_SUPPORT_STATIC_ENTRIES
204 && (state != ETHARP_STATE_STATIC)
205 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
206 ) {
207 if (arp_table[i].state != ETHARP_STATE_STABLE) {
208 LWIP_DEBUGF(LOWPOWER_DEBUG, ("%s tmr tick: 1\n", "etharp_tmr_tick"));
209 return 1;
210 }
211 time = (u32_t)ARP_MAXAGE - arp_table[i].ctime;
212 SET_TMR_TICK(tick, time);
213 }
214 }
215 LWIP_DEBUGF(LOWPOWER_DEBUG, ("%s tmr tick: %u\n", "etharp_tmr_tick", tick));
216 return tick;
217 }
218 #endif /* LWIP_LOWPOWER */
219
220 /**
221 * Clears expired entries in the ARP table.
222 *
223 * This function should be called every ARP_TMR_INTERVAL milliseconds (1 second),
224 * in order to expire entries in the ARP table.
225 */
226 void
etharp_tmr(void)227 etharp_tmr(void)
228 {
229 int i;
230
231 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n"));
232 /* remove expired entries from the ARP table */
233 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
234 u8_t state = arp_table[i].state;
235 if (state != ETHARP_STATE_EMPTY
236 #if ETHARP_SUPPORT_STATIC_ENTRIES
237 && (state != ETHARP_STATE_STATIC)
238 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
239 ) {
240 arp_table[i].ctime++;
241 if ((arp_table[i].ctime >= ARP_MAXAGE) ||
242 ((arp_table[i].state == ETHARP_STATE_PENDING) &&
243 (arp_table[i].ctime >= ARP_MAXPENDING))) {
244 /* pending or stable entry has become old! */
245 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %d.\n",
246 arp_table[i].state >= ETHARP_STATE_STABLE ? "stable" : "pending", i));
247 /* clean up entries that have just been expired */
248 etharp_free_entry(i);
249 } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_1) {
250 /* Don't send more than one request every 2 seconds. */
251 arp_table[i].state = ETHARP_STATE_STABLE_REREQUESTING_2;
252 } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_2) {
253 /* Reset state to stable, so that the next transmitted packet will
254 re-send an ARP request. */
255 arp_table[i].state = ETHARP_STATE_STABLE;
256 } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
257 /* still pending, resend an ARP query */
258 etharp_request(arp_table[i].netif, &arp_table[i].ipaddr);
259 }
260 }
261 }
262 }
263
264 /**
265 * Search the ARP table for a matching or new entry.
266 *
267 * If an IP address is given, return a pending or stable ARP entry that matches
268 * the address. If no match is found, create a new entry with this address set,
269 * but in state ETHARP_EMPTY. The caller must check and possibly change the
270 * state of the returned entry.
271 *
272 * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
273 *
274 * In all cases, attempt to create new entries from an empty entry. If no
275 * empty entries are available and ETHARP_FLAG_TRY_HARD flag is set, recycle
276 * old entries. Heuristic choose the least important entry for recycling.
277 *
278 * @param ipaddr IP address to find in ARP cache, or to add if not found.
279 * @param flags See @ref etharp_state
280 * @param netif netif related to this address (used for NETIF_HWADDRHINT)
281 *
282 * @return The ARP entry index that matched or is created, ERR_MEM if no
283 * entry is found or could be recycled.
284 */
285 static s16_t
etharp_find_entry(const ip4_addr_t * ipaddr,u8_t flags,struct netif * netif)286 etharp_find_entry(const ip4_addr_t *ipaddr, u8_t flags, struct netif *netif)
287 {
288 s16_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
289 s16_t empty = ARP_TABLE_SIZE;
290 s16_t i = 0;
291 /* oldest entry with packets on queue */
292 s16_t old_queue = ARP_TABLE_SIZE;
293 /* its age */
294 u16_t age_queue = 0, age_pending = 0, age_stable = 0;
295
296 LWIP_UNUSED_ARG(netif);
297
298 /**
299 * a) do a search through the cache, remember candidates
300 * b) select candidate entry
301 * c) create new entry
302 */
303
304 /* a) in a single search sweep, do all of this
305 * 1) remember the first empty entry (if any)
306 * 2) remember the oldest stable entry (if any)
307 * 3) remember the oldest pending entry without queued packets (if any)
308 * 4) remember the oldest pending entry with queued packets (if any)
309 * 5) search for a matching IP entry, either pending or stable
310 * until 5 matches, or all entries are searched for.
311 */
312
313 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
314 u8_t state = arp_table[i].state;
315 /* no empty entry found yet and now we do find one? */
316 if ((empty == ARP_TABLE_SIZE) && (state == ETHARP_STATE_EMPTY)) {
317 LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_find_entry: found empty entry %d\n", (int)i));
318 /* remember first empty entry */
319 empty = i;
320 } else if (state != ETHARP_STATE_EMPTY) {
321 LWIP_ASSERT("state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE",
322 state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE);
323 /* if given, does IP address match IP address in ARP entry? */
324 if (ipaddr && ip4_addr_eq(ipaddr, &arp_table[i].ipaddr)
325 #if ETHARP_TABLE_MATCH_NETIF
326 && ((netif == NULL) || (netif == arp_table[i].netif))
327 #endif /* ETHARP_TABLE_MATCH_NETIF */
328 ) {
329 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: found matching entry %d\n", (int)i));
330 /* found exact IP address match, simply bail out */
331 return i;
332 }
333 /* pending entry? */
334 if (state == ETHARP_STATE_PENDING) {
335 /* pending with queued packets? */
336 if (arp_table[i].q != NULL) {
337 if (arp_table[i].ctime >= age_queue) {
338 old_queue = i;
339 age_queue = arp_table[i].ctime;
340 }
341 } else
342 /* pending without queued packets? */
343 {
344 if (arp_table[i].ctime >= age_pending) {
345 old_pending = i;
346 age_pending = arp_table[i].ctime;
347 }
348 }
349 /* stable entry? */
350 } else if (state >= ETHARP_STATE_STABLE) {
351 #if ETHARP_SUPPORT_STATIC_ENTRIES
352 /* don't record old_stable for static entries since they never expire */
353 if (state < ETHARP_STATE_STATIC)
354 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
355 {
356 /* remember entry with oldest stable entry in oldest, its age in maxtime */
357 if (arp_table[i].ctime >= age_stable) {
358 old_stable = i;
359 age_stable = arp_table[i].ctime;
360 }
361 }
362 }
363 }
364 }
365 /* { we have no match } => try to create a new entry */
366
367 /* don't create new entry, only search? */
368 if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) ||
369 /* or no empty entry found and not allowed to recycle? */
370 ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) {
371 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty entry found and not allowed to recycle\n"));
372 return (s16_t)ERR_MEM;
373 }
374
375 /* b) choose the least destructive entry to recycle:
376 * 1) empty entry
377 * 2) oldest stable entry
378 * 3) oldest pending entry without queued packets
379 * 4) oldest pending entry with queued packets
380 *
381 * { ETHARP_FLAG_TRY_HARD is set at this point }
382 */
383
384 /* 1) empty entry available? */
385 if (empty < ARP_TABLE_SIZE) {
386 i = empty;
387 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting empty entry %d\n", (int)i));
388 } else {
389 /* 2) found recyclable stable entry? */
390 if (old_stable < ARP_TABLE_SIZE) {
391 /* recycle oldest stable*/
392 i = old_stable;
393 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest stable entry %d\n", (int)i));
394 /* no queued packets should exist on stable entries */
395 LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL);
396 /* 3) found recyclable pending entry without queued packets? */
397 } else if (old_pending < ARP_TABLE_SIZE) {
398 /* recycle oldest pending */
399 i = old_pending;
400 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %d (without queue)\n", (int)i));
401 /* 4) found recyclable pending entry with queued packets? */
402 } else if (old_queue < ARP_TABLE_SIZE) {
403 /* recycle oldest pending (queued packets are free in etharp_free_entry) */
404 i = old_queue;
405 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %d, freeing packet queue %p\n", (int)i, (void *)(arp_table[i].q)));
406 /* no empty or recyclable entries found */
407 } else {
408 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty or recyclable entries found\n"));
409 return (s16_t)ERR_MEM;
410 }
411
412 /* { empty or recyclable entry found } */
413 LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
414 etharp_free_entry(i);
415 }
416
417 LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
418 LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY",
419 arp_table[i].state == ETHARP_STATE_EMPTY);
420
421 /* IP address given? */
422 if (ipaddr != NULL) {
423 /* set IP address */
424 ip4_addr_copy(arp_table[i].ipaddr, *ipaddr);
425 }
426 arp_table[i].ctime = 0;
427 #if ETHARP_TABLE_MATCH_NETIF
428 arp_table[i].netif = netif;
429 #endif /* ETHARP_TABLE_MATCH_NETIF */
430 return (s16_t)i;
431 }
432
433 /**
434 * Update (or insert) a IP/MAC address pair in the ARP cache.
435 *
436 * If a pending entry is resolved, any queued packets will be sent
437 * at this point.
438 *
439 * @param netif netif related to this entry (used for NETIF_ADDRHINT)
440 * @param ipaddr IP address of the inserted ARP entry.
441 * @param ethaddr Ethernet address of the inserted ARP entry.
442 * @param flags See @ref etharp_state
443 *
444 * @return
445 * - ERR_OK Successfully updated ARP cache.
446 * - ERR_MEM If we could not add a new ARP entry when ETHARP_FLAG_TRY_HARD was set.
447 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
448 *
449 * @see pbuf_free()
450 */
451 static err_t
etharp_update_arp_entry(struct netif * netif,const ip4_addr_t * ipaddr,struct eth_addr * ethaddr,u8_t flags)452 etharp_update_arp_entry(struct netif *netif, const ip4_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)
453 {
454 s16_t i;
455 LWIP_ASSERT("netif->hwaddr_len == ETH_HWADDR_LEN", netif->hwaddr_len == ETH_HWADDR_LEN);
456 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_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",
457 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
458 (u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2],
459 (u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5]));
460 /* non-unicast address? */
461 if (ip4_addr_isany(ipaddr) ||
462 ip4_addr_isbroadcast(ipaddr, netif) ||
463 ip4_addr_ismulticast(ipaddr)) {
464 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
465 return ERR_ARG;
466 }
467 /* find or create ARP entry */
468 i = etharp_find_entry(ipaddr, flags, netif);
469 /* bail out if no entry could be found */
470 if (i < 0) {
471 return (err_t)i;
472 }
473
474 #if ETHARP_SUPPORT_STATIC_ENTRIES
475 if (flags & ETHARP_FLAG_STATIC_ENTRY) {
476 /* record static type */
477 arp_table[i].state = ETHARP_STATE_STATIC;
478 } else if (arp_table[i].state == ETHARP_STATE_STATIC) {
479 /* found entry is a static type, don't overwrite it */
480 return ERR_VAL;
481 } else
482 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
483 {
484 /* mark it stable */
485 arp_table[i].state = ETHARP_STATE_STABLE;
486 }
487
488 /* record network interface */
489 arp_table[i].netif = netif;
490 /* insert in SNMP ARP index tree */
491 mib2_add_arp_entry(netif, &arp_table[i].ipaddr);
492
493 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: updating stable entry %"S16_F"\n", i));
494 /* update address */
495 SMEMCPY(&arp_table[i].ethaddr, ethaddr, ETH_HWADDR_LEN);
496 /* reset time stamp */
497 arp_table[i].ctime = 0;
498 /* this is where we will send out queued packets! */
499 #if ARP_QUEUEING
500 while (arp_table[i].q != NULL) {
501 struct pbuf *p;
502 /* remember remainder of queue */
503 struct etharp_q_entry *q = arp_table[i].q;
504 /* pop first item off the queue */
505 arp_table[i].q = q->next;
506 /* get the packet pointer */
507 p = q->p;
508 /* now queue entry can be freed */
509 memp_free(MEMP_ARP_QUEUE, q);
510 #else /* ARP_QUEUEING */
511 if (arp_table[i].q != NULL) {
512 struct pbuf *p = arp_table[i].q;
513 arp_table[i].q = NULL;
514 #endif /* ARP_QUEUEING */
515 /* send the queued IP packet */
516 ethernet_output(netif, p, (struct eth_addr *)(netif->hwaddr), ethaddr, ETHTYPE_IP);
517 /* free the queued IP packet */
518 pbuf_free(p);
519 }
520 return ERR_OK;
521 }
522
523 #if ETHARP_SUPPORT_STATIC_ENTRIES
524 /** Add a new static entry to the ARP table. If an entry exists for the
525 * specified IP address, this entry is overwritten.
526 * If packets are queued for the specified IP address, they are sent out.
527 *
528 * @param ipaddr IP address for the new static entry
529 * @param ethaddr ethernet address for the new static entry
530 * @return See return values of etharp_add_static_entry
531 */
532 err_t
533 etharp_add_static_entry(const ip4_addr_t *ipaddr, struct eth_addr *ethaddr)
534 {
535 struct netif *netif;
536 LWIP_ASSERT_CORE_LOCKED();
537 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_add_static_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",
538 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
539 (u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2],
540 (u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5]));
541 #ifdef LOSCFG_NET_CONTAINER
542 netif = ip4_route(ipaddr, get_root_net_group());
543 #else
544 netif = ip4_route(ipaddr);
545 #endif
546 if (netif == NULL) {
547 return ERR_RTE;
548 }
549
550 return etharp_update_arp_entry(netif, ipaddr, ethaddr, ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY);
551 }
552
553 /** Remove a static entry from the ARP table previously added with a call to
554 * etharp_add_static_entry.
555 *
556 * @param ipaddr IP address of the static entry to remove
557 * @return ERR_OK: entry removed
558 * ERR_MEM: entry wasn't found
559 * ERR_ARG: entry wasn't a static entry but a dynamic one
560 */
561 err_t
562 etharp_remove_static_entry(const ip4_addr_t *ipaddr)
563 {
564 s16_t i;
565 LWIP_ASSERT_CORE_LOCKED();
566 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_remove_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
567 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
568
569 /* find or create ARP entry */
570 i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY, NULL);
571 /* bail out if no entry could be found */
572 if (i < 0) {
573 return (err_t)i;
574 }
575
576 if (arp_table[i].state != ETHARP_STATE_STATIC) {
577 /* entry wasn't a static entry, cannot remove it */
578 return ERR_ARG;
579 }
580 /* entry found, free it */
581 etharp_free_entry(i);
582 return ERR_OK;
583 }
584 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
585
586 /**
587 * Remove all ARP table entries of the specified netif.
588 *
589 * @param netif points to a network interface
590 */
591 void
592 etharp_cleanup_netif(struct netif *netif)
593 {
594 int i;
595
596 for (i = 0; i < ARP_TABLE_SIZE; ++i) {
597 u8_t state = arp_table[i].state;
598 if ((state != ETHARP_STATE_EMPTY) && (arp_table[i].netif == netif)) {
599 etharp_free_entry(i);
600 }
601 }
602 }
603
604 /**
605 * Finds (stable) ethernet/IP address pair from ARP table
606 * using interface and IP address index.
607 * @note the addresses in the ARP table are in network order!
608 *
609 * @param netif points to interface index
610 * @param ipaddr points to the (network order) IP address index
611 * @param eth_ret points to return pointer
612 * @param ip_ret points to return pointer
613 * @return table index if found, -1 otherwise
614 */
615 ssize_t
616 etharp_find_addr(struct netif *netif, const ip4_addr_t *ipaddr,
617 struct eth_addr **eth_ret, const ip4_addr_t **ip_ret)
618 {
619 s16_t i;
620
621 LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL",
622 eth_ret != NULL && ip_ret != NULL);
623
624 LWIP_UNUSED_ARG(netif);
625
626 i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY, netif);
627 if ((i >= 0) && (arp_table[i].state >= ETHARP_STATE_STABLE)) {
628 *eth_ret = &arp_table[i].ethaddr;
629 *ip_ret = &arp_table[i].ipaddr;
630 return i;
631 }
632 return -1;
633 }
634
635 /**
636 * Possibility to iterate over stable ARP table entries
637 *
638 * @param i entry number, 0 to ARP_TABLE_SIZE
639 * @param ipaddr return value: IP address
640 * @param netif return value: points to interface
641 * @param eth_ret return value: ETH address
642 * @return 1 on valid index, 0 otherwise
643 */
644 int
645 etharp_get_entry(size_t i, ip4_addr_t **ipaddr, struct netif **netif, struct eth_addr **eth_ret)
646 {
647 LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL);
648 LWIP_ASSERT("netif != NULL", netif != NULL);
649 LWIP_ASSERT("eth_ret != NULL", eth_ret != NULL);
650
651 if ((i < ARP_TABLE_SIZE) && (arp_table[i].state >= ETHARP_STATE_STABLE)) {
652 *ipaddr = &arp_table[i].ipaddr;
653 *netif = arp_table[i].netif;
654 *eth_ret = &arp_table[i].ethaddr;
655 return 1;
656 } else {
657 return 0;
658 }
659 }
660
661 /**
662 * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache
663 * send out queued IP packets. Updates cache with snooped address pairs.
664 *
665 * Should be called for incoming ARP packets. The pbuf in the argument
666 * is freed by this function.
667 *
668 * @param p The ARP packet that arrived on netif. Is freed by this function.
669 * @param netif The lwIP network interface on which the ARP packet pbuf arrived.
670 *
671 * @see pbuf_free()
672 */
673 void
674 etharp_input(struct pbuf *p, struct netif *netif)
675 {
676 struct etharp_hdr *hdr;
677 /* these are aligned properly, whereas the ARP header fields might not be */
678 ip4_addr_t sipaddr, dipaddr;
679 u8_t for_us, from_us;
680
681 LWIP_ASSERT_CORE_LOCKED();
682
683 LWIP_ERROR("netif != NULL", (netif != NULL), return;);
684
685 hdr = (struct etharp_hdr *)p->payload;
686
687 /* RFC 826 "Packet Reception": */
688 if ((hdr->hwtype != PP_HTONS(LWIP_IANA_HWTYPE_ETHERNET)) ||
689 (hdr->hwlen != ETH_HWADDR_LEN) ||
690 (hdr->protolen != sizeof(ip4_addr_t)) ||
691 (hdr->proto != PP_HTONS(ETHTYPE_IP))) {
692 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
693 ("etharp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n",
694 hdr->hwtype, (u16_t)hdr->hwlen, hdr->proto, (u16_t)hdr->protolen));
695 ETHARP_STATS_INC(etharp.proterr);
696 ETHARP_STATS_INC(etharp.drop);
697 pbuf_free(p);
698 return;
699 }
700 ETHARP_STATS_INC(etharp.recv);
701
702 #if LWIP_ACD
703 /* We have to check if a host already has configured our ip address and
704 * continuously check if there is a host with this IP-address so we can
705 * detect collisions.
706 * acd_arp_reply ensures the detection of conflicts. It will handle possible
707 * defending or retreating and will make sure a new IP address is selected.
708 * etharp_input does not need to handle packets that originate "from_us".
709 */
710 acd_arp_reply(netif, hdr);
711 #endif /* LWIP_ACD */
712
713 /* Copy struct ip4_addr_wordaligned to aligned ip4_addr, to support compilers without
714 * structure packing (not using structure copy which breaks strict-aliasing rules). */
715 IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&sipaddr, &hdr->sipaddr);
716 IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&dipaddr, &hdr->dipaddr);
717
718 /* this interface is not configured? */
719 if (ip4_addr_isany_val(*netif_ip4_addr(netif))) {
720 for_us = 0;
721 from_us = 0;
722 } else {
723 /* ARP packet directed to us? */
724 for_us = (u8_t)ip4_addr_eq(&dipaddr, netif_ip4_addr(netif));
725 /* ARP packet from us? */
726 from_us = (u8_t)ip4_addr_eq(&sipaddr, netif_ip4_addr(netif));
727 }
728
729 /* ARP message directed to us?
730 -> add IP address in ARP cache; assume requester wants to talk to us,
731 can result in directly sending the queued packets for this host.
732 ARP message not directed to us?
733 -> update the source IP address in the cache, if present */
734 etharp_update_arp_entry(netif, &sipaddr, &(hdr->shwaddr),
735 for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY);
736
737 /* now act on the message itself */
738 switch (hdr->opcode) {
739 /* ARP request? */
740 case PP_HTONS(ARP_REQUEST):
741 /* ARP request. If it asked for our address, we send out a
742 * reply. In any case, we time-stamp any existing ARP entry,
743 * and possibly send out an IP packet that was queued on it. */
744
745 LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP request\n"));
746 /* ARP request for our address? */
747 if (for_us && !from_us) {
748 /* send ARP response */
749 etharp_raw(netif,
750 (struct eth_addr *)netif->hwaddr, &hdr->shwaddr,
751 (struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif),
752 &hdr->shwaddr, &sipaddr,
753 ARP_REPLY);
754 /* we are not configured? */
755 } else if (ip4_addr_isany_val(*netif_ip4_addr(netif))) {
756 /* { for_us == 0 and netif->ip_addr.addr == 0 } */
757 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: we are unconfigured, ARP request ignored.\n"));
758 /* request was not directed to us */
759 } else {
760 /* { for_us == 0 and netif->ip_addr.addr != 0 } */
761 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP request was not for us.\n"));
762 }
763 break;
764 case PP_HTONS(ARP_REPLY):
765 /* ARP reply. We already updated the ARP cache earlier. */
766 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP reply\n"));
767 break;
768 default:
769 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP unknown opcode type %"S16_F"\n", lwip_htons(hdr->opcode)));
770 ETHARP_STATS_INC(etharp.err);
771 break;
772 }
773 /* free ARP packet */
774 pbuf_free(p);
775 }
776
777 /** Just a small helper function that sends a pbuf to an ethernet address
778 * in the arp_table specified by the index 'arp_idx'.
779 */
780 static err_t
781 etharp_output_to_arp_index(struct netif *netif, struct pbuf *q, netif_addr_idx_t arp_idx)
782 {
783 LWIP_ASSERT("arp_table[arp_idx].state >= ETHARP_STATE_STABLE",
784 arp_table[arp_idx].state >= ETHARP_STATE_STABLE);
785 /* if arp table entry is about to expire: re-request it,
786 but only if its state is ETHARP_STATE_STABLE to prevent flooding the
787 network with ARP requests if this address is used frequently. */
788 if (arp_table[arp_idx].state == ETHARP_STATE_STABLE) {
789 if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_BROADCAST) {
790 /* issue a standard request using broadcast */
791 if (etharp_request(netif, &arp_table[arp_idx].ipaddr) == ERR_OK) {
792 arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING_1;
793 }
794 } else if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_UNICAST) {
795 /* issue a unicast request (for 15 seconds) to prevent unnecessary broadcast */
796 if (etharp_request_dst(netif, &arp_table[arp_idx].ipaddr, &arp_table[arp_idx].ethaddr) == ERR_OK) {
797 arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING_1;
798 }
799 }
800 }
801
802 return ethernet_output(netif, q, (struct eth_addr *)(netif->hwaddr), &arp_table[arp_idx].ethaddr, ETHTYPE_IP);
803 }
804
805 /**
806 * Resolve and fill-in Ethernet address header for outgoing IP packet.
807 *
808 * For IP multicast and broadcast, corresponding Ethernet addresses
809 * are selected and the packet is transmitted on the link.
810 *
811 * For unicast addresses, the packet is submitted to etharp_query(). In
812 * case the IP address is outside the local network, the IP address of
813 * the gateway is used.
814 *
815 * @param netif The lwIP network interface which the IP packet will be sent on.
816 * @param q The pbuf(s) containing the IP packet to be sent.
817 * @param ipaddr The IP address of the packet destination.
818 *
819 * @return
820 * - ERR_RTE No route to destination (no gateway to external networks),
821 * or the return type of either etharp_query() or ethernet_output().
822 */
823 err_t
824 etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr)
825 {
826 const struct eth_addr *dest;
827 struct eth_addr mcastaddr;
828 const ip4_addr_t *dst_addr = ipaddr;
829
830 LWIP_ASSERT_CORE_LOCKED();
831 LWIP_ASSERT("netif != NULL", netif != NULL);
832 LWIP_ASSERT("q != NULL", q != NULL);
833 LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL);
834
835 /* Determine on destination hardware address. Broadcasts and multicasts
836 * are special, other IP addresses are looked up in the ARP table. */
837
838 /* broadcast destination IP address? */
839 if (ip4_addr_isbroadcast(ipaddr, netif)) {
840 /* broadcast on Ethernet also */
841 dest = (const struct eth_addr *)ðbroadcast;
842 /* multicast destination IP address? */
843 } else if (ip4_addr_ismulticast(ipaddr)) {
844 /* Hash IP multicast address to MAC address.*/
845 mcastaddr.addr[0] = LL_IP4_MULTICAST_ADDR_0;
846 mcastaddr.addr[1] = LL_IP4_MULTICAST_ADDR_1;
847 mcastaddr.addr[2] = LL_IP4_MULTICAST_ADDR_2;
848 mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
849 mcastaddr.addr[4] = ip4_addr3(ipaddr);
850 mcastaddr.addr[5] = ip4_addr4(ipaddr);
851 /* destination Ethernet address is multicast */
852 dest = &mcastaddr;
853 /* unicast destination IP address? */
854 } else {
855 netif_addr_idx_t i;
856 /* outside local network? if so, this can neither be a global broadcast nor
857 a subnet broadcast. */
858 if (!ip4_addr_net_eq(ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) &&
859 !ip4_addr_islinklocal(ipaddr)) {
860 #if LWIP_AUTOIP
861 struct ip_hdr *iphdr = LWIP_ALIGNMENT_CAST(struct ip_hdr *, q->payload);
862 /* According to RFC 3297, chapter 2.6.2 (Forwarding Rules), a packet with
863 a link-local source address must always be "directly to its destination
864 on the same physical link. The host MUST NOT send the packet to any
865 router for forwarding". */
866 if (!ip4_addr_islinklocal(&iphdr->src))
867 #endif /* LWIP_AUTOIP */
868 {
869 #ifdef LWIP_HOOK_ETHARP_GET_GW
870 /* For advanced routing, a single default gateway might not be enough, so get
871 the IP address of the gateway to handle the current destination address. */
872 dst_addr = LWIP_HOOK_ETHARP_GET_GW(netif, ipaddr);
873 if (dst_addr == NULL)
874 #endif /* LWIP_HOOK_ETHARP_GET_GW */
875 {
876 /* interface has default gateway? */
877 if (!ip4_addr_isany_val(*netif_ip4_gw(netif))) {
878 /* send to hardware address of default gateway IP address */
879 dst_addr = netif_ip4_gw(netif);
880 /* no default gateway available */
881 } else {
882 /* no route to destination error (default gateway missing) */
883 return ERR_RTE;
884 }
885 }
886 }
887 }
888 #if LWIP_NETIF_HWADDRHINT
889 if (netif->hints != NULL) {
890 /* per-pcb cached entry was given */
891 netif_addr_idx_t etharp_cached_entry = netif->hints->addr_hint;
892 if (etharp_cached_entry < ARP_TABLE_SIZE) {
893 #endif /* LWIP_NETIF_HWADDRHINT */
894 if ((arp_table[etharp_cached_entry].state >= ETHARP_STATE_STABLE) &&
895 #if ETHARP_TABLE_MATCH_NETIF
896 (arp_table[etharp_cached_entry].netif == netif) &&
897 #endif
898 (ip4_addr_eq(dst_addr, &arp_table[etharp_cached_entry].ipaddr))) {
899 /* the per-pcb-cached entry is stable and the right one! */
900 ETHARP_STATS_INC(etharp.cachehit);
901 return etharp_output_to_arp_index(netif, q, etharp_cached_entry);
902 }
903 #if LWIP_NETIF_HWADDRHINT
904 }
905 }
906 #endif /* LWIP_NETIF_HWADDRHINT */
907
908 /* find stable entry: do this here since this is a critical path for
909 throughput and etharp_find_entry() is kind of slow */
910 for (i = 0; i < ARP_TABLE_SIZE; i++) {
911 if ((arp_table[i].state >= ETHARP_STATE_STABLE) &&
912 #if ETHARP_TABLE_MATCH_NETIF
913 (arp_table[i].netif == netif) &&
914 #endif
915 (ip4_addr_eq(dst_addr, &arp_table[i].ipaddr))) {
916 /* found an existing, stable entry */
917 ETHARP_SET_ADDRHINT(netif, i);
918 return etharp_output_to_arp_index(netif, q, i);
919 }
920 }
921 /* no stable entry found, use the (slower) query function:
922 queue on destination Ethernet address belonging to ipaddr */
923 return etharp_query(netif, dst_addr, q);
924 }
925
926 /* continuation for multicast/broadcast destinations */
927 /* obtain source Ethernet address of the given interface */
928 /* send packet directly on the link */
929 return ethernet_output(netif, q, (struct eth_addr *)(netif->hwaddr), dest, ETHTYPE_IP);
930 }
931
932 /**
933 * Send an ARP request for the given IP address and/or queue a packet.
934 *
935 * If the IP address was not yet in the cache, a pending ARP cache entry
936 * is added and an ARP request is sent for the given address. The packet
937 * is queued on this entry.
938 *
939 * If the IP address was already pending in the cache, a new ARP request
940 * is sent for the given address. The packet is queued on this entry.
941 *
942 * If the IP address was already stable in the cache, and a packet is
943 * given, it is directly sent and no ARP request is sent out.
944 *
945 * If the IP address was already stable in the cache, and no packet is
946 * given, an ARP request is sent out.
947 *
948 * @param netif The lwIP network interface on which ipaddr
949 * must be queried for.
950 * @param ipaddr The IP address to be resolved.
951 * @param q If non-NULL, a pbuf that must be delivered to the IP address.
952 * q is not freed by this function.
953 *
954 * @note q must only be ONE packet, not a packet queue!
955 *
956 * @return
957 * - ERR_BUF Could not make room for Ethernet header.
958 * - ERR_MEM Hardware address unknown, and no more ARP entries available
959 * to query for address or queue the packet.
960 * - ERR_MEM Could not queue packet due to memory shortage.
961 * - ERR_RTE No route to destination (no gateway to external networks).
962 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
963 *
964 */
965 err_t
966 etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q)
967 {
968 struct eth_addr *srcaddr = (struct eth_addr *)netif->hwaddr;
969 err_t result = ERR_MEM;
970 int is_new_entry = 0;
971 s16_t i_err;
972 netif_addr_idx_t i;
973
974 /* non-unicast address? */
975 if (ip4_addr_isbroadcast(ipaddr, netif) ||
976 ip4_addr_ismulticast(ipaddr) ||
977 ip4_addr_isany(ipaddr)) {
978 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
979 return ERR_ARG;
980 }
981
982 /* find entry in ARP cache, ask to create entry if queueing packet */
983 i_err = etharp_find_entry(ipaddr, ETHARP_FLAG_TRY_HARD, netif);
984
985 /* could not find or create entry? */
986 if (i_err < 0) {
987 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
988 if (q) {
989 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n"));
990 ETHARP_STATS_INC(etharp.memerr);
991 }
992 return (err_t)i_err;
993 }
994 LWIP_ASSERT("type overflow", (size_t)i_err < NETIF_ADDR_IDX_MAX);
995 i = (netif_addr_idx_t)i_err;
996
997 /* mark a fresh entry as pending (we just sent a request) */
998 if (arp_table[i].state == ETHARP_STATE_EMPTY) {
999 is_new_entry = 1;
1000 arp_table[i].state = ETHARP_STATE_PENDING;
1001 /* record network interface for re-sending arp request in etharp_tmr */
1002 arp_table[i].netif = netif;
1003 }
1004
1005 /* { i is either a STABLE or (new or existing) PENDING entry } */
1006 LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
1007 ((arp_table[i].state == ETHARP_STATE_PENDING) ||
1008 (arp_table[i].state >= ETHARP_STATE_STABLE)));
1009
1010 /* do we have a new entry? or an implicit query request? */
1011 if (is_new_entry || (q == NULL)) {
1012 /* try to resolve it; send out ARP request */
1013 result = etharp_request(netif, ipaddr);
1014 if (result != ERR_OK) {
1015 /* ARP request couldn't be sent */
1016 /* We don't re-send arp request in etharp_tmr, but we still queue packets,
1017 since this failure could be temporary, and the next packet calling
1018 etharp_query again could lead to sending the queued packets. */
1019 } else {
1020 /* ARP request successfully sent */
1021 if ((arp_table[i].state == ETHARP_STATE_PENDING) && !is_new_entry) {
1022 /* A new ARP request has been sent for a pending entry. Reset the ctime to
1023 not let it expire too fast. */
1024 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: reset ctime for entry %"S16_F"\n", (s16_t)i));
1025 arp_table[i].ctime = 0;
1026 }
1027 }
1028 if (q == NULL) {
1029 return result;
1030 }
1031 }
1032
1033 /* packet given? */
1034 LWIP_ASSERT("q != NULL", q != NULL);
1035 /* stable entry? */
1036 if (arp_table[i].state >= ETHARP_STATE_STABLE) {
1037 /* we have a valid IP->Ethernet address mapping */
1038 ETHARP_SET_ADDRHINT(netif, i);
1039 /* send the packet */
1040 result = ethernet_output(netif, q, srcaddr, &(arp_table[i].ethaddr), ETHTYPE_IP);
1041 /* pending entry? (either just created or already pending */
1042 } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
1043 /* entry is still pending, queue the given packet 'q' */
1044 struct pbuf *p;
1045 int copy_needed = 0;
1046 /* IF q includes a pbuf that must be copied, copy the whole chain into a
1047 * new PBUF_RAM. See the definition of PBUF_NEEDS_COPY for details. */
1048 p = q;
1049 while (p) {
1050 LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == NULL));
1051 if (PBUF_NEEDS_COPY(p)) {
1052 copy_needed = 1;
1053 break;
1054 }
1055 p = p->next;
1056 }
1057 if (copy_needed) {
1058 /* copy the whole packet into new pbufs */
1059 p = pbuf_clone(PBUF_LINK, PBUF_RAM, q);
1060 } else {
1061 /* referencing the old pbuf is enough */
1062 p = q;
1063 pbuf_ref(p);
1064 }
1065 /* packet could be taken over? */
1066 if (p != NULL) {
1067 /* queue packet ... */
1068 #if ARP_QUEUEING
1069 struct etharp_q_entry *new_entry;
1070 /* allocate a new arp queue entry */
1071 new_entry = (struct etharp_q_entry *)memp_malloc(MEMP_ARP_QUEUE);
1072 if (new_entry != NULL) {
1073 unsigned int qlen = 0;
1074 new_entry->next = NULL;
1075 new_entry->p = p;
1076 if (arp_table[i].q != NULL) {
1077 /* queue was already existent, append the new entry to the end */
1078 struct etharp_q_entry *r;
1079 r = arp_table[i].q;
1080 qlen++;
1081 while (r->next != NULL) {
1082 r = r->next;
1083 qlen++;
1084 }
1085 r->next = new_entry;
1086 } else {
1087 /* queue did not exist, first item in queue */
1088 arp_table[i].q = new_entry;
1089 }
1090 #if ARP_QUEUE_LEN
1091 if (qlen >= ARP_QUEUE_LEN) {
1092 struct etharp_q_entry *old;
1093 old = arp_table[i].q;
1094 arp_table[i].q = arp_table[i].q->next;
1095 pbuf_free(old->p);
1096 memp_free(MEMP_ARP_QUEUE, old);
1097 }
1098 #endif
1099 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"U16_F"\n", (void *)q, i));
1100 result = ERR_OK;
1101 } else {
1102 /* the pool MEMP_ARP_QUEUE is empty */
1103 pbuf_free(p);
1104 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q));
1105 result = ERR_MEM;
1106 }
1107 #else /* ARP_QUEUEING */
1108 /* always queue one packet per ARP request only, freeing a previously queued packet */
1109 if (arp_table[i].q != NULL) {
1110 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: dropped previously queued packet %p for ARP entry %"U16_F"\n", (void *)q, (u16_t)i));
1111 pbuf_free(arp_table[i].q);
1112 }
1113 arp_table[i].q = p;
1114 result = ERR_OK;
1115 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"U16_F"\n", (void *)q, (u16_t)i));
1116 #endif /* ARP_QUEUEING */
1117 } else {
1118 ETHARP_STATS_INC(etharp.memerr);
1119 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q));
1120 result = ERR_MEM;
1121 }
1122 }
1123 return result;
1124 }
1125
1126 /**
1127 * Send a raw ARP packet (opcode and all addresses can be modified)
1128 *
1129 * @param netif the lwip network interface on which to send the ARP packet
1130 * @param ethsrc_addr the source MAC address for the ethernet header
1131 * @param ethdst_addr the destination MAC address for the ethernet header
1132 * @param hwsrc_addr the source MAC address for the ARP protocol header
1133 * @param ipsrc_addr the source IP address for the ARP protocol header
1134 * @param hwdst_addr the destination MAC address for the ARP protocol header
1135 * @param ipdst_addr the destination IP address for the ARP protocol header
1136 * @param opcode the type of the ARP packet
1137 * @return ERR_OK if the ARP packet has been sent
1138 * ERR_MEM if the ARP packet couldn't be allocated
1139 * any other err_t on failure
1140 */
1141 static err_t
1142 etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr,
1143 const struct eth_addr *ethdst_addr,
1144 const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr,
1145 const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr,
1146 const u16_t opcode)
1147 {
1148 struct pbuf *p;
1149 err_t result = ERR_OK;
1150 struct etharp_hdr *hdr;
1151
1152 LWIP_ASSERT("netif != NULL", netif != NULL);
1153
1154 /* allocate a pbuf for the outgoing ARP request packet */
1155 p = pbuf_alloc(PBUF_LINK, SIZEOF_ETHARP_HDR, PBUF_RAM);
1156 /* could allocate a pbuf for an ARP request? */
1157 if (p == NULL) {
1158 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
1159 ("etharp_raw: could not allocate pbuf for ARP request.\n"));
1160 ETHARP_STATS_INC(etharp.memerr);
1161 return ERR_MEM;
1162 }
1163 LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr",
1164 (p->len >= SIZEOF_ETHARP_HDR));
1165
1166 hdr = (struct etharp_hdr *)p->payload;
1167 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n"));
1168 hdr->opcode = lwip_htons(opcode);
1169
1170 LWIP_ASSERT("netif->hwaddr_len must be the same as ETH_HWADDR_LEN for etharp!",
1171 (netif->hwaddr_len == ETH_HWADDR_LEN));
1172
1173 /* Write the ARP MAC-Addresses */
1174 SMEMCPY(&hdr->shwaddr, hwsrc_addr, ETH_HWADDR_LEN);
1175 SMEMCPY(&hdr->dhwaddr, hwdst_addr, ETH_HWADDR_LEN);
1176 /* Copy struct ip4_addr_wordaligned to aligned ip4_addr, to support compilers without
1177 * structure packing. */
1178 IPADDR_WORDALIGNED_COPY_FROM_IP4_ADDR_T(&hdr->sipaddr, ipsrc_addr);
1179 IPADDR_WORDALIGNED_COPY_FROM_IP4_ADDR_T(&hdr->dipaddr, ipdst_addr);
1180
1181 hdr->hwtype = PP_HTONS(LWIP_IANA_HWTYPE_ETHERNET);
1182 hdr->proto = PP_HTONS(ETHTYPE_IP);
1183 /* set hwlen and protolen */
1184 hdr->hwlen = ETH_HWADDR_LEN;
1185 hdr->protolen = sizeof(ip4_addr_t);
1186
1187 /* send ARP query */
1188 #if LWIP_AUTOIP
1189 /* If we are using Link-Local, all ARP packets that contain a Link-Local
1190 * 'sender IP address' MUST be sent using link-layer broadcast instead of
1191 * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
1192 if (ip4_addr_islinklocal(ipsrc_addr)) {
1193 ethernet_output(netif, p, ethsrc_addr, ðbroadcast, ETHTYPE_ARP);
1194 } else
1195 #endif /* LWIP_AUTOIP */
1196 {
1197 ethernet_output(netif, p, ethsrc_addr, ethdst_addr, ETHTYPE_ARP);
1198 }
1199
1200 ETHARP_STATS_INC(etharp.xmit);
1201 /* free ARP query packet */
1202 pbuf_free(p);
1203 p = NULL;
1204 /* could not allocate pbuf for ARP request */
1205
1206 return result;
1207 }
1208
1209 /**
1210 * Send an ARP request packet asking for ipaddr to a specific eth address.
1211 * Used to send unicast request to refresh the ARP table just before an entry
1212 * times out
1213 *
1214 * @param netif the lwip network interface on which to send the request
1215 * @param ipaddr the IP address for which to ask
1216 * @param hw_dst_addr the ethernet address to send this packet to
1217 * @return ERR_OK if the request has been sent
1218 * ERR_MEM if the ARP packet couldn't be allocated
1219 * any other err_t on failure
1220 */
1221 static err_t
1222 etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr *hw_dst_addr)
1223 {
1224 return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, hw_dst_addr,
1225 (struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif), ðzero,
1226 ipaddr, ARP_REQUEST);
1227 }
1228
1229 /**
1230 * Send an ARP request packet asking for ipaddr.
1231 *
1232 * @param netif the lwip network interface on which to send the request
1233 * @param ipaddr the IP address for which to ask
1234 * @return ERR_OK if the request has been sent
1235 * ERR_MEM if the ARP packet couldn't be allocated
1236 * any other err_t on failure
1237 */
1238 err_t
1239 etharp_request(struct netif *netif, const ip4_addr_t *ipaddr)
1240 {
1241 LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n"));
1242 return etharp_request_dst(netif, ipaddr, ðbroadcast);
1243 }
1244
1245 #if LWIP_ACD
1246 /**
1247 * Send an ARP request packet probing for an ipaddr.
1248 * Used to send probe messages for address conflict detection.
1249 *
1250 * @param netif the lwip network interface on which to send the request
1251 * @param ipaddr the IP address to probe
1252 * @return ERR_OK if the request has been sent
1253 * ERR_MEM if the ARP packet couldn't be allocated
1254 * any other err_t on failure
1255 */
1256 err_t
1257 etharp_acd_probe(struct netif *netif, const ip4_addr_t *ipaddr)
1258 {
1259 return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, ðbroadcast,
1260 (struct eth_addr *)netif->hwaddr, IP4_ADDR_ANY4, ðzero,
1261 ipaddr, ARP_REQUEST);
1262 }
1263
1264 /**
1265 * Send an ARP request packet announcing an ipaddr.
1266 * Used to send announce messages for address conflict detection.
1267 *
1268 * @param netif the lwip network interface on which to send the request
1269 * @param ipaddr the IP address to announce
1270 * @return ERR_OK if the request has been sent
1271 * ERR_MEM if the ARP packet couldn't be allocated
1272 * any other err_t on failure
1273 */
1274 err_t
1275 etharp_acd_announce(struct netif *netif, const ip4_addr_t *ipaddr)
1276 {
1277 return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, ðbroadcast,
1278 (struct eth_addr *)netif->hwaddr, ipaddr, ðzero,
1279 ipaddr, ARP_REQUEST);
1280 }
1281 #endif /* LWIP_ACD */
1282
1283 #endif /* LWIP_IPV4 && LWIP_ARP */
1284