zettabgp 0.4.1

This is a BGP (parsing and composing) and BMP (only parsing) protocols driver library for Rust
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
// Copyright 2021 Vladimir Melnikov.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::{ntoh16, slice, slice_mut, BgpCapability, BgpError, BgpMessage, BgpSessionParams};
use std::vec::Vec;
/// BGP open message
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BgpOpenMessage {
    /// Autonomous system number
    pub as_num: u32,
    /// Hold time in seconds
    pub hold_time: u16,
    /// router Id
    pub router_id: std::net::Ipv4Addr,
    /// Capability set
    pub caps: Vec<BgpCapability>,
}

#[repr(C, packed)]
struct BgpOpenHead {
    as_num: u16,
    hold_time: u16,
    routerid: [u8; 4],
    caplen: u8,
}

impl BgpMessage for BgpOpenMessage {
    fn decode_from(&mut self, _peer: &BgpSessionParams, buf: &[u8]) -> Result<(), BgpError> {
        if buf.len() < 10 {
            return Err(BgpError::InsufficientBufferSize(file!(), line!()));
        }
        if buf[0] != 4 {
            return Err(BgpError::static_str("Invalid BGP version <> 4"));
        }
        let ptr: *const u8 = slice(buf, 1, buf.len())?.as_ptr();
        let ptr: *const BgpOpenHead = ptr as *const BgpOpenHead;
        let ptr: &BgpOpenHead = unsafe { &*ptr };
        self.as_num = ntoh16(ptr.as_num) as u32;
        self.hold_time = ntoh16(ptr.hold_time);
        self.router_id = std::net::Ipv4Addr::new(
            ptr.routerid[0],
            ptr.routerid[1],
            ptr.routerid[2],
            ptr.routerid[3],
        );
        self.caps.clear();
        let mut pos: usize = 10;
        while pos + 1 < buf.len() {
            if buf[pos] != 2 {
                return Err(BgpError::from_string(format!(
                    "Invalid optional parameter in BGP open message {:?}!",
                    buf[pos]
                )));
            }
            let mut optlen = buf[pos + 1] as usize;
            pos += 2;
            while optlen > 0 {
                let maybe_cap = BgpCapability::from_buffer(slice(buf, pos, pos + optlen)?)?;
                optlen -= maybe_cap.1;
                pos += maybe_cap.1;
                match maybe_cap.0 {
                    Ok(cap) => self.caps.push(cap),
                    Err((captype, data)) => log::trace!(
                        "ignoring unknown capability code {} data {:x?}",
                        captype,
                        data
                    ),
                }
            }
        }
        Ok(())
    }
    fn encode_to(&self, _peer: &BgpSessionParams, buf: &mut [u8]) -> Result<usize, BgpError> {
        if buf.len() < 10 {
            return Err(BgpError::InsufficientBufferSize(file!(), line!()));
        }
        let ptr: *mut u8 = slice_mut(buf, 1, buf.len())?.as_mut_ptr();
        let ptr: *mut BgpOpenHead = ptr as *mut BgpOpenHead;
        let ptr: &mut BgpOpenHead = unsafe { &mut *ptr };
        buf[0] = 4;
        ptr.as_num = ntoh16(if self.as_num < 65536 {
            self.as_num as u16
        } else {
            23456
        });
        ptr.hold_time = ntoh16(self.hold_time);
        ptr.routerid = self.router_id.octets();
        ptr.caplen = self
            .caps
            .iter()
            .fold(0u32, |sum, i| sum + (i.bytes_len() as u32) + 2) as u8;
        let mut pos: usize = 10;
        for cp in self.caps.iter() {
            let caplen = cp.bytes_len();
            buf[pos] = 2; //capability
            buf[pos + 1] = caplen as u8;
            cp.fill_buffer(slice_mut(buf, pos + 2, caplen + pos + 2)?)?;
            pos += 2 + caplen;
        }
        Ok(pos)
    }
}
impl BgpOpenMessage {
    pub fn new() -> BgpOpenMessage {
        BgpOpenMessage {
            as_num: 0,
            hold_time: 180,
            router_id: std::net::Ipv4Addr::new(127, 0, 0, 1),
            caps: Vec::new(),
        }
    }
}
impl Default for BgpOpenMessage {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_good_open() {
        // Setup
        let mut buf = vec![0_u8; 4096];
        let caps = vec![
            BgpCapability::SafiIPv4u,
            BgpCapability::CapRR,
            BgpCapability::CapASN32(65450),
        ];
        let params = BgpSessionParams::new(
            65001,
            30,
            BgpTransportMode::IPv4,
            "10.0.0.1".parse().unwrap(),
            caps.clone(),
        );
        let msg = BgpOpenMessage {
            as_num: 200,
            router_id: "10.0.0.1".parse().unwrap(),
            caps,
            hold_time: 180,
        };

