trace.c revision 1.1.1.5 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 #if !defined(lint) && !defined(sgi) && !defined(__NetBSD__)
35 static char sccsid[] = "@(#)trace.c 8.1 (Berkeley) 6/5/93";
36 #elif defined(__NetBSD__)
37 static char rcsid[] = "$NetBSD: trace.c,v 1.1.1.5 1997/02/03 21:06:42 christos Exp $";
38 #endif
39 #ident "$Revision: 1.1.1.5 $"
40
41 #define RIPCMDS
42 #include "defs.h"
43 #include "pathnames.h"
44 #include <sys/stat.h>
45 #include <sys/signal.h>
46 #include <fcntl.h>
47
48
49 #ifdef sgi
50 /* use *stat64 for files on large filesystems */
51 #define stat stat64
52 #endif
53
54 #define NRECORDS 50 /* size of circular trace buffer */
55
56 int tracelevel, new_tracelevel;
57 FILE *ftrace = stdout; /* output trace file */
58 static char *sigtrace_pat = "%s";
59 static char savetracename[MAXPATHLEN+1];
60 char inittracename[MAXPATHLEN+1];
61 int file_trace; /* 1=tracing to file, not stdout */
62
63 static void trace_dump(void);
64
65
66 /* convert string to printable characters
67 */
68 static char *
69 qstring(u_char *s, int len)
70 {
71 static char buf[8*20+1];
72 char *p;
73 u_char *s2, c;
74
75
76 for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
77 c = *s++;
78 if (c == '\0') {
79 for (s2 = s+1; s2 < &s[len]; s2++) {
80 if (*s2 != '\0')
81 break;
82 }
83 if (s2 >= &s[len])
84 goto exit;
85 }
86
87 if (c >= ' ' && c < 0x7f && c != '\\') {
88 *p++ = c;
89 continue;
90 }
91 *p++ = '\\';
92 switch (c) {
93 case '\\':
94 *p++ = '\\';
95 break;
96 case '\n':
97 *p++= 'n';
98 break;
99 case '\r':
100 *p++= 'r';
101 break;
102 case '\t':
103 *p++ = 't';
104 break;
105 case '\b':
106 *p++ = 'b';
107 break;
108 default:
109 p += sprintf(p,"%o",c);
110 break;
111 }
112 }
113 exit:
114 *p = '\0';
115 return buf;
116 }
117
118
119 /* convert IP address to a string, but not into a single buffer
120 */
121 char *
122 naddr_ntoa(naddr a)
123 {
124 #define NUM_BUFS 4
125 static int bufno;
126 static struct {
127 char str[16]; /* xxx.xxx.xxx.xxx\0 */
128 } bufs[NUM_BUFS];
129 char *s;
130 struct in_addr addr;
131
132 addr.s_addr = a;
133 s = strcpy(bufs[bufno].str, inet_ntoa(addr));
134 bufno = (bufno+1) % NUM_BUFS;
135 return s;
136 #undef NUM_BUFS
137 }
138
139
140 char *
141 saddr_ntoa(struct sockaddr *sa)
142 {
143 return (sa == 0) ? "?" : naddr_ntoa(S_ADDR(sa));
144 }
145
146
147 static char *
148 ts(time_t secs) {
149 static char s[20];
150
151 secs += epoch.tv_sec;
152 #ifdef sgi
153 (void)cftime(s, "%T", &secs);
154 #else
155 bcopy(ctime(&secs)+11, s, 8);
156 s[8] = '\0';
157 #endif
158 return s;
159 }
160
161
162 /* On each event, display a time stamp.
163 * This assumes that 'now' is update once for each event, and
164 * that at least now.tv_usec changes.
165 */
166 static struct timeval lastlog_time;
167
168 void
169 lastlog(void)
170 {
171 if (lastlog_time.tv_sec != now.tv_sec
172 || lastlog_time.tv_usec != now.tv_usec) {
173 (void)fprintf(ftrace, "-- %s --\n", ts(now.tv_sec));
174 lastlog_time = now;
175 }
176 }
177
178
179 static void
180 tmsg(char *p, ...)
181 {
182 va_list args;
183
184 if (ftrace != 0) {
185 lastlog();
186 va_start(args, p);
187 vfprintf(ftrace, p, args);
188 (void)fputc('\n',ftrace);
189 fflush(ftrace);
190 }
191 }
192
193
194 static void
195 trace_close(void)
196 {
197 int fd;
198
199
200 fflush(stdout);
201 fflush(stderr);
202
203 if (ftrace != 0 && file_trace) {
204 if (ftrace != stdout)
205 fclose(ftrace);
206 ftrace = 0;
207 fd = open(_PATH_DEVNULL, O_RDWR);
208 (void)dup2(fd, STDIN_FILENO);
209 (void)dup2(fd, STDOUT_FILENO);
210 (void)dup2(fd, STDERR_FILENO);
211 (void)close(fd);
212 }
213 lastlog_time.tv_sec = 0;
214 }
215
216
217 void
218 trace_flush(void)
219 {
220 if (ftrace != 0) {
221 fflush(ftrace);
222 if (ferror(ftrace))
223 trace_off("tracing off: ", strerror(ferror(ftrace)));
224 }
225 }
226
227
228 void
229 trace_off(char *p, ...)
230 {
231 va_list args;
232
233
234 if (ftrace != 0) {
235 lastlog();
236 va_start(args, p);
237 vfprintf(ftrace, p, args);
238 (void)fputc('\n',ftrace);
239 }
240 trace_close();
241
242 new_tracelevel = tracelevel = 0;
243 }
244
245
246 /* log a change in tracing
247 */
248 void
249 tracelevel_msg(char *pat,
250 int dump) /* -1=no dump, 0=default, 1=force */
251 {
252 static char *off_msgs[MAX_TRACELEVEL] = {
253 "Tracing actions stopped",
254 "Tracing packets stopped",
255 "Tracing packet contents stopped",
256 "Tracing kernel changes stopped",
257 };
258 static char *on_msgs[MAX_TRACELEVEL] = {
259 "Tracing actions started",
260 "Tracing packets started",
261 "Tracing packet contents started",
262 "Tracing kernel changes started",
263 };
264 u_int old_tracelevel = tracelevel;
265
266
267 if (new_tracelevel < 0)
268 new_tracelevel = 0;
269 else if (new_tracelevel > MAX_TRACELEVEL)
270 new_tracelevel = MAX_TRACELEVEL;
271
272 if (new_tracelevel < tracelevel) {
273 if (new_tracelevel <= 0) {
274 trace_off(pat, off_msgs[0]);
275 } else do {
276 tmsg(pat, off_msgs[tracelevel]);
277 }
278 while (--tracelevel != new_tracelevel);
279
280 } else if (new_tracelevel > tracelevel) {
281 do {
282 tmsg(pat, on_msgs[tracelevel++]);
283 } while (tracelevel != new_tracelevel);
284 }
285
286 if (dump > 0
287 || (dump == 0 && old_tracelevel == 0 && tracelevel != 0))
288 trace_dump();
289 }
290
291
292 void
293 set_tracefile(char *filename,
294 char *pat,
295 int dump) /* -1=no dump, 0=default, 1=force */
296 {
297 struct stat stbuf;
298 FILE *n_ftrace;
299 char *fn;
300
301
302 /* Allow a null filename to increase the level if the trace file
303 * is already open or if coming from a trusted source, such as
304 * a signal or the command line.
305 */
306 if (filename == 0 || filename[0] == '\0') {
307 filename = 0;
308 if (ftrace == 0) {
309 if (inittracename[0] == '\0') {
310 msglog("missing trace file name");
311 return;
312 }
313 fn = inittracename;
314 } else {
315 fn = 0;
316 }
317
318 } else if (!strcmp(filename,"dump/../table")) {
319 trace_dump();
320 return;
321
322 } else {
323 /* Allow the file specified with "-T file" to be reopened,
324 * but require all other names specified over the net to
325 * match the official path. The path can specify a directory
326 * in which the file is to be created.
327 */
328 if (strcmp(filename, inittracename)
329 #ifdef _PATH_TRACE
330 && (strncmp(filename, _PATH_TRACE, sizeof(_PATH_TRACE)-1)
331 || strstr(filename,"../")
332 || 0 > stat(_PATH_TRACE, &stbuf))
333 #endif
334 ) {
335 msglog("wrong trace file \"%s\"", filename);
336 return;
337 }
338
339 /* If the new tracefile exists, it must be a regular file.
340 */
341 if (stat(filename, &stbuf) >= 0
342 && (stbuf.st_mode & S_IFMT) != S_IFREG) {
343 msglog("wrong type (%#x) of trace file \"%s\"",
344 stbuf.st_mode, filename);
345 return;
346 }
347
348 fn = filename;
349 }
350
351 if (fn != 0) {
352 n_ftrace = fopen(fn, "a");
353 if (n_ftrace == 0) {
354 msglog("failed to open trace file \"%s\" %s",
355 fn, strerror(errno));
356 if (fn == inittracename)
357 inittracename[0] = '\0';
358 return;
359 }
360
361 tmsg("switch to trace file %s", fn);
362
363 file_trace = 1;
364 trace_close();
365
366 if (fn != savetracename)
367 strncpy(savetracename, fn, sizeof(savetracename)-1);
368 ftrace = n_ftrace;
369
370 fflush(stdout);
371 fflush(stderr);
372 dup2(fileno(ftrace), STDOUT_FILENO);
373 dup2(fileno(ftrace), STDERR_FILENO);
374 }
375
376 if (new_tracelevel == 0 || filename == 0)
377 new_tracelevel++;
378 tracelevel_msg(pat, dump != 0 ? dump : (filename != 0));
379 }
380
381
382 /* ARGSUSED */
383 void
384 sigtrace_on(int s)
385 {
386 new_tracelevel++;
387 sigtrace_pat = "SIGUSR1: %s";
388 }
389
390
391 /* ARGSUSED */
392 void
393 sigtrace_off(int s)
394 {
395 new_tracelevel--;
396 sigtrace_pat = "SIGUSR2: %s";
397 }
398
399
400 /* Set tracing after a signal.
401 */
402 void
403 set_tracelevel(void)
404 {
405 if (new_tracelevel == tracelevel)
406 return;
407
408 /* If tracing entirely off, and there was no tracefile specified
409 * on the command line, then leave it off.
410 */
411 if (new_tracelevel > tracelevel && ftrace == 0) {
412 if (savetracename[0] != '\0') {
413 set_tracefile(savetracename,sigtrace_pat,0);
414 } else if (inittracename[0] != '\0') {
415 set_tracefile(inittracename,sigtrace_pat,0);
416 } else {
417 new_tracelevel = 0;
418 return;
419 }
420 } else {
421 tracelevel_msg(sigtrace_pat, 0);
422 }
423 }
424
425
426 /* display an address
427 */
428 char *
429 addrname(naddr addr, /* in network byte order */
430 naddr mask,
431 int force) /* 0=show mask if nonstandard, */
432 { /* 1=always show mask, 2=never */
433 #define NUM_BUFS 4
434 static int bufno;
435 static struct {
436 char str[15+20];
437 } bufs[NUM_BUFS];
438 char *s, *sp;
439 naddr dmask;
440 int i;
441
442 s = strcpy(bufs[bufno].str, naddr_ntoa(addr));
443 bufno = (bufno+1) % NUM_BUFS;
444
445 if (force == 1 || (force == 0 && mask != std_mask(addr))) {
446 sp = &s[strlen(s)];
447
448 dmask = mask & -mask;
449 if (mask + dmask == 0) {
450 for (i = 0; i != 32 && ((1<<i) & mask) == 0; i++)
451 continue;
452 (void)sprintf(sp, "/%d", 32-i);
453
454 } else {
455 (void)sprintf(sp, " (mask %#x)", (u_int)mask);
456 }
457 }
458
459 return s;
460 #undef NUM_BUFS
461 }
462
463
464 /* display a bit-field
465 */
466 struct bits {
467 int bits_mask;
468 int bits_clear;
469 char *bits_name;
470 };
471
472 static struct bits if_bits[] = {
473 { IFF_LOOPBACK, 0, "LOOPBACK" },
474 { IFF_POINTOPOINT, 0, "PT-TO-PT" },
475 { 0, 0, 0}
476 };
477
478 static struct bits is_bits[] = {
479 { IS_ALIAS, 0, "ALIAS" },
480 { IS_SUBNET, 0, "" },
481 { IS_REMOTE, (IS_NO_RDISC
482 | IS_BCAST_RDISC), "REMOTE" },
483 { IS_PASSIVE, (IS_NO_RDISC
484 | IS_NO_RIP
485 | IS_NO_SUPER_AG
486 | IS_PM_RDISC
487 | IS_NO_AG), "PASSIVE" },
488 { IS_EXTERNAL, 0, "EXTERNAL" },
489 { IS_CHECKED, 0, "" },
490 { IS_ALL_HOSTS, 0, "" },
491 { IS_ALL_ROUTERS, 0, "" },
492 { IS_DISTRUST, 0, "DISTRUST" },
493 { IS_BROKE, IS_SICK, "BROKEN" },
494 { IS_SICK, 0, "SICK" },
495 { IS_DUP, 0, "DUPLICATE" },
496 { IS_REDIRECT_OK, 0, "REDIRECT_OK" },
497 { IS_NEED_NET_SYN, 0, "" },
498 { IS_NO_AG, IS_NO_SUPER_AG, "NO_AG" },
499 { IS_NO_SUPER_AG, 0, "NO_SUPER_AG" },
500 { (IS_NO_RIPV1_IN
501 | IS_NO_RIPV2_IN
502 | IS_NO_RIPV1_OUT
503 | IS_NO_RIPV2_OUT), 0, "NO_RIP" },
504 { (IS_NO_RIPV1_IN
505 | IS_NO_RIPV1_OUT), 0, "RIPV2" },
506 { IS_NO_RIPV1_IN, 0, "NO_RIPV1_IN" },
507 { IS_NO_RIPV2_IN, 0, "NO_RIPV2_IN" },
508 { IS_NO_RIPV1_OUT, 0, "NO_RIPV1_OUT" },
509 { IS_NO_RIPV2_OUT, 0, "NO_RIPV2_OUT" },
510 { (IS_NO_ADV_IN
511 | IS_NO_SOL_OUT
512 | IS_NO_ADV_OUT), IS_BCAST_RDISC, "NO_RDISC" },
513 { IS_NO_SOL_OUT, 0, "NO_SOLICIT" },
514 { IS_SOL_OUT, 0, "SEND_SOLICIT" },
515 { IS_NO_ADV_OUT, IS_BCAST_RDISC, "NO_RDISC_ADV" },
516 { IS_ADV_OUT, 0, "RDISC_ADV" },
517 { IS_BCAST_RDISC, 0, "BCAST_RDISC" },
518 { IS_PM_RDISC, 0, "" },
519 { 0, 0, "%#x"}
520 };
521
522 static struct bits rs_bits[] = {
523 { RS_IF, 0, "IF" },
524 { RS_NET_INT, RS_NET_SYN, "NET_INT" },
525 { RS_NET_SYN, 0, "NET_SYN" },
526 { RS_SUBNET, 0, "" },
527 { RS_LOCAL, 0, "LOCAL" },
528 { RS_MHOME, 0, "MHOME" },
529 { RS_STATIC, 0, "STATIC" },
530 { RS_RDISC, 0, "RDISC" },
531 { 0, 0, "%#x"}
532 };
533
534
535 static void
536 trace_bits(struct bits *tbl,
537 u_int field,
538 int force)
539 {
540 int b;
541 char c;
542
543 if (force) {
544 (void)putc('<', ftrace);
545 c = 0;
546 } else {
547 c = '<';
548 }
549
550 while (field != 0
551 && (b = tbl->bits_mask) != 0) {
552 if ((b & field) == b) {
553 if (tbl->bits_name[0] != '\0') {
554 if (c)
555 (void)putc(c, ftrace);
556 (void)fprintf(ftrace, "%s", tbl->bits_name);
557 c = '|';
558 }
559 if (0 == (field &= ~(b | tbl->bits_clear)))
560 break;
561 }
562 tbl++;
563 }
564 if (field != 0 && tbl->bits_name != 0) {
565 if (c)
566 (void)putc(c, ftrace);
567 (void)fprintf(ftrace, tbl->bits_name, field);
568 c = '|';
569 }
570
571 if (c != '<' || force)
572 (void)fputs("> ", ftrace);
573 }
574
575
576 char *
577 rtname(naddr dst,
578 naddr mask,
579 naddr gate)
580 {
581 static char buf[3*4+3+1+2+3 /* "xxx.xxx.xxx.xxx/xx-->" */
582 +3*4+3+1]; /* "xxx.xxx.xxx.xxx" */
583 int i;
584
585 i = sprintf(buf, "%-16s-->", addrname(dst, mask, 0));
586 (void)sprintf(&buf[i], "%-*s", 15+20-MAX(20,i), naddr_ntoa(gate));
587 return buf;
588 }
589
590
591 static void
592 print_rts(struct rt_spare *rts,
593 int force_metric, /* -1=suppress, 0=default */
594 int force_ifp, /* -1=suppress, 0=default */
595 int force_router, /* -1=suppress, 0=default, 1=display */
596 int force_tag, /* -1=suppress, 0=default, 1=display */
597 int force_time) /* 0=suppress, 1=display */
598 {
599 if (force_metric >= 0)
600 (void)fprintf(ftrace, "metric=%-2d ", rts->rts_metric);
601 if (force_ifp >= 0)
602 (void)fprintf(ftrace, "%s ", (rts->rts_ifp == 0 ?
603 "if?" : rts->rts_ifp->int_name));
604 if (force_router > 0
605 || (force_router == 0 && rts->rts_router != rts->rts_gate))
606 (void)fprintf(ftrace, "router=%s ",
607 naddr_ntoa(rts->rts_router));
608 if (force_time > 0)
609 (void)fprintf(ftrace, "%s ", ts(rts->rts_time));
610 if (force_tag > 0
611 || (force_tag == 0 && rts->rts_tag != 0))
612 (void)fprintf(ftrace, "tag=%#x ",
613 ntohs(rts->rts_tag));
614 }
615
616
617 void
618 trace_if(char *act,
619 struct interface *ifp)
620 {
621 if (!TRACEACTIONS || ftrace == 0)
622 return;
623
624 lastlog();
625 (void)fprintf(ftrace, "%-3s interface %-4s ", act, ifp->int_name);
626 (void)fprintf(ftrace, "%-15s-->%-15s ",
627 naddr_ntoa(ifp->int_addr),
628 addrname(((ifp->int_if_flags & IFF_POINTOPOINT)
629 ? ifp->int_dstaddr
630 : htonl(ifp->int_net)),
631 ifp->int_mask, 1));
632 if (ifp->int_metric != 0)
633 (void)fprintf(ftrace, "metric=%d ", ifp->int_metric);
634 if (!IS_RIP_OUT_OFF(ifp->int_state)
635 && ifp->int_d_metric != 0)
636 (void)fprintf(ftrace, "fake_default=%d ", ifp->int_d_metric);
637 trace_bits(if_bits, ifp->int_if_flags, 0);
638 trace_bits(is_bits, ifp->int_state, 0);
639 (void)fputc('\n',ftrace);
640 }
641
642
643 void
644 trace_upslot(struct rt_entry *rt,
645 struct rt_spare *rts,
646 naddr gate,
647 naddr router,
648 struct interface *ifp,
649 int metric,
650 u_short tag,
651 time_t new_time)
652 {
653 struct rt_spare new;
654
655 if (!TRACEACTIONS || ftrace == 0)
656 return;
657
658 if (rts->rts_gate == gate
659 && rts->rts_router == router
660 && rts->rts_metric == metric
661 && rts->rts_tag == tag)
662 return;
663 new.rts_ifp = ifp;
664 new.rts_gate = gate;
665 new.rts_router = router;
666 new.rts_metric = metric;
667 new.rts_time = new_time;
668 new.rts_tag = tag;
669
670 lastlog();
671 if (gate == 0) {
672 (void)fprintf(ftrace, "Del #%d %-35s ",
673 rts - rt->rt_spares,
674 rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
675 print_rts(&new, 0,0,0,0,
676 rts != rt->rt_spares || AGE_RT(rt->rt_state,ifp));
677
678 } else if (rts->rts_gate != RIP_DEFAULT) {
679 (void)fprintf(ftrace, "Chg #%d %-35s ",
680 rts - rt->rt_spares,
681 rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
682 print_rts(rts, 0,0,
683 rts->rts_gate != gate,
684 rts->rts_tag != tag,
685 rts != rt->rt_spares || AGE_RT(rt->rt_state,
686 rt->rt_ifp));
687
688 (void)fprintf(ftrace, "\n %19s%-16s ", "",
689 gate != rts->rts_gate ? naddr_ntoa(gate) : "");
690 print_rts(&new,
691 -(metric == rts->rts_metric),
692 -(ifp == rts->rts_ifp),
693 0,
694 rts->rts_tag != tag,
695 new_time != rts->rts_time && (rts != rt->rt_spares
696 || AGE_RT(rt->rt_state,
697 ifp)));
698
699 } else {
700 (void)fprintf(ftrace, "Add #%d %-35s ",
701 rts - rt->rt_spares,
702 rtname(rt->rt_dst, rt->rt_mask, gate));
703 print_rts(&new, 0,0,0,0,
704 rts != rt->rt_spares || AGE_RT(rt->rt_state,ifp));
705 }
706 (void)fputc('\n',ftrace);
707 }
708
709
710 /* talk about a change made to the kernel table
711 */
712 void
713 trace_kernel(char *p, ...)
714 {
715 va_list args;
716
717 if (!TRACEKERNEL || ftrace == 0)
718 return;
719
720 lastlog();
721 va_start(args, p);
722 vfprintf(ftrace, p, args);
723 (void)fputc('\n',ftrace);
724 }
725
726
727 /* display a message if tracing actions
728 */
729 void
730 trace_act(char *p, ...)
731 {
732 va_list args;
733
734 if (!TRACEACTIONS || ftrace == 0)
735 return;
736
737 lastlog();
738 va_start(args, p);
739 vfprintf(ftrace, p, args);
740 (void)fputc('\n',ftrace);
741 }
742
743
744 /* display a message if tracing packets
745 */
746 void
747 trace_pkt(char *p, ...)
748 {
749 va_list args;
750
751 if (!TRACEPACKETS || ftrace == 0)
752 return;
753
754 lastlog();
755 va_start(args, p);
756 vfprintf(ftrace, p, args);
757 (void)fputc('\n',ftrace);
758 }
759
760
761 void
762 trace_change(struct rt_entry *rt,
763 u_int state,
764 naddr gate, /* forward packets here */
765 naddr router, /* on the authority of this router */
766 int metric,
767 u_short tag,
768 struct interface *ifp,
769 time_t new_time,
770 char *label)
771 {
772 struct rt_spare new;
773
774 if (ftrace == 0)
775 return;
776
777 if (rt->rt_metric == metric
778 && rt->rt_gate == gate
779 && rt->rt_router == router
780 && rt->rt_state == state
781 && rt->rt_tag == tag)
782 return;
783 new.rts_ifp = ifp;
784 new.rts_gate = gate;
785 new.rts_router = router;
786 new.rts_metric = metric;
787 new.rts_time = new_time;
788 new.rts_tag = tag;
789
790 lastlog();
791 (void)fprintf(ftrace, "%s %-35s ",
792 label,
793 rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
794 print_rts(rt->rt_spares,
795 0,0,0,0, AGE_RT(rt->rt_state, rt->rt_ifp));
796 trace_bits(rs_bits, rt->rt_state, rt->rt_state != state);
797
798 (void)fprintf(ftrace, "\n%*s %19s%-16s ",
799 strlen(label), "", "",
800 rt->rt_gate != gate ? naddr_ntoa(gate) : "");
801 print_rts(&new,
802 -(metric == rt->rt_metric),
803 -(ifp == rt->rt_ifp),
804 0,
805 rt->rt_tag != tag,
806 rt->rt_time != new_time && AGE_RT(rt->rt_state,ifp));
807 if (rt->rt_state != state)
808 trace_bits(rs_bits, state, 1);
809 (void)fputc('\n',ftrace);
810 }
811
812
813 void
814 trace_add_del(char * action, struct rt_entry *rt)
815 {
816 if (ftrace == 0)
817 return;
818
819 lastlog();
820 (void)fprintf(ftrace, "%s %-35s ",
821 action,
822 rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
823 print_rts(rt->rt_spares, 0,0,0,0,AGE_RT(rt->rt_state,rt->rt_ifp));
824 trace_bits(rs_bits, rt->rt_state, 0);
825 (void)fputc('\n',ftrace);
826 }
827
828
829 /* ARGSUSED */
830 static int
831 walk_trace(struct radix_node *rn,
832 struct walkarg *w)
833 {
834 #define RT ((struct rt_entry *)rn)
835 struct rt_spare *rts;
836 int i, age = AGE_RT(RT->rt_state, RT->rt_ifp);
837
838 (void)fprintf(ftrace, " %-35s ",
839 rtname(RT->rt_dst, RT->rt_mask, RT->rt_gate));
840 print_rts(&RT->rt_spares[0], 0,0,0,0,age);
841 trace_bits(rs_bits, RT->rt_state, 0);
842 if (RT->rt_poison_time >= now_garbage
843 && RT->rt_poison_metric < RT->rt_metric)
844 (void)fprintf(ftrace, "pm=%d@%s",
845 RT->rt_poison_metric, ts(RT->rt_poison_time));
846
847 rts = &RT->rt_spares[1];
848 for (i = 1; i < NUM_SPARES; i++, rts++) {
849 if (rts->rts_gate != RIP_DEFAULT) {
850 (void)fprintf(ftrace,"\n #%d%15s%-16s ",
851 i, "", naddr_ntoa(rts->rts_gate));
852 print_rts(rts, 0,0,0,0,1);
853 }
854 }
855 (void)fputc('\n',ftrace);
856
857 return 0;
858 }
859
860
861 static void
862 trace_dump(void)
863 {
864 struct interface *ifp;
865
866 if (ftrace == 0)
867 return;
868 lastlog();
869
870 (void)fputs("current daemon state:\n", ftrace);
871 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next)
872 trace_if("", ifp);
873 (void)rn_walktree(rhead, walk_trace, 0);
874 }
875
876
877 void
878 trace_rip(char *dir1, char *dir2,
879 struct sockaddr_in *who,
880 struct interface *ifp,
881 struct rip *msg,
882 int size) /* total size of message */
883 {
884 struct netinfo *n, *lim;
885 # define NA (msg->rip_auths)
886 int i, seen_route;
887
888 if (!TRACEPACKETS || ftrace == 0)
889 return;
890
891 lastlog();
892 if (msg->rip_cmd >= RIPCMD_MAX
893 || msg->rip_vers == 0) {
894 (void)fprintf(ftrace, "%s bad RIPv%d cmd=%d %s"
895 " %s.%d size=%d\n",
896 dir1, msg->rip_vers, msg->rip_cmd, dir2,
897 naddr_ntoa(who->sin_addr.s_addr),
898 ntohs(who->sin_port),
899 size);
900 return;
901 }
902
903 (void)fprintf(ftrace, "%s RIPv%d %s %s %s.%d%s%s\n",
904 dir1, msg->rip_vers, ripcmds[msg->rip_cmd], dir2,
905 naddr_ntoa(who->sin_addr.s_addr), ntohs(who->sin_port),
906 ifp ? " via " : "", ifp ? ifp->int_name : "");
907 if (!TRACECONTENTS)
908 return;
909
910 seen_route = 0;
911 switch (msg->rip_cmd) {
912 case RIPCMD_REQUEST:
913 case RIPCMD_RESPONSE:
914 n = msg->rip_nets;
915 lim = (struct netinfo *)((char*)msg + size);
916 for (; n < lim; n++) {
917 if (!seen_route
918 && n->n_family == RIP_AF_UNSPEC
919 && ntohl(n->n_metric) == HOPCNT_INFINITY
920 && msg->rip_cmd == RIPCMD_REQUEST
921 && (n+1 == lim
922 || (n+2 == lim
923 && (n+1)->n_family == RIP_AF_AUTH))) {
924 (void)fputs("\tQUERY ", ftrace);
925 if (n->n_dst != 0)
926 (void)fprintf(ftrace, "%s ",
927 naddr_ntoa(n->n_dst));
928 if (n->n_mask != 0)
929 (void)fprintf(ftrace, "mask=%#x ",
930 (u_int)ntohl(n->n_mask));
931 if (n->n_nhop != 0)
932 (void)fprintf(ftrace, "nhop=%s ",
933 naddr_ntoa(n->n_nhop));
934 if (n->n_tag != 0)
935 (void)fprintf(ftrace, "tag=%#x ",
936 ntohs(n->n_tag));
937 (void)fputc('\n',ftrace);
938 continue;
939 }
940
941 if (n->n_family == RIP_AF_AUTH) {
942 if (NA->a_type == RIP_AUTH_PW
943 && n == msg->rip_nets) {
944 (void)fprintf(ftrace, "\tPassword"
945 " Authentication:"
946 " \"%s\"\n",
947 qstring(NA->au.au_pw,
948 RIP_AUTH_PW_LEN));
949 continue;
950 }
951
952 if (NA->a_type == RIP_AUTH_MD5
953 && n == msg->rip_nets) {
954 (void)fprintf(ftrace,
955 "\tMD5 Authentication"
956 " len=%d KeyID=%u"
957 " seqno=%u"
958 " rsvd=%#x,%#x\n",
959 NA->au.a_md5.md5_pkt_len,
960 NA->au.a_md5.md5_keyid,
961 NA->au.a_md5.md5_seqno,
962 NA->au.a_md5.rsvd[0],
963 NA->au.a_md5.rsvd[1]);
964 continue;
965 }
966 (void)fprintf(ftrace,
967 "\tAuthentication"
968 " type %d: ",
969 ntohs(NA->a_type));
970 for (i = 0;
971 i < sizeof(NA->au.au_pw);
972 i++)
973 (void)fprintf(ftrace, "%02x ",
974 NA->au.au_pw[i]);
975 (void)fputc('\n',ftrace);
976 continue;
977 }
978
979 seen_route = 1;
980 if (n->n_family != RIP_AF_INET) {
981 (void)fprintf(ftrace,
982 "\t(af %d) %-18s mask=%#x ",
983 ntohs(n->n_family),
984 naddr_ntoa(n->n_dst),
985 (u_int)ntohl(n->n_mask));
986 } else if (msg->rip_vers == RIPv1) {
987 (void)fprintf(ftrace, "\t%-18s ",
988 addrname(n->n_dst,
989 ntohl(n->n_mask),
990 n->n_mask==0 ? 2 : 1));
991 } else {
992 (void)fprintf(ftrace, "\t%-18s ",
993 addrname(n->n_dst,
994 ntohl(n->n_mask),
995 n->n_mask==0 ? 2 : 0));
996 }
997 (void)fprintf(ftrace, "metric=%-2d ",
998 (u_int)ntohl(n->n_metric));
999 if (n->n_nhop != 0)
1000 (void)fprintf(ftrace, " nhop=%s ",
1001 naddr_ntoa(n->n_nhop));
1002 if (n->n_tag != 0)
1003 (void)fprintf(ftrace, "tag=%#x",
1004 ntohs(n->n_tag));
1005 (void)fputc('\n',ftrace);
1006 }
1007 if (size != (char *)n - (char *)msg)
1008 (void)fprintf(ftrace, "truncated record, len %d\n",
1009 size);
1010 break;
1011
1012 case RIPCMD_TRACEON:
1013 fprintf(ftrace, "\tfile=\"%.*s\"\n", size-4,
1014 msg->rip_tracefile);
1015 break;
1016
1017 case RIPCMD_TRACEOFF:
1018 break;
1019 }
1020 }
1021