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 setifdesc(const char *val
, int dummy __unused
, int s
, const struct afswtch
*afp
)
861 struct if_descreq ifdr
;
863 bzero(&ifdr
, sizeof (ifdr
));
864 strlcpy(ifdr
.ifdr_name
, name
, sizeof (ifdr
.ifdr_name
));
865 ifdr
.ifdr_len
= strlen(val
);
866 strlcpy((char *)ifdr
.ifdr_desc
, val
, sizeof (ifdr
.ifdr_desc
));
868 if (ioctl(s
, SIOCSIFDESC
, (caddr_t
)&ifdr
) < 0) {
869 warn("ioctl (set desc)");
874 settbr(const char *val
, int dummy __unused
, int s
, const struct afswtch
*afp
)
876 struct if_linkparamsreq iflpr
;
879 u_int32_t percent
= 0;
883 bzero(&iflpr
, sizeof (iflpr
));
884 strlcpy(iflpr
.iflpr_name
, name
, sizeof (iflpr
.iflpr_name
));
886 bps
= strtold(val
, &cp
);
887 if (val
== cp
|| errno
!= 0) {
888 warn("Invalid value '%s'", val
);
891 rate
= (u_int64_t
)bps
;
893 if (!strcmp(cp
, "b") || !strcmp(cp
, "bps")) {
895 } else if (!strcmp(cp
, "Kb") || !strcmp(cp
, "Kbps")) {
897 } else if (!strcmp(cp
, "Mb") || !strcmp(cp
, "Mbps")) {
899 } else if (!strcmp(cp
, "Gb") || !strcmp(cp
, "Gbps")) {
900 rate
*= 1000 * 1000 * 1000;
901 } else if (!strcmp(cp
, "%")) {
903 if (percent
== 0 || percent
> 100) {
904 printf("Value out of range '%s'", val
);
907 } else if (*cp
!= '\0') {
908 printf("Unknown unit '%s'", cp
);
912 iflpr
.iflpr_output_tbr_rate
= rate
;
913 iflpr
.iflpr_output_tbr_percent
= percent
;
914 if (ioctl(s
, SIOCSIFLINKPARAMS
, &iflpr
) < 0 &&
915 errno
!= ENOENT
&& errno
!= ENXIO
&& errno
!= ENODEV
) {
916 warn("ioctl (set link params)");
917 } else if (errno
== ENXIO
) {
918 printf("TBR cannot be set on %s\n", name
);
919 } else if (errno
== 0 && rate
== 0) {
920 printf("%s: TBR is now disabled\n", name
);
921 } else if (errno
== ENODEV
) {
922 printf("%s: requires absolute TBR rate\n", name
);
923 } else if (percent
!= 0) {
924 printf("%s: TBR rate set to %u%% of effective link rate\n",
927 printf("%s: TBR rate set to %s\n", name
, bps_to_str(rate
));
932 get_int64(uint64_t *i
, char const *s
)
935 *i
= strtol(s
, &cp
, 10);
936 if (cp
== s
|| errno
!= 0) {
943 get_int32(uint32_t *i
, char const *s
)
946 *i
= strtol(s
, &cp
, 10);
947 if (cp
== s
|| errno
!= 0) {
954 get_percent(double *d
, const char *s
)
957 *d
= strtod(s
, &cp
) / (double)100;
958 if (*d
== HUGE_VALF
|| *d
== HUGE_VALL
) {
961 if (*d
== 0.0 || (*cp
!= '\0' && strcmp(cp
, "%") != 0)) {
968 get_percent_fixed_point(uint32_t *i
, const char *s
)
972 if (get_percent(&p
, s
) != 0){
976 *i
= p
* IF_NETEM_PARAMS_PSCALE
;
981 netem_parse_args(struct if_netem_params
*p
, int argc
, char *const *argv
)
983 int argc_saved
= argc
;
984 uint64_t bandwitdh
= 0;
985 uint32_t latency
= 0, jitter
= 0;
986 uint32_t corruption
= 0;
987 uint32_t duplication
= 0;
988 uint32_t loss_p_gr_gl
= 0, loss_p_gr_bl
= 0, loss_p_bl_br
= 0,
989 loss_p_bl_gr
= 0, loss_p_br_bl
= 0;
990 uint32_t reordering
= 0;
992 bzero(p
, sizeof (*p
));
994 /* take out "input"/"output" */
997 for ( ; argc
> 0; ) {
998 if (strcmp(*argv
, "bandwidth") == 0) {
1000 if (argc
<= 0 || get_int64(&bandwitdh
, *argv
) != 0) {
1001 err(1, "Invalid value '%s'", *argv
);
1004 } else if (strcmp(*argv
, "corruption") == 0) {
1007 get_percent_fixed_point(&corruption
, *argv
) != 0) {
1008 err(1, "Invalid value '%s'", *argv
);
1011 } else if (strcmp(*argv
, "delay") == 0) {
1013 if (argc
<= 0 || get_int32(&latency
, *argv
) != 0) {
1014 err(1, "Invalid value '%s'", *argv
);
1017 if (argc
> 0 && get_int32(&jitter
, *argv
) == 0) {
1020 } else if (strcmp(*argv
, "duplication") == 0) {
1023 get_percent_fixed_point(&duplication
, *argv
) != 0) {
1024 err(1, "Invalid value '%s'", *argv
);
1028 } else if (strcmp(*argv
, "loss") == 0) {
1030 if (argc
<= 0 || get_percent_fixed_point(&loss_p_gr_gl
, *argv
) != 0) {
1031 err(1, "Invalid value '%s'", *argv
);
1033 /* we may have all 5 probs, use naive model if not */
1035 if (argc
<= 0 || get_percent_fixed_point(&loss_p_gr_bl
, *argv
) != 0) {
1038 /* if more than p_gr_gl, then should have all probs */
1040 if (argc
<= 0 || get_percent_fixed_point(&loss_p_bl_br
, *argv
) != 0) {
1041 err(1, "Invalid value '%s' for p_bl_br", *argv
);
1044 if (argc
<= 0 || get_percent_fixed_point(&loss_p_bl_gr
, *argv
) != 0) {
1045 err(1, "Invalid value '%s' for p_bl_gr", *argv
);
1048 if (argc
<= 0 || get_percent_fixed_point(&loss_p_br_bl
, *argv
) != 0) {
1049 err(1, "Invalid value '%s' for p_br_bl", *argv
);
1052 } else if (strcmp(*argv
, "reordering") == 0) {
1054 if (argc
<= 0 || get_percent_fixed_point(&reordering
, *argv
) != 0) {
1055 err(1, "Invalid value '%s'", *argv
);
1063 if (corruption
> IF_NETEM_PARAMS_PSCALE
) {
1064 err(1, "corruption percentage > 100%%");
1067 if (duplication
> IF_NETEM_PARAMS_PSCALE
) {
1068 err(1, "duplication percentage > 100%%");
1071 if (duplication
> 0 && latency
== 0) {
1072 /* we need to insert dup'ed packet with latency */
1073 err(1, "duplication needs latency param");
1076 if (latency
> 1000) {
1077 err(1, "latency %dms too big (> 1 sec)", latency
);
1080 if (jitter
* 3 > latency
) {
1081 err(1, "jitter %dms too big (latency %dms)", jitter
, latency
);
1084 /* if gr_gl == 0 (no loss), other prob should all be zero */
1085 if (loss_p_gr_gl
== 0 &&
1086 (loss_p_gr_bl
!= 0 || loss_p_bl_br
!= 0 || loss_p_bl_gr
!= 0 ||
1087 loss_p_br_bl
!= 0)) {
1088 err(1, "loss params not all zero when gr_gl is zero");
1091 /* check state machine transition prob integrity */
1092 if (loss_p_gr_gl
> IF_NETEM_PARAMS_PSCALE
||
1093 /* gr_gl = IF_NETEM_PARAMS_PSCALE for total loss */
1094 loss_p_gr_bl
> IF_NETEM_PARAMS_PSCALE
||
1095 loss_p_bl_br
> IF_NETEM_PARAMS_PSCALE
||
1096 loss_p_bl_gr
> IF_NETEM_PARAMS_PSCALE
||
1097 loss_p_br_bl
> IF_NETEM_PARAMS_PSCALE
||
1098 loss_p_gr_gl
+ loss_p_gr_bl
> IF_NETEM_PARAMS_PSCALE
||
1099 loss_p_bl_br
+ loss_p_bl_gr
> IF_NETEM_PARAMS_PSCALE
) {
1100 err(1, "loss params too big");
1103 if (reordering
> IF_NETEM_PARAMS_PSCALE
) {
1104 err(1, "reordering percentage > 100%%");
1107 p
->ifnetem_bandwidth_bps
= bandwitdh
;
1108 p
->ifnetem_latency_ms
= latency
;
1109 p
->ifnetem_jitter_ms
= jitter
;
1110 p
->ifnetem_corruption_p
= corruption
;
1111 p
->ifnetem_duplication_p
= duplication
;
1112 p
->ifnetem_loss_p_gr_gl
= loss_p_gr_gl
;
1113 p
->ifnetem_loss_p_gr_bl
= loss_p_gr_bl
;
1114 p
->ifnetem_loss_p_bl_br
= loss_p_bl_br
;
1115 p
->ifnetem_loss_p_bl_gr
= loss_p_bl_gr
;
1116 p
->ifnetem_loss_p_br_bl
= loss_p_br_bl
;
1117 p
->ifnetem_reordering_p
= reordering
;
1119 return (argc_saved
- argc
);
1123 print_netem_params(struct if_netem_params
*p
, const char *desc
)
1125 struct if_netem_params zero_params
;
1126 double pscale
= IF_NETEM_PARAMS_PSCALE
/ 100;
1127 bzero(&zero_params
, sizeof (zero_params
));
1129 if (memcmp(p
, &zero_params
, sizeof (zero_params
)) == 0) {
1130 printf("%s NetEm Disabled\n\n", desc
);
1133 "%s NetEm Parameters\n"
1134 "\tbandwidth rate %llubps\n"
1135 "\tdelay latency %dms\n"
1137 desc
, p
->ifnetem_bandwidth_bps
,
1138 p
->ifnetem_latency_ms
, p
->ifnetem_jitter_ms
);
1139 if (p
->ifnetem_loss_p_gr_bl
== 0 &&
1140 p
->ifnetem_loss_p_bl_br
== 0 &&
1141 p
->ifnetem_loss_p_bl_gr
== 0 &&
1142 p
->ifnetem_loss_p_br_bl
== 0) {
1145 (double) p
->ifnetem_loss_p_gr_gl
/ pscale
);
1148 "\tloss GAP_RECV -> GAP_LOSS %.3f%%\n"
1149 "\t GAP_RECV -> BURST_LOSS %.3f%%\n"
1150 "\t BURST_LOSS -> BURST_RECV %.3f%%\n"
1151 "\t BURST_LOSS -> GAP_RECV %.3f%%\n"
1152 "\t BURST_RECV -> BURST_LOSS %.3f%%\n",
1153 (double) p
->ifnetem_loss_p_gr_gl
/ pscale
,
1154 (double) p
->ifnetem_loss_p_gr_bl
/ pscale
,
1155 (double) p
->ifnetem_loss_p_bl_br
/ pscale
,
1156 (double) p
->ifnetem_loss_p_bl_gr
/ pscale
,
1157 (double) p
->ifnetem_loss_p_br_bl
/ pscale
);
1160 "\tcorruption %.3f%%\n"
1161 "\treordering %.3f%%\n\n",
1162 (double) p
->ifnetem_corruption_p
/ pscale
,
1163 (double) p
->ifnetem_reordering_p
/ pscale
);
1168 setnetem(int argc
, char *const *argv
, int s
, const struct afswtch
*afp
)
1170 struct if_linkparamsreq iflpr
;
1171 struct if_netem_params input_params
, output_params
;
1172 int ret
= 0, error
= 0;
1174 bzero(&iflpr
, sizeof (iflpr
));
1175 bzero(&input_params
, sizeof (input_params
));
1176 bzero(&output_params
, sizeof (output_params
));
1179 if (strcmp(argv
[0], "input") == 0) {
1180 ret
= netem_parse_args(&input_params
, argc
, argv
);
1181 } else if (strcmp(argv
[0], "output") == 0) {
1182 ret
= netem_parse_args(&output_params
, argc
, argv
);
1183 } else if (strcmp(argv
[0], "-h") == 0 || strcmp(argv
[0], "--help") == 0) {
1186 fprintf(stderr
, "uknown option %s\n", argv
[0]);
1195 strlcpy(iflpr
.iflpr_name
, name
, sizeof (iflpr
.iflpr_name
));
1196 error
= ioctl(s
, SIOCGIFLINKPARAMS
, &iflpr
);
1198 warn("ioctl (get link params)");
1202 print_netem_params(&iflpr
.iflpr_input_netem
, "Input");
1203 print_netem_params(&iflpr
.iflpr_output_netem
, "Output");
1205 } else if (argc
== 1) {
1206 if (strcmp(argv
[0], "input") == 0) {
1207 bzero(&iflpr
.iflpr_input_netem
,
1208 sizeof (iflpr
.iflpr_input_netem
));
1209 } else if (strcmp(argv
[0], "output") == 0) {
1210 bzero(&iflpr
.iflpr_output_netem
,
1211 sizeof (iflpr
.iflpr_output_netem
));
1213 fprintf(stderr
, "uknown option %s\n", argv
[0]);
1216 printf("%s: netem is now disabled for %s\n", name
, argv
[0]);
1219 if (strcmp(argv
[0], "input") == 0) {
1220 iflpr
.iflpr_input_netem
= input_params
;
1221 } else if (strcmp(argv
[0], "output") == 0) {
1222 iflpr
.iflpr_output_netem
= output_params
;
1226 error
= ioctl(s
, SIOCSIFLINKPARAMS
, &iflpr
);
1227 if (error
< 0 && errno
!= ENOENT
&& errno
!= ENXIO
&& errno
!= ENODEV
) {
1228 warn("ioctl (set link params)");
1229 } else if (errno
== ENXIO
) {
1230 printf("netem cannot be set on %s\n", name
);
1232 printf("%s: netem configured\n", name
);
1237 fprintf(stderr
, "Usage:\n"
1238 "\tTo enable/set netem params\n"
1239 "\t\tnetem <input|output>\n"
1240 "\t\t [ bandwidth BIT_PER_SEC ]\n"
1241 "\t\t [ delay DELAY_MSEC [ JITTER_MSEC ] ]\n"
1242 "\t\t [ loss PERCENTAGE ]\n"
1243 "\t\t [ duplication PERCENTAGE ]\n"
1244 "\t\t [ reordering PERCENTAGE ]\n\n"
1245 "\tTo disable <input|output> netem\n"
1246 "\t\tnetem <input|output>\n\n"
1247 "\tTo show current settings\n"
1253 setthrottle(const char *val
, int dummy __unused
, int s
,
1254 const struct afswtch
*afp
)
1256 struct if_throttlereq iftr
;
1260 bzero(&iftr
, sizeof (iftr
));
1261 strlcpy(iftr
.ifthr_name
, name
, sizeof (iftr
.ifthr_name
));
1263 iftr
.ifthr_level
= strtold(val
, &cp
);
1264 if (val
== cp
|| errno
!= 0) {
1265 warn("Invalid value '%s'", val
);
1269 if (ioctl(s
, SIOCSIFTHROTTLE
, &iftr
) < 0 && errno
!= ENXIO
) {
1270 warn("ioctl (set throttling level)");
1271 } else if (errno
== ENXIO
) {
1272 printf("throttling level cannot be set on %s\n", name
);
1274 printf("%s: throttling level set to %d\n", name
,
1280 setdisableoutput(const char *val
, int dummy __unused
, int s
,
1281 const struct afswtch
*afp
)
1286 bzero(&ifr
, sizeof (ifr
));
1287 strlcpy(ifr
.ifr_name
, name
, sizeof (ifr
.ifr_name
));
1289 ifr
.ifr_ifru
.ifru_disable_output
= strtold(val
, &cp
);
1290 if (val
== cp
|| errno
!= 0) {
1291 warn("Invalid value '%s'", val
);
1295 if (ioctl(s
, SIOCSIFDISABLEOUTPUT
, &ifr
) < 0 && errno
!= ENXIO
) {
1296 warn("ioctl set disable output");
1297 } else if (errno
== ENXIO
) {
1298 printf("output thread can not be disabled on %s\n", name
);
1300 printf("output %s on %s\n",
1301 ((ifr
.ifr_ifru
.ifru_disable_output
== 0) ? "enabled" : "disabled"),
1307 setlog(const char *val
, int dummy __unused
, int s
,
1308 const struct afswtch
*afp
)
1313 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1315 ifr
.ifr_log
.ifl_level
= strtold(val
, &cp
);
1316 if (val
== cp
|| errno
!= 0) {
1317 warn("Invalid value '%s'", val
);
1320 ifr
.ifr_log
.ifl_flags
= (IFRLOGF_DLIL
|IFRLOGF_FAMILY
|IFRLOGF_DRIVER
|
1323 if (ioctl(s
, SIOCSIFLOG
, &ifr
) < 0)
1324 warn("ioctl (set logging parameters)");
1328 setcl2k(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1330 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1331 ifr
.ifr_ifru
.ifru_2kcl
= value
;
1333 if (ioctl(s
, SIOCSIF2KCL
, (caddr_t
)&ifr
) < 0)
1338 setexpensive(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1340 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1341 ifr
.ifr_ifru
.ifru_expensive
= value
;
1343 if (ioctl(s
, SIOCSIFEXPENSIVE
, (caddr_t
)&ifr
) < 0)
1347 #ifdef SIOCSIFCONSTRAINED
1349 setconstrained(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1351 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1352 ifr
.ifr_ifru
.ifru_constrained
= value
;
1354 if (ioctl(s
, SIOCSIFCONSTRAINED
, (caddr_t
)&ifr
) < 0)
1360 setifmpklog(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1362 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1363 ifr
.ifr_ifru
.ifru_mpk_log
= value
;
1365 if (ioctl(s
, SIOCSIFMPKLOG
, (caddr_t
)&ifr
) < 0)
1370 settimestamp(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1372 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1375 if (ioctl(s
, SIOCSIFTIMESTAMPDISABLE
, (caddr_t
)&ifr
) < 0)
1378 if (ioctl(s
, SIOCSIFTIMESTAMPENABLE
, (caddr_t
)&ifr
) < 0)
1384 setecnmode(const char *val
, int dummy __unused
, int s
,
1385 const struct afswtch
*afp
)
1389 if (strcmp(val
, "default") == 0)
1390 ifr
.ifr_ifru
.ifru_ecn_mode
= IFRTYPE_ECN_DEFAULT
;
1391 else if (strcmp(val
, "enable") == 0)
1392 ifr
.ifr_ifru
.ifru_ecn_mode
= IFRTYPE_ECN_ENABLE
;
1393 else if (strcmp(val
, "disable") == 0)
1394 ifr
.ifr_ifru
.ifru_ecn_mode
= IFRTYPE_ECN_DISABLE
;
1396 ifr
.ifr_ifru
.ifru_ecn_mode
= strtold(val
, &cp
);
1397 if (val
== cp
|| errno
!= 0) {
1398 warn("Invalid ECN mode value '%s'", val
);
1403 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1405 if (ioctl(s
, SIOCSECNMODE
, (caddr_t
)&ifr
) < 0)
1406 Perror("ioctl(SIOCSECNMODE)");
1410 setprobeconnectivity(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1412 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1413 ifr
.ifr_ifru
.ifru_probe_connectivity
= value
;
1415 if (ioctl(s
, SIOCSIFPROBECONNECTIVITY
, (caddr_t
)&ifr
) < 0)
1419 #if defined(SIOCSQOSMARKINGMODE) && defined(SIOCSQOSMARKINGENABLED)
1422 setqosmarking(const char *cmd
, const char *arg
, int s
, const struct afswtch
*afp
)
1426 #if (DEBUG | DEVELOPMENT)
1427 printf("%s(%s, %s)\n", __func__
, cmd
, arg
);
1428 #endif /* (DEBUG | DEVELOPMENT) */
1430 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1432 if (strcmp(cmd
, "mode") == 0) {
1433 ioc
= SIOCSQOSMARKINGMODE
;
1435 if (strcmp(arg
, "fastlane") == 0)
1436 ifr
.ifr_qosmarking_mode
= IFRTYPE_QOSMARKING_FASTLANE
;
1437 else if (strcmp(arg
, "rfc4594") == 0)
1438 ifr
.ifr_qosmarking_mode
= IFRTYPE_QOSMARKING_RFC4594
;
1439 else if (strcasecmp(arg
, "none") == 0 || strcasecmp(arg
, "off") == 0)
1440 ifr
.ifr_qosmarking_mode
= IFRTYPE_QOSMARKING_MODE_NONE
;
1442 err(EX_USAGE
, "bad value for qosmarking mode: %s", arg
);
1443 } else if (strcmp(cmd
, "enabled") == 0) {
1444 ioc
= SIOCSQOSMARKINGENABLED
;
1445 if (strcmp(arg
, "1") == 0 || strcasecmp(arg
, "on") == 0||
1446 strcasecmp(arg
, "yes") == 0 || strcasecmp(arg
, "true") == 0)
1447 ifr
.ifr_qosmarking_enabled
= 1;
1448 else if (strcmp(arg
, "0") == 0 || strcasecmp(arg
, "off") == 0||
1449 strcasecmp(arg
, "no") == 0 || strcasecmp(arg
, "false") == 0)
1450 ifr
.ifr_qosmarking_enabled
= 0;
1452 err(EX_USAGE
, "bad value for qosmarking enabled: %s", arg
);
1454 err(EX_USAGE
, "qosmarking takes mode or enabled");
1457 if (ioctl(s
, ioc
, (caddr_t
)&ifr
) < 0)
1458 err(EX_OSERR
, "ioctl(%s, %s)", cmd
, arg
);
1462 setfastlane(const char *cmd
, const char *arg
, int s
, const struct afswtch
*afp
)
1464 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1466 warnx("### fastlane is obsolete, use qosmarking ###");
1468 if (strcmp(cmd
, "capable") == 0) {
1469 if (strcmp(arg
, "1") == 0 || strcasecmp(arg
, "on") == 0||
1470 strcasecmp(arg
, "yes") == 0 || strcasecmp(arg
, "true") == 0)
1471 setqosmarking("mode", "fastlane", s
, afp
);
1472 else if (strcmp(arg
, "0") == 0 || strcasecmp(arg
, "off") == 0||
1473 strcasecmp(arg
, "no") == 0 || strcasecmp(arg
, "false") == 0)
1474 setqosmarking("mode", "off", s
, afp
);
1476 err(EX_USAGE
, "bad value for fastlane %s", cmd
);
1477 } else if (strcmp(cmd
, "enable") == 0) {
1478 if (strcmp(arg
, "1") == 0 || strcasecmp(arg
, "on") == 0||
1479 strcasecmp(arg
, "yes") == 0 || strcasecmp(arg
, "true") == 0)
1480 setqosmarking("enabled", "1", s
, afp
);
1481 else if (strcmp(arg
, "0") == 0 || strcasecmp(arg
, "off") == 0||
1482 strcasecmp(arg
, "no") == 0 || strcasecmp(arg
, "false") == 0)
1483 setqosmarking("enabled", "0", s
, afp
);
1485 err(EX_USAGE
, "bad value for fastlane %s", cmd
);
1487 err(EX_USAGE
, "fastlane takes capable or enable");
1491 #else /* defined(SIOCSQOSMARKINGMODE) && defined(SIOCSQOSMARKINGENABLED) */
1494 setfastlane(const char *cmd
, const char *arg
, int s
, const struct afswtch
*afp
)
1499 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1501 if (strcmp(cmd
, "capable") == 0)
1502 ioc
= SIOCSFASTLANECAPABLE
;
1503 else if (strcmp(cmd
, "enable") == 0)
1504 ioc
= SIOCSFASTLEENABLED
;
1506 err(EX_USAGE
, "fastlane takes capable or enabled");
1508 if (strcmp(arg
, "1") == 0 || strcasecmp(arg
, "on") == 0||
1509 strcasecmp(arg
, "yes") == 0 || strcasecmp(arg
, "true") == 0)
1511 else if (strcmp(arg
, "0") == 0 || strcasecmp(arg
, "off") == 0||
1512 strcasecmp(arg
, "no") == 0 || strcasecmp(arg
, "false") == 0)
1515 err(EX_USAGE
, "bad value for fastlane %s", cmd
);
1517 if (ioc
== SIOCSFASTLANECAPABLE
)
1518 ifr
.ifr_fastlane_capable
= value
;
1520 ifr
.ifr_fastlane_enabled
= value
;
1522 if (ioctl(s
, ioc
, (caddr_t
)&ifr
) < 0)
1523 err(EX_OSERR
, "ioctl(%s, %s)", cmd
, arg
);
1528 setqosmarking(const char *cmd
, const char *arg
, int s
, const struct afswtch
*afp
)
1530 if (strcmp(cmd
, "mode") == 0) {
1531 if (strcmp(arg
, "fastlane") == 0)
1532 setfastlane("capable", "on", s
, afp
);
1533 else if (strcmp(arg
, "none") == 0)
1534 setfastlane("capable", "off", s
, afp
);
1536 err(EX_USAGE
, "bad value for qosmarking mode: %s", arg
);
1537 } else if (strcmp(cmd
, "enabled") == 0) {
1538 if (strcmp(arg
, "1") == 0 || strcasecmp(arg
, "on") == 0||
1539 strcasecmp(arg
, "yes") == 0 || strcasecmp(arg
, "true") == 0)
1540 setfastlane("enable", "on", s
, afp
);
1541 else if (strcmp(arg
, "0") == 0 || strcasecmp(arg
, "off") == 0||
1542 strcasecmp(arg
, "no") == 0 || strcasecmp(arg
, "false") == 0)
1543 setfastlane("enable", "off", s
, afp
);
1545 err(EX_USAGE
, "bad value for qosmarking enabled: %s", arg
);
1547 err(EX_USAGE
, "qosmarking takes mode or enabled");
1551 #endif /* defined(SIOCSQOSMARKINGMODE) && defined(SIOCSQOSMARKINGENABLED) */
1554 setlowpowermode(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1556 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1557 ifr
.ifr_low_power_mode
= !!value
;
1559 if (ioctl(s
, SIOCSIFLOWPOWER
, (caddr_t
)&ifr
) < 0)
1568 static struct str2num subfamily_str2num
[] = {
1569 { .str
= "any", .num
= IFRTYPE_SUBFAMILY_ANY
},
1570 { .str
= "USB", .num
= IFRTYPE_SUBFAMILY_USB
},
1571 { .str
= "Bluetooth", .num
= IFRTYPE_SUBFAMILY_BLUETOOTH
},
1572 { .str
= "Wi-Fi", .num
= IFRTYPE_SUBFAMILY_WIFI
},
1573 { .str
= "wifi", .num
= IFRTYPE_SUBFAMILY_WIFI
},
1574 { .str
= "Thunderbolt", .num
= IFRTYPE_SUBFAMILY_THUNDERBOLT
},
1575 { .str
= "reserverd", .num
= IFRTYPE_SUBFAMILY_RESERVED
},
1576 { .str
= "intcoproc", .num
= IFRTYPE_SUBFAMILY_INTCOPROC
},
1577 { .str
= "QuickRelay", .num
= IFRTYPE_SUBFAMILY_QUICKRELAY
},
1578 { .str
= "Default", .num
= IFRTYPE_SUBFAMILY_DEFAULT
},
1579 { .str
= NULL
, .num
= 0 },
1583 get_num_from_str(struct str2num
* str2nums
, const char *str
)
1585 struct str2num
*str2num
= str2nums
;
1587 while (str2num
!= NULL
&& str2num
->str
!= NULL
) {
1588 if (strcasecmp(str2num
->str
, str
) == 0) {
1589 return str2num
->num
;
1597 setifsubfamily(const char *val
, int dummy __unused
, int s
,
1598 const struct afswtch
*afp
)
1600 strlcpy(ifr
.ifr_name
, name
, sizeof (ifr
.ifr_name
));
1603 uint32_t subfamily
= strtoul(val
, &endptr
, 0);
1605 subfamily
= get_num_from_str(subfamily_str2num
, val
);
1606 if (subfamily
== 0) {
1611 ifr
.ifr_type
.ift_subfamily
= subfamily
;
1612 if (ioctl(s
, SIOCSIFSUBFAMILY
, (caddr_t
)&ifr
) < 0)
1613 warn("ioctl(SIOCSIFSUBFAMILY)");
1617 setifavailability(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
1619 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1620 ifr
.ifr_interface_state
.valid_bitmask
= IF_INTERFACE_STATE_INTERFACE_AVAILABILITY_VALID
;
1622 ifr
.ifr_interface_state
.interface_availability
= IF_INTERFACE_STATE_INTERFACE_UNAVAILABLE
;
1624 ifr
.ifr_interface_state
.interface_availability
= IF_INTERFACE_STATE_INTERFACE_AVAILABLE
;
1626 if (ioctl(s
, SIOCSIFINTERFACESTATE
, (caddr_t
)&ifr
) < 0)
1627 warn("ioctl(SIOCSIFINTERFACESTATE)");
1632 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1633 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1637 "\020\1AUTOCONFIGURING\4PROBE_CONNECTIVITY\5FASTLN_CAP\6IPV6_DISABLED\7ACCEPT_RTADV\10TXSTART\11RXPOLL" \
1638 "\12VLAN\13BOND\14ARPLL\15CLAT46\16NOAUTOIPV6LL\17EXPENSIVE\20ROUTER4" \
1639 "\21ROUTER6\22LOCALNET_PRIVATE\23ND6ALT\24RESTRICTED_RECV\25AWDL\26NOACKPRI" \
1640 "\27AWDL_RESTRICTED\30CL2K\31ECN_ENABLE\32ECN_DISABLE\33CHANNEL_DRV\34CA" \
1641 "\35SENDLIST\36DIRECTLINK\37FASTLN_ON\40UPDOWNCHANGE"
1644 "\020\1WOL\2TIMESTAMP\3NOAUTONX\4LEGACY\5TXLOWINET\6RXLOWINET\7ALLOCKPI" \
1645 "\10LOWPOWER\11MPKLOG\12CONSTRAINED"
1648 "\020\1RXCSUM\2TXCSUM\3VLAN_MTU\4VLAN_HWTAGGING\5JUMBO_MTU" \
1649 "\6TSO4\7TSO6\10LRO\11AV\12TXSTATUS\13CHANNEL_IO\14HW_TIMESTAMP\15SW_TIMESTAMP" \
1650 "\16PARTIAL_CSUM\17ZEROINVERT_CSUM"
1652 #define IFRLOGF_BITS \
1653 "\020\1DLIL\21FAMILY\31DRIVER\35FIRMWARE"
1656 * Print the status of the interface. If an address family was
1657 * specified, show only it; otherwise, show them all.
1660 status(const struct afswtch
*afp
, const struct sockaddr_dl
*sdl
,
1661 struct ifaddrs
*ifa
)
1663 struct ifaddrs
*ift
;
1666 struct if_descreq ifdr
;
1667 struct if_linkparamsreq iflpr
;
1669 struct ifmibdata_supplemental ifmsupp
;
1670 size_t miblen
= sizeof(struct ifmibdata_supplemental
);
1671 u_int64_t eflags
= 0;
1672 u_int64_t xflags
= 0;
1677 afp
= af_getbyname("inet");
1681 ifr
.ifr_addr
.sa_family
= afp
->af_af
== AF_LINK
? AF_INET
: afp
->af_af
;
1682 strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
1684 s
= socket(ifr
.ifr_addr
.sa_family
, SOCK_DGRAM
, 0);
1686 err(1, "socket(family %u,SOCK_DGRAM)", ifr
.ifr_addr
.sa_family
);
1688 printf("%s: ", name
);
1689 printb("flags", ifa
->ifa_flags
, IFFBITS
);
1690 if (ioctl(s
, SIOCGIFMETRIC
, &ifr
) != -1)
1692 printf(" metric %d", ifr
.ifr_metric
);
1693 if (ioctl(s
, SIOCGIFMTU
, &ifr
) != -1)
1694 printf(" mtu %d", ifr
.ifr_mtu
);
1695 if (showrtref
&& ioctl(s
, SIOCGIFGETRTREFCNT
, &ifr
) != -1)
1696 printf(" rtref %d", ifr
.ifr_route_refcnt
);
1698 unsigned int ifindex
= if_nametoindex(ifa
->ifa_name
);
1700 printf(" index %u", ifindex
);
1702 #ifdef SIOCGIFCONSTRAINED
1703 // Constrained is stored in if_xflags which isn't exposed directly
1704 if (ioctl(s
, SIOCGIFCONSTRAINED
, (caddr_t
)&ifr
) == 0 &&
1705 ifr
.ifr_constrained
!= 0) {
1706 printf(" constrained");
1711 if (verbose
&& ioctl(s
, SIOCGIFEFLAGS
, (caddr_t
)&ifr
) != -1 &&
1712 (eflags
= ifr
.ifr_eflags
) != 0) {
1713 printb("\teflags", eflags
, IFEFBITS
);
1717 if (verbose
&& ioctl(s
, SIOCGIFXFLAGS
, (caddr_t
)&ifr
) != -1 &&
1718 (xflags
= ifr
.ifr_xflags
) != 0) {
1719 printb("\txflags", xflags
, IFXFBITS
);
1723 if (ioctl(s
, SIOCGIFCAP
, (caddr_t
)&ifr
) == 0) {
1724 if (ifr
.ifr_curcap
!= 0) {
1725 curcap
= ifr
.ifr_curcap
;
1726 printb("\toptions", ifr
.ifr_curcap
, IFCAPBITS
);
1729 if (supmedia
&& ifr
.ifr_reqcap
!= 0) {
1730 printb("\tcapabilities", ifr
.ifr_reqcap
, IFCAPBITS
);
1737 for (ift
= ifa
; ift
!= NULL
; ift
= ift
->ifa_next
) {
1738 if (ift
->ifa_addr
== NULL
)
1740 if (strcmp(ifa
->ifa_name
, ift
->ifa_name
) != 0)
1743 const struct afswtch
*p
;
1744 p
= af_getbyfamily(ift
->ifa_addr
->sa_family
);
1745 if (p
!= NULL
&& p
->af_status
!= NULL
)
1746 p
->af_status(s
, ift
);
1747 } else if (afp
->af_af
== ift
->ifa_addr
->sa_family
)
1748 afp
->af_status(s
, ift
);
1751 /* Print CLAT46 address */
1752 clat46_addr(s
, name
);
1754 /* Print NAT64 prefix */
1755 nat64_status(s
, name
);
1758 if (allfamilies
|| afp
->af_af
== AF_LINK
) {
1759 const struct afswtch
*lafp
;
1762 * Hack; the link level address is received separately
1763 * from the routing information so any address is not
1764 * handled above. Cobble together an entry and invoke
1765 * the status method specially.
1767 lafp
= af_getbyname("lladdr");
1769 info
.rti_info
[RTAX_IFA
] = (struct sockaddr
*)sdl
;
1770 lafp
->af_status(s
, &info
);
1776 else if (afp
->af_other_status
!= NULL
)
1777 afp
->af_other_status(s
);
1779 strlcpy(ifs
.ifs_name
, name
, sizeof ifs
.ifs_name
);
1780 if (ioctl(s
, SIOCGIFSTATUS
, &ifs
) == 0)
1781 printf("%s", ifs
.ascii
);
1783 /* The rest is for when verbose is set; if not set, we're done */
1787 if (ioctl(s
, SIOCGIFTYPE
, &ifr
) != -1) {
1788 char *c
= ift2str(ifr
.ifr_type
.ift_type
,
1789 ifr
.ifr_type
.ift_family
, ifr
.ifr_type
.ift_subfamily
);
1791 printf("\ttype: %s\n", c
);
1795 if (ioctl(s
, SIOCGIFFUNCTIONALTYPE
, &ifr
) != -1) {
1796 char *c
= iffunct2str(ifr
.ifr_functional_type
);
1798 printf("\tfunctional type: %s\n", c
);
1803 struct if_agentidsreq ifar
;
1804 memset(&ifar
, 0, sizeof(ifar
));
1806 strlcpy(ifar
.ifar_name
, name
, sizeof(ifar
.ifar_name
));
1808 if (ioctl(s
, SIOCGIFAGENTIDS
, &ifar
) != -1) {
1809 if (ifar
.ifar_count
!= 0) {
1810 ifar
.ifar_uuids
= calloc(ifar
.ifar_count
, sizeof(uuid_t
));
1811 if (ifar
.ifar_uuids
!= NULL
) {
1812 if (ioctl(s
, SIOCGIFAGENTIDS
, &ifar
) != 1) {
1813 for (int agent_i
= 0; agent_i
< ifar
.ifar_count
; agent_i
++) {
1814 struct netagent_req nar
;
1815 memset(&nar
, 0, sizeof(nar
));
1817 uuid_copy(nar
.netagent_uuid
, ifar
.ifar_uuids
[agent_i
]);
1819 if (ioctl(s
, SIOCGIFAGENTDATA
, &nar
) != 1) {
1820 printf("\tagent domain:%s type:%s flags:0x%x desc:\"%s\"\n",
1821 nar
.netagent_domain
, nar
.netagent_type
,
1822 nar
.netagent_flags
, nar
.netagent_desc
);
1826 free(ifar
.ifar_uuids
);
1832 if (ioctl(s
, SIOCGIFLINKQUALITYMETRIC
, &ifr
) != -1) {
1833 int lqm
= ifr
.ifr_link_quality_metric
;
1835 printf("\tlink quality: %d ", lqm
);
1836 if (lqm
== IFNET_LQM_THRESH_OFF
)
1838 else if (lqm
== IFNET_LQM_THRESH_UNKNOWN
)
1839 printf("(unknown)");
1840 else if (lqm
> IFNET_LQM_THRESH_UNKNOWN
&&
1841 lqm
<= IFNET_LQM_THRESH_BAD
)
1843 else if (lqm
> IFNET_LQM_THRESH_UNKNOWN
&&
1844 lqm
<= IFNET_LQM_THRESH_POOR
)
1846 else if (lqm
> IFNET_LQM_THRESH_POOR
&&
1847 lqm
<= IFNET_LQM_THRESH_GOOD
)
1852 } else if (lqm
> IFNET_LQM_THRESH_UNKNOWN
) {
1853 printf("\tlink quality: %d ", lqm
);
1854 if (lqm
<= IFNET_LQM_THRESH_BAD
)
1856 else if (lqm
<= IFNET_LQM_THRESH_POOR
)
1858 else if (lqm
<= IFNET_LQM_THRESH_GOOD
)
1867 if (ioctl(s
, SIOCGIFINTERFACESTATE
, &ifr
) != -1) {
1869 if (ifr
.ifr_interface_state
.valid_bitmask
&
1870 IF_INTERFACE_STATE_RRC_STATE_VALID
) {
1871 uint8_t rrc_state
= ifr
.ifr_interface_state
.rrc_state
;
1873 printf(" rrc: %u ", rrc_state
);
1874 if (rrc_state
== IF_INTERFACE_STATE_RRC_STATE_CONNECTED
)
1875 printf("(connected)");
1876 else if (rrc_state
== IF_INTERFACE_STATE_RRC_STATE_IDLE
)
1881 if (ifr
.ifr_interface_state
.valid_bitmask
&
1882 IF_INTERFACE_STATE_INTERFACE_AVAILABILITY_VALID
) {
1883 uint8_t ifavail
= ifr
.ifr_interface_state
.interface_availability
;
1885 printf(" availability: %u ", ifavail
);
1886 if (ifavail
== IF_INTERFACE_STATE_INTERFACE_AVAILABLE
)
1888 else if (ifavail
== IF_INTERFACE_STATE_INTERFACE_UNAVAILABLE
)
1893 printf(" availability: (not valid)");
1896 ifr
.ifr_interface_state
.valid_bitmask
&
1897 IF_INTERFACE_STATE_LQM_STATE_VALID
) {
1898 int8_t lqm
= ifr
.ifr_interface_state
.lqm_state
;
1900 printf(" lqm: %d", lqm
);
1902 if (lqm
== IFNET_LQM_THRESH_OFF
)
1904 else if (lqm
== IFNET_LQM_THRESH_UNKNOWN
)
1905 printf("(unknown)");
1906 else if (lqm
== IFNET_LQM_THRESH_BAD
)
1908 else if (lqm
== IFNET_LQM_THRESH_POOR
)
1910 else if (lqm
== IFNET_LQM_THRESH_GOOD
)
1919 bzero(&iflpr
, sizeof (iflpr
));
1920 strlcpy(iflpr
.iflpr_name
, name
, sizeof (iflpr
.iflpr_name
));
1921 if (ioctl(s
, SIOCGIFLINKPARAMS
, &iflpr
) != -1) {
1922 u_int64_t ibw_max
= iflpr
.iflpr_input_bw
.max_bw
;
1923 u_int64_t ibw_eff
= iflpr
.iflpr_input_bw
.eff_bw
;
1924 u_int64_t obw_max
= iflpr
.iflpr_output_bw
.max_bw
;
1925 u_int64_t obw_eff
= iflpr
.iflpr_output_bw
.eff_bw
;
1926 u_int64_t obw_tbr
= iflpr
.iflpr_output_tbr_rate
;
1927 u_int32_t obw_pct
= iflpr
.iflpr_output_tbr_percent
;
1928 u_int64_t ilt_max
= iflpr
.iflpr_input_lt
.max_lt
;
1929 u_int64_t ilt_eff
= iflpr
.iflpr_input_lt
.eff_lt
;
1930 u_int64_t olt_max
= iflpr
.iflpr_output_lt
.max_lt
;
1931 u_int64_t olt_eff
= iflpr
.iflpr_output_lt
.eff_lt
;
1934 if (eflags
& IFEF_TXSTART
) {
1935 u_int32_t flags
= iflpr
.iflpr_flags
;
1936 u_int32_t sched
= iflpr
.iflpr_output_sched
;
1937 struct if_throttlereq iftr
;
1939 printf("\tscheduler: %s%s ",
1940 (flags
& IFLPRF_ALTQ
) ? "ALTQ_" : "",
1942 if (flags
& IFLPRF_DRVMANAGED
)
1943 printf("(driver managed)");
1946 bzero(&iftr
, sizeof (iftr
));
1947 strlcpy(iftr
.ifthr_name
, name
,
1948 sizeof (iftr
.ifthr_name
));
1949 if (ioctl(s
, SIOCGIFTHROTTLE
, &iftr
) != -1 &&
1950 iftr
.ifthr_level
!= IFNET_THROTTLE_OFF
)
1951 printf("\tthrottling: level %d (%s)\n",
1952 iftr
.ifthr_level
, tl2str(iftr
.ifthr_level
));
1955 if (obw_tbr
!= 0 && obw_eff
> obw_tbr
)
1958 if (ibw_max
!= 0 || obw_max
!= 0) {
1959 if (ibw_max
== obw_max
&& ibw_eff
== obw_eff
&&
1960 ibw_max
== ibw_eff
&& obw_tbr
== 0) {
1961 printf("\tlink rate: %s\n",
1962 bps_to_str(ibw_max
));
1964 printf("\tuplink rate: %s [eff] / ",
1965 bps_to_str(obw_eff
));
1968 printf("%s [tbr] / ",
1969 bps_to_str(obw_tbr
));
1971 printf("%s [tbr %u%%] / ",
1972 bps_to_str(obw_tbr
),
1975 printf("%s", bps_to_str(obw_max
));
1979 if (ibw_eff
== ibw_max
) {
1980 printf("\tdownlink rate: %s\n",
1981 bps_to_str(ibw_max
));
1983 printf("\tdownlink rate: "
1984 "%s [eff] / ", bps_to_str(ibw_eff
));
1985 printf("%s [max]\n",
1986 bps_to_str(ibw_max
));
1989 } else if (obw_tbr
!= 0) {
1990 printf("\tuplink rate: %s [tbr]\n",
1991 bps_to_str(obw_tbr
));
1994 if (ilt_max
!= 0 || olt_max
!= 0) {
1995 if (ilt_max
== olt_max
&& ilt_eff
== olt_eff
&&
1996 ilt_max
== ilt_eff
) {
1997 printf("\tlink latency: %s\n",
1998 ns_to_str(ilt_max
));
2000 if (olt_max
!= 0 && olt_eff
== olt_max
) {
2001 printf("\tuplink latency: %s\n",
2002 ns_to_str(olt_max
));
2003 } else if (olt_max
!= 0) {
2004 printf("\tuplink latency: "
2005 "%s [eff] / ", ns_to_str(olt_eff
));
2006 printf("%s [max]\n",
2007 ns_to_str(olt_max
));
2009 if (ilt_max
!= 0 && ilt_eff
== ilt_max
) {
2010 printf("\tdownlink latency: %s\n",
2011 ns_to_str(ilt_max
));
2012 } else if (ilt_max
!= 0) {
2013 printf("\tdownlink latency: "
2014 "%s [eff] / ", ns_to_str(ilt_eff
));
2015 printf("%s [max]\n",
2016 ns_to_str(ilt_max
));
2022 /* Common OID prefix */
2025 mib
[2] = NETLINK_GENERIC
;
2026 mib
[3] = IFMIB_IFDATA
;
2027 mib
[4] = if_nametoindex(name
);
2028 mib
[5] = IFDATA_SUPPLEMENTAL
;
2029 if (sysctl(mib
, 6, &ifmsupp
, &miblen
, (void *)0, 0) == -1)
2030 err(1, "sysctl IFDATA_SUPPLEMENTAL");
2032 if (ifmsupp
.ifmd_data_extended
.ifi_alignerrs
!= 0) {
2033 printf("\tunaligned pkts: %llu\n",
2034 ifmsupp
.ifmd_data_extended
.ifi_alignerrs
);
2036 if (ifmsupp
.ifmd_data_extended
.ifi_dt_bytes
!= 0) {
2037 printf("\tdata milestone interval: %s\n",
2038 bytes_to_str(ifmsupp
.ifmd_data_extended
.ifi_dt_bytes
));
2041 bzero(&ifdr
, sizeof (ifdr
));
2042 strlcpy(ifdr
.ifdr_name
, name
, sizeof (ifdr
.ifdr_name
));
2043 if (ioctl(s
, SIOCGIFDESC
, &ifdr
) != -1 && ifdr
.ifdr_len
) {
2044 printf("\tdesc: %s\n", ifdr
.ifdr_desc
);
2047 if (ioctl(s
, SIOCGIFLOG
, &ifr
) != -1 && ifr
.ifr_log
.ifl_level
) {
2048 printf("\tlogging: level %d ", ifr
.ifr_log
.ifl_level
);
2049 printb("facilities", ifr
.ifr_log
.ifl_flags
, IFRLOGF_BITS
);
2053 if (ioctl(s
, SIOCGIFDELEGATE
, &ifr
) != -1 && ifr
.ifr_delegated
) {
2054 char delegatedif
[IFNAMSIZ
+1];
2055 if (if_indextoname(ifr
.ifr_delegated
, delegatedif
) != NULL
)
2056 printf("\teffective interface: %s\n", delegatedif
);
2059 if (ioctl(s
, SIOCGSTARTDELAY
, &ifr
) != -1) {
2060 if (ifr
.ifr_start_delay_qlen
> 0 &&
2061 ifr
.ifr_start_delay_timeout
> 0) {
2062 printf("\ttxstart qlen: %u packets "
2063 "timeout: %u microseconds\n",
2064 ifr
.ifr_start_delay_qlen
,
2065 ifr
.ifr_start_delay_timeout
/1000);
2068 #if defined(IFCAP_HW_TIMESTAMP) && defined(IFCAP_SW_TIMESTAMP)
2069 if ((curcap
& (IFCAP_HW_TIMESTAMP
| IFCAP_SW_TIMESTAMP
)) &&
2070 ioctl(s
, SIOCGIFTIMESTAMPENABLED
, &ifr
) != -1) {
2071 printf("\ttimestamp: %s\n",
2072 (ifr
.ifr_intval
!= 0) ? "enabled" : "disabled");
2075 #if defined(SIOCGQOSMARKINGENABLED) && defined(SIOCGQOSMARKINGMODE)
2076 if (ioctl(s
, SIOCGQOSMARKINGENABLED
, &ifr
) != -1) {
2077 printf("\tqosmarking enabled: %s mode: ",
2078 ifr
.ifr_qosmarking_enabled
? "yes" : "no");
2079 if (ioctl(s
, SIOCGQOSMARKINGMODE
, &ifr
) != -1) {
2080 switch (ifr
.ifr_qosmarking_mode
) {
2081 case IFRTYPE_QOSMARKING_FASTLANE
:
2082 printf("fastlane\n");
2084 case IFRTYPE_QOSMARKING_RFC4594
:
2085 printf("RFC4594\n");
2087 case IFRTYPE_QOSMARKING_MODE_NONE
:
2091 printf("unknown (%u)\n", ifr
.ifr_qosmarking_mode
);
2096 #endif /* defined(SIOCGQOSMARKINGENABLED) && defined(SIOCGQOSMARKINGMODE) */
2098 if (verbose
> 0 && ioctl(s
, SIOCGIFLOWPOWER
, &ifr
) != -1) {
2099 printf("\tlow power mode: %s\n",
2100 (ifr
.ifr_low_power_mode
!= 0) ? "enabled" : "disabled");
2102 if (verbose
> 0 && ioctl(s
, SIOCGIFMPKLOG
, &ifr
) != -1) {
2103 printf("\tmulti layer packet logging (mpklog): %s\n",
2104 (ifr
.ifr_mpk_log
!= 0) ? "enabled" : "disabled");
2111 #define KILOBYTES 1024
2112 #define MEGABYTES (KILOBYTES * KILOBYTES)
2113 #define GIGABYTES (KILOBYTES * KILOBYTES * KILOBYTES)
2116 bytes_to_str(unsigned long long bytes
)
2118 static char buf
[32];
2120 long double n
= bytes
, t
;
2122 if (bytes
>= GIGABYTES
) {
2125 } else if (n
>= MEGABYTES
) {
2128 } else if (n
>= KILOBYTES
) {
2136 snprintf(buf
, sizeof (buf
), "%-4.2Lf %s", t
, u
);
2140 #define GIGABIT_PER_SEC 1000000000 /* gigabit per second */
2141 #define MEGABIT_PER_SEC 1000000 /* megabit per second */
2142 #define KILOBIT_PER_SEC 1000 /* kilobit per second */
2145 bps_to_str(unsigned long long rate
)
2147 static char buf
[32];
2149 long double n
= rate
, t
;
2151 if (rate
>= GIGABIT_PER_SEC
) {
2152 t
= n
/ GIGABIT_PER_SEC
;
2154 } else if (n
>= MEGABIT_PER_SEC
) {
2155 t
= n
/ MEGABIT_PER_SEC
;
2157 } else if (n
>= KILOBIT_PER_SEC
) {
2158 t
= n
/ KILOBIT_PER_SEC
;
2165 snprintf(buf
, sizeof (buf
), "%-4.2Lf %4s", t
, u
);
2169 #define NSEC_PER_SEC 1000000000 /* nanosecond per second */
2170 #define USEC_PER_SEC 1000000 /* microsecond per second */
2171 #define MSEC_PER_SEC 1000 /* millisecond per second */
2174 ns_to_str(unsigned long long nsec
)
2176 static char buf
[32];
2178 long double n
= nsec
, t
;
2180 if (nsec
>= NSEC_PER_SEC
) {
2181 t
= n
/ NSEC_PER_SEC
;
2183 } else if (n
>= USEC_PER_SEC
) {
2184 t
= n
/ USEC_PER_SEC
;
2186 } else if (n
>= MSEC_PER_SEC
) {
2187 t
= n
/ MSEC_PER_SEC
;
2194 snprintf(buf
, sizeof (buf
), "%-4.2Lf %4s", t
, u
);
2199 tunnel_status(int s
)
2201 af_all_tunnel_status(s
);
2205 clat46_addr(int s
, char * if_name
)
2207 struct if_clat46req ifr
;
2208 char buf
[MAXHOSTNAMELEN
];
2210 bzero(&ifr
, sizeof (ifr
));
2211 strlcpy(ifr
.ifclat46_name
, if_name
, sizeof(ifr
.ifclat46_name
));
2213 if (ioctl(s
, SIOCGIFCLAT46ADDR
, &ifr
) < 0) {
2214 if (errno
!= ENOENT
)
2215 syslog(LOG_WARNING
, "ioctl (SIOCGIFCLAT46ADDR): %d", errno
);
2219 if (inet_ntop(AF_INET6
, &ifr
.ifclat46_addr
.v6_address
, buf
, sizeof(buf
)) != NULL
)
2220 printf("\tinet6 %s prefixlen %d clat46\n",
2221 buf
, ifr
.ifclat46_addr
.v6_prefixlen
);
2225 nat64_status(int s
, char * if_name
)
2228 struct if_nat64req ifr
;
2229 char buf
[MAXHOSTNAMELEN
];
2231 bzero(&ifr
, sizeof(ifr
));
2232 strlcpy(ifr
.ifnat64_name
, if_name
, sizeof(ifr
.ifnat64_name
));
2234 if (ioctl(s
, SIOCGIFNAT64PREFIX
, &ifr
) < 0) {
2235 if (errno
!= ENOENT
)
2236 syslog(LOG_WARNING
, "ioctl(SIOCGIFNAT64PREFIX): %d", errno
);
2240 for (i
= 0; i
< NAT64_MAX_NUM_PREFIXES
; i
++) {
2241 if (ifr
.ifnat64_prefixes
[i
].prefix_len
> 0) {
2242 inet_ntop(AF_INET6
, &ifr
.ifnat64_prefixes
[i
].ipv6_prefix
, buf
, sizeof(buf
));
2243 printf("\tnat64 prefix %s prefixlen %d\n",
2244 buf
, ifr
.ifnat64_prefixes
[i
].prefix_len
<< 3);
2250 Perror(const char *cmd
)
2255 errx(1, "%s: no such interface", cmd
);
2259 errx(1, "%s: permission denied", cmd
);
2268 * Print a value a la the %b format of the kernel's printf
2271 printb(const char *s
, unsigned v
, const char *bits
)
2276 if (bits
&& *bits
== 8)
2277 printf("%s=%o", s
, v
);
2279 printf("%s=%x", s
, v
);
2283 while ((i
= *bits
++) != '\0') {
2284 if (v
& (1 << (i
-1))) {
2288 for (; (c
= *bits
) > 32; bits
++)
2291 for (; *bits
> 32; bits
++)
2300 ifmaybeload(const char *name
)
2302 #define MOD_PREFIX_LEN 3 /* "if_" */
2303 struct module_stat mstat
;
2305 char ifkind
[IFNAMSIZ
+ MOD_PREFIX_LEN
], ifname
[IFNAMSIZ
], *dp
;
2308 /* loading suppressed by the user */
2312 /* trim the interface number off the end */
2313 strlcpy(ifname
, name
, sizeof(ifname
));
2314 for (dp
= ifname
; *dp
!= 0; dp
++)
2320 /* turn interface and unit into module name */
2321 strlcpy(ifkind
, "if_", sizeof(ifkind
));
2322 strlcpy(ifkind
+ MOD_PREFIX_LEN
, ifname
,
2323 sizeof(ifkind
) - MOD_PREFIX_LEN
);
2325 /* scan files in kernel */
2326 mstat
.version
= sizeof(struct module_stat
);
2327 for (fileid
= kldnext(0); fileid
> 0; fileid
= kldnext(fileid
)) {
2328 /* scan modules in file */
2329 for (modid
= kldfirstmod(fileid
); modid
> 0;
2330 modid
= modfnext(modid
)) {
2331 if (modstat(modid
, &mstat
) < 0)
2333 /* strip bus name if present */
2334 if ((cp
= strchr(mstat
.name
, '/')) != NULL
) {
2339 /* already loaded? */
2340 if (strncmp(ifname
, cp
, strlen(ifname
) + 1) == 0 ||
2341 strncmp(ifkind
, cp
, strlen(ifkind
) + 1) == 0)
2346 /* not present, we should try to load it */
2351 static struct cmd basic_cmds
[] = {
2352 DEF_CMD("up", IFF_UP
, setifflags
),
2353 DEF_CMD("down", -IFF_UP
, setifflags
),
2354 DEF_CMD("arp", -IFF_NOARP
, setifflags
),
2355 DEF_CMD("-arp", IFF_NOARP
, setifflags
),
2356 DEF_CMD("debug", IFF_DEBUG
, setifflags
),
2357 DEF_CMD("-debug", -IFF_DEBUG
, setifflags
),
2359 DEF_CMD("promisc", IFF_PPROMISC
, setifflags
),
2360 DEF_CMD("-promisc", -IFF_PPROMISC
, setifflags
),
2361 #endif /* IFF_PPROMISC */
2362 DEF_CMD("add", IFF_UP
, notealias
),
2363 DEF_CMD("alias", IFF_UP
, notealias
),
2364 DEF_CMD("-alias", -IFF_UP
, notealias
),
2365 DEF_CMD("delete", -IFF_UP
, notealias
),
2366 DEF_CMD("remove", -IFF_UP
, notealias
),
2368 #define EN_SWABIPS 0x1000
2369 DEF_CMD("swabips", EN_SWABIPS
, setifflags
),
2370 DEF_CMD("-swabips", -EN_SWABIPS
, setifflags
),
2372 DEF_CMD_ARG("netmask", setifnetmask
),
2373 DEF_CMD_ARG("metric", setifmetric
),
2374 DEF_CMD_ARG("broadcast", setifbroadaddr
),
2375 DEF_CMD_ARG("ipdst", setifipdst
),
2376 DEF_CMD_ARG2("tunnel", settunnel
),
2377 DEF_CMD("-tunnel", 0, deletetunnel
),
2378 DEF_CMD("deletetunnel", 0, deletetunnel
),
2379 DEF_CMD("link0", IFF_LINK0
, setifflags
),
2380 DEF_CMD("-link0", -IFF_LINK0
, setifflags
),
2381 DEF_CMD("link1", IFF_LINK1
, setifflags
),
2382 DEF_CMD("-link1", -IFF_LINK1
, setifflags
),
2383 DEF_CMD("link2", IFF_LINK2
, setifflags
),
2384 DEF_CMD("-link2", -IFF_LINK2
, setifflags
),
2386 DEF_CMD("monitor", IFF_MONITOR
:, setifflags
),
2387 DEF_CMD("-monitor", -IFF_MONITOR
, setifflags
),
2388 #endif /* IFF_MONITOR */
2389 DEF_CMD("mpklog", 1, setifmpklog
),
2390 DEF_CMD("-mpklog", 0, setifmpklog
),
2391 #ifdef IFF_STATICARP
2392 DEF_CMD("staticarp", IFF_STATICARP
, setifflags
),
2393 DEF_CMD("-staticarp", -IFF_STATICARP
, setifflags
),
2394 #endif /* IFF_STATICARP */
2396 DEF_CMD("rxcsum", IFCAP_RXCSUM
, setifcap
),
2397 DEF_CMD("-rxcsum", -IFCAP_RXCSUM
, setifcap
),
2398 #endif /* IFCAP_RXCSUM */
2400 DEF_CMD("txcsum", IFCAP_TXCSUM
, setifcap
),
2401 DEF_CMD("-txcsum", -IFCAP_TXCSUM
, setifcap
),
2402 #endif /* IFCAP_TXCSUM */
2403 #ifdef IFCAP_NETCONS
2404 DEF_CMD("netcons", IFCAP_NETCONS
, setifcap
),
2405 DEF_CMD("-netcons", -IFCAP_NETCONS
, setifcap
),
2406 #endif /* IFCAP_NETCONS */
2407 #ifdef IFCAP_POLLING
2408 DEF_CMD("polling", IFCAP_POLLING
, setifcap
),
2409 DEF_CMD("-polling", -IFCAP_POLLING
, setifcap
),
2410 #endif /* IFCAP_POLLING */
2412 DEF_CMD("tso", IFCAP_TSO
, setifcap
),
2413 DEF_CMD("-tso", -IFCAP_TSO
, setifcap
),
2414 #endif /* IFCAP_TSO */
2416 DEF_CMD("lro", IFCAP_LRO
, setifcap
),
2417 DEF_CMD("-lro", -IFCAP_LRO
, setifcap
),
2418 #endif /* IFCAP_LRO */
2420 DEF_CMD("wol", IFCAP_WOL
, setifcap
),
2421 DEF_CMD("-wol", -IFCAP_WOL
, setifcap
),
2422 #endif /* IFCAP_WOL */
2423 #ifdef IFCAP_WOL_UCAST
2424 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST
, setifcap
),
2425 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST
, setifcap
),
2426 #endif /* IFCAP_WOL_UCAST */
2427 #ifdef IFCAP_WOL_MCAST
2428 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST
, setifcap
),
2429 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST
, setifcap
),
2430 #endif /* IFCAP_WOL_MCAST */
2431 #ifdef IFCAP_WOL_MAGIC
2432 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC
, setifcap
),
2433 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC
, setifcap
),
2434 #endif /* IFCAP_WOL_MAGIC */
2435 DEF_CMD("normal", -IFF_LINK0
, setifflags
),
2436 DEF_CMD("compress", IFF_LINK0
, setifflags
),
2437 DEF_CMD("noicmp", IFF_LINK1
, setifflags
),
2438 DEF_CMD_ARG("mtu", setifmtu
),
2440 DEF_CMD_ARG("name", setifname
),
2443 DEF_CMD("av", IFCAP_AV
, setifcap
),
2444 DEF_CMD("-av", -IFCAP_AV
, setifcap
),
2445 #endif /* IFCAP_AV */
2446 DEF_CMD("router", 1, setrouter
),
2447 DEF_CMD("-router", 0, setrouter
),
2448 DEF_CMD_ARG("desc", setifdesc
),
2449 DEF_CMD_ARG("tbr", settbr
),
2450 DEF_CMD_VA("netem", setnetem
),
2451 DEF_CMD_ARG("throttle", setthrottle
),
2452 DEF_CMD_ARG("log", setlog
),
2453 DEF_CMD("cl2k", 1, setcl2k
),
2454 DEF_CMD("-cl2k", 0, setcl2k
),
2455 DEF_CMD("expensive", 1, setexpensive
),
2456 DEF_CMD("-expensive", 0, setexpensive
),
2457 #ifdef SIOCSIFCONSTRAINED
2458 DEF_CMD("constrained", 1, setconstrained
),
2459 DEF_CMD("-constrained", 0, setconstrained
),
2461 DEF_CMD("timestamp", 1, settimestamp
),
2462 DEF_CMD("-timestamp", 0, settimestamp
),
2463 DEF_CMD_ARG("ecn", setecnmode
),
2464 DEF_CMD_ARG2("fastlane", setfastlane
),
2465 DEF_CMD_ARG2("qosmarking", setqosmarking
),
2466 DEF_CMD_ARG("disable_output", setdisableoutput
),
2467 DEF_CMD("probe_connectivity", 1, setprobeconnectivity
),
2468 DEF_CMD("-probe_connectivity", 0, setprobeconnectivity
),
2469 DEF_CMD("lowpowermode", 1, setlowpowermode
),
2470 DEF_CMD("-lowpowermode", 0, setlowpowermode
),
2471 DEF_CMD_ARG("subfamily", setifsubfamily
),
2472 DEF_CMD("available", 1, setifavailability
),
2473 DEF_CMD("-available", 0, setifavailability
),
2474 DEF_CMD("unavailable", 0, setifavailability
),
2477 static __constructor
void
2480 #define N(a) (sizeof(a) / sizeof(a[0]))
2483 for (i
= 0; i
< N(basic_cmds
); i
++)
2484 cmd_register(&basic_cmds
[i
]);
2489 sched2str(unsigned int s
)
2494 case PKTSCHEDT_NONE
:
2503 case PKTSCHEDT_FQ_CODEL
:
2515 tl2str(unsigned int s
)
2520 case IFNET_THROTTLE_OFF
:
2523 case IFNET_THROTTLE_OPPORTUNISTIC
:
2524 c
= "opportunistic";
2535 ift2str(unsigned int t
, unsigned int f
, unsigned int sf
)
2537 static char buf
[256];
2543 case IFRTYPE_SUBFAMILY_USB
:
2546 case IFRTYPE_SUBFAMILY_BLUETOOTH
:
2547 c
= "Bluetooth PAN";
2549 case IFRTYPE_SUBFAMILY_WIFI
:
2552 case IFRTYPE_SUBFAMILY_THUNDERBOLT
:
2553 c
= "IP over Thunderbolt";
2555 case IFRTYPE_SUBFAMILY_ANY
:
2563 c
= "IP over FireWire";
2567 c
= "Packet capture";
2575 if (ifr
.ifr_type
.ift_family
== APPLE_IF_FAM_IPSEC
) {
2576 if (ifr
.ifr_type
.ift_subfamily
== IFRTYPE_SUBFAMILY_BLUETOOTH
) {
2577 c
= "Companion Link Bluetooth";
2578 } else if (ifr
.ifr_type
.ift_subfamily
== IFRTYPE_SUBFAMILY_QUICKRELAY
) {
2579 c
= "Companion Link QuickRelay";
2580 } else if (ifr
.ifr_type
.ift_subfamily
== IFRTYPE_SUBFAMILY_WIFI
) {
2581 c
= "Companion Link Wi-Fi";
2582 } else if (ifr
.ifr_type
.ift_subfamily
== IFRTYPE_SUBFAMILY_DEFAULT
) {
2583 c
= "Companion Link Default";
2596 case IFT_IEEE8023ADLAG
:
2603 (void) snprintf(buf
, sizeof (buf
),
2604 "0x%x family: %u subfamily: %u",
2605 ifr
.ifr_type
.ift_type
, ifr
.ifr_type
.ift_family
,
2606 ifr
.ifr_type
.ift_subfamily
);
2608 (void) snprintf(buf
, sizeof (buf
),
2609 "%s (0x%x) family: %u subfamily: %u", c
,
2610 ifr
.ifr_type
.ift_type
, ifr
.ifr_type
.ift_family
,
2611 ifr
.ifr_type
.ift_subfamily
);
2620 iffunct2str(u_int32_t functional_type
)
2624 switch (functional_type
) {
2625 case IFRTYPE_FUNCTIONAL_UNKNOWN
:
2628 case IFRTYPE_FUNCTIONAL_LOOPBACK
:
2632 case IFRTYPE_FUNCTIONAL_WIRED
:
2636 case IFRTYPE_FUNCTIONAL_WIFI_INFRA
:
2640 case IFRTYPE_FUNCTIONAL_WIFI_AWDL
:
2644 case IFRTYPE_FUNCTIONAL_CELLULAR
:
2648 case IFRTYPE_FUNCTIONAL_INTCOPROC
:
2651 case IFRTYPE_FUNCTIONAL_COMPANIONLINK
:
2652 str
= "companionlink";