xref: /freebsd/usr.sbin/rtsold/dump.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*	$KAME: dump.c,v 1.13 2003/10/05 00:09:36 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (C) 1999 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35 
36 #include <sys/types.h>
37 #include <sys/capsicum.h>
38 #include <sys/socket.h>
39 #include <sys/queue.h>
40 
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/icmp6.h>
44 #include <arpa/inet.h>
45 
46 #include <capsicum_helpers.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <syslog.h>
51 #include <time.h>
52 
53 #include "rtsold.h"
54 
55 static const char * const ifstatstr[] =
56     { "IDLE", "DELAY", "PROBE", "DOWN", "TENTATIVE" };
57 
58 void
59 rtsold_dump(FILE *fp)
60 {
61 	struct ifinfo *ifi;
62 	struct rainfo *rai;
63 	struct ra_opt *rao;
64 	struct timespec now;
65 	char ntopbuf[INET6_ADDRSTRLEN];
66 
67 	if (fseek(fp, 0, SEEK_SET) != 0) {
68 		warnmsg(LOG_ERR, __func__, "fseek(): %s", strerror(errno));
69 		return;
70 	}
71 	(void)ftruncate(fileno(fp), 0);
72 
73 	(void)clock_gettime(CLOCK_MONOTONIC_FAST, &now);
74 
75 	TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
76 		fprintf(fp, "Interface %s\n", ifi->ifname);
77 		fprintf(fp, "  probe interval: ");
78 		if (ifi->probeinterval) {
79 			fprintf(fp, "%d\n", ifi->probeinterval);
80 			fprintf(fp, "  probe timer: %d\n", ifi->probetimer);
81 		} else {
82 			fprintf(fp, "infinity\n");
83 			fprintf(fp, "  no probe timer\n");
84 		}
85 		fprintf(fp, "  interface status: %s\n",
86 		    ifi->active > 0 ? "active" : "inactive");
87 		fprintf(fp, "  managed config: %s\n",
88 		    ifi->managedconfig ? "on" : "off");
89 		fprintf(fp, "  other config: %s\n",
90 		    ifi->otherconfig ? "on" : "off");
91 		fprintf(fp, "  rtsold status: %s\n", ifstatstr[ifi->state]);
92 		fprintf(fp, "  carrier detection: %s\n",
93 		    ifi->mediareqok ? "available" : "unavailable");
94 		fprintf(fp, "  probes: %d, dadcount = %d\n",
95 		    ifi->probes, ifi->dadcount);
96 		if (ifi->timer.tv_sec == tm_max.tv_sec &&
97 		    ifi->timer.tv_nsec == tm_max.tv_nsec)
98 			fprintf(fp, "  no timer\n");
99 		else {
100 			fprintf(fp, "  timer: interval=%d:%d, expire=%s\n",
101 			    (int)ifi->timer.tv_sec,
102 			    (int)ifi->timer.tv_nsec / 1000,
103 			    (ifi->expire.tv_sec < now.tv_sec) ? "expired"
104 			    : sec2str(&ifi->expire));
105 		}
106 		fprintf(fp, "  number of valid RAs: %d\n", ifi->racnt);
107 
108 		TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
109 			fprintf(fp, "   RA from %s\n",
110 			    inet_ntop(AF_INET6, &rai->rai_saddr.sin6_addr,
111 				ntopbuf, sizeof(ntopbuf)));
112 			TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
113 				fprintf(fp, "    option: ");
114 				switch (rao->rao_type) {
115 				case ND_OPT_RDNSS:
116 					fprintf(fp, "RDNSS: %s (expire: %s)\n",
117 					    (char *)rao->rao_msg,
118 					    sec2str(&rao->rao_expire));
119 					break;
120 				case ND_OPT_DNSSL:
121 					fprintf(fp, "DNSSL: %s (expire: %s)\n",
122 					    (char *)rao->rao_msg,
123 					    sec2str(&rao->rao_expire));
124 					break;
125 				default:
126 					break;
127 				}
128 			}
129 			fprintf(fp, "\n");
130 		}
131 	}
132 	fflush(fp);
133 }
134 
135 FILE *
136 rtsold_init_dumpfile(const char *dumpfile)
137 {
138 	cap_rights_t rights;
139 	FILE *fp;
140 
141 	if ((fp = fopen(dumpfile, "w")) == NULL) {
142 		warnmsg(LOG_WARNING, __func__, "opening a dump file(%s): %s",
143 		    dumpfile, strerror(errno));
144 		return (NULL);
145 	}
146 
147 	cap_rights_init(&rights, CAP_FSTAT, CAP_FTRUNCATE, CAP_SEEK, CAP_WRITE);
148 	if (caph_rights_limit(fileno(fp), &rights) != 0) {
149 		warnmsg(LOG_WARNING, __func__, "caph_rights_limit(%s): %s",
150 		    dumpfile, strerror(errno));
151 		(void)fclose(fp);
152 		return (NULL);
153 	}
154 	return (fp);
155 }
156 
157 const char *
158 sec2str(const struct timespec *total)
159 {
160 	static char result[256];
161 	int days, hours, mins, secs;
162 	int first = 1;
163 	char *p = result;
164 	char *ep = &result[sizeof(result)];
165 	int n;
166 	struct timespec now;
167 	time_t tsec;
168 
169 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
170 	tsec  = total->tv_sec;
171 	tsec += total->tv_nsec / 1000 / 1000000;
172 	tsec -= now.tv_sec;
173 	tsec -= now.tv_nsec / 1000 / 1000000;
174 
175 	days = tsec / 3600 / 24;
176 	hours = (tsec / 3600) % 24;
177 	mins = (tsec / 60) % 60;
178 	secs = tsec % 60;
179 
180 	if (days) {
181 		first = 0;
182 		n = snprintf(p, ep - p, "%dd", days);
183 		if (n < 0 || n >= ep - p)
184 			return "?";
185 		p += n;
186 	}
187 	if (!first || hours) {
188 		first = 0;
189 		n = snprintf(p, ep - p, "%dh", hours);
190 		if (n < 0 || n >= ep - p)
191 			return "?";
192 		p += n;
193 	}
194 	if (!first || mins) {
195 		first = 0;
196 		n = snprintf(p, ep - p, "%dm", mins);
197 		if (n < 0 || n >= ep - p)
198 			return "?";
199 		p += n;
200 	}
201 	snprintf(p, ep - p, "%ds", secs);
202 
203 	return (result);
204 }
205