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_raw(NET_IPV4_HDR(pkt)->dst) ||
198 net_ipv4_is_addr_bcast_raw(net_pkt_iface(pkt),
199 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_raw(net_pkt_iface(pkt),
469 NET_IPV4_HDR(pkt)->dst) ||
470 net_ipv4_is_addr_mcast_raw(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_raw(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,struct net_pkt ** out)492 static int ethernet_ll_prepare_on_ipv4(struct net_if *iface,
493 struct net_pkt *pkt,
494 struct net_pkt **out)
495 {
496 struct ethernet_context *ctx = net_if_l2_data(iface);
497
498 if (IS_ENABLED(CONFIG_NET_VLAN) &&
499 net_pkt_vlan_tag(pkt) != NET_VLAN_TAG_UNSPEC &&
500 net_eth_is_vlan_enabled(ctx, net_pkt_iface(pkt))) {
501 iface = net_eth_get_vlan_iface(iface,
502 net_pkt_vlan_tag(pkt));
503 net_pkt_set_iface(pkt, iface);
504 }
505
506 if (ethernet_ipv4_dst_is_broadcast_or_mcast(pkt)) {
507 return NET_ARP_COMPLETE;
508 }
509
510 if (IS_ENABLED(CONFIG_NET_ARP)) {
511 return net_arp_prepare(pkt, (struct in_addr *)NET_IPV4_HDR(pkt)->dst, NULL, out);
512 }
513
514 return NET_ARP_COMPLETE;
515 }
516 #else
517 #define ethernet_ipv4_dst_is_broadcast_or_mcast(...) false
518 #define ethernet_fill_in_dst_on_ipv4_mcast(...) false
519 #define ethernet_ll_prepare_on_ipv4(...) NET_ARP_COMPLETE
520 #endif /* CONFIG_NET_IPV4 */
521
522 #ifdef CONFIG_NET_IPV6
ethernet_fill_in_dst_on_ipv6_mcast(struct net_pkt * pkt,struct net_eth_addr * dst)523 static bool ethernet_fill_in_dst_on_ipv6_mcast(struct net_pkt *pkt,
524 struct net_eth_addr *dst)
525 {
526 if (net_pkt_family(pkt) == AF_INET6 &&
527 net_ipv6_is_addr_mcast_raw(NET_IPV6_HDR(pkt)->dst)) {
528 memcpy(dst, (uint8_t *)multicast_eth_addr.addr,
529 sizeof(struct net_eth_addr) - 4);
530 memcpy((uint8_t *)dst + 2,
531 NET_IPV6_HDR(pkt)->dst + 12,
532 sizeof(struct net_eth_addr) - 2);
533
534 return true;
535 }
536
537 return false;
538 }
539 #else
540 #define ethernet_fill_in_dst_on_ipv6_mcast(...) false
541 #endif /* CONFIG_NET_IPV6 */
542
get_reserve_ll_header_size(struct net_if * iface)543 static inline size_t get_reserve_ll_header_size(struct net_if *iface)
544 {
545 bool is_vlan = false;
546
547 #if defined(CONFIG_NET_VLAN)
548 if (net_if_l2(iface) == &NET_L2_GET_NAME(VIRTUAL)) {
549 iface = net_eth_get_vlan_main(iface);
550 is_vlan = true;
551 }
552 #endif
553
554 if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) {
555 return 0U;
556 }
557
558 if (!IS_ENABLED(CONFIG_NET_L2_ETHERNET_RESERVE_HEADER)) {
559 return 0U;
560 }
561
562 if (is_vlan) {
563 return sizeof(struct net_eth_vlan_hdr);
564 } else {
565 return sizeof(struct net_eth_hdr);
566 }
567 }
568
ethernet_fill_header(struct ethernet_context * ctx,struct net_if * iface,struct net_pkt * pkt,uint32_t ptype)569 static struct net_buf *ethernet_fill_header(struct ethernet_context *ctx,
570 struct net_if *iface,
571 struct net_pkt *pkt,
572 uint32_t ptype)
573 {
574 struct net_if *orig_iface = iface;
575 struct net_buf *hdr_frag;
576 struct net_eth_hdr *hdr;
577 size_t reserve_ll_header;
578 size_t hdr_len;
579 bool is_vlan;
580
581 is_vlan = IS_ENABLED(CONFIG_NET_VLAN) &&
582 net_eth_is_vlan_enabled(ctx, iface) &&
583 net_pkt_vlan_tag(pkt) != NET_VLAN_TAG_UNSPEC;
584 if (is_vlan) {
585 orig_iface = net_eth_get_vlan_iface(iface, net_pkt_vlan_tag(pkt));
586 }
587
588 reserve_ll_header = get_reserve_ll_header_size(orig_iface);
589 if (reserve_ll_header > 0) {
590 hdr_len = reserve_ll_header;
591 hdr_frag = pkt->buffer;
592
593 NET_DBG("Making room for link header %zd bytes", hdr_len);
594
595 /* Make room for the header */
596 net_buf_push(pkt->buffer, hdr_len);
597 } else {
598 hdr_len = IS_ENABLED(CONFIG_NET_VLAN) ?
599 sizeof(struct net_eth_vlan_hdr) :
600 sizeof(struct net_eth_hdr);
601
602 hdr_frag = net_pkt_get_frag(pkt, hdr_len, NET_BUF_TIMEOUT);
603 if (!hdr_frag) {
604 return NULL;
605 }
606 }
607
608 if (is_vlan) {
609 struct net_eth_vlan_hdr *hdr_vlan;
610
611 if (reserve_ll_header == 0U) {
612 hdr_len = sizeof(struct net_eth_vlan_hdr);
613 net_buf_add(hdr_frag, hdr_len);
614 }
615
616 hdr_vlan = (struct net_eth_vlan_hdr *)(hdr_frag->data);
617
618 if (ptype == htons(NET_ETH_PTYPE_ARP) ||
619 (!ethernet_fill_in_dst_on_ipv4_mcast(pkt, &hdr_vlan->dst) &&
620 !ethernet_fill_in_dst_on_ipv6_mcast(pkt, &hdr_vlan->dst))) {
621 memcpy(&hdr_vlan->dst, net_pkt_lladdr_dst(pkt)->addr,
622 sizeof(struct net_eth_addr));
623 }
624
625 memcpy(&hdr_vlan->src, net_pkt_lladdr_src(pkt)->addr,
626 sizeof(struct net_eth_addr));
627
628 hdr_vlan->type = ptype;
629 hdr_vlan->vlan.tpid = htons(NET_ETH_PTYPE_VLAN);
630 hdr_vlan->vlan.tci = htons(net_pkt_vlan_tci(pkt));
631
632 print_vlan_ll_addrs(pkt, ntohs(hdr_vlan->type),
633 net_pkt_vlan_tci(pkt),
634 hdr_len,
635 &hdr_vlan->src, &hdr_vlan->dst, false);
636 } else {
637 hdr = (struct net_eth_hdr *)(hdr_frag->data);
638
639 if (reserve_ll_header == 0U) {
640 hdr_len = sizeof(struct net_eth_hdr);
641 net_buf_add(hdr_frag, hdr_len);
642 }
643
644 if (ptype == htons(NET_ETH_PTYPE_ARP) ||
645 (!ethernet_fill_in_dst_on_ipv4_mcast(pkt, &hdr->dst) &&
646 !ethernet_fill_in_dst_on_ipv6_mcast(pkt, &hdr->dst))) {
647 memcpy(&hdr->dst, net_pkt_lladdr_dst(pkt)->addr,
648 sizeof(struct net_eth_addr));
649 }
650
651 memcpy(&hdr->src, net_pkt_lladdr_src(pkt)->addr,
652 sizeof(struct net_eth_addr));
653
654 hdr->type = ptype;
655
656 print_ll_addrs(pkt, ntohs(hdr->type),
657 hdr_len, &hdr->src, &hdr->dst);
658 }
659
660 if (reserve_ll_header == 0U) {
661 net_pkt_frag_insert(pkt, hdr_frag);
662 }
663
664 return hdr_frag;
665 }
666
ethernet_update_tx_stats(struct net_if * iface,struct net_pkt * pkt)667 static void ethernet_update_tx_stats(struct net_if *iface, struct net_pkt *pkt)
668 {
669 struct net_eth_hdr *hdr = NET_ETH_HDR(pkt);
670
671 if (!IS_ENABLED(CONFIG_NET_STATISTICS_ETHERNET)) {
672 return;
673 }
674
675 eth_stats_update_bytes_tx(iface, net_pkt_get_len(pkt));
676 eth_stats_update_pkts_tx(iface);
677
678 if (net_eth_is_addr_multicast(&hdr->dst)) {
679 eth_stats_update_multicast_tx(iface);
680 } else if (net_eth_is_addr_broadcast(&hdr->dst)) {
681 eth_stats_update_broadcast_tx(iface);
682 }
683 }
684
ethernet_send(struct net_if * iface,struct net_pkt * pkt)685 static int ethernet_send(struct net_if *iface, struct net_pkt *pkt)
686 {
687 const struct ethernet_api *api = net_if_get_device(iface)->api;
688 struct ethernet_context *ctx = net_if_l2_data(iface);
689 uint16_t ptype = htons(net_pkt_ll_proto_type(pkt));
690 struct net_pkt *orig_pkt = pkt;
691 int ret;
692
693 if (!api) {
694 ret = -ENOENT;
695 goto error;
696 }
697
698 if (!api->send) {
699 ret = -ENOTSUP;
700 goto error;
701 }
702
703 /* We are trying to send a packet that is from bridge interface,
704 * so all the bits and pieces should be there (like Ethernet header etc)
705 * so just send it.
706 */
707 if (IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE) && net_pkt_is_l2_bridged(pkt)) {
708 goto send;
709 }
710
711 if (IS_ENABLED(CONFIG_NET_IPV4) && net_pkt_family(pkt) == AF_INET &&
712 net_pkt_ll_proto_type(pkt) == NET_ETH_PTYPE_IP) {
713 if (!net_pkt_ipv4_acd(pkt)) {
714 struct net_pkt *arp;
715
716 ret = ethernet_ll_prepare_on_ipv4(iface, pkt, &arp);
717 if (ret == NET_ARP_COMPLETE) {
718 /* ARP resolution complete, packet ready to send */
719 NET_DBG("Found ARP entry, sending pkt %p to iface %d (%p)",
720 pkt, net_if_get_by_iface(iface), iface);
721 } else if (ret == NET_ARP_PKT_REPLACED) {
722 /* Original pkt got queued and is replaced
723 * by an ARP request packet.
724 */
725 NET_DBG("Sending arp pkt %p (orig %p) to iface %d (%p)",
726 arp, pkt, net_if_get_by_iface(iface), iface);
727 net_pkt_unref(pkt);
728 pkt = arp;
729 ptype = htons(net_pkt_ll_proto_type(pkt));
730 } else if (ret == NET_ARP_PKT_QUEUED) {
731 /* Original pkt got queued, pending resolution
732 * of an ongoing ARP request.
733 */
734 NET_DBG("Pending ARP request, pkt %p queued", pkt);
735 net_pkt_unref(pkt);
736 ret = 0;
737 goto error;
738 } else {
739 __ASSERT_NO_MSG(ret < 0);
740 goto error;
741 }
742 }
743 } else if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET) &&
744 net_pkt_family(pkt) == AF_PACKET) {
745 struct net_context *context = net_pkt_context(pkt);
746
747 if (!(context && net_context_get_type(context) == SOCK_DGRAM)) {
748 /* Raw packet, just send it */
749 goto send;
750 }
751 }
752
753 if (ptype == 0) {
754 /* Caller of this function has not set the ptype */
755 NET_ERR("No protocol set for pkt %p", pkt);
756 ret = -ENOTSUP;
757 goto error;
758 }
759
760 /* If the ll dst addr has not been set before, let's assume
761 * temporarily it's a broadcast one. When filling the header,
762 * it might detect this should be multicast and act accordingly.
763 */
764 if (net_pkt_lladdr_dst(pkt)->len == 0) {
765 (void)net_linkaddr_set(net_pkt_lladdr_dst(pkt),
766 broadcast_eth_addr.addr,
767 sizeof(struct net_eth_addr));
768 }
769
770 /* Then set the ethernet header. Note that the iface parameter tells
771 * where we are actually sending the packet. The interface in net_pkt
772 * is used to determine if the VLAN header is added to Ethernet frame.
773 */
774 if (!ethernet_fill_header(ctx, iface, pkt, ptype)) {
775 ret = -ENOMEM;
776 goto arp_error;
777 }
778
779 net_pkt_cursor_init(pkt);
780
781 send:
782 if (IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE) &&
783 net_eth_iface_is_bridged(ctx) && !net_pkt_is_l2_bridged(pkt)) {
784 struct net_if *bridge = net_eth_get_bridge(ctx);
785 struct net_pkt *out_pkt;
786
787 out_pkt = net_pkt_clone(pkt, K_NO_WAIT);
788 if (out_pkt == NULL) {
789 ret = -ENOMEM;
790 goto error;
791 }
792
793 net_pkt_set_l2_bridged(out_pkt, true);
794 net_pkt_set_iface(out_pkt, bridge);
795 net_pkt_set_orig_iface(out_pkt, iface);
796
797 NET_DBG("Passing pkt %p (orig %p) to bridge %d from %d",
798 out_pkt, pkt, net_if_get_by_iface(bridge),
799 net_if_get_by_iface(iface));
800
801 (void)net_if_queue_tx(bridge, out_pkt);
802 }
803
804 ret = net_l2_send(api->send, net_if_get_device(iface), iface, pkt);
805 if (ret != 0) {
806 eth_stats_update_errors_tx(iface);
807 goto arp_error;
808 }
809
810 ethernet_update_tx_stats(iface, pkt);
811
812 ret = net_pkt_get_len(pkt);
813
814 net_pkt_unref(pkt);
815 error:
816 return ret;
817
818 arp_error:
819 if (IS_ENABLED(CONFIG_NET_ARP) && ptype == htons(NET_ETH_PTYPE_ARP)) {
820 /* Original packet was added to ARP's pending Q, so, to avoid it
821 * being freed, take a reference, the reference is dropped when we
822 * clear the pending Q in ARP and then it will be freed by net_if.
823 */
824 net_pkt_ref(orig_pkt);
825 if (net_arp_clear_pending(
826 iface, (struct in_addr *)NET_IPV4_HDR(pkt)->dst)) {
827 NET_DBG("Could not find pending ARP entry");
828 }
829 /* Free the ARP request */
830 net_pkt_unref(pkt);
831 }
832
833 return ret;
834 }
835
ethernet_enable(struct net_if * iface,bool state)836 static inline int ethernet_enable(struct net_if *iface, bool state)
837 {
838 int ret = 0;
839 const struct ethernet_api *eth =
840 net_if_get_device(iface)->api;
841
842 if (!eth) {
843 return -ENOENT;
844 }
845
846 if (!state) {
847 net_arp_clear_cache(iface);
848
849 if (eth->stop) {
850 ret = eth->stop(net_if_get_device(iface));
851 }
852 } else {
853 if (eth->start) {
854 ret = eth->start(net_if_get_device(iface));
855 }
856 }
857
858 return ret;
859 }
860
ethernet_flags(struct net_if * iface)861 enum net_l2_flags ethernet_flags(struct net_if *iface)
862 {
863 struct ethernet_context *ctx = net_if_l2_data(iface);
864
865 return ctx->ethernet_l2_flags;
866 }
867
868 #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)869 static int ethernet_l2_alloc(struct net_if *iface, struct net_pkt *pkt,
870 size_t size, enum net_ip_protocol proto,
871 k_timeout_t timeout)
872 {
873 size_t reserve = get_reserve_ll_header_size(iface);
874 struct ethernet_config config;
875
876 if (net_eth_get_hw_config(iface, ETHERNET_CONFIG_TYPE_EXTRA_TX_PKT_HEADROOM,
877 &config) == 0) {
878 reserve += config.extra_tx_pkt_headroom;
879 }
880
881 return net_pkt_alloc_buffer_with_reserve(pkt, size, reserve,
882 proto, timeout);
883 }
884 #else
885 #define ethernet_l2_alloc NULL
886 #endif
887
888 NET_L2_INIT(ETHERNET_L2, ethernet_recv, ethernet_send, ethernet_enable,
889 ethernet_flags, ethernet_l2_alloc);
890
carrier_on_off(struct k_work * work)891 static void carrier_on_off(struct k_work *work)
892 {
893 struct ethernet_context *ctx = CONTAINER_OF(work, struct ethernet_context,
894 carrier_work);
895 bool eth_carrier_up;
896
897 if (ctx->iface == NULL) {
898 return;
899 }
900
901 eth_carrier_up = atomic_test_bit(&ctx->flags, ETH_CARRIER_UP);
902
903 if (eth_carrier_up == ctx->is_net_carrier_up) {
904 return;
905 }
906
907 ctx->is_net_carrier_up = eth_carrier_up;
908
909 NET_DBG("Carrier %s for interface %p", eth_carrier_up ? "ON" : "OFF",
910 ctx->iface);
911
912 if (eth_carrier_up) {
913 ethernet_mgmt_raise_carrier_on_event(ctx->iface);
914 net_if_carrier_on(ctx->iface);
915 } else {
916 ethernet_mgmt_raise_carrier_off_event(ctx->iface);
917 net_if_carrier_off(ctx->iface);
918 }
919 }
920
net_eth_carrier_on(struct net_if * iface)921 void net_eth_carrier_on(struct net_if *iface)
922 {
923 struct ethernet_context *ctx = net_if_l2_data(iface);
924
925 if (!atomic_test_and_set_bit(&ctx->flags, ETH_CARRIER_UP)) {
926 k_work_submit(&ctx->carrier_work);
927 }
928 }
929
net_eth_carrier_off(struct net_if * iface)930 void net_eth_carrier_off(struct net_if *iface)
931 {
932 struct ethernet_context *ctx = net_if_l2_data(iface);
933
934 if (atomic_test_and_clear_bit(&ctx->flags, ETH_CARRIER_UP)) {
935 k_work_submit(&ctx->carrier_work);
936 }
937 }
938
net_eth_get_phy(struct net_if * iface)939 const struct device *net_eth_get_phy(struct net_if *iface)
940 {
941 const struct device *dev = net_if_get_device(iface);
942 const struct ethernet_api *api = dev->api;
943
944 if (!api) {
945 return NULL;
946 }
947
948 if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) {
949 return NULL;
950 }
951
952 if (!api->get_phy) {
953 return NULL;
954 }
955
956 return api->get_phy(net_if_get_device(iface));
957 }
958
959 #if defined(CONFIG_PTP_CLOCK)
net_eth_get_ptp_clock(struct net_if * iface)960 const struct device *net_eth_get_ptp_clock(struct net_if *iface)
961 {
962 const struct device *dev = net_if_get_device(iface);
963 const struct ethernet_api *api = dev->api;
964
965 if (!api) {
966 return NULL;
967 }
968
969 if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) {
970 return NULL;
971 }
972
973 if (!(net_eth_get_hw_capabilities(iface) & ETHERNET_PTP)) {
974 return NULL;
975 }
976
977 if (!api->get_ptp_clock) {
978 return NULL;
979 }
980
981 return api->get_ptp_clock(net_if_get_device(iface));
982 }
983 #endif /* CONFIG_PTP_CLOCK */
984
985 #if defined(CONFIG_PTP_CLOCK)
z_impl_net_eth_get_ptp_clock_by_index(int index)986 const struct device *z_impl_net_eth_get_ptp_clock_by_index(int index)
987 {
988 struct net_if *iface;
989
990 iface = net_if_get_by_index(index);
991 if (!iface) {
992 return NULL;
993 }
994
995 return net_eth_get_ptp_clock(iface);
996 }
997
998 #ifdef CONFIG_USERSPACE
z_vrfy_net_eth_get_ptp_clock_by_index(int index)999 static inline const struct device *z_vrfy_net_eth_get_ptp_clock_by_index(int index)
1000 {
1001 return z_impl_net_eth_get_ptp_clock_by_index(index);
1002 }
1003 #include <zephyr/syscalls/net_eth_get_ptp_clock_by_index_mrsh.c>
1004 #endif /* CONFIG_USERSPACE */
1005 #else /* CONFIG_PTP_CLOCK */
z_impl_net_eth_get_ptp_clock_by_index(int index)1006 const struct device *z_impl_net_eth_get_ptp_clock_by_index(int index)
1007 {
1008 ARG_UNUSED(index);
1009
1010 return NULL;
1011 }
1012 #endif /* CONFIG_PTP_CLOCK */
1013
1014 #if defined(CONFIG_NET_L2_PTP)
net_eth_get_ptp_port(struct net_if * iface)1015 int net_eth_get_ptp_port(struct net_if *iface)
1016 {
1017 struct ethernet_context *ctx = net_if_l2_data(iface);
1018
1019 return ctx->port;
1020 }
1021
net_eth_set_ptp_port(struct net_if * iface,int port)1022 void net_eth_set_ptp_port(struct net_if *iface, int port)
1023 {
1024 struct ethernet_context *ctx = net_if_l2_data(iface);
1025
1026 ctx->port = port;
1027 }
1028 #endif /* CONFIG_NET_L2_PTP */
1029
1030 #if defined(CONFIG_NET_PROMISCUOUS_MODE)
net_eth_promisc_mode(struct net_if * iface,bool enable)1031 int net_eth_promisc_mode(struct net_if *iface, bool enable)
1032 {
1033 struct ethernet_req_params params;
1034
1035 if (!(net_eth_get_hw_capabilities(iface) & ETHERNET_PROMISC_MODE)) {
1036 return -ENOTSUP;
1037 }
1038
1039 params.promisc_mode = enable;
1040
1041 return net_mgmt(NET_REQUEST_ETHERNET_SET_PROMISC_MODE, iface,
1042 ¶ms, sizeof(struct ethernet_req_params));
1043 }
1044 #endif/* CONFIG_NET_PROMISCUOUS_MODE */
1045
net_eth_txinjection_mode(struct net_if * iface,bool enable)1046 int net_eth_txinjection_mode(struct net_if *iface, bool enable)
1047 {
1048 struct ethernet_req_params params;
1049
1050 if (!(net_eth_get_hw_capabilities(iface) & ETHERNET_TXINJECTION_MODE)) {
1051 return -ENOTSUP;
1052 }
1053
1054 params.txinjection_mode = enable;
1055
1056 return net_mgmt(NET_REQUEST_ETHERNET_SET_TXINJECTION_MODE, iface,
1057 ¶ms, sizeof(struct ethernet_req_params));
1058 }
1059
net_eth_mac_filter(struct net_if * iface,struct net_eth_addr * mac,enum ethernet_filter_type type,bool enable)1060 int net_eth_mac_filter(struct net_if *iface, struct net_eth_addr *mac,
1061 enum ethernet_filter_type type, bool enable)
1062 {
1063 #ifdef CONFIG_NET_L2_ETHERNET_MGMT
1064 struct ethernet_req_params params;
1065
1066 if (!(net_eth_get_hw_capabilities(iface) & ETHERNET_HW_FILTERING)) {
1067 return -ENOTSUP;
1068 }
1069
1070 memcpy(¶ms.filter.mac_address, mac, sizeof(struct net_eth_addr));
1071 params.filter.type = type;
1072 params.filter.set = enable;
1073
1074 return net_mgmt(NET_REQUEST_ETHERNET_SET_MAC_FILTER, iface, ¶ms,
1075 sizeof(struct ethernet_req_params));
1076 #else
1077 ARG_UNUSED(iface);
1078 ARG_UNUSED(mac);
1079 ARG_UNUSED(type);
1080 ARG_UNUSED(enable);
1081
1082 return -ENOTSUP;
1083 #endif
1084 }
1085
ethernet_init(struct net_if * iface)1086 void ethernet_init(struct net_if *iface)
1087 {
1088 struct ethernet_context *ctx = net_if_l2_data(iface);
1089
1090 NET_DBG("Initializing Ethernet L2 %p for iface %d (%p)", ctx,
1091 net_if_get_by_iface(iface), iface);
1092
1093 #if defined(CONFIG_NET_DSA) && !defined(CONFIG_NET_DSA_DEPRECATED)
1094 /* DSA port may need to handle flags */
1095 dsa_eth_init(iface);
1096 #endif
1097
1098 ctx->ethernet_l2_flags = NET_L2_MULTICAST;
1099 ctx->iface = iface;
1100 k_work_init(&ctx->carrier_work, carrier_on_off);
1101
1102 if (net_eth_get_hw_capabilities(iface) & ETHERNET_PROMISC_MODE) {
1103 ctx->ethernet_l2_flags |= NET_L2_PROMISC_MODE;
1104 }
1105
1106 #if defined(CONFIG_NET_NATIVE_IP) && !defined(CONFIG_NET_RAW_MODE)
1107 if (net_eth_get_hw_capabilities(iface) & ETHERNET_HW_FILTERING) {
1108 net_if_mcast_mon_register(&mcast_monitor, NULL, ethernet_mcast_monitor_cb);
1109 }
1110 #endif
1111
1112 net_arp_init();
1113
1114 ctx->is_init = true;
1115 }
1116