trace.c revision 1.9 1 /*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)trace.c 8.1 (Berkeley) 6/5/93";*/
36 static char *rcsid = "$Id: trace.c,v 1.9 1995/01/30 19:42:24 mycroft Exp $";
37 #endif /* not lint */
38
39 /*
40 * Routing Table Management Daemon
41 */
42 #define RIPCMDS
43 #include "defs.h"
44 #include <sys/stat.h>
45 #include <sys/signal.h>
46 #include <fcntl.h>
47 #include "pathnames.h"
48
49 #define NRECORDS 50 /* size of circular trace buffer */
50 #ifdef DEBUG
51 FILE *ftrace = stdout;
52 int traceactions = 0;
53 #endif
54 static struct timeval lastlog;
55 static char *savetracename;
56
57 static int iftraceinit __P((struct interface *, struct ifdebug *));
58 void dumpif __P((FILE *, struct interface *));
59 void dumptrace __P((FILE *, char *, struct ifdebug *));
60
61 void
62 traceinit(ifp)
63 register struct interface *ifp;
64 {
65 static int iftraceinit();
66
67 if (iftraceinit(ifp, &ifp->int_input) &&
68 iftraceinit(ifp, &ifp->int_output))
69 return;
70 tracehistory = 0;
71 fprintf(stderr, "traceinit: can't init %s\n", ifp->int_name);
72 }
73
74 static int
75 iftraceinit(ifp, ifd)
76 struct interface *ifp;
77 register struct ifdebug *ifd;
78 {
79 register struct iftrace *t;
80
81 ifd->ifd_records =
82 (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
83 if (ifd->ifd_records == 0)
84 return (0);
85 ifd->ifd_front = ifd->ifd_records;
86 ifd->ifd_count = 0;
87 for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
88 t->ift_size = 0;
89 t->ift_packet = 0;
90 }
91 ifd->ifd_if = ifp;
92 return (1);
93 }
94
95 void
96 traceon(file)
97 char *file;
98 {
99 struct stat stbuf;
100
101 if (ftrace != NULL)
102 return;
103 if (stat(file, &stbuf) >= 0 && !S_ISREG(stbuf.st_mode))
104 return;
105 savetracename = file;
106 (void) gettimeofday(&now, (struct timezone *)NULL);
107 ftrace = fopen(file, "a");
108 if (ftrace == NULL)
109 return;
110 dup2(fileno(ftrace), 1);
111 dup2(fileno(ftrace), 2);
112 traceactions = 1;
113 fprintf(ftrace, "Tracing enabled %s\n", ctime((time_t *)&now.tv_sec));
114 }
115
116 void
117 traceoff()
118 {
119 if (!traceactions)
120 return;
121 if (ftrace != NULL) {
122 int fd = open(_PATH_DEVNULL, O_RDWR);
123
124 fprintf(ftrace, "Tracing disabled %s\n",
125 ctime((time_t *)&now.tv_sec));
126 fflush(ftrace);
127 (void) dup2(fd, 1);
128 (void) dup2(fd, 2);
129 (void) close(fd);
130 fclose(ftrace);
131 ftrace = NULL;
132 }
133 traceactions = 0;
134 tracehistory = 0;
135 tracepackets = 0;
136 tracecontents = 0;
137 }
138
139 void
140 sigtrace(s)
141 int s;
142 {
143
144 if (s == SIGUSR2)
145 traceoff();
146 else if (ftrace == NULL && savetracename)
147 traceon(savetracename);
148 else
149 bumploglevel();
150 }
151
152 /*
153 * Move to next higher level of tracing when -t option processed or
154 * SIGUSR1 is received. Successive levels are:
155 * traceactions
156 * traceactions + tracepackets
157 * traceactions + tracehistory (packets and contents after change)
158 * traceactions + tracepackets + tracecontents
159 */
160 void
161 bumploglevel()
162 {
163
164 (void) gettimeofday(&now, (struct timezone *)NULL);
165 if (traceactions == 0) {
166 traceactions++;
167 if (ftrace)
168 fprintf(ftrace, "Tracing actions started %s\n",
169 ctime((time_t *)&now.tv_sec));
170 } else if (tracepackets == 0) {
171 tracepackets++;
172 tracehistory = 0;
173 tracecontents = 0;
174 if (ftrace)
175 fprintf(ftrace, "Tracing packets started %s\n",
176 ctime((time_t *)&now.tv_sec));
177 } else if (tracehistory == 0) {
178 tracehistory++;
179 if (ftrace)
180 fprintf(ftrace, "Tracing history started %s\n",
181 ctime((time_t *)&now.tv_sec));
182 } else {
183 tracepackets++;
184 tracecontents++;
185 tracehistory = 0;
186 if (ftrace)
187 fprintf(ftrace, "Tracing packet contents started %s\n",
188 ctime((time_t *)&now.tv_sec));
189 }
190 if (ftrace)
191 fflush(ftrace);
192 }
193
194 void
195 trace(ifd, who, p, len, m)
196 register struct ifdebug *ifd;
197 struct sockaddr *who;
198 char *p;
199 int len, m;
200 {
201 register struct iftrace *t;
202
203 if (ifd->ifd_records == 0)
204 return;
205 t = ifd->ifd_front++;
206 if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
207 ifd->ifd_front = ifd->ifd_records;
208 if (ifd->ifd_count < NRECORDS)
209 ifd->ifd_count++;
210 if (t->ift_size > 0 && t->ift_size < len && t->ift_packet) {
211 free(t->ift_packet);
212 t->ift_packet = 0;
213 }
214 t->ift_stamp = now;
215 t->ift_who = *who;
216 if (len > 0 && t->ift_packet == 0) {
217 t->ift_packet = malloc(len);
218 if (t->ift_packet == 0)
219 len = 0;
220 }
221 if (len > 0)
222 memcpy(t->ift_packet, p, len);
223 t->ift_size = len;
224 t->ift_metric = m;
225 }
226
227 void
228 traceaction(fd, action, rt)
229 FILE *fd;
230 char *action;
231 struct rt_entry *rt;
232 {
233 struct sockaddr_in *dst, *gate;
234 static struct bits {
235 int t_bits;
236 char *t_name;
237 } flagbits[] = {
238 { RTF_UP, "UP" },
239 { RTF_GATEWAY, "GATEWAY" },
240 { RTF_HOST, "HOST" },
241 { 0 }
242 }, statebits[] = {
243 { RTS_PASSIVE, "PASSIVE" },
244 { RTS_REMOTE, "REMOTE" },
245 { RTS_INTERFACE,"INTERFACE" },
246 { RTS_CHANGED, "CHANGED" },
247 { RTS_INTERNAL, "INTERNAL" },
248 { RTS_EXTERNAL, "EXTERNAL" },
249 { RTS_SUBNET, "SUBNET" },
250 { 0 }
251 };
252 register struct bits *p;
253 register int first;
254 char *cp;
255
256 if (fd == NULL)
257 return;
258 if (lastlog.tv_sec != now.tv_sec || lastlog.tv_usec != now.tv_usec) {
259 fprintf(fd, "\n%.19s:\n", ctime((time_t *)&now.tv_sec));
260 lastlog = now;
261 }
262 fprintf(fd, "%s ", action);
263 dst = (struct sockaddr_in *)&rt->rt_dst;
264 gate = (struct sockaddr_in *)&rt->rt_router;
265 fprintf(fd, "dst %s, ", inet_ntoa(dst->sin_addr));
266 fprintf(fd, "router %s, metric %d, flags",
267 inet_ntoa(gate->sin_addr), rt->rt_metric);
268 cp = " %s";
269 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
270 if ((rt->rt_flags & p->t_bits) == 0)
271 continue;
272 fprintf(fd, cp, p->t_name);
273 if (first) {
274 cp = "|%s";
275 first = 0;
276 }
277 }
278 fprintf(fd, " state");
279 cp = " %s";
280 for (first = 1, p = statebits; p->t_bits > 0; p++) {
281 if ((rt->rt_state & p->t_bits) == 0)
282 continue;
283 fprintf(fd, cp, p->t_name);
284 if (first) {
285 cp = "|%s";
286 first = 0;
287 }
288 }
289 fprintf(fd, " timer %d\n", rt->rt_timer);
290 if (tracehistory && !tracepackets &&
291 (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
292 dumpif(fd, rt->rt_ifp);
293 fflush(fd);
294 if (ferror(fd))
295 traceoff();
296 }
297
298 void
299 tracenewmetric(fd, rt, newmetric)
300 FILE *fd;
301 struct rt_entry *rt;
302 int newmetric;
303 {
304 struct sockaddr_in *dst, *gate;
305
306 if (fd == NULL)
307 return;
308 if (lastlog.tv_sec != now.tv_sec || lastlog.tv_usec != now.tv_usec) {
309 fprintf(fd, "\n%.19s:\n", ctime((time_t *)&now.tv_sec));
310 lastlog = now;
311 }
312 dst = (struct sockaddr_in *)&rt->rt_dst;
313 gate = (struct sockaddr_in *)&rt->rt_router;
314 fprintf(fd, "CHANGE metric dst %s, ", inet_ntoa(dst->sin_addr));
315 fprintf(fd, "router %s, from %d to %d\n",
316 inet_ntoa(gate->sin_addr), rt->rt_metric, newmetric);
317 fflush(fd);
318 if (ferror(fd))
319 traceoff();
320 }
321
322 void
323 dumpif(fd, ifp)
324 FILE *fd;
325 register struct interface *ifp;
326 {
327 if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
328 fprintf(fd, "*** Packet history for interface %s ***\n",
329 ifp->int_name);
330 #ifdef notneeded
331 dumptrace(fd, "to", &ifp->int_output);
332 #endif
333 dumptrace(fd, "from", &ifp->int_input);
334 fprintf(fd, "*** end packet history ***\n");
335 }
336 }
337
338 void
339 dumptrace(fd, dir, ifd)
340 FILE *fd;
341 char *dir;
342 register struct ifdebug *ifd;
343 {
344 register struct iftrace *t;
345 char *cp = !strcmp(dir, "to") ? "Output" : "Input";
346
347 if (ifd->ifd_front == ifd->ifd_records &&
348 ifd->ifd_front->ift_size == 0) {
349 fprintf(fd, "%s: no packets.\n", cp);
350 fflush(fd);
351 return;
352 }
353 fprintf(fd, "%s trace:\n", cp);
354 t = ifd->ifd_front - ifd->ifd_count;
355 if (t < ifd->ifd_records)
356 t += NRECORDS;
357 for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
358 if (t >= ifd->ifd_records + NRECORDS)
359 t = ifd->ifd_records;
360 if (t->ift_size == 0)
361 continue;
362 dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size,
363 &t->ift_stamp);
364 }
365 }
366
367 void
368 dumppacket(fd, dir, who, cp, size, stamp)
369 FILE *fd;
370 struct sockaddr_in *who; /* should be sockaddr */
371 char *dir, *cp;
372 register int size;
373 struct timeval *stamp;
374 {
375 register struct rip *msg = (struct rip *)cp;
376 register struct netinfo *n;
377
378 if (fd == NULL)
379 return;
380 if (msg->rip_cmd && msg->rip_cmd < RIPCMD_MAX)
381 fprintf(fd, "%s %s %s.%d %.19s:\n", ripcmds[msg->rip_cmd],
382 dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port),
383 ctime((time_t *)&stamp->tv_sec));
384 else {
385 fprintf(fd, "Bad cmd 0x%x %s %x.%d %.19s\n", msg->rip_cmd,
386 dir, inet_ntoa(who->sin_addr), ntohs(who->sin_port),
387 ctime((time_t *)&stamp->tv_sec));
388 fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet);
389 fflush(fd);
390 return;
391 }
392 if (tracepackets && tracecontents == 0) {
393 fflush(fd);
394 return;
395 }
396 switch (msg->rip_cmd) {
397
398 case RIPCMD_REQUEST:
399 case RIPCMD_RESPONSE:
400 size -= 4 * sizeof (char);
401 n = msg->rip_nets;
402 for (; size > 0; n++, size -= sizeof (struct netinfo)) {
403 if (size < sizeof (struct netinfo)) {
404 fprintf(fd, "(truncated record, len %d)\n",
405 size);
406 break;
407 }
408 if (sizeof(n->rip_dst.sa_family) > 1)
409 n->rip_dst.sa_family = ntohs(n->rip_dst.sa_family);
410
411 switch ((int)n->rip_dst.sa_family) {
412
413 case AF_INET:
414 fprintf(fd, "\tdst %s metric %d\n",
415 #define satosin(sa) ((struct sockaddr_in *)&sa)
416 inet_ntoa(satosin(n->rip_dst)->sin_addr),
417 ntohl(n->rip_metric));
418 break;
419
420 default:
421 fprintf(fd, "\taf %d? metric %d\n",
422 n->rip_dst.sa_family,
423 ntohl(n->rip_metric));
424 break;
425 }
426 }
427 break;
428
429 case RIPCMD_TRACEON:
430 fprintf(fd, "\tfile=%*s\n", size, msg->rip_tracefile);
431 break;
432
433 case RIPCMD_TRACEOFF:
434 break;
435 }
436 fflush(fd);
437 if (ferror(fd))
438 traceoff();
439 }
440