1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1983, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
41 #endif
42 static const char rcsid[] =
43 "$FreeBSD$";
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/ioctl.h>
48 #include <sys/module.h>
49 #include <sys/linker.h>
50 #include <sys/queue.h>
51 #include <sys/socket.h>
52 #include <sys/time.h>
53
54 #include <net/ethernet.h>
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59
60 /* IP */
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <arpa/inet.h>
64 #include <netdb.h>
65
66 #include <ifaddrs.h>
67 #include <ctype.h>
68 #include <err.h>
69 #include <errno.h>
70 #include <fcntl.h>
71 #ifdef JAIL
72 #include <jail.h>
73 #endif
74 #include <stdbool.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <unistd.h>
79
80 #include "ifconfig.h"
81
82 /*
83 * Since "struct ifreq" is composed of various union members, callers
84 * should pay special attention to interpret the value.
85 * (.e.g. little/big endian difference in the structure.)
86 */
87 struct ifreq ifr;
88
89 char name[IFNAMSIZ];
90 char *descr = NULL;
91 size_t descrlen = 64;
92 int setaddr;
93 int setmask;
94 int doalias;
95 int clearaddr;
96 int newaddr = 1;
97 int verbose;
98 int noload;
99 int printifname = 0;
100
101 int supmedia = 0;
102 int printkeys = 0; /* Print keying material for interfaces. */
103 int exit_code = 0;
104
105 /* Formatter Strings */
106 char *f_inet, *f_inet6, *f_ether, *f_addr;
107
108 static int ifconfig(int argc, char *const *argv, int iscreate,
109 const struct afswtch *afp);
110 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
111 struct ifaddrs *ifa);
112 static void tunnel_status(int s);
113 static _Noreturn void usage(void);
114
115 static int getifflags(const char *ifname, int us);
116
117 static struct afswtch *af_getbyname(const char *name);
118 static struct afswtch *af_getbyfamily(int af);
119 static void af_other_status(int);
120
121 void printifnamemaybe(void);
122
123 static struct option *opts = NULL;
124
125 struct ifa_order_elt {
126 int if_order;
127 int af_orders[255];
128 struct ifaddrs *ifa;
129 TAILQ_ENTRY(ifa_order_elt) link;
130 };
131
132 TAILQ_HEAD(ifa_queue, ifa_order_elt);
133
134 static struct module_map_entry {
135 const char *ifname;
136 const char *kldname;
137 } module_map[] = {
138 {
139 .ifname = "vmnet",
140 .kldname = "if_tap",
141 },
142 {
143 .ifname = "ipsec",
144 .kldname = "ipsec",
145 },
146 {
147 /*
148 * This mapping exists because there is a conflicting enc module
149 * in CAM. ifconfig's guessing behavior will attempt to match
150 * the ifname to a module as well as if_${ifname} and clash with
151 * CAM enc. This is an assertion of the correct module to load.
152 */
153 .ifname = "enc",
154 .kldname = "if_enc",
155 },
156 };
157
158
159 void
opt_register(struct option * p)160 opt_register(struct option *p)
161 {
162 p->next = opts;
163 opts = p;
164 }
165
166 static void
usage(void)167 usage(void)
168 {
169 char options[1024];
170 struct option *p;
171
172 /* XXX not right but close enough for now */
173 options[0] = '\0';
174 for (p = opts; p != NULL; p = p->next) {
175 strlcat(options, p->opt_usage, sizeof(options));
176 strlcat(options, " ", sizeof(options));
177 }
178
179 fprintf(stderr,
180 "usage: ifconfig [-f type:format] %sinterface address_family\n"
181 " [address [dest_address]] [parameters]\n"
182 " ifconfig interface create\n"
183 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
184 " ifconfig -l [-d] [-u] [address_family]\n"
185 " ifconfig %s[-d] [-m] [-u] [-v]\n",
186 options, options, options);
187 exit(1);
188 }
189
190 #define ORDERS_SIZE(x) sizeof(x) / sizeof(x[0])
191
192 static int
calcorders(struct ifaddrs * ifa,struct ifa_queue * q)193 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
194 {
195 struct ifaddrs *prev;
196 struct ifa_order_elt *cur;
197 unsigned int ord, af, ifa_ord;
198
199 prev = NULL;
200 cur = NULL;
201 ord = 0;
202 ifa_ord = 0;
203
204 while (ifa != NULL) {
205 if (prev == NULL ||
206 strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
207 cur = calloc(1, sizeof(*cur));
208
209 if (cur == NULL)
210 return (-1);
211
212 TAILQ_INSERT_TAIL(q, cur, link);
213 cur->if_order = ifa_ord ++;
214 cur->ifa = ifa;
215 ord = 0;
216 }
217
218 if (ifa->ifa_addr) {
219 af = ifa->ifa_addr->sa_family;
220
221 if (af < ORDERS_SIZE(cur->af_orders) &&
222 cur->af_orders[af] == 0)
223 cur->af_orders[af] = ++ord;
224 }
225 prev = ifa;
226 ifa = ifa->ifa_next;
227 }
228
229 return (0);
230 }
231
232 static int
cmpifaddrs(struct ifaddrs * a,struct ifaddrs * b,struct ifa_queue * q)233 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
234 {
235 struct ifa_order_elt *cur, *e1, *e2;
236 unsigned int af1, af2;
237 int ret;
238
239 e1 = e2 = NULL;
240
241 ret = strcmp(a->ifa_name, b->ifa_name);
242 if (ret != 0) {
243 TAILQ_FOREACH(cur, q, link) {
244 if (e1 && e2)
245 break;
246
247 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
248 e1 = cur;
249 else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
250 e2 = cur;
251 }
252
253 if (!e1 || !e2)
254 return (0);
255 else
256 return (e1->if_order - e2->if_order);
257
258 } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
259 TAILQ_FOREACH(cur, q, link) {
260 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
261 e1 = cur;
262 break;
263 }
264 }
265
266 if (!e1)
267 return (0);
268
269 af1 = a->ifa_addr->sa_family;
270 af2 = b->ifa_addr->sa_family;
271
272 if (af1 < ORDERS_SIZE(e1->af_orders) &&
273 af2 < ORDERS_SIZE(e1->af_orders))
274 return (e1->af_orders[af1] - e1->af_orders[af2]);
275 }
276
277 return (0);
278 }
279
freeformat(void)280 static void freeformat(void)
281 {
282
283 if (f_inet != NULL)
284 free(f_inet);
285 if (f_inet6 != NULL)
286 free(f_inet6);
287 if (f_ether != NULL)
288 free(f_ether);
289 if (f_addr != NULL)
290 free(f_addr);
291 }
292
setformat(char * input)293 static void setformat(char *input)
294 {
295 char *formatstr, *category, *modifier;
296
297 formatstr = strdup(input);
298 while ((category = strsep(&formatstr, ",")) != NULL) {
299 modifier = strchr(category, ':');
300 if (modifier == NULL || modifier[1] == '\0') {
301 warnx("Skipping invalid format specification: %s\n",
302 category);
303 continue;
304 }
305
306 /* Split the string on the separator, then seek past it */
307 modifier[0] = '\0';
308 modifier++;
309
310 if (strcmp(category, "addr") == 0)
311 f_addr = strdup(modifier);
312 else if (strcmp(category, "ether") == 0)
313 f_ether = strdup(modifier);
314 else if (strcmp(category, "inet") == 0)
315 f_inet = strdup(modifier);
316 else if (strcmp(category, "inet6") == 0)
317 f_inet6 = strdup(modifier);
318 }
319 free(formatstr);
320 }
321
322 #undef ORDERS_SIZE
323
324 static struct ifaddrs *
sortifaddrs(struct ifaddrs * list,int (* compare)(struct ifaddrs *,struct ifaddrs *,struct ifa_queue *),struct ifa_queue * q)325 sortifaddrs(struct ifaddrs *list,
326 int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *),
327 struct ifa_queue *q)
328 {
329 struct ifaddrs *right, *temp, *last, *result, *next, *tail;
330
331 right = list;
332 temp = list;
333 last = list;
334 result = NULL;
335 next = NULL;
336 tail = NULL;
337
338 if (!list || !list->ifa_next)
339 return (list);
340
341 while (temp && temp->ifa_next) {
342 last = right;
343 right = right->ifa_next;
344 temp = temp->ifa_next->ifa_next;
345 }
346
347 last->ifa_next = NULL;
348
349 list = sortifaddrs(list, compare, q);
350 right = sortifaddrs(right, compare, q);
351
352 while (list || right) {
353
354 if (!right) {
355 next = list;
356 list = list->ifa_next;
357 } else if (!list) {
358 next = right;
359 right = right->ifa_next;
360 } else if (compare(list, right, q) <= 0) {
361 next = list;
362 list = list->ifa_next;
363 } else {
364 next = right;
365 right = right->ifa_next;
366 }
367
368 if (!result)
369 result = next;
370 else
371 tail->ifa_next = next;
372
373 tail = next;
374 }
375
376 return (result);
377 }
378
printifnamemaybe()379 void printifnamemaybe()
380 {
381 if (printifname)
382 printf("%s\n", name);
383 }
384
385 int
main(int argc,char * argv[])386 main(int argc, char *argv[])
387 {
388 int c, all, namesonly, downonly, uponly;
389 const struct afswtch *afp = NULL;
390 int ifindex;
391 struct ifaddrs *ifap, *sifap, *ifa;
392 struct ifreq paifr;
393 const struct sockaddr_dl *sdl;
394 char options[1024], *cp, *envformat, *namecp = NULL;
395 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
396 struct ifa_order_elt *cur, *tmp;
397 const char *ifname;
398 struct option *p;
399 size_t iflen;
400 int flags;
401
402 all = downonly = uponly = namesonly = noload = verbose = 0;
403 f_inet = f_inet6 = f_ether = f_addr = NULL;
404
405 envformat = getenv("IFCONFIG_FORMAT");
406 if (envformat != NULL)
407 setformat(envformat);
408
409 /*
410 * Ensure we print interface name when expected to,
411 * even if we terminate early due to error.
412 */
413 atexit(printifnamemaybe);
414
415 /* Parse leading line options */
416 strlcpy(options, "f:adklmnuv", sizeof(options));
417 for (p = opts; p != NULL; p = p->next)
418 strlcat(options, p->opt, sizeof(options));
419 while ((c = getopt(argc, argv, options)) != -1) {
420 switch (c) {
421 case 'a': /* scan all interfaces */
422 all++;
423 break;
424 case 'd': /* restrict scan to "down" interfaces */
425 downonly++;
426 break;
427 case 'f':
428 if (optarg == NULL)
429 usage();
430 setformat(optarg);
431 break;
432 case 'k':
433 printkeys++;
434 break;
435 case 'l': /* scan interface names only */
436 namesonly++;
437 break;
438 case 'm': /* show media choices in status */
439 supmedia = 1;
440 break;
441 case 'n': /* suppress module loading */
442 noload++;
443 break;
444 case 'u': /* restrict scan to "up" interfaces */
445 uponly++;
446 break;
447 case 'v':
448 verbose++;
449 break;
450 default:
451 for (p = opts; p != NULL; p = p->next)
452 if (p->opt[0] == c) {
453 p->cb(optarg);
454 break;
455 }
456 if (p == NULL)
457 usage();
458 break;
459 }
460 }
461 argc -= optind;
462 argv += optind;
463
464 /* -l cannot be used with -a or -m */
465 if (namesonly && (all || supmedia))
466 usage();
467
468 /* nonsense.. */
469 if (uponly && downonly)
470 usage();
471
472 /* no arguments is equivalent to '-a' */
473 if (!namesonly && argc < 1)
474 all = 1;
475
476 /* -a and -l allow an address family arg to limit the output */
477 if (all || namesonly) {
478 if (argc > 1)
479 usage();
480
481 ifname = NULL;
482 ifindex = 0;
483 if (argc == 1) {
484 afp = af_getbyname(*argv);
485 if (afp == NULL) {
486 warnx("Address family '%s' unknown.", *argv);
487 usage();
488 }
489 if (afp->af_name != NULL)
490 argc--, argv++;
491 /* leave with afp non-zero */
492 }
493 } else {
494 /* not listing, need an argument */
495 if (argc < 1)
496 usage();
497
498 ifname = *argv;
499 argc--, argv++;
500
501 /* check and maybe load support for this interface */
502 ifmaybeload(ifname);
503
504 ifindex = if_nametoindex(ifname);
505 if (ifindex == 0) {
506 /*
507 * NOTE: We must special-case the `create' command
508 * right here as we would otherwise fail when trying
509 * to find the interface.
510 */
511 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
512 strcmp(argv[0], "plumb") == 0)) {
513 iflen = strlcpy(name, ifname, sizeof(name));
514 if (iflen >= sizeof(name))
515 errx(1, "%s: cloning name too long",
516 ifname);
517 ifconfig(argc, argv, 1, NULL);
518 exit(exit_code);
519 }
520 #ifdef JAIL
521 /*
522 * NOTE: We have to special-case the `-vnet' command
523 * right here as we would otherwise fail when trying
524 * to find the interface as it lives in another vnet.
525 */
526 if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
527 iflen = strlcpy(name, ifname, sizeof(name));
528 if (iflen >= sizeof(name))
529 errx(1, "%s: interface name too long",
530 ifname);
531 ifconfig(argc, argv, 0, NULL);
532 exit(exit_code);
533 }
534 #endif
535 errx(1, "interface %s does not exist", ifname);
536 } else {
537 /*
538 * Do not allow use `create` command as hostname if
539 * address family is not specified.
540 */
541 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
542 strcmp(argv[0], "plumb") == 0)) {
543 if (argc == 1)
544 errx(1, "interface %s already exists",
545 ifname);
546 argc--, argv++;
547 }
548 }
549 }
550
551 /* Check for address family */
552 if (argc > 0) {
553 afp = af_getbyname(*argv);
554 if (afp != NULL)
555 argc--, argv++;
556 }
557
558 /*
559 * Check for a requested configuration action on a single interface,
560 * which doesn't require building, sorting, and searching the entire
561 * system address list
562 */
563 if ((argc > 0) && (ifname != NULL)) {
564 iflen = strlcpy(name, ifname, sizeof(name));
565 if (iflen >= sizeof(name)) {
566 warnx("%s: interface name too long, skipping", ifname);
567 } else {
568 flags = getifflags(name, -1);
569 if (!(((flags & IFF_CANTCONFIG) != 0) ||
570 (downonly && (flags & IFF_UP) != 0) ||
571 (uponly && (flags & IFF_UP) == 0)))
572 ifconfig(argc, argv, 0, afp);
573 }
574 goto done;
575 }
576
577 if (getifaddrs(&ifap) != 0)
578 err(EXIT_FAILURE, "getifaddrs");
579
580 cp = NULL;
581
582 if (calcorders(ifap, &q) != 0)
583 err(EXIT_FAILURE, "calcorders");
584
585 sifap = sortifaddrs(ifap, cmpifaddrs, &q);
586
587 TAILQ_FOREACH_SAFE(cur, &q, link, tmp)
588 free(cur);
589
590 ifindex = 0;
591 for (ifa = sifap; ifa; ifa = ifa->ifa_next) {
592 memset(&paifr, 0, sizeof(paifr));
593 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
594 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
595 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
596 ifa->ifa_addr->sa_len);
597 }
598
599 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
600 continue;
601 if (ifa->ifa_addr->sa_family == AF_LINK)
602 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
603 else
604 sdl = NULL;
605 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
606 continue;
607 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
608 if (iflen >= sizeof(name)) {
609 warnx("%s: interface name too long, skipping",
610 ifa->ifa_name);
611 continue;
612 }
613 cp = ifa->ifa_name;
614
615 if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
616 continue;
617 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
618 continue;
619 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
620 continue;
621 /*
622 * Are we just listing the interfaces?
623 */
624 if (namesonly) {
625 if (namecp == cp)
626 continue;
627 if (afp != NULL) {
628 /* special case for "ether" address family */
629 if (!strcmp(afp->af_name, "ether")) {
630 if (sdl == NULL ||
631 (sdl->sdl_type != IFT_ETHER &&
632 sdl->sdl_type != IFT_L2VLAN &&
633 sdl->sdl_type != IFT_BRIDGE) ||
634 sdl->sdl_alen != ETHER_ADDR_LEN)
635 continue;
636 } else {
637 if (ifa->ifa_addr->sa_family
638 != afp->af_af)
639 continue;
640 }
641 }
642 namecp = cp;
643 ifindex++;
644 if (ifindex > 1)
645 printf(" ");
646 fputs(name, stdout);
647 continue;
648 }
649 ifindex++;
650
651 if (argc > 0)
652 ifconfig(argc, argv, 0, afp);
653 else
654 status(afp, sdl, ifa);
655 }
656 if (namesonly)
657 printf("\n");
658 freeifaddrs(ifap);
659
660 done:
661 freeformat();
662 exit(exit_code);
663 }
664
665 static struct afswtch *afs = NULL;
666
667 void
af_register(struct afswtch * p)668 af_register(struct afswtch *p)
669 {
670 p->af_next = afs;
671 afs = p;
672 }
673
674 static struct afswtch *
af_getbyname(const char * name)675 af_getbyname(const char *name)
676 {
677 struct afswtch *afp;
678
679 for (afp = afs; afp != NULL; afp = afp->af_next)
680 if (strcmp(afp->af_name, name) == 0)
681 return afp;
682 return NULL;
683 }
684
685 static struct afswtch *
af_getbyfamily(int af)686 af_getbyfamily(int af)
687 {
688 struct afswtch *afp;
689
690 for (afp = afs; afp != NULL; afp = afp->af_next)
691 if (afp->af_af == af)
692 return afp;
693 return NULL;
694 }
695
696 static void
af_other_status(int s)697 af_other_status(int s)
698 {
699 struct afswtch *afp;
700 uint8_t afmask[howmany(AF_MAX, NBBY)];
701
702 memset(afmask, 0, sizeof(afmask));
703 for (afp = afs; afp != NULL; afp = afp->af_next) {
704 if (afp->af_other_status == NULL)
705 continue;
706 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
707 continue;
708 afp->af_other_status(s);
709 setbit(afmask, afp->af_af);
710 }
711 }
712
713 static void
af_all_tunnel_status(int s)714 af_all_tunnel_status(int s)
715 {
716 struct afswtch *afp;
717 uint8_t afmask[howmany(AF_MAX, NBBY)];
718
719 memset(afmask, 0, sizeof(afmask));
720 for (afp = afs; afp != NULL; afp = afp->af_next) {
721 if (afp->af_status_tunnel == NULL)
722 continue;
723 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
724 continue;
725 afp->af_status_tunnel(s);
726 setbit(afmask, afp->af_af);
727 }
728 }
729
730 static struct cmd *cmds = NULL;
731
732 void
cmd_register(struct cmd * p)733 cmd_register(struct cmd *p)
734 {
735 p->c_next = cmds;
736 cmds = p;
737 }
738
739 static const struct cmd *
cmd_lookup(const char * name,int iscreate)740 cmd_lookup(const char *name, int iscreate)
741 {
742 const struct cmd *p;
743
744 for (p = cmds; p != NULL; p = p->c_next)
745 if (strcmp(name, p->c_name) == 0) {
746 if (iscreate) {
747 if (p->c_iscloneop)
748 return p;
749 } else {
750 if (!p->c_iscloneop)
751 return p;
752 }
753 }
754 return NULL;
755 }
756
757 struct callback {
758 callback_func *cb_func;
759 void *cb_arg;
760 struct callback *cb_next;
761 };
762 static struct callback *callbacks = NULL;
763
764 void
callback_register(callback_func * func,void * arg)765 callback_register(callback_func *func, void *arg)
766 {
767 struct callback *cb;
768
769 cb = malloc(sizeof(struct callback));
770 if (cb == NULL)
771 errx(1, "unable to allocate memory for callback");
772 cb->cb_func = func;
773 cb->cb_arg = arg;
774 cb->cb_next = callbacks;
775 callbacks = cb;
776 }
777
778 /* specially-handled commands */
779 static void setifaddr(const char *, int, int, const struct afswtch *);
780 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
781
782 static void setifdstaddr(const char *, int, int, const struct afswtch *);
783 static const struct cmd setifdstaddr_cmd =
784 DEF_CMD("ifdstaddr", 0, setifdstaddr);
785
786 static int
ifconfig(int argc,char * const * argv,int iscreate,const struct afswtch * uafp)787 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
788 {
789 const struct afswtch *afp, *nafp;
790 const struct cmd *p;
791 struct callback *cb;
792 int s;
793
794 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
795 afp = NULL;
796 if (uafp != NULL)
797 afp = uafp;
798 /*
799 * This is the historical "accident" allowing users to configure IPv4
800 * addresses without the "inet" keyword which while a nice feature has
801 * proven to complicate other things. We cannot remove this but only
802 * make sure we will never have a similar implicit default for IPv6 or
803 * any other address familiy. We need a fallback though for
804 * ifconfig IF up/down etc. to work without INET support as people
805 * never used ifconfig IF link up/down, etc. either.
806 */
807 #ifndef RESCUE
808 #ifdef INET
809 if (afp == NULL && feature_present("inet"))
810 afp = af_getbyname("inet");
811 #endif
812 #endif
813 if (afp == NULL)
814 afp = af_getbyname("link");
815 if (afp == NULL) {
816 warnx("Please specify an address_family.");
817 usage();
818 }
819 top:
820 ifr.ifr_addr.sa_family =
821 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
822 AF_LOCAL : afp->af_af;
823
824 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
825 (uafp != NULL || errno != EAFNOSUPPORT ||
826 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
827 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
828
829 while (argc > 0) {
830 p = cmd_lookup(*argv, iscreate);
831 if (iscreate && p == NULL) {
832 /*
833 * Push the clone create callback so the new
834 * device is created and can be used for any
835 * remaining arguments.
836 */
837 cb = callbacks;
838 if (cb == NULL)
839 errx(1, "internal error, no callback");
840 callbacks = cb->cb_next;
841 cb->cb_func(s, cb->cb_arg);
842 iscreate = 0;
843 /*
844 * Handle any address family spec that
845 * immediately follows and potentially
846 * recreate the socket.
847 */
848 nafp = af_getbyname(*argv);
849 if (nafp != NULL) {
850 argc--, argv++;
851 if (nafp != afp) {
852 close(s);
853 afp = nafp;
854 goto top;
855 }
856 }
857 /*
858 * Look for a normal parameter.
859 */
860 continue;
861 }
862 if (p == NULL) {
863 /*
864 * Not a recognized command, choose between setting
865 * the interface address and the dst address.
866 */
867 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
868 }
869 if (p->c_parameter == NEXTARG && p->c_u.c_func) {
870 if (argv[1] == NULL)
871 errx(1, "'%s' requires argument",
872 p->c_name);
873 p->c_u.c_func(argv[1], 0, s, afp);
874 argc--, argv++;
875 } else if (p->c_parameter == OPTARG && p->c_u.c_func) {
876 p->c_u.c_func(argv[1], 0, s, afp);
877 if (argv[1] != NULL)
878 argc--, argv++;
879 } else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) {
880 if (argc < 3)
881 errx(1, "'%s' requires 2 arguments",
882 p->c_name);
883 p->c_u.c_func2(argv[1], argv[2], s, afp);
884 argc -= 2, argv += 2;
885 } else if (p->c_u.c_func)
886 p->c_u.c_func(*argv, p->c_parameter, s, afp);
887 argc--, argv++;
888 }
889
890 /*
891 * Do any post argument processing required by the address family.
892 */
893 if (afp->af_postproc != NULL)
894 afp->af_postproc(s, afp);
895 /*
896 * Do deferred callbacks registered while processing
897 * command-line arguments.
898 */
899 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
900 cb->cb_func(s, cb->cb_arg);
901 /*
902 * Do deferred operations.
903 */
904 if (clearaddr) {
905 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
906 warnx("interface %s cannot change %s addresses!",
907 name, afp->af_name);
908 clearaddr = 0;
909 }
910 }
911 if (clearaddr) {
912 int ret;
913 strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
914 sizeof ifr.ifr_name);
915 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
916 if (ret < 0) {
917 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
918 /* means no previous address for interface */
919 } else
920 Perror("ioctl (SIOCDIFADDR)");
921 }
922 }
923 if (newaddr) {
924 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
925 warnx("interface %s cannot change %s addresses!",
926 name, afp->af_name);
927 newaddr = 0;
928 }
929 }
930 if (newaddr && (setaddr || setmask)) {
931 strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
932 sizeof ifr.ifr_name);
933 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
934 Perror("ioctl (SIOCAIFADDR)");
935 }
936
937 close(s);
938 return(0);
939 }
940
941 /*ARGSUSED*/
942 static void
setifaddr(const char * addr,int param,int s,const struct afswtch * afp)943 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
944 {
945 if (afp->af_getaddr == NULL)
946 return;
947 /*
948 * Delay the ioctl to set the interface addr until flags are all set.
949 * The address interpretation may depend on the flags,
950 * and the flags may change when the address is set.
951 */
952 setaddr++;
953 if (doalias == 0 && afp->af_af != AF_LINK)
954 clearaddr = 1;
955 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
956 }
957
958 static void
settunnel(const char * src,const char * dst,int s,const struct afswtch * afp)959 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
960 {
961 struct addrinfo *srcres, *dstres;
962 int ecode;
963
964 if (afp->af_settunnel == NULL) {
965 warn("address family %s does not support tunnel setup",
966 afp->af_name);
967 return;
968 }
969
970 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
971 errx(1, "error in parsing address string: %s",
972 gai_strerror(ecode));
973
974 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
975 errx(1, "error in parsing address string: %s",
976 gai_strerror(ecode));
977
978 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
979 errx(1,
980 "source and destination address families do not match");
981
982 afp->af_settunnel(s, srcres, dstres);
983
984 freeaddrinfo(srcres);
985 freeaddrinfo(dstres);
986 }
987
988 /* ARGSUSED */
989 static void
deletetunnel(const char * vname,int param,int s,const struct afswtch * afp)990 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
991 {
992
993 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
994 err(1, "SIOCDIFPHYADDR");
995 }
996
997 #ifdef JAIL
998 static void
setifvnet(const char * jname,int dummy __unused,int s,const struct afswtch * afp)999 setifvnet(const char *jname, int dummy __unused, int s,
1000 const struct afswtch *afp)
1001 {
1002 struct ifreq my_ifr;
1003
1004 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1005 my_ifr.ifr_jid = jail_getid(jname);
1006 if (my_ifr.ifr_jid < 0)
1007 errx(1, "%s", jail_errmsg);
1008 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
1009 err(1, "SIOCSIFVNET");
1010 }
1011
1012 static void
setifrvnet(const char * jname,int dummy __unused,int s,const struct afswtch * afp)1013 setifrvnet(const char *jname, int dummy __unused, int s,
1014 const struct afswtch *afp)
1015 {
1016 struct ifreq my_ifr;
1017
1018 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1019 my_ifr.ifr_jid = jail_getid(jname);
1020 if (my_ifr.ifr_jid < 0)
1021 errx(1, "%s", jail_errmsg);
1022 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
1023 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
1024 }
1025 #endif
1026
1027 static void
setifnetmask(const char * addr,int dummy __unused,int s,const struct afswtch * afp)1028 setifnetmask(const char *addr, int dummy __unused, int s,
1029 const struct afswtch *afp)
1030 {
1031 if (afp->af_getaddr != NULL) {
1032 setmask++;
1033 afp->af_getaddr(addr, MASK);
1034 }
1035 }
1036
1037 static void
setifbroadaddr(const char * addr,int dummy __unused,int s,const struct afswtch * afp)1038 setifbroadaddr(const char *addr, int dummy __unused, int s,
1039 const struct afswtch *afp)
1040 {
1041 if (afp->af_getaddr != NULL)
1042 afp->af_getaddr(addr, DSTADDR);
1043 }
1044
1045 static void
notealias(const char * addr,int param,int s,const struct afswtch * afp)1046 notealias(const char *addr, int param, int s, const struct afswtch *afp)
1047 {
1048 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1049 if (setaddr && doalias == 0 && param < 0)
1050 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1051 bcopy((caddr_t)rqtosa(af_addreq),
1052 (caddr_t)rqtosa(af_ridreq),
1053 rqtosa(af_addreq)->sa_len);
1054 doalias = param;
1055 if (param < 0) {
1056 clearaddr = 1;
1057 newaddr = 0;
1058 } else
1059 clearaddr = 0;
1060 #undef rqtosa
1061 }
1062
1063 /*ARGSUSED*/
1064 static void
setifdstaddr(const char * addr,int param __unused,int s,const struct afswtch * afp)1065 setifdstaddr(const char *addr, int param __unused, int s,
1066 const struct afswtch *afp)
1067 {
1068 if (afp->af_getaddr != NULL)
1069 afp->af_getaddr(addr, DSTADDR);
1070 }
1071
1072 static int
getifflags(const char * ifname,int us)1073 getifflags(const char *ifname, int us)
1074 {
1075 struct ifreq my_ifr;
1076 int s;
1077
1078 memset(&my_ifr, 0, sizeof(my_ifr));
1079 (void) strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name));
1080 if (us < 0) {
1081 if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
1082 err(1, "socket(family AF_LOCAL,SOCK_DGRAM");
1083 } else
1084 s = us;
1085 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1086 Perror("ioctl (SIOCGIFFLAGS)");
1087 exit(1);
1088 }
1089 if (us < 0)
1090 close(s);
1091 return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16));
1092 }
1093
1094 /*
1095 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1096 * of the ifreq structure, which may confuse other parts of ifconfig.
1097 * Make a private copy so we can avoid that.
1098 */
1099 static void
setifflags(const char * vname,int value,int s,const struct afswtch * afp)1100 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1101 {
1102 struct ifreq my_ifr;
1103 int flags;
1104
1105 flags = getifflags(name, s);
1106 if (value < 0) {
1107 value = -value;
1108 flags &= ~value;
1109 } else
1110 flags |= value;
1111 memset(&my_ifr, 0, sizeof(my_ifr));
1112 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1113 my_ifr.ifr_flags = flags & 0xffff;
1114 my_ifr.ifr_flagshigh = flags >> 16;
1115 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1116 Perror(vname);
1117 }
1118
1119 void
setifcap(const char * vname,int value,int s,const struct afswtch * afp)1120 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1121 {
1122 int flags;
1123
1124 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1125 Perror("ioctl (SIOCGIFCAP)");
1126 exit(1);
1127 }
1128 flags = ifr.ifr_curcap;
1129 if (value < 0) {
1130 value = -value;
1131 flags &= ~value;
1132 } else
1133 flags |= value;
1134 flags &= ifr.ifr_reqcap;
1135 ifr.ifr_reqcap = flags;
1136 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1137 Perror(vname);
1138 }
1139
1140 static void
setifmetric(const char * val,int dummy __unused,int s,const struct afswtch * afp)1141 setifmetric(const char *val, int dummy __unused, int s,
1142 const struct afswtch *afp)
1143 {
1144 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1145 ifr.ifr_metric = atoi(val);
1146 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1147 err(1, "ioctl SIOCSIFMETRIC (set metric)");
1148 }
1149
1150 static void
setifmtu(const char * val,int dummy __unused,int s,const struct afswtch * afp)1151 setifmtu(const char *val, int dummy __unused, int s,
1152 const struct afswtch *afp)
1153 {
1154 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1155 ifr.ifr_mtu = atoi(val);
1156 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1157 err(1, "ioctl SIOCSIFMTU (set mtu)");
1158 }
1159
1160 static void
setifpcp(const char * val,int arg __unused,int s,const struct afswtch * afp)1161 setifpcp(const char *val, int arg __unused, int s, const struct afswtch *afp)
1162 {
1163 u_long ul;
1164 char *endp;
1165
1166 ul = strtoul(val, &endp, 0);
1167 if (*endp != '\0')
1168 errx(1, "invalid value for pcp");
1169 if (ul > 7)
1170 errx(1, "value for pcp out of range");
1171 ifr.ifr_lan_pcp = ul;
1172 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1173 err(1, "SIOCSLANPCP");
1174 }
1175
1176 static void
disableifpcp(const char * val,int arg __unused,int s,const struct afswtch * afp)1177 disableifpcp(const char *val, int arg __unused, int s,
1178 const struct afswtch *afp)
1179 {
1180
1181 ifr.ifr_lan_pcp = IFNET_PCP_NONE;
1182 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1183 err(1, "SIOCSLANPCP");
1184 }
1185
1186 static void
setifname(const char * val,int dummy __unused,int s,const struct afswtch * afp)1187 setifname(const char *val, int dummy __unused, int s,
1188 const struct afswtch *afp)
1189 {
1190 char *newname;
1191
1192 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1193
1194 newname = strdup(val);
1195 if (newname == NULL)
1196 err(1, "no memory to set ifname");
1197 ifr.ifr_data = newname;
1198 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1199 free(newname);
1200 err(1, "ioctl SIOCSIFNAME (set name)");
1201 }
1202 printifname = 1;
1203 strlcpy(name, newname, sizeof(name));
1204 free(newname);
1205 }
1206
1207 /* ARGSUSED */
1208 static void
setifdescr(const char * val,int dummy __unused,int s,const struct afswtch * afp)1209 setifdescr(const char *val, int dummy __unused, int s,
1210 const struct afswtch *afp)
1211 {
1212 char *newdescr;
1213
1214 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1215
1216 ifr.ifr_buffer.length = strlen(val) + 1;
1217 if (ifr.ifr_buffer.length == 1) {
1218 ifr.ifr_buffer.buffer = newdescr = NULL;
1219 ifr.ifr_buffer.length = 0;
1220 } else {
1221 newdescr = strdup(val);
1222 ifr.ifr_buffer.buffer = newdescr;
1223 if (newdescr == NULL) {
1224 warn("no memory to set ifdescr");
1225 return;
1226 }
1227 }
1228
1229 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
1230 err(1, "ioctl SIOCSIFDESCR (set descr)");
1231
1232 free(newdescr);
1233 }
1234
1235 /* ARGSUSED */
1236 static void
unsetifdescr(const char * val,int value,int s,const struct afswtch * afp)1237 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
1238 {
1239
1240 setifdescr("", 0, s, 0);
1241 }
1242
1243 #define IFFBITS \
1244 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \
1245 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1246 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
1247
1248 #define IFCAPBITS \
1249 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1250 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1251 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1252 "\26RXCSUM_IPV6\27TXCSUM_IPV6\31TXRTLMT\32HWRXTSTMP"
1253
1254 /*
1255 * Print the status of the interface. If an address family was
1256 * specified, show only it; otherwise, show them all.
1257 */
1258 static void
status(const struct afswtch * afp,const struct sockaddr_dl * sdl,struct ifaddrs * ifa)1259 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
1260 struct ifaddrs *ifa)
1261 {
1262 struct ifaddrs *ift;
1263 int allfamilies, s;
1264 struct ifstat ifs;
1265
1266 if (afp == NULL) {
1267 allfamilies = 1;
1268 ifr.ifr_addr.sa_family = AF_LOCAL;
1269 } else {
1270 allfamilies = 0;
1271 ifr.ifr_addr.sa_family =
1272 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
1273 }
1274 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1275
1276 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1277 if (s < 0)
1278 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1279
1280 printf("%s: ", name);
1281 printb("flags", ifa->ifa_flags, IFFBITS);
1282 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1283 printf(" metric %d", ifr.ifr_metric);
1284 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1285 printf(" mtu %d", ifr.ifr_mtu);
1286 putchar('\n');
1287
1288 for (;;) {
1289 if ((descr = reallocf(descr, descrlen)) != NULL) {
1290 ifr.ifr_buffer.buffer = descr;
1291 ifr.ifr_buffer.length = descrlen;
1292 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1293 if (ifr.ifr_buffer.buffer == descr) {
1294 if (strlen(descr) > 0)
1295 printf("\tdescription: %s\n",
1296 descr);
1297 } else if (ifr.ifr_buffer.length > descrlen) {
1298 descrlen = ifr.ifr_buffer.length;
1299 continue;
1300 }
1301 }
1302 } else
1303 warn("unable to allocate memory for interface"
1304 "description");
1305 break;
1306 }
1307
1308 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1309 if (ifr.ifr_curcap != 0) {
1310 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1311 putchar('\n');
1312 }
1313 if (supmedia && ifr.ifr_reqcap != 0) {
1314 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1315 putchar('\n');
1316 }
1317 }
1318
1319 tunnel_status(s);
1320
1321 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1322 if (ift->ifa_addr == NULL)
1323 continue;
1324 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1325 continue;
1326 if (allfamilies) {
1327 const struct afswtch *p;
1328 p = af_getbyfamily(ift->ifa_addr->sa_family);
1329 if (p != NULL && p->af_status != NULL)
1330 p->af_status(s, ift);
1331 } else if (afp->af_af == ift->ifa_addr->sa_family)
1332 afp->af_status(s, ift);
1333 }
1334 #if 0
1335 if (allfamilies || afp->af_af == AF_LINK) {
1336 const struct afswtch *lafp;
1337
1338 /*
1339 * Hack; the link level address is received separately
1340 * from the routing information so any address is not
1341 * handled above. Cobble together an entry and invoke
1342 * the status method specially.
1343 */
1344 lafp = af_getbyname("lladdr");
1345 if (lafp != NULL) {
1346 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1347 lafp->af_status(s, &info);
1348 }
1349 }
1350 #endif
1351 if (allfamilies)
1352 af_other_status(s);
1353 else if (afp->af_other_status != NULL)
1354 afp->af_other_status(s);
1355
1356 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1357 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1358 printf("%s", ifs.ascii);
1359
1360 if (verbose > 0)
1361 sfp_status(s, &ifr, verbose);
1362
1363 close(s);
1364 return;
1365 }
1366
1367 static void
tunnel_status(int s)1368 tunnel_status(int s)
1369 {
1370 af_all_tunnel_status(s);
1371 }
1372
1373 void
Perror(const char * cmd)1374 Perror(const char *cmd)
1375 {
1376 switch (errno) {
1377
1378 case ENXIO:
1379 errx(1, "%s: no such interface", cmd);
1380 break;
1381
1382 case EPERM:
1383 errx(1, "%s: permission denied", cmd);
1384 break;
1385
1386 default:
1387 err(1, "%s", cmd);
1388 }
1389 }
1390
1391 /*
1392 * Print a value a la the %b format of the kernel's printf
1393 */
1394 void
printb(const char * s,unsigned v,const char * bits)1395 printb(const char *s, unsigned v, const char *bits)
1396 {
1397 int i, any = 0;
1398 char c;
1399
1400 if (bits && *bits == 8)
1401 printf("%s=%o", s, v);
1402 else
1403 printf("%s=%x", s, v);
1404 if (bits) {
1405 bits++;
1406 putchar('<');
1407 while ((i = *bits++) != '\0') {
1408 if (v & (1 << (i-1))) {
1409 if (any)
1410 putchar(',');
1411 any = 1;
1412 for (; (c = *bits) > 32; bits++)
1413 putchar(c);
1414 } else
1415 for (; *bits > 32; bits++)
1416 ;
1417 }
1418 putchar('>');
1419 }
1420 }
1421
1422 void
print_vhid(const struct ifaddrs * ifa,const char * s)1423 print_vhid(const struct ifaddrs *ifa, const char *s)
1424 {
1425 struct if_data *ifd;
1426
1427 if (ifa->ifa_data == NULL)
1428 return;
1429
1430 ifd = ifa->ifa_data;
1431 if (ifd->ifi_vhid == 0)
1432 return;
1433
1434 printf(" vhid %d", ifd->ifi_vhid);
1435 }
1436
1437 void
ifmaybeload(const char * name)1438 ifmaybeload(const char *name)
1439 {
1440 #define MOD_PREFIX_LEN 3 /* "if_" */
1441 struct module_stat mstat;
1442 int i, fileid, modid;
1443 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1444 const char *cp;
1445 struct module_map_entry *mme;
1446 bool found;
1447
1448 /* loading suppressed by the user */
1449 if (noload)
1450 return;
1451
1452 /* trim the interface number off the end */
1453 strlcpy(ifname, name, sizeof(ifname));
1454 for (dp = ifname; *dp != 0; dp++)
1455 if (isdigit(*dp)) {
1456 *dp = 0;
1457 break;
1458 }
1459
1460 /* Either derive it from the map or guess otherwise */
1461 *ifkind = '\0';
1462 found = false;
1463 for (i = 0; i < nitems(module_map); ++i) {
1464 mme = &module_map[i];
1465 if (strcmp(mme->ifname, ifname) == 0) {
1466 strlcpy(ifkind, mme->kldname, sizeof(ifkind));
1467 found = true;
1468 break;
1469 }
1470 }
1471
1472 /* We didn't have an alias for it... we'll guess. */
1473 if (!found) {
1474 /* turn interface and unit into module name */
1475 strlcpy(ifkind, "if_", sizeof(ifkind));
1476 strlcat(ifkind, ifname, sizeof(ifkind));
1477 }
1478
1479 /* scan files in kernel */
1480 mstat.version = sizeof(struct module_stat);
1481 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1482 /* scan modules in file */
1483 for (modid = kldfirstmod(fileid); modid > 0;
1484 modid = modfnext(modid)) {
1485 if (modstat(modid, &mstat) < 0)
1486 continue;
1487 /* strip bus name if present */
1488 if ((cp = strchr(mstat.name, '/')) != NULL) {
1489 cp++;
1490 } else {
1491 cp = mstat.name;
1492 }
1493 /*
1494 * Is it already loaded? Don't compare with ifname if
1495 * we were specifically told which kld to use. Doing
1496 * so could lead to conflicts not trivially solved.
1497 */
1498 if ((!found && strcmp(ifname, cp) == 0) ||
1499 strcmp(ifkind, cp) == 0)
1500 return;
1501 }
1502 }
1503
1504 /*
1505 * Try to load the module. But ignore failures, because ifconfig can't
1506 * infer the names of all drivers (eg mlx4en(4)).
1507 */
1508 (void) kldload(ifkind);
1509 }
1510
1511 static struct cmd basic_cmds[] = {
1512 DEF_CMD("up", IFF_UP, setifflags),
1513 DEF_CMD("down", -IFF_UP, setifflags),
1514 DEF_CMD("arp", -IFF_NOARP, setifflags),
1515 DEF_CMD("-arp", IFF_NOARP, setifflags),
1516 DEF_CMD("debug", IFF_DEBUG, setifflags),
1517 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1518 DEF_CMD_ARG("description", setifdescr),
1519 DEF_CMD_ARG("descr", setifdescr),
1520 DEF_CMD("-description", 0, unsetifdescr),
1521 DEF_CMD("-descr", 0, unsetifdescr),
1522 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1523 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1524 DEF_CMD("add", IFF_UP, notealias),
1525 DEF_CMD("alias", IFF_UP, notealias),
1526 DEF_CMD("-alias", -IFF_UP, notealias),
1527 DEF_CMD("delete", -IFF_UP, notealias),
1528 DEF_CMD("remove", -IFF_UP, notealias),
1529 #ifdef notdef
1530 #define EN_SWABIPS 0x1000
1531 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1532 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1533 #endif
1534 DEF_CMD_ARG("netmask", setifnetmask),
1535 DEF_CMD_ARG("metric", setifmetric),
1536 DEF_CMD_ARG("broadcast", setifbroadaddr),
1537 DEF_CMD_ARG2("tunnel", settunnel),
1538 DEF_CMD("-tunnel", 0, deletetunnel),
1539 DEF_CMD("deletetunnel", 0, deletetunnel),
1540 #ifdef JAIL
1541 DEF_CMD_ARG("vnet", setifvnet),
1542 DEF_CMD_ARG("-vnet", setifrvnet),
1543 #endif
1544 DEF_CMD("link0", IFF_LINK0, setifflags),
1545 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1546 DEF_CMD("link1", IFF_LINK1, setifflags),
1547 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1548 DEF_CMD("link2", IFF_LINK2, setifflags),
1549 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1550 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1551 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1552 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1553 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1554 DEF_CMD("rxcsum6", IFCAP_RXCSUM_IPV6, setifcap),
1555 DEF_CMD("-rxcsum6", -IFCAP_RXCSUM_IPV6, setifcap),
1556 DEF_CMD("txcsum6", IFCAP_TXCSUM_IPV6, setifcap),
1557 DEF_CMD("-txcsum6", -IFCAP_TXCSUM_IPV6, setifcap),
1558 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1559 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1560 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1561 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1562 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1563 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1564 DEF_CMD_ARG("pcp", setifpcp),
1565 DEF_CMD("-pcp", 0, disableifpcp),
1566 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1567 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1568 DEF_CMD("tso6", IFCAP_TSO6, setifcap),
1569 DEF_CMD("-tso6", -IFCAP_TSO6, setifcap),
1570 DEF_CMD("tso4", IFCAP_TSO4, setifcap),
1571 DEF_CMD("-tso4", -IFCAP_TSO4, setifcap),
1572 DEF_CMD("tso", IFCAP_TSO, setifcap),
1573 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1574 DEF_CMD("toe", IFCAP_TOE, setifcap),
1575 DEF_CMD("-toe", -IFCAP_TOE, setifcap),
1576 DEF_CMD("lro", IFCAP_LRO, setifcap),
1577 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1578 DEF_CMD("wol", IFCAP_WOL, setifcap),
1579 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1580 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1581 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1582 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1583 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1584 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1585 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1586 DEF_CMD("txrtlmt", IFCAP_TXRTLMT, setifcap),
1587 DEF_CMD("-txrtlmt", -IFCAP_TXRTLMT, setifcap),
1588 DEF_CMD("hwrxtstmp", IFCAP_HWRXTSTMP, setifcap),
1589 DEF_CMD("-hwrxtstmp", -IFCAP_HWRXTSTMP, setifcap),
1590 DEF_CMD("normal", -IFF_LINK0, setifflags),
1591 DEF_CMD("compress", IFF_LINK0, setifflags),
1592 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1593 DEF_CMD_ARG("mtu", setifmtu),
1594 DEF_CMD_ARG("name", setifname),
1595 };
1596
1597 static __constructor void
ifconfig_ctor(void)1598 ifconfig_ctor(void)
1599 {
1600 size_t i;
1601
1602 for (i = 0; i < nitems(basic_cmds); i++)
1603 cmd_register(&basic_cmds[i]);
1604 }
1605