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
This commit is contained in:
Alexander V. Chernikov 2023-06-13 06:10:41 +00:00
parent ab4d1b73cb
commit 74b4261161
9 changed files with 59 additions and 71 deletions

View File

@ -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 = { };

View File

@ -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);
}

View File

@ -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.
*/

View File

@ -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;

View File

@ -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));
}

View File

@ -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[] = {

View File

@ -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;

View File

@ -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");

View File

@ -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);
}