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