git: 1cacf672e3ee - stable/14 - kern: wg: refactor out some repetitive bits in allowed-ip config

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Mon, 21 Jul 2025 02:13:56 UTC
The branch stable/14 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=1cacf672e3ee3719b72534347cf7d5ce69cf716f

commit 1cacf672e3ee3719b72534347cf7d5ce69cf716f
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2025-06-26 02:57:02 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2025-07-21 02:12:28 +0000

    kern: wg: refactor out some repetitive bits in allowed-ip config
    
    The only difference in the wg_aip_add() call after IP validation is the
    address family.  Just pull that out into a variable and avoid the two
    different callsites for wg_aip_add().  A future change will add a new
    call for each case to remove an address from the peer, so it's nice to
    avoid needing to repeat the logic for two different branches.
    
    Reviewed by:    Aaron LI, Jason A. Donenfeld, ivy, jhb, markj
    
    (cherry picked from commit ba2607ae7dff17957d9e62ccd567ba716c168e77)
---
 sys/dev/wg/if_wg.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/sys/dev/wg/if_wg.c b/sys/dev/wg/if_wg.c
index 2534929a1e37..9e86e1074ef5 100644
--- a/sys/dev/wg/if_wg.c
+++ b/sys/dev/wg/if_wg.c
@@ -2459,8 +2459,12 @@ wg_peer_add(struct wg_softc *sc, const nvlist_t *nvl)
 
 		aipl = nvlist_get_nvlist_array(nvl, "allowed-ips", &allowedip_count);
 		for (size_t idx = 0; idx < allowedip_count; idx++) {
+			sa_family_t ipaf;
+
 			if (!nvlist_exists_number(aipl[idx], "cidr"))
 				continue;
+
+			ipaf = AF_UNSPEC;
 			cidr = nvlist_get_number(aipl[idx], "cidr");
 			if (nvlist_exists_binary(aipl[idx], "ipv4")) {
 				addr = nvlist_get_binary(aipl[idx], "ipv4", &size);
@@ -2468,19 +2472,23 @@ wg_peer_add(struct wg_softc *sc, const nvlist_t *nvl)
 					err = EINVAL;
 					goto out;
 				}
-				if ((err = wg_aip_add(sc, peer, AF_INET, addr, cidr)) != 0)
-					goto out;
+
+				ipaf = AF_INET;
 			} else if (nvlist_exists_binary(aipl[idx], "ipv6")) {
 				addr = nvlist_get_binary(aipl[idx], "ipv6", &size);
 				if (addr == NULL || cidr > 128 || size != sizeof(struct in6_addr)) {
 					err = EINVAL;
 					goto out;
 				}
-				if ((err = wg_aip_add(sc, peer, AF_INET6, addr, cidr)) != 0)
-					goto out;
+
+				ipaf = AF_INET6;
 			} else {
 				continue;
 			}
+
+			MPASS(ipaf != AF_UNSPEC);
+			if ((err = wg_aip_add(sc, peer, ipaf, addr, cidr)) != 0)
+				goto out;
 		}
 	}
 	if (remote != NULL)