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