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