2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1983, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
42 static const char rcsid[] =
46 #include <sys/param.h>
47 #include <sys/ioctl.h>
48 #include <sys/module.h>
49 #include <sys/linker.h>
50 #include <sys/queue.h>
51 #include <sys/socket.h>
54 #include <net/ethernet.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <arpa/inet.h>
83 * Since "struct ifreq" is composed of various union members, callers
84 * should pay special attention to interpret the value.
85 * (.e.g. little/big endian difference in the structure.)
102 int printkeys = 0; /* Print keying material for interfaces. */
105 /* Formatter Strings */
106 char *f_inet, *f_inet6, *f_ether, *f_addr;
108 static int ifconfig(int argc, char *const *argv, int iscreate,
109 const struct afswtch *afp);
110 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
111 struct ifaddrs *ifa);
112 static void tunnel_status(int s);
113 static _Noreturn void usage(void);
115 static int getifflags(const char *ifname, int us);
117 static struct afswtch *af_getbyname(const char *name);
118 static struct afswtch *af_getbyfamily(int af);
119 static void af_other_status(int);
121 void printifnamemaybe(void);
123 static struct option *opts = NULL;
125 struct ifa_order_elt {
129 TAILQ_ENTRY(ifa_order_elt) link;
132 TAILQ_HEAD(ifa_queue, ifa_order_elt);
134 static struct module_map_entry {
140 .kldname = "if_tuntap",
144 .kldname = "if_tuntap",
148 .kldname = "if_tuntap",
156 * This mapping exists because there is a conflicting enc module
157 * in CAM. ifconfig's guessing behavior will attempt to match
158 * the ifname to a module as well as if_${ifname} and clash with
159 * CAM enc. This is an assertion of the correct module to load.
168 opt_register(struct option *p)
180 /* XXX not right but close enough for now */
182 for (p = opts; p != NULL; p = p->next) {
183 strlcat(options, p->opt_usage, sizeof(options));
184 strlcat(options, " ", sizeof(options));
188 "usage: ifconfig [-f type:format] %sinterface address_family\n"
189 " [address [dest_address]] [parameters]\n"
190 " ifconfig interface create\n"
191 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
192 " ifconfig -l [-d] [-u] [address_family]\n"
193 " ifconfig %s[-d] [-m] [-u] [-v]\n",
194 options, options, options);
198 #define ORDERS_SIZE(x) sizeof(x) / sizeof(x[0])
201 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
203 struct ifaddrs *prev;
204 struct ifa_order_elt *cur;
205 unsigned int ord, af, ifa_ord;
212 while (ifa != NULL) {
214 strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
215 cur = calloc(1, sizeof(*cur));
220 TAILQ_INSERT_TAIL(q, cur, link);
221 cur->if_order = ifa_ord ++;
227 af = ifa->ifa_addr->sa_family;
229 if (af < ORDERS_SIZE(cur->af_orders) &&
230 cur->af_orders[af] == 0)
231 cur->af_orders[af] = ++ord;
241 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
243 struct ifa_order_elt *cur, *e1, *e2;
244 unsigned int af1, af2;
249 ret = strcmp(a->ifa_name, b->ifa_name);
251 TAILQ_FOREACH(cur, q, link) {
255 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
257 else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
264 return (e1->if_order - e2->if_order);
266 } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
267 TAILQ_FOREACH(cur, q, link) {
268 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
277 af1 = a->ifa_addr->sa_family;
278 af2 = b->ifa_addr->sa_family;
280 if (af1 < ORDERS_SIZE(e1->af_orders) &&
281 af2 < ORDERS_SIZE(e1->af_orders))
282 return (e1->af_orders[af1] - e1->af_orders[af2]);
288 static void freeformat(void)
301 static void setformat(char *input)
303 char *formatstr, *category, *modifier;
305 formatstr = strdup(input);
306 while ((category = strsep(&formatstr, ",")) != NULL) {
307 modifier = strchr(category, ':');
308 if (modifier == NULL || modifier[1] == '\0') {
309 warnx("Skipping invalid format specification: %s\n",
314 /* Split the string on the separator, then seek past it */
318 if (strcmp(category, "addr") == 0)
319 f_addr = strdup(modifier);
320 else if (strcmp(category, "ether") == 0)
321 f_ether = strdup(modifier);
322 else if (strcmp(category, "inet") == 0)
323 f_inet = strdup(modifier);
324 else if (strcmp(category, "inet6") == 0)
325 f_inet6 = strdup(modifier);
332 static struct ifaddrs *
333 sortifaddrs(struct ifaddrs *list,
334 int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *),
337 struct ifaddrs *right, *temp, *last, *result, *next, *tail;
346 if (!list || !list->ifa_next)
349 while (temp && temp->ifa_next) {
351 right = right->ifa_next;
352 temp = temp->ifa_next->ifa_next;
355 last->ifa_next = NULL;
357 list = sortifaddrs(list, compare, q);
358 right = sortifaddrs(right, compare, q);
360 while (list || right) {
364 list = list->ifa_next;
367 right = right->ifa_next;
368 } else if (compare(list, right, q) <= 0) {
370 list = list->ifa_next;
373 right = right->ifa_next;
379 tail->ifa_next = next;
387 void printifnamemaybe()
390 printf("%s\n", name);
394 main(int argc, char *argv[])
396 int c, all, namesonly, downonly, uponly;
397 const struct afswtch *afp = NULL;
399 struct ifaddrs *ifap, *sifap, *ifa;
401 const struct sockaddr_dl *sdl;
402 char options[1024], *cp, *envformat, *namecp = NULL;
403 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
404 struct ifa_order_elt *cur, *tmp;
410 all = downonly = uponly = namesonly = noload = verbose = 0;
411 f_inet = f_inet6 = f_ether = f_addr = NULL;
413 envformat = getenv("IFCONFIG_FORMAT");
414 if (envformat != NULL)
415 setformat(envformat);
418 * Ensure we print interface name when expected to,
419 * even if we terminate early due to error.
421 atexit(printifnamemaybe);
423 /* Parse leading line options */
424 strlcpy(options, "f:adklmnuv", sizeof(options));
425 for (p = opts; p != NULL; p = p->next)
426 strlcat(options, p->opt, sizeof(options));
427 while ((c = getopt(argc, argv, options)) != -1) {
429 case 'a': /* scan all interfaces */
432 case 'd': /* restrict scan to "down" interfaces */
443 case 'l': /* scan interface names only */
446 case 'm': /* show media choices in status */
449 case 'n': /* suppress module loading */
452 case 'u': /* restrict scan to "up" interfaces */
459 for (p = opts; p != NULL; p = p->next)
460 if (p->opt[0] == c) {
472 /* -l cannot be used with -a or -m */
473 if (namesonly && (all || supmedia))
477 if (uponly && downonly)
480 /* no arguments is equivalent to '-a' */
481 if (!namesonly && argc < 1)
484 /* -a and -l allow an address family arg to limit the output */
485 if (all || namesonly) {
492 afp = af_getbyname(*argv);
494 warnx("Address family '%s' unknown.", *argv);
497 if (afp->af_name != NULL)
499 /* leave with afp non-zero */
502 /* not listing, need an argument */
509 /* check and maybe load support for this interface */
512 ifindex = if_nametoindex(ifname);
515 * NOTE: We must special-case the `create' command
516 * right here as we would otherwise fail when trying
517 * to find the interface.
519 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
520 strcmp(argv[0], "plumb") == 0)) {
521 iflen = strlcpy(name, ifname, sizeof(name));
522 if (iflen >= sizeof(name))
523 errx(1, "%s: cloning name too long",
525 ifconfig(argc, argv, 1, NULL);
530 * NOTE: We have to special-case the `-vnet' command
531 * right here as we would otherwise fail when trying
532 * to find the interface as it lives in another vnet.
534 if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
535 iflen = strlcpy(name, ifname, sizeof(name));
536 if (iflen >= sizeof(name))
537 errx(1, "%s: interface name too long",
539 ifconfig(argc, argv, 0, NULL);
543 errx(1, "interface %s does not exist", ifname);
546 * Do not allow use `create` command as hostname if
547 * address family is not specified.
549 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
550 strcmp(argv[0], "plumb") == 0)) {
552 errx(1, "interface %s already exists",
559 /* Check for address family */
561 afp = af_getbyname(*argv);
567 * Check for a requested configuration action on a single interface,
568 * which doesn't require building, sorting, and searching the entire
569 * system address list
571 if ((argc > 0) && (ifname != NULL)) {
572 iflen = strlcpy(name, ifname, sizeof(name));
573 if (iflen >= sizeof(name)) {
574 warnx("%s: interface name too long, skipping", ifname);
576 flags = getifflags(name, -1);
577 if (!(((flags & IFF_CANTCONFIG) != 0) ||
578 (downonly && (flags & IFF_UP) != 0) ||
579 (uponly && (flags & IFF_UP) == 0)))
580 ifconfig(argc, argv, 0, afp);
585 if (getifaddrs(&ifap) != 0)
586 err(EXIT_FAILURE, "getifaddrs");
590 if (calcorders(ifap, &q) != 0)
591 err(EXIT_FAILURE, "calcorders");
593 sifap = sortifaddrs(ifap, cmpifaddrs, &q);
595 TAILQ_FOREACH_SAFE(cur, &q, link, tmp)
599 for (ifa = sifap; ifa; ifa = ifa->ifa_next) {
600 memset(&paifr, 0, sizeof(paifr));
601 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
602 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
603 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
604 ifa->ifa_addr->sa_len);
607 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
609 if (ifa->ifa_addr->sa_family == AF_LINK)
610 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
613 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
615 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
616 if (iflen >= sizeof(name)) {
617 warnx("%s: interface name too long, skipping",
623 if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
625 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
627 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
630 * Are we just listing the interfaces?
636 /* special case for "ether" address family */
637 if (!strcmp(afp->af_name, "ether")) {
639 (sdl->sdl_type != IFT_ETHER &&
640 sdl->sdl_type != IFT_L2VLAN &&
641 sdl->sdl_type != IFT_BRIDGE) ||
642 sdl->sdl_alen != ETHER_ADDR_LEN)
645 if (ifa->ifa_addr->sa_family
660 ifconfig(argc, argv, 0, afp);
662 status(afp, sdl, ifa);
673 static struct afswtch *afs = NULL;
676 af_register(struct afswtch *p)
682 static struct afswtch *
683 af_getbyname(const char *name)
687 for (afp = afs; afp != NULL; afp = afp->af_next)
688 if (strcmp(afp->af_name, name) == 0)
693 static struct afswtch *
694 af_getbyfamily(int af)
698 for (afp = afs; afp != NULL; afp = afp->af_next)
699 if (afp->af_af == af)
705 af_other_status(int s)
708 uint8_t afmask[howmany(AF_MAX, NBBY)];
710 memset(afmask, 0, sizeof(afmask));
711 for (afp = afs; afp != NULL; afp = afp->af_next) {
712 if (afp->af_other_status == NULL)
714 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
716 afp->af_other_status(s);
717 setbit(afmask, afp->af_af);
722 af_all_tunnel_status(int s)
725 uint8_t afmask[howmany(AF_MAX, NBBY)];
727 memset(afmask, 0, sizeof(afmask));
728 for (afp = afs; afp != NULL; afp = afp->af_next) {
729 if (afp->af_status_tunnel == NULL)
731 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
733 afp->af_status_tunnel(s);
734 setbit(afmask, afp->af_af);
738 static struct cmd *cmds = NULL;
741 cmd_register(struct cmd *p)
747 static const struct cmd *
748 cmd_lookup(const char *name, int iscreate)
752 for (p = cmds; p != NULL; p = p->c_next)
753 if (strcmp(name, p->c_name) == 0) {
766 callback_func *cb_func;
768 struct callback *cb_next;
770 static struct callback *callbacks = NULL;
773 callback_register(callback_func *func, void *arg)
777 cb = malloc(sizeof(struct callback));
779 errx(1, "unable to allocate memory for callback");
782 cb->cb_next = callbacks;
786 /* specially-handled commands */
787 static void setifaddr(const char *, int, int, const struct afswtch *);
788 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
790 static void setifdstaddr(const char *, int, int, const struct afswtch *);
791 static const struct cmd setifdstaddr_cmd =
792 DEF_CMD("ifdstaddr", 0, setifdstaddr);
795 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
797 const struct afswtch *afp, *nafp;
802 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
807 * This is the historical "accident" allowing users to configure IPv4
808 * addresses without the "inet" keyword which while a nice feature has
809 * proven to complicate other things. We cannot remove this but only
810 * make sure we will never have a similar implicit default for IPv6 or
811 * any other address familiy. We need a fallback though for
812 * ifconfig IF up/down etc. to work without INET support as people
813 * never used ifconfig IF link up/down, etc. either.
817 if (afp == NULL && feature_present("inet"))
818 afp = af_getbyname("inet");
822 afp = af_getbyname("link");
824 warnx("Please specify an address_family.");
828 ifr.ifr_addr.sa_family =
829 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
830 AF_LOCAL : afp->af_af;
832 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
833 (uafp != NULL || errno != EAFNOSUPPORT ||
834 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
835 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
838 p = cmd_lookup(*argv, iscreate);
839 if (iscreate && p == NULL) {
841 * Push the clone create callback so the new
842 * device is created and can be used for any
843 * remaining arguments.
847 errx(1, "internal error, no callback");
848 callbacks = cb->cb_next;
849 cb->cb_func(s, cb->cb_arg);
852 * Handle any address family spec that
853 * immediately follows and potentially
854 * recreate the socket.
856 nafp = af_getbyname(*argv);
866 * Look for a normal parameter.
872 * Not a recognized command, choose between setting
873 * the interface address and the dst address.
875 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
877 if (p->c_parameter == NEXTARG && p->c_u.c_func) {
879 errx(1, "'%s' requires argument",
881 p->c_u.c_func(argv[1], 0, s, afp);
883 } else if (p->c_parameter == OPTARG && p->c_u.c_func) {
884 p->c_u.c_func(argv[1], 0, s, afp);
887 } else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) {
889 errx(1, "'%s' requires 2 arguments",
891 p->c_u.c_func2(argv[1], argv[2], s, afp);
892 argc -= 2, argv += 2;
893 } else if (p->c_u.c_func)
894 p->c_u.c_func(*argv, p->c_parameter, s, afp);
899 * Do any post argument processing required by the address family.
901 if (afp->af_postproc != NULL)
902 afp->af_postproc(s, afp);
904 * Do deferred callbacks registered while processing
905 * command-line arguments.
907 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
908 cb->cb_func(s, cb->cb_arg);
910 * Do deferred operations.
913 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
914 warnx("interface %s cannot change %s addresses!",
921 strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
922 sizeof ifr.ifr_name);
923 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
925 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
926 /* means no previous address for interface */
928 Perror("ioctl (SIOCDIFADDR)");
932 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
933 warnx("interface %s cannot change %s addresses!",
938 if (newaddr && (setaddr || setmask)) {
939 strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
940 sizeof ifr.ifr_name);
941 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
942 Perror("ioctl (SIOCAIFADDR)");
951 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
953 if (afp->af_getaddr == NULL)
956 * Delay the ioctl to set the interface addr until flags are all set.
957 * The address interpretation may depend on the flags,
958 * and the flags may change when the address is set.
961 if (doalias == 0 && afp->af_af != AF_LINK)
963 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
967 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
969 struct addrinfo *srcres, *dstres;
972 if (afp->af_settunnel == NULL) {
973 warn("address family %s does not support tunnel setup",
978 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
979 errx(1, "error in parsing address string: %s",
980 gai_strerror(ecode));
982 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
983 errx(1, "error in parsing address string: %s",
984 gai_strerror(ecode));
986 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
988 "source and destination address families do not match");
990 afp->af_settunnel(s, srcres, dstres);
992 freeaddrinfo(srcres);
993 freeaddrinfo(dstres);
998 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
1001 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
1002 err(1, "SIOCDIFPHYADDR");
1007 setifvnet(const char *jname, int dummy __unused, int s,
1008 const struct afswtch *afp)
1010 struct ifreq my_ifr;
1012 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1013 my_ifr.ifr_jid = jail_getid(jname);
1014 if (my_ifr.ifr_jid < 0)
1015 errx(1, "%s", jail_errmsg);
1016 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
1017 err(1, "SIOCSIFVNET");
1021 setifrvnet(const char *jname, int dummy __unused, int s,
1022 const struct afswtch *afp)
1024 struct ifreq my_ifr;
1026 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1027 my_ifr.ifr_jid = jail_getid(jname);
1028 if (my_ifr.ifr_jid < 0)
1029 errx(1, "%s", jail_errmsg);
1030 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
1031 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
1036 setifnetmask(const char *addr, int dummy __unused, int s,
1037 const struct afswtch *afp)
1039 if (afp->af_getaddr != NULL) {
1041 afp->af_getaddr(addr, MASK);
1046 setifbroadaddr(const char *addr, int dummy __unused, int s,
1047 const struct afswtch *afp)
1049 if (afp->af_getaddr != NULL)
1050 afp->af_getaddr(addr, DSTADDR);
1054 notealias(const char *addr, int param, int s, const struct afswtch *afp)
1056 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1057 if (setaddr && doalias == 0 && param < 0)
1058 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1059 bcopy((caddr_t)rqtosa(af_addreq),
1060 (caddr_t)rqtosa(af_ridreq),
1061 rqtosa(af_addreq)->sa_len);
1073 setifdstaddr(const char *addr, int param __unused, int s,
1074 const struct afswtch *afp)
1076 if (afp->af_getaddr != NULL)
1077 afp->af_getaddr(addr, DSTADDR);
1081 getifflags(const char *ifname, int us)
1083 struct ifreq my_ifr;
1086 memset(&my_ifr, 0, sizeof(my_ifr));
1087 (void) strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name));
1089 if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
1090 err(1, "socket(family AF_LOCAL,SOCK_DGRAM");
1093 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1094 Perror("ioctl (SIOCGIFFLAGS)");
1099 return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16));
1103 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1104 * of the ifreq structure, which may confuse other parts of ifconfig.
1105 * Make a private copy so we can avoid that.
1108 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1110 struct ifreq my_ifr;
1113 flags = getifflags(name, s);
1119 memset(&my_ifr, 0, sizeof(my_ifr));
1120 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1121 my_ifr.ifr_flags = flags & 0xffff;
1122 my_ifr.ifr_flagshigh = flags >> 16;
1123 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1128 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1132 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1133 Perror("ioctl (SIOCGIFCAP)");
1136 flags = ifr.ifr_curcap;
1142 flags &= ifr.ifr_reqcap;
1143 ifr.ifr_reqcap = flags;
1144 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1149 setifmetric(const char *val, int dummy __unused, int s,
1150 const struct afswtch *afp)
1152 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1153 ifr.ifr_metric = atoi(val);
1154 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1155 err(1, "ioctl SIOCSIFMETRIC (set metric)");
1159 setifmtu(const char *val, int dummy __unused, int s,
1160 const struct afswtch *afp)
1162 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1163 ifr.ifr_mtu = atoi(val);
1164 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1165 err(1, "ioctl SIOCSIFMTU (set mtu)");
1169 setifpcp(const char *val, int arg __unused, int s, const struct afswtch *afp)
1174 ul = strtoul(val, &endp, 0);
1176 errx(1, "invalid value for pcp");
1178 errx(1, "value for pcp out of range");
1179 ifr.ifr_lan_pcp = ul;
1180 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1181 err(1, "SIOCSLANPCP");
1185 disableifpcp(const char *val, int arg __unused, int s,
1186 const struct afswtch *afp)
1189 ifr.ifr_lan_pcp = IFNET_PCP_NONE;
1190 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1191 err(1, "SIOCSLANPCP");
1195 setifname(const char *val, int dummy __unused, int s,
1196 const struct afswtch *afp)
1200 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1202 newname = strdup(val);
1203 if (newname == NULL)
1204 err(1, "no memory to set ifname");
1205 ifr.ifr_data = newname;
1206 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1208 err(1, "ioctl SIOCSIFNAME (set name)");
1211 strlcpy(name, newname, sizeof(name));
1217 setifdescr(const char *val, int dummy __unused, int s,
1218 const struct afswtch *afp)
1222 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1224 ifr.ifr_buffer.length = strlen(val) + 1;
1225 if (ifr.ifr_buffer.length == 1) {
1226 ifr.ifr_buffer.buffer = newdescr = NULL;
1227 ifr.ifr_buffer.length = 0;
1229 newdescr = strdup(val);
1230 ifr.ifr_buffer.buffer = newdescr;
1231 if (newdescr == NULL) {
1232 warn("no memory to set ifdescr");
1237 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
1238 err(1, "ioctl SIOCSIFDESCR (set descr)");
1245 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
1248 setifdescr("", 0, s, 0);
1252 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \
1253 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1254 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
1257 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1258 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1259 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1260 "\26RXCSUM_IPV6\27TXCSUM_IPV6\31TXRTLMT\32HWRXTSTMP"
1263 * Print the status of the interface. If an address family was
1264 * specified, show only it; otherwise, show them all.
1267 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
1268 struct ifaddrs *ifa)
1270 struct ifaddrs *ift;
1276 ifr.ifr_addr.sa_family = AF_LOCAL;
1279 ifr.ifr_addr.sa_family =
1280 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
1282 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1284 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1286 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1288 printf("%s: ", name);
1289 printb("flags", ifa->ifa_flags, IFFBITS);
1290 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1291 printf(" metric %d", ifr.ifr_metric);
1292 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1293 printf(" mtu %d", ifr.ifr_mtu);
1297 if ((descr = reallocf(descr, descrlen)) != NULL) {
1298 ifr.ifr_buffer.buffer = descr;
1299 ifr.ifr_buffer.length = descrlen;
1300 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1301 if (ifr.ifr_buffer.buffer == descr) {
1302 if (strlen(descr) > 0)
1303 printf("\tdescription: %s\n",
1305 } else if (ifr.ifr_buffer.length > descrlen) {
1306 descrlen = ifr.ifr_buffer.length;
1311 warn("unable to allocate memory for interface"
1316 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1317 if (ifr.ifr_curcap != 0) {
1318 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1321 if (supmedia && ifr.ifr_reqcap != 0) {
1322 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1329 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1330 if (ift->ifa_addr == NULL)
1332 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1335 const struct afswtch *p;
1336 p = af_getbyfamily(ift->ifa_addr->sa_family);
1337 if (p != NULL && p->af_status != NULL)
1338 p->af_status(s, ift);
1339 } else if (afp->af_af == ift->ifa_addr->sa_family)
1340 afp->af_status(s, ift);
1343 if (allfamilies || afp->af_af == AF_LINK) {
1344 const struct afswtch *lafp;
1347 * Hack; the link level address is received separately
1348 * from the routing information so any address is not
1349 * handled above. Cobble together an entry and invoke
1350 * the status method specially.
1352 lafp = af_getbyname("lladdr");
1354 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1355 lafp->af_status(s, &info);
1361 else if (afp->af_other_status != NULL)
1362 afp->af_other_status(s);
1364 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1365 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1366 printf("%s", ifs.ascii);
1369 sfp_status(s, &ifr, verbose);
1376 tunnel_status(int s)
1378 af_all_tunnel_status(s);
1382 Perror(const char *cmd)
1387 errx(1, "%s: no such interface", cmd);
1391 errx(1, "%s: permission denied", cmd);
1400 * Print a value a la the %b format of the kernel's printf
1403 printb(const char *s, unsigned v, const char *bits)
1408 if (bits && *bits == 8)
1409 printf("%s=%o", s, v);
1411 printf("%s=%x", s, v);
1415 while ((i = *bits++) != '\0') {
1416 if (v & (1 << (i-1))) {
1420 for (; (c = *bits) > 32; bits++)
1423 for (; *bits > 32; bits++)
1431 print_vhid(const struct ifaddrs *ifa, const char *s)
1433 struct if_data *ifd;
1435 if (ifa->ifa_data == NULL)
1438 ifd = ifa->ifa_data;
1439 if (ifd->ifi_vhid == 0)
1442 printf(" vhid %d", ifd->ifi_vhid);
1446 ifmaybeload(const char *name)
1448 #define MOD_PREFIX_LEN 3 /* "if_" */
1449 struct module_stat mstat;
1450 int i, fileid, modid;
1451 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1453 struct module_map_entry *mme;
1456 /* loading suppressed by the user */
1460 /* trim the interface number off the end */
1461 strlcpy(ifname, name, sizeof(ifname));
1462 for (dp = ifname; *dp != 0; dp++)
1468 /* Either derive it from the map or guess otherwise */
1471 for (i = 0; i < nitems(module_map); ++i) {
1472 mme = &module_map[i];
1473 if (strcmp(mme->ifname, ifname) == 0) {
1474 strlcpy(ifkind, mme->kldname, sizeof(ifkind));
1480 /* We didn't have an alias for it... we'll guess. */
1482 /* turn interface and unit into module name */
1483 strlcpy(ifkind, "if_", sizeof(ifkind));
1484 strlcat(ifkind, ifname, sizeof(ifkind));
1487 /* scan files in kernel */
1488 mstat.version = sizeof(struct module_stat);
1489 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1490 /* scan modules in file */
1491 for (modid = kldfirstmod(fileid); modid > 0;
1492 modid = modfnext(modid)) {
1493 if (modstat(modid, &mstat) < 0)
1495 /* strip bus name if present */
1496 if ((cp = strchr(mstat.name, '/')) != NULL) {
1502 * Is it already loaded? Don't compare with ifname if
1503 * we were specifically told which kld to use. Doing
1504 * so could lead to conflicts not trivially solved.
1506 if ((!found && strcmp(ifname, cp) == 0) ||
1507 strcmp(ifkind, cp) == 0)
1513 * Try to load the module. But ignore failures, because ifconfig can't
1514 * infer the names of all drivers (eg mlx4en(4)).
1516 (void) kldload(ifkind);
1519 static struct cmd basic_cmds[] = {
1520 DEF_CMD("up", IFF_UP, setifflags),
1521 DEF_CMD("down", -IFF_UP, setifflags),
1522 DEF_CMD("arp", -IFF_NOARP, setifflags),
1523 DEF_CMD("-arp", IFF_NOARP, setifflags),
1524 DEF_CMD("debug", IFF_DEBUG, setifflags),
1525 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1526 DEF_CMD_ARG("description", setifdescr),
1527 DEF_CMD_ARG("descr", setifdescr),
1528 DEF_CMD("-description", 0, unsetifdescr),
1529 DEF_CMD("-descr", 0, unsetifdescr),
1530 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1531 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1532 DEF_CMD("add", IFF_UP, notealias),
1533 DEF_CMD("alias", IFF_UP, notealias),
1534 DEF_CMD("-alias", -IFF_UP, notealias),
1535 DEF_CMD("delete", -IFF_UP, notealias),
1536 DEF_CMD("remove", -IFF_UP, notealias),
1538 #define EN_SWABIPS 0x1000
1539 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1540 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1542 DEF_CMD_ARG("netmask", setifnetmask),
1543 DEF_CMD_ARG("metric", setifmetric),
1544 DEF_CMD_ARG("broadcast", setifbroadaddr),
1545 DEF_CMD_ARG2("tunnel", settunnel),
1546 DEF_CMD("-tunnel", 0, deletetunnel),
1547 DEF_CMD("deletetunnel", 0, deletetunnel),
1549 DEF_CMD_ARG("vnet", setifvnet),
1550 DEF_CMD_ARG("-vnet", setifrvnet),
1552 DEF_CMD("link0", IFF_LINK0, setifflags),
1553 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1554 DEF_CMD("link1", IFF_LINK1, setifflags),
1555 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1556 DEF_CMD("link2", IFF_LINK2, setifflags),
1557 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1558 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1559 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1560 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1561 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1562 DEF_CMD("rxcsum6", IFCAP_RXCSUM_IPV6, setifcap),
1563 DEF_CMD("-rxcsum6", -IFCAP_RXCSUM_IPV6, setifcap),
1564 DEF_CMD("txcsum6", IFCAP_TXCSUM_IPV6, setifcap),
1565 DEF_CMD("-txcsum6", -IFCAP_TXCSUM_IPV6, setifcap),
1566 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1567 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1568 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1569 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1570 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1571 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1572 DEF_CMD_ARG("pcp", setifpcp),
1573 DEF_CMD("-pcp", 0, disableifpcp),
1574 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1575 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1576 DEF_CMD("tso6", IFCAP_TSO6, setifcap),
1577 DEF_CMD("-tso6", -IFCAP_TSO6, setifcap),
1578 DEF_CMD("tso4", IFCAP_TSO4, setifcap),
1579 DEF_CMD("-tso4", -IFCAP_TSO4, setifcap),
1580 DEF_CMD("tso", IFCAP_TSO, setifcap),
1581 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1582 DEF_CMD("toe", IFCAP_TOE, setifcap),
1583 DEF_CMD("-toe", -IFCAP_TOE, setifcap),
1584 DEF_CMD("lro", IFCAP_LRO, setifcap),
1585 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1586 DEF_CMD("wol", IFCAP_WOL, setifcap),
1587 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1588 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1589 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1590 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1591 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1592 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1593 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1594 DEF_CMD("txrtlmt", IFCAP_TXRTLMT, setifcap),
1595 DEF_CMD("-txrtlmt", -IFCAP_TXRTLMT, setifcap),
1596 DEF_CMD("hwrxtstmp", IFCAP_HWRXTSTMP, setifcap),
1597 DEF_CMD("-hwrxtstmp", -IFCAP_HWRXTSTMP, setifcap),
1598 DEF_CMD("normal", -IFF_LINK0, setifflags),
1599 DEF_CMD("compress", IFF_LINK0, setifflags),
1600 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1601 DEF_CMD_ARG("mtu", setifmtu),
1602 DEF_CMD_ARG("name", setifname),
1605 static __constructor void
1610 for (i = 0; i < nitems(basic_cmds); i++)
1611 cmd_register(&basic_cmds[i]);