aboutsummaryrefslogtreecommitdiffstats
path: root/lease.c
blob: b7258eff742c0da18020060e2d7653d940b89d3a (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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */
#define _GNU_SOURCE

#include <arpa/inet.h>
#include <inttypes.h>
#include <libmnl/libmnl.h>
#include <linux/rtnetlink.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <time.h>
#include <string.h>

#include "common.h"
#include "dbg.h"
#include "khash.h"
#include "lease.h"
#include "netlink.h"
#include "radix-trie.h"
#include "random.h"

#define TIME_T_MAX (((time_t)1 << (sizeof(time_t) * CHAR_BIT - 2)) - 1) * 2 + 1

static struct ipns ipns;
static time_t gexpires = TIME_T_MAX;
static bool synchronized;

KHASH_MAP_INIT_WGKEY(leaseht, struct wg_dynamic_lease *)
khash_t(leaseht) *leases_ht = NULL;

static time_t get_monotonic_time()
{
	struct timespec monotime;
#ifdef __linux__
	if (clock_gettime(CLOCK_BOOTTIME, &monotime))
		fatal("clock_gettime(CLOCK_BOOTTIME)");
#else
	/* CLOCK_MONOTONIC works on openbsd, but apparently not (yet) on
	 * freebsd: https://lists.freebsd.org/pipermail/freebsd-hackers/2018-June/052899.html
	 */
	if (clock_gettime(CLOCK_MONOTONIC, &monotime))
		fatal("clock_gettime(CLOCK_MONOTONIC)");

#endif
	/* TODO: what about darwin? */

	return monotime.tv_sec;
}

void leases_init(char *fname, struct mnl_socket *nlsock, uint32_t ifindex)
{
	struct nlmsghdr *nlh;
	struct rtmsg *rtm;
	char buf[MNL_NLMSG_HDRLEN + MNL_ALIGN(sizeof *rtm)];
	unsigned int seq;

	synchronized = false;
	leases_ht = kh_init(leaseht);
	ipp_init(&ipns);

	nlh = mnl_nlmsg_put_header(buf);
	nlh->nlmsg_type = RTM_GETROUTE;
	nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
	nlh->nlmsg_seq = seq = time(NULL);
	rtm = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtmsg));
	rtm->rtm_family = 0; /* both ipv4 and ipv6 */

	if (mnl_socket_sendto(nlsock, nlh, nlh->nlmsg_len) < 0)
		fatal("mnl_socket_sendto()");

	leases_update_pools(nlsock, ifindex);
	synchronized = true;

	UNUSED(fname); /* TODO: open file and initialize from it */
}

void leases_free()
{
	if (leases_ht) {
		for (khint_t k = 0; k < kh_end(leases_ht); ++k)
			if (kh_exist(leases_ht, k))
				free((char *)kh_key(leases_ht, k));
	}
	kh_destroy(leaseht, leases_ht);

	ipp_free(&ipns);
}

static struct wg_dynamic_lease *new_lease(const struct wg_dynamic_lease *lease,
					  const struct in_addr *ipv4,
					  const struct in6_addr *ipv6)
{
	struct wg_dynamic_lease *newlease;

	newlease = calloc(1, sizeof(*newlease));
	if (!newlease)
		fatal("calloc()");

	if (!lease_is_valid(lease))
		return newlease;

	if (ipv4 && ipv4->s_addr && ipv4->s_addr == lease->ipv4.s_addr) {
		char ip_asc[INET_ADDRSTRLEN];
		inet_ntop(AF_INET, ipv4, ip_asc, sizeof(ip_asc));
		debug("extending %s\n", ip_asc);

		newlease->ipv4 = lease->ipv4;
	}

	if (ipv6 && !IN6_IS_ADDR_UNSPECIFIED(ipv6) &&
	    IN6_ARE_ADDR_EQUAL(ipv6, &lease->ipv6)) {
		char ip_asc[INET6_ADDRSTRLEN];
		inet_ntop(AF_INET6, ipv6, ip_asc, sizeof(ip_asc));
		debug("extending %s\n", ip_asc);

		newlease->ipv6 = lease->ipv6;
	}

	return newlease;
}

