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