2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)trace.c 8.1 (Berkeley) 6/5/93
30 * $FreeBSD: src/sbin/routed/trace.c,v 1.5.2.1 2002/11/07 17:19:13 imp Exp $
35 #include "pathnames.h"
37 #include <sys/signal.h>
40 #define NRECORDS 50 /* size of circular trace buffer */
42 int tracelevel, new_tracelevel;
43 FILE *ftrace; /* output trace file */
44 static const char *sigtrace_pat = "%s";
45 static char savetracename[MAXPATHLEN+1];
46 char inittracename[MAXPATHLEN+1];
47 int file_trace; /* 1=tracing to file, not stdout */
49 static void trace_dump(void);
50 static void tmsg(const char *, ...) __printflike(1, 2);
53 /* convert string to printable characters
56 qstring(u_char *s, int len)
58 static char buf[8*20+1];
63 for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
66 for (s2 = s+1; s2 < &s[len]; s2++) {
74 if (c >= ' ' && c < 0x7f && c != '\\') {
96 p += sprintf(p,"%o",c);
106 /* convert IP address to a string, but not into a single buffer
114 char str[16]; /* xxx.xxx.xxx.xxx\0 */
120 s = strcpy(bufs[bufno].str, inet_ntoa(addr));
121 bufno = (bufno+1) % NUM_BUFS;
128 saddr_ntoa(struct sockaddr *sa)
130 return (sa == NULL) ? "?" : naddr_ntoa(S_ADDR(sa));
138 secs += epoch.tv_sec;
139 memcpy(s, ctime(&secs)+11, 8);
145 /* On each event, display a time stamp.
146 * This assumes that 'now' is update once for each event, and
147 * that at least now.tv_usec changes.
149 static struct timeval lastlog_time;
154 if (lastlog_time.tv_sec != now.tv_sec
155 || lastlog_time.tv_usec != now.tv_usec) {
156 fprintf(ftrace, "-- %s --\n", ts(now.tv_sec));
163 tmsg(const char *p, ...)
167 if (ftrace != NULL) {
170 vfprintf(ftrace, p, args);
178 trace_close(int zap_stdio)
186 if (ftrace != NULL && zap_stdio) {
187 if (ftrace != stdout)
190 fd = open(_PATH_DEVNULL, O_RDWR);
191 if (isatty(STDIN_FILENO))
192 dup2(fd, STDIN_FILENO);
193 if (isatty(STDOUT_FILENO))
194 dup2(fd, STDOUT_FILENO);
195 if (isatty(STDERR_FILENO))
196 dup2(fd, STDERR_FILENO);
199 lastlog_time.tv_sec = 0;
206 if (ftrace != NULL) {
209 trace_off("tracing off: %s", strerror(ferror(ftrace)));
215 trace_off(const char *p, ...)
220 if (ftrace != NULL) {
223 vfprintf(ftrace, p, args);
226 trace_close(file_trace);
228 new_tracelevel = tracelevel = 0;
232 /* log a change in tracing
235 tracelevel_msg(const char *pat,
236 int dump) /* -1=no dump, 0=default, 1=force */
238 static const char *off_msgs[MAX_TRACELEVEL] = {
239 "Tracing actions stopped",
240 "Tracing packets stopped",
241 "Tracing packet contents stopped",
242 "Tracing kernel changes stopped",
244 static const char *on_msgs[MAX_TRACELEVEL] = {
245 "Tracing actions started",
246 "Tracing packets started",
247 "Tracing packet contents started",
248 "Tracing kernel changes started",
250 u_int old_tracelevel = tracelevel;
253 if (new_tracelevel < 0)
255 else if (new_tracelevel > MAX_TRACELEVEL)
256 new_tracelevel = MAX_TRACELEVEL;
258 if (new_tracelevel < tracelevel) {
259 if (new_tracelevel <= 0) {
260 trace_off(pat, off_msgs[0]);
262 tmsg(pat, off_msgs[tracelevel]);
264 while (--tracelevel != new_tracelevel);
266 } else if (new_tracelevel > tracelevel) {
268 tmsg(pat, on_msgs[tracelevel++]);
269 } while (tracelevel != new_tracelevel);
273 || (dump == 0 && old_tracelevel == 0 && tracelevel != 0))
279 set_tracefile(const char *filename,
281 int dump) /* -1=no dump, 0=default, 1=force */
288 /* Allow a null filename to increase the level if the trace file
289 * is already open or if coming from a trusted source, such as
290 * a signal or the command line.
292 if (filename == NULL || filename[0] == '\0') {
294 if (ftrace == NULL) {
295 if (inittracename[0] == '\0') {
296 msglog("missing trace file name");
304 } else if (!strcmp(filename,"dump/../table")) {
309 /* Allow the file specified with "-T file" to be reopened,
310 * but require all other names specified over the net to
311 * match the official path. The path can specify a directory
312 * in which the file is to be created.
314 if (strcmp(filename, inittracename)
316 && (strncmp(filename, _PATH_TRACE, sizeof(_PATH_TRACE)-1)
317 || strstr(filename,"../")
318 || 0 > stat(_PATH_TRACE, &stbuf))
321 msglog("wrong trace file \"%s\"", filename);
325 /* If the new tracefile exists, it must be a regular file.
327 if (stat(filename, &stbuf) >= 0 && !S_ISREG(stbuf.st_mode)) {
328 msglog("wrong type (%#x) of trace file \"%s\"",
329 stbuf.st_mode, filename);
337 n_ftrace = fopen(fn, "a");
338 if (n_ftrace == NULL) {
339 msglog("failed to open trace file \"%s\" %s",
340 fn, strerror(errno));
341 if (fn == inittracename)
342 inittracename[0] = '\0';
346 tmsg("switch to trace file %s", fn);
348 trace_close(file_trace = 1);
350 if (fn != savetracename)
351 strncpy(savetracename, fn, sizeof(savetracename)-1);
356 dup2(fileno(ftrace), STDOUT_FILENO);
357 dup2(fileno(ftrace), STDERR_FILENO);
360 if (new_tracelevel == 0 || filename == NULL)
362 tracelevel_msg(pat, dump != 0 ? dump : (filename != NULL));
368 sigtrace_on(__unused int s)
371 sigtrace_pat = "SIGUSR1: %s";
377 sigtrace_off(__unused int s)
380 sigtrace_pat = "SIGUSR2: %s";
384 /* Set tracing after a signal.
389 if (new_tracelevel == tracelevel)
392 /* If tracing entirely off, and there was no tracefile specified
393 * on the command line, then leave it off.
395 if (new_tracelevel > tracelevel && ftrace == NULL) {
396 if (savetracename[0] != '\0') {
397 set_tracefile(savetracename,sigtrace_pat,0);
398 } else if (inittracename[0] != '\0') {
399 set_tracefile(inittracename,sigtrace_pat,0);
405 tracelevel_msg(sigtrace_pat, 0);
410 /* display an address
413 addrname(naddr addr, /* in network byte order */
415 int force) /* 0=show mask if nonstandard, */
416 { /* 1=always show mask, 2=never */
426 s = strcpy(bufs[bufno].str, naddr_ntoa(addr));
427 bufno = (bufno+1) % NUM_BUFS;
429 if (force == 1 || (force == 0 && mask != std_mask(addr))) {
432 dmask = mask & -mask;
433 if (mask + dmask == 0) {
434 for (i = 0; i != 32 && ((1<<i) & mask) == 0; i++)
436 sprintf(sp, "/%d", 32-i);
439 sprintf(sp, " (mask %#x)", (u_int)mask);
448 /* display a bit-field
453 const char *bits_name;
456 static struct bits if_bits[] = {
457 { IFF_LOOPBACK, 0, "LOOPBACK" },
458 { IFF_POINTOPOINT, 0, "PT-TO-PT" },
462 static struct bits is_bits[] = {
463 { IS_ALIAS, 0, "ALIAS" },
464 { IS_SUBNET, 0, "" },
465 { IS_REMOTE, (IS_NO_RDISC
466 | IS_BCAST_RDISC), "REMOTE" },
467 { IS_PASSIVE, (IS_NO_RDISC
471 | IS_NO_AG), "PASSIVE" },
472 { IS_EXTERNAL, 0, "EXTERNAL" },
473 { IS_CHECKED, 0, "" },
474 { IS_ALL_HOSTS, 0, "" },
475 { IS_ALL_ROUTERS, 0, "" },
476 { IS_DISTRUST, 0, "DISTRUST" },
477 { IS_BROKE, IS_SICK, "BROKEN" },
478 { IS_SICK, 0, "SICK" },
479 { IS_DUP, 0, "DUPLICATE" },
480 { IS_REDIRECT_OK, 0, "REDIRECT_OK" },
481 { IS_NEED_NET_SYN, 0, "" },
482 { IS_NO_AG, IS_NO_SUPER_AG, "NO_AG" },
483 { IS_NO_SUPER_AG, 0, "NO_SUPER_AG" },
487 | IS_NO_RIPV2_OUT), 0, "NO_RIP" },
489 | IS_NO_RIPV1_OUT), 0, "RIPV2" },
490 { IS_NO_RIPV1_IN, 0, "NO_RIPV1_IN" },
491 { IS_NO_RIPV2_IN, 0, "NO_RIPV2_IN" },
492 { IS_NO_RIPV1_OUT, 0, "NO_RIPV1_OUT" },
493 { IS_NO_RIPV2_OUT, 0, "NO_RIPV2_OUT" },
496 | IS_NO_ADV_OUT), IS_BCAST_RDISC, "NO_RDISC" },
497 { IS_NO_SOL_OUT, 0, "NO_SOLICIT" },
498 { IS_SOL_OUT, 0, "SEND_SOLICIT" },
499 { IS_NO_ADV_OUT, IS_BCAST_RDISC, "NO_RDISC_ADV" },
500 { IS_ADV_OUT, 0, "RDISC_ADV" },
501 { IS_BCAST_RDISC, 0, "BCAST_RDISC" },
502 { IS_PM_RDISC, 0, "" },
506 static struct bits rs_bits[] = {
508 { RS_NET_INT, RS_NET_SYN, "NET_INT" },
509 { RS_NET_SYN, 0, "NET_SYN" },
510 { RS_SUBNET, 0, "" },
511 { RS_LOCAL, 0, "LOCAL" },
512 { RS_MHOME, 0, "MHOME" },
513 { RS_STATIC, 0, "STATIC" },
514 { RS_RDISC, 0, "RDISC" },
520 trace_bits(const struct bits *tbl,
535 && (b = tbl->bits_mask) != 0) {
536 if ((b & field) == b) {
537 if (tbl->bits_name[0] != '\0') {
540 fprintf(ftrace, "%s", tbl->bits_name);
543 if (0 == (field &= ~(b | tbl->bits_clear)))
548 if (field != 0 && tbl->bits_name != NULL) {
551 fprintf(ftrace, tbl->bits_name, field);
555 if (c != '<' || force)
565 static char buf[3*4+3+1+2+3 /* "xxx.xxx.xxx.xxx/xx-->" */
566 +3*4+3+1]; /* "xxx.xxx.xxx.xxx" */
569 i = sprintf(buf, "%-16s-->", addrname(dst, mask, 0));
570 sprintf(&buf[i], "%-*s", 15+20-MAX(20,i), naddr_ntoa(gate));
576 print_rts(struct rt_spare *rts,
577 int force_metric, /* -1=suppress, 0=default */
578 int force_ifp, /* -1=suppress, 0=default */
579 int force_router, /* -1=suppress, 0=default, 1=display */
580 int force_tag, /* -1=suppress, 0=default, 1=display */
581 int force_time) /* 0=suppress, 1=display */
586 if (force_metric >= 0)
587 fprintf(ftrace, "metric=%-2d ", rts->rts_metric);
589 fprintf(ftrace, "%s ", (rts->rts_ifp == 0 ?
590 "if?" : rts->rts_ifp->int_name));
592 || (force_router == 0 && rts->rts_router != rts->rts_gate))
593 fprintf(ftrace, "router=%s ", naddr_ntoa(rts->rts_router));
595 fprintf(ftrace, "%s ", ts(rts->rts_time));
597 || (force_tag == 0 && rts->rts_tag != 0))
598 fprintf(ftrace, "tag=%#x ", ntohs(rts->rts_tag));
599 if (rts->rts_de_ag != 0) {
600 for (i = 1; (u_int)(1 << i) <= rts->rts_de_ag; i++)
602 fprintf(ftrace, "de_ag=%d ", i);
609 trace_if(const char *act,
610 struct interface *ifp)
612 if (!TRACEACTIONS || ftrace == NULL)
616 fprintf(ftrace, "%-3s interface %-4s ", act, ifp->int_name);
617 fprintf(ftrace, "%-15s-->%-15s ",
618 naddr_ntoa(ifp->int_addr),
619 addrname(((ifp->int_if_flags & IFF_POINTOPOINT)
620 ? ifp->int_dstaddr : htonl(ifp->int_net)),
622 if (ifp->int_metric != 0)
623 fprintf(ftrace, "metric=%d ", ifp->int_metric);
624 if (!IS_RIP_OUT_OFF(ifp->int_state)
625 && ifp->int_d_metric != 0)
626 fprintf(ftrace, "fake_default=%d ", ifp->int_d_metric);
627 trace_bits(if_bits, ifp->int_if_flags, 0);
628 trace_bits(is_bits, ifp->int_state, 0);
634 trace_upslot(struct rt_entry *rt,
635 struct rt_spare *rts,
636 struct rt_spare *new)
638 if (!TRACEACTIONS || ftrace == NULL)
641 if (rts->rts_gate == new->rts_gate
642 && rts->rts_router == new->rts_router
643 && rts->rts_metric == new->rts_metric
644 && rts->rts_tag == new->rts_tag
645 && rts->rts_de_ag == new->rts_de_ag)
649 if (new->rts_gate == 0) {
650 fprintf(ftrace, "Del #%d %-35s ",
651 (int)(rts - rt->rt_spares),
652 rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
653 print_rts(rts, 0,0,0,0,
654 (rts != rt->rt_spares
655 || AGE_RT(rt->rt_state,new->rts_ifp)));
657 } else if (rts->rts_gate != RIP_DEFAULT) {
658 fprintf(ftrace, "Chg #%d %-35s ",
659 (int)(rts - rt->rt_spares),
660 rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
662 rts->rts_gate != new->rts_gate,
663 rts->rts_tag != new->rts_tag,
664 rts != rt->rt_spares || AGE_RT(rt->rt_state,
667 fprintf(ftrace, "\n %19s%-16s ", "",
668 (new->rts_gate != rts->rts_gate
669 ? naddr_ntoa(new->rts_gate) : ""));
671 -(new->rts_metric == rts->rts_metric),
672 -(new->rts_ifp == rts->rts_ifp),
674 rts->rts_tag != new->rts_tag,
675 (new->rts_time != rts->rts_time
676 && (rts != rt->rt_spares
677 || AGE_RT(rt->rt_state, new->rts_ifp))));
680 fprintf(ftrace, "Add #%d %-35s ",
681 (int)(rts - rt->rt_spares),
682 rtname(rt->rt_dst, rt->rt_mask, new->rts_gate));
683 print_rts(new, 0,0,0,0,
684 (rts != rt->rt_spares
685 || AGE_RT(rt->rt_state,new->rts_ifp)));
691 /* miscellaneous message checked by the caller
694 trace_misc(const char *p, ...)
703 vfprintf(ftrace, p, args);
708 /* display a message if tracing actions
711 trace_act(const char *p, ...)
715 if (!TRACEACTIONS || ftrace == NULL)
720 vfprintf(ftrace, p, args);
725 /* display a message if tracing packets
728 trace_pkt(const char *p, ...)
732 if (!TRACEPACKETS || ftrace == NULL)
737 vfprintf(ftrace, p, args);
743 trace_change(struct rt_entry *rt,
745 struct rt_spare *new,
751 if (rt->rt_metric == new->rts_metric
752 && rt->rt_gate == new->rts_gate
753 && rt->rt_router == new->rts_router
754 && rt->rt_state == state
755 && rt->rt_tag == new->rts_tag
756 && rt->rt_de_ag == new->rts_de_ag)
760 fprintf(ftrace, "%s %-35s ",
761 label, rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
762 print_rts(rt->rt_spares,
763 0,0,0,0, AGE_RT(rt->rt_state, rt->rt_ifp));
764 trace_bits(rs_bits, rt->rt_state, rt->rt_state != state);
766 fprintf(ftrace, "\n%*s %19s%-16s ",
767 (int)strlen(label), "", "",
768 (rt->rt_gate != new->rts_gate
769 ? naddr_ntoa(new->rts_gate) : ""));
771 -(new->rts_metric == rt->rt_metric),
772 -(new->rts_ifp == rt->rt_ifp),
774 rt->rt_tag != new->rts_tag,
775 (rt->rt_time != new->rts_time
776 && AGE_RT(rt->rt_state,new->rts_ifp)));
777 if (rt->rt_state != state)
778 trace_bits(rs_bits, state, 1);
784 trace_add_del(const char * action, struct rt_entry *rt)
790 fprintf(ftrace, "%s %-35s ",
791 action, rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
792 print_rts(rt->rt_spares, 0,0,0,0,AGE_RT(rt->rt_state,rt->rt_ifp));
793 trace_bits(rs_bits, rt->rt_state, 0);
800 walk_trace(struct radix_node *rn, __unused struct walkarg *w)
802 #define RT ((struct rt_entry *)rn)
803 struct rt_spare *rts;
806 fprintf(ftrace, " %-35s ",
807 rtname(RT->rt_dst, RT->rt_mask, RT->rt_gate));
808 print_rts(&RT->rt_spares[0], 0,0,0,0, AGE_RT(RT->rt_state, RT->rt_ifp));
809 trace_bits(rs_bits, RT->rt_state, 0);
810 if (RT->rt_poison_time >= now_garbage
811 && RT->rt_poison_metric < RT->rt_metric)
812 fprintf(ftrace, "pm=%d@%s",
813 RT->rt_poison_metric, ts(RT->rt_poison_time));
815 rts = &RT->rt_spares[1];
816 for (i = 1; i < NUM_SPARES; i++, rts++) {
817 if (rts->rts_gate != RIP_DEFAULT) {
818 fprintf(ftrace,"\n #%d%15s%-16s ",
819 i, "", naddr_ntoa(rts->rts_gate));
820 print_rts(rts, 0,0,0,0,1);
832 struct interface *ifp;
838 fputs("current daemon state:\n", ftrace);
839 for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next)
841 rn_walktree(rhead, walk_trace, 0);
846 trace_rip(const char *dir1, const char *dir2,
847 struct sockaddr_in *who,
848 struct interface *ifp,
850 int size) /* total size of message */
852 struct netinfo *n, *lim;
853 # define NA ((struct netauth*)n)
856 if (!TRACEPACKETS || ftrace == NULL)
860 if (msg->rip_cmd >= RIPCMD_MAX
861 || msg->rip_vers == 0) {
862 fprintf(ftrace, "%s bad RIPv%d cmd=%d %s"
864 dir1, msg->rip_vers, msg->rip_cmd, dir2,
865 naddr_ntoa(who->sin_addr.s_addr),
866 ntohs(who->sin_port),
871 fprintf(ftrace, "%s RIPv%d %s %s %s.%d%s%s\n",
872 dir1, msg->rip_vers, ripcmds[msg->rip_cmd], dir2,
873 naddr_ntoa(who->sin_addr.s_addr), ntohs(who->sin_port),
874 ifp ? " via " : "", ifp ? ifp->int_name : "");
879 switch (msg->rip_cmd) {
881 case RIPCMD_RESPONSE:
883 lim = (struct netinfo *)((char*)msg + size);
884 for (; n < lim; n++) {
886 && n->n_family == RIP_AF_UNSPEC
887 && ntohl(n->n_metric) == HOPCNT_INFINITY
888 && msg->rip_cmd == RIPCMD_REQUEST
891 && (n+1)->n_family == RIP_AF_AUTH))) {
892 fputs("\tQUERY ", ftrace);
894 fprintf(ftrace, "%s ",
895 naddr_ntoa(n->n_dst));
897 fprintf(ftrace, "mask=%#x ",
898 (u_int)ntohl(n->n_mask));
900 fprintf(ftrace, "nhop=%s ",
901 naddr_ntoa(n->n_nhop));
903 fprintf(ftrace, "tag=%#x ",
909 if (n->n_family == RIP_AF_AUTH) {
910 if (NA->a_type == RIP_AUTH_PW
911 && n == msg->rip_nets) {
912 fprintf(ftrace, "\tPassword"
915 qstring(NA->au.au_pw,
920 if (NA->a_type == RIP_AUTH_MD5
921 && n == msg->rip_nets) {
924 " pkt_len=%d KeyID=%u"
928 ntohs(NA->au.a_md5.md5_pkt_len),
929 NA->au.a_md5.md5_keyid,
930 NA->au.a_md5.md5_auth_len,
931 (int)ntohl(NA->au.a_md5.md5_seqno),
932 (int)ntohs(NA->au.a_md5.rsvd[0]),
933 (int)ntohs(NA->au.a_md5.rsvd[1]));
937 "\tAuthentication type %d: ",
940 i < (int)sizeof(NA->au.au_pw);
942 fprintf(ftrace, "%02x ",
949 if (n->n_family != RIP_AF_INET) {
951 "\t(af %d) %-18s mask=%#x ",
953 naddr_ntoa(n->n_dst),
954 (u_int)ntohl(n->n_mask));
955 } else if (msg->rip_vers == RIPv1) {
956 fprintf(ftrace, "\t%-18s ",
957 addrname(n->n_dst, ntohl(n->n_mask),
958 n->n_mask==0 ? 2 : 1));
960 fprintf(ftrace, "\t%-18s ",
961 addrname(n->n_dst, ntohl(n->n_mask),
962 n->n_mask==0 ? 2 : 0));
964 fprintf(ftrace, "metric=%-2d ",
965 (u_int)ntohl(n->n_metric));
967 fprintf(ftrace, " nhop=%s ",
968 naddr_ntoa(n->n_nhop));
970 fprintf(ftrace, "tag=%#x", ntohs(n->n_tag));
973 if (size != (char *)n - (char *)msg)
974 fprintf(ftrace, "truncated record, len %d\n", size);
978 fprintf(ftrace, "\tfile=\"%.*s\"\n", size-4,
982 case RIPCMD_TRACEOFF: