1 /*
2 * Copyright (c) 2016 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @file
9 * @brief Public API for network L2 interface
10 */
11
12 #ifndef ZEPHYR_INCLUDE_NET_NET_L2_H_
13 #define ZEPHYR_INCLUDE_NET_NET_L2_H_
14
15 #include <device.h>
16 #include <net/buf.h>
17 #include <net/capture.h>
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 /**
24 * @brief Network Layer 2 abstraction layer
25 * @defgroup net_l2 Network L2 Abstraction Layer
26 * @ingroup networking
27 * @{
28 */
29
30 struct net_if;
31
32 /** L2 flags */
33 enum net_l2_flags {
34 /** IP multicast supported */
35 NET_L2_MULTICAST = BIT(0),
36
37 /** Do not joint solicited node multicast group */
38 NET_L2_MULTICAST_SKIP_JOIN_SOLICIT_NODE = BIT(1),
39
40 /** Is promiscuous mode supported */
41 NET_L2_PROMISC_MODE = BIT(2),
42
43 /** Is this L2 point-to-point with tunneling so no need to have
44 * IP address etc to network interface.
45 */
46 NET_L2_POINT_TO_POINT = BIT(3),
47 } __packed;
48
49 /**
50 * @brief Network L2 structure
51 *
52 * Used to provide an interface to lower network stack.
53 */
54 struct net_l2 {
55 /**
56 * This function is used by net core to get iface's L2 layer parsing
57 * what's relevant to itself.
58 */
59 enum net_verdict (*recv)(struct net_if *iface, struct net_pkt *pkt);
60
61 /**
62 * This function is used by net core to push a packet to lower layer
63 * (interface's L2), which in turn might work on the packet relevantly.
64 * (adding proper header etc...)
65 * Returns a negative error code, or the number of bytes sent otherwise.
66 */
67 int (*send)(struct net_if *iface, struct net_pkt *pkt);
68
69 /**
70 * This function is used to enable/disable traffic over a network
71 * interface. The function returns <0 if error and >=0 if no error.
72 */
73 int (*enable)(struct net_if *iface, bool state);
74
75 /**
76 * Return L2 flags for the network interface.
77 */
78 enum net_l2_flags (*get_flags)(struct net_if *iface);
79 };
80
81 /** @cond INTERNAL_HIDDEN */
82 #define NET_L2_GET_NAME(_name) _net_l2_##_name
83 #define NET_L2_DECLARE_PUBLIC(_name) \
84 extern const struct net_l2 NET_L2_GET_NAME(_name)
85 #define NET_L2_GET_CTX_TYPE(_name) _name##_CTX_TYPE
86
87 #ifdef CONFIG_NET_L2_VIRTUAL
88 #define VIRTUAL_L2 VIRTUAL
89 NET_L2_DECLARE_PUBLIC(VIRTUAL_L2);
90 #endif /* CONFIG_NET_L2_DUMMY */
91
92 #ifdef CONFIG_NET_L2_DUMMY
93 #define DUMMY_L2 DUMMY
94 #define DUMMY_L2_CTX_TYPE void*
95 NET_L2_DECLARE_PUBLIC(DUMMY_L2);
96 #endif /* CONFIG_NET_L2_DUMMY */
97
98 #ifdef CONFIG_NET_L2_ETHERNET
99 #define ETHERNET_L2 ETHERNET
100 NET_L2_DECLARE_PUBLIC(ETHERNET_L2);
101 #endif /* CONFIG_NET_L2_ETHERNET */
102
103 #ifdef CONFIG_NET_L2_PPP
104 #define PPP_L2 PPP
105 NET_L2_DECLARE_PUBLIC(PPP_L2);
106 #endif /* CONFIG_NET_L2_PPP */
107
108 #ifdef CONFIG_NET_L2_IEEE802154
109 #define IEEE802154_L2 IEEE802154
110 NET_L2_DECLARE_PUBLIC(IEEE802154_L2);
111 #endif /* CONFIG_NET_L2_IEEE802154 */
112
113 #ifdef CONFIG_NET_L2_BT
114 #define BLUETOOTH_L2 BLUETOOTH
115 #define BLUETOOTH_L2_CTX_TYPE void*
116 NET_L2_DECLARE_PUBLIC(BLUETOOTH_L2);
117 #endif /* CONFIG_NET_L2_BT */
118
119 #ifdef CONFIG_NET_L2_OPENTHREAD
120 #define OPENTHREAD_L2 OPENTHREAD
121 NET_L2_DECLARE_PUBLIC(OPENTHREAD_L2);
122 #endif /* CONFIG_NET_L2_OPENTHREAD */
123
124 #ifdef CONFIG_NET_L2_CANBUS_RAW
125 #define CANBUS_RAW_L2 CANBUS_RAW
126 #define CANBUS_RAW_L2_CTX_TYPE void*
127 NET_L2_DECLARE_PUBLIC(CANBUS_RAW_L2);
128 #endif /* CONFIG_NET_L2_CANBUS_RAW */
129
130 #ifdef CONFIG_NET_L2_CANBUS
131 #define CANBUS_L2 CANBUS
132 NET_L2_DECLARE_PUBLIC(CANBUS_L2);
133 #endif /* CONFIG_NET_L2_CANBUS */
134
135 #define NET_L2_INIT(_name, _recv_fn, _send_fn, _enable_fn, _get_flags_fn) \
136 const STRUCT_SECTION_ITERABLE(net_l2, \
137 NET_L2_GET_NAME(_name)) = { \
138 .recv = (_recv_fn), \
139 .send = (_send_fn), \
140 .enable = (_enable_fn), \
141 .get_flags = (_get_flags_fn), \
142 }
143
144 #define NET_L2_GET_DATA(name, sfx) _net_l2_data_##name##sfx
145
146 #define NET_L2_DATA_INIT(name, sfx, ctx_type) \
147 static ctx_type NET_L2_GET_DATA(name, sfx) __used;
148
149 typedef int (*net_l2_send_t)(const struct device *dev, struct net_pkt *pkt);
150
net_l2_send(net_l2_send_t send_fn,const struct device * dev,struct net_if * iface,struct net_pkt * pkt)151 static inline int net_l2_send(net_l2_send_t send_fn,
152 const struct device *dev,
153 struct net_if *iface,
154 struct net_pkt *pkt)
155 {
156 net_capture_pkt(iface, pkt);
157
158 return send_fn(dev, pkt);
159 }
160
161 /** @endcond */
162
163 /**
164 * @}
165 */
166
167 #ifdef __cplusplus
168 }
169 #endif
170
171 #endif /* ZEPHYR_INCLUDE_NET_NET_L2_H_ */
172