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