xref: /netbsd-src/sbin/routed/trace.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: trace.c,v 1.32 2009/04/06 12:36:27 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgment:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #define	RIPCMDS
37 #include "defs.h"
38 #include "pathnames.h"
39 #include <sys/stat.h>
40 #include <sys/signal.h>
41 #include <fcntl.h>
42 
43 #ifdef __NetBSD__
44 __RCSID("$NetBSD: trace.c,v 1.32 2009/04/06 12:36:27 lukem Exp $");
45 #elif defined(__FreeBSD__)
46 __RCSID("$FreeBSD$");
47 #else
48 __RCSID("Revision: 2.27 ");
49 #ident "Revision: 2.27 "
50 #endif
51 
52 
53 #ifdef sgi
54 /* use *stat64 for files on large filesystems */
55 #define stat	stat64
56 #endif
57 
58 #define	NRECORDS	50		/* size of circular trace buffer */
59 
60 int	tracelevel, new_tracelevel;
61 FILE	*ftrace = stdout;		/* output trace file */
62 static const char *sigtrace_pat = "%s";
63 static char savetracename[MAXPATHLEN+1];
64 char	inittracename[MAXPATHLEN+1];
65 int	file_trace;			/* 1=tracing to file, not stdout */
66 
67 static void trace_dump(void);
68 static void tmsg(const char *, ...) PATTRIB(1,2);
69 
70 
71 /* convert string to printable characters
72  */
73 static char *
74 qstring(u_char *s, int len)
75 {
76 	static char buf[8*20+1];
77 	char *p;
78 	u_char *s2, c;
79 	int n;
80 
81 	for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
82 		c = *s++;
83 		if (c == '\0') {
84 			for (s2 = s+1; s2 < &s[len]; s2++) {
85 				if (*s2 != '\0')
86 					break;
87 			}
88 			if (s2 >= &s[len])
89 			    goto exit;
90 		}
91 
92 		if (c >= ' ' && c < 0x7f && c != '\\') {
93 			*p++ = c;
94 			continue;
95 		}
96 		*p++ = '\\';
97 		switch (c) {
98 		case '\\':
99 			*p++ = '\\';
100 			break;
101 		case '\n':
102 			*p++= 'n';
103 			break;
104 		case '\r':
105 			*p++= 'r';
106 			break;
107 		case '\t':
108 			*p++ = 't';
109 			break;
110 		case '\b':
111 			*p++ = 'b';
112 			break;
113 		default:
114 			n = snprintf(p, sizeof(buf) - (p - buf), "%o", c);
115 			if (n <= 0)
116 				goto exit;
117 			p += n;
118 			break;
119 		}
120 	}
121 exit:
122 	*p = '\0';
123 	return buf;
124 }
125 
126 
127 /* convert IP address to a string, but not into a single buffer
128  */
129 char *
130 naddr_ntoa(naddr a)
131 {
132 #define NUM_BUFS 4
133 	static int bufno;
134 	static struct {
135 	    char    str[16];		/* xxx.xxx.xxx.xxx\0 */
136 	} bufs[NUM_BUFS];
137 	char *s;
138 	struct in_addr addr;
139 
140 	addr.s_addr = a;
141 	strlcpy(bufs[bufno].str, inet_ntoa(addr), sizeof(bufs[bufno].str));
142 	s = bufs[bufno].str;
143 	bufno = (bufno+1) % NUM_BUFS;
144 	return s;
145 #undef NUM_BUFS
146 }
147 
148 
149 const char *
150 saddr_ntoa(const struct sockaddr *sa)
151 {
152 	return (sa == 0) ? "?" : naddr_ntoa(S_ADDR(sa));
153 }
154 
155 
156 static char *
157 ts(time_t secs) {
158 	static char s[20];
159 
160 	secs += epoch.tv_sec;
161 #ifdef sgi
162 	(void)cftime(s, "%T", &secs);
163 #else
164 	memcpy(s, ctime(&secs)+11, 8);
165 	s[8] = '\0';
166 #endif
167 	return s;
168 }
169 
170 
171 /* On each event, display a time stamp.
172  * This assumes that 'now' is update once for each event, and
173  * that at least now.tv_usec changes.
174  */
175 static struct timeval lastlog_time;
176 
177 void
178 lastlog(void)
179 {
180 	if (lastlog_time.tv_sec != now.tv_sec
181 	    || lastlog_time.tv_usec != now.tv_usec) {
182 		(void)fprintf(ftrace, "-- %s --\n", ts(now.tv_sec));
183 		lastlog_time = now;
184 	}
185 }
186 
187 
188 static void
189 tmsg(const char *p, ...)
190 {
191 	va_list args;
192 
193 	if (ftrace != 0) {
194 		lastlog();
195 		va_start(args, p);
196 		vfprintf(ftrace, p, args);
197 		va_end(args);
198 		(void)fputc('\n',ftrace);
199 		fflush(ftrace);
200 	}
201 }
202 
203 
204 void
205 trace_close(int zap_stdio)
206 {
207 	int fd;
208 
209 
210 	fflush(stdout);
211 	fflush(stderr);
212 
213 	if (ftrace != 0 && zap_stdio) {
214 		if (ftrace != stdout)
215 			fclose(ftrace);
216 		ftrace = 0;
217 		fd = open(_PATH_DEVNULL, O_RDWR);
218 		if (fd == -1)
219 			return;
220 		if (isatty(STDIN_FILENO))
221 			(void)dup2(fd, STDIN_FILENO);
222 		if (isatty(STDOUT_FILENO))
223 			(void)dup2(fd, STDOUT_FILENO);
224 		if (isatty(STDERR_FILENO))
225 			(void)dup2(fd, STDERR_FILENO);
226 		(void)close(fd);
227 	}
228 	lastlog_time.tv_sec = 0;
229 }
230 
231 
232 void
233 trace_flush(void)
234 {
235 	if (ftrace != 0) {
236 		fflush(ftrace);
237 		if (ferror(ftrace))
238 			trace_off("tracing off: %s", strerror(ferror(ftrace)));
239 	}
240 }
241 
242 
243 void
244 trace_off(const char *p, ...)
245 {
246 	va_list args;
247 
248 
249 	if (ftrace != 0) {
250 		lastlog();
251 		va_start(args, p);
252 		vfprintf(ftrace, p, args);
253 		va_end(args);
254 		(void)fputc('\n',ftrace);
255 	}
256 	trace_close(file_trace);
257 
258 	new_tracelevel = tracelevel = 0;
259 }
260 
261 
262 /* log a change in tracing
263  */
264 void
265 tracelevel_msg(const char *pat,
266 	       int dump)		/* -1=no dump, 0=default, 1=force */
267 {
268 	static const char *off_msgs[MAX_TRACELEVEL] = {
269 		"Tracing actions stopped",
270 		"Tracing packets stopped",
271 		"Tracing packet contents stopped",
272 		"Tracing kernel changes stopped",
273 	};
274 	static const char *on_msgs[MAX_TRACELEVEL] = {
275 		"Tracing actions started",
276 		"Tracing packets started",
277 		"Tracing packet contents started",
278 		"Tracing kernel changes started",
279 	};
280 	u_int old_tracelevel = tracelevel;
281 
282 
283 	if (new_tracelevel < 0)
284 		new_tracelevel = 0;
285 	else if (new_tracelevel > MAX_TRACELEVEL)
286 		new_tracelevel = MAX_TRACELEVEL;
287 
288 	if (new_tracelevel < tracelevel) {
289 		if (new_tracelevel <= 0) {
290 			trace_off(pat, off_msgs[0]);
291 		} else do {
292 			tmsg(pat, off_msgs[tracelevel]);
293 		}
294 		while (--tracelevel != new_tracelevel);
295 
296 	} else if (new_tracelevel > tracelevel) {
297 		do {
298 			tmsg(pat, on_msgs[tracelevel++]);
299 		} while (tracelevel != new_tracelevel);
300 	}
301 
302 	if (dump > 0
303 	    || (dump == 0 && old_tracelevel == 0 && tracelevel != 0))
304 		trace_dump();
305 }
306 
307 
308 void
309 set_tracefile(const char *filename,
310 	      const char *pat,
311 	      int dump)			/* -1=no dump, 0=default, 1=force */
312 {
313 	struct stat stbuf;
314 	FILE *n_ftrace;
315 	const char *fn;
316 
317 
318 	/* Allow a null filename to increase the level if the trace file
319 	 * is already open or if coming from a trusted source, such as
320 	 * a signal or the command line.
321 	 */
322 	if (filename == 0 || filename[0] == '\0') {
323 		filename = 0;
324 		if (ftrace == 0) {
325 			if (inittracename[0] == '\0') {
326 				msglog("missing trace file name");
327 				return;
328 			}
329 			fn = inittracename;
330 		} else {
331 			fn = 0;
332 		}
333 
334 	} else if (!strcmp(filename,"dump/../table")) {
335 		trace_dump();
336 		return;
337 
338 	} else {
339 		/* Allow the file specified with "-T file" to be reopened,
340 		 * but require all other names specified over the net to
341 		 * match the official path.  The path can specify a directory
342 		 * in which the file is to be created.
343 		 */
344 		if (strcmp(filename, inittracename)
345 #ifdef _PATH_TRACE
346 		    && (strncmp(filename, _PATH_TRACE, sizeof(_PATH_TRACE)-1)
347 			|| strstr(filename,"../")
348 			|| 0 > stat(_PATH_TRACE, &stbuf))
349 #endif
350 		    ) {
351 			msglog("wrong trace file \"%s\"", filename);
352 			return;
353 		}
354 
355 		/* If the new tracefile exists, it must be a regular file.
356 		 */
357 		if (stat(filename, &stbuf) >= 0 && !S_ISREG(stbuf.st_mode)) {
358 			msglog("wrong type (%#x) of trace file \"%s\"",
359 			       stbuf.st_mode, filename);
360 			return;
361 		}
362 
363 		fn = filename;
364 	}
365 
366 	if (fn != 0) {
367 		n_ftrace = fopen(fn, "a");
368 		if (n_ftrace == 0) {
369 			msglog("failed to open trace file \"%s\" %s",
370 			       fn, strerror(errno));
371 			if (fn == inittracename)
372 				inittracename[0] = '\0';
373 			return;
374 		}
375 
376 		tmsg("switch to trace file %s", fn);
377 
378 		trace_close(file_trace = 1);
379 
380 		if (fn != savetracename)
381 			strlcpy(savetracename, fn, sizeof(savetracename));
382 		ftrace = n_ftrace;
383 
384 		fflush(stdout);
385 		fflush(stderr);
386 		dup2(fileno(ftrace), STDOUT_FILENO);
387 		dup2(fileno(ftrace), STDERR_FILENO);
388 	}
389 
390 	if (new_tracelevel == 0 || filename == 0)
391 		new_tracelevel++;
392 	tracelevel_msg(pat, dump != 0 ? dump : (filename != 0));
393 }
394 
395 
396 /* ARGSUSED */
397 void
398 sigtrace_on(int s UNUSED)
399 {
400 	new_tracelevel++;
401 	sigtrace_pat = "SIGUSR1: %s";
402 }
403 
404 
405 /* ARGSUSED */
406 void
407 sigtrace_off(int s UNUSED)
408 {
409 	new_tracelevel--;
410 	sigtrace_pat = "SIGUSR2: %s";
411 }
412 
413 
414 /* Set tracing after a signal.
415  */
416 void
417 set_tracelevel(void)
418 {
419 	if (new_tracelevel == tracelevel)
420 		return;
421 
422 	/* If tracing entirely off, and there was no tracefile specified
423 	 * on the command line, then leave it off.
424 	 */
425 	if (new_tracelevel > tracelevel && ftrace == 0) {
426 		if (savetracename[0] != '\0') {
427 			set_tracefile(savetracename,sigtrace_pat,0);
428 		} else if (inittracename[0] != '\0') {
429 				set_tracefile(inittracename,sigtrace_pat,0);
430 		} else {
431 			new_tracelevel = 0;
432 			return;
433 		}
434 	} else {
435 		tracelevel_msg(sigtrace_pat, 0);
436 	}
437 }
438 
439 
440 /* display an address
441  */
442 char *
443 addrname(naddr	addr,			/* in network byte order */
444 	 naddr	mask,
445 	 int	force)			/* 0=show mask if nonstandard, */
446 {					/*	1=always show mask, 2=never */
447 #define NUM_BUFS 4
448 	static int bufno;
449 	static struct {
450 	    char    str[15+20];
451 	} bufs[NUM_BUFS];
452 	char *s, *sp;
453 	naddr dmask;
454 	size_t l;
455 	int i;
456 
457 	strlcpy(bufs[bufno].str, naddr_ntoa(addr), sizeof(bufs[bufno].str));
458 	s = bufs[bufno].str;
459 	l = sizeof(bufs[bufno].str);
460 	bufno = (bufno+1) % NUM_BUFS;
461 
462 	if (force == 1 || (force == 0 && mask != std_mask(addr))) {
463 		sp = &s[strlen(s)];
464 
465 		dmask = mask & -mask;
466 		if (mask + dmask == 0) {
467 			for (i = 0; i != 32 && ((1<<i) & mask) == 0; i++)
468 				continue;
469 			(void)snprintf(sp, s + l - sp, "/%d", 32-i);
470 
471 		} else {
472 			(void)snprintf(sp, s + l - sp, " (mask %#x)",
473 			    (u_int)mask);
474 		}
475 	}
476 
477 	return s;
478 #undef NUM_BUFS
479 }
480 
481 
482 /* display a bit-field
483  */
484 struct bits {
485 	u_int	bits_mask;
486 	u_int	bits_clear;
487 	const char *bits_name;
488 };
489 
490 static struct bits if_bits[] = {
491 	{ IFF_LOOPBACK,		0,		"LOOPBACK" },
492 	{ IFF_POINTOPOINT,	0,		"PT-TO-PT" },
493 	{ 0,			0,		0}
494 };
495 
496 static struct bits is_bits[] = {
497 	{ IS_ALIAS,		0,		"ALIAS" },
498 	{ IS_SUBNET,		0,		"" },
499 	{ IS_REMOTE,		(IS_NO_RDISC
500 				 | IS_BCAST_RDISC), "REMOTE" },
501 	{ IS_PASSIVE,		(IS_NO_RDISC
502 				 | IS_NO_RIP
503 				 | IS_NO_SUPER_AG
504 				 | IS_PM_RDISC
505 				 | IS_NO_AG),	"PASSIVE" },
506 	{ IS_EXTERNAL,		0,		"EXTERNAL" },
507 	{ IS_CHECKED,		0,		"" },
508 	{ IS_ALL_HOSTS,		0,		"" },
509 	{ IS_ALL_ROUTERS,	0,		"" },
510 	{ IS_DISTRUST,		0,		"DISTRUST" },
511 	{ IS_BROKE,		IS_SICK,	"BROKEN" },
512 	{ IS_SICK,		0,		"SICK" },
513 	{ IS_DUP,		0,		"DUPLICATE" },
514 	{ IS_REDIRECT_OK,	0,		"REDIRECT_OK" },
515 	{ IS_NEED_NET_SYN,	0,		"" },
516 	{ IS_NO_AG,		IS_NO_SUPER_AG,	"NO_AG" },
517 	{ IS_NO_SUPER_AG,	0,		"NO_SUPER_AG" },
518 	{ (IS_NO_RIPV1_IN
519 	   | IS_NO_RIPV2_IN
520 	   | IS_NO_RIPV1_OUT
521 	   | IS_NO_RIPV2_OUT),	0,		"NO_RIP" },
522 	{ (IS_NO_RIPV1_IN
523 	   | IS_NO_RIPV1_OUT),	0,		"RIPV2" },
524 	{ IS_NO_RIPV1_IN,	0,		"NO_RIPV1_IN" },
525 	{ IS_NO_RIPV2_IN,	0,		"NO_RIPV2_IN" },
526 	{ IS_NO_RIPV1_OUT,	0,		"NO_RIPV1_OUT" },
527 	{ IS_NO_RIPV2_OUT,	0,		"NO_RIPV2_OUT" },
528 	{ (IS_NO_ADV_IN
529 	   | IS_NO_SOL_OUT
530 	   | IS_NO_ADV_OUT),	IS_BCAST_RDISC,	"NO_RDISC" },
531 	{ IS_NO_SOL_OUT,	0,		"NO_SOLICIT" },
532 	{ IS_SOL_OUT,		0,		"SEND_SOLICIT" },
533 	{ IS_NO_ADV_OUT,	IS_BCAST_RDISC,	"NO_RDISC_ADV" },
534 	{ IS_ADV_OUT,		0,		"RDISC_ADV" },
535 	{ IS_BCAST_RDISC,	0,		"BCAST_RDISC" },
536 	{ IS_PM_RDISC,		0,		"" },
537 	{ 0,			0,		"%#x"}
538 };
539 
540 static struct bits rs_bits[] = {
541 	{ RS_IF,		0,		"IF" },
542 	{ RS_NET_INT,		RS_NET_SYN,	"NET_INT" },
543 	{ RS_NET_SYN,		0,		"NET_SYN" },
544 	{ RS_SUBNET,		0,		"" },
545 	{ RS_LOCAL,		0,		"LOCAL" },
546 	{ RS_MHOME,		0,		"MHOME" },
547 	{ RS_STATIC,		0,		"STATIC" },
548 	{ RS_RDISC,		0,		"RDISC" },
549 	{ 0,			0,		"%#x"}
550 };
551 
552 
553 static void
554 trace_bits(const struct bits *tbl,
555 	   u_int field,
556 	   int force)
557 {
558 	u_int b;
559 	char c;
560 
561 	if (force) {
562 		(void)putc('<', ftrace);
563 		c = 0;
564 	} else {
565 		c = '<';
566 	}
567 
568 	while (field != 0
569 	       && (b = tbl->bits_mask) != 0) {
570 		if ((b & field) == b) {
571 			if (tbl->bits_name[0] != '\0') {
572 				if (c)
573 					(void)putc(c, ftrace);
574 				(void)fprintf(ftrace, "%s", tbl->bits_name);
575 				c = '|';
576 			}
577 			if (0 == (field &= ~(b | tbl->bits_clear)))
578 				break;
579 		}
580 		tbl++;
581 	}
582 	if (field != 0 && tbl->bits_name != 0) {
583 		if (c)
584 			(void)putc(c, ftrace);
585 		(void)fprintf(ftrace, tbl->bits_name, field);
586 		c = '|';
587 	}
588 
589 	if (c != '<' || force)
590 		(void)fputs("> ", ftrace);
591 }
592 
593 
594 char *
595 rtname(naddr dst,
596        naddr mask,
597        naddr gate)
598 {
599 	static char buf[3*4+3+1+2+3	/* "xxx.xxx.xxx.xxx/xx-->" */
600 			+3*4+3+1];	/* "xxx.xxx.xxx.xxx" */
601 	int i;
602 
603 	i = snprintf(buf, sizeof(buf), "%-16s-->", addrname(dst, mask, 0));
604 	if (i < 0 || (size_t)i >= sizeof(buf))
605 		return buf;
606 	(void)snprintf(&buf[i], sizeof(buf) - i, "%-*s", 15+20-MAX(20, i),
607 	    naddr_ntoa(gate));
608 	return buf;
609 }
610 
611 
612 static void
613 print_rts(struct rt_spare *rts,
614 	  int force_metric,		/* -1=suppress, 0=default */
615 	  int force_ifp,		/* -1=suppress, 0=default */
616 	  int force_router,		/* -1=suppress, 0=default, 1=display */
617 	  int force_tag,		/* -1=suppress, 0=default, 1=display */
618 	  int force_time)		/* 0=suppress, 1=display */
619 {
620 	int i;
621 
622 
623 	if (force_metric >= 0)
624 		(void)fprintf(ftrace, "metric=%-2d ", rts->rts_metric);
625 	if (force_ifp >= 0)
626 		(void)fprintf(ftrace, "%s ", (rts->rts_ifp == 0 ?
627 					      "if?" : rts->rts_ifp->int_name));
628 	if (force_router > 0
629 	    || (force_router == 0 && rts->rts_router != rts->rts_gate))
630 		(void)fprintf(ftrace, "router=%s ",
631 			      naddr_ntoa(rts->rts_router));
632 	if (force_time > 0)
633 		(void)fprintf(ftrace, "%s ", ts(rts->rts_time));
634 	if (force_tag > 0
635 	    || (force_tag == 0 && rts->rts_tag != 0))
636 		(void)fprintf(ftrace, "tag=%#x ", ntohs(rts->rts_tag));
637 	if (rts->rts_de_ag != 0) {
638 		for (i = 1; (u_int)(1 << i) <= rts->rts_de_ag; i++)
639 			continue;
640 		(void)fprintf(ftrace, "de_ag=%d ", i);
641 	}
642 
643 }
644 
645 
646 void
647 trace_if(const char *act,
648 	 struct interface *ifp)
649 {
650 	if (!TRACEACTIONS || ftrace == 0)
651 		return;
652 
653 	lastlog();
654 	(void)fprintf(ftrace, "%-3s interface %-4s ", act, ifp->int_name);
655 	(void)fprintf(ftrace, "%-15s-->%-15s ",
656 		      naddr_ntoa(ifp->int_addr),
657 		      addrname(((ifp->int_if_flags & IFF_POINTOPOINT)
658 				? ifp->int_dstaddr
659 				: htonl(ifp->int_net)),
660 			       ifp->int_mask, 1));
661 	if (ifp->int_metric != 0)
662 		(void)fprintf(ftrace, "metric=%d ", ifp->int_metric);
663 	if (ifp->int_adj_inmetric != 0)
664 		(void)fprintf(ftrace, "adj_inmetric=%u ",
665 			      ifp->int_adj_inmetric);
666 	if (ifp->int_adj_outmetric != 0)
667 		(void)fprintf(ftrace, "adj_outmetric=%u ",
668 			      ifp->int_adj_outmetric);
669 	if (!IS_RIP_OUT_OFF(ifp->int_state)
670 	    && ifp->int_d_metric != 0)
671 		(void)fprintf(ftrace, "fake_default=%u ", ifp->int_d_metric);
672 	trace_bits(if_bits, ifp->int_if_flags, 0);
673 	trace_bits(is_bits, ifp->int_state, 0);
674 	(void)fputc('\n',ftrace);
675 }
676 
677 
678 void
679 trace_upslot(struct rt_entry *rt,
680 	     struct rt_spare *rts,
681 	     struct rt_spare *new)
682 {
683 	if (!TRACEACTIONS || ftrace == 0)
684 		return;
685 
686 	if (rts->rts_gate == new->rts_gate
687 	    && rts->rts_router == new->rts_router
688 	    && rts->rts_metric == new->rts_metric
689 	    && rts->rts_tag == new->rts_tag
690 	    && rts->rts_de_ag == new->rts_de_ag)
691 		return;
692 
693 	lastlog();
694 	if (new->rts_gate == 0) {
695 		(void)fprintf(ftrace, "Del #%d %-35s ",
696 			      (int)(rts - rt->rt_spares),
697 			      rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
698 		print_rts(rts, 0,0,0,0,
699 			  (rts != rt->rt_spares
700 			   || AGE_RT(rt->rt_state,new->rts_ifp)));
701 
702 	} else if (rts->rts_gate != RIP_DEFAULT) {
703 		(void)fprintf(ftrace, "Chg #%d %-35s ",
704 			      (int)(rts - rt->rt_spares),
705 			      rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
706 		print_rts(rts, 0,0,
707 			  rts->rts_gate != new->rts_gate,
708 			  rts->rts_tag != new->rts_tag,
709 			  rts != rt->rt_spares || AGE_RT(rt->rt_state,
710 							rt->rt_ifp));
711 
712 		(void)fprintf(ftrace, "\n       %19s%-16s ", "",
713 			      (new->rts_gate != rts->rts_gate
714 			       ? naddr_ntoa(new->rts_gate) : ""));
715 		print_rts(new,
716 			  -(new->rts_metric == rts->rts_metric),
717 			  -(new->rts_ifp == rts->rts_ifp),
718 			  0,
719 			  rts->rts_tag != new->rts_tag,
720 			  (new->rts_time != rts->rts_time
721 			   && (rts != rt->rt_spares
722 			       || AGE_RT(rt->rt_state, new->rts_ifp))));
723 
724 	} else {
725 		(void)fprintf(ftrace, "Add #%d %-35s ",
726 			      (int)(rts - rt->rt_spares),
727 			      rtname(rt->rt_dst, rt->rt_mask, new->rts_gate));
728 		print_rts(new, 0,0,0,0,
729 			  (rts != rt->rt_spares
730 			   || AGE_RT(rt->rt_state,new->rts_ifp)));
731 	}
732 	(void)fputc('\n',ftrace);
733 }
734 
735 
736 /* miscellaneous message checked by the caller
737  */
738 void
739 trace_misc(const char *p, ...)
740 {
741 	va_list args;
742 
743 	if (ftrace == 0)
744 		return;
745 
746 	lastlog();
747 	va_start(args, p);
748 	vfprintf(ftrace, p, args);
749 	va_end(args);
750 	(void)fputc('\n',ftrace);
751 }
752 
753 
754 /* display a message if tracing actions
755  */
756 void
757 trace_act(const char *p, ...)
758 {
759 	va_list args;
760 
761 	if (!TRACEACTIONS || ftrace == 0)
762 		return;
763 
764 	lastlog();
765 	va_start(args, p);
766 	vfprintf(ftrace, p, args);
767 	va_end(args);
768 	(void)fputc('\n',ftrace);
769 }
770 
771 
772 /* display a message if tracing packets
773  */
774 void
775 trace_pkt(const char *p, ...)
776 {
777 	va_list args;
778 
779 	if (!TRACEPACKETS || ftrace == 0)
780 		return;
781 
782 	lastlog();
783 	va_start(args, p);
784 	vfprintf(ftrace, p, args);
785 	va_end(args);
786 	(void)fputc('\n',ftrace);
787 }
788 
789 
790 void
791 trace_change(struct rt_entry *rt,
792 	     u_int	state,
793 	     struct	rt_spare *new,
794 	     const char	*label)
795 {
796 	if (ftrace == 0)
797 		return;
798 
799 	if (rt->rt_metric == new->rts_metric
800 	    && rt->rt_gate == new->rts_gate
801 	    && rt->rt_router == new->rts_router
802 	    && rt->rt_state == state
803 	    && rt->rt_tag == new->rts_tag
804 	    && rt->rt_de_ag == new->rts_de_ag)
805 		return;
806 
807 	lastlog();
808 	(void)fprintf(ftrace, "%s %-35s ",
809 		      label,
810 		      rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
811 	print_rts(rt->rt_spares,
812 		  0,0,0,0, AGE_RT(rt->rt_state, rt->rt_ifp));
813 	trace_bits(rs_bits, rt->rt_state, rt->rt_state != state);
814 
815 	(void)fprintf(ftrace, "\n%*s %19s%-16s ",
816 		      (int)strlen(label), "", "",
817 		      (rt->rt_gate != new->rts_gate
818 		       ? naddr_ntoa(new->rts_gate) : ""));
819 	print_rts(new,
820 		  -(new->rts_metric == rt->rt_metric),
821 		  -(new->rts_ifp == rt->rt_ifp),
822 		  0,
823 		  rt->rt_tag != new->rts_tag,
824 		  (rt->rt_time != new->rts_time
825 		   && AGE_RT(rt->rt_state,new->rts_ifp)));
826 	if (rt->rt_state != state)
827 		trace_bits(rs_bits, state, 1);
828 	(void)fputc('\n',ftrace);
829 }
830 
831 
832 void
833 trace_add_del(const char * action, struct rt_entry *rt)
834 {
835 	if (ftrace == 0)
836 		return;
837 
838 	lastlog();
839 	(void)fprintf(ftrace, "%s    %-35s ",
840 		      action,
841 		      rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
842 	print_rts(rt->rt_spares, 0,0,0,0,AGE_RT(rt->rt_state,rt->rt_ifp));
843 	trace_bits(rs_bits, rt->rt_state, 0);
844 	(void)fputc('\n',ftrace);
845 }
846 
847 
848 /* ARGSUSED */
849 static int
850 walk_trace(struct radix_node *rn,
851 	   struct walkarg *w UNUSED)
852 {
853 #define RT ((struct rt_entry *)rn)
854 	struct rt_spare *rts;
855 	int i;
856 
857 	(void)fprintf(ftrace, "  %-35s ",
858 		      rtname(RT->rt_dst, RT->rt_mask, RT->rt_gate));
859 	print_rts(&RT->rt_spares[0], 0,0,0,0, AGE_RT(RT->rt_state, RT->rt_ifp));
860 	trace_bits(rs_bits, RT->rt_state, 0);
861 	if (RT->rt_poison_time >= now_garbage
862 	    && RT->rt_poison_metric < RT->rt_metric)
863 		(void)fprintf(ftrace, "pm=%d@%s",
864 			      RT->rt_poison_metric, ts(RT->rt_poison_time));
865 
866 	rts = &RT->rt_spares[1];
867 	for (i = 1; i < NUM_SPARES; i++, rts++) {
868 		if (rts->rts_gate != RIP_DEFAULT) {
869 			(void)fprintf(ftrace,"\n    #%d%15s%-16s ",
870 				      i, "", naddr_ntoa(rts->rts_gate));
871 			print_rts(rts, 0,0,0,0,1);
872 		}
873 	}
874 	(void)fputc('\n',ftrace);
875 
876 	return 0;
877 }
878 
879 
880 static void
881 trace_dump(void)
882 {
883 	struct interface *ifp;
884 
885 	if (ftrace == 0)
886 		return;
887 	lastlog();
888 
889 	(void)fputs("current daemon state:\n", ftrace);
890 	for (ifp = ifnet; ifp != 0; ifp = ifp->int_next)
891 		trace_if("", ifp);
892 	(void)rn_walktree(rhead, walk_trace, 0);
893 }
894 
895 
896 void
897 trace_rip(const char *dir1, const char *dir2,
898 	  struct sockaddr_in *who,
899 	  struct interface *ifp,
900 	  struct rip *msg,
901 	  int size)			/* total size of message */
902 {
903 	struct netinfo *n, *lim;
904 #	define NA ((struct netauth*)n)
905 	int i, seen_route;
906 
907 	if (!TRACEPACKETS || ftrace == 0)
908 		return;
909 
910 	lastlog();
911 	if (msg->rip_cmd >= RIPCMD_MAX
912 	    || msg->rip_vers == 0) {
913 		(void)fprintf(ftrace, "%s bad RIPv%d cmd=%d %s"
914 			      " %s.%d size=%d\n",
915 			      dir1, msg->rip_vers, msg->rip_cmd, dir2,
916 			      naddr_ntoa(who->sin_addr.s_addr),
917 			      ntohs(who->sin_port),
918 			      size);
919 		return;
920 	}
921 
922 	(void)fprintf(ftrace, "%s RIPv%d %s %s %s.%d%s%s\n",
923 		      dir1, msg->rip_vers, ripcmds[msg->rip_cmd], dir2,
924 		      naddr_ntoa(who->sin_addr.s_addr), ntohs(who->sin_port),
925 		      ifp ? " via " : "", ifp ? ifp->int_name : "");
926 	if (!TRACECONTENTS)
927 		return;
928 
929 	seen_route = 0;
930 	switch (msg->rip_cmd) {
931 	case RIPCMD_REQUEST:
932 	case RIPCMD_RESPONSE:
933 		n = msg->rip_nets;
934 		lim = (struct netinfo *)((char*)msg + size);
935 		for (; n < lim; n++) {
936 			if (!seen_route
937 			    && n->n_family == RIP_AF_UNSPEC
938 			    && ntohl(n->n_metric) == HOPCNT_INFINITY
939 			    && msg->rip_cmd == RIPCMD_REQUEST
940 			    && (n+1 == lim
941 				|| (n+2 == lim
942 				    && (n+1)->n_family == RIP_AF_AUTH))) {
943 				(void)fputs("\tQUERY ", ftrace);
944 				if (n->n_dst != 0)
945 					(void)fprintf(ftrace, "%s ",
946 						      naddr_ntoa(n->n_dst));
947 				if (n->n_mask != 0)
948 					(void)fprintf(ftrace, "mask=%#x ",
949 						      (u_int)ntohl(n->n_mask));
950 				if (n->n_nhop != 0)
951 					(void)fprintf(ftrace, "nhop=%s ",
952 						      naddr_ntoa(n->n_nhop));
953 				if (n->n_tag != 0)
954 					(void)fprintf(ftrace, "tag=%#x ",
955 						      ntohs(n->n_tag));
956 				(void)fputc('\n',ftrace);
957 				continue;
958 			}
959 
960 			if (n->n_family == RIP_AF_AUTH) {
961 				if (NA->a_type == RIP_AUTH_PW
962 				    && n == msg->rip_nets) {
963 					(void)fprintf(ftrace, "\tPassword"
964 						      " Authentication:"
965 						      " \"%s\"\n",
966 						      qstring(NA->au.au_pw,
967 							  RIP_AUTH_PW_LEN));
968 					continue;
969 				}
970 
971 				if (NA->a_type == RIP_AUTH_MD5
972 				    && n == msg->rip_nets) {
973 					(void)fprintf(ftrace,
974 						      "\tMD5 Auth"
975 						      " pkt_len=%d KeyID=%u"
976 						      " auth_len=%d"
977 						      " seqno=%#x"
978 						      " rsvd=%#x,%#x\n",
979 					    ntohs(NA->au.a_md5.md5_pkt_len),
980 					    NA->au.a_md5.md5_keyid,
981 					    NA->au.a_md5.md5_auth_len,
982 					    (int)ntohl(NA->au.a_md5.md5_seqno),
983 					    (int)ntohs(NA->au.a_md5.rsvd[0]),
984 					    (int)ntohs(NA->au.a_md5.rsvd[1]));
985 					continue;
986 				}
987 				(void)fprintf(ftrace,
988 					      "\tAuthentication type %d: ",
989 					      ntohs(NA->a_type));
990 				for (i = 0;
991 				     i < (int)sizeof(NA->au.au_pw);
992 				     i++)
993 					(void)fprintf(ftrace, "%02x ",
994 						      NA->au.au_pw[i]);
995 				(void)fputc('\n',ftrace);
996 				continue;
997 			}
998 
999 			seen_route = 1;
1000 			if (n->n_family != RIP_AF_INET) {
1001 				(void)fprintf(ftrace,
1002 					      "\t(af %d) %-18s mask=%#x ",
1003 					      ntohs(n->n_family),
1004 					      naddr_ntoa(n->n_dst),
1005 					      (u_int)ntohl(n->n_mask));
1006 			} else if (msg->rip_vers == RIPv1) {
1007 				(void)fprintf(ftrace, "\t%-18s ",
1008 					      addrname(n->n_dst,
1009 						       ntohl(n->n_mask),
1010 						       n->n_mask==0 ? 2 : 1));
1011 			} else {
1012 				(void)fprintf(ftrace, "\t%-18s ",
1013 					      addrname(n->n_dst,
1014 						       ntohl(n->n_mask),
1015 						       n->n_mask==0 ? 2 : 0));
1016 			}
1017 			(void)fprintf(ftrace, "metric=%-2d ",
1018 				      (u_int)ntohl(n->n_metric));
1019 			if (n->n_nhop != 0)
1020 				(void)fprintf(ftrace, " nhop=%s ",
1021 					      naddr_ntoa(n->n_nhop));
1022 			if (n->n_tag != 0)
1023 				(void)fprintf(ftrace, "tag=%#x",
1024 					      ntohs(n->n_tag));
1025 			(void)fputc('\n',ftrace);
1026 		}
1027 		if (size != (char *)n - (char *)msg)
1028 			(void)fprintf(ftrace, "truncated record, len %d\n",
1029 				size);
1030 		break;
1031 
1032 	case RIPCMD_TRACEON:
1033 		fprintf(ftrace, "\tfile=\"%.*s\"\n", size-4,
1034 			msg->rip_tracefile);
1035 		break;
1036 
1037 	case RIPCMD_TRACEOFF:
1038 		break;
1039 	}
1040 }
1041