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