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
, 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
;
158 all
= downonly
= uponly
= namesonly
= verbose
= 0;
160 /* Parse leading line options */
161 strlcpy(options
, "adklmuv", sizeof(options
));
162 for (p
= opts
; p
!= NULL
; p
= p
->next
)
163 strlcat(options
, p
->opt
, sizeof(options
));
164 while ((c
= getopt(argc
, argv
, options
)) != -1) {
166 case 'a': /* scan all interfaces */
169 case 'd': /* restrict scan to "down" interfaces */
175 case 'l': /* scan interface names only */
178 case 'm': /* show media choices in status */
181 case 'u': /* restrict scan to "up" interfaces */
188 for (p
= opts
; p
!= NULL
; p
= p
->next
)
189 if (p
->opt
[0] == c
) {
201 /* -l cannot be used with -a or -m */
202 if (namesonly
&& (all
|| supmedia
))
206 if (uponly
&& downonly
)
209 /* no arguments is equivalent to '-a' */
210 if (!namesonly
&& argc
< 1)
213 /* -a and -l allow an address family arg to limit the output */
214 if (all
|| namesonly
) {
220 afp
= af_getbyname(*argv
);
223 if (afp
->af_name
!= NULL
)
225 /* leave with afp non-zero */
228 /* not listing, need an argument */
232 strncpy(name
, *argv
, sizeof(name
));
235 /* check and maybe load support for this interface */
239 * NOTE: We must special-case the `create' command right
240 * here as we would otherwise fail when trying to find
243 if (argc
> 0 && (strcmp(argv
[0], "create") == 0 ||
244 strcmp(argv
[0], "plumb") == 0)) {
250 ifindex
= if_nametoindex(name
);
252 errx(1, "interface %s does not exist", name
);
255 /* Check for address family */
257 afp
= af_getbyname(*argv
);
266 mib
[3] = 0; /* address family */
267 mib
[4] = NET_RT_IFLIST
;
268 mib
[5] = ifindex
; /* interface index */
270 /* if particular family specified, only ask about it */
274 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
275 errx(1, "iflist-sysctl-estimate");
276 if ((buf
= malloc(needed
)) == NULL
)
278 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0) {
279 if (errno
== ENOMEM
&& count
++ < 10) {
280 warnx("Routing table grew, retrying");
285 errx(1, "actual retrieval of interface table");
293 ifm
= (struct if_msghdr
*)next
;
295 if (ifm
->ifm_type
== RTM_IFINFO
) {
297 if (ifm
->ifm_data
.ifi_datalen
== 0)
298 ifm
->ifm_data
.ifi_datalen
= sizeof(struct if_data
);
299 sdl
= (struct sockaddr_dl
*)((char *)ifm
+ sizeof(struct if_msghdr
) -
300 sizeof(struct if_data
) + ifm
->ifm_data
.ifi_datalen
);
302 sdl
= (struct sockaddr_dl
*)(ifm
+ 1);
304 flags
= ifm
->ifm_flags
;
306 fprintf(stderr
, "out of sync parsing NET_RT_IFLIST\n");
307 fprintf(stderr
, "expected %d, got %d\n", RTM_IFINFO
,
309 fprintf(stderr
, "msglen = %d\n", ifm
->ifm_msglen
);
310 fprintf(stderr
, "buf:%p, next:%p, lim:%p\n", buf
, next
,
315 next
+= ifm
->ifm_msglen
;
320 nextifm
= (struct if_msghdr
*)next
;
322 if (nextifm
->ifm_type
!= RTM_NEWADDR
)
326 ifam
= (struct ifa_msghdr
*)nextifm
;
329 next
+= nextifm
->ifm_msglen
;
332 if (sizeof(name
) <= sdl
->sdl_nlen
)
333 name_len
= sizeof(name
) - 1;
335 name_len
= sdl
->sdl_nlen
;
337 memcpy(name
, sdl
->sdl_data
, name_len
);
338 name
[name_len
] = '\0';
340 if (all
|| namesonly
) {
342 if ((flags
& IFF_UP
) == 0)
343 continue; /* not up */
346 continue; /* not down */
348 if (afp
== NULL
|| afp
->af_af
!= AF_LINK
||
349 sdl
->sdl_type
== IFT_ETHER
) {
360 ifconfig(argc
, argv
, afp
);
362 status(afp
, addrcount
, sdl
, ifm
, ifam
);
366 if (namesonly
&& need_nl
> 0)
370 printf("%s\n", name
);
375 static struct afswtch
*afs
= NULL
;
378 af_register(struct afswtch
*p
)
384 static struct afswtch
*
385 af_getbyname(const char *name
)
389 for (afp
= afs
; afp
!= NULL
; afp
= afp
->af_next
)
390 if (strcmp(afp
->af_name
, name
) == 0)
395 static struct afswtch
*
396 af_getbyfamily(int af
)
400 for (afp
= afs
; afp
!= NULL
; afp
= afp
->af_next
)
401 if (afp
->af_af
== af
)
407 af_other_status(int s
)
410 uint8_t afmask
[howmany(AF_MAX
, NBBY
)];
412 memset(afmask
, 0, sizeof(afmask
));
413 for (afp
= afs
; afp
!= NULL
; afp
= afp
->af_next
) {
414 if (afp
->af_other_status
== NULL
)
416 if (afp
->af_af
!= AF_UNSPEC
&& isset(afmask
, afp
->af_af
))
418 afp
->af_other_status(s
);
419 setbit(afmask
, afp
->af_af
);
424 af_all_tunnel_status(int s
)
427 uint8_t afmask
[howmany(AF_MAX
, NBBY
)];
429 memset(afmask
, 0, sizeof(afmask
));
430 for (afp
= afs
; afp
!= NULL
; afp
= afp
->af_next
) {
431 if (afp
->af_status_tunnel
== NULL
)
433 if (afp
->af_af
!= AF_UNSPEC
&& isset(afmask
, afp
->af_af
))
435 afp
->af_status_tunnel(s
);
436 setbit(afmask
, afp
->af_af
);
440 static struct cmd
*cmds
= NULL
;
443 cmd_register(struct cmd
*p
)
449 static const struct cmd
*
450 cmd_lookup(const char *name
)
452 #define N(a) (sizeof(a)/sizeof(a[0]))
455 for (p
= cmds
; p
!= NULL
; p
= p
->c_next
)
456 if (strcmp(name
, p
->c_name
) == 0)
463 callback_func
*cb_func
;
465 struct callback
*cb_next
;
467 static struct callback
*callbacks
= NULL
;
470 callback_register(callback_func
*func
, void *arg
)
474 cb
= malloc(sizeof(struct callback
));
476 errx(1, "unable to allocate memory for callback");
479 cb
->cb_next
= callbacks
;
483 /* specially-handled commands */
484 static void setifaddr(const char *, int, int, const struct afswtch
*);
485 static const struct cmd setifaddr_cmd
= DEF_CMD("ifaddr", 0, setifaddr
);
487 static void setifdstaddr(const char *, int, int, const struct afswtch
*);
488 static const struct cmd setifdstaddr_cmd
=
489 DEF_CMD("ifdstaddr", 0, setifdstaddr
);
492 ifconfig(int argc
, char *const *argv
, const struct afswtch
*afp
)
498 afp
= af_getbyname("inet");
499 ifr
.ifr_addr
.sa_family
=
500 afp
->af_af
== AF_LINK
|| afp
->af_af
== AF_UNSPEC
?
501 AF_INET
: afp
->af_af
;
502 strncpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
504 if ((s
= socket(ifr
.ifr_addr
.sa_family
, SOCK_DGRAM
, 0)) < 0)
505 err(1, "socket(family %u,SOCK_DGRAM", ifr
.ifr_addr
.sa_family
);
510 p
= cmd_lookup(*argv
);
513 * Not a recognized command, choose between setting
514 * the interface address and the dst address.
516 p
= (setaddr
? &setifdstaddr_cmd
: &setifaddr_cmd
);
518 if (p
->c_u
.c_func
|| p
->c_u
.c_func2
) {
519 if (p
->c_parameter
== NEXTARG
) {
521 errx(1, "'%s' requires argument",
523 p
->c_u
.c_func(argv
[1], 0, s
, afp
);
525 } else if (p
->c_parameter
== OPTARG
) {
526 p
->c_u
.c_func(argv
[1], 0, s
, afp
);
529 } else if (p
->c_parameter
== NEXTARG2
) {
531 errx(1, "'%s' requires 2 arguments",
533 p
->c_u
.c_func2(argv
[1], argv
[2], s
, afp
);
534 argc
-= 2, argv
+= 2;
536 p
->c_u
.c_func(*argv
, p
->c_parameter
, s
, afp
);
542 * Do any post argument processing required by the address family.
544 if (afp
->af_postproc
!= NULL
)
545 afp
->af_postproc(s
, afp
);
547 * Do deferred callbacks registered while processing
548 * command-line arguments.
550 for (cb
= callbacks
; cb
!= NULL
; cb
= cb
->cb_next
)
551 cb
->cb_func(s
, cb
->cb_arg
);
553 * Do deferred operations.
556 if (afp
->af_ridreq
== NULL
|| afp
->af_difaddr
== 0) {
557 warnx("interface %s cannot change %s addresses!",
564 strncpy(afp
->af_ridreq
, name
, sizeof ifr
.ifr_name
);
565 ret
= ioctl(s
, afp
->af_difaddr
, afp
->af_ridreq
);
567 if (errno
== EADDRNOTAVAIL
&& (doalias
>= 0)) {
568 /* means no previous address for interface */
570 Perror("ioctl (SIOCDIFADDR)");
574 if (afp
->af_addreq
== NULL
|| afp
->af_aifaddr
== 0) {
575 warnx("interface %s cannot change %s addresses!",
580 if (newaddr
&& (setaddr
|| setmask
)) {
581 strncpy(afp
->af_addreq
, name
, sizeof ifr
.ifr_name
);
582 if (ioctl(s
, afp
->af_aifaddr
, afp
->af_addreq
) < 0)
583 Perror("ioctl (SIOCAIFADDR)");
592 setifaddr(const char *addr
, int param
, int s
, const struct afswtch
*afp
)
594 if (afp
->af_getaddr
== NULL
)
597 * Delay the ioctl to set the interface addr until flags are all set.
598 * The address interpretation may depend on the flags,
599 * and the flags may change when the address is set.
602 if (doalias
== 0 && afp
->af_af
!= AF_LINK
)
604 afp
->af_getaddr(addr
, (doalias
>= 0 ? ADDR
: RIDADDR
));
608 settunnel(const char *src
, const char *dst
, int s
, const struct afswtch
*afp
)
610 struct addrinfo
*srcres
, *dstres
;
613 if (afp
->af_settunnel
== NULL
) {
614 warn("address family %s does not support tunnel setup",
619 if ((ecode
= getaddrinfo(src
, NULL
, NULL
, &srcres
)) != 0)
620 errx(1, "error in parsing address string: %s",
621 gai_strerror(ecode
));
623 if ((ecode
= getaddrinfo(dst
, NULL
, NULL
, &dstres
)) != 0)
624 errx(1, "error in parsing address string: %s",
625 gai_strerror(ecode
));
627 if (srcres
->ai_addr
->sa_family
!= dstres
->ai_addr
->sa_family
)
629 "source and destination address families do not match");
631 afp
->af_settunnel(s
, srcres
, dstres
);
633 freeaddrinfo(srcres
);
634 freeaddrinfo(dstres
);
639 deletetunnel(const char *vname
, int param
, int s
, const struct afswtch
*afp
)
642 if (ioctl(s
, SIOCDIFPHYADDR
, &ifr
) < 0)
643 err(1, "SIOCDIFPHYADDR");
647 setifnetmask(const char *addr
, int dummy __unused
, int s
,
648 const struct afswtch
*afp
)
650 if (afp
->af_getaddr
!= NULL
) {
652 afp
->af_getaddr(addr
, MASK
);
657 setifbroadaddr(const char *addr
, int dummy __unused
, int s
,
658 const struct afswtch
*afp
)
660 if (afp
->af_getaddr
!= NULL
)
661 afp
->af_getaddr(addr
, DSTADDR
);
665 setifipdst(const char *addr
, int dummy __unused
, int s
,
666 const struct afswtch
*afp
)
668 const struct afswtch
*inet
;
670 inet
= af_getbyname("inet");
673 inet
->af_getaddr(addr
, DSTADDR
);
680 notealias(const char *addr
, int param
, int s
, const struct afswtch
*afp
)
682 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
683 if (setaddr
&& doalias
== 0 && param
< 0)
684 if (afp
->af_addreq
!= NULL
&& afp
->af_ridreq
!= NULL
)
685 bcopy((caddr_t
)rqtosa(af_addreq
),
686 (caddr_t
)rqtosa(af_ridreq
),
687 rqtosa(af_addreq
)->sa_len
);
699 setifdstaddr(const char *addr
, int param __unused
, int s
,
700 const struct afswtch
*afp
)
702 if (afp
->af_getaddr
!= NULL
)
703 afp
->af_getaddr(addr
, DSTADDR
);
707 * Note: doing an SIOCIGIFFLAGS scribbles on the union portion
708 * of the ifreq structure, which may confuse other parts of ifconfig.
709 * Make a private copy so we can avoid that.
712 setifflags(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
716 bcopy((char *)&ifr
, (char *)&my_ifr
, sizeof(struct ifreq
));
718 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&my_ifr
) < 0) {
719 Perror("ioctl (SIOCGIFFLAGS)");
722 strncpy(my_ifr
.ifr_name
, name
, sizeof (my_ifr
.ifr_name
));
723 flags
= (my_ifr
.ifr_flags
& 0xffff) | (my_ifr
.ifr_flagshigh
<< 16);
730 my_ifr
.ifr_flags
= flags
& 0xffff;
731 my_ifr
.ifr_flagshigh
= flags
>> 16;
732 if (ioctl(s
, SIOCSIFFLAGS
, (caddr_t
)&my_ifr
) < 0)
737 setifcap(const char *vname
, int value
, int s
, const struct afswtch
*afp
)
740 if (ioctl(s
, SIOCGIFCAP
, (caddr_t
)&ifr
) < 0) {
741 Perror("ioctl (SIOCGIFCAP)");
744 flags
= ifr
.ifr_curcap
;
750 ifr
.ifr_reqcap
= flags
;
751 if (ioctl(s
, SIOCSIFCAP
, (caddr_t
)&ifr
) < 0)
756 setifmetric(const char *val
, int dummy __unused
, int s
,
757 const struct afswtch
*afp
)
759 strncpy(ifr
.ifr_name
, name
, sizeof (ifr
.ifr_name
));
760 ifr
.ifr_metric
= atoi(val
);
761 if (ioctl(s
, SIOCSIFMETRIC
, (caddr_t
)&ifr
) < 0)
762 warn("ioctl (set metric)");
766 setifmtu(const char *val
, int dummy __unused
, int s
,
767 const struct afswtch
*afp
)
769 strncpy(ifr
.ifr_name
, name
, sizeof (ifr
.ifr_name
));
770 ifr
.ifr_mtu
= atoi(val
);
771 if (ioctl(s
, SIOCSIFMTU
, (caddr_t
)&ifr
) < 0)
772 warn("ioctl (set mtu)");
776 setifname(const char *val
, int dummy __unused
, int s
,
777 const struct afswtch
*afp
)
781 newname
= strdup(val
);
782 if (newname
== NULL
) {
783 warn("no memory to set ifname");
786 ifr
.ifr_data
= newname
;
787 if (ioctl(s
, SIOCSIFNAME
, (caddr_t
)&ifr
) < 0) {
788 warn("ioctl (set name)");
792 strlcpy(name
, newname
, sizeof(name
));
796 * Even if we just created the interface, we don't need to print
797 * its name because we just nailed it down separately.
803 setifpollcpu(const char *val
, int dummy __unused
, int s
,
804 const struct afswtch
*afp
)
814 setifflags("-polling", -IFF_POLLING
, s
, afp
);
816 ifr
.ifr_pollcpu
= pollcpu
;
817 if (ioctl(s
, SIOCSIFPOLLCPU
, (caddr_t
)&ifr
) < 0) {
818 warn("ioctl (set pollcpu)");
824 * Expand the compacted form of addresses as returned via the
825 * configuration read via sysctl().
828 rt_xaddrs(caddr_t cp
, caddr_t cplim
, struct rt_addrinfo
*rtinfo
)
833 memset(rtinfo
->rti_info
, 0, sizeof(rtinfo
->rti_info
));
834 for (i
= 0; (i
< RTAX_MAX
) && (cp
< cplim
); i
++) {
835 if ((rtinfo
->rti_addrs
& (1 << i
)) == 0)
837 rtinfo
->rti_info
[i
] = sa
= (struct sockaddr
*)cp
;
843 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
844 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
845 "\20MULTICAST\21POLLING\22PPROMISC\23MONITOR\24STATICARP\25NEEDSGIANT"
848 "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING"
851 * Print the status of the interface. If an address family was
852 * specified, show only it; otherwise, show them all.
855 status(const struct afswtch
*afp
, int addrcount
, struct sockaddr_dl
*sdl
,
856 struct if_msghdr
*ifm
, struct ifa_msghdr
*ifam
)
858 struct rt_addrinfo info
;
864 afp
= af_getbyname("inet");
868 ifr
.ifr_addr
.sa_family
= afp
->af_af
== AF_LINK
? AF_INET
: afp
->af_af
;
869 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
871 s
= socket(ifr
.ifr_addr
.sa_family
, SOCK_DGRAM
, 0);
873 err(1, "socket(family %u,SOCK_DGRAM)", ifr
.ifr_addr
.sa_family
);
875 printf("%s: ", name
);
876 printb("flags", flags
, IFFBITS
);
877 if (ifm
->ifm_data
.ifi_metric
)
878 printf(" metric %ld", ifm
->ifm_data
.ifi_metric
);
879 if (ifm
->ifm_data
.ifi_mtu
)
880 printf(" mtu %ld", ifm
->ifm_data
.ifi_mtu
);
883 if (ioctl(s
, SIOCGIFCAP
, (caddr_t
)&ifr
) == 0) {
884 if (ifr
.ifr_curcap
!= 0) {
885 printb("\toptions", ifr
.ifr_curcap
, IFCAPBITS
);
888 if (supmedia
&& ifr
.ifr_reqcap
!= 0) {
889 printb("\tcapabilities", ifr
.ifr_reqcap
, IFCAPBITS
);
896 while (addrcount
> 0) {
897 info
.rti_addrs
= ifam
->ifam_addrs
;
898 /* Expand the compacted addresses */
899 rt_xaddrs((char *)(ifam
+ 1), ifam
->ifam_msglen
+ (char *)ifam
,
903 const struct afswtch
*p
;
904 p
= af_getbyfamily(info
.rti_info
[RTAX_IFA
]->sa_family
);
905 if (p
!= NULL
&& p
->af_status
!= NULL
)
906 p
->af_status(s
, &info
);
907 } else if (afp
->af_af
== info
.rti_info
[RTAX_IFA
]->sa_family
)
908 afp
->af_status(s
, &info
);
910 ifam
= (struct ifa_msghdr
*)((char *)ifam
+ ifam
->ifam_msglen
);
912 if (allfamilies
|| afp
->af_af
== AF_LINK
) {
913 const struct afswtch
*lafp
;
916 * Hack; the link level address is received separately
917 * from the routing information so any address is not
918 * handled above. Cobble together an entry and invoke
919 * the status method specially.
921 lafp
= af_getbyname("lladdr");
923 info
.rti_info
[RTAX_IFA
] = (struct sockaddr
*)sdl
;
924 lafp
->af_status(s
, &info
);
929 else if (afp
->af_other_status
!= NULL
)
930 afp
->af_other_status(s
);
932 strncpy(ifs
.ifs_name
, name
, sizeof ifs
.ifs_name
);
933 if (ioctl(s
, SIOCGIFSTATUS
, &ifs
) == 0)
934 printf("%s", ifs
.ascii
);
936 if (flags
& IFF_POLLING
) {
937 if (ioctl(s
, SIOCGIFPOLLCPU
, &ifr
) == 0 && ifr
.ifr_pollcpu
>= 0)
938 printf("\tpollcpu: %d\n", ifr
.ifr_pollcpu
);
948 af_all_tunnel_status(s
);
952 Perror(const char *cmd
)
957 errx(1, "%s: no such interface", cmd
);
961 errx(1, "%s: permission denied", cmd
);
970 * Print a value a la the %b format of the kernel's printf
973 printb(const char *s
, unsigned v
, const char *bits
)
978 if (bits
&& *bits
== 8)
979 printf("%s=%o", s
, v
);
981 printf("%s=%x", s
, v
);
985 while ((i
= *bits
++) != '\0') {
986 if (v
& (1 << (i
-1))) {
990 for (; (c
= *bits
) > 32; bits
++)
993 for (; *bits
> 32; bits
++)
1001 ifmaybeload(char *name
)
1003 struct module_stat mstat
;
1005 char ifkind
[35], *cp
, *dp
;
1007 /* turn interface and unit into module name */
1008 strcpy(ifkind
, "if_");
1009 for (cp
= name
, dp
= ifkind
+ 3;
1010 (*cp
!= 0) && !isdigit(*cp
); cp
++, dp
++)
1014 /* scan files in kernel */
1015 mstat
.version
= sizeof(struct module_stat
);
1016 for (fileid
= kldnext(0); fileid
> 0; fileid
= kldnext(fileid
)) {
1017 /* scan modules in file */
1018 for (modid
= kldfirstmod(fileid
); modid
> 0;
1019 modid
= modfnext(modid
)) {
1020 if (modstat(modid
, &mstat
) < 0)
1022 /* strip bus name if present */
1023 if ((cp
= strchr(mstat
.name
, '/')) != NULL
) {
1028 /* already loaded? */
1029 if (strncmp(name
, cp
, strlen(cp
)) == 0 ||
1030 strncmp(ifkind
, cp
, strlen(cp
)) == 0)
1035 /* not present, we should try to load it */
1039 static struct cmd basic_cmds
[] = {
1040 DEF_CMD("up", IFF_UP
, setifflags
),
1041 DEF_CMD("down", -IFF_UP
, setifflags
),
1042 DEF_CMD("arp", -IFF_NOARP
, setifflags
),
1043 DEF_CMD("-arp", IFF_NOARP
, setifflags
),
1044 DEF_CMD("debug", IFF_DEBUG
, setifflags
),
1045 DEF_CMD("-debug", -IFF_DEBUG
, setifflags
),
1046 DEF_CMD("promisc", IFF_PPROMISC
, setifflags
),
1047 DEF_CMD("-promisc", -IFF_PPROMISC
, setifflags
),
1048 DEF_CMD("add", IFF_UP
, notealias
),
1049 DEF_CMD("alias", IFF_UP
, notealias
),
1050 DEF_CMD("-alias", -IFF_UP
, notealias
),
1051 DEF_CMD("delete", -IFF_UP
, notealias
),
1052 DEF_CMD("remove", -IFF_UP
, notealias
),
1054 #define EN_SWABIPS 0x1000
1055 DEF_CMD("swabips", EN_SWABIPS
, setifflags
),
1056 DEF_CMD("-swabips", -EN_SWABIPS
, setifflags
),
1058 DEF_CMD_ARG("netmask", setifnetmask
),
1059 DEF_CMD_ARG("metric", setifmetric
),
1060 DEF_CMD_ARG("broadcast", setifbroadaddr
),
1062 DEF_CMD_ARG("ipdst", setifipdst
),
1064 DEF_CMD_ARG2("tunnel", settunnel
),
1065 DEF_CMD("-tunnel", 0, deletetunnel
),
1066 DEF_CMD("deletetunnel", 0, deletetunnel
),
1067 DEF_CMD("link0", IFF_LINK0
, setifflags
),
1068 DEF_CMD("-link0", -IFF_LINK0
, setifflags
),
1069 DEF_CMD("link1", IFF_LINK1
, setifflags
),
1070 DEF_CMD("-link1", -IFF_LINK1
, setifflags
),
1071 DEF_CMD("link2", IFF_LINK2
, setifflags
),
1072 DEF_CMD("-link2", -IFF_LINK2
, setifflags
),
1073 DEF_CMD("monitor", IFF_MONITOR
, setifflags
),
1074 DEF_CMD("-monitor", -IFF_MONITOR
, setifflags
),
1076 DEF_CMD("staticarp", IFF_STATICARP
, setifflags
),
1077 DEF_CMD("-staticarp", -IFF_STATICARP
, setifflags
),
1079 DEF_CMD("polling", IFF_POLLING
, setifflags
),
1080 DEF_CMD("-polling", -IFF_POLLING
, setifflags
),
1081 DEF_CMD("rxcsum", IFCAP_RXCSUM
, setifcap
),
1082 DEF_CMD("-rxcsum", -IFCAP_RXCSUM
, setifcap
),
1083 DEF_CMD("txcsum", IFCAP_TXCSUM
, setifcap
),
1084 DEF_CMD("-txcsum", -IFCAP_TXCSUM
, setifcap
),
1085 DEF_CMD("netcons", IFCAP_NETCONS
, setifcap
),
1086 DEF_CMD("-netcons", -IFCAP_NETCONS
, setifcap
),
1087 DEF_CMD("normal", -IFF_LINK0
, setifflags
),
1088 DEF_CMD("compress", IFF_LINK0
, setifflags
),
1089 DEF_CMD("noicmp", IFF_LINK1
, setifflags
),
1090 DEF_CMD_ARG("mtu", setifmtu
),
1091 DEF_CMD_ARG("name", setifname
),
1092 DEF_CMD_ARG("pollcpu", setifpollcpu
)
1095 static __constructor
void
1098 #define N(a) (sizeof(a) / sizeof(a[0]))
1101 for (i
= 0; i
< N(basic_cmds
); i
++)
1102 cmd_register(&basic_cmds
[i
]);