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