Skip to main content

bgpkit_parser/parser/bgp/attributes/
mod.rs

1mod attr_01_origin;
2mod attr_02_17_as_path;
3mod attr_03_next_hop;
4mod attr_04_med;
5mod attr_05_local_pref;
6mod attr_07_18_aggregator;
7mod attr_08_communities;
8mod attr_09_originator;
9mod attr_10_13_cluster;
10mod attr_14_15_nlri;
11mod attr_16_25_extended_communities;
12mod attr_23_tunnel_encap;
13mod attr_24_traffic_engineering;
14mod attr_26_aigp;
15mod attr_29_linkstate;
16mod attr_32_large_communities;
17mod attr_35_otc;
18mod attr_36_domain_path;
19mod attr_37_sfp;
20mod attr_38_bfd_discriminator;
21mod attr_40_bgp_prefix_sid;
22mod attr_41_bier;
23
24use bytes::{Buf, BufMut, Bytes, BytesMut};
25use log::{debug, warn};
26use std::net::IpAddr;
27
28use crate::models::*;
29
30use crate::encoder::sink::{with_u16_len, with_u8_len};
31use crate::error::{BgpValidationWarning, EncodingError, ParserError};
32use crate::parser::bgp::attributes::attr_01_origin::{encode_origin, parse_origin};
33use crate::parser::bgp::attributes::attr_02_17_as_path::encode_as_path;
34pub(crate) use crate::parser::bgp::attributes::attr_02_17_as_path::parse_as_path;
35use crate::parser::bgp::attributes::attr_03_next_hop::{encode_next_hop, parse_next_hop};
36use crate::parser::bgp::attributes::attr_04_med::{encode_med, parse_med};
37use crate::parser::bgp::attributes::attr_05_local_pref::{encode_local_pref, parse_local_pref};
38use crate::parser::bgp::attributes::attr_07_18_aggregator::{encode_aggregator, parse_aggregator};
39use crate::parser::bgp::attributes::attr_08_communities::{
40    encode_regular_communities, parse_regular_communities,
41};
42use crate::parser::bgp::attributes::attr_09_originator::{
43    encode_originator_id, parse_originator_id,
44};
45use crate::parser::bgp::attributes::attr_10_13_cluster::{encode_clusters, parse_clusters};
46use crate::parser::bgp::attributes::attr_14_15_nlri::encode_nlri;
47pub(crate) use crate::parser::bgp::attributes::attr_14_15_nlri::parse_nlri;
48use crate::parser::bgp::attributes::attr_16_25_extended_communities::{
49    encode_extended_communities, encode_ipv6_extended_communities, parse_extended_community,
50    parse_ipv6_extended_community,
51};
52use crate::parser::bgp::attributes::attr_23_tunnel_encap::{
53    encode_tunnel_encapsulation_attribute, parse_tunnel_encapsulation_attribute,
54};
55use crate::parser::bgp::attributes::attr_24_traffic_engineering::{
56    encode_traffic_engineering, parse_traffic_engineering,
57};
58use crate::parser::bgp::attributes::attr_26_aigp::{encode_aigp, parse_aigp};
59use crate::parser::bgp::attributes::attr_29_linkstate::{
60    encode_link_state_attribute, parse_link_state_attribute,
61};
62use crate::parser::bgp::attributes::attr_32_large_communities::{
63    encode_large_communities, parse_large_communities,
64};
65use crate::parser::bgp::attributes::attr_35_otc::{
66    encode_only_to_customer, parse_only_to_customer,
67};
68use crate::parser::bgp::attributes::attr_36_domain_path::{encode_domain_path, parse_domain_path};
69use crate::parser::bgp::attributes::attr_37_sfp::{encode_sfp, parse_sfp};
70use crate::parser::bgp::attributes::attr_38_bfd_discriminator::{
71    encode_bfd_discriminator, parse_bfd_discriminator,
72};
73use crate::parser::bgp::attributes::attr_40_bgp_prefix_sid::{
74    encode_bgp_prefix_sid, parse_bgp_prefix_sid,
75};
76use crate::parser::bgp::attributes::attr_41_bier::{encode_bier, parse_bier};
77use crate::parser::ReadUtils;
78
79/// Validate attribute flags according to RFC 4271 and RFC 7606
80fn validate_attribute_flags(
81    attr_type: AttrType,
82    flags: AttrFlags,
83    warnings: &mut Vec<BgpValidationWarning>,
84) {
85    let expected_flags = match attr_type {
86        // Well-known mandatory attributes
87        AttrType::ORIGIN | AttrType::AS_PATH | AttrType::NEXT_HOP => AttrFlags::TRANSITIVE,
88        // Well-known discretionary attributes
89        AttrType::ATOMIC_AGGREGATE => AttrFlags::TRANSITIVE,
90        // Optional non-transitive attributes
91        AttrType::MULTI_EXIT_DISCRIMINATOR
92        | AttrType::ORIGINATOR_ID
93        | AttrType::CLUSTER_LIST
94        | AttrType::MP_REACHABLE_NLRI
95        | AttrType::MP_UNREACHABLE_NLRI
96        | AttrType::TRAFFIC_ENGINEERING => AttrFlags::OPTIONAL,
97        // Optional transitive attributes
98        AttrType::AGGREGATOR
99        | AttrType::AS4_AGGREGATOR
100        | AttrType::AS4_PATH
101        | AttrType::COMMUNITIES
102        | AttrType::EXTENDED_COMMUNITIES
103        | AttrType::IPV6_ADDRESS_SPECIFIC_EXTENDED_COMMUNITIES
104        | AttrType::LARGE_COMMUNITIES
105        | AttrType::ONLY_TO_CUSTOMER
106        | AttrType::BGP_DOMAIN_PATH => AttrFlags::OPTIONAL | AttrFlags::TRANSITIVE,
107        // LOCAL_PREFERENCE is well-known mandatory for IBGP
108        AttrType::LOCAL_PREFERENCE => AttrFlags::TRANSITIVE,
109        // Unknown or development attributes
110        _ => return, // Don't validate unknown attributes
111    };
112
113    // Check if flags match expected (ignoring EXTENDED and PARTIAL flags for this check)
114    let relevant_flags = flags & (AttrFlags::OPTIONAL | AttrFlags::TRANSITIVE);
115    if relevant_flags != expected_flags {
116        warnings.push(BgpValidationWarning::AttributeFlagsError {
117            attr_type,
118            expected_flags: expected_flags.bits(),
119            actual_flags: relevant_flags.bits(),
120        });
121    }
122
123    // Check partial flag constraint
124    if flags.contains(AttrFlags::PARTIAL) {
125        match attr_type {
126            // Partial bit MUST be 0 for well-known attributes and optional non-transitive
127            AttrType::ORIGIN
128            | AttrType::AS_PATH
129            | AttrType::NEXT_HOP
130            | AttrType::LOCAL_PREFERENCE
131            | AttrType::ATOMIC_AGGREGATE
132            | AttrType::MULTI_EXIT_DISCRIMINATOR
133            | AttrType::ORIGINATOR_ID
134            | AttrType::CLUSTER_LIST
135            | AttrType::MP_REACHABLE_NLRI
136            | AttrType::MP_UNREACHABLE_NLRI
137            | AttrType::TRAFFIC_ENGINEERING => {
138                warnings.push(BgpValidationWarning::AttributeFlagsError {
139                    attr_type,
140                    expected_flags: expected_flags.bits(),
141                    actual_flags: flags.bits(),
142                });
143            }
144            _ => {} // Partial is OK for optional transitive attributes
145        }
146    }
147}
148
149fn is_raw_retained_attr(attr_type: AttrType) -> bool {
150    matches!(
151        attr_type,
152        AttrType::RESERVED
153            | AttrType::PMSI_TUNNEL
154            | AttrType::PE_DISTINGUISHER_LABELS
155            | AttrType::BGPSEC_PATH
156            | AttrType::ATTR_SET
157    )
158}
159
160/// Check if an attribute type is well-known mandatory
161fn is_well_known_mandatory(attr_type: AttrType) -> bool {
162    matches!(
163        attr_type,
164        AttrType::ORIGIN | AttrType::AS_PATH | AttrType::NEXT_HOP | AttrType::LOCAL_PREFERENCE
165    )
166}
167
168pub(crate) struct AttributeValidationState {
169    warnings: Vec<BgpValidationWarning>,
170    attr_mask: [u64; 4],
171}
172
173impl AttributeValidationState {
174    pub(crate) fn new() -> Self {
175        Self {
176            warnings: Vec::new(),
177            attr_mask: [0; 4],
178        }
179    }
180
181    fn has_raw_attr(&self, attr: u8) -> bool {
182        (self.attr_mask[(attr / 64) as usize] & (1u64 << (attr % 64))) != 0
183    }
184
185    pub(crate) fn has_attr(&self, attr_type: AttrType) -> bool {
186        self.has_raw_attr(u8::from(attr_type))
187    }
188
189    fn set_attr(&mut self, attr: u8) {
190        self.attr_mask[(attr / 64) as usize] |= 1u64 << (attr % 64);
191    }
192
193    pub(crate) fn observe_header(
194        &mut self,
195        raw_attr_type: u8,
196        attr_type: AttrType,
197        flags: AttrFlags,
198        length: usize,
199    ) -> bool {
200        if self.has_raw_attr(raw_attr_type) {
201            self.warnings
202                .push(BgpValidationWarning::DuplicateAttribute { attr_type });
203        }
204        self.set_attr(raw_attr_type);
205
206        validate_attribute_flags(attr_type, flags, &mut self.warnings);
207        validate_attribute_length(attr_type, length, &mut self.warnings);
208
209        flags.contains(AttrFlags::PARTIAL)
210    }
211
212    pub(crate) fn observe_parse_error(
213        &mut self,
214        attr_type: AttrType,
215        partial: bool,
216        error: &ParserError,
217    ) {
218        if partial {
219            self.warnings
220                .push(BgpValidationWarning::PartialAttributeError {
221                    attr_type,
222                    reason: error.to_string(),
223                });
224            debug!("PARTIAL attribute error: {}", error);
225        } else if is_well_known_mandatory(attr_type) {
226            self.warnings
227                .push(BgpValidationWarning::MalformedAttributeList {
228                    reason: format!(
229                        "Well-known mandatory attribute {} parsing failed: {}",
230                        u8::from(attr_type),
231                        error
232                    ),
233                });
234            debug!(
235                "Well-known mandatory attribute parsing failed, treating as withdraw: {}",
236                error
237            );
238        } else {
239            self.warnings
240                .push(BgpValidationWarning::OptionalAttributeError {
241                    attr_type,
242                    reason: error.to_string(),
243                });
244            debug!("Optional attribute error, discarding: {}", error);
245        }
246    }
247
248    pub(crate) fn check_mandatory_attributes(
249        &mut self,
250        is_announcement: bool,
251        has_standard_nlri: bool,
252    ) {
253        if !is_announcement {
254            return;
255        }
256
257        if !self.has_attr(AttrType::ORIGIN) {
258            self.warnings
259                .push(BgpValidationWarning::MissingWellKnownAttribute {
260                    attr_type: AttrType::ORIGIN,
261                });
262        }
263
264        if !self.has_attr(AttrType::AS_PATH) {
265            self.warnings
266                .push(BgpValidationWarning::MissingWellKnownAttribute {
267                    attr_type: AttrType::AS_PATH,
268                });
269        }
270
271        let has_mp_reach = self.has_attr(AttrType::MP_REACHABLE_NLRI);
272        if (has_standard_nlri || !has_mp_reach) && !self.has_attr(AttrType::NEXT_HOP) {
273            self.warnings
274                .push(BgpValidationWarning::MissingWellKnownAttribute {
275                    attr_type: AttrType::NEXT_HOP,
276                });
277        }
278    }
279
280    pub(crate) fn finish(self) -> (Vec<BgpValidationWarning>, [u64; 4]) {
281        (self.warnings, self.attr_mask)
282    }
283}
284
285/// Validate attribute length constraints
286fn validate_attribute_length(
287    attr_type: AttrType,
288    length: usize,
289    warnings: &mut Vec<BgpValidationWarning>,
290) {
291    let expected_length = match attr_type {
292        AttrType::ORIGIN => Some(1),
293        AttrType::NEXT_HOP => Some(4), // IPv4 next hop
294        AttrType::MULTI_EXIT_DISCRIMINATOR => Some(4),
295        AttrType::LOCAL_PREFERENCE => Some(4),
296        AttrType::ATOMIC_AGGREGATE => Some(0),
297        AttrType::ORIGINATOR_ID => Some(4),
298        AttrType::ONLY_TO_CUSTOMER => Some(4),
299        // Variable length attributes - no fixed constraint
300        AttrType::AS_PATH
301        | AttrType::AS4_PATH
302        | AttrType::AGGREGATOR
303        | AttrType::AS4_AGGREGATOR
304        | AttrType::COMMUNITIES
305        | AttrType::EXTENDED_COMMUNITIES
306        | AttrType::IPV6_ADDRESS_SPECIFIC_EXTENDED_COMMUNITIES
307        | AttrType::LARGE_COMMUNITIES
308        | AttrType::CLUSTER_LIST
309        | AttrType::MP_REACHABLE_NLRI
310        | AttrType::MP_UNREACHABLE_NLRI => None,
311        _ => None, // Unknown attributes
312    };
313
314    if let Some(expected) = expected_length {
315        if length != expected {
316            warnings.push(BgpValidationWarning::AttributeLengthError {
317                attr_type,
318                expected_length: Some(expected),
319                actual_length: length,
320            });
321        }
322    }
323}
324
325/// Whether RFC 10039 §4 allows the D-PATH attribute on routes of this family: IPVPN, which is
326/// IPv4 or IPv6 with SAFI 128, and EVPN, which is AFI 25 with SAFI 70.
327fn is_domain_path_family(afi: u16, safi: u8) -> bool {
328    matches!((afi, safi), (1 | 2, 128) | (25, 70))
329}
330
331/// The AFI/SAFI header an MP_REACH_NLRI attribute value starts with, when it is long enough to
332/// carry one. The header identifies the family even when the rest did not decode.
333fn mp_reach_family(bytes: &[u8]) -> Option<(u16, u8)> {
334    match bytes.len() >= 3 {
335        true => Some((u16::from_be_bytes([bytes[0], bytes[1]]), bytes[2])),
336        false => None,
337    }
338}
339
340/// The route families a D-PATH-carrying message carries, as far as the caller can tell.
341#[derive(Debug, Clone, Copy, Default)]
342struct DomainPathFamilies {
343    /// Family the caller already knows, e.g. a RIB entry from its record header.
344    declared: Option<(u16, u8)>,
345    /// Family of the routes an MP_REACH_NLRI announces, decoded or read off its header.
346    announced: Option<(u16, u8)>,
347    /// Classic IPv4 unicast NLRI, which the UPDATE parser adds once it has parsed them.
348    classic_ipv4: bool,
349}
350
351impl DomainPathFamilies {
352    /// The families visible in an attribute set on its own, before any NLRI is parsed. An
353    /// MP_UNREACH_NLRI announces nothing, so it says nothing about the family here. An
354    /// MP_REACH_NLRI whose NLRI did not decode still identifies its family through the AFI/SAFI
355    /// header it starts with, which is enough to judge it.
356    fn from_attributes(attributes: &[Attribute], declared: Option<(u16, u8)>) -> Self {
357        let announced = attributes
358            .iter()
359            .find_map(|attribute| match &attribute.value {
360                AttributeValue::MpReachNlri(nlri) => Some((nlri.afi as u16, nlri.safi as u8)),
361                _ => None,
362            })
363            .or_else(|| {
364                attributes
365                    .iter()
366                    .find_map(|attribute| match &attribute.value {
367                        AttributeValue::Raw(raw) | AttributeValue::Unknown(raw)
368                            if raw.code == u8::from(AttrType::MP_REACHABLE_NLRI) =>
369                        {
370                            mp_reach_family(&raw.bytes)
371                        }
372                        _ => None,
373                    })
374            });
375
376        DomainPathFamilies {
377            declared,
378            announced,
379            classic_ipv4: false,
380        }
381    }
382
383    /// Describe a carried family that RFC 10039 §4 does not allow D-PATH on, if there is one.
384    ///
385    /// The rule is about the routes a message carries, so a decoded announcement decides the
386    /// answer and a withdrawal never introduces one. A family this attribute set does not
387    /// reveal stays unknown: the UPDATE parser reports classic IPv4 unicast NLRI once it has
388    /// parsed them, and a caller that knows the family up front passes it in.
389    fn disallowed(&self) -> Option<String> {
390        let declared = self
391            .declared
392            .filter(|(afi, safi)| !is_domain_path_family(*afi, *safi));
393        if let Some((afi, safi)) = declared {
394            return Some(describe_family(afi, safi, "declared"));
395        }
396
397        if let Some((afi, safi)) = self
398            .announced
399            .filter(|(afi, safi)| !is_domain_path_family(*afi, *safi))
400        {
401            return Some(describe_family(afi, safi, "announced"));
402        }
403
404        if self.classic_ipv4 {
405            return Some("classic IPv4 unicast NLRI (AFI 1, SAFI 1)".to_string());
406        }
407
408        None
409    }
410}
411
412fn describe_family(afi: u16, safi: u8, source: &str) -> String {
413    match (afi, safi) {
414        // a mismatched EVPN SAFI is worth naming, since EVPN is AFI 25 with SAFI 70
415        (_, 70) if afi != 25 => {
416            format!("{source} AFI {afi} routes with SAFI 70 (EVPN requires AFI 25)")
417        }
418        _ => format!("{source} AFI {afi} routes with SAFI {safi}"),
419    }
420}
421
422/// RFC 10039 §4 limits D-PATH to UPDATEs that carry IPVPN or EVPN routes and requires the
423/// treat-as-withdraw action for any other family, so report the finding when a carried family
424/// is known to be something else.
425fn domain_path_family_warning(families: DomainPathFamilies) -> Option<BgpValidationWarning> {
426    families.disallowed().map(|family| {
427        BgpValidationWarning::OptionalAttributeError {
428            attr_type: AttrType::BGP_DOMAIN_PATH,
429            reason: format!(
430                "D-PATH is only valid on IPVPN (AFI 1/2, SAFI 128) or EVPN (AFI 25, SAFI 70) routes, this UPDATE carries {family}: RFC 10039 §4 requires treat-as-withdraw"
431            ),
432        }
433    })
434}
435
436/// Parse BGP attributes given a slice of u8 and some options.
437///
438/// The `data: &[u8]` contains the entirety of the attributes bytes, therefore the size of
439/// the slice is the total byte length of the attributes section of the message.
440pub fn parse_attributes(
441    mut data: Bytes,
442    asn_len: &AsnLength,
443    add_path: bool,
444    afi: Option<Afi>,
445    safi: Option<Safi>,
446    prefixes: Option<&[NetworkPrefix]>,
447) -> Result<Attributes, ParserError> {
448    // Estimate capacity from data size: each attribute is at least 3 bytes
449    // (flag + type + length). Cap at 256 to avoid over-allocation for corrupted data.
450    let estimated_attrs = (data.remaining() / 3).min(256);
451    let mut attributes: Vec<Attribute> = Vec::with_capacity(estimated_attrs.max(8));
452    let mut validation = AttributeValidationState::new();
453    // A handle on the whole attribute section, so an attribute that fails to parse can be kept
454    // raw by re-slicing it, without cloning every attribute's bytes up front.
455    let section = data.clone();
456
457    while data.remaining() >= 3 {
458        // each attribute is at least 3 bytes: flag(1) + type(1) + length(1)
459        // thus the while loop condition is set to be at least 3 bytes to read.
460
461        // has content to read
462        let flag = AttrFlags::from_bits_retain(data.read_u8()?);
463        let attr_type = data.read_u8()?;
464        let attr_length = match flag.contains(AttrFlags::EXTENDED) {
465            false => data.read_u8()? as usize,
466            true => data.read_u16()? as usize,
467        };
468
469        debug!(
470            "reading attribute: type -- {:?}, length -- {}",
471            attr_type, attr_length
472        );
473
474        let attr_type = AttrType::from(attr_type);
475
476        let partial = validation.observe_header(attr_type.into(), attr_type, flag, attr_length);
477
478        let bytes_left = data.remaining();
479
480        if data.remaining() < attr_length {
481            warn!(
482                "{:?} attribute encodes a length ({}) that is longer than the remaining attribute data ({}). Skipping remaining attribute data for BGP message",
483                attr_type, attr_length, bytes_left
484            );
485            // break and return already parsed attributes
486            break;
487        }
488
489        // we know data has enough bytes to read, so we can split the bytes into a new Bytes object
490        data.has_n_remaining(attr_length)?;
491        let value_start = section.len() - data.len();
492        let mut attr_data = data.split_to(attr_length);
493        let raw_code = u8::from(attr_type);
494
495        if let Some(t) = get_deprecated_attr_type(raw_code) {
496            debug!("deprecated attribute type: {} - {}", raw_code, t);
497            attributes.push(Attribute {
498                value: AttributeValue::Deprecated(AttrRaw {
499                    code: raw_code,
500                    bytes: attr_data,
501                }),
502                flag,
503            });
504            continue;
505        }
506
507        if matches!(attr_type, AttrType::Unknown(_)) {
508            debug!("unknown attribute type: {}", raw_code);
509            attributes.push(Attribute {
510                value: AttributeValue::Unknown(AttrRaw {
511                    code: raw_code,
512                    bytes: attr_data,
513                }),
514                flag,
515            });
516            continue;
517        }
518
519        if is_raw_retained_attr(attr_type) {
520            debug!("raw-retained attribute type: {}", raw_code);
521            attributes.push(Attribute {
522                value: AttributeValue::Raw(AttrRaw {
523                    code: raw_code,
524                    bytes: attr_data,
525                }),
526                flag,
527            });
528            continue;
529        }
530
531        let attr = match attr_type {
532            AttrType::ORIGIN => parse_origin(attr_data),
533            AttrType::AS_PATH => parse_as_path(attr_data, asn_len).map(AttributeValue::AsPath),
534            AttrType::NEXT_HOP => parse_next_hop(attr_data, &afi),
535            AttrType::MULTI_EXIT_DISCRIMINATOR => parse_med(attr_data),
536            AttrType::LOCAL_PREFERENCE => parse_local_pref(attr_data),
537            AttrType::ATOMIC_AGGREGATE => Ok(AttributeValue::AtomicAggregate),
538            AttrType::AGGREGATOR => parse_aggregator(attr_data, asn_len)
539                .map(|(asn, id)| AttributeValue::Aggregator { asn, id }),
540            AttrType::ORIGINATOR_ID => parse_originator_id(attr_data),
541            AttrType::CLUSTER_LIST => parse_clusters(attr_data),
542            AttrType::MP_REACHABLE_NLRI => {
543                parse_nlri(attr_data, &afi, &safi, &prefixes, true, add_path)
544            }
545            AttrType::MP_UNREACHABLE_NLRI => {
546                parse_nlri(attr_data, &afi, &safi, &prefixes, false, add_path)
547            }
548            AttrType::AS4_PATH => {
549                parse_as_path(attr_data, &AsnLength::Bits32).map(AttributeValue::As4Path)
550            }
551            AttrType::AS4_AGGREGATOR => parse_aggregator(attr_data, &AsnLength::Bits32)
552                .map(|(asn, id)| AttributeValue::As4Aggregator { asn, id }),
553
554            // communities
555            AttrType::COMMUNITIES => parse_regular_communities(attr_data),
556            AttrType::LARGE_COMMUNITIES => parse_large_communities(attr_data),
557            AttrType::EXTENDED_COMMUNITIES => parse_extended_community(attr_data),
558            AttrType::IPV6_ADDRESS_SPECIFIC_EXTENDED_COMMUNITIES => {
559                parse_ipv6_extended_community(attr_data)
560            }
561            AttrType::DEVELOPMENT => {
562                let mut value = vec![];
563                for _i in 0..attr_length {
564                    value.push(attr_data.read_u8()?);
565                }
566                Ok(AttributeValue::Development(value))
567            }
568            AttrType::ONLY_TO_CUSTOMER => parse_only_to_customer(attr_data),
569            AttrType::AIGP => parse_aigp(attr_data),
570            AttrType::TUNNEL_ENCAPSULATION => parse_tunnel_encapsulation_attribute(attr_data),
571            AttrType::TRAFFIC_ENGINEERING => parse_traffic_engineering(attr_data),
572            AttrType::BGP_LS_ATTRIBUTE => parse_link_state_attribute(attr_data),
573            AttrType::BGP_DOMAIN_PATH => parse_domain_path(attr_data),
574            AttrType::SFP_ATTRIBUTE => parse_sfp(attr_data),
575            AttrType::BFD_DISCRIMINATOR => parse_bfd_discriminator(attr_data),
576            AttrType::BGP_PREFIX_SID => parse_bgp_prefix_sid(attr_data),
577            AttrType::BIER => parse_bier(attr_data),
578            _ => Err(ParserError::Unsupported(format!(
579                "unsupported attribute type: {attr_type:?}"
580            ))),
581        };
582
583        match attr {
584            Ok(value) => {
585                assert_eq!(attr_type, value.attr_type());
586                attributes.push(Attribute { value, flag });
587            }
588            Err(e) => {
589                validation.observe_parse_error(attr_type, partial, &e);
590                attributes.push(Attribute {
591                    value: AttributeValue::Raw(AttrRaw {
592                        code: raw_code,
593                        bytes: section.slice(value_start..value_start + attr_length),
594                    }),
595                    flag,
596                });
597                continue;
598            }
599        };
600    }
601
602    let (mut validation_warnings, attr_mask) = validation.finish();
603    if attributes
604        .iter()
605        .any(|attribute| matches!(attribute.value, AttributeValue::DomainPath(_)))
606    {
607        let declared = afi.zip(safi).map(|(afi, safi)| (afi as u16, safi as u8));
608        let families = DomainPathFamilies::from_attributes(&attributes, declared);
609        if let Some(warning) = domain_path_family_warning(families) {
610            validation_warnings.push(warning);
611        }
612    }
613
614    Ok(Attributes {
615        inner: attributes,
616        validation_warnings,
617        attr_mask,
618    })
619}
620
621/// Whether any route in this NLRI carries an ADD-PATH identifier (RFC 7911).
622///
623/// The wire format has no flag for it: the encoder has to infer the state from
624/// the routes themselves, exactly as a decoder is told the state out of band.
625fn has_path_id(nlri: &Nlri) -> bool {
626    nlri.labeled_prefixes
627        .as_ref()
628        .is_some_and(|prefixes| prefixes.iter().any(|p| p.path_id.is_some()))
629        || nlri
630            .evpn_nlris
631            .as_ref()
632            .is_some_and(|routes| routes.iter().any(|r| r.path_id.is_some()))
633}
634
635impl Attribute {
636    /// Append the wire representation of this attribute to `buf`.
637    ///
638    /// The length-field width follows the attribute's EXTENDED_LENGTH flag; a
639    /// value that does not fit the resulting field yields
640    /// [`EncodingError::ValueTooLarge`] instead of being truncated.
641    pub fn encode_to(&self, asn_len: AsnLength, buf: &mut BytesMut) -> Result<(), EncodingError> {
642        buf.put_u8(self.flag.bits());
643        buf.put_u8(self.value.attr_code());
644
645        let write_value = |b: &mut BytesMut| -> Result<(), EncodingError> {
646            match &self.value {
647                AttributeValue::Origin(v) => b.put_slice(&encode_origin(v)),
648                // AS_PATH segment width follows the session's AS number length.
649                AttributeValue::AsPath(path) => {
650                    encode_as_path(path, asn_len, b)?;
651                }
652                // AS4_PATH segments are 4-octet by definition (RFC 6793 §4.2),
653                // independent of the session's AS number length.
654                AttributeValue::As4Path(path) => {
655                    encode_as_path(path, AsnLength::Bits32, b)?;
656                }
657                AttributeValue::NextHop(v) => b.put_slice(&encode_next_hop(v)),
658                AttributeValue::MultiExitDiscriminator(v) => b.put_slice(&encode_med(*v)),
659                AttributeValue::LocalPreference(v) => b.put_slice(&encode_local_pref(*v)),
660                AttributeValue::OnlyToCustomer(v) => {
661                    b.put_slice(&encode_only_to_customer(v.into()))
662                }
663                AttributeValue::AtomicAggregate => {}
664                AttributeValue::Aggregator { asn, id } => {
665                    b.put_slice(&encode_aggregator(asn, &IpAddr::from(*id), asn_len)?)
666                }
667                AttributeValue::As4Aggregator { asn, id } => b.put_slice(&encode_aggregator(
668                    asn,
669                    &IpAddr::from(*id),
670                    AsnLength::Bits32,
671                )?),
672                AttributeValue::Communities(v) => b.put_slice(&encode_regular_communities(v)),
673                AttributeValue::ExtendedCommunities(v) => {
674                    b.put_slice(&encode_extended_communities(v))
675                }
676                AttributeValue::LargeCommunities(v) => b.put_slice(&encode_large_communities(v)),
677                AttributeValue::Ipv6AddressSpecificExtendedCommunities(v) => {
678                    b.put_slice(&encode_ipv6_extended_communities(v))
679                }
680                AttributeValue::OriginatorId(v) => {
681                    b.put_slice(&encode_originator_id(&IpAddr::from(*v)))
682                }
683                AttributeValue::Clusters(v) => b.put_slice(&encode_clusters(v)),
684                AttributeValue::MpReachNlri(v) => {
685                    encode_nlri(v, true, has_path_id(v), b)?;
686                }
687                AttributeValue::MpUnreachNlri(v) => {
688                    // Labeled withdrawals carry plain prefixes (RFC 8277 §2.4),
689                    // but an EVPN withdrawal still carries its ADD-PATH
690                    // identifier, so the state follows the NLRI either way.
691                    encode_nlri(v, false, has_path_id(v), b)?;
692                }
693                AttributeValue::LinkState(v) => encode_link_state_attribute(v, b)?,
694                AttributeValue::TunnelEncapsulation(v) => {
695                    encode_tunnel_encapsulation_attribute(v, b)?
696                }
697                AttributeValue::TrafficEngineering(v) => encode_traffic_engineering(v, b)?,
698                AttributeValue::DomainPath(v) => encode_domain_path(v, b)?,
699                AttributeValue::BfdDiscriminator(v) => encode_bfd_discriminator(v, b)?,
700                AttributeValue::BgpPrefixSid(v) => encode_bgp_prefix_sid(v, b)?,
701                AttributeValue::Bier(v) => encode_bier(v, b)?,
702                AttributeValue::Sfp(v) => encode_sfp(v, b)?,
703                AttributeValue::Development(v) => b.put_slice(v),
704                AttributeValue::Raw(v) => b.put_slice(&v.bytes),
705                AttributeValue::Deprecated(v) => b.put_slice(&v.bytes),
706                AttributeValue::Unknown(v) => b.put_slice(&v.bytes),
707                AttributeValue::Aigp(v) => b.put_slice(&encode_aigp(v)),
708                AttributeValue::AttrSet(_v) => {
709                    return Err(EncodingError::unencodable(
710                        "ATTR_SET attribute",
711                        "encoding not implemented",
712                    ));
713                }
714            }
715            Ok(())
716        };
717
718        match self.is_extended() {
719            false => with_u8_len(buf, "BGP attribute value length", write_value),
720            true => with_u16_len(buf, "BGP attribute value length (extended)", write_value),
721        }
722    }
723
724    /// Encode this attribute into a fresh buffer.
725    pub fn encode(&self, asn_len: AsnLength) -> Result<Bytes, EncodingError> {
726        let mut buf = BytesMut::new();
727        self.encode_to(asn_len, &mut buf)?;
728        Ok(buf.freeze())
729    }
730}
731
732impl Attributes {
733    /// Append the wire representation of all attributes to `buf`.
734    pub fn encode_to(&self, asn_len: AsnLength, buf: &mut BytesMut) -> Result<(), EncodingError> {
735        for attr in &self.inner {
736            attr.encode_to(asn_len, buf)?;
737        }
738        Ok(())
739    }
740
741    /// Encode all attributes into a fresh buffer.
742    pub fn encode(&self, asn_len: AsnLength) -> Result<Bytes, EncodingError> {
743        let mut buf = BytesMut::new();
744        self.encode_to(asn_len, &mut buf)?;
745        Ok(buf.freeze())
746    }
747}
748
749#[cfg(test)]
750mod tests {
751    use super::*;
752
753    #[test]
754    fn test_unknwon_attribute_type() {
755        let data = Bytes::from(vec![0x40, 0xFE, 0x00]);
756        let asn_len = AsnLength::Bits16;
757        let add_path = false;
758        let afi = None;
759        let safi = None;
760        let prefixes = None;
761        let attributes = parse_attributes(data, &asn_len, add_path, afi, safi, prefixes);
762        assert!(attributes.is_ok());
763        let attributes = attributes.unwrap();
764        assert_eq!(attributes.inner.len(), 1);
765        assert_eq!(
766            attributes.inner[0].value.attr_type(),
767            AttrType::Unknown(254)
768        );
769    }
770
771    #[test]
772    fn test_rfc7606_attribute_flags_error() {
773        // Create an ORIGIN attribute with wrong flags (should be transitive, not optional)
774        let data = Bytes::from(vec![0x80, 0x01, 0x01, 0x00]); // Optional flag set incorrectly
775        let asn_len = AsnLength::Bits16;
776        let add_path = false;
777        let afi = None;
778        let safi = None;
779        let prefixes = None;
780
781        let attributes = parse_attributes(data, &asn_len, add_path, afi, safi, prefixes).unwrap();
782
783        // Should have validation warning for incorrect flags
784        assert!(attributes.has_validation_warnings());
785        let warnings = attributes.validation_warnings();
786        // Will have attribute flags error + missing mandatory attributes
787        assert!(!warnings.is_empty());
788
789        match &warnings[0] {
790            BgpValidationWarning::AttributeFlagsError { attr_type, .. } => {
791                assert_eq!(*attr_type, AttrType::ORIGIN);
792            }
793            _ => panic!("Expected AttributeFlagsError warning"),
794        }
795    }
796
797    #[test]
798    fn test_rfc7606_missing_mandatory_attribute() {
799        // Attributes with only LOCAL_PREF (missing ORIGIN, AS_PATH, NEXT_HOP)
800        let data = Bytes::from(vec![
801            0x40, 0x05, 0x04, 0x00, 0x00, 0x00, 0x64, // LOCAL_PREF = 100
802        ]);
803        let asn_len = AsnLength::Bits16;
804        let add_path = false;
805        let afi = None;
806        let safi = None;
807        let prefixes = None;
808
809        let mut attributes =
810            parse_attributes(data, &asn_len, add_path, afi, safi, prefixes).unwrap();
811        // Manually trigger mandatory check as an announcement with standard NLRI
812        attributes.check_mandatory_attributes(true, true);
813
814        // Should have warnings for missing mandatory attributes
815        assert!(attributes.has_validation_warnings());
816        let warnings = attributes.validation_warnings();
817        // LOCAL_PREF is not a withdrawal, so ORIGIN, AS_PATH, NEXT_HOP are required
818        assert_eq!(warnings.len(), 3); // ORIGIN, AS_PATH, NEXT_HOP
819
820        for warning in warnings {
821            match warning {
822                BgpValidationWarning::MissingWellKnownAttribute { attr_type } => {
823                    assert!(matches!(
824                        attr_type,
825                        AttrType::ORIGIN | AttrType::AS_PATH | AttrType::NEXT_HOP
826                    ));
827                }
828                _ => panic!("Expected MissingWellKnownAttribute warning"),
829            }
830        }
831    }
832
833    #[test]
834    fn test_mp_reach_no_next_hop() {
835        // Attributes with MP_REACH_NLRI (missing ORIGIN, AS_PATH)
836        // MP_REACH_NLRI is type 14 (0x0E).
837        // We just need a dummy MP_REACH_NLRI.
838        let data = Bytes::from(vec![
839            0x80, 0x0E, 0x06, 0x00, 0x01, 0x01, // AFI=1, SAFI=1
840            0x00, // Next Hop Len = 0 (invalid for parsing, but enough to trigger logic)
841            0x00, // Reserved
842            0x00, // NLRI
843        ]);
844        let asn_len = AsnLength::Bits16;
845        let add_path = false;
846        let afi = None;
847        let safi = None;
848        let prefixes = None;
849
850        let mut attributes =
851            parse_attributes(data, &asn_len, add_path, afi, safi, prefixes).unwrap();
852        // Manually trigger mandatory check as an announcement but NO standard NLRI (MP only)
853        attributes.check_mandatory_attributes(true, false);
854
855        // Should NOT have NEXT_HOP warning because MP_REACH_NLRI is present and has_standard_nlri is false
856        let warnings = attributes.validation_warnings();
857        let has_next_hop_warning = warnings.iter().any(|w| {
858            matches!(
859                w,
860                BgpValidationWarning::MissingWellKnownAttribute {
861                    attr_type: AttrType::NEXT_HOP
862                }
863            )
864        });
865        assert!(!has_next_hop_warning);
866    }
867
868    #[test]
869    fn test_pure_withdrawal_no_warnings() {
870        // Empty attributes - pure withdrawal
871        let data = Bytes::from(vec![]);
872        let asn_len = AsnLength::Bits16;
873        let add_path = false;
874        let afi = None;
875        let safi = None;
876        let prefixes = None;
877
878        let mut attributes =
879            parse_attributes(data, &asn_len, add_path, afi, safi, prefixes).unwrap();
880        // Manually trigger mandatory check as a withdrawal
881        attributes.check_mandatory_attributes(false, false);
882
883        // Should have NO warnings
884        assert!(!attributes.has_validation_warnings());
885
886        // Attributes with only MP_UNREACH_NLRI - pure withdrawal
887        let data = Bytes::from(vec![
888            0x80, 0x0F, 0x03, 0x00, 0x01, 0x01, // AFI=1, SAFI=1
889        ]);
890        let mut attributes =
891            parse_attributes(data, &asn_len, add_path, afi, safi, prefixes).unwrap();
892        attributes.check_mandatory_attributes(false, false);
893        assert!(!attributes.has_validation_warnings());
894    }
895
896    #[test]
897    fn test_rfc7606_duplicate_attribute() {
898        // Create two ORIGIN attributes
899        let data = Bytes::from(vec![
900            0x40, 0x01, 0x01, 0x00, // First ORIGIN attribute
901            0x40, 0x01, 0x01, 0x01, // Second ORIGIN attribute (duplicate)
902        ]);
903        let asn_len = AsnLength::Bits16;
904        let add_path = false;
905        let afi = None;
906        let safi = None;
907        let prefixes = None;
908
909        let attributes = parse_attributes(data, &asn_len, add_path, afi, safi, prefixes).unwrap();
910
911        // Should have warning for duplicate attribute
912        assert!(attributes.has_validation_warnings());
913        let warnings = attributes.validation_warnings();
914
915        // Should have at least one duplicate attribute warning
916        let has_duplicate_warning = warnings
917            .iter()
918            .any(|w| matches!(w, BgpValidationWarning::DuplicateAttribute { .. }));
919        assert!(has_duplicate_warning);
920    }
921
922    #[test]
923    fn test_attribute_type_boundaries() {
924        let asn_len = AsnLength::Bits16;
925        let add_path = false;
926        let afi = None;
927        let safi = None;
928        let prefixes = None;
929
930        // Required attributes for valid BGP message
931        const REQUIRED_ATTRS: &[u8] = &[
932            0x40, 0x01, 0x01, 0x00, // origin
933            0x40, 0x02, 0x00, // as_path
934            0x40, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, // next_hop
935        ];
936
937        // Test highest (development) attribute type
938        let mut data = REQUIRED_ATTRS.to_vec();
939        data.extend_from_slice(&[0x40, 0xFF, 0x01, 0x00]); // development
940        let data = Bytes::from(data);
941
942        let attributes = parse_attributes(data, &asn_len, add_path, afi, safi, prefixes).unwrap();
943
944        assert!(attributes.has_attr(AttrType::DEVELOPMENT));
945        assert!(!attributes.has_validation_warnings());
946
947        // Test lowest (reserved) attribute type
948        let mut data = REQUIRED_ATTRS.to_vec();
949        data.extend_from_slice(&[0x40, 0x00, 0x01, 0x01]); // reserved
950        let data = Bytes::from(data);
951
952        let attributes = parse_attributes(data, &asn_len, add_path, afi, safi, prefixes).unwrap();
953
954        assert!(attributes.inner.iter().any(|attr| {
955            matches!(
956                &attr.value,
957                AttributeValue::Raw(raw)
958                    if raw.code == u8::from(AttrType::RESERVED)
959                        && raw.bytes == Bytes::from_static(&[0x01])
960            )
961        }));
962        assert!(!attributes.validation_warnings.iter().any(|vw| {
963            matches!(vw, BgpValidationWarning::OptionalAttributeError { attr_type, reason:_ } if *attr_type == AttrType::RESERVED)
964        }));
965    }
966
967    #[test]
968    fn test_raw_retention_for_known_unsupported_attribute() {
969        let data = Bytes::from(vec![0x80, 0x16, 0x03, 0xaa, 0xbb, 0xcc]); // PMSI_TUNNEL
970        let attributes =
971            parse_attributes(data, &AsnLength::Bits16, false, None, None, None).unwrap();
972
973        assert_eq!(attributes.inner.len(), 1);
974        match &attributes.inner[0].value {
975            AttributeValue::Raw(raw) => {
976                assert_eq!(raw.code, 22);
977                assert_eq!(raw.attr_type(), AttrType::PMSI_TUNNEL);
978                assert_eq!(raw.bytes, Bytes::from_static(&[0xaa, 0xbb, 0xcc]));
979            }
980            value => panic!("expected Raw, got {value:?}"),
981        }
982        assert_eq!(
983            attributes.encode(AsnLength::Bits16).unwrap(),
984            Bytes::from_static(&[0x80, 0x16, 0x03, 0xaa, 0xbb, 0xcc])
985        );
986    }
987
988    #[test]
989    fn test_deprecated_code_13_retained_as_deprecated() {
990        let data = Bytes::from(vec![0x80, 0x0d, 0x04, 0x01, 0x02, 0x03, 0x04]);
991        let attributes =
992            parse_attributes(data, &AsnLength::Bits16, false, None, None, None).unwrap();
993
994        assert_eq!(attributes.inner.len(), 1);
995        match &attributes.inner[0].value {
996            AttributeValue::Deprecated(raw) => {
997                assert_eq!(raw.code, 13);
998                assert_eq!(raw.attr_type(), AttrType::Unknown(13));
999                assert_eq!(raw.bytes, Bytes::from_static(&[0x01, 0x02, 0x03, 0x04]));
1000            }
1001            value => panic!("expected Deprecated, got {value:?}"),
1002        }
1003        assert_eq!(
1004            attributes.encode(AsnLength::Bits16).unwrap(),
1005            Bytes::from_static(&[0x80, 0x0d, 0x04, 0x01, 0x02, 0x03, 0x04])
1006        );
1007    }
1008
1009    #[test]
1010    fn test_malformed_typed_attribute_falls_back_to_raw() {
1011        let data = Bytes::from(vec![0x40, 0x03, 0x03, 0x01, 0x02, 0x03]); // NEXT_HOP length must be 4
1012        let attributes =
1013            parse_attributes(data, &AsnLength::Bits16, false, None, None, None).unwrap();
1014
1015        assert!(attributes.has_validation_warnings());
1016        match &attributes.inner[0].value {
1017            AttributeValue::Raw(raw) => {
1018                assert_eq!(raw.code, 3);
1019                assert_eq!(raw.attr_type(), AttrType::NEXT_HOP);
1020                assert_eq!(raw.bytes, Bytes::from_static(&[0x01, 0x02, 0x03]));
1021            }
1022            value => panic!("expected Raw fallback, got {value:?}"),
1023        }
1024        assert_eq!(
1025            attributes.encode(AsnLength::Bits16).unwrap(),
1026            Bytes::from_static(&[0x40, 0x03, 0x03, 0x01, 0x02, 0x03])
1027        );
1028    }
1029
1030    #[test]
1031    fn test_malformed_attribute_after_others_keeps_its_own_bytes() {
1032        let data = Bytes::from(
1033            [
1034                // ORIGIN IGP
1035                &[0x40, 0x01, 0x01, 0x00][..],
1036                // NEXT_HOP with an extended length of 3 (must be 4)
1037                &[0x50, 0x03, 0x00, 0x03, 0x0a, 0x0b, 0x0c],
1038                // MED 7
1039                &[0x80, 0x04, 0x04, 0x00, 0x00, 0x00, 0x07],
1040            ]
1041            .concat(),
1042        );
1043        let attributes =
1044            parse_attributes(data, &AsnLength::Bits16, false, None, None, None).unwrap();
1045
1046        assert_eq!(attributes.inner.len(), 3);
1047        assert_eq!(
1048            attributes.inner[0].value,
1049            AttributeValue::Origin(Origin::IGP)
1050        );
1051        match &attributes.inner[1].value {
1052            AttributeValue::Raw(raw) => {
1053                assert_eq!(raw.code, 3);
1054                assert_eq!(raw.bytes, Bytes::from_static(&[0x0a, 0x0b, 0x0c]));
1055            }
1056            value => panic!("expected Raw fallback, got {value:?}"),
1057        }
1058        assert_eq!(
1059            attributes.inner[2].value,
1060            AttributeValue::MultiExitDiscriminator(7)
1061        );
1062    }
1063
1064    #[test]
1065    fn test_all_raw_retained_attribute_codes_parse_and_round_trip() {
1066        let raw_codes = [0, 22, 24, 27, 33, 128];
1067
1068        for code in raw_codes {
1069            let wire = vec![0xc0, code, 0x02, 0xaa, 0xbb];
1070            let attributes = parse_attributes(
1071                Bytes::from(wire.clone()),
1072                &AsnLength::Bits16,
1073                false,
1074                None,
1075                None,
1076                None,
1077            )
1078            .unwrap();
1079            assert_eq!(attributes.inner.len(), 1, "code {code}");
1080            match &attributes.inner[0].value {
1081                AttributeValue::Raw(raw) => {
1082                    assert_eq!(raw.code, code);
1083                    assert_eq!(raw.bytes, Bytes::from_static(&[0xaa, 0xbb]));
1084                }
1085                value => panic!("expected Raw for code {code}, got {value:?}"),
1086            }
1087            assert!(attributes.has_attr(AttrType::from(code)), "code {code}");
1088            assert_eq!(
1089                attributes.encode(AsnLength::Bits16).unwrap(),
1090                Bytes::from(wire)
1091            );
1092        }
1093    }
1094
1095    #[test]
1096    fn test_unassigned_attribute_code_retained_as_unknown() {
1097        let wire = vec![0xc0, 0x7f, 0x02, 0xaa, 0xbb];
1098        let attributes = parse_attributes(
1099            Bytes::from(wire.clone()),
1100            &AsnLength::Bits16,
1101            false,
1102            None,
1103            None,
1104            None,
1105        )
1106        .unwrap();
1107
1108        assert_eq!(attributes.inner.len(), 1);
1109        match &attributes.inner[0].value {
1110            AttributeValue::Unknown(raw) => {
1111                assert_eq!(raw.code, 0x7f);
1112                assert_eq!(raw.attr_type(), AttrType::Unknown(0x7f));
1113                assert_eq!(raw.bytes, Bytes::from_static(&[0xaa, 0xbb]));
1114            }
1115            value => panic!("expected Unknown, got {value:?}"),
1116        }
1117        assert!(attributes.has_attr(AttrType::Unknown(0x7f)));
1118        assert_eq!(
1119            attributes.encode(AsnLength::Bits16).unwrap(),
1120            Bytes::from(wire)
1121        );
1122    }
1123
1124    #[test]
1125    fn test_structured_tlv_attributes_parse_and_round_trip() {
1126        let cases = [
1127            (
1128                vec![0xc0, 0x26, 0x05, 0x01, 0x01, 0x02, 0x03, 0x04],
1129                "BFD Discriminator",
1130            ),
1131            (
1132                vec![0xc0, 0x28, 0x05, 0x7f, 0x00, 0x02, 0xaa, 0xbb],
1133                "BGP Prefix-SID",
1134            ),
1135            (
1136                vec![0xc0, 0x29, 0x06, 0x12, 0x34, 0x00, 0x02, 0xde, 0xad],
1137                "BIER",
1138            ),
1139            (vec![0xc0, 0x25, 0x05, 0x7f, 0x00, 0x02, 0xde, 0xad], "SFP"),
1140            (
1141                vec![
1142                    0xc0, 0x24, 0x0f, // BGP Domain Path, 15-octet value
1143                    0x02, // segment: 2 domains
1144                    0x00, 0x00, 0x00, 0x0A, 0x00, 0x02, 0x80, // ASN 10 / 2 / 128
1145                    0x00, 0x00, 0x00, 0x0B, 0x00, 0x03, 0x46, // ASN 11 / 3 / 70
1146                ],
1147                "BGP Domain Path",
1148            ),
1149        ];
1150
1151        for (wire, name) in cases {
1152            let data = Bytes::from(wire.clone());
1153            let attributes =
1154                parse_attributes(data, &AsnLength::Bits16, false, None, None, None).unwrap();
1155            assert_eq!(attributes.inner.len(), 1, "{name}");
1156            match (name, &attributes.inner[0].value) {
1157                ("BFD Discriminator", AttributeValue::BfdDiscriminator(_))
1158                | ("BGP Prefix-SID", AttributeValue::BgpPrefixSid(_))
1159                | ("BIER", AttributeValue::Bier(_))
1160                | ("SFP", AttributeValue::Sfp(_))
1161                | ("BGP Domain Path", AttributeValue::DomainPath(_)) => {}
1162                (_, value) => panic!("unexpected value for {name}: {value:?}"),
1163            }
1164            assert_eq!(
1165                attributes.encode(AsnLength::Bits16).unwrap(),
1166                Bytes::from(wire),
1167                "{name}"
1168            );
1169        }
1170    }
1171
1172    #[test]
1173    fn test_rfc7606_attribute_length_error() {
1174        // Create an ORIGIN attribute with wrong length (should be 1 byte, not 2)
1175        let data = Bytes::from(vec![0x40, 0x01, 0x02, 0x00, 0x01]);
1176        let asn_len = AsnLength::Bits16;
1177        let add_path = false;
1178        let afi = None;
1179        let safi = None;
1180        let prefixes = None;
1181
1182        let attributes = parse_attributes(data, &asn_len, add_path, afi, safi, prefixes).unwrap();
1183
1184        // Should have warning for incorrect attribute length
1185        assert!(attributes.has_validation_warnings());
1186        let warnings = attributes.validation_warnings();
1187
1188        let has_length_warning = warnings
1189            .iter()
1190            .any(|w| matches!(w, BgpValidationWarning::AttributeLengthError { .. }));
1191        assert!(has_length_warning);
1192    }
1193
1194    #[test]
1195    fn test_rfc7606_no_session_reset() {
1196        // Test that parsing continues even with multiple errors
1197        let data = Bytes::from(vec![
1198            0x80, 0x01, 0x02, 0x00, 0x01, // Wrong flags and length for ORIGIN
1199            0x40, 0x01, 0x01, 0x00, // Duplicate ORIGIN
1200            0x40, 0xFF, 0x01, 0x00, // Unknown attribute
1201        ]);
1202        let asn_len = AsnLength::Bits16;
1203        let add_path = false;
1204        let afi = None;
1205        let safi = None;
1206        let prefixes = None;
1207
1208        // Should not panic or return error - RFC 7606 requires continued parsing
1209        let result = parse_attributes(data, &asn_len, add_path, afi, safi, prefixes);
1210        assert!(result.is_ok());
1211
1212        let attributes = result.unwrap();
1213        assert!(attributes.has_validation_warnings());
1214
1215        // Should have multiple warnings but parsing should continue
1216        let warnings = attributes.validation_warnings();
1217        assert!(!warnings.is_empty());
1218    }
1219
1220    #[test]
1221    fn test_encode_as_path_attribute_type_selection() {
1222        // Regression test for issue #329: an AS_PATH built for a 4-octet
1223        // session must encode as attribute type 2 with 4-octet segments, not
1224        // as AS4_PATH (type 17).
1225        let attrs = Attributes::from_iter(vec![AttributeValue::AsPath(AsPath::from_sequence([
1226            400644,
1227        ]))]);
1228        let buf = attrs.encode(AsnLength::Bits32).unwrap();
1229        assert_eq!(buf[1], 2, "attribute type must be AS_PATH (2)");
1230        // segment header (type 2, count 1) + one 4-octet ASN
1231        assert_eq!(&buf[3..], &[0x02, 0x01, 0x00, 0x06, 0x1D, 0x04]);
1232
1233        // AS4_PATH always encodes as type 17 with 4-octet segments, even on a
1234        // 2-octet session (RFC 6793 §4.2).
1235        let attrs = Attributes::from_iter(vec![AttributeValue::As4Path(AsPath::from_sequence([
1236            400644,
1237        ]))]);
1238        let buf = attrs.encode(AsnLength::Bits16).unwrap();
1239        assert_eq!(buf[1], 17, "attribute type must be AS4_PATH (17)");
1240        assert_eq!(&buf[3..], &[0x02, 0x01, 0x00, 0x06, 0x1D, 0x04]);
1241    }
1242
1243    #[test]
1244    fn test_encode_as_path_on_2octet_session() {
1245        let attrs = Attributes::from_iter(vec![AttributeValue::AsPath(AsPath::from_sequence([
1246            64496, 64497,
1247        ]))]);
1248        let buf = attrs.encode(AsnLength::Bits16).unwrap();
1249        assert_eq!(buf[1], 2);
1250        // segment header (type 2, count 2) + two 2-octet ASNs
1251        assert_eq!(&buf[3..], &[0x02, 0x02, 0xFB, 0xF0, 0xFB, 0xF1]);
1252    }
1253
1254    #[test]
1255    fn test_encode_as_path_rejects_4octet_asn_on_2octet_session() {
1256        // A 4-octet AS number that does not fit a 2-octet segment must be an
1257        // encoding error, not a silent truncation to its low 16 bits.
1258        let attrs = Attributes::from_iter(vec![AttributeValue::AsPath(AsPath::from_sequence([
1259            400644,
1260        ]))]);
1261        let err = attrs.encode(AsnLength::Bits16).unwrap_err();
1262        assert_eq!(
1263            err,
1264            EncodingError::ValueTooLarge {
1265                field: "2-octet AS number in AS_PATH segment",
1266                actual: 400644,
1267                max: 65535,
1268            }
1269        );
1270    }
1271
1272    #[test]
1273    fn test_encode_aggregator_attribute_type_selection() {
1274        let attrs = Attributes::from_iter(vec![AttributeValue::As4Aggregator {
1275            asn: Asn::new_32bit(400644),
1276            id: std::net::Ipv4Addr::new(192, 0, 2, 1),
1277        }]);
1278        let buf = attrs.encode(AsnLength::Bits16).unwrap();
1279        assert_eq!(buf[1], 18, "attribute type must be AS4_AGGREGATOR (18)");
1280        assert_eq!(
1281            buf.len(),
1282            3 + 8,
1283            "value must be a 4-octet ASN plus an IPv4 id"
1284        );
1285    }
1286
1287    #[test]
1288    fn test_encode_aggregator_width_follows_session_not_asn_flag() {
1289        // A 4-octet-flagged Asn holding a value that fits 16 bits must encode
1290        // as 2 octets on a 2-octet session: the session width decides, not the
1291        // Asn's internal flag.
1292        let attrs = Attributes::from_iter(vec![AttributeValue::Aggregator {
1293            asn: Asn::new_32bit(258),
1294            id: std::net::Ipv4Addr::new(10, 0, 0, 1),
1295        }]);
1296        let buf = attrs.encode(AsnLength::Bits16).unwrap();
1297        assert_eq!(buf[1], 7, "attribute type must be AGGREGATOR (7)");
1298        assert_eq!(buf.len(), 3 + 6);
1299        assert_eq!(&buf[3..], &[0x01, 0x02, 10, 0, 0, 1]);
1300    }
1301
1302    #[test]
1303    fn test_parse_attributes_maps_as4_wire_types() {
1304        // AS4_PATH (type 17): optional transitive flag 0xC0, one AS_SEQUENCE
1305        // segment with the 4-octet ASN 400644.
1306        let data = Bytes::from(vec![0xC0, 17, 6, 0x02, 0x01, 0x00, 0x06, 0x1D, 0x04]);
1307        let attributes = parse_attributes(data, &AsnLength::Bits16, false, None, None, None)
1308            .expect("AS4_PATH must parse");
1309        match attributes.get_attr(AttrType::AS4_PATH).unwrap().value {
1310            AttributeValue::As4Path(path) => assert_eq!(
1311                path.to_u32_vec_opt(false).unwrap(),
1312                vec![400644],
1313                "AS4_PATH segments must parse as 4-octet regardless of session length"
1314            ),
1315            other => panic!("expected As4Path variant, got {other:?}"),
1316        }
1317
1318        // AS4_AGGREGATOR (type 18): 4-octet ASN plus an IPv4 id.
1319        let data = Bytes::from(vec![0xC0, 18, 8, 0x00, 0x06, 0x1D, 0x04, 192, 0, 2, 1]);
1320        let attributes = parse_attributes(data, &AsnLength::Bits16, false, None, None, None)
1321            .expect("AS4_AGGREGATOR must parse");
1322        match attributes.get_attr(AttrType::AS4_AGGREGATOR).unwrap().value {
1323            AttributeValue::As4Aggregator { asn, .. } => {
1324                assert_eq!(asn, Asn::new_32bit(400644))
1325            }
1326            other => panic!("expected As4Aggregator variant, got {other:?}"),
1327        }
1328    }
1329}