netring 0.29.0

High-performance zero-copy packet I/O for Linux (AF_PACKET TPACKET_V3 + AF_XDP)
Documentation
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Runtime filter-expression parser (0.25 Phase A4).
//!
//! A small, dependency-free recursive-descent parser from a Wireshark-ish
//! filter string to the **same** [`Predicate`] AST the typed builders produce.
//! So `packet().expr("tcp and dst port 443")` and
//! `packet().tcp().dst_port(443)` are identical — one AST, two frontends — and
//! both lower to the same userspace eval + kernel pushdown. This is the path
//! for filters that come from *outside* the binary (config, CLI, a control
//! plane), where a compile-time typed builder can't reach.
//!
//! We deliberately do **not** depend on `wirefilter-engine` (its crates.io
//! release is stale at 0.6.1/2019); the grammar here is small enough to own.
//!
//! ## Grammar
//!
//! ```text
//! expr    := or
//! or      := and ( ("or" | "||") and )*
//! and     := not ( ("and" | "&&") not )*
//! not     := ("not" | "!") not | primary
//! primary := "(" expr ")" | atom
//! atom    := "tcp" | "udp" | "icmp"
//!          | dir? "port" INT | dir? "host" IP | dir? "net" CIDR
//!          | "vlan" INT
//!          | "arp" | "ethertype" (HEX | INT)
//!          | "bytes" ">" INT | "packets" ">" INT
//!          | "tls.sni" "~" GLOB | "http.host" "~" GLOB | "dns.qname" "~" GLOB
//! dir     := "src" | "dst"
//! ```
//!
//! Tier-inappropriate atoms parse fine but simply never match at eval time
//! (e.g. `tls.sni` on the packet tier — the field is `None` there), mirroring
//! the userspace-`None`-is-false rule of [`Predicate::eval`].

use std::fmt;
use std::net::IpAddr;

use flowscope::L4Proto;

use super::predicate::{Atom, Glob, Predicate};
use crate::config::ipnet::IpNet;

/// A filter-expression parse error, with a short human-readable reason.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
    /// What went wrong (e.g. `unexpected token "foo"`, `expected a port number`).
    pub message: String,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "filter expression parse error: {}", self.message)
    }
}

impl std::error::Error for ParseError {}

impl ParseError {
    fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
        }
    }
}

/// Parse a filter expression into a [`Predicate`].
pub fn parse(input: &str) -> Result<Predicate, ParseError> {
    let tokens = tokenize(input);
    if tokens.is_empty() {
        // An empty filter matches everything (the unfiltered subscription).
        return Ok(Predicate::Always);
    }
    let mut p = Parser { tokens, pos: 0 };
    let pred = p.parse_or()?;
    if p.pos != p.tokens.len() {
        return Err(ParseError::new(format!(
            "unexpected trailing token {:?}",
            p.tokens[p.pos]
        )));
    }
    Ok(pred)
}

/// Split into whitespace-delimited tokens, with `(` `)` `!` `~` `>` always
/// standing alone (so `tls.sni~*.bank` and `(tcp)` tokenize correctly).
fn tokenize(input: &str) -> Vec<String> {
    let mut spaced = String::with_capacity(input.len() * 2);
    for ch in input.chars() {
        if matches!(ch, '(' | ')' | '!' | '~' | '>') {
            spaced.push(' ');
            spaced.push(ch);
            spaced.push(' ');
        } else {
            spaced.push(ch);
        }
    }
    spaced.split_whitespace().map(|s| s.to_string()).collect()
}

struct Parser {
    tokens: Vec<String>,
    pos: usize,
}

impl Parser {
    fn peek(&self) -> Option<&str> {
        self.tokens.get(self.pos).map(|s| s.as_str())
    }

    fn advance(&mut self) -> Option<String> {
        let t = self.tokens.get(self.pos).cloned();
        if t.is_some() {
            self.pos += 1;
        }
        t
    }

