Hi Stefan,

Richard Procter offered some kind advice on the ordering of options in the man 
page
(to be done alphabetically) and commented on an unnecessary cast.

I also believe that I goofed by failing to initalize the mutex and zero the 
description
upon peer creation (in wg_peer_create).

I’ve attempted to address these issues and have pasted the diff below.

NM


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     26 Oct 2021 23:01:11 -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 *
@@ -409,6 +413,9 @@ wg_peer_create(struct wg_softc *sc, uint
        peer->p_counters_tx = 0;
        peer->p_counters_rx = 0;

+       mtx_init(&peer->p_description_mtx, IPL_NET);
+       memset(peer->p_description, 0, IFDESCRSIZE);
+
        mtx_init(&peer->p_endpoint_mtx, IPL_NET);
        bzero(&peer->p_endpoint, sizeof(peer->p_endpoint));

@@ -583,6 +590,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 +2339,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 +2452,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     26 Oct 2021 23:01:11 -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[];
};
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  26 Oct 2021 23:01:34 -0000
@@ -2309,6 +2309,10 @@ Set the peer's IPv4 or IPv6
range for tunneled traffic.
Repeat the option to set multiple ranges.
By default, no addresses are allowed.
+.It Cm wgdesc Ar value
+Specify a description of the peer.
+.It Cm -wgdesc
+Clear the peer description.
.It Cm wgendpoint Ar peer_address port
Address traffic to the peer's IPv4 or IPv6
.Ar peer_address
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  26 Oct 2021 23:01:34 -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, "", 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");

Reply via email to