Home | History | Annotate | Line # | Download | only in rtadvd
dump.c revision 1.8.8.1
      1 /*	$NetBSD: dump.c,v 1.8.8.1 2012/04/17 00:09:53 yamt Exp $	*/
      2 /*	$KAME: dump.c,v 1.34 2004/06/14 05:35:59 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 2000 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     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 project 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.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT 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 PROJECT 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
     30  * SUCH DAMAGE.
     31  */
     32 #include <sys/types.h>
     33 #include <sys/socket.h>
     34 #include <sys/queue.h>
     35 
     36 #include <net/if.h>
     37 #include <net/if_dl.h>
     38 
     39 #include <netinet/in.h>
     40 
     41 /* XXX: the following two are non-standard include files */
     42 #include <netinet6/in6_var.h>
     43 #include <netinet6/nd6.h>
     44 
     45 #include <arpa/inet.h>
     46 
     47 #include <time.h>
     48 #include <stdio.h>
     49 #include <stdarg.h>
     50 #include <syslog.h>
     51 #include <string.h>
     52 #include <errno.h>
     53 #include <netdb.h>
     54 
     55 #include "rtadvd.h"
     56 #include "timer.h"
     57 #include "if.h"
     58 #include "dump.h"
     59 
     60 static FILE *fp;
     61 
     62 static char *ether_str(struct sockaddr_dl *);
     63 static void if_dump(void);
     64 
     65 static const char *rtpref_str[] = {
     66 	"medium",		/* 00 */
     67 	"high",			/* 01 */
     68 	"rsv",			/* 10 */
     69 	"low"			/* 11 */
     70 };
     71 
     72 static char *
     73 ether_str(struct sockaddr_dl *sdl)
     74 {
     75 	static char hbuf[NI_MAXHOST];
     76 
     77 	if (sdl->sdl_alen) {
     78 		if (getnameinfo((struct sockaddr *)sdl, sdl->sdl_len,
     79 		    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
     80 			snprintf(hbuf, sizeof(hbuf), "<invalid>");
     81 	} else
     82 		snprintf(hbuf, sizeof(hbuf), "NONE");
     83 
     84 	return(hbuf);
     85 }
     86 
     87 static void
     88 if_dump(void)
     89 {
     90 	struct rainfo *rai;
     91 	struct prefix *pfx;
     92 	struct rtinfo *rti;
     93 	struct rdnss *rdns;
     94 	struct rdnss_addr *rdnsa;
     95 	struct dnssl *dnsl;
     96 	struct dnssl_domain *dnsd;
     97 	char *p, len;
     98 	char prefixbuf[INET6_ADDRSTRLEN];
     99 	struct timeval now;
    100 
    101 	gettimeofday(&now, NULL); /* XXX: unused in most cases */
    102 	TAILQ_FOREACH(rai, &ralist, next) {
    103 		fprintf(fp, "%s:\n", rai->ifname);
    104 
    105 		fprintf(fp, "  Status: %s\n",
    106 			(iflist[rai->ifindex]->ifm_flags & IFF_UP) ? "UP" :
    107 			"DOWN");
    108 
    109 		/* control information */
    110 		if (rai->lastsent.tv_sec) {
    111 			/* note that ctime() appends CR by itself */
    112 			fprintf(fp, "  Last RA sent: %s",
    113 				ctime((time_t *)&rai->lastsent.tv_sec));
    114 		}
    115 		if (rai->timer) {
    116 			fprintf(fp, "  Next RA will be sent: %s",
    117 				ctime((time_t *)&rai->timer->tm.tv_sec));
    118 		}
    119 		else
    120 			fprintf(fp, "  RA timer is stopped");
    121 		fprintf(fp, "  waits: %d, initcount: %d\n",
    122 			rai->waiting, rai->initcounter);
    123 
    124 		/* statistics */
    125 		fprintf(fp, "  statistics: RA(out/in/inconsistent): "
    126 		    "%llu/%llu/%llu, ",
    127 		    (unsigned long long)rai->raoutput,
    128 		    (unsigned long long)rai->rainput,
    129 		    (unsigned long long)rai->rainconsistent);
    130 		fprintf(fp, "RS(input): %llu\n",
    131 		    (unsigned long long)rai->rsinput);
    132 
    133 		/* interface information */
    134 		if (rai->advlinkopt)
    135 			fprintf(fp, "  Link-layer address: %s\n",
    136 			    ether_str(rai->sdl));
    137 		fprintf(fp, "  MTU: %d\n", rai->phymtu);
    138 
    139 		/* Router configuration variables */
    140 		fprintf(fp, "  DefaultLifetime: %d, MaxAdvInterval: %d, "
    141 		    "MinAdvInterval: %d\n", rai->lifetime, rai->maxinterval,
    142 		    rai->mininterval);
    143 		fprintf(fp, "  Flags: %s%s%s, ",
    144 		    rai->managedflg ? "M" : "", rai->otherflg ? "O" : "",
    145 		    "");
    146 		fprintf(fp, "Preference: %s, ",
    147 			rtpref_str[(rai->rtpref >> 3) & 0xff]);
    148 		fprintf(fp, "MTU: %d\n", rai->linkmtu);
    149 		fprintf(fp, "  ReachableTime: %d, RetransTimer: %d, "
    150 			"CurHopLimit: %d\n", rai->reachabletime,
    151 			rai->retranstimer, rai->hoplimit);
    152 		if (rai->clockskew)
    153 			fprintf(fp, "  Clock skew: %dsec\n",
    154 			    rai->clockskew);
    155 		TAILQ_FOREACH(pfx, &rai->prefix, next) {
    156 			if (pfx == TAILQ_FIRST(&rai->prefix))
    157 				fprintf(fp, "  Prefixes:\n");
    158 			fprintf(fp, "    %s/%d(",
    159 			    inet_ntop(AF_INET6, &pfx->prefix, prefixbuf,
    160 			    sizeof(prefixbuf)), pfx->prefixlen);
    161 			switch (pfx->origin) {
    162 			case PREFIX_FROM_KERNEL:
    163 				fprintf(fp, "KERNEL, ");
    164 				break;
    165 			case PREFIX_FROM_CONFIG:
    166 				fprintf(fp, "CONFIG, ");
    167 				break;
    168 			case PREFIX_FROM_DYNAMIC:
    169 				fprintf(fp, "DYNAMIC, ");
    170 				break;
    171 			}
    172 			if (pfx->validlifetime == ND6_INFINITE_LIFETIME)
    173 				fprintf(fp, "vltime: infinity");
    174 			else
    175 				fprintf(fp, "vltime: %ld",
    176 					(long)pfx->validlifetime);
    177 			if (pfx->vltimeexpire != 0)
    178 				fprintf(fp, "(decr,expire %lld), ", (long long)
    179 					(pfx->vltimeexpire > now.tv_sec ?
    180 					pfx->vltimeexpire - now.tv_sec : 0));
    181 			else
    182 				fprintf(fp, ", ");
    183 			if (pfx->preflifetime ==  ND6_INFINITE_LIFETIME)
    184 				fprintf(fp, "pltime: infinity");
    185 			else
    186 				fprintf(fp, "pltime: %ld",
    187 					(long)pfx->preflifetime);
    188 			if (pfx->pltimeexpire != 0)
    189 				fprintf(fp, "(decr,expire %lld), ", (long long)
    190 					(pfx->pltimeexpire > now.tv_sec ?
    191 					pfx->pltimeexpire - now.tv_sec : 0));
    192 			else
    193 				fprintf(fp, ", ");
    194 			fprintf(fp, "flags: %s%s%s",
    195 				pfx->onlinkflg ? "L" : "",
    196 				pfx->autoconfflg ? "A" : "",
    197 				"");
    198 			if (pfx->timer) {
    199 				struct timeval *rest;
    200 
    201 				rest = rtadvd_timer_rest(pfx->timer);
    202 				if (rest) { /* XXX: what if not? */
    203 					fprintf(fp, ", expire in: %ld",
    204 					    (long)rest->tv_sec);
    205 				}
    206 			}
    207 			fprintf(fp, ")\n");
    208 		}
    209 
    210 		TAILQ_FOREACH(rti, &rai->route, next) {
    211 			if (rti == TAILQ_FIRST(&rai->route))
    212 				fprintf(fp, "  Route Information:\n");
    213 			fprintf(fp, "    %s/%d (",
    214 				inet_ntop(AF_INET6, &rti->prefix,
    215 					  prefixbuf, sizeof(prefixbuf)),
    216 				rti->prefixlen);
    217 			fprintf(fp, "preference: %s, ",
    218 				rtpref_str[0xff & (rti->rtpref >> 3)]);
    219 			if (rti->ltime == ND6_INFINITE_LIFETIME)
    220 				fprintf(fp, "lifetime: infinity");
    221 			else
    222 				fprintf(fp, "lifetime: %ld", (long)rti->ltime);
    223 			fprintf(fp, ")\n");
    224 		}
    225 
    226 		TAILQ_FOREACH(rdns, &rai->rdnss, next) {
    227 			fprintf(fp, "  Recursive DNS Servers:\n");
    228 			if (rdns->lifetime == ND6_INFINITE_LIFETIME)
    229 				fprintf(fp, "    lifetime: infinity\n");
    230 			else
    231 				fprintf(fp, "    lifetime: %ld\n",
    232 				    (long)rdns->lifetime);
    233 			TAILQ_FOREACH(rdnsa, &rdns->list, next)
    234 				fprintf(fp, "    %s\n",
    235 				    inet_ntop(AF_INET6, &rdnsa->addr,
    236 				    prefixbuf, sizeof(prefixbuf)));
    237 		}
    238 
    239 		TAILQ_FOREACH(dnsl, &rai->dnssl, next) {
    240 			fprintf(fp, "  DNS Search List:\n");
    241 			if (dnsl->lifetime == ND6_INFINITE_LIFETIME)
    242 				fprintf(fp, "    lifetime: infinity\n");
    243 			else
    244 				fprintf(fp, "    lifetime: %ld\n",
    245 				    (long)dnsl->lifetime);
    246 			TAILQ_FOREACH(dnsd, &dnsl->list, next) {
    247 				fprintf(fp, "    ");
    248 				for (p = dnsd->domain, len = *p++;
    249 				    len != 0;
    250 				    len = *p++)
    251 				{
    252 					if (p != dnsd->domain)
    253 					    fputc('.', fp);
    254 					while(len-- != 0)
    255 					    fputc(*p++, fp);
    256 				}
    257 				fputc('\n', fp);
    258 			}
    259 		}
    260 	}
    261 }
    262 
    263 void
    264 rtadvd_dump_file(const char *dumpfile)
    265 {
    266 	syslog(LOG_DEBUG, "<%s> dump current status to %s", __func__,
    267 	    dumpfile);
    268 
    269 	if ((fp = fopen(dumpfile, "w")) == NULL) {
    270 		syslog(LOG_WARNING, "<%s> open a dump file(%s)",
    271 		       __func__, dumpfile);
    272 		return;
    273 	}
    274 
    275 	if_dump();
    276 
    277 	fclose(fp);
    278 }
    279