xref: /freebsd/usr.sbin/rtsold/dump.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
18e7409edSHajimu UMEMOTO /*	$KAME: dump.c,v 1.13 2003/10/05 00:09:36 itojun Exp $	*/
2804c83d4SKris Kennaway 
38a16b7a1SPedro F. Giffuni /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
67d56d374SYoshinobu Inoue  * Copyright (C) 1999 WIDE Project.
77d56d374SYoshinobu Inoue  * All rights reserved.
87d56d374SYoshinobu Inoue  *
97d56d374SYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
107d56d374SYoshinobu Inoue  * modification, are permitted provided that the following conditions
117d56d374SYoshinobu Inoue  * are met:
127d56d374SYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
137d56d374SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
147d56d374SYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
157d56d374SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
167d56d374SYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
177d56d374SYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
187d56d374SYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
197d56d374SYoshinobu Inoue  *    without specific prior written permission.
207d56d374SYoshinobu Inoue  *
217d56d374SYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
227d56d374SYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237d56d374SYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247d56d374SYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
257d56d374SYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267d56d374SYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277d56d374SYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287d56d374SYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297d56d374SYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307d56d374SYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317d56d374SYoshinobu Inoue  * SUCH DAMAGE.
327d56d374SYoshinobu Inoue  */
337d56d374SYoshinobu Inoue 
347d56d374SYoshinobu Inoue #include <sys/types.h>
3504e9edb5SMark Johnston #include <sys/capsicum.h>
36259df286SKris Kennaway #include <sys/socket.h>
37db82af41SHiroki Sato #include <sys/queue.h>
387d56d374SYoshinobu Inoue 
39259df286SKris Kennaway #include <net/if.h>
407d56d374SYoshinobu Inoue #include <netinet/in.h>
417d56d374SYoshinobu Inoue #include <netinet/icmp6.h>
4206056832SHiroki Sato #include <arpa/inet.h>
437d56d374SYoshinobu Inoue 
4404e9edb5SMark Johnston #include <capsicum_helpers.h>
4504e9edb5SMark Johnston #include <errno.h>
467d56d374SYoshinobu Inoue #include <stdio.h>
477d56d374SYoshinobu Inoue #include <string.h>
4804e9edb5SMark Johnston #include <syslog.h>
4904e9edb5SMark Johnston #include <time.h>
507d56d374SYoshinobu Inoue 
517d56d374SYoshinobu Inoue #include "rtsold.h"
527d56d374SYoshinobu Inoue 
5304e9edb5SMark Johnston static const char * const ifstatstr[] =
5404e9edb5SMark Johnston     { "IDLE", "DELAY", "PROBE", "DOWN", "TENTATIVE" };
557d56d374SYoshinobu Inoue 
5604e9edb5SMark Johnston void
rtsold_dump(FILE * fp)5704e9edb5SMark Johnston rtsold_dump(FILE *fp)
587d56d374SYoshinobu Inoue {
59db82af41SHiroki Sato 	struct ifinfo *ifi;
6006056832SHiroki Sato 	struct rainfo *rai;
6106056832SHiroki Sato 	struct ra_opt *rao;
627d26db17SHiroki Sato 	struct timespec now;
6306056832SHiroki Sato 	char ntopbuf[INET6_ADDRSTRLEN];
647d56d374SYoshinobu Inoue 
6504e9edb5SMark Johnston 	if (fseek(fp, 0, SEEK_SET) != 0) {
6604e9edb5SMark Johnston 		warnmsg(LOG_ERR, __func__, "fseek(): %s", strerror(errno));
6704e9edb5SMark Johnston 		return;
6804e9edb5SMark Johnston 	}
6904e9edb5SMark Johnston 	(void)ftruncate(fileno(fp), 0);
7004e9edb5SMark Johnston 
7104e9edb5SMark Johnston 	(void)clock_gettime(CLOCK_MONOTONIC_FAST, &now);
727d56d374SYoshinobu Inoue 
73db82af41SHiroki Sato 	TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
74db82af41SHiroki Sato 		fprintf(fp, "Interface %s\n", ifi->ifname);
757d56d374SYoshinobu Inoue 		fprintf(fp, "  probe interval: ");
76db82af41SHiroki Sato 		if (ifi->probeinterval) {
77db82af41SHiroki Sato 			fprintf(fp, "%d\n", ifi->probeinterval);
78db82af41SHiroki Sato 			fprintf(fp, "  probe timer: %d\n", ifi->probetimer);
79fa19f9beSHajimu UMEMOTO 		} else {
807d56d374SYoshinobu Inoue 			fprintf(fp, "infinity\n");
817d56d374SYoshinobu Inoue 			fprintf(fp, "  no probe timer\n");
827d56d374SYoshinobu Inoue 		}
837d56d374SYoshinobu Inoue 		fprintf(fp, "  interface status: %s\n",
84db82af41SHiroki Sato 		    ifi->active > 0 ? "active" : "inactive");
8560e7f669SBjoern A. Zeeb 		fprintf(fp, "  managed config: %s\n",
8660e7f669SBjoern A. Zeeb 		    ifi->managedconfig ? "on" : "off");
8786b032afSHajimu UMEMOTO 		fprintf(fp, "  other config: %s\n",
88db82af41SHiroki Sato 		    ifi->otherconfig ? "on" : "off");
89db82af41SHiroki Sato 		fprintf(fp, "  rtsold status: %s\n", ifstatstr[ifi->state]);
907d56d374SYoshinobu Inoue 		fprintf(fp, "  carrier detection: %s\n",
91db82af41SHiroki Sato 		    ifi->mediareqok ? "available" : "unavailable");
927d56d374SYoshinobu Inoue 		fprintf(fp, "  probes: %d, dadcount = %d\n",
93db82af41SHiroki Sato 		    ifi->probes, ifi->dadcount);
94db82af41SHiroki Sato 		if (ifi->timer.tv_sec == tm_max.tv_sec &&
957d26db17SHiroki Sato 		    ifi->timer.tv_nsec == tm_max.tv_nsec)
967d56d374SYoshinobu Inoue 			fprintf(fp, "  no timer\n");
977d56d374SYoshinobu Inoue 		else {
987d56d374SYoshinobu Inoue 			fprintf(fp, "  timer: interval=%d:%d, expire=%s\n",
99db82af41SHiroki Sato 			    (int)ifi->timer.tv_sec,
1007d26db17SHiroki Sato 			    (int)ifi->timer.tv_nsec / 1000,
101db82af41SHiroki Sato 			    (ifi->expire.tv_sec < now.tv_sec) ? "expired"
10206056832SHiroki Sato 			    : sec2str(&ifi->expire));
1037d56d374SYoshinobu Inoue 		}
104db82af41SHiroki Sato 		fprintf(fp, "  number of valid RAs: %d\n", ifi->racnt);
10506056832SHiroki Sato 
10606056832SHiroki Sato 		TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
10706056832SHiroki Sato 			fprintf(fp, "   RA from %s\n",
10806056832SHiroki Sato 			    inet_ntop(AF_INET6, &rai->rai_saddr.sin6_addr,
10906056832SHiroki Sato 				ntopbuf, sizeof(ntopbuf)));
11006056832SHiroki Sato 			TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
11106056832SHiroki Sato 				fprintf(fp, "    option: ");
11206056832SHiroki Sato 				switch (rao->rao_type) {
11306056832SHiroki Sato 				case ND_OPT_RDNSS:
11406056832SHiroki Sato 					fprintf(fp, "RDNSS: %s (expire: %s)\n",
11506056832SHiroki Sato 					    (char *)rao->rao_msg,
11606056832SHiroki Sato 					    sec2str(&rao->rao_expire));
11706056832SHiroki Sato 					break;
11806056832SHiroki Sato 				case ND_OPT_DNSSL:
11906056832SHiroki Sato 					fprintf(fp, "DNSSL: %s (expire: %s)\n",
12006056832SHiroki Sato 					    (char *)rao->rao_msg,
12106056832SHiroki Sato 					    sec2str(&rao->rao_expire));
12206056832SHiroki Sato 					break;
12306056832SHiroki Sato 				default:
12406056832SHiroki Sato 					break;
12506056832SHiroki Sato 				}
12606056832SHiroki Sato 			}
12706056832SHiroki Sato 			fprintf(fp, "\n");
12806056832SHiroki Sato 		}
1297d56d374SYoshinobu Inoue 	}
13004e9edb5SMark Johnston 	fflush(fp);
1317d56d374SYoshinobu Inoue }
1327d56d374SYoshinobu Inoue 
13304e9edb5SMark Johnston FILE *
rtsold_init_dumpfile(const char * dumpfile)13404e9edb5SMark Johnston rtsold_init_dumpfile(const char *dumpfile)
1357d56d374SYoshinobu Inoue {
13604e9edb5SMark Johnston 	cap_rights_t rights;
13704e9edb5SMark Johnston 	FILE *fp;
13804e9edb5SMark Johnston 
1397d56d374SYoshinobu Inoue 	if ((fp = fopen(dumpfile, "w")) == NULL) {
14004e9edb5SMark Johnston 		warnmsg(LOG_WARNING, __func__, "opening a dump file(%s): %s",
1417d56d374SYoshinobu Inoue 		    dumpfile, strerror(errno));
14204e9edb5SMark Johnston 		return (NULL);
1437d56d374SYoshinobu Inoue 	}
14404e9edb5SMark Johnston 
14504e9edb5SMark Johnston 	cap_rights_init(&rights, CAP_FSTAT, CAP_FTRUNCATE, CAP_SEEK, CAP_WRITE);
14604e9edb5SMark Johnston 	if (caph_rights_limit(fileno(fp), &rights) != 0) {
14704e9edb5SMark Johnston 		warnmsg(LOG_WARNING, __func__, "caph_rights_limit(%s): %s",
14804e9edb5SMark Johnston 		    dumpfile, strerror(errno));
149*ecce515dSMark Johnston 		(void)fclose(fp);
15004e9edb5SMark Johnston 		return (NULL);
15104e9edb5SMark Johnston 	}
15204e9edb5SMark Johnston 	return (fp);
1537d56d374SYoshinobu Inoue }
1547d56d374SYoshinobu Inoue 
15506056832SHiroki Sato const char *
sec2str(const struct timespec * total)1567d26db17SHiroki Sato sec2str(const struct timespec *total)
1577d56d374SYoshinobu Inoue {
1587d56d374SYoshinobu Inoue 	static char result[256];
1597d56d374SYoshinobu Inoue 	int days, hours, mins, secs;
1607d56d374SYoshinobu Inoue 	int first = 1;
1617d56d374SYoshinobu Inoue 	char *p = result;
162bb58b617SHajimu UMEMOTO 	char *ep = &result[sizeof(result)];
163bb58b617SHajimu UMEMOTO 	int n;
1647d26db17SHiroki Sato 	struct timespec now;
16506056832SHiroki Sato 	time_t tsec;
1667d56d374SYoshinobu Inoue 
1677d26db17SHiroki Sato 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
16806056832SHiroki Sato 	tsec  = total->tv_sec;
1697d26db17SHiroki Sato 	tsec += total->tv_nsec / 1000 / 1000000;
17006056832SHiroki Sato 	tsec -= now.tv_sec;
1717d26db17SHiroki Sato 	tsec -= now.tv_nsec / 1000 / 1000000;
17206056832SHiroki Sato 
17306056832SHiroki Sato 	days = tsec / 3600 / 24;
17406056832SHiroki Sato 	hours = (tsec / 3600) % 24;
17506056832SHiroki Sato 	mins = (tsec / 60) % 60;
17606056832SHiroki Sato 	secs = tsec % 60;
1777d56d374SYoshinobu Inoue 
1787d56d374SYoshinobu Inoue 	if (days) {
1797d56d374SYoshinobu Inoue 		first = 0;
180bb58b617SHajimu UMEMOTO 		n = snprintf(p, ep - p, "%dd", days);
181bb58b617SHajimu UMEMOTO 		if (n < 0 || n >= ep - p)
182bb58b617SHajimu UMEMOTO 			return "?";
183bb58b617SHajimu UMEMOTO 		p += n;
1847d56d374SYoshinobu Inoue 	}
1857d56d374SYoshinobu Inoue 	if (!first || hours) {
1867d56d374SYoshinobu Inoue 		first = 0;
187bb58b617SHajimu UMEMOTO 		n = snprintf(p, ep - p, "%dh", hours);
188bb58b617SHajimu UMEMOTO 		if (n < 0 || n >= ep - p)
189bb58b617SHajimu UMEMOTO 			return "?";
190bb58b617SHajimu UMEMOTO 		p += n;
1917d56d374SYoshinobu Inoue 	}
1927d56d374SYoshinobu Inoue 	if (!first || mins) {
1937d56d374SYoshinobu Inoue 		first = 0;
194bb58b617SHajimu UMEMOTO 		n = snprintf(p, ep - p, "%dm", mins);
195bb58b617SHajimu UMEMOTO 		if (n < 0 || n >= ep - p)
196bb58b617SHajimu UMEMOTO 			return "?";
197bb58b617SHajimu UMEMOTO 		p += n;
1987d56d374SYoshinobu Inoue 	}
199bb58b617SHajimu UMEMOTO 	snprintf(p, ep - p, "%ds", secs);
200db82af41SHiroki Sato 
2017d56d374SYoshinobu Inoue 	return (result);
2027d56d374SYoshinobu Inoue }
203