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