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 _Noreturn 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_parameter == NEXTARG && p->c_u.c_func) {
582 errx(1, "'%s' requires argument",
584 p->c_u.c_func(argv[1], 0, s, afp);
586 } else if (p->c_parameter == OPTARG && p->c_u.c_func) {
587 p->c_u.c_func(argv[1], 0, s, afp);
590 } else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) {
592 errx(1, "'%s' requires 2 arguments",
594 p->c_u.c_func2(argv[1], argv[2], s, afp);
595 argc -= 2, argv += 2;
596 } else if (p->c_u.c_func)
597 p->c_u.c_func(*argv, p->c_parameter, s, afp);
602 * Do any post argument processing required by the address family.
604 if (afp->af_postproc != NULL)
605 afp->af_postproc(s, afp);
607 * Do deferred callbacks registered while processing
608 * command-line arguments.
610 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
611 cb->cb_func(s, cb->cb_arg);
613 * Do deferred operations.
616 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
617 warnx("interface %s cannot change %s addresses!",
624 strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
625 sizeof ifr.ifr_name);
626 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
628 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
629 /* means no previous address for interface */
631 Perror("ioctl (SIOCDIFADDR)");
635 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
636 warnx("interface %s cannot change %s addresses!",
641 if (newaddr && (setaddr || setmask)) {
642 strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
643 sizeof ifr.ifr_name);
644 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
645 Perror("ioctl (SIOCAIFADDR)");
654 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
656 if (afp->af_getaddr == NULL)
659 * Delay the ioctl to set the interface addr until flags are all set.
660 * The address interpretation may depend on the flags,
661 * and the flags may change when the address is set.
664 if (doalias == 0 && afp->af_af != AF_LINK)
666 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
670 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
672 struct addrinfo *srcres, *dstres;
675 if (afp->af_settunnel == NULL) {
676 warn("address family %s does not support tunnel setup",
681 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
682 errx(1, "error in parsing address string: %s",
683 gai_strerror(ecode));
685 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
686 errx(1, "error in parsing address string: %s",
687 gai_strerror(ecode));
689 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
691 "source and destination address families do not match");
693 afp->af_settunnel(s, srcres, dstres);
695 freeaddrinfo(srcres);
696 freeaddrinfo(dstres);
701 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
704 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
705 err(1, "SIOCDIFPHYADDR");
710 setifvnet(const char *jname, int dummy __unused, int s,
711 const struct afswtch *afp)
715 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
716 my_ifr.ifr_jid = jail_getid(jname);
717 if (my_ifr.ifr_jid < 0)
718 errx(1, "%s", jail_errmsg);
719 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
720 err(1, "SIOCSIFVNET");
724 setifrvnet(const char *jname, int dummy __unused, int s,
725 const struct afswtch *afp)
729 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
730 my_ifr.ifr_jid = jail_getid(jname);
731 if (my_ifr.ifr_jid < 0)
732 errx(1, "%s", jail_errmsg);
733 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
734 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
739 setifnetmask(const char *addr, int dummy __unused, int s,
740 const struct afswtch *afp)
742 if (afp->af_getaddr != NULL) {
744 afp->af_getaddr(addr, MASK);
749 setifbroadaddr(const char *addr, int dummy __unused, int s,
750 const struct afswtch *afp)
752 if (afp->af_getaddr != NULL)
753 afp->af_getaddr(addr, DSTADDR);
757 setifipdst(const char *addr, int dummy __unused, int s,
758 const struct afswtch *afp)
760 const struct afswtch *inet;
762 inet = af_getbyname("inet");
765 inet->af_getaddr(addr, DSTADDR);
771 notealias(const char *addr, int param, int s, const struct afswtch *afp)
773 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
774 if (setaddr && doalias == 0 && param < 0)
775 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
776 bcopy((caddr_t)rqtosa(af_addreq),
777 (caddr_t)rqtosa(af_ridreq),
778 rqtosa(af_addreq)->sa_len);
790 setifdstaddr(const char *addr, int param __unused, int s,
791 const struct afswtch *afp)
793 if (afp->af_getaddr != NULL)
794 afp->af_getaddr(addr, DSTADDR);
798 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
799 * of the ifreq structure, which may confuse other parts of ifconfig.
800 * Make a private copy so we can avoid that.
803 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
808 memset(&my_ifr, 0, sizeof(my_ifr));
809 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
811 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
812 Perror("ioctl (SIOCGIFFLAGS)");
815 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
822 my_ifr.ifr_flags = flags & 0xffff;
823 my_ifr.ifr_flagshigh = flags >> 16;
824 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
829 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
833 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
834 Perror("ioctl (SIOCGIFCAP)");
837 flags = ifr.ifr_curcap;
843 flags &= ifr.ifr_reqcap;
844 ifr.ifr_reqcap = flags;
845 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
850 setifmetric(const char *val, int dummy __unused, int s,
851 const struct afswtch *afp)
853 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
854 ifr.ifr_metric = atoi(val);
855 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
856 err(1, "ioctl SIOCSIFMETRIC (set metric)");
860 setifmtu(const char *val, int dummy __unused, int s,
861 const struct afswtch *afp)
863 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
864 ifr.ifr_mtu = atoi(val);
865 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
866 err(1, "ioctl SIOCSIFMTU (set mtu)");
870 setifname(const char *val, int dummy __unused, int s,
871 const struct afswtch *afp)
875 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
877 newname = strdup(val);
879 err(1, "no memory to set ifname");
880 ifr.ifr_data = newname;
881 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
883 err(1, "ioctl SIOCSIFNAME (set name)");
886 strlcpy(name, newname, sizeof(name));
892 setifdescr(const char *val, int dummy __unused, int s,
893 const struct afswtch *afp)
897 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
899 ifr.ifr_buffer.length = strlen(val) + 1;
900 if (ifr.ifr_buffer.length == 1) {
901 ifr.ifr_buffer.buffer = newdescr = NULL;
902 ifr.ifr_buffer.length = 0;
904 newdescr = strdup(val);
905 ifr.ifr_buffer.buffer = newdescr;
906 if (newdescr == NULL) {
907 warn("no memory to set ifdescr");
912 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
913 err(1, "ioctl SIOCSIFDESCR (set descr)");
920 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
923 setifdescr("", 0, s, 0);
927 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
928 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
929 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
932 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
933 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
934 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
935 "\26RXCSUM_IPV6\27TXCSUM_IPV6"
938 * Print the status of the interface. If an address family was
939 * specified, show only it; otherwise, show them all.
942 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
951 ifr.ifr_addr.sa_family = AF_LOCAL;
954 ifr.ifr_addr.sa_family =
955 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
957 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
959 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
961 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
963 printf("%s: ", name);
964 printb("flags", ifa->ifa_flags, IFFBITS);
965 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
966 printf(" metric %d", ifr.ifr_metric);
967 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
968 printf(" mtu %d", ifr.ifr_mtu);
972 if ((descr = reallocf(descr, descrlen)) != NULL) {
973 ifr.ifr_buffer.buffer = descr;
974 ifr.ifr_buffer.length = descrlen;
975 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
976 if (ifr.ifr_buffer.buffer == descr) {
977 if (strlen(descr) > 0)
978 printf("\tdescription: %s\n",
980 } else if (ifr.ifr_buffer.length > descrlen) {
981 descrlen = ifr.ifr_buffer.length;
986 warn("unable to allocate memory for interface"
991 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
992 if (ifr.ifr_curcap != 0) {
993 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
996 if (supmedia && ifr.ifr_reqcap != 0) {
997 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1004 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1005 if (ift->ifa_addr == NULL)
1007 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1010 const struct afswtch *p;
1011 p = af_getbyfamily(ift->ifa_addr->sa_family);
1012 if (p != NULL && p->af_status != NULL)
1013 p->af_status(s, ift);
1014 } else if (afp->af_af == ift->ifa_addr->sa_family)
1015 afp->af_status(s, ift);
1018 if (allfamilies || afp->af_af == AF_LINK) {
1019 const struct afswtch *lafp;
1022 * Hack; the link level address is received separately
1023 * from the routing information so any address is not
1024 * handled above. Cobble together an entry and invoke
1025 * the status method specially.
1027 lafp = af_getbyname("lladdr");
1029 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1030 lafp->af_status(s, &info);
1036 else if (afp->af_other_status != NULL)
1037 afp->af_other_status(s);
1039 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1040 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1041 printf("%s", ifs.ascii);
1044 sfp_status(s, &ifr, verbose);
1051 tunnel_status(int s)
1053 af_all_tunnel_status(s);
1057 Perror(const char *cmd)
1062 errx(1, "%s: no such interface", cmd);
1066 errx(1, "%s: permission denied", cmd);
1075 * Print a value a la the %b format of the kernel's printf
1078 printb(const char *s, unsigned v, const char *bits)
1083 if (bits && *bits == 8)
1084 printf("%s=%o", s, v);
1086 printf("%s=%x", s, v);
1090 while ((i = *bits++) != '\0') {
1091 if (v & (1 << (i-1))) {
1095 for (; (c = *bits) > 32; bits++)
1098 for (; *bits > 32; bits++)
1106 print_vhid(const struct ifaddrs *ifa, const char *s)
1108 struct if_data *ifd;
1110 if (ifa->ifa_data == NULL)
1113 ifd = ifa->ifa_data;
1114 if (ifd->ifi_vhid == 0)
1117 printf("vhid %d ", ifd->ifi_vhid);
1121 ifmaybeload(const char *name)
1123 #define MOD_PREFIX_LEN 3 /* "if_" */
1124 struct module_stat mstat;
1126 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1129 /* loading suppressed by the user */
1133 /* trim the interface number off the end */
1134 strlcpy(ifname, name, sizeof(ifname));
1135 for (dp = ifname; *dp != 0; dp++)
1141 /* turn interface and unit into module name */
1142 strlcpy(ifkind, "if_", sizeof(ifkind));
1143 strlcat(ifkind, ifname, sizeof(ifkind));
1145 /* scan files in kernel */
1146 mstat.version = sizeof(struct module_stat);
1147 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1148 /* scan modules in file */
1149 for (modid = kldfirstmod(fileid); modid > 0;
1150 modid = modfnext(modid)) {
1151 if (modstat(modid, &mstat) < 0)
1153 /* strip bus name if present */
1154 if ((cp = strchr(mstat.name, '/')) != NULL) {
1159 /* already loaded? */
1160 if (strcmp(ifname, cp) == 0 ||
1161 strcmp(ifkind, cp) == 0)
1167 * Try to load the module. But ignore failures, because ifconfig can't
1168 * infer the names of all drivers (eg mlx4en(4)).
1170 (void) kldload(ifkind);
1173 static struct cmd basic_cmds[] = {
1174 DEF_CMD("up", IFF_UP, setifflags),
1175 DEF_CMD("down", -IFF_UP, setifflags),
1176 DEF_CMD("arp", -IFF_NOARP, setifflags),
1177 DEF_CMD("-arp", IFF_NOARP, setifflags),
1178 DEF_CMD("debug", IFF_DEBUG, setifflags),
1179 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1180 DEF_CMD_ARG("description", setifdescr),
1181 DEF_CMD_ARG("descr", setifdescr),
1182 DEF_CMD("-description", 0, unsetifdescr),
1183 DEF_CMD("-descr", 0, unsetifdescr),
1184 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1185 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1186 DEF_CMD("add", IFF_UP, notealias),
1187 DEF_CMD("alias", IFF_UP, notealias),
1188 DEF_CMD("-alias", -IFF_UP, notealias),
1189 DEF_CMD("delete", -IFF_UP, notealias),
1190 DEF_CMD("remove", -IFF_UP, notealias),
1192 #define EN_SWABIPS 0x1000
1193 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1194 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1196 DEF_CMD_ARG("netmask", setifnetmask),
1197 DEF_CMD_ARG("metric", setifmetric),
1198 DEF_CMD_ARG("broadcast", setifbroadaddr),
1199 DEF_CMD_ARG("ipdst", setifipdst),
1200 DEF_CMD_ARG2("tunnel", settunnel),
1201 DEF_CMD("-tunnel", 0, deletetunnel),
1202 DEF_CMD("deletetunnel", 0, deletetunnel),
1204 DEF_CMD_ARG("vnet", setifvnet),
1205 DEF_CMD_ARG("-vnet", setifrvnet),
1207 DEF_CMD("link0", IFF_LINK0, setifflags),
1208 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1209 DEF_CMD("link1", IFF_LINK1, setifflags),
1210 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1211 DEF_CMD("link2", IFF_LINK2, setifflags),
1212 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1213 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1214 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1215 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1216 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1217 DEF_CMD("rxcsum6", IFCAP_RXCSUM_IPV6, setifcap),
1218 DEF_CMD("-rxcsum6", -IFCAP_RXCSUM_IPV6, setifcap),
1219 DEF_CMD("txcsum6", IFCAP_TXCSUM_IPV6, setifcap),
1220 DEF_CMD("-txcsum6", -IFCAP_TXCSUM_IPV6, setifcap),
1221 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1222 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1223 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1224 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1225 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1226 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1227 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1228 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1229 DEF_CMD("tso6", IFCAP_TSO6, setifcap),
1230 DEF_CMD("-tso6", -IFCAP_TSO6, setifcap),
1231 DEF_CMD("tso4", IFCAP_TSO4, setifcap),
1232 DEF_CMD("-tso4", -IFCAP_TSO4, setifcap),
1233 DEF_CMD("tso", IFCAP_TSO, setifcap),
1234 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1235 DEF_CMD("toe", IFCAP_TOE, setifcap),
1236 DEF_CMD("-toe", -IFCAP_TOE, setifcap),
1237 DEF_CMD("lro", IFCAP_LRO, setifcap),
1238 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1239 DEF_CMD("wol", IFCAP_WOL, setifcap),
1240 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1241 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1242 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1243 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1244 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1245 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1246 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1247 DEF_CMD("normal", -IFF_LINK0, setifflags),
1248 DEF_CMD("compress", IFF_LINK0, setifflags),
1249 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1250 DEF_CMD_ARG("mtu", setifmtu),
1251 DEF_CMD_ARG("name", setifname),
1254 static __constructor void
1259 for (i = 0; i < nitems(basic_cmds); i++)
1260 cmd_register(&basic_cmds[i]);