diff options
author | Linus Nordberg <linus@nordberg.se> | 2019-09-30 09:17:16 +0200 |
---|---|---|
committer | Thomas Gschwantner <tharre3@gmail.com> | 2019-12-11 06:21:37 +0100 |
commit | 0f5c4001bfab753b7d97866504cfcdd5635f2727 (patch) | |
tree | 9d10b19504675b81054027c6cd59a710d8922bb2 /wg-dynamic-server.c | |
parent | server: add --leasetime as a commandline option (diff) | |
download | wg-dynamic-0f5c4001bfab753b7d97866504cfcdd5635f2727.tar.xz wg-dynamic-0f5c4001bfab753b7d97866504cfcdd5635f2727.zip |
server: restore leases from allowedips at startup
Diffstat (limited to 'wg-dynamic-server.c')
-rw-r--r-- | wg-dynamic-server.c | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/wg-dynamic-server.c b/wg-dynamic-server.c index b53f857..959a0f1 100644 --- a/wg-dynamic-server.c +++ b/wg-dynamic-server.c @@ -401,6 +401,33 @@ static void cleanup() } } +static void init_leases_from_peers() +{ + wg_peer *peer; + + wg_for_each_peer (device, peer) { + wg_allowedip *allowedip; + struct in6_addr *lladdr = NULL; + struct in_addr *ipv4 = NULL; + struct in6_addr *ipv6 = NULL; + wg_for_each_allowedip (peer, allowedip) { + if (allowedip->family == AF_INET6 && + IN6_IS_ADDR_LINKLOCAL(&allowedip->ip6)) + lladdr = &allowedip->ip6; + else if (allowedip->family == AF_INET && !ipv4) + ipv4 = &allowedip->ip4; + else if (allowedip->family == AF_INET6 && !ipv6) + ipv6 = &allowedip->ip6; + } + + if (!ipv4 && !ipv6) + continue; + + set_lease(wg_interface, peer->public_key, leasetime, lladdr, + ipv4, ipv6); + } +} + static void setup() { if (inet_pton(AF_INET6, WG_DYNAMIC_ADDR, &well_known) != 1) @@ -426,7 +453,8 @@ static void setup() wg_interface); setup_sockets(); - leases_init("leases_file", nlsock); + leases_init(NULL, nlsock); + init_leases_from_peers(); } static int get_avail_request() |