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.)
82 int printkeys = 0; /* Print keying material for interfaces. */
83 int printifname = 0; /* Print the name of the created interface. */
85 static int ifconfig(int argc, char *const *argv, int iscreate,
86 const struct afswtch *afp);
87 static void status(const struct afswtch *afp, int addrcount,
88 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
89 struct ifa_msghdr *ifam);
90 static void tunnel_status(int s);
91 static void usage(void) __dead2;
93 static struct afswtch *af_getbyname(const char *name);
94 static struct afswtch *af_getbyfamily(int af);
95 static void af_other_status(int);
97 static struct option *opts = NULL;
100 opt_register(struct option *p)
112 /* XXX not right but close enough for now */
114 for (p = opts; p != NULL; p = p->next) {
115 strlcat(options, p->opt_usage, sizeof(options));
116 strlcat(options, " ", sizeof(options));
120 "usage: ifconfig %sinterface address_family [address [dest_address]]\n"
122 " ifconfig interface create\n"
123 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
124 " ifconfig -l [-d] [-u] [address_family]\n"
125 " ifconfig %s[-d] [-m] [-u] [-v]\n",
126 options, options, options);
131 main(int argc, char *argv[])
133 int c, all, namesonly, downonly, uponly;
134 int need_nl = 0, count = 0;
135 const struct afswtch *afp = NULL;
136 int addrcount, ifindex;
137 struct if_msghdr *ifm, *nextifm;
138 struct ifa_msghdr *ifam;
139 struct sockaddr_dl *sdl;
140 char *buf, *lim, *next;
148 all = downonly = uponly = namesonly = verbose = 0;
150 /* Parse leading line options */
151 strlcpy(options, "adklmuv", sizeof(options));
152 for (p = opts; p != NULL; p = p->next)
153 strlcat(options, p->opt, sizeof(options));
154 while ((c = getopt(argc, argv, options)) != -1) {
156 case 'a': /* scan all interfaces */
159 case 'd': /* restrict scan to "down" interfaces */
165 case 'l': /* scan interface names only */
168 case 'm': /* show media choices in status */
171 case 'u': /* restrict scan to "up" interfaces */
178 for (p = opts; p != NULL; p = p->next)
179 if (p->opt[0] == c) {
191 /* -l cannot be used with -a or -m */
192 if (namesonly && (all || supmedia))
196 if (uponly && downonly)
199 /* no arguments is equivalent to '-a' */
200 if (!namesonly && argc < 1)
203 /* -a and -l allow an address family arg to limit the output */
204 if (all || namesonly) {
211 afp = af_getbyname(*argv);
214 if (afp->af_name != NULL)
216 /* leave with afp non-zero */
219 /* not listing, need an argument */
226 /* check and maybe load support for this interface */
229 ifindex = if_nametoindex(ifname);
232 * NOTE: We must special-case the `create' command
233 * right here as we would otherwise fail when trying
234 * to find the interface.
236 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
237 strcmp(argv[0], "plumb") == 0)) {
238 iflen = strlcpy(name, ifname, sizeof(name));
239 if (iflen >= sizeof(name))
240 errx(1, "%s: cloning name too long",
242 ifconfig(argc, argv, 1, NULL);
245 errx(1, "interface %s does not exist", ifname);
249 /* Check for address family */
251 afp = af_getbyname(*argv);
260 mib[3] = 0; /* address family */
261 mib[4] = NET_RT_IFLIST;
262 mib[5] = ifindex; /* interface index */
264 /* if particular family specified, only ask about it */
268 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
269 errx(1, "iflist-sysctl-estimate");
270 if ((buf = malloc(needed)) == NULL)
272 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
273 if (errno == ENOMEM && count++ < 10) {
274 warnx("Routing table grew, retrying");
279 errx(1, "actual retrieval of interface table");
287 ifm = (struct if_msghdr *)next;
289 if (ifm->ifm_type == RTM_IFINFO) {
291 if (ifm->ifm_data.ifi_datalen == 0)
292 ifm->ifm_data.ifi_datalen = sizeof(struct if_data);
293 sdl = (struct sockaddr_dl *)((char *)ifm + sizeof(struct if_msghdr) -
294 sizeof(struct if_data) + ifm->ifm_data.ifi_datalen);
296 sdl = (struct sockaddr_dl *)(ifm + 1);
298 flags = ifm->ifm_flags;
300 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
301 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
303 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
304 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
309 next += ifm->ifm_msglen;
313 nextifm = (struct if_msghdr *)next;
315 if (nextifm->ifm_type != RTM_NEWADDR)
319 ifam = (struct ifa_msghdr *)nextifm;
322 next += nextifm->ifm_msglen;
325 if (sizeof(name) <= sdl->sdl_nlen)
326 name_len = sizeof(name) - 1;
328 name_len = sdl->sdl_nlen;
330 memcpy(name, sdl->sdl_data, name_len);
331 name[name_len] = '\0';
333 if (all || namesonly) {
335 if ((flags & IFF_UP) == 0)
336 continue; /* not up */
339 continue; /* not down */
341 if (afp == NULL || afp->af_af != AF_LINK ||
342 sdl->sdl_type == IFT_ETHER) {
353 ifconfig(argc, argv, 0, afp);
355 status(afp, addrcount, sdl, ifm, ifam);
359 if (namesonly && need_nl > 0)
362 printf("%s\n", name);
367 static struct afswtch *afs = NULL;
370 af_register(struct afswtch *p)
376 static struct afswtch *
377 af_getbyname(const char *name)
381 for (afp = afs; afp != NULL; afp = afp->af_next)
382 if (strcmp(afp->af_name, name) == 0)
387 static struct afswtch *
388 af_getbyfamily(int af)
392 for (afp = afs; afp != NULL; afp = afp->af_next)
393 if (afp->af_af == af)
399 af_other_status(int s)
402 uint8_t afmask[howmany(AF_MAX, NBBY)];
404 memset(afmask, 0, sizeof(afmask));
405 for (afp = afs; afp != NULL; afp = afp->af_next) {
406 if (afp->af_other_status == NULL)
408 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
410 afp->af_other_status(s);
411 setbit(afmask, afp->af_af);
416 af_all_tunnel_status(int s)
419 uint8_t afmask[howmany(AF_MAX, NBBY)];
421 memset(afmask, 0, sizeof(afmask));
422 for (afp = afs; afp != NULL; afp = afp->af_next) {
423 if (afp->af_status_tunnel == NULL)
425 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
427 afp->af_status_tunnel(s);
428 setbit(afmask, afp->af_af);
432 static struct cmd *cmds = NULL;
435 cmd_register(struct cmd *p)
441 static const struct cmd *
442 cmd_lookup(const char *name, int iscreate)
446 for (p = cmds; p != NULL; p = p->c_next)
447 if (strcmp(name, p->c_name) == 0) {
460 callback_func *cb_func;
462 struct callback *cb_next;
464 static struct callback *callbacks = NULL;
467 callback_register(callback_func *func, void *arg)
471 cb = malloc(sizeof(struct callback));
473 errx(1, "unable to allocate memory for callback");
476 cb->cb_next = callbacks;
480 /* specially-handled commands */
481 static void setifaddr(const char *, int, int, const struct afswtch *);
482 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
484 static void setifdstaddr(const char *, int, int, const struct afswtch *);
485 static const struct cmd setifdstaddr_cmd =
486 DEF_CMD("ifdstaddr", 0, setifdstaddr);
489 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
491 const struct afswtch *afp, *nafp;
495 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
496 afp = uafp != NULL ? uafp : af_getbyname("inet");
498 ifr.ifr_addr.sa_family =
499 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
500 AF_LOCAL : afp->af_af;
502 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
503 (uafp != NULL || errno != EAFNOSUPPORT ||
504 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
505 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
510 p = cmd_lookup(*argv, iscreate);
512 if (iscreate && p == NULL) {
514 * Push the clone create callback so the new
515 * device is created and can be used for any
516 * remaining arguments.
520 errx(1, "internal error, no callback");
521 callbacks = cb->cb_next;
522 cb->cb_func(s, cb->cb_arg);
526 * After cloning, make sure we have an up-to-date name
529 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
532 * Handle any address family spec that
533 * immediately follows and potentially
534 * recreate the socket.
536 nafp = af_getbyname(*argv);
546 * Look for a normal parameter.
552 * Not a recognized command, choose between setting
553 * the interface address and the dst address.
555 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
557 if (p->c_u.c_func || p->c_u.c_func2) {
558 if (p->c_parameter == NEXTARG) {
560 errx(1, "'%s' requires argument",
562 p->c_u.c_func(argv[1], 0, s, afp);
564 } else if (p->c_parameter == OPTARG) {
565 p->c_u.c_func(argv[1], 0, s, afp);
568 } else if (p->c_parameter == NEXTARG2) {
570 errx(1, "'%s' requires 2 arguments",
572 p->c_u.c_func2(argv[1], argv[2], s, afp);
573 argc -= 2, argv += 2;
575 p->c_u.c_func(*argv, p->c_parameter, s, afp);
581 * Do any post argument processing required by the address family.
583 if (afp->af_postproc != NULL)
584 afp->af_postproc(s, afp);
586 * Do deferred callbacks registered while processing
587 * command-line arguments.
589 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
590 cb->cb_func(s, cb->cb_arg);
592 * Do deferred operations.
595 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
596 warnx("interface %s cannot change %s addresses!",
603 strlcpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
604 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
606 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
607 /* means no previous address for interface */
609 Perror("ioctl (SIOCDIFADDR)");
613 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
614 warnx("interface %s cannot change %s addresses!",
619 if (newaddr && (setaddr || setmask)) {
620 strlcpy(afp->af_addreq, name, sizeof ifr.ifr_name);
621 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
622 Perror("ioctl (SIOCAIFADDR)");
631 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
633 if (afp->af_getaddr == NULL)
636 * Delay the ioctl to set the interface addr until flags are all set.
637 * The address interpretation may depend on the flags,
638 * and the flags may change when the address is set.
641 if (doalias == 0 && afp->af_af != AF_LINK)
643 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
647 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
649 struct addrinfo *srcres, *dstres;
652 if (afp->af_settunnel == NULL) {
653 warn("address family %s does not support tunnel setup",
658 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
659 errx(1, "error in parsing address string: %s",
660 gai_strerror(ecode));
662 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
663 errx(1, "error in parsing address string: %s",
664 gai_strerror(ecode));
666 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
668 "source and destination address families do not match");
670 afp->af_settunnel(s, srcres, dstres);
672 freeaddrinfo(srcres);
673 freeaddrinfo(dstres);
678 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
681 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
682 err(1, "SIOCDIFPHYADDR");
686 setifnetmask(const char *addr, int dummy __unused, int s,
687 const struct afswtch *afp)
689 if (afp->af_getaddr != NULL) {
691 afp->af_getaddr(addr, MASK);
696 setifbroadaddr(const char *addr, int dummy __unused, int s,
697 const struct afswtch *afp)
699 if (afp->af_getaddr != NULL)
700 afp->af_getaddr(addr, DSTADDR);
704 notealias(const char *addr, int param, int s, const struct afswtch *afp)
706 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
707 if (setaddr && doalias == 0 && param < 0)
708 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
709 bcopy((caddr_t)rqtosa(af_addreq),
710 (caddr_t)rqtosa(af_ridreq),
711 rqtosa(af_addreq)->sa_len);
723 setifdstaddr(const char *addr, int param __unused, int s,
724 const struct afswtch *afp)
726 if (afp->af_getaddr != NULL)
727 afp->af_getaddr(addr, DSTADDR);
731 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
732 * of the ifreq structure, which may confuse other parts of ifconfig.
733 * Make a private copy so we can avoid that.
736 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
740 bcopy((char *)&ifr, (char *)&my_ifr, sizeof(struct ifreq));
742 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
743 Perror("ioctl (SIOCGIFFLAGS)");
745 strlcpy(my_ifr.ifr_name, name, sizeof (my_ifr.ifr_name));
746 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
753 my_ifr.ifr_flags = flags & 0xffff;
754 my_ifr.ifr_flagshigh = flags >> 16;
755 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
760 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
763 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
764 Perror("ioctl (SIOCGIFCAP)");
766 flags = ifr.ifr_curcap;
772 ifr.ifr_reqcap = flags;
773 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
778 setifmetric(const char *val, int dummy __unused, int s,
779 const struct afswtch *afp)
781 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
782 ifr.ifr_metric = atoi(val);
783 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
784 err(1, "ioctl SIOCSIFMETRIC (set metric)");
788 setifmtu(const char *val, int dummy __unused, int s,
789 const struct afswtch *afp)
791 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
792 ifr.ifr_mtu = atoi(val);
793 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
794 err(1, "ioctl SIOCSIFMTU (set mtu)");
798 setiftsolen(const char *val, int dummy __unused, int s,
799 const struct afswtch *afp)
801 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
802 ifr.ifr_tsolen = atoi(val);
803 if (ioctl(s, SIOCSIFTSOLEN, (caddr_t)&ifr) < 0)
804 err(1, "ioctl SIOCSIFTSOLEN (set tsolen)");
808 setifname(const char *val, int dummy __unused, int s,
809 const struct afswtch *afp)
813 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
815 newname = strdup(val);
817 err(1, "no memory to set ifname");
818 ifr.ifr_data = newname;
819 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
821 err(1, "ioctl SIOCSIFNAME (set name)");
824 strlcpy(name, newname, sizeof(name));
829 setifpollcpu(const char *val, int dummy __unused, int s,
830 const struct afswtch *afp)
832 warnx("pollcpu is deprecated, use polling or npolling instead");
833 setifflags("npolling", IFF_NPOLLING, s, afp);
837 * Expand the compacted form of addresses as returned via the
838 * configuration read via sysctl().
841 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
846 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
847 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
848 if ((rtinfo->rti_addrs & (1 << i)) == 0)
850 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
856 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
857 "\10NOARP\11PROMISC\12ALLMULTI\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
858 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25NPOLLING\26IDIRECT"
861 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7RSS" \
862 "\10VLAN_HWCSUM\11TSO"
865 * Print the status of the interface. If an address family was
866 * specified, show only it; otherwise, show them all.
869 status(const struct afswtch *afp, int addrcount, struct sockaddr_dl *sdl,
870 struct if_msghdr *ifm, struct ifa_msghdr *ifam)
872 struct rt_addrinfo info;
878 ifr.ifr_addr.sa_family = AF_LOCAL;
881 ifr.ifr_addr.sa_family =
882 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
884 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
886 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
888 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
890 printf("%s: ", name);
891 printb("flags", flags, IFFBITS);
892 if (ifm->ifm_data.ifi_metric)
893 printf(" metric %ld", ifm->ifm_data.ifi_metric);
894 if (ifm->ifm_data.ifi_mtu)
895 printf(" mtu %ld", ifm->ifm_data.ifi_mtu);
898 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
899 if (ifr.ifr_curcap != 0) {
900 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
903 if (supmedia && ifr.ifr_reqcap != 0) {
904 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
906 if (ifr.ifr_reqcap & IFCAP_TSO) {
907 if (ioctl(s, SIOCGIFTSOLEN,
908 (caddr_t)&ifr) == 0) {
909 printf("\ttsolen %d", ifr.ifr_tsolen);
918 while (addrcount > 0) {
919 info.rti_addrs = ifam->ifam_addrs;
920 /* Expand the compacted addresses */
921 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
925 const struct afswtch *p;
926 p = af_getbyfamily(info.rti_info[RTAX_IFA]->sa_family);
927 if (p != NULL && p->af_status != NULL)
928 p->af_status(s, &info);
929 } else if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family)
930 afp->af_status(s, &info);
932 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
934 if (allfamilies || afp->af_af == AF_LINK) {
935 const struct afswtch *lafp;
938 * Hack; the link level address is received separately
939 * from the routing information so any address is not
940 * handled above. Cobble together an entry and invoke
941 * the status method specially.
943 lafp = af_getbyname("lladdr");
945 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
946 lafp->af_status(s, &info);
951 else if (afp->af_other_status != NULL)
952 afp->af_other_status(s);
954 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
955 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
956 printf("%s", ifs.ascii);
965 af_all_tunnel_status(s);
969 Perror(const char *cmd)
974 errx(1, "%s: no such interface", cmd);
978 errx(1, "%s: permission denied", cmd);
987 * Print a value a la the %pb%i format of the kernel's kprintf()
990 printb(const char *s, unsigned v, const char *bits)
995 if (bits && *bits == 8)
996 printf("%s=%o", s, v);
998 printf("%s=%x", s, v);
1002 while ((i = *bits++) != '\0') {
1003 if (v & (1 << (i-1))) {
1007 for (; (c = *bits) > 32; bits++)
1010 for (; *bits > 32; bits++)
1018 ifmaybeload(const char *name)
1020 #define MOD_PREFIX_LEN 3 /* "if_" */
1021 struct module_stat mstat;
1023 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1026 /* trim the interface number off the end */
1027 strlcpy(ifname, name, sizeof(ifname));
1028 for (dp = ifname; *dp != 0; dp++)
1034 /* turn interface and unit into module name */
1035 strlcpy(ifkind, "if_", sizeof(ifkind));
1036 strlcat(ifkind, ifname, sizeof(ifkind));
1038 /* scan files in kernel */
1039 mstat.version = sizeof(struct module_stat);
1040 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1041 /* scan modules in file */
1042 for (modid = kldfirstmod(fileid); modid > 0;
1043 modid = modfnext(modid)) {
1044 if (modstat(modid, &mstat) < 0)
1046 /* strip bus name if present */
1047 if ((cp = strchr(mstat.name, '/')) != NULL) {
1052 /* already loaded? */
1053 if (strcmp(ifname, cp) == 0 ||
1054 strcmp(ifkind, cp) == 0)
1059 /* not present, we should try to load it */
1063 static struct cmd basic_cmds[] = {
1064 DEF_CMD("up", IFF_UP, setifflags),
1065 DEF_CMD("down", -IFF_UP, setifflags),
1066 DEF_CMD("arp", -IFF_NOARP, setifflags),
1067 DEF_CMD("-arp", IFF_NOARP, setifflags),
1068 DEF_CMD("debug", IFF_DEBUG, setifflags),
1069 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1070 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1071 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1072 DEF_CMD("add", IFF_UP, notealias),
1073 DEF_CMD("alias", IFF_UP, notealias),
1074 DEF_CMD("-alias", -IFF_UP, notealias),
1075 DEF_CMD("delete", -IFF_UP, notealias),
1076 DEF_CMD("remove", -IFF_UP, notealias),
1078 #define EN_SWABIPS 0x1000
1079 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1080 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1082 DEF_CMD_ARG("netmask", setifnetmask),
1083 DEF_CMD_ARG("metric", setifmetric),
1084 DEF_CMD_ARG("broadcast", setifbroadaddr),
1085 DEF_CMD_ARG2("tunnel", settunnel),
1086 DEF_CMD("-tunnel", 0, deletetunnel),
1087 DEF_CMD("deletetunnel", 0, deletetunnel),
1088 DEF_CMD("link0", IFF_LINK0, setifflags),
1089 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1090 DEF_CMD("link1", IFF_LINK1, setifflags),
1091 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1092 DEF_CMD("link2", IFF_LINK2, setifflags),
1093 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1094 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1095 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1096 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1097 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1098 DEF_CMD("polling", IFF_NPOLLING, setifflags),
1099 DEF_CMD("-polling", -IFF_NPOLLING, setifflags),
1100 DEF_CMD("npolling", IFF_NPOLLING, setifflags),
1101 DEF_CMD("-npolling", -IFF_NPOLLING, setifflags),
1102 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1103 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1104 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1105 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1106 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1107 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1108 DEF_CMD("rss", IFCAP_RSS, setifcap),
1109 DEF_CMD("-rss", -IFCAP_RSS, setifcap),
1110 DEF_CMD("tso", IFCAP_TSO, setifcap),
1111 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1112 DEF_CMD("normal", -IFF_LINK0, setifflags),
1113 DEF_CMD("compress", IFF_LINK0, setifflags),
1114 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1115 DEF_CMD_ARG("mtu", setifmtu),
1116 DEF_CMD_ARG("name", setifname),
1117 DEF_CMD_ARG("pollcpu", setifpollcpu),
1118 DEF_CMD_ARG("tsolen", setiftsolen)
1121 static __constructor(101) void
1126 for (i = 0; i < nitems(basic_cmds); i++)
1127 cmd_register(&basic_cmds[i]);