xref: /freebsd/usr.sbin/rtsold/dump.c (revision 04e9edb5442826a14616157962361ff81e4a38fe)
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  * $FreeBSD$
347d56d374SYoshinobu Inoue  */
357d56d374SYoshinobu Inoue 
367d56d374SYoshinobu Inoue #include <sys/types.h>
37*04e9edb5SMark Johnston #include <sys/capsicum.h>
38259df286SKris Kennaway #include <sys/socket.h>
39db82af41SHiroki Sato #include <sys/queue.h>
407d56d374SYoshinobu Inoue 
41259df286SKris Kennaway #include <net/if.h>
427d56d374SYoshinobu Inoue #include <netinet/in.h>
437d56d374SYoshinobu Inoue #include <netinet/icmp6.h>
4406056832SHiroki Sato #include <arpa/inet.h>
457d56d374SYoshinobu Inoue 
46*04e9edb5SMark Johnston #include <capsicum_helpers.h>
47*04e9edb5SMark Johnston #include <errno.h>
487d56d374SYoshinobu Inoue #include <stdio.h>
497d56d374SYoshinobu Inoue #include <string.h>
50*04e9edb5SMark Johnston #include <syslog.h>
51*04e9edb5SMark Johnston #include <time.h>
527d56d374SYoshinobu Inoue 
537d56d374SYoshinobu Inoue #include "rtsold.h"
547d56d374SYoshinobu Inoue 
55*04e9edb5SMark Johnston static const char * const ifstatstr[] =
56*04e9edb5SMark Johnston     { "IDLE", "DELAY", "PROBE", "DOWN", "TENTATIVE" };
577d56d374SYoshinobu Inoue 
58*04e9edb5SMark Johnston void
59*04e9edb5SMark Johnston rtsold_dump(FILE *fp)
607d56d374SYoshinobu Inoue {
61db82af41SHiroki Sato 	struct ifinfo *ifi;
6206056832SHiroki Sato 	struct rainfo *rai;
6306056832SHiroki Sato 	struct ra_opt *rao;
647d26db17SHiroki Sato 	struct timespec now;
6506056832SHiroki Sato 	char ntopbuf[INET6_ADDRSTRLEN];
667d56d374SYoshinobu Inoue 
67*04e9edb5SMark Johnston 	if (fseek(fp, 0, SEEK_SET) != 0) {
68*04e9edb5SMark Johnston 		warnmsg(LOG_ERR, __func__, "fseek(): %s", strerror(errno));
69*04e9edb5SMark Johnston 		return;
70*04e9edb5SMark Johnston 	}
71*04e9edb5SMark Johnston 	(void)ftruncate(fileno(fp), 0);
72*04e9edb5SMark Johnston 
73*04e9edb5SMark Johnston 	(void)clock_gettime(CLOCK_MONOTONIC_FAST, &now);
747d56d374SYoshinobu Inoue 
75db82af41SHiroki Sato 	TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
76db82af41SHiroki Sato 		fprintf(fp, "Interface %s\n", ifi->ifname);
777d56d374SYoshinobu Inoue 		fprintf(fp, "  probe interval: ");
78db82af41SHiroki Sato 		if (ifi->probeinterval) {
79db82af41SHiroki Sato 			fprintf(fp, "%d\n", ifi->probeinterval);
80db82af41SHiroki Sato 			fprintf(fp, "  probe timer: %d\n", ifi->probetimer);
81fa19f9beSHajimu UMEMOTO 		} else {
827d56d374SYoshinobu Inoue 			fprintf(fp, "infinity\n");
837d56d374SYoshinobu Inoue 			fprintf(fp, "  no probe timer\n");
847d56d374SYoshinobu Inoue 		}
857d56d374SYoshinobu Inoue 		fprintf(fp, "  interface status: %s\n",
86db82af41SHiroki Sato 		    ifi->active > 0 ? "active" : "inactive");
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 	}
130*04e9edb5SMark Johnston 	fflush(fp);
1317d56d374SYoshinobu Inoue }
1327d56d374SYoshinobu Inoue 
133*04e9edb5SMark Johnston FILE *
134*04e9edb5SMark Johnston rtsold_init_dumpfile(const char *dumpfile)
1357d56d374SYoshinobu Inoue {
136*04e9edb5SMark Johnston 	cap_rights_t rights;
137*04e9edb5SMark Johnston 	FILE *fp;
138*04e9edb5SMark Johnston 
1397d56d374SYoshinobu Inoue 	if ((fp = fopen(dumpfile, "w")) == NULL) {
140*04e9edb5SMark Johnston 		warnmsg(LOG_WARNING, __func__, "opening a dump file(%s): %s",
1417d56d374SYoshinobu Inoue 		    dumpfile, strerror(errno));
142*04e9edb5SMark Johnston 		return (NULL);
1437d56d374SYoshinobu Inoue 	}
144*04e9edb5SMark Johnston 
145*04e9edb5SMark Johnston 	cap_rights_init(&rights, CAP_FSTAT, CAP_FTRUNCATE, CAP_SEEK, CAP_WRITE);
146*04e9edb5SMark Johnston 	if (caph_rights_limit(fileno(fp), &rights) != 0) {
147*04e9edb5SMark Johnston 		warnmsg(LOG_WARNING, __func__, "caph_rights_limit(%s): %s",
148*04e9edb5SMark Johnston 		    dumpfile, strerror(errno));
149*04e9edb5SMark Johnston 		return (NULL);
150*04e9edb5SMark Johnston 	}
151*04e9edb5SMark Johnston 	return (fp);
1527d56d374SYoshinobu Inoue }
1537d56d374SYoshinobu Inoue 
15406056832SHiroki Sato const char *
1557d26db17SHiroki Sato sec2str(const struct timespec *total)
1567d56d374SYoshinobu Inoue {
1577d56d374SYoshinobu Inoue 	static char result[256];
1587d56d374SYoshinobu Inoue 	int days, hours, mins, secs;
1597d56d374SYoshinobu Inoue 	int first = 1;
1607d56d374SYoshinobu Inoue 	char *p = result;
161bb58b617SHajimu UMEMOTO 	char *ep = &result[sizeof(result)];
162bb58b617SHajimu UMEMOTO 	int n;
1637d26db17SHiroki Sato 	struct timespec now;
16406056832SHiroki Sato 	time_t tsec;
1657d56d374SYoshinobu Inoue 
1667d26db17SHiroki Sato 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
16706056832SHiroki Sato 	tsec  = total->tv_sec;
1687d26db17SHiroki Sato 	tsec += total->tv_nsec / 1000 / 1000000;
16906056832SHiroki Sato 	tsec -= now.tv_sec;
1707d26db17SHiroki Sato 	tsec -= now.tv_nsec / 1000 / 1000000;
17106056832SHiroki Sato 
17206056832SHiroki Sato 	days = tsec / 3600 / 24;
17306056832SHiroki Sato 	hours = (tsec / 3600) % 24;
17406056832SHiroki Sato 	mins = (tsec / 60) % 60;
17506056832SHiroki Sato 	secs = tsec % 60;
1767d56d374SYoshinobu Inoue 
1777d56d374SYoshinobu Inoue 	if (days) {
1787d56d374SYoshinobu Inoue 		first = 0;
179bb58b617SHajimu UMEMOTO 		n = snprintf(p, ep - p, "%dd", days);
180bb58b617SHajimu UMEMOTO 		if (n < 0 || n >= ep - p)
181bb58b617SHajimu UMEMOTO 			return "?";
182bb58b617SHajimu UMEMOTO 		p += n;
1837d56d374SYoshinobu Inoue 	}
1847d56d374SYoshinobu Inoue 	if (!first || hours) {
1857d56d374SYoshinobu Inoue 		first = 0;
186bb58b617SHajimu UMEMOTO 		n = snprintf(p, ep - p, "%dh", hours);
187bb58b617SHajimu UMEMOTO 		if (n < 0 || n >= ep - p)
188bb58b617SHajimu UMEMOTO 			return "?";
189bb58b617SHajimu UMEMOTO 		p += n;
1907d56d374SYoshinobu Inoue 	}
1917d56d374SYoshinobu Inoue 	if (!first || mins) {
1927d56d374SYoshinobu Inoue 		first = 0;
193bb58b617SHajimu UMEMOTO 		n = snprintf(p, ep - p, "%dm", mins);
194bb58b617SHajimu UMEMOTO 		if (n < 0 || n >= ep - p)
195bb58b617SHajimu UMEMOTO 			return "?";
196bb58b617SHajimu UMEMOTO 		p += n;
1977d56d374SYoshinobu Inoue 	}
198bb58b617SHajimu UMEMOTO 	snprintf(p, ep - p, "%ds", secs);
199db82af41SHiroki Sato 
2007d56d374SYoshinobu Inoue 	return (result);
2017d56d374SYoshinobu Inoue }
202