1 /* $NetBSD: trace.c,v 1.20 1997/10/19 18:23:55 mycroft 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.20 1997/10/19 18:23:55 mycroft 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 && !S_ISREG(stbuf.st_mode)) { 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 (int)(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 (int)(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 (int)(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 (int)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