ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
promisc.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#ifndef PROTOCORE_PROMISC_H
5#define PROTOCORE_PROMISC_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
9EMBED_BEGIN_DECLS
10
11/**
12 * @file promisc.h
13 * @brief Wi-Fi promiscuous (monitor) capture (PROTOCORE_ENABLE_PROMISC) - passive 802.11 sniffing.
14 *
15 * A read-only capture path: instead of joining a network and terminating traffic, listen to
16 * every 802.11 frame on a channel and hand it to a sink. The canonical wiring feeds the sink
17 * into the forwarding plane (network_drivers/network/forward), so captured Wi-Fi frames are bridged to another
18 * interface (e.g. Ethernet) for a wired collector - "capture on Wi-Fi, forward to Ethernet".
19 *
20 * Two host-testable pieces plus the ESP32 radio binding:
21 * - wifi_frame_parse(): decode the 802.11 MAC header (type/subtype, the to/from-DS address
22 * layout -> src / dst / bssid, sequence number, header length). Pure.
23 * - pcap_* : build the classic libpcap global + per-record headers (DLT_IEEE802_11) so a
24 * forwarded frame is a valid PCAP stream a wired Wireshark / tcpdump can read. Pure.
25 * - protocore_promisc_begin() / _set_channel() / _end(): monitor-mode bring-up whose rx
26 * callback copies each frame (with RSSI + channel) to the registered sink. ESP32 only.
27 *
28 * Capture is strictly passive (no injection) and fail-closed: the sink is expected to drop, not
29 * block, when its downstream is full, so the live data path is never stalled.
30 *
31 * @c work is PROTOCORE_PROMISC_BORROW bytes the CALLER took, at an address it knows. It is not held past the call, so
32 * nothing here aliases it. How those bytes are carved is this module's and is never named here.
33 *
34 * @author Douglas Quigg (dstroy0)
35 * @date 2026
36 */
37
38/** @brief 802.11 frame type (frame-control bits 2-3). */
39EMBED_ENUM_BYTE(WifiFrameType, WIFI_FT_MGMT = 0, WIFI_FT_CTRL = 1, WIFI_FT_DATA = 2, WIFI_FT_EXT = 3, );
40
41/** @brief Decoded 802.11 MAC header. src / dst / bssid point into the frame (6 bytes) or null. */
42typedef struct
43{
44 WifiFrameType type; ///< WifiFrameType
45 uint8_t subtype; ///< 0..15
46 embed_bool to_ds;
47 embed_bool from_ds;
48 embed_bool protected_frame; ///< the Protected-Frame (WEP/CCMP) bit
49 embed_bool is_qos; ///< QoS data subtype (adds 2 header bytes)
50 uint16_t seq; ///< 12-bit sequence number
51 uint16_t hdr_len; ///< MAC header length (bytes)
52 const uint8_t *dst; ///< destination (receiver) MAC, per the to/from-DS layout
53 const uint8_t *src; ///< source (transmitter) MAC
54 const uint8_t *bssid; ///< BSSID (null for a WDS 4-address frame)
56
57/**
58 * @brief Sink for one captured frame: the raw 802.11 bytes plus radio metadata.
59 * @param frame the 802.11 MAC frame (points into the driver buffer; copy if retained).
60 * @param len frame length in bytes.
61 * @param rssi received signal strength (dBm).
62 * @param channel the channel it was captured on.
63 */
64typedef void (*protocore_promisc_sink_fn)(const uint8_t *frame, uint16_t len, int8_t rssi, uint8_t channel);
65
66/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
67typedef struct
68{
69 embed_bool (*wifi_frame_parse)(const uint8_t *, const uint8_t *, uint16_t, WifiFrameInfo *);
70 embed_bool (*begin)(uint8_t *, uint8_t, protocore_promisc_sink_fn);
71 void (*set_channel)(const uint8_t *, uint8_t);
72 void (*end)(uint8_t *);
73} PromiscNs;
74PROTOCORE_NS_LAYOUT(PromiscNs, wifi_frame_parse, begin, set_channel, end);
75
76/**
77 * @brief Parse an 802.11 MAC header (IEEE 802.11 §9.2 / §9.3.2, the .
78 * @param work PROTOCORE_PROMISC_BORROW bytes the caller took. Not held past the call.
79 * @param frame Frame
80 * @param len Len
81 * @param out Out
82 * @return EMBED_TRUE on success.
83 */
84embed_bool protocore_promisc_wifi_frame_parse(const uint8_t *work, const uint8_t *frame, uint16_t len,
85 WifiFrameInfo *out);
86/**
87 * @brief Start promiscuous capture on channel; every frame is delivered to .
88 * @param work PROTOCORE_PROMISC_BORROW bytes the caller took. Not held past the call.
89 * @param channel Channel
90 * @param sink Sink
91 * @return EMBED_TRUE on success.
92 */
93embed_bool protocore_promisc_begin(uint8_t *work, uint8_t channel, protocore_promisc_sink_fn sink);
94/**
95 * @brief Retune the capture to a different channel (1..14).
96 * @param work PROTOCORE_PROMISC_BORROW bytes the caller took. Not held past the call.
97 * @param channel Channel
98 */
99void protocore_promisc_set_channel(const uint8_t *work, uint8_t channel);
100/**
101 * @brief Stop promiscuous capture.
102 * @param work PROTOCORE_PROMISC_BORROW bytes the caller took. Not held past the call.
103 */
104void protocore_promisc_end(uint8_t *work);
105
106/**
107 * @brief Sink for one captured frame: the raw 802.11 bytes plus radio metadata.
108 * @param frame the 802.11 MAC frame (points into the driver buffer; copy if retained).
109 * @param len frame length in bytes.
110 * @param rssi received signal strength (dBm).
111 * @param channel the channel it was captured on.
112 */
113typedef void (*protocore_promisc_sink_fn)(const uint8_t *frame, uint16_t len, int8_t rssi, uint8_t channel);
114/**
115 * @brief The PROTOCORE_PROMISC_BORROW bytes this module's state lives in.
116 *
117 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
118 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
119 * walks, so the state lasts the life of the program.
120 *
121 * @return the span.
122 */
124
125/** @brief Module namespace. */
130
131EMBED_END_DECLS
132
133#endif // PROTOCORE_PROMISC_H
#define PROTOCORE_UNUSED
Suppress the unused warning. Every module ends in a namespace most callers only partly use.
Definition ns_contract.h:36
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
embed_bool protocore_promisc_begin(uint8_t *work, uint8_t channel, protocore_promisc_sink_fn sink)
Start promiscuous capture on channel; every frame is delivered to .
EMBED_ENUM_BYTE(WifiFrameType, WIFI_FT_MGMT=0, WIFI_FT_CTRL=1, WIFI_FT_DATA=2, WIFI_FT_EXT=3,)
802.11 frame type (frame-control bits 2-3).
void protocore_promisc_end(uint8_t *work)
Stop promiscuous capture.
uint8_t * protocore_promisc_span(void)
The PROTOCORE_PROMISC_BORROW bytes this module's state lives in.
void protocore_promisc_set_channel(const uint8_t *work, uint8_t channel)
Retune the capture to a different channel (1..14).
embed_bool protocore_promisc_wifi_frame_parse(const uint8_t *work, const uint8_t *frame, uint16_t len, WifiFrameInfo *out)
Parse an 802.11 MAC header (IEEE 802.11 §9.2 / §9.3.2, the .
void(* protocore_promisc_sink_fn)(const uint8_t *frame, uint16_t len, int8_t rssi, uint8_t channel)
Sink for one captured frame: the raw 802.11 bytes plus radio metadata.
Definition promisc.h:64
Dispatch table. Addressed by offset, so the layout is asserted below.
Definition promisc.h:68
void(* set_channel)(const uint8_t *, uint8_t)
Definition promisc.h:71
embed_bool(* begin)(uint8_t *, uint8_t, protocore_promisc_sink_fn)
Definition promisc.h:70
embed_bool(* wifi_frame_parse)(const uint8_t *, const uint8_t *, uint16_t, WifiFrameInfo *)
Definition promisc.h:69
void(* end)(uint8_t *)
Definition promisc.h:72
Decoded 802.11 MAC header. src / dst / bssid point into the frame (6 bytes) or null.
Definition promisc.h:43
const uint8_t * src
source (transmitter) MAC
Definition promisc.h:53
embed_bool is_qos
QoS data subtype (adds 2 header bytes).
Definition promisc.h:49
uint16_t hdr_len
MAC header length (bytes).
Definition promisc.h:51
embed_bool to_ds
Definition promisc.h:46
embed_bool protected_frame
the Protected-Frame (WEP/CCMP) bit
Definition promisc.h:48
uint16_t seq
12-bit sequence number
Definition promisc.h:50
const uint8_t * dst
destination (receiver) MAC, per the to/from-DS layout
Definition promisc.h:52
embed_bool from_ds
Definition promisc.h:47
const uint8_t * bssid
BSSID (null for a WDS 4-address frame).
Definition promisc.h:54
WifiFrameType type
WifiFrameType.
Definition promisc.h:44
uint8_t subtype
0..15
Definition promisc.h:45