xref: /btstack/3rd-party/lwip/core/src/netif/ethernet.c (revision 97dc5e692c7d94a280158af58036a0efee5b0e56)
1*97dc5e69SMatthias Ringwald /**
2*97dc5e69SMatthias Ringwald  * @file
3*97dc5e69SMatthias Ringwald  * Ethernet common functions
4*97dc5e69SMatthias Ringwald  *
5*97dc5e69SMatthias Ringwald  * @defgroup ethernet Ethernet
6*97dc5e69SMatthias Ringwald  * @ingroup callbackstyle_api
7*97dc5e69SMatthias Ringwald  */
8*97dc5e69SMatthias Ringwald 
9*97dc5e69SMatthias Ringwald /*
10*97dc5e69SMatthias Ringwald  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
11*97dc5e69SMatthias Ringwald  * Copyright (c) 2003-2004 Leon Woestenberg <[email protected]>
12*97dc5e69SMatthias Ringwald  * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands.
13*97dc5e69SMatthias Ringwald  * All rights reserved.
14*97dc5e69SMatthias Ringwald  *
15*97dc5e69SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without modification,
16*97dc5e69SMatthias Ringwald  * are permitted provided that the following conditions are met:
17*97dc5e69SMatthias Ringwald  *
18*97dc5e69SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright notice,
19*97dc5e69SMatthias Ringwald  *    this list of conditions and the following disclaimer.
20*97dc5e69SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright notice,
21*97dc5e69SMatthias Ringwald  *    this list of conditions and the following disclaimer in the documentation
22*97dc5e69SMatthias Ringwald  *    and/or other materials provided with the distribution.
23*97dc5e69SMatthias Ringwald  * 3. The name of the author may not be used to endorse or promote products
24*97dc5e69SMatthias Ringwald  *    derived from this software without specific prior written permission.
25*97dc5e69SMatthias Ringwald  *
26*97dc5e69SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
27*97dc5e69SMatthias Ringwald  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28*97dc5e69SMatthias Ringwald  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
29*97dc5e69SMatthias Ringwald  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30*97dc5e69SMatthias Ringwald  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31*97dc5e69SMatthias Ringwald  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32*97dc5e69SMatthias Ringwald  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33*97dc5e69SMatthias Ringwald  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34*97dc5e69SMatthias Ringwald  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35*97dc5e69SMatthias Ringwald  * OF SUCH DAMAGE.
36*97dc5e69SMatthias Ringwald  *
37*97dc5e69SMatthias Ringwald  * This file is part of the lwIP TCP/IP stack.
38*97dc5e69SMatthias Ringwald  *
39*97dc5e69SMatthias Ringwald  */
40*97dc5e69SMatthias Ringwald 
41*97dc5e69SMatthias Ringwald #include "lwip/opt.h"
42*97dc5e69SMatthias Ringwald 
43*97dc5e69SMatthias Ringwald #if LWIP_ARP || LWIP_ETHERNET
44*97dc5e69SMatthias Ringwald 
45*97dc5e69SMatthias Ringwald #include "netif/ethernet.h"
46*97dc5e69SMatthias Ringwald #include "lwip/def.h"
47*97dc5e69SMatthias Ringwald #include "lwip/stats.h"
48*97dc5e69SMatthias Ringwald #include "lwip/etharp.h"
49*97dc5e69SMatthias Ringwald #include "lwip/ip.h"
50*97dc5e69SMatthias Ringwald #include "lwip/snmp.h"
51*97dc5e69SMatthias Ringwald 
52*97dc5e69SMatthias Ringwald #include <string.h>
53*97dc5e69SMatthias Ringwald 
54*97dc5e69SMatthias Ringwald #include "netif/ppp/ppp_opts.h"
55*97dc5e69SMatthias Ringwald #if PPPOE_SUPPORT
56*97dc5e69SMatthias Ringwald #include "netif/ppp/pppoe.h"
57*97dc5e69SMatthias Ringwald #endif /* PPPOE_SUPPORT */
58*97dc5e69SMatthias Ringwald 
59*97dc5e69SMatthias Ringwald #ifdef LWIP_HOOK_FILENAME
60*97dc5e69SMatthias Ringwald #include LWIP_HOOK_FILENAME
61*97dc5e69SMatthias Ringwald #endif
62*97dc5e69SMatthias Ringwald 
63*97dc5e69SMatthias Ringwald const struct eth_addr ethbroadcast = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
64*97dc5e69SMatthias Ringwald const struct eth_addr ethzero = {{0, 0, 0, 0, 0, 0}};
65*97dc5e69SMatthias Ringwald 
66*97dc5e69SMatthias Ringwald /**
67*97dc5e69SMatthias Ringwald  * @ingroup lwip_nosys
68*97dc5e69SMatthias Ringwald  * Process received ethernet frames. Using this function instead of directly
69*97dc5e69SMatthias Ringwald  * calling ip_input and passing ARP frames through etharp in ethernetif_input,
70*97dc5e69SMatthias Ringwald  * the ARP cache is protected from concurrent access.<br>
71*97dc5e69SMatthias Ringwald  * Don't call directly, pass to netif_add() and call netif->input().
72*97dc5e69SMatthias Ringwald  *
73*97dc5e69SMatthias Ringwald  * @param p the received packet, p->payload pointing to the ethernet header
74*97dc5e69SMatthias Ringwald  * @param netif the network interface on which the packet was received
75*97dc5e69SMatthias Ringwald  *
76*97dc5e69SMatthias Ringwald  * @see LWIP_HOOK_UNKNOWN_ETH_PROTOCOL
77*97dc5e69SMatthias Ringwald  * @see ETHARP_SUPPORT_VLAN
78*97dc5e69SMatthias Ringwald  * @see LWIP_HOOK_VLAN_CHECK
79*97dc5e69SMatthias Ringwald  */
80*97dc5e69SMatthias Ringwald err_t
ethernet_input(struct pbuf * p,struct netif * netif)81*97dc5e69SMatthias Ringwald ethernet_input(struct pbuf *p, struct netif *netif)
82*97dc5e69SMatthias Ringwald {
83*97dc5e69SMatthias Ringwald   struct eth_hdr *ethhdr;
84*97dc5e69SMatthias Ringwald   u16_t type;
85*97dc5e69SMatthias Ringwald #if LWIP_ARP || ETHARP_SUPPORT_VLAN || LWIP_IPV6
86*97dc5e69SMatthias Ringwald   u16_t next_hdr_offset = SIZEOF_ETH_HDR;
87*97dc5e69SMatthias Ringwald #endif /* LWIP_ARP || ETHARP_SUPPORT_VLAN */
88*97dc5e69SMatthias Ringwald 
89*97dc5e69SMatthias Ringwald   LWIP_ASSERT_CORE_LOCKED();
90*97dc5e69SMatthias Ringwald 
91*97dc5e69SMatthias Ringwald   if (p->len <= SIZEOF_ETH_HDR) {
92*97dc5e69SMatthias Ringwald     /* a packet with only an ethernet header (or less) is not valid for us */
93*97dc5e69SMatthias Ringwald     ETHARP_STATS_INC(etharp.proterr);
94*97dc5e69SMatthias Ringwald     ETHARP_STATS_INC(etharp.drop);
95*97dc5e69SMatthias Ringwald     MIB2_STATS_NETIF_INC(netif, ifinerrors);
96*97dc5e69SMatthias Ringwald     goto free_and_return;
97*97dc5e69SMatthias Ringwald   }
98*97dc5e69SMatthias Ringwald 
99*97dc5e69SMatthias Ringwald   if (p->if_idx == NETIF_NO_INDEX) {
100*97dc5e69SMatthias Ringwald     p->if_idx = netif_get_index(netif);
101*97dc5e69SMatthias Ringwald   }
102*97dc5e69SMatthias Ringwald 
103*97dc5e69SMatthias Ringwald   /* points to packet payload, which starts with an Ethernet header */
104*97dc5e69SMatthias Ringwald   ethhdr = (struct eth_hdr *)p->payload;
105*97dc5e69SMatthias Ringwald   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
106*97dc5e69SMatthias Ringwald               ("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",
107*97dc5e69SMatthias Ringwald                (unsigned char)ethhdr->dest.addr[0], (unsigned char)ethhdr->dest.addr[1], (unsigned char)ethhdr->dest.addr[2],
108*97dc5e69SMatthias Ringwald                (unsigned char)ethhdr->dest.addr[3], (unsigned char)ethhdr->dest.addr[4], (unsigned char)ethhdr->dest.addr[5],
109*97dc5e69SMatthias Ringwald                (unsigned char)ethhdr->src.addr[0],  (unsigned char)ethhdr->src.addr[1],  (unsigned char)ethhdr->src.addr[2],
110*97dc5e69SMatthias Ringwald                (unsigned char)ethhdr->src.addr[3],  (unsigned char)ethhdr->src.addr[4],  (unsigned char)ethhdr->src.addr[5],
111*97dc5e69SMatthias Ringwald                lwip_htons(ethhdr->type)));
112*97dc5e69SMatthias Ringwald 
113*97dc5e69SMatthias Ringwald   type = ethhdr->type;
114*97dc5e69SMatthias Ringwald #if ETHARP_SUPPORT_VLAN
115*97dc5e69SMatthias Ringwald   if (type == PP_HTONS(ETHTYPE_VLAN)) {
116*97dc5e69SMatthias Ringwald     struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr *)(((char *)ethhdr) + SIZEOF_ETH_HDR);
117*97dc5e69SMatthias Ringwald     next_hdr_offset = SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR;
118*97dc5e69SMatthias Ringwald     if (p->len <= SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR) {
119*97dc5e69SMatthias Ringwald       /* a packet with only an ethernet/vlan header (or less) is not valid for us */
120*97dc5e69SMatthias Ringwald       ETHARP_STATS_INC(etharp.proterr);
121*97dc5e69SMatthias Ringwald       ETHARP_STATS_INC(etharp.drop);
122*97dc5e69SMatthias Ringwald       MIB2_STATS_NETIF_INC(netif, ifinerrors);
123*97dc5e69SMatthias Ringwald       goto free_and_return;
124*97dc5e69SMatthias Ringwald     }
125*97dc5e69SMatthias Ringwald #if defined(LWIP_HOOK_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) /* if not, allow all VLANs */
126*97dc5e69SMatthias Ringwald #ifdef LWIP_HOOK_VLAN_CHECK
127*97dc5e69SMatthias Ringwald     if (!LWIP_HOOK_VLAN_CHECK(netif, ethhdr, vlan)) {
128*97dc5e69SMatthias Ringwald #elif defined(ETHARP_VLAN_CHECK_FN)
129*97dc5e69SMatthias Ringwald     if (!ETHARP_VLAN_CHECK_FN(ethhdr, vlan)) {
130*97dc5e69SMatthias Ringwald #elif defined(ETHARP_VLAN_CHECK)
131*97dc5e69SMatthias Ringwald     if (VLAN_ID(vlan) != ETHARP_VLAN_CHECK) {
132*97dc5e69SMatthias Ringwald #endif
133*97dc5e69SMatthias Ringwald       /* silently ignore this packet: not for our VLAN */
134*97dc5e69SMatthias Ringwald       pbuf_free(p);
135*97dc5e69SMatthias Ringwald       return ERR_OK;
136*97dc5e69SMatthias Ringwald     }
137*97dc5e69SMatthias Ringwald #endif /* defined(LWIP_HOOK_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK) || defined(ETHARP_VLAN_CHECK_FN) */
138*97dc5e69SMatthias Ringwald     type = vlan->tpid;
139*97dc5e69SMatthias Ringwald   }
140*97dc5e69SMatthias Ringwald #endif /* ETHARP_SUPPORT_VLAN */
141*97dc5e69SMatthias Ringwald 
142*97dc5e69SMatthias Ringwald #if LWIP_ARP_FILTER_NETIF
143*97dc5e69SMatthias Ringwald   netif = LWIP_ARP_FILTER_NETIF_FN(p, netif, lwip_htons(type));
144*97dc5e69SMatthias Ringwald #endif /* LWIP_ARP_FILTER_NETIF*/
145*97dc5e69SMatthias Ringwald 
146*97dc5e69SMatthias Ringwald   if (ethhdr->dest.addr[0] & 1) {
147*97dc5e69SMatthias Ringwald     /* this might be a multicast or broadcast packet */
148*97dc5e69SMatthias Ringwald     if (ethhdr->dest.addr[0] == LL_IP4_MULTICAST_ADDR_0) {
149*97dc5e69SMatthias Ringwald #if LWIP_IPV4
150*97dc5e69SMatthias Ringwald       if ((ethhdr->dest.addr[1] == LL_IP4_MULTICAST_ADDR_1) &&
151*97dc5e69SMatthias Ringwald           (ethhdr->dest.addr[2] == LL_IP4_MULTICAST_ADDR_2)) {
152*97dc5e69SMatthias Ringwald         /* mark the pbuf as link-layer multicast */
153*97dc5e69SMatthias Ringwald         p->flags |= PBUF_FLAG_LLMCAST;
154*97dc5e69SMatthias Ringwald       }
155*97dc5e69SMatthias Ringwald #endif /* LWIP_IPV4 */
156*97dc5e69SMatthias Ringwald     }
157*97dc5e69SMatthias Ringwald #if LWIP_IPV6
158*97dc5e69SMatthias Ringwald     else if ((ethhdr->dest.addr[0] == LL_IP6_MULTICAST_ADDR_0) &&
159*97dc5e69SMatthias Ringwald              (ethhdr->dest.addr[1] == LL_IP6_MULTICAST_ADDR_1)) {
160*97dc5e69SMatthias Ringwald       /* mark the pbuf as link-layer multicast */
161*97dc5e69SMatthias Ringwald       p->flags |= PBUF_FLAG_LLMCAST;
162*97dc5e69SMatthias Ringwald     }
163*97dc5e69SMatthias Ringwald #endif /* LWIP_IPV6 */
164*97dc5e69SMatthias Ringwald     else if (eth_addr_cmp(&ethhdr->dest, &ethbroadcast)) {
165*97dc5e69SMatthias Ringwald       /* mark the pbuf as link-layer broadcast */
166*97dc5e69SMatthias Ringwald       p->flags |= PBUF_FLAG_LLBCAST;
167*97dc5e69SMatthias Ringwald     }
168*97dc5e69SMatthias Ringwald   }
169*97dc5e69SMatthias Ringwald 
170*97dc5e69SMatthias Ringwald   switch (type) {
171*97dc5e69SMatthias Ringwald #if LWIP_IPV4 && LWIP_ARP
172*97dc5e69SMatthias Ringwald     /* IP packet? */
173*97dc5e69SMatthias Ringwald     case PP_HTONS(ETHTYPE_IP):
174*97dc5e69SMatthias Ringwald       if (!(netif->flags & NETIF_FLAG_ETHARP)) {
175*97dc5e69SMatthias Ringwald         goto free_and_return;
176*97dc5e69SMatthias Ringwald       }
177*97dc5e69SMatthias Ringwald       /* skip Ethernet header (min. size checked above) */
178*97dc5e69SMatthias Ringwald       if (pbuf_remove_header(p, next_hdr_offset)) {
179*97dc5e69SMatthias Ringwald         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
180*97dc5e69SMatthias Ringwald                     ("ethernet_input: IPv4 packet dropped, too short (%"U16_F"/%"U16_F")\n",
181*97dc5e69SMatthias Ringwald                      p->tot_len, next_hdr_offset));
182*97dc5e69SMatthias Ringwald         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("Can't move over header in packet"));
183*97dc5e69SMatthias Ringwald         goto free_and_return;
184*97dc5e69SMatthias Ringwald       } else {
185*97dc5e69SMatthias Ringwald         /* pass to IP layer */
186*97dc5e69SMatthias Ringwald         ip4_input(p, netif);
187*97dc5e69SMatthias Ringwald       }
188*97dc5e69SMatthias Ringwald       break;
189*97dc5e69SMatthias Ringwald 
190*97dc5e69SMatthias Ringwald     case PP_HTONS(ETHTYPE_ARP):
191*97dc5e69SMatthias Ringwald       if (!(netif->flags & NETIF_FLAG_ETHARP)) {
192*97dc5e69SMatthias Ringwald         goto free_and_return;
193*97dc5e69SMatthias Ringwald       }
194*97dc5e69SMatthias Ringwald       /* skip Ethernet header (min. size checked above) */
195*97dc5e69SMatthias Ringwald       if (pbuf_remove_header(p, next_hdr_offset)) {
196*97dc5e69SMatthias Ringwald         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
197*97dc5e69SMatthias Ringwald                     ("ethernet_input: ARP response packet dropped, too short (%"U16_F"/%"U16_F")\n",
198*97dc5e69SMatthias Ringwald                      p->tot_len, next_hdr_offset));
199*97dc5e69SMatthias Ringwald         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("Can't move over header in packet"));
200*97dc5e69SMatthias Ringwald         ETHARP_STATS_INC(etharp.lenerr);
201*97dc5e69SMatthias Ringwald         ETHARP_STATS_INC(etharp.drop);
202*97dc5e69SMatthias Ringwald         goto free_and_return;
203*97dc5e69SMatthias Ringwald       } else {
204*97dc5e69SMatthias Ringwald         /* pass p to ARP module */
205*97dc5e69SMatthias Ringwald         etharp_input(p, netif);
206*97dc5e69SMatthias Ringwald       }
207*97dc5e69SMatthias Ringwald       break;
208*97dc5e69SMatthias Ringwald #endif /* LWIP_IPV4 && LWIP_ARP */
209*97dc5e69SMatthias Ringwald #if PPPOE_SUPPORT
210*97dc5e69SMatthias Ringwald     case PP_HTONS(ETHTYPE_PPPOEDISC): /* PPP Over Ethernet Discovery Stage */
211*97dc5e69SMatthias Ringwald       pppoe_disc_input(netif, p);
212*97dc5e69SMatthias Ringwald       break;
213*97dc5e69SMatthias Ringwald 
214*97dc5e69SMatthias Ringwald     case PP_HTONS(ETHTYPE_PPPOE): /* PPP Over Ethernet Session Stage */
215*97dc5e69SMatthias Ringwald       pppoe_data_input(netif, p);
216*97dc5e69SMatthias Ringwald       break;
217*97dc5e69SMatthias Ringwald #endif /* PPPOE_SUPPORT */
218*97dc5e69SMatthias Ringwald 
219*97dc5e69SMatthias Ringwald #if LWIP_IPV6
220*97dc5e69SMatthias Ringwald     case PP_HTONS(ETHTYPE_IPV6): /* IPv6 */
221*97dc5e69SMatthias Ringwald       /* skip Ethernet header */
222*97dc5e69SMatthias Ringwald       if ((p->len < next_hdr_offset) || pbuf_remove_header(p, next_hdr_offset)) {
223*97dc5e69SMatthias Ringwald         LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
224*97dc5e69SMatthias Ringwald                     ("ethernet_input: IPv6 packet dropped, too short (%"U16_F"/%"U16_F")\n",
225*97dc5e69SMatthias Ringwald                      p->tot_len, next_hdr_offset));
226*97dc5e69SMatthias Ringwald         goto free_and_return;
227*97dc5e69SMatthias Ringwald       } else {
228*97dc5e69SMatthias Ringwald         /* pass to IPv6 layer */
229*97dc5e69SMatthias Ringwald         ip6_input(p, netif);
230*97dc5e69SMatthias Ringwald       }
231*97dc5e69SMatthias Ringwald       break;
232*97dc5e69SMatthias Ringwald #endif /* LWIP_IPV6 */
233*97dc5e69SMatthias Ringwald 
234*97dc5e69SMatthias Ringwald     default:
235*97dc5e69SMatthias Ringwald #ifdef LWIP_HOOK_UNKNOWN_ETH_PROTOCOL
236*97dc5e69SMatthias Ringwald       if (LWIP_HOOK_UNKNOWN_ETH_PROTOCOL(p, netif) == ERR_OK) {
237*97dc5e69SMatthias Ringwald         break;
238*97dc5e69SMatthias Ringwald       }
239*97dc5e69SMatthias Ringwald #endif
240*97dc5e69SMatthias Ringwald       ETHARP_STATS_INC(etharp.proterr);
241*97dc5e69SMatthias Ringwald       ETHARP_STATS_INC(etharp.drop);
242*97dc5e69SMatthias Ringwald       MIB2_STATS_NETIF_INC(netif, ifinunknownprotos);
243*97dc5e69SMatthias Ringwald       goto free_and_return;
244*97dc5e69SMatthias Ringwald   }
245*97dc5e69SMatthias Ringwald 
246*97dc5e69SMatthias Ringwald   /* This means the pbuf is freed or consumed,
247*97dc5e69SMatthias Ringwald      so the caller doesn't have to free it again */
248*97dc5e69SMatthias Ringwald   return ERR_OK;
249*97dc5e69SMatthias Ringwald 
250*97dc5e69SMatthias Ringwald free_and_return:
251*97dc5e69SMatthias Ringwald   pbuf_free(p);
252*97dc5e69SMatthias Ringwald   return ERR_OK;
253*97dc5e69SMatthias Ringwald }
254*97dc5e69SMatthias Ringwald 
255*97dc5e69SMatthias Ringwald /**
256*97dc5e69SMatthias Ringwald  * @ingroup ethernet
257*97dc5e69SMatthias Ringwald  * Send an ethernet packet on the network using netif->linkoutput().
258*97dc5e69SMatthias Ringwald  * The ethernet header is filled in before sending.
259*97dc5e69SMatthias Ringwald  *
260*97dc5e69SMatthias Ringwald  * @see LWIP_HOOK_VLAN_SET
261*97dc5e69SMatthias Ringwald  *
262*97dc5e69SMatthias Ringwald  * @param netif the lwIP network interface on which to send the packet
263*97dc5e69SMatthias Ringwald  * @param p the packet to send. pbuf layer must be @ref PBUF_LINK.
264*97dc5e69SMatthias Ringwald  * @param src the source MAC address to be copied into the ethernet header
265*97dc5e69SMatthias Ringwald  * @param dst the destination MAC address to be copied into the ethernet header
266*97dc5e69SMatthias Ringwald  * @param eth_type ethernet type (@ref lwip_ieee_eth_type)
267*97dc5e69SMatthias Ringwald  * @return ERR_OK if the packet was sent, any other err_t on failure
268*97dc5e69SMatthias Ringwald  */
269*97dc5e69SMatthias Ringwald err_t
270*97dc5e69SMatthias Ringwald ethernet_output(struct netif * netif, struct pbuf * p,
271*97dc5e69SMatthias Ringwald                 const struct eth_addr * src, const struct eth_addr * dst,
272*97dc5e69SMatthias Ringwald                 u16_t eth_type) {
273*97dc5e69SMatthias Ringwald   struct eth_hdr *ethhdr;
274*97dc5e69SMatthias Ringwald   u16_t eth_type_be = lwip_htons(eth_type);
275*97dc5e69SMatthias Ringwald 
276*97dc5e69SMatthias Ringwald #if ETHARP_SUPPORT_VLAN && (defined(LWIP_HOOK_VLAN_SET) || LWIP_VLAN_PCP)
277*97dc5e69SMatthias Ringwald   s32_t vlan_prio_vid;
278*97dc5e69SMatthias Ringwald #ifdef LWIP_HOOK_VLAN_SET
279*97dc5e69SMatthias Ringwald   vlan_prio_vid = LWIP_HOOK_VLAN_SET(netif, p, src, dst, eth_type);
280*97dc5e69SMatthias Ringwald #elif LWIP_VLAN_PCP
281*97dc5e69SMatthias Ringwald   vlan_prio_vid = -1;
282*97dc5e69SMatthias Ringwald   if (netif->hints && (netif->hints->tci >= 0)) {
283*97dc5e69SMatthias Ringwald     vlan_prio_vid = (u16_t)netif->hints->tci;
284*97dc5e69SMatthias Ringwald   }
285*97dc5e69SMatthias Ringwald #endif
286*97dc5e69SMatthias Ringwald   if (vlan_prio_vid >= 0) {
287*97dc5e69SMatthias Ringwald     struct eth_vlan_hdr *vlanhdr;
288*97dc5e69SMatthias Ringwald 
289*97dc5e69SMatthias Ringwald     LWIP_ASSERT("prio_vid must be <= 0xFFFF", vlan_prio_vid <= 0xFFFF);
290*97dc5e69SMatthias Ringwald 
291*97dc5e69SMatthias Ringwald     if (pbuf_add_header(p, SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR) != 0) {
292*97dc5e69SMatthias Ringwald       goto pbuf_header_failed;
293*97dc5e69SMatthias Ringwald     }
294*97dc5e69SMatthias Ringwald     vlanhdr = (struct eth_vlan_hdr *)(((u8_t *)p->payload) + SIZEOF_ETH_HDR);
295*97dc5e69SMatthias Ringwald     vlanhdr->tpid     = eth_type_be;
296*97dc5e69SMatthias Ringwald     vlanhdr->prio_vid = lwip_htons((u16_t)vlan_prio_vid);
297*97dc5e69SMatthias Ringwald 
298*97dc5e69SMatthias Ringwald     eth_type_be = PP_HTONS(ETHTYPE_VLAN);
299*97dc5e69SMatthias Ringwald   } else
300*97dc5e69SMatthias Ringwald #endif /* ETHARP_SUPPORT_VLAN && (defined(LWIP_HOOK_VLAN_SET) || LWIP_VLAN_PCP) */
301*97dc5e69SMatthias Ringwald   {
302*97dc5e69SMatthias Ringwald     if (pbuf_add_header(p, SIZEOF_ETH_HDR) != 0) {
303*97dc5e69SMatthias Ringwald       goto pbuf_header_failed;
304*97dc5e69SMatthias Ringwald     }
305*97dc5e69SMatthias Ringwald   }
306*97dc5e69SMatthias Ringwald 
307*97dc5e69SMatthias Ringwald   LWIP_ASSERT_CORE_LOCKED();
308*97dc5e69SMatthias Ringwald 
309*97dc5e69SMatthias Ringwald   ethhdr = (struct eth_hdr *)p->payload;
310*97dc5e69SMatthias Ringwald   ethhdr->type = eth_type_be;
311*97dc5e69SMatthias Ringwald   SMEMCPY(&ethhdr->dest, dst, ETH_HWADDR_LEN);
312*97dc5e69SMatthias Ringwald   SMEMCPY(&ethhdr->src,  src, ETH_HWADDR_LEN);
313*97dc5e69SMatthias Ringwald 
314*97dc5e69SMatthias Ringwald   LWIP_ASSERT("netif->hwaddr_len must be 6 for ethernet_output!",
315*97dc5e69SMatthias Ringwald               (netif->hwaddr_len == ETH_HWADDR_LEN));
316*97dc5e69SMatthias Ringwald   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
317*97dc5e69SMatthias Ringwald               ("ethernet_output: sending packet %p\n", (void *)p));
318*97dc5e69SMatthias Ringwald 
319*97dc5e69SMatthias Ringwald   /* send the packet */
320*97dc5e69SMatthias Ringwald   return netif->linkoutput(netif, p);
321*97dc5e69SMatthias Ringwald 
322*97dc5e69SMatthias Ringwald pbuf_header_failed:
323*97dc5e69SMatthias Ringwald   LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
324*97dc5e69SMatthias Ringwald               ("ethernet_output: could not allocate room for header.\n"));
325*97dc5e69SMatthias Ringwald   LINK_STATS_INC(link.lenerr);
326*97dc5e69SMatthias Ringwald   return ERR_BUF;
327*97dc5e69SMatthias Ringwald }
328*97dc5e69SMatthias Ringwald 
329*97dc5e69SMatthias Ringwald #endif /* LWIP_ARP || LWIP_ETHERNET */
330