ifconfig: introduce ifconfig_context to store current global state.

The structure consists of all current context - arguments,
open sockets, current family and so on.

Pass this structure as a first argument to most of the af_ menthods.
This allows to propagate and update shared data without using
 global variables.

The diff is pretty large, but de-facto mechanical. All changes
 except the structure setup in ifconfig[_netlink].c are one-line
 mechanical changes.

Reviewed by:	kp
Differential Revision: https://reviews.freebsd.org/D40239
MFC after:	2 weeks
This commit is contained in:
Alexander V. Chernikov 2023-05-23 17:02:12 +00:00
parent 44cd85d42e
commit 6e3a9d7f2c
23 changed files with 777 additions and 783 deletions

View File

@ -83,7 +83,7 @@ print_addr(struct sockaddr_in *sin)
#ifdef WITHOUT_NETLINK
static void
in_status(int s __unused, const struct ifaddrs *ifa)
in_status(if_ctx *ctx __unused, const struct ifaddrs *ifa)
{
struct sockaddr_in *sin, null_sin = {};
@ -143,8 +143,7 @@ get_mask(int plen)
}
static void
in_status_nl(struct ifconfig_args *args __unused, struct io_handler *h,
if_link_t *link, if_addr_t *ifa)
in_status_nl(if_ctx *ctx __unused, if_link_t *link, if_addr_t *ifa)
{
struct sockaddr_in *sin = satosin(ifa->ifa_local);
int plen = ifa->ifa_prefixlen;
@ -228,7 +227,7 @@ in_getaddr(const char *s, int which)
}
static void
in_postproc(int s, const struct afswtch *afp, int newaddr, int ifflags)
in_postproc(if_ctx *ctx __unused, int newaddr, int ifflags)
{
if (sintab[ADDR]->sin_len != 0 && sintab[MASK]->sin_len == 0 &&
newaddr && (ifflags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
@ -292,7 +291,7 @@ static struct afswtch af_inet = {
#ifdef WITHOUT_NETLINK
.af_status = in_status,
#else
.af_status_nl = in_status_nl,
.af_status = in_status_nl,
#endif
.af_getaddr = in_getaddr,
.af_postproc = in_postproc,

View File

@ -72,25 +72,27 @@ static char *sec2str(time_t);
static int explicit_prefix = 0;
extern char *f_inet6, *f_addr;
extern void setnd6flags(const char *, int, int, const struct afswtch *);
extern void setnd6defif(const char *, int, int, const struct afswtch *);
extern void nd6_status(int);
extern void setnd6flags(if_ctx *, const char *, int);
extern void setnd6defif(if_ctx *,const char *, int);
extern void nd6_status(if_ctx *);
static char addr_buf[NI_MAXHOST]; /*for getnameinfo()*/
static void
setifprefixlen(const char *addr, int dummy __unused, int s,
const struct afswtch *afp)
setifprefixlen(if_ctx *ctx, const char *addr, int dummy __unused)
{
const struct afswtch *afp = ctx->afp;
if (afp->af_getprefix != NULL)
afp->af_getprefix(addr, MASK);
explicit_prefix = 1;
}
static void
setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
const struct afswtch *afp)
setip6flags(if_ctx *ctx, const char *dummyaddr __unused, int flag)
{
const struct afswtch *afp = ctx->afp;
if (afp->af_af != AF_INET6)
err(1, "address flags can be set only for inet6 addresses");
@ -101,9 +103,9 @@ setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
}
static void
setip6lifetime(const char *cmd, const char *val, int s,
const struct afswtch *afp)
setip6lifetime(if_ctx *ctx, const char *cmd, const char *val)
{
const struct afswtch *afp = ctx->afp;
struct timespec now;
time_t newval;
char *ep;
@ -124,23 +126,21 @@ setip6lifetime(const char *cmd, const char *val, int s,
}
static void
setip6pltime(const char *seconds, int dummy __unused, int s,
const struct afswtch *afp)
setip6pltime(if_ctx *ctx, const char *seconds, int dummy __unused)
{
setip6lifetime("pltime", seconds, s, afp);
setip6lifetime(ctx, "pltime", seconds);
}
static void
setip6vltime(const char *seconds, int dummy __unused, int s,
const struct afswtch *afp)
setip6vltime(if_ctx *ctx, const char *seconds, int dummy __unused)
{
setip6lifetime("vltime", seconds, s, afp);
setip6lifetime(ctx, "vltime", seconds);
}
static void
setip6eui64(const char *cmd, int dummy __unused, int s,
const struct afswtch *afp)
setip6eui64(if_ctx *ctx, const char *cmd, int dummy __unused)
{
const struct afswtch *afp = ctx->afp;
struct ifaddrs *ifap, *ifa;
const struct sockaddr_in6 *sin6 = NULL;
const struct in6_addr *lladdr = NULL;
@ -247,7 +247,7 @@ print_lifetime(const char *prepend, time_t px_time, struct timespec *now)
#ifdef WITHOUT_NETLINK
static void
in6_status(int s __unused, const struct ifaddrs *ifa)
in6_status(if_ctx *ctx __unused, const struct ifaddrs *ifa)
{
struct sockaddr_in6 *sin, null_sin = {};
struct in6_ifreq ifr6;
@ -341,8 +341,7 @@ show_lifetime(struct ifa_cacheinfo *ci)
}
static void
in6_status_nl(struct ifconfig_args *args __unused, struct io_handler *h,
if_link_t *link, if_addr_t *ifa)
in6_status_nl(if_ctx *ctx __unused, if_link_t *link, if_addr_t *ifa)
{
int plen = ifa->ifa_prefixlen;
uint32_t scopeid;
@ -495,14 +494,14 @@ sec2str(time_t total)
}
static void
in6_postproc(int s, const struct afswtch *afp, int newaddr __unused,
in6_postproc(if_ctx *ctx, int newaddr __unused,
int ifflags __unused)
{
if (explicit_prefix == 0) {
/* Aggregatable address architecture defines all prefixes
are 64. So, it is convenient to set prefixlen to 64 if
it is not specified. */
setifprefixlen("64", 0, s, afp);
setifprefixlen(ctx, "64", 0);
/* in6_getprefix("64", MASK) if MASK is available here... */
}
}
@ -600,7 +599,7 @@ static struct afswtch af_inet6 = {
#ifdef WITHOUT_NETLINK
.af_status = in6_status,
#else
.af_status_nl = in6_status_nl,
.af_status = in6_status_nl,
#endif
.af_getaddr = in6_getaddr,
.af_getprefix = in6_getprefix,

View File

@ -93,7 +93,7 @@ print_pcp(int s)
#ifdef WITHOUT_NETLINK
static void
link_status(int s __unused, const struct ifaddrs *ifa)
link_status(if_ctx *ctx, const struct ifaddrs *ifa)
{
/* XXX no const 'cuz LLADDR is defined wrong */
struct sockaddr_dl *sdl;
@ -142,7 +142,7 @@ link_status(int s __unused, const struct ifaddrs *ifa)
print_ether((const struct ether_addr *)&ifr.ifr_addr.sa_data, "hwaddr");
pcp:
print_pcp(s);
print_pcp(ctx->io_s);
}
#else
@ -159,8 +159,7 @@ convert_iftype(uint8_t iftype)
}
static void
link_status_nl(struct ifconfig_args *args __unused, struct io_handler *h,
if_link_t *link, if_addr_t *ifa __unused)
link_status_nl(if_ctx *ctx, if_link_t *link, if_addr_t *ifa __unused)
{
if (link->ifla_address != NULL) {
struct sockaddr_dl sdl = {
@ -180,7 +179,7 @@ link_status_nl(struct ifconfig_args *args __unused, struct io_handler *h,
}
}
if (convert_iftype(link->ifi_type) == IFT_ETHER)
print_pcp(h->s);
print_pcp(ctx->io_s);
}
#endif
@ -224,7 +223,7 @@ static struct afswtch af_link = {
#ifdef WITHOUT_NETLINK
.af_status = link_status,
#else
.af_status_nl = link_status_nl,
.af_status = link_status_nl,
#endif
.af_getaddr = link_getaddr,
.af_aifaddr = SIOCSIFLLADDR,
@ -236,7 +235,7 @@ static struct afswtch af_ether = {
#ifdef WITHOUT_NETLINK
.af_status = link_status,
#else
.af_status_nl = link_status_nl,
.af_status = link_status_nl,
#endif
.af_getaddr = link_getaddr,
.af_aifaddr = SIOCSIFLLADDR,
@ -248,7 +247,7 @@ static struct afswtch af_lladdr = {
#ifdef WITHOUT_NETLINK
.af_status = link_status,
#else
.af_status_nl = link_status_nl,
.af_status = link_status_nl,
#endif
.af_getaddr = link_getaddr,
.af_aifaddr = SIOCSIFLLADDR,

View File

@ -70,21 +70,19 @@ static const char rcsid[] =
#endif
static int isnd6defif(int);
void setnd6flags(const char *, int, int, const struct afswtch *);
void setnd6defif(const char *, int, int, const struct afswtch *);
void nd6_status(int);
void setnd6flags(if_ctx *, const char *, int);
void setnd6defif(if_ctx *,const char *, int);
void nd6_status(if_ctx *);
void
setnd6flags(const char *dummyaddr __unused,
int d, int s,
const struct afswtch *afp)
setnd6flags(if_ctx *ctx, const char *dummyaddr __unused, int d)
{
struct in6_ndireq nd;
int error;
memset(&nd, 0, sizeof(nd));
strlcpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname));
error = ioctl(s, SIOCGIFINFO_IN6, &nd);
error = ioctl_ctx(ctx, SIOCGIFINFO_IN6, &nd);
if (error) {
warn("ioctl(SIOCGIFINFO_IN6)");
return;
@ -93,15 +91,13 @@ setnd6flags(const char *dummyaddr __unused,
nd.ndi.flags &= ~(-d);
else
nd.ndi.flags |= d;
error = ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd);
error = ioctl_ctx(ctx, SIOCSIFINFO_IN6, (caddr_t)&nd);
if (error)
warn("ioctl(SIOCSIFINFO_IN6)");
}
void
setnd6defif(const char *dummyaddr __unused,
int d, int s,
const struct afswtch *afp)
setnd6defif(if_ctx *ctx, const char *dummyaddr __unused, int d)
{
struct in6_ndifreq ndifreq;
int ifindex;
@ -111,7 +107,7 @@ setnd6defif(const char *dummyaddr __unused,
strlcpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname));
if (d < 0) {
if (isnd6defif(s)) {
if (isnd6defif(ctx->io_s)) {
/* ifindex = 0 means to remove default if */
ifindex = 0;
} else
@ -122,7 +118,7 @@ setnd6defif(const char *dummyaddr __unused,
}
ndifreq.ifindex = ifindex;
error = ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq);
error = ioctl_ctx(ctx, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq);
if (error)
warn("ioctl(SIOCSDEFIFACE_IN6)");
}
@ -147,7 +143,7 @@ isnd6defif(int s)
}
void
nd6_status(int s)
nd6_status(if_ctx *ctx __unused)
{
struct in6_ndireq nd;
int s6;

View File

@ -60,12 +60,7 @@
static const char *carp_states[] = { CARP_STATES };
static void carp_status(int s);
static void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
static void setcarp_callback(int, void *);
static void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
static void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
static void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
static int carpr_vhid = -1;
static int carpr_advskew = -1;
@ -76,7 +71,7 @@ static struct in6_addr carp_addr6;
static unsigned char const *carpr_key;
static void
carp_status(int s)
carp_status(if_ctx *ctx __unused)
{
struct ifconfig_carp carpr[CARP_MAXVHID];
char addr_buf[NI_MAXHOST];
@ -102,8 +97,9 @@ carp_status(int s)
}
static void
setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
setcarp_vhid(if_ctx *ctx, const char *val, int dummy __unused)
{
const struct afswtch *afp = ctx->afp;
carpr_vhid = atoi(val);
@ -148,7 +144,7 @@ setcarp_callback(int s, void *arg __unused)
}
static void
setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
setcarp_passwd(if_ctx *ctx __unused, const char *val, int dummy __unused)
{
if (carpr_vhid == -1)
@ -158,7 +154,7 @@ setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
}
static void
setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
setcarp_advskew(if_ctx *ctx __unused, const char *val, int dummy __unused)
{
if (carpr_vhid == -1)
@ -168,7 +164,7 @@ setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
}
static void
setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
setcarp_advbase(if_ctx *ctx __unused, const char *val, int dummy __unused)
{
if (carpr_vhid == -1)
@ -178,7 +174,7 @@ setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
}
static void
setcarp_state(const char *val, int d, int s, const struct afswtch *afp)
setcarp_state(if_ctx *ctx __unused, const char *val, int dummy __unused)
{
int i;
@ -195,19 +191,19 @@ setcarp_state(const char *val, int d, int s, const struct afswtch *afp)
}
static void
setcarp_peer(const char *val, int d, int s, const struct afswtch *afp)
setcarp_peer(if_ctx *ctx __unused, const char *val, int dummy __unused)
{
carp_addr.s_addr = inet_addr(val);
}
static void
setcarp_mcast(const char *val, int d, int s, const struct afswtch *afp)
setcarp_mcast(if_ctx *ctx __unused, const char *val __unused, int dummy __unused)
{
carp_addr.s_addr = htonl(INADDR_CARP_GROUP);
}
static void
setcarp_peer6(const char *val, int d, int s, const struct afswtch *afp)
setcarp_peer6(if_ctx *ctx __unused, const char *val, int dummy __unused)
{
struct addrinfo hints, *res;
@ -224,7 +220,7 @@ setcarp_peer6(const char *val, int d, int s, const struct afswtch *afp)
}
static void
setcarp_mcast6(const char *val, int d, int s, const struct afswtch *afp)
setcarp_mcast6(if_ctx *ctx __unused, const char *val __unused, int dummy __unused)
{
bzero(&carp_addr6, sizeof(carp_addr6));
carp_addr6.s6_addr[0] = 0xff;
@ -253,8 +249,8 @@ static __constructor void
carp_ctor(void)
{
/* Default to multicast. */
setcarp_mcast(NULL, 0, 0, NULL);
setcarp_mcast6(NULL, 0, 0, NULL);
setcarp_mcast(NULL, NULL, 0);
setcarp_mcast6(NULL, NULL, 0);
for (size_t i = 0; i < nitems(carp_cmds); i++)
cmd_register(&carp_cmds[i]);

View File

@ -154,7 +154,7 @@ bridge_addresses(int s, const char *prefix)
}
static void
bridge_status(int s)
bridge_status(if_ctx *ctx __unused)
{
struct ifconfig_bridge_status *bridge;
struct ifbropreq *params;
@ -225,178 +225,177 @@ bridge_status(int s)
}
static void
setbridge_add(const char *val, int d, int s, const struct afswtch *afp)
setbridge_add(if_ctx *ctx, const char *val, int dummy __unused)
{
struct ifbreq req;
memset(&req, 0, sizeof(req));
strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
if (do_cmd(s, BRDGADD, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx->io_s, BRDGADD, &req, sizeof(req), 1) < 0)
err(1, "BRDGADD %s", val);
}
static void
setbridge_delete(const char *val, int d, int s, const struct afswtch *afp)
setbridge_delete(if_ctx *ctx, const char *val, int dummy __unused)
{
struct ifbreq req;
memset(&req, 0, sizeof(req));
strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
if (do_cmd(s, BRDGDEL, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx->io_s, BRDGDEL, &req, sizeof(req), 1) < 0)
err(1, "BRDGDEL %s", val);
}
static void
setbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
setbridge_discover(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_DISCOVER, 1);
do_bridgeflag(ctx->io_s, val, IFBIF_DISCOVER, 1);
}
static void
unsetbridge_discover(const char *val, int d, int s, const struct afswtch *afp)
unsetbridge_discover(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_DISCOVER, 0);
do_bridgeflag(ctx->io_s, val, IFBIF_DISCOVER, 0);
}
static void
setbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
setbridge_learn(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_LEARNING, 1);
do_bridgeflag(ctx->io_s, val, IFBIF_LEARNING, 1);
}
static void
unsetbridge_learn(const char *val, int d, int s, const struct afswtch *afp)
unsetbridge_learn(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_LEARNING, 0);
do_bridgeflag(ctx->io_s, val, IFBIF_LEARNING, 0);
}
static void
setbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
setbridge_sticky(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_STICKY, 1);
do_bridgeflag(ctx->io_s, val, IFBIF_STICKY, 1);
}
static void
unsetbridge_sticky(const char *val, int d, int s, const struct afswtch *afp)
unsetbridge_sticky(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_STICKY, 0);
do_bridgeflag(ctx->io_s, val, IFBIF_STICKY, 0);
}
static void
setbridge_span(const char *val, int d, int s, const struct afswtch *afp)
setbridge_span(if_ctx *ctx, const char *val, int dummy __unused)
{
struct ifbreq req;
memset(&req, 0, sizeof(req));
strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
if (do_cmd(s, BRDGADDS, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx->io_s, BRDGADDS, &req, sizeof(req), 1) < 0)
err(1, "BRDGADDS %s", val);
}
static void
unsetbridge_span(const char *val, int d, int s, const struct afswtch *afp)
unsetbridge_span(if_ctx *ctx, const char *val, int dummy __unused)
{
struct ifbreq req;
memset(&req, 0, sizeof(req));
strlcpy(req.ifbr_ifsname, val, sizeof(req.ifbr_ifsname));
if (do_cmd(s, BRDGDELS, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx->io_s, BRDGDELS, &req, sizeof(req), 1) < 0)
err(1, "BRDGDELS %s", val);
}
static void
setbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
setbridge_stp(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_STP, 1);
do_bridgeflag(ctx->io_s, val, IFBIF_STP, 1);
}
static void
unsetbridge_stp(const char *val, int d, int s, const struct afswtch *afp)
unsetbridge_stp(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_STP, 0);
do_bridgeflag(ctx->io_s, val, IFBIF_STP, 0);
}
static void
setbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
setbridge_edge(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 1);
do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_EDGE, 1);
}
static void
unsetbridge_edge(const char *val, int d, int s, const struct afswtch *afp)
unsetbridge_edge(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_BSTP_EDGE, 0);
do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_EDGE, 0);
}
static void
setbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
setbridge_autoedge(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 1);
do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_AUTOEDGE, 1);
}
static void
unsetbridge_autoedge(const char *val, int d, int s, const struct afswtch *afp)
unsetbridge_autoedge(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_BSTP_AUTOEDGE, 0);
do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_AUTOEDGE, 0);
}
static void
setbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
setbridge_ptp(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_BSTP_PTP, 1);
do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_PTP, 1);
}
static void
unsetbridge_ptp(const char *val, int d, int s, const struct afswtch *afp)
unsetbridge_ptp(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_BSTP_PTP, 0);
do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_PTP, 0);
}
static void
setbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
setbridge_autoptp(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 1);
do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_AUTOPTP, 1);
}
static void
unsetbridge_autoptp(const char *val, int d, int s, const struct afswtch *afp)
unsetbridge_autoptp(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_BSTP_AUTOPTP, 0);
do_bridgeflag(ctx->io_s, val, IFBIF_BSTP_AUTOPTP, 0);
}
static void
setbridge_flush(const char *val, int d, int s, const struct afswtch *afp)
setbridge_flush(if_ctx *ctx, const char *val, int dummy __unused)
{
struct ifbreq req;
memset(&req, 0, sizeof(req));
req.ifbr_ifsflags = IFBF_FLUSHDYN;
if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx->io_s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
err(1, "BRDGFLUSH");
}
static void
setbridge_flushall(const char *val, int d, int s, const struct afswtch *afp)
setbridge_flushall(if_ctx *ctx, const char *val, int dummy __unused)
{
struct ifbreq req;
memset(&req, 0, sizeof(req));
req.ifbr_ifsflags = IFBF_FLUSHALL;
if (do_cmd(s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx->io_s, BRDGFLUSH, &req, sizeof(req), 1) < 0)
err(1, "BRDGFLUSH");
}
static void
setbridge_static(const char *val, const char *mac, int s,
const struct afswtch *afp)
setbridge_static(if_ctx *ctx, const char *val, const char *mac)
{
struct ifbareq req;
struct ether_addr *ea;
@ -412,12 +411,12 @@ setbridge_static(const char *val, const char *mac, int s,
req.ifba_flags = IFBAF_STATIC;
req.ifba_vlan = 1; /* XXX allow user to specify */
if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx->io_s, BRDGSADDR, &req, sizeof(req), 1) < 0)
err(1, "BRDGSADDR %s", val);
}
static void
setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
setbridge_deladdr(if_ctx *ctx, const char *val, int dummy __unused)
{
struct ifbareq req;
struct ether_addr *ea;
@ -430,19 +429,19 @@ setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx->io_s, BRDGDADDR, &req, sizeof(req), 1) < 0)
err(1, "BRDGDADDR %s", val);
}
static void
setbridge_addr(const char *val, int d, int s, const struct afswtch *afp)
setbridge_addr(if_ctx *ctx, const char *val, int dummy __unused)
{
bridge_addresses(s, "");
bridge_addresses(ctx->io_s, "");
}
static void
setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp)
setbridge_maxaddr(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifbrparam param;
u_long val;
@ -452,12 +451,12 @@ setbridge_maxaddr(const char *arg, int d, int s, const struct afswtch *afp)
param.ifbrp_csize = val & 0xffffffff;
if (do_cmd(s, BRDGSCACHE, &param, sizeof(param), 1) < 0)
if (do_cmd(ctx->io_s, BRDGSCACHE, &param, sizeof(param), 1) < 0)
err(1, "BRDGSCACHE %s", arg);
}
static void
setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp)
setbridge_hellotime(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifbrparam param;
u_long val;
@ -467,12 +466,12 @@ setbridge_hellotime(const char *arg, int d, int s, const struct afswtch *afp)
param.ifbrp_hellotime = val & 0xff;
if (do_cmd(s, BRDGSHT, &param, sizeof(param), 1) < 0)
if (do_cmd(ctx->io_s, BRDGSHT, &param, sizeof(param), 1) < 0)
err(1, "BRDGSHT %s", arg);
}
static void
setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp)
setbridge_fwddelay(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifbrparam param;
u_long val;
@ -482,12 +481,12 @@ setbridge_fwddelay(const char *arg, int d, int s, const struct afswtch *afp)
param.ifbrp_fwddelay = val & 0xff;
if (do_cmd(s, BRDGSFD, &param, sizeof(param), 1) < 0)
if (do_cmd(ctx->io_s, BRDGSFD, &param, sizeof(param), 1) < 0)
err(1, "BRDGSFD %s", arg);
}
static void
setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp)
setbridge_maxage(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifbrparam param;
u_long val;
@ -497,12 +496,12 @@ setbridge_maxage(const char *arg, int d, int s, const struct afswtch *afp)
param.ifbrp_maxage = val & 0xff;
if (do_cmd(s, BRDGSMA, &param, sizeof(param), 1) < 0)
if (do_cmd(ctx->io_s, BRDGSMA, &param, sizeof(param), 1) < 0)
err(1, "BRDGSMA %s", arg);
}
static void
setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp)
setbridge_priority(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifbrparam param;
u_long val;
@ -512,12 +511,12 @@ setbridge_priority(const char *arg, int d, int s, const struct afswtch *afp)
param.ifbrp_prio = val & 0xffff;
if (do_cmd(s, BRDGSPRI, &param, sizeof(param), 1) < 0)
if (do_cmd(ctx->io_s, BRDGSPRI, &param, sizeof(param), 1) < 0)
err(1, "BRDGSPRI %s", arg);
}
static void
setbridge_protocol(const char *arg, int d, int s, const struct afswtch *afp)
setbridge_protocol(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifbrparam param;
@ -529,12 +528,12 @@ setbridge_protocol(const char *arg, int d, int s, const struct afswtch *afp)
errx(1, "unknown stp protocol");
}
if (do_cmd(s, BRDGSPROTO, &param, sizeof(param), 1) < 0)
if (do_cmd(ctx->io_s, BRDGSPROTO, &param, sizeof(param), 1) < 0)
err(1, "BRDGSPROTO %s", arg);
}
static void
setbridge_holdcount(const char *arg, int d, int s, const struct afswtch *afp)
setbridge_holdcount(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifbrparam param;
u_long val;
@ -544,13 +543,12 @@ setbridge_holdcount(const char *arg, int d, int s, const struct afswtch *afp)
param.ifbrp_txhc = val & 0xff;
if (do_cmd(s, BRDGSTXHC, &param, sizeof(param), 1) < 0)
if (do_cmd(ctx->io_s, BRDGSTXHC, &param, sizeof(param), 1) < 0)
err(1, "BRDGSTXHC %s", arg);
}
static void
setbridge_ifpriority(const char *ifn, const char *pri, int s,
const struct afswtch *afp)
setbridge_ifpriority(if_ctx *ctx, const char *ifn, const char *pri)
{
struct ifbreq req;
u_long val;
@ -563,13 +561,12 @@ setbridge_ifpriority(const char *ifn, const char *pri, int s,
strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
req.ifbr_priority = val & 0xff;
if (do_cmd(s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx->io_s, BRDGSIFPRIO, &req, sizeof(req), 1) < 0)
err(1, "BRDGSIFPRIO %s", pri);
}
static void
setbridge_ifpathcost(const char *ifn, const char *cost, int s,
const struct afswtch *afp)
setbridge_ifpathcost(if_ctx *ctx, const char *ifn, const char *cost)
{
struct ifbreq req;
u_long val;
@ -582,13 +579,12 @@ setbridge_ifpathcost(const char *ifn, const char *cost, int s,
strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
req.ifbr_path_cost = val;
if (do_cmd(s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx->io_s, BRDGSIFCOST, &req, sizeof(req), 1) < 0)
err(1, "BRDGSIFCOST %s", cost);
}
static void
setbridge_ifmaxaddr(const char *ifn, const char *arg, int s,
const struct afswtch *afp)
setbridge_ifmaxaddr(if_ctx *ctx, const char *ifn, const char *arg)
{
struct ifbreq req;
u_long val;
@ -601,12 +597,12 @@ setbridge_ifmaxaddr(const char *ifn, const char *arg, int s,
strlcpy(req.ifbr_ifsname, ifn, sizeof(req.ifbr_ifsname));
req.ifbr_addrmax = val & 0xffffffff;
if (do_cmd(s, BRDGSIFAMAX, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx->io_s, BRDGSIFAMAX, &req, sizeof(req), 1) < 0)
err(1, "BRDGSIFAMAX %s", arg);
}
static void
setbridge_timeout(const char *arg, int d, int s, const struct afswtch *afp)
setbridge_timeout(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifbrparam param;
u_long val;
@ -616,22 +612,22 @@ setbridge_timeout(const char *arg, int d, int s, const struct afswtch *afp)
param.ifbrp_ctime = val & 0xffffffff;
if (do_cmd(s, BRDGSTO, &param, sizeof(param), 1) < 0)
if (do_cmd(ctx->io_s, BRDGSTO, &param, sizeof(param), 1) < 0)
err(1, "BRDGSTO %s", arg);
}
static void
setbridge_private(const char *val, int d, int s, const struct afswtch *afp)
setbridge_private(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_PRIVATE, 1);
do_bridgeflag(ctx->io_s, val, IFBIF_PRIVATE, 1);
}
static void
unsetbridge_private(const char *val, int d, int s, const struct afswtch *afp)
unsetbridge_private(if_ctx *ctx, const char *val, int dummy __unused)
{
do_bridgeflag(s, val, IFBIF_PRIVATE, 0);
do_bridgeflag(ctx->io_s, val, IFBIF_PRIVATE, 0);
}
static struct cmd bridge_cmds[] = {

View File

@ -160,17 +160,17 @@ ifclonecreate(int s, void *arg)
}
}
static
DECL_CMD_FUNC(clone_create, arg, d)
static void
clone_create(if_ctx *ctx __unused, const char *cmd __unused, int d __unused)
{
callback_register(ifclonecreate, NULL);
}
static
DECL_CMD_FUNC(clone_destroy, arg, d)
static void
clone_destroy(if_ctx *ctx, const char *cmd __unused, int d __unused)
{
(void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(ctx->io_s, SIOCIFDESTROY, &ifr) < 0)
err(1, "SIOCIFDESTROY");
}

View File

@ -105,7 +105,7 @@ int newaddr = 1;
int verbose;
int printifname = 0;
struct ifconfig_args args;
struct ifconfig_args global_args;
int printkeys = 0; /* Print keying material for interfaces. */
int exit_code = 0;
@ -569,6 +569,8 @@ main(int ac, char *av[])
#ifdef JAIL
int jid;
#endif
struct ifconfig_args *args = &global_args;
f_inet = f_inet6 = f_ether = f_addr = NULL;
lifh = ifconfig_open();
@ -585,11 +587,11 @@ main(int ac, char *av[])
*/
atexit(printifnamemaybe);
args_parse(&args, ac, av);
args_parse(args, ac, av);
#ifdef JAIL
if (args.jail_name) {
jid = jail_getid(args.jail_name);
if (args->jail_name) {
jid = jail_getid(args->jail_name);
if (jid == -1)
Perror("jail not found");
if (jail_attach(jid) != 0)
@ -597,15 +599,15 @@ main(int ac, char *av[])
}
#endif
if (!args.all && !args.namesonly) {
if (!args->all && !args->namesonly) {
/* not listing, need an argument */
args.ifname = args_pop(&args);
args->ifname = args_pop(args);
/* check and maybe load support for this interface */
ifmaybeload(&args, args.ifname);
ifmaybeload(args, args->ifname);
char *arg = args_peek(&args);
if (if_nametoindex(args.ifname) == 0) {
char *arg = args_peek(args);
if (if_nametoindex(args->ifname) == 0) {
/*
* NOTE: We must special-case the `create' command
* right here as we would otherwise fail when trying
@ -613,11 +615,11 @@ main(int ac, char *av[])
*/
if (arg != NULL && (strcmp(arg, "create") == 0 ||
strcmp(arg, "plumb") == 0)) {
iflen = strlcpy(name, args.ifname, sizeof(name));
iflen = strlcpy(name, args->ifname, sizeof(name));
if (iflen >= sizeof(name))
errx(1, "%s: cloning name too long",
args.ifname);
ifconfig(args.argc, args.argv, 1, NULL);
args->ifname);
ifconfig(args->argc, args->argv, 1, NULL);
exit(exit_code);
}
#ifdef JAIL
@ -627,15 +629,15 @@ 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));
iflen = strlcpy(name, args->ifname, sizeof(name));
if (iflen >= sizeof(name))
errx(1, "%s: interface name too long",
args.ifname);
ifconfig(args.argc, args.argv, 0, NULL);
args->ifname);
ifconfig(args->argc, args->argv, 0, NULL);
exit(exit_code);
}
#endif
errx(1, "interface %s does not exist", args.ifname);
errx(1, "interface %s does not exist", args->ifname);
} else {
/*
* Do not allow use `create` command as hostname if
@ -643,19 +645,19 @@ main(int ac, char *av[])
*/
if (arg != NULL && (strcmp(arg, "create") == 0 ||
strcmp(arg, "plumb") == 0)) {
if (args.argc == 1)
if (args->argc == 1)
errx(1, "interface %s already exists",
args.ifname);
args_pop(&args);
args->ifname);
args_pop(args);
}
}
}
/* Check for address family */
if (args.argc > 0) {
args.afp = af_getbyname(args_peek(&args));
if (args.afp != NULL)
args_pop(&args);
if (args->argc > 0) {
args->afp = af_getbyname(args_peek(args));
if (args->afp != NULL)
args_pop(args);
}
/*
@ -663,23 +665,23 @@ main(int ac, char *av[])
* which doesn't require building, sorting, and searching the entire
* system address list
*/
if ((args.argc > 0) && (args.ifname != NULL)) {
iflen = strlcpy(name, args.ifname, sizeof(name));
if ((args->argc > 0) && (args->ifname != NULL)) {
iflen = strlcpy(name, args->ifname, sizeof(name));
if (iflen >= sizeof(name)) {
warnx("%s: interface name too long, skipping", args.ifname);
warnx("%s: interface name too long, skipping", args->ifname);
} else {
flags = getifflags(name, -1, false);
if (!(((flags & IFF_CANTCONFIG) != 0) ||
(args.downonly && (flags & IFF_UP) != 0) ||
(args.uponly && (flags & IFF_UP) == 0)))
ifconfig(args.argc, args.argv, 0, args.afp);
(args->downonly && (flags & IFF_UP) != 0) ||
(args->uponly && (flags & IFF_UP) == 0)))
ifconfig(args->argc, args->argv, 0, args->afp);
}
goto done;
}
args.allfamilies = args.afp == NULL;
args->allfamilies = args->afp == NULL;
list_interfaces(&args);
list_interfaces(args);
done:
freeformat();
@ -910,7 +912,7 @@ af_getbyfamily(int af)
}
void
af_other_status(int s)
af_other_status(if_ctx *ctx)
{
struct afswtch *afp;
uint8_t afmask[howmany(AF_MAX, NBBY)];
@ -921,7 +923,7 @@ af_other_status(int s)
continue;
if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
continue;
afp->af_other_status(s);
afp->af_other_status(ctx);
setbit(afmask, afp->af_af);
}
}
@ -992,10 +994,10 @@ callback_register(callback_func *func, void *arg)
}
/* specially-handled commands */
static void setifaddr(const char *, int, int, const struct afswtch *);
static void setifaddr(if_ctx *ctx, const char *addr, int param);
static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
static void setifdstaddr(const char *, int, int, const struct afswtch *);
static void setifdstaddr(if_ctx *ctx, const char *addr, int param __unused);
static const struct cmd setifdstaddr_cmd =
DEF_CMD("ifdstaddr", 0, setifdstaddr);
@ -1041,6 +1043,7 @@ addifaddr(int s, const struct afswtch *afp)
int
ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
{
struct ifconfig_context ctx = {};
const struct afswtch *afp, *nafp;
const struct cmd *p;
struct callback *cb;
@ -1071,6 +1074,7 @@ ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
warnx("Please specify an address_family.");
usage();
}
top:
ifr.ifr_addr.sa_family =
afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
@ -1081,6 +1085,9 @@ top:
(s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
ctx.io_s = s;
ctx.afp = afp;
while (argc > 0) {
p = cmd_lookup(*argv, iscreate);
if (iscreate && p == NULL) {
@ -1125,22 +1132,22 @@ top:
if (argv[1] == NULL)
errx(1, "'%s' requires argument",
p->c_name);
p->c_u.c_func(argv[1], 0, s, afp);
p->c_u.c_func(&ctx, argv[1], 0);
argc--, argv++;
} else if (p->c_parameter == OPTARG && p->c_u.c_func) {
p->c_u.c_func(argv[1], 0, s, afp);
p->c_u.c_func(&ctx, argv[1], 0);
if (argv[1] != NULL)
argc--, argv++;
} else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) {
if (argc < 3)
errx(1, "'%s' requires 2 arguments",
p->c_name);
p->c_u.c_func2(argv[1], argv[2], s, afp);
p->c_u.c_func2(&ctx, argv[1], argv[2]);
argc -= 2, argv += 2;
} else if (p->c_parameter == SPARAM && p->c_u.c_func3) {
p->c_u.c_func3(*argv, p->c_sparameter, s, afp);
p->c_u.c_func3(&ctx, *argv, p->c_sparameter);
} else if (p->c_u.c_func)
p->c_u.c_func(*argv, p->c_parameter, s, afp);
p->c_u.c_func(&ctx, *argv, p->c_parameter);
argc--, argv++;
}
@ -1148,7 +1155,7 @@ top:
* Do any post argument processing required by the address family.
*/
if (afp->af_postproc != NULL)
afp->af_postproc(s, afp, newaddr, getifflags(name, s, true));
afp->af_postproc(&ctx, newaddr, getifflags(name, s, true));
/*
* Do deferred callbacks registered while processing
* command-line arguments.
@ -1167,10 +1174,11 @@ top:
return(0);
}
/*ARGSUSED*/
static void
setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
setifaddr(if_ctx *ctx, const char *addr, int param)
{
const struct afswtch *afp = ctx->afp;
if (afp->af_getaddr == NULL)
return;
/*
@ -1185,8 +1193,9 @@ setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
}
static void
settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
settunnel(if_ctx *ctx, const char *src, const char *dst)
{
const struct afswtch *afp = ctx->afp;
struct addrinfo *srcres, *dstres;
int ecode;
@ -1208,25 +1217,23 @@ settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
errx(1,
"source and destination address families do not match");
afp->af_settunnel(s, srcres, dstres);
afp->af_settunnel(ctx->io_s, srcres, dstres);
freeaddrinfo(srcres);
freeaddrinfo(dstres);
}
/* ARGSUSED */
static void
deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
deletetunnel(if_ctx *ctx, const char *vname, int param)
{
if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
if (ioctl(ctx->io_s, SIOCDIFPHYADDR, &ifr) < 0)
err(1, "SIOCDIFPHYADDR");
}
#ifdef JAIL
static void
setifvnet(const char *jname, int dummy __unused, int s,
const struct afswtch *afp)
setifvnet(if_ctx *ctx, const char *jname, int dummy __unused)
{
struct ifreq my_ifr;
@ -1234,13 +1241,12 @@ setifvnet(const char *jname, int dummy __unused, int s,
my_ifr.ifr_jid = jail_getid(jname);
if (my_ifr.ifr_jid < 0)
errx(1, "%s", jail_errmsg);
if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
if (ioctl(ctx->io_s, SIOCSIFVNET, &my_ifr) < 0)
err(1, "SIOCSIFVNET");
}
static void
setifrvnet(const char *jname, int dummy __unused, int s,
const struct afswtch *afp)
setifrvnet(if_ctx *ctx, const char *jname, int dummy __unused)
{
struct ifreq my_ifr;
@ -1248,15 +1254,16 @@ setifrvnet(const char *jname, int dummy __unused, int s,
my_ifr.ifr_jid = jail_getid(jname);
if (my_ifr.ifr_jid < 0)
errx(1, "%s", jail_errmsg);
if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
if (ioctl(ctx->io_s, SIOCSIFRVNET, &my_ifr) < 0)
err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
}
#endif
static void
setifnetmask(const char *addr, int dummy __unused, int s,
const struct afswtch *afp)
setifnetmask(if_ctx *ctx, const char *addr, int dummy __unused)
{
const struct afswtch *afp = ctx->afp;
if (afp->af_getaddr != NULL) {
setmask++;
afp->af_getaddr(addr, MASK);
@ -1264,16 +1271,19 @@ setifnetmask(const char *addr, int dummy __unused, int s,
}
static void
setifbroadaddr(const char *addr, int dummy __unused, int s,
const struct afswtch *afp)
setifbroadaddr(if_ctx *ctx, const char *addr, int dummy __unused)
{
const struct afswtch *afp = ctx->afp;
if (afp->af_getaddr != NULL)
afp->af_getaddr(addr, DSTADDR);
}
static void
notealias(const char *addr, int param, int s, const struct afswtch *afp)
notealias(if_ctx *ctx, const char *addr, int param)
{
const struct afswtch *afp = ctx->afp;
#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
if (setaddr && doalias == 0 && param < 0)
if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
@ -1289,11 +1299,11 @@ notealias(const char *addr, int param, int s, const struct afswtch *afp)
#undef rqtosa
}
/*ARGSUSED*/
static void
setifdstaddr(const char *addr, int param __unused, int s,
const struct afswtch *afp)
setifdstaddr(if_ctx *ctx, const char *addr, int param __unused)
{
const struct afswtch *afp = ctx->afp;
if (afp->af_getaddr != NULL)
afp->af_getaddr(addr, DSTADDR);
}
@ -1328,12 +1338,12 @@ getifflags(const char *ifname, int us, bool err_ok)
* Make a private copy so we can avoid that.
*/
static void
setifflags(const char *vname, int value, int s, const struct afswtch *afp)
setifflags(if_ctx *ctx, const char *vname, int value)
{
struct ifreq my_ifr;
int flags;
flags = getifflags(name, s, false);
flags = getifflags(name, ctx->io_s, false);
if (value < 0) {
value = -value;
flags &= ~value;
@ -1343,16 +1353,16 @@ setifflags(const char *vname, int value, int s, const struct afswtch *afp)
(void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
my_ifr.ifr_flags = flags & 0xffff;
my_ifr.ifr_flagshigh = flags >> 16;
if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
if (ioctl(ctx->io_s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
Perror(vname);
}
void
setifcap(const char *vname, int value, int s, const struct afswtch *afp)
setifcap(if_ctx *ctx, const char *vname, int value)
{
int flags;
if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
if (ioctl(ctx->io_s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
Perror("ioctl (SIOCGIFCAP)");
exit(1);
}
@ -1367,12 +1377,12 @@ setifcap(const char *vname, int value, int s, const struct afswtch *afp)
if (ifr.ifr_curcap == flags)
return;
ifr.ifr_reqcap = flags;
if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
if (ioctl(ctx->io_s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
Perror(vname);
}
void
setifcapnv(const char *vname, const char *arg, int s, const struct afswtch *afp)
setifcapnv(if_ctx *ctx, const char *vname, const char *arg)
{
nvlist_t *nvcap;
void *buf;
@ -1380,7 +1390,7 @@ setifcapnv(const char *vname, const char *arg, int s, const struct afswtch *afp)
size_t nvbuflen;
bool neg;
if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0)
if (ioctl(ctx->io_s, SIOCGIFCAP, (caddr_t)&ifr) < 0)
Perror("ioctl (SIOCGIFCAP)");
if ((ifr.ifr_curcap & IFCAP_NV) == 0) {
warnx("IFCAP_NV not supported");
@ -1411,7 +1421,7 @@ setifcapnv(const char *vname, const char *arg, int s, const struct afswtch *afp)
}
ifr.ifr_cap_nv.buf_length = ifr.ifr_cap_nv.length = nvbuflen;
ifr.ifr_cap_nv.buffer = buf;
if (ioctl(s, SIOCSIFCAPNV, (caddr_t)&ifr) < 0)
if (ioctl(ctx->io_s, SIOCSIFCAPNV, (caddr_t)&ifr) < 0)
Perror(vname);
free(buf);
nvlist_destroy(nvcap);
@ -1419,27 +1429,25 @@ setifcapnv(const char *vname, const char *arg, int s, const struct afswtch *afp)
}
static void
setifmetric(const char *val, int dummy __unused, int s,
const struct afswtch *afp)
setifmetric(if_ctx *ctx, const char *val, int dummy __unused)
{
strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
ifr.ifr_metric = atoi(val);
if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
if (ioctl(ctx->io_s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
err(1, "ioctl SIOCSIFMETRIC (set metric)");
}
static void
setifmtu(const char *val, int dummy __unused, int s,
const struct afswtch *afp)
setifmtu(if_ctx *ctx, const char *val, int dummy __unused)
{
strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
ifr.ifr_mtu = atoi(val);
if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
if (ioctl(ctx->io_s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
err(1, "ioctl SIOCSIFMTU (set mtu)");
}
static void
setifpcp(const char *val, int arg __unused, int s, const struct afswtch *afp)
setifpcp(if_ctx *ctx, const char *val, int arg __unused)
{
u_long ul;
char *endp;
@ -1450,23 +1458,21 @@ setifpcp(const char *val, int arg __unused, int s, const struct afswtch *afp)
if (ul > 7)
errx(1, "value for pcp out of range");
ifr.ifr_lan_pcp = ul;
if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
if (ioctl(ctx->io_s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
err(1, "SIOCSLANPCP");
}
static void
disableifpcp(const char *val, int arg __unused, int s,
const struct afswtch *afp)
disableifpcp(if_ctx *ctx, const char *val, int arg __unused)
{
ifr.ifr_lan_pcp = IFNET_PCP_NONE;
if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
if (ioctl(ctx->io_s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
err(1, "SIOCSLANPCP");
}
static void
setifname(const char *val, int dummy __unused, int s,
const struct afswtch *afp)
setifname(if_ctx *ctx, const char *val, int dummy __unused)
{
char *newname;
@ -1476,7 +1482,7 @@ setifname(const char *val, int dummy __unused, int s,
if (newname == NULL)
err(1, "no memory to set ifname");
ifr.ifr_data = newname;
if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
if (ioctl(ctx->io_s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
free(newname);
err(1, "ioctl SIOCSIFNAME (set name)");
}
@ -1485,10 +1491,8 @@ setifname(const char *val, int dummy __unused, int s,
free(newname);
}
/* ARGSUSED */
static void
setifdescr(const char *val, int dummy __unused, int s,
const struct afswtch *afp)
setifdescr(if_ctx *ctx, const char *val, int dummy __unused)
{
char *newdescr;
@ -1507,18 +1511,16 @@ setifdescr(const char *val, int dummy __unused, int s,
}
}
if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
if (ioctl(ctx->io_s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
err(1, "ioctl SIOCSIFDESCR (set descr)");
free(newdescr);
}
/* ARGSUSED */
static void
unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
unsetifdescr(if_ctx *ctx, const char *val, int value)
{
setifdescr("", 0, s, 0);
setifdescr(ctx, "", 0);
}
#define IFFBITS \
@ -1682,6 +1684,9 @@ status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
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;
printf("%s: ", name);
printb("flags", ifa->ifa_flags, IFFBITS);
print_metric(s);
@ -1703,9 +1708,9 @@ status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
const struct afswtch *p;
p = af_getbyfamily(ift->ifa_addr->sa_family);
if (p != NULL && p->af_status != NULL)
p->af_status(s, ift);
p->af_status(ctx, ift);
} else if (args->afp->af_af == ift->ifa_addr->sa_family)
args->afp->af_status(s, ift);
args->afp->af_status(ctx, ift);
}
#if 0
if (allfamilies || afp->af_af == AF_LINK) {
@ -1725,9 +1730,9 @@ status(struct ifconfig_args *args, const struct sockaddr_dl *sdl,
}
#endif
if (allfamilies)
af_other_status(s);
af_other_status(ctx);
else if (args->afp->af_other_status != NULL)
args->afp->af_other_status(s);
args->afp->af_other_status(ctx);
print_ifstatus(s);
if (args->verbose > 0)

View File

@ -45,12 +45,13 @@
struct afswtch;
struct cmd;
struct ifconfig_context;
typedef void c_func(const char *cmd, int arg, int s, const struct afswtch *afp);
typedef void c_func2(const char *arg1, const char *arg2, int s,
const struct afswtch *afp);
typedef void c_func3(const char *cmd, const char *arg, int s,
const struct afswtch *afp);
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);
struct cmd {
const char *c_name;
@ -74,13 +75,8 @@ typedef void callback_func(int s, void *);
void callback_register(callback_func *, void *);
/*
* Macros for declaring command functions and initializing entries.
* Macros for initializing command handlers.
*/
#define DECL_CMD_FUNC(name, cmd, arg) \
void name(const char *cmd, int arg, int s, const struct afswtch *afp)
#define DECL_CMD_FUNC2(name, arg1, arg2) \
void name(const char *arg1, const char *arg2, int s, \
const struct afswtch *afp)
#define DEF_CMD(name, param, func) { \
.c_name = (name), \
@ -140,6 +136,18 @@ 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;
struct addrinfo;
@ -150,20 +158,16 @@ enum {
DSTADDR,
};
struct snl_state;
struct snl_parsed_addr;
struct snl_parsed_link;
typedef struct snl_parsed_link if_link_t;
typedef struct snl_parsed_addr if_addr_t;
struct ifconfig_args;
struct io_handler {
int s; /* socket to use for ioctls */
struct snl_state *ss; /* NETLINK_ROUTE snl(3) socket */
};
typedef void af_setvhid_f(int vhid);
typedef void af_status_nl_f(struct ifconfig_args *args, struct io_handler *h,
if_link_t *link, if_addr_t *ifa);
typedef void af_status_nl_f(if_ctx *ctx, if_link_t *link, if_addr_t *ifa);
typedef void af_status_f(if_ctx *ctx, const struct ifaddrs *);
typedef void af_other_status_f(if_ctx *ctx);
typedef void af_postproc_f(if_ctx *ctx, int newaddr, int ifflags);
struct afswtch {
const char *af_name; /* as given on cmd line, e.g. "inet" */
@ -178,17 +182,15 @@ struct afswtch {
* is presented.
*/
#ifndef WITHOUT_NETLINK
af_status_nl_f *af_status_nl;
af_status_nl_f *af_status;
#else
void (*af_status)(int, const struct ifaddrs *);
af_status_f *af_status;
#endif
void (*af_other_status)(int);
/* parse address method */
af_other_status_f *af_other_status;
void (*af_getaddr)(const char *, int);
/* parse prefix method (IPv6) */
void (*af_getprefix)(const char *, int);
void (*af_postproc)(int s, const struct afswtch *,
int newaddr, int ifflags);
af_postproc_f *af_postproc;
af_setvhid_f *af_setvhid; /* Set CARP vhid for an address */
u_long af_difaddr; /* set dst if address ioctl */
u_long af_aifaddr; /* set if address ioctl */
@ -239,12 +241,11 @@ extern int newaddr;
extern int verbose;
extern int printifname;
extern int exit_code;
extern struct ifconfig_args args;
extern struct ifconfig_args global_args;
extern char *f_inet, *f_inet6, *f_ether, *f_addr;
void setifcap(const char *, int value, int s, const struct afswtch *);
void setifcapnv(const char *vname, const char *arg, int s,
const struct afswtch *afp);
void setifcap(if_ctx *ctx, const char *, int value);
void setifcapnv(if_ctx *ctx, const char *vname, const char *arg);
void Perror(const char *cmd);
void printb(const char *s, unsigned value, const char *bits);
@ -266,7 +267,7 @@ bool group_member(const char *ifname, const char *match, const char *nomatch);
void print_ifcap(struct ifconfig_args *args, int s);
void tunnel_status(int s);
struct afswtch *af_getbyfamily(int af);
void af_other_status(int s);
void af_other_status(if_ctx *ctx);
void print_ifstatus(int s);
void print_metric(int s);

View File

@ -305,16 +305,17 @@ sort_iface_ifaddrs(struct snl_state *ss, struct iface *iface)
}
static void
status_nl(struct ifconfig_args *args, struct io_handler *h, struct iface *iface)
status_nl(if_ctx *ctx, struct iface *iface)
{
if_link_t *link = &iface->link;
struct ifconfig_args *args = ctx->args;
printf("%s: ", link->ifla_ifname);
printf("flags=%x", link->ifi_flags);
print_bits("IFF", &link->ifi_flags, 1, IFFBITS, nitems(IFFBITS));
print_metric(h->s);
print_metric(ctx->io_s);
printf(" mtu %d\n", link->ifla_mtu);
if (link->ifla_ifalias != NULL)
@ -322,40 +323,40 @@ status_nl(struct ifconfig_args *args, struct io_handler *h, struct iface *iface)
/* TODO: convert to netlink */
strlcpy(ifr.ifr_name, link->ifla_ifname, sizeof(ifr.ifr_name));
print_ifcap(args, h->s);
tunnel_status(h->s);
print_ifcap(args, ctx->io_s);
tunnel_status(ctx->io_s);
if (args->allfamilies | (args->afp != NULL && args->afp->af_af == AF_LINK)) {
/* Start with link-level */
const struct afswtch *p = af_getbyfamily(AF_LINK);
if (p != NULL && link->ifla_address != NULL)
p->af_status_nl(args, h, link, NULL);
p->af_status(ctx, link, NULL);
}
sort_iface_ifaddrs(h->ss, iface);
sort_iface_ifaddrs(ctx->io_ss, iface);
for (struct ifa *ifa = iface->ifa; ifa != NULL; ifa = ifa->next) {
if (args->allfamilies) {
const struct afswtch *p = af_getbyfamily(ifa->addr.ifa_family);
if (p != NULL)
p->af_status_nl(args, h, link, &ifa->addr);
p->af_status(ctx, link, &ifa->addr);
} else if (args->afp->af_af == ifa->addr.ifa_family) {
const struct afswtch *p = args->afp;
p->af_status_nl(args, h, link, &ifa->addr);
p->af_status(ctx, link, &ifa->addr);
}
}
/* TODO: convert to netlink */
if (args->allfamilies)
af_other_status(h->s);
af_other_status(ctx);
else if (args->afp->af_other_status != NULL)
args->afp->af_other_status(h->s);
args->afp->af_other_status(ctx);
print_ifstatus(h->s);
print_ifstatus(ctx->io_s);
if (args->verbose > 0)
sfp_status(h->s, &ifr, args->verbose);
sfp_status(ctx->io_s, &ifr, args->verbose);
}
static int
@ -382,6 +383,12 @@ void
list_interfaces_nl(struct ifconfig_args *args)
{
struct snl_state ss = {};
struct ifconfig_context _ctx = {
.args = args,
.io_s = get_local_socket(),
.io_ss = &ss,
};
struct ifconfig_context *ctx = &_ctx;
nl_init_socket(&ss);
@ -397,11 +404,6 @@ list_interfaces_nl(struct ifconfig_args *args)
qsort(sorted_ifaces, ifmap->count, sizeof(void *), cmp_iface);
prepare_ifaddrs(&ss, ifmap);
struct io_handler h = {
.s = get_local_socket(),
.ss = &ss,
};
for (uint32_t i = 0, num = 0; i < ifmap->count; i++) {
struct iface *iface = sorted_ifaces[i];
@ -415,14 +417,14 @@ list_interfaces_nl(struct ifconfig_args *args)
printf(" ");
fputs(iface->link.ifla_ifname, stdout);
} else if (args->argc == 0)
status_nl(args, &h, iface);
status_nl(ctx, iface);
else
ifconfig(args->argc, args->argv, 0, args->afp);
}
if (args->namesonly)
printf("\n");
close(h.s);
close(ctx->io_s);
snl_free(&ss);
}

View File

@ -46,26 +46,25 @@
#include "ifconfig.h"
static void
fib_status(int s)
fib_status(if_ctx *ctx)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFFIB, (caddr_t)&ifr) == 0 &&
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));
if (ioctl(s, SIOCGTUNFIB, (caddr_t)&ifr) == 0 &&
if (ioctl_ctx(ctx, SIOCGTUNFIB, (caddr_t)&ifr) == 0 &&
ifr.ifr_fib != RT_DEFAULT_FIB)
printf("\ttunnelfib: %u\n", ifr.ifr_fib);
}
static void
setiffib(const char *val, int dummy __unused, int s,
const struct afswtch *afp)
setiffib(if_ctx *ctx, const char *val, int dummy __unused)
{
unsigned long fib;
char *ep;
@ -78,13 +77,12 @@ setiffib(const char *val, int dummy __unused, int s,
strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
ifr.ifr_fib = fib;
if (ioctl(s, SIOCSIFFIB, (caddr_t)&ifr) < 0)
if (ioctl(ctx->io_s, SIOCSIFFIB, (caddr_t)&ifr) < 0)
warn("ioctl (SIOCSIFFIB)");
}
static void
settunfib(const char *val, int dummy __unused, int s,
const struct afswtch *afp)
settunfib(if_ctx *ctx, const char *val, int dummy __unused)
{
unsigned long fib;
char *ep;
@ -97,7 +95,7 @@ settunfib(const char *val, int dummy __unused, int s,
strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
ifr.ifr_fib = fib;
if (ioctl(s, SIOCSTUNFIB, (caddr_t)&ifr) < 0)
if (ioctl(ctx->io_s, SIOCSTUNFIB, (caddr_t)&ifr) < 0)
warn("ioctl (SIOCSTUNFIB)");
}

View File

@ -55,15 +55,13 @@ static const char rcsid[] =
#define GIFBITS "\020\2IGNORE_SOURCE"
static void gif_status(int);
static void
gif_status(int s)
gif_status(if_ctx *ctx)
{
int opts;
ifr.ifr_data = (caddr_t)&opts;
if (ioctl(s, GIFGOPTS, &ifr) == -1)
if (ioctl_ctx(ctx, GIFGOPTS, &ifr) == -1)
return;
if (opts == 0)
return;
@ -72,12 +70,12 @@ gif_status(int s)
}
static void
setgifopts(const char *val, int d, int s, const struct afswtch *afp)
setgifopts(if_ctx *ctx, const char *val, int d)
{
int opts;
ifr.ifr_data = (caddr_t)&opts;
if (ioctl(s, GIFGOPTS, &ifr) == -1) {
if (ioctl_ctx(ctx, GIFGOPTS, &ifr) == -1) {
warn("ioctl(GIFGOPTS)");
return;
}
@ -87,7 +85,7 @@ setgifopts(const char *val, int d, int s, const struct afswtch *afp)
else
opts |= d;
if (ioctl(s, GIFSOPTS, &ifr) == -1) {
if (ioctl_ctx(ctx, GIFSOPTS, &ifr) == -1) {
warn("ioctl(GIFSOPTS)");
return;
}

View File

@ -46,61 +46,57 @@ __FBSDID("$FreeBSD$");
#define GREBITS "\020\01ENABLE_CSUM\02ENABLE_SEQ\03UDPENCAP"
static void gre_status(int s);
static void
gre_status(int s)
gre_status(if_ctx *ctx)
{
uint32_t opts, port;
opts = 0;
ifr.ifr_data = (caddr_t)&opts;
if (ioctl(s, GREGKEY, &ifr) == 0)
if (ioctl_ctx(ctx, GREGKEY, &ifr) == 0)
if (opts != 0)
printf("\tgrekey: 0x%x (%u)\n", opts, opts);
opts = 0;
if (ioctl(s, GREGOPTS, &ifr) != 0 || opts == 0)
if (ioctl_ctx(ctx, GREGOPTS, &ifr) != 0 || opts == 0)
return;
port = 0;
ifr.ifr_data = (caddr_t)&port;
if (ioctl(s, GREGPORT, &ifr) == 0 && port != 0)
if (ioctl_ctx(ctx, GREGPORT, &ifr) == 0 && port != 0)
printf("\tudpport: %u\n", port);
printb("\toptions", opts, GREBITS);
putchar('\n');
}
static void
setifgrekey(const char *val, int dummy __unused, int s,
const struct afswtch *afp)
setifgrekey(if_ctx *ctx, const char *val, int dummy __unused)
{
uint32_t grekey = strtol(val, NULL, 0);
strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
ifr.ifr_data = (caddr_t)&grekey;
if (ioctl(s, GRESKEY, (caddr_t)&ifr) < 0)
if (ioctl(ctx->io_s, GRESKEY, (caddr_t)&ifr) < 0)
warn("ioctl (set grekey)");
}
static void
setifgreport(const char *val, int dummy __unused, int s,
const struct afswtch *afp)
setifgreport(if_ctx *ctx, const char *val, int dummy __unused)
{
uint32_t udpport = strtol(val, NULL, 0);
strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
ifr.ifr_data = (caddr_t)&udpport;
if (ioctl(s, GRESPORT, (caddr_t)&ifr) < 0)
if (ioctl(ctx->io_s, GRESPORT, (caddr_t)&ifr) < 0)
warn("ioctl (set udpport)");
}
static void
setifgreopts(const char *val, int d, int s, const struct afswtch *afp)
setifgreopts(if_ctx *ctx, const char *val, int d)
{
uint32_t opts;
ifr.ifr_data = (caddr_t)&opts;
if (ioctl(s, GREGOPTS, &ifr) == -1) {
if (ioctl(ctx->io_s, GREGOPTS, &ifr) == -1) {
warn("ioctl(GREGOPTS)");
return;
}
@ -110,7 +106,7 @@ setifgreopts(const char *val, int d, int s, const struct afswtch *afp)
else
opts |= d;
if (ioctl(s, GRESOPTS, &ifr) == -1) {
if (ioctl(ctx->io_s, GRESOPTS, &ifr) == -1) {
warn("ioctl(GIFSOPTS)");
return;
}

View File

@ -47,9 +47,8 @@ static const char rcsid[] =
#include "ifconfig.h"
/* ARGSUSED */
static void
setifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
setifgroup(if_ctx *ctx, const char *group_name, int dummy __unused)
{
struct ifgroupreq ifgr;
@ -61,13 +60,12 @@ setifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
errx(1, "setifgroup: group name too long");
if (ioctl(s, SIOCAIFGROUP, (caddr_t)&ifgr) == -1 && errno != EEXIST)
if (ioctl_ctx(ctx, SIOCAIFGROUP, (caddr_t)&ifgr) == -1 && errno != EEXIST)
err(1," SIOCAIFGROUP");
}
/* ARGSUSED */
static void
unsetifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
unsetifgroup(if_ctx *ctx, const char *group_name, int dummy __unused)
{
struct ifgroupreq ifgr;
@ -79,12 +77,12 @@ unsetifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
errx(1, "unsetifgroup: group name too long");
if (ioctl(s, SIOCDIFGROUP, (caddr_t)&ifgr) == -1 && errno != ENOENT)
if (ioctl_ctx(ctx, SIOCDIFGROUP, (caddr_t)&ifgr) == -1 && errno != ENOENT)
err(1, "SIOCDIFGROUP");
}
static void
getifgroups(int s)
getifgroups(if_ctx *ctx __unused)
{
struct ifgroupreq ifgr;
size_t cnt;

File diff suppressed because it is too large Load Diff

View File

@ -51,18 +51,18 @@ __FBSDID("$FreeBSD$");
#include "ifconfig.h"
static void
ipsec_status(int s)
ipsec_status(if_ctx *ctx)
{
uint32_t reqid;
ifr.ifr_data = (caddr_t)&reqid;
if (ioctl(s, IPSECGREQID, &ifr) == -1)
if (ioctl_ctx(ctx, IPSECGREQID, &ifr) == -1)
return;
printf("\treqid: %u\n", reqid);
}
static
DECL_CMD_FUNC(setreqid, val, arg)
static void
setreqid(if_ctx *ctx, const char *val, int dummy __unused)
{
char *ep;
uint32_t v;
@ -74,7 +74,7 @@ DECL_CMD_FUNC(setreqid, val, arg)
}
strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
ifr.ifr_data = (char *)&v;
if (ioctl(s, IPSECSREQID, &ifr) == -1) {
if (ioctl_ctx(ctx, IPSECSREQID, &ifr) == -1) {
warn("ioctl(IPSECSREQID)");
return;
}

View File

@ -39,7 +39,7 @@ static struct iflaggparam params = {
static char lacpbuf[120]; /* LACP peer '[(a,a,a),(p,p,p)]' */
static void
setlaggport(const char *val, int d, int s, const struct afswtch *afp)
setlaggport(if_ctx *ctx, const char *val, int dummy __unused)
{
struct lagg_reqport rp;
@ -53,7 +53,7 @@ setlaggport(const char *val, int d, int s, const struct afswtch *afp)
*
* Don't error at all if the port is already in the lagg.
*/
if (ioctl(s, SIOCSLAGGPORT, &rp) && errno != EEXIST) {
if (ioctl_ctx(ctx, SIOCSLAGGPORT, &rp) && errno != EEXIST) {
warnx("%s %s: SIOCSLAGGPORT: %s",
name, val, strerror(errno));
exit_code = 1;
@ -61,7 +61,7 @@ setlaggport(const char *val, int d, int s, const struct afswtch *afp)
}
static void
unsetlaggport(const char *val, int d, int s, const struct afswtch *afp)
unsetlaggport(if_ctx *ctx, const char *val, int dummy __unused)
{
struct lagg_reqport rp;
@ -69,12 +69,12 @@ unsetlaggport(const char *val, int d, int s, const struct afswtch *afp)
strlcpy(rp.rp_ifname, name, sizeof(rp.rp_ifname));
strlcpy(rp.rp_portname, val, sizeof(rp.rp_portname));
if (ioctl(s, SIOCSLAGGDELPORT, &rp))
if (ioctl_ctx(ctx, SIOCSLAGGDELPORT, &rp))
err(1, "SIOCSLAGGDELPORT");
}
static void
setlaggproto(const char *val, int d, int s, const struct afswtch *afp)
setlaggproto(if_ctx *ctx, const char *val, int dummy __unused)
{
struct lagg_protos lpr[] = LAGG_PROTOS;
struct lagg_reqall ra;
@ -92,12 +92,12 @@ setlaggproto(const char *val, int d, int s, const struct afswtch *afp)
errx(1, "Invalid aggregation protocol: %s", val);
strlcpy(ra.ra_ifname, name, sizeof(ra.ra_ifname));
if (ioctl(s, SIOCSLAGG, &ra) != 0)
if (ioctl_ctx(ctx, SIOCSLAGG, &ra) != 0)
err(1, "SIOCSLAGG");
}
static void
setlaggflowidshift(const char *val, int d, int s, const struct afswtch *afp)
setlaggflowidshift(if_ctx *ctx, const char *val, int dummy __unused)
{
struct lagg_reqopts ro;
@ -108,12 +108,12 @@ setlaggflowidshift(const char *val, int d, int s, const struct afswtch *afp)
if (ro.ro_flowid_shift & ~LAGG_OPT_FLOWIDSHIFT_MASK)
errx(1, "Invalid flowid_shift option: %s", val);
if (ioctl(s, SIOCSLAGGOPTS, &ro) != 0)
if (ioctl_ctx(ctx, SIOCSLAGGOPTS, &ro) != 0)
err(1, "SIOCSLAGGOPTS");
}
static void
setlaggrr_limit(const char *val, int d, int s, const struct afswtch *afp)
setlaggrr_limit(if_ctx *ctx, const char *val, int dummy __unused)
{
struct lagg_reqopts ro;
@ -124,12 +124,12 @@ setlaggrr_limit(const char *val, int d, int s, const struct afswtch *afp)
if (ro.ro_bkt == 0)
errx(1, "Invalid round-robin stride: %s", val);
if (ioctl(s, SIOCSLAGGOPTS, &ro) != 0)
if (ioctl_ctx(ctx, SIOCSLAGGOPTS, &ro) != 0)
err(1, "SIOCSLAGGOPTS");
}
static void
setlaggsetopt(const char *val, int d, int s, const struct afswtch *afp)
setlaggsetopt(if_ctx *ctx, const char *val __unused, int d)
{
struct lagg_reqopts ro;
@ -154,12 +154,12 @@ setlaggsetopt(const char *val, int d, int s, const struct afswtch *afp)
}
strlcpy(ro.ro_ifname, name, sizeof(ro.ro_ifname));
if (ioctl(s, SIOCSLAGGOPTS, &ro) != 0)
if (ioctl_ctx(ctx, SIOCSLAGGOPTS, &ro) != 0)
err(1, "SIOCSLAGGOPTS");
}
static void
setlagghash(const char *val, int d, int s, const struct afswtch *afp)
setlagghash(if_ctx *ctx, const char *val, int dummy __unused)
{
struct lagg_reqflags rf;
char *str, *tmp, *tok;
@ -182,7 +182,7 @@ setlagghash(const char *val, int d, int s, const struct afswtch *afp)
errx(1, "No lagghash options supplied");
strlcpy(rf.rf_ifname, name, sizeof(rf.rf_ifname));
if (ioctl(s, SIOCSLAGGHASH, &rf))
if (ioctl_ctx(ctx, SIOCSLAGGHASH, &rf))
err(1, "SIOCSLAGGHASH");
}
@ -215,7 +215,7 @@ lacp_format_peer(struct lacp_opreq *req, const char *sep)
}
static void
lagg_status(int s)
lagg_status(if_ctx *ctx __unused)
{
struct lagg_protos protos[] = LAGG_PROTOS;
struct ifconfig_lagg_status *lagg;
@ -293,8 +293,8 @@ lagg_status(int s)
ifconfig_lagg_free_lagg_status(lagg);
}
static
DECL_CMD_FUNC(setlaggtype, arg, d)
static void
setlaggtype(if_ctx *ctx, const char *arg, int dummy __unused)
{
static const struct lagg_types lt[] = LAGG_TYPES;

View File

@ -52,7 +52,7 @@
#include "ifconfig.h"
static void
maclabel_status(int s)
maclabel_status(if_ctx *ctx)
{
struct ifreq ifr;
mac_t label;
@ -64,7 +64,7 @@ maclabel_status(int s)
if (mac_prepare_ifnet_label(&label) == -1)
return;
ifr.ifr_ifru.ifru_data = (void *)label;
if (ioctl(s, SIOCGIFMAC, &ifr) == -1)
if (ioctl_ctx(ctx, SIOCGIFMAC, &ifr) == -1)
goto mac_free;
@ -80,7 +80,7 @@ mac_free:
}
static void
setifmaclabel(const char *val, int d, int s, const struct afswtch *rafp)
setifmaclabel(if_ctx *ctx, const char *val, int d __unused)
{
struct ifreq ifr;
mac_t label;
@ -95,7 +95,7 @@ setifmaclabel(const char *val, int d, int s, const struct afswtch *rafp)
strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
ifr.ifr_ifru.ifru_data = (void *)label;
error = ioctl(s, SIOCSIFMAC, &ifr);
error = ioctl(ctx->io_s, SIOCSIFMAC, &ifr);
mac_free(label);
if (error == -1)
perror("setifmac");

View File

@ -98,7 +98,7 @@ static void print_media(ifmedia_t, bool);
static void print_media_ifconfig(ifmedia_t);
static void
media_status(int s)
media_status(if_ctx *ctx __unused)
{
struct ifmediareq *ifmr;
@ -144,7 +144,7 @@ media_status(int s)
putchar('\n');
}
if (args.supmedia) {
if (global_args.supmedia) {
printf("\tsupported media:\n");
for (int i = 0; i < ifmr->ifm_count; ++i) {
printf("\t\t");
@ -190,7 +190,7 @@ setifmediacallback(int s, void *arg)
}
static void
setmedia(const char *val, int d, int s, const struct afswtch *afp)
setmedia(if_ctx *ctx __unused, const char *val, int d __unused)
{
struct ifmediareq *ifmr;
int subtype;
@ -217,17 +217,17 @@ setmedia(const char *val, int d, int s, const struct afswtch *afp)
}
static void
setmediaopt(const char *val, int d, int s, const struct afswtch *afp)
setmediaopt(if_ctx *ctx, const char *val, int d __unused)
{
domediaopt(val, false, s);
domediaopt(val, false, ctx->io_s);
}
static void
unsetmediaopt(const char *val, int d, int s, const struct afswtch *afp)
unsetmediaopt(if_ctx *ctx, const char *val, int d __unused)
{
domediaopt(val, true, s);
domediaopt(val, true, ctx->io_s);
}
static void
@ -256,7 +256,7 @@ domediaopt(const char *val, bool clear, int s)
}
static void
setmediainst(const char *val, int d, int s, const struct afswtch *afp)
setmediainst(if_ctx *ctx __unused, const char *val, int d __unused)
{
struct ifmediareq *ifmr;
int inst;
@ -275,7 +275,7 @@ setmediainst(const char *val, int d, int s, const struct afswtch *afp)
}
static void
setmediamode(const char *val, int d, int s, const struct afswtch *afp)
setmediamode(if_ctx *ctx __unused, const char *val, int d __unused)
{
struct ifmediareq *ifmr;
int mode;

View File

@ -50,15 +50,6 @@
#include "ifconfig.h"
void setpfsync_syncdev(const char *, int, int, const struct afswtch *);
void unsetpfsync_syncdev(const char *, int, int, const struct afswtch *);
void setpfsync_syncpeer(const char *, int, int, const struct afswtch *);
void unsetpfsync_syncpeer(const char *, int, int, const struct afswtch *);
void setpfsync_syncpeer(const char *, int, int, const struct afswtch *);
void setpfsync_maxupd(const char *, int, int, const struct afswtch *);
void setpfsync_defer(const char *, int, int, const struct afswtch *);
void pfsync_status(int);
static int
pfsync_do_ioctl(int s, uint cmd, nvlist_t **nvl)
{
@ -173,15 +164,15 @@ pfsync_syncpeer_nvlist_to_sockaddr(const nvlist_t *nvl,
return (0);
}
void
setpfsync_syncdev(const char *val, int d, int s, const struct afswtch *rafp)
static void
setpfsync_syncdev(if_ctx *ctx, const char *val, int dummy __unused)
{
nvlist_t *nvl = nvlist_create(0);
if (strlen(val) > IFNAMSIZ)
errx(1, "interface name %s is too long", val);
if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1)
if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1)
err(1, "SIOCGETPFSYNCNV");
if (nvlist_exists_string(nvl, "syncdev"))
@ -189,17 +180,16 @@ setpfsync_syncdev(const char *val, int d, int s, const struct afswtch *rafp)
nvlist_add_string(nvl, "syncdev", val);
if (pfsync_do_ioctl(s, SIOCSETPFSYNCNV, &nvl) == -1)
if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1)
err(1, "SIOCSETPFSYNCNV");
}
/* ARGSUSED */
void
unsetpfsync_syncdev(const char *val, int d, int s, const struct afswtch *rafp)
static void
unsetpfsync_syncdev(if_ctx *ctx, const char *val, int dummy __unused)
{
nvlist_t *nvl = nvlist_create(0);
if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1)
if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1)
err(1, "SIOCGETPFSYNCNV");
if (nvlist_exists_string(nvl, "syncdev"))
@ -207,13 +197,12 @@ unsetpfsync_syncdev(const char *val, int d, int s, const struct afswtch *rafp)
nvlist_add_string(nvl, "syncdev", "");
if (pfsync_do_ioctl(s, SIOCSETPFSYNCNV, &nvl) == -1)
if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1)
err(1, "SIOCSETPFSYNCNV");
}
/* ARGSUSED */
void
setpfsync_syncpeer(const char *val, int d, int s, const struct afswtch *rafp)
static void
setpfsync_syncpeer(if_ctx *ctx, const char *val, int dummy __unused)
{
struct addrinfo *peerres;
struct sockaddr_storage addr;
@ -221,7 +210,7 @@ setpfsync_syncpeer(const char *val, int d, int s, const struct afswtch *rafp)
nvlist_t *nvl = nvlist_create(0);
if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1)
if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1)
err(1, "SIOCGETPFSYNCNV");
if ((ecode = getaddrinfo(val, NULL, NULL, &peerres)) != 0)
@ -251,23 +240,22 @@ setpfsync_syncpeer(const char *val, int d, int s, const struct afswtch *rafp)
nvlist_add_nvlist(nvl, "syncpeer",
pfsync_sockaddr_to_syncpeer_nvlist(&addr));
if (pfsync_do_ioctl(s, SIOCSETPFSYNCNV, &nvl) == -1)
if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1)
err(1, "SIOCSETPFSYNCNV");
nvlist_destroy(nvl);
freeaddrinfo(peerres);
}
/* ARGSUSED */
void
unsetpfsync_syncpeer(const char *val, int d, int s, const struct afswtch *rafp)
static void
unsetpfsync_syncpeer(if_ctx *ctx, const char *val, int dummy __unused)
{
struct sockaddr_storage addr;
memset(&addr, 0, sizeof(addr));
nvlist_t *nvl = nvlist_create(0);
if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1)
if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1)
err(1, "SIOCGETPFSYNCNV");
if (nvlist_exists_nvlist(nvl, "syncpeer"))
@ -276,15 +264,14 @@ unsetpfsync_syncpeer(const char *val, int d, int s, const struct afswtch *rafp)
nvlist_add_nvlist(nvl, "syncpeer",
pfsync_sockaddr_to_syncpeer_nvlist(&addr));
if (pfsync_do_ioctl(s, SIOCSETPFSYNCNV, &nvl) == -1)
if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1)
err(1, "SIOCSETPFSYNCNV");
nvlist_destroy(nvl);
}
/* ARGSUSED */
void
setpfsync_maxupd(const char *val, int d, int s, const struct afswtch *rafp)
static void
setpfsync_maxupd(if_ctx *ctx, const char *val, int dummy __unused)
{
int maxupdates;
nvlist_t *nvl = nvlist_create(0);
@ -293,38 +280,37 @@ setpfsync_maxupd(const char *val, int d, int s, const struct afswtch *rafp)
if ((maxupdates < 0) || (maxupdates > 255))
errx(1, "maxupd %s: out of range", val);
if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1)
if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1)
err(1, "SIOCGETPFSYNCNV");
nvlist_free_number(nvl, "maxupdates");
nvlist_add_number(nvl, "maxupdates", maxupdates);
if (pfsync_do_ioctl(s, SIOCSETPFSYNCNV, &nvl) == -1)
if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1)
err(1, "SIOCSETPFSYNCNV");
nvlist_destroy(nvl);
}
/* ARGSUSED */
void
setpfsync_defer(const char *val, int d, int s, const struct afswtch *rafp)
static void
setpfsync_defer(if_ctx *ctx, const char *val, int d)
{
nvlist_t *nvl = nvlist_create(0);
if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1)
if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1)
err(1, "SIOCGETPFSYNCNV");
nvlist_free_number(nvl, "flags");
nvlist_add_number(nvl, "flags", d ? PFSYNCF_DEFER : 0);
if (pfsync_do_ioctl(s, SIOCSETPFSYNCNV, &nvl) == -1)
if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1)
err(1, "SIOCSETPFSYNCNV");
nvlist_destroy(nvl);
}
void
pfsync_status(int s)
static void
pfsync_status(if_ctx *ctx)
{
nvlist_t *nvl;
char syncdev[IFNAMSIZ];
@ -336,7 +322,7 @@ pfsync_status(int s)
nvl = nvlist_create(0);
if (pfsync_do_ioctl(s, SIOCGETPFSYNCNV, &nvl) == -1) {
if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) {
nvlist_destroy(nvl);
return;
}

View File

@ -52,7 +52,7 @@
#include "ifconfig.h"
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;
@ -63,15 +63,15 @@ do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
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
stf_status(int s)
stf_status(if_ctx *ctx)
{
struct stfv4args param;
if (do_cmd(s, STF6RD_GV4NET, &param, sizeof(param), 0) < 0)
if (do_cmd(ctx, STF6RD_GV4NET, &param, sizeof(param), 0) < 0)
return;
printf("\tv4net %s/%d -> ", inet_ntoa(param.srcv4_addr),
@ -80,7 +80,7 @@ stf_status(int s)
}
static void
setstf_br(const char *val, int d, int s, const struct afswtch *afp)
setstf_br(if_ctx *ctx, const char *val, int d __unused)
{
struct stfv4args req;
struct sockaddr_in sin;
@ -94,12 +94,12 @@ setstf_br(const char *val, int d, int s, const struct afswtch *afp)
errx(1, "%s: bad value", val);
req.braddr = sin.sin_addr;
if (do_cmd(s, STF6RD_SBR, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx, STF6RD_SBR, &req, sizeof(req), 1) < 0)
err(1, "STF6RD_SBR%s", val);
}
static void
setstf_set(const char *val, int d, int s, const struct afswtch *afp)
setstf_set(if_ctx *ctx, const char *val, int d __unused)
{
struct stfv4args req;
struct sockaddr_in sin;
@ -126,7 +126,7 @@ setstf_set(const char *val, int d, int s, const struct afswtch *afp)
errx(1, "%s: bad value", val);
memcpy(&req.srcv4_addr, &sin.sin_addr, sizeof(req.srcv4_addr));
if (do_cmd(s, STF6RD_SV4NET, &req, sizeof(req), 1) < 0)
if (do_cmd(ctx, STF6RD_SV4NET, &req, sizeof(req), 1) < 0)
err(1, "STF6RD_SV4NET %s", val);
}

View File

@ -85,11 +85,11 @@ getvlan(int s, struct ifreq *ifr, struct vlanreq *vreq)
}
static void
vlan_status(int s)
vlan_status(if_ctx *ctx)
{
struct vlanreq vreq;
if (getvlan(s, &ifr, &vreq) == -1)
if (getvlan(ctx->io_s, &ifr, &vreq) == -1)
return;
printf("\tvlan: %d", vreq.vlr_tag);
printf(" vlanproto: ");
@ -103,7 +103,7 @@ vlan_status(int s)
default:
printf("0x%04x", vreq.vlr_proto);
}
if (ioctl(s, SIOCGVLANPCP, (caddr_t)&ifr) != -1)
if (ioctl_ctx(ctx, SIOCGVLANPCP, (caddr_t)&ifr) != -1)
printf(" vlanpcp: %u", ifr.ifr_vlan_pcp);
printf(" parent interface: %s", vreq.vlr_parent[0] == '\0' ?
"<none>" : vreq.vlr_parent);
@ -192,8 +192,8 @@ vlan_set(int s, struct ifreq *ifr)
}
}
static
DECL_CMD_FUNC(setvlantag, val, d)
static void
setvlantag(if_ctx *ctx, const char *val, int dummy __unused)
{
struct vlanreq vreq;
u_long ul;
@ -207,26 +207,26 @@ DECL_CMD_FUNC(setvlantag, val, d)
if (params.vlr_tag != ul)
errx(1, "value for vlan out of range");
if (getvlan(s, &ifr, &vreq) != -1) {
if (getvlan(ctx->io_s, &ifr, &vreq) != -1) {
vreq.vlr_tag = params.vlr_tag;
memcpy(&params, &vreq, sizeof(params));
vlan_set(s, &ifr);
vlan_set(ctx->io_s, &ifr);
}
}
static
DECL_CMD_FUNC(setvlandev, val, d)
static void
setvlandev(if_ctx *ctx, const char *val, int dummy __unused)
{
struct vlanreq vreq;
strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent));
if (getvlan(s, &ifr, &vreq) != -1)
vlan_set(s, &ifr);
if (getvlan(ctx->io_s, &ifr, &vreq) != -1)
vlan_set(ctx->io_s, &ifr);
}
static
DECL_CMD_FUNC(setvlanproto, val, d)
static void
setvlanproto(if_ctx *ctx, const char *val, int dummy __unused)
{
struct vlanreq vreq;
@ -239,15 +239,15 @@ DECL_CMD_FUNC(setvlanproto, val, d)
} else
errx(1, "invalid value for vlanproto");
if (getvlan(s, &ifr, &vreq) != -1) {
if (getvlan(ctx->io_s, &ifr, &vreq) != -1) {
vreq.vlr_proto = params.vlr_proto;
memcpy(&params, &vreq, sizeof(params));
vlan_set(s, &ifr);
vlan_set(ctx->io_s, &ifr);
}
}
static
DECL_CMD_FUNC(setvlanpcp, val, d)
static void
setvlanpcp(if_ctx *ctx, const char *val, int dummy __unused)
{
u_long ul;
char *endp;
@ -258,25 +258,25 @@ DECL_CMD_FUNC(setvlanpcp, val, d)
if (ul > 7)
errx(1, "value for vlanpcp out of range");
ifr.ifr_vlan_pcp = ul;
if (ioctl(s, SIOCSVLANPCP, (caddr_t)&ifr) == -1)
if (ioctl(ctx->io_s, SIOCSVLANPCP, (caddr_t)&ifr) == -1)
err(1, "SIOCSVLANPCP");
}
static
DECL_CMD_FUNC(unsetvlandev, val, d)
static void
unsetvlandev(if_ctx *ctx, const char *val, int dummy __unused)
{
struct vlanreq vreq;
bzero((char *)&vreq, sizeof(struct vlanreq));
ifr.ifr_data = (caddr_t)&vreq;
if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
if (ioctl(ctx->io_s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
err(1, "SIOCGETVLAN");
bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent));
vreq.vlr_tag = 0;
if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
if (ioctl(ctx->io_s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
err(1, "SIOCSETVLAN");
}

View File

@ -73,7 +73,7 @@ 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;
@ -84,21 +84,21 @@ do_cmd(int sock, u_long op, void *arg, size_t argsize, int set)
ifd.ifd_len = argsize;
ifd.ifd_data = arg;
return (ioctl(sock, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
return (ioctl(ctx->io_s, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd));
}
static int
vxlan_exists(int sock)
vxlan_exists(if_ctx *ctx)
{
struct ifvxlancfg cfg;
bzero(&cfg, sizeof(cfg));
return (do_cmd(sock, VXLAN_CMD_GET_CONFIG, &cfg, sizeof(cfg), 0) != -1);
return (do_cmd(ctx, VXLAN_CMD_GET_CONFIG, &cfg, sizeof(cfg), 0) != -1);
}
static void
vxlan_status(int s)
vxlan_status(if_ctx *ctx)
{
struct ifvxlancfg cfg;
char src[NI_MAXHOST], dst[NI_MAXHOST];
@ -108,7 +108,7 @@ vxlan_status(int s)
bzero(&cfg, sizeof(cfg));
if (do_cmd(s, VXLAN_CMD_GET_CONFIG, &cfg, sizeof(cfg), 0) < 0)
if (do_cmd(ctx, VXLAN_CMD_GET_CONFIG, &cfg, sizeof(cfg), 0) < 0)
return;
vni = cfg.vxlc_vni;
@ -194,8 +194,8 @@ vxlan_create(int s, struct ifreq *ifr)
ioctl_ifcreate(s, ifr);
}
static
DECL_CMD_FUNC(setvxlan_vni, arg, d)
static void
setvxlan_vni(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifvxlancmd cmd;
u_long val;
@ -203,7 +203,7 @@ DECL_CMD_FUNC(setvxlan_vni, arg, d)
if (get_val(arg, &val) < 0 || val >= VXLAN_VNI_MAX)
errx(1, "invalid network identifier: %s", arg);
if (!vxlan_exists(s)) {
if (!vxlan_exists(ctx)) {
params.vxlp_with |= VXLAN_PARAM_WITH_VNI;
params.vxlp_vni = val;
return;
@ -212,12 +212,12 @@ DECL_CMD_FUNC(setvxlan_vni, arg, d)
bzero(&cmd, sizeof(cmd));
cmd.vxlcmd_vni = val;
if (do_cmd(s, VXLAN_CMD_SET_VNI, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_SET_VNI, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_SET_VNI");
}
static
DECL_CMD_FUNC(setvxlan_local, addr, d)
static void
setvxlan_local(if_ctx *ctx, const char *addr, int dummy __unused)
{
struct ifvxlancmd cmd;
struct addrinfo *ai;
@ -261,7 +261,7 @@ DECL_CMD_FUNC(setvxlan_local, addr, d)
freeaddrinfo(ai);
if (!vxlan_exists(s)) {
if (!vxlan_exists(ctx)) {
if (cmd.vxlcmd_sa.sa.sa_family == AF_INET) {
params.vxlp_with |= VXLAN_PARAM_WITH_LOCAL_ADDR4;
params.vxlp_local_sa.in4 = cmd.vxlcmd_sa.in4;
@ -272,12 +272,12 @@ DECL_CMD_FUNC(setvxlan_local, addr, d)
return;
}
if (do_cmd(s, VXLAN_CMD_SET_LOCAL_ADDR, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_SET_LOCAL_ADDR, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_SET_LOCAL_ADDR");
}
static
DECL_CMD_FUNC(setvxlan_remote, addr, d)
static void
setvxlan_remote(if_ctx *ctx, const char *addr, int dummy __unused)
{
struct ifvxlancmd cmd;
struct addrinfo *ai;
@ -321,7 +321,7 @@ DECL_CMD_FUNC(setvxlan_remote, addr, d)
freeaddrinfo(ai);
if (!vxlan_exists(s)) {
if (!vxlan_exists(ctx)) {
if (cmd.vxlcmd_sa.sa.sa_family == AF_INET) {
params.vxlp_with |= VXLAN_PARAM_WITH_REMOTE_ADDR4;
params.vxlp_remote_sa.in4 = cmd.vxlcmd_sa.in4;
@ -332,12 +332,12 @@ DECL_CMD_FUNC(setvxlan_remote, addr, d)
return;
}
if (do_cmd(s, VXLAN_CMD_SET_REMOTE_ADDR, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_SET_REMOTE_ADDR, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_SET_REMOTE_ADDR");
}
static
DECL_CMD_FUNC(setvxlan_group, addr, d)
static void
setvxlan_group(if_ctx *ctx, const char *addr, int dummy __unused)
{
struct ifvxlancmd cmd;
struct addrinfo *ai;
@ -381,7 +381,7 @@ DECL_CMD_FUNC(setvxlan_group, addr, d)
freeaddrinfo(ai);
if (!vxlan_exists(s)) {
if (!vxlan_exists(ctx)) {
if (cmd.vxlcmd_sa.sa.sa_family == AF_INET) {
params.vxlp_with |= VXLAN_PARAM_WITH_REMOTE_ADDR4;
params.vxlp_remote_sa.in4 = cmd.vxlcmd_sa.in4;
@ -392,12 +392,12 @@ DECL_CMD_FUNC(setvxlan_group, addr, d)
return;
}
if (do_cmd(s, VXLAN_CMD_SET_REMOTE_ADDR, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_SET_REMOTE_ADDR, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_SET_REMOTE_ADDR");
}
static
DECL_CMD_FUNC(setvxlan_local_port, arg, d)
static void
setvxlan_local_port(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifvxlancmd cmd;
u_long val;
@ -405,7 +405,7 @@ DECL_CMD_FUNC(setvxlan_local_port, arg, d)
if (get_val(arg, &val) < 0 || val >= UINT16_MAX)
errx(1, "invalid local port: %s", arg);
if (!vxlan_exists(s)) {
if (!vxlan_exists(ctx)) {
params.vxlp_with |= VXLAN_PARAM_WITH_LOCAL_PORT;
params.vxlp_local_port = val;
return;
@ -414,12 +414,12 @@ DECL_CMD_FUNC(setvxlan_local_port, arg, d)
bzero(&cmd, sizeof(cmd));
cmd.vxlcmd_port = val;
if (do_cmd(s, VXLAN_CMD_SET_LOCAL_PORT, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_SET_LOCAL_PORT, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_SET_LOCAL_PORT");
}
static
DECL_CMD_FUNC(setvxlan_remote_port, arg, d)
static void
setvxlan_remote_port(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifvxlancmd cmd;
u_long val;
@ -427,7 +427,7 @@ DECL_CMD_FUNC(setvxlan_remote_port, arg, d)
if (get_val(arg, &val) < 0 || val >= UINT16_MAX)
errx(1, "invalid remote port: %s", arg);
if (!vxlan_exists(s)) {
if (!vxlan_exists(ctx)) {
params.vxlp_with |= VXLAN_PARAM_WITH_REMOTE_PORT;
params.vxlp_remote_port = val;
return;
@ -436,12 +436,12 @@ DECL_CMD_FUNC(setvxlan_remote_port, arg, d)
bzero(&cmd, sizeof(cmd));
cmd.vxlcmd_port = val;
if (do_cmd(s, VXLAN_CMD_SET_REMOTE_PORT, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_SET_REMOTE_PORT, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_SET_REMOTE_PORT");
}
static
DECL_CMD_FUNC2(setvxlan_port_range, arg1, arg2)
static void
setvxlan_port_range(if_ctx *ctx, const char *arg1, const char *arg2)
{
struct ifvxlancmd cmd;
u_long min, max;
@ -453,7 +453,7 @@ DECL_CMD_FUNC2(setvxlan_port_range, arg1, arg2)
if (max < min)
errx(1, "invalid port range");
if (!vxlan_exists(s)) {
if (!vxlan_exists(ctx)) {
params.vxlp_with |= VXLAN_PARAM_WITH_PORT_RANGE;
params.vxlp_min_port = min;
params.vxlp_max_port = max;
@ -464,12 +464,12 @@ DECL_CMD_FUNC2(setvxlan_port_range, arg1, arg2)
cmd.vxlcmd_port_min = min;
cmd.vxlcmd_port_max = max;
if (do_cmd(s, VXLAN_CMD_SET_PORT_RANGE, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_SET_PORT_RANGE, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_SET_PORT_RANGE");
}
static
DECL_CMD_FUNC(setvxlan_timeout, arg, d)
static void
setvxlan_timeout(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifvxlancmd cmd;
u_long val;
@ -477,7 +477,7 @@ DECL_CMD_FUNC(setvxlan_timeout, arg, d)
if (get_val(arg, &val) < 0 || (val & ~0xFFFFFFFF) != 0)
errx(1, "invalid timeout value: %s", arg);
if (!vxlan_exists(s)) {
if (!vxlan_exists(ctx)) {
params.vxlp_with |= VXLAN_PARAM_WITH_FTABLE_TIMEOUT;
params.vxlp_ftable_timeout = val & 0xFFFFFFFF;
return;
@ -486,12 +486,12 @@ DECL_CMD_FUNC(setvxlan_timeout, arg, d)
bzero(&cmd, sizeof(cmd));
cmd.vxlcmd_ftable_timeout = val & 0xFFFFFFFF;
if (do_cmd(s, VXLAN_CMD_SET_FTABLE_TIMEOUT, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_SET_FTABLE_TIMEOUT, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_SET_FTABLE_TIMEOUT");
}
static
DECL_CMD_FUNC(setvxlan_maxaddr, arg, d)
static void
setvxlan_maxaddr(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifvxlancmd cmd;
u_long val;
@ -499,7 +499,7 @@ DECL_CMD_FUNC(setvxlan_maxaddr, arg, d)
if (get_val(arg, &val) < 0 || (val & ~0xFFFFFFFF) != 0)
errx(1, "invalid maxaddr value: %s", arg);
if (!vxlan_exists(s)) {
if (!vxlan_exists(ctx)) {
params.vxlp_with |= VXLAN_PARAM_WITH_FTABLE_MAX;
params.vxlp_ftable_max = val & 0xFFFFFFFF;
return;
@ -508,16 +508,16 @@ DECL_CMD_FUNC(setvxlan_maxaddr, arg, d)
bzero(&cmd, sizeof(cmd));
cmd.vxlcmd_ftable_max = val & 0xFFFFFFFF;
if (do_cmd(s, VXLAN_CMD_SET_FTABLE_MAX, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_SET_FTABLE_MAX, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_SET_FTABLE_MAX");
}
static
DECL_CMD_FUNC(setvxlan_dev, arg, d)
static void
setvxlan_dev(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifvxlancmd cmd;
if (!vxlan_exists(s)) {
if (!vxlan_exists(ctx)) {
params.vxlp_with |= VXLAN_PARAM_WITH_MULTICAST_IF;
strlcpy(params.vxlp_mc_ifname, arg,
sizeof(params.vxlp_mc_ifname));
@ -527,12 +527,12 @@ DECL_CMD_FUNC(setvxlan_dev, arg, d)
bzero(&cmd, sizeof(cmd));
strlcpy(cmd.vxlcmd_ifname, arg, sizeof(cmd.vxlcmd_ifname));
if (do_cmd(s, VXLAN_CMD_SET_MULTICAST_IF, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_SET_MULTICAST_IF, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_SET_MULTICAST_IF");
}
static
DECL_CMD_FUNC(setvxlan_ttl, arg, d)
static void
setvxlan_ttl(if_ctx *ctx, const char *arg, int dummy __unused)
{
struct ifvxlancmd cmd;
u_long val;
@ -540,7 +540,7 @@ DECL_CMD_FUNC(setvxlan_ttl, arg, d)
if (get_val(arg, &val) < 0 || val > 256)
errx(1, "invalid TTL value: %s", arg);
if (!vxlan_exists(s)) {
if (!vxlan_exists(ctx)) {
params.vxlp_with |= VXLAN_PARAM_WITH_TTL;
params.vxlp_ttl = val;
return;
@ -549,16 +549,16 @@ DECL_CMD_FUNC(setvxlan_ttl, arg, d)
bzero(&cmd, sizeof(cmd));
cmd.vxlcmd_ttl = val;
if (do_cmd(s, VXLAN_CMD_SET_TTL, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_SET_TTL, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_SET_TTL");
}
static
DECL_CMD_FUNC(setvxlan_learn, arg, d)
static void
setvxlan_learn(if_ctx *ctx, const char *arg, int d)
{
struct ifvxlancmd cmd;
if (!vxlan_exists(s)) {
if (!vxlan_exists(ctx)) {
params.vxlp_with |= VXLAN_PARAM_WITH_LEARN;
params.vxlp_learn = d;
return;
@ -568,12 +568,12 @@ DECL_CMD_FUNC(setvxlan_learn, arg, d)
if (d != 0)
cmd.vxlcmd_flags |= VXLAN_CMD_FLAG_LEARN;
if (do_cmd(s, VXLAN_CMD_SET_LEARN, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_SET_LEARN, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_SET_LEARN");
}
static void
setvxlan_flush(const char *val, int d, int s, const struct afswtch *afp)
setvxlan_flush(if_ctx *ctx, const char *val __unused, int d)
{
struct ifvxlancmd cmd;
@ -581,7 +581,7 @@ setvxlan_flush(const char *val, int d, int s, const struct afswtch *afp)
if (d != 0)
cmd.vxlcmd_flags |= VXLAN_CMD_FLAG_FLUSH_ALL;
if (do_cmd(s, VXLAN_CMD_FLUSH, &cmd, sizeof(cmd), 1) < 0)
if (do_cmd(ctx, VXLAN_CMD_FLUSH, &cmd, sizeof(cmd), 1) < 0)
err(1, "VXLAN_CMD_FLUSH");
}