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
|
use std::io::{self, Cursor};
use std::io::{Read, Write};
use std::net::Ipv4Addr;
/// A VRRP version 2 packet.
///
/// Packet format
///
/// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |Version| Type | Virtual Rtr ID| Priority | Count IP Addrs|
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | Auth Type | Adver Int | Checksum |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | IP Address (1) |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | . |
/// | . |
/// | . |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | IP Address (n) |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | Authentication Data (1) |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | Authentication Data (2) |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#[derive(Debug, PartialEq)]
pub struct VRRPv2 {
pub virtual_router_id: u8,
pub priority: u8,
pub count_ip_addrs: u8,
pub auth_type: VRRPv2AuthType,
pub advertisement_interval: u8,
pub checksum: u16,
pub ip_addrs: Vec<Ipv4Addr>,
}
#[derive(Debug, PartialEq)]
pub enum VRRPv2Error {
InvalidAuthType,
InvalidChecksum,
InvalidType,
InvalidVersion,
ParseError,
}
impl std::fmt::Display for VRRPv2Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidAuthType => write!(f, "Invalid Auth Type"),
Self::InvalidChecksum => write!(f, "Invalid Checksum"),
Self::InvalidType => write!(f, "Invalid Type"),
Self::InvalidVersion => write!(f, "Invalid Version"),
Self::ParseError => write!(f, "Parse Error"),
}
}
}
impl std::error::Error for VRRPv2Error {}
impl From<std::io::Error> for VRRPv2Error {
fn from(_err: std::io::Error) -> VRRPv2Error {
VRRPv2Error::ParseError
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum VRRPv2AuthType {
VRRPv2AuthNoAuth = 0x00,
VRRPv2AuthReserved1 = 0x01,
VRRPv2AuthReserved2 = 0x02,
}
trait BytesReader {
fn read_u8(&mut self) -> io::Result<u8>;
fn read_u16(&mut self) -> io::Result<u16>;
fn read_u32(&mut self) -> io::Result<u32>;
}
impl<T: AsRef<[u8]>> BytesReader for Cursor<T> {
fn read_u8(&mut self) -> io::Result<u8> {
let mut buffer = [0; 1];
self.read_exact(&mut buffer)?;
Ok(u8::from_be_bytes(buffer))
}
fn read_u16(&mut self) -> io::Result<u16> {
let mut buffer = [0; 2];
self.read_exact(&mut buffer)?;
Ok(u16::from_be_bytes(buffer))
}
fn read_u32(&mut self) -> io::Result<u32> {
let mut buffer = [0; 4];
self.read_exact(&mut buffer)?;
Ok(u32::from_be_bytes(buffer))
}
}
impl VRRPv2 {
pub fn to_bytes(&self) -> Result<Vec<u8>, std::io::Error> {
let sz = (4 + self.count_ip_addrs) * 4;
let bytes: Vec<u8> = Vec::with_capacity(sz.into());
let mut wr = Cursor::new(bytes);
wr.write(
[
0x21,
self.virtual_router_id,
self.priority,
self.count_ip_addrs,
]
.as_ref(),
)?;
wr.write([self.auth_type as u8, self.advertisement_interval].as_ref())?;
wr.write(self.checksum.to_be_bytes().as_ref())?;
for ip in self.ip_addrs.iter() {
wr.write(&ip.to_bits().to_be_bytes())?;
}
wr.write([0; 4].as_ref())?; // Authentication Data 1
wr.write([0; 4].as_ref())?; // Authentication Data 2
Ok(wr.into_inner())
}
}
fn parse(bytes: &[u8]) -> Result<VRRPv2, VRRPv2Error> {
let mut rdr = Cursor::new(bytes);
let vertype = rdr.read_u8()?;
if (vertype & 0xF) != 1 {
return Err(VRRPv2Error::InvalidType);
}
if (vertype >> 4) != 2 {
return Err(VRRPv2Error::InvalidVersion);
}
let virtual_router_id = rdr.read_u8()?;
let priority = rdr.read_u8()?;
let count_ip_addrs = rdr.read_u8()?;
let auth_type = rdr.read_u8()?;
let auth_type = match auth_type {
0 => VRRPv2AuthType::VRRPv2AuthNoAuth,
1 => VRRPv2AuthType::VRRPv2AuthReserved1,
2 => VRRPv2AuthType::VRRPv2AuthReserved2,
_ => return Err(VRRPv2Error::InvalidAuthType),
};
let advertisement_interval = rdr.read_u8()?;
let checksum = rdr.read_u16()?;
let mut ip_addrs = Vec::with_capacity(count_ip_addrs as usize);
for _i in 0..count_ip_addrs {
let b = rdr.read_u32()?;
ip_addrs.push(Ipv4Addr::from(b));
}
Ok(VRRPv2 {
virtual_router_id,
priority,
count_ip_addrs,
auth_type,
advertisement_interval,
checksum,
ip_addrs,
})
}
/// Parse and validate a slice to construct a VRRPv2 struct.
///
/// # Examples
///
/// ```
/// use vrrpd::vrrpv2::VRRPv2;
/// use vrrpd::vrrpv2::VRRPv2AuthType;
/// use vrrpd::vrrpv2::from_bytes;
/// use std::net::Ipv4Addr;
///
/// let bytes = [
/// 0x21, 0x01, 0x64, 0x01, 0x00, 0x01, 0xba, 0x52, 0xc0, 0xa8, 0x00, 0x01, 0x00, 0x00, 0x00,
/// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/// ];
/// let expected = VRRPv2 {
/// virtual_router_id: 1,
/// priority: 100,
/// count_ip_addrs: 1,
/// auth_type: VRRPv2AuthType::VRRPv2AuthNoAuth,
/// checksum: 47698,
/// advertisement_interval: 1,
/// ip_addrs: vec![Ipv4Addr::from([192, 168, 0, 1])],
/// };
/// assert_eq!(from_bytes(&bytes), Ok(expected));
/// ```
pub fn from_bytes(bytes: &[u8]) -> Result<VRRPv2, VRRPv2Error> {
let vrrpv2 = parse(bytes)?;
if checksum(bytes) != 0 {
return Err(VRRPv2Error::InvalidChecksum);
}
Ok(vrrpv2)
}
fn checksum(bytes: &[u8]) -> u16 {
let (chunks, remainder) = bytes.as_chunks::<2>();
let mut sum = chunks
.iter()
.fold(0, |acc, x| acc + u32::from(u16::from_be_bytes(*x)));
if !remainder.is_empty() {
sum += u32::from(remainder[0]);
}
while (sum >> 16) > 0 {
sum = (sum & 0xffff) + (sum >> 16);
}
!(sum as u16)
}
#[test]
fn test_incomplete_bytes() {
let bytes = [0x21, 0x01];
assert_eq!(from_bytes(&bytes), Err(VRRPv2Error::ParseError));
}
#[test]
fn test_invalid_version() {
let bytes = [
0x31, 0x1, 0x2a, 0x0, 0x0, 0x1, 0xb5, 0xfd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0,
];
assert_eq!(from_bytes(&bytes), Err(VRRPv2Error::InvalidVersion));
}
#[test]
fn test_invalid_type() {
let bytes = [
0x20, 0x2a, 0x64, 0x1, 0x0, 0x1, 0xaa, 0x29, 0xc0, 0xa8, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0,
];
assert_eq!(from_bytes(&bytes), Err(VRRPv2Error::InvalidType));
}
#[test]
fn test_invalid_auth_type() {
let bytes = [
0x21, 0x01, 0x64, 0x01, 0x03, 0x01, 0xba, 0x52, 0xc0, 0xa8, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
assert_eq!(from_bytes(&bytes), Err(VRRPv2Error::InvalidAuthType));
}
#[test]
fn test_invalid_checksum() {
let bytes = [
0x21, 0x01, 0x64, 0x01, 0x00, 0x01, 0xbb, 0x52, 0xc0, 0xa8, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
assert_eq!(from_bytes(&bytes), Err(VRRPv2Error::InvalidChecksum));
}
#[test]
fn test_checksum() {
let bytes = [0x00, 0x01, 0xf2, 0x03, 0xf4, 0xf5, 0xf6, 0xf7];
assert_eq!(checksum(&bytes), 0x220d);
}
#[test]
fn test_checksum_singlebyte() {
let bytes = [0; 1];
assert_eq!(checksum(&bytes), 0xffff);
}
#[test]
fn test_checksum_twobytes() {
let bytes = [0x00, 0xff];
assert_eq!(checksum(&bytes), 0xff00);
}
#[test]
fn test_checksum_another() {
let bytes = [0xe3, 0x4f, 0x23, 0x96, 0x44, 0x27, 0x99, 0xf3];
assert_eq!(checksum(&bytes), 0x1aff);
}
#[test]
fn test_to_bytes() {
let in_bytes = [
0x21, 0x01, 0x64, 0x01, 0x00, 0x01, 0xba, 0x52, 0xc0, 0xa8, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
];
let vrrpv2 = from_bytes(&in_bytes).expect("parsing failed");
let out_bytes = vrrpv2.to_bytes().expect("conversion failed");
assert_eq!(in_bytes.as_ref(), out_bytes);
}
|