1 /*
2  * Copyright (c) 2016-2018 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(net_ethernet, CONFIG_NET_L2_ETHERNET_LOG_LEVEL);
9 
10 #include <zephyr/net/net_core.h>
11 #include <zephyr/net/net_l2.h>
12 #include <zephyr/net/net_if.h>
13 #include <zephyr/net/net_mgmt.h>
14 #include <zephyr/net/ethernet.h>
15 #include <zephyr/net/ethernet_mgmt.h>
16 #include <zephyr/net/gptp.h>
17 #include <zephyr/random/random.h>
18 
19 #if defined(CONFIG_NET_LLDP)
20 #include <zephyr/net/lldp.h>
21 #endif
22 
23 #include <zephyr/internal/syscall_handler.h>
24 
25 #include "arp.h"
26 #include "eth_stats.h"
27 #include "net_private.h"
28 #include "ipv6.h"
29 #include "ipv4.h"
30 #include "bridge.h"
31 
32 #define NET_BUF_TIMEOUT K_MSEC(100)
33 
34 static const struct net_eth_addr multicast_eth_addr __unused = {
35 	{ 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 } };
36 
37 static const struct net_eth_addr broadcast_eth_addr = {
38 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
39 
40 #if defined(CONFIG_NET_NATIVE_IP) && !defined(CONFIG_NET_RAW_MODE)
41 static struct net_if_mcast_monitor mcast_monitor;
42 #endif
43 
net_eth_broadcast_addr(void)44 const struct net_eth_addr *net_eth_broadcast_addr(void)
45 {
46 	return &broadcast_eth_addr;
47 }
48 
net_eth_ipv4_mcast_to_mac_addr(const struct in_addr * ipv4_addr,struct net_eth_addr * mac_addr)49 void net_eth_ipv4_mcast_to_mac_addr(const struct in_addr *ipv4_addr,
50 				    struct net_eth_addr *mac_addr)
51 {
52 	/* RFC 1112 6.4. Extensions to an Ethernet Local Network Module
53 	 * "An IP host group address is mapped to an Ethernet multicast
54 	 * address by placing the low-order 23-bits of the IP address into
55 	 * the low-order 23 bits of the Ethernet multicast address
56 	 * 01-00-5E-00-00-00 (hex)."
57 	 */
58 	mac_addr->addr[0] = 0x01;
59 	mac_addr->addr[1] = 0x00;
60 	mac_addr->addr[2] = 0x5e;
61 	mac_addr->addr[3] = ipv4_addr->s4_addr[1];
62 	mac_addr->addr[4] = ipv4_addr->s4_addr[2];
63 	mac_addr->addr[5] = ipv4_addr->s4_addr[3];
64 
65 	mac_addr->addr[3] &= 0x7f;
66 }
67 
net_eth_ipv6_mcast_to_mac_addr(const struct in6_addr * ipv6_addr,struct net_eth_addr * mac_addr)68 void net_eth_ipv6_mcast_to_mac_addr(const struct in6_addr *ipv6_addr,
69 				    struct net_eth_addr *mac_addr)
70 {
71 	/* RFC 2464 7. Address Mapping -- Multicast
72 	 * "An IPv6 packet with a multicast destination address DST,
73 	 * consisting of the sixteen octets DST[1] through DST[16],
74 	 * is transmitted to the Ethernet multicast address whose
75 	 * first two octets are the value 3333 hexadecimal and whose
76 	 * last four octets are the last four octets of DST."
77 	 */
78 	mac_addr->addr[0] = mac_addr->addr[1] = 0x33;
79 	memcpy(mac_addr->addr + 2, &ipv6_addr->s6_addr[12], 4);
80 }
81 
82 #define print_ll_addrs(pkt, type, len, src, dst)			   \
83 	if (CONFIG_NET_L2_ETHERNET_LOG_LEVEL >= LOG_LEVEL_DBG) {	   \
84 		char out[sizeof("xx:xx:xx:xx:xx:xx")];			   \
85 									   \
86 		snprintk(out, sizeof(out), "%s",			   \
87 			 net_sprint_ll_addr((src)->addr,		   \
88 					    sizeof(struct net_eth_addr))); \
89 									   \
90 		NET_DBG("iface %d (%p) src %s dst %s type 0x%x len %zu",   \
91 			net_if_get_by_iface(net_pkt_iface(pkt)),	   \
92 			net_pkt_iface(pkt), out,			   \
93 			net_sprint_ll_addr((dst)->addr,			   \
94 					    sizeof(struct net_eth_addr)),  \
95 			type, (size_t)len);				   \
96 	}
97 
98 #ifdef CONFIG_NET_VLAN
99 #define print_vlan_ll_addrs(pkt, type, tci, len, src, dst, tagstrip)       \
100 	if (CONFIG_NET_L2_ETHERNET_LOG_LEVEL >= LOG_LEVEL_DBG) {	   \
101 		char out[sizeof("xx:xx:xx:xx:xx:xx")];			   \
102 									   \
103 		snprintk(out, sizeof(out), "%s",			   \
104 			 net_sprint_ll_addr((src)->addr,		   \
105 					    sizeof(struct net_eth_addr))); \
106 									   \
107 		NET_DBG("iface %d (%p) src %s dst %s type 0x%x "	   \
108 			"tag %d %spri %d len %zu",			   \
109 			net_if_get_by_iface(net_pkt_iface(pkt)),	   \
110 			net_pkt_iface(pkt), out,			   \
111 			net_sprint_ll_addr((dst)->addr,			   \
112 				   sizeof(struct net_eth_addr)),	   \
113 			type, net_eth_vlan_get_vid(tci),		   \
114 			tagstrip ? "(stripped) " : "",			   \
115 			net_eth_vlan_get_pcp(tci), (size_t)len);	   \
116 	}
117 #else
118 #define print_vlan_ll_addrs(...)
119 #endif /* CONFIG_NET_VLAN */
120 
ethernet_update_length(struct net_if * iface,struct net_pkt * pkt)121 static inline void ethernet_update_length(struct net_if *iface,
122 					  struct net_pkt *pkt)
123 {
124 	uint16_t len;
125 
126 	/* Let's check IP payload's length. If it's smaller than 46 bytes,
127 	 * i.e. smaller than minimal Ethernet frame size minus ethernet
128 	 * header size,then Ethernet has padded so it fits in the minimal
129 	 * frame size of 60 bytes. In that case, we need to get rid of it.
130 	 */
131 
132 	if (net_pkt_family(pkt) == AF_INET) {
133 		len = ntohs(NET_IPV4_HDR(pkt)->len);
134 	} else {
135 		len = ntohs(NET_IPV6_HDR(pkt)->len) + NET_IPV6H_LEN;
136 	}
137 
138 	if (len < NET_ETH_MINIMAL_FRAME_SIZE - sizeof(struct net_eth_hdr)) {
139 		struct net_buf *frag;
140 
141 		for (frag = pkt->frags; frag; frag = frag->frags) {
142 			if (frag->len < len) {
143 				len -= frag->len;
144 			} else {
145 				frag->len = len;
146 				len = 0U;
147 			}
148 		}
149 	}
150 }
151 
ethernet_update_rx_stats(struct net_if * iface,struct net_eth_hdr * hdr,size_t length)152 static void ethernet_update_rx_stats(struct net_if *iface,
153 				     struct net_eth_hdr *hdr, size_t length)
154 {
155 #if defined(CONFIG_NET_STATISTICS_ETHERNET)
156 	eth_stats_update_bytes_rx(iface, length);
157 	eth_stats_update_pkts_rx(iface);
158 
159 	if (net_eth_is_addr_broadcast(&hdr->dst)) {
160 		eth_stats_update_broadcast_rx(iface);
161 	} else if (net_eth_is_addr_multicast(&hdr->dst)) {
162 		eth_stats_update_multicast_rx(iface);
163 	}
164 #endif /* CONFIG_NET_STATISTICS_ETHERNET */
165 }
166 
eth_is_vlan_tag_stripped(struct net_if * iface)167 static inline bool eth_is_vlan_tag_stripped(struct net_if *iface)
168 {
169 	return (net_eth_get_hw_capabilities(iface) & ETHERNET_HW_VLAN_TAG_STRIP);
170 }
171 
172 /* Drop packet if it has broadcast destination MAC address but the IP
173  * address is not multicast or broadcast address. See RFC 1122 ch 3.3.6
174  */
175 static inline
ethernet_check_ipv4_bcast_addr(struct net_pkt * pkt,struct net_eth_hdr * hdr)176 enum net_verdict ethernet_check_ipv4_bcast_addr(struct net_pkt *pkt,
177 						struct net_eth_hdr *hdr)
178 {
179 	if (net_eth_is_addr_broadcast(&hdr->dst) &&
180 	    !(net_ipv4_is_addr_mcast((struct in_addr *)NET_IPV4_HDR(pkt)->dst) ||
181 	      net_ipv4_is_addr_bcast(net_pkt_iface(pkt),
182 				     (struct in_addr *)NET_IPV4_HDR(pkt)->dst))) {
183 		return NET_DROP;
184 	}
185 
186 	return NET_OK;
187 }
188 
189 #if defined(CONFIG_NET_NATIVE_IP) && !defined(CONFIG_NET_RAW_MODE)
ethernet_mcast_monitor_cb(struct net_if * iface,const struct net_addr * addr,bool is_joined)190 static void ethernet_mcast_monitor_cb(struct net_if *iface, const struct net_addr *addr,
191 				      bool is_joined)
192 {
193 	struct ethernet_config cfg = {
194 		.filter = {
195 			.set = is_joined,
196 			.type = ETHERNET_FILTER_TYPE_DST_MAC_ADDRESS,
197 		},
198 	};
199 
200 	const struct device *dev = net_if_get_device(iface);
201 	const struct ethernet_api *api = dev->api;
202 
203 	/* Make sure we're an ethernet device */
204 	if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) {
205 		return;
206 	}
207 
208 	if (!(net_eth_get_hw_capabilities(iface) & ETHERNET_HW_FILTERING)) {
209 		return;
210 	}
211 
212 	if (!api || !api->set_config) {
213 		return;
214 	}
215 
216 	switch (addr->family) {
217 #if defined(CONFIG_NET_IPV4)
218 	case AF_INET:
219 		net_eth_ipv4_mcast_to_mac_addr(&addr->in_addr, &cfg.filter.mac_address);
220 		break;
221 #endif /* CONFIG_NET_IPV4 */
222 #if defined(CONFIG_NET_IPV6)
223 	case AF_INET6:
224 		net_eth_ipv6_mcast_to_mac_addr(&addr->in6_addr, &cfg.filter.mac_address);
225 		break;
226 #endif /* CONFIG_NET_IPV6 */
227 	default:
228 		return;
229 	}
230 
231 	api->set_config(dev, ETHERNET_CONFIG_TYPE_FILTER, &cfg);
232 }
233 #endif
234 
ethernet_recv(struct net_if * iface,struct net_pkt * pkt)235 static enum net_verdict ethernet_recv(struct net_if *iface,
236 				      struct net_pkt *pkt)
237 {
238 	struct ethernet_context *ctx = net_if_l2_data(iface);
239 	struct net_eth_hdr *hdr = NET_ETH_HDR(pkt);
240 	uint8_t hdr_len = sizeof(struct net_eth_hdr);
241 	uint16_t type;
242 	struct net_linkaddr *lladdr;
243 	sa_family_t family = AF_UNSPEC;
244 	bool is_vlan_pkt = false;
245 
246 	/* This expects that the Ethernet header is in the first net_buf
247 	 * fragment. This is a safe expectation here as it would not make
248 	 * any sense to split the Ethernet header to two net_buf's by the
249 	 * Ethernet driver.
250 	 */
251 	if (hdr == NULL || pkt->buffer->len < hdr_len) {
252 		goto drop;
253 	}
254 
255 	if (IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE) &&
256 	    net_eth_iface_is_bridged(ctx) && !net_pkt_is_l2_bridged(pkt)) {
257 		struct net_if *bridge = net_eth_get_bridge(ctx);
258 		struct net_pkt *out_pkt;
259 
260 		out_pkt = net_pkt_clone(pkt, K_NO_WAIT);
261 		if (out_pkt == NULL) {
262 			goto drop;
263 		}
264 
265 		net_pkt_set_l2_bridged(out_pkt, true);
266 		net_pkt_set_iface(out_pkt, bridge);
267 		net_pkt_set_orig_iface(out_pkt, iface);
268 
269 		NET_DBG("Passing pkt %p (orig %p) to bridge %d from %d",
270 			out_pkt, pkt, net_if_get_by_iface(bridge),
271 			net_if_get_by_iface(iface));
272 
273 		(void)net_if_queue_tx(bridge, out_pkt);
274 	}
275 
276 	type = ntohs(hdr->type);
277 
278 	if (IS_ENABLED(CONFIG_NET_VLAN) && type == NET_ETH_PTYPE_VLAN) {
279 		if (net_eth_is_vlan_enabled(ctx, iface) &&
280 		    !eth_is_vlan_tag_stripped(iface)) {
281 			struct net_eth_vlan_hdr *hdr_vlan =
282 				(struct net_eth_vlan_hdr *)NET_ETH_HDR(pkt);
283 			enum net_verdict verdict;
284 
285 			net_pkt_set_vlan_tci(pkt, ntohs(hdr_vlan->vlan.tci));
286 			type = ntohs(hdr_vlan->type);
287 			hdr_len = sizeof(struct net_eth_vlan_hdr);
288 			is_vlan_pkt = true;
289 
290 			net_pkt_set_iface(pkt,
291 					  net_eth_get_vlan_iface(iface,
292 						       net_pkt_vlan_tag(pkt)));
293 
294 			/* If we receive a packet with a VLAN tag, for that we don't
295 			 * have a VLAN interface, drop the packet.
296 			 */
297 			if (net_if_l2(net_pkt_iface(pkt)) == NULL) {
298 				goto drop;
299 			}
300 
301 			/* We could call VLAN interface directly but then the
302 			 * interface statistics would not get updated so route
303 			 * the call via Virtual L2 layer.
304 			 */
305 			if (net_if_l2(net_pkt_iface(pkt))->recv != NULL) {
306 				verdict = net_if_l2(net_pkt_iface(pkt))->recv(iface, pkt);
307 				if (verdict == NET_DROP) {
308 					goto drop;
309 				}
310 			}
311 		}
312 	}
313 
314 	switch (type) {
315 	case NET_ETH_PTYPE_IP:
316 	case NET_ETH_PTYPE_ARP:
317 		net_pkt_set_family(pkt, AF_INET);
318 		family = AF_INET;
319 		break;
320 	case NET_ETH_PTYPE_IPV6:
321 		net_pkt_set_family(pkt, AF_INET6);
322 		family = AF_INET6;
323 		break;
324 	case NET_ETH_PTYPE_EAPOL:
325 		family = AF_UNSPEC;
326 		break;
327 #if defined(CONFIG_NET_L2_PTP)
328 	case NET_ETH_PTYPE_PTP:
329 		family = AF_UNSPEC;
330 		break;
331 #endif
332 	case NET_ETH_PTYPE_LLDP:
333 #if defined(CONFIG_NET_LLDP)
334 		net_buf_pull(pkt->frags, hdr_len);
335 		return net_lldp_recv(iface, pkt);
336 #else
337 		NET_DBG("LLDP Rx agent not enabled");
338 		goto drop;
339 #endif
340 	default:
341 		if (IS_ENABLED(CONFIG_NET_ETHERNET_FORWARD_UNRECOGNISED_ETHERTYPE)) {
342 			family = AF_UNSPEC;
343 			break;
344 		}
345 
346 		NET_DBG("Unknown hdr type 0x%04x iface %d (%p)", type,
347 			net_if_get_by_iface(iface), iface);
348 		eth_stats_update_unknown_protocol(iface);
349 		return NET_DROP;
350 	}
351 
352 	/* Set the pointers to ll src and dst addresses */
353 	lladdr = net_pkt_lladdr_src(pkt);
354 	lladdr->addr = hdr->src.addr;
355 	lladdr->len = sizeof(struct net_eth_addr);
356 	lladdr->type = NET_LINK_ETHERNET;
357 
358 	lladdr = net_pkt_lladdr_dst(pkt);
359 	lladdr->addr = hdr->dst.addr;
360 	lladdr->len = sizeof(struct net_eth_addr);
361 	lladdr->type = NET_LINK_ETHERNET;
362 
363 	net_pkt_set_ll_proto_type(pkt, type);
364 
365 	if (is_vlan_pkt) {
366 		print_vlan_ll_addrs(pkt, type, net_pkt_vlan_tci(pkt),
367 				    net_pkt_get_len(pkt),
368 				    net_pkt_lladdr_src(pkt),
369 				    net_pkt_lladdr_dst(pkt),
370 				    eth_is_vlan_tag_stripped(iface));
371 	} else {
372 		print_ll_addrs(pkt, type, net_pkt_get_len(pkt),
373 			       net_pkt_lladdr_src(pkt),
374 			       net_pkt_lladdr_dst(pkt));
375 	}
376 
377 	if (!net_eth_is_addr_broadcast((struct net_eth_addr *)lladdr->addr) &&
378 	    !net_eth_is_addr_multicast((struct net_eth_addr *)lladdr->addr) &&
379 	    !net_eth_is_addr_lldp_multicast(
380 		    (struct net_eth_addr *)lladdr->addr) &&
381 	    !net_eth_is_addr_ptp_multicast((struct net_eth_addr *)lladdr->addr) &&
382 	    !net_linkaddr_cmp(net_if_get_link_addr(iface), lladdr)) {
383 		/* The ethernet frame is not for me as the link addresses
384 		 * are different.
385 		 */
386 		NET_DBG("Dropping frame, not for me [%s]",
387 			net_sprint_ll_addr(net_if_get_link_addr(iface)->addr,
388 					   sizeof(struct net_eth_addr)));
389 		goto drop;
390 	}
391 
392 	net_buf_pull(pkt->frags, hdr_len);
393 
394 	if (IS_ENABLED(CONFIG_NET_IPV4) && type == NET_ETH_PTYPE_IP &&
395 	    ethernet_check_ipv4_bcast_addr(pkt, hdr) == NET_DROP) {
396 		goto drop;
397 	}
398 
399 	ethernet_update_rx_stats(iface, hdr, net_pkt_get_len(pkt) + hdr_len);
400 
401 	if (IS_ENABLED(CONFIG_NET_ARP) &&
402 	    family == AF_INET && type == NET_ETH_PTYPE_ARP) {
403 		NET_DBG("ARP packet from %s received",
404 			net_sprint_ll_addr((uint8_t *)hdr->src.addr,
405 					   sizeof(struct net_eth_addr)));
406 
407 		if (IS_ENABLED(CONFIG_NET_IPV4_ACD) &&
408 		    net_ipv4_acd_input(iface, pkt) == NET_DROP) {
409 			return NET_DROP;
410 		}
411 
412 		return net_arp_input(pkt, hdr);
413 	}
414 
415 	if (IS_ENABLED(CONFIG_NET_GPTP) && type == NET_ETH_PTYPE_PTP) {
416 		return net_gptp_recv(iface, pkt);
417 	}
418 
419 	if (type != NET_ETH_PTYPE_EAPOL) {
420 		ethernet_update_length(iface, pkt);
421 	}
422 
423 	return NET_CONTINUE;
424 drop:
425 	eth_stats_update_errors_rx(iface);
426 	return NET_DROP;
427 }
428 
429 #ifdef CONFIG_NET_IPV4
ethernet_ipv4_dst_is_broadcast_or_mcast(struct net_pkt * pkt)430 static inline bool ethernet_ipv4_dst_is_broadcast_or_mcast(struct net_pkt *pkt)
431 {
432 	if (net_ipv4_is_addr_bcast(net_pkt_iface(pkt),
433 				   (struct in_addr *)NET_IPV4_HDR(pkt)->dst) ||
434 	    net_ipv4_is_addr_mcast((struct in_addr *)NET_IPV4_HDR(pkt)->dst)) {
435 		return true;
436 	}
437 
438 	return false;
439 }
440 
ethernet_fill_in_dst_on_ipv4_mcast(struct net_pkt * pkt,struct net_eth_addr * dst)441 static bool ethernet_fill_in_dst_on_ipv4_mcast(struct net_pkt *pkt,
442 					       struct net_eth_addr *dst)
443 {
444 	if (net_pkt_family(pkt) == AF_INET &&
445 	    net_ipv4_is_addr_mcast((struct in_addr *)NET_IPV4_HDR(pkt)->dst)) {
446 		/* Multicast address */
447 		net_eth_ipv4_mcast_to_mac_addr(
448 			(struct in_addr *)NET_IPV4_HDR(pkt)->dst, dst);
449 
450 		return true;
451 	}
452 
453 	return false;
454 }
455 
ethernet_ll_prepare_on_ipv4(struct net_if * iface,struct net_pkt * pkt)456 static struct net_pkt *ethernet_ll_prepare_on_ipv4(struct net_if *iface,
457 						   struct net_pkt *pkt)
458 {
459 	struct ethernet_context *ctx = net_if_l2_data(iface);
460 
461 	if (IS_ENABLED(CONFIG_NET_VLAN) &&
462 	    net_pkt_vlan_tag(pkt) != NET_VLAN_TAG_UNSPEC &&
463 	    net_eth_is_vlan_enabled(ctx, net_pkt_iface(pkt))) {
464 		iface = net_eth_get_vlan_iface(iface,
465 					       net_pkt_vlan_tag(pkt));
466 		net_pkt_set_iface(pkt, iface);
467 	}
468 
469 	if (ethernet_ipv4_dst_is_broadcast_or_mcast(pkt)) {
470 		return pkt;
471 	}
472 
473 	if (IS_ENABLED(CONFIG_NET_ARP)) {
474 		struct net_pkt *arp_pkt;
475 
476 		arp_pkt = net_arp_prepare(pkt, (struct in_addr *)NET_IPV4_HDR(pkt)->dst, NULL);
477 		if (!arp_pkt) {
478 			return NULL;
479 		}
480 
481 		if (pkt != arp_pkt) {
482 			NET_DBG("Sending arp pkt %p (orig %p) to iface %d (%p)",
483 				arp_pkt, pkt, net_if_get_by_iface(iface), iface);
484 			net_pkt_unref(pkt);
485 			return arp_pkt;
486 		}
487 
488 		NET_DBG("Found ARP entry, sending pkt %p to iface %d (%p)",
489 			pkt, net_if_get_by_iface(iface), iface);
490 	}
491 
492 	return pkt;
493 }
494 #else
495 #define ethernet_ipv4_dst_is_broadcast_or_mcast(...) false
496 #define ethernet_fill_in_dst_on_ipv4_mcast(...) false
497 #define ethernet_ll_prepare_on_ipv4(...) NULL
498 #endif /* CONFIG_NET_IPV4 */
499 
500 #ifdef CONFIG_NET_IPV6
ethernet_fill_in_dst_on_ipv6_mcast(struct net_pkt * pkt,struct net_eth_addr * dst)501 static bool ethernet_fill_in_dst_on_ipv6_mcast(struct net_pkt *pkt,
502 					       struct net_eth_addr *dst)
503 {
504 	if (net_pkt_family(pkt) == AF_INET6 &&
505 	    net_ipv6_is_addr_mcast((struct in6_addr *)NET_IPV6_HDR(pkt)->dst)) {
506 		memcpy(dst, (uint8_t *)multicast_eth_addr.addr,
507 		       sizeof(struct net_eth_addr) - 4);
508 		memcpy((uint8_t *)dst + 2,
509 		       NET_IPV6_HDR(pkt)->dst + 12,
510 		       sizeof(struct net_eth_addr) - 2);
511 
512 		return true;
513 	}
514 
515 	return false;
516 }
517 #else
518 #define ethernet_fill_in_dst_on_ipv6_mcast(...) false
519 #endif /* CONFIG_NET_IPV6 */
520 
get_reserve_ll_header_size(struct net_if * iface)521 static inline size_t get_reserve_ll_header_size(struct net_if *iface)
522 {
523 	bool is_vlan = false;
524 
525 #if defined(CONFIG_NET_VLAN)
526 	if (net_if_l2(iface) == &NET_L2_GET_NAME(VIRTUAL)) {
527 		iface = net_eth_get_vlan_main(iface);
528 		is_vlan = true;
529 	}
530 #endif
531 
532 	if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) {
533 		return 0U;
534 	}
535 
536 	if (!IS_ENABLED(CONFIG_NET_L2_ETHERNET_RESERVE_HEADER)) {
537 		return 0U;
538 	}
539 
540 	if (is_vlan) {
541 		return sizeof(struct net_eth_vlan_hdr);
542 	} else {
543 		return sizeof(struct net_eth_hdr);
544 	}
545 }
546 
ethernet_fill_header(struct ethernet_context * ctx,struct net_if * iface,struct net_pkt * pkt,uint32_t ptype)547 static struct net_buf *ethernet_fill_header(struct ethernet_context *ctx,
548 					    struct net_if *iface,
549 					    struct net_pkt *pkt,
550 					    uint32_t ptype)
551 {
552 	struct net_if *orig_iface = iface;
553 	struct net_buf *hdr_frag;
554 	struct net_eth_hdr *hdr;
555 	size_t reserve_ll_header;
556 	size_t hdr_len;
557 	bool is_vlan;
558 
559 	is_vlan = IS_ENABLED(CONFIG_NET_VLAN) &&
560 		net_eth_is_vlan_enabled(ctx, iface) &&
561 		net_pkt_vlan_tag(pkt) != NET_VLAN_TAG_UNSPEC;
562 	if (is_vlan) {
563 		orig_iface = net_eth_get_vlan_iface(iface, net_pkt_vlan_tag(pkt));
564 	}
565 
566 	reserve_ll_header = get_reserve_ll_header_size(orig_iface);
567 	if (reserve_ll_header > 0) {
568 		hdr_len = reserve_ll_header;
569 		hdr_frag = pkt->buffer;
570 
571 		NET_DBG("Making room for link header %zd bytes", hdr_len);
572 
573 		/* Make room for the header */
574 		net_buf_push(pkt->buffer, hdr_len);
575 	} else {
576 		hdr_len = IS_ENABLED(CONFIG_NET_VLAN) ?
577 			sizeof(struct net_eth_vlan_hdr) :
578 			sizeof(struct net_eth_hdr);
579 
580 		hdr_frag = net_pkt_get_frag(pkt, hdr_len, NET_BUF_TIMEOUT);
581 		if (!hdr_frag) {
582 			return NULL;
583 		}
584 	}
585 
586 	if (is_vlan) {
587 		struct net_eth_vlan_hdr *hdr_vlan;
588 
589 		if (reserve_ll_header == 0U) {
590 			hdr_len = sizeof(struct net_eth_vlan_hdr);
591 			net_buf_add(hdr_frag, hdr_len);
592 		}
593 
594 		hdr_vlan = (struct net_eth_vlan_hdr *)(hdr_frag->data);
595 
596 		if (ptype == htons(NET_ETH_PTYPE_ARP) ||
597 		    (!ethernet_fill_in_dst_on_ipv4_mcast(pkt, &hdr_vlan->dst) &&
598 		     !ethernet_fill_in_dst_on_ipv6_mcast(pkt, &hdr_vlan->dst))) {
599 			memcpy(&hdr_vlan->dst, net_pkt_lladdr_dst(pkt)->addr,
600 			       sizeof(struct net_eth_addr));
601 		}
602 
603 		memcpy(&hdr_vlan->src, net_pkt_lladdr_src(pkt)->addr,
604 		       sizeof(struct net_eth_addr));
605 
606 		hdr_vlan->type = ptype;
607 		hdr_vlan->vlan.tpid = htons(NET_ETH_PTYPE_VLAN);
608 		hdr_vlan->vlan.tci = htons(net_pkt_vlan_tci(pkt));
609 
610 		print_vlan_ll_addrs(pkt, ntohs(hdr_vlan->type),
611 				    net_pkt_vlan_tci(pkt),
612 				    hdr_len,
613 				    &hdr_vlan->src, &hdr_vlan->dst, false);
614 	} else {
615 		hdr = (struct net_eth_hdr *)(hdr_frag->data);
616 
617 		if (reserve_ll_header == 0U) {
618 			hdr_len = sizeof(struct net_eth_hdr);
619 			net_buf_add(hdr_frag, hdr_len);
620 		}
621 
622 		if (ptype == htons(NET_ETH_PTYPE_ARP) ||
623 		    (!ethernet_fill_in_dst_on_ipv4_mcast(pkt, &hdr->dst) &&
624 		     !ethernet_fill_in_dst_on_ipv6_mcast(pkt, &hdr->dst))) {
625 			memcpy(&hdr->dst, net_pkt_lladdr_dst(pkt)->addr,
626 			       sizeof(struct net_eth_addr));
627 		}
628 
629 		memcpy(&hdr->src, net_pkt_lladdr_src(pkt)->addr,
630 		       sizeof(struct net_eth_addr));
631 
632 		hdr->type = ptype;
633 
634 		print_ll_addrs(pkt, ntohs(hdr->type),
635 			       hdr_len, &hdr->src, &hdr->dst);
636 	}
637 
638 	if (reserve_ll_header == 0U) {
639 		net_pkt_frag_insert(pkt, hdr_frag);
640 	}
641 
642 	return hdr_frag;
643 }
644 
645 #if defined(CONFIG_NET_STATISTICS_ETHERNET)
ethernet_update_tx_stats(struct net_if * iface,struct net_pkt * pkt)646 static void ethernet_update_tx_stats(struct net_if *iface, struct net_pkt *pkt)
647 {
648 	struct net_eth_hdr *hdr = NET_ETH_HDR(pkt);
649 
650 	eth_stats_update_bytes_tx(iface, net_pkt_get_len(pkt));
651 	eth_stats_update_pkts_tx(iface);
652 
653 	if (net_eth_is_addr_multicast(&hdr->dst)) {
654 		eth_stats_update_multicast_tx(iface);
655 	} else if (net_eth_is_addr_broadcast(&hdr->dst)) {
656 		eth_stats_update_broadcast_tx(iface);
657 	}
658 }
659 #else
660 #define ethernet_update_tx_stats(...)
661 #endif /* CONFIG_NET_STATISTICS_ETHERNET */
662 
ethernet_remove_l2_header(struct net_pkt * pkt)663 static void ethernet_remove_l2_header(struct net_pkt *pkt)
664 {
665 	size_t reserve = get_reserve_ll_header_size(net_pkt_iface(pkt));
666 	struct net_buf *buf;
667 
668 	/* Remove the buffer added in ethernet_fill_header() */
669 	if (reserve == 0U) {
670 		buf = pkt->buffer;
671 		pkt->buffer = buf->frags;
672 		buf->frags = NULL;
673 
674 		net_pkt_frag_unref(buf);
675 	} else {
676 		net_buf_pull(pkt->buffer, reserve);
677 	}
678 }
679 
ethernet_send(struct net_if * iface,struct net_pkt * pkt)680 static int ethernet_send(struct net_if *iface, struct net_pkt *pkt)
681 {
682 	const struct ethernet_api *api = net_if_get_device(iface)->api;
683 	struct ethernet_context *ctx = net_if_l2_data(iface);
684 	uint16_t ptype = 0;
685 	int ret;
686 	struct net_pkt *orig_pkt = pkt;
687 
688 	if (!api) {
689 		ret = -ENOENT;
690 		goto error;
691 	}
692 
693 	if (!api->send) {
694 		ret = -ENOTSUP;
695 		goto error;
696 	}
697 
698 	/* We are trying to send a packet that is from bridge interface,
699 	 * so all the bits and pieces should be there (like Ethernet header etc)
700 	 * so just send it.
701 	 */
702 	if (IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE) && net_pkt_is_l2_bridged(pkt)) {
703 		goto send;
704 	}
705 
706 	if (IS_ENABLED(CONFIG_NET_IPV4) &&
707 	    net_pkt_family(pkt) == AF_INET) {
708 		struct net_pkt *tmp;
709 
710 		if (net_pkt_ipv4_acd(pkt)) {
711 			ptype = htons(NET_ETH_PTYPE_ARP);
712 		} else {
713 			tmp = ethernet_ll_prepare_on_ipv4(iface, pkt);
714 			if (!tmp) {
715 				ret = -ENOMEM;
716 				goto error;
717 			} else if (IS_ENABLED(CONFIG_NET_ARP) && tmp != pkt) {
718 				/* Original pkt got queued and is replaced
719 				 * by an ARP request packet.
720 				 */
721 				pkt = tmp;
722 				ptype = htons(NET_ETH_PTYPE_ARP);
723 				net_pkt_set_family(pkt, AF_INET);
724 			} else {
725 				ptype = htons(NET_ETH_PTYPE_IP);
726 			}
727 		}
728 	} else if (IS_ENABLED(CONFIG_NET_IPV6) &&
729 		   net_pkt_family(pkt) == AF_INET6) {
730 		ptype = htons(NET_ETH_PTYPE_IPV6);
731 	} else if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET) &&
732 		   net_pkt_family(pkt) == AF_PACKET) {
733 		struct net_context *context = net_pkt_context(pkt);
734 
735 		if (context && net_context_get_type(context) == SOCK_DGRAM) {
736 			struct sockaddr_ll *dst_addr;
737 			struct sockaddr_ll_ptr *src_addr;
738 
739 			/* The destination address is set in remote for this
740 			 * socket type.
741 			 */
742 			dst_addr = (struct sockaddr_ll *)&context->remote;
743 			src_addr = (struct sockaddr_ll_ptr *)&context->local;
744 
745 			net_pkt_lladdr_dst(pkt)->addr = dst_addr->sll_addr;
746 			net_pkt_lladdr_dst(pkt)->len =
747 						sizeof(struct net_eth_addr);
748 			net_pkt_lladdr_src(pkt)->addr = src_addr->sll_addr;
749 			net_pkt_lladdr_src(pkt)->len =
750 						sizeof(struct net_eth_addr);
751 			ptype = dst_addr->sll_protocol;
752 		} else {
753 			goto send;
754 		}
755 	} else if (IS_ENABLED(CONFIG_NET_L2_PTP) && net_pkt_is_ptp(pkt)) {
756 		ptype = htons(NET_ETH_PTYPE_PTP);
757 	} else if (IS_ENABLED(CONFIG_NET_LLDP) && net_pkt_is_lldp(pkt)) {
758 		ptype = htons(NET_ETH_PTYPE_LLDP);
759 	} else if (IS_ENABLED(CONFIG_NET_ARP)) {
760 		/* Unknown type: Unqueued pkt is an ARP reply.
761 		 */
762 		ptype = htons(NET_ETH_PTYPE_ARP);
763 		net_pkt_set_family(pkt, AF_INET);
764 	} else {
765 		ret = -ENOTSUP;
766 		goto error;
767 	}
768 
769 	/* If the ll dst addr has not been set before, let's assume
770 	 * temporarily it's a broadcast one. When filling the header,
771 	 * it might detect this should be multicast and act accordingly.
772 	 */
773 	if (!net_pkt_lladdr_dst(pkt)->addr) {
774 		net_pkt_lladdr_dst(pkt)->addr = (uint8_t *)broadcast_eth_addr.addr;
775 		net_pkt_lladdr_dst(pkt)->len = sizeof(struct net_eth_addr);
776 	}
777 
778 	/* Then set the ethernet header. Note that the iface parameter tells
779 	 * where we are actually sending the packet. The interface in net_pkt
780 	 * is used to determine if the VLAN header is added to Ethernet frame.
781 	 */
782 	if (!ethernet_fill_header(ctx, iface, pkt, ptype)) {
783 		ret = -ENOMEM;
784 		goto arp_error;
785 	}
786 
787 	net_pkt_cursor_init(pkt);
788 
789 send:
790 	if (IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE) &&
791 	    net_eth_iface_is_bridged(ctx) && !net_pkt_is_l2_bridged(pkt)) {
792 		struct net_if *bridge = net_eth_get_bridge(ctx);
793 		struct net_pkt *out_pkt;
794 
795 		out_pkt = net_pkt_clone(pkt, K_NO_WAIT);
796 		if (out_pkt == NULL) {
797 			ret = -ENOMEM;
798 			goto error;
799 		}
800 
801 		net_pkt_set_l2_bridged(out_pkt, true);
802 		net_pkt_set_iface(out_pkt, bridge);
803 		net_pkt_set_orig_iface(out_pkt, iface);
804 
805 		NET_DBG("Passing pkt %p (orig %p) to bridge %d from %d",
806 			out_pkt, pkt, net_if_get_by_iface(bridge),
807 			net_if_get_by_iface(iface));
808 
809 		(void)net_if_queue_tx(bridge, out_pkt);
810 	}
811 
812 	ret = net_l2_send(api->send, net_if_get_device(iface), iface, pkt);
813 	if (ret != 0) {
814 		eth_stats_update_errors_tx(iface);
815 		ethernet_remove_l2_header(pkt);
816 		goto arp_error;
817 	}
818 
819 	ethernet_update_tx_stats(iface, pkt);
820 
821 	ret = net_pkt_get_len(pkt);
822 	ethernet_remove_l2_header(pkt);
823 
824 	net_pkt_unref(pkt);
825 error:
826 	return ret;
827 
828 arp_error:
829 	if (IS_ENABLED(CONFIG_NET_ARP) && ptype == htons(NET_ETH_PTYPE_ARP)) {
830 		/* Original packet was added to ARP's pending Q, so, to avoid it
831 		 * being freed, take a reference, the reference is dropped when we
832 		 * clear the pending Q in ARP and then it will be freed by net_if.
833 		 */
834 		net_pkt_ref(orig_pkt);
835 		if (net_arp_clear_pending(
836 			    iface, (struct in_addr *)NET_IPV4_HDR(pkt)->dst)) {
837 			NET_DBG("Could not find pending ARP entry");
838 		}
839 		/* Free the ARP request */
840 		net_pkt_unref(pkt);
841 	}
842 
843 	return ret;
844 }
845 
ethernet_enable(struct net_if * iface,bool state)846 static inline int ethernet_enable(struct net_if *iface, bool state)
847 {
848 	int ret = 0;
849 	const struct ethernet_api *eth =
850 		net_if_get_device(iface)->api;
851 
852 	if (!eth) {
853 		return -ENOENT;
854 	}
855 
856 	if (!state) {
857 		net_arp_clear_cache(iface);
858 
859 		if (eth->stop) {
860 			ret = eth->stop(net_if_get_device(iface));
861 		}
862 	} else {
863 		if (eth->start) {
864 			ret = eth->start(net_if_get_device(iface));
865 		}
866 	}
867 
868 	return ret;
869 }
870 
ethernet_flags(struct net_if * iface)871 enum net_l2_flags ethernet_flags(struct net_if *iface)
872 {
873 	struct ethernet_context *ctx = net_if_l2_data(iface);
874 
875 	return ctx->ethernet_l2_flags;
876 }
877 
878 #if defined(CONFIG_NET_L2_ETHERNET_RESERVE_HEADER)
ethernet_l2_alloc(struct net_if * iface,struct net_pkt * pkt,size_t size,enum net_ip_protocol proto,k_timeout_t timeout)879 static int ethernet_l2_alloc(struct net_if *iface, struct net_pkt *pkt,
880 			     size_t size, enum net_ip_protocol proto,
881 			     k_timeout_t timeout)
882 {
883 	return net_pkt_alloc_buffer_with_reserve(pkt, size,
884 						 get_reserve_ll_header_size(iface),
885 						 proto, timeout);
886 }
887 #else
888 #define ethernet_l2_alloc NULL
889 #endif
890 
891 NET_L2_INIT(ETHERNET_L2, ethernet_recv, ethernet_send, ethernet_enable,
892 	    ethernet_flags, ethernet_l2_alloc);
893 
carrier_on_off(struct k_work * work)894 static void carrier_on_off(struct k_work *work)
895 {
896 	struct ethernet_context *ctx = CONTAINER_OF(work, struct ethernet_context,
897 						    carrier_work);
898 	bool eth_carrier_up;
899 
900 	if (ctx->iface == NULL) {
901 		return;
902 	}
903 
904 	eth_carrier_up = atomic_test_bit(&ctx->flags, ETH_CARRIER_UP);
905 
906 	if (eth_carrier_up == ctx->is_net_carrier_up) {
907 		return;
908 	}
909 
910 	ctx->is_net_carrier_up = eth_carrier_up;
911 
912 	NET_DBG("Carrier %s for interface %p", eth_carrier_up ? "ON" : "OFF",
913 		ctx->iface);
914 
915 	if (eth_carrier_up) {
916 		ethernet_mgmt_raise_carrier_on_event(ctx->iface);
917 		net_if_carrier_on(ctx->iface);
918 	} else {
919 		ethernet_mgmt_raise_carrier_off_event(ctx->iface);
920 		net_if_carrier_off(ctx->iface);
921 	}
922 }
923 
net_eth_carrier_on(struct net_if * iface)924 void net_eth_carrier_on(struct net_if *iface)
925 {
926 	struct ethernet_context *ctx = net_if_l2_data(iface);
927 
928 	if (!atomic_test_and_set_bit(&ctx->flags, ETH_CARRIER_UP)) {
929 		k_work_submit(&ctx->carrier_work);
930 	}
931 }
932 
net_eth_carrier_off(struct net_if * iface)933 void net_eth_carrier_off(struct net_if *iface)
934 {
935 	struct ethernet_context *ctx = net_if_l2_data(iface);
936 
937 	if (atomic_test_and_clear_bit(&ctx->flags, ETH_CARRIER_UP)) {
938 		k_work_submit(&ctx->carrier_work);
939 	}
940 }
941 
net_eth_get_phy(struct net_if * iface)942 const struct device *net_eth_get_phy(struct net_if *iface)
943 {
944 	const struct device *dev = net_if_get_device(iface);
945 	const struct ethernet_api *api = dev->api;
946 
947 	if (!api) {
948 		return NULL;
949 	}
950 
951 	if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) {
952 		return NULL;
953 	}
954 
955 	if (!api->get_phy) {
956 		return NULL;
957 	}
958 
959 	return api->get_phy(net_if_get_device(iface));
960 }
961 
962 #if defined(CONFIG_PTP_CLOCK)
net_eth_get_ptp_clock(struct net_if * iface)963 const struct device *net_eth_get_ptp_clock(struct net_if *iface)
964 {
965 	const struct device *dev = net_if_get_device(iface);
966 	const struct ethernet_api *api = dev->api;
967 
968 	if (!api) {
969 		return NULL;
970 	}
971 
972 	if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) {
973 		return NULL;
974 	}
975 
976 	if (!(net_eth_get_hw_capabilities(iface) & ETHERNET_PTP)) {
977 		return NULL;
978 	}
979 
980 	if (!api->get_ptp_clock) {
981 		return NULL;
982 	}
983 
984 	return api->get_ptp_clock(net_if_get_device(iface));
985 }
986 #endif /* CONFIG_PTP_CLOCK */
987 
988 #if defined(CONFIG_PTP_CLOCK)
z_impl_net_eth_get_ptp_clock_by_index(int index)989 const struct device *z_impl_net_eth_get_ptp_clock_by_index(int index)
990 {
991 	struct net_if *iface;
992 
993 	iface = net_if_get_by_index(index);
994 	if (!iface) {
995 		return NULL;
996 	}
997 
998 	return net_eth_get_ptp_clock(iface);
999 }
1000 
1001 #ifdef CONFIG_USERSPACE
z_vrfy_net_eth_get_ptp_clock_by_index(int index)1002 static inline const struct device *z_vrfy_net_eth_get_ptp_clock_by_index(int index)
1003 {
1004 	return z_impl_net_eth_get_ptp_clock_by_index(index);
1005 }
1006 #include <zephyr/syscalls/net_eth_get_ptp_clock_by_index_mrsh.c>
1007 #endif /* CONFIG_USERSPACE */
1008 #else /* CONFIG_PTP_CLOCK */
z_impl_net_eth_get_ptp_clock_by_index(int index)1009 const struct device *z_impl_net_eth_get_ptp_clock_by_index(int index)
1010 {
1011 	ARG_UNUSED(index);
1012 
1013 	return NULL;
1014 }
1015 #endif /* CONFIG_PTP_CLOCK */
1016 
1017 #if defined(CONFIG_NET_L2_PTP)
net_eth_get_ptp_port(struct net_if * iface)1018 int net_eth_get_ptp_port(struct net_if *iface)
1019 {
1020 	struct ethernet_context *ctx = net_if_l2_data(iface);
1021 
1022 	return ctx->port;
1023 }
1024 
net_eth_set_ptp_port(struct net_if * iface,int port)1025 void net_eth_set_ptp_port(struct net_if *iface, int port)
1026 {
1027 	struct ethernet_context *ctx = net_if_l2_data(iface);
1028 
1029 	ctx->port = port;
1030 }
1031 #endif /* CONFIG_NET_L2_PTP */
1032 
1033 #if defined(CONFIG_NET_PROMISCUOUS_MODE)
net_eth_promisc_mode(struct net_if * iface,bool enable)1034 int net_eth_promisc_mode(struct net_if *iface, bool enable)
1035 {
1036 	struct ethernet_req_params params;
1037 
1038 	if (!(net_eth_get_hw_capabilities(iface) & ETHERNET_PROMISC_MODE)) {
1039 		return -ENOTSUP;
1040 	}
1041 
1042 	params.promisc_mode = enable;
1043 
1044 	return net_mgmt(NET_REQUEST_ETHERNET_SET_PROMISC_MODE, iface,
1045 			&params, sizeof(struct ethernet_req_params));
1046 }
1047 #endif/* CONFIG_NET_PROMISCUOUS_MODE */
1048 
net_eth_txinjection_mode(struct net_if * iface,bool enable)1049 int net_eth_txinjection_mode(struct net_if *iface, bool enable)
1050 {
1051 	struct ethernet_req_params params;
1052 
1053 	if (!(net_eth_get_hw_capabilities(iface) & ETHERNET_TXINJECTION_MODE)) {
1054 		return -ENOTSUP;
1055 	}
1056 
1057 	params.txinjection_mode = enable;
1058 
1059 	return net_mgmt(NET_REQUEST_ETHERNET_SET_TXINJECTION_MODE, iface,
1060 			&params, sizeof(struct ethernet_req_params));
1061 }
1062 
net_eth_mac_filter(struct net_if * iface,struct net_eth_addr * mac,enum ethernet_filter_type type,bool enable)1063 int net_eth_mac_filter(struct net_if *iface, struct net_eth_addr *mac,
1064 		       enum ethernet_filter_type type, bool enable)
1065 {
1066 #ifdef CONFIG_NET_L2_ETHERNET_MGMT
1067 	struct ethernet_req_params params;
1068 
1069 	if (!(net_eth_get_hw_capabilities(iface) & ETHERNET_HW_FILTERING)) {
1070 		return -ENOTSUP;
1071 	}
1072 
1073 	memcpy(&params.filter.mac_address, mac, sizeof(struct net_eth_addr));
1074 	params.filter.type = type;
1075 	params.filter.set = enable;
1076 
1077 	return net_mgmt(NET_REQUEST_ETHERNET_SET_MAC_FILTER, iface, &params,
1078 			sizeof(struct ethernet_req_params));
1079 #else
1080 	ARG_UNUSED(iface);
1081 	ARG_UNUSED(mac);
1082 	ARG_UNUSED(type);
1083 	ARG_UNUSED(enable);
1084 
1085 	return -ENOTSUP;
1086 #endif
1087 }
1088 
ethernet_init(struct net_if * iface)1089 void ethernet_init(struct net_if *iface)
1090 {
1091 	struct ethernet_context *ctx = net_if_l2_data(iface);
1092 
1093 	NET_DBG("Initializing Ethernet L2 %p for iface %d (%p)", ctx,
1094 		net_if_get_by_iface(iface), iface);
1095 
1096 	ctx->ethernet_l2_flags = NET_L2_MULTICAST;
1097 	ctx->iface = iface;
1098 	k_work_init(&ctx->carrier_work, carrier_on_off);
1099 
1100 	if (net_eth_get_hw_capabilities(iface) & ETHERNET_PROMISC_MODE) {
1101 		ctx->ethernet_l2_flags |= NET_L2_PROMISC_MODE;
1102 	}
1103 
1104 #if defined(CONFIG_NET_NATIVE_IP) && !defined(CONFIG_NET_RAW_MODE)
1105 	if (net_eth_get_hw_capabilities(iface) & ETHERNET_HW_FILTERING) {
1106 		net_if_mcast_mon_register(&mcast_monitor, NULL, ethernet_mcast_monitor_cb);
1107 	}
1108 #endif
1109 
1110 	net_arp_init();
1111 
1112 	ctx->is_init = true;
1113 }
1114