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