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