hammer2 - Shift inode xop functions into hammer2_xop.c
[dragonfly.git] / sbin / routed / trace.c
blob757cd3dc44af807e01beea2c915546dae17c941f
1 /*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)trace.c 8.1 (Berkeley) 6/5/93
30 * $FreeBSD: src/sbin/routed/trace.c,v 1.5.2.1 2002/11/07 17:19:13 imp Exp $
33 #define RIPCMDS
34 #include "defs.h"
35 #include "pathnames.h"
36 #include <sys/stat.h>
37 #include <sys/signal.h>
38 #include <fcntl.h>
40 #define NRECORDS 50 /* size of circular trace buffer */
42 int tracelevel, new_tracelevel;
43 FILE *ftrace; /* output trace file */
44 static const char *sigtrace_pat = "%s";
45 static char savetracename[MAXPATHLEN+1];
46 char inittracename[MAXPATHLEN+1];
47 int file_trace; /* 1=tracing to file, not stdout */
49 static void trace_dump(void);
50 static void tmsg(const char *, ...) __printflike(1, 2);
53 /* convert string to printable characters
55 static char *
56 qstring(u_char *s, int len)
58 static char buf[8*20+1];
59 char *p;
60 u_char *s2, c;
63 for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
64 c = *s++;
65 if (c == '\0') {
66 for (s2 = s+1; s2 < &s[len]; s2++) {
67 if (*s2 != '\0')
68 break;
70 if (s2 >= &s[len])
71 goto exit;
74 if (c >= ' ' && c < 0x7f && c != '\\') {
75 *p++ = c;
76 continue;
78 *p++ = '\\';
79 switch (c) {
80 case '\\':
81 *p++ = '\\';
82 break;
83 case '\n':
84 *p++= 'n';
85 break;
86 case '\r':
87 *p++= 'r';
88 break;
89 case '\t':
90 *p++ = 't';
91 break;
92 case '\b':
93 *p++ = 'b';
94 break;
95 default:
96 p += sprintf(p,"%o",c);
97 break;
100 exit:
101 *p = '\0';
102 return buf;
106 /* convert IP address to a string, but not into a single buffer
108 char *
109 naddr_ntoa(naddr a)
111 #define NUM_BUFS 4
112 static int bufno;
113 static struct {
114 char str[16]; /* xxx.xxx.xxx.xxx\0 */
115 } bufs[NUM_BUFS];
116 char *s;
117 struct in_addr addr;
119 addr.s_addr = a;
120 s = strcpy(bufs[bufno].str, inet_ntoa(addr));
121 bufno = (bufno+1) % NUM_BUFS;
122 return s;
123 #undef NUM_BUFS
127 const char *
128 saddr_ntoa(struct sockaddr *sa)
130 return (sa == NULL) ? "?" : naddr_ntoa(S_ADDR(sa));
134 static char *
135 ts(time_t secs) {
136 static char s[20];
138 secs += epoch.tv_sec;
139 memcpy(s, ctime(&secs)+11, 8);
140 s[8] = '\0';
141 return s;
145 /* On each event, display a time stamp.
146 * This assumes that 'now' is update once for each event, and
147 * that at least now.tv_usec changes.
149 static struct timeval lastlog_time;
151 void
152 lastlog(void)
154 if (lastlog_time.tv_sec != now.tv_sec
155 || lastlog_time.tv_usec != now.tv_usec) {
156 fprintf(ftrace, "-- %s --\n", ts(now.tv_sec));
157 lastlog_time = now;
162 static void
163 tmsg(const char *p, ...)
165 va_list args;
167 if (ftrace != NULL) {
168 lastlog();
169 va_start(args, p);
170 vfprintf(ftrace, p, args);
171 fputc('\n',ftrace);
172 fflush(ftrace);
177 void
178 trace_close(int zap_stdio)
180 int fd;
183 fflush(stdout);
184 fflush(stderr);
186 if (ftrace != NULL && zap_stdio) {
187 if (ftrace != stdout)
188 fclose(ftrace);
189 ftrace = NULL;
190 fd = open(_PATH_DEVNULL, O_RDWR);
191 if (isatty(STDIN_FILENO))
192 dup2(fd, STDIN_FILENO);
193 if (isatty(STDOUT_FILENO))
194 dup2(fd, STDOUT_FILENO);
195 if (isatty(STDERR_FILENO))
196 dup2(fd, STDERR_FILENO);
197 close(fd);
199 lastlog_time.tv_sec = 0;
203 void
204 trace_flush(void)
206 if (ftrace != NULL) {
207 fflush(ftrace);
208 if (ferror(ftrace))
209 trace_off("tracing off: %s", strerror(ferror(ftrace)));
214 void
215 trace_off(const char *p, ...)
217 va_list args;
220 if (ftrace != NULL) {
221 lastlog();
222 va_start(args, p);
223 vfprintf(ftrace, p, args);
224 fputc('\n',ftrace);
226 trace_close(file_trace);
228 new_tracelevel = tracelevel = 0;
232 /* log a change in tracing
234 void
235 tracelevel_msg(const char *pat,
236 int dump) /* -1=no dump, 0=default, 1=force */
238 static const char *off_msgs[MAX_TRACELEVEL] = {
239 "Tracing actions stopped",
240 "Tracing packets stopped",
241 "Tracing packet contents stopped",
242 "Tracing kernel changes stopped",
244 static const char *on_msgs[MAX_TRACELEVEL] = {
245 "Tracing actions started",
246 "Tracing packets started",
247 "Tracing packet contents started",
248 "Tracing kernel changes started",
250 u_int old_tracelevel = tracelevel;
253 if (new_tracelevel < 0)
254 new_tracelevel = 0;
255 else if (new_tracelevel > MAX_TRACELEVEL)
256 new_tracelevel = MAX_TRACELEVEL;
258 if (new_tracelevel < tracelevel) {
259 if (new_tracelevel <= 0) {
260 trace_off(pat, off_msgs[0]);
261 } else do {
262 tmsg(pat, off_msgs[tracelevel]);
264 while (--tracelevel != new_tracelevel);
266 } else if (new_tracelevel > tracelevel) {
267 do {
268 tmsg(pat, on_msgs[tracelevel++]);
269 } while (tracelevel != new_tracelevel);
272 if (dump > 0
273 || (dump == 0 && old_tracelevel == 0 && tracelevel != 0))
274 trace_dump();
278 void
279 set_tracefile(const char *filename,
280 const char *pat,
281 int dump) /* -1=no dump, 0=default, 1=force */
283 struct stat stbuf;
284 FILE *n_ftrace;
285 const char *fn;
288 /* Allow a null filename to increase the level if the trace file
289 * is already open or if coming from a trusted source, such as
290 * a signal or the command line.
292 if (filename == NULL || filename[0] == '\0') {
293 filename = NULL;
294 if (ftrace == NULL) {
295 if (inittracename[0] == '\0') {
296 msglog("missing trace file name");
297 return;
299 fn = inittracename;
300 } else {
301 fn = NULL;
304 } else if (!strcmp(filename,"dump/../table")) {
305 trace_dump();
306 return;
308 } else {
309 /* Allow the file specified with "-T file" to be reopened,
310 * but require all other names specified over the net to
311 * match the official path. The path can specify a directory
312 * in which the file is to be created.
314 if (strcmp(filename, inittracename)
315 #ifdef _PATH_TRACE
316 && (strncmp(filename, _PATH_TRACE, sizeof(_PATH_TRACE)-1)
317 || strstr(filename,"../")
318 || 0 > stat(_PATH_TRACE, &stbuf))
319 #endif
321 msglog("wrong trace file \"%s\"", filename);
322 return;
325 /* If the new tracefile exists, it must be a regular file.
327 if (stat(filename, &stbuf) >= 0 && !S_ISREG(stbuf.st_mode)) {
328 msglog("wrong type (%#x) of trace file \"%s\"",
329 stbuf.st_mode, filename);
330 return;
333 fn = filename;
336 if (fn != NULL) {
337 n_ftrace = fopen(fn, "a");
338 if (n_ftrace == NULL) {
339 msglog("failed to open trace file \"%s\" %s",
340 fn, strerror(errno));
341 if (fn == inittracename)
342 inittracename[0] = '\0';
343 return;
346 tmsg("switch to trace file %s", fn);
348 trace_close(file_trace = 1);
350 if (fn != savetracename)
351 strncpy(savetracename, fn, sizeof(savetracename)-1);
352 ftrace = n_ftrace;
354 fflush(stdout);
355 fflush(stderr);
356 dup2(fileno(ftrace), STDOUT_FILENO);
357 dup2(fileno(ftrace), STDERR_FILENO);
360 if (new_tracelevel == 0 || filename == NULL)
361 new_tracelevel++;
362 tracelevel_msg(pat, dump != 0 ? dump : (filename != NULL));
366 /* ARGSUSED */
367 void
368 sigtrace_on(__unused int s)
370 new_tracelevel++;
371 sigtrace_pat = "SIGUSR1: %s";
375 /* ARGSUSED */
376 void
377 sigtrace_off(__unused int s)
379 new_tracelevel--;
380 sigtrace_pat = "SIGUSR2: %s";
384 /* Set tracing after a signal.
386 void
387 set_tracelevel(void)
389 if (new_tracelevel == tracelevel)
390 return;
392 /* If tracing entirely off, and there was no tracefile specified
393 * on the command line, then leave it off.
395 if (new_tracelevel > tracelevel && ftrace == NULL) {
396 if (savetracename[0] != '\0') {
397 set_tracefile(savetracename,sigtrace_pat,0);
398 } else if (inittracename[0] != '\0') {
399 set_tracefile(inittracename,sigtrace_pat,0);
400 } else {
401 new_tracelevel = 0;
402 return;
404 } else {
405 tracelevel_msg(sigtrace_pat, 0);
410 /* display an address
412 char *
413 addrname(naddr addr, /* in network byte order */
414 naddr mask,
415 int force) /* 0=show mask if nonstandard, */
416 { /* 1=always show mask, 2=never */
417 #define NUM_BUFS 4
418 static int bufno;
419 static struct {
420 char str[15+20];
421 } bufs[NUM_BUFS];
422 char *s, *sp;
423 naddr dmask;
424 int i;
426 s = strcpy(bufs[bufno].str, naddr_ntoa(addr));
427 bufno = (bufno+1) % NUM_BUFS;
429 if (force == 1 || (force == 0 && mask != std_mask(addr))) {
430 sp = &s[strlen(s)];
432 dmask = mask & -mask;
433 if (mask + dmask == 0) {
434 for (i = 0; i != 32 && ((1<<i) & mask) == 0; i++)
435 continue;
436 sprintf(sp, "/%d", 32-i);
438 } else {
439 sprintf(sp, " (mask %#x)", (u_int)mask);
443 return s;
444 #undef NUM_BUFS
448 /* display a bit-field
450 struct bits {
451 u_int bits_mask;
452 u_int bits_clear;
453 const char *bits_name;
456 static struct bits if_bits[] = {
457 { IFF_LOOPBACK, 0, "LOOPBACK" },
458 { IFF_POINTOPOINT, 0, "PT-TO-PT" },
459 { 0, 0, 0}
462 static struct bits is_bits[] = {
463 { IS_ALIAS, 0, "ALIAS" },
464 { IS_SUBNET, 0, "" },
465 { IS_REMOTE, (IS_NO_RDISC
466 | IS_BCAST_RDISC), "REMOTE" },
467 { IS_PASSIVE, (IS_NO_RDISC
468 | IS_NO_RIP
469 | IS_NO_SUPER_AG
470 | IS_PM_RDISC
471 | IS_NO_AG), "PASSIVE" },
472 { IS_EXTERNAL, 0, "EXTERNAL" },
473 { IS_CHECKED, 0, "" },
474 { IS_ALL_HOSTS, 0, "" },
475 { IS_ALL_ROUTERS, 0, "" },
476 { IS_DISTRUST, 0, "DISTRUST" },
477 { IS_BROKE, IS_SICK, "BROKEN" },
478 { IS_SICK, 0, "SICK" },
479 { IS_DUP, 0, "DUPLICATE" },
480 { IS_REDIRECT_OK, 0, "REDIRECT_OK" },
481 { IS_NEED_NET_SYN, 0, "" },
482 { IS_NO_AG, IS_NO_SUPER_AG, "NO_AG" },
483 { IS_NO_SUPER_AG, 0, "NO_SUPER_AG" },
484 { (IS_NO_RIPV1_IN
485 | IS_NO_RIPV2_IN
486 | IS_NO_RIPV1_OUT
487 | IS_NO_RIPV2_OUT), 0, "NO_RIP" },
488 { (IS_NO_RIPV1_IN
489 | IS_NO_RIPV1_OUT), 0, "RIPV2" },
490 { IS_NO_RIPV1_IN, 0, "NO_RIPV1_IN" },
491 { IS_NO_RIPV2_IN, 0, "NO_RIPV2_IN" },
492 { IS_NO_RIPV1_OUT, 0, "NO_RIPV1_OUT" },
493 { IS_NO_RIPV2_OUT, 0, "NO_RIPV2_OUT" },
494 { (IS_NO_ADV_IN
495 | IS_NO_SOL_OUT
496 | IS_NO_ADV_OUT), IS_BCAST_RDISC, "NO_RDISC" },
497 { IS_NO_SOL_OUT, 0, "NO_SOLICIT" },
498 { IS_SOL_OUT, 0, "SEND_SOLICIT" },
499 { IS_NO_ADV_OUT, IS_BCAST_RDISC, "NO_RDISC_ADV" },
500 { IS_ADV_OUT, 0, "RDISC_ADV" },
501 { IS_BCAST_RDISC, 0, "BCAST_RDISC" },
502 { IS_PM_RDISC, 0, "" },
503 { 0, 0, "%#x"}
506 static struct bits rs_bits[] = {
507 { RS_IF, 0, "IF" },
508 { RS_NET_INT, RS_NET_SYN, "NET_INT" },
509 { RS_NET_SYN, 0, "NET_SYN" },
510 { RS_SUBNET, 0, "" },
511 { RS_LOCAL, 0, "LOCAL" },
512 { RS_MHOME, 0, "MHOME" },
513 { RS_STATIC, 0, "STATIC" },
514 { RS_RDISC, 0, "RDISC" },
515 { 0, 0, "%#x"}
519 static void
520 trace_bits(const struct bits *tbl,
521 u_int field,
522 int force)
524 u_int b;
525 char c;
527 if (force) {
528 putc('<', ftrace);
529 c = 0;
530 } else {
531 c = '<';
534 while (field != 0
535 && (b = tbl->bits_mask) != 0) {
536 if ((b & field) == b) {
537 if (tbl->bits_name[0] != '\0') {
538 if (c)
539 putc(c, ftrace);
540 fprintf(ftrace, "%s", tbl->bits_name);
541 c = '|';
543 if (0 == (field &= ~(b | tbl->bits_clear)))
544 break;
546 tbl++;
548 if (field != 0 && tbl->bits_name != NULL) {
549 if (c)
550 putc(c, ftrace);
551 fprintf(ftrace, tbl->bits_name, field);
552 c = '|';
555 if (c != '<' || force)
556 fputs("> ", ftrace);
560 char *
561 rtname(naddr dst,
562 naddr mask,
563 naddr gate)
565 static char buf[3*4+3+1+2+3 /* "xxx.xxx.xxx.xxx/xx-->" */
566 +3*4+3+1]; /* "xxx.xxx.xxx.xxx" */
567 int i;
569 i = sprintf(buf, "%-16s-->", addrname(dst, mask, 0));
570 sprintf(&buf[i], "%-*s", 15+20-MAX(20,i), naddr_ntoa(gate));
571 return buf;
575 static void
576 print_rts(struct rt_spare *rts,
577 int force_metric, /* -1=suppress, 0=default */
578 int force_ifp, /* -1=suppress, 0=default */
579 int force_router, /* -1=suppress, 0=default, 1=display */
580 int force_tag, /* -1=suppress, 0=default, 1=display */
581 int force_time) /* 0=suppress, 1=display */
583 int i;
586 if (force_metric >= 0)
587 fprintf(ftrace, "metric=%-2d ", rts->rts_metric);
588 if (force_ifp >= 0)
589 fprintf(ftrace, "%s ", (rts->rts_ifp == 0 ?
590 "if?" : rts->rts_ifp->int_name));
591 if (force_router > 0
592 || (force_router == 0 && rts->rts_router != rts->rts_gate))
593 fprintf(ftrace, "router=%s ", naddr_ntoa(rts->rts_router));
594 if (force_time > 0)
595 fprintf(ftrace, "%s ", ts(rts->rts_time));
596 if (force_tag > 0
597 || (force_tag == 0 && rts->rts_tag != 0))
598 fprintf(ftrace, "tag=%#x ", ntohs(rts->rts_tag));
599 if (rts->rts_de_ag != 0) {
600 for (i = 1; (u_int)(1 << i) <= rts->rts_de_ag; i++)
601 continue;
602 fprintf(ftrace, "de_ag=%d ", i);
608 void
609 trace_if(const char *act,
610 struct interface *ifp)
612 if (!TRACEACTIONS || ftrace == NULL)
613 return;
615 lastlog();
616 fprintf(ftrace, "%-3s interface %-4s ", act, ifp->int_name);
617 fprintf(ftrace, "%-15s-->%-15s ",
618 naddr_ntoa(ifp->int_addr),
619 addrname(((ifp->int_if_flags & IFF_POINTOPOINT)
620 ? ifp->int_dstaddr : htonl(ifp->int_net)),
621 ifp->int_mask, 1));
622 if (ifp->int_metric != 0)
623 fprintf(ftrace, "metric=%d ", ifp->int_metric);
624 if (!IS_RIP_OUT_OFF(ifp->int_state)
625 && ifp->int_d_metric != 0)
626 fprintf(ftrace, "fake_default=%d ", ifp->int_d_metric);
627 trace_bits(if_bits, ifp->int_if_flags, 0);
628 trace_bits(is_bits, ifp->int_state, 0);
629 fputc('\n',ftrace);
633 void
634 trace_upslot(struct rt_entry *rt,
635 struct rt_spare *rts,
636 struct rt_spare *new)
638 if (!TRACEACTIONS || ftrace == NULL)
639 return;
641 if (rts->rts_gate == new->rts_gate
642 && rts->rts_router == new->rts_router
643 && rts->rts_metric == new->rts_metric
644 && rts->rts_tag == new->rts_tag
645 && rts->rts_de_ag == new->rts_de_ag)
646 return;
648 lastlog();
649 if (new->rts_gate == 0) {
650 fprintf(ftrace, "Del #%d %-35s ",
651 (int)(rts - rt->rt_spares),
652 rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
653 print_rts(rts, 0,0,0,0,
654 (rts != rt->rt_spares
655 || AGE_RT(rt->rt_state,new->rts_ifp)));
657 } else if (rts->rts_gate != RIP_DEFAULT) {
658 fprintf(ftrace, "Chg #%d %-35s ",
659 (int)(rts - rt->rt_spares),
660 rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
661 print_rts(rts, 0,0,
662 rts->rts_gate != new->rts_gate,
663 rts->rts_tag != new->rts_tag,
664 rts != rt->rt_spares || AGE_RT(rt->rt_state,
665 rt->rt_ifp));
667 fprintf(ftrace, "\n %19s%-16s ", "",
668 (new->rts_gate != rts->rts_gate
669 ? naddr_ntoa(new->rts_gate) : ""));
670 print_rts(new,
671 -(new->rts_metric == rts->rts_metric),
672 -(new->rts_ifp == rts->rts_ifp),
674 rts->rts_tag != new->rts_tag,
675 (new->rts_time != rts->rts_time
676 && (rts != rt->rt_spares
677 || AGE_RT(rt->rt_state, new->rts_ifp))));
679 } else {
680 fprintf(ftrace, "Add #%d %-35s ",
681 (int)(rts - rt->rt_spares),
682 rtname(rt->rt_dst, rt->rt_mask, new->rts_gate));
683 print_rts(new, 0,0,0,0,
684 (rts != rt->rt_spares
685 || AGE_RT(rt->rt_state,new->rts_ifp)));
687 fputc('\n',ftrace);
691 /* miscellaneous message checked by the caller
693 void
694 trace_misc(const char *p, ...)
696 va_list args;
698 if (ftrace == NULL)
699 return;
701 lastlog();
702 va_start(args, p);
703 vfprintf(ftrace, p, args);
704 fputc('\n',ftrace);
708 /* display a message if tracing actions
710 void
711 trace_act(const char *p, ...)
713 va_list args;
715 if (!TRACEACTIONS || ftrace == NULL)
716 return;
718 lastlog();
719 va_start(args, p);
720 vfprintf(ftrace, p, args);
721 fputc('\n',ftrace);
725 /* display a message if tracing packets
727 void
728 trace_pkt(const char *p, ...)
730 va_list args;
732 if (!TRACEPACKETS || ftrace == NULL)
733 return;
735 lastlog();
736 va_start(args, p);
737 vfprintf(ftrace, p, args);
738 fputc('\n',ftrace);
742 void
743 trace_change(struct rt_entry *rt,
744 u_int state,
745 struct rt_spare *new,
746 const char *label)
748 if (ftrace == NULL)
749 return;
751 if (rt->rt_metric == new->rts_metric
752 && rt->rt_gate == new->rts_gate
753 && rt->rt_router == new->rts_router
754 && rt->rt_state == state
755 && rt->rt_tag == new->rts_tag
756 && rt->rt_de_ag == new->rts_de_ag)
757 return;
759 lastlog();
760 fprintf(ftrace, "%s %-35s ",
761 label, rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
762 print_rts(rt->rt_spares,
763 0,0,0,0, AGE_RT(rt->rt_state, rt->rt_ifp));
764 trace_bits(rs_bits, rt->rt_state, rt->rt_state != state);
766 fprintf(ftrace, "\n%*s %19s%-16s ",
767 (int)strlen(label), "", "",
768 (rt->rt_gate != new->rts_gate
769 ? naddr_ntoa(new->rts_gate) : ""));
770 print_rts(new,
771 -(new->rts_metric == rt->rt_metric),
772 -(new->rts_ifp == rt->rt_ifp),
774 rt->rt_tag != new->rts_tag,
775 (rt->rt_time != new->rts_time
776 && AGE_RT(rt->rt_state,new->rts_ifp)));
777 if (rt->rt_state != state)
778 trace_bits(rs_bits, state, 1);
779 fputc('\n',ftrace);
783 void
784 trace_add_del(const char * action, struct rt_entry *rt)
786 if (ftrace == NULL)
787 return;
789 lastlog();
790 fprintf(ftrace, "%s %-35s ",
791 action, rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
792 print_rts(rt->rt_spares, 0,0,0,0,AGE_RT(rt->rt_state,rt->rt_ifp));
793 trace_bits(rs_bits, rt->rt_state, 0);
794 fputc('\n',ftrace);
798 /* ARGSUSED */
799 static int
800 walk_trace(struct radix_node *rn, __unused struct walkarg *w)
802 #define RT ((struct rt_entry *)rn)
803 struct rt_spare *rts;
804 int i;
806 fprintf(ftrace, " %-35s ",
807 rtname(RT->rt_dst, RT->rt_mask, RT->rt_gate));
808 print_rts(&RT->rt_spares[0], 0,0,0,0, AGE_RT(RT->rt_state, RT->rt_ifp));
809 trace_bits(rs_bits, RT->rt_state, 0);
810 if (RT->rt_poison_time >= now_garbage
811 && RT->rt_poison_metric < RT->rt_metric)
812 fprintf(ftrace, "pm=%d@%s",
813 RT->rt_poison_metric, ts(RT->rt_poison_time));
815 rts = &RT->rt_spares[1];
816 for (i = 1; i < NUM_SPARES; i++, rts++) {
817 if (rts->rts_gate != RIP_DEFAULT) {
818 fprintf(ftrace,"\n #%d%15s%-16s ",
819 i, "", naddr_ntoa(rts->rts_gate));
820 print_rts(rts, 0,0,0,0,1);
823 fputc('\n',ftrace);
825 return 0;
829 static void
830 trace_dump(void)
832 struct interface *ifp;
834 if (ftrace == NULL)
835 return;
836 lastlog();
838 fputs("current daemon state:\n", ftrace);
839 for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next)
840 trace_if("", ifp);
841 rn_walktree(rhead, walk_trace, 0);
845 void
846 trace_rip(const char *dir1, const char *dir2,
847 struct sockaddr_in *who,
848 struct interface *ifp,
849 struct rip *msg,
850 int size) /* total size of message */
852 struct netinfo *n, *lim;
853 # define NA ((struct netauth*)n)
854 int i, seen_route;
856 if (!TRACEPACKETS || ftrace == NULL)
857 return;
859 lastlog();
860 if (msg->rip_cmd >= RIPCMD_MAX
861 || msg->rip_vers == 0) {
862 fprintf(ftrace, "%s bad RIPv%d cmd=%d %s"
863 " %s.%d size=%d\n",
864 dir1, msg->rip_vers, msg->rip_cmd, dir2,
865 naddr_ntoa(who->sin_addr.s_addr),
866 ntohs(who->sin_port),
867 size);
868 return;
871 fprintf(ftrace, "%s RIPv%d %s %s %s.%d%s%s\n",
872 dir1, msg->rip_vers, ripcmds[msg->rip_cmd], dir2,
873 naddr_ntoa(who->sin_addr.s_addr), ntohs(who->sin_port),
874 ifp ? " via " : "", ifp ? ifp->int_name : "");
875 if (!TRACECONTENTS)
876 return;
878 seen_route = 0;
879 switch (msg->rip_cmd) {
880 case RIPCMD_REQUEST:
881 case RIPCMD_RESPONSE:
882 n = msg->rip_nets;
883 lim = (struct netinfo *)((char*)msg + size);
884 for (; n < lim; n++) {
885 if (!seen_route
886 && n->n_family == RIP_AF_UNSPEC
887 && ntohl(n->n_metric) == HOPCNT_INFINITY
888 && msg->rip_cmd == RIPCMD_REQUEST
889 && (n+1 == lim
890 || (n+2 == lim
891 && (n+1)->n_family == RIP_AF_AUTH))) {
892 fputs("\tQUERY ", ftrace);
893 if (n->n_dst != 0)
894 fprintf(ftrace, "%s ",
895 naddr_ntoa(n->n_dst));
896 if (n->n_mask != 0)
897 fprintf(ftrace, "mask=%#x ",
898 (u_int)ntohl(n->n_mask));
899 if (n->n_nhop != 0)
900 fprintf(ftrace, "nhop=%s ",
901 naddr_ntoa(n->n_nhop));
902 if (n->n_tag != 0)
903 fprintf(ftrace, "tag=%#x ",
904 ntohs(n->n_tag));
905 fputc('\n',ftrace);
906 continue;
909 if (n->n_family == RIP_AF_AUTH) {
910 if (NA->a_type == RIP_AUTH_PW
911 && n == msg->rip_nets) {
912 fprintf(ftrace, "\tPassword"
913 " Authentication:"
914 " \"%s\"\n",
915 qstring(NA->au.au_pw,
916 RIP_AUTH_PW_LEN));
917 continue;
920 if (NA->a_type == RIP_AUTH_MD5
921 && n == msg->rip_nets) {
922 fprintf(ftrace,
923 "\tMD5 Auth"
924 " pkt_len=%d KeyID=%u"
925 " auth_len=%d"
926 " seqno=%#x"
927 " rsvd=%#x,%#x\n",
928 ntohs(NA->au.a_md5.md5_pkt_len),
929 NA->au.a_md5.md5_keyid,
930 NA->au.a_md5.md5_auth_len,
931 (int)ntohl(NA->au.a_md5.md5_seqno),
932 (int)ntohs(NA->au.a_md5.rsvd[0]),
933 (int)ntohs(NA->au.a_md5.rsvd[1]));
934 continue;
936 fprintf(ftrace,
937 "\tAuthentication type %d: ",
938 ntohs(NA->a_type));
939 for (i = 0;
940 i < (int)sizeof(NA->au.au_pw);
941 i++)
942 fprintf(ftrace, "%02x ",
943 NA->au.au_pw[i]);
944 fputc('\n',ftrace);
945 continue;
948 seen_route = 1;
949 if (n->n_family != RIP_AF_INET) {
950 fprintf(ftrace,
951 "\t(af %d) %-18s mask=%#x ",
952 ntohs(n->n_family),
953 naddr_ntoa(n->n_dst),
954 (u_int)ntohl(n->n_mask));
955 } else if (msg->rip_vers == RIPv1) {
956 fprintf(ftrace, "\t%-18s ",
957 addrname(n->n_dst, ntohl(n->n_mask),
958 n->n_mask==0 ? 2 : 1));
959 } else {
960 fprintf(ftrace, "\t%-18s ",
961 addrname(n->n_dst, ntohl(n->n_mask),
962 n->n_mask==0 ? 2 : 0));
964 fprintf(ftrace, "metric=%-2d ",
965 (u_int)ntohl(n->n_metric));
966 if (n->n_nhop != 0)
967 fprintf(ftrace, " nhop=%s ",
968 naddr_ntoa(n->n_nhop));
969 if (n->n_tag != 0)
970 fprintf(ftrace, "tag=%#x", ntohs(n->n_tag));
971 fputc('\n',ftrace);
973 if (size != (char *)n - (char *)msg)
974 fprintf(ftrace, "truncated record, len %d\n", size);
975 break;
977 case RIPCMD_TRACEON:
978 fprintf(ftrace, "\tfile=\"%.*s\"\n", size-4,
979 msg->rip_tracefile);
980 break;
982 case RIPCMD_TRACEOFF:
983 break;