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