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