git: 85e0016a9730 - main - ifconfig: remove global 'name' variable.

From: Alexander V. Chernikov <melifaro_at_FreeBSD.org>
Date: Tue, 13 Jun 2023 06:27:02 UTC
The branch main has been updated by melifaro:

URL: https://cgit.FreeBSD.org/src/commit/?id=85e0016a9730b6540d729bfe41ddc33eaedfee69

commit 85e0016a9730b6540d729bfe41ddc33eaedfee69
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2023-06-13 06:22:29 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-06-13 06:22:41 +0000

    ifconfig: remove global 'name' variable.
    
    Consistenly use newly-added 'ctx->ifname' as the name of the current
    target interface.
    
    Reviewed By: kp
    Differential Revision: https://reviews.freebsd.org/D40438
    MFC after:      2 weeks
---
 sbin/ifconfig/af_inet.c          |   8 +-
 sbin/ifconfig/af_inet6.c         |   8 +-
 sbin/ifconfig/carp.c             |   8 +-
 sbin/ifconfig/ifbridge.c         | 100 +++---
 sbin/ifconfig/ifclone.c          |   4 +-
 sbin/ifconfig/ifconfig.c         |  76 ++---
 sbin/ifconfig/ifconfig.h         |   9 +-
 sbin/ifconfig/ifconfig_netlink.c |  24 +-
 sbin/ifconfig/iffib.c            |   8 +-
 sbin/ifconfig/ifgre.c            |   4 +-
 sbin/ifconfig/ifgroup.c          |  14 +-
 sbin/ifconfig/ifieee80211.c      | 644 ++++++++++++++++++---------------------
 sbin/ifconfig/ifipsec.c          |   2 +-
 sbin/ifconfig/iflagg.c           |  33 +-
 sbin/ifconfig/ifmac.c            |   4 +-
 sbin/ifconfig/ifmedia.c          |  32 +-
 sbin/ifconfig/sfp.c              |   8 +-
 17 files changed, 467 insertions(+), 519 deletions(-)

diff --git a/sbin/ifconfig/af_inet.c b/sbin/ifconfig/af_inet.c
index 0538f9415272..842fdad2a20f 100644
--- a/sbin/ifconfig/af_inet.c
+++ b/sbin/ifconfig/af_inet.c
@@ -347,7 +347,7 @@ in_delete_first_nl(if_ctx *ctx)
 	struct snl_state *ss = ctx->io_ss;
 	bool found = false;
 
-	uint32_t ifindex = if_nametoindex_nl(ss, name);
+	uint32_t ifindex = if_nametoindex_nl(ss, ctx->ifname);
 	if (ifindex == 0) {
 		/* No interface with the desired name, nothing to delete */
 		return (EADDRNOTAVAIL);
@@ -417,7 +417,7 @@ in_exec_nl(if_ctx *ctx, unsigned long action, void *data)
 
 	ifahdr->ifa_family = AF_INET;
 	ifahdr->ifa_prefixlen = pdata->addr.plen;
-	ifahdr->ifa_index = if_nametoindex_nl(ctx->io_ss, name);
+	ifahdr->ifa_index = if_nametoindex_nl(ctx->io_ss, ctx->ifname);
 
 	snl_add_msg_attr_ip4(&nw, IFA_LOCAL, &pdata->addr.addr);
 	if (action == NL_RTM_NEWADDR && pdata->dst_addr.addrset)
@@ -498,7 +498,7 @@ in_status_tunnel(if_ctx *ctx)
 	const struct sockaddr *sa = (const struct sockaddr *) &ifr.ifr_addr;
 
 	memset(&ifr, 0, sizeof(ifr));
-	strlcpy(ifr.ifr_name, name, IFNAMSIZ);
+	strlcpy(ifr.ifr_name, ctx->ifname, IFNAMSIZ);
 
 	if (ioctl_ctx(ctx, SIOCGIFPSRCADDR, (caddr_t)&ifr) < 0)
 		return;
@@ -523,7 +523,7 @@ in_set_tunnel(if_ctx *ctx, struct addrinfo *srcres, struct addrinfo *dstres)
 	struct in_aliasreq addreq;
 
 	memset(&addreq, 0, sizeof(addreq));
-	strlcpy(addreq.ifra_name, name, IFNAMSIZ);
+	strlcpy(addreq.ifra_name, ctx->ifname, IFNAMSIZ);
 	memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
 	memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len);
 
diff --git a/sbin/ifconfig/af_inet6.c b/sbin/ifconfig/af_inet6.c
index 610fd70c04c1..7a771a885412 100644
--- a/sbin/ifconfig/af_inet6.c
+++ b/sbin/ifconfig/af_inet6.c
@@ -199,7 +199,7 @@ setip6eui64(if_ctx *ctx, const char *cmd, int dummy __unused)
 		err(EXIT_FAILURE, "getifaddrs");
 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
-		    strcmp(ifa->ifa_name, name) == 0) {
+		    strcmp(ifa->ifa_name, ctx->ifname) == 0) {
 			sin6 = (const struct sockaddr_in6 *)satosin6(ifa->ifa_addr);
 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
 				lladdr = &sin6->sin6_addr;
@@ -477,7 +477,7 @@ in6_exec_nl(if_ctx *ctx, unsigned long action, void *data)
 
 	ifahdr->ifa_family = AF_INET6;
 	ifahdr->ifa_prefixlen = pdata->addr.plen;
-	ifahdr->ifa_index = if_nametoindex_nl(ctx->io_ss, name);
+	ifahdr->ifa_index = if_nametoindex_nl(ctx->io_ss, ctx->ifname);
 
 	snl_add_msg_attr_ip6(&nw, IFA_LOCAL, &pdata->addr.addr);
 	if (action == NL_RTM_NEWADDR && pdata->dst_addr.set)
@@ -655,7 +655,7 @@ in6_status_tunnel(if_ctx *ctx)
 	const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
 
 	memset(&in6_ifr, 0, sizeof(in6_ifr));
-	strlcpy(in6_ifr.ifr_name, name, sizeof(in6_ifr.ifr_name));
+	strlcpy(in6_ifr.ifr_name, ctx->ifname, sizeof(in6_ifr.ifr_name));
 
 	if (ioctl_ctx(ctx, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
 		return;
@@ -682,7 +682,7 @@ in6_set_tunnel(if_ctx *ctx, struct addrinfo *srcres, struct addrinfo *dstres)
 	struct in6_aliasreq in6_addreq; 
 
 	memset(&in6_addreq, 0, sizeof(in6_addreq));
-	strlcpy(in6_addreq.ifra_name, name, sizeof(in6_addreq.ifra_name));
+	strlcpy(in6_addreq.ifra_name, ctx->ifname, sizeof(in6_addreq.ifra_name));
 	memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
 	memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
 	    dstres->ai_addr->sa_len);
diff --git a/sbin/ifconfig/carp.c b/sbin/ifconfig/carp.c
index 9de8177d210e..30df82481441 100644
--- a/sbin/ifconfig/carp.c
+++ b/sbin/ifconfig/carp.c
@@ -76,7 +76,7 @@ carp_status(if_ctx *ctx)
 	struct ifconfig_carp carpr[CARP_MAXVHID];
 	char addr_buf[NI_MAXHOST];
 
-	if (ifconfig_carp_get_info(lifh, name, carpr, CARP_MAXVHID) == -1)
+	if (ifconfig_carp_get_info(lifh, ctx->ifname, carpr, CARP_MAXVHID) == -1)
 		return;
 
 	for (size_t i = 0; i < carpr[0].carpr_count; i++) {
@@ -114,11 +114,11 @@ setcarp_vhid(if_ctx *ctx, const char *val, int dummy __unused)
 }
 
 static void
-setcarp_callback(if_ctx *ctx __unused, void *arg __unused)
+setcarp_callback(if_ctx *ctx, void *arg __unused)
 {
 	struct ifconfig_carp carpr = { };
 
-	if (ifconfig_carp_get_vhid(lifh, name, &carpr, carpr_vhid) == -1) {
+	if (ifconfig_carp_get_vhid(lifh, ctx->ifname, &carpr, carpr_vhid) == -1) {
 		if (ifconfig_err_errno(lifh) != ENOENT)
 			return;
 	}
@@ -139,7 +139,7 @@ setcarp_callback(if_ctx *ctx __unused, void *arg __unused)
 		memcpy(&carpr.carpr_addr6, &carp_addr6,
 		    sizeof(carp_addr6));
 
-	if (ifconfig_carp_set_info(lifh, name, &carpr))
+	if (ifconfig_carp_set_info(lifh, ctx->ifname, &carpr))
 		err(1, "SIOCSVH");
 }
 
diff --git a/sbin/ifconfig/ifbridge.c b/sbin/ifconfig/ifbridge.c
index c8e7111e6722..3a97a5af3931 100644
--- a/sbin/ifconfig/ifbridge.c
+++ b/sbin/ifconfig/ifbridge.c
@@ -85,28 +85,26 @@ get_val(const char *cp, u_long *valp)
 }
 
 static int
-do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
+do_cmd(if_ctx *ctx, u_long op, void *arg, size_t argsize, int set)
 {
-	struct ifdrv ifd;
+	struct ifdrv ifd = {};
 
-	memset(&ifd, 0, sizeof(ifd));
-
-	strlcpy(ifd.ifd_name, name, sizeof(ifd.ifd_name));
+	strlcpy(ifd.ifd_name, ctx->ifname, sizeof(ifd.ifd_name));
 	ifd.ifd_cmd = op;
 	ifd.ifd_len = argsize;
 	ifd.ifd_data = arg;
 
-	return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
+	return (ioctl_ctx(ctx, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
 }
 
 static void
-do_bridgeflag(int sock, const char *ifs, int flag, int set)
+do_bridgeflag(if_ctx *ctx, const char *ifs, int flag, int set)
 {
 	struct ifbreq req;
 
 	strlcpy(req.ifbr_ifsname, ifs, sizeof(req.ifbr_ifsname));
 
-	if (do_cmd(sock, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
+	if (do_cmd(ctx, BRDGGIFFLGS, &req, sizeof(req), 0) < 0)
 		err(1, "unable to get bridge flags");
 
 	if (set)
@@ -114,12 +112,12 @@ do_bridgeflag(int sock, const char *ifs, int flag, int set)
 	else
 		req.ifbr_ifsflags &= ~flag;
 
-	if (do_cmd(sock, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
+	if (do_cmd(ctx, BRDGSIFFLGS, &req, sizeof(req), 1) < 0)
 		err(1, "unable to set bridge flags");
 }
 
 static void
-bridge_addresses(int s, const char *prefix)
+bridge_addresses(if_ctx *ctx, const char *prefix)
 {
 	struct ifbaconf ifbac;
 	struct ifbareq *ifba;
@@ -133,7 +131,7 @@ bridge_addresses(int s, const char *prefix)
 			err(1, "unable to allocate address buffer");
 		ifbac.ifbac_len = len;
 		ifbac.ifbac_buf = inbuf = ninbuf;
-		if (do_cmd(s, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
+		if (do_cmd(ctx, BRDGRTS, &ifbac, sizeof(ifbac), 0) < 0)
 			err(1, "unable to get address cache");
 		if ((ifbac.ifbac_len + sizeof(*ifba)) < len)
 			break;
@@ -154,7 +152,7 @@ bridge_addresses(int s, const char *prefix)
 }
 
 static void
-bridge_status(if_ctx *ctx __unused)
+bridge_status(if_ctx *ctx)
 {
 	struct ifconfig_bridge_status *bridge;
 	struct ifbropreq *params;
@@ -162,7 +160,7 @@ bridge_status(if_ctx *ctx __unused)
 	uint8_t lladdr[ETHER_ADDR_LEN];
 	uint16_t bprio;
 
-	if (ifconfig_bridge_get_bridge_status(lifh, name, &bridge) == -1)
+	if (ifconfig_bridge_get_bridge_status(lifh, ctx->ifname, &bridge) == -1)
 		return;
 
 	params = bridge->params;
@@ -231,7 +229,7 @@ setbridge_add(if_ctx *ctx, const char *val, int dummy __unused)
 
 	memset(&req, 0, sizeof(req));
 	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
-	if (do_cmd(ctx->io_s, BRDGADD, &req, sizeof(req), 1) < 0)
+	if (do_cmd(ctx, BRDGADD, &req, sizeof(req), 1) < 0)
 		err(1, "BRDGADD %s",  val);
 }
 
@@ -242,7 +240,7 @@ setbridge_delete(if_ctx *ctx, const char *val, int dummy __unused)
 
 	memset(&req, 0, sizeof(req));
 	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
-	if (do_cmd(ctx->io_s, BRDGDEL, &req, sizeof(req), 1) < 0)
+	if (do_cmd(ctx, BRDGDEL, &req, sizeof(req), 1) < 0)
 		err(1, "BRDGDEL %s",  val);
 }
 
@@ -250,42 +248,42 @@ static void
 setbridge_discover(if_ctx *ctx, const char *val, int dummy __unused)
 {
 
-	do_bridgeflag(ctx->io_s, val, IFBIF_DISCOVER, 1);
+	do_bridgeflag(ctx, val, IFBIF_DISCOVER, 1);
 }
 
 static void
 unsetbridge_discover(if_ctx *ctx, const char *val, int dummy __unused)
 {
 
-	do_bridgeflag(ctx->io_s, val, IFBIF_DISCOVER, 0);
+	do_bridgeflag(ctx, val, IFBIF_DISCOVER, 0);
 }
 
 static void
 setbridge_learn(if_ctx *ctx, const char *val, int dummy __unused)
 {
 
-	do_bridgeflag(ctx->io_s, val, IFBIF_LEARNING,  1);
+	do_bridgeflag(ctx, val, IFBIF_LEARNING,  1);
 }
 
 static void
 unsetbridge_learn(if_ctx *ctx, const char *val, int dummy __unused)
 {
 
-	do_bridgeflag(ctx->io_s, val, IFBIF_LEARNING,  0);
+	do_bridgeflag(ctx, val, IFBIF_LEARNING,  0);
 }
 
 static void
 setbridge_sticky(if_ctx *ctx, const char *val, int dummy __unused)
 {
 
-	do_bridgeflag(ctx->io_s, val, IFBIF_STICKY,  1);
+	do_bridgeflag(ctx, val, IFBIF_STICKY,  1);
 }
 
 static void
 unsetbridge_sticky(if_ctx *ctx, const char *val, int dummy __unused)
 {
 
-	do_bridgeflag(ctx->io_s, val, IFBIF_STICKY,  0);
+	do_bridgeflag(ctx, val, IFBIF_STICKY,  0);
 }
 
 static void
@@ -295,7 +293,7 @@ setbridge_span(if_ctx *ctx, const char *val, int dummy __unused)
 
 	memset(&req, 0, sizeof(req));
 	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
-	if (do_cmd(ctx->io_s, BRDGADDS, &req, sizeof(req), 1) < 0)
+	if (do_cmd(ctx, BRDGADDS, &req, sizeof(req), 1) < 0)
 		err(1, "BRDGADDS %s",  val);
 }
 
@@ -306,7 +304,7 @@ unsetbridge_span(if_ctx *ctx, const char *val, int dummy __unused)
 
 	memset(&req, 0, sizeof(req));
 	strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
-	if (do_cmd(ctx->io_s, BRDGDELS, &req, sizeof(req), 1) < 0)
+	if (do_cmd(ctx, BRDGDELS, &req, sizeof(req), 1) < 0)
 		err(1, "BRDGDELS %s",  val);
 }
 
@@ -314,62 +312,62 @@ static void
 setbridge_stp(if_ctx *ctx, const char *val, int dummy __unused)
 {
 
-	do_bridgeflag(ctx->io_s, val, IFBIF_STP, 1);
+	do_bridgeflag(ctx, val, IFBIF_STP, 1);
 }
 
 static void
 unsetbridge_stp(if_ctx *ctx, const char *val, int dummy __unused)
 {
 
-	do_bridgeflag(ctx->io_s, val, IFBIF_STP, 0);
+	do_bridgeflag(ctx, val, IFBIF_STP, 0);
 }
 
 static void
 setbridge_edge(if_ctx *ctx, const char *val, int dummy __unused)
 {
-	do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_EDGE, 1);
+	do_bridgeflag(ctx, val, IFBIF_BSTP_EDGE, 1);
 }
 
 static void
 unsetbridge_edge(if_ctx *ctx, const char *val, int dummy __unused)
 {
-	do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_EDGE, 0);
+	do_bridgeflag(ctx, val, IFBIF_BSTP_EDGE, 0);
 }
 
 static void
 setbridge_autoedge(if_ctx *ctx, const char *val, int dummy __unused)
 {
-	do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_AUTOEDGE, 1);
+	do_bridgeflag(ctx, val, IFBIF_BSTP_AUTOEDGE, 1);
 }
 
 static void
 unsetbridge_autoedge(if_ctx *ctx, const char *val, int dummy __unused)
 {
-	do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_AUTOEDGE, 0);
+	do_bridgeflag(ctx, val, IFBIF_BSTP_AUTOEDGE, 0);
 }
 
 static void
 setbridge_ptp(if_ctx *ctx, const char *val, int dummy __unused)
 {
-	do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_PTP, 1);
+	do_bridgeflag(ctx, val, IFBIF_BSTP_PTP, 1);
 }
 
 static void
 unsetbridge_ptp(if_ctx *ctx, const char *val, int dummy __unused)
 {
-	do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_PTP, 0);
+	do_bridgeflag(ctx, val, IFBIF_BSTP_PTP, 0);
 }
 
 static void
 setbridge_autoptp(if_ctx *ctx, const char *val, int dummy __unused)
 {
-	do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_AUTOPTP, 1);
+	do_bridgeflag(ctx, val, IFBIF_BSTP_AUTOPTP, 1);
 }
 
 static void
 unsetbridge_autoptp(if_ctx *ctx, const char *val, int dummy __unused)
 {
-	do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_AUTOPTP, 0);
+	do_bridgeflag(ctx, val, IFBIF_BSTP_AUTOPTP, 0);
 }
 
 static void
@@ -379,7 +377,7 @@ setbridge_flush(if_ctx *ctx, const char *val __unused, int dummy __unused)
 
 	memset(&req, 0, sizeof(req));
 	req.ifbr_ifsflags = IFBF_FLUSHDYN;
-	if (do_cmd(ctx->io_s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
+	if (do_cmd(ctx, BRDGFLUSH, &req, sizeof(req), 1) < 0)
 		err(1, "BRDGFLUSH");
 }
 
@@ -390,7 +388,7 @@ setbridge_flushall(if_ctx *ctx, const char *val __unused, int dummy __unused)
 
 	memset(&req, 0, sizeof(req));
 	req.ifbr_ifsflags = IFBF_FLUSHALL;
-	if (do_cmd(ctx->io_s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
+	if (do_cmd(ctx, BRDGFLUSH, &req, sizeof(req), 1) < 0)
 		err(1, "BRDGFLUSH");
 }
 
@@ -411,7 +409,7 @@ setbridge_static(if_ctx *ctx, const char *val, const char *mac)
 	req.ifba_flags = IFBAF_STATIC;
 	req.ifba_vlan = 1; /* XXX allow user to specify */
 
-	if (do_cmd(ctx->io_s, BRDGSADDR, &req, sizeof(req), 1) < 0)
+	if (do_cmd(ctx, BRDGSADDR, &req, sizeof(req), 1) < 0)
 		err(1, "BRDGSADDR %s",  val);
 }
 
@@ -429,7 +427,7 @@ setbridge_deladdr(if_ctx *ctx, const char *val, int dummy __unused)
 
 	memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
 
-	if (do_cmd(ctx->io_s, BRDGDADDR, &req, sizeof(req), 1) < 0)
+	if (do_cmd(ctx, BRDGDADDR, &req, sizeof(req), 1) < 0)
 		err(1, "BRDGDADDR %s",  val);
 }
 
@@ -437,7 +435,7 @@ static void
 setbridge_addr(if_ctx *ctx, const char *val __unused, int dummy __unused)
 {
 
-	bridge_addresses(ctx->io_s, "");
+	bridge_addresses(ctx, "");
 }
 
 static void
@@ -451,7 +449,7 @@ setbridge_maxaddr(if_ctx *ctx, const char *arg, int dummy __unused)
 
 	param.ifbrp_csize = val & 0xffffffff;
 
-	if (do_cmd(ctx->io_s, BRDGSCACHE, &param, sizeof(param), 1) < 0)
+	if (do_cmd(ctx, BRDGSCACHE, &param, sizeof(param), 1) < 0)
 		err(1, "BRDGSCACHE %s",  arg);
 }
 
@@ -466,7 +464,7 @@ setbridge_hellotime(if_ctx *ctx, const char *arg, int dummy __unused)
 
 	param.ifbrp_hellotime = val & 0xff;
 
-	if (do_cmd(ctx->io_s, BRDGSHT, &param, sizeof(param), 1) < 0)
+	if (do_cmd(ctx, BRDGSHT, &param, sizeof(param), 1) < 0)
 		err(1, "BRDGSHT %s",  arg);
 }
 
@@ -481,7 +479,7 @@ setbridge_fwddelay(if_ctx *ctx, const char *arg, int dummy __unused)
 
 	param.ifbrp_fwddelay = val & 0xff;
 
-	if (do_cmd(ctx->io_s, BRDGSFD, &param, sizeof(param), 1) < 0)
+	if (do_cmd(ctx, BRDGSFD, &param, sizeof(param), 1) < 0)
 		err(1, "BRDGSFD %s",  arg);
 }
 
@@ -496,7 +494,7 @@ setbridge_maxage(if_ctx *ctx, const char *arg, int dummy __unused)
 
 	param.ifbrp_maxage = val & 0xff;
 
-	if (do_cmd(ctx->io_s, BRDGSMA, &param, sizeof(param), 1) < 0)
+	if (do_cmd(ctx, BRDGSMA, &param, sizeof(param), 1) < 0)
 		err(1, "BRDGSMA %s",  arg);
 }
 
@@ -511,7 +509,7 @@ setbridge_priority(if_ctx *ctx, const char *arg, int dummy __unused)
 
 	param.ifbrp_prio = val & 0xffff;
 
-	if (do_cmd(ctx->io_s, BRDGSPRI, &param, sizeof(param), 1) < 0)
+	if (do_cmd(ctx, BRDGSPRI, &param, sizeof(param), 1) < 0)
 		err(1, "BRDGSPRI %s",  arg);
 }
 
@@ -528,7 +526,7 @@ setbridge_protocol(if_ctx *ctx, const char *arg, int dummy __unused)
 		errx(1, "unknown stp protocol");
 	}
 
-	if (do_cmd(ctx->io_s, BRDGSPROTO, &param, sizeof(param), 1) < 0)
+	if (do_cmd(ctx, BRDGSPROTO, &param, sizeof(param), 1) < 0)
 		err(1, "BRDGSPROTO %s",  arg);
 }
 
@@ -543,7 +541,7 @@ setbridge_holdcount(if_ctx *ctx, const char *arg, int dummy __unused)
 
 	param.ifbrp_txhc = val & 0xff;
 
-	if (do_cmd(ctx->io_s, BRDGSTXHC, &param, sizeof(param), 1) < 0)
+	if (do_cmd(ctx, BRDGSTXHC, &param, sizeof(param), 1) < 0)
 		err(1, "BRDGSTXHC %s",  arg);
 }
 
@@ -561,7 +559,7 @@ setbridge_ifpriority(if_ctx *ctx, const char *ifn, const char *pri)
 	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
 	req.ifbr_priority = val & 0xff;
 
-	if (do_cmd(ctx->io_s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
+	if (do_cmd(ctx, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
 		err(1, "BRDGSIFPRIO %s",  pri);
 }
 
@@ -579,7 +577,7 @@ setbridge_ifpathcost(if_ctx *ctx, const char *ifn, const char *cost)
 	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
 	req.ifbr_path_cost = val;
 
-	if (do_cmd(ctx->io_s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
+	if (do_cmd(ctx, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
 		err(1, "BRDGSIFCOST %s",  cost);
 }
 
@@ -597,7 +595,7 @@ setbridge_ifmaxaddr(if_ctx *ctx, const char *ifn, const char *arg)
 	strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
 	req.ifbr_addrmax = val & 0xffffffff;
 
-	if (do_cmd(ctx->io_s, BRDGSIFAMAX, &req, sizeof(req), 1) < 0)
+	if (do_cmd(ctx, BRDGSIFAMAX, &req, sizeof(req), 1) < 0)
 		err(1, "BRDGSIFAMAX %s",  arg);
 }
 
@@ -612,7 +610,7 @@ setbridge_timeout(if_ctx *ctx, const char *arg, int dummy __unused)
 
 	param.ifbrp_ctime = val & 0xffffffff;
 
-	if (do_cmd(ctx->io_s, BRDGSTO, &param, sizeof(param), 1) < 0)
+	if (do_cmd(ctx, BRDGSTO, &param, sizeof(param), 1) < 0)
 		err(1, "BRDGSTO %s",  arg);
 }
 
@@ -620,14 +618,14 @@ static void
 setbridge_private(if_ctx *ctx, const char *val, int dummy __unused)
 {
 
-	do_bridgeflag(ctx->io_s, val, IFBIF_PRIVATE, 1);
+	do_bridgeflag(ctx, val, IFBIF_PRIVATE, 1);
 }
 
 static void
 unsetbridge_private(if_ctx *ctx, const char *val, int dummy __unused)
 {
 
-	do_bridgeflag(ctx->io_s, val, IFBIF_PRIVATE, 0);
+	do_bridgeflag(ctx, val, IFBIF_PRIVATE, 0);
 }
 
 static struct cmd bridge_cmds[] = {
diff --git a/sbin/ifconfig/ifclone.c b/sbin/ifconfig/ifclone.c
index f09b91b4181b..ecd8bbc2e556 100644
--- a/sbin/ifconfig/ifclone.c
+++ b/sbin/ifconfig/ifclone.c
@@ -123,7 +123,7 @@ ifclonecreate(if_ctx *ctx, void *arg __unused)
 	struct ifreq ifr = {};
 	struct clone_defcb *dcp;
 
-	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+	strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));
 
 	/* Try to find a default callback by filter */
 	SLIST_FOREACH(dcp, &clone_defcbh, next) {
@@ -159,7 +159,7 @@ clone_create(if_ctx *ctx, const char *cmd __unused, int d __unused)
 static void
 clone_destroy(if_ctx *ctx, const char *cmd __unused, int d __unused)
 {
-	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+	strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));
 	if (ioctl(ctx->io_s, SIOCIFDESTROY, &ifr) < 0)
 		err(1, "SIOCIFDESTROY");
 }
diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c
index a250062d69e1..0ccca3666bed 100644
--- a/sbin/ifconfig/ifconfig.c
+++ b/sbin/ifconfig/ifconfig.c
@@ -92,7 +92,7 @@ ifconfig_handle_t *lifh;
  */
 struct	ifreq ifr;
 
-char	name[IFNAMSIZ];
+//char	name[IFNAMSIZ];
 #ifdef WITHOUT_NETLINK
 static char	*descr = NULL;
 static size_t	descrlen = 64;
@@ -576,15 +576,16 @@ static int
 ifconfig_wrapper(struct ifconfig_args *args, int iscreate,
     const struct afswtch *uafp)
 {
-#ifdef WITHOUT_NETLINK
 	struct ifconfig_context ctx = {
 		.args = args,
 		.io_s = -1,
+		.ifname = args->ifname,
 	};
 
+#ifdef WITHOUT_NETLINK
 	return (ifconfig(&ctx, iscreate, uafp));
 #else
-	return (ifconfig_wrapper_nl(args, iscreate, uafp));
+	return (ifconfig_wrapper_nl(&ctx, iscreate, uafp));
 #endif
 }
 
@@ -600,11 +601,16 @@ isargcreate(const char *arg)
 	return (false);
 }
 
+static bool
+isnametoolong(const char *ifname)
+{
+	return (strlen(ifname) >= IFNAMSIZ);
+}
+
 int
 main(int ac, char *av[])
 {
 	char *envformat;
-	size_t iflen;
 	int flags;
 #ifdef JAIL
 	int jid;
@@ -654,8 +660,7 @@ main(int ac, char *av[])
 			 * to find the interface.
 			 */
 			if (isargcreate(arg)) {
-				iflen = strlcpy(name, args->ifname, sizeof(name));
-				if (iflen >= sizeof(name))
+				if (isnametoolong(args->ifname))
 					errx(1, "%s: cloning name too long",
 					    args->ifname);
 				ifconfig_wrapper(args, 1, NULL);
@@ -668,8 +673,7 @@ main(int ac, char *av[])
 			 * to find the interface as it lives in another vnet.
 			 */
 			if (arg != NULL && (strcmp(arg, "-vnet") == 0)) {
-				iflen = strlcpy(name, args->ifname, sizeof(name));
-				if (iflen >= sizeof(name))
+				if (isnametoolong(args->ifname))
 					errx(1, "%s: interface name too long",
 					    args->ifname);
 				ifconfig_wrapper(args, 0, NULL);
@@ -704,11 +708,10 @@ main(int ac, char *av[])
 	 * system address list
 	 */
 	if ((args->argc > 0) && (args->ifname != NULL)) {
-		iflen = strlcpy(name, args->ifname, sizeof(name));
-		if (iflen >= sizeof(name)) {
+		if (isnametoolong(args->ifname))
 			warnx("%s: interface name too long, skipping", args->ifname);
-		} else {
-			flags = getifflags(name, -1, false);
+		else {
+			flags = getifflags(args->ifname, -1, false);
 			if (!(((flags & IFF_CANTCONFIG) != 0) ||
 				(args->downonly && (flags & IFF_UP) != 0) ||
 				(args->uponly && (flags & IFF_UP) == 0)))
@@ -776,7 +779,6 @@ list_interfaces_ioctl(struct ifconfig_args *args)
 	struct ifa_order_elt *cur, *tmp;
 	char *namecp = NULL;
 	int ifindex;
-	size_t iflen;
 
 	if (getifaddrs(&ifap) != 0)
 		err(EXIT_FAILURE, "getifaddrs");
@@ -810,8 +812,7 @@ list_interfaces_ioctl(struct ifconfig_args *args)
 			sdl = NULL;
 		if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !args->namesonly)
 			continue;
-		iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
-		if (iflen >= sizeof(name)) {
+		if (isnametoolong(ifa->ifa_name)) {
 			warnx("%s: interface name too long, skipping",
 			    ifa->ifa_name);
 			continue;
@@ -834,7 +835,7 @@ list_interfaces_ioctl(struct ifconfig_args *args)
 			ifindex++;
 			if (ifindex > 1)
 				printf(" ");
-			fputs(name, stdout);
+			fputs(cp, stdout);
 			continue;
 		}
 		ifindex++;
@@ -1044,7 +1045,7 @@ af_exec_ioctl(if_ctx *ctx, unsigned long action, void *data)
 {
 	struct ifreq *req = (struct ifreq *)data;
 
-	strlcpy(req->ifr_name, name, sizeof(req->ifr_name));
+	strlcpy(req->ifr_name, ctx->ifname, sizeof(req->ifr_name));
 	if (ioctl_ctx(ctx, action, req) == 0)
 		return (0);
 	return (errno);
@@ -1057,7 +1058,7 @@ delifaddr(if_ctx *ctx, const struct afswtch *afp)
 
 	if (afp->af_exec == NULL) {
 		warnx("interface %s cannot change %s addresses!",
-		    name, afp->af_name);
+		    ctx->ifname, afp->af_name);
 		clearaddr = 0;
 		return;
 	}
@@ -1076,7 +1077,7 @@ addifaddr(if_ctx *ctx, const struct afswtch *afp)
 {
 	if (afp->af_exec == NULL) {
 		warnx("interface %s cannot change %s addresses!",
-		      name, afp->af_name);
+		      ctx->ifname, afp->af_name);
 		newaddr = 0;
 		return;
 	}
@@ -1100,10 +1101,11 @@ ifconfig(if_ctx *orig_ctx, int iscreate, const struct afswtch *uafp)
 	struct ifconfig_context _ctx = {
 		.args = orig_ctx->args,
 		.io_ss = orig_ctx->io_ss,
+		.ifname = orig_ctx->ifname,
 	};
 	struct ifconfig_context *ctx = &_ctx;
 
-	strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
+	strlcpy(ifr.ifr_name, ctx->ifname, sizeof ifr.ifr_name);
 	afp = NULL;
 	if (uafp != NULL)
 		afp = uafp;
@@ -1209,7 +1211,7 @@ top:
 	 * Do any post argument processing required by the address family.
 	 */
 	if (afp->af_postproc != NULL)
-		afp->af_postproc(ctx, newaddr, getifflags(name, s, true));
+		afp->af_postproc(ctx, newaddr, getifflags(ctx->ifname, s, true));
 	/*
 	 * Do deferred callbacks registered while processing
 	 * command-line arguments.
@@ -1394,14 +1396,14 @@ setifflags(if_ctx *ctx, const char *vname, int value)
 	struct ifreq		my_ifr;
 	int flags;
 
-	flags = getifflags(name, ctx->io_s, false);
+	flags = getifflags(ctx->ifname, ctx->io_s, false);
 	if (value < 0) {
 		value = -value;
 		flags &= ~value;
 	} else
 		flags |= value;
 	memset(&my_ifr, 0, sizeof(my_ifr));
-	(void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
+	strlcpy(my_ifr.ifr_name, ctx->ifname, sizeof(my_ifr.ifr_name));
 	my_ifr.ifr_flags = flags & 0xffff;
 	my_ifr.ifr_flagshigh = flags >> 16;
 	if (ioctl(ctx->io_s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
@@ -1482,7 +1484,7 @@ setifcapnv(if_ctx *ctx, const char *vname, const char *arg)
 static void
 setifmetric(if_ctx *ctx, const char *val, int dummy __unused)
 {
-	strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
+	strlcpy(ifr.ifr_name, ctx->ifname, sizeof (ifr.ifr_name));
 	ifr.ifr_metric = atoi(val);
 	if (ioctl(ctx->io_s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
 		err(1, "ioctl SIOCSIFMETRIC (set metric)");
@@ -1491,7 +1493,7 @@ setifmetric(if_ctx *ctx, const char *val, int dummy __unused)
 static void
 setifmtu(if_ctx *ctx, const char *val, int dummy __unused)
 {
-	strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
+	strlcpy(ifr.ifr_name, ctx->ifname, sizeof (ifr.ifr_name));
 	ifr.ifr_mtu = atoi(val);
 	if (ioctl(ctx->io_s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
 		err(1, "ioctl SIOCSIFMTU (set mtu)");
@@ -1527,7 +1529,7 @@ setifname(if_ctx *ctx, const char *val, int dummy __unused)
 {
 	char *newname;
 	
-	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+	strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));
 
 	newname = strdup(val);
 	if (newname == NULL)
@@ -1546,7 +1548,7 @@ setifdescr(if_ctx *ctx, const char *val, int dummy __unused)
 {
 	char *newdescr;
 
-	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+	strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));
 	
 	ifr.ifr_buffer.length = strlen(val) + 1;
 	if (ifr.ifr_buffer.length == 1) {
@@ -1663,12 +1665,12 @@ print_ifcap(struct ifconfig_args *args, int s)
 }
 
 void
-print_ifstatus(int s)
+print_ifstatus(if_ctx *ctx)
 {
 	struct ifstat ifs;
 
-	strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
-	if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
+	strlcpy(ifs.ifs_name, ctx->ifname, sizeof ifs.ifs_name);
+	if (ioctl_ctx(ctx, SIOCGIFSTATUS, &ifs) == 0)
 		printf("%s", ifs.ascii);
 }
 
@@ -1722,22 +1724,26 @@ status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
 	struct ifaddrs *ift;
 	int s;
 	bool allfamilies = args->afp == NULL;
+	char *ifname = ifa->ifa_name;
 
 	if (args->afp == NULL)
 		ifr.ifr_addr.sa_family = AF_LOCAL;
 	else
 		ifr.ifr_addr.sa_family =
 		   args->afp->af_af == AF_LINK ? AF_LOCAL : args->afp->af_af;
-	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
 
 	s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
 	if (s < 0)
 		err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
 
-	struct ifconfig_context _ctx = { .io_s = s }, *ctx;
-	ctx = &_ctx;
+	struct ifconfig_context _ctx = {
+		.io_s = s,
+		.ifname = ifname,
+	};
+	struct ifconfig_context *ctx = &_ctx;
 
-	printf("%s: ", name);
+	printf("%s: ", ifname);
 	printb("flags", ifa->ifa_flags, IFFBITS);
 	print_metric(s);
 	print_mtu(s);
@@ -1784,7 +1790,7 @@ status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
 	else if (args->afp->af_other_status != NULL)
 		args->afp->af_other_status(ctx);
 
-	print_ifstatus(s);
+	print_ifstatus(ctx);
 	if (args->verbose > 0)
 		sfp_status(ctx);
 
diff --git a/sbin/ifconfig/ifconfig.h b/sbin/ifconfig/ifconfig.h
index 5e0dfcd7e8c5..45d05d1bf077 100644
--- a/sbin/ifconfig/ifconfig.h
+++ b/sbin/ifconfig/ifconfig.h
@@ -60,7 +60,7 @@ struct ifconfig_context {
 	const struct afswtch	*afp;
 	int			io_s;		/* fd to use for ioctl() */
 	struct snl_state	*io_ss;		/* NETLINK_ROUTE socket */
-	char			*ifname;	/* Current interface name */
+	const char		*ifname;	/* Current interface name */
 	char			_ifname_storage_ioctl[IFNAMSIZ];
 };
 typedef struct ifconfig_context if_ctx;
@@ -251,7 +251,6 @@ void	opt_register(struct option *);
 
 extern	ifconfig_handle_t *lifh;
 extern	struct ifreq ifr;
-extern	char name[IFNAMSIZ];	/* name of interface */
 extern	int allmedia;
 extern	int newaddr;
 extern	int exit_code;
@@ -282,12 +281,12 @@ void	print_ifcap(struct ifconfig_args *args, int s);
 void	tunnel_status(if_ctx *ctx);
 struct afswtch	*af_getbyfamily(int af);
 void	af_other_status(if_ctx *ctx);
-void	print_ifstatus(int s);
+void	print_ifstatus(if_ctx *ctx);
 void	print_metric(int s);
 
 /* Netlink-related functions */
 void	list_interfaces_nl(struct ifconfig_args *args);
-int	ifconfig_wrapper_nl(struct ifconfig_args *args, int iscreate,
+int	ifconfig_wrapper_nl(if_ctx *ctx, int iscreate,
 		const struct afswtch *uafp);
 uint32_t if_nametoindex_nl(struct snl_state *ss, const char *ifname);
 
@@ -295,7 +294,7 @@ uint32_t if_nametoindex_nl(struct snl_state *ss, const char *ifname);
  * XXX expose this so modules that need to know of any pending
  * operations on ifmedia can avoid cmd line ordering confusion.
  */
-struct ifmediareq *ifmedia_getstate(void);
+struct ifmediareq *ifmedia_getstate(if_ctx *ctx);
 
 void print_vhid(const struct ifaddrs *, const char *);
 
diff --git a/sbin/ifconfig/ifconfig_netlink.c b/sbin/ifconfig/ifconfig_netlink.c
index bded84484a52..1635a4cb80c7 100644
--- a/sbin/ifconfig/ifconfig_netlink.c
+++ b/sbin/ifconfig/ifconfig_netlink.c
@@ -123,19 +123,15 @@ nl_init_socket(struct snl_state *ss)
 }
 
 int
-ifconfig_wrapper_nl(struct ifconfig_args *args, int iscreate,
+ifconfig_wrapper_nl(if_ctx *ctx, int iscreate,
     const struct afswtch *uafp)
 {
 	struct snl_state ss = {};
-	struct ifconfig_context ctx = {
-		.args = args,
-		.io_s = -1,
-		.io_ss = &ss,
-	};
 
 	nl_init_socket(&ss);
+	ctx->io_ss = &ss;
 
-	int error = ifconfig(&ctx, iscreate, uafp);
+	int error = ifconfig(ctx, iscreate, uafp);
 
 	snl_free(&ss);
 
@@ -396,7 +392,7 @@ status_nl(if_ctx *ctx, struct iface *iface)
 	else if (args->afp->af_other_status != NULL)
 		args->afp->af_other_status(ctx);
 
-	print_ifstatus(ctx->io_s);
+	print_ifstatus(ctx);
 	if (args->verbose > 0)
 		sfp_status(ctx);
 }
@@ -411,16 +407,6 @@ get_local_socket(void)
 	return (s);
 }
 
-static void
-set_global_ifname(if_link_t *link)
-{
-	size_t iflen = strlcpy(name, link->ifla_ifname, sizeof(name));
-
-	if (iflen >= sizeof(name))
-		errx(1, "%s: cloning name too long", link->ifla_ifname);
-	strlcpy(ifr.ifr_name, link->ifla_ifname, sizeof(ifr.ifr_name));
-}
-
 void
 list_interfaces_nl(struct ifconfig_args *args)
 {
@@ -452,7 +438,7 @@ list_interfaces_nl(struct ifconfig_args *args)
 		if (!match_iface(args, iface))
 			continue;
 
-		set_global_ifname(&iface->link);
+		ctx->ifname = iface->link.ifla_ifname;
 
 		if (args->namesonly) {
 			if (num++ != 0)
diff --git a/sbin/ifconfig/iffib.c b/sbin/ifconfig/iffib.c
index 4ebc8341338f..684c57c88f64 100644
--- a/sbin/ifconfig/iffib.c
+++ b/sbin/ifconfig/iffib.c
@@ -51,13 +51,13 @@ fib_status(if_ctx *ctx)
 	struct ifreq ifr;
 
 	memset(&ifr, 0, sizeof(ifr));
-	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+	strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));
 	if (ioctl_ctx(ctx, SIOCGIFFIB, (caddr_t)&ifr) == 0 &&
 	    ifr.ifr_fib != RT_DEFAULT_FIB)
 		printf("\tfib: %u\n", ifr.ifr_fib);
 
 	memset(&ifr, 0, sizeof(ifr));
-	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+	strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name));
 	if (ioctl_ctx(ctx, SIOCGTUNFIB, (caddr_t)&ifr) == 0 &&
 	    ifr.ifr_fib != RT_DEFAULT_FIB)
 		printf("\ttunnelfib: %u\n", ifr.ifr_fib);
@@ -75,7 +75,7 @@ setiffib(if_ctx *ctx, const char *val, int dummy __unused)
 		return;
 	}
 
-	strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
+	strlcpy(ifr.ifr_name, ctx->ifname, sizeof (ifr.ifr_name));
 	ifr.ifr_fib = fib;
 	if (ioctl(ctx->io_s, SIOCSIFFIB, (caddr_t)&ifr) < 0)
 		warn("ioctl (SIOCSIFFIB)");
@@ -93,7 +93,7 @@ settunfib(if_ctx *ctx, const char *val, int dummy __unused)
 		return;
 	}
 
-	strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
*** 2597 LINES SKIPPED ***