1 /* $FreeBSD: src/usr.sbin/gifconfig/gifconfig.c,v 1.2.2.4 2002/08/30 14:23:39 sobomax Exp $ */
2 /* $KAME: gifconfig.c,v 1.14 2001/01/01 04:04:56 jinmei Exp $ */
5 * Copyright (c) 1983, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * gifconfig, derived from ifconfig
40 * @(#) Copyright (c) 1983, 1993\n\
41 * The Regents of the University of California. All rights reserved.\n
43 * @(#)ifconfig.c 8.2 (Berkeley) 2/16/94
47 * 951109 - Andrew@pubnix.net - Changed to iterative buffer growing mechanism
48 * for ifconfig -a so all interfaces are queried.
50 * 960101 - peter@freebsd.org - Blow away the SIOCGIFCONF code and use
51 * sysctl() to get the structured interface conf
52 * and parse the messages in there. REALLY UGLY!
55 #include <sys/param.h>
56 #include <sys/socket.h>
57 #include <sys/ioctl.h>
58 #include <sys/sysctl.h>
61 #if defined(__DragonFly__)
62 #include <net/if_var.h>
63 #endif /* __DragonFly__ */
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #include <arpa/inet.h>
72 #include <sys/protosw.h>
86 struct ifaliasreq addreq
;
88 struct in6_ifreq in6_ifr
;
89 struct in6_aliasreq in6_addreq
;
102 char ntop_buf
[INET6_ADDRSTRLEN
]; /*inet_ntop()*/
105 void setifpsrc(char *, int);
106 void setifpdst(char *, int);
107 void setifflags(char *, int);
108 #ifdef SIOCDIFPHYADDR
109 void delifaddrs(char *, int);
112 #define NEXTARG 0xffffff
116 int c_parameter
; /* NEXTARG means next argv */
117 void (*c_func
)(char *, int);
119 { "up", IFF_UP
, setifflags
} ,
120 { "down", -IFF_UP
, setifflags
},
121 #ifdef SIOCDIFPHYADDR
122 { "delete", 0, delifaddrs
},
129 * XNS support liberally adapted from code written at the University of
130 * Maryland principally by James O'Toole and Chris Torek.
132 int main(int, char *[]);
134 void phys_status(int);
137 void in6_status(int);
139 void ether_status(int);
141 void in_getaddr(char *, int);
143 void in6_getaddr(char *, int);
144 void in6_getprefix(char *, int);
146 void printb(char *, unsigned int, char *);
147 int prefix(void *, int);
149 char ntop_buf
[INET6_ADDRSTRLEN
];
151 /* Known address families */
155 void (*af_status
)(int);
156 void (*af_getaddr
)(char *, int);
157 void (*af_getprefix
)(char *, int);
162 #define C(x) ((caddr_t) &x)
163 { "inet", AF_INET
, in_status
, in_getaddr
, 0,
164 SIOCSIFPHYADDR
, C(addreq
), C(ifr
) },
166 { "inet6", AF_INET6
, in6_status
, in6_getaddr
, in6_getprefix
,
167 SIOCSIFPHYADDR_IN6
, C(in6_addreq
), C(in6_ifr
) },
169 { "ether", AF_INET
, ether_status
, NULL
, NULL
}, /* XXX not real!! */
173 struct afswtch
*afp
= NULL
; /*the address family being set or asked about*/
175 void rt_xaddrs(caddr_t
, caddr_t
, struct rt_addrinfo
*);
176 int ifconfig(int argc
, char *argv
[], int af
, struct afswtch
*rafp
);
181 * Expand the compacted form of addresses as returned via the
182 * configuration read via sysctl().
186 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
187 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
190 rt_xaddrs(caddr_t cp
, caddr_t cplim
, struct rt_addrinfo
*rtinfo
)
195 memset(rtinfo
->rti_info
, 0, sizeof(rtinfo
->rti_info
));
196 for (i
= 0; (i
< RTAX_MAX
) && (cp
< cplim
); i
++) {
197 if ((rtinfo
->rti_addrs
& (1 << i
)) == 0)
199 rtinfo
->rti_info
[i
] = sa
= (struct sockaddr
*)cp
;
206 * Grunge for new-style sysctl() decoding.. :-(
207 * Apologies to the world for committing gross things like this in 1996..
209 struct if_msghdr
*ifm
;
210 struct ifa_msghdr
*ifam
;
211 struct sockaddr_dl
*sdl
;
212 struct rt_addrinfo info
;
213 char *buf
, *lim
, *next
;
217 main(int argc
, char **argv
)
220 struct afswtch
*rafp
= NULL
;
227 "usage: gifconfig interface [af] [physsrc physdst]\n");
228 #ifdef SIOCDIFPHYADDR
230 " gifconfig interface delete\n");
237 strncpy(name
, *argv
, sizeof(name
));
238 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
241 for (afp
= rafp
= afs
; rafp
->af_name
; rafp
++)
242 if (strcmp(rafp
->af_name
, *argv
) == 0) {
243 afp
= rafp
; argc
--; argv
++;
247 af
= ifr
.ifr_addr
.sa_family
= rafp
->af_af
;
253 mib
[3] = 0; /* address family */
254 mib
[4] = NET_RT_IFLIST
;
257 /* if particular family specified, only ask about it */
262 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
263 errx(1, "iflist-sysctl-estimate");
264 if ((buf
= malloc(needed
)) == NULL
)
266 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
267 errx(1, "actual retrieval of interface table");
271 if (strcmp(name
, "-a") == 0)
272 all
= 1; /* All interfaces */
273 else if (strcmp(name
, "-au") == 0)
274 all
= 2; /* All IFF_UPinterfaces */
275 else if (strcmp(name
, "-ad") == 0)
276 all
= 3; /* All !IFF_UP interfaces */
278 for (next
= buf
; next
< lim
; next
+= ifm
->ifm_msglen
) {
280 ifm
= (struct if_msghdr
*)next
;
282 /* XXX: Swallow up leftover NEWADDR messages */
283 if (ifm
->ifm_type
== RTM_NEWADDR
)
286 if (ifm
->ifm_type
== RTM_IFINFO
) {
287 sdl
= (struct sockaddr_dl
*)(ifm
+ 1);
288 flags
= ifm
->ifm_flags
;
290 errx(1, "out of sync parsing NET_RT_IFLIST");
296 if (strlen(name
) != sdl
->sdl_nlen
)
297 continue; /* not same len */
298 if (strncmp(name
, sdl
->sdl_data
, sdl
->sdl_nlen
) != 0)
299 continue; /* not same name */
302 break; /* always do it */
304 if ((flags
& IFF_UP
) == 0)
305 continue; /* not up */
309 continue; /* not down */
314 * Let's just do it for gif only
316 if (sdl
->sdl_type
!= IFT_GIF
) {
320 fprintf(stderr
, "gifconfig: %s is not gif.\n",
326 strncpy(name
, sdl
->sdl_data
, sdl
->sdl_nlen
);
327 name
[sdl
->sdl_nlen
] = '\0';
330 if ((s
= socket(af
, SOCK_DGRAM
, 0)) < 0) {
331 perror("gifconfig: socket");
335 ifconfig(argc
,argv
,af
,rafp
);
340 all
= -1; /* flag it as 'done' */
347 errx(1, "interface %s does not exist", name
);
355 ifconfig(int argc
, char **argv
, int af
, struct afswtch
*rafp
)
360 strncpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
362 strncpy(in6_ifr
.ifr_name
, name
, sizeof in6_ifr
.ifr_name
);
365 if (ioctl(s
, SIOCGIFMETRIC
, (caddr_t
)&ifr
) < 0)
366 perror("ioctl (SIOCGIFMETRIC)");
368 metric
= ifr
.ifr_metric
;
370 #if defined(SIOCGIFMTU) && !defined(__OpenBSD__)
371 if (ioctl(s
, SIOCGIFMTU
, (caddr_t
)&ifr
) < 0)
372 perror("ioctl (SIOCGIFMTU)");
387 for (p
= cmds
; p
->c_name
; p
++)
388 if (strcmp(*argv
, p
->c_name
) == 0)
390 if (p
->c_name
== NULL
&& setpsrc
)
391 p
++; /* got src, do dst */
393 if (p
->c_parameter
== NEXTARG
) {
395 errx(1, "'%s' requires argument",
397 (*p
->c_func
)(argv
[1], 0);
400 (*p
->c_func
)(*argv
, p
->c_parameter
);
405 strncpy(rafp
->af_addreq
, name
, sizeof ifr
.ifr_name
);
406 if (ioctl(s
, rafp
->af_pifaddr
, rafp
->af_addreq
) < 0)
407 Perror("ioctl (SIOCSIFPHYADDR)");
410 errx(1, "destination is not specified");
419 setifpsrc(char *addr
, int param
)
421 param
= 0; /*fool gcc*/
422 (*afp
->af_getaddr
)(addr
, PSRC
);
428 setifpdst(char *addr
, int param
)
430 param
= 0; /*fool gcc*/
431 (*afp
->af_getaddr
)(addr
, PDST
);
436 setifflags(char *vname
, int value
)
438 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
) < 0) {
439 Perror("ioctl (SIOCGIFFLAGS)");
442 strncpy(ifr
.ifr_name
, name
, sizeof (ifr
.ifr_name
));
443 flags
= (ifr
.ifr_flags
& 0xffff) | (ifr
.ifr_flagshigh
<< 16);
450 ifr
.ifr_flags
= flags
& 0xffff;
451 ifr
.ifr_flagshigh
= flags
>> 16;
452 if (ioctl(s
, SIOCSIFFLAGS
, (caddr_t
)&ifr
) < 0)
456 #ifdef SIOCDIFPHYADDR
459 delifaddrs(char *vname
, int param
)
461 param
= 0; /* fool gcc */
462 vname
= NULL
; /* ditto */
464 if (ioctl(s
, SIOCDIFPHYADDR
, (caddr_t
)&ifr
) < 0)
465 err(1, "ioctl(SIOCDIFPHYADDR)");
470 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
471 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
474 * Print the status of the interface. If an address family was
475 * specified, show it and it only; otherwise, show them all.
480 struct afswtch
*p
= NULL
;
482 struct if_msghdr
*myifm
;
484 printf("%s: ", name
);
485 printb("flags", flags
, IFFBITS
);
487 printf(" metric %d", metric
);
489 printf(" mtu %d", mtu
);
493 * XXX: Sigh. This is bad, I know. At this point, we may have
494 * *zero* RTM_NEWADDR's, so we have to "feel the water" before
495 * incrementing the loop. One day, I might feel inspired enough
496 * to get the top level loop to pass a count down here so we
497 * dont have to mess with this. -Peter
503 mynext
= next
+ ifm
->ifm_msglen
;
508 myifm
= (struct if_msghdr
*)mynext
;
510 if (myifm
->ifm_type
!= RTM_NEWADDR
)
515 ifm
= (struct if_msghdr
*)next
;
517 ifam
= (struct ifa_msghdr
*)myifm
;
518 info
.rti_addrs
= ifam
->ifam_addrs
;
520 /* Expand the compacted addresses */
521 rt_xaddrs((char *)(ifam
+ 1), ifam
->ifam_msglen
+ (char *)ifam
,
525 if (afp
->af_af
== info
.rti_info
[RTAX_IFA
]->sa_family
&&
526 afp
->af_status
!= ether_status
) {
528 if (p
->af_status
!= ether_status
)
531 } else for (p
= afs
; p
->af_name
; p
++) {
532 if (p
->af_af
== info
.rti_info
[RTAX_IFA
]->sa_family
&&
533 p
->af_status
!= ether_status
)
537 if (afp
== NULL
|| afp
->af_status
== ether_status
)
539 else if (afp
&& !p
) {
540 warnx("%s has no %s IFA address!", name
, afp
->af_name
);
547 phys_status(int force
)
551 int flags
= NI_NUMERICHOST
;
553 #ifndef SIOCGLIFPHYADDR
554 u_long srccmd
, dstcmd
;
560 force
= 0; /*fool gcc*/
562 psrcaddr
[0] = pdstaddr
[0] = '\0';
565 s6
= socket(AF_INET6
, SOCK_DGRAM
, 0);
568 srccmd
= SIOCGIFPSRCADDR
;
569 dstcmd
= SIOCGIFPDSTADDR
;
572 srccmd
= SIOCGIFPSRCADDR_IN6
;
573 dstcmd
= SIOCGIFPDSTADDR_IN6
;
574 ifrp
= (struct ifreq
*)&in6_ifr
;
578 srccmd
= SIOCGIFPSRCADDR
;
579 dstcmd
= SIOCGIFPDSTADDR
;
582 if (0 <= ioctl(s
, srccmd
, (caddr_t
)ifrp
)) {
584 if (ifrp
->ifr_addr
.sa_family
== AF_INET6
)
591 if (getnameinfo(&ifrp
->ifr_addr
, ifrp
->ifr_addr
.sa_len
,
592 psrcaddr
, sizeof(psrcaddr
), 0, 0, flags
) != 0)
595 if (0 <= ioctl(s
, dstcmd
, (caddr_t
)ifrp
)) {
596 if (getnameinfo(&ifrp
->ifr_addr
, ifrp
->ifr_addr
.sa_len
,
597 pdstaddr
, sizeof(pdstaddr
), 0, 0, flags
) != 0)
600 printf("\tphysical address %s %s --> %s\n", af
, psrcaddr
, pdstaddr
);
602 struct if_laddrreq iflr
;
604 force
= 0; /*fool gcc*/
606 psrcaddr
[0] = pdstaddr
[0] = '\0';
608 memset(&iflr
, 0, sizeof(iflr
));
609 memcpy(iflr
.iflr_name
, ifr
.ifr_name
, sizeof(iflr
.iflr_name
));
611 if (0 <= ioctl(s
, SIOCGLIFPHYADDR
, (caddr_t
)&iflr
)) {
612 switch (iflr
.addr
.ss_family
) {
622 if (getnameinfo((struct sockaddr
*)&iflr
.addr
, iflr
.addr
.ss_len
,
623 psrcaddr
, sizeof(psrcaddr
), 0, 0, flags
) != 0)
625 if (getnameinfo((struct sockaddr
*)&iflr
.dstaddr
,
626 iflr
.dstaddr
.ss_len
, pdstaddr
, sizeof(pdstaddr
), 0, 0,
630 printf("\tphysical address %s %s --> %s\n", af
, psrcaddr
, pdstaddr
);
637 struct sockaddr_in
*sin
, null_sin
;
642 memset(&null_sin
, 0, sizeof(null_sin
));
644 sin
= (struct sockaddr_in
*)info
.rti_info
[RTAX_IFA
];
645 if (!sin
|| sin
->sin_family
!= AF_INET
) {
648 /* warnx("%s has no AF_INET IFA address!", name); */
651 printf("\tinet %s ", inet_ntoa(sin
->sin_addr
));
653 if (flags
& IFF_POINTOPOINT
) {
654 /* note RTAX_BRD overlap with IFF_BROADCAST */
655 sin
= (struct sockaddr_in
*)info
.rti_info
[RTAX_BRD
];
658 printf("--> %s ", inet_ntoa(sin
->sin_addr
));
661 sin
= (struct sockaddr_in
*)info
.rti_info
[RTAX_NETMASK
];
664 printf("netmask 0x%x ", (u_int32_t
)ntohl(sin
->sin_addr
.s_addr
));
666 if (flags
& IFF_BROADCAST
) {
667 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
668 sin
= (struct sockaddr_in
*)info
.rti_info
[RTAX_BRD
];
669 if (sin
&& sin
->sin_addr
.s_addr
!= 0)
670 printf("broadcast %s", inet_ntoa(sin
->sin_addr
));
677 in6_status(int force
)
679 struct sockaddr_in6
*sin
, null_sin
;
680 char hostname
[NI_MAXHOST
];
681 int niflags
= NI_NUMERICHOST
;
683 memset(&null_sin
, 0, sizeof(null_sin
));
684 #ifdef NI_WITHSCOPEID
685 niflags
|= NI_WITHSCOPEID
;
688 sin
= (struct sockaddr_in6
*)info
.rti_info
[RTAX_IFA
];
689 if (!sin
|| sin
->sin6_family
!= AF_INET6
) {
692 /* warnx("%s has no AF_INET6 IFA address!", name); */
696 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
698 ntohs(*(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2]);
699 sin
->sin6_addr
.s6_addr
[2] = 0;
700 sin
->sin6_addr
.s6_addr
[3] = 0;
703 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
,
704 hostname
, sizeof(hostname
), 0, 0, niflags
);
705 printf("\tinet6 %s ", hostname
);
707 if (flags
& IFF_POINTOPOINT
) {
708 /* note RTAX_BRD overlap with IFF_BROADCAST */
709 sin
= (struct sockaddr_in6
*)info
.rti_info
[RTAX_BRD
];
711 * some of ther interfaces do not have valid destination
714 if (sin
->sin6_family
== AF_INET6
) {
716 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
718 ntohs(*(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2]);
719 sin
->sin6_addr
.s6_addr
[2] = 0;
720 sin
->sin6_addr
.s6_addr
[3] = 0;
723 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
,
724 hostname
, sizeof(hostname
), 0, 0, niflags
);
725 printf("--> %s ", hostname
);
729 sin
= (struct sockaddr_in6
*)info
.rti_info
[RTAX_NETMASK
];
732 printf(" prefixlen %d ", prefix(&sin
->sin6_addr
,
733 sizeof(struct in6_addr
)));
741 ether_status(int dummy
)
746 dummy
= 0; /*fool gcc*/
748 cp
= (char *)LLADDR(sdl
);
749 if ((n
= sdl
->sdl_alen
) > 0) {
750 if (sdl
->sdl_type
== IFT_ETHER
)
753 printf ("\tlladdr ");
755 printf("%02x%c",*cp
++ & 0xff, n
>0? ':' : ' ');
766 errx(1, "%s: no such interface", cmd
);
770 errx(1, "%s: permission denied", cmd
);
778 #define SIN(x) ((struct sockaddr_in *) &(x))
779 struct sockaddr_in
*sintab
[] = {
780 SIN(addreq
.ifra_addr
), SIN(addreq
.ifra_dstaddr
)};
783 in_getaddr(char *s
, int which
)
785 struct sockaddr_in
*sin
= sintab
[which
];
789 sin
->sin_len
= sizeof(*sin
);
790 sin
->sin_family
= AF_INET
;
792 if (inet_aton(s
, &sin
->sin_addr
))
794 else if ((hp
= gethostbyname(s
)) != NULL
)
795 bcopy(hp
->h_addr
, (char *)&sin
->sin_addr
, hp
->h_length
);
796 else if ((np
= getnetbyname(s
)) != NULL
)
797 sin
->sin_addr
= inet_makeaddr(np
->n_net
, INADDR_ANY
);
799 errx(1, "%s: bad value", s
);
803 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
804 struct sockaddr_in6
*sin6tab
[] = {
805 SIN6(in6_addreq
.ifra_addr
), SIN6(in6_addreq
.ifra_dstaddr
)};
808 in6_getaddr(char *s
, int which
)
810 struct sockaddr_in6
*sin
= sin6tab
[which
];
812 sin
->sin6_len
= sizeof(*sin
);
813 sin
->sin6_family
= AF_INET6
;
815 if (inet_pton(AF_INET6
, s
, &sin
->sin6_addr
) != 1)
816 errx(1, "%s: bad value", s
);
820 in6_getprefix(char *plen
, int which
)
822 struct sockaddr_in6
*sin
= sin6tab
[which
];
824 int len
= atoi(plen
);
826 if ((len
< 0) || (len
> 128))
827 errx(1, "%s: bad value", plen
);
828 sin
->sin6_len
= sizeof(*sin
);
829 sin
->sin6_family
= AF_INET6
;
830 if ((len
== 0) || (len
== 128)) {
831 memset(&sin
->sin6_addr
, -1, sizeof(struct in6_addr
));
834 for (cp
= (u_char
*)&sin
->sin6_addr
; len
> 7; len
-= 8)
836 *cp
= (-1) << (8 - len
);
841 * Print a value a la the %b format of the kernel's printf
844 printb(char *s
, unsigned int v
, char *bits
)
849 if (bits
&& *bits
== 8)
850 printf("%s=%o", s
, v
& 0xffff);
852 printf("%s=%x", s
, v
& 0xffff);
856 while ((i
= *bits
++) != 0) {
857 if ((v
& (1 << (i
-1))) != 0) {
861 for (; (c
= *bits
) > 32; bits
++)
864 for (; *bits
> 32; bits
++)
873 prefix(void *val
, int size
)
875 u_char
*name
= (u_char
*)val
;
876 int byte
, bit
, plen
= 0;
878 for (byte
= 0; byte
< size
; byte
++, plen
+= 8)
879 if (name
[byte
] != 0xff)
883 for (bit
= 7; bit
!= 0; bit
--, plen
++)
884 if (!(name
[byte
] & (1 << bit
)))
886 for (; bit
!= 0; bit
--)
887 if (name
[byte
] & (1 << bit
))
890 for (; byte
< size
; byte
++)