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>
82 * Since "struct ifreq" is composed of various union members, callers
83 * should pay special attention to interpret the value.
84 * (.e.g. little/big endian difference in the structure.)
101 int printkeys = 0; /* Print keying material for interfaces. */
104 /* Formatter Strings */
105 char *f_inet, *f_inet6, *f_ether, *f_addr;
107 static int ifconfig(int argc, char *const *argv, int iscreate,
108 const struct afswtch *afp);
109 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
110 struct ifaddrs *ifa);
111 static void tunnel_status(int s);
112 static _Noreturn void usage(void);
114 static int getifflags(const char *ifname, int us);
116 static struct afswtch *af_getbyname(const char *name);
117 static struct afswtch *af_getbyfamily(int af);
118 static void af_other_status(int);
120 void printifnamemaybe(void);
122 static struct option *opts = NULL;
124 struct ifa_order_elt {
128 TAILQ_ENTRY(ifa_order_elt) link;
131 TAILQ_HEAD(ifa_queue, ifa_order_elt);
133 static struct module_map_entry {
139 .kldname = "if_tuntap",
143 .kldname = "if_tuntap",
147 .kldname = "if_tuntap",
153 opt_register(struct option *p)
165 /* XXX not right but close enough for now */
167 for (p = opts; p != NULL; p = p->next) {
168 strlcat(options, p->opt_usage, sizeof(options));
169 strlcat(options, " ", sizeof(options));
173 "usage: ifconfig [-f type:format] %sinterface address_family\n"
174 " [address [dest_address]] [parameters]\n"
175 " ifconfig interface create\n"
176 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
177 " ifconfig -l [-d] [-u] [address_family]\n"
178 " ifconfig %s[-d] [-m] [-u] [-v]\n",
179 options, options, options);
183 #define ORDERS_SIZE(x) sizeof(x) / sizeof(x[0])
186 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
188 struct ifaddrs *prev;
189 struct ifa_order_elt *cur;
190 unsigned int ord, af, ifa_ord;
197 while (ifa != NULL) {
199 strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
200 cur = calloc(1, sizeof(*cur));
205 TAILQ_INSERT_TAIL(q, cur, link);
206 cur->if_order = ifa_ord ++;
212 af = ifa->ifa_addr->sa_family;
214 if (af < ORDERS_SIZE(cur->af_orders) &&
215 cur->af_orders[af] == 0)
216 cur->af_orders[af] = ++ord;
226 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
228 struct ifa_order_elt *cur, *e1, *e2;
229 unsigned int af1, af2;
234 ret = strcmp(a->ifa_name, b->ifa_name);
236 TAILQ_FOREACH(cur, q, link) {
240 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
242 else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
249 return (e1->if_order - e2->if_order);
251 } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
252 TAILQ_FOREACH(cur, q, link) {
253 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
262 af1 = a->ifa_addr->sa_family;
263 af2 = b->ifa_addr->sa_family;
265 if (af1 < ORDERS_SIZE(e1->af_orders) &&
266 af2 < ORDERS_SIZE(e1->af_orders))
267 return (e1->af_orders[af1] - e1->af_orders[af2]);
273 static void freeformat(void)
286 static void setformat(char *input)
288 char *formatstr, *category, *modifier;
290 formatstr = strdup(input);
291 while ((category = strsep(&formatstr, ",")) != NULL) {
292 modifier = strchr(category, ':');
293 if (modifier == NULL || modifier[1] == '\0') {
294 warnx("Skipping invalid format specification: %s\n",
299 /* Split the string on the separator, then seek past it */
303 if (strcmp(category, "addr") == 0)
304 f_addr = strdup(modifier);
305 else if (strcmp(category, "ether") == 0)
306 f_ether = strdup(modifier);
307 else if (strcmp(category, "inet") == 0)
308 f_inet = strdup(modifier);
309 else if (strcmp(category, "inet6") == 0)
310 f_inet6 = strdup(modifier);
317 static struct ifaddrs *
318 sortifaddrs(struct ifaddrs *list,
319 int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *),
322 struct ifaddrs *right, *temp, *last, *result, *next, *tail;
331 if (!list || !list->ifa_next)
334 while (temp && temp->ifa_next) {
336 right = right->ifa_next;
337 temp = temp->ifa_next->ifa_next;
340 last->ifa_next = NULL;
342 list = sortifaddrs(list, compare, q);
343 right = sortifaddrs(right, compare, q);
345 while (list || right) {
349 list = list->ifa_next;
352 right = right->ifa_next;
353 } else if (compare(list, right, q) <= 0) {
355 list = list->ifa_next;
358 right = right->ifa_next;
364 tail->ifa_next = next;
372 void printifnamemaybe()
375 printf("%s\n", name);
379 main(int argc, char *argv[])
381 int c, all, namesonly, downonly, uponly;
382 const struct afswtch *afp = NULL;
384 struct ifaddrs *ifap, *sifap, *ifa;
386 const struct sockaddr_dl *sdl;
387 char options[1024], *cp, *envformat, *namecp = NULL;
388 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
389 struct ifa_order_elt *cur, *tmp;
395 all = downonly = uponly = namesonly = noload = verbose = 0;
396 f_inet = f_inet6 = f_ether = f_addr = NULL;
398 envformat = getenv("IFCONFIG_FORMAT");
399 if (envformat != NULL)
400 setformat(envformat);
403 * Ensure we print interface name when expected to,
404 * even if we terminate early due to error.
406 atexit(printifnamemaybe);
408 /* Parse leading line options */
409 strlcpy(options, "f:adklmnuv", sizeof(options));
410 for (p = opts; p != NULL; p = p->next)
411 strlcat(options, p->opt, sizeof(options));
412 while ((c = getopt(argc, argv, options)) != -1) {
414 case 'a': /* scan all interfaces */
417 case 'd': /* restrict scan to "down" interfaces */
428 case 'l': /* scan interface names only */
431 case 'm': /* show media choices in status */
434 case 'n': /* suppress module loading */
437 case 'u': /* restrict scan to "up" interfaces */
444 for (p = opts; p != NULL; p = p->next)
445 if (p->opt[0] == c) {
457 /* -l cannot be used with -a or -m */
458 if (namesonly && (all || supmedia))
462 if (uponly && downonly)
465 /* no arguments is equivalent to '-a' */
466 if (!namesonly && argc < 1)
469 /* -a and -l allow an address family arg to limit the output */
470 if (all || namesonly) {
477 afp = af_getbyname(*argv);
479 warnx("Address family '%s' unknown.", *argv);
482 if (afp->af_name != NULL)
484 /* leave with afp non-zero */
487 /* not listing, need an argument */
494 /* check and maybe load support for this interface */
497 ifindex = if_nametoindex(ifname);
500 * NOTE: We must special-case the `create' command
501 * right here as we would otherwise fail when trying
502 * to find the interface.
504 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
505 strcmp(argv[0], "plumb") == 0)) {
506 iflen = strlcpy(name, ifname, sizeof(name));
507 if (iflen >= sizeof(name))
508 errx(1, "%s: cloning name too long",
510 ifconfig(argc, argv, 1, NULL);
515 * NOTE: We have to special-case the `-vnet' command
516 * right here as we would otherwise fail when trying
517 * to find the interface as it lives in another vnet.
519 if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
520 iflen = strlcpy(name, ifname, sizeof(name));
521 if (iflen >= sizeof(name))
522 errx(1, "%s: interface name too long",
524 ifconfig(argc, argv, 0, NULL);
528 errx(1, "interface %s does not exist", ifname);
531 * Do not allow use `create` command as hostname if
532 * address family is not specified.
534 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
535 strcmp(argv[0], "plumb") == 0)) {
537 errx(1, "interface %s already exists",
544 /* Check for address family */
546 afp = af_getbyname(*argv);
552 * Check for a requested configuration action on a single interface,
553 * which doesn't require building, sorting, and searching the entire
554 * system address list
556 if ((argc > 0) && (ifname != NULL)) {
557 iflen = strlcpy(name, ifname, sizeof(name));
558 if (iflen >= sizeof(name)) {
559 warnx("%s: interface name too long, skipping", ifname);
561 flags = getifflags(name, -1);
562 if (!(((flags & IFF_CANTCONFIG) != 0) ||
563 (downonly && (flags & IFF_UP) != 0) ||
564 (uponly && (flags & IFF_UP) == 0)))
565 ifconfig(argc, argv, 0, afp);
570 if (getifaddrs(&ifap) != 0)
571 err(EXIT_FAILURE, "getifaddrs");
575 if (calcorders(ifap, &q) != 0)
576 err(EXIT_FAILURE, "calcorders");
578 sifap = sortifaddrs(ifap, cmpifaddrs, &q);
580 TAILQ_FOREACH_SAFE(cur, &q, link, tmp)
584 for (ifa = sifap; ifa; ifa = ifa->ifa_next) {
585 memset(&paifr, 0, sizeof(paifr));
586 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
587 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
588 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
589 ifa->ifa_addr->sa_len);
592 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
594 if (ifa->ifa_addr->sa_family == AF_LINK)
595 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
598 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
600 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
601 if (iflen >= sizeof(name)) {
602 warnx("%s: interface name too long, skipping",
608 if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
610 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
612 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
615 * Are we just listing the interfaces?
621 /* special case for "ether" address family */
622 if (!strcmp(afp->af_name, "ether")) {
624 (sdl->sdl_type != IFT_ETHER &&
625 sdl->sdl_type != IFT_L2VLAN &&
626 sdl->sdl_type != IFT_BRIDGE) ||
627 sdl->sdl_alen != ETHER_ADDR_LEN)
630 if (ifa->ifa_addr->sa_family
645 ifconfig(argc, argv, 0, afp);
647 status(afp, sdl, ifa);
658 static struct afswtch *afs = NULL;
661 af_register(struct afswtch *p)
667 static struct afswtch *
668 af_getbyname(const char *name)
672 for (afp = afs; afp != NULL; afp = afp->af_next)
673 if (strcmp(afp->af_name, name) == 0)
678 static struct afswtch *
679 af_getbyfamily(int af)
683 for (afp = afs; afp != NULL; afp = afp->af_next)
684 if (afp->af_af == af)
690 af_other_status(int s)
693 uint8_t afmask[howmany(AF_MAX, NBBY)];
695 memset(afmask, 0, sizeof(afmask));
696 for (afp = afs; afp != NULL; afp = afp->af_next) {
697 if (afp->af_other_status == NULL)
699 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
701 afp->af_other_status(s);
702 setbit(afmask, afp->af_af);
707 af_all_tunnel_status(int s)
710 uint8_t afmask[howmany(AF_MAX, NBBY)];
712 memset(afmask, 0, sizeof(afmask));
713 for (afp = afs; afp != NULL; afp = afp->af_next) {
714 if (afp->af_status_tunnel == NULL)
716 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
718 afp->af_status_tunnel(s);
719 setbit(afmask, afp->af_af);
723 static struct cmd *cmds = NULL;
726 cmd_register(struct cmd *p)
732 static const struct cmd *
733 cmd_lookup(const char *name, int iscreate)
737 for (p = cmds; p != NULL; p = p->c_next)
738 if (strcmp(name, p->c_name) == 0) {
751 callback_func *cb_func;
753 struct callback *cb_next;
755 static struct callback *callbacks = NULL;
758 callback_register(callback_func *func, void *arg)
762 cb = malloc(sizeof(struct callback));
764 errx(1, "unable to allocate memory for callback");
767 cb->cb_next = callbacks;
771 /* specially-handled commands */
772 static void setifaddr(const char *, int, int, const struct afswtch *);
773 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
775 static void setifdstaddr(const char *, int, int, const struct afswtch *);
776 static const struct cmd setifdstaddr_cmd =
777 DEF_CMD("ifdstaddr", 0, setifdstaddr);
780 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
782 const struct afswtch *afp, *nafp;
787 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
792 * This is the historical "accident" allowing users to configure IPv4
793 * addresses without the "inet" keyword which while a nice feature has
794 * proven to complicate other things. We cannot remove this but only
795 * make sure we will never have a similar implicit default for IPv6 or
796 * any other address familiy. We need a fallback though for
797 * ifconfig IF up/down etc. to work without INET support as people
798 * never used ifconfig IF link up/down, etc. either.
802 if (afp == NULL && feature_present("inet"))
803 afp = af_getbyname("inet");
807 afp = af_getbyname("link");
809 warnx("Please specify an address_family.");
813 ifr.ifr_addr.sa_family =
814 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
815 AF_LOCAL : afp->af_af;
817 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
818 (uafp != NULL || errno != EAFNOSUPPORT ||
819 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
820 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
823 p = cmd_lookup(*argv, iscreate);
824 if (iscreate && p == NULL) {
826 * Push the clone create callback so the new
827 * device is created and can be used for any
828 * remaining arguments.
832 errx(1, "internal error, no callback");
833 callbacks = cb->cb_next;
834 cb->cb_func(s, cb->cb_arg);
837 * Handle any address family spec that
838 * immediately follows and potentially
839 * recreate the socket.
841 nafp = af_getbyname(*argv);
851 * Look for a normal parameter.
857 * Not a recognized command, choose between setting
858 * the interface address and the dst address.
860 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
862 if (p->c_parameter == NEXTARG && p->c_u.c_func) {
864 errx(1, "'%s' requires argument",
866 p->c_u.c_func(argv[1], 0, s, afp);
868 } else if (p->c_parameter == OPTARG && p->c_u.c_func) {
869 p->c_u.c_func(argv[1], 0, s, afp);
872 } else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) {
874 errx(1, "'%s' requires 2 arguments",
876 p->c_u.c_func2(argv[1], argv[2], s, afp);
877 argc -= 2, argv += 2;
878 } else if (p->c_u.c_func)
879 p->c_u.c_func(*argv, p->c_parameter, s, afp);
884 * Do any post argument processing required by the address family.
886 if (afp->af_postproc != NULL)
887 afp->af_postproc(s, afp);
889 * Do deferred callbacks registered while processing
890 * command-line arguments.
892 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
893 cb->cb_func(s, cb->cb_arg);
895 * Do deferred operations.
898 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
899 warnx("interface %s cannot change %s addresses!",
906 strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
907 sizeof ifr.ifr_name);
908 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
910 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
911 /* means no previous address for interface */
913 Perror("ioctl (SIOCDIFADDR)");
917 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
918 warnx("interface %s cannot change %s addresses!",
923 if (newaddr && (setaddr || setmask)) {
924 strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
925 sizeof ifr.ifr_name);
926 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
927 Perror("ioctl (SIOCAIFADDR)");
936 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
938 if (afp->af_getaddr == NULL)
941 * Delay the ioctl to set the interface addr until flags are all set.
942 * The address interpretation may depend on the flags,
943 * and the flags may change when the address is set.
946 if (doalias == 0 && afp->af_af != AF_LINK)
948 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
952 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
954 struct addrinfo *srcres, *dstres;
957 if (afp->af_settunnel == NULL) {
958 warn("address family %s does not support tunnel setup",
963 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
964 errx(1, "error in parsing address string: %s",
965 gai_strerror(ecode));
967 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
968 errx(1, "error in parsing address string: %s",
969 gai_strerror(ecode));
971 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
973 "source and destination address families do not match");
975 afp->af_settunnel(s, srcres, dstres);
977 freeaddrinfo(srcres);
978 freeaddrinfo(dstres);
983 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
986 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
987 err(1, "SIOCDIFPHYADDR");
992 setifvnet(const char *jname, int dummy __unused, int s,
993 const struct afswtch *afp)
997 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
998 my_ifr.ifr_jid = jail_getid(jname);
999 if (my_ifr.ifr_jid < 0)
1000 errx(1, "%s", jail_errmsg);
1001 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
1002 err(1, "SIOCSIFVNET");
1006 setifrvnet(const char *jname, int dummy __unused, int s,
1007 const struct afswtch *afp)
1009 struct ifreq my_ifr;
1011 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1012 my_ifr.ifr_jid = jail_getid(jname);
1013 if (my_ifr.ifr_jid < 0)
1014 errx(1, "%s", jail_errmsg);
1015 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
1016 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
1021 setifnetmask(const char *addr, int dummy __unused, int s,
1022 const struct afswtch *afp)
1024 if (afp->af_getaddr != NULL) {
1026 afp->af_getaddr(addr, MASK);
1031 setifbroadaddr(const char *addr, int dummy __unused, int s,
1032 const struct afswtch *afp)
1034 if (afp->af_getaddr != NULL)
1035 afp->af_getaddr(addr, DSTADDR);
1039 notealias(const char *addr, int param, int s, const struct afswtch *afp)
1041 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1042 if (setaddr && doalias == 0 && param < 0)
1043 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1044 bcopy((caddr_t)rqtosa(af_addreq),
1045 (caddr_t)rqtosa(af_ridreq),
1046 rqtosa(af_addreq)->sa_len);
1058 setifdstaddr(const char *addr, int param __unused, int s,
1059 const struct afswtch *afp)
1061 if (afp->af_getaddr != NULL)
1062 afp->af_getaddr(addr, DSTADDR);
1066 getifflags(const char *ifname, int us)
1068 struct ifreq my_ifr;
1071 memset(&my_ifr, 0, sizeof(my_ifr));
1072 (void) strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name));
1074 if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
1075 err(1, "socket(family AF_LOCAL,SOCK_DGRAM");
1078 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1079 Perror("ioctl (SIOCGIFFLAGS)");
1084 return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16));
1088 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1089 * of the ifreq structure, which may confuse other parts of ifconfig.
1090 * Make a private copy so we can avoid that.
1093 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1095 struct ifreq my_ifr;
1098 flags = getifflags(name, s);
1104 memset(&my_ifr, 0, sizeof(my_ifr));
1105 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1106 my_ifr.ifr_flags = flags & 0xffff;
1107 my_ifr.ifr_flagshigh = flags >> 16;
1108 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1113 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1117 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1118 Perror("ioctl (SIOCGIFCAP)");
1121 flags = ifr.ifr_curcap;
1127 flags &= ifr.ifr_reqcap;
1128 ifr.ifr_reqcap = flags;
1129 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1134 setifmetric(const char *val, int dummy __unused, int s,
1135 const struct afswtch *afp)
1137 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1138 ifr.ifr_metric = atoi(val);
1139 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1140 err(1, "ioctl SIOCSIFMETRIC (set metric)");
1144 setifmtu(const char *val, int dummy __unused, int s,
1145 const struct afswtch *afp)
1147 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1148 ifr.ifr_mtu = atoi(val);
1149 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1150 err(1, "ioctl SIOCSIFMTU (set mtu)");
1154 setifpcp(const char *val, int arg __unused, int s, const struct afswtch *afp)
1159 ul = strtoul(val, &endp, 0);
1161 errx(1, "invalid value for pcp");
1163 errx(1, "value for pcp out of range");
1164 ifr.ifr_lan_pcp = ul;
1165 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1166 err(1, "SIOCSLANPCP");
1170 disableifpcp(const char *val, int arg __unused, int s,
1171 const struct afswtch *afp)
1174 ifr.ifr_lan_pcp = IFNET_PCP_NONE;
1175 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1176 err(1, "SIOCSLANPCP");
1180 setifname(const char *val, int dummy __unused, int s,
1181 const struct afswtch *afp)
1185 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1187 newname = strdup(val);
1188 if (newname == NULL)
1189 err(1, "no memory to set ifname");
1190 ifr.ifr_data = newname;
1191 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1193 err(1, "ioctl SIOCSIFNAME (set name)");
1196 strlcpy(name, newname, sizeof(name));
1202 setifdescr(const char *val, int dummy __unused, int s,
1203 const struct afswtch *afp)
1207 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1209 ifr.ifr_buffer.length = strlen(val) + 1;
1210 if (ifr.ifr_buffer.length == 1) {
1211 ifr.ifr_buffer.buffer = newdescr = NULL;
1212 ifr.ifr_buffer.length = 0;
1214 newdescr = strdup(val);
1215 ifr.ifr_buffer.buffer = newdescr;
1216 if (newdescr == NULL) {
1217 warn("no memory to set ifdescr");
1222 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
1223 err(1, "ioctl SIOCSIFDESCR (set descr)");
1230 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
1233 setifdescr("", 0, s, 0);
1237 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \
1238 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1239 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
1242 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1243 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1244 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1245 "\26RXCSUM_IPV6\27TXCSUM_IPV6\31TXRTLMT\32HWRXTSTMP"
1248 * Print the status of the interface. If an address family was
1249 * specified, show only it; otherwise, show them all.
1252 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
1253 struct ifaddrs *ifa)
1255 struct ifaddrs *ift;
1261 ifr.ifr_addr.sa_family = AF_LOCAL;
1264 ifr.ifr_addr.sa_family =
1265 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
1267 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1269 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1271 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1273 printf("%s: ", name);
1274 printb("flags", ifa->ifa_flags, IFFBITS);
1275 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1276 printf(" metric %d", ifr.ifr_metric);
1277 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1278 printf(" mtu %d", ifr.ifr_mtu);
1282 if ((descr = reallocf(descr, descrlen)) != NULL) {
1283 ifr.ifr_buffer.buffer = descr;
1284 ifr.ifr_buffer.length = descrlen;
1285 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1286 if (ifr.ifr_buffer.buffer == descr) {
1287 if (strlen(descr) > 0)
1288 printf("\tdescription: %s\n",
1290 } else if (ifr.ifr_buffer.length > descrlen) {
1291 descrlen = ifr.ifr_buffer.length;
1296 warn("unable to allocate memory for interface"
1301 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1302 if (ifr.ifr_curcap != 0) {
1303 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1306 if (supmedia && ifr.ifr_reqcap != 0) {
1307 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1314 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1315 if (ift->ifa_addr == NULL)
1317 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1320 const struct afswtch *p;
1321 p = af_getbyfamily(ift->ifa_addr->sa_family);
1322 if (p != NULL && p->af_status != NULL)
1323 p->af_status(s, ift);
1324 } else if (afp->af_af == ift->ifa_addr->sa_family)
1325 afp->af_status(s, ift);
1328 if (allfamilies || afp->af_af == AF_LINK) {
1329 const struct afswtch *lafp;
1332 * Hack; the link level address is received separately
1333 * from the routing information so any address is not
1334 * handled above. Cobble together an entry and invoke
1335 * the status method specially.
1337 lafp = af_getbyname("lladdr");
1339 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1340 lafp->af_status(s, &info);
1346 else if (afp->af_other_status != NULL)
1347 afp->af_other_status(s);
1349 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1350 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1351 printf("%s", ifs.ascii);
1354 sfp_status(s, &ifr, verbose);
1361 tunnel_status(int s)
1363 af_all_tunnel_status(s);
1367 Perror(const char *cmd)
1372 errx(1, "%s: no such interface", cmd);
1376 errx(1, "%s: permission denied", cmd);
1385 * Print a value a la the %b format of the kernel's printf
1388 printb(const char *s, unsigned v, const char *bits)
1393 if (bits && *bits == 8)
1394 printf("%s=%o", s, v);
1396 printf("%s=%x", s, v);
1400 while ((i = *bits++) != '\0') {
1401 if (v & (1 << (i-1))) {
1405 for (; (c = *bits) > 32; bits++)
1408 for (; *bits > 32; bits++)
1416 print_vhid(const struct ifaddrs *ifa, const char *s)
1418 struct if_data *ifd;
1420 if (ifa->ifa_data == NULL)
1423 ifd = ifa->ifa_data;
1424 if (ifd->ifi_vhid == 0)
1427 printf(" vhid %d", ifd->ifi_vhid);
1431 ifmaybeload(const char *name)
1433 #define MOD_PREFIX_LEN 3 /* "if_" */
1434 struct module_stat mstat;
1435 int i, fileid, modid;
1436 char ifname[IFNAMSIZ], *ifkind, *dp;
1438 struct module_map_entry *mme;
1440 /* loading suppressed by the user */
1444 /* trim the interface number off the end */
1445 strlcpy(ifname, name, sizeof(ifname));
1446 for (dp = ifname; *dp != 0; dp++)
1452 /* Either derive it from the map or guess otherwise */
1454 for (i = 0; i < nitems(module_map); ++i) {
1455 mme = &module_map[i];
1456 if (strcmp(mme->ifname, ifname) == 0) {
1457 ifkind = strdup(mme->kldname);
1459 err(EXIT_FAILURE, "ifmaybeload");
1464 /* We didn't have an alias for it... we'll guess. */
1465 if (ifkind == NULL) {
1466 ifkind = malloc(IFNAMSIZ + MOD_PREFIX_LEN);
1468 /* turn interface and unit into module name */
1469 strlcpy(ifkind, "if_", sizeof(ifkind));
1470 strlcat(ifkind, ifname, sizeof(ifkind));
1473 /* scan files in kernel */
1474 mstat.version = sizeof(struct module_stat);
1475 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1476 /* scan modules in file */
1477 for (modid = kldfirstmod(fileid); modid > 0;
1478 modid = modfnext(modid)) {
1479 if (modstat(modid, &mstat) < 0)
1481 /* strip bus name if present */
1482 if ((cp = strchr(mstat.name, '/')) != NULL) {
1487 /* already loaded? */
1488 if (strcmp(ifname, cp) == 0 ||
1489 strcmp(ifkind, cp) == 0)
1495 * Try to load the module. But ignore failures, because ifconfig can't
1496 * infer the names of all drivers (eg mlx4en(4)).
1498 (void) kldload(ifkind);
1503 static struct cmd basic_cmds[] = {
1504 DEF_CMD("up", IFF_UP, setifflags),
1505 DEF_CMD("down", -IFF_UP, setifflags),
1506 DEF_CMD("arp", -IFF_NOARP, setifflags),
1507 DEF_CMD("-arp", IFF_NOARP, setifflags),
1508 DEF_CMD("debug", IFF_DEBUG, setifflags),
1509 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1510 DEF_CMD_ARG("description", setifdescr),
1511 DEF_CMD_ARG("descr", setifdescr),
1512 DEF_CMD("-description", 0, unsetifdescr),
1513 DEF_CMD("-descr", 0, unsetifdescr),
1514 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1515 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1516 DEF_CMD("add", IFF_UP, notealias),
1517 DEF_CMD("alias", IFF_UP, notealias),
1518 DEF_CMD("-alias", -IFF_UP, notealias),
1519 DEF_CMD("delete", -IFF_UP, notealias),
1520 DEF_CMD("remove", -IFF_UP, notealias),
1522 #define EN_SWABIPS 0x1000
1523 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1524 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1526 DEF_CMD_ARG("netmask", setifnetmask),
1527 DEF_CMD_ARG("metric", setifmetric),
1528 DEF_CMD_ARG("broadcast", setifbroadaddr),
1529 DEF_CMD_ARG2("tunnel", settunnel),
1530 DEF_CMD("-tunnel", 0, deletetunnel),
1531 DEF_CMD("deletetunnel", 0, deletetunnel),
1533 DEF_CMD_ARG("vnet", setifvnet),
1534 DEF_CMD_ARG("-vnet", setifrvnet),
1536 DEF_CMD("link0", IFF_LINK0, setifflags),
1537 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1538 DEF_CMD("link1", IFF_LINK1, setifflags),
1539 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1540 DEF_CMD("link2", IFF_LINK2, setifflags),
1541 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1542 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1543 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1544 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1545 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1546 DEF_CMD("rxcsum6", IFCAP_RXCSUM_IPV6, setifcap),
1547 DEF_CMD("-rxcsum6", -IFCAP_RXCSUM_IPV6, setifcap),
1548 DEF_CMD("txcsum6", IFCAP_TXCSUM_IPV6, setifcap),
1549 DEF_CMD("-txcsum6", -IFCAP_TXCSUM_IPV6, setifcap),
1550 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1551 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1552 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1553 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1554 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1555 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1556 DEF_CMD_ARG("pcp", setifpcp),
1557 DEF_CMD("-pcp", 0, disableifpcp),
1558 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1559 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1560 DEF_CMD("tso6", IFCAP_TSO6, setifcap),
1561 DEF_CMD("-tso6", -IFCAP_TSO6, setifcap),
1562 DEF_CMD("tso4", IFCAP_TSO4, setifcap),
1563 DEF_CMD("-tso4", -IFCAP_TSO4, setifcap),
1564 DEF_CMD("tso", IFCAP_TSO, setifcap),
1565 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1566 DEF_CMD("toe", IFCAP_TOE, setifcap),
1567 DEF_CMD("-toe", -IFCAP_TOE, setifcap),
1568 DEF_CMD("lro", IFCAP_LRO, setifcap),
1569 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1570 DEF_CMD("wol", IFCAP_WOL, setifcap),
1571 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1572 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1573 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1574 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1575 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1576 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1577 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1578 DEF_CMD("txrtlmt", IFCAP_TXRTLMT, setifcap),
1579 DEF_CMD("-txrtlmt", -IFCAP_TXRTLMT, setifcap),
1580 DEF_CMD("hwrxtstmp", IFCAP_HWRXTSTMP, setifcap),
1581 DEF_CMD("-hwrxtstmp", -IFCAP_HWRXTSTMP, setifcap),
1582 DEF_CMD("normal", -IFF_LINK0, setifflags),
1583 DEF_CMD("compress", IFF_LINK0, setifflags),
1584 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1585 DEF_CMD_ARG("mtu", setifmtu),
1586 DEF_CMD_ARG("name", setifname),
1589 static __constructor void
1594 for (i = 0; i < nitems(basic_cmds); i++)
1595 cmd_register(&basic_cmds[i]);