struct wg_dynamic_lease *set_lease(const char *devname, wg_key pubkey,
				   uint32_t leasetime,
				   const struct in6_addr *lladdr,
				   const struct in_addr *ipv4,
				   const struct in6_addr *ipv6)
{
	struct wg_dynamic_lease *current, *new;
	uint64_t index_l;
	uint32_t index, index_h;
	struct timespec tp;
	khiter_t k;
	int ret;
	bool wants_ipv4 = !ipv4 || ipv4->s_addr;
	bool wants_ipv6 = !ipv6 || !IN6_IS_ADDR_UNSPECIFIED(ipv6);
	wg_key_b64_string pubkey_asc;
	wg_key_to_base64(pubkey_asc, pubkey);

	current = get_leases(pubkey);
	new = new_lease(current, ipv4, ipv6);

	if (current) {
		char ip_asc[INET6_ADDRSTRLEN];

		if (current->ipv4.s_addr && !new->ipv4.s_addr) {
			inet_ntop(AF_INET, &current->ipv4, ip_asc,
				  sizeof(ip_asc));
			debug("deleting from pool: %s\n", ip_asc);

			if (ipp_del_v4(&ipns, &current->ipv4, 32))
				die("ipp_del_v4()\n");
		}

		if (!IN6_IS_ADDR_UNSPECIFIED(&current->ipv6) &&
		    IN6_IS_ADDR_UNSPECIFIED(&new->ipv6)) {
			inet_ntop(AF_INET6, &current->ipv6, ip_asc,
				  sizeof(ip_asc));
			debug("deleting from pool: %s\n", ip_asc);

			if (ipp_del_v6(&ipns, &current->ipv6, 128))
				die("ipp_del_v6()\n");
		}
	}

	if (wants_ipv4 && !new->ipv4.s_addr) {
		if (!ipns.total_ipv4) {
			debug("IPv4 pool empty\n");
		} else if (!ipv4) {
			index = random_bounded(ipns.total_ipv4 - 1);
			debug("new_lease(v4): %u of %ju\n", index,
			      ipns.total_ipv4);

			ipp_addnth_v4(&ipns, &new->ipv4, index);
		} else {
			char ip_asc[INET_ADDRSTRLEN];
			inet_ntop(AF_INET, ipv4, ip_asc, sizeof(ip_asc));
			debug("wants %s: ", ip_asc);

			if (!ipp_add_v4(&ipns, ipv4, 32)) {
				debug("allocated\n");

				new->ipv4 = *ipv4;
			} else {
				debug("not free\n");
			}
		}
	}

	if (wants_ipv6 && IN6_IS_ADDR_UNSPECIFIED(&new->ipv6)) {
		if (!ipns.totalh_ipv6 && !ipns.totall_ipv6) {
			debug("IPv6 pool empty\n");
		} else if (!ipv6) {
			if (ipns.totalh_ipv6 > 0) {
				index_l = random_bounded(UINT64_MAX);
				index_h = random_bounded(ipns.totalh_ipv6 - 1);
			} else {
				index_l = random_bounded(ipns.totall_ipv6);
				index_h = 0;
			}

			debug("new_lease(v6): %u:%ju of %u:%ju\n", index_h,
			      index_l, ipns.totalh_ipv6, ipns.totall_ipv6);
			ipp_addnth_v6(&ipns, &new->ipv6, index_l, index_h);
		} else {
			char ip_asc[INET6_ADDRSTRLEN];
			inet_ntop(AF_INET6, ipv6, ip_asc, sizeof(ip_asc));
			debug("wants %s: ", ip_asc);

			if (!ipp_add_v6(&ipns, ipv6, 128)) {
				debug("allocated\n");

				new->ipv6 = *ipv6;
			} else {
				debug("not free\n");
			}
		}
	}

	new->lladdr = *lladdr;
	update_allowed_ips(devname, pubkey, new);

	if (!new->ipv4.s_addr && IN6_IS_ADDR_UNSPECIFIED(&new->ipv6)) {
		khiter_t k = kh_get(leaseht, leases_ht, pubkey);
		if (k != kh_end(leases_ht)) {
			BUG_ON(!current);
			BUG_ON(kh_value(leases_ht, k) != current);
			debug("freeing lease: %s\n", lease_to_str(current));
			free(current);
			free((char *)kh_key(leases_ht, k));
			kh_del(leaseht, leases_ht, k);
		}

		free(new);
		return NULL;
	}

	if (clock_gettime(CLOCK_REALTIME, &tp))
		fatal("clock_gettime(CLOCK_REALTIME)");
	new->start_real = tp.tv_sec;
	new->start_mono = get_monotonic_time();
	new->leasetime = leasetime;

	wg_key *pubcopy = malloc(sizeof(wg_key));
	if (!pubcopy)
		fatal("malloc()");

	memcpy(pubcopy, pubkey, sizeof(wg_key));
	k = kh_put(leaseht, leases_ht, *pubcopy, &ret);
	if (ret < 0) {
		fatal("kh_put()");
	} else if (ret == 0) {
		BUG_ON(!current);
		BUG_ON(kh_value(leases_ht, k) != current);
		debug("freeing lease (replace): %s\n", lease_to_str(current));
		free(current);
	}
	kh_value(leases_ht, k) = new;

	debug("new lease: %s\n", lease_to_str(new));

	if (new->start_mono + new->leasetime < gexpires)
		gexpires = new->start_mono + new->leasetime;

	/* TODO: add record to file */

	return new;
}

