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