2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
40 static const char rcsid[] =
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
48 #include <sys/module.h>
49 #include <sys/linker.h>
51 #include <net/ethernet.h>
53 #include <net/if_var.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <arpa/inet.h>
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.)
96 int printkeys = 0; /* Print keying material for interfaces. */
98 static int ifconfig(int argc, char *const *argv, int iscreate,
99 const struct afswtch *afp);
100 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
101 struct ifaddrs *ifa);
102 static void tunnel_status(int s);
103 static void usage(void);
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 const struct afswtch *afp = NULL;
148 struct ifaddrs *ifap, *ifa;
150 const struct sockaddr_dl *sdl;
151 char options[1024], *cp, *namecp = NULL;
156 all = downonly = uponly = namesonly = noload = verbose = 0;
158 /* Parse leading line options */
159 strlcpy(options, "adklmnuv", sizeof(options));
160 for (p = opts; p != NULL; p = p->next)
161 strlcat(options, p->opt, sizeof(options));
162 while ((c = getopt(argc, argv, options)) != -1) {
164 case 'a': /* scan all interfaces */
167 case 'd': /* restrict scan to "down" interfaces */
173 case 'l': /* scan interface names only */
176 case 'm': /* show media choices in status */
179 case 'n': /* suppress module loading */
182 case 'u': /* restrict scan to "up" interfaces */
189 for (p = opts; p != NULL; p = p->next)
190 if (p->opt[0] == c) {
202 /* -l cannot be used with -a or -m */
203 if (namesonly && (all || supmedia))
207 if (uponly && downonly)
210 /* no arguments is equivalent to '-a' */
211 if (!namesonly && argc < 1)
214 /* -a and -l allow an address family arg to limit the output */
215 if (all || namesonly) {
222 afp = af_getbyname(*argv);
225 if (afp->af_name != NULL)
227 /* leave with afp non-zero */
230 /* not listing, need an argument */
237 /* 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);
257 * NOTE: We have to special-case the `-vnet' command
258 * right here as we would otherwise fail when trying
259 * to find the interface as it lives in another vnet.
261 if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
262 iflen = strlcpy(name, ifname, sizeof(name));
263 if (iflen >= sizeof(name))
264 errx(1, "%s: interface name too long",
266 ifconfig(argc, argv, 0, NULL);
269 errx(1, "interface %s does not exist", ifname);
273 /* Check for address family */
275 afp = af_getbyname(*argv);
280 if (getifaddrs(&ifap) != 0)
281 err(EXIT_FAILURE, "getifaddrs");
284 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
285 memset(&paifr, 0, sizeof(paifr));
286 strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
287 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
288 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
289 ifa->ifa_addr->sa_len);
292 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
294 if (ifa->ifa_addr->sa_family == AF_LINK)
295 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
298 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0 && !namesonly)
300 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
301 if (iflen >= sizeof(name)) {
302 warnx("%s: interface name too long, skipping",
308 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
310 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
313 * Are we just listing the interfaces?
319 /* special case for "ether" address family */
320 if (!strcmp(afp->af_name, "ether")) {
322 sdl->sdl_type != IFT_ETHER ||
323 sdl->sdl_alen != ETHER_ADDR_LEN)
326 if (ifa->ifa_addr->sa_family != afp->af_af)
340 ifconfig(argc, argv, 0, afp);
342 status(afp, sdl, ifa);
351 static struct afswtch *afs = NULL;
354 af_register(struct afswtch *p)
360 static struct afswtch *
361 af_getbyname(const char *name)
365 for (afp = afs; afp != NULL; afp = afp->af_next)
366 if (strcmp(afp->af_name, name) == 0)
371 static struct afswtch *
372 af_getbyfamily(int af)
376 for (afp = afs; afp != NULL; afp = afp->af_next)
377 if (afp->af_af == af)
383 af_other_status(int s)
386 uint8_t afmask[howmany(AF_MAX, NBBY)];
388 memset(afmask, 0, sizeof(afmask));
389 for (afp = afs; afp != NULL; afp = afp->af_next) {
390 if (afp->af_other_status == NULL)
392 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
394 afp->af_other_status(s);
395 setbit(afmask, afp->af_af);
400 af_all_tunnel_status(int s)
403 uint8_t afmask[howmany(AF_MAX, NBBY)];
405 memset(afmask, 0, sizeof(afmask));
406 for (afp = afs; afp != NULL; afp = afp->af_next) {
407 if (afp->af_status_tunnel == NULL)
409 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
411 afp->af_status_tunnel(s);
412 setbit(afmask, afp->af_af);
416 static struct cmd *cmds = NULL;
419 cmd_register(struct cmd *p)
425 static const struct cmd *
426 cmd_lookup(const char *name, int iscreate)
428 #define N(a) (sizeof(a)/sizeof(a[0]))
431 for (p = cmds; p != NULL; p = p->c_next)
432 if (strcmp(name, p->c_name) == 0) {
446 callback_func *cb_func;
448 struct callback *cb_next;
450 static struct callback *callbacks = NULL;
453 callback_register(callback_func *func, void *arg)
457 cb = malloc(sizeof(struct callback));
459 errx(1, "unable to allocate memory for callback");
462 cb->cb_next = callbacks;
466 /* specially-handled commands */
467 static void setifaddr(const char *, int, int, const struct afswtch *);
468 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
470 static void setifdstaddr(const char *, int, int, const struct afswtch *);
471 static const struct cmd setifdstaddr_cmd =
472 DEF_CMD("ifdstaddr", 0, setifdstaddr);
475 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
477 const struct afswtch *afp, *nafp;
482 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
483 afp = uafp != NULL ? uafp : af_getbyname("inet");
485 ifr.ifr_addr.sa_family =
486 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
487 AF_LOCAL : afp->af_af;
489 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
490 (uafp != NULL || errno != EPROTONOSUPPORT ||
491 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
492 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
495 p = cmd_lookup(*argv, iscreate);
496 if (iscreate && p == NULL) {
498 * Push the clone create callback so the new
499 * device is created and can be used for any
500 * remaining arguments.
504 errx(1, "internal error, no callback");
505 callbacks = cb->cb_next;
506 cb->cb_func(s, cb->cb_arg);
509 * Handle any address family spec that
510 * immediately follows and potentially
511 * recreate the socket.
513 nafp = af_getbyname(*argv);
523 * Look for a normal parameter.
529 * Not a recognized command, choose between setting
530 * the interface address and the dst address.
532 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
534 if (p->c_u.c_func || p->c_u.c_func2) {
535 if (p->c_parameter == NEXTARG) {
537 errx(1, "'%s' requires argument",
539 p->c_u.c_func(argv[1], 0, s, afp);
541 } else if (p->c_parameter == OPTARG) {
542 p->c_u.c_func(argv[1], 0, s, afp);
545 } else if (p->c_parameter == NEXTARG2) {
547 errx(1, "'%s' requires 2 arguments",
549 p->c_u.c_func2(argv[1], argv[2], s, afp);
550 argc -= 2, argv += 2;
552 p->c_u.c_func(*argv, p->c_parameter, s, afp);
558 * Do any post argument processing required by the address family.
560 if (afp->af_postproc != NULL)
561 afp->af_postproc(s, afp);
563 * Do deferred callbacks registered while processing
564 * command-line arguments.
566 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
567 cb->cb_func(s, cb->cb_arg);
569 * Do deferred operations.
572 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
573 warnx("interface %s cannot change %s addresses!",
580 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
581 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
583 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
584 /* means no previous address for interface */
586 Perror("ioctl (SIOCDIFADDR)");
590 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
591 warnx("interface %s cannot change %s addresses!",
596 if (newaddr && (setaddr || setmask)) {
597 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
598 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
599 Perror("ioctl (SIOCAIFADDR)");
608 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
610 if (afp->af_getaddr == NULL)
613 * Delay the ioctl to set the interface addr until flags are all set.
614 * The address interpretation may depend on the flags,
615 * and the flags may change when the address is set.
618 if (doalias == 0 && afp->af_af != AF_LINK)
620 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
624 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
626 struct addrinfo *srcres, *dstres;
629 if (afp->af_settunnel == NULL) {
630 warn("address family %s does not support tunnel setup",
635 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
636 errx(1, "error in parsing address string: %s",
637 gai_strerror(ecode));
639 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
640 errx(1, "error in parsing address string: %s",
641 gai_strerror(ecode));
643 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
645 "source and destination address families do not match");
647 afp->af_settunnel(s, srcres, dstres);
649 freeaddrinfo(srcres);
650 freeaddrinfo(dstres);
655 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
658 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
659 err(1, "SIOCDIFPHYADDR");
663 setifvnet(const char *jname, int dummy __unused, int s,
664 const struct afswtch *afp)
668 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
669 my_ifr.ifr_jid = jail_getid(jname);
670 if (my_ifr.ifr_jid < 0)
671 errx(1, "%s", jail_errmsg);
672 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
673 err(1, "SIOCSIFVNET");
677 setifrvnet(const char *jname, int dummy __unused, int s,
678 const struct afswtch *afp)
682 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
683 my_ifr.ifr_jid = jail_getid(jname);
684 if (my_ifr.ifr_jid < 0)
685 errx(1, "%s", jail_errmsg);
686 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
687 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
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);
723 notealias(const char *addr, int param, int s, const struct afswtch *afp)
725 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
726 if (setaddr && doalias == 0 && param < 0)
727 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
728 bcopy((caddr_t)rqtosa(af_addreq),
729 (caddr_t)rqtosa(af_ridreq),
730 rqtosa(af_addreq)->sa_len);
742 setifdstaddr(const char *addr, int param __unused, int s,
743 const struct afswtch *afp)
745 if (afp->af_getaddr != NULL)
746 afp->af_getaddr(addr, DSTADDR);
750 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
751 * of the ifreq structure, which may confuse other parts of ifconfig.
752 * Make a private copy so we can avoid that.
755 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
760 memset(&my_ifr, 0, sizeof(my_ifr));
761 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
763 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
764 Perror("ioctl (SIOCGIFFLAGS)");
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)
785 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
786 Perror("ioctl (SIOCGIFCAP)");
789 flags = ifr.ifr_curcap;
795 flags &= ifr.ifr_reqcap;
796 ifr.ifr_reqcap = flags;
797 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
802 setifmetric(const char *val, int dummy __unused, int s,
803 const struct afswtch *afp)
805 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
806 ifr.ifr_metric = atoi(val);
807 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
808 warn("ioctl (set metric)");
812 setifmtu(const char *val, int dummy __unused, int s,
813 const struct afswtch *afp)
815 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
816 ifr.ifr_mtu = atoi(val);
817 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
818 warn("ioctl (set mtu)");
822 setifname(const char *val, int dummy __unused, int s,
823 const struct afswtch *afp)
827 newname = strdup(val);
828 if (newname == NULL) {
829 warn("no memory to set ifname");
832 ifr.ifr_data = newname;
833 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
834 warn("ioctl (set name)");
838 strlcpy(name, newname, sizeof(name));
844 setifdescr(const char *val, int dummy __unused, int s,
845 const struct afswtch *afp)
849 ifr.ifr_buffer.length = strlen(val) + 1;
850 if (ifr.ifr_buffer.length == 1) {
851 ifr.ifr_buffer.buffer = newdescr = NULL;
852 ifr.ifr_buffer.length = 0;
854 newdescr = strdup(val);
855 ifr.ifr_buffer.buffer = newdescr;
856 if (newdescr == NULL) {
857 warn("no memory to set ifdescr");
862 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0)
863 warn("ioctl (set descr)");
870 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
873 setifdescr("", 0, s, 0);
877 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
878 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
879 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
882 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
883 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
884 "\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE"
887 * Print the status of the interface. If an address family was
888 * specified, show only it; otherwise, show them all.
891 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
900 ifr.ifr_addr.sa_family = AF_LOCAL;
903 ifr.ifr_addr.sa_family =
904 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
906 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
908 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
910 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
912 printf("%s: ", name);
913 printb("flags", ifa->ifa_flags, IFFBITS);
914 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
915 printf(" metric %d", ifr.ifr_metric);
916 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
917 printf(" mtu %d", ifr.ifr_mtu);
921 if ((descr = reallocf(descr, descrlen)) != NULL) {
922 ifr.ifr_buffer.buffer = descr;
923 ifr.ifr_buffer.length = descrlen;
924 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
925 if (ifr.ifr_buffer.buffer == descr) {
926 if (strlen(descr) > 0)
927 printf("\tdescription: %s\n",
929 } else if (ifr.ifr_buffer.length > descrlen) {
930 descrlen = ifr.ifr_buffer.length;
935 warn("unable to allocate memory for interface"
940 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
941 if (ifr.ifr_curcap != 0) {
942 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
945 if (supmedia && ifr.ifr_reqcap != 0) {
946 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
953 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
954 if (ift->ifa_addr == NULL)
956 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
959 const struct afswtch *p;
960 p = af_getbyfamily(ift->ifa_addr->sa_family);
961 if (p != NULL && p->af_status != NULL)
962 p->af_status(s, ift);
963 } else if (afp->af_af == ift->ifa_addr->sa_family)
964 afp->af_status(s, ift);
967 if (allfamilies || afp->af_af == AF_LINK) {
968 const struct afswtch *lafp;
971 * Hack; the link level address is received separately
972 * from the routing information so any address is not
973 * handled above. Cobble together an entry and invoke
974 * the status method specially.
976 lafp = af_getbyname("lladdr");
978 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
979 lafp->af_status(s, &info);
985 else if (afp->af_other_status != NULL)
986 afp->af_other_status(s);
988 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
989 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
990 printf("%s", ifs.ascii);
999 af_all_tunnel_status(s);
1003 Perror(const char *cmd)
1008 errx(1, "%s: no such interface", cmd);
1012 errx(1, "%s: permission denied", cmd);
1021 * Print a value a la the %b format of the kernel's printf
1024 printb(const char *s, unsigned v, const char *bits)
1029 if (bits && *bits == 8)
1030 printf("%s=%o", s, v);
1032 printf("%s=%x", s, v);
1036 while ((i = *bits++) != '\0') {
1037 if (v & (1 << (i-1))) {
1041 for (; (c = *bits) > 32; bits++)
1044 for (; *bits > 32; bits++)
1052 ifmaybeload(const char *name)
1054 #define MOD_PREFIX_LEN 3 /* "if_" */
1055 struct module_stat mstat;
1057 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1060 /* loading suppressed by the user */
1064 /* trim the interface number off the end */
1065 strlcpy(ifname, name, sizeof(ifname));
1066 for (dp = ifname; *dp != 0; dp++)
1072 /* turn interface and unit into module name */
1073 strcpy(ifkind, "if_");
1074 strlcpy(ifkind + MOD_PREFIX_LEN, ifname,
1075 sizeof(ifkind) - MOD_PREFIX_LEN);
1077 /* scan files in kernel */
1078 mstat.version = sizeof(struct module_stat);
1079 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1080 /* scan modules in file */
1081 for (modid = kldfirstmod(fileid); modid > 0;
1082 modid = modfnext(modid)) {
1083 if (modstat(modid, &mstat) < 0)
1085 /* strip bus name if present */
1086 if ((cp = strchr(mstat.name, '/')) != NULL) {
1091 /* already loaded? */
1092 if (strncmp(ifname, cp, strlen(ifname) + 1) == 0 ||
1093 strncmp(ifkind, cp, strlen(ifkind) + 1) == 0)
1098 /* not present, we should try to load it */
1102 static struct cmd basic_cmds[] = {
1103 DEF_CMD("up", IFF_UP, setifflags),
1104 DEF_CMD("down", -IFF_UP, setifflags),
1105 DEF_CMD("arp", -IFF_NOARP, setifflags),
1106 DEF_CMD("-arp", IFF_NOARP, setifflags),
1107 DEF_CMD("debug", IFF_DEBUG, setifflags),
1108 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1109 DEF_CMD_ARG("description", setifdescr),
1110 DEF_CMD_ARG("descr", setifdescr),
1111 DEF_CMD("-description", 0, unsetifdescr),
1112 DEF_CMD("-descr", 0, unsetifdescr),
1113 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1114 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1115 DEF_CMD("add", IFF_UP, notealias),
1116 DEF_CMD("alias", IFF_UP, notealias),
1117 DEF_CMD("-alias", -IFF_UP, notealias),
1118 DEF_CMD("delete", -IFF_UP, notealias),
1119 DEF_CMD("remove", -IFF_UP, notealias),
1121 #define EN_SWABIPS 0x1000
1122 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1123 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1125 DEF_CMD_ARG("netmask", setifnetmask),
1126 DEF_CMD_ARG("metric", setifmetric),
1127 DEF_CMD_ARG("broadcast", setifbroadaddr),
1128 DEF_CMD_ARG("ipdst", setifipdst),
1129 DEF_CMD_ARG2("tunnel", settunnel),
1130 DEF_CMD("-tunnel", 0, deletetunnel),
1131 DEF_CMD("deletetunnel", 0, deletetunnel),
1132 DEF_CMD_ARG("vnet", setifvnet),
1133 DEF_CMD_ARG("-vnet", setifrvnet),
1134 DEF_CMD("link0", IFF_LINK0, setifflags),
1135 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1136 DEF_CMD("link1", IFF_LINK1, setifflags),
1137 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1138 DEF_CMD("link2", IFF_LINK2, setifflags),
1139 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1140 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1141 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1142 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1143 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1144 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1145 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1146 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1147 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1148 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1149 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1150 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1151 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1152 DEF_CMD("tso", IFCAP_TSO, setifcap),
1153 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1154 DEF_CMD("lro", IFCAP_LRO, setifcap),
1155 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1156 DEF_CMD("wol", IFCAP_WOL, setifcap),
1157 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1158 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1159 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1160 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1161 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1162 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1163 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1164 DEF_CMD("normal", -IFF_LINK0, setifflags),
1165 DEF_CMD("compress", IFF_LINK0, setifflags),
1166 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1167 DEF_CMD_ARG("mtu", setifmtu),
1168 DEF_CMD_ARG("name", setifname),
1171 static __constructor void
1174 #define N(a) (sizeof(a) / sizeof(a[0]))
1177 for (i = 0; i < N(basic_cmds); i++)
1178 cmd_register(&basic_cmds[i]);