bgpkit-parser 0.25.0-beta.1

MRT/BGP/BMP data processing library
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//! Byte-level pre-checks for filters that only need attribute bytes.
//!
//! An origin filter can only match an element whose AS path ends at the requested origin. A dump
//! record in which no entry can satisfy that yields no matching elements, so it can be skipped before
//! its body is parsed. Deciding this costs one walk over the TLV headers plus a comparison on the
//! values the predicate names, against a full attribute decode and an element construction per entry.
//!
//! Only shapes with a structural meaning are decided here: a path and an ORIGIN attribute have one,
//! while a community or AS path regular expression depends on how those values are written out, so
//! those filters are left to the general path.
//!
//! The one rule is that a reject must be certain: whenever this module cannot decide an entry it
//! reports that, and the caller parses the record the general way.

use crate::models::{Asn, Origin};
use crate::parser::mrt::frame::FLAG_ADD_PATH;
use crate::Filter;
use smallvec::SmallVec;

/// The predicates one record-level pass can decide.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum AttrPredicate {
    /// `origin_asn=N`
    Asn(u32),
    /// `origin_asns=N,M`
    Asns(Vec<u32>),
    /// `origin=igp|egp|incomplete`
    Value(Origin),
}

/// What one entry's attribute block says, as far as the predicates above need to know.
#[derive(Debug, Default)]
struct EntryFacts<'a> {
    origin_at: Option<u8>,
    path: Option<&'a [u8]>,
    /// `AS4_PATH` can replace the origin.
    has_as4_path: bool,
    truncated: bool,
}

fn read_u16(bytes: &[u8]) -> u16 {
    u16::from_be_bytes([bytes[0], bytes[1]])
}

/// Walk one attribute block once and record where the attributes this module cares about sit.
fn read_entry(attrs: &[u8]) -> EntryFacts<'_> {
    let mut facts = EntryFacts::default();
    let mut rest = attrs;
    while !rest.is_empty() {
        if rest.len() < 3 {
            facts.truncated = true;
            return facts;
        }
        let extended = rest[0] & 0x10 != 0;
        let attr_type = rest[1];
        let (header_len, body_len) = if extended {
            if rest.len() < 4 {
                facts.truncated = true;
                return facts;
            }
            (4, read_u16(&rest[2..]) as usize)
        } else {
            (3, rest[2] as usize)
        };
        if rest.len() < header_len + body_len {
            facts.truncated = true;
            return facts;
        }
        let body = &rest[header_len..header_len + body_len];
        match attr_type {
            // The owned path keeps the earlier value when a later attribute of the same type cannot
            // be parsed: the malformed one becomes raw bytes in `unknown`.
            1 => {
                if let Some(at) = body.first().filter(|at| **at <= 2) {
                    facts.origin_at = Some(*at);
                }
            }
            2 => {
                if path_validates(body) {
                    facts.path = Some(body);
                }
            }
            17 => facts.has_as4_path = true,
            _ => {}
        }
        rest = &rest[header_len + body_len..];
    }
    facts
}

/// Whether a four-byte `AS_PATH` is well formed, by the rule the owned decoder applies.
///
/// The owned path rejects the whole attribute when any segment is invalid, so a malformed duplicate
/// must not replace a path this module already read.
fn path_validates(body: &[u8]) -> bool {
    crate::parser::bgp::attributes::validate_as_path_slice(body, &crate::models::AsnLength::Bits32)
}

/// The origin of a 4-byte `AS_PATH`: `Ok(None)` for a path that has none.
///
/// Mirrors `AsPath::iter_origins`: only the last segment decides, a sequence contributes its last ASN,
/// a set contributes every member, and a confederation segment contributes nothing.
fn path_origin(mut body: &[u8]) -> Result<Option<Asn>, ()> {
    let mut last: Option<(u8, &[u8])> = None;
    while !body.is_empty() {
        if body.len() < 2 {
            return Err(());
        }
        let segment_type = body[0];
        let count = body[1] as usize;
        let len = count * 4;
        if !(1..=4).contains(&segment_type) || body.len() < 2 + len {
            return Err(());
        }
        last = Some((segment_type, &body[2..2 + len]));
        body = &body[2 + len..];
    }
    let Some((segment_type, members)) = last else {
        return Ok(None);
    };
    match segment_type {
        // AS_SEQUENCE: the origin is the last ASN.
        2 => Ok(members
            .as_chunks::<4>()
            .0
            .last()
            .map(|chunk| u32::from_be_bytes(*chunk).into())),
        // AS_SET: a single member is decisive, several are not worth deciding here.
        1 => {
            if members.len() == 4 {
                Ok(Some(
                    u32::from_be_bytes([members[0], members[1], members[2], members[3]]).into(),
                ))
            } else {
                Err(())
            }
        }
        // Confederations never contribute an origin.
        3 | 4 => Ok(None),
        _ => Err(()),
    }
}

