Home | History | Annotate | Line # | Download | only in ifconfig
ifconfig.c revision 1.229
      1 /*	$NetBSD: ifconfig.c,v 1.229 2013/02/07 13:21:34 apb Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1983, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. Neither the name of the University nor the names of its contributors
     46  *    may be used to endorse or promote products derived from this software
     47  *    without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 #ifndef lint
     64 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
     65  The Regents of the University of California.  All rights reserved.");
     66 __RCSID("$NetBSD: ifconfig.c,v 1.229 2013/02/07 13:21:34 apb Exp $");
     67 #endif /* not lint */
     68 
     69 #include <sys/param.h>
     70 #include <sys/queue.h>
     71 #include <sys/socket.h>
     72 #include <sys/ioctl.h>
     73 
     74 #include <net/if.h>
     75 #include <net/if_dl.h>
     76 #include <net/if_media.h>
     77 #include <net/if_ether.h>
     78 #include <netinet/in.h>		/* XXX */
     79 #include <netinet/in_var.h>	/* XXX */
     80 
     81 #include <netdb.h>
     82 
     83 #include <sys/protosw.h>
     84 
     85 #include <assert.h>
     86 #include <ctype.h>
     87 #include <err.h>
     88 #include <errno.h>
     89 #include <stdbool.h>
     90 #include <stddef.h>
     91 #include <stdio.h>
     92 #include <stdlib.h>
     93 #include <string.h>
     94 #include <unistd.h>
     95 #include <ifaddrs.h>
     96 #include <util.h>
     97 
     98 #include "extern.h"
     99 
    100 #include "media.h"
    101 #include "parse.h"
    102 #include "env.h"
    103 #include "prog_ops.h"
    104 
    105 static bool bflag, dflag, hflag, sflag, uflag;
    106 bool lflag, Nflag, vflag, zflag;
    107 
    108 static char gflags[10 + 26 * 2 + 1] = "AabCdhlNsuvz";
    109 bool gflagset[10 + 26 * 2];
    110 
    111 static int carrier(prop_dictionary_t);
    112 static int clone_command(prop_dictionary_t, prop_dictionary_t);
    113 static void do_setifpreference(prop_dictionary_t);
    114 static int flag_index(int);
    115 static void init_afs(void);
    116 static int list_cloners(prop_dictionary_t, prop_dictionary_t);
    117 static int media_status_exec(prop_dictionary_t, prop_dictionary_t);
    118 static int no_cmds_exec(prop_dictionary_t, prop_dictionary_t);
    119 static int notrailers(prop_dictionary_t, prop_dictionary_t);
    120 static void printall(const char *, prop_dictionary_t);
    121 static int setifaddr(prop_dictionary_t, prop_dictionary_t);
    122 static int setifbroadaddr(prop_dictionary_t, prop_dictionary_t);
    123 static int setifcaps(prop_dictionary_t, prop_dictionary_t);
    124 static int setifdstormask(prop_dictionary_t, prop_dictionary_t);
    125 static int setifflags(prop_dictionary_t, prop_dictionary_t);
    126 static int setifmetric(prop_dictionary_t, prop_dictionary_t);
    127 static int setifmtu(prop_dictionary_t, prop_dictionary_t);
    128 static int setifnetmask(prop_dictionary_t, prop_dictionary_t);
    129 static int setifprefixlen(prop_dictionary_t, prop_dictionary_t);
    130 static int setlinkstr(prop_dictionary_t, prop_dictionary_t);
    131 static int unsetlinkstr(prop_dictionary_t, prop_dictionary_t);
    132 static void status(const struct sockaddr *, prop_dictionary_t,
    133     prop_dictionary_t);
    134 __dead static void usage(void);
    135 
    136 static const struct kwinst ifflagskw[] = {
    137 	  IFKW("arp", -IFF_NOARP)
    138 	, IFKW("debug", IFF_DEBUG)
    139 	, IFKW("link0", IFF_LINK0)
    140 	, IFKW("link1", IFF_LINK1)
    141 	, IFKW("link2", IFF_LINK2)
    142 	, {.k_word = "down", .k_type = KW_T_INT, .k_int = -IFF_UP}
    143 	, {.k_word = "up", .k_type = KW_T_INT, .k_int = IFF_UP}
    144 };
    145 
    146 static const struct kwinst ifcapskw[] = {
    147 	  IFKW("ip4csum-tx",	IFCAP_CSUM_IPv4_Tx)
    148 	, IFKW("ip4csum-rx",	IFCAP_CSUM_IPv4_Rx)
    149 	, IFKW("tcp4csum-tx",	IFCAP_CSUM_TCPv4_Tx)
    150 	, IFKW("tcp4csum-rx",	IFCAP_CSUM_TCPv4_Rx)
    151 	, IFKW("udp4csum-tx",	IFCAP_CSUM_UDPv4_Tx)
    152 	, IFKW("udp4csum-rx",	IFCAP_CSUM_UDPv4_Rx)
    153 	, IFKW("tcp6csum-tx",	IFCAP_CSUM_TCPv6_Tx)
    154 	, IFKW("tcp6csum-rx",	IFCAP_CSUM_TCPv6_Rx)
    155 	, IFKW("udp6csum-tx",	IFCAP_CSUM_UDPv6_Tx)
    156 	, IFKW("udp6csum-rx",	IFCAP_CSUM_UDPv6_Rx)
    157 	, IFKW("ip4csum",	IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx)
    158 	, IFKW("tcp4csum",	IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx)
    159 	, IFKW("udp4csum",	IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx)
    160 	, IFKW("tcp6csum",	IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx)
    161 	, IFKW("udp6csum",	IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx)
    162 	, IFKW("tso4",		IFCAP_TSOv4)
    163 	, IFKW("tso6",		IFCAP_TSOv6)
    164 };
    165 
    166 extern struct pbranch command_root;
    167 extern struct pbranch opt_command;
    168 extern struct pbranch opt_family, opt_silent_family;
    169 extern struct pkw cloning, silent_family, family, ifcaps, ifflags, misc;
    170 extern struct pstr parse_linkstr;
    171 
    172 struct pinteger parse_metric = PINTEGER_INITIALIZER(&parse_metric, "metric", 10,
    173     setifmetric, "metric", &command_root.pb_parser);
    174 
    175 struct pinteger parse_mtu = PINTEGER_INITIALIZER(&parse_mtu, "mtu", 10,
    176     setifmtu, "mtu", &command_root.pb_parser);
    177 
    178 struct pinteger parse_prefixlen = PINTEGER_INITIALIZER(&parse_prefixlen,
    179     "prefixlen", 10, setifprefixlen, "prefixlen", &command_root.pb_parser);
    180 
    181 struct pinteger parse_preference = PINTEGER_INITIALIZER1(&parse_preference,
    182     "preference", INT16_MIN, INT16_MAX, 10, NULL, "preference",
    183     &command_root.pb_parser);
    184 
    185 struct paddr parse_netmask = PADDR_INITIALIZER(&parse_netmask, "netmask",
    186     setifnetmask, "dstormask", NULL, NULL, NULL, &command_root.pb_parser);
    187 
    188 struct paddr parse_broadcast = PADDR_INITIALIZER(&parse_broadcast,
    189     "broadcast address",
    190     setifbroadaddr, "broadcast", NULL, NULL, NULL, &command_root.pb_parser);
    191 
    192 static const struct kwinst misckw[] = {
    193 	  {.k_word = "alias", .k_key = "alias", .k_deact = "alias",
    194 	   .k_type = KW_T_BOOL, .k_neg = true,
    195 	   .k_bool = true, .k_negbool = false,
    196 	   .k_nextparser = &command_root.pb_parser}
    197 	, {.k_word = "broadcast", .k_nextparser = &parse_broadcast.pa_parser}
    198 	, {.k_word = "delete", .k_key = "alias", .k_deact = "alias",
    199 	   .k_type = KW_T_BOOL, .k_bool = false,
    200 	   .k_nextparser = &command_root.pb_parser}
    201 	, {.k_word = "metric", .k_nextparser = &parse_metric.pi_parser}
    202 	, {.k_word = "mtu", .k_nextparser = &parse_mtu.pi_parser}
    203 	, {.k_word = "netmask", .k_nextparser = &parse_netmask.pa_parser}
    204 	, {.k_word = "preference", .k_act = "address",
    205 	   .k_nextparser = &parse_preference.pi_parser}
    206 	, {.k_word = "prefixlen", .k_nextparser = &parse_prefixlen.pi_parser}
    207 	, {.k_word = "trailers", .k_neg = true,
    208 	   .k_exec = notrailers, .k_nextparser = &command_root.pb_parser}
    209 	, {.k_word = "linkstr", .k_nextparser = &parse_linkstr.ps_parser }
    210 	, {.k_word = "-linkstr", .k_exec = unsetlinkstr,
    211 	   .k_nextparser = &command_root.pb_parser }
    212 };
    213 
    214 /* key: clonecmd */
    215 static const struct kwinst clonekw[] = {
    216 	{.k_word = "create", .k_type = KW_T_INT, .k_int = SIOCIFCREATE,
    217 	 .k_nextparser = &opt_silent_family.pb_parser},
    218 	{.k_word = "destroy", .k_type = KW_T_INT, .k_int = SIOCIFDESTROY}
    219 };
    220 
    221 static struct kwinst familykw[24];
    222 
    223 struct pterm cloneterm = PTERM_INITIALIZER(&cloneterm, "list cloners",
    224     list_cloners, "none");
    225 
    226 struct pterm no_cmds = PTERM_INITIALIZER(&no_cmds, "no commands", no_cmds_exec,
    227     "none");
    228 
    229 struct pkw family_only =
    230     PKW_INITIALIZER(&family_only, "family-only", NULL, "af", familykw,
    231 	__arraycount(familykw), &no_cmds.pt_parser);
    232 
    233 struct paddr address = PADDR_INITIALIZER(&address,
    234     "local address (address 1)",
    235     setifaddr, "address", "netmask", NULL, "address", &command_root.pb_parser);
    236 
    237 struct paddr dstormask = PADDR_INITIALIZER(&dstormask,
    238     "destination/netmask (address 2)",
    239     setifdstormask, "dstormask", NULL, "address", "dstormask",
    240     &command_root.pb_parser);
    241 
    242 struct paddr broadcast = PADDR_INITIALIZER(&broadcast,
    243     "broadcast address (address 3)",
    244     setifbroadaddr, "broadcast", NULL, "dstormask", "broadcast",
    245     &command_root.pb_parser);
    246 
    247 struct pstr parse_linkstr = PSTR_INITIALIZER(&parse_linkstr, "linkstr",
    248     setlinkstr, "linkstr", &command_root.pb_parser);
    249 
    250 static SIMPLEQ_HEAD(, afswtch) aflist = SIMPLEQ_HEAD_INITIALIZER(aflist);
    251 
    252 static SIMPLEQ_HEAD(, usage_func) usage_funcs =
    253     SIMPLEQ_HEAD_INITIALIZER(usage_funcs);
    254 static SIMPLEQ_HEAD(, status_func) status_funcs =
    255     SIMPLEQ_HEAD_INITIALIZER(status_funcs);
    256 static SIMPLEQ_HEAD(, statistics_func) statistics_funcs =
    257     SIMPLEQ_HEAD_INITIALIZER(statistics_funcs);
    258 static SIMPLEQ_HEAD(, cmdloop_branch) cmdloop_branches =
    259     SIMPLEQ_HEAD_INITIALIZER(cmdloop_branches);
    260 
    261 struct branch opt_clone_brs[] = {
    262 	  {.b_nextparser = &cloning.pk_parser}
    263 	, {.b_nextparser = &opt_family.pb_parser}
    264 }, opt_silent_family_brs[] = {
    265 	  {.b_nextparser = &silent_family.pk_parser}
    266 	, {.b_nextparser = &command_root.pb_parser}
    267 }, opt_family_brs[] = {
    268 	  {.b_nextparser = &family.pk_parser}
    269 	, {.b_nextparser = &opt_command.pb_parser}
    270 }, command_root_brs[] = {
    271 	  {.b_nextparser = &ifflags.pk_parser}
    272 	, {.b_nextparser = &ifcaps.pk_parser}
    273 	, {.b_nextparser = &kwmedia.pk_parser}
    274 	, {.b_nextparser = &misc.pk_parser}
    275 	, {.b_nextparser = &address.pa_parser}
    276 	, {.b_nextparser = &dstormask.pa_parser}
    277 	, {.b_nextparser = &broadcast.pa_parser}
    278 	, {.b_nextparser = NULL}
    279 }, opt_command_brs[] = {
    280 	  {.b_nextparser = &no_cmds.pt_parser}
    281 	, {.b_nextparser = &command_root.pb_parser}
    282 };
    283 
    284 struct branch opt_family_only_brs[] = {
    285 	  {.b_nextparser = &no_cmds.pt_parser}
    286 	, {.b_nextparser = &family_only.pk_parser}
    287 };
    288 struct pbranch opt_family_only = PBRANCH_INITIALIZER(&opt_family_only,
    289     "opt-family-only", opt_family_only_brs,
    290     __arraycount(opt_family_only_brs), true);
    291 struct pbranch opt_command = PBRANCH_INITIALIZER(&opt_command,
    292     "optional command",
    293     opt_command_brs, __arraycount(opt_command_brs), true);
    294 
    295 struct pbranch command_root = PBRANCH_INITIALIZER(&command_root,
    296     "command-root", command_root_brs, __arraycount(command_root_brs), true);
    297 
    298 struct piface iface_opt_family_only =
    299     PIFACE_INITIALIZER(&iface_opt_family_only, "iface-opt-family-only",
    300     NULL, "if", &opt_family_only.pb_parser);
    301 
    302 struct pkw family = PKW_INITIALIZER(&family, "family", NULL, "af",
    303     familykw, __arraycount(familykw), &opt_command.pb_parser);
    304 
    305 struct pkw silent_family = PKW_INITIALIZER(&silent_family, "silent family",
    306     NULL, "af", familykw, __arraycount(familykw), &command_root.pb_parser);
    307 
    308 struct pkw *family_users[] = {&family_only, &family, &silent_family};
    309 
    310 struct pkw ifcaps = PKW_INITIALIZER(&ifcaps, "ifcaps", setifcaps,
    311     "ifcap", ifcapskw, __arraycount(ifcapskw), &command_root.pb_parser);
    312 
    313 struct pkw ifflags = PKW_INITIALIZER(&ifflags, "ifflags", setifflags,
    314     "ifflag", ifflagskw, __arraycount(ifflagskw), &command_root.pb_parser);
    315 
    316 struct pkw cloning = PKW_INITIALIZER(&cloning, "cloning", clone_command,
    317     "clonecmd", clonekw, __arraycount(clonekw), NULL);
    318 
    319 struct pkw misc = PKW_INITIALIZER(&misc, "misc", NULL, NULL,
    320     misckw, __arraycount(misckw), NULL);
    321 
    322 struct pbranch opt_clone = PBRANCH_INITIALIZER(&opt_clone,
    323     "opt-clone", opt_clone_brs, __arraycount(opt_clone_brs), true);
    324 
    325 struct pbranch opt_silent_family = PBRANCH_INITIALIZER(&opt_silent_family,
    326     "optional silent family", opt_silent_family_brs,
    327     __arraycount(opt_silent_family_brs), true);
    328 
    329 struct pbranch opt_family = PBRANCH_INITIALIZER(&opt_family,
    330     "opt-family", opt_family_brs, __arraycount(opt_family_brs), true);
    331 
    332 struct piface iface_start = PIFACE_INITIALIZER(&iface_start,
    333     "iface-opt-family", NULL, "if", &opt_clone.pb_parser);
    334 
    335 struct piface iface_only = PIFACE_INITIALIZER(&iface_only, "iface",
    336     media_status_exec, "if", NULL);
    337 
    338 static bool
    339 flag_is_registered(const char *flags, int flag)
    340 {
    341 	return flags != NULL && strchr(flags, flag) != NULL;
    342 }
    343 
    344 static int
    345 check_flag(const char *flags, int flag)
    346 {
    347 	if (flag_is_registered(flags, flag)) {
    348 		errno = EEXIST;
    349 		return -1;
    350 	}
    351 
    352 	if (flag >= '0' && flag <= '9')
    353 		return 0;
    354 	if (flag >= 'a' && flag <= 'z')
    355 		return 0;
    356 	if (flag >= 'A' && flag <= 'Z')
    357 		return 0;
    358 
    359 	errno = EINVAL;
    360 	return -1;
    361 }
    362 
    363 void
    364 cmdloop_branch_init(cmdloop_branch_t *b, struct parser *p)
    365 {
    366 	b->b_parser = p;
    367 }
    368 
    369 void
    370 statistics_func_init(statistics_func_t *f, statistics_cb_t func)
    371 {
    372 	f->f_func = func;
    373 }
    374 
    375 void
    376 status_func_init(status_func_t *f, status_cb_t func)
    377 {
    378 	f->f_func = func;
    379 }
    380 
    381 void
    382 usage_func_init(usage_func_t *f, usage_cb_t func)
    383 {
    384 	f->f_func = func;
    385 }
    386 
    387 int
    388 register_cmdloop_branch(cmdloop_branch_t *b)
    389 {
    390 	SIMPLEQ_INSERT_TAIL(&cmdloop_branches, b, b_next);
    391 	return 0;
    392 }
    393 
    394 int
    395 register_statistics(statistics_func_t *f)
    396 {
    397 	SIMPLEQ_INSERT_TAIL(&statistics_funcs, f, f_next);
    398 	return 0;
    399 }
    400 
    401 int
    402 register_status(status_func_t *f)
    403 {
    404 	SIMPLEQ_INSERT_TAIL(&status_funcs, f, f_next);
    405 	return 0;
    406 }
    407 
    408 int
    409 register_usage(usage_func_t *f)
    410 {
    411 	SIMPLEQ_INSERT_TAIL(&usage_funcs, f, f_next);
    412 	return 0;
    413 }
    414 
    415 int
    416 register_family(struct afswtch *af)
    417 {
    418 	SIMPLEQ_INSERT_TAIL(&aflist, af, af_next);
    419 	return 0;
    420 }
    421 
    422 int
    423 register_flag(int flag)
    424 {
    425 	if (check_flag(gflags, flag) == -1)
    426 		return -1;
    427 
    428 	if (strlen(gflags) + 1 >= sizeof(gflags)) {
    429 		errno = ENOMEM;
    430 		return -1;
    431 	}
    432 
    433 	gflags[strlen(gflags)] = flag;
    434 
    435 	return 0;
    436 }
    437 
    438 static int
    439 flag_index(int flag)
    440 {
    441 	if (flag >= '0' && flag <= '9')
    442 		return flag - '0';
    443 	if (flag >= 'a' && flag <= 'z')
    444 		return 10 + flag - 'a';
    445 	if (flag >= 'A' && flag <= 'Z')
    446 		return 10 + 26 + flag - 'a';
    447 
    448 	errno = EINVAL;
    449 	return -1;
    450 }
    451 
    452 static bool
    453 set_flag(int flag)
    454 {
    455 	int idx;
    456 
    457 	if ((idx = flag_index(flag)) == -1)
    458 		return false;
    459 
    460 	return gflagset[idx] = true;
    461 }
    462 
    463 bool
    464 get_flag(int flag)
    465 {
    466 	int idx;
    467 
    468 	if ((idx = flag_index(flag)) == -1)
    469 		return false;
    470 
    471 	return gflagset[idx];
    472 }
    473 
    474 static struct parser *
    475 init_parser(void)
    476 {
    477 	cmdloop_branch_t *b;
    478 
    479 	if (parser_init(&iface_opt_family_only.pif_parser) == -1)
    480 		err(EXIT_FAILURE, "parser_init(iface_opt_family_only)");
    481 	if (parser_init(&iface_only.pif_parser) == -1)
    482 		err(EXIT_FAILURE, "parser_init(iface_only)");
    483 	if (parser_init(&iface_start.pif_parser) == -1)
    484 		err(EXIT_FAILURE, "parser_init(iface_start)");
    485 
    486 	SIMPLEQ_FOREACH(b, &cmdloop_branches, b_next)
    487 		pbranch_addbranch(&command_root, b->b_parser);
    488 
    489 	return &iface_start.pif_parser;
    490 }
    491 
    492 static int
    493 no_cmds_exec(prop_dictionary_t env, prop_dictionary_t oenv)
    494 {
    495 	const char *ifname;
    496 	unsigned short ignore;
    497 
    498 	/* ifname == NULL is ok.  It indicates 'ifconfig -a'. */
    499 	if ((ifname = getifname(env)) == NULL)
    500 		;
    501 	else if (getifflags(env, oenv, &ignore) == -1)
    502 		err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
    503 
    504 	printall(ifname, env);
    505 	exit(EXIT_SUCCESS);
    506 }
    507 
    508 static int
    509 media_status_exec(prop_dictionary_t env, prop_dictionary_t oenv)
    510 {
    511 	const char *ifname;
    512 	unsigned short ignore;
    513 
    514 	/* ifname == NULL is ok.  It indicates 'ifconfig -a'. */
    515 	if ((ifname = getifname(env)) == NULL)
    516 		;
    517 	else if (getifflags(env, oenv, &ignore) == -1)
    518 		err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
    519 
    520 	exit(carrier(env));
    521 }
    522 
    523 static void
    524 do_setifcaps(prop_dictionary_t env)
    525 {
    526 	struct ifcapreq ifcr;
    527 	prop_data_t d;
    528 
    529 	d = (prop_data_t )prop_dictionary_get(env, "ifcaps");
    530 	if (d == NULL)
    531 		return;
    532 
    533 	assert(sizeof(ifcr) == prop_data_size(d));
    534 
    535 	memcpy(&ifcr, prop_data_data_nocopy(d), sizeof(ifcr));
    536 	if (direct_ioctl(env, SIOCSIFCAP, &ifcr) == -1)
    537 		err(EXIT_FAILURE, "SIOCSIFCAP");
    538 }
    539 
    540 int
    541 main(int argc, char **argv)
    542 {
    543 	const struct afswtch *afp;
    544 	int af, s;
    545 	bool aflag = false, Cflag = false;
    546 	struct match match[32];
    547 	size_t nmatch;
    548 	struct parser *start;
    549 	int ch, narg = 0, rc;
    550 	prop_dictionary_t env, oenv;
    551 	const char *ifname;
    552 
    553 	memset(match, 0, sizeof(match));
    554 
    555 	init_afs();
    556 
    557 	start = init_parser();
    558 
    559 	/* Parse command-line options */
    560 	Nflag = vflag = zflag = false;
    561 	aflag = argc == 1 ? true : false;
    562 	if (aflag)
    563 		start = &opt_family_only.pb_parser;
    564 
    565 	while ((ch = getopt(argc, argv, gflags)) != -1) {
    566 		switch (ch) {
    567 		case 'A':
    568 			warnx("-A is deprecated");
    569 			break;
    570 
    571 		case 'a':
    572 			aflag = true;
    573 			break;
    574 
    575 		case 'b':
    576 			bflag = true;
    577 			break;
    578 
    579 		case 'C':
    580 			Cflag = true;
    581 			break;
    582 
    583 		case 'd':
    584 			dflag = true;
    585 			break;
    586 		case 'h':
    587 			hflag = true;
    588 			break;
    589 		case 'l':
    590 			lflag = true;
    591 			break;
    592 		case 'N':
    593 			Nflag = true;
    594 			break;
    595 
    596 		case 's':
    597 			sflag = true;
    598 			break;
    599 
    600 		case 'u':
    601 			uflag = true;
    602 			break;
    603 
    604 		case 'v':
    605 			vflag = true;
    606 			break;
    607 
    608 		case 'z':
    609 			zflag = true;
    610 			break;
    611 
    612 		default:
    613 			if (!set_flag(ch))
    614 				usage();
    615 			break;
    616 		}
    617 		switch (ch) {
    618 		case 'a':
    619 			start = &opt_family_only.pb_parser;
    620 			break;
    621 
    622 		case 'L':
    623 		case 'm':
    624 		case 'v':
    625 		case 'z':
    626 			if (start != &opt_family_only.pb_parser)
    627 				start = &iface_opt_family_only.pif_parser;
    628 			break;
    629 		case 'C':
    630 			start = &cloneterm.pt_parser;
    631 			break;
    632 		case 'l':
    633 			start = &no_cmds.pt_parser;
    634 			break;
    635 		case 's':
    636 			if (start != &no_cmds.pt_parser &&
    637 			    start != &opt_family_only.pb_parser)
    638 				start = &iface_only.pif_parser;
    639 			break;
    640 		default:
    641 			break;
    642 		}
    643 	}
    644 	argc -= optind;
    645 	argv += optind;
    646 
    647 	/*
    648 	 * -l means "list all interfaces", and is mutally exclusive with
    649 	 * all other flags/commands.
    650 	 *
    651 	 * -C means "list all names of cloners", and it mutually exclusive
    652 	 * with all other flags/commands.
    653 	 *
    654 	 * -a means "print status of all interfaces".
    655 	 */
    656 	if ((lflag || Cflag) && (aflag || get_flag('m') || vflag || zflag))
    657 		usage();
    658 	if ((lflag || Cflag) && get_flag('L'))
    659 		usage();
    660 	if (lflag && Cflag)
    661 		usage();
    662 
    663 	nmatch = __arraycount(match);
    664 
    665 	rc = parse(argc, argv, start, match, &nmatch, &narg);
    666 	if (rc != 0)
    667 		usage();
    668 
    669 	if (prog_init && prog_init() == -1)
    670 		err(1, "rump client init");
    671 
    672 	if ((oenv = prop_dictionary_create()) == NULL)
    673 		err(EXIT_FAILURE, "%s: prop_dictionary_create", __func__);
    674 
    675 	if (matches_exec(match, oenv, nmatch) == -1)
    676 		err(EXIT_FAILURE, "exec_matches");
    677 
    678 	argc -= narg;
    679 	argv += narg;
    680 
    681 	env = (nmatch > 0) ? match[(int)nmatch - 1].m_env : NULL;
    682 	if (env == NULL)
    683 		env = oenv;
    684 	else {
    685 		env = prop_dictionary_augment(env, oenv);
    686 		if (env == NULL)
    687 			err(EXIT_FAILURE, "%s: prop_dictionary_augment",
    688 			    __func__);
    689 	}
    690 
    691 	/* Process any media commands that may have been issued. */
    692 	process_media_commands(env);
    693 
    694 	if ((af = getaf(env)) == -1)
    695 		af = AF_INET;
    696 
    697 	if ((s = getsock(af)) == -1)
    698 		err(EXIT_FAILURE, "%s: getsock", __func__);
    699 
    700 	if ((ifname = getifname(env)) == NULL)
    701 		err(EXIT_FAILURE, "%s: getifname", __func__);
    702 
    703 	if ((afp = lookup_af_bynum(af)) == NULL)
    704 		errx(EXIT_FAILURE, "%s: lookup_af_bynum", __func__);
    705 
    706 	assert(afp->af_addr_commit != NULL);
    707 	(*afp->af_addr_commit)(env, oenv);
    708 
    709 	do_setifpreference(env);
    710 	do_setifcaps(env);
    711 
    712 	exit(EXIT_SUCCESS);
    713 }
    714 
    715 static void
    716 init_afs(void)
    717 {
    718 	size_t i;
    719 	const struct afswtch *afp;
    720 	struct kwinst kw = {.k_type = KW_T_INT};
    721 
    722 	SIMPLEQ_FOREACH(afp, &aflist, af_next) {
    723 		kw.k_word = afp->af_name;
    724 		kw.k_int = afp->af_af;
    725 		for (i = 0; i < __arraycount(familykw); i++) {
    726 			if (familykw[i].k_word == NULL) {
    727 				familykw[i] = kw;
    728 				break;
    729 			}
    730 		}
    731 	}
    732 }
    733 
    734 const struct afswtch *
    735 lookup_af_bynum(int afnum)
    736 {
    737 	const struct afswtch *afp;
    738 
    739 	SIMPLEQ_FOREACH(afp, &aflist, af_next) {
    740 		if (afp->af_af == afnum)
    741 			break;
    742 	}
    743 	return afp;
    744 }
    745 
    746 void
    747 printall(const char *ifname, prop_dictionary_t env0)
    748 {
    749 	struct ifaddrs *ifap, *ifa;
    750 	struct ifreq ifr;
    751 	const struct sockaddr *sdl = NULL;
    752 	prop_dictionary_t env, oenv;
    753 	int idx;
    754 	char *p;
    755 
    756 	if (env0 == NULL)
    757 		env = prop_dictionary_create();
    758 	else
    759 		env = prop_dictionary_copy_mutable(env0);
    760 
    761 	oenv = prop_dictionary_create();
    762 
    763 	if (env == NULL || oenv == NULL)
    764 		errx(EXIT_FAILURE, "%s: prop_dictionary_copy/create", __func__);
    765 
    766 	if (getifaddrs(&ifap) != 0)
    767 		err(EXIT_FAILURE, "getifaddrs");
    768 	p = NULL;
    769 	idx = 0;
    770 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    771 		memset(&ifr, 0, sizeof(ifr));
    772 		estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
    773 		if (sizeof(ifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
    774 			memcpy(&ifr.ifr_addr, ifa->ifa_addr,
    775 			    ifa->ifa_addr->sa_len);
    776 		}
    777 
    778 		if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
    779 			continue;
    780 		if (ifa->ifa_addr->sa_family == AF_LINK)
    781 			sdl = ifa->ifa_addr;
    782 		if (p && strcmp(p, ifa->ifa_name) == 0)
    783 			continue;
    784 		if (!prop_dictionary_set_cstring(env, "if", ifa->ifa_name))
    785 			continue;
    786 		p = ifa->ifa_name;
    787 
    788 		if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
    789 			continue;
    790 		if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
    791 			continue;
    792 		if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
    793 			continue;
    794 
    795 		if (sflag && carrier(env))
    796 			continue;
    797 		idx++;
    798 		/*
    799 		 * Are we just listing the interfaces?
    800 		 */
    801 		if (lflag) {
    802 			if (idx > 1)
    803 				printf(" ");
    804 			fputs(ifa->ifa_name, stdout);
    805 			continue;
    806 		}
    807 
    808 		status(sdl, env, oenv);
    809 		sdl = NULL;
    810 	}
    811 	if (lflag)
    812 		printf("\n");
    813 	prop_object_release((prop_object_t)env);
    814 	prop_object_release((prop_object_t)oenv);
    815 	freeifaddrs(ifap);
    816 }
    817 
    818 static int
    819 list_cloners(prop_dictionary_t env, prop_dictionary_t oenv)
    820 {
    821 	struct if_clonereq ifcr;
    822 	char *cp, *buf;
    823 	int idx, s;
    824 
    825 	memset(&ifcr, 0, sizeof(ifcr));
    826 
    827 	s = getsock(AF_INET);
    828 
    829 	if (prog_ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
    830 		err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
    831 
    832 	buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
    833 	if (buf == NULL)
    834 		err(EXIT_FAILURE, "unable to allocate cloner name buffer");
    835 
    836 	ifcr.ifcr_count = ifcr.ifcr_total;
    837 	ifcr.ifcr_buffer = buf;
    838 
    839 	if (prog_ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
    840 		err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
    841 
    842 	/*
    843 	 * In case some disappeared in the mean time, clamp it down.
    844 	 */
    845 	if (ifcr.ifcr_count > ifcr.ifcr_total)
    846 		ifcr.ifcr_count = ifcr.ifcr_total;
    847 
    848 	for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
    849 		if (idx > 0)
    850 			printf(" ");
    851 		printf("%s", cp);
    852 	}
    853 
    854 	printf("\n");
    855 	free(buf);
    856 	exit(EXIT_SUCCESS);
    857 }
    858 
    859 static int
    860 clone_command(prop_dictionary_t env, prop_dictionary_t oenv)
    861 {
    862 	int64_t cmd;
    863 
    864 	if (!prop_dictionary_get_int64(env, "clonecmd", &cmd)) {
    865 		errno = ENOENT;
    866 		return -1;
    867 	}
    868 
    869 	if (indirect_ioctl(env, (unsigned long)cmd, NULL) == -1) {
    870 		warn("%s", __func__);
    871 		return -1;
    872 	}
    873 	return 0;
    874 }
    875 
    876 /*ARGSUSED*/
    877 static int
    878 setifaddr(prop_dictionary_t env, prop_dictionary_t oenv)
    879 {
    880 	const struct paddr_prefix *pfx0;
    881 	struct paddr_prefix *pfx;
    882 	prop_data_t d;
    883 	int af;
    884 
    885 	if ((af = getaf(env)) == -1)
    886 		af = AF_INET;
    887 
    888 	d = (prop_data_t)prop_dictionary_get(env, "address");
    889 	assert(d != NULL);
    890 	pfx0 = prop_data_data_nocopy(d);
    891 
    892 	if (pfx0->pfx_len >= 0) {
    893 		pfx = prefixlen_to_mask(af, pfx0->pfx_len);
    894 		if (pfx == NULL)
    895 			err(EXIT_FAILURE, "prefixlen_to_mask");
    896 		free(pfx);
    897 	}
    898 
    899 	return 0;
    900 }
    901 
    902 static int
    903 setifnetmask(prop_dictionary_t env, prop_dictionary_t oenv)
    904 {
    905 	const struct paddr_prefix *pfx;
    906 	prop_data_t d;
    907 
    908 	d = (prop_data_t)prop_dictionary_get(env, "dstormask");
    909 	assert(d != NULL);
    910 	pfx = prop_data_data_nocopy(d);
    911 
    912 	if (!prop_dictionary_set(oenv, "netmask", (prop_object_t)d))
    913 		return -1;
    914 
    915 	return 0;
    916 }
    917 
    918 static int
    919 setifbroadaddr(prop_dictionary_t env, prop_dictionary_t oenv)
    920 {
    921 	const struct paddr_prefix *pfx;
    922 	prop_data_t d;
    923 	unsigned short flags;
    924 
    925 	if (getifflags(env, oenv, &flags) == -1)
    926 		err(EXIT_FAILURE, "%s: getifflags", __func__);
    927 
    928 	if ((flags & IFF_BROADCAST) == 0)
    929 		errx(EXIT_FAILURE, "not a broadcast interface");
    930 
    931 	d = (prop_data_t)prop_dictionary_get(env, "broadcast");
    932 	assert(d != NULL);
    933 	pfx = prop_data_data_nocopy(d);
    934 
    935 	if (!prop_dictionary_set(oenv, "broadcast", (prop_object_t)d))
    936 		return -1;
    937 
    938 	return 0;
    939 }
    940 
    941 /*ARGSUSED*/
    942 static int
    943 notrailers(prop_dictionary_t env, prop_dictionary_t oenv)
    944 {
    945 	puts("Note: trailers are no longer sent, but always received");
    946 	return 0;
    947 }
    948 
    949 /*ARGSUSED*/
    950 static int
    951 setifdstormask(prop_dictionary_t env, prop_dictionary_t oenv)
    952 {
    953 	const char *key;
    954 	const struct paddr_prefix *pfx;
    955 	prop_data_t d;
    956 	unsigned short flags;
    957 
    958 	if (getifflags(env, oenv, &flags) == -1)
    959 		err(EXIT_FAILURE, "%s: getifflags", __func__);
    960 
    961 	d = (prop_data_t)prop_dictionary_get(env, "dstormask");
    962 	assert(d != NULL);
    963 	pfx = prop_data_data_nocopy(d);
    964 
    965 	if ((flags & IFF_BROADCAST) == 0) {
    966 		key = "dst";
    967 	} else {
    968 		key = "netmask";
    969 	}
    970 
    971 	if (!prop_dictionary_set(oenv, key, (prop_object_t)d))
    972 		return -1;
    973 
    974 	return 0;
    975 }
    976 
    977 static int
    978 setifflags(prop_dictionary_t env, prop_dictionary_t oenv)
    979 {
    980 	struct ifreq ifr;
    981 	int64_t ifflag;
    982 	bool rc;
    983 
    984 	rc = prop_dictionary_get_int64(env, "ifflag", &ifflag);
    985 	assert(rc);
    986 
    987  	if (direct_ioctl(env, SIOCGIFFLAGS, &ifr) == -1)
    988 		return -1;
    989 
    990 	if (ifflag < 0) {
    991 		ifflag = -ifflag;
    992 		ifr.ifr_flags &= ~ifflag;
    993 	} else
    994 		ifr.ifr_flags |= ifflag;
    995 
    996 	if (direct_ioctl(env, SIOCSIFFLAGS, &ifr) == -1)
    997 		return -1;
    998 
    999 	return 0;
   1000 }
   1001 
   1002 static int
   1003 getifcaps(prop_dictionary_t env, prop_dictionary_t oenv, struct ifcapreq *oifcr)
   1004 {
   1005 	bool rc;
   1006 	struct ifcapreq ifcr;
   1007 	const struct ifcapreq *tmpifcr;
   1008 	prop_data_t capdata;
   1009 
   1010 	capdata = (prop_data_t)prop_dictionary_get(env, "ifcaps");
   1011 
   1012 	if (capdata != NULL) {
   1013 		tmpifcr = prop_data_data_nocopy(capdata);
   1014 		*oifcr = *tmpifcr;
   1015 		return 0;
   1016 	}
   1017 
   1018 	(void)direct_ioctl(env, SIOCGIFCAP, &ifcr);
   1019 	*oifcr = ifcr;
   1020 
   1021 	capdata = prop_data_create_data(&ifcr, sizeof(ifcr));
   1022 
   1023 	rc = prop_dictionary_set(oenv, "ifcaps", capdata);
   1024 
   1025 	prop_object_release((prop_object_t)capdata);
   1026 
   1027 	return rc ? 0 : -1;
   1028 }
   1029 
   1030 static int
   1031 setifcaps(prop_dictionary_t env, prop_dictionary_t oenv)
   1032 {
   1033 	int64_t ifcap;
   1034 	int s;
   1035 	bool rc;
   1036 	prop_data_t capdata;
   1037 	struct ifcapreq ifcr;
   1038 
   1039 	s = getsock(AF_INET);
   1040 
   1041 	rc = prop_dictionary_get_int64(env, "ifcap", &ifcap);
   1042 	assert(rc);
   1043 
   1044 	if (getifcaps(env, oenv, &ifcr) == -1)
   1045 		return -1;
   1046 
   1047 	if (ifcap < 0) {
   1048 		ifcap = -ifcap;
   1049 		ifcr.ifcr_capenable &= ~ifcap;
   1050 	} else
   1051 		ifcr.ifcr_capenable |= ifcap;
   1052 
   1053 	if ((capdata = prop_data_create_data(&ifcr, sizeof(ifcr))) == NULL)
   1054 		return -1;
   1055 
   1056 	rc = prop_dictionary_set(oenv, "ifcaps", capdata);
   1057 	prop_object_release((prop_object_t)capdata);
   1058 
   1059 	return rc ? 0 : -1;
   1060 }
   1061 
   1062 static int
   1063 setifmetric(prop_dictionary_t env, prop_dictionary_t oenv)
   1064 {
   1065 	struct ifreq ifr;
   1066 	bool rc;
   1067 	int64_t metric;
   1068 
   1069 	rc = prop_dictionary_get_int64(env, "metric", &metric);
   1070 	assert(rc);
   1071 
   1072 	ifr.ifr_metric = metric;
   1073 	if (direct_ioctl(env, SIOCSIFMETRIC, &ifr) == -1)
   1074 		warn("SIOCSIFMETRIC");
   1075 	return 0;
   1076 }
   1077 
   1078 static void
   1079 do_setifpreference(prop_dictionary_t env)
   1080 {
   1081 	struct if_addrprefreq ifap;
   1082 	prop_data_t d;
   1083 	const struct paddr_prefix *pfx;
   1084 
   1085 	memset(&ifap, 0, sizeof(ifap));
   1086 
   1087 	if (!prop_dictionary_get_int16(env, "preference",
   1088 	    &ifap.ifap_preference))
   1089 		return;
   1090 
   1091 	d = (prop_data_t)prop_dictionary_get(env, "address");
   1092 	assert(d != NULL);
   1093 
   1094 	pfx = prop_data_data_nocopy(d);
   1095 
   1096 	memcpy(&ifap.ifap_addr, &pfx->pfx_addr,
   1097 	    MIN(sizeof(ifap.ifap_addr), pfx->pfx_addr.sa_len));
   1098 	if (direct_ioctl(env, SIOCSIFADDRPREF, &ifap) == -1)
   1099 		warn("SIOCSIFADDRPREF");
   1100 }
   1101 
   1102 static int
   1103 setifmtu(prop_dictionary_t env, prop_dictionary_t oenv)
   1104 {
   1105 	int64_t mtu;
   1106 	bool rc;
   1107 	struct ifreq ifr;
   1108 
   1109 	rc = prop_dictionary_get_int64(env, "mtu", &mtu);
   1110 	assert(rc);
   1111 
   1112 	ifr.ifr_mtu = mtu;
   1113 	if (direct_ioctl(env, SIOCSIFMTU, &ifr) == -1)
   1114 		warn("SIOCSIFMTU");
   1115 
   1116 	return 0;
   1117 }
   1118 
   1119 static int
   1120 carrier(prop_dictionary_t env)
   1121 {
   1122 	struct ifmediareq ifmr;
   1123 
   1124 	memset(&ifmr, 0, sizeof(ifmr));
   1125 
   1126 	if (direct_ioctl(env, SIOCGIFMEDIA, &ifmr) == -1) {
   1127 		/*
   1128 		 * Interface doesn't support SIOC{G,S}IFMEDIA;
   1129 		 * assume ok.
   1130 		 */
   1131 		return EXIT_SUCCESS;
   1132 	}
   1133 	if ((ifmr.ifm_status & IFM_AVALID) == 0) {
   1134 		/*
   1135 		 * Interface doesn't report media-valid status.
   1136 		 * assume ok.
   1137 		 */
   1138 		return EXIT_SUCCESS;
   1139 	}
   1140 	/* otherwise, return ok for active, not-ok if not active. */
   1141 	if (ifmr.ifm_status & IFM_ACTIVE)
   1142 		return EXIT_SUCCESS;
   1143 	else
   1144 		return EXIT_FAILURE;
   1145 }
   1146 
   1147 static void
   1148 print_plural(const char *prefix, uint64_t n, const char *unit)
   1149 {
   1150 	printf("%s%" PRIu64 " %s%s", prefix, n, unit, (n == 1) ? "" : "s");
   1151 }
   1152 
   1153 static void
   1154 print_human_bytes(bool humanize, uint64_t n)
   1155 {
   1156 	char buf[5];
   1157 
   1158 	if (humanize) {
   1159 		(void)humanize_number(buf, sizeof(buf),
   1160 		    (int64_t)n, "", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
   1161 		printf(", %s byte%s", buf, (atof(buf) == 1.0) ? "" : "s");
   1162 	} else
   1163 		print_plural(", ", n, "byte");
   1164 }
   1165 
   1166 /*
   1167  * Print the status of the interface.  If an address family was
   1168  * specified, show it and it only; otherwise, show them all.
   1169  */
   1170 
   1171 #define MAX_PRINT_LEN 58	/* XXX need a better way to determine this! */
   1172 
   1173 void
   1174 status(const struct sockaddr *sdl, prop_dictionary_t env,
   1175     prop_dictionary_t oenv)
   1176 {
   1177 	const struct if_data *ifi;
   1178 	status_func_t *status_f;
   1179 	statistics_func_t *statistics_f;
   1180 	struct ifdatareq ifdr;
   1181 	struct ifreq ifr;
   1182 	struct ifdrv ifdrv;
   1183 	char fbuf[BUFSIZ];
   1184 	char *bp;
   1185 	int af, s;
   1186 	const char *ifname;
   1187 	struct ifcapreq ifcr;
   1188 	unsigned short flags;
   1189 	const struct afswtch *afp;
   1190 
   1191 	if ((af = getaf(env)) == -1) {
   1192 		afp = NULL;
   1193 		af = AF_UNSPEC;
   1194 	} else
   1195 		afp = lookup_af_bynum(af);
   1196 
   1197 	/* get out early if the family is unsupported by the kernel */
   1198 	if ((s = getsock(af)) == -1)
   1199 		err(EXIT_FAILURE, "%s: getsock", __func__);
   1200 
   1201 	if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
   1202 		err(EXIT_FAILURE, "%s: getifinfo", __func__);
   1203 
   1204 	(void)snprintb_m(fbuf, sizeof(fbuf), IFFBITS, flags, MAX_PRINT_LEN);
   1205 	bp = fbuf;
   1206 	while (*bp != '\0') {
   1207 		printf("%s: flags=%s", ifname, &bp[2]);
   1208 		bp += strlen(bp) + 1;
   1209 	}
   1210 
   1211 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
   1212 	if (prog_ioctl(s, SIOCGIFMETRIC, &ifr) == -1)
   1213 		warn("SIOCGIFMETRIC %s", ifr.ifr_name);
   1214 	else if (ifr.ifr_metric != 0)
   1215 		printf(" metric %d", ifr.ifr_metric);
   1216 
   1217 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
   1218 	if (prog_ioctl(s, SIOCGIFMTU, &ifr) != -1 && ifr.ifr_mtu != 0)
   1219 		printf(" mtu %d", ifr.ifr_mtu);
   1220 	printf("\n");
   1221 
   1222 	if (getifcaps(env, oenv, &ifcr) == -1)
   1223 		err(EXIT_FAILURE, "%s: getifcaps", __func__);
   1224 
   1225 	if (ifcr.ifcr_capabilities != 0) {
   1226 		(void)snprintb_m(fbuf, sizeof(fbuf), IFCAPBITS,
   1227 		    ifcr.ifcr_capabilities, MAX_PRINT_LEN);
   1228 		bp = fbuf;
   1229 		while (*bp != '\0') {
   1230 			printf("\tcapabilities=%s\n", &bp[2]);
   1231 			bp += strlen(bp) + 1;
   1232 		}
   1233 		(void)snprintb_m(fbuf, sizeof(fbuf), IFCAPBITS,
   1234 		    ifcr.ifcr_capenable, MAX_PRINT_LEN);
   1235 		bp = fbuf;
   1236 		while (*bp != '\0') {
   1237 			printf("\tenabled=%s\n", &bp[2]);
   1238 			bp += strlen(bp) + 1;
   1239 		}
   1240 	}
   1241 
   1242 	SIMPLEQ_FOREACH(status_f, &status_funcs, f_next)
   1243 		(*status_f->f_func)(env, oenv);
   1244 
   1245 	print_link_addresses(env, true);
   1246 
   1247 	estrlcpy(ifdrv.ifd_name, ifname, sizeof(ifdrv.ifd_name));
   1248 	ifdrv.ifd_cmd = IFLINKSTR_QUERYLEN;
   1249 	ifdrv.ifd_len = 0;
   1250 	ifdrv.ifd_data = NULL;
   1251 	/* interface supports linkstr? */
   1252 	if (prog_ioctl(s, SIOCGLINKSTR, &ifdrv) != -1) {
   1253 		char *p;
   1254 
   1255 		p = malloc(ifdrv.ifd_len);
   1256 		if (p == NULL)
   1257 			err(EXIT_FAILURE, "malloc linkstr buf failed");
   1258 		ifdrv.ifd_data = p;
   1259 		ifdrv.ifd_cmd = 0;
   1260 		if (prog_ioctl(s, SIOCGLINKSTR, &ifdrv) == -1)
   1261 			err(EXIT_FAILURE, "failed to query linkstr");
   1262 		printf("\tlinkstr: %s\n", (char *)ifdrv.ifd_data);
   1263 		free(p);
   1264 	}
   1265 
   1266 	media_status(env, oenv);
   1267 
   1268 	if (!vflag && !zflag)
   1269 		goto proto_status;
   1270 
   1271 	estrlcpy(ifdr.ifdr_name, ifname, sizeof(ifdr.ifdr_name));
   1272 
   1273 	if (prog_ioctl(s, zflag ? SIOCZIFDATA : SIOCGIFDATA, &ifdr) == -1)
   1274 		err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
   1275 
   1276 	ifi = &ifdr.ifdr_data;
   1277 
   1278 	print_plural("\tinput: ", ifi->ifi_ipackets, "packet");
   1279 	print_human_bytes(hflag, ifi->ifi_ibytes);
   1280 	if (ifi->ifi_imcasts)
   1281 		print_plural(", ", ifi->ifi_imcasts, "multicast");
   1282 	if (ifi->ifi_ierrors)
   1283 		print_plural(", ", ifi->ifi_ierrors, "error");
   1284 	if (ifi->ifi_iqdrops)
   1285 		print_plural(", ", ifi->ifi_iqdrops, "queue drop");
   1286 	if (ifi->ifi_noproto)
   1287 		printf(", %" PRIu64 " unknown protocol", ifi->ifi_noproto);
   1288 	print_plural("\n\toutput: ", ifi->ifi_opackets, "packet");
   1289 	print_human_bytes(hflag, ifi->ifi_obytes);
   1290 	if (ifi->ifi_omcasts)
   1291 		print_plural(", ", ifi->ifi_omcasts, "multicast");
   1292 	if (ifi->ifi_oerrors)
   1293 		print_plural(", ", ifi->ifi_oerrors, "error");
   1294 	if (ifi->ifi_collisions)
   1295 		print_plural(", ", ifi->ifi_collisions, "collision");
   1296 	printf("\n");
   1297 
   1298 	SIMPLEQ_FOREACH(statistics_f, &statistics_funcs, f_next)
   1299 		(*statistics_f->f_func)(env);
   1300 
   1301  proto_status:
   1302 
   1303 	if (afp != NULL)
   1304 		(*afp->af_status)(env, oenv, true);
   1305 	else SIMPLEQ_FOREACH(afp, &aflist, af_next)
   1306 		(*afp->af_status)(env, oenv, false);
   1307 }
   1308 
   1309 static int
   1310 setifprefixlen(prop_dictionary_t env, prop_dictionary_t oenv)
   1311 {
   1312 	bool rc;
   1313 	int64_t plen;
   1314 	int af;
   1315 	struct paddr_prefix *pfx;
   1316 	prop_data_t d;
   1317 
   1318 	if ((af = getaf(env)) == -1)
   1319 		af = AF_INET;
   1320 
   1321 	rc = prop_dictionary_get_int64(env, "prefixlen", &plen);
   1322 	assert(rc);
   1323 
   1324 	pfx = prefixlen_to_mask(af, plen);
   1325 	if (pfx == NULL)
   1326 		err(EXIT_FAILURE, "prefixlen_to_mask");
   1327 
   1328 	d = prop_data_create_data(pfx, paddr_prefix_size(pfx));
   1329 	if (d == NULL)
   1330 		err(EXIT_FAILURE, "%s: prop_data_create_data", __func__);
   1331 
   1332 	if (!prop_dictionary_set(oenv, "netmask", (prop_object_t)d))
   1333 		err(EXIT_FAILURE, "%s: prop_dictionary_set", __func__);
   1334 
   1335 	free(pfx);
   1336 	return 0;
   1337 }
   1338 
   1339 static int
   1340 setlinkstr(prop_dictionary_t env, prop_dictionary_t oenv)
   1341 {
   1342 	struct ifdrv ifdrv;
   1343 	size_t linkstrlen;
   1344 	prop_data_t data;
   1345 	char *linkstr;
   1346 
   1347 	data = (prop_data_t)prop_dictionary_get(env, "linkstr");
   1348 	if (data == NULL) {
   1349 		errno = ENOENT;
   1350 		return -1;
   1351 	}
   1352 	linkstrlen = prop_data_size(data)+1;
   1353 
   1354 	linkstr = malloc(linkstrlen);
   1355 	if (linkstr == NULL)
   1356 		err(EXIT_FAILURE, "malloc linkstr space");
   1357 	if (getargstr(env, "linkstr", linkstr, linkstrlen) == -1)
   1358 		errx(EXIT_FAILURE, "getargstr linkstr failed");
   1359 
   1360 	ifdrv.ifd_cmd = 0;
   1361 	ifdrv.ifd_len = linkstrlen;
   1362 	ifdrv.ifd_data = __UNCONST(linkstr);
   1363 
   1364 	if (direct_ioctl(env, SIOCSLINKSTR, &ifdrv) == -1)
   1365 		err(EXIT_FAILURE, "SIOCSLINKSTR");
   1366 	free(linkstr);
   1367 
   1368 	return 0;
   1369 }
   1370 
   1371 static int
   1372 unsetlinkstr(prop_dictionary_t env, prop_dictionary_t oenv)
   1373 {
   1374 	struct ifdrv ifdrv;
   1375 
   1376 	memset(&ifdrv, 0, sizeof(ifdrv));
   1377 	ifdrv.ifd_cmd = IFLINKSTR_UNSET;
   1378 
   1379 	if (direct_ioctl(env, SIOCSLINKSTR, &ifdrv) == -1)
   1380 		err(EXIT_FAILURE, "SIOCSLINKSTR");
   1381 
   1382 	return 0;
   1383 }
   1384 
   1385 static void
   1386 usage(void)
   1387 {
   1388 	const char *progname = getprogname();
   1389 	usage_func_t *usage_f;
   1390 	prop_dictionary_t env;
   1391 
   1392 	if ((env = prop_dictionary_create()) == NULL)
   1393 		err(EXIT_FAILURE, "%s: prop_dictionary_create", __func__);
   1394 
   1395 	fprintf(stderr, "usage: %s [-h] %s[-v] [-z] %sinterface\n"
   1396 		"\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
   1397 		"\t\t[ alias | -alias ] ]\n"
   1398 		"\t[ up ] [ down ] [ metric n ] [ mtu n ]\n", progname,
   1399 		flag_is_registered(gflags, 'm') ? "[-m] " : "",
   1400 		flag_is_registered(gflags, 'L') ? "[-L] " : "");
   1401 
   1402 	SIMPLEQ_FOREACH(usage_f, &usage_funcs, f_next)
   1403 		(*usage_f->f_func)(env);
   1404 
   1405 	fprintf(stderr,
   1406 		"\t[ arp | -arp ]\n"
   1407 		"\t[ preference n ]\n"
   1408 		"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
   1409 		"       %s -a [-b] [-d] [-h] %s[-u] [-v] [-z] [ af ]\n"
   1410 		"       %s -l [-b] [-d] [-s] [-u]\n"
   1411 		"       %s -C\n"
   1412 		"       %s interface create\n"
   1413 		"       %s interface destroy\n",
   1414 		progname, flag_is_registered(gflags, 'm') ? "[-m] " : "",
   1415 		progname, progname, progname, progname);
   1416 
   1417 	prop_object_release((prop_object_t)env);
   1418 	exit(EXIT_FAILURE);
   1419 }
   1420