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
// SPDX-License-Identifier: MIT

use anyhow::Context;
use std::net::IpAddr;

use crate::{constants, route::nlas::Nla};

use netlink_packet_utils::{
    nla::{NlaBuffer, NlasIterator},
    parsers::parse_ip,
    traits::{Emitable, Parseable},
    DecodeError,
};

bitflags! {
    #[non_exhaustive]
pub struct NextHopFlags: u8 {
        const RTNH_F_DEAD = constants::RTNH_F_DEAD;
        const RTNH_F_PERVASIVE = constants::RTNH_F_PERVASIVE;
        const RTNH_F_ONLINK = constants::RTNH_F_ONLINK;
        const RTNH_F_OFFLOAD = constants::RTNH_F_OFFLOAD;
        const RTNH_F_LINKDOWN = constants::RTNH_F_LINKDOWN;
        const RTNH_F_UNRESOLVED = constants::RTNH_F_UNRESOLVED;
    }
}

const PAYLOAD_OFFSET: usize = 8;

buffer!(NextHopBuffer {
    length: (u16, 0..2),
    flags: (u8, 2),
    hops: (u8, 3),
    interface_id: (u32, 4..8),
    payload: (slice, PAYLOAD_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 < PAYLOAD_OFFSET {
            return Err(format!(
                "invalid NextHopBuffer: length {len} < {PAYLOAD_OFFSET}"
            )
            .into());
        }
        if len < self.length() as usize {
            return Err(format!(
                "invalid NextHopBuffer: length {} < {}",
                len,
                8 + self.length()
            )
            .into());
        }
        Ok(())
    }
}

impl<'a, T: AsRef<[u8]> + ?Sized> NextHopBuffer<&'a T> {
    pub fn nlas(
        &self,
    ) -> impl Iterator<Item = Result<NlaBuffer<&'a [u8]>, DecodeError>> {
        NlasIterator::new(
            &self.payload()[..(self.length() as usize - PAYLOAD_OFFSET)],
        )
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub struct NextHop {
    /// Next-hop flags (see [`NextHopFlags`])
    pub flags: NextHopFlags,
    /// Next-hop priority
    pub hops: u8,
    /// Interface index for the next-hop
    pub interface_id: u32,
    /// Attributes
    pub nlas: Vec<Nla>,
}

impl<'a, T: AsRef<[u8]>> Parseable<NextHopBuffer<&'a T>> for NextHop {
    fn parse(buf: &NextHopBuffer<&T>) -> Result<NextHop, DecodeError> {
        let nlas = Vec::<Nla>::parse(
            &NextHopBuffer::new_checked(buf.buffer)
                .context("cannot parse route attributes in next-hop")?,
        )
        .context("cannot parse route attributes in next-hop")?;
        Ok(NextHop {
            flags: NextHopFlags::from_bits_truncate(buf.flags()),
            hops: buf.hops(),
            interface_id: buf.interface_id(),
            nlas,
        })
    }
}

impl<'a, T: AsRef<[u8]> + 'a> Parseable<NextHopBuffer<&'a T>> for Vec<Nla> {
    fn parse(buf: &NextHopBuffer<&'a T>) -> Result<Self, DecodeError> {
        let mut nlas = vec![];
        for nla_buf in buf.nlas() {
            nlas.push(Nla::parse(&nla_buf?)?);
        }
        Ok(nlas)
    }
}

impl Emitable for NextHop {
    fn buffer_len(&self) -> usize {
        // len, flags, hops and interface id fields
        PAYLOAD_OFFSET + self.nlas.as_slice().buffer_len()
    }

    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);
        self.nlas.as_slice().emit(nh_buffer.payload_mut())
    }
}

impl NextHop {
    /// Gateway address (it is actually encoded as an `RTA_GATEWAY` nla)
    pub fn gateway(&self) -> Option<IpAddr> {
        self.nlas.iter().find_map(|nla| {
            if let Nla::Gateway(ip) = nla {
                parse_ip(ip).ok()
            } else {
                None
            }
        })
    }
}