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 * Since "struct ifreq" is composed of various union members, callers
67 * should pay special attention to interpret the value.
68 * (.e.g. little/big endian difference in the structure.)
83 int printkeys = 0; /* Print keying material for interfaces. */
84 int printifname = 0; /* Print the name of the created interface. */
86 static int ifconfig(int argc, char *const *argv, int iscreate,
87 const struct afswtch *afp);
88 static void status(const struct afswtch *afp, int addrcount,
89 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
90 struct ifa_msghdr *ifam);
91 static void tunnel_status(int s);
92 static void usage(void) __dead2;
94 static struct afswtch *af_getbyname(const char *name);
95 static struct afswtch *af_getbyfamily(int af);
96 static void af_other_status(int);
97 static void printifnamemaybe(void);
99 static struct option *opts = NULL;
102 opt_register(struct option *p)
114 /* XXX not right but close enough for now */
116 for (p = opts; p != NULL; p = p->next) {
117 strlcat(options, p->opt_usage, sizeof(options));
118 strlcat(options, " ", sizeof(options));
122 "usage: ifconfig %s[-n] interface address_family [address [dest_address]]\n"
124 " ifconfig [-n] interface create\n"
125 " ifconfig [-n] interface destroy\n"
126 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
127 " ifconfig -l [-d] [-u] [address_family]\n"
128 " ifconfig %s[-d] [-m] [-u] [-v]\n",
129 options, options, options);
133 static void printifnamemaybe(void)
136 printf("%s\n", name);
140 main(int argc, char *argv[])
142 int c, all, namesonly, downonly, uponly;
143 int need_nl = 0, count = 0;
144 const struct afswtch *afp = NULL;
145 int addrcount, ifindex;
146 struct if_msghdr *ifm, *nextifm;
147 struct ifa_msghdr *ifam;
148 struct sockaddr_dl *sdl;
149 char *buf, *lim, *next;
157 all = downonly = uponly = namesonly = verbose = noload = 0;
160 * Ensure we print interface name when expected to,
161 * even if we terminate early due to error.
163 atexit(printifnamemaybe);
165 /* Parse leading line options */
166 strlcpy(options, "adklmnuv", sizeof(options));
167 for (p = opts; p != NULL; p = p->next)
168 strlcat(options, p->opt, sizeof(options));
169 while ((c = getopt(argc, argv, options)) != -1) {
171 case 'a': /* scan all interfaces */
174 case 'd': /* restrict scan to "down" interfaces */
180 case 'l': /* scan interface names only */
183 case 'm': /* show media choices in status */
186 case 'n': /* suppress module loading */
189 case 'u': /* restrict scan to "up" interfaces */
196 for (p = opts; p != NULL; p = p->next)
197 if (p->opt[0] == c) {
209 /* -l cannot be used with -a or -m */
210 if (namesonly && (all || supmedia))
214 if (uponly && downonly)
217 /* no arguments is equivalent to '-a' */
218 if (!namesonly && argc < 1)
221 /* -a and -l allow an address family arg to limit the output */
222 if (all || namesonly) {
229 afp = af_getbyname(*argv);
232 if (afp->af_name != NULL)
234 /* leave with afp non-zero */
237 /* not listing, need an argument */
244 /* check and maybe load support for this interface */
247 ifindex = if_nametoindex(ifname);
250 * NOTE: We must special-case the `create' command
251 * right here as we would otherwise fail when trying
252 * to find the interface.
254 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
255 strcmp(argv[0], "plumb") == 0)) {
256 iflen = strlcpy(name, ifname, sizeof(name));
257 if (iflen >= sizeof(name))
258 errx(1, "%s: cloning name too long",
260 ifconfig(argc, argv, 1, NULL);
263 errx(1, "interface %s does not exist", ifname);
267 /* Check for address family */
269 afp = af_getbyname(*argv);
278 mib[3] = 0; /* address family */
279 mib[4] = NET_RT_IFLIST;
280 mib[5] = ifindex; /* interface index */
282 /* if particular family specified, only ask about it */
286 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
287 errx(1, "iflist-sysctl-estimate");
288 if ((buf = malloc(needed)) == NULL)
290 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
291 if (errno == ENOMEM && count++ < 10) {
292 warnx("Routing table grew, retrying");
297 errx(1, "actual retrieval of interface table");
305 ifm = (struct if_msghdr *)next;
307 if (ifm->ifm_type == RTM_IFINFO) {
309 if (ifm->ifm_data.ifi_datalen == 0)
310 ifm->ifm_data.ifi_datalen = sizeof(struct if_data);
311 sdl = (struct sockaddr_dl *)((char *)ifm + sizeof(struct if_msghdr) -
312 sizeof(struct if_data) + ifm->ifm_data.ifi_datalen);
314 sdl = (struct sockaddr_dl *)(ifm + 1);
316 flags = ifm->ifm_flags;
318 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
319 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
321 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
322 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
327 next += ifm->ifm_msglen;
331 nextifm = (struct if_msghdr *)next;
333 if (nextifm->ifm_type != RTM_NEWADDR)
337 ifam = (struct ifa_msghdr *)nextifm;
340 next += nextifm->ifm_msglen;
343 if (sizeof(name) <= sdl->sdl_nlen)
344 name_len = sizeof(name) - 1;
346 name_len = sdl->sdl_nlen;
348 memcpy(name, sdl->sdl_data, name_len);
349 name[name_len] = '\0';
351 if (all || namesonly) {
353 if ((flags & IFF_UP) == 0)
354 continue; /* not up */
357 continue; /* not down */
359 if (afp == NULL || afp->af_af != AF_LINK ||
360 sdl->sdl_type == IFT_ETHER) {
371 ifconfig(argc, argv, 0, afp);
373 status(afp, addrcount, sdl, ifm, ifam);
377 if (namesonly && need_nl > 0)
383 static struct afswtch *afs = NULL;
386 af_register(struct afswtch *p)
392 static struct afswtch *
393 af_getbyname(const char *name)
397 for (afp = afs; afp != NULL; afp = afp->af_next)
398 if (strcmp(afp->af_name, name) == 0)
403 static struct afswtch *
404 af_getbyfamily(int af)
408 for (afp = afs; afp != NULL; afp = afp->af_next)
409 if (afp->af_af == af)
415 af_other_status(int s)
418 uint8_t afmask[howmany(AF_MAX, NBBY)];
420 memset(afmask, 0, sizeof(afmask));
421 for (afp = afs; afp != NULL; afp = afp->af_next) {
422 if (afp->af_other_status == NULL)
424 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
426 afp->af_other_status(s);
427 setbit(afmask, afp->af_af);
432 af_all_tunnel_status(int s)
435 uint8_t afmask[howmany(AF_MAX, NBBY)];
437 memset(afmask, 0, sizeof(afmask));
438 for (afp = afs; afp != NULL; afp = afp->af_next) {
439 if (afp->af_status_tunnel == NULL)
441 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
443 afp->af_status_tunnel(s);
444 setbit(afmask, afp->af_af);
448 static struct cmd *cmds = NULL;
451 cmd_register(struct cmd *p)
457 static const struct cmd *
458 cmd_lookup(const char *name, int iscreate)
462 for (p = cmds; p != NULL; p = p->c_next)
463 if (strcmp(name, p->c_name) == 0) {
476 callback_func *cb_func;
478 struct callback *cb_next;
480 static struct callback *callbacks = NULL;
483 callback_register(callback_func *func, void *arg)
487 cb = malloc(sizeof(struct callback));
489 errx(1, "unable to allocate memory for callback");
492 cb->cb_next = callbacks;
496 /* specially-handled commands */
497 static void setifaddr(const char *, int, int, const struct afswtch *);
498 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
500 static void setifdstaddr(const char *, int, int, const struct afswtch *);
501 static const struct cmd setifdstaddr_cmd =
502 DEF_CMD("ifdstaddr", 0, setifdstaddr);
505 ifconfig(int argc, char *const *argv, int iscreate,
506 const struct afswtch *uafp)
508 const struct afswtch *afp, *nafp;
512 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
513 afp = uafp != NULL ? uafp : af_getbyname("inet");
515 ifr.ifr_addr.sa_family =
516 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
517 AF_LOCAL : afp->af_af;
519 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
520 (uafp != NULL || errno != EAFNOSUPPORT ||
521 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
522 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
527 p = cmd_lookup(*argv, iscreate);
529 if (iscreate && p == NULL) {
531 * Push the clone create callback so the new
532 * device is created and can be used for any
533 * remaining arguments.
537 errx(1, "internal error, no callback");
538 callbacks = cb->cb_next;
539 cb->cb_func(s, cb->cb_arg);
543 * After cloning, make sure we have an up-to-date name
546 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
549 * Handle any address family spec that
550 * immediately follows and potentially
551 * recreate the socket.
553 nafp = af_getbyname(*argv);
563 * Look for a normal parameter.
569 * Not a recognized command, choose between setting
570 * the interface address and the dst address.
572 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
574 if (p->c_u.c_func || p->c_u.c_func2) {
575 if (p->c_parameter == NEXTARG) {
577 errx(1, "'%s' requires argument",
579 p->c_u.c_func(argv[1], 0, s, afp);
581 } else if (p->c_parameter == OPTARG) {
582 p->c_u.c_func(argv[1], 0, s, afp);
585 } else if (p->c_parameter == NEXTARG2) {
587 errx(1, "'%s' requires 2 arguments",
589 p->c_u.c_func2(argv[1], argv[2], s, afp);
590 argc -= 2, argv += 2;
592 p->c_u.c_func(*argv, p->c_parameter, s, afp);
598 * Do any post argument processing required by the address family.
600 if (afp->af_postproc != NULL)
601 afp->af_postproc(s, afp);
603 * Do deferred callbacks registered while processing
604 * command-line arguments.
606 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
607 cb->cb_func(s, cb->cb_arg);
609 * Do deferred operations.
612 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
613 warnx("interface %s cannot change %s addresses!",
620 strlcpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
621 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
623 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
624 /* means no previous address for interface */
626 Perror("ioctl (SIOCDIFADDR)");
630 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
631 warnx("interface %s cannot change %s addresses!",
636 if (newaddr && (setaddr || setmask)) {
637 strlcpy(afp->af_addreq, name, sizeof ifr.ifr_name);
638 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
639 Perror("ioctl (SIOCAIFADDR)");
648 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
650 if (afp->af_getaddr == NULL)
653 * Delay the ioctl to set the interface addr until flags are all set.
654 * The address interpretation may depend on the flags,
655 * and the flags may change when the address is set.
658 if (doalias == 0 && afp->af_af != AF_LINK)
660 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
664 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
666 struct addrinfo *srcres, *dstres;
669 if (afp->af_settunnel == NULL) {
670 warn("address family %s does not support tunnel setup",
675 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
676 errx(1, "error in parsing address string: %s",
677 gai_strerror(ecode));
679 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
680 errx(1, "error in parsing address string: %s",
681 gai_strerror(ecode));
683 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
685 "source and destination address families do not match");
687 afp->af_settunnel(s, srcres, dstres);
689 freeaddrinfo(srcres);
690 freeaddrinfo(dstres);
695 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
698 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
699 err(1, "SIOCDIFPHYADDR");
703 setifnetmask(const char *addr, int dummy __unused, int s,
704 const struct afswtch *afp)
706 if (afp->af_getaddr != NULL) {
708 afp->af_getaddr(addr, MASK);
713 setifbroadaddr(const char *addr, int dummy __unused, int s,
714 const struct afswtch *afp)
716 if (afp->af_getaddr != NULL)
717 afp->af_getaddr(addr, DSTADDR);
721 notealias(const char *addr, int param, int s, const struct afswtch *afp)
723 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
724 if (setaddr && doalias == 0 && param < 0)
725 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
726 bcopy((caddr_t)rqtosa(af_addreq),
727 (caddr_t)rqtosa(af_ridreq),
728 rqtosa(af_addreq)->sa_len);
740 setifdstaddr(const char *addr, int param __unused, int s,
741 const struct afswtch *afp)
743 if (afp->af_getaddr != NULL)
744 afp->af_getaddr(addr, DSTADDR);
748 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
749 * of the ifreq structure, which may confuse other parts of ifconfig.
750 * Make a private copy so we can avoid that.
753 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
757 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
759 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
760 Perror("ioctl (SIOCGIFFLAGS)");
762 strlcpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
763 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
770 my_ifr.ifr_flags = flags & 0xffff;
771 my_ifr.ifr_flagshigh = flags >> 16;
772 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
777 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
780 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
781 Perror("ioctl (SIOCGIFCAP)");
783 flags = ifr.ifr_curcap;
789 ifr.ifr_reqcap = flags;
790 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
795 setifmetric(const char *val, int dummy __unused, int s,
796 const struct afswtch *afp)
798 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
799 ifr.ifr_metric = atoi(val);
800 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
801 err(1, "ioctl SIOCSIFMETRIC (set metric)");
805 setifmtu(const char *val, int dummy __unused, int s,
806 const struct afswtch *afp)
808 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
809 ifr.ifr_mtu = atoi(val);
810 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
811 err(1, "ioctl SIOCSIFMTU (set mtu)");
815 setiftsolen(const char *val, int dummy __unused, int s,
816 const struct afswtch *afp)
818 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
819 ifr.ifr_tsolen = atoi(val);
820 if (ioctl(s, SIOCSIFTSOLEN, (caddr_t)&ifr) < 0)
821 err(1, "ioctl SIOCSIFTSOLEN (set tsolen)");
825 setifname(const char *val, int dummy __unused, int s,
826 const struct afswtch *afp)
830 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
832 newname = strdup(val);
834 err(1, "no memory to set ifname");
835 ifr.ifr_data = newname;
836 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
838 err(1, "ioctl SIOCSIFNAME (set name)");
841 strlcpy(name, newname, sizeof(name));
846 setifpollcpu(const char *val, int dummy __unused, int s,
847 const struct afswtch *afp)
849 warnx("pollcpu is deprecated, use polling or npolling instead");
850 setifflags("npolling", IFF_NPOLLING, s, afp);
854 * Expand the compacted form of addresses as returned via the
855 * configuration read via sysctl().
858 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
863 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
864 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
865 if ((rtinfo->rti_addrs & (1 << i)) == 0)
867 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
873 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
874 "\10NOARP\11PROMISC\12ALLMULTI\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
875 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25NPOLLING\26IDIRECT"
878 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7RSS" \
879 "\10VLAN_HWCSUM\11TSO"
882 * Print the status of the interface. If an address family was
883 * specified, show only it; otherwise, show them all.
886 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
887 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
889 struct rt_addrinfo info;
895 ifr.ifr_addr.sa_family = AF_LOCAL;
898 ifr.ifr_addr.sa_family =
899 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
901 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
903 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
905 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
907 printf("%s: ", name);
908 printb("flags", flags, IFFBITS);
909 if (ifm->ifm_data.ifi_metric)
910 printf(" metric %ld", ifm->ifm_data.ifi_metric);
911 if (ifm->ifm_data.ifi_mtu)
912 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
915 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
916 if (ifr.ifr_curcap != 0) {
917 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
920 if (supmedia && ifr.ifr_reqcap != 0) {
921 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
923 if (ifr.ifr_reqcap & IFCAP_TSO) {
924 if (ioctl(s, SIOCGIFTSOLEN,
925 (caddr_t)&ifr) == 0) {
926 printf("\ttsolen %d", ifr.ifr_tsolen);
935 while (addrcount > 0) {
936 info.rti_addrs = ifam->ifam_addrs;
937 /* Expand the compacted addresses */
938 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
942 const struct afswtch *p;
943 p = af_getbyfamily(info.rti_info[RTAX_IFA]->sa_family);
944 if (p != NULL && p->af_status != NULL)
945 p->af_status(s, &info);
946 } else if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family)
947 afp->af_status(s, &info);
949 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
951 if (allfamilies || afp->af_af == AF_LINK) {
952 const struct afswtch *lafp;
955 * Hack; the link level address is received separately
956 * from the routing information so any address is not
957 * handled above. Cobble together an entry and invoke
958 * the status method specially.
960 lafp = af_getbyname("lladdr");
962 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
963 lafp->af_status(s, &info);
968 else if (afp->af_other_status != NULL)
969 afp->af_other_status(s);
971 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
972 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
973 printf("%s", ifs.ascii);
982 af_all_tunnel_status(s);
986 Perror(const char *cmd)
991 errx(1, "%s: no such interface", cmd);
995 errx(1, "%s: permission denied", cmd);
1004 * Print a value a la the %pb%i format of the kernel's kprintf()
1007 printb(const char *s, unsigned v, const char *bits)
1012 if (bits && *bits == 8)
1013 printf("%s=%o", s, v);
1015 printf("%s=%x", s, v);
1019 while ((i = *bits++) != '\0') {
1020 if (v & (1 << (i-1))) {
1024 for (; (c = *bits) > 32; bits++)
1027 for (; *bits > 32; bits++)
1035 ifmaybeload(const char *name)
1037 #define MOD_PREFIX_LEN 3 /* "if_" */
1038 struct module_stat mstat;
1040 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1043 /* loading suppressed by the user */
1047 /* trim the interface number off the end */
1048 strlcpy(ifname, name, sizeof(ifname));
1049 for (dp = ifname; *dp != 0; dp++)
1055 /* turn interface and unit into module name */
1056 strlcpy(ifkind, "if_", sizeof(ifkind));
1057 strlcat(ifkind, ifname, sizeof(ifkind));
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 (strcmp(ifname, cp) == 0 ||
1075 strcmp(ifkind, 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),
1106 DEF_CMD_ARG2("tunnel", settunnel),
1107 DEF_CMD("-tunnel", 0, deletetunnel),
1108 DEF_CMD("deletetunnel", 0, deletetunnel),
1109 DEF_CMD("link0", IFF_LINK0, setifflags),
1110 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1111 DEF_CMD("link1", IFF_LINK1, setifflags),
1112 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1113 DEF_CMD("link2", IFF_LINK2, setifflags),
1114 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1115 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1116 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1117 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1118 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1119 DEF_CMD("polling", IFF_NPOLLING, setifflags),
1120 DEF_CMD("-polling", -IFF_NPOLLING, setifflags),
1121 DEF_CMD("npolling", IFF_NPOLLING, setifflags),
1122 DEF_CMD("-npolling", -IFF_NPOLLING, setifflags),
1123 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1124 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1125 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1126 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1127 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1128 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1129 DEF_CMD("rss", IFCAP_RSS, setifcap),
1130 DEF_CMD("-rss", -IFCAP_RSS, setifcap),
1131 DEF_CMD("tso", IFCAP_TSO, setifcap),
1132 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1133 DEF_CMD("normal", -IFF_LINK0, setifflags),
1134 DEF_CMD("compress", IFF_LINK0, setifflags),
1135 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1136 DEF_CMD_ARG("mtu", setifmtu),
1137 DEF_CMD_ARG("name", setifname),
1138 DEF_CMD_ARG("pollcpu", setifpollcpu),
1139 DEF_CMD_ARG("tsolen", setiftsolen)
1142 static __constructor(101) void
1147 for (i = 0; i < nitems(basic_cmds); i++)
1148 cmd_register(&basic_cmds[i]);