diff options
author | Linus Nordberg <linus@nordberg.se> | 2019-12-02 08:46:55 +0100 |
---|---|---|
committer | Thomas Gschwantner <tharre3@gmail.com> | 2019-12-11 06:22:17 +0100 |
commit | 29a46b6e37393970f4e4a35778d876c26c9ca0a4 (patch) | |
tree | 700b2bf3551dccca8550cbd443eb686739ce30a6 | |
parent | Ignore routes not for the wg interface (diff) | |
download | wg-dynamic-29a46b6e37393970f4e4a35778d876c26c9ca0a4.tar.xz wg-dynamic-29a46b6e37393970f4e4a35778d876c26c9ca0a4.zip |
Stop passing devname and ifindex around
Diffstat (limited to '')
-rw-r--r-- | lease.c | 30 | ||||
-rw-r--r-- | lease.h | 10 | ||||
-rw-r--r-- | wg-dynamic-server.c | 13 |
3 files changed, 28 insertions, 25 deletions
@@ -26,6 +26,8 @@ #define TIME_T_MAX (((time_t)1 << (sizeof(time_t) * CHAR_BIT - 2)) - 1) * 2 + 1 +static const char *devname = NULL; +static int ifindex = 0; static struct ip_pool pool; static time_t gexpires = TIME_T_MAX; static bool synchronized; @@ -52,13 +54,17 @@ static time_t get_monotonic_time() return monotime.tv_sec; } -void leases_init(char *fname, struct mnl_socket *nlsock, uint32_t ifindex) +void leases_init(const char *device_name, int interface_index, char *fname, + struct mnl_socket *nlsock) { struct nlmsghdr *nlh; struct rtmsg *rtm; char buf[MNL_NLMSG_HDRLEN + MNL_ALIGN(sizeof *rtm)]; unsigned int seq; + devname = device_name; + ifindex = interface_index; + synchronized = false; leases_ht = kh_init(leaseht); ipp_init(&pool); @@ -73,7 +79,7 @@ void leases_init(char *fname, struct mnl_socket *nlsock, uint32_t ifindex) if (mnl_socket_sendto(nlsock, nlh, nlh->nlmsg_len) < 0) fatal("mnl_socket_sendto()"); - leases_update_pools(nlsock, ifindex); + leases_update_pools(nlsock); synchronized = true; UNUSED(fname); /* TODO: open file and initialize from it */ @@ -120,8 +126,7 @@ static char *updates_to_str(const struct allowedips_update *u) return buf; } -static void update_allowed_ips_bulk(const char *devname, - const struct allowedips_update *updates, +static void update_allowed_ips_bulk(const struct allowedips_update *updates, int nupdates) { wg_peer peers[WG_DYNAMIC_LEASE_CHUNKSIZE] = { 0 }; @@ -180,7 +185,7 @@ static void update_allowed_ips_bulk(const char *devname, /* Updates allowedips for peer_pubkey, adding what's in lease * (including lladdr), removing all others. */ -static void update_allowed_ips(const char *devname, wg_key peer_pubkey, +static void update_allowed_ips(wg_key peer_pubkey, struct wg_dynamic_lease *lease) { struct allowedips_update update; @@ -190,11 +195,10 @@ static void update_allowed_ips(const char *devname, wg_key peer_pubkey, update.ipv4 = &lease->ipv4; update.ipv6 = &lease->ipv6; - update_allowed_ips_bulk(devname, &update, 1); + update_allowed_ips_bulk(&update, 1); } -struct wg_dynamic_lease *set_lease(const char *devname, wg_key pubkey, - uint32_t leasetime, +struct wg_dynamic_lease *set_lease(wg_key pubkey, uint32_t leasetime, const struct in6_addr *lladdr, const struct in_addr *ipv4, const struct in6_addr *ipv6) @@ -281,7 +285,7 @@ struct wg_dynamic_lease *set_lease(const char *devname, wg_key pubkey, } } - update_allowed_ips(devname, pubkey, lease); + update_allowed_ips(pubkey, lease); if (clock_gettime(CLOCK_REALTIME, &tp)) fatal("clock_gettime(CLOCK_REALTIME)"); @@ -319,7 +323,7 @@ struct wg_dynamic_lease *get_leases(wg_key pubkey) return kh_val(leases_ht, k); } -int leases_refresh(const char *devname) +int leases_refresh() { time_t cur_time = get_monotonic_time(); struct allowedips_update updates[WG_DYNAMIC_LEASE_CHUNKSIZE] = { 0 }; @@ -353,7 +357,7 @@ int leases_refresh(const char *devname) ++i; if (i == WG_DYNAMIC_LEASE_CHUNKSIZE) { - update_allowed_ips_bulk(devname, updates, i); + update_allowed_ips_bulk(updates, i); i = 0; memset(updates, 0, sizeof updates); } @@ -368,7 +372,7 @@ int leases_refresh(const char *devname) } if (i) - update_allowed_ips_bulk(devname, updates, i); + update_allowed_ips_bulk(updates, i); return MIN(INT_MAX / 1000, gexpires - cur_time); } @@ -480,7 +484,7 @@ static int process_nlpacket_cb(const struct nlmsghdr *nlh, void *data) return MNL_CB_OK; } -void leases_update_pools(struct mnl_socket *nlsock, uint32_t ifindex) +void leases_update_pools(struct mnl_socket *nlsock) { int ret; char buf[MNL_SOCKET_BUFFER_SIZE]; @@ -28,7 +28,8 @@ struct wg_dynamic_lease { * Initializes internal state, retrieves routes from nlsock and reads leases * from fname. */ -void leases_init(char *fname, struct mnl_socket *nlsock, uint32_t ifindex); +void leases_init(const char *device_name, int interface_index, char *fname, + struct mnl_socket *nlsock); /* * Frees everything, closes file. @@ -41,8 +42,7 @@ void leases_free(); * taken. Frees currently held lease, if any. Updates allowedips for * the peer. */ -struct wg_dynamic_lease *set_lease(const char *devname, wg_key pubkey, - uint32_t leasetime, +struct wg_dynamic_lease *set_lease(wg_key pubkey, uint32_t leasetime, const struct in6_addr *lladdr, const struct in_addr *ipv4, const struct in6_addr *ipv6); @@ -55,11 +55,11 @@ struct wg_dynamic_lease *get_leases(wg_key pubkey); /* Refreshes all leases, meaning expired ones will be removed. Returns the * amount of seconds until the next lease will expire, or at most INT_MAX/1000. */ -int leases_refresh(const char *devname); +int leases_refresh(); /* * Updates all pools with information from the mnl socket nlsock. */ -void leases_update_pools(struct mnl_socket *nlsock, uint32_t ifindex); +void leases_update_pools(struct mnl_socket *nlsock); #endif diff --git a/wg-dynamic-server.c b/wg-dynamic-server.c index 3aa4344..fa06e40 100644 --- a/wg-dynamic-server.c +++ b/wg-dynamic-server.c @@ -99,7 +99,7 @@ static bool validate_link_local_ip(uint32_t ifindex) return cb_data.valid_ip_found; } -static bool valid_peer_found(wg_device *device) +static bool valid_peer_found() { wg_peer *peer; wg_key_b64_string key; @@ -292,7 +292,7 @@ static int response_request_ip(struct wg_dynamic_attr *cur, wg_key pubkey, cur = cur->next; } - *lease = set_lease(wg_interface, pubkey, leasetime, lladdr, ipv4, ipv6); + *lease = set_lease(pubkey, leasetime, lladdr, ipv4, ipv6); if (!*lease) return E_IP_UNAVAIL; @@ -423,8 +423,7 @@ static void init_leases_from_peers() if (!ipv4 && !ipv6) continue; - set_lease(wg_interface, peer->public_key, leasetime, lladdr, - ipv4, ipv6); + set_lease(peer->public_key, leasetime, lladdr, ipv4, ipv6); } } @@ -453,7 +452,7 @@ static void setup() wg_interface); setup_sockets(); - leases_init(NULL, nlsock, device->ifindex); + leases_init(wg_interface, device->ifindex, NULL, nlsock); init_leases_from_peers(); } @@ -509,7 +508,7 @@ static void handle_event(void *ptr, uint32_t events) } if (ptr == nlsock) { - leases_update_pools(nlsock, device->ifindex); + leases_update_pools(nlsock); return; } @@ -543,7 +542,7 @@ static void poll_loop() fatal("epoll_ctl()"); while (1) { - time_t next = leases_refresh(wg_interface) * 1000; + time_t next = leases_refresh() * 1000; int nfds = epoll_wait(epollfd, events, MAX_CONNECTIONS, next); if (nfds == -1) { if (errno == EINTR) |