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 $
30 * $DragonFly: src/sbin/ifconfig/ifconfig.c,v 1.30 2007/09/30 04:37:27 sephe Exp $
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
38 #include <sys/module.h>
39 #include <sys/linker.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);
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", name);
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)
375 printf("%s\n", name);
380 static struct afswtch *afs = NULL;
383 af_register(struct afswtch *p)
389 static struct afswtch *
390 af_getbyname(const char *name)
394 for (afp = afs; afp != NULL; afp = afp->af_next)
395 if (strcmp(afp->af_name, name) == 0)
400 static struct afswtch *
401 af_getbyfamily(int af)
405 for (afp = afs; afp != NULL; afp = afp->af_next)
406 if (afp->af_af == af)
412 af_other_status(int s)
415 uint8_t afmask[howmany(AF_MAX, NBBY)];
417 memset(afmask, 0, sizeof(afmask));
418 for (afp = afs; afp != NULL; afp = afp->af_next) {
419 if (afp->af_other_status == NULL)
421 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
423 afp->af_other_status(s);
424 setbit(afmask, afp->af_af);
429 af_all_tunnel_status(int s)
432 uint8_t afmask[howmany(AF_MAX, NBBY)];
434 memset(afmask, 0, sizeof(afmask));
435 for (afp = afs; afp != NULL; afp = afp->af_next) {
436 if (afp->af_status_tunnel == NULL)
438 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
440 afp->af_status_tunnel(s);
441 setbit(afmask, afp->af_af);
445 static struct cmd *cmds = NULL;
448 cmd_register(struct cmd *p)
454 static const struct cmd *
455 cmd_lookup(const char *name, int iscreate)
457 #define N(a) (sizeof(a)/sizeof(a[0]))
460 for (p = cmds; p != NULL; p = p->c_next)
461 if (strcmp(name, p->c_name) == 0)
475 callback_func *cb_func;
477 struct callback *cb_next;
479 static struct callback *callbacks = NULL;
482 callback_register(callback_func *func, void *arg)
486 cb = malloc(sizeof(struct callback));
488 errx(1, "unable to allocate memory for callback");
491 cb->cb_next = callbacks;
495 /* specially-handled commands */
496 static void setifaddr(const char *, int, int, const struct afswtch *);
497 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
499 static void setifdstaddr(const char *, int, int, const struct afswtch *);
500 static const struct cmd setifdstaddr_cmd =
501 DEF_CMD("ifdstaddr", 0, setifdstaddr);
504 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
506 const struct afswtch *afp, *nafp;
510 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
511 afp = uafp != NULL ? uafp : af_getbyname("inet");
513 ifr.ifr_addr.sa_family =
514 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
515 AF_INET : afp->af_af;
517 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
518 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
523 p = cmd_lookup(*argv, iscreate);
525 if (iscreate && p == NULL) {
527 * Push the clone create callback so the new
528 * device is created and can be used for any
529 * remaining arguments.
533 errx(1, "internal error, no callback");
534 callbacks = cb->cb_next;
535 cb->cb_func(s, cb->cb_arg);
538 * Handle any address family spec that
539 * immediately follows and potentially
540 * recreate the socket.
542 nafp = af_getbyname(*argv);
552 * Look for a normal parameter.
558 * Not a recognized command, choose between setting
559 * the interface address and the dst address.
561 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
563 if (p->c_u.c_func || p->c_u.c_func2) {
564 if (p->c_parameter == NEXTARG) {
566 errx(1, "'%s' requires argument",
568 p->c_u.c_func(argv[1], 0, s, afp);
570 } else if (p->c_parameter == OPTARG) {
571 p->c_u.c_func(argv[1], 0, s, afp);
574 } else if (p->c_parameter == NEXTARG2) {
576 errx(1, "'%s' requires 2 arguments",
578 p->c_u.c_func2(argv[1], argv[2], s, afp);
579 argc -= 2, argv += 2;
581 p->c_u.c_func(*argv, p->c_parameter, s, afp);
587 * Do any post argument processing required by the address family.
589 if (afp->af_postproc != NULL)
590 afp->af_postproc(s, afp);
592 * Do deferred callbacks registered while processing
593 * command-line arguments.
595 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
596 cb->cb_func(s, cb->cb_arg);
598 * Do deferred operations.
601 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
602 warnx("interface %s cannot change %s addresses!",
609 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
610 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
612 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
613 /* means no previous address for interface */
615 Perror("ioctl (SIOCDIFADDR)");
619 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
620 warnx("interface %s cannot change %s addresses!",
625 if (newaddr && (setaddr || setmask)) {
626 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
627 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
628 Perror("ioctl (SIOCAIFADDR)");
637 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
639 if (afp->af_getaddr == NULL)
642 * Delay the ioctl to set the interface addr until flags are all set.
643 * The address interpretation may depend on the flags,
644 * and the flags may change when the address is set.
647 if (doalias == 0 && afp->af_af != AF_LINK)
649 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
653 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
655 struct addrinfo *srcres, *dstres;
658 if (afp->af_settunnel == NULL) {
659 warn("address family %s does not support tunnel setup",
664 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
665 errx(1, "error in parsing address string: %s",
666 gai_strerror(ecode));
668 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
669 errx(1, "error in parsing address string: %s",
670 gai_strerror(ecode));
672 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
674 "source and destination address families do not match");
676 afp->af_settunnel(s, srcres, dstres);
678 freeaddrinfo(srcres);
679 freeaddrinfo(dstres);
684 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
687 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
688 err(1, "SIOCDIFPHYADDR");
692 setifnetmask(const char *addr, int dummy __unused, int s,
693 const struct afswtch *afp)
695 if (afp->af_getaddr != NULL) {
697 afp->af_getaddr(addr, MASK);
702 setifbroadaddr(const char *addr, int dummy __unused, int s,
703 const struct afswtch *afp)
705 if (afp->af_getaddr != NULL)
706 afp->af_getaddr(addr, DSTADDR);
710 setifipdst(const char *addr, int dummy __unused, int s,
711 const struct afswtch *afp)
713 const struct afswtch *inet;
715 inet = af_getbyname("inet");
718 inet->af_getaddr(addr, DSTADDR);
725 notealias(const char *addr, int param, int s, const struct afswtch *afp)
727 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
728 if (setaddr && doalias == 0 && param < 0)
729 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
730 bcopy((caddr_t)rqtosa(af_addreq),
731 (caddr_t)rqtosa(af_ridreq),
732 rqtosa(af_addreq)->sa_len);
744 setifdstaddr(const char *addr, int param __unused, int s,
745 const struct afswtch *afp)
747 if (afp->af_getaddr != NULL)
748 afp->af_getaddr(addr, DSTADDR);
752 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
753 * of the ifreq structure, which may confuse other parts of ifconfig.
754 * Make a private copy so we can avoid that.
757 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
761 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
763 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
764 Perror("ioctl (SIOCGIFFLAGS)");
767 strncpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
768 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
775 my_ifr.ifr_flags = flags & 0xffff;
776 my_ifr.ifr_flagshigh = flags >> 16;
777 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
782 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 ifr.ifr_reqcap = flags;
796 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
801 setifmetric(const char *val, int dummy __unused, int s,
802 const struct afswtch *afp)
804 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
805 ifr.ifr_metric = atoi(val);
806 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
807 warn("ioctl (set metric)");
811 setifmtu(const char *val, int dummy __unused, int s,
812 const struct afswtch *afp)
814 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
815 ifr.ifr_mtu = atoi(val);
816 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
817 warn("ioctl (set mtu)");
821 setifname(const char *val, int dummy __unused, int s,
822 const struct afswtch *afp)
826 newname = strdup(val);
827 if (newname == NULL) {
828 warn("no memory to set ifname");
831 ifr.ifr_data = newname;
832 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
833 warn("ioctl (set name)");
837 strlcpy(name, newname, sizeof(name));
841 * Even if we just created the interface, we don't need to print
842 * its name because we just nailed it down separately.
848 setifpollcpu(const char *val, int dummy __unused, int s,
849 const struct afswtch *afp)
859 setifflags("-polling", -IFF_POLLING, s, afp);
861 ifr.ifr_pollcpu = pollcpu;
862 if (ioctl(s, SIOCSIFPOLLCPU, (caddr_t)&ifr) < 0) {
863 warn("ioctl (set pollcpu)");
869 * Expand the compacted form of addresses as returned via the
870 * configuration read via sysctl().
873 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
878 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
879 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
880 if ((rtinfo->rti_addrs & (1 << i)) == 0)
882 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
888 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
889 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
890 "\20MULTICAST\21POLLING\22PPROMISC\23MONITOR\24STATICARP\25NPOLLING"
893 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7RSS"
896 * Print the status of the interface. If an address family was
897 * specified, show only it; otherwise, show them all.
900 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
901 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
903 struct rt_addrinfo info;
909 afp = af_getbyname("inet");
913 ifr.ifr_addr.sa_family = afp->af_af == AF_LINK ? AF_INET : afp->af_af;
914 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
916 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
918 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
920 printf("%s: ", name);
921 printb("flags", flags, IFFBITS);
922 if (ifm->ifm_data.ifi_metric)
923 printf(" metric %ld", ifm->ifm_data.ifi_metric);
924 if (ifm->ifm_data.ifi_mtu)
925 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
928 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
929 if (ifr.ifr_curcap != 0) {
930 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
933 if (supmedia && ifr.ifr_reqcap != 0) {
934 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
941 while (addrcount > 0) {
942 info.rti_addrs = ifam->ifam_addrs;
943 /* Expand the compacted addresses */
944 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
948 const struct afswtch *p;
949 p = af_getbyfamily(info.rti_info[RTAX_IFA]->sa_family);
950 if (p != NULL && p->af_status != NULL)
951 p->af_status(s, &info);
952 } else if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family)
953 afp->af_status(s, &info);
955 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
957 if (allfamilies || afp->af_af == AF_LINK) {
958 const struct afswtch *lafp;
961 * Hack; the link level address is received separately
962 * from the routing information so any address is not
963 * handled above. Cobble together an entry and invoke
964 * the status method specially.
966 lafp = af_getbyname("lladdr");
968 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
969 lafp->af_status(s, &info);
974 else if (afp->af_other_status != NULL)
975 afp->af_other_status(s);
977 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
978 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
979 printf("%s", ifs.ascii);
981 if (flags & IFF_POLLING) {
982 if (ioctl(s, SIOCGIFPOLLCPU, &ifr) == 0 && ifr.ifr_pollcpu >= 0)
983 printf("\tpollcpu: %d\n", ifr.ifr_pollcpu);
993 af_all_tunnel_status(s);
997 Perror(const char *cmd)
1002 errx(1, "%s: no such interface", cmd);
1006 errx(1, "%s: permission denied", cmd);
1015 * Print a value a la the %b format of the kernel's printf
1018 printb(const char *s, unsigned v, const char *bits)
1023 if (bits && *bits == 8)
1024 printf("%s=%o", s, v);
1026 printf("%s=%x", s, v);
1030 while ((i = *bits++) != '\0') {
1031 if (v & (1 << (i-1))) {
1035 for (; (c = *bits) > 32; bits++)
1038 for (; *bits > 32; bits++)
1046 ifmaybeload(char *name)
1048 struct module_stat mstat;
1050 char ifkind[35], *cp, *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_POLLING, setifflags),
1123 DEF_CMD("-polling", -IFF_POLLING, 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("normal", -IFF_LINK0, setifflags),
1135 DEF_CMD("compress", IFF_LINK0, setifflags),
1136 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1137 DEF_CMD_ARG("mtu", setifmtu),
1138 DEF_CMD_ARG("name", setifname),
1139 DEF_CMD_ARG("pollcpu", setifpollcpu)
1142 static __constructor void
1145 #define N(a) (sizeof(a) / sizeof(a[0]))
1148 for (i = 0; i < N(basic_cmds); i++)
1149 cmd_register(&basic_cmds[i]);