Home | History | Annotate | Line # | Download | only in routed
trace.c revision 1.10
      1 /*	$NetBSD: trace.c,v 1.10 1995/03/18 15:00:47 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)trace.c	8.1 (Berkeley) 6/5/93";
     39 #else
     40 static char rcsid[] = "$NetBSD: trace.c,v 1.10 1995/03/18 15:00:47 cgd Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 /*
     45  * Routing Table Management Daemon
     46  */
     47 #define	RIPCMDS
     48 #include "defs.h"
     49 #include <sys/stat.h>
     50 #include <sys/signal.h>
     51 #include <fcntl.h>
     52 #include "pathnames.h"
     53 
     54 #define	NRECORDS	50		/* size of circular trace buffer */
     55 #ifdef DEBUG
     56 FILE	*ftrace = stdout;
     57 int	traceactions = 0;
     58 #endif
     59 static	struct timeval lastlog;
     60 static	char *savetracename;
     61 
     62 static int iftraceinit __P((struct interface *, struct ifdebug *));
     63 void dumpif __P((FILE *, struct interface *));
     64 void dumptrace __P((FILE *, char *, struct ifdebug *));
     65 
     66 void
     67 traceinit(ifp)
     68 	register struct interface *ifp;
     69 {
     70 	static int iftraceinit();
     71 
     72 	if (iftraceinit(ifp, &ifp->int_input) &&
     73 	    iftraceinit(ifp, &ifp->int_output))
     74 		return;
     75 	tracehistory = 0;
     76 	fprintf(stderr, "traceinit: can't init %s\n", ifp->int_name);
     77 }
     78 
     79 static int
     80 iftraceinit(ifp, ifd)
     81 	struct interface *ifp;
     82 	register struct ifdebug *ifd;
     83 {
     84 	register struct iftrace *t;
     85 
     86 	ifd->ifd_records =
     87 	  (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
     88 	if (ifd->ifd_records == 0)
     89 		return (0);
     90 	ifd->ifd_front = ifd->ifd_records;
     91 	ifd->ifd_count = 0;
     92 	for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
     93 		t->ift_size = 0;
     94 		t->ift_packet = 0;
     95 	}
     96 	ifd->ifd_if = ifp;
     97 	return (1);
     98 }
     99 
    100 void
    101 traceon(file)
    102 	char *file;
    103 {
    104 	struct stat stbuf;
    105 
    106 	if (ftrace != NULL)
    107 		return;
    108 	if (stat(file, &stbuf) >= 0 && !S_ISREG(stbuf.st_mode))
    109 		return;
    110 	savetracename = file;
    111 	(void) gettimeofday(&now, (struct timezone *)NULL);
    112 	ftrace = fopen(file, "a");
    113 	if (ftrace == NULL)
    114 		return;
    115 	dup2(fileno(ftrace), 1);
    116 	dup2(fileno(ftrace), 2);
    117 	traceactions = 1;
    118 	fprintf(ftrace, "Tracing enabled %s\n", ctime((time_t *)&now.tv_sec));
    119 }
    120 
    121 void
    122 traceoff()
    123 {
    124 	if (!traceactions)
    125 		return;
    126 	if (ftrace != NULL) {
    127 		int fd = open(_PATH_DEVNULL, O_RDWR);
    128 
    129 		fprintf(ftrace, "Tracing disabled %s\n",
    130 		    ctime((time_t *)&now.tv_sec));
    131 		fflush(ftrace);
    132 		(void) dup2(fd, 1);
    133 		(void) dup2(fd, 2);
    134 		(void) close(fd);
    135 		fclose(ftrace);
    136 		ftrace = NULL;
    137 	}
    138 	traceactions = 0;
    139 	tracehistory = 0;
    140 	tracepackets = 0;
    141 	tracecontents = 0;
    142 }
    143 
    144 void
    145 sigtrace(s)
    146 	int s;
    147 {
    148 
    149 	if (s == SIGUSR2)
    150 		traceoff();
    151 	else if (ftrace == NULL && savetracename)
    152 		traceon(savetracename);
    153 	else
    154 		bumploglevel();
    155 }
    156 
    157 /*
    158  * Move to next higher level of tracing when -t option processed or
    159  * SIGUSR1 is received.  Successive levels are:
    160  *	traceactions
    161  *	traceactions + tracepackets
    162  *	traceactions + tracehistory (packets and contents after change)
    163  *	traceactions + tracepackets + tracecontents
    164  */
    165 void
    166 bumploglevel()
    167 {
    168 
    169 	(void) gettimeofday(&now, (struct timezone *)NULL);
    170 	if (traceactions == 0) {
    171 		traceactions++;
    172 		if (ftrace)
    173 			fprintf(ftrace, "Tracing actions started %s\n",
    174 			    ctime((time_t *)&now.tv_sec));
    175 	} else if (tracepackets == 0) {
    176 		tracepackets++;
    177 		tracehistory = 0;
    178 		tracecontents = 0;
    179 		if (ftrace)
    180 			fprintf(ftrace, "Tracing packets started %s\n",
    181 			    ctime((time_t *)&now.tv_sec));
    182 	} else if (tracehistory == 0) {
    183 		tracehistory++;
    184 		if (ftrace)
    185 			fprintf(ftrace, "Tracing history started %s\n",
    186 			    ctime((time_t *)&now.tv_sec));
    187 	} else {
    188 		tracepackets++;
    189 		tracecontents++;
    190 		tracehistory = 0;
    191 		if (ftrace)
    192 			fprintf(ftrace, "Tracing packet contents started %s\n",
    193 			    ctime((time_t *)&now.tv_sec));
    194 	}
    195 	if (ftrace)
    196 		fflush(ftrace);
    197 }
    198 
    199 void
    200 trace(ifd, who, p, len, m)
    201 	register struct ifdebug *ifd;
    202 	struct sockaddr *who;
    203 	char *p;
    204 	int len, m;
    205 {
    206 	register struct iftrace *t;
    207 
    208 	if (ifd->ifd_records == 0)
    209 		return;
    210 	t = ifd->ifd_front++;
    211 	if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
    212 		ifd->ifd_front = ifd->ifd_records;
    213 	if (ifd->ifd_count < NRECORDS)
    214 		ifd->ifd_count++;
    215 	if (t->ift_size > 0 && t->ift_size < len && t->ift_packet) {
    216 		free(t->ift_packet);
    217 		t->ift_packet = 0;
    218 	}
    219 	t->ift_stamp = now;
    220 	t->ift_who = *who;
    221 	if (len > 0 && t->ift_packet == 0) {
    222 		t->ift_packet = malloc(len);
    223 		if (t->ift_packet == 0)
    224 			len = 0;
    225 	}
    226 	if (len > 0)
    227 		memcpy(t->ift_packet, p, len);
    228 	t->ift_size = len;
    229 	t->ift_metric = m;
    230 }
    231 
    232 void
    233 traceaction(fd, action, rt)
    234 	FILE *fd;
    235 	char *action;
    236 	struct rt_entry *rt;
    237 {
    238 	struct sockaddr_in *dst, *gate;
    239 	static struct bits {
    240 		int	t_bits;
    241 		char	*t_name;
    242 	} flagbits[] = {
    243 		{ RTF_UP,	"UP" },
    244 		{ RTF_GATEWAY,	"GATEWAY" },
    245 		{ RTF_HOST,	"HOST" },
    246 		{ 0 }
    247 	}, statebits[] = {
    248 		{ RTS_PASSIVE,	"PASSIVE" },
    249 		{ RTS_REMOTE,	"REMOTE" },
    250 		{ RTS_INTERFACE,"INTERFACE" },
    251 		{ RTS_CHANGED,	"CHANGED" },
    252 		{ RTS_INTERNAL,	"INTERNAL" },
    253 		{ RTS_EXTERNAL,	"EXTERNAL" },
    254 		{ RTS_SUBNET,	"SUBNET" },
    255 		{ 0 }
    256 	};
    257 	register struct bits *p;
    258 	register int first;
    259 	char *cp;
    260 
    261 	if (fd == NULL)
    262 		return;
    263 	if (lastlog.tv_sec != now.tv_sec || lastlog.tv_usec != now.tv_usec) {
    264 		fprintf(fd, "\n%.19s:\n", ctime((time_t *)&now.tv_sec));
    265 		lastlog = now;
    266 	}
    267 	fprintf(fd, "%s ", action);
    268 	dst = (struct sockaddr_in *)&rt->rt_dst;
    269 	gate = (struct sockaddr_in *)&rt->rt_router;
    270 	fprintf(fd, "dst %s, ", inet_ntoa(dst->sin_addr));
    271 	fprintf(fd, "router %s, metric %d, flags",
    272 	     inet_ntoa(gate->sin_addr), rt->rt_metric);
    273 	cp = " %s";
    274 	for (first = 1, p = flagbits; p->t_bits > 0; p++) {
    275 		if ((rt->rt_flags & p->t_bits) == 0)
    276 			continue;
    277 		fprintf(fd, cp, p->t_name);
    278 		if (first) {
    279 			cp = "|%s";
    280 			first = 0;
    281 		}
    282 	}
    283 	fprintf(fd, " state");
    284 	cp = " %s";
    285 	for (first = 1, p = statebits; p->t_bits > 0; p++) {
    286 		if ((rt->rt_state & p->t_bits) == 0)
    287 			continue;
    288 		fprintf(fd, cp, p->t_name);
    289 		if (first) {
    290 			cp = "|%s";
    291 			first = 0;
    292 		}
    293 	}
    294 	fprintf(fd, " timer %d\n", rt->rt_timer);
    295 	if (tracehistory && !tracepackets &&
    296 	    (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
    297 		dumpif(fd, rt->rt_ifp);
    298 	fflush(fd);
    299 	if (ferror(fd))
    300 		traceoff();
    301 }
    302 
    303 void
    304 tracenewmetric(fd, rt, newmetric)
    305 	FILE *fd;
    306 	struct rt_entry *rt;
    307 	int newmetric;
    308 {
    309 	struct sockaddr_in *dst, *gate;
    310 
    311 	if (fd == NULL)
    312 		return;
    313 	if (lastlog.tv_sec != now.tv_sec || lastlog.tv_usec != now.tv_usec) {
    314 		fprintf(fd, "\n%.19s:\n", ctime((time_t *)&now.tv_sec));
    315 		lastlog = now;
    316 	}
    317 	dst = (struct sockaddr_in *)&rt->rt_dst;
    318 	gate = (struct sockaddr_in *)&rt->rt_router;
    319 	fprintf(fd, "CHANGE metric dst %s, ", inet_ntoa(dst->sin_addr));
    320 	fprintf(fd, "router %s, from %d to %d\n",
    321 	     inet_ntoa(gate->sin_addr), rt->rt_metric, newmetric);
    322 	fflush(fd);
    323 	if (ferror(fd))
    324 		traceoff();
    325 }
    326 
    327 void
    328 dumpif(fd, ifp)
    329 	FILE *fd;
    330 	register struct interface *ifp;
    331 {
    332 	if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
    333 		fprintf(fd, "*** Packet history for interface %s ***\n",
    334 			ifp->int_name);
    335 #ifdef notneeded
    336 		dumptrace(fd, "to", &ifp->int_output);
    337 #endif
    338 		dumptrace(fd, "from", &ifp->int_input);
    339 		fprintf(fd, "*** end packet history ***\n");
    340 	}
    341 }
    342 
    343 void
    344 dumptrace(fd, dir, ifd)
    345 	FILE *fd;
    346 	char *dir;
    347 	register struct ifdebug *ifd;
    348 {
    349 	register struct iftrace *t;
    350 	char *cp = !strcmp(dir, "to") ? "Output" : "Input";
    351 
    352 	if (ifd->ifd_front == ifd->ifd_records &&
    353 	    ifd->ifd_front->ift_size == 0) {
    354 		fprintf(fd, "%s: no packets.\n", cp);
    355 		fflush(fd);
    356 		return;
    357 	}
    358 	fprintf(fd, "%s trace:\n", cp);
    359 	t = ifd->ifd_front - ifd->ifd_count;
    360 	if (t < ifd->ifd_records)
    361 		t += NRECORDS;
    362 	for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
    363 		if (t >= ifd->ifd_records + NRECORDS)
    364 			t = ifd->ifd_records;
    365 		if (t->ift_size == 0)
    366 			continue;
    367 		dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size,
    368 		    &t->ift_stamp);
    369 	}
    370 }
    371 
    372 void
    373 dumppacket(fd, dir, who, cp, size, stamp)
    374 	FILE *fd;
    375 	struct sockaddr_in *who;		/* should be sockaddr */
    376 	char *dir, *cp;
    377 	register int size;
    378 	struct timeval *stamp;
    379 {
    380 	register struct rip *msg = (struct rip *)cp;
    381 	register struct netinfo *n;
    382 
    383 	if (fd == NULL)
    384 		return;
    385 	if (msg->rip_cmd && msg->rip_cmd < RIPCMD_MAX)
    386 		fprintf(fd, "%s %s %s.%d %.19s:\n", ripcmds[msg->rip_cmd],
    387 		    dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port),
    388 		    ctime((time_t *)&stamp->tv_sec));
    389 	else {
    390 		fprintf(fd, "Bad cmd 0x%x %s %x.%d %.19s\n", msg->rip_cmd,
    391 		    dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port),
    392 		    ctime((time_t *)&stamp->tv_sec));
    393 		fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet);
    394 		fflush(fd);
    395 		return;
    396 	}
    397 	if (tracepackets && tracecontents == 0) {
    398 		fflush(fd);
    399 		return;
    400 	}
    401 	switch (msg->rip_cmd) {
    402 
    403 	case RIPCMD_REQUEST:
    404 	case RIPCMD_RESPONSE:
    405 		size -= 4 * sizeof (char);
    406 		n = msg->rip_nets;
    407 		for (; size > 0; n++, size -= sizeof (struct netinfo)) {
    408 			if (size < sizeof (struct netinfo)) {
    409 				fprintf(fd, "(truncated record, len %d)\n",
    410 				    size);
    411 				break;
    412 			}
    413 			if (sizeof(n->rip_dst.sa_family) > 1)
    414 			    n->rip_dst.sa_family = ntohs(n->rip_dst.sa_family);
    415 
    416 			switch ((int)n->rip_dst.sa_family) {
    417 
    418 			case AF_INET:
    419 				fprintf(fd, "\tdst %s metric %d\n",
    420 #define	satosin(sa)	((struct sockaddr_in *)&sa)
    421 				     inet_ntoa(satosin(n->rip_dst)->sin_addr),
    422 				     ntohl(n->rip_metric));
    423 				break;
    424 
    425 			default:
    426 				fprintf(fd, "\taf %d? metric %d\n",
    427 				     n->rip_dst.sa_family,
    428 				     ntohl(n->rip_metric));
    429 				break;
    430 			}
    431 		}
    432 		break;
    433 
    434 	case RIPCMD_TRACEON:
    435 		fprintf(fd, "\tfile=%*s\n", size, msg->rip_tracefile);
    436 		break;
    437 
    438 	case RIPCMD_TRACEOFF:
    439 		break;
    440 	}
    441 	fflush(fd);
    442 	if (ferror(fd))
    443 		traceoff();
    444 }
    445