trace.c revision 1.10 1 1.10 cgd /* $NetBSD: trace.c,v 1.10 1995/03/18 15:00:47 cgd 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.1 cgd * must display the following acknowledgement:
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 #ifndef lint
37 1.10 cgd #if 0
38 1.10 cgd static char sccsid[] = "@(#)trace.c 8.1 (Berkeley) 6/5/93";
39 1.10 cgd #else
40 1.10 cgd static char rcsid[] = "$NetBSD: trace.c,v 1.10 1995/03/18 15:00:47 cgd Exp $";
41 1.10 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * Routing Table Management Daemon
46 1.1 cgd */
47 1.1 cgd #define RIPCMDS
48 1.1 cgd #include "defs.h"
49 1.1 cgd #include <sys/stat.h>
50 1.1 cgd #include <sys/signal.h>
51 1.1 cgd #include <fcntl.h>
52 1.1 cgd #include "pathnames.h"
53 1.1 cgd
54 1.1 cgd #define NRECORDS 50 /* size of circular trace buffer */
55 1.1 cgd #ifdef DEBUG
56 1.1 cgd FILE *ftrace = stdout;
57 1.1 cgd int traceactions = 0;
58 1.1 cgd #endif
59 1.1 cgd static struct timeval lastlog;
60 1.1 cgd static char *savetracename;
61 1.1 cgd
62 1.7 cgd static int iftraceinit __P((struct interface *, struct ifdebug *));
63 1.7 cgd void dumpif __P((FILE *, struct interface *));
64 1.7 cgd void dumptrace __P((FILE *, char *, struct ifdebug *));
65 1.7 cgd
66 1.7 cgd void
67 1.1 cgd traceinit(ifp)
68 1.1 cgd register struct interface *ifp;
69 1.1 cgd {
70 1.1 cgd static int iftraceinit();
71 1.1 cgd
72 1.1 cgd if (iftraceinit(ifp, &ifp->int_input) &&
73 1.1 cgd iftraceinit(ifp, &ifp->int_output))
74 1.1 cgd return;
75 1.1 cgd tracehistory = 0;
76 1.1 cgd fprintf(stderr, "traceinit: can't init %s\n", ifp->int_name);
77 1.1 cgd }
78 1.1 cgd
79 1.7 cgd static int
80 1.1 cgd iftraceinit(ifp, ifd)
81 1.1 cgd struct interface *ifp;
82 1.1 cgd register struct ifdebug *ifd;
83 1.1 cgd {
84 1.1 cgd register struct iftrace *t;
85 1.1 cgd
86 1.1 cgd ifd->ifd_records =
87 1.1 cgd (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
88 1.1 cgd if (ifd->ifd_records == 0)
89 1.1 cgd return (0);
90 1.1 cgd ifd->ifd_front = ifd->ifd_records;
91 1.1 cgd ifd->ifd_count = 0;
92 1.1 cgd for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
93 1.1 cgd t->ift_size = 0;
94 1.1 cgd t->ift_packet = 0;
95 1.1 cgd }
96 1.1 cgd ifd->ifd_if = ifp;
97 1.1 cgd return (1);
98 1.1 cgd }
99 1.1 cgd
100 1.7 cgd void
101 1.1 cgd traceon(file)
102 1.1 cgd char *file;
103 1.1 cgd {
104 1.1 cgd struct stat stbuf;
105 1.1 cgd
106 1.1 cgd if (ftrace != NULL)
107 1.1 cgd return;
108 1.9 mycroft if (stat(file, &stbuf) >= 0 && !S_ISREG(stbuf.st_mode))
109 1.1 cgd return;
110 1.1 cgd savetracename = file;
111 1.1 cgd (void) gettimeofday(&now, (struct timezone *)NULL);
112 1.1 cgd ftrace = fopen(file, "a");
113 1.1 cgd if (ftrace == NULL)
114 1.1 cgd return;
115 1.1 cgd dup2(fileno(ftrace), 1);
116 1.1 cgd dup2(fileno(ftrace), 2);
117 1.1 cgd traceactions = 1;
118 1.1 cgd fprintf(ftrace, "Tracing enabled %s\n", ctime((time_t *)&now.tv_sec));
119 1.1 cgd }
120 1.1 cgd
121 1.7 cgd void
122 1.1 cgd traceoff()
123 1.1 cgd {
124 1.1 cgd if (!traceactions)
125 1.1 cgd return;
126 1.1 cgd if (ftrace != NULL) {
127 1.1 cgd int fd = open(_PATH_DEVNULL, O_RDWR);
128 1.1 cgd
129 1.1 cgd fprintf(ftrace, "Tracing disabled %s\n",
130 1.1 cgd ctime((time_t *)&now.tv_sec));
131 1.1 cgd fflush(ftrace);
132 1.1 cgd (void) dup2(fd, 1);
133 1.1 cgd (void) dup2(fd, 2);
134 1.1 cgd (void) close(fd);
135 1.1 cgd fclose(ftrace);
136 1.1 cgd ftrace = NULL;
137 1.1 cgd }
138 1.1 cgd traceactions = 0;
139 1.1 cgd tracehistory = 0;
140 1.1 cgd tracepackets = 0;
141 1.1 cgd tracecontents = 0;
142 1.1 cgd }
143 1.1 cgd
144 1.1 cgd void
145 1.1 cgd sigtrace(s)
146 1.1 cgd int s;
147 1.1 cgd {
148 1.1 cgd
149 1.1 cgd if (s == SIGUSR2)
150 1.1 cgd traceoff();
151 1.1 cgd else if (ftrace == NULL && savetracename)
152 1.1 cgd traceon(savetracename);
153 1.1 cgd else
154 1.1 cgd bumploglevel();
155 1.1 cgd }
156 1.1 cgd
157 1.1 cgd /*
158 1.1 cgd * Move to next higher level of tracing when -t option processed or
159 1.1 cgd * SIGUSR1 is received. Successive levels are:
160 1.1 cgd * traceactions
161 1.1 cgd * traceactions + tracepackets
162 1.1 cgd * traceactions + tracehistory (packets and contents after change)
163 1.1 cgd * traceactions + tracepackets + tracecontents
164 1.1 cgd */
165 1.7 cgd void
166 1.1 cgd bumploglevel()
167 1.1 cgd {
168 1.1 cgd
169 1.1 cgd (void) gettimeofday(&now, (struct timezone *)NULL);
170 1.1 cgd if (traceactions == 0) {
171 1.1 cgd traceactions++;
172 1.1 cgd if (ftrace)
173 1.1 cgd fprintf(ftrace, "Tracing actions started %s\n",
174 1.1 cgd ctime((time_t *)&now.tv_sec));
175 1.1 cgd } else if (tracepackets == 0) {
176 1.1 cgd tracepackets++;
177 1.1 cgd tracehistory = 0;
178 1.1 cgd tracecontents = 0;
179 1.1 cgd if (ftrace)
180 1.1 cgd fprintf(ftrace, "Tracing packets started %s\n",
181 1.1 cgd ctime((time_t *)&now.tv_sec));
182 1.1 cgd } else if (tracehistory == 0) {
183 1.1 cgd tracehistory++;
184 1.1 cgd if (ftrace)
185 1.1 cgd fprintf(ftrace, "Tracing history started %s\n",
186 1.1 cgd ctime((time_t *)&now.tv_sec));
187 1.1 cgd } else {
188 1.1 cgd tracepackets++;
189 1.1 cgd tracecontents++;
190 1.1 cgd tracehistory = 0;
191 1.1 cgd if (ftrace)
192 1.1 cgd fprintf(ftrace, "Tracing packet contents started %s\n",
193 1.1 cgd ctime((time_t *)&now.tv_sec));
194 1.1 cgd }
195 1.1 cgd if (ftrace)
196 1.1 cgd fflush(ftrace);
197 1.1 cgd }
198 1.1 cgd
199 1.7 cgd void
200 1.1 cgd trace(ifd, who, p, len, m)
201 1.1 cgd register struct ifdebug *ifd;
202 1.1 cgd struct sockaddr *who;
203 1.1 cgd char *p;
204 1.1 cgd int len, m;
205 1.1 cgd {
206 1.1 cgd register struct iftrace *t;
207 1.1 cgd
208 1.1 cgd if (ifd->ifd_records == 0)
209 1.1 cgd return;
210 1.1 cgd t = ifd->ifd_front++;
211 1.1 cgd if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
212 1.1 cgd ifd->ifd_front = ifd->ifd_records;
213 1.1 cgd if (ifd->ifd_count < NRECORDS)
214 1.1 cgd ifd->ifd_count++;
215 1.1 cgd if (t->ift_size > 0 && t->ift_size < len && t->ift_packet) {
216 1.1 cgd free(t->ift_packet);
217 1.1 cgd t->ift_packet = 0;
218 1.1 cgd }
219 1.1 cgd t->ift_stamp = now;
220 1.1 cgd t->ift_who = *who;
221 1.1 cgd if (len > 0 && t->ift_packet == 0) {
222 1.1 cgd t->ift_packet = malloc(len);
223 1.1 cgd if (t->ift_packet == 0)
224 1.1 cgd len = 0;
225 1.1 cgd }
226 1.1 cgd if (len > 0)
227 1.6 mycroft memcpy(t->ift_packet, p, len);
228 1.1 cgd t->ift_size = len;
229 1.1 cgd t->ift_metric = m;
230 1.1 cgd }
231 1.1 cgd
232 1.7 cgd void
233 1.1 cgd traceaction(fd, action, rt)
234 1.1 cgd FILE *fd;
235 1.1 cgd char *action;
236 1.1 cgd struct rt_entry *rt;
237 1.1 cgd {
238 1.1 cgd struct sockaddr_in *dst, *gate;
239 1.1 cgd static struct bits {
240 1.1 cgd int t_bits;
241 1.1 cgd char *t_name;
242 1.1 cgd } flagbits[] = {
243 1.1 cgd { RTF_UP, "UP" },
244 1.1 cgd { RTF_GATEWAY, "GATEWAY" },
245 1.1 cgd { RTF_HOST, "HOST" },
246 1.1 cgd { 0 }
247 1.1 cgd }, statebits[] = {
248 1.1 cgd { RTS_PASSIVE, "PASSIVE" },
249 1.1 cgd { RTS_REMOTE, "REMOTE" },
250 1.1 cgd { RTS_INTERFACE,"INTERFACE" },
251 1.1 cgd { RTS_CHANGED, "CHANGED" },
252 1.1 cgd { RTS_INTERNAL, "INTERNAL" },
253 1.1 cgd { RTS_EXTERNAL, "EXTERNAL" },
254 1.1 cgd { RTS_SUBNET, "SUBNET" },
255 1.1 cgd { 0 }
256 1.1 cgd };
257 1.1 cgd register struct bits *p;
258 1.1 cgd register int first;
259 1.1 cgd char *cp;
260 1.1 cgd
261 1.1 cgd if (fd == NULL)
262 1.1 cgd return;
263 1.1 cgd if (lastlog.tv_sec != now.tv_sec || lastlog.tv_usec != now.tv_usec) {
264 1.1 cgd fprintf(fd, "\n%.19s:\n", ctime((time_t *)&now.tv_sec));
265 1.1 cgd lastlog = now;
266 1.1 cgd }
267 1.1 cgd fprintf(fd, "%s ", action);
268 1.1 cgd dst = (struct sockaddr_in *)&rt->rt_dst;
269 1.1 cgd gate = (struct sockaddr_in *)&rt->rt_router;
270 1.1 cgd fprintf(fd, "dst %s, ", inet_ntoa(dst->sin_addr));
271 1.1 cgd fprintf(fd, "router %s, metric %d, flags",
272 1.1 cgd inet_ntoa(gate->sin_addr), rt->rt_metric);
273 1.1 cgd cp = " %s";
274 1.1 cgd for (first = 1, p = flagbits; p->t_bits > 0; p++) {
275 1.1 cgd if ((rt->rt_flags & p->t_bits) == 0)
276 1.1 cgd continue;
277 1.1 cgd fprintf(fd, cp, p->t_name);
278 1.1 cgd if (first) {
279 1.1 cgd cp = "|%s";
280 1.1 cgd first = 0;
281 1.1 cgd }
282 1.1 cgd }
283 1.1 cgd fprintf(fd, " state");
284 1.1 cgd cp = " %s";
285 1.1 cgd for (first = 1, p = statebits; p->t_bits > 0; p++) {
286 1.1 cgd if ((rt->rt_state & p->t_bits) == 0)
287 1.1 cgd continue;
288 1.1 cgd fprintf(fd, cp, p->t_name);
289 1.1 cgd if (first) {
290 1.1 cgd cp = "|%s";
291 1.1 cgd first = 0;
292 1.1 cgd }
293 1.1 cgd }
294 1.1 cgd fprintf(fd, " timer %d\n", rt->rt_timer);
295 1.1 cgd if (tracehistory && !tracepackets &&
296 1.1 cgd (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
297 1.1 cgd dumpif(fd, rt->rt_ifp);
298 1.1 cgd fflush(fd);
299 1.1 cgd if (ferror(fd))
300 1.1 cgd traceoff();
301 1.1 cgd }
302 1.1 cgd
303 1.7 cgd void
304 1.1 cgd tracenewmetric(fd, rt, newmetric)
305 1.1 cgd FILE *fd;
306 1.1 cgd struct rt_entry *rt;
307 1.1 cgd int newmetric;
308 1.1 cgd {
309 1.1 cgd struct sockaddr_in *dst, *gate;
310 1.1 cgd
311 1.1 cgd if (fd == NULL)
312 1.1 cgd return;
313 1.1 cgd if (lastlog.tv_sec != now.tv_sec || lastlog.tv_usec != now.tv_usec) {
314 1.1 cgd fprintf(fd, "\n%.19s:\n", ctime((time_t *)&now.tv_sec));
315 1.1 cgd lastlog = now;
316 1.1 cgd }
317 1.1 cgd dst = (struct sockaddr_in *)&rt->rt_dst;
318 1.1 cgd gate = (struct sockaddr_in *)&rt->rt_router;
319 1.1 cgd fprintf(fd, "CHANGE metric dst %s, ", inet_ntoa(dst->sin_addr));
320 1.1 cgd fprintf(fd, "router %s, from %d to %d\n",
321 1.1 cgd inet_ntoa(gate->sin_addr), rt->rt_metric, newmetric);
322 1.1 cgd fflush(fd);
323 1.1 cgd if (ferror(fd))
324 1.1 cgd traceoff();
325 1.1 cgd }
326 1.1 cgd
327 1.7 cgd void
328 1.1 cgd dumpif(fd, ifp)
329 1.1 cgd FILE *fd;
330 1.1 cgd register struct interface *ifp;
331 1.1 cgd {
332 1.1 cgd if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
333 1.1 cgd fprintf(fd, "*** Packet history for interface %s ***\n",
334 1.1 cgd ifp->int_name);
335 1.1 cgd #ifdef notneeded
336 1.1 cgd dumptrace(fd, "to", &ifp->int_output);
337 1.1 cgd #endif
338 1.1 cgd dumptrace(fd, "from", &ifp->int_input);
339 1.1 cgd fprintf(fd, "*** end packet history ***\n");
340 1.1 cgd }
341 1.1 cgd }
342 1.1 cgd
343 1.7 cgd void
344 1.1 cgd dumptrace(fd, dir, ifd)
345 1.1 cgd FILE *fd;
346 1.1 cgd char *dir;
347 1.1 cgd register struct ifdebug *ifd;
348 1.1 cgd {
349 1.1 cgd register struct iftrace *t;
350 1.1 cgd char *cp = !strcmp(dir, "to") ? "Output" : "Input";
351 1.1 cgd
352 1.1 cgd if (ifd->ifd_front == ifd->ifd_records &&
353 1.1 cgd ifd->ifd_front->ift_size == 0) {
354 1.1 cgd fprintf(fd, "%s: no packets.\n", cp);
355 1.1 cgd fflush(fd);
356 1.1 cgd return;
357 1.1 cgd }
358 1.1 cgd fprintf(fd, "%s trace:\n", cp);
359 1.1 cgd t = ifd->ifd_front - ifd->ifd_count;
360 1.1 cgd if (t < ifd->ifd_records)
361 1.1 cgd t += NRECORDS;
362 1.1 cgd for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
363 1.1 cgd if (t >= ifd->ifd_records + NRECORDS)
364 1.1 cgd t = ifd->ifd_records;
365 1.1 cgd if (t->ift_size == 0)
366 1.1 cgd continue;
367 1.1 cgd dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size,
368 1.1 cgd &t->ift_stamp);
369 1.1 cgd }
370 1.1 cgd }
371 1.1 cgd
372 1.7 cgd void
373 1.1 cgd dumppacket(fd, dir, who, cp, size, stamp)
374 1.1 cgd FILE *fd;
375 1.1 cgd struct sockaddr_in *who; /* should be sockaddr */
376 1.1 cgd char *dir, *cp;
377 1.1 cgd register int size;
378 1.1 cgd struct timeval *stamp;
379 1.1 cgd {
380 1.1 cgd register struct rip *msg = (struct rip *)cp;
381 1.1 cgd register struct netinfo *n;
382 1.1 cgd
383 1.1 cgd if (fd == NULL)
384 1.1 cgd return;
385 1.1 cgd if (msg->rip_cmd && msg->rip_cmd < RIPCMD_MAX)
386 1.1 cgd fprintf(fd, "%s %s %s.%d %.19s:\n", ripcmds[msg->rip_cmd],
387 1.1 cgd dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port),
388 1.1 cgd ctime((time_t *)&stamp->tv_sec));
389 1.1 cgd else {
390 1.1 cgd fprintf(fd, "Bad cmd 0x%x %s %x.%d %.19s\n", msg->rip_cmd,
391 1.7 cgd dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port),
392 1.1 cgd ctime((time_t *)&stamp->tv_sec));
393 1.7 cgd fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet);
394 1.1 cgd fflush(fd);
395 1.1 cgd return;
396 1.1 cgd }
397 1.1 cgd if (tracepackets && tracecontents == 0) {
398 1.1 cgd fflush(fd);
399 1.1 cgd return;
400 1.1 cgd }
401 1.1 cgd switch (msg->rip_cmd) {
402 1.1 cgd
403 1.1 cgd case RIPCMD_REQUEST:
404 1.1 cgd case RIPCMD_RESPONSE:
405 1.1 cgd size -= 4 * sizeof (char);
406 1.1 cgd n = msg->rip_nets;
407 1.1 cgd for (; size > 0; n++, size -= sizeof (struct netinfo)) {
408 1.1 cgd if (size < sizeof (struct netinfo)) {
409 1.1 cgd fprintf(fd, "(truncated record, len %d)\n",
410 1.1 cgd size);
411 1.1 cgd break;
412 1.1 cgd }
413 1.1 cgd if (sizeof(n->rip_dst.sa_family) > 1)
414 1.1 cgd n->rip_dst.sa_family = ntohs(n->rip_dst.sa_family);
415 1.1 cgd
416 1.1 cgd switch ((int)n->rip_dst.sa_family) {
417 1.1 cgd
418 1.1 cgd case AF_INET:
419 1.1 cgd fprintf(fd, "\tdst %s metric %d\n",
420 1.1 cgd #define satosin(sa) ((struct sockaddr_in *)&sa)
421 1.1 cgd inet_ntoa(satosin(n->rip_dst)->sin_addr),
422 1.1 cgd ntohl(n->rip_metric));
423 1.1 cgd break;
424 1.1 cgd
425 1.1 cgd default:
426 1.1 cgd fprintf(fd, "\taf %d? metric %d\n",
427 1.1 cgd n->rip_dst.sa_family,
428 1.1 cgd ntohl(n->rip_metric));
429 1.1 cgd break;
430 1.1 cgd }
431 1.1 cgd }
432 1.1 cgd break;
433 1.1 cgd
434 1.1 cgd case RIPCMD_TRACEON:
435 1.1 cgd fprintf(fd, "\tfile=%*s\n", size, msg->rip_tracefile);
436 1.1 cgd break;
437 1.1 cgd
438 1.1 cgd case RIPCMD_TRACEOFF:
439 1.1 cgd break;
440 1.1 cgd }
441 1.1 cgd fflush(fd);
442 1.1 cgd if (ferror(fd))
443 1.1 cgd traceoff();
444 1.1 cgd }
445