Home | History | Annotate | Line # | Download | only in rtadvd
dump.c revision 1.8.8.2
      1  1.8.8.2      yamt /*	$NetBSD: dump.c,v 1.8.8.2 2013/01/16 05:34:12 yamt Exp $	*/
      2      1.7    rpaulo /*	$KAME: dump.c,v 1.34 2004/06/14 05:35:59 itojun 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 #include <net/if_dl.h>
     38  1.8.8.2      yamt #ifdef __FreeBSD__
     39  1.8.8.2      yamt #include <net/if_var.h>
     40  1.8.8.2      yamt #endif
     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.5    itojun #include <netdb.h>
     57      1.1    itojun 
     58      1.1    itojun #include "rtadvd.h"
     59      1.1    itojun #include "timer.h"
     60      1.2    itojun #include "if.h"
     61      1.1    itojun #include "dump.h"
     62      1.1    itojun 
     63      1.1    itojun static FILE *fp;
     64      1.1    itojun 
     65  1.8.8.1      yamt static char *ether_str(struct sockaddr_dl *);
     66  1.8.8.1      yamt static void if_dump(void);
     67      1.1    itojun 
     68  1.8.8.1      yamt static const char *rtpref_str[] = {
     69      1.5    itojun 	"medium",		/* 00 */
     70      1.5    itojun 	"high",			/* 01 */
     71      1.5    itojun 	"rsv",			/* 10 */
     72      1.5    itojun 	"low"			/* 11 */
     73      1.5    itojun };
     74      1.1    itojun 
     75      1.1    itojun static char *
     76  1.8.8.1      yamt ether_str(struct sockaddr_dl *sdl)
     77      1.1    itojun {
     78      1.5    itojun 	static char hbuf[NI_MAXHOST];
     79      1.1    itojun 
     80      1.5    itojun 	if (sdl->sdl_alen) {
     81      1.5    itojun 		if (getnameinfo((struct sockaddr *)sdl, sdl->sdl_len,
     82      1.5    itojun 		    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
     83      1.5    itojun 			snprintf(hbuf, sizeof(hbuf), "<invalid>");
     84      1.5    itojun 	} else
     85      1.5    itojun 		snprintf(hbuf, sizeof(hbuf), "NONE");
     86      1.1    itojun 
     87      1.5    itojun 	return(hbuf);
     88      1.1    itojun }
     89      1.1    itojun 
     90      1.1    itojun static void
     91  1.8.8.1      yamt if_dump(void)
     92      1.1    itojun {
     93      1.1    itojun 	struct rainfo *rai;
     94      1.1    itojun 	struct prefix *pfx;
     95      1.7    rpaulo 	struct rtinfo *rti;
     96  1.8.8.1      yamt 	struct rdnss *rdns;
     97  1.8.8.1      yamt 	struct rdnss_addr *rdnsa;
     98  1.8.8.1      yamt 	struct dnssl *dnsl;
     99  1.8.8.1      yamt 	struct dnssl_domain *dnsd;
    100  1.8.8.1      yamt 	char *p, len;
    101      1.1    itojun 	char prefixbuf[INET6_ADDRSTRLEN];
    102      1.3    itojun 	struct timeval now;
    103      1.1    itojun 
    104      1.3    itojun 	gettimeofday(&now, NULL); /* XXX: unused in most cases */
    105  1.8.8.1      yamt 	TAILQ_FOREACH(rai, &ralist, next) {
    106      1.1    itojun 		fprintf(fp, "%s:\n", rai->ifname);
    107      1.1    itojun 
    108      1.2    itojun 		fprintf(fp, "  Status: %s\n",
    109  1.8.8.2      yamt 			(rai->ifflags & IFF_UP) ? "UP" : "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.5    itojun 		fprintf(fp, "  statistics: RA(out/in/inconsistent): "
    128      1.5    itojun 		    "%llu/%llu/%llu, ",
    129      1.5    itojun 		    (unsigned long long)rai->raoutput,
    130      1.5    itojun 		    (unsigned long long)rai->rainput,
    131      1.5    itojun 		    (unsigned long long)rai->rainconsistent);
    132      1.5    itojun 		fprintf(fp, "RS(input): %llu\n",
    133      1.5    itojun 		    (unsigned long long)rai->rsinput);
    134      1.1    itojun 
    135      1.1    itojun 		/* interface information */
    136      1.1    itojun 		if (rai->advlinkopt)
    137      1.1    itojun 			fprintf(fp, "  Link-layer address: %s\n",
    138      1.5    itojun 			    ether_str(rai->sdl));
    139      1.1    itojun 		fprintf(fp, "  MTU: %d\n", rai->phymtu);
    140      1.1    itojun 
    141      1.1    itojun 		/* Router configuration variables */
    142      1.5    itojun 		fprintf(fp, "  DefaultLifetime: %d, MaxAdvInterval: %d, "
    143      1.5    itojun 		    "MinAdvInterval: %d\n", rai->lifetime, rai->maxinterval,
    144      1.5    itojun 		    rai->mininterval);
    145      1.5    itojun 		fprintf(fp, "  Flags: %s%s%s, ",
    146      1.5    itojun 		    rai->managedflg ? "M" : "", rai->otherflg ? "O" : "",
    147      1.5    itojun 		    "");
    148      1.5    itojun 		fprintf(fp, "Preference: %s, ",
    149      1.5    itojun 			rtpref_str[(rai->rtpref >> 3) & 0xff]);
    150      1.5    itojun 		fprintf(fp, "MTU: %d\n", rai->linkmtu);
    151      1.1    itojun 		fprintf(fp, "  ReachableTime: %d, RetransTimer: %d, "
    152      1.1    itojun 			"CurHopLimit: %d\n", rai->reachabletime,
    153      1.1    itojun 			rai->retranstimer, rai->hoplimit);
    154      1.3    itojun 		if (rai->clockskew)
    155  1.8.8.1      yamt 			fprintf(fp, "  Clock skew: %dsec\n",
    156      1.5    itojun 			    rai->clockskew);
    157  1.8.8.1      yamt 		TAILQ_FOREACH(pfx, &rai->prefix, next) {
    158  1.8.8.1      yamt 			if (pfx == TAILQ_FIRST(&rai->prefix))
    159      1.1    itojun 				fprintf(fp, "  Prefixes:\n");
    160      1.1    itojun 			fprintf(fp, "    %s/%d(",
    161      1.5    itojun 			    inet_ntop(AF_INET6, &pfx->prefix, prefixbuf,
    162      1.5    itojun 			    sizeof(prefixbuf)), pfx->prefixlen);
    163      1.4    itojun 			switch (pfx->origin) {
    164      1.1    itojun 			case PREFIX_FROM_KERNEL:
    165      1.1    itojun 				fprintf(fp, "KERNEL, ");
    166      1.1    itojun 				break;
    167      1.1    itojun 			case PREFIX_FROM_CONFIG:
    168      1.1    itojun 				fprintf(fp, "CONFIG, ");
    169      1.1    itojun 				break;
    170      1.1    itojun 			case PREFIX_FROM_DYNAMIC:
    171      1.1    itojun 				fprintf(fp, "DYNAMIC, ");
    172      1.1    itojun 				break;
    173      1.1    itojun 			}
    174      1.1    itojun 			if (pfx->validlifetime == ND6_INFINITE_LIFETIME)
    175      1.3    itojun 				fprintf(fp, "vltime: infinity");
    176      1.1    itojun 			else
    177      1.3    itojun 				fprintf(fp, "vltime: %ld",
    178      1.1    itojun 					(long)pfx->validlifetime);
    179      1.3    itojun 			if (pfx->vltimeexpire != 0)
    180      1.8  christos 				fprintf(fp, "(decr,expire %lld), ", (long long)
    181      1.8  christos 					(pfx->vltimeexpire > now.tv_sec ?
    182      1.8  christos 					pfx->vltimeexpire - now.tv_sec : 0));
    183      1.3    itojun 			else
    184      1.3    itojun 				fprintf(fp, ", ");
    185      1.1    itojun 			if (pfx->preflifetime ==  ND6_INFINITE_LIFETIME)
    186      1.3    itojun 				fprintf(fp, "pltime: infinity");
    187      1.1    itojun 			else
    188      1.3    itojun 				fprintf(fp, "pltime: %ld",
    189      1.1    itojun 					(long)pfx->preflifetime);
    190      1.3    itojun 			if (pfx->pltimeexpire != 0)
    191      1.8  christos 				fprintf(fp, "(decr,expire %lld), ", (long long)
    192      1.8  christos 					(pfx->pltimeexpire > now.tv_sec ?
    193      1.8  christos 					pfx->pltimeexpire - now.tv_sec : 0));
    194      1.3    itojun 			else
    195      1.3    itojun 				fprintf(fp, ", ");
    196      1.1    itojun 			fprintf(fp, "flags: %s%s%s",
    197      1.1    itojun 				pfx->onlinkflg ? "L" : "",
    198      1.1    itojun 				pfx->autoconfflg ? "A" : "",
    199      1.1    itojun 				"");
    200      1.7    rpaulo 			if (pfx->timer) {
    201      1.7    rpaulo 				struct timeval *rest;
    202      1.7    rpaulo 
    203      1.7    rpaulo 				rest = rtadvd_timer_rest(pfx->timer);
    204      1.7    rpaulo 				if (rest) { /* XXX: what if not? */
    205      1.7    rpaulo 					fprintf(fp, ", expire in: %ld",
    206      1.7    rpaulo 					    (long)rest->tv_sec);
    207      1.7    rpaulo 				}
    208      1.7    rpaulo 			}
    209      1.1    itojun 			fprintf(fp, ")\n");
    210      1.1    itojun 		}
    211  1.8.8.1      yamt 
    212  1.8.8.1      yamt 		TAILQ_FOREACH(rti, &rai->route, next) {
    213  1.8.8.1      yamt 			if (rti == TAILQ_FIRST(&rai->route))
    214      1.7    rpaulo 				fprintf(fp, "  Route Information:\n");
    215      1.7    rpaulo 			fprintf(fp, "    %s/%d (",
    216      1.7    rpaulo 				inet_ntop(AF_INET6, &rti->prefix,
    217      1.7    rpaulo 					  prefixbuf, sizeof(prefixbuf)),
    218      1.7    rpaulo 				rti->prefixlen);
    219      1.7    rpaulo 			fprintf(fp, "preference: %s, ",
    220      1.7    rpaulo 				rtpref_str[0xff & (rti->rtpref >> 3)]);
    221      1.7    rpaulo 			if (rti->ltime == ND6_INFINITE_LIFETIME)
    222      1.7    rpaulo 				fprintf(fp, "lifetime: infinity");
    223      1.7    rpaulo 			else
    224      1.7    rpaulo 				fprintf(fp, "lifetime: %ld", (long)rti->ltime);
    225      1.7    rpaulo 			fprintf(fp, ")\n");
    226      1.7    rpaulo 		}
    227  1.8.8.1      yamt 
    228  1.8.8.1      yamt 		TAILQ_FOREACH(rdns, &rai->rdnss, next) {
    229  1.8.8.1      yamt 			fprintf(fp, "  Recursive DNS Servers:\n");
    230  1.8.8.1      yamt 			if (rdns->lifetime == ND6_INFINITE_LIFETIME)
    231  1.8.8.1      yamt 				fprintf(fp, "    lifetime: infinity\n");
    232  1.8.8.1      yamt 			else
    233  1.8.8.1      yamt 				fprintf(fp, "    lifetime: %ld\n",
    234  1.8.8.1      yamt 				    (long)rdns->lifetime);
    235  1.8.8.1      yamt 			TAILQ_FOREACH(rdnsa, &rdns->list, next)
    236  1.8.8.1      yamt 				fprintf(fp, "    %s\n",
    237  1.8.8.1      yamt 				    inet_ntop(AF_INET6, &rdnsa->addr,
    238  1.8.8.1      yamt 				    prefixbuf, sizeof(prefixbuf)));
    239  1.8.8.1      yamt 		}
    240  1.8.8.1      yamt 
    241  1.8.8.1      yamt 		TAILQ_FOREACH(dnsl, &rai->dnssl, next) {
    242  1.8.8.1      yamt 			fprintf(fp, "  DNS Search List:\n");
    243  1.8.8.1      yamt 			if (dnsl->lifetime == ND6_INFINITE_LIFETIME)
    244  1.8.8.1      yamt 				fprintf(fp, "    lifetime: infinity\n");
    245  1.8.8.1      yamt 			else
    246  1.8.8.1      yamt 				fprintf(fp, "    lifetime: %ld\n",
    247  1.8.8.1      yamt 				    (long)dnsl->lifetime);
    248  1.8.8.1      yamt 			TAILQ_FOREACH(dnsd, &dnsl->list, next) {
    249  1.8.8.1      yamt 				fprintf(fp, "    ");
    250  1.8.8.1      yamt 				for (p = dnsd->domain, len = *p++;
    251  1.8.8.1      yamt 				    len != 0;
    252  1.8.8.1      yamt 				    len = *p++)
    253  1.8.8.1      yamt 				{
    254  1.8.8.1      yamt 					if (p != dnsd->domain)
    255  1.8.8.1      yamt 					    fputc('.', fp);
    256  1.8.8.1      yamt 					while(len-- != 0)
    257  1.8.8.1      yamt 					    fputc(*p++, fp);
    258  1.8.8.1      yamt 				}
    259  1.8.8.1      yamt 				fputc('\n', fp);
    260  1.8.8.1      yamt 			}
    261  1.8.8.1      yamt 		}
    262      1.1    itojun 	}
    263      1.1    itojun }
    264      1.1    itojun 
    265      1.1    itojun void
    266  1.8.8.1      yamt rtadvd_dump_file(const char *dumpfile)
    267      1.1    itojun {
    268      1.7    rpaulo 	syslog(LOG_DEBUG, "<%s> dump current status to %s", __func__,
    269      1.7    rpaulo 	    dumpfile);
    270      1.7    rpaulo 
    271      1.1    itojun 	if ((fp = fopen(dumpfile, "w")) == NULL) {
    272      1.1    itojun 		syslog(LOG_WARNING, "<%s> open a dump file(%s)",
    273      1.6    itojun 		       __func__, dumpfile);
    274      1.1    itojun 		return;
    275      1.1    itojun 	}
    276      1.1    itojun 
    277      1.1    itojun 	if_dump();
    278      1.1    itojun 
    279      1.1    itojun 	fclose(fp);
    280      1.1    itojun }
    281