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