Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
capture.h
Go to the documentation of this file.
1
7/*
8 * Copyright (c) 2021 Intel Corporation
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12
13#ifndef ZEPHYR_INCLUDE_NET_CAPTURE_H_
14#define ZEPHYR_INCLUDE_NET_CAPTURE_H_
15
16#include <zephyr/kernel.h>
17#include <zephyr/device.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
32struct net_if;
33struct net_pkt;
34struct device;
35
36struct net_capture_interface_api {
40 int (*cleanup)(const struct device *dev);
41
43 int (*enable)(const struct device *dev, struct net_if *iface);
44
46 int (*disable)(const struct device *dev);
47
50 bool (*is_enabled)(const struct device *dev);
51
53 int (*send)(const struct device *dev, struct net_if *iface, struct net_pkt *pkt);
54};
55
73int net_capture_setup(const char *remote_addr, const char *my_local_addr, const char *peer_addr,
74 const struct device **dev);
75
87static inline int net_capture_cleanup(const struct device *dev)
88{
89#if defined(CONFIG_NET_CAPTURE)
90 const struct net_capture_interface_api *api =
91 (const struct net_capture_interface_api *)dev->api;
92
93 return api->cleanup(dev);
94#else
95 ARG_UNUSED(dev);
96
97 return -ENOTSUP;
98#endif
99}
100
113static inline int net_capture_enable(const struct device *dev, struct net_if *iface)
114{
115#if defined(CONFIG_NET_CAPTURE)
116 const struct net_capture_interface_api *api =
117 (const struct net_capture_interface_api *)dev->api;
118
119 return api->enable(dev, iface);
120#else
121 ARG_UNUSED(dev);
122 ARG_UNUSED(iface);
123
124 return -ENOTSUP;
125#endif
126}
127
136static inline bool net_capture_is_enabled(const struct device *dev)
137{
138#if defined(CONFIG_NET_CAPTURE)
139 const struct net_capture_interface_api *api;
140
141 if (dev == NULL) {
142 /* TODO: Go through all capture devices instead of one */
143 dev = device_get_binding("NET_CAPTURE0");
144 if (dev == NULL) {
145 return false;
146 }
147 }
148
149 api = (const struct net_capture_interface_api *)dev->api;
150
151 return api->is_enabled(dev);
152#else
153 ARG_UNUSED(dev);
154
155 return false;
156#endif
157}
158
166static inline int net_capture_disable(const struct device *dev)
167{
168#if defined(CONFIG_NET_CAPTURE)
169 const struct net_capture_interface_api *api =
170 (const struct net_capture_interface_api *)dev->api;
171
172 return api->disable(dev);
173#else
174 ARG_UNUSED(dev);
175
176 return -ENOTSUP;
177#endif
178}
179
191static inline int net_capture_send(const struct device *dev, struct net_if *iface,
192 struct net_pkt *pkt)
193{
194#if defined(CONFIG_NET_CAPTURE)
195 const struct net_capture_interface_api *api =
196 (const struct net_capture_interface_api *)dev->api;
197
198 return api->send(dev, iface, pkt);
199#else
200 ARG_UNUSED(dev);
201 ARG_UNUSED(iface);
202 ARG_UNUSED(pkt);
203
204 return -ENOTSUP;
205#endif
206}
207
215#if defined(CONFIG_NET_CAPTURE)
216void net_capture_pkt(struct net_if *iface, struct net_pkt *pkt);
217#else
218static inline void net_capture_pkt(struct net_if *iface, struct net_pkt *pkt)
219{
220 ARG_UNUSED(iface);
221 ARG_UNUSED(pkt);
222}
223#endif
224
236#if defined(CONFIG_NET_CAPTURE)
237int net_capture_pkt_with_status(struct net_if *iface, struct net_pkt *pkt);
238#else
239static inline int net_capture_pkt_with_status(struct net_if *iface, struct net_pkt *pkt)
240{
241 ARG_UNUSED(iface);
242 ARG_UNUSED(pkt);
243
244 return -ENOTSUP;
245}
246#endif
247
251enum net_capture_packet_type {
252 NET_CAPTURE_HOST,
253 NET_CAPTURE_BROADCAST,
254 NET_CAPTURE_MULTICAST,
255 NET_CAPTURE_OTHERHOST,
256 NET_CAPTURE_OUTGOING,
257};
258
259#define NET_CAPTURE_LL_ADDRLEN 8
262struct net_capture_cooked {
264 uint16_t hatype;
266 uint16_t halen;
268 uint8_t addr[NET_CAPTURE_LL_ADDRLEN];
269};
270
281#if defined(CONFIG_NET_CAPTURE_COOKED_MODE)
282int net_capture_cooked_setup(struct net_capture_cooked *ctx,
283 uint16_t hatype,
284 uint16_t halen,
285 uint8_t *addr);
286#else
287static inline int net_capture_cooked_setup(struct net_capture_cooked *ctx,
288 uint16_t hatype,
289 uint16_t halen,
290 uint8_t *addr)
291{
292 ARG_UNUSED(ctx);
293 ARG_UNUSED(hatype);
294 ARG_UNUSED(halen);
295 ARG_UNUSED(addr);
296
297 return -ENOTSUP;
298}
299#endif
300
315#if defined(CONFIG_NET_CAPTURE_COOKED_MODE)
316void net_capture_data(struct net_capture_cooked *ctx,
317 const uint8_t *data, size_t len,
318 enum net_capture_packet_type type,
319 uint16_t ptype);
320#else
321static inline void net_capture_data(struct net_capture_cooked *ctx,
322 const uint8_t *data, size_t len,
323 enum net_capture_packet_type type,
324 uint16_t ptype)
325{
326 ARG_UNUSED(ctx);
327 ARG_UNUSED(data);
328 ARG_UNUSED(len);
329 ARG_UNUSED(type);
330 ARG_UNUSED(ptype);
331}
332#endif
333
334struct net_capture_info {
335 const struct device *capture_dev;
336 struct net_if *capture_iface;
337 struct net_if *tunnel_iface;
338 struct sockaddr *peer;
339 struct sockaddr *local;
340 bool is_enabled;
341};
342
350typedef void (*net_capture_cb_t)(struct net_capture_info *info, void *user_data);
351
361#if defined(CONFIG_NET_CAPTURE)
362void net_capture_foreach(net_capture_cb_t cb, void *user_data);
363#else
364static inline void net_capture_foreach(net_capture_cb_t cb, void *user_data)
365{
366 ARG_UNUSED(cb);
367 ARG_UNUSED(user_data);
368}
369#endif
370
377#ifdef __cplusplus
378}
379#endif
380
381#endif /* ZEPHYR_INCLUDE_NET_CAPTURE_H_ */
static ssize_t send(int sock, const void *buf, size_t len, int flags)
POSIX wrapper for zsock_send.
Definition: socket.h:917
const struct device * device_get_binding(const char *name)
Get a device reference from its device::name field.
static int net_capture_disable(const struct device *dev)
Disable network packet capturing support.
Definition: capture.h:166
static bool net_capture_is_enabled(const struct device *dev)
Is network packet capture enabled or disabled.
Definition: capture.h:136
static int net_capture_cleanup(const struct device *dev)
Cleanup network packet capturing support.
Definition: capture.h:87
int net_capture_setup(const char *remote_addr, const char *my_local_addr, const char *peer_addr, const struct device **dev)
Setup network packet capturing support.
static int net_capture_enable(const struct device *dev, struct net_if *iface)
Enable network packet capturing support.
Definition: capture.h:113
#define ENOTSUP
Unsupported value.
Definition: errno.h:115
Public kernel APIs.
#define bool
Definition: stdbool.h:13
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition: device.h:399
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:405
Network Interface structure.
Definition: net_if.h:675
Network packet.
Definition: net_pkt.h:67
Generic sockaddr struct.
Definition: net_ip.h:385