uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
etharp.c
Go to the documentation of this file.
1 
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 || LWIP_ETHERNET
49 
50 #include "lwip/ip_addr.h"
51 #include "lwip/def.h"
52 #include "lwip/ip.h"
53 #include "lwip/stats.h"
54 #include "lwip/snmp.h"
55 #include "lwip/dhcp.h"
56 #include "lwip/autoip.h"
57 #include "netif/etharp.h"
58 
59 #if PPPOE_SUPPORT
60 #include "netif/ppp_oe.h"
61 #endif /* PPPOE_SUPPORT */
62 
63 #include <string.h>
64 
65 const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
66 const struct eth_addr ethzero = {{0,0,0,0,0,0}};
67 
68 #if LWIP_ARP /* don't build if not configured for use in lwipopts.h */
69 
74 #define ARP_MAXAGE 240
75 
82 #define ARP_MAXPENDING 2
83 
84 #define HWTYPE_ETHERNET 1
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  struct etharp_q_entry *q;
96 #else /* ARP_QUEUEING */
97 
98  struct pbuf *q;
99 #endif /* ARP_QUEUEING */
100  ip_addr_t ipaddr;
101  struct eth_addr ethaddr;
102 #if LWIP_SNMP
103  struct netif *netif;
104 #endif /* LWIP_SNMP */
105  u8_t state;
106  u8_t ctime;
107 #if ETHARP_SUPPORT_STATIC_ENTRIES
108  u8_t static_entry;
109 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
110 };
111 
112 static struct etharp_entry arp_table[ARP_TABLE_SIZE];
113 
114 #if !LWIP_NETIF_HWADDRHINT
115 static u8_t etharp_cached_entry;
116 #endif /* !LWIP_NETIF_HWADDRHINT */
117 
120 #define ETHARP_FLAG_TRY_HARD 1
121 #define ETHARP_FLAG_FIND_ONLY 2
122 #define ETHARP_FLAG_STATIC_ENTRY 4
123 
124 #if LWIP_NETIF_HWADDRHINT
125 #define ETHARP_SET_HINT(netif, hint) if (((netif) != NULL) && ((netif)->addr_hint != NULL)) \
126  *((netif)->addr_hint) = (hint);
127 #else /* LWIP_NETIF_HWADDRHINT */
128 #define ETHARP_SET_HINT(netif, hint) (etharp_cached_entry = (hint))
129 #endif /* LWIP_NETIF_HWADDRHINT */
130 
131 static err_t update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags);
132 
133 
134 /* Some checks, instead of etharp_init(): */
135 #if (LWIP_ARP && (ARP_TABLE_SIZE > 0x7f))
136  #error "ARP_TABLE_SIZE must fit in an s8_t, you have to reduce it in your lwipopts.h"
137 #endif
138 
139 
140 #if ARP_QUEUEING
141 
146 static void
147 free_etharp_q(struct etharp_q_entry *q)
148 {
149  struct etharp_q_entry *r;
150  LWIP_ASSERT("q != NULL", q != NULL);
151  LWIP_ASSERT("q->p != NULL", q->p != NULL);
152  while (q) {
153  r = q;
154  q = q->next;
155  LWIP_ASSERT("r->p != NULL", (r->p != NULL));
156  pbuf_free(r->p);
157  memp_free(MEMP_ARP_QUEUE, r);
158  }
159 }
160 #else /* ARP_QUEUEING */
161 
163 #define free_etharp_q(q) pbuf_free(q)
164 
165 #endif /* ARP_QUEUEING */
166 
168 static void
169 free_entry(int i)
170 {
171  /* remove from SNMP ARP index tree */
172  snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr);
173  /* and empty packet queue */
174  if (arp_table[i].q != NULL) {
175  /* remove all queued packets */
176  LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q)));
177  free_etharp_q(arp_table[i].q);
178  arp_table[i].q = NULL;
179  }
180  /* recycle entry for re-use */
181  arp_table[i].state = ETHARP_STATE_EMPTY;
182 #if ETHARP_SUPPORT_STATIC_ENTRIES
183  arp_table[i].static_entry = 0;
184 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
185 #ifdef LWIP_DEBUG
186  /* for debugging, clean out the complete entry */
187  arp_table[i].ctime = 0;
188 #if LWIP_SNMP
189  arp_table[i].netif = NULL;
190 #endif /* LWIP_SNMP */
191  ip_addr_set_zero(&arp_table[i].ipaddr);
192  arp_table[i].ethaddr = ethzero;
193 #endif /* LWIP_DEBUG */
194 }
195 
202 void
203 etharp_tmr(void)
204 {
205  u8_t i;
206 
207  LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n"));
208  /* remove expired entries from the ARP table */
209  for (i = 0; i < ARP_TABLE_SIZE; ++i) {
210  u8_t state = arp_table[i].state;
211  if (state != ETHARP_STATE_EMPTY
213  && (arp_table[i].static_entry == 0)
214 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
215  ) {
216  arp_table[i].ctime++;
217  if ((arp_table[i].ctime >= ARP_MAXAGE) ||
218  ((arp_table[i].state == ETHARP_STATE_PENDING) &&
219  (arp_table[i].ctime >= ARP_MAXPENDING))) {
220  /* pending or stable entry has become old! */
221  LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %"U16_F".\n",
222  arp_table[i].state == ETHARP_STATE_STABLE ? "stable" : "pending", (u16_t)i));
223  /* clean up entries that have just been expired */
224  free_entry(i);
225  }
226 #if ARP_QUEUEING
227  /* still pending entry? (not expired) */
228  if (arp_table[i].state == ETHARP_STATE_PENDING) {
229  /* resend an ARP query here? */
230  }
231 #endif /* ARP_QUEUEING */
232  }
233  }
234 }
235 
257 static s8_t
258 find_entry(ip_addr_t *ipaddr, u8_t flags)
259 {
260  s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
261  s8_t empty = ARP_TABLE_SIZE;
262  u8_t i = 0, age_pending = 0, age_stable = 0;
263  /* oldest entry with packets on queue */
264  s8_t old_queue = ARP_TABLE_SIZE;
265  /* its age */
266  u8_t age_queue = 0;
267 
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, ("find_entry: found empty entry %"U16_F"\n", (u16_t)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 && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) {
295  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: found matching entry %"U16_F"\n", (u16_t)i));
296  /* found exact IP address match, simply bail out */
297  return i;
298  }
299  /* pending entry? */
300  if (state == ETHARP_STATE_PENDING) {
301  /* pending with queued packets? */
302  if (arp_table[i].q != NULL) {
303  if (arp_table[i].ctime >= age_queue) {
304  old_queue = i;
305  age_queue = arp_table[i].ctime;
306  }
307  } else
308  /* pending without queued packets? */
309  {
310  if (arp_table[i].ctime >= age_pending) {
311  old_pending = i;
312  age_pending = arp_table[i].ctime;
313  }
314  }
315  /* stable entry? */
316  } else if (state == ETHARP_STATE_STABLE) {
317 #if ETHARP_SUPPORT_STATIC_ENTRIES
318  /* don't record old_stable for static entries since they never expire */
319  if (arp_table[i].static_entry == 0)
320 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
321  {
322  /* remember entry with oldest stable entry in oldest, its age in maxtime */
323  if (arp_table[i].ctime >= age_stable) {
324  old_stable = i;
325  age_stable = arp_table[i].ctime;
326  }
327  }
328  }
329  }
330  }
331  /* { we have no match } => try to create a new entry */
332 
333  /* don't create new entry, only search? */
334  if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) ||
335  /* or no empty entry found and not allowed to recycle? */
336  ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) {
337  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: no empty entry found and not allowed to recycle\n"));
338  return (s8_t)ERR_MEM;
339  }
340 
341  /* b) choose the least destructive entry to recycle:
342  * 1) empty entry
343  * 2) oldest stable entry
344  * 3) oldest pending entry without queued packets
345  * 4) oldest pending entry with queued packets
346  *
347  * { ETHARP_FLAG_TRY_HARD is set at this point }
348  */
349 
350  /* 1) empty entry available? */
351  if (empty < ARP_TABLE_SIZE) {
352  i = empty;
353  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting empty entry %"U16_F"\n", (u16_t)i));
354  } else {
355  /* 2) found recyclable stable entry? */
356  if (old_stable < ARP_TABLE_SIZE) {
357  /* recycle oldest stable*/
358  i = old_stable;
359  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest stable entry %"U16_F"\n", (u16_t)i));
360  /* no queued packets should exist on stable entries */
361  LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL);
362  /* 3) found recyclable pending entry without queued packets? */
363  } else if (old_pending < ARP_TABLE_SIZE) {
364  /* recycle oldest pending */
365  i = old_pending;
366  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest pending entry %"U16_F" (without queue)\n", (u16_t)i));
367  /* 4) found recyclable pending entry with queued packets? */
368  } else if (old_queue < ARP_TABLE_SIZE) {
369  /* recycle oldest pending (queued packets are free in free_entry) */
370  i = old_queue;
371  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)));
372  /* no empty or recyclable entries found */
373  } else {
374  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: no empty or recyclable entries found\n"));
375  return (s8_t)ERR_MEM;
376  }
377 
378  /* { empty or recyclable entry found } */
379  LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
380  free_entry(i);
381  }
382 
383  LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
384  LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY",
385  arp_table[i].state == ETHARP_STATE_EMPTY);
386 
387  /* IP address given? */
388  if (ipaddr != NULL) {
389  /* set IP address */
390  ip_addr_copy(arp_table[i].ipaddr, *ipaddr);
391  }
392  arp_table[i].ctime = 0;
393 #if ETHARP_SUPPORT_STATIC_ENTRIES
394  arp_table[i].static_entry = 0;
395 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
396  return (err_t)i;
397 }
398 
409 static err_t
410 etharp_send_ip(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst)
411 {
412  struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
413 
414  LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
415  (netif->hwaddr_len == ETHARP_HWADDR_LEN));
416  ETHADDR32_COPY(&ethhdr->dest, dst);
417  ETHADDR16_COPY(&ethhdr->src, src);
418  ethhdr->type = PP_HTONS(ETHTYPE_IP);
419  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_send_ip: sending packet %p\n", (void *)p));
420  /* send the packet */
421  return netif->linkoutput(netif, p);
422 }
423 
442 static err_t
443 update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)
444 {
445  s8_t i;
446  LWIP_ASSERT("netif->hwaddr_len == ETHARP_HWADDR_LEN", netif->hwaddr_len == ETHARP_HWADDR_LEN);
447  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",
448  ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
449  ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
450  ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]));
451  /* non-unicast address? */
452  if (ip_addr_isany(ipaddr) ||
453  ip_addr_isbroadcast(ipaddr, netif) ||
454  ip_addr_ismulticast(ipaddr)) {
455  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
456  return ERR_ARG;
457  }
458  /* find or create ARP entry */
459  i = find_entry(ipaddr, flags);
460  /* bail out if no entry could be found */
461  if (i < 0) {
462  return (err_t)i;
463  }
464 
465 #if ETHARP_SUPPORT_STATIC_ENTRIES
466  if (flags & ETHARP_FLAG_STATIC_ENTRY) {
467  /* record static type */
468  arp_table[i].static_entry = 1;
469  }
470 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
471 
472  /* mark it stable */
473  arp_table[i].state = ETHARP_STATE_STABLE;
474 
475 #if LWIP_SNMP
476  /* record network interface */
477  arp_table[i].netif = netif;
478 #endif /* LWIP_SNMP */
479  /* insert in SNMP ARP index tree */
480  snmp_insert_arpidx_tree(netif, &arp_table[i].ipaddr);
481 
482  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: updating stable entry %"S16_F"\n", (s16_t)i));
483  /* update address */
484  ETHADDR32_COPY(&arp_table[i].ethaddr, ethaddr);
485  /* reset time stamp */
486  arp_table[i].ctime = 0;
487  /* this is where we will send out queued packets! */
488 #if ARP_QUEUEING
489  while (arp_table[i].q != NULL) {
490  struct pbuf *p;
491  /* remember remainder of queue */
492  struct etharp_q_entry *q = arp_table[i].q;
493  /* pop first item off the queue */
494  arp_table[i].q = q->next;
495  /* get the packet pointer */
496  p = q->p;
497  /* now queue entry can be freed */
498  memp_free(MEMP_ARP_QUEUE, q);
499 #else /* ARP_QUEUEING */
500  if (arp_table[i].q != NULL) {
501  struct pbuf *p = arp_table[i].q;
502  arp_table[i].q = NULL;
503 #endif /* ARP_QUEUEING */
504  /* send the queued IP packet */
505  etharp_send_ip(netif, p, (struct eth_addr*)(netif->hwaddr), ethaddr);
506  /* free the queued IP packet */
507  pbuf_free(p);
508  }
509  return ERR_OK;
510 }
511 
512 #if ETHARP_SUPPORT_STATIC_ENTRIES
513 
521 err_t
522 etharp_add_static_entry(ip_addr_t *ipaddr, struct eth_addr *ethaddr)
523 {
524  struct netif *netif;
525  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",
526  ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
527  ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
528  ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]));
529 
530  netif = ip_route(ipaddr);
531  if (netif == NULL) {
532  return ERR_RTE;
533  }
534 
535  return update_arp_entry(netif, ipaddr, ethaddr, ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY);
536 }
537 
546 err_t
547 etharp_remove_static_entry(ip_addr_t *ipaddr)
548 {
549  s8_t i;
550  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_remove_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
551  ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
552 
553  /* find or create ARP entry */
554  i = find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY);
555  /* bail out if no entry could be found */
556  if (i < 0) {
557  return (err_t)i;
558  }
559 
560  if ((arp_table[i].state != ETHARP_STATE_STABLE) ||
561  (arp_table[i].static_entry == 0)) {
562  /* entry wasn't a static entry, cannot remove it */
563  return ERR_ARG;
564  }
565  /* entry found, free it */
566  free_entry(i);
567  return ERR_OK;
568 }
569 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
570 
582 s8_t
583 etharp_find_addr(struct netif *netif, ip_addr_t *ipaddr,
584  struct eth_addr **eth_ret, ip_addr_t **ip_ret)
585 {
586  s8_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 = find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY);
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 #if ETHARP_TRUST_IP_MAC
603 
618 static void
619 etharp_ip_input(struct netif *netif, struct pbuf *p)
620 {
621  struct eth_hdr *ethhdr;
622  struct ip_hdr *iphdr;
623  ip_addr_t iphdr_src;
624  LWIP_ERROR("netif != NULL", (netif != NULL), return;);
625 
626  /* Only insert an entry if the source IP address of the
627  incoming IP packet comes from a host on the local network. */
628  ethhdr = (struct eth_hdr *)p->payload;
629  iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
630 #if ETHARP_SUPPORT_VLAN
631  if (ethhdr->type == ETHTYPE_VLAN) {
632  iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR);
633  }
634 #endif /* ETHARP_SUPPORT_VLAN */
635 
636  ip_addr_copy(iphdr_src, iphdr->src);
637 
638  /* source is not on the local network? */
639  if (!ip_addr_netcmp(&iphdr_src, &(netif->ip_addr), &(netif->netmask))) {
640  /* do nothing */
641  return;
642  }
643 
644  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_ip_input: updating ETHARP table.\n"));
645  /* update the source IP address in the cache, if present */
646  /* @todo We could use ETHARP_FLAG_TRY_HARD if we think we are going to talk
647  * back soon (for example, if the destination IP address is ours. */
648  update_arp_entry(netif, &iphdr_src, &(ethhdr->src), ETHARP_FLAG_FIND_ONLY);
649 }
650 #endif /* ETHARP_TRUST_IP_MAC */
651 
667 static void
668 etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p)
669 {
670  struct etharp_hdr *hdr;
671  struct eth_hdr *ethhdr;
672  /* these are aligned properly, whereas the ARP header fields might not be */
673  ip_addr_t sipaddr, dipaddr;
674  u8_t for_us;
675 #if LWIP_AUTOIP
676  const u8_t * ethdst_hwaddr;
677 #endif /* LWIP_AUTOIP */
678 
679  LWIP_ERROR("netif != NULL", (netif != NULL), return;);
680 
681  /* drop short ARP packets: we have to check for p->len instead of p->tot_len here
682  since a struct etharp_hdr is pointed to p->payload, so it musn't be chained! */
683  if (p->len < SIZEOF_ETHARP_PACKET) {
685  ("etharp_arp_input: packet dropped, too short (%"S16_F"/%"S16_F")\n", p->tot_len,
686  (s16_t)SIZEOF_ETHARP_PACKET));
687  ETHARP_STATS_INC(etharp.lenerr);
688  ETHARP_STATS_INC(etharp.drop);
689  pbuf_free(p);
690  return;
691  }
692 
693  ethhdr = (struct eth_hdr *)p->payload;
694  hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
695 #if ETHARP_SUPPORT_VLAN
696  if (ethhdr->type == ETHTYPE_VLAN) {
697  hdr = (struct etharp_hdr *)(((u8_t*)ethhdr) + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR);
698  }
699 #endif /* ETHARP_SUPPORT_VLAN */
700 
701  /* RFC 826 "Packet Reception": */
702  if ((hdr->hwtype != PP_HTONS(HWTYPE_ETHERNET)) ||
703  (hdr->hwlen != ETHARP_HWADDR_LEN) ||
704  (hdr->protolen != sizeof(ip_addr_t)) ||
705  (hdr->proto != PP_HTONS(ETHTYPE_IP)) ||
706  (ethhdr->type != PP_HTONS(ETHTYPE_ARP))) {
708  ("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",
709  hdr->hwtype, hdr->hwlen, hdr->proto, hdr->protolen, ethhdr->type));
710  ETHARP_STATS_INC(etharp.proterr);
711  ETHARP_STATS_INC(etharp.drop);
712  pbuf_free(p);
713  return;
714  }
715  ETHARP_STATS_INC(etharp.recv);
716 
717 #if LWIP_AUTOIP
718  /* We have to check if a host already has configured our random
719  * created link local address and continously check if there is
720  * a host with this IP-address so we can detect collisions */
721  autoip_arp_reply(netif, hdr);
722 #endif /* LWIP_AUTOIP */
723 
724  /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
725  * structure packing (not using structure copy which breaks strict-aliasing rules). */
726  IPADDR2_COPY(&sipaddr, &hdr->sipaddr);
727  IPADDR2_COPY(&dipaddr, &hdr->dipaddr);
728 
729  /* this interface is not configured? */
730  if (ip_addr_isany(&netif->ip_addr)) {
731  for_us = 0;
732  } else {
733  /* ARP packet directed to us? */
734  for_us = (u8_t)ip_addr_cmp(&dipaddr, &(netif->ip_addr));
735  }
736 
737  /* ARP message directed to us?
738  -> add IP address in ARP cache; assume requester wants to talk to us,
739  can result in directly sending the queued packets for this host.
740  ARP message not directed to us?
741  -> update the source IP address in the cache, if present */
742  update_arp_entry(netif, &sipaddr, &(hdr->shwaddr),
743  for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY);
744 
745  /* now act on the message itself */
746  switch (hdr->opcode) {
747  /* ARP request? */
748  case PP_HTONS(ARP_REQUEST):
749  /* ARP request. If it asked for our address, we send out a
750  * reply. In any case, we time-stamp any existing ARP entry,
751  * and possiby send out an IP packet that was queued on it. */
752 
753  LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP request\n"));
754  /* ARP request for our address? */
755  if (for_us) {
756 
757  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: replying to ARP request for our IP address\n"));
758  /* Re-use pbuf to send ARP reply.
759  Since we are re-using an existing pbuf, we can't call etharp_raw since
760  that would allocate a new pbuf. */
761  hdr->opcode = htons(ARP_REPLY);
762 
763  IPADDR2_COPY(&hdr->dipaddr, &hdr->sipaddr);
764  IPADDR2_COPY(&hdr->sipaddr, &netif->ip_addr);
765 
766  LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
767  (netif->hwaddr_len == ETHARP_HWADDR_LEN));
768 #if LWIP_AUTOIP
769  /* If we are using Link-Local, all ARP packets that contain a Link-Local
770  * 'sender IP address' MUST be sent using link-layer broadcast instead of
771  * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
772  ethdst_hwaddr = ip_addr_islinklocal(&netif->ip_addr) ? (u8_t*)(ethbroadcast.addr) : hdr->shwaddr.addr;
773 #endif /* LWIP_AUTOIP */
774 
775  ETHADDR16_COPY(&hdr->dhwaddr, &hdr->shwaddr);
776 #if LWIP_AUTOIP
777  ETHADDR16_COPY(&ethhdr->dest, ethdst_hwaddr);
778 #else /* LWIP_AUTOIP */
779  ETHADDR16_COPY(&ethhdr->dest, &hdr->shwaddr);
780 #endif /* LWIP_AUTOIP */
781  ETHADDR16_COPY(&hdr->shwaddr, ethaddr);
782  ETHADDR16_COPY(&ethhdr->src, ethaddr);
783 
784  /* hwtype, hwaddr_len, proto, protolen and the type in the ethernet header
785  are already correct, we tested that before */
786 
787  /* return ARP reply */
788  netif->linkoutput(netif, p);
789  /* we are not configured? */
790  } else if (ip_addr_isany(&netif->ip_addr)) {
791  /* { for_us == 0 and netif->ip_addr.addr == 0 } */
792  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n"));
793  /* request was not directed to us */
794  } else {
795  /* { for_us == 0 and netif->ip_addr.addr != 0 } */
796  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP request was not for us.\n"));
797  }
798  break;
799  case PP_HTONS(ARP_REPLY):
800  /* ARP reply. We already updated the ARP cache earlier. */
801  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP reply\n"));
802 #if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
803  /* DHCP wants to know about ARP replies from any host with an
804  * IP address also offered to us by the DHCP server. We do not
805  * want to take a duplicate IP address on a single network.
806  * @todo How should we handle redundant (fail-over) interfaces? */
807  dhcp_arp_reply(netif, &sipaddr);
808 #endif /* (LWIP_DHCP && DHCP_DOES_ARP_CHECK) */
809  break;
810  default:
811  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP unknown opcode type %"S16_F"\n", htons(hdr->opcode)));
812  ETHARP_STATS_INC(etharp.err);
813  break;
814  }
815  /* free ARP packet */
816  pbuf_free(p);
817 }
818 
837 err_t
838 etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr)
839 {
840  struct eth_addr *dest, mcastaddr;
841 
842  /* make room for Ethernet header - should not fail */
843  if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) {
844  /* bail out */
846  ("etharp_output: could not allocate room for header.\n"));
847  LINK_STATS_INC(link.lenerr);
848  return ERR_BUF;
849  }
850 
851  /* assume unresolved Ethernet address */
852  dest = NULL;
853  /* Determine on destination hardware address. Broadcasts and multicasts
854  * are special, other IP addresses are looked up in the ARP table. */
855 
856  /* broadcast destination IP address? */
857  if (ip_addr_isbroadcast(ipaddr, netif)) {
858  /* broadcast on Ethernet also */
859  dest = (struct eth_addr *)&ethbroadcast;
860  /* multicast destination IP address? */
861  } else if (ip_addr_ismulticast(ipaddr)) {
862  /* Hash IP multicast address to MAC address.*/
863  mcastaddr.addr[0] = 0x01;
864  mcastaddr.addr[1] = 0x00;
865  mcastaddr.addr[2] = 0x5e;
866  mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
867  mcastaddr.addr[4] = ip4_addr3(ipaddr);
868  mcastaddr.addr[5] = ip4_addr4(ipaddr);
869  /* destination Ethernet address is multicast */
870  dest = &mcastaddr;
871  /* unicast destination IP address? */
872  } else {
873  /* outside local network? */
874  if (!ip_addr_netcmp(ipaddr, &(netif->ip_addr), &(netif->netmask)) &&
875  !ip_addr_islinklocal(ipaddr)) {
876 #if LWIP_AUTOIP
877  struct ip_hdr *iphdr = (struct ip_hdr*)((u8_t*)q->payload +
878  sizeof(struct eth_hdr));
879  /* According to RFC 3297, chapter 2.6.2 (Forwarding Rules), a packet with
880  a link-local source address must always be "directly to its destination
881  on the same physical link. The host MUST NOT send the packet to any
882  router for forwarding". */
883  if (!ip_addr_islinklocal(&iphdr->src))
884 #endif /* LWIP_AUTOIP */
885  {
886  /* interface has default gateway? */
887  if (!ip_addr_isany(&netif->gw)) {
888  /* send to hardware address of default gateway IP address */
889  ipaddr = &(netif->gw);
890  /* no default gateway available */
891  } else {
892  /* no route to destination error (default gateway missing) */
893  return ERR_RTE;
894  }
895  }
896  }
897 #if LWIP_NETIF_HWADDRHINT
898  if (netif->addr_hint != NULL) {
899  /* per-pcb cached entry was given */
900  u8_t etharp_cached_entry = *(netif->addr_hint);
901  if (etharp_cached_entry < ARP_TABLE_SIZE) {
902 #endif /* LWIP_NETIF_HWADDRHINT */
903  if ((arp_table[etharp_cached_entry].state == ETHARP_STATE_STABLE) &&
904  (ip_addr_cmp(ipaddr, &arp_table[etharp_cached_entry].ipaddr))) {
905  /* the per-pcb-cached entry is stable and the right one! */
906  ETHARP_STATS_INC(etharp.cachehit);
907  return etharp_send_ip(netif, q, (struct eth_addr*)(netif->hwaddr),
908  &arp_table[etharp_cached_entry].ethaddr);
909  }
910 #if LWIP_NETIF_HWADDRHINT
911  }
912  }
913 #endif /* LWIP_NETIF_HWADDRHINT */
914  /* queue on destination Ethernet address belonging to ipaddr */
915  return etharp_query(netif, ipaddr, q);
916  }
917 
918  /* continuation for multicast/broadcast destinations */
919  /* obtain source Ethernet address of the given interface */
920  /* send packet directly on the link */
921  return etharp_send_ip(netif, q, (struct eth_addr*)(netif->hwaddr), dest);
922 }
923 
957 err_t
958 etharp_query(struct netif *netif, ip_addr_t *ipaddr, struct pbuf *q)
959 {
960  struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
961  err_t result = ERR_MEM;
962  s8_t i; /* ARP entry index */
963 
964  /* non-unicast address? */
965  if (ip_addr_isbroadcast(ipaddr, netif) ||
966  ip_addr_ismulticast(ipaddr) ||
967  ip_addr_isany(ipaddr)) {
968  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
969  return ERR_ARG;
970  }
971 
972  /* find entry in ARP cache, ask to create entry if queueing packet */
973  i = find_entry(ipaddr, ETHARP_FLAG_TRY_HARD);
974 
975  /* could not find or create entry? */
976  if (i < 0) {
977  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
978  if (q) {
979  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n"));
980  ETHARP_STATS_INC(etharp.memerr);
981  }
982  return (err_t)i;
983  }
984 
985  /* mark a fresh entry as pending (we just sent a request) */
986  if (arp_table[i].state == ETHARP_STATE_EMPTY) {
987  arp_table[i].state = ETHARP_STATE_PENDING;
988  }
989 
990  /* { i is either a STABLE or (new or existing) PENDING entry } */
991  LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
992  ((arp_table[i].state == ETHARP_STATE_PENDING) ||
993  (arp_table[i].state == ETHARP_STATE_STABLE)));
994 
995  /* do we have a pending entry? or an implicit query request? */
996  if ((arp_table[i].state == ETHARP_STATE_PENDING) || (q == NULL)) {
997  /* try to resolve it; send out ARP request */
998  result = etharp_request(netif, ipaddr);
999  if (result != ERR_OK) {
1000  /* ARP request couldn't be sent */
1001  /* We don't re-send arp request in etharp_tmr, but we still queue packets,
1002  since this failure could be temporary, and the next packet calling
1003  etharp_query again could lead to sending the queued packets. */
1004  }
1005  if (q == NULL) {
1006  return result;
1007  }
1008  }
1009 
1010  /* packet given? */
1011  LWIP_ASSERT("q != NULL", q != NULL);
1012  /* stable entry? */
1013  if (arp_table[i].state == ETHARP_STATE_STABLE) {
1014  /* we have a valid IP->Ethernet address mapping */
1015  ETHARP_SET_HINT(netif, i);
1016  /* send the packet */
1017  result = etharp_send_ip(netif, q, srcaddr, &(arp_table[i].ethaddr));
1018  /* pending entry? (either just created or already pending */
1019  } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
1020  /* entry is still pending, queue the given packet 'q' */
1021  struct pbuf *p;
1022  int copy_needed = 0;
1023  /* IF q includes a PBUF_REF, PBUF_POOL or PBUF_RAM, we have no choice but
1024  * to copy the whole queue into a new PBUF_RAM (see bug #11400)
1025  * PBUF_ROMs can be left as they are, since ROM must not get changed. */
1026  p = q;
1027  while (p) {
1028  LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0));
1029  if(p->type != PBUF_ROM) {
1030  copy_needed = 1;
1031  break;
1032  }
1033  p = p->next;
1034  }
1035  if(copy_needed) {
1036  /* copy the whole packet into new pbufs */
1037  p = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
1038  if(p != NULL) {
1039  if (pbuf_copy(p, q) != ERR_OK) {
1040  pbuf_free(p);
1041  p = NULL;
1042  }
1043  }
1044  } else {
1045  /* referencing the old pbuf is enough */
1046  p = q;
1047  pbuf_ref(p);
1048  }
1049  /* packet could be taken over? */
1050  if (p != NULL) {
1051  /* queue packet ... */
1052 #if ARP_QUEUEING
1053  struct etharp_q_entry *new_entry;
1054  /* allocate a new arp queue entry */
1055  new_entry = (struct etharp_q_entry *)memp_malloc(MEMP_ARP_QUEUE);
1056  if (new_entry != NULL) {
1057  new_entry->next = 0;
1058  new_entry->p = p;
1059  if(arp_table[i].q != NULL) {
1060  /* queue was already existent, append the new entry to the end */
1061  struct etharp_q_entry *r;
1062  r = arp_table[i].q;
1063  while (r->next != NULL) {
1064  r = r->next;
1065  }
1066  r->next = new_entry;
1067  } else {
1068  /* queue did not exist, first item in queue */
1069  arp_table[i].q = new_entry;
1070  }
1071  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
1072  result = ERR_OK;
1073  } else {
1074  /* the pool MEMP_ARP_QUEUE is empty */
1075  pbuf_free(p);
1076  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));
1077  result = ERR_MEM;
1078  }
1079 #else /* ARP_QUEUEING */
1080  /* always queue one packet per ARP request only, freeing a previously queued packet */
1081  if (arp_table[i].q != NULL) {
1082  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: dropped previously queued packet %p for ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
1083  pbuf_free(arp_table[i].q);
1084  }
1085  arp_table[i].q = p;
1086  result = ERR_OK;
1087  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
1088 #endif /* ARP_QUEUEING */
1089  } else {
1090  ETHARP_STATS_INC(etharp.memerr);
1091  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));
1092  result = ERR_MEM;
1093  }
1094  }
1095  return result;
1096 }
1097 
1113 #if !LWIP_AUTOIP
1114 static
1115 #endif /* LWIP_AUTOIP */
1116 err_t
1117 etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr,
1118  const struct eth_addr *ethdst_addr,
1119  const struct eth_addr *hwsrc_addr, const ip_addr_t *ipsrc_addr,
1120  const struct eth_addr *hwdst_addr, const ip_addr_t *ipdst_addr,
1121  const u16_t opcode)
1122 {
1123  struct pbuf *p;
1124  err_t result = ERR_OK;
1125  struct eth_hdr *ethhdr;
1126  struct etharp_hdr *hdr;
1127 #if LWIP_AUTOIP
1128  const u8_t * ethdst_hwaddr;
1129 #endif /* LWIP_AUTOIP */
1130 
1131  /* allocate a pbuf for the outgoing ARP request packet */
1132  p = pbuf_alloc(PBUF_RAW, SIZEOF_ETHARP_PACKET, PBUF_RAM);
1133  /* could allocate a pbuf for an ARP request? */
1134  if (p == NULL) {
1136  ("etharp_raw: could not allocate pbuf for ARP request.\n"));
1137  ETHARP_STATS_INC(etharp.memerr);
1138  return ERR_MEM;
1139  }
1140  LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr",
1141  (p->len >= SIZEOF_ETHARP_PACKET));
1142 
1143  ethhdr = (struct eth_hdr *)p->payload;
1144  hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
1145  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n"));
1146  hdr->opcode = htons(opcode);
1147 
1148  LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
1149  (netif->hwaddr_len == ETHARP_HWADDR_LEN));
1150 #if LWIP_AUTOIP
1151  /* If we are using Link-Local, all ARP packets that contain a Link-Local
1152  * 'sender IP address' MUST be sent using link-layer broadcast instead of
1153  * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
1154  ethdst_hwaddr = ip_addr_islinklocal(ipsrc_addr) ? (u8_t*)(ethbroadcast.addr) : ethdst_addr->addr;
1155 #endif /* LWIP_AUTOIP */
1156  /* Write the ARP MAC-Addresses */
1157  ETHADDR16_COPY(&hdr->shwaddr, hwsrc_addr);
1158  ETHADDR16_COPY(&hdr->dhwaddr, hwdst_addr);
1159  /* Write the Ethernet MAC-Addresses */
1160 #if LWIP_AUTOIP
1161  ETHADDR16_COPY(&ethhdr->dest, ethdst_hwaddr);
1162 #else /* LWIP_AUTOIP */
1163  ETHADDR16_COPY(&ethhdr->dest, ethdst_addr);
1164 #endif /* LWIP_AUTOIP */
1165  ETHADDR16_COPY(&ethhdr->src, ethsrc_addr);
1166  /* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
1167  * structure packing. */
1168  IPADDR2_COPY(&hdr->sipaddr, ipsrc_addr);
1169  IPADDR2_COPY(&hdr->dipaddr, ipdst_addr);
1170 
1171  hdr->hwtype = PP_HTONS(HWTYPE_ETHERNET);
1172  hdr->proto = PP_HTONS(ETHTYPE_IP);
1173  /* set hwlen and protolen */
1174  hdr->hwlen = ETHARP_HWADDR_LEN;
1175  hdr->protolen = sizeof(ip_addr_t);
1176 
1177  ethhdr->type = PP_HTONS(ETHTYPE_ARP);
1178  /* send ARP query */
1179  result = netif->linkoutput(netif, p);
1180  ETHARP_STATS_INC(etharp.xmit);
1181  /* free ARP query packet */
1182  pbuf_free(p);
1183  p = NULL;
1184  /* could not allocate pbuf for ARP request */
1185 
1186  return result;
1187 }
1188 
1198 err_t
1199 etharp_request(struct netif *netif, ip_addr_t *ipaddr)
1200 {
1201  LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n"));
1202  return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, &ethbroadcast,
1203  (struct eth_addr *)netif->hwaddr, &netif->ip_addr, &ethzero,
1204  ipaddr, ARP_REQUEST);
1205 }
1206 #endif /* LWIP_ARP */
1207 
1216 err_t
1217 ethernet_input(struct pbuf *p, struct netif *netif)
1218 {
1219  struct eth_hdr* ethhdr;
1220  u16_t type;
1221 
1222  /* points to packet payload, which starts with an Ethernet header */
1223  ethhdr = (struct eth_hdr *)p->payload;
1225  ("ethernet_input: dest:%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F", src:%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F", type:%"X16_F"\n",
1226  (unsigned)ethhdr->dest.addr[0], (unsigned)ethhdr->dest.addr[1], (unsigned)ethhdr->dest.addr[2],
1227  (unsigned)ethhdr->dest.addr[3], (unsigned)ethhdr->dest.addr[4], (unsigned)ethhdr->dest.addr[5],
1228  (unsigned)ethhdr->src.addr[0], (unsigned)ethhdr->src.addr[1], (unsigned)ethhdr->src.addr[2],
1229  (unsigned)ethhdr->src.addr[3], (unsigned)ethhdr->src.addr[4], (unsigned)ethhdr->src.addr[5],
1230  (unsigned)htons(ethhdr->type)));
1231 
1232  type = ethhdr->type;
1233 #if ETHARP_SUPPORT_VLAN
1234  if (type == PP_HTONS(ETHTYPE_VLAN)) {
1235  struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr*)(((char*)ethhdr) + SIZEOF_ETH_HDR);
1236 #ifdef ETHARP_VLAN_CHECK /* if not, allow all VLANs */
1237  if (VLAN_ID(vlan) != ETHARP_VLAN_CHECK) {
1238  /* silently ignore this packet: not for our VLAN */
1239  pbuf_free(p);
1240  return ERR_OK;
1241  }
1242 #endif /* ETHARP_VLAN_CHECK */
1243  type = vlan->tpid;
1244  }
1245 #endif /* ETHARP_SUPPORT_VLAN */
1246 
1247 #if LWIP_ARP_FILTER_NETIF
1248  netif = LWIP_ARP_FILTER_NETIF_FN(p, netif, htons(type));
1249 #endif /* LWIP_ARP_FILTER_NETIF*/
1250 
1251  switch (type) {
1252 #if LWIP_ARP
1253  /* IP packet? */
1254  case PP_HTONS(ETHTYPE_IP):
1255  if (!(netif->flags & NETIF_FLAG_ETHARP)) {
1256  goto free_and_return;
1257  }
1258 #if ETHARP_TRUST_IP_MAC
1259  /* update ARP table */
1260  etharp_ip_input(netif, p);
1261 #endif /* ETHARP_TRUST_IP_MAC */
1262  /* skip Ethernet header */
1263  if(pbuf_header(p, -(s16_t)SIZEOF_ETH_HDR)) {
1264  LWIP_ASSERT("Can't move over header in packet", 0);
1265  goto free_and_return;
1266  } else {
1267  /* pass to IP layer */
1268  ip_input(p, netif);
1269  }
1270  break;
1271 
1272  case PP_HTONS(ETHTYPE_ARP):
1273  if (!(netif->flags & NETIF_FLAG_ETHARP)) {
1274  goto free_and_return;
1275  }
1276  /* pass p to ARP module */
1277  etharp_arp_input(netif, (struct eth_addr*)(netif->hwaddr), p);
1278  break;
1279 #endif /* LWIP_ARP */
1280 #if PPPOE_SUPPORT
1281  case PP_HTONS(ETHTYPE_PPPOEDISC): /* PPP Over Ethernet Discovery Stage */
1282  pppoe_disc_input(netif, p);
1283  break;
1284 
1285  case PP_HTONS(ETHTYPE_PPPOE): /* PPP Over Ethernet Session Stage */
1286  pppoe_data_input(netif, p);
1287  break;
1288 #endif /* PPPOE_SUPPORT */
1289 
1290  default:
1291  ETHARP_STATS_INC(etharp.proterr);
1292  ETHARP_STATS_INC(etharp.drop);
1293  goto free_and_return;
1294  }
1295 
1296  /* This means the pbuf is freed or consumed,
1297  so the caller doesn't have to free it again */
1298  return ERR_OK;
1299 
1300 free_and_return:
1301  pbuf_free(p);
1302  return ERR_OK;
1303 }
1304 #endif /* LWIP_ARP || LWIP_ETHERNET */