1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//! **pktbaffle** — compile libpcap-style packet filter expressions into
//! classic BPF (cBPF) or extended BPF (eBPF) programs.
//!
//! # Overview
//!
//! `pktbaffle` turns the same filter syntax used by `tcpdump` and
//! `pcap_compile(3)` into compact bytecode with no C runtime dependency.
//! The output can be attached to a raw socket with `SO_ATTACH_FILTER`
//! (classic BPF) or loaded into an XDP / TC hook (extended BPF).
//!
//! # Quick start
//!
//! ```rust
//! use pktbaffle::{compile, LinkType, Target};
//!
//! // Classic BPF — attach to a raw socket with SO_ATTACH_FILTER
//! let prog = compile("tcp port 443", LinkType::Ethernet, Target::Classic).unwrap();
//! assert!(prog.len() > 0);
//! let bytes = prog.to_le_bytes(); // 8 bytes per instruction, little-endian
//!
//! // eBPF — load into an XDP or TC program
//! let prog = compile("tcp port 443", LinkType::Ethernet, Target::Extended).unwrap();
//! let bytes = prog.to_le_bytes();
//! ```
//!
//! # Filter syntax
//!
//! The filter language is a subset of the libpcap expression syntax.
//! Primitives can be combined with `and`, `or`, `not` (or `!`);
//! juxtaposition is treated as AND.
//!
//! | Expression | Matches |
//! |---|---|
//! | `host 192.168.1.1` | IPv4 src or dst |
//! | `src host 10.0.0.1` | IPv4 source only |
//! | `net 10.0.0.0/8` | Any address in 10.0.0.0/8 |
//! | `tcp port 443` | TCP to/from port 443 |
//! | `udp portrange 1024-65535` | UDP ephemeral ports |
//! | `port 80 or port 443` | HTTP or HTTPS |
//! | `tcp and not port 22` | TCP excluding SSH |
//! | `ether host aa:bb:cc:dd:ee:ff` | Ethernet MAC address |
//! | `vlan 100` | VLAN-tagged, ID 100 |
//! | `mpls` | Any MPLS-labeled packet |
//! | `ip multicast` | IPv4 multicast destination |
//! | `ip6 and tcp port 80` | IPv6 HTTP traffic |
//! | `len <= 64` | Packets ≤ 64 bytes |
//! | `tcp[13] & 0x02 != 0` | TCP SYN flag (raw byte access) |
//!
//! # Compilation pipeline
//!
//! Calling [`compile`] runs the following stages in sequence:
//!
//! 1. **Lex** ([`lexer::lex`]) — tokenise the input string into a
//! [`Vec<lexer::Spanned>`][lexer::Spanned].
//! 2. **Parse** ([`parser::parse`]) — build an [`ast::Expr`] tree.
//! 3. **Codegen** ([`codegen::compile`] or [`ebpf_codegen::compile`]) — emit
//! BPF instructions with a two-pass jump-patch strategy.
//!
//! You can stop after any stage if you only need the intermediate
//! representation. [`parse`] is a convenience wrapper for steps 1–2.
//!
//! # Link types
//!
//! [`LinkType`] tells the compiler how to interpret packet offsets:
//!
//! | Variant | Header | Typical use |
//! |---|---|---|
//! | [`LinkType::Ethernet`] | 14-byte Ethernet II | `AF_PACKET` / `pcap` |
//! | [`LinkType::RawIp`] | None — packet starts at IP | `SOCK_RAW` + `IPPROTO_*` |
//! | [`LinkType::LinuxSll`] | 16-byte Linux SLL | `any` interface in `pcap` |
//!
//! # Feature flags
//!
//! | Feature | Description |
//! |---|---|
//! | `vm` | Enable the software cBPF interpreter (`bpf::Program::matches`) |
pub use LinkType;
pub use ;
// Re-export instruction types for downstream code that inspects programs.
pub use Insn;
/// Compilation target: classic BPF or extended BPF.
/// A compiled packet filter program, either cBPF or eBPF.
/// Parse and compile a filter expression into a [`Program`].
///
/// # Errors
///
/// Returns [`Error::LexError`] for unrecognised characters,
/// [`Error::ParseError`] for grammatically invalid expressions, and
/// [`Error::CodegenError`] for constructs that cannot be represented in
/// the chosen target for the chosen link type.
/// Parse a filter expression string into an [`ast::Expr`] without generating code.
///
/// Useful for inspecting or transforming the filter tree before compilation,
/// or for validating syntax without committing to a [`Target`] or [`LinkType`].
///
/// # Errors
///
/// Returns [`Error::LexError`] or [`Error::ParseError`] on invalid input.
///
/// # Example
///
/// ```rust
/// use pktbaffle::parse;
///
/// let expr = parse("host 192.168.1.1 and tcp port 22").unwrap();
/// // Inspect the AST:
/// println!("{expr:#?}");
/// ```