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";
37 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
41 #include <sys/param.h>
42 #include <sys/ioctl.h>
46 #include <sys/module.h>
47 #include <sys/linker.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
52 #include <net/ethernet.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <arpa/inet.h>
79 #include <libifconfig.h>
83 ifconfig_handle_t *lifh;
86 * Since "struct ifreq" is composed of various union members, callers
87 * should pay special attention to interpret the value.
88 * (.e.g. little/big endian difference in the structure.)
105 int printkeys = 0; /* Print keying material for interfaces. */
108 /* Formatter Strings */
109 char *f_inet, *f_inet6, *f_ether, *f_addr;
111 static bool group_member(const char *ifname, const char *match,
112 const char *nomatch);
113 static int ifconfig(int argc, char *const *argv, int iscreate,
114 const struct afswtch *afp);
115 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
116 struct ifaddrs *ifa);
117 static void tunnel_status(int s);
118 static _Noreturn void usage(void);
120 static int getifflags(const char *ifname, int us, bool err_ok);
122 static struct afswtch *af_getbyname(const char *name);
123 static struct afswtch *af_getbyfamily(int af);
124 static void af_other_status(int);
126 void printifnamemaybe(void);
128 static struct option *opts = NULL;
130 struct ifa_order_elt {
134 TAILQ_ENTRY(ifa_order_elt) link;
137 TAILQ_HEAD(ifa_queue, ifa_order_elt);
139 static struct module_map_entry {
145 .kldname = "if_tuntap",
149 .kldname = "if_tuntap",
153 .kldname = "if_tuntap",
161 * This mapping exists because there is a conflicting enc module
162 * in CAM. ifconfig's guessing behavior will attempt to match
163 * the ifname to a module as well as if_${ifname} and clash with
164 * CAM enc. This is an assertion of the correct module to load.
173 opt_register(struct option *p)
185 /* XXX not right but close enough for now */
187 for (p = opts; p != NULL; p = p->next) {
188 strlcat(options, p->opt_usage, sizeof(options));
189 strlcat(options, " ", sizeof(options));
193 "usage: ifconfig [-j jail] [-f type:format] %sinterface address_family\n"
194 " [address [dest_address]] [parameters]\n"
195 " ifconfig [-j jail] interface create\n"
196 " ifconfig [-j jail] -a %s[-d] [-m] [-u] [-v] [address_family]\n"
197 " ifconfig [-j jail] -l [-d] [-u] [address_family]\n"
198 " ifconfig [-j jail] %s[-d] [-m] [-u] [-v]\n",
199 options, options, options);
204 ioctl_ifcreate(int s, struct ifreq *ifr)
206 if (ioctl(s, SIOCIFCREATE2, ifr) < 0) {
209 errx(1, "interface %s already exists", ifr->ifr_name);
211 err(1, "SIOCIFCREATE2 (%s)", ifr->ifr_name);
217 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
219 struct ifaddrs *prev;
220 struct ifa_order_elt *cur;
221 unsigned int ord, af, ifa_ord;
228 while (ifa != NULL) {
230 strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
231 cur = calloc(1, sizeof(*cur));
236 TAILQ_INSERT_TAIL(q, cur, link);
237 cur->if_order = ifa_ord ++;
243 af = ifa->ifa_addr->sa_family;
245 if (af < nitems(cur->af_orders) &&
246 cur->af_orders[af] == 0)
247 cur->af_orders[af] = ++ord;
257 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
259 struct ifa_order_elt *cur, *e1, *e2;
260 unsigned int af1, af2;
265 ret = strcmp(a->ifa_name, b->ifa_name);
267 TAILQ_FOREACH(cur, q, link) {
271 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
273 else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
280 return (e1->if_order - e2->if_order);
282 } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
283 TAILQ_FOREACH(cur, q, link) {
284 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
293 af1 = a->ifa_addr->sa_family;
294 af2 = b->ifa_addr->sa_family;
296 if (af1 < nitems(e1->af_orders) && af2 < nitems(e1->af_orders))
297 return (e1->af_orders[af1] - e1->af_orders[af2]);
303 static void freeformat(void)
316 static void setformat(char *input)
318 char *formatstr, *category, *modifier;
320 formatstr = strdup(input);
321 while ((category = strsep(&formatstr, ",")) != NULL) {
322 modifier = strchr(category, ':');
323 if (modifier == NULL || modifier[1] == '\0') {
324 warnx("Skipping invalid format specification: %s\n",
329 /* Split the string on the separator, then seek past it */
333 if (strcmp(category, "addr") == 0)
334 f_addr = strdup(modifier);
335 else if (strcmp(category, "ether") == 0)
336 f_ether = strdup(modifier);
337 else if (strcmp(category, "inet") == 0)
338 f_inet = strdup(modifier);
339 else if (strcmp(category, "inet6") == 0)
340 f_inet6 = strdup(modifier);
345 static struct ifaddrs *
346 sortifaddrs(struct ifaddrs *list,
347 int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *),
350 struct ifaddrs *right, *temp, *last, *result, *next, *tail;
359 if (!list || !list->ifa_next)
362 while (temp && temp->ifa_next) {
364 right = right->ifa_next;
365 temp = temp->ifa_next->ifa_next;
368 last->ifa_next = NULL;
370 list = sortifaddrs(list, compare, q);
371 right = sortifaddrs(right, compare, q);
373 while (list || right) {
377 list = list->ifa_next;
380 right = right->ifa_next;
381 } else if (compare(list, right, q) <= 0) {
383 list = list->ifa_next;
386 right = right->ifa_next;
392 tail->ifa_next = next;
400 void printifnamemaybe()
403 printf("%s\n", name);
407 main(int argc, char *argv[])
409 int c, all, namesonly, downonly, uponly;
410 const struct afswtch *afp = NULL;
412 struct ifaddrs *ifap, *sifap, *ifa;
414 const struct sockaddr_dl *sdl;
415 char options[1024], *cp, *envformat, *namecp = NULL;
417 char *jail_name = NULL;
419 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
420 struct ifa_order_elt *cur, *tmp;
421 const char *ifname, *matchgroup, *nogroup;
429 all = downonly = uponly = namesonly = noload = verbose = 0;
430 f_inet = f_inet6 = f_ether = f_addr = NULL;
431 matchgroup = nogroup = NULL;
433 lifh = ifconfig_open();
435 err(EXIT_FAILURE, "ifconfig_open");
437 envformat = getenv("IFCONFIG_FORMAT");
438 if (envformat != NULL)
439 setformat(envformat);
442 * Ensure we print interface name when expected to,
443 * even if we terminate early due to error.
445 atexit(printifnamemaybe);
447 /* Parse leading line options */
448 strlcpy(options, "G:adf:j:klmnuv", sizeof(options));
449 for (p = opts; p != NULL; p = p->next)
450 strlcat(options, p->opt, sizeof(options));
451 while ((c = getopt(argc, argv, options)) != -1) {
453 case 'a': /* scan all interfaces */
456 case 'd': /* restrict scan to "down" interfaces */
465 if (optarg == NULL || all == 0)
475 Perror("not built with jail support");
481 case 'l': /* scan interface names only */
484 case 'm': /* show media choices in status */
487 case 'n': /* suppress module loading */
490 case 'u': /* restrict scan to "up" interfaces */
505 for (p = opts; p != NULL; p = p->next)
506 if (p->opt[0] == c) {
518 /* -l cannot be used with -a or -m */
519 if (namesonly && (all || supmedia))
523 if (uponly && downonly)
526 /* no arguments is equivalent to '-a' */
527 if (!namesonly && argc < 1)
532 jid = jail_getid(jail_name);
534 Perror("jail not found");
535 if (jail_attach(jid) != 0)
536 Perror("cannot attach to jail");
540 /* -a and -l allow an address family arg to limit the output */
541 if (all || namesonly) {
548 afp = af_getbyname(*argv);
550 warnx("Address family '%s' unknown.", *argv);
553 if (afp->af_name != NULL)
555 /* leave with afp non-zero */
558 /* not listing, need an argument */
565 /* check and maybe load support for this interface */
568 ifindex = if_nametoindex(ifname);
571 * NOTE: We must special-case the `create' command
572 * right here as we would otherwise fail when trying
573 * to find the interface.
575 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
576 strcmp(argv[0], "plumb") == 0)) {
577 iflen = strlcpy(name, ifname, sizeof(name));
578 if (iflen >= sizeof(name))
579 errx(1, "%s: cloning name too long",
581 ifconfig(argc, argv, 1, NULL);
586 * NOTE: We have to special-case the `-vnet' command
587 * right here as we would otherwise fail when trying
588 * to find the interface as it lives in another vnet.
590 if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
591 iflen = strlcpy(name, ifname, sizeof(name));
592 if (iflen >= sizeof(name))
593 errx(1, "%s: interface name too long",
595 ifconfig(argc, argv, 0, NULL);
599 errx(1, "interface %s does not exist", ifname);
602 * Do not allow use `create` command as hostname if
603 * address family is not specified.
605 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
606 strcmp(argv[0], "plumb") == 0)) {
608 errx(1, "interface %s already exists",
615 /* Check for address family */
617 afp = af_getbyname(*argv);
623 * Check for a requested configuration action on a single interface,
624 * which doesn't require building, sorting, and searching the entire
625 * system address list
627 if ((argc > 0) && (ifname != NULL)) {
628 iflen = strlcpy(name, ifname, sizeof(name));
629 if (iflen >= sizeof(name)) {
630 warnx("%s: interface name too long, skipping", ifname);
632 flags = getifflags(name, -1, false);
633 if (!(((flags & IFF_CANTCONFIG) != 0) ||
634 (downonly && (flags & IFF_UP) != 0) ||
635 (uponly && (flags & IFF_UP) == 0)))
636 ifconfig(argc, argv, 0, afp);
641 if (getifaddrs(&ifap) != 0)
642 err(EXIT_FAILURE, "getifaddrs");
646 if (calcorders(ifap, &q) != 0)
647 err(EXIT_FAILURE, "calcorders");
649 sifap = sortifaddrs(ifap, cmpifaddrs, &q);
651 TAILQ_FOREACH_SAFE(cur, &q, link, tmp)
655 for (ifa = sifap; ifa; ifa = ifa->ifa_next) {
656 memset(&paifr, 0, sizeof(paifr));
657 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
658 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
659 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
660 ifa->ifa_addr->sa_len);
663 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
665 if (ifa->ifa_addr->sa_family == AF_LINK)
666 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
669 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
671 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
672 if (iflen >= sizeof(name)) {
673 warnx("%s: interface name too long, skipping",
679 if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
681 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
683 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
685 if (!group_member(ifa->ifa_name, matchgroup, nogroup))
688 * Are we just listing the interfaces?
694 /* special case for "ether" address family */
695 if (!strcmp(afp->af_name, "ether")) {
697 (sdl->sdl_type != IFT_ETHER &&
698 sdl->sdl_type != IFT_L2VLAN &&
699 sdl->sdl_type != IFT_BRIDGE) ||
700 sdl->sdl_alen != ETHER_ADDR_LEN)
703 if (ifa->ifa_addr->sa_family
718 ifconfig(argc, argv, 0, afp);
720 status(afp, sdl, ifa);
728 ifconfig_close(lifh);
733 * Returns true if an interface should be listed because any its groups
734 * matches shell pattern "match" and none of groups matches pattern "nomatch".
735 * If any pattern is NULL, corresponding condition is skipped.
738 group_member(const char *ifname, const char *match, const char *nomatch)
740 static int sock = -1;
742 struct ifgroupreq ifgr;
745 bool matched, nomatched;
748 if (match == NULL && nomatch == NULL)
753 memset(&ifgr, 0, sizeof(ifgr));
754 strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ);
756 /* The socket is opened once. Let _exit() close it. */
758 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
760 errx(1, "%s: socket(AF_LOCAL,SOCK_DGRAM)", __func__);
763 /* Determine amount of memory for the list of groups. */
764 if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
765 if (errno == EINVAL || errno == ENOTTY)
768 errx(1, "%s: SIOCGIFGROUP", __func__);
771 /* Obtain the list of groups. */
774 (struct ifg_req *)calloc(len / sizeof(*ifg), sizeof(*ifg));
776 if (ifgr.ifgr_groups == NULL)
777 errx(1, "%s: no memory", __func__);
778 if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
779 errx(1, "%s: SIOCGIFGROUP", __func__);
781 /* Perform matching. */
784 for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(*ifg); ifg++) {
785 len -= sizeof(struct ifg_req);
787 matched |= !fnmatch(match, ifg->ifgrq_group, 0);
789 nomatched &= fnmatch(nomatch, ifg->ifgrq_group, 0);
791 free(ifgr.ifgr_groups);
793 if (match && !nomatch)
795 if (!match && nomatch)
797 return (matched && nomatched);
800 static struct afswtch *afs = NULL;
803 af_register(struct afswtch *p)
809 static struct afswtch *
810 af_getbyname(const char *name)
814 for (afp = afs; afp != NULL; afp = afp->af_next)
815 if (strcmp(afp->af_name, name) == 0)
820 static struct afswtch *
821 af_getbyfamily(int af)
825 for (afp = afs; afp != NULL; afp = afp->af_next)
826 if (afp->af_af == af)
832 af_other_status(int s)
835 uint8_t afmask[howmany(AF_MAX, NBBY)];
837 memset(afmask, 0, sizeof(afmask));
838 for (afp = afs; afp != NULL; afp = afp->af_next) {
839 if (afp->af_other_status == NULL)
841 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
843 afp->af_other_status(s);
844 setbit(afmask, afp->af_af);
849 af_all_tunnel_status(int s)
852 uint8_t afmask[howmany(AF_MAX, NBBY)];
854 memset(afmask, 0, sizeof(afmask));
855 for (afp = afs; afp != NULL; afp = afp->af_next) {
856 if (afp->af_status_tunnel == NULL)
858 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
860 afp->af_status_tunnel(s);
861 setbit(afmask, afp->af_af);
865 static struct cmd *cmds = NULL;
868 cmd_register(struct cmd *p)
874 static const struct cmd *
875 cmd_lookup(const char *name, int iscreate)
879 for (p = cmds; p != NULL; p = p->c_next)
880 if (strcmp(name, p->c_name) == 0) {
893 callback_func *cb_func;
895 struct callback *cb_next;
897 static struct callback *callbacks = NULL;
900 callback_register(callback_func *func, void *arg)
904 cb = malloc(sizeof(struct callback));
906 errx(1, "unable to allocate memory for callback");
909 cb->cb_next = callbacks;
913 /* specially-handled commands */
914 static void setifaddr(const char *, int, int, const struct afswtch *);
915 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
917 static void setifdstaddr(const char *, int, int, const struct afswtch *);
918 static const struct cmd setifdstaddr_cmd =
919 DEF_CMD("ifdstaddr", 0, setifdstaddr);
922 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
924 const struct afswtch *afp, *nafp;
929 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
934 * This is the historical "accident" allowing users to configure IPv4
935 * addresses without the "inet" keyword which while a nice feature has
936 * proven to complicate other things. We cannot remove this but only
937 * make sure we will never have a similar implicit default for IPv6 or
938 * any other address familiy. We need a fallback though for
939 * ifconfig IF up/down etc. to work without INET support as people
940 * never used ifconfig IF link up/down, etc. either.
944 if (afp == NULL && feature_present("inet"))
945 afp = af_getbyname("inet");
949 afp = af_getbyname("link");
951 warnx("Please specify an address_family.");
955 ifr.ifr_addr.sa_family =
956 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
957 AF_LOCAL : afp->af_af;
959 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
960 (uafp != NULL || errno != EAFNOSUPPORT ||
961 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
962 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
965 p = cmd_lookup(*argv, iscreate);
966 if (iscreate && p == NULL) {
968 * Push the clone create callback so the new
969 * device is created and can be used for any
970 * remaining arguments.
974 errx(1, "internal error, no callback");
975 callbacks = cb->cb_next;
976 cb->cb_func(s, cb->cb_arg);
979 * Handle any address family spec that
980 * immediately follows and potentially
981 * recreate the socket.
983 nafp = af_getbyname(*argv);
993 * Look for a normal parameter.
999 * Not a recognized command, choose between setting
1000 * the interface address and the dst address.
1002 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
1004 if (p->c_parameter == NEXTARG && p->c_u.c_func) {
1005 if (argv[1] == NULL)
1006 errx(1, "'%s' requires argument",
1008 p->c_u.c_func(argv[1], 0, s, afp);
1010 } else if (p->c_parameter == OPTARG && p->c_u.c_func) {
1011 p->c_u.c_func(argv[1], 0, s, afp);
1012 if (argv[1] != NULL)
1014 } else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) {
1016 errx(1, "'%s' requires 2 arguments",
1018 p->c_u.c_func2(argv[1], argv[2], s, afp);
1019 argc -= 2, argv += 2;
1020 } else if (p->c_u.c_func)
1021 p->c_u.c_func(*argv, p->c_parameter, s, afp);
1026 * Do any post argument processing required by the address family.
1028 if (afp->af_postproc != NULL)
1029 afp->af_postproc(s, afp, newaddr, getifflags(name, s, true));
1031 * Do deferred callbacks registered while processing
1032 * command-line arguments.
1034 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
1035 cb->cb_func(s, cb->cb_arg);
1037 * Do deferred operations.
1040 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
1041 warnx("interface %s cannot change %s addresses!",
1042 name, afp->af_name);
1048 strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
1049 sizeof ifr.ifr_name);
1050 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
1052 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
1053 /* means no previous address for interface */
1055 Perror("ioctl (SIOCDIFADDR)");
1059 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
1060 warnx("interface %s cannot change %s addresses!",
1061 name, afp->af_name);
1065 if (newaddr && (setaddr || setmask)) {
1066 strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
1067 sizeof ifr.ifr_name);
1068 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
1069 Perror("ioctl (SIOCAIFADDR)");
1078 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
1080 if (afp->af_getaddr == NULL)
1083 * Delay the ioctl to set the interface addr until flags are all set.
1084 * The address interpretation may depend on the flags,
1085 * and the flags may change when the address is set.
1088 if (doalias == 0 && afp->af_af != AF_LINK)
1090 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
1094 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
1096 struct addrinfo *srcres, *dstres;
1099 if (afp->af_settunnel == NULL) {
1100 warn("address family %s does not support tunnel setup",
1105 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
1106 errx(1, "error in parsing address string: %s",
1107 gai_strerror(ecode));
1109 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
1110 errx(1, "error in parsing address string: %s",
1111 gai_strerror(ecode));
1113 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
1115 "source and destination address families do not match");
1117 afp->af_settunnel(s, srcres, dstres);
1119 freeaddrinfo(srcres);
1120 freeaddrinfo(dstres);
1125 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
1128 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
1129 err(1, "SIOCDIFPHYADDR");
1134 setifvnet(const char *jname, int dummy __unused, int s,
1135 const struct afswtch *afp)
1137 struct ifreq my_ifr;
1139 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1140 my_ifr.ifr_jid = jail_getid(jname);
1141 if (my_ifr.ifr_jid < 0)
1142 errx(1, "%s", jail_errmsg);
1143 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
1144 err(1, "SIOCSIFVNET");
1148 setifrvnet(const char *jname, int dummy __unused, int s,
1149 const struct afswtch *afp)
1151 struct ifreq my_ifr;
1153 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1154 my_ifr.ifr_jid = jail_getid(jname);
1155 if (my_ifr.ifr_jid < 0)
1156 errx(1, "%s", jail_errmsg);
1157 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
1158 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
1163 setifnetmask(const char *addr, int dummy __unused, int s,
1164 const struct afswtch *afp)
1166 if (afp->af_getaddr != NULL) {
1168 afp->af_getaddr(addr, MASK);
1173 setifbroadaddr(const char *addr, int dummy __unused, int s,
1174 const struct afswtch *afp)
1176 if (afp->af_getaddr != NULL)
1177 afp->af_getaddr(addr, DSTADDR);
1181 notealias(const char *addr, int param, int s, const struct afswtch *afp)
1183 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1184 if (setaddr && doalias == 0 && param < 0)
1185 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1186 bcopy((caddr_t)rqtosa(af_addreq),
1187 (caddr_t)rqtosa(af_ridreq),
1188 rqtosa(af_addreq)->sa_len);
1200 setifdstaddr(const char *addr, int param __unused, int s,
1201 const struct afswtch *afp)
1203 if (afp->af_getaddr != NULL)
1204 afp->af_getaddr(addr, DSTADDR);
1208 getifflags(const char *ifname, int us, bool err_ok)
1210 struct ifreq my_ifr;
1213 memset(&my_ifr, 0, sizeof(my_ifr));
1214 (void) strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name));
1216 if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
1217 err(1, "socket(family AF_LOCAL,SOCK_DGRAM");
1220 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1222 Perror("ioctl (SIOCGIFFLAGS)");
1228 return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16));
1232 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1233 * of the ifreq structure, which may confuse other parts of ifconfig.
1234 * Make a private copy so we can avoid that.
1237 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1239 struct ifreq my_ifr;
1242 flags = getifflags(name, s, false);
1248 memset(&my_ifr, 0, sizeof(my_ifr));
1249 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1250 my_ifr.ifr_flags = flags & 0xffff;
1251 my_ifr.ifr_flagshigh = flags >> 16;
1252 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1257 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1261 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1262 Perror("ioctl (SIOCGIFCAP)");
1265 flags = ifr.ifr_curcap;
1271 flags &= ifr.ifr_reqcap;
1272 /* Check for no change in capabilities. */
1273 if (ifr.ifr_curcap == flags)
1275 ifr.ifr_reqcap = flags;
1276 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1281 setifmetric(const char *val, int dummy __unused, int s,
1282 const struct afswtch *afp)
1284 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1285 ifr.ifr_metric = atoi(val);
1286 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1287 err(1, "ioctl SIOCSIFMETRIC (set metric)");
1291 setifmtu(const char *val, int dummy __unused, int s,
1292 const struct afswtch *afp)
1294 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1295 ifr.ifr_mtu = atoi(val);
1296 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1297 err(1, "ioctl SIOCSIFMTU (set mtu)");
1301 setifpcp(const char *val, int arg __unused, int s, const struct afswtch *afp)
1306 ul = strtoul(val, &endp, 0);
1308 errx(1, "invalid value for pcp");
1310 errx(1, "value for pcp out of range");
1311 ifr.ifr_lan_pcp = ul;
1312 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1313 err(1, "SIOCSLANPCP");
1317 disableifpcp(const char *val, int arg __unused, int s,
1318 const struct afswtch *afp)
1321 ifr.ifr_lan_pcp = IFNET_PCP_NONE;
1322 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1323 err(1, "SIOCSLANPCP");
1327 setifname(const char *val, int dummy __unused, int s,
1328 const struct afswtch *afp)
1332 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1334 newname = strdup(val);
1335 if (newname == NULL)
1336 err(1, "no memory to set ifname");
1337 ifr.ifr_data = newname;
1338 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1340 err(1, "ioctl SIOCSIFNAME (set name)");
1343 strlcpy(name, newname, sizeof(name));
1349 setifdescr(const char *val, int dummy __unused, int s,
1350 const struct afswtch *afp)
1354 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1356 ifr.ifr_buffer.length = strlen(val) + 1;
1357 if (ifr.ifr_buffer.length == 1) {
1358 ifr.ifr_buffer.buffer = newdescr = NULL;
1359 ifr.ifr_buffer.length = 0;
1361 newdescr = strdup(val);
1362 ifr.ifr_buffer.buffer = newdescr;
1363 if (newdescr == NULL) {
1364 warn("no memory to set ifdescr");
1369 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
1370 err(1, "ioctl SIOCSIFDESCR (set descr)");
1377 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
1380 setifdescr("", 0, s, 0);
1384 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \
1385 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1386 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
1389 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1390 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1391 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1392 "\26RXCSUM_IPV6\27TXCSUM_IPV6\31TXRTLMT\32HWRXTSTMP\33NOMAP\34TXTLS4\35TXTLS6" \
1393 "\36VXLAN_HWCSUM\37VXLAN_HWTSO\40TXTLS_RTLMT"
1396 * Print the status of the interface. If an address family was
1397 * specified, show only it; otherwise, show them all.
1400 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
1401 struct ifaddrs *ifa)
1403 struct ifaddrs *ift;
1409 ifr.ifr_addr.sa_family = AF_LOCAL;
1412 ifr.ifr_addr.sa_family =
1413 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
1415 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1417 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1419 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1421 printf("%s: ", name);
1422 printb("flags", ifa->ifa_flags, IFFBITS);
1423 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1424 printf(" metric %d", ifr.ifr_metric);
1425 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1426 printf(" mtu %d", ifr.ifr_mtu);
1430 if ((descr = reallocf(descr, descrlen)) != NULL) {
1431 ifr.ifr_buffer.buffer = descr;
1432 ifr.ifr_buffer.length = descrlen;
1433 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1434 if (ifr.ifr_buffer.buffer == descr) {
1435 if (strlen(descr) > 0)
1436 printf("\tdescription: %s\n",
1438 } else if (ifr.ifr_buffer.length > descrlen) {
1439 descrlen = ifr.ifr_buffer.length;
1444 warn("unable to allocate memory for interface"
1449 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1450 if (ifr.ifr_curcap != 0) {
1451 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1454 if (supmedia && ifr.ifr_reqcap != 0) {
1455 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1462 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1463 if (ift->ifa_addr == NULL)
1465 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1468 const struct afswtch *p;
1469 p = af_getbyfamily(ift->ifa_addr->sa_family);
1470 if (p != NULL && p->af_status != NULL)
1471 p->af_status(s, ift);
1472 } else if (afp->af_af == ift->ifa_addr->sa_family)
1473 afp->af_status(s, ift);
1476 if (allfamilies || afp->af_af == AF_LINK) {
1477 const struct afswtch *lafp;
1480 * Hack; the link level address is received separately
1481 * from the routing information so any address is not
1482 * handled above. Cobble together an entry and invoke
1483 * the status method specially.
1485 lafp = af_getbyname("lladdr");
1487 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1488 lafp->af_status(s, &info);
1494 else if (afp->af_other_status != NULL)
1495 afp->af_other_status(s);
1497 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1498 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1499 printf("%s", ifs.ascii);
1502 sfp_status(s, &ifr, verbose);
1509 tunnel_status(int s)
1511 af_all_tunnel_status(s);
1515 Perror(const char *cmd)
1520 errx(1, "%s: no such interface", cmd);
1524 errx(1, "%s: permission denied", cmd);
1533 * Print a value a la the %b format of the kernel's printf
1536 printb(const char *s, unsigned v, const char *bits)
1541 if (bits && *bits == 8)
1542 printf("%s=%o", s, v);
1544 printf("%s=%x", s, v);
1548 while ((i = *bits++) != '\0') {
1549 if (v & (1u << (i-1))) {
1553 for (; (c = *bits) > 32; bits++)
1556 for (; *bits > 32; bits++)
1564 print_vhid(const struct ifaddrs *ifa, const char *s)
1566 struct if_data *ifd;
1568 if (ifa->ifa_data == NULL)
1571 ifd = ifa->ifa_data;
1572 if (ifd->ifi_vhid == 0)
1575 printf(" vhid %d", ifd->ifi_vhid);
1579 ifmaybeload(const char *name)
1581 #define MOD_PREFIX_LEN 3 /* "if_" */
1582 struct module_stat mstat;
1583 int i, fileid, modid;
1584 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1586 struct module_map_entry *mme;
1589 /* loading suppressed by the user */
1593 /* trim the interface number off the end */
1594 strlcpy(ifname, name, sizeof(ifname));
1595 dp = ifname + strlen(ifname) - 1;
1596 for (; dp > ifname; dp--) {
1603 /* Either derive it from the map or guess otherwise */
1606 for (i = 0; i < nitems(module_map); ++i) {
1607 mme = &module_map[i];
1608 if (strcmp(mme->ifname, ifname) == 0) {
1609 strlcpy(ifkind, mme->kldname, sizeof(ifkind));
1615 /* We didn't have an alias for it... we'll guess. */
1617 /* turn interface and unit into module name */
1618 strlcpy(ifkind, "if_", sizeof(ifkind));
1619 strlcat(ifkind, ifname, sizeof(ifkind));
1622 /* scan files in kernel */
1623 mstat.version = sizeof(struct module_stat);
1624 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1625 /* scan modules in file */
1626 for (modid = kldfirstmod(fileid); modid > 0;
1627 modid = modfnext(modid)) {
1628 if (modstat(modid, &mstat) < 0)
1630 /* strip bus name if present */
1631 if ((cp = strchr(mstat.name, '/')) != NULL) {
1637 * Is it already loaded? Don't compare with ifname if
1638 * we were specifically told which kld to use. Doing
1639 * so could lead to conflicts not trivially solved.
1641 if ((!found && strcmp(ifname, cp) == 0) ||
1642 strcmp(ifkind, cp) == 0)
1648 * Try to load the module. But ignore failures, because ifconfig can't
1649 * infer the names of all drivers (eg mlx4en(4)).
1651 (void) kldload(ifkind);
1654 static struct cmd basic_cmds[] = {
1655 DEF_CMD("up", IFF_UP, setifflags),
1656 DEF_CMD("down", -IFF_UP, setifflags),
1657 DEF_CMD("arp", -IFF_NOARP, setifflags),
1658 DEF_CMD("-arp", IFF_NOARP, setifflags),
1659 DEF_CMD("debug", IFF_DEBUG, setifflags),
1660 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1661 DEF_CMD_ARG("description", setifdescr),
1662 DEF_CMD_ARG("descr", setifdescr),
1663 DEF_CMD("-description", 0, unsetifdescr),
1664 DEF_CMD("-descr", 0, unsetifdescr),
1665 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1666 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1667 DEF_CMD("add", IFF_UP, notealias),
1668 DEF_CMD("alias", IFF_UP, notealias),
1669 DEF_CMD("-alias", -IFF_UP, notealias),
1670 DEF_CMD("delete", -IFF_UP, notealias),
1671 DEF_CMD("remove", -IFF_UP, notealias),
1673 #define EN_SWABIPS 0x1000
1674 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1675 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1677 DEF_CMD_ARG("netmask", setifnetmask),
1678 DEF_CMD_ARG("metric", setifmetric),
1679 DEF_CMD_ARG("broadcast", setifbroadaddr),
1680 DEF_CMD_ARG2("tunnel", settunnel),
1681 DEF_CMD("-tunnel", 0, deletetunnel),
1682 DEF_CMD("deletetunnel", 0, deletetunnel),
1684 DEF_CMD_ARG("vnet", setifvnet),
1685 DEF_CMD_ARG("-vnet", setifrvnet),
1687 DEF_CMD("link0", IFF_LINK0, setifflags),
1688 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1689 DEF_CMD("link1", IFF_LINK1, setifflags),
1690 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1691 DEF_CMD("link2", IFF_LINK2, setifflags),
1692 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1693 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1694 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1695 DEF_CMD("mextpg", IFCAP_MEXTPG, setifcap),
1696 DEF_CMD("-mextpg", -IFCAP_MEXTPG, setifcap),
1697 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1698 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1699 DEF_CMD("rxcsum6", IFCAP_RXCSUM_IPV6, setifcap),
1700 DEF_CMD("-rxcsum6", -IFCAP_RXCSUM_IPV6, setifcap),
1701 DEF_CMD("txcsum6", IFCAP_TXCSUM_IPV6, setifcap),
1702 DEF_CMD("-txcsum6", -IFCAP_TXCSUM_IPV6, setifcap),
1703 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1704 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1705 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1706 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1707 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1708 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1709 DEF_CMD_ARG("pcp", setifpcp),
1710 DEF_CMD("-pcp", 0, disableifpcp),
1711 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1712 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1713 DEF_CMD("tso6", IFCAP_TSO6, setifcap),
1714 DEF_CMD("-tso6", -IFCAP_TSO6, setifcap),
1715 DEF_CMD("tso4", IFCAP_TSO4, setifcap),
1716 DEF_CMD("-tso4", -IFCAP_TSO4, setifcap),
1717 DEF_CMD("tso", IFCAP_TSO, setifcap),
1718 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1719 DEF_CMD("toe", IFCAP_TOE, setifcap),
1720 DEF_CMD("-toe", -IFCAP_TOE, setifcap),
1721 DEF_CMD("lro", IFCAP_LRO, setifcap),
1722 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1723 DEF_CMD("txtls", IFCAP_TXTLS, setifcap),
1724 DEF_CMD("-txtls", -IFCAP_TXTLS, setifcap),
1725 DEF_CMD("wol", IFCAP_WOL, setifcap),
1726 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1727 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1728 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1729 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1730 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1731 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1732 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1733 DEF_CMD("txrtlmt", IFCAP_TXRTLMT, setifcap),
1734 DEF_CMD("-txrtlmt", -IFCAP_TXRTLMT, setifcap),
1735 DEF_CMD("txtlsrtlmt", IFCAP_TXTLS_RTLMT, setifcap),
1736 DEF_CMD("-txtlsrtlmt", -IFCAP_TXTLS_RTLMT, setifcap),
1737 DEF_CMD("hwrxtstmp", IFCAP_HWRXTSTMP, setifcap),
1738 DEF_CMD("-hwrxtstmp", -IFCAP_HWRXTSTMP, setifcap),
1739 DEF_CMD("normal", -IFF_LINK0, setifflags),
1740 DEF_CMD("compress", IFF_LINK0, setifflags),
1741 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1742 DEF_CMD_ARG("mtu", setifmtu),
1743 DEF_CMD_ARG("name", setifname),
1746 static __constructor void
1751 for (i = 0; i < nitems(basic_cmds); i++)
1752 cmd_register(&basic_cmds[i]);