1 /*
2 * Copyright (c) 2024 Nordic Semiconductor
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(net_any, CONFIG_NET_PSEUDO_IFACE_LOG_LEVEL);
9
10 #include <zephyr/net/net_core.h>
11 #include <zephyr/net/net_if.h>
12 #include <zephyr/net/dummy.h>
13 #include <zephyr/net/virtual.h>
14
15 #include "net_private.h"
16 #include "net_stats.h"
17
18 struct any_context {
19 struct net_if *iface;
20 };
21
22 static struct any_context any_data;
23
any_iface_init(struct net_if * iface)24 static void any_iface_init(struct net_if *iface)
25 {
26 struct any_context *ctx = net_if_get_device(iface)->data;
27 int ret;
28
29 ctx->iface = iface;
30
31 ret = net_if_set_name(iface, "any");
32 if (ret < 0) {
33 NET_DBG("Cannot set any interface name (%d)", ret);
34 }
35
36 net_if_flag_set(iface, NET_IF_NO_AUTO_START);
37 net_if_flag_clear(iface, NET_IF_IPV6);
38 net_if_flag_clear(iface, NET_IF_IPV4);
39 }
40
any_recv(struct net_if * iface,struct net_pkt * pkt)41 static enum net_verdict any_recv(struct net_if *iface, struct net_pkt *pkt)
42 {
43 enum net_verdict verdict = NET_DROP;
44 struct virtual_interface_context *ctx;
45 sys_snode_t *first;
46
47 if (!pkt->buffer) {
48 goto drop;
49 }
50
51 first = sys_slist_peek_head(&iface->config.virtual_interfaces);
52 if (first == NULL) {
53 goto drop;
54 }
55
56 ctx = CONTAINER_OF(first, struct virtual_interface_context, node);
57 if (ctx == NULL) {
58 goto drop;
59 }
60
61 if (net_if_l2(ctx->virtual_iface)->recv == NULL) {
62 goto drop;
63 }
64
65 NET_DBG("Passing pkt %p (len %zd) to virtual L2",
66 pkt, net_pkt_get_len(pkt));
67
68 verdict = net_if_l2(ctx->virtual_iface)->recv(iface, pkt);
69
70 NET_DBG("Verdict for pkt %p is %s (%d)", pkt, net_verdict2str(verdict),
71 verdict);
72
73 drop:
74 return verdict;
75 }
76
77 static struct dummy_api any_api = {
78 .iface_api.init = any_iface_init,
79 .recv = any_recv,
80 };
81
82 NET_DEVICE_INIT(any,
83 "NET_ANY",
84 NULL,
85 NULL,
86 &any_data,
87 NULL,
88 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
89 &any_api,
90 DUMMY_L2,
91 NET_L2_GET_CTX_TYPE(DUMMY_L2),
92 1024);
93