From nobody Sat Apr 26 03:21:14 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Zkw3G4ldwz5tXwG; Sat, 26 Apr 2025 03:21:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Zkw3G3w0Bz3LHc; Sat, 26 Apr 2025 03:21:14 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1745637674; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=dm45fPDXrDbnp22pPSoU4EfCsNVG6hBLsFf4Vnxz3Z8=; b=Le50r3hvGDr8NfRZQusbIUc/LrYqCACArY6eqKnQsCkXMCrI90F5Yi4j0EpyKkUJ6zbyP5 2qZGlVFn7T/Bsj1ihaXbaRvJtHbexVJRE3VBV+jiKQu5fMHU1Z23FH6x4i5TKyJ3bhZvtI Pk+lmw7QV/rxtRzW0zXMTW1XyJ9ooDG+WtgGmTvNekQbPWdZEXoPP0G3Z07Zi8FkCP8SBO 9jzBkd2IGCEM3lW6g9xRHIuXb1EXk+ZZTIyQuxTp2t6qq25F3YX6qPwsgx0+y5G8utEZbA aiagq0Lq+1DC9S+xBsTl9Kn+kKW5DpiEf5ddq5HW/JZ18HC9QkdyPDfc8PtZnQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1745637674; a=rsa-sha256; cv=none; b=USc522F0VGTyUzY9WVaM/FHQgFAkDZSIjQnhsbJYB6mHX64+9Ko4Lw11zaBggEzWl6z5Fd fFljwsa3cgGL7cGm2om4KsCVf6ofODV3bNIiscFd5A1XJEQAgbGf1h7tniX8Jo3dCLn6gQ St6tcQNmIdgCEvIdabAe1sAV2JtLhR2XyyZr2M252vOVq0Ic56KADo08gulLOanRKwKvg1 6vZLjQ7zI6iia9mqfDEQobzzCNXzalqLAykQkscyc2eTFCmhnEa9gngYG+dfyuNI1wPwJo JNaDa9+//ySPAs6HduG820wFjY5JxzoIZTYHjcD2UGBQJT/zyJd8V6aDn5jt5A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1745637674; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=dm45fPDXrDbnp22pPSoU4EfCsNVG6hBLsFf4Vnxz3Z8=; b=wbkpX69VAVRWqLSWrCLM9Vz/VuGB9NMGpJC9hP0erbh4o9+ow95NhiYEcc1FBFvNtKf+dQ jWGe/scT6DZ2z5+6jZgxIRZQT+XRcny8+StSrI+3OmnNLqUgz2UM1+vf/mAfWqZ0HBamLb SlpAIDfnZFalu5Ao/clSh8fbHtLRBtMgUr3RfAQjvTMUBfKBy7bGo57z36iB3ll7rGqoxQ e8Up7U6Pa4yca6xdlQ00w4zkQbDhagxycOUExxjbsxS0mzIDwfRQvlYNJYW8LR/RCaSm5d g4x+L3uzhVuctLXZ47X30yU/abZdWDVJvL2eG6LIIrRjMu/lQiYMxJA7uk8E3Q== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Zkw3G3WhBz12x3; Sat, 26 Apr 2025 03:21:14 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 53Q3LEmw009662; Sat, 26 Apr 2025 03:21:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 53Q3LEDq009659; Sat, 26 Apr 2025 03:21:14 GMT (envelope-from git) Date: Sat, 26 Apr 2025 03:21:14 GMT Message-Id: <202504260321.53Q3LEDq009659@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: ba560cd8fba3 - stable/14 - wg: Improve wg_peer_alloc() to simplify the calling List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: ba560cd8fba390334fd543c4f0190b72794cc6fe Auto-Submitted: auto-generated The branch stable/14 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=ba560cd8fba390334fd543c4f0190b72794cc6fe commit ba560cd8fba390334fd543c4f0190b72794cc6fe Author: Aaron LI AuthorDate: 2025-04-18 00:30:11 +0000 Commit: Kyle Evans CommitDate: 2025-04-26 03:19:47 +0000 wg: Improve wg_peer_alloc() to simplify the calling 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(). Reviewed by: aly_aaronly.me (diff), markj Obtained from: DragonflyBSD 902964ab24ba (with some changes) (cherry picked from commit 7121e9414f294d116caeadd07ebd969136d3a631) --- sys/dev/wg/if_wg.c | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/sys/dev/wg/if_wg.c b/sys/dev/wg/if_wg.c index ed23baf33a17..2534929a1e37 100644 --- a/sys/dev/wg/if_wg.c +++ b/sys/dev/wg/if_wg.c @@ -315,7 +315,8 @@ static void wg_timers_run_persistent_keepalive(void *); static int wg_aip_add(struct wg_softc *, struct wg_peer *, sa_family_t, const void *, uint8_t); static struct wg_peer *wg_aip_lookup(struct wg_softc *, sa_family_t, void *); static void wg_aip_remove_all(struct wg_softc *, struct wg_peer *); -static struct wg_peer *wg_peer_alloc(struct wg_softc *, const uint8_t [WG_KEY_SIZE]); +static struct wg_peer *wg_peer_create(struct wg_softc *, + const uint8_t [WG_KEY_SIZE], int *); static void wg_peer_free_deferred(struct noise_remote *); static void wg_peer_destroy(struct wg_peer *); static void wg_peer_destroy_all(struct wg_softc *); @@ -378,18 +379,26 @@ static void wg_module_deinit(void); /* TODO Peer */ static struct wg_peer * -wg_peer_alloc(struct wg_softc *sc, const uint8_t pub_key[WG_KEY_SIZE]) +wg_peer_create(struct wg_softc *sc, const uint8_t pub_key[WG_KEY_SIZE], + int *errp) { struct wg_peer *peer; sx_assert(&sc->sc_lock, SX_XLOCKED); peer = malloc(sizeof(*peer), M_WG, M_WAITOK | M_ZERO); + peer->p_remote = noise_remote_alloc(sc->sc_local, peer, pub_key); - peer->p_tx_bytes = counter_u64_alloc(M_WAITOK); - peer->p_rx_bytes = counter_u64_alloc(M_WAITOK); + if ((*errp = noise_remote_enable(peer->p_remote)) != 0) { + noise_remote_free(peer->p_remote, NULL); + free(peer, M_WG); + return (NULL); + } + peer->p_id = peer_counter++; peer->p_sc = sc; + peer->p_tx_bytes = counter_u64_alloc(M_WAITOK); + peer->p_rx_bytes = counter_u64_alloc(M_WAITOK); cookie_maker_init(&peer->p_cookie, pub_key); @@ -420,6 +429,13 @@ wg_peer_alloc(struct wg_softc *sc, const uint8_t pub_key[WG_KEY_SIZE]) LIST_INIT(&peer->p_aips); peer->p_aips_num = 0; + TAILQ_INSERT_TAIL(&sc->sc_peers, peer, p_entry); + sc->sc_peers_num++; + + if (if_getlinkstate(sc->sc_ifp) == LINK_STATE_UP) + wg_timers_enable(peer); + + DPRINTF(sc, "Peer %" PRIu64 " created\n", peer->p_id); return (peer); } @@ -2374,7 +2390,7 @@ wg_peer_add(struct wg_softc *sc, const nvlist_t *nvl) size_t size; struct noise_remote *remote; struct wg_peer *peer = NULL; - bool need_insert = false; + bool need_cleanup = false; sx_assert(&sc->sc_lock, SX_XLOCKED); @@ -2406,8 +2422,10 @@ wg_peer_add(struct wg_softc *sc, const nvlist_t *nvl) wg_aip_remove_all(sc, peer); } if (peer == NULL) { - peer = wg_peer_alloc(sc, pub_key); - need_insert = true; + peer = wg_peer_create(sc, pub_key, &err); + if (peer == NULL) + goto out; + need_cleanup = true; } if (nvlist_exists_binary(nvl, "endpoint")) { endpoint = nvlist_get_binary(nvl, "endpoint", &size); @@ -2465,19 +2483,11 @@ wg_peer_add(struct wg_softc *sc, const nvlist_t *nvl) } } } - if (need_insert) { - if ((err = noise_remote_enable(peer->p_remote)) != 0) - goto out; - TAILQ_INSERT_TAIL(&sc->sc_peers, peer, p_entry); - sc->sc_peers_num++; - if (if_getlinkstate(sc->sc_ifp) == LINK_STATE_UP) - wg_timers_enable(peer); - } if (remote != NULL) noise_remote_put(remote); return (0); out: - if (need_insert) /* If we fail, only destroy if it was new. */ + if (need_cleanup) /* If we fail, only destroy if it was new. */ wg_peer_destroy(peer); if (remote != NULL) noise_remote_put(remote);