        let encode = msg.encode_to(&params, &mut buf);
        assert!(encode.is_ok());

        let mut decode_msg = BgpOpenMessage::new();

        // Trucate to fit encoded message length
        buf.truncate(encode.unwrap());

        let decode = decode_msg.decode_from(&params, &buf);
        match decode {
            Ok(_) => {
                assert_eq!(decode_msg.as_num, msg.as_num);
                assert_eq!(decode_msg.hold_time, msg.hold_time);
                assert_eq!(decode_msg.router_id, msg.router_id,);
                assert_eq!(decode_msg.caps.len(), msg.caps.len());
                for c in decode_msg.caps.iter() {
                    assert!(msg.caps.contains(c));
                }
            }
            _ => panic!("incorrect decode: {:?}", decode),
        }
    }

    #[test]
    fn test_concatenated_open() {
        // Setup
        let mut buf = vec![0_u8; 4096];
        let caps = vec![
            BgpCapability::SafiIPv4u,
            BgpCapability::CapRR,
            BgpCapability::CapASN32(65450),
        ];
        let params = BgpSessionParams::new(
            65001,
            30,
            BgpTransportMode::IPv4,
            "10.0.0.1".parse().unwrap(),
            caps.clone(),
        );
        let msg = BgpOpenMessage {
            as_num: 200,
            router_id: "10.0.0.1".parse().unwrap(),
            caps,
            hold_time: 180,
        };

        let encode = msg.encode_to(&params, &mut buf);
        assert!(encode.is_ok());

        let encode = msg.encode_to(&params, &mut buf);
        assert!(encode.is_ok());

        // Trucate to fit encoded message length
        buf.truncate(encode.unwrap());

        let mut decode_msg = BgpOpenMessage::new();
        let decode = decode_msg.decode_from(&params, &buf);
        match decode {
            Ok(_) => {
                assert_eq!(decode_msg.as_num, msg.as_num);
                assert_eq!(decode_msg.hold_time, msg.hold_time);
                assert_eq!(decode_msg.router_id, msg.router_id);
                assert_eq!(decode_msg.caps.len(), msg.caps.len());
                for c in decode_msg.caps.iter() {
                    assert!(params.caps.contains(c), "caps.contains(): {:?}", c);
                }
            }
            _ => panic!("incorrect decode: {:?}", decode),
        }

        let mut decode_msg = BgpOpenMessage::new();
        let decode = decode_msg.decode_from(&params, &buf);
        match decode {
            Ok(_) => {
                assert_eq!(decode_msg.as_num, msg.as_num);
                assert_eq!(decode_msg.hold_time, msg.hold_time);
                assert_eq!(decode_msg.router_id, msg.router_id);
                assert_eq!(decode_msg.caps.len(), msg.caps.len());
                for c in decode_msg.caps.iter() {
                    assert!(params.caps.contains(c), "caps.contains(): {:?}", c);
                }
            }
            _ => panic!("incorrect decode: {:?}", decode),
        }
    }

    #[test]
    fn test_bad_open_decode_length() {
        // Setup
        let mut buf = vec![0_u8; 4096];
        let caps = vec![
            BgpCapability::SafiIPv4u,
            BgpCapability::CapRR,
            BgpCapability::CapASN32(65450),
        ];
        let params = BgpSessionParams::new(
            65001,
            30,
            BgpTransportMode::IPv4,
            "10.0.0.1".parse().unwrap(),
            caps.clone(),
        );
        let mut msg = BgpOpenMessage {
            as_num: 200,
            router_id: "10.0.0.1".parse().unwrap(),
            caps,
            hold_time: 180,
        };

        let encode = msg.encode_to(&params, &mut buf);
        assert!(encode.is_ok());

        // Truncate encoded cap data
        let truncate_amount = 3;
        buf.truncate(encode.unwrap() - truncate_amount as usize);
        let decode = msg.decode_from(&params, &buf);
        assert!(matches!(decode, Err(BgpError::InsufficientBufferSize(_, _))));
    }

