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