The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=ba2607ae7dff17957d9e62ccd567ba716c168e77
commit ba2607ae7dff17957d9e62ccd567ba716c168e77 Author: Kyle Evans <kev...@freebsd.org> AuthorDate: 2025-06-26 02:57:02 +0000 Commit: Kyle Evans <kev...@freebsd.org> CommitDate: 2025-06-26 02:57:02 +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 Differential Revision: https://reviews.freebsd.org/D50446 --- 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 5a3b60e45b7a..ce12d623be6f 100644 --- a/sys/dev/wg/if_wg.c +++ b/sys/dev/wg/if_wg.c @@ -2461,8 +2461,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); @@ -2470,19 +2474,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)