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