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.)
86 int printkeys = 0; /* Print keying material for interfaces. */
87 int printifname = 0; /* Print the name of the created interface. */
90 /* Formatter strings */
91 char *f_inet, *f_inet6, *f_ether, *f_addr;
93 static bool group_member(const char *ifname, const char *match,
95 static int ifconfig(int argc, char *const *argv, int iscreate,
96 const struct afswtch *afp);
97 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
99 static void tunnel_status(int s);
100 static void usage(void) __dead2;
102 static int getifflags(const char *ifname, int us);
103 static struct afswtch *af_getbyname(const char *name);
104 static struct afswtch *af_getbyfamily(int af);
105 static void af_other_status(int);
106 static void printifnamemaybe(void);
107 static void freeformat(void);
108 static void setformat(char *input);
110 static struct option *opts = NULL;
112 struct ifa_order_elt {
116 TAILQ_ENTRY(ifa_order_elt) link;
118 TAILQ_HEAD(ifa_queue, ifa_order_elt);
120 static int calcorders(struct ifaddrs *ifa, struct ifa_queue *q);
121 static int cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b,
122 struct ifa_queue *q);
123 typedef int (*ifaddrs_cmp)(struct ifaddrs *, struct ifaddrs *, struct ifa_queue *);
124 static struct ifaddrs *sortifaddrs(struct ifaddrs *list, ifaddrs_cmp compare,
125 struct ifa_queue *q);
129 opt_register(struct option *p)
141 /* XXX not right but close enough for now */
143 for (p = opts; p != NULL; p = p->next) {
144 strlcat(options, p->opt_usage, sizeof(options));
145 strlcat(options, " ", sizeof(options));
149 "usage: ifconfig %s[-n] [-f type:format] interface address_family\n"
150 " [address [dest_address]] [parameters]\n"
151 " ifconfig [-n] interface create\n"
152 " ifconfig [-n] interface destroy\n"
153 " ifconfig -a %s[-G nogroup] [-d | -u] [-m] [-v] [address_family]\n"
154 " ifconfig -l [-d | -u] [address_family]\n"
155 " ifconfig %s[-d | -u] [-m] [-v]\n",
156 options, options, options);
161 calcorders(struct ifaddrs *ifa, struct ifa_queue *q)
163 struct ifaddrs *prev;
164 struct ifa_order_elt *cur;
165 unsigned int ord, af, ifa_ord;
172 while (ifa != NULL) {
174 strcmp(ifa->ifa_name, prev->ifa_name) != 0) {
175 cur = calloc(1, sizeof(*cur));
179 TAILQ_INSERT_TAIL(q, cur, link);
180 cur->if_order = ifa_ord++;
186 af = ifa->ifa_addr->sa_family;
188 if (af < nitems(cur->af_orders) &&
189 cur->af_orders[af] == 0)
190 cur->af_orders[af] = ++ord;
201 cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
203 struct ifa_order_elt *cur, *e1, *e2;
204 unsigned int af1, af2;
208 if (strcmp(a->ifa_name, b->ifa_name) != 0) {
209 TAILQ_FOREACH(cur, q, link) {
210 if (e1 != NULL && e2 != NULL)
213 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0)
215 else if (strcmp(cur->ifa->ifa_name, b->ifa_name) == 0)
219 if (e1 == NULL || e2 == NULL)
222 return (e1->if_order - e2->if_order);
224 } else if (a->ifa_addr != NULL && b->ifa_addr != NULL) {
225 TAILQ_FOREACH(cur, q, link) {
226 if (strcmp(cur->ifa->ifa_name, a->ifa_name) == 0) {
235 af1 = a->ifa_addr->sa_family;
236 af2 = b->ifa_addr->sa_family;
238 if (af1 < nitems(e1->af_orders) && af2 < nitems(e1->af_orders))
239 return (e1->af_orders[af1] - e1->af_orders[af2]);
245 static struct ifaddrs *
246 sortifaddrs(struct ifaddrs *list, ifaddrs_cmp compare, struct ifa_queue *q)
248 struct ifaddrs *right, *temp, *last, *result, *next, *tail;
250 right = temp = last = list;
251 result = next = tail = NULL;
253 if (list == NULL || list->ifa_next == NULL)
256 while (temp != NULL && temp->ifa_next != NULL) {
258 right = right->ifa_next;
259 temp = temp->ifa_next->ifa_next;
262 last->ifa_next = NULL;
264 list = sortifaddrs(list, compare, q);
265 right = sortifaddrs(right, compare, q);
267 while (list != NULL || right != NULL) {
270 list = list->ifa_next;
271 } else if (list == NULL) {
273 right = right->ifa_next;
274 } else if (compare(list, right, q) <= 0) {
276 list = list->ifa_next;
279 right = right->ifa_next;
285 tail->ifa_next = next;
294 printifnamemaybe(void)
297 printf("%s\n", name);
314 setformat(char *input)
316 char *formatstr, *category, *modifier;
319 formatstr = strdup(input);
320 if (formatstr == NULL)
321 err(1, "no memory to set format");
323 while ((category = strsep(&formatstr, ",")) != NULL) {
324 modifier = strchr(category, ':');
325 if (modifier == NULL || modifier[1] == '\0') {
326 warnx("skip invalid format specification: %s\n",
335 if (strcmp(category, "addr") == 0)
337 else if (strcmp(category, "ether") == 0)
339 else if (strcmp(category, "inet") == 0)
341 else if (strcmp(category, "inet6") == 0)
345 *fp = strdup(modifier);
356 main(int argc, char *argv[])
358 int c, all, namesonly, downonly, uponly;
360 const struct afswtch *afp = NULL;
361 const struct sockaddr_dl *sdl;
362 const char *ifname, *matchgroup, *nogroup;
363 struct ifa_order_elt *cur, *tmp;
364 struct ifa_queue q = TAILQ_HEAD_INITIALIZER(q);
365 struct ifaddrs *ifap, *sifap, *ifa;
369 char *envformat, *cp;
372 all = downonly = uponly = namesonly = verbose = noload = 0;
373 f_inet = f_inet6 = f_ether = f_addr = NULL;
374 matchgroup = nogroup = NULL;
377 * Ensure we print interface name when expected to,
378 * even if we terminate early due to error.
380 atexit(printifnamemaybe);
382 envformat = getenv("IFCONFIG_FORMAT");
383 if (envformat != NULL)
384 setformat(envformat);
386 /* Parse leading line options */
387 strlcpy(options, "adf:G:klmnuv", sizeof(options));
388 for (p = opts; p != NULL; p = p->next)
389 strlcat(options, p->opt, sizeof(options));
390 while ((c = getopt(argc, argv, options)) != -1) {
392 case 'a': /* scan all interfaces */
395 case 'd': /* restrict scan to "down" interfaces */
409 case 'l': /* scan interface names only */
412 case 'm': /* show media choices in status */
415 case 'n': /* suppress module loading */
418 case 'u': /* restrict scan to "up" interfaces */
429 /* FALLTHROUGH (for ifgroup) */
431 for (p = opts; p != NULL; p = p->next)
432 if (p->opt[0] == c) {
444 /* -l cannot be used with -a or -m */
445 if (namesonly && (all || supmedia))
449 if (uponly && downonly)
452 /* no arguments is equivalent to '-a' */
453 if (!namesonly && argc < 1)
456 /* -a and -l allow an address family arg to limit the output */
457 if (all || namesonly) {
464 afp = af_getbyname(*argv);
467 if (afp->af_name != NULL)
469 /* leave with afp non-zero */
472 /* not listing, need an argument */
479 /* check and maybe load support for this interface */
482 ifindex = if_nametoindex(ifname);
485 * NOTE: We must special-case the `create' command
486 * right here as we would otherwise fail when trying
487 * to find the interface.
489 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
490 strcmp(argv[0], "plumb") == 0)) {
491 iflen = strlcpy(name, ifname, sizeof(name));
492 if (iflen >= sizeof(name))
493 errx(1, "%s: cloning name too long",
495 ifconfig(argc, argv, 1, NULL);
498 errx(1, "interface %s does not exist", ifname);
501 * Do not allow to use `create` command as hostname
502 * if address family is not specified.
504 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
505 strcmp(argv[0], "plumb") == 0)) {
507 errx(1, "interface %s alreay exists",
514 /* Check for address family */
516 afp = af_getbyname(*argv);
522 * Check for a requested configuration action on a single interface,
523 * which doesn't require building, sorting, and searching the entire
524 * system address list.
526 if (argc > 0 && ifname != NULL) {
527 iflen = strlcpy(name, ifname, sizeof(name));
528 if (iflen >= sizeof(name))
529 errx(1, "%s: cloning name too long", ifname);
531 flags = getifflags(name, -1);
532 if (!((downonly && (flags & IFF_UP) != 0) ||
533 (uponly && (flags & IFF_UP) == 0))) {
534 ifconfig(argc, argv, 0, afp);
540 if (getifaddrs(&ifap) != 0)
541 err(1, "getifaddrs");
542 if (calcorders(ifap, &q) != 0)
543 err(1, "calcorders");
544 sifap = sortifaddrs(ifap, cmpifaddrs, &q);
546 TAILQ_FOREACH_MUTABLE(cur, &q, link, tmp)
551 for (ifa = sifap; ifa != NULL; ifa = ifa->ifa_next) {
552 memset(&paifr, 0, sizeof(paifr));
553 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
554 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
555 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
556 ifa->ifa_addr->sa_len);
559 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
561 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0)
563 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
564 if (iflen >= sizeof(name)) {
565 warnx("%s: interface name too long, skipping",
571 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
573 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
575 if (!group_member(ifa->ifa_name, matchgroup, nogroup))
578 if (ifa->ifa_addr->sa_family == AF_LINK)
579 sdl = (const struct sockaddr_dl *)ifa->ifa_addr;
583 /* Are we just listing the interfaces? */
586 afp->af_af != AF_LINK ||
587 (sdl != NULL && sdl->sdl_type == IFT_ETHER)) {
588 printf("%s%s", (ifindex > 0 ? " " : ""), name);
595 ifconfig(argc, argv, 0, afp);
597 status(afp, sdl, ifa);
611 * Returns true if an interface should be listed because any its groups
612 * matches shell pattern "match" and none of groups matches pattern "nomatch".
613 * If any pattern is NULL, corresponding condition is skipped.
616 group_member(const char *ifname, const char *match, const char *nomatch)
618 static int sock = -1;
620 struct ifgroupreq ifgr;
623 bool matched, nomatched;
626 if (match == NULL && nomatch == NULL)
631 memset(&ifgr, 0, sizeof(ifgr));
632 strlcpy(ifgr.ifgr_name, ifname, sizeof(ifgr.ifgr_name));
634 /* The socket is opened once. Let _exit() close it. */
636 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
638 errx(1, "%s: socket(AF_LOCAL,SOCK_DGRAM)", __func__);
641 /* Determine amount of memory for the list of groups. */
642 if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
643 if (errno == EINVAL || errno == ENOTTY)
646 errx(1, "%s: SIOCGIFGROUP", __func__);
649 /* Obtain the list of groups. */
652 (struct ifg_req *)calloc(len / sizeof(*ifg), sizeof(*ifg));
653 if (ifgr.ifgr_groups == NULL)
654 errx(1, "%s: no memory", __func__);
655 if (ioctl(sock, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
656 errx(1, "%s: SIOCGIFGROUP", __func__);
658 /* Perform matching. */
661 for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(*ifg); ifg++) {
662 len -= sizeof(struct ifg_req);
664 matched |= !fnmatch(match, ifg->ifgrq_group, 0);
666 nomatched &= fnmatch(nomatch, ifg->ifgrq_group, 0);
669 if (match && !nomatch)
671 if (!match && nomatch)
673 return (matched && nomatched);
677 static struct afswtch *afs = NULL;
680 af_register(struct afswtch *p)
686 static struct afswtch *
687 af_getbyname(const char *name)
691 for (afp = afs; afp != NULL; afp = afp->af_next)
692 if (strcmp(afp->af_name, name) == 0)
697 static struct afswtch *
698 af_getbyfamily(int af)
702 for (afp = afs; afp != NULL; afp = afp->af_next)
703 if (afp->af_af == af)
709 af_other_status(int s)
712 uint8_t afmask[howmany(AF_MAX, NBBY)];
714 memset(afmask, 0, sizeof(afmask));
715 for (afp = afs; afp != NULL; afp = afp->af_next) {
716 if (afp->af_other_status == NULL)
718 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
720 afp->af_other_status(s);
721 setbit(afmask, afp->af_af);
726 af_all_tunnel_status(int s)
729 uint8_t afmask[howmany(AF_MAX, NBBY)];
731 memset(afmask, 0, sizeof(afmask));
732 for (afp = afs; afp != NULL; afp = afp->af_next) {
733 if (afp->af_status_tunnel == NULL)
735 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
737 afp->af_status_tunnel(s);
738 setbit(afmask, afp->af_af);
742 static struct cmd *cmds = NULL;
745 cmd_register(struct cmd *p)
751 static const struct cmd *
752 cmd_lookup(const char *name, int iscreate)
756 for (p = cmds; p != NULL; p = p->c_next) {
757 if (strcmp(name, p->c_name) == 0) {
772 callback_func *cb_func;
774 struct callback *cb_next;
776 static struct callback *callbacks = NULL;
779 callback_register(callback_func *func, void *arg)
783 cb = malloc(sizeof(struct callback));
785 errx(1, "unable to allocate memory for callback");
788 cb->cb_next = callbacks;
792 /* specially-handled commands */
793 static void setifaddr(const char *, int, int, const struct afswtch *);
794 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
796 static void setifdstaddr(const char *, int, int, const struct afswtch *);
797 static const struct cmd setifdstaddr_cmd =
798 DEF_CMD("ifdstaddr", 0, setifdstaddr);
801 ifconfig(int argc, char *const *argv, int iscreate,
802 const struct afswtch *uafp)
804 const struct afswtch *afp, *nafp;
808 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
809 afp = uafp != NULL ? uafp : af_getbyname("inet");
811 ifr.ifr_addr.sa_family =
812 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
813 AF_LOCAL : afp->af_af;
815 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
816 (uafp != NULL || errno != EAFNOSUPPORT ||
817 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
818 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
823 p = cmd_lookup(*argv, iscreate);
825 if (iscreate && p == NULL) {
827 * Push the clone create callback so the new
828 * device is created and can be used for any
829 * remaining arguments.
833 errx(1, "internal error, no callback");
834 callbacks = cb->cb_next;
835 cb->cb_func(s, cb->cb_arg);
839 * After cloning, make sure we have an up-to-date name
842 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
845 * Handle any address family spec that
846 * immediately follows and potentially
847 * recreate the socket.
849 nafp = af_getbyname(*argv);
859 * Look for a normal parameter.
865 * Not a recognized command, choose between setting
866 * the interface address and the dst address.
868 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
870 if (p->c_u.c_func || p->c_u.c_func2) {
871 if (p->c_parameter == NEXTARG) {
873 errx(1, "'%s' requires argument",
875 p->c_u.c_func(argv[1], 0, s, afp);
877 } else if (p->c_parameter == OPTARG) {
878 p->c_u.c_func(argv[1], 0, s, afp);
881 } else if (p->c_parameter == NEXTARG2) {
883 errx(1, "'%s' requires 2 arguments",
885 p->c_u.c_func2(argv[1], argv[2], s, afp);
886 argc -= 2, argv += 2;
888 p->c_u.c_func(*argv, p->c_parameter, s, afp);
894 * Do any post argument processing required by the address family.
896 if (afp->af_postproc != NULL)
897 afp->af_postproc(s, afp);
899 * Do deferred callbacks registered while processing
900 * command-line arguments.
902 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
903 cb->cb_func(s, cb->cb_arg);
905 * Do deferred operations.
908 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
909 warnx("interface %s cannot change %s addresses!",
916 strlcpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
917 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
919 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
920 /* means no previous address for interface */
922 Perror("ioctl (SIOCDIFADDR)");
926 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
927 warnx("interface %s cannot change %s addresses!",
932 if (newaddr && (setaddr || setmask)) {
933 strlcpy(afp->af_addreq, name, sizeof ifr.ifr_name);
934 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
935 Perror("ioctl (SIOCAIFADDR)");
944 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
946 if (afp->af_getaddr == NULL)
949 * Delay the ioctl to set the interface addr until flags are all set.
950 * The address interpretation may depend on the flags,
951 * and the flags may change when the address is set.
954 if (doalias == 0 && afp->af_af != AF_LINK)
956 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
960 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
962 struct addrinfo *srcres, *dstres;
965 if (afp->af_settunnel == NULL) {
966 warn("address family %s does not support tunnel setup",
971 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
972 errx(1, "error in parsing address string: %s",
973 gai_strerror(ecode));
975 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
976 errx(1, "error in parsing address string: %s",
977 gai_strerror(ecode));
979 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
981 "source and destination address families do not match");
983 afp->af_settunnel(s, srcres, dstres);
985 freeaddrinfo(srcres);
986 freeaddrinfo(dstres);
991 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
994 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
995 err(1, "SIOCDIFPHYADDR");
999 setifnetmask(const char *addr, int dummy __unused, int s,
1000 const struct afswtch *afp)
1002 if (afp->af_getaddr != NULL) {
1004 afp->af_getaddr(addr, MASK);
1009 setifbroadaddr(const char *addr, int dummy __unused, int s,
1010 const struct afswtch *afp)
1012 if (afp->af_getaddr != NULL)
1013 afp->af_getaddr(addr, DSTADDR);
1017 notealias(const char *addr, int param, int s, const struct afswtch *afp)
1019 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
1020 if (setaddr && doalias == 0 && param < 0)
1021 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
1022 bcopy((caddr_t)rqtosa(af_addreq),
1023 (caddr_t)rqtosa(af_ridreq),
1024 rqtosa(af_addreq)->sa_len);
1037 setifdstaddr(const char *addr, int param __unused, int s,
1038 const struct afswtch *afp)
1040 if (afp->af_getaddr != NULL)
1041 afp->af_getaddr(addr, DSTADDR);
1045 getifflags(const char *ifname, int us)
1047 struct ifreq my_ifr;
1050 memset(&my_ifr, 0, sizeof(struct ifreq));
1051 strlcpy(my_ifr.ifr_name, ifname, sizeof(my_ifr.ifr_name));
1055 if ((s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)
1056 err(1, "socket(family AF_LOCAL,SOCK_DGRAM)");
1059 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0)
1060 Perror("ioctl (SIOCGIFFLAGS)");
1065 return ((my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16));
1069 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
1071 struct ifreq my_ifr;
1074 flags = getifflags(name, s);
1082 memset(&my_ifr, 0, sizeof(struct ifreq));
1083 strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
1084 my_ifr.ifr_flags = flags & 0xffff;
1085 my_ifr.ifr_flagshigh = flags >> 16;
1086 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
1091 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
1095 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0)
1096 Perror("ioctl (SIOCGIFCAP)");
1098 flags = ifr.ifr_curcap;
1105 ifr.ifr_reqcap = flags;
1106 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
1111 setifmetric(const char *val, int dummy __unused, int s,
1112 const struct afswtch *afp)
1114 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1115 ifr.ifr_metric = atoi(val);
1116 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
1117 err(1, "ioctl SIOCSIFMETRIC (set metric)");
1121 setifmtu(const char *val, int dummy __unused, int s,
1122 const struct afswtch *afp)
1124 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1125 ifr.ifr_mtu = atoi(val);
1126 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
1127 err(1, "ioctl SIOCSIFMTU (set mtu)");
1131 setiftsolen(const char *val, int dummy __unused, int s,
1132 const struct afswtch *afp)
1134 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
1135 ifr.ifr_tsolen = atoi(val);
1136 if (ioctl(s, SIOCSIFTSOLEN, (caddr_t)&ifr) < 0)
1137 err(1, "ioctl SIOCSIFTSOLEN (set tsolen)");
1141 setifname(const char *val, int dummy __unused, int s,
1142 const struct afswtch *afp)
1146 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1148 newname = strdup(val);
1149 if (newname == NULL)
1150 err(1, "no memory to set ifname");
1151 ifr.ifr_data = newname;
1152 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
1154 err(1, "ioctl SIOCSIFNAME (set name)");
1157 strlcpy(name, newname, sizeof(name));
1162 setifpollcpu(const char *val, int dummy __unused, int s,
1163 const struct afswtch *afp)
1165 warnx("pollcpu is deprecated, use polling or npolling instead");
1166 setifflags("npolling", IFF_NPOLLING, s, afp);
1171 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
1172 "\10NOARP\11PROMISC\12ALLMULTI\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
1173 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25NPOLLING\26IDIRECT"
1176 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7RSS" \
1177 "\10VLAN_HWCSUM\11TSO"
1180 * Print the status of the interface. If an address family was
1181 * specified, show only it; otherwise, show them all.
1184 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
1185 struct ifaddrs *ifa)
1187 struct ifaddrs *ift;
1193 ifr.ifr_addr.sa_family = AF_LOCAL;
1196 ifr.ifr_addr.sa_family =
1197 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
1199 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1201 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
1203 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
1205 printf("%s: ", name);
1206 printb("flags", ifa->ifa_flags, IFFBITS);
1207 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
1208 printf(" metric %d", ifr.ifr_metric);
1209 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
1210 printf(" mtu %d", ifr.ifr_mtu);
1213 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
1214 if (ifr.ifr_curcap != 0) {
1215 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
1218 if (supmedia && ifr.ifr_reqcap != 0) {
1219 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
1221 if (ifr.ifr_reqcap & IFCAP_TSO) {
1222 if (ioctl(s, SIOCGIFTSOLEN,
1223 (caddr_t)&ifr) == 0) {
1224 printf("\ttsolen %d", ifr.ifr_tsolen);
1233 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
1234 if (ift->ifa_addr == NULL)
1236 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
1239 const struct afswtch *p;
1240 p = af_getbyfamily(ift->ifa_addr->sa_family);
1241 if (p != NULL && p->af_status != NULL)
1242 p->af_status(s, ift);
1243 } else if (afp->af_af == ift->ifa_addr->sa_family)
1244 afp->af_status(s, ift);
1247 if (allfamilies || afp->af_af == AF_LINK) {
1248 const struct afswtch *lafp;
1251 * Hack; the link level address is received separately
1252 * from the routing information so any address is not
1253 * handled above. Cobble together an entry and invoke
1254 * the status method specially.
1256 lafp = af_getbyname("lladdr");
1258 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
1259 lafp->af_status(s, &info);
1265 else if (afp->af_other_status != NULL)
1266 afp->af_other_status(s);
1268 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
1269 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
1270 printf("%s", ifs.ascii);
1277 tunnel_status(int s)
1279 af_all_tunnel_status(s);
1283 Perror(const char *cmd)
1288 errx(1, "%s: no such interface", cmd);
1292 errx(1, "%s: permission denied", cmd);
1301 * Print a value a la the %pb%i format of the kernel's kprintf()
1304 printb(const char *s, unsigned v, const char *bits)
1309 if (bits && *bits == 8)
1310 printf("%s=%o", s, v);
1312 printf("%s=%x", s, v);
1316 while ((i = *bits++) != '\0') {
1317 if (v & (1 << (i-1))) {
1321 for (; (c = *bits) > 32; bits++)
1324 for (; *bits > 32; bits++)
1332 ifmaybeload(const char *name)
1334 #define MOD_PREFIX_LEN 3 /* "if_" */
1335 struct module_stat mstat;
1337 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1340 /* loading suppressed by the user */
1344 /* trim the interface number off the end */
1345 strlcpy(ifname, name, sizeof(ifname));
1346 for (dp = ifname; *dp != 0; dp++)
1352 /* turn interface and unit into module name */
1353 strlcpy(ifkind, "if_", sizeof(ifkind));
1354 strlcat(ifkind, ifname, sizeof(ifkind));
1356 /* scan files in kernel */
1357 mstat.version = sizeof(struct module_stat);
1358 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1359 /* scan modules in file */
1360 for (modid = kldfirstmod(fileid); modid > 0;
1361 modid = modfnext(modid)) {
1362 if (modstat(modid, &mstat) < 0)
1364 /* strip bus name if present */
1365 if ((cp = strchr(mstat.name, '/')) != NULL) {
1370 /* already loaded? */
1371 if (strcmp(ifname, cp) == 0 ||
1372 strcmp(ifkind, cp) == 0)
1377 /* not present, we should try to load it */
1381 static struct cmd basic_cmds[] = {
1382 DEF_CMD("up", IFF_UP, setifflags),
1383 DEF_CMD("down", -IFF_UP, setifflags),
1384 DEF_CMD("arp", -IFF_NOARP, setifflags),
1385 DEF_CMD("-arp", IFF_NOARP, setifflags),
1386 DEF_CMD("debug", IFF_DEBUG, setifflags),
1387 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1388 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1389 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1390 DEF_CMD("add", IFF_UP, notealias),
1391 DEF_CMD("alias", IFF_UP, notealias),
1392 DEF_CMD("-alias", -IFF_UP, notealias),
1393 DEF_CMD("delete", -IFF_UP, notealias),
1394 DEF_CMD("remove", -IFF_UP, notealias),
1396 #define EN_SWABIPS 0x1000
1397 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1398 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1400 DEF_CMD_ARG("netmask", setifnetmask),
1401 DEF_CMD_ARG("metric", setifmetric),
1402 DEF_CMD_ARG("broadcast", setifbroadaddr),
1403 DEF_CMD_ARG2("tunnel", settunnel),
1404 DEF_CMD("-tunnel", 0, deletetunnel),
1405 DEF_CMD("deletetunnel", 0, deletetunnel),
1406 DEF_CMD("link0", IFF_LINK0, setifflags),
1407 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1408 DEF_CMD("link1", IFF_LINK1, setifflags),
1409 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1410 DEF_CMD("link2", IFF_LINK2, setifflags),
1411 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1412 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1413 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1414 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1415 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1416 DEF_CMD("polling", IFF_NPOLLING, setifflags),
1417 DEF_CMD("-polling", -IFF_NPOLLING, setifflags),
1418 DEF_CMD("npolling", IFF_NPOLLING, setifflags),
1419 DEF_CMD("-npolling", -IFF_NPOLLING, setifflags),
1420 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1421 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1422 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1423 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1424 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1425 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1426 DEF_CMD("rss", IFCAP_RSS, setifcap),
1427 DEF_CMD("-rss", -IFCAP_RSS, setifcap),
1428 DEF_CMD("tso", IFCAP_TSO, setifcap),
1429 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1430 DEF_CMD("normal", -IFF_LINK0, setifflags),
1431 DEF_CMD("compress", IFF_LINK0, setifflags),
1432 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1433 DEF_CMD_ARG("mtu", setifmtu),
1434 DEF_CMD_ARG("name", setifname),
1435 DEF_CMD_ARG("pollcpu", setifpollcpu),
1436 DEF_CMD_ARG("tsolen", setiftsolen)
1439 static __constructor(101) void
1444 for (i = 0; i < nitems(basic_cmds); i++)
1445 cmd_register(&basic_cmds[i]);