diff --git a/sbin/ifconfig/af_inet6.c b/sbin/ifconfig/af_inet6.c --- a/sbin/ifconfig/af_inet6.c +++ b/sbin/ifconfig/af_inet6.c @@ -291,7 +291,7 @@ #ifdef WITHOUT_NETLINK static void -in6_status(if_ctx *ctx __unused, const struct ifaddrs *ifa) +in6_status(if_ctx *ctx, const struct ifaddrs *ifa) { struct sockaddr_in6 *sin, null_sin = {}; struct in6_ifreq ifr6; @@ -303,7 +303,7 @@ if (sin == NULL) return; - strlcpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name)); + strlcpy(ifr6.ifr_name, ctx->ifname, sizeof(ifr6.ifr_name)); if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { warn("socket(AF_INET6,SOCK_DGRAM)"); return; diff --git a/sbin/ifconfig/af_link.c b/sbin/ifconfig/af_link.c --- a/sbin/ifconfig/af_link.c +++ b/sbin/ifconfig/af_link.c @@ -101,9 +101,11 @@ } static void -print_pcp(int s) +print_pcp(if_ctx *ctx) { - if (ioctl(s, SIOCGLANPCP, (caddr_t)&ifr) == 0 && + struct ifreq ifr = {}; + + if (ioctl_ctx_ifr(ctx, SIOCGLANPCP, &ifr) == 0 && ifr.ifr_lan_pcp != IFNET_PCP_NONE) printf("\tpcp %d\n", ifr.ifr_lan_pcp); } @@ -159,7 +161,7 @@ print_ether((const struct ether_addr *)&ifr.ifr_addr.sa_data, "hwaddr"); pcp: - print_pcp(ctx->io_s); + print_pcp(ctx); } #else @@ -196,7 +198,7 @@ } } if (convert_iftype(link->ifi_type) == IFT_ETHER) - print_pcp(ctx->io_s); + print_pcp(ctx); } #endif diff --git a/sbin/ifconfig/af_nd6.c b/sbin/ifconfig/af_nd6.c --- a/sbin/ifconfig/af_nd6.c +++ b/sbin/ifconfig/af_nd6.c @@ -69,7 +69,7 @@ "\007NO_RADR\010NO_PREFER_IFACE\011NO_DAD\020DEFAULTIF" #endif -static int isnd6defif(int); +static int isnd6defif(if_ctx *ctx, int s); void setnd6flags(if_ctx *, const char *, int); void setnd6defif(if_ctx *,const char *, int); void nd6_status(if_ctx *); @@ -77,11 +77,10 @@ void setnd6flags(if_ctx *ctx, const char *dummyaddr __unused, int d) { - struct in6_ndireq nd; + struct in6_ndireq nd = {}; int error; - memset(&nd, 0, sizeof(nd)); - strlcpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname)); + strlcpy(nd.ifname, ctx->ifname, sizeof(nd.ifname)); error = ioctl_ctx(ctx, SIOCGIFINFO_IN6, &nd); if (error) { warn("ioctl(SIOCGIFINFO_IN6)"); @@ -99,15 +98,14 @@ void setnd6defif(if_ctx *ctx, const char *dummyaddr __unused, int d) { - struct in6_ndifreq ndifreq; + struct in6_ndifreq ndifreq = {}; int ifindex; int error; - memset(&ndifreq, 0, sizeof(ndifreq)); - strlcpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname)); + strlcpy(ndifreq.ifname, ctx->ifname, sizeof(ndifreq.ifname)); if (d < 0) { - if (isnd6defif(ctx->io_s)) { + if (isnd6defif(ctx, ctx->io_s)) { /* ifindex = 0 means to remove default if */ ifindex = 0; } else @@ -124,14 +122,13 @@ } static int -isnd6defif(int s) +isnd6defif(if_ctx *ctx, int s) { - struct in6_ndifreq ndifreq; + struct in6_ndifreq ndifreq = {}; unsigned int ifindex; int error; - memset(&ndifreq, 0, sizeof(ndifreq)); - strlcpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname)); + strlcpy(ndifreq.ifname, ctx->ifname, sizeof(ndifreq.ifname)); ifindex = if_nametoindex(ndifreq.ifname); error = ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq); @@ -143,15 +140,14 @@ } void -nd6_status(if_ctx *ctx __unused) +nd6_status(if_ctx *ctx) { - struct in6_ndireq nd; + struct in6_ndireq nd = {}; int s6; int error; int isdefif; - memset(&nd, 0, sizeof(nd)); - strlcpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname)); + strlcpy(nd.ifname, ctx->ifname, sizeof(nd.ifname)); if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { if (errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT) warn("socket(AF_INET6, SOCK_DGRAM)"); @@ -164,7 +160,7 @@ close(s6); return; } - isdefif = isnd6defif(s6); + isdefif = isnd6defif(ctx, s6); close(s6); if (nd.ndi.flags == 0 && !isdefif) return; diff --git a/sbin/ifconfig/ifclone.c b/sbin/ifconfig/ifclone.c --- a/sbin/ifconfig/ifclone.c +++ b/sbin/ifconfig/ifclone.c @@ -159,8 +159,9 @@ static void clone_destroy(if_ctx *ctx, const char *cmd __unused, int d __unused) { - strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name)); - if (ioctl(ctx->io_s, SIOCIFDESTROY, &ifr) < 0) + struct ifreq ifr = {}; + + if (ioctl_ctx_ifr(ctx, SIOCIFDESTROY, &ifr) < 0) err(1, "SIOCIFDESTROY"); } diff --git a/sbin/ifconfig/ifconfig.h b/sbin/ifconfig/ifconfig.h --- a/sbin/ifconfig/ifconfig.h +++ b/sbin/ifconfig/ifconfig.h @@ -153,6 +153,7 @@ } #define ioctl_ctx(ctx, _req, ...) ioctl((ctx)->io_s, _req, ## __VA_ARGS__) +int ioctl_ctx_ifr(if_ctx *ctx, unsigned long cmd, struct ifreq *ifr); struct ifaddrs; struct addrinfo; @@ -275,12 +276,12 @@ bool match_if_flags(struct ifconfig_args *args, int if_flags); int ifconfig_ioctl(if_ctx *ctx, int iscreate, const struct afswtch *uafp); bool group_member(const char *ifname, const char *match, const char *nomatch); -void print_ifcap(struct ifconfig_args *args, int s); +void print_ifcap(if_ctx *ctx); void tunnel_status(if_ctx *ctx); struct afswtch *af_getbyfamily(int af); void af_other_status(if_ctx *ctx); void print_ifstatus(if_ctx *ctx); -void print_metric(int s); +void print_metric(if_ctx *ctx); /* Netlink-related functions */ void list_interfaces_nl(struct ifconfig_args *args); diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -206,6 +206,19 @@ strlcpy(ifname_to_print, name, sizeof(ifname_to_print)); } +static void +ifr_set_name(struct ifreq *ifr, const char *name) +{ + strlcpy(ifr->ifr_name, name, sizeof(ifr->ifr_name)); +} + +int +ioctl_ctx_ifr(if_ctx *ctx, unsigned long cmd, struct ifreq *ifr) +{ + ifr_set_name(ifr, ctx->ifname); + return (ioctl_ctx(ctx, cmd, ifr)); +} + void ifcreate_ioctl(if_ctx *ctx, struct ifreq *ifr) { @@ -1105,6 +1118,7 @@ }; struct ifconfig_context *ctx = &_ctx; + struct ifreq ifr = {}; strlcpy(ifr.ifr_name, ctx->ifname, sizeof ifr.ifr_name); afp = NULL; if (uafp != NULL) @@ -1282,8 +1296,9 @@ static void deletetunnel(if_ctx *ctx, const char *vname __unused, int param __unused) { + struct ifreq ifr = {}; - if (ioctl_ctx(ctx, SIOCDIFPHYADDR, &ifr) < 0) + if (ioctl_ctx_ifr(ctx, SIOCDIFPHYADDR, &ifr) < 0) err(1, "SIOCDIFPHYADDR"); } @@ -1291,27 +1306,25 @@ static void setifvnet(if_ctx *ctx, const char *jname, int dummy __unused) { - struct ifreq my_ifr; + struct ifreq ifr = {}; - memcpy(&my_ifr, &ifr, sizeof(my_ifr)); - my_ifr.ifr_jid = jail_getid(jname); - if (my_ifr.ifr_jid < 0) + ifr.ifr_jid = jail_getid(jname); + if (ifr.ifr_jid < 0) errx(1, "%s", jail_errmsg); - if (ioctl(ctx->io_s, SIOCSIFVNET, &my_ifr) < 0) + if (ioctl_ctx_ifr(ctx, SIOCSIFVNET, &ifr) < 0) err(1, "SIOCSIFVNET"); } static void setifrvnet(if_ctx *ctx, const char *jname, int dummy __unused) { - struct ifreq my_ifr; + struct ifreq ifr = {}; - memcpy(&my_ifr, &ifr, sizeof(my_ifr)); - my_ifr.ifr_jid = jail_getid(jname); - if (my_ifr.ifr_jid < 0) + ifr.ifr_jid = jail_getid(jname); + if (ifr.ifr_jid < 0) errx(1, "%s", jail_errmsg); - if (ioctl(ctx->io_s, SIOCSIFRVNET, &my_ifr) < 0) - err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name); + if (ioctl_ctx_ifr(ctx, SIOCSIFRVNET, &ifr) < 0) + err(1, "SIOCSIFRVNET(%d, %s)", ifr.ifr_jid, ifr.ifr_name); } #endif @@ -1413,9 +1426,10 @@ void setifcap(if_ctx *ctx, const char *vname, int value) { + struct ifreq ifr = {}; int flags; - if (ioctl(ctx->io_s, SIOCGIFCAP, (caddr_t)&ifr) < 0) { + if (ioctl_ctx_ifr(ctx, SIOCGIFCAP, &ifr) < 0) { Perror("ioctl (SIOCGIFCAP)"); exit(1); } @@ -1430,7 +1444,7 @@ if (ifr.ifr_curcap == flags) return; ifr.ifr_reqcap = flags; - if (ioctl(ctx->io_s, SIOCSIFCAP, (caddr_t)&ifr) < 0) + if (ioctl_ctx(ctx, SIOCSIFCAP, &ifr) < 0) Perror(vname); } @@ -1442,8 +1456,9 @@ char *marg, *mopt; size_t nvbuflen; bool neg; + struct ifreq ifr = {}; - if (ioctl(ctx->io_s, SIOCGIFCAP, (caddr_t)&ifr) < 0) + if (ioctl_ctx_ifr(ctx, SIOCGIFCAP, &ifr) < 0) Perror("ioctl (SIOCGIFCAP)"); if ((ifr.ifr_curcap & IFCAP_NV) == 0) { warnx("IFCAP_NV not supported"); @@ -1474,7 +1489,7 @@ } ifr.ifr_cap_nv.buf_length = ifr.ifr_cap_nv.length = nvbuflen; ifr.ifr_cap_nv.buffer = buf; - if (ioctl(ctx->io_s, SIOCSIFCAPNV, (caddr_t)&ifr) < 0) + if (ioctl_ctx(ctx, SIOCSIFCAPNV, (caddr_t)&ifr) < 0) Perror(vname); free(buf); nvlist_destroy(nvcap); @@ -1484,24 +1499,27 @@ static void setifmetric(if_ctx *ctx, const char *val, int dummy __unused) { - strlcpy(ifr.ifr_name, ctx->ifname, sizeof (ifr.ifr_name)); + struct ifreq ifr = {}; + ifr.ifr_metric = atoi(val); - if (ioctl(ctx->io_s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0) + if (ioctl_ctx_ifr(ctx, SIOCSIFMETRIC, &ifr) < 0) err(1, "ioctl SIOCSIFMETRIC (set metric)"); } static void setifmtu(if_ctx *ctx, const char *val, int dummy __unused) { - strlcpy(ifr.ifr_name, ctx->ifname, sizeof (ifr.ifr_name)); + struct ifreq ifr = {}; + ifr.ifr_mtu = atoi(val); - if (ioctl(ctx->io_s, SIOCSIFMTU, (caddr_t)&ifr) < 0) + if (ioctl_ctx_ifr(ctx, SIOCSIFMTU, &ifr) < 0) err(1, "ioctl SIOCSIFMTU (set mtu)"); } static void setifpcp(if_ctx *ctx, const char *val, int arg __unused) { + struct ifreq ifr = {}; u_long ul; char *endp; @@ -1511,31 +1529,32 @@ if (ul > 7) errx(1, "value for pcp out of range"); ifr.ifr_lan_pcp = ul; - if (ioctl(ctx->io_s, SIOCSLANPCP, (caddr_t)&ifr) == -1) + if (ioctl_ctx_ifr(ctx, SIOCSLANPCP, &ifr) == -1) err(1, "SIOCSLANPCP"); } static void disableifpcp(if_ctx *ctx, const char *val __unused, int arg __unused) { + struct ifreq ifr = {}; ifr.ifr_lan_pcp = IFNET_PCP_NONE; - if (ioctl_ctx(ctx, SIOCSLANPCP, (caddr_t)&ifr) == -1) + if (ioctl_ctx_ifr(ctx, SIOCSLANPCP, &ifr) == -1) err(1, "SIOCSLANPCP"); } static void setifname(if_ctx *ctx, const char *val, int dummy __unused) { + struct ifreq ifr = {}; char *newname; - - strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name)); + ifr_set_name(&ifr, ctx->ifname); newname = strdup(val); if (newname == NULL) err(1, "no memory to set ifname"); ifr.ifr_data = newname; - if (ioctl(ctx->io_s, SIOCSIFNAME, (caddr_t)&ifr) < 0) { + if (ioctl_ctx(ctx, SIOCSIFNAME, (caddr_t)&ifr) < 0) { free(newname); err(1, "ioctl SIOCSIFNAME (set name)"); } @@ -1546,10 +1565,9 @@ static void setifdescr(if_ctx *ctx, const char *val, int dummy __unused) { + struct ifreq ifr = {}; char *newdescr; - strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name)); - ifr.ifr_buffer.length = strlen(val) + 1; if (ifr.ifr_buffer.length == 1) { ifr.ifr_buffer.buffer = newdescr = NULL; @@ -1563,7 +1581,7 @@ } } - if (ioctl(ctx->io_s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) + if (ioctl_ctx_ifr(ctx, SIOCSIFDESCR, &ifr) < 0) err(1, "ioctl SIOCSIFDESCR (set descr)"); free(newdescr); @@ -1588,8 +1606,9 @@ "\36VXLAN_HWCSUM\37VXLAN_HWTSO\40TXTLS_RTLMT" static void -print_ifcap_nv(struct ifconfig_args *args, int s) +print_ifcap_nv(if_ctx *ctx) { + struct ifreq ifr = {}; nvlist_t *nvcap; const char *nvname; void *buf, *cookie; @@ -1601,7 +1620,7 @@ Perror("malloc"); ifr.ifr_cap_nv.buffer = buf; ifr.ifr_cap_nv.buf_length = IFR_CAP_NV_MAXBUFSIZE; - if (ioctl(s, SIOCGIFCAPNV, (caddr_t)&ifr) != 0) + if (ioctl_ctx_ifr(ctx, SIOCGIFCAPNV, &ifr) != 0) Perror("ioctl (SIOCGIFCAPNV)"); nvcap = nvlist_unpack(ifr.ifr_cap_nv.buffer, ifr.ifr_cap_nv.length, 0); @@ -1623,7 +1642,7 @@ } } } - if (args->supmedia) { + if (ctx->args->supmedia) { printf("\tcapabilities"); cookie = NULL; for (first = true;; first = false) { @@ -1641,24 +1660,25 @@ nvlist_destroy(nvcap); free(buf); - if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) != 0) + if (ioctl_ctx(ctx, SIOCGIFCAP, (caddr_t)&ifr) != 0) Perror("ioctl (SIOCGIFCAP)"); } void -print_ifcap(struct ifconfig_args *args, int s) +print_ifcap(if_ctx *ctx) { - if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) != 0) + struct ifreq ifr = {}; + + if (ioctl_ctx_ifr(ctx, SIOCGIFCAP, &ifr) != 0) return; if ((ifr.ifr_curcap & IFCAP_NV) != 0) - print_ifcap_nv(args, s); + print_ifcap_nv(ctx); else { printb("\toptions", ifr.ifr_curcap, IFCAPBITS); putchar('\n'); - if (args->supmedia && ifr.ifr_reqcap != 0) { - printb("\tcapabilities", ifr.ifr_reqcap, - IFCAPBITS); + if (ctx->args->supmedia && ifr.ifr_reqcap != 0) { + printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS); putchar('\n'); } } @@ -1675,28 +1695,35 @@ } void -print_metric(int s) +print_metric(if_ctx *ctx) { - if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1) + struct ifreq ifr = {}; + + if (ioctl_ctx_ifr(ctx, SIOCGIFMETRIC, &ifr) != -1) printf(" metric %d", ifr.ifr_metric); } #ifdef WITHOUT_NETLINK static void -print_mtu(int s) +print_mtu(if_ctx *ctx) { - if (ioctl(s, SIOCGIFMTU, &ifr) != -1) + struct ifreq ifr = {}; + + if (ioctl_ctx_ifr(ctx, SIOCGIFMTU, &ifr) != -1) printf(" mtu %d", ifr.ifr_mtu); } static void -print_description(int s) +print_description(if_ctx *ctx) { + struct ifreq ifr = {}; + + ifr_set_name(&ifr, ctx->ifname); for (;;) { if ((descr = reallocf(descr, descrlen)) != NULL) { ifr.ifr_buffer.buffer = descr; ifr.ifr_buffer.length = descrlen; - if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) { + if (ioctl_ctx(ctx, SIOCGIFDESCR, &ifr) == 0) { if (ifr.ifr_buffer.buffer == descr) { if (strlen(descr) > 0) printf("\tdescription: %s\n", @@ -1725,14 +1752,13 @@ int s, old_s; struct ifconfig_args *args = ctx->args; bool allfamilies = args->afp == NULL; - char *ifname = ifa->ifa_name; + struct ifreq ifr = {}; if (args->afp == NULL) ifr.ifr_addr.sa_family = AF_LOCAL; else ifr.ifr_addr.sa_family = args->afp->af_af == AF_LINK ? AF_LOCAL : args->afp->af_af; - strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0); if (s < 0) @@ -1740,15 +1766,15 @@ old_s = ctx->io_s; ctx->io_s = s; - printf("%s: ", ifname); + printf("%s: ", ctx->ifname); printb("flags", ifa->ifa_flags, IFFBITS); - print_metric(s); - print_mtu(s); + print_metric(ctx); + print_mtu(ctx); putchar('\n'); - print_description(s); + print_description(ctx); - print_ifcap(args, s); + print_ifcap(ctx); tunnel_status(ctx); diff --git a/sbin/ifconfig/ifconfig_netlink.c b/sbin/ifconfig/ifconfig_netlink.c --- a/sbin/ifconfig/ifconfig_netlink.c +++ b/sbin/ifconfig/ifconfig_netlink.c @@ -354,15 +354,14 @@ printf("flags=%x", link->ifi_flags); print_bits("IFF", &link->ifi_flags, 1, IFFBITS, nitems(IFFBITS)); - print_metric(ctx->io_s); + print_metric(ctx); printf(" mtu %d\n", link->ifla_mtu); if (link->ifla_ifalias != NULL) printf("\tdescription: %s\n", link->ifla_ifalias); /* TODO: convert to netlink */ - strlcpy(ifr.ifr_name, link->ifla_ifname, sizeof(ifr.ifr_name)); - print_ifcap(args, ctx->io_s); + print_ifcap(ctx); tunnel_status(ctx); if (args->allfamilies | (args->afp != NULL && args->afp->af_af == AF_LINK)) { diff --git a/sbin/ifconfig/iffib.c b/sbin/ifconfig/iffib.c --- a/sbin/ifconfig/iffib.c +++ b/sbin/ifconfig/iffib.c @@ -66,6 +66,7 @@ static void setiffib(if_ctx *ctx, const char *val, int dummy __unused) { + struct ifreq ifr = {}; unsigned long fib; char *ep; @@ -75,15 +76,15 @@ return; } - strlcpy(ifr.ifr_name, ctx->ifname, sizeof (ifr.ifr_name)); ifr.ifr_fib = fib; - if (ioctl(ctx->io_s, SIOCSIFFIB, (caddr_t)&ifr) < 0) + if (ioctl_ctx_ifr(ctx, SIOCSIFFIB, &ifr) < 0) warn("ioctl (SIOCSIFFIB)"); } static void settunfib(if_ctx *ctx, const char *val, int dummy __unused) { + struct ifreq ifr = {}; unsigned long fib; char *ep; @@ -93,9 +94,8 @@ return; } - strlcpy(ifr.ifr_name, ctx->ifname, sizeof (ifr.ifr_name)); ifr.ifr_fib = fib; - if (ioctl(ctx->io_s, SIOCSTUNFIB, (caddr_t)&ifr) < 0) + if (ioctl_ctx_ifr(ctx, SIOCSTUNFIB, &ifr) < 0) warn("ioctl (SIOCSTUNFIB)"); } diff --git a/sbin/ifconfig/ifgif.c b/sbin/ifconfig/ifgif.c --- a/sbin/ifconfig/ifgif.c +++ b/sbin/ifconfig/ifgif.c @@ -59,9 +59,9 @@ gif_status(if_ctx *ctx) { int opts; + struct ifreq ifr = { .ifr_data = (caddr_t)&opts }; - ifr.ifr_data = (caddr_t)&opts; - if (ioctl_ctx(ctx, GIFGOPTS, &ifr) == -1) + if (ioctl_ctx_ifr(ctx, GIFGOPTS, &ifr) == -1) return; if (opts == 0) return; @@ -73,9 +73,9 @@ setgifopts(if_ctx *ctx, const char *val __unused, int d) { int opts; + struct ifreq ifr = { .ifr_data = (caddr_t)&opts }; - ifr.ifr_data = (caddr_t)&opts; - if (ioctl_ctx(ctx, GIFGOPTS, &ifr) == -1) { + if (ioctl_ctx_ifr(ctx, GIFGOPTS, &ifr) == -1) { warn("ioctl(GIFGOPTS)"); return; } diff --git a/sbin/ifconfig/ifgre.c b/sbin/ifconfig/ifgre.c --- a/sbin/ifconfig/ifgre.c +++ b/sbin/ifconfig/ifgre.c @@ -49,10 +49,9 @@ static void gre_status(if_ctx *ctx) { - uint32_t opts, port; + uint32_t opts = 0, port; + struct ifreq ifr = { .ifr_data = (caddr_t)&opts }; - opts = 0; - ifr.ifr_data = (caddr_t)&opts; if (ioctl_ctx(ctx, GREGKEY, &ifr) == 0) if (opts != 0) printf("\tgrekey: 0x%x (%u)\n", opts, opts); @@ -62,7 +61,7 @@ port = 0; ifr.ifr_data = (caddr_t)&port; - if (ioctl_ctx(ctx, GREGPORT, &ifr) == 0 && port != 0) + if (ioctl_ctx_ifr(ctx, GREGPORT, &ifr) == 0 && port != 0) printf("\tudpport: %u\n", port); printb("\toptions", opts, GREBITS); putchar('\n'); @@ -72,10 +71,10 @@ setifgrekey(if_ctx *ctx, const char *val, int dummy __unused) { uint32_t grekey = strtol(val, NULL, 0); + struct ifreq ifr = { .ifr_data = (caddr_t)&grekey }; - strlcpy(ifr.ifr_name, ctx->ifname, sizeof (ifr.ifr_name)); ifr.ifr_data = (caddr_t)&grekey; - if (ioctl(ctx->io_s, GRESKEY, (caddr_t)&ifr) < 0) + if (ioctl_ctx_ifr(ctx, GRESKEY, &ifr) < 0) warn("ioctl (set grekey)"); } @@ -83,10 +82,9 @@ setifgreport(if_ctx *ctx, const char *val, int dummy __unused) { uint32_t udpport = strtol(val, NULL, 0); + struct ifreq ifr = { .ifr_data = (caddr_t)&udpport }; - strlcpy(ifr.ifr_name, ctx->ifname, sizeof (ifr.ifr_name)); - ifr.ifr_data = (caddr_t)&udpport; - if (ioctl(ctx->io_s, GRESPORT, (caddr_t)&ifr) < 0) + if (ioctl_ctx_ifr(ctx, GRESPORT, &ifr) < 0) warn("ioctl (set udpport)"); } @@ -94,9 +92,9 @@ setifgreopts(if_ctx *ctx, const char *val __unused, int d) { uint32_t opts; + struct ifreq ifr = { .ifr_data = (caddr_t)&opts }; - ifr.ifr_data = (caddr_t)&opts; - if (ioctl(ctx->io_s, GREGOPTS, &ifr) == -1) { + if (ioctl_ctx_ifr(ctx, GREGOPTS, &ifr) == -1) { warn("ioctl(GREGOPTS)"); return; } @@ -106,7 +104,7 @@ else opts |= d; - if (ioctl(ctx->io_s, GRESOPTS, &ifr) == -1) { + if (ioctl_ctx(ctx, GRESOPTS, &ifr) == -1) { warn("ioctl(GIFSOPTS)"); return; } diff --git a/sbin/ifconfig/ifipsec.c b/sbin/ifconfig/ifipsec.c --- a/sbin/ifconfig/ifipsec.c +++ b/sbin/ifconfig/ifipsec.c @@ -54,9 +54,9 @@ ipsec_status(if_ctx *ctx) { uint32_t reqid; + struct ifreq ifr = { .ifr_data = (caddr_t)&reqid }; - ifr.ifr_data = (caddr_t)&reqid; - if (ioctl_ctx(ctx, IPSECGREQID, &ifr) == -1) + if (ioctl_ctx_ifr(ctx, IPSECGREQID, &ifr) == -1) return; printf("\treqid: %u\n", reqid); } @@ -66,15 +66,14 @@ { char *ep; uint32_t v; + struct ifreq ifr = { .ifr_data = (caddr_t)&v }; v = strtoul(val, &ep, 0); if (*ep != '\0') { warn("Invalid reqid value %s", val); return; } - strlcpy(ifr.ifr_name, ctx->ifname, sizeof(ifr.ifr_name)); - ifr.ifr_data = (char *)&v; - if (ioctl_ctx(ctx, IPSECSREQID, &ifr) == -1) { + if (ioctl_ctx_ifr(ctx, IPSECSREQID, &ifr) == -1) { warn("ioctl(IPSECSREQID)"); return; } diff --git a/sbin/ifconfig/ifpfsync.c b/sbin/ifconfig/ifpfsync.c --- a/sbin/ifconfig/ifpfsync.c +++ b/sbin/ifconfig/ifpfsync.c @@ -51,10 +51,11 @@ #include "ifconfig.h" static int -pfsync_do_ioctl(int s, uint cmd, nvlist_t **nvl) +pfsync_do_ioctl(if_ctx *ctx, uint cmd, nvlist_t **nvl) { void *data; size_t nvlen; + struct ifreq ifr = {}; data = nvlist_pack(*nvl, &nvlen); @@ -64,7 +65,7 @@ ifr.ifr_cap_nv.length = nvlen; free(data); - if (ioctl(s, cmd, (caddr_t)&ifr) == -1) { + if (ioctl_ctx_ifr(ctx, cmd, &ifr) == -1) { free(ifr.ifr_cap_nv.buffer); return -1; } @@ -172,7 +173,7 @@ if (strlen(val) > IFNAMSIZ) errx(1, "interface name %s is too long", val); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); if (nvlist_exists_string(nvl, "syncdev")) @@ -180,7 +181,7 @@ nvlist_add_string(nvl, "syncdev", val); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); } @@ -189,7 +190,7 @@ { nvlist_t *nvl = nvlist_create(0); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); if (nvlist_exists_string(nvl, "syncdev")) @@ -197,7 +198,7 @@ nvlist_add_string(nvl, "syncdev", ""); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); } @@ -210,7 +211,7 @@ nvlist_t *nvl = nvlist_create(0); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); if ((ecode = getaddrinfo(val, NULL, NULL, &peerres)) != 0) @@ -239,7 +240,7 @@ nvlist_add_nvlist(nvl, "syncpeer", pfsync_sockaddr_to_syncpeer_nvlist(&addr)); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); nvlist_destroy(nvl); @@ -254,7 +255,7 @@ nvlist_t *nvl = nvlist_create(0); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); if (nvlist_exists_nvlist(nvl, "syncpeer")) @@ -263,7 +264,7 @@ nvlist_add_nvlist(nvl, "syncpeer", pfsync_sockaddr_to_syncpeer_nvlist(&addr)); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); nvlist_destroy(nvl); @@ -279,13 +280,13 @@ if ((maxupdates < 0) || (maxupdates > 255)) errx(1, "maxupd %s: out of range", val); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); nvlist_free_number(nvl, "maxupdates"); nvlist_add_number(nvl, "maxupdates", maxupdates); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); nvlist_destroy(nvl); @@ -296,13 +297,13 @@ { nvlist_t *nvl = nvlist_create(0); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); nvlist_free_number(nvl, "flags"); nvlist_add_number(nvl, "flags", d ? PFSYNCF_DEFER : 0); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); nvlist_destroy(nvl); @@ -317,13 +318,13 @@ /* Don't verify, kernel knows which versions are supported.*/ version = atoi(val); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) err(1, "SIOCGETPFSYNCNV"); nvlist_free_number(nvl, "version"); nvlist_add_number(nvl, "version", version); - if (pfsync_do_ioctl(ctx->io_s, SIOCSETPFSYNCNV, &nvl) == -1) + if (pfsync_do_ioctl(ctx, SIOCSETPFSYNCNV, &nvl) == -1) err(1, "SIOCSETPFSYNCNV"); nvlist_destroy(nvl); @@ -343,7 +344,7 @@ nvl = nvlist_create(0); - if (pfsync_do_ioctl(ctx->io_s, SIOCGETPFSYNCNV, &nvl) == -1) { + if (pfsync_do_ioctl(ctx, SIOCGETPFSYNCNV, &nvl) == -1) { nvlist_destroy(nvl); return; } diff --git a/sbin/ifconfig/ifstf.c b/sbin/ifconfig/ifstf.c --- a/sbin/ifconfig/ifstf.c +++ b/sbin/ifconfig/ifstf.c @@ -54,11 +54,9 @@ static int do_cmd(if_ctx *ctx, u_long op, void *arg, size_t argsize, int set) { - struct ifdrv ifd; + struct ifdrv ifd = {}; - memset(&ifd, 0, sizeof(ifd)); - - strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name)); + strlcpy(ifd.ifd_name, ctx->ifname, sizeof(ifd.ifd_name)); ifd.ifd_cmd = op; ifd.ifd_len = argsize; ifd.ifd_data = arg; diff --git a/sbin/ifconfig/ifvlan.c b/sbin/ifconfig/ifvlan.c --- a/sbin/ifconfig/ifvlan.c +++ b/sbin/ifconfig/ifvlan.c @@ -75,21 +75,13 @@ .vlr_proto = ETHERTYPE_VLAN, }; -static int -getvlan(int s, struct ifreq *ifr, struct vlanreq *vreq) -{ - bzero((char *)vreq, sizeof(*vreq)); - ifr->ifr_data = (caddr_t)vreq; - - return ioctl(s, SIOCGETVLAN, (caddr_t)ifr); -} - static void vlan_status(if_ctx *ctx) { - struct vlanreq vreq; + struct vlanreq vreq = {}; + struct ifreq ifr = { .ifr_data = (caddr_t)&vreq }; - if (getvlan(ctx->io_s, &ifr, &vreq) == -1) + if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) == -1) return; printf("\tvlan: %d", vreq.vlr_tag); printf(" vlanproto: "); @@ -103,7 +95,7 @@ default: printf("0x%04x", vreq.vlr_proto); } - if (ioctl_ctx(ctx, SIOCGVLANPCP, (caddr_t)&ifr) != -1) + if (ioctl_ctx_ifr(ctx, SIOCGVLANPCP, &ifr) != -1) printf(" vlanpcp: %u", ifr.ifr_vlan_pcp); printf(" parent interface: %s", vreq.vlr_parent[0] == '\0' ? "" : vreq.vlr_parent); @@ -195,7 +187,8 @@ static void setvlantag(if_ctx *ctx, const char *val, int dummy __unused) { - struct vlanreq vreq; + struct vlanreq vreq = {}; + struct ifreq ifr = { .ifr_data = (caddr_t)&vreq }; u_long ul; char *endp; @@ -207,7 +200,7 @@ if (params.vlr_tag != ul) errx(1, "value for vlan out of range"); - if (getvlan(ctx->io_s, &ifr, &vreq) != -1) { + if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) != -1) { vreq.vlr_tag = params.vlr_tag; memcpy(¶ms, &vreq, sizeof(params)); vlan_set(ctx->io_s, &ifr); @@ -217,18 +210,20 @@ static void setvlandev(if_ctx *ctx, const char *val, int dummy __unused) { - struct vlanreq vreq; + struct vlanreq vreq = {}; + struct ifreq ifr = { .ifr_data = (caddr_t)&vreq }; strlcpy(params.vlr_parent, val, sizeof(params.vlr_parent)); - if (getvlan(ctx->io_s, &ifr, &vreq) != -1) + if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) != -1) vlan_set(ctx->io_s, &ifr); } static void setvlanproto(if_ctx *ctx, const char *val, int dummy __unused) { - struct vlanreq vreq; + struct vlanreq vreq = {}; + struct ifreq ifr = { .ifr_data = (caddr_t)&vreq }; if (strncasecmp(proto_8021Q, val, strlen(proto_8021Q)) == 0) { @@ -239,7 +234,7 @@ } else errx(1, "invalid value for vlanproto"); - if (getvlan(ctx->io_s, &ifr, &vreq) != -1) { + if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) != -1) { vreq.vlr_proto = params.vlr_proto; memcpy(¶ms, &vreq, sizeof(params)); vlan_set(ctx->io_s, &ifr); @@ -251,6 +246,7 @@ { u_long ul; char *endp; + struct ifreq ifr = {}; ul = strtoul(val, &endp, 0); if (*endp != '\0') @@ -258,25 +254,23 @@ if (ul > 7) errx(1, "value for vlanpcp out of range"); ifr.ifr_vlan_pcp = ul; - if (ioctl(ctx->io_s, SIOCSVLANPCP, (caddr_t)&ifr) == -1) + if (ioctl_ctx_ifr(ctx, SIOCSVLANPCP, &ifr) == -1) err(1, "SIOCSVLANPCP"); } static void unsetvlandev(if_ctx *ctx, const char *val __unused, int dummy __unused) { - struct vlanreq vreq; - - bzero((char *)&vreq, sizeof(struct vlanreq)); - ifr.ifr_data = (caddr_t)&vreq; + struct vlanreq vreq = {}; + struct ifreq ifr = { .ifr_data = (caddr_t)&vreq }; - if (ioctl(ctx->io_s, SIOCGETVLAN, (caddr_t)&ifr) == -1) + if (ioctl_ctx_ifr(ctx, SIOCGETVLAN, &ifr) == -1) err(1, "SIOCGETVLAN"); bzero((char *)&vreq.vlr_parent, sizeof(vreq.vlr_parent)); vreq.vlr_tag = 0; - if (ioctl(ctx->io_s, SIOCSETVLAN, (caddr_t)&ifr) == -1) + if (ioctl_ctx(ctx, SIOCSETVLAN, (caddr_t)&ifr) == -1) err(1, "SIOCSETVLAN"); } diff --git a/sbin/ifconfig/ifvxlan.c b/sbin/ifconfig/ifvxlan.c --- a/sbin/ifconfig/ifvxlan.c +++ b/sbin/ifconfig/ifvxlan.c @@ -75,16 +75,14 @@ static int do_cmd(if_ctx *ctx, u_long op, void *arg, size_t argsize, int set) { - struct ifdrv ifd; + struct ifdrv ifd = {}; - bzero(&ifd, sizeof(ifd)); - - strlcpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name)); + strlcpy(ifd.ifd_name, ctx->ifname, sizeof(ifd.ifd_name)); ifd.ifd_cmd = op; ifd.ifd_len = argsize; ifd.ifd_data = arg; - return (ioctl(ctx->io_s, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd)); + return (ioctl_ctx(ctx, set ? SIOCSDRVSPEC : SIOCGDRVSPEC, &ifd)); } static int