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