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