Zephyr API Documentation 3.7.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
34struct net_if;
35struct net_pkt;
36struct device;
37
38struct net_capture_interface_api {
42 int (*cleanup)(const struct device *dev);
43
45 int (*enable)(const struct device *dev, struct net_if *iface);
46
48 int (*disable)(const struct device *dev);
49
52 bool (*is_enabled)(const struct device *dev);
53
55 int (*send)(const struct device *dev, struct net_if *iface, struct net_pkt *pkt);
56};
57
75int net_capture_setup(const char *remote_addr, const char *my_local_addr, const char *peer_addr,
76 const struct device **dev);
77
89static inline int net_capture_cleanup(const struct device *dev)
90{
91#if defined(CONFIG_NET_CAPTURE)
92 const struct net_capture_interface_api *api =
93 (const struct net_capture_interface_api *)dev->api;
94
95 return api->cleanup(dev);
96#else
97 ARG_UNUSED(dev);
98
99 return -ENOTSUP;
100#endif
101}
102
115static inline int net_capture_enable(const struct device *dev, struct net_if *iface)
116{
117#if defined(CONFIG_NET_CAPTURE)
118 const struct net_capture_interface_api *api =
119 (const struct net_capture_interface_api *)dev->api;
120
121 return api->enable(dev, iface);
122#else
123 ARG_UNUSED(dev);
124 ARG_UNUSED(iface);
125
126 return -ENOTSUP;
127#endif
128}
129
138static inline bool net_capture_is_enabled(const struct device *dev)
139{
140#if defined(CONFIG_NET_CAPTURE)
141 const struct net_capture_interface_api *api;
142
143 if (dev == NULL) {
144 /* TODO: Go through all capture devices instead of one */
145 dev = device_get_binding("NET_CAPTURE0");
146 if (dev == NULL) {
147 return false;
148 }
149 }
150
151 api = (const struct net_capture_interface_api *)dev->api;
152
153 return api->is_enabled(dev);
154#else
155 ARG_UNUSED(dev);
156
157 return false;
158#endif
159}
160
168static inline int net_capture_disable(const struct device *dev)
169{
170#if defined(CONFIG_NET_CAPTURE)
171 const struct net_capture_interface_api *api =
172 (const struct net_capture_interface_api *)dev->api;
173
174 return api->disable(dev);
175#else
176 ARG_UNUSED(dev);
177
178 return -ENOTSUP;
179#endif
180}
181
193static inline int net_capture_send(const struct device *dev, struct net_if *iface,
194 struct net_pkt *pkt)
195{
196#if defined(CONFIG_NET_CAPTURE)
197 const struct net_capture_interface_api *api =
198 (const struct net_capture_interface_api *)dev->api;
199
200 return api->send(dev, iface, pkt);
201#else
202 ARG_UNUSED(dev);
203 ARG_UNUSED(iface);
204 ARG_UNUSED(pkt);
205
206 return -ENOTSUP;
207#endif
208}
209
217#if defined(CONFIG_NET_CAPTURE)
218void net_capture_pkt(struct net_if *iface, struct net_pkt *pkt);
219#else
220static inline void net_capture_pkt(struct net_if *iface, struct net_pkt *pkt)
221{
222 ARG_UNUSED(iface);
223 ARG_UNUSED(pkt);
224}
225#endif
226
238#if defined(CONFIG_NET_CAPTURE)
239int net_capture_pkt_with_status(struct net_if *iface, struct net_pkt *pkt);
240#else
241static inline int net_capture_pkt_with_status(struct net_if *iface, struct net_pkt *pkt)
242{
243 ARG_UNUSED(iface);
244 ARG_UNUSED(pkt);
245
246 return -ENOTSUP;
247}
248#endif
249
253enum net_capture_packet_type {
254 NET_CAPTURE_HOST,
255 NET_CAPTURE_BROADCAST,
256 NET_CAPTURE_MULTICAST,
257 NET_CAPTURE_OTHERHOST,
258 NET_CAPTURE_OUTGOING,
259};
260
261#define NET_CAPTURE_LL_ADDRLEN 8
264struct net_capture_cooked {
266 uint16_t hatype;
268 uint16_t halen;
270 uint8_t addr[NET_CAPTURE_LL_ADDRLEN];
271};
272
283#if defined(CONFIG_NET_CAPTURE_COOKED_MODE)
284int net_capture_cooked_setup(struct net_capture_cooked *ctx,
285 uint16_t hatype,
286 uint16_t halen,
287 uint8_t *addr);
288#else
289static inline int net_capture_cooked_setup(struct net_capture_cooked *ctx,
290 uint16_t hatype,
291 uint16_t halen,
292 uint8_t *addr)
293{
294 ARG_UNUSED(ctx);
295 ARG_UNUSED(hatype);
296 ARG_UNUSED(halen);
297 ARG_UNUSED(addr);
298
299 return -ENOTSUP;
300}
301#endif
302
317#if defined(CONFIG_NET_CAPTURE_COOKED_MODE)
318void net_capture_data(struct net_capture_cooked *ctx,
319 const uint8_t *data, size_t len,
320 enum net_capture_packet_type type,
321 uint16_t ptype);
322#else
323static inline void net_capture_data(struct net_capture_cooked *ctx,
324 const uint8_t *data, size_t len,
325 enum net_capture_packet_type type,
326 uint16_t ptype)
327{
328 ARG_UNUSED(ctx);
329 ARG_UNUSED(data);
330 ARG_UNUSED(len);
331 ARG_UNUSED(type);
332 ARG_UNUSED(ptype);
333}
334#endif
335
336struct net_capture_info {
337 const struct device *capture_dev;
338 struct net_if *capture_iface;
339 struct net_if *tunnel_iface;
340 struct sockaddr *peer;
341 struct sockaddr *local;
342 bool is_enabled;
343};
344
352typedef void (*net_capture_cb_t)(struct net_capture_info *info, void *user_data);
353
363#if defined(CONFIG_NET_CAPTURE)
364void net_capture_foreach(net_capture_cb_t cb, void *user_data);
365#else
366static inline void net_capture_foreach(net_capture_cb_t cb, void *user_data)
367{
368 ARG_UNUSED(cb);
369 ARG_UNUSED(user_data);
370}
371#endif
372
379#ifdef __cplusplus
380}
381#endif
382
383#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:920
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:168
static bool net_capture_is_enabled(const struct device *dev)
Is network packet capture enabled or disabled.
Definition capture.h:138
static int net_capture_cleanup(const struct device *dev)
Cleanup network packet capturing support.
Definition capture.h:89
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:115
#define ENOTSUP
Unsupported value.
Definition errno.h:114
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:412
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:418
Network Interface structure.
Definition net_if.h:680
Network packet.
Definition net_pkt.h:69
Generic sockaddr struct.
Definition net_ip.h:388