    /// `lc` of the current token, for case-insensitive keyword matching.
    fn peek_lc(&self) -> Option<String> {
        self.peek().map(|s| s.to_ascii_lowercase())
    }

    fn parse_or(&mut self) -> Result<Predicate, ParseError> {
        let mut lhs = self.parse_and()?;
        while matches!(self.peek_lc().as_deref(), Some("or") | Some("||")) {
            self.advance();
            let rhs = self.parse_and()?;
            lhs = lhs.or(rhs);
        }
        Ok(lhs)
    }

    fn parse_and(&mut self) -> Result<Predicate, ParseError> {
        let mut lhs = self.parse_not()?;
        while matches!(self.peek_lc().as_deref(), Some("and") | Some("&&")) {
            self.advance();
            let rhs = self.parse_not()?;
            lhs = lhs.and(rhs);
        }
        Ok(lhs)
    }

    fn parse_not(&mut self) -> Result<Predicate, ParseError> {
        if matches!(self.peek_lc().as_deref(), Some("not") | Some("!")) {
            self.advance();
            return Ok(self.parse_not()?.negate());
        }
        self.parse_primary()
    }

    fn parse_primary(&mut self) -> Result<Predicate, ParseError> {
        if self.peek() == Some("(") {
            self.advance();
            let inner = self.parse_or()?;
            match self.advance().as_deref() {
                Some(")") => Ok(inner),
                _ => Err(ParseError::new("expected closing `)`")),
            }
        } else {
            self.parse_atom()
        }
    }

    fn parse_atom(&mut self) -> Result<Predicate, ParseError> {
        let tok = self
            .advance()
            .ok_or_else(|| ParseError::new("unexpected end of expression"))?;
        let lc = tok.to_ascii_lowercase();
        let atom = match lc.as_str() {
            "tcp" => Atom::Proto(L4Proto::Tcp),
            "udp" => Atom::Proto(L4Proto::Udp),
            "icmp" => Atom::Proto(L4Proto::Icmp),
            "arp" => Atom::EtherType(0x0806),
            "ethertype" => Atom::EtherType(self.expect_ethertype()?),
            "vlan" => Atom::VlanId(self.expect_u16("a VLAN id")?),
            "port" => Atom::AnyPort(self.expect_u16("a port number")?),
            "host" => Atom::AnyHost(self.expect_ip()?),
            "net" => Atom::AnyNet(self.expect_net()?),
            "src" | "dst" => return self.parse_directional(&lc),
            "bytes" => {
                self.expect_gt()?;
                Atom::BytesOver(self.expect_u64("a byte count")?)
            }
            "packets" => {
                self.expect_gt()?;
                Atom::PacketsOver(self.expect_u64("a packet count")?)
            }
            "tls.sni" => Atom::SniGlob(self.expect_glob()?),
            "http.host" => Atom::HttpHostGlob(self.expect_glob()?),
            "dns.qname" => Atom::DnsQnameGlob(self.expect_glob()?),
            other => return Err(ParseError::new(format!("unexpected token {other:?}"))),
        };
        Ok(Predicate::Atom(atom))
    }

    /// `src`/`dst` followed by `port` / `host` / `net`.
    fn parse_directional(&mut self, dir: &str) -> Result<Predicate, ParseError> {
        let kind = self
            .advance()
            .ok_or_else(|| ParseError::new("expected `port`, `host`, or `net` after src/dst"))?
            .to_ascii_lowercase();
        let src = dir == "src";
        let atom = match kind.as_str() {
            "port" => {
                let p = self.expect_u16("a port number")?;
                if src {
                    Atom::SrcPort(p)
                } else {
                    Atom::DstPort(p)
                }
            }
            "host" => {
                let ip = self.expect_ip()?;
                if src {
                    Atom::SrcHost(ip)
                } else {
                    Atom::DstHost(ip)
                }
            }
            "net" => {
                let n = self.expect_net()?;
                if src {
                    Atom::SrcNet(n)
                } else {
                    Atom::DstNet(n)
                }
            }
            other => {
                return Err(ParseError::new(format!(
                    "expected `port`/`host`/`net` after {dir}, got {other:?}"
                )));
            }
        };
        Ok(Predicate::Atom(atom))
    }

