2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
40 static const char rcsid[] =
44 #include <sys/param.h>
45 #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>
81 * Since "struct ifreq" is composed of various union members, callers
82 * should pay special attention to interpret the value.
83 * (.e.g. little/big endian difference in the structure.)
100 int printkeys = 0; /* Print keying material for interfaces. */
102 /* Formatter Strings */
103 char *f_inet, *f_inet6, *f_ether, *f_addr;
105 static int ifconfig(int argc, char *const *argv, int iscreate,
106 const struct afswtch *afp);
107 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
108 struct ifaddrs *ifa);
109 static void tunnel_status(int s);
110 static _Noreturn void usage(void);
112 static struct afswtch *af_getbyname(const char *name);
113 static struct afswtch *af_getbyfamily(int af);
114 static void af_other_status(int);
116 void printifnamemaybe(void);
118 static struct option *opts = NULL;
120 struct ifa_order_elt {
124 TAILQ_ENTRY(ifa_order_elt) link;
127 TAILQ_HEAD(ifa_queue, ifa_order_elt);
129 static struct module_map_entry {
143 * This mapping exists because there is a conflicting enc module
144 * in CAM. ifconfig's guessing behavior will attempt to match
145 * the ifname to a module as well as if_${ifname} and clash with
146 * CAM enc. This is an assertion of the correct module to load.
155 opt_register(struct option *p)
167 /* XXX not right but close enough for now */
169 for (p = opts; p != NULL; p = p->next) {
170 strlcat(options, p->opt_usage, sizeof(options));
171 strlcat(options, " ", sizeof(options));
175 "usage: ifconfig [-f type:format] %sinterface address_family\n"
176 " [address [dest_address]] [parameters]\n"
177 " ifconfig interface create\n"
178 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
179 " ifconfig -l [-d] [-u] [address_family]\n"
180 " ifconfig %s[-d] [-m] [-u] [-v]\n",
181 options, options, options);
185 #define ORDERS_SIZE(x) sizeof(x) / sizeof(x[0])
188 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
190 struct ifaddrs *prev;
191 struct ifa_order_elt *cur;
192 unsigned int ord, af, ifa_ord;
199 while (ifa != NULL) {
201 strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
202 cur = calloc(1, sizeof(*cur));
207 TAILQ_INSERT_TAIL(q, cur, link);
208 cur->if_order = ifa_ord ++;
214 af = ifa->ifa_addr->sa_family;
216 if (af < ORDERS_SIZE(cur->af_orders) &&
217 cur->af_orders[af] == 0)
218 cur->af_orders[af] = ++ord;
228 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
230 struct ifa_order_elt *cur, *e1, *e2;
231 unsigned int af1, af2;
236 ret = strcmp(a->ifa_name, b->ifa_name);
238 TAILQ_FOREACH(cur, q, link) {
242 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
244 else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
251 return (e1->if_order - e2->if_order);
253 } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
254 TAILQ_FOREACH(cur, q, link) {
255 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
264 af1 = a->ifa_addr->sa_family;
265 af2 = b->ifa_addr->sa_family;
267 if (af1 < ORDERS_SIZE(e1->af_orders) &&
268 af2 < ORDERS_SIZE(e1->af_orders))
269 return (e1->af_orders[af1] - e1->af_orders[af2]);
275 static void freeformat(void)
288 static void setformat(char *input)
290 char *formatstr, *category, *modifier;
292 formatstr = strdup(input);
293 while ((category = strsep(&formatstr, ",")) != NULL) {
294 modifier = strchr(category, ':');
295 if (modifier == NULL || modifier[1] == '\0') {
296 warnx("Skipping invalid format specification: %s\n",
301 /* Split the string on the separator, then seek past it */
305 if (strcmp(category, "addr") == 0)
306 f_addr = strdup(modifier);
307 else if (strcmp(category, "ether") == 0)
308 f_ether = strdup(modifier);
309 else if (strcmp(category, "inet") == 0)
310 f_inet = strdup(modifier);
311 else if (strcmp(category, "inet6") == 0)
312 f_inet6 = strdup(modifier);
319 static struct ifaddrs *
320 sortifaddrs(struct ifaddrs *list,
321 int (*compare)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *),
324 struct ifaddrs *right, *temp, *last, *result, *next, *tail;
333 if (!list || !list->ifa_next)
336 while (temp && temp->ifa_next) {
338 right = right->ifa_next;
339 temp = temp->ifa_next->ifa_next;
342 last->ifa_next = NULL;
344 list = sortifaddrs(list, compare, q);
345 right = sortifaddrs(right, compare, q);
347 while (list || right) {
351 list = list->ifa_next;
354 right = right->ifa_next;
355 } else if (compare(list, right, q) <= 0) {
357 list = list->ifa_next;
360 right = right->ifa_next;
366 tail->ifa_next = next;
374 void printifnamemaybe()
377 printf("%s\n", name);
381 main(int argc, char *argv[])
383 int c, all, namesonly, downonly, uponly;
384 const struct afswtch *afp = NULL;
386 struct ifaddrs *ifap, *sifap, *ifa;
388 const struct sockaddr_dl *sdl;
389 char options[1024], *cp, *envformat, *namecp = NULL;
390 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
391 struct ifa_order_elt *cur, *tmp;
396 all = downonly = uponly = namesonly = noload = verbose = 0;
397 f_inet = f_inet6 = f_ether = f_addr = NULL;
399 envformat = getenv("IFCONFIG_FORMAT");
400 if (envformat != NULL)
401 setformat(envformat);
404 * Ensure we print interface name when expected to,
405 * even if we terminate early due to error.
407 atexit(printifnamemaybe);
409 /* Parse leading line options */
410 strlcpy(options, "f:adklmnuv", sizeof(options));
411 for (p = opts; p != NULL; p = p->next)
412 strlcat(options, p->opt, sizeof(options));
413 while ((c = getopt(argc, argv, options)) != -1) {
415 case 'a': /* scan all interfaces */
418 case 'd': /* restrict scan to "down" interfaces */
429 case 'l': /* scan interface names only */
432 case 'm': /* show media choices in status */
435 case 'n': /* suppress module loading */
438 case 'u': /* restrict scan to "up" interfaces */
445 for (p = opts; p != NULL; p = p->next)
446 if (p->opt[0] == c) {
458 /* -l cannot be used with -a or -m */
459 if (namesonly && (all || supmedia))
463 if (uponly && downonly)
466 /* no arguments is equivalent to '-a' */
467 if (!namesonly && argc < 1)
470 /* -a and -l allow an address family arg to limit the output */
471 if (all || namesonly) {
478 afp = af_getbyname(*argv);
480 warnx("Address family '%s' unknown.", *argv);
483 if (afp->af_name != NULL)
485 /* leave with afp non-zero */
488 /* not listing, need an argument */
495 /* check and maybe load support for this interface */
498 ifindex = if_nametoindex(ifname);
501 * NOTE: We must special-case the `create' command
502 * right here as we would otherwise fail when trying
503 * to find the interface.
505 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
506 strcmp(argv[0], "plumb") == 0)) {
507 iflen = strlcpy(name, ifname, sizeof(name));
508 if (iflen >= sizeof(name))
509 errx(1, "%s: cloning name too long",
511 ifconfig(argc, argv, 1, NULL);
516 * NOTE: We have to special-case the `-vnet' command
517 * right here as we would otherwise fail when trying
518 * to find the interface as it lives in another vnet.
520 if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
521 iflen = strlcpy(name, ifname, sizeof(name));
522 if (iflen >= sizeof(name))
523 errx(1, "%s: interface name too long",
525 ifconfig(argc, argv, 0, NULL);
529 errx(1, "interface %s does not exist", ifname);
532 * Do not allow use `create` command as hostname if
533 * address family is not specified.
535 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
536 strcmp(argv[0], "plumb") == 0)) {
538 errx(1, "interface %s already exists",
545 /* Check for address family */
547 afp = af_getbyname(*argv);
552 if (getifaddrs(&ifap) != 0)
553 err(EXIT_FAILURE, "getifaddrs");
557 if (calcorders(ifap, &q) != 0)
558 err(EXIT_FAILURE, "calcorders");
560 sifap = sortifaddrs(ifap, cmpifaddrs, &q);
562 TAILQ_FOREACH_SAFE(cur, &q, link, tmp)
566 for (ifa = sifap; ifa; ifa = ifa->ifa_next) {
567 memset(&paifr, 0, sizeof(paifr));
568 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
569 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
570 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
571 ifa->ifa_addr->sa_len);
574 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
576 if (ifa->ifa_addr->sa_family == AF_LINK)
577 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
580 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
582 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
583 if (iflen >= sizeof(name)) {
584 warnx("%s: interface name too long, skipping",
590 if ((ifa->ifa_flags & IFF_CANTCONFIG) != 0)
592 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
594 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
597 * Are we just listing the interfaces?
603 /* special case for "ether" address family */
604 if (!strcmp(afp->af_name, "ether")) {
606 (sdl->sdl_type != IFT_ETHER &&
607 sdl->sdl_type != IFT_L2VLAN &&
608 sdl->sdl_type != IFT_BRIDGE) ||
609 sdl->sdl_alen != ETHER_ADDR_LEN)
612 if (ifa->ifa_addr->sa_family
627 ifconfig(argc, argv, 0, afp);
629 status(afp, sdl, ifa);
639 static struct afswtch *afs = NULL;
642 af_register(struct afswtch *p)
648 static struct afswtch *
649 af_getbyname(const char *name)
653 for (afp = afs; afp != NULL; afp = afp->af_next)
654 if (strcmp(afp->af_name, name) == 0)
659 static struct afswtch *
660 af_getbyfamily(int af)
664 for (afp = afs; afp != NULL; afp = afp->af_next)
665 if (afp->af_af == af)
671 af_other_status(int s)
674 uint8_t afmask[howmany(AF_MAX, NBBY)];
676 memset(afmask, 0, sizeof(afmask));
677 for (afp = afs; afp != NULL; afp = afp->af_next) {
678 if (afp->af_other_status == NULL)
680 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
682 afp->af_other_status(s);
683 setbit(afmask, afp->af_af);
688 af_all_tunnel_status(int s)
691 uint8_t afmask[howmany(AF_MAX, NBBY)];
693 memset(afmask, 0, sizeof(afmask));
694 for (afp = afs; afp != NULL; afp = afp->af_next) {
695 if (afp->af_status_tunnel == NULL)
697 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
699 afp->af_status_tunnel(s);
700 setbit(afmask, afp->af_af);
704 static struct cmd *cmds = NULL;
707 cmd_register(struct cmd *p)
713 static const struct cmd *
714 cmd_lookup(const char *name, int iscreate)
718 for (p = cmds; p != NULL; p = p->c_next)
719 if (strcmp(name, p->c_name) == 0) {
732 callback_func *cb_func;
734 struct callback *cb_next;
736 static struct callback *callbacks = NULL;
739 callback_register(callback_func *func, void *arg)
743 cb = malloc(sizeof(struct callback));
745 errx(1, "unable to allocate memory for callback");
748 cb->cb_next = callbacks;
752 /* specially-handled commands */
753 static void setifaddr(const char *, int, int, const struct afswtch *);
754 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
756 static void setifdstaddr(const char *, int, int, const struct afswtch *);
757 static const struct cmd setifdstaddr_cmd =
758 DEF_CMD("ifdstaddr", 0, setifdstaddr);
761 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
763 const struct afswtch *afp, *nafp;
768 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
773 * This is the historical "accident" allowing users to configure IPv4
774 * addresses without the "inet" keyword which while a nice feature has
775 * proven to complicate other things. We cannot remove this but only
776 * make sure we will never have a similar implicit default for IPv6 or
777 * any other address familiy. We need a fallback though for
778 * ifconfig IF up/down etc. to work without INET support as people
779 * never used ifconfig IF link up/down, etc. either.
783 if (afp == NULL && feature_present("inet"))
784 afp = af_getbyname("inet");
788 afp = af_getbyname("link");
790 warnx("Please specify an address_family.");
794 ifr.ifr_addr.sa_family =
795 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
796 AF_LOCAL : afp->af_af;
798 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
799 (uafp != NULL || errno != EAFNOSUPPORT ||
800 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
801 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
804 p = cmd_lookup(*argv, iscreate);
805 if (iscreate && p == NULL) {
807 * Push the clone create callback so the new
808 * device is created and can be used for any
809 * remaining arguments.
813 errx(1, "internal error, no callback");
814 callbacks = cb->cb_next;
815 cb->cb_func(s, cb->cb_arg);
818 * Handle any address family spec that
819 * immediately follows and potentially
820 * recreate the socket.
822 nafp = af_getbyname(*argv);
832 * Look for a normal parameter.
838 * Not a recognized command, choose between setting
839 * the interface address and the dst address.
841 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
843 if (p->c_parameter == NEXTARG && p->c_u.c_func) {
845 errx(1, "'%s' requires argument",
847 p->c_u.c_func(argv[1], 0, s, afp);
849 } else if (p->c_parameter == OPTARG && p->c_u.c_func) {
850 p->c_u.c_func(argv[1], 0, s, afp);
853 } else if (p->c_parameter == NEXTARG2 && p->c_u.c_func2) {
855 errx(1, "'%s' requires 2 arguments",
857 p->c_u.c_func2(argv[1], argv[2], s, afp);
858 argc -= 2, argv += 2;
859 } else if (p->c_u.c_func)
860 p->c_u.c_func(*argv, p->c_parameter, s, afp);
865 * Do any post argument processing required by the address family.
867 if (afp->af_postproc != NULL)
868 afp->af_postproc(s, afp);
870 * Do deferred callbacks registered while processing
871 * command-line arguments.
873 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
874 cb->cb_func(s, cb->cb_arg);
876 * Do deferred operations.
879 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
880 warnx("interface %s cannot change %s addresses!",
887 strlcpy(((struct ifreq *)afp->af_ridreq)->ifr_name, name,
888 sizeof ifr.ifr_name);
889 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
891 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
892 /* means no previous address for interface */
894 Perror("ioctl (SIOCDIFADDR)");
898 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
899 warnx("interface %s cannot change %s addresses!",
904 if (newaddr && (setaddr || setmask)) {
905 strlcpy(((struct ifreq *)afp->af_addreq)->ifr_name, name,
906 sizeof ifr.ifr_name);
907 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
908 Perror("ioctl (SIOCAIFADDR)");
917 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
919 if (afp->af_getaddr == NULL)
922 * Delay the ioctl to set the interface addr until flags are all set.
923 * The address interpretation may depend on the flags,
924 * and the flags may change when the address is set.
927 if (doalias == 0 && afp->af_af != AF_LINK)
929 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
933 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
935 struct addrinfo *srcres, *dstres;
938 if (afp->af_settunnel == NULL) {
939 warn("address family %s does not support tunnel setup",
944 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
945 errx(1, "error in parsing address string: %s",
946 gai_strerror(ecode));
948 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
949 errx(1, "error in parsing address string: %s",
950 gai_strerror(ecode));
952 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
954 "source and destination address families do not match");
956 afp->af_settunnel(s, srcres, dstres);
958 freeaddrinfo(srcres);
959 freeaddrinfo(dstres);
964 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
967 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
968 err(1, "SIOCDIFPHYADDR");
973 setifvnet(const char *jname, int dummy __unused, int s,
974 const struct afswtch *afp)
978 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
979 my_ifr.ifr_jid = jail_getid(jname);
980 if (my_ifr.ifr_jid < 0)
981 errx(1, "%s", jail_errmsg);
982 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
983 err(1, "SIOCSIFVNET");
987 setifrvnet(const char *jname, int dummy __unused, int s,
988 const struct afswtch *afp)
992 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
993 my_ifr.ifr_jid = jail_getid(jname);
994 if (my_ifr.ifr_jid < 0)
995 errx(1, "%s", jail_errmsg);
996 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
997 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
1002 setifnetmask(const char *addr, int dummy __unused, int s,
1003 const struct afswtch *afp)
1005 if (afp->af_getaddr != NULL) {
1007 afp->af_getaddr(addr, MASK);
1012 setifbroadaddr(const char *addr, int dummy __unused, int s,
1013 const struct afswtch *afp)
1015 if (afp->af_getaddr != NULL)
1016 afp->af_getaddr(addr, DSTADDR);
1020 notealias(const char *addr, int param, int s, const struct afswtch *afp)
1022 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1023 if (setaddr && doalias == 0 && param < 0)
1024 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1025 bcopy((caddr_t)rqtosa(af_addreq),
1026 (caddr_t)rqtosa(af_ridreq),
1027 rqtosa(af_addreq)->sa_len);
1039 setifdstaddr(const char *addr, int param __unused, int s,
1040 const struct afswtch *afp)
1042 if (afp->af_getaddr != NULL)
1043 afp->af_getaddr(addr, DSTADDR);
1047 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
1048 * of the ifreq structure, which may confuse other parts of ifconfig.
1049 * Make a private copy so we can avoid that.
1052 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1054 struct ifreq my_ifr;
1057 memset(&my_ifr, 0, sizeof(my_ifr));
1058 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1060 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
1061 Perror("ioctl (SIOCGIFFLAGS)");
1064 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
1071 my_ifr.ifr_flags = flags & 0xffff;
1072 my_ifr.ifr_flagshigh = flags >> 16;
1073 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1078 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1082 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
1083 Perror("ioctl (SIOCGIFCAP)");
1086 flags = ifr.ifr_curcap;
1092 flags &= ifr.ifr_reqcap;
1093 ifr.ifr_reqcap = flags;
1094 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1099 setifmetric(const char *val, int dummy __unused, int s,
1100 const struct afswtch *afp)
1102 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1103 ifr.ifr_metric = atoi(val);
1104 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1105 err(1, "ioctl SIOCSIFMETRIC (set metric)");
1109 setifmtu(const char *val, int dummy __unused, int s,
1110 const struct afswtch *afp)
1112 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1113 ifr.ifr_mtu = atoi(val);
1114 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1115 err(1, "ioctl SIOCSIFMTU (set mtu)");
1119 setifpcp(const char *val, int arg __unused, int s, const struct afswtch *afp)
1124 ul = strtoul(val, &endp, 0);
1126 errx(1, "invalid value for pcp");
1128 errx(1, "value for pcp out of range");
1129 ifr.ifr_lan_pcp = ul;
1130 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1131 err(1, "SIOCSLANPCP");
1135 disableifpcp(const char *val, int arg __unused, int s,
1136 const struct afswtch *afp)
1139 ifr.ifr_lan_pcp = IFNET_PCP_NONE;
1140 if (ioctl(s, SIOCSLANPCP, (caddr_t)&ifr) == -1)
1141 err(1, "SIOCSLANPCP");
1145 setifname(const char *val, int dummy __unused, int s,
1146 const struct afswtch *afp)
1150 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1152 newname = strdup(val);
1153 if (newname == NULL)
1154 err(1, "no memory to set ifname");
1155 ifr.ifr_data = newname;
1156 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1158 err(1, "ioctl SIOCSIFNAME (set name)");
1161 strlcpy(name, newname, sizeof(name));
1167 setifdescr(const char *val, int dummy __unused, int s,
1168 const struct afswtch *afp)
1172 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1174 ifr.ifr_buffer.length = strlen(val) + 1;
1175 if (ifr.ifr_buffer.length == 1) {
1176 ifr.ifr_buffer.buffer = newdescr = NULL;
1177 ifr.ifr_buffer.length = 0;
1179 newdescr = strdup(val);
1180 ifr.ifr_buffer.buffer = newdescr;
1181 if (newdescr == NULL) {
1182 warn("no memory to set ifdescr");
1187 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
1188 err(1, "ioctl SIOCSIFDESCR (set descr)");
1195 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
1198 setifdescr("", 0, s, 0);
1202 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\7RUNNING" \
1203 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1204 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
1207 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
1208 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
1209 "\17TOE4\20TOE6\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE\25NETMAP" \
1210 "\26RXCSUM_IPV6\27TXCSUM_IPV6"
1213 * Print the status of the interface. If an address family was
1214 * specified, show only it; otherwise, show them all.
1217 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
1218 struct ifaddrs *ifa)
1220 struct ifaddrs *ift;
1226 ifr.ifr_addr.sa_family = AF_LOCAL;
1229 ifr.ifr_addr.sa_family =
1230 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
1232 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1234 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1236 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1238 printf("%s: ", name);
1239 printb("flags", ifa->ifa_flags, IFFBITS);
1240 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1241 printf(" metric %d", ifr.ifr_metric);
1242 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1243 printf(" mtu %d", ifr.ifr_mtu);
1247 if ((descr = reallocf(descr, descrlen)) != NULL) {
1248 ifr.ifr_buffer.buffer = descr;
1249 ifr.ifr_buffer.length = descrlen;
1250 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1251 if (ifr.ifr_buffer.buffer == descr) {
1252 if (strlen(descr) > 0)
1253 printf("\tdescription: %s\n",
1255 } else if (ifr.ifr_buffer.length > descrlen) {
1256 descrlen = ifr.ifr_buffer.length;
1261 warn("unable to allocate memory for interface"
1266 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1267 if (ifr.ifr_curcap != 0) {
1268 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1271 if (supmedia && ifr.ifr_reqcap != 0) {
1272 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1279 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1280 if (ift->ifa_addr == NULL)
1282 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1285 const struct afswtch *p;
1286 p = af_getbyfamily(ift->ifa_addr->sa_family);
1287 if (p != NULL && p->af_status != NULL)
1288 p->af_status(s, ift);
1289 } else if (afp->af_af == ift->ifa_addr->sa_family)
1290 afp->af_status(s, ift);
1293 if (allfamilies || afp->af_af == AF_LINK) {
1294 const struct afswtch *lafp;
1297 * Hack; the link level address is received separately
1298 * from the routing information so any address is not
1299 * handled above. Cobble together an entry and invoke
1300 * the status method specially.
1302 lafp = af_getbyname("lladdr");
1304 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1305 lafp->af_status(s, &info);
1311 else if (afp->af_other_status != NULL)
1312 afp->af_other_status(s);
1314 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1315 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1316 printf("%s", ifs.ascii);
1319 sfp_status(s, &ifr, verbose);
1326 tunnel_status(int s)
1328 af_all_tunnel_status(s);
1332 Perror(const char *cmd)
1337 errx(1, "%s: no such interface", cmd);
1341 errx(1, "%s: permission denied", cmd);
1350 * Print a value a la the %b format of the kernel's printf
1353 printb(const char *s, unsigned v, const char *bits)
1358 if (bits && *bits == 8)
1359 printf("%s=%o", s, v);
1361 printf("%s=%x", s, v);
1365 while ((i = *bits++) != '\0') {
1366 if (v & (1 << (i-1))) {
1370 for (; (c = *bits) > 32; bits++)
1373 for (; *bits > 32; bits++)
1381 print_vhid(const struct ifaddrs *ifa, const char *s)
1383 struct if_data *ifd;
1385 if (ifa->ifa_data == NULL)
1388 ifd = ifa->ifa_data;
1389 if (ifd->ifi_vhid == 0)
1392 printf(" vhid %d", ifd->ifi_vhid);
1396 ifmaybeload(const char *name)
1398 #define MOD_PREFIX_LEN 3 /* "if_" */
1399 struct module_stat mstat;
1400 int i, fileid, modid;
1401 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1403 struct module_map_entry *mme;
1406 /* loading suppressed by the user */
1410 /* trim the interface number off the end */
1411 strlcpy(ifname, name, sizeof(ifname));
1412 for (dp = ifname; *dp != 0; dp++)
1418 /* Either derive it from the map or guess otherwise */
1421 for (i = 0; i < nitems(module_map); ++i) {
1422 mme = &module_map[i];
1423 if (strcmp(mme->ifname, ifname) == 0) {
1424 strlcpy(ifkind, mme->kldname, sizeof(ifkind));
1430 /* We didn't have an alias for it... we'll guess. */
1432 /* turn interface and unit into module name */
1433 strlcpy(ifkind, "if_", sizeof(ifkind));
1434 strlcat(ifkind, ifname, sizeof(ifkind));
1437 /* scan files in kernel */
1438 mstat.version = sizeof(struct module_stat);
1439 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1440 /* scan modules in file */
1441 for (modid = kldfirstmod(fileid); modid > 0;
1442 modid = modfnext(modid)) {
1443 if (modstat(modid, &mstat) < 0)
1445 /* strip bus name if present */
1446 if ((cp = strchr(mstat.name, '/')) != NULL) {
1452 * Is it already loaded? Don't compare with ifname if
1453 * we were specifically told which kld to use. Doing
1454 * so could lead to conflicts not trivially solved.
1456 if ((!found && strcmp(ifname, cp) == 0) ||
1457 strcmp(ifkind, cp) == 0)
1463 * Try to load the module. But ignore failures, because ifconfig can't
1464 * infer the names of all drivers (eg mlx4en(4)).
1466 (void) kldload(ifkind);
1469 static struct cmd basic_cmds[] = {
1470 DEF_CMD("up", IFF_UP, setifflags),
1471 DEF_CMD("down", -IFF_UP, setifflags),
1472 DEF_CMD("arp", -IFF_NOARP, setifflags),
1473 DEF_CMD("-arp", IFF_NOARP, setifflags),
1474 DEF_CMD("debug", IFF_DEBUG, setifflags),
1475 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1476 DEF_CMD_ARG("description", setifdescr),
1477 DEF_CMD_ARG("descr", setifdescr),
1478 DEF_CMD("-description", 0, unsetifdescr),
1479 DEF_CMD("-descr", 0, unsetifdescr),
1480 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1481 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1482 DEF_CMD("add", IFF_UP, notealias),
1483 DEF_CMD("alias", IFF_UP, notealias),
1484 DEF_CMD("-alias", -IFF_UP, notealias),
1485 DEF_CMD("delete", -IFF_UP, notealias),
1486 DEF_CMD("remove", -IFF_UP, notealias),
1488 #define EN_SWABIPS 0x1000
1489 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1490 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1492 DEF_CMD_ARG("netmask", setifnetmask),
1493 DEF_CMD_ARG("metric", setifmetric),
1494 DEF_CMD_ARG("broadcast", setifbroadaddr),
1495 DEF_CMD_ARG2("tunnel", settunnel),
1496 DEF_CMD("-tunnel", 0, deletetunnel),
1497 DEF_CMD("deletetunnel", 0, deletetunnel),
1499 DEF_CMD_ARG("vnet", setifvnet),
1500 DEF_CMD_ARG("-vnet", setifrvnet),
1502 DEF_CMD("link0", IFF_LINK0, setifflags),
1503 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1504 DEF_CMD("link1", IFF_LINK1, setifflags),
1505 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1506 DEF_CMD("link2", IFF_LINK2, setifflags),
1507 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1508 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1509 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1510 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1511 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1512 DEF_CMD("rxcsum6", IFCAP_RXCSUM_IPV6, setifcap),
1513 DEF_CMD("-rxcsum6", -IFCAP_RXCSUM_IPV6, setifcap),
1514 DEF_CMD("txcsum6", IFCAP_TXCSUM_IPV6, setifcap),
1515 DEF_CMD("-txcsum6", -IFCAP_TXCSUM_IPV6, setifcap),
1516 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1517 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1518 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1519 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1520 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1521 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1522 DEF_CMD_ARG("pcp", setifpcp),
1523 DEF_CMD("-pcp", 0, disableifpcp),
1524 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1525 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1526 DEF_CMD("tso6", IFCAP_TSO6, setifcap),
1527 DEF_CMD("-tso6", -IFCAP_TSO6, setifcap),
1528 DEF_CMD("tso4", IFCAP_TSO4, setifcap),
1529 DEF_CMD("-tso4", -IFCAP_TSO4, setifcap),
1530 DEF_CMD("tso", IFCAP_TSO, setifcap),
1531 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1532 DEF_CMD("toe", IFCAP_TOE, setifcap),
1533 DEF_CMD("-toe", -IFCAP_TOE, setifcap),
1534 DEF_CMD("lro", IFCAP_LRO, setifcap),
1535 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1536 DEF_CMD("wol", IFCAP_WOL, setifcap),
1537 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1538 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1539 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1540 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1541 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1542 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1543 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1544 DEF_CMD("normal", -IFF_LINK0, setifflags),
1545 DEF_CMD("compress", IFF_LINK0, setifflags),
1546 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1547 DEF_CMD_ARG("mtu", setifmtu),
1548 DEF_CMD_ARG("name", setifname),
1551 static __constructor void
1556 for (i = 0; i < nitems(basic_cmds); i++)
1557 cmd_register(&basic_cmds[i]);