diff --git a/sbin/ifconfig/ifconfig.h b/sbin/ifconfig/ifconfig.h --- a/sbin/ifconfig/ifconfig.h +++ b/sbin/ifconfig/ifconfig.h @@ -274,7 +274,7 @@ struct sockaddr_dl; bool match_ether(const struct sockaddr_dl *sdl); bool match_if_flags(struct ifconfig_args *args, int if_flags); -int ifconfig(if_ctx *ctx, int iscreate, const struct afswtch *uafp); +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 tunnel_status(if_ctx *ctx); @@ -285,7 +285,7 @@ /* Netlink-related functions */ void list_interfaces_nl(struct ifconfig_args *args); -int ifconfig_wrapper_nl(if_ctx *ctx, int iscreate, +int ifconfig_nl(if_ctx *ctx, int iscreate, const struct afswtch *uafp); uint32_t if_nametoindex_nl(struct snl_state *ss, const char *ifname); diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -111,8 +111,8 @@ char *f_inet, *f_inet6, *f_ether, *f_addr; #ifdef WITHOUT_NETLINK -static void list_interfaces_ioctl(struct ifconfig_args *args); -static void status(struct ifconfig_args *args, const struct sockaddr_dl *sdl, +static void list_interfaces_ioctl(if_ctx *ctx); +static void status(if_ctx *ctx, const struct sockaddr_dl *sdl, struct ifaddrs *ifa); #endif static _Noreturn void usage(void); @@ -422,12 +422,12 @@ } static void -list_interfaces(struct ifconfig_args *args) +list_interfaces(if_ctx *ctx) { #ifdef WITHOUT_NETLINK - list_interfaces_ioctl(args); + list_interfaces_ioctl(ctx); #else - list_interfaces_nl(args); + list_interfaces_nl(ctx->args); #endif } @@ -571,19 +571,12 @@ } static int -ifconfig_wrapper(struct ifconfig_args *args, int iscreate, - const struct afswtch *uafp) +ifconfig(if_ctx *ctx, int iscreate, const struct afswtch *uafp) { - struct ifconfig_context ctx = { - .args = args, - .io_s = -1, - .ifname = args->ifname, - }; - #ifdef WITHOUT_NETLINK - return (ifconfig(&ctx, iscreate, uafp)); + return (ifconfig_ioctl(ctx, iscreate, uafp)); #else - return (ifconfig_wrapper_nl(&ctx, iscreate, uafp)); + return (ifconfig_nl(ctx, iscreate, uafp)); #endif } @@ -616,6 +609,11 @@ struct ifconfig_args _args = {}; struct ifconfig_args *args = &_args; + struct ifconfig_context ctx = { + .args = args, + .io_s = -1, + }; + f_inet = f_inet6 = f_ether = f_addr = NULL; lifh = ifconfig_open(); @@ -647,6 +645,7 @@ if (!args->all && !args->namesonly) { /* not listing, need an argument */ args->ifname = args_pop(args); + ctx.ifname = args->ifname; /* check and maybe load support for this interface */ ifmaybeload(args, args->ifname); @@ -662,7 +661,7 @@ if (isnametoolong(args->ifname)) errx(1, "%s: cloning name too long", args->ifname); - ifconfig_wrapper(args, 1, NULL); + ifconfig(&ctx, 1, NULL); exit(exit_code); } #ifdef JAIL @@ -675,7 +674,7 @@ if (isnametoolong(args->ifname)) errx(1, "%s: interface name too long", args->ifname); - ifconfig_wrapper(args, 0, NULL); + ifconfig(&ctx, 0, NULL); exit(exit_code); } #endif @@ -714,14 +713,14 @@ if (!(((flags & IFF_CANTCONFIG) != 0) || (args->downonly && (flags & IFF_UP) != 0) || (args->uponly && (flags & IFF_UP) == 0))) - ifconfig_wrapper(args, 0, args->afp); + ifconfig(&ctx, 0, args->afp); } goto done; } args->allfamilies = args->afp == NULL; - list_interfaces(args); + list_interfaces(&ctx); done: freeformat(); @@ -771,13 +770,14 @@ } static void -list_interfaces_ioctl(struct ifconfig_args *args) +list_interfaces_ioctl(if_ctx *ctx) { struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q); struct ifaddrs *ifap, *sifap, *ifa; struct ifa_order_elt *cur, *tmp; char *namecp = NULL; int ifindex; + struct ifconfig_args *args = ctx->args; if (getifaddrs(&ifap) != 0) err(EXIT_FAILURE, "getifaddrs"); @@ -840,9 +840,9 @@ ifindex++; if (args->argc > 0) - ifconfig_wrapper(args, 0, args->afp); + ifconfig(ctx, 0, args->afp); else - status(args, sdl, ifa); + status(ctx, sdl, ifa); } if (args->namesonly) printf("\n"); @@ -1089,7 +1089,7 @@ } int -ifconfig(if_ctx *orig_ctx, int iscreate, const struct afswtch *uafp) +ifconfig_ioctl(if_ctx *orig_ctx, int iscreate, const struct afswtch *uafp) { const struct afswtch *afp, *nafp; const struct cmd *p; @@ -1717,11 +1717,12 @@ * specified, show only it; otherwise, show them all. */ static void -status(struct ifconfig_args *args, const struct sockaddr_dl *sdl, +status(if_ctx *ctx, const struct sockaddr_dl *sdl, struct ifaddrs *ifa) { struct ifaddrs *ift; - int s; + int s, old_s; + struct ifconfig_args *args = ctx->args; bool allfamilies = args->afp == NULL; char *ifname = ifa->ifa_name; @@ -1735,12 +1736,8 @@ s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0); if (s < 0) err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family); - - struct ifconfig_context _ctx = { - .io_s = s, - .ifname = ifname, - }; - struct ifconfig_context *ctx = &_ctx; + old_s = ctx->io_s; + ctx->io_s = s; printf("%s: ", ifname); printb("flags", ifa->ifa_flags, IFFBITS); @@ -1794,6 +1791,7 @@ sfp_status(ctx); close(s); + ctx->io_s = old_s; return; } #endif 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 @@ -123,7 +123,7 @@ } int -ifconfig_wrapper_nl(if_ctx *ctx, int iscreate, +ifconfig_nl(if_ctx *ctx, int iscreate, const struct afswtch *uafp) { struct snl_state ss = {}; @@ -131,9 +131,10 @@ nl_init_socket(&ss); ctx->io_ss = &ss; - int error = ifconfig(ctx, iscreate, uafp); + int error = ifconfig_ioctl(ctx, iscreate, uafp); snl_free(&ss); + ctx->io_ss = NULL; return (error); } @@ -447,7 +448,7 @@ } else if (args->argc == 0) status_nl(ctx, iface); else - ifconfig(ctx, 0, args->afp); + ifconfig_ioctl(ctx, 0, args->afp); } if (args->namesonly) printf("\n");