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