diff options
Diffstat (limited to 'sbin/ifconfig/ifconfig.c')
-rw-r--r-- | sbin/ifconfig/ifconfig.c | 195 |
1 files changed, 132 insertions, 63 deletions
diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index b26fbaf82776..9aeb4a09ef49 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -29,17 +29,6 @@ * SUCH DAMAGE. */ -#ifndef lint -static const char copyright[] = -"@(#) Copyright (c) 1983, 1993\n\ - The Regents of the University of California. All rights reserved.\n"; -#if 0 -static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94"; -#endif -static const char rcsid[] = - "$FreeBSD$"; -#endif /* not lint */ - #include <sys/param.h> #include <sys/ioctl.h> #ifdef JAIL @@ -324,14 +313,10 @@ cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q) static void freeformat(void) { - if (f_inet != NULL) - free(f_inet); - if (f_inet6 != NULL) - free(f_inet6); - if (f_ether != NULL) - free(f_ether); - if (f_addr != NULL) - free(f_addr); + free(f_inet); + free(f_inet6); + free(f_ether); + free(f_addr); } static void setformat(char *input) @@ -341,9 +326,18 @@ static void setformat(char *input) formatstr = strdup(input); while ((category = strsep(&formatstr, ",")) != NULL) { modifier = strchr(category, ':'); - if (modifier == NULL || modifier[1] == '\0') { - warnx("Skipping invalid format specification: %s\n", - category); + if (modifier == NULL) { + if (strcmp(category, "default") == 0) { + freeformat(); + } else if (strcmp(category, "cidr") == 0) { + free(f_inet); + f_inet = strdup(category); + free(f_inet6); + f_inet6 = strdup(category); + } else { + warnx("Skipping invalid format: %s\n", + category); + } continue; } @@ -351,14 +345,19 @@ static void setformat(char *input) modifier[0] = '\0'; modifier++; - if (strcmp(category, "addr") == 0) + if (strcmp(category, "addr") == 0) { + free(f_addr); f_addr = strdup(modifier); - else if (strcmp(category, "ether") == 0) + } else if (strcmp(category, "ether") == 0) { + free(f_ether); f_ether = strdup(modifier); - else if (strcmp(category, "inet") == 0) + } else if (strcmp(category, "inet") == 0) { + free(f_inet); f_inet = strdup(modifier); - else if (strcmp(category, "inet6") == 0) + } else if (strcmp(category, "inet6") == 0) { + free(f_inet6); f_inet6 = strdup(modifier); + } } free(formatstr); } @@ -464,10 +463,13 @@ args_parse(struct ifconfig_args *args, int argc, char *argv[]) { char options[1024]; struct option *p; +#ifdef JAIL + int jid; +#endif int c; /* Parse leading line options */ - strlcpy(options, "G:adf:j:klmnuv", sizeof(options)); + strlcpy(options, "G:adDf:j:klmnuv", sizeof(options)); for (p = opts; p != NULL; p = p->next) strlcat(options, p->opt, sizeof(options)); while ((c = getopt(argc, argv, options)) != -1) { @@ -478,6 +480,9 @@ args_parse(struct ifconfig_args *args, int argc, char *argv[]) case 'd': /* restrict scan to "down" interfaces */ args->downonly = true; break; + case 'D': /* Print driver name */ + args->drivername = true; + break; case 'f': if (optarg == NULL) usage(); @@ -492,7 +497,11 @@ args_parse(struct ifconfig_args *args, int argc, char *argv[]) #ifdef JAIL if (optarg == NULL) usage(); - args->jail_name = optarg; + jid = jail_getid(optarg); + if (jid == -1) + Perror("jail not found"); + if (jail_attach(jid) != 0) + Perror("cannot attach to jail"); #else Perror("not built with jail support"); #endif @@ -609,9 +618,6 @@ main(int ac, char *av[]) { char *envformat; int flags; -#ifdef JAIL - int jid; -#endif struct ifconfig_args _args = {}; struct ifconfig_args *args = &_args; @@ -620,8 +626,6 @@ main(int ac, char *av[]) .io_s = -1, }; - f_inet = f_inet6 = f_ether = f_addr = NULL; - lifh = ifconfig_open(); if (lifh == NULL) err(EXIT_FAILURE, "ifconfig_open"); @@ -638,16 +642,6 @@ main(int ac, char *av[]) args_parse(args, ac, av); -#ifdef JAIL - if (args->jail_name) { - jid = jail_getid(args->jail_name); - if (jid == -1) - Perror("jail not found"); - if (jail_attach(jid) != 0) - Perror("cannot attach to jail"); - } -#endif - if (!args->all && !args->namesonly) { /* not listing, need an argument */ args->ifname = args_pop(args); @@ -768,7 +762,7 @@ match_afp(const struct afswtch *afp, int sa_family, const struct sockaddr_dl *sd return (true); /* special case for "ether" address family */ if (!strcmp(afp->af_name, "ether")) { - if (sdl == NULL && !match_ether(sdl)) + if (sdl == NULL || !match_ether(sdl)) return (false); return (true); } @@ -1209,6 +1203,13 @@ top: argc -= 2, argv += 2; } else if (p->c_parameter == SPARAM && p->c_u.c_func3) { p->c_u.c_func3(ctx, *argv, p->c_sparameter); + } else if (p->c_parameter == ARGVECTOR && p->c_u.c_funcv) { + int argsdone; + + argsdone = p->c_u.c_funcv(ctx, argc - 1, + (const char *const *)argv + 1); + argc -= argsdone; + argv += argsdone; } else if (p->c_u.c_func) p->c_u.c_func(ctx, *argv, p->c_parameter); argc--, argv++; @@ -1617,17 +1618,61 @@ unsetifdescr(if_ctx *ctx, const char *val __unused, int value __unused) #ifdef WITHOUT_NETLINK -#define IFFBITS \ -"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \ -"\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \ -"\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25STICKYARP" +static const char *IFFBITS[] = { + [0] = "UP", + [1] = "BROADCAST", + [2] = "DEBUG", + [3] = "LOOPBACK", + [4] = "POINTOPOINT", + [6] = "RUNNING", + [7] = "NOARP", + [8] = "PROMISC", + [9] = "ALLMULTI", + [10] = "OACTIVE", + [11] = "SIMPLEX", + [12] = "LINK0", + [13] = "LINK1", + [14] = "LINK2", + [15] = "MULTICAST", + [17] = "PPROMISC", + [18] = "MONITOR", + [19] = "STATICARP", + [20] = "STICKYARP", +}; -#define IFCAPBITS \ -"\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \ -"\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \ -"\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \ -"\26RXCSUM_IPV6\27TXCSUM_IPV6\31TXRTLMT\32HWRXTSTMP\33NOMAP\34TXTLS4\35TXTLS6" \ -"\36VXLAN_HWCSUM\37VXLAN_HWTSO\40TXTLS_RTLMT" +static const char *IFCAPBITS[] = { + [0] = "RXCSUM", + [1] = "TXCSUM", + [2] = "NETCONS", + [3] = "VLAN_MTU", + [4] = "VLAN_HWTAGGING", + [5] = "JUMBO_MTU", + [6] = "POLLING", + [7] = "VLAN_HWCSUM", + [8] = "TSO4", + [9] = "TSO6", + [10] = "LRO", + [11] = "WOL_UCAST", + [12] = "WOL_MCAST", + [13] = "WOL_MAGIC", + [14] = "TOE4", + [15] = "TOE6", + [16] = "VLAN_HWFILTER", + [18] = "VLAN_HWTSO", + [19] = "LINKSTATE", + [20] = "NETMAP", + [21] = "RXCSUM_IPV6", + [22] = "TXCSUM_IPV6", + [23] = "HWSTATS", + [24] = "TXRTLMT", + [25] = "HWRXTSTMP", + [26] = "MEXTPG", + [27] = "TXTLS4", + [28] = "TXTLS6", + [29] = "VXLAN_HWCSUM", + [30] = "VXLAN_HWTSO", + [31] = "TXTLS_RTLMT", +}; static void print_ifcap_nv(if_ctx *ctx) @@ -1699,10 +1744,12 @@ print_ifcap(if_ctx *ctx) if ((ifr.ifr_curcap & IFCAP_NV) != 0) print_ifcap_nv(ctx); else { - printb("\toptions", ifr.ifr_curcap, IFCAPBITS); + printf("\toptions=%x", ifr.ifr_curcap); + print_bits("options", &ifr.ifr_curcap, 1, IFCAPBITS, nitems(IFCAPBITS)); putchar('\n'); if (ctx->args->supmedia && ifr.ifr_reqcap != 0) { - printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS); + printf("\tcapabilities=%x", ifr.ifr_reqcap); + print_bits("capabilities", &ifr.ifr_reqcap, 1, IFCAPBITS, nitems(IFCAPBITS)); putchar('\n'); } } @@ -1790,8 +1837,8 @@ status(if_ctx *ctx, const struct sockaddr_dl *sdl __unused, struct ifaddrs *ifa) old_s = ctx->io_s; ctx->io_s = s; - printf("%s: ", ctx->ifname); - printb("flags", ifa->ifa_flags, IFFBITS); + printf("%s: flags=%x", ctx->ifname, ifa->ifa_flags); + print_bits("flags", &ifa->ifa_flags, 1, IFFBITS, nitems(IFFBITS)); print_metric(ctx); print_mtu(ctx); putchar('\n'); @@ -1877,6 +1924,29 @@ Perror(const char *cmd) Perrorc(cmd, errno); } +void +print_bits(const char *btype, uint32_t *v, const int v_count, + const char **names, const int n_count) +{ + int num = 0; + + for (int i = 0; i < v_count * 32; i++) { + bool is_set = v[i / 32] & (1U << (i % 32)); + if (is_set) { + if (num++ == 0) + printf("<"); + if (num != 1) + printf(","); + if (i < n_count) + printf("%s", names[i]); + else + printf("%s_%d", btype, i); + } + } + if (num > 0) + printf(">"); +} + /* * Print a value a la the %b format of the kernel's printf */ @@ -2010,6 +2080,8 @@ static struct cmd basic_cmds[] = { DEF_CMD_ARG("descr", setifdescr), DEF_CMD("-description", 0, unsetifdescr), DEF_CMD("-descr", 0, unsetifdescr), + DEF_CMD("allmulti", IFF_PALLMULTI, setifflags), + DEF_CMD("-allmulti", IFF_PALLMULTI, clearifflags), DEF_CMD("promisc", IFF_PPROMISC, setifflags), DEF_CMD("-promisc", IFF_PPROMISC, clearifflags), DEF_CMD("add", IFF_UP, notealias), @@ -2017,11 +2089,6 @@ static struct cmd basic_cmds[] = { DEF_CMD("-alias", -IFF_UP, notealias), DEF_CMD("delete", -IFF_UP, notealias), DEF_CMD("remove", -IFF_UP, notealias), -#ifdef notdef -#define EN_SWABIPS 0x1000 - DEF_CMD("swabips", EN_SWABIPS, setifflags), - DEF_CMD("-swabips", EN_SWABIPS, clearifflags), -#endif DEF_CMD_ARG("netmask", setifnetmask), DEF_CMD_ARG("metric", setifmetric), DEF_CMD_ARG("broadcast", setifbroadaddr), @@ -2076,6 +2143,8 @@ static struct cmd basic_cmds[] = { setifcapnv), DEF_CMD_SARG("-rxtls", "-"IFCAP2_RXTLS4_NAME ",-" IFCAP2_RXTLS6_NAME, setifcapnv), + DEF_CMD_SARG("ipsec", IFCAP2_IPSEC_OFFLOAD_NAME, setifcapnv), + DEF_CMD_SARG("-ipsec", "-"IFCAP2_IPSEC_OFFLOAD_NAME, setifcapnv), DEF_CMD("wol", IFCAP_WOL, setifcap), DEF_CMD("-wol", IFCAP_WOL, clearifcap), DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap), |