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";
39 static const char rcsid[] =
43 #include <sys/param.h>
44 #include <sys/ioctl.h>
45 #include <sys/module.h>
46 #include <sys/linker.h>
47 #include <sys/queue.h>
48 #include <sys/socket.h>
51 #include <net/ethernet.h>
53 #include <net/if_dl.h>
54 #include <net/if_types.h>
55 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <arpa/inet.h>
78 #include <libifconfig.h>
82 ifconfig_handle_t *lifh;
85 * Since "struct ifreq" is composed of various union members, callers
86 * should pay special attention to interpret the value.
87 * (.e.g. little/big endian difference in the structure.)
104 int printkeys = 0; /* Print keying material for interfaces. */
107 /* Formatter Strings */
108 char *f_inet, *f_inet6, *f_ether, *f_addr;
110 static bool group_member(const char *ifname, const char *match,
111 const char *nomatch);
112 static int ifconfig(int argc, char *const *argv, int iscreate,
113 const struct afswtch *afp);
114 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
115 struct ifaddrs *ifa);
116 static void tunnel_status(int s);
117 static _Noreturn void usage(void);
119 static int getifflags(const char *ifname, int us);
121 static struct afswtch *af_getbyname(const char *name);
122 static struct afswtch *af_getbyfamily(int af);
123 static void af_other_status(int);
125 void printifnamemaybe(void);
127 static struct option *opts = NULL;
129 struct ifa_order_elt {
133 TAILQ_ENTRY(ifa_order_elt) link;
136 TAILQ_HEAD(ifa_queue, ifa_order_elt);
138 static struct module_map_entry {
144 .kldname = "if_tuntap",
148 .kldname = "if_tuntap",
152 .kldname = "if_tuntap",
160 * This mapping exists because there is a conflicting enc module
161 * in CAM. ifconfig's guessing behavior will attempt to match
162 * the ifname to a module as well as if_${ifname} and clash with
163 * CAM enc. This is an assertion of the correct module to load.
172 opt_register(struct option *p)
184 /* XXX not right but close enough for now */
186 for (p = opts; p != NULL; p = p->next) {
187 strlcat(options, p->opt_usage, sizeof(options));
188 strlcat(options, " ", sizeof(options));
192 "usage: ifconfig [-f type:format] %sinterface address_family\n"
193 " [address [dest_address]] [parameters]\n"
194 " ifconfig interface create\n"
195 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
196 " ifconfig -l [-d] [-u] [address_family]\n"
197 " ifconfig %s[-d] [-m] [-u] [-v]\n",
198 options, options, options);
203 ioctl_ifcreate(int s, struct ifreq *ifr)
205 if (ioctl(s, SIOCIFCREATE2, ifr) < 0) {
208 errx(1, "interface %s already exists", ifr->ifr_name);
210 err(1, "SIOCIFCREATE2");
216 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
218 struct ifaddrs *prev;
219 struct ifa_order_elt *cur;
220 unsigned int ord, af, ifa_ord;
227 while (ifa != NULL) {
229 strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
230 cur = calloc(1, sizeof(*cur));
235 TAILQ_INSERT_TAIL(q, cur, link);
236 cur->if_order = ifa_ord ++;
242 af = ifa->ifa_addr->sa_family;
244 if (af < nitems(cur->af_orders) &&
245 cur->af_orders[af] == 0)
246 cur->af_orders[af] = ++ord;
256 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
258 struct ifa_order_elt *cur, *e1, *e2;
259 unsigned int af1, af2;
264 ret = strcmp(a->ifa_name, b->ifa_name);
266 TAILQ_FOREACH(cur, q, link) {
270 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
272 else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
279 return (e1->if_order - e2->if_order);
281 } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
282 TAILQ_FOREACH(cur, q, link) {
283 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
292 af1 = a->ifa_addr->sa_family;
293 af2 = b->ifa_addr->sa_family;
295 if (af1 < nitems(e1->af_orders) && af2 < nitems(e1->af_orders))
296 return (e1->af_orders[af1] - e1->af_orders[af2]);
302 static void freeformat(void)
315 static void setformat(char *input)
317 char *formatstr, *category, *modifier;
319 formatstr = strdup(input);
320 while ((category = strsep(&formatstr, ",")) != NULL) {
321 modifier = strchr(category, ':');
322 if (modifier == NULL || modifier[1] == '\0') {
323 warnx("Skipping invalid format specification: %s\n",
328 /* Split the string on the separator, then seek past it */
332 if (strcmp(category, "addr") == 0)
333 f_addr = strdup(modifier);
334 else if (strcmp(category, "ether") == 0)
335 f_ether = strdup(modifier);
336 else if (strcmp(category, "inet") == 0)
337 f_inet = strdup(modifier);
338 else if (strcmp(category, "inet6") == 0)
339 f_inet6 = strdup(modifier);
344 static struct ifaddrs *
345 sortifaddrs(struct ifaddrs *list,
346 int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *),
349 struct ifaddrs *right, *temp, *last, *result, *next, *tail;
358 if (!list || !list->ifa_next)
361 while (temp && temp->ifa_next) {
363 right = right->ifa_next;
364 temp = temp->ifa_next->ifa_next;
367 last->ifa_next = NULL;
369 list = sortifaddrs(list, compare, q);
370 right = sortifaddrs(right, compare, q);
372 while (list || right) {
376 list = list->ifa_next;
379 right = right->ifa_next;
380 } else if (compare(list, right, q) <= 0) {
382 list = list->ifa_next;
385 right = right->ifa_next;
391 tail->ifa_next = next;
399 void printifnamemaybe()
402 printf("%s\n", name);
406 main(int argc, char *argv[])
408 int c, all, namesonly, downonly, uponly;
409 const struct afswtch *afp = NULL;
411 struct ifaddrs *ifap, *sifap, *ifa;
413 const struct sockaddr_dl *sdl;
414 char options[1024], *cp, *envformat, *namecp = NULL;
415 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
416 struct ifa_order_elt *cur, *tmp;
417 const char *ifname, *matchgroup, *nogroup;
422 all = downonly = uponly = namesonly = noload = verbose = 0;
423 f_inet = f_inet6 = f_ether = f_addr = NULL;
424 matchgroup = nogroup = NULL;
426 lifh = ifconfig_open();
428 err(EXIT_FAILURE, "ifconfig_open");
430 envformat = getenv("IFCONFIG_FORMAT");
431 if (envformat != NULL)
432 setformat(envformat);
435 * Ensure we print interface name when expected to,
436 * even if we terminate early due to error.
438 atexit(printifnamemaybe);
440 /* Parse leading line options */
441 strlcpy(options, "G:adf:klmnuv", sizeof(options));
442 for (p = opts; p != NULL; p = p->next)
443 strlcat(options, p->opt, sizeof(options));
444 while ((c = getopt(argc, argv, options)) != -1) {
446 case 'a': /* scan all interfaces */
449 case 'd': /* restrict scan to "down" interfaces */
458 if (optarg == NULL || all == 0)
465 case 'l': /* scan interface names only */
468 case 'm': /* show media choices in status */
471 case 'n': /* suppress module loading */
474 case 'u': /* restrict scan to "up" interfaces */
489 for (p = opts; p != NULL; p = p->next)
490 if (p->opt[0] == c) {
502 /* -l cannot be used with -a or -m */
503 if (namesonly && (all || supmedia))
507 if (uponly && downonly)
510 /* no arguments is equivalent to '-a' */
511 if (!namesonly && argc < 1)
514 /* -a and -l allow an address family arg to limit the output */
515 if (all || namesonly) {
522 afp = af_getbyname(*argv);
524 warnx("Address family '%s' unknown.", *argv);
527 if (afp->af_name != NULL)
529 /* leave with afp non-zero */
532 /* not listing, need an argument */
539 /* check and maybe load support for this interface */
542 ifindex = if_nametoindex(ifname);
545 * NOTE: We must special-case the `create' command
546 * right here as we would otherwise fail when trying
547 * to find the interface.
549 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
550 strcmp(argv[0], "plumb") == 0)) {
551 iflen = strlcpy(name, ifname, sizeof(name));
552 if (iflen >= sizeof(name))
553 errx(1, "%s: cloning name too long",
555 ifconfig(argc, argv, 1, NULL);
560 * NOTE: We have to special-case the `-vnet' command
561 * right here as we would otherwise fail when trying
562 * to find the interface as it lives in another vnet.
564 if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
565 iflen = strlcpy(name, ifname, sizeof(name));
566 if (iflen >= sizeof(name))
567 errx(1, "%s: interface name too long",
569 ifconfig(argc, argv, 0, NULL);
573 errx(1, "interface %s does not exist", ifname);
576 * Do not allow use `create` command as hostname if
577 * address family is not specified.
579 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
580 strcmp(argv[0], "plumb") == 0)) {
582 errx(1, "interface %s already exists",
589 /* Check for address family */
591 afp = af_getbyname(*argv);
597 * Check for a requested configuration action on a single interface,
598 * which doesn't require building, sorting, and searching the entire
599 * system address list
601 if ((argc > 0) && (ifname != NULL)) {
602 iflen = strlcpy(name, ifname, sizeof(name));
603 if (iflen >= sizeof(name)) {
604 warnx("%s: interface name too long, skipping", ifname);
606 flags = getifflags(name, -1);
607 if (!(((flags & IFF_CANTCONFIG) != 0) ||
608 (downonly && (flags & IFF_UP) != 0) ||
609 (uponly && (flags & IFF_UP) == 0)))
610 ifconfig(argc, argv, 0, afp);
615 if (getifaddrs(&ifap) != 0)
616 err(EXIT_FAILURE, "getifaddrs");
620 if (calcorders(ifap, &q) != 0)
621 err(EXIT_FAILURE, "calcorders");
623 sifap = sortifaddrs(ifap, cmpifaddrs, &q);
625 TAILQ_FOREACH_SAFE(cur, &q, link, tmp)
629 for (ifa = sifap; ifa; ifa = ifa->ifa_next) {
630 memset(&paifr, 0, sizeof(paifr));
631 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
632 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
633 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
634 ifa->ifa_addr->sa_len);
637 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
639 if (ifa->ifa_addr->sa_family == AF_LINK)
640 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
643 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
645 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
646 if (iflen >= sizeof(name)) {
647 warnx("%s: interface name too long, skipping",
653 if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
655 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
657 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
659 if (!group_member(ifa->ifa_name, matchgroup, nogroup))
662 * Are we just listing the interfaces?
668 /* special case for "ether" address family */
669 if (!strcmp(afp->af_name, "ether")) {
671 (sdl->sdl_type != IFT_ETHER &&
672 sdl->sdl_type != IFT_L2VLAN &&
673 sdl->sdl_type != IFT_BRIDGE) ||
674 sdl->sdl_alen != ETHER_ADDR_LEN)
677 if (ifa->ifa_addr->sa_family
692 ifconfig(argc, argv, 0, afp);
694 status(afp, sdl, ifa);
702 ifconfig_close(lifh);
707 * Returns true if an interface should be listed because any its groups
708 * matches shell pattern "match" and none of groups matches pattern "nomatch".
709 * If any pattern is NULL, corresponding condition is skipped.
712 group_member(const char *ifname, const char *match, const char *nomatch)
714 static int sock = -1;
716 struct ifgroupreq ifgr;
719 bool matched, nomatched;
722 if (match == NULL && nomatch == NULL)
727 memset(&ifgr, 0, sizeof(ifgr));
728 strlcpy(ifgr.ifgr_name, ifname, IFNAMSIZ);
730 /* The socket is opened once. Let _exit() close it. */
732 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
734 errx(1, "%s: socket(AF_LOCAL,SOCK_DGRAM)", __func__);
737 /* Determine amount of memory for the list of groups. */
738 if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
739 if (errno == EINVAL || errno == ENOTTY)
742 errx(1, "%s: SIOCGIFGROUP", __func__);
745 /* Obtain the list of groups. */
748 (struct ifg_req *)calloc(len / sizeof(*ifg), sizeof(*ifg));
750 if (ifgr.ifgr_groups == NULL)
751 errx(1, "%s: no memory", __func__);
752 if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
753 errx(1, "%s: SIOCGIFGROUP", __func__);
755 /* Perform matching. */
758 for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(*ifg); ifg++) {
759 len -= sizeof(struct ifg_req);
761 matched |= !fnmatch(match, ifg->ifgrq_group, 0);
763 nomatched &= fnmatch(nomatch, ifg->ifgrq_group, 0);
765 free(ifgr.ifgr_groups);
767 if (match && !nomatch)
769 if (!match && nomatch)
771 return (matched && nomatched);
774 static struct afswtch *afs = NULL;
777 af_register(struct afswtch *p)
783 static struct afswtch *
784 af_getbyname(const char *name)
788 for (afp = afs; afp != NULL; afp = afp->af_next)
789 if (strcmp(afp->af_name, name) == 0)
794 static struct afswtch *
795 af_getbyfamily(int af)
799 for (afp = afs; afp != NULL; afp = afp->af_next)
800 if (afp->af_af == af)
806 af_other_status(int s)
809 uint8_t afmask[howmany(AF_MAX, NBBY)];
811 memset(afmask, 0, sizeof(afmask));
812 for (afp = afs; afp != NULL; afp = afp->af_next) {
813 if (afp->af_other_status == NULL)
815 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
817 afp->af_other_status(s);
818 setbit(afmask, afp->af_af);
823 af_all_tunnel_status(int s)
826 uint8_t afmask[howmany(AF_MAX, NBBY)];
828 memset(afmask, 0, sizeof(afmask));
829 for (afp = afs; afp != NULL; afp = afp->af_next) {
830 if (afp->af_status_tunnel == NULL)
832 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
834 afp->af_status_tunnel(s);
835 setbit(afmask, afp->af_af);
839 static struct cmd *cmds = NULL;
842 cmd_register(struct cmd *p)
848 static const struct cmd *
849 cmd_lookup(const char *name, int iscreate)
853 for (p = cmds; p != NULL; p = p->c_next)
854 if (strcmp(name, p->c_name) == 0) {
867 callback_func *cb_func;
869 struct callback *cb_next;
871 static struct callback *callbacks = NULL;
874 callback_register(callback_func *func, void *arg)
878 cb = malloc(sizeof(struct callback));
880 errx(1, "unable to allocate memory for callback");
883 cb->cb_next = callbacks;
887 /* specially-handled commands */
888 static void setifaddr(const char *, int, int, const struct afswtch *);
889 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
891 static void setifdstaddr(const char *, int, int, const struct afswtch *);
892 static const struct cmd setifdstaddr_cmd =
893 DEF_CMD("ifdstaddr", 0, setifdstaddr);
896 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
898 const struct afswtch *afp, *nafp;
903 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
908 * This is the historical "accident" allowing users to configure IPv4
909 * addresses without the "inet" keyword which while a nice feature has
910 * proven to complicate other things. We cannot remove this but only
911 * make sure we will never have a similar implicit default for IPv6 or
912 * any other address familiy. We need a fallback though for
913 * ifconfig IF up/down etc. to work without INET support as people
914 * never used ifconfig IF link up/down, etc. either.
918 if (afp == NULL && feature_present("inet"))
919 afp = af_getbyname("inet");
923 afp = af_getbyname("link");
925 warnx("Please specify an address_family.");
929 ifr.ifr_addr.sa_family =
930 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
931 AF_LOCAL : afp->af_af;
933 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
934 (uafp != NULL || errno != EAFNOSUPPORT ||
935 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
936 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
939 p = cmd_lookup(*argv, iscreate);
940 if (iscreate && p == NULL) {
942 * Push the clone create callback so the new
943 * device is created and can be used for any
944 * remaining arguments.
948 errx(1, "internal error, no callback");
949 callbacks = cb->cb_next;
950 cb->cb_func(s, cb->cb_arg);
953 * Handle any address family spec that
954 * immediately follows and potentially
955 * recreate the socket.
957 nafp = af_getbyname(*argv);
967 * Look for a normal parameter.
973 * Not a recognized command, choose between setting
974 * the interface address and the dst address.
976 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
978 if (p->c_parameter == NEXTARG && p->c_u.c_func) {
980 errx(1, "'%s' requires argument",
982 p->c_u.c_func(argv[1], 0, s, afp);
984 } else if (p->c_parameter == OPTARG && p->c_u.c_func) {
985 p->c_u.c_func(argv[1], 0, s, afp);
988 } else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) {
990 errx(1, "'%s' requires 2 arguments",
992 p->c_u.c_func2(argv[1], argv[2], s, afp);
993 argc -= 2, argv += 2;
994 } else if (p->c_u.c_func)
995 p->c_u.c_func(*argv, p->c_parameter, s, afp);
1000 * Do any post argument processing required by the address family.
1002 if (afp->af_postproc != NULL)
1003 afp->af_postproc(s, afp);
1005 * Do deferred callbacks registered while processing
1006 * command-line arguments.
1008 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
1009 cb->cb_func(s, cb->cb_arg);
1011 * Do deferred operations.
1014 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
1015 warnx("interface %s cannot change %s addresses!",
1016 name, afp->af_name);
1022 strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
1023 sizeof ifr.ifr_name);
1024 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
1026 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
1027 /* means no previous address for interface */
1029 Perror("ioctl (SIOCDIFADDR)");
1033 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
1034 warnx("interface %s cannot change %s addresses!",
1035 name, afp->af_name);
1039 if (newaddr && (setaddr || setmask)) {
1040 strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
1041 sizeof ifr.ifr_name);
1042 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
1043 Perror("ioctl (SIOCAIFADDR)");
1052 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
1054 if (afp->af_getaddr == NULL)
1057 * Delay the ioctl to set the interface addr until flags are all set.
1058 * The address interpretation may depend on the flags,
1059 * and the flags may change when the address is set.
1062 if (doalias == 0 && afp->af_af != AF_LINK)
1064 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
1068 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
1070 struct addrinfo *srcres, *dstres;
1073 if (afp->af_settunnel == NULL) {
1074 warn("address family %s does not support tunnel setup",
1079 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
1080 errx(1, "error in parsing address string: %s",
1081 gai_strerror(ecode));
1083 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
1084 errx(1, "error in parsing address string: %s",
1085 gai_strerror(ecode));
1087 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
1089 "source and destination address families do not match");
1091 afp->af_settunnel(s, srcres, dstres);
1093 freeaddrinfo(srcres);
1094 freeaddrinfo(dstres);
1099 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
1102 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
1103 err(1, "SIOCDIFPHYADDR");
1108 setifvnet(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, SIOCSIFVNET, &my_ifr) < 0)
1118 err(1, "SIOCSIFVNET");
1122 setifrvnet(const char *jname, int dummy __unused, int s,
1123 const struct afswtch *afp)
1125 struct ifreq my_ifr;
1127 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
1128 my_ifr.ifr_jid = jail_getid(jname);
1129 if (my_ifr.ifr_jid < 0)
1130 errx(1, "%s", jail_errmsg);
1131 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
1132 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
1137 setifnetmask(const char *addr, int dummy __unused, int s,
1138 const struct afswtch *afp)
1140 if (afp->af_getaddr != NULL) {
1142 afp->af_getaddr(addr, MASK);
1147 setifbroadaddr(const char *addr, int dummy __unused, int s,
1148 const struct afswtch *afp)
1150 if (afp->af_getaddr != NULL)
1151 afp->af_getaddr(addr, DSTADDR);
1155 notealias(const char *addr, int param, int s, const struct afswtch *afp)
1157 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1158 if (setaddr && doalias == 0 && param < 0)
1159 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1160 bcopy((caddr_t)rqtosa(af_addreq),
1161 (caddr_t)rqtosa(af_ridreq),
1162 rqtosa(af_addreq)->sa_len);
1174 setifdstaddr(const char *addr, int param __unused, int s,
1175 const struct afswtch *afp)
1177 if (afp->af_getaddr != NULL)
1178 afp->af_getaddr(addr, DSTADDR);
1182 getifflags(const char *ifname, int us)
1184 struct ifreq my_ifr;
1187 memset(&my_ifr, 0, sizeof(my_ifr));
1188 (void) strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name));
1190 if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
1191 err(1, "socket(family AF_LOCAL,SOCK_DGRAM");
1194 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1195 Perror("ioctl (SIOCGIFFLAGS)");
1200 return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16));
1204 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1205 * of the ifreq structure, which may confuse other parts of ifconfig.
1206 * Make a private copy so we can avoid that.
1209 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1211 struct ifreq my_ifr;
1214 flags = getifflags(name, s);
1220 memset(&my_ifr, 0, sizeof(my_ifr));
1221 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1222 my_ifr.ifr_flags = flags & 0xffff;
1223 my_ifr.ifr_flagshigh = flags >> 16;
1224 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1229 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1233 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1234 Perror("ioctl (SIOCGIFCAP)");
1237 flags = ifr.ifr_curcap;
1243 flags &= ifr.ifr_reqcap;
1244 ifr.ifr_reqcap = flags;
1245 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1250 setifmetric(const char *val, int dummy __unused, int s,
1251 const struct afswtch *afp)
1253 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1254 ifr.ifr_metric = atoi(val);
1255 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1256 err(1, "ioctl SIOCSIFMETRIC (set metric)");
1260 setifmtu(const char *val, int dummy __unused, int s,
1261 const struct afswtch *afp)
1263 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1264 ifr.ifr_mtu = atoi(val);
1265 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1266 err(1, "ioctl SIOCSIFMTU (set mtu)");
1270 setifpcp(const char *val, int arg __unused, int s, const struct afswtch *afp)
1275 ul = strtoul(val, &endp, 0);
1277 errx(1, "invalid value for pcp");
1279 errx(1, "value for pcp out of range");
1280 ifr.ifr_lan_pcp = ul;
1281 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1282 err(1, "SIOCSLANPCP");
1286 disableifpcp(const char *val, int arg __unused, int s,
1287 const struct afswtch *afp)
1290 ifr.ifr_lan_pcp = IFNET_PCP_NONE;
1291 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1292 err(1, "SIOCSLANPCP");
1296 setifname(const char *val, int dummy __unused, int s,
1297 const struct afswtch *afp)
1301 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1303 newname = strdup(val);
1304 if (newname == NULL)
1305 err(1, "no memory to set ifname");
1306 ifr.ifr_data = newname;
1307 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1309 err(1, "ioctl SIOCSIFNAME (set name)");
1312 strlcpy(name, newname, sizeof(name));
1318 setifdescr(const char *val, int dummy __unused, int s,
1319 const struct afswtch *afp)
1323 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1325 ifr.ifr_buffer.length = strlen(val) + 1;
1326 if (ifr.ifr_buffer.length == 1) {
1327 ifr.ifr_buffer.buffer = newdescr = NULL;
1328 ifr.ifr_buffer.length = 0;
1330 newdescr = strdup(val);
1331 ifr.ifr_buffer.buffer = newdescr;
1332 if (newdescr == NULL) {
1333 warn("no memory to set ifdescr");
1338 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
1339 err(1, "ioctl SIOCSIFDESCR (set descr)");
1346 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
1349 setifdescr("", 0, s, 0);
1353 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \
1354 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1355 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
1358 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1359 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1360 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1361 "\26RXCSUM_IPV6\27TXCSUM_IPV6\31TXRTLMT\32HWRXTSTMP\33NOMAP\34TXTLS4\35TXTLS6" \
1362 "\36VXLAN_HWCSUM\37VXLAN_HWTSO\40TXTLS_RTLMT"
1365 * Print the status of the interface. If an address family was
1366 * specified, show only it; otherwise, show them all.
1369 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
1370 struct ifaddrs *ifa)
1372 struct ifaddrs *ift;
1378 ifr.ifr_addr.sa_family = AF_LOCAL;
1381 ifr.ifr_addr.sa_family =
1382 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
1384 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1386 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1388 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1390 printf("%s: ", name);
1391 printb("flags", ifa->ifa_flags, IFFBITS);
1392 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1393 printf(" metric %d", ifr.ifr_metric);
1394 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1395 printf(" mtu %d", ifr.ifr_mtu);
1399 if ((descr = reallocf(descr, descrlen)) != NULL) {
1400 ifr.ifr_buffer.buffer = descr;
1401 ifr.ifr_buffer.length = descrlen;
1402 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1403 if (ifr.ifr_buffer.buffer == descr) {
1404 if (strlen(descr) > 0)
1405 printf("\tdescription: %s\n",
1407 } else if (ifr.ifr_buffer.length > descrlen) {
1408 descrlen = ifr.ifr_buffer.length;
1413 warn("unable to allocate memory for interface"
1418 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1419 if (ifr.ifr_curcap != 0) {
1420 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1423 if (supmedia && ifr.ifr_reqcap != 0) {
1424 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1431 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1432 if (ift->ifa_addr == NULL)
1434 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1437 const struct afswtch *p;
1438 p = af_getbyfamily(ift->ifa_addr->sa_family);
1439 if (p != NULL && p->af_status != NULL)
1440 p->af_status(s, ift);
1441 } else if (afp->af_af == ift->ifa_addr->sa_family)
1442 afp->af_status(s, ift);
1445 if (allfamilies || afp->af_af == AF_LINK) {
1446 const struct afswtch *lafp;
1449 * Hack; the link level address is received separately
1450 * from the routing information so any address is not
1451 * handled above. Cobble together an entry and invoke
1452 * the status method specially.
1454 lafp = af_getbyname("lladdr");
1456 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1457 lafp->af_status(s, &info);
1463 else if (afp->af_other_status != NULL)
1464 afp->af_other_status(s);
1466 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1467 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1468 printf("%s", ifs.ascii);
1471 sfp_status(s, &ifr, verbose);
1478 tunnel_status(int s)
1480 af_all_tunnel_status(s);
1484 Perror(const char *cmd)
1489 errx(1, "%s: no such interface", cmd);
1493 errx(1, "%s: permission denied", cmd);
1502 * Print a value a la the %b format of the kernel's printf
1505 printb(const char *s, unsigned v, const char *bits)
1510 if (bits && *bits == 8)
1511 printf("%s=%o", s, v);
1513 printf("%s=%x", s, v);
1517 while ((i = *bits++) != '\0') {
1518 if (v & (1u << (i-1))) {
1522 for (; (c = *bits) > 32; bits++)
1525 for (; *bits > 32; bits++)
1533 print_vhid(const struct ifaddrs *ifa, const char *s)
1535 struct if_data *ifd;
1537 if (ifa->ifa_data == NULL)
1540 ifd = ifa->ifa_data;
1541 if (ifd->ifi_vhid == 0)
1544 printf(" vhid %d", ifd->ifi_vhid);
1548 ifmaybeload(const char *name)
1550 #define MOD_PREFIX_LEN 3 /* "if_" */
1551 struct module_stat mstat;
1552 int i, fileid, modid;
1553 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1555 struct module_map_entry *mme;
1558 /* loading suppressed by the user */
1562 /* trim the interface number off the end */
1563 strlcpy(ifname, name, sizeof(ifname));
1564 for (dp = ifname; *dp != 0; dp++)
1570 /* Either derive it from the map or guess otherwise */
1573 for (i = 0; i < nitems(module_map); ++i) {
1574 mme = &module_map[i];
1575 if (strcmp(mme->ifname, ifname) == 0) {
1576 strlcpy(ifkind, mme->kldname, sizeof(ifkind));
1582 /* We didn't have an alias for it... we'll guess. */
1584 /* turn interface and unit into module name */
1585 strlcpy(ifkind, "if_", sizeof(ifkind));
1586 strlcat(ifkind, ifname, sizeof(ifkind));
1589 /* scan files in kernel */
1590 mstat.version = sizeof(struct module_stat);
1591 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1592 /* scan modules in file */
1593 for (modid = kldfirstmod(fileid); modid > 0;
1594 modid = modfnext(modid)) {
1595 if (modstat(modid, &mstat) < 0)
1597 /* strip bus name if present */
1598 if ((cp = strchr(mstat.name, '/')) != NULL) {
1604 * Is it already loaded? Don't compare with ifname if
1605 * we were specifically told which kld to use. Doing
1606 * so could lead to conflicts not trivially solved.
1608 if ((!found && strcmp(ifname, cp) == 0) ||
1609 strcmp(ifkind, cp) == 0)
1615 * Try to load the module. But ignore failures, because ifconfig can't
1616 * infer the names of all drivers (eg mlx4en(4)).
1618 (void) kldload(ifkind);
1621 static struct cmd basic_cmds[] = {
1622 DEF_CMD("up", IFF_UP, setifflags),
1623 DEF_CMD("down", -IFF_UP, setifflags),
1624 DEF_CMD("arp", -IFF_NOARP, setifflags),
1625 DEF_CMD("-arp", IFF_NOARP, setifflags),
1626 DEF_CMD("debug", IFF_DEBUG, setifflags),
1627 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1628 DEF_CMD_ARG("description", setifdescr),
1629 DEF_CMD_ARG("descr", setifdescr),
1630 DEF_CMD("-description", 0, unsetifdescr),
1631 DEF_CMD("-descr", 0, unsetifdescr),
1632 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1633 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1634 DEF_CMD("add", IFF_UP, notealias),
1635 DEF_CMD("alias", IFF_UP, notealias),
1636 DEF_CMD("-alias", -IFF_UP, notealias),
1637 DEF_CMD("delete", -IFF_UP, notealias),
1638 DEF_CMD("remove", -IFF_UP, notealias),
1640 #define EN_SWABIPS 0x1000
1641 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1642 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1644 DEF_CMD_ARG("netmask", setifnetmask),
1645 DEF_CMD_ARG("metric", setifmetric),
1646 DEF_CMD_ARG("broadcast", setifbroadaddr),
1647 DEF_CMD_ARG2("tunnel", settunnel),
1648 DEF_CMD("-tunnel", 0, deletetunnel),
1649 DEF_CMD("deletetunnel", 0, deletetunnel),
1651 DEF_CMD_ARG("vnet", setifvnet),
1652 DEF_CMD_ARG("-vnet", setifrvnet),
1654 DEF_CMD("link0", IFF_LINK0, setifflags),
1655 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1656 DEF_CMD("link1", IFF_LINK1, setifflags),
1657 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1658 DEF_CMD("link2", IFF_LINK2, setifflags),
1659 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1660 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1661 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1662 DEF_CMD("mextpg", IFCAP_MEXTPG, setifcap),
1663 DEF_CMD("-mextpg", -IFCAP_MEXTPG, setifcap),
1664 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1665 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1666 DEF_CMD("rxcsum6", IFCAP_RXCSUM_IPV6, setifcap),
1667 DEF_CMD("-rxcsum6", -IFCAP_RXCSUM_IPV6, setifcap),
1668 DEF_CMD("txcsum6", IFCAP_TXCSUM_IPV6, setifcap),
1669 DEF_CMD("-txcsum6", -IFCAP_TXCSUM_IPV6, setifcap),
1670 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1671 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1672 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1673 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1674 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1675 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1676 DEF_CMD_ARG("pcp", setifpcp),
1677 DEF_CMD("-pcp", 0, disableifpcp),
1678 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1679 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1680 DEF_CMD("tso6", IFCAP_TSO6, setifcap),
1681 DEF_CMD("-tso6", -IFCAP_TSO6, setifcap),
1682 DEF_CMD("tso4", IFCAP_TSO4, setifcap),
1683 DEF_CMD("-tso4", -IFCAP_TSO4, setifcap),
1684 DEF_CMD("tso", IFCAP_TSO, setifcap),
1685 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1686 DEF_CMD("toe", IFCAP_TOE, setifcap),
1687 DEF_CMD("-toe", -IFCAP_TOE, setifcap),
1688 DEF_CMD("lro", IFCAP_LRO, setifcap),
1689 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1690 DEF_CMD("txtls", IFCAP_TXTLS, setifcap),
1691 DEF_CMD("-txtls", -IFCAP_TXTLS, setifcap),
1692 DEF_CMD("wol", IFCAP_WOL, setifcap),
1693 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1694 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1695 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1696 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1697 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1698 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1699 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1700 DEF_CMD("txrtlmt", IFCAP_TXRTLMT, setifcap),
1701 DEF_CMD("-txrtlmt", -IFCAP_TXRTLMT, setifcap),
1702 DEF_CMD("txtlsrtlmt", IFCAP_TXTLS_RTLMT, setifcap),
1703 DEF_CMD("-txtlsrtlmt", -IFCAP_TXTLS_RTLMT, setifcap),
1704 DEF_CMD("hwrxtstmp", IFCAP_HWRXTSTMP, setifcap),
1705 DEF_CMD("-hwrxtstmp", -IFCAP_HWRXTSTMP, setifcap),
1706 DEF_CMD("normal", -IFF_LINK0, setifflags),
1707 DEF_CMD("compress", IFF_LINK0, setifflags),
1708 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1709 DEF_CMD_ARG("mtu", setifmtu),
1710 DEF_CMD_ARG("name", setifname),
1713 static __constructor void
1718 for (i = 0; i < nitems(basic_cmds); i++)
1719 cmd_register(&basic_cmds[i]);