* Peer names
@ 2019-04-15 5:35 Eric Atkin
0 siblings, 0 replies; only message in thread
From: Eric Atkin @ 2019-04-15 5:35 UTC (permalink / raw)
To: wireguard
[-- Attachment #1.1: Type: text/plain, Size: 427 bytes --]
I find identifying peers in `wg show` output by the public key to be human
un-friendly. The following patch adds an optional "alias". I am not a
kernel developer or even a competent C developer. I think there are memory
leaks here and perhaps other issues, but it does work and demonstrates the
concept. This is as far as I can take it. Is this of interest or help to
anyone who could get it across the finish line?
Eric Atkin
[-- Attachment #1.2: Type: text/html, Size: 499 bytes --]
[-- Attachment #2: 0001-Add-peer-aliases.patch --]
[-- Type: text/x-patch, Size: 4969 bytes --]
From f1e82b660e9d8e2cc5b2ab6bb0c31758b045eeb2 Mon Sep 17 00:00:00 2001
From: Eric Atkin <eatkin@certusllc.us>
Date: Sun, 14 Apr 2019 23:25:58 -0600
Subject: [PATCH] Add peer aliases
---
src/netlink.c | 6 +++++-
src/peer.h | 1 +
src/tools/config.c | 12 +++++++++++-
src/tools/containers.h | 1 +
src/tools/ipc.c | 6 ++++++
src/tools/show.c | 2 ++
src/uapi/wireguard.h | 1 +
7 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/src/netlink.c b/src/netlink.c
index b179b31..6fee6b8 100644
--- a/src/netlink.c
+++ b/src/netlink.c
@@ -122,7 +122,8 @@ get_peer(struct wg_peer *peer, struct allowedips_node **next_allowedips_node,
if (fail)
goto err;
- if (nla_put(skb, WGPEER_A_LAST_HANDSHAKE_TIME,
+ if ((peer->alias && nla_put_string(skb, WGPEER_A_ALIAS, peer->alias)) ||
+ nla_put(skb, WGPEER_A_LAST_HANDSHAKE_TIME,
sizeof(last_handshake), &last_handshake) ||
nla_put_u16(skb, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
peer->persistent_keepalive_interval) ||
@@ -426,6 +427,9 @@ static int set_peer(struct wg_device *wg, struct nlattr **attrs)
up_write(&peer->handshake.lock);
}
+ if (attrs[WGPEER_A_ALIAS])
+ peer->alias = nla_strdup(attrs[WGPEER_A_ALIAS], GFP_KERNEL);
+
if (attrs[WGPEER_A_ENDPOINT]) {
struct sockaddr *addr = nla_data(attrs[WGPEER_A_ENDPOINT]);
size_t len = nla_len(attrs[WGPEER_A_ENDPOINT]);
diff --git a/src/peer.h b/src/peer.h
index 23af409..21db333 100644
--- a/src/peer.h
+++ b/src/peer.h
@@ -64,6 +64,7 @@ struct wg_peer {
u64 internal_id;
struct napi_struct napi;
bool is_dead;
+ char *alias;
};
struct wg_peer *wg_peer_create(struct wg_device *wg,
diff --git a/src/tools/config.c b/src/tools/config.c
index d510ea7..72c43ce 100644
--- a/src/tools/config.c
+++ b/src/tools/config.c
@@ -174,6 +174,14 @@ static inline bool parse_ip(struct wgallowedip *allowedip, const char *value)
return true;
}
+static inline bool parse_alias(char **alias, const char *value) {
+ if (!(*alias = strdup(value))) {
+ perror("strdup");
+ return false;
+ }
+ return true;
+}
+
static inline bool parse_endpoint(struct sockaddr *endpoint, const char *value)
{
char *mutable = strdup(value);
@@ -435,7 +443,9 @@ static bool process_line(struct config_ctx *ctx, const char *line)
} else
goto error;
} else if (ctx->is_peer_section) {
- if (key_match("Endpoint"))
+ if (key_match("Alias"))
+ ret = parse_alias(&ctx->last_peer->alias, value);
+ else if (key_match("Endpoint"))
ret = parse_endpoint(&ctx->last_peer->endpoint.addr, value);
else if (key_match("PublicKey")) {
ret = parse_key(ctx->last_peer->public_key, value);
diff --git a/src/tools/containers.h b/src/tools/containers.h
index 59a213e..e961192 100644
--- a/src/tools/containers.h
+++ b/src/tools/containers.h
@@ -42,6 +42,7 @@ enum {
struct wgpeer {
uint32_t flags;
+ char *alias;
uint8_t public_key[WG_KEY_LEN];
uint8_t preshared_key[WG_KEY_LEN];
diff --git a/src/tools/ipc.c b/src/tools/ipc.c
index 7ab3a62..133aa38 100644
--- a/src/tools/ipc.c
+++ b/src/tools/ipc.c
@@ -590,6 +590,8 @@ again:
uint32_t flags = 0;
peer_nest = mnl_attr_nest_start_check(nlh, SOCKET_BUFFER_SIZE, 0);
+ if (peer->alias)
+ mnl_attr_put_str(nlh, WGPEER_A_ALIAS, peer->alias);
if (!peer_nest)
goto toobig_peers;
if (!mnl_attr_put_check(nlh, SOCKET_BUFFER_SIZE, WGPEER_A_PUBLIC_KEY, sizeof(peer->public_key), peer->public_key))
@@ -783,6 +785,10 @@ static int parse_peer(const struct nlattr *attr, void *data)
if (!mnl_attr_validate(attr, MNL_TYPE_U64))
peer->tx_bytes = mnl_attr_get_u64(attr);
break;
+ case WGPEER_A_ALIAS:
+ if (!mnl_attr_validate(attr, MNL_TYPE_STRING))
+ peer->alias = strdup(mnl_attr_get_str(attr));
+ break;
case WGPEER_A_ALLOWEDIPS:
return mnl_attr_parse_nested(attr, parse_allowedips, peer);
}
diff --git a/src/tools/show.c b/src/tools/show.c
index ff0897d..ff167a3 100644
--- a/src/tools/show.c
+++ b/src/tools/show.c
@@ -226,6 +226,8 @@ static void pretty_print(struct wgdevice *device)
}
for_each_wgpeer(device, peer) {
terminal_printf(TERMINAL_FG_YELLOW TERMINAL_BOLD "peer" TERMINAL_RESET ": " TERMINAL_FG_YELLOW "%s" TERMINAL_RESET "\n", key(peer->public_key));
+ if (peer->alias)
+ terminal_printf(" " TERMINAL_BOLD "alias" TERMINAL_RESET ": %s\n", peer->alias);
if (peer->flags & WGPEER_HAS_PRESHARED_KEY)
terminal_printf(" " TERMINAL_BOLD "preshared key" TERMINAL_RESET ": %s\n", masked_key(peer->preshared_key));
if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6)
diff --git a/src/uapi/wireguard.h b/src/uapi/wireguard.h
index 071ce41..b97ff16 100644
--- a/src/uapi/wireguard.h
+++ b/src/uapi/wireguard.h
@@ -174,6 +174,7 @@ enum wgpeer_attribute {
WGPEER_A_TX_BYTES,
WGPEER_A_ALLOWEDIPS,
WGPEER_A_PROTOCOL_VERSION,
+ WGPEER_A_ALIAS,
__WGPEER_A_LAST
};
#define WGPEER_A_MAX (__WGPEER_A_LAST - 1)
--
2.21.0
[-- Attachment #3: Type: text/plain, Size: 148 bytes --]
_______________________________________________
WireGuard mailing list
WireGuard@lists.zx2c4.com
https://lists.zx2c4.com/mailman/listinfo/wireguard
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2019-05-06 20:28 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-15 5:35 Peer names Eric Atkin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).