trace.c revision 1.1.1.6 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.6 1998/06/02 17:41:27 thorpej Exp $";
38 #endif
39 #ident "$Revision: 1.1.1.6 $"
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 void
195 trace_close(int zap_stdio)
196 {
197 int fd;
198
199
200 fflush(stdout);
201 fflush(stderr);
202
203 if (ftrace != 0 && zap_stdio) {
204 if (ftrace != stdout)
205 fclose(ftrace);
206 ftrace = 0;
207 fd = open(_PATH_DEVNULL, O_RDWR);
208 if (isatty(STDIN_FILENO))
209 (void)dup2(fd, STDIN_FILENO);
210 if (isatty(STDOUT_FILENO))
211 (void)dup2(fd, STDOUT_FILENO);
212 if (isatty(STDERR_FILENO))
213 (void)dup2(fd, STDERR_FILENO);
214 (void)close(fd);
215 }
216 lastlog_time.tv_sec = 0;
217 }
218
219
220 void
221 trace_flush(void)
222 {
223 if (ftrace != 0) {
224 fflush(ftrace);
225 if (ferror(ftrace))
226 trace_off("tracing off: ", strerror(ferror(ftrace)));
227 }
228 }
229
230
231 void
232 trace_off(char *p, ...)
233 {
234 va_list args;
235
236
237 if (ftrace != 0) {
238 lastlog();
239 va_start(args, p);
240 vfprintf(ftrace, p, args);
241 (void)fputc('\n',ftrace);
242 }
243 trace_close(file_trace);
244
245 new_tracelevel = tracelevel = 0;
246 }
247
248
249 /* log a change in tracing
250 */
251 void
252 tracelevel_msg(char *pat,
253 int dump) /* -1=no dump, 0=default, 1=force */
254 {
255 static char *off_msgs[MAX_TRACELEVEL] = {
256 "Tracing actions stopped",
257 "Tracing packets stopped",
258 "Tracing packet contents stopped",
259 "Tracing kernel changes stopped",
260 };
261 static char *on_msgs[MAX_TRACELEVEL] = {
262 "Tracing actions started",
263 "Tracing packets started",
264 "Tracing packet contents started",
265 "Tracing kernel changes started",
266 };
267 u_int old_tracelevel = tracelevel;
268
269
270 if (new_tracelevel < 0)
271 new_tracelevel = 0;
272 else if (new_tracelevel > MAX_TRACELEVEL)
273 new_tracelevel = MAX_TRACELEVEL;
274
275 if (new_tracelevel < tracelevel) {
276 if (new_tracelevel <= 0) {
277 trace_off(pat, off_msgs[0]);
278 } else do {
279 tmsg(pat, off_msgs[tracelevel]);
280 }
281 while (--tracelevel != new_tracelevel);
282
283 } else if (new_tracelevel > tracelevel) {
284 do {
285 tmsg(pat, on_msgs[tracelevel++]);
286 } while (tracelevel != new_tracelevel);
287 }
288
289 if (dump > 0
290 || (dump == 0 && old_tracelevel == 0 && tracelevel != 0))
291 trace_dump();
292 }
293
294
295 void
296 set_tracefile(char *filename,
297 char *pat,
298 int dump) /* -1=no dump, 0=default, 1=force */
299 {
300 struct stat stbuf;
301 FILE *n_ftrace;
302 char *fn;
303
304
305 /* Allow a null filename to increase the level if the trace file
306 * is already open or if coming from a trusted source, such as
307 * a signal or the command line.
308 */
309 if (filename == 0 || filename[0] == '\0') {
310 filename = 0;
311 if (ftrace == 0) {
312 if (inittracename[0] == '\0') {
313 msglog("missing trace file name");
314 return;
315 }
316 fn = inittracename;
317 } else {
318 fn = 0;
319 }
320
321 } else if (!strcmp(filename,"dump/../table")) {
322 trace_dump();
323 return;
324
325 } else {
326 /* Allow the file specified with "-T file" to be reopened,
327 * but require all other names specified over the net to
328 * match the official path. The path can specify a directory
329 * in which the file is to be created.
330 */
331 if (strcmp(filename, inittracename)
332 #ifdef _PATH_TRACE
333 && (strncmp(filename, _PATH_TRACE, sizeof(_PATH_TRACE)-1)
334 || strstr(filename,"../")
335 || 0 > stat(_PATH_TRACE, &stbuf))
336 #endif
337 ) {
338 msglog("wrong trace file \"%s\"", filename);
339 return;
340 }
341
342 /* If the new tracefile exists, it must be a regular file.
343 */
344 if (stat(filename, &stbuf) >= 0
345 && (stbuf.st_mode & S_IFMT) != S_IFREG) {
346 msglog("wrong type (%#x) of trace file \"%s\"",
347 stbuf.st_mode, filename);
348 return;
349 }
350
351 fn = filename;
352 }
353
354 if (fn != 0) {
355 n_ftrace = fopen(fn, "a");
356 if (n_ftrace == 0) {
357 msglog("failed to open trace file \"%s\" %s",
358 fn, strerror(errno));
359 if (fn == inittracename)
360 inittracename[0] = '\0';
361 return;
362 }
363
364 tmsg("switch to trace file %s", fn);
365
366 trace_close(file_trace = 1);
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 int i;
602
603
604 if (force_metric >= 0)
605 (void)fprintf(ftrace, "metric=%-2d ", rts->rts_metric);
606 if (force_ifp >= 0)
607 (void)fprintf(ftrace, "%s ", (rts->rts_ifp == 0 ?
608 "if?" : rts->rts_ifp->int_name));
609 if (force_router > 0
610 || (force_router == 0 && rts->rts_router != rts->rts_gate))
611 (void)fprintf(ftrace, "router=%s ",
612 naddr_ntoa(rts->rts_router));
613 if (force_time > 0)
614 (void)fprintf(ftrace, "%s ", ts(rts->rts_time));
615 if (force_tag > 0
616 || (force_tag == 0 && rts->rts_tag != 0))
617 (void)fprintf(ftrace, "tag=%#x ", ntohs(rts->rts_tag));
618 if (rts->rts_de_ag != 0) {
619 for (i = 1; (1 << i) <= rts->rts_de_ag; i++)
620 continue;
621 (void)fprintf(ftrace, "de_ag=%d ", i);
622 }
623
624 }
625
626
627 void
628 trace_if(char *act,
629 struct interface *ifp)
630 {
631 if (!TRACEACTIONS || ftrace == 0)
632 return;
633
634 lastlog();
635 (void)fprintf(ftrace, "%-3s interface %-4s ", act, ifp->int_name);
636 (void)fprintf(ftrace, "%-15s-->%-15s ",
637 naddr_ntoa(ifp->int_addr),
638 addrname(((ifp->int_if_flags & IFF_POINTOPOINT)
639 ? ifp->int_dstaddr
640 : htonl(ifp->int_net)),
641 ifp->int_mask, 1));
642 if (ifp->int_metric != 0)
643 (void)fprintf(ftrace, "metric=%d ", ifp->int_metric);
644 if (!IS_RIP_OUT_OFF(ifp->int_state)
645 && ifp->int_d_metric != 0)
646 (void)fprintf(ftrace, "fake_default=%d ", ifp->int_d_metric);
647 trace_bits(if_bits, ifp->int_if_flags, 0);
648 trace_bits(is_bits, ifp->int_state, 0);
649 (void)fputc('\n',ftrace);
650 }
651
652
653 void
654 trace_upslot(struct rt_entry *rt,
655 struct rt_spare *rts,
656 struct rt_spare *new)
657 {
658 if (!TRACEACTIONS || ftrace == 0)
659 return;
660
661 if (rts->rts_gate == new->rts_gate
662 && rts->rts_router == new->rts_router
663 && rts->rts_metric == new->rts_metric
664 && rts->rts_tag == new->rts_tag
665 && rts->rts_de_ag == new->rts_de_ag)
666 return;
667
668 lastlog();
669 if (new->rts_gate == 0) {
670 (void)fprintf(ftrace, "Del #%d %-35s ",
671 rts - rt->rt_spares,
672 rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
673 print_rts(rts, 0,0,0,0,
674 (rts != rt->rt_spares
675 || AGE_RT(rt->rt_state,new->rts_ifp)));
676
677 } else if (rts->rts_gate != RIP_DEFAULT) {
678 (void)fprintf(ftrace, "Chg #%d %-35s ",
679 rts - rt->rt_spares,
680 rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
681 print_rts(rts, 0,0,
682 rts->rts_gate != new->rts_gate,
683 rts->rts_tag != new->rts_tag,
684 rts != rt->rt_spares || AGE_RT(rt->rt_state,
685 rt->rt_ifp));
686
687 (void)fprintf(ftrace, "\n %19s%-16s ", "",
688 (new->rts_gate != rts->rts_gate
689 ? naddr_ntoa(new->rts_gate) : ""));
690 print_rts(new,
691 -(new->rts_metric == rts->rts_metric),
692 -(new->rts_ifp == rts->rts_ifp),
693 0,
694 rts->rts_tag != new->rts_tag,
695 (new->rts_time != rts->rts_time
696 && (rts != rt->rt_spares
697 || AGE_RT(rt->rt_state, new->rts_ifp))));
698
699 } else {
700 (void)fprintf(ftrace, "Add #%d %-35s ",
701 rts - rt->rt_spares,
702 rtname(rt->rt_dst, rt->rt_mask, new->rts_gate));
703 print_rts(new, 0,0,0,0,
704 (rts != rt->rt_spares
705 || AGE_RT(rt->rt_state,new->rts_ifp)));
706 }
707 (void)fputc('\n',ftrace);
708 }
709
710
711 /* miscellaneous message checked by the caller
712 */
713 void
714 trace_misc(char *p, ...)
715 {
716 va_list args;
717
718 if (ftrace == 0)
719 return;
720
721 lastlog();
722 va_start(args, p);
723 vfprintf(ftrace, p, args);
724 (void)fputc('\n',ftrace);
725 }
726
727
728 /* display a message if tracing actions
729 */
730 void
731 trace_act(char *p, ...)
732 {
733 va_list args;
734
735 if (!TRACEACTIONS || ftrace == 0)
736 return;
737
738 lastlog();
739 va_start(args, p);
740 vfprintf(ftrace, p, args);
741 (void)fputc('\n',ftrace);
742 }
743
744
745 /* display a message if tracing packets
746 */
747 void
748 trace_pkt(char *p, ...)
749 {
750 va_list args;
751
752 if (!TRACEPACKETS || ftrace == 0)
753 return;
754
755 lastlog();
756 va_start(args, p);
757 vfprintf(ftrace, p, args);
758 (void)fputc('\n',ftrace);
759 }
760
761
762 void
763 trace_change(struct rt_entry *rt,
764 u_int state,
765 struct rt_spare *new,
766 char *label)
767 {
768 if (ftrace == 0)
769 return;
770
771 if (rt->rt_metric == new->rts_metric
772 && rt->rt_gate == new->rts_gate
773 && rt->rt_router == new->rts_router
774 && rt->rt_state == state
775 && rt->rt_tag == new->rts_tag
776 && rt->rt_de_ag == new->rts_de_ag)
777 return;
778
779 lastlog();
780 (void)fprintf(ftrace, "%s %-35s ",
781 label,
782 rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
783 print_rts(rt->rt_spares,
784 0,0,0,0, AGE_RT(rt->rt_state, rt->rt_ifp));
785 trace_bits(rs_bits, rt->rt_state, rt->rt_state != state);
786
787 (void)fprintf(ftrace, "\n%*s %19s%-16s ",
788 strlen(label), "", "",
789 (rt->rt_gate != new->rts_gate
790 ? naddr_ntoa(new->rts_gate) : ""));
791 print_rts(new,
792 -(new->rts_metric == rt->rt_metric),
793 -(new->rts_ifp == rt->rt_ifp),
794 0,
795 rt->rt_tag != new->rts_tag,
796 (rt->rt_time != new->rts_time
797 && AGE_RT(rt->rt_state,new->rts_ifp)));
798 if (rt->rt_state != state)
799 trace_bits(rs_bits, state, 1);
800 (void)fputc('\n',ftrace);
801 }
802
803
804 void
805 trace_add_del(char * action, struct rt_entry *rt)
806 {
807 if (ftrace == 0)
808 return;
809
810 lastlog();
811 (void)fprintf(ftrace, "%s %-35s ",
812 action,
813 rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
814 print_rts(rt->rt_spares, 0,0,0,0,AGE_RT(rt->rt_state,rt->rt_ifp));
815 trace_bits(rs_bits, rt->rt_state, 0);
816 (void)fputc('\n',ftrace);
817 }
818
819
820 /* ARGSUSED */
821 static int
822 walk_trace(struct radix_node *rn,
823 struct walkarg *w)
824 {
825 #define RT ((struct rt_entry *)rn)
826 struct rt_spare *rts;
827 int i, age = AGE_RT(RT->rt_state, RT->rt_ifp);
828
829 (void)fprintf(ftrace, " %-35s ",
830 rtname(RT->rt_dst, RT->rt_mask, RT->rt_gate));
831 print_rts(&RT->rt_spares[0], 0,0,0,0,age);
832 trace_bits(rs_bits, RT->rt_state, 0);
833 if (RT->rt_poison_time >= now_garbage
834 && RT->rt_poison_metric < RT->rt_metric)
835 (void)fprintf(ftrace, "pm=%d@%s",
836 RT->rt_poison_metric, ts(RT->rt_poison_time));
837
838 rts = &RT->rt_spares[1];
839 for (i = 1; i < NUM_SPARES; i++, rts++) {
840 if (rts->rts_gate != RIP_DEFAULT) {
841 (void)fprintf(ftrace,"\n #%d%15s%-16s ",
842 i, "", naddr_ntoa(rts->rts_gate));
843 print_rts(rts, 0,0,0,0,1);
844 }
845 }
846 (void)fputc('\n',ftrace);
847
848 return 0;
849 }
850
851
852 static void
853 trace_dump(void)
854 {
855 struct interface *ifp;
856
857 if (ftrace == 0)
858 return;
859 lastlog();
860
861 (void)fputs("current daemon state:\n", ftrace);
862 for (ifp = ifnet; ifp != 0; ifp = ifp->int_next)
863 trace_if("", ifp);
864 (void)rn_walktree(rhead, walk_trace, 0);
865 }
866
867
868 void
869 trace_rip(char *dir1, char *dir2,
870 struct sockaddr_in *who,
871 struct interface *ifp,
872 struct rip *msg,
873 int size) /* total size of message */
874 {
875 struct netinfo *n, *lim;
876 # define NA (msg->rip_auths)
877 int i, seen_route;
878
879 if (!TRACEPACKETS || ftrace == 0)
880 return;
881
882 lastlog();
883 if (msg->rip_cmd >= RIPCMD_MAX
884 || msg->rip_vers == 0) {
885 (void)fprintf(ftrace, "%s bad RIPv%d cmd=%d %s"
886 " %s.%d size=%d\n",
887 dir1, msg->rip_vers, msg->rip_cmd, dir2,
888 naddr_ntoa(who->sin_addr.s_addr),
889 ntohs(who->sin_port),
890 size);
891 return;
892 }
893
894 (void)fprintf(ftrace, "%s RIPv%d %s %s %s.%d%s%s\n",
895 dir1, msg->rip_vers, ripcmds[msg->rip_cmd], dir2,
896 naddr_ntoa(who->sin_addr.s_addr), ntohs(who->sin_port),
897 ifp ? " via " : "", ifp ? ifp->int_name : "");
898 if (!TRACECONTENTS)
899 return;
900
901 seen_route = 0;
902 switch (msg->rip_cmd) {
903 case RIPCMD_REQUEST:
904 case RIPCMD_RESPONSE:
905 n = msg->rip_nets;
906 lim = (struct netinfo *)((char*)msg + size);
907 for (; n < lim; n++) {
908 if (!seen_route
909 && n->n_family == RIP_AF_UNSPEC
910 && ntohl(n->n_metric) == HOPCNT_INFINITY
911 && msg->rip_cmd == RIPCMD_REQUEST
912 && (n+1 == lim
913 || (n+2 == lim
914 && (n+1)->n_family == RIP_AF_AUTH))) {
915 (void)fputs("\tQUERY ", ftrace);
916 if (n->n_dst != 0)
917 (void)fprintf(ftrace, "%s ",
918 naddr_ntoa(n->n_dst));
919 if (n->n_mask != 0)
920 (void)fprintf(ftrace, "mask=%#x ",
921 (u_int)ntohl(n->n_mask));
922 if (n->n_nhop != 0)
923 (void)fprintf(ftrace, "nhop=%s ",
924 naddr_ntoa(n->n_nhop));
925 if (n->n_tag != 0)
926 (void)fprintf(ftrace, "tag=%#x ",
927 ntohs(n->n_tag));
928 (void)fputc('\n',ftrace);
929 continue;
930 }
931
932 if (n->n_family == RIP_AF_AUTH) {
933 if (NA->a_type == RIP_AUTH_PW
934 && n == msg->rip_nets) {
935 (void)fprintf(ftrace, "\tPassword"
936 " Authentication:"
937 " \"%s\"\n",
938 qstring(NA->au.au_pw,
939 RIP_AUTH_PW_LEN));
940 continue;
941 }
942
943 if (NA->a_type == RIP_AUTH_MD5
944 && n == msg->rip_nets) {
945 (void)fprintf(ftrace,
946 "\tMD5 Authentication"
947 " len=%d KeyID=%u"
948 " seqno=%u"
949 " rsvd=%#x,%#x\n",
950 NA->au.a_md5.md5_pkt_len,
951 NA->au.a_md5.md5_keyid,
952 NA->au.a_md5.md5_seqno,
953 NA->au.a_md5.rsvd[0],
954 NA->au.a_md5.rsvd[1]);
955 continue;
956 }
957 (void)fprintf(ftrace,
958 "\tAuthentication"
959 " type %d: ",
960 ntohs(NA->a_type));
961 for (i = 0;
962 i < sizeof(NA->au.au_pw);
963 i++)
964 (void)fprintf(ftrace, "%02x ",
965 NA->au.au_pw[i]);
966 (void)fputc('\n',ftrace);
967 continue;
968 }
969
970 seen_route = 1;
971 if (n->n_family != RIP_AF_INET) {
972 (void)fprintf(ftrace,
973 "\t(af %d) %-18s mask=%#x ",
974 ntohs(n->n_family),
975 naddr_ntoa(n->n_dst),
976 (u_int)ntohl(n->n_mask));
977 } else if (msg->rip_vers == RIPv1) {
978 (void)fprintf(ftrace, "\t%-18s ",
979 addrname(n->n_dst,
980 ntohl(n->n_mask),
981 n->n_mask==0 ? 2 : 1));
982 } else {
983 (void)fprintf(ftrace, "\t%-18s ",
984 addrname(n->n_dst,
985 ntohl(n->n_mask),
986 n->n_mask==0 ? 2 : 0));
987 }
988 (void)fprintf(ftrace, "metric=%-2d ",
989 (u_int)ntohl(n->n_metric));
990 if (n->n_nhop != 0)
991 (void)fprintf(ftrace, " nhop=%s ",
992 naddr_ntoa(n->n_nhop));
993 if (n->n_tag != 0)
994 (void)fprintf(ftrace, "tag=%#x",
995 ntohs(n->n_tag));
996 (void)fputc('\n',ftrace);
997 }
998 if (size != (char *)n - (char *)msg)
999 (void)fprintf(ftrace, "truncated record, len %d\n",
1000 size);
1001 break;
1002
1003 case RIPCMD_TRACEON:
1004 fprintf(ftrace, "\tfile=\"%.*s\"\n", size-4,
1005 msg->rip_tracefile);
1006 break;
1007
1008 case RIPCMD_TRACEOFF:
1009 break;
1010 }
1011 }
1012