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(__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 & 0xffff) | (ifr.ifr_flagshigh << 16);
464 ifr.ifr_flags = flags & 0xffff;
465 ifr.ifr_flagshigh = flags >> 16;
466 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
470 #ifdef SIOCDIFPHYADDR
473 delifaddrs(vname, param)
477 param = 0; /* fool gcc */
478 vname = NULL; /* ditto */
480 if (ioctl(s, SIOCDIFPHYADDR, (caddr_t)&ifr) < 0)
481 err(1, "ioctl(SIOCDIFPHYADDR)");
486 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
487 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
490 * Print the status of the interface. If an address family was
491 * specified, show it and it only; otherwise, show them all.
496 struct afswtch *p = NULL;
498 struct if_msghdr *myifm;
500 printf("%s: ", name);
501 printb("flags", flags, IFFBITS);
503 printf(" metric %d", metric);
505 printf(" mtu %d", mtu);
509 * XXX: Sigh. This is bad, I know. At this point, we may have
510 * *zero* RTM_NEWADDR's, so we have to "feel the water" before
511 * incrementing the loop. One day, I might feel inspired enough
512 * to get the top level loop to pass a count down here so we
513 * dont have to mess with this. -Peter
519 mynext = next + ifm->ifm_msglen;
524 myifm = (struct if_msghdr *)mynext;
526 if (myifm->ifm_type != RTM_NEWADDR)
531 ifm = (struct if_msghdr *)next;
533 ifam = (struct ifa_msghdr *)myifm;
534 info.rti_addrs = ifam->ifam_addrs;
536 /* Expand the compacted addresses */
537 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
541 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family &&
542 afp->af_status != ether_status) {
544 if (p->af_status != ether_status)
547 } else for (p = afs; p->af_name; p++) {
548 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family &&
549 p->af_status != ether_status)
553 if (afp == NULL || afp->af_status == ether_status)
555 else if (afp && !p) {
556 warnx("%s has no %s IFA address!", name, afp->af_name);
568 int flags = NI_NUMERICHOST;
570 #ifndef SIOCGLIFPHYADDR
571 u_long srccmd, dstcmd;
577 force = 0; /*fool gcc*/
579 psrcaddr[0] = pdstaddr[0] = '\0';
582 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
585 srccmd = SIOCGIFPSRCADDR;
586 dstcmd = SIOCGIFPDSTADDR;
589 srccmd = SIOCGIFPSRCADDR_IN6;
590 dstcmd = SIOCGIFPDSTADDR_IN6;
591 ifrp = (struct ifreq *)&in6_ifr;
595 srccmd = SIOCGIFPSRCADDR;
596 dstcmd = SIOCGIFPDSTADDR;
599 if (0 <= ioctl(s, srccmd, (caddr_t)ifrp)) {
601 if (ifrp->ifr_addr.sa_family == AF_INET6)
608 if (getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
609 psrcaddr, sizeof(psrcaddr), 0, 0, flags) != 0)
612 if (0 <= ioctl(s, dstcmd, (caddr_t)ifrp)) {
613 if (getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
614 pdstaddr, sizeof(pdstaddr), 0, 0, flags) != 0)
617 printf("\tphysical address %s %s --> %s\n", af, psrcaddr, pdstaddr);
619 struct if_laddrreq iflr;
621 force = 0; /*fool gcc*/
623 psrcaddr[0] = pdstaddr[0] = '\0';
625 memset(&iflr, 0, sizeof(iflr));
626 memcpy(iflr.iflr_name, ifr.ifr_name, sizeof(iflr.iflr_name));
628 if (0 <= ioctl(s, SIOCGLIFPHYADDR, (caddr_t)&iflr)) {
629 switch (iflr.addr.ss_family) {
639 if (getnameinfo((struct sockaddr *)&iflr.addr, iflr.addr.ss_len,
640 psrcaddr, sizeof(psrcaddr), 0, 0, flags) != 0)
642 if (getnameinfo((struct sockaddr *)&iflr.dstaddr,
643 iflr.dstaddr.ss_len, pdstaddr, sizeof(pdstaddr), 0, 0,
647 printf("\tphysical address %s %s --> %s\n", af, psrcaddr, pdstaddr);
655 struct sockaddr_in *sin, null_sin;
660 memset(&null_sin, 0, sizeof(null_sin));
662 sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
663 if (!sin || sin->sin_family != AF_INET) {
666 /* warnx("%s has no AF_INET IFA address!", name); */
669 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
671 if (flags & IFF_POINTOPOINT) {
672 /* note RTAX_BRD overlap with IFF_BROADCAST */
673 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
676 printf("--> %s ", inet_ntoa(sin->sin_addr));
679 sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
682 printf("netmask 0x%x ", (u_int32_t)ntohl(sin->sin_addr.s_addr));
684 if (flags & IFF_BROADCAST) {
685 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
686 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
687 if (sin && sin->sin_addr.s_addr != 0)
688 printf("broadcast %s", inet_ntoa(sin->sin_addr));
698 struct sockaddr_in6 *sin, null_sin;
699 char hostname[NI_MAXHOST];
700 int niflags = NI_NUMERICHOST;
702 memset(&null_sin, 0, sizeof(null_sin));
703 #ifdef NI_WITHSCOPEID
704 niflags |= NI_WITHSCOPEID;
707 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_IFA];
708 if (!sin || sin->sin6_family != AF_INET6) {
711 /* warnx("%s has no AF_INET6 IFA address!", name); */
715 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
717 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]);
718 sin->sin6_addr.s6_addr[2] = 0;
719 sin->sin6_addr.s6_addr[3] = 0;
722 getnameinfo((struct sockaddr *)sin, sin->sin6_len,
723 hostname, sizeof(hostname), 0, 0, niflags);
724 printf("\tinet6 %s ", hostname);
726 if (flags & IFF_POINTOPOINT) {
727 /* note RTAX_BRD overlap with IFF_BROADCAST */
728 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_BRD];
730 * some of ther interfaces do not have valid destination
733 if (sin->sin6_family == AF_INET6) {
735 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
737 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]);
738 sin->sin6_addr.s6_addr[2] = 0;
739 sin->sin6_addr.s6_addr[3] = 0;
742 getnameinfo((struct sockaddr *)sin, sin->sin6_len,
743 hostname, sizeof(hostname), 0, 0, niflags);
744 printf("--> %s ", hostname);
748 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_NETMASK];
751 printf(" prefixlen %d ", prefix(&sin->sin6_addr,
752 sizeof(struct in6_addr)));
766 dummy = 0; /*fool gcc*/
768 cp = (char *)LLADDR(sdl);
769 if ((n = sdl->sdl_alen) > 0) {
770 if (sdl->sdl_type == IFT_ETHER)
773 printf ("\tlladdr ");
775 printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' ');
787 errx(1, "%s: no such interface", cmd);
791 errx(1, "%s: permission denied", cmd);
799 #define SIN(x) ((struct sockaddr_in *) &(x))
800 struct sockaddr_in *sintab[] = {
801 SIN(addreq.ifra_addr), SIN(addreq.ifra_dstaddr)};
808 register struct sockaddr_in *sin = sintab[which];
812 sin->sin_len = sizeof(*sin);
813 sin->sin_family = AF_INET;
815 if (inet_aton(s, &sin->sin_addr))
817 else if ((hp = gethostbyname(s)) != NULL)
818 bcopy(hp->h_addr, (char *)&sin->sin_addr, hp->h_length);
819 else if ((np = getnetbyname(s)) != NULL)
820 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
822 errx(1, "%s: bad value", s);
826 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
827 struct sockaddr_in6 *sin6tab[] = {
828 SIN6(in6_addreq.ifra_addr), SIN6(in6_addreq.ifra_dstaddr)};
831 in6_getaddr(s, which)
835 register struct sockaddr_in6 *sin = sin6tab[which];
837 sin->sin6_len = sizeof(*sin);
838 sin->sin6_family = AF_INET6;
840 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
841 errx(1, "%s: bad value", s);
845 in6_getprefix(plen, which)
849 register struct sockaddr_in6 *sin = sin6tab[which];
851 int len = atoi(plen);
853 if ((len < 0) || (len > 128))
854 errx(1, "%s: bad value", plen);
855 sin->sin6_len = sizeof(*sin);
856 sin->sin6_family = AF_INET6;
857 if ((len == 0) || (len == 128)) {
858 memset(&sin->sin6_addr, -1, sizeof(struct in6_addr));
861 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
863 *cp = (-1) << (8 - len);
868 * Print a value a la the %b format of the kernel's printf
873 register unsigned int v;
876 register int i, any = 0;
879 if (bits && *bits == 8)
880 printf("%s=%o", s, v & 0xffff);
882 printf("%s=%x", s, v & 0xffff);
886 while ((i = *bits++) != 0) {
887 if ((v & (1 << (i-1))) != 0) {
891 for (; (c = *bits) > 32; bits++)
894 for (; *bits > 32; bits++)
907 register u_char *name = (u_char *)val;
908 register int byte, bit, plen = 0;
910 for (byte = 0; byte < size; byte++, plen += 8)
911 if (name[byte] != 0xff)
915 for (bit = 7; bit != 0; bit--, plen++)
916 if (!(name[byte] & (1 << bit)))
918 for (; bit != 0; bit--)
919 if (name[byte] & (1 << bit))
922 for (; byte < size; byte++)