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
use crate::models::*;
use crate::parser::bgp::attributes::attr_03_next_hop::parse_mp_next_hop;
use crate::parser::bgp::attributes::attr_29_linkstate::parse_link_state_nlri;
use crate::parser::{parse_nlri_list, ReadUtils};
use crate::ParserError;
use bytes::{BufMut, Bytes, BytesMut};
use log::warn;
///
/// <https://datatracker.ietf.org/doc/html/rfc4760#section-3>
/// The attribute is encoded as shown below:
/// +---------------------------------------------------------+
/// | Address Family Identifier (2 octets) |
/// +---------------------------------------------------------+
/// | Subsequent Address Family Identifier (1 octet) |
/// +---------------------------------------------------------+
/// | Length of Next Hop Network Address (1 octet) |
/// +---------------------------------------------------------+
/// | Network Address of Next Hop (variable) |
/// +---------------------------------------------------------+
/// | Reserved (1 octet) |
/// +---------------------------------------------------------+
/// | Network Layer Reachability Information (variable) |
/// +---------------------------------------------------------+
pub fn parse_nlri(
mut input: Bytes,
afi: &Option<Afi>,
safi: &Option<Safi>,
prefixes: &Option<&[NetworkPrefix]>,
reachable: bool, // whether the NLRI is announcements or withdrawals
additional_paths: bool, // whether the NLRI is part of an additional paths message
) -> Result<AttributeValue, ParserError> {
let first_byte_zero = input.first().map(|b| *b == 0).unwrap_or(false);
// read address family
let afi = match afi {
Some(afi) => {
if first_byte_zero {
input.read_afi()?
} else {
afi.to_owned()
}
}
None => input.read_afi()?,
};
let safi = match safi {
Some(safi) => {
if first_byte_zero {
input.read_safi()?
} else {
safi.to_owned()
}
}
None => input.read_safi()?,
};
let mut next_hop = None;
if reachable {
let next_hop_length = input.read_u8()? as usize;
input.has_n_remaining(next_hop_length)?;
let next_hop_bytes = input.split_to(next_hop_length);
next_hop = parse_mp_next_hop(next_hop_bytes)?;
}
// Handle Link-State NLRI differently from traditional IP prefixes
let (prefixes, link_state_nlris) =
if afi == Afi::LinkState && (safi == Safi::LinkState || safi == Safi::LinkStateVpn) {
// Parse Link-State NLRI
if reachable {
// skip reserved byte for reachable NRLI
if input.read_u8()? != 0 {
warn!("NLRI reserved byte not 0 (parsing NLRI Link-State)");
}
}
let ls_nlri = parse_link_state_nlri(input, afi, safi, next_hop, reachable)?;
let link_state_list = ls_nlri.link_state_nlris;
(Vec::new(), link_state_list)
} else {
// Parse traditional IP prefixes
let prefixes = match prefixes {
Some(pfxs) => {
// skip parsing prefixes: https://datatracker.ietf.org/doc/html/rfc6396#section-4.3.4
if first_byte_zero {
if reachable {
// skip reserved byte for reachable NRLI
if input.read_u8()? != 0 {
warn!("NLRI reserved byte not 0 (parsing NLRI prefixes)");
}
}
parse_nlri_list(input, additional_paths, &afi)?
} else {
pfxs.to_vec()
}
}
None => {
if reachable {
// skip reserved byte for reachable NRLI
if input.read_u8()? != 0 {
warn!("NLRI reserved byte not 0 (parsing NLRI prefixes)");
}
}
parse_nlri_list(input, additional_paths, &afi)?
}
};
(prefixes, None)
};
let nlri = Nlri {
afi,
safi,
next_hop,
prefixes,
link_state_nlris,
flowspec_nlris: None,
};
match reachable {
true => Ok(AttributeValue::MpReachNlri(nlri)),
false => Ok(AttributeValue::MpUnreachNlri(nlri)),
}
}
/// Encode a NLRI attribute.
pub fn encode_nlri(nlri: &Nlri, reachable: bool) -> Bytes {
let mut bytes = BytesMut::new();
// encode address family
bytes.put_u16(nlri.afi as u16);
bytes.put_u8(nlri.safi as u8);
if let Some(next_hop) = &nlri.next_hop {
if !reachable {
warn!("NLRI next hop should not be set for unreachable NLRI (encoding NLRI)");
}
// encode next hop
let next_hop_bytes = match next_hop {
NextHopAddress::Ipv4(ip) => ip.octets().to_vec(),
NextHopAddress::Ipv6(ip) => ip.octets().to_vec(),
NextHopAddress::Ipv6LinkLocal(ip1, ip2) => {
let mut ip_bytes = ip1.octets().to_vec();
ip_bytes.extend_from_slice(&ip2.octets());
ip_bytes
}
// RFC 8950: VPN-IPv6 next hop (24 bytes)
NextHopAddress::VpnIpv6(rd, ip) => {
let mut ip_bytes = rd.0.to_vec(); // 8 bytes RD
ip_bytes.extend_from_slice(&ip.octets()); // 16 bytes IPv6
ip_bytes
}
// RFC 8950: VPN-IPv6 next hop with link-local (48 bytes)
NextHopAddress::VpnIpv6LinkLocal(rd1, ip1, rd2, ip2) => {
let mut ip_bytes = rd1.0.to_vec(); // 8 bytes RD1
ip_bytes.extend_from_slice(&ip1.octets()); // 16 bytes IPv6
ip_bytes.extend_from_slice(&rd2.0); // 8 bytes RD2
ip_bytes.extend_from_slice(&ip2.octets()); // 16 bytes IPv6 link-local
ip_bytes
}
};
bytes.put_u8(next_hop_bytes.len() as u8);
bytes.put_slice(&next_hop_bytes);
}
// write reserved byte for reachable NRLI
if reachable {
bytes.put_u8(0);
}
// Handle Link-State NLRI encoding
if nlri.afi == Afi::LinkState {
if let Some(link_state_nlris) = &nlri.link_state_nlris {
// Encode Link-State NLRI entries
for ls_nlri in link_state_nlris {
// Encode each Link-State NLRI (this would need a proper implementation)
// For now, we'll create a placeholder
bytes.put_u16(ls_nlri.nlri_type as u16);
bytes.put_u16(0); // Length placeholder - would need actual encoding
}
}
} else {
// NLRI for traditional IP prefixes
for prefix in &nlri.prefixes {
bytes.extend(prefix.encode());
}
}
bytes.freeze()
}
#[cfg(test)]
mod tests {
use super::*;
use ipnet::IpNet;
use std::net::Ipv4Addr;
use std::str::FromStr;
#[test]
fn test_parsing_nlri_simple() {
let test_bytes = Bytes::from(vec![
0x00, 0x01, // address family: IPv4
0x01, // safi: unicast
0x04, // next hop length: 4
0xC0, 0x00, 0x02, 0x01, // next hop: 192.0.2.1
0x00, // reserved
// NLRI
0x18, // 24 bits prefix length
0xC0, 0x00, 0x02, // 192.0.2
]);
let res = parse_nlri(test_bytes, &None, &None, &None, true, false);
if let Ok(AttributeValue::MpReachNlri(nlri)) = res {
assert_eq!(nlri.afi, Afi::Ipv4);
assert_eq!(nlri.safi, Safi::Unicast);
assert_eq!(
nlri.next_hop,
Some(NextHopAddress::Ipv4(
Ipv4Addr::from_str("192.0.2.1").unwrap()
))
);
assert_eq!(
nlri.prefixes,
vec![NetworkPrefix::from_str("192.0.2.0/24").unwrap()]
);
}
}
#[test]
fn test_parsing_nlri_passed_in_afi() {
let test_bytes = Bytes::from(vec![
0x00, 0x01, // address family: IPv4
0x01, // safi: unicast
0x04, // next hop length: 4
0xC0, 0x00, 0x02, 0x01, // next hop: 192.0.2.1
0x00, // reserved
// NLRI
0x18, // 24 bits prefix length
0xC0, 0x00, 0x02, // 192.0.2
]);
let res = parse_nlri(
test_bytes,
&Some(Afi::Ipv4),
&Some(Safi::Unicast),
&None,
true,
false,
);
if let Ok(AttributeValue::MpReachNlri(nlri)) = res {
assert_eq!(nlri.afi, Afi::Ipv4);
assert_eq!(nlri.safi, Safi::Unicast);
assert_eq!(
nlri.next_hop,
Some(NextHopAddress::Ipv4(
Ipv4Addr::from_str("192.0.2.1").unwrap()
))
);
assert_eq!(
nlri.prefixes,
vec![NetworkPrefix::from_str("192.0.2.0/24").unwrap()]
);
}
}
#[test]
fn test_parsing_nlri_errors() {
// wrong next hop
let test_bytes = Bytes::from(vec![
0x00, 0x01, // address family: IPv4
0x01, // safi: unicast
0x04, // next hop length: 4
0xC0, 0x00, 0x02, // next hop: 192.0.2.??
0x00, // reserved
// NLRI
0x18, // 24 bits prefix length
0xC0, 0x00, 0x02, // 192.0.2
]);
let res = parse_nlri(
test_bytes,
&Some(Afi::Ipv4),
&Some(Safi::Unicast),
&None,
true,
false,
);
assert!(res.is_err());
}
#[test]
fn test_parsing_nlri_add_path() {
let test_bytes = Bytes::from(vec![
0x00, 0x01, // address family: IPv4
0x01, // safi: unicast
0x04, // next hop length: 4
0xC0, 0x00, 0x02, 0x01, // next hop: 192.0.2.1
0x00, // reserved
// NLRI
0x00, 0x00, 0x00, 0x7B, // path_id: 123
0x18, // 24 bits prefix length
0xC0, 0x00, 0x02, // 192.0.2
]);
let res = parse_nlri(test_bytes, &None, &None, &None, true, true);
if let Ok(AttributeValue::MpReachNlri(nlri)) = res {
assert_eq!(nlri.afi, Afi::Ipv4);
assert_eq!(nlri.safi, Safi::Unicast);
assert_eq!(
nlri.next_hop,
Some(NextHopAddress::Ipv4(
Ipv4Addr::from_str("192.0.2.1").unwrap()
))
);
let prefix = NetworkPrefix::new(IpNet::from_str("192.0.2.0/24").unwrap(), Some(123));
assert_eq!(nlri.prefixes[0], prefix);
assert_eq!(nlri.prefixes[0].path_id, prefix.path_id);
} else {
panic!("Unexpected result: {res:?}");
}
}
#[test]
fn test_encode_nlri() {
let nlri = Nlri {
afi: Afi::Ipv4,
safi: Safi::Unicast,
next_hop: Some(NextHopAddress::Ipv4(
Ipv4Addr::from_str("10.0.0.1").unwrap(),
)),
prefixes: vec![NetworkPrefix {
prefix: IpNet::from_str("192.0.1.0/24").unwrap(),
path_id: None,
}],
link_state_nlris: None,
flowspec_nlris: None,
};
let bytes = encode_nlri(&nlri, true);
assert_eq!(
bytes,
Bytes::from(vec![
0x00, 0x01, // address family: IPv4
0x01, // safi: unicast
0x04, // next hop length: 4
0x0A, 0x00, 0x00, 0x01, // next hop:
0x00, // reserved
// NLRI
0x18, // 24 bits prefix length
0xC0, 0x00, 0x01, // 192.0.1
])
);
let parsed_nlri = parse_nlri(bytes, &None, &None, &None, true, false).unwrap();
assert_eq!(parsed_nlri, AttributeValue::MpReachNlri(nlri));
let nlri = Nlri {
afi: Afi::Ipv4,
safi: Safi::Unicast,
next_hop: Some(NextHopAddress::Ipv4(
Ipv4Addr::from_str("10.0.0.1").unwrap(),
)),
prefixes: vec![NetworkPrefix {
prefix: IpNet::from_str("192.0.1.0/24").unwrap(),
path_id: Some(123),
}],
link_state_nlris: None,
flowspec_nlris: None,
};
let bytes = encode_nlri(&nlri, true);
assert_eq!(
bytes,
Bytes::from(vec![
0x00, 0x01, // address family: IPv4
0x01, // safi: unicast
0x04, // next hop length: 4
0x0A, 0x00, 0x00, 0x01, // next hop:
0x00, // reserved
// NLRI
0x00, 0x00, 0x00, 0x7B, // path_id: 123
0x18, // 24 bits prefix length
0xC0, 0x00, 0x01, // 192.0.1
])
);
}
#[test]
fn test_parsing_unreachable_nlri() {
// Test unreachable NLRI (withdrawals)
let test_bytes = Bytes::from(vec![
0x00, 0x01, // address family: IPv4
0x01, // safi: unicast
// No next hop for unreachable NLRI
// No reserved byte for unreachable NLRI
// NLRI
0x18, // 24 bits prefix length
0xC0, 0x00, 0x02, // 192.0.2
]);
let res = parse_nlri(test_bytes, &None, &None, &None, false, false);
if let Ok(AttributeValue::MpUnreachNlri(nlri)) = res {
assert_eq!(nlri.afi, Afi::Ipv4);
assert_eq!(nlri.safi, Safi::Unicast);
assert_eq!(nlri.next_hop, None);
assert_eq!(
nlri.prefixes,
vec![NetworkPrefix::from_str("192.0.2.0/24").unwrap()]
);
} else {
panic!("Unexpected result: {res:?}");
}
}
#[test]
fn test_encode_unreachable_nlri() {
// Test encoding unreachable NLRI (withdrawals)
let nlri = Nlri {
afi: Afi::Ipv4,
safi: Safi::Unicast,
next_hop: None,
prefixes: vec![NetworkPrefix {
prefix: IpNet::from_str("192.0.1.0/24").unwrap(),
path_id: None,
}],
link_state_nlris: None,
flowspec_nlris: None,
};
let bytes = encode_nlri(&nlri, false);
assert_eq!(
bytes,
Bytes::from(vec![
0x00, 0x01, // address family: IPv4
0x01, // safi: unicast
// No next hop for unreachable NLRI
// No reserved byte for unreachable NLRI
// NLRI
0x18, // 24 bits prefix length
0xC0, 0x00, 0x01, // 192.0.1
])
);
// Test that the encoded bytes can be parsed back correctly
let parsed_nlri = parse_nlri(bytes, &None, &None, &None, false, false).unwrap();
assert_eq!(parsed_nlri, AttributeValue::MpUnreachNlri(nlri));
// Test with next_hop set (should be encoded for unreachable NLRI)
let nlri_with_next_hop = Nlri {
afi: Afi::Ipv4,
safi: Safi::Unicast,
next_hop: Some(NextHopAddress::Ipv4(
Ipv4Addr::from_str("10.0.0.1").unwrap(),
)),
prefixes: vec![NetworkPrefix {
prefix: IpNet::from_str("192.0.1.0/24").unwrap(),
path_id: None,
}],
link_state_nlris: None,
flowspec_nlris: None,
};
let bytes = encode_nlri(&nlri_with_next_hop, false);
// The encoded bytes should include the next_hop
assert_eq!(
bytes,
Bytes::from(vec![
0x00, 0x01, // address family: IPv4
0x01, // safi: unicast
0x04, // next hop length: 4
0x0A, 0x00, 0x00, 0x01, // next hop: 10.0.0.1
// No reserved byte for unreachable NLRI
// NLRI
0x18, // 24 bits prefix length
0xC0, 0x00, 0x01, // 192.0.1
])
);
}
}