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.)
88 int printkeys = 0; /* Print keying material for interfaces. */
89 int printifname = 0; /* Print the name of the created interface. */
92 /* Formatter strings */
93 char *f_inet, *f_inet6, *f_ether, *f_addr;
95 static bool group_member(const char *ifname, const char *match,
97 static int ifconfig(int argc, char *const *argv, int iscreate,
98 const struct afswtch *afp);
99 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
100 struct ifaddrs *ifa);
101 static void tunnel_status(int s);
102 static void usage(void) __dead2;
104 static int getifflags(const char *ifname, int us);
105 static struct afswtch *af_getbyname(const char *name);
106 static struct afswtch *af_getbyfamily(int af);
107 static void af_other_status(int);
108 static void printifnamemaybe(void);
109 static void freeformat(void);
110 static void setformat(char *input);
112 static struct option *opts = NULL;
114 struct ifa_order_elt {
118 TAILQ_ENTRY(ifa_order_elt) link;
120 TAILQ_HEAD(ifa_queue, ifa_order_elt);
122 static int calcorders(struct ifaddrs *ifa, struct ifa_queue *q);
123 static int cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b,
124 struct ifa_queue *q);
125 typedef int (*ifaddrs_cmp)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *);
126 static struct ifaddrs *sortifaddrs(struct ifaddrs *list, ifaddrs_cmp compare,
127 struct ifa_queue *q);
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 %s[-n] [-f type:format] interface address_family\n"
152 " [address [dest_address]] [parameters]\n"
153 " ifconfig [-n] interface create\n"
154 " ifconfig [-n] interface destroy\n"
155 " ifconfig -a %s[-G nogroup] [-d | -u] [-m] [-v] [address_family]\n"
156 " ifconfig -l [-d | -u] [address_family]\n"
157 " ifconfig %s[-d | -u] [-m] [-v]\n",
158 options, options, options);
163 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
165 struct ifaddrs *prev;
166 struct ifa_order_elt *cur;
167 unsigned int ord, af, ifa_ord;
174 while (ifa != NULL) {
176 strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
177 cur = calloc(1, sizeof(*cur));
181 TAILQ_INSERT_TAIL(q, cur, link);
182 cur->if_order = ifa_ord++;
188 af = ifa->ifa_addr->sa_family;
190 if (af < nitems(cur->af_orders) &&
191 cur->af_orders[af] == 0)
192 cur->af_orders[af] = ++ord;
203 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
205 struct ifa_order_elt *cur, *e1, *e2;
206 unsigned int af1, af2;
210 if (strcmp(a->ifa_name, b->ifa_name) != 0) {
211 TAILQ_FOREACH(cur, q, link) {
212 if (e1 != NULL && e2 != NULL)
215 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
217 else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
221 if (e1 == NULL || e2 == NULL)
224 return (e1->if_order - e2->if_order);
226 } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
227 TAILQ_FOREACH(cur, q, link) {
228 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
237 af1 = a->ifa_addr->sa_family;
238 af2 = b->ifa_addr->sa_family;
240 if (af1 < nitems(e1->af_orders) && af2 < nitems(e1->af_orders))
241 return (e1->af_orders[af1] - e1->af_orders[af2]);
247 static struct ifaddrs *
248 sortifaddrs(struct ifaddrs *list, ifaddrs_cmp compare, struct ifa_queue *q)
250 struct ifaddrs *right, *temp, *last, *result, *next, *tail;
252 right = temp = last = list;
253 result = next = tail = NULL;
255 if (list == NULL || list->ifa_next == NULL)
258 while (temp != NULL && temp->ifa_next != NULL) {
260 right = right->ifa_next;
261 temp = temp->ifa_next->ifa_next;
264 last->ifa_next = NULL;
266 list = sortifaddrs(list, compare, q);
267 right = sortifaddrs(right, compare, q);
269 while (list != NULL || right != NULL) {
272 list = list->ifa_next;
273 } else if (list == NULL) {
275 right = right->ifa_next;
276 } else if (compare(list, right, q) <= 0) {
278 list = list->ifa_next;
281 right = right->ifa_next;
287 tail->ifa_next = next;
296 printifnamemaybe(void)
299 printf("%s\n", name);
316 setformat(char *input)
318 char *formatstr, *category, *modifier;
321 formatstr = strdup(input);
322 if (formatstr == NULL)
323 err(1, "no memory to set format");
325 while ((category = strsep(&formatstr, ",")) != NULL) {
326 modifier = strchr(category, ':');
327 if (modifier == NULL || modifier[1] == '\0') {
328 warnx("skip invalid format specification: %s\n",
337 if (strcmp(category, "addr") == 0)
339 else if (strcmp(category, "ether") == 0)
341 else if (strcmp(category, "inet") == 0)
343 else if (strcmp(category, "inet6") == 0)
347 *fp = strdup(modifier);
358 main(int argc, char *argv[])
360 int c, all, namesonly, downonly, uponly;
362 const struct afswtch *afp = NULL;
363 const struct sockaddr_dl *sdl;
364 const char *ifname, *matchgroup, *nogroup;
365 struct ifa_order_elt *cur, *tmp;
366 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
367 struct ifaddrs *ifap, *sifap, *ifa;
371 char *envformat, *cp;
374 all = downonly = uponly = namesonly = verbose = noload = 0;
375 f_inet = f_inet6 = f_ether = f_addr = NULL;
376 matchgroup = nogroup = NULL;
379 * Ensure we print interface name when expected to,
380 * even if we terminate early due to error.
382 atexit(printifnamemaybe);
384 envformat = getenv("IFCONFIG_FORMAT");
385 if (envformat != NULL)
386 setformat(envformat);
388 /* Parse leading line options */
389 strlcpy(options, "adf:G:klmnuv", sizeof(options));
390 for (p = opts; p != NULL; p = p->next)
391 strlcat(options, p->opt, sizeof(options));
392 while ((c = getopt(argc, argv, options)) != -1) {
394 case 'a': /* scan all interfaces */
397 case 'd': /* restrict scan to "down" interfaces */
411 case 'l': /* scan interface names only */
414 case 'm': /* show media choices in status */
417 case 'n': /* suppress module loading */
420 case 'u': /* restrict scan to "up" interfaces */
431 /* FALLTHROUGH (for ifgroup) */
433 for (p = opts; p != NULL; p = p->next)
434 if (p->opt[0] == c) {
446 /* -l cannot be used with -a or -m */
447 if (namesonly && (all || supmedia))
451 if (uponly && downonly)
454 /* no arguments is equivalent to '-a' */
455 if (!namesonly && argc < 1)
458 /* -a and -l allow an address family arg to limit the output */
459 if (all || namesonly) {
466 afp = af_getbyname(*argv);
469 if (afp->af_name != NULL)
471 /* leave with afp non-zero */
474 /* not listing, need an argument */
481 /* check and maybe load support for this interface */
484 ifindex = if_nametoindex(ifname);
487 * NOTE: We must special-case the `create' command
488 * right here as we would otherwise fail when trying
489 * to find the interface.
491 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
492 strcmp(argv[0], "plumb") == 0)) {
493 iflen = strlcpy(name, ifname, sizeof(name));
494 if (iflen >= sizeof(name))
495 errx(1, "%s: cloning name too long",
497 ifconfig(argc, argv, 1, NULL);
500 errx(1, "interface %s does not exist", ifname);
503 * Do not allow to use `create` command as hostname
504 * if address family is not specified.
506 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
507 strcmp(argv[0], "plumb") == 0)) {
509 errx(1, "interface %s already exists",
516 /* Check for address family */
518 afp = af_getbyname(*argv);
524 * Check for a requested configuration action on a single interface,
525 * which doesn't require building, sorting, and searching the entire
526 * system address list.
528 if (argc > 0 && ifname != NULL) {
529 iflen = strlcpy(name, ifname, sizeof(name));
530 if (iflen >= sizeof(name))
531 errx(1, "%s: cloning name too long", ifname);
533 flags = getifflags(name, -1);
534 if (!((downonly && (flags & IFF_UP) != 0) ||
535 (uponly && (flags & IFF_UP) == 0))) {
536 ifconfig(argc, argv, 0, afp);
542 if (getifaddrs(&ifap) != 0)
543 err(1, "getifaddrs");
544 if (calcorders(ifap, &q) != 0)
545 err(1, "calcorders");
546 sifap = sortifaddrs(ifap, cmpifaddrs, &q);
548 TAILQ_FOREACH_MUTABLE(cur, &q, link, tmp)
553 for (ifa = sifap; ifa != NULL; ifa = ifa->ifa_next) {
554 memset(&paifr, 0, sizeof(paifr));
555 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
556 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
557 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
558 ifa->ifa_addr->sa_len);
561 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
563 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0)
565 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
566 if (iflen >= sizeof(name)) {
567 warnx("%s: interface name too long, skipping",
573 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
575 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
577 if (!group_member(ifa->ifa_name, matchgroup, nogroup))
580 if (ifa->ifa_addr->sa_family == AF_LINK)
581 sdl = (const struct sockaddr_dl *)ifa->ifa_addr;
585 /* Are we just listing the interfaces? */
588 afp->af_af != AF_LINK ||
589 (sdl != NULL && sdl->sdl_type == IFT_ETHER)) {
590 printf("%s%s", (ifindex > 0 ? " " : ""), name);
597 ifconfig(argc, argv, 0, afp);
599 status(afp, sdl, ifa);
613 * Returns true if an interface should be listed because any its groups
614 * matches shell pattern "match" and none of groups matches pattern "nomatch".
615 * If any pattern is NULL, corresponding condition is skipped.
618 group_member(const char *ifname, const char *match, const char *nomatch)
620 static int sock = -1;
622 struct ifgroupreq ifgr;
625 bool matched, nomatched;
628 if (match == NULL && nomatch == NULL)
633 memset(&ifgr, 0, sizeof(ifgr));
634 strlcpy(ifgr.ifgr_name, ifname, sizeof(ifgr.ifgr_name));
636 /* The socket is opened once. Let _exit() close it. */
638 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
640 errx(1, "%s: socket(AF_LOCAL,SOCK_DGRAM)", __func__);
643 /* Determine amount of memory for the list of groups. */
644 if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
645 if (errno == EINVAL || errno == ENOTTY)
648 errx(1, "%s: SIOCGIFGROUP", __func__);
651 /* Obtain the list of groups. */
654 (struct ifg_req *)calloc(len / sizeof(*ifg), sizeof(*ifg));
655 if (ifgr.ifgr_groups == NULL)
656 errx(1, "%s: no memory", __func__);
657 if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
658 errx(1, "%s: SIOCGIFGROUP", __func__);
660 /* Perform matching. */
663 for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(*ifg); ifg++) {
664 len -= sizeof(struct ifg_req);
666 matched |= !fnmatch(match, ifg->ifgrq_group, 0);
668 nomatched &= fnmatch(nomatch, ifg->ifgrq_group, 0);
671 if (match && !nomatch)
673 if (!match && nomatch)
675 return (matched && nomatched);
679 static struct afswtch *afs = NULL;
682 af_register(struct afswtch *p)
688 static struct afswtch *
689 af_getbyname(const char *name)
693 for (afp = afs; afp != NULL; afp = afp->af_next)
694 if (strcmp(afp->af_name, name) == 0)
699 static struct afswtch *
700 af_getbyfamily(int af)
704 for (afp = afs; afp != NULL; afp = afp->af_next)
705 if (afp->af_af == af)
711 af_other_status(int s)
714 uint8_t afmask[howmany(AF_MAX, NBBY)];
716 memset(afmask, 0, sizeof(afmask));
717 for (afp = afs; afp != NULL; afp = afp->af_next) {
718 if (afp->af_other_status == NULL)
720 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
722 afp->af_other_status(s);
723 setbit(afmask, afp->af_af);
728 af_all_tunnel_status(int s)
731 uint8_t afmask[howmany(AF_MAX, NBBY)];
733 memset(afmask, 0, sizeof(afmask));
734 for (afp = afs; afp != NULL; afp = afp->af_next) {
735 if (afp->af_status_tunnel == NULL)
737 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
739 afp->af_status_tunnel(s);
740 setbit(afmask, afp->af_af);
744 static struct cmd *cmds = NULL;
747 cmd_register(struct cmd *p)
753 static const struct cmd *
754 cmd_lookup(const char *name, int iscreate)
758 for (p = cmds; p != NULL; p = p->c_next) {
759 if (strcmp(name, p->c_name) == 0) {
774 callback_func *cb_func;
776 struct callback *cb_next;
778 static struct callback *callbacks = NULL;
781 callback_register(callback_func *func, void *arg)
785 cb = malloc(sizeof(struct callback));
787 errx(1, "unable to allocate memory for callback");
790 cb->cb_next = callbacks;
794 /* specially-handled commands */
795 static void setifaddr(const char *, int, int, const struct afswtch *);
796 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
798 static void setifdstaddr(const char *, int, int, const struct afswtch *);
799 static const struct cmd setifdstaddr_cmd =
800 DEF_CMD("ifdstaddr", 0, setifdstaddr);
803 ifconfig(int argc, char *const *argv, int iscreate,
804 const struct afswtch *uafp)
806 const struct afswtch *afp, *nafp;
810 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
811 afp = uafp != NULL ? uafp : af_getbyname("inet");
813 ifr.ifr_addr.sa_family =
814 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
815 AF_LOCAL : afp->af_af;
817 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
818 (uafp != NULL || errno != EAFNOSUPPORT ||
819 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
820 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
825 p = cmd_lookup(*argv, iscreate);
827 if (iscreate && p == NULL) {
829 * Push the clone create callback so the new
830 * device is created and can be used for any
831 * remaining arguments.
835 errx(1, "internal error, no callback");
836 callbacks = cb->cb_next;
837 cb->cb_func(s, cb->cb_arg);
841 * After cloning, make sure we have an up-to-date name
844 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
847 * Handle any address family spec that
848 * immediately follows and potentially
849 * recreate the socket.
851 nafp = af_getbyname(*argv);
861 * Look for a normal parameter.
867 * Not a recognized command, choose between setting
868 * the interface address and the dst address.
870 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
872 if (p->c_u.c_func || p->c_u.c_func2) {
873 if (p->c_parameter == NEXTARG) {
875 errx(1, "'%s' requires argument",
877 p->c_u.c_func(argv[1], 0, s, afp);
879 } else if (p->c_parameter == OPTARG) {
880 p->c_u.c_func(argv[1], 0, s, afp);
883 } else if (p->c_parameter == NEXTARG2) {
885 errx(1, "'%s' requires 2 arguments",
887 p->c_u.c_func2(argv[1], argv[2], s, afp);
888 argc -= 2, argv += 2;
890 p->c_u.c_func(*argv, p->c_parameter, s, afp);
896 * Do any post argument processing required by the address family.
898 if (afp->af_postproc != NULL)
899 afp->af_postproc(s, afp);
901 * Do deferred callbacks registered while processing
902 * command-line arguments.
904 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
905 cb->cb_func(s, cb->cb_arg);
907 * Do deferred operations.
910 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
911 warnx("interface %s cannot change %s addresses!",
918 strlcpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
919 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
921 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
922 /* means no previous address for interface */
924 Perror("ioctl (SIOCDIFADDR)");
928 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
929 warnx("interface %s cannot change %s addresses!",
934 if (newaddr && (setaddr || setmask)) {
935 strlcpy(afp->af_addreq, name, sizeof ifr.ifr_name);
936 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
937 Perror("ioctl (SIOCAIFADDR)");
946 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
948 if (afp->af_getaddr == NULL)
951 * Delay the ioctl to set the interface addr until flags are all set.
952 * The address interpretation may depend on the flags,
953 * and the flags may change when the address is set.
956 if (doalias == 0 && afp->af_af != AF_LINK)
958 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
962 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
964 struct addrinfo *srcres, *dstres;
967 if (afp->af_settunnel == NULL) {
968 warn("address family %s does not support tunnel setup",
973 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
974 errx(1, "error in parsing address string: %s",
975 gai_strerror(ecode));
977 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
978 errx(1, "error in parsing address string: %s",
979 gai_strerror(ecode));
981 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
983 "source and destination address families do not match");
985 afp->af_settunnel(s, srcres, dstres);
987 freeaddrinfo(srcres);
988 freeaddrinfo(dstres);
993 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
996 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
997 err(1, "SIOCDIFPHYADDR");
1001 setifnetmask(const char *addr, int dummy __unused, int s,
1002 const struct afswtch *afp)
1004 if (afp->af_getaddr != NULL) {
1006 afp->af_getaddr(addr, MASK);
1011 setifbroadaddr(const char *addr, int dummy __unused, int s,
1012 const struct afswtch *afp)
1014 if (afp->af_getaddr != NULL)
1015 afp->af_getaddr(addr, DSTADDR);
1019 notealias(const char *addr, int param, int s, const struct afswtch *afp)
1021 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1022 if (setaddr && doalias == 0 && param < 0)
1023 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1024 bcopy((caddr_t)rqtosa(af_addreq),
1025 (caddr_t)rqtosa(af_ridreq),
1026 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 getifflags(const char *ifname, int us)
1049 struct ifreq my_ifr;
1052 memset(&my_ifr, 0, sizeof(struct ifreq));
1053 strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name));
1057 if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
1058 err(1, "socket(family AF_LOCAL,SOCK_DGRAM)");
1061 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0)
1062 Perror("ioctl (SIOCGIFFLAGS)");
1067 return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16));
1071 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1073 struct ifreq my_ifr;
1076 flags = getifflags(name, s);
1084 memset(&my_ifr, 0, sizeof(struct ifreq));
1085 strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1086 my_ifr.ifr_flags = flags & 0xffff;
1087 my_ifr.ifr_flagshigh = flags >> 16;
1088 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1093 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1097 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0)
1098 Perror("ioctl (SIOCGIFCAP)");
1100 flags = ifr.ifr_curcap;
1107 ifr.ifr_reqcap = flags;
1108 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1113 setifmetric(const char *val, int dummy __unused, int s,
1114 const struct afswtch *afp)
1116 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1117 ifr.ifr_metric = atoi(val);
1118 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1119 err(1, "ioctl SIOCSIFMETRIC (set metric)");
1123 setifmtu(const char *val, int dummy __unused, int s,
1124 const struct afswtch *afp)
1126 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1127 ifr.ifr_mtu = atoi(val);
1128 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1129 err(1, "ioctl SIOCSIFMTU (set mtu)");
1133 setiftsolen(const char *val, int dummy __unused, int s,
1134 const struct afswtch *afp)
1136 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1137 ifr.ifr_tsolen = atoi(val);
1138 if (ioctl(s, SIOCSIFTSOLEN, (caddr_t)&ifr) < 0)
1139 err(1, "ioctl SIOCSIFTSOLEN (set tsolen)");
1143 setifname(const char *val, int dummy __unused, int s,
1144 const struct afswtch *afp)
1148 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1150 newname = strdup(val);
1151 if (newname == NULL)
1152 err(1, "no memory to set ifname");
1153 ifr.ifr_data = newname;
1154 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1156 err(1, "ioctl SIOCSIFNAME (set name)");
1159 strlcpy(name, newname, sizeof(name));
1164 setifpollcpu(const char *val, int dummy __unused, int s,
1165 const struct afswtch *afp)
1167 warnx("pollcpu is deprecated, use polling or npolling instead");
1168 setifflags("npolling", IFF_NPOLLING, s, afp);
1172 setifdescr(const char *val, int dummy __unused, int s,
1173 const struct afswtch *afp __unused)
1177 ifr.ifr_buffer.length = strlen(val) + 1;
1178 if (ifr.ifr_buffer.length == 1) {
1179 ifr.ifr_buffer.buffer = newdescr = NULL;
1180 ifr.ifr_buffer.length = 0;
1182 newdescr = strdup(val);
1183 ifr.ifr_buffer.buffer = newdescr;
1184 if (newdescr == NULL) {
1185 warn("no memory to set ifdescr");
1190 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
1191 warn("ioctl (set descr)");
1197 unsetifdescr(const char *val, int dummy __unused, int s,
1198 const struct afswtch *afp __unused)
1200 setifdescr("", 0, s, 0);
1204 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1205 "\10NOARP\11PROMISC\12ALLMULTI\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1206 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25NPOLLING\26IDIRECT"
1209 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7RSS" \
1210 "\10VLAN_HWCSUM\11TSO"
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.length > 1)
1252 printf("\tdescription: %s\n", descr);
1253 } else if (errno == ENOMSG) {
1255 } else if (errno == ENAMETOOLONG) {
1256 descrlen = ifr.ifr_buffer.length;
1259 warn("ioctl (get descr)");
1262 warn("unable to allocate memory for interface "
1268 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1269 if (ifr.ifr_curcap != 0) {
1270 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1273 if (supmedia && ifr.ifr_reqcap != 0) {
1274 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1276 if (ifr.ifr_reqcap & IFCAP_TSO) {
1277 if (ioctl(s, SIOCGIFTSOLEN,
1278 (caddr_t)&ifr) == 0) {
1279 printf("\ttsolen %d", ifr.ifr_tsolen);
1288 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1289 if (ift->ifa_addr == NULL)
1291 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1294 const struct afswtch *p;
1295 p = af_getbyfamily(ift->ifa_addr->sa_family);
1296 if (p != NULL && p->af_status != NULL)
1297 p->af_status(s, ift);
1298 } else if (afp->af_af == ift->ifa_addr->sa_family)
1299 afp->af_status(s, ift);
1302 if (allfamilies || afp->af_af == AF_LINK) {
1303 const struct afswtch *lafp;
1306 * Hack; the link level address is received separately
1307 * from the routing information so any address is not
1308 * handled above. Cobble together an entry and invoke
1309 * the status method specially.
1311 lafp = af_getbyname("lladdr");
1313 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1314 lafp->af_status(s, &info);
1320 else if (afp->af_other_status != NULL)
1321 afp->af_other_status(s);
1323 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1324 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1325 printf("%s", ifs.ascii);
1332 tunnel_status(int s)
1334 af_all_tunnel_status(s);
1338 Perror(const char *cmd)
1343 errx(1, "%s: no such interface", cmd);
1347 errx(1, "%s: permission denied", cmd);
1356 * Print a value a la the %pb%i format of the kernel's kprintf()
1359 printb(const char *s, unsigned v, const char *bits)
1364 if (bits && *bits == 8)
1365 printf("%s=%o", s, v);
1367 printf("%s=%x", s, v);
1371 while ((i = *bits++) != '\0') {
1372 if (v & (1 << (i-1))) {
1376 for (; (c = *bits) > 32; bits++)
1379 for (; *bits > 32; bits++)
1387 ifmaybeload(const char *name)
1389 #define MOD_PREFIX_LEN 3 /* "if_" */
1390 struct module_stat mstat;
1392 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1395 /* loading suppressed by the user */
1399 /* trim the interface number off the end */
1400 strlcpy(ifname, name, sizeof(ifname));
1401 for (dp = ifname; *dp != 0; dp++)
1407 /* turn interface and unit into module name */
1408 strlcpy(ifkind, "if_", sizeof(ifkind));
1409 strlcat(ifkind, ifname, sizeof(ifkind));
1411 /* scan files in kernel */
1412 mstat.version = sizeof(struct module_stat);
1413 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1414 /* scan modules in file */
1415 for (modid = kldfirstmod(fileid); modid > 0;
1416 modid = modfnext(modid)) {
1417 if (modstat(modid, &mstat) < 0)
1419 /* strip bus name if present */
1420 if ((cp = strchr(mstat.name, '/')) != NULL) {
1425 /* already loaded? */
1426 if (strcmp(ifname, cp) == 0 ||
1427 strcmp(ifkind, cp) == 0)
1432 /* not present, we should try to load it */
1436 static struct cmd basic_cmds[] = {
1437 DEF_CMD("up", IFF_UP, setifflags),
1438 DEF_CMD("down", -IFF_UP, setifflags),
1439 DEF_CMD("arp", -IFF_NOARP, setifflags),
1440 DEF_CMD("-arp", IFF_NOARP, setifflags),
1441 DEF_CMD("debug", IFF_DEBUG, setifflags),
1442 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1443 DEF_CMD_ARG("description", setifdescr),
1444 DEF_CMD_ARG("descr", setifdescr),
1445 DEF_CMD("-description", 0, unsetifdescr),
1446 DEF_CMD("-descr", 0, unsetifdescr),
1447 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1448 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1449 DEF_CMD("add", IFF_UP, notealias),
1450 DEF_CMD("alias", IFF_UP, notealias),
1451 DEF_CMD("-alias", -IFF_UP, notealias),
1452 DEF_CMD("delete", -IFF_UP, notealias),
1453 DEF_CMD("remove", -IFF_UP, notealias),
1455 #define EN_SWABIPS 0x1000
1456 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1457 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1459 DEF_CMD_ARG("netmask", setifnetmask),
1460 DEF_CMD_ARG("metric", setifmetric),
1461 DEF_CMD_ARG("broadcast", setifbroadaddr),
1462 DEF_CMD_ARG2("tunnel", settunnel),
1463 DEF_CMD("-tunnel", 0, deletetunnel),
1464 DEF_CMD("deletetunnel", 0, deletetunnel),
1465 DEF_CMD("link0", IFF_LINK0, setifflags),
1466 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1467 DEF_CMD("link1", IFF_LINK1, setifflags),
1468 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1469 DEF_CMD("link2", IFF_LINK2, setifflags),
1470 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1471 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1472 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1473 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1474 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1475 DEF_CMD("polling", IFF_NPOLLING, setifflags),
1476 DEF_CMD("-polling", -IFF_NPOLLING, setifflags),
1477 DEF_CMD("npolling", IFF_NPOLLING, setifflags),
1478 DEF_CMD("-npolling", -IFF_NPOLLING, setifflags),
1479 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1480 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1481 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1482 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1483 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1484 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1485 DEF_CMD("rss", IFCAP_RSS, setifcap),
1486 DEF_CMD("-rss", -IFCAP_RSS, setifcap),
1487 DEF_CMD("tso", IFCAP_TSO, setifcap),
1488 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1489 DEF_CMD("normal", -IFF_LINK0, setifflags),
1490 DEF_CMD("compress", IFF_LINK0, setifflags),
1491 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1492 DEF_CMD_ARG("mtu", setifmtu),
1493 DEF_CMD_ARG("name", setifname),
1494 DEF_CMD_ARG("pollcpu", setifpollcpu),
1495 DEF_CMD_ARG("tsolen", setiftsolen)
1498 static __constructor(101) void
1503 for (i = 0; i < nitems(basic_cmds); i++)
1504 cmd_register(&basic_cmds[i]);