/// A per-entry screen derived from a filter set, applied before an element is built.
///
/// It is deliberately pessimistic: an entry is dropped only when one conjunct is certain to fail for
/// its decoded attributes, and every other entry goes on to be built and filtered as before.
pub(crate) struct EntryScreen {
    predicates: SmallVec<[AttrPredicate; 4]>,
}

impl EntryScreen {
    pub(crate) fn new(filters: &[Filter]) -> Option<Self> {
        let predicates: SmallVec<[AttrPredicate; 4]> =
            filters.iter().filter_map(predicate_for).collect();
        (!predicates.is_empty()).then_some(EntryScreen { predicates })
    }

    /// The same answer from the entry's raw attribute block, for the borrowed path.
    pub(crate) fn admits_bytes(&self, attrs: &[u8]) -> bool {
        let facts = read_entry(attrs);
        if facts.truncated {
            return true;
        }
        self.predicates
            .iter()
            .all(|predicate| entry_satisfies(&facts, predicate) != Some(false))
    }
}

/// Read a filter into a predicate this module can decide, when it recognises the shape.
pub(crate) fn predicate_for(filter: &Filter) -> Option<AttrPredicate> {
    match filter {
        Filter::OriginAsn(asn) => Some(AttrPredicate::Asn(*asn)),
        Filter::OriginAsns(asns) => Some(AttrPredicate::Asns(asns.clone())),
        Filter::Origin(origin) => Some(AttrPredicate::Value(*origin)),
        _ => None,
    }
}

/// Whether one entry satisfies one predicate: `Some(true)`, `Some(false)`, or `None` for undecidable.
fn entry_satisfies(facts: &EntryFacts<'_>, predicate: &AttrPredicate) -> Option<bool> {
    if facts.truncated {
        return None;
    }
    match predicate {
        AttrPredicate::Asn(asn) => {
            if facts.has_as4_path {
                return None;
            }
            Some(path_origin(facts.path?).ok()? == Some((*asn).into()))
        }
        AttrPredicate::Asns(asns) => {
            if facts.has_as4_path {
                return None;
            }
            let origin = path_origin(facts.path?).ok()?;
            Some(origin.is_some_and(|origin| asns.iter().any(|asn| Asn::from(*asn) == origin)))
        }
        AttrPredicate::Value(origin) => Some(facts.origin_at == Some(*origin as u8)),
    }
}

/// Where a `BGP4MP` UPDATE record keeps its path attributes, or `None` when this module should not
/// guess. Only the fixed header and the two length prefixes are walked; nothing is decoded.
fn bgp4mp_attributes(body: &[u8], subtype: u16) -> Option<&[u8]> {
    // A two-byte record carries a two-byte AS_PATH, which this module does not read.
    match crate::parser::mrt::frame::bgp4mp_asn_len(subtype)? {
        crate::models::AsnLength::Bits32 => {}
        crate::models::AsnLength::Bits16 => return None,
    }
    let asn_len = 4;
    let mut rest = body.get(asn_len * 2..)?;
    if rest.len() < 4 {
        return None;
    }
    let ip_len = match u16::from_be_bytes([rest[2], rest[3]]) {
        1 => 4,
        2 => 16,
        _ => return None,
    };
    rest = rest.get(4 + ip_len * 2..)?;
    // The BGP message: 16-byte marker, two-byte length, one-byte type.
    if rest.len() < 19 || rest[18] != 2 {
        return None;
    }
    rest = &rest[19..];
    if rest.len() < 2 {
        return None;
    }
    let withdrawn_len = read_u16(rest) as usize;
    rest = rest.get(2 + withdrawn_len..)?;
    if rest.len() < 2 {
        return None;
    }
    let attr_len = read_u16(rest) as usize;
    rest.get(2..2 + attr_len)
}

