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