xref: /freebsd/sbin/routed/trace.c (revision da5432eda807c4b7232d030d5157d5b417ea4f52)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
47b6ab19dSGarrett Wollman  * Copyright (c) 1983, 1988, 1993
57b6ab19dSGarrett Wollman  *	The Regents of the University of California.  All rights reserved.
67b6ab19dSGarrett Wollman  *
77b6ab19dSGarrett Wollman  * Redistribution and use in source and binary forms, with or without
87b6ab19dSGarrett Wollman  * modification, are permitted provided that the following conditions
97b6ab19dSGarrett Wollman  * are met:
107b6ab19dSGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
117b6ab19dSGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
127b6ab19dSGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
137b6ab19dSGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
147b6ab19dSGarrett Wollman  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
167b6ab19dSGarrett Wollman  *    may be used to endorse or promote products derived from this software
177b6ab19dSGarrett Wollman  *    without specific prior written permission.
187b6ab19dSGarrett Wollman  *
197b6ab19dSGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
207b6ab19dSGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
217b6ab19dSGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
227b6ab19dSGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
237b6ab19dSGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
247b6ab19dSGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
257b6ab19dSGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
267b6ab19dSGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
277b6ab19dSGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
287b6ab19dSGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
297b6ab19dSGarrett Wollman  * SUCH DAMAGE.
307b6ab19dSGarrett Wollman  */
317b6ab19dSGarrett Wollman 
327b6ab19dSGarrett Wollman #define	RIPCMDS
337b6ab19dSGarrett Wollman #include "defs.h"
347b6ab19dSGarrett Wollman #include "pathnames.h"
357b6ab19dSGarrett Wollman #include <sys/stat.h>
367b6ab19dSGarrett Wollman #include <sys/signal.h>
377b6ab19dSGarrett Wollman #include <fcntl.h>
387b6ab19dSGarrett Wollman 
397b6ab19dSGarrett Wollman 
4071965874SGarrett Wollman int	tracelevel, new_tracelevel;
4198815cc7SPeter Wemm FILE	*ftrace;			/* output trace file */
422c7a9781SMark Murray static const char *sigtrace_pat = "%s";
43534734edSWarner Losh static char savetracename[PATH_MAX];
44534734edSWarner Losh char	inittracename[PATH_MAX];
45f2ca6d8aSPoul-Henning Kamp static int file_trace;			/* 1=tracing to file, not stdout */
467b6ab19dSGarrett Wollman 
477b6ab19dSGarrett Wollman static void trace_dump(void);
482c7a9781SMark Murray static void tmsg(const char *, ...) PATTRIB(1,2);
497b6ab19dSGarrett Wollman 
507b6ab19dSGarrett Wollman 
51d5b718b3SGarrett Wollman /* convert string to printable characters
52d5b718b3SGarrett Wollman  */
53d5b718b3SGarrett Wollman static char *
qstring(u_char * s,int len)54d5b718b3SGarrett Wollman qstring(u_char *s, int len)
55d5b718b3SGarrett Wollman {
56d5b718b3SGarrett Wollman 	static char buf[8*20+1];
57d5b718b3SGarrett Wollman 	char *p;
58d5b718b3SGarrett Wollman 	u_char *s2, c;
59d5b718b3SGarrett Wollman 
60d5b718b3SGarrett Wollman 
61d5b718b3SGarrett Wollman 	for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
62d5b718b3SGarrett Wollman 		c = *s++;
63d5b718b3SGarrett Wollman 		if (c == '\0') {
64d5b718b3SGarrett Wollman 			for (s2 = s+1; s2 < &s[len]; s2++) {
65d5b718b3SGarrett Wollman 				if (*s2 != '\0')
66d5b718b3SGarrett Wollman 					break;
67d5b718b3SGarrett Wollman 			}
68d5b718b3SGarrett Wollman 			if (s2 >= &s[len])
69d5b718b3SGarrett Wollman 			    goto exit;
70d5b718b3SGarrett Wollman 		}
71d5b718b3SGarrett Wollman 
72d5b718b3SGarrett Wollman 		if (c >= ' ' && c < 0x7f && c != '\\') {
73d5b718b3SGarrett Wollman 			*p++ = c;
74d5b718b3SGarrett Wollman 			continue;
75d5b718b3SGarrett Wollman 		}
76d5b718b3SGarrett Wollman 		*p++ = '\\';
77d5b718b3SGarrett Wollman 		switch (c) {
78d5b718b3SGarrett Wollman 		case '\\':
79d5b718b3SGarrett Wollman 			*p++ = '\\';
80d5b718b3SGarrett Wollman 			break;
81d5b718b3SGarrett Wollman 		case '\n':
82d5b718b3SGarrett Wollman 			*p++= 'n';
83d5b718b3SGarrett Wollman 			break;
84d5b718b3SGarrett Wollman 		case '\r':
85d5b718b3SGarrett Wollman 			*p++= 'r';
86d5b718b3SGarrett Wollman 			break;
87d5b718b3SGarrett Wollman 		case '\t':
88d5b718b3SGarrett Wollman 			*p++ = 't';
89d5b718b3SGarrett Wollman 			break;
90d5b718b3SGarrett Wollman 		case '\b':
91d5b718b3SGarrett Wollman 			*p++ = 'b';
92d5b718b3SGarrett Wollman 			break;
93d5b718b3SGarrett Wollman 		default:
94d5b718b3SGarrett Wollman 			p += sprintf(p,"%o",c);
95d5b718b3SGarrett Wollman 			break;
96d5b718b3SGarrett Wollman 		}
97d5b718b3SGarrett Wollman 	}
98d5b718b3SGarrett Wollman exit:
99d5b718b3SGarrett Wollman 	*p = '\0';
100d5b718b3SGarrett Wollman 	return buf;
101d5b718b3SGarrett Wollman }
102d5b718b3SGarrett Wollman 
103d5b718b3SGarrett Wollman 
1047b6ab19dSGarrett Wollman /* convert IP address to a string, but not into a single buffer
1057b6ab19dSGarrett Wollman  */
1067b6ab19dSGarrett Wollman char *
naddr_ntoa(naddr a)1077b6ab19dSGarrett Wollman naddr_ntoa(naddr a)
1087b6ab19dSGarrett Wollman {
1097b6ab19dSGarrett Wollman #define NUM_BUFS 4
1107b6ab19dSGarrett Wollman 	static int bufno;
1117b6ab19dSGarrett Wollman 	static struct {
1127b6ab19dSGarrett Wollman 	    char    str[16];		/* xxx.xxx.xxx.xxx\0 */
1137b6ab19dSGarrett Wollman 	} bufs[NUM_BUFS];
1147b6ab19dSGarrett Wollman 	char *s;
1157b6ab19dSGarrett Wollman 	struct in_addr addr;
1167b6ab19dSGarrett Wollman 
1177b6ab19dSGarrett Wollman 	addr.s_addr = a;
1187b6ab19dSGarrett Wollman 	s = strcpy(bufs[bufno].str, inet_ntoa(addr));
1197b6ab19dSGarrett Wollman 	bufno = (bufno+1) % NUM_BUFS;
1207b6ab19dSGarrett Wollman 	return s;
1217b6ab19dSGarrett Wollman #undef NUM_BUFS
1227b6ab19dSGarrett Wollman }
1237b6ab19dSGarrett Wollman 
1247b6ab19dSGarrett Wollman 
1252c7a9781SMark Murray const char *
saddr_ntoa(struct sockaddr * sa)1267b6ab19dSGarrett Wollman saddr_ntoa(struct sockaddr *sa)
1277b6ab19dSGarrett Wollman {
128562c5a82SPedro F. Giffuni 	return (sa == NULL) ? "?" : naddr_ntoa(S_ADDR(sa));
1297b6ab19dSGarrett Wollman }
1307b6ab19dSGarrett Wollman 
1317b6ab19dSGarrett Wollman 
1327b6ab19dSGarrett Wollman static char *
ts(time_t secs)1337b6ab19dSGarrett Wollman ts(time_t secs) {
1347b6ab19dSGarrett Wollman 	static char s[20];
1357b6ab19dSGarrett Wollman 
1367b6ab19dSGarrett Wollman 	secs += epoch.tv_sec;
1372c7a9781SMark Murray 	memcpy(s, ctime(&secs)+11, 8);
1387b6ab19dSGarrett Wollman 	s[8] = '\0';
1397b6ab19dSGarrett Wollman 	return s;
1407b6ab19dSGarrett Wollman }
1417b6ab19dSGarrett Wollman 
1427b6ab19dSGarrett Wollman 
1437b6ab19dSGarrett Wollman /* On each event, display a time stamp.
1447b6ab19dSGarrett Wollman  * This assumes that 'now' is update once for each event, and
1457b6ab19dSGarrett Wollman  * that at least now.tv_usec changes.
1467b6ab19dSGarrett Wollman  */
14771965874SGarrett Wollman static struct timeval lastlog_time;
14871965874SGarrett Wollman 
1497b6ab19dSGarrett Wollman void
lastlog(void)1507b6ab19dSGarrett Wollman lastlog(void)
1517b6ab19dSGarrett Wollman {
15271965874SGarrett Wollman 	if (lastlog_time.tv_sec != now.tv_sec
15371965874SGarrett Wollman 	    || lastlog_time.tv_usec != now.tv_usec) {
1547b6ab19dSGarrett Wollman 		(void)fprintf(ftrace, "-- %s --\n", ts(now.tv_sec));
15571965874SGarrett Wollman 		lastlog_time = now;
1567b6ab19dSGarrett Wollman 	}
1577b6ab19dSGarrett Wollman }
1587b6ab19dSGarrett Wollman 
1597b6ab19dSGarrett Wollman 
1607b6ab19dSGarrett Wollman static void
tmsg(const char * p,...)1612c7a9781SMark Murray tmsg(const char *p, ...)
1627b6ab19dSGarrett Wollman {
1637b6ab19dSGarrett Wollman 	va_list args;
1647b6ab19dSGarrett Wollman 
165562c5a82SPedro F. Giffuni 	if (ftrace != NULL) {
1667b6ab19dSGarrett Wollman 		lastlog();
1677b6ab19dSGarrett Wollman 		va_start(args, p);
1687b6ab19dSGarrett Wollman 		vfprintf(ftrace, p, args);
169caa79e36SBruce M Simpson 		va_end(args);
1702c7a9781SMark Murray 		(void)fputc('\n',ftrace);
1717b6ab19dSGarrett Wollman 		fflush(ftrace);
1727b6ab19dSGarrett Wollman 	}
1737b6ab19dSGarrett Wollman }
1747b6ab19dSGarrett Wollman 
1757b6ab19dSGarrett Wollman 
1762c7a9781SMark Murray void
trace_close(int zap_stdio)1772c7a9781SMark Murray trace_close(int zap_stdio)
1787b6ab19dSGarrett Wollman {
1797b6ab19dSGarrett Wollman 	int fd;
1807b6ab19dSGarrett Wollman 
1817b6ab19dSGarrett Wollman 
1827b6ab19dSGarrett Wollman 	fflush(stdout);
1837b6ab19dSGarrett Wollman 	fflush(stderr);
1847b6ab19dSGarrett Wollman 
185562c5a82SPedro F. Giffuni 	if (ftrace != NULL && zap_stdio) {
18671965874SGarrett Wollman 		if (ftrace != stdout)
18771965874SGarrett Wollman 			fclose(ftrace);
188562c5a82SPedro F. Giffuni 		ftrace = NULL;
1897b6ab19dSGarrett Wollman 		fd = open(_PATH_DEVNULL, O_RDWR);
190e09d48f6SPedro F. Giffuni 		if (fd < 0)
191e09d48f6SPedro F. Giffuni 			return;
1922c7a9781SMark Murray 		if (isatty(STDIN_FILENO))
19371965874SGarrett Wollman 			(void)dup2(fd, STDIN_FILENO);
1942c7a9781SMark Murray 		if (isatty(STDOUT_FILENO))
1957b6ab19dSGarrett Wollman 			(void)dup2(fd, STDOUT_FILENO);
1962c7a9781SMark Murray 		if (isatty(STDERR_FILENO))
1977b6ab19dSGarrett Wollman 			(void)dup2(fd, STDERR_FILENO);
1987b6ab19dSGarrett Wollman 		(void)close(fd);
1997b6ab19dSGarrett Wollman 	}
20071965874SGarrett Wollman 	lastlog_time.tv_sec = 0;
2017b6ab19dSGarrett Wollman }
2027b6ab19dSGarrett Wollman 
2037b6ab19dSGarrett Wollman 
2047b6ab19dSGarrett Wollman void
trace_flush(void)2057b6ab19dSGarrett Wollman trace_flush(void)
2067b6ab19dSGarrett Wollman {
207562c5a82SPedro F. Giffuni 	if (ftrace != NULL) {
2087b6ab19dSGarrett Wollman 		fflush(ftrace);
2097b6ab19dSGarrett Wollman 		if (ferror(ftrace))
2102c7a9781SMark Murray 			trace_off("tracing off: %s", strerror(ferror(ftrace)));
2117b6ab19dSGarrett Wollman 	}
2127b6ab19dSGarrett Wollman }
2137b6ab19dSGarrett Wollman 
2147b6ab19dSGarrett Wollman 
2157b6ab19dSGarrett Wollman void
trace_off(const char * p,...)2162c7a9781SMark Murray trace_off(const char *p, ...)
2177b6ab19dSGarrett Wollman {
2187b6ab19dSGarrett Wollman 	va_list args;
2197b6ab19dSGarrett Wollman 
2207b6ab19dSGarrett Wollman 
221562c5a82SPedro F. Giffuni 	if (ftrace != NULL) {
2227b6ab19dSGarrett Wollman 		lastlog();
2237b6ab19dSGarrett Wollman 		va_start(args, p);
2247b6ab19dSGarrett Wollman 		vfprintf(ftrace, p, args);
225caa79e36SBruce M Simpson 		va_end(args);
2262c7a9781SMark Murray 		(void)fputc('\n',ftrace);
2277b6ab19dSGarrett Wollman 	}
2282c7a9781SMark Murray 	trace_close(file_trace);
2297b6ab19dSGarrett Wollman 
2307b6ab19dSGarrett Wollman 	new_tracelevel = tracelevel = 0;
2317b6ab19dSGarrett Wollman }
2327b6ab19dSGarrett Wollman 
2337b6ab19dSGarrett Wollman 
23471965874SGarrett Wollman /* log a change in tracing
2357b6ab19dSGarrett Wollman  */
2367b6ab19dSGarrett Wollman void
tracelevel_msg(const char * pat,int dump)2372c7a9781SMark Murray tracelevel_msg(const char *pat,
23871965874SGarrett Wollman 	       int dump)		/* -1=no dump, 0=default, 1=force */
2397b6ab19dSGarrett Wollman {
240a3e04ddcSPoul-Henning Kamp 	static const char * const off_msgs[MAX_TRACELEVEL] = {
2417b6ab19dSGarrett Wollman 		"Tracing actions stopped",
2427b6ab19dSGarrett Wollman 		"Tracing packets stopped",
2437b6ab19dSGarrett Wollman 		"Tracing packet contents stopped",
2447b6ab19dSGarrett Wollman 		"Tracing kernel changes stopped",
2457b6ab19dSGarrett Wollman 	};
246a3e04ddcSPoul-Henning Kamp 	static const char * const on_msgs[MAX_TRACELEVEL] = {
2477b6ab19dSGarrett Wollman 		"Tracing actions started",
2487b6ab19dSGarrett Wollman 		"Tracing packets started",
2497b6ab19dSGarrett Wollman 		"Tracing packet contents started",
2507b6ab19dSGarrett Wollman 		"Tracing kernel changes started",
2517b6ab19dSGarrett Wollman 	};
25271965874SGarrett Wollman 	u_int old_tracelevel = tracelevel;
2537b6ab19dSGarrett Wollman 
2547b6ab19dSGarrett Wollman 
25571965874SGarrett Wollman 	if (new_tracelevel < 0)
25671965874SGarrett Wollman 		new_tracelevel = 0;
25771965874SGarrett Wollman 	else if (new_tracelevel > MAX_TRACELEVEL)
2587b6ab19dSGarrett Wollman 		new_tracelevel = MAX_TRACELEVEL;
25971965874SGarrett Wollman 
26071965874SGarrett Wollman 	if (new_tracelevel < tracelevel) {
26171965874SGarrett Wollman 		if (new_tracelevel <= 0) {
26271965874SGarrett Wollman 			trace_off(pat, off_msgs[0]);
26371965874SGarrett Wollman 		} else do {
26471965874SGarrett Wollman 			tmsg(pat, off_msgs[tracelevel]);
26571965874SGarrett Wollman 		}
26671965874SGarrett Wollman 		while (--tracelevel != new_tracelevel);
26771965874SGarrett Wollman 
26871965874SGarrett Wollman 	} else if (new_tracelevel > tracelevel) {
26971965874SGarrett Wollman 		do {
27071965874SGarrett Wollman 			tmsg(pat, on_msgs[tracelevel++]);
27171965874SGarrett Wollman 		} while (tracelevel != new_tracelevel);
27271965874SGarrett Wollman 	}
27371965874SGarrett Wollman 
27471965874SGarrett Wollman 	if (dump > 0
27571965874SGarrett Wollman 	    || (dump == 0 && old_tracelevel == 0 && tracelevel != 0))
27671965874SGarrett Wollman 		trace_dump();
27771965874SGarrett Wollman }
27871965874SGarrett Wollman 
27971965874SGarrett Wollman 
28071965874SGarrett Wollman void
set_tracefile(const char * filename,const char * pat,int dump)2812c7a9781SMark Murray set_tracefile(const char *filename,
2822c7a9781SMark Murray 	      const char *pat,
28371965874SGarrett Wollman 	      int dump)			/* -1=no dump, 0=default, 1=force */
28471965874SGarrett Wollman {
28571965874SGarrett Wollman 	struct stat stbuf;
28671965874SGarrett Wollman 	FILE *n_ftrace;
2872c7a9781SMark Murray 	const char *fn;
28871965874SGarrett Wollman 
28971965874SGarrett Wollman 
29071965874SGarrett Wollman 	/* Allow a null filename to increase the level if the trace file
29171965874SGarrett Wollman 	 * is already open or if coming from a trusted source, such as
29271965874SGarrett Wollman 	 * a signal or the command line.
29371965874SGarrett Wollman 	 */
294562c5a82SPedro F. Giffuni 	if (filename == NULL || filename[0] == '\0') {
295562c5a82SPedro F. Giffuni 		filename = NULL;
296562c5a82SPedro F. Giffuni 		if (ftrace == NULL) {
29771965874SGarrett Wollman 			if (inittracename[0] == '\0') {
29871965874SGarrett Wollman 				msglog("missing trace file name");
2997b6ab19dSGarrett Wollman 				return;
3007b6ab19dSGarrett Wollman 			}
30171965874SGarrett Wollman 			fn = inittracename;
30271965874SGarrett Wollman 		} else {
303562c5a82SPedro F. Giffuni 			fn = NULL;
3047b6ab19dSGarrett Wollman 		}
305d5b718b3SGarrett Wollman 
30671965874SGarrett Wollman 	} else if (!strcmp(filename,"dump/../table")) {
30771965874SGarrett Wollman 		trace_dump();
30871965874SGarrett Wollman 		return;
30971965874SGarrett Wollman 
3107b6ab19dSGarrett Wollman 	} else {
31171965874SGarrett Wollman 		/* Allow the file specified with "-T file" to be reopened,
31271965874SGarrett Wollman 		 * but require all other names specified over the net to
31371965874SGarrett Wollman 		 * match the official path.  The path can specify a directory
31471965874SGarrett Wollman 		 * in which the file is to be created.
31571965874SGarrett Wollman 		 */
31671965874SGarrett Wollman 		if (strcmp(filename, inittracename)
31771965874SGarrett Wollman #ifdef _PATH_TRACE
31871965874SGarrett Wollman 		    && (strncmp(filename, _PATH_TRACE, sizeof(_PATH_TRACE)-1)
31971965874SGarrett Wollman 			|| strstr(filename,"../")
32071965874SGarrett Wollman 			|| 0 > stat(_PATH_TRACE, &stbuf))
32171965874SGarrett Wollman #endif
32271965874SGarrett Wollman 		    ) {
32371965874SGarrett Wollman 			msglog("wrong trace file \"%s\"", filename);
32471965874SGarrett Wollman 			return;
3257b6ab19dSGarrett Wollman 		}
32671965874SGarrett Wollman 
32771965874SGarrett Wollman 		/* If the new tracefile exists, it must be a regular file.
32871965874SGarrett Wollman 		 */
3292c7a9781SMark Murray 		if (stat(filename, &stbuf) >= 0 && !S_ISREG(stbuf.st_mode)) {
33071965874SGarrett Wollman 			msglog("wrong type (%#x) of trace file \"%s\"",
33171965874SGarrett Wollman 			       stbuf.st_mode, filename);
33271965874SGarrett Wollman 			return;
3337b6ab19dSGarrett Wollman 		}
33471965874SGarrett Wollman 
33571965874SGarrett Wollman 		fn = filename;
3367b6ab19dSGarrett Wollman 	}
33771965874SGarrett Wollman 
338562c5a82SPedro F. Giffuni 	if (fn != NULL) {
33971965874SGarrett Wollman 		n_ftrace = fopen(fn, "a");
340562c5a82SPedro F. Giffuni 		if (n_ftrace == NULL) {
34171965874SGarrett Wollman 			msglog("failed to open trace file \"%s\" %s",
34271965874SGarrett Wollman 			       fn, strerror(errno));
34371965874SGarrett Wollman 			if (fn == inittracename)
34471965874SGarrett Wollman 				inittracename[0] = '\0';
34571965874SGarrett Wollman 			return;
34671965874SGarrett Wollman 		}
34771965874SGarrett Wollman 
3482c7a9781SMark Murray 		tmsg("switch to trace file %s", fn);
34971965874SGarrett Wollman 
3502c7a9781SMark Murray 		trace_close(file_trace = 1);
35171965874SGarrett Wollman 
35271965874SGarrett Wollman 		if (fn != savetracename)
35371965874SGarrett Wollman 			strncpy(savetracename, fn, sizeof(savetracename)-1);
35471965874SGarrett Wollman 		ftrace = n_ftrace;
35571965874SGarrett Wollman 
35671965874SGarrett Wollman 		fflush(stdout);
35771965874SGarrett Wollman 		fflush(stderr);
35871965874SGarrett Wollman 		dup2(fileno(ftrace), STDOUT_FILENO);
35971965874SGarrett Wollman 		dup2(fileno(ftrace), STDERR_FILENO);
36071965874SGarrett Wollman 	}
36171965874SGarrett Wollman 
362562c5a82SPedro F. Giffuni 	if (new_tracelevel == 0 || filename == NULL)
36371965874SGarrett Wollman 		new_tracelevel++;
364562c5a82SPedro F. Giffuni 	tracelevel_msg(pat, dump != 0 ? dump : (filename != NULL));
36571965874SGarrett Wollman }
36671965874SGarrett Wollman 
36771965874SGarrett Wollman 
36871965874SGarrett Wollman /* ARGSUSED */
36971965874SGarrett Wollman void
sigtrace_on(int s UNUSED)3702c7a9781SMark Murray sigtrace_on(int s UNUSED)
37171965874SGarrett Wollman {
37271965874SGarrett Wollman 	new_tracelevel++;
3732c7a9781SMark Murray 	sigtrace_pat = "SIGUSR1: %s";
37471965874SGarrett Wollman }
37571965874SGarrett Wollman 
37671965874SGarrett Wollman 
37771965874SGarrett Wollman /* ARGSUSED */
37871965874SGarrett Wollman void
sigtrace_off(int s UNUSED)3792c7a9781SMark Murray sigtrace_off(int s UNUSED)
38071965874SGarrett Wollman {
38171965874SGarrett Wollman 	new_tracelevel--;
3822c7a9781SMark Murray 	sigtrace_pat = "SIGUSR2: %s";
38371965874SGarrett Wollman }
38471965874SGarrett Wollman 
38571965874SGarrett Wollman 
38671965874SGarrett Wollman /* Set tracing after a signal.
38771965874SGarrett Wollman  */
38871965874SGarrett Wollman void
set_tracelevel(void)38971965874SGarrett Wollman set_tracelevel(void)
39071965874SGarrett Wollman {
39171965874SGarrett Wollman 	if (new_tracelevel == tracelevel)
39271965874SGarrett Wollman 		return;
39371965874SGarrett Wollman 
39471965874SGarrett Wollman 	/* If tracing entirely off, and there was no tracefile specified
39571965874SGarrett Wollman 	 * on the command line, then leave it off.
39671965874SGarrett Wollman 	 */
397562c5a82SPedro F. Giffuni 	if (new_tracelevel > tracelevel && ftrace == NULL) {
39871965874SGarrett Wollman 		if (savetracename[0] != '\0') {
39971965874SGarrett Wollman 			set_tracefile(savetracename,sigtrace_pat,0);
40071965874SGarrett Wollman 		} else if (inittracename[0] != '\0') {
40171965874SGarrett Wollman 				set_tracefile(inittracename,sigtrace_pat,0);
40271965874SGarrett Wollman 		} else {
40371965874SGarrett Wollman 			new_tracelevel = 0;
40471965874SGarrett Wollman 			return;
40571965874SGarrett Wollman 		}
40671965874SGarrett Wollman 	} else {
40771965874SGarrett Wollman 		tracelevel_msg(sigtrace_pat, 0);
40871965874SGarrett Wollman 	}
4097b6ab19dSGarrett Wollman }
4107b6ab19dSGarrett Wollman 
4117b6ab19dSGarrett Wollman 
4127b6ab19dSGarrett Wollman /* display an address
4137b6ab19dSGarrett Wollman  */
4147b6ab19dSGarrett Wollman char *
addrname(naddr addr,naddr mask,int force)4157b6ab19dSGarrett Wollman addrname(naddr	addr,			/* in network byte order */
4167b6ab19dSGarrett Wollman 	 naddr	mask,
4177b6ab19dSGarrett Wollman 	 int	force)			/* 0=show mask if nonstandard, */
4187b6ab19dSGarrett Wollman {					/*	1=always show mask, 2=never */
4197b6ab19dSGarrett Wollman #define NUM_BUFS 4
4207b6ab19dSGarrett Wollman 	static int bufno;
4217b6ab19dSGarrett Wollman 	static struct {
4227b6ab19dSGarrett Wollman 	    char    str[15+20];
4237b6ab19dSGarrett Wollman 	} bufs[NUM_BUFS];
4247b6ab19dSGarrett Wollman 	char *s, *sp;
4257b6ab19dSGarrett Wollman 	naddr dmask;
426e09d48f6SPedro F. Giffuni 	size_t l;
4277b6ab19dSGarrett Wollman 	int i;
4287b6ab19dSGarrett Wollman 
429e09d48f6SPedro F. Giffuni 	strlcpy(bufs[bufno].str, naddr_ntoa(addr), sizeof(bufs[bufno].str));
430e09d48f6SPedro F. Giffuni 	s = bufs[bufno].str;
431e09d48f6SPedro F. Giffuni 	l = sizeof(bufs[bufno].str);
4327b6ab19dSGarrett Wollman 	bufno = (bufno+1) % NUM_BUFS;
4337b6ab19dSGarrett Wollman 
4347b6ab19dSGarrett Wollman 	if (force == 1 || (force == 0 && mask != std_mask(addr))) {
4357b6ab19dSGarrett Wollman 		sp = &s[strlen(s)];
4367b6ab19dSGarrett Wollman 
4377b6ab19dSGarrett Wollman 		dmask = mask & -mask;
4387b6ab19dSGarrett Wollman 		if (mask + dmask == 0) {
4397b6ab19dSGarrett Wollman 			for (i = 0; i != 32 && ((1<<i) & mask) == 0; i++)
4407b6ab19dSGarrett Wollman 				continue;
441e09d48f6SPedro F. Giffuni 			(void)snprintf(sp, s + l - sp, "/%d", 32-i);
4427b6ab19dSGarrett Wollman 
4437b6ab19dSGarrett Wollman 		} else {
444e09d48f6SPedro F. Giffuni 			(void)snprintf(sp, s + l - sp, " (mask %#x)",
445e09d48f6SPedro F. Giffuni 			    (u_int)mask);
4467b6ab19dSGarrett Wollman 		}
4477b6ab19dSGarrett Wollman 	}
4487b6ab19dSGarrett Wollman 
4497b6ab19dSGarrett Wollman 	return s;
4507b6ab19dSGarrett Wollman #undef NUM_BUFS
4517b6ab19dSGarrett Wollman }
4527b6ab19dSGarrett Wollman 
4537b6ab19dSGarrett Wollman 
4547b6ab19dSGarrett Wollman /* display a bit-field
4557b6ab19dSGarrett Wollman  */
4567b6ab19dSGarrett Wollman struct bits {
4572c7a9781SMark Murray 	u_int	bits_mask;
4582c7a9781SMark Murray 	u_int	bits_clear;
4592c7a9781SMark Murray 	const char *bits_name;
4607b6ab19dSGarrett Wollman };
4617b6ab19dSGarrett Wollman 
462a3e04ddcSPoul-Henning Kamp static const struct bits if_bits[] = {
4637b6ab19dSGarrett Wollman 	{ IFF_LOOPBACK,		0,		"LOOPBACK" },
4647b6ab19dSGarrett Wollman 	{ IFF_POINTOPOINT,	0,		"PT-TO-PT" },
4657b6ab19dSGarrett Wollman 	{ 0,			0,		0}
4667b6ab19dSGarrett Wollman };
4677b6ab19dSGarrett Wollman 
468a3e04ddcSPoul-Henning Kamp static const struct bits is_bits[] = {
469d5b718b3SGarrett Wollman 	{ IS_ALIAS,		0,		"ALIAS" },
4707b6ab19dSGarrett Wollman 	{ IS_SUBNET,		0,		"" },
471d5b718b3SGarrett Wollman 	{ IS_REMOTE,		(IS_NO_RDISC
472d5b718b3SGarrett Wollman 				 | IS_BCAST_RDISC), "REMOTE" },
4737b6ab19dSGarrett Wollman 	{ IS_PASSIVE,		(IS_NO_RDISC
4747b6ab19dSGarrett Wollman 				 | IS_NO_RIP
4757b6ab19dSGarrett Wollman 				 | IS_NO_SUPER_AG
4767b6ab19dSGarrett Wollman 				 | IS_PM_RDISC
4777b6ab19dSGarrett Wollman 				 | IS_NO_AG),	"PASSIVE" },
4787b6ab19dSGarrett Wollman 	{ IS_EXTERNAL,		0,		"EXTERNAL" },
4797b6ab19dSGarrett Wollman 	{ IS_CHECKED,		0,		"" },
4807b6ab19dSGarrett Wollman 	{ IS_ALL_HOSTS,		0,		"" },
4817b6ab19dSGarrett Wollman 	{ IS_ALL_ROUTERS,	0,		"" },
482d5b718b3SGarrett Wollman 	{ IS_DISTRUST,		0,		"DISTRUST" },
4837b6ab19dSGarrett Wollman 	{ IS_BROKE,		IS_SICK,	"BROKEN" },
4847b6ab19dSGarrett Wollman 	{ IS_SICK,		0,		"SICK" },
485d5b718b3SGarrett Wollman 	{ IS_DUP,		0,		"DUPLICATE" },
48671965874SGarrett Wollman 	{ IS_REDIRECT_OK,	0,		"REDIRECT_OK" },
4877b6ab19dSGarrett Wollman 	{ IS_NEED_NET_SYN,	0,		"" },
4887b6ab19dSGarrett Wollman 	{ IS_NO_AG,		IS_NO_SUPER_AG,	"NO_AG" },
4897b6ab19dSGarrett Wollman 	{ IS_NO_SUPER_AG,	0,		"NO_SUPER_AG" },
4907b6ab19dSGarrett Wollman 	{ (IS_NO_RIPV1_IN
4917b6ab19dSGarrett Wollman 	   | IS_NO_RIPV2_IN
4927b6ab19dSGarrett Wollman 	   | IS_NO_RIPV1_OUT
4937b6ab19dSGarrett Wollman 	   | IS_NO_RIPV2_OUT),	0,		"NO_RIP" },
4947b6ab19dSGarrett Wollman 	{ (IS_NO_RIPV1_IN
4957b6ab19dSGarrett Wollman 	   | IS_NO_RIPV1_OUT),	0,		"RIPV2" },
4967b6ab19dSGarrett Wollman 	{ IS_NO_RIPV1_IN,	0,		"NO_RIPV1_IN" },
4977b6ab19dSGarrett Wollman 	{ IS_NO_RIPV2_IN,	0,		"NO_RIPV2_IN" },
4987b6ab19dSGarrett Wollman 	{ IS_NO_RIPV1_OUT,	0,		"NO_RIPV1_OUT" },
4997b6ab19dSGarrett Wollman 	{ IS_NO_RIPV2_OUT,	0,		"NO_RIPV2_OUT" },
5007b6ab19dSGarrett Wollman 	{ (IS_NO_ADV_IN
5017b6ab19dSGarrett Wollman 	   | IS_NO_SOL_OUT
5027b6ab19dSGarrett Wollman 	   | IS_NO_ADV_OUT),	IS_BCAST_RDISC,	"NO_RDISC" },
5037b6ab19dSGarrett Wollman 	{ IS_NO_SOL_OUT,	0,		"NO_SOLICIT" },
5047b6ab19dSGarrett Wollman 	{ IS_SOL_OUT,		0,		"SEND_SOLICIT" },
5057b6ab19dSGarrett Wollman 	{ IS_NO_ADV_OUT,	IS_BCAST_RDISC,	"NO_RDISC_ADV" },
5067b6ab19dSGarrett Wollman 	{ IS_ADV_OUT,		0,		"RDISC_ADV" },
5077b6ab19dSGarrett Wollman 	{ IS_BCAST_RDISC,	0,		"BCAST_RDISC" },
508d5b718b3SGarrett Wollman 	{ IS_PM_RDISC,		0,		"" },
5097b6ab19dSGarrett Wollman 	{ 0,			0,		"%#x"}
5107b6ab19dSGarrett Wollman };
5117b6ab19dSGarrett Wollman 
512a3e04ddcSPoul-Henning Kamp static const struct bits rs_bits[] = {
5137b6ab19dSGarrett Wollman 	{ RS_IF,		0,		"IF" },
5147b6ab19dSGarrett Wollman 	{ RS_NET_INT,		RS_NET_SYN,	"NET_INT" },
5157b6ab19dSGarrett Wollman 	{ RS_NET_SYN,		0,		"NET_SYN" },
5167b6ab19dSGarrett Wollman 	{ RS_SUBNET,		0,		"" },
5177b6ab19dSGarrett Wollman 	{ RS_LOCAL,		0,		"LOCAL" },
5187b6ab19dSGarrett Wollman 	{ RS_MHOME,		0,		"MHOME" },
5197b6ab19dSGarrett Wollman 	{ RS_STATIC,		0,		"STATIC" },
5207b6ab19dSGarrett Wollman 	{ RS_RDISC,		0,		"RDISC" },
5217b6ab19dSGarrett Wollman 	{ 0,			0,		"%#x"}
5227b6ab19dSGarrett Wollman };
5237b6ab19dSGarrett Wollman 
5247b6ab19dSGarrett Wollman 
5257b6ab19dSGarrett Wollman static void
trace_bits(const struct bits * tbl,u_int field,int force)5262c7a9781SMark Murray trace_bits(const struct bits *tbl,
5277b6ab19dSGarrett Wollman 	   u_int field,
5287b6ab19dSGarrett Wollman 	   int force)
5297b6ab19dSGarrett Wollman {
5302c7a9781SMark Murray 	u_int b;
5317b6ab19dSGarrett Wollman 	char c;
5327b6ab19dSGarrett Wollman 
5337b6ab19dSGarrett Wollman 	if (force) {
5347b6ab19dSGarrett Wollman 		(void)putc('<', ftrace);
5357b6ab19dSGarrett Wollman 		c = 0;
5367b6ab19dSGarrett Wollman 	} else {
5377b6ab19dSGarrett Wollman 		c = '<';
5387b6ab19dSGarrett Wollman 	}
5397b6ab19dSGarrett Wollman 
5407b6ab19dSGarrett Wollman 	while (field != 0
5417b6ab19dSGarrett Wollman 	       && (b = tbl->bits_mask) != 0) {
5427b6ab19dSGarrett Wollman 		if ((b & field) == b) {
5437b6ab19dSGarrett Wollman 			if (tbl->bits_name[0] != '\0') {
5447b6ab19dSGarrett Wollman 				if (c)
5457b6ab19dSGarrett Wollman 					(void)putc(c, ftrace);
5467b6ab19dSGarrett Wollman 				(void)fprintf(ftrace, "%s", tbl->bits_name);
5477b6ab19dSGarrett Wollman 				c = '|';
5487b6ab19dSGarrett Wollman 			}
5497b6ab19dSGarrett Wollman 			if (0 == (field &= ~(b | tbl->bits_clear)))
5507b6ab19dSGarrett Wollman 				break;
5517b6ab19dSGarrett Wollman 		}
5527b6ab19dSGarrett Wollman 		tbl++;
5537b6ab19dSGarrett Wollman 	}
554562c5a82SPedro F. Giffuni 	if (field != 0 && tbl->bits_name != NULL) {
5557b6ab19dSGarrett Wollman 		if (c)
5567b6ab19dSGarrett Wollman 			(void)putc(c, ftrace);
5577b6ab19dSGarrett Wollman 		(void)fprintf(ftrace, tbl->bits_name, field);
5587b6ab19dSGarrett Wollman 		c = '|';
5597b6ab19dSGarrett Wollman 	}
5607b6ab19dSGarrett Wollman 
5617b6ab19dSGarrett Wollman 	if (c != '<' || force)
5627b6ab19dSGarrett Wollman 		(void)fputs("> ", ftrace);
5637b6ab19dSGarrett Wollman }
5647b6ab19dSGarrett Wollman 
5657b6ab19dSGarrett Wollman 
5662c7a9781SMark Murray char *
rtname(naddr dst,naddr mask,naddr gate)5672c7a9781SMark Murray rtname(naddr dst,
5687b6ab19dSGarrett Wollman        naddr mask,
5692c7a9781SMark Murray        naddr gate)
5707b6ab19dSGarrett Wollman {
5717b6ab19dSGarrett Wollman 	static char buf[3*4+3+1+2+3	/* "xxx.xxx.xxx.xxx/xx-->" */
5727b6ab19dSGarrett Wollman 			+3*4+3+1];	/* "xxx.xxx.xxx.xxx" */
5737b6ab19dSGarrett Wollman 	int i;
5747b6ab19dSGarrett Wollman 
5757b6ab19dSGarrett Wollman 	i = sprintf(buf, "%-16s-->", addrname(dst, mask, 0));
5762c7a9781SMark Murray 	(void)sprintf(&buf[i], "%-*s", 15+20-MAX(20,i), naddr_ntoa(gate));
5777b6ab19dSGarrett Wollman 	return buf;
5787b6ab19dSGarrett Wollman }
5797b6ab19dSGarrett Wollman 
5807b6ab19dSGarrett Wollman 
58171965874SGarrett Wollman static void
print_rts(struct rt_spare * rts,int force_metric,int force_ifp,int force_router,int force_tag,int force_time)58271965874SGarrett Wollman print_rts(struct rt_spare *rts,
58371965874SGarrett Wollman 	  int force_metric,		/* -1=suppress, 0=default */
58471965874SGarrett Wollman 	  int force_ifp,		/* -1=suppress, 0=default */
58571965874SGarrett Wollman 	  int force_router,		/* -1=suppress, 0=default, 1=display */
58671965874SGarrett Wollman 	  int force_tag,		/* -1=suppress, 0=default, 1=display */
58771965874SGarrett Wollman 	  int force_time)		/* 0=suppress, 1=display */
58871965874SGarrett Wollman {
5892c7a9781SMark Murray 	int i;
5902c7a9781SMark Murray 
5912c7a9781SMark Murray 
59271965874SGarrett Wollman 	if (force_metric >= 0)
59371965874SGarrett Wollman 		(void)fprintf(ftrace, "metric=%-2d ", rts->rts_metric);
59471965874SGarrett Wollman 	if (force_ifp >= 0)
595562c5a82SPedro F. Giffuni 		(void)fprintf(ftrace, "%s ", (rts->rts_ifp == NULL ?
59671965874SGarrett Wollman 					      "if?" : rts->rts_ifp->int_name));
59771965874SGarrett Wollman 	if (force_router > 0
59871965874SGarrett Wollman 	    || (force_router == 0 && rts->rts_router != rts->rts_gate))
59971965874SGarrett Wollman 		(void)fprintf(ftrace, "router=%s ",
60071965874SGarrett Wollman 			      naddr_ntoa(rts->rts_router));
60171965874SGarrett Wollman 	if (force_time > 0)
60271965874SGarrett Wollman 		(void)fprintf(ftrace, "%s ", ts(rts->rts_time));
60371965874SGarrett Wollman 	if (force_tag > 0
60471965874SGarrett Wollman 	    || (force_tag == 0 && rts->rts_tag != 0))
6052c7a9781SMark Murray 		(void)fprintf(ftrace, "tag=%#x ", ntohs(rts->rts_tag));
6062c7a9781SMark Murray 	if (rts->rts_de_ag != 0) {
6072c7a9781SMark Murray 		for (i = 1; (u_int)(1 << i) <= rts->rts_de_ag; i++)
6082c7a9781SMark Murray 			continue;
6092c7a9781SMark Murray 		(void)fprintf(ftrace, "de_ag=%d ", i);
6102c7a9781SMark Murray 	}
6112c7a9781SMark Murray 
61271965874SGarrett Wollman }
61371965874SGarrett Wollman 
61471965874SGarrett Wollman 
6157b6ab19dSGarrett Wollman void
trace_if(const char * act,struct interface * ifp)6162c7a9781SMark Murray trace_if(const char *act,
6177b6ab19dSGarrett Wollman 	 struct interface *ifp)
6187b6ab19dSGarrett Wollman {
619562c5a82SPedro F. Giffuni 	if (!TRACEACTIONS || ftrace == NULL)
6207b6ab19dSGarrett Wollman 		return;
6217b6ab19dSGarrett Wollman 
6227b6ab19dSGarrett Wollman 	lastlog();
623d5b718b3SGarrett Wollman 	(void)fprintf(ftrace, "%-3s interface %-4s ", act, ifp->int_name);
6247b6ab19dSGarrett Wollman 	(void)fprintf(ftrace, "%-15s-->%-15s ",
6257b6ab19dSGarrett Wollman 		      naddr_ntoa(ifp->int_addr),
626d5b718b3SGarrett Wollman 		      addrname(((ifp->int_if_flags & IFF_POINTOPOINT)
6277b6ab19dSGarrett Wollman 				? ifp->int_dstaddr
628d5b718b3SGarrett Wollman 				: htonl(ifp->int_net)),
6297b6ab19dSGarrett Wollman 			       ifp->int_mask, 1));
6307b6ab19dSGarrett Wollman 	if (ifp->int_metric != 0)
6317b6ab19dSGarrett Wollman 		(void)fprintf(ftrace, "metric=%d ", ifp->int_metric);
632caa79e36SBruce M Simpson 	if (ifp->int_adj_inmetric != 0)
633caa79e36SBruce M Simpson 		(void)fprintf(ftrace, "adj_inmetric=%u ",
634caa79e36SBruce M Simpson 			      ifp->int_adj_inmetric);
635caa79e36SBruce M Simpson 	if (ifp->int_adj_outmetric != 0)
636caa79e36SBruce M Simpson 		(void)fprintf(ftrace, "adj_outmetric=%u ",
637caa79e36SBruce M Simpson 			      ifp->int_adj_outmetric);
638d5b718b3SGarrett Wollman 	if (!IS_RIP_OUT_OFF(ifp->int_state)
639d5b718b3SGarrett Wollman 	    && ifp->int_d_metric != 0)
640caa79e36SBruce M Simpson 		(void)fprintf(ftrace, "fake_default=%u ", ifp->int_d_metric);
6417b6ab19dSGarrett Wollman 	trace_bits(if_bits, ifp->int_if_flags, 0);
6427b6ab19dSGarrett Wollman 	trace_bits(is_bits, ifp->int_state, 0);
6437b6ab19dSGarrett Wollman 	(void)fputc('\n',ftrace);
6447b6ab19dSGarrett Wollman }
6457b6ab19dSGarrett Wollman 
6467b6ab19dSGarrett Wollman 
6477b6ab19dSGarrett Wollman void
trace_upslot(struct rt_entry * rt,struct rt_spare * rts,struct rt_spare * new)6487b6ab19dSGarrett Wollman trace_upslot(struct rt_entry *rt,
6497b6ab19dSGarrett Wollman 	     struct rt_spare *rts,
6502c7a9781SMark Murray 	     struct rt_spare *new)
6517b6ab19dSGarrett Wollman {
652562c5a82SPedro F. Giffuni 	if (!TRACEACTIONS || ftrace == NULL)
6537b6ab19dSGarrett Wollman 		return;
65471965874SGarrett Wollman 
6552c7a9781SMark Murray 	if (rts->rts_gate == new->rts_gate
6562c7a9781SMark Murray 	    && rts->rts_router == new->rts_router
6572c7a9781SMark Murray 	    && rts->rts_metric == new->rts_metric
6582c7a9781SMark Murray 	    && rts->rts_tag == new->rts_tag
6592c7a9781SMark Murray 	    && rts->rts_de_ag == new->rts_de_ag)
6607b6ab19dSGarrett Wollman 		return;
6617b6ab19dSGarrett Wollman 
6627b6ab19dSGarrett Wollman 	lastlog();
6632c7a9781SMark Murray 	if (new->rts_gate == 0) {
6642c7a9781SMark Murray 		(void)fprintf(ftrace, "Del #%d %-35s ",
6652c7a9781SMark Murray 			      (int)(rts - rt->rt_spares),
6662c7a9781SMark Murray 			      rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
6672c7a9781SMark Murray 		print_rts(rts, 0,0,0,0,
6682c7a9781SMark Murray 			  (rts != rt->rt_spares
6692c7a9781SMark Murray 			   || AGE_RT(rt->rt_state,new->rts_ifp)));
6702c7a9781SMark Murray 
6712c7a9781SMark Murray 	} else if (rts->rts_gate != RIP_DEFAULT) {
6727b6ab19dSGarrett Wollman 		(void)fprintf(ftrace, "Chg #%d %-35s ",
6732c7a9781SMark Murray 			      (int)(rts - rt->rt_spares),
6742c7a9781SMark Murray 			      rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
67571965874SGarrett Wollman 		print_rts(rts, 0,0,
6762c7a9781SMark Murray 			  rts->rts_gate != new->rts_gate,
6772c7a9781SMark Murray 			  rts->rts_tag != new->rts_tag,
67871965874SGarrett Wollman 			  rts != rt->rt_spares || AGE_RT(rt->rt_state,
67971965874SGarrett Wollman 							rt->rt_ifp));
6807b6ab19dSGarrett Wollman 
68171965874SGarrett Wollman 		(void)fprintf(ftrace, "\n       %19s%-16s ", "",
6822c7a9781SMark Murray 			      (new->rts_gate != rts->rts_gate
6832c7a9781SMark Murray 			       ? naddr_ntoa(new->rts_gate) : ""));
6842c7a9781SMark Murray 		print_rts(new,
6852c7a9781SMark Murray 			  -(new->rts_metric == rts->rts_metric),
6862c7a9781SMark Murray 			  -(new->rts_ifp == rts->rts_ifp),
68771965874SGarrett Wollman 			  0,
6882c7a9781SMark Murray 			  rts->rts_tag != new->rts_tag,
6892c7a9781SMark Murray 			  (new->rts_time != rts->rts_time
6902c7a9781SMark Murray 			   && (rts != rt->rt_spares
6912c7a9781SMark Murray 			       || AGE_RT(rt->rt_state, new->rts_ifp))));
6927b6ab19dSGarrett Wollman 
6937b6ab19dSGarrett Wollman 	} else {
6947b6ab19dSGarrett Wollman 		(void)fprintf(ftrace, "Add #%d %-35s ",
6952c7a9781SMark Murray 			      (int)(rts - rt->rt_spares),
6962c7a9781SMark Murray 			      rtname(rt->rt_dst, rt->rt_mask, new->rts_gate));
6972c7a9781SMark Murray 		print_rts(new, 0,0,0,0,
6982c7a9781SMark Murray 			  (rts != rt->rt_spares
6992c7a9781SMark Murray 			   || AGE_RT(rt->rt_state,new->rts_ifp)));
7007b6ab19dSGarrett Wollman 	}
70171965874SGarrett Wollman 	(void)fputc('\n',ftrace);
7027b6ab19dSGarrett Wollman }
7037b6ab19dSGarrett Wollman 
7047b6ab19dSGarrett Wollman 
7052c7a9781SMark Murray /* miscellaneous message checked by the caller
7067b6ab19dSGarrett Wollman  */
7077b6ab19dSGarrett Wollman void
trace_misc(const char * p,...)7082c7a9781SMark Murray trace_misc(const char *p, ...)
7097b6ab19dSGarrett Wollman {
7107b6ab19dSGarrett Wollman 	va_list args;
7117b6ab19dSGarrett Wollman 
712562c5a82SPedro F. Giffuni 	if (ftrace == NULL)
7137b6ab19dSGarrett Wollman 		return;
7147b6ab19dSGarrett Wollman 
7157b6ab19dSGarrett Wollman 	lastlog();
7167b6ab19dSGarrett Wollman 	va_start(args, p);
7177b6ab19dSGarrett Wollman 	vfprintf(ftrace, p, args);
718caa79e36SBruce M Simpson 	va_end(args);
7192c7a9781SMark Murray 	(void)fputc('\n',ftrace);
7207b6ab19dSGarrett Wollman }
7217b6ab19dSGarrett Wollman 
7227b6ab19dSGarrett Wollman 
7237b6ab19dSGarrett Wollman /* display a message if tracing actions
7247b6ab19dSGarrett Wollman  */
7257b6ab19dSGarrett Wollman void
trace_act(const char * p,...)7262c7a9781SMark Murray trace_act(const char *p, ...)
7277b6ab19dSGarrett Wollman {
7287b6ab19dSGarrett Wollman 	va_list args;
7297b6ab19dSGarrett Wollman 
730562c5a82SPedro F. Giffuni 	if (!TRACEACTIONS || ftrace == NULL)
7317b6ab19dSGarrett Wollman 		return;
7327b6ab19dSGarrett Wollman 
7337b6ab19dSGarrett Wollman 	lastlog();
7347b6ab19dSGarrett Wollman 	va_start(args, p);
7357b6ab19dSGarrett Wollman 	vfprintf(ftrace, p, args);
736caa79e36SBruce M Simpson 	va_end(args);
737d5b718b3SGarrett Wollman 	(void)fputc('\n',ftrace);
7387b6ab19dSGarrett Wollman }
7397b6ab19dSGarrett Wollman 
7407b6ab19dSGarrett Wollman 
7417b6ab19dSGarrett Wollman /* display a message if tracing packets
7427b6ab19dSGarrett Wollman  */
7437b6ab19dSGarrett Wollman void
trace_pkt(const char * p,...)7442c7a9781SMark Murray trace_pkt(const char *p, ...)
7457b6ab19dSGarrett Wollman {
7467b6ab19dSGarrett Wollman 	va_list args;
7477b6ab19dSGarrett Wollman 
748562c5a82SPedro F. Giffuni 	if (!TRACEPACKETS || ftrace == NULL)
7497b6ab19dSGarrett Wollman 		return;
7507b6ab19dSGarrett Wollman 
7517b6ab19dSGarrett Wollman 	lastlog();
7527b6ab19dSGarrett Wollman 	va_start(args, p);
7537b6ab19dSGarrett Wollman 	vfprintf(ftrace, p, args);
754caa79e36SBruce M Simpson 	va_end(args);
755d5b718b3SGarrett Wollman 	(void)fputc('\n',ftrace);
7567b6ab19dSGarrett Wollman }
7577b6ab19dSGarrett Wollman 
7587b6ab19dSGarrett Wollman 
7597b6ab19dSGarrett Wollman void
trace_change(struct rt_entry * rt,u_int state,struct rt_spare * new,const char * label)7607b6ab19dSGarrett Wollman trace_change(struct rt_entry *rt,
7617b6ab19dSGarrett Wollman 	     u_int	state,
7622c7a9781SMark Murray 	     struct	rt_spare *new,
7632c7a9781SMark Murray 	     const char	*label)
7647b6ab19dSGarrett Wollman {
765562c5a82SPedro F. Giffuni 	if (ftrace == NULL)
7667b6ab19dSGarrett Wollman 		return;
7677b6ab19dSGarrett Wollman 
7682c7a9781SMark Murray 	if (rt->rt_metric == new->rts_metric
7692c7a9781SMark Murray 	    && rt->rt_gate == new->rts_gate
7702c7a9781SMark Murray 	    && rt->rt_router == new->rts_router
7717b6ab19dSGarrett Wollman 	    && rt->rt_state == state
7722c7a9781SMark Murray 	    && rt->rt_tag == new->rts_tag
7732c7a9781SMark Murray 	    && rt->rt_de_ag == new->rts_de_ag)
7747b6ab19dSGarrett Wollman 		return;
7757b6ab19dSGarrett Wollman 
7767b6ab19dSGarrett Wollman 	lastlog();
77771965874SGarrett Wollman 	(void)fprintf(ftrace, "%s %-35s ",
7787b6ab19dSGarrett Wollman 		      label,
7792c7a9781SMark Murray 		      rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
78071965874SGarrett Wollman 	print_rts(rt->rt_spares,
78171965874SGarrett Wollman 		  0,0,0,0, AGE_RT(rt->rt_state, rt->rt_ifp));
7827b6ab19dSGarrett Wollman 	trace_bits(rs_bits, rt->rt_state, rt->rt_state != state);
7837b6ab19dSGarrett Wollman 
78471965874SGarrett Wollman 	(void)fprintf(ftrace, "\n%*s %19s%-16s ",
7852c7a9781SMark Murray 		      (int)strlen(label), "", "",
7862c7a9781SMark Murray 		      (rt->rt_gate != new->rts_gate
7872c7a9781SMark Murray 		       ? naddr_ntoa(new->rts_gate) : ""));
7882c7a9781SMark Murray 	print_rts(new,
7892c7a9781SMark Murray 		  -(new->rts_metric == rt->rt_metric),
7902c7a9781SMark Murray 		  -(new->rts_ifp == rt->rt_ifp),
79171965874SGarrett Wollman 		  0,
7922c7a9781SMark Murray 		  rt->rt_tag != new->rts_tag,
7932c7a9781SMark Murray 		  (rt->rt_time != new->rts_time
7942c7a9781SMark Murray 		   && AGE_RT(rt->rt_state,new->rts_ifp)));
7957b6ab19dSGarrett Wollman 	if (rt->rt_state != state)
7967b6ab19dSGarrett Wollman 		trace_bits(rs_bits, state, 1);
79771965874SGarrett Wollman 	(void)fputc('\n',ftrace);
7987b6ab19dSGarrett Wollman }
7997b6ab19dSGarrett Wollman 
8007b6ab19dSGarrett Wollman 
8017b6ab19dSGarrett Wollman void
trace_add_del(const char * action,struct rt_entry * rt)8022c7a9781SMark Murray trace_add_del(const char * action, struct rt_entry *rt)
8037b6ab19dSGarrett Wollman {
804562c5a82SPedro F. Giffuni 	if (ftrace == NULL)
8057b6ab19dSGarrett Wollman 		return;
8067b6ab19dSGarrett Wollman 
8077b6ab19dSGarrett Wollman 	lastlog();
80871965874SGarrett Wollman 	(void)fprintf(ftrace, "%s    %-35s ",
8097b6ab19dSGarrett Wollman 		      action,
8102c7a9781SMark Murray 		      rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
81171965874SGarrett Wollman 	print_rts(rt->rt_spares, 0,0,0,0,AGE_RT(rt->rt_state,rt->rt_ifp));
81271965874SGarrett Wollman 	trace_bits(rs_bits, rt->rt_state, 0);
81371965874SGarrett Wollman 	(void)fputc('\n',ftrace);
8147b6ab19dSGarrett Wollman }
8157b6ab19dSGarrett Wollman 
8167b6ab19dSGarrett Wollman 
8177b6ab19dSGarrett Wollman /* ARGSUSED */
8187b6ab19dSGarrett Wollman static int
walk_trace(struct radix_node * rn,struct walkarg * w UNUSED)8197b6ab19dSGarrett Wollman walk_trace(struct radix_node *rn,
8202c7a9781SMark Murray 	   struct walkarg *w UNUSED)
8217b6ab19dSGarrett Wollman {
8227b6ab19dSGarrett Wollman #define RT ((struct rt_entry *)rn)
8237b6ab19dSGarrett Wollman 	struct rt_spare *rts;
8242c7a9781SMark Murray 	int i;
8257b6ab19dSGarrett Wollman 
8262c7a9781SMark Murray 	(void)fprintf(ftrace, "  %-35s ",
8272c7a9781SMark Murray 		      rtname(RT->rt_dst, RT->rt_mask, RT->rt_gate));
8282c7a9781SMark Murray 	print_rts(&RT->rt_spares[0], 0,0,0,0, AGE_RT(RT->rt_state, RT->rt_ifp));
8297b6ab19dSGarrett Wollman 	trace_bits(rs_bits, RT->rt_state, 0);
83071965874SGarrett Wollman 	if (RT->rt_poison_time >= now_garbage
83171965874SGarrett Wollman 	    && RT->rt_poison_metric < RT->rt_metric)
83271965874SGarrett Wollman 		(void)fprintf(ftrace, "pm=%d@%s",
8332c7a9781SMark Murray 			      RT->rt_poison_metric, ts(RT->rt_poison_time));
8347b6ab19dSGarrett Wollman 
8357b6ab19dSGarrett Wollman 	rts = &RT->rt_spares[1];
8367b6ab19dSGarrett Wollman 	for (i = 1; i < NUM_SPARES; i++, rts++) {
83771965874SGarrett Wollman 		if (rts->rts_gate != RIP_DEFAULT) {
83871965874SGarrett Wollman 			(void)fprintf(ftrace,"\n    #%d%15s%-16s ",
83971965874SGarrett Wollman 				      i, "", naddr_ntoa(rts->rts_gate));
84071965874SGarrett Wollman 			print_rts(rts, 0,0,0,0,1);
8417b6ab19dSGarrett Wollman 		}
8427b6ab19dSGarrett Wollman 	}
8437b6ab19dSGarrett Wollman 	(void)fputc('\n',ftrace);
8447b6ab19dSGarrett Wollman 
8457b6ab19dSGarrett Wollman 	return 0;
8467b6ab19dSGarrett Wollman }
8477b6ab19dSGarrett Wollman 
8487b6ab19dSGarrett Wollman 
8497b6ab19dSGarrett Wollman static void
trace_dump(void)8507b6ab19dSGarrett Wollman trace_dump(void)
8517b6ab19dSGarrett Wollman {
852d5b718b3SGarrett Wollman 	struct interface *ifp;
853d5b718b3SGarrett Wollman 
854562c5a82SPedro F. Giffuni 	if (ftrace == NULL)
8557b6ab19dSGarrett Wollman 		return;
8567b6ab19dSGarrett Wollman 	lastlog();
8577b6ab19dSGarrett Wollman 
85871965874SGarrett Wollman 	(void)fputs("current daemon state:\n", ftrace);
85938ae6523SPoul-Henning Kamp 	LIST_FOREACH(ifp, &ifnet, int_list)
860d5b718b3SGarrett Wollman 		trace_if("", ifp);
8617b6ab19dSGarrett Wollman 	(void)rn_walktree(rhead, walk_trace, 0);
8627b6ab19dSGarrett Wollman }
8637b6ab19dSGarrett Wollman 
8647b6ab19dSGarrett Wollman 
8657b6ab19dSGarrett Wollman void
trace_rip(const char * dir1,const char * dir2,struct sockaddr_in * who,struct interface * ifp,struct rip * msg,int size)8662c7a9781SMark Murray trace_rip(const char *dir1, const char *dir2,
8677b6ab19dSGarrett Wollman 	  struct sockaddr_in *who,
8687b6ab19dSGarrett Wollman 	  struct interface *ifp,
8697b6ab19dSGarrett Wollman 	  struct rip *msg,
8707b6ab19dSGarrett Wollman 	  int size)			/* total size of message */
8717b6ab19dSGarrett Wollman {
8727b6ab19dSGarrett Wollman 	struct netinfo *n, *lim;
8732c7a9781SMark Murray #	define NA ((struct netauth*)n)
874d5b718b3SGarrett Wollman 	int i, seen_route;
8757b6ab19dSGarrett Wollman 
876562c5a82SPedro F. Giffuni 	if (!TRACEPACKETS || ftrace == NULL)
8777b6ab19dSGarrett Wollman 		return;
8787b6ab19dSGarrett Wollman 
8797b6ab19dSGarrett Wollman 	lastlog();
8807b6ab19dSGarrett Wollman 	if (msg->rip_cmd >= RIPCMD_MAX
8817b6ab19dSGarrett Wollman 	    || msg->rip_vers == 0) {
8827b6ab19dSGarrett Wollman 		(void)fprintf(ftrace, "%s bad RIPv%d cmd=%d %s"
8837b6ab19dSGarrett Wollman 			      " %s.%d size=%d\n",
8847b6ab19dSGarrett Wollman 			      dir1, msg->rip_vers, msg->rip_cmd, dir2,
8857b6ab19dSGarrett Wollman 			      naddr_ntoa(who->sin_addr.s_addr),
8867b6ab19dSGarrett Wollman 			      ntohs(who->sin_port),
8877b6ab19dSGarrett Wollman 			      size);
8887b6ab19dSGarrett Wollman 		return;
8897b6ab19dSGarrett Wollman 	}
8907b6ab19dSGarrett Wollman 
8917b6ab19dSGarrett Wollman 	(void)fprintf(ftrace, "%s RIPv%d %s %s %s.%d%s%s\n",
8927b6ab19dSGarrett Wollman 		      dir1, msg->rip_vers, ripcmds[msg->rip_cmd], dir2,
8937b6ab19dSGarrett Wollman 		      naddr_ntoa(who->sin_addr.s_addr), ntohs(who->sin_port),
8947b6ab19dSGarrett Wollman 		      ifp ? " via " : "", ifp ? ifp->int_name : "");
8957b6ab19dSGarrett Wollman 	if (!TRACECONTENTS)
8967b6ab19dSGarrett Wollman 		return;
8977b6ab19dSGarrett Wollman 
898d5b718b3SGarrett Wollman 	seen_route = 0;
8997b6ab19dSGarrett Wollman 	switch (msg->rip_cmd) {
9007b6ab19dSGarrett Wollman 	case RIPCMD_REQUEST:
9017b6ab19dSGarrett Wollman 	case RIPCMD_RESPONSE:
9027b6ab19dSGarrett Wollman 		n = msg->rip_nets;
9037b6ab19dSGarrett Wollman 		lim = (struct netinfo *)((char*)msg + size);
9047b6ab19dSGarrett Wollman 		for (; n < lim; n++) {
905d5b718b3SGarrett Wollman 			if (!seen_route
906d5b718b3SGarrett Wollman 			    && n->n_family == RIP_AF_UNSPEC
9077b6ab19dSGarrett Wollman 			    && ntohl(n->n_metric) == HOPCNT_INFINITY
908d5b718b3SGarrett Wollman 			    && msg->rip_cmd == RIPCMD_REQUEST
909d5b718b3SGarrett Wollman 			    && (n+1 == lim
910d5b718b3SGarrett Wollman 				|| (n+2 == lim
911d5b718b3SGarrett Wollman 				    && (n+1)->n_family == RIP_AF_AUTH))) {
9127b6ab19dSGarrett Wollman 				(void)fputs("\tQUERY ", ftrace);
9137b6ab19dSGarrett Wollman 				if (n->n_dst != 0)
9147b6ab19dSGarrett Wollman 					(void)fprintf(ftrace, "%s ",
9157b6ab19dSGarrett Wollman 						      naddr_ntoa(n->n_dst));
9167b6ab19dSGarrett Wollman 				if (n->n_mask != 0)
9177b6ab19dSGarrett Wollman 					(void)fprintf(ftrace, "mask=%#x ",
9187b6ab19dSGarrett Wollman 						      (u_int)ntohl(n->n_mask));
9197b6ab19dSGarrett Wollman 				if (n->n_nhop != 0)
9207b6ab19dSGarrett Wollman 					(void)fprintf(ftrace, "nhop=%s ",
9217b6ab19dSGarrett Wollman 						      naddr_ntoa(n->n_nhop));
9227b6ab19dSGarrett Wollman 				if (n->n_tag != 0)
9237b6ab19dSGarrett Wollman 					(void)fprintf(ftrace, "tag=%#x ",
9247b6ab19dSGarrett Wollman 						      ntohs(n->n_tag));
9257b6ab19dSGarrett Wollman 				(void)fputc('\n',ftrace);
9267b6ab19dSGarrett Wollman 				continue;
9277b6ab19dSGarrett Wollman 			}
9287b6ab19dSGarrett Wollman 
9297b6ab19dSGarrett Wollman 			if (n->n_family == RIP_AF_AUTH) {
930d5b718b3SGarrett Wollman 				if (NA->a_type == RIP_AUTH_PW
931d5b718b3SGarrett Wollman 				    && n == msg->rip_nets) {
932d5b718b3SGarrett Wollman 					(void)fprintf(ftrace, "\tPassword"
933d5b718b3SGarrett Wollman 						      " Authentication:"
934d5b718b3SGarrett Wollman 						      " \"%s\"\n",
935d5b718b3SGarrett Wollman 						      qstring(NA->au.au_pw,
936d5b718b3SGarrett Wollman 							  RIP_AUTH_PW_LEN));
937d5b718b3SGarrett Wollman 					continue;
938d5b718b3SGarrett Wollman 				}
939d5b718b3SGarrett Wollman 
940d5b718b3SGarrett Wollman 				if (NA->a_type == RIP_AUTH_MD5
941d5b718b3SGarrett Wollman 				    && n == msg->rip_nets) {
9427b6ab19dSGarrett Wollman 					(void)fprintf(ftrace,
9432c7a9781SMark Murray 						      "\tMD5 Auth"
9442c7a9781SMark Murray 						      " pkt_len=%d KeyID=%u"
9452c7a9781SMark Murray 						      " auth_len=%d"
9462c7a9781SMark Murray 						      " seqno=%#x"
947d5b718b3SGarrett Wollman 						      " rsvd=%#x,%#x\n",
9482c7a9781SMark Murray 					    ntohs(NA->au.a_md5.md5_pkt_len),
949d5b718b3SGarrett Wollman 					    NA->au.a_md5.md5_keyid,
9502c7a9781SMark Murray 					    NA->au.a_md5.md5_auth_len,
9512c7a9781SMark Murray 					    (int)ntohl(NA->au.a_md5.md5_seqno),
9522c7a9781SMark Murray 					    (int)ntohs(NA->au.a_md5.rsvd[0]),
9532c7a9781SMark Murray 					    (int)ntohs(NA->au.a_md5.rsvd[1]));
954d5b718b3SGarrett Wollman 					continue;
955d5b718b3SGarrett Wollman 				}
956d5b718b3SGarrett Wollman 				(void)fprintf(ftrace,
9572c7a9781SMark Murray 					      "\tAuthentication type %d: ",
958d5b718b3SGarrett Wollman 					      ntohs(NA->a_type));
9597b6ab19dSGarrett Wollman 				for (i = 0;
9602c7a9781SMark Murray 				     i < (int)sizeof(NA->au.au_pw);
9617b6ab19dSGarrett Wollman 				     i++)
9627b6ab19dSGarrett Wollman 					(void)fprintf(ftrace, "%02x ",
963d5b718b3SGarrett Wollman 						      NA->au.au_pw[i]);
9647b6ab19dSGarrett Wollman 				(void)fputc('\n',ftrace);
9657b6ab19dSGarrett Wollman 				continue;
9667b6ab19dSGarrett Wollman 			}
9677b6ab19dSGarrett Wollman 
968d5b718b3SGarrett Wollman 			seen_route = 1;
9697b6ab19dSGarrett Wollman 			if (n->n_family != RIP_AF_INET) {
9707b6ab19dSGarrett Wollman 				(void)fprintf(ftrace,
9717b6ab19dSGarrett Wollman 					      "\t(af %d) %-18s mask=%#x ",
9727b6ab19dSGarrett Wollman 					      ntohs(n->n_family),
9737b6ab19dSGarrett Wollman 					      naddr_ntoa(n->n_dst),
9747b6ab19dSGarrett Wollman 					      (u_int)ntohl(n->n_mask));
9757b6ab19dSGarrett Wollman 			} else if (msg->rip_vers == RIPv1) {
9767b6ab19dSGarrett Wollman 				(void)fprintf(ftrace, "\t%-18s ",
9777b6ab19dSGarrett Wollman 					      addrname(n->n_dst,
9787b6ab19dSGarrett Wollman 						       ntohl(n->n_mask),
9797b6ab19dSGarrett Wollman 						       n->n_mask==0 ? 2 : 1));
9807b6ab19dSGarrett Wollman 			} else {
9817b6ab19dSGarrett Wollman 				(void)fprintf(ftrace, "\t%-18s ",
9827b6ab19dSGarrett Wollman 					      addrname(n->n_dst,
9837b6ab19dSGarrett Wollman 						       ntohl(n->n_mask),
9847b6ab19dSGarrett Wollman 						       n->n_mask==0 ? 2 : 0));
9857b6ab19dSGarrett Wollman 			}
9867b6ab19dSGarrett Wollman 			(void)fprintf(ftrace, "metric=%-2d ",
9877b6ab19dSGarrett Wollman 				      (u_int)ntohl(n->n_metric));
9887b6ab19dSGarrett Wollman 			if (n->n_nhop != 0)
9897b6ab19dSGarrett Wollman 				(void)fprintf(ftrace, " nhop=%s ",
9907b6ab19dSGarrett Wollman 					      naddr_ntoa(n->n_nhop));
9917b6ab19dSGarrett Wollman 			if (n->n_tag != 0)
9927b6ab19dSGarrett Wollman 				(void)fprintf(ftrace, "tag=%#x",
9937b6ab19dSGarrett Wollman 					      ntohs(n->n_tag));
9947b6ab19dSGarrett Wollman 			(void)fputc('\n',ftrace);
9957b6ab19dSGarrett Wollman 		}
9967b6ab19dSGarrett Wollman 		if (size != (char *)n - (char *)msg)
9977b6ab19dSGarrett Wollman 			(void)fprintf(ftrace, "truncated record, len %d\n",
9987b6ab19dSGarrett Wollman 				size);
9997b6ab19dSGarrett Wollman 		break;
10007b6ab19dSGarrett Wollman 
10017b6ab19dSGarrett Wollman 	case RIPCMD_TRACEON:
100271965874SGarrett Wollman 		fprintf(ftrace, "\tfile=\"%.*s\"\n", size-4,
100371965874SGarrett Wollman 			msg->rip_tracefile);
10047b6ab19dSGarrett Wollman 		break;
10057b6ab19dSGarrett Wollman 
10067b6ab19dSGarrett Wollman 	case RIPCMD_TRACEOFF:
10077b6ab19dSGarrett Wollman 		break;
10087b6ab19dSGarrett Wollman 	}
10097b6ab19dSGarrett Wollman }
1010