2 /* $KAME: gifconfig.c,v 1.12 2000/05/22 05:50:43 itojun 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 char hostname[NI_MAXHOST];
568 u_long srccmd, dstcmd;
569 int flags = NI_NUMERICHOST;
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)) {
599 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
600 hostname, sizeof(hostname), 0, 0, flags);
602 if (ifrp->ifr_addr.sa_family == AF_INET6)
605 sprintf(psrcaddr, "inet%s %s", ver, hostname);
607 if (0 <= ioctl(s, dstcmd, (caddr_t)ifrp)) {
608 getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
609 hostname, sizeof(hostname), 0, 0, flags);
610 sprintf(pdstaddr, "%s", hostname);
612 printf("\tphysical address %s --> %s\n", psrcaddr, pdstaddr);
619 struct sockaddr_in *sin, null_sin;
624 memset(&null_sin, 0, sizeof(null_sin));
626 sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
627 if (!sin || sin->sin_family != AF_INET) {
630 /* warnx("%s has no AF_INET IFA address!", name); */
633 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
635 if (flags & IFF_POINTOPOINT) {
636 /* note RTAX_BRD overlap with IFF_BROADCAST */
637 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
640 printf("--> %s ", inet_ntoa(sin->sin_addr));
643 sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
646 printf("netmask 0x%x ", (u_int32_t)ntohl(sin->sin_addr.s_addr));
648 if (flags & IFF_BROADCAST) {
649 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
650 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
651 if (sin && sin->sin_addr.s_addr != 0)
652 printf("broadcast %s", inet_ntoa(sin->sin_addr));
662 struct sockaddr_in6 *sin, null_sin;
663 char hostname[NI_MAXHOST];
664 int niflags = NI_NUMERICHOST;
666 memset(&null_sin, 0, sizeof(null_sin));
667 #ifdef NI_WITHSCOPEID
668 niflags |= NI_WITHSCOPEID;
671 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_IFA];
672 if (!sin || sin->sin6_family != AF_INET6) {
675 /* warnx("%s has no AF_INET6 IFA address!", name); */
679 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
681 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]);
682 sin->sin6_addr.s6_addr[2] = 0;
683 sin->sin6_addr.s6_addr[3] = 0;
686 getnameinfo((struct sockaddr *)sin, sin->sin6_len,
687 hostname, sizeof(hostname), 0, 0, niflags);
688 printf("\tinet6 %s ", hostname);
690 if (flags & IFF_POINTOPOINT) {
691 /* note RTAX_BRD overlap with IFF_BROADCAST */
692 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_BRD];
694 * some of ther interfaces do not have valid destination
697 if (sin->sin6_family == AF_INET6) {
699 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
701 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]);
702 sin->sin6_addr.s6_addr[2] = 0;
703 sin->sin6_addr.s6_addr[3] = 0;
706 getnameinfo((struct sockaddr *)sin, sin->sin6_len,
707 hostname, sizeof(hostname), 0, 0, niflags);
708 printf("--> %s ", hostname);
712 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_NETMASK];
715 printf(" prefixlen %d ", prefix(&sin->sin6_addr,
716 sizeof(struct in6_addr)));
730 dummy = 0; /*fool gcc*/
732 cp = (char *)LLADDR(sdl);
733 if ((n = sdl->sdl_alen) > 0) {
734 if (sdl->sdl_type == IFT_ETHER)
737 printf ("\tlladdr ");
739 printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' ');
751 errx(1, "%s: no such interface", cmd);
755 errx(1, "%s: permission denied", cmd);
763 #define SIN(x) ((struct sockaddr_in *) &(x))
764 struct sockaddr_in *sintab[] = {
765 SIN(addreq.ifra_addr), SIN(addreq.ifra_dstaddr)};
772 register struct sockaddr_in *sin = sintab[which];
776 sin->sin_len = sizeof(*sin);
777 sin->sin_family = AF_INET;
779 if (inet_aton(s, &sin->sin_addr))
781 else if ((hp = gethostbyname(s)) != NULL)
782 bcopy(hp->h_addr, (char *)&sin->sin_addr, hp->h_length);
783 else if ((np = getnetbyname(s)) != NULL)
784 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
786 errx(1, "%s: bad value", s);
790 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
791 struct sockaddr_in6 *sin6tab[] = {
792 SIN6(in6_addreq.ifra_addr), SIN6(in6_addreq.ifra_dstaddr)};
795 in6_getaddr(s, which)
799 register struct sockaddr_in6 *sin = sin6tab[which];
801 sin->sin6_len = sizeof(*sin);
802 sin->sin6_family = AF_INET6;
804 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
805 errx(1, "%s: bad value", s);
809 in6_getprefix(plen, which)
813 register struct sockaddr_in6 *sin = sin6tab[which];
815 int len = atoi(plen);
817 if ((len < 0) || (len > 128))
818 errx(1, "%s: bad value", plen);
819 sin->sin6_len = sizeof(*sin);
820 sin->sin6_family = AF_INET6;
821 if ((len == 0) || (len == 128)) {
822 memset(&sin->sin6_addr, -1, sizeof(struct in6_addr));
825 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
827 *cp = (-1) << (8 - len);
832 * Print a value a la the %b format of the kernel's printf
837 register unsigned int v;
840 register int i, any = 0;
843 if (bits && *bits == 8)
844 printf("%s=%o", s, v & 0xffff);
846 printf("%s=%x", s, v & 0xffff);
850 while ((i = *bits++) != 0) {
851 if ((v & (1 << (i-1))) != 0) {
855 for (; (c = *bits) > 32; bits++)
858 for (; *bits > 32; bits++)
871 register u_char *name = (u_char *)val;
872 register int byte, bit, plen = 0;
874 for (byte = 0; byte < size; byte++, plen += 8)
875 if (name[byte] != 0xff)
879 for (bit = 7; bit != 0; bit--, plen++)
880 if (!(name[byte] & (1 << bit)))
882 for (; bit != 0; bit--)
883 if (name[byte] & (1 << bit))
886 for (; byte < size; byte++)