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