    #[test]
    fn test_bad_open_bgp_version() {
        // Setup
        let mut buf = vec![0_u8; 4096];
        let caps = vec![
            BgpCapability::SafiIPv4u,
            BgpCapability::CapRR,
            BgpCapability::CapASN32(65450),
        ];
        let params = BgpSessionParams::new(
            65001,
            30,
            BgpTransportMode::IPv4,
            "10.0.0.1".parse().unwrap(),
            caps.clone(),
        );
        let msg = BgpOpenMessage {
            as_num: 200,
            router_id: "10.0.0.1".parse().unwrap(),
            caps,
            hold_time: 180,
        };

        let encode = msg.encode_to(&params, &mut buf);
        assert!(encode.is_ok());

        // Set bgp version to be 3
        buf[0] = 3;

        let mut decode_msg = BgpOpenMessage::new();
        let decode = decode_msg.decode_from(&params, &buf);
        assert!(matches!(
            decode,
            Err(BgpError::Static("Invalid BGP version <> 4"))
        ));
    }

    #[test]
    fn test_bad_open_decode_empty() {
        // Setup
        let buf = vec![0_u8; 0];
        let caps = vec![
            BgpCapability::SafiIPv4u,
            BgpCapability::CapRR,
            BgpCapability::CapASN32(65450),
        ];
        let params = BgpSessionParams::new(
            65001,
            30,
            BgpTransportMode::IPv4,
            "10.0.0.1".parse().unwrap(),
            caps,
        );

        let mut decode_msg = BgpOpenMessage::new();
        let decode = decode_msg.decode_from(&params, &buf);
        assert!(matches!(decode, Err(BgpError::InsufficientBufferSize(_, _))));
    }

    #[test]
    fn test_bad_open_missing_optlen() {
        // Setup
        let mut buf = vec![0_u8; 4096];
        let caps = vec![
            BgpCapability::SafiIPv4u,
            BgpCapability::CapRR,
            BgpCapability::CapASN32(65450),
        ];
        let params = BgpSessionParams::new(
            65001,
            30,
            BgpTransportMode::IPv4,
            "10.0.0.1".parse().unwrap(),
            caps.clone(),
        );
        let msg = BgpOpenMessage {
            as_num: 200,
            router_id: "10.0.0.1".parse().unwrap(),
            caps,
            hold_time: 180,
        };

        let encode = msg.encode_to(&params, &mut buf);
        assert!(encode.is_ok());

        // Truncate to remove optlen
        buf.truncate(10_usize);

        let mut decode_msg = BgpOpenMessage::new();
        let decode = decode_msg.decode_from(&params, &buf);
        assert!(decode.is_ok());
    }

    #[test]
    fn test_bad_open_encode_empty() {
        // Setup
        let mut buf = vec![0_u8; 0];
        let caps = vec![
            BgpCapability::SafiIPv4u,
            BgpCapability::CapRR,
            BgpCapability::CapASN32(65450),
        ];
        let params = BgpSessionParams::new(
            65001,
            30,
            BgpTransportMode::IPv4,
            "10.0.0.1".parse().unwrap(),
            caps.clone(),
        );
        let msg = BgpOpenMessage {
            as_num: 200,
            router_id: "10.0.0.1".parse().unwrap(),
            caps,
            hold_time: 180,
        };

        let encode = msg.encode_to(&params, &mut buf);
        assert!(matches!(encode, Err(BgpError::InsufficientBufferSize(_, _))));
    }

    #[test]
    fn test_bad_open_encode_length() {
        // Setup
        let mut buf = vec![0_u8; 20];
        let caps = vec![
            BgpCapability::SafiIPv4u,
            BgpCapability::CapRR,
            BgpCapability::CapASN32(65450),
        ];
        let params = BgpSessionParams::new(
            65001,
            30,
            BgpTransportMode::IPv4,
            "10.0.0.1".parse().unwrap(),
            caps.clone(),
        );
        let msg = BgpOpenMessage {
            as_num: 200,
            router_id: "10.0.0.1".parse().unwrap(),
            caps,
            hold_time: 180,
        };

        let encode = msg.encode_to(&params, &mut buf);
        assert!(matches!(encode, Err(BgpError::InsufficientBufferSize(_, _))));
    }
}