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