Home | History | Annotate | Line # | Download | only in ifconfig
ifconfig.c revision 1.203
      1 /*	$NetBSD: ifconfig.c,v 1.203 2008/05/11 22:07:23 dyoung 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\n\
     65 	The Regents of the University of California.  All rights reserved.\n");
     66 #endif /* not lint */
     67 
     68 #ifndef lint
     69 #if 0
     70 static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
     71 #else
     72 __RCSID("$NetBSD: ifconfig.c,v 1.203 2008/05/11 22:07:23 dyoung Exp $");
     73 #endif
     74 #endif /* not lint */
     75 
     76 #include <sys/param.h>
     77 #include <sys/socket.h>
     78 #include <sys/ioctl.h>
     79 
     80 #include <net/if.h>
     81 #include <net/if_dl.h>
     82 #include <net/if_media.h>
     83 #include <net/if_ether.h>
     84 #include <netinet/in.h>		/* XXX */
     85 #include <netinet/in_var.h>	/* XXX */
     86 
     87 #include <netdb.h>
     88 
     89 #include <sys/protosw.h>
     90 
     91 #include <assert.h>
     92 #include <ctype.h>
     93 #include <err.h>
     94 #include <errno.h>
     95 #include <stdbool.h>
     96 #include <stddef.h>
     97 #include <stdio.h>
     98 #include <stdlib.h>
     99 #include <string.h>
    100 #include <unistd.h>
    101 #include <ifaddrs.h>
    102 #include <util.h>
    103 
    104 #include "extern.h"
    105 
    106 #ifndef INET_ONLY
    107 #include "af_atalk.h"
    108 #include "af_iso.h"
    109 #endif /* ! INET_ONLY */
    110 #include "af_inet.h"
    111 #ifdef INET6
    112 #include "af_inet6.h"
    113 #endif /* INET6 */
    114 
    115 #include "agr.h"
    116 #include "carp.h"
    117 #include "ieee80211.h"
    118 #include "tunnel.h"
    119 #include "vlan.h"
    120 #include "parse.h"
    121 #include "env.h"
    122 
    123 int	setaddr, doalias;
    124 int	clearaddr;
    125 int	newaddr = -1;
    126 int	check_up_state = -1;
    127 int	bflag, dflag, hflag, lflag, mflag, sflag, uflag, vflag, zflag;
    128 #ifdef INET6
    129 int	Lflag;
    130 #endif
    131 
    132 /*
    133  * Media stuff.  Whenever a media command is first performed, the
    134  * currently select media is grabbed for this interface.  If `media'
    135  * is given, the current media word is modifed.  `mediaopt' commands
    136  * only modify the set and clear words.  They then operate on the
    137  * current media word later.
    138  */
    139 int	media_current;
    140 int	mediaopt_set;
    141 int	mediaopt_clear;
    142 
    143 void	check_ifflags_up(prop_dictionary_t);
    144 
    145 int 	notealias(prop_dictionary_t, prop_dictionary_t);
    146 int 	notrailers(prop_dictionary_t, prop_dictionary_t);
    147 int 	setifaddr(prop_dictionary_t, prop_dictionary_t);
    148 int 	setifdstormask(prop_dictionary_t, prop_dictionary_t);
    149 int 	setifflags(prop_dictionary_t, prop_dictionary_t);
    150 static int	setifcaps(prop_dictionary_t, prop_dictionary_t);
    151 int 	setifbroadaddr(prop_dictionary_t, prop_dictionary_t);
    152 static int 	setifmetric(prop_dictionary_t, prop_dictionary_t);
    153 static int 	setifmtu(prop_dictionary_t, prop_dictionary_t);
    154 int 	setifnetmask(prop_dictionary_t, prop_dictionary_t);
    155 int	setifprefixlen(prop_dictionary_t, prop_dictionary_t);
    156 int	setmedia(prop_dictionary_t, prop_dictionary_t);
    157 int	setmediamode(prop_dictionary_t, prop_dictionary_t);
    158 int	setmediaopt(prop_dictionary_t, prop_dictionary_t);
    159 int	unsetmediaopt(prop_dictionary_t, prop_dictionary_t);
    160 int	setmediainst(prop_dictionary_t, prop_dictionary_t);
    161 static int	clone_command(prop_dictionary_t, prop_dictionary_t);
    162 static void	do_setifpreference(prop_dictionary_t);
    163 
    164 static int no_cmds_exec(prop_dictionary_t, prop_dictionary_t);
    165 static int media_status_exec(prop_dictionary_t, prop_dictionary_t);
    166 
    167 int	carrier(prop_dictionary_t);
    168 void	printall(const char *, prop_dictionary_t);
    169 int	list_cloners(prop_dictionary_t, prop_dictionary_t);
    170 void 	status(const struct sockaddr_dl *, prop_dictionary_t,
    171     prop_dictionary_t);
    172 void 	usage(void);
    173 
    174 void	print_media_word(int, const char *);
    175 void	process_media_commands(prop_dictionary_t);
    176 void	init_current_media(prop_dictionary_t, prop_dictionary_t);
    177 
    178 /* Known address families */
    179 static const struct afswtch afs[] = {
    180 	  {.af_name = "inet", .af_af = AF_INET, .af_status = in_status,
    181 	   .af_addr_commit = in_commit_address}
    182 
    183 #ifdef INET6
    184 	, {.af_name = "inet6", .af_af = AF_INET6, .af_status = in6_status,
    185 	   .af_addr_commit = in6_commit_address
    186 }
    187 #endif
    188 
    189 #ifndef INET_ONLY	/* small version, for boot media */
    190 	, {.af_name = "atalk", .af_af = AF_APPLETALK, .af_status = at_status,
    191 	   .af_getaddr = at_getaddr, NULL, .af_difaddr = SIOCDIFADDR,
    192 	   .af_aifaddr = SIOCAIFADDR, .af_gifaddr = SIOCGIFADDR,
    193 	   .af_ridreq = &at_addreq, .af_addreq = &at_addreq}
    194 	, {.af_name = "iso", .af_af = AF_ISO, .af_status = iso_status,
    195 	   .af_getaddr = iso_getaddr, NULL, .af_difaddr = SIOCDIFADDR_ISO,
    196 	   .af_aifaddr = SIOCAIFADDR_ISO, .af_gifaddr = SIOCGIFADDR_ISO,
    197 	   .af_ridreq = &iso_ridreq, .af_addreq = &iso_addreq}
    198 #endif	/* INET_ONLY */
    199 
    200 	, {.af_name = NULL}	/* sentinel */
    201 };
    202 
    203 static const struct kwinst ifflagskw[] = {
    204 	  IFKW("arp", IFF_NOARP)
    205 	, IFKW("debug", IFF_DEBUG)
    206 	, IFKW("link0", IFF_LINK0)
    207 	, IFKW("link1", IFF_LINK1)
    208 	, IFKW("link2", IFF_LINK2)
    209 	, {.k_word = "down", .k_type = KW_T_NUM, .k_num = -IFF_UP}
    210 	, {.k_word = "up", .k_type = KW_T_NUM, .k_num = IFF_UP}
    211 };
    212 
    213 static const struct kwinst ifcapskw[] = {
    214 	  IFKW("ip4csum-tx",	IFCAP_CSUM_IPv4_Tx)
    215 	, IFKW("ip4csum-rx",	IFCAP_CSUM_IPv4_Rx)
    216 	, IFKW("tcp4csum-tx",	IFCAP_CSUM_TCPv4_Tx)
    217 	, IFKW("tcp4csum-rx",	IFCAP_CSUM_TCPv4_Rx)
    218 	, IFKW("udp4csum-tx",	IFCAP_CSUM_UDPv4_Tx)
    219 	, IFKW("udp4csum-rx",	IFCAP_CSUM_UDPv4_Rx)
    220 	, IFKW("tcp6csum-tx",	IFCAP_CSUM_TCPv6_Tx)
    221 	, IFKW("tcp6csum-rx",	IFCAP_CSUM_TCPv6_Rx)
    222 	, IFKW("udp6csum-tx",	IFCAP_CSUM_UDPv6_Tx)
    223 	, IFKW("udp6csum-rx",	IFCAP_CSUM_UDPv6_Rx)
    224 	, IFKW("ip4csum",	IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx)
    225 	, IFKW("tcp4csum",	IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx)
    226 	, IFKW("udp4csum",	IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx)
    227 	, IFKW("tcp6csum",	IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx)
    228 	, IFKW("udp6csum",	IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx)
    229 	, IFKW("tso4",		IFCAP_TSOv4)
    230 	, IFKW("tso6",		IFCAP_TSOv6)
    231 };
    232 
    233 extern struct pbranch command_root;
    234 extern struct pbranch opt_command;
    235 extern struct pbranch opt_family, opt_silent_family;
    236 extern struct pkw cloning, silent_family, family, ifcaps, ifflags, misc;
    237 
    238 struct pinteger parse_metric = PINTEGER_INITIALIZER(&parse_metric, "metric", 10,
    239     setifmetric, "metric", &command_root.pb_parser);
    240 
    241 struct pinteger parse_mtu = PINTEGER_INITIALIZER(&parse_mtu, "mtu", 10,
    242     setifmtu, "mtu", &command_root.pb_parser);
    243 
    244 struct pinteger parse_prefixlen = PINTEGER_INITIALIZER(&parse_prefixlen,
    245     "prefixlen", 10, setifprefixlen, "prefixlen", &command_root.pb_parser);
    246 
    247 struct pinteger parse_preference = PINTEGER_INITIALIZER1(&parse_preference,
    248     "preference", INT16_MIN, INT16_MAX, 10, NULL, "preference",
    249     &command_root.pb_parser);
    250 
    251 struct paddr parse_netmask = PADDR_INITIALIZER(&parse_netmask, "netmask",
    252     setifnetmask, "dstormask", NULL, NULL, NULL, &command_root.pb_parser);
    253 
    254 struct paddr parse_broadcast = PADDR_INITIALIZER(&parse_broadcast,
    255     "broadcast address",
    256     setifbroadaddr, "broadcast", NULL, NULL, NULL, &command_root.pb_parser);
    257 
    258 struct pstr mediamode = PSTR_INITIALIZER(&mediamode, "mediamode",
    259     setmediamode, "mediamode", &command_root.pb_parser);
    260 
    261 struct pinteger mediainst = PINTEGER_INITIALIZER1(&mediainst, "mediainst",
    262     0, IFM_INST_MAX, 10, setmediainst, "mediainst", &command_root.pb_parser);
    263 
    264 struct pstr unmediaopt = PSTR_INITIALIZER(&unmediaopt, "-mediaopt",
    265     unsetmediaopt, "unmediaopt", &command_root.pb_parser);
    266 
    267 struct pstr mediaopt = PSTR_INITIALIZER(&mediaopt, "mediaopt",
    268     setmediaopt, "mediaopt", &command_root.pb_parser);
    269 
    270 struct pstr media = PSTR_INITIALIZER(&media, "media",
    271     setmedia, "media", &command_root.pb_parser);
    272 
    273 static const struct kwinst misckw[] = {
    274 	  {.k_word = "alias", .k_key = "alias", .k_deact = "alias",
    275 	   .k_type = KW_T_BOOL, .k_neg = true,
    276 	   .k_bool = true, .k_negbool = false,
    277 	   .k_exec = notealias, .k_nextparser = &command_root.pb_parser}
    278 	, {.k_word = "broadcast", .k_nextparser = &parse_broadcast.pa_parser}
    279 	, {.k_word = "delete", .k_key = "alias", .k_deact = "alias",
    280 	   .k_type = KW_T_BOOL, .k_bool = false, .k_exec = notealias,
    281 	   .k_nextparser = &command_root.pb_parser}
    282 	, {.k_word = "instance", .k_key = "anymedia", .k_type = KW_T_BOOL,
    283 	   .k_bool = true, .k_act = "media", .k_deact = "mediainst",
    284 	   .k_nextparser = &mediainst.pi_parser}
    285 	, {.k_word = "inst", .k_key = "anymedia", .k_type = KW_T_BOOL,
    286 	   .k_bool = true, .k_act = "media", .k_deact = "mediainst",
    287 	   .k_nextparser = &mediainst.pi_parser}
    288 	, {.k_word = "media", .k_key = "anymedia", .k_type = KW_T_BOOL,
    289 	   .k_bool = true, .k_deact = "media", .k_altdeact = "anymedia",
    290 	   .k_nextparser = &media.ps_parser}
    291 	, {.k_word = "mediaopt", .k_key = "anymedia", .k_type = KW_T_BOOL,
    292 	   .k_bool = true, .k_deact = "mediaopt", .k_altdeact = "instance",
    293 	   .k_nextparser = &mediaopt.ps_parser}
    294 	, {.k_word = "-mediaopt", .k_key = "anymedia", .k_type = KW_T_BOOL,
    295 	   .k_bool = true, .k_deact = "unmediaopt", .k_altdeact = "media",
    296 	   .k_nextparser = &unmediaopt.ps_parser}
    297 	, {.k_word = "mode", .k_key = "anymedia", .k_type = KW_T_BOOL,
    298 	   .k_bool = true, .k_deact = "mode",
    299 	   .k_nextparser = &mediamode.ps_parser}
    300 	, {.k_word = "metric", .k_nextparser = &parse_metric.pi_parser}
    301 	, {.k_word = "mtu", .k_nextparser = &parse_mtu.pi_parser}
    302 	, {.k_word = "netmask", .k_nextparser = &parse_netmask.pa_parser}
    303 	, {.k_word = "preference", .k_act = "address",
    304 	   .k_nextparser = &parse_preference.pi_parser}
    305 	, {.k_word = "prefixlen", .k_nextparser = &parse_prefixlen.pi_parser}
    306 	, {.k_word = "trailers", .k_neg = true,
    307 	   .k_exec = notrailers, .k_nextparser = &command_root.pb_parser}
    308 };
    309 
    310 /* key: clonecmd */
    311 static const struct kwinst clonekw[] = {
    312 	{.k_word = "create", .k_type = KW_T_NUM, .k_num = SIOCIFCREATE,
    313 	 .k_nextparser = &opt_silent_family.pb_parser},
    314 	{.k_word = "destroy", .k_type = KW_T_NUM, .k_num = SIOCIFDESTROY}
    315 };
    316 
    317 static const struct kwinst familykw[] = {
    318 	  {.k_word = "inet", .k_type = KW_T_NUM, .k_num = AF_INET,
    319 	   .k_nextparser = NULL}
    320 #ifdef INET6
    321 	, {.k_word = "inet6", .k_type = KW_T_NUM, .k_num = AF_INET6,
    322 	   .k_nextparser = NULL}
    323 #endif
    324 #ifndef INET_ONLY	/* small version, for boot media */
    325 	, {.k_word = "atalk", .k_type = KW_T_NUM, .k_num = AF_APPLETALK,
    326 	   .k_nextparser = NULL}
    327 	, {.k_word = "iso", .k_type = KW_T_NUM, .k_num = AF_ISO,
    328 	   .k_nextparser = NULL}
    329 #endif	/* INET_ONLY */
    330 };
    331 
    332 struct pterm cloneterm = PTERM_INITIALIZER(&cloneterm, "list cloners",
    333     list_cloners, "none");
    334 
    335 struct pterm no_cmds = PTERM_INITIALIZER(&no_cmds, "no commands", no_cmds_exec,
    336     "none");
    337 
    338 struct pkw family_only =
    339     PKW_INITIALIZER(&family_only, "family-only", NULL, "af", familykw,
    340 	__arraycount(familykw), &no_cmds.pt_parser);
    341 
    342 struct paddr address = PADDR_INITIALIZER(&address,
    343     "local address (address 1)",
    344     setifaddr, "address", "netmask", NULL, "address", &command_root.pb_parser);
    345 
    346 struct paddr dstormask = PADDR_INITIALIZER(&dstormask,
    347     "destination/netmask (address 2)",
    348     setifdstormask, "dstormask", NULL, "address", "dstormask",
    349     &command_root.pb_parser);
    350 
    351 struct paddr broadcast = PADDR_INITIALIZER(&broadcast,
    352     "broadcast address (address 3)",
    353     setifbroadaddr, "broadcast", NULL, "dstormask", "broadcast",
    354     &command_root.pb_parser);
    355 
    356 struct branch opt_clone_brs[] = {
    357 	  {.b_nextparser = &cloning.pk_parser}
    358 	, {.b_nextparser = &opt_family.pb_parser}
    359 }, opt_silent_family_brs[] = {
    360 	  {.b_nextparser = &silent_family.pk_parser}
    361 	, {.b_nextparser = &command_root.pb_parser}
    362 }, opt_family_brs[] = {
    363 	  {.b_nextparser = &family.pk_parser}
    364 	, {.b_nextparser = &opt_command.pb_parser}
    365 }, command_root_brs[] = {
    366 	  {.b_nextparser = &ieee80211bool.pk_parser}
    367 	, {.b_nextparser = &ifflags.pk_parser}
    368 	, {.b_nextparser = &ifcaps.pk_parser}
    369 #ifdef INET6
    370 	, {.b_nextparser = &ia6flags.pk_parser}
    371 	, {.b_nextparser = &inet6.pk_parser}
    372 #endif /*INET6*/
    373 	, {.b_nextparser = &misc.pk_parser}
    374 	, {.b_nextparser = &tunnel.pk_parser}
    375 	, {.b_nextparser = &vlan.pk_parser}
    376 	, {.b_nextparser = &agr.pk_parser}
    377 #ifndef INET_ONLY
    378 	, {.b_nextparser = &carp.pk_parser}
    379 	, {.b_nextparser = &atalk.pk_parser}
    380 	, {.b_nextparser = &iso.pk_parser}
    381 #endif
    382 	, {.b_nextparser = &kw80211.pk_parser}
    383 	, {.b_nextparser = &address.pa_parser}
    384 	, {.b_nextparser = &dstormask.pa_parser}
    385 	, {.b_nextparser = &broadcast.pa_parser}
    386 	, {.b_nextparser = NULL}
    387 }, opt_command_brs[] = {
    388 	  {.b_nextparser = &no_cmds.pt_parser}
    389 	, {.b_nextparser = &command_root.pb_parser}
    390 };
    391 
    392 struct branch opt_family_only_brs[] = {
    393 	  {.b_nextparser = &no_cmds.pt_parser}
    394 	, {.b_nextparser = &family_only.pk_parser}
    395 };
    396 struct pbranch opt_family_only = PBRANCH_INITIALIZER(&opt_family_only,
    397     "opt-family-only", opt_family_only_brs,
    398     __arraycount(opt_family_only_brs), true);
    399 struct pbranch opt_command = PBRANCH_INITIALIZER(&opt_command,
    400     "optional command",
    401     opt_command_brs, __arraycount(opt_command_brs), true);
    402 
    403 struct pbranch command_root = PBRANCH_INITIALIZER(&command_root,
    404     "command-root", command_root_brs, __arraycount(command_root_brs), true);
    405 
    406 struct piface iface_opt_family_only =
    407     PIFACE_INITIALIZER(&iface_opt_family_only, "iface-opt-family-only",
    408     NULL, "if", &opt_family_only.pb_parser);
    409 
    410 struct pkw family = PKW_INITIALIZER(&family, "family", NULL, "af",
    411     familykw, __arraycount(familykw), &opt_command.pb_parser);
    412 
    413 struct pkw silent_family = PKW_INITIALIZER(&silent_family, "silent family",
    414     NULL, "af", familykw, __arraycount(familykw), &command_root.pb_parser);
    415 
    416 struct pkw ifcaps = PKW_INITIALIZER(&ifcaps, "ifcaps", setifcaps,
    417     "ifcap", ifcapskw, __arraycount(ifcapskw), &command_root.pb_parser);
    418 
    419 struct pkw ifflags = PKW_INITIALIZER(&ifflags, "ifflags", setifflags,
    420     "ifflag", ifflagskw, __arraycount(ifflagskw), &command_root.pb_parser);
    421 
    422 struct pkw cloning = PKW_INITIALIZER(&cloning, "cloning", clone_command,
    423     "clonecmd", clonekw, __arraycount(clonekw), NULL);
    424 
    425 struct pkw misc = PKW_INITIALIZER(&misc, "misc", NULL, NULL,
    426     misckw, __arraycount(misckw), NULL);
    427 
    428 struct pbranch opt_clone = PBRANCH_INITIALIZER(&opt_clone,
    429     "opt-clone", opt_clone_brs, __arraycount(opt_clone_brs), true);
    430 
    431 struct pbranch opt_silent_family = PBRANCH_INITIALIZER(&opt_silent_family,
    432     "optional silent family", opt_silent_family_brs,
    433     __arraycount(opt_silent_family_brs), true);
    434 
    435 struct pbranch opt_family = PBRANCH_INITIALIZER(&opt_family,
    436     "opt-family", opt_family_brs, __arraycount(opt_family_brs), true);
    437 
    438 struct piface iface_start = PIFACE_INITIALIZER(&iface_start,
    439     "iface-opt-family", NULL, "if", &opt_clone.pb_parser);
    440 
    441 struct piface iface_only = PIFACE_INITIALIZER(&iface_only, "iface",
    442     media_status_exec, "if", NULL);
    443 
    444 static struct parser *
    445 init_parser(void)
    446 {
    447 	if (parser_init(&iface_opt_family_only.pif_parser) == -1)
    448 		err(EXIT_FAILURE, "parser_init(iface_opt_family_only)");
    449 	if (parser_init(&iface_only.pif_parser) == -1)
    450 		err(EXIT_FAILURE, "parser_init(iface_only)");
    451 	if (parser_init(&iface_start.pif_parser) == -1)
    452 		err(EXIT_FAILURE, "parser_init(iface_start)");
    453 
    454 	return &iface_start.pif_parser;
    455 }
    456 
    457 static int
    458 no_cmds_exec(prop_dictionary_t env, prop_dictionary_t xenv)
    459 {
    460 	const char *ifname;
    461 	unsigned short ignore;
    462 
    463 	/* ifname == NULL is ok.  It indicates 'ifconfig -a'. */
    464 	if ((ifname = getifname(env)) == NULL)
    465 		;
    466 	else if (getifflags(env, xenv, &ignore) == -1)
    467 		err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
    468 
    469 	printall(ifname, env);
    470 	exit(EXIT_SUCCESS);
    471 }
    472 
    473 static int
    474 media_status_exec(prop_dictionary_t env, prop_dictionary_t xenv)
    475 {
    476 	const char *ifname;
    477 	unsigned short ignore;
    478 
    479 	/* ifname == NULL is ok.  It indicates 'ifconfig -a'. */
    480 	if ((ifname = getifname(env)) == NULL)
    481 		;
    482 	else if (getifflags(env, xenv, &ignore) == -1)
    483 		err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
    484 
    485 	exit(carrier(env));
    486 }
    487 
    488 static void
    489 do_setifcaps(prop_dictionary_t env)
    490 {
    491 	struct ifcapreq ifcr;
    492 	prop_data_t d;
    493 
    494 	d = (prop_data_t )prop_dictionary_get(env, "ifcaps");
    495 	if (d == NULL)
    496 		return;
    497 
    498 	assert(sizeof(ifcr) == prop_data_size(d));
    499 
    500 	memcpy(&ifcr, prop_data_data_nocopy(d), sizeof(ifcr));
    501 	if (direct_ioctl(env, SIOCSIFCAP, &ifcr) == -1)
    502 		err(EXIT_FAILURE, "SIOCSIFCAP");
    503 }
    504 
    505 int
    506 main(int argc, char **argv)
    507 {
    508 	const struct afswtch *afp;
    509 	int af, aflag = 0, Cflag = 0, s;
    510 	struct match match[32];
    511 	size_t nmatch;
    512 	struct parser *start;
    513 	int ch, narg = 0, rc;
    514 	prop_dictionary_t env, xenv;
    515 	const char *ifname;
    516 
    517 	memset(match, 0, sizeof(match));
    518 
    519 	start = init_parser();
    520 
    521 	/* Parse command-line options */
    522 	aflag = mflag = vflag = zflag = 0;
    523 	while ((ch = getopt(argc, argv, "AabCdhlmsuvz"
    524 #ifdef INET6
    525 					"L"
    526 #endif
    527 			)) != -1) {
    528 		switch (ch) {
    529 		case 'A':
    530 			warnx("-A is deprecated");
    531 			break;
    532 
    533 		case 'a':
    534 			aflag = 1;
    535 			break;
    536 
    537 		case 'b':
    538 			bflag = 1;
    539 			break;
    540 
    541 		case 'C':
    542 			Cflag = 1;
    543 			break;
    544 
    545 		case 'd':
    546 			dflag = 1;
    547 			break;
    548 		case 'h':
    549 			hflag = 1;
    550 			break;
    551 #ifdef INET6
    552 		case 'L':
    553 			Lflag = 1;
    554 			break;
    555 #endif
    556 
    557 		case 'l':
    558 			lflag = 1;
    559 			break;
    560 
    561 		case 'm':
    562 			mflag = 1;
    563 			break;
    564 
    565 		case 's':
    566 			sflag = 1;
    567 			break;
    568 
    569 		case 'u':
    570 			uflag = 1;
    571 			break;
    572 
    573 		case 'v':
    574 			vflag = 1;
    575 			break;
    576 
    577 		case 'z':
    578 			zflag = 1;
    579 			break;
    580 
    581 
    582 		default:
    583 			usage();
    584 			/* NOTREACHED */
    585 		}
    586 		switch (ch) {
    587 		case 'a':
    588 			start = &opt_family_only.pb_parser;
    589 			break;
    590 
    591 #ifdef INET6
    592 		case 'L':
    593 #endif
    594 		case 'm':
    595 		case 'v':
    596 		case 'z':
    597 			if (start != &opt_family_only.pb_parser)
    598 				start = &iface_opt_family_only.pif_parser;
    599 			break;
    600 		case 'C':
    601 			start = &cloneterm.pt_parser;
    602 			break;
    603 		case 'l':
    604 			start = &no_cmds.pt_parser;
    605 			break;
    606 		case 's':
    607 			if (start != &no_cmds.pt_parser &&
    608 			    start != &opt_family_only.pb_parser)
    609 				start = &iface_only.pif_parser;
    610 			break;
    611 		default:
    612 			break;
    613 		}
    614 	}
    615 	argc -= optind;
    616 	argv += optind;
    617 
    618 	/*
    619 	 * -l means "list all interfaces", and is mutally exclusive with
    620 	 * all other flags/commands.
    621 	 *
    622 	 * -C means "list all names of cloners", and it mutually exclusive
    623 	 * with all other flags/commands.
    624 	 *
    625 	 * -a means "print status of all interfaces".
    626 	 */
    627 	if ((lflag || Cflag) && (aflag || mflag || vflag || zflag))
    628 		usage();
    629 #ifdef INET6
    630 	if ((lflag || Cflag) && Lflag)
    631 		usage();
    632 #endif
    633 	if (lflag && Cflag)
    634 		usage();
    635 
    636 	nmatch = __arraycount(match);
    637 
    638 	rc = parse(argc, argv, start, match, &nmatch, &narg);
    639 	if (rc != 0)
    640 		usage();
    641 
    642 	if ((xenv = prop_dictionary_create()) == NULL)
    643 		err(EXIT_FAILURE, "%s: prop_dictionary_create", __func__);
    644 
    645 	if (matches_exec(match, xenv, nmatch) == -1)
    646 		err(EXIT_FAILURE, "exec_matches");
    647 
    648 	argc -= narg;
    649 	argv += narg;
    650 
    651 	env = (nmatch > 0) ? match[(int)nmatch - 1].m_env : NULL;
    652 	if (env == NULL)
    653 		env = xenv;
    654 	else
    655 		env = prop_dictionary_augment(env, xenv);
    656 
    657 	/* Process any media commands that may have been issued. */
    658 	process_media_commands(env);
    659 
    660 	af = getaf(env);
    661 	switch (af) {
    662 #ifndef INET_ONLY
    663 	case AF_APPLETALK:
    664 		checkatrange(&at_addreq.ifra_addr);
    665 		break;
    666 #endif	/* INET_ONLY */
    667 	default:
    668 		break;
    669 	case -1:
    670 		af = AF_INET;
    671 		break;
    672 	}
    673 
    674 	if ((s = getsock(af)) == -1)
    675 		err(EXIT_FAILURE, "%s: getsock", __func__);
    676 
    677 	if ((ifname = getifname(env)) == NULL)
    678 		err(EXIT_FAILURE, "%s: getifname", __func__);
    679 
    680 	if ((afp = lookup_af_bynum(af)) == NULL)
    681 		errx(EXIT_FAILURE, "%s: lookup_af_bynum", __func__);
    682 
    683 	if (afp->af_addr_commit != NULL) {
    684 		(*afp->af_addr_commit)(env, xenv);
    685 	} else {
    686 		if (clearaddr) {
    687 			estrlcpy(afp->af_ridreq, ifname, IFNAMSIZ);
    688 			if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
    689 				err(EXIT_FAILURE, "SIOCDIFADDR");
    690 		}
    691 		if (newaddr > 0) {
    692 			estrlcpy(afp->af_addreq, ifname, IFNAMSIZ);
    693 			if (ioctl(s, afp->af_aifaddr, afp->af_addreq) == -1)
    694 				warn("SIOCAIFADDR");
    695 			else if (check_up_state < 0)
    696 				check_up_state = 1;
    697 		}
    698 	}
    699 
    700 	do_setifpreference(env);
    701 	do_setifcaps(env);
    702 
    703 	if (check_up_state == 1)
    704 		check_ifflags_up(env);
    705 
    706 	exit(EXIT_SUCCESS);
    707 }
    708 
    709 const struct afswtch *
    710 lookup_af_bynum(int afnum)
    711 {
    712 	const struct afswtch *a;
    713 
    714 	for (a = afs; a->af_name != NULL; a++)
    715 		if (a->af_af == afnum)
    716 			return (a);
    717 	return (NULL);
    718 }
    719 
    720 void
    721 printall(const char *ifname, prop_dictionary_t env0)
    722 {
    723 	struct ifaddrs *ifap, *ifa;
    724 	struct ifreq ifr;
    725 	const struct sockaddr_dl *sdl = NULL;
    726 	prop_dictionary_t env, oenv;
    727 	int idx;
    728 	char *p;
    729 
    730 	if (env0 == NULL)
    731 		env = prop_dictionary_create();
    732 	else
    733 		env = prop_dictionary_copy_mutable(env0);
    734 
    735 	oenv = prop_dictionary_create();
    736 
    737 	if (env == NULL || oenv == NULL)
    738 		errx(EXIT_FAILURE, "%s: prop_dictionary_copy/create", __func__);
    739 
    740 	if (getifaddrs(&ifap) != 0)
    741 		err(EXIT_FAILURE, "getifaddrs");
    742 	p = NULL;
    743 	idx = 0;
    744 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    745 		memset(&ifr, 0, sizeof(ifr));
    746 		estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
    747 		if (sizeof(ifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
    748 			memcpy(&ifr.ifr_addr, ifa->ifa_addr,
    749 			    ifa->ifa_addr->sa_len);
    750 		}
    751 
    752 		if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
    753 			continue;
    754 		if (ifa->ifa_addr->sa_family == AF_LINK)
    755 			sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
    756 		if (p && strcmp(p, ifa->ifa_name) == 0)
    757 			continue;
    758 		if (!prop_dictionary_set_cstring(env, "if", ifa->ifa_name))
    759 			continue;
    760 		p = ifa->ifa_name;
    761 
    762 		if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
    763 			continue;
    764 		if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
    765 			continue;
    766 		if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
    767 			continue;
    768 
    769 		if (sflag && carrier(env))
    770 			continue;
    771 		idx++;
    772 		/*
    773 		 * Are we just listing the interfaces?
    774 		 */
    775 		if (lflag) {
    776 			if (idx > 1)
    777 				printf(" ");
    778 			fputs(ifa->ifa_name, stdout);
    779 			continue;
    780 		}
    781 
    782 		status(sdl, env, oenv);
    783 		sdl = NULL;
    784 	}
    785 	if (lflag)
    786 		printf("\n");
    787 	prop_object_release((prop_object_t)env);
    788 	prop_object_release((prop_object_t)oenv);
    789 	freeifaddrs(ifap);
    790 }
    791 
    792 int
    793 list_cloners(prop_dictionary_t env, prop_dictionary_t oenv)
    794 {
    795 	struct if_clonereq ifcr;
    796 	char *cp, *buf;
    797 	int idx, s;
    798 
    799 	memset(&ifcr, 0, sizeof(ifcr));
    800 
    801 	s = getsock(AF_INET);
    802 
    803 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
    804 		err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
    805 
    806 	buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
    807 	if (buf == NULL)
    808 		err(EXIT_FAILURE, "unable to allocate cloner name buffer");
    809 
    810 	ifcr.ifcr_count = ifcr.ifcr_total;
    811 	ifcr.ifcr_buffer = buf;
    812 
    813 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
    814 		err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
    815 
    816 	/*
    817 	 * In case some disappeared in the mean time, clamp it down.
    818 	 */
    819 	if (ifcr.ifcr_count > ifcr.ifcr_total)
    820 		ifcr.ifcr_count = ifcr.ifcr_total;
    821 
    822 	for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
    823 		if (idx > 0)
    824 			printf(" ");
    825 		printf("%s", cp);
    826 	}
    827 
    828 	printf("\n");
    829 	free(buf);
    830 	exit(EXIT_SUCCESS);
    831 }
    832 
    833 static int
    834 clone_command(prop_dictionary_t env, prop_dictionary_t xenv)
    835 {
    836 	int64_t cmd;
    837 
    838 	if (!prop_dictionary_get_int64(env, "clonecmd", &cmd)) {
    839 		errno = ENOENT;
    840 		return -1;
    841 	}
    842 
    843 	if (indirect_ioctl(env, (unsigned long)cmd, NULL) == -1) {
    844 		warn("%s", __func__);
    845 		return -1;
    846 	}
    847 	return 0;
    848 }
    849 
    850 /*ARGSUSED*/
    851 int
    852 setifaddr(prop_dictionary_t env, prop_dictionary_t xenv)
    853 {
    854 	struct ifreq *ifr;		/* XXX */
    855 	const struct paddr_prefix *pfx0;
    856 	struct paddr_prefix *pfx;
    857 	prop_data_t d;
    858 	const char *ifname;
    859 	int af, s;
    860 	const struct afswtch *afp;
    861 
    862 	if ((af = getaf(env)) == -1)
    863 		af = AF_INET;
    864 
    865 	if ((afp = lookup_af_bynum(af)) == NULL)
    866 		return -1;
    867 
    868 	d = (prop_data_t)prop_dictionary_get(env, "address");
    869 	assert(d != NULL);
    870 	pfx0 = prop_data_data_nocopy(d);
    871 
    872 	if (pfx0->pfx_len >= 0) {
    873 		pfx = prefixlen_to_mask(af, pfx0->pfx_len);
    874 		if (pfx == NULL)
    875 			err(EXIT_FAILURE, "prefixlen_to_mask");
    876 
    877 		if (afp->af_getaddr != NULL)
    878 			(*afp->af_getaddr)(pfx, MASK);
    879 		free(pfx);
    880 	}
    881 
    882 	if (afp->af_addr_commit != NULL)
    883 		return 0;
    884 
    885 	if ((ifname = getifname(env)) == NULL)
    886 		return -1;
    887 
    888 	/*
    889 	 * Delay the ioctl to set the interface addr until flags are all set.
    890 	 * The address interpretation may depend on the flags,
    891 	 * and the flags may change when the address is set.
    892 	 */
    893 	setaddr++;
    894 	if (newaddr == -1)
    895 		newaddr = 1;
    896 	if (doalias == 0 && afp->af_gifaddr != 0) {
    897 		ifr = (struct ifreq *)afp->af_ridreq;
    898 		estrlcpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name));
    899 		ifr->ifr_addr.sa_family = af;
    900 
    901 		if ((s = getsock(af)) == -1)
    902 			err(EXIT_FAILURE, "getsock");
    903 
    904 		if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
    905 			clearaddr = 1;
    906 		else if (errno == EADDRNOTAVAIL)
    907 			/* No address was assigned yet. */
    908 			;
    909 		else
    910 			err(EXIT_FAILURE, "SIOCGIFADDR");
    911 	}
    912 
    913 #if 0
    914 	int i;
    915 	for (i = 0; i < pfx0->pfx_addr.sa_len; i++)
    916 		printf(" %02x", ((const uint8_t *)&pfx->pfx_addr)[i]);
    917 	printf("\n");
    918 #endif
    919 	if (afp->af_getaddr != NULL)
    920 		(*afp->af_getaddr)(pfx0, (doalias >= 0 ? ADDR : RIDADDR));
    921 	return 0;
    922 }
    923 
    924 int
    925 setifnetmask(prop_dictionary_t env, prop_dictionary_t xenv)
    926 {
    927 	const struct paddr_prefix *pfx;
    928 	prop_data_t d;
    929 	int af;
    930 	const struct afswtch *afp;
    931 
    932 	if ((af = getaf(env)) == -1)
    933 		af = AF_INET;
    934 
    935 	if ((afp = lookup_af_bynum(af)) == NULL)
    936 		return -1;
    937 
    938 	d = (prop_data_t)prop_dictionary_get(env, "dstormask");
    939 	assert(d != NULL);
    940 	pfx = prop_data_data_nocopy(d);
    941 
    942 	if (!prop_dictionary_set(xenv, "netmask", (prop_object_t)d))
    943 		return -1;
    944 
    945 	if (afp->af_getaddr != NULL)
    946 		(*afp->af_getaddr)(pfx, MASK);
    947 	return 0;
    948 }
    949 
    950 int
    951 setifbroadaddr(prop_dictionary_t env, prop_dictionary_t xenv)
    952 {
    953 	const struct paddr_prefix *pfx;
    954 	prop_data_t d;
    955 	int af;
    956 	const struct afswtch *afp;
    957 	unsigned short flags;
    958 
    959 	if ((af = getaf(env)) == -1)
    960 		af = AF_INET;
    961 
    962 	if ((afp = lookup_af_bynum(af)) == NULL)
    963 		return -1;
    964 
    965 	if (getifflags(env, xenv, &flags) == -1)
    966 		err(EXIT_FAILURE, "%s: getifflags", __func__);
    967 
    968 	if ((flags & IFF_BROADCAST) == 0)
    969 		errx(EXIT_FAILURE, "not a broadcast interface");
    970 
    971 	d = (prop_data_t)prop_dictionary_get(env, "broadcast");
    972 	assert(d != NULL);
    973 	pfx = prop_data_data_nocopy(d);
    974 
    975 	if (!prop_dictionary_set(xenv, "broadcast", (prop_object_t)d))
    976 		return -1;
    977 
    978 	if (afp->af_getaddr != NULL)
    979 		(*afp->af_getaddr)(pfx, DSTADDR);
    980 
    981 	return 0;
    982 }
    983 
    984 #define rqtosa(__afp, __x) (&(((struct ifreq *)(__afp->__x))->ifr_addr))
    985 
    986 int
    987 notealias(prop_dictionary_t env, prop_dictionary_t xenv)
    988 {
    989 	bool alias, delete;
    990 	int af;
    991 	const struct afswtch *afp;
    992 
    993 	if ((af = getaf(env)) == -1)
    994 		af = AF_INET;
    995 
    996 	if ((afp = lookup_af_bynum(af)) == NULL)
    997 		return -1;
    998 
    999 	if (afp->af_addr_commit != NULL)
   1000 		return 0;
   1001 
   1002 	if (!prop_dictionary_get_bool(env, "alias", &alias)) {
   1003 		errno = ENOENT;
   1004 		return -1;
   1005 	}
   1006 	delete = !alias;
   1007 	if (setaddr && doalias == 0 && delete)
   1008 		memcpy(rqtosa(afp, af_ridreq), rqtosa(afp, af_addreq),
   1009 		    rqtosa(afp, af_addreq)->sa_len);
   1010 	doalias = delete ? -1 : 1;
   1011 	if (delete) {
   1012 		clearaddr = 1;
   1013 		newaddr = 0;
   1014 	} else {
   1015 		clearaddr = 0;
   1016 	}
   1017 	return 0;
   1018 }
   1019 
   1020 /*ARGSUSED*/
   1021 int
   1022 notrailers(prop_dictionary_t env, prop_dictionary_t xenv)
   1023 {
   1024 	puts("Note: trailers are no longer sent, but always received");
   1025 	return 0;
   1026 }
   1027 
   1028 /*ARGSUSED*/
   1029 int
   1030 setifdstormask(prop_dictionary_t env, prop_dictionary_t xenv)
   1031 {
   1032 	const char *key;
   1033 	const struct paddr_prefix *pfx;
   1034 	prop_data_t d;
   1035 	int af, which;
   1036 	const struct afswtch *afp;
   1037 	unsigned short flags;
   1038 
   1039 	if ((af = getaf(env)) == -1)
   1040 		af = AF_INET;
   1041 
   1042 	if ((afp = lookup_af_bynum(af)) == NULL)
   1043 		return -1;
   1044 
   1045 	if (getifflags(env, xenv, &flags) == -1)
   1046 		err(EXIT_FAILURE, "%s: getifflags", __func__);
   1047 
   1048 	d = (prop_data_t)prop_dictionary_get(env, "dstormask");
   1049 	assert(d != NULL);
   1050 	pfx = prop_data_data_nocopy(d);
   1051 
   1052 	if ((flags & IFF_BROADCAST) == 0) {
   1053 		key = "dst";
   1054 		which = DSTADDR;
   1055 	} else {
   1056 		key = "netmask";
   1057 		which = MASK;
   1058 	}
   1059 
   1060 	if (!prop_dictionary_set(xenv, key, (prop_object_t)d))
   1061 		return -1;
   1062 
   1063 	if (afp->af_getaddr != NULL)
   1064 		(*afp->af_getaddr)(pfx, which);
   1065 	return 0;
   1066 }
   1067 
   1068 void
   1069 check_ifflags_up(prop_dictionary_t env)
   1070 {
   1071 	struct ifreq ifr;
   1072 
   1073  	if (direct_ioctl(env, SIOCGIFFLAGS, &ifr) == -1)
   1074 		err(EXIT_FAILURE, "SIOCGIFFLAGS");
   1075 	if (ifr.ifr_flags & IFF_UP)
   1076 		return;
   1077 	ifr.ifr_flags |= IFF_UP;
   1078 	if (direct_ioctl(env, SIOCSIFFLAGS, &ifr) == -1)
   1079 		err(EXIT_FAILURE, "SIOCSIFFLAGS");
   1080 }
   1081 
   1082 int
   1083 setifflags(prop_dictionary_t env, prop_dictionary_t xenv)
   1084 {
   1085 	struct ifreq ifr;
   1086 	int64_t ifflag;
   1087 	bool rc;
   1088 
   1089 	rc = prop_dictionary_get_int64(env, "ifflag", &ifflag);
   1090 	assert(rc);
   1091 
   1092  	if (direct_ioctl(env, SIOCGIFFLAGS, &ifr) == -1)
   1093 		return -1;
   1094 
   1095 	if (ifflag < 0) {
   1096 		ifflag = -ifflag;
   1097 		if (ifflag == IFF_UP)
   1098 			check_up_state = 0;
   1099 		ifr.ifr_flags &= ~ifflag;
   1100 	} else
   1101 		ifr.ifr_flags |= ifflag;
   1102 
   1103 	if (direct_ioctl(env, SIOCSIFFLAGS, &ifr) == -1)
   1104 		return -1;
   1105 
   1106 	return 0;
   1107 }
   1108 
   1109 static int
   1110 getifcaps(prop_dictionary_t env, prop_dictionary_t oenv, struct ifcapreq *oifcr)
   1111 {
   1112 	bool rc;
   1113 	struct ifcapreq ifcr;
   1114 	const struct ifcapreq *tmpifcr;
   1115 	prop_data_t capdata;
   1116 
   1117 	capdata = (prop_data_t)prop_dictionary_get(env, "ifcaps");
   1118 
   1119 	if (capdata != NULL) {
   1120 		tmpifcr = prop_data_data_nocopy(capdata);
   1121 		*oifcr = *tmpifcr;
   1122 		return 0;
   1123 	}
   1124 
   1125 	(void)direct_ioctl(env, SIOCGIFCAP, &ifcr);
   1126 	*oifcr = ifcr;
   1127 
   1128 	capdata = prop_data_create_data(&ifcr, sizeof(ifcr));
   1129 
   1130 	rc = prop_dictionary_set(oenv, "ifcaps", capdata);
   1131 
   1132 	prop_object_release((prop_object_t)capdata);
   1133 
   1134 	return rc ? 0 : -1;
   1135 }
   1136 
   1137 static int
   1138 setifcaps(prop_dictionary_t env, prop_dictionary_t oenv)
   1139 {
   1140 	int64_t ifcap;
   1141 	int s;
   1142 	bool rc;
   1143 	prop_data_t capdata;
   1144 	const char *ifname;
   1145 	struct ifcapreq ifcr;
   1146 
   1147 	s = getsock(AF_INET);
   1148 
   1149 	rc = prop_dictionary_get_int64(env, "ifcap", &ifcap);
   1150 	assert(rc);
   1151 
   1152 	if ((ifname = getifname(env)) == NULL)
   1153 		return -1;
   1154 
   1155 	if (getifcaps(env, oenv, &ifcr) == -1)
   1156 		return -1;
   1157 
   1158 	if (ifcap < 0) {
   1159 		ifcap = -ifcap;
   1160 		ifcr.ifcr_capenable &= ~ifcap;
   1161 	} else
   1162 		ifcr.ifcr_capenable |= ifcap;
   1163 
   1164 	if ((capdata = prop_data_create_data(&ifcr, sizeof(ifcr))) == NULL)
   1165 		return -1;
   1166 
   1167 	rc = prop_dictionary_set(oenv, "ifcaps", capdata);
   1168 	prop_object_release((prop_object_t)capdata);
   1169 
   1170 	return rc ? 0 : -1;
   1171 }
   1172 
   1173 static int
   1174 setifmetric(prop_dictionary_t env, prop_dictionary_t xenv)
   1175 {
   1176 	struct ifreq ifr;
   1177 	bool rc;
   1178 	int64_t metric;
   1179 
   1180 	rc = prop_dictionary_get_int64(env, "metric", &metric);
   1181 	assert(rc);
   1182 
   1183 	ifr.ifr_metric = metric;
   1184 	if (direct_ioctl(env, SIOCSIFMETRIC, &ifr) == -1)
   1185 		warn("SIOCSIFMETRIC");
   1186 	return 0;
   1187 }
   1188 
   1189 static void
   1190 do_setifpreference(prop_dictionary_t env)
   1191 {
   1192 	struct if_addrprefreq ifap;
   1193 	int af;
   1194 	const struct afswtch *afp;
   1195 	prop_data_t d;
   1196 	const struct paddr_prefix *pfx;
   1197 
   1198 	if ((af = getaf(env)) == -1)
   1199 		af = AF_INET;
   1200 
   1201 	if ((afp = lookup_af_bynum(af)) == NULL)
   1202 		errx(EXIT_FAILURE, "%s: lookup_af_bynum", __func__);
   1203 
   1204 	memset(&ifap, 0, sizeof(ifap));
   1205 
   1206 	if (!prop_dictionary_get_int16(env, "preference",
   1207 	    &ifap.ifap_preference))
   1208 		return;
   1209 
   1210 	d = (prop_data_t)prop_dictionary_get(env, "address");
   1211 	assert(d != NULL);
   1212 
   1213 	pfx = prop_data_data_nocopy(d);
   1214 
   1215 	memcpy(&ifap.ifap_addr, &pfx->pfx_addr,
   1216 	    MIN(sizeof(ifap.ifap_addr), pfx->pfx_addr.sa_len));
   1217 	if (direct_ioctl(env, SIOCSIFADDRPREF, &ifap) == -1)
   1218 		warn("SIOCSIFADDRPREF");
   1219 }
   1220 
   1221 static int
   1222 setifmtu(prop_dictionary_t env, prop_dictionary_t xenv)
   1223 {
   1224 	int64_t mtu;
   1225 	bool rc;
   1226 	struct ifreq ifr;
   1227 
   1228 	rc = prop_dictionary_get_int64(env, "mtu", &mtu);
   1229 	assert(rc);
   1230 
   1231 	ifr.ifr_mtu = mtu;
   1232 	if (direct_ioctl(env, SIOCSIFMTU, &ifr) == -1)
   1233 		warn("SIOCSIFMTU");
   1234 
   1235 	return 0;
   1236 }
   1237 
   1238 static void
   1239 media_error(int type, const char *val, const char *opt)
   1240 {
   1241 	errx(EXIT_FAILURE, "unknown %s media %s: %s",
   1242 		get_media_type_string(type), opt, val);
   1243 }
   1244 
   1245 void
   1246 init_current_media(prop_dictionary_t env, prop_dictionary_t oenv)
   1247 {
   1248 	const char *ifname;
   1249 	struct ifmediareq ifmr;
   1250 
   1251 	if ((ifname = getifname(env)) == NULL)
   1252 		err(EXIT_FAILURE, "getifname");
   1253 
   1254 	/*
   1255 	 * If we have not yet done so, grab the currently-selected
   1256 	 * media.
   1257 	 */
   1258 
   1259 	if (prop_dictionary_get(env, "initmedia") == NULL) {
   1260 		memset(&ifmr, 0, sizeof(ifmr));
   1261 
   1262 		if (direct_ioctl(env, SIOCGIFMEDIA, &ifmr) == -1) {
   1263 			/*
   1264 			 * If we get E2BIG, the kernel is telling us
   1265 			 * that there are more, so we can ignore it.
   1266 			 */
   1267 			if (errno != E2BIG)
   1268 				err(EXIT_FAILURE, "SIOCGIFMEDIA");
   1269 		}
   1270 
   1271 		if (!prop_dictionary_set_bool(oenv, "initmedia", true)) {
   1272 			err(EXIT_FAILURE, "%s: prop_dictionary_set_bool",
   1273 			    __func__);
   1274 		}
   1275 		media_current = ifmr.ifm_current;
   1276 	}
   1277 
   1278 	/* Sanity. */
   1279 	if (IFM_TYPE(media_current) == 0)
   1280 		errx(EXIT_FAILURE, "%s: no link type?", ifname);
   1281 }
   1282 
   1283 void
   1284 process_media_commands(prop_dictionary_t env)
   1285 {
   1286 	struct ifreq ifr;
   1287 
   1288 	if (prop_dictionary_get(env, "media") == NULL &&
   1289 	    prop_dictionary_get(env, "mediaopt") == NULL &&
   1290 	    prop_dictionary_get(env, "unmediaopt") == NULL &&
   1291 	    prop_dictionary_get(env, "mediamode") == NULL) {
   1292 		/* Nothing to do. */
   1293 		return;
   1294 	}
   1295 
   1296 	/*
   1297 	 * Media already set up, and commands sanity-checked.  Set/clear
   1298 	 * any options, and we're ready to go.
   1299 	 */
   1300 	media_current |= mediaopt_set;
   1301 	media_current &= ~mediaopt_clear;
   1302 
   1303 	memset(&ifr, 0, sizeof(ifr));
   1304 	ifr.ifr_media = media_current;
   1305 
   1306 	if (direct_ioctl(env, SIOCSIFMEDIA, &ifr) == -1)
   1307 		err(EXIT_FAILURE, "SIOCSIFMEDIA");
   1308 }
   1309 
   1310 int
   1311 setmedia(prop_dictionary_t env, prop_dictionary_t xenv)
   1312 {
   1313 	int type, subtype, inst;
   1314 	prop_data_t data;
   1315 	char *val;
   1316 
   1317 	init_current_media(env, xenv);
   1318 
   1319 	data = (prop_data_t)prop_dictionary_get(env, "media");
   1320 	assert(data != NULL);
   1321 
   1322 	/* Only one media command may be given. */
   1323 	/* Must not come after mode commands */
   1324 	/* Must not come after mediaopt commands */
   1325 
   1326 	/*
   1327 	 * No need to check if `instance' has been issued; setmediainst()
   1328 	 * craps out if `media' has not been specified.
   1329 	 */
   1330 
   1331 	type = IFM_TYPE(media_current);
   1332 	inst = IFM_INST(media_current);
   1333 
   1334 	val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
   1335 	if (val == NULL)
   1336 		return -1;
   1337 
   1338 	/* Look up the subtype. */
   1339 	subtype = get_media_subtype(type, val);
   1340 	if (subtype == -1)
   1341 		media_error(type, val, "subtype");
   1342 
   1343 	/* Build the new current media word. */
   1344 	media_current = IFM_MAKEWORD(type, subtype, 0, inst);
   1345 
   1346 	/* Media will be set after other processing is complete. */
   1347 	return 0;
   1348 }
   1349 
   1350 int
   1351 setmediaopt(prop_dictionary_t env, prop_dictionary_t xenv)
   1352 {
   1353 	char *invalid;
   1354 	prop_data_t data;
   1355 	char *val;
   1356 
   1357 	init_current_media(env, xenv);
   1358 
   1359 	data = (prop_data_t)prop_dictionary_get(env, "mediaopt");
   1360 	assert(data != NULL);
   1361 
   1362 	/* Can only issue `mediaopt' once. */
   1363 	/* Can't issue `mediaopt' if `instance' has already been issued. */
   1364 
   1365 	val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
   1366 	if (val == NULL)
   1367 		return -1;
   1368 
   1369 	mediaopt_set = get_media_options(media_current, val, &invalid);
   1370 	free(val);
   1371 	if (mediaopt_set == -1)
   1372 		media_error(media_current, invalid, "option");
   1373 
   1374 	/* Media will be set after other processing is complete. */
   1375 	return 0;
   1376 }
   1377 
   1378 int
   1379 unsetmediaopt(prop_dictionary_t env, prop_dictionary_t xenv)
   1380 {
   1381 	char *invalid, *val;
   1382 	prop_data_t data;
   1383 
   1384 	init_current_media(env, xenv);
   1385 
   1386 	data = (prop_data_t)prop_dictionary_get(env, "unmediaopt");
   1387 	if (data == NULL) {
   1388 		errno = ENOENT;
   1389 		return -1;
   1390 	}
   1391 
   1392 	val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
   1393 	if (val == NULL)
   1394 		return -1;
   1395 
   1396 	/*
   1397 	 * No need to check for A_MEDIAINST, since the test for A_MEDIA
   1398 	 * implicitly checks for A_MEDIAINST.
   1399 	 */
   1400 
   1401 	mediaopt_clear = get_media_options(media_current, val, &invalid);
   1402 	free(val);
   1403 	if (mediaopt_clear == -1)
   1404 		media_error(media_current, invalid, "option");
   1405 
   1406 	/* Media will be set after other processing is complete. */
   1407 	return 0;
   1408 }
   1409 
   1410 int
   1411 setmediainst(prop_dictionary_t env, prop_dictionary_t xenv)
   1412 {
   1413 	int type, subtype, options;
   1414 	int64_t inst;
   1415 	bool rc;
   1416 
   1417 	init_current_media(env, xenv);
   1418 
   1419 	rc = prop_dictionary_get_int64(env, "mediainst", &inst);
   1420 	assert(rc);
   1421 
   1422 	/* Can only issue `instance' once. */
   1423 	/* Must have already specified `media' */
   1424 
   1425 	type = IFM_TYPE(media_current);
   1426 	subtype = IFM_SUBTYPE(media_current);
   1427 	options = IFM_OPTIONS(media_current);
   1428 
   1429 	media_current = IFM_MAKEWORD(type, subtype, options, inst);
   1430 
   1431 	/* Media will be set after other processing is complete. */
   1432 	return 0;
   1433 }
   1434 
   1435 int
   1436 setmediamode(prop_dictionary_t env, prop_dictionary_t xenv)
   1437 {
   1438 	int type, subtype, options, inst, mode;
   1439 	prop_data_t data;
   1440 	char *val;
   1441 
   1442 	init_current_media(env, xenv);
   1443 
   1444 	data = (prop_data_t)prop_dictionary_get(env, "mediamode");
   1445 	assert(data != NULL);
   1446 
   1447 	type = IFM_TYPE(media_current);
   1448 	subtype = IFM_SUBTYPE(media_current);
   1449 	options = IFM_OPTIONS(media_current);
   1450 	inst = IFM_INST(media_current);
   1451 
   1452 	val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
   1453 	if (val == NULL)
   1454 		return -1;
   1455 
   1456 	mode = get_media_mode(type, val);
   1457 	if (mode == -1)
   1458 		media_error(type, val, "mode");
   1459 
   1460 	free(val);
   1461 
   1462 	media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
   1463 
   1464 	/* Media will be set after other processing is complete. */
   1465 	return 0;
   1466 }
   1467 
   1468 void
   1469 print_media_word(int ifmw, const char *opt_sep)
   1470 {
   1471 	const char *str;
   1472 
   1473 	printf("%s", get_media_subtype_string(ifmw));
   1474 
   1475 	/* Find mode. */
   1476 	if (IFM_MODE(ifmw) != 0) {
   1477 		str = get_media_mode_string(ifmw);
   1478 		if (str != NULL)
   1479 			printf(" mode %s", str);
   1480 	}
   1481 
   1482 	/* Find options. */
   1483 	for (; (str = get_media_option_string(&ifmw)) != NULL; opt_sep = ",")
   1484 		printf("%s%s", opt_sep, str);
   1485 
   1486 	if (IFM_INST(ifmw) != 0)
   1487 		printf(" instance %d", IFM_INST(ifmw));
   1488 }
   1489 
   1490 int
   1491 carrier(prop_dictionary_t env)
   1492 {
   1493 	struct ifmediareq ifmr;
   1494 
   1495 	memset(&ifmr, 0, sizeof(ifmr));
   1496 
   1497 	if (direct_ioctl(env, SIOCGIFMEDIA, &ifmr) == -1) {
   1498 		/*
   1499 		 * Interface doesn't support SIOC{G,S}IFMEDIA;
   1500 		 * assume ok.
   1501 		 */
   1502 		return EXIT_SUCCESS;
   1503 	}
   1504 	if ((ifmr.ifm_status & IFM_AVALID) == 0) {
   1505 		/*
   1506 		 * Interface doesn't report media-valid status.
   1507 		 * assume ok.
   1508 		 */
   1509 		return EXIT_SUCCESS;
   1510 	}
   1511 	/* otherwise, return ok for active, not-ok if not active. */
   1512 	if (ifmr.ifm_status & IFM_ACTIVE)
   1513 		return EXIT_SUCCESS;
   1514 	else
   1515 		return EXIT_FAILURE;
   1516 }
   1517 
   1518 const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
   1519 
   1520 const struct ifmedia_status_description ifm_status_descriptions[] =
   1521     IFM_STATUS_DESCRIPTIONS;
   1522 
   1523 /*
   1524  * Print the status of the interface.  If an address family was
   1525  * specified, show it and it only; otherwise, show them all.
   1526  */
   1527 void
   1528 status(const struct sockaddr_dl *sdl, prop_dictionary_t env,
   1529     prop_dictionary_t oenv)
   1530 {
   1531 	struct ifmediareq ifmr;
   1532 	struct ifdatareq ifdr;
   1533 	struct ifreq ifr;
   1534 	int *media_list, i;
   1535 	char hbuf[NI_MAXHOST];
   1536 	char fbuf[BUFSIZ];
   1537 	int af, s;
   1538 	const char *ifname;
   1539 	struct ifcapreq ifcr;
   1540 	unsigned short flags;
   1541 	const struct afswtch *afp;
   1542 
   1543 	if ((af = getaf(env)) == -1) {
   1544 		afp = NULL;
   1545 		af = AF_UNSPEC;
   1546 	} else
   1547 		afp = lookup_af_bynum(af);
   1548 
   1549 	/* get out early if the family is unsupported by the kernel */
   1550 	if ((s = getsock(af)) == -1)
   1551 		err(EXIT_FAILURE, "%s: getsock", __func__);
   1552 
   1553 	if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
   1554 		err(EXIT_FAILURE, "%s: getifinfo", __func__);
   1555 
   1556 	(void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
   1557 	printf("%s: flags=%s", ifname, &fbuf[2]);
   1558 
   1559 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
   1560 	if (ioctl(s, SIOCGIFMETRIC, &ifr) == -1)
   1561 		warn("SIOCGIFMETRIC %s", ifr.ifr_name);
   1562 	else if (ifr.ifr_metric != 0)
   1563 		printf(" metric %d", ifr.ifr_metric);
   1564 
   1565 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
   1566 	if (ioctl(s, SIOCGIFMTU, &ifr) != -1 && ifr.ifr_mtu != 0)
   1567 		printf(" mtu %d", ifr.ifr_mtu);
   1568 	printf("\n");
   1569 
   1570 	if (getifcaps(env, oenv, &ifcr) == -1)
   1571 		err(EXIT_FAILURE, "%s: getifcaps", __func__);
   1572 
   1573 	if (ifcr.ifcr_capabilities != 0) {
   1574 		(void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
   1575 		    ifcr.ifcr_capabilities);
   1576 		printf("\tcapabilities=%s\n", &fbuf[2]);
   1577 		(void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
   1578 		    ifcr.ifcr_capenable);
   1579 		printf("\tenabled=%s\n", &fbuf[2]);
   1580 	}
   1581 
   1582 	ieee80211_status(env, oenv);
   1583 	vlan_status(env, oenv);
   1584 #ifndef INET_ONLY
   1585 	carp_status(env, oenv);
   1586 #endif
   1587 	tunnel_status(env, oenv);
   1588 	agr_status(env, oenv);
   1589 
   1590 	if (sdl != NULL &&
   1591 	    getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len,
   1592 		hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
   1593 	    hbuf[0] != '\0')
   1594 		printf("\taddress: %s\n", hbuf);
   1595 
   1596 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1597 	estrlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
   1598 
   1599 	if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
   1600 		/*
   1601 		 * Interface doesn't support SIOC{G,S}IFMEDIA.
   1602 		 */
   1603 		goto iface_stats;
   1604 	}
   1605 
   1606 	if (ifmr.ifm_count == 0) {
   1607 		warnx("%s: no media types?", ifname);
   1608 		goto iface_stats;
   1609 	}
   1610 
   1611 	media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
   1612 	if (media_list == NULL)
   1613 		err(EXIT_FAILURE, "malloc");
   1614 	ifmr.ifm_ulist = media_list;
   1615 
   1616 	if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1)
   1617 		err(EXIT_FAILURE, "SIOCGIFMEDIA");
   1618 
   1619 	printf("\tmedia: %s ", get_media_type_string(ifmr.ifm_current));
   1620 	print_media_word(ifmr.ifm_current, " ");
   1621 	if (ifmr.ifm_active != ifmr.ifm_current) {
   1622 		printf(" (");
   1623 		print_media_word(ifmr.ifm_active, " ");
   1624 		printf(")");
   1625 	}
   1626 	printf("\n");
   1627 
   1628 	if (ifmr.ifm_status & IFM_STATUS_VALID) {
   1629 		const struct ifmedia_status_description *ifms;
   1630 		int bitno, found = 0;
   1631 
   1632 		printf("\tstatus: ");
   1633 		for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
   1634 			for (ifms = ifm_status_descriptions;
   1635 			     ifms->ifms_valid != 0; ifms++) {
   1636 				if (ifms->ifms_type !=
   1637 				      IFM_TYPE(ifmr.ifm_current) ||
   1638 				    ifms->ifms_valid !=
   1639 				      ifm_status_valid_list[bitno])
   1640 					continue;
   1641 				printf("%s%s", found ? ", " : "",
   1642 				    IFM_STATUS_DESC(ifms, ifmr.ifm_status));
   1643 				found = 1;
   1644 
   1645 				/*
   1646 				 * For each valid indicator bit, there's
   1647 				 * only one entry for each media type, so
   1648 				 * terminate the inner loop now.
   1649 				 */
   1650 				break;
   1651 			}
   1652 		}
   1653 
   1654 		if (found == 0)
   1655 			printf("unknown");
   1656 		printf("\n");
   1657 	}
   1658 
   1659 	if (mflag) {
   1660 		int type, printed_type;
   1661 
   1662 		for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
   1663 			for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
   1664 				if (IFM_TYPE(media_list[i]) != type)
   1665 					continue;
   1666 				if (printed_type == 0) {
   1667 					printf("\tsupported %s media:\n",
   1668 					    get_media_type_string(type));
   1669 					printed_type = 1;
   1670 				}
   1671 				printf("\t\tmedia ");
   1672 				print_media_word(media_list[i], " mediaopt ");
   1673 				printf("\n");
   1674 			}
   1675 		}
   1676 	}
   1677 
   1678 	free(media_list);
   1679 
   1680  iface_stats:
   1681 	if (!vflag && !zflag)
   1682 		goto proto_status;
   1683 
   1684 	estrlcpy(ifdr.ifdr_name, ifname, sizeof(ifdr.ifdr_name));
   1685 
   1686 	if (ioctl(s, zflag ? SIOCZIFDATA:SIOCGIFDATA, &ifdr) == -1) {
   1687 		err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
   1688 	} else {
   1689 		struct if_data * const ifi = &ifdr.ifdr_data;
   1690 		char buf[5];
   1691 
   1692 #define	PLURAL(n)	((n) == 1 ? "" : "s")
   1693 #define PLURALSTR(s)	((atof(s)) == 1.0 ? "" : "s")
   1694 		printf("\tinput: %llu packet%s, ",
   1695 		    (unsigned long long) ifi->ifi_ipackets,
   1696 		    PLURAL(ifi->ifi_ipackets));
   1697 		if (hflag) {
   1698 			(void) humanize_number(buf, sizeof(buf),
   1699 			    (int64_t) ifi->ifi_ibytes, "", HN_AUTOSCALE,
   1700 			    HN_NOSPACE | HN_DECIMAL);
   1701 			printf("%s byte%s", buf,
   1702 			    PLURALSTR(buf));
   1703 		} else
   1704 			printf("%llu byte%s",
   1705 			    (unsigned long long) ifi->ifi_ibytes,
   1706 		            PLURAL(ifi->ifi_ibytes));
   1707 		if (ifi->ifi_imcasts)
   1708 			printf(", %llu multicast%s",
   1709 			    (unsigned long long) ifi->ifi_imcasts,
   1710 			    PLURAL(ifi->ifi_imcasts));
   1711 		if (ifi->ifi_ierrors)
   1712 			printf(", %llu error%s",
   1713 			    (unsigned long long) ifi->ifi_ierrors,
   1714 			    PLURAL(ifi->ifi_ierrors));
   1715 		if (ifi->ifi_iqdrops)
   1716 			printf(", %llu queue drop%s",
   1717 			    (unsigned long long) ifi->ifi_iqdrops,
   1718 			    PLURAL(ifi->ifi_iqdrops));
   1719 		if (ifi->ifi_noproto)
   1720 			printf(", %llu unknown protocol",
   1721 			    (unsigned long long) ifi->ifi_noproto);
   1722 		printf("\n\toutput: %llu packet%s, ",
   1723 		    (unsigned long long) ifi->ifi_opackets,
   1724 		    PLURAL(ifi->ifi_opackets));
   1725 		if (hflag) {
   1726 			(void) humanize_number(buf, sizeof(buf),
   1727 			    (int64_t) ifi->ifi_obytes, "", HN_AUTOSCALE,
   1728 			    HN_NOSPACE | HN_DECIMAL);
   1729 			printf("%s byte%s", buf,
   1730 			    PLURALSTR(buf));
   1731 		} else
   1732 			printf("%llu byte%s",
   1733 			    (unsigned long long) ifi->ifi_obytes,
   1734 			    PLURAL(ifi->ifi_obytes));
   1735 		if (ifi->ifi_omcasts)
   1736 			printf(", %llu multicast%s",
   1737 			    (unsigned long long) ifi->ifi_omcasts,
   1738 			    PLURAL(ifi->ifi_omcasts));
   1739 		if (ifi->ifi_oerrors)
   1740 			printf(", %llu error%s",
   1741 			    (unsigned long long) ifi->ifi_oerrors,
   1742 			    PLURAL(ifi->ifi_oerrors));
   1743 		if (ifi->ifi_collisions)
   1744 			printf(", %llu collision%s",
   1745 			    (unsigned long long) ifi->ifi_collisions,
   1746 			    PLURAL(ifi->ifi_collisions));
   1747 		printf("\n");
   1748 #undef PLURAL
   1749 #undef PLURALSTR
   1750 	}
   1751 
   1752 	ieee80211_statistics(env);
   1753 
   1754  proto_status:
   1755 
   1756 	if (afp != NULL)
   1757 		(*afp->af_status)(env, oenv, true);
   1758 	else for (afp = afs; afp->af_name != NULL; afp++)
   1759 		(*afp->af_status)(env, oenv, false);
   1760 }
   1761 
   1762 int
   1763 setifprefixlen(prop_dictionary_t env, prop_dictionary_t xenv)
   1764 {
   1765 	const char *ifname;
   1766 	bool rc;
   1767 	int64_t plen;
   1768 	int af;
   1769 	const struct afswtch *afp;
   1770 	unsigned short flags;
   1771 	struct paddr_prefix *pfx;
   1772 	prop_data_t d;
   1773 
   1774 	if ((af = getaf(env)) == -1)
   1775 		af = AF_INET;
   1776 
   1777 	if ((afp = lookup_af_bynum(af)) == NULL)
   1778 		return -1;
   1779 
   1780 	if ((ifname = getifinfo(env, xenv, &flags)) == NULL)
   1781 		err(EXIT_FAILURE, "getifinfo");
   1782 
   1783 	rc = prop_dictionary_get_int64(env, "prefixlen", &plen);
   1784 	assert(rc);
   1785 
   1786 	pfx = prefixlen_to_mask(af, plen);
   1787 	if (pfx == NULL)
   1788 		err(EXIT_FAILURE, "prefixlen_to_mask");
   1789 
   1790 	d = prop_data_create_data(pfx,
   1791 	    offsetof(struct paddr_prefix, pfx_addr) + pfx->pfx_addr.sa_len);
   1792 	if (d == NULL)
   1793 		err(EXIT_FAILURE, "%s: prop_data_create_data", __func__);
   1794 
   1795 	if (!prop_dictionary_set(xenv, "netmask", (prop_object_t)d))
   1796 		err(EXIT_FAILURE, "%s: prop_dictionary_set", __func__);
   1797 
   1798 	if (afp->af_getaddr != NULL)
   1799 		(*afp->af_getaddr)(pfx, MASK);
   1800 
   1801 	free(pfx);
   1802 	return 0;
   1803 }
   1804 
   1805 void
   1806 usage(void)
   1807 {
   1808 	const char *progname = getprogname();
   1809 
   1810 	fprintf(stderr,
   1811 	    "usage: %s [-h] [-m] [-v] [-z] "
   1812 #ifdef INET6
   1813 		"[-L] "
   1814 #endif
   1815 		"interface\n"
   1816 		"\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
   1817 		"\t\t[ alias | -alias ] ]\n"
   1818 		"\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
   1819 		"\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
   1820 		"\t[ list scan ]\n"
   1821 		"\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
   1822 		"\t[ hidessid | -hidessid ] [ apbridge | -apbridge ]\n"
   1823 		"\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
   1824 		"\t[ arp | -arp ]\n"
   1825 		"\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
   1826 		"[ instance minst ]\n"
   1827 		"\t[ preference n ]\n"
   1828 		"\t[ vlan n vlanif i ]\n"
   1829 		"\t[ agrport i ] [ -agrport i ]\n"
   1830 		"\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
   1831 		"\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
   1832 		"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
   1833 		"       %s -a [-b] [-h] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
   1834 		"       %s -l [-b] [-d] [-u] [-s]\n"
   1835 		"       %s -C\n"
   1836 		"       %s interface create\n"
   1837 		"       %s interface destroy\n",
   1838 		progname, progname, progname, progname, progname, progname);
   1839 	exit(EXIT_FAILURE);
   1840 }
   1841