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