    fn expect_u16(&mut self, what: &str) -> Result<u16, ParseError> {
        let t = self
            .advance()
            .ok_or_else(|| ParseError::new(format!("expected {what}")))?;
        t.parse::<u16>()
            .map_err(|_| ParseError::new(format!("expected {what}, got {t:?}")))
    }

    /// An EtherType, accepting both hex (`0x0806`) and decimal (`2054`).
    fn expect_ethertype(&mut self) -> Result<u16, ParseError> {
        let t = self
            .advance()
            .ok_or_else(|| ParseError::new("expected an EtherType (e.g. 0x0806)"))?;
        let parsed = t
            .strip_prefix("0x")
            .or_else(|| t.strip_prefix("0X"))
            .map(|hex| u16::from_str_radix(hex, 16))
            .unwrap_or_else(|| t.parse::<u16>());
        parsed.map_err(|_| ParseError::new(format!("expected an EtherType, got {t:?}")))
    }

    fn expect_u64(&mut self, what: &str) -> Result<u64, ParseError> {
        let t = self
            .advance()
            .ok_or_else(|| ParseError::new(format!("expected {what}")))?;
        t.parse::<u64>()
            .map_err(|_| ParseError::new(format!("expected {what}, got {t:?}")))
    }

    fn expect_ip(&mut self) -> Result<IpAddr, ParseError> {
        let t = self
            .advance()
            .ok_or_else(|| ParseError::new("expected an IP address"))?;
        t.parse::<IpAddr>()
            .map_err(|_| ParseError::new(format!("expected an IP address, got {t:?}")))
    }

    fn expect_net(&mut self) -> Result<IpNet, ParseError> {
        let t = self
            .advance()
            .ok_or_else(|| ParseError::new("expected a CIDR network"))?;
        t.parse::<IpNet>()
            .map_err(|_| ParseError::new(format!("expected a CIDR network, got {t:?}")))
    }

    fn expect_glob(&mut self) -> Result<Glob, ParseError> {
        match self.advance().as_deref() {
            Some("~") => {}
            _ => return Err(ParseError::new("expected `~` before a glob pattern")),
        }
        let t = self
            .advance()
            .ok_or_else(|| ParseError::new("expected a glob pattern after `~`"))?;
        Ok(Glob::new(t))
    }

