Home | History | Annotate | Line # | Download | only in ifconfig
ifconfig.c revision 1.208
      1  1.208    dyoung /*	$NetBSD: ifconfig.c,v 1.208 2008/07/02 07:44:14 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.208    dyoung __RCSID("$NetBSD: ifconfig.c,v 1.208 2008/07/02 07:44:14 dyoung Exp $");
     67    1.1       cgd #endif /* not lint */
     68    1.1       cgd 
     69    1.1       cgd #include <sys/param.h>
     70  1.208    dyoung #include <sys/queue.h>
     71    1.1       cgd #include <sys/socket.h>
     72    1.1       cgd #include <sys/ioctl.h>
     73    1.1       cgd 
     74    1.1       cgd #include <net/if.h>
     75   1.30   thorpej #include <net/if_dl.h>
     76   1.24   thorpej #include <net/if_media.h>
     77   1.62    chopps #include <net/if_ether.h>
     78  1.166   thorpej #include <netinet/in.h>		/* XXX */
     79  1.166   thorpej #include <netinet/in_var.h>	/* XXX */
     80  1.187    dyoung 
     81   1.13   mycroft #include <netdb.h>
     82    1.1       cgd 
     83    1.1       cgd #include <sys/protosw.h>
     84    1.1       cgd 
     85  1.187    dyoung #include <assert.h>
     86   1.13   mycroft #include <ctype.h>
     87   1.13   mycroft #include <err.h>
     88   1.13   mycroft #include <errno.h>
     89  1.185    dyoung #include <stdbool.h>
     90   1.50    chopps #include <stddef.h>
     91    1.1       cgd #include <stdio.h>
     92    1.1       cgd #include <stdlib.h>
     93    1.1       cgd #include <string.h>
     94   1.13   mycroft #include <unistd.h>
     95   1.78    itojun #include <ifaddrs.h>
     96  1.130  christos #include <util.h>
     97    1.1       cgd 
     98  1.156   thorpej #include "extern.h"
     99  1.161   thorpej 
    100  1.208    dyoung #include "media.h"
    101  1.187    dyoung #include "parse.h"
    102  1.187    dyoung #include "env.h"
    103  1.153      yamt 
    104  1.208    dyoung static bool bflag, dflag, hflag, sflag, uflag;
    105  1.208    dyoung bool lflag, vflag, zflag;
    106    1.1       cgd 
    107  1.208    dyoung static char gflags[10 + 26 * 2 + 1] = "AabCdhlsuvz";
    108  1.208    dyoung bool gflagset[10 + 26 * 2];
    109  1.187    dyoung 
    110  1.208    dyoung static int carrier(prop_dictionary_t);
    111  1.208    dyoung static int clone_command(prop_dictionary_t, prop_dictionary_t);
    112  1.208    dyoung static void do_setifpreference(prop_dictionary_t);
    113  1.208    dyoung static int flag_index(int);
    114  1.208    dyoung static void init_afs(void);
    115  1.208    dyoung static int list_cloners(prop_dictionary_t, prop_dictionary_t);
    116  1.208    dyoung static int media_status_exec(prop_dictionary_t, prop_dictionary_t);
    117  1.187    dyoung static int no_cmds_exec(prop_dictionary_t, prop_dictionary_t);
    118  1.208    dyoung static int notrailers(prop_dictionary_t, prop_dictionary_t);
    119  1.208    dyoung static void printall(const char *, prop_dictionary_t);
    120  1.208    dyoung static int setifaddr(prop_dictionary_t, prop_dictionary_t);
    121  1.208    dyoung static int setifbroadaddr(prop_dictionary_t, prop_dictionary_t);
    122  1.208    dyoung static int setifcaps(prop_dictionary_t, prop_dictionary_t);
    123  1.208    dyoung static int setifdstormask(prop_dictionary_t, prop_dictionary_t);
    124  1.208    dyoung static int setifflags(prop_dictionary_t, prop_dictionary_t);
    125  1.208    dyoung static int setifmetric(prop_dictionary_t, prop_dictionary_t);
    126  1.208    dyoung static int setifmtu(prop_dictionary_t, prop_dictionary_t);
    127  1.208    dyoung static int setifnetmask(prop_dictionary_t, prop_dictionary_t);
    128  1.208    dyoung static int setifprefixlen(prop_dictionary_t, prop_dictionary_t);
    129  1.208    dyoung static void status(const struct sockaddr_dl *, prop_dictionary_t,
    130  1.189    dyoung     prop_dictionary_t);
    131  1.208    dyoung static void usage(void);
    132  1.187    dyoung 
    133  1.198    dyoung static const struct kwinst ifflagskw[] = {
    134  1.187    dyoung 	  IFKW("arp", IFF_NOARP)
    135  1.187    dyoung 	, IFKW("debug", IFF_DEBUG)
    136  1.187    dyoung 	, IFKW("link0", IFF_LINK0)
    137  1.187    dyoung 	, IFKW("link1", IFF_LINK1)
    138  1.187    dyoung 	, IFKW("link2", IFF_LINK2)
    139  1.205    dyoung 	, {.k_word = "down", .k_type = KW_T_INT, .k_int = -IFF_UP}
    140  1.205    dyoung 	, {.k_word = "up", .k_type = KW_T_INT, .k_int = IFF_UP}
    141  1.187    dyoung };
    142  1.187    dyoung 
    143  1.198    dyoung static const struct kwinst ifcapskw[] = {
    144  1.187    dyoung 	  IFKW("ip4csum-tx",	IFCAP_CSUM_IPv4_Tx)
    145  1.187    dyoung 	, IFKW("ip4csum-rx",	IFCAP_CSUM_IPv4_Rx)
    146  1.187    dyoung 	, IFKW("tcp4csum-tx",	IFCAP_CSUM_TCPv4_Tx)
    147  1.187    dyoung 	, IFKW("tcp4csum-rx",	IFCAP_CSUM_TCPv4_Rx)
    148  1.187    dyoung 	, IFKW("udp4csum-tx",	IFCAP_CSUM_UDPv4_Tx)
    149  1.187    dyoung 	, IFKW("udp4csum-rx",	IFCAP_CSUM_UDPv4_Rx)
    150  1.187    dyoung 	, IFKW("tcp6csum-tx",	IFCAP_CSUM_TCPv6_Tx)
    151  1.187    dyoung 	, IFKW("tcp6csum-rx",	IFCAP_CSUM_TCPv6_Rx)
    152  1.187    dyoung 	, IFKW("udp6csum-tx",	IFCAP_CSUM_UDPv6_Tx)
    153  1.187    dyoung 	, IFKW("udp6csum-rx",	IFCAP_CSUM_UDPv6_Rx)
    154  1.187    dyoung 	, IFKW("ip4csum",	IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx)
    155  1.187    dyoung 	, IFKW("tcp4csum",	IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx)
    156  1.187    dyoung 	, IFKW("udp4csum",	IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx)
    157  1.187    dyoung 	, IFKW("tcp6csum",	IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx)
    158  1.187    dyoung 	, IFKW("udp6csum",	IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx)
    159  1.187    dyoung 	, IFKW("tso4",		IFCAP_TSOv4)
    160  1.187    dyoung 	, IFKW("tso6",		IFCAP_TSOv6)
    161  1.187    dyoung };
    162   1.45   thorpej 
    163  1.187    dyoung extern struct pbranch command_root;
    164  1.187    dyoung extern struct pbranch opt_command;
    165  1.187    dyoung extern struct pbranch opt_family, opt_silent_family;
    166  1.200    dyoung extern struct pkw cloning, silent_family, family, ifcaps, ifflags, misc;
    167  1.187    dyoung 
    168  1.187    dyoung struct pinteger parse_metric = PINTEGER_INITIALIZER(&parse_metric, "metric", 10,
    169  1.187    dyoung     setifmetric, "metric", &command_root.pb_parser);
    170  1.187    dyoung 
    171  1.187    dyoung struct pinteger parse_mtu = PINTEGER_INITIALIZER(&parse_mtu, "mtu", 10,
    172  1.187    dyoung     setifmtu, "mtu", &command_root.pb_parser);
    173  1.187    dyoung 
    174  1.187    dyoung struct pinteger parse_prefixlen = PINTEGER_INITIALIZER(&parse_prefixlen,
    175  1.187    dyoung     "prefixlen", 10, setifprefixlen, "prefixlen", &command_root.pb_parser);
    176  1.187    dyoung 
    177  1.187    dyoung struct pinteger parse_preference = PINTEGER_INITIALIZER1(&parse_preference,
    178  1.187    dyoung     "preference", INT16_MIN, INT16_MAX, 10, NULL, "preference",
    179  1.187    dyoung     &command_root.pb_parser);
    180  1.187    dyoung 
    181  1.187    dyoung struct paddr parse_netmask = PADDR_INITIALIZER(&parse_netmask, "netmask",
    182  1.187    dyoung     setifnetmask, "dstormask", NULL, NULL, NULL, &command_root.pb_parser);
    183  1.187    dyoung 
    184  1.187    dyoung struct paddr parse_broadcast = PADDR_INITIALIZER(&parse_broadcast,
    185  1.187    dyoung     "broadcast address",
    186  1.187    dyoung     setifbroadaddr, "broadcast", NULL, NULL, NULL, &command_root.pb_parser);
    187  1.187    dyoung 
    188  1.198    dyoung static const struct kwinst misckw[] = {
    189  1.208    dyoung 	  {.k_word = "alias", .k_key = "alias", .k_deact = "alias",
    190  1.187    dyoung 	   .k_type = KW_T_BOOL, .k_neg = true,
    191  1.187    dyoung 	   .k_bool = true, .k_negbool = false,
    192  1.208    dyoung 	   .k_nextparser = &command_root.pb_parser}
    193  1.187    dyoung 	, {.k_word = "broadcast", .k_nextparser = &parse_broadcast.pa_parser}
    194  1.187    dyoung 	, {.k_word = "delete", .k_key = "alias", .k_deact = "alias",
    195  1.208    dyoung 	   .k_type = KW_T_BOOL, .k_bool = false,
    196  1.187    dyoung 	   .k_nextparser = &command_root.pb_parser}
    197  1.187    dyoung 	, {.k_word = "metric", .k_nextparser = &parse_metric.pi_parser}
    198  1.187    dyoung 	, {.k_word = "mtu", .k_nextparser = &parse_mtu.pi_parser}
    199  1.187    dyoung 	, {.k_word = "netmask", .k_nextparser = &parse_netmask.pa_parser}
    200  1.187    dyoung 	, {.k_word = "preference", .k_act = "address",
    201  1.187    dyoung 	   .k_nextparser = &parse_preference.pi_parser}
    202  1.187    dyoung 	, {.k_word = "prefixlen", .k_nextparser = &parse_prefixlen.pi_parser}
    203  1.187    dyoung 	, {.k_word = "trailers", .k_neg = true,
    204  1.187    dyoung 	   .k_exec = notrailers, .k_nextparser = &command_root.pb_parser}
    205    1.1       cgd };
    206    1.1       cgd 
    207  1.187    dyoung /* key: clonecmd */
    208  1.198    dyoung static const struct kwinst clonekw[] = {
    209  1.205    dyoung 	{.k_word = "create", .k_type = KW_T_INT, .k_int = SIOCIFCREATE,
    210  1.187    dyoung 	 .k_nextparser = &opt_silent_family.pb_parser},
    211  1.205    dyoung 	{.k_word = "destroy", .k_type = KW_T_INT, .k_int = SIOCIFDESTROY}
    212  1.187    dyoung };
    213   1.24   thorpej 
    214  1.208    dyoung static struct kwinst familykw[24];
    215    1.1       cgd 
    216  1.187    dyoung struct pterm cloneterm = PTERM_INITIALIZER(&cloneterm, "list cloners",
    217  1.187    dyoung     list_cloners, "none");
    218  1.187    dyoung 
    219  1.187    dyoung struct pterm no_cmds = PTERM_INITIALIZER(&no_cmds, "no commands", no_cmds_exec,
    220  1.187    dyoung     "none");
    221  1.187    dyoung 
    222  1.187    dyoung struct pkw family_only =
    223  1.187    dyoung     PKW_INITIALIZER(&family_only, "family-only", NULL, "af", familykw,
    224  1.187    dyoung 	__arraycount(familykw), &no_cmds.pt_parser);
    225  1.187    dyoung 
    226  1.187    dyoung struct paddr address = PADDR_INITIALIZER(&address,
    227  1.187    dyoung     "local address (address 1)",
    228  1.187    dyoung     setifaddr, "address", "netmask", NULL, "address", &command_root.pb_parser);
    229  1.187    dyoung 
    230  1.187    dyoung struct paddr dstormask = PADDR_INITIALIZER(&dstormask,
    231  1.187    dyoung     "destination/netmask (address 2)",
    232  1.187    dyoung     setifdstormask, "dstormask", NULL, "address", "dstormask",
    233  1.187    dyoung     &command_root.pb_parser);
    234  1.187    dyoung 
    235  1.187    dyoung struct paddr broadcast = PADDR_INITIALIZER(&broadcast,
    236  1.187    dyoung     "broadcast address (address 3)",
    237  1.187    dyoung     setifbroadaddr, "broadcast", NULL, "dstormask", "broadcast",
    238  1.187    dyoung     &command_root.pb_parser);
    239  1.187    dyoung 
    240  1.208    dyoung static SIMPLEQ_HEAD(, afswtch) aflist = SIMPLEQ_HEAD_INITIALIZER(aflist);
    241  1.208    dyoung 
    242  1.208    dyoung static SIMPLEQ_HEAD(, status_func) status_funcs =
    243  1.208    dyoung     SIMPLEQ_HEAD_INITIALIZER(status_funcs);
    244  1.208    dyoung static SIMPLEQ_HEAD(, statistics_func) statistics_funcs =
    245  1.208    dyoung     SIMPLEQ_HEAD_INITIALIZER(statistics_funcs);
    246  1.208    dyoung static SIMPLEQ_HEAD(, cmdloop_branch) cmdloop_branches =
    247  1.208    dyoung     SIMPLEQ_HEAD_INITIALIZER(cmdloop_branches);
    248  1.208    dyoung 
    249  1.187    dyoung struct branch opt_clone_brs[] = {
    250  1.187    dyoung 	  {.b_nextparser = &cloning.pk_parser}
    251  1.187    dyoung 	, {.b_nextparser = &opt_family.pb_parser}
    252  1.187    dyoung }, opt_silent_family_brs[] = {
    253  1.187    dyoung 	  {.b_nextparser = &silent_family.pk_parser}
    254  1.187    dyoung 	, {.b_nextparser = &command_root.pb_parser}
    255  1.187    dyoung }, opt_family_brs[] = {
    256  1.187    dyoung 	  {.b_nextparser = &family.pk_parser}
    257  1.187    dyoung 	, {.b_nextparser = &opt_command.pb_parser}
    258  1.187    dyoung }, command_root_brs[] = {
    259  1.208    dyoung 	  {.b_nextparser = &ifflags.pk_parser}
    260  1.187    dyoung 	, {.b_nextparser = &ifcaps.pk_parser}
    261  1.208    dyoung 	, {.b_nextparser = &kwmedia.pk_parser}
    262  1.187    dyoung 	, {.b_nextparser = &misc.pk_parser}
    263  1.187    dyoung 	, {.b_nextparser = &address.pa_parser}
    264  1.187    dyoung 	, {.b_nextparser = &dstormask.pa_parser}
    265  1.187    dyoung 	, {.b_nextparser = &broadcast.pa_parser}
    266  1.187    dyoung 	, {.b_nextparser = NULL}
    267  1.187    dyoung }, opt_command_brs[] = {
    268  1.187    dyoung 	  {.b_nextparser = &no_cmds.pt_parser}
    269  1.187    dyoung 	, {.b_nextparser = &command_root.pb_parser}
    270  1.187    dyoung };
    271  1.187    dyoung 
    272  1.187    dyoung struct branch opt_family_only_brs[] = {
    273  1.187    dyoung 	  {.b_nextparser = &no_cmds.pt_parser}
    274  1.187    dyoung 	, {.b_nextparser = &family_only.pk_parser}
    275  1.187    dyoung };
    276  1.187    dyoung struct pbranch opt_family_only = PBRANCH_INITIALIZER(&opt_family_only,
    277  1.187    dyoung     "opt-family-only", opt_family_only_brs,
    278  1.187    dyoung     __arraycount(opt_family_only_brs), true);
    279  1.187    dyoung struct pbranch opt_command = PBRANCH_INITIALIZER(&opt_command,
    280  1.187    dyoung     "optional command",
    281  1.187    dyoung     opt_command_brs, __arraycount(opt_command_brs), true);
    282  1.187    dyoung 
    283  1.187    dyoung struct pbranch command_root = PBRANCH_INITIALIZER(&command_root,
    284  1.187    dyoung     "command-root", command_root_brs, __arraycount(command_root_brs), true);
    285  1.187    dyoung 
    286  1.187    dyoung struct piface iface_opt_family_only =
    287  1.187    dyoung     PIFACE_INITIALIZER(&iface_opt_family_only, "iface-opt-family-only",
    288  1.187    dyoung     NULL, "if", &opt_family_only.pb_parser);
    289  1.187    dyoung 
    290  1.187    dyoung struct pkw family = PKW_INITIALIZER(&family, "family", NULL, "af",
    291  1.187    dyoung     familykw, __arraycount(familykw), &opt_command.pb_parser);
    292  1.187    dyoung 
    293  1.187    dyoung struct pkw silent_family = PKW_INITIALIZER(&silent_family, "silent family",
    294  1.187    dyoung     NULL, "af", familykw, __arraycount(familykw), &command_root.pb_parser);
    295  1.187    dyoung 
    296  1.208    dyoung struct pkw *family_users[] = {&family_only, &family, &silent_family};
    297  1.208    dyoung 
    298  1.187    dyoung struct pkw ifcaps = PKW_INITIALIZER(&ifcaps, "ifcaps", setifcaps,
    299  1.187    dyoung     "ifcap", ifcapskw, __arraycount(ifcapskw), &command_root.pb_parser);
    300  1.187    dyoung 
    301  1.187    dyoung struct pkw ifflags = PKW_INITIALIZER(&ifflags, "ifflags", setifflags,
    302  1.187    dyoung     "ifflag", ifflagskw, __arraycount(ifflagskw), &command_root.pb_parser);
    303  1.187    dyoung 
    304  1.187    dyoung struct pkw cloning = PKW_INITIALIZER(&cloning, "cloning", clone_command,
    305  1.187    dyoung     "clonecmd", clonekw, __arraycount(clonekw), NULL);
    306  1.187    dyoung 
    307  1.187    dyoung struct pkw misc = PKW_INITIALIZER(&misc, "misc", NULL, NULL,
    308  1.187    dyoung     misckw, __arraycount(misckw), NULL);
    309  1.187    dyoung 
    310  1.187    dyoung struct pbranch opt_clone = PBRANCH_INITIALIZER(&opt_clone,
    311  1.187    dyoung     "opt-clone", opt_clone_brs, __arraycount(opt_clone_brs), true);
    312  1.187    dyoung 
    313  1.187    dyoung struct pbranch opt_silent_family = PBRANCH_INITIALIZER(&opt_silent_family,
    314  1.187    dyoung     "optional silent family", opt_silent_family_brs,
    315  1.187    dyoung     __arraycount(opt_silent_family_brs), true);
    316  1.187    dyoung 
    317  1.187    dyoung struct pbranch opt_family = PBRANCH_INITIALIZER(&opt_family,
    318  1.187    dyoung     "opt-family", opt_family_brs, __arraycount(opt_family_brs), true);
    319  1.187    dyoung 
    320  1.187    dyoung struct piface iface_start = PIFACE_INITIALIZER(&iface_start,
    321  1.187    dyoung     "iface-opt-family", NULL, "if", &opt_clone.pb_parser);
    322  1.187    dyoung 
    323  1.187    dyoung struct piface iface_only = PIFACE_INITIALIZER(&iface_only, "iface",
    324  1.187    dyoung     media_status_exec, "if", NULL);
    325  1.187    dyoung 
    326  1.208    dyoung static int
    327  1.208    dyoung check_flag(char *flags, int flag)
    328  1.208    dyoung {
    329  1.208    dyoung 	if (flags != NULL && strchr(flags, flag) != NULL) {
    330  1.208    dyoung 		errno = EEXIST;
    331  1.208    dyoung 		return -1;
    332  1.208    dyoung 	}
    333  1.208    dyoung 
    334  1.208    dyoung 	if (flag >= '0' && flag <= '9')
    335  1.208    dyoung 		return 0;
    336  1.208    dyoung 	if (flag >= 'a' && flag <= 'z')
    337  1.208    dyoung 		return 0;
    338  1.208    dyoung 	if (flag >= 'A' && flag <= 'Z')
    339  1.208    dyoung 		return 0;
    340  1.208    dyoung 
    341  1.208    dyoung 	errno = EINVAL;
    342  1.208    dyoung 	return -1;
    343  1.208    dyoung }
    344  1.208    dyoung 
    345  1.208    dyoung void
    346  1.208    dyoung cmdloop_branch_init(cmdloop_branch_t *b, struct parser *p)
    347  1.208    dyoung {
    348  1.208    dyoung 	b->b_parser = p;
    349  1.208    dyoung }
    350  1.208    dyoung 
    351  1.208    dyoung void
    352  1.208    dyoung statistics_func_init(statistics_func_t *f, statistics_cb_t func)
    353  1.208    dyoung {
    354  1.208    dyoung 	f->f_func = func;
    355  1.208    dyoung }
    356  1.208    dyoung 
    357  1.208    dyoung void
    358  1.208    dyoung status_func_init(status_func_t *f, status_cb_t func)
    359  1.208    dyoung {
    360  1.208    dyoung 	f->f_func = func;
    361  1.208    dyoung }
    362  1.208    dyoung 
    363  1.208    dyoung int
    364  1.208    dyoung register_cmdloop_branch(cmdloop_branch_t *b)
    365  1.208    dyoung {
    366  1.208    dyoung 	SIMPLEQ_INSERT_TAIL(&cmdloop_branches, b, b_next);
    367  1.208    dyoung 	return 0;
    368  1.208    dyoung }
    369  1.208    dyoung 
    370  1.208    dyoung int
    371  1.208    dyoung register_statistics(statistics_func_t *f)
    372  1.208    dyoung {
    373  1.208    dyoung 	SIMPLEQ_INSERT_TAIL(&statistics_funcs, f, f_next);
    374  1.208    dyoung 	return 0;
    375  1.208    dyoung }
    376  1.208    dyoung 
    377  1.208    dyoung int
    378  1.208    dyoung register_status(status_func_t *f)
    379  1.208    dyoung {
    380  1.208    dyoung 	SIMPLEQ_INSERT_TAIL(&status_funcs, f, f_next);
    381  1.208    dyoung 	return 0;
    382  1.208    dyoung }
    383  1.208    dyoung 
    384  1.208    dyoung int
    385  1.208    dyoung register_family(struct afswtch *af)
    386  1.208    dyoung {
    387  1.208    dyoung 	SIMPLEQ_INSERT_TAIL(&aflist, af, af_next);
    388  1.208    dyoung 	return 0;
    389  1.208    dyoung }
    390  1.208    dyoung 
    391  1.208    dyoung int
    392  1.208    dyoung register_flag(int flag)
    393  1.208    dyoung {
    394  1.208    dyoung 	if (check_flag(gflags, flag) == -1)
    395  1.208    dyoung 		return -1;
    396  1.208    dyoung 
    397  1.208    dyoung 	if (strlen(gflags) + 1 >= sizeof(gflags)) {
    398  1.208    dyoung 		errno = ENOMEM;
    399  1.208    dyoung 		return -1;
    400  1.208    dyoung 	}
    401  1.208    dyoung 
    402  1.208    dyoung 	gflags[strlen(gflags)] = flag;
    403  1.208    dyoung 
    404  1.208    dyoung 	return 0;
    405  1.208    dyoung }
    406  1.208    dyoung 
    407  1.208    dyoung static int
    408  1.208    dyoung flag_index(int flag)
    409  1.208    dyoung {
    410  1.208    dyoung 	if (flag >= '0' && flag <= '9')
    411  1.208    dyoung 		return flag - '0';
    412  1.208    dyoung 	if (flag >= 'a' && flag <= 'z')
    413  1.208    dyoung 		return 10 + flag - 'a';
    414  1.208    dyoung 	if (flag >= 'A' && flag <= 'Z')
    415  1.208    dyoung 		return 10 + 26 + flag - 'a';
    416  1.208    dyoung 
    417  1.208    dyoung 	errno = EINVAL;
    418  1.208    dyoung 	return -1;
    419  1.208    dyoung }
    420  1.208    dyoung 
    421  1.208    dyoung static bool
    422  1.208    dyoung set_flag(int flag)
    423  1.208    dyoung {
    424  1.208    dyoung 	int idx;
    425  1.208    dyoung 
    426  1.208    dyoung 	if ((idx = flag_index(flag)) == -1)
    427  1.208    dyoung 		return false;
    428  1.208    dyoung 
    429  1.208    dyoung 	return gflagset[idx] = true;
    430  1.208    dyoung }
    431  1.208    dyoung 
    432  1.208    dyoung bool
    433  1.208    dyoung get_flag(int flag)
    434  1.208    dyoung {
    435  1.208    dyoung 	int idx;
    436  1.208    dyoung 
    437  1.208    dyoung 	if ((idx = flag_index(flag)) == -1)
    438  1.208    dyoung 		return false;
    439  1.208    dyoung 
    440  1.208    dyoung 	return gflagset[idx];
    441  1.208    dyoung }
    442  1.208    dyoung 
    443  1.187    dyoung static struct parser *
    444  1.187    dyoung init_parser(void)
    445  1.187    dyoung {
    446  1.208    dyoung 	cmdloop_branch_t *b;
    447  1.208    dyoung 
    448  1.187    dyoung 	if (parser_init(&iface_opt_family_only.pif_parser) == -1)
    449  1.187    dyoung 		err(EXIT_FAILURE, "parser_init(iface_opt_family_only)");
    450  1.187    dyoung 	if (parser_init(&iface_only.pif_parser) == -1)
    451  1.187    dyoung 		err(EXIT_FAILURE, "parser_init(iface_only)");
    452  1.187    dyoung 	if (parser_init(&iface_start.pif_parser) == -1)
    453  1.187    dyoung 		err(EXIT_FAILURE, "parser_init(iface_start)");
    454  1.208    dyoung 
    455  1.208    dyoung 	SIMPLEQ_FOREACH(b, &cmdloop_branches, b_next)
    456  1.208    dyoung 		pbranch_addbranch(&command_root, b->b_parser);
    457  1.187    dyoung 
    458  1.187    dyoung 	return &iface_start.pif_parser;
    459  1.187    dyoung }
    460  1.187    dyoung 
    461  1.187    dyoung static int
    462  1.187    dyoung no_cmds_exec(prop_dictionary_t env, prop_dictionary_t xenv)
    463  1.187    dyoung {
    464  1.187    dyoung 	const char *ifname;
    465  1.187    dyoung 	unsigned short ignore;
    466  1.187    dyoung 
    467  1.187    dyoung 	/* ifname == NULL is ok.  It indicates 'ifconfig -a'. */
    468  1.187    dyoung 	if ((ifname = getifname(env)) == NULL)
    469  1.187    dyoung 		;
    470  1.187    dyoung 	else if (getifflags(env, xenv, &ignore) == -1)
    471  1.187    dyoung 		err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
    472  1.187    dyoung 
    473  1.187    dyoung 	printall(ifname, env);
    474  1.187    dyoung 	exit(EXIT_SUCCESS);
    475  1.187    dyoung }
    476  1.187    dyoung 
    477  1.187    dyoung static int
    478  1.187    dyoung media_status_exec(prop_dictionary_t env, prop_dictionary_t xenv)
    479  1.187    dyoung {
    480  1.187    dyoung 	const char *ifname;
    481  1.187    dyoung 	unsigned short ignore;
    482  1.187    dyoung 
    483  1.187    dyoung 	/* ifname == NULL is ok.  It indicates 'ifconfig -a'. */
    484  1.187    dyoung 	if ((ifname = getifname(env)) == NULL)
    485  1.187    dyoung 		;
    486  1.187    dyoung 	else if (getifflags(env, xenv, &ignore) == -1)
    487  1.187    dyoung 		err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
    488  1.187    dyoung 
    489  1.187    dyoung 	exit(carrier(env));
    490  1.187    dyoung }
    491    1.1       cgd 
    492  1.192    dyoung static void
    493  1.201    dyoung do_setifcaps(prop_dictionary_t env)
    494  1.192    dyoung {
    495  1.192    dyoung 	struct ifcapreq ifcr;
    496  1.192    dyoung 	prop_data_t d;
    497  1.192    dyoung 
    498  1.193    dyoung 	d = (prop_data_t )prop_dictionary_get(env, "ifcaps");
    499  1.193    dyoung 	if (d == NULL)
    500  1.192    dyoung 		return;
    501  1.192    dyoung 
    502  1.193    dyoung 	assert(sizeof(ifcr) == prop_data_size(d));
    503  1.193    dyoung 
    504  1.192    dyoung 	memcpy(&ifcr, prop_data_data_nocopy(d), sizeof(ifcr));
    505  1.201    dyoung 	if (direct_ioctl(env, SIOCSIFCAP, &ifcr) == -1)
    506  1.192    dyoung 		err(EXIT_FAILURE, "SIOCSIFCAP");
    507  1.192    dyoung }
    508  1.192    dyoung 
    509   1.16       cgd int
    510  1.187    dyoung main(int argc, char **argv)
    511    1.1       cgd {
    512  1.187    dyoung 	const struct afswtch *afp;
    513  1.208    dyoung 	int af, s;
    514  1.208    dyoung 	bool aflag = false, Cflag = false;
    515  1.187    dyoung 	struct match match[32];
    516  1.187    dyoung 	size_t nmatch;
    517  1.187    dyoung 	struct parser *start;
    518  1.187    dyoung 	int ch, narg = 0, rc;
    519  1.187    dyoung 	prop_dictionary_t env, xenv;
    520  1.187    dyoung 	const char *ifname;
    521  1.187    dyoung 
    522  1.187    dyoung 	memset(match, 0, sizeof(match));
    523  1.187    dyoung 
    524  1.208    dyoung 	init_afs();
    525  1.208    dyoung 
    526  1.187    dyoung 	start = init_parser();
    527   1.24   thorpej 
    528   1.24   thorpej 	/* Parse command-line options */
    529  1.208    dyoung 	aflag = vflag = zflag = false;
    530  1.208    dyoung 	while ((ch = getopt(argc, argv, gflags)) != -1) {
    531   1.24   thorpej 		switch (ch) {
    532   1.49  christos 		case 'A':
    533  1.107    itojun 			warnx("-A is deprecated");
    534   1.49  christos 			break;
    535   1.49  christos 
    536   1.24   thorpej 		case 'a':
    537  1.208    dyoung 			aflag = true;
    538   1.24   thorpej 			break;
    539   1.24   thorpej 
    540   1.54  sommerfe 		case 'b':
    541  1.208    dyoung 			bflag = true;
    542   1.54  sommerfe 			break;
    543   1.54  sommerfe 
    544   1.87   thorpej 		case 'C':
    545  1.208    dyoung 			Cflag = true;
    546   1.87   thorpej 			break;
    547   1.87   thorpej 
    548   1.34     lukem 		case 'd':
    549  1.208    dyoung 			dflag = true;
    550   1.34     lukem 			break;
    551  1.169    rpaulo 		case 'h':
    552  1.208    dyoung 			hflag = true;
    553  1.169    rpaulo 			break;
    554   1.31   thorpej 		case 'l':
    555  1.208    dyoung 			lflag = true;
    556   1.24   thorpej 			break;
    557    1.1       cgd 
    558   1.54  sommerfe 		case 's':
    559  1.208    dyoung 			sflag = true;
    560   1.54  sommerfe 			break;
    561   1.54  sommerfe 
    562   1.34     lukem 		case 'u':
    563  1.208    dyoung 			uflag = true;
    564   1.34     lukem 			break;
    565   1.34     lukem 
    566  1.124      matt 		case 'v':
    567  1.208    dyoung 			vflag = true;
    568  1.124      matt 			break;
    569  1.124      matt 
    570  1.134     perry 		case 'z':
    571  1.208    dyoung 			zflag = true;
    572  1.134     perry 			break;
    573  1.134     perry 
    574   1.24   thorpej 		default:
    575  1.208    dyoung 			if (!set_flag(ch))
    576  1.208    dyoung 				usage();
    577  1.208    dyoung 			break;
    578   1.24   thorpej 		}
    579  1.187    dyoung 		switch (ch) {
    580  1.187    dyoung 		case 'a':
    581  1.187    dyoung 			start = &opt_family_only.pb_parser;
    582  1.187    dyoung 			break;
    583  1.187    dyoung 
    584  1.187    dyoung #ifdef INET6
    585  1.187    dyoung 		case 'L':
    586  1.187    dyoung #endif
    587  1.187    dyoung 		case 'm':
    588  1.187    dyoung 		case 'v':
    589  1.187    dyoung 		case 'z':
    590  1.187    dyoung 			if (start != &opt_family_only.pb_parser)
    591  1.187    dyoung 				start = &iface_opt_family_only.pif_parser;
    592  1.187    dyoung 			break;
    593  1.187    dyoung 		case 'C':
    594  1.187    dyoung 			start = &cloneterm.pt_parser;
    595  1.187    dyoung 			break;
    596  1.187    dyoung 		case 'l':
    597  1.187    dyoung 			start = &no_cmds.pt_parser;
    598  1.187    dyoung 			break;
    599  1.187    dyoung 		case 's':
    600  1.187    dyoung 			if (start != &no_cmds.pt_parser &&
    601  1.187    dyoung 			    start != &opt_family_only.pb_parser)
    602  1.187    dyoung 				start = &iface_only.pif_parser;
    603  1.187    dyoung 			break;
    604  1.187    dyoung 		default:
    605  1.187    dyoung 			break;
    606  1.187    dyoung 		}
    607    1.1       cgd 	}
    608   1.24   thorpej 	argc -= optind;
    609   1.24   thorpej 	argv += optind;
    610   1.24   thorpej 
    611   1.31   thorpej 	/*
    612   1.31   thorpej 	 * -l means "list all interfaces", and is mutally exclusive with
    613   1.31   thorpej 	 * all other flags/commands.
    614   1.31   thorpej 	 *
    615   1.87   thorpej 	 * -C means "list all names of cloners", and it mutually exclusive
    616   1.87   thorpej 	 * with all other flags/commands.
    617   1.87   thorpej 	 *
    618   1.31   thorpej 	 * -a means "print status of all interfaces".
    619   1.31   thorpej 	 */
    620  1.208    dyoung 	if ((lflag || Cflag) && (aflag || get_flag('m') || vflag || zflag))
    621   1.54  sommerfe 		usage();
    622   1.54  sommerfe #ifdef INET6
    623  1.208    dyoung 	if ((lflag || Cflag) && get_flag('L'))
    624   1.31   thorpej 		usage();
    625   1.54  sommerfe #endif
    626   1.87   thorpej 	if (lflag && Cflag)
    627   1.87   thorpej 		usage();
    628   1.24   thorpej 
    629  1.187    dyoung 	nmatch = __arraycount(match);
    630  1.187    dyoung 
    631  1.187    dyoung 	rc = parse(argc, argv, start, match, &nmatch, &narg);
    632  1.187    dyoung 	if (rc != 0)
    633   1.24   thorpej 		usage();
    634   1.24   thorpej 
    635  1.187    dyoung 	if ((xenv = prop_dictionary_create()) == NULL)
    636  1.187    dyoung 		err(EXIT_FAILURE, "%s: prop_dictionary_create", __func__);
    637  1.187    dyoung 
    638  1.187    dyoung 	if (matches_exec(match, xenv, nmatch) == -1)
    639  1.187    dyoung 		err(EXIT_FAILURE, "exec_matches");
    640   1.81   thorpej 
    641  1.187    dyoung 	argc -= narg;
    642  1.187    dyoung 	argv += narg;
    643   1.24   thorpej 
    644  1.187    dyoung 	env = (nmatch > 0) ? match[(int)nmatch - 1].m_env : NULL;
    645  1.187    dyoung 	if (env == NULL)
    646  1.187    dyoung 		env = xenv;
    647   1.52   thorpej 	else
    648  1.187    dyoung 		env = prop_dictionary_augment(env, xenv);
    649   1.24   thorpej 
    650  1.187    dyoung 	/* Process any media commands that may have been issued. */
    651  1.187    dyoung 	process_media_commands(env);
    652   1.24   thorpej 
    653  1.208    dyoung 	if ((af = getaf(env)) == -1)
    654  1.187    dyoung 		af = AF_INET;
    655   1.54  sommerfe 
    656  1.187    dyoung 	if ((s = getsock(af)) == -1)
    657  1.187    dyoung 		err(EXIT_FAILURE, "%s: getsock", __func__);
    658   1.51   thorpej 
    659  1.187    dyoung 	if ((ifname = getifname(env)) == NULL)
    660  1.187    dyoung 		err(EXIT_FAILURE, "%s: getifname", __func__);
    661   1.24   thorpej 
    662  1.187    dyoung 	if ((afp = lookup_af_bynum(af)) == NULL)
    663  1.187    dyoung 		errx(EXIT_FAILURE, "%s: lookup_af_bynum", __func__);
    664    1.1       cgd 
    665  1.208    dyoung 	assert(afp->af_addr_commit != NULL);
    666  1.208    dyoung 	(*afp->af_addr_commit)(env, xenv);
    667   1.21       gwr 
    668  1.187    dyoung 	do_setifpreference(env);
    669  1.201    dyoung 	do_setifcaps(env);
    670  1.108   thorpej 
    671  1.208    dyoung 	exit(EXIT_SUCCESS);
    672  1.208    dyoung }
    673   1.24   thorpej 
    674  1.208    dyoung static void
    675  1.208    dyoung init_afs(void)
    676  1.208    dyoung {
    677  1.208    dyoung 	int i;
    678  1.208    dyoung 	const struct afswtch *afp;
    679  1.208    dyoung 	struct kwinst kw = {.k_type = KW_T_INT};
    680  1.208    dyoung 
    681  1.208    dyoung 	SIMPLEQ_FOREACH(afp, &aflist, af_next) {
    682  1.208    dyoung 		kw.k_word = afp->af_name;
    683  1.208    dyoung 		kw.k_int = afp->af_af;
    684  1.208    dyoung 		for (i = 0; i < __arraycount(familykw); i++) {
    685  1.208    dyoung 			if (familykw[i].k_word == NULL) {
    686  1.208    dyoung 				familykw[i] = kw;
    687  1.208    dyoung 				break;
    688  1.208    dyoung 			}
    689  1.208    dyoung 		}
    690  1.208    dyoung 	}
    691   1.24   thorpej }
    692   1.24   thorpej 
    693  1.160   thorpej const struct afswtch *
    694  1.158   thorpej lookup_af_bynum(int afnum)
    695  1.158   thorpej {
    696  1.208    dyoung 	const struct afswtch *afp;
    697  1.158   thorpej 
    698  1.208    dyoung 	SIMPLEQ_FOREACH(afp, &aflist, af_next) {
    699  1.208    dyoung 		if (afp->af_af == afnum)
    700  1.208    dyoung 			break;
    701  1.208    dyoung 	}
    702  1.208    dyoung 	return afp;
    703  1.158   thorpej }
    704  1.158   thorpej 
    705   1.13   mycroft void
    706  1.187    dyoung printall(const char *ifname, prop_dictionary_t env0)
    707    1.5   deraadt {
    708   1.78    itojun 	struct ifaddrs *ifap, *ifa;
    709  1.187    dyoung 	struct ifreq ifr;
    710   1.94    itojun 	const struct sockaddr_dl *sdl = NULL;
    711  1.189    dyoung 	prop_dictionary_t env, oenv;
    712   1.78    itojun 	int idx;
    713   1.78    itojun 	char *p;
    714   1.78    itojun 
    715  1.187    dyoung 	if (env0 == NULL)
    716  1.187    dyoung 		env = prop_dictionary_create();
    717  1.187    dyoung 	else
    718  1.187    dyoung 		env = prop_dictionary_copy_mutable(env0);
    719  1.187    dyoung 
    720  1.189    dyoung 	oenv = prop_dictionary_create();
    721  1.189    dyoung 
    722  1.189    dyoung 	if (env == NULL || oenv == NULL)
    723  1.187    dyoung 		errx(EXIT_FAILURE, "%s: prop_dictionary_copy/create", __func__);
    724  1.187    dyoung 
    725   1.78    itojun 	if (getifaddrs(&ifap) != 0)
    726  1.120    atatat 		err(EXIT_FAILURE, "getifaddrs");
    727   1.78    itojun 	p = NULL;
    728   1.78    itojun 	idx = 0;
    729   1.78    itojun 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    730  1.187    dyoung 		memset(&ifr, 0, sizeof(ifr));
    731  1.187    dyoung 		estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
    732  1.187    dyoung 		if (sizeof(ifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
    733  1.187    dyoung 			memcpy(&ifr.ifr_addr, ifa->ifa_addr,
    734   1.78    itojun 			    ifa->ifa_addr->sa_len);
    735   1.78    itojun 		}
    736   1.78    itojun 
    737  1.187    dyoung 		if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
    738  1.107    itojun 			continue;
    739   1.78    itojun 		if (ifa->ifa_addr->sa_family == AF_LINK)
    740   1.78    itojun 			sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
    741   1.78    itojun 		if (p && strcmp(p, ifa->ifa_name) == 0)
    742   1.78    itojun 			continue;
    743  1.189    dyoung 		if (!prop_dictionary_set_cstring(env, "if", ifa->ifa_name))
    744  1.140    itojun 			continue;
    745   1.78    itojun 		p = ifa->ifa_name;
    746   1.78    itojun 
    747  1.135     lukem 		if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
    748   1.78    itojun 			continue;
    749   1.78    itojun 		if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
    750   1.78    itojun 			continue;
    751   1.78    itojun 		if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
    752   1.78    itojun 			continue;
    753   1.78    itojun 
    754  1.187    dyoung 		if (sflag && carrier(env))
    755   1.78    itojun 			continue;
    756   1.78    itojun 		idx++;
    757   1.78    itojun 		/*
    758   1.78    itojun 		 * Are we just listing the interfaces?
    759   1.78    itojun 		 */
    760   1.78    itojun 		if (lflag) {
    761   1.78    itojun 			if (idx > 1)
    762  1.147       dsl 				printf(" ");
    763  1.187    dyoung 			fputs(ifa->ifa_name, stdout);
    764   1.78    itojun 			continue;
    765   1.78    itojun 		}
    766   1.78    itojun 
    767  1.189    dyoung 		status(sdl, env, oenv);
    768  1.119     bjh21 		sdl = NULL;
    769   1.78    itojun 	}
    770   1.78    itojun 	if (lflag)
    771  1.147       dsl 		printf("\n");
    772  1.187    dyoung 	prop_object_release((prop_object_t)env);
    773  1.189    dyoung 	prop_object_release((prop_object_t)oenv);
    774   1.78    itojun 	freeifaddrs(ifap);
    775   1.87   thorpej }
    776   1.87   thorpej 
    777  1.208    dyoung static int
    778  1.187    dyoung list_cloners(prop_dictionary_t env, prop_dictionary_t oenv)
    779   1.87   thorpej {
    780   1.87   thorpej 	struct if_clonereq ifcr;
    781   1.87   thorpej 	char *cp, *buf;
    782  1.187    dyoung 	int idx, s;
    783   1.87   thorpej 
    784   1.87   thorpej 	memset(&ifcr, 0, sizeof(ifcr));
    785   1.87   thorpej 
    786  1.187    dyoung 	s = getsock(AF_INET);
    787   1.87   thorpej 
    788  1.120    atatat 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
    789  1.120    atatat 		err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
    790   1.87   thorpej 
    791   1.87   thorpej 	buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
    792   1.87   thorpej 	if (buf == NULL)
    793  1.120    atatat 		err(EXIT_FAILURE, "unable to allocate cloner name buffer");
    794   1.87   thorpej 
    795   1.87   thorpej 	ifcr.ifcr_count = ifcr.ifcr_total;
    796   1.87   thorpej 	ifcr.ifcr_buffer = buf;
    797   1.87   thorpej 
    798  1.120    atatat 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
    799  1.120    atatat 		err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
    800   1.87   thorpej 
    801   1.87   thorpej 	/*
    802   1.87   thorpej 	 * In case some disappeared in the mean time, clamp it down.
    803   1.87   thorpej 	 */
    804   1.87   thorpej 	if (ifcr.ifcr_count > ifcr.ifcr_total)
    805   1.87   thorpej 		ifcr.ifcr_count = ifcr.ifcr_total;
    806   1.87   thorpej 
    807   1.87   thorpej 	for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
    808   1.87   thorpej 		if (idx > 0)
    809  1.147       dsl 			printf(" ");
    810   1.87   thorpej 		printf("%s", cp);
    811   1.87   thorpej 	}
    812   1.87   thorpej 
    813  1.147       dsl 	printf("\n");
    814   1.87   thorpej 	free(buf);
    815  1.187    dyoung 	exit(EXIT_SUCCESS);
    816    1.5   deraadt }
    817    1.5   deraadt 
    818  1.187    dyoung static int
    819  1.187    dyoung clone_command(prop_dictionary_t env, prop_dictionary_t xenv)
    820   1.81   thorpej {
    821  1.188    dyoung 	int64_t cmd;
    822  1.187    dyoung 
    823  1.188    dyoung 	if (!prop_dictionary_get_int64(env, "clonecmd", &cmd)) {
    824  1.187    dyoung 		errno = ENOENT;
    825  1.187    dyoung 		return -1;
    826  1.187    dyoung 	}
    827  1.187    dyoung 
    828  1.201    dyoung 	if (indirect_ioctl(env, (unsigned long)cmd, NULL) == -1) {
    829  1.187    dyoung 		warn("%s", __func__);
    830  1.187    dyoung 		return -1;
    831  1.187    dyoung 	}
    832  1.187    dyoung 	return 0;
    833   1.81   thorpej }
    834   1.81   thorpej 
    835   1.81   thorpej /*ARGSUSED*/
    836  1.208    dyoung static int
    837  1.187    dyoung setifaddr(prop_dictionary_t env, prop_dictionary_t xenv)
    838   1.81   thorpej {
    839  1.187    dyoung 	const struct paddr_prefix *pfx0;
    840  1.187    dyoung 	struct paddr_prefix *pfx;
    841  1.187    dyoung 	prop_data_t d;
    842  1.208    dyoung 	int af;
    843  1.187    dyoung 
    844  1.187    dyoung 	if ((af = getaf(env)) == -1)
    845  1.187    dyoung 		af = AF_INET;
    846  1.187    dyoung 
    847  1.187    dyoung 	d = (prop_data_t)prop_dictionary_get(env, "address");
    848  1.187    dyoung 	assert(d != NULL);
    849  1.187    dyoung 	pfx0 = prop_data_data_nocopy(d);
    850   1.81   thorpej 
    851  1.194    dyoung 	if (pfx0->pfx_len >= 0) {
    852  1.187    dyoung 		pfx = prefixlen_to_mask(af, pfx0->pfx_len);
    853  1.187    dyoung 		if (pfx == NULL)
    854  1.187    dyoung 			err(EXIT_FAILURE, "prefixlen_to_mask");
    855  1.187    dyoung 		free(pfx);
    856  1.187    dyoung 	}
    857  1.187    dyoung 
    858  1.187    dyoung 	return 0;
    859    1.1       cgd }
    860    1.1       cgd 
    861  1.208    dyoung static int
    862  1.187    dyoung setifnetmask(prop_dictionary_t env, prop_dictionary_t xenv)
    863    1.1       cgd {
    864  1.187    dyoung 	const struct paddr_prefix *pfx;
    865  1.187    dyoung 	prop_data_t d;
    866  1.187    dyoung 
    867  1.187    dyoung 	d = (prop_data_t)prop_dictionary_get(env, "dstormask");
    868  1.187    dyoung 	assert(d != NULL);
    869  1.187    dyoung 	pfx = prop_data_data_nocopy(d);
    870  1.187    dyoung 
    871  1.187    dyoung 	if (!prop_dictionary_set(xenv, "netmask", (prop_object_t)d))
    872  1.187    dyoung 		return -1;
    873  1.187    dyoung 
    874  1.187    dyoung 	return 0;
    875    1.1       cgd }
    876    1.1       cgd 
    877  1.208    dyoung static int
    878  1.187    dyoung setifbroadaddr(prop_dictionary_t env, prop_dictionary_t xenv)
    879    1.1       cgd {
    880  1.187    dyoung 	const struct paddr_prefix *pfx;
    881  1.187    dyoung 	prop_data_t d;
    882  1.187    dyoung 	unsigned short flags;
    883  1.187    dyoung 
    884  1.187    dyoung 	if (getifflags(env, xenv, &flags) == -1)
    885  1.187    dyoung 		err(EXIT_FAILURE, "%s: getifflags", __func__);
    886  1.187    dyoung 
    887  1.187    dyoung 	if ((flags & IFF_BROADCAST) == 0)
    888  1.187    dyoung 		errx(EXIT_FAILURE, "not a broadcast interface");
    889  1.187    dyoung 
    890  1.187    dyoung 	d = (prop_data_t)prop_dictionary_get(env, "broadcast");
    891  1.187    dyoung 	assert(d != NULL);
    892  1.187    dyoung 	pfx = prop_data_data_nocopy(d);
    893  1.187    dyoung 
    894  1.187    dyoung 	if (!prop_dictionary_set(xenv, "broadcast", (prop_object_t)d))
    895  1.187    dyoung 		return -1;
    896  1.187    dyoung 
    897  1.187    dyoung 	return 0;
    898    1.1       cgd }
    899    1.1       cgd 
    900    1.1       cgd /*ARGSUSED*/
    901  1.208    dyoung static int
    902  1.187    dyoung notrailers(prop_dictionary_t env, prop_dictionary_t xenv)
    903   1.13   mycroft {
    904   1.34     lukem 	puts("Note: trailers are no longer sent, but always received");
    905  1.187    dyoung 	return 0;
    906   1.13   mycroft }
    907   1.13   mycroft 
    908   1.13   mycroft /*ARGSUSED*/
    909  1.208    dyoung static int
    910  1.187    dyoung setifdstormask(prop_dictionary_t env, prop_dictionary_t xenv)
    911    1.1       cgd {
    912  1.187    dyoung 	const char *key;
    913  1.187    dyoung 	const struct paddr_prefix *pfx;
    914  1.187    dyoung 	prop_data_t d;
    915  1.187    dyoung 	unsigned short flags;
    916  1.187    dyoung 
    917  1.187    dyoung 	if (getifflags(env, xenv, &flags) == -1)
    918  1.187    dyoung 		err(EXIT_FAILURE, "%s: getifflags", __func__);
    919  1.187    dyoung 
    920  1.187    dyoung 	d = (prop_data_t)prop_dictionary_get(env, "dstormask");
    921  1.187    dyoung 	assert(d != NULL);
    922  1.187    dyoung 	pfx = prop_data_data_nocopy(d);
    923  1.187    dyoung 
    924  1.187    dyoung 	if ((flags & IFF_BROADCAST) == 0) {
    925  1.187    dyoung 		key = "dst";
    926  1.187    dyoung 	} else {
    927  1.187    dyoung 		key = "netmask";
    928  1.187    dyoung 	}
    929  1.187    dyoung 
    930  1.187    dyoung 	if (!prop_dictionary_set(xenv, key, (prop_object_t)d))
    931  1.187    dyoung 		return -1;
    932  1.187    dyoung 
    933  1.187    dyoung 	return 0;
    934    1.1       cgd }
    935    1.1       cgd 
    936  1.208    dyoung static int
    937  1.187    dyoung setifflags(prop_dictionary_t env, prop_dictionary_t xenv)
    938    1.1       cgd {
    939  1.201    dyoung 	struct ifreq ifr;
    940  1.189    dyoung 	int64_t ifflag;
    941  1.189    dyoung 	bool rc;
    942  1.187    dyoung 
    943  1.189    dyoung 	rc = prop_dictionary_get_int64(env, "ifflag", &ifflag);
    944  1.189    dyoung 	assert(rc);
    945   1.79     enami 
    946  1.201    dyoung  	if (direct_ioctl(env, SIOCGIFFLAGS, &ifr) == -1)
    947  1.187    dyoung 		return -1;
    948  1.187    dyoung 
    949  1.187    dyoung 	if (ifflag < 0) {
    950  1.187    dyoung 		ifflag = -ifflag;
    951  1.201    dyoung 		ifr.ifr_flags &= ~ifflag;
    952    1.1       cgd 	} else
    953  1.201    dyoung 		ifr.ifr_flags |= ifflag;
    954  1.187    dyoung 
    955  1.201    dyoung 	if (direct_ioctl(env, SIOCSIFFLAGS, &ifr) == -1)
    956  1.187    dyoung 		return -1;
    957  1.187    dyoung 
    958  1.187    dyoung 	return 0;
    959  1.187    dyoung }
    960  1.187    dyoung 
    961  1.187    dyoung static int
    962  1.187    dyoung getifcaps(prop_dictionary_t env, prop_dictionary_t oenv, struct ifcapreq *oifcr)
    963  1.187    dyoung {
    964  1.187    dyoung 	bool rc;
    965  1.187    dyoung 	struct ifcapreq ifcr;
    966  1.187    dyoung 	const struct ifcapreq *tmpifcr;
    967  1.187    dyoung 	prop_data_t capdata;
    968  1.187    dyoung 
    969  1.187    dyoung 	capdata = (prop_data_t)prop_dictionary_get(env, "ifcaps");
    970  1.187    dyoung 
    971  1.187    dyoung 	if (capdata != NULL) {
    972  1.187    dyoung 		tmpifcr = prop_data_data_nocopy(capdata);
    973  1.187    dyoung 		*oifcr = *tmpifcr;
    974  1.187    dyoung 		return 0;
    975  1.187    dyoung 	}
    976  1.187    dyoung 
    977  1.201    dyoung 	(void)direct_ioctl(env, SIOCGIFCAP, &ifcr);
    978  1.187    dyoung 	*oifcr = ifcr;
    979  1.187    dyoung 
    980  1.187    dyoung 	capdata = prop_data_create_data(&ifcr, sizeof(ifcr));
    981  1.187    dyoung 
    982  1.187    dyoung 	rc = prop_dictionary_set(oenv, "ifcaps", capdata);
    983  1.187    dyoung 
    984  1.187    dyoung 	prop_object_release((prop_object_t)capdata);
    985  1.187    dyoung 
    986  1.190    dyoung 	return rc ? 0 : -1;
    987    1.1       cgd }
    988    1.1       cgd 
    989  1.187    dyoung static int
    990  1.187    dyoung setifcaps(prop_dictionary_t env, prop_dictionary_t oenv)
    991  1.108   thorpej {
    992  1.189    dyoung 	int64_t ifcap;
    993  1.189    dyoung 	int s;
    994  1.189    dyoung 	bool rc;
    995  1.187    dyoung 	prop_data_t capdata;
    996  1.187    dyoung 	struct ifcapreq ifcr;
    997  1.187    dyoung 
    998  1.187    dyoung 	s = getsock(AF_INET);
    999  1.187    dyoung 
   1000  1.189    dyoung 	rc = prop_dictionary_get_int64(env, "ifcap", &ifcap);
   1001  1.189    dyoung 	assert(rc);
   1002  1.189    dyoung 
   1003  1.187    dyoung 	if (getifcaps(env, oenv, &ifcr) == -1)
   1004  1.187    dyoung 		return -1;
   1005  1.187    dyoung 
   1006  1.187    dyoung 	if (ifcap < 0) {
   1007  1.187    dyoung 		ifcap = -ifcap;
   1008  1.187    dyoung 		ifcr.ifcr_capenable &= ~ifcap;
   1009  1.108   thorpej 	} else
   1010  1.187    dyoung 		ifcr.ifcr_capenable |= ifcap;
   1011  1.187    dyoung 
   1012  1.187    dyoung 	if ((capdata = prop_data_create_data(&ifcr, sizeof(ifcr))) == NULL)
   1013  1.187    dyoung 		return -1;
   1014  1.108   thorpej 
   1015  1.191    dyoung 	rc = prop_dictionary_set(oenv, "ifcaps", capdata);
   1016  1.190    dyoung 	prop_object_release((prop_object_t)capdata);
   1017  1.187    dyoung 
   1018  1.190    dyoung 	return rc ? 0 : -1;
   1019  1.108   thorpej }
   1020  1.108   thorpej 
   1021  1.187    dyoung static int
   1022  1.187    dyoung setifmetric(prop_dictionary_t env, prop_dictionary_t xenv)
   1023    1.1       cgd {
   1024  1.187    dyoung 	struct ifreq ifr;
   1025  1.189    dyoung 	bool rc;
   1026  1.189    dyoung 	int64_t metric;
   1027  1.187    dyoung 
   1028  1.189    dyoung 	rc = prop_dictionary_get_int64(env, "metric", &metric);
   1029  1.189    dyoung 	assert(rc);
   1030  1.125    itojun 
   1031  1.189    dyoung 	ifr.ifr_metric = metric;
   1032  1.201    dyoung 	if (direct_ioctl(env, SIOCSIFMETRIC, &ifr) == -1)
   1033   1.13   mycroft 		warn("SIOCSIFMETRIC");
   1034  1.187    dyoung 	return 0;
   1035    1.1       cgd }
   1036    1.1       cgd 
   1037  1.187    dyoung static void
   1038  1.187    dyoung do_setifpreference(prop_dictionary_t env)
   1039  1.178    dyoung {
   1040  1.178    dyoung 	struct if_addrprefreq ifap;
   1041  1.187    dyoung 	prop_data_t d;
   1042  1.187    dyoung 	const struct paddr_prefix *pfx;
   1043  1.187    dyoung 
   1044  1.189    dyoung 	memset(&ifap, 0, sizeof(ifap));
   1045  1.189    dyoung 
   1046  1.189    dyoung 	if (!prop_dictionary_get_int16(env, "preference",
   1047  1.189    dyoung 	    &ifap.ifap_preference))
   1048  1.187    dyoung 		return;
   1049  1.187    dyoung 
   1050  1.187    dyoung 	d = (prop_data_t)prop_dictionary_get(env, "address");
   1051  1.187    dyoung 	assert(d != NULL);
   1052  1.189    dyoung 
   1053  1.187    dyoung 	pfx = prop_data_data_nocopy(d);
   1054  1.187    dyoung 
   1055  1.187    dyoung 	memcpy(&ifap.ifap_addr, &pfx->pfx_addr,
   1056  1.187    dyoung 	    MIN(sizeof(ifap.ifap_addr), pfx->pfx_addr.sa_len));
   1057  1.201    dyoung 	if (direct_ioctl(env, SIOCSIFADDRPREF, &ifap) == -1)
   1058  1.178    dyoung 		warn("SIOCSIFADDRPREF");
   1059  1.178    dyoung }
   1060  1.178    dyoung 
   1061  1.187    dyoung static int
   1062  1.187    dyoung setifmtu(prop_dictionary_t env, prop_dictionary_t xenv)
   1063   1.33        is {
   1064  1.189    dyoung 	int64_t mtu;
   1065  1.189    dyoung 	bool rc;
   1066  1.187    dyoung 	struct ifreq ifr;
   1067  1.125    itojun 
   1068  1.189    dyoung 	rc = prop_dictionary_get_int64(env, "mtu", &mtu);
   1069  1.189    dyoung 	assert(rc);
   1070  1.187    dyoung 
   1071  1.189    dyoung 	ifr.ifr_mtu = mtu;
   1072  1.201    dyoung 	if (direct_ioctl(env, SIOCSIFMTU, &ifr) == -1)
   1073   1.33        is 		warn("SIOCSIFMTU");
   1074   1.33        is 
   1075  1.187    dyoung 	return 0;
   1076   1.88      onoe }
   1077   1.88      onoe 
   1078  1.208    dyoung static int
   1079  1.187    dyoung carrier(prop_dictionary_t env)
   1080   1.54  sommerfe {
   1081   1.54  sommerfe 	struct ifmediareq ifmr;
   1082  1.187    dyoung 
   1083  1.201    dyoung 	memset(&ifmr, 0, sizeof(ifmr));
   1084   1.54  sommerfe 
   1085  1.201    dyoung 	if (direct_ioctl(env, SIOCGIFMEDIA, &ifmr) == -1) {
   1086   1.54  sommerfe 		/*
   1087   1.54  sommerfe 		 * Interface doesn't support SIOC{G,S}IFMEDIA;
   1088   1.54  sommerfe 		 * assume ok.
   1089   1.54  sommerfe 		 */
   1090  1.187    dyoung 		return EXIT_SUCCESS;
   1091   1.54  sommerfe 	}
   1092   1.54  sommerfe 	if ((ifmr.ifm_status & IFM_AVALID) == 0) {
   1093   1.54  sommerfe 		/*
   1094   1.54  sommerfe 		 * Interface doesn't report media-valid status.
   1095   1.54  sommerfe 		 * assume ok.
   1096   1.54  sommerfe 		 */
   1097  1.187    dyoung 		return EXIT_SUCCESS;
   1098   1.54  sommerfe 	}
   1099   1.54  sommerfe 	/* otherwise, return ok for active, not-ok if not active. */
   1100  1.187    dyoung 	if (ifmr.ifm_status & IFM_ACTIVE)
   1101  1.187    dyoung 		return EXIT_SUCCESS;
   1102  1.187    dyoung 	else
   1103  1.187    dyoung 		return EXIT_FAILURE;
   1104   1.54  sommerfe }
   1105   1.54  sommerfe 
   1106  1.208    dyoung static void
   1107  1.208    dyoung print_plural(const char *prefix, uint64_t n, const char *unit)
   1108  1.208    dyoung {
   1109  1.208    dyoung 	printf("%s%" PRIu64 " %s%s", prefix, n, unit, (n == 1) ? "" : "s");
   1110  1.208    dyoung }
   1111   1.63   thorpej 
   1112  1.208    dyoung static void
   1113  1.208    dyoung print_human_bytes(bool humanize, uint64_t n)
   1114  1.208    dyoung {
   1115  1.208    dyoung 	char buf[5];
   1116  1.208    dyoung 
   1117  1.208    dyoung 	if (humanize) {
   1118  1.208    dyoung 		(void)humanize_number(buf, sizeof(buf),
   1119  1.208    dyoung 		    (int64_t)n, "", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
   1120  1.208    dyoung 		printf(", %s byte%s", buf, (atof(buf) == 1.0) ? "" : "s");
   1121  1.208    dyoung 	} else
   1122  1.208    dyoung 		print_plural(", ", n, "byte");
   1123  1.208    dyoung }
   1124   1.63   thorpej 
   1125    1.1       cgd /*
   1126    1.1       cgd  * Print the status of the interface.  If an address family was
   1127    1.1       cgd  * specified, show it and it only; otherwise, show them all.
   1128    1.1       cgd  */
   1129   1.16       cgd void
   1130  1.189    dyoung status(const struct sockaddr_dl *sdl, prop_dictionary_t env,
   1131  1.189    dyoung     prop_dictionary_t oenv)
   1132    1.1       cgd {
   1133  1.208    dyoung 	const struct if_data *ifi;
   1134  1.208    dyoung 	status_func_t *status_f;
   1135  1.208    dyoung 	statistics_func_t *statistics_f;
   1136  1.124      matt 	struct ifdatareq ifdr;
   1137  1.187    dyoung 	struct ifreq ifr;
   1138  1.119     bjh21 	char hbuf[NI_MAXHOST];
   1139  1.130  christos 	char fbuf[BUFSIZ];
   1140  1.187    dyoung 	int af, s;
   1141  1.187    dyoung 	const char *ifname;
   1142  1.187    dyoung 	struct ifcapreq ifcr;
   1143  1.187    dyoung 	unsigned short flags;
   1144  1.187    dyoung 	const struct afswtch *afp;
   1145  1.187    dyoung 
   1146  1.188    dyoung 	if ((af = getaf(env)) == -1) {
   1147  1.188    dyoung 		afp = NULL;
   1148  1.188    dyoung 		af = AF_UNSPEC;
   1149  1.188    dyoung 	} else
   1150  1.188    dyoung 		afp = lookup_af_bynum(af);
   1151  1.188    dyoung 
   1152  1.201    dyoung 	/* get out early if the family is unsupported by the kernel */
   1153  1.188    dyoung 	if ((s = getsock(af)) == -1)
   1154  1.187    dyoung 		err(EXIT_FAILURE, "%s: getsock", __func__);
   1155  1.187    dyoung 
   1156  1.189    dyoung 	if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
   1157  1.187    dyoung 		err(EXIT_FAILURE, "%s: getifinfo", __func__);
   1158    1.1       cgd 
   1159  1.130  christos 	(void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
   1160  1.187    dyoung 	printf("%s: flags=%s", ifname, &fbuf[2]);
   1161  1.187    dyoung 
   1162  1.187    dyoung 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
   1163  1.187    dyoung 	if (ioctl(s, SIOCGIFMETRIC, &ifr) == -1)
   1164  1.187    dyoung 		warn("SIOCGIFMETRIC %s", ifr.ifr_name);
   1165  1.187    dyoung 	else if (ifr.ifr_metric != 0)
   1166  1.187    dyoung 		printf(" metric %d", ifr.ifr_metric);
   1167  1.187    dyoung 
   1168  1.187    dyoung 	estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
   1169  1.187    dyoung 	if (ioctl(s, SIOCGIFMTU, &ifr) != -1 && ifr.ifr_mtu != 0)
   1170  1.187    dyoung 		printf(" mtu %d", ifr.ifr_mtu);
   1171  1.147       dsl 	printf("\n");
   1172  1.108   thorpej 
   1173  1.189    dyoung 	if (getifcaps(env, oenv, &ifcr) == -1)
   1174  1.187    dyoung 		err(EXIT_FAILURE, "%s: getifcaps", __func__);
   1175  1.187    dyoung 
   1176  1.187    dyoung 	if (ifcr.ifcr_capabilities != 0) {
   1177  1.130  christos 		(void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
   1178  1.187    dyoung 		    ifcr.ifcr_capabilities);
   1179  1.130  christos 		printf("\tcapabilities=%s\n", &fbuf[2]);
   1180  1.130  christos 		(void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
   1181  1.187    dyoung 		    ifcr.ifcr_capenable);
   1182  1.130  christos 		printf("\tenabled=%s\n", &fbuf[2]);
   1183  1.108   thorpej 	}
   1184   1.65   thorpej 
   1185  1.208    dyoung 	SIMPLEQ_FOREACH(status_f, &status_funcs, f_next)
   1186  1.208    dyoung 		(*status_f->f_func)(env, oenv);
   1187   1.65   thorpej 
   1188  1.119     bjh21 	if (sdl != NULL &&
   1189  1.150   xtraeme 	    getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len,
   1190  1.119     bjh21 		hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
   1191  1.119     bjh21 	    hbuf[0] != '\0')
   1192  1.119     bjh21 		printf("\taddress: %s\n", hbuf);
   1193   1.24   thorpej 
   1194  1.208    dyoung 	media_status(env, oenv);
   1195   1.24   thorpej 
   1196  1.134     perry 	if (!vflag && !zflag)
   1197  1.124      matt 		goto proto_status;
   1198  1.124      matt 
   1199  1.187    dyoung 	estrlcpy(ifdr.ifdr_name, ifname, sizeof(ifdr.ifdr_name));
   1200  1.124      matt 
   1201  1.208    dyoung 	if (ioctl(s, zflag ? SIOCZIFDATA:SIOCGIFDATA, &ifdr) == -1)
   1202  1.134     perry 		err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
   1203  1.169    rpaulo 
   1204  1.208    dyoung 	ifi = &ifdr.ifdr_data;
   1205  1.208    dyoung 
   1206  1.208    dyoung 	print_plural("\tinput: ", ifi->ifi_ipackets, "packet");
   1207  1.208    dyoung 	print_human_bytes(hflag, ifi->ifi_ibytes);
   1208  1.208    dyoung 	if (ifi->ifi_imcasts)
   1209  1.208    dyoung 		print_plural(", ", ifi->ifi_imcasts, "multicast");
   1210  1.208    dyoung 	if (ifi->ifi_ierrors)
   1211  1.208    dyoung 		print_plural(", ", ifi->ifi_ierrors, "error");
   1212  1.208    dyoung 	if (ifi->ifi_iqdrops)
   1213  1.208    dyoung 		print_plural(", ", ifi->ifi_iqdrops, "queue drop");
   1214  1.208    dyoung 	if (ifi->ifi_noproto)
   1215  1.208    dyoung 		printf(", %" PRIu64 " unknown protocol", ifi->ifi_noproto);
   1216  1.208    dyoung 	print_plural("\n\toutput: ", ifi->ifi_opackets, "packet");
   1217  1.208    dyoung 	print_human_bytes(hflag, ifi->ifi_obytes);
   1218  1.208    dyoung 	if (ifi->ifi_omcasts)
   1219  1.208    dyoung 		print_plural(", ", ifi->ifi_omcasts, "multicast");
   1220  1.208    dyoung 	if (ifi->ifi_oerrors)
   1221  1.208    dyoung 		print_plural(", ", ifi->ifi_oerrors, "error");
   1222  1.208    dyoung 	if (ifi->ifi_collisions)
   1223  1.208    dyoung 		print_plural(", ", ifi->ifi_collisions, "collision");
   1224  1.208    dyoung 	printf("\n");
   1225  1.124      matt 
   1226  1.208    dyoung 	SIMPLEQ_FOREACH(statistics_f, &statistics_funcs, f_next)
   1227  1.208    dyoung 		(*statistics_f->f_func)(env);
   1228  1.149    dyoung 
   1229   1.24   thorpej  proto_status:
   1230  1.187    dyoung 
   1231  1.187    dyoung 	if (afp != NULL)
   1232  1.189    dyoung 		(*afp->af_status)(env, oenv, true);
   1233  1.208    dyoung 	else SIMPLEQ_FOREACH(afp, &aflist, af_next)
   1234  1.189    dyoung 		(*afp->af_status)(env, oenv, false);
   1235   1.49  christos }
   1236   1.49  christos 
   1237  1.208    dyoung static int
   1238  1.187    dyoung setifprefixlen(prop_dictionary_t env, prop_dictionary_t xenv)
   1239   1.53    itojun {
   1240  1.189    dyoung 	bool rc;
   1241  1.189    dyoung 	int64_t plen;
   1242  1.187    dyoung 	int af;
   1243  1.187    dyoung 	struct paddr_prefix *pfx;
   1244  1.187    dyoung 	prop_data_t d;
   1245  1.187    dyoung 
   1246  1.187    dyoung 	if ((af = getaf(env)) == -1)
   1247  1.187    dyoung 		af = AF_INET;
   1248  1.187    dyoung 
   1249  1.189    dyoung 	rc = prop_dictionary_get_int64(env, "prefixlen", &plen);
   1250  1.189    dyoung 	assert(rc);
   1251  1.187    dyoung 
   1252  1.189    dyoung 	pfx = prefixlen_to_mask(af, plen);
   1253  1.187    dyoung 	if (pfx == NULL)
   1254  1.187    dyoung 		err(EXIT_FAILURE, "prefixlen_to_mask");
   1255  1.187    dyoung 
   1256  1.187    dyoung 	d = prop_data_create_data(pfx,
   1257  1.187    dyoung 	    offsetof(struct paddr_prefix, pfx_addr) + pfx->pfx_addr.sa_len);
   1258  1.187    dyoung 	if (d == NULL)
   1259  1.187    dyoung 		err(EXIT_FAILURE, "%s: prop_data_create_data", __func__);
   1260  1.187    dyoung 
   1261  1.187    dyoung 	if (!prop_dictionary_set(xenv, "netmask", (prop_object_t)d))
   1262  1.187    dyoung 		err(EXIT_FAILURE, "%s: prop_dictionary_set", __func__);
   1263  1.187    dyoung 
   1264  1.187    dyoung 	free(pfx);
   1265  1.187    dyoung 	return 0;
   1266   1.53    itojun }
   1267   1.53    itojun 
   1268   1.18     glass void
   1269  1.145       dsl usage(void)
   1270   1.18     glass {
   1271   1.99       cgd 	const char *progname = getprogname();
   1272   1.80   thorpej 
   1273   1.31   thorpej 	fprintf(stderr,
   1274  1.169    rpaulo 	    "usage: %s [-h] [-m] [-v] [-z] "
   1275   1.53    itojun #ifdef INET6
   1276  1.134     perry 		"[-L] "
   1277   1.53    itojun #endif
   1278   1.80   thorpej 		"interface\n"
   1279  1.105    itojun 		"\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
   1280  1.105    itojun 		"\t\t[ alias | -alias ] ]\n"
   1281  1.104    itojun 		"\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
   1282   1.93      onoe 		"\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
   1283  1.181  degroote 		"\t[ list scan ]\n"
   1284   1.93      onoe 		"\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
   1285  1.170    rpaulo 		"\t[ hidessid | -hidessid ] [ apbridge | -apbridge ]\n"
   1286   1.93      onoe 		"\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
   1287   1.80   thorpej 		"\t[ arp | -arp ]\n"
   1288   1.93      onoe 		"\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
   1289   1.93      onoe 		"[ instance minst ]\n"
   1290  1.178    dyoung 		"\t[ preference n ]\n"
   1291   1.89   thorpej 		"\t[ vlan n vlanif i ]\n"
   1292  1.154      yamt 		"\t[ agrport i ] [ -agrport i ]\n"
   1293  1.104    itojun 		"\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
   1294  1.127    itojun 		"\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
   1295   1.80   thorpej 		"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
   1296  1.169    rpaulo 		"       %s -a [-b] [-h] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
   1297  1.134     perry 		"       %s -l [-b] [-d] [-u] [-s]\n"
   1298  1.101  christos 		"       %s -C\n"
   1299   1.81   thorpej 		"       %s interface create\n"
   1300   1.81   thorpej 		"       %s interface destroy\n",
   1301  1.101  christos 		progname, progname, progname, progname, progname, progname);
   1302  1.187    dyoung 	exit(EXIT_FAILURE);
   1303    1.1       cgd }
   1304