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>
84 * Since "struct ifreq" is composed of various union members, callers
85 * should pay special attention to interpret the value.
86 * (.e.g. little/big endian difference in the structure.)
103 int printkeys = 0; /* Print keying material for interfaces. */
106 /* Formatter Strings */
107 char *f_inet, *f_inet6, *f_ether, *f_addr;
109 static bool group_member(const char *ifname, const char *match,
110 const char *nomatch);
111 static int ifconfig(int argc, char *const *argv, int iscreate,
112 const struct afswtch *afp);
113 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
114 struct ifaddrs *ifa);
115 static void tunnel_status(int s);
116 static _Noreturn void usage(void);
118 static int getifflags(const char *ifname, int us);
120 static struct afswtch *af_getbyname(const char *name);
121 static struct afswtch *af_getbyfamily(int af);
122 static void af_other_status(int);
124 void printifnamemaybe(void);
126 static struct option *opts = NULL;
128 struct ifa_order_elt {
132 TAILQ_ENTRY(ifa_order_elt) link;
135 TAILQ_HEAD(ifa_queue, ifa_order_elt);
137 static struct module_map_entry {
143 .kldname = "if_tuntap",
147 .kldname = "if_tuntap",
151 .kldname = "if_tuntap",
159 * This mapping exists because there is a conflicting enc module
160 * in CAM. ifconfig's guessing behavior will attempt to match
161 * the ifname to a module as well as if_${ifname} and clash with
162 * CAM enc. This is an assertion of the correct module to load.
171 opt_register(struct option *p)
183 /* XXX not right but close enough for now */
185 for (p = opts; p != NULL; p = p->next) {
186 strlcat(options, p->opt_usage, sizeof(options));
187 strlcat(options, " ", sizeof(options));
191 "usage: ifconfig [-f type:format] %sinterface address_family\n"
192 " [address [dest_address]] [parameters]\n"
193 " ifconfig interface create\n"
194 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
195 " ifconfig -l [-d] [-u] [address_family]\n"
196 " ifconfig %s[-d] [-m] [-u] [-v]\n",
197 options, options, options);
201 #define ORDERS_SIZE(x) sizeof(x) / sizeof(x[0])
204 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
206 struct ifaddrs *prev;
207 struct ifa_order_elt *cur;
208 unsigned int ord, af, ifa_ord;
215 while (ifa != NULL) {
217 strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
218 cur = calloc(1, sizeof(*cur));
223 TAILQ_INSERT_TAIL(q, cur, link);
224 cur->if_order = ifa_ord ++;
230 af = ifa->ifa_addr->sa_family;
232 if (af < ORDERS_SIZE(cur->af_orders) &&
233 cur->af_orders[af] == 0)
234 cur->af_orders[af] = ++ord;
244 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
246 struct ifa_order_elt *cur, *e1, *e2;
247 unsigned int af1, af2;
252 ret = strcmp(a->ifa_name, b->ifa_name);
254 TAILQ_FOREACH(cur, q, link) {
258 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
260 else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
267 return (e1->if_order - e2->if_order);
269 } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
270 TAILQ_FOREACH(cur, q, link) {
271 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
280 af1 = a->ifa_addr->sa_family;
281 af2 = b->ifa_addr->sa_family;
283 if (af1 < ORDERS_SIZE(e1->af_orders) &&
284 af2 < ORDERS_SIZE(e1->af_orders))
285 return (e1->af_orders[af1] - e1->af_orders[af2]);
291 static void freeformat(void)
304 static void setformat(char *input)
306 char *formatstr, *category, *modifier;
308 formatstr = strdup(input);
309 while ((category = strsep(&formatstr, ",")) != NULL) {
310 modifier = strchr(category, ':');
311 if (modifier == NULL || modifier[1] == '\0') {
312 warnx("Skipping invalid format specification: %s\n",
317 /* Split the string on the separator, then seek past it */
321 if (strcmp(category, "addr") == 0)
322 f_addr = strdup(modifier);
323 else if (strcmp(category, "ether") == 0)
324 f_ether = strdup(modifier);
325 else if (strcmp(category, "inet") == 0)
326 f_inet = strdup(modifier);
327 else if (strcmp(category, "inet6") == 0)
328 f_inet6 = strdup(modifier);
335 static struct ifaddrs *
336 sortifaddrs(struct ifaddrs *list,
337 int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *),
340 struct ifaddrs *right, *temp, *last, *result, *next, *tail;
349 if (!list || !list->ifa_next)
352 while (temp && temp->ifa_next) {
354 right = right->ifa_next;
355 temp = temp->ifa_next->ifa_next;
358 last->ifa_next = NULL;
360 list = sortifaddrs(list, compare, q);
361 right = sortifaddrs(right, compare, q);
363 while (list || right) {
367 list = list->ifa_next;
370 right = right->ifa_next;
371 } else if (compare(list, right, q) <= 0) {
373 list = list->ifa_next;
376 right = right->ifa_next;
382 tail->ifa_next = next;
390 void printifnamemaybe()
393 printf("%s\n", name);
397 main(int argc, char *argv[])
399 int c, all, namesonly, downonly, uponly;
400 const struct afswtch *afp = NULL;
402 struct ifaddrs *ifap, *sifap, *ifa;
404 const struct sockaddr_dl *sdl;
405 char options[1024], *cp, *envformat, *namecp = NULL;
406 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
407 struct ifa_order_elt *cur, *tmp;
408 const char *ifname, *matchgroup, *nogroup;
413 all = downonly = uponly = namesonly = noload = verbose = 0;
414 f_inet = f_inet6 = f_ether = f_addr = NULL;
415 matchgroup = nogroup = NULL;
417 envformat = getenv("IFCONFIG_FORMAT");
418 if (envformat != NULL)
419 setformat(envformat);
422 * Ensure we print interface name when expected to,
423 * even if we terminate early due to error.
425 atexit(printifnamemaybe);
427 /* Parse leading line options */
428 strlcpy(options, "G:adf:klmnuv", sizeof(options));
429 for (p = opts; p != NULL; p = p->next)
430 strlcat(options, p->opt, sizeof(options));
431 while ((c = getopt(argc, argv, options)) != -1) {
433 case 'a': /* scan all interfaces */
436 case 'd': /* restrict scan to "down" interfaces */
445 if (optarg == NULL || all == 0)
452 case 'l': /* scan interface names only */
455 case 'm': /* show media choices in status */
458 case 'n': /* suppress module loading */
461 case 'u': /* restrict scan to "up" interfaces */
476 for (p = opts; p != NULL; p = p->next)
477 if (p->opt[0] == c) {
489 /* -l cannot be used with -a or -m */
490 if (namesonly && (all || supmedia))
494 if (uponly && downonly)
497 /* no arguments is equivalent to '-a' */
498 if (!namesonly && argc < 1)
501 /* -a and -l allow an address family arg to limit the output */
502 if (all || namesonly) {
509 afp = af_getbyname(*argv);
511 warnx("Address family '%s' unknown.", *argv);
514 if (afp->af_name != NULL)
516 /* leave with afp non-zero */
519 /* not listing, need an argument */
526 /* check and maybe load support for this interface */
529 ifindex = if_nametoindex(ifname);
532 * NOTE: We must special-case the `create' command
533 * right here as we would otherwise fail when trying
534 * to find the interface.
536 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
537 strcmp(argv[0], "plumb") == 0)) {
538 iflen = strlcpy(name, ifname, sizeof(name));
539 if (iflen >= sizeof(name))
540 errx(1, "%s: cloning name too long",
542 ifconfig(argc, argv, 1, NULL);
547 * NOTE: We have to special-case the `-vnet' command
548 * right here as we would otherwise fail when trying
549 * to find the interface as it lives in another vnet.
551 if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
552 iflen = strlcpy(name, ifname, sizeof(name));
553 if (iflen >= sizeof(name))
554 errx(1, "%s: interface name too long",
556 ifconfig(argc, argv, 0, NULL);
560 errx(1, "interface %s does not exist", ifname);
563 * Do not allow use `create` command as hostname if
564 * address family is not specified.
566 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
567 strcmp(argv[0], "plumb") == 0)) {
569 errx(1, "interface %s already exists",
576 /* Check for address family */
578 afp = af_getbyname(*argv);
584 * Check for a requested configuration action on a single interface,
585 * which doesn't require building, sorting, and searching the entire
586 * system address list
588 if ((argc > 0) && (ifname != NULL)) {
589 iflen = strlcpy(name, ifname, sizeof(name));
590 if (iflen >= sizeof(name)) {
591 warnx("%s: interface name too long, skipping", ifname);
593 flags = getifflags(name, -1);
594 if (!(((flags & IFF_CANTCONFIG) != 0) ||
595 (downonly && (flags & IFF_UP) != 0) ||
596 (uponly && (flags & IFF_UP) == 0)))
597 ifconfig(argc, argv, 0, afp);
602 if (getifaddrs(&ifap) != 0)
603 err(EXIT_FAILURE, "getifaddrs");
607 if (calcorders(ifap, &q) != 0)
608 err(EXIT_FAILURE, "calcorders");
610 sifap = sortifaddrs(ifap, cmpifaddrs, &q);
612 TAILQ_FOREACH_SAFE(cur, &q, link, tmp)
616 for (ifa = sifap; ifa; ifa = ifa->ifa_next) {
617 memset(&paifr, 0, sizeof(paifr));
618 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
619 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
620 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
621 ifa->ifa_addr->sa_len);
624 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
626 if (ifa->ifa_addr->sa_family == AF_LINK)
627 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
630 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
632 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
633 if (iflen >= sizeof(name)) {
634 warnx("%s: interface name too long, skipping",
640 if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
642 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
644 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
646 if (!group_member(ifa->ifa_name, matchgroup, nogroup))
649 * Are we just listing the interfaces?
655 /* special case for "ether" address family */
656 if (!strcmp(afp->af_name, "ether")) {
658 (sdl->sdl_type != IFT_ETHER &&
659 sdl->sdl_type != IFT_L2VLAN &&
660 sdl->sdl_type != IFT_BRIDGE) ||
661 sdl->sdl_alen != ETHER_ADDR_LEN)
664 if (ifa->ifa_addr->sa_family
679 ifconfig(argc, argv, 0, afp);
681 status(afp, sdl, ifa);
693 * Returns true if an interface should be listed because any its groups
694 * matches shell pattern "match" and none of groups matches pattern "nomatch".
695 * If any pattern is NULL, corresponding condition is skipped.
698 group_member(const char *ifname, const char *match, const char *nomatch)
700 static int sock = -1;
702 struct ifgroupreq ifgr;
705 bool matched, nomatched;
708 if (match == NULL && nomatch == NULL)
713 memset(&ifgr, 0, sizeof(ifgr));
714 strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ);
716 /* The socket is opened once. Let _exit() close it. */
718 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
720 errx(1, "%s: socket(AF_LOCAL,SOCK_DGRAM)", __func__);
723 /* Determine amount of memory for the list of groups. */
724 if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
725 if (errno == EINVAL || errno == ENOTTY)
728 errx(1, "%s: SIOCGIFGROUP", __func__);
731 /* Obtain the list of groups. */
734 (struct ifg_req *)calloc(len / sizeof(*ifg), sizeof(*ifg));
736 if (ifgr.ifgr_groups == NULL)
737 errx(1, "%s: no memory", __func__);
738 if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
739 errx(1, "%s: SIOCGIFGROUP", __func__);
741 /* Perform matching. */
744 for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(*ifg); ifg++) {
745 len -= sizeof(struct ifg_req);
747 matched |= !fnmatch(match, ifg->ifgrq_group, 0);
749 nomatched &= fnmatch(nomatch, ifg->ifgrq_group, 0);
751 free(ifgr.ifgr_groups);
753 if (match && !nomatch)
755 if (!match && nomatch)
757 return (matched && nomatched);
760 static struct afswtch *afs = NULL;
763 af_register(struct afswtch *p)
769 static struct afswtch *
770 af_getbyname(const char *name)
774 for (afp = afs; afp != NULL; afp = afp->af_next)
775 if (strcmp(afp->af_name, name) == 0)
780 static struct afswtch *
781 af_getbyfamily(int af)
785 for (afp = afs; afp != NULL; afp = afp->af_next)
786 if (afp->af_af == af)
792 af_other_status(int s)
795 uint8_t afmask[howmany(AF_MAX, NBBY)];
797 memset(afmask, 0, sizeof(afmask));
798 for (afp = afs; afp != NULL; afp = afp->af_next) {
799 if (afp->af_other_status == NULL)
801 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
803 afp->af_other_status(s);
804 setbit(afmask, afp->af_af);
809 af_all_tunnel_status(int s)
812 uint8_t afmask[howmany(AF_MAX, NBBY)];
814 memset(afmask, 0, sizeof(afmask));
815 for (afp = afs; afp != NULL; afp = afp->af_next) {
816 if (afp->af_status_tunnel == NULL)
818 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
820 afp->af_status_tunnel(s);
821 setbit(afmask, afp->af_af);
825 static struct cmd *cmds = NULL;
828 cmd_register(struct cmd *p)
834 static const struct cmd *
835 cmd_lookup(const char *name, int iscreate)
839 for (p = cmds; p != NULL; p = p->c_next)
840 if (strcmp(name, p->c_name) == 0) {
853 callback_func *cb_func;
855 struct callback *cb_next;
857 static struct callback *callbacks = NULL;
860 callback_register(callback_func *func, void *arg)
864 cb = malloc(sizeof(struct callback));
866 errx(1, "unable to allocate memory for callback");
869 cb->cb_next = callbacks;
873 /* specially-handled commands */
874 static void setifaddr(const char *, int, int, const struct afswtch *);
875 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
877 static void setifdstaddr(const char *, int, int, const struct afswtch *);
878 static const struct cmd setifdstaddr_cmd =
879 DEF_CMD("ifdstaddr", 0, setifdstaddr);
882 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
884 const struct afswtch *afp, *nafp;
889 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
894 * This is the historical "accident" allowing users to configure IPv4
895 * addresses without the "inet" keyword which while a nice feature has
896 * proven to complicate other things. We cannot remove this but only
897 * make sure we will never have a similar implicit default for IPv6 or
898 * any other address familiy. We need a fallback though for
899 * ifconfig IF up/down etc. to work without INET support as people
900 * never used ifconfig IF link up/down, etc. either.
904 if (afp == NULL && feature_present("inet"))
905 afp = af_getbyname("inet");
909 afp = af_getbyname("link");
911 warnx("Please specify an address_family.");
915 ifr.ifr_addr.sa_family =
916 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
917 AF_LOCAL : afp->af_af;
919 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
920 (uafp != NULL || errno != EAFNOSUPPORT ||
921 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
922 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
925 p = cmd_lookup(*argv, iscreate);
926 if (iscreate && p == NULL) {
928 * Push the clone create callback so the new
929 * device is created and can be used for any
930 * remaining arguments.
934 errx(1, "internal error, no callback");
935 callbacks = cb->cb_next;
936 cb->cb_func(s, cb->cb_arg);
939 * Handle any address family spec that
940 * immediately follows and potentially
941 * recreate the socket.
943 nafp = af_getbyname(*argv);
953 * Look for a normal parameter.
959 * Not a recognized command, choose between setting
960 * the interface address and the dst address.
962 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
964 if (p->c_parameter == NEXTARG && p->c_u.c_func) {
966 errx(1, "'%s' requires argument",
968 p->c_u.c_func(argv[1], 0, s, afp);
970 } else if (p->c_parameter == OPTARG && p->c_u.c_func) {
971 p->c_u.c_func(argv[1], 0, s, afp);
974 } else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) {
976 errx(1, "'%s' requires 2 arguments",
978 p->c_u.c_func2(argv[1], argv[2], s, afp);
979 argc -= 2, argv += 2;
980 } else if (p->c_u.c_func)
981 p->c_u.c_func(*argv, p->c_parameter, s, afp);
986 * Do any post argument processing required by the address family.
988 if (afp->af_postproc != NULL)
989 afp->af_postproc(s, afp);
991 * Do deferred callbacks registered while processing
992 * command-line arguments.
994 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
995 cb->cb_func(s, cb->cb_arg);
997 * Do deferred operations.
1000 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
1001 warnx("interface %s cannot change %s addresses!",
1002 name, afp->af_name);
1008 strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
1009 sizeof ifr.ifr_name);
1010 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
1012 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
1013 /* means no previous address for interface */
1015 Perror("ioctl (SIOCDIFADDR)");
1019 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
1020 warnx("interface %s cannot change %s addresses!",
1021 name, afp->af_name);
1025 if (newaddr && (setaddr || setmask)) {
1026 strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
1027 sizeof ifr.ifr_name);
1028 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
1029 Perror("ioctl (SIOCAIFADDR)");
1038 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
1040 if (afp->af_getaddr == NULL)
1043 * Delay the ioctl to set the interface addr until flags are all set.
1044 * The address interpretation may depend on the flags,
1045 * and the flags may change when the address is set.
1048 if (doalias == 0 && afp->af_af != AF_LINK)
1050 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
1054 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
1056 struct addrinfo *srcres, *dstres;
1059 if (afp->af_settunnel == NULL) {
1060 warn("address family %s does not support tunnel setup",
1065 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
1066 errx(1, "error in parsing address string: %s",
1067 gai_strerror(ecode));
1069 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
1070 errx(1, "error in parsing address string: %s",
1071 gai_strerror(ecode));
1073 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
1075 "source and destination address families do not match");
1077 afp->af_settunnel(s, srcres, dstres);
1079 freeaddrinfo(srcres);
1080 freeaddrinfo(dstres);
1085 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
1088 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
1089 err(1, "SIOCDIFPHYADDR");
1094 setifvnet(const char *jname, int dummy __unused, int s,
1095 const struct afswtch *afp)
1097 struct ifreq my_ifr;
1099 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1100 my_ifr.ifr_jid = jail_getid(jname);
1101 if (my_ifr.ifr_jid < 0)
1102 errx(1, "%s", jail_errmsg);
1103 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
1104 err(1, "SIOCSIFVNET");
1108 setifrvnet(const char *jname, int dummy __unused, int s,
1109 const struct afswtch *afp)
1111 struct ifreq my_ifr;
1113 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1114 my_ifr.ifr_jid = jail_getid(jname);
1115 if (my_ifr.ifr_jid < 0)
1116 errx(1, "%s", jail_errmsg);
1117 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
1118 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
1123 setifnetmask(const char *addr, int dummy __unused, int s,
1124 const struct afswtch *afp)
1126 if (afp->af_getaddr != NULL) {
1128 afp->af_getaddr(addr, MASK);
1133 setifbroadaddr(const char *addr, int dummy __unused, int s,
1134 const struct afswtch *afp)
1136 if (afp->af_getaddr != NULL)
1137 afp->af_getaddr(addr, DSTADDR);
1141 notealias(const char *addr, int param, int s, const struct afswtch *afp)
1143 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1144 if (setaddr && doalias == 0 && param < 0)
1145 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1146 bcopy((caddr_t)rqtosa(af_addreq),
1147 (caddr_t)rqtosa(af_ridreq),
1148 rqtosa(af_addreq)->sa_len);
1160 setifdstaddr(const char *addr, int param __unused, int s,
1161 const struct afswtch *afp)
1163 if (afp->af_getaddr != NULL)
1164 afp->af_getaddr(addr, DSTADDR);
1168 getifflags(const char *ifname, int us)
1170 struct ifreq my_ifr;
1173 memset(&my_ifr, 0, sizeof(my_ifr));
1174 (void) strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name));
1176 if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
1177 err(1, "socket(family AF_LOCAL,SOCK_DGRAM");
1180 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1181 Perror("ioctl (SIOCGIFFLAGS)");
1186 return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16));
1190 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1191 * of the ifreq structure, which may confuse other parts of ifconfig.
1192 * Make a private copy so we can avoid that.
1195 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1197 struct ifreq my_ifr;
1200 flags = getifflags(name, s);
1206 memset(&my_ifr, 0, sizeof(my_ifr));
1207 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1208 my_ifr.ifr_flags = flags & 0xffff;
1209 my_ifr.ifr_flagshigh = flags >> 16;
1210 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1215 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1219 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1220 Perror("ioctl (SIOCGIFCAP)");
1223 flags = ifr.ifr_curcap;
1229 flags &= ifr.ifr_reqcap;
1230 ifr.ifr_reqcap = flags;
1231 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1236 setifmetric(const char *val, int dummy __unused, int s,
1237 const struct afswtch *afp)
1239 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1240 ifr.ifr_metric = atoi(val);
1241 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1242 err(1, "ioctl SIOCSIFMETRIC (set metric)");
1246 setifmtu(const char *val, int dummy __unused, int s,
1247 const struct afswtch *afp)
1249 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1250 ifr.ifr_mtu = atoi(val);
1251 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1252 err(1, "ioctl SIOCSIFMTU (set mtu)");
1256 setifpcp(const char *val, int arg __unused, int s, const struct afswtch *afp)
1261 ul = strtoul(val, &endp, 0);
1263 errx(1, "invalid value for pcp");
1265 errx(1, "value for pcp out of range");
1266 ifr.ifr_lan_pcp = ul;
1267 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1268 err(1, "SIOCSLANPCP");
1272 disableifpcp(const char *val, int arg __unused, int s,
1273 const struct afswtch *afp)
1276 ifr.ifr_lan_pcp = IFNET_PCP_NONE;
1277 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1278 err(1, "SIOCSLANPCP");
1282 setifname(const char *val, int dummy __unused, int s,
1283 const struct afswtch *afp)
1287 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1289 newname = strdup(val);
1290 if (newname == NULL)
1291 err(1, "no memory to set ifname");
1292 ifr.ifr_data = newname;
1293 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1295 err(1, "ioctl SIOCSIFNAME (set name)");
1298 strlcpy(name, newname, sizeof(name));
1304 setifdescr(const char *val, int dummy __unused, int s,
1305 const struct afswtch *afp)
1309 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1311 ifr.ifr_buffer.length = strlen(val) + 1;
1312 if (ifr.ifr_buffer.length == 1) {
1313 ifr.ifr_buffer.buffer = newdescr = NULL;
1314 ifr.ifr_buffer.length = 0;
1316 newdescr = strdup(val);
1317 ifr.ifr_buffer.buffer = newdescr;
1318 if (newdescr == NULL) {
1319 warn("no memory to set ifdescr");
1324 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
1325 err(1, "ioctl SIOCSIFDESCR (set descr)");
1332 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
1335 setifdescr("", 0, s, 0);
1339 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \
1340 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1341 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
1344 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1345 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1346 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1347 "\26RXCSUM_IPV6\27TXCSUM_IPV6\31TXRTLMT\32HWRXTSTMP\33NOMAP\34TXTLS4\35TXTLS6" \
1348 "\36VXLAN_HWCSUM\37VXLAN_HWTSO"
1351 * Print the status of the interface. If an address family was
1352 * specified, show only it; otherwise, show them all.
1355 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
1356 struct ifaddrs *ifa)
1358 struct ifaddrs *ift;
1364 ifr.ifr_addr.sa_family = AF_LOCAL;
1367 ifr.ifr_addr.sa_family =
1368 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
1370 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1372 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1374 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1376 printf("%s: ", name);
1377 printb("flags", ifa->ifa_flags, IFFBITS);
1378 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1379 printf(" metric %d", ifr.ifr_metric);
1380 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1381 printf(" mtu %d", ifr.ifr_mtu);
1385 if ((descr = reallocf(descr, descrlen)) != NULL) {
1386 ifr.ifr_buffer.buffer = descr;
1387 ifr.ifr_buffer.length = descrlen;
1388 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1389 if (ifr.ifr_buffer.buffer == descr) {
1390 if (strlen(descr) > 0)
1391 printf("\tdescription: %s\n",
1393 } else if (ifr.ifr_buffer.length > descrlen) {
1394 descrlen = ifr.ifr_buffer.length;
1399 warn("unable to allocate memory for interface"
1404 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1405 if (ifr.ifr_curcap != 0) {
1406 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1409 if (supmedia && ifr.ifr_reqcap != 0) {
1410 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1417 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1418 if (ift->ifa_addr == NULL)
1420 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1423 const struct afswtch *p;
1424 p = af_getbyfamily(ift->ifa_addr->sa_family);
1425 if (p != NULL && p->af_status != NULL)
1426 p->af_status(s, ift);
1427 } else if (afp->af_af == ift->ifa_addr->sa_family)
1428 afp->af_status(s, ift);
1431 if (allfamilies || afp->af_af == AF_LINK) {
1432 const struct afswtch *lafp;
1435 * Hack; the link level address is received separately
1436 * from the routing information so any address is not
1437 * handled above. Cobble together an entry and invoke
1438 * the status method specially.
1440 lafp = af_getbyname("lladdr");
1442 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1443 lafp->af_status(s, &info);
1449 else if (afp->af_other_status != NULL)
1450 afp->af_other_status(s);
1452 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1453 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1454 printf("%s", ifs.ascii);
1457 sfp_status(s, &ifr, verbose);
1464 tunnel_status(int s)
1466 af_all_tunnel_status(s);
1470 Perror(const char *cmd)
1475 errx(1, "%s: no such interface", cmd);
1479 errx(1, "%s: permission denied", cmd);
1488 * Print a value a la the %b format of the kernel's printf
1491 printb(const char *s, unsigned v, const char *bits)
1496 if (bits && *bits == 8)
1497 printf("%s=%o", s, v);
1499 printf("%s=%x", s, v);
1503 while ((i = *bits++) != '\0') {
1504 if (v & (1 << (i-1))) {
1508 for (; (c = *bits) > 32; bits++)
1511 for (; *bits > 32; bits++)
1519 print_vhid(const struct ifaddrs *ifa, const char *s)
1521 struct if_data *ifd;
1523 if (ifa->ifa_data == NULL)
1526 ifd = ifa->ifa_data;
1527 if (ifd->ifi_vhid == 0)
1530 printf(" vhid %d", ifd->ifi_vhid);
1534 ifmaybeload(const char *name)
1536 #define MOD_PREFIX_LEN 3 /* "if_" */
1537 struct module_stat mstat;
1538 int i, fileid, modid;
1539 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1541 struct module_map_entry *mme;
1544 /* loading suppressed by the user */
1548 /* trim the interface number off the end */
1549 strlcpy(ifname, name, sizeof(ifname));
1550 for (dp = ifname; *dp != 0; dp++)
1556 /* Either derive it from the map or guess otherwise */
1559 for (i = 0; i < nitems(module_map); ++i) {
1560 mme = &module_map[i];
1561 if (strcmp(mme->ifname, ifname) == 0) {
1562 strlcpy(ifkind, mme->kldname, sizeof(ifkind));
1568 /* We didn't have an alias for it... we'll guess. */
1570 /* turn interface and unit into module name */
1571 strlcpy(ifkind, "if_", sizeof(ifkind));
1572 strlcat(ifkind, ifname, sizeof(ifkind));
1575 /* scan files in kernel */
1576 mstat.version = sizeof(struct module_stat);
1577 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1578 /* scan modules in file */
1579 for (modid = kldfirstmod(fileid); modid > 0;
1580 modid = modfnext(modid)) {
1581 if (modstat(modid, &mstat) < 0)
1583 /* strip bus name if present */
1584 if ((cp = strchr(mstat.name, '/')) != NULL) {
1590 * Is it already loaded? Don't compare with ifname if
1591 * we were specifically told which kld to use. Doing
1592 * so could lead to conflicts not trivially solved.
1594 if ((!found && strcmp(ifname, cp) == 0) ||
1595 strcmp(ifkind, cp) == 0)
1601 * Try to load the module. But ignore failures, because ifconfig can't
1602 * infer the names of all drivers (eg mlx4en(4)).
1604 (void) kldload(ifkind);
1607 static struct cmd basic_cmds[] = {
1608 DEF_CMD("up", IFF_UP, setifflags),
1609 DEF_CMD("down", -IFF_UP, setifflags),
1610 DEF_CMD("arp", -IFF_NOARP, setifflags),
1611 DEF_CMD("-arp", IFF_NOARP, setifflags),
1612 DEF_CMD("debug", IFF_DEBUG, setifflags),
1613 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1614 DEF_CMD_ARG("description", setifdescr),
1615 DEF_CMD_ARG("descr", setifdescr),
1616 DEF_CMD("-description", 0, unsetifdescr),
1617 DEF_CMD("-descr", 0, unsetifdescr),
1618 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1619 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1620 DEF_CMD("add", IFF_UP, notealias),
1621 DEF_CMD("alias", IFF_UP, notealias),
1622 DEF_CMD("-alias", -IFF_UP, notealias),
1623 DEF_CMD("delete", -IFF_UP, notealias),
1624 DEF_CMD("remove", -IFF_UP, notealias),
1626 #define EN_SWABIPS 0x1000
1627 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1628 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1630 DEF_CMD_ARG("netmask", setifnetmask),
1631 DEF_CMD_ARG("metric", setifmetric),
1632 DEF_CMD_ARG("broadcast", setifbroadaddr),
1633 DEF_CMD_ARG2("tunnel", settunnel),
1634 DEF_CMD("-tunnel", 0, deletetunnel),
1635 DEF_CMD("deletetunnel", 0, deletetunnel),
1637 DEF_CMD_ARG("vnet", setifvnet),
1638 DEF_CMD_ARG("-vnet", setifrvnet),
1640 DEF_CMD("link0", IFF_LINK0, setifflags),
1641 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1642 DEF_CMD("link1", IFF_LINK1, setifflags),
1643 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1644 DEF_CMD("link2", IFF_LINK2, setifflags),
1645 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1646 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1647 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1648 DEF_CMD("nomap", IFCAP_NOMAP, setifcap),
1649 DEF_CMD("-nomap", -IFCAP_NOMAP, setifcap),
1650 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1651 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1652 DEF_CMD("rxcsum6", IFCAP_RXCSUM_IPV6, setifcap),
1653 DEF_CMD("-rxcsum6", -IFCAP_RXCSUM_IPV6, setifcap),
1654 DEF_CMD("txcsum6", IFCAP_TXCSUM_IPV6, setifcap),
1655 DEF_CMD("-txcsum6", -IFCAP_TXCSUM_IPV6, setifcap),
1656 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1657 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1658 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1659 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1660 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1661 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1662 DEF_CMD_ARG("pcp", setifpcp),
1663 DEF_CMD("-pcp", 0, disableifpcp),
1664 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1665 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1666 DEF_CMD("tso6", IFCAP_TSO6, setifcap),
1667 DEF_CMD("-tso6", -IFCAP_TSO6, setifcap),
1668 DEF_CMD("tso4", IFCAP_TSO4, setifcap),
1669 DEF_CMD("-tso4", -IFCAP_TSO4, setifcap),
1670 DEF_CMD("tso", IFCAP_TSO, setifcap),
1671 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1672 DEF_CMD("toe", IFCAP_TOE, setifcap),
1673 DEF_CMD("-toe", -IFCAP_TOE, setifcap),
1674 DEF_CMD("lro", IFCAP_LRO, setifcap),
1675 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1676 DEF_CMD("txtls", IFCAP_TXTLS, setifcap),
1677 DEF_CMD("-txtls", -IFCAP_TXTLS, setifcap),
1678 DEF_CMD("wol", IFCAP_WOL, setifcap),
1679 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1680 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1681 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1682 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1683 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1684 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1685 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1686 DEF_CMD("txrtlmt", IFCAP_TXRTLMT, setifcap),
1687 DEF_CMD("-txrtlmt", -IFCAP_TXRTLMT, setifcap),
1688 DEF_CMD("hwrxtstmp", IFCAP_HWRXTSTMP, setifcap),
1689 DEF_CMD("-hwrxtstmp", -IFCAP_HWRXTSTMP, setifcap),
1690 DEF_CMD("normal", -IFF_LINK0, setifflags),
1691 DEF_CMD("compress", IFF_LINK0, setifflags),
1692 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1693 DEF_CMD_ARG("mtu", setifmtu),
1694 DEF_CMD_ARG("name", setifname),
1697 static __constructor void
1702 for (i = 0; i < nitems(basic_cmds); i++)
1703 cmd_register(&basic_cmds[i]);