Page MenuHomeFreeBSD

wg: Improve wg_peer_alloc() to simplify the calling
ClosedPublic

Authored by kevans on Apr 12 2025, 3:08 AM.
Tags
None
Referenced Files
Unknown Object (File)
Thu, Oct 23, 6:14 AM
Unknown Object (File)
Wed, Oct 22, 6:30 PM
Unknown Object (File)
Mon, Oct 20, 5:33 PM
Unknown Object (File)
Sat, Oct 18, 11:24 AM
Unknown Object (File)
Wed, Oct 8, 9:28 PM
Unknown Object (File)
Sun, Oct 5, 10:24 AM
Unknown Object (File)
Fri, Oct 3, 4:17 PM
Unknown Object (File)
Fri, Oct 3, 12:30 PM
Subscribers

Details

Summary

Move the necessary extra logics (i.e., noise_remote_enable() and
TAILQ_INSERT_TAIL()) from wg_ioctl_set() to wg_peer_alloc(), and thus
make it easier to be called. Actually, the updated version is more
asymmetric to wg_peer_destroy() and thus less likely to be misused.
Meanwhile, rename it to wg_peer_create() to look more consistent with
wg_peer_destroy().

Obtained from: DragonflyBSD

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 63535
Build 60419: arc lint + arc unit

Event Timeline

Replaces D46670; I've set the author to this one to Aaron as I've only made two very minor tweaks from what appears in DragonflyBSD:

  • We return ENOSPC if wg_peer_create() failed, since all of the allocations are M_WAITOK... this is perhaps a bit ugly
  • Retained the need_insert notion, renamed it to need_cleanup... I went for the minimally invasive change, haven't looked at the rest of the diff from DragonflyBSD.

Replaces D46670; I've set the author to this one to Aaron as I've only made two very minor tweaks from what appears in DragonflyBSD:

Thank you very much.

  • We return ENOSPC if wg_peer_create() failed, since all of the allocations are M_WAITOK... this is perhaps a bit ugly

Yah, I now see it; was unaware of this.

  • Retained the need_insert notion, renamed it to need_cleanup... I went for the minimally invasive change, haven't looked at the rest of the diff from DragonflyBSD.

I decided to be lazy and not to clean the new but unconfigured peer (e.g., invalid sockaddr, aip failure), because the user would retry to configure the peer.

sys/dev/wg/if_wg.c
393

Need to add noise_remote_free(peer->p_remote, NULL) before free(). I missed this in DragonFly; will fix it later.

2429

The comment is inaccurate. I suggest "wg_peer_create() can only fail because noise_remote_enable() failed and can only return ENOSPC".

Hmm, I think it maybe better to change it to:

static struct wg_peer *
wg_peer_create(struct wg_softc *sc, const uint8_t pub_key[WG_KEY_SIZE], int *errp);

I personally prefer it over int wg_peer_create(struct wg_peer **peerp, ...).

kevans marked 2 inline comments as done.

Address review feedback:

  • Free p_remote if we failed to enable it
  • Propagate the error from noise_remote_enable() more explicitly

Commit message locally is updated to note the actual DragonflyBSD commit in the
Obtained from: line and also to note that it's (with some changes).

Cool. I'll also tweak wg_peer_create() in DragonFly to propagate the error. Thank you.

This revision is now accepted and ready to land.Apr 16 2025, 11:18 PM