xref: /netbsd-src/usr.sbin/rtadvd/dump.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: dump.c,v 1.2 2000/07/06 12:37:56 itojun Exp $	*/
2 /*	$KAME: dump.c,v 1.11 2000/05/27 11:30:43 jinmei 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 
35 #include <net/if.h>
36 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
37 #include <net/if_var.h>
38 #endif /* __FreeBSD__ >= 3 */
39 #include <net/if_dl.h>
40 
41 #include <netinet/in.h>
42 
43 /* XXX: the following two are non-standard include files */
44 #include <netinet6/in6_var.h>
45 #include <netinet6/nd6.h>
46 
47 #include <arpa/inet.h>
48 
49 #include <time.h>
50 #include <stdio.h>
51 #include <stdarg.h>
52 #include <syslog.h>
53 #include <string.h>
54 #include <errno.h>
55 
56 #include "rtadvd.h"
57 #include "timer.h"
58 #include "if.h"
59 #include "dump.h"
60 
61 static FILE *fp;
62 
63 extern struct rainfo *ralist;
64 
65 static char *ether_str __P((struct sockaddr_dl *));
66 static void if_dump __P((void));
67 
68 #ifdef __FreeBSD__		/* XXX: see PORTABILITY */
69 #define LONGLONG "%qu"
70 #else
71 #define LONGLONG "%llu"
72 #endif
73 
74 static char *
75 ether_str(sdl)
76 	struct sockaddr_dl *sdl;
77 {
78 	static char ebuf[32];
79 	u_char *cp;
80 
81 	if (sdl->sdl_alen && sdl->sdl_alen > 5) {
82 		cp = (u_char *)LLADDR(sdl);
83 		sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
84 			cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
85 	}
86 	else {
87 		sprintf(ebuf, "NONE");
88 	}
89 
90 	return(ebuf);
91 }
92 
93 static void
94 if_dump()
95 {
96 	struct rainfo *rai;
97 	struct prefix *pfx;
98 	char prefixbuf[INET6_ADDRSTRLEN];
99 	int first;
100 
101 	for (rai = ralist; rai; rai = rai->next) {
102 		fprintf(fp, "%s:\n", rai->ifname);
103 
104 		fprintf(fp, "  Status: %s\n",
105 			(iflist[rai->ifindex]->ifm_flags & IFF_UP) ? "UP" :
106 			"DOWN");
107 
108 		/* control information */
109 		if (rai->lastsent.tv_sec) {
110 			/* note that ctime() appends CR by itself */
111 			fprintf(fp, "  Last RA sent: %s",
112 				ctime((time_t *)&rai->lastsent.tv_sec));
113 		}
114 		if (rai->timer) {
115 			fprintf(fp, "  Next RA will be sent: %s",
116 				ctime((time_t *)&rai->timer->tm.tv_sec));
117 		}
118 		else
119 			fprintf(fp, "  RA timer is stopped");
120 		fprintf(fp, "  waits: %d, initcount: %d\n",
121 			rai->waiting, rai->initcounter);
122 
123 		/* statistics */
124 		fprintf(fp,
125 			"  statistics: RA(out/in/inconsistent): "
126 			LONGLONG "/" LONGLONG "/" LONGLONG ", ",
127 			(unsigned long long)rai->raoutput,
128 			(unsigned long long)rai->rainput,
129 			(unsigned long long)rai->rainconsistent);
130 		fprintf(fp, "RS(input): " LONGLONG "\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,
141 			"  DefaultLifetime: %d, MaxAdvInterval: %d, "
142 			"MinAdvInterval: %d\n",
143 			rai->lifetime, rai->maxinterval, rai->mininterval);
144 		fprintf(fp, "  Flags: %s%s%s MTU: %d\n",
145 			rai->managedflg ? "M" : "", rai->otherflg ? "O" : "",
146 #ifdef MIP6
147 			rai->haflg ? "H" :
148 #endif
149 			"", rai->linkmtu);
150 		fprintf(fp, "  ReachableTime: %d, RetransTimer: %d, "
151 			"CurHopLimit: %d\n", rai->reachabletime,
152 			rai->retranstimer, rai->hoplimit);
153 #ifdef MIP6
154 		fprintf(fp, "  HAPreference: %d, HALifetime: %d\n",
155 			rai->hapref, rai->hatime);
156 #endif
157 
158 		for (first = 1, pfx = rai->prefix.next; pfx != &rai->prefix;
159 		     pfx = pfx->next) {
160 			if (first) {
161 				fprintf(fp, "  Prefixes:\n");
162 				first = 0;
163 			}
164 			fprintf(fp, "    %s/%d(",
165 				inet_ntop(AF_INET6, &pfx->prefix,
166 					  prefixbuf, sizeof(prefixbuf)),
167 				pfx->prefixlen);
168 			switch(pfx->origin) {
169 			case PREFIX_FROM_KERNEL:
170 				fprintf(fp, "KERNEL, ");
171 				break;
172 			case PREFIX_FROM_CONFIG:
173 				fprintf(fp, "CONFIG, ");
174 				break;
175 			case PREFIX_FROM_DYNAMIC:
176 				fprintf(fp, "DYNAMIC, ");
177 				break;
178 			}
179 			if (pfx->validlifetime == ND6_INFINITE_LIFETIME)
180 				fprintf(fp, "vltime: infinity, ");
181 			else
182 				fprintf(fp, "vltime: %ld, ",
183 					(long)pfx->validlifetime);
184 			if (pfx->preflifetime ==  ND6_INFINITE_LIFETIME)
185 				fprintf(fp, "pltime: infinity, ");
186 			else
187 				fprintf(fp, "pltime: %ld, ",
188 					(long)pfx->preflifetime);
189 			fprintf(fp, "flags: %s%s%s",
190 				pfx->onlinkflg ? "L" : "",
191 				pfx->autoconfflg ? "A" : "",
192 #ifdef MIP6
193 				pfx->routeraddr ? "R" :
194 #endif
195 				"");
196 			fprintf(fp, ")\n");
197 		}
198 	}
199 }
200 
201 void
202 rtadvd_dump_file(dumpfile)
203 	char *dumpfile;
204 {
205 	if ((fp = fopen(dumpfile, "w")) == NULL) {
206 		syslog(LOG_WARNING, "<%s> open a dump file(%s)",
207 		       __FUNCTION__, dumpfile);
208 		return;
209 	}
210 
211 	if_dump();
212 
213 	fclose(fp);
214 }
215