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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright (c) 2019 Cloudflare, Inc. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
use super::dev_lock::LockReadGuard;
use super::drop_privileges::get_saved_ids;
use super::{AllowedIP, Device, Error, SocketAddr};
use crate::device::Action;
use crate::serialization::KeyBytes;
use crate::x25519;
use hex::encode as encode_hex;
use libc::*;
use std::fs::{create_dir, remove_file};
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::os::unix::io::{AsRawFd, FromRawFd};
use std::os::unix::net::{UnixListener, UnixStream};
use std::sync::atomic::Ordering;
const SOCK_DIR: &str = "/var/run/wireguard/";
fn create_sock_dir() {
let _ = create_dir(SOCK_DIR); // Create the directory if it does not exist
if let Ok((saved_uid, saved_gid)) = get_saved_ids() {
unsafe {
let c_path = std::ffi::CString::new(SOCK_DIR).unwrap();
// The directory is under the root user, but we want to be able to
// delete the files there when we exit, so we need to change the owner
chown(
c_path.as_bytes_with_nul().as_ptr() as _,
saved_uid,
saved_gid,
);
}
}
}
impl Device {
/// Register the api handler for this Device. The api handler receives stream connections on a Unix socket
/// with a known path: /var/run/wireguard/{tun_name}.sock.
pub fn register_api_handler(&mut self) -> Result<(), Error> {
let path = format!("{}/{}.sock", SOCK_DIR, self.iface.name()?);
create_sock_dir();
let _ = remove_file(&path); // Attempt to remove the socket if already exists
let api_listener = UnixListener::bind(&path).map_err(Error::ApiSocket)?; // Bind a new socket to the path
self.cleanup_paths.push(path.clone());
self.queue.new_event(
api_listener.as_raw_fd(),
Box::new(move |d, _| {
// This is the closure that listens on the api unix socket
let (api_conn, _) = match api_listener.accept() {
Ok(conn) => conn,
_ => return Action::Continue,
};
let mut reader = BufReader::new(&api_conn);
let mut writer = BufWriter::new(&api_conn);
let mut cmd = String::new();
if reader.read_line(&mut cmd).is_ok() {
cmd.pop(); // pop the new line character
let status = match cmd.as_ref() {
// Only two commands are legal according to the protocol, get=1 and set=1.
"get=1" => api_get(&mut writer, d),
"set=1" => api_set(&mut reader, d),
_ => EIO,
};
// The protocol requires to return an error code as the response, or zero on success
writeln!(writer, "errno={}\n", status).ok();
}
Action::Continue // Indicates the worker thread should continue as normal
}),
)?;
self.register_monitor(path)?;
self.register_api_signal_handlers()
}
pub fn register_api_fd(&mut self, fd: i32) -> Result<(), Error> {
let io_file = unsafe { UnixStream::from_raw_fd(fd) };
self.queue.new_event(
io_file.as_raw_fd(),
Box::new(move |d, _| {
// This is the closure that listens on the api file descriptor
let mut reader = BufReader::new(&io_file);
let mut writer = BufWriter::new(&io_file);
let mut cmd = String::new();
if reader.read_line(&mut cmd).is_ok() {
cmd.pop(); // pop the new line character
let status = match cmd.as_ref() {
// Only two commands are legal according to the protocol, get=1 and set=1.
"get=1" => api_get(&mut writer, d),
"set=1" => api_set(&mut reader, d),
_ => EIO,
};
// The protocol requires to return an error code as the response, or zero on success
writeln!(writer, "errno={}\n", status).ok();
} else {
// The remote side is likely closed; we should trigger an exit.
d.trigger_exit();
return Action::Exit;
}
Action::Continue // Indicates the worker thread should continue as normal
}),
)?;
Ok(())
}
fn register_monitor(&self, path: String) -> Result<(), Error> {
self.queue.new_periodic_event(
Box::new(move |d, _| {
// This is not a very nice hack to detect if the control socket was removed
// and exiting nicely as a result. We check every 3 seconds in a loop if the
// file was deleted by stating it.
// The problem is that on linux inotify can be used quite beautifully to detect
// deletion, and kqueue EVFILT_VNODE can be used for the same purpose, but that
// will require introducing new events, for no measurable benefit.
// TODO: Could this be an issue if we restart the service too quickly?
let path = std::path::Path::new(&path);
if !path.exists() {
d.trigger_exit();
return Action::Exit;
}
// Periodically read the mtu of the interface in case it changes
if let Ok(mtu) = d.iface.mtu() {
d.mtu.store(mtu, Ordering::Relaxed);
}
Action::Continue
}),
std::time::Duration::from_millis(1000),
)?;
Ok(())
}
fn register_api_signal_handlers(&self) -> Result<(), Error> {
self.queue
.new_signal_event(SIGINT, Box::new(move |_, _| Action::Exit))?;
self.queue
.new_signal_event(SIGTERM, Box::new(move |_, _| Action::Exit))?;
Ok(())
}
}
#[allow(unused_must_use)]
fn api_get(writer: &mut BufWriter<&UnixStream>, d: &Device) -> i32 {
// get command requires an empty line, but there is no reason to be religious about it
if let Some(ref k) = d.key_pair {
writeln!(writer, "own_public_key={}", encode_hex(k.1.as_bytes()));
}
if d.listen_port != 0 {
writeln!(writer, "listen_port={}", d.listen_port);
}
if let Some(fwmark) = d.fwmark {
writeln!(writer, "fwmark={}", fwmark);
}
for (k, p) in d.peers.iter() {
let p = p.lock();
writeln!(writer, "public_key={}", encode_hex(k.as_bytes()));
if let Some(ref key) = p.preshared_key() {
writeln!(writer, "preshared_key={}", encode_hex(key));
}
if let Some(keepalive) = p.persistent_keepalive() {
writeln!(writer, "persistent_keepalive_interval={}", keepalive);
}
if let Some(ref addr) = p.endpoint().addr {
writeln!(writer, "endpoint={}", addr);
}
for (ip, cidr) in p.allowed_ips() {
writeln!(writer, "allowed_ip={}/{}", ip, cidr);
}
if let Some(time) = p.time_since_last_handshake() {
writeln!(writer, "last_handshake_time_sec={}", time.as_secs());
writeln!(writer, "last_handshake_time_nsec={}", time.subsec_nanos());
}
let (_, tx_bytes, rx_bytes, ..) = p.tunnel.stats();
writeln!(writer, "rx_bytes={}", rx_bytes);
writeln!(writer, "tx_bytes={}", tx_bytes);
}
0
}
fn api_set(reader: &mut BufReader<&UnixStream>, d: &mut LockReadGuard<Device>) -> i32 {
d.try_writeable(
|device| device.trigger_yield(),
|device| {
device.cancel_yield();
let mut cmd = String::new();
while reader.read_line(&mut cmd).is_ok() {
cmd.pop(); // remove newline if any
if cmd.is_empty() {
return 0; // Done
}
{
let parsed_cmd: Vec<&str> = cmd.split('=').collect();
if parsed_cmd.len() != 2 {
return EPROTO;
}
let (key, val) = (parsed_cmd[0], parsed_cmd[1]);
match key {
"private_key" => match val.parse::<KeyBytes>() {
Ok(key_bytes) => {
device.set_key(x25519::StaticSecret::from(key_bytes.0))
}
Err(_) => return EINVAL,
},
"listen_port" => match val.parse::<u16>() {
Ok(port) => match device.open_listen_socket(port) {
Ok(()) => {}
Err(_) => return EADDRINUSE,
},
Err(_) => return EINVAL,
},
#[cfg(any(
target_os = "android",
target_os = "fuchsia",
target_os = "linux"
))]
"fwmark" => match val.parse::<u32>() {
Ok(mark) => match device.set_fwmark(mark) {
Ok(()) => {}
Err(_) => return EADDRINUSE,
},
Err(_) => return EINVAL,
},
"replace_peers" => match val.parse::<bool>() {
Ok(true) => device.clear_peers(),
Ok(false) => {}
Err(_) => return EINVAL,
},
"public_key" => match val.parse::<KeyBytes>() {
// Indicates a new peer section
Ok(key_bytes) => {
return api_set_peer(
reader,
device,
x25519::PublicKey::from(key_bytes.0),
)
}
Err(_) => return EINVAL,
},
_ => return EINVAL,
}
}
cmd.clear();
}
0
},
)
.unwrap_or(EIO)
}
fn api_set_peer(
reader: &mut BufReader<&UnixStream>,
d: &mut Device,
pub_key: x25519::PublicKey,
) -> i32 {
let mut cmd = String::new();
let mut remove = false;
let mut replace_ips = false;
let mut endpoint = None;
let mut keepalive = None;
let mut public_key = pub_key;
let mut preshared_key = None;
let mut allowed_ips: Vec<AllowedIP> = vec![];
while reader.read_line(&mut cmd).is_ok() {
cmd.pop(); // remove newline if any
if cmd.is_empty() {
d.update_peer(
public_key,
remove,
replace_ips,
endpoint,
allowed_ips.as_slice(),
keepalive,
preshared_key,
);
allowed_ips.clear(); //clear the vector content after update
return 0; // Done
}
{
let parsed_cmd: Vec<&str> = cmd.splitn(2, '=').collect();
if parsed_cmd.len() != 2 {
return EPROTO;
}
let (key, val) = (parsed_cmd[0], parsed_cmd[1]);
match key {
"remove" => match val.parse::<bool>() {
Ok(true) => remove = true,
Ok(false) => remove = false,
Err(_) => return EINVAL,
},
"preshared_key" => match val.parse::<KeyBytes>() {
Ok(key_bytes) => preshared_key = Some(key_bytes.0),
Err(_) => return EINVAL,
},
"endpoint" => match val.parse::<SocketAddr>() {
Ok(addr) => endpoint = Some(addr),
Err(_) => return EINVAL,
},
"persistent_keepalive_interval" => match val.parse::<u16>() {
Ok(interval) => keepalive = Some(interval),
Err(_) => return EINVAL,
},
"replace_allowed_ips" => match val.parse::<bool>() {
Ok(true) => replace_ips = true,
Ok(false) => replace_ips = false,
Err(_) => return EINVAL,
},
"allowed_ip" => match val.parse::<AllowedIP>() {
Ok(ip) => allowed_ips.push(ip),
Err(_) => return EINVAL,
},
"public_key" => {
// Indicates a new peer section. Commit changes for current peer, and continue to next peer
d.update_peer(
public_key,
remove,
replace_ips,
endpoint,
allowed_ips.as_slice(),
keepalive,
preshared_key,
);
allowed_ips.clear(); //clear the vector content after update
match val.parse::<KeyBytes>() {
Ok(key_bytes) => public_key = key_bytes.0.into(),
Err(_) => return EINVAL,
}
}
"protocol_version" => match val.parse::<u32>() {
Ok(1) => {} // Only version 1 is legal
_ => return EINVAL,
},
_ => return EINVAL,
}
}
cmd.clear();
}
0
}