aboutsummaryrefslogtreecommitdiffstats
path: root/src/wireguard/router/route.rs
blob: a5560101c005f5038ff6edf867bc45458fd9143d (plain) (blame)
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
use super::ip::*;

// TODO: no_std alternatives
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use spin::RwLock;
use treebitmap::address::Address;
use treebitmap::IpLookupTable;
use zerocopy::LayoutVerified;

/* Functions for obtaining and validating "cryptokey" routes */

pub struct RoutingTable<T: Eq + Clone> {
    ipv4: RwLock<IpLookupTable<Ipv4Addr, T>>,
    ipv6: RwLock<IpLookupTable<Ipv6Addr, T>>,
}

impl<T: Eq + Clone> RoutingTable<T> {
    pub fn new() -> Self {
        RoutingTable {
            ipv4: RwLock::new(IpLookupTable::new()),
            ipv6: RwLock::new(IpLookupTable::new()),
        }
    }

    // collect keys mapping to the given value
    fn collect<A>(table: &IpLookupTable<A, T>, value: &T) -> Vec<(A, u32)>
    where
        A: Address,
    {
        let mut res = Vec::new();
        for (ip, cidr, v) in table.iter() {
            if v == value {
                res.push((ip, cidr))
            }
        }
        res
    }

    pub fn insert(&self, ip: IpAddr, cidr: u32, value: T) {
        match ip {
            IpAddr::V4(v4) => self.ipv4.write().insert(v4.mask(cidr), cidr, value),
            IpAddr::V6(v6) => self.ipv6.write().insert(v6.mask(cidr), cidr, value),
        };
    }

    pub fn list(&self, value: &T) -> Vec<(IpAddr, u32)> {
        let mut res = vec![];
        res.extend(
            Self::collect(&*self.ipv4.read(), value)
                .into_iter()
                .map(|(ip, cidr)| (IpAddr::V4(ip), cidr)),
        );
        res.extend(
            Self::collect(&*self.ipv6.read(), value)
                .into_iter()
                .map(|(ip, cidr)| (IpAddr::V6(ip), cidr)),
        );
        res
    }

    pub fn remove(&self, value: &T) {
        let mut v4 = self.ipv4.write();
        for (ip, cidr) in Self::collect(&*v4, value) {
            v4.remove(ip, cidr);
        }

        let mut v6 = self.ipv6.write();
        for (ip, cidr) in Self::collect(&*v6, value) {
            v6.remove(ip, cidr);
        }
    }

    #[inline(always)]
    pub fn get_route(&self, packet: &[u8]) -> Option<T> {
        match packet.get(0)? >> 4 {
            VERSION_IP4 => {
                // check length and cast to IPv4 header
                let (header, _): (LayoutVerified<&[u8], IPv4Header>, _) =
                    LayoutVerified::new_from_prefix(packet)?;

                log::trace!(
                    "router, get route for IPv4 destination: {:?}",
                    Ipv4Addr::from(header.f_destination)
                );

                // check IPv4 source address
                self.ipv4
                    .read()
                    .longest_match(Ipv4Addr::from(header.f_destination))
                    .and_then(|(_, _, p)| Some(p.clone()))
            }
            VERSION_IP6 => {
                // check length and cast to IPv6 header
                let (header, _): (LayoutVerified<&[u8], IPv6Header>, _) =
                    LayoutVerified::new_from_prefix(packet)?;

                log::trace!(
                    "router, get route for IPv6 destination: {:?}",
                    Ipv6Addr::from(header.f_destination)
                );

                // check IPv6 source address
                self.ipv6
                    .read()
                    .longest_match(Ipv6Addr::from(header.f_destination))
                    .and_then(|(_, _, p)| Some(p.clone()))
            }
            v => {
                log::trace!("router, invalid IP version {}", v);
                None
            }
        }
    }

    #[inline(always)]
    pub fn check_route(&self, peer: &T, packet: &[u8]) -> bool {
        match packet.get(0).map(|v| v >> 4) {
            Some(VERSION_IP4) => LayoutVerified::new_from_prefix(packet)
                .and_then(|(header, _): (LayoutVerified<&[u8], IPv4Header>, _)| {
                    self.ipv4
                        .read()
                        .longest_match(Ipv4Addr::from(header.f_source))
                        .map(|(_, _, p)| p == peer)
                })
                .is_some(),

            Some(VERSION_IP6) => LayoutVerified::new_from_prefix(packet)
                .and_then(|(header, _): (LayoutVerified<&[u8], IPv6Header>, _)| {
                    self.ipv6
                        .read()
                        .longest_match(Ipv6Addr::from(header.f_source))
                        .map(|(_, _, p)| p == peer)
                })
                .is_some(),
            _ => false,
        }
    }
}