summaryrefslogtreecommitdiff
path: root/src/vrrpv2.rs
blob: 16bd74e62fe2ec22c8d0c154c994aede5db8512f (plain)
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
use nom::bits::{bits, streaming::take};
use nom::combinator::map_res;
use nom::error::{Error, ErrorKind};
use nom::multi::count;
use nom::number::complete::{be_u16, be_u32, u8};
use nom::sequence::tuple;
use nom::{Err, IResult};
use std::net::Ipv4Addr;

const VRRP_REQUIRED_VERSION: u8 = 2;
const VRRP_REQUIRED_TYPE: u8 = 1; // Advertisement

#[derive(Debug, Clone, PartialEq)]
pub enum VRRPv2Error {
    VRRPv2ParseError,
}

type NomError<'a> = nom::Err<nom::error::Error<&'a [u8]>>;
impl From<NomError<'_>> for VRRPv2Error {
    fn from(_: NomError) -> Self {
        Self::VRRPv2ParseError
    }
}

#[derive(Debug, PartialEq)]
pub enum VRRPVersion {
    V2,
}

#[derive(Debug, PartialEq)]
pub enum VRRPv2Type {
    VRRPv2Advertisement,
}

#[derive(Debug, PartialEq)]
pub enum VRRPv2AuthType {
    VRRPv2AuthNoAuth = 0x00,
    VRRPv2AuthReserved1 = 0x01,
    VRRPv2AuthReserved2 = 0x02,
}

#[derive(Debug, PartialEq)]
pub struct VRRPv2 {
    pub version: VRRPVersion,
    pub type_: VRRPv2Type,
    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>,
}

fn two_nibbles(input: &[u8]) -> IResult<&[u8], (u8, u8)> {
    bits::<_, _, Error<(&[u8], usize)>, _, _>(tuple((take(4usize), take(4usize))))(input)
}

fn parse_version_type(input: &[u8]) -> IResult<&[u8], (VRRPVersion, VRRPv2Type)> {
    let (input, pair) = two_nibbles(input)?;
    match pair {
        (VRRP_REQUIRED_VERSION, VRRP_REQUIRED_TYPE) => {
            Ok((input, (VRRPVersion::V2, VRRPv2Type::VRRPv2Advertisement)))
        }
        _ => Err(Err::Error(Error::new(input, ErrorKind::Alt))),
    }
}

fn parse_auth_type(input: &[u8]) -> IResult<&[u8], VRRPv2AuthType> {
    map_res(u8, |auth_type| {
        Ok(match auth_type {
            0 => VRRPv2AuthType::VRRPv2AuthNoAuth,
            1 => VRRPv2AuthType::VRRPv2AuthReserved1,
            2 => VRRPv2AuthType::VRRPv2AuthReserved2,
            _ => return Err(Err::Error(Error::new(input, ErrorKind::Alt))),
        })
    })(input)
}

// Nightly has a nicer array_chunks API to express it more succinctly.
// let mut chunks = bytes.array_chunks(2);
// let mut sum = chunks.map(u16::from_ne_bytes).map(|b| b as u32).sum::<u32>();
// // handle the remainder
// if let Some([b]) = chunks.remainder() {
//     sum += *b as u32
// }

// Shadowing can be used to avoid `mut`...
// let sum =...;
// let sum = (sum & 0xffff) + (sum >> 16);
// let sum = (sum & 0xffff) + (sum >> 16);
// manually un-rolling while loop since it's needed atmost twice for an u32.
fn validate_checksum(bytes: &[u8]) -> bool {
    let mut sum: u32 = bytes.chunks(2).fold(0, |acc: u32, x| {
        acc + u32::from(u16::from_ne_bytes(x.try_into().unwrap()))
    });
    while (sum >> 16) > 0 {
        sum = (sum & 0xffff) + (sum >> 16);
    }
    let checksum = !(sum as u16);
    checksum == 0
}

fn parse(input: &[u8]) -> IResult<&[u8], VRRPv2> {
    if !validate_checksum(input) {
        return Err(Err::Error(Error::new(input, ErrorKind::Alt)));
    }
    let (input, (version, type_)) = parse_version_type(input)?;
    let (input, virtual_router_id) = u8(input)?;
    let (input, priority) = u8(input)?;
    let (input, count_ip_addrs) = u8(input)?;
    let (input, auth_type) = parse_auth_type(input)?;
    let (input, advertisement_interval) = u8(input)?;
    let (input, checksum) = be_u16(input)?;
    let (input, xs) = count(be_u32, usize::from(count_ip_addrs))(input)?;
    let ip_addrs = xs.into_iter().map(Ipv4Addr::from).collect();
    Ok((
        input,
        VRRPv2 {
            version,
            type_,
            virtual_router_id,
            priority,
            count_ip_addrs,
            auth_type,
            advertisement_interval,
            checksum,
            ip_addrs,
        },
    ))
}

pub fn from_bytes(bytes: &[u8]) -> Result<VRRPv2, VRRPv2Error> {
    match parse(bytes) {
        Ok((_, v)) => Ok(v),
        Err(e) => Err(e.into()),
    }
}

#[test]
fn test_standard_bytes() {
    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 got = from_bytes(&bytes).unwrap();
    let expected = VRRPv2 {
        version: VRRPVersion::V2,
        type_: VRRPv2Type::VRRPv2Advertisement,
        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!(got, expected);
}

#[test]
fn test_incomplete_bytes() {
    let bytes = [0x21, 0x01];
    let got = from_bytes(&bytes);
    assert_eq!(got.is_err(), true);
    assert_eq!(got.err(), Some(VRRPv2Error::VRRPv2ParseError));
}

#[test]
fn test_invalid_version_type() {
    let bytes = [
        0x00, 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 got = from_bytes(&bytes);
    assert_eq!(got.is_err(), true);
    assert_eq!(got.err(), Some(VRRPv2Error::VRRPv2ParseError));
}

#[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,
    ];
    let got = from_bytes(&bytes);
    assert_eq!(got.is_err(), true);
    assert_eq!(got.err(), Some(VRRPv2Error::VRRPv2ParseError));
}

#[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,
    ];
    let got = from_bytes(&bytes);
    assert_eq!(got.is_err(), true);
    assert_eq!(got.err(), Some(VRRPv2Error::VRRPv2ParseError));
}