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