1 /* $FreeBSD: src/usr.sbin/gifconfig/gifconfig.c,v 1.2.2.4 2002/08/30 14:23:39 sobomax Exp $ */
2 /* $DragonFly: src/usr.sbin/gifconfig/gifconfig.c,v 1.3 2003/11/03 19:31:37 eirikn Exp $ */
3 /* $KAME: gifconfig.c,v 1.14 2001/01/01 04:04:56 jinmei Exp $ */
6 * Copyright (c) 1983, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * gifconfig, derived from ifconfig
41 * @(#) Copyright (c) 1983, 1993\n\
42 * The Regents of the University of California. All rights reserved.\n
44 * @(#)ifconfig.c 8.2 (Berkeley) 2/16/94
48 * 951109 - Andrew@pubnix.net - Changed to iterative buffer growing mechanism
49 * for ifconfig -a so all interfaces are queried.
51 * 960101 - peter@freebsd.org - Blow away the SIOCGIFCONF code and use
52 * sysctl() to get the structured interface conf
53 * and parse the messages in there. REALLY UGLY!
56 #include <sys/param.h>
57 #include <sys/socket.h>
58 #include <sys/ioctl.h>
59 #include <sys/sysctl.h>
62 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
63 #include <net/if_var.h>
64 #endif /* __FreeBSD__ >= 3 */
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #include <arpa/inet.h>
73 #include <sys/protosw.h>
87 struct ifaliasreq addreq;
89 struct in6_ifreq in6_ifr;
90 struct in6_aliasreq in6_addreq;
103 char ntop_buf[INET6_ADDRSTRLEN]; /*inet_ntop()*/
106 void setifpsrc(char *, int);
107 void setifpdst(char *, int);
108 void setifflags(char *, int);
109 #ifdef SIOCDIFPHYADDR
110 void delifaddrs(char *, int);
113 #define NEXTARG 0xffffff
117 int c_parameter; /* NEXTARG means next argv */
118 void (*c_func)(char *, int);
120 { "up", IFF_UP, setifflags } ,
121 { "down", -IFF_UP, setifflags },
122 #ifdef SIOCDIFPHYADDR
123 { "delete", 0, delifaddrs },
130 * XNS support liberally adapted from code written at the University of
131 * Maryland principally by James O'Toole and Chris Torek.
133 int main(int, char *[]);
135 void phys_status(int);
138 void in6_status(int);
140 void ether_status(int);
142 void in_getaddr(char *, int);
144 void in6_getaddr(char *, int);
145 void in6_getprefix(char *, int);
147 void printb(char *, unsigned int, char *);
148 int prefix(void *, int);
150 char ntop_buf[INET6_ADDRSTRLEN];
152 /* Known address families */
156 void (*af_status)(int);
157 void (*af_getaddr)(char *, int);
158 void (*af_getprefix)(char *, int);
163 #define C(x) ((caddr_t) &x)
164 { "inet", AF_INET, in_status, in_getaddr, 0,
165 SIOCSIFPHYADDR, C(addreq), C(ifr) },
167 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_getprefix,
168 SIOCSIFPHYADDR_IN6, C(in6_addreq), C(in6_ifr) },
170 { "ether", AF_INET, ether_status, NULL, NULL }, /* XXX not real!! */
174 struct afswtch *afp = NULL; /*the address family being set or asked about*/
176 void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
177 int ifconfig(int argc, char *argv[], int af, struct afswtch *rafp);
182 * Expand the compacted form of addresses as returned via the
183 * configuration read via sysctl().
187 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
188 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
191 rt_xaddrs(cp, cplim, rtinfo)
193 struct rt_addrinfo *rtinfo;
198 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
199 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
200 if ((rtinfo->rti_addrs & (1 << i)) == 0)
202 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
209 * Grunge for new-style sysctl() decoding.. :-(
210 * Apologies to the world for committing gross things like this in 1996..
212 struct if_msghdr *ifm;
213 struct ifa_msghdr *ifam;
214 struct sockaddr_dl *sdl;
215 struct rt_addrinfo info;
216 char *buf, *lim, *next;
225 struct afswtch *rafp = NULL;
232 "usage: gifconfig interface [af] [physsrc physdst]\n");
233 #ifdef SIOCDIFPHYADDR
235 " gifconfig interface delete\n");
242 strncpy(name, *argv, sizeof(name));
243 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
246 for (afp = rafp = afs; rafp->af_name; rafp++)
247 if (strcmp(rafp->af_name, *argv) == 0) {
248 afp = rafp; argc--; argv++;
252 af = ifr.ifr_addr.sa_family = rafp->af_af;
258 mib[3] = 0; /* address family */
259 mib[4] = NET_RT_IFLIST;
262 /* if particular family specified, only ask about it */
267 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
268 errx(1, "iflist-sysctl-estimate");
269 if ((buf = malloc(needed)) == NULL)
271 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
272 errx(1, "actual retrieval of interface table");
276 if (strcmp(name, "-a") == 0)
277 all = 1; /* All interfaces */
278 else if (strcmp(name, "-au") == 0)
279 all = 2; /* All IFF_UPinterfaces */
280 else if (strcmp(name, "-ad") == 0)
281 all = 3; /* All !IFF_UP interfaces */
283 for (next = buf; next < lim; next += ifm->ifm_msglen) {
285 ifm = (struct if_msghdr *)next;
287 /* XXX: Swallow up leftover NEWADDR messages */
288 if (ifm->ifm_type == RTM_NEWADDR)
291 if (ifm->ifm_type == RTM_IFINFO) {
292 sdl = (struct sockaddr_dl *)(ifm + 1);
293 flags = ifm->ifm_flags;
295 errx(1, "out of sync parsing NET_RT_IFLIST");
301 if (strlen(name) != sdl->sdl_nlen)
302 continue; /* not same len */
303 if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
304 continue; /* not same name */
307 break; /* always do it */
309 if ((flags & IFF_UP) == 0)
310 continue; /* not up */
314 continue; /* not down */
319 * Let's just do it for gif only
321 if (sdl->sdl_type != IFT_GIF) {
325 fprintf(stderr, "gifconfig: %s is not gif.\n",
331 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
332 name[sdl->sdl_nlen] = '\0';
335 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
336 perror("gifconfig: socket");
340 ifconfig(argc,argv,af,rafp);
345 all = -1; /* flag it as 'done' */
352 errx(1, "interface %s does not exist", name);
360 ifconfig(argc, argv, af, rafp)
364 struct afswtch *rafp;
369 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
371 strncpy(in6_ifr.ifr_name, name, sizeof in6_ifr.ifr_name);
374 if (ioctl(s, SIOCGIFMETRIC, (caddr_t)&ifr) < 0)
375 perror("ioctl (SIOCGIFMETRIC)");
377 metric = ifr.ifr_metric;
379 #if defined(SIOCGIFMTU) && !defined(__OpenBSD__)
380 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0)
381 perror("ioctl (SIOCGIFMTU)");
394 register struct cmd *p;
396 for (p = cmds; p->c_name; p++)
397 if (strcmp(*argv, p->c_name) == 0)
399 if (p->c_name == 0 && setpsrc)
400 p++; /* got src, do dst */
402 if (p->c_parameter == NEXTARG) {
404 errx(1, "'%s' requires argument",
406 (*p->c_func)(argv[1], 0);
409 (*p->c_func)(*argv, p->c_parameter);
414 strncpy(rafp->af_addreq, name, sizeof ifr.ifr_name);
415 if (ioctl(s, rafp->af_pifaddr, rafp->af_addreq) < 0)
416 Perror("ioctl (SIOCSIFPHYADDR)");
419 errx(1, "destination is not specified");
428 setifpsrc(addr, param)
432 param = 0; /*fool gcc*/
433 (*afp->af_getaddr)(addr, PSRC);
439 setifpdst(addr, param)
443 param = 0; /*fool gcc*/
444 (*afp->af_getaddr)(addr, PDST);
449 setifflags(vname, value)
453 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
454 Perror("ioctl (SIOCGIFFLAGS)");
457 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
458 flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
465 ifr.ifr_flags = flags & 0xffff;
466 ifr.ifr_flagshigh = flags >> 16;
467 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
471 #ifdef SIOCDIFPHYADDR
474 delifaddrs(vname, param)
478 param = 0; /* fool gcc */
479 vname = NULL; /* ditto */
481 if (ioctl(s, SIOCDIFPHYADDR, (caddr_t)&ifr) < 0)
482 err(1, "ioctl(SIOCDIFPHYADDR)");
487 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
488 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"
491 * Print the status of the interface. If an address family was
492 * specified, show it and it only; otherwise, show them all.
497 struct afswtch *p = NULL;
499 struct if_msghdr *myifm;
501 printf("%s: ", name);
502 printb("flags", flags, IFFBITS);
504 printf(" metric %d", metric);
506 printf(" mtu %d", mtu);
510 * XXX: Sigh. This is bad, I know. At this point, we may have
511 * *zero* RTM_NEWADDR's, so we have to "feel the water" before
512 * incrementing the loop. One day, I might feel inspired enough
513 * to get the top level loop to pass a count down here so we
514 * dont have to mess with this. -Peter
520 mynext = next + ifm->ifm_msglen;
525 myifm = (struct if_msghdr *)mynext;
527 if (myifm->ifm_type != RTM_NEWADDR)
532 ifm = (struct if_msghdr *)next;
534 ifam = (struct ifa_msghdr *)myifm;
535 info.rti_addrs = ifam->ifam_addrs;
537 /* Expand the compacted addresses */
538 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
542 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family &&
543 afp->af_status != ether_status) {
545 if (p->af_status != ether_status)
548 } else for (p = afs; p->af_name; p++) {
549 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family &&
550 p->af_status != ether_status)
554 if (afp == NULL || afp->af_status == ether_status)
556 else if (afp && !p) {
557 warnx("%s has no %s IFA address!", name, afp->af_name);
569 int flags = NI_NUMERICHOST;
571 #ifndef SIOCGLIFPHYADDR
572 u_long srccmd, dstcmd;
578 force = 0; /*fool gcc*/
580 psrcaddr[0] = pdstaddr[0] = '\0';
583 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
586 srccmd = SIOCGIFPSRCADDR;
587 dstcmd = SIOCGIFPDSTADDR;
590 srccmd = SIOCGIFPSRCADDR_IN6;
591 dstcmd = SIOCGIFPDSTADDR_IN6;
592 ifrp = (struct ifreq *)&in6_ifr;
596 srccmd = SIOCGIFPSRCADDR;
597 dstcmd = SIOCGIFPDSTADDR;
600 if (0 <= ioctl(s, srccmd, (caddr_t)ifrp)) {
602 if (ifrp->ifr_addr.sa_family == AF_INET6)
609 if (getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
610 psrcaddr, sizeof(psrcaddr), 0, 0, flags) != 0)
613 if (0 <= ioctl(s, dstcmd, (caddr_t)ifrp)) {
614 if (getnameinfo(&ifrp->ifr_addr, ifrp->ifr_addr.sa_len,
615 pdstaddr, sizeof(pdstaddr), 0, 0, flags) != 0)
618 printf("\tphysical address %s %s --> %s\n", af, psrcaddr, pdstaddr);
620 struct if_laddrreq iflr;
622 force = 0; /*fool gcc*/
624 psrcaddr[0] = pdstaddr[0] = '\0';
626 memset(&iflr, 0, sizeof(iflr));
627 memcpy(iflr.iflr_name, ifr.ifr_name, sizeof(iflr.iflr_name));
629 if (0 <= ioctl(s, SIOCGLIFPHYADDR, (caddr_t)&iflr)) {
630 switch (iflr.addr.ss_family) {
640 if (getnameinfo((struct sockaddr *)&iflr.addr, iflr.addr.ss_len,
641 psrcaddr, sizeof(psrcaddr), 0, 0, flags) != 0)
643 if (getnameinfo((struct sockaddr *)&iflr.dstaddr,
644 iflr.dstaddr.ss_len, pdstaddr, sizeof(pdstaddr), 0, 0,
648 printf("\tphysical address %s %s --> %s\n", af, psrcaddr, pdstaddr);
656 struct sockaddr_in *sin, null_sin;
661 memset(&null_sin, 0, sizeof(null_sin));
663 sin = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
664 if (!sin || sin->sin_family != AF_INET) {
667 /* warnx("%s has no AF_INET IFA address!", name); */
670 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
672 if (flags & IFF_POINTOPOINT) {
673 /* note RTAX_BRD overlap with IFF_BROADCAST */
674 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
677 printf("--> %s ", inet_ntoa(sin->sin_addr));
680 sin = (struct sockaddr_in *)info.rti_info[RTAX_NETMASK];
683 printf("netmask 0x%x ", (u_int32_t)ntohl(sin->sin_addr.s_addr));
685 if (flags & IFF_BROADCAST) {
686 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
687 sin = (struct sockaddr_in *)info.rti_info[RTAX_BRD];
688 if (sin && sin->sin_addr.s_addr != 0)
689 printf("broadcast %s", inet_ntoa(sin->sin_addr));
699 struct sockaddr_in6 *sin, null_sin;
700 char hostname[NI_MAXHOST];
701 int niflags = NI_NUMERICHOST;
703 memset(&null_sin, 0, sizeof(null_sin));
704 #ifdef NI_WITHSCOPEID
705 niflags |= NI_WITHSCOPEID;
708 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_IFA];
709 if (!sin || sin->sin6_family != AF_INET6) {
712 /* warnx("%s has no AF_INET6 IFA address!", name); */
716 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
718 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]);
719 sin->sin6_addr.s6_addr[2] = 0;
720 sin->sin6_addr.s6_addr[3] = 0;
723 getnameinfo((struct sockaddr *)sin, sin->sin6_len,
724 hostname, sizeof(hostname), 0, 0, niflags);
725 printf("\tinet6 %s ", hostname);
727 if (flags & IFF_POINTOPOINT) {
728 /* note RTAX_BRD overlap with IFF_BROADCAST */
729 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_BRD];
731 * some of ther interfaces do not have valid destination
734 if (sin->sin6_family == AF_INET6) {
736 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
738 ntohs(*(u_int16_t *)&sin->sin6_addr.s6_addr[2]);
739 sin->sin6_addr.s6_addr[2] = 0;
740 sin->sin6_addr.s6_addr[3] = 0;
743 getnameinfo((struct sockaddr *)sin, sin->sin6_len,
744 hostname, sizeof(hostname), 0, 0, niflags);
745 printf("--> %s ", hostname);
749 sin = (struct sockaddr_in6 *)info.rti_info[RTAX_NETMASK];
752 printf(" prefixlen %d ", prefix(&sin->sin6_addr,
753 sizeof(struct in6_addr)));
767 dummy = 0; /*fool gcc*/
769 cp = (char *)LLADDR(sdl);
770 if ((n = sdl->sdl_alen) > 0) {
771 if (sdl->sdl_type == IFT_ETHER)
774 printf ("\tlladdr ");
776 printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' ');
788 errx(1, "%s: no such interface", cmd);
792 errx(1, "%s: permission denied", cmd);
800 #define SIN(x) ((struct sockaddr_in *) &(x))
801 struct sockaddr_in *sintab[] = {
802 SIN(addreq.ifra_addr), SIN(addreq.ifra_dstaddr)};
809 register struct sockaddr_in *sin = sintab[which];
813 sin->sin_len = sizeof(*sin);
814 sin->sin_family = AF_INET;
816 if (inet_aton(s, &sin->sin_addr))
818 else if ((hp = gethostbyname(s)) != NULL)
819 bcopy(hp->h_addr, (char *)&sin->sin_addr, hp->h_length);
820 else if ((np = getnetbyname(s)) != NULL)
821 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
823 errx(1, "%s: bad value", s);
827 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
828 struct sockaddr_in6 *sin6tab[] = {
829 SIN6(in6_addreq.ifra_addr), SIN6(in6_addreq.ifra_dstaddr)};
832 in6_getaddr(s, which)
836 register struct sockaddr_in6 *sin = sin6tab[which];
838 sin->sin6_len = sizeof(*sin);
839 sin->sin6_family = AF_INET6;
841 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
842 errx(1, "%s: bad value", s);
846 in6_getprefix(plen, which)
850 register struct sockaddr_in6 *sin = sin6tab[which];
852 int len = atoi(plen);
854 if ((len < 0) || (len > 128))
855 errx(1, "%s: bad value", plen);
856 sin->sin6_len = sizeof(*sin);
857 sin->sin6_family = AF_INET6;
858 if ((len == 0) || (len == 128)) {
859 memset(&sin->sin6_addr, -1, sizeof(struct in6_addr));
862 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
864 *cp = (-1) << (8 - len);
869 * Print a value a la the %b format of the kernel's printf
874 register unsigned int v;
877 register int i, any = 0;
880 if (bits && *bits == 8)
881 printf("%s=%o", s, v & 0xffff);
883 printf("%s=%x", s, v & 0xffff);
887 while ((i = *bits++) != 0) {
888 if ((v & (1 << (i-1))) != 0) {
892 for (; (c = *bits) > 32; bits++)
895 for (; *bits > 32; bits++)
908 register u_char *name = (u_char *)val;
909 register int byte, bit, plen = 0;
911 for (byte = 0; byte < size; byte++, plen += 8)
912 if (name[byte] != 0xff)
916 for (bit = 7; bit != 0; bit--, plen++)
917 if (!(name[byte] & (1 << bit)))
919 for (; bit != 0; bit--)
920 if (name[byte] & (1 << bit))
923 for (; byte < size; byte++)