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