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
#[macro_use]
extern crate log;
use crate::constants::*;
use anyhow::Context;
use netlink_packet_generic::{GenlFamily, GenlHeader};
use netlink_packet_utils::{nla::NlasIterator, traits::*, DecodeError};
use nlas::WgDeviceAttrs;
use std::convert::{TryFrom, TryInto};
pub mod constants;
pub mod nlas;
mod raw;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WireguardCmd {
GetDevice,
SetDevice,
}
impl From<WireguardCmd> for u8 {
fn from(cmd: WireguardCmd) -> Self {
use WireguardCmd::*;
match cmd {
GetDevice => WG_CMD_GET_DEVICE,
SetDevice => WG_CMD_SET_DEVICE,
}
}
}
impl TryFrom<u8> for WireguardCmd {
type Error = DecodeError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
use WireguardCmd::*;
Ok(match value {
WG_CMD_GET_DEVICE => GetDevice,
WG_CMD_SET_DEVICE => SetDevice,
cmd => {
return Err(DecodeError::from(format!(
"Unknown wireguard command: {}",
cmd
)))
}
})
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Wireguard {
pub cmd: WireguardCmd,
pub nlas: Vec<nlas::WgDeviceAttrs>,
}
impl GenlFamily for Wireguard {
fn family_name() -> &'static str {
"wireguard"
}
fn version(&self) -> u8 {
1
}
fn command(&self) -> u8 {
self.cmd.into()
}
}
impl Emitable for Wireguard {
fn emit(&self, buffer: &mut [u8]) {
self.nlas.as_slice().emit(buffer)
}
fn buffer_len(&self) -> usize {
self.nlas.as_slice().buffer_len()
}
}
impl ParseableParametrized<[u8], GenlHeader> for Wireguard {
fn parse_with_param(buf: &[u8], header: GenlHeader) -> Result<Self, DecodeError> {
Ok(Self {
cmd: header.cmd.try_into()?,
nlas: parse_nlas(buf)?,
})
}
}
fn parse_nlas(buf: &[u8]) -> Result<Vec<WgDeviceAttrs>, DecodeError> {
let mut nlas = Vec::new();
let error_msg = "failed to parse message attributes";
for nla in NlasIterator::new(buf) {
let nla = &nla.context(error_msg)?;
let parsed = WgDeviceAttrs::parse(nla).context(error_msg)?;
nlas.push(parsed);
}
Ok(nlas)
}
#[cfg(test)]
mod test {
use netlink_packet_core::{NetlinkMessage, NLM_F_ACK, NLM_F_REQUEST};
use netlink_packet_generic::GenlMessage;
use crate::nlas::{WgAllowedIp, WgAllowedIpAttrs, WgPeer, WgPeerAttrs};
use super::*;
const KNOWN_VALID_PACKET: &[u8] = &[
0x74, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x05, 0x00, 0x38, 0x24, 0xd6, 0x61, 0x00, 0x00, 0x00,
0x00, 0x01, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x02, 0x00, 0x66, 0x72, 0x61, 0x6e, 0x64, 0x73,
0x00, 0x00, 0x54, 0x00, 0x08, 0x80, 0x50, 0x00, 0x00, 0x80, 0x24, 0x00, 0x01, 0x00, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x08, 0x00, 0x03, 0x00, 0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x09, 0x80, 0x1c, 0x00,
0x00, 0x80, 0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x02, 0x00, 0x0a,
0x0a, 0x0a, 0x0a, 0x05, 0x00, 0x03, 0x00, 0x1e, 0x00, 0x00, 0x00,
];
#[test]
fn test_parse_known_valid_packet() {
NetlinkMessage::<GenlMessage<Wireguard>>::deserialize(KNOWN_VALID_PACKET).unwrap();
}
#[test]
fn test_serialize_then_deserialize() {
let genlmsg: GenlMessage<Wireguard> = GenlMessage::from_payload(Wireguard {
cmd: WireguardCmd::SetDevice,
nlas: vec![
WgDeviceAttrs::IfName("wg0".to_string()),
WgDeviceAttrs::PrivateKey([0xaa; 32]),
WgDeviceAttrs::Peers(vec![
WgPeer(vec![
WgPeerAttrs::PublicKey([0x01; 32]),
WgPeerAttrs::PresharedKey([0x01; 32]),
WgPeerAttrs::AllowedIps(vec![WgAllowedIp(vec![
WgAllowedIpAttrs::IpAddr([10, 0, 0, 0].into()),
WgAllowedIpAttrs::Cidr(24),
WgAllowedIpAttrs::Family(AF_INET),
])]),
]),
WgPeer(vec![
WgPeerAttrs::PublicKey([0x02; 32]),
WgPeerAttrs::PresharedKey([0x01; 32]),
WgPeerAttrs::AllowedIps(vec![WgAllowedIp(vec![
WgAllowedIpAttrs::IpAddr([10, 0, 1, 0].into()),
WgAllowedIpAttrs::Cidr(24),
WgAllowedIpAttrs::Family(AF_INET),
])]),
]),
]),
],
});
let mut nlmsg = NetlinkMessage::from(genlmsg);
nlmsg.header.flags = NLM_F_REQUEST | NLM_F_ACK;
nlmsg.finalize();
let mut buf = [0; 4096];
nlmsg.serialize(&mut buf);
let len = nlmsg.buffer_len();
NetlinkMessage::<GenlMessage<Wireguard>>::deserialize(&buf[..len]).unwrap();
}
}