2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
40 static const char rcsid[] =
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/module.h>
47 #include <sys/linker.h>
48 #include <sys/socket.h>
51 #include <net/ethernet.h>
53 #include <net/if_var.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <arpa/inet.h>
80 * Since "struct ifreq" is composed of various union members, callers
81 * should pay special attention to interpret the value.
82 * (.e.g. little/big endian difference in the structure.)
99 int printkeys = 0; /* Print keying material for interfaces. */
101 static int ifconfig(int argc, char *const *argv, int iscreate,
102 const struct afswtch *afp);
103 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
104 struct ifaddrs *ifa);
105 static void tunnel_status(int s);
106 static void usage(void);
108 static struct afswtch *af_getbyname(const char *name);
109 static struct afswtch *af_getbyfamily(int af);
110 static void af_other_status(int);
112 void printifnamemaybe(void);
114 static struct option *opts = NULL;
117 opt_register(struct option *p)
129 /* XXX not right but close enough for now */
131 for (p = opts; p != NULL; p = p->next) {
132 strlcat(options, p->opt_usage, sizeof(options));
133 strlcat(options, " ", sizeof(options));
137 "usage: ifconfig %sinterface address_family [address [dest_address]]\n"
139 " ifconfig interface create\n"
140 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
141 " ifconfig -l [-d] [-u] [address_family]\n"
142 " ifconfig %s[-d] [-m] [-u] [-v]\n",
143 options, options, options);
147 void printifnamemaybe()
150 printf("%s\n", name);
154 main(int argc, char *argv[])
156 int c, all, namesonly, downonly, uponly;
157 const struct afswtch *afp = NULL;
159 struct ifaddrs *ifap, *ifa;
161 const struct sockaddr_dl *sdl;
162 char options[1024], *cp, *namecp = NULL;
167 all = downonly = uponly = namesonly = noload = verbose = 0;
170 * Ensure we print interface name when expected to,
171 * even if we terminate early due to error.
173 atexit(printifnamemaybe);
175 /* Parse leading line options */
176 strlcpy(options, "adklmnuv", sizeof(options));
177 for (p = opts; p != NULL; p = p->next)
178 strlcat(options, p->opt, sizeof(options));
179 while ((c = getopt(argc, argv, options)) != -1) {
181 case 'a': /* scan all interfaces */
184 case 'd': /* restrict scan to "down" interfaces */
190 case 'l': /* scan interface names only */
193 case 'm': /* show media choices in status */
196 case 'n': /* suppress module loading */
199 case 'u': /* restrict scan to "up" interfaces */
206 for (p = opts; p != NULL; p = p->next)
207 if (p->opt[0] == c) {
219 /* -l cannot be used with -a or -m */
220 if (namesonly && (all || supmedia))
224 if (uponly && downonly)
227 /* no arguments is equivalent to '-a' */
228 if (!namesonly && argc < 1)
231 /* -a and -l allow an address family arg to limit the output */
232 if (all || namesonly) {
239 afp = af_getbyname(*argv);
241 warnx("Address family '%s' unknown.", *argv);
244 if (afp->af_name != NULL)
246 /* leave with afp non-zero */
249 /* not listing, need an argument */
256 /* check and maybe load support for this interface */
259 ifindex = if_nametoindex(ifname);
262 * NOTE: We must special-case the `create' command
263 * right here as we would otherwise fail when trying
264 * to find the interface.
266 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
267 strcmp(argv[0], "plumb") == 0)) {
268 iflen = strlcpy(name, ifname, sizeof(name));
269 if (iflen >= sizeof(name))
270 errx(1, "%s: cloning name too long",
272 ifconfig(argc, argv, 1, NULL);
277 * NOTE: We have to special-case the `-vnet' command
278 * right here as we would otherwise fail when trying
279 * to find the interface as it lives in another vnet.
281 if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
282 iflen = strlcpy(name, ifname, sizeof(name));
283 if (iflen >= sizeof(name))
284 errx(1, "%s: interface name too long",
286 ifconfig(argc, argv, 0, NULL);
290 errx(1, "interface %s does not exist", ifname);
294 /* Check for address family */
296 afp = af_getbyname(*argv);
301 if (getifaddrs(&ifap) != 0)
302 err(EXIT_FAILURE, "getifaddrs");
305 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
306 memset(&paifr, 0, sizeof(paifr));
307 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
308 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
309 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
310 ifa->ifa_addr->sa_len);
313 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
315 if (ifa->ifa_addr->sa_family == AF_LINK)
316 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
319 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
321 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
322 if (iflen >= sizeof(name)) {
323 warnx("%s: interface name too long, skipping",
329 if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
331 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
333 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
336 * Are we just listing the interfaces?
342 /* special case for "ether" address family */
343 if (!strcmp(afp->af_name, "ether")) {
345 (sdl->sdl_type != IFT_ETHER &&
346 sdl->sdl_type != IFT_L2VLAN &&
347 sdl->sdl_type != IFT_BRIDGE) ||
348 sdl->sdl_alen != ETHER_ADDR_LEN)
351 if (ifa->ifa_addr->sa_family != afp->af_af)
365 ifconfig(argc, argv, 0, afp);
367 status(afp, sdl, ifa);
376 static struct afswtch *afs = NULL;
379 af_register(struct afswtch *p)
385 static struct afswtch *
386 af_getbyname(const char *name)
390 for (afp = afs; afp != NULL; afp = afp->af_next)
391 if (strcmp(afp->af_name, name) == 0)
396 static struct afswtch *
397 af_getbyfamily(int af)
401 for (afp = afs; afp != NULL; afp = afp->af_next)
402 if (afp->af_af == af)
408 af_other_status(int s)
411 uint8_t afmask[howmany(AF_MAX, NBBY)];
413 memset(afmask, 0, sizeof(afmask));
414 for (afp = afs; afp != NULL; afp = afp->af_next) {
415 if (afp->af_other_status == NULL)
417 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
419 afp->af_other_status(s);
420 setbit(afmask, afp->af_af);
425 af_all_tunnel_status(int s)
428 uint8_t afmask[howmany(AF_MAX, NBBY)];
430 memset(afmask, 0, sizeof(afmask));
431 for (afp = afs; afp != NULL; afp = afp->af_next) {
432 if (afp->af_status_tunnel == NULL)
434 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
436 afp->af_status_tunnel(s);
437 setbit(afmask, afp->af_af);
441 static struct cmd *cmds = NULL;
444 cmd_register(struct cmd *p)
450 static const struct cmd *
451 cmd_lookup(const char *name, int iscreate)
455 for (p = cmds; p != NULL; p = p->c_next)
456 if (strcmp(name, p->c_name) == 0) {
469 callback_func *cb_func;
471 struct callback *cb_next;
473 static struct callback *callbacks = NULL;
476 callback_register(callback_func *func, void *arg)
480 cb = malloc(sizeof(struct callback));
482 errx(1, "unable to allocate memory for callback");
485 cb->cb_next = callbacks;
489 /* specially-handled commands */
490 static void setifaddr(const char *, int, int, const struct afswtch *);
491 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
493 static void setifdstaddr(const char *, int, int, const struct afswtch *);
494 static const struct cmd setifdstaddr_cmd =
495 DEF_CMD("ifdstaddr", 0, setifdstaddr);
498 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
500 const struct afswtch *afp, *nafp;
505 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
510 * This is the historical "accident" allowing users to configure IPv4
511 * addresses without the "inet" keyword which while a nice feature has
512 * proven to complicate other things. We cannot remove this but only
513 * make sure we will never have a similar implicit default for IPv6 or
514 * any other address familiy. We need a fallback though for
515 * ifconfig IF up/down etc. to work without INET support as people
516 * never used ifconfig IF link up/down, etc. either.
520 if (afp == NULL && feature_present("inet"))
521 afp = af_getbyname("inet");
525 afp = af_getbyname("link");
527 warnx("Please specify an address_family.");
531 ifr.ifr_addr.sa_family =
532 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
533 AF_LOCAL : afp->af_af;
535 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
536 (uafp != NULL || errno != EAFNOSUPPORT ||
537 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
538 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
541 p = cmd_lookup(*argv, iscreate);
542 if (iscreate && p == NULL) {
544 * Push the clone create callback so the new
545 * device is created and can be used for any
546 * remaining arguments.
550 errx(1, "internal error, no callback");
551 callbacks = cb->cb_next;
552 cb->cb_func(s, cb->cb_arg);
555 * Handle any address family spec that
556 * immediately follows and potentially
557 * recreate the socket.
559 nafp = af_getbyname(*argv);
569 * Look for a normal parameter.
575 * Not a recognized command, choose between setting
576 * the interface address and the dst address.
578 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
580 if (p->c_u.c_func || p->c_u.c_func2) {
581 if (p->c_parameter == NEXTARG) {
583 errx(1, "'%s' requires argument",
585 p->c_u.c_func(argv[1], 0, s, afp);
587 } else if (p->c_parameter == OPTARG) {
588 p->c_u.c_func(argv[1], 0, s, afp);
591 } else if (p->c_parameter == NEXTARG2) {
593 errx(1, "'%s' requires 2 arguments",
595 p->c_u.c_func2(argv[1], argv[2], s, afp);
596 argc -= 2, argv += 2;
598 p->c_u.c_func(*argv, p->c_parameter, s, afp);
604 * Do any post argument processing required by the address family.
606 if (afp->af_postproc != NULL)
607 afp->af_postproc(s, afp);
609 * Do deferred callbacks registered while processing
610 * command-line arguments.
612 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
613 cb->cb_func(s, cb->cb_arg);
615 * Do deferred operations.
618 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
619 warnx("interface %s cannot change %s addresses!",
626 strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
627 sizeof ifr.ifr_name);
628 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
630 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
631 /* means no previous address for interface */
633 Perror("ioctl (SIOCDIFADDR)");
637 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
638 warnx("interface %s cannot change %s addresses!",
643 if (newaddr && (setaddr || setmask)) {
644 strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
645 sizeof ifr.ifr_name);
646 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
647 Perror("ioctl (SIOCAIFADDR)");
656 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
658 if (afp->af_getaddr == NULL)
661 * Delay the ioctl to set the interface addr until flags are all set.
662 * The address interpretation may depend on the flags,
663 * and the flags may change when the address is set.
666 if (doalias == 0 && afp->af_af != AF_LINK)
668 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
672 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
674 struct addrinfo *srcres, *dstres;
677 if (afp->af_settunnel == NULL) {
678 warn("address family %s does not support tunnel setup",
683 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
684 errx(1, "error in parsing address string: %s",
685 gai_strerror(ecode));
687 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
688 errx(1, "error in parsing address string: %s",
689 gai_strerror(ecode));
691 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
693 "source and destination address families do not match");
695 afp->af_settunnel(s, srcres, dstres);
697 freeaddrinfo(srcres);
698 freeaddrinfo(dstres);
703 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
706 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
707 err(1, "SIOCDIFPHYADDR");
712 setifvnet(const char *jname, int dummy __unused, int s,
713 const struct afswtch *afp)
717 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
718 my_ifr.ifr_jid = jail_getid(jname);
719 if (my_ifr.ifr_jid < 0)
720 errx(1, "%s", jail_errmsg);
721 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
722 err(1, "SIOCSIFVNET");
726 setifrvnet(const char *jname, int dummy __unused, int s,
727 const struct afswtch *afp)
731 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
732 my_ifr.ifr_jid = jail_getid(jname);
733 if (my_ifr.ifr_jid < 0)
734 errx(1, "%s", jail_errmsg);
735 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
736 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
741 setifnetmask(const char *addr, int dummy __unused, int s,
742 const struct afswtch *afp)
744 if (afp->af_getaddr != NULL) {
746 afp->af_getaddr(addr, MASK);
751 setifbroadaddr(const char *addr, int dummy __unused, int s,
752 const struct afswtch *afp)
754 if (afp->af_getaddr != NULL)
755 afp->af_getaddr(addr, DSTADDR);
759 setifipdst(const char *addr, int dummy __unused, int s,
760 const struct afswtch *afp)
762 const struct afswtch *inet;
764 inet = af_getbyname("inet");
767 inet->af_getaddr(addr, DSTADDR);
773 notealias(const char *addr, int param, int s, const struct afswtch *afp)
775 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
776 if (setaddr && doalias == 0 && param < 0)
777 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
778 bcopy((caddr_t)rqtosa(af_addreq),
779 (caddr_t)rqtosa(af_ridreq),
780 rqtosa(af_addreq)->sa_len);
792 setifdstaddr(const char *addr, int param __unused, int s,
793 const struct afswtch *afp)
795 if (afp->af_getaddr != NULL)
796 afp->af_getaddr(addr, DSTADDR);
800 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
801 * of the ifreq structure, which may confuse other parts of ifconfig.
802 * Make a private copy so we can avoid that.
805 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
810 memset(&my_ifr, 0, sizeof(my_ifr));
811 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
813 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
814 Perror("ioctl (SIOCGIFFLAGS)");
817 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
824 my_ifr.ifr_flags = flags & 0xffff;
825 my_ifr.ifr_flagshigh = flags >> 16;
826 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
831 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
835 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
836 Perror("ioctl (SIOCGIFCAP)");
839 flags = ifr.ifr_curcap;
845 flags &= ifr.ifr_reqcap;
846 ifr.ifr_reqcap = flags;
847 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
852 setifmetric(const char *val, int dummy __unused, int s,
853 const struct afswtch *afp)
855 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
856 ifr.ifr_metric = atoi(val);
857 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
858 err(1, "ioctl SIOCSIFMETRIC (set metric)");
862 setifmtu(const char *val, int dummy __unused, int s,
863 const struct afswtch *afp)
865 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
866 ifr.ifr_mtu = atoi(val);
867 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
868 err(1, "ioctl SIOCSIFMTU (set mtu)");
872 setifname(const char *val, int dummy __unused, int s,
873 const struct afswtch *afp)
877 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
879 newname = strdup(val);
881 err(1, "no memory to set ifname");
882 ifr.ifr_data = newname;
883 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
885 err(1, "ioctl SIOCSIFNAME (set name)");
888 strlcpy(name, newname, sizeof(name));
894 setifdescr(const char *val, int dummy __unused, int s,
895 const struct afswtch *afp)
899 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
901 ifr.ifr_buffer.length = strlen(val) + 1;
902 if (ifr.ifr_buffer.length == 1) {
903 ifr.ifr_buffer.buffer = newdescr = NULL;
904 ifr.ifr_buffer.length = 0;
906 newdescr = strdup(val);
907 ifr.ifr_buffer.buffer = newdescr;
908 if (newdescr == NULL) {
909 warn("no memory to set ifdescr");
914 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
915 err(1, "ioctl SIOCSIFDESCR (set descr)");
922 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
925 setifdescr("", 0, s, 0);
929 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
930 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
931 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
934 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
935 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
936 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
937 "\26RXCSUM_IPV6\27TXCSUM_IPV6"
940 * Print the status of the interface. If an address family was
941 * specified, show only it; otherwise, show them all.
944 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
953 ifr.ifr_addr.sa_family = AF_LOCAL;
956 ifr.ifr_addr.sa_family =
957 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
959 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
961 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
963 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
965 printf("%s: ", name);
966 printb("flags", ifa->ifa_flags, IFFBITS);
967 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
968 printf(" metric %d", ifr.ifr_metric);
969 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
970 printf(" mtu %d", ifr.ifr_mtu);
974 if ((descr = reallocf(descr, descrlen)) != NULL) {
975 ifr.ifr_buffer.buffer = descr;
976 ifr.ifr_buffer.length = descrlen;
977 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
978 if (ifr.ifr_buffer.buffer == descr) {
979 if (strlen(descr) > 0)
980 printf("\tdescription: %s\n",
982 } else if (ifr.ifr_buffer.length > descrlen) {
983 descrlen = ifr.ifr_buffer.length;
988 warn("unable to allocate memory for interface"
993 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
994 if (ifr.ifr_curcap != 0) {
995 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
998 if (supmedia && ifr.ifr_reqcap != 0) {
999 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1006 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1007 if (ift->ifa_addr == NULL)
1009 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1012 const struct afswtch *p;
1013 p = af_getbyfamily(ift->ifa_addr->sa_family);
1014 if (p != NULL && p->af_status != NULL)
1015 p->af_status(s, ift);
1016 } else if (afp->af_af == ift->ifa_addr->sa_family)
1017 afp->af_status(s, ift);
1020 if (allfamilies || afp->af_af == AF_LINK) {
1021 const struct afswtch *lafp;
1024 * Hack; the link level address is received separately
1025 * from the routing information so any address is not
1026 * handled above. Cobble together an entry and invoke
1027 * the status method specially.
1029 lafp = af_getbyname("lladdr");
1031 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1032 lafp->af_status(s, &info);
1038 else if (afp->af_other_status != NULL)
1039 afp->af_other_status(s);
1041 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1042 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1043 printf("%s", ifs.ascii);
1046 sfp_status(s, &ifr, verbose);
1053 tunnel_status(int s)
1055 af_all_tunnel_status(s);
1059 Perror(const char *cmd)
1064 errx(1, "%s: no such interface", cmd);
1068 errx(1, "%s: permission denied", cmd);
1077 * Print a value a la the %b format of the kernel's printf
1080 printb(const char *s, unsigned v, const char *bits)
1085 if (bits && *bits == 8)
1086 printf("%s=%o", s, v);
1088 printf("%s=%x", s, v);
1092 while ((i = *bits++) != '\0') {
1093 if (v & (1 << (i-1))) {
1097 for (; (c = *bits) > 32; bits++)
1100 for (; *bits > 32; bits++)
1108 print_vhid(const struct ifaddrs *ifa, const char *s)
1110 struct if_data *ifd;
1112 if (ifa->ifa_data == NULL)
1115 ifd = ifa->ifa_data;
1116 if (ifd->ifi_vhid == 0)
1119 printf("vhid %d ", ifd->ifi_vhid);
1123 ifmaybeload(const char *name)
1125 #define MOD_PREFIX_LEN 3 /* "if_" */
1126 struct module_stat mstat;
1128 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1131 /* loading suppressed by the user */
1135 /* trim the interface number off the end */
1136 strlcpy(ifname, name, sizeof(ifname));
1137 for (dp = ifname; *dp != 0; dp++)
1143 /* turn interface and unit into module name */
1144 strlcpy(ifkind, "if_", sizeof(ifkind));
1145 strlcat(ifkind, ifname, sizeof(ifkind));
1147 /* scan files in kernel */
1148 mstat.version = sizeof(struct module_stat);
1149 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1150 /* scan modules in file */
1151 for (modid = kldfirstmod(fileid); modid > 0;
1152 modid = modfnext(modid)) {
1153 if (modstat(modid, &mstat) < 0)
1155 /* strip bus name if present */
1156 if ((cp = strchr(mstat.name, '/')) != NULL) {
1161 /* already loaded? */
1162 if (strcmp(ifname, cp) == 0 ||
1163 strcmp(ifkind, cp) == 0)
1168 /* not present, we should try to load it */
1172 static struct cmd basic_cmds[] = {
1173 DEF_CMD("up", IFF_UP, setifflags),
1174 DEF_CMD("down", -IFF_UP, setifflags),
1175 DEF_CMD("arp", -IFF_NOARP, setifflags),
1176 DEF_CMD("-arp", IFF_NOARP, setifflags),
1177 DEF_CMD("debug", IFF_DEBUG, setifflags),
1178 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1179 DEF_CMD_ARG("description", setifdescr),
1180 DEF_CMD_ARG("descr", setifdescr),
1181 DEF_CMD("-description", 0, unsetifdescr),
1182 DEF_CMD("-descr", 0, unsetifdescr),
1183 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1184 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1185 DEF_CMD("add", IFF_UP, notealias),
1186 DEF_CMD("alias", IFF_UP, notealias),
1187 DEF_CMD("-alias", -IFF_UP, notealias),
1188 DEF_CMD("delete", -IFF_UP, notealias),
1189 DEF_CMD("remove", -IFF_UP, notealias),
1191 #define EN_SWABIPS 0x1000
1192 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1193 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1195 DEF_CMD_ARG("netmask", setifnetmask),
1196 DEF_CMD_ARG("metric", setifmetric),
1197 DEF_CMD_ARG("broadcast", setifbroadaddr),
1198 DEF_CMD_ARG("ipdst", setifipdst),
1199 DEF_CMD_ARG2("tunnel", settunnel),
1200 DEF_CMD("-tunnel", 0, deletetunnel),
1201 DEF_CMD("deletetunnel", 0, deletetunnel),
1203 DEF_CMD_ARG("vnet", setifvnet),
1204 DEF_CMD_ARG("-vnet", setifrvnet),
1206 DEF_CMD("link0", IFF_LINK0, setifflags),
1207 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1208 DEF_CMD("link1", IFF_LINK1, setifflags),
1209 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1210 DEF_CMD("link2", IFF_LINK2, setifflags),
1211 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1212 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1213 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1214 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1215 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1216 DEF_CMD("rxcsum6", IFCAP_RXCSUM_IPV6, setifcap),
1217 DEF_CMD("-rxcsum6", -IFCAP_RXCSUM_IPV6, setifcap),
1218 DEF_CMD("txcsum6", IFCAP_TXCSUM_IPV6, setifcap),
1219 DEF_CMD("-txcsum6", -IFCAP_TXCSUM_IPV6, setifcap),
1220 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1221 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1222 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1223 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1224 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1225 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1226 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1227 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1228 DEF_CMD("tso6", IFCAP_TSO6, setifcap),
1229 DEF_CMD("-tso6", -IFCAP_TSO6, setifcap),
1230 DEF_CMD("tso4", IFCAP_TSO4, setifcap),
1231 DEF_CMD("-tso4", -IFCAP_TSO4, setifcap),
1232 DEF_CMD("tso", IFCAP_TSO, setifcap),
1233 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1234 DEF_CMD("toe", IFCAP_TOE, setifcap),
1235 DEF_CMD("-toe", -IFCAP_TOE, setifcap),
1236 DEF_CMD("lro", IFCAP_LRO, setifcap),
1237 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1238 DEF_CMD("wol", IFCAP_WOL, setifcap),
1239 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1240 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1241 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1242 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1243 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1244 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1245 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1246 DEF_CMD("normal", -IFF_LINK0, setifflags),
1247 DEF_CMD("compress", IFF_LINK0, setifflags),
1248 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1249 DEF_CMD_ARG("mtu", setifmtu),
1250 DEF_CMD_ARG("name", setifname),
1253 static __constructor void
1258 for (i = 0; i < nitems(basic_cmds); i++)
1259 cmd_register(&basic_cmds[i]);