GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/rtadvd/dump.c Lines: 0 108 0.0 %
Date: 2017-11-07 Branches: 0 50 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: dump.c,v 1.23 2017/04/05 13:38:18 jca Exp $	*/
2
/*	$KAME: dump.c,v 1.27 2002/05/29 14:23:55 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 <stdlib.h>
51
#include <string.h>
52
#include <errno.h>
53
#include <netdb.h>
54
#include <event.h>
55
56
#include "rtadvd.h"
57
#include "if.h"
58
#include "log.h"
59
#include "dump.h"
60
61
extern struct ralist ralist;
62
63
static char *ether_str(struct sockaddr_dl *);
64
char *lifetime(int);
65
66
static char *rtpref_str[] = {
67
	"medium",		/* 00 */
68
	"high",			/* 01 */
69
	"rsv",			/* 10 */
70
	"low"			/* 11 */
71
};
72
73
static char *
74
ether_str(struct sockaddr_dl *sdl)
75
{
76
	static char hbuf[NI_MAXHOST];
77
	u_char *cp;
78
79
	if (sdl->sdl_alen) {
80
		cp = (u_char *)LLADDR(sdl);
81
		snprintf(hbuf, sizeof(hbuf), "%02x:%02x:%02x:%02x:%02x:%02x",
82
		    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
83
	} else
84
		snprintf(hbuf, sizeof(hbuf), "NONE");
85
86
	return(hbuf);
87
}
88
89
char *
90
lifetime(int lt)
91
{
92
	char *str;
93
94
	if (lt == ND6_INFINITE_LIFETIME) {
95
		if (asprintf(&str, "infinity") < 0)
96
			return (NULL);
97
	} else {
98
		if (asprintf(&str, "%ld", (long)lt) < 0)
99
			return (NULL);
100
	}
101
	return str;
102
}
103
104
void
105
rtadvd_dump(void)
106
{
107
	struct rainfo *rai;
108
	struct prefix *pfx;
109
	struct rdnss *rds;
110
	struct dnssl *dsl;
111
	struct dnssldom *dnsd;
112
	char prefixbuf[INET6_ADDRSTRLEN];
113
	int first;
114
	struct timeval now, next;
115
	char *origin, *vltime, *pltime, *flags;
116
	char *vltimexpire, *pltimexpire;
117
	char ctimebuf[26];
118
119
	gettimeofday(&now, NULL);
120
	SLIST_FOREACH(rai, &ralist, entry) {
121
		log_info("%s:", rai->ifname);
122
123
		log_info("  Status: %s",
124
		    (iflist[rai->ifindex]->ifm_flags & IFF_UP) ? "UP" : "DOWN");
125
126
		/* control information */
127
		if (rai->lastsent.tv_sec) {
128
			time_t t = rai->lastsent.tv_sec;
129
			(void)strlcpy(ctimebuf, ctime(&t), sizeof(ctimebuf));
130
			ctimebuf[strcspn(ctimebuf, "\n")] = '\0';
131
			log_info("  Last RA sent: %s", ctimebuf);
132
		}
133
		if (evtimer_pending(&rai->timer.ev, &next)) {
134
			time_t t = next.tv_sec;
135
			(void)strlcpy(ctimebuf, ctime(&t), sizeof(ctimebuf));
136
			ctimebuf[strcspn(ctimebuf, "\n")] = '\0';
137
			log_info("  Next RA will be sent: %s", ctimebuf);
138
		} else
139
			log_info("  RA timer is stopped");
140
		log_info("  waits: %u, initcount: %u",
141
		    rai->waiting, rai->initcounter);
142
143
		/* statistics */
144
		log_info("  statistics: RA(out/in/inconsistent): "
145
		    "%llu/%llu/%llu, RS(input): %llu",
146
		    (unsigned long long)rai->raoutput,
147
		    (unsigned long long)rai->rainput,
148
		    (unsigned long long)rai->rainconsistent,
149
		    (unsigned long long)rai->rsinput);
150
151
		/* interface information */
152
		if (rai->advlinkopt)
153
			log_info("  Link-layer address: %s",
154
			    ether_str(rai->sdl));
155
		log_info("  MTU: %d", rai->phymtu);
156
157
		/* Router configuration variables */
158
		log_info("  DefaultLifetime: %d, MaxAdvInterval: %d, "
159
		    "MinAdvInterval: %d, "
160
		    "Flags: %s%s, Preference: %s, MTU: %d",
161
		    rai->lifetime, rai->maxinterval, rai->mininterval,
162
		    rai->managedflg ? "M" : "-", rai->otherflg ? "O" : "-",
163
		    rtpref_str[(rai->rtpref >> 3) & 0xff], rai->linkmtu);
164
		log_info("  ReachableTime: %d, RetransTimer: %d, "
165
		    "CurHopLimit: %d", rai->reachabletime,
166
		    rai->retranstimer, rai->hoplimit);
167
		if (rai->clockskew)
168
			log_info("  Clock skew: %ldsec",
169
			    rai->clockskew);
170
		first = 1;
171
		TAILQ_FOREACH(pfx, &rai->prefixes, entry) {
172
			if (first) {
173
				log_info("  Prefixes:");
174
				first = 0;
175
			}
176
			switch (pfx->origin) {
177
			case PREFIX_FROM_KERNEL:
178
				origin = "KERNEL";
179
				break;
180
			case PREFIX_FROM_CONFIG:
181
				origin = "CONFIG";
182
				break;
183
			case PREFIX_FROM_DYNAMIC:
184
				origin = "DYNAMIC";
185
				break;
186
			default:
187
				origin = "";
188
			}
189
			if (pfx->vltimeexpire != 0) {
190
				/* truncate to onwire value */
191
				if (asprintf(&vltimexpire, "(decr,expire %u)",
192
				    (u_int32_t)(pfx->vltimeexpire > now.tv_sec ?
193
				    pfx->vltimeexpire - now.tv_sec : 0)) == -1)
194
					vltimexpire = NULL;
195
			} else
196
				vltimexpire = NULL;
197
198
			if (pfx->pltimeexpire != 0) {
199
				/* truncate to onwire value */
200
				if (asprintf(&pltimexpire, "(decr,expire %u)",
201
				    (u_int32_t)(pfx->pltimeexpire > now.tv_sec ?
202
				    pfx->pltimeexpire - now.tv_sec : 0)) == -1)
203
					pltimexpire = NULL;
204
			} else
205
				pltimexpire = NULL;
206
207
			vltime = lifetime(pfx->validlifetime);
208
			pltime = lifetime(pfx->preflifetime);
209
			asprintf(&flags, "%s%s",
210
			    pfx->onlinkflg ? "L" : "-",
211
			    pfx->autoconfflg ? "A" : "-");
212
			log_info("    %s/%d(%s, vltime: %s%s, "
213
			    "pltime: %s%s, flags: %s)",
214
			    inet_ntop(AF_INET6, &pfx->prefix, prefixbuf,
215
			    sizeof(prefixbuf)), pfx->prefixlen, origin,
216
			    vltime, (vltimexpire)? vltimexpire : "",
217
			    pltime, (pltimexpire)? pltimexpire : "", flags);
218
219
			free(vltimexpire);
220
			vltimexpire = NULL;
221
			free(pltimexpire);
222
			pltimexpire = NULL;
223
			free(vltime);
224
			free(pltime);
225
			free(flags);
226
		}
227
228
		if (!TAILQ_EMPTY(&rai->rdnsss))
229
			log_info("  Recursive DNS servers:");
230
		TAILQ_FOREACH(rds, &rai->rdnsss, entry) {
231
			log_info("    Servers:");
232
			for (first = 0; first < rds->servercnt; ++first) {
233
				inet_ntop(AF_INET6, &rds->servers[first],
234
				    prefixbuf, sizeof(prefixbuf));
235
				log_info("      %s", prefixbuf);
236
			}
237
			log_info("    Lifetime: %u", rds->lifetime);
238
		}
239
240
		if (!TAILQ_EMPTY(&rai->dnssls))
241
			log_info("  DNS search lists:");
242
		TAILQ_FOREACH(dsl, &rai->dnssls, entry) {
243
			log_info("    Domains:");
244
245
			TAILQ_FOREACH(dnsd, &dsl->dnssldoms, entry)
246
				log_info("      %s", dnsd->domain);
247
248
			log_info("    Lifetime: %u", dsl->lifetime);
249
		}
250
	}
251
}