struct wg_dynamic_lease *get_leases(wg_key pubkey)
{
	khiter_t k = kh_get(leaseht, leases_ht, pubkey);

	if (k == kh_end(leases_ht))
		return NULL;
	else
		return kh_val(leases_ht, k);
}

struct allowedips_update {
	wg_key peer_pubkey;
	struct in6_addr lladdr;
	struct in_addr ipv4;
	struct in6_addr ipv6;
};

static char *updates_to_str(const struct allowedips_update *u)
{
	static char buf[4096];
	wg_key_b64_string pubkey_asc;
	char ll[INET6_ADDRSTRLEN], v4[INET_ADDRSTRLEN], v6[INET6_ADDRSTRLEN];

	if (!u)
		return "(null)";

	wg_key_to_base64(pubkey_asc, u->peer_pubkey);
	inet_ntop(AF_INET, &u->ipv4, v4, sizeof v4);
	inet_ntop(AF_INET6, &u->ipv6, v6, sizeof v6);
	inet_ntop(AF_INET6, &u->lladdr, ll, sizeof ll);
	snprintf(buf, sizeof buf, "(%p) [%s] %s [%s]", u, ll, v4, v6);

	return buf;
}

static void update_allowed_ips_bulk(const char *devname,
				    const struct allowedips_update *updates,
				    int nupdates)
{
	wg_peer peers[WG_DYNAMIC_LEASE_CHUNKSIZE] = { 0 };
	wg_allowedip allowedips[3 * WG_DYNAMIC_LEASE_CHUNKSIZE] = { 0 };
	wg_device dev = { 0 };
	wg_peer **pp = &dev.first_peer;

	int peer_idx = 0;
	int allowedips_idx = 0;
	for (int i = 0; i < nupdates; i++) {
		debug("setting allowedips for %s\n",
		      updates_to_str(&updates[i]));

		peers[peer_idx].flags |= WGPEER_REPLACE_ALLOWEDIPS;
		memcpy(peers[peer_idx].public_key, updates[i].peer_pubkey,
		       sizeof(wg_key));
		wg_allowedip **aipp = &peers[peer_idx].first_allowedip;

		if (!IN6_IS_ADDR_UNSPECIFIED(&updates[i].lladdr)) {
			allowedips[allowedips_idx] = (wg_allowedip){
				.family = AF_INET6,
				.cidr = 128,
				.ip6 = updates[i].lladdr,
			};
			*aipp = &allowedips[allowedips_idx];
			aipp = &allowedips[allowedips_idx].next_allowedip;
			++allowedips_idx;
		}
		if (updates[i].ipv4.s_addr) {
			allowedips[allowedips_idx] = (wg_allowedip){
				.family = AF_INET,
				.cidr = 32,
				.ip4 = updates[i].ipv4,
			};
			*aipp = &allowedips[allowedips_idx];
			aipp = &allowedips[allowedips_idx].next_allowedip;
			++allowedips_idx;
		}
		if (!IN6_IS_ADDR_UNSPECIFIED(&updates[i].ipv6)) {
			allowedips[allowedips_idx] = (wg_allowedip){
				.family = AF_INET6,
				.cidr = 128,
				.ip6 = updates[i].ipv6,
			};
			*aipp = &allowedips[allowedips_idx];
			++allowedips_idx;
		}

		*pp = &peers[peer_idx];
		pp = &peers[peer_idx].next_peer;
		++peer_idx;
	}

	strncpy(dev.name, devname, sizeof(dev.name) - 1);
	if (wg_set_device(&dev))
		fatal("wg_set_device()");
}