    fn expect_gt(&mut self) -> Result<(), ParseError> {
        match self.advance().as_deref() {
            Some(">") => Ok(()),
            _ => Err(ParseError::new("expected `>` for a count comparison")),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::monitor::subscription::builder::packet;

    fn p(s: &str) -> Predicate {
        parse(s).unwrap_or_else(|e| panic!("parse {s:?}: {e}"))
    }

    #[test]
    fn empty_is_always() {
        assert_eq!(parse("").unwrap(), Predicate::Always);
        assert_eq!(parse("   ").unwrap(), Predicate::Always);
    }

    #[test]
    fn protocol_and_port_atoms() {
        assert_eq!(p("tcp"), Predicate::Atom(Atom::Proto(L4Proto::Tcp)));
        assert_eq!(p("dst port 443"), Predicate::Atom(Atom::DstPort(443)));
        assert_eq!(p("src port 53"), Predicate::Atom(Atom::SrcPort(53)));
        assert_eq!(p("port 80"), Predicate::Atom(Atom::AnyPort(80)));
        assert_eq!(p("vlan 100"), Predicate::Atom(Atom::VlanId(100)));
    }

    #[test]
    fn ethertype_and_arp_atoms() {
        // Issue #20: `arp` sugar + `ethertype` with hex or decimal.
        assert_eq!(p("arp"), Predicate::Atom(Atom::EtherType(0x0806)));
        assert_eq!(
            p("ethertype 0x0806"),
            Predicate::Atom(Atom::EtherType(0x0806))
        );
        assert_eq!(
            p("ethertype 2048"),
            Predicate::Atom(Atom::EtherType(0x0800))
        );
        // Composes with the rest of the grammar.
        assert_eq!(
            p("arp or tcp"),
            Predicate::Atom(Atom::EtherType(0x0806)).or(Predicate::Atom(Atom::Proto(L4Proto::Tcp)))
        );
        assert!(parse("ethertype nothex").is_err());
    }

    #[test]
    fn host_net_count_l7_atoms() {
        assert_eq!(
            p("host 8.8.8.8"),
            Predicate::Atom(Atom::AnyHost("8.8.8.8".parse().unwrap()))
        );
        assert_eq!(
            p("src net 10.0.0.0/8"),
            Predicate::Atom(Atom::SrcNet("10.0.0.0/8".parse().unwrap()))
        );
        assert_eq!(
            p("bytes > 1048576"),
            Predicate::Atom(Atom::BytesOver(1048576))
        );
        assert_eq!(p("packets > 10"), Predicate::Atom(Atom::PacketsOver(10)));
        assert_eq!(
            p("tls.sni ~ *.bank"),
            Predicate::Atom(Atom::SniGlob(Glob::new("*.bank")))
        );
        assert_eq!(
            p("dns.qname ~ *.evil.test"),
            Predicate::Atom(Atom::DnsQnameGlob(Glob::new("*.evil.test")))
        );
    }

    #[test]
    fn precedence_and_binds_tighter_than_or() {
        // tcp and port 443 or udp  ==  (tcp AND 443) OR udp
        let got = p("tcp and dst port 443 or udp");
        let expect = Predicate::Atom(Atom::Proto(L4Proto::Tcp))
            .and(Predicate::Atom(Atom::DstPort(443)))
            .or(Predicate::Atom(Atom::Proto(L4Proto::Udp)));
        assert_eq!(got, expect);
    }

    #[test]
    fn parens_override_precedence() {
        // tcp and ( port 80 or port 443 )
        let got = p("tcp and ( dst port 80 or dst port 443 )");
        let expect = Predicate::Atom(Atom::Proto(L4Proto::Tcp))
            .and(Predicate::Atom(Atom::DstPort(80)).or(Predicate::Atom(Atom::DstPort(443))));
        assert_eq!(got, expect);
    }

    #[test]
    fn negation_and_symbols() {
        assert_eq!(
            p("not tcp"),
            Predicate::Atom(Atom::Proto(L4Proto::Tcp)).negate()
        );
        // `!` and `&&`/`||` aliases.
        let a = p("udp && ! dst port 53");
        let b = Predicate::Atom(Atom::Proto(L4Proto::Udp))
            .and(Predicate::Atom(Atom::DstPort(53)).negate());
        assert_eq!(a, b);
    }

    #[test]
    fn case_insensitive_keywords() {
        assert_eq!(p("TCP AND DST PORT 443"), p("tcp and dst port 443"));
    }

    #[test]
    fn expr_equals_typed_builder() {
        // The headline property: the string and typed frontends produce the
        // identical AST.
        let from_expr = p("tcp and dst port 443");
        let from_builder = packet().tcp().dst_port(443).into_predicate();
        assert_eq!(from_expr, from_builder);
    }

    #[test]
    fn errors_are_reported_not_panicked() {
        assert!(parse("tcp and").is_err()); // dangling operator → empty rhs atom
        assert!(parse("port").is_err()); // missing number
        assert!(parse("port abc").is_err()); // bad number
        assert!(parse("host nope").is_err()); // bad ip
        assert!(parse("( tcp").is_err()); // unbalanced paren
        assert!(parse("frobnicate").is_err()); // unknown token
        assert!(parse("tcp udp").is_err()); // trailing token (no operator)
    }
}