-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_wireguard.c
418 lines (316 loc) · 9.97 KB
/
container_wireguard.c
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <fcntl.h>
#include <inttypes.h>
#include <sched.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/capability.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "capabilities.h"
#include "subprocess.h"
#include "validation.h"
#include "config.h"
#include "container_wireguard.h"
#define WG_KEY_SIZE 44l
/** Validate the pid, net and host inputs each command has.
*
* This assumes that there are at least three arguments, do check that first.
**/
static void cwg_validate_pid_net_host(char * argv[]) {
int value = -1;
if (validate_number(7, argv[0], NULL)) {
fprintf(stderr, "Invalid network namespace PID\n");
goto exit_usage;
}
if (validate_number(7, argv[1], NULL)) {
fprintf(stderr, "Invalid network number\n");
goto exit_usage;
}
value = atoi(argv[0]);
if ((value < 0) || ((1 << 23) <= value)) {
fprintf(stderr, "Network number out of range [0, %d]\n", (1 << 23) - 1);
goto exit_usage;
}
if (validate_number(1, argv[2], NULL)) {
fprintf(stderr, "Invalid host number\n");
goto exit_usage;
}
value = atoi(argv[2]);
if ((value < 0) || (1 < value)) {
fprintf(stderr, "Host number must be 0 or 1\n");
goto exit_usage;
}
return;
exit_usage:
fprintf(stderr, "Usage: " SYNOPSIS_CWG_CREATE);
exit(EXIT_FAILURE);
}
/** Compute the device name from net and host.
*
* Caller owns the returned buffer and needs to free() it. Returns NULL on
* error.
*/
static const char * cwg_device_name(char * argv[]) {
int net = atoi(argv[1]);
int host = atoi(argv[2]);
const size_t dev_buf_size = strlen(CWG_PREFIX) + 11;
char * dev = (char *)malloc(dev_buf_size);
if (!dev) return NULL;
snprintf(dev, dev_buf_size, "%s-%u-%u", CWG_PREFIX, net, host);
return dev;
}
/** Compute network IP. */
static uint32_t cwg_network_ip(char * argv[]) {
int net = atoi(argv[1]);
return (10u << 24) | (net << 1);
}
/** Compute host IP. */
static uint32_t cwg_host_ip(char * argv[]) {
int host = atoi(argv[2]);
return cwg_network_ip(argv) | host;
}
/** Compute network IP/netmask and return as dotted-quad/nm.
*
* Caller owns the returned buffer and needs to free() it. Returns NULL on
* error.
*/
static const char * cwg_network_ip_nm(char * argv[]) {
uint32_t ip = cwg_network_ip(argv);
char * ips = (char *)malloc(19);
if (!ips) return NULL;
snprintf(
ips, 19, "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32"/31",
ip >> 24, ip >> 16 & 0xff, ip >> 8 & 0xff, ip & 0xff);
return ips;
}
/** Compute device IP address and return as dotted-quad.
*
* Caller owns the returned buffer and needs to free() it. Returns NULL on
* error.
*/
static const char * cwg_device_ip(char * argv[]) {
uint32_t ip = cwg_host_ip(argv);
char * ips = (char *)malloc(16);
if (!ips) return NULL;
snprintf(
ips, 16, "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32,
ip >> 24, ip >> 16 & 0xff, ip >> 8 & 0xff, ip & 0xff);
return ips;
}
/** Switch to the target ns for the device.
*
* Returns 0 on success, 1 on failure.
*
* */
static int cwg_set_ns(const char * netns_pid) {
char netns_path[32];
snprintf(netns_path, 32, "/proc/%s/ns/net", netns_pid);
enable_cap(CAP_SYS_PTRACE);
int netns_fd = open(netns_path, O_RDONLY | O_NONBLOCK);
disable_cap(CAP_SYS_PTRACE);
if (netns_fd == -1) {
fprintf(stderr, "When opening %s\n", netns_path);
perror("Could not open network namespace\n");
goto exit_fail;
}
enable_cap(CAP_SYS_ADMIN);
int err = setns(netns_fd, CLONE_NEWNET);
disable_cap(CAP_SYS_ADMIN);
if (err) {
fprintf(stderr, "Could not enter namespace\n");
perror(NULL);
goto exit_fd;
}
close(netns_fd);
return 0;
exit_fd:
close(netns_fd);
exit_fail:
return 1;
}
/** Validate the input for the cwg_create command. */
static void cwg_create_validate(int argc, char * argv[]) {
int value = -1;
if (argc != 4) {
fprintf(stderr, "Incorrect number of command line arguments\n");
goto exit_usage;
}
cwg_validate_pid_net_host(argv);
if (validate_number(5, argv[3], NULL)) {
fprintf(stderr, "Invalid listen port\n");
goto exit_usage;
}
value = atoi(argv[3]);
if ((value < 1) || (65536 <= value)) {
fprintf(stderr, "Port number of out of range [1, 65535]\n");
goto exit_usage;
}
return;
exit_usage:
fprintf(stderr, "Usage: " SYNOPSIS_CWG_CREATE);
exit(EXIT_FAILURE);
}
int cwg_create(int argc, char * argv[]) {
const char * private_key = NULL, * public_key = NULL;
ssize_t public_key_size = 0l, private_key_size = 0l;
// get inputs
cwg_create_validate(argc, argv);
const char * netns_pid = argv[0];
const char * dev = cwg_device_name(argv);
if (!dev) goto exit_fail;
const char * ips = cwg_device_ip(argv);
if (!ips) goto exit_dev;
const char * vpn_ip_nm = cwg_network_ip_nm(argv);
if (!vpn_ip_nm) goto exit_ips;
const char * port = argv[3];
// create endpoint
const char * const genkey_args[] = { WG, "genkey", NULL };
if (run_check(
WG, genkey_args, NULL, NULL, 0l,
&private_key, &private_key_size)) {
fprintf(stderr, "Error generating private key\n");
goto exit_vpn_ip_nm;
}
const char * const pubkey_args[] = { WG, "pubkey", NULL };
if (run_check(
WG, pubkey_args, NULL, private_key, WG_KEY_SIZE,
&public_key, &public_key_size)) {
fprintf(stderr, "Error calculating public key\n");
goto exit_private_key;
}
const char * const create_dev_args[] = {
IP, "link", "add", dev, "type", "wireguard", NULL };
if (run_check2(IP, create_dev_args)) {
fprintf(stderr, "Error creating device\n");
goto exit_public_key;
}
const char * const move_dev_args[] = {
IP, "link", "set", dev, "netns", netns_pid, NULL };
if (run_check2(IP, move_dev_args)) {
fprintf(stderr, "Error moving device into namespace\n");
goto exit_if;
}
if (cwg_set_ns(netns_pid))
goto exit_if;
const char * const set_addr_args[] = {
IP, "addr", "add", ips, "dev", dev, NULL };
if (run_check2(IP, set_addr_args)) {
fprintf(stderr, "Error setting IP address\n");
goto exit_if;
}
const char * const set_key_args[] = {
WG, "set", dev, "listen-port", port, "private-key", "/dev/stdin",
NULL };
if (run_check(
WG, set_key_args, NULL, private_key, WG_KEY_SIZE,
NULL, NULL)) {
fprintf(stderr, "Error setting port and key\n");
goto exit_if;
}
const char * const ifup_args[] = { IP, "link", "set", dev, "up", NULL };
if (run_check2(IP, ifup_args)) {
fprintf(stderr, "Error bringing up interface\n");
goto exit_if;
}
const char * const route_args[] = {
IP, "route", "add", vpn_ip_nm, "dev", dev, NULL };
if (run_check2(IP, route_args)) {
fprintf(stderr, "Error adding route\n");
goto exit_if;
}
// produce output
printf("%s\n", public_key);
// clean up
explicit_bzero((void*)public_key, public_key_size);
free((void*)public_key);
explicit_bzero((void*)private_key, private_key_size);
free((void*)private_key);
free((void*)vpn_ip_nm);
free((void*)ips);
free((void*)dev);
return EXIT_SUCCESS;
exit_if:;
const char * const destroy_dev_args[] = { IP, "link", "delete", dev, NULL };
run_check2(IP, destroy_dev_args);
exit_public_key:
explicit_bzero((void*)public_key, public_key_size);
free((void*)public_key);
exit_private_key:
explicit_bzero((void*)private_key, private_key_size);
free((void*)private_key);
exit_vpn_ip_nm:
free((void*)vpn_ip_nm);
exit_ips:
free((void*)ips);
exit_dev:
free((void*)dev);
exit_fail:
return EXIT_FAILURE;
}
/** Validate the input for the cwg_connect command. */
static void cwg_connect_validate(int argc, char * argv[]) {
if (argc != 5) {
fprintf(stderr, "Incorrect number of command line arguments\n");
goto exit_usage;
}
cwg_validate_pid_net_host(argv);
if (validate_endpoint(argv[3], NULL)) {
fprintf(stderr, "Invalid endpoint\n");
goto exit_usage;
}
if (validate_wireguard_key(argv[4], NULL)) {
fprintf(stderr, "Invalid key\n");
goto exit_usage;
}
return;
exit_usage:
fprintf(stderr, "Usage: " SYNOPSIS_CWG_CONNECT);
exit(EXIT_FAILURE);
}
int cwg_connect(int argc, char * argv[]) {
// get inputs
cwg_connect_validate(argc, argv);
const char * netns_pid = argv[0];
const char * dev = cwg_device_name(argv);
if (!dev) goto exit_fail;
const char * vpn_ip_nm = cwg_network_ip_nm(argv);
if (!vpn_ip_nm) goto exit_dev;
const char * peer_endpoint = argv[3];
const char * peer_key = argv[4];
// add peer
if (cwg_set_ns(netns_pid))
goto exit_ip;
const char * const add_args[] = {
WG, "set", dev, "peer", peer_key, "allowed-ips", vpn_ip_nm, "endpoint",
peer_endpoint, NULL };
if (run_check2(WG, add_args))
goto exit_ip;
return EXIT_SUCCESS;
exit_ip:
free((void*)vpn_ip_nm);
exit_dev:
free((void*)dev);
exit_fail:
return EXIT_FAILURE;
}
int cwg_destroy(int argc, char * argv[]) {
if (argc != 3) {
fprintf(stderr, "Incorrect number of command line arguments\n");
goto exit_usage;
}
cwg_validate_pid_net_host(argv);
const char * netns_pid = argv[0];
cwg_set_ns(netns_pid);
const char * dev = cwg_device_name(argv);
const char * const delete_args[] = { IP, "link", "delete", dev, NULL };
int err = run_check2(IP, delete_args);
free((void*)dev);
if (err) return EXIT_FAILURE;
return EXIT_SUCCESS;
exit_usage:
fprintf(stderr, "Usage: " SYNOPSIS_CWG_DESTROY);
return EXIT_FAILURE;
}