void update_allowed_ips(const char *devname, wg_key peer_pubkey,
			const struct wg_dynamic_lease *lease)
{
	struct allowedips_update update;

	memcpy(update.peer_pubkey, peer_pubkey, sizeof(wg_key));
	update.lladdr = lease->lladdr;
	update.ipv4 = lease->ipv4;
	update.ipv6 = lease->ipv6;

	update_allowed_ips_bulk(devname, &update, 1);
}

int leases_refresh(const char *devname)
{
	time_t cur_time = get_monotonic_time();
	struct allowedips_update updates[WG_DYNAMIC_LEASE_CHUNKSIZE] = { 0 };

	if (cur_time < gexpires)
		return MIN(INT_MAX / 1000, gexpires - cur_time);

	gexpires = TIME_T_MAX;

	int i = 0;
	for (khint_t k = kh_begin(leases_ht); k != kh_end(leases_ht); ++k) {
		if (!kh_exist(leases_ht, k))
			continue;
		struct wg_dynamic_lease *lease = kh_val(leases_ht, k);
		BUG_ON(!lease);
		time_t expires = lease->start_mono + lease->leasetime;
		if (cur_time >= expires) {
			if (lease->ipv4.s_addr)
				ipp_del_v4(&ipns, &lease->ipv4, 32);

			if (!IN6_IS_ADDR_UNSPECIFIED(&lease->ipv6))
				ipp_del_v6(&ipns, &lease->ipv6, 128);

			memcpy(updates[i].peer_pubkey, kh_key(leases_ht, k),
			       sizeof(wg_key));
			updates[i].lladdr = lease->lladdr;

			wg_key_b64_string pubkey_asc;
			wg_key_to_base64(pubkey_asc, updates[i].peer_pubkey);
			debug("Peer losing its lease: %s\n", pubkey_asc);

			++i;
			if (i == WG_DYNAMIC_LEASE_CHUNKSIZE) {
				update_allowed_ips_bulk(devname, updates, i);
				i = 0;
				memset(updates, 0, sizeof updates);
			}

			free(lease);
			free((char *)kh_key(leases_ht, k));
			kh_del(leaseht, leases_ht, k);
		} else {
			if (expires < gexpires)
				gexpires = expires;
		}
	}

	if (i)
		update_allowed_ips_bulk(devname, updates, i);

	return MIN(INT_MAX / 1000, gexpires - cur_time);
}

static int data_ipv4_attr_cb(const struct nlattr *attr, void *data)
{
	const struct nlattr **tb = data;
	int type = mnl_attr_get_type(attr);

	switch (type) {
	case RTA_DST:
	case RTA_GATEWAY:
		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
			log_err("mnl_attr_validate: %s\n", strerror(errno));
			return MNL_CB_ERROR;
		}
		break;
	case RTA_OIF:
		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
			log_err("mnl_attr_validate: %s\n", strerror(errno));
			return MNL_CB_ERROR;
		}
		break;
	default:
		return MNL_CB_OK;
	}
	tb[type] = attr;
	return MNL_CB_OK;
}

static int data_ipv6_attr_cb(const struct nlattr *attr, void *data)
{
	const struct nlattr **tb = data;
	int type = mnl_attr_get_type(attr);

	switch (type) {
	case RTA_DST:
	case RTA_GATEWAY:
		if (mnl_attr_validate2(attr, MNL_TYPE_BINARY,
				       sizeof(struct in6_addr)) < 0) {
			log_err("mnl_attr_validate: %s\n", strerror(errno));
			return MNL_CB_ERROR;
		}
		break;
	case RTA_OIF:
		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
			log_err("mnl_attr_validate: %s\n", strerror(errno));
			return MNL_CB_ERROR;
		}
		break;
	default:
		return MNL_CB_OK;
	}
	tb[type] = attr;
	return MNL_CB_OK;
}

