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. 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>
81 static struct ifreq ifr;
82 static struct ifaliasreq addreq;
84 static struct in6_ifreq in6_ifr;
85 static struct in6_aliasreq in6_addreq;
92 static int setpsrc = 0;
93 static int newaddr = 0;
96 static void setifpsrc(char *, int);
97 static void setifpdst(char *, int);
98 static void setifflags(char *, int);
100 static void delifaddrs(char *, int);
103 #define NEXTARG 0xffffff
107 int c_parameter; /* NEXTARG means next argv */
108 void (*c_func)(char *, int);
110 { "up", IFF_UP, setifflags } ,
111 { "down", -IFF_UP, setifflags },
112 #ifdef SIOCDIFPHYADDR
113 { "delete", 0, delifaddrs },
120 * XNS support liberally adapted from code written at the University of
121 * Maryland principally by James O'Toole and Chris Torek.
123 static void status(void);
124 static void phys_status(int);
125 static void in_status(int);
127 static void in6_status(int);
129 static void ether_status(int);
130 static void Perror(char *);
131 static void in_getaddr(char *, int);
133 static void in6_getaddr(char *, int);
134 static void in6_getprefix(char *, int);
136 static void printb(char *, unsigned int, char *);
137 static int prefix(void *, int);
139 /* Known address families */
140 static struct afswtch {
143 void (*af_status)(int);
144 void (*af_getaddr)(char *, int);
145 void (*af_getprefix)(char *, int);
150 #define C(x) ((caddr_t) &x)
151 { "inet", AF_INET, in_status, in_getaddr, 0,
152 SIOCSIFPHYADDR, C(addreq), C(ifr) },
154 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
155 SIOCSIFPHYADDR_IN6, C(in6_addreq), C(in6_ifr) },
157 { "ether", AF_INET, ether_status, NULL, NULL }, /* XXX not real!! */
161 static struct afswtch *afp = NULL; /*the address family being set or asked about*/
163 static void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
164 static int ifconfig(int argc, char *argv[], int af, struct afswtch *rafp);
167 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
172 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
173 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
174 if ((rtinfo->rti_addrs & (1 << i)) == 0)
176 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
183 * Grunge for new-style sysctl() decoding.. :-(
184 * Apologies to the world for committing gross things like this in 1996..
186 static struct if_msghdr *ifm;
187 static struct ifa_msghdr *ifam;
188 static struct sockaddr_dl *sdl;
189 static struct rt_addrinfo info;
190 static char *buf, *lim, *next;
194 main(int argc, char **argv)
197 struct afswtch *rafp = NULL;
204 "usage: gifconfig interface [af] [physsrc physdst]\n");
205 #ifdef SIOCDIFPHYADDR
207 " gifconfig interface delete\n");
214 strncpy(name, *argv, sizeof(name));
215 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
218 for (afp = rafp = afs; rafp->af_name; rafp++)
219 if (strcmp(rafp->af_name, *argv) == 0) {
220 afp = rafp; argc--; argv++;
224 af = ifr.ifr_addr.sa_family = rafp->af_af;
230 mib[3] = 0; /* address family */
231 mib[4] = NET_RT_IFLIST;
234 /* if particular family specified, only ask about it */
239 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
240 errx(1, "iflist-sysctl-estimate");
241 if ((buf = malloc(needed)) == NULL)
243 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
244 errx(1, "actual retrieval of interface table");
248 if (strcmp(name, "-a") == 0)
249 all = 1; /* All interfaces */
250 else if (strcmp(name, "-au") == 0)
251 all = 2; /* All IFF_UPinterfaces */
252 else if (strcmp(name, "-ad") == 0)
253 all = 3; /* All !IFF_UP interfaces */
255 for (next = buf; next < lim; next += ifm->ifm_msglen) {
257 ifm = (struct if_msghdr *)next;
259 /* XXX: Swallow up leftover NEWADDR messages */
260 if (ifm->ifm_type == RTM_NEWADDR)
263 if (ifm->ifm_type == RTM_IFINFO) {
264 sdl = (struct sockaddr_dl *)(ifm + 1);
265 flags = ifm->ifm_flags;
267 errx(1, "out of sync parsing NET_RT_IFLIST");
273 if (strlen(name) != sdl->sdl_nlen)
274 continue; /* not same len */
275 if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
276 continue; /* not same name */
279 break; /* always do it */
281 if ((flags & IFF_UP) == 0)
282 continue; /* not up */
286 continue; /* not down */
291 * Let's just do it for gif only
293 if (sdl->sdl_type != IFT_GIF) {
297 fprintf(stderr, "gifconfig: %s is not gif.\n",
303 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
304 name[sdl->sdl_nlen] = '\0';
307 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
308 perror("gifconfig: socket");
312 ifconfig(argc,argv,af,rafp);
317 all = -1; /* flag it as 'done' */
324 errx(1, "interface %s does not exist", name);
332 ifconfig(int argc, char **argv, int af, struct afswtch *rafp)
337 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
339 strncpy(in6_ifr.ifr_name, name, sizeof in6_ifr.ifr_name);
342 if (ioctl(s, SIOCGIFMETRIC, (caddr_t)&ifr) < 0)
343 perror("ioctl (SIOCGIFMETRIC)");
345 metric = ifr.ifr_metric;
347 #if defined(SIOCGIFMTU) && !defined(__OpenBSD__)
348 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0)
349 perror("ioctl (SIOCGIFMTU)");
364 for (p = cmds; p->c_name; p++)
365 if (strcmp(*argv, p->c_name) == 0)
367 if (p->c_name == NULL && setpsrc)
368 p++; /* got src, do dst */
370 if (p->c_parameter == NEXTARG) {
372 errx(1, "'%s' requires argument",
374 (*p->c_func)(argv[1], 0);
377 (*p->c_func)(*argv, p->c_parameter);
382 strncpy(rafp->af_addreq, name, sizeof ifr.ifr_name);
383 if (ioctl(s, rafp->af_pifaddr, rafp->af_addreq) < 0)
384 Perror("ioctl (SIOCSIFPHYADDR)");
387 errx(1, "destination is not specified");
396 setifpsrc(char *addr, int param)
398 param = 0; /*fool gcc*/
399 (*afp->af_getaddr)(addr, PSRC);
405 setifpdst(char *addr, int param)
407 param = 0; /*fool gcc*/
408 (*afp->af_getaddr)(addr, PDST);
413 setifflags(char *vname, int value)
415 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
416 Perror("ioctl (SIOCGIFFLAGS)");
419 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
420 flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
427 ifr.ifr_flags = flags & 0xffff;
428 ifr.ifr_flagshigh = flags >> 16;
429 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
433 #ifdef SIOCDIFPHYADDR
436 delifaddrs(char *vname, int param)
438 param = 0; /* fool gcc */
439 vname = NULL; /* ditto */
441 if (ioctl(s, SIOCDIFPHYADDR, (caddr_t)&ifr) < 0)
442 err(1, "ioctl(SIOCDIFPHYADDR)");
447 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
448 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
451 * Print the status of the interface. If an address family was
452 * specified, show it and it only; otherwise, show them all.
457 struct afswtch *p = NULL;
459 struct if_msghdr *myifm;
461 printf("%s: ", name);
462 printb("flags", flags, IFFBITS);
464 printf(" metric %d", metric);
466 printf(" mtu %d", mtu);
470 * XXX: Sigh. This is bad, I know. At this point, we may have
471 * *zero* RTM_NEWADDR's, so we have to "feel the water" before
472 * incrementing the loop. One day, I might feel inspired enough
473 * to get the top level loop to pass a count down here so we
474 * dont have to mess with this. -Peter
480 mynext = next + ifm->ifm_msglen;
485 myifm = (struct if_msghdr *)mynext;
487 if (myifm->ifm_type != RTM_NEWADDR)
492 ifm = (struct if_msghdr *)next;
494 ifam = (struct ifa_msghdr *)myifm;
495 info.rti_addrs = ifam->ifam_addrs;
497 /* Expand the compacted addresses */
498 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
502 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family &&
503 afp->af_status != ether_status) {
505 if (p->af_status != ether_status)
508 } else for (p = afs; p->af_name; p++) {
509 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family &&
510 p->af_status != ether_status)
514 if (afp == NULL || afp->af_status == ether_status)
516 else if (afp && !p) {
517 warnx("%s has no %s IFA address!", name, afp->af_name);
524 phys_status(int force)
528 int flags = NI_NUMERICHOST;
530 #ifndef SIOCGLIFPHYADDR
531 u_long srccmd, dstcmd;
537 force = 0; /*fool gcc*/
539 psrcaddr[0] = pdstaddr[0] = '\0';
542 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
545 srccmd = SIOCGIFPSRCADDR;
546 dstcmd = SIOCGIFPDSTADDR;
549 srccmd = SIOCGIFPSRCADDR_IN6;
550 dstcmd = SIOCGIFPDSTADDR_IN6;
551 ifrp = (struct ifreq *)&in6_ifr;
555 srccmd = SIOCGIFPSRCADDR;
556 dstcmd = SIOCGIFPDSTADDR;
559 if (0 <= ioctl(s, srccmd, (caddr_t)ifrp)) {
561 if (ifrp->ifr_addr.sa_family == AF_INET6)
568 if (getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
569 psrcaddr, sizeof(psrcaddr), 0, 0, flags) != 0)
572 if (0 <= ioctl(s, dstcmd, (caddr_t)ifrp)) {
573 if (getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
574 pdstaddr, sizeof(pdstaddr), 0, 0, flags) != 0)
577 printf("\tphysical address %s %s --> %s\n", af, psrcaddr, pdstaddr);
579 struct if_laddrreq iflr;
581 force = 0; /*fool gcc*/
583 psrcaddr[0] = pdstaddr[0] = '\0';
585 memset(&iflr, 0, sizeof(iflr));
586 memcpy(iflr.iflr_name, ifr.ifr_name, sizeof(iflr.iflr_name));
588 if (0 <= ioctl(s, SIOCGLIFPHYADDR, (caddr_t)&iflr)) {
589 switch (iflr.addr.ss_family) {
599 if (getnameinfo((struct sockaddr *)&iflr.addr, iflr.addr.ss_len,
600 psrcaddr, sizeof(psrcaddr), 0, 0, flags) != 0)
602 if (getnameinfo((struct sockaddr *)&iflr.dstaddr,
603 iflr.dstaddr.ss_len, pdstaddr, sizeof(pdstaddr), 0, 0,
607 printf("\tphysical address %s %s --> %s\n", af, psrcaddr, pdstaddr);
614 struct sockaddr_in *sin, null_sin;
619 memset(&null_sin, 0, sizeof(null_sin));
621 sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
622 if (!sin || sin->sin_family != AF_INET) {
625 /* warnx("%s has no AF_INET IFA address!", name); */
628 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
630 if (flags & IFF_POINTOPOINT) {
631 /* note RTAX_BRD overlap with IFF_BROADCAST */
632 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
635 printf("--> %s ", inet_ntoa(sin->sin_addr));
638 sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
641 printf("netmask 0x%x ", (u_int32_t)ntohl(sin->sin_addr.s_addr));
643 if (flags & IFF_BROADCAST) {
644 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
645 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
646 if (sin && sin->sin_addr.s_addr != 0)
647 printf("broadcast %s", inet_ntoa(sin->sin_addr));
654 in6_status(int force)
656 struct sockaddr_in6 *sin, null_sin;
657 char hostname[NI_MAXHOST];
658 int niflags = NI_NUMERICHOST;
660 memset(&null_sin, 0, sizeof(null_sin));
661 #ifdef NI_WITHSCOPEID
662 niflags |= NI_WITHSCOPEID;
665 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_IFA];
666 if (!sin || sin->sin6_family != AF_INET6) {
669 /* warnx("%s has no AF_INET6 IFA address!", name); */
673 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
675 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]);
676 sin->sin6_addr.s6_addr[2] = 0;
677 sin->sin6_addr.s6_addr[3] = 0;
680 getnameinfo((struct sockaddr *)sin, sin->sin6_len,
681 hostname, sizeof(hostname), 0, 0, niflags);
682 printf("\tinet6 %s ", hostname);
684 if (flags & IFF_POINTOPOINT) {
685 /* note RTAX_BRD overlap with IFF_BROADCAST */
686 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_BRD];
688 * some of ther interfaces do not have valid destination
691 if (sin->sin6_family == AF_INET6) {
693 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
695 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]);
696 sin->sin6_addr.s6_addr[2] = 0;
697 sin->sin6_addr.s6_addr[3] = 0;
700 getnameinfo((struct sockaddr *)sin, sin->sin6_len,
701 hostname, sizeof(hostname), 0, 0, niflags);
702 printf("--> %s ", hostname);
706 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_NETMASK];
709 printf(" prefixlen %d ", prefix(&sin->sin6_addr,
710 sizeof(struct in6_addr)));
718 ether_status(int dummy)
723 dummy = 0; /*fool gcc*/
725 cp = (char *)LLADDR(sdl);
726 if ((n = sdl->sdl_alen) > 0) {
727 if (sdl->sdl_type == IFT_ETHER)
730 printf ("\tlladdr ");
732 printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' ');
743 errx(1, "%s: no such interface", cmd);
747 errx(1, "%s: permission denied", cmd);
755 #define SIN(x) ((struct sockaddr_in *) &(x))
756 static struct sockaddr_in *sintab[] = {
757 SIN(addreq.ifra_addr), SIN(addreq.ifra_dstaddr)};
760 in_getaddr(char *s, int which)
762 struct sockaddr_in *sin = sintab[which];
766 sin->sin_len = sizeof(*sin);
767 sin->sin_family = AF_INET;
769 if (inet_aton(s, &sin->sin_addr))
771 else if ((hp = gethostbyname(s)) != NULL)
772 bcopy(hp->h_addr, (char *)&sin->sin_addr, hp->h_length);
773 else if ((np = getnetbyname(s)) != NULL)
774 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
776 errx(1, "%s: bad value", s);
780 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
781 static struct sockaddr_in6 *sin6tab[] = {
782 SIN6(in6_addreq.ifra_addr), SIN6(in6_addreq.ifra_dstaddr)};
785 in6_getaddr(char *s, int which)
787 struct sockaddr_in6 *sin = sin6tab[which];
789 sin->sin6_len = sizeof(*sin);
790 sin->sin6_family = AF_INET6;
792 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
793 errx(1, "%s: bad value", s);
797 in6_getprefix(char *plen, int which)
799 struct sockaddr_in6 *sin = sin6tab[which];
801 int len = atoi(plen);
803 if ((len < 0) || (len > 128))
804 errx(1, "%s: bad value", plen);
805 sin->sin6_len = sizeof(*sin);
806 sin->sin6_family = AF_INET6;
807 if ((len == 0) || (len == 128)) {
808 memset(&sin->sin6_addr, -1, sizeof(struct in6_addr));
811 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
813 *cp = (-1) << (8 - len);
818 * Print a value a la the %b format of the kernel's printf
821 printb(char *s, unsigned int v, char *bits)
826 if (bits && *bits == 8)
827 printf("%s=%o", s, v & 0xffff);
829 printf("%s=%x", s, v & 0xffff);
833 while ((i = *bits++) != 0) {
834 if ((v & (1 << (i-1))) != 0) {
838 for (; (c = *bits) > 32; bits++)
841 for (; *bits > 32; bits++)
850 prefix(void *val, int size)
852 u_char *name = (u_char *)val;
853 int byte, bit, plen = 0;
855 for (byte = 0; byte < size; byte++, plen += 8)
856 if (name[byte] != 0xff)
860 for (bit = 7; bit != 0; bit--, plen++)
861 if (!(name[byte] & (1 << bit)))
863 for (; bit != 0; bit--)
864 if (name[byte] & (1 << bit))
867 for (; byte < size; byte++)