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