xref: /netbsd/usr.sbin/rtadvd/dump.c (revision c4a72b64)
1 /*	$NetBSD: dump.c,v 1.6 2002/07/10 21:11:43 itojun Exp $	*/
2 /*	$KAME: dump.c,v 1.28 2002/05/29 14:25:01 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 	char prefixbuf[INET6_ADDRSTRLEN];
96 	int first;
97 	struct timeval now;
98 
99 	gettimeofday(&now, NULL); /* XXX: unused in most cases */
100 	for (rai = ralist; rai; rai = rai->next) {
101 		fprintf(fp, "%s:\n", rai->ifname);
102 
103 		fprintf(fp, "  Status: %s\n",
104 			(iflist[rai->ifindex]->ifm_flags & IFF_UP) ? "UP" :
105 			"DOWN");
106 
107 		/* control information */
108 		if (rai->lastsent.tv_sec) {
109 			/* note that ctime() appends CR by itself */
110 			fprintf(fp, "  Last RA sent: %s",
111 				ctime((time_t *)&rai->lastsent.tv_sec));
112 		}
113 		if (rai->timer) {
114 			fprintf(fp, "  Next RA will be sent: %s",
115 				ctime((time_t *)&rai->timer->tm.tv_sec));
116 		}
117 		else
118 			fprintf(fp, "  RA timer is stopped");
119 		fprintf(fp, "  waits: %d, initcount: %d\n",
120 			rai->waiting, rai->initcounter);
121 
122 		/* statistics */
123 		fprintf(fp, "  statistics: RA(out/in/inconsistent): "
124 		    "%llu/%llu/%llu, ",
125 		    (unsigned long long)rai->raoutput,
126 		    (unsigned long long)rai->rainput,
127 		    (unsigned long long)rai->rainconsistent);
128 		fprintf(fp, "RS(input): %llu\n",
129 		    (unsigned long long)rai->rsinput);
130 
131 		/* interface information */
132 		if (rai->advlinkopt)
133 			fprintf(fp, "  Link-layer address: %s\n",
134 			    ether_str(rai->sdl));
135 		fprintf(fp, "  MTU: %d\n", rai->phymtu);
136 
137 		/* Router configuration variables */
138 		fprintf(fp, "  DefaultLifetime: %d, MaxAdvInterval: %d, "
139 		    "MinAdvInterval: %d\n", rai->lifetime, rai->maxinterval,
140 		    rai->mininterval);
141 		fprintf(fp, "  Flags: %s%s%s, ",
142 		    rai->managedflg ? "M" : "", rai->otherflg ? "O" : "",
143 		    "");
144 		fprintf(fp, "Preference: %s, ",
145 			rtpref_str[(rai->rtpref >> 3) & 0xff]);
146 		fprintf(fp, "MTU: %d\n", rai->linkmtu);
147 		fprintf(fp, "  ReachableTime: %d, RetransTimer: %d, "
148 			"CurHopLimit: %d\n", rai->reachabletime,
149 			rai->retranstimer, rai->hoplimit);
150 		if (rai->clockskew)
151 			fprintf(fp, "  Clock skew: %ldsec\n",
152 			    rai->clockskew);
153 		for (first = 1, pfx = rai->prefix.next; pfx != &rai->prefix;
154 		     pfx = pfx->next) {
155 			if (first) {
156 				fprintf(fp, "  Prefixes:\n");
157 				first = 0;
158 			}
159 			fprintf(fp, "    %s/%d(",
160 			    inet_ntop(AF_INET6, &pfx->prefix, prefixbuf,
161 			    sizeof(prefixbuf)), pfx->prefixlen);
162 			switch (pfx->origin) {
163 			case PREFIX_FROM_KERNEL:
164 				fprintf(fp, "KERNEL, ");
165 				break;
166 			case PREFIX_FROM_CONFIG:
167 				fprintf(fp, "CONFIG, ");
168 				break;
169 			case PREFIX_FROM_DYNAMIC:
170 				fprintf(fp, "DYNAMIC, ");
171 				break;
172 			}
173 			if (pfx->validlifetime == ND6_INFINITE_LIFETIME)
174 				fprintf(fp, "vltime: infinity");
175 			else
176 				fprintf(fp, "vltime: %ld",
177 					(long)pfx->validlifetime);
178 			if (pfx->vltimeexpire != 0)
179 				fprintf(fp, "(decr,expire %ld), ", (long)
180 					pfx->vltimeexpire > now.tv_sec ?
181 					pfx->vltimeexpire - now.tv_sec : 0);
182 			else
183 				fprintf(fp, ", ");
184 			if (pfx->preflifetime ==  ND6_INFINITE_LIFETIME)
185 				fprintf(fp, "pltime: infinity");
186 			else
187 				fprintf(fp, "pltime: %ld",
188 					(long)pfx->preflifetime);
189 			if (pfx->pltimeexpire != 0)
190 				fprintf(fp, "(decr,expire %ld), ", (long)
191 					pfx->pltimeexpire > now.tv_sec ?
192 					pfx->pltimeexpire - now.tv_sec : 0);
193 			else
194 				fprintf(fp, ", ");
195 			fprintf(fp, "flags: %s%s%s",
196 				pfx->onlinkflg ? "L" : "",
197 				pfx->autoconfflg ? "A" : "",
198 				"");
199 			fprintf(fp, ")\n");
200 		}
201 	}
202 }
203 
204 void
205 rtadvd_dump_file(dumpfile)
206 	char *dumpfile;
207 {
208 	if ((fp = fopen(dumpfile, "w")) == NULL) {
209 		syslog(LOG_WARNING, "<%s> open a dump file(%s)",
210 		       __func__, dumpfile);
211 		return;
212 	}
213 
214 	if_dump();
215 
216 	fclose(fp);
217 }
218