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