xref: /netbsd-src/usr.sbin/rtadvd/dump.c (revision 6dffe8d42bd46273f674d7ab834e7be9b1af990e)
1 /*	$NetBSD: dump.c,v 1.8 2008/12/29 04:01:21 christos 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 extern struct rainfo *ralist;
63 
64 static char *ether_str __P((struct sockaddr_dl *));
65 static void if_dump __P((void));
66 
67 static char *rtpref_str[] = {
68 	"medium",		/* 00 */
69 	"high",			/* 01 */
70 	"rsv",			/* 10 */
71 	"low"			/* 11 */
72 };
73 
74 static char *
75 ether_str(sdl)
76 	struct sockaddr_dl *sdl;
77 {
78 	static char hbuf[NI_MAXHOST];
79 
80 	if (sdl->sdl_alen) {
81 		if (getnameinfo((struct sockaddr *)sdl, sdl->sdl_len,
82 		    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
83 			snprintf(hbuf, sizeof(hbuf), "<invalid>");
84 	} else
85 		snprintf(hbuf, sizeof(hbuf), "NONE");
86 
87 	return(hbuf);
88 }
89 
90 static void
91 if_dump()
92 {
93 	struct rainfo *rai;
94 	struct prefix *pfx;
95 #ifdef ROUTEINFO
96 	struct rtinfo *rti;
97 #endif
98 	char prefixbuf[INET6_ADDRSTRLEN];
99 	int first;
100 	struct timeval now;
101 
102 	gettimeofday(&now, NULL); /* XXX: unused in most cases */
103 	for (rai = ralist; rai; rai = rai->next) {
104 		fprintf(fp, "%s:\n", rai->ifname);
105 
106 		fprintf(fp, "  Status: %s\n",
107 			(iflist[rai->ifindex]->ifm_flags & IFF_UP) ? "UP" :
108 			"DOWN");
109 
110 		/* control information */
111 		if (rai->lastsent.tv_sec) {
112 			/* note that ctime() appends CR by itself */
113 			fprintf(fp, "  Last RA sent: %s",
114 				ctime((time_t *)&rai->lastsent.tv_sec));
115 		}
116 		if (rai->timer) {
117 			fprintf(fp, "  Next RA will be sent: %s",
118 				ctime((time_t *)&rai->timer->tm.tv_sec));
119 		}
120 		else
121 			fprintf(fp, "  RA timer is stopped");
122 		fprintf(fp, "  waits: %d, initcount: %d\n",
123 			rai->waiting, rai->initcounter);
124 
125 		/* statistics */
126 		fprintf(fp, "  statistics: RA(out/in/inconsistent): "
127 		    "%llu/%llu/%llu, ",
128 		    (unsigned long long)rai->raoutput,
129 		    (unsigned long long)rai->rainput,
130 		    (unsigned long long)rai->rainconsistent);
131 		fprintf(fp, "RS(input): %llu\n",
132 		    (unsigned long long)rai->rsinput);
133 
134 		/* interface information */
135 		if (rai->advlinkopt)
136 			fprintf(fp, "  Link-layer address: %s\n",
137 			    ether_str(rai->sdl));
138 		fprintf(fp, "  MTU: %d\n", rai->phymtu);
139 
140 		/* Router configuration variables */
141 		fprintf(fp, "  DefaultLifetime: %d, MaxAdvInterval: %d, "
142 		    "MinAdvInterval: %d\n", rai->lifetime, rai->maxinterval,
143 		    rai->mininterval);
144 		fprintf(fp, "  Flags: %s%s%s, ",
145 		    rai->managedflg ? "M" : "", rai->otherflg ? "O" : "",
146 		    "");
147 		fprintf(fp, "Preference: %s, ",
148 			rtpref_str[(rai->rtpref >> 3) & 0xff]);
149 		fprintf(fp, "MTU: %d\n", rai->linkmtu);
150 		fprintf(fp, "  ReachableTime: %d, RetransTimer: %d, "
151 			"CurHopLimit: %d\n", rai->reachabletime,
152 			rai->retranstimer, rai->hoplimit);
153 		if (rai->clockskew)
154 			fprintf(fp, "  Clock skew: %ldsec\n",
155 			    rai->clockskew);
156 		for (first = 1, pfx = rai->prefix.next; pfx != &rai->prefix;
157 		     pfx = pfx->next) {
158 			if (first) {
159 				fprintf(fp, "  Prefixes:\n");
160 				first = 0;
161 			}
162 			fprintf(fp, "    %s/%d(",
163 			    inet_ntop(AF_INET6, &pfx->prefix, prefixbuf,
164 			    sizeof(prefixbuf)), pfx->prefixlen);
165 			switch (pfx->origin) {
166 			case PREFIX_FROM_KERNEL:
167 				fprintf(fp, "KERNEL, ");
168 				break;
169 			case PREFIX_FROM_CONFIG:
170 				fprintf(fp, "CONFIG, ");
171 				break;
172 			case PREFIX_FROM_DYNAMIC:
173 				fprintf(fp, "DYNAMIC, ");
174 				break;
175 			}
176 			if (pfx->validlifetime == ND6_INFINITE_LIFETIME)
177 				fprintf(fp, "vltime: infinity");
178 			else
179 				fprintf(fp, "vltime: %ld",
180 					(long)pfx->validlifetime);
181 			if (pfx->vltimeexpire != 0)
182 				fprintf(fp, "(decr,expire %lld), ", (long long)
183 					(pfx->vltimeexpire > now.tv_sec ?
184 					pfx->vltimeexpire - now.tv_sec : 0));
185 			else
186 				fprintf(fp, ", ");
187 			if (pfx->preflifetime ==  ND6_INFINITE_LIFETIME)
188 				fprintf(fp, "pltime: infinity");
189 			else
190 				fprintf(fp, "pltime: %ld",
191 					(long)pfx->preflifetime);
192 			if (pfx->pltimeexpire != 0)
193 				fprintf(fp, "(decr,expire %lld), ", (long long)
194 					(pfx->pltimeexpire > now.tv_sec ?
195 					pfx->pltimeexpire - now.tv_sec : 0));
196 			else
197 				fprintf(fp, ", ");
198 			fprintf(fp, "flags: %s%s%s",
199 				pfx->onlinkflg ? "L" : "",
200 				pfx->autoconfflg ? "A" : "",
201 				"");
202 			if (pfx->timer) {
203 				struct timeval *rest;
204 
205 				rest = rtadvd_timer_rest(pfx->timer);
206 				if (rest) { /* XXX: what if not? */
207 					fprintf(fp, ", expire in: %ld",
208 					    (long)rest->tv_sec);
209 				}
210 			}
211 			fprintf(fp, ")\n");
212 		}
213 #ifdef ROUTEINFO
214 		for (first = 1, rti = rai->route.next; rti != &rai->route;
215 		     rti = rti->next) {
216 			if (first) {
217 				fprintf(fp, "  Route Information:\n");
218 				first = 0;
219 			}
220 			fprintf(fp, "    %s/%d (",
221 				inet_ntop(AF_INET6, &rti->prefix,
222 					  prefixbuf, sizeof(prefixbuf)),
223 				rti->prefixlen);
224 			fprintf(fp, "preference: %s, ",
225 				rtpref_str[0xff & (rti->rtpref >> 3)]);
226 			if (rti->ltime == ND6_INFINITE_LIFETIME)
227 				fprintf(fp, "lifetime: infinity");
228 			else
229 				fprintf(fp, "lifetime: %ld", (long)rti->ltime);
230 			fprintf(fp, ")\n");
231 		}
232 #endif
233 	}
234 }
235 
236 void
237 rtadvd_dump_file(dumpfile)
238 	char *dumpfile;
239 {
240 	syslog(LOG_DEBUG, "<%s> dump current status to %s", __func__,
241 	    dumpfile);
242 
243 	if ((fp = fopen(dumpfile, "w")) == NULL) {
244 		syslog(LOG_WARNING, "<%s> open a dump file(%s)",
245 		       __func__, dumpfile);
246 		return;
247 	}
248 
249 	if_dump();
250 
251 	fclose(fp);
252 }
253