2 * Copyright (c) 2009-2019 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>
103 #include <sysexits.h>
106 #include "ifconfig.h"
109 #include <TargetConditionals.h>
113 * Since "struct ifreq" is composed of various union members, callers
114 * should pay special attention to interprete the value.
115 * (.e.g. little/big endian difference in the structure.)
128 int bond_details
= 0;
130 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
133 #else /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
136 #endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
137 int printkeys
= 0; /* Print keying material for interfaces. */
139 static int ifconfig(int argc
, char *const *argv
, int iscreate
,
140 const struct afswtch
*afp
);
141 static void status(const struct afswtch
*afp
, const struct sockaddr_dl
*sdl
,
142 struct ifaddrs
*ifa
);
143 static char *bytes_to_str(unsigned long long bytes
);
144 static char *bps_to_str(unsigned long long rate
);
145 static char *ns_to_str(unsigned long long nsec
);
146 static void tunnel_status(int s
);
147 static void clat46_addr(int s
, char *name
);
148 static void nat64_status(int s
, char *name
);
149 static void usage(void);
150 static char *sched2str(unsigned int s
);
151 static char *tl2str(unsigned int s
);
152 static char *ift2str(unsigned int t
, unsigned int f
, unsigned int sf
);
153 static char *iffunct2str(u_int32_t functional_type
);
155 static struct afswtch
*af_getbyname(const char *name
);
156 static struct afswtch
*af_getbyfamily(int af
);
157 static void af_other_status(int);
159 static struct option
*opts
= NULL
;
162 opt_register(struct option
*p
)
174 /* XXX not right but close enough for now */
176 for (p
= opts
; p
!= NULL
; p
= p
->next
) {
177 strlcat(options
, p
->opt_usage
, sizeof(options
));
178 strlcat(options
, " ", sizeof(options
));
182 "usage: ifconfig %sinterface address_family [address [dest_address]]\n"
184 " ifconfig interface create\n"
185 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
186 " ifconfig -l [-d] [-u] [address_family]\n"
187 " ifconfig %s[-d] [-m] [-u] [-v]\n",
188 options
, options
, options
);
193 main(int argc
, char *argv
[])
195 int c
, namesonly
, downonly
, uponly
;
196 const struct afswtch
*afp
= NULL
;
198 struct ifaddrs
*ifap
, *ifa
;
200 const struct sockaddr_dl
*sdl
;
201 char options
[1024], *cp
;
206 all
= downonly
= uponly
= namesonly
= noload
= 0;
208 /* Parse leading line options */
210 strlcpy(options
, "adklmnuv", sizeof(options
));
212 strlcpy(options
, "abdlmruv", sizeof(options
));
214 for (p
= opts
; p
!= NULL
; p
= p
->next
)
215 strlcat(options
, p
->opt
, sizeof(options
));
216 while ((c
= getopt(argc
, argv
, options
)) != -1) {
218 case 'a': /* scan all interfaces */
221 case 'b': /* bond detailed output */
224 case 'd': /* restrict scan to "down" interfaces */
232 case 'l': /* scan interface names only */
235 case 'm': /* show media choices in status */
239 case 'n': /* suppress module loading */
246 case 'u': /* restrict scan to "up" interfaces */
253 for (p
= opts
; p
!= NULL
; p
= p
->next
)
254 if (p
->opt
[0] == c
) {
266 /* -l cannot be used with -a or -q or -m or -b */
268 (all
|| supmedia
|| bond_details
))
272 if (uponly
&& downonly
)
275 /* no arguments is equivalent to '-a' */
276 if (!namesonly
&& argc
< 1)
279 /* -a and -l allow an address family arg to limit the output */
280 if (all
|| namesonly
) {
286 afp
= af_getbyname(*argv
);
289 if (afp
->af_name
!= NULL
)
291 /* leave with afp non-zero */
294 /* not listing, need an argument */
302 /* check and maybe load support for this interface */
305 ifindex
= if_nametoindex(ifname
);
308 * NOTE: We must special-case the `create' command
309 * right here as we would otherwise fail when trying
310 * to find the interface.
312 if (argc
> 0 && (strcmp(argv
[0], "create") == 0 ||
313 strcmp(argv
[0], "plumb") == 0)) {
314 iflen
= strlcpy(name
, ifname
, sizeof(name
));
315 if (iflen
>= sizeof(name
))
316 errx(1, "%s: cloning name too long",
318 ifconfig(argc
, argv
, 1, NULL
);
321 errx(1, "interface %s does not exist", ifname
);
325 /* Check for address family */
327 afp
= af_getbyname(*argv
);
332 if (getifaddrs(&ifap
) != 0)
333 err(EXIT_FAILURE
, "getifaddrs");
336 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
337 memset(&paifr
, 0, sizeof(paifr
));
338 strlcpy(paifr
.ifr_name
, ifa
->ifa_name
, sizeof(paifr
.ifr_name
));
339 if (sizeof(paifr
.ifr_addr
) >= ifa
->ifa_addr
->sa_len
) {
340 memcpy(&paifr
.ifr_addr
, ifa
->ifa_addr
,
341 ifa
->ifa_addr
->sa_len
);
344 if (ifname
!= NULL
&& strcmp(ifname
, ifa
->ifa_name
) != 0)
346 if (ifa
->ifa_addr
->sa_family
== AF_LINK
)
347 sdl
= (const struct sockaddr_dl
*) ifa
->ifa_addr
;
350 if (cp
!= NULL
&& strcmp(cp
, ifa
->ifa_name
) == 0)
352 iflen
= strlcpy(name
, ifa
->ifa_name
, sizeof(name
));
353 if (iflen
>= sizeof(name
)) {
354 warnx("%s: interface name too long, skipping",
360 if (downonly
&& (ifa
->ifa_flags
& IFF_UP
) != 0)
362 if (uponly
&& (ifa
->ifa_flags
& IFF_UP
) == 0)
366 * Are we just listing the interfaces?
376 ifconfig(argc
, argv
, 0, afp
);
378 status(afp
, sdl
, ifa
);
387 static struct afswtch
*afs
= NULL
;
390 af_register(struct afswtch
*p
)
396 static struct afswtch
*
397 af_getbyname(const char *name
)
401 for (afp
= afs
; afp
!= NULL
; afp
= afp
->af_next
)
402 if (strcmp(afp
->af_name
, name
) == 0)
407 static struct afswtch
*
408 af_getbyfamily(int af
)
412 for (afp
= afs
; afp
!= NULL
; afp
= afp
->af_next
)
413 if (afp
->af_af
== af
)
419 af_other_status(int s
)
422 uint8_t afmask
[howmany(AF_MAX
, NBBY
)];
424 memset(afmask
, 0, sizeof(afmask
));
425 for (afp
= afs
; afp
!= NULL
; afp
= afp
->af_next
) {
426 if (afp
->af_other_status
== NULL
)
428 if (afp
->af_af
!= AF_UNSPEC
&& isset(afmask
, afp
->af_af
))
430 afp
->af_other_status(s
);
431 setbit(afmask
, afp
->af_af
);
436 af_all_tunnel_status(int s
)
439 uint8_t afmask
[howmany(AF_MAX
, NBBY
)];
441 memset(afmask
, 0, sizeof(afmask
));
442 for (afp
= afs
; afp
!= NULL
; afp
= afp
->af_next
) {
443 if (afp
->af_status_tunnel
== NULL
)
445 if (afp
->af_af
!= AF_UNSPEC
&& isset(afmask
, afp
->af_af
))
447 afp
->af_status_tunnel(s
);
448 setbit(afmask
, afp
->af_af
);
452 static struct cmd
*cmds
= NULL
;
455 cmd_register(struct cmd
*p
)
461 static const struct cmd
*
462 cmd_lookup(const char *name
)
464 #define N(a) (sizeof(a)/sizeof(a[0]))
467 for (p
= cmds
; p
!= NULL
; p
= p
->c_next
)
468 if (strcmp(name
, p
->c_name
) == 0)
475 callback_func
*cb_func
;
477 struct callback
*cb_next
;
479 static struct callback
*callbacks
= NULL
;
482 callback_register(callback_func
*func
, void *arg
)
486 cb
= malloc(sizeof(struct callback
));
488 errx(1, "unable to allocate memory for callback");
491 cb
->cb_next
= callbacks
;
495 /* specially-handled commands */
496 static void setifaddr(const char *, int, int, const struct afswtch
*);
497 static const struct cmd setifaddr_cmd
= DEF_CMD("ifaddr", 0, setifaddr
);
499 static void setifdstaddr(const char *, int, int, const struct afswtch
*);
500 static const struct cmd setifdstaddr_cmd
=
501 DEF_CMD("ifdstaddr", 0, setifdstaddr
);
504 ifconfig(int argc
, char *const *argv
, int iscreate
, const struct afswtch
*afp
)
506 const struct afswtch
*nafp
;
510 strlcpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
513 afp
= af_getbyname("inet");
514 ifr
.ifr_addr
.sa_family
=
515 afp
->af_af
== AF_LINK
|| afp
->af_af
== AF_UNSPEC
?
516 AF_INET
: afp
->af_af
;
518 if ((s
= socket(ifr
.ifr_addr
.sa_family
, SOCK_DGRAM
, 0)) < 0)
519 err(1, "socket(family %u,SOCK_DGRAM", ifr
.ifr_addr
.sa_family
);
524 p
= cmd_lookup(*argv
);
527 * Not a recognized command, choose between setting
528 * the interface address and the dst address.
530 p
= (setaddr
? &setifdstaddr_cmd
: &setifaddr_cmd
);
532 if (p
->c_u
.c_func
|| p
->c_u
.c_func2
) {
533 if (iscreate
&& !p
->c_iscloneop
) {
535 * Push the clone create callback so the new
536 * device is created and can be used for any
537 * remaining arguments.
541 errx(1, "internal error, no callback");
542 callbacks
= cb
->cb_next
;
543 cb
->cb_func(s
, cb
->cb_arg
);
546 * Handle any address family spec that
547 * immediately follows and potentially
548 * recreate the socket.
550 nafp
= af_getbyname(*argv
);
560 if (p
->c_parameter
== NEXTARG
) {
562 errx(1, "'%s' requires argument",
564 p
->c_u
.c_func(argv
[1], 0, s
, afp
);
566 } else if (p
->c_parameter
== OPTARG
) {
567 p
->c_u
.c_func(argv
[1], 0, s
, afp
);
570 } else if (p
->c_parameter
== NEXTARG2
) {
572 errx(1, "'%s' requires 2 arguments",
574 p
->c_u
.c_func2(argv
[1], argv
[2], s
, afp
);
575 argc
-= 2, argv
+= 2;
576 } else if (p
->c_parameter
== VAARGS
) {
577 ret
= p
->c_u
.c_funcv(argc
- 1, argv
+ 1, s
, afp
);
579 errx(1, "'%s' command error",
581 argc
-= ret
, argv
+= ret
;
583 p
->c_u
.c_func(*argv
, p
->c_parameter
, s
, afp
);
590 * Do any post argument processing required by the address family.
592 if (afp
->af_postproc
!= NULL
)
593 afp
->af_postproc(s
, afp
);
595 * Do deferred callbacks registered while processing
596 * command-line arguments.
598 for (cb
= callbacks
; cb
!= NULL
; cb
= cb
->cb_next
)
599 cb
->cb_func(s
, cb
->cb_arg
);
601 * Do deferred operations.
604 if (afp
->af_ridreq
== NULL
|| afp
->af_difaddr
== 0) {
605 warnx("interface %s cannot change %s addresses!",
611 strlcpy(afp
->af_ridreq
, name
, sizeof ifr
.ifr_name
);
612 ret
= ioctl(s
, afp
->af_difaddr
, afp
->af_ridreq
);
614 if (errno
== EADDRNOTAVAIL
&& (doalias
>= 0)) {
615 /* means no previous address for interface */
617 Perror("ioctl (SIOCDIFADDR)");
621 if (afp
->af_addreq
== NULL
|| afp
->af_aifaddr
== 0) {
622 warnx("interface %s cannot change %s addresses!",
627 if (newaddr
&& (setaddr
|| setmask
)) {
628 strlcpy(afp
->af_addreq
, name
, sizeof ifr
.ifr_name
);
629 if (ioctl(s
, afp
->af_aifaddr
, afp
->af_addreq
) < 0)
630 Perror("ioctl (SIOCAIFADDR)");
639 setifaddr(const char *addr
, int param
, int s
, const struct afswtch
*afp
)
641 if (afp
->af_getaddr
== NULL
)
644 * Delay the ioctl to set the interface addr until flags are all set.
645 * The address interpretation may depend on the flags,
646 * and the flags may change when the address is set.
649 if (doalias
== 0 && afp
->af_af
!= AF_LINK
)
651 afp
->af_getaddr(addr
, (doalias
>= 0 ? ADDR
: RIDADDR
));
655 settunnel(const char *src
, const char *dst
, int s
, const struct afswtch
*afp
)
657 struct addrinfo
*srcres
, *dstres
;
660 if (afp
->af_settunnel
== NULL
) {
661 warn("address family %s does not support tunnel setup",
666 if ((ecode
= getaddrinfo(src
, NULL
, NULL
, &srcres
)) != 0)
667 errx(1, "error in parsing address string: %s",
668 gai_strerror(ecode
));
670 if ((ecode
= getaddrinfo(dst
, NULL
, NULL
, &dstres
)) != 0)
671 errx(1, "error in parsing address string: %s",
672 gai_strerror(ecode
));
674 if (srcres
->ai_addr
->sa_family
!= dstres
->ai_addr
->sa_family
)
676 "source and destination address families do not match");
678 afp
->af_settunnel(s
, srcres
, dstres
);
680 freeaddrinfo(srcres
);
681 freeaddrinfo(dstres
);
686 deletetunnel(const char *vname
, int param
, int s
, const struct afswtch
*afp
)
689 if (ioctl(s
, SIOCDIFPHYADDR
, &ifr
) < 0)
690 err(1, "SIOCDIFPHYADDR");
694 setifnetmask(const char *addr
, int dummy __unused
, int s
,
695 const struct afswtch
*afp
)
697 if (afp
->af_getaddr
!= NULL
) {
699 afp
->af_getaddr(addr
, MASK
);
704 setifbroadaddr(const char *addr
, int dummy __unused
, int s
,
705 const struct afswtch
*afp
)
707 if (afp
->af_getaddr
!= NULL
)
708 afp
->af_getaddr(addr
, DSTADDR
);
712 setifipdst(const char *addr
, int dummy __unused
, int s
,
713 const struct afswtch
*afp
)
715 const struct afswtch
*inet
;
717 inet
= af_getbyname("inet");
720 inet
->af_getaddr(addr
, DSTADDR
);
726 notealias(const char *addr
, int param
, int s
, const struct afswtch
*afp
)
728 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
729 if (setaddr
&& doalias
== 0 && param
< 0)
730 if (afp
->af_addreq
!= NULL
&& afp
->af_ridreq
!= NULL
)
731 bcopy((caddr_t
)rqtosa(af_addreq
),
732 (caddr_t
)rqtosa(af_ridreq
),
733 rqtosa(af_addreq
)->sa_len
);
745 setifdstaddr(const char *addr
, int param __unused
, int s
,
746 const struct afswtch
*afp
)
748 if (afp
->af_getaddr
!= NULL
)
749 afp
->af_getaddr(addr
, DSTADDR
);
753 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
754 * of the ifreq structure, which may confuse other parts of ifconfig.
755 * Make a private copy so we can avoid that.
758 setifflags(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
763 bcopy((char *)&ifr
, (char *)&my_ifr
, sizeof(struct ifreq
));
765 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&my_ifr
) < 0) {
766 Perror("ioctl (SIOCGIFFLAGS)");
769 strlcpy(my_ifr
.ifr_name
, name
, sizeof (my_ifr
.ifr_name
));
770 flags
= my_ifr
.ifr_flags
;
777 my_ifr
.ifr_flags
= flags
& 0xffff;
778 if (ioctl(s
, SIOCSIFFLAGS
, (caddr_t
)&my_ifr
) < 0)
783 setifcap(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
787 if (ioctl(s
, SIOCGIFCAP
, (caddr_t
)&ifr
) < 0) {
788 Perror("ioctl (SIOCGIFCAP)");
791 flags
= ifr
.ifr_curcap
;
797 flags
&= ifr
.ifr_reqcap
;
798 ifr
.ifr_reqcap
= flags
;
799 if (ioctl(s
, SIOCSIFCAP
, (caddr_t
)&ifr
) < 0)
804 setifmetric(const char *val
, int dummy __unused
, int s
,
805 const struct afswtch
*afp
)
807 strlcpy(ifr
.ifr_name
, name
, sizeof (ifr
.ifr_name
));
808 ifr
.ifr_metric
= atoi(val
);
809 if (ioctl(s
, SIOCSIFMETRIC
, (caddr_t
)&ifr
) < 0)
810 warn("ioctl (set metric)");
814 setifmtu(const char *val
, int dummy __unused
, int s
,
815 const struct afswtch
*afp
)
817 strlcpy(ifr
.ifr_name
, name
, sizeof (ifr
.ifr_name
));
818 ifr
.ifr_mtu
= atoi(val
);
819 if (ioctl(s
, SIOCSIFMTU
, (caddr_t
)&ifr
) < 0)
820 warn("ioctl (set mtu)");
825 setifname(const char *val
, int dummy __unused
, int s
,
826 const struct afswtch
*afp
)
830 newname
= strdup(val
);
831 if (newname
== NULL
) {
832 warn("no memory to set ifname");
835 ifr
.ifr_data
= newname
;
836 if (ioctl(s
, SIOCSIFNAME
, (caddr_t
)&ifr
) < 0) {
837 warn("ioctl (set name)");
841 strlcpy(name
, newname
, sizeof(name
));
847 setrouter(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
849 if (afp
->af_setrouter
== NULL
) {
850 warn("address family %s does not support router mode",
855 afp
->af_setrouter(s
, value
);
859 routermode(int argc
, char *const *argv
, int s
, const struct afswtch
*afp
)
861 return (*afp
->af_routermode
)(s
, argc
, argv
);
866 setifdesc(const char *val
, int dummy __unused
, int s
, const struct afswtch
*afp
)
868 struct if_descreq ifdr
;
870 bzero(&ifdr
, sizeof (ifdr
));
871 strlcpy(ifdr
.ifdr_name
, name
, sizeof (ifdr
.ifdr_name
));
872 ifdr
.ifdr_len
= strlen(val
);
873 strlcpy((char *)ifdr
.ifdr_desc
, val
, sizeof (ifdr
.ifdr_desc
));
875 if (ioctl(s
, SIOCSIFDESC
, (caddr_t
)&ifdr
) < 0) {
876 warn("ioctl (set desc)");
881 settbr(const char *val
, int dummy __unused
, int s
, const struct afswtch
*afp
)
883 struct if_linkparamsreq iflpr
;
886 u_int32_t percent
= 0;
890 bzero(&iflpr
, sizeof (iflpr
));
891 strlcpy(iflpr
.iflpr_name
, name
, sizeof (iflpr
.iflpr_name
));
893 bps
= strtold(val
, &cp
);
894 if (val
== cp
|| errno
!= 0) {
895 warn("Invalid value '%s'", val
);
898 rate
= (u_int64_t
)bps
;
900 if (!strcmp(cp
, "b") || !strcmp(cp
, "bps")) {
902 } else if (!strcmp(cp
, "Kb") || !strcmp(cp
, "Kbps")) {
904 } else if (!strcmp(cp
, "Mb") || !strcmp(cp
, "Mbps")) {
906 } else if (!strcmp(cp
, "Gb") || !strcmp(cp
, "Gbps")) {
907 rate
*= 1000 * 1000 * 1000;
908 } else if (!strcmp(cp
, "%")) {
910 if (percent
== 0 || percent
> 100) {
911 printf("Value out of range '%s'", val
);
914 } else if (*cp
!= '\0') {
915 printf("Unknown unit '%s'", cp
);
919 iflpr
.iflpr_output_tbr_rate
= rate
;
920 iflpr
.iflpr_output_tbr_percent
= percent
;
921 if (ioctl(s
, SIOCSIFLINKPARAMS
, &iflpr
) < 0 &&
922 errno
!= ENOENT
&& errno
!= ENXIO
&& errno
!= ENODEV
) {
923 warn("ioctl (set link params)");
924 } else if (errno
== ENXIO
) {
925 printf("TBR cannot be set on %s\n", name
);
926 } else if (errno
== 0 && rate
== 0) {
927 printf("%s: TBR is now disabled\n", name
);
928 } else if (errno
== ENODEV
) {
929 printf("%s: requires absolute TBR rate\n", name
);
930 } else if (percent
!= 0) {
931 printf("%s: TBR rate set to %u%% of effective link rate\n",
934 printf("%s: TBR rate set to %s\n", name
, bps_to_str(rate
));
939 get_int64(uint64_t *i
, char const *s
)
942 *i
= strtol(s
, &cp
, 10);
943 if (cp
== s
|| errno
!= 0) {
950 get_int32(uint32_t *i
, char const *s
)
953 *i
= strtol(s
, &cp
, 10);
954 if (cp
== s
|| errno
!= 0) {
961 get_percent(double *d
, const char *s
)
964 *d
= strtod(s
, &cp
) / (double)100;
965 if (*d
== HUGE_VALF
|| *d
== HUGE_VALL
) {
968 if (*d
== 0.0 || (*cp
!= '\0' && strcmp(cp
, "%") != 0)) {
975 get_percent_fixed_point(uint32_t *i
, const char *s
)
979 if (get_percent(&p
, s
) != 0){
983 *i
= p
* IF_NETEM_PARAMS_PSCALE
;
988 netem_parse_args(struct if_netem_params
*p
, int argc
, char *const *argv
)
990 int argc_saved
= argc
;
991 uint64_t bandwitdh
= 0;
992 uint32_t latency
= 0, jitter
= 0;
993 uint32_t corruption
= 0;
994 uint32_t duplication
= 0;
995 uint32_t loss_p_gr_gl
= 0, loss_p_gr_bl
= 0, loss_p_bl_br
= 0,
996 loss_p_bl_gr
= 0, loss_p_br_bl
= 0;
997 uint32_t reordering
= 0;
999 bzero(p
, sizeof (*p
));
1001 /* take out "input"/"output" */
1004 for ( ; argc
> 0; ) {
1005 if (strcmp(*argv
, "bandwidth") == 0) {
1007 if (argc
<= 0 || get_int64(&bandwitdh
, *argv
) != 0) {
1008 err(1, "Invalid value '%s'", *argv
);
1011 } else if (strcmp(*argv
, "corruption") == 0) {
1014 get_percent_fixed_point(&corruption
, *argv
) != 0) {
1015 err(1, "Invalid value '%s'", *argv
);
1018 } else if (strcmp(*argv
, "delay") == 0) {
1020 if (argc
<= 0 || get_int32(&latency
, *argv
) != 0) {
1021 err(1, "Invalid value '%s'", *argv
);
1024 if (argc
> 0 && get_int32(&jitter
, *argv
) == 0) {
1027 } else if (strcmp(*argv
, "duplication") == 0) {
1030 get_percent_fixed_point(&duplication
, *argv
) != 0) {
1031 err(1, "Invalid value '%s'", *argv
);
1035 } else if (strcmp(*argv
, "loss") == 0) {
1037 if (argc
<= 0 || get_percent_fixed_point(&loss_p_gr_gl
, *argv
) != 0) {
1038 err(1, "Invalid value '%s'", *argv
);
1040 /* we may have all 5 probs, use naive model if not */
1042 if (argc
<= 0 || get_percent_fixed_point(&loss_p_gr_bl
, *argv
) != 0) {
1045 /* if more than p_gr_gl, then should have all probs */
1047 if (argc
<= 0 || get_percent_fixed_point(&loss_p_bl_br
, *argv
) != 0) {
1048 err(1, "Invalid value '%s' for p_bl_br", *argv
);
1051 if (argc
<= 0 || get_percent_fixed_point(&loss_p_bl_gr
, *argv
) != 0) {
1052 err(1, "Invalid value '%s' for p_bl_gr", *argv
);
1055 if (argc
<= 0 || get_percent_fixed_point(&loss_p_br_bl
, *argv
) != 0) {
1056 err(1, "Invalid value '%s' for p_br_bl", *argv
);
1059 } else if (strcmp(*argv
, "reordering") == 0) {
1061 if (argc
<= 0 || get_percent_fixed_point(&reordering
, *argv
) != 0) {
1062 err(1, "Invalid value '%s'", *argv
);
1070 if (corruption
> IF_NETEM_PARAMS_PSCALE
) {
1071 err(1, "corruption percentage > 100%%");
1074 if (duplication
> IF_NETEM_PARAMS_PSCALE
) {
1075 err(1, "duplication percentage > 100%%");
1078 if (duplication
> 0 && latency
== 0) {
1079 /* we need to insert dup'ed packet with latency */
1080 err(1, "duplication needs latency param");
1083 if (latency
> 1000) {
1084 err(1, "latency %dms too big (> 1 sec)", latency
);
1087 if (jitter
* 3 > latency
) {
1088 err(1, "jitter %dms too big (latency %dms)", jitter
, latency
);
1091 /* if gr_gl == 0 (no loss), other prob should all be zero */
1092 if (loss_p_gr_gl
== 0 &&
1093 (loss_p_gr_bl
!= 0 || loss_p_bl_br
!= 0 || loss_p_bl_gr
!= 0 ||
1094 loss_p_br_bl
!= 0)) {
1095 err(1, "loss params not all zero when gr_gl is zero");
1098 /* check state machine transition prob integrity */
1099 if (loss_p_gr_gl
> IF_NETEM_PARAMS_PSCALE
||
1100 /* gr_gl = IF_NETEM_PARAMS_PSCALE for total loss */
1101 loss_p_gr_bl
> IF_NETEM_PARAMS_PSCALE
||
1102 loss_p_bl_br
> IF_NETEM_PARAMS_PSCALE
||
1103 loss_p_bl_gr
> IF_NETEM_PARAMS_PSCALE
||
1104 loss_p_br_bl
> IF_NETEM_PARAMS_PSCALE
||
1105 loss_p_gr_gl
+ loss_p_gr_bl
> IF_NETEM_PARAMS_PSCALE
||
1106 loss_p_bl_br
+ loss_p_bl_gr
> IF_NETEM_PARAMS_PSCALE
) {
1107 err(1, "loss params too big");
1110 if (reordering
> IF_NETEM_PARAMS_PSCALE
) {
1111 err(1, "reordering percentage > 100%%");
1114 p
->ifnetem_bandwidth_bps
= bandwitdh
;
1115 p
->ifnetem_latency_ms
= latency
;
1116 p
->ifnetem_jitter_ms
= jitter
;
1117 p
->ifnetem_corruption_p
= corruption
;
1118 p
->ifnetem_duplication_p
= duplication
;
1119 p
->ifnetem_loss_p_gr_gl
= loss_p_gr_gl
;
1120 p
->ifnetem_loss_p_gr_bl
= loss_p_gr_bl
;
1121 p
->ifnetem_loss_p_bl_br
= loss_p_bl_br
;
1122 p
->ifnetem_loss_p_bl_gr
= loss_p_bl_gr
;
1123 p
->ifnetem_loss_p_br_bl
= loss_p_br_bl
;
1124 p
->ifnetem_reordering_p
= reordering
;
1126 return (argc_saved
- argc
);
1130 print_netem_params(struct if_netem_params
*p
, const char *desc
)
1132 struct if_netem_params zero_params
;
1133 double pscale
= IF_NETEM_PARAMS_PSCALE
/ 100;
1134 bzero(&zero_params
, sizeof (zero_params
));
1136 if (memcmp(p
, &zero_params
, sizeof (zero_params
)) == 0) {
1137 printf("%s NetEm Disabled\n\n", desc
);
1140 "%s NetEm Parameters\n"
1141 "\tbandwidth rate %llubps\n"
1142 "\tdelay latency %dms\n"
1144 desc
, p
->ifnetem_bandwidth_bps
,
1145 p
->ifnetem_latency_ms
, p
->ifnetem_jitter_ms
);
1146 if (p
->ifnetem_loss_p_gr_bl
== 0 &&
1147 p
->ifnetem_loss_p_bl_br
== 0 &&
1148 p
->ifnetem_loss_p_bl_gr
== 0 &&
1149 p
->ifnetem_loss_p_br_bl
== 0) {
1152 (double) p
->ifnetem_loss_p_gr_gl
/ pscale
);
1155 "\tloss GAP_RECV -> GAP_LOSS %.3f%%\n"
1156 "\t GAP_RECV -> BURST_LOSS %.3f%%\n"
1157 "\t BURST_LOSS -> BURST_RECV %.3f%%\n"
1158 "\t BURST_LOSS -> GAP_RECV %.3f%%\n"
1159 "\t BURST_RECV -> BURST_LOSS %.3f%%\n",
1160 (double) p
->ifnetem_loss_p_gr_gl
/ pscale
,
1161 (double) p
->ifnetem_loss_p_gr_bl
/ pscale
,
1162 (double) p
->ifnetem_loss_p_bl_br
/ pscale
,
1163 (double) p
->ifnetem_loss_p_bl_gr
/ pscale
,
1164 (double) p
->ifnetem_loss_p_br_bl
/ pscale
);
1167 "\tcorruption %.3f%%\n"
1168 "\treordering %.3f%%\n\n",
1169 (double) p
->ifnetem_corruption_p
/ pscale
,
1170 (double) p
->ifnetem_reordering_p
/ pscale
);
1175 setnetem(int argc
, char *const *argv
, int s
, const struct afswtch
*afp
)
1177 struct if_linkparamsreq iflpr
;
1178 struct if_netem_params input_params
, output_params
;
1179 int ret
= 0, error
= 0;
1181 bzero(&iflpr
, sizeof (iflpr
));
1182 bzero(&input_params
, sizeof (input_params
));
1183 bzero(&output_params
, sizeof (output_params
));
1186 if (strcmp(argv
[0], "input") == 0) {
1187 ret
= netem_parse_args(&input_params
, argc
, argv
);
1188 } else if (strcmp(argv
[0], "output") == 0) {
1189 ret
= netem_parse_args(&output_params
, argc
, argv
);
1190 } else if (strcmp(argv
[0], "-h") == 0 || strcmp(argv
[0], "--help") == 0) {
1193 fprintf(stderr
, "uknown option %s\n", argv
[0]);
1202 strlcpy(iflpr
.iflpr_name
, name
, sizeof (iflpr
.iflpr_name
));
1203 error
= ioctl(s
, SIOCGIFLINKPARAMS
, &iflpr
);
1205 warn("ioctl (get link params)");
1209 print_netem_params(&iflpr
.iflpr_input_netem
, "Input");
1210 print_netem_params(&iflpr
.iflpr_output_netem
, "Output");
1212 } else if (argc
== 1) {
1213 if (strcmp(argv
[0], "input") == 0) {
1214 bzero(&iflpr
.iflpr_input_netem
,
1215 sizeof (iflpr
.iflpr_input_netem
));
1216 } else if (strcmp(argv
[0], "output") == 0) {
1217 bzero(&iflpr
.iflpr_output_netem
,
1218 sizeof (iflpr
.iflpr_output_netem
));
1220 fprintf(stderr
, "uknown option %s\n", argv
[0]);
1223 printf("%s: netem is now disabled for %s\n", name
, argv
[0]);
1226 if (strcmp(argv
[0], "input") == 0) {
1227 iflpr
.iflpr_input_netem
= input_params
;
1228 } else if (strcmp(argv
[0], "output") == 0) {
1229 iflpr
.iflpr_output_netem
= output_params
;
1233 error
= ioctl(s
, SIOCSIFLINKPARAMS
, &iflpr
);
1234 if (error
< 0 && errno
!= ENOENT
&& errno
!= ENXIO
&& errno
!= ENODEV
) {
1235 warn("ioctl (set link params)");
1236 } else if (errno
== ENXIO
) {
1237 printf("netem cannot be set on %s\n", name
);
1239 printf("%s: netem configured\n", name
);
1244 fprintf(stderr
, "Usage:\n"
1245 "\tTo enable/set netem params\n"
1246 "\t\tnetem <input|output>\n"
1247 "\t\t [ bandwidth BIT_PER_SEC ]\n"
1248 "\t\t [ delay DELAY_MSEC [ JITTER_MSEC ] ]\n"
1249 "\t\t [ loss PERCENTAGE ]\n"
1250 "\t\t [ duplication PERCENTAGE ]\n"
1251 "\t\t [ reordering PERCENTAGE ]\n\n"
1252 "\tTo disable <input|output> netem\n"
1253 "\t\tnetem <input|output>\n\n"
1254 "\tTo show current settings\n"
1260 setthrottle(const char *val
, int dummy __unused
, int s
,
1261 const struct afswtch
*afp
)
1263 struct if_throttlereq iftr
;
1267 bzero(&iftr
, sizeof (iftr
));
1268 strlcpy(iftr
.ifthr_name
, name
, sizeof (iftr
.ifthr_name
));
1270 iftr
.ifthr_level
= strtold(val
, &cp
);
1271 if (val
== cp
|| errno
!= 0) {
1272 warn("Invalid value '%s'", val
);
1276 if (ioctl(s
, SIOCSIFTHROTTLE
, &iftr
) < 0 && errno
!= ENXIO
) {
1277 warn("ioctl (set throttling level)");
1278 } else if (errno
== ENXIO
) {
1279 printf("throttling level cannot be set on %s\n", name
);
1281 printf("%s: throttling level set to %d\n", name
,
1287 setdisableoutput(const char *val
, int dummy __unused
, int s
,
1288 const struct afswtch
*afp
)
1293 bzero(&ifr
, sizeof (ifr
));
1294 strlcpy(ifr
.ifr_name
, name
, sizeof (ifr
.ifr_name
));
1296 ifr
.ifr_ifru
.ifru_disable_output
= strtold(val
, &cp
);
1297 if (val
== cp
|| errno
!= 0) {
1298 warn("Invalid value '%s'", val
);
1302 if (ioctl(s
, SIOCSIFDISABLEOUTPUT
, &ifr
) < 0 && errno
!= ENXIO
) {
1303 warn("ioctl set disable output");
1304 } else if (errno
== ENXIO
) {
1305 printf("output thread can not be disabled on %s\n", name
);
1307 printf("output %s on %s\n",
1308 ((ifr
.ifr_ifru
.ifru_disable_output
== 0) ? "enabled" : "disabled"),
1314 setlog(const char *val
, int dummy __unused
, int s
,
1315 const struct afswtch
*afp
)
1320 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1322 ifr
.ifr_log
.ifl_level
= strtold(val
, &cp
);
1323 if (val
== cp
|| errno
!= 0) {
1324 warn("Invalid value '%s'", val
);
1327 ifr
.ifr_log
.ifl_flags
= (IFRLOGF_DLIL
|IFRLOGF_FAMILY
|IFRLOGF_DRIVER
|
1330 if (ioctl(s
, SIOCSIFLOG
, &ifr
) < 0)
1331 warn("ioctl (set logging parameters)");
1335 setcl2k(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1337 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1338 ifr
.ifr_ifru
.ifru_2kcl
= value
;
1340 if (ioctl(s
, SIOCSIF2KCL
, (caddr_t
)&ifr
) < 0)
1345 setexpensive(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1347 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1348 ifr
.ifr_ifru
.ifru_expensive
= value
;
1350 if (ioctl(s
, SIOCSIFEXPENSIVE
, (caddr_t
)&ifr
) < 0)
1354 #ifdef SIOCSIFCONSTRAINED
1356 setconstrained(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1358 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1359 ifr
.ifr_ifru
.ifru_constrained
= value
;
1361 if (ioctl(s
, SIOCSIFCONSTRAINED
, (caddr_t
)&ifr
) < 0)
1367 setifmpklog(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1369 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1370 ifr
.ifr_ifru
.ifru_mpk_log
= value
;
1372 if (ioctl(s
, SIOCSIFMPKLOG
, (caddr_t
)&ifr
) < 0)
1377 settimestamp(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1379 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1382 if (ioctl(s
, SIOCSIFTIMESTAMPDISABLE
, (caddr_t
)&ifr
) < 0)
1385 if (ioctl(s
, SIOCSIFTIMESTAMPENABLE
, (caddr_t
)&ifr
) < 0)
1391 setecnmode(const char *val
, int dummy __unused
, int s
,
1392 const struct afswtch
*afp
)
1396 if (strcmp(val
, "default") == 0)
1397 ifr
.ifr_ifru
.ifru_ecn_mode
= IFRTYPE_ECN_DEFAULT
;
1398 else if (strcmp(val
, "enable") == 0)
1399 ifr
.ifr_ifru
.ifru_ecn_mode
= IFRTYPE_ECN_ENABLE
;
1400 else if (strcmp(val
, "disable") == 0)
1401 ifr
.ifr_ifru
.ifru_ecn_mode
= IFRTYPE_ECN_DISABLE
;
1403 ifr
.ifr_ifru
.ifru_ecn_mode
= strtold(val
, &cp
);
1404 if (val
== cp
|| errno
!= 0) {
1405 warn("Invalid ECN mode value '%s'", val
);
1410 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1412 if (ioctl(s
, SIOCSECNMODE
, (caddr_t
)&ifr
) < 0)
1413 Perror("ioctl(SIOCSECNMODE)");
1417 setprobeconnectivity(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1419 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1420 ifr
.ifr_ifru
.ifru_probe_connectivity
= value
;
1422 if (ioctl(s
, SIOCSIFPROBECONNECTIVITY
, (caddr_t
)&ifr
) < 0)
1426 #if defined(SIOCSQOSMARKINGMODE) && defined(SIOCSQOSMARKINGENABLED)
1429 setqosmarking(const char *cmd
, const char *arg
, int s
, const struct afswtch
*afp
)
1433 #if (DEBUG | DEVELOPMENT)
1434 printf("%s(%s, %s)\n", __func__
, cmd
, arg
);
1435 #endif /* (DEBUG | DEVELOPMENT) */
1437 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1439 if (strcmp(cmd
, "mode") == 0) {
1440 ioc
= SIOCSQOSMARKINGMODE
;
1442 if (strcmp(arg
, "fastlane") == 0)
1443 ifr
.ifr_qosmarking_mode
= IFRTYPE_QOSMARKING_FASTLANE
;
1444 else if (strcmp(arg
, "rfc4594") == 0)
1445 ifr
.ifr_qosmarking_mode
= IFRTYPE_QOSMARKING_RFC4594
;
1446 else if (strcasecmp(arg
, "none") == 0 || strcasecmp(arg
, "off") == 0)
1447 ifr
.ifr_qosmarking_mode
= IFRTYPE_QOSMARKING_MODE_NONE
;
1449 err(EX_USAGE
, "bad value for qosmarking mode: %s", arg
);
1450 } else if (strcmp(cmd
, "enabled") == 0) {
1451 ioc
= SIOCSQOSMARKINGENABLED
;
1452 if (strcmp(arg
, "1") == 0 || strcasecmp(arg
, "on") == 0||
1453 strcasecmp(arg
, "yes") == 0 || strcasecmp(arg
, "true") == 0)
1454 ifr
.ifr_qosmarking_enabled
= 1;
1455 else if (strcmp(arg
, "0") == 0 || strcasecmp(arg
, "off") == 0||
1456 strcasecmp(arg
, "no") == 0 || strcasecmp(arg
, "false") == 0)
1457 ifr
.ifr_qosmarking_enabled
= 0;
1459 err(EX_USAGE
, "bad value for qosmarking enabled: %s", arg
);
1461 err(EX_USAGE
, "qosmarking takes mode or enabled");
1464 if (ioctl(s
, ioc
, (caddr_t
)&ifr
) < 0)
1465 err(EX_OSERR
, "ioctl(%s, %s)", cmd
, arg
);
1469 setfastlane(const char *cmd
, const char *arg
, int s
, const struct afswtch
*afp
)
1471 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1473 warnx("### fastlane is obsolete, use qosmarking ###");
1475 if (strcmp(cmd
, "capable") == 0) {
1476 if (strcmp(arg
, "1") == 0 || strcasecmp(arg
, "on") == 0||
1477 strcasecmp(arg
, "yes") == 0 || strcasecmp(arg
, "true") == 0)
1478 setqosmarking("mode", "fastlane", s
, afp
);
1479 else if (strcmp(arg
, "0") == 0 || strcasecmp(arg
, "off") == 0||
1480 strcasecmp(arg
, "no") == 0 || strcasecmp(arg
, "false") == 0)
1481 setqosmarking("mode", "off", s
, afp
);
1483 err(EX_USAGE
, "bad value for fastlane %s", cmd
);
1484 } else if (strcmp(cmd
, "enable") == 0) {
1485 if (strcmp(arg
, "1") == 0 || strcasecmp(arg
, "on") == 0||
1486 strcasecmp(arg
, "yes") == 0 || strcasecmp(arg
, "true") == 0)
1487 setqosmarking("enabled", "1", s
, afp
);
1488 else if (strcmp(arg
, "0") == 0 || strcasecmp(arg
, "off") == 0||
1489 strcasecmp(arg
, "no") == 0 || strcasecmp(arg
, "false") == 0)
1490 setqosmarking("enabled", "0", s
, afp
);
1492 err(EX_USAGE
, "bad value for fastlane %s", cmd
);
1494 err(EX_USAGE
, "fastlane takes capable or enable");
1498 #else /* defined(SIOCSQOSMARKINGMODE) && defined(SIOCSQOSMARKINGENABLED) */
1501 setfastlane(const char *cmd
, const char *arg
, int s
, const struct afswtch
*afp
)
1506 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1508 if (strcmp(cmd
, "capable") == 0)
1509 ioc
= SIOCSFASTLANECAPABLE
;
1510 else if (strcmp(cmd
, "enable") == 0)
1511 ioc
= SIOCSFASTLEENABLED
;
1513 err(EX_USAGE
, "fastlane takes capable or enabled");
1515 if (strcmp(arg
, "1") == 0 || strcasecmp(arg
, "on") == 0||
1516 strcasecmp(arg
, "yes") == 0 || strcasecmp(arg
, "true") == 0)
1518 else if (strcmp(arg
, "0") == 0 || strcasecmp(arg
, "off") == 0||
1519 strcasecmp(arg
, "no") == 0 || strcasecmp(arg
, "false") == 0)
1522 err(EX_USAGE
, "bad value for fastlane %s", cmd
);
1524 if (ioc
== SIOCSFASTLANECAPABLE
)
1525 ifr
.ifr_fastlane_capable
= value
;
1527 ifr
.ifr_fastlane_enabled
= value
;
1529 if (ioctl(s
, ioc
, (caddr_t
)&ifr
) < 0)
1530 err(EX_OSERR
, "ioctl(%s, %s)", cmd
, arg
);
1535 setqosmarking(const char *cmd
, const char *arg
, int s
, const struct afswtch
*afp
)
1537 if (strcmp(cmd
, "mode") == 0) {
1538 if (strcmp(arg
, "fastlane") == 0)
1539 setfastlane("capable", "on", s
, afp
);
1540 else if (strcmp(arg
, "none") == 0)
1541 setfastlane("capable", "off", s
, afp
);
1543 err(EX_USAGE
, "bad value for qosmarking mode: %s", arg
);
1544 } else if (strcmp(cmd
, "enabled") == 0) {
1545 if (strcmp(arg
, "1") == 0 || strcasecmp(arg
, "on") == 0||
1546 strcasecmp(arg
, "yes") == 0 || strcasecmp(arg
, "true") == 0)
1547 setfastlane("enable", "on", s
, afp
);
1548 else if (strcmp(arg
, "0") == 0 || strcasecmp(arg
, "off") == 0||
1549 strcasecmp(arg
, "no") == 0 || strcasecmp(arg
, "false") == 0)
1550 setfastlane("enable", "off", s
, afp
);
1552 err(EX_USAGE
, "bad value for qosmarking enabled: %s", arg
);
1554 err(EX_USAGE
, "qosmarking takes mode or enabled");
1558 #endif /* defined(SIOCSQOSMARKINGMODE) && defined(SIOCSQOSMARKINGENABLED) */
1561 setlowpowermode(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1563 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1564 ifr
.ifr_low_power_mode
= !!value
;
1566 if (ioctl(s
, SIOCSIFLOWPOWER
, (caddr_t
)&ifr
) < 0)
1575 static struct str2num subfamily_str2num
[] = {
1576 { .str
= "any", .num
= IFRTYPE_SUBFAMILY_ANY
},
1577 { .str
= "USB", .num
= IFRTYPE_SUBFAMILY_USB
},
1578 { .str
= "Bluetooth", .num
= IFRTYPE_SUBFAMILY_BLUETOOTH
},
1579 { .str
= "Wi-Fi", .num
= IFRTYPE_SUBFAMILY_WIFI
},
1580 { .str
= "wifi", .num
= IFRTYPE_SUBFAMILY_WIFI
},
1581 { .str
= "Thunderbolt", .num
= IFRTYPE_SUBFAMILY_THUNDERBOLT
},
1582 { .str
= "reserverd", .num
= IFRTYPE_SUBFAMILY_RESERVED
},
1583 { .str
= "intcoproc", .num
= IFRTYPE_SUBFAMILY_INTCOPROC
},
1584 { .str
= "QuickRelay", .num
= IFRTYPE_SUBFAMILY_QUICKRELAY
},
1585 { .str
= "Default", .num
= IFRTYPE_SUBFAMILY_DEFAULT
},
1586 { .str
= NULL
, .num
= 0 },
1590 get_num_from_str(struct str2num
* str2nums
, const char *str
)
1592 struct str2num
*str2num
= str2nums
;
1594 while (str2num
!= NULL
&& str2num
->str
!= NULL
) {
1595 if (strcasecmp(str2num
->str
, str
) == 0) {
1596 return str2num
->num
;
1604 setifsubfamily(const char *val
, int dummy __unused
, int s
,
1605 const struct afswtch
*afp
)
1607 strlcpy(ifr
.ifr_name
, name
, sizeof (ifr
.ifr_name
));
1610 uint32_t subfamily
= strtoul(val
, &endptr
, 0);
1612 subfamily
= get_num_from_str(subfamily_str2num
, val
);
1613 if (subfamily
== 0) {
1618 ifr
.ifr_type
.ift_subfamily
= subfamily
;
1619 if (ioctl(s
, SIOCSIFSUBFAMILY
, (caddr_t
)&ifr
) < 0)
1620 warn("ioctl(SIOCSIFSUBFAMILY)");
1624 setifavailability(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1626 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1627 ifr
.ifr_interface_state
.valid_bitmask
= IF_INTERFACE_STATE_INTERFACE_AVAILABILITY_VALID
;
1629 ifr
.ifr_interface_state
.interface_availability
= IF_INTERFACE_STATE_INTERFACE_UNAVAILABLE
;
1631 ifr
.ifr_interface_state
.interface_availability
= IF_INTERFACE_STATE_INTERFACE_AVAILABLE
;
1633 if (ioctl(s
, SIOCSIFINTERFACESTATE
, (caddr_t
)&ifr
) < 0)
1634 warn("ioctl(SIOCSIFINTERFACESTATE)");
1638 show_routermode(int s
)
1640 struct afswtch
*afp
;
1642 afp
= af_getbyname("inet");
1644 (*afp
->af_routermode
)(s
, 0, NULL
);
1649 show_routermode6(void)
1651 struct afswtch
*afp
;
1654 afp
= af_getbyname("inet6");
1657 s
= socket(AF_INET6
, SOCK_DGRAM
, 0);
1663 (*afp
->af_routermode
)(s
, 0, NULL
);
1668 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1669 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1673 "\020\1AUTOCONFIGURING\4PROBE_CONNECTIVITY\5FASTLN_CAP\6IPV6_DISABLED\7ACCEPT_RTADV\10TXSTART\11RXPOLL" \
1674 "\12VLAN\13BOND\14ARPLL\15CLAT46\16NOAUTOIPV6LL\17EXPENSIVE\20ROUTER4" \
1675 "\22LOCALNET_PRIVATE\23ND6ALT\24RESTRICTED_RECV\25AWDL\26NOACKPRI" \
1676 "\27AWDL_RESTRICTED\30CL2K\31ECN_ENABLE\32ECN_DISABLE\33CHANNEL_DRV\34CA" \
1677 "\35SENDLIST\36DIRECTLINK\37FASTLN_ON\40UPDOWNCHANGE"
1680 "\020\1WOL\2TIMESTAMP\3NOAUTONX\4LEGACY\5TXLOWINET\6RXLOWINET\7ALLOCKPI" \
1681 "\10LOWPOWER\11MPKLOG\12CONSTRAINED"
1684 "\020\1RXCSUM\2TXCSUM\3VLAN_MTU\4VLAN_HWTAGGING\5JUMBO_MTU" \
1685 "\6TSO4\7TSO6\10LRO\11AV\12TXSTATUS\13CHANNEL_IO\14HW_TIMESTAMP\15SW_TIMESTAMP" \
1686 "\16PARTIAL_CSUM\17ZEROINVERT_CSUM"
1688 #define IFRLOGF_BITS \
1689 "\020\1DLIL\21FAMILY\31DRIVER\35FIRMWARE"
1692 * Print the status of the interface. If an address family was
1693 * specified, show only it; otherwise, show them all.
1696 status(const struct afswtch
*afp
, const struct sockaddr_dl
*sdl
,
1697 struct ifaddrs
*ifa
)
1699 struct ifaddrs
*ift
;
1702 struct if_descreq ifdr
;
1703 struct if_linkparamsreq iflpr
;
1705 struct ifmibdata_supplemental ifmsupp
;
1706 size_t miblen
= sizeof(struct ifmibdata_supplemental
);
1707 u_int64_t eflags
= 0;
1708 u_int64_t xflags
= 0;
1713 afp
= af_getbyname("inet");
1717 ifr
.ifr_addr
.sa_family
= afp
->af_af
== AF_LINK
? AF_INET
: afp
->af_af
;
1718 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1720 s
= socket(ifr
.ifr_addr
.sa_family
, SOCK_DGRAM
, 0);
1722 err(1, "socket(family %u,SOCK_DGRAM)", ifr
.ifr_addr
.sa_family
);
1724 printf("%s: ", name
);
1725 printb("flags", ifa
->ifa_flags
, IFFBITS
);
1726 if (ioctl(s
, SIOCGIFMETRIC
, &ifr
) != -1)
1728 printf(" metric %d", ifr
.ifr_metric
);
1729 if (ioctl(s
, SIOCGIFMTU
, &ifr
) != -1)
1730 printf(" mtu %d", ifr
.ifr_mtu
);
1731 if (showrtref
&& ioctl(s
, SIOCGIFGETRTREFCNT
, &ifr
) != -1)
1732 printf(" rtref %d", ifr
.ifr_route_refcnt
);
1734 unsigned int ifindex
= if_nametoindex(ifa
->ifa_name
);
1736 printf(" index %u", ifindex
);
1738 #ifdef SIOCGIFCONSTRAINED
1739 // Constrained is stored in if_xflags which isn't exposed directly
1740 if (ioctl(s
, SIOCGIFCONSTRAINED
, (caddr_t
)&ifr
) == 0 &&
1741 ifr
.ifr_constrained
!= 0) {
1742 printf(" constrained");
1747 if (verbose
&& ioctl(s
, SIOCGIFEFLAGS
, (caddr_t
)&ifr
) != -1 &&
1748 (eflags
= ifr
.ifr_eflags
) != 0) {
1749 printb("\teflags", eflags
, IFEFBITS
);
1753 if (verbose
&& ioctl(s
, SIOCGIFXFLAGS
, (caddr_t
)&ifr
) != -1 &&
1754 (xflags
= ifr
.ifr_xflags
) != 0) {
1755 printb("\txflags", xflags
, IFXFBITS
);
1759 if (ioctl(s
, SIOCGIFCAP
, (caddr_t
)&ifr
) == 0) {
1760 if (ifr
.ifr_curcap
!= 0) {
1761 curcap
= ifr
.ifr_curcap
;
1762 printb("\toptions", ifr
.ifr_curcap
, IFCAPBITS
);
1765 if (supmedia
&& ifr
.ifr_reqcap
!= 0) {
1766 printb("\tcapabilities", ifr
.ifr_reqcap
, IFCAPBITS
);
1773 for (ift
= ifa
; ift
!= NULL
; ift
= ift
->ifa_next
) {
1774 if (ift
->ifa_addr
== NULL
)
1776 if (strcmp(ifa
->ifa_name
, ift
->ifa_name
) != 0)
1779 const struct afswtch
*p
;
1780 p
= af_getbyfamily(ift
->ifa_addr
->sa_family
);
1781 if (p
!= NULL
&& p
->af_status
!= NULL
)
1782 p
->af_status(s
, ift
);
1783 } else if (afp
->af_af
== ift
->ifa_addr
->sa_family
)
1784 afp
->af_status(s
, ift
);
1787 /* Print CLAT46 address */
1788 clat46_addr(s
, name
);
1790 /* Print NAT64 prefix */
1791 nat64_status(s
, name
);
1794 if (allfamilies
|| afp
->af_af
== AF_LINK
) {
1795 const struct afswtch
*lafp
;
1798 * Hack; the link level address is received separately
1799 * from the routing information so any address is not
1800 * handled above. Cobble together an entry and invoke
1801 * the status method specially.
1803 lafp
= af_getbyname("lladdr");
1805 info
.rti_info
[RTAX_IFA
] = (struct sockaddr
*)sdl
;
1806 lafp
->af_status(s
, &info
);
1812 else if (afp
->af_other_status
!= NULL
)
1813 afp
->af_other_status(s
);
1815 strlcpy(ifs
.ifs_name
, name
, sizeof ifs
.ifs_name
);
1816 if (ioctl(s
, SIOCGIFSTATUS
, &ifs
) == 0)
1817 printf("%s", ifs
.ascii
);
1819 /* The rest is for when verbose is set; if not set, we're done */
1823 if (ioctl(s
, SIOCGIFTYPE
, &ifr
) != -1) {
1824 char *c
= ift2str(ifr
.ifr_type
.ift_type
,
1825 ifr
.ifr_type
.ift_family
, ifr
.ifr_type
.ift_subfamily
);
1827 printf("\ttype: %s\n", c
);
1831 if (ioctl(s
, SIOCGIFFUNCTIONALTYPE
, &ifr
) != -1) {
1832 char *c
= iffunct2str(ifr
.ifr_functional_type
);
1834 printf("\tfunctional type: %s\n", c
);
1838 struct if_agentidsreq ifar
;
1839 memset(&ifar
, 0, sizeof(ifar
));
1841 strlcpy(ifar
.ifar_name
, name
, sizeof(ifar
.ifar_name
));
1843 if (ioctl(s
, SIOCGIFAGENTIDS
, &ifar
) != -1) {
1844 if (ifar
.ifar_count
!= 0) {
1845 ifar
.ifar_uuids
= calloc(ifar
.ifar_count
, sizeof(uuid_t
));
1846 if (ifar
.ifar_uuids
!= NULL
) {
1847 if (ioctl(s
, SIOCGIFAGENTIDS
, &ifar
) != 1) {
1848 for (int agent_i
= 0; agent_i
< ifar
.ifar_count
; agent_i
++) {
1849 struct netagent_req nar
;
1850 memset(&nar
, 0, sizeof(nar
));
1852 uuid_copy(nar
.netagent_uuid
, ifar
.ifar_uuids
[agent_i
]);
1854 if (ioctl(s
, SIOCGIFAGENTDATA
, &nar
) != 1) {
1855 printf("\tagent domain:%s type:%s flags:0x%x desc:\"%s\"\n",
1856 nar
.netagent_domain
, nar
.netagent_type
,
1857 nar
.netagent_flags
, nar
.netagent_desc
);
1861 free(ifar
.ifar_uuids
);
1867 if (ioctl(s
, SIOCGIFLINKQUALITYMETRIC
, &ifr
) != -1) {
1868 int lqm
= ifr
.ifr_link_quality_metric
;
1870 printf("\tlink quality: %d ", lqm
);
1871 if (lqm
== IFNET_LQM_THRESH_OFF
)
1873 else if (lqm
== IFNET_LQM_THRESH_UNKNOWN
)
1874 printf("(unknown)");
1875 else if (lqm
> IFNET_LQM_THRESH_UNKNOWN
&&
1876 lqm
<= IFNET_LQM_THRESH_BAD
)
1878 else if (lqm
> IFNET_LQM_THRESH_UNKNOWN
&&
1879 lqm
<= IFNET_LQM_THRESH_POOR
)
1881 else if (lqm
> IFNET_LQM_THRESH_POOR
&&
1882 lqm
<= IFNET_LQM_THRESH_GOOD
)
1887 } else if (lqm
> IFNET_LQM_THRESH_UNKNOWN
) {
1888 printf("\tlink quality: %d ", lqm
);
1889 if (lqm
<= IFNET_LQM_THRESH_BAD
)
1891 else if (lqm
<= IFNET_LQM_THRESH_POOR
)
1893 else if (lqm
<= IFNET_LQM_THRESH_GOOD
)
1902 if (ioctl(s
, SIOCGIFINTERFACESTATE
, &ifr
) != -1) {
1904 if (ifr
.ifr_interface_state
.valid_bitmask
&
1905 IF_INTERFACE_STATE_RRC_STATE_VALID
) {
1906 uint8_t rrc_state
= ifr
.ifr_interface_state
.rrc_state
;
1908 printf(" rrc: %u ", rrc_state
);
1909 if (rrc_state
== IF_INTERFACE_STATE_RRC_STATE_CONNECTED
)
1910 printf("(connected)");
1911 else if (rrc_state
== IF_INTERFACE_STATE_RRC_STATE_IDLE
)
1916 if (ifr
.ifr_interface_state
.valid_bitmask
&
1917 IF_INTERFACE_STATE_INTERFACE_AVAILABILITY_VALID
) {
1918 uint8_t ifavail
= ifr
.ifr_interface_state
.interface_availability
;
1920 printf(" availability: %u ", ifavail
);
1921 if (ifavail
== IF_INTERFACE_STATE_INTERFACE_AVAILABLE
)
1923 else if (ifavail
== IF_INTERFACE_STATE_INTERFACE_UNAVAILABLE
)
1928 printf(" availability: (not valid)");
1931 ifr
.ifr_interface_state
.valid_bitmask
&
1932 IF_INTERFACE_STATE_LQM_STATE_VALID
) {
1933 int8_t lqm
= ifr
.ifr_interface_state
.lqm_state
;
1935 printf(" lqm: %d", lqm
);
1937 if (lqm
== IFNET_LQM_THRESH_OFF
)
1939 else if (lqm
== IFNET_LQM_THRESH_UNKNOWN
)
1940 printf("(unknown)");
1941 else if (lqm
== IFNET_LQM_THRESH_BAD
)
1943 else if (lqm
== IFNET_LQM_THRESH_POOR
)
1945 else if (lqm
== IFNET_LQM_THRESH_GOOD
)
1954 bzero(&iflpr
, sizeof (iflpr
));
1955 strlcpy(iflpr
.iflpr_name
, name
, sizeof (iflpr
.iflpr_name
));
1956 if (ioctl(s
, SIOCGIFLINKPARAMS
, &iflpr
) != -1) {
1957 u_int64_t ibw_max
= iflpr
.iflpr_input_bw
.max_bw
;
1958 u_int64_t ibw_eff
= iflpr
.iflpr_input_bw
.eff_bw
;
1959 u_int64_t obw_max
= iflpr
.iflpr_output_bw
.max_bw
;
1960 u_int64_t obw_eff
= iflpr
.iflpr_output_bw
.eff_bw
;
1961 u_int64_t obw_tbr
= iflpr
.iflpr_output_tbr_rate
;
1962 u_int32_t obw_pct
= iflpr
.iflpr_output_tbr_percent
;
1963 u_int64_t ilt_max
= iflpr
.iflpr_input_lt
.max_lt
;
1964 u_int64_t ilt_eff
= iflpr
.iflpr_input_lt
.eff_lt
;
1965 u_int64_t olt_max
= iflpr
.iflpr_output_lt
.max_lt
;
1966 u_int64_t olt_eff
= iflpr
.iflpr_output_lt
.eff_lt
;
1969 if (eflags
& IFEF_TXSTART
) {
1970 u_int32_t flags
= iflpr
.iflpr_flags
;
1971 u_int32_t sched
= iflpr
.iflpr_output_sched
;
1972 struct if_throttlereq iftr
;
1974 printf("\tscheduler: %s%s ",
1975 (flags
& IFLPRF_ALTQ
) ? "ALTQ_" : "",
1977 if (flags
& IFLPRF_DRVMANAGED
)
1978 printf("(driver managed)");
1981 bzero(&iftr
, sizeof (iftr
));
1982 strlcpy(iftr
.ifthr_name
, name
,
1983 sizeof (iftr
.ifthr_name
));
1984 if (ioctl(s
, SIOCGIFTHROTTLE
, &iftr
) != -1 &&
1985 iftr
.ifthr_level
!= IFNET_THROTTLE_OFF
)
1986 printf("\tthrottling: level %d (%s)\n",
1987 iftr
.ifthr_level
, tl2str(iftr
.ifthr_level
));
1990 if (obw_tbr
!= 0 && obw_eff
> obw_tbr
)
1993 if (ibw_max
!= 0 || obw_max
!= 0) {
1994 if (ibw_max
== obw_max
&& ibw_eff
== obw_eff
&&
1995 ibw_max
== ibw_eff
&& obw_tbr
== 0) {
1996 printf("\tlink rate: %s\n",
1997 bps_to_str(ibw_max
));
1999 printf("\tuplink rate: %s [eff] / ",
2000 bps_to_str(obw_eff
));
2003 printf("%s [tbr] / ",
2004 bps_to_str(obw_tbr
));
2006 printf("%s [tbr %u%%] / ",
2007 bps_to_str(obw_tbr
),
2010 printf("%s", bps_to_str(obw_max
));
2014 if (ibw_eff
== ibw_max
) {
2015 printf("\tdownlink rate: %s\n",
2016 bps_to_str(ibw_max
));
2018 printf("\tdownlink rate: "
2019 "%s [eff] / ", bps_to_str(ibw_eff
));
2020 printf("%s [max]\n",
2021 bps_to_str(ibw_max
));
2024 } else if (obw_tbr
!= 0) {
2025 printf("\tuplink rate: %s [tbr]\n",
2026 bps_to_str(obw_tbr
));
2029 if (ilt_max
!= 0 || olt_max
!= 0) {
2030 if (ilt_max
== olt_max
&& ilt_eff
== olt_eff
&&
2031 ilt_max
== ilt_eff
) {
2032 printf("\tlink latency: %s\n",
2033 ns_to_str(ilt_max
));
2035 if (olt_max
!= 0 && olt_eff
== olt_max
) {
2036 printf("\tuplink latency: %s\n",
2037 ns_to_str(olt_max
));
2038 } else if (olt_max
!= 0) {
2039 printf("\tuplink latency: "
2040 "%s [eff] / ", ns_to_str(olt_eff
));
2041 printf("%s [max]\n",
2042 ns_to_str(olt_max
));
2044 if (ilt_max
!= 0 && ilt_eff
== ilt_max
) {
2045 printf("\tdownlink latency: %s\n",
2046 ns_to_str(ilt_max
));
2047 } else if (ilt_max
!= 0) {
2048 printf("\tdownlink latency: "
2049 "%s [eff] / ", ns_to_str(ilt_eff
));
2050 printf("%s [max]\n",
2051 ns_to_str(ilt_max
));
2057 /* Common OID prefix */
2060 mib
[2] = NETLINK_GENERIC
;
2061 mib
[3] = IFMIB_IFDATA
;
2062 mib
[4] = if_nametoindex(name
);
2063 mib
[5] = IFDATA_SUPPLEMENTAL
;
2064 if (sysctl(mib
, 6, &ifmsupp
, &miblen
, (void *)0, 0) == -1)
2065 err(1, "sysctl IFDATA_SUPPLEMENTAL");
2067 if (ifmsupp
.ifmd_data_extended
.ifi_alignerrs
!= 0) {
2068 printf("\tunaligned pkts: %llu\n",
2069 ifmsupp
.ifmd_data_extended
.ifi_alignerrs
);
2071 if (ifmsupp
.ifmd_data_extended
.ifi_dt_bytes
!= 0) {
2072 printf("\tdata milestone interval: %s\n",
2073 bytes_to_str(ifmsupp
.ifmd_data_extended
.ifi_dt_bytes
));
2076 bzero(&ifdr
, sizeof (ifdr
));
2077 strlcpy(ifdr
.ifdr_name
, name
, sizeof (ifdr
.ifdr_name
));
2078 if (ioctl(s
, SIOCGIFDESC
, &ifdr
) != -1 && ifdr
.ifdr_len
) {
2079 printf("\tdesc: %s\n", ifdr
.ifdr_desc
);
2082 if (ioctl(s
, SIOCGIFLOG
, &ifr
) != -1 && ifr
.ifr_log
.ifl_level
) {
2083 printf("\tlogging: level %d ", ifr
.ifr_log
.ifl_level
);
2084 printb("facilities", ifr
.ifr_log
.ifl_flags
, IFRLOGF_BITS
);
2088 if (ioctl(s
, SIOCGIFDELEGATE
, &ifr
) != -1 && ifr
.ifr_delegated
) {
2089 char delegatedif
[IFNAMSIZ
+1];
2090 if (if_indextoname(ifr
.ifr_delegated
, delegatedif
) != NULL
)
2091 printf("\teffective interface: %s\n", delegatedif
);
2094 if (ioctl(s
, SIOCGSTARTDELAY
, &ifr
) != -1) {
2095 if (ifr
.ifr_start_delay_qlen
> 0 &&
2096 ifr
.ifr_start_delay_timeout
> 0) {
2097 printf("\ttxstart qlen: %u packets "
2098 "timeout: %u microseconds\n",
2099 ifr
.ifr_start_delay_qlen
,
2100 ifr
.ifr_start_delay_timeout
/1000);
2103 #if defined(IFCAP_HW_TIMESTAMP) && defined(IFCAP_SW_TIMESTAMP)
2104 if ((curcap
& (IFCAP_HW_TIMESTAMP
| IFCAP_SW_TIMESTAMP
)) &&
2105 ioctl(s
, SIOCGIFTIMESTAMPENABLED
, &ifr
) != -1) {
2106 printf("\ttimestamp: %s\n",
2107 (ifr
.ifr_intval
!= 0) ? "enabled" : "disabled");
2110 #if defined(SIOCGQOSMARKINGENABLED) && defined(SIOCGQOSMARKINGMODE)
2111 if (ioctl(s
, SIOCGQOSMARKINGENABLED
, &ifr
) != -1) {
2112 printf("\tqosmarking enabled: %s mode: ",
2113 ifr
.ifr_qosmarking_enabled
? "yes" : "no");
2114 if (ioctl(s
, SIOCGQOSMARKINGMODE
, &ifr
) != -1) {
2115 switch (ifr
.ifr_qosmarking_mode
) {
2116 case IFRTYPE_QOSMARKING_FASTLANE
:
2117 printf("fastlane\n");
2119 case IFRTYPE_QOSMARKING_RFC4594
:
2120 printf("RFC4594\n");
2122 case IFRTYPE_QOSMARKING_MODE_NONE
:
2126 printf("unknown (%u)\n", ifr
.ifr_qosmarking_mode
);
2131 #endif /* defined(SIOCGQOSMARKINGENABLED) && defined(SIOCGQOSMARKINGMODE) */
2133 if (ioctl(s
, SIOCGIFLOWPOWER
, &ifr
) != -1) {
2134 printf("\tlow power mode: %s\n",
2135 (ifr
.ifr_low_power_mode
!= 0) ? "enabled" : "disabled");
2137 if (ioctl(s
, SIOCGIFMPKLOG
, &ifr
) != -1) {
2138 printf("\tmulti layer packet logging (mpklog): %s\n",
2139 (ifr
.ifr_mpk_log
!= 0) ? "enabled" : "disabled");
2149 #define KILOBYTES 1024
2150 #define MEGABYTES (KILOBYTES * KILOBYTES)
2151 #define GIGABYTES (KILOBYTES * KILOBYTES * KILOBYTES)
2154 bytes_to_str(unsigned long long bytes
)
2156 static char buf
[32];
2158 long double n
= bytes
, t
;
2160 if (bytes
>= GIGABYTES
) {
2163 } else if (n
>= MEGABYTES
) {
2166 } else if (n
>= KILOBYTES
) {
2174 snprintf(buf
, sizeof (buf
), "%-4.2Lf %s", t
, u
);
2178 #define GIGABIT_PER_SEC 1000000000 /* gigabit per second */
2179 #define MEGABIT_PER_SEC 1000000 /* megabit per second */
2180 #define KILOBIT_PER_SEC 1000 /* kilobit per second */
2183 bps_to_str(unsigned long long rate
)
2185 static char buf
[32];
2187 long double n
= rate
, t
;
2189 if (rate
>= GIGABIT_PER_SEC
) {
2190 t
= n
/ GIGABIT_PER_SEC
;
2192 } else if (n
>= MEGABIT_PER_SEC
) {
2193 t
= n
/ MEGABIT_PER_SEC
;
2195 } else if (n
>= KILOBIT_PER_SEC
) {
2196 t
= n
/ KILOBIT_PER_SEC
;
2203 snprintf(buf
, sizeof (buf
), "%-4.2Lf %4s", t
, u
);
2207 #define NSEC_PER_SEC 1000000000 /* nanosecond per second */
2208 #define USEC_PER_SEC 1000000 /* microsecond per second */
2209 #define MSEC_PER_SEC 1000 /* millisecond per second */
2212 ns_to_str(unsigned long long nsec
)
2214 static char buf
[32];
2216 long double n
= nsec
, t
;
2218 if (nsec
>= NSEC_PER_SEC
) {
2219 t
= n
/ NSEC_PER_SEC
;
2221 } else if (n
>= USEC_PER_SEC
) {
2222 t
= n
/ USEC_PER_SEC
;
2224 } else if (n
>= MSEC_PER_SEC
) {
2225 t
= n
/ MSEC_PER_SEC
;
2232 snprintf(buf
, sizeof (buf
), "%-4.2Lf %4s", t
, u
);
2237 tunnel_status(int s
)
2239 af_all_tunnel_status(s
);
2243 clat46_addr(int s
, char * if_name
)
2245 struct if_clat46req ifr
;
2246 char buf
[MAXHOSTNAMELEN
];
2248 bzero(&ifr
, sizeof (ifr
));
2249 strlcpy(ifr
.ifclat46_name
, if_name
, sizeof(ifr
.ifclat46_name
));
2251 if (ioctl(s
, SIOCGIFCLAT46ADDR
, &ifr
) < 0) {
2252 if (errno
!= ENOENT
)
2253 syslog(LOG_WARNING
, "ioctl (SIOCGIFCLAT46ADDR): %d", errno
);
2257 if (inet_ntop(AF_INET6
, &ifr
.ifclat46_addr
.v6_address
, buf
, sizeof(buf
)) != NULL
)
2258 printf("\tinet6 %s prefixlen %d clat46\n",
2259 buf
, ifr
.ifclat46_addr
.v6_prefixlen
);
2263 nat64_status(int s
, char * if_name
)
2266 struct if_nat64req ifr
;
2267 char buf
[MAXHOSTNAMELEN
];
2269 bzero(&ifr
, sizeof(ifr
));
2270 strlcpy(ifr
.ifnat64_name
, if_name
, sizeof(ifr
.ifnat64_name
));
2272 if (ioctl(s
, SIOCGIFNAT64PREFIX
, &ifr
) < 0) {
2273 if (errno
!= ENOENT
)
2274 syslog(LOG_WARNING
, "ioctl(SIOCGIFNAT64PREFIX): %d", errno
);
2278 for (i
= 0; i
< NAT64_MAX_NUM_PREFIXES
; i
++) {
2279 if (ifr
.ifnat64_prefixes
[i
].prefix_len
> 0) {
2280 inet_ntop(AF_INET6
, &ifr
.ifnat64_prefixes
[i
].ipv6_prefix
, buf
, sizeof(buf
));
2281 printf("\tnat64 prefix %s prefixlen %d\n",
2282 buf
, ifr
.ifnat64_prefixes
[i
].prefix_len
<< 3);
2288 Perror(const char *cmd
)
2293 errx(1, "%s: no such interface", cmd
);
2297 errx(1, "%s: permission denied", cmd
);
2306 * Print a value a la the %b format of the kernel's printf
2309 printb(const char *s
, unsigned v
, const char *bits
)
2314 if (bits
&& *bits
== 8)
2315 printf("%s=%o", s
, v
);
2317 printf("%s=%x", s
, v
);
2321 while ((i
= *bits
++) != '\0') {
2322 if (v
& (1 << (i
-1))) {
2326 for (; (c
= *bits
) > 32; bits
++)
2329 for (; *bits
> 32; bits
++)
2338 ifmaybeload(const char *name
)
2340 #define MOD_PREFIX_LEN 3 /* "if_" */
2341 struct module_stat mstat
;
2343 char ifkind
[IFNAMSIZ
+ MOD_PREFIX_LEN
], ifname
[IFNAMSIZ
], *dp
;
2346 /* loading suppressed by the user */
2350 /* trim the interface number off the end */
2351 strlcpy(ifname
, name
, sizeof(ifname
));
2352 for (dp
= ifname
; *dp
!= 0; dp
++)
2358 /* turn interface and unit into module name */
2359 strlcpy(ifkind
, "if_", sizeof(ifkind
));
2360 strlcpy(ifkind
+ MOD_PREFIX_LEN
, ifname
,
2361 sizeof(ifkind
) - MOD_PREFIX_LEN
);
2363 /* scan files in kernel */
2364 mstat
.version
= sizeof(struct module_stat
);
2365 for (fileid
= kldnext(0); fileid
> 0; fileid
= kldnext(fileid
)) {
2366 /* scan modules in file */
2367 for (modid
= kldfirstmod(fileid
); modid
> 0;
2368 modid
= modfnext(modid
)) {
2369 if (modstat(modid
, &mstat
) < 0)
2371 /* strip bus name if present */
2372 if ((cp
= strchr(mstat
.name
, '/')) != NULL
) {
2377 /* already loaded? */
2378 if (strncmp(ifname
, cp
, strlen(ifname
) + 1) == 0 ||
2379 strncmp(ifkind
, cp
, strlen(ifkind
) + 1) == 0)
2384 /* not present, we should try to load it */
2389 static struct cmd basic_cmds
[] = {
2390 DEF_CMD("up", IFF_UP
, setifflags
),
2391 DEF_CMD("down", -IFF_UP
, setifflags
),
2392 DEF_CMD("arp", -IFF_NOARP
, setifflags
),
2393 DEF_CMD("-arp", IFF_NOARP
, setifflags
),
2394 DEF_CMD("debug", IFF_DEBUG
, setifflags
),
2395 DEF_CMD("-debug", -IFF_DEBUG
, setifflags
),
2397 DEF_CMD("promisc", IFF_PPROMISC
, setifflags
),
2398 DEF_CMD("-promisc", -IFF_PPROMISC
, setifflags
),
2399 #endif /* IFF_PPROMISC */
2400 DEF_CMD("add", IFF_UP
, notealias
),
2401 DEF_CMD("alias", IFF_UP
, notealias
),
2402 DEF_CMD("-alias", -IFF_UP
, notealias
),
2403 DEF_CMD("delete", -IFF_UP
, notealias
),
2404 DEF_CMD("remove", -IFF_UP
, notealias
),
2406 #define EN_SWABIPS 0x1000
2407 DEF_CMD("swabips", EN_SWABIPS
, setifflags
),
2408 DEF_CMD("-swabips", -EN_SWABIPS
, setifflags
),
2410 DEF_CMD_ARG("netmask", setifnetmask
),
2411 DEF_CMD_ARG("metric", setifmetric
),
2412 DEF_CMD_ARG("broadcast", setifbroadaddr
),
2413 DEF_CMD_ARG("ipdst", setifipdst
),
2414 DEF_CMD_ARG2("tunnel", settunnel
),
2415 DEF_CMD("-tunnel", 0, deletetunnel
),
2416 DEF_CMD("deletetunnel", 0, deletetunnel
),
2417 DEF_CMD("link0", IFF_LINK0
, setifflags
),
2418 DEF_CMD("-link0", -IFF_LINK0
, setifflags
),
2419 DEF_CMD("link1", IFF_LINK1
, setifflags
),
2420 DEF_CMD("-link1", -IFF_LINK1
, setifflags
),
2421 DEF_CMD("link2", IFF_LINK2
, setifflags
),
2422 DEF_CMD("-link2", -IFF_LINK2
, setifflags
),
2424 DEF_CMD("monitor", IFF_MONITOR
:, setifflags
),
2425 DEF_CMD("-monitor", -IFF_MONITOR
, setifflags
),
2426 #endif /* IFF_MONITOR */
2427 DEF_CMD("mpklog", 1, setifmpklog
),
2428 DEF_CMD("-mpklog", 0, setifmpklog
),
2429 #ifdef IFF_STATICARP
2430 DEF_CMD("staticarp", IFF_STATICARP
, setifflags
),
2431 DEF_CMD("-staticarp", -IFF_STATICARP
, setifflags
),
2432 #endif /* IFF_STATICARP */
2434 DEF_CMD("rxcsum", IFCAP_RXCSUM
, setifcap
),
2435 DEF_CMD("-rxcsum", -IFCAP_RXCSUM
, setifcap
),
2436 #endif /* IFCAP_RXCSUM */
2438 DEF_CMD("txcsum", IFCAP_TXCSUM
, setifcap
),
2439 DEF_CMD("-txcsum", -IFCAP_TXCSUM
, setifcap
),
2440 #endif /* IFCAP_TXCSUM */
2441 #ifdef IFCAP_NETCONS
2442 DEF_CMD("netcons", IFCAP_NETCONS
, setifcap
),
2443 DEF_CMD("-netcons", -IFCAP_NETCONS
, setifcap
),
2444 #endif /* IFCAP_NETCONS */
2445 #ifdef IFCAP_POLLING
2446 DEF_CMD("polling", IFCAP_POLLING
, setifcap
),
2447 DEF_CMD("-polling", -IFCAP_POLLING
, setifcap
),
2448 #endif /* IFCAP_POLLING */
2450 DEF_CMD("tso", IFCAP_TSO
, setifcap
),
2451 DEF_CMD("-tso", -IFCAP_TSO
, setifcap
),
2452 #endif /* IFCAP_TSO */
2454 DEF_CMD("lro", IFCAP_LRO
, setifcap
),
2455 DEF_CMD("-lro", -IFCAP_LRO
, setifcap
),
2456 #endif /* IFCAP_LRO */
2458 DEF_CMD("wol", IFCAP_WOL
, setifcap
),
2459 DEF_CMD("-wol", -IFCAP_WOL
, setifcap
),
2460 #endif /* IFCAP_WOL */
2461 #ifdef IFCAP_WOL_UCAST
2462 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST
, setifcap
),
2463 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST
, setifcap
),
2464 #endif /* IFCAP_WOL_UCAST */
2465 #ifdef IFCAP_WOL_MCAST
2466 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST
, setifcap
),
2467 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST
, setifcap
),
2468 #endif /* IFCAP_WOL_MCAST */
2469 #ifdef IFCAP_WOL_MAGIC
2470 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC
, setifcap
),
2471 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC
, setifcap
),
2472 #endif /* IFCAP_WOL_MAGIC */
2473 DEF_CMD("normal", -IFF_LINK0
, setifflags
),
2474 DEF_CMD("compress", IFF_LINK0
, setifflags
),
2475 DEF_CMD("noicmp", IFF_LINK1
, setifflags
),
2476 DEF_CMD_ARG("mtu", setifmtu
),
2478 DEF_CMD_ARG("name", setifname
),
2481 DEF_CMD("av", IFCAP_AV
, setifcap
),
2482 DEF_CMD("-av", -IFCAP_AV
, setifcap
),
2483 #endif /* IFCAP_AV */
2484 DEF_CMD("router", 1, setrouter
),
2485 DEF_CMD("-router", 0, setrouter
),
2486 DEF_CMD_VA("routermode", routermode
),
2487 DEF_CMD_ARG("desc", setifdesc
),
2488 DEF_CMD_ARG("tbr", settbr
),
2489 DEF_CMD_VA("netem", setnetem
),
2490 DEF_CMD_ARG("throttle", setthrottle
),
2491 DEF_CMD_ARG("log", setlog
),
2492 DEF_CMD("cl2k", 1, setcl2k
),
2493 DEF_CMD("-cl2k", 0, setcl2k
),
2494 DEF_CMD("expensive", 1, setexpensive
),
2495 DEF_CMD("-expensive", 0, setexpensive
),
2496 #ifdef SIOCSIFCONSTRAINED
2497 DEF_CMD("constrained", 1, setconstrained
),
2498 DEF_CMD("-constrained", 0, setconstrained
),
2500 DEF_CMD("timestamp", 1, settimestamp
),
2501 DEF_CMD("-timestamp", 0, settimestamp
),
2502 DEF_CMD_ARG("ecn", setecnmode
),
2503 DEF_CMD_ARG2("fastlane", setfastlane
),
2504 DEF_CMD_ARG2("qosmarking", setqosmarking
),
2505 DEF_CMD_ARG("disable_output", setdisableoutput
),
2506 DEF_CMD("probe_connectivity", 1, setprobeconnectivity
),
2507 DEF_CMD("-probe_connectivity", 0, setprobeconnectivity
),
2508 DEF_CMD("lowpowermode", 1, setlowpowermode
),
2509 DEF_CMD("-lowpowermode", 0, setlowpowermode
),
2510 DEF_CMD_ARG("subfamily", setifsubfamily
),
2511 DEF_CMD("available", 1, setifavailability
),
2512 DEF_CMD("-available", 0, setifavailability
),
2513 DEF_CMD("unavailable", 0, setifavailability
),
2516 static __constructor
void
2519 #define N(a) (sizeof(a) / sizeof(a[0]))
2522 for (i
= 0; i
< N(basic_cmds
); i
++)
2523 cmd_register(&basic_cmds
[i
]);
2528 sched2str(unsigned int s
)
2533 case PKTSCHEDT_NONE
:
2536 case PKTSCHEDT_FQ_CODEL
:
2548 tl2str(unsigned int s
)
2553 case IFNET_THROTTLE_OFF
:
2556 case IFNET_THROTTLE_OPPORTUNISTIC
:
2557 c
= "opportunistic";
2568 ift2str(unsigned int t
, unsigned int f
, unsigned int sf
)
2570 static char buf
[256];
2576 case IFRTYPE_SUBFAMILY_USB
:
2579 case IFRTYPE_SUBFAMILY_BLUETOOTH
:
2580 c
= "Bluetooth PAN";
2582 case IFRTYPE_SUBFAMILY_WIFI
:
2585 case IFRTYPE_SUBFAMILY_THUNDERBOLT
:
2586 c
= "IP over Thunderbolt";
2588 case IFRTYPE_SUBFAMILY_ANY
:
2596 c
= "IP over FireWire";
2600 c
= "Packet capture";
2608 if (ifr
.ifr_type
.ift_family
== APPLE_IF_FAM_IPSEC
) {
2609 if (ifr
.ifr_type
.ift_subfamily
== IFRTYPE_SUBFAMILY_BLUETOOTH
) {
2610 c
= "Companion Link Bluetooth";
2611 } else if (ifr
.ifr_type
.ift_subfamily
== IFRTYPE_SUBFAMILY_QUICKRELAY
) {
2612 c
= "Companion Link QuickRelay";
2613 } else if (ifr
.ifr_type
.ift_subfamily
== IFRTYPE_SUBFAMILY_WIFI
) {
2614 c
= "Companion Link Wi-Fi";
2615 } else if (ifr
.ifr_type
.ift_subfamily
== IFRTYPE_SUBFAMILY_DEFAULT
) {
2616 c
= "Companion Link Default";
2629 case IFT_IEEE8023ADLAG
:
2636 (void) snprintf(buf
, sizeof (buf
),
2637 "0x%x family: %u subfamily: %u",
2638 ifr
.ifr_type
.ift_type
, ifr
.ifr_type
.ift_family
,
2639 ifr
.ifr_type
.ift_subfamily
);
2641 (void) snprintf(buf
, sizeof (buf
),
2642 "%s (0x%x) family: %u subfamily: %u", c
,
2643 ifr
.ifr_type
.ift_type
, ifr
.ifr_type
.ift_family
,
2644 ifr
.ifr_type
.ift_subfamily
);
2653 iffunct2str(u_int32_t functional_type
)
2657 switch (functional_type
) {
2658 case IFRTYPE_FUNCTIONAL_UNKNOWN
:
2661 case IFRTYPE_FUNCTIONAL_LOOPBACK
:
2665 case IFRTYPE_FUNCTIONAL_WIRED
:
2669 case IFRTYPE_FUNCTIONAL_WIFI_INFRA
:
2673 case IFRTYPE_FUNCTIONAL_WIFI_AWDL
:
2677 case IFRTYPE_FUNCTIONAL_CELLULAR
:
2681 case IFRTYPE_FUNCTIONAL_INTCOPROC
:
2684 case IFRTYPE_FUNCTIONAL_COMPANIONLINK
:
2685 str
= "companionlink";