/// Whether a `BGP4MP` UPDATE record provably yields no element that any of `filters` would keep.
///
/// Every element such a record yields shares one attribute block, so one decision covers the record.
/// Withdrawals carry no attributes and therefore fail an attribute predicate as well, which is what
/// makes the record-level answer safe.
pub(crate) fn bgp4mp_record_excluded(filters: &[Filter], body: &[u8], subtype: u16) -> bool {
    let predicates: SmallVec<[AttrPredicate; 4]> =
        filters.iter().filter_map(predicate_for).collect();
    if predicates.is_empty() {
        return false;
    }
    // A record whose AS numbers are two bytes wide also carries a two-byte AS_PATH; this module reads
    // the four-byte form, so an origin predicate is left to the general path there.
    if !matches!(
        crate::parser::mrt::frame::bgp4mp_asn_len(subtype),
        Some(crate::models::AsnLength::Bits32)
    ) {
        return false;
    }
    let Some(attrs) = bgp4mp_attributes(body, subtype) else {
        return false;
    };
    let facts = read_entry(attrs);
    if facts.truncated {
        return false;
    }
    predicates
        .iter()
        .any(|predicate| entry_satisfies(&facts, predicate) == Some(false))
}

/// Whether a RIB record provably yields no element that any of `filters` would keep.
///
/// `false` is the answer whenever the record cannot be decided, so callers may skip the record on
/// `true` and parse it otherwise.
pub(crate) fn rib_record_excluded(
    filters: &[Filter],
    body: &[u8],
    entries_offset: usize,
    flags: u8,
) -> bool {
    let predicates: SmallVec<[AttrPredicate; 4]> =
        filters.iter().filter_map(predicate_for).collect();
    if predicates.is_empty() {
        return false;
    }
    // 0 = no entry matched yet, 1 = an entry matched, 2 = an entry could not be decided.
    let mut state: SmallVec<[u8; 4]> = SmallVec::from_elem(0, predicates.len());

    let head = if flags & FLAG_ADD_PATH != 0 { 12 } else { 8 };
    let mut rest = body.get(entries_offset..).unwrap_or(&[]);
    while !rest.is_empty() {
        if rest.len() < head {
            return false;
        }
        let attr_len = read_u16(&rest[head - 2..]) as usize;
        if rest.len() < head + attr_len {
            return false;
        }
        let facts = read_entry(&rest[head..head + attr_len]);
        if facts.truncated {
            return false;
        }
        for (index, predicate) in predicates.iter().enumerate() {
            match entry_satisfies(&facts, predicate) {
                Some(true) => state[index] = 1,
                Some(false) => {}
                None if state[index] != 1 => state[index] = 2,
                None => {}
            }
        }
        rest = &rest[head + attr_len..];
    }
    // A record is skipped only when one conjunct is certain to fail for every entry it has.
    state.contains(&0)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A well-formed attribute block with the given TLVs.
    fn attrs(items: &[(u8, &[u8])]) -> Vec<u8> {
        let mut out = Vec::new();
        for (attr_type, body) in items {
            out.push(0x40);
            out.push(*attr_type);
            out.push(body.len() as u8);
            out.extend_from_slice(body);
        }
        out
    }

    fn as_path(segments: &[(u8, &[u32])]) -> Vec<u8> {
        let mut out = Vec::new();
        for (segment_type, asns) in segments {
            out.push(*segment_type);
            out.push(asns.len() as u8);
            for asn in *asns {
                out.extend_from_slice(&asn.to_be_bytes());
            }
        }
        out
    }

    #[test]
    fn sequence_origin_is_the_last_asn() {
        let path = as_path(&[(2, &[64512, 174, 15169])]);
        let block = attrs(&[(1, &[0]), (2, &path)]);
        let facts = read_entry(&block);
        assert_eq!(path_origin(facts.path.unwrap()), Ok(Some(15169u32.into())));
        assert_eq!(facts.origin_at, Some(0));
    }

    #[test]
    fn several_sequences_use_the_last_segment() {
        let path = as_path(&[(2, &[64512, 174]), (2, &[3356, 15169])]);
        assert_eq!(path_origin(&path), Ok(Some(15169u32.into())));
    }

    #[test]
    fn a_single_member_set_is_decided_and_a_multi_member_set_is_not() {
        let single = as_path(&[(2, &[64512]), (1, &[15169])]);
        assert_eq!(path_origin(&single), Ok(Some(15169u32.into())));
        let multi = as_path(&[(2, &[64512]), (1, &[15169, 13335])]);
        assert_eq!(path_origin(&multi), Err(()));
    }

    #[test]
    fn confederation_segments_contribute_no_origin() {
        let path = as_path(&[(2, &[64512]), (3, &[65001])]);
        assert_eq!(path_origin(&path), Ok(None));
    }

    #[test]
    fn empty_and_missing_paths_have_no_origin() {
        assert_eq!(path_origin(&[]), Ok(None));
        let block = attrs(&[(1, &[0])]);
        let facts = read_entry(&block);
        assert_eq!(entry_satisfies(&facts, &AttrPredicate::Asn(15169)), None);
    }

    /// A BGP4MP record whose AS numbers are two bytes wide carries a two-byte AS_PATH. Reading it
    /// with the four-byte layout invents an origin, so an origin predicate must bow out.
    #[test]
    fn two_byte_bgp4mp_records_are_undecidable_for_origin_predicates() {
        use crate::models::Bgp4MpType;

        // Envelope: peer AS 64512, local AS 64513, interface index, AFI 1, two IPv4 addresses.
        let mut body = Vec::new();
        body.extend_from_slice(&64512u16.to_be_bytes());
        body.extend_from_slice(&64513u16.to_be_bytes());
        body.extend_from_slice(&0u16.to_be_bytes());
        body.extend_from_slice(&1u16.to_be_bytes());
        body.extend_from_slice(&[192, 0, 2, 1]);
        body.extend_from_slice(&[192, 0, 2, 2]);
        // A BGP UPDATE: ORIGIN IGP, AS_PATH `3356 13335` in two-byte form, one IPv4 NLRI.
        body.extend_from_slice(&[0xFF; 16]);
        // 19 header octets plus 21 of UPDATE: two length fields, thirteen attribute octets, one NLRI.
        body.extend_from_slice(&40u16.to_be_bytes());
        body.push(2);
        body.extend_from_slice(&0u16.to_be_bytes());
        body.extend_from_slice(&13u16.to_be_bytes());
        body.extend_from_slice(&[0x40, 1, 1, 0]);
        body.extend_from_slice(&[0x40, 2, 6, 2, 2]);
        body.extend_from_slice(&3356u16.to_be_bytes());
        body.extend_from_slice(&13335u16.to_be_bytes());
        body.extend_from_slice(&[24, 203, 0, 113]);

        // Read as four-byte ASNs the path is one ASN, 3356 << 16 | 13335, so a four-byte reading
        // would exclude this record for AS3356.
        assert!(
            !bgp4mp_record_excluded(
                &[Filter::OriginAsn(3356)],
                &body,
                Bgp4MpType::Message as u16
            ),
            "a two-byte record must not be excluded on an origin predicate"
        );
    }

    /// A BGP4MP `MESSAGE_AS4` body: peer 65001, local 65002, IPv4, then one UPDATE carrying
    /// `attributes` and a single /24 NLRI.
    fn bgp4mp_update(attributes: &[u8]) -> Vec<u8> {
        let mut body = Vec::new();
        body.extend_from_slice(&65001u32.to_be_bytes());
        body.extend_from_slice(&65002u32.to_be_bytes());
        body.extend_from_slice(&0u16.to_be_bytes());
        body.extend_from_slice(&1u16.to_be_bytes());
        body.extend_from_slice(&[10, 0, 0, 1]);
        body.extend_from_slice(&[10, 0, 0, 2]);
        // BGP header: marker, length, type = UPDATE.
        body.extend_from_slice(&[0xff; 16]);
        body.extend_from_slice(&(19u16 + 2 + 2 + attributes.len() as u16 + 4).to_be_bytes());
        body.push(2);
        // Withdrawn routes: none.
        body.extend_from_slice(&0u16.to_be_bytes());
        // Path attributes.
        body.extend_from_slice(&(attributes.len() as u16).to_be_bytes());
        body.extend_from_slice(attributes);
        // NLRI: 8.8.8.0/24.
        body.extend_from_slice(&[24, 8, 8, 8]);
        body
    }

    #[test]
    fn a_bgp4mp_update_locates_its_attributes() {
        let block = attrs(&[(1, &[0]), (2, &as_path(&[(2, &[174, 15169])]))]);
        let body = bgp4mp_update(&block);
        // 0x0004 is BGP4MP_MESSAGE_AS4.
        let located = bgp4mp_attributes(&body, 4).expect("attributes located");
        assert_eq!(located, block.as_slice());
        assert!(!bgp4mp_record_excluded(
            &[Filter::OriginAsn(15169)],
            &body,
            4
        ));
        assert!(bgp4mp_record_excluded(&[Filter::OriginAsn(3356)], &body, 4));
        // `ORIGIN` needs no path width, and the record is four-byte ASNs, so it decides too.
        assert!(!bgp4mp_record_excluded(
            &[Filter::Origin(Origin::IGP)],
            &body,
            4
        ));
        assert!(bgp4mp_record_excluded(
            &[Filter::Origin(Origin::INCOMPLETE)],
            &body,
            4
        ));
        // A subtype whose layout this module does not model stays undecided, and so does a body
        // whose header does not line up with the subtype's widths.
        assert!(bgp4mp_attributes(&body, 99).is_none());
        assert!(!bgp4mp_record_excluded(
            &[Filter::OriginAsn(3356)],
            &body,
            99
        ));
        assert!(
            bgp4mp_attributes(&body, 6).is_none(),
            "2-byte header against a 4-byte body"
        );
    }

    /// A valid attribute followed by a malformed duplicate keeps its value on the owned path: the
    /// duplicate becomes raw bytes and the earlier parse stands. The byte scan must agree.
    #[test]
    fn a_malformed_duplicate_does_not_overwrite_a_valid_origin() {
        let block = attrs(&[(1, &[0]), (1, &[99])]);
        let facts = read_entry(&block);
        assert_eq!(
            entry_satisfies(&facts, &AttrPredicate::Value(Origin::IGP)),
            Some(true)
        );
    }

    /// Same shape for the AS path: an invalid segment in a later duplicate must not replace the
    /// path that decides the origin, and `ORIGIN` still decides on its own.
    #[test]
    fn a_malformed_duplicate_does_not_overwrite_a_valid_as_path() {
        let valid = as_path(&[(2, &[13335])]);
        let mut malformed = vec![9u8, 0];
        malformed.extend_from_slice(&[2u8, 1]);
        malformed.extend_from_slice(&64512u32.to_be_bytes());
        let block = attrs(&[(2, &valid), (2, &malformed), (1, &[0])]);
        let facts = read_entry(&block);
        assert_eq!(
            entry_satisfies(&facts, &AttrPredicate::Asn(13335)),
            Some(true)
        );
        assert_eq!(
            entry_satisfies(&facts, &AttrPredicate::Value(Origin::IGP)),
            Some(true)
        );
    }

    #[test]
    fn as4_path_and_truncation_are_undecidable() {
        let path = as_path(&[(2, &[64512, 13335])]);
        let mut block = attrs(&[(1, &[0]), (2, &path), (17, &[0, 0, 0, 0])]);
        // `AS4_PATH` can replace the origin, so the answer is left to the general path.
        block.extend_from_slice(&[0x40, 8, 4, 0, 0, 0, 0]);
        assert_eq!(
            entry_satisfies(&read_entry(&block), &AttrPredicate::Asn(13335)),
            None
        );
        let mut short = attrs(&[(1, &[0])]);
        short.extend_from_slice(&[0x40, 2, 30]);
        assert!(read_entry(&short).truncated);
    }

    /// Nothing may be skipped when the filter set names no shape this module decides. A community or
    /// AS path expression is written out by the renderer, so those filters go down the general path.
    #[test]
    fn filters_this_module_does_not_decide_never_exclude() {
        use crate::ComparableRegex;

        let path = as_path(&[(2, &[64512, 3356])]);
        let block = attrs(&[(1, &[0]), (2, &path)]);
        for filter in [
            Filter::Community(ComparableRegex::new("^3356:").unwrap()),
            Filter::AsPath(ComparableRegex::new("^64512 ").unwrap()),
        ] {
            assert_eq!(predicate_for(&filter), None);
            let body = rib_body(&[203, 0, 113], std::slice::from_ref(&block));
            assert!(!rib_record_excluded(
                std::slice::from_ref(&filter),
                &body,
                4,
                0
            ));
        }
    }

    /// A RIB record is a prefix length followed by the prefix, then entries of
    /// `peer_index, originated_time, attr_len, attributes`.
    fn rib_body(prefix: &[u8], entries: &[Vec<u8>]) -> Vec<u8> {
        let mut body = vec![(prefix.len() * 8) as u8];
        body.extend_from_slice(prefix);
        for (index, entry) in entries.iter().enumerate() {
            // Peer index (two bytes), originated time (four), attribute length (two).
            body.extend_from_slice(&(index as u16).to_be_bytes());
            body.extend_from_slice(&0u32.to_be_bytes());
            body.extend_from_slice(&(entry.len() as u16).to_be_bytes());
            body.extend_from_slice(entry);
        }
        body
    }

    #[test]
    fn a_record_is_excluded_only_when_no_entry_matches() {
        let matching = attrs(&[(1, &[0]), (2, &as_path(&[(2, &[64512, 15169])]))]);
        let other = attrs(&[(1, &[0]), (2, &as_path(&[(2, &[64512, 3356])]))]);
        let filters = [Filter::OriginAsn(15169)];
        let both = rib_body(&[203, 0, 113], &[other.clone(), matching]);
        assert!(!rib_record_excluded(&filters, &both, 4, 0));
        let only_other = rib_body(&[203, 0, 113], &[other]);
        assert!(rib_record_excluded(&filters, &only_other, 4, 0));
    }

    /// An ADD-PATH record carries twelve bytes before the attributes: a four-byte peer index, a
    /// four-byte path identifier, the originated time and the length.
    #[test]
    fn an_add_path_record_is_read_with_the_longer_entry_head() {
        use crate::parser::mrt::frame::FLAG_ADD_PATH;

        let other = attrs(&[(1, &[0]), (2, &as_path(&[(2, &[64512, 3356])]))]);

        let mut body = vec![(3u8) * 8];
        body.extend_from_slice(&[203, 0, 113]);
        for (index, entry) in [&other, &other].iter().enumerate() {
            body.extend_from_slice(&(index as u16).to_be_bytes()); // peer index
            body.extend_from_slice(&0u32.to_be_bytes()); // path identifier
            body.extend_from_slice(&0u32.to_be_bytes()); // originated time
            body.extend_from_slice(&(entry.len() as u16).to_be_bytes());
            body.extend_from_slice(entry);
        }

        let filters = [Filter::OriginAsn(15169)];
        assert!(
            rib_record_excluded(&filters, &body, 4, FLAG_ADD_PATH),
            "neither entry reaches 15169, so the record is skipped"
        );

        // The same bytes read with the short head put the length field in the wrong place, so the
        // walk cannot complete and the record must not be excluded.
        assert!(!rib_record_excluded(&filters, &body, 4, 0));
    }

    #[test]
    fn an_undecidable_entry_prevents_exclusion() {
        // A multi-member AS_SET leaves the origin open.
        let undecided = attrs(&[
            (1, &[0]),
            (2, &as_path(&[(2, &[64512]), (1, &[15169, 13335])])),
        ]);
        let other = attrs(&[(1, &[0]), (2, &as_path(&[(2, &[64512, 3356])]))]);
        let body = rib_body(&[203, 0, 113], &[other, undecided]);
        assert!(!rib_record_excluded(
            &[Filter::OriginAsn(15169)],
            &body,
            4,
            0
        ));
    }

    /// The prefilter decides whole records and whole entries, so the only acceptable outcome is
    /// the same element stream the element-level filters produce. The reference side runs
    /// unfiltered, which takes the framing path that never consults this module.
    #[test]
    fn origin_prefilter_matches_the_element_level_semantics() {
        use crate::parser::BgpkitParser;

        // The entry-level reading of the same shapes first, including the ones a routeviews slice
        // cannot pin down: a single-member AS_SET, an EGP origin, and a filter naming an origin
        // the entry does not carry.
        let igp_15169 = attrs(&[(1, &[0]), (2, &as_path(&[(2, &[64512, 174, 15169])]))]);
        let egp_3356 = attrs(&[(1, &[1]), (2, &as_path(&[(2, &[64512, 3356])]))]);
        let single_member_set = attrs(&[(1, &[0]), (2, &as_path(&[(2, &[64512]), (1, &[15169])]))]);
        // A confederation at the end leaves no origin, which is a fact: no origin filter can match.
        let confederation = attrs(&[(1, &[0]), (2, &as_path(&[(2, &[64512]), (3, &[65001])]))]);
        assert_eq!(
            entry_satisfies(&read_entry(&igp_15169), &AttrPredicate::Asn(15169)),
            Some(true)
        );
        assert_eq!(
            entry_satisfies(&read_entry(&egp_3356), &AttrPredicate::Asn(3356)),
            Some(true)
        );
        assert_eq!(
            entry_satisfies(&read_entry(&egp_3356), &AttrPredicate::Value(Origin::EGP)),
            Some(true)
        );
        assert_eq!(
            entry_satisfies(
                &read_entry(&single_member_set),
                &AttrPredicate::Asns(vec![15169, 13335])
            ),
            Some(true)
        );
        assert_eq!(
            entry_satisfies(&read_entry(&confederation), &AttrPredicate::Asn(65001)),
            Some(false)
        );
        assert_eq!(
            entry_satisfies(
                &read_entry(&igp_15169),
                &AttrPredicate::Value(Origin::INCOMPLETE)
            ),
            Some(false)
        );

        let fixture = "tests/fixtures/routeviews/bview.slice.gz";
        let all: Vec<crate::models::BgpElem> = BgpkitParser::new(fixture)
            .expect("open fixture")
            .into_elem_iter()
            .collect();
        assert!(all.len() > 10_000, "fixture should hold a real slice");

        let expected_for = |predicate: &dyn Fn(&crate::models::BgpElem) -> bool| -> Vec<_> {
            all.iter().filter(|elem| predicate(elem)).cloned().collect()
        };

        for asn in [15169u32, 13335, 3356, 174, 64073, 9999] {
            let expected = expected_for(&|elem| {
                elem.origin_asns
                    .as_ref()
                    .is_some_and(|origins| origins.contains(&asn.into()))
            });
            let actual: Vec<_> = BgpkitParser::new(fixture)
                .expect("open fixture")
                .add_filters(&[Filter::OriginAsn(asn)])
                .into_elem_iter()
                .collect();
            assert_eq!(expected, actual, "origin_asn {asn} diverged");
        }

        for origin in [Origin::IGP, Origin::INCOMPLETE] {
            let expected = expected_for(&|elem| elem.origin == Some(origin));
            let actual: Vec<_> = BgpkitParser::new(fixture)
                .expect("open fixture")
                .add_filters(&[Filter::Origin(origin)])
                .into_elem_iter()
                .collect();
            assert_eq!(expected, actual, "origin {origin} diverged");
        }
    }

    /// Two origin conjuncts over one record: every entry is decided by both, and the per-conjunct
    /// state must be kept apart. Entry one satisfies only `origin_asn=15169`, entry two only
    /// `origin=egp`, so no entry satisfies both; the record still must not be skipped, because
    /// the element-level filter is what decides the conjunction.
    #[test]
    fn conjuncts_are_decided_independently() {
        let only_asn = attrs(&[(1, &[0]), (2, &as_path(&[(2, &[64512, 15169])]))]);
        let only_origin = attrs(&[(1, &[1]), (2, &as_path(&[(2, &[64512, 3356])]))]);
        let both = attrs(&[(1, &[1]), (2, &as_path(&[(2, &[3356, 15169])]))]);
        let filters = [Filter::OriginAsn(15169), Filter::Origin(Origin::EGP)];

        // Each conjunct is satisfied by some entry, so neither is certain to fail for the record.
        assert!(!rib_record_excluded(
            &filters,
            &rib_body(&[203, 0, 113], &[only_asn.clone(), only_origin.clone()]),
            4,
            0
        ));
        // Either entry alone leaves the other conjunct with no matching entry, so the record goes.
        assert!(rib_record_excluded(
            &filters,
            &rib_body(&[203, 0, 113], std::slice::from_ref(&only_asn)),
            4,
            0
        ));
        assert!(rib_record_excluded(
            &filters,
            &rib_body(&[203, 0, 113], std::slice::from_ref(&only_origin)),
            4,
            0
        ));
        // An entry that satisfies both keeps the record on its own.
        assert!(!rib_record_excluded(
            &filters,
            &rib_body(&[203, 0, 113], std::slice::from_ref(&both)),
            4,
            0
        ));
        // And a record where neither conjunct can match is skipped.
        let neither = attrs(&[(1, &[0]), (2, &as_path(&[(2, &[64512, 3356])]))]);
        assert!(rib_record_excluded(
            &filters,
            &rib_body(&[203, 0, 113], std::slice::from_ref(&neither)),
            4,
            0
        ));
    }
}