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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * gifconfig, derived from ifconfig
36 * @(#) Copyright (c) 1983, 1993\n\
37 * The Regents of the University of California. All rights reserved.\n
39 * @(#)ifconfig.c 8.2 (Berkeley) 2/16/94
43 * 951109 - Andrew@pubnix.net - Changed to iterative buffer growing mechanism
44 * for ifconfig -a so all interfaces are queried.
46 * 960101 - peter@freebsd.org - Blow away the SIOCGIFCONF code and use
47 * sysctl() to get the structured interface conf
48 * and parse the messages in there. REALLY UGLY!
51 #include <sys/param.h>
52 #include <sys/socket.h>
53 #include <sys/ioctl.h>
54 #include <sys/sysctl.h>
57 #if defined(__DragonFly__)
58 #include <net/if_var.h>
59 #endif /* __DragonFly__ */
60 #include <net/if_dl.h>
61 #include <net/if_types.h>
62 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #include <arpa/inet.h>
68 #include <sys/protosw.h>
82 struct ifaliasreq addreq;
84 struct in6_ifreq in6_ifr;
85 struct in6_aliasreq in6_addreq;
98 char ntop_buf[INET6_ADDRSTRLEN]; /*inet_ntop()*/
101 void setifpsrc(char *, int);
102 void setifpdst(char *, int);
103 void setifflags(char *, int);
104 #ifdef SIOCDIFPHYADDR
105 void delifaddrs(char *, int);
108 #define NEXTARG 0xffffff
112 int c_parameter; /* NEXTARG means next argv */
113 void (*c_func)(char *, int);
115 { "up", IFF_UP, setifflags } ,
116 { "down", -IFF_UP, setifflags },
117 #ifdef SIOCDIFPHYADDR
118 { "delete", 0, delifaddrs },
125 * XNS support liberally adapted from code written at the University of
126 * Maryland principally by James O'Toole and Chris Torek.
128 int main(int, char *[]);
130 void phys_status(int);
133 void in6_status(int);
135 void ether_status(int);
137 void in_getaddr(char *, int);
139 void in6_getaddr(char *, int);
140 void in6_getprefix(char *, int);
142 void printb(char *, unsigned int, char *);
143 int prefix(void *, int);
145 char ntop_buf[INET6_ADDRSTRLEN];
147 /* Known address families */
151 void (*af_status)(int);
152 void (*af_getaddr)(char *, int);
153 void (*af_getprefix)(char *, int);
158 #define C(x) ((caddr_t) &x)
159 { "inet", AF_INET, in_status, in_getaddr, 0,
160 SIOCSIFPHYADDR, C(addreq), C(ifr) },
162 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
163 SIOCSIFPHYADDR_IN6, C(in6_addreq), C(in6_ifr) },
165 { "ether", AF_INET, ether_status, NULL, NULL }, /* XXX not real!! */
169 struct afswtch *afp = NULL; /*the address family being set or asked about*/
171 void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
172 int ifconfig(int argc, char *argv[], int af, struct afswtch *rafp);
177 * Expand the compacted form of addresses as returned via the
178 * configuration read via sysctl().
182 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
183 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
186 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
191 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
192 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
193 if ((rtinfo->rti_addrs & (1 << i)) == 0)
195 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
202 * Grunge for new-style sysctl() decoding.. :-(
203 * Apologies to the world for committing gross things like this in 1996..
205 struct if_msghdr *ifm;
206 struct ifa_msghdr *ifam;
207 struct sockaddr_dl *sdl;
208 struct rt_addrinfo info;
209 char *buf, *lim, *next;
213 main(int argc, char **argv)
216 struct afswtch *rafp = NULL;
223 "usage: gifconfig interface [af] [physsrc physdst]\n");
224 #ifdef SIOCDIFPHYADDR
226 " gifconfig interface delete\n");
233 strncpy(name, *argv, sizeof(name));
234 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
237 for (afp = rafp = afs; rafp->af_name; rafp++)
238 if (strcmp(rafp->af_name, *argv) == 0) {
239 afp = rafp; argc--; argv++;
243 af = ifr.ifr_addr.sa_family = rafp->af_af;
249 mib[3] = 0; /* address family */
250 mib[4] = NET_RT_IFLIST;
253 /* if particular family specified, only ask about it */
258 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
259 errx(1, "iflist-sysctl-estimate");
260 if ((buf = malloc(needed)) == NULL)
262 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
263 errx(1, "actual retrieval of interface table");
267 if (strcmp(name, "-a") == 0)
268 all = 1; /* All interfaces */
269 else if (strcmp(name, "-au") == 0)
270 all = 2; /* All IFF_UPinterfaces */
271 else if (strcmp(name, "-ad") == 0)
272 all = 3; /* All !IFF_UP interfaces */
274 for (next = buf; next < lim; next += ifm->ifm_msglen) {
276 ifm = (struct if_msghdr *)next;
278 /* XXX: Swallow up leftover NEWADDR messages */
279 if (ifm->ifm_type == RTM_NEWADDR)
282 if (ifm->ifm_type == RTM_IFINFO) {
283 sdl = (struct sockaddr_dl *)(ifm + 1);
284 flags = ifm->ifm_flags;
286 errx(1, "out of sync parsing NET_RT_IFLIST");
292 if (strlen(name) != sdl->sdl_nlen)
293 continue; /* not same len */
294 if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
295 continue; /* not same name */
298 break; /* always do it */
300 if ((flags & IFF_UP) == 0)
301 continue; /* not up */
305 continue; /* not down */
310 * Let's just do it for gif only
312 if (sdl->sdl_type != IFT_GIF) {
316 fprintf(stderr, "gifconfig: %s is not gif.\n",
322 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
323 name[sdl->sdl_nlen] = '\0';
326 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
327 perror("gifconfig: socket");
331 ifconfig(argc,argv,af,rafp);
336 all = -1; /* flag it as 'done' */
343 errx(1, "interface %s does not exist", name);
351 ifconfig(int argc, char **argv, int af, struct afswtch *rafp)
356 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
358 strncpy(in6_ifr.ifr_name, name, sizeof in6_ifr.ifr_name);
361 if (ioctl(s, SIOCGIFMETRIC, (caddr_t)&ifr) < 0)
362 perror("ioctl (SIOCGIFMETRIC)");
364 metric = ifr.ifr_metric;
366 #if defined(SIOCGIFMTU) && !defined(__OpenBSD__)
367 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0)
368 perror("ioctl (SIOCGIFMTU)");
383 for (p = cmds; p->c_name; p++)
384 if (strcmp(*argv, p->c_name) == 0)
386 if (p->c_name == NULL && setpsrc)
387 p++; /* got src, do dst */
389 if (p->c_parameter == NEXTARG) {
391 errx(1, "'%s' requires argument",
393 (*p->c_func)(argv[1], 0);
396 (*p->c_func)(*argv, p->c_parameter);
401 strncpy(rafp->af_addreq, name, sizeof ifr.ifr_name);
402 if (ioctl(s, rafp->af_pifaddr, rafp->af_addreq) < 0)
403 Perror("ioctl (SIOCSIFPHYADDR)");
406 errx(1, "destination is not specified");
415 setifpsrc(char *addr, int param)
417 param = 0; /*fool gcc*/
418 (*afp->af_getaddr)(addr, PSRC);
424 setifpdst(char *addr, int param)
426 param = 0; /*fool gcc*/
427 (*afp->af_getaddr)(addr, PDST);
432 setifflags(char *vname, int value)
434 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
435 Perror("ioctl (SIOCGIFFLAGS)");
438 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
439 flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
446 ifr.ifr_flags = flags & 0xffff;
447 ifr.ifr_flagshigh = flags >> 16;
448 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
452 #ifdef SIOCDIFPHYADDR
455 delifaddrs(char *vname, int param)
457 param = 0; /* fool gcc */
458 vname = NULL; /* ditto */
460 if (ioctl(s, SIOCDIFPHYADDR, (caddr_t)&ifr) < 0)
461 err(1, "ioctl(SIOCDIFPHYADDR)");
466 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
467 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
470 * Print the status of the interface. If an address family was
471 * specified, show it and it only; otherwise, show them all.
476 struct afswtch *p = NULL;
478 struct if_msghdr *myifm;
480 printf("%s: ", name);
481 printb("flags", flags, IFFBITS);
483 printf(" metric %d", metric);
485 printf(" mtu %d", mtu);
489 * XXX: Sigh. This is bad, I know. At this point, we may have
490 * *zero* RTM_NEWADDR's, so we have to "feel the water" before
491 * incrementing the loop. One day, I might feel inspired enough
492 * to get the top level loop to pass a count down here so we
493 * dont have to mess with this. -Peter
499 mynext = next + ifm->ifm_msglen;
504 myifm = (struct if_msghdr *)mynext;
506 if (myifm->ifm_type != RTM_NEWADDR)
511 ifm = (struct if_msghdr *)next;
513 ifam = (struct ifa_msghdr *)myifm;
514 info.rti_addrs = ifam->ifam_addrs;
516 /* Expand the compacted addresses */
517 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
521 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family &&
522 afp->af_status != ether_status) {
524 if (p->af_status != ether_status)
527 } else for (p = afs; p->af_name; p++) {
528 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family &&
529 p->af_status != ether_status)
533 if (afp == NULL || afp->af_status == ether_status)
535 else if (afp && !p) {
536 warnx("%s has no %s IFA address!", name, afp->af_name);
543 phys_status(int force)
547 int flags = NI_NUMERICHOST;
549 #ifndef SIOCGLIFPHYADDR
550 u_long srccmd, dstcmd;
556 force = 0; /*fool gcc*/
558 psrcaddr[0] = pdstaddr[0] = '\0';
561 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
564 srccmd = SIOCGIFPSRCADDR;
565 dstcmd = SIOCGIFPDSTADDR;
568 srccmd = SIOCGIFPSRCADDR_IN6;
569 dstcmd = SIOCGIFPDSTADDR_IN6;
570 ifrp = (struct ifreq *)&in6_ifr;
574 srccmd = SIOCGIFPSRCADDR;
575 dstcmd = SIOCGIFPDSTADDR;
578 if (0 <= ioctl(s, srccmd, (caddr_t)ifrp)) {
580 if (ifrp->ifr_addr.sa_family == AF_INET6)
587 if (getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
588 psrcaddr, sizeof(psrcaddr), 0, 0, flags) != 0)
591 if (0 <= ioctl(s, dstcmd, (caddr_t)ifrp)) {
592 if (getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
593 pdstaddr, sizeof(pdstaddr), 0, 0, flags) != 0)
596 printf("\tphysical address %s %s --> %s\n", af, psrcaddr, pdstaddr);
598 struct if_laddrreq iflr;
600 force = 0; /*fool gcc*/
602 psrcaddr[0] = pdstaddr[0] = '\0';
604 memset(&iflr, 0, sizeof(iflr));
605 memcpy(iflr.iflr_name, ifr.ifr_name, sizeof(iflr.iflr_name));
607 if (0 <= ioctl(s, SIOCGLIFPHYADDR, (caddr_t)&iflr)) {
608 switch (iflr.addr.ss_family) {
618 if (getnameinfo((struct sockaddr *)&iflr.addr, iflr.addr.ss_len,
619 psrcaddr, sizeof(psrcaddr), 0, 0, flags) != 0)
621 if (getnameinfo((struct sockaddr *)&iflr.dstaddr,
622 iflr.dstaddr.ss_len, pdstaddr, sizeof(pdstaddr), 0, 0,
626 printf("\tphysical address %s %s --> %s\n", af, psrcaddr, pdstaddr);
633 struct sockaddr_in *sin, null_sin;
638 memset(&null_sin, 0, sizeof(null_sin));
640 sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
641 if (!sin || sin->sin_family != AF_INET) {
644 /* warnx("%s has no AF_INET IFA address!", name); */
647 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
649 if (flags & IFF_POINTOPOINT) {
650 /* note RTAX_BRD overlap with IFF_BROADCAST */
651 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
654 printf("--> %s ", inet_ntoa(sin->sin_addr));
657 sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
660 printf("netmask 0x%x ", (u_int32_t)ntohl(sin->sin_addr.s_addr));
662 if (flags & IFF_BROADCAST) {
663 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
664 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
665 if (sin && sin->sin_addr.s_addr != 0)
666 printf("broadcast %s", inet_ntoa(sin->sin_addr));
673 in6_status(int force)
675 struct sockaddr_in6 *sin, null_sin;
676 char hostname[NI_MAXHOST];
677 int niflags = NI_NUMERICHOST;
679 memset(&null_sin, 0, sizeof(null_sin));
680 #ifdef NI_WITHSCOPEID
681 niflags |= NI_WITHSCOPEID;
684 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_IFA];
685 if (!sin || sin->sin6_family != AF_INET6) {
688 /* warnx("%s has no AF_INET6 IFA address!", name); */
692 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
694 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]);
695 sin->sin6_addr.s6_addr[2] = 0;
696 sin->sin6_addr.s6_addr[3] = 0;
699 getnameinfo((struct sockaddr *)sin, sin->sin6_len,
700 hostname, sizeof(hostname), 0, 0, niflags);
701 printf("\tinet6 %s ", hostname);
703 if (flags & IFF_POINTOPOINT) {
704 /* note RTAX_BRD overlap with IFF_BROADCAST */
705 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_BRD];
707 * some of ther interfaces do not have valid destination
710 if (sin->sin6_family == AF_INET6) {
712 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
714 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]);
715 sin->sin6_addr.s6_addr[2] = 0;
716 sin->sin6_addr.s6_addr[3] = 0;
719 getnameinfo((struct sockaddr *)sin, sin->sin6_len,
720 hostname, sizeof(hostname), 0, 0, niflags);
721 printf("--> %s ", hostname);
725 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_NETMASK];
728 printf(" prefixlen %d ", prefix(&sin->sin6_addr,
729 sizeof(struct in6_addr)));
737 ether_status(int dummy)
742 dummy = 0; /*fool gcc*/
744 cp = (char *)LLADDR(sdl);
745 if ((n = sdl->sdl_alen) > 0) {
746 if (sdl->sdl_type == IFT_ETHER)
749 printf ("\tlladdr ");
751 printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' ');
762 errx(1, "%s: no such interface", cmd);
766 errx(1, "%s: permission denied", cmd);
774 #define SIN(x) ((struct sockaddr_in *) &(x))
775 struct sockaddr_in *sintab[] = {
776 SIN(addreq.ifra_addr), SIN(addreq.ifra_dstaddr)};
779 in_getaddr(char *s, int which)
781 struct sockaddr_in *sin = sintab[which];
785 sin->sin_len = sizeof(*sin);
786 sin->sin_family = AF_INET;
788 if (inet_aton(s, &sin->sin_addr))
790 else if ((hp = gethostbyname(s)) != NULL)
791 bcopy(hp->h_addr, (char *)&sin->sin_addr, hp->h_length);
792 else if ((np = getnetbyname(s)) != NULL)
793 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
795 errx(1, "%s: bad value", s);
799 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
800 struct sockaddr_in6 *sin6tab[] = {
801 SIN6(in6_addreq.ifra_addr), SIN6(in6_addreq.ifra_dstaddr)};
804 in6_getaddr(char *s, int which)
806 struct sockaddr_in6 *sin = sin6tab[which];
808 sin->sin6_len = sizeof(*sin);
809 sin->sin6_family = AF_INET6;
811 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
812 errx(1, "%s: bad value", s);
816 in6_getprefix(char *plen, int which)
818 struct sockaddr_in6 *sin = sin6tab[which];
820 int len = atoi(plen);
822 if ((len < 0) || (len > 128))
823 errx(1, "%s: bad value", plen);
824 sin->sin6_len = sizeof(*sin);
825 sin->sin6_family = AF_INET6;
826 if ((len == 0) || (len == 128)) {
827 memset(&sin->sin6_addr, -1, sizeof(struct in6_addr));
830 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
832 *cp = (-1) << (8 - len);
837 * Print a value a la the %b format of the kernel's printf
840 printb(char *s, unsigned int v, char *bits)
845 if (bits && *bits == 8)
846 printf("%s=%o", s, v & 0xffff);
848 printf("%s=%x", s, v & 0xffff);
852 while ((i = *bits++) != 0) {
853 if ((v & (1 << (i-1))) != 0) {
857 for (; (c = *bits) > 32; bits++)
860 for (; *bits > 32; bits++)
869 prefix(void *val, int size)
871 u_char *name = (u_char *)val;
872 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++)