xref: /freebsd/usr.sbin/rtsold/dump.c (revision edf8578117e8844e02c0121147f45e4609b30680)
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 
34 #include <sys/types.h>
35 #include <sys/capsicum.h>
36 #include <sys/socket.h>
37 #include <sys/queue.h>
38 
39 #include <net/if.h>
40 #include <netinet/in.h>
41 #include <netinet/icmp6.h>
42 #include <arpa/inet.h>
43 
44 #include <capsicum_helpers.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <syslog.h>
49 #include <time.h>
50 
51 #include "rtsold.h"
52 
53 static const char * const ifstatstr[] =
54     { "IDLE", "DELAY", "PROBE", "DOWN", "TENTATIVE" };
55 
56 void
57 rtsold_dump(FILE *fp)
58 {
59 	struct ifinfo *ifi;
60 	struct rainfo *rai;
61 	struct ra_opt *rao;
62 	struct timespec now;
63 	char ntopbuf[INET6_ADDRSTRLEN];
64 
65 	if (fseek(fp, 0, SEEK_SET) != 0) {
66 		warnmsg(LOG_ERR, __func__, "fseek(): %s", strerror(errno));
67 		return;
68 	}
69 	(void)ftruncate(fileno(fp), 0);
70 
71 	(void)clock_gettime(CLOCK_MONOTONIC_FAST, &now);
72 
73 	TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
74 		fprintf(fp, "Interface %s\n", ifi->ifname);
75 		fprintf(fp, "  probe interval: ");
76 		if (ifi->probeinterval) {
77 			fprintf(fp, "%d\n", ifi->probeinterval);
78 			fprintf(fp, "  probe timer: %d\n", ifi->probetimer);
79 		} else {
80 			fprintf(fp, "infinity\n");
81 			fprintf(fp, "  no probe timer\n");
82 		}
83 		fprintf(fp, "  interface status: %s\n",
84 		    ifi->active > 0 ? "active" : "inactive");
85 		fprintf(fp, "  managed config: %s\n",
86 		    ifi->managedconfig ? "on" : "off");
87 		fprintf(fp, "  other config: %s\n",
88 		    ifi->otherconfig ? "on" : "off");
89 		fprintf(fp, "  rtsold status: %s\n", ifstatstr[ifi->state]);
90 		fprintf(fp, "  carrier detection: %s\n",
91 		    ifi->mediareqok ? "available" : "unavailable");
92 		fprintf(fp, "  probes: %d, dadcount = %d\n",
93 		    ifi->probes, ifi->dadcount);
94 		if (ifi->timer.tv_sec == tm_max.tv_sec &&
95 		    ifi->timer.tv_nsec == tm_max.tv_nsec)
96 			fprintf(fp, "  no timer\n");
97 		else {
98 			fprintf(fp, "  timer: interval=%d:%d, expire=%s\n",
99 			    (int)ifi->timer.tv_sec,
100 			    (int)ifi->timer.tv_nsec / 1000,
101 			    (ifi->expire.tv_sec < now.tv_sec) ? "expired"
102 			    : sec2str(&ifi->expire));
103 		}
104 		fprintf(fp, "  number of valid RAs: %d\n", ifi->racnt);
105 
106 		TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
107 			fprintf(fp, "   RA from %s\n",
108 			    inet_ntop(AF_INET6, &rai->rai_saddr.sin6_addr,
109 				ntopbuf, sizeof(ntopbuf)));
110 			TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
111 				fprintf(fp, "    option: ");
112 				switch (rao->rao_type) {
113 				case ND_OPT_RDNSS:
114 					fprintf(fp, "RDNSS: %s (expire: %s)\n",
115 					    (char *)rao->rao_msg,
116 					    sec2str(&rao->rao_expire));
117 					break;
118 				case ND_OPT_DNSSL:
119 					fprintf(fp, "DNSSL: %s (expire: %s)\n",
120 					    (char *)rao->rao_msg,
121 					    sec2str(&rao->rao_expire));
122 					break;
123 				default:
124 					break;
125 				}
126 			}
127 			fprintf(fp, "\n");
128 		}
129 	}
130 	fflush(fp);
131 }
132 
133 FILE *
134 rtsold_init_dumpfile(const char *dumpfile)
135 {
136 	cap_rights_t rights;
137 	FILE *fp;
138 
139 	if ((fp = fopen(dumpfile, "w")) == NULL) {
140 		warnmsg(LOG_WARNING, __func__, "opening a dump file(%s): %s",
141 		    dumpfile, strerror(errno));
142 		return (NULL);
143 	}
144 
145 	cap_rights_init(&rights, CAP_FSTAT, CAP_FTRUNCATE, CAP_SEEK, CAP_WRITE);
146 	if (caph_rights_limit(fileno(fp), &rights) != 0) {
147 		warnmsg(LOG_WARNING, __func__, "caph_rights_limit(%s): %s",
148 		    dumpfile, strerror(errno));
149 		(void)fclose(fp);
150 		return (NULL);
151 	}
152 	return (fp);
153 }
154 
155 const char *
156 sec2str(const struct timespec *total)
157 {
158 	static char result[256];
159 	int days, hours, mins, secs;
160 	int first = 1;
161 	char *p = result;
162 	char *ep = &result[sizeof(result)];
163 	int n;
164 	struct timespec now;
165 	time_t tsec;
166 
167 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
168 	tsec  = total->tv_sec;
169 	tsec += total->tv_nsec / 1000 / 1000000;
170 	tsec -= now.tv_sec;
171 	tsec -= now.tv_nsec / 1000 / 1000000;
172 
173 	days = tsec / 3600 / 24;
174 	hours = (tsec / 3600) % 24;
175 	mins = (tsec / 60) % 60;
176 	secs = tsec % 60;
177 
178 	if (days) {
179 		first = 0;
180 		n = snprintf(p, ep - p, "%dd", days);
181 		if (n < 0 || n >= ep - p)
182 			return "?";
183 		p += n;
184 	}
185 	if (!first || hours) {
186 		first = 0;
187 		n = snprintf(p, ep - p, "%dh", hours);
188 		if (n < 0 || n >= ep - p)
189 			return "?";
190 		p += n;
191 	}
192 	if (!first || mins) {
193 		first = 0;
194 		n = snprintf(p, ep - p, "%dm", mins);
195 		if (n < 0 || n >= ep - p)
196 			return "?";
197 		p += n;
198 	}
199 	snprintf(p, ep - p, "%ds", secs);
200 
201 	return (result);
202 }
203