2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1983, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
42 static const char rcsid[] =
46 #include <sys/param.h>
47 #include <sys/ioctl.h>
48 #include <sys/module.h>
49 #include <sys/linker.h>
50 #include <sys/queue.h>
51 #include <sys/socket.h>
54 #include <net/ethernet.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <arpa/inet.h>
82 * Since "struct ifreq" is composed of various union members, callers
83 * should pay special attention to interpret the value.
84 * (.e.g. little/big endian difference in the structure.)
101 int printkeys = 0; /* Print keying material for interfaces. */
103 /* Formatter Strings */
104 char *f_inet, *f_inet6, *f_ether, *f_addr;
106 static int ifconfig(int argc, char *const *argv, int iscreate,
107 const struct afswtch *afp);
108 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
109 struct ifaddrs *ifa);
110 static void tunnel_status(int s);
111 static void usage(void) _Noreturn;
113 static struct afswtch *af_getbyname(const char *name);
114 static struct afswtch *af_getbyfamily(int af);
115 static void af_other_status(int);
117 void printifnamemaybe(void);
119 static struct option *opts = NULL;
121 struct ifa_order_elt {
125 TAILQ_ENTRY(ifa_order_elt) link;
128 TAILQ_HEAD(ifa_queue, ifa_order_elt);
131 opt_register(struct option *p)
143 /* XXX not right but close enough for now */
145 for (p = opts; p != NULL; p = p->next) {
146 strlcat(options, p->opt_usage, sizeof(options));
147 strlcat(options, " ", sizeof(options));
151 "usage: ifconfig [-f type:format] %sinterface address_family\n"
152 " [address [dest_address]] [parameters]\n"
153 " ifconfig interface create\n"
154 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
155 " ifconfig -l [-d] [-u] [address_family]\n"
156 " ifconfig %s[-d] [-m] [-u] [-v]\n",
157 options, options, options);
161 #define ORDERS_SIZE(x) sizeof(x) / sizeof(x[0])
164 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
166 struct ifaddrs *prev;
167 struct ifa_order_elt *cur;
168 unsigned int ord, af, ifa_ord;
175 while (ifa != NULL) {
177 strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
178 cur = calloc(1, sizeof(*cur));
183 TAILQ_INSERT_TAIL(q, cur, link);
184 cur->if_order = ifa_ord ++;
190 af = ifa->ifa_addr->sa_family;
192 if (af < ORDERS_SIZE(cur->af_orders) &&
193 cur->af_orders[af] == 0)
194 cur->af_orders[af] = ++ord;
204 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
206 struct ifa_order_elt *cur, *e1, *e2;
207 unsigned int af1, af2;
212 ret = strcmp(a->ifa_name, b->ifa_name);
214 TAILQ_FOREACH(cur, q, link) {
218 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
220 else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
227 return (e1->if_order - e2->if_order);
229 } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
230 TAILQ_FOREACH(cur, q, link) {
231 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
240 af1 = a->ifa_addr->sa_family;
241 af2 = b->ifa_addr->sa_family;
243 if (af1 < ORDERS_SIZE(e1->af_orders) &&
244 af2 < ORDERS_SIZE(e1->af_orders))
245 return (e1->af_orders[af1] - e1->af_orders[af2]);
251 static void freeformat(void)
264 static void setformat(char *input)
266 char *formatstr, *category, *modifier;
268 formatstr = strdup(input);
269 while ((category = strsep(&formatstr, ",")) != NULL) {
270 modifier = strchr(category, ':');
271 if (modifier == NULL || modifier[1] == '\0') {
272 warnx("Skipping invalid format specification: %s\n",
277 /* Split the string on the separator, then seek past it */
281 if (strcmp(category, "addr") == 0)
282 f_addr = strdup(modifier);
283 else if (strcmp(category, "ether") == 0)
284 f_ether = strdup(modifier);
285 else if (strcmp(category, "inet") == 0)
286 f_inet = strdup(modifier);
287 else if (strcmp(category, "inet6") == 0)
288 f_inet6 = strdup(modifier);
295 static struct ifaddrs *
296 sortifaddrs(struct ifaddrs *list,
297 int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *),
300 struct ifaddrs *right, *temp, *last, *result, *next, *tail;
309 if (!list || !list->ifa_next)
312 while (temp && temp->ifa_next) {
314 right = right->ifa_next;
315 temp = temp->ifa_next->ifa_next;
318 last->ifa_next = NULL;
320 list = sortifaddrs(list, compare, q);
321 right = sortifaddrs(right, compare, q);
323 while (list || right) {
327 list = list->ifa_next;
330 right = right->ifa_next;
331 } else if (compare(list, right, q) <= 0) {
333 list = list->ifa_next;
336 right = right->ifa_next;
342 tail->ifa_next = next;
350 void printifnamemaybe()
353 printf("%s\n", name);
357 main(int argc, char *argv[])
359 int c, all, namesonly, downonly, uponly;
360 const struct afswtch *afp = NULL;
362 struct ifaddrs *ifap, *sifap, *ifa;
364 const struct sockaddr_dl *sdl;
365 char options[1024], *cp, *envformat, *namecp = NULL;
366 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
367 struct ifa_order_elt *cur, *tmp;
372 all = downonly = uponly = namesonly = noload = verbose = 0;
373 f_inet = f_inet6 = f_ether = f_addr = NULL;
375 envformat = getenv("IFCONFIG_FORMAT");
376 if (envformat != NULL)
377 setformat(envformat);
380 * Ensure we print interface name when expected to,
381 * even if we terminate early due to error.
383 atexit(printifnamemaybe);
385 /* Parse leading line options */
386 strlcpy(options, "f:adklmnuv", sizeof(options));
387 for (p = opts; p != NULL; p = p->next)
388 strlcat(options, p->opt, sizeof(options));
389 while ((c = getopt(argc, argv, options)) != -1) {
391 case 'a': /* scan all interfaces */
394 case 'd': /* restrict scan to "down" interfaces */
405 case 'l': /* scan interface names only */
408 case 'm': /* show media choices in status */
411 case 'n': /* suppress module loading */
414 case 'u': /* restrict scan to "up" interfaces */
421 for (p = opts; p != NULL; p = p->next)
422 if (p->opt[0] == c) {
434 /* -l cannot be used with -a or -m */
435 if (namesonly && (all || supmedia))
439 if (uponly && downonly)
442 /* no arguments is equivalent to '-a' */
443 if (!namesonly && argc < 1)
446 /* -a and -l allow an address family arg to limit the output */
447 if (all || namesonly) {
454 afp = af_getbyname(*argv);
456 warnx("Address family '%s' unknown.", *argv);
459 if (afp->af_name != NULL)
461 /* leave with afp non-zero */
464 /* not listing, need an argument */
471 /* check and maybe load support for this interface */
474 ifindex = if_nametoindex(ifname);
477 * NOTE: We must special-case the `create' command
478 * right here as we would otherwise fail when trying
479 * to find the interface.
481 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
482 strcmp(argv[0], "plumb") == 0)) {
483 iflen = strlcpy(name, ifname, sizeof(name));
484 if (iflen >= sizeof(name))
485 errx(1, "%s: cloning name too long",
487 ifconfig(argc, argv, 1, NULL);
492 * NOTE: We have to special-case the `-vnet' command
493 * right here as we would otherwise fail when trying
494 * to find the interface as it lives in another vnet.
496 if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
497 iflen = strlcpy(name, ifname, sizeof(name));
498 if (iflen >= sizeof(name))
499 errx(1, "%s: interface name too long",
501 ifconfig(argc, argv, 0, NULL);
505 errx(1, "interface %s does not exist", ifname);
509 /* Check for address family */
511 afp = af_getbyname(*argv);
516 if (getifaddrs(&ifap) != 0)
517 err(EXIT_FAILURE, "getifaddrs");
521 if (calcorders(ifap, &q) != 0)
522 err(EXIT_FAILURE, "calcorders");
524 sifap = sortifaddrs(ifap, cmpifaddrs, &q);
526 TAILQ_FOREACH_SAFE(cur, &q, link, tmp)
530 for (ifa = sifap; ifa; ifa = ifa->ifa_next) {
531 memset(&paifr, 0, sizeof(paifr));
532 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
533 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
534 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
535 ifa->ifa_addr->sa_len);
538 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
540 if (ifa->ifa_addr->sa_family == AF_LINK)
541 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
544 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
546 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
547 if (iflen >= sizeof(name)) {
548 warnx("%s: interface name too long, skipping",
554 if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
556 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
558 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
561 * Are we just listing the interfaces?
567 /* special case for "ether" address family */
568 if (!strcmp(afp->af_name, "ether")) {
570 (sdl->sdl_type != IFT_ETHER &&
571 sdl->sdl_type != IFT_L2VLAN &&
572 sdl->sdl_type != IFT_BRIDGE) ||
573 sdl->sdl_alen != ETHER_ADDR_LEN)
576 if (ifa->ifa_addr->sa_family
591 ifconfig(argc, argv, 0, afp);
593 status(afp, sdl, ifa);
603 static struct afswtch *afs = NULL;
606 af_register(struct afswtch *p)
612 static struct afswtch *
613 af_getbyname(const char *name)
617 for (afp = afs; afp != NULL; afp = afp->af_next)
618 if (strcmp(afp->af_name, name) == 0)
623 static struct afswtch *
624 af_getbyfamily(int af)
628 for (afp = afs; afp != NULL; afp = afp->af_next)
629 if (afp->af_af == af)
635 af_other_status(int s)
638 uint8_t afmask[howmany(AF_MAX, NBBY)];
640 memset(afmask, 0, sizeof(afmask));
641 for (afp = afs; afp != NULL; afp = afp->af_next) {
642 if (afp->af_other_status == NULL)
644 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
646 afp->af_other_status(s);
647 setbit(afmask, afp->af_af);
652 af_all_tunnel_status(int s)
655 uint8_t afmask[howmany(AF_MAX, NBBY)];
657 memset(afmask, 0, sizeof(afmask));
658 for (afp = afs; afp != NULL; afp = afp->af_next) {
659 if (afp->af_status_tunnel == NULL)
661 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
663 afp->af_status_tunnel(s);
664 setbit(afmask, afp->af_af);
668 static struct cmd *cmds = NULL;
671 cmd_register(struct cmd *p)
677 static const struct cmd *
678 cmd_lookup(const char *name, int iscreate)
682 for (p = cmds; p != NULL; p = p->c_next)
683 if (strcmp(name, p->c_name) == 0) {
696 callback_func *cb_func;
698 struct callback *cb_next;
700 static struct callback *callbacks = NULL;
703 callback_register(callback_func *func, void *arg)
707 cb = malloc(sizeof(struct callback));
709 errx(1, "unable to allocate memory for callback");
712 cb->cb_next = callbacks;
716 /* specially-handled commands */
717 static void setifaddr(const char *, int, int, const struct afswtch *);
718 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
720 static void setifdstaddr(const char *, int, int, const struct afswtch *);
721 static const struct cmd setifdstaddr_cmd =
722 DEF_CMD("ifdstaddr", 0, setifdstaddr);
725 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
727 const struct afswtch *afp, *nafp;
732 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
737 * This is the historical "accident" allowing users to configure IPv4
738 * addresses without the "inet" keyword which while a nice feature has
739 * proven to complicate other things. We cannot remove this but only
740 * make sure we will never have a similar implicit default for IPv6 or
741 * any other address familiy. We need a fallback though for
742 * ifconfig IF up/down etc. to work without INET support as people
743 * never used ifconfig IF link up/down, etc. either.
747 if (afp == NULL && feature_present("inet"))
748 afp = af_getbyname("inet");
752 afp = af_getbyname("link");
754 warnx("Please specify an address_family.");
758 ifr.ifr_addr.sa_family =
759 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
760 AF_LOCAL : afp->af_af;
762 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
763 (uafp != NULL || errno != EAFNOSUPPORT ||
764 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
765 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
768 p = cmd_lookup(*argv, iscreate);
769 if (iscreate && p == NULL) {
771 * Push the clone create callback so the new
772 * device is created and can be used for any
773 * remaining arguments.
777 errx(1, "internal error, no callback");
778 callbacks = cb->cb_next;
779 cb->cb_func(s, cb->cb_arg);
782 * Handle any address family spec that
783 * immediately follows and potentially
784 * recreate the socket.
786 nafp = af_getbyname(*argv);
796 * Look for a normal parameter.
802 * Not a recognized command, choose between setting
803 * the interface address and the dst address.
805 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
807 if (p->c_parameter == NEXTARG && p->c_u.c_func) {
809 errx(1, "'%s' requires argument",
811 p->c_u.c_func(argv[1], 0, s, afp);
813 } else if (p->c_parameter == OPTARG && p->c_u.c_func) {
814 p->c_u.c_func(argv[1], 0, s, afp);
817 } else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) {
819 errx(1, "'%s' requires 2 arguments",
821 p->c_u.c_func2(argv[1], argv[2], s, afp);
822 argc -= 2, argv += 2;
823 } else if (p->c_u.c_func)
824 p->c_u.c_func(*argv, p->c_parameter, s, afp);
829 * Do any post argument processing required by the address family.
831 if (afp->af_postproc != NULL)
832 afp->af_postproc(s, afp);
834 * Do deferred callbacks registered while processing
835 * command-line arguments.
837 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
838 cb->cb_func(s, cb->cb_arg);
840 * Do deferred operations.
843 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
844 warnx("interface %s cannot change %s addresses!",
851 strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
852 sizeof ifr.ifr_name);
853 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
855 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
856 /* means no previous address for interface */
858 Perror("ioctl (SIOCDIFADDR)");
862 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
863 warnx("interface %s cannot change %s addresses!",
868 if (newaddr && (setaddr || setmask)) {
869 strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
870 sizeof ifr.ifr_name);
871 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
872 Perror("ioctl (SIOCAIFADDR)");
881 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
883 if (afp->af_getaddr == NULL)
886 * Delay the ioctl to set the interface addr until flags are all set.
887 * The address interpretation may depend on the flags,
888 * and the flags may change when the address is set.
891 if (doalias == 0 && afp->af_af != AF_LINK)
893 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
897 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
899 struct addrinfo *srcres, *dstres;
902 if (afp->af_settunnel == NULL) {
903 warn("address family %s does not support tunnel setup",
908 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
909 errx(1, "error in parsing address string: %s",
910 gai_strerror(ecode));
912 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
913 errx(1, "error in parsing address string: %s",
914 gai_strerror(ecode));
916 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
918 "source and destination address families do not match");
920 afp->af_settunnel(s, srcres, dstres);
922 freeaddrinfo(srcres);
923 freeaddrinfo(dstres);
928 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
931 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
932 err(1, "SIOCDIFPHYADDR");
937 setifvnet(const char *jname, int dummy __unused, int s,
938 const struct afswtch *afp)
942 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
943 my_ifr.ifr_jid = jail_getid(jname);
944 if (my_ifr.ifr_jid < 0)
945 errx(1, "%s", jail_errmsg);
946 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
947 err(1, "SIOCSIFVNET");
951 setifrvnet(const char *jname, int dummy __unused, int s,
952 const struct afswtch *afp)
956 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
957 my_ifr.ifr_jid = jail_getid(jname);
958 if (my_ifr.ifr_jid < 0)
959 errx(1, "%s", jail_errmsg);
960 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
961 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
966 setifnetmask(const char *addr, int dummy __unused, int s,
967 const struct afswtch *afp)
969 if (afp->af_getaddr != NULL) {
971 afp->af_getaddr(addr, MASK);
976 setifbroadaddr(const char *addr, int dummy __unused, int s,
977 const struct afswtch *afp)
979 if (afp->af_getaddr != NULL)
980 afp->af_getaddr(addr, DSTADDR);
984 notealias(const char *addr, int param, int s, const struct afswtch *afp)
986 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
987 if (setaddr && doalias == 0 && param < 0)
988 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
989 bcopy((caddr_t)rqtosa(af_addreq),
990 (caddr_t)rqtosa(af_ridreq),
991 rqtosa(af_addreq)->sa_len);
1003 setifdstaddr(const char *addr, int param __unused, int s,
1004 const struct afswtch *afp)
1006 if (afp->af_getaddr != NULL)
1007 afp->af_getaddr(addr, DSTADDR);
1011 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1012 * of the ifreq structure, which may confuse other parts of ifconfig.
1013 * Make a private copy so we can avoid that.
1016 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1018 struct ifreq my_ifr;
1021 memset(&my_ifr, 0, sizeof(my_ifr));
1022 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1024 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1025 Perror("ioctl (SIOCGIFFLAGS)");
1028 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1035 my_ifr.ifr_flags = flags & 0xffff;
1036 my_ifr.ifr_flagshigh = flags >> 16;
1037 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1042 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1046 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1047 Perror("ioctl (SIOCGIFCAP)");
1050 flags = ifr.ifr_curcap;
1056 flags &= ifr.ifr_reqcap;
1057 ifr.ifr_reqcap = flags;
1058 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1063 setifmetric(const char *val, int dummy __unused, int s,
1064 const struct afswtch *afp)
1066 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1067 ifr.ifr_metric = atoi(val);
1068 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1069 err(1, "ioctl SIOCSIFMETRIC (set metric)");
1073 setifmtu(const char *val, int dummy __unused, int s,
1074 const struct afswtch *afp)
1076 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1077 ifr.ifr_mtu = atoi(val);
1078 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1079 err(1, "ioctl SIOCSIFMTU (set mtu)");
1083 setifname(const char *val, int dummy __unused, int s,
1084 const struct afswtch *afp)
1088 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1090 newname = strdup(val);
1091 if (newname == NULL)
1092 err(1, "no memory to set ifname");
1093 ifr.ifr_data = newname;
1094 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1096 err(1, "ioctl SIOCSIFNAME (set name)");
1099 strlcpy(name, newname, sizeof(name));
1105 setifdescr(const char *val, int dummy __unused, int s,
1106 const struct afswtch *afp)
1110 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1112 ifr.ifr_buffer.length = strlen(val) + 1;
1113 if (ifr.ifr_buffer.length == 1) {
1114 ifr.ifr_buffer.buffer = newdescr = NULL;
1115 ifr.ifr_buffer.length = 0;
1117 newdescr = strdup(val);
1118 ifr.ifr_buffer.buffer = newdescr;
1119 if (newdescr == NULL) {
1120 warn("no memory to set ifdescr");
1125 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
1126 err(1, "ioctl SIOCSIFDESCR (set descr)");
1133 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
1136 setifdescr("", 0, s, 0);
1140 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \
1141 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1142 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
1145 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1146 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1147 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1148 "\26RXCSUM_IPV6\27TXCSUM_IPV6\31TXRTLMT\32HWRXTSTMP"
1151 * Print the status of the interface. If an address family was
1152 * specified, show only it; otherwise, show them all.
1155 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
1156 struct ifaddrs *ifa)
1158 struct ifaddrs *ift;
1164 ifr.ifr_addr.sa_family = AF_LOCAL;
1167 ifr.ifr_addr.sa_family =
1168 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
1170 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1172 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1174 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1176 printf("%s: ", name);
1177 printb("flags", ifa->ifa_flags, IFFBITS);
1178 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1179 printf(" metric %d", ifr.ifr_metric);
1180 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1181 printf(" mtu %d", ifr.ifr_mtu);
1185 if ((descr = reallocf(descr, descrlen)) != NULL) {
1186 ifr.ifr_buffer.buffer = descr;
1187 ifr.ifr_buffer.length = descrlen;
1188 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1189 if (ifr.ifr_buffer.buffer == descr) {
1190 if (strlen(descr) > 0)
1191 printf("\tdescription: %s\n",
1193 } else if (ifr.ifr_buffer.length > descrlen) {
1194 descrlen = ifr.ifr_buffer.length;
1199 warn("unable to allocate memory for interface"
1204 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1205 if (ifr.ifr_curcap != 0) {
1206 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1209 if (supmedia && ifr.ifr_reqcap != 0) {
1210 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1217 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1218 if (ift->ifa_addr == NULL)
1220 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1223 const struct afswtch *p;
1224 p = af_getbyfamily(ift->ifa_addr->sa_family);
1225 if (p != NULL && p->af_status != NULL)
1226 p->af_status(s, ift);
1227 } else if (afp->af_af == ift->ifa_addr->sa_family)
1228 afp->af_status(s, ift);
1231 if (allfamilies || afp->af_af == AF_LINK) {
1232 const struct afswtch *lafp;
1235 * Hack; the link level address is received separately
1236 * from the routing information so any address is not
1237 * handled above. Cobble together an entry and invoke
1238 * the status method specially.
1240 lafp = af_getbyname("lladdr");
1242 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1243 lafp->af_status(s, &info);
1249 else if (afp->af_other_status != NULL)
1250 afp->af_other_status(s);
1252 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1253 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1254 printf("%s", ifs.ascii);
1257 sfp_status(s, &ifr, verbose);
1264 tunnel_status(int s)
1266 af_all_tunnel_status(s);
1270 Perror(const char *cmd)
1275 errx(1, "%s: no such interface", cmd);
1279 errx(1, "%s: permission denied", cmd);
1288 * Print a value a la the %b format of the kernel's printf
1291 printb(const char *s, unsigned v, const char *bits)
1296 if (bits && *bits == 8)
1297 printf("%s=%o", s, v);
1299 printf("%s=%x", s, v);
1303 while ((i = *bits++) != '\0') {
1304 if (v & (1 << (i-1))) {
1308 for (; (c = *bits) > 32; bits++)
1311 for (; *bits > 32; bits++)
1319 print_vhid(const struct ifaddrs *ifa, const char *s)
1321 struct if_data *ifd;
1323 if (ifa->ifa_data == NULL)
1326 ifd = ifa->ifa_data;
1327 if (ifd->ifi_vhid == 0)
1330 printf("vhid %d ", ifd->ifi_vhid);
1334 ifmaybeload(const char *name)
1336 #define MOD_PREFIX_LEN 3 /* "if_" */
1337 struct module_stat mstat;
1339 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1342 /* loading suppressed by the user */
1346 /* trim the interface number off the end */
1347 strlcpy(ifname, name, sizeof(ifname));
1348 for (dp = ifname; *dp != 0; dp++)
1354 /* turn interface and unit into module name */
1355 strlcpy(ifkind, "if_", sizeof(ifkind));
1356 strlcat(ifkind, ifname, sizeof(ifkind));
1358 /* scan files in kernel */
1359 mstat.version = sizeof(struct module_stat);
1360 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1361 /* scan modules in file */
1362 for (modid = kldfirstmod(fileid); modid > 0;
1363 modid = modfnext(modid)) {
1364 if (modstat(modid, &mstat) < 0)
1366 /* strip bus name if present */
1367 if ((cp = strchr(mstat.name, '/')) != NULL) {
1372 /* already loaded? */
1373 if (strcmp(ifname, cp) == 0 ||
1374 strcmp(ifkind, cp) == 0)
1380 * Try to load the module. But ignore failures, because ifconfig can't
1381 * infer the names of all drivers (eg mlx4en(4)).
1383 (void) kldload(ifkind);
1386 static struct cmd basic_cmds[] = {
1387 DEF_CMD("up", IFF_UP, setifflags),
1388 DEF_CMD("down", -IFF_UP, setifflags),
1389 DEF_CMD("arp", -IFF_NOARP, setifflags),
1390 DEF_CMD("-arp", IFF_NOARP, setifflags),
1391 DEF_CMD("debug", IFF_DEBUG, setifflags),
1392 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1393 DEF_CMD_ARG("description", setifdescr),
1394 DEF_CMD_ARG("descr", setifdescr),
1395 DEF_CMD("-description", 0, unsetifdescr),
1396 DEF_CMD("-descr", 0, unsetifdescr),
1397 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1398 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1399 DEF_CMD("add", IFF_UP, notealias),
1400 DEF_CMD("alias", IFF_UP, notealias),
1401 DEF_CMD("-alias", -IFF_UP, notealias),
1402 DEF_CMD("delete", -IFF_UP, notealias),
1403 DEF_CMD("remove", -IFF_UP, notealias),
1405 #define EN_SWABIPS 0x1000
1406 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1407 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1409 DEF_CMD_ARG("netmask", setifnetmask),
1410 DEF_CMD_ARG("metric", setifmetric),
1411 DEF_CMD_ARG("broadcast", setifbroadaddr),
1412 DEF_CMD_ARG2("tunnel", settunnel),
1413 DEF_CMD("-tunnel", 0, deletetunnel),
1414 DEF_CMD("deletetunnel", 0, deletetunnel),
1416 DEF_CMD_ARG("vnet", setifvnet),
1417 DEF_CMD_ARG("-vnet", setifrvnet),
1419 DEF_CMD("link0", IFF_LINK0, setifflags),
1420 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1421 DEF_CMD("link1", IFF_LINK1, setifflags),
1422 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1423 DEF_CMD("link2", IFF_LINK2, setifflags),
1424 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1425 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1426 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1427 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1428 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1429 DEF_CMD("rxcsum6", IFCAP_RXCSUM_IPV6, setifcap),
1430 DEF_CMD("-rxcsum6", -IFCAP_RXCSUM_IPV6, setifcap),
1431 DEF_CMD("txcsum6", IFCAP_TXCSUM_IPV6, setifcap),
1432 DEF_CMD("-txcsum6", -IFCAP_TXCSUM_IPV6, setifcap),
1433 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1434 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1435 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1436 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1437 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1438 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1439 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1440 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1441 DEF_CMD("tso6", IFCAP_TSO6, setifcap),
1442 DEF_CMD("-tso6", -IFCAP_TSO6, setifcap),
1443 DEF_CMD("tso4", IFCAP_TSO4, setifcap),
1444 DEF_CMD("-tso4", -IFCAP_TSO4, setifcap),
1445 DEF_CMD("tso", IFCAP_TSO, setifcap),
1446 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1447 DEF_CMD("toe", IFCAP_TOE, setifcap),
1448 DEF_CMD("-toe", -IFCAP_TOE, setifcap),
1449 DEF_CMD("lro", IFCAP_LRO, setifcap),
1450 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1451 DEF_CMD("wol", IFCAP_WOL, setifcap),
1452 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1453 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1454 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1455 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1456 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1457 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1458 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1459 DEF_CMD("txrtlmt", IFCAP_TXRTLMT, setifcap),
1460 DEF_CMD("-txrtlmt", -IFCAP_TXRTLMT, setifcap),
1461 DEF_CMD("hwrxtsmp", IFCAP_HWRXTSTMP, setifcap),
1462 DEF_CMD("-hwrxtsmp", -IFCAP_HWRXTSTMP, setifcap),
1463 DEF_CMD("normal", -IFF_LINK0, setifflags),
1464 DEF_CMD("compress", IFF_LINK0, setifflags),
1465 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1466 DEF_CMD_ARG("mtu", setifmtu),
1467 DEF_CMD_ARG("name", setifname),
1470 static __constructor void
1475 for (i = 0; i < nitems(basic_cmds); i++)
1476 cmd_register(&basic_cmds[i]);