I took the libery and refreshed the patch. What I did so far:

- compiled GENERIC.MP on amd64
- compiled new ifconfig, same arch
- booted up new bsd.mp with the patch
- when wgdesc is not set, pre-patch ifconfig seems to work
- when with new ifconfig I set wgdesc, old ifconfig wg segfaults

Example from running -current:

pce-0067# ifconfig.new wg0 
wg0: flags=80c3<UP,BROADCAST,RUNNING,NOARP,MULTICAST> mtu 1420
        index 8 priority 0 llprio 3
        wgport 51820
        wgpubkey qcb...
        wgpeer klM...
                description: ks2
                wgpsk (present)
                wgpka 25 (sec)
                wgendpoint xxx.xxx.xxx.xxx 51820
                tx: 1932, rx: 620
                last handshake: 83 seconds ago
                wgaip fde4:f456:48c2:13c0::/64
        groups: wg
        inet6 fde4:f456:48c2:13c0::cc67 prefixlen 64

Seems to work for me. Looking at above ifconfig(8) output, not sure
should the output should say `description:` or just follow the pattern
of other wireguard keywords and just say `wgdesc` (same keyword like
for ifconfig command and no colon). I think I would prefer that.

Patch is not mine. It's from Noah Meier <noahgmeier at gmail.com> and
I just re-applied it to -current. It's at the end of this email.

I hope I didn't screw up anything.


On Wed, Jul 13, 2022 at 11:29:51AM +0000, Mikolaj Kucharski wrote:
> Hi,
> 
> I'm sorry I didn't test this, as I don't have -current OpenBSD on all my
> machines, but I do have one WireGuard gateway running -stable with few
> peers:
> 
> # ifconfig wg | grep -cw wgpeer
> 25
> 
> I would love to have this merged into main repo, as it would make my
> life a tiny bit easier to see which pubkey is which peer.
> 
> Based on what Stefan wrote below, do you have by any chance newer
> version of your diff, Noah?
> 
> 
> 
> On Wed, Dec 01, 2021 at 12:18:52AM +0100, Stefan Sperling wrote:
> > On Tue, Nov 30, 2021 at 02:31:20PM -0500, Noah Meier wrote:
> > > 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
> > 
> > This new patch does not apply cleanly.
> > Leading whitespace was stripped, and thus patch complains as follows:
> > 
> > Patching file if_wg.c using Plan A...
> > patch: **** malformed patch at line 15: };
> > 
> > And it would help if you created a patch where all paths are relative to
> > the /usr/src directory. Something like this should do it:
> > 
> >  cd /usr/src
> >  cvs diff -u sbin/ifconfig sys/net  > /tmp/wgdesc.patch
> > 
> > If your mailer cannot preserve whitespace as-is then please try attaching
> > the patch file instead of inlining it.
> > 
> > Thanks!




Index: sbin/ifconfig/ifconfig.8
===================================================================
RCS file: /cvs/src/sbin/ifconfig/ifconfig.8,v
retrieving revision 1.384
diff -u -p -u -r1.384 ifconfig.8
--- sbin/ifconfig/ifconfig.8    27 Jun 2022 16:27:03 -0000      1.384
+++ sbin/ifconfig/ifconfig.8    13 Jul 2022 14:53:22 -0000
@@ -2251,6 +2251,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: sbin/ifconfig/ifconfig.c
===================================================================
RCS file: /cvs/src/sbin/ifconfig/ifconfig.c,v
retrieving revision 1.456
diff -u -p -u -r1.456 ifconfig.c
--- sbin/ifconfig/ifconfig.c    8 Jul 2022 07:04:54 -0000       1.456
+++ sbin/ifconfig/ifconfig.c    13 Jul 2022 14:53:22 -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();
@@ -620,11 +622,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 */
@@ -5843,6 +5847,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;
@@ -5889,6 +5903,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();
@@ -5947,6 +5970,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: sys/net/if_wg.c
===================================================================
RCS file: /cvs/src/sys/net/if_wg.c,v
retrieving revision 1.25
diff -u -p -u -r1.25 if_wg.c
--- sys/net/if_wg.c     6 Jun 2022 14:45:41 -0000       1.25
+++ sys/net/if_wg.c     13 Jul 2022 14:53:22 -0000
@@ -221,6 +221,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 {
@@ -275,6 +278,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 *
@@ -408,6 +412,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));
 
@@ -582,6 +589,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)
 {
@@ -2321,6 +2337,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)
@@ -2430,6 +2450,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: sys/net/if_wg.h
===================================================================
RCS file: /cvs/src/sys/net/if_wg.h,v
retrieving revision 1.4
diff -u -p -u -r1.4 if_wg.h
--- sys/net/if_wg.h     22 Jun 2020 12:20:44 -0000      1.4
+++ sys/net/if_wg.h     13 Jul 2022 14:53:22 -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[];
 };


-- 
Regards,
 Mikolaj

Reply via email to