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