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