On Wed, Oct 20, 2021 at 10:20:09PM -0400, Noah Meier wrote:
> Hi,
>
> While wireguard interfaces can have a description set by ifconfig, wireguard
> peers currently cannot. I now have a lot of peers and descriptions of them in
> ifconfig would be helpful.
>
> This diff adds a 'wgdesc' option to a 'wgpeer' in ifconfig (and a
> corresponding '-wgdesc' option). Man page also updated.
>
> NM
This looks useful to me.
Did you get any feedback for this patch yet, Noah?
> Index: ifconfig.8
> ===================================================================
> RCS file: /cvs/src/sbin/ifconfig/ifconfig.8,v
> retrieving revision 1.375
> diff -u -p -u -p -r1.375 ifconfig.8
> --- ifconfig.8 18 Aug 2021 18:10:33 -0000 1.375
> +++ ifconfig.8 21 Oct 2021 00:09:20 -0000
> @@ -2343,6 +2343,10 @@ It is optional but recommended and can b
> .Dl $ openssl rand -base64 32
> .It Cm -wgpsk
> Remove the pre-shared key for this peer.
> +.It Cm wgdesc Ar value
> +Specify a description of the peer.
> +.It Cm -wgdesc
> +Clear the peer description.
> .El
> .Sh EXAMPLES
> Assign the
> Index: ifconfig.c
> ===================================================================
> RCS file: /cvs/src/sbin/ifconfig/ifconfig.c,v
> retrieving revision 1.445
> diff -u -p -u -p -r1.445 ifconfig.c
> --- ifconfig.c 6 Oct 2021 06:14:08 -0000 1.445
> +++ ifconfig.c 21 Oct 2021 00:09:20 -0000
> @@ -355,12 +355,14 @@ void setwgpeerep(const char *, const cha
> void setwgpeeraip(const char *, int);
> void setwgpeerpsk(const char *, int);
> void setwgpeerpka(const char *, int);
> +void setwgpeerdesc(const char *, int);
> void setwgport(const char *, int);
> void setwgkey(const char *, int);
> void setwgrtable(const char *, int);
>
> void unsetwgpeer(const char *, int);
> void unsetwgpeerpsk(const char *, int);
> +void unsetwgpeerdesc(const char *, int);
> void unsetwgpeerall(const char *, int);
>
> void wg_status();
> @@ -625,11 +627,13 @@ const struct cmd {
> { "wgaip", NEXTARG, A_WIREGUARD, setwgpeeraip},
> { "wgpsk", NEXTARG, A_WIREGUARD, setwgpeerpsk},
> { "wgpka", NEXTARG, A_WIREGUARD, setwgpeerpka},
> + { "wgdesc", NEXTARG, A_WIREGUARD, setwgpeerdesc},
> { "wgport", NEXTARG, A_WIREGUARD, setwgport},
> { "wgkey", NEXTARG, A_WIREGUARD, setwgkey},
> { "wgrtable", NEXTARG, A_WIREGUARD, setwgrtable},
> { "-wgpeer", NEXTARG, A_WIREGUARD, unsetwgpeer},
> { "-wgpsk", 0, A_WIREGUARD, unsetwgpeerpsk},
> + { "-wgdesc", 0, A_WIREGUARD, unsetwgpeerdesc},
> { "-wgpeerall", 0, A_WIREGUARD, unsetwgpeerall},
>
> #else /* SMALL */
> @@ -5827,6 +5831,16 @@ setwgpeerpka(const char *pka, int param)
> }
>
> void
> +setwgpeerdesc(const char *wgdesc, int param)
> +{
> + if (wg_peer == NULL)
> + errx(1, "wgdesc: wgpeer not set");
> + if (strlen(wgdesc))
> + strlcpy(wg_peer->p_description, wgdesc, IFDESCRSIZE);
> + wg_peer->p_flags |= WG_PEER_SET_DESCRIPTION;
> +}
> +
> +void
> setwgport(const char *port, int param)
> {
> const char *errmsg = NULL;
> @@ -5873,6 +5887,15 @@ unsetwgpeerpsk(const char *value, int pa
> }
>
> void
> +unsetwgpeerdesc(const char *value, int param)
> +{
> + if (wg_peer == NULL)
> + errx(1, "wgpesc: wgpeer not set");
> + strlcpy(wg_peer->p_description, (const char *)"", IFDESCRSIZE);
> + wg_peer->p_flags |= WG_PEER_SET_DESCRIPTION;
> +}
> +
> +void
> unsetwgpeerall(const char *value, int param)
> {
> ensurewginterface();
> @@ -5931,6 +5954,9 @@ wg_status(void)
> b64_ntop(wg_peer->p_public, WG_KEY_LEN,
> key, sizeof(key));
> printf("\twgpeer %s\n", key);
> +
> + if (strlen(wg_peer->p_description))
> + printf("\t\tdescription: %s\n", wg_peer->p_description);
>
> if (wg_peer->p_flags & WG_PEER_HAS_PSK)
> printf("\t\twgpsk (present)\n");
> Index: if_wg.c
> ===================================================================
> RCS file: /cvs/src/sys/net/if_wg.c,v
> retrieving revision 1.18
> diff -u -p -u -p -r1.18 if_wg.c
> --- if_wg.c 5 Aug 2021 13:37:04 -0000 1.18
> +++ if_wg.c 21 Oct 2021 00:10:29 -0000
> @@ -222,6 +222,9 @@ struct wg_peer {
>
> SLIST_ENTRY(wg_peer) p_start_list;
> int p_start_onlist;
> +
> + struct mutex p_description_mtx;
> + char p_description[IFDESCRSIZE];
> };
>
> struct wg_softc {
> @@ -276,6 +279,7 @@ int wg_peer_get_sockaddr(struct wg_peer
> void wg_peer_clear_src(struct wg_peer *);
> void wg_peer_get_endpoint(struct wg_peer *, struct wg_endpoint *);
> void wg_peer_counters_add(struct wg_peer *, uint64_t, uint64_t);
> +void wg_peer_set_description(struct wg_peer *, char *);
>
> int wg_aip_add(struct wg_softc *, struct wg_peer *, struct wg_aip_io *);
> struct wg_peer *
> @@ -583,6 +587,15 @@ wg_peer_counters_add(struct wg_peer *pee
> mtx_leave(&peer->p_counters_mtx);
> }
>
> +void
> +wg_peer_set_description(struct wg_peer *peer, char *description)
> +{
> + mtx_enter(&peer->p_description_mtx);
> + memset(peer->p_description, 0, IFDESCRSIZE);
> + strlcpy(peer->p_description, description, IFDESCRSIZE);
> + mtx_leave(&peer->p_description_mtx);
> +}
> +
> int
> wg_aip_add(struct wg_softc *sc, struct wg_peer *peer, struct wg_aip_io *d)
> {
> @@ -2323,6 +2336,10 @@ wg_ioctl_set(struct wg_softc *sc, struct
> }
> }
>
> + if (peer_o.p_flags & WG_PEER_SET_DESCRIPTION) {
> + wg_peer_set_description(peer, peer_o.p_description);
> + }
> +
> aip_p = &peer_p->p_aips[0];
> for (j = 0; j < peer_o.p_aips_count; j++) {
> if ((ret = copyin(aip_p, &aip_o, sizeof(aip_o))) != 0)
> @@ -2432,6 +2449,8 @@ wg_ioctl_get(struct wg_softc *sc, struct
> aip_count++;
> }
> peer_o.p_aips_count = aip_count;
> +
> + strlcpy(peer_o.p_description, peer->p_description, IFDESCRSIZE);
>
> if ((ret = copyout(&peer_o, peer_p, sizeof(peer_o))) != 0)
> goto unlock_and_ret_size;
> Index: if_wg.h
> ===================================================================
> RCS file: /cvs/src/sys/net/if_wg.h,v
> retrieving revision 1.4
> diff -u -p -u -p -r1.4 if_wg.h
> --- if_wg.h 22 Jun 2020 12:20:44 -0000 1.4
> +++ if_wg.h 21 Oct 2021 00:10:29 -0000
> @@ -61,6 +61,7 @@ struct wg_aip_io {
> #define WG_PEER_REPLACE_AIPS (1 << 4)
> #define WG_PEER_REMOVE (1 << 5)
> #define WG_PEER_UPDATE (1 << 6)
> +#define WG_PEER_SET_DESCRIPTION (1 << 7)
>
> #define p_sa p_endpoint.sa_sa
> #define p_sin p_endpoint.sa_sin
> @@ -80,6 +81,7 @@ struct wg_peer_io {
> uint64_t p_txbytes;
> uint64_t p_rxbytes;
> struct timespec p_last_handshake; /* nanotime */
> + char p_description[IFDESCRSIZE];
> size_t p_aips_count;
> struct wg_aip_io p_aips[];
> };
>
>