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
#[cfg(feature = "serialization")]
extern crate serde;
pub mod afi;
pub mod bmp;
pub mod error;
pub mod message;
pub mod prelude;
pub mod util;
use error::*;
use message::open::*;
use util::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BgpTransportMode {
IPv4,
IPv6,
}
impl From<std::net::IpAddr> for BgpTransportMode {
#[inline]
fn from(addr: std::net::IpAddr) -> Self {
match addr {
std::net::IpAddr::V4(_) => BgpTransportMode::IPv4,
std::net::IpAddr::V6(_) => BgpTransportMode::IPv6,
}
}
}
pub trait BgpAddrItem<T: std::marker::Sized> {
fn decode_from(mode: BgpTransportMode, buf: &[u8]) -> Result<(T, usize), BgpError>;
fn encode_to(&self, mode: BgpTransportMode, buf: &mut [u8]) -> Result<usize, BgpError>;
}
pub trait BgpMessage {
fn decode_from(&mut self, peer: &BgpSessionParams, buf: &[u8]) -> Result<(), BgpError>;
fn encode_to(&self, peer: &BgpSessionParams, buf: &mut [u8]) -> Result<usize, BgpError>;
}
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct MacAddress {
pub mac_address: [u8; 6],
}
impl MacAddress {
pub fn new() -> MacAddress {
MacAddress {
mac_address: [0 as u8; 6],
}
}
pub fn from(b: &[u8]) -> MacAddress {
let mut bf = [0 as u8; 6];
bf.clone_from_slice(&b[0..6]);
MacAddress { mac_address: bf }
}
}
impl std::fmt::Display for MacAddress {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
self.mac_address[0],
self.mac_address[1],
self.mac_address[2],
self.mac_address[3],
self.mac_address[4],
self.mac_address[5],
)
}
}
impl std::fmt::Debug for MacAddress {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
self.mac_address[0],
self.mac_address[1],
self.mac_address[2],
self.mac_address[3],
self.mac_address[4],
self.mac_address[5]
))
}
}
#[cfg(feature = "serialization")]
impl serde::Serialize for MacAddress {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.to_string().as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BgpCapability {
SafiIPv4u,
SafiIPv4m,
SafiIPv4mvpn,
SafiIPv4fu,
SafiVPNv4u,
SafiVPNv4fu,
SafiVPNv4m,
SafiIPv4lu,
SafiIPv6u,
SafiIPv6lu,
SafiIPv6fu,
SafiVPNv6u,
SafiVPNv6m,
SafiVPLS,
SafiEVPN,
CapASN32(u32),
CapRR,
}
impl BgpCapability {
fn bytes_len(&self) -> usize {
match self {
BgpCapability::SafiIPv4u => 6,
BgpCapability::SafiIPv4fu => 6,
BgpCapability::SafiIPv4m => 6,
BgpCapability::SafiIPv4mvpn => 6,
BgpCapability::SafiVPNv4u => 6,
BgpCapability::SafiVPNv4fu => 6,
BgpCapability::SafiVPNv4m => 6,
BgpCapability::SafiIPv4lu => 6,
BgpCapability::SafiIPv6u => 6,
BgpCapability::SafiIPv6lu => 6,
BgpCapability::SafiIPv6fu => 6,
BgpCapability::SafiVPNv6u => 6,
BgpCapability::SafiVPNv6m => 6,
BgpCapability::SafiVPLS => 6,
BgpCapability::SafiEVPN => 6,
BgpCapability::CapASN32(_) => 6,
BgpCapability::CapRR => 2,
}
}
fn fill_buffer(&self, buf: &mut [u8]) {
match self {
BgpCapability::SafiIPv4u => {
buf.clone_from_slice(&[1, 4, 0, 1, 0, 1]);
}
BgpCapability::SafiIPv4fu => {
buf.clone_from_slice(&[1, 4, 0, 1, 0, 133]);
}
BgpCapability::SafiIPv4m => {
buf.clone_from_slice(&[1, 4, 0, 1, 0, 4]);
}
BgpCapability::SafiIPv4mvpn => {
buf.clone_from_slice(&[1, 4, 0, 1, 0, 5]);
}
BgpCapability::SafiVPNv4u => {
buf.clone_from_slice(&[1, 4, 0, 1, 0, 128]);
}
BgpCapability::SafiVPNv4fu => {
buf.clone_from_slice(&[1, 4, 0, 1, 0, 134]);
}
BgpCapability::SafiVPNv4m => {
buf.clone_from_slice(&[1, 4, 0, 1, 0, 129]);
}
BgpCapability::SafiIPv4lu => {
buf.clone_from_slice(&[1, 4, 0, 1, 0, 2]);
}
BgpCapability::SafiIPv6u => {
buf.clone_from_slice(&[1, 4, 0, 2, 0, 1]);
}
BgpCapability::SafiIPv6fu => {
buf.clone_from_slice(&[1, 4, 0, 2, 0, 133]);
}
BgpCapability::SafiIPv6lu => {
buf.clone_from_slice(&[1, 4, 0, 2, 0, 4]);
}
BgpCapability::SafiVPNv6u => {
buf.clone_from_slice(&[1, 4, 0, 2, 0, 128]);
}
BgpCapability::SafiVPNv6m => {
buf.clone_from_slice(&[1, 4, 0, 2, 0, 129]);
}
BgpCapability::SafiVPLS => {
buf.clone_from_slice(&[1, 4, 0, 25, 0, 65]);
}
BgpCapability::SafiEVPN => {
buf.clone_from_slice(&[1, 4, 0, 25, 0, 70]);
}
BgpCapability::CapASN32(as_num) => {
buf.clone_from_slice(&[
65,
4,
(as_num >> 24) as u8,
((as_num >> 16) & 0xff) as u8,
((as_num >> 8) & 0xff) as u8,
(as_num & 0xff) as u8,
]);
}
BgpCapability::CapRR => {
buf.clone_from_slice(&[2, 0]);
}
};
}
fn from_buffer(buf: &[u8]) -> Result<(BgpCapability, usize), BgpError> {
if buf.len() >= 6 && buf[0] == 1 && buf[1] == 4 && buf[2] == 0 {
if buf[3] == 1 && buf[4] == 0 {
match buf[5] {
1 => Ok((BgpCapability::SafiIPv4u, 6)),
2 => Ok((BgpCapability::SafiIPv4lu, 6)),
4 => Ok((BgpCapability::SafiIPv4m, 6)),
5 => Ok((BgpCapability::SafiIPv4mvpn, 6)),
128 => Ok((BgpCapability::SafiVPNv4u, 6)),
129 => Ok((BgpCapability::SafiVPNv4m, 6)),
133 => Ok((BgpCapability::SafiIPv4fu, 6)),
134 => Ok((BgpCapability::SafiVPNv4fu, 6)),
_ => Err(BgpError::static_str("Invalid ipv4 safi capability")),
}
} else if buf[3] == 2 && buf[4] == 0 {
match buf[5] {
1 => Ok((BgpCapability::SafiIPv6u, 6)),
4 => Ok((BgpCapability::SafiIPv6lu, 6)),
128 => Ok((BgpCapability::SafiVPNv6u, 6)),
129 => Ok((BgpCapability::SafiVPNv6m, 6)),
133 => Ok((BgpCapability::SafiIPv6fu, 6)),
_ => Err(BgpError::static_str("Invalid ipv6 safi capability")),
}
} else if buf[3] == 25 && buf[4] == 0 && buf[5] == 65 {
match buf[5] {
65 => Ok((BgpCapability::SafiVPLS, 6)),
70 => Ok((BgpCapability::SafiEVPN, 6)),
_ => Err(BgpError::static_str("Invalid vpls safi capability")),
}
} else {
Err(BgpError::static_str("Invalid capability"))
}
} else if buf.len() >= 6 && buf[0] == 65 && buf[1] == 4 {
Ok((BgpCapability::CapASN32(getn_u32(&buf[2..6])), 6))
} else if buf.len() >= 2 && buf[0] == 2 && buf[1] == 0 {
Ok((BgpCapability::CapRR, 2))
} else {
Err(BgpError::static_str("Invalid capability"))
}
}
}
#[derive(Debug, Clone)]
pub struct BgpSessionParams {
pub as_num: u32,
pub hold_time: u16,
pub peer_mode: BgpTransportMode,
pub has_as32bit: bool,
pub router_id: std::net::Ipv4Addr,
pub caps: std::collections::HashSet<BgpCapability>,
}
impl BgpSessionParams {
pub fn new(
asnum: u32,
holdtime: u16,
peermode: BgpTransportMode,
routerid: std::net::Ipv4Addr,
cps: std::collections::HashSet<BgpCapability>,
) -> BgpSessionParams {
BgpSessionParams {
as_num: asnum,
hold_time: holdtime,
peer_mode: peermode,
has_as32bit: true,
router_id: routerid,
caps: cps,
}
}
pub fn open_message(&self) -> BgpOpenMessage {
let mut bom = BgpOpenMessage::new();
bom.as_num = self.as_num;
bom.router_id = self.router_id;
bom.caps = self.caps.iter().copied().collect();
bom.hold_time = self.hold_time;
bom
}
pub fn check_caps(&mut self) {
self.has_as32bit = false;
for cap in self.caps.iter() {
match cap {
BgpCapability::CapASN32(n) => {
self.has_as32bit = true;
if self.as_num != 0 && self.as_num != 23456 && self.as_num != *n {
eprintln!(
"Warning: Capability 32-bit AS mismatch AS number: {:?}!={:?}",
self.as_num, *n
);
}
self.as_num = *n;
}
_ => {}
}
}
}
pub fn decode_message_head(
&self,
buf: &[u8],
) -> Result<(message::BgpMessageType, usize), BgpError> {
if buf.len() < 19 {
return Err(BgpError::static_str("Invalid message header size!"));
}
for q in buf[0..16].iter() {
if (*q) != 255 {
return Err(BgpError::static_str(
"Invalid header content, MD5 is not supported!",
));
}
}
let messagetype = message::BgpMessageType::decode_from(buf[18])?;
Ok((messagetype, (getn_u16(&buf[16..18]) - 19) as usize))
}
pub fn recv_message_head(
&mut self,
rdsrc: &mut impl std::io::Read,
) -> Result<(message::BgpMessageType, usize), BgpError> {
let mut buf = [0 as u8; 19];
rdsrc.read_exact(&mut buf)?;
self.decode_message_head(&buf)
}
pub fn prepare_message_buf(
&self,
buf: &mut [u8],
messagetype: message::BgpMessageType,
messagelen: usize,
) -> Result<usize, BgpError> {
if buf.len() < (messagelen + 19) {
return Err(BgpError::insufficient_buffer_size());
}
buf[0..16].clone_from_slice(&[255 as u8; 16]);
let lng: u16 = (messagelen as u16) + 19;
buf[16] = (lng >> 8) as u8;
buf[17] = (lng & 0xff) as u8;
buf[18] = messagetype.encode();
Ok(lng as usize)
}
pub fn send_message_buf(
&mut self,
wrdst: &mut impl std::io::Write,
buf: &mut [u8],
messagetype: message::BgpMessageType,
messagelen: usize,
) -> Result<(), BgpError> {
if buf.len() < (messagelen + 19) {
return Err(BgpError::insufficient_buffer_size());
}
buf[0..16].clone_from_slice(&[255 as u8; 16]);
let lng: u16 = (messagelen as u16) + 19;
buf[16] = (lng >> 8) as u8;
buf[17] = (lng & 0xff) as u8;
buf[18] = messagetype.encode();
match wrdst.write_all(&buf[0..(lng as usize)]) {
Ok(_) => Ok(()),
Err(e) => Err(e.into()),
}
}
}