-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwg_prefix_provider.c
472 lines (413 loc) · 11 KB
/
wg_prefix_provider.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>
#include <poll.h>
#include "wireguard.h"
#include "prefix_table.h"
#define timediff(a, b, result) \
do { \
result.tv_sec = a.tv_sec - b.tv_sec; \
result.tv_nsec = a.tv_nsec - b.tv_nsec; \
if (result.tv_nsec < 0) { \
--result.tv_sec; \
result.tv_nsec += 1000000000L; \
} \
} while (0)
char *wg_device_name;
struct in6_addr prefix;
uint8_t prefix_len;
struct prefix_table prefix_tab;
bool run = true;
void signal_handler(int sig) {
if( sig != SIGTERM )
return;
run = false;
}
void print_in6_addr(const struct in6_addr * addr) {
char buffer[40];
inet_ntop(AF_INET6, addr, buffer, 40);
printf("%s", buffer);
}
void print_key(wg_key key) {
wg_key_b64_string b64key;
wg_key_to_base64(b64key, key);
printf("%s", b64key);
}
void get_peer(wg_key key, struct wg_allowedip **allowed_ips, const struct in6_addr * addr) {
wg_device *device;
if(wg_get_device(&device, wg_device_name) < 0) {
printf("Unable to get wg device\n");
return;
}
wg_peer *peer;
wg_allowedip *allowed_ip;
bool found = false;
wg_for_each_peer(device, peer) {
if( !(peer->flags & WGPEER_HAS_PUBLIC_KEY) )
continue;
wg_for_each_allowedip(peer, allowed_ip) {
if( allowed_ip->family != AF_INET6 )
continue;
if( addr_in_prefix( addr, &allowed_ip->ip6, 128 ) ) {
found = true;
memcpy( key, peer->public_key, 32 );
break;
}
}
if( found ) {
wg_for_each_allowedip(peer, allowed_ip) {
struct wg_allowedip *new_allowedip = calloc(1, sizeof(wg_allowedip));
if (!new_allowedip)
break;
memcpy( new_allowedip, allowed_ip, sizeof(wg_allowedip) );
if(!(*allowed_ips)) {
(*allowed_ips) = new_allowedip;
(*allowed_ips)->next_allowedip = NULL;
} else {
new_allowedip->next_allowedip = (*allowed_ips);
(*allowed_ips) = new_allowedip;
}
}
break;
}
}
wg_free_device(device);
}
struct in6_addr get_prefix(const struct in6_addr * addr) {
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
struct in6_addr out_addr;
wg_key key;
struct wg_allowedip *allowed_ips = NULL;
get_peer(key, &allowed_ips, addr);
if( !allowed_ips )
return out_addr;
bool found = false;
for( struct wg_allowedip *aip = allowed_ips; aip; aip = aip->next_allowedip ) {
if( aip->family != AF_INET6 )
continue;
if( addr_in_prefix( &prefix, &aip->ip6, prefix_len ) ) {
out_addr = aip->ip6;
struct prefix_entry *entry;
if( !get_prefix_entry(&prefix_tab, &out_addr, &entry) ) {
add_prefix_entry(&prefix_tab, key, &out_addr, &now);
if( !get_prefix_entry(&prefix_tab, &out_addr, &entry) )
break;
}
entry->last_seen = now;
found = true;
break;
}
}
if( !found ) {
uint64_t net = 0;
uint8_t net_bits = 64-prefix_len;
for( uint8_t bit = 0; bit < net_bits; bit++ ) {
uint8_t byte = bit/8;
uint8_t mask = 1 << (7-(bit%8));
if( key[byte+1] & mask )
net |= 1 << bit;
}
bool found_free = false;
while( !found_free ) {
out_addr = prefix;
for( uint8_t bit = 0; bit < net_bits; bit++ ) {
uint8_t byte = (bit + prefix_len)/8;
uint8_t mask = 1 << (7-((bit + prefix_len)%8));
if( net & (1 << bit) )
out_addr.s6_addr[byte] |= mask;
}
struct prefix_entry *entry;
if( get_prefix_entry(&prefix_tab, &out_addr, &entry) ) {
net++;
if( net >> net_bits )
net = 0;
}
else {
found_free = true;
}
}
{
wg_device device = {0};
wg_peer peer = {0};
wg_allowedip allowed_ip = {0};
strncpy(device.name, wg_device_name, IFNAMSIZ - 1);
device.name[IFNAMSIZ-1] = '\0';
device.first_peer = &peer;
device.last_peer = &peer;
peer.flags = WGPEER_HAS_PUBLIC_KEY | WGPEER_REPLACE_ALLOWEDIPS;
memcpy(peer.public_key, key, 32);
peer.first_allowedip = &allowed_ip;
allowed_ip.family = AF_INET6;
allowed_ip.ip6 = out_addr;
allowed_ip.cidr = 64;
allowed_ip.next_allowedip = allowed_ips;
for( struct wg_allowedip *aip = allowed_ips; aip; aip = aip->next_allowedip ) {
peer.last_allowedip = aip;
}
if(wg_set_device(&device) < 0) {
perror("Unable to set device");
}
else {
add_prefix_entry(&prefix_tab, key, &out_addr, &now);
printf("new prefix for ");
print_in6_addr(addr);
printf(" with key ");
print_key(key);
printf(": ");
print_in6_addr(&out_addr);
printf("\n");
}
}
}
{
struct wg_allowedip *aip = allowed_ips, *next;
while( aip ) {
next = aip->next_allowedip;
free(aip);
aip = next;
}
}
return out_addr;
}
void * flush_stale_peers(void *) {
struct timespec time_to_sleep = { .tv_sec = 1, .tv_nsec = 0 };
struct timespec last_run;
struct timespec now;
struct timespec diff;
clock_gettime(CLOCK_MONOTONIC, &last_run);
while( run ) {
do
{
nanosleep(&time_to_sleep, NULL);
clock_gettime(CLOCK_MONOTONIC, &now);
timediff(now, last_run, diff);
} while( run && diff.tv_sec < 300 );
clock_gettime(CLOCK_MONOTONIC, &last_run);
printf("flushing stale prefixes...\n");
clock_gettime(CLOCK_REALTIME, &now);
struct timespec then = now;
then.tv_sec -= (1*60*60);
struct prefix_entry *entry;
while( get_prefix_entry_older_than(&prefix_tab, &then, &entry) ) {
wg_peer out_peer = {0};
out_peer.flags = WGPEER_HAS_PUBLIC_KEY | WGPEER_REPLACE_ALLOWEDIPS;
memcpy(out_peer.public_key, entry->key, 32);
out_peer.first_allowedip = NULL;
out_peer.last_allowedip = NULL;
wg_device *device;
if(wg_get_device(&device, wg_device_name) < 0) {
printf("Unable to get wg device\n");
break;
}
wg_peer *peer;
wg_allowedip *allowed_ip;
wg_for_each_peer(device, peer) {
if( !(peer->flags & WGPEER_HAS_PUBLIC_KEY) )
continue;
if( memcmp(peer->public_key, entry->key, sizeof(wg_key)) )
continue;
wg_for_each_allowedip(peer, allowed_ip) {
if( allowed_ip->family == AF_INET6 && addr_in_prefix( &entry->addr, &allowed_ip->ip6, 64 ) ) {
continue;
}
struct wg_allowedip *new_allowedip = calloc(1, sizeof(wg_allowedip));
if (!new_allowedip)
break;
memcpy( new_allowedip, allowed_ip, sizeof(wg_allowedip) );
new_allowedip->next_allowedip = NULL;
if( !out_peer.first_allowedip )
out_peer.first_allowedip = new_allowedip;
if( out_peer.last_allowedip ) {
out_peer.last_allowedip->next_allowedip = new_allowedip;
}
out_peer.last_allowedip = new_allowedip;
}
}
wg_free_device(device);
wg_device out_device = {0};
strncpy(out_device.name, wg_device_name, IFNAMSIZ - 1);
out_device.name[IFNAMSIZ-1] = '\0';
out_device.first_peer = &out_peer;
out_device.last_peer = &out_peer;
printf("removing prefix ");
print_in6_addr(&entry->addr);
printf(" from key ");
print_key(entry->key);
printf("\n");
if(wg_set_device(&out_device) < 0) {
perror("Unable to set device");
}
else {
remove_prefix_entry(&prefix_tab, &entry->addr);
}
{
struct wg_allowedip *aip = out_peer.first_allowedip, *next;
while( aip ) {
next = aip->next_allowedip;
free(aip);
aip = next;
}
}
}
printf("done\n");
}
}
int main(int argc, char **argv) {
if( argc != 4 ) {
printf("usage: %s <wg interface> <port> <prefix>\n", argv[0]);
return 1;
}
wg_device_name = argv[1];
uint16_t port = atoi(argv[2]);
{
char * slash = strchr(argv[3], '/');
if( slash == NULL ) {
printf("%s has no /\n", argv[3]);
exit(1);
}
*slash = 0;
if( inet_pton( AF_INET6, argv[3], &prefix) <= 0 ) {
printf("wrong format: %s\n", argv[3]);
exit(1);
}
prefix_len = atoi(slash+1);
if(prefix_len >= 64) {
printf("prefix too small ( > 64 needed)\n");
exit(1);
}
}
// make output line buffered
setvbuf(stdout, NULL, _IOLBF, 0);
init_prefix_table(&prefix_tab, &prefix, prefix_len);
{
wg_device *device;
if(wg_get_device(&device, wg_device_name) < 0) {
printf("Unable to get wg device\n");
return 1;
}
wg_peer *peer;
wg_allowedip *allowed_ip;
wg_key key;
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
wg_for_each_peer(device, peer) {
wg_for_each_allowedip(peer, allowed_ip) {
if( allowed_ip->family != AF_INET6 )
continue;
if( addr_in_prefix( &prefix, &allowed_ip->ip6, prefix_len ) ) {
memcpy( key, peer->public_key, 32 );
add_prefix_entry(&prefix_tab, key, &allowed_ip->ip6, &now);
break;
}
}
}
wg_free_device(device);
}
int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if( sock < 0) {
perror("socket");
return 1;
}
int enable = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
perror("setsockopt(REUSEADDR)");
exit(1);
}
struct sockaddr_in6 bind_addr;
bind_addr.sin6_family = AF_INET6;
bind_addr.sin6_port = htons(port);
bind_addr.sin6_flowinfo = 0;
size_t wg_device_name_len = strnlen(wg_device_name, IFNAMSIZ);
struct ifaddrs *ifaddr;
if( getifaddrs(&ifaddr) != 0 ) {
perror("getifaddrs");
exit(1);
}
char buffer[40];
bool found_addr = false;
for( struct ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if( ifa->ifa_addr == NULL )
continue;
if( strcmp( wg_device_name, ifa->ifa_name ) != 0 )
continue;
if( ifa->ifa_addr->sa_family != AF_INET6 )
continue;
struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ifa->ifa_addr;
inet_ntop(AF_INET6, (struct in6_addr *) &addr->sin6_addr, buffer, 40);
if( strncmp( "fe80::", buffer, 6 ) == 0 ) {
bind_addr.sin6_addr = addr->sin6_addr;
found_addr = true;
break;
}
}
freeifaddrs(ifaddr);
if( !found_addr ) {
printf("did not find the fe80:: address of %s\n", wg_device_name);
exit(1);
}
size_t ifidx = if_nametoindex(wg_device_name);
if( ifidx == 0 ) {
perror("if_nametoindex");
exit(1);
}
bind_addr.sin6_scope_id = ifidx;
if( bind( sock, (struct sockaddr*) &bind_addr, sizeof(bind_addr) ) < 0 ) {
perror("bind");
exit(1);
}
if( listen( sock, 100 ) != 0 ) {
perror("listen");
exit(1);
}
pthread_t flush_thread;
{
int rc = pthread_create( &flush_thread, NULL, &flush_stale_peers, NULL );
if( rc ) {
printf("Could not create flush_stale_peers thread\n");
return 1;
}
}
struct pollfd pollfds[1];
pollfds[0].fd = sock;
pollfds[0].events = POLLIN;
signal(SIGTERM, signal_handler);
printf("running...\n");
while( run ) {
struct sockaddr_in6 addr;
unsigned int addr_len = sizeof( addr );
int rc = poll(pollfds, 1, 100);
if( rc < 0 ) {
perror("poll");
run = false;
break;
}
if( rc == 0 )
continue;
if( ! pollfds[0].revents & POLLIN )
continue;
int fd = accept( sock, (struct sockaddr *)&addr, &addr_len );
if( fd == -1 ) {
perror("accept");
run = false;
break;
}
struct in6_addr prefix = get_prefix(&addr.sin6_addr);
char buffer[44];
inet_ntop(AF_INET6, &prefix, buffer, 40);
strcat(buffer, "/64\n");
write(fd, buffer, strlen(buffer));
close(fd);
}
close(sock);
pthread_join( flush_thread, NULL );
return 0;
}