git: 287a5fdcd3c9 - main - ifconfig/ifbridge.c: add get_vlan_id()

From: Lexi Winter <ivy_at_FreeBSD.org>
Date: Mon, 28 Jul 2025 14:09:47 UTC
The branch main has been updated by ivy:

URL: https://cgit.FreeBSD.org/src/commit/?id=287a5fdcd3c941ce73705c664b5df4932ba3bad4

commit 287a5fdcd3c941ce73705c664b5df4932ba3bad4
Author:     Lexi Winter <ivy@FreeBSD.org>
AuthorDate: 2025-07-28 14:09:29 +0000
Commit:     Lexi Winter <ivy@FreeBSD.org>
CommitDate: 2025-07-28 14:09:29 +0000

    ifconfig/ifbridge.c: add get_vlan_id()
    
    This is like get_val() but takes an ether_vlanid_t* and ensures the
    value is a valid VLAN ID. This avoids redundant comparisons and
    casting when parsing VLAN IDs.
    
    Reviewed by:    des
    Differential Revision:  https://reviews.freebsd.org/D51548
---
 sbin/ifconfig/ifbridge.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/sbin/ifconfig/ifbridge.c b/sbin/ifconfig/ifbridge.c
index 3465dc223ada..9b46cc70c409 100644
--- a/sbin/ifconfig/ifbridge.c
+++ b/sbin/ifconfig/ifbridge.c
@@ -79,6 +79,20 @@ get_val(const char *cp, u_long *valp)
 	return (0);
 }
 
+static int
+get_vlan_id(const char *cp, ether_vlanid_t *valp)
+{
+	u_long val;
+
+	if (get_val(cp, &val) == -1)
+		return (-1);
+	if (val < DOT1Q_VID_MIN || val > DOT1Q_VID_MAX)
+		return (-1);
+
+	*valp = (ether_vlanid_t)val;
+	return (0);
+}
+
 static int
 do_cmd(if_ctx *ctx, u_long op, void *arg, size_t argsize, int set)
 {
@@ -614,23 +628,13 @@ static void
 setbridge_untagged(if_ctx *ctx, const char *ifn, const char *vlanid)
 {
 	struct ifbreq req;
-	u_long val;
 
 	memset(&req, 0, sizeof(req));
+	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
 
-	if (get_val(vlanid, &val) < 0)
+	if (get_vlan_id(vlanid, &req.ifbr_untagged) < 0)
 		errx(1, "invalid VLAN identifier: %s", vlanid);
 
-	/*
-	 * Reject vlan 0, since it's not a valid vlan identifier and has a
-	 * special meaning in the kernel interface.
-	 */
-	if (val == 0)
-		errx(1, "invalid VLAN identifier: %lu", val);
-
-	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
-	req.ifbr_untagged = val;
-
 	if (do_cmd(ctx, BRDGSIFUNTAGGED, &req, sizeof(req), 1) < 0)
 		err(1, "BRDGSIFUNTAGGED %s", vlanid);
 }