xref: /freebsd/sbin/routed/trace.c (revision 38ae65235e00a1e87239372f973d057aeff1f2d8)
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  * 4. 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$
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 #ifdef __NetBSD__
40 __RCSID("$NetBSD$");
41 #elif defined(__FreeBSD__)
42 __RCSID("$FreeBSD$");
43 #else
44 __RCSID("$Revision: 2.27 $");
45 #ident "$Revision: 2.27 $"
46 #endif
47 
48 
49 #ifdef sgi
50 /* use *stat64 for files on large file systems */
51 #define stat	stat64
52 #endif
53 
54 #define	NRECORDS	50		/* size of circular trace buffer */
55 
56 int	tracelevel, new_tracelevel;
57 FILE	*ftrace;			/* output trace file */
58 static const char *sigtrace_pat = "%s";
59 static char savetracename[PATH_MAX];
60 char	inittracename[PATH_MAX];
61 int	file_trace;			/* 1=tracing to file, not stdout */
62 
63 static void trace_dump(void);
64 static void tmsg(const char *, ...) PATTRIB(1,2);
65 
66 
67 /* convert string to printable characters
68  */
69 static char *
70 qstring(u_char *s, int len)
71 {
72 	static char buf[8*20+1];
73 	char *p;
74 	u_char *s2, c;
75 
76 
77 	for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
78 		c = *s++;
79 		if (c == '\0') {
80 			for (s2 = s+1; s2 < &s[len]; s2++) {
81 				if (*s2 != '\0')
82 					break;
83 			}
84 			if (s2 >= &s[len])
85 			    goto exit;
86 		}
87 
88 		if (c >= ' ' && c < 0x7f && c != '\\') {
89 			*p++ = c;
90 			continue;
91 		}
92 		*p++ = '\\';
93 		switch (c) {
94 		case '\\':
95 			*p++ = '\\';
96 			break;
97 		case '\n':
98 			*p++= 'n';
99 			break;
100 		case '\r':
101 			*p++= 'r';
102 			break;
103 		case '\t':
104 			*p++ = 't';
105 			break;
106 		case '\b':
107 			*p++ = 'b';
108 			break;
109 		default:
110 			p += sprintf(p,"%o",c);
111 			break;
112 		}
113 	}
114 exit:
115 	*p = '\0';
116 	return buf;
117 }
118 
119 
120 /* convert IP address to a string, but not into a single buffer
121  */
122 char *
123 naddr_ntoa(naddr a)
124 {
125 #define NUM_BUFS 4
126 	static int bufno;
127 	static struct {
128 	    char    str[16];		/* xxx.xxx.xxx.xxx\0 */
129 	} bufs[NUM_BUFS];
130 	char *s;
131 	struct in_addr addr;
132 
133 	addr.s_addr = a;
134 	s = strcpy(bufs[bufno].str, inet_ntoa(addr));
135 	bufno = (bufno+1) % NUM_BUFS;
136 	return s;
137 #undef NUM_BUFS
138 }
139 
140 
141 const char *
142 saddr_ntoa(struct sockaddr *sa)
143 {
144 	return (sa == 0) ? "?" : naddr_ntoa(S_ADDR(sa));
145 }
146 
147 
148 static char *
149 ts(time_t secs) {
150 	static char s[20];
151 
152 	secs += epoch.tv_sec;
153 #ifdef sgi
154 	(void)cftime(s, "%T", &secs);
155 #else
156 	memcpy(s, ctime(&secs)+11, 8);
157 	s[8] = '\0';
158 #endif
159 	return s;
160 }
161 
162 
163 /* On each event, display a time stamp.
164  * This assumes that 'now' is update once for each event, and
165  * that at least now.tv_usec changes.
166  */
167 static struct timeval lastlog_time;
168 
169 void
170 lastlog(void)
171 {
172 	if (lastlog_time.tv_sec != now.tv_sec
173 	    || lastlog_time.tv_usec != now.tv_usec) {
174 		(void)fprintf(ftrace, "-- %s --\n", ts(now.tv_sec));
175 		lastlog_time = now;
176 	}
177 }
178 
179 
180 static void
181 tmsg(const char *p, ...)
182 {
183 	va_list args;
184 
185 	if (ftrace != 0) {
186 		lastlog();
187 		va_start(args, p);
188 		vfprintf(ftrace, p, args);
189 		va_end(args);
190 		(void)fputc('\n',ftrace);
191 		fflush(ftrace);
192 	}
193 }
194 
195 
196 void
197 trace_close(int zap_stdio)
198 {
199 	int fd;
200 
201 
202 	fflush(stdout);
203 	fflush(stderr);
204 
205 	if (ftrace != 0 && zap_stdio) {
206 		if (ftrace != stdout)
207 			fclose(ftrace);
208 		ftrace = 0;
209 		fd = open(_PATH_DEVNULL, O_RDWR);
210 		if (isatty(STDIN_FILENO))
211 			(void)dup2(fd, STDIN_FILENO);
212 		if (isatty(STDOUT_FILENO))
213 			(void)dup2(fd, STDOUT_FILENO);
214 		if (isatty(STDERR_FILENO))
215 			(void)dup2(fd, STDERR_FILENO);
216 		(void)close(fd);
217 	}
218 	lastlog_time.tv_sec = 0;
219 }
220 
221 
222 void
223 trace_flush(void)
224 {
225 	if (ftrace != 0) {
226 		fflush(ftrace);
227 		if (ferror(ftrace))
228 			trace_off("tracing off: %s", strerror(ferror(ftrace)));
229 	}
230 }
231 
232 
233 void
234 trace_off(const char *p, ...)
235 {
236 	va_list args;
237 
238 
239 	if (ftrace != 0) {
240 		lastlog();
241 		va_start(args, p);
242 		vfprintf(ftrace, p, args);
243 		va_end(args);
244 		(void)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 			(void)sprintf(sp, "/%d", 32-i);
457 
458 		} else {
459 			(void)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 		(void)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 					(void)putc(c, ftrace);
560 				(void)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 			(void)putc(c, ftrace);
571 		(void)fprintf(ftrace, tbl->bits_name, field);
572 		c = '|';
573 	}
574 
575 	if (c != '<' || force)
576 		(void)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 	(void)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 		(void)fprintf(ftrace, "metric=%-2d ", rts->rts_metric);
608 	if (force_ifp >= 0)
609 		(void)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 		(void)fprintf(ftrace, "router=%s ",
614 			      naddr_ntoa(rts->rts_router));
615 	if (force_time > 0)
616 		(void)fprintf(ftrace, "%s ", ts(rts->rts_time));
617 	if (force_tag > 0
618 	    || (force_tag == 0 && rts->rts_tag != 0))
619 		(void)fprintf(ftrace, "tag=%#x ", ntohs(rts->rts_tag));
620 	if (rts->rts_de_ag != 0) {
621 		for (i = 1; (u_int)(1 << i) <= rts->rts_de_ag; i++)
622 			continue;
623 		(void)fprintf(ftrace, "de_ag=%d ", i);
624 	}
625 
626 }
627 
628 
629 void
630 trace_if(const char *act,
631 	 struct interface *ifp)
632 {
633 	if (!TRACEACTIONS || ftrace == 0)
634 		return;
635 
636 	lastlog();
637 	(void)fprintf(ftrace, "%-3s interface %-4s ", act, ifp->int_name);
638 	(void)fprintf(ftrace, "%-15s-->%-15s ",
639 		      naddr_ntoa(ifp->int_addr),
640 		      addrname(((ifp->int_if_flags & IFF_POINTOPOINT)
641 				? ifp->int_dstaddr
642 				: htonl(ifp->int_net)),
643 			       ifp->int_mask, 1));
644 	if (ifp->int_metric != 0)
645 		(void)fprintf(ftrace, "metric=%d ", ifp->int_metric);
646 	if (ifp->int_adj_inmetric != 0)
647 		(void)fprintf(ftrace, "adj_inmetric=%u ",
648 			      ifp->int_adj_inmetric);
649 	if (ifp->int_adj_outmetric != 0)
650 		(void)fprintf(ftrace, "adj_outmetric=%u ",
651 			      ifp->int_adj_outmetric);
652 	if (!IS_RIP_OUT_OFF(ifp->int_state)
653 	    && ifp->int_d_metric != 0)
654 		(void)fprintf(ftrace, "fake_default=%u ", ifp->int_d_metric);
655 	trace_bits(if_bits, ifp->int_if_flags, 0);
656 	trace_bits(is_bits, ifp->int_state, 0);
657 	(void)fputc('\n',ftrace);
658 }
659 
660 
661 void
662 trace_upslot(struct rt_entry *rt,
663 	     struct rt_spare *rts,
664 	     struct rt_spare *new)
665 {
666 	if (!TRACEACTIONS || ftrace == 0)
667 		return;
668 
669 	if (rts->rts_gate == new->rts_gate
670 	    && rts->rts_router == new->rts_router
671 	    && rts->rts_metric == new->rts_metric
672 	    && rts->rts_tag == new->rts_tag
673 	    && rts->rts_de_ag == new->rts_de_ag)
674 		return;
675 
676 	lastlog();
677 	if (new->rts_gate == 0) {
678 		(void)fprintf(ftrace, "Del #%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,0,0,
682 			  (rts != rt->rt_spares
683 			   || AGE_RT(rt->rt_state,new->rts_ifp)));
684 
685 	} else if (rts->rts_gate != RIP_DEFAULT) {
686 		(void)fprintf(ftrace, "Chg #%d %-35s ",
687 			      (int)(rts - rt->rt_spares),
688 			      rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
689 		print_rts(rts, 0,0,
690 			  rts->rts_gate != new->rts_gate,
691 			  rts->rts_tag != new->rts_tag,
692 			  rts != rt->rt_spares || AGE_RT(rt->rt_state,
693 							rt->rt_ifp));
694 
695 		(void)fprintf(ftrace, "\n       %19s%-16s ", "",
696 			      (new->rts_gate != rts->rts_gate
697 			       ? naddr_ntoa(new->rts_gate) : ""));
698 		print_rts(new,
699 			  -(new->rts_metric == rts->rts_metric),
700 			  -(new->rts_ifp == rts->rts_ifp),
701 			  0,
702 			  rts->rts_tag != new->rts_tag,
703 			  (new->rts_time != rts->rts_time
704 			   && (rts != rt->rt_spares
705 			       || AGE_RT(rt->rt_state, new->rts_ifp))));
706 
707 	} else {
708 		(void)fprintf(ftrace, "Add #%d %-35s ",
709 			      (int)(rts - rt->rt_spares),
710 			      rtname(rt->rt_dst, rt->rt_mask, new->rts_gate));
711 		print_rts(new, 0,0,0,0,
712 			  (rts != rt->rt_spares
713 			   || AGE_RT(rt->rt_state,new->rts_ifp)));
714 	}
715 	(void)fputc('\n',ftrace);
716 }
717 
718 
719 /* miscellaneous message checked by the caller
720  */
721 void
722 trace_misc(const char *p, ...)
723 {
724 	va_list args;
725 
726 	if (ftrace == 0)
727 		return;
728 
729 	lastlog();
730 	va_start(args, p);
731 	vfprintf(ftrace, p, args);
732 	va_end(args);
733 	(void)fputc('\n',ftrace);
734 }
735 
736 
737 /* display a message if tracing actions
738  */
739 void
740 trace_act(const char *p, ...)
741 {
742 	va_list args;
743 
744 	if (!TRACEACTIONS || ftrace == 0)
745 		return;
746 
747 	lastlog();
748 	va_start(args, p);
749 	vfprintf(ftrace, p, args);
750 	va_end(args);
751 	(void)fputc('\n',ftrace);
752 }
753 
754 
755 /* display a message if tracing packets
756  */
757 void
758 trace_pkt(const char *p, ...)
759 {
760 	va_list args;
761 
762 	if (!TRACEPACKETS || ftrace == 0)
763 		return;
764 
765 	lastlog();
766 	va_start(args, p);
767 	vfprintf(ftrace, p, args);
768 	va_end(args);
769 	(void)fputc('\n',ftrace);
770 }
771 
772 
773 void
774 trace_change(struct rt_entry *rt,
775 	     u_int	state,
776 	     struct	rt_spare *new,
777 	     const char	*label)
778 {
779 	if (ftrace == 0)
780 		return;
781 
782 	if (rt->rt_metric == new->rts_metric
783 	    && rt->rt_gate == new->rts_gate
784 	    && rt->rt_router == new->rts_router
785 	    && rt->rt_state == state
786 	    && rt->rt_tag == new->rts_tag
787 	    && rt->rt_de_ag == new->rts_de_ag)
788 		return;
789 
790 	lastlog();
791 	(void)fprintf(ftrace, "%s %-35s ",
792 		      label,
793 		      rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
794 	print_rts(rt->rt_spares,
795 		  0,0,0,0, AGE_RT(rt->rt_state, rt->rt_ifp));
796 	trace_bits(rs_bits, rt->rt_state, rt->rt_state != state);
797 
798 	(void)fprintf(ftrace, "\n%*s %19s%-16s ",
799 		      (int)strlen(label), "", "",
800 		      (rt->rt_gate != new->rts_gate
801 		       ? naddr_ntoa(new->rts_gate) : ""));
802 	print_rts(new,
803 		  -(new->rts_metric == rt->rt_metric),
804 		  -(new->rts_ifp == rt->rt_ifp),
805 		  0,
806 		  rt->rt_tag != new->rts_tag,
807 		  (rt->rt_time != new->rts_time
808 		   && AGE_RT(rt->rt_state,new->rts_ifp)));
809 	if (rt->rt_state != state)
810 		trace_bits(rs_bits, state, 1);
811 	(void)fputc('\n',ftrace);
812 }
813 
814 
815 void
816 trace_add_del(const char * action, struct rt_entry *rt)
817 {
818 	if (ftrace == 0)
819 		return;
820 
821 	lastlog();
822 	(void)fprintf(ftrace, "%s    %-35s ",
823 		      action,
824 		      rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
825 	print_rts(rt->rt_spares, 0,0,0,0,AGE_RT(rt->rt_state,rt->rt_ifp));
826 	trace_bits(rs_bits, rt->rt_state, 0);
827 	(void)fputc('\n',ftrace);
828 }
829 
830 
831 /* ARGSUSED */
832 static int
833 walk_trace(struct radix_node *rn,
834 	   struct walkarg *w UNUSED)
835 {
836 #define RT ((struct rt_entry *)rn)
837 	struct rt_spare *rts;
838 	int i;
839 
840 	(void)fprintf(ftrace, "  %-35s ",
841 		      rtname(RT->rt_dst, RT->rt_mask, RT->rt_gate));
842 	print_rts(&RT->rt_spares[0], 0,0,0,0, AGE_RT(RT->rt_state, RT->rt_ifp));
843 	trace_bits(rs_bits, RT->rt_state, 0);
844 	if (RT->rt_poison_time >= now_garbage
845 	    && RT->rt_poison_metric < RT->rt_metric)
846 		(void)fprintf(ftrace, "pm=%d@%s",
847 			      RT->rt_poison_metric, ts(RT->rt_poison_time));
848 
849 	rts = &RT->rt_spares[1];
850 	for (i = 1; i < NUM_SPARES; i++, rts++) {
851 		if (rts->rts_gate != RIP_DEFAULT) {
852 			(void)fprintf(ftrace,"\n    #%d%15s%-16s ",
853 				      i, "", naddr_ntoa(rts->rts_gate));
854 			print_rts(rts, 0,0,0,0,1);
855 		}
856 	}
857 	(void)fputc('\n',ftrace);
858 
859 	return 0;
860 }
861 
862 
863 static void
864 trace_dump(void)
865 {
866 	struct interface *ifp;
867 
868 	if (ftrace == 0)
869 		return;
870 	lastlog();
871 
872 	(void)fputs("current daemon state:\n", ftrace);
873 	LIST_FOREACH(ifp, &ifnet, int_list)
874 		trace_if("", ifp);
875 	(void)rn_walktree(rhead, walk_trace, 0);
876 }
877 
878 
879 void
880 trace_rip(const char *dir1, const char *dir2,
881 	  struct sockaddr_in *who,
882 	  struct interface *ifp,
883 	  struct rip *msg,
884 	  int size)			/* total size of message */
885 {
886 	struct netinfo *n, *lim;
887 #	define NA ((struct netauth*)n)
888 	int i, seen_route;
889 
890 	if (!TRACEPACKETS || ftrace == 0)
891 		return;
892 
893 	lastlog();
894 	if (msg->rip_cmd >= RIPCMD_MAX
895 	    || msg->rip_vers == 0) {
896 		(void)fprintf(ftrace, "%s bad RIPv%d cmd=%d %s"
897 			      " %s.%d size=%d\n",
898 			      dir1, msg->rip_vers, msg->rip_cmd, dir2,
899 			      naddr_ntoa(who->sin_addr.s_addr),
900 			      ntohs(who->sin_port),
901 			      size);
902 		return;
903 	}
904 
905 	(void)fprintf(ftrace, "%s RIPv%d %s %s %s.%d%s%s\n",
906 		      dir1, msg->rip_vers, ripcmds[msg->rip_cmd], dir2,
907 		      naddr_ntoa(who->sin_addr.s_addr), ntohs(who->sin_port),
908 		      ifp ? " via " : "", ifp ? ifp->int_name : "");
909 	if (!TRACECONTENTS)
910 		return;
911 
912 	seen_route = 0;
913 	switch (msg->rip_cmd) {
914 	case RIPCMD_REQUEST:
915 	case RIPCMD_RESPONSE:
916 		n = msg->rip_nets;
917 		lim = (struct netinfo *)((char*)msg + size);
918 		for (; n < lim; n++) {
919 			if (!seen_route
920 			    && n->n_family == RIP_AF_UNSPEC
921 			    && ntohl(n->n_metric) == HOPCNT_INFINITY
922 			    && msg->rip_cmd == RIPCMD_REQUEST
923 			    && (n+1 == lim
924 				|| (n+2 == lim
925 				    && (n+1)->n_family == RIP_AF_AUTH))) {
926 				(void)fputs("\tQUERY ", ftrace);
927 				if (n->n_dst != 0)
928 					(void)fprintf(ftrace, "%s ",
929 						      naddr_ntoa(n->n_dst));
930 				if (n->n_mask != 0)
931 					(void)fprintf(ftrace, "mask=%#x ",
932 						      (u_int)ntohl(n->n_mask));
933 				if (n->n_nhop != 0)
934 					(void)fprintf(ftrace, "nhop=%s ",
935 						      naddr_ntoa(n->n_nhop));
936 				if (n->n_tag != 0)
937 					(void)fprintf(ftrace, "tag=%#x ",
938 						      ntohs(n->n_tag));
939 				(void)fputc('\n',ftrace);
940 				continue;
941 			}
942 
943 			if (n->n_family == RIP_AF_AUTH) {
944 				if (NA->a_type == RIP_AUTH_PW
945 				    && n == msg->rip_nets) {
946 					(void)fprintf(ftrace, "\tPassword"
947 						      " Authentication:"
948 						      " \"%s\"\n",
949 						      qstring(NA->au.au_pw,
950 							  RIP_AUTH_PW_LEN));
951 					continue;
952 				}
953 
954 				if (NA->a_type == RIP_AUTH_MD5
955 				    && n == msg->rip_nets) {
956 					(void)fprintf(ftrace,
957 						      "\tMD5 Auth"
958 						      " pkt_len=%d KeyID=%u"
959 						      " auth_len=%d"
960 						      " seqno=%#x"
961 						      " rsvd=%#x,%#x\n",
962 					    ntohs(NA->au.a_md5.md5_pkt_len),
963 					    NA->au.a_md5.md5_keyid,
964 					    NA->au.a_md5.md5_auth_len,
965 					    (int)ntohl(NA->au.a_md5.md5_seqno),
966 					    (int)ntohs(NA->au.a_md5.rsvd[0]),
967 					    (int)ntohs(NA->au.a_md5.rsvd[1]));
968 					continue;
969 				}
970 				(void)fprintf(ftrace,
971 					      "\tAuthentication type %d: ",
972 					      ntohs(NA->a_type));
973 				for (i = 0;
974 				     i < (int)sizeof(NA->au.au_pw);
975 				     i++)
976 					(void)fprintf(ftrace, "%02x ",
977 						      NA->au.au_pw[i]);
978 				(void)fputc('\n',ftrace);
979 				continue;
980 			}
981 
982 			seen_route = 1;
983 			if (n->n_family != RIP_AF_INET) {
984 				(void)fprintf(ftrace,
985 					      "\t(af %d) %-18s mask=%#x ",
986 					      ntohs(n->n_family),
987 					      naddr_ntoa(n->n_dst),
988 					      (u_int)ntohl(n->n_mask));
989 			} else if (msg->rip_vers == RIPv1) {
990 				(void)fprintf(ftrace, "\t%-18s ",
991 					      addrname(n->n_dst,
992 						       ntohl(n->n_mask),
993 						       n->n_mask==0 ? 2 : 1));
994 			} else {
995 				(void)fprintf(ftrace, "\t%-18s ",
996 					      addrname(n->n_dst,
997 						       ntohl(n->n_mask),
998 						       n->n_mask==0 ? 2 : 0));
999 			}
1000 			(void)fprintf(ftrace, "metric=%-2d ",
1001 				      (u_int)ntohl(n->n_metric));
1002 			if (n->n_nhop != 0)
1003 				(void)fprintf(ftrace, " nhop=%s ",
1004 					      naddr_ntoa(n->n_nhop));
1005 			if (n->n_tag != 0)
1006 				(void)fprintf(ftrace, "tag=%#x",
1007 					      ntohs(n->n_tag));
1008 			(void)fputc('\n',ftrace);
1009 		}
1010 		if (size != (char *)n - (char *)msg)
1011 			(void)fprintf(ftrace, "truncated record, len %d\n",
1012 				size);
1013 		break;
1014 
1015 	case RIPCMD_TRACEON:
1016 		fprintf(ftrace, "\tfile=\"%.*s\"\n", size-4,
1017 			msg->rip_tracefile);
1018 		break;
1019 
1020 	case RIPCMD_TRACEOFF:
1021 		break;
1022 	}
1023 }
1024