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