ifconfig.c revision 55215
1/*
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1983, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
43#endif
44static const char rcsid[] =
45  "$FreeBSD: head/sbin/ifconfig/ifconfig.c 55215 1999-12-29 13:53:13Z ru $";
46#endif /* not lint */
47
48#include <sys/param.h>
49#include <sys/ioctl.h>
50#include <sys/socket.h>
51#include <sys/sysctl.h>
52#include <sys/time.h>
53#include <sys/module.h>
54#include <sys/linker.h>
55
56#include <net/if.h>
57#include <net/if_var.h>
58#include <net/if_dl.h>
59#include <net/if_types.h>
60#include <net/route.h>
61
62/* IP */
63#include <netinet/in.h>
64#include <netinet/in_var.h>
65#include <arpa/inet.h>
66#include <netdb.h>
67
68/* IPX */
69#define	IPXIP
70#define IPTUNNEL
71#include <netipx/ipx.h>
72#include <netipx/ipx_if.h>
73
74/* Appletalk */
75#include <netatalk/at.h>
76
77/* XNS */
78#ifdef NS
79#define	NSIP
80#include <netns/ns.h>
81#include <netns/ns_if.h>
82#endif
83
84/* OSI */
85
86#include <ctype.h>
87#include <err.h>
88#include <errno.h>
89#include <fcntl.h>
90#include <stdio.h>
91#include <stdlib.h>
92#include <string.h>
93#include <unistd.h>
94
95#include "ifconfig.h"
96
97struct	ifreq		ifr, ridreq;
98struct	ifaliasreq	addreq;
99#ifdef INET6
100struct	in6_ifreq	in6_ridreq;
101struct	in6_aliasreq	in6_addreq;
102#endif
103struct	sockaddr_in	netmask;
104struct	netrange	at_nr;		/* AppleTalk net range */
105
106char	name[32];
107int	flags;
108int	metric;
109int	mtu;
110int	setaddr;
111int	setipdst;
112int	doalias;
113int	clearaddr;
114int	newaddr = 1;
115#ifdef INET6
116static	int ip6lifetime;
117#endif
118
119struct	afswtch;
120
121#ifdef INET6
122char	ntop_buf[INET6_ADDRSTRLEN];	/*inet_ntop()*/
123#endif
124
125void	Perror __P((const char *cmd));
126void	checkatrange __P((struct sockaddr_at *));
127int	ifconfig __P((int argc, char *const *argv, const struct afswtch *afp));
128void	notealias __P((const char *, int, int, const struct afswtch *afp));
129void	printb __P((const char *s, unsigned value, const char *bits));
130void	rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
131void	status __P((const struct afswtch *afp, int addrcount,
132		    struct sockaddr_dl *sdl, struct if_msghdr *ifm,
133		    struct ifa_msghdr *ifam));
134void	usage __P((void));
135void	ifmaybeload __P((char *name));
136
137#ifdef INET6
138int	prefix __P((void *, int));
139static	char *sec2str __P((time_t));
140int	explicit_prefix = 0;
141#endif
142
143typedef	void c_func __P((const char *cmd, int arg, int s, const struct afswtch *afp));
144c_func	setatphase, setatrange;
145c_func	setifaddr, setifbroadaddr, setifdstaddr, setifnetmask;
146#ifdef INET6
147c_func	setifprefixlen;
148c_func	setip6flags;
149#endif
150c_func	setifipdst;
151c_func	setifflags, setifmetric, setifmtu;
152
153
154#define	NEXTARG		0xffffff
155
156const
157struct	cmd {
158	const	char *c_name;
159	int	c_parameter;		/* NEXTARG means next argv */
160	void	(*c_func) __P((const char *, int, int, const struct afswtch *afp));
161} cmds[] = {
162	{ "up",		IFF_UP,		setifflags } ,
163	{ "down",	-IFF_UP,	setifflags },
164	{ "arp",	-IFF_NOARP,	setifflags },
165	{ "-arp",	IFF_NOARP,	setifflags },
166	{ "debug",	IFF_DEBUG,	setifflags },
167	{ "-debug",	-IFF_DEBUG,	setifflags },
168	{ "alias",	IFF_UP,		notealias },
169	{ "-alias",	-IFF_UP,	notealias },
170	{ "delete",	-IFF_UP,	notealias },
171#ifdef notdef
172#define	EN_SWABIPS	0x1000
173	{ "swabips",	EN_SWABIPS,	setifflags },
174	{ "-swabips",	-EN_SWABIPS,	setifflags },
175#endif
176	{ "netmask",	NEXTARG,	setifnetmask },
177#ifdef INET6
178	{ "prefixlen",	NEXTARG,	setifprefixlen },
179	{ "anycast",	IN6_IFF_ANYCAST, setip6flags },
180	{ "tentative",	IN6_IFF_TENTATIVE, setip6flags },
181	{ "-tentative",	-IN6_IFF_TENTATIVE, setip6flags },
182#endif
183	{ "range",	NEXTARG,	setatrange },
184	{ "phase",	NEXTARG,	setatphase },
185	{ "metric",	NEXTARG,	setifmetric },
186	{ "broadcast",	NEXTARG,	setifbroadaddr },
187	{ "ipdst",	NEXTARG,	setifipdst },
188	{ "link0",	IFF_LINK0,	setifflags },
189	{ "-link0",	-IFF_LINK0,	setifflags },
190	{ "link1",	IFF_LINK1,	setifflags },
191	{ "-link1",	-IFF_LINK1,	setifflags },
192	{ "link2",	IFF_LINK2,	setifflags },
193	{ "-link2",	-IFF_LINK2,	setifflags },
194#ifdef USE_IF_MEDIA
195	{ "media",	NEXTARG,	setmedia },
196	{ "mediaopt",	NEXTARG,	setmediaopt },
197	{ "-mediaopt",	NEXTARG,	unsetmediaopt },
198#endif
199#ifdef USE_VLANS
200	{ "vlan",	NEXTARG,	setvlantag },
201	{ "vlandev",	NEXTARG,	setvlandev },
202	{ "-vlandev",	NEXTARG,	unsetvlandev },
203#endif
204	{ "normal",	-IFF_LINK0,	setifflags },
205	{ "compress",	IFF_LINK0,	setifflags },
206	{ "noicmp",	IFF_LINK1,	setifflags },
207	{ "mtu",	NEXTARG,	setifmtu },
208	{ 0,		0,		setifaddr },
209	{ 0,		0,		setifdstaddr },
210};
211
212/*
213 * XNS support liberally adapted from code written at the University of
214 * Maryland principally by James O'Toole and Chris Torek.
215 */
216typedef	void af_status __P((int, struct rt_addrinfo *));
217typedef	void af_getaddr __P((const char *, int));
218typedef void af_getprefix __P((const char *, int));
219
220af_status	in_status, ipx_status, at_status, ether_status;
221af_getaddr	in_getaddr, ipx_getaddr, at_getaddr;
222
223#ifdef INET6
224af_status	in6_status;
225af_getaddr	in6_getaddr;
226af_getprefix	in6_getprefix;
227#endif /*INET6*/
228#ifdef NS
229af_status	xns_status;
230af_getaddr	xns_getaddr;
231#endif
232
233/* Known address families */
234const
235struct	afswtch {
236	const char *af_name;
237	short af_af;
238	af_status *af_status;
239	af_getaddr *af_getaddr;
240	af_getprefix *af_getprefix;
241	u_long af_difaddr;
242	u_long af_aifaddr;
243	caddr_t af_ridreq;
244	caddr_t af_addreq;
245} afs[] = {
246#define C(x) ((caddr_t) &x)
247	{ "inet", AF_INET, in_status, in_getaddr, NULL,
248	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
249#ifdef INET6
250	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
251	     SIOCDIFADDR_IN6, SIOCAIFADDR_IN6,
252	     C(in6_ridreq), C(in6_addreq) },
253#endif /*INET6*/
254	{ "ipx", AF_IPX, ipx_status, ipx_getaddr, NULL,
255	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
256	{ "atalk", AF_APPLETALK, at_status, at_getaddr, NULL,
257	     SIOCDIFADDR, SIOCAIFADDR, C(addreq), C(addreq) },
258#ifdef NS
259	{ "ns", AF_NS, xns_status, xns_getaddr, NULL,
260	     SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
261#endif
262	{ "ether", AF_INET, ether_status, NULL, NULL },	/* XXX not real!! */
263#if 0	/* XXX conflicts with the media command */
264#ifdef USE_IF_MEDIA
265	{ "media", AF_INET, media_status, NULL, NULL, }, /* XXX not real!! */
266#endif
267#ifdef USE_VLANS
268	{ "vlan", AF_INET, media_status, NULL, NULL, },	/* XXX not real!! */
269#endif
270#endif
271	{ 0,	0,	    0,		0 }
272};
273
274/*
275 * Expand the compacted form of addresses as returned via the
276 * configuration read via sysctl().
277 */
278
279#define ROUNDUP(a) \
280	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
281#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
282
283void
284rt_xaddrs(cp, cplim, rtinfo)
285	caddr_t cp, cplim;
286	struct rt_addrinfo *rtinfo;
287{
288	struct sockaddr *sa;
289	int i;
290
291	memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
292	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
293		if ((rtinfo->rti_addrs & (1 << i)) == 0)
294			continue;
295		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
296		ADVANCE(cp, sa);
297	}
298}
299
300
301void
302usage()
303{
304#ifndef INET6
305	fprintf(stderr, "%s\n%s\n%s\n%s\n",
306	"usage: ifconfig interface address_family [address [dest_address]]",
307	"                [parameters]",
308	"       ifconfig -a [-d] [-u] [address_family]",
309	"       ifconfig -l [-d] [-u] [address_family]");
310#else
311	fprintf(stderr, "%s\n%s\n%s\n%s\n",
312	"usage: ifconfig [-L] interface address_family [address [dest_address]]",
313	"                [parameters]",
314	"       ifconfig -a [-L] [-d] [-u] [address_family]",
315	"       ifconfig -l [-d] [-u] [address_family]");
316#endif
317	exit(1);
318}
319
320int
321main(argc, argv)
322	int argc;
323	char *const *argv;
324{
325	int c;
326	int all, namesonly, downonly, uponly;
327	int foundit = 0, need_nl = 0;
328	const struct afswtch *afp = 0;
329	int addrcount;
330	struct	if_msghdr *ifm, *nextifm;
331	struct	ifa_msghdr *ifam;
332	struct	sockaddr_dl *sdl;
333	char	*buf, *lim, *next;
334
335
336	size_t needed;
337	int mib[6];
338
339	/* Parse leading line options */
340	all = downonly = uponly = namesonly = 0;
341	while ((c = getopt(argc, argv, "adlmu"
342#ifdef INET6
343					"L"
344#endif
345			)) != -1) {
346		switch (c) {
347		case 'a':	/* scan all interfaces */
348			all++;
349			break;
350#ifdef INET6
351		case 'L':
352			ip6lifetime++;	/* print IPv6 address lifetime */
353			break;
354#endif
355		case 'l':	/* scan interface names only */
356			namesonly++;
357			break;
358		case 'd':	/* restrict scan to "down" interfaces */
359			downonly++;
360			break;
361		case 'u':	/* restrict scan to "up" interfaces */
362			uponly++;
363			break;
364		case 'm':	/* show media choices in status */
365			/* ignored for compatibility */
366			break;
367		default:
368			usage();
369			break;
370		}
371	}
372	argc -= optind;
373	argv += optind;
374
375	/* -l cannot be used with -a or -m */
376	if (namesonly && all)
377		usage();
378
379	/* nonsense.. */
380	if (uponly && downonly)
381		usage();
382
383	/* -a and -l allow an address family arg to limit the output */
384	if (all || namesonly) {
385		if (argc > 1)
386			usage();
387
388		if (argc == 1) {
389			for (afp = afs; afp->af_name; afp++)
390				if (strcmp(afp->af_name, *argv) == 0) {
391					argc--, argv++;
392					break;
393				}
394			if (afp->af_name == NULL)
395				usage();
396			/* leave with afp non-zero */
397		}
398	} else {
399		/* not listing, need an argument */
400		if (argc < 1)
401			usage();
402
403		strncpy(name, *argv, sizeof(name));
404		argc--, argv++;
405
406		/* check and maybe load support for this interface */
407		ifmaybeload(name);
408	}
409
410	/* Check for address family */
411	if (argc > 0) {
412		for (afp = afs; afp->af_name; afp++)
413			if (strcmp(afp->af_name, *argv) == 0) {
414				argc--, argv++;
415				break;
416			}
417		if (afp->af_name == NULL)
418			afp = NULL;	/* not a family, NULL */
419	}
420
421	mib[0] = CTL_NET;
422	mib[1] = PF_ROUTE;
423	mib[2] = 0;
424	mib[3] = 0;	/* address family */
425	mib[4] = NET_RT_IFLIST;
426	mib[5] = 0;
427
428	/* if particular family specified, only ask about it */
429	if (afp)
430		mib[3] = afp->af_af;
431
432	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
433		errx(1, "iflist-sysctl-estimate");
434	if ((buf = malloc(needed)) == NULL)
435		errx(1, "malloc");
436	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
437		errx(1, "actual retrieval of interface table");
438	lim = buf + needed;
439
440	next = buf;
441	while (next < lim) {
442
443		ifm = (struct if_msghdr *)next;
444
445		if (ifm->ifm_type == RTM_IFINFO) {
446			sdl = (struct sockaddr_dl *)(ifm + 1);
447			flags = ifm->ifm_flags;
448		} else {
449			fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
450			fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
451				ifm->ifm_type);
452			fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
453			fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
454				lim);
455			exit (1);
456		}
457
458		next += ifm->ifm_msglen;
459		ifam = NULL;
460		addrcount = 0;
461		while (next < lim) {
462
463			nextifm = (struct if_msghdr *)next;
464
465			if (nextifm->ifm_type != RTM_NEWADDR)
466				break;
467
468			if (ifam == NULL)
469				ifam = (struct ifa_msghdr *)nextifm;
470
471			addrcount++;
472			next += nextifm->ifm_msglen;
473		}
474
475		if (all || namesonly) {
476			if (uponly)
477				if ((flags & IFF_UP) == 0)
478					continue; /* not up */
479			if (downonly)
480				if (flags & IFF_UP)
481					continue; /* not down */
482			strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
483			name[sdl->sdl_nlen] = '\0';
484			if (namesonly) {
485				if (afp == NULL ||
486					afp->af_status != ether_status ||
487					sdl->sdl_type == IFT_ETHER) {
488					if (need_nl)
489						putchar(' ');
490					fputs(name, stdout);
491					need_nl++;
492				}
493				continue;
494			}
495		} else {
496			if (strlen(name) != sdl->sdl_nlen)
497				continue; /* not same len */
498			if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
499				continue; /* not same name */
500		}
501
502		if (argc > 0)
503			ifconfig(argc, argv, afp);
504		else
505			status(afp, addrcount, sdl, ifm, ifam);
506
507		if (all == 0 && namesonly == 0) {
508			foundit++; /* flag it as 'done' */
509			break;
510		}
511	}
512	free(buf);
513
514	if (namesonly && need_nl > 0)
515		putchar('\n');
516
517	if (all == 0 && namesonly == 0 && foundit == 0)
518		errx(1, "interface %s does not exist", name);
519
520
521	exit (0);
522}
523
524
525int
526ifconfig(argc, argv, afp)
527	int argc;
528	char *const *argv;
529	const struct afswtch *afp;
530{
531	int s;
532
533	if (afp == NULL)
534		afp = &afs[0];
535	ifr.ifr_addr.sa_family = afp->af_af;
536	strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
537
538	if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
539		err(1, "socket");
540
541	while (argc > 0) {
542		register const struct cmd *p;
543
544		for (p = cmds; p->c_name; p++)
545			if (strcmp(*argv, p->c_name) == 0)
546				break;
547		if (p->c_name == 0 && setaddr)
548			p++;	/* got src, do dst */
549		if (p->c_func) {
550			if (p->c_parameter == NEXTARG) {
551				if (argv[1] == NULL)
552					errx(1, "'%s' requires argument",
553					    p->c_name);
554				(*p->c_func)(argv[1], 0, s, afp);
555				argc--, argv++;
556			} else
557				(*p->c_func)(*argv, p->c_parameter, s, afp);
558		}
559		argc--, argv++;
560	}
561#ifdef INET6
562	if (ifr.ifr_addr.sa_family == AF_INET6 && explicit_prefix == 0) {
563		/* Aggregatable address architecture defines all prefixes
564		   are 64. So, it is convenient to set prefixlen to 64 if
565		   it is not specified. */
566		setifprefixlen("64", 0, s, afp);
567		/* in6_getprefix("64", MASK) if MASK is available here... */
568	}
569#endif
570	if (setipdst && ifr.ifr_addr.sa_family == AF_IPX) {
571		struct ipxip_req rq;
572		int size = sizeof(rq);
573
574		rq.rq_ipx = addreq.ifra_addr;
575		rq.rq_ip = addreq.ifra_dstaddr;
576
577		if (setsockopt(s, 0, SO_IPXIP_ROUTE, &rq, size) < 0)
578			Perror("Encapsulation Routing");
579	}
580	if (ifr.ifr_addr.sa_family == AF_APPLETALK)
581		checkatrange((struct sockaddr_at *) &addreq.ifra_addr);
582#ifdef NS
583	if (setipdst && ifr.ifr_addr.sa_family == AF_NS) {
584		struct nsip_req rq;
585		int size = sizeof(rq);
586
587		rq.rq_ns = addreq.ifra_addr;
588		rq.rq_ip = addreq.ifra_dstaddr;
589
590		if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
591			Perror("Encapsulation Routing");
592	}
593#endif
594	if (clearaddr) {
595		if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
596			warnx("interface %s cannot change %s addresses!",
597			      name, afp->af_name);
598			clearaddr = NULL;
599		}
600	}
601	if (clearaddr) {
602		int ret;
603		strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
604		if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
605			if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
606				/* means no previous address for interface */
607			} else
608				Perror("ioctl (SIOCDIFADDR)");
609		}
610	}
611	if (newaddr) {
612		if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
613			warnx("interface %s cannot change %s addresses!",
614			      name, afp->af_name);
615			newaddr = 0;
616		}
617	}
618	if (newaddr) {
619		strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
620		if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
621			Perror("ioctl (SIOCAIFADDR)");
622	}
623	close(s);
624	return(0);
625}
626#define RIDADDR 0
627#define ADDR	1
628#define MASK	2
629#define DSTADDR	3
630
631/*ARGSUSED*/
632void
633setifaddr(addr, param, s, afp)
634	const char *addr;
635	int param;
636	int s;
637	const struct afswtch *afp;
638{
639	if (*afp->af_getaddr == NULL)
640		return;
641	/*
642	 * Delay the ioctl to set the interface addr until flags are all set.
643	 * The address interpretation may depend on the flags,
644	 * and the flags may change when the address is set.
645	 */
646	setaddr++;
647	if (doalias == 0)
648		clearaddr = 1;
649	(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
650}
651
652void
653setifnetmask(addr, dummy, s, afp)
654	const char *addr;
655	int dummy __unused;
656	int s;
657	const struct afswtch *afp;
658{
659	if (*afp->af_getaddr == NULL)
660		return;
661	(*afp->af_getaddr)(addr, MASK);
662}
663
664#ifdef INET6
665void
666setifprefixlen(addr, dummy, s, afp)
667        const char *addr;
668	int dummy __unused;
669	int s;
670	const struct afswtch *afp;
671{
672        if (*afp->af_getprefix)
673                (*afp->af_getprefix)(addr, MASK);
674	explicit_prefix = 1;
675}
676
677void
678setip6flags(dummyaddr, flag, dummysoc, afp)
679	const char *dummyaddr __unused;
680	int flag;
681	int dummysoc __unused;
682	const struct afswtch *afp;
683{
684	if (afp->af_af != AF_INET6)
685		err(1, "address flags can be set only for inet6 addresses");
686
687	if (flag < 0)
688		in6_addreq.ifra_flags &= ~(-flag);
689	else
690		in6_addreq.ifra_flags |= flag;
691}
692#endif
693
694void
695setifbroadaddr(addr, dummy, s, afp)
696	const char *addr;
697	int dummy __unused;
698	int s;
699	const struct afswtch *afp;
700{
701	if (*afp->af_getaddr == NULL)
702		return;
703	(*afp->af_getaddr)(addr, DSTADDR);
704}
705
706void
707setifipdst(addr, dummy, s, afp)
708	const char *addr;
709	int dummy __unused;
710	int s;
711	const struct afswtch *afp;
712{
713	in_getaddr(addr, DSTADDR);
714	setipdst++;
715	clearaddr = 0;
716	newaddr = 0;
717}
718#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
719
720void
721notealias(addr, param, s, afp)
722	const char *addr;
723	int param;
724	int s;
725	const struct afswtch *afp;
726{
727	if (setaddr && doalias == 0 && param < 0)
728		bcopy((caddr_t)rqtosa(af_addreq),
729		      (caddr_t)rqtosa(af_ridreq),
730		      rqtosa(af_addreq)->sa_len);
731	doalias = param;
732	if (param < 0) {
733		clearaddr = 1;
734		newaddr = 0;
735	} else
736		clearaddr = 0;
737}
738
739/*ARGSUSED*/
740void
741setifdstaddr(addr, param, s, afp)
742	const char *addr;
743	int param __unused;
744	int s;
745	const struct afswtch *afp;
746{
747	if (*afp->af_getaddr == NULL)
748		return;
749	(*afp->af_getaddr)(addr, DSTADDR);
750}
751
752/*
753 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
754 * of the ifreq structure, which may confuse other parts of ifconfig.
755 * Make a private copy so we can avoid that.
756 */
757void
758setifflags(vname, value, s, afp)
759	const char *vname;
760	int value;
761	int s;
762	const struct afswtch *afp;
763{
764	struct ifreq		my_ifr;
765
766	bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
767
768 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
769 		Perror("ioctl (SIOCGIFFLAGS)");
770 		exit(1);
771 	}
772	strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
773 	flags = my_ifr.ifr_flags;
774
775	if (value < 0) {
776		value = -value;
777		flags &= ~value;
778	} else
779		flags |= value;
780	my_ifr.ifr_flags = flags;
781	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
782		Perror(vname);
783}
784
785void
786setifmetric(val, dummy, s, afp)
787	const char *val;
788	int dummy __unused;
789	int s;
790	const struct afswtch *afp;
791{
792	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
793	ifr.ifr_metric = atoi(val);
794	if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
795		warn("ioctl (set metric)");
796}
797
798void
799setifmtu(val, dummy, s, afp)
800	const char *val;
801	int dummy __unused;
802	int s;
803	const struct afswtch *afp;
804{
805	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
806	ifr.ifr_mtu = atoi(val);
807	if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
808		warn("ioctl (set mtu)");
809}
810
811
812#define	IFFBITS \
813"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
814"\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
815"\20MULTICAST"
816
817/*
818 * Print the status of the interface.  If an address family was
819 * specified, show it and it only; otherwise, show them all.
820 */
821void
822status(afp, addrcount, sdl, ifm, ifam)
823	const struct afswtch *afp;
824	int addrcount;
825	struct	sockaddr_dl *sdl;
826	struct if_msghdr *ifm;
827	struct ifa_msghdr *ifam;
828{
829	const struct afswtch *p = NULL;
830	struct	rt_addrinfo info;
831	int allfamilies, s;
832	struct ifstat ifs;
833
834	if (afp == NULL) {
835		allfamilies = 1;
836		afp = &afs[0];
837	} else
838		allfamilies = 0;
839
840	ifr.ifr_addr.sa_family = afp->af_af;
841	strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
842
843	if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
844		err(1, "socket");
845
846	/*
847	 * XXX is it we are doing a SIOCGIFMETRIC etc for one family.
848	 * is it possible that the metric and mtu can be different for
849	 * each family?  If so, we have a format problem, because the
850	 * metric and mtu is printed on the global the flags line.
851	 */
852	if (ioctl(s, SIOCGIFMETRIC, (caddr_t)&ifr) < 0)
853		warn("ioctl (SIOCGIFMETRIC)");
854	else
855		metric = ifr.ifr_metric;
856
857	if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0)
858		warn("ioctl (SIOCGIFMTU)");
859	else
860		mtu = ifr.ifr_mtu;
861
862	printf("%s: ", name);
863	printb("flags", flags, IFFBITS);
864	if (metric)
865		printf(" metric %d", metric);
866	if (mtu)
867		printf(" mtu %d", mtu);
868	putchar('\n');
869
870	while (addrcount > 0) {
871
872		info.rti_addrs = ifam->ifam_addrs;
873
874		/* Expand the compacted addresses */
875		rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
876			  &info);
877
878		if (!allfamilies) {
879			if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family &&
880#ifdef USE_IF_MEDIA
881			    afp->af_status != media_status &&
882#endif
883#ifdef USE_VLANS
884			    afp->af_status != vlan_status &&
885#endif
886			    afp->af_status != ether_status) {
887				p = afp;
888				(*p->af_status)(s, &info);
889			}
890		} else for (p = afs; p->af_name; p++) {
891			if (p->af_af == info.rti_info[RTAX_IFA]->sa_family &&
892#ifdef USE_IF_MEDIA
893			    p->af_status != media_status &&
894#endif
895#ifdef USE_VLANS
896			    p->af_status != vlan_status &&
897#endif
898			    p->af_status != ether_status)
899				(*p->af_status)(s, &info);
900		}
901		addrcount--;
902		ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
903	}
904	if (allfamilies || afp->af_status == ether_status)
905		ether_status(s, (struct rt_addrinfo *)sdl);
906#ifdef USE_IF_MEDIA
907	if (allfamilies || afp->af_status == media_status)
908		media_status(s, NULL);
909#endif
910#ifdef USE_VLANS
911	if (allfamilies || afp->af_status == vlan_status)
912		vlan_status(s, NULL);
913#endif
914	strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
915	if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
916		printf("%s", ifs.ascii);
917
918	if (!allfamilies && !p && afp->af_status != media_status &&
919	    afp->af_status != ether_status && afp->af_status != vlan_status)
920		warnx("%s has no %s interface address!", name, afp->af_name);
921
922	close(s);
923	return;
924}
925
926void
927in_status(s, info)
928	int s __unused;
929	struct rt_addrinfo * info;
930{
931	struct sockaddr_in *sin, null_sin;
932
933	memset(&null_sin, 0, sizeof(null_sin));
934
935	sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
936	printf("\tinet %s ", inet_ntoa(sin->sin_addr));
937
938	if (flags & IFF_POINTOPOINT) {
939		/* note RTAX_BRD overlap with IFF_BROADCAST */
940		sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
941		if (!sin)
942			sin = &null_sin;
943		printf("--> %s ", inet_ntoa(sin->sin_addr));
944	}
945
946	sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
947	if (!sin)
948		sin = &null_sin;
949	printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
950
951	if (flags & IFF_BROADCAST) {
952		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
953		sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
954		if (sin && sin->sin_addr.s_addr != 0)
955			printf("broadcast %s", inet_ntoa(sin->sin_addr));
956	}
957	putchar('\n');
958}
959
960#ifdef INET6
961void
962in6_status(s, info)
963	int s __unused;
964	struct rt_addrinfo * info;
965{
966	struct sockaddr_in6 *sin, null_sin;
967	struct in6_ifreq ifr6;
968	int s6;
969	u_int32_t flags6;
970	struct in6_addrlifetime lifetime;
971	time_t t = time(NULL);
972
973	memset(&null_sin, 0, sizeof(null_sin));
974
975	sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA];
976	strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
977	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
978		perror("ifconfig: socket");
979		return;
980	}
981	ifr6.ifr_addr = *sin;
982	if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
983		perror("ifconfig: ioctl(SIOCGIFAFLAG_IN6)");
984		close(s6);
985		return;
986	}
987	flags6 = ifr6.ifr_ifru.ifru_flags6;
988	memset(&lifetime, 0, sizeof(lifetime));
989	ifr6.ifr_addr = *sin;
990	if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
991		perror("ifconfig: ioctl(SIOCGIFALIFETIME_IN6)");
992		close(s6);
993		return;
994	}
995	lifetime = ifr6.ifr_ifru.ifru_lifetime;
996	close(s6);
997
998	printf("\tinet6 %s ", inet_ntop(AF_INET6, &sin->sin6_addr,
999				ntop_buf, sizeof(ntop_buf)));
1000
1001
1002	if (flags & IFF_POINTOPOINT) {
1003		/* note RTAX_BRD overlap with IFF_BROADCAST */
1004		sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD];
1005		/*
1006		 * some of the interfaces do not have valid destination
1007		 * address.
1008		 */
1009		if (sin && sin->sin6_family == AF_INET6) {
1010			printf("--> %s ", inet_ntop(AF_INET6, &sin->sin6_addr,
1011						ntop_buf, sizeof(ntop_buf)));
1012		}
1013	}
1014
1015	sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1016	if (!sin)
1017		sin = &null_sin;
1018	printf("prefixlen %d ", prefix(&sin->sin6_addr,
1019		sizeof(struct in6_addr)));
1020
1021	if (flags6 & IN6_IFF_ANYCAST)
1022		printf("anycast ");
1023	if (flags6 & IN6_IFF_TENTATIVE)
1024		printf("tentative ");
1025	if (flags6 & IN6_IFF_DUPLICATED)
1026		printf("duplicated ");
1027	if (flags6 & IN6_IFF_DETACHED)
1028		printf("detached ");
1029	if (flags6 & IN6_IFF_DEPRECATED)
1030		printf("deprecated ");
1031
1032
1033	if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
1034		printf("pltime ");
1035		if (lifetime.ia6t_preferred) {
1036			printf("%s ", lifetime.ia6t_preferred < t
1037				? "0" : sec2str(lifetime.ia6t_preferred - t));
1038		} else
1039			printf("infty ");
1040
1041		printf("vltime ");
1042		if (lifetime.ia6t_expire) {
1043			printf("%s ", lifetime.ia6t_expire < t
1044				? "0" : sec2str(lifetime.ia6t_expire - t));
1045		} else
1046			printf("infty ");
1047	}
1048
1049	putchar('\n');
1050}
1051#endif /*INET6*/
1052
1053void
1054ipx_status(s, info)
1055	int s __unused;
1056	struct rt_addrinfo * info;
1057{
1058	struct sockaddr_ipx *sipx, null_sipx;
1059
1060	memset(&null_sipx, 0, sizeof(null_sipx));
1061
1062	sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_IFA];
1063	printf("\tipx %s ", ipx_ntoa(sipx->sipx_addr));
1064
1065	if (flags & IFF_POINTOPOINT) {
1066		sipx = (struct sockaddr_ipx *)info->rti_info[RTAX_BRD];
1067		if (!sipx)
1068			sipx = &null_sipx;
1069		printf("--> %s ", ipx_ntoa(sipx->sipx_addr));
1070	}
1071	putchar('\n');
1072}
1073
1074void
1075at_status(s, info)
1076	int s __unused;
1077	struct rt_addrinfo * info;
1078{
1079	struct sockaddr_at *sat, null_sat;
1080	struct netrange *nr;
1081
1082	memset(&null_sat, 0, sizeof(null_sat));
1083
1084	sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
1085	nr = &sat->sat_range.r_netrange;
1086	printf("\tatalk %d.%d range %d-%d phase %d",
1087		ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1088		ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
1089	if (flags & IFF_POINTOPOINT) {
1090		/* note RTAX_BRD overlap with IFF_BROADCAST */
1091		sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1092		if (!sat)
1093			sat = &null_sat;
1094		printf("--> %d.%d",
1095			ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
1096	}
1097	if (flags & IFF_BROADCAST) {
1098		/* note RTAX_BRD overlap with IFF_POINTOPOINT */
1099		sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
1100		if (sat)
1101			printf(" broadcast %d.%d",
1102				ntohs(sat->sat_addr.s_net),
1103				sat->sat_addr.s_node);
1104	}
1105
1106	putchar('\n');
1107}
1108
1109#ifdef NS
1110void
1111xns_status(s, info)
1112	int s __unused;
1113	struct rt_addrinfo * info;
1114{
1115	struct sockaddr_ns *sns, null_sns;
1116
1117	memset(&null_sns, 0, sizeof(null_sns));
1118
1119	sns = (struct sockaddr_ns *)info->rti_info[RTAX_IFA];
1120	printf("\tns %s ", ns_ntoa(sns->sns_addr));
1121
1122	if (flags & IFF_POINTOPOINT) {
1123		sns = (struct sockaddr_ns *)info->rti_info[RTAX_BRD];
1124		if (!sns)
1125			sns = &null_sns;
1126		printf("--> %s ", ns_ntoa(sns->sns_addr));
1127	}
1128
1129	putchar('\n');
1130	close(s);
1131}
1132#endif
1133
1134
1135void
1136ether_status(s, info)
1137	int s __unused;
1138	struct rt_addrinfo *info;
1139{
1140	char *cp;
1141	int n;
1142	struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
1143
1144	cp = (char *)LLADDR(sdl);
1145	if ((n = sdl->sdl_alen) > 0) {
1146		if (sdl->sdl_type == IFT_ETHER)
1147			printf ("\tether ");
1148		else
1149			printf ("\tlladdr ");
1150             	while (--n >= 0)
1151			printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' ');
1152		putchar('\n');
1153	}
1154}
1155
1156void
1157Perror(cmd)
1158	const char *cmd;
1159{
1160	switch (errno) {
1161
1162	case ENXIO:
1163		errx(1, "%s: no such interface", cmd);
1164		break;
1165
1166	case EPERM:
1167		errx(1, "%s: permission denied", cmd);
1168		break;
1169
1170	default:
1171		err(1, "%s", cmd);
1172	}
1173}
1174
1175#define SIN(x) ((struct sockaddr_in *) &(x))
1176struct sockaddr_in *sintab[] = {
1177SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
1178SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
1179
1180void
1181in_getaddr(s, which)
1182	const char *s;
1183	int which;
1184{
1185	register struct sockaddr_in *sin = sintab[which];
1186	struct hostent *hp;
1187	struct netent *np;
1188
1189	sin->sin_len = sizeof(*sin);
1190	if (which != MASK)
1191		sin->sin_family = AF_INET;
1192
1193	if (inet_aton(s, &sin->sin_addr))
1194		return;
1195	if ((hp = gethostbyname(s)) != 0)
1196		bcopy(hp->h_addr, (char *)&sin->sin_addr,
1197		    MIN(hp->h_length, sizeof(sin->sin_addr)));
1198	else if ((np = getnetbyname(s)) != 0)
1199		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
1200	else
1201		errx(1, "%s: bad value", s);
1202}
1203
1204#ifdef INET6
1205#define	SIN6(x) ((struct sockaddr_in6 *) &(x))
1206struct	sockaddr_in6 *sin6tab[] = {
1207SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
1208SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
1209
1210void
1211in6_getaddr(s, which)
1212	const char *s;
1213	int which;
1214{
1215	register struct sockaddr_in6 *sin = sin6tab[which];
1216
1217	newaddr &= 1;
1218
1219	sin->sin6_len = sizeof(*sin);
1220	if (which != MASK)
1221		sin->sin6_family = AF_INET6;
1222
1223        if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
1224		errx(1, "%s: bad value", s);
1225}
1226
1227void
1228in6_getprefix(plen, which)
1229	const char *plen;
1230	int which;
1231{
1232	register struct sockaddr_in6 *sin = sin6tab[which];
1233	register u_char *cp;
1234	int len = atoi(plen);
1235
1236	if ((len < 0) || (len > 128))
1237		errx(1, "%s: bad value", plen);
1238	sin->sin6_len = sizeof(*sin);
1239	if (which != MASK)
1240		sin->sin6_family = AF_INET6;
1241	if ((len == 0) || (len == 128)) {
1242		memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
1243		return;
1244	}
1245	memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
1246	for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
1247		*cp++ = 0xff;
1248	*cp = 0xff << (8 - len);
1249}
1250#endif
1251
1252/*
1253 * Print a value a la the %b format of the kernel's printf
1254 */
1255void
1256printb(s, v, bits)
1257	const char *s;
1258	register unsigned v;
1259	register const char *bits;
1260{
1261	register int i, any = 0;
1262	register char c;
1263
1264	if (bits && *bits == 8)
1265		printf("%s=%o", s, v);
1266	else
1267		printf("%s=%x", s, v);
1268	bits++;
1269	if (bits) {
1270		putchar('<');
1271		while ((i = *bits++) != '\0') {
1272			if (v & (1 << (i-1))) {
1273				if (any)
1274					putchar(',');
1275				any = 1;
1276				for (; (c = *bits) > 32; bits++)
1277					putchar(c);
1278			} else
1279				for (; *bits > 32; bits++)
1280					;
1281		}
1282		putchar('>');
1283	}
1284}
1285
1286#define SIPX(x) ((struct sockaddr_ipx *) &(x))
1287struct sockaddr_ipx *sipxtab[] = {
1288SIPX(ridreq.ifr_addr), SIPX(addreq.ifra_addr),
1289SIPX(addreq.ifra_mask), SIPX(addreq.ifra_broadaddr)};
1290
1291void
1292ipx_getaddr(addr, which)
1293	const char *addr;
1294	int which;
1295{
1296	struct sockaddr_ipx *sipx = sipxtab[which];
1297
1298	sipx->sipx_family = AF_IPX;
1299	sipx->sipx_len = sizeof(*sipx);
1300	sipx->sipx_addr = ipx_addr(addr);
1301	if (which == MASK)
1302		printf("Attempt to set IPX netmask will be ineffectual\n");
1303}
1304
1305void
1306at_getaddr(addr, which)
1307	const char *addr;
1308	int which;
1309{
1310	struct sockaddr_at *sat = (struct sockaddr_at *) &addreq.ifra_addr;
1311	u_int net, node;
1312
1313	sat->sat_family = AF_APPLETALK;
1314	sat->sat_len = sizeof(*sat);
1315	if (which == MASK)
1316		errx(1, "AppleTalk does not use netmasks");
1317	if (sscanf(addr, "%u.%u", &net, &node) != 2
1318	    || net > 0xffff || node > 0xfe)
1319		errx(1, "%s: illegal address", addr);
1320	sat->sat_addr.s_net = htons(net);
1321	sat->sat_addr.s_node = node;
1322}
1323
1324/* XXX  FIXME -- should use strtoul for better parsing. */
1325void
1326setatrange(range, dummy, s, afp)
1327	const char *range;
1328	int dummy __unused;
1329	int s;
1330	const struct afswtch *afp;
1331{
1332	u_short	first = 123, last = 123;
1333
1334	if (sscanf(range, "%hu-%hu", &first, &last) != 2
1335	    || first == 0 || first > 0xffff
1336	    || last == 0 || last > 0xffff || first > last)
1337		errx(1, "%s: illegal net range: %u-%u", range, first, last);
1338	at_nr.nr_firstnet = htons(first);
1339	at_nr.nr_lastnet = htons(last);
1340}
1341
1342void
1343setatphase(phase, dummy, s, afp)
1344	const char *phase;
1345	int dummy __unused;
1346	int s;
1347	const struct afswtch *afp;
1348{
1349	if (!strcmp(phase, "1"))
1350		at_nr.nr_phase = 1;
1351	else if (!strcmp(phase, "2"))
1352		at_nr.nr_phase = 2;
1353	else
1354		errx(1, "%s: illegal phase", phase);
1355}
1356
1357void
1358checkatrange(struct sockaddr_at *sat)
1359{
1360	if (at_nr.nr_phase == 0)
1361		at_nr.nr_phase = 2;	/* Default phase 2 */
1362	if (at_nr.nr_firstnet == 0)
1363		at_nr.nr_firstnet =	/* Default range of one */
1364		at_nr.nr_lastnet = sat->sat_addr.s_net;
1365printf("\tatalk %d.%d range %d-%d phase %d\n",
1366	ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
1367	ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
1368	if ((u_short) ntohs(at_nr.nr_firstnet) >
1369			(u_short) ntohs(sat->sat_addr.s_net)
1370		    || (u_short) ntohs(at_nr.nr_lastnet) <
1371			(u_short) ntohs(sat->sat_addr.s_net))
1372		errx(1, "AppleTalk address is not in range");
1373	sat->sat_range.r_netrange = at_nr;
1374}
1375
1376#ifdef NS
1377#define SNS(x) ((struct sockaddr_ns *) &(x))
1378struct sockaddr_ns *snstab[] = {
1379SNS(ridreq.ifr_addr), SNS(addreq.ifra_addr),
1380SNS(addreq.ifra_mask), SNS(addreq.ifra_broadaddr)};
1381
1382void
1383xns_getaddr(addr, which)
1384	const char *addr;
1385	int which;
1386{
1387	struct sockaddr_ns *sns = snstab[which];
1388
1389	sns->sns_family = AF_NS;
1390	sns->sns_len = sizeof(*sns);
1391	sns->sns_addr = ns_addr(addr);
1392	if (which == MASK)
1393		printf("Attempt to set XNS netmask will be ineffectual\n");
1394}
1395#endif
1396
1397#ifdef INET6
1398int
1399prefix(val, size)
1400        void *val;
1401        int size;
1402{
1403        register u_char *name = (u_char *)val;
1404        register int byte, bit, plen = 0;
1405
1406        for (byte = 0; byte < size; byte++, plen += 8)
1407                if (name[byte] != 0xff)
1408                        break;
1409	if (byte == size)
1410		return (plen);
1411	for (bit = 7; bit != 0; bit--, plen++)
1412                if (!(name[byte] & (1 << bit)))
1413                        break;
1414        for (; bit != 0; bit--)
1415                if (name[byte] & (1 << bit))
1416                        return(0);
1417        byte++;
1418        for (; byte < size; byte++)
1419                if (name[byte])
1420                        return(0);
1421        return (plen);
1422}
1423
1424static char *
1425sec2str(total)
1426	time_t total;
1427{
1428	static char result[256];
1429	int days, hours, mins, secs;
1430	int first = 1;
1431	char *p = result;
1432
1433	if (0) {
1434		days = total / 3600 / 24;
1435		hours = (total / 3600) % 24;
1436		mins = (total / 60) % 60;
1437		secs = total % 60;
1438
1439		if (days) {
1440			first = 0;
1441			p += sprintf(p, "%dd", days);
1442		}
1443		if (!first || hours) {
1444			first = 0;
1445			p += sprintf(p, "%dh", hours);
1446		}
1447		if (!first || mins) {
1448			first = 0;
1449			p += sprintf(p, "%dm", mins);
1450		}
1451		sprintf(p, "%ds", secs);
1452	} else
1453		sprintf(result, "%lu", (unsigned long)total);
1454
1455	return(result);
1456}
1457#endif /*INET6*/
1458
1459void
1460ifmaybeload(name)
1461	char *name;
1462{
1463	struct module_stat mstat;
1464	int fileid, modid;
1465	char ifkind[35], *cp, *dp;
1466
1467
1468	/* turn interface and unit into module name */
1469	strcpy(ifkind, "if_");
1470	for (cp = name, dp = ifkind + 3; (*cp != 0) && !isdigit(*cp); cp++, dp++)
1471		*dp = *cp;
1472	*dp = 0;
1473
1474	/* scan files in kernel */
1475	mstat.version = sizeof(struct module_stat);
1476	for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1477		/* scan modules in file */
1478		for (modid = kldfirstmod(fileid); modid > 0;
1479		     modid = modfnext(modid)) {
1480			if (modstat(modid, &mstat) < 0)
1481				continue;
1482			/* strip bus name if present */
1483			if ((cp = strchr(mstat.name, '/')) != NULL) {
1484				cp++;
1485			} else {
1486				cp = mstat.name;
1487			}
1488			/* already loaded? */
1489			if (!strcmp(ifkind, cp))
1490				return;
1491		}
1492	}
1493
1494	/* not present, we should try to load it */
1495	kldload(ifkind);
1496}
1497