ifconfig.c revision 1.64
1/*	$NetBSD: ifconfig.c,v 1.64 2000/01/25 01:03:21 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the NetBSD
22 *	Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40/*
41 * Copyright (c) 1983, 1993
42 *	The Regents of the University of California.  All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 *    notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 *    notice, this list of conditions and the following disclaimer in the
51 *    documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 *    must display the following acknowledgement:
54 *	This product includes software developed by the University of
55 *	California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 *    may be used to endorse or promote products derived from this software
58 *    without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 */
72
73#include <sys/cdefs.h>
74#ifndef lint
75__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
76	The Regents of the University of California.  All rights reserved.\n");
77#endif /* not lint */
78
79#ifndef lint
80#if 0
81static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
82#else
83__RCSID("$NetBSD: ifconfig.c,v 1.64 2000/01/25 01:03:21 thorpej Exp $");
84#endif
85#endif /* not lint */
86
87#include <sys/param.h>
88#include <sys/socket.h>
89#include <sys/ioctl.h>
90
91#include <net/if.h>
92#include <net/if_dl.h>
93#include <net/if_media.h>
94#include <net/if_ether.h>
95#include <net/if_ieee80211.h>
96#include <netinet/in.h>
97#include <netinet/in_var.h>
98#ifdef INET6
99#include <netinet6/nd6.h>
100#endif
101#include <arpa/inet.h>
102
103#include <netatalk/at.h>
104
105#define	NSIP
106#include <netns/ns.h>
107#include <netns/ns_if.h>
108#include <netdb.h>
109
110#define EON
111#include <netiso/iso.h>
112#include <netiso/iso_var.h>
113#include <sys/protosw.h>
114
115#include <ctype.h>
116#include <err.h>
117#include <errno.h>
118#include <stddef.h>
119#include <stdio.h>
120#include <stdlib.h>
121#include <string.h>
122#include <unistd.h>
123
124struct	ifreq		ifr, ridreq;
125struct	ifaliasreq	addreq __attribute__((aligned(4)));
126#ifdef INET6
127struct	in6_ifreq	ifr6;
128struct	in6_ifreq	in6_ridreq;
129struct	in6_aliasreq	in6_addreq __attribute__((aligned(4)));
130#endif
131struct	iso_ifreq	iso_ridreq;
132struct	iso_aliasreq	iso_addreq;
133struct	sockaddr_in	netmask;
134struct	netrange	at_nr;		/* AppleTalk net range */
135
136char	name[30];
137int	flags, metric, mtu, setaddr, setipdst, doalias;
138int	clearaddr, s;
139int	newaddr = -1;
140int	nsellength = 1;
141int	af;
142int	Aflag, aflag, bflag, dflag, lflag, mflag, sflag, uflag;
143#ifdef INET6
144int	Lflag;
145#endif
146int	reset_if_flags;
147int	explicit_prefix = 0;
148
149void 	notealias __P((char *, int));
150void 	notrailers __P((char *, int));
151void 	setifaddr __P((char *, int));
152void 	setifdstaddr __P((char *, int));
153void 	setifflags __P((char *, int));
154void 	setifbroadaddr __P((char *, int));
155void 	setifipdst __P((char *, int));
156void 	setifmetric __P((char *, int));
157void 	setifmtu __P((char *, int));
158void	setifnwid __P((char *, int));
159void 	setifnetmask __P((char *, int));
160void	setifprefixlen __P((char *, int));
161void 	setnsellength __P((char *, int));
162void 	setsnpaoffset __P((char *, int));
163void	setatrange __P((char *, int));
164void	setatphase __P((char *, int));
165#ifdef INET6
166void 	setia6flags __P((char *, int));
167void	setia6pltime __P((char *, int));
168void	setia6vltime __P((char *, int));
169void	setia6lifetime __P((char *, char *));
170#endif
171void	checkatrange __P ((struct sockaddr_at *));
172void	setmedia __P((char *, int));
173void	setmediaopt __P((char *, int));
174void	unsetmediaopt __P((char *, int));
175void	setmediainst __P((char *, int));
176void	fixnsel __P((struct sockaddr_iso *));
177int	main __P((int, char *[]));
178
179/*
180 * Media stuff.  Whenever a media command is first performed, the
181 * currently select media is grabbed for this interface.  If `media'
182 * is given, the current media word is modifed.  `mediaopt' commands
183 * only modify the set and clear words.  They then operate on the
184 * current media word later.
185 */
186int	media_current;
187int	mediaopt_set;
188int	mediaopt_clear;
189
190int	actions;			/* Actions performed */
191
192#define	A_MEDIA		0x0001		/* media command */
193#define	A_MEDIAOPTSET	0x0002		/* mediaopt command */
194#define	A_MEDIAOPTCLR	0x0004		/* -mediaopt command */
195#define	A_MEDIAOPT	(A_MEDIAOPTSET|A_MEDIAOPTCLR)
196#define	A_MEDIAINST	0x0008		/* instance or inst command */
197
198#define	NEXTARG		0xffffff
199
200struct	cmd {
201	char	*c_name;
202	int	c_parameter;		/* NEXTARG means next argv */
203	int	c_action;	/* defered action */
204	void	(*c_func) __P((char *, int));
205} cmds[] = {
206	{ "up",		IFF_UP,		0,		setifflags } ,
207	{ "down",	-IFF_UP,	0,		setifflags },
208	{ "trailers",	-1,		0,		notrailers },
209	{ "-trailers",	1,		0,		notrailers },
210	{ "arp",	-IFF_NOARP,	0,		setifflags },
211	{ "-arp",	IFF_NOARP,	0,		setifflags },
212	{ "debug",	IFF_DEBUG,	0,		setifflags },
213	{ "-debug",	-IFF_DEBUG,	0,		setifflags },
214	{ "alias",	IFF_UP,		0,		notealias },
215	{ "-alias",	-IFF_UP,	0,		notealias },
216	{ "delete",	-IFF_UP,	0,		notealias },
217#ifdef notdef
218#define	EN_SWABIPS	0x1000
219	{ "swabips",	EN_SWABIPS,	0,		setifflags },
220	{ "-swabips",	-EN_SWABIPS,	0,		setifflags },
221#endif
222	{ "netmask",	NEXTARG,	0,		setifnetmask },
223	{ "metric",	NEXTARG,	0,		setifmetric },
224	{ "mtu",	NEXTARG,	0,		setifmtu },
225	{ "nwid",	NEXTARG,	0,		setifnwid },
226	{ "broadcast",	NEXTARG,	0,		setifbroadaddr },
227	{ "ipdst",	NEXTARG,	0,		setifipdst },
228	{ "prefixlen",  NEXTARG,	0,		setifprefixlen},
229#ifdef INET6
230	{ "anycast",	IN6_IFF_ANYCAST,	0,	setia6flags },
231	{ "-anycast",	-IN6_IFF_ANYCAST,	0,	setia6flags },
232	{ "tentative",	IN6_IFF_TENTATIVE,	0,	setia6flags },
233	{ "-tentative",	-IN6_IFF_TENTATIVE,	0,	setia6flags },
234	{ "pltime",	NEXTARG,	0,		setia6pltime },
235	{ "vltime",	NEXTARG,	0,		setia6vltime },
236#endif /*INET6*/
237#ifndef INET_ONLY
238	{ "range",	NEXTARG,	0,		setatrange },
239	{ "phase",	NEXTARG,	0,		setatphase },
240	{ "snpaoffset",	NEXTARG,	0,		setsnpaoffset },
241	{ "nsellength",	NEXTARG,	0,		setnsellength },
242#endif	/* INET_ONLY */
243	{ "link0",	IFF_LINK0,	0,		setifflags } ,
244	{ "-link0",	-IFF_LINK0,	0,		setifflags } ,
245	{ "link1",	IFF_LINK1,	0,		setifflags } ,
246	{ "-link1",	-IFF_LINK1,	0,		setifflags } ,
247	{ "link2",	IFF_LINK2,	0,		setifflags } ,
248	{ "-link2",	-IFF_LINK2,	0,		setifflags } ,
249	{ "media",	NEXTARG,	A_MEDIA,	setmedia },
250	{ "mediaopt",	NEXTARG,	A_MEDIAOPTSET,	setmediaopt },
251	{ "-mediaopt",	NEXTARG,	A_MEDIAOPTCLR,	unsetmediaopt },
252	{ "instance",	NEXTARG,	A_MEDIAINST,	setmediainst },
253	{ "inst",	NEXTARG,	A_MEDIAINST,	setmediainst },
254	{ 0,		0,		0,		setifaddr },
255	{ 0,		0,		0,		setifdstaddr },
256};
257
258void 	adjust_nsellength __P((void));
259int	getinfo __P((struct ifreq *));
260int	carrier __P((void));
261void	getsock __P((int));
262void	printall __P((void));
263void	printalias __P((const char *, int));
264void 	printb __P((char *, unsigned short, char *));
265int	prefix __P((void *, int));
266void 	status __P((const u_int8_t *, int));
267void 	usage __P((void));
268char	*sec2str __P((time_t));
269
270const char *get_media_type_string __P((int));
271const char *get_media_subtype_string __P((int));
272int	get_media_subtype __P((int, const char *));
273int	get_media_options __P((int, const char *));
274int	lookup_media_word __P((struct ifmedia_description *, int,
275	    const char *));
276void	print_media_word __P((int, int, int));
277void	process_media_commands __P((void));
278void	init_current_media __P((void));
279
280/*
281 * XNS support liberally adapted from code written at the University of
282 * Maryland principally by James O'Toole and Chris Torek.
283 */
284void	in_alias __P((struct ifreq *));
285void	in_status __P((int));
286void 	in_getaddr __P((char *, int));
287#ifdef INET6
288void	in6_fillscopeid __P((struct sockaddr_in6 *sin6));
289void	in6_alias __P((struct in6_ifreq *));
290void	in6_status __P((int));
291void 	in6_getaddr __P((char *, int));
292void 	in6_getprefix __P((char *, int));
293#endif
294void	at_status __P((int));
295void	at_getaddr __P((char *, int));
296void 	xns_status __P((int));
297void 	xns_getaddr __P((char *, int));
298void 	iso_status __P((int));
299void 	iso_getaddr __P((char *, int));
300void	ieee80211_status __P((void));
301
302/* Known address families */
303struct afswtch {
304	char *af_name;
305	short af_af;
306	void (*af_status) __P((int));
307	void (*af_getaddr) __P((char *, int));
308	void (*af_getprefix) __P((char *, int));
309	u_long af_difaddr;
310	u_long af_aifaddr;
311	caddr_t af_ridreq;
312	caddr_t af_addreq;
313} afs[] = {
314#define C(x) ((caddr_t) &x)
315	{ "inet", AF_INET, in_status, in_getaddr, NULL,
316	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
317#ifdef INET6
318	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
319	     SIOCDIFADDR_IN6, SIOCAIFADDR_IN6, C(in6_ridreq), C(in6_addreq) },
320#endif
321#ifndef INET_ONLY	/* small version, for boot media */
322	{ "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
323	     SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
324	{ "ns", AF_NS, xns_status, xns_getaddr, NULL,
325	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
326	{ "iso", AF_ISO, iso_status, iso_getaddr, NULL,
327	     SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, C(iso_ridreq), C(iso_addreq) },
328#endif	/* INET_ONLY */
329	{ 0,	0,	    0,		0 }
330};
331
332struct afswtch *afp;	/*the address family being set or asked about*/
333
334struct afswtch *lookup_af __P((const char *));
335
336int
337main(argc, argv)
338	int argc;
339	char *argv[];
340{
341	int ch;
342
343	/* Parse command-line options */
344	aflag = mflag = 0;
345	while ((ch = getopt(argc, argv, "Aabdlmsu"
346#ifdef INET6
347					"L"
348#endif
349			)) != -1) {
350		switch (ch) {
351		case 'A':
352			Aflag = 1;
353			break;
354
355		case 'a':
356			aflag = 1;
357			break;
358
359		case 'b':
360			bflag = 1;
361			break;
362
363		case 'd':
364			dflag = 1;
365			break;
366
367#ifdef INET6
368		case 'L':
369			Lflag = 1;
370			break;
371#endif
372
373		case 'l':
374			lflag = 1;
375			break;
376
377		case 'm':
378			mflag = 1;
379			break;
380
381		case 's':
382			sflag = 1;
383			break;
384
385		case 'u':
386			uflag = 1;
387			break;
388
389
390		default:
391			usage();
392			/* NOTREACHED */
393		}
394	}
395	argc -= optind;
396	argv += optind;
397
398	/*
399	 * -l means "list all interfaces", and is mutally exclusive with
400	 * all other flags/commands.
401	 *
402	 * -a means "print status of all interfaces".
403	 */
404	if (lflag && (aflag || mflag || Aflag || argc))
405		usage();
406#ifdef INET6
407	if (lflag && Lflag)
408		usage();
409#endif
410	if (aflag || lflag) {
411		if (argc > 1)
412			usage();
413		else if (argc == 1) {
414			afp = lookup_af(argv[0]);
415			if (afp == NULL)
416				usage();
417		}
418		if (afp)
419			af = ifr.ifr_addr.sa_family = afp->af_af;
420		else
421			af = ifr.ifr_addr.sa_family = afs[0].af_af;
422		printall();
423		exit(0);
424	}
425
426	/* Make sure there's an interface name. */
427	if (argc < 1)
428		usage();
429	(void) strncpy(name, argv[0], sizeof(name));
430	argc--; argv++;
431
432	/* Check for address family. */
433	afp = NULL;
434	if (argc > 0) {
435		afp = lookup_af(argv[0]);
436		if (afp != NULL) {
437			argv++;
438			argc--;
439		}
440	}
441
442	/* Initialize af, just for use in getinfo(). */
443	if (afp == NULL)
444		af = afs->af_af;
445	else
446		af = afp->af_af;
447
448	/* Get information about the interface. */
449	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
450	if (getinfo(&ifr) < 0)
451		exit(1);
452
453	if (sflag) {
454		if (argc != 0)
455			usage();
456		else
457			exit(carrier());
458	}
459
460	/* No more arguments means interface status. */
461	if (argc == 0) {
462		status(NULL, 0);
463		exit(0);
464	}
465
466	/* following operations assume inet familiy as default */
467	if (afp == NULL)
468		afp = afs;
469	af = ifr.ifr_addr.sa_family = afp->af_af;
470
471#ifdef INET6
472	/* initialization */
473	in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
474	in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
475#endif
476
477	/* Process commands. */
478	while (argc > 0) {
479		struct cmd *p;
480
481		for (p = cmds; p->c_name; p++)
482			if (strcmp(argv[0], p->c_name) == 0)
483				break;
484		if (p->c_name == 0 && setaddr) {
485			if ((flags & IFF_POINTOPOINT) == 0) {
486				errx(1, "can't set destination address %s",
487				     "on non-point-to-point link");
488			}
489			p++;	/* got src, do dst */
490		}
491		if (p->c_func) {
492			if (p->c_parameter == NEXTARG) {
493				if (argc < 2)
494					errx(1, "'%s' requires argument",
495					    p->c_name);
496				(*p->c_func)(argv[1], 0);
497				argc--, argv++;
498			} else
499				(*p->c_func)(argv[0], p->c_parameter);
500			actions |= p->c_action;
501		}
502		argc--, argv++;
503	}
504
505	/* Process any media commands that may have been issued. */
506	process_media_commands();
507
508	if (af == AF_INET6 && explicit_prefix == 0) {
509		/*
510		 * Aggregatable address architecture defines all prefixes
511		 * are 64. So, it is convenient to set prefixlen to 64 if
512		 * it is not specified.
513		 */
514		setifprefixlen("64", 0);
515		/* in6_getprefix("64", MASK) if MASK is available here... */
516	}
517
518#ifndef INET_ONLY
519	if (af == AF_ISO)
520		adjust_nsellength();
521
522	if (af == AF_APPLETALK)
523		checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
524
525	if (setipdst && af==AF_NS) {
526		struct nsip_req rq;
527		int size = sizeof(rq);
528
529		rq.rq_ns = addreq.ifra_addr;
530		rq.rq_ip = addreq.ifra_dstaddr;
531
532		if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
533			warn("encapsulation routing");
534	}
535
536#endif	/* INET_ONLY */
537
538	if (clearaddr) {
539		int ret;
540		(void) strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
541		if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
542			if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
543				/* means no previous address for interface */
544			} else
545				warn("SIOCDIFADDR");
546		}
547	}
548	if (newaddr > 0) {
549		(void) strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
550		if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
551			warn("SIOCAIFADDR");
552	}
553	if (reset_if_flags && ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
554		err(1, "SIOCSIFFLAGS");
555	exit(0);
556}
557
558struct afswtch *
559lookup_af(cp)
560	const char *cp;
561{
562	struct afswtch *a;
563
564	for (a = afs; a->af_name != NULL; a++)
565		if (strcmp(a->af_name, cp) == 0)
566			return (a);
567	return (NULL);
568}
569
570void
571getsock(naf)
572	int naf;
573{
574	static int oaf = -1;
575
576	if (oaf == naf)
577		return;
578	if (oaf != -1)
579		close(s);
580	s = socket(naf, SOCK_DGRAM, 0);
581	if (s < 0)
582		oaf = -1;
583	else
584		oaf = naf;
585}
586
587int
588getinfo(ifr)
589	struct ifreq *ifr;
590{
591
592	getsock(af);
593	if (s < 0)
594		err(1, "socket");
595	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
596		warn("SIOCGIFFLAGS %s", ifr->ifr_name);
597		return (-1);
598	}
599	flags = ifr->ifr_flags;
600	if (ioctl(s, SIOCGIFMETRIC, (caddr_t)ifr) < 0) {
601		warn("SIOCGIFMETRIC %s", ifr->ifr_name);
602		metric = 0;
603	} else
604		metric = ifr->ifr_metric;
605	if (ioctl(s, SIOCGIFMTU, (caddr_t)ifr) < 0)
606		mtu = 0;
607	else
608		mtu = ifr->ifr_mtu;
609	return (0);
610}
611
612void
613printalias(iname, af)
614	const char *iname;
615	int af;
616{
617	char inbuf[8192];
618	struct ifconf ifc;
619	struct ifreq *ifr;
620	int i, siz;
621	char ifrbuf[8192];
622
623	ifc.ifc_len = sizeof(inbuf);
624	ifc.ifc_buf = inbuf;
625	getsock(af);
626	if (s < 0)
627		err(1, "socket");
628	if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
629		err(1, "SIOCGIFCONF");
630	ifr = ifc.ifc_req;
631	for (i = 0; i < ifc.ifc_len; ) {
632		ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
633		siz = sizeof(ifr->ifr_name) +
634			(ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
635				? ifr->ifr_addr.sa_len
636				: sizeof(struct sockaddr));
637		i += siz;
638		/* avoid alignment issue */
639		if (sizeof(ifrbuf) < siz)
640			errx(1, "ifr too big");
641		memcpy(ifrbuf, ifr, siz);
642		ifr = (struct ifreq *)ifrbuf;
643		if (!strncmp(iname, ifr->ifr_name, sizeof(ifr->ifr_name))) {
644			if (ifr->ifr_addr.sa_family == af)
645				switch (af) {
646				case AF_INET:
647					in_alias(ifr);
648					break;
649				default:
650					/*none*/
651				}
652			continue;
653		}
654	}
655}
656
657void
658printall()
659{
660	char inbuf[8192];
661	const struct sockaddr_dl *sdl = NULL;
662	struct ifconf ifc;
663	struct ifreq ifreq, *ifr;
664	int i, siz, idx;
665	char ifrbuf[8192];
666
667	ifc.ifc_len = sizeof(inbuf);
668	ifc.ifc_buf = inbuf;
669	getsock(af);
670	if (s < 0)
671		err(1, "socket");
672	if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
673		err(1, "SIOCGIFCONF");
674	ifr = ifc.ifc_req;
675	ifreq.ifr_name[0] = '\0';
676	for (i = 0, idx = 0; i < ifc.ifc_len; ) {
677		ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
678		siz = sizeof(ifr->ifr_name) +
679			(ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
680				? ifr->ifr_addr.sa_len
681				: sizeof(struct sockaddr));
682		i += siz;
683		/* avoid alignment issue */
684		if (sizeof(ifrbuf) < siz)
685			errx(1, "ifr too big");
686		memcpy(ifrbuf, ifr, siz);
687		ifr = (struct ifreq *)ifrbuf;
688		if (ifr->ifr_addr.sa_family == AF_LINK)
689			sdl = (const struct sockaddr_dl *) &ifr->ifr_addr;
690		if (!strncmp(ifreq.ifr_name, ifr->ifr_name,
691			     sizeof(ifr->ifr_name))) {
692			if (Aflag && ifr->ifr_addr.sa_family == AF_INET)
693				in_alias(ifr);
694			continue;
695		}
696		(void) strncpy(name, ifr->ifr_name, sizeof(ifr->ifr_name));
697		ifreq = *ifr;
698
699		if (getinfo(&ifreq) < 0)
700			continue;
701		if (bflag && (flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))
702			continue;
703		if (dflag && (flags & IFF_UP) != 0)
704			continue;
705		if (uflag && (flags & IFF_UP) == 0)
706			continue;
707
708		if (sflag && carrier())
709			continue;
710		idx++;
711		/*
712		 * Are we just listing the interfaces?
713		 */
714		if (lflag) {
715			if (idx > 1)
716				putchar(' ');
717			fputs(name, stdout);
718			continue;
719		}
720
721		if (sdl == NULL) {
722			status(NULL, 0);
723		} else {
724			status(LLADDR(sdl), sdl->sdl_alen);
725			sdl = NULL;
726		}
727	}
728	if (lflag)
729		putchar('\n');
730}
731
732#define RIDADDR 0
733#define ADDR	1
734#define MASK	2
735#define DSTADDR	3
736
737/*ARGSUSED*/
738void
739setifaddr(addr, param)
740	char *addr;
741	int param;
742{
743	/*
744	 * Delay the ioctl to set the interface addr until flags are all set.
745	 * The address interpretation may depend on the flags,
746	 * and the flags may change when the address is set.
747	 */
748	setaddr++;
749	if (newaddr == -1)
750		newaddr = 1;
751	if (doalias == 0)
752		clearaddr = 1;
753	(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
754}
755
756void
757setifnetmask(addr, d)
758	char *addr;
759	int d;
760{
761	(*afp->af_getaddr)(addr, MASK);
762}
763
764void
765setifbroadaddr(addr, d)
766	char *addr;
767	int d;
768{
769	(*afp->af_getaddr)(addr, DSTADDR);
770}
771
772void
773setifipdst(addr, d)
774	char *addr;
775	int d;
776{
777	in_getaddr(addr, DSTADDR);
778	setipdst++;
779	clearaddr = 0;
780	newaddr = 0;
781}
782
783#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
784/*ARGSUSED*/
785void
786notealias(addr, param)
787	char *addr;
788	int param;
789{
790	if (setaddr && doalias == 0 && param < 0)
791		(void) memcpy(rqtosa(af_ridreq), rqtosa(af_addreq),
792		    rqtosa(af_addreq)->sa_len);
793	doalias = param;
794	if (param < 0) {
795		clearaddr = 1;
796		newaddr = 0;
797	} else
798		clearaddr = 0;
799}
800
801/*ARGSUSED*/
802void
803notrailers(vname, value)
804	char *vname;
805	int value;
806{
807	puts("Note: trailers are no longer sent, but always received");
808}
809
810/*ARGSUSED*/
811void
812setifdstaddr(addr, param)
813	char *addr;
814	int param;
815{
816	(*afp->af_getaddr)(addr, DSTADDR);
817}
818
819void
820setifflags(vname, value)
821	char *vname;
822	int value;
823{
824 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0)
825		err(1, "SIOCGIFFLAGS");
826	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
827 	flags = ifr.ifr_flags;
828
829	if (value < 0) {
830		value = -value;
831		flags &= ~value;
832	} else
833		flags |= value;
834	ifr.ifr_flags = flags;
835	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
836		err(1, "SIOCSIFFLAGS");
837
838	reset_if_flags = 1;
839}
840
841#ifdef INET6
842void
843setia6flags(vname, value)
844	char *vname;
845	int value;
846{
847	if (value < 0) {
848		value = -value;
849		in6_addreq.ifra_flags &= ~value;
850	} else
851		in6_addreq.ifra_flags |= value;
852}
853
854void
855setia6pltime(val, d)
856	char *val;
857	int d;
858{
859	setia6lifetime("pltime", val);
860}
861
862void
863setia6vltime(val, d)
864	char *val;
865	int d;
866{
867	setia6lifetime("vltime", val);
868}
869
870void
871setia6lifetime(cmd, val)
872	char *cmd;
873	char *val;
874{
875	time_t newval, t;
876	char *ep;
877
878	t = time(NULL);
879	newval = (time_t)strtoul(val, &ep, 0);
880	if (val == ep)
881		errx(1, "invalid %s", cmd);
882	if (afp->af_af != AF_INET6)
883		errx(1, "%s not allowed for the AF", cmd);
884	if (strcmp(cmd, "vltime") == 0) {
885		in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
886		in6_addreq.ifra_lifetime.ia6t_vltime = newval;
887	} else if (strcmp(cmd, "pltime") == 0) {
888		in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
889		in6_addreq.ifra_lifetime.ia6t_pltime = newval;
890	}
891}
892#endif
893
894void
895setifmetric(val, d)
896	char *val;
897	int d;
898{
899	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
900	ifr.ifr_metric = atoi(val);
901	if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
902		warn("SIOCSIFMETRIC");
903}
904
905void
906setifmtu(val, d)
907	char *val;
908	int d;
909{
910	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
911	ifr.ifr_mtu = atoi(val);
912	if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
913		warn("SIOCSIFMTU");
914}
915
916void
917setifnwid(val, d)
918	char *val;
919	int d;
920{
921	u_int8_t nwid[IEEE80211_NWID_LEN];
922
923	memset(&nwid, 0, sizeof(nwid));
924	(void)strncpy(nwid, val, sizeof(nwid));
925	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
926	ifr.ifr_data = (caddr_t)nwid;
927	if (ioctl(s, SIOCS80211NWID, (caddr_t)&ifr) < 0)
928		warn("SIOCS80211NWID");
929}
930
931void
932ieee80211_status()
933{
934	u_int8_t nwid[IEEE80211_NWID_LEN + 1];
935
936	memset(&ifr, 0, sizeof(ifr));
937	ifr.ifr_data = (caddr_t)nwid;
938	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
939	nwid[IEEE80211_NWID_LEN] = 0;
940	if (ioctl(s, SIOCG80211NWID, (caddr_t)&ifr) == 0)
941		printf("\tnwid %s\n", nwid);
942}
943
944void
945init_current_media()
946{
947	struct ifmediareq ifmr;
948
949	/*
950	 * If we have not yet done so, grab the currently-selected
951	 * media.
952	 */
953	if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
954		(void) memset(&ifmr, 0, sizeof(ifmr));
955		(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
956
957		if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
958			/*
959			 * If we get E2BIG, the kernel is telling us
960			 * that there are more, so we can ignore it.
961			 */
962			if (errno != E2BIG)
963				err(1, "SGIOCGIFMEDIA");
964		}
965
966		media_current = ifmr.ifm_current;
967	}
968
969	/* Sanity. */
970	if (IFM_TYPE(media_current) == 0)
971		errx(1, "%s: no link type?", name);
972}
973
974void
975process_media_commands()
976{
977
978	if ((actions & (A_MEDIA|A_MEDIAOPT)) == 0) {
979		/* Nothing to do. */
980		return;
981	}
982
983	/*
984	 * Media already set up, and commands sanity-checked.  Set/clear
985	 * any options, and we're ready to go.
986	 */
987	media_current |= mediaopt_set;
988	media_current &= ~mediaopt_clear;
989
990	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
991	ifr.ifr_media = media_current;
992
993	if (ioctl(s, SIOCSIFMEDIA, (caddr_t)&ifr) < 0)
994		err(1, "SIOCSIFMEDIA");
995}
996
997void
998setmedia(val, d)
999	char *val;
1000	int d;
1001{
1002	int type, subtype, inst;
1003
1004	init_current_media();
1005
1006	/* Only one media command may be given. */
1007	if (actions & A_MEDIA)
1008		errx(1, "only one `media' command may be issued");
1009
1010	/* Must not come after mediaopt commands */
1011	if (actions & A_MEDIAOPT)
1012		errx(1, "may not issue `media' after `mediaopt' commands");
1013
1014	/*
1015	 * No need to check if `instance' has been issued; setmediainst()
1016	 * craps out if `media' has not been specified.
1017	 */
1018
1019	type = IFM_TYPE(media_current);
1020	inst = IFM_INST(media_current);
1021
1022	/* Look up the subtype. */
1023	subtype = get_media_subtype(type, val);
1024
1025	/* Build the new current media word. */
1026	media_current = IFM_MAKEWORD(type, subtype, 0, inst);
1027
1028	/* Media will be set after other processing is complete. */
1029}
1030
1031void
1032setmediaopt(val, d)
1033	char *val;
1034	int d;
1035{
1036
1037	init_current_media();
1038
1039	/* Can only issue `mediaopt' once. */
1040	if (actions & A_MEDIAOPTSET)
1041		errx(1, "only one `mediaopt' command may be issued");
1042
1043	/* Can't issue `mediaopt' if `instance' has already been issued. */
1044	if (actions & A_MEDIAINST)
1045		errx(1, "may not issue `mediaopt' after `instance'");
1046
1047	mediaopt_set = get_media_options(IFM_TYPE(media_current), val);
1048
1049	/* Media will be set after other processing is complete. */
1050}
1051
1052void
1053unsetmediaopt(val, d)
1054	char *val;
1055	int d;
1056{
1057
1058	init_current_media();
1059
1060	/* Can only issue `-mediaopt' once. */
1061	if (actions & A_MEDIAOPTCLR)
1062		errx(1, "only one `-mediaopt' command may be issued");
1063
1064	/* May not issue `media' and `-mediaopt'. */
1065	if (actions & A_MEDIA)
1066		errx(1, "may not issue both `media' and `-mediaopt'");
1067
1068	/*
1069	 * No need to check for A_MEDIAINST, since the test for A_MEDIA
1070	 * implicitly checks for A_MEDIAINST.
1071	 */
1072
1073	mediaopt_clear = get_media_options(IFM_TYPE(media_current), val);
1074
1075	/* Media will be set after other processing is complete. */
1076}
1077
1078void
1079setmediainst(val, d)
1080	char *val;
1081	int d;
1082{
1083	int type, subtype, options, inst;
1084
1085	init_current_media();
1086
1087	/* Can only issue `instance' once. */
1088	if (actions & A_MEDIAINST)
1089		errx(1, "only one `instance' command may be issued");
1090
1091	/* Must have already specified `media' */
1092	if ((actions & A_MEDIA) == 0)
1093		errx(1, "must specify `media' before `instance'");
1094
1095	type = IFM_TYPE(media_current);
1096	subtype = IFM_SUBTYPE(media_current);
1097	options = IFM_OPTIONS(media_current);
1098
1099	inst = atoi(val);
1100	if (inst < 0 || inst > IFM_INST_MAX)
1101		errx(1, "invalid media instance: %s", val);
1102
1103	media_current = IFM_MAKEWORD(type, subtype, options, inst);
1104
1105	/* Media will be set after other processing is complete. */
1106}
1107
1108struct ifmedia_description ifm_type_descriptions[] =
1109    IFM_TYPE_DESCRIPTIONS;
1110
1111struct ifmedia_description ifm_subtype_descriptions[] =
1112    IFM_SUBTYPE_DESCRIPTIONS;
1113
1114struct ifmedia_description ifm_option_descriptions[] =
1115    IFM_OPTION_DESCRIPTIONS;
1116
1117const char *
1118get_media_type_string(mword)
1119	int mword;
1120{
1121	struct ifmedia_description *desc;
1122
1123	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
1124	     desc++) {
1125		if (IFM_TYPE(mword) == desc->ifmt_word)
1126			return (desc->ifmt_string);
1127	}
1128	return ("<unknown type>");
1129}
1130
1131const char *
1132get_media_subtype_string(mword)
1133	int mword;
1134{
1135	struct ifmedia_description *desc;
1136
1137	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
1138	     desc++) {
1139		if (IFM_TYPE_MATCH(desc->ifmt_word, mword) &&
1140		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(mword))
1141			return (desc->ifmt_string);
1142	}
1143	return ("<unknown subtype>");
1144}
1145
1146int
1147get_media_subtype(type, val)
1148	int type;
1149	const char *val;
1150{
1151	int rval;
1152
1153	rval = lookup_media_word(ifm_subtype_descriptions, type, val);
1154	if (rval == -1)
1155		errx(1, "unknown %s media subtype: %s",
1156		    get_media_type_string(type), val);
1157
1158	return (rval);
1159}
1160
1161int
1162get_media_options(type, val)
1163	int type;
1164	const char *val;
1165{
1166	char *optlist, *str;
1167	int option, rval = 0;
1168
1169	/* We muck with the string, so copy it. */
1170	optlist = strdup(val);
1171	if (optlist == NULL)
1172		err(1, "strdup");
1173	str = optlist;
1174
1175	/*
1176	 * Look up the options in the user-provided comma-separated list.
1177	 */
1178	for (; (str = strtok(str, ",")) != NULL; str = NULL) {
1179		option = lookup_media_word(ifm_option_descriptions, type, str);
1180		if (option == -1)
1181			errx(1, "unknown %s media option: %s",
1182			    get_media_type_string(type), str);
1183		rval |= IFM_OPTIONS(option);
1184	}
1185
1186	free(optlist);
1187	return (rval);
1188}
1189
1190int
1191lookup_media_word(desc, type, val)
1192	struct ifmedia_description *desc;
1193	int type;
1194	const char *val;
1195{
1196
1197	for (; desc->ifmt_string != NULL; desc++) {
1198		if (IFM_TYPE_MATCH(desc->ifmt_word, type) &&
1199		    strcasecmp(desc->ifmt_string, val) == 0)
1200			return (desc->ifmt_word);
1201	}
1202	return (-1);
1203}
1204
1205void
1206print_media_word(ifmw, print_type, as_syntax)
1207	int ifmw, print_type, as_syntax;
1208{
1209	struct ifmedia_description *desc;
1210	int seen_option = 0;
1211
1212	if (print_type)
1213		printf("%s ", get_media_type_string(ifmw));
1214	printf("%s%s", as_syntax ? "media " : "",
1215	    get_media_subtype_string(ifmw));
1216
1217	/* Find options. */
1218	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
1219	     desc++) {
1220		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
1221		    (ifmw & desc->ifmt_word) != 0 &&
1222		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
1223			if (seen_option == 0)
1224				printf(" %s", as_syntax ? "mediaopt " : "");
1225			printf("%s%s", seen_option ? "," : "",
1226			    desc->ifmt_string);
1227			seen_option |= IFM_OPTIONS(desc->ifmt_word);
1228		}
1229	}
1230	if (IFM_INST(ifmw) != 0)
1231		printf(" instance %d", IFM_INST(ifmw));
1232}
1233
1234int carrier()
1235{
1236	struct ifmediareq ifmr;
1237
1238	(void) memset(&ifmr, 0, sizeof(ifmr));
1239	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1240
1241	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
1242		/*
1243		 * Interface doesn't support SIOC{G,S}IFMEDIA;
1244		 * assume ok.
1245		 */
1246		return 0;
1247	}
1248	if ((ifmr.ifm_status & IFM_AVALID) == 0) {
1249		/*
1250		 * Interface doesn't report media-valid status.
1251		 * assume ok.
1252		 */
1253		return 0;
1254	}
1255	/* otherwise, return ok for active, not-ok if not active. */
1256	return !(ifmr.ifm_status & IFM_ACTIVE);
1257}
1258
1259
1260#define	IFFBITS \
1261"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
1262\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
1263
1264const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
1265
1266const struct ifmedia_status_description ifm_status_descriptions[] =
1267    IFM_STATUS_DESCRIPTIONS;
1268
1269/*
1270 * Print the status of the interface.  If an address family was
1271 * specified, show it and it only; otherwise, show them all.
1272 */
1273void
1274status(ap, alen)
1275	const u_int8_t *ap;
1276	int alen;
1277{
1278	struct afswtch *p = afp;
1279	struct ifmediareq ifmr;
1280	int *media_list, i;
1281
1282	printf("%s: ", name);
1283	printb("flags", flags, IFFBITS);
1284	if (metric)
1285		printf(" metric %d", metric);
1286	if (mtu)
1287		printf(" mtu %d", mtu);
1288	putchar('\n');
1289	if (ap && alen > 0) {
1290		printf("\taddress:");
1291		for (i = 0; i < alen; i++, ap++)
1292			printf("%c%02x", i > 0 ? ':' : ' ', *ap);
1293		putchar('\n');
1294	}
1295
1296	(void) memset(&ifmr, 0, sizeof(ifmr));
1297	(void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
1298
1299	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
1300		/*
1301		 * Interface doesn't support SIOC{G,S}IFMEDIA.
1302		 */
1303		goto proto_status;
1304	}
1305
1306	if (ifmr.ifm_count == 0) {
1307		warnx("%s: no media types?", name);
1308		goto proto_status;
1309	}
1310
1311	media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
1312	if (media_list == NULL)
1313		err(1, "malloc");
1314	ifmr.ifm_ulist = media_list;
1315
1316	if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
1317		err(1, "SIOCGIFMEDIA");
1318
1319	printf("\tmedia: ");
1320	print_media_word(ifmr.ifm_current, 1, 0);
1321	if (ifmr.ifm_active != ifmr.ifm_current) {
1322		putchar(' ');
1323		putchar('(');
1324		print_media_word(ifmr.ifm_active, 0, 0);
1325		putchar(')');
1326	}
1327	putchar('\n');
1328
1329	if (ifmr.ifm_status & IFM_STATUS_VALID) {
1330		const struct ifmedia_status_description *ifms;
1331		int bitno, found = 0;
1332
1333		printf("\tstatus: ");
1334		for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
1335			for (ifms = ifm_status_descriptions;
1336			     ifms->ifms_valid != 0; ifms++) {
1337				if (ifms->ifms_type !=
1338				      IFM_TYPE(ifmr.ifm_active) &&
1339				    ifms->ifms_valid !=
1340				      ifm_status_valid_list[bitno])
1341					continue;
1342				printf("%s%s", found ? ", " : "",
1343				    IFM_STATUS_DESC(ifms, ifmr.ifm_status));
1344				found = 1;
1345
1346				/*
1347				 * For each valid indicator bit, there's
1348				 * only one entry for each media type, so
1349				 * terminate the inner loop now.
1350				 */
1351				break;
1352			}
1353		}
1354
1355		if (found == 0)
1356			printf("unknown");
1357		putchar('\n');
1358	}
1359
1360	if (mflag) {
1361		int type, printed_type;
1362
1363		for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
1364			for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
1365				if (IFM_TYPE(media_list[i]) == type) {
1366					if (printed_type == 0) {
1367					    printf("\tsupported %s media:\n",
1368					      get_media_type_string(type));
1369					    printed_type = 1;
1370					}
1371					printf("\t\t");
1372					print_media_word(media_list[i], 0, 1);
1373					printf("\n");
1374				}
1375			}
1376		}
1377	}
1378
1379	free(media_list);
1380
1381 proto_status:
1382	ieee80211_status();
1383
1384	if ((p = afp) != NULL) {
1385		(*p->af_status)(1);
1386		if (Aflag & !aflag)
1387			printalias(name, p->af_af);
1388	} else for (p = afs; p->af_name; p++) {
1389		ifr.ifr_addr.sa_family = p->af_af;
1390		(*p->af_status)(0);
1391		if (Aflag & !aflag && p->af_af == AF_INET)
1392			printalias(name, p->af_af);
1393	}
1394}
1395
1396void
1397in_alias(creq)
1398	struct ifreq *creq;
1399{
1400	struct sockaddr_in *sin;
1401
1402	if (lflag)
1403		return;
1404
1405	/* Get the non-alias address for this interface. */
1406	getsock(AF_INET);
1407	if (s < 0) {
1408		if (errno == EPROTONOSUPPORT)
1409			return;
1410		err(1, "socket");
1411	}
1412	(void) memset(&ifr, 0, sizeof(ifr));
1413	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1414	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
1415		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1416			return;
1417		} else
1418			warn("SIOCGIFADDR");
1419	}
1420	/* If creq and ifr are the same address, this is not an alias. */
1421	if (memcmp(&ifr.ifr_addr, &creq->ifr_addr,
1422		   sizeof(creq->ifr_addr)) == 0)
1423		return;
1424	(void) memset(&addreq, 0, sizeof(addreq));
1425	(void) strncpy(addreq.ifra_name, name, sizeof(addreq.ifra_name));
1426	addreq.ifra_addr = creq->ifr_addr;
1427	if (ioctl(s, SIOCGIFALIAS, (caddr_t)&addreq) < 0) {
1428		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1429			return;
1430		} else
1431			warn("SIOCGIFALIAS");
1432	}
1433
1434	sin = (struct sockaddr_in *)&addreq.ifra_addr;
1435	printf("\tinet alias %s", inet_ntoa(sin->sin_addr));
1436
1437	if (flags & IFF_POINTOPOINT) {
1438		sin = (struct sockaddr_in *)&addreq.ifra_dstaddr;
1439		printf(" -> %s", inet_ntoa(sin->sin_addr));
1440	}
1441
1442	sin = (struct sockaddr_in *)&addreq.ifra_mask;
1443	printf(" netmask 0x%x", ntohl(sin->sin_addr.s_addr));
1444
1445	if (flags & IFF_BROADCAST) {
1446		sin = (struct sockaddr_in *)&addreq.ifra_broadaddr;
1447		printf(" broadcast %s", inet_ntoa(sin->sin_addr));
1448	}
1449	printf("\n");
1450}
1451
1452void
1453in_status(force)
1454	int force;
1455{
1456	struct sockaddr_in *sin;
1457
1458	getsock(AF_INET);
1459	if (s < 0) {
1460		if (errno == EPROTONOSUPPORT)
1461			return;
1462		err(1, "socket");
1463	}
1464	(void) memset(&ifr, 0, sizeof(ifr));
1465	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1466	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
1467		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1468			if (!force)
1469				return;
1470			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1471		} else
1472			warn("SIOCGIFADDR");
1473	}
1474	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1475	sin = (struct sockaddr_in *)&ifr.ifr_addr;
1476	printf("\tinet %s ", inet_ntoa(sin->sin_addr));
1477	(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1478	if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
1479		if (errno != EADDRNOTAVAIL)
1480			warn("SIOCGIFNETMASK");
1481		(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1482	} else
1483		netmask.sin_addr =
1484		    ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
1485	if (flags & IFF_POINTOPOINT) {
1486		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
1487			if (errno == EADDRNOTAVAIL)
1488			    (void) memset(&ifr.ifr_addr, 0,
1489				sizeof(ifr.ifr_addr));
1490			else
1491			    warn("SIOCGIFDSTADDR");
1492		}
1493		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1494		sin = (struct sockaddr_in *)&ifr.ifr_dstaddr;
1495		printf("--> %s ", inet_ntoa(sin->sin_addr));
1496	}
1497	printf("netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
1498	if (flags & IFF_BROADCAST) {
1499		if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) {
1500			if (errno == EADDRNOTAVAIL)
1501				(void) memset(&ifr.ifr_addr, 0,
1502				    sizeof(ifr.ifr_addr));
1503			else
1504			    warn("SIOCGIFBRDADDR");
1505		}
1506		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1507		sin = (struct sockaddr_in *)&ifr.ifr_addr;
1508		if (sin->sin_addr.s_addr != 0)
1509			printf("broadcast %s", inet_ntoa(sin->sin_addr));
1510	}
1511	putchar('\n');
1512}
1513
1514void
1515setifprefixlen(addr, d)
1516	char *addr;
1517	int d;
1518{
1519	if (*afp->af_getprefix)
1520		(*afp->af_getprefix)(addr, MASK);
1521	explicit_prefix = 1;
1522}
1523
1524#ifdef INET6
1525void
1526in6_fillscopeid(sin6)
1527	struct sockaddr_in6 *sin6;
1528{
1529#if defined(__KAME__) && defined(KAME_SCOPEID)
1530	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1531		sin6->sin6_scope_id =
1532			ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
1533		sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
1534	}
1535#endif
1536}
1537
1538/* XXX not really an alias */
1539void
1540in6_alias(creq)
1541	struct in6_ifreq *creq;
1542{
1543	struct sockaddr_in6 *sin6;
1544	char hbuf[NI_MAXHOST];
1545	u_int32_t scopeid;
1546#ifdef NI_WITHSCOPEID
1547	const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
1548#else
1549	const int niflag = NI_NUMERICHOST;
1550#endif
1551
1552	/* Get the non-alias address for this interface. */
1553	getsock(AF_INET6);
1554	if (s < 0) {
1555		if (errno == EPROTONOSUPPORT)
1556			return;
1557		err(1, "socket");
1558	}
1559
1560	sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
1561
1562	in6_fillscopeid(sin6);
1563	scopeid = sin6->sin6_scope_id;
1564	if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
1565			hbuf, sizeof(hbuf), NULL, 0, niflag))
1566		strncpy(hbuf, "", sizeof(hbuf));	/* some message? */
1567	printf("\tinet6 %s", hbuf);
1568
1569	if (flags & IFF_POINTOPOINT) {
1570		(void) memset(&ifr6, 0, sizeof(ifr6));
1571		(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
1572		ifr6.ifr_addr = creq->ifr_addr;
1573		if (ioctl(s, SIOCGIFDSTADDR_IN6, (caddr_t)&ifr6) < 0) {
1574			if (errno != EADDRNOTAVAIL)
1575				warn("SIOCGIFDSTADDR_IN6");
1576			(void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
1577			ifr6.ifr_addr.sin6_family = AF_INET6;
1578			ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
1579		}
1580		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
1581		in6_fillscopeid(sin6);
1582		hbuf[0] = '\0';
1583		if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
1584				hbuf, sizeof(hbuf), NULL, 0, niflag))
1585			strncpy(hbuf, "", sizeof(hbuf)); /* some message? */
1586		printf(" -> %s", hbuf);
1587	}
1588
1589	(void) memset(&ifr6, 0, sizeof(ifr6));
1590	(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
1591	ifr6.ifr_addr = creq->ifr_addr;
1592	if (ioctl(s, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) {
1593		if (errno != EADDRNOTAVAIL)
1594			warn("SIOCGIFNETMASK_IN6");
1595	} else {
1596		sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
1597		printf(" prefixlen %d", prefix(&sin6->sin6_addr,
1598					       sizeof(struct in6_addr)));
1599	}
1600
1601	(void) memset(&ifr6, 0, sizeof(ifr6));
1602	(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
1603	ifr6.ifr_addr = creq->ifr_addr;
1604	if (ioctl(s, SIOCGIFAFLAG_IN6, (caddr_t)&ifr6) < 0) {
1605		if (errno != EADDRNOTAVAIL)
1606			warn("SIOCGIFAFLAG_IN6");
1607	} else {
1608		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
1609			printf(" anycast");
1610		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
1611			printf(" tentative");
1612		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
1613			printf(" duplicated");
1614		if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
1615			printf(" detached");
1616	}
1617
1618	if (scopeid)
1619		printf(" scopeid 0x%x", scopeid);
1620
1621	if (Lflag) {
1622		struct in6_addrlifetime *lifetime;
1623		(void) memset(&ifr6, 0, sizeof(ifr6));
1624		(void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
1625		ifr6.ifr_addr = creq->ifr_addr;
1626		lifetime = &ifr6.ifr_ifru.ifru_lifetime;
1627		if (ioctl(s, SIOCGIFALIFETIME_IN6, (caddr_t)&ifr6) < 0) {
1628			if (errno != EADDRNOTAVAIL)
1629				warn("SIOCGIFALIFETIME_IN6");
1630		} else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
1631			time_t t = time(NULL);
1632			printf(" pltime ");
1633			if (lifetime->ia6t_preferred) {
1634				printf("%s", lifetime->ia6t_preferred < t
1635					? "0"
1636					: sec2str(lifetime->ia6t_preferred - t));
1637			} else
1638				printf("infty");
1639
1640			printf(" vltime ");
1641			if (lifetime->ia6t_expire) {
1642				printf("%s", lifetime->ia6t_expire < t
1643					? "0"
1644					: sec2str(lifetime->ia6t_expire - t));
1645			} else
1646				printf("infty");
1647		}
1648	}
1649
1650	printf("\n");
1651}
1652
1653void
1654in6_status(force)
1655	int force;
1656{
1657	char inbuf[8192];
1658	struct ifconf ifc;
1659	struct ifreq *ifr;
1660	int i, siz;
1661	char ifrbuf[8192];
1662
1663	ifc.ifc_len = sizeof(inbuf);
1664	ifc.ifc_buf = inbuf;
1665	getsock(af);
1666	if (s < 0)
1667		err(1, "socket");
1668	if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
1669		err(1, "SIOCGIFCONF");
1670	ifr = ifc.ifc_req;
1671	for (i = 0; i < ifc.ifc_len; ) {
1672		ifr = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
1673		siz = sizeof(ifr->ifr_name) +
1674			(ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
1675				? ifr->ifr_addr.sa_len
1676				: sizeof(struct sockaddr));
1677		i += siz;
1678		/* avoid alignment issue */
1679		if (sizeof(ifrbuf) < siz)
1680			errx(1, "ifr too big");
1681		memcpy(ifrbuf, ifr, siz);
1682		ifr = (struct ifreq *)ifrbuf;
1683		if (!strncmp(name, ifr->ifr_name, sizeof(ifr->ifr_name))) {
1684			if (ifr->ifr_addr.sa_family == AF_INET6)
1685				in6_alias((struct in6_ifreq *)ifr);
1686		}
1687	}
1688}
1689#endif /*INET6*/
1690
1691#ifndef INET_ONLY
1692
1693void
1694at_status(force)
1695	int force;
1696{
1697	struct sockaddr_at *sat, null_sat;
1698	struct netrange *nr;
1699
1700	getsock(AF_APPLETALK);
1701	if (s < 0) {
1702		if (errno == EPROTONOSUPPORT)
1703			return;
1704		err(1, "socket");
1705	}
1706	(void) memset(&ifr, 0, sizeof(ifr));
1707	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1708	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
1709		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1710			if (!force)
1711				return;
1712			(void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1713		} else
1714			warn("SIOCGIFADDR");
1715	}
1716	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1717	sat = (struct sockaddr_at *)&ifr.ifr_addr;
1718
1719	(void) memset(&null_sat, 0, sizeof(null_sat));
1720
1721	nr = (struct netrange *) &sat->sat_zero;
1722	printf("\tatalk %d.%d range %d-%d phase %d",
1723	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1724	    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1725	if (flags & IFF_POINTOPOINT) {
1726		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
1727			if (errno == EADDRNOTAVAIL)
1728			    (void) memset(&ifr.ifr_addr, 0,
1729				sizeof(ifr.ifr_addr));
1730			else
1731			    warn("SIOCGIFDSTADDR");
1732		}
1733		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1734		sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
1735		if (!sat)
1736			sat = &null_sat;
1737		printf("--> %d.%d",
1738		    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1739	}
1740	if (flags & IFF_BROADCAST) {
1741		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
1742		sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
1743		if (sat)
1744			printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
1745			    sat->sat_addr.s_node);
1746	}
1747	putchar('\n');
1748}
1749
1750void
1751xns_status(force)
1752	int force;
1753{
1754	struct sockaddr_ns *sns;
1755
1756	getsock(AF_NS);
1757	if (s < 0) {
1758		if (errno == EPROTONOSUPPORT)
1759			return;
1760		err(1, "socket");
1761	}
1762	(void) memset(&ifr, 0, sizeof(ifr));
1763	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1764	if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
1765		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1766			if (!force)
1767				return;
1768			memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1769		} else
1770			warn("SIOCGIFADDR");
1771	}
1772	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1773	sns = (struct sockaddr_ns *)&ifr.ifr_addr;
1774	printf("\tns %s ", ns_ntoa(sns->sns_addr));
1775	if (flags & IFF_POINTOPOINT) { /* by W. Nesheim@Cornell */
1776		if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
1777			if (errno == EADDRNOTAVAIL)
1778			    memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1779			else
1780			    warn("SIOCGIFDSTADDR");
1781		}
1782		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1783		sns = (struct sockaddr_ns *)&ifr.ifr_dstaddr;
1784		printf("--> %s ", ns_ntoa(sns->sns_addr));
1785	}
1786	putchar('\n');
1787}
1788
1789void
1790iso_status(force)
1791	int force;
1792{
1793	struct sockaddr_iso *siso;
1794	struct iso_ifreq ifr;
1795
1796	getsock(AF_ISO);
1797	if (s < 0) {
1798		if (errno == EPROTONOSUPPORT)
1799			return;
1800		err(1, "socket");
1801	}
1802	(void) memset(&ifr, 0, sizeof(ifr));
1803	(void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1804	if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&ifr) < 0) {
1805		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
1806			if (!force)
1807				return;
1808			(void) memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
1809		} else
1810			warn("SIOCGIFADDR_ISO");
1811	}
1812	(void) strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
1813	siso = &ifr.ifr_Addr;
1814	printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
1815	if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&ifr) < 0) {
1816		if (errno == EADDRNOTAVAIL)
1817			memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
1818		else
1819			warn("SIOCGIFNETMASK_ISO");
1820	} else {
1821		if (siso->siso_len > offsetof(struct sockaddr_iso, siso_addr))
1822			siso->siso_addr.isoa_len = siso->siso_len
1823			    - offsetof(struct sockaddr_iso, siso_addr);
1824		printf("\n\t\tnetmask %s ", iso_ntoa(&siso->siso_addr));
1825	}
1826	if (flags & IFF_POINTOPOINT) {
1827		if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&ifr) < 0) {
1828			if (errno == EADDRNOTAVAIL)
1829			    memset(&ifr.ifr_Addr, 0, sizeof(ifr.ifr_Addr));
1830			else
1831			    warn("SIOCGIFDSTADDR_ISO");
1832		}
1833		(void) strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1834		siso = &ifr.ifr_Addr;
1835		printf("--> %s ", iso_ntoa(&siso->siso_addr));
1836	}
1837	putchar('\n');
1838}
1839
1840#endif	/* INET_ONLY */
1841
1842#define SIN(x) ((struct sockaddr_in *) &(x))
1843struct sockaddr_in *sintab[] = {
1844SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1845SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1846
1847void
1848in_getaddr(s, which)
1849	char *s;
1850	int which;
1851{
1852	struct sockaddr_in *sin = sintab[which];
1853	struct hostent *hp;
1854	struct netent *np;
1855
1856	sin->sin_len = sizeof(*sin);
1857	if (which != MASK)
1858		sin->sin_family = AF_INET;
1859
1860	if (which == ADDR) {
1861		char *p = NULL;
1862
1863		if((p = strrchr(s, '/')) != NULL) {
1864			/* address is `name/masklen' */
1865			int masklen;
1866			int ret;
1867			struct sockaddr_in *min = sintab[MASK];
1868			*p = '\0';
1869			ret = sscanf(p+1, "%u", &masklen);
1870			if(ret != 1 || (masklen < 0 || masklen > 32)) {
1871				*p = '/';
1872				errx(1, "%s: bad value", s);
1873			}
1874			min->sin_len = sizeof(*min);
1875			min->sin_addr.s_addr =
1876				htonl(~((1LL << (32 - masklen)) - 1) &
1877				      0xffffffff);
1878		}
1879	}
1880
1881	if (inet_aton(s, &sin->sin_addr) == 0) {
1882		if ((hp = gethostbyname(s)) != NULL)
1883			(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
1884		else if ((np = getnetbyname(s)) != NULL)
1885			sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1886		else
1887			errx(1, "%s: bad value", s);
1888	}
1889}
1890
1891/*
1892 * Print a value a la the %b format of the kernel's printf
1893 */
1894void
1895printb(s, v, bits)
1896	char *s;
1897	char *bits;
1898	unsigned short v;
1899{
1900	int i, any = 0;
1901	char c;
1902
1903	if (bits && *bits == 8)
1904		printf("%s=%o", s, v);
1905	else
1906		printf("%s=%x", s, v);
1907	bits++;
1908	if (bits) {
1909		putchar('<');
1910		while ((i = *bits++) != 0) {
1911			if (v & (1 << (i-1))) {
1912				if (any)
1913					putchar(',');
1914				any = 1;
1915				for (; (c = *bits) > 32; bits++)
1916					putchar(c);
1917			} else
1918				for (; *bits > 32; bits++)
1919					;
1920		}
1921		putchar('>');
1922	}
1923}
1924
1925#ifdef INET6
1926#define SIN6(x) ((struct sockaddr_in6 *) &(x))
1927struct sockaddr_in6 *sin6tab[] = {
1928SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1929SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1930
1931void
1932in6_getaddr(s, which)
1933	char *s;
1934	int which;
1935{
1936#if defined(__KAME__) && defined(KAME_SCOPEID)
1937	struct sockaddr_in6 *sin6 = sin6tab[which];
1938	struct addrinfo hints, *res;
1939	int error;
1940
1941	memset(&hints, 0, sizeof(hints));
1942	hints.ai_family = AF_INET6;
1943	hints.ai_socktype = SOCK_DGRAM;
1944#if 0 /* in_getaddr() allows FQDN */
1945	hints.ai_flags = AI_NUMERICHOST;
1946#endif
1947	error = getaddrinfo(s, "0", &hints, &res);
1948	if (error)
1949		errx(1, "%s: %s", s, gai_strerror(error));
1950	if (res->ai_next)
1951		errx(1, "%s: resolved to multiple hosts", s);
1952	if (res->ai_addrlen != sizeof(struct sockaddr_in6))
1953		errx(1, "%s: bad value", s);
1954	memcpy(sin6, res->ai_addr, res->ai_addrlen);
1955	freeaddrinfo(res);
1956	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && sin6->sin6_scope_id) {
1957		*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] =
1958			htons(sin6->sin6_scope_id);
1959		sin6->sin6_scope_id = 0;
1960	}
1961#else
1962	struct sockaddr_in6 *sin = sin6tab[which];
1963
1964	sin->sin6_len = sizeof(*sin);
1965	if (which != MASK)
1966		sin->sin6_family = AF_INET6;
1967
1968	if (which == ADDR) {
1969		char *p = NULL;
1970		if((p = strrchr(s, '/')) != NULL) {
1971			*p = '\0';
1972			in6_getprefix(p + 1, MASK);
1973			explicit_prefix = 1;
1974		}
1975	}
1976
1977	if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
1978		errx(1, "%s: bad value", s);
1979#endif
1980}
1981
1982void
1983in6_getprefix(plen, which)
1984	char *plen;
1985	int which;
1986{
1987	register struct sockaddr_in6 *sin = sin6tab[which];
1988	register u_char *cp;
1989	int len = strtol(plen, (char **)NULL, 10);
1990
1991	if ((len < 0) || (len > 128))
1992		errx(1, "%s: bad value", plen);
1993	sin->sin6_len = sizeof(*sin);
1994	if (which != MASK)
1995		sin->sin6_family = AF_INET6;
1996	if ((len == 0) || (len == 128)) {
1997		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
1998		return;
1999	}
2000	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
2001	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
2002		*cp++ = 0xff;
2003	*cp = 0xff << (8 - len);
2004}
2005
2006int
2007prefix(val, size)
2008	void *val;
2009	int size;
2010{
2011	register u_char *name = (u_char *)val;
2012	register int byte, bit, plen = 0;
2013
2014	for (byte = 0; byte < size; byte++, plen += 8)
2015		if (name[byte] != 0xff)
2016			break;
2017	if (byte == size)
2018		return (plen);
2019	for (bit = 7; bit != 0; bit--, plen++)
2020		if (!(name[byte] & (1 << bit)))
2021			break;
2022	for (; bit != 0; bit--)
2023		if (name[byte] & (1 << bit))
2024			return(0);
2025	byte++;
2026	for (; byte < size; byte++)
2027		if (name[byte])
2028			return(0);
2029	return (plen);
2030}
2031#endif /*INET6*/
2032
2033#ifndef INET_ONLY
2034void
2035at_getaddr(addr, which)
2036	char *addr;
2037	int which;
2038{
2039	struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
2040	u_int net, node;
2041
2042	sat->sat_family = AF_APPLETALK;
2043	sat->sat_len = sizeof(*sat);
2044	if (which == MASK)
2045		errx(1, "AppleTalk does not use netmasks\n");
2046	if (sscanf(addr, "%u.%u", &net, &node) != 2
2047	    || net == 0 || net > 0xffff || node == 0 || node > 0xfe)
2048		errx(1, "%s: illegal address", addr);
2049	sat->sat_addr.s_net = htons(net);
2050	sat->sat_addr.s_node = node;
2051}
2052
2053void
2054setatrange(range, d)
2055	char *range;
2056	int d;
2057{
2058	u_short	first = 123, last = 123;
2059
2060	if (sscanf(range, "%hu-%hu", &first, &last) != 2
2061	    || first == 0 || first > 0xffff
2062	    || last == 0 || last > 0xffff || first > last)
2063		errx(1, "%s: illegal net range: %u-%u", range, first, last);
2064	at_nr.nr_firstnet = htons(first);
2065	at_nr.nr_lastnet = htons(last);
2066}
2067
2068void
2069setatphase(phase, d)
2070	char *phase;
2071	int d;
2072{
2073	if (!strcmp(phase, "1"))
2074		at_nr.nr_phase = 1;
2075	else if (!strcmp(phase, "2"))
2076		at_nr.nr_phase = 2;
2077	else
2078		errx(1, "%s: illegal phase", phase);
2079}
2080
2081void
2082checkatrange(sat)
2083	struct sockaddr_at *sat;
2084{
2085	if (at_nr.nr_phase == 0)
2086		at_nr.nr_phase = 2;	/* Default phase 2 */
2087	if (at_nr.nr_firstnet == 0)
2088		at_nr.nr_firstnet =	/* Default range of one */
2089		at_nr.nr_lastnet = sat->sat_addr.s_net;
2090	printf("\tatalk %d.%d range %d-%d phase %d\n",
2091	ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
2092	ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
2093	if ((u_short) ntohs(at_nr.nr_firstnet) >
2094			(u_short) ntohs(sat->sat_addr.s_net)
2095		    || (u_short) ntohs(at_nr.nr_lastnet) <
2096			(u_short) ntohs(sat->sat_addr.s_net))
2097		errx(1, "AppleTalk address is not in range");
2098	*((struct netrange *) &sat->sat_zero) = at_nr;
2099}
2100
2101#define SNS(x) ((struct sockaddr_ns *) &(x))
2102struct sockaddr_ns *snstab[] = {
2103SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
2104SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
2105
2106void
2107xns_getaddr(addr, which)
2108	char *addr;
2109	int which;
2110{
2111	struct sockaddr_ns *sns = snstab[which];
2112
2113	sns->sns_family = AF_NS;
2114	sns->sns_len = sizeof(*sns);
2115	sns->sns_addr = ns_addr(addr);
2116	if (which == MASK)
2117		puts("Attempt to set XNS netmask will be ineffectual");
2118}
2119
2120#define SISO(x) ((struct sockaddr_iso *) &(x))
2121struct sockaddr_iso *sisotab[] = {
2122SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
2123SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
2124
2125void
2126iso_getaddr(addr, which)
2127	char *addr;
2128	int which;
2129{
2130	struct sockaddr_iso *siso = sisotab[which];
2131	siso->siso_addr = *iso_addr(addr);
2132
2133	if (which == MASK) {
2134		siso->siso_len = TSEL(siso) - (caddr_t)(siso);
2135		siso->siso_nlen = 0;
2136	} else {
2137		siso->siso_len = sizeof(*siso);
2138		siso->siso_family = AF_ISO;
2139	}
2140}
2141
2142void
2143setsnpaoffset(val, d)
2144	char *val;
2145	int d;
2146{
2147	iso_addreq.ifra_snpaoffset = atoi(val);
2148}
2149
2150void
2151setnsellength(val, d)
2152	char *val;
2153	int d;
2154{
2155	nsellength = atoi(val);
2156	if (nsellength < 0)
2157		errx(1, "Negative NSEL length is absurd");
2158	if (afp == 0 || afp->af_af != AF_ISO)
2159		errx(1, "Setting NSEL length valid only for iso");
2160}
2161
2162void
2163fixnsel(s)
2164	struct sockaddr_iso *s;
2165{
2166	if (s->siso_family == 0)
2167		return;
2168	s->siso_tlen = nsellength;
2169}
2170
2171void
2172adjust_nsellength()
2173{
2174	fixnsel(sisotab[RIDADDR]);
2175	fixnsel(sisotab[ADDR]);
2176	fixnsel(sisotab[DSTADDR]);
2177}
2178
2179#endif	/* INET_ONLY */
2180
2181void
2182usage()
2183{
2184	fprintf(stderr,
2185	    "usage: ifconfig [ -m ] [ -A ] %s interface\n%s%s%s%s%s%s%s%s%s%s%s%s",
2186#ifdef INET6
2187		"[ -L ]",
2188#else
2189		"",
2190#endif
2191		"\t[ af [ address [ dest_addr ] ] [ up ] [ down ] ",
2192		"[ netmask mask ] ]\n",
2193		"\t[ metric n ]\n",
2194		"\t[ mtu n ]\n",
2195		"\t[ arp | -arp ]\n",
2196		"\t[ media mtype ]\n",
2197		"\t[ mediaopt mopts ]\n",
2198		"\t[ -mediaopt mopts ]\n",
2199		"\t[ instance minst ]\n",
2200		"\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n",
2201		"       ifconfig -a [ -A ] [ -m ] [ -d ] [ -u ] [ af ]\n",
2202		"       ifconfig -l [ -d ] [ -u ]\n");
2203	exit(1);
2204}
2205
2206#ifdef INET6
2207char *
2208sec2str(total)
2209	time_t total;
2210{
2211	static char result[256];
2212	int days, hours, mins, secs;
2213	int first = 1;
2214	char *p = result;
2215
2216	if (0) {	/*XXX*/
2217		days = total / 3600 / 24;
2218		hours = (total / 3600) % 24;
2219		mins = (total / 60) % 60;
2220		secs = total % 60;
2221
2222		if (days) {
2223			first = 0;
2224			p += sprintf(p, "%dd", days);
2225		}
2226		if (!first || hours) {
2227			first = 0;
2228			p += sprintf(p, "%dh", hours);
2229		}
2230		if (!first || mins) {
2231			first = 0;
2232			p += sprintf(p, "%dm", mins);
2233		}
2234		sprintf(p, "%ds", secs);
2235	} else
2236		sprintf(p, "%lu", (u_long)total);
2237
2238	return(result);
2239}
2240#endif
2241