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