ifconfig.c revision 172438
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31static const char copyright[] =
32"@(#) Copyright (c) 1983, 1993\n\
33	The Regents of the University of California.  All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)ifconfig.c	8.2 (Berkeley) 2/16/94";
39#endif
40static const char rcsid[] =
41  "$FreeBSD: head/sbin/ifconfig/ifconfig.c 172438 2007-10-04 09:45:41Z thompsa $";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/ioctl.h>
46#include <sys/socket.h>
47#include <sys/sysctl.h>
48#include <sys/time.h>
49#include <sys/module.h>
50#include <sys/linker.h>
51
52#include <net/ethernet.h>
53#include <net/if.h>
54#include <net/if_var.h>
55#include <net/if_dl.h>
56#include <net/if_types.h>
57#include <net/route.h>
58
59/* IP */
60#include <netinet/in.h>
61#include <netinet/in_var.h>
62#include <arpa/inet.h>
63#include <netdb.h>
64
65#include <ifaddrs.h>
66#include <ctype.h>
67#include <err.h>
68#include <errno.h>
69#include <fcntl.h>
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73#include <unistd.h>
74
75#include "ifconfig.h"
76
77/*
78 * Since "struct ifreq" is composed of various union members, callers
79 * should pay special attention to interprete the value.
80 * (.e.g. little/big endian difference in the structure.)
81 */
82struct	ifreq ifr;
83
84char	name[IFNAMSIZ];
85int	setaddr;
86int	setmask;
87int	doalias;
88int	clearaddr;
89int	newaddr = 1;
90int	verbose;
91int	noload;
92
93int	supmedia = 0;
94int	printkeys = 0;		/* Print keying material for interfaces. */
95
96static	int ifconfig(int argc, char *const *argv, const struct afswtch *afp);
97static	void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
98		struct ifaddrs *ifa);
99static	void tunnel_status(int s);
100static	void usage(void);
101
102static struct afswtch *af_getbyname(const char *name);
103static struct afswtch *af_getbyfamily(int af);
104static void af_other_status(int);
105
106static struct option *opts = NULL;
107
108void
109opt_register(struct option *p)
110{
111	p->next = opts;
112	opts = p;
113}
114
115static void
116usage(void)
117{
118	char options[1024];
119	struct option *p;
120
121	/* XXX not right but close enough for now */
122	options[0] = '\0';
123	for (p = opts; p != NULL; p = p->next) {
124		strlcat(options, p->opt_usage, sizeof(options));
125		strlcat(options, " ", sizeof(options));
126	}
127
128	fprintf(stderr,
129	"usage: ifconfig %sinterface address_family [address [dest_address]]\n"
130	"                [parameters]\n"
131	"       ifconfig interface create\n"
132	"       ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
133	"       ifconfig -l [-d] [-u] [address_family]\n"
134	"       ifconfig %s[-d] [-m] [-u] [-v]\n",
135		options, options, options);
136	exit(1);
137}
138
139int
140main(int argc, char *argv[])
141{
142	int c, all, namesonly, downonly, uponly;
143	const struct afswtch *afp = NULL;
144	int ifindex;
145	struct ifaddrs *ifap, *ifa;
146	struct ifreq paifr;
147	const struct sockaddr_dl *sdl;
148	char options[1024], *cp;
149	const char *ifname;
150	struct option *p;
151	size_t iflen;
152
153	all = downonly = uponly = namesonly = noload = verbose = 0;
154
155	/* Parse leading line options */
156	strlcpy(options, "adklmnuv", sizeof(options));
157	for (p = opts; p != NULL; p = p->next)
158		strlcat(options, p->opt, sizeof(options));
159	while ((c = getopt(argc, argv, options)) != -1) {
160		switch (c) {
161		case 'a':	/* scan all interfaces */
162			all++;
163			break;
164		case 'd':	/* restrict scan to "down" interfaces */
165			downonly++;
166			break;
167		case 'k':
168			printkeys++;
169			break;
170		case 'l':	/* scan interface names only */
171			namesonly++;
172			break;
173		case 'm':	/* show media choices in status */
174			supmedia = 1;
175			break;
176		case 'n':	/* suppress module loading */
177			noload++;
178			break;
179		case 'u':	/* restrict scan to "up" interfaces */
180			uponly++;
181			break;
182		case 'v':
183			verbose++;
184			break;
185		default:
186			for (p = opts; p != NULL; p = p->next)
187				if (p->opt[0] == c) {
188					p->cb(optarg);
189					break;
190				}
191			if (p == NULL)
192				usage();
193			break;
194		}
195	}
196	argc -= optind;
197	argv += optind;
198
199	/* -l cannot be used with -a or -m */
200	if (namesonly && (all || supmedia))
201		usage();
202
203	/* nonsense.. */
204	if (uponly && downonly)
205		usage();
206
207	/* no arguments is equivalent to '-a' */
208	if (!namesonly && argc < 1)
209		all = 1;
210
211	/* -a and -l allow an address family arg to limit the output */
212	if (all || namesonly) {
213		if (argc > 1)
214			usage();
215
216		ifname = NULL;
217		ifindex = 0;
218		if (argc == 1) {
219			afp = af_getbyname(*argv);
220			if (afp == NULL)
221				usage();
222			if (afp->af_name != NULL)
223				argc--, argv++;
224			/* leave with afp non-zero */
225		}
226	} else {
227		/* not listing, need an argument */
228		if (argc < 1)
229			usage();
230
231		ifname = *argv;
232		argc--, argv++;
233
234		/* check and maybe load support for this interface */
235		ifmaybeload(ifname);
236
237		ifindex = if_nametoindex(ifname);
238		if (ifindex == 0) {
239			/*
240			 * NOTE:  We must special-case the `create' command
241			 * right here as we would otherwise fail when trying
242			 * to find the interface.
243			 */
244			if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
245			    strcmp(argv[0], "plumb") == 0)) {
246				iflen = strlcpy(name, ifname, sizeof(name));
247				if (iflen >= sizeof(name))
248					errx(1, "%s: cloning name too long",
249					    ifname);
250				ifconfig(argc, argv, NULL);
251				exit(0);
252			}
253			errx(1, "interface %s does not exist", ifname);
254		}
255	}
256
257	/* Check for address family */
258	if (argc > 0) {
259		afp = af_getbyname(*argv);
260		if (afp != NULL)
261			argc--, argv++;
262	}
263
264	if (getifaddrs(&ifap) != 0)
265		err(EXIT_FAILURE, "getifaddrs");
266	cp = NULL;
267	ifindex = 0;
268	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
269		memset(&paifr, 0, sizeof(paifr));
270		strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
271		if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
272			memcpy(&paifr.ifr_addr, ifa->ifa_addr,
273			    ifa->ifa_addr->sa_len);
274		}
275
276		if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
277			continue;
278		if (ifa->ifa_addr->sa_family == AF_LINK)
279			sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
280		else
281			sdl = NULL;
282		if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0)
283			continue;
284		iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
285		if (iflen >= sizeof(name)) {
286			warnx("%s: interface name too long, skipping",
287			    ifa->ifa_name);
288			continue;
289		}
290		cp = ifa->ifa_name;
291
292		if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
293			continue;
294		if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
295			continue;
296		ifindex++;
297		/*
298		 * Are we just listing the interfaces?
299		 */
300		if (namesonly) {
301			if (ifindex > 1)
302				printf(" ");
303			fputs(name, stdout);
304			continue;
305		}
306
307		if (argc > 0)
308			ifconfig(argc, argv, afp);
309		else
310			status(afp, sdl, ifa);
311	}
312	if (namesonly)
313		printf("\n");
314	freeifaddrs(ifap);
315
316	exit(0);
317}
318
319static struct afswtch *afs = NULL;
320
321void
322af_register(struct afswtch *p)
323{
324	p->af_next = afs;
325	afs = p;
326}
327
328static struct afswtch *
329af_getbyname(const char *name)
330{
331	struct afswtch *afp;
332
333	for (afp = afs; afp !=  NULL; afp = afp->af_next)
334		if (strcmp(afp->af_name, name) == 0)
335			return afp;
336	return NULL;
337}
338
339static struct afswtch *
340af_getbyfamily(int af)
341{
342	struct afswtch *afp;
343
344	for (afp = afs; afp != NULL; afp = afp->af_next)
345		if (afp->af_af == af)
346			return afp;
347	return NULL;
348}
349
350static void
351af_other_status(int s)
352{
353	struct afswtch *afp;
354	uint8_t afmask[howmany(AF_MAX, NBBY)];
355
356	memset(afmask, 0, sizeof(afmask));
357	for (afp = afs; afp != NULL; afp = afp->af_next) {
358		if (afp->af_other_status == NULL)
359			continue;
360		if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
361			continue;
362		afp->af_other_status(s);
363		setbit(afmask, afp->af_af);
364	}
365}
366
367static void
368af_all_tunnel_status(int s)
369{
370	struct afswtch *afp;
371	uint8_t afmask[howmany(AF_MAX, NBBY)];
372
373	memset(afmask, 0, sizeof(afmask));
374	for (afp = afs; afp != NULL; afp = afp->af_next) {
375		if (afp->af_status_tunnel == NULL)
376			continue;
377		if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
378			continue;
379		afp->af_status_tunnel(s);
380		setbit(afmask, afp->af_af);
381	}
382}
383
384static struct cmd *cmds = NULL;
385
386void
387cmd_register(struct cmd *p)
388{
389	p->c_next = cmds;
390	cmds = p;
391}
392
393static const struct cmd *
394cmd_lookup(const char *name)
395{
396#define	N(a)	(sizeof(a)/sizeof(a[0]))
397	const struct cmd *p;
398
399	for (p = cmds; p != NULL; p = p->c_next)
400		if (strcmp(name, p->c_name) == 0)
401			return p;
402	return NULL;
403#undef N
404}
405
406struct callback {
407	callback_func *cb_func;
408	void	*cb_arg;
409	struct callback *cb_next;
410};
411static struct callback *callbacks = NULL;
412
413void
414callback_register(callback_func *func, void *arg)
415{
416	struct callback *cb;
417
418	cb = malloc(sizeof(struct callback));
419	if (cb == NULL)
420		errx(1, "unable to allocate memory for callback");
421	cb->cb_func = func;
422	cb->cb_arg = arg;
423	cb->cb_next = callbacks;
424	callbacks = cb;
425}
426
427/* specially-handled commands */
428static void setifaddr(const char *, int, int, const struct afswtch *);
429static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
430
431static void setifdstaddr(const char *, int, int, const struct afswtch *);
432static const struct cmd setifdstaddr_cmd =
433	DEF_CMD("ifdstaddr", 0, setifdstaddr);
434
435static int
436ifconfig(int argc, char *const *argv, const struct afswtch *afp)
437{
438	struct callback *cb;
439	int s;
440
441	if (afp == NULL)
442		afp = af_getbyname("inet");
443	ifr.ifr_addr.sa_family =
444		afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
445		AF_INET : afp->af_af;
446	strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
447
448	if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
449		err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
450
451	while (argc > 0) {
452		const struct cmd *p;
453
454		p = cmd_lookup(*argv);
455		if (p == NULL) {
456			/*
457			 * Not a recognized command, choose between setting
458			 * the interface address and the dst address.
459			 */
460			p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
461		}
462		if (p->c_u.c_func || p->c_u.c_func2) {
463			if (p->c_parameter == NEXTARG) {
464				if (argv[1] == NULL)
465					errx(1, "'%s' requires argument",
466					    p->c_name);
467				p->c_u.c_func(argv[1], 0, s, afp);
468				argc--, argv++;
469			} else if (p->c_parameter == OPTARG) {
470				p->c_u.c_func(argv[1], 0, s, afp);
471				if (argv[1] != NULL)
472					argc--, argv++;
473			} else if (p->c_parameter == NEXTARG2) {
474				if (argc < 3)
475					errx(1, "'%s' requires 2 arguments",
476					    p->c_name);
477				p->c_u.c_func2(argv[1], argv[2], s, afp);
478				argc -= 2, argv += 2;
479			} else
480				p->c_u.c_func(*argv, p->c_parameter, s, afp);
481		}
482		argc--, argv++;
483	}
484
485	/*
486	 * Do any post argument processing required by the address family.
487	 */
488	if (afp->af_postproc != NULL)
489		afp->af_postproc(s, afp);
490	/*
491	 * Do deferred callbacks registered while processing
492	 * command-line arguments.
493	 */
494	for (cb = callbacks; cb != NULL; cb = cb->cb_next)
495		cb->cb_func(s, cb->cb_arg);
496	/*
497	 * Do deferred operations.
498	 */
499	if (clearaddr) {
500		if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
501			warnx("interface %s cannot change %s addresses!",
502			      name, afp->af_name);
503			clearaddr = 0;
504		}
505	}
506	if (clearaddr) {
507		int ret;
508		strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
509		ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
510		if (ret < 0) {
511			if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
512				/* means no previous address for interface */
513			} else
514				Perror("ioctl (SIOCDIFADDR)");
515		}
516	}
517	if (newaddr) {
518		if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
519			warnx("interface %s cannot change %s addresses!",
520			      name, afp->af_name);
521			newaddr = 0;
522		}
523	}
524	if (newaddr && (setaddr || setmask)) {
525		strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
526		if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
527			Perror("ioctl (SIOCAIFADDR)");
528	}
529
530	close(s);
531	return(0);
532}
533
534/*ARGSUSED*/
535static void
536setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
537{
538	if (afp->af_getaddr == NULL)
539		return;
540	/*
541	 * Delay the ioctl to set the interface addr until flags are all set.
542	 * The address interpretation may depend on the flags,
543	 * and the flags may change when the address is set.
544	 */
545	setaddr++;
546	if (doalias == 0 && afp->af_af != AF_LINK)
547		clearaddr = 1;
548	afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
549}
550
551static void
552settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
553{
554	struct addrinfo *srcres, *dstres;
555	int ecode;
556
557	if (afp->af_settunnel == NULL) {
558		warn("address family %s does not support tunnel setup",
559			afp->af_name);
560		return;
561	}
562
563	if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
564		errx(1, "error in parsing address string: %s",
565		    gai_strerror(ecode));
566
567	if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
568		errx(1, "error in parsing address string: %s",
569		    gai_strerror(ecode));
570
571	if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
572		errx(1,
573		    "source and destination address families do not match");
574
575	afp->af_settunnel(s, srcres, dstres);
576
577	freeaddrinfo(srcres);
578	freeaddrinfo(dstres);
579}
580
581/* ARGSUSED */
582static void
583deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
584{
585
586	if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
587		err(1, "SIOCDIFPHYADDR");
588}
589
590static void
591setifnetmask(const char *addr, int dummy __unused, int s,
592    const struct afswtch *afp)
593{
594	if (afp->af_getaddr != NULL) {
595		setmask++;
596		afp->af_getaddr(addr, MASK);
597	}
598}
599
600static void
601setifbroadaddr(const char *addr, int dummy __unused, int s,
602    const struct afswtch *afp)
603{
604	if (afp->af_getaddr != NULL)
605		afp->af_getaddr(addr, DSTADDR);
606}
607
608static void
609setifipdst(const char *addr, int dummy __unused, int s,
610    const struct afswtch *afp)
611{
612	const struct afswtch *inet;
613
614	inet = af_getbyname("inet");
615	if (inet == NULL)
616		return;
617	inet->af_getaddr(addr, DSTADDR);
618	clearaddr = 0;
619	newaddr = 0;
620}
621
622static void
623notealias(const char *addr, int param, int s, const struct afswtch *afp)
624{
625#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
626	if (setaddr && doalias == 0 && param < 0)
627		if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
628			bcopy((caddr_t)rqtosa(af_addreq),
629			      (caddr_t)rqtosa(af_ridreq),
630			      rqtosa(af_addreq)->sa_len);
631	doalias = param;
632	if (param < 0) {
633		clearaddr = 1;
634		newaddr = 0;
635	} else
636		clearaddr = 0;
637#undef rqtosa
638}
639
640/*ARGSUSED*/
641static void
642setifdstaddr(const char *addr, int param __unused, int s,
643    const struct afswtch *afp)
644{
645	if (afp->af_getaddr != NULL)
646		afp->af_getaddr(addr, DSTADDR);
647}
648
649/*
650 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
651 * of the ifreq structure, which may confuse other parts of ifconfig.
652 * Make a private copy so we can avoid that.
653 */
654static void
655setifflags(const char *vname, int value, int s, const struct afswtch *afp)
656{
657	struct ifreq		my_ifr;
658	int flags;
659
660	bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
661
662 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
663 		Perror("ioctl (SIOCGIFFLAGS)");
664 		exit(1);
665 	}
666	strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
667	flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
668
669	if (value < 0) {
670		value = -value;
671		flags &= ~value;
672	} else
673		flags |= value;
674	my_ifr.ifr_flags = flags & 0xffff;
675	my_ifr.ifr_flagshigh = flags >> 16;
676	if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
677		Perror(vname);
678}
679
680void
681setifcap(const char *vname, int value, int s, const struct afswtch *afp)
682{
683	int flags;
684
685 	if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
686 		Perror("ioctl (SIOCGIFCAP)");
687 		exit(1);
688 	}
689	flags = ifr.ifr_curcap;
690	if (value < 0) {
691		value = -value;
692		flags &= ~value;
693	} else
694		flags |= value;
695	flags &= ifr.ifr_reqcap;
696	ifr.ifr_reqcap = flags;
697	if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
698		Perror(vname);
699}
700
701static void
702setifmetric(const char *val, int dummy __unused, int s,
703    const struct afswtch *afp)
704{
705	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
706	ifr.ifr_metric = atoi(val);
707	if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
708		warn("ioctl (set metric)");
709}
710
711static void
712setifmtu(const char *val, int dummy __unused, int s,
713    const struct afswtch *afp)
714{
715	strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
716	ifr.ifr_mtu = atoi(val);
717	if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
718		warn("ioctl (set mtu)");
719}
720
721static void
722setifname(const char *val, int dummy __unused, int s,
723    const struct afswtch *afp)
724{
725	char *newname;
726
727	newname = strdup(val);
728	if (newname == NULL) {
729		warn("no memory to set ifname");
730		return;
731	}
732	ifr.ifr_data = newname;
733	if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
734		warn("ioctl (set name)");
735		free(newname);
736		return;
737	}
738	strlcpy(name, newname, sizeof(name));
739	free(newname);
740}
741
742#define	IFFBITS \
743"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
744"\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
745"\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25NEEDSGIANT"
746
747#define	IFCAPBITS \
748"\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
749"\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO"
750
751/*
752 * Print the status of the interface.  If an address family was
753 * specified, show only it; otherwise, show them all.
754 */
755static void
756status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
757	struct ifaddrs *ifa)
758{
759	struct ifaddrs *ift;
760	int allfamilies, s;
761	struct ifstat ifs;
762
763	if (afp == NULL) {
764		allfamilies = 1;
765		afp = af_getbyname("inet");
766	} else
767		allfamilies = 0;
768
769	ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
770	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
771
772	s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
773	if (s < 0)
774		err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
775
776	printf("%s: ", name);
777	printb("flags", ifa->ifa_flags, IFFBITS);
778	if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
779		printf(" metric %d", ifr.ifr_metric);
780	if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
781		printf(" mtu %d", ifr.ifr_mtu);
782	putchar('\n');
783
784	if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
785		if (ifr.ifr_curcap != 0) {
786			printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
787			putchar('\n');
788		}
789		if (supmedia && ifr.ifr_reqcap != 0) {
790			printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
791			putchar('\n');
792		}
793	}
794
795	tunnel_status(s);
796
797	for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
798		if (ift->ifa_addr == NULL)
799			continue;
800		if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
801			continue;
802		if (allfamilies) {
803			const struct afswtch *p;
804			p = af_getbyfamily(ift->ifa_addr->sa_family);
805			if (p != NULL && p->af_status != NULL)
806				p->af_status(s, ift);
807		} else if (afp->af_af == ift->ifa_addr->sa_family)
808			afp->af_status(s, ift);
809	}
810#if 0
811	if (allfamilies || afp->af_af == AF_LINK) {
812		const struct afswtch *lafp;
813
814		/*
815		 * Hack; the link level address is received separately
816		 * from the routing information so any address is not
817		 * handled above.  Cobble together an entry and invoke
818		 * the status method specially.
819		 */
820		lafp = af_getbyname("lladdr");
821		if (lafp != NULL) {
822			info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
823			lafp->af_status(s, &info);
824		}
825	}
826#endif
827	if (allfamilies)
828		af_other_status(s);
829	else if (afp->af_other_status != NULL)
830		afp->af_other_status(s);
831
832	strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
833	if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
834		printf("%s", ifs.ascii);
835
836	close(s);
837	return;
838}
839
840static void
841tunnel_status(int s)
842{
843	af_all_tunnel_status(s);
844}
845
846void
847Perror(const char *cmd)
848{
849	switch (errno) {
850
851	case ENXIO:
852		errx(1, "%s: no such interface", cmd);
853		break;
854
855	case EPERM:
856		errx(1, "%s: permission denied", cmd);
857		break;
858
859	default:
860		err(1, "%s", cmd);
861	}
862}
863
864/*
865 * Print a value a la the %b format of the kernel's printf
866 */
867void
868printb(const char *s, unsigned v, const char *bits)
869{
870	int i, any = 0;
871	char c;
872
873	if (bits && *bits == 8)
874		printf("%s=%o", s, v);
875	else
876		printf("%s=%x", s, v);
877	bits++;
878	if (bits) {
879		putchar('<');
880		while ((i = *bits++) != '\0') {
881			if (v & (1 << (i-1))) {
882				if (any)
883					putchar(',');
884				any = 1;
885				for (; (c = *bits) > 32; bits++)
886					putchar(c);
887			} else
888				for (; *bits > 32; bits++)
889					;
890		}
891		putchar('>');
892	}
893}
894
895void
896ifmaybeload(const char *name)
897{
898#define MOD_PREFIX_LEN		3	/* "if_" */
899	struct module_stat mstat;
900	int fileid, modid;
901	char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
902	const char *cp;
903
904	/* loading suppressed by the user */
905	if (noload)
906		return;
907
908	/* trim the interface number off the end */
909	strlcpy(ifname, name, sizeof(ifname));
910	for (dp = ifname; *dp != 0; dp++)
911		if (isdigit(*dp)) {
912			*dp = 0;
913			break;
914		}
915
916	/* turn interface and unit into module name */
917	strcpy(ifkind, "if_");
918	strlcpy(ifkind + MOD_PREFIX_LEN, ifname,
919	    sizeof(ifkind) - MOD_PREFIX_LEN);
920
921	/* scan files in kernel */
922	mstat.version = sizeof(struct module_stat);
923	for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
924		/* scan modules in file */
925		for (modid = kldfirstmod(fileid); modid > 0;
926		     modid = modfnext(modid)) {
927			if (modstat(modid, &mstat) < 0)
928				continue;
929			/* strip bus name if present */
930			if ((cp = strchr(mstat.name, '/')) != NULL) {
931				cp++;
932			} else {
933				cp = mstat.name;
934			}
935			/* already loaded? */
936			if (strncmp(ifname, cp, strlen(ifname) + 1) == 0 ||
937			    strncmp(ifkind, cp, strlen(ifkind) + 1) == 0)
938				return;
939		}
940	}
941
942	/* not present, we should try to load it */
943	kldload(ifkind);
944}
945
946static struct cmd basic_cmds[] = {
947	DEF_CMD("up",		IFF_UP,		setifflags),
948	DEF_CMD("down",		-IFF_UP,	setifflags),
949	DEF_CMD("arp",		-IFF_NOARP,	setifflags),
950	DEF_CMD("-arp",		IFF_NOARP,	setifflags),
951	DEF_CMD("debug",	IFF_DEBUG,	setifflags),
952	DEF_CMD("-debug",	-IFF_DEBUG,	setifflags),
953	DEF_CMD("promisc",	IFF_PPROMISC,	setifflags),
954	DEF_CMD("-promisc",	-IFF_PPROMISC,	setifflags),
955	DEF_CMD("add",		IFF_UP,		notealias),
956	DEF_CMD("alias",	IFF_UP,		notealias),
957	DEF_CMD("-alias",	-IFF_UP,	notealias),
958	DEF_CMD("delete",	-IFF_UP,	notealias),
959	DEF_CMD("remove",	-IFF_UP,	notealias),
960#ifdef notdef
961#define	EN_SWABIPS	0x1000
962	DEF_CMD("swabips",	EN_SWABIPS,	setifflags),
963	DEF_CMD("-swabips",	-EN_SWABIPS,	setifflags),
964#endif
965	DEF_CMD_ARG("netmask",			setifnetmask),
966	DEF_CMD_ARG("metric",			setifmetric),
967	DEF_CMD_ARG("broadcast",		setifbroadaddr),
968	DEF_CMD_ARG("ipdst",			setifipdst),
969	DEF_CMD_ARG2("tunnel",			settunnel),
970	DEF_CMD("-tunnel", 0,			deletetunnel),
971	DEF_CMD("deletetunnel", 0,		deletetunnel),
972	DEF_CMD("link0",	IFF_LINK0,	setifflags),
973	DEF_CMD("-link0",	-IFF_LINK0,	setifflags),
974	DEF_CMD("link1",	IFF_LINK1,	setifflags),
975	DEF_CMD("-link1",	-IFF_LINK1,	setifflags),
976	DEF_CMD("link2",	IFF_LINK2,	setifflags),
977	DEF_CMD("-link2",	-IFF_LINK2,	setifflags),
978	DEF_CMD("monitor",	IFF_MONITOR,	setifflags),
979	DEF_CMD("-monitor",	-IFF_MONITOR,	setifflags),
980	DEF_CMD("staticarp",	IFF_STATICARP,	setifflags),
981	DEF_CMD("-staticarp",	-IFF_STATICARP,	setifflags),
982	DEF_CMD("rxcsum",	IFCAP_RXCSUM,	setifcap),
983	DEF_CMD("-rxcsum",	-IFCAP_RXCSUM,	setifcap),
984	DEF_CMD("txcsum",	IFCAP_TXCSUM,	setifcap),
985	DEF_CMD("-txcsum",	-IFCAP_TXCSUM,	setifcap),
986	DEF_CMD("netcons",	IFCAP_NETCONS,	setifcap),
987	DEF_CMD("-netcons",	-IFCAP_NETCONS,	setifcap),
988	DEF_CMD("polling",	IFCAP_POLLING,	setifcap),
989	DEF_CMD("-polling",	-IFCAP_POLLING,	setifcap),
990	DEF_CMD("tso",		IFCAP_TSO,	setifcap),
991	DEF_CMD("-tso",		-IFCAP_TSO,	setifcap),
992	DEF_CMD("lro",		IFCAP_LRO,	setifcap),
993	DEF_CMD("-lro",		-IFCAP_LRO,	setifcap),
994	DEF_CMD("normal",	-IFF_LINK0,	setifflags),
995	DEF_CMD("compress",	IFF_LINK0,	setifflags),
996	DEF_CMD("noicmp",	IFF_LINK1,	setifflags),
997	DEF_CMD_ARG("mtu",			setifmtu),
998	DEF_CMD_ARG("name",			setifname),
999};
1000
1001static __constructor void
1002ifconfig_ctor(void)
1003{
1004#define	N(a)	(sizeof(a) / sizeof(a[0]))
1005	int i;
1006
1007	for (i = 0; i < N(basic_cmds);  i++)
1008		cmd_register(&basic_cmds[i]);
1009#undef N
1010}
1011