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