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