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 * 3. 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
29 * $FreeBSD: src/sbin/ifconfig/ifconfig.c,v 1.113.2.4 2006/02/09 10:48:43 yar Exp $
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
37 #include <sys/module.h>
38 #include <sys/linker.h>
39 #include <sys/cdefs.h>
40 #include <sys/queue.h>
42 #include <net/ethernet.h>
44 #include <net/if_var.h>
45 #include <net/if_dl.h>
46 #include <net/if_types.h>
47 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <arpa/inet.h>
70 * Since "struct ifreq" is composed of various union members, callers
71 * should pay special attention to interpret the value.
72 * (.e.g. little/big endian difference in the structure.)
80 int printkeys = 0; /* Print keying material for interfaces. */
81 int printifname = 0; /* Print the name of the created interface. */
83 char *f_inet, *f_inet6, *f_ether, *f_addr; /* Formatter strings */
85 static bool group_member(const char *ifname, const char *match,
87 static int ifconfig(int argc, char *const *argv, int iscreate,
88 const struct afswtch *afp);
89 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
91 static void tunnel_status(int s);
92 static void usage(void) __dead2;
94 static int getifflags(const char *ifname, int us);
95 static struct afswtch *af_getbyname(const char *name);
96 static struct afswtch *af_getbyfamily(int af);
97 static void af_other_status(int);
98 static void printifnamemaybe(void);
99 static void freeformat(void);
100 static void setformat(char *input);
102 static struct option *opts = NULL;
103 static char *descr = NULL;
104 static size_t descrlen = 64;
105 static int clearaddr;
111 struct ifa_order_elt {
115 TAILQ_ENTRY(ifa_order_elt) link;
117 TAILQ_HEAD(ifa_queue, ifa_order_elt);
119 static int calcorders(struct ifaddrs *ifa, struct ifa_queue *q);
120 static int cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b,
121 struct ifa_queue *q);
122 typedef int (*ifaddrs_cmp)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *);
123 static struct ifaddrs *sortifaddrs(struct ifaddrs *list, ifaddrs_cmp compare,
124 struct ifa_queue *q);
128 opt_register(struct option *p)
140 /* XXX not right but close enough for now */
142 for (p = opts; p != NULL; p = p->next) {
143 strlcat(options, p->opt_usage, sizeof(options));
144 strlcat(options, " ", sizeof(options));
148 "usage: ifconfig %s[-n] [-f type:format] interface address_family\n"
149 " [address [dest_address]] [parameters]\n"
150 " ifconfig [-n] interface create\n"
151 " ifconfig [-n] interface destroy\n"
152 " ifconfig -a %s[-G nogroup] [-d | -u] [-m] [-v] [address_family]\n"
153 " ifconfig -l [-d | -u] [address_family]\n"
154 " ifconfig %s[-d | -u] [-m] [-v]\n",
155 options, options, options);
160 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
162 struct ifaddrs *prev;
163 struct ifa_order_elt *cur;
164 unsigned int ord, af, ifa_ord;
171 while (ifa != NULL) {
173 strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
174 cur = calloc(1, sizeof(*cur));
178 TAILQ_INSERT_TAIL(q, cur, link);
179 cur->if_order = ifa_ord++;
185 af = ifa->ifa_addr->sa_family;
187 if (af < nitems(cur->af_orders) &&
188 cur->af_orders[af] == 0)
189 cur->af_orders[af] = ++ord;
200 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
202 struct ifa_order_elt *cur, *e1, *e2;
203 unsigned int af1, af2;
207 if (strcmp(a->ifa_name, b->ifa_name) != 0) {
208 TAILQ_FOREACH(cur, q, link) {
209 if (e1 != NULL && e2 != NULL)
212 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
214 else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
218 if (e1 == NULL || e2 == NULL)
221 return (e1->if_order - e2->if_order);
223 } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
224 TAILQ_FOREACH(cur, q, link) {
225 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
234 af1 = a->ifa_addr->sa_family;
235 af2 = b->ifa_addr->sa_family;
237 if (af1 < nitems(e1->af_orders) && af2 < nitems(e1->af_orders))
238 return (e1->af_orders[af1] - e1->af_orders[af2]);
244 static struct ifaddrs *
245 sortifaddrs(struct ifaddrs *list, ifaddrs_cmp compare, struct ifa_queue *q)
247 struct ifaddrs *right, *temp, *last, *result, *next, *tail;
249 right = temp = last = list;
250 result = next = tail = NULL;
252 if (list == NULL || list->ifa_next == NULL)
255 while (temp != NULL && temp->ifa_next != NULL) {
257 right = right->ifa_next;
258 temp = temp->ifa_next->ifa_next;
261 last->ifa_next = NULL;
263 list = sortifaddrs(list, compare, q);
264 right = sortifaddrs(right, compare, q);
266 while (list != NULL || right != NULL) {
269 list = list->ifa_next;
270 } else if (list == NULL) {
272 right = right->ifa_next;
273 } else if (compare(list, right, q) <= 0) {
275 list = list->ifa_next;
278 right = right->ifa_next;
284 tail->ifa_next = next;
293 printifnamemaybe(void)
296 printf("%s\n", name);
313 setformat(char *input)
315 char *formatstr, *category, *modifier;
318 formatstr = strdup(input);
319 if (formatstr == NULL)
320 err(1, "no memory to set format");
322 while ((category = strsep(&formatstr, ",")) != NULL) {
323 modifier = strchr(category, ':');
324 if (modifier == NULL || modifier[1] == '\0') {
325 warnx("skip invalid format specification: %s\n",
334 if (strcmp(category, "addr") == 0)
336 else if (strcmp(category, "ether") == 0)
338 else if (strcmp(category, "inet") == 0)
340 else if (strcmp(category, "inet6") == 0)
344 *fp = strdup(modifier);
355 main(int argc, char *argv[])
357 int c, all, namesonly, downonly, uponly;
359 const struct afswtch *afp = NULL;
360 const struct sockaddr_dl *sdl;
361 const char *ifname, *matchgroup, *nogroup;
362 struct ifa_order_elt *cur, *tmp;
363 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
364 struct ifaddrs *ifap, *sifap, *ifa;
368 char *envformat, *cp;
371 all = downonly = uponly = namesonly = verbose = noload = 0;
372 f_inet = f_inet6 = f_ether = f_addr = NULL;
373 matchgroup = nogroup = NULL;
376 * Ensure we print interface name when expected to,
377 * even if we terminate early due to error.
379 atexit(printifnamemaybe);
381 envformat = getenv("IFCONFIG_FORMAT");
382 if (envformat != NULL)
383 setformat(envformat);
385 /* Parse leading line options */
386 strlcpy(options, "adf:G:klmnuv", 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 */
408 case 'l': /* scan interface names only */
411 case 'm': /* show media choices in status */
414 case 'n': /* suppress module loading */
417 case 'u': /* restrict scan to "up" interfaces */
428 /* FALLTHROUGH (for ifgroup) */
430 for (p = opts; p != NULL; p = p->next)
431 if (p->opt[0] == c) {
443 /* -l cannot be used with -a or -m */
444 if (namesonly && (all || supmedia))
448 if (uponly && downonly)
451 /* no arguments is equivalent to '-a' */
452 if (!namesonly && argc < 1)
455 /* -a and -l allow an address family arg to limit the output */
456 if (all || namesonly) {
463 afp = af_getbyname(*argv);
466 if (afp->af_name != NULL)
468 /* leave with afp non-zero */
471 /* not listing, need an argument */
478 /* check and maybe load support for this interface */
481 ifindex = if_nametoindex(ifname);
484 * NOTE: We must special-case the `create' command
485 * right here as we would otherwise fail when trying
486 * to find the interface.
488 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
489 strcmp(argv[0], "plumb") == 0)) {
490 iflen = strlcpy(name, ifname, sizeof(name));
491 if (iflen >= sizeof(name))
492 errx(1, "%s: cloning name too long",
494 ifconfig(argc, argv, 1, NULL);
497 errx(1, "interface %s does not exist", ifname);
500 * Do not allow to use `create` command as hostname
501 * if address family is not specified.
503 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
504 strcmp(argv[0], "plumb") == 0)) {
506 errx(1, "interface %s already exists",
513 /* Check for address family */
515 afp = af_getbyname(*argv);
521 * Check for a requested configuration action on a single interface,
522 * which doesn't require building, sorting, and searching the entire
523 * system address list.
525 if (argc > 0 && ifname != NULL) {
526 iflen = strlcpy(name, ifname, sizeof(name));
527 if (iflen >= sizeof(name))
528 errx(1, "%s: cloning name too long", ifname);
530 flags = getifflags(name, -1);
531 if (!((downonly && (flags & IFF_UP) != 0) ||
532 (uponly && (flags & IFF_UP) == 0))) {
533 ifconfig(argc, argv, 0, afp);
539 if (getifaddrs(&ifap) != 0)
540 err(1, "getifaddrs");
541 if (calcorders(ifap, &q) != 0)
542 err(1, "calcorders");
543 sifap = sortifaddrs(ifap, cmpifaddrs, &q);
545 TAILQ_FOREACH_MUTABLE(cur, &q, link, tmp)
550 for (ifa = sifap; ifa != NULL; ifa = ifa->ifa_next) {
551 memset(&paifr, 0, sizeof(paifr));
552 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
553 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
554 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
555 ifa->ifa_addr->sa_len);
558 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
560 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0)
562 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
563 if (iflen >= sizeof(name)) {
564 warnx("%s: interface name too long, skipping",
570 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
572 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
574 if (!group_member(ifa->ifa_name, matchgroup, nogroup))
577 if (ifa->ifa_addr->sa_family == AF_LINK)
578 sdl = (const struct sockaddr_dl *)ifa->ifa_addr;
582 /* Are we just listing the interfaces? */
585 afp->af_af != AF_LINK ||
586 (sdl != NULL && sdl->sdl_type == IFT_ETHER)) {
587 printf("%s%s", (ifindex > 0 ? " " : ""), name);
594 ifconfig(argc, argv, 0, afp);
596 status(afp, sdl, ifa);
610 * Returns true if an interface should be listed because any its groups
611 * matches shell pattern "match" and none of groups matches pattern "nomatch".
612 * If any pattern is NULL, corresponding condition is skipped.
615 group_member(const char *ifname, const char *match, const char *nomatch)
617 static int sock = -1;
619 struct ifgroupreq ifgr;
622 bool matched, nomatched;
625 if (match == NULL && nomatch == NULL)
630 memset(&ifgr, 0, sizeof(ifgr));
631 strlcpy(ifgr.ifgr_name, ifname, sizeof(ifgr.ifgr_name));
633 /* The socket is opened once. Let _exit() close it. */
635 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
637 errx(1, "%s: socket(AF_LOCAL,SOCK_DGRAM)", __func__);
640 /* Determine amount of memory for the list of groups. */
641 if (ioctl(sock, SIOCGIFGROUP, &ifgr) == -1) {
642 if (errno == EINVAL || errno == ENOTTY)
645 errx(1, "%s: SIOCGIFGROUP", __func__);
648 /* Obtain the list of groups. */
650 ifgr.ifgr_groups = calloc(1, len);
651 if (ifgr.ifgr_groups == NULL)
652 errx(1, "%s: no memory", __func__);
653 if (ioctl(sock, SIOCGIFGROUP, &ifgr) == -1)
654 errx(1, "%s: SIOCGIFGROUP", __func__);
656 /* Perform matching. */
659 for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(*ifg); ifg++) {
660 len -= sizeof(struct ifg_req);
662 matched |= !fnmatch(match, ifg->ifgrq_group, 0);
664 nomatched &= fnmatch(nomatch, ifg->ifgrq_group, 0);
667 if (match && !nomatch)
669 if (!match && nomatch)
671 return (matched && nomatched);
675 static struct afswtch *afs = NULL;
678 af_register(struct afswtch *p)
684 static struct afswtch *
685 af_getbyname(const char *name)
689 for (afp = afs; afp != NULL; afp = afp->af_next)
690 if (strcmp(afp->af_name, name) == 0)
695 static struct afswtch *
696 af_getbyfamily(int af)
700 for (afp = afs; afp != NULL; afp = afp->af_next)
701 if (afp->af_af == af)
707 af_other_status(int s)
710 uint8_t afmask[howmany(AF_MAX, NBBY)];
712 memset(afmask, 0, sizeof(afmask));
713 for (afp = afs; afp != NULL; afp = afp->af_next) {
714 if (afp->af_other_status == NULL)
716 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
718 afp->af_other_status(s);
719 setbit(afmask, afp->af_af);
724 af_all_tunnel_status(int s)
727 uint8_t afmask[howmany(AF_MAX, NBBY)];
729 memset(afmask, 0, sizeof(afmask));
730 for (afp = afs; afp != NULL; afp = afp->af_next) {
731 if (afp->af_status_tunnel == NULL)
733 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
735 afp->af_status_tunnel(s);
736 setbit(afmask, afp->af_af);
740 static struct cmd *cmds = NULL;
743 cmd_register(struct cmd *p)
749 static const struct cmd *
750 cmd_lookup(const char *name, int iscreate)
754 for (p = cmds; p != NULL; p = p->c_next) {
755 if (strcmp(name, p->c_name) == 0) {
770 callback_func *cb_func;
772 struct callback *cb_next;
774 static struct callback *callbacks = NULL;
777 callback_register(callback_func *func, void *arg)
781 cb = malloc(sizeof(struct callback));
783 errx(1, "unable to allocate memory for callback");
786 cb->cb_next = callbacks;
790 /* specially-handled commands */
791 static void setifaddr(const char *, int, int, const struct afswtch *);
792 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
794 static void setifdstaddr(const char *, int, int, const struct afswtch *);
795 static const struct cmd setifdstaddr_cmd =
796 DEF_CMD("ifdstaddr", 0, setifdstaddr);
799 ifconfig(int argc, char *const *argv, int iscreate,
800 const struct afswtch *uafp)
802 const struct afswtch *afp, *nafp;
807 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
808 afp = uafp != NULL ? uafp : af_getbyname("inet");
810 ifr.ifr_addr.sa_family =
811 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
812 AF_LOCAL : afp->af_af;
814 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
815 (uafp != NULL || errno != EAFNOSUPPORT ||
816 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
817 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
820 p = cmd_lookup(*argv, iscreate);
821 if (iscreate && p == NULL) {
823 * Push the clone create callback so the new
824 * device is created and can be used for any
825 * remaining arguments.
829 errx(1, "internal error, no callback");
830 callbacks = cb->cb_next;
831 cb->cb_func(s, cb->cb_arg);
835 * After cloning, make sure we have an up-to-date name
838 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
841 * Handle any address family spec that
842 * immediately follows and potentially
843 * recreate the socket.
845 nafp = af_getbyname(*argv);
855 /* Look for a normal parameter. */
860 * Not a recognized command, choose between setting
861 * the interface address and the dst address.
863 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
865 if (p->c_u.c_func || p->c_u.c_func2) {
866 if (p->c_parameter == NEXTARG) {
868 errx(1, "'%s' requires argument",
870 p->c_u.c_func(argv[1], 0, s, afp);
872 } else if (p->c_parameter == OPTARG) {
873 p->c_u.c_func(argv[1], 0, s, afp);
876 } else if (p->c_parameter == NEXTARG2) {
878 errx(1, "'%s' requires 2 arguments",
880 p->c_u.c_func2(argv[1], argv[2], s, afp);
881 argc -= 2, argv += 2;
883 p->c_u.c_func(*argv, p->c_parameter, s, afp);
889 * Do any post argument processing required by the address family.
891 if (afp->af_postproc != NULL)
892 afp->af_postproc(s, afp);
894 * Do deferred callbacks registered while processing
895 * command-line arguments.
897 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
898 cb->cb_func(s, cb->cb_arg);
900 * Do deferred operations.
903 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
904 warnx("interface %s cannot change %s addresses!",
910 strlcpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
911 if (ioctl(s, afp->af_difaddr, afp->af_ridreq) < 0) {
912 if (errno == EADDRNOTAVAIL && doalias >= 0) {
913 /* means no previous address for interface */
915 Perror("ioctl (SIOCDIFADDR)");
919 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
920 warnx("interface %s cannot change %s addresses!",
925 if (newaddr && (setaddr || setmask)) {
926 strlcpy(afp->af_addreq, name, sizeof ifr.ifr_name);
927 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
928 Perror("ioctl (SIOCAIFADDR)");
936 setifaddr(const char *addr, int dummy __unused, int s __unused,
937 const struct afswtch *afp)
939 if (afp->af_getaddr == NULL)
942 * Delay the ioctl to set the interface addr until flags are all set.
943 * The address interpretation may depend on the flags,
944 * and the flags may change when the address is set.
947 if (doalias == 0 && afp->af_af != AF_LINK)
949 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
953 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
955 struct addrinfo *srcres, *dstres;
958 if (afp->af_settunnel == NULL) {
959 warn("address family %s does not support tunnel setup",
964 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
965 errx(1, "error in parsing address string: %s",
966 gai_strerror(ecode));
968 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
969 errx(1, "error in parsing address string: %s",
970 gai_strerror(ecode));
972 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
974 "source and destination address families do not match");
976 afp->af_settunnel(s, srcres, dstres);
978 freeaddrinfo(srcres);
979 freeaddrinfo(dstres);
983 deletetunnel(const char *arg __unused, int dummy __unused, int s,
984 const struct afswtch *afp __unused)
986 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
987 err(1, "SIOCDIFPHYADDR");
991 setifnetmask(const char *addr, int dummy __unused, int s __unused,
992 const struct afswtch *afp)
994 if (afp->af_getaddr != NULL) {
996 afp->af_getaddr(addr, MASK);
1001 setifbroadaddr(const char *addr, int dummy __unused, int s __unused,
1002 const struct afswtch *afp)
1004 if (afp->af_getaddr != NULL)
1005 afp->af_getaddr(addr, DSTADDR);
1009 notealias(const char *addr __unused, int param, int s __unused,
1010 const struct afswtch *afp)
1012 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1013 if (setaddr && doalias == 0 && param < 0)
1014 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1015 bcopy(rqtosa(af_addreq), rqtosa(af_ridreq),
1016 rqtosa(af_addreq)->sa_len);
1028 setifdstaddr(const char *addr, int dummy __unused, int s __unused,
1029 const struct afswtch *afp __unused)
1031 if (afp->af_getaddr != NULL)
1032 afp->af_getaddr(addr, DSTADDR);
1036 getifflags(const char *ifname, int us)
1038 struct ifreq my_ifr;
1041 memset(&my_ifr, 0, sizeof(struct ifreq));
1042 strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name));
1046 if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
1047 err(1, "socket(family AF_LOCAL,SOCK_DGRAM)");
1050 if (ioctl(s, SIOCGIFFLAGS, &my_ifr) < 0)
1051 Perror("ioctl (SIOCGIFFLAGS)");
1056 return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16));
1060 setifflags(const char *vname, int value, int s,
1061 const struct afswtch *afp __unused)
1063 struct ifreq my_ifr;
1066 flags = getifflags(name, s);
1074 memset(&my_ifr, 0, sizeof(struct ifreq));
1075 strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1076 my_ifr.ifr_flags = flags & 0xffff;
1077 my_ifr.ifr_flagshigh = flags >> 16;
1079 if (ioctl(s, SIOCSIFFLAGS, &my_ifr) < 0)
1084 setifcap(const char *vname, int value, int s,
1085 const struct afswtch *afp __unused)
1089 if (ioctl(s, SIOCGIFCAP, &ifr) < 0)
1090 Perror("ioctl (SIOCGIFCAP)");
1092 flags = ifr.ifr_curcap;
1099 ifr.ifr_reqcap = flags;
1100 if (ioctl(s, SIOCSIFCAP, &ifr) < 0)
1105 setifmetric(const char *val, int dummy __unused, int s,
1106 const struct afswtch *afp __unused)
1108 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1109 ifr.ifr_metric = atoi(val);
1110 if (ioctl(s, SIOCSIFMETRIC, &ifr) < 0)
1111 err(1, "ioctl SIOCSIFMETRIC (set metric)");
1115 setifmtu(const char *val, int dummy __unused, int s,
1116 const struct afswtch *afp __unused)
1118 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1119 ifr.ifr_mtu = atoi(val);
1120 if (ioctl(s, SIOCSIFMTU, &ifr) < 0)
1121 err(1, "ioctl SIOCSIFMTU (set mtu)");
1125 setiftsolen(const char *val, int dummy __unused, int s,
1126 const struct afswtch *afp __unused)
1128 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1129 ifr.ifr_tsolen = atoi(val);
1130 if (ioctl(s, SIOCSIFTSOLEN, &ifr) < 0)
1131 err(1, "ioctl SIOCSIFTSOLEN (set tsolen)");
1135 setifname(const char *val, int dummy __unused, int s,
1136 const struct afswtch *afp __unused)
1140 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1142 newname = strdup(val);
1143 if (newname == NULL)
1144 err(1, "no memory to set ifname");
1145 ifr.ifr_data = newname;
1146 if (ioctl(s, SIOCSIFNAME, &ifr) < 0) {
1148 err(1, "ioctl SIOCSIFNAME (set name)");
1151 strlcpy(name, newname, sizeof(name));
1156 setifpollcpu(const char *val __unused, int dummy __unused, int s,
1157 const struct afswtch *afp)
1159 warnx("pollcpu is deprecated, use polling or npolling instead");
1160 setifflags("npolling", IFF_NPOLLING, s, afp);
1164 setifdescr(const char *val, int dummy __unused, int s,
1165 const struct afswtch *afp __unused)
1169 ifr.ifr_buffer.length = strlen(val) + 1;
1170 if (ifr.ifr_buffer.length == 1) {
1171 ifr.ifr_buffer.buffer = newdescr = NULL;
1172 ifr.ifr_buffer.length = 0;
1174 newdescr = strdup(val);
1175 ifr.ifr_buffer.buffer = newdescr;
1176 if (newdescr == NULL) {
1177 warn("no memory to set ifdescr");
1182 if (ioctl(s, SIOCSIFDESCR, &ifr) < 0)
1183 warn("ioctl (set descr)");
1189 unsetifdescr(const char *val __unused, int dummy __unused, int s,
1190 const struct afswtch *afp __unused)
1192 setifdescr("", 0, s, 0);
1196 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1197 "\10NOARP\11PROMISC\12ALLMULTI\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1198 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25NPOLLING\26IDIRECT"
1201 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7RSS" \
1202 "\10VLAN_HWCSUM\11TSO"
1205 * Print the status of the interface. If an address family was
1206 * specified, show only it; otherwise, show them all.
1209 status(const struct afswtch *afp, const struct sockaddr_dl *sdl __unused,
1210 struct ifaddrs *ifa)
1212 struct ifaddrs *ift;
1218 ifr.ifr_addr.sa_family = AF_LOCAL;
1221 ifr.ifr_addr.sa_family =
1222 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
1224 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1226 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1228 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1230 printf("%s: ", name);
1231 printb("flags", ifa->ifa_flags, IFFBITS);
1232 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1233 printf(" metric %d", ifr.ifr_metric);
1234 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1235 printf(" mtu %d", ifr.ifr_mtu);
1239 if ((descr = reallocf(descr, descrlen)) != NULL) {
1240 ifr.ifr_buffer.buffer = descr;
1241 ifr.ifr_buffer.length = descrlen;
1242 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
1243 if (ifr.ifr_buffer.length > 1)
1244 printf("\tdescription: %s\n", descr);
1245 } else if (errno == ENOMSG) {
1247 } else if (errno == ENAMETOOLONG) {
1248 descrlen = ifr.ifr_buffer.length;
1251 warn("ioctl (get descr)");
1254 warn("unable to allocate memory for interface "
1260 if (ioctl(s, SIOCGIFCAP, &ifr) == 0) {
1261 if (ifr.ifr_curcap != 0) {
1262 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1265 if (supmedia && ifr.ifr_reqcap != 0) {
1266 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1268 if (ifr.ifr_reqcap & IFCAP_TSO) {
1269 if (ioctl(s, SIOCGIFTSOLEN, &ifr) == 0) {
1270 printf("\ttsolen %d", ifr.ifr_tsolen);
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);
1322 tunnel_status(int s)
1324 af_all_tunnel_status(s);
1328 Perror(const char *cmd)
1332 errx(1, "%s: no such interface", cmd);
1335 errx(1, "%s: permission denied", cmd);
1343 * Print a value a la the %pb%i format of the kernel's kprintf()
1346 printb(const char *s, unsigned v, const char *bits)
1351 if (bits && *bits == 8)
1352 printf("%s=%o", s, v);
1354 printf("%s=%x", s, v);
1358 while ((i = *bits++) != '\0') {
1359 if (v & (1 << (i-1))) {
1363 for (; (c = *bits) > 32; bits++)
1366 for (; *bits > 32; bits++)
1374 ifmaybeload(const char *name)
1376 #define MOD_PREFIX_LEN 3 /* "if_" */
1377 struct module_stat mstat;
1379 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1382 /* loading suppressed by the user */
1386 /* trim the interface number off the end */
1387 strlcpy(ifname, name, sizeof(ifname));
1388 for (dp = ifname; *dp != 0; dp++)
1394 /* turn interface and unit into module name */
1395 strlcpy(ifkind, "if_", sizeof(ifkind));
1396 strlcat(ifkind, ifname, sizeof(ifkind));
1398 /* scan files in kernel */
1399 mstat.version = sizeof(struct module_stat);
1400 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1401 /* scan modules in file */
1402 for (modid = kldfirstmod(fileid); modid > 0;
1403 modid = modfnext(modid)) {
1404 if (modstat(modid, &mstat) < 0)
1406 /* strip bus name if present */
1407 if ((cp = strchr(mstat.name, '/')) != NULL) {
1412 /* already loaded? */
1413 if (strcmp(ifname, cp) == 0 ||
1414 strcmp(ifkind, cp) == 0)
1419 /* not present, we should try to load it */
1423 static struct cmd basic_cmds[] = {
1424 DEF_CMD("up", IFF_UP, setifflags),
1425 DEF_CMD("down", -IFF_UP, setifflags),
1426 DEF_CMD("arp", -IFF_NOARP, setifflags),
1427 DEF_CMD("-arp", IFF_NOARP, setifflags),
1428 DEF_CMD("debug", IFF_DEBUG, setifflags),
1429 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1430 DEF_CMD_ARG("description", setifdescr),
1431 DEF_CMD_ARG("descr", setifdescr),
1432 DEF_CMD("-description", 0, unsetifdescr),
1433 DEF_CMD("-descr", 0, unsetifdescr),
1434 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1435 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1436 DEF_CMD("add", IFF_UP, notealias),
1437 DEF_CMD("alias", IFF_UP, notealias),
1438 DEF_CMD("-alias", -IFF_UP, notealias),
1439 DEF_CMD("delete", -IFF_UP, notealias),
1440 DEF_CMD("remove", -IFF_UP, notealias),
1442 #define EN_SWABIPS 0x1000
1443 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1444 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1446 DEF_CMD_ARG("netmask", setifnetmask),
1447 DEF_CMD_ARG("metric", setifmetric),
1448 DEF_CMD_ARG("broadcast", setifbroadaddr),
1449 DEF_CMD_ARG2("tunnel", settunnel),
1450 DEF_CMD("-tunnel", 0, deletetunnel),
1451 DEF_CMD("deletetunnel", 0, deletetunnel),
1452 DEF_CMD("link0", IFF_LINK0, setifflags),
1453 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1454 DEF_CMD("link1", IFF_LINK1, setifflags),
1455 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1456 DEF_CMD("link2", IFF_LINK2, setifflags),
1457 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1458 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1459 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1460 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1461 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1462 DEF_CMD("polling", IFF_NPOLLING, setifflags),
1463 DEF_CMD("-polling", -IFF_NPOLLING, setifflags),
1464 DEF_CMD("npolling", IFF_NPOLLING, setifflags),
1465 DEF_CMD("-npolling", -IFF_NPOLLING, setifflags),
1466 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1467 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1468 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1469 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1470 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1471 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1472 DEF_CMD("rss", IFCAP_RSS, setifcap),
1473 DEF_CMD("-rss", -IFCAP_RSS, setifcap),
1474 DEF_CMD("tso", IFCAP_TSO, setifcap),
1475 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1476 DEF_CMD("normal", -IFF_LINK0, setifflags),
1477 DEF_CMD("compress", IFF_LINK0, setifflags),
1478 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1479 DEF_CMD_ARG("mtu", setifmtu),
1480 DEF_CMD_ARG("name", setifname),
1481 DEF_CMD_ARG("pollcpu", setifpollcpu),
1482 DEF_CMD_ARG("tsolen", setiftsolen)
1491 for (i = 0; i < nitems(basic_cmds); i++)
1492 cmd_register(&basic_cmds[i]);