2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
40 static const char rcsid[] =
44 #include <sys/param.h>
45 #include <sys/ioctl.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
49 #include <sys/module.h>
50 #include <sys/linker.h>
52 #include <net/ethernet.h>
54 #include <net/if_var.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <arpa/inet.h>
79 * Since "struct ifreq" is composed of various union members, callers
80 * should pay special attention to interprete the value.
81 * (.e.g. little/big endian difference in the structure.)
97 int printkeys = 0; /* Print keying material for interfaces. */
99 static int ifconfig(int argc, char *const *argv, int iscreate,
100 const struct afswtch *afp);
101 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
102 struct ifaddrs *ifa);
103 static void tunnel_status(int s);
104 static void usage(void);
106 static struct afswtch *af_getbyname(const char *name);
107 static struct afswtch *af_getbyfamily(int af);
108 static void af_other_status(int);
110 static struct option *opts = NULL;
113 opt_register(struct option *p)
125 /* XXX not right but close enough for now */
127 for (p = opts; p != NULL; p = p->next) {
128 strlcat(options, p->opt_usage, sizeof(options));
129 strlcat(options, " ", sizeof(options));
133 "usage: ifconfig %sinterface address_family [address [dest_address]]\n"
135 " ifconfig interface create\n"
136 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
137 " ifconfig -l [-d] [-u] [address_family]\n"
138 " ifconfig %s[-d] [-m] [-u] [-v]\n",
139 options, options, options);
144 main(int argc, char *argv[])
146 int c, all, namesonly, downonly, uponly;
147 const struct afswtch *afp = NULL;
149 struct ifaddrs *ifap, *ifa;
151 const struct sockaddr_dl *sdl;
152 char options[1024], *cp;
157 all = downonly = uponly = namesonly = noload = verbose = 0;
159 /* Parse leading line options */
160 strlcpy(options, "adklmnuv", sizeof(options));
161 for (p = opts; p != NULL; p = p->next)
162 strlcat(options, p->opt, sizeof(options));
163 while ((c = getopt(argc, argv, options)) != -1) {
165 case 'a': /* scan all interfaces */
168 case 'd': /* restrict scan to "down" interfaces */
174 case 'l': /* scan interface names only */
177 case 'm': /* show media choices in status */
180 case 'n': /* suppress module loading */
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 */
241 ifindex = if_nametoindex(ifname);
244 * NOTE: We must special-case the `create' command
245 * right here as we would otherwise fail when trying
246 * to find the interface.
248 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
249 strcmp(argv[0], "plumb") == 0)) {
250 iflen = strlcpy(name, ifname, sizeof(name));
251 if (iflen >= sizeof(name))
252 errx(1, "%s: cloning name too long",
254 ifconfig(argc, argv, 1, NULL);
258 * NOTE: We have to special-case the `-vnet' command
259 * right here as we would otherwise fail when trying
260 * to find the interface as it lives in another vnet.
262 if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) {
263 iflen = strlcpy(name, ifname, sizeof(name));
264 if (iflen >= sizeof(name))
265 errx(1, "%s: interface name too long",
267 ifconfig(argc, argv, 0, NULL);
270 errx(1, "interface %s does not exist", ifname);
274 /* Check for address family */
276 afp = af_getbyname(*argv);
281 if (getifaddrs(&ifap) != 0)
282 err(EXIT_FAILURE, "getifaddrs");
285 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
286 memset(&paifr, 0, sizeof(paifr));
287 strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
288 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
289 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
290 ifa->ifa_addr->sa_len);
293 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
295 if (ifa->ifa_addr->sa_family == AF_LINK)
296 sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
299 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0)
301 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
302 if (iflen >= sizeof(name)) {
303 warnx("%s: interface name too long, skipping",
309 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
311 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
315 * Are we just listing the interfaces?
325 ifconfig(argc, argv, 0, afp);
327 status(afp, sdl, ifa);
336 static struct afswtch *afs = NULL;
339 af_register(struct afswtch *p)
345 static struct afswtch *
346 af_getbyname(const char *name)
350 for (afp = afs; afp != NULL; afp = afp->af_next)
351 if (strcmp(afp->af_name, name) == 0)
356 static struct afswtch *
357 af_getbyfamily(int af)
361 for (afp = afs; afp != NULL; afp = afp->af_next)
362 if (afp->af_af == af)
368 af_other_status(int s)
371 uint8_t afmask[howmany(AF_MAX, NBBY)];
373 memset(afmask, 0, sizeof(afmask));
374 for (afp = afs; afp != NULL; afp = afp->af_next) {
375 if (afp->af_other_status == NULL)
377 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
379 afp->af_other_status(s);
380 setbit(afmask, afp->af_af);
385 af_all_tunnel_status(int s)
388 uint8_t afmask[howmany(AF_MAX, NBBY)];
390 memset(afmask, 0, sizeof(afmask));
391 for (afp = afs; afp != NULL; afp = afp->af_next) {
392 if (afp->af_status_tunnel == NULL)
394 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
396 afp->af_status_tunnel(s);
397 setbit(afmask, afp->af_af);
401 static struct cmd *cmds = NULL;
404 cmd_register(struct cmd *p)
410 static const struct cmd *
411 cmd_lookup(const char *name, int iscreate)
413 #define N(a) (sizeof(a)/sizeof(a[0]))
416 for (p = cmds; p != NULL; p = p->c_next)
417 if (strcmp(name, p->c_name) == 0) {
431 callback_func *cb_func;
433 struct callback *cb_next;
435 static struct callback *callbacks = NULL;
438 callback_register(callback_func *func, void *arg)
442 cb = malloc(sizeof(struct callback));
444 errx(1, "unable to allocate memory for callback");
447 cb->cb_next = callbacks;
451 /* specially-handled commands */
452 static void setifaddr(const char *, int, int, const struct afswtch *);
453 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
455 static void setifdstaddr(const char *, int, int, const struct afswtch *);
456 static const struct cmd setifdstaddr_cmd =
457 DEF_CMD("ifdstaddr", 0, setifdstaddr);
460 ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp)
462 const struct afswtch *afp, *nafp;
467 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
468 afp = uafp != NULL ? uafp : af_getbyname("inet");
470 ifr.ifr_addr.sa_family =
471 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
472 AF_LOCAL : afp->af_af;
474 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
475 (uafp != NULL || errno != EPROTONOSUPPORT ||
476 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
477 err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family);
480 p = cmd_lookup(*argv, iscreate);
481 if (iscreate && p == NULL) {
483 * Push the clone create callback so the new
484 * device is created and can be used for any
485 * remaining arguments.
489 errx(1, "internal error, no callback");
490 callbacks = cb->cb_next;
491 cb->cb_func(s, cb->cb_arg);
494 * Handle any address family spec that
495 * immediately follows and potentially
496 * recreate the socket.
498 nafp = af_getbyname(*argv);
508 * Look for a normal parameter.
514 * Not a recognized command, choose between setting
515 * the interface address and the dst address.
517 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
519 if (p->c_u.c_func || p->c_u.c_func2) {
520 if (p->c_parameter == NEXTARG) {
522 errx(1, "'%s' requires argument",
524 p->c_u.c_func(argv[1], 0, s, afp);
526 } else if (p->c_parameter == OPTARG) {
527 p->c_u.c_func(argv[1], 0, s, afp);
530 } else if (p->c_parameter == NEXTARG2) {
532 errx(1, "'%s' requires 2 arguments",
534 p->c_u.c_func2(argv[1], argv[2], s, afp);
535 argc -= 2, argv += 2;
537 p->c_u.c_func(*argv, p->c_parameter, s, afp);
543 * Do any post argument processing required by the address family.
545 if (afp->af_postproc != NULL)
546 afp->af_postproc(s, afp);
548 * Do deferred callbacks registered while processing
549 * command-line arguments.
551 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
552 cb->cb_func(s, cb->cb_arg);
554 * Do deferred operations.
557 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
558 warnx("interface %s cannot change %s addresses!",
565 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
566 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
568 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
569 /* means no previous address for interface */
571 Perror("ioctl (SIOCDIFADDR)");
575 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
576 warnx("interface %s cannot change %s addresses!",
581 if (newaddr && (setaddr || setmask)) {
582 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
583 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
584 Perror("ioctl (SIOCAIFADDR)");
593 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
595 if (afp->af_getaddr == NULL)
598 * Delay the ioctl to set the interface addr until flags are all set.
599 * The address interpretation may depend on the flags,
600 * and the flags may change when the address is set.
603 if (doalias == 0 && afp->af_af != AF_LINK)
605 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
609 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
611 struct addrinfo *srcres, *dstres;
614 if (afp->af_settunnel == NULL) {
615 warn("address family %s does not support tunnel setup",
620 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
621 errx(1, "error in parsing address string: %s",
622 gai_strerror(ecode));
624 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
625 errx(1, "error in parsing address string: %s",
626 gai_strerror(ecode));
628 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
630 "source and destination address families do not match");
632 afp->af_settunnel(s, srcres, dstres);
634 freeaddrinfo(srcres);
635 freeaddrinfo(dstres);
640 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
643 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
644 err(1, "SIOCDIFPHYADDR");
648 setifvnet(const char *jname, int dummy __unused, int s,
649 const struct afswtch *afp)
653 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
654 my_ifr.ifr_jid = jail_getid(jname);
655 if (my_ifr.ifr_jid < 0)
656 errx(1, "%s", jail_errmsg);
657 if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0)
658 err(1, "SIOCSIFVNET");
662 setifrvnet(const char *jname, int dummy __unused, int s,
663 const struct afswtch *afp)
667 memcpy(&my_ifr, &ifr, sizeof(my_ifr));
668 my_ifr.ifr_jid = jail_getid(jname);
669 if (my_ifr.ifr_jid < 0)
670 errx(1, "%s", jail_errmsg);
671 if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0)
672 err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name);
676 setifnetmask(const char *addr, int dummy __unused, int s,
677 const struct afswtch *afp)
679 if (afp->af_getaddr != NULL) {
681 afp->af_getaddr(addr, MASK);
686 setifbroadaddr(const char *addr, int dummy __unused, int s,
687 const struct afswtch *afp)
689 if (afp->af_getaddr != NULL)
690 afp->af_getaddr(addr, DSTADDR);
694 setifipdst(const char *addr, int dummy __unused, int s,
695 const struct afswtch *afp)
697 const struct afswtch *inet;
699 inet = af_getbyname("inet");
702 inet->af_getaddr(addr, DSTADDR);
708 notealias(const char *addr, int param, int s, const struct afswtch *afp)
710 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
711 if (setaddr && doalias == 0 && param < 0)
712 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
713 bcopy((caddr_t)rqtosa(af_addreq),
714 (caddr_t)rqtosa(af_ridreq),
715 rqtosa(af_addreq)->sa_len);
727 setifdstaddr(const char *addr, int param __unused, int s,
728 const struct afswtch *afp)
730 if (afp->af_getaddr != NULL)
731 afp->af_getaddr(addr, DSTADDR);
735 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
736 * of the ifreq structure, which may confuse other parts of ifconfig.
737 * Make a private copy so we can avoid that.
740 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
745 memset(&my_ifr, 0, sizeof(my_ifr));
746 (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
748 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) {
749 Perror("ioctl (SIOCGIFFLAGS)");
752 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
759 my_ifr.ifr_flags = flags & 0xffff;
760 my_ifr.ifr_flagshigh = flags >> 16;
761 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
766 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
770 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) {
771 Perror("ioctl (SIOCGIFCAP)");
774 flags = ifr.ifr_curcap;
780 flags &= ifr.ifr_reqcap;
781 ifr.ifr_reqcap = flags;
782 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
787 setifmetric(const char *val, int dummy __unused, int s,
788 const struct afswtch *afp)
790 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
791 ifr.ifr_metric = atoi(val);
792 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
793 warn("ioctl (set metric)");
797 setifmtu(const char *val, int dummy __unused, int s,
798 const struct afswtch *afp)
800 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
801 ifr.ifr_mtu = atoi(val);
802 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
803 warn("ioctl (set mtu)");
807 setifname(const char *val, int dummy __unused, int s,
808 const struct afswtch *afp)
812 newname = strdup(val);
813 if (newname == NULL) {
814 warn("no memory to set ifname");
817 ifr.ifr_data = newname;
818 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
819 warn("ioctl (set name)");
823 strlcpy(name, newname, sizeof(name));
829 setifdescr(const char *val, int dummy __unused, int s,
830 const struct afswtch *afp)
834 newdescr = strdup(val);
835 if (newdescr == NULL) {
836 warn("no memory to set ifdescr");
839 ifr.ifr_buffer.buffer = newdescr;
840 ifr.ifr_buffer.length = strlen(newdescr);
841 if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) {
842 warn("ioctl (set descr)");
851 unsetifdescr(const char *val, int value, int s, const struct afswtch *afp)
854 setifdescr("", 0, s, 0);
858 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
859 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
860 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP"
863 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \
864 "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \
868 * Print the status of the interface. If an address family was
869 * specified, show only it; otherwise, show them all.
872 status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
881 ifr.ifr_addr.sa_family = AF_LOCAL;
884 ifr.ifr_addr.sa_family =
885 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
887 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
889 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
891 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
893 printf("%s: ", name);
894 printb("flags", ifa->ifa_flags, IFFBITS);
895 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
896 printf(" metric %d", ifr.ifr_metric);
897 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
898 printf(" mtu %d", ifr.ifr_mtu);
901 descr = reallocf(descr, descrlen);
904 ifr.ifr_buffer.buffer = descr;
905 ifr.ifr_buffer.length = descrlen;
906 if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) {
907 if (strlen(descr) > 0)
908 printf("\tdescription: %s\n", descr);
911 if (errno == ENAMETOOLONG) {
913 descr = reallocf(descr, descrlen);
915 } while (errno == ENAMETOOLONG);
918 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
919 if (ifr.ifr_curcap != 0) {
920 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
923 if (supmedia && ifr.ifr_reqcap != 0) {
924 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
931 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
932 if (ift->ifa_addr == NULL)
934 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
937 const struct afswtch *p;
938 p = af_getbyfamily(ift->ifa_addr->sa_family);
939 if (p != NULL && p->af_status != NULL)
940 p->af_status(s, ift);
941 } else if (afp->af_af == ift->ifa_addr->sa_family)
942 afp->af_status(s, ift);
945 if (allfamilies || afp->af_af == AF_LINK) {
946 const struct afswtch *lafp;
949 * Hack; the link level address is received separately
950 * from the routing information so any address is not
951 * handled above. Cobble together an entry and invoke
952 * the status method specially.
954 lafp = af_getbyname("lladdr");
956 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
957 lafp->af_status(s, &info);
963 else if (afp->af_other_status != NULL)
964 afp->af_other_status(s);
966 strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
967 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
968 printf("%s", ifs.ascii);
977 af_all_tunnel_status(s);
981 Perror(const char *cmd)
986 errx(1, "%s: no such interface", cmd);
990 errx(1, "%s: permission denied", cmd);
999 * Print a value a la the %b format of the kernel's printf
1002 printb(const char *s, unsigned v, const char *bits)
1007 if (bits && *bits == 8)
1008 printf("%s=%o", s, v);
1010 printf("%s=%x", s, v);
1014 while ((i = *bits++) != '\0') {
1015 if (v & (1 << (i-1))) {
1019 for (; (c = *bits) > 32; bits++)
1022 for (; *bits > 32; bits++)
1030 ifmaybeload(const char *name)
1032 #define MOD_PREFIX_LEN 3 /* "if_" */
1033 struct module_stat mstat;
1035 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1038 /* loading suppressed by the user */
1042 /* trim the interface number off the end */
1043 strlcpy(ifname, name, sizeof(ifname));
1044 for (dp = ifname; *dp != 0; dp++)
1050 /* turn interface and unit into module name */
1051 strcpy(ifkind, "if_");
1052 strlcpy(ifkind + MOD_PREFIX_LEN, ifname,
1053 sizeof(ifkind) - MOD_PREFIX_LEN);
1055 /* scan files in kernel */
1056 mstat.version = sizeof(struct module_stat);
1057 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1058 /* scan modules in file */
1059 for (modid = kldfirstmod(fileid); modid > 0;
1060 modid = modfnext(modid)) {
1061 if (modstat(modid, &mstat) < 0)
1063 /* strip bus name if present */
1064 if ((cp = strchr(mstat.name, '/')) != NULL) {
1069 /* already loaded? */
1070 if (strncmp(ifname, cp, strlen(ifname) + 1) == 0 ||
1071 strncmp(ifkind, cp, strlen(ifkind) + 1) == 0)
1076 /* not present, we should try to load it */
1080 static struct cmd basic_cmds[] = {
1081 DEF_CMD("up", IFF_UP, setifflags),
1082 DEF_CMD("down", -IFF_UP, setifflags),
1083 DEF_CMD("arp", -IFF_NOARP, setifflags),
1084 DEF_CMD("-arp", IFF_NOARP, setifflags),
1085 DEF_CMD("debug", IFF_DEBUG, setifflags),
1086 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1087 DEF_CMD_ARG("description", setifdescr),
1088 DEF_CMD_ARG("descr", setifdescr),
1089 DEF_CMD("-description", 0, unsetifdescr),
1090 DEF_CMD("-descr", 0, unsetifdescr),
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_ARG("ipdst", setifipdst),
1107 DEF_CMD_ARG2("tunnel", settunnel),
1108 DEF_CMD("-tunnel", 0, deletetunnel),
1109 DEF_CMD("deletetunnel", 0, deletetunnel),
1110 DEF_CMD_ARG("vnet", setifvnet),
1111 DEF_CMD_ARG("-vnet", setifrvnet),
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("rxcsum", IFCAP_RXCSUM, setifcap),
1123 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1124 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1125 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1126 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1127 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1128 DEF_CMD("polling", IFCAP_POLLING, setifcap),
1129 DEF_CMD("-polling", -IFCAP_POLLING, setifcap),
1130 DEF_CMD("tso", IFCAP_TSO, setifcap),
1131 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1132 DEF_CMD("lro", IFCAP_LRO, setifcap),
1133 DEF_CMD("-lro", -IFCAP_LRO, setifcap),
1134 DEF_CMD("wol", IFCAP_WOL, setifcap),
1135 DEF_CMD("-wol", -IFCAP_WOL, setifcap),
1136 DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap),
1137 DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap),
1138 DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap),
1139 DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap),
1140 DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap),
1141 DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap),
1142 DEF_CMD("normal", -IFF_LINK0, setifflags),
1143 DEF_CMD("compress", IFF_LINK0, setifflags),
1144 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1145 DEF_CMD_ARG("mtu", setifmtu),
1146 DEF_CMD_ARG("name", setifname),
1149 static __constructor void
1152 #define N(a) (sizeof(a) / sizeof(a[0]))
1155 for (i = 0; i < N(basic_cmds); i++)
1156 cmd_register(&basic_cmds[i]);