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>
41 #include <net/ethernet.h>
43 #include <net/if_var.h>
44 #include <net/if_dl.h>
45 #include <net/if_types.h>
46 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <arpa/inet.h>
66 * This macro returns the size of a struct sockaddr when passed
67 * through a routing socket. Basically we round up sa_len to
68 * a multiple of sizeof(long), with a minimum of sizeof(long).
69 * The check for a NULL pointer is just a convenience, probably never used.
70 * The case sa_len == 0 should only apply to empty structures.
73 ( (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ? \
75 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) )
78 * Since "struct ifreq" is composed of various union members, callers
79 * should pay special attention to interprete the value.
80 * (.e.g. little/big endian difference in the structure.)
95 int printkeys = 0; /* Print keying material for interfaces. */
96 int printname = 0; /* Print the name of the created interface. */
98 static int ifconfig(int argc, char *const *argv, int, const struct afswtch *afp);
99 static void status(const struct afswtch *afp, int addrcount,
100 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
101 struct ifa_msghdr *ifam);
102 static void tunnel_status(int s);
103 static void usage(void) __dead2;
105 static struct afswtch *af_getbyname(const char *name);
106 static struct afswtch *af_getbyfamily(int af);
107 static void af_other_status(int);
109 static struct option *opts = NULL;
112 opt_register(struct option *p)
124 /* XXX not right but close enough for now */
126 for (p = opts; p != NULL; p = p->next) {
127 strlcat(options, p->opt_usage, sizeof(options));
128 strlcat(options, " ", sizeof(options));
132 "usage: ifconfig %sinterface address_family [address [dest_address]]\n"
134 " ifconfig interface create\n"
135 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
136 " ifconfig -l [-d] [-u] [address_family]\n"
137 " ifconfig %s[-d] [-m] [-u] [-v]\n",
138 options, options, options);
143 main(int argc, char *argv[])
145 int c, all, namesonly, downonly, uponly;
146 int need_nl = 0, count = 0;
147 const struct afswtch *afp = NULL;
148 int addrcount, ifindex;
149 struct if_msghdr *ifm, *nextifm;
150 struct ifa_msghdr *ifam;
151 struct sockaddr_dl *sdl;
152 char *buf, *lim, *next;
160 all = downonly = uponly = namesonly = verbose = 0;
162 /* Parse leading line options */
163 strlcpy(options, "adklmuv", sizeof(options));
164 for (p = opts; p != NULL; p = p->next)
165 strlcat(options, p->opt, sizeof(options));
166 while ((c = getopt(argc, argv, options)) != -1) {
168 case 'a': /* scan all interfaces */
171 case 'd': /* restrict scan to "down" interfaces */
177 case 'l': /* scan interface names only */
180 case 'm': /* show media choices in status */
183 case 'u': /* restrict scan to "up" interfaces */
190 for (p = opts; p != NULL; p = p->next)
191 if (p->opt[0] == c) {
203 /* -l cannot be used with -a or -m */
204 if (namesonly && (all || supmedia))
208 if (uponly && downonly)
211 /* no arguments is equivalent to '-a' */
212 if (!namesonly && argc < 1)
215 /* -a and -l allow an address family arg to limit the output */
216 if (all || namesonly) {
223 afp = af_getbyname(*argv);
226 if (afp->af_name != NULL)
228 /* leave with afp non-zero */
231 /* not listing, need an argument */
238 /* check and maybe load support for this interface */
240 ifindex = if_nametoindex(ifname);
243 * NOTE: We must special-case the `create' command
244 * right here as we would otherwise fail when trying
245 * to find the interface.
247 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
248 strcmp(argv[0], "plumb") == 0)) {
249 iflen = strlcpy(name, ifname, sizeof(name));
250 if (iflen >= sizeof(name))
251 errx(1, "%s: cloning name too long",
253 ifconfig(argc, argv, 1, NULL);
256 errx(1, "interface %s does not exist", ifname);
260 /* Check for address family */
262 afp = af_getbyname(*argv);
271 mib[3] = 0; /* address family */
272 mib[4] = NET_RT_IFLIST;
273 mib[5] = ifindex; /* interface index */
275 /* if particular family specified, only ask about it */
279 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
280 errx(1, "iflist-sysctl-estimate");
281 if ((buf = malloc(needed)) == NULL)
283 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
284 if (errno == ENOMEM && count++ < 10) {
285 warnx("Routing table grew, retrying");
290 errx(1, "actual retrieval of interface table");
298 ifm = (struct if_msghdr *)next;
300 if (ifm->ifm_type == RTM_IFINFO) {
302 if (ifm->ifm_data.ifi_datalen == 0)
303 ifm->ifm_data.ifi_datalen = sizeof(struct if_data);
304 sdl = (struct sockaddr_dl *)((char *)ifm + sizeof(struct if_msghdr) -
305 sizeof(struct if_data) + ifm->ifm_data.ifi_datalen);
307 sdl = (struct sockaddr_dl *)(ifm + 1);
309 flags = ifm->ifm_flags;
311 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
312 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
314 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
315 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
320 next += ifm->ifm_msglen;
325 nextifm = (struct if_msghdr *)next;
327 if (nextifm->ifm_type != RTM_NEWADDR)
331 ifam = (struct ifa_msghdr *)nextifm;
334 next += nextifm->ifm_msglen;
337 if (sizeof(name) <= sdl->sdl_nlen)
338 name_len = sizeof(name) - 1;
340 name_len = sdl->sdl_nlen;
342 memcpy(name, sdl->sdl_data, name_len);
343 name[name_len] = '\0';
345 if (all || namesonly) {
347 if ((flags & IFF_UP) == 0)
348 continue; /* not up */
351 continue; /* not down */
353 if (afp == NULL || afp->af_af != AF_LINK ||
354 sdl->sdl_type == IFT_ETHER) {
365 ifconfig(argc, argv, 0, afp);
367 status(afp, addrcount, sdl, ifm, ifam);
371 if (namesonly && need_nl > 0)
374 printf("%s\n", name);
379 static struct afswtch *afs = NULL;
382 af_register(struct afswtch *p)
388 static struct afswtch *
389 af_getbyname(const char *name)
393 for (afp = afs; afp != NULL; afp = afp->af_next)
394 if (strcmp(afp->af_name, name) == 0)
399 static struct afswtch *
400 af_getbyfamily(int af)
404 for (afp = afs; afp != NULL; afp = afp->af_next)
405 if (afp->af_af == af)
411 af_other_status(int s)
414 uint8_t afmask[howmany(AF_MAX, NBBY)];
416 memset(afmask, 0, sizeof(afmask));
417 for (afp = afs; afp != NULL; afp = afp->af_next) {
418 if (afp->af_other_status == NULL)
420 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
422 afp->af_other_status(s);
423 setbit(afmask, afp->af_af);
428 af_all_tunnel_status(int s)
431 uint8_t afmask[howmany(AF_MAX, NBBY)];
433 memset(afmask, 0, sizeof(afmask));
434 for (afp = afs; afp != NULL; afp = afp->af_next) {
435 if (afp->af_status_tunnel == NULL)
437 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
439 afp->af_status_tunnel(s);
440 setbit(afmask, afp->af_af);
444 static struct cmd *cmds = NULL;
447 cmd_register(struct cmd *p)
453 static const struct cmd *
454 cmd_lookup(const char *name, int iscreate)
456 #define N(a) (sizeof(a)/sizeof(a[0]))
459 for (p = cmds; p != NULL; p = p->c_next)
460 if (strcmp(name, p->c_name) == 0) {
474 callback_func *cb_func;
476 struct callback *cb_next;
478 static struct callback *callbacks = NULL;
481 callback_register(callback_func *func, void *arg)
485 cb = malloc(sizeof(struct callback));
487 errx(1, "unable to allocate memory for callback");
490 cb->cb_next = callbacks;
494 /* specially-handled commands */
495 static void setifaddr(const char *, int, int, const struct afswtch *);
496 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
498 static void setifdstaddr(const char *, int, int, const struct afswtch *);
499 static const struct cmd setifdstaddr_cmd =
500 DEF_CMD("ifdstaddr", 0, setifdstaddr);
503 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
505 const struct afswtch *afp, *nafp;
509 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
510 afp = uafp != NULL ? uafp : af_getbyname("inet");
512 ifr.ifr_addr.sa_family =
513 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
514 AF_INET : afp->af_af;
516 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
517 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
522 p = cmd_lookup(*argv, iscreate);
524 if (iscreate && p == NULL) {
526 * Push the clone create callback so the new
527 * device is created and can be used for any
528 * remaining arguments.
532 errx(1, "internal error, no callback");
533 callbacks = cb->cb_next;
534 cb->cb_func(s, cb->cb_arg);
537 * Handle any address family spec that
538 * immediately follows and potentially
539 * recreate the socket.
541 nafp = af_getbyname(*argv);
551 * Look for a normal parameter.
557 * Not a recognized command, choose between setting
558 * the interface address and the dst address.
560 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
562 if (p->c_u.c_func || p->c_u.c_func2) {
563 if (p->c_parameter == NEXTARG) {
565 errx(1, "'%s' requires argument",
567 p->c_u.c_func(argv[1], 0, s, afp);
569 } else if (p->c_parameter == OPTARG) {
570 p->c_u.c_func(argv[1], 0, s, afp);
573 } else if (p->c_parameter == NEXTARG2) {
575 errx(1, "'%s' requires 2 arguments",
577 p->c_u.c_func2(argv[1], argv[2], s, afp);
578 argc -= 2, argv += 2;
580 p->c_u.c_func(*argv, p->c_parameter, s, afp);
586 * Do any post argument processing required by the address family.
588 if (afp->af_postproc != NULL)
589 afp->af_postproc(s, afp);
591 * Do deferred callbacks registered while processing
592 * command-line arguments.
594 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
595 cb->cb_func(s, cb->cb_arg);
597 * Do deferred operations.
600 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
601 warnx("interface %s cannot change %s addresses!",
608 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
609 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
611 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
612 /* means no previous address for interface */
614 Perror("ioctl (SIOCDIFADDR)");
618 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
619 warnx("interface %s cannot change %s addresses!",
624 if (newaddr && (setaddr || setmask)) {
625 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
626 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
627 Perror("ioctl (SIOCAIFADDR)");
636 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
638 if (afp->af_getaddr == NULL)
641 * Delay the ioctl to set the interface addr until flags are all set.
642 * The address interpretation may depend on the flags,
643 * and the flags may change when the address is set.
646 if (doalias == 0 && afp->af_af != AF_LINK)
648 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
652 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
654 struct addrinfo *srcres, *dstres;
657 if (afp->af_settunnel == NULL) {
658 warn("address family %s does not support tunnel setup",
663 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
664 errx(1, "error in parsing address string: %s",
665 gai_strerror(ecode));
667 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
668 errx(1, "error in parsing address string: %s",
669 gai_strerror(ecode));
671 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
673 "source and destination address families do not match");
675 afp->af_settunnel(s, srcres, dstres);
677 freeaddrinfo(srcres);
678 freeaddrinfo(dstres);
683 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
686 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
687 err(1, "SIOCDIFPHYADDR");
691 setifnetmask(const char *addr, int dummy __unused, int s,
692 const struct afswtch *afp)
694 if (afp->af_getaddr != NULL) {
696 afp->af_getaddr(addr, MASK);
701 setifbroadaddr(const char *addr, int dummy __unused, int s,
702 const struct afswtch *afp)
704 if (afp->af_getaddr != NULL)
705 afp->af_getaddr(addr, DSTADDR);
709 setifipdst(const char *addr, int dummy __unused, int s,
710 const struct afswtch *afp)
712 const struct afswtch *inet;
714 inet = af_getbyname("inet");
717 inet->af_getaddr(addr, DSTADDR);
724 notealias(const char *addr, int param, int s, const struct afswtch *afp)
726 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
727 if (setaddr && doalias == 0 && param < 0)
728 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
729 bcopy((caddr_t)rqtosa(af_addreq),
730 (caddr_t)rqtosa(af_ridreq),
731 rqtosa(af_addreq)->sa_len);
743 setifdstaddr(const char *addr, int param __unused, int s,
744 const struct afswtch *afp)
746 if (afp->af_getaddr != NULL)
747 afp->af_getaddr(addr, DSTADDR);
751 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
752 * of the ifreq structure, which may confuse other parts of ifconfig.
753 * Make a private copy so we can avoid that.
756 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
760 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
762 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
763 Perror("ioctl (SIOCGIFFLAGS)");
766 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
767 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
774 my_ifr.ifr_flags = flags & 0xffff;
775 my_ifr.ifr_flagshigh = flags >> 16;
776 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
781 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
784 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
785 Perror("ioctl (SIOCGIFCAP)");
788 flags = ifr.ifr_curcap;
794 ifr.ifr_reqcap = flags;
795 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
800 setifmetric(const char *val, int dummy __unused, int s,
801 const struct afswtch *afp)
803 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
804 ifr.ifr_metric = atoi(val);
805 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
806 warn("ioctl (set metric)");
810 setifmtu(const char *val, int dummy __unused, int s,
811 const struct afswtch *afp)
813 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
814 ifr.ifr_mtu = atoi(val);
815 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
816 warn("ioctl (set mtu)");
820 setiftsolen(const char *val, int dummy __unused, int s,
821 const struct afswtch *afp)
823 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
824 ifr.ifr_tsolen = atoi(val);
825 if (ioctl(s, SIOCSIFTSOLEN, (caddr_t)&ifr) < 0)
826 warn("ioctl (set tsolen)");
830 setifname(const char *val, int dummy __unused, int s,
831 const struct afswtch *afp)
835 newname = strdup(val);
836 if (newname == NULL) {
837 warn("no memory to set ifname");
840 ifr.ifr_data = newname;
841 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
842 warn("ioctl (set name)");
846 strlcpy(name, newname, sizeof(name));
850 * Even if we just created the interface, we don't need to print
851 * its name because we just nailed it down separately.
857 setifpollcpu(const char *val, int dummy __unused, int s,
858 const struct afswtch *afp)
860 warnx("pollcpu is deprecated, use polling or npolling instead");
861 setifflags("npolling", IFF_NPOLLING, s, afp);
865 * Expand the compacted form of addresses as returned via the
866 * configuration read via sysctl().
869 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
874 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
875 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
876 if ((rtinfo->rti_addrs & (1 << i)) == 0)
878 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
884 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
885 "\10NOARP\11PROMISC\12ALLMULTI\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
886 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25NPOLLING"
889 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7RSS" \
890 "\10VLAN_HWCSUM\11TSO"
893 * Print the status of the interface. If an address family was
894 * specified, show only it; otherwise, show them all.
897 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
898 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
900 struct rt_addrinfo info;
906 afp = af_getbyname("inet");
910 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
911 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
913 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
915 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
917 printf("%s: ", name);
918 printb("flags", flags, IFFBITS);
919 if (ifm->ifm_data.ifi_metric)
920 printf(" metric %ld", ifm->ifm_data.ifi_metric);
921 if (ifm->ifm_data.ifi_mtu)
922 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
925 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
926 if (ifr.ifr_curcap != 0) {
927 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
930 if (supmedia && ifr.ifr_reqcap != 0) {
931 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
933 if (ifr.ifr_reqcap & IFCAP_TSO) {
934 if (ioctl(s, SIOCGIFTSOLEN,
935 (caddr_t)&ifr) == 0) {
936 printf("\ttsolen %d", ifr.ifr_tsolen);
945 while (addrcount > 0) {
946 info.rti_addrs = ifam->ifam_addrs;
947 /* Expand the compacted addresses */
948 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
952 const struct afswtch *p;
953 p = af_getbyfamily(info.rti_info[RTAX_IFA]->sa_family);
954 if (p != NULL && p->af_status != NULL)
955 p->af_status(s, &info);
956 } else if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family)
957 afp->af_status(s, &info);
959 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
961 if (allfamilies || afp->af_af == AF_LINK) {
962 const struct afswtch *lafp;
965 * Hack; the link level address is received separately
966 * from the routing information so any address is not
967 * handled above. Cobble together an entry and invoke
968 * the status method specially.
970 lafp = af_getbyname("lladdr");
972 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
973 lafp->af_status(s, &info);
978 else if (afp->af_other_status != NULL)
979 afp->af_other_status(s);
981 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
982 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
983 printf("%s", ifs.ascii);
992 af_all_tunnel_status(s);
996 Perror(const char *cmd)
1001 errx(1, "%s: no such interface", cmd);
1005 errx(1, "%s: permission denied", cmd);
1014 * Print a value a la the %b format of the kernel's printf
1017 printb(const char *s, unsigned v, const char *bits)
1022 if (bits && *bits == 8)
1023 printf("%s=%o", s, v);
1025 printf("%s=%x", s, v);
1029 while ((i = *bits++) != '\0') {
1030 if (v & (1 << (i-1))) {
1034 for (; (c = *bits) > 32; bits++)
1037 for (; *bits > 32; bits++)
1045 ifmaybeload(const char *name)
1047 struct module_stat mstat;
1049 char ifkind[35], *dp;
1052 /* turn interface and unit into module name */
1053 strcpy(ifkind, "if_");
1054 for (cp = name, dp = ifkind + 3;
1055 (*cp != 0) && !isdigit(*cp); cp++, dp++)
1059 /* scan files in kernel */
1060 mstat.version = sizeof(struct module_stat);
1061 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1062 /* scan modules in file */
1063 for (modid = kldfirstmod(fileid); modid > 0;
1064 modid = modfnext(modid)) {
1065 if (modstat(modid, &mstat) < 0)
1067 /* strip bus name if present */
1068 if ((cp = strchr(mstat.name, '/')) != NULL) {
1073 /* already loaded? */
1074 if (strncmp(name, cp, strlen(cp)) == 0 ||
1075 strncmp(ifkind, cp, strlen(cp)) == 0)
1080 /* not present, we should try to load it */
1084 static struct cmd basic_cmds[] = {
1085 DEF_CMD("up", IFF_UP, setifflags),
1086 DEF_CMD("down", -IFF_UP, setifflags),
1087 DEF_CMD("arp", -IFF_NOARP, setifflags),
1088 DEF_CMD("-arp", IFF_NOARP, setifflags),
1089 DEF_CMD("debug", IFF_DEBUG, setifflags),
1090 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1091 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1092 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1093 DEF_CMD("add", IFF_UP, notealias),
1094 DEF_CMD("alias", IFF_UP, notealias),
1095 DEF_CMD("-alias", -IFF_UP, notealias),
1096 DEF_CMD("delete", -IFF_UP, notealias),
1097 DEF_CMD("remove", -IFF_UP, notealias),
1099 #define EN_SWABIPS 0x1000
1100 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1101 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1103 DEF_CMD_ARG("netmask", setifnetmask),
1104 DEF_CMD_ARG("metric", setifmetric),
1105 DEF_CMD_ARG("broadcast", setifbroadaddr),
1107 DEF_CMD_ARG("ipdst", setifipdst),
1109 DEF_CMD_ARG2("tunnel", settunnel),
1110 DEF_CMD("-tunnel", 0, deletetunnel),
1111 DEF_CMD("deletetunnel", 0, deletetunnel),
1112 DEF_CMD("link0", IFF_LINK0, setifflags),
1113 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1114 DEF_CMD("link1", IFF_LINK1, setifflags),
1115 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1116 DEF_CMD("link2", IFF_LINK2, setifflags),
1117 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1118 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1119 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1120 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1121 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1122 DEF_CMD("polling", IFF_NPOLLING, setifflags),
1123 DEF_CMD("-polling", -IFF_NPOLLING, setifflags),
1124 DEF_CMD("npolling", IFF_NPOLLING, setifflags),
1125 DEF_CMD("-npolling", -IFF_NPOLLING, setifflags),
1126 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1127 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1128 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1129 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1130 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1131 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1132 DEF_CMD("rss", IFCAP_RSS, setifcap),
1133 DEF_CMD("-rss", -IFCAP_RSS, setifcap),
1134 DEF_CMD("tso", IFCAP_TSO, setifcap),
1135 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1136 DEF_CMD("normal", -IFF_LINK0, setifflags),
1137 DEF_CMD("compress", IFF_LINK0, setifflags),
1138 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1139 DEF_CMD_ARG("mtu", setifmtu),
1140 DEF_CMD_ARG("name", setifname),
1141 DEF_CMD_ARG("pollcpu", setifpollcpu),
1142 DEF_CMD_ARG("tsolen", setiftsolen)
1145 static __constructor(101) void
1148 #define N(a) (sizeof(a) / sizeof(a[0]))
1151 for (i = 0; i < N(basic_cmds); i++)
1152 cmd_register(&basic_cmds[i]);