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