From 49ee2110426b69d74a4446a9f6b2104b7114f6a6 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Sun, 29 Sep 2019 15:08:18 +0200 Subject: Wireguard: Dump peers --- proto/wireguard/wireguard.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/proto/wireguard/wireguard.c b/proto/wireguard/wireguard.c index f4bc56c6..e66bd7fa 100644 --- a/proto/wireguard/wireguard.c +++ b/proto/wireguard/wireguard.c @@ -16,6 +16,8 @@ #include "sysdep/unix/wg_user.h" #include "wireguard.h" +static ip_addr allowedip_to_ipa(struct wg_allowedip *allowedip); + static int get_device(struct wg_proto *p, wg_device **pdev, const char *device_name) { @@ -157,6 +159,21 @@ dump(void *ptr, size_t len) fprintf(stderr, "\n"); } +static void +dump_peer(struct wg_peer *peer) +{ + wg_key_b64_string base64; + wg_key_to_base64(base64, peer->public_key); + debug("WG: peer %s\n", base64); + + struct wg_allowedip *allowedip = NULL; + wg_for_each_allowedip(peer, allowedip) { + ip_addr ip = allowedip_to_ipa(allowedip); + + debug("allowedip %I/%d\n", ip, allowedip->cidr); + } +} + static wg_peer * add_peer(wg_device *dev, const wg_key pubkey) { @@ -228,6 +245,20 @@ set_peer_tunnel_ep(wg_peer *peer, ip_addr tunnel_ep_addr, u16 udp_dest_port) return 0; } +static ip_addr +allowedip_to_ipa(struct wg_allowedip *allowedip) +{ + switch (allowedip->family) { + case AF_INET: + return ipa_from_in4(allowedip->ip4); + break; + case AF_INET6: + return ipa_from_in6(allowedip->ip6); + } + + return IPA_NONE; +} + static void init_allowed_ip(struct wg_allowedip *allowedip, u8 net_type, struct network *n) { @@ -261,7 +292,7 @@ add_allowed_ip(u8 net_type, struct network *n, wg_peer *peer) return 0; } -static void +static bool remove_allowed_ip(wg_peer *peer, struct wg_allowedip *allowedip) { struct wg_allowedip *ip = NULL; @@ -307,8 +338,10 @@ remove_allowed_ip(wg_peer *peer, struct wg_allowedip *allowedip) } free(ip); - break; + return true; } + + return false; } static void @@ -635,6 +668,13 @@ wg_dump(struct proto *P) } FIB_WALK_END; } + + struct wg_peer *peer = NULL; + + log(L_TRACE "WG: dump peers"); + wg_for_each_peer(p->dev, peer) { + dump_peer(peer); + } } static void -- cgit v1.2.3