2 * Copyright (c) 2009-2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 1983, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
58 #include <sys/cdefs.h>
61 __unused
static const char copyright
[] =
62 "@(#) Copyright (c) 1983, 1993\n\
63 The Regents of the University of California. All rights reserved.\n";
66 #include <sys/param.h>
67 #include <sys/ioctl.h>
68 #include <sys/socket.h>
69 #include <sys/sysctl.h>
72 #include <sys/module.h>
73 #include <sys/linker.h>
76 #include <net/ethernet.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>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <arpa/inet.h>
101 #include <sysexits.h>
103 #include "ifconfig.h"
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.)
121 int bond_details
= 0;
123 #if TARGET_OS_EMBEDDED
126 #else /* !TARGET_OS_EMBEDDED */
129 #endif /* !TARGET_OS_EMBEDDED */
130 int printkeys
= 0; /* Print keying material for interfaces. */
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
);
145 static struct afswtch
*af_getbyname(const char *name
);
146 static struct afswtch
*af_getbyfamily(int af
);
147 static void af_other_status(int);
149 static struct option
*opts
= NULL
;
152 opt_register(struct option
*p
)
164 /* XXX not right but close enough for now */
166 for (p
= opts
; p
!= NULL
; p
= p
->next
) {
167 strlcat(options
, p
->opt_usage
, sizeof(options
));
168 strlcat(options
, " ", sizeof(options
));
172 "usage: ifconfig %sinterface address_family [address [dest_address]]\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
);
183 main(int argc
, char *argv
[])
185 int c
, namesonly
, downonly
, uponly
;
186 const struct afswtch
*afp
= NULL
;
188 struct ifaddrs
*ifap
, *ifa
;
190 const struct sockaddr_dl
*sdl
;
191 char options
[1024], *cp
;
196 all
= downonly
= uponly
= namesonly
= noload
= 0;
198 /* Parse leading line options */
200 strlcpy(options
, "adklmnuv", sizeof(options
));
202 strlcpy(options
, "abdlmruv", sizeof(options
));
204 for (p
= opts
; p
!= NULL
; p
= p
->next
)
205 strlcat(options
, p
->opt
, sizeof(options
));
206 while ((c
= getopt(argc
, argv
, options
)) != -1) {
208 case 'a': /* scan all interfaces */
211 case 'b': /* bond detailed output */
214 case 'd': /* restrict scan to "down" interfaces */
222 case 'l': /* scan interface names only */
225 case 'm': /* show media choices in status */
229 case 'n': /* suppress module loading */
236 case 'u': /* restrict scan to "up" interfaces */
243 for (p
= opts
; p
!= NULL
; p
= p
->next
)
244 if (p
->opt
[0] == c
) {
256 /* -l cannot be used with -a or -q or -m or -b */
258 (all
|| supmedia
|| bond_details
))
262 if (uponly
&& downonly
)
265 /* no arguments is equivalent to '-a' */
266 if (!namesonly
&& argc
< 1)
269 /* -a and -l allow an address family arg to limit the output */
270 if (all
|| namesonly
) {
276 afp
= af_getbyname(*argv
);
279 if (afp
->af_name
!= NULL
)
281 /* leave with afp non-zero */
284 /* not listing, need an argument */
292 /* check and maybe load support for this interface */
295 ifindex
= if_nametoindex(ifname
);
298 * NOTE: We must special-case the `create' command
299 * right here as we would otherwise fail when trying
300 * to find the interface.
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",
308 ifconfig(argc
, argv
, 1, NULL
);
311 errx(1, "interface %s does not exist", ifname
);
315 /* Check for address family */
317 afp
= af_getbyname(*argv
);
322 if (getifaddrs(&ifap
) != 0)
323 err(EXIT_FAILURE
, "getifaddrs");
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
);
334 if (ifname
!= NULL
&& strcmp(ifname
, ifa
->ifa_name
) != 0)
336 if (ifa
->ifa_addr
->sa_family
== AF_LINK
)
337 sdl
= (const struct sockaddr_dl
*) ifa
->ifa_addr
;
340 if (cp
!= NULL
&& strcmp(cp
, ifa
->ifa_name
) == 0)
342 iflen
= strlcpy(name
, ifa
->ifa_name
, sizeof(name
));
343 if (iflen
>= sizeof(name
)) {
344 warnx("%s: interface name too long, skipping",
350 if (downonly
&& (ifa
->ifa_flags
& IFF_UP
) != 0)
352 if (uponly
&& (ifa
->ifa_flags
& IFF_UP
) == 0)
356 * Are we just listing the interfaces?
366 ifconfig(argc
, argv
, 0, afp
);
368 status(afp
, sdl
, ifa
);
377 static struct afswtch
*afs
= NULL
;
380 af_register(struct afswtch
*p
)
386 static struct afswtch
*
387 af_getbyname(const char *name
)
391 for (afp
= afs
; afp
!= NULL
; afp
= afp
->af_next
)
392 if (strcmp(afp
->af_name
, name
) == 0)
397 static struct afswtch
*
398 af_getbyfamily(int af
)
402 for (afp
= afs
; afp
!= NULL
; afp
= afp
->af_next
)
403 if (afp
->af_af
== af
)
409 af_other_status(int s
)
412 uint8_t afmask
[howmany(AF_MAX
, NBBY
)];
414 memset(afmask
, 0, sizeof(afmask
));
415 for (afp
= afs
; afp
!= NULL
; afp
= afp
->af_next
) {
416 if (afp
->af_other_status
== NULL
)
418 if (afp
->af_af
!= AF_UNSPEC
&& isset(afmask
, afp
->af_af
))
420 afp
->af_other_status(s
);
421 setbit(afmask
, afp
->af_af
);
426 af_all_tunnel_status(int s
)
429 uint8_t afmask
[howmany(AF_MAX
, NBBY
)];
431 memset(afmask
, 0, sizeof(afmask
));
432 for (afp
= afs
; afp
!= NULL
; afp
= afp
->af_next
) {
433 if (afp
->af_status_tunnel
== NULL
)
435 if (afp
->af_af
!= AF_UNSPEC
&& isset(afmask
, afp
->af_af
))
437 afp
->af_status_tunnel(s
);
438 setbit(afmask
, afp
->af_af
);
442 static struct cmd
*cmds
= NULL
;
445 cmd_register(struct cmd
*p
)
451 static const struct cmd
*
452 cmd_lookup(const char *name
)
454 #define N(a) (sizeof(a)/sizeof(a[0]))
457 for (p
= cmds
; p
!= NULL
; p
= p
->c_next
)
458 if (strcmp(name
, p
->c_name
) == 0)
465 callback_func
*cb_func
;
467 struct callback
*cb_next
;
469 static struct callback
*callbacks
= NULL
;
472 callback_register(callback_func
*func
, void *arg
)
476 cb
= malloc(sizeof(struct callback
));
478 errx(1, "unable to allocate memory for callback");
481 cb
->cb_next
= callbacks
;
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
);
489 static void setifdstaddr(const char *, int, int, const struct afswtch
*);
490 static const struct cmd setifdstaddr_cmd
=
491 DEF_CMD("ifdstaddr", 0, setifdstaddr
);
494 ifconfig(int argc
, char *const *argv
, int iscreate
, const struct afswtch
*afp
)
496 const struct afswtch
*nafp
;
500 strncpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
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
;
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
);
514 p
= cmd_lookup(*argv
);
517 * Not a recognized command, choose between setting
518 * the interface address and the dst address.
520 p
= (setaddr
? &setifdstaddr_cmd
: &setifaddr_cmd
);
522 if (p
->c_u
.c_func
|| p
->c_u
.c_func2
) {
523 if (iscreate
&& !p
->c_iscloneop
) {
525 * Push the clone create callback so the new
526 * device is created and can be used for any
527 * remaining arguments.
531 errx(1, "internal error, no callback");
532 callbacks
= cb
->cb_next
;
533 cb
->cb_func(s
, cb
->cb_arg
);
536 * Handle any address family spec that
537 * immediately follows and potentially
538 * recreate the socket.
540 nafp
= af_getbyname(*argv
);
550 if (p
->c_parameter
== NEXTARG
) {
552 errx(1, "'%s' requires argument",
554 p
->c_u
.c_func(argv
[1], 0, s
, afp
);
556 } else if (p
->c_parameter
== OPTARG
) {
557 p
->c_u
.c_func(argv
[1], 0, s
, afp
);
560 } else if (p
->c_parameter
== NEXTARG2
) {
562 errx(1, "'%s' requires 2 arguments",
564 p
->c_u
.c_func2(argv
[1], argv
[2], s
, afp
);
565 argc
-= 2, argv
+= 2;
567 p
->c_u
.c_func(*argv
, p
->c_parameter
, s
, afp
);
573 * Do any post argument processing required by the address family.
575 if (afp
->af_postproc
!= NULL
)
576 afp
->af_postproc(s
, afp
);
578 * Do deferred callbacks registered while processing
579 * command-line arguments.
581 for (cb
= callbacks
; cb
!= NULL
; cb
= cb
->cb_next
)
582 cb
->cb_func(s
, cb
->cb_arg
);
584 * Do deferred operations.
587 if (afp
->af_ridreq
== NULL
|| afp
->af_difaddr
== 0) {
588 warnx("interface %s cannot change %s addresses!",
595 strncpy(afp
->af_ridreq
, name
, sizeof ifr
.ifr_name
);
596 ret
= ioctl(s
, afp
->af_difaddr
, afp
->af_ridreq
);
598 if (errno
== EADDRNOTAVAIL
&& (doalias
>= 0)) {
599 /* means no previous address for interface */
601 Perror("ioctl (SIOCDIFADDR)");
605 if (afp
->af_addreq
== NULL
|| afp
->af_aifaddr
== 0) {
606 warnx("interface %s cannot change %s addresses!",
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)");
623 setifaddr(const char *addr
, int param
, int s
, const struct afswtch
*afp
)
625 if (afp
->af_getaddr
== NULL
)
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.
633 if (doalias
== 0 && afp
->af_af
!= AF_LINK
)
635 afp
->af_getaddr(addr
, (doalias
>= 0 ? ADDR
: RIDADDR
));
639 settunnel(const char *src
, const char *dst
, int s
, const struct afswtch
*afp
)
641 struct addrinfo
*srcres
, *dstres
;
644 if (afp
->af_settunnel
== NULL
) {
645 warn("address family %s does not support tunnel setup",
650 if ((ecode
= getaddrinfo(src
, NULL
, NULL
, &srcres
)) != 0)
651 errx(1, "error in parsing address string: %s",
652 gai_strerror(ecode
));
654 if ((ecode
= getaddrinfo(dst
, NULL
, NULL
, &dstres
)) != 0)
655 errx(1, "error in parsing address string: %s",
656 gai_strerror(ecode
));
658 if (srcres
->ai_addr
->sa_family
!= dstres
->ai_addr
->sa_family
)
660 "source and destination address families do not match");
662 afp
->af_settunnel(s
, srcres
, dstres
);
664 freeaddrinfo(srcres
);
665 freeaddrinfo(dstres
);
670 deletetunnel(const char *vname
, int param
, int s
, const struct afswtch
*afp
)
673 if (ioctl(s
, SIOCDIFPHYADDR
, &ifr
) < 0)
674 err(1, "SIOCDIFPHYADDR");
678 setifnetmask(const char *addr
, int dummy __unused
, int s
,
679 const struct afswtch
*afp
)
681 if (afp
->af_getaddr
!= NULL
) {
683 afp
->af_getaddr(addr
, MASK
);
688 setifbroadaddr(const char *addr
, int dummy __unused
, int s
,
689 const struct afswtch
*afp
)
691 if (afp
->af_getaddr
!= NULL
)
692 afp
->af_getaddr(addr
, DSTADDR
);
696 setifipdst(const char *addr
, int dummy __unused
, int s
,
697 const struct afswtch
*afp
)
699 const struct afswtch
*inet
;
701 inet
= af_getbyname("inet");
704 inet
->af_getaddr(addr
, DSTADDR
);
710 notealias(const char *addr
, int param
, int s
, const struct afswtch
*afp
)
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
);
729 setifdstaddr(const char *addr
, int param __unused
, int s
,
730 const struct afswtch
*afp
)
732 if (afp
->af_getaddr
!= NULL
)
733 afp
->af_getaddr(addr
, DSTADDR
);
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.
742 setifflags(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
747 bcopy((char *)&ifr
, (char *)&my_ifr
, sizeof(struct ifreq
));
749 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&my_ifr
) < 0) {
750 Perror("ioctl (SIOCGIFFLAGS)");
753 strncpy(my_ifr
.ifr_name
, name
, sizeof (my_ifr
.ifr_name
));
754 flags
= my_ifr
.ifr_flags
;
761 my_ifr
.ifr_flags
= flags
& 0xffff;
762 if (ioctl(s
, SIOCSIFFLAGS
, (caddr_t
)&my_ifr
) < 0)
767 setifcap(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
771 if (ioctl(s
, SIOCGIFCAP
, (caddr_t
)&ifr
) < 0) {
772 Perror("ioctl (SIOCGIFCAP)");
775 flags
= ifr
.ifr_curcap
;
781 flags
&= ifr
.ifr_reqcap
;
782 ifr
.ifr_reqcap
= flags
;
783 if (ioctl(s
, SIOCSIFCAP
, (caddr_t
)&ifr
) < 0)
788 setifmetric(const char *val
, int dummy __unused
, int s
,
789 const struct afswtch
*afp
)
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)");
798 setifmtu(const char *val
, int dummy __unused
, int s
,
799 const struct afswtch
*afp
)
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)");
809 setifname(const char *val
, int dummy __unused
, int s
,
810 const struct afswtch
*afp
)
814 newname
= strdup(val
);
815 if (newname
== NULL
) {
816 warn("no memory to set ifname");
819 ifr
.ifr_data
= newname
;
820 if (ioctl(s
, SIOCSIFNAME
, (caddr_t
)&ifr
) < 0) {
821 warn("ioctl (set name)");
825 strlcpy(name
, newname
, sizeof(name
));
831 setrouter(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
833 if (afp
->af_setrouter
== NULL
) {
834 warn("address family %s does not support router mode",
839 afp
->af_setrouter(s
, value
);
843 setifdesc(const char *val
, int dummy __unused
, int s
, const struct afswtch
*afp
)
845 struct if_descreq ifdr
;
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
));
852 if (ioctl(s
, SIOCSIFDESC
, (caddr_t
)&ifdr
) < 0) {
853 warn("ioctl (set desc)");
858 settbr(const char *val
, int dummy __unused
, int s
, const struct afswtch
*afp
)
860 struct if_linkparamsreq iflpr
;
863 u_int32_t percent
= 0;
867 bzero(&iflpr
, sizeof (iflpr
));
868 strncpy(iflpr
.iflpr_name
, name
, sizeof (iflpr
.iflpr_name
));
870 bps
= strtold(val
, &cp
);
871 if (val
== cp
|| errno
!= 0) {
872 warn("Invalid value '%s'", val
);
875 rate
= (u_int64_t
)bps
;
877 if (!strcmp(cp
, "b") || !strcmp(cp
, "bps")) {
879 } else if (!strcmp(cp
, "Kb") || !strcmp(cp
, "Kbps")) {
881 } else if (!strcmp(cp
, "Mb") || !strcmp(cp
, "Mbps")) {
883 } else if (!strcmp(cp
, "Gb") || !strcmp(cp
, "Gbps")) {
884 rate
*= 1000 * 1000 * 1000;
885 } else if (!strcmp(cp
, "%")) {
887 if (percent
== 0 || percent
> 100) {
888 printf("Value out of range '%s'", val
);
891 } else if (*cp
!= '\0') {
892 printf("Unknown unit '%s'", cp
);
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",
911 printf("%s: TBR rate set to %s\n", name
, bps_to_str(rate
));
916 setthrottle(const char *val
, int dummy __unused
, int s
,
917 const struct afswtch
*afp
)
919 struct if_throttlereq iftr
;
923 bzero(&iftr
, sizeof (iftr
));
924 strncpy(iftr
.ifthr_name
, name
, sizeof (iftr
.ifthr_name
));
926 iftr
.ifthr_level
= strtold(val
, &cp
);
927 if (val
== cp
|| errno
!= 0) {
928 warn("Invalid value '%s'", val
);
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
);
937 printf("%s: throttling level set to %d\n", name
,
943 setdisableoutput(const char *val
, int dummy __unused
, int s
,
944 const struct afswtch
*afp
)
949 bzero(&ifr
, sizeof (ifr
));
950 strncpy(ifr
.ifr_name
, name
, sizeof (ifr
.ifr_name
));
952 ifr
.ifr_ifru
.ifru_disable_output
= strtold(val
, &cp
);
953 if (val
== cp
|| errno
!= 0) {
954 warn("Invalid value '%s'", val
);
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
);
963 printf("output %s on %s\n",
964 ((ifr
.ifr_ifru
.ifru_disable_output
== 0) ? "enabled" : "disabled"),
970 setlog(const char *val
, int dummy __unused
, int s
,
971 const struct afswtch
*afp
)
976 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
978 ifr
.ifr_log
.ifl_level
= strtold(val
, &cp
);
979 if (val
== cp
|| errno
!= 0) {
980 warn("Invalid value '%s'", val
);
983 ifr
.ifr_log
.ifl_flags
= (IFRLOGF_DLIL
|IFRLOGF_FAMILY
|IFRLOGF_DRIVER
|
986 if (ioctl(s
, SIOCSIFLOG
, &ifr
) < 0)
987 warn("ioctl (set logging parameters)");
991 setcl2k(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
993 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
994 ifr
.ifr_ifru
.ifru_2kcl
= value
;
996 if (ioctl(s
, SIOCSIF2KCL
, (caddr_t
)&ifr
) < 0)
1001 setexpensive(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1003 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1004 ifr
.ifr_ifru
.ifru_expensive
= value
;
1006 if (ioctl(s
, SIOCSIFEXPENSIVE
, (caddr_t
)&ifr
) < 0)
1011 settimestamp(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1013 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1016 if (ioctl(s
, SIOCSIFTIMESTAMPDISABLE
, (caddr_t
)&ifr
) < 0)
1019 if (ioctl(s
, SIOCSIFTIMESTAMPENABLE
, (caddr_t
)&ifr
) < 0)
1025 setecnmode(const char *val
, int dummy __unused
, int s
,
1026 const struct afswtch
*afp
)
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
;
1037 ifr
.ifr_ifru
.ifru_ecn_mode
= strtold(val
, &cp
);
1038 if (val
== cp
|| errno
!= 0) {
1039 warn("Invalid ECN mode value '%s'", val
);
1044 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1046 if (ioctl(s
, SIOCSECNMODE
, (caddr_t
)&ifr
) < 0)
1047 Perror("ioctl(SIOCSECNMODE)");
1050 #if defined(SIOCSQOSMARKINGMODE) && defined(SIOCSQOSMARKINGENABLED)
1053 setqosmarking(const char *cmd
, const char *arg
, int s
, const struct afswtch
*afp
)
1057 #if (DEBUG | DEVELOPMENT)
1058 printf("%s(%s, %s)\n", __func__
, cmd
, arg
);
1059 #endif /* (DEBUG | DEVELOPMENT) */
1061 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1063 if (strcmp(cmd
, "mode") == 0) {
1064 ioc
= SIOCSQOSMARKINGMODE
;
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
;
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;
1081 err(EX_USAGE
, "bad value for qosmarking enabled: %s", arg
);
1083 err(EX_USAGE
, "qosmarking takes mode or enabled");
1086 if (ioctl(s
, ioc
, (caddr_t
)&ifr
) < 0)
1087 err(EX_OSERR
, "ioctl(%s, %s)", cmd
, arg
);
1091 setfastlane(const char *cmd
, const char *arg
, int s
, const struct afswtch
*afp
)
1093 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1095 warnx("### fastlane is obsolete, use qosmarking ###");
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
);
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
);
1114 err(EX_USAGE
, "bad value for fastlane %s", cmd
);
1116 err(EX_USAGE
, "fastlane takes capable or enable");
1120 #else /* defined(SIOCSQOSMARKINGMODE) && defined(SIOCSQOSMARKINGENABLED) */
1123 setfastlane(const char *cmd
, const char *arg
, int s
, const struct afswtch
*afp
)
1128 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1130 if (strcmp(cmd
, "capable") == 0)
1131 ioc
= SIOCSFASTLANECAPABLE
;
1132 else if (strcmp(cmd
, "enable") == 0)
1133 ioc
= SIOCSFASTLEENABLED
;
1135 err(EX_USAGE
, "fastlane takes capable or enabled");
1137 if (strcmp(arg
, "1") == 0 || strcasecmp(arg
, "on") == 0||
1138 strcasecmp(arg
, "yes") == 0 || strcasecmp(arg
, "true") == 0)
1140 else if (strcmp(arg
, "0") == 0 || strcasecmp(arg
, "off") == 0||
1141 strcasecmp(arg
, "no") == 0 || strcasecmp(arg
, "false") == 0)
1144 err(EX_USAGE
, "bad value for fastlane %s", cmd
);
1146 if (ioc
== SIOCSFASTLANECAPABLE
)
1147 ifr
.ifr_fastlane_capable
= value
;
1149 ifr
.ifr_fastlane_enabled
= value
;
1151 if (ioctl(s
, ioc
, (caddr_t
)&ifr
) < 0)
1152 err(EX_OSERR
, "ioctl(%s, %s)", cmd
, arg
);
1157 setqosmarking(const char *cmd
, const char *arg
, int s
, const struct afswtch
*afp
)
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
);
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
);
1174 err(EX_USAGE
, "bad value for qosmarking enabled: %s", arg
);
1176 err(EX_USAGE
, "qosmarking takes mode or enabled");
1180 #endif /* defined(SIOCSQOSMARKINGMODE) && defined(SIOCSQOSMARKINGENABLED) */
1183 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1184 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
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"
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"
1199 #define IFRLOGF_BITS \
1200 "\020\1DLIL\21FAMILY\31DRIVER\35FIRMWARE"
1203 * Print the status of the interface. If an address family was
1204 * specified, show only it; otherwise, show them all.
1207 status(const struct afswtch
*afp
, const struct sockaddr_dl
*sdl
,
1208 struct ifaddrs
*ifa
)
1210 struct ifaddrs
*ift
;
1213 struct if_descreq ifdr
;
1214 struct if_linkparamsreq iflpr
;
1216 struct ifmibdata_supplemental ifmsupp
;
1217 size_t miblen
= sizeof(struct ifmibdata_supplemental
);
1218 u_int64_t eflags
= 0;
1223 afp
= af_getbyname("inet");
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
));
1230 s
= socket(ifr
.ifr_addr
.sa_family
, SOCK_DGRAM
, 0);
1232 err(1, "socket(family %u,SOCK_DGRAM)", ifr
.ifr_addr
.sa_family
);
1234 printf("%s: ", name
);
1235 printb("flags", ifa
->ifa_flags
, IFFBITS
);
1236 if (ioctl(s
, SIOCGIFMETRIC
, &ifr
) != -1)
1238 printf(" metric %d", ifr
.ifr_metric
);
1239 if (ioctl(s
, SIOCGIFMTU
, &ifr
) != -1)
1240 printf(" mtu %d", ifr
.ifr_mtu
);
1241 if (showrtref
&& ioctl(s
, SIOCGIFGETRTREFCNT
, &ifr
) != -1)
1242 printf(" rtref %d", ifr
.ifr_route_refcnt
);
1244 unsigned int ifindex
= if_nametoindex(ifa
->ifa_name
);
1246 printf(" index %u", ifindex
);
1250 if (verbose
&& ioctl(s
, SIOCGIFEFLAGS
, (caddr_t
)&ifr
) != -1 &&
1251 (eflags
= ifr
.ifr_eflags
) != 0) {
1252 printb("\teflags", eflags
, IFEFBITS
);
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
);
1262 if (supmedia
&& ifr
.ifr_reqcap
!= 0) {
1263 printb("\tcapabilities", ifr
.ifr_reqcap
, IFCAPBITS
);
1270 for (ift
= ifa
; ift
!= NULL
; ift
= ift
->ifa_next
) {
1271 if (ift
->ifa_addr
== NULL
)
1273 if (strcmp(ifa
->ifa_name
, ift
->ifa_name
) != 0)
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
);
1284 if (allfamilies
|| afp
->af_af
== AF_LINK
) {
1285 const struct afswtch
*lafp
;
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.
1293 lafp
= af_getbyname("lladdr");
1295 info
.rti_info
[RTAX_IFA
] = (struct sockaddr
*)sdl
;
1296 lafp
->af_status(s
, &info
);
1302 else if (afp
->af_other_status
!= NULL
)
1303 afp
->af_other_status(s
);
1305 strncpy(ifs
.ifs_name
, name
, sizeof ifs
.ifs_name
);
1306 if (ioctl(s
, SIOCGIFSTATUS
, &ifs
) == 0)
1307 printf("%s", ifs
.ascii
);
1309 /* The rest is for when verbose is set; if not set, we're done */
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
);
1317 printf("\ttype: %s\n", c
);
1321 struct if_agentidsreq ifar
;
1322 memset(&ifar
, 0, sizeof(ifar
));
1324 strlcpy(ifar
.ifar_name
, name
, sizeof(ifar
.ifar_name
));
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
));
1335 uuid_copy(nar
.netagent_uuid
, ifar
.ifar_uuids
[agent_i
]);
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
);
1344 free(ifar
.ifar_uuids
);
1350 if (ioctl(s
, SIOCGIFLINKQUALITYMETRIC
, &ifr
) != -1) {
1351 int lqm
= ifr
.ifr_link_quality_metric
;
1353 printf("\tlink quality: %d ", lqm
);
1354 if (lqm
== IFNET_LQM_THRESH_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
)
1361 else if (lqm
> IFNET_LQM_THRESH_UNKNOWN
&&
1362 lqm
<= IFNET_LQM_THRESH_POOR
)
1364 else if (lqm
> IFNET_LQM_THRESH_POOR
&&
1365 lqm
<= IFNET_LQM_THRESH_GOOD
)
1370 } else if (lqm
> IFNET_LQM_THRESH_UNKNOWN
) {
1371 printf("\tlink quality: %d ", lqm
);
1372 if (lqm
<= IFNET_LQM_THRESH_BAD
)
1374 else if (lqm
<= IFNET_LQM_THRESH_POOR
)
1376 else if (lqm
<= IFNET_LQM_THRESH_GOOD
)
1385 if (ioctl(s
, SIOCGIFINTERFACESTATE
, &ifr
) != -1) {
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
;
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
)
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
;
1403 printf(" availability: %u ", ifavail
);
1404 if (ifavail
== IF_INTERFACE_STATE_INTERFACE_AVAILABLE
)
1406 else if (ifavail
== IF_INTERFACE_STATE_INTERFACE_UNAVAILABLE
)
1411 printf(" availability: (not valid)");
1414 ifr
.ifr_interface_state
.valid_bitmask
&
1415 IF_INTERFACE_STATE_LQM_STATE_VALID
) {
1416 int8_t lqm
= ifr
.ifr_interface_state
.lqm_state
;
1418 printf(" lqm: %d", lqm
);
1420 if (lqm
== IFNET_LQM_THRESH_OFF
)
1422 else if (lqm
== IFNET_LQM_THRESH_UNKNOWN
)
1423 printf("(unknown)");
1424 else if (lqm
== IFNET_LQM_THRESH_BAD
)
1426 else if (lqm
== IFNET_LQM_THRESH_POOR
)
1428 else if (lqm
== IFNET_LQM_THRESH_GOOD
)
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
;
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
;
1457 printf("\tscheduler: %s%s ",
1458 (flags
& IFLPRF_ALTQ
) ? "ALTQ_" : "",
1460 if (flags
& IFLPRF_DRVMANAGED
)
1461 printf("(driver managed)");
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
));
1473 if (obw_tbr
!= 0 && obw_eff
> obw_tbr
)
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
));
1482 printf("\tuplink rate: %s [eff] / ",
1483 bps_to_str(obw_eff
));
1486 printf("%s [tbr] / ",
1487 bps_to_str(obw_tbr
));
1489 printf("%s [tbr %u%%] / ",
1490 bps_to_str(obw_tbr
),
1493 printf("%s", bps_to_str(obw_max
));
1497 if (ibw_eff
== ibw_max
) {
1498 printf("\tdownlink rate: %s\n",
1499 bps_to_str(ibw_max
));
1501 printf("\tdownlink rate: "
1502 "%s [eff] / ", bps_to_str(ibw_eff
));
1503 printf("%s [max]\n",
1504 bps_to_str(ibw_max
));
1507 } else if (obw_tbr
!= 0) {
1508 printf("\tuplink rate: %s [tbr]\n",
1509 bps_to_str(obw_tbr
));
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
));
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
));
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
));
1540 /* Common OID prefix */
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");
1550 if (ifmsupp
.ifmd_data_extended
.ifi_alignerrs
!= 0) {
1551 printf("\tunaligned pkts: %llu\n",
1552 ifmsupp
.ifmd_data_extended
.ifi_alignerrs
);
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
));
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
);
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
);
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
);
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);
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");
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");
1602 case IFRTYPE_QOSMARKING_MODE_NONE
:
1606 printf("unknown (%u)\n", ifr
.ifr_qosmarking_mode
);
1611 #endif /* defined(SIOCGQOSMARKINGENABLED) && defined(SIOCGQOSMARKINGMODE) */
1617 #define KILOBYTES 1024
1618 #define MEGABYTES (KILOBYTES * KILOBYTES)
1619 #define GIGABYTES (KILOBYTES * KILOBYTES * KILOBYTES)
1622 bytes_to_str(unsigned long long bytes
)
1624 static char buf
[32];
1626 long double n
= bytes
, t
;
1628 if (bytes
>= GIGABYTES
) {
1631 } else if (n
>= MEGABYTES
) {
1634 } else if (n
>= KILOBYTES
) {
1642 snprintf(buf
, sizeof (buf
), "%-4.2Lf %s", t
, u
);
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 */
1651 bps_to_str(unsigned long long rate
)
1653 static char buf
[32];
1655 long double n
= rate
, t
;
1657 if (rate
>= GIGABIT_PER_SEC
) {
1658 t
= n
/ GIGABIT_PER_SEC
;
1660 } else if (n
>= MEGABIT_PER_SEC
) {
1661 t
= n
/ MEGABIT_PER_SEC
;
1663 } else if (n
>= KILOBIT_PER_SEC
) {
1664 t
= n
/ KILOBIT_PER_SEC
;
1671 snprintf(buf
, sizeof (buf
), "%-4.2Lf %4s", t
, u
);
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 */
1680 ns_to_str(unsigned long long nsec
)
1682 static char buf
[32];
1684 long double n
= nsec
, t
;
1686 if (nsec
>= NSEC_PER_SEC
) {
1687 t
= n
/ NSEC_PER_SEC
;
1689 } else if (n
>= USEC_PER_SEC
) {
1690 t
= n
/ USEC_PER_SEC
;
1692 } else if (n
>= MSEC_PER_SEC
) {
1693 t
= n
/ MSEC_PER_SEC
;
1700 snprintf(buf
, sizeof (buf
), "%-4.2Lf %4s", t
, u
);
1705 tunnel_status(int s
)
1707 af_all_tunnel_status(s
);
1711 Perror(const char *cmd
)
1716 errx(1, "%s: no such interface", cmd
);
1720 errx(1, "%s: permission denied", cmd
);
1729 * Print a value a la the %b format of the kernel's printf
1732 printb(const char *s
, unsigned v
, const char *bits
)
1737 if (bits
&& *bits
== 8)
1738 printf("%s=%o", s
, v
);
1740 printf("%s=%x", s
, v
);
1744 while ((i
= *bits
++) != '\0') {
1745 if (v
& (1 << (i
-1))) {
1749 for (; (c
= *bits
) > 32; bits
++)
1752 for (; *bits
> 32; bits
++)
1761 ifmaybeload(const char *name
)
1763 #define MOD_PREFIX_LEN 3 /* "if_" */
1764 struct module_stat mstat
;
1766 char ifkind
[IFNAMSIZ
+ MOD_PREFIX_LEN
], ifname
[IFNAMSIZ
], *dp
;
1769 /* loading suppressed by the user */
1773 /* trim the interface number off the end */
1774 strlcpy(ifname
, name
, sizeof(ifname
));
1775 for (dp
= ifname
; *dp
!= 0; dp
++)
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
);
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)
1794 /* strip bus name if present */
1795 if ((cp
= strchr(mstat
.name
, '/')) != NULL
) {
1800 /* already loaded? */
1801 if (strncmp(ifname
, cp
, strlen(ifname
) + 1) == 0 ||
1802 strncmp(ifkind
, cp
, strlen(ifkind
) + 1) == 0)
1807 /* not present, we should try to load it */
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
),
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
),
1829 #define EN_SWABIPS 0x1000
1830 DEF_CMD("swabips", EN_SWABIPS
, setifflags
),
1831 DEF_CMD("-swabips", -EN_SWABIPS
, setifflags
),
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
),
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 */
1855 DEF_CMD("rxcsum", IFCAP_RXCSUM
, setifcap
),
1856 DEF_CMD("-rxcsum", -IFCAP_RXCSUM
, setifcap
),
1857 #endif /* IFCAP_RXCSUM */
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 */
1871 DEF_CMD("tso", IFCAP_TSO
, setifcap
),
1872 DEF_CMD("-tso", -IFCAP_TSO
, setifcap
),
1873 #endif /* IFCAP_TSO */
1875 DEF_CMD("lro", IFCAP_LRO
, setifcap
),
1876 DEF_CMD("-lro", -IFCAP_LRO
, setifcap
),
1877 #endif /* IFCAP_LRO */
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
),
1899 DEF_CMD_ARG("name", setifname
),
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
),
1923 static __constructor
void
1926 #define N(a) (sizeof(a) / sizeof(a[0]))
1929 for (i
= 0; i
< N(basic_cmds
); i
++)
1930 cmd_register(&basic_cmds
[i
]);
1935 sched2str(unsigned int s
)
1940 case PKTSCHEDT_NONE
:
1949 case PKTSCHEDT_FQ_CODEL
:
1961 tl2str(unsigned int s
)
1966 case IFNET_THROTTLE_OFF
:
1969 case IFNET_THROTTLE_OPPORTUNISTIC
:
1970 c
= "opportunistic";
1981 ift2str(unsigned int t
, unsigned int f
, unsigned int sf
)
1983 static char buf
[256];
1989 case IFRTYPE_SUBFAMILY_USB
:
1992 case IFRTYPE_SUBFAMILY_BLUETOOTH
:
1993 c
= "Bluetooth PAN";
1995 case IFRTYPE_SUBFAMILY_WIFI
:
1998 case IFRTYPE_SUBFAMILY_THUNDERBOLT
:
1999 c
= "IP over Thunderbolt";
2001 case IFRTYPE_SUBFAMILY_ANY
:
2009 c
= "IP over FireWire";
2013 c
= "Packet capture";
2029 case IFT_IEEE8023ADLAG
:
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
);
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
);