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