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(__FreeBSD__) && __FreeBSD__ >= 3
62 #include <net/if_var.h>
63 #endif /* __FreeBSD__ >= 3 */
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 __P((char *, int));
106 void setifpdst __P((char *, int));
107 void setifflags __P((char *, int));
108 #ifdef SIOCDIFPHYADDR
109 void delifaddrs __P((char *, int));
112 #define NEXTARG 0xffffff
116 int c_parameter; /* NEXTARG means next argv */
117 void (*c_func) __P((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 __P((int, char *[]));
133 void status __P((void));
134 void phys_status __P((int));
135 void in_status __P((int));
137 void in6_status __P((int));
139 void ether_status __P((int));
140 void Perror __P((char *));
141 void in_getaddr __P((char *, int));
143 void in6_getaddr __P((char *, int));
144 void in6_getprefix __P((char *, int));
146 void printb __P((char *, unsigned int, char *));
147 int prefix __P((void *, int));
149 char ntop_buf[INET6_ADDRSTRLEN];
151 /* Known address families */
155 void (*af_status) __P((int));
156 void (*af_getaddr) __P((char *, int));
157 void (*af_getprefix) __P((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 __P((caddr_t, caddr_t, struct rt_addrinfo *));
176 int ifconfig __P((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(cp, cplim, rtinfo)
192 struct rt_addrinfo *rtinfo;
197 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
198 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
199 if ((rtinfo->rti_addrs & (1 << i)) == 0)
201 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
208 * Grunge for new-style sysctl() decoding.. :-(
209 * Apologies to the world for committing gross things like this in 1996..
211 struct if_msghdr *ifm;
212 struct ifa_msghdr *ifam;
213 struct sockaddr_dl *sdl;
214 struct rt_addrinfo info;
215 char *buf, *lim, *next;
224 struct afswtch *rafp = NULL;
231 "usage: gifconfig interface [af] [physsrc physdst]\n");
232 #ifdef SIOCDIFPHYADDR
234 " gifconfig interface delete\n");
241 strncpy(name, *argv, sizeof(name));
242 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
245 for (afp = rafp = afs; rafp->af_name; rafp++)
246 if (strcmp(rafp->af_name, *argv) == 0) {
247 afp = rafp; argc--; argv++;
251 af = ifr.ifr_addr.sa_family = rafp->af_af;
257 mib[3] = 0; /* address family */
258 mib[4] = NET_RT_IFLIST;
261 /* if particular family specified, only ask about it */
266 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
267 errx(1, "iflist-sysctl-estimate");
268 if ((buf = malloc(needed)) == NULL)
270 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
271 errx(1, "actual retrieval of interface table");
275 if (strcmp(name, "-a") == 0)
276 all = 1; /* All interfaces */
277 else if (strcmp(name, "-au") == 0)
278 all = 2; /* All IFF_UPinterfaces */
279 else if (strcmp(name, "-ad") == 0)
280 all = 3; /* All !IFF_UP interfaces */
282 for (next = buf; next < lim; next += ifm->ifm_msglen) {
284 ifm = (struct if_msghdr *)next;
286 /* XXX: Swallow up leftover NEWADDR messages */
287 if (ifm->ifm_type == RTM_NEWADDR)
290 if (ifm->ifm_type == RTM_IFINFO) {
291 sdl = (struct sockaddr_dl *)(ifm + 1);
292 flags = ifm->ifm_flags;
294 errx(1, "out of sync parsing NET_RT_IFLIST");
300 if (strlen(name) != sdl->sdl_nlen)
301 continue; /* not same len */
302 if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
303 continue; /* not same name */
306 break; /* always do it */
308 if ((flags & IFF_UP) == 0)
309 continue; /* not up */
313 continue; /* not down */
318 * Let's just do it for gif only
320 if (sdl->sdl_type != IFT_GIF) {
324 fprintf(stderr, "gifconfig: %s is not gif.\n",
330 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
331 name[sdl->sdl_nlen] = '\0';
334 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
335 perror("gifconfig: socket");
339 ifconfig(argc,argv,af,rafp);
344 all = -1; /* flag it as 'done' */
351 errx(1, "interface %s does not exist", name);
359 ifconfig(argc, argv, af, rafp)
363 struct afswtch *rafp;
368 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
370 strncpy(in6_ifr.ifr_name, name, sizeof in6_ifr.ifr_name);
373 if (ioctl(s, SIOCGIFMETRIC, (caddr_t)&ifr) < 0)
374 perror("ioctl (SIOCGIFMETRIC)");
376 metric = ifr.ifr_metric;
378 #if defined(SIOCGIFMTU) && !defined(__OpenBSD__)
379 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0)
380 perror("ioctl (SIOCGIFMTU)");
393 register struct cmd *p;
395 for (p = cmds; p->c_name; p++)
396 if (strcmp(*argv, p->c_name) == 0)
398 if (p->c_name == 0 && setpsrc)
399 p++; /* got src, do dst */
401 if (p->c_parameter == NEXTARG) {
403 errx(1, "'%s' requires argument",
405 (*p->c_func)(argv[1], 0);
408 (*p->c_func)(*argv, p->c_parameter);
413 strncpy(rafp->af_addreq, name, sizeof ifr.ifr_name);
414 if (ioctl(s, rafp->af_pifaddr, rafp->af_addreq) < 0)
415 Perror("ioctl (SIOCSIFPHYADDR)");
418 errx(1, "destination is not specified");
427 setifpsrc(addr, param)
431 param = 0; /*fool gcc*/
432 (*afp->af_getaddr)(addr, PSRC);
438 setifpdst(addr, param)
442 param = 0; /*fool gcc*/
443 (*afp->af_getaddr)(addr, PDST);
448 setifflags(vname, value)
452 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
453 Perror("ioctl (SIOCGIFFLAGS)");
456 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
457 flags = ifr.ifr_flags;
464 ifr.ifr_flags = flags;
465 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
469 #ifdef SIOCDIFPHYADDR
472 delifaddrs(vname, param)
476 param = 0; /* fool gcc */
477 vname = NULL; /* ditto */
479 if (ioctl(s, SIOCDIFPHYADDR, (caddr_t)&ifr) < 0)
480 err(1, "ioctl(SIOCDIFPHYADDR)");
485 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
486 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
489 * Print the status of the interface. If an address family was
490 * specified, show it and it only; otherwise, show them all.
495 struct afswtch *p = NULL;
497 struct if_msghdr *myifm;
499 printf("%s: ", name);
500 printb("flags", flags, IFFBITS);
502 printf(" metric %d", metric);
504 printf(" mtu %d", mtu);
508 * XXX: Sigh. This is bad, I know. At this point, we may have
509 * *zero* RTM_NEWADDR's, so we have to "feel the water" before
510 * incrementing the loop. One day, I might feel inspired enough
511 * to get the top level loop to pass a count down here so we
512 * dont have to mess with this. -Peter
518 mynext = next + ifm->ifm_msglen;
523 myifm = (struct if_msghdr *)mynext;
525 if (myifm->ifm_type != RTM_NEWADDR)
530 ifm = (struct if_msghdr *)next;
532 ifam = (struct ifa_msghdr *)myifm;
533 info.rti_addrs = ifam->ifam_addrs;
535 /* Expand the compacted addresses */
536 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
540 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family &&
541 afp->af_status != ether_status) {
543 if (p->af_status != ether_status)
546 } else for (p = afs; p->af_name; p++) {
547 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family &&
548 p->af_status != ether_status)
552 if (afp == NULL || afp->af_status == ether_status)
554 else if (afp && !p) {
555 warnx("%s has no %s IFA address!", name, afp->af_name);
567 int flags = NI_NUMERICHOST;
569 #ifndef SIOCGLIFPHYADDR
570 u_long srccmd, dstcmd;
576 force = 0; /*fool gcc*/
578 psrcaddr[0] = pdstaddr[0] = '\0';
581 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
584 srccmd = SIOCGIFPSRCADDR;
585 dstcmd = SIOCGIFPDSTADDR;
588 srccmd = SIOCGIFPSRCADDR_IN6;
589 dstcmd = SIOCGIFPDSTADDR_IN6;
590 ifrp = (struct ifreq *)&in6_ifr;
594 srccmd = SIOCGIFPSRCADDR;
595 dstcmd = SIOCGIFPDSTADDR;
598 if (0 <= ioctl(s, srccmd, (caddr_t)ifrp)) {
600 if (ifrp->ifr_addr.sa_family == AF_INET6)
607 if (getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
608 psrcaddr, sizeof(psrcaddr), 0, 0, flags) != 0)
611 if (0 <= ioctl(s, dstcmd, (caddr_t)ifrp)) {
612 if (getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
613 pdstaddr, sizeof(pdstaddr), 0, 0, flags) != 0)
616 printf("\tphysical address %s %s --> %s\n", af, psrcaddr, pdstaddr);
618 struct if_laddrreq iflr;
620 force = 0; /*fool gcc*/
622 psrcaddr[0] = pdstaddr[0] = '\0';
624 memset(&iflr, 0, sizeof(iflr));
625 memcpy(iflr.iflr_name, ifr.ifr_name, sizeof(iflr.iflr_name));
627 if (0 <= ioctl(s, SIOCGLIFPHYADDR, (caddr_t)&iflr)) {
628 switch (iflr.addr.ss_family) {
638 if (getnameinfo((struct sockaddr *)&iflr.addr, iflr.addr.ss_len,
639 psrcaddr, sizeof(psrcaddr), 0, 0, flags) != 0)
641 if (getnameinfo((struct sockaddr *)&iflr.dstaddr,
642 iflr.dstaddr.ss_len, pdstaddr, sizeof(pdstaddr), 0, 0,
646 printf("\tphysical address %s %s --> %s\n", af, psrcaddr, pdstaddr);
654 struct sockaddr_in *sin, null_sin;
659 memset(&null_sin, 0, sizeof(null_sin));
661 sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
662 if (!sin || sin->sin_family != AF_INET) {
665 /* warnx("%s has no AF_INET IFA address!", name); */
668 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
670 if (flags & IFF_POINTOPOINT) {
671 /* note RTAX_BRD overlap with IFF_BROADCAST */
672 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
675 printf("--> %s ", inet_ntoa(sin->sin_addr));
678 sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
681 printf("netmask 0x%x ", (u_int32_t)ntohl(sin->sin_addr.s_addr));
683 if (flags & IFF_BROADCAST) {
684 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
685 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
686 if (sin && sin->sin_addr.s_addr != 0)
687 printf("broadcast %s", inet_ntoa(sin->sin_addr));
697 struct sockaddr_in6 *sin, null_sin;
698 char hostname[NI_MAXHOST];
699 int niflags = NI_NUMERICHOST;
701 memset(&null_sin, 0, sizeof(null_sin));
702 #ifdef NI_WITHSCOPEID
703 niflags |= NI_WITHSCOPEID;
706 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_IFA];
707 if (!sin || sin->sin6_family != AF_INET6) {
710 /* warnx("%s has no AF_INET6 IFA address!", name); */
714 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
716 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]);
717 sin->sin6_addr.s6_addr[2] = 0;
718 sin->sin6_addr.s6_addr[3] = 0;
721 getnameinfo((struct sockaddr *)sin, sin->sin6_len,
722 hostname, sizeof(hostname), 0, 0, niflags);
723 printf("\tinet6 %s ", hostname);
725 if (flags & IFF_POINTOPOINT) {
726 /* note RTAX_BRD overlap with IFF_BROADCAST */
727 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_BRD];
729 * some of ther interfaces do not have valid destination
732 if (sin->sin6_family == AF_INET6) {
734 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
736 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]);
737 sin->sin6_addr.s6_addr[2] = 0;
738 sin->sin6_addr.s6_addr[3] = 0;
741 getnameinfo((struct sockaddr *)sin, sin->sin6_len,
742 hostname, sizeof(hostname), 0, 0, niflags);
743 printf("--> %s ", hostname);
747 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_NETMASK];
750 printf(" prefixlen %d ", prefix(&sin->sin6_addr,
751 sizeof(struct in6_addr)));
765 dummy = 0; /*fool gcc*/
767 cp = (char *)LLADDR(sdl);
768 if ((n = sdl->sdl_alen) > 0) {
769 if (sdl->sdl_type == IFT_ETHER)
772 printf ("\tlladdr ");
774 printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' ');
786 errx(1, "%s: no such interface", cmd);
790 errx(1, "%s: permission denied", cmd);
798 #define SIN(x) ((struct sockaddr_in *) &(x))
799 struct sockaddr_in *sintab[] = {
800 SIN(addreq.ifra_addr), SIN(addreq.ifra_dstaddr)};
807 register struct sockaddr_in *sin = sintab[which];
811 sin->sin_len = sizeof(*sin);
812 sin->sin_family = AF_INET;
814 if (inet_aton(s, &sin->sin_addr))
816 else if ((hp = gethostbyname(s)) != NULL)
817 bcopy(hp->h_addr, (char *)&sin->sin_addr, hp->h_length);
818 else if ((np = getnetbyname(s)) != NULL)
819 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
821 errx(1, "%s: bad value", s);
825 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
826 struct sockaddr_in6 *sin6tab[] = {
827 SIN6(in6_addreq.ifra_addr), SIN6(in6_addreq.ifra_dstaddr)};
830 in6_getaddr(s, which)
834 register struct sockaddr_in6 *sin = sin6tab[which];
836 sin->sin6_len = sizeof(*sin);
837 sin->sin6_family = AF_INET6;
839 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
840 errx(1, "%s: bad value", s);
844 in6_getprefix(plen, which)
848 register struct sockaddr_in6 *sin = sin6tab[which];
850 int len = atoi(plen);
852 if ((len < 0) || (len > 128))
853 errx(1, "%s: bad value", plen);
854 sin->sin6_len = sizeof(*sin);
855 sin->sin6_family = AF_INET6;
856 if ((len == 0) || (len == 128)) {
857 memset(&sin->sin6_addr, -1, sizeof(struct in6_addr));
860 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
862 *cp = (-1) << (8 - len);
867 * Print a value a la the %b format of the kernel's printf
872 register unsigned int v;
875 register int i, any = 0;
878 if (bits && *bits == 8)
879 printf("%s=%o", s, v & 0xffff);
881 printf("%s=%x", s, v & 0xffff);
885 while ((i = *bits++) != 0) {
886 if ((v & (1 << (i-1))) != 0) {
890 for (; (c = *bits) > 32; bits++)
893 for (; *bits > 32; bits++)
906 register u_char *name = (u_char *)val;
907 register int byte, bit, plen = 0;
909 for (byte = 0; byte < size; byte++, plen += 8)
910 if (name[byte] != 0xff)
914 for (bit = 7; bit != 0; bit--, plen++)
915 if (!(name[byte] & (1 << bit)))
917 for (; bit != 0; bit--)
918 if (name[byte] & (1 << bit))
921 for (; byte < size; byte++)