Home | History | Annotate | Line # | Download | only in ifconfig
ifconfig.c revision 1.107
      1  1.107    itojun /*	$NetBSD: ifconfig.c,v 1.107 2001/04/28 04:11:10 itojun 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  * 3. All advertising materials mentioning features or use of this software
     20   1.24   thorpej  *    must display the following acknowledgement:
     21   1.41   thorpej  *	This product includes software developed by the NetBSD
     22   1.41   thorpej  *	Foundation, Inc. and its contributors.
     23   1.41   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.41   thorpej  *    contributors may be used to endorse or promote products derived
     25   1.41   thorpej  *    from this software without specific prior written permission.
     26   1.24   thorpej  *
     27   1.41   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.41   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.41   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.41   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.41   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.41   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.41   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.41   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.41   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.41   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.41   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38   1.24   thorpej  */
     39   1.19       cgd 
     40    1.1       cgd /*
     41   1.13   mycroft  * Copyright (c) 1983, 1993
     42   1.13   mycroft  *	The Regents of the University of California.  All rights reserved.
     43    1.1       cgd  *
     44    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     45    1.1       cgd  * modification, are permitted provided that the following conditions
     46    1.1       cgd  * are met:
     47    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     48    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     49    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     50    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     51    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     52    1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     53    1.1       cgd  *    must display the following acknowledgement:
     54    1.1       cgd  *	This product includes software developed by the University of
     55    1.1       cgd  *	California, Berkeley and its contributors.
     56    1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     57    1.1       cgd  *    may be used to endorse or promote products derived from this software
     58    1.1       cgd  *    without specific prior written permission.
     59    1.1       cgd  *
     60    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70    1.1       cgd  * SUCH DAMAGE.
     71    1.1       cgd  */
     72    1.1       cgd 
     73   1.39     lukem #include <sys/cdefs.h>
     74    1.1       cgd #ifndef lint
     75   1.39     lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     76   1.39     lukem 	The Regents of the University of California.  All rights reserved.\n");
     77    1.1       cgd #endif /* not lint */
     78    1.1       cgd 
     79    1.1       cgd #ifndef lint
     80   1.19       cgd #if 0
     81   1.19       cgd static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
     82   1.19       cgd #else
     83  1.107    itojun __RCSID("$NetBSD: ifconfig.c,v 1.107 2001/04/28 04:11:10 itojun Exp $");
     84   1.19       cgd #endif
     85    1.1       cgd #endif /* not lint */
     86    1.1       cgd 
     87    1.1       cgd #include <sys/param.h>
     88    1.1       cgd #include <sys/socket.h>
     89    1.1       cgd #include <sys/ioctl.h>
     90    1.1       cgd 
     91    1.1       cgd #include <net/if.h>
     92   1.30   thorpej #include <net/if_dl.h>
     93   1.24   thorpej #include <net/if_media.h>
     94   1.62    chopps #include <net/if_ether.h>
     95   1.62    chopps #include <net/if_ieee80211.h>
     96   1.89   thorpej #include <net/if_vlanvar.h>
     97    1.1       cgd #include <netinet/in.h>
     98   1.53    itojun #include <netinet/in_var.h>
     99   1.54  sommerfe #ifdef INET6
    100   1.53    itojun #include <netinet6/nd6.h>
    101   1.54  sommerfe #endif
    102    1.1       cgd #include <arpa/inet.h>
    103    1.1       cgd 
    104   1.32  christos #include <netatalk/at.h>
    105   1.32  christos 
    106    1.1       cgd #define	NSIP
    107    1.1       cgd #include <netns/ns.h>
    108    1.1       cgd #include <netns/ns_if.h>
    109   1.13   mycroft #include <netdb.h>
    110    1.1       cgd 
    111    1.1       cgd #define EON
    112    1.1       cgd #include <netiso/iso.h>
    113    1.1       cgd #include <netiso/iso_var.h>
    114    1.1       cgd #include <sys/protosw.h>
    115    1.1       cgd 
    116   1.13   mycroft #include <ctype.h>
    117   1.13   mycroft #include <err.h>
    118   1.13   mycroft #include <errno.h>
    119   1.50    chopps #include <stddef.h>
    120    1.1       cgd #include <stdio.h>
    121    1.1       cgd #include <stdlib.h>
    122    1.1       cgd #include <string.h>
    123   1.13   mycroft #include <unistd.h>
    124   1.78    itojun #include <ifaddrs.h>
    125    1.1       cgd 
    126   1.79     enami struct	ifreq		ifr, ridreq;
    127   1.35   thorpej struct	ifaliasreq	addreq __attribute__((aligned(4)));
    128   1.53    itojun #ifdef INET6
    129   1.53    itojun struct	in6_ifreq	ifr6;
    130   1.53    itojun struct	in6_ifreq	in6_ridreq;
    131   1.53    itojun struct	in6_aliasreq	in6_addreq __attribute__((aligned(4)));
    132   1.53    itojun #endif
    133   1.50    chopps struct	iso_ifreq	iso_ridreq;
    134    1.1       cgd struct	iso_aliasreq	iso_addreq;
    135    1.1       cgd struct	sockaddr_in	netmask;
    136   1.32  christos struct	netrange	at_nr;		/* AppleTalk net range */
    137   1.32  christos 
    138    1.1       cgd char	name[30];
    139   1.33        is int	flags, metric, mtu, setaddr, setipdst, doalias;
    140   1.16       cgd int	clearaddr, s;
    141   1.36     lukem int	newaddr = -1;
    142    1.1       cgd int	nsellength = 1;
    143   1.24   thorpej int	af;
    144  1.107    itojun int	aflag, bflag, Cflag, dflag, lflag, mflag, sflag, uflag;
    145   1.53    itojun #ifdef INET6
    146   1.53    itojun int	Lflag;
    147   1.53    itojun #endif
    148   1.38       mrg int	reset_if_flags;
    149   1.53    itojun int	explicit_prefix = 0;
    150   1.89   thorpej u_int	vlan_tag = (u_int)-1;
    151    1.1       cgd 
    152   1.80   thorpej void 	notealias __P((const char *, int));
    153   1.80   thorpej void 	notrailers __P((const char *, int));
    154   1.80   thorpej void 	setifaddr __P((const char *, int));
    155   1.80   thorpej void 	setifdstaddr __P((const char *, int));
    156   1.80   thorpej void 	setifflags __P((const char *, int));
    157   1.80   thorpej void 	setifbroadaddr __P((const char *, int));
    158   1.80   thorpej void 	setifipdst __P((const char *, int));
    159   1.80   thorpej void 	setifmetric __P((const char *, int));
    160   1.80   thorpej void 	setifmtu __P((const char *, int));
    161   1.80   thorpej void	setifnwid __P((const char *, int));
    162   1.88      onoe void	setifnwkey __P((const char *, int));
    163   1.92   thorpej void	setifpowersave __P((const char *, int));
    164   1.92   thorpej void	setifpowersavesleep __P((const char *, int));
    165   1.80   thorpej void 	setifnetmask __P((const char *, int));
    166   1.80   thorpej void	setifprefixlen __P((const char *, int));
    167   1.80   thorpej void 	setnsellength __P((const char *, int));
    168   1.80   thorpej void 	setsnpaoffset __P((const char *, int));
    169   1.80   thorpej void	setatrange __P((const char *, int));
    170   1.80   thorpej void	setatphase __P((const char *, int));
    171   1.80   thorpej void	settunnel __P((const char *, const char *));
    172   1.80   thorpej void	deletetunnel __P((const char *, int));
    173   1.80   thorpej #ifdef INET6
    174   1.80   thorpej void 	setia6flags __P((const char *, int));
    175   1.80   thorpej void	setia6pltime __P((const char *, int));
    176   1.80   thorpej void	setia6vltime __P((const char *, int));
    177   1.80   thorpej void	setia6lifetime __P((const char *, const char *));
    178   1.53    itojun #endif
    179   1.32  christos void	checkatrange __P ((struct sockaddr_at *));
    180   1.80   thorpej void	setmedia __P((const char *, int));
    181   1.80   thorpej void	setmediaopt __P((const char *, int));
    182   1.80   thorpej void	unsetmediaopt __P((const char *, int));
    183   1.80   thorpej void	setmediainst __P((const char *, int));
    184   1.81   thorpej void	clone_create __P((const char *, int));
    185   1.81   thorpej void	clone_destroy __P((const char *, int));
    186   1.32  christos void	fixnsel __P((struct sockaddr_iso *));
    187   1.89   thorpej void	setvlan __P((const char *, int));
    188   1.89   thorpej void	setvlanif __P((const char *, int));
    189   1.89   thorpej void	unsetvlanif __P((const char *, int));
    190   1.32  christos int	main __P((int, char *[]));
    191    1.1       cgd 
    192   1.45   thorpej /*
    193   1.45   thorpej  * Media stuff.  Whenever a media command is first performed, the
    194   1.45   thorpej  * currently select media is grabbed for this interface.  If `media'
    195   1.45   thorpej  * is given, the current media word is modifed.  `mediaopt' commands
    196   1.45   thorpej  * only modify the set and clear words.  They then operate on the
    197   1.45   thorpej  * current media word later.
    198   1.45   thorpej  */
    199   1.45   thorpej int	media_current;
    200   1.45   thorpej int	mediaopt_set;
    201   1.45   thorpej int	mediaopt_clear;
    202   1.45   thorpej 
    203   1.46   thorpej int	actions;			/* Actions performed */
    204   1.45   thorpej 
    205   1.46   thorpej #define	A_MEDIA		0x0001		/* media command */
    206   1.46   thorpej #define	A_MEDIAOPTSET	0x0002		/* mediaopt command */
    207   1.46   thorpej #define	A_MEDIAOPTCLR	0x0004		/* -mediaopt command */
    208   1.46   thorpej #define	A_MEDIAOPT	(A_MEDIAOPTSET|A_MEDIAOPTCLR)
    209   1.47   thorpej #define	A_MEDIAINST	0x0008		/* instance or inst command */
    210   1.45   thorpej 
    211    1.1       cgd #define	NEXTARG		0xffffff
    212   1.80   thorpej #define	NEXTARG2	0xfffffe
    213    1.1       cgd 
    214   1.80   thorpej const struct cmd {
    215   1.80   thorpej 	const char *c_name;
    216   1.80   thorpej 	int	c_parameter;	/* NEXTARG means next argv */
    217   1.46   thorpej 	int	c_action;	/* defered action */
    218   1.80   thorpej 	void	(*c_func) __P((const char *, int));
    219   1.80   thorpej 	void	(*c_func2) __P((const char *, const char *));
    220    1.1       cgd } cmds[] = {
    221   1.45   thorpej 	{ "up",		IFF_UP,		0,		setifflags } ,
    222   1.45   thorpej 	{ "down",	-IFF_UP,	0,		setifflags },
    223   1.45   thorpej 	{ "trailers",	-1,		0,		notrailers },
    224   1.45   thorpej 	{ "-trailers",	1,		0,		notrailers },
    225   1.45   thorpej 	{ "arp",	-IFF_NOARP,	0,		setifflags },
    226   1.45   thorpej 	{ "-arp",	IFF_NOARP,	0,		setifflags },
    227   1.45   thorpej 	{ "debug",	IFF_DEBUG,	0,		setifflags },
    228   1.45   thorpej 	{ "-debug",	-IFF_DEBUG,	0,		setifflags },
    229   1.45   thorpej 	{ "alias",	IFF_UP,		0,		notealias },
    230   1.45   thorpej 	{ "-alias",	-IFF_UP,	0,		notealias },
    231   1.45   thorpej 	{ "delete",	-IFF_UP,	0,		notealias },
    232    1.1       cgd #ifdef notdef
    233    1.1       cgd #define	EN_SWABIPS	0x1000
    234   1.45   thorpej 	{ "swabips",	EN_SWABIPS,	0,		setifflags },
    235   1.45   thorpej 	{ "-swabips",	-EN_SWABIPS,	0,		setifflags },
    236    1.1       cgd #endif
    237   1.45   thorpej 	{ "netmask",	NEXTARG,	0,		setifnetmask },
    238   1.45   thorpej 	{ "metric",	NEXTARG,	0,		setifmetric },
    239   1.45   thorpej 	{ "mtu",	NEXTARG,	0,		setifmtu },
    240   1.62    chopps 	{ "nwid",	NEXTARG,	0,		setifnwid },
    241   1.88      onoe 	{ "nwkey",	NEXTARG,	0,		setifnwkey },
    242   1.88      onoe 	{ "-nwkey",	-1,		0,		setifnwkey },
    243   1.92   thorpej 	{ "powersave",	1,		0,		setifpowersave },
    244   1.92   thorpej 	{ "-powersave",	0,		0,		setifpowersave },
    245   1.92   thorpej 	{ "powersavesleep", NEXTARG,	0,		setifpowersavesleep },
    246   1.45   thorpej 	{ "broadcast",	NEXTARG,	0,		setifbroadaddr },
    247   1.45   thorpej 	{ "ipdst",	NEXTARG,	0,		setifipdst },
    248   1.53    itojun 	{ "prefixlen",  NEXTARG,	0,		setifprefixlen},
    249   1.53    itojun #ifdef INET6
    250   1.53    itojun 	{ "anycast",	IN6_IFF_ANYCAST,	0,	setia6flags },
    251   1.53    itojun 	{ "-anycast",	-IN6_IFF_ANYCAST,	0,	setia6flags },
    252   1.53    itojun 	{ "tentative",	IN6_IFF_TENTATIVE,	0,	setia6flags },
    253   1.53    itojun 	{ "-tentative",	-IN6_IFF_TENTATIVE,	0,	setia6flags },
    254  1.104    itojun 	{ "deprecated",	IN6_IFF_DEPRECATED,	0,	setia6flags },
    255  1.104    itojun 	{ "-deprecated", -IN6_IFF_DEPRECATED,	0,	setia6flags },
    256   1.53    itojun 	{ "pltime",	NEXTARG,	0,		setia6pltime },
    257   1.53    itojun 	{ "vltime",	NEXTARG,	0,		setia6vltime },
    258   1.53    itojun #endif /*INET6*/
    259   1.21       gwr #ifndef INET_ONLY
    260   1.45   thorpej 	{ "range",	NEXTARG,	0,		setatrange },
    261   1.45   thorpej 	{ "phase",	NEXTARG,	0,		setatphase },
    262   1.45   thorpej 	{ "snpaoffset",	NEXTARG,	0,		setsnpaoffset },
    263   1.45   thorpej 	{ "nsellength",	NEXTARG,	0,		setnsellength },
    264   1.21       gwr #endif	/* INET_ONLY */
    265   1.80   thorpej 	{ "tunnel",	NEXTARG2,	0,		NULL,
    266   1.80   thorpej 							settunnel } ,
    267   1.80   thorpej 	{ "deletetunnel", 0,		0,		deletetunnel },
    268   1.89   thorpej 	{ "vlan",	NEXTARG,	0,		setvlan } ,
    269   1.89   thorpej 	{ "vlanif",	NEXTARG,	0,		setvlanif } ,
    270   1.89   thorpej 	{ "-vlanif",	0,		0,		unsetvlanif } ,
    271   1.89   thorpej 	{ "deletetunnel", 0,		0,		deletetunnel },
    272   1.81   thorpej #if 0
    273   1.81   thorpej 	/* XXX `create' special-cased below */
    274   1.81   thorpej 	{ "create",	0,		0,		clone_create } ,
    275   1.81   thorpej #endif
    276   1.81   thorpej 	{ "destroy",	0,		0,		clone_destroy } ,
    277   1.45   thorpej 	{ "link0",	IFF_LINK0,	0,		setifflags } ,
    278   1.45   thorpej 	{ "-link0",	-IFF_LINK0,	0,		setifflags } ,
    279   1.45   thorpej 	{ "link1",	IFF_LINK1,	0,		setifflags } ,
    280   1.45   thorpej 	{ "-link1",	-IFF_LINK1,	0,		setifflags } ,
    281   1.45   thorpej 	{ "link2",	IFF_LINK2,	0,		setifflags } ,
    282   1.45   thorpej 	{ "-link2",	-IFF_LINK2,	0,		setifflags } ,
    283   1.46   thorpej 	{ "media",	NEXTARG,	A_MEDIA,	setmedia },
    284   1.46   thorpej 	{ "mediaopt",	NEXTARG,	A_MEDIAOPTSET,	setmediaopt },
    285   1.46   thorpej 	{ "-mediaopt",	NEXTARG,	A_MEDIAOPTCLR,	unsetmediaopt },
    286   1.47   thorpej 	{ "instance",	NEXTARG,	A_MEDIAINST,	setmediainst },
    287   1.47   thorpej 	{ "inst",	NEXTARG,	A_MEDIAINST,	setmediainst },
    288   1.45   thorpej 	{ 0,		0,		0,		setifaddr },
    289   1.45   thorpej 	{ 0,		0,		0,		setifdstaddr },
    290    1.1       cgd };
    291    1.1       cgd 
    292   1.32  christos void 	adjust_nsellength __P((void));
    293   1.16       cgd int	getinfo __P((struct ifreq *));
    294   1.54  sommerfe int	carrier __P((void));
    295   1.16       cgd void	getsock __P((int));
    296  1.107    itojun void	printall __P((const char *));
    297   1.87   thorpej void	list_cloners __P((void));
    298   1.80   thorpej void 	printb __P((const char *, unsigned short, const char *));
    299   1.53    itojun int	prefix __P((void *, int));
    300   1.30   thorpej void 	status __P((const u_int8_t *, int));
    301   1.32  christos void 	usage __P((void));
    302   1.88      onoe const char *get_string __P((const char *, const char *, u_int8_t *, int *));
    303   1.88      onoe void	print_string __P((const u_int8_t *, int));
    304   1.53    itojun char	*sec2str __P((time_t));
    305   1.16       cgd 
    306   1.42   thorpej const char *get_media_type_string __P((int));
    307   1.42   thorpej const char *get_media_subtype_string __P((int));
    308   1.42   thorpej int	get_media_subtype __P((int, const char *));
    309   1.42   thorpej int	get_media_options __P((int, const char *));
    310   1.42   thorpej int	lookup_media_word __P((struct ifmedia_description *, int,
    311   1.42   thorpej 	    const char *));
    312   1.44   thorpej void	print_media_word __P((int, int, int));
    313   1.45   thorpej void	process_media_commands __P((void));
    314   1.45   thorpej void	init_current_media __P((void));
    315   1.24   thorpej 
    316    1.1       cgd /*
    317   1.13   mycroft  * XNS support liberally adapted from code written at the University of
    318   1.13   mycroft  * Maryland principally by James O'Toole and Chris Torek.
    319    1.1       cgd  */
    320   1.49  christos void	in_alias __P((struct ifreq *));
    321   1.16       cgd void	in_status __P((int));
    322   1.80   thorpej void 	in_getaddr __P((const char *, int));
    323   1.95    itojun void 	in_getprefix __P((const char *, int));
    324   1.53    itojun #ifdef INET6
    325   1.59    itojun void	in6_fillscopeid __P((struct sockaddr_in6 *sin6));
    326   1.53    itojun void	in6_alias __P((struct in6_ifreq *));
    327   1.53    itojun void	in6_status __P((int));
    328   1.80   thorpej void 	in6_getaddr __P((const char *, int));
    329   1.80   thorpej void 	in6_getprefix __P((const char *, int));
    330   1.53    itojun #endif
    331   1.32  christos void	at_status __P((int));
    332   1.80   thorpej void	at_getaddr __P((const char *, int));
    333   1.16       cgd void 	xns_status __P((int));
    334   1.80   thorpej void 	xns_getaddr __P((const char *, int));
    335   1.16       cgd void 	iso_status __P((int));
    336   1.80   thorpej void 	iso_getaddr __P((const char *, int));
    337   1.80   thorpej 
    338   1.62    chopps void	ieee80211_status __P((void));
    339   1.80   thorpej void	tunnel_status __P((void));
    340   1.89   thorpej void	vlan_status __P((void));
    341    1.1       cgd 
    342    1.1       cgd /* Known address families */
    343    1.1       cgd struct afswtch {
    344   1.80   thorpej 	const char *af_name;
    345    1.1       cgd 	short af_af;
    346   1.32  christos 	void (*af_status) __P((int));
    347   1.80   thorpej 	void (*af_getaddr) __P((const char *, int));
    348   1.80   thorpej 	void (*af_getprefix) __P((const char *, int));
    349   1.15       cgd 	u_long af_difaddr;
    350   1.15       cgd 	u_long af_aifaddr;
    351   1.76     enami 	u_long af_gifaddr;
    352    1.1       cgd 	caddr_t af_ridreq;
    353    1.1       cgd 	caddr_t af_addreq;
    354    1.1       cgd } afs[] = {
    355    1.1       cgd #define C(x) ((caddr_t) &x)
    356   1.95    itojun 	{ "inet", AF_INET, in_status, in_getaddr, in_getprefix,
    357   1.76     enami 	     SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(ridreq), C(addreq) },
    358   1.53    itojun #ifdef INET6
    359   1.53    itojun 	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
    360   1.76     enami 	     SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
    361   1.76     enami 	     /*
    362   1.76     enami 	      * Deleting the first address before setting new one is
    363   1.76     enami 	      * not prefered way in this protocol.
    364   1.76     enami 	      */
    365   1.76     enami 	     0,
    366   1.76     enami 	     C(in6_ridreq), C(in6_addreq) },
    367   1.53    itojun #endif
    368   1.21       gwr #ifndef INET_ONLY	/* small version, for boot media */
    369   1.53    itojun 	{ "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
    370   1.76     enami 	     SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(addreq), C(addreq) },
    371   1.53    itojun 	{ "ns", AF_NS, xns_status, xns_getaddr, NULL,
    372   1.76     enami 	     SIOCDIFADDR, SIOCAIFADDR, SIOCGIFADDR, C(ridreq), C(addreq) },
    373   1.53    itojun 	{ "iso", AF_ISO, iso_status, iso_getaddr, NULL,
    374   1.76     enami 	     SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, SIOCGIFADDR_ISO,
    375   1.76     enami 	     C(iso_ridreq), C(iso_addreq) },
    376   1.21       gwr #endif	/* INET_ONLY */
    377    1.1       cgd 	{ 0,	0,	    0,		0 }
    378    1.1       cgd };
    379    1.1       cgd 
    380    1.1       cgd struct afswtch *afp;	/*the address family being set or asked about*/
    381    1.1       cgd 
    382   1.24   thorpej struct afswtch *lookup_af __P((const char *));
    383   1.24   thorpej 
    384   1.16       cgd int
    385    1.1       cgd main(argc, argv)
    386    1.1       cgd 	int argc;
    387    1.1       cgd 	char *argv[];
    388    1.1       cgd {
    389   1.79     enami 	struct ifreq ifreq;
    390   1.49  christos 	int ch;
    391   1.24   thorpej 
    392   1.24   thorpej 	/* Parse command-line options */
    393   1.24   thorpej 	aflag = mflag = 0;
    394   1.87   thorpej 	while ((ch = getopt(argc, argv, "AabCdlmsu"
    395   1.53    itojun #ifdef INET6
    396   1.53    itojun 					"L"
    397   1.53    itojun #endif
    398   1.53    itojun 			)) != -1) {
    399   1.24   thorpej 		switch (ch) {
    400   1.49  christos 		case 'A':
    401  1.107    itojun 			warnx("-A is deprecated");
    402   1.49  christos 			break;
    403   1.49  christos 
    404   1.24   thorpej 		case 'a':
    405   1.24   thorpej 			aflag = 1;
    406   1.24   thorpej 			break;
    407   1.24   thorpej 
    408   1.54  sommerfe 		case 'b':
    409   1.54  sommerfe 			bflag = 1;
    410   1.54  sommerfe 			break;
    411   1.54  sommerfe 
    412   1.87   thorpej 
    413   1.87   thorpej 		case 'C':
    414   1.87   thorpej 			Cflag = 1;
    415   1.87   thorpej 			break;
    416   1.87   thorpej 
    417   1.34     lukem 		case 'd':
    418   1.34     lukem 			dflag = 1;
    419   1.34     lukem 			break;
    420   1.34     lukem 
    421   1.53    itojun #ifdef INET6
    422   1.53    itojun 		case 'L':
    423   1.53    itojun 			Lflag = 1;
    424   1.53    itojun 			break;
    425   1.53    itojun #endif
    426   1.53    itojun 
    427   1.31   thorpej 		case 'l':
    428   1.31   thorpej 			lflag = 1;
    429   1.31   thorpej 			break;
    430   1.31   thorpej 
    431   1.24   thorpej 		case 'm':
    432   1.24   thorpej 			mflag = 1;
    433   1.24   thorpej 			break;
    434    1.1       cgd 
    435   1.54  sommerfe 		case 's':
    436   1.54  sommerfe 			sflag = 1;
    437   1.54  sommerfe 			break;
    438   1.54  sommerfe 
    439   1.34     lukem 		case 'u':
    440   1.34     lukem 			uflag = 1;
    441   1.34     lukem 			break;
    442   1.34     lukem 
    443   1.54  sommerfe 
    444   1.24   thorpej 		default:
    445   1.24   thorpej 			usage();
    446   1.24   thorpej 			/* NOTREACHED */
    447   1.24   thorpej 		}
    448    1.1       cgd 	}
    449   1.24   thorpej 	argc -= optind;
    450   1.24   thorpej 	argv += optind;
    451   1.24   thorpej 
    452   1.31   thorpej 	/*
    453   1.31   thorpej 	 * -l means "list all interfaces", and is mutally exclusive with
    454   1.31   thorpej 	 * all other flags/commands.
    455   1.31   thorpej 	 *
    456   1.87   thorpej 	 * -C means "list all names of cloners", and it mutually exclusive
    457   1.87   thorpej 	 * with all other flags/commands.
    458   1.87   thorpej 	 *
    459   1.31   thorpej 	 * -a means "print status of all interfaces".
    460   1.31   thorpej 	 */
    461  1.107    itojun 	if ((lflag || Cflag) && (aflag || mflag || argc))
    462   1.54  sommerfe 		usage();
    463   1.54  sommerfe #ifdef INET6
    464   1.87   thorpej 	if ((lflag || Cflag) && Lflag)
    465   1.31   thorpej 		usage();
    466   1.54  sommerfe #endif
    467   1.87   thorpej 	if (lflag && Cflag)
    468   1.87   thorpej 		usage();
    469   1.87   thorpej 	if (Cflag) {
    470   1.87   thorpej 		if (argc)
    471   1.87   thorpej 			usage();
    472   1.87   thorpej 		list_cloners();
    473   1.87   thorpej 		exit(0);
    474   1.87   thorpej 	}
    475   1.31   thorpej 	if (aflag || lflag) {
    476   1.24   thorpej 		if (argc > 1)
    477   1.18     glass 			usage();
    478   1.24   thorpej 		else if (argc == 1) {
    479   1.24   thorpej 			afp = lookup_af(argv[0]);
    480   1.24   thorpej 			if (afp == NULL)
    481   1.24   thorpej 				usage();
    482   1.32  christos 		}
    483   1.32  christos 		if (afp)
    484   1.32  christos 			af = ifr.ifr_addr.sa_family = afp->af_af;
    485   1.32  christos 		else
    486   1.32  christos 			af = ifr.ifr_addr.sa_family = afs[0].af_af;
    487  1.107    itojun 		printall(NULL);
    488    1.5   deraadt 		exit(0);
    489    1.5   deraadt 	}
    490   1.24   thorpej 
    491   1.24   thorpej 	/* Make sure there's an interface name. */
    492   1.24   thorpej 	if (argc < 1)
    493   1.24   thorpej 		usage();
    494   1.32  christos 	(void) strncpy(name, argv[0], sizeof(name));
    495   1.24   thorpej 	argc--; argv++;
    496   1.24   thorpej 
    497   1.81   thorpej 	/*
    498   1.81   thorpej 	 * NOTE:  We must special-case the `create' command right
    499   1.81   thorpej 	 * here as we would otherwise fail in getinfo().
    500   1.81   thorpej 	 */
    501   1.81   thorpej 	if (argc > 0 && strcmp(argv[0], "create") == 0) {
    502   1.81   thorpej 		clone_create(argv[0], 0);
    503   1.81   thorpej 		argc--, argv++;
    504   1.81   thorpej 		if (argc == 0)
    505   1.81   thorpej 			exit(0);
    506   1.81   thorpej 	}
    507   1.81   thorpej 
    508   1.24   thorpej 	/* Check for address family. */
    509   1.24   thorpej 	afp = NULL;
    510   1.24   thorpej 	if (argc > 0) {
    511   1.24   thorpej 		afp = lookup_af(argv[0]);
    512   1.24   thorpej 		if (afp != NULL) {
    513   1.24   thorpej 			argv++;
    514   1.24   thorpej 			argc--;
    515   1.24   thorpej 		}
    516   1.24   thorpej 	}
    517   1.24   thorpej 
    518   1.51   thorpej 	/* Initialize af, just for use in getinfo(). */
    519   1.24   thorpej 	if (afp == NULL)
    520   1.51   thorpej 		af = afs->af_af;
    521   1.52   thorpej 	else
    522   1.52   thorpej 		af = afp->af_af;
    523   1.24   thorpej 
    524   1.24   thorpej 	/* Get information about the interface. */
    525   1.32  christos 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    526   1.13   mycroft 	if (getinfo(&ifr) < 0)
    527    1.1       cgd 		exit(1);
    528   1.24   thorpej 
    529   1.54  sommerfe 	if (sflag) {
    530   1.54  sommerfe 		if (argc != 0)
    531   1.54  sommerfe 			usage();
    532   1.54  sommerfe 		else
    533   1.54  sommerfe 			exit(carrier());
    534   1.54  sommerfe 	}
    535   1.54  sommerfe 
    536   1.24   thorpej 	/* No more arguments means interface status. */
    537   1.13   mycroft 	if (argc == 0) {
    538  1.107    itojun 		printall(name);
    539    1.5   deraadt 		exit(0);
    540    1.5   deraadt 	}
    541   1.51   thorpej 
    542   1.69     enami 	/* The following operations assume inet family as the default. */
    543   1.51   thorpej 	if (afp == NULL)
    544   1.51   thorpej 		afp = afs;
    545   1.51   thorpej 	af = ifr.ifr_addr.sa_family = afp->af_af;
    546   1.24   thorpej 
    547   1.53    itojun #ifdef INET6
    548   1.53    itojun 	/* initialization */
    549   1.53    itojun 	in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
    550   1.53    itojun 	in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
    551   1.53    itojun #endif
    552   1.53    itojun 
    553   1.24   thorpej 	/* Process commands. */
    554    1.1       cgd 	while (argc > 0) {
    555   1.80   thorpej 		const struct cmd *p;
    556    1.1       cgd 
    557    1.1       cgd 		for (p = cmds; p->c_name; p++)
    558   1.24   thorpej 			if (strcmp(argv[0], p->c_name) == 0)
    559    1.1       cgd 				break;
    560   1.55   darrenr 		if (p->c_name == 0 && setaddr) {
    561   1.57   thorpej 			if ((flags & IFF_POINTOPOINT) == 0) {
    562   1.56   darrenr 				errx(1, "can't set destination address %s",
    563   1.55   darrenr 				     "on non-point-to-point link");
    564   1.57   thorpej 			}
    565    1.1       cgd 			p++;	/* got src, do dst */
    566   1.55   darrenr 		}
    567   1.80   thorpej 		if (p->c_func != NULL || p->c_func2 != NULL) {
    568    1.1       cgd 			if (p->c_parameter == NEXTARG) {
    569   1.24   thorpej 				if (argc < 2)
    570   1.12       cgd 					errx(1, "'%s' requires argument",
    571   1.12       cgd 					    p->c_name);
    572   1.32  christos 				(*p->c_func)(argv[1], 0);
    573    1.1       cgd 				argc--, argv++;
    574   1.80   thorpej 			} else if (p->c_parameter == NEXTARG2) {
    575   1.80   thorpej 				if (argc < 3)
    576   1.80   thorpej 					errx(1, "'%s' requires 2 arguments",
    577   1.80   thorpej 					    p->c_name);
    578   1.80   thorpej 				(*p->c_func2)(argv[1], argv[2]);
    579   1.80   thorpej 				argc -= 2, argv += 2;
    580    1.1       cgd 			} else
    581   1.24   thorpej 				(*p->c_func)(argv[0], p->c_parameter);
    582   1.46   thorpej 			actions |= p->c_action;
    583    1.1       cgd 		}
    584    1.1       cgd 		argc--, argv++;
    585    1.1       cgd 	}
    586   1.21       gwr 
    587   1.45   thorpej 	/* Process any media commands that may have been issued. */
    588   1.45   thorpej 	process_media_commands();
    589   1.45   thorpej 
    590   1.53    itojun 	if (af == AF_INET6 && explicit_prefix == 0) {
    591   1.53    itojun 		/*
    592   1.53    itojun 		 * Aggregatable address architecture defines all prefixes
    593   1.53    itojun 		 * are 64. So, it is convenient to set prefixlen to 64 if
    594   1.53    itojun 		 * it is not specified.
    595   1.53    itojun 		 */
    596   1.53    itojun 		setifprefixlen("64", 0);
    597   1.53    itojun 		/* in6_getprefix("64", MASK) if MASK is available here... */
    598   1.53    itojun 	}
    599   1.53    itojun 
    600   1.21       gwr #ifndef INET_ONLY
    601    1.1       cgd 	if (af == AF_ISO)
    602    1.1       cgd 		adjust_nsellength();
    603   1.32  christos 
    604   1.32  christos 	if (af == AF_APPLETALK)
    605   1.32  christos 		checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
    606   1.32  christos 
    607    1.1       cgd 	if (setipdst && af==AF_NS) {
    608    1.1       cgd 		struct nsip_req rq;
    609    1.1       cgd 		int size = sizeof(rq);
    610    1.1       cgd 
    611    1.1       cgd 		rq.rq_ns = addreq.ifra_addr;
    612    1.1       cgd 		rq.rq_ip = addreq.ifra_dstaddr;
    613    1.1       cgd 
    614    1.1       cgd 		if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
    615   1.13   mycroft 			warn("encapsulation routing");
    616    1.1       cgd 	}
    617   1.21       gwr 
    618   1.21       gwr #endif	/* INET_ONLY */
    619   1.21       gwr 
    620    1.1       cgd 	if (clearaddr) {
    621   1.32  christos 		(void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
    622   1.76     enami 		if (ioctl(s, afp->af_difaddr, afp->af_ridreq) < 0)
    623   1.76     enami 			err(1, "SIOCDIFADDR");
    624    1.1       cgd 	}
    625   1.36     lukem 	if (newaddr > 0) {
    626   1.32  christos 		(void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
    627   1.24   thorpej 		if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
    628   1.13   mycroft 			warn("SIOCAIFADDR");
    629    1.1       cgd 	}
    630   1.79     enami 
    631   1.79     enami 	if (reset_if_flags) {
    632   1.79     enami 		(void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
    633   1.79     enami 		ifreq.ifr_flags = flags;
    634   1.79     enami 		if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) < 0)
    635   1.79     enami 			err(1, "SIOCSIFFLAGS");
    636   1.79     enami 	}
    637   1.13   mycroft 	exit(0);
    638   1.13   mycroft }
    639   1.13   mycroft 
    640   1.24   thorpej struct afswtch *
    641   1.24   thorpej lookup_af(cp)
    642   1.24   thorpej 	const char *cp;
    643   1.24   thorpej {
    644   1.24   thorpej 	struct afswtch *a;
    645   1.24   thorpej 
    646   1.24   thorpej 	for (a = afs; a->af_name != NULL; a++)
    647   1.24   thorpej 		if (strcmp(a->af_name, cp) == 0)
    648   1.24   thorpej 			return (a);
    649   1.24   thorpej 	return (NULL);
    650   1.24   thorpej }
    651   1.24   thorpej 
    652   1.13   mycroft void
    653   1.13   mycroft getsock(naf)
    654   1.13   mycroft 	int naf;
    655   1.13   mycroft {
    656   1.13   mycroft 	static int oaf = -1;
    657   1.13   mycroft 
    658   1.13   mycroft 	if (oaf == naf)
    659   1.13   mycroft 		return;
    660   1.13   mycroft 	if (oaf != -1)
    661   1.13   mycroft 		close(s);
    662   1.13   mycroft 	s = socket(naf, SOCK_DGRAM, 0);
    663   1.13   mycroft 	if (s < 0)
    664   1.13   mycroft 		oaf = -1;
    665   1.13   mycroft 	else
    666   1.13   mycroft 		oaf = naf;
    667   1.13   mycroft }
    668   1.13   mycroft 
    669   1.13   mycroft int
    670   1.13   mycroft getinfo(ifr)
    671   1.13   mycroft 	struct ifreq *ifr;
    672   1.13   mycroft {
    673    1.5   deraadt 
    674   1.13   mycroft 	getsock(af);
    675   1.13   mycroft 	if (s < 0)
    676   1.13   mycroft 		err(1, "socket");
    677   1.13   mycroft 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
    678   1.32  christos 		warn("SIOCGIFFLAGS %s", ifr->ifr_name);
    679   1.13   mycroft 		return (-1);
    680   1.13   mycroft 	}
    681   1.13   mycroft 	flags = ifr->ifr_flags;
    682   1.13   mycroft 	if (ioctl(s, SIOCGIFMETRIC, (caddr_t)ifr) < 0) {
    683   1.32  christos 		warn("SIOCGIFMETRIC %s", ifr->ifr_name);
    684   1.13   mycroft 		metric = 0;
    685   1.13   mycroft 	} else
    686   1.13   mycroft 		metric = ifr->ifr_metric;
    687   1.33        is 	if (ioctl(s, SIOCGIFMTU, (caddr_t)ifr) < 0)
    688   1.33        is 		mtu = 0;
    689   1.33        is 	else
    690   1.37        is 		mtu = ifr->ifr_mtu;
    691   1.13   mycroft 	return (0);
    692    1.1       cgd }
    693    1.5   deraadt 
    694   1.13   mycroft void
    695  1.107    itojun printall(ifname)
    696  1.107    itojun 	const char *ifname;
    697    1.5   deraadt {
    698   1.78    itojun 	struct ifaddrs *ifap, *ifa;
    699   1.78    itojun 	struct ifreq ifr;
    700   1.94    itojun 	const struct sockaddr_dl *sdl = NULL;
    701   1.78    itojun 	int idx;
    702   1.78    itojun 	char *p;
    703   1.78    itojun 
    704   1.78    itojun 	if (getifaddrs(&ifap) != 0)
    705   1.78    itojun 		err(1, "getifaddrs");
    706   1.78    itojun 	p = NULL;
    707   1.78    itojun 	idx = 0;
    708   1.78    itojun 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    709   1.78    itojun 		memset(&ifr, 0, sizeof(ifr));
    710   1.78    itojun 		strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
    711   1.78    itojun 		if (sizeof(ifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
    712   1.78    itojun 			memcpy(&ifr.ifr_addr, ifa->ifa_addr,
    713   1.78    itojun 			    ifa->ifa_addr->sa_len);
    714   1.78    itojun 		}
    715   1.78    itojun 
    716  1.107    itojun 		if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
    717  1.107    itojun 			continue;
    718   1.78    itojun 		if (ifa->ifa_addr->sa_family == AF_LINK)
    719   1.78    itojun 			sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
    720   1.78    itojun 		if (p && strcmp(p, ifa->ifa_name) == 0)
    721   1.78    itojun 			continue;
    722   1.78    itojun 		(void) strncpy(name, ifa->ifa_name, sizeof(name));
    723   1.78    itojun 		name[sizeof(name) - 1] = '\0';
    724   1.78    itojun 		p = ifa->ifa_name;
    725   1.78    itojun 
    726   1.78    itojun 		if (getinfo(&ifr) < 0)
    727   1.78    itojun 			continue;
    728   1.78    itojun 		if (bflag && (ifa->ifa_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))
    729   1.78    itojun 			continue;
    730   1.78    itojun 		if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
    731   1.78    itojun 			continue;
    732   1.78    itojun 		if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
    733   1.78    itojun 			continue;
    734   1.78    itojun 
    735   1.78    itojun 		if (sflag && carrier())
    736   1.78    itojun 			continue;
    737   1.78    itojun 		idx++;
    738   1.78    itojun 		/*
    739   1.78    itojun 		 * Are we just listing the interfaces?
    740   1.78    itojun 		 */
    741   1.78    itojun 		if (lflag) {
    742   1.78    itojun 			if (idx > 1)
    743   1.78    itojun 				putchar(' ');
    744   1.78    itojun 			fputs(name, stdout);
    745   1.78    itojun 			continue;
    746   1.78    itojun 		}
    747   1.78    itojun 
    748   1.78    itojun 		if (sdl == NULL) {
    749   1.78    itojun 			status(NULL, 0);
    750   1.78    itojun 		} else {
    751   1.78    itojun 			status(LLADDR(sdl), sdl->sdl_alen);
    752   1.78    itojun 			sdl = NULL;
    753   1.78    itojun 		}
    754   1.78    itojun 	}
    755   1.78    itojun 	if (lflag)
    756   1.78    itojun 		putchar('\n');
    757   1.78    itojun 	freeifaddrs(ifap);
    758   1.87   thorpej }
    759   1.87   thorpej 
    760   1.87   thorpej void
    761   1.87   thorpej list_cloners(void)
    762   1.87   thorpej {
    763   1.87   thorpej 	struct if_clonereq ifcr;
    764   1.87   thorpej 	char *cp, *buf;
    765   1.87   thorpej 	int idx;
    766   1.87   thorpej 
    767   1.87   thorpej 	memset(&ifcr, 0, sizeof(ifcr));
    768   1.87   thorpej 
    769   1.87   thorpej 	getsock(AF_INET);
    770   1.87   thorpej 
    771   1.87   thorpej 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
    772   1.87   thorpej 		err(1, "SIOCIFGCLONERS for count");
    773   1.87   thorpej 
    774   1.87   thorpej 	buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
    775   1.87   thorpej 	if (buf == NULL)
    776   1.87   thorpej 		err(1, "unable to allocate cloner name buffer");
    777   1.87   thorpej 
    778   1.87   thorpej 	ifcr.ifcr_count = ifcr.ifcr_total;
    779   1.87   thorpej 	ifcr.ifcr_buffer = buf;
    780   1.87   thorpej 
    781   1.87   thorpej 	if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0)
    782   1.87   thorpej 		err(1, "SIOCIFGCLONERS for names");
    783   1.87   thorpej 
    784   1.87   thorpej 	/*
    785   1.87   thorpej 	 * In case some disappeared in the mean time, clamp it down.
    786   1.87   thorpej 	 */
    787   1.87   thorpej 	if (ifcr.ifcr_count > ifcr.ifcr_total)
    788   1.87   thorpej 		ifcr.ifcr_count = ifcr.ifcr_total;
    789   1.87   thorpej 
    790   1.87   thorpej 	for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
    791   1.87   thorpej 		if (idx > 0)
    792   1.87   thorpej 			putchar(' ');
    793   1.87   thorpej 		printf("%s", cp);
    794   1.87   thorpej 	}
    795   1.87   thorpej 
    796   1.87   thorpej 	putchar('\n');
    797   1.87   thorpej 	free(buf);
    798   1.87   thorpej 	return;
    799    1.5   deraadt }
    800    1.5   deraadt 
    801   1.81   thorpej /*ARGSUSED*/
    802   1.81   thorpej void
    803   1.81   thorpej clone_create(addr, param)
    804   1.81   thorpej 	const char *addr;
    805   1.81   thorpej 	int param;
    806   1.81   thorpej {
    807   1.81   thorpej 
    808   1.81   thorpej 	/* We're called early... */
    809   1.81   thorpej 	getsock(AF_INET);
    810   1.81   thorpej 
    811   1.81   thorpej 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    812   1.81   thorpej 	if (ioctl(s, SIOCIFCREATE, &ifr) < 0)
    813   1.81   thorpej 		err(1, "SIOCIFCREATE");
    814   1.81   thorpej }
    815   1.81   thorpej 
    816   1.81   thorpej /*ARGSUSED*/
    817   1.81   thorpej void
    818   1.81   thorpej clone_destroy(addr, param)
    819   1.81   thorpej 	const char *addr;
    820   1.81   thorpej 	int param;
    821   1.81   thorpej {
    822   1.81   thorpej 
    823   1.81   thorpej 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    824   1.81   thorpej 	if (ioctl(s, SIOCIFDESTROY, &ifr) < 0)
    825   1.81   thorpej 		err(1, "SIOCIFDESTROY");
    826   1.81   thorpej }
    827   1.81   thorpej 
    828    1.1       cgd #define RIDADDR 0
    829    1.1       cgd #define ADDR	1
    830    1.1       cgd #define MASK	2
    831    1.1       cgd #define DSTADDR	3
    832    1.1       cgd 
    833    1.1       cgd /*ARGSUSED*/
    834   1.16       cgd void
    835    1.1       cgd setifaddr(addr, param)
    836   1.80   thorpej 	const char *addr;
    837   1.16       cgd 	int param;
    838    1.1       cgd {
    839   1.76     enami 	struct ifreq *ifr;		/* XXX */
    840   1.76     enami 
    841    1.1       cgd 	/*
    842    1.1       cgd 	 * Delay the ioctl to set the interface addr until flags are all set.
    843    1.1       cgd 	 * The address interpretation may depend on the flags,
    844    1.1       cgd 	 * and the flags may change when the address is set.
    845    1.1       cgd 	 */
    846    1.1       cgd 	setaddr++;
    847   1.36     lukem 	if (newaddr == -1)
    848   1.36     lukem 		newaddr = 1;
    849   1.76     enami 	if (doalias == 0 && afp->af_gifaddr != 0) {
    850   1.76     enami 		ifr = (struct ifreq *)afp->af_ridreq;
    851   1.76     enami 		(void) strncpy(ifr->ifr_name, name, sizeof(ifr->ifr_name));
    852   1.76     enami 		ifr->ifr_addr.sa_family = afp->af_af;
    853   1.76     enami 		if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
    854   1.76     enami 			clearaddr = 1;
    855   1.76     enami 		else if (errno == EADDRNOTAVAIL)
    856   1.76     enami 			/* No address was assigned yet. */
    857   1.76     enami 			;
    858   1.76     enami 		else
    859   1.76     enami 			err(1, "SIOCGIFADDR");
    860   1.76     enami 	}
    861   1.76     enami 
    862    1.1       cgd 	(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
    863    1.1       cgd }
    864    1.1       cgd 
    865   1.16       cgd void
    866   1.80   thorpej settunnel(src, dst)
    867   1.80   thorpej 	const char *src, *dst;
    868   1.80   thorpej {
    869   1.80   thorpej 	struct addrinfo hints, *srcres, *dstres;
    870   1.80   thorpej 	int ecode;
    871  1.100    itojun 	struct if_laddrreq req;
    872   1.80   thorpej 
    873   1.80   thorpej 	memset(&hints, 0, sizeof(hints));
    874   1.80   thorpej 	hints.ai_family = afp->af_af;
    875  1.102    itojun 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
    876   1.80   thorpej 
    877  1.102    itojun 	if ((ecode = getaddrinfo(src, NULL, &hints, &srcres)) != 0)
    878   1.80   thorpej 		errx(1, "error in parsing address string: %s",
    879   1.80   thorpej 		    gai_strerror(ecode));
    880   1.80   thorpej 
    881  1.103    itojun 	if ((ecode = getaddrinfo(dst, NULL, &hints, &dstres)) != 0)
    882   1.80   thorpej 		errx(1, "error in parsing address string: %s",
    883   1.80   thorpej 		    gai_strerror(ecode));
    884   1.80   thorpej 
    885   1.80   thorpej 	if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
    886   1.80   thorpej 		errx(1,
    887   1.80   thorpej 		    "source and destination address families do not match");
    888   1.80   thorpej 
    889  1.100    itojun 	if (srcres->ai_addrlen > sizeof(req.addr) ||
    890  1.100    itojun 	    dstres->ai_addrlen > sizeof(req.dstaddr))
    891  1.100    itojun 		errx(1, "invalid sockaddr");
    892  1.100    itojun 
    893  1.100    itojun 	memset(&req, 0, sizeof(req));
    894  1.100    itojun 	strncpy(req.iflr_name, name, sizeof(req.iflr_name));
    895  1.100    itojun 	memcpy(&req.addr, srcres->ai_addr, srcres->ai_addrlen);
    896  1.100    itojun 	memcpy(&req.dstaddr, dstres->ai_addr, dstres->ai_addrlen);
    897  1.100    itojun 	if (ioctl(s, SIOCSLIFPHYADDR, &req) < 0)
    898  1.100    itojun 		warn("SIOCSLIFPHYADDR");
    899   1.80   thorpej 
    900   1.80   thorpej 	freeaddrinfo(srcres);
    901   1.80   thorpej 	freeaddrinfo(dstres);
    902   1.80   thorpej }
    903   1.80   thorpej 
    904   1.80   thorpej /* ARGSUSED */
    905   1.80   thorpej void
    906   1.80   thorpej deletetunnel(vname, param)
    907   1.80   thorpej 	const char *vname;
    908   1.80   thorpej 	int param;
    909   1.80   thorpej {
    910   1.80   thorpej 
    911   1.80   thorpej 	if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
    912   1.80   thorpej 		err(1, "SIOCDIFPHYADDR");
    913   1.80   thorpej }
    914   1.80   thorpej 
    915   1.89   thorpej void setvlan(val, d)
    916   1.89   thorpej 	const char *val;
    917   1.89   thorpej 	int d;
    918   1.89   thorpej {
    919   1.89   thorpej 	struct vlanreq vlr;
    920   1.89   thorpej 
    921   1.89   thorpej 	if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
    922   1.89   thorpej 	    !isdigit(ifr.ifr_name[4]))
    923   1.89   thorpej 		errx(EXIT_FAILURE,
    924   1.89   thorpej 		    "``vlan'' valid only with vlan(4) interfaces");
    925   1.89   thorpej 
    926   1.89   thorpej 	vlan_tag = atoi(val);
    927   1.89   thorpej 
    928   1.89   thorpej 	memset(&vlr, 0, sizeof(vlr));
    929   1.89   thorpej 	ifr.ifr_data = (caddr_t)&vlr;
    930   1.89   thorpej 
    931   1.89   thorpej 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
    932   1.89   thorpej 		err(EXIT_FAILURE, "SIOCGETVLAN");
    933   1.89   thorpej 
    934   1.89   thorpej 	vlr.vlr_tag = vlan_tag;
    935   1.89   thorpej 
    936   1.89   thorpej 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
    937   1.89   thorpej 		err(EXIT_FAILURE, "SIOCSETVLAN");
    938   1.89   thorpej }
    939   1.89   thorpej 
    940   1.89   thorpej void setvlanif(val, d)
    941   1.89   thorpej 	const char *val;
    942   1.89   thorpej 	int d;
    943   1.89   thorpej {
    944   1.89   thorpej 	struct vlanreq vlr;
    945   1.89   thorpej 
    946   1.89   thorpej 	if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
    947   1.89   thorpej 	    !isdigit(ifr.ifr_name[4]))
    948   1.89   thorpej 		errx(EXIT_FAILURE,
    949   1.89   thorpej 		    "``vlanif'' valid only with vlan(4) interfaces");
    950   1.89   thorpej 
    951   1.89   thorpej 	if (vlan_tag == (u_int)-1)
    952   1.89   thorpej 		errx(EXIT_FAILURE,
    953   1.89   thorpej 		    "must specify both ``vlan'' and ``vlanif''");
    954   1.89   thorpej 
    955   1.89   thorpej 	memset(&vlr, 0, sizeof(vlr));
    956   1.89   thorpej 	ifr.ifr_data = (caddr_t)&vlr;
    957   1.89   thorpej 
    958   1.89   thorpej 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
    959   1.89   thorpej 		err(EXIT_FAILURE, "SIOCGETVLAN");
    960   1.89   thorpej 
    961   1.89   thorpej 	strlcpy(vlr.vlr_parent, val, sizeof(vlr.vlr_parent));
    962   1.89   thorpej 	vlr.vlr_tag = vlan_tag;
    963   1.89   thorpej 
    964   1.89   thorpej 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
    965   1.89   thorpej 		err(EXIT_FAILURE, "SIOCSETVLAN");
    966   1.89   thorpej }
    967   1.89   thorpej 
    968   1.89   thorpej void unsetvlanif(val, d)
    969   1.89   thorpej 	const char *val;
    970   1.89   thorpej 	int d;
    971   1.89   thorpej {
    972   1.89   thorpej 	struct vlanreq vlr;
    973   1.89   thorpej 
    974   1.89   thorpej 	if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
    975   1.89   thorpej 	    !isdigit(ifr.ifr_name[4]))
    976   1.89   thorpej 		errx(EXIT_FAILURE,
    977   1.89   thorpej 		    "``vlanif'' valid only with vlan(4) interfaces");
    978   1.89   thorpej 
    979   1.89   thorpej 	memset(&vlr, 0, sizeof(vlr));
    980   1.89   thorpej 	ifr.ifr_data = (caddr_t)&vlr;
    981   1.89   thorpej 
    982   1.89   thorpej 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
    983   1.89   thorpej 		err(EXIT_FAILURE, "SIOCGETVLAN");
    984   1.89   thorpej 
    985   1.89   thorpej 	vlr.vlr_parent[0] = '\0';
    986   1.89   thorpej 	vlr.vlr_tag = 0;
    987   1.89   thorpej 
    988   1.89   thorpej 	if (ioctl(s, SIOCSETVLAN, (caddr_t)&ifr) == -1)
    989   1.89   thorpej 		err(EXIT_FAILURE, "SIOCSETVLAN");
    990   1.89   thorpej }
    991   1.89   thorpej 
    992   1.80   thorpej void
    993   1.32  christos setifnetmask(addr, d)
    994   1.80   thorpej 	const char *addr;
    995   1.32  christos 	int d;
    996    1.1       cgd {
    997    1.1       cgd 	(*afp->af_getaddr)(addr, MASK);
    998    1.1       cgd }
    999    1.1       cgd 
   1000   1.16       cgd void
   1001   1.32  christos setifbroadaddr(addr, d)
   1002   1.80   thorpej 	const char *addr;
   1003   1.32  christos 	int d;
   1004    1.1       cgd {
   1005    1.1       cgd 	(*afp->af_getaddr)(addr, DSTADDR);
   1006    1.1       cgd }
   1007    1.1       cgd 
   1008   1.16       cgd void
   1009   1.32  christos setifipdst(addr, d)
   1010   1.80   thorpej 	const char *addr;
   1011   1.32  christos 	int d;
   1012    1.1       cgd {
   1013    1.1       cgd 	in_getaddr(addr, DSTADDR);
   1014    1.1       cgd 	setipdst++;
   1015    1.1       cgd 	clearaddr = 0;
   1016    1.1       cgd 	newaddr = 0;
   1017    1.1       cgd }
   1018   1.16       cgd 
   1019    1.1       cgd #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
   1020    1.1       cgd /*ARGSUSED*/
   1021   1.16       cgd void
   1022    1.1       cgd notealias(addr, param)
   1023   1.80   thorpej 	const char *addr;
   1024   1.16       cgd 	int param;
   1025    1.1       cgd {
   1026    1.1       cgd 	if (setaddr && doalias == 0 && param < 0)
   1027   1.32  christos 		(void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
   1028   1.32  christos 		    rqtosa(af_addreq)->sa_len);
   1029    1.1       cgd 	doalias = param;
   1030    1.1       cgd 	if (param < 0) {
   1031    1.1       cgd 		clearaddr = 1;
   1032    1.1       cgd 		newaddr = 0;
   1033    1.1       cgd 	} else
   1034    1.1       cgd 		clearaddr = 0;
   1035    1.1       cgd }
   1036    1.1       cgd 
   1037    1.1       cgd /*ARGSUSED*/
   1038   1.16       cgd void
   1039   1.13   mycroft notrailers(vname, value)
   1040   1.80   thorpej 	const char *vname;
   1041   1.13   mycroft 	int value;
   1042   1.13   mycroft {
   1043   1.34     lukem 	puts("Note: trailers are no longer sent, but always received");
   1044   1.13   mycroft }
   1045   1.13   mycroft 
   1046   1.13   mycroft /*ARGSUSED*/
   1047   1.16       cgd void
   1048    1.1       cgd setifdstaddr(addr, param)
   1049   1.80   thorpej 	const char *addr;
   1050    1.1       cgd 	int param;
   1051    1.1       cgd {
   1052    1.1       cgd 	(*afp->af_getaddr)(addr, DSTADDR);
   1053    1.1       cgd }
   1054    1.1       cgd 
   1055   1.16       cgd void
   1056    1.1       cgd setifflags(vname, value)
   1057   1.80   thorpej 	const char *vname;
   1058   1.16       cgd 	int value;
   1059    1.1       cgd {
   1060   1.79     enami 	struct ifreq ifreq;
   1061   1.79     enami 
   1062   1.79     enami 	(void) strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name));
   1063   1.79     enami  	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifreq) < 0)
   1064   1.13   mycroft 		err(1, "SIOCGIFFLAGS");
   1065   1.79     enami  	flags = ifreq.ifr_flags;
   1066    1.1       cgd 
   1067    1.1       cgd 	if (value < 0) {
   1068    1.1       cgd 		value = -value;
   1069    1.1       cgd 		flags &= ~value;
   1070    1.1       cgd 	} else
   1071    1.1       cgd 		flags |= value;
   1072   1.79     enami 	ifreq.ifr_flags = flags;
   1073   1.79     enami 	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifreq) < 0)
   1074   1.13   mycroft 		err(1, "SIOCSIFFLAGS");
   1075   1.38       mrg 
   1076   1.38       mrg 	reset_if_flags = 1;
   1077    1.1       cgd }
   1078    1.1       cgd 
   1079   1.53    itojun #ifdef INET6
   1080   1.53    itojun void
   1081   1.53    itojun setia6flags(vname, value)
   1082   1.80   thorpej 	const char *vname;
   1083   1.53    itojun 	int value;
   1084   1.53    itojun {
   1085   1.53    itojun 	if (value < 0) {
   1086   1.53    itojun 		value = -value;
   1087   1.53    itojun 		in6_addreq.ifra_flags &= ~value;
   1088   1.53    itojun 	} else
   1089   1.53    itojun 		in6_addreq.ifra_flags |= value;
   1090   1.53    itojun }
   1091   1.53    itojun 
   1092   1.53    itojun void
   1093   1.53    itojun setia6pltime(val, d)
   1094   1.80   thorpej 	const char *val;
   1095   1.53    itojun 	int d;
   1096   1.53    itojun {
   1097   1.53    itojun 	setia6lifetime("pltime", val);
   1098   1.53    itojun }
   1099   1.53    itojun 
   1100   1.53    itojun void
   1101   1.53    itojun setia6vltime(val, d)
   1102   1.80   thorpej 	const char *val;
   1103   1.53    itojun 	int d;
   1104   1.53    itojun {
   1105   1.53    itojun 	setia6lifetime("vltime", val);
   1106   1.53    itojun }
   1107   1.53    itojun 
   1108   1.53    itojun void
   1109   1.53    itojun setia6lifetime(cmd, val)
   1110   1.80   thorpej 	const char *cmd;
   1111   1.80   thorpej 	const char *val;
   1112   1.53    itojun {
   1113   1.53    itojun 	time_t newval, t;
   1114   1.53    itojun 	char *ep;
   1115   1.53    itojun 
   1116   1.53    itojun 	t = time(NULL);
   1117   1.53    itojun 	newval = (time_t)strtoul(val, &ep, 0);
   1118   1.53    itojun 	if (val == ep)
   1119   1.53    itojun 		errx(1, "invalid %s", cmd);
   1120   1.53    itojun 	if (afp->af_af != AF_INET6)
   1121   1.53    itojun 		errx(1, "%s not allowed for the AF", cmd);
   1122   1.53    itojun 	if (strcmp(cmd, "vltime") == 0) {
   1123   1.53    itojun 		in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
   1124   1.53    itojun 		in6_addreq.ifra_lifetime.ia6t_vltime = newval;
   1125   1.53    itojun 	} else if (strcmp(cmd, "pltime") == 0) {
   1126   1.53    itojun 		in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
   1127   1.53    itojun 		in6_addreq.ifra_lifetime.ia6t_pltime = newval;
   1128   1.53    itojun 	}
   1129   1.53    itojun }
   1130   1.53    itojun #endif
   1131   1.53    itojun 
   1132   1.16       cgd void
   1133   1.32  christos setifmetric(val, d)
   1134   1.80   thorpej 	const char *val;
   1135   1.32  christos 	int d;
   1136    1.1       cgd {
   1137   1.32  christos 	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   1138    1.1       cgd 	ifr.ifr_metric = atoi(val);
   1139    1.1       cgd 	if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
   1140   1.13   mycroft 		warn("SIOCSIFMETRIC");
   1141    1.1       cgd }
   1142    1.1       cgd 
   1143   1.24   thorpej void
   1144   1.33        is setifmtu(val, d)
   1145   1.80   thorpej 	const char *val;
   1146   1.33        is 	int d;
   1147   1.33        is {
   1148   1.33        is 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1149   1.37        is 	ifr.ifr_mtu = atoi(val);
   1150   1.33        is 	if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
   1151   1.33        is 		warn("SIOCSIFMTU");
   1152   1.33        is }
   1153   1.33        is 
   1154   1.88      onoe const char *
   1155   1.88      onoe get_string(val, sep, buf, lenp)
   1156   1.88      onoe 	const char *val, *sep;
   1157   1.88      onoe 	u_int8_t *buf;
   1158   1.88      onoe 	int *lenp;
   1159   1.62    chopps {
   1160   1.85      onoe 	int len;
   1161   1.88      onoe 	int hexstr;
   1162   1.82      onoe 	u_int8_t *p;
   1163   1.62    chopps 
   1164   1.88      onoe 	len = *lenp;
   1165   1.88      onoe 	p = buf;
   1166   1.88      onoe 	hexstr = (val[0] == '0' && tolower((u_char)val[1]) == 'x');
   1167   1.88      onoe 	if (hexstr)
   1168   1.82      onoe 		val += 2;
   1169   1.88      onoe 	for (;;) {
   1170   1.88      onoe 		if (*val == '\0')
   1171   1.88      onoe 			break;
   1172   1.88      onoe 		if (sep != NULL && strchr(sep, *val) != NULL) {
   1173   1.88      onoe 			val++;
   1174   1.88      onoe 			break;
   1175   1.88      onoe 		}
   1176   1.88      onoe 		if (hexstr) {
   1177   1.88      onoe 			if (!isxdigit((u_char)val[0]) ||
   1178   1.88      onoe 			    !isxdigit((u_char)val[1])) {
   1179   1.88      onoe 				warnx("bad hexadecimal digits");
   1180   1.88      onoe 				return NULL;
   1181   1.85      onoe 			}
   1182   1.88      onoe 		}
   1183   1.88      onoe 		if (p > buf + len) {
   1184   1.88      onoe 			if (hexstr)
   1185   1.88      onoe 				warnx("hexadecimal digits too long");
   1186   1.88      onoe 			else
   1187   1.88      onoe 				warnx("strings too long");
   1188   1.88      onoe 			return NULL;
   1189   1.88      onoe 		}
   1190   1.88      onoe 		if (hexstr) {
   1191   1.82      onoe #define	tohex(x)	(isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10)
   1192   1.84      onoe 			*p++ = (tohex((u_char)val[0]) << 4) |
   1193   1.84      onoe 			    tohex((u_char)val[1]);
   1194   1.82      onoe #undef tohex
   1195   1.82      onoe 			val += 2;
   1196   1.88      onoe 		} else
   1197   1.88      onoe 			*p++ = *val++;
   1198   1.88      onoe 	}
   1199   1.88      onoe 	len = p - buf;
   1200   1.88      onoe 	if (len < *lenp)
   1201   1.88      onoe 		memset(p, 0, *lenp - len);
   1202   1.88      onoe 	*lenp = len;
   1203   1.88      onoe 	return val;
   1204   1.88      onoe }
   1205   1.88      onoe 
   1206   1.88      onoe void
   1207   1.88      onoe print_string(buf, len)
   1208   1.88      onoe 	const u_int8_t *buf;
   1209   1.88      onoe 	int len;
   1210   1.88      onoe {
   1211   1.88      onoe 	int i;
   1212   1.88      onoe 	int hasspc;
   1213   1.88      onoe 
   1214   1.88      onoe 	i = 0;
   1215   1.88      onoe 	hasspc = 0;
   1216   1.88      onoe 	if (len < 2 || buf[0] != '0' || tolower(buf[1]) != 'x') {
   1217   1.88      onoe 		for (; i < len; i++) {
   1218   1.88      onoe 			if (!isprint(buf[i]))
   1219   1.88      onoe 				break;
   1220   1.88      onoe 			if (isspace(buf[i]))
   1221   1.88      onoe 				hasspc++;
   1222   1.88      onoe 		}
   1223   1.88      onoe 	}
   1224   1.88      onoe 	if (i == len) {
   1225   1.88      onoe 		if (hasspc || len == 0)
   1226   1.88      onoe 			printf("\"%.*s\"", len, buf);
   1227   1.88      onoe 		else
   1228   1.88      onoe 			printf("%.*s", len, buf);
   1229   1.88      onoe 	} else {
   1230   1.88      onoe 		printf("0x");
   1231   1.88      onoe 		for (i = 0; i < len; i++)
   1232   1.88      onoe 			printf("%02x", buf[i]);
   1233   1.88      onoe 	}
   1234   1.88      onoe }
   1235   1.88      onoe 
   1236   1.88      onoe void
   1237   1.88      onoe setifnwid(val, d)
   1238   1.88      onoe 	const char *val;
   1239   1.88      onoe 	int d;
   1240   1.88      onoe {
   1241   1.88      onoe 	struct ieee80211_nwid nwid;
   1242   1.88      onoe 	int len;
   1243   1.88      onoe 
   1244   1.88      onoe 	len = sizeof(nwid.i_nwid);
   1245   1.88      onoe 	if (get_string(val, NULL, nwid.i_nwid, &len) == NULL)
   1246   1.88      onoe 		return;
   1247   1.88      onoe 	nwid.i_len = len;
   1248   1.88      onoe 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1249   1.88      onoe 	ifr.ifr_data = (caddr_t)&nwid;
   1250   1.88      onoe 	if (ioctl(s, SIOCS80211NWID, (caddr_t)&ifr) < 0)
   1251   1.88      onoe 		warn("SIOCS80211NWID");
   1252   1.88      onoe }
   1253   1.88      onoe 
   1254   1.88      onoe void
   1255   1.88      onoe setifnwkey(val, d)
   1256   1.88      onoe 	const char *val;
   1257   1.88      onoe 	int d;
   1258   1.88      onoe {
   1259   1.88      onoe 	struct ieee80211_nwkey nwkey;
   1260   1.88      onoe 	int i;
   1261   1.88      onoe 	u_int8_t keybuf[IEEE80211_WEP_NKID][16];
   1262   1.88      onoe 
   1263   1.88      onoe 	nwkey.i_wepon = 1;
   1264   1.88      onoe 	nwkey.i_defkid = 1;
   1265   1.88      onoe 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1266   1.88      onoe 		nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
   1267   1.88      onoe 		nwkey.i_key[i].i_keydat = keybuf[i];
   1268   1.88      onoe 	}
   1269   1.88      onoe 	if (d != 0) {
   1270   1.88      onoe 		/* disable WEP encryption */
   1271   1.88      onoe 		nwkey.i_wepon = 0;
   1272   1.88      onoe 		i = 0;
   1273   1.88      onoe 	} else if (isdigit(val[0]) && val[1] == ':') {
   1274   1.88      onoe 		/* specifying a full set of four keys */
   1275   1.88      onoe 		nwkey.i_defkid = val[0] - '0';
   1276   1.88      onoe 		val += 2;
   1277   1.88      onoe 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1278   1.88      onoe 			val = get_string(val, ",", keybuf[i],
   1279   1.88      onoe 			    &nwkey.i_key[i].i_keylen);
   1280   1.88      onoe 			if (val == NULL)
   1281   1.88      onoe 				return;
   1282   1.82      onoe 		}
   1283   1.82      onoe 		if (*val != '\0') {
   1284   1.88      onoe 			warnx("SIOCS80211NWKEY: too many keys.");
   1285   1.82      onoe 			return;
   1286   1.82      onoe 		}
   1287   1.82      onoe 	} else {
   1288   1.88      onoe 		val = get_string(val, NULL, keybuf[0],
   1289   1.88      onoe 		    &nwkey.i_key[0].i_keylen);
   1290   1.88      onoe 		if (val == NULL)
   1291   1.85      onoe 			return;
   1292   1.88      onoe 		i = 1;
   1293   1.82      onoe 	}
   1294   1.88      onoe 	for (; i < IEEE80211_WEP_NKID; i++)
   1295   1.88      onoe 		nwkey.i_key[i].i_keylen = 0;
   1296   1.88      onoe 	(void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
   1297   1.88      onoe 	if (ioctl(s, SIOCS80211NWKEY, (caddr_t)&nwkey) < 0)
   1298   1.88      onoe 		warn("SIOCS80211NWKEY");
   1299   1.62    chopps }
   1300   1.62    chopps 
   1301   1.62    chopps void
   1302   1.92   thorpej setifpowersave(val, d)
   1303   1.92   thorpej 	const char *val;
   1304   1.92   thorpej 	int d;
   1305   1.92   thorpej {
   1306   1.92   thorpej 	struct ieee80211_power power;
   1307   1.92   thorpej 
   1308   1.92   thorpej 	(void)strncpy(power.i_name, name, sizeof(power.i_name));
   1309   1.92   thorpej 	if (ioctl(s, SIOCG80211POWER, (caddr_t)&power) < 0) {
   1310   1.92   thorpej 		warn("SIOCG80211POWER");
   1311   1.92   thorpej 		return;
   1312   1.92   thorpej 	}
   1313   1.92   thorpej 
   1314   1.92   thorpej 	power.i_enabled = d;
   1315   1.92   thorpej 	if (ioctl(s, SIOCS80211POWER, (caddr_t)&power) < 0)
   1316   1.92   thorpej 		warn("SIOCS80211POWER");
   1317   1.92   thorpej }
   1318   1.92   thorpej 
   1319   1.92   thorpej void
   1320   1.92   thorpej setifpowersavesleep(val, d)
   1321   1.92   thorpej 	const char *val;
   1322   1.92   thorpej 	int d;
   1323   1.92   thorpej {
   1324   1.92   thorpej 	struct ieee80211_power power;
   1325   1.92   thorpej 
   1326   1.92   thorpej 	(void)strncpy(power.i_name, name, sizeof(power.i_name));
   1327   1.92   thorpej 	if (ioctl(s, SIOCG80211POWER, (caddr_t)&power) < 0) {
   1328   1.92   thorpej 		warn("SIOCG80211POWER");
   1329   1.92   thorpej 		return;
   1330   1.92   thorpej 	}
   1331   1.92   thorpej 
   1332   1.92   thorpej 	power.i_maxsleep = atoi(val);
   1333   1.92   thorpej 	if (ioctl(s, SIOCS80211POWER, (caddr_t)&power) < 0)
   1334   1.92   thorpej 		warn("SIOCS80211POWER");
   1335   1.92   thorpej }
   1336   1.92   thorpej 
   1337   1.92   thorpej void
   1338   1.62    chopps ieee80211_status()
   1339   1.62    chopps {
   1340   1.82      onoe 	int i;
   1341   1.82      onoe 	struct ieee80211_nwid nwid;
   1342   1.88      onoe 	struct ieee80211_nwkey nwkey;
   1343   1.92   thorpej 	struct ieee80211_power power;
   1344   1.88      onoe 	u_int8_t keybuf[IEEE80211_WEP_NKID][16];
   1345   1.62    chopps 
   1346   1.62    chopps 	memset(&ifr, 0, sizeof(ifr));
   1347   1.82      onoe 	ifr.ifr_data = (caddr_t)&nwid;
   1348   1.62    chopps 	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1349   1.82      onoe 	if (ioctl(s, SIOCG80211NWID, (caddr_t)&ifr) != 0)
   1350   1.82      onoe 		return;
   1351   1.83      onoe 	if (nwid.i_len > IEEE80211_NWID_LEN) {
   1352   1.86     enami 		warnx("SIOCG80211NWID: wrong length of nwid (%d)", nwid.i_len);
   1353   1.83      onoe 		return;
   1354   1.83      onoe 	}
   1355   1.88      onoe 	printf("\tnwid ");
   1356   1.88      onoe 	print_string(nwid.i_nwid, nwid.i_len);
   1357   1.88      onoe 	memset(&nwkey, 0, sizeof(nwkey));
   1358   1.88      onoe 	(void)strncpy(nwkey.i_name, name, sizeof(nwkey.i_name));
   1359   1.88      onoe 	/* show nwkey only when WEP is enabled */
   1360   1.88      onoe 	if (ioctl(s, SIOCG80211NWKEY, (caddr_t)&nwkey) != 0 ||
   1361   1.88      onoe 	    nwkey.i_wepon == 0) {
   1362   1.88      onoe 		printf("\n");
   1363   1.92   thorpej 		goto skip_wep;
   1364   1.88      onoe 	}
   1365   1.88      onoe 
   1366   1.88      onoe 	printf(" nwkey ");
   1367   1.88      onoe 	/* try to retrieve WEP keys */
   1368   1.88      onoe 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1369   1.88      onoe 		nwkey.i_key[i].i_keydat = keybuf[i];
   1370   1.88      onoe 		nwkey.i_key[i].i_keylen = sizeof(keybuf[i]);
   1371   1.88      onoe 	}
   1372   1.88      onoe 	if (ioctl(s, SIOCG80211NWKEY, (caddr_t)&nwkey) != 0) {
   1373   1.88      onoe 		printf("*****");
   1374   1.88      onoe 	} else {
   1375   1.88      onoe 		if (nwkey.i_defkid != 1) {
   1376   1.88      onoe 			/* non default key or multiple keys defined */
   1377   1.88      onoe 			i = 0;
   1378   1.88      onoe 		} else if (nwkey.i_key[0].i_keylen >= 2 &&
   1379   1.88      onoe 		    isdigit(nwkey.i_key[0].i_keydat[0]) &&
   1380   1.88      onoe 		    nwkey.i_key[0].i_keydat[1] == ':') {
   1381   1.88      onoe 			/* ambiguous */
   1382   1.88      onoe 			i = 0;
   1383   1.88      onoe 		} else {
   1384   1.88      onoe 			for (i = 1; i < IEEE80211_WEP_NKID; i++) {
   1385   1.88      onoe 				if (nwkey.i_key[i].i_keylen != 0)
   1386   1.88      onoe 					break;
   1387   1.88      onoe 			}
   1388   1.88      onoe 		}
   1389   1.88      onoe 		if (i == IEEE80211_WEP_NKID) {
   1390   1.88      onoe 			/* only show the first key */
   1391   1.88      onoe 			print_string(nwkey.i_key[0].i_keydat,
   1392   1.88      onoe 			    nwkey.i_key[0].i_keylen);
   1393   1.88      onoe 		} else {
   1394   1.88      onoe 			printf("%d:", nwkey.i_defkid);
   1395   1.88      onoe 			for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   1396   1.88      onoe 				if (i > 0)
   1397   1.88      onoe 					printf(",");
   1398   1.88      onoe 				print_string(nwkey.i_key[i].i_keydat,
   1399   1.88      onoe 				    nwkey.i_key[i].i_keylen);
   1400   1.88      onoe 			}
   1401   1.82      onoe 		}
   1402   1.82      onoe 	}
   1403   1.92   thorpej 	printf("\n");
   1404   1.92   thorpej 
   1405   1.92   thorpej  skip_wep:
   1406   1.92   thorpej 	(void)strncpy(power.i_name, name, sizeof(power.i_name));
   1407   1.92   thorpej 	if (ioctl(s, SIOCG80211POWER, &power) != 0)
   1408   1.92   thorpej 		return;
   1409   1.92   thorpej 	printf("\tpowersave ");
   1410   1.92   thorpej 	if (power.i_enabled)
   1411   1.92   thorpej 		printf("on (%dms sleep)", power.i_maxsleep);
   1412   1.92   thorpej 	else
   1413   1.92   thorpej 		printf("off");
   1414   1.88      onoe 	printf("\n");
   1415   1.62    chopps }
   1416   1.62    chopps 
   1417   1.62    chopps void
   1418   1.45   thorpej init_current_media()
   1419   1.24   thorpej {
   1420   1.24   thorpej 	struct ifmediareq ifmr;
   1421   1.24   thorpej 
   1422   1.45   thorpej 	/*
   1423   1.45   thorpej 	 * If we have not yet done so, grab the currently-selected
   1424   1.45   thorpej 	 * media.
   1425   1.45   thorpej 	 */
   1426   1.46   thorpej 	if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
   1427   1.45   thorpej 		(void) memset(&ifmr, 0, sizeof(ifmr));
   1428   1.45   thorpej 		(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1429   1.45   thorpej 
   1430   1.45   thorpej 		if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
   1431   1.45   thorpej 			/*
   1432   1.45   thorpej 			 * If we get E2BIG, the kernel is telling us
   1433   1.45   thorpej 			 * that there are more, so we can ignore it.
   1434   1.45   thorpej 			 */
   1435   1.45   thorpej 			if (errno != E2BIG)
   1436   1.45   thorpej 				err(1, "SGIOCGIFMEDIA");
   1437   1.45   thorpej 		}
   1438   1.24   thorpej 
   1439   1.45   thorpej 		media_current = ifmr.ifm_current;
   1440   1.24   thorpej 	}
   1441   1.24   thorpej 
   1442   1.45   thorpej 	/* Sanity. */
   1443   1.45   thorpej 	if (IFM_TYPE(media_current) == 0)
   1444   1.45   thorpej 		errx(1, "%s: no link type?", name);
   1445   1.45   thorpej }
   1446   1.45   thorpej 
   1447   1.45   thorpej void
   1448   1.45   thorpej process_media_commands()
   1449   1.45   thorpej {
   1450   1.45   thorpej 
   1451   1.46   thorpej 	if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
   1452   1.45   thorpej 		/* Nothing to do. */
   1453   1.45   thorpej 		return;
   1454   1.45   thorpej 	}
   1455   1.24   thorpej 
   1456   1.24   thorpej 	/*
   1457   1.45   thorpej 	 * Media already set up, and commands sanity-checked.  Set/clear
   1458   1.45   thorpej 	 * any options, and we're ready to go.
   1459   1.24   thorpej 	 */
   1460   1.45   thorpej 	media_current |= mediaopt_set;
   1461   1.45   thorpej 	media_current &= ~mediaopt_clear;
   1462   1.24   thorpej 
   1463   1.24   thorpej 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1464   1.45   thorpej 	ifr.ifr_media = media_current;
   1465   1.24   thorpej 
   1466   1.24   thorpej 	if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
   1467   1.24   thorpej 		err(1, "SIOCSIFMEDIA");
   1468   1.24   thorpej }
   1469   1.24   thorpej 
   1470   1.24   thorpej void
   1471   1.45   thorpej setmedia(val, d)
   1472   1.80   thorpej 	const char *val;
   1473   1.32  christos 	int d;
   1474   1.24   thorpej {
   1475   1.45   thorpej 	int type, subtype, inst;
   1476   1.45   thorpej 
   1477   1.45   thorpej 	init_current_media();
   1478   1.45   thorpej 
   1479   1.45   thorpej 	/* Only one media command may be given. */
   1480   1.46   thorpej 	if (actions & A_MEDIA)
   1481   1.45   thorpej 		errx(1, "only one `media' command may be issued");
   1482   1.24   thorpej 
   1483   1.45   thorpej 	/* Must not come after mediaopt commands */
   1484   1.46   thorpej 	if (actions & A_MEDIAOPT)
   1485   1.45   thorpej 		errx(1, "may not issue `media' after `mediaopt' commands");
   1486   1.45   thorpej 
   1487   1.47   thorpej 	/*
   1488   1.47   thorpej 	 * No need to check if `instance' has been issued; setmediainst()
   1489   1.47   thorpej 	 * craps out if `media' has not been specified.
   1490   1.47   thorpej 	 */
   1491   1.47   thorpej 
   1492   1.45   thorpej 	type = IFM_TYPE(media_current);
   1493   1.45   thorpej 	inst = IFM_INST(media_current);
   1494   1.45   thorpej 
   1495   1.45   thorpej 	/* Look up the subtype. */
   1496   1.45   thorpej 	subtype = get_media_subtype(type, val);
   1497   1.24   thorpej 
   1498   1.45   thorpej 	/* Build the new current media word. */
   1499   1.45   thorpej 	media_current = IFM_MAKEWORD(type, subtype, 0, inst);
   1500   1.24   thorpej 
   1501   1.45   thorpej 	/* Media will be set after other processing is complete. */
   1502   1.24   thorpej }
   1503   1.24   thorpej 
   1504   1.24   thorpej void
   1505   1.45   thorpej setmediaopt(val, d)
   1506   1.80   thorpej 	const char *val;
   1507   1.45   thorpej 	int d;
   1508   1.24   thorpej {
   1509   1.24   thorpej 
   1510   1.45   thorpej 	init_current_media();
   1511   1.24   thorpej 
   1512   1.45   thorpej 	/* Can only issue `mediaopt' once. */
   1513   1.46   thorpej 	if (actions & A_MEDIAOPTSET)
   1514   1.45   thorpej 		errx(1, "only one `mediaopt' command may be issued");
   1515   1.26   thorpej 
   1516   1.47   thorpej 	/* Can't issue `mediaopt' if `instance' has already been issued. */
   1517   1.47   thorpej 	if (actions & A_MEDIAINST)
   1518   1.47   thorpej 		errx(1, "may not issue `mediaopt' after `instance'");
   1519   1.47   thorpej 
   1520   1.45   thorpej 	mediaopt_set = get_media_options(IFM_TYPE(media_current), val);
   1521   1.24   thorpej 
   1522   1.45   thorpej 	/* Media will be set after other processing is complete. */
   1523   1.45   thorpej }
   1524   1.24   thorpej 
   1525   1.45   thorpej void
   1526   1.45   thorpej unsetmediaopt(val, d)
   1527   1.80   thorpej 	const char *val;
   1528   1.45   thorpej 	int d;
   1529   1.45   thorpej {
   1530   1.26   thorpej 
   1531   1.45   thorpej 	init_current_media();
   1532   1.26   thorpej 
   1533   1.45   thorpej 	/* Can only issue `-mediaopt' once. */
   1534   1.46   thorpej 	if (actions & A_MEDIAOPTCLR)
   1535   1.45   thorpej 		errx(1, "only one `-mediaopt' command may be issued");
   1536   1.26   thorpej 
   1537   1.45   thorpej 	/* May not issue `media' and `-mediaopt'. */
   1538   1.46   thorpej 	if (actions & A_MEDIA)
   1539   1.45   thorpej 		errx(1, "may not issue both `media' and `-mediaopt'");
   1540   1.24   thorpej 
   1541   1.47   thorpej 	/*
   1542   1.47   thorpej 	 * No need to check for A_MEDIAINST, since the test for A_MEDIA
   1543   1.47   thorpej 	 * implicitly checks for A_MEDIAINST.
   1544   1.47   thorpej 	 */
   1545   1.47   thorpej 
   1546   1.45   thorpej 	mediaopt_clear = get_media_options(IFM_TYPE(media_current), val);
   1547   1.24   thorpej 
   1548   1.45   thorpej 	/* Media will be set after other processing is complete. */
   1549   1.24   thorpej }
   1550   1.24   thorpej 
   1551   1.47   thorpej void
   1552   1.47   thorpej setmediainst(val, d)
   1553   1.80   thorpej 	const char *val;
   1554   1.47   thorpej 	int d;
   1555   1.47   thorpej {
   1556   1.47   thorpej 	int type, subtype, options, inst;
   1557   1.47   thorpej 
   1558   1.47   thorpej 	init_current_media();
   1559   1.47   thorpej 
   1560   1.47   thorpej 	/* Can only issue `instance' once. */
   1561   1.47   thorpej 	if (actions & A_MEDIAINST)
   1562   1.47   thorpej 		errx(1, "only one `instance' command may be issued");
   1563   1.47   thorpej 
   1564   1.47   thorpej 	/* Must have already specified `media' */
   1565   1.47   thorpej 	if ((actions & A_MEDIA) == 0)
   1566   1.47   thorpej 		errx(1, "must specify `media' before `instance'");
   1567   1.47   thorpej 
   1568   1.47   thorpej 	type = IFM_TYPE(media_current);
   1569   1.47   thorpej 	subtype = IFM_SUBTYPE(media_current);
   1570   1.47   thorpej 	options = IFM_OPTIONS(media_current);
   1571   1.47   thorpej 
   1572   1.47   thorpej 	inst = atoi(val);
   1573   1.47   thorpej 	if (inst < 0 || inst > IFM_INST_MAX)
   1574   1.47   thorpej 		errx(1, "invalid media instance: %s", val);
   1575   1.47   thorpej 
   1576   1.47   thorpej 	media_current = IFM_MAKEWORD(type, subtype, options, inst);
   1577   1.47   thorpej 
   1578   1.47   thorpej 	/* Media will be set after other processing is complete. */
   1579   1.47   thorpej }
   1580   1.47   thorpej 
   1581   1.24   thorpej struct ifmedia_description ifm_type_descriptions[] =
   1582   1.24   thorpej     IFM_TYPE_DESCRIPTIONS;
   1583   1.24   thorpej 
   1584   1.42   thorpej struct ifmedia_description ifm_subtype_descriptions[] =
   1585   1.42   thorpej     IFM_SUBTYPE_DESCRIPTIONS;
   1586   1.24   thorpej 
   1587   1.42   thorpej struct ifmedia_description ifm_option_descriptions[] =
   1588   1.42   thorpej     IFM_OPTION_DESCRIPTIONS;
   1589   1.24   thorpej 
   1590   1.42   thorpej const char *
   1591   1.42   thorpej get_media_type_string(mword)
   1592   1.42   thorpej 	int mword;
   1593   1.42   thorpej {
   1594   1.42   thorpej 	struct ifmedia_description *desc;
   1595   1.24   thorpej 
   1596   1.42   thorpej 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
   1597   1.42   thorpej 	     desc++) {
   1598   1.42   thorpej 		if (IFM_TYPE(mword) == desc->ifmt_word)
   1599   1.42   thorpej 			return (desc->ifmt_string);
   1600   1.42   thorpej 	}
   1601   1.42   thorpej 	return ("<unknown type>");
   1602   1.42   thorpej }
   1603   1.24   thorpej 
   1604   1.42   thorpej const char *
   1605   1.42   thorpej get_media_subtype_string(mword)
   1606   1.42   thorpej 	int mword;
   1607   1.42   thorpej {
   1608   1.42   thorpej 	struct ifmedia_description *desc;
   1609   1.24   thorpej 
   1610   1.42   thorpej 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
   1611   1.42   thorpej 	     desc++) {
   1612   1.42   thorpej 		if (IFM_TYPE_MATCH(desc->ifmt_word, mword) &&
   1613   1.42   thorpej 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(mword))
   1614   1.42   thorpej 			return (desc->ifmt_string);
   1615   1.42   thorpej 	}
   1616   1.42   thorpej 	return ("<unknown subtype>");
   1617   1.42   thorpej }
   1618   1.24   thorpej 
   1619   1.24   thorpej int
   1620   1.24   thorpej get_media_subtype(type, val)
   1621   1.24   thorpej 	int type;
   1622   1.42   thorpej 	const char *val;
   1623   1.24   thorpej {
   1624   1.42   thorpej 	int rval;
   1625   1.24   thorpej 
   1626   1.42   thorpej 	rval = lookup_media_word(ifm_subtype_descriptions, type, val);
   1627   1.42   thorpej 	if (rval == -1)
   1628   1.42   thorpej 		errx(1, "unknown %s media subtype: %s",
   1629   1.42   thorpej 		    get_media_type_string(type), val);
   1630   1.24   thorpej 
   1631   1.42   thorpej 	return (rval);
   1632   1.24   thorpej }
   1633   1.24   thorpej 
   1634   1.24   thorpej int
   1635   1.24   thorpej get_media_options(type, val)
   1636   1.24   thorpej 	int type;
   1637   1.42   thorpej 	const char *val;
   1638   1.24   thorpej {
   1639   1.42   thorpej 	char *optlist, *str;
   1640   1.42   thorpej 	int option, rval = 0;
   1641   1.24   thorpej 
   1642   1.24   thorpej 	/* We muck with the string, so copy it. */
   1643   1.24   thorpej 	optlist = strdup(val);
   1644   1.24   thorpej 	if (optlist == NULL)
   1645   1.24   thorpej 		err(1, "strdup");
   1646   1.42   thorpej 	str = optlist;
   1647   1.24   thorpej 
   1648   1.24   thorpej 	/*
   1649   1.42   thorpej 	 * Look up the options in the user-provided comma-separated list.
   1650   1.24   thorpej 	 */
   1651   1.42   thorpej 	for (; (str = strtok(str, ",")) != NULL; str = NULL) {
   1652   1.42   thorpej 		option = lookup_media_word(ifm_option_descriptions, type, str);
   1653   1.42   thorpej 		if (option == -1)
   1654   1.42   thorpej 			errx(1, "unknown %s media option: %s",
   1655   1.42   thorpej 			    get_media_type_string(type), str);
   1656   1.61   thorpej 		rval |= IFM_OPTIONS(option);
   1657   1.24   thorpej 	}
   1658   1.24   thorpej 
   1659   1.24   thorpej 	free(optlist);
   1660   1.24   thorpej 	return (rval);
   1661   1.24   thorpej }
   1662   1.24   thorpej 
   1663   1.24   thorpej int
   1664   1.42   thorpej lookup_media_word(desc, type, val)
   1665   1.24   thorpej 	struct ifmedia_description *desc;
   1666   1.42   thorpej 	int type;
   1667   1.42   thorpej 	const char *val;
   1668   1.24   thorpej {
   1669   1.24   thorpej 
   1670   1.42   thorpej 	for (; desc->ifmt_string != NULL; desc++) {
   1671   1.42   thorpej 		if (IFM_TYPE_MATCH(desc->ifmt_word, type) &&
   1672   1.42   thorpej 		    strcasecmp(desc->ifmt_string, val) == 0)
   1673   1.24   thorpej 			return (desc->ifmt_word);
   1674   1.42   thorpej 	}
   1675   1.27   thorpej 	return (-1);
   1676   1.24   thorpej }
   1677   1.24   thorpej 
   1678   1.24   thorpej void
   1679   1.44   thorpej print_media_word(ifmw, print_type, as_syntax)
   1680   1.44   thorpej 	int ifmw, print_type, as_syntax;
   1681   1.24   thorpej {
   1682   1.24   thorpej 	struct ifmedia_description *desc;
   1683   1.42   thorpej 	int seen_option = 0;
   1684   1.24   thorpej 
   1685   1.42   thorpej 	if (print_type)
   1686   1.42   thorpej 		printf("%s ", get_media_type_string(ifmw));
   1687   1.44   thorpej 	printf("%s%s", as_syntax ? "media " : "",
   1688   1.44   thorpej 	    get_media_subtype_string(ifmw));
   1689   1.24   thorpej 
   1690   1.24   thorpej 	/* Find options. */
   1691   1.42   thorpej 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
   1692   1.42   thorpej 	     desc++) {
   1693   1.42   thorpej 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
   1694   1.67   mycroft 		    (ifmw & IFM_OPTIONS(desc->ifmt_word)) != 0 &&
   1695   1.42   thorpej 		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
   1696   1.42   thorpej 			if (seen_option == 0)
   1697   1.44   thorpej 				printf(" %s", as_syntax ? "mediaopt " : "");
   1698   1.42   thorpej 			printf("%s%s", seen_option ? "," : "",
   1699   1.42   thorpej 			    desc->ifmt_string);
   1700   1.42   thorpej 			seen_option |= IFM_OPTIONS(desc->ifmt_word);
   1701   1.24   thorpej 		}
   1702   1.24   thorpej 	}
   1703   1.43   thorpej 	if (IFM_INST(ifmw) != 0)
   1704   1.47   thorpej 		printf(" instance %d", IFM_INST(ifmw));
   1705   1.24   thorpej }
   1706   1.54  sommerfe 
   1707   1.54  sommerfe int carrier()
   1708   1.54  sommerfe {
   1709   1.54  sommerfe 	struct ifmediareq ifmr;
   1710   1.54  sommerfe 
   1711   1.54  sommerfe 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1712   1.54  sommerfe 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1713   1.54  sommerfe 
   1714   1.54  sommerfe 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
   1715   1.54  sommerfe 		/*
   1716   1.54  sommerfe 		 * Interface doesn't support SIOC{G,S}IFMEDIA;
   1717   1.54  sommerfe 		 * assume ok.
   1718   1.54  sommerfe 		 */
   1719   1.54  sommerfe 		return 0;
   1720   1.54  sommerfe 	}
   1721   1.54  sommerfe 	if ((ifmr.ifm_status & IFM_AVALID) == 0) {
   1722   1.54  sommerfe 		/*
   1723   1.54  sommerfe 		 * Interface doesn't report media-valid status.
   1724   1.54  sommerfe 		 * assume ok.
   1725   1.54  sommerfe 		 */
   1726   1.54  sommerfe 		return 0;
   1727   1.54  sommerfe 	}
   1728   1.54  sommerfe 	/* otherwise, return ok for active, not-ok if not active. */
   1729   1.54  sommerfe 	return !(ifmr.ifm_status & IFM_ACTIVE);
   1730   1.54  sommerfe }
   1731   1.54  sommerfe 
   1732   1.24   thorpej 
   1733    1.1       cgd #define	IFFBITS \
   1734   1.13   mycroft "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
   1735   1.13   mycroft \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
   1736    1.1       cgd 
   1737   1.63   thorpej const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
   1738   1.63   thorpej 
   1739   1.63   thorpej const struct ifmedia_status_description ifm_status_descriptions[] =
   1740   1.63   thorpej     IFM_STATUS_DESCRIPTIONS;
   1741   1.63   thorpej 
   1742    1.1       cgd /*
   1743    1.1       cgd  * Print the status of the interface.  If an address family was
   1744    1.1       cgd  * specified, show it and it only; otherwise, show them all.
   1745    1.1       cgd  */
   1746   1.16       cgd void
   1747   1.30   thorpej status(ap, alen)
   1748   1.30   thorpej 	const u_int8_t *ap;
   1749   1.30   thorpej 	int alen;
   1750    1.1       cgd {
   1751   1.39     lukem 	struct afswtch *p = afp;
   1752   1.24   thorpej 	struct ifmediareq ifmr;
   1753   1.24   thorpej 	int *media_list, i;
   1754    1.1       cgd 
   1755    1.1       cgd 	printf("%s: ", name);
   1756    1.1       cgd 	printb("flags", flags, IFFBITS);
   1757    1.1       cgd 	if (metric)
   1758    1.1       cgd 		printf(" metric %d", metric);
   1759   1.33        is 	if (mtu)
   1760   1.33        is 		printf(" mtu %d", mtu);
   1761    1.1       cgd 	putchar('\n');
   1762   1.65   thorpej 
   1763   1.65   thorpej 	ieee80211_status();
   1764   1.89   thorpej 	vlan_status();
   1765   1.80   thorpej 	tunnel_status();
   1766   1.65   thorpej 
   1767   1.30   thorpej 	if (ap && alen > 0) {
   1768   1.30   thorpej 		printf("\taddress:");
   1769   1.30   thorpej 		for (i = 0; i < alen; i++, ap++)
   1770   1.30   thorpej 			printf("%c%02x", i > 0 ? ':' : ' ', *ap);
   1771   1.30   thorpej 		putchar('\n');
   1772   1.30   thorpej 	}
   1773   1.24   thorpej 
   1774   1.32  christos 	(void) memset(&ifmr, 0, sizeof(ifmr));
   1775   1.32  christos 	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
   1776   1.24   thorpej 
   1777   1.24   thorpej 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
   1778   1.24   thorpej 		/*
   1779   1.24   thorpej 		 * Interface doesn't support SIOC{G,S}IFMEDIA.
   1780   1.24   thorpej 		 */
   1781   1.24   thorpej 		goto proto_status;
   1782   1.24   thorpej 	}
   1783   1.24   thorpej 
   1784   1.24   thorpej 	if (ifmr.ifm_count == 0) {
   1785   1.29   thorpej 		warnx("%s: no media types?", name);
   1786   1.24   thorpej 		goto proto_status;
   1787   1.24   thorpej 	}
   1788   1.24   thorpej 
   1789   1.24   thorpej 	media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
   1790   1.24   thorpej 	if (media_list == NULL)
   1791   1.24   thorpej 		err(1, "malloc");
   1792   1.24   thorpej 	ifmr.ifm_ulist = media_list;
   1793   1.24   thorpej 
   1794   1.24   thorpej 	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
   1795   1.24   thorpej 		err(1, "SIOCGIFMEDIA");
   1796   1.24   thorpej 
   1797   1.24   thorpej 	printf("\tmedia: ");
   1798   1.44   thorpej 	print_media_word(ifmr.ifm_current, 1, 0);
   1799   1.24   thorpej 	if (ifmr.ifm_active != ifmr.ifm_current) {
   1800   1.28   thorpej 		putchar(' ');
   1801   1.24   thorpej 		putchar('(');
   1802   1.44   thorpej 		print_media_word(ifmr.ifm_active, 0, 0);
   1803   1.24   thorpej 		putchar(')');
   1804   1.24   thorpej 	}
   1805   1.42   thorpej 	putchar('\n');
   1806   1.24   thorpej 
   1807   1.63   thorpej 	if (ifmr.ifm_status & IFM_STATUS_VALID) {
   1808   1.63   thorpej 		const struct ifmedia_status_description *ifms;
   1809   1.63   thorpej 		int bitno, found = 0;
   1810   1.63   thorpej 
   1811   1.42   thorpej 		printf("\tstatus: ");
   1812   1.63   thorpej 		for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
   1813   1.63   thorpej 			for (ifms = ifm_status_descriptions;
   1814   1.63   thorpej 			     ifms->ifms_valid != 0; ifms++) {
   1815   1.63   thorpej 				if (ifms->ifms_type !=
   1816   1.66   thorpej 				      IFM_TYPE(ifmr.ifm_current) ||
   1817   1.63   thorpej 				    ifms->ifms_valid !=
   1818   1.63   thorpej 				      ifm_status_valid_list[bitno])
   1819   1.63   thorpej 					continue;
   1820   1.63   thorpej 				printf("%s%s", found ? ", " : "",
   1821   1.63   thorpej 				    IFM_STATUS_DESC(ifms, ifmr.ifm_status));
   1822   1.63   thorpej 				found = 1;
   1823   1.63   thorpej 
   1824   1.63   thorpej 				/*
   1825   1.63   thorpej 				 * For each valid indicator bit, there's
   1826   1.63   thorpej 				 * only one entry for each media type, so
   1827   1.63   thorpej 				 * terminate the inner loop now.
   1828   1.63   thorpej 				 */
   1829   1.63   thorpej 				break;
   1830   1.63   thorpej 			}
   1831   1.63   thorpej 		}
   1832   1.24   thorpej 
   1833   1.63   thorpej 		if (found == 0)
   1834   1.42   thorpej 			printf("unknown");
   1835   1.42   thorpej 		putchar('\n');
   1836   1.24   thorpej 	}
   1837   1.24   thorpej 
   1838   1.24   thorpej 	if (mflag) {
   1839   1.44   thorpej 		int type, printed_type;
   1840   1.44   thorpej 
   1841   1.44   thorpej 		for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
   1842   1.44   thorpej 			for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
   1843   1.44   thorpej 				if (IFM_TYPE(media_list[i]) == type) {
   1844   1.44   thorpej 					if (printed_type == 0) {
   1845   1.44   thorpej 					    printf("\tsupported %s media:\n",
   1846   1.44   thorpej 					      get_media_type_string(type));
   1847   1.44   thorpej 					    printed_type = 1;
   1848   1.44   thorpej 					}
   1849   1.44   thorpej 					printf("\t\t");
   1850   1.44   thorpej 					print_media_word(media_list[i], 0, 1);
   1851   1.44   thorpej 					printf("\n");
   1852   1.44   thorpej 				}
   1853   1.44   thorpej 			}
   1854   1.24   thorpej 		}
   1855   1.24   thorpej 	}
   1856   1.24   thorpej 
   1857   1.24   thorpej 	free(media_list);
   1858   1.24   thorpej 
   1859   1.24   thorpej  proto_status:
   1860    1.1       cgd 	if ((p = afp) != NULL) {
   1861    1.1       cgd 		(*p->af_status)(1);
   1862    1.1       cgd 	} else for (p = afs; p->af_name; p++) {
   1863    1.1       cgd 		ifr.ifr_addr.sa_family = p->af_af;
   1864    1.1       cgd 		(*p->af_status)(0);
   1865   1.49  christos 	}
   1866   1.49  christos }
   1867   1.49  christos 
   1868   1.49  christos void
   1869   1.80   thorpej tunnel_status()
   1870   1.80   thorpej {
   1871   1.80   thorpej 	char psrcaddr[NI_MAXHOST];
   1872   1.80   thorpej 	char pdstaddr[NI_MAXHOST];
   1873   1.80   thorpej 	const char *ver = "";
   1874   1.80   thorpej #ifdef NI_WITHSCOPEID
   1875   1.80   thorpej 	const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
   1876   1.80   thorpej #else
   1877   1.80   thorpej 	const int niflag = NI_NUMERICHOST;
   1878   1.80   thorpej #endif
   1879  1.100    itojun 	struct if_laddrreq req;
   1880   1.80   thorpej 
   1881   1.80   thorpej 	psrcaddr[0] = pdstaddr[0] = '\0';
   1882   1.80   thorpej 
   1883  1.100    itojun 	memset(&req, 0, sizeof(req));
   1884  1.100    itojun 	strncpy(req.iflr_name, name, IFNAMSIZ);
   1885  1.100    itojun 	if (ioctl(s, SIOCGLIFPHYADDR, (caddr_t)&req) < 0)
   1886   1.80   thorpej 		return;
   1887   1.80   thorpej #ifdef INET6
   1888  1.100    itojun 	if (req.addr.ss_family == AF_INET6)
   1889  1.100    itojun 		in6_fillscopeid((struct sockaddr_in6 *)&req.addr);
   1890   1.80   thorpej #endif
   1891  1.100    itojun 	getnameinfo((struct sockaddr *)&req.addr, req.addr.ss_len,
   1892   1.80   thorpej 	    psrcaddr, sizeof(psrcaddr), 0, 0, niflag);
   1893   1.80   thorpej #ifdef INET6
   1894  1.100    itojun 	if (req.addr.ss_family == AF_INET6)
   1895   1.80   thorpej 		ver = "6";
   1896   1.80   thorpej #endif
   1897   1.80   thorpej 
   1898   1.80   thorpej #ifdef INET6
   1899  1.100    itojun 	if (req.dstaddr.ss_family == AF_INET6)
   1900  1.100    itojun 		in6_fillscopeid((struct sockaddr_in6 *)&req.dstaddr);
   1901   1.80   thorpej #endif
   1902  1.100    itojun 	getnameinfo((struct sockaddr *)&req.dstaddr, req.dstaddr.ss_len,
   1903   1.80   thorpej 	    pdstaddr, sizeof(pdstaddr), 0, 0, niflag);
   1904   1.80   thorpej 
   1905  1.100    itojun 	printf("\ttunnel inet%s %s --> %s\n", ver, psrcaddr, pdstaddr);
   1906   1.80   thorpej }
   1907   1.80   thorpej 
   1908   1.80   thorpej void
   1909   1.89   thorpej vlan_status()
   1910   1.89   thorpej {
   1911   1.89   thorpej 	struct vlanreq vlr;
   1912   1.89   thorpej 
   1913   1.89   thorpej 	if (strncmp(ifr.ifr_name, "vlan", 4) != 0 ||
   1914   1.89   thorpej 	    !isdigit(ifr.ifr_name[4]))
   1915   1.89   thorpej 		return;
   1916   1.89   thorpej 
   1917   1.89   thorpej 	memset(&vlr, 0, sizeof(vlr));
   1918   1.89   thorpej 	ifr.ifr_data = (caddr_t)&vlr;
   1919   1.89   thorpej 
   1920   1.89   thorpej 	if (ioctl(s, SIOCGETVLAN, (caddr_t)&ifr) == -1)
   1921   1.89   thorpej 		return;
   1922   1.89   thorpej 
   1923   1.89   thorpej 	if (vlr.vlr_tag || vlr.vlr_parent[0] != '\0')
   1924   1.89   thorpej 		printf("\tvlan: %d parent: %s\n",
   1925   1.89   thorpej 		    vlr.vlr_tag, vlr.vlr_parent[0] == '\0' ?
   1926   1.89   thorpej 		    "<none>" : vlr.vlr_parent);
   1927   1.89   thorpej }
   1928   1.89   thorpej 
   1929   1.89   thorpej void
   1930   1.49  christos in_alias(creq)
   1931   1.49  christos 	struct ifreq *creq;
   1932   1.49  christos {
   1933   1.49  christos 	struct sockaddr_in *sin;
   1934   1.77    itojun 	int alias;
   1935   1.49  christos 
   1936   1.49  christos 	if (lflag)
   1937   1.49  christos 		return;
   1938   1.49  christos 
   1939   1.77    itojun 	alias = 1;
   1940   1.77    itojun 
   1941   1.49  christos 	/* Get the non-alias address for this interface. */
   1942   1.49  christos 	getsock(AF_INET);
   1943   1.49  christos 	if (s < 0) {
   1944   1.49  christos 		if (errno == EPROTONOSUPPORT)
   1945   1.49  christos 			return;
   1946   1.49  christos 		err(1, "socket");
   1947   1.49  christos 	}
   1948   1.49  christos 	(void) memset(&ifr, 0, sizeof(ifr));
   1949   1.49  christos 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   1950   1.49  christos 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   1951   1.49  christos 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1952   1.49  christos 			return;
   1953   1.49  christos 		} else
   1954   1.49  christos 			warn("SIOCGIFADDR");
   1955   1.49  christos 	}
   1956   1.49  christos 	/* If creq and ifr are the same address, this is not an alias. */
   1957   1.49  christos 	if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
   1958   1.49  christos 		   sizeof(creq->ifr_addr)) == 0)
   1959   1.77    itojun 		alias = 0;
   1960   1.49  christos 	(void) memset(&addreq, 0, sizeof(addreq));
   1961   1.49  christos 	(void) strncpy(addreq.ifra_name, name, sizeof(addreq.ifra_name));
   1962   1.49  christos 	addreq.ifra_addr = creq->ifr_addr;
   1963   1.49  christos 	if (ioctl(s, SIOCGIFALIAS, (caddr_t)&addreq) < 0) {
   1964   1.49  christos 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   1965   1.49  christos 			return;
   1966   1.49  christos 		} else
   1967   1.49  christos 			warn("SIOCGIFALIAS");
   1968   1.49  christos 	}
   1969   1.49  christos 
   1970   1.49  christos 	sin = (struct sockaddr_in *)&addreq.ifra_addr;
   1971   1.77    itojun 	printf("\tinet %s%s", alias ? "alias " : "", inet_ntoa(sin->sin_addr));
   1972   1.49  christos 
   1973   1.49  christos 	if (flags & IFF_POINTOPOINT) {
   1974   1.49  christos 		sin = (struct sockaddr_in *)&addreq.ifra_dstaddr;
   1975   1.49  christos 		printf(" -> %s", inet_ntoa(sin->sin_addr));
   1976   1.49  christos 	}
   1977   1.49  christos 
   1978   1.49  christos 	sin = (struct sockaddr_in *)&addreq.ifra_mask;
   1979   1.49  christos 	printf(" netmask 0x%x", ntohl(sin->sin_addr.s_addr));
   1980   1.49  christos 
   1981   1.49  christos 	if (flags & IFF_BROADCAST) {
   1982   1.49  christos 		sin = (struct sockaddr_in *)&addreq.ifra_broadaddr;
   1983   1.49  christos 		printf(" broadcast %s", inet_ntoa(sin->sin_addr));
   1984    1.1       cgd 	}
   1985   1.49  christos 	printf("\n");
   1986    1.1       cgd }
   1987    1.1       cgd 
   1988   1.16       cgd void
   1989    1.1       cgd in_status(force)
   1990    1.1       cgd 	int force;
   1991    1.1       cgd {
   1992   1.78    itojun 	struct ifaddrs *ifap, *ifa;
   1993   1.78    itojun 	struct ifreq ifr;
   1994   1.78    itojun 
   1995   1.78    itojun 	if (getifaddrs(&ifap) != 0)
   1996   1.78    itojun 		err(1, "getifaddrs");
   1997   1.78    itojun 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
   1998   1.78    itojun 		if (strcmp(name, ifa->ifa_name) != 0)
   1999   1.78    itojun 			continue;
   2000   1.78    itojun 		if (ifa->ifa_addr->sa_family != AF_INET)
   2001   1.78    itojun 			continue;
   2002   1.78    itojun 		if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
   2003   1.78    itojun 			continue;
   2004   1.78    itojun 
   2005   1.78    itojun 		memset(&ifr, 0, sizeof(ifr));
   2006   1.78    itojun 		strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
   2007   1.78    itojun 		memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
   2008   1.78    itojun 		in_alias(&ifr);
   2009   1.78    itojun 	}
   2010   1.78    itojun 	freeifaddrs(ifap);
   2011    1.1       cgd }
   2012    1.1       cgd 
   2013   1.53    itojun void
   2014   1.53    itojun setifprefixlen(addr, d)
   2015   1.80   thorpej 	const char *addr;
   2016   1.53    itojun 	int d;
   2017   1.53    itojun {
   2018   1.53    itojun 	if (*afp->af_getprefix)
   2019   1.53    itojun 		(*afp->af_getprefix)(addr, MASK);
   2020   1.53    itojun 	explicit_prefix = 1;
   2021   1.53    itojun }
   2022   1.53    itojun 
   2023   1.53    itojun #ifdef INET6
   2024   1.59    itojun void
   2025   1.59    itojun in6_fillscopeid(sin6)
   2026   1.59    itojun 	struct sockaddr_in6 *sin6;
   2027   1.59    itojun {
   2028   1.59    itojun #if defined(__KAME__) && defined(KAME_SCOPEID)
   2029   1.59    itojun 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   2030   1.59    itojun 		sin6->sin6_scope_id =
   2031   1.59    itojun 			ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
   2032   1.59    itojun 		sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
   2033   1.59    itojun 	}
   2034   1.59    itojun #endif
   2035   1.59    itojun }
   2036   1.59    itojun 
   2037   1.53    itojun /* XXX not really an alias */
   2038   1.53    itojun void
   2039   1.53    itojun in6_alias(creq)
   2040   1.53    itojun 	struct in6_ifreq *creq;
   2041   1.53    itojun {
   2042   1.53    itojun 	struct sockaddr_in6 *sin6;
   2043   1.59    itojun 	char hbuf[NI_MAXHOST];
   2044   1.59    itojun 	u_int32_t scopeid;
   2045   1.59    itojun #ifdef NI_WITHSCOPEID
   2046   1.59    itojun 	const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
   2047   1.59    itojun #else
   2048   1.59    itojun 	const int niflag = NI_NUMERICHOST;
   2049   1.59    itojun #endif
   2050   1.53    itojun 
   2051   1.53    itojun 	/* Get the non-alias address for this interface. */
   2052   1.53    itojun 	getsock(AF_INET6);
   2053   1.53    itojun 	if (s < 0) {
   2054   1.53    itojun 		if (errno == EPROTONOSUPPORT)
   2055   1.53    itojun 			return;
   2056   1.53    itojun 		err(1, "socket");
   2057   1.53    itojun 	}
   2058   1.53    itojun 
   2059   1.53    itojun 	sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
   2060   1.53    itojun 
   2061   1.59    itojun 	in6_fillscopeid(sin6);
   2062   1.59    itojun 	scopeid = sin6->sin6_scope_id;
   2063   1.59    itojun 	if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
   2064   1.59    itojun 			hbuf, sizeof(hbuf), NULL, 0, niflag))
   2065   1.59    itojun 		strncpy(hbuf, "", sizeof(hbuf));	/* some message? */
   2066   1.59    itojun 	printf("\tinet6 %s", hbuf);
   2067   1.53    itojun 
   2068   1.53    itojun 	if (flags & IFF_POINTOPOINT) {
   2069   1.53    itojun 		(void) memset(&ifr6, 0, sizeof(ifr6));
   2070   1.53    itojun 		(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   2071   1.53    itojun 		ifr6.ifr_addr = creq->ifr_addr;
   2072   1.53    itojun 		if (ioctl(s, SIOCGIFDSTADDR_IN6, (caddr_t)&ifr6) < 0) {
   2073   1.53    itojun 			if (errno != EADDRNOTAVAIL)
   2074   1.53    itojun 				warn("SIOCGIFDSTADDR_IN6");
   2075   1.53    itojun 			(void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
   2076   1.53    itojun 			ifr6.ifr_addr.sin6_family = AF_INET6;
   2077   1.53    itojun 			ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
   2078   1.53    itojun 		}
   2079   1.53    itojun 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
   2080   1.59    itojun 		in6_fillscopeid(sin6);
   2081   1.59    itojun 		hbuf[0] = '\0';
   2082   1.59    itojun 		if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
   2083   1.59    itojun 				hbuf, sizeof(hbuf), NULL, 0, niflag))
   2084   1.59    itojun 			strncpy(hbuf, "", sizeof(hbuf)); /* some message? */
   2085   1.59    itojun 		printf(" -> %s", hbuf);
   2086   1.53    itojun 	}
   2087   1.53    itojun 
   2088   1.53    itojun 	(void) memset(&ifr6, 0, sizeof(ifr6));
   2089   1.53    itojun 	(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   2090   1.53    itojun 	ifr6.ifr_addr = creq->ifr_addr;
   2091   1.53    itojun 	if (ioctl(s, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) {
   2092   1.53    itojun 		if (errno != EADDRNOTAVAIL)
   2093   1.53    itojun 			warn("SIOCGIFNETMASK_IN6");
   2094   1.53    itojun 	} else {
   2095   1.53    itojun 		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
   2096   1.53    itojun 		printf(" prefixlen %d", prefix(&sin6->sin6_addr,
   2097   1.53    itojun 					       sizeof(struct in6_addr)));
   2098   1.53    itojun 	}
   2099   1.53    itojun 
   2100   1.53    itojun 	(void) memset(&ifr6, 0, sizeof(ifr6));
   2101   1.53    itojun 	(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   2102   1.53    itojun 	ifr6.ifr_addr = creq->ifr_addr;
   2103   1.53    itojun 	if (ioctl(s, SIOCGIFAFLAG_IN6, (caddr_t)&ifr6) < 0) {
   2104   1.53    itojun 		if (errno != EADDRNOTAVAIL)
   2105   1.53    itojun 			warn("SIOCGIFAFLAG_IN6");
   2106   1.53    itojun 	} else {
   2107   1.53    itojun 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
   2108   1.53    itojun 			printf(" anycast");
   2109   1.53    itojun 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
   2110   1.53    itojun 			printf(" tentative");
   2111   1.53    itojun 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
   2112   1.53    itojun 			printf(" duplicated");
   2113   1.53    itojun 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
   2114   1.53    itojun 			printf(" detached");
   2115   1.91    itojun 		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
   2116   1.91    itojun 			printf(" deprecated");
   2117   1.53    itojun 	}
   2118   1.53    itojun 
   2119   1.59    itojun 	if (scopeid)
   2120   1.59    itojun 		printf(" scopeid 0x%x", scopeid);
   2121   1.59    itojun 
   2122   1.53    itojun 	if (Lflag) {
   2123   1.53    itojun 		struct in6_addrlifetime *lifetime;
   2124   1.53    itojun 		(void) memset(&ifr6, 0, sizeof(ifr6));
   2125   1.53    itojun 		(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
   2126   1.53    itojun 		ifr6.ifr_addr = creq->ifr_addr;
   2127   1.53    itojun 		lifetime = &ifr6.ifr_ifru.ifru_lifetime;
   2128   1.53    itojun 		if (ioctl(s, SIOCGIFALIFETIME_IN6, (caddr_t)&ifr6) < 0) {
   2129   1.53    itojun 			if (errno != EADDRNOTAVAIL)
   2130   1.53    itojun 				warn("SIOCGIFALIFETIME_IN6");
   2131   1.53    itojun 		} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
   2132   1.53    itojun 			time_t t = time(NULL);
   2133   1.53    itojun 			printf(" pltime ");
   2134   1.53    itojun 			if (lifetime->ia6t_preferred) {
   2135   1.53    itojun 				printf("%s", lifetime->ia6t_preferred < t
   2136   1.53    itojun 					? "0"
   2137   1.53    itojun 					: sec2str(lifetime->ia6t_preferred - t));
   2138   1.53    itojun 			} else
   2139   1.53    itojun 				printf("infty");
   2140   1.53    itojun 
   2141   1.53    itojun 			printf(" vltime ");
   2142   1.53    itojun 			if (lifetime->ia6t_expire) {
   2143   1.53    itojun 				printf("%s", lifetime->ia6t_expire < t
   2144   1.53    itojun 					? "0"
   2145   1.53    itojun 					: sec2str(lifetime->ia6t_expire - t));
   2146   1.53    itojun 			} else
   2147   1.53    itojun 				printf("infty");
   2148   1.53    itojun 		}
   2149   1.53    itojun 	}
   2150   1.53    itojun 
   2151   1.53    itojun 	printf("\n");
   2152   1.53    itojun }
   2153   1.53    itojun 
   2154   1.53    itojun void
   2155   1.53    itojun in6_status(force)
   2156   1.53    itojun 	int force;
   2157   1.53    itojun {
   2158   1.78    itojun 	struct ifaddrs *ifap, *ifa;
   2159   1.78    itojun 	struct in6_ifreq ifr;
   2160   1.78    itojun 
   2161   1.78    itojun 	if (getifaddrs(&ifap) != 0)
   2162   1.78    itojun 		err(1, "getifaddrs");
   2163   1.78    itojun 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
   2164   1.78    itojun 		if (strcmp(name, ifa->ifa_name) != 0)
   2165   1.78    itojun 			continue;
   2166   1.78    itojun 		if (ifa->ifa_addr->sa_family != AF_INET6)
   2167   1.78    itojun 			continue;
   2168   1.78    itojun 		if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
   2169   1.78    itojun 			continue;
   2170   1.78    itojun 
   2171   1.78    itojun 		memset(&ifr, 0, sizeof(ifr));
   2172   1.78    itojun 		strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
   2173   1.78    itojun 		memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
   2174   1.78    itojun 		in6_alias(&ifr);
   2175   1.78    itojun 	}
   2176   1.78    itojun 	freeifaddrs(ifap);
   2177   1.53    itojun }
   2178   1.53    itojun #endif /*INET6*/
   2179   1.53    itojun 
   2180   1.21       gwr #ifndef INET_ONLY
   2181   1.21       gwr 
   2182   1.16       cgd void
   2183   1.32  christos at_status(force)
   2184   1.32  christos 	int force;
   2185   1.32  christos {
   2186   1.32  christos 	struct sockaddr_at *sat, null_sat;
   2187   1.32  christos 	struct netrange *nr;
   2188   1.32  christos 
   2189   1.32  christos 	getsock(AF_APPLETALK);
   2190   1.32  christos 	if (s < 0) {
   2191   1.32  christos 		if (errno == EPROTONOSUPPORT)
   2192   1.32  christos 			return;
   2193   1.32  christos 		err(1, "socket");
   2194   1.32  christos 	}
   2195   1.32  christos 	(void) memset(&ifr, 0, sizeof(ifr));
   2196   1.32  christos 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   2197   1.32  christos 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   2198   1.32  christos 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2199   1.32  christos 			if (!force)
   2200   1.32  christos 				return;
   2201   1.32  christos 			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   2202   1.32  christos 		} else
   2203   1.32  christos 			warn("SIOCGIFADDR");
   2204   1.32  christos 	}
   2205   1.32  christos 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   2206   1.32  christos 	sat = (struct sockaddr_at *)&ifr.ifr_addr;
   2207   1.32  christos 
   2208   1.32  christos 	(void) memset(&null_sat, 0, sizeof(null_sat));
   2209   1.32  christos 
   2210   1.32  christos 	nr = (struct netrange *) &sat->sat_zero;
   2211   1.32  christos 	printf("\tatalk %d.%d range %d-%d phase %d",
   2212   1.32  christos 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
   2213   1.32  christos 	    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
   2214   1.32  christos 	if (flags & IFF_POINTOPOINT) {
   2215   1.32  christos 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   2216   1.32  christos 			if (errno == EADDRNOTAVAIL)
   2217   1.32  christos 			    (void) memset(&ifr.ifr_addr, 0,
   2218   1.32  christos 				sizeof(ifr.ifr_addr));
   2219   1.32  christos 			else
   2220   1.32  christos 			    warn("SIOCGIFDSTADDR");
   2221   1.32  christos 		}
   2222   1.32  christos 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   2223   1.32  christos 		sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
   2224   1.32  christos 		if (!sat)
   2225   1.32  christos 			sat = &null_sat;
   2226   1.32  christos 		printf("--> %d.%d",
   2227   1.32  christos 		    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
   2228   1.32  christos 	}
   2229   1.32  christos 	if (flags & IFF_BROADCAST) {
   2230   1.32  christos 		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
   2231   1.32  christos 		sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
   2232   1.32  christos 		if (sat)
   2233   1.32  christos 			printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
   2234   1.32  christos 			    sat->sat_addr.s_node);
   2235   1.32  christos 	}
   2236   1.32  christos 	putchar('\n');
   2237   1.32  christos }
   2238   1.32  christos 
   2239   1.32  christos void
   2240    1.1       cgd xns_status(force)
   2241    1.1       cgd 	int force;
   2242    1.1       cgd {
   2243    1.1       cgd 	struct sockaddr_ns *sns;
   2244    1.1       cgd 
   2245   1.13   mycroft 	getsock(AF_NS);
   2246   1.13   mycroft 	if (s < 0) {
   2247   1.13   mycroft 		if (errno == EPROTONOSUPPORT)
   2248    1.1       cgd 			return;
   2249   1.13   mycroft 		err(1, "socket");
   2250   1.13   mycroft 	}
   2251   1.32  christos 	(void) memset(&ifr, 0, sizeof(ifr));
   2252   1.32  christos 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   2253   1.13   mycroft 	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
   2254    1.1       cgd 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2255    1.1       cgd 			if (!force)
   2256    1.1       cgd 				return;
   2257   1.14   mycroft 			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   2258    1.1       cgd 		} else
   2259   1.13   mycroft 			warn("SIOCGIFADDR");
   2260    1.1       cgd 	}
   2261   1.32  christos 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   2262    1.1       cgd 	sns = (struct sockaddr_ns *)&ifr.ifr_addr;
   2263    1.1       cgd 	printf("\tns %s ", ns_ntoa(sns->sns_addr));
   2264    1.1       cgd 	if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
   2265   1.13   mycroft 		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
   2266    1.1       cgd 			if (errno == EADDRNOTAVAIL)
   2267   1.14   mycroft 			    memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
   2268    1.1       cgd 			else
   2269   1.13   mycroft 			    warn("SIOCGIFDSTADDR");
   2270    1.1       cgd 		}
   2271   1.32  christos 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   2272    1.1       cgd 		sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr;
   2273    1.1       cgd 		printf("--> %s ", ns_ntoa(sns->sns_addr));
   2274    1.1       cgd 	}
   2275    1.1       cgd 	putchar('\n');
   2276    1.1       cgd }
   2277    1.1       cgd 
   2278   1.16       cgd void
   2279    1.1       cgd iso_status(force)
   2280    1.1       cgd 	int force;
   2281    1.1       cgd {
   2282    1.1       cgd 	struct sockaddr_iso *siso;
   2283   1.50    chopps 	struct iso_ifreq ifr;
   2284    1.1       cgd 
   2285   1.13   mycroft 	getsock(AF_ISO);
   2286   1.13   mycroft 	if (s < 0) {
   2287   1.13   mycroft 		if (errno == EPROTONOSUPPORT)
   2288    1.1       cgd 			return;
   2289   1.13   mycroft 		err(1, "socket");
   2290   1.13   mycroft 	}
   2291   1.32  christos 	(void) memset(&ifr, 0, sizeof(ifr));
   2292   1.32  christos 	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
   2293   1.50    chopps 	if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&ifr) < 0) {
   2294    1.1       cgd 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
   2295    1.1       cgd 			if (!force)
   2296    1.1       cgd 				return;
   2297   1.50    chopps 			(void) memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
   2298   1.13   mycroft 		} else
   2299   1.50    chopps 			warn("SIOCGIFADDR_ISO");
   2300    1.1       cgd 	}
   2301   1.32  christos 	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
   2302   1.50    chopps 	siso = &ifr.ifr_Addr;
   2303    1.1       cgd 	printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
   2304   1.50    chopps 	if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&ifr) < 0) {
   2305   1.13   mycroft 		if (errno == EADDRNOTAVAIL)
   2306   1.50    chopps 			memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
   2307   1.13   mycroft 		else
   2308   1.50    chopps 			warn("SIOCGIFNETMASK_ISO");
   2309    1.1       cgd 	} else {
   2310   1.50    chopps 		if (siso->siso_len > offsetof(struct sockaddr_iso, siso_addr))
   2311   1.50    chopps 			siso->siso_addr.isoa_len = siso->siso_len
   2312   1.50    chopps 			    - offsetof(struct sockaddr_iso, siso_addr);
   2313   1.50    chopps 		printf("\n\t\tnetmask %s ", iso_ntoa(&siso->siso_addr));
   2314    1.1       cgd 	}
   2315    1.1       cgd 	if (flags & IFF_POINTOPOINT) {
   2316   1.50    chopps 		if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&ifr) < 0) {
   2317    1.1       cgd 			if (errno == EADDRNOTAVAIL)
   2318   1.50    chopps 			    memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
   2319    1.1       cgd 			else
   2320   1.50    chopps 			    warn("SIOCGIFDSTADDR_ISO");
   2321    1.1       cgd 		}
   2322   1.32  christos 		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
   2323   1.50    chopps 		siso = &ifr.ifr_Addr;
   2324    1.1       cgd 		printf("--> %s ", iso_ntoa(&siso->siso_addr));
   2325    1.1       cgd 	}
   2326    1.1       cgd 	putchar('\n');
   2327    1.1       cgd }
   2328    1.1       cgd 
   2329   1.21       gwr #endif	/* INET_ONLY */
   2330   1.21       gwr 
   2331    1.1       cgd #define SIN(x) ((struct sockaddr_in *) &(x))
   2332    1.1       cgd struct sockaddr_in *sintab[] = {
   2333    1.1       cgd SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
   2334    1.1       cgd SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
   2335    1.1       cgd 
   2336   1.16       cgd void
   2337    1.1       cgd in_getaddr(s, which)
   2338   1.80   thorpej 	const char *s;
   2339   1.16       cgd 	int which;
   2340    1.1       cgd {
   2341   1.39     lukem 	struct sockaddr_in *sin = sintab[which];
   2342    1.1       cgd 	struct hostent *hp;
   2343    1.1       cgd 	struct netent *np;
   2344    1.1       cgd 
   2345    1.1       cgd 	sin->sin_len = sizeof(*sin);
   2346    1.1       cgd 	if (which != MASK)
   2347    1.1       cgd 		sin->sin_family = AF_INET;
   2348    1.1       cgd 
   2349   1.60      joda 	if (which == ADDR) {
   2350   1.60      joda 		char *p = NULL;
   2351   1.96    itojun 		if ((p = strrchr(s, '/')) != NULL) {
   2352   1.96    itojun 			*p = '\0';
   2353   1.96    itojun 			in_getprefix(p + 1, MASK);
   2354   1.96    itojun 		}
   2355   1.60      joda 	}
   2356   1.60      joda 
   2357   1.20   mycroft 	if (inet_aton(s, &sin->sin_addr) == 0) {
   2358   1.32  christos 		if ((hp = gethostbyname(s)) != NULL)
   2359   1.32  christos 			(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
   2360   1.32  christos 		else if ((np = getnetbyname(s)) != NULL)
   2361   1.20   mycroft 			sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
   2362   1.20   mycroft 		else
   2363   1.20   mycroft 			errx(1, "%s: bad value", s);
   2364   1.20   mycroft 	}
   2365   1.95    itojun }
   2366   1.95    itojun 
   2367   1.95    itojun void
   2368   1.95    itojun in_getprefix(plen, which)
   2369   1.95    itojun 	const char *plen;
   2370   1.95    itojun 	int which;
   2371   1.95    itojun {
   2372   1.95    itojun 	register struct sockaddr_in *sin = sintab[which];
   2373   1.95    itojun 	register u_char *cp;
   2374   1.95    itojun 	int len = strtol(plen, (char **)NULL, 10);
   2375   1.95    itojun 
   2376   1.95    itojun 	if ((len < 0) || (len > 32))
   2377   1.95    itojun 		errx(1, "%s: bad value", plen);
   2378   1.95    itojun 	sin->sin_len = sizeof(*sin);
   2379   1.95    itojun 	if (which != MASK)
   2380   1.95    itojun 		sin->sin_family = AF_INET;
   2381   1.95    itojun 	if ((len == 0) || (len == 32)) {
   2382   1.95    itojun 		memset(&sin->sin_addr, 0xff, sizeof(struct in_addr));
   2383   1.95    itojun 		return;
   2384   1.95    itojun 	}
   2385   1.95    itojun 	memset((void *)&sin->sin_addr, 0x00, sizeof(sin->sin_addr));
   2386   1.95    itojun 	for (cp = (u_char *)&sin->sin_addr; len > 7; len -= 8)
   2387   1.95    itojun 		*cp++ = 0xff;
   2388   1.95    itojun 	*cp = 0xff << (8 - len);
   2389    1.1       cgd }
   2390    1.1       cgd 
   2391    1.1       cgd /*
   2392    1.1       cgd  * Print a value a la the %b format of the kernel's printf
   2393    1.1       cgd  */
   2394   1.16       cgd void
   2395    1.1       cgd printb(s, v, bits)
   2396   1.80   thorpej 	const char *s;
   2397   1.39     lukem 	unsigned short v;
   2398   1.80   thorpej 	const char *bits;
   2399    1.1       cgd {
   2400   1.39     lukem 	int i, any = 0;
   2401   1.39     lukem 	char c;
   2402    1.1       cgd 
   2403    1.1       cgd 	if (bits && *bits == 8)
   2404    1.1       cgd 		printf("%s=%o", s, v);
   2405    1.1       cgd 	else
   2406    1.1       cgd 		printf("%s=%x", s, v);
   2407    1.1       cgd 	bits++;
   2408    1.1       cgd 	if (bits) {
   2409    1.1       cgd 		putchar('<');
   2410   1.32  christos 		while ((i = *bits++) != 0) {
   2411    1.1       cgd 			if (v & (1 << (i-1))) {
   2412    1.1       cgd 				if (any)
   2413    1.1       cgd 					putchar(',');
   2414    1.1       cgd 				any = 1;
   2415    1.1       cgd 				for (; (c = *bits) > 32; bits++)
   2416    1.1       cgd 					putchar(c);
   2417    1.1       cgd 			} else
   2418    1.1       cgd 				for (; *bits > 32; bits++)
   2419    1.1       cgd 					;
   2420    1.1       cgd 		}
   2421    1.1       cgd 		putchar('>');
   2422    1.1       cgd 	}
   2423    1.1       cgd }
   2424    1.1       cgd 
   2425   1.53    itojun #ifdef INET6
   2426   1.53    itojun #define SIN6(x) ((struct sockaddr_in6 *) &(x))
   2427   1.53    itojun struct sockaddr_in6 *sin6tab[] = {
   2428   1.53    itojun SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
   2429   1.53    itojun SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
   2430   1.53    itojun 
   2431   1.53    itojun void
   2432   1.53    itojun in6_getaddr(s, which)
   2433   1.80   thorpej 	const char *s;
   2434   1.53    itojun 	int which;
   2435   1.53    itojun {
   2436   1.59    itojun #if defined(__KAME__) && defined(KAME_SCOPEID)
   2437   1.59    itojun 	struct sockaddr_in6 *sin6 = sin6tab[which];
   2438   1.59    itojun 	struct addrinfo hints, *res;
   2439   1.59    itojun 	int error;
   2440   1.59    itojun 
   2441   1.59    itojun 	memset(&hints, 0, sizeof(hints));
   2442   1.59    itojun 	hints.ai_family = AF_INET6;
   2443   1.59    itojun 	hints.ai_socktype = SOCK_DGRAM;
   2444   1.59    itojun #if 0 /* in_getaddr() allows FQDN */
   2445   1.59    itojun 	hints.ai_flags = AI_NUMERICHOST;
   2446   1.59    itojun #endif
   2447   1.59    itojun 	error = getaddrinfo(s, "0", &hints, &res);
   2448   1.59    itojun 	if (error)
   2449   1.59    itojun 		errx(1, "%s: %s", s, gai_strerror(error));
   2450   1.59    itojun 	if (res->ai_next)
   2451   1.59    itojun 		errx(1, "%s: resolved to multiple hosts", s);
   2452   1.59    itojun 	if (res->ai_addrlen != sizeof(struct sockaddr_in6))
   2453   1.59    itojun 		errx(1, "%s: bad value", s);
   2454   1.59    itojun 	memcpy(sin6, res->ai_addr, res->ai_addrlen);
   2455   1.59    itojun 	freeaddrinfo(res);
   2456   1.59    itojun 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && sin6->sin6_scope_id) {
   2457   1.59    itojun 		*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] =
   2458   1.59    itojun 			htons(sin6->sin6_scope_id);
   2459   1.59    itojun 		sin6->sin6_scope_id = 0;
   2460   1.59    itojun 	}
   2461   1.59    itojun #else
   2462   1.53    itojun 	struct sockaddr_in6 *sin = sin6tab[which];
   2463   1.53    itojun 
   2464   1.53    itojun 	sin->sin6_len = sizeof(*sin);
   2465   1.53    itojun 	if (which != MASK)
   2466   1.53    itojun 		sin->sin6_family = AF_INET6;
   2467   1.60      joda 
   2468   1.60      joda 	if (which == ADDR) {
   2469   1.60      joda 		char *p = NULL;
   2470   1.60      joda 		if((p = strrchr(s, '/')) != NULL) {
   2471   1.60      joda 			*p = '\0';
   2472   1.60      joda 			in6_getprefix(p + 1, MASK);
   2473   1.60      joda 			explicit_prefix = 1;
   2474   1.60      joda 		}
   2475   1.60      joda 	}
   2476   1.53    itojun 
   2477   1.59    itojun 	if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
   2478   1.53    itojun 		errx(1, "%s: bad value", s);
   2479   1.59    itojun #endif
   2480   1.53    itojun }
   2481   1.53    itojun 
   2482   1.53    itojun void
   2483   1.53    itojun in6_getprefix(plen, which)
   2484   1.80   thorpej 	const char *plen;
   2485   1.53    itojun 	int which;
   2486   1.53    itojun {
   2487   1.53    itojun 	register struct sockaddr_in6 *sin = sin6tab[which];
   2488   1.53    itojun 	register u_char *cp;
   2489   1.53    itojun 	int len = strtol(plen, (char **)NULL, 10);
   2490   1.53    itojun 
   2491   1.53    itojun 	if ((len < 0) || (len > 128))
   2492   1.53    itojun 		errx(1, "%s: bad value", plen);
   2493   1.53    itojun 	sin->sin6_len = sizeof(*sin);
   2494   1.53    itojun 	if (which != MASK)
   2495   1.53    itojun 		sin->sin6_family = AF_INET6;
   2496   1.53    itojun 	if ((len == 0) || (len == 128)) {
   2497   1.53    itojun 		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
   2498   1.53    itojun 		return;
   2499   1.53    itojun 	}
   2500   1.53    itojun 	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
   2501   1.53    itojun 	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
   2502   1.53    itojun 		*cp++ = 0xff;
   2503   1.53    itojun 	*cp = 0xff << (8 - len);
   2504   1.53    itojun }
   2505   1.53    itojun 
   2506   1.53    itojun int
   2507   1.53    itojun prefix(val, size)
   2508   1.53    itojun 	void *val;
   2509   1.53    itojun 	int size;
   2510   1.53    itojun {
   2511   1.53    itojun 	register u_char *name = (u_char *)val;
   2512   1.53    itojun 	register int byte, bit, plen = 0;
   2513   1.53    itojun 
   2514   1.53    itojun 	for (byte = 0; byte < size; byte++, plen += 8)
   2515   1.53    itojun 		if (name[byte] != 0xff)
   2516   1.53    itojun 			break;
   2517   1.53    itojun 	if (byte == size)
   2518   1.53    itojun 		return (plen);
   2519   1.53    itojun 	for (bit = 7; bit != 0; bit--, plen++)
   2520   1.53    itojun 		if (!(name[byte] & (1 << bit)))
   2521   1.53    itojun 			break;
   2522   1.53    itojun 	for (; bit != 0; bit--)
   2523   1.53    itojun 		if (name[byte] & (1 << bit))
   2524   1.53    itojun 			return(0);
   2525   1.53    itojun 	byte++;
   2526   1.53    itojun 	for (; byte < size; byte++)
   2527   1.53    itojun 		if (name[byte])
   2528   1.53    itojun 			return(0);
   2529   1.53    itojun 	return (plen);
   2530   1.53    itojun }
   2531   1.53    itojun #endif /*INET6*/
   2532   1.53    itojun 
   2533   1.21       gwr #ifndef INET_ONLY
   2534   1.32  christos void
   2535   1.32  christos at_getaddr(addr, which)
   2536   1.80   thorpej 	const char *addr;
   2537   1.32  christos 	int which;
   2538   1.32  christos {
   2539   1.32  christos 	struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
   2540   1.32  christos 	u_int net, node;
   2541   1.32  christos 
   2542   1.32  christos 	sat->sat_family = AF_APPLETALK;
   2543   1.32  christos 	sat->sat_len = sizeof(*sat);
   2544   1.32  christos 	if (which == MASK)
   2545   1.32  christos 		errx(1, "AppleTalk does not use netmasks\n");
   2546   1.32  christos 	if (sscanf(addr, "%u.%u", &net, &node) != 2
   2547   1.32  christos 	    || net == 0 || net > 0xffff || node == 0 || node > 0xfe)
   2548   1.32  christos 		errx(1, "%s: illegal address", addr);
   2549   1.32  christos 	sat->sat_addr.s_net = htons(net);
   2550   1.32  christos 	sat->sat_addr.s_node = node;
   2551   1.32  christos }
   2552   1.32  christos 
   2553   1.32  christos void
   2554   1.32  christos setatrange(range, d)
   2555   1.80   thorpej 	const char *range;
   2556   1.32  christos 	int d;
   2557   1.32  christos {
   2558   1.32  christos 	u_short	first = 123, last = 123;
   2559   1.32  christos 
   2560   1.32  christos 	if (sscanf(range, "%hu-%hu", &first, &last) != 2
   2561   1.32  christos 	    || first == 0 || first > 0xffff
   2562   1.32  christos 	    || last == 0 || last > 0xffff || first > last)
   2563   1.32  christos 		errx(1, "%s: illegal net range: %u-%u", range, first, last);
   2564   1.32  christos 	at_nr.nr_firstnet = htons(first);
   2565   1.32  christos 	at_nr.nr_lastnet = htons(last);
   2566   1.32  christos }
   2567   1.32  christos 
   2568   1.32  christos void
   2569   1.32  christos setatphase(phase, d)
   2570   1.80   thorpej 	const char *phase;
   2571   1.32  christos 	int d;
   2572   1.32  christos {
   2573   1.32  christos 	if (!strcmp(phase, "1"))
   2574   1.32  christos 		at_nr.nr_phase = 1;
   2575   1.32  christos 	else if (!strcmp(phase, "2"))
   2576   1.32  christos 		at_nr.nr_phase = 2;
   2577   1.32  christos 	else
   2578   1.32  christos 		errx(1, "%s: illegal phase", phase);
   2579   1.32  christos }
   2580   1.32  christos 
   2581   1.32  christos void
   2582   1.32  christos checkatrange(sat)
   2583   1.32  christos 	struct sockaddr_at *sat;
   2584   1.32  christos {
   2585   1.32  christos 	if (at_nr.nr_phase == 0)
   2586   1.32  christos 		at_nr.nr_phase = 2;	/* Default phase 2 */
   2587   1.32  christos 	if (at_nr.nr_firstnet == 0)
   2588   1.32  christos 		at_nr.nr_firstnet =	/* Default range of one */
   2589   1.32  christos 		at_nr.nr_lastnet = sat->sat_addr.s_net;
   2590   1.32  christos 	printf("\tatalk %d.%d range %d-%d phase %d\n",
   2591   1.32  christos 	ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
   2592   1.32  christos 	ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
   2593   1.32  christos 	if ((u_short) ntohs(at_nr.nr_firstnet) >
   2594   1.32  christos 			(u_short) ntohs(sat->sat_addr.s_net)
   2595   1.32  christos 		    || (u_short) ntohs(at_nr.nr_lastnet) <
   2596   1.32  christos 			(u_short) ntohs(sat->sat_addr.s_net))
   2597   1.32  christos 		errx(1, "AppleTalk address is not in range");
   2598   1.32  christos 	*((struct netrange *) &sat->sat_zero) = at_nr;
   2599   1.32  christos }
   2600   1.32  christos 
   2601    1.1       cgd #define SNS(x) ((struct sockaddr_ns *) &(x))
   2602    1.1       cgd struct sockaddr_ns *snstab[] = {
   2603    1.1       cgd SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
   2604    1.1       cgd SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
   2605    1.1       cgd 
   2606   1.16       cgd void
   2607    1.1       cgd xns_getaddr(addr, which)
   2608   1.80   thorpej 	const char *addr;
   2609   1.16       cgd 	int which;
   2610    1.1       cgd {
   2611    1.1       cgd 	struct sockaddr_ns *sns = snstab[which];
   2612    1.1       cgd 
   2613    1.1       cgd 	sns->sns_family = AF_NS;
   2614    1.1       cgd 	sns->sns_len = sizeof(*sns);
   2615    1.1       cgd 	sns->sns_addr = ns_addr(addr);
   2616    1.1       cgd 	if (which == MASK)
   2617   1.34     lukem 		puts("Attempt to set XNS netmask will be ineffectual");
   2618    1.1       cgd }
   2619    1.1       cgd 
   2620    1.1       cgd #define SISO(x) ((struct sockaddr_iso *) &(x))
   2621    1.1       cgd struct sockaddr_iso *sisotab[] = {
   2622   1.50    chopps SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
   2623    1.1       cgd SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
   2624    1.1       cgd 
   2625   1.16       cgd void
   2626    1.1       cgd iso_getaddr(addr, which)
   2627   1.80   thorpej 	const char *addr;
   2628   1.16       cgd 	int which;
   2629    1.1       cgd {
   2630   1.39     lukem 	struct sockaddr_iso *siso = sisotab[which];
   2631    1.1       cgd 	siso->siso_addr = *iso_addr(addr);
   2632    1.1       cgd 
   2633    1.1       cgd 	if (which == MASK) {
   2634    1.1       cgd 		siso->siso_len = TSEL(siso) - (caddr_t)(siso);
   2635    1.1       cgd 		siso->siso_nlen = 0;
   2636    1.1       cgd 	} else {
   2637    1.1       cgd 		siso->siso_len = sizeof(*siso);
   2638    1.1       cgd 		siso->siso_family = AF_ISO;
   2639    1.1       cgd 	}
   2640    1.1       cgd }
   2641    1.1       cgd 
   2642   1.16       cgd void
   2643   1.32  christos setsnpaoffset(val, d)
   2644   1.80   thorpej 	const char *val;
   2645   1.32  christos 	int d;
   2646   1.21       gwr {
   2647   1.21       gwr 	iso_addreq.ifra_snpaoffset = atoi(val);
   2648   1.21       gwr }
   2649   1.21       gwr 
   2650   1.21       gwr void
   2651   1.32  christos setnsellength(val, d)
   2652   1.80   thorpej 	const char *val;
   2653   1.32  christos 	int d;
   2654    1.1       cgd {
   2655    1.1       cgd 	nsellength = atoi(val);
   2656   1.12       cgd 	if (nsellength < 0)
   2657   1.12       cgd 		errx(1, "Negative NSEL length is absurd");
   2658   1.12       cgd 	if (afp == 0 || afp->af_af != AF_ISO)
   2659   1.12       cgd 		errx(1, "Setting NSEL length valid only for iso");
   2660    1.1       cgd }
   2661    1.1       cgd 
   2662   1.16       cgd void
   2663    1.1       cgd fixnsel(s)
   2664   1.39     lukem 	struct sockaddr_iso *s;
   2665    1.1       cgd {
   2666    1.1       cgd 	if (s->siso_family == 0)
   2667    1.1       cgd 		return;
   2668    1.1       cgd 	s->siso_tlen = nsellength;
   2669    1.1       cgd }
   2670    1.1       cgd 
   2671   1.16       cgd void
   2672    1.1       cgd adjust_nsellength()
   2673    1.1       cgd {
   2674    1.1       cgd 	fixnsel(sisotab[RIDADDR]);
   2675    1.1       cgd 	fixnsel(sisotab[ADDR]);
   2676    1.1       cgd 	fixnsel(sisotab[DSTADDR]);
   2677   1.18     glass }
   2678   1.21       gwr 
   2679   1.21       gwr #endif	/* INET_ONLY */
   2680   1.18     glass 
   2681   1.18     glass void
   2682   1.18     glass usage()
   2683   1.18     glass {
   2684   1.99       cgd 	const char *progname = getprogname();
   2685   1.80   thorpej 
   2686   1.31   thorpej 	fprintf(stderr,
   2687  1.107    itojun 	    "usage: %s [ -m ] "
   2688   1.53    itojun #ifdef INET6
   2689   1.80   thorpej 		"[ -L ] "
   2690   1.53    itojun #endif
   2691   1.80   thorpej 		"interface\n"
   2692  1.105    itojun 		"\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
   2693  1.105    itojun 		"\t\t[ alias | -alias ] ]\n"
   2694  1.104    itojun 		"\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
   2695   1.93      onoe 		"\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
   2696   1.93      onoe 		"\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
   2697   1.93      onoe 		"\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
   2698   1.80   thorpej 		"\t[ arp | -arp ]\n"
   2699   1.93      onoe 		"\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
   2700   1.93      onoe 		"[ instance minst ]\n"
   2701   1.89   thorpej 		"\t[ vlan n vlanif i ]\n"
   2702  1.104    itojun 		"\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
   2703  1.104    itojun 		"\t[ tentative | -tentative ] [ pltime n ] [ vltime n ]\n"
   2704   1.80   thorpej 		"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
   2705  1.107    itojun 		"       %s -a [ -m ] [ -d ] [ -u ] [ af ]\n"
   2706   1.97   garbled 		"       %s -l [ -b ] [ -d ] [ -u ] [ -s ]\n"
   2707  1.101  christos 		"       %s -C\n"
   2708   1.81   thorpej 		"       %s interface create\n"
   2709   1.81   thorpej 		"       %s interface destroy\n",
   2710  1.101  christos 		progname, progname, progname, progname, progname, progname);
   2711   1.18     glass 	exit(1);
   2712    1.1       cgd }
   2713   1.53    itojun 
   2714   1.53    itojun #ifdef INET6
   2715   1.53    itojun char *
   2716   1.53    itojun sec2str(total)
   2717   1.53    itojun 	time_t total;
   2718   1.53    itojun {
   2719   1.53    itojun 	static char result[256];
   2720   1.53    itojun 	int days, hours, mins, secs;
   2721   1.53    itojun 	int first = 1;
   2722   1.53    itojun 	char *p = result;
   2723   1.53    itojun 
   2724   1.53    itojun 	if (0) {	/*XXX*/
   2725   1.53    itojun 		days = total / 3600 / 24;
   2726   1.53    itojun 		hours = (total / 3600) % 24;
   2727   1.53    itojun 		mins = (total / 60) % 60;
   2728   1.53    itojun 		secs = total % 60;
   2729   1.53    itojun 
   2730   1.53    itojun 		if (days) {
   2731   1.53    itojun 			first = 0;
   2732   1.53    itojun 			p += sprintf(p, "%dd", days);
   2733   1.53    itojun 		}
   2734   1.53    itojun 		if (!first || hours) {
   2735   1.53    itojun 			first = 0;
   2736   1.53    itojun 			p += sprintf(p, "%dh", hours);
   2737   1.53    itojun 		}
   2738   1.53    itojun 		if (!first || mins) {
   2739   1.53    itojun 			first = 0;
   2740   1.53    itojun 			p += sprintf(p, "%dm", mins);
   2741   1.53    itojun 		}
   2742   1.53    itojun 		sprintf(p, "%ds", secs);
   2743   1.53    itojun 	} else
   2744   1.53    itojun 		sprintf(p, "%lu", (u_long)total);
   2745   1.53    itojun 
   2746   1.53    itojun 	return(result);
   2747   1.53    itojun }
   2748   1.53    itojun #endif
   2749