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