]> git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/ifconfig.c
33af6fbe20e7d3a551d08b1cf1b820a672dfc475
[apple/network_cmds.git] / ifconfig.tproj / ifconfig.c
1 /*
2 * Copyright (c) 2009-2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * Copyright (c) 1983, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58 #include <sys/cdefs.h>
59
60 #ifndef lint
61 __unused static const char copyright[] =
62 "@(#) Copyright (c) 1983, 1993\n\
63 The Regents of the University of California. All rights reserved.\n";
64 #endif /* not lint */
65
66 #include <sys/param.h>
67 #include <sys/ioctl.h>
68 #include <sys/socket.h>
69 #include <sys/sysctl.h>
70 #include <sys/time.h>
71 #ifndef __APPLE__
72 #include <sys/module.h>
73 #include <sys/linker.h>
74 #endif
75
76 #include <net/ethernet.h>
77 #include <net/if.h>
78 #include <net/if_var.h>
79 #include <net/if_dl.h>
80 #include <net/if_types.h>
81 #include <net/if_mib.h>
82 #include <net/route.h>
83 #include <net/pktsched/pktsched.h>
84 #include <net/network_agent.h>
85
86 /* IP */
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <arpa/inet.h>
90 #include <netdb.h>
91
92 #include <ifaddrs.h>
93 #include <ctype.h>
94 #include <err.h>
95 #include <errno.h>
96 #include <fcntl.h>
97 #include <stdio.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include <unistd.h>
101 #include <sysexits.h>
102
103 #include "ifconfig.h"
104
105 /*
106 * Since "struct ifreq" is composed of various union members, callers
107 * should pay special attention to interprete the value.
108 * (.e.g. little/big endian difference in the structure.)
109 */
110 struct ifreq ifr;
111
112 char name[IFNAMSIZ];
113 int setaddr;
114 int setmask;
115 int doalias;
116 int clearaddr;
117 int newaddr = 1;
118 int noload;
119 int all;
120
121 int bond_details = 0;
122 int supmedia = 0;
123 #if TARGET_OS_EMBEDDED
124 int verbose = 1;
125 int showrtref = 1;
126 #else /* !TARGET_OS_EMBEDDED */
127 int verbose = 0;
128 int showrtref = 0;
129 #endif /* !TARGET_OS_EMBEDDED */
130 int printkeys = 0; /* Print keying material for interfaces. */
131
132 static int ifconfig(int argc, char *const *argv, int iscreate,
133 const struct afswtch *afp);
134 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
135 struct ifaddrs *ifa);
136 static char *bytes_to_str(unsigned long long bytes);
137 static char *bps_to_str(unsigned long long rate);
138 static char *ns_to_str(unsigned long long nsec);
139 static void tunnel_status(int s);
140 static void usage(void);
141 static char *sched2str(unsigned int s);
142 static char *tl2str(unsigned int s);
143 static char *ift2str(unsigned int t, unsigned int f, unsigned int sf);
144
145 static struct afswtch *af_getbyname(const char *name);
146 static struct afswtch *af_getbyfamily(int af);
147 static void af_other_status(int);
148
149 static struct option *opts = NULL;
150
151 void
152 opt_register(struct option *p)
153 {
154 p->next = opts;
155 opts = p;
156 }
157
158 static void
159 usage(void)
160 {
161 char options[1024];
162 struct option *p;
163
164 /* XXX not right but close enough for now */
165 options[0] = '\0';
166 for (p = opts; p != NULL; p = p->next) {
167 strlcat(options, p->opt_usage, sizeof(options));
168 strlcat(options, " ", sizeof(options));
169 }
170
171 fprintf(stderr,
172 "usage: ifconfig %sinterface address_family [address [dest_address]]\n"
173 " [parameters]\n"
174 " ifconfig interface create\n"
175 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
176 " ifconfig -l [-d] [-u] [address_family]\n"
177 " ifconfig %s[-d] [-m] [-u] [-v]\n",
178 options, options, options);
179 exit(1);
180 }
181
182 int
183 main(int argc, char *argv[])
184 {
185 int c, namesonly, downonly, uponly;
186 const struct afswtch *afp = NULL;
187 int ifindex;
188 struct ifaddrs *ifap, *ifa;
189 struct ifreq paifr;
190 const struct sockaddr_dl *sdl;
191 char options[1024], *cp;
192 const char *ifname;
193 struct option *p;
194 size_t iflen;
195
196 all = downonly = uponly = namesonly = noload = 0;
197
198 /* Parse leading line options */
199 #ifndef __APPLE__
200 strlcpy(options, "adklmnuv", sizeof(options));
201 #else
202 strlcpy(options, "abdlmruv", sizeof(options));
203 #endif
204 for (p = opts; p != NULL; p = p->next)
205 strlcat(options, p->opt, sizeof(options));
206 while ((c = getopt(argc, argv, options)) != -1) {
207 switch (c) {
208 case 'a': /* scan all interfaces */
209 all++;
210 break;
211 case 'b': /* bond detailed output */
212 bond_details++;
213 break;
214 case 'd': /* restrict scan to "down" interfaces */
215 downonly++;
216 break;
217 #ifndef __APPLE__
218 case 'k':
219 printkeys++;
220 break;
221 #endif
222 case 'l': /* scan interface names only */
223 namesonly++;
224 break;
225 case 'm': /* show media choices in status */
226 supmedia = 1;
227 break;
228 #ifndef __APPLE__
229 case 'n': /* suppress module loading */
230 noload++;
231 break;
232 #endif
233 case 'r':
234 showrtref++;
235 break;
236 case 'u': /* restrict scan to "up" interfaces */
237 uponly++;
238 break;
239 case 'v':
240 verbose++;
241 break;
242 default:
243 for (p = opts; p != NULL; p = p->next)
244 if (p->opt[0] == c) {
245 p->cb(optarg);
246 break;
247 }
248 if (p == NULL)
249 usage();
250 break;
251 }
252 }
253 argc -= optind;
254 argv += optind;
255
256 /* -l cannot be used with -a or -q or -m or -b */
257 if (namesonly &&
258 (all || supmedia || bond_details))
259 usage();
260
261 /* nonsense.. */
262 if (uponly && downonly)
263 usage();
264
265 /* no arguments is equivalent to '-a' */
266 if (!namesonly && argc < 1)
267 all = 1;
268
269 /* -a and -l allow an address family arg to limit the output */
270 if (all || namesonly) {
271 if (argc > 1)
272 usage();
273
274 ifname = NULL;
275 if (argc == 1) {
276 afp = af_getbyname(*argv);
277 if (afp == NULL)
278 usage();
279 if (afp->af_name != NULL)
280 argc--, argv++;
281 /* leave with afp non-zero */
282 }
283 } else {
284 /* not listing, need an argument */
285 if (argc < 1)
286 usage();
287
288 ifname = *argv;
289 argc--, argv++;
290
291 #ifdef notdef
292 /* check and maybe load support for this interface */
293 ifmaybeload(ifname);
294 #endif
295 ifindex = if_nametoindex(ifname);
296 if (ifindex == 0) {
297 /*
298 * NOTE: We must special-case the `create' command
299 * right here as we would otherwise fail when trying
300 * to find the interface.
301 */
302 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
303 strcmp(argv[0], "plumb") == 0)) {
304 iflen = strlcpy(name, ifname, sizeof(name));
305 if (iflen >= sizeof(name))
306 errx(1, "%s: cloning name too long",
307 ifname);
308 ifconfig(argc, argv, 1, NULL);
309 exit(0);
310 }
311 errx(1, "interface %s does not exist", ifname);
312 }
313 }
314
315 /* Check for address family */
316 if (argc > 0) {
317 afp = af_getbyname(*argv);
318 if (afp != NULL)
319 argc--, argv++;
320 }
321
322 if (getifaddrs(&ifap) != 0)
323 err(EXIT_FAILURE, "getifaddrs");
324 cp = NULL;
325 ifindex = 0;
326 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
327 memset(&paifr, 0, sizeof(paifr));
328 strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
329 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
330 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
331 ifa->ifa_addr->sa_len);
332 }
333
334 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
335 continue;
336 if (ifa->ifa_addr->sa_family == AF_LINK)
337 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
338 else
339 sdl = NULL;
340 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0)
341 continue;
342 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
343 if (iflen >= sizeof(name)) {
344 warnx("%s: interface name too long, skipping",
345 ifa->ifa_name);
346 continue;
347 }
348 cp = ifa->ifa_name;
349
350 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
351 continue;
352 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
353 continue;
354 ifindex++;
355 /*
356 * Are we just listing the interfaces?
357 */
358 if (namesonly) {
359 if (ifindex > 1)
360 printf(" ");
361 fputs(name, stdout);
362 continue;
363 }
364
365 if (argc > 0)
366 ifconfig(argc, argv, 0, afp);
367 else
368 status(afp, sdl, ifa);
369 }
370 if (namesonly)
371 printf("\n");
372 freeifaddrs(ifap);
373
374 exit(0);
375 }
376
377 static struct afswtch *afs = NULL;
378
379 void
380 af_register(struct afswtch *p)
381 {
382 p->af_next = afs;
383 afs = p;
384 }
385
386 static struct afswtch *
387 af_getbyname(const char *name)
388 {
389 struct afswtch *afp;
390
391 for (afp = afs; afp != NULL; afp = afp->af_next)
392 if (strcmp(afp->af_name, name) == 0)
393 return afp;
394 return NULL;
395 }
396
397 static struct afswtch *
398 af_getbyfamily(int af)
399 {
400 struct afswtch *afp;
401
402 for (afp = afs; afp != NULL; afp = afp->af_next)
403 if (afp->af_af == af)
404 return afp;
405 return NULL;
406 }
407
408 static void
409 af_other_status(int s)
410 {
411 struct afswtch *afp;
412 uint8_t afmask[howmany(AF_MAX, NBBY)];
413
414 memset(afmask, 0, sizeof(afmask));
415 for (afp = afs; afp != NULL; afp = afp->af_next) {
416 if (afp->af_other_status == NULL)
417 continue;
418 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
419 continue;
420 afp->af_other_status(s);
421 setbit(afmask, afp->af_af);
422 }
423 }
424
425 static void
426 af_all_tunnel_status(int s)
427 {
428 struct afswtch *afp;
429 uint8_t afmask[howmany(AF_MAX, NBBY)];
430
431 memset(afmask, 0, sizeof(afmask));
432 for (afp = afs; afp != NULL; afp = afp->af_next) {
433 if (afp->af_status_tunnel == NULL)
434 continue;
435 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
436 continue;
437 afp->af_status_tunnel(s);
438 setbit(afmask, afp->af_af);
439 }
440 }
441
442 static struct cmd *cmds = NULL;
443
444 void
445 cmd_register(struct cmd *p)
446 {
447 p->c_next = cmds;
448 cmds = p;
449 }
450
451 static const struct cmd *
452 cmd_lookup(const char *name)
453 {
454 #define N(a) (sizeof(a)/sizeof(a[0]))
455 const struct cmd *p;
456
457 for (p = cmds; p != NULL; p = p->c_next)
458 if (strcmp(name, p->c_name) == 0)
459 return p;
460 return NULL;
461 #undef N
462 }
463
464 struct callback {
465 callback_func *cb_func;
466 void *cb_arg;
467 struct callback *cb_next;
468 };
469 static struct callback *callbacks = NULL;
470
471 void
472 callback_register(callback_func *func, void *arg)
473 {
474 struct callback *cb;
475
476 cb = malloc(sizeof(struct callback));
477 if (cb == NULL)
478 errx(1, "unable to allocate memory for callback");
479 cb->cb_func = func;
480 cb->cb_arg = arg;
481 cb->cb_next = callbacks;
482 callbacks = cb;
483 }
484
485 /* specially-handled commands */
486 static void setifaddr(const char *, int, int, const struct afswtch *);
487 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
488
489 static void setifdstaddr(const char *, int, int, const struct afswtch *);
490 static const struct cmd setifdstaddr_cmd =
491 DEF_CMD("ifdstaddr", 0, setifdstaddr);
492
493 static int
494 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *afp)
495 {
496 const struct afswtch *nafp;
497 struct callback *cb;
498 int s;
499
500 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
501 top:
502 if (afp == NULL)
503 afp = af_getbyname("inet");
504 ifr.ifr_addr.sa_family =
505 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
506 AF_INET : afp->af_af;
507
508 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
509 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
510
511 while (argc > 0) {
512 const struct cmd *p;
513
514 p = cmd_lookup(*argv);
515 if (p == NULL) {
516 /*
517 * Not a recognized command, choose between setting
518 * the interface address and the dst address.
519 */
520 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
521 }
522 if (p->c_u.c_func || p->c_u.c_func2) {
523 if (iscreate && !p->c_iscloneop) {
524 /*
525 * Push the clone create callback so the new
526 * device is created and can be used for any
527 * remaining arguments.
528 */
529 cb = callbacks;
530 if (cb == NULL)
531 errx(1, "internal error, no callback");
532 callbacks = cb->cb_next;
533 cb->cb_func(s, cb->cb_arg);
534 iscreate = 0;
535 /*
536 * Handle any address family spec that
537 * immediately follows and potentially
538 * recreate the socket.
539 */
540 nafp = af_getbyname(*argv);
541 if (nafp != NULL) {
542 argc--, argv++;
543 if (nafp != afp) {
544 close(s);
545 afp = nafp;
546 goto top;
547 }
548 }
549 }
550 if (p->c_parameter == NEXTARG) {
551 if (argv[1] == NULL)
552 errx(1, "'%s' requires argument",
553 p->c_name);
554 p->c_u.c_func(argv[1], 0, s, afp);
555 argc--, argv++;
556 } else if (p->c_parameter == OPTARG) {
557 p->c_u.c_func(argv[1], 0, s, afp);
558 if (argv[1] != NULL)
559 argc--, argv++;
560 } else if (p->c_parameter == NEXTARG2) {
561 if (argc < 3)
562 errx(1, "'%s' requires 2 arguments",
563 p->c_name);
564 p->c_u.c_func2(argv[1], argv[2], s, afp);
565 argc -= 2, argv += 2;
566 } else
567 p->c_u.c_func(*argv, p->c_parameter, s, afp);
568 }
569 argc--, argv++;
570 }
571
572 /*
573 * Do any post argument processing required by the address family.
574 */
575 if (afp->af_postproc != NULL)
576 afp->af_postproc(s, afp);
577 /*
578 * Do deferred callbacks registered while processing
579 * command-line arguments.
580 */
581 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
582 cb->cb_func(s, cb->cb_arg);
583 /*
584 * Do deferred operations.
585 */
586 if (clearaddr) {
587 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
588 warnx("interface %s cannot change %s addresses!",
589 name, afp->af_name);
590 clearaddr = 0;
591 }
592 }
593 if (clearaddr) {
594 int ret;
595 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
596 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
597 if (ret < 0) {
598 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
599 /* means no previous address for interface */
600 } else
601 Perror("ioctl (SIOCDIFADDR)");
602 }
603 }
604 if (newaddr) {
605 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
606 warnx("interface %s cannot change %s addresses!",
607 name, afp->af_name);
608 newaddr = 0;
609 }
610 }
611 if (newaddr && (setaddr || setmask)) {
612 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
613 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
614 Perror("ioctl (SIOCAIFADDR)");
615 }
616
617 close(s);
618 return(0);
619 }
620
621 /*ARGSUSED*/
622 static void
623 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
624 {
625 if (afp->af_getaddr == NULL)
626 return;
627 /*
628 * Delay the ioctl to set the interface addr until flags are all set.
629 * The address interpretation may depend on the flags,
630 * and the flags may change when the address is set.
631 */
632 setaddr++;
633 if (doalias == 0 && afp->af_af != AF_LINK)
634 clearaddr = 1;
635 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
636 }
637
638 static void
639 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
640 {
641 struct addrinfo *srcres, *dstres;
642 int ecode;
643
644 if (afp->af_settunnel == NULL) {
645 warn("address family %s does not support tunnel setup",
646 afp->af_name);
647 return;
648 }
649
650 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
651 errx(1, "error in parsing address string: %s",
652 gai_strerror(ecode));
653
654 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
655 errx(1, "error in parsing address string: %s",
656 gai_strerror(ecode));
657
658 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
659 errx(1,
660 "source and destination address families do not match");
661
662 afp->af_settunnel(s, srcres, dstres);
663
664 freeaddrinfo(srcres);
665 freeaddrinfo(dstres);
666 }
667
668 /* ARGSUSED */
669 static void
670 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
671 {
672
673 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
674 err(1, "SIOCDIFPHYADDR");
675 }
676
677 static void
678 setifnetmask(const char *addr, int dummy __unused, int s,
679 const struct afswtch *afp)
680 {
681 if (afp->af_getaddr != NULL) {
682 setmask++;
683 afp->af_getaddr(addr, MASK);
684 }
685 }
686
687 static void
688 setifbroadaddr(const char *addr, int dummy __unused, int s,
689 const struct afswtch *afp)
690 {
691 if (afp->af_getaddr != NULL)
692 afp->af_getaddr(addr, DSTADDR);
693 }
694
695 static void
696 setifipdst(const char *addr, int dummy __unused, int s,
697 const struct afswtch *afp)
698 {
699 const struct afswtch *inet;
700
701 inet = af_getbyname("inet");
702 if (inet == NULL)
703 return;
704 inet->af_getaddr(addr, DSTADDR);
705 clearaddr = 0;
706 newaddr = 0;
707 }
708
709 static void
710 notealias(const char *addr, int param, int s, const struct afswtch *afp)
711 {
712 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
713 if (setaddr && doalias == 0 && param < 0)
714 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
715 bcopy((caddr_t)rqtosa(af_addreq),
716 (caddr_t)rqtosa(af_ridreq),
717 rqtosa(af_addreq)->sa_len);
718 doalias = param;
719 if (param < 0) {
720 clearaddr = 1;
721 newaddr = 0;
722 } else
723 clearaddr = 0;
724 #undef rqtosa
725 }
726
727 /*ARGSUSED*/
728 static void
729 setifdstaddr(const char *addr, int param __unused, int s,
730 const struct afswtch *afp)
731 {
732 if (afp->af_getaddr != NULL)
733 afp->af_getaddr(addr, DSTADDR);
734 }
735
736 /*
737 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
738 * of the ifreq structure, which may confuse other parts of ifconfig.
739 * Make a private copy so we can avoid that.
740 */
741 static void
742 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
743 {
744 struct ifreq my_ifr;
745 int flags;
746
747 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
748
749 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
750 Perror("ioctl (SIOCGIFFLAGS)");
751 exit(1);
752 }
753 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
754 flags = my_ifr.ifr_flags;
755
756 if (value < 0) {
757 value = -value;
758 flags &= ~value;
759 } else
760 flags |= value;
761 my_ifr.ifr_flags = flags & 0xffff;
762 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
763 Perror(vname);
764 }
765
766 void
767 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
768 {
769 int flags;
770
771 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
772 Perror("ioctl (SIOCGIFCAP)");
773 exit(1);
774 }
775 flags = ifr.ifr_curcap;
776 if (value < 0) {
777 value = -value;
778 flags &= ~value;
779 } else
780 flags |= value;
781 flags &= ifr.ifr_reqcap;
782 ifr.ifr_reqcap = flags;
783 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
784 Perror(vname);
785 }
786
787 static void
788 setifmetric(const char *val, int dummy __unused, int s,
789 const struct afswtch *afp)
790 {
791 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
792 ifr.ifr_metric = atoi(val);
793 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
794 warn("ioctl (set metric)");
795 }
796
797 static void
798 setifmtu(const char *val, int dummy __unused, int s,
799 const struct afswtch *afp)
800 {
801 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
802 ifr.ifr_mtu = atoi(val);
803 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
804 warn("ioctl (set mtu)");
805 }
806
807 #ifndef __APPLE__
808 static void
809 setifname(const char *val, int dummy __unused, int s,
810 const struct afswtch *afp)
811 {
812 char *newname;
813
814 newname = strdup(val);
815 if (newname == NULL) {
816 warn("no memory to set ifname");
817 return;
818 }
819 ifr.ifr_data = newname;
820 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
821 warn("ioctl (set name)");
822 free(newname);
823 return;
824 }
825 strlcpy(name, newname, sizeof(name));
826 free(newname);
827 }
828 #endif
829
830 static void
831 setrouter(const char *vname, int value, int s, const struct afswtch *afp)
832 {
833 if (afp->af_setrouter == NULL) {
834 warn("address family %s does not support router mode",
835 afp->af_name);
836 return;
837 }
838
839 afp->af_setrouter(s, value);
840 }
841
842 static void
843 setifdesc(const char *val, int dummy __unused, int s, const struct afswtch *afp)
844 {
845 struct if_descreq ifdr;
846
847 bzero(&ifdr, sizeof (ifdr));
848 strncpy(ifdr.ifdr_name, name, sizeof (ifdr.ifdr_name));
849 ifdr.ifdr_len = strlen(val);
850 strncpy((char *)ifdr.ifdr_desc, val, sizeof (ifdr.ifdr_desc));
851
852 if (ioctl(s, SIOCSIFDESC, (caddr_t)&ifdr) < 0) {
853 warn("ioctl (set desc)");
854 }
855 }
856
857 static void
858 settbr(const char *val, int dummy __unused, int s, const struct afswtch *afp)
859 {
860 struct if_linkparamsreq iflpr;
861 long double bps;
862 u_int64_t rate;
863 u_int32_t percent = 0;
864 char *cp;
865
866 errno = 0;
867 bzero(&iflpr, sizeof (iflpr));
868 strncpy(iflpr.iflpr_name, name, sizeof (iflpr.iflpr_name));
869
870 bps = strtold(val, &cp);
871 if (val == cp || errno != 0) {
872 warn("Invalid value '%s'", val);
873 return;
874 }
875 rate = (u_int64_t)bps;
876 if (cp != NULL) {
877 if (!strcmp(cp, "b") || !strcmp(cp, "bps")) {
878 ; /* nothing */
879 } else if (!strcmp(cp, "Kb") || !strcmp(cp, "Kbps")) {
880 rate *= 1000;
881 } else if (!strcmp(cp, "Mb") || !strcmp(cp, "Mbps")) {
882 rate *= 1000 * 1000;
883 } else if (!strcmp(cp, "Gb") || !strcmp(cp, "Gbps")) {
884 rate *= 1000 * 1000 * 1000;
885 } else if (!strcmp(cp, "%")) {
886 percent = rate;
887 if (percent == 0 || percent > 100) {
888 printf("Value out of range '%s'", val);
889 return;
890 }
891 } else if (*cp != '\0') {
892 printf("Unknown unit '%s'", cp);
893 return;
894 }
895 }
896 iflpr.iflpr_output_tbr_rate = rate;
897 iflpr.iflpr_output_tbr_percent = percent;
898 if (ioctl(s, SIOCSIFLINKPARAMS, &iflpr) < 0 &&
899 errno != ENOENT && errno != ENXIO && errno != ENODEV) {
900 warn("ioctl (set link params)");
901 } else if (errno == ENXIO) {
902 printf("TBR cannot be set on %s\n", name);
903 } else if (errno == ENOENT || rate == 0) {
904 printf("%s: TBR is now disabled\n", name);
905 } else if (errno == ENODEV) {
906 printf("%s: requires absolute TBR rate\n", name);
907 } else if (percent != 0) {
908 printf("%s: TBR rate set to %u%% of effective link rate\n",
909 name, percent);
910 } else {
911 printf("%s: TBR rate set to %s\n", name, bps_to_str(rate));
912 }
913 }
914
915 static void
916 setthrottle(const char *val, int dummy __unused, int s,
917 const struct afswtch *afp)
918 {
919 struct if_throttlereq iftr;
920 char *cp;
921
922 errno = 0;
923 bzero(&iftr, sizeof (iftr));
924 strncpy(iftr.ifthr_name, name, sizeof (iftr.ifthr_name));
925
926 iftr.ifthr_level = strtold(val, &cp);
927 if (val == cp || errno != 0) {
928 warn("Invalid value '%s'", val);
929 return;
930 }
931
932 if (ioctl(s, SIOCSIFTHROTTLE, &iftr) < 0 && errno != ENXIO) {
933 warn("ioctl (set throttling level)");
934 } else if (errno == ENXIO) {
935 printf("throttling level cannot be set on %s\n", name);
936 } else {
937 printf("%s: throttling level set to %d\n", name,
938 iftr.ifthr_level);
939 }
940 }
941
942 static void
943 setdisableoutput(const char *val, int dummy __unused, int s,
944 const struct afswtch *afp)
945 {
946 struct ifreq ifr;
947 char *cp;
948 errno = 0;
949 bzero(&ifr, sizeof (ifr));
950 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
951
952 ifr.ifr_ifru.ifru_disable_output = strtold(val, &cp);
953 if (val == cp || errno != 0) {
954 warn("Invalid value '%s'", val);
955 return;
956 }
957
958 if (ioctl(s, SIOCSIFDISABLEOUTPUT, &ifr) < 0 && errno != ENXIO) {
959 warn("ioctl set disable output");
960 } else if (errno == ENXIO) {
961 printf("output thread can not be disabled on %s\n", name);
962 } else {
963 printf("output %s on %s\n",
964 ((ifr.ifr_ifru.ifru_disable_output == 0) ? "enabled" : "disabled"),
965 name);
966 }
967 }
968
969 static void
970 setlog(const char *val, int dummy __unused, int s,
971 const struct afswtch *afp)
972 {
973 char *cp;
974
975 errno = 0;
976 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
977
978 ifr.ifr_log.ifl_level = strtold(val, &cp);
979 if (val == cp || errno != 0) {
980 warn("Invalid value '%s'", val);
981 return;
982 }
983 ifr.ifr_log.ifl_flags = (IFRLOGF_DLIL|IFRLOGF_FAMILY|IFRLOGF_DRIVER|
984 IFRLOGF_FIRMWARE);
985
986 if (ioctl(s, SIOCSIFLOG, &ifr) < 0)
987 warn("ioctl (set logging parameters)");
988 }
989
990 void
991 setcl2k(const char *vname, int value, int s, const struct afswtch *afp)
992 {
993 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
994 ifr.ifr_ifru.ifru_2kcl = value;
995
996 if (ioctl(s, SIOCSIF2KCL, (caddr_t)&ifr) < 0)
997 Perror(vname);
998 }
999
1000 void
1001 setexpensive(const char *vname, int value, int s, const struct afswtch *afp)
1002 {
1003 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1004 ifr.ifr_ifru.ifru_expensive = value;
1005
1006 if (ioctl(s, SIOCSIFEXPENSIVE, (caddr_t)&ifr) < 0)
1007 Perror(vname);
1008 }
1009
1010 void
1011 settimestamp(const char *vname, int value, int s, const struct afswtch *afp)
1012 {
1013 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1014
1015 if (value == 0) {
1016 if (ioctl(s, SIOCSIFTIMESTAMPDISABLE, (caddr_t)&ifr) < 0)
1017 Perror(vname);
1018 } else {
1019 if (ioctl(s, SIOCSIFTIMESTAMPENABLE, (caddr_t)&ifr) < 0)
1020 Perror(vname);
1021 }
1022 }
1023
1024 void
1025 setecnmode(const char *val, int dummy __unused, int s,
1026 const struct afswtch *afp)
1027 {
1028 char *cp;
1029
1030 if (strcmp(val, "default") == 0)
1031 ifr.ifr_ifru.ifru_ecn_mode = IFRTYPE_ECN_DEFAULT;
1032 else if (strcmp(val, "enable") == 0)
1033 ifr.ifr_ifru.ifru_ecn_mode = IFRTYPE_ECN_ENABLE;
1034 else if (strcmp(val, "disable") == 0)
1035 ifr.ifr_ifru.ifru_ecn_mode = IFRTYPE_ECN_DISABLE;
1036 else {
1037 ifr.ifr_ifru.ifru_ecn_mode = strtold(val, &cp);
1038 if (val == cp || errno != 0) {
1039 warn("Invalid ECN mode value '%s'", val);
1040 return;
1041 }
1042 }
1043
1044 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1045
1046 if (ioctl(s, SIOCSECNMODE, (caddr_t)&ifr) < 0)
1047 Perror("ioctl(SIOCSECNMODE)");
1048 }
1049
1050 #if defined(SIOCSQOSMARKINGMODE) && defined(SIOCSQOSMARKINGENABLED)
1051
1052 void
1053 setqosmarking(const char *cmd, const char *arg, int s, const struct afswtch *afp)
1054 {
1055 u_long ioc;
1056
1057 #if (DEBUG | DEVELOPMENT)
1058 printf("%s(%s, %s)\n", __func__, cmd, arg);
1059 #endif /* (DEBUG | DEVELOPMENT) */
1060
1061 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1062
1063 if (strcmp(cmd, "mode") == 0) {
1064 ioc = SIOCSQOSMARKINGMODE;
1065
1066 if (strcmp(arg, "fastlane") == 0)
1067 ifr.ifr_qosmarking_mode = IFRTYPE_QOSMARKING_FASTLANE;
1068 else if (strcasecmp(arg, "none") == 0 || strcasecmp(arg, "off") == 0)
1069 ifr.ifr_qosmarking_mode = IFRTYPE_QOSMARKING_MODE_NONE;
1070 else
1071 err(EX_USAGE, "bad value for qosmarking mode: %s", arg);
1072 } else if (strcmp(cmd, "enabled") == 0) {
1073 ioc = SIOCSQOSMARKINGENABLED;
1074 if (strcmp(arg, "1") == 0 || strcasecmp(arg, "on") == 0||
1075 strcasecmp(arg, "yes") == 0 || strcasecmp(arg, "true") == 0)
1076 ifr.ifr_qosmarking_enabled = 1;
1077 else if (strcmp(arg, "0") == 0 || strcasecmp(arg, "off") == 0||
1078 strcasecmp(arg, "no") == 0 || strcasecmp(arg, "false") == 0)
1079 ifr.ifr_qosmarking_enabled = 0;
1080 else
1081 err(EX_USAGE, "bad value for qosmarking enabled: %s", arg);
1082 } else {
1083 err(EX_USAGE, "qosmarking takes mode or enabled");
1084 }
1085
1086 if (ioctl(s, ioc, (caddr_t)&ifr) < 0)
1087 err(EX_OSERR, "ioctl(%s, %s)", cmd, arg);
1088 }
1089
1090 void
1091 setfastlane(const char *cmd, const char *arg, int s, const struct afswtch *afp)
1092 {
1093 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1094
1095 warnx("### fastlane is obsolete, use qosmarking ###");
1096
1097 if (strcmp(cmd, "capable") == 0) {
1098 if (strcmp(arg, "1") == 0 || strcasecmp(arg, "on") == 0||
1099 strcasecmp(arg, "yes") == 0 || strcasecmp(arg, "true") == 0)
1100 setqosmarking("mode", "fastlane", s, afp);
1101 else if (strcmp(arg, "0") == 0 || strcasecmp(arg, "off") == 0||
1102 strcasecmp(arg, "no") == 0 || strcasecmp(arg, "false") == 0)
1103 setqosmarking("mode", "off", s, afp);
1104 else
1105 err(EX_USAGE, "bad value for fastlane %s", cmd);
1106 } else if (strcmp(cmd, "enable") == 0) {
1107 if (strcmp(arg, "1") == 0 || strcasecmp(arg, "on") == 0||
1108 strcasecmp(arg, "yes") == 0 || strcasecmp(arg, "true") == 0)
1109 setqosmarking("enabled", "1", s, afp);
1110 else if (strcmp(arg, "0") == 0 || strcasecmp(arg, "off") == 0||
1111 strcasecmp(arg, "no") == 0 || strcasecmp(arg, "false") == 0)
1112 setqosmarking("enabled", "0", s, afp);
1113 else
1114 err(EX_USAGE, "bad value for fastlane %s", cmd);
1115 } else {
1116 err(EX_USAGE, "fastlane takes capable or enable");
1117 }
1118 }
1119
1120 #else /* defined(SIOCSQOSMARKINGMODE) && defined(SIOCSQOSMARKINGENABLED) */
1121
1122 void
1123 setfastlane(const char *cmd, const char *arg, int s, const struct afswtch *afp)
1124 {
1125 int value;
1126 u_long ioc;
1127
1128 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1129
1130 if (strcmp(cmd, "capable") == 0)
1131 ioc = SIOCSFASTLANECAPABLE;
1132 else if (strcmp(cmd, "enable") == 0)
1133 ioc = SIOCSFASTLEENABLED;
1134 else
1135 err(EX_USAGE, "fastlane takes capable or enabled");
1136
1137 if (strcmp(arg, "1") == 0 || strcasecmp(arg, "on") == 0||
1138 strcasecmp(arg, "yes") == 0 || strcasecmp(arg, "true") == 0)
1139 value = 1;
1140 else if (strcmp(arg, "0") == 0 || strcasecmp(arg, "off") == 0||
1141 strcasecmp(arg, "no") == 0 || strcasecmp(arg, "false") == 0)
1142 value = 0;
1143 else
1144 err(EX_USAGE, "bad value for fastlane %s", cmd);
1145
1146 if (ioc == SIOCSFASTLANECAPABLE)
1147 ifr.ifr_fastlane_capable = value;
1148 else
1149 ifr.ifr_fastlane_enabled = value;
1150
1151 if (ioctl(s, ioc, (caddr_t)&ifr) < 0)
1152 err(EX_OSERR, "ioctl(%s, %s)", cmd, arg);
1153 }
1154
1155
1156 void
1157 setqosmarking(const char *cmd, const char *arg, int s, const struct afswtch *afp)
1158 {
1159 if (strcmp(cmd, "mode") == 0) {
1160 if (strcmp(arg, "fastlane") == 0)
1161 setfastlane("capable", "on", s, afp);
1162 else if (strcmp(arg, "none") == 0)
1163 setfastlane("capable", "off", s, afp);
1164 else
1165 err(EX_USAGE, "bad value for qosmarking mode: %s", arg);
1166 } else if (strcmp(cmd, "enabled") == 0) {
1167 if (strcmp(arg, "1") == 0 || strcasecmp(arg, "on") == 0||
1168 strcasecmp(arg, "yes") == 0 || strcasecmp(arg, "true") == 0)
1169 setfastlane("enable", "on", s, afp);
1170 else if (strcmp(arg, "0") == 0 || strcasecmp(arg, "off") == 0||
1171 strcasecmp(arg, "no") == 0 || strcasecmp(arg, "false") == 0)
1172 setfastlane("enable", "off", s, afp);
1173 else
1174 err(EX_USAGE, "bad value for qosmarking enabled: %s", arg);
1175 } else {
1176 err(EX_USAGE, "qosmarking takes mode or enabled");
1177 }
1178 }
1179
1180 #endif /* defined(SIOCSQOSMARKINGMODE) && defined(SIOCSQOSMARKINGENABLED) */
1181
1182 #define IFFBITS \
1183 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1184 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1185 "\20MULTICAST"
1186
1187 #define IFEFBITS \
1188 "\020\1AUTOCONFIGURING\5FASTLN_CAP\6IPV6_DISABLED\7ACCEPT_RTADV\10TXSTART\11RXPOLL" \
1189 "\12VLAN\13BOND\14ARPLL\15NOWINDOWSCALE\16NOAUTOIPV6LL\17EXPENSIVE\20ROUTER4" \
1190 "\21ROUTER6\22LOCALNET_PRIVATE\23ND6ALT\24RESTRICTED_RECV\25AWDL\26NOACKPRI" \
1191 "\27AWDL_RESTRICTED\30CL2K\31ECN_ENABLE\32ECN_DISABLE\33CHANNEL_DRV\34CA" \
1192 "\35SENDLIST\36DIRECTLINK\37FASTLN_ON\40UPDOWNCHANGE"
1193
1194 #define IFCAPBITS \
1195 "\020\1RXCSUM\2TXCSUM\3VLAN_MTU\4VLAN_HWTAGGING\5JUMBO_MTU" \
1196 "\6TSO4\7TSO6\10LRO\11AV\12TXSTATUS\13CHANNEL_IO\14HW_TIMESTAMP\15SW_TIMESTAMP" \
1197 "\16PARTIAL_CSUM\17ZEROINVERT_CSUM"
1198
1199 #define IFRLOGF_BITS \
1200 "\020\1DLIL\21FAMILY\31DRIVER\35FIRMWARE"
1201
1202 /*
1203 * Print the status of the interface. If an address family was
1204 * specified, show only it; otherwise, show them all.
1205 */
1206 static void
1207 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
1208 struct ifaddrs *ifa)
1209 {
1210 struct ifaddrs *ift;
1211 int allfamilies, s;
1212 struct ifstat ifs;
1213 struct if_descreq ifdr;
1214 struct if_linkparamsreq iflpr;
1215 int mib[6];
1216 struct ifmibdata_supplemental ifmsupp;
1217 size_t miblen = sizeof(struct ifmibdata_supplemental);
1218 u_int64_t eflags = 0;
1219 int curcap = 0;
1220
1221 if (afp == NULL) {
1222 allfamilies = 1;
1223 afp = af_getbyname("inet");
1224 } else
1225 allfamilies = 0;
1226
1227 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
1228 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1229
1230 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1231 if (s < 0)
1232 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1233
1234 printf("%s: ", name);
1235 printb("flags", ifa->ifa_flags, IFFBITS);
1236 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1237 if (ifr.ifr_metric)
1238 printf(" metric %d", ifr.ifr_metric);
1239 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1240 printf(" mtu %d", ifr.ifr_mtu);
1241 if (showrtref && ioctl(s, SIOCGIFGETRTREFCNT, &ifr) != -1)
1242 printf(" rtref %d", ifr.ifr_route_refcnt);
1243 if (verbose) {
1244 unsigned int ifindex = if_nametoindex(ifa->ifa_name);
1245 if (ifindex != 0)
1246 printf(" index %u", ifindex);
1247 }
1248 putchar('\n');
1249
1250 if (verbose && ioctl(s, SIOCGIFEFLAGS, (caddr_t)&ifr) != -1 &&
1251 (eflags = ifr.ifr_eflags) != 0) {
1252 printb("\teflags", eflags, IFEFBITS);
1253 putchar('\n');
1254 }
1255
1256 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1257 if (ifr.ifr_curcap != 0) {
1258 curcap = ifr.ifr_curcap;
1259 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1260 putchar('\n');
1261 }
1262 if (supmedia && ifr.ifr_reqcap != 0) {
1263 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1264 putchar('\n');
1265 }
1266 }
1267
1268 tunnel_status(s);
1269
1270 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1271 if (ift->ifa_addr == NULL)
1272 continue;
1273 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1274 continue;
1275 if (allfamilies) {
1276 const struct afswtch *p;
1277 p = af_getbyfamily(ift->ifa_addr->sa_family);
1278 if (p != NULL && p->af_status != NULL)
1279 p->af_status(s, ift);
1280 } else if (afp->af_af == ift->ifa_addr->sa_family)
1281 afp->af_status(s, ift);
1282 }
1283 #if 0
1284 if (allfamilies || afp->af_af == AF_LINK) {
1285 const struct afswtch *lafp;
1286
1287 /*
1288 * Hack; the link level address is received separately
1289 * from the routing information so any address is not
1290 * handled above. Cobble together an entry and invoke
1291 * the status method specially.
1292 */
1293 lafp = af_getbyname("lladdr");
1294 if (lafp != NULL) {
1295 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1296 lafp->af_status(s, &info);
1297 }
1298 }
1299 #endif
1300 if (allfamilies)
1301 af_other_status(s);
1302 else if (afp->af_other_status != NULL)
1303 afp->af_other_status(s);
1304
1305 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1306 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1307 printf("%s", ifs.ascii);
1308
1309 /* The rest is for when verbose is set; if not set, we're done */
1310 if (!verbose)
1311 goto done;
1312
1313 if (ioctl(s, SIOCGIFTYPE, &ifr) != -1) {
1314 char *c = ift2str(ifr.ifr_type.ift_type,
1315 ifr.ifr_type.ift_family, ifr.ifr_type.ift_subfamily);
1316 if (c != NULL)
1317 printf("\ttype: %s\n", c);
1318 }
1319
1320 if (verbose > 0) {
1321 struct if_agentidsreq ifar;
1322 memset(&ifar, 0, sizeof(ifar));
1323
1324 strlcpy(ifar.ifar_name, name, sizeof(ifar.ifar_name));
1325
1326 if (ioctl(s, SIOCGIFAGENTIDS, &ifar) != -1) {
1327 if (ifar.ifar_count != 0) {
1328 ifar.ifar_uuids = calloc(ifar.ifar_count, sizeof(uuid_t));
1329 if (ifar.ifar_uuids != NULL) {
1330 if (ioctl(s, SIOCGIFAGENTIDS, &ifar) != 1) {
1331 for (int agent_i = 0; agent_i < ifar.ifar_count; agent_i++) {
1332 struct netagent_req nar;
1333 memset(&nar, 0, sizeof(nar));
1334
1335 uuid_copy(nar.netagent_uuid, ifar.ifar_uuids[agent_i]);
1336
1337 if (ioctl(s, SIOCGIFAGENTDATA, &nar) != 1) {
1338 printf("\tagent domain:%s type:%s flags:0x%x desc:\"%s\"\n",
1339 nar.netagent_domain, nar.netagent_type,
1340 nar.netagent_flags, nar.netagent_desc);
1341 }
1342 }
1343 }
1344 free(ifar.ifar_uuids);
1345 }
1346 }
1347 }
1348 }
1349
1350 if (ioctl(s, SIOCGIFLINKQUALITYMETRIC, &ifr) != -1) {
1351 int lqm = ifr.ifr_link_quality_metric;
1352 if (verbose > 1) {
1353 printf("\tlink quality: %d ", lqm);
1354 if (lqm == IFNET_LQM_THRESH_OFF)
1355 printf("(off)");
1356 else if (lqm == IFNET_LQM_THRESH_UNKNOWN)
1357 printf("(unknown)");
1358 else if (lqm > IFNET_LQM_THRESH_UNKNOWN &&
1359 lqm <= IFNET_LQM_THRESH_BAD)
1360 printf("(bad)");
1361 else if (lqm > IFNET_LQM_THRESH_UNKNOWN &&
1362 lqm <= IFNET_LQM_THRESH_POOR)
1363 printf("(poor)");
1364 else if (lqm > IFNET_LQM_THRESH_POOR &&
1365 lqm <= IFNET_LQM_THRESH_GOOD)
1366 printf("(good)");
1367 else
1368 printf("(?)");
1369 printf("\n");
1370 } else if (lqm > IFNET_LQM_THRESH_UNKNOWN) {
1371 printf("\tlink quality: %d ", lqm);
1372 if (lqm <= IFNET_LQM_THRESH_BAD)
1373 printf("(bad)");
1374 else if (lqm <= IFNET_LQM_THRESH_POOR)
1375 printf("(poor)");
1376 else if (lqm <= IFNET_LQM_THRESH_GOOD)
1377 printf("(good)");
1378 else
1379 printf("(?)");
1380 printf("\n");
1381 }
1382 }
1383
1384 if (verbose > 0) {
1385 if (ioctl(s, SIOCGIFINTERFACESTATE, &ifr) != -1) {
1386 printf("\tstate");
1387 if (ifr.ifr_interface_state.valid_bitmask &
1388 IF_INTERFACE_STATE_RRC_STATE_VALID) {
1389 uint8_t rrc_state = ifr.ifr_interface_state.rrc_state;
1390
1391 printf(" rrc: %u ", rrc_state);
1392 if (rrc_state == IF_INTERFACE_STATE_RRC_STATE_CONNECTED)
1393 printf("(connected)");
1394 else if (rrc_state == IF_INTERFACE_STATE_RRC_STATE_IDLE)
1395 printf("(idle)");
1396 else
1397 printf("(?)");
1398 }
1399 if (ifr.ifr_interface_state.valid_bitmask &
1400 IF_INTERFACE_STATE_INTERFACE_AVAILABILITY_VALID) {
1401 uint8_t ifavail = ifr.ifr_interface_state.interface_availability;
1402
1403 printf(" availability: %u ", ifavail);
1404 if (ifavail == IF_INTERFACE_STATE_INTERFACE_AVAILABLE)
1405 printf("(true)");
1406 else if (ifavail == IF_INTERFACE_STATE_INTERFACE_UNAVAILABLE)
1407 printf("(false)");
1408 else
1409 printf("(?)");
1410 } else {
1411 printf(" availability: (not valid)");
1412 }
1413 if (verbose > 1 &&
1414 ifr.ifr_interface_state.valid_bitmask &
1415 IF_INTERFACE_STATE_LQM_STATE_VALID) {
1416 int8_t lqm = ifr.ifr_interface_state.lqm_state;
1417
1418 printf(" lqm: %d", lqm);
1419
1420 if (lqm == IFNET_LQM_THRESH_OFF)
1421 printf("(off)");
1422 else if (lqm == IFNET_LQM_THRESH_UNKNOWN)
1423 printf("(unknown)");
1424 else if (lqm == IFNET_LQM_THRESH_BAD)
1425 printf("(bad)");
1426 else if (lqm == IFNET_LQM_THRESH_POOR)
1427 printf("(poor)");
1428 else if (lqm == IFNET_LQM_THRESH_GOOD)
1429 printf("(good)");
1430 else
1431 printf("(?)");
1432 }
1433 }
1434 printf("\n");
1435 }
1436
1437 bzero(&iflpr, sizeof (iflpr));
1438 strncpy(iflpr.iflpr_name, name, sizeof (iflpr.iflpr_name));
1439 if (ioctl(s, SIOCGIFLINKPARAMS, &iflpr) != -1) {
1440 u_int64_t ibw_max = iflpr.iflpr_input_bw.max_bw;
1441 u_int64_t ibw_eff = iflpr.iflpr_input_bw.eff_bw;
1442 u_int64_t obw_max = iflpr.iflpr_output_bw.max_bw;
1443 u_int64_t obw_eff = iflpr.iflpr_output_bw.eff_bw;
1444 u_int64_t obw_tbr = iflpr.iflpr_output_tbr_rate;
1445 u_int32_t obw_pct = iflpr.iflpr_output_tbr_percent;
1446 u_int64_t ilt_max = iflpr.iflpr_input_lt.max_lt;
1447 u_int64_t ilt_eff = iflpr.iflpr_input_lt.eff_lt;
1448 u_int64_t olt_max = iflpr.iflpr_output_lt.max_lt;
1449 u_int64_t olt_eff = iflpr.iflpr_output_lt.eff_lt;
1450
1451
1452 if (eflags & IFEF_TXSTART) {
1453 u_int32_t flags = iflpr.iflpr_flags;
1454 u_int32_t sched = iflpr.iflpr_output_sched;
1455 struct if_throttlereq iftr;
1456
1457 printf("\tscheduler: %s%s ",
1458 (flags & IFLPRF_ALTQ) ? "ALTQ_" : "",
1459 sched2str(sched));
1460 if (flags & IFLPRF_DRVMANAGED)
1461 printf("(driver managed)");
1462 printf("\n");
1463
1464 bzero(&iftr, sizeof (iftr));
1465 strncpy(iftr.ifthr_name, name,
1466 sizeof (iftr.ifthr_name));
1467 if (ioctl(s, SIOCGIFTHROTTLE, &iftr) != -1 &&
1468 iftr.ifthr_level != IFNET_THROTTLE_OFF)
1469 printf("\tthrottling: level %d (%s)\n",
1470 iftr.ifthr_level, tl2str(iftr.ifthr_level));
1471 }
1472
1473 if (obw_tbr != 0 && obw_eff > obw_tbr)
1474 obw_eff = obw_tbr;
1475
1476 if (ibw_max != 0 || obw_max != 0) {
1477 if (ibw_max == obw_max && ibw_eff == obw_eff &&
1478 ibw_max == ibw_eff && obw_tbr == 0) {
1479 printf("\tlink rate: %s\n",
1480 bps_to_str(ibw_max));
1481 } else {
1482 printf("\tuplink rate: %s [eff] / ",
1483 bps_to_str(obw_eff));
1484 if (obw_tbr != 0) {
1485 if (obw_pct == 0)
1486 printf("%s [tbr] / ",
1487 bps_to_str(obw_tbr));
1488 else
1489 printf("%s [tbr %u%%] / ",
1490 bps_to_str(obw_tbr),
1491 obw_pct);
1492 }
1493 printf("%s", bps_to_str(obw_max));
1494 if (obw_tbr != 0)
1495 printf(" [max]");
1496 printf("\n");
1497 if (ibw_eff == ibw_max) {
1498 printf("\tdownlink rate: %s\n",
1499 bps_to_str(ibw_max));
1500 } else {
1501 printf("\tdownlink rate: "
1502 "%s [eff] / ", bps_to_str(ibw_eff));
1503 printf("%s [max]\n",
1504 bps_to_str(ibw_max));
1505 }
1506 }
1507 } else if (obw_tbr != 0) {
1508 printf("\tuplink rate: %s [tbr]\n",
1509 bps_to_str(obw_tbr));
1510 }
1511
1512 if (ilt_max != 0 || olt_max != 0) {
1513 if (ilt_max == olt_max && ilt_eff == olt_eff &&
1514 ilt_max == ilt_eff) {
1515 printf("\tlink latency: %s\n",
1516 ns_to_str(ilt_max));
1517 } else {
1518 if (olt_max != 0 && olt_eff == olt_max) {
1519 printf("\tuplink latency: %s\n",
1520 ns_to_str(olt_max));
1521 } else if (olt_max != 0) {
1522 printf("\tuplink latency: "
1523 "%s [eff] / ", ns_to_str(olt_eff));
1524 printf("%s [max]\n",
1525 ns_to_str(olt_max));
1526 }
1527 if (ilt_max != 0 && ilt_eff == ilt_max) {
1528 printf("\tdownlink latency: %s\n",
1529 ns_to_str(ilt_max));
1530 } else if (ilt_max != 0) {
1531 printf("\tdownlink latency: "
1532 "%s [eff] / ", ns_to_str(ilt_eff));
1533 printf("%s [max]\n",
1534 ns_to_str(ilt_max));
1535 }
1536 }
1537 }
1538 }
1539
1540 /* Common OID prefix */
1541 mib[0] = CTL_NET;
1542 mib[1] = PF_LINK;
1543 mib[2] = NETLINK_GENERIC;
1544 mib[3] = IFMIB_IFDATA;
1545 mib[4] = if_nametoindex(name);
1546 mib[5] = IFDATA_SUPPLEMENTAL;
1547 if (sysctl(mib, 6, &ifmsupp, &miblen, (void *)0, 0) == -1)
1548 err(1, "sysctl IFDATA_SUPPLEMENTAL");
1549
1550 if (ifmsupp.ifmd_data_extended.ifi_alignerrs != 0) {
1551 printf("\tunaligned pkts: %llu\n",
1552 ifmsupp.ifmd_data_extended.ifi_alignerrs);
1553 }
1554 if (ifmsupp.ifmd_data_extended.ifi_dt_bytes != 0) {
1555 printf("\tdata milestone interval: %s\n",
1556 bytes_to_str(ifmsupp.ifmd_data_extended.ifi_dt_bytes));
1557 }
1558
1559 bzero(&ifdr, sizeof (ifdr));
1560 strncpy(ifdr.ifdr_name, name, sizeof (ifdr.ifdr_name));
1561 if (ioctl(s, SIOCGIFDESC, &ifdr) != -1 && ifdr.ifdr_len) {
1562 printf("\tdesc: %s\n", ifdr.ifdr_desc);
1563 }
1564
1565 if (ioctl(s, SIOCGIFLOG, &ifr) != -1 && ifr.ifr_log.ifl_level) {
1566 printf("\tlogging: level %d ", ifr.ifr_log.ifl_level);
1567 printb("facilities", ifr.ifr_log.ifl_flags, IFRLOGF_BITS);
1568 putchar('\n');
1569 }
1570
1571 if (ioctl(s, SIOCGIFDELEGATE, &ifr) != -1 && ifr.ifr_delegated) {
1572 char delegatedif[IFNAMSIZ+1];
1573 if (if_indextoname(ifr.ifr_delegated, delegatedif) != NULL)
1574 printf("\teffective interface: %s\n", delegatedif);
1575 }
1576
1577 if (ioctl(s, SIOCGSTARTDELAY, &ifr) != -1) {
1578 if (ifr.ifr_start_delay_qlen > 0 &&
1579 ifr.ifr_start_delay_timeout > 0) {
1580 printf("\ttxstart qlen: %u packets "
1581 "timeout: %u microseconds\n",
1582 ifr.ifr_start_delay_qlen,
1583 ifr.ifr_start_delay_timeout/1000);
1584 }
1585 }
1586 #if defined(IFCAP_HW_TIMESTAMP) && defined(IFCAP_SW_TIMESTAMP)
1587 if ((curcap & (IFCAP_HW_TIMESTAMP | IFCAP_SW_TIMESTAMP)) &&
1588 ioctl(s, SIOCGIFTIMESTAMPENABLED, &ifr) != -1) {
1589 printf("\ttimestamp: %s\n",
1590 (ifr.ifr_intval != 0) ? "enabled" : "disabled");
1591 }
1592 #endif
1593 #if defined(SIOCGQOSMARKINGENABLED) && defined(SIOCGQOSMARKINGMODE)
1594 if (ioctl(s, SIOCGQOSMARKINGENABLED, &ifr) != -1) {
1595 printf("\tqosmarking enabled: %s mode: ",
1596 ifr.ifr_qosmarking_enabled ? "yes" : "no");
1597 if (ioctl(s, SIOCGQOSMARKINGMODE, &ifr) != -1) {
1598 switch (ifr.ifr_qosmarking_mode) {
1599 case IFRTYPE_QOSMARKING_FASTLANE:
1600 printf("fastlane\n");
1601 break;
1602 case IFRTYPE_QOSMARKING_MODE_NONE:
1603 printf("none\n");
1604 break;
1605 default:
1606 printf("unknown (%u)\n", ifr.ifr_qosmarking_mode);
1607 break;
1608 }
1609 }
1610 }
1611 #endif /* defined(SIOCGQOSMARKINGENABLED) && defined(SIOCGQOSMARKINGMODE) */
1612 done:
1613 close(s);
1614 return;
1615 }
1616
1617 #define KILOBYTES 1024
1618 #define MEGABYTES (KILOBYTES * KILOBYTES)
1619 #define GIGABYTES (KILOBYTES * KILOBYTES * KILOBYTES)
1620
1621 static char *
1622 bytes_to_str(unsigned long long bytes)
1623 {
1624 static char buf[32];
1625 const char *u;
1626 long double n = bytes, t;
1627
1628 if (bytes >= GIGABYTES) {
1629 t = n / GIGABYTES;
1630 u = "GB";
1631 } else if (n >= MEGABYTES) {
1632 t = n / MEGABYTES;
1633 u = "MB";
1634 } else if (n >= KILOBYTES) {
1635 t = n / KILOBYTES;
1636 u = "KB";
1637 } else {
1638 t = n;
1639 u = "bytes";
1640 }
1641
1642 snprintf(buf, sizeof (buf), "%-4.2Lf %s", t, u);
1643 return (buf);
1644 }
1645
1646 #define GIGABIT_PER_SEC 1000000000 /* gigabit per second */
1647 #define MEGABIT_PER_SEC 1000000 /* megabit per second */
1648 #define KILOBIT_PER_SEC 1000 /* kilobit per second */
1649
1650 static char *
1651 bps_to_str(unsigned long long rate)
1652 {
1653 static char buf[32];
1654 const char *u;
1655 long double n = rate, t;
1656
1657 if (rate >= GIGABIT_PER_SEC) {
1658 t = n / GIGABIT_PER_SEC;
1659 u = "Gbps";
1660 } else if (n >= MEGABIT_PER_SEC) {
1661 t = n / MEGABIT_PER_SEC;
1662 u = "Mbps";
1663 } else if (n >= KILOBIT_PER_SEC) {
1664 t = n / KILOBIT_PER_SEC;
1665 u = "Kbps";
1666 } else {
1667 t = n;
1668 u = "bps ";
1669 }
1670
1671 snprintf(buf, sizeof (buf), "%-4.2Lf %4s", t, u);
1672 return (buf);
1673 }
1674
1675 #define NSEC_PER_SEC 1000000000 /* nanosecond per second */
1676 #define USEC_PER_SEC 1000000 /* microsecond per second */
1677 #define MSEC_PER_SEC 1000 /* millisecond per second */
1678
1679 static char *
1680 ns_to_str(unsigned long long nsec)
1681 {
1682 static char buf[32];
1683 const char *u;
1684 long double n = nsec, t;
1685
1686 if (nsec >= NSEC_PER_SEC) {
1687 t = n / NSEC_PER_SEC;
1688 u = "sec ";
1689 } else if (n >= USEC_PER_SEC) {
1690 t = n / USEC_PER_SEC;
1691 u = "msec";
1692 } else if (n >= MSEC_PER_SEC) {
1693 t = n / MSEC_PER_SEC;
1694 u = "usec";
1695 } else {
1696 t = n;
1697 u = "nsec";
1698 }
1699
1700 snprintf(buf, sizeof (buf), "%-4.2Lf %4s", t, u);
1701 return (buf);
1702 }
1703
1704 static void
1705 tunnel_status(int s)
1706 {
1707 af_all_tunnel_status(s);
1708 }
1709
1710 void
1711 Perror(const char *cmd)
1712 {
1713 switch (errno) {
1714
1715 case ENXIO:
1716 errx(1, "%s: no such interface", cmd);
1717 break;
1718
1719 case EPERM:
1720 errx(1, "%s: permission denied", cmd);
1721 break;
1722
1723 default:
1724 err(1, "%s", cmd);
1725 }
1726 }
1727
1728 /*
1729 * Print a value a la the %b format of the kernel's printf
1730 */
1731 void
1732 printb(const char *s, unsigned v, const char *bits)
1733 {
1734 int i, any = 0;
1735 char c;
1736
1737 if (bits && *bits == 8)
1738 printf("%s=%o", s, v);
1739 else
1740 printf("%s=%x", s, v);
1741 bits++;
1742 if (bits) {
1743 putchar('<');
1744 while ((i = *bits++) != '\0') {
1745 if (v & (1 << (i-1))) {
1746 if (any)
1747 putchar(',');
1748 any = 1;
1749 for (; (c = *bits) > 32; bits++)
1750 putchar(c);
1751 } else
1752 for (; *bits > 32; bits++)
1753 ;
1754 }
1755 putchar('>');
1756 }
1757 }
1758
1759 #ifndef __APPLE__
1760 void
1761 ifmaybeload(const char *name)
1762 {
1763 #define MOD_PREFIX_LEN 3 /* "if_" */
1764 struct module_stat mstat;
1765 int fileid, modid;
1766 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1767 const char *cp;
1768
1769 /* loading suppressed by the user */
1770 if (noload)
1771 return;
1772
1773 /* trim the interface number off the end */
1774 strlcpy(ifname, name, sizeof(ifname));
1775 for (dp = ifname; *dp != 0; dp++)
1776 if (isdigit(*dp)) {
1777 *dp = 0;
1778 break;
1779 }
1780
1781 /* turn interface and unit into module name */
1782 strlcpy(ifkind, "if_", sizeof(ifkind));
1783 strlcpy(ifkind + MOD_PREFIX_LEN, ifname,
1784 sizeof(ifkind) - MOD_PREFIX_LEN);
1785
1786 /* scan files in kernel */
1787 mstat.version = sizeof(struct module_stat);
1788 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1789 /* scan modules in file */
1790 for (modid = kldfirstmod(fileid); modid > 0;
1791 modid = modfnext(modid)) {
1792 if (modstat(modid, &mstat) < 0)
1793 continue;
1794 /* strip bus name if present */
1795 if ((cp = strchr(mstat.name, '/')) != NULL) {
1796 cp++;
1797 } else {
1798 cp = mstat.name;
1799 }
1800 /* already loaded? */
1801 if (strncmp(ifname, cp, strlen(ifname) + 1) == 0 ||
1802 strncmp(ifkind, cp, strlen(ifkind) + 1) == 0)
1803 return;
1804 }
1805 }
1806
1807 /* not present, we should try to load it */
1808 kldload(ifkind);
1809 }
1810 #endif
1811
1812 static struct cmd basic_cmds[] = {
1813 DEF_CMD("up", IFF_UP, setifflags),
1814 DEF_CMD("down", -IFF_UP, setifflags),
1815 DEF_CMD("arp", -IFF_NOARP, setifflags),
1816 DEF_CMD("-arp", IFF_NOARP, setifflags),
1817 DEF_CMD("debug", IFF_DEBUG, setifflags),
1818 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1819 #ifdef IFF_PPROMISC
1820 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1821 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1822 #endif /* IFF_PPROMISC */
1823 DEF_CMD("add", IFF_UP, notealias),
1824 DEF_CMD("alias", IFF_UP, notealias),
1825 DEF_CMD("-alias", -IFF_UP, notealias),
1826 DEF_CMD("delete", -IFF_UP, notealias),
1827 DEF_CMD("remove", -IFF_UP, notealias),
1828 #ifdef notdef
1829 #define EN_SWABIPS 0x1000
1830 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1831 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1832 #endif
1833 DEF_CMD_ARG("netmask", setifnetmask),
1834 DEF_CMD_ARG("metric", setifmetric),
1835 DEF_CMD_ARG("broadcast", setifbroadaddr),
1836 DEF_CMD_ARG("ipdst", setifipdst),
1837 DEF_CMD_ARG2("tunnel", settunnel),
1838 DEF_CMD("-tunnel", 0, deletetunnel),
1839 DEF_CMD("deletetunnel", 0, deletetunnel),
1840 DEF_CMD("link0", IFF_LINK0, setifflags),
1841 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1842 DEF_CMD("link1", IFF_LINK1, setifflags),
1843 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1844 DEF_CMD("link2", IFF_LINK2, setifflags),
1845 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1846 #ifdef IFF_MONITOR
1847 DEF_CMD("monitor", IFF_MONITOR:, setifflags),
1848 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1849 #endif /* IFF_MONITOR */
1850 #ifdef IFF_STATICARP
1851 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1852 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1853 #endif /* IFF_STATICARP */
1854 #ifdef IFCAP_RXCSUM
1855 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1856 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1857 #endif /* IFCAP_RXCSUM */
1858 #ifdef IFCAP_TXCSUM
1859 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1860 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1861 #endif /* IFCAP_TXCSUM */
1862 #ifdef IFCAP_NETCONS
1863 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1864 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1865 #endif /* IFCAP_NETCONS */
1866 #ifdef IFCAP_POLLING
1867 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1868 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1869 #endif /* IFCAP_POLLING */
1870 #ifdef IFCAP_TSO
1871 DEF_CMD("tso", IFCAP_TSO, setifcap),
1872 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1873 #endif /* IFCAP_TSO */
1874 #ifdef IFCAP_LRO
1875 DEF_CMD("lro", IFCAP_LRO, setifcap),
1876 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1877 #endif /* IFCAP_LRO */
1878 #ifdef IFCAP_WOL
1879 DEF_CMD("wol", IFCAP_WOL, setifcap),
1880 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1881 #endif /* IFCAP_WOL */
1882 #ifdef IFCAP_WOL_UCAST
1883 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1884 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1885 #endif /* IFCAP_WOL_UCAST */
1886 #ifdef IFCAP_WOL_MCAST
1887 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1888 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1889 #endif /* IFCAP_WOL_MCAST */
1890 #ifdef IFCAP_WOL_MAGIC
1891 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1892 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1893 #endif /* IFCAP_WOL_MAGIC */
1894 DEF_CMD("normal", -IFF_LINK0, setifflags),
1895 DEF_CMD("compress", IFF_LINK0, setifflags),
1896 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1897 DEF_CMD_ARG("mtu", setifmtu),
1898 #ifdef notdef
1899 DEF_CMD_ARG("name", setifname),
1900 #endif /* notdef */
1901 #ifdef IFCAP_AV
1902 DEF_CMD("av", IFCAP_AV, setifcap),
1903 DEF_CMD("-av", -IFCAP_AV, setifcap),
1904 #endif /* IFCAP_AV */
1905 DEF_CMD("router", 1, setrouter),
1906 DEF_CMD("-router", 0, setrouter),
1907 DEF_CMD_ARG("desc", setifdesc),
1908 DEF_CMD_ARG("tbr", settbr),
1909 DEF_CMD_ARG("throttle", setthrottle),
1910 DEF_CMD_ARG("log", setlog),
1911 DEF_CMD("cl2k", 1, setcl2k),
1912 DEF_CMD("-cl2k", 0, setcl2k),
1913 DEF_CMD("expensive", 1, setexpensive),
1914 DEF_CMD("-expensive", 0, setexpensive),
1915 DEF_CMD("timestamp", 1, settimestamp),
1916 DEF_CMD("-timestamp", 0, settimestamp),
1917 DEF_CMD_ARG("ecn", setecnmode),
1918 DEF_CMD_ARG2("fastlane", setfastlane),
1919 DEF_CMD_ARG2("qosmarking", setqosmarking),
1920 DEF_CMD_ARG("disable_output", setdisableoutput),
1921 };
1922
1923 static __constructor void
1924 ifconfig_ctor(void)
1925 {
1926 #define N(a) (sizeof(a) / sizeof(a[0]))
1927 int i;
1928
1929 for (i = 0; i < N(basic_cmds); i++)
1930 cmd_register(&basic_cmds[i]);
1931 #undef N
1932 }
1933
1934 static char *
1935 sched2str(unsigned int s)
1936 {
1937 char *c;
1938
1939 switch (s) {
1940 case PKTSCHEDT_NONE:
1941 c = "NONE";
1942 break;
1943 case PKTSCHEDT_TCQ:
1944 c = "TCQ";
1945 break;
1946 case PKTSCHEDT_QFQ:
1947 c = "QFQ";
1948 break;
1949 case PKTSCHEDT_FQ_CODEL:
1950 c = "FQ_CODEL";
1951 break;
1952 default:
1953 c = "UNKNOWN";
1954 break;
1955 }
1956
1957 return (c);
1958 }
1959
1960 static char *
1961 tl2str(unsigned int s)
1962 {
1963 char *c;
1964
1965 switch (s) {
1966 case IFNET_THROTTLE_OFF:
1967 c = "off";
1968 break;
1969 case IFNET_THROTTLE_OPPORTUNISTIC:
1970 c = "opportunistic";
1971 break;
1972 default:
1973 c = "unknown";
1974 break;
1975 }
1976
1977 return (c);
1978 }
1979
1980 static char *
1981 ift2str(unsigned int t, unsigned int f, unsigned int sf)
1982 {
1983 static char buf[256];
1984 char *c = NULL;
1985
1986 switch (t) {
1987 case IFT_ETHER:
1988 switch (sf) {
1989 case IFRTYPE_SUBFAMILY_USB:
1990 c = "USB Ethernet";
1991 break;
1992 case IFRTYPE_SUBFAMILY_BLUETOOTH:
1993 c = "Bluetooth PAN";
1994 break;
1995 case IFRTYPE_SUBFAMILY_WIFI:
1996 c = "Wi-Fi";
1997 break;
1998 case IFRTYPE_SUBFAMILY_THUNDERBOLT:
1999 c = "IP over Thunderbolt";
2000 break;
2001 case IFRTYPE_SUBFAMILY_ANY:
2002 default:
2003 c = "Ethernet";
2004 break;
2005 }
2006 break;
2007
2008 case IFT_IEEE1394:
2009 c = "IP over FireWire";
2010 break;
2011
2012 case IFT_PKTAP:
2013 c = "Packet capture";
2014 break;
2015
2016 case IFT_CELLULAR:
2017 c = "Cellular";
2018 break;
2019
2020 case IFT_BRIDGE:
2021 case IFT_PFLOG:
2022 case IFT_PFSYNC:
2023 case IFT_OTHER:
2024 case IFT_PPP:
2025 case IFT_LOOP:
2026 case IFT_GIF:
2027 case IFT_STF:
2028 case IFT_L2VLAN:
2029 case IFT_IEEE8023ADLAG:
2030 default:
2031 break;
2032 }
2033
2034 if (verbose > 1) {
2035 if (c == NULL) {
2036 (void) snprintf(buf, sizeof (buf),
2037 "0x%x family: %u subfamily: %u",
2038 ifr.ifr_type.ift_type, ifr.ifr_type.ift_family,
2039 ifr.ifr_type.ift_subfamily);
2040 } else {
2041 (void) snprintf(buf, sizeof (buf),
2042 "%s (0x%x) family: %u subfamily: %u", c,
2043 ifr.ifr_type.ift_type, ifr.ifr_type.ift_family,
2044 ifr.ifr_type.ift_subfamily);
2045 }
2046 c = buf;
2047 }
2048
2049 return (c);
2050 }