Home | History | Annotate | Line # | Download | only in routed
      1 /*	$NetBSD: trace.c,v 1.33 2017/10/02 11:02:19 maya 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.33 2017/10/02 11:02:19 maya Exp $");
     45 #elif defined(__FreeBSD__)
     46 __RCSID("$FreeBSD$");
     47 #else
     48 __RCSID("Revision: 2.27 ");
     49 #ident "Revision: 2.27 "
     50 #endif
     51 
     52 #define	NRECORDS	50		/* size of circular trace buffer */
     53 
     54 int	tracelevel, new_tracelevel;
     55 FILE	*ftrace = stdout;		/* output trace file */
     56 static const char *sigtrace_pat = "%s";
     57 static char savetracename[MAXPATHLEN+1];
     58 char	inittracename[MAXPATHLEN+1];
     59 int	file_trace;			/* 1=tracing to file, not stdout */
     60 
     61 static void trace_dump(void);
     62 static void tmsg(const char *, ...) PATTRIB(1,2);
     63 
     64 
     65 /* convert string to printable characters
     66  */
     67 static char *
     68 qstring(u_char *s, int len)
     69 {
     70 	static char buf[8*20+1];
     71 	char *p;
     72 	u_char *s2, c;
     73 	int n;
     74 
     75 	for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
     76 		c = *s++;
     77 		if (c == '\0') {
     78 			for (s2 = s+1; s2 < &s[len]; s2++) {
     79 				if (*s2 != '\0')
     80 					break;
     81 			}
     82 			if (s2 >= &s[len])
     83 			    goto exit;
     84 		}
     85 
     86 		if (c >= ' ' && c < 0x7f && c != '\\') {
     87 			*p++ = c;
     88 			continue;
     89 		}
     90 		*p++ = '\\';
     91 		switch (c) {
     92 		case '\\':
     93 			*p++ = '\\';
     94 			break;
     95 		case '\n':
     96 			*p++= 'n';
     97 			break;
     98 		case '\r':
     99 			*p++= 'r';
    100 			break;
    101 		case '\t':
    102 			*p++ = 't';
    103 			break;
    104 		case '\b':
    105 			*p++ = 'b';
    106 			break;
    107 		default:
    108 			n = snprintf(p, sizeof(buf) - (p - buf), "%o", c);
    109 			if (n <= 0)
    110 				goto exit;
    111 			p += n;
    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 	strlcpy(bufs[bufno].str, inet_ntoa(addr), sizeof(bufs[bufno].str));
    136 	s = bufs[bufno].str;
    137 	bufno = (bufno+1) % NUM_BUFS;
    138 	return s;
    139 #undef NUM_BUFS
    140 }
    141 
    142 
    143 const char *
    144 saddr_ntoa(const struct sockaddr *sa)
    145 {
    146 	return (sa == 0) ? "?" : naddr_ntoa(S_ADDR(sa));
    147 }
    148 
    149 
    150 static char *
    151 ts(time_t secs) {
    152 	static char s[20];
    153 
    154 	secs += epoch.tv_sec;
    155 	memcpy(s, ctime(&secs)+11, 8);
    156 	s[8] = '\0';
    157 
    158 	return s;
    159 }
    160 
    161 
    162 /* On each event, display a time stamp.
    163  * This assumes that 'now' is update once for each event, and
    164  * that at least now.tv_usec changes.
    165  */
    166 static struct timeval lastlog_time;
    167 
    168 void
    169 lastlog(void)
    170 {
    171 	if (lastlog_time.tv_sec != now.tv_sec
    172 	    || lastlog_time.tv_usec != now.tv_usec) {
    173 		(void)fprintf(ftrace, "-- %s --\n", ts(now.tv_sec));
    174 		lastlog_time = now;
    175 	}
    176 }
    177 
    178 
    179 static void
    180 tmsg(const char *p, ...)
    181 {
    182 	va_list args;
    183 
    184 	if (ftrace != 0) {
    185 		lastlog();
    186 		va_start(args, p);
    187 		vfprintf(ftrace, p, args);
    188 		va_end(args);
    189 		(void)fputc('\n',ftrace);
    190 		fflush(ftrace);
    191 	}
    192 }
    193 
    194 
    195 void
    196 trace_close(int zap_stdio)
    197 {
    198 	int fd;
    199 
    200 
    201 	fflush(stdout);
    202 	fflush(stderr);
    203 
    204 	if (ftrace != 0 && zap_stdio) {
    205 		if (ftrace != stdout)
    206 			fclose(ftrace);
    207 		ftrace = 0;
    208 		fd = open(_PATH_DEVNULL, O_RDWR);
    209 		if (fd == -1)
    210 			return;
    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 			strlcpy(savetracename, fn, sizeof(savetracename));
    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 	size_t l;
    446 	int i;
    447 
    448 	strlcpy(bufs[bufno].str, naddr_ntoa(addr), sizeof(bufs[bufno].str));
    449 	s = bufs[bufno].str;
    450 	l = sizeof(bufs[bufno].str);
    451 	bufno = (bufno+1) % NUM_BUFS;
    452 
    453 	if (force == 1 || (force == 0 && mask != std_mask(addr))) {
    454 		sp = &s[strlen(s)];
    455 
    456 		dmask = mask & -mask;
    457 		if (mask + dmask == 0) {
    458 			for (i = 0; i != 32 && ((1<<i) & mask) == 0; i++)
    459 				continue;
    460 			(void)snprintf(sp, s + l - sp, "/%d", 32-i);
    461 
    462 		} else {
    463 			(void)snprintf(sp, s + l - sp, " (mask %#x)",
    464 			    (u_int)mask);
    465 		}
    466 	}
    467 
    468 	return s;
    469 #undef NUM_BUFS
    470 }
    471 
    472 
    473 /* display a bit-field
    474  */
    475 struct bits {
    476 	u_int	bits_mask;
    477 	u_int	bits_clear;
    478 	const char *bits_name;
    479 };
    480 
    481 static struct bits if_bits[] = {
    482 	{ IFF_LOOPBACK,		0,		"LOOPBACK" },
    483 	{ IFF_POINTOPOINT,	0,		"PT-TO-PT" },
    484 	{ 0,			0,		0}
    485 };
    486 
    487 static struct bits is_bits[] = {
    488 	{ IS_ALIAS,		0,		"ALIAS" },
    489 	{ IS_SUBNET,		0,		"" },
    490 	{ IS_REMOTE,		(IS_NO_RDISC
    491 				 | IS_BCAST_RDISC), "REMOTE" },
    492 	{ IS_PASSIVE,		(IS_NO_RDISC
    493 				 | IS_NO_RIP
    494 				 | IS_NO_SUPER_AG
    495 				 | IS_PM_RDISC
    496 				 | IS_NO_AG),	"PASSIVE" },
    497 	{ IS_EXTERNAL,		0,		"EXTERNAL" },
    498 	{ IS_CHECKED,		0,		"" },
    499 	{ IS_ALL_HOSTS,		0,		"" },
    500 	{ IS_ALL_ROUTERS,	0,		"" },
    501 	{ IS_DISTRUST,		0,		"DISTRUST" },
    502 	{ IS_BROKE,		IS_SICK,	"BROKEN" },
    503 	{ IS_SICK,		0,		"SICK" },
    504 	{ IS_DUP,		0,		"DUPLICATE" },
    505 	{ IS_REDIRECT_OK,	0,		"REDIRECT_OK" },
    506 	{ IS_NEED_NET_SYN,	0,		"" },
    507 	{ IS_NO_AG,		IS_NO_SUPER_AG,	"NO_AG" },
    508 	{ IS_NO_SUPER_AG,	0,		"NO_SUPER_AG" },
    509 	{ (IS_NO_RIPV1_IN
    510 	   | IS_NO_RIPV2_IN
    511 	   | IS_NO_RIPV1_OUT
    512 	   | IS_NO_RIPV2_OUT),	0,		"NO_RIP" },
    513 	{ (IS_NO_RIPV1_IN
    514 	   | IS_NO_RIPV1_OUT),	0,		"RIPV2" },
    515 	{ IS_NO_RIPV1_IN,	0,		"NO_RIPV1_IN" },
    516 	{ IS_NO_RIPV2_IN,	0,		"NO_RIPV2_IN" },
    517 	{ IS_NO_RIPV1_OUT,	0,		"NO_RIPV1_OUT" },
    518 	{ IS_NO_RIPV2_OUT,	0,		"NO_RIPV2_OUT" },
    519 	{ (IS_NO_ADV_IN
    520 	   | IS_NO_SOL_OUT
    521 	   | IS_NO_ADV_OUT),	IS_BCAST_RDISC,	"NO_RDISC" },
    522 	{ IS_NO_SOL_OUT,	0,		"NO_SOLICIT" },
    523 	{ IS_SOL_OUT,		0,		"SEND_SOLICIT" },
    524 	{ IS_NO_ADV_OUT,	IS_BCAST_RDISC,	"NO_RDISC_ADV" },
    525 	{ IS_ADV_OUT,		0,		"RDISC_ADV" },
    526 	{ IS_BCAST_RDISC,	0,		"BCAST_RDISC" },
    527 	{ IS_PM_RDISC,		0,		"" },
    528 	{ 0,			0,		"%#x"}
    529 };
    530 
    531 static struct bits rs_bits[] = {
    532 	{ RS_IF,		0,		"IF" },
    533 	{ RS_NET_INT,		RS_NET_SYN,	"NET_INT" },
    534 	{ RS_NET_SYN,		0,		"NET_SYN" },
    535 	{ RS_SUBNET,		0,		"" },
    536 	{ RS_LOCAL,		0,		"LOCAL" },
    537 	{ RS_MHOME,		0,		"MHOME" },
    538 	{ RS_STATIC,		0,		"STATIC" },
    539 	{ RS_RDISC,		0,		"RDISC" },
    540 	{ 0,			0,		"%#x"}
    541 };
    542 
    543 
    544 static void
    545 trace_bits(const struct bits *tbl,
    546 	   u_int field,
    547 	   int force)
    548 {
    549 	u_int b;
    550 	char c;
    551 
    552 	if (force) {
    553 		(void)putc('<', ftrace);
    554 		c = 0;
    555 	} else {
    556 		c = '<';
    557 	}
    558 
    559 	while (field != 0
    560 	       && (b = tbl->bits_mask) != 0) {
    561 		if ((b & field) == b) {
    562 			if (tbl->bits_name[0] != '\0') {
    563 				if (c)
    564 					(void)putc(c, ftrace);
    565 				(void)fprintf(ftrace, "%s", tbl->bits_name);
    566 				c = '|';
    567 			}
    568 			if (0 == (field &= ~(b | tbl->bits_clear)))
    569 				break;
    570 		}
    571 		tbl++;
    572 	}
    573 	if (field != 0 && tbl->bits_name != 0) {
    574 		if (c)
    575 			(void)putc(c, ftrace);
    576 		(void)fprintf(ftrace, tbl->bits_name, field);
    577 		c = '|';
    578 	}
    579 
    580 	if (c != '<' || force)
    581 		(void)fputs("> ", ftrace);
    582 }
    583 
    584 
    585 char *
    586 rtname(naddr dst,
    587        naddr mask,
    588        naddr gate)
    589 {
    590 	static char buf[3*4+3+1+2+3	/* "xxx.xxx.xxx.xxx/xx-->" */
    591 			+3*4+3+1];	/* "xxx.xxx.xxx.xxx" */
    592 	int i;
    593 
    594 	i = snprintf(buf, sizeof(buf), "%-16s-->", addrname(dst, mask, 0));
    595 	if (i < 0 || (size_t)i >= sizeof(buf))
    596 		return buf;
    597 	(void)snprintf(&buf[i], sizeof(buf) - i, "%-*s", 15+20-MAX(20, i),
    598 	    naddr_ntoa(gate));
    599 	return buf;
    600 }
    601 
    602 
    603 static void
    604 print_rts(struct rt_spare *rts,
    605 	  int force_metric,		/* -1=suppress, 0=default */
    606 	  int force_ifp,		/* -1=suppress, 0=default */
    607 	  int force_router,		/* -1=suppress, 0=default, 1=display */
    608 	  int force_tag,		/* -1=suppress, 0=default, 1=display */
    609 	  int force_time)		/* 0=suppress, 1=display */
    610 {
    611 	int i;
    612 
    613 
    614 	if (force_metric >= 0)
    615 		(void)fprintf(ftrace, "metric=%-2d ", rts->rts_metric);
    616 	if (force_ifp >= 0)
    617 		(void)fprintf(ftrace, "%s ", (rts->rts_ifp == 0 ?
    618 					      "if?" : rts->rts_ifp->int_name));
    619 	if (force_router > 0
    620 	    || (force_router == 0 && rts->rts_router != rts->rts_gate))
    621 		(void)fprintf(ftrace, "router=%s ",
    622 			      naddr_ntoa(rts->rts_router));
    623 	if (force_time > 0)
    624 		(void)fprintf(ftrace, "%s ", ts(rts->rts_time));
    625 	if (force_tag > 0
    626 	    || (force_tag == 0 && rts->rts_tag != 0))
    627 		(void)fprintf(ftrace, "tag=%#x ", ntohs(rts->rts_tag));
    628 	if (rts->rts_de_ag != 0) {
    629 		for (i = 1; (u_int)(1 << i) <= rts->rts_de_ag; i++)
    630 			continue;
    631 		(void)fprintf(ftrace, "de_ag=%d ", i);
    632 	}
    633 
    634 }
    635 
    636 
    637 void
    638 trace_if(const char *act,
    639 	 struct interface *ifp)
    640 {
    641 	if (!TRACEACTIONS || ftrace == 0)
    642 		return;
    643 
    644 	lastlog();
    645 	(void)fprintf(ftrace, "%-3s interface %-4s ", act, ifp->int_name);
    646 	(void)fprintf(ftrace, "%-15s-->%-15s ",
    647 		      naddr_ntoa(ifp->int_addr),
    648 		      addrname(((ifp->int_if_flags & IFF_POINTOPOINT)
    649 				? ifp->int_dstaddr
    650 				: htonl(ifp->int_net)),
    651 			       ifp->int_mask, 1));
    652 	if (ifp->int_metric != 0)
    653 		(void)fprintf(ftrace, "metric=%d ", ifp->int_metric);
    654 	if (ifp->int_adj_inmetric != 0)
    655 		(void)fprintf(ftrace, "adj_inmetric=%u ",
    656 			      ifp->int_adj_inmetric);
    657 	if (ifp->int_adj_outmetric != 0)
    658 		(void)fprintf(ftrace, "adj_outmetric=%u ",
    659 			      ifp->int_adj_outmetric);
    660 	if (!IS_RIP_OUT_OFF(ifp->int_state)
    661 	    && ifp->int_d_metric != 0)
    662 		(void)fprintf(ftrace, "fake_default=%u ", ifp->int_d_metric);
    663 	trace_bits(if_bits, ifp->int_if_flags, 0);
    664 	trace_bits(is_bits, ifp->int_state, 0);
    665 	(void)fputc('\n',ftrace);
    666 }
    667 
    668 
    669 void
    670 trace_upslot(struct rt_entry *rt,
    671 	     struct rt_spare *rts,
    672 	     struct rt_spare *new)
    673 {
    674 	if (!TRACEACTIONS || ftrace == 0)
    675 		return;
    676 
    677 	if (rts->rts_gate == new->rts_gate
    678 	    && rts->rts_router == new->rts_router
    679 	    && rts->rts_metric == new->rts_metric
    680 	    && rts->rts_tag == new->rts_tag
    681 	    && rts->rts_de_ag == new->rts_de_ag)
    682 		return;
    683 
    684 	lastlog();
    685 	if (new->rts_gate == 0) {
    686 		(void)fprintf(ftrace, "Del #%d %-35s ",
    687 			      (int)(rts - rt->rt_spares),
    688 			      rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
    689 		print_rts(rts, 0,0,0,0,
    690 			  (rts != rt->rt_spares
    691 			   || AGE_RT(rt->rt_state,new->rts_ifp)));
    692 
    693 	} else if (rts->rts_gate != RIP_DEFAULT) {
    694 		(void)fprintf(ftrace, "Chg #%d %-35s ",
    695 			      (int)(rts - rt->rt_spares),
    696 			      rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
    697 		print_rts(rts, 0,0,
    698 			  rts->rts_gate != new->rts_gate,
    699 			  rts->rts_tag != new->rts_tag,
    700 			  rts != rt->rt_spares || AGE_RT(rt->rt_state,
    701 							rt->rt_ifp));
    702 
    703 		(void)fprintf(ftrace, "\n       %19s%-16s ", "",
    704 			      (new->rts_gate != rts->rts_gate
    705 			       ? naddr_ntoa(new->rts_gate) : ""));
    706 		print_rts(new,
    707 			  -(new->rts_metric == rts->rts_metric),
    708 			  -(new->rts_ifp == rts->rts_ifp),
    709 			  0,
    710 			  rts->rts_tag != new->rts_tag,
    711 			  (new->rts_time != rts->rts_time
    712 			   && (rts != rt->rt_spares
    713 			       || AGE_RT(rt->rt_state, new->rts_ifp))));
    714 
    715 	} else {
    716 		(void)fprintf(ftrace, "Add #%d %-35s ",
    717 			      (int)(rts - rt->rt_spares),
    718 			      rtname(rt->rt_dst, rt->rt_mask, new->rts_gate));
    719 		print_rts(new, 0,0,0,0,
    720 			  (rts != rt->rt_spares
    721 			   || AGE_RT(rt->rt_state,new->rts_ifp)));
    722 	}
    723 	(void)fputc('\n',ftrace);
    724 }
    725 
    726 
    727 /* miscellaneous message checked by the caller
    728  */
    729 void
    730 trace_misc(const char *p, ...)
    731 {
    732 	va_list args;
    733 
    734 	if (ftrace == 0)
    735 		return;
    736 
    737 	lastlog();
    738 	va_start(args, p);
    739 	vfprintf(ftrace, p, args);
    740 	va_end(args);
    741 	(void)fputc('\n',ftrace);
    742 }
    743 
    744 
    745 /* display a message if tracing actions
    746  */
    747 void
    748 trace_act(const char *p, ...)
    749 {
    750 	va_list args;
    751 
    752 	if (!TRACEACTIONS || ftrace == 0)
    753 		return;
    754 
    755 	lastlog();
    756 	va_start(args, p);
    757 	vfprintf(ftrace, p, args);
    758 	va_end(args);
    759 	(void)fputc('\n',ftrace);
    760 }
    761 
    762 
    763 /* display a message if tracing packets
    764  */
    765 void
    766 trace_pkt(const char *p, ...)
    767 {
    768 	va_list args;
    769 
    770 	if (!TRACEPACKETS || ftrace == 0)
    771 		return;
    772 
    773 	lastlog();
    774 	va_start(args, p);
    775 	vfprintf(ftrace, p, args);
    776 	va_end(args);
    777 	(void)fputc('\n',ftrace);
    778 }
    779 
    780 
    781 void
    782 trace_change(struct rt_entry *rt,
    783 	     u_int	state,
    784 	     struct	rt_spare *new,
    785 	     const char	*label)
    786 {
    787 	if (ftrace == 0)
    788 		return;
    789 
    790 	if (rt->rt_metric == new->rts_metric
    791 	    && rt->rt_gate == new->rts_gate
    792 	    && rt->rt_router == new->rts_router
    793 	    && rt->rt_state == state
    794 	    && rt->rt_tag == new->rts_tag
    795 	    && rt->rt_de_ag == new->rts_de_ag)
    796 		return;
    797 
    798 	lastlog();
    799 	(void)fprintf(ftrace, "%s %-35s ",
    800 		      label,
    801 		      rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
    802 	print_rts(rt->rt_spares,
    803 		  0,0,0,0, AGE_RT(rt->rt_state, rt->rt_ifp));
    804 	trace_bits(rs_bits, rt->rt_state, rt->rt_state != state);
    805 
    806 	(void)fprintf(ftrace, "\n%*s %19s%-16s ",
    807 		      (int)strlen(label), "", "",
    808 		      (rt->rt_gate != new->rts_gate
    809 		       ? naddr_ntoa(new->rts_gate) : ""));
    810 	print_rts(new,
    811 		  -(new->rts_metric == rt->rt_metric),
    812 		  -(new->rts_ifp == rt->rt_ifp),
    813 		  0,
    814 		  rt->rt_tag != new->rts_tag,
    815 		  (rt->rt_time != new->rts_time
    816 		   && AGE_RT(rt->rt_state,new->rts_ifp)));
    817 	if (rt->rt_state != state)
    818 		trace_bits(rs_bits, state, 1);
    819 	(void)fputc('\n',ftrace);
    820 }
    821 
    822 
    823 void
    824 trace_add_del(const char * action, struct rt_entry *rt)
    825 {
    826 	if (ftrace == 0)
    827 		return;
    828 
    829 	lastlog();
    830 	(void)fprintf(ftrace, "%s    %-35s ",
    831 		      action,
    832 		      rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
    833 	print_rts(rt->rt_spares, 0,0,0,0,AGE_RT(rt->rt_state,rt->rt_ifp));
    834 	trace_bits(rs_bits, rt->rt_state, 0);
    835 	(void)fputc('\n',ftrace);
    836 }
    837 
    838 
    839 /* ARGSUSED */
    840 static int
    841 walk_trace(struct radix_node *rn,
    842 	   struct walkarg *w UNUSED)
    843 {
    844 #define RT ((struct rt_entry *)rn)
    845 	struct rt_spare *rts;
    846 	int i;
    847 
    848 	(void)fprintf(ftrace, "  %-35s ",
    849 		      rtname(RT->rt_dst, RT->rt_mask, RT->rt_gate));
    850 	print_rts(&RT->rt_spares[0], 0,0,0,0, AGE_RT(RT->rt_state, RT->rt_ifp));
    851 	trace_bits(rs_bits, RT->rt_state, 0);
    852 	if (RT->rt_poison_time >= now_garbage
    853 	    && RT->rt_poison_metric < RT->rt_metric)
    854 		(void)fprintf(ftrace, "pm=%d@%s",
    855 			      RT->rt_poison_metric, ts(RT->rt_poison_time));
    856 
    857 	rts = &RT->rt_spares[1];
    858 	for (i = 1; i < NUM_SPARES; i++, rts++) {
    859 		if (rts->rts_gate != RIP_DEFAULT) {
    860 			(void)fprintf(ftrace,"\n    #%d%15s%-16s ",
    861 				      i, "", naddr_ntoa(rts->rts_gate));
    862 			print_rts(rts, 0,0,0,0,1);
    863 		}
    864 	}
    865 	(void)fputc('\n',ftrace);
    866 
    867 	return 0;
    868 }
    869 
    870 
    871 static void
    872 trace_dump(void)
    873 {
    874 	struct interface *ifp;
    875 
    876 	if (ftrace == 0)
    877 		return;
    878 	lastlog();
    879 
    880 	(void)fputs("current daemon state:\n", ftrace);
    881 	for (ifp = ifnet; ifp != 0; ifp = ifp->int_next)
    882 		trace_if("", ifp);
    883 	(void)rn_walktree(rhead, walk_trace, 0);
    884 }
    885 
    886 
    887 void
    888 trace_rip(const char *dir1, const char *dir2,
    889 	  struct sockaddr_in *who,
    890 	  struct interface *ifp,
    891 	  struct rip *msg,
    892 	  int size)			/* total size of message */
    893 {
    894 	struct netinfo *n, *lim;
    895 #	define NA ((struct netauth*)n)
    896 	int i, seen_route;
    897 
    898 	if (!TRACEPACKETS || ftrace == 0)
    899 		return;
    900 
    901 	lastlog();
    902 	if (msg->rip_cmd >= RIPCMD_MAX
    903 	    || msg->rip_vers == 0) {
    904 		(void)fprintf(ftrace, "%s bad RIPv%d cmd=%d %s"
    905 			      " %s.%d size=%d\n",
    906 			      dir1, msg->rip_vers, msg->rip_cmd, dir2,
    907 			      naddr_ntoa(who->sin_addr.s_addr),
    908 			      ntohs(who->sin_port),
    909 			      size);
    910 		return;
    911 	}
    912 
    913 	(void)fprintf(ftrace, "%s RIPv%d %s %s %s.%d%s%s\n",
    914 		      dir1, msg->rip_vers, ripcmds[msg->rip_cmd], dir2,
    915 		      naddr_ntoa(who->sin_addr.s_addr), ntohs(who->sin_port),
    916 		      ifp ? " via " : "", ifp ? ifp->int_name : "");
    917 	if (!TRACECONTENTS)
    918 		return;
    919 
    920 	seen_route = 0;
    921 	switch (msg->rip_cmd) {
    922 	case RIPCMD_REQUEST:
    923 	case RIPCMD_RESPONSE:
    924 		n = msg->rip_nets;
    925 		lim = (struct netinfo *)((char*)msg + size);
    926 		for (; n < lim; n++) {
    927 			if (!seen_route
    928 			    && n->n_family == RIP_AF_UNSPEC
    929 			    && ntohl(n->n_metric) == HOPCNT_INFINITY
    930 			    && msg->rip_cmd == RIPCMD_REQUEST
    931 			    && (n+1 == lim
    932 				|| (n+2 == lim
    933 				    && (n+1)->n_family == RIP_AF_AUTH))) {
    934 				(void)fputs("\tQUERY ", ftrace);
    935 				if (n->n_dst != 0)
    936 					(void)fprintf(ftrace, "%s ",
    937 						      naddr_ntoa(n->n_dst));
    938 				if (n->n_mask != 0)
    939 					(void)fprintf(ftrace, "mask=%#x ",
    940 						      (u_int)ntohl(n->n_mask));
    941 				if (n->n_nhop != 0)
    942 					(void)fprintf(ftrace, "nhop=%s ",
    943 						      naddr_ntoa(n->n_nhop));
    944 				if (n->n_tag != 0)
    945 					(void)fprintf(ftrace, "tag=%#x ",
    946 						      ntohs(n->n_tag));
    947 				(void)fputc('\n',ftrace);
    948 				continue;
    949 			}
    950 
    951 			if (n->n_family == RIP_AF_AUTH) {
    952 				if (NA->a_type == RIP_AUTH_PW
    953 				    && n == msg->rip_nets) {
    954 					(void)fprintf(ftrace, "\tPassword"
    955 						      " Authentication:"
    956 						      " \"%s\"\n",
    957 						      qstring(NA->au.au_pw,
    958 							  RIP_AUTH_PW_LEN));
    959 					continue;
    960 				}
    961 
    962 				if (NA->a_type == RIP_AUTH_MD5
    963 				    && n == msg->rip_nets) {
    964 					(void)fprintf(ftrace,
    965 						      "\tMD5 Auth"
    966 						      " pkt_len=%d KeyID=%u"
    967 						      " auth_len=%d"
    968 						      " seqno=%#x"
    969 						      " rsvd=%#x,%#x\n",
    970 					    ntohs(NA->au.a_md5.md5_pkt_len),
    971 					    NA->au.a_md5.md5_keyid,
    972 					    NA->au.a_md5.md5_auth_len,
    973 					    (int)ntohl(NA->au.a_md5.md5_seqno),
    974 					    (int)ntohs(NA->au.a_md5.rsvd[0]),
    975 					    (int)ntohs(NA->au.a_md5.rsvd[1]));
    976 					continue;
    977 				}
    978 				(void)fprintf(ftrace,
    979 					      "\tAuthentication type %d: ",
    980 					      ntohs(NA->a_type));
    981 				for (i = 0;
    982 				     i < (int)sizeof(NA->au.au_pw);
    983 				     i++)
    984 					(void)fprintf(ftrace, "%02x ",
    985 						      NA->au.au_pw[i]);
    986 				(void)fputc('\n',ftrace);
    987 				continue;
    988 			}
    989 
    990 			seen_route = 1;
    991 			if (n->n_family != RIP_AF_INET) {
    992 				(void)fprintf(ftrace,
    993 					      "\t(af %d) %-18s mask=%#x ",
    994 					      ntohs(n->n_family),
    995 					      naddr_ntoa(n->n_dst),
    996 					      (u_int)ntohl(n->n_mask));
    997 			} else if (msg->rip_vers == RIPv1) {
    998 				(void)fprintf(ftrace, "\t%-18s ",
    999 					      addrname(n->n_dst,
   1000 						       ntohl(n->n_mask),
   1001 						       n->n_mask==0 ? 2 : 1));
   1002 			} else {
   1003 				(void)fprintf(ftrace, "\t%-18s ",
   1004 					      addrname(n->n_dst,
   1005 						       ntohl(n->n_mask),
   1006 						       n->n_mask==0 ? 2 : 0));
   1007 			}
   1008 			(void)fprintf(ftrace, "metric=%-2d ",
   1009 				      (u_int)ntohl(n->n_metric));
   1010 			if (n->n_nhop != 0)
   1011 				(void)fprintf(ftrace, " nhop=%s ",
   1012 					      naddr_ntoa(n->n_nhop));
   1013 			if (n->n_tag != 0)
   1014 				(void)fprintf(ftrace, "tag=%#x",
   1015 					      ntohs(n->n_tag));
   1016 			(void)fputc('\n',ftrace);
   1017 		}
   1018 		if (size != (char *)n - (char *)msg)
   1019 			(void)fprintf(ftrace, "truncated record, len %d\n",
   1020 				size);
   1021 		break;
   1022 
   1023 	case RIPCMD_TRACEON:
   1024 		fprintf(ftrace, "\tfile=\"%.*s\"\n", size-4,
   1025 			msg->rip_tracefile);
   1026 		break;
   1027 
   1028 	case RIPCMD_TRACEOFF:
   1029 		break;
   1030 	}
   1031 }
   1032