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