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
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>
40 #include <net/ethernet.h>
42 #include <net/if_var.h>
43 #include <net/if_dl.h>
44 #include <net/if_types.h>
45 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <arpa/inet.h>
65 * This macro returns the size of a struct sockaddr when passed
66 * through a routing socket. Basically we round up sa_len to
67 * a multiple of sizeof(long), with a minimum of sizeof(long).
68 * The check for a NULL pointer is just a convenience, probably never used.
69 * The case sa_len == 0 should only apply to empty structures.
72 ( (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ? \
74 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) )
77 * Since "struct ifreq" is composed of various union members, callers
78 * should pay special attention to interprete the value.
79 * (.e.g. little/big endian difference in the structure.)
94 int printkeys = 0; /* Print keying material for interfaces. */
95 int printname = 0; /* Print the name of the created interface. */
97 static int ifconfig(int argc, char *const *argv, int, const struct afswtch *afp);
98 static void status(const struct afswtch *afp, int addrcount,
99 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
100 struct ifa_msghdr *ifam);
101 static void tunnel_status(int s);
102 static void usage(void) __dead2;
104 static struct afswtch *af_getbyname(const char *name);
105 static struct afswtch *af_getbyfamily(int af);
106 static void af_other_status(int);
108 static struct option *opts = NULL;
111 opt_register(struct option *p)
123 /* XXX not right but close enough for now */
125 for (p = opts; p != NULL; p = p->next) {
126 strlcat(options, p->opt_usage, sizeof(options));
127 strlcat(options, " ", sizeof(options));
131 "usage: ifconfig %sinterface address_family [address [dest_address]]\n"
133 " ifconfig interface create\n"
134 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
135 " ifconfig -l [-d] [-u] [address_family]\n"
136 " ifconfig %s[-d] [-m] [-u] [-v]\n",
137 options, options, options);
142 main(int argc, char *argv[])
144 int c, all, namesonly, downonly, uponly;
145 int need_nl = 0, count = 0;
146 const struct afswtch *afp = NULL;
147 int addrcount, ifindex;
148 struct if_msghdr *ifm, *nextifm;
149 struct ifa_msghdr *ifam;
150 struct sockaddr_dl *sdl;
151 char *buf, *lim, *next;
159 all = downonly = uponly = namesonly = verbose = 0;
161 /* Parse leading line options */
162 strlcpy(options, "adklmuv", sizeof(options));
163 for (p = opts; p != NULL; p = p->next)
164 strlcat(options, p->opt, sizeof(options));
165 while ((c = getopt(argc, argv, options)) != -1) {
167 case 'a': /* scan all interfaces */
170 case 'd': /* restrict scan to "down" interfaces */
176 case 'l': /* scan interface names only */
179 case 'm': /* show media choices in status */
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 */
239 ifindex = if_nametoindex(ifname);
242 * NOTE: We must special-case the `create' command
243 * right here as we would otherwise fail when trying
244 * to find the interface.
246 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
247 strcmp(argv[0], "plumb") == 0)) {
248 iflen = strlcpy(name, ifname, sizeof(name));
249 if (iflen >= sizeof(name))
250 errx(1, "%s: cloning name too long",
252 ifconfig(argc, argv, 1, NULL);
255 errx(1, "interface %s does not exist", ifname);
259 /* Check for address family */
261 afp = af_getbyname(*argv);
270 mib[3] = 0; /* address family */
271 mib[4] = NET_RT_IFLIST;
272 mib[5] = ifindex; /* interface index */
274 /* if particular family specified, only ask about it */
278 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
279 errx(1, "iflist-sysctl-estimate");
280 if ((buf = malloc(needed)) == NULL)
282 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
283 if (errno == ENOMEM && count++ < 10) {
284 warnx("Routing table grew, retrying");
289 errx(1, "actual retrieval of interface table");
297 ifm = (struct if_msghdr *)next;
299 if (ifm->ifm_type == RTM_IFINFO) {
301 if (ifm->ifm_data.ifi_datalen == 0)
302 ifm->ifm_data.ifi_datalen = sizeof(struct if_data);
303 sdl = (struct sockaddr_dl *)((char *)ifm + sizeof(struct if_msghdr) -
304 sizeof(struct if_data) + ifm->ifm_data.ifi_datalen);
306 sdl = (struct sockaddr_dl *)(ifm + 1);
308 flags = ifm->ifm_flags;
310 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
311 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
313 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
314 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
319 next += ifm->ifm_msglen;
324 nextifm = (struct if_msghdr *)next;
326 if (nextifm->ifm_type != RTM_NEWADDR)
330 ifam = (struct ifa_msghdr *)nextifm;
333 next += nextifm->ifm_msglen;
336 if (sizeof(name) <= sdl->sdl_nlen)
337 name_len = sizeof(name) - 1;
339 name_len = sdl->sdl_nlen;
341 memcpy(name, sdl->sdl_data, name_len);
342 name[name_len] = '\0';
344 if (all || namesonly) {
346 if ((flags & IFF_UP) == 0)
347 continue; /* not up */
350 continue; /* not down */
352 if (afp == NULL || afp->af_af != AF_LINK ||
353 sdl->sdl_type == IFT_ETHER) {
364 ifconfig(argc, argv, 0, afp);
366 status(afp, addrcount, sdl, ifm, ifam);
370 if (namesonly && need_nl > 0)
373 printf("%s\n", name);
378 static struct afswtch *afs = NULL;
381 af_register(struct afswtch *p)
387 static struct afswtch *
388 af_getbyname(const char *name)
392 for (afp = afs; afp != NULL; afp = afp->af_next)
393 if (strcmp(afp->af_name, name) == 0)
398 static struct afswtch *
399 af_getbyfamily(int af)
403 for (afp = afs; afp != NULL; afp = afp->af_next)
404 if (afp->af_af == af)
410 af_other_status(int s)
413 uint8_t afmask[howmany(AF_MAX, NBBY)];
415 memset(afmask, 0, sizeof(afmask));
416 for (afp = afs; afp != NULL; afp = afp->af_next) {
417 if (afp->af_other_status == NULL)
419 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
421 afp->af_other_status(s);
422 setbit(afmask, afp->af_af);
427 af_all_tunnel_status(int s)
430 uint8_t afmask[howmany(AF_MAX, NBBY)];
432 memset(afmask, 0, sizeof(afmask));
433 for (afp = afs; afp != NULL; afp = afp->af_next) {
434 if (afp->af_status_tunnel == NULL)
436 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
438 afp->af_status_tunnel(s);
439 setbit(afmask, afp->af_af);
443 static struct cmd *cmds = NULL;
446 cmd_register(struct cmd *p)
452 static const struct cmd *
453 cmd_lookup(const char *name, int iscreate)
455 #define N(a) (sizeof(a)/sizeof(a[0]))
458 for (p = cmds; p != NULL; p = p->c_next)
459 if (strcmp(name, p->c_name) == 0) {
473 callback_func *cb_func;
475 struct callback *cb_next;
477 static struct callback *callbacks = NULL;
480 callback_register(callback_func *func, void *arg)
484 cb = malloc(sizeof(struct callback));
486 errx(1, "unable to allocate memory for callback");
489 cb->cb_next = callbacks;
493 /* specially-handled commands */
494 static void setifaddr(const char *, int, int, const struct afswtch *);
495 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
497 static void setifdstaddr(const char *, int, int, const struct afswtch *);
498 static const struct cmd setifdstaddr_cmd =
499 DEF_CMD("ifdstaddr", 0, setifdstaddr);
502 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
504 const struct afswtch *afp, *nafp;
508 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
509 afp = uafp != NULL ? uafp : af_getbyname("inet");
511 ifr.ifr_addr.sa_family =
512 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
513 AF_INET : afp->af_af;
515 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
516 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
521 p = cmd_lookup(*argv, iscreate);
523 if (iscreate && p == NULL) {
525 * Push the clone create callback so the new
526 * device is created and can be used for any
527 * remaining arguments.
531 errx(1, "internal error, no callback");
532 callbacks = cb->cb_next;
533 cb->cb_func(s, cb->cb_arg);
536 * Handle any address family spec that
537 * immediately follows and potentially
538 * recreate the socket.
540 nafp = af_getbyname(*argv);
550 * Look for a normal parameter.
556 * Not a recognized command, choose between setting
557 * the interface address and the dst address.
559 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
561 if (p->c_u.c_func || p->c_u.c_func2) {
562 if (p->c_parameter == NEXTARG) {
564 errx(1, "'%s' requires argument",
566 p->c_u.c_func(argv[1], 0, s, afp);
568 } else if (p->c_parameter == OPTARG) {
569 p->c_u.c_func(argv[1], 0, s, afp);
572 } else if (p->c_parameter == NEXTARG2) {
574 errx(1, "'%s' requires 2 arguments",
576 p->c_u.c_func2(argv[1], argv[2], s, afp);
577 argc -= 2, argv += 2;
579 p->c_u.c_func(*argv, p->c_parameter, s, afp);
585 * Do any post argument processing required by the address family.
587 if (afp->af_postproc != NULL)
588 afp->af_postproc(s, afp);
590 * Do deferred callbacks registered while processing
591 * command-line arguments.
593 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
594 cb->cb_func(s, cb->cb_arg);
596 * Do deferred operations.
599 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
600 warnx("interface %s cannot change %s addresses!",
607 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
608 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
610 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
611 /* means no previous address for interface */
613 Perror("ioctl (SIOCDIFADDR)");
617 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
618 warnx("interface %s cannot change %s addresses!",
623 if (newaddr && (setaddr || setmask)) {
624 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
625 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
626 Perror("ioctl (SIOCAIFADDR)");
635 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
637 if (afp->af_getaddr == NULL)
640 * Delay the ioctl to set the interface addr until flags are all set.
641 * The address interpretation may depend on the flags,
642 * and the flags may change when the address is set.
645 if (doalias == 0 && afp->af_af != AF_LINK)
647 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
651 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
653 struct addrinfo *srcres, *dstres;
656 if (afp->af_settunnel == NULL) {
657 warn("address family %s does not support tunnel setup",
662 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
663 errx(1, "error in parsing address string: %s",
664 gai_strerror(ecode));
666 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
667 errx(1, "error in parsing address string: %s",
668 gai_strerror(ecode));
670 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
672 "source and destination address families do not match");
674 afp->af_settunnel(s, srcres, dstres);
676 freeaddrinfo(srcres);
677 freeaddrinfo(dstres);
682 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
685 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
686 err(1, "SIOCDIFPHYADDR");
690 setifnetmask(const char *addr, int dummy __unused, int s,
691 const struct afswtch *afp)
693 if (afp->af_getaddr != NULL) {
695 afp->af_getaddr(addr, MASK);
700 setifbroadaddr(const char *addr, int dummy __unused, int s,
701 const struct afswtch *afp)
703 if (afp->af_getaddr != NULL)
704 afp->af_getaddr(addr, DSTADDR);
708 setifipdst(const char *addr, int dummy __unused, int s,
709 const struct afswtch *afp)
711 const struct afswtch *inet;
713 inet = af_getbyname("inet");
716 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)
759 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
761 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
762 Perror("ioctl (SIOCGIFFLAGS)");
765 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
766 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
773 my_ifr.ifr_flags = flags & 0xffff;
774 my_ifr.ifr_flagshigh = flags >> 16;
775 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
780 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
783 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
784 Perror("ioctl (SIOCGIFCAP)");
787 flags = ifr.ifr_curcap;
793 ifr.ifr_reqcap = flags;
794 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
799 setifmetric(const char *val, int dummy __unused, int s,
800 const struct afswtch *afp)
802 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
803 ifr.ifr_metric = atoi(val);
804 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
805 warn("ioctl (set metric)");
809 setifmtu(const char *val, int dummy __unused, int s,
810 const struct afswtch *afp)
812 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
813 ifr.ifr_mtu = atoi(val);
814 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
815 warn("ioctl (set mtu)");
819 setifname(const char *val, int dummy __unused, int s,
820 const struct afswtch *afp)
824 newname = strdup(val);
825 if (newname == NULL) {
826 warn("no memory to set ifname");
829 ifr.ifr_data = newname;
830 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
831 warn("ioctl (set name)");
835 strlcpy(name, newname, sizeof(name));
839 * Even if we just created the interface, we don't need to print
840 * its name because we just nailed it down separately.
846 setifpollcpu(const char *val, int dummy __unused, int s,
847 const struct afswtch *afp)
857 setifflags("-polling", -IFF_POLLING, s, afp);
859 ifr.ifr_pollcpu = pollcpu;
860 if (ioctl(s, SIOCSIFPOLLCPU, (caddr_t)&ifr) < 0) {
861 warn("ioctl (set pollcpu)");
867 * Expand the compacted form of addresses as returned via the
868 * configuration read via sysctl().
871 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
876 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
877 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
878 if ((rtinfo->rti_addrs & (1 << i)) == 0)
880 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
886 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
887 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
888 "\20MULTICAST\21POLLING\22PPROMISC\23MONITOR\24STATICARP\25NPOLLING"
891 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7RSS"
894 * Print the status of the interface. If an address family was
895 * specified, show only it; otherwise, show them all.
898 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
899 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
901 struct rt_addrinfo info;
907 afp = af_getbyname("inet");
911 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
912 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
914 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
916 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
918 printf("%s: ", name);
919 printb("flags", flags, IFFBITS);
920 if (ifm->ifm_data.ifi_metric)
921 printf(" metric %ld", ifm->ifm_data.ifi_metric);
922 if (ifm->ifm_data.ifi_mtu)
923 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
926 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
927 if (ifr.ifr_curcap != 0) {
928 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
931 if (supmedia && ifr.ifr_reqcap != 0) {
932 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
939 while (addrcount > 0) {
940 info.rti_addrs = ifam->ifam_addrs;
941 /* Expand the compacted addresses */
942 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
946 const struct afswtch *p;
947 p = af_getbyfamily(info.rti_info[RTAX_IFA]->sa_family);
948 if (p != NULL && p->af_status != NULL)
949 p->af_status(s, &info);
950 } else if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family)
951 afp->af_status(s, &info);
953 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
955 if (allfamilies || afp->af_af == AF_LINK) {
956 const struct afswtch *lafp;
959 * Hack; the link level address is received separately
960 * from the routing information so any address is not
961 * handled above. Cobble together an entry and invoke
962 * the status method specially.
964 lafp = af_getbyname("lladdr");
966 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
967 lafp->af_status(s, &info);
972 else if (afp->af_other_status != NULL)
973 afp->af_other_status(s);
975 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
976 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
977 printf("%s", ifs.ascii);
979 if (flags & IFF_POLLING) {
980 if (ioctl(s, SIOCGIFPOLLCPU, &ifr) == 0 && ifr.ifr_pollcpu >= 0)
981 printf("\tpollcpu: %d\n", ifr.ifr_pollcpu);
991 af_all_tunnel_status(s);
995 Perror(const char *cmd)
1000 errx(1, "%s: no such interface", cmd);
1004 errx(1, "%s: permission denied", cmd);
1013 * Print a value a la the %b format of the kernel's printf
1016 printb(const char *s, unsigned v, const char *bits)
1021 if (bits && *bits == 8)
1022 printf("%s=%o", s, v);
1024 printf("%s=%x", s, v);
1028 while ((i = *bits++) != '\0') {
1029 if (v & (1 << (i-1))) {
1033 for (; (c = *bits) > 32; bits++)
1036 for (; *bits > 32; bits++)
1044 ifmaybeload(const char *name)
1046 struct module_stat mstat;
1048 char ifkind[35], *dp;
1051 /* turn interface and unit into module name */
1052 strcpy(ifkind, "if_");
1053 for (cp = name, dp = ifkind + 3;
1054 (*cp != 0) && !isdigit(*cp); cp++, dp++)
1058 /* scan files in kernel */
1059 mstat.version = sizeof(struct module_stat);
1060 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1061 /* scan modules in file */
1062 for (modid = kldfirstmod(fileid); modid > 0;
1063 modid = modfnext(modid)) {
1064 if (modstat(modid, &mstat) < 0)
1066 /* strip bus name if present */
1067 if ((cp = strchr(mstat.name, '/')) != NULL) {
1072 /* already loaded? */
1073 if (strncmp(name, cp, strlen(cp)) == 0 ||
1074 strncmp(ifkind, cp, strlen(cp)) == 0)
1079 /* not present, we should try to load it */
1083 static struct cmd basic_cmds[] = {
1084 DEF_CMD("up", IFF_UP, setifflags),
1085 DEF_CMD("down", -IFF_UP, setifflags),
1086 DEF_CMD("arp", -IFF_NOARP, setifflags),
1087 DEF_CMD("-arp", IFF_NOARP, setifflags),
1088 DEF_CMD("debug", IFF_DEBUG, setifflags),
1089 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1090 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1091 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1092 DEF_CMD("add", IFF_UP, notealias),
1093 DEF_CMD("alias", IFF_UP, notealias),
1094 DEF_CMD("-alias", -IFF_UP, notealias),
1095 DEF_CMD("delete", -IFF_UP, notealias),
1096 DEF_CMD("remove", -IFF_UP, notealias),
1098 #define EN_SWABIPS 0x1000
1099 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1100 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1102 DEF_CMD_ARG("netmask", setifnetmask),
1103 DEF_CMD_ARG("metric", setifmetric),
1104 DEF_CMD_ARG("broadcast", setifbroadaddr),
1106 DEF_CMD_ARG("ipdst", setifipdst),
1108 DEF_CMD_ARG2("tunnel", settunnel),
1109 DEF_CMD("-tunnel", 0, deletetunnel),
1110 DEF_CMD("deletetunnel", 0, deletetunnel),
1111 DEF_CMD("link0", IFF_LINK0, setifflags),
1112 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1113 DEF_CMD("link1", IFF_LINK1, setifflags),
1114 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1115 DEF_CMD("link2", IFF_LINK2, setifflags),
1116 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1117 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1118 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1119 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1120 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1121 DEF_CMD("polling", IFF_POLLING, setifflags),
1122 DEF_CMD("-polling", -IFF_POLLING, setifflags),
1123 DEF_CMD("npolling", IFF_NPOLLING, setifflags),
1124 DEF_CMD("-npolling", -IFF_NPOLLING, setifflags),
1125 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1126 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1127 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1128 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1129 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1130 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1131 DEF_CMD("rss", IFCAP_RSS, setifcap),
1132 DEF_CMD("-rss", -IFCAP_RSS, 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)
1141 static __constructor void
1144 #define N(a) (sizeof(a) / sizeof(a[0]))
1147 for (i = 0; i < N(basic_cmds); i++)
1148 cmd_register(&basic_cmds[i]);