xref: /netbsd-src/usr.sbin/rtadvd/dump.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: dump.c,v 1.16 2018/04/20 10:39:37 roy 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 "logit.h"
61 #include "if.h"
62 #include "dump.h"
63 #include "prog_ops.h"
64 
65 static FILE *fp;
66 
67 static char *ether_str(struct sockaddr_dl *);
68 static void if_dump(void);
69 
70 static const char *rtpref_str[] = {
71 	"medium",		/* 00 */
72 	"high",			/* 01 */
73 	"rsv",			/* 10 */
74 	"low"			/* 11 */
75 };
76 
77 static char *
78 ether_str(struct sockaddr_dl *sdl)
79 {
80 	static char hbuf[NI_MAXHOST];
81 
82 	if (sdl->sdl_alen) {
83 		if (getnameinfo((struct sockaddr *)sdl, sdl->sdl_len,
84 		    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
85 			snprintf(hbuf, sizeof(hbuf), "<invalid>");
86 	} else
87 		snprintf(hbuf, sizeof(hbuf), "NONE");
88 
89 	return(hbuf);
90 }
91 
92 static void
93 if_dump(void)
94 {
95 	struct rainfo *rai;
96 	struct prefix *pfx;
97 	struct rtinfo *rti;
98 	struct rdnss *rdns;
99 	struct rdnss_addr *rdnsa;
100 	struct dnssl *dnsl;
101 	struct dnssl_domain *dnsd;
102 	char *p, len;
103 	char prefixbuf[INET6_ADDRSTRLEN];
104 	struct timespec now;
105 
106 	prog_clock_gettime(CLOCK_MONOTONIC, &now); /* XXX: unused in most cases */
107 	TAILQ_FOREACH(rai, &ralist, next) {
108 		fprintf(fp, "%s:\n", rai->ifname);
109 
110 		fprintf(fp, "  Status: %s\n",
111 			(rai->ifflags & IFF_UP) ? "UP" : "DOWN");
112 
113 		/* control information */
114 		if (rai->lastsent.tv_sec) {
115 			/* note that ctime() appends CR by itself */
116 			fprintf(fp, "  Last RA sent: %s",
117 				ctime(&rai->lastsent.tv_sec));
118 		}
119 		if (rai->timer) {
120 			fprintf(fp, "  Next RA will be sent: %s",
121 				ctime(&rai->timer->tm.tv_sec));
122 		}
123 		else
124 			fprintf(fp, "  RA timer is stopped");
125 		fprintf(fp, "  waits: %d, initcount: %d\n",
126 			rai->waiting, rai->initcounter);
127 
128 		/* statistics */
129 		fprintf(fp, "  statistics: RA(out/in/inconsistent): "
130 		    "%llu/%llu/%llu, ",
131 		    (unsigned long long)rai->raoutput,
132 		    (unsigned long long)rai->rainput,
133 		    (unsigned long long)rai->rainconsistent);
134 		fprintf(fp, "RS(input): %llu\n",
135 		    (unsigned long long)rai->rsinput);
136 
137 		/* interface information */
138 		if (rai->advlinkopt)
139 			fprintf(fp, "  Link-layer address: %s\n",
140 			    ether_str(rai->sdl));
141 		fprintf(fp, "  MTU: %d\n", rai->phymtu);
142 
143 		/* Router configuration variables */
144 		fprintf(fp, "  DefaultLifetime: %d, MaxAdvInterval: %d, "
145 		    "MinAdvInterval: %d\n", rai->lifetime, rai->maxinterval,
146 		    rai->mininterval);
147 		fprintf(fp, "  Flags: %s%s%s, ",
148 		    rai->managedflg ? "M" : "", rai->otherflg ? "O" : "",
149 		    "");
150 		fprintf(fp, "Preference: %s, ",
151 			rtpref_str[(rai->rtpref >> 3) & 0xff]);
152 		fprintf(fp, "MTU: %d\n", rai->linkmtu);
153 		fprintf(fp, "  ReachableTime: %d, RetransTimer: %d, "
154 			"CurHopLimit: %d\n", rai->reachabletime,
155 			rai->retranstimer, rai->hoplimit);
156 		if (rai->clockskew)
157 			fprintf(fp, "  Clock skew: %dsec\n",
158 			    rai->clockskew);
159 		TAILQ_FOREACH(pfx, &rai->prefix, next) {
160 			if (pfx == TAILQ_FIRST(&rai->prefix))
161 				fprintf(fp, "  Prefixes:\n");
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 timespec *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 
214 		TAILQ_FOREACH(rti, &rai->route, next) {
215 			if (rti == TAILQ_FIRST(&rai->route))
216 				fprintf(fp, "  Route Information:\n");
217 			fprintf(fp, "    %s/%d (",
218 				inet_ntop(AF_INET6, &rti->prefix,
219 					  prefixbuf, sizeof(prefixbuf)),
220 				rti->prefixlen);
221 			fprintf(fp, "preference: %s, ",
222 				rtpref_str[0xff & (rti->rtpref >> 3)]);
223 			if (rti->ltime == ND6_INFINITE_LIFETIME)
224 				fprintf(fp, "lifetime: infinity");
225 			else
226 				fprintf(fp, "lifetime: %ld", (long)rti->ltime);
227 			fprintf(fp, ")\n");
228 		}
229 
230 		TAILQ_FOREACH(rdns, &rai->rdnss, next) {
231 			fprintf(fp, "  Recursive DNS Servers:\n");
232 			if (rdns->lifetime == ND6_INFINITE_LIFETIME)
233 				fprintf(fp, "    lifetime: infinity\n");
234 			else
235 				fprintf(fp, "    lifetime: %ld\n",
236 				    (long)rdns->lifetime);
237 			TAILQ_FOREACH(rdnsa, &rdns->list, next)
238 				fprintf(fp, "    %s\n",
239 				    inet_ntop(AF_INET6, &rdnsa->addr,
240 				    prefixbuf, sizeof(prefixbuf)));
241 		}
242 
243 		TAILQ_FOREACH(dnsl, &rai->dnssl, next) {
244 			fprintf(fp, "  DNS Search List:\n");
245 			if (dnsl->lifetime == ND6_INFINITE_LIFETIME)
246 				fprintf(fp, "    lifetime: infinity\n");
247 			else
248 				fprintf(fp, "    lifetime: %ld\n",
249 				    (long)dnsl->lifetime);
250 			TAILQ_FOREACH(dnsd, &dnsl->list, next) {
251 				fprintf(fp, "    ");
252 				for (p = dnsd->domain, len = *p++;
253 				    len != 0;
254 				    len = *p++)
255 				{
256 					if (p != dnsd->domain)
257 					    fputc('.', fp);
258 					while(len-- != 0)
259 					    fputc(*p++, fp);
260 				}
261 				fputc('\n', fp);
262 			}
263 		}
264 	}
265 }
266 
267 void
268 rtadvd_dump_file(const char *dumpfile)
269 {
270 	logit(LOG_DEBUG, "<%s> dump current status to %s", __func__,
271 	    dumpfile);
272 
273 	if ((fp = fopen(dumpfile, "w")) == NULL) {
274 		logit(LOG_WARNING, "<%s> open a dump file(%s): %m",
275 		       __func__, dumpfile);
276 		return;
277 	}
278 
279 	if_dump();
280 
281 	fclose(fp);
282 }
283