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
use anyhow::Context;
use std::net::IpAddr;
use crate::{
constants::{self, RTA_GATEWAY},
emit_ip,
ip_len,
nlas::{Nla, NlaBuffer},
parsers::parse_ip,
traits::{Emitable, Parseable},
DecodeError,
};
bitflags! {
pub struct NextHopFlags: u8 {
const RTNH_F_EMPTY = 0;
const RTNH_F_DEAD = constants::RTNH_F_DEAD as u8;
const RTNH_F_PERVASIVE = constants::RTNH_F_PERVASIVE as u8;
const RTNH_F_ONLINK = constants::RTNH_F_ONLINK as u8;
const RTNH_F_OFFLOAD = constants::RTNH_F_OFFLOAD as u8;
const RTNH_F_LINKDOWN = constants::RTNH_F_LINKDOWN as u8;
const RTNH_F_UNRESOLVED = constants::RTNH_F_UNRESOLVED as u8;
}
}
buffer!(NextHopBuffer {
length: (u16, 0..2),
flags: (u8, 2),
hops: (u8, 3),
interface_id: (u32, 4..8),
gateway_nla: (slice, GATEWAY_OFFSET..),
});
impl<T: AsRef<[u8]>> NextHopBuffer<T> {
pub fn new_checked(buffer: T) -> Result<Self, DecodeError> {
let packet = Self::new(buffer);
packet.check_buffer_length()?;
Ok(packet)
}
fn check_buffer_length(&self) -> Result<(), DecodeError> {
let len = self.buffer.as_ref().len();
if len < 8 {
return Err(format!("invalid NextHopBuffer: length {} < {}", len, 8).into());
}
if len < self.length() as usize {
return Err(format!(
"invalid NextHopBuffer: length {} < {}",
len,
8 + self.length()
)
.into());
}
Ok(())
}
}
const GATEWAY_OFFSET: usize = 8;
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct NextHop {
pub flags: NextHopFlags,
pub hops: u8,
pub interface_id: u32,
pub gateway: Option<IpAddr>,
}
impl<'a, T: AsRef<[u8]>> Parseable<NextHopBuffer<&'a T>> for NextHop {
fn parse(buf: &NextHopBuffer<&T>) -> Result<NextHop, DecodeError> {
let gateway = if buf.length() as usize > GATEWAY_OFFSET {
let gateway_nla_buf = NlaBuffer::new_checked(buf.gateway_nla())
.context("cannot parse RTA_GATEWAY attribute in next-hop")?;
if gateway_nla_buf.kind() != RTA_GATEWAY {
return Err(format!("invalid RTA_GATEWAY attribute in next-hop: expected NLA type to be RTA_GATEWAY ({}), but got {} instead", RTA_GATEWAY, gateway_nla_buf.kind()).into());
}
let gateway = parse_ip(gateway_nla_buf.value()).context(
"invalid RTA_GATEWAY attribute in next-hop: failed to parse NLA value as an IP address",
)?;
Some(gateway)
} else {
None
};
Ok(NextHop {
flags: NextHopFlags::from_bits_truncate(buf.flags()),
hops: buf.hops(),
interface_id: buf.interface_id(),
gateway,
})
}
}
struct GatewayNla<'a>(&'a IpAddr);
impl<'a> Nla for GatewayNla<'a> {
fn value_len(&self) -> usize {
ip_len(self.0)
}
fn kind(&self) -> u16 {
RTA_GATEWAY
}
fn emit_value(&self, buffer: &mut [u8]) {
emit_ip(buffer, self.0)
}
}
impl Emitable for NextHop {
fn buffer_len(&self) -> usize {
8 + self
.gateway
.as_ref()
.map(|ip| {
4 + ip_len(ip)
})
.unwrap_or(0)
}
fn emit(&self, buffer: &mut [u8]) {
let mut nh_buffer = NextHopBuffer::new(buffer);
nh_buffer.set_length(self.buffer_len() as u16);
nh_buffer.set_flags(self.flags.bits());
nh_buffer.set_hops(self.hops);
nh_buffer.set_interface_id(self.interface_id);
if let Some(ref gateway) = self.gateway {
let gateway_nla = GatewayNla(gateway);
gateway_nla.emit(nh_buffer.gateway_nla_mut());
}
}
}