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