Home | History | Annotate | Line # | Download | only in rtadvd
dump.c revision 1.4
      1  1.4  itojun /*	$NetBSD: dump.c,v 1.4 2002/05/21 14:29:52 itojun Exp $	*/
      2  1.3  itojun /*	$KAME: dump.c,v 1.15 2000/11/11 06:57:22 jinmei Exp $	*/
      3  1.1  itojun 
      4  1.1  itojun /*
      5  1.1  itojun  * Copyright (C) 2000 WIDE Project.
      6  1.1  itojun  * All rights reserved.
      7  1.1  itojun  *
      8  1.1  itojun  * Redistribution and use in source and binary forms, with or without
      9  1.1  itojun  * modification, are permitted provided that the following conditions
     10  1.1  itojun  * are met:
     11  1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     12  1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     13  1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  itojun  *    documentation and/or other materials provided with the distribution.
     16  1.1  itojun  * 3. Neither the name of the project nor the names of its contributors
     17  1.1  itojun  *    may be used to endorse or promote products derived from this software
     18  1.1  itojun  *    without specific prior written permission.
     19  1.1  itojun  *
     20  1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  1.1  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  1.1  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.1  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  1.1  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.1  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  1.1  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.1  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  1.1  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  1.1  itojun  * SUCH DAMAGE.
     31  1.1  itojun  */
     32  1.1  itojun #include <sys/types.h>
     33  1.1  itojun #include <sys/socket.h>
     34  1.3  itojun #include <sys/queue.h>
     35  1.1  itojun 
     36  1.1  itojun #include <net/if.h>
     37  1.1  itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
     38  1.1  itojun #include <net/if_var.h>
     39  1.1  itojun #endif /* __FreeBSD__ >= 3 */
     40  1.1  itojun #include <net/if_dl.h>
     41  1.1  itojun 
     42  1.1  itojun #include <netinet/in.h>
     43  1.1  itojun 
     44  1.1  itojun /* XXX: the following two are non-standard include files */
     45  1.1  itojun #include <netinet6/in6_var.h>
     46  1.1  itojun #include <netinet6/nd6.h>
     47  1.1  itojun 
     48  1.1  itojun #include <arpa/inet.h>
     49  1.1  itojun 
     50  1.1  itojun #include <time.h>
     51  1.1  itojun #include <stdio.h>
     52  1.1  itojun #include <stdarg.h>
     53  1.1  itojun #include <syslog.h>
     54  1.1  itojun #include <string.h>
     55  1.1  itojun #include <errno.h>
     56  1.1  itojun 
     57  1.1  itojun #include "rtadvd.h"
     58  1.1  itojun #include "timer.h"
     59  1.2  itojun #include "if.h"
     60  1.1  itojun #include "dump.h"
     61  1.1  itojun 
     62  1.1  itojun static FILE *fp;
     63  1.1  itojun 
     64  1.1  itojun extern struct rainfo *ralist;
     65  1.1  itojun 
     66  1.1  itojun static char *ether_str __P((struct sockaddr_dl *));
     67  1.1  itojun static void if_dump __P((void));
     68  1.1  itojun 
     69  1.1  itojun #ifdef __FreeBSD__		/* XXX: see PORTABILITY */
     70  1.1  itojun #define LONGLONG "%qu"
     71  1.1  itojun #else
     72  1.1  itojun #define LONGLONG "%llu"
     73  1.1  itojun #endif
     74  1.1  itojun 
     75  1.1  itojun static char *
     76  1.1  itojun ether_str(sdl)
     77  1.1  itojun 	struct sockaddr_dl *sdl;
     78  1.1  itojun {
     79  1.1  itojun 	static char ebuf[32];
     80  1.1  itojun 	u_char *cp;
     81  1.1  itojun 
     82  1.1  itojun 	if (sdl->sdl_alen && sdl->sdl_alen > 5) {
     83  1.1  itojun 		cp = (u_char *)LLADDR(sdl);
     84  1.1  itojun 		sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
     85  1.1  itojun 			cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
     86  1.1  itojun 	}
     87  1.1  itojun 	else {
     88  1.1  itojun 		sprintf(ebuf, "NONE");
     89  1.1  itojun 	}
     90  1.1  itojun 
     91  1.1  itojun 	return(ebuf);
     92  1.1  itojun }
     93  1.1  itojun 
     94  1.1  itojun static void
     95  1.1  itojun if_dump()
     96  1.1  itojun {
     97  1.1  itojun 	struct rainfo *rai;
     98  1.1  itojun 	struct prefix *pfx;
     99  1.1  itojun 	char prefixbuf[INET6_ADDRSTRLEN];
    100  1.1  itojun 	int first;
    101  1.3  itojun 	struct timeval now;
    102  1.1  itojun 
    103  1.3  itojun 	gettimeofday(&now, NULL); /* XXX: unused in most cases */
    104  1.1  itojun 	for (rai = ralist; rai; rai = rai->next) {
    105  1.1  itojun 		fprintf(fp, "%s:\n", rai->ifname);
    106  1.1  itojun 
    107  1.2  itojun 		fprintf(fp, "  Status: %s\n",
    108  1.2  itojun 			(iflist[rai->ifindex]->ifm_flags & IFF_UP) ? "UP" :
    109  1.2  itojun 			"DOWN");
    110  1.2  itojun 
    111  1.1  itojun 		/* control information */
    112  1.1  itojun 		if (rai->lastsent.tv_sec) {
    113  1.1  itojun 			/* note that ctime() appends CR by itself */
    114  1.1  itojun 			fprintf(fp, "  Last RA sent: %s",
    115  1.1  itojun 				ctime((time_t *)&rai->lastsent.tv_sec));
    116  1.1  itojun 		}
    117  1.1  itojun 		if (rai->timer) {
    118  1.1  itojun 			fprintf(fp, "  Next RA will be sent: %s",
    119  1.1  itojun 				ctime((time_t *)&rai->timer->tm.tv_sec));
    120  1.1  itojun 		}
    121  1.2  itojun 		else
    122  1.2  itojun 			fprintf(fp, "  RA timer is stopped");
    123  1.1  itojun 		fprintf(fp, "  waits: %d, initcount: %d\n",
    124  1.1  itojun 			rai->waiting, rai->initcounter);
    125  1.1  itojun 
    126  1.1  itojun 		/* statistics */
    127  1.1  itojun 		fprintf(fp,
    128  1.1  itojun 			"  statistics: RA(out/in/inconsistent): "
    129  1.1  itojun 			LONGLONG "/" LONGLONG "/" LONGLONG ", ",
    130  1.1  itojun 			(unsigned long long)rai->raoutput,
    131  1.1  itojun 			(unsigned long long)rai->rainput,
    132  1.1  itojun 			(unsigned long long)rai->rainconsistent);
    133  1.1  itojun 		fprintf(fp, "RS(input): " LONGLONG "\n",
    134  1.1  itojun 			(unsigned long long)rai->rsinput);
    135  1.1  itojun 
    136  1.1  itojun 		/* interface information */
    137  1.1  itojun 		if (rai->advlinkopt)
    138  1.1  itojun 			fprintf(fp, "  Link-layer address: %s\n",
    139  1.1  itojun 				ether_str(rai->sdl));
    140  1.1  itojun 		fprintf(fp, "  MTU: %d\n", rai->phymtu);
    141  1.1  itojun 
    142  1.1  itojun 		/* Router configuration variables */
    143  1.1  itojun 		fprintf(fp,
    144  1.1  itojun 			"  DefaultLifetime: %d, MaxAdvInterval: %d, "
    145  1.1  itojun 			"MinAdvInterval: %d\n",
    146  1.1  itojun 			rai->lifetime, rai->maxinterval, rai->mininterval);
    147  1.1  itojun 		fprintf(fp, "  Flags: %s%s%s MTU: %d\n",
    148  1.1  itojun 			rai->managedflg ? "M" : "", rai->otherflg ? "O" : "",
    149  1.1  itojun #ifdef MIP6
    150  1.1  itojun 			rai->haflg ? "H" :
    151  1.1  itojun #endif
    152  1.1  itojun 			"", rai->linkmtu);
    153  1.1  itojun 		fprintf(fp, "  ReachableTime: %d, RetransTimer: %d, "
    154  1.1  itojun 			"CurHopLimit: %d\n", rai->reachabletime,
    155  1.1  itojun 			rai->retranstimer, rai->hoplimit);
    156  1.1  itojun #ifdef MIP6
    157  1.1  itojun 		fprintf(fp, "  HAPreference: %d, HALifetime: %d\n",
    158  1.1  itojun 			rai->hapref, rai->hatime);
    159  1.1  itojun #endif
    160  1.1  itojun 
    161  1.3  itojun 		if (rai->clockskew)
    162  1.3  itojun 			fprintf(fp, "  Clock skew: %ldsec\n",
    163  1.3  itojun 				rai->clockskew);
    164  1.1  itojun 		for (first = 1, pfx = rai->prefix.next; pfx != &rai->prefix;
    165  1.1  itojun 		     pfx = pfx->next) {
    166  1.1  itojun 			if (first) {
    167  1.1  itojun 				fprintf(fp, "  Prefixes:\n");
    168  1.1  itojun 				first = 0;
    169  1.1  itojun 			}
    170  1.1  itojun 			fprintf(fp, "    %s/%d(",
    171  1.1  itojun 				inet_ntop(AF_INET6, &pfx->prefix,
    172  1.1  itojun 					  prefixbuf, sizeof(prefixbuf)),
    173  1.1  itojun 				pfx->prefixlen);
    174  1.4  itojun 			switch (pfx->origin) {
    175  1.1  itojun 			case PREFIX_FROM_KERNEL:
    176  1.1  itojun 				fprintf(fp, "KERNEL, ");
    177  1.1  itojun 				break;
    178  1.1  itojun 			case PREFIX_FROM_CONFIG:
    179  1.1  itojun 				fprintf(fp, "CONFIG, ");
    180  1.1  itojun 				break;
    181  1.1  itojun 			case PREFIX_FROM_DYNAMIC:
    182  1.1  itojun 				fprintf(fp, "DYNAMIC, ");
    183  1.1  itojun 				break;
    184  1.1  itojun 			}
    185  1.1  itojun 			if (pfx->validlifetime == ND6_INFINITE_LIFETIME)
    186  1.3  itojun 				fprintf(fp, "vltime: infinity");
    187  1.1  itojun 			else
    188  1.3  itojun 				fprintf(fp, "vltime: %ld",
    189  1.1  itojun 					(long)pfx->validlifetime);
    190  1.3  itojun 			if (pfx->vltimeexpire != 0)
    191  1.3  itojun 				fprintf(fp, "(decr,expire %ld), ", (long)
    192  1.3  itojun 					pfx->vltimeexpire > now.tv_sec ?
    193  1.3  itojun 					pfx->vltimeexpire - now.tv_sec : 0);
    194  1.3  itojun 			else
    195  1.3  itojun 				fprintf(fp, ", ");
    196  1.1  itojun 			if (pfx->preflifetime ==  ND6_INFINITE_LIFETIME)
    197  1.3  itojun 				fprintf(fp, "pltime: infinity");
    198  1.1  itojun 			else
    199  1.3  itojun 				fprintf(fp, "pltime: %ld",
    200  1.1  itojun 					(long)pfx->preflifetime);
    201  1.3  itojun 			if (pfx->pltimeexpire != 0)
    202  1.3  itojun 				fprintf(fp, "(decr,expire %ld), ", (long)
    203  1.3  itojun 					pfx->pltimeexpire > now.tv_sec ?
    204  1.3  itojun 					pfx->pltimeexpire - now.tv_sec : 0);
    205  1.3  itojun 			else
    206  1.3  itojun 				fprintf(fp, ", ");
    207  1.1  itojun 			fprintf(fp, "flags: %s%s%s",
    208  1.1  itojun 				pfx->onlinkflg ? "L" : "",
    209  1.1  itojun 				pfx->autoconfflg ? "A" : "",
    210  1.1  itojun #ifdef MIP6
    211  1.1  itojun 				pfx->routeraddr ? "R" :
    212  1.1  itojun #endif
    213  1.1  itojun 				"");
    214  1.1  itojun 			fprintf(fp, ")\n");
    215  1.1  itojun 		}
    216  1.1  itojun 	}
    217  1.1  itojun }
    218  1.1  itojun 
    219  1.1  itojun void
    220  1.1  itojun rtadvd_dump_file(dumpfile)
    221  1.1  itojun 	char *dumpfile;
    222  1.1  itojun {
    223  1.1  itojun 	if ((fp = fopen(dumpfile, "w")) == NULL) {
    224  1.1  itojun 		syslog(LOG_WARNING, "<%s> open a dump file(%s)",
    225  1.1  itojun 		       __FUNCTION__, dumpfile);
    226  1.1  itojun 		return;
    227  1.1  itojun 	}
    228  1.1  itojun 
    229  1.1  itojun 	if_dump();
    230  1.1  itojun 
    231  1.1  itojun 	fclose(fp);
    232  1.1  itojun }
    233