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>
67 * Since "struct ifreq" is composed of various union members, callers
68 * should pay special attention to interpret the value.
69 * (.e.g. little/big endian difference in the structure.)
83 int printkeys = 0; /* Print keying material for interfaces. */
84 int printifname = 0; /* Print the name of the created interface. */
87 /* Formatter strings */
88 char *f_inet, *f_inet6, *f_ether, *f_addr;
90 static int ifconfig(int argc, char *const *argv, int iscreate,
91 const struct afswtch *afp);
92 static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl,
94 static void tunnel_status(int s);
95 static void usage(void) __dead2;
97 static struct afswtch *af_getbyname(const char *name);
98 static struct afswtch *af_getbyfamily(int af);
99 static void af_other_status(int);
100 static void printifnamemaybe(void);
101 static void freeformat(void);
102 static void setformat(char *input);
104 static struct option *opts = NULL;
107 opt_register(struct option *p)
119 /* XXX not right but close enough for now */
121 for (p = opts; p != NULL; p = p->next) {
122 strlcat(options, p->opt_usage, sizeof(options));
123 strlcat(options, " ", sizeof(options));
127 "usage: ifconfig %s[-n] [-f type:format] interface address_family\n"
128 " [address [dest_address]] [parameters]\n"
129 " ifconfig [-n] interface create\n"
130 " ifconfig [-n] interface destroy\n"
131 " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n"
132 " ifconfig -l [-d] [-u] [address_family]\n"
133 " ifconfig %s[-d] [-m] [-u] [-v]\n",
134 options, options, options);
139 printifnamemaybe(void)
142 printf("%s\n", name);
159 setformat(char *input)
161 char *formatstr, *category, *modifier;
164 formatstr = strdup(input);
165 if (formatstr == NULL)
166 err(1, "no memory to set format");
168 while ((category = strsep(&formatstr, ",")) != NULL) {
169 modifier = strchr(category, ':');
170 if (modifier == NULL || modifier[1] == '\0') {
171 warnx("skip invalid format specification: %s\n",
180 if (strcmp(category, "addr") == 0)
182 else if (strcmp(category, "ether") == 0)
184 else if (strcmp(category, "inet") == 0)
186 else if (strcmp(category, "inet6") == 0)
190 *fp = strdup(modifier);
201 main(int argc, char *argv[])
203 int c, all, namesonly, downonly, uponly;
205 const struct afswtch *afp = NULL;
206 const struct sockaddr_dl *sdl;
208 struct ifaddrs *ifap, *ifa;
212 char *envformat, *cp;
215 all = downonly = uponly = namesonly = verbose = noload = 0;
216 f_inet = f_inet6 = f_ether = f_addr = NULL;
219 * Ensure we print interface name when expected to,
220 * even if we terminate early due to error.
222 atexit(printifnamemaybe);
224 envformat = getenv("IFCONFIG_FORMAT");
225 if (envformat != NULL)
226 setformat(envformat);
228 /* Parse leading line options */
229 strlcpy(options, "adf:klmnuv", sizeof(options));
230 for (p = opts; p != NULL; p = p->next)
231 strlcat(options, p->opt, sizeof(options));
232 while ((c = getopt(argc, argv, options)) != -1) {
234 case 'a': /* scan all interfaces */
237 case 'd': /* restrict scan to "down" interfaces */
246 case 'l': /* scan interface names only */
249 case 'm': /* show media choices in status */
252 case 'n': /* suppress module loading */
255 case 'u': /* restrict scan to "up" interfaces */
262 for (p = opts; p != NULL; p = p->next)
263 if (p->opt[0] == c) {
275 /* -l cannot be used with -a or -m */
276 if (namesonly && (all || supmedia))
280 if (uponly && downonly)
283 /* no arguments is equivalent to '-a' */
284 if (!namesonly && argc < 1)
287 /* -a and -l allow an address family arg to limit the output */
288 if (all || namesonly) {
295 afp = af_getbyname(*argv);
298 if (afp->af_name != NULL)
300 /* leave with afp non-zero */
303 /* not listing, need an argument */
310 /* check and maybe load support for this interface */
313 ifindex = if_nametoindex(ifname);
316 * NOTE: We must special-case the `create' command
317 * right here as we would otherwise fail when trying
318 * to find the interface.
320 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
321 strcmp(argv[0], "plumb") == 0)) {
322 iflen = strlcpy(name, ifname, sizeof(name));
323 if (iflen >= sizeof(name))
324 errx(1, "%s: cloning name too long",
326 ifconfig(argc, argv, 1, NULL);
329 errx(1, "interface %s does not exist", ifname);
332 * Do not allow to use `create` command as hostname
333 * if address family is not specified.
335 if (argc > 0 && (strcmp(argv[0], "create") == 0 ||
336 strcmp(argv[0], "plumb") == 0)) {
338 errx(1, "interface %s alreay exists",
345 /* Check for address family */
347 afp = af_getbyname(*argv);
352 if (getifaddrs(&ifap) != 0)
353 err(1, "getifaddrs");
357 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
358 memset(&paifr, 0, sizeof(paifr));
359 strlcpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name));
360 if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
361 memcpy(&paifr.ifr_addr, ifa->ifa_addr,
362 ifa->ifa_addr->sa_len);
365 if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
367 if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0)
369 iflen = strlcpy(name, ifa->ifa_name, sizeof(name));
370 if (iflen >= sizeof(name)) {
371 warnx("%s: interface name too long, skipping",
377 if (downonly && (ifa->ifa_flags & IFF_UP) != 0)
379 if (uponly && (ifa->ifa_flags & IFF_UP) == 0)
382 if (ifa->ifa_addr->sa_family == AF_LINK)
383 sdl = (const struct sockaddr_dl *)ifa->ifa_addr;
387 /* Are we just listing the interfaces? */
390 afp->af_af != AF_LINK ||
391 (sdl != NULL && sdl->sdl_type == IFT_ETHER)) {
392 printf("%s%s", (ifindex > 0 ? " " : ""), name);
399 ifconfig(argc, argv, 0, afp);
401 status(afp, sdl, ifa);
414 static struct afswtch *afs = NULL;
417 af_register(struct afswtch *p)
423 static struct afswtch *
424 af_getbyname(const char *name)
428 for (afp = afs; afp != NULL; afp = afp->af_next)
429 if (strcmp(afp->af_name, name) == 0)
434 static struct afswtch *
435 af_getbyfamily(int af)
439 for (afp = afs; afp != NULL; afp = afp->af_next)
440 if (afp->af_af == af)
446 af_other_status(int s)
449 uint8_t afmask[howmany(AF_MAX, NBBY)];
451 memset(afmask, 0, sizeof(afmask));
452 for (afp = afs; afp != NULL; afp = afp->af_next) {
453 if (afp->af_other_status == NULL)
455 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
457 afp->af_other_status(s);
458 setbit(afmask, afp->af_af);
463 af_all_tunnel_status(int s)
466 uint8_t afmask[howmany(AF_MAX, NBBY)];
468 memset(afmask, 0, sizeof(afmask));
469 for (afp = afs; afp != NULL; afp = afp->af_next) {
470 if (afp->af_status_tunnel == NULL)
472 if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af))
474 afp->af_status_tunnel(s);
475 setbit(afmask, afp->af_af);
479 static struct cmd *cmds = NULL;
482 cmd_register(struct cmd *p)
488 static const struct cmd *
489 cmd_lookup(const char *name, int iscreate)
493 for (p = cmds; p != NULL; p = p->c_next) {
494 if (strcmp(name, p->c_name) == 0) {
509 callback_func *cb_func;
511 struct callback *cb_next;
513 static struct callback *callbacks = NULL;
516 callback_register(callback_func *func, void *arg)
520 cb = malloc(sizeof(struct callback));
522 errx(1, "unable to allocate memory for callback");
525 cb->cb_next = callbacks;
529 /* specially-handled commands */
530 static void setifaddr(const char *, int, int, const struct afswtch *);
531 static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr);
533 static void setifdstaddr(const char *, int, int, const struct afswtch *);
534 static const struct cmd setifdstaddr_cmd =
535 DEF_CMD("ifdstaddr", 0, setifdstaddr);
538 ifconfig(int argc, char *const *argv, int iscreate,
539 const struct afswtch *uafp)
541 const struct afswtch *afp, *nafp;
545 strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
546 afp = uafp != NULL ? uafp : af_getbyname("inet");
548 ifr.ifr_addr.sa_family =
549 afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ?
550 AF_LOCAL : afp->af_af;
552 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 &&
553 (uafp != NULL || errno != EAFNOSUPPORT ||
554 (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0))
555 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
560 p = cmd_lookup(*argv, iscreate);
562 if (iscreate && p == NULL) {
564 * Push the clone create callback so the new
565 * device is created and can be used for any
566 * remaining arguments.
570 errx(1, "internal error, no callback");
571 callbacks = cb->cb_next;
572 cb->cb_func(s, cb->cb_arg);
576 * After cloning, make sure we have an up-to-date name
579 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
582 * Handle any address family spec that
583 * immediately follows and potentially
584 * recreate the socket.
586 nafp = af_getbyname(*argv);
596 * Look for a normal parameter.
602 * Not a recognized command, choose between setting
603 * the interface address and the dst address.
605 p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd);
607 if (p->c_u.c_func || p->c_u.c_func2) {
608 if (p->c_parameter == NEXTARG) {
610 errx(1, "'%s' requires argument",
612 p->c_u.c_func(argv[1], 0, s, afp);
614 } else if (p->c_parameter == OPTARG) {
615 p->c_u.c_func(argv[1], 0, s, afp);
618 } else if (p->c_parameter == NEXTARG2) {
620 errx(1, "'%s' requires 2 arguments",
622 p->c_u.c_func2(argv[1], argv[2], s, afp);
623 argc -= 2, argv += 2;
625 p->c_u.c_func(*argv, p->c_parameter, s, afp);
631 * Do any post argument processing required by the address family.
633 if (afp->af_postproc != NULL)
634 afp->af_postproc(s, afp);
636 * Do deferred callbacks registered while processing
637 * command-line arguments.
639 for (cb = callbacks; cb != NULL; cb = cb->cb_next)
640 cb->cb_func(s, cb->cb_arg);
642 * Do deferred operations.
645 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
646 warnx("interface %s cannot change %s addresses!",
653 strlcpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
654 ret = ioctl(s, afp->af_difaddr, afp->af_ridreq);
656 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
657 /* means no previous address for interface */
659 Perror("ioctl (SIOCDIFADDR)");
663 if (afp->af_addreq == NULL || afp->af_aifaddr == 0) {
664 warnx("interface %s cannot change %s addresses!",
669 if (newaddr && (setaddr || setmask)) {
670 strlcpy(afp->af_addreq, name, sizeof ifr.ifr_name);
671 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
672 Perror("ioctl (SIOCAIFADDR)");
681 setifaddr(const char *addr, int param, int s, const struct afswtch *afp)
683 if (afp->af_getaddr == NULL)
686 * Delay the ioctl to set the interface addr until flags are all set.
687 * The address interpretation may depend on the flags,
688 * and the flags may change when the address is set.
691 if (doalias == 0 && afp->af_af != AF_LINK)
693 afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR));
697 settunnel(const char *src, const char *dst, int s, const struct afswtch *afp)
699 struct addrinfo *srcres, *dstres;
702 if (afp->af_settunnel == NULL) {
703 warn("address family %s does not support tunnel setup",
708 if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0)
709 errx(1, "error in parsing address string: %s",
710 gai_strerror(ecode));
712 if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0)
713 errx(1, "error in parsing address string: %s",
714 gai_strerror(ecode));
716 if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family)
718 "source and destination address families do not match");
720 afp->af_settunnel(s, srcres, dstres);
722 freeaddrinfo(srcres);
723 freeaddrinfo(dstres);
728 deletetunnel(const char *vname, int param, int s, const struct afswtch *afp)
731 if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0)
732 err(1, "SIOCDIFPHYADDR");
736 setifnetmask(const char *addr, int dummy __unused, int s,
737 const struct afswtch *afp)
739 if (afp->af_getaddr != NULL) {
741 afp->af_getaddr(addr, MASK);
746 setifbroadaddr(const char *addr, int dummy __unused, int s,
747 const struct afswtch *afp)
749 if (afp->af_getaddr != NULL)
750 afp->af_getaddr(addr, DSTADDR);
754 notealias(const char *addr, int param, int s, const struct afswtch *afp)
756 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
757 if (setaddr && doalias == 0 && param < 0)
758 if (afp->af_addreq != NULL && afp->af_ridreq != NULL)
759 bcopy((caddr_t)rqtosa(af_addreq),
760 (caddr_t)rqtosa(af_ridreq),
761 rqtosa(af_addreq)->sa_len);
774 setifdstaddr(const char *addr, int param __unused, int s,
775 const struct afswtch *afp)
777 if (afp->af_getaddr != NULL)
778 afp->af_getaddr(addr, DSTADDR);
782 setifflags(const char *vname, int value, int s, const struct afswtch *afp)
787 memset(&my_ifr, 0, sizeof(struct ifreq));
788 strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
790 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0)
791 Perror("ioctl (SIOCGIFFLAGS)");
793 flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16);
800 my_ifr.ifr_flags = flags & 0xffff;
801 my_ifr.ifr_flagshigh = flags >> 16;
802 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
807 setifcap(const char *vname, int value, int s, const struct afswtch *afp)
811 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0)
812 Perror("ioctl (SIOCGIFCAP)");
814 flags = ifr.ifr_curcap;
821 ifr.ifr_reqcap = flags;
822 if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0)
827 setifmetric(const char *val, int dummy __unused, int s,
828 const struct afswtch *afp)
830 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
831 ifr.ifr_metric = atoi(val);
832 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
833 err(1, "ioctl SIOCSIFMETRIC (set metric)");
837 setifmtu(const char *val, int dummy __unused, int s,
838 const struct afswtch *afp)
840 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
841 ifr.ifr_mtu = atoi(val);
842 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
843 err(1, "ioctl SIOCSIFMTU (set mtu)");
847 setiftsolen(const char *val, int dummy __unused, int s,
848 const struct afswtch *afp)
850 strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
851 ifr.ifr_tsolen = atoi(val);
852 if (ioctl(s, SIOCSIFTSOLEN, (caddr_t)&ifr) < 0)
853 err(1, "ioctl SIOCSIFTSOLEN (set tsolen)");
857 setifname(const char *val, int dummy __unused, int s,
858 const struct afswtch *afp)
862 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
864 newname = strdup(val);
866 err(1, "no memory to set ifname");
867 ifr.ifr_data = newname;
868 if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) {
870 err(1, "ioctl SIOCSIFNAME (set name)");
873 strlcpy(name, newname, sizeof(name));
878 setifpollcpu(const char *val, int dummy __unused, int s,
879 const struct afswtch *afp)
881 warnx("pollcpu is deprecated, use polling or npolling instead");
882 setifflags("npolling", IFF_NPOLLING, s, afp);
887 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
888 "\10NOARP\11PROMISC\12ALLMULTI\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
889 "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25NPOLLING\26IDIRECT"
892 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7RSS" \
893 "\10VLAN_HWCSUM\11TSO"
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, const struct sockaddr_dl *sdl,
909 ifr.ifr_addr.sa_family = AF_LOCAL;
912 ifr.ifr_addr.sa_family =
913 afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af;
915 strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
917 s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0);
919 err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family);
921 printf("%s: ", name);
922 printb("flags", ifa->ifa_flags, IFFBITS);
923 if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1)
924 printf(" metric %d", ifr.ifr_metric);
925 if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
926 printf(" mtu %d", ifr.ifr_mtu);
929 if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
930 if (ifr.ifr_curcap != 0) {
931 printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
934 if (supmedia && ifr.ifr_reqcap != 0) {
935 printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS);
937 if (ifr.ifr_reqcap & IFCAP_TSO) {
938 if (ioctl(s, SIOCGIFTSOLEN,
939 (caddr_t)&ifr) == 0) {
940 printf("\ttsolen %d", ifr.ifr_tsolen);
949 for (ift = ifa; ift != NULL; ift = ift->ifa_next) {
950 if (ift->ifa_addr == NULL)
952 if (strcmp(ifa->ifa_name, ift->ifa_name) != 0)
955 const struct afswtch *p;
956 p = af_getbyfamily(ift->ifa_addr->sa_family);
957 if (p != NULL && p->af_status != NULL)
958 p->af_status(s, ift);
959 } else if (afp->af_af == ift->ifa_addr->sa_family)
960 afp->af_status(s, ift);
963 if (allfamilies || afp->af_af == AF_LINK) {
964 const struct afswtch *lafp;
967 * Hack; the link level address is received separately
968 * from the routing information so any address is not
969 * handled above. Cobble together an entry and invoke
970 * the status method specially.
972 lafp = af_getbyname("lladdr");
974 info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl;
975 lafp->af_status(s, &info);
981 else if (afp->af_other_status != NULL)
982 afp->af_other_status(s);
984 strlcpy(ifs.ifs_name, name, sizeof ifs.ifs_name);
985 if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0)
986 printf("%s", ifs.ascii);
995 af_all_tunnel_status(s);
999 Perror(const char *cmd)
1004 errx(1, "%s: no such interface", cmd);
1008 errx(1, "%s: permission denied", cmd);
1017 * Print a value a la the %pb%i format of the kernel's kprintf()
1020 printb(const char *s, unsigned v, const char *bits)
1025 if (bits && *bits == 8)
1026 printf("%s=%o", s, v);
1028 printf("%s=%x", s, v);
1032 while ((i = *bits++) != '\0') {
1033 if (v & (1 << (i-1))) {
1037 for (; (c = *bits) > 32; bits++)
1040 for (; *bits > 32; bits++)
1048 ifmaybeload(const char *name)
1050 #define MOD_PREFIX_LEN 3 /* "if_" */
1051 struct module_stat mstat;
1053 char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp;
1056 /* loading suppressed by the user */
1060 /* trim the interface number off the end */
1061 strlcpy(ifname, name, sizeof(ifname));
1062 for (dp = ifname; *dp != 0; dp++)
1068 /* turn interface and unit into module name */
1069 strlcpy(ifkind, "if_", sizeof(ifkind));
1070 strlcat(ifkind, ifname, sizeof(ifkind));
1072 /* scan files in kernel */
1073 mstat.version = sizeof(struct module_stat);
1074 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1075 /* scan modules in file */
1076 for (modid = kldfirstmod(fileid); modid > 0;
1077 modid = modfnext(modid)) {
1078 if (modstat(modid, &mstat) < 0)
1080 /* strip bus name if present */
1081 if ((cp = strchr(mstat.name, '/')) != NULL) {
1086 /* already loaded? */
1087 if (strcmp(ifname, cp) == 0 ||
1088 strcmp(ifkind, cp) == 0)
1093 /* not present, we should try to load it */
1097 static struct cmd basic_cmds[] = {
1098 DEF_CMD("up", IFF_UP, setifflags),
1099 DEF_CMD("down", -IFF_UP, setifflags),
1100 DEF_CMD("arp", -IFF_NOARP, setifflags),
1101 DEF_CMD("-arp", IFF_NOARP, setifflags),
1102 DEF_CMD("debug", IFF_DEBUG, setifflags),
1103 DEF_CMD("-debug", -IFF_DEBUG, setifflags),
1104 DEF_CMD("promisc", IFF_PPROMISC, setifflags),
1105 DEF_CMD("-promisc", -IFF_PPROMISC, setifflags),
1106 DEF_CMD("add", IFF_UP, notealias),
1107 DEF_CMD("alias", IFF_UP, notealias),
1108 DEF_CMD("-alias", -IFF_UP, notealias),
1109 DEF_CMD("delete", -IFF_UP, notealias),
1110 DEF_CMD("remove", -IFF_UP, notealias),
1112 #define EN_SWABIPS 0x1000
1113 DEF_CMD("swabips", EN_SWABIPS, setifflags),
1114 DEF_CMD("-swabips", -EN_SWABIPS, setifflags),
1116 DEF_CMD_ARG("netmask", setifnetmask),
1117 DEF_CMD_ARG("metric", setifmetric),
1118 DEF_CMD_ARG("broadcast", setifbroadaddr),
1119 DEF_CMD_ARG2("tunnel", settunnel),
1120 DEF_CMD("-tunnel", 0, deletetunnel),
1121 DEF_CMD("deletetunnel", 0, deletetunnel),
1122 DEF_CMD("link0", IFF_LINK0, setifflags),
1123 DEF_CMD("-link0", -IFF_LINK0, setifflags),
1124 DEF_CMD("link1", IFF_LINK1, setifflags),
1125 DEF_CMD("-link1", -IFF_LINK1, setifflags),
1126 DEF_CMD("link2", IFF_LINK2, setifflags),
1127 DEF_CMD("-link2", -IFF_LINK2, setifflags),
1128 DEF_CMD("monitor", IFF_MONITOR, setifflags),
1129 DEF_CMD("-monitor", -IFF_MONITOR, setifflags),
1130 DEF_CMD("staticarp", IFF_STATICARP, setifflags),
1131 DEF_CMD("-staticarp", -IFF_STATICARP, setifflags),
1132 DEF_CMD("polling", IFF_NPOLLING, setifflags),
1133 DEF_CMD("-polling", -IFF_NPOLLING, setifflags),
1134 DEF_CMD("npolling", IFF_NPOLLING, setifflags),
1135 DEF_CMD("-npolling", -IFF_NPOLLING, setifflags),
1136 DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap),
1137 DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap),
1138 DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap),
1139 DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap),
1140 DEF_CMD("netcons", IFCAP_NETCONS, setifcap),
1141 DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap),
1142 DEF_CMD("rss", IFCAP_RSS, setifcap),
1143 DEF_CMD("-rss", -IFCAP_RSS, setifcap),
1144 DEF_CMD("tso", IFCAP_TSO, setifcap),
1145 DEF_CMD("-tso", -IFCAP_TSO, setifcap),
1146 DEF_CMD("normal", -IFF_LINK0, setifflags),
1147 DEF_CMD("compress", IFF_LINK0, setifflags),
1148 DEF_CMD("noicmp", IFF_LINK1, setifflags),
1149 DEF_CMD_ARG("mtu", setifmtu),
1150 DEF_CMD_ARG("name", setifname),
1151 DEF_CMD_ARG("pollcpu", setifpollcpu),
1152 DEF_CMD_ARG("tsolen", setiftsolen)
1155 static __constructor(101) void
1160 for (i = 0; i < nitems(basic_cmds); i++)
1161 cmd_register(&basic_cmds[i]);