static int process_nlpacket_cb(const struct nlmsghdr *nlh, void *data)
{
	struct nlattr *tb[RTA_MAX + 1] = {};
	struct rtmsg *rm = mnl_nlmsg_get_payload(nlh);
	uint32_t ifindex;

	BUG_ON(!data);
	ifindex = *((int *) data);

	if (rm->rtm_family == AF_INET)
		mnl_attr_parse(nlh, sizeof(*rm), data_ipv4_attr_cb, tb);
	else if (rm->rtm_family == AF_INET6)
		mnl_attr_parse(nlh, sizeof(*rm), data_ipv6_attr_cb, tb);

	if (!tb[RTA_OIF] || mnl_attr_get_u32(tb[RTA_OIF]) != ifindex) {
		debug("ignoring interface %u (want %u)\n", tb[RTA_OIF] ? mnl_attr_get_u32(tb[RTA_OIF]) : 0, ifindex);
	  	return MNL_CB_OK;
	}

	if (tb[RTA_GATEWAY])
		return MNL_CB_OK;

	if (!tb[RTA_DST]) {
		debug("Netlink packet without RTA_DST, ignoring\n");
		return MNL_CB_OK;
	}

	void *addr = mnl_attr_get_payload(tb[RTA_DST]);
	if (rm->rtm_family == AF_INET6 &&
	    (is_link_local(addr) || IN6_IS_ADDR_MULTICAST(addr)))
		return MNL_CB_OK;

	if (nlh->nlmsg_type == RTM_NEWROUTE) {
		if (rm->rtm_family == AF_INET) {
			if (ipp_addpool_v4(&ipns, addr, rm->rtm_dst_len))
				die("ipp_addpool_v4()\n");
		} else if (rm->rtm_family == AF_INET6) {
			if (ipp_addpool_v6(&ipns, addr, rm->rtm_dst_len))
				die("ipp_addpool_v6()\n");
		}
	} else if (nlh->nlmsg_type == RTM_DELROUTE) {
		if (rm->rtm_family == AF_INET) {
			if (ipp_removepool_v4(&ipns, addr) && synchronized)
				die("ipp_removepool_v4()\n");
		} else if (rm->rtm_family == AF_INET6) {
			if (ipp_removepool_v6(&ipns, addr) && synchronized)
				die("ipp_removepool_v6()\n");
		}
	}

	return MNL_CB_OK;
}

void leases_update_pools(struct mnl_socket *nlsock, uint32_t ifindex)
{
	int ret;
	char buf[MNL_SOCKET_BUFFER_SIZE];

	while ((ret = mnl_socket_recvfrom(nlsock, buf, sizeof buf)) > 0) {
		if (mnl_cb_run(buf, ret, 0, 0, process_nlpacket_cb, (void *) &ifindex) == -1)
			fatal("mnl_cb_run()");
	}

	if (ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
		fatal("mnl_socket_recvfrom()");
}

bool lease_is_valid(const struct wg_dynamic_lease *lease)
{
	if (!lease)
		return false;

	if (get_monotonic_time() >= lease->start_mono + lease->leasetime)
		return false;

	return true;
}

#ifdef DEBUG
char *lease_to_str(const struct wg_dynamic_lease *l)
{
	static char buf[4096];
	char ll[INET6_ADDRSTRLEN], v4[INET_ADDRSTRLEN], v6[INET6_ADDRSTRLEN];

	if (!l)
		return "(null)";

	inet_ntop(AF_INET6, &l->lladdr, ll, sizeof ll);
	inet_ntop(AF_INET, &l->ipv4, v4, sizeof v4);
	inet_ntop(AF_INET6, &l->ipv6, v6, sizeof v6);
	snprintf(buf, sizeof buf, "(%p) [%s] %s [%s]", l, ll, v4, v6);

	return buf;
}
#endif