git: 74b426116181 - main - ifconfig: add if_ctx argument to the generic and ifclone callbacks.

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

URL: https://cgit.FreeBSD.org/src/commit/?id=74b426116181bdef5cc57760345d8270471b4aa8

commit 74b426116181bdef5cc57760345d8270471b4aa8
Author:     Alexander V. Chernikov <melifaro@FreeBSD.org>
AuthorDate: 2023-06-13 06:10:41 +0000
Commit:     Alexander V. Chernikov <melifaro@FreeBSD.org>
CommitDate: 2023-06-13 06:10:56 +0000

    ifconfig: add if_ctx argument to the generic and ifclone callbacks.
    
    This is the continuation of the ifconfig cleanup work. This change is
    a pre-requsite for the next changes removing some of the global variables.
    It will also help in implementing functionality via Netlink instead of ioctl.
    No functional changes intended.
    * vxlan_cb() was removed as it contained no code
    * ioctl_ifcreate() was renamed to ifcreate_ioctl() to follow the other
    netlink/ioctl function naming. Netlink and ioctl provide _different_
    interfaces and it's not possible to have a unified interface object
    that can be filled by either netlink or ioctl implementations. With that
    in mind, I'm leaning more to the function_<nl|ioctl> postfix pattern,
    than doing ioctl_ or netlink_ prefix.
    
    Reviewed By: kp
    Differential Revision: https://reviews.freebsd.org/D40426
    MFC after:      2 weeks
---
 sbin/ifconfig/carp.c        |  4 ++--
 sbin/ifconfig/ifclone.c     | 13 ++++++-------
 sbin/ifconfig/ifconfig.c    |  8 ++++----
 sbin/ifconfig/ifconfig.h    | 34 +++++++++++++++-------------------
 sbin/ifconfig/ifieee80211.c | 28 ++++++++++++++--------------
 sbin/ifconfig/iflagg.c      |  4 ++--
 sbin/ifconfig/ifmedia.c     | 22 +++++++++++-----------
 sbin/ifconfig/ifvlan.c      |  6 +++---
 sbin/ifconfig/ifvxlan.c     | 11 ++---------
 9 files changed, 59 insertions(+), 71 deletions(-)

diff --git a/sbin/ifconfig/carp.c b/sbin/ifconfig/carp.c
index 18f0eb893752..37f2cd485d97 100644
--- a/sbin/ifconfig/carp.c
+++ b/sbin/ifconfig/carp.c
@@ -60,7 +60,7 @@
 
 static const char *carp_states[] = { CARP_STATES };
 
-static void setcarp_callback(int, void *);
+static void setcarp_callback(if_ctx *, void *);
 
 static int carpr_vhid = -1;
 static int carpr_advskew = -1;
@@ -114,7 +114,7 @@ setcarp_vhid(if_ctx *ctx, const char *val, int dummy __unused)
 }
 
 static void
-setcarp_callback(int s __unused, void *arg __unused)
+setcarp_callback(if_ctx *ctx __unused, void *arg __unused)
 {
 	struct ifconfig_carp carpr = { };
 
diff --git a/sbin/ifconfig/ifclone.c b/sbin/ifconfig/ifclone.c
index 9c71a218b72a..7939ff95a065 100644
--- a/sbin/ifconfig/ifclone.c
+++ b/sbin/ifconfig/ifclone.c
@@ -118,13 +118,12 @@ clone_setdefcallback_filter(clone_match_func *filter, clone_callback_func *p)
  * no parameters.
  */
 static void
-ifclonecreate(int s, void *arg __unused)
+ifclonecreate(if_ctx *ctx, void *arg __unused)
 {
-	struct ifreq ifr;
+	struct ifreq ifr = {};
 	struct clone_defcb *dcp;
 
-	memset(&ifr, 0, sizeof(ifr));
-	(void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
 
 	/* Try to find a default callback by filter */
 	SLIST_FOREACH(dcp, &clone_defcbh, next) {
@@ -145,9 +144,9 @@ ifclonecreate(int s, void *arg __unused)
 
 	if (dcp == NULL || dcp->clone_cb == NULL) {
 		/* NB: no parameters */
-	  	ioctl_ifcreate(s, &ifr);
+		ifcreate_ioctl(ctx, &ifr);
 	} else {
-		dcp->clone_cb(s, &ifr);
+		dcp->clone_cb(ctx, &ifr);
 	}
 
 	/*
@@ -161,7 +160,7 @@ ifclonecreate(int s, void *arg __unused)
 }
 
 static void
-clone_create(if_ctx *ctx __unused, const char *cmd __unused, int d __unused)
+clone_create(if_ctx *ctx, const char *cmd __unused, int d __unused)
 {
 	callback_register(ifclonecreate, NULL);
 }
diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c
index 06cfefc5a466..9b337c04d327 100644
--- a/sbin/ifconfig/ifconfig.c
+++ b/sbin/ifconfig/ifconfig.c
@@ -201,9 +201,9 @@ usage(void)
 }
 
 void
-ioctl_ifcreate(int s, struct ifreq *ifr)
+ifcreate_ioctl(if_ctx *ctx, struct ifreq *ifr)
 {
-	if (ioctl(s, SIOCIFCREATE2, ifr) < 0) {
+	if (ioctl(ctx->io_s, SIOCIFCREATE2, ifr) < 0) {
 		switch (errno) {
 		case EEXIST:
 			errx(1, "interface %s already exists", ifr->ifr_name);
@@ -1143,7 +1143,7 @@ top:
 			if (cb == NULL)
 				errx(1, "internal error, no callback");
 			callbacks = cb->cb_next;
-			cb->cb_func(s, cb->cb_arg);
+			cb->cb_func(ctx, cb->cb_arg);
 			iscreate = 0;
 			/*
 			 * Handle any address family spec that
@@ -1204,7 +1204,7 @@ top:
 	 * command-line arguments.
 	 */
 	for (cb = callbacks; cb != NULL; cb = cb->cb_next)
-		cb->cb_func(s, cb->cb_arg);
+		cb->cb_func(ctx, cb->cb_arg);
 	/*
 	 * Do deferred operations.
 	 */
diff --git a/sbin/ifconfig/ifconfig.h b/sbin/ifconfig/ifconfig.h
index e1c7f84793e7..cc343a3e5686 100644
--- a/sbin/ifconfig/ifconfig.h
+++ b/sbin/ifconfig/ifconfig.h
@@ -53,13 +53,19 @@
 
 struct afswtch;
 struct cmd;
-struct ifconfig_context;
+struct snl_state;
+struct ifconfig_args;
+struct ifconfig_context {
+	struct ifconfig_args	*args;
+	const struct afswtch	*afp;
+	int			io_s;	/* fd to use for ioctl() */
+	struct snl_state	*io_ss;	/* NETLINK_ROUTE socket */
+};
+typedef const struct ifconfig_context if_ctx;
 
-typedef	void c_func(const struct ifconfig_context *ctx, const char *cmd, int arg);
-typedef	void c_func2(const struct ifconfig_context *ctx, const char *arg1,
-    const char *arg2);
-typedef	void c_func3(const struct ifconfig_context *ctx, const char *cmd,
-    const char *arg);
+typedef	void c_func(if_ctx *ctx, const char *cmd, int arg);
+typedef	void c_func2(if_ctx *ctx, const char *arg1, const char *arg2);
+typedef	void c_func3(if_ctx *ctx, const char *cmd, const char *arg);
 
 struct cmd {
 	const char *c_name;
@@ -79,7 +85,7 @@ struct cmd {
 };
 void	cmd_register(struct cmd *);
 
-typedef	void callback_func(int s, void *);
+typedef	void callback_func(if_ctx *, void *);
 void	callback_register(callback_func *, void *);
 
 /*
@@ -144,16 +150,6 @@ void	callback_register(callback_func *, void *);
     .c_next = NULL,				\
 }
 
-struct snl_state;
-struct ifconfig_args;
-struct ifconfig_context {
-	struct ifconfig_args	*args;
-	const struct afswtch	*afp;
-	int			io_s;	/* fd to use for ioctl() */
-	struct snl_state	*io_ss;	/* NETLINK_ROUTE socket */
-};
-typedef const struct ifconfig_context if_ctx;
-
 #define	ioctl_ctx(ctx, _req, ...)	ioctl((ctx)->io_s, _req, ## __VA_ARGS__)
 
 struct ifaddrs;
@@ -271,7 +267,7 @@ void	printb(const char *s, unsigned value, const char *bits);
 void	ifmaybeload(struct ifconfig_args *args, const char *name);
 
 typedef int  clone_match_func(const char *);
-typedef void clone_callback_func(int, struct ifreq *);
+typedef void clone_callback_func(if_ctx *, struct ifreq *);
 void	clone_setdefcallback_prefix(const char *, clone_callback_func *);
 void	clone_setdefcallback_filter(clone_match_func *, clone_callback_func *);
 
@@ -303,7 +299,7 @@ struct ifmediareq *ifmedia_getstate(void);
 
 void print_vhid(const struct ifaddrs *, const char *);
 
-void ioctl_ifcreate(int s, struct ifreq *);
+void ifcreate_ioctl(if_ctx *ctx, struct ifreq *ifr);
 
 /* Helpers */
 struct sockaddr_in;
diff --git a/sbin/ifconfig/ifieee80211.c b/sbin/ifconfig/ifieee80211.c
index c096428dd3a8..688d5b6464ad 100644
--- a/sbin/ifconfig/ifieee80211.c
+++ b/sbin/ifconfig/ifieee80211.c
@@ -447,10 +447,10 @@ getroam(int s)
 }
 
 static void
-setroam_cb(int s, void *arg)
+setroam_cb(if_ctx *ctx, void *arg)
 {
 	struct ieee80211_roamparams_req *roam = arg;
-	set80211(s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
+	set80211(ctx->io_s, IEEE80211_IOC_ROAM, 0, sizeof(*roam), roam);
 }
 
 static void
@@ -465,10 +465,10 @@ gettxparams(int s)
 }
 
 static void
-settxparams_cb(int s, void *arg)
+settxparams_cb(if_ctx *ctx, void *arg)
 {
 	struct ieee80211_txparams_req *txp = arg;
-	set80211(s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
+	set80211(ctx->io_s, IEEE80211_IOC_TXPARAMS, 0, sizeof(*txp), txp);
 }
 
 static void
@@ -491,7 +491,7 @@ getdevcaps(int s, struct ieee80211_devcaps_req *dc)
 }
 
 static void
-setregdomain_cb(int s, void *arg)
+setregdomain_cb(if_ctx *ctx, void *arg)
 {
 	struct ieee80211_regdomain_req *req;
 	struct ieee80211_regdomain *rd = arg;
@@ -545,7 +545,7 @@ setregdomain_cb(int s, void *arg)
 	if (dc == NULL)
 		errx(1, "no space for device capabilities");
 	dc->dc_chaninfo.ic_nchans = MAXCHAN;
-	getdevcaps(s, dc);
+	getdevcaps(ctx->io_s, dc);
 #if 0
 	if (verbose) {
 		printf("drivercaps: 0x%x\n", dc->dc_drivercaps);
@@ -576,11 +576,11 @@ setregdomain_cb(int s, void *arg)
 			errx(1, "no space for channel list");
 		memcpy(chaninfo, &req->chaninfo,
 		    IEEE80211_CHANINFO_SPACE(&req->chaninfo));
-		print_channels(s, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
+		print_channels(ctx->io_s, &req->chaninfo, 1/*allchans*/, 1/*verbose*/);
 	}
 	if (req->chaninfo.ic_nchans == 0)
 		errx(1, "no channels calculated");
-	set80211(s, IEEE80211_IOC_REGDOMAIN, 0,
+	set80211(ctx->io_s, IEEE80211_IOC_REGDOMAIN, 0,
 	    IEEE80211_REGDOMAIN_SPACE(req), req);
 	free(req);
 	free(dc);
@@ -5739,7 +5739,7 @@ print_string(const u_int8_t *buf, int len)
 }
 
 static void
-setdefregdomain(int s)
+setdefregdomain(if_ctx *ctx)
 {
 	struct regdata *rdp = getregdata();
 	const struct regdomain *rd;
@@ -5750,7 +5750,7 @@ setdefregdomain(int s)
 	    regdomain.country != CTRY_DEFAULT)
 		return;
 
-	getregdomain(s);
+	getregdomain(ctx->io_s);
 
 	/* Check if it was already set by the driver. */
 	if (regdomain.regdomain != 0 ||
@@ -5767,7 +5767,7 @@ setdefregdomain(int s)
 		defaultcountry(rd);
 
 	/* Send changes to net80211. */
-	setregdomain_cb(s, &regdomain);
+	setregdomain_cb(ctx, &regdomain);
 
 	/* Cleanup (so it can be overridden by subsequent parameters). */
 	regdomain.regdomain = 0;
@@ -5784,7 +5784,7 @@ static struct ieee80211_clone_params params = {
 };
 
 static void
-wlan_create(int s, struct ifreq *ifr)
+wlan_create(if_ctx *ctx, struct ifreq *ifr)
 {
 	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
 	char orig_name[IFNAMSIZ];
@@ -5796,13 +5796,13 @@ wlan_create(int s, struct ifreq *ifr)
 	    memcmp(params.icp_bssid, zerobssid, sizeof(zerobssid)) == 0)
 		errx(1, "no bssid specified for WDS (use wlanbssid)");
 	ifr->ifr_data = (caddr_t) &params;
-	ioctl_ifcreate(s, ifr);
+	ifcreate_ioctl(ctx, ifr);
 
 	/* XXX preserve original name for ifclonecreate(). */
 	strlcpy(orig_name, name, sizeof(orig_name));
 	strlcpy(name, ifr->ifr_name, sizeof(name));
 
-	setdefregdomain(s);
+	setdefregdomain(ctx);
 
 	strlcpy(name, orig_name, sizeof(name));
 }
diff --git a/sbin/ifconfig/iflagg.c b/sbin/ifconfig/iflagg.c
index 5bb1fa6161b0..45f88f2feebd 100644
--- a/sbin/ifconfig/iflagg.c
+++ b/sbin/ifconfig/iflagg.c
@@ -308,10 +308,10 @@ setlaggtype(if_ctx *ctx __unused, const char *arg, int dummy __unused)
 }
 
 static void
-lagg_create(int s, struct ifreq *ifr)
+lagg_create(if_ctx *ctx, struct ifreq *ifr)
 {
 	ifr->ifr_data = (caddr_t) &params;
-	ioctl_ifcreate(s, ifr);
+	ifcreate_ioctl(ctx, ifr);
 }
 
 static struct cmd lagg_cmds[] = {
diff --git a/sbin/ifconfig/ifmedia.c b/sbin/ifconfig/ifmedia.c
index 0774727a9444..49d1edb2d72c 100644
--- a/sbin/ifconfig/ifmedia.c
+++ b/sbin/ifconfig/ifmedia.c
@@ -90,7 +90,7 @@
 
 #include "ifconfig.h"
 
-static void domediaopt(const char *, bool);
+static void domediaopt(if_ctx *, const char *, bool);
 static ifmedia_t get_media_subtype(ifmedia_t, const char *);
 static ifmedia_t get_media_mode(ifmedia_t, const char *);
 static ifmedia_t get_media_options(ifmedia_t, const char *);
@@ -175,14 +175,14 @@ ifmedia_getstate(void)
 }
 
 static void
-setifmediacallback(int s, void *arg)
+setifmediacallback(if_ctx *ctx, void *arg)
 {
 	struct ifmediareq *ifmr = (struct ifmediareq *)arg;
 	static bool did_it = false;
 
 	if (!did_it) {
 		ifr.ifr_media = ifmr->ifm_current;
-		if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
+		if (ioctl_ctx(ctx, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
 			err(1, "SIOCSIFMEDIA (media)");
 		free(ifmr);
 		did_it = true;
@@ -190,7 +190,7 @@ setifmediacallback(int s, void *arg)
 }
 
 static void
-setmedia(if_ctx *ctx __unused, const char *val, int d __unused)
+setmedia(if_ctx *ctx, const char *val, int d __unused)
 {
 	struct ifmediareq *ifmr;
 	int subtype;
@@ -217,21 +217,21 @@ setmedia(if_ctx *ctx __unused, const char *val, int d __unused)
 }
 
 static void
-setmediaopt(if_ctx *ctx __unused, const char *val, int d __unused)
+setmediaopt(if_ctx *ctx, const char *val, int d __unused)
 {
 
-	domediaopt(val, false);
+	domediaopt(ctx, val, false);
 }
 
 static void
-unsetmediaopt(if_ctx *ctx __unused, const char *val, int d __unused)
+unsetmediaopt(if_ctx *ctx, const char *val, int d __unused)
 {
 
-	domediaopt(val, true);
+	domediaopt(ctx, val, true);
 }
 
 static void
-domediaopt(const char *val, bool clear)
+domediaopt(if_ctx *ctx, const char *val, bool clear)
 {
 	struct ifmediareq *ifmr;
 	ifmedia_t options;
@@ -256,7 +256,7 @@ domediaopt(const char *val, bool clear)
 }
 
 static void
-setmediainst(if_ctx *ctx __unused, const char *val, int d __unused)
+setmediainst(if_ctx *ctx, const char *val, int d __unused)
 {
 	struct ifmediareq *ifmr;
 	int inst;
@@ -275,7 +275,7 @@ setmediainst(if_ctx *ctx __unused, const char *val, int d __unused)
 }
 
 static void
-setmediamode(if_ctx *ctx __unused, const char *val, int d __unused)
+setmediamode(if_ctx *ctx, const char *val, int d __unused)
 {
 	struct ifmediareq *ifmr;
 	int mode;
diff --git a/sbin/ifconfig/ifvlan.c b/sbin/ifconfig/ifvlan.c
index 6dfc990d0558..2d0d15fdd828 100644
--- a/sbin/ifconfig/ifvlan.c
+++ b/sbin/ifconfig/ifvlan.c
@@ -158,7 +158,7 @@ vlan_parse_ethervid(const char *name)
 }
 
 static void
-vlan_create(int s, struct ifreq *ifr)
+vlan_create(if_ctx *ctx, struct ifreq *ifr)
 {
 	vlan_parse_ethervid(ifr->ifr_name);
 
@@ -172,11 +172,11 @@ vlan_create(int s, struct ifreq *ifr)
 			errx(1, "must specify a parent device for vlan create");
 		ifr->ifr_data = (caddr_t) &params;
 	}
-	ioctl_ifcreate(s, ifr);
+	ifcreate_ioctl(ctx, ifr);
 }
 
 static void
-vlan_cb(int s __unused, void *arg __unused)
+vlan_cb(if_ctx *ctx __unused, void *arg __unused)
 {
 	if ((params.vlr_tag != NOTAG) ^ (params.vlr_parent[0] != '\0'))
 		errx(1, "both vlan and vlandev must be specified");
diff --git a/sbin/ifconfig/ifvxlan.c b/sbin/ifconfig/ifvxlan.c
index 02d353ff7abd..ae4b54c7e64d 100644
--- a/sbin/ifconfig/ifvxlan.c
+++ b/sbin/ifconfig/ifvxlan.c
@@ -179,19 +179,13 @@ vxlan_check_params(void)
 #undef _REMOTE_ADDR46
 
 static void
-vxlan_cb(int s __unused, void *arg __unused)
-{
-
-}
-
-static void
-vxlan_create(int s, struct ifreq *ifr)
+vxlan_create(if_ctx *ctx, struct ifreq *ifr)
 {
 
 	vxlan_check_params();
 
 	ifr->ifr_data = (caddr_t) &params;
-	ioctl_ifcreate(s, ifr);
+	ifcreate_ioctl(ctx, ifr);
 }
 
 static void
@@ -640,6 +634,5 @@ vxlan_ctor(void)
 	for (i = 0; i < nitems(vxlan_cmds); i++)
 		cmd_register(&vxlan_cmds[i]);
 	af_register(&af_vxlan);
-	callback_register(vxlan_cb, NULL);
 	clone_setdefcallback_prefix("vxlan", vxlan_create);
 }