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