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