1*79d830d6Smaya /* $NetBSD: trace.c,v 1.33 2017/10/02 11:02:19 maya Exp $ */
20114e805Scgd
361f28255Scgd /*
44c8599d3Smycroft * Copyright (c) 1983, 1988, 1993
54c8599d3Smycroft * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1561f28255Scgd * 3. All advertising materials mentioning features or use of this software
1694b2d428Schristos * must display the following acknowledgment:
1761f28255Scgd * This product includes software developed by the University of
1861f28255Scgd * California, Berkeley and its contributors.
1961f28255Scgd * 4. Neither the name of the University nor the names of its contributors
2061f28255Scgd * may be used to endorse or promote products derived from this software
2161f28255Scgd * without specific prior written permission.
2261f28255Scgd *
2361f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2461f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2561f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2661f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2761f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2861f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2961f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3061f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3161f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3261f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3361f28255Scgd * SUCH DAMAGE.
3461f28255Scgd */
3561f28255Scgd
3661f28255Scgd #define RIPCMDS
3761f28255Scgd #include "defs.h"
3861f28255Scgd #include "pathnames.h"
39fc1a5246Sthorpej #include <sys/stat.h>
40fc1a5246Sthorpej #include <sys/signal.h>
41fc1a5246Sthorpej #include <fcntl.h>
42fc1a5246Sthorpej
434fea751dSchristos #ifdef __NetBSD__
44*79d830d6Smaya __RCSID("$NetBSD: trace.c,v 1.33 2017/10/02 11:02:19 maya Exp $");
454fea751dSchristos #elif defined(__FreeBSD__)
464fea751dSchristos __RCSID("$FreeBSD$");
474fea751dSchristos #else
48f93fe60aSchristos __RCSID("Revision: 2.27 ");
49f93fe60aSchristos #ident "Revision: 2.27 "
504fea751dSchristos #endif
51fc1a5246Sthorpej
5261f28255Scgd #define NRECORDS 50 /* size of circular trace buffer */
53fc1a5246Sthorpej
54e7512e5aSchristos int tracelevel, new_tracelevel;
55fc1a5246Sthorpej FILE *ftrace = stdout; /* output trace file */
56756b1291Schristos static const char *sigtrace_pat = "%s";
57e7512e5aSchristos static char savetracename[MAXPATHLEN+1];
58e7512e5aSchristos char inittracename[MAXPATHLEN+1];
59e7512e5aSchristos int file_trace; /* 1=tracing to file, not stdout */
60fc1a5246Sthorpej
614d3fba59Schristos static void trace_dump(void);
62756b1291Schristos static void tmsg(const char *, ...) PATTRIB(1,2);
634d3fba59Schristos
64fc1a5246Sthorpej
65e7512e5aSchristos /* convert string to printable characters
66e7512e5aSchristos */
67e7512e5aSchristos static char *
qstring(u_char * s,int len)68e7512e5aSchristos qstring(u_char *s, int len)
69e7512e5aSchristos {
70e7512e5aSchristos static char buf[8*20+1];
71e7512e5aSchristos char *p;
72e7512e5aSchristos u_char *s2, c;
73efb986d7Sitojun int n;
74e7512e5aSchristos
75e7512e5aSchristos for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
76e7512e5aSchristos c = *s++;
77e7512e5aSchristos if (c == '\0') {
78e7512e5aSchristos for (s2 = s+1; s2 < &s[len]; s2++) {
79e7512e5aSchristos if (*s2 != '\0')
80e7512e5aSchristos break;
81e7512e5aSchristos }
82e7512e5aSchristos if (s2 >= &s[len])
83e7512e5aSchristos goto exit;
84e7512e5aSchristos }
85e7512e5aSchristos
86e7512e5aSchristos if (c >= ' ' && c < 0x7f && c != '\\') {
87e7512e5aSchristos *p++ = c;
88e7512e5aSchristos continue;
89e7512e5aSchristos }
90e7512e5aSchristos *p++ = '\\';
91e7512e5aSchristos switch (c) {
92e7512e5aSchristos case '\\':
93e7512e5aSchristos *p++ = '\\';
94e7512e5aSchristos break;
95e7512e5aSchristos case '\n':
96e7512e5aSchristos *p++= 'n';
97e7512e5aSchristos break;
98e7512e5aSchristos case '\r':
99e7512e5aSchristos *p++= 'r';
100e7512e5aSchristos break;
101e7512e5aSchristos case '\t':
102e7512e5aSchristos *p++ = 't';
103e7512e5aSchristos break;
104e7512e5aSchristos case '\b':
105e7512e5aSchristos *p++ = 'b';
106e7512e5aSchristos break;
107e7512e5aSchristos default:
108efb986d7Sitojun n = snprintf(p, sizeof(buf) - (p - buf), "%o", c);
109efb986d7Sitojun if (n <= 0)
110efb986d7Sitojun goto exit;
111efb986d7Sitojun p += n;
112e7512e5aSchristos break;
113e7512e5aSchristos }
114e7512e5aSchristos }
115e7512e5aSchristos exit:
116e7512e5aSchristos *p = '\0';
117e7512e5aSchristos return buf;
118e7512e5aSchristos }
119e7512e5aSchristos
120e7512e5aSchristos
121fc1a5246Sthorpej /* convert IP address to a string, but not into a single buffer
122fc1a5246Sthorpej */
123fc1a5246Sthorpej char *
naddr_ntoa(naddr a)124fc1a5246Sthorpej naddr_ntoa(naddr a)
125fc1a5246Sthorpej {
126fc1a5246Sthorpej #define NUM_BUFS 4
1274d3fba59Schristos static int bufno;
128fc1a5246Sthorpej static struct {
129fc1a5246Sthorpej char str[16]; /* xxx.xxx.xxx.xxx\0 */
130fc1a5246Sthorpej } bufs[NUM_BUFS];
131fc1a5246Sthorpej char *s;
1324d3fba59Schristos struct in_addr addr;
133fc1a5246Sthorpej
134fc1a5246Sthorpej addr.s_addr = a;
13516435cdbSitojun strlcpy(bufs[bufno].str, inet_ntoa(addr), sizeof(bufs[bufno].str));
13616435cdbSitojun s = bufs[bufno].str;
1374d3fba59Schristos bufno = (bufno+1) % NUM_BUFS;
138fc1a5246Sthorpej return s;
1394d3fba59Schristos #undef NUM_BUFS
140fc1a5246Sthorpej }
141fc1a5246Sthorpej
142fc1a5246Sthorpej
143756b1291Schristos const char *
saddr_ntoa(const struct sockaddr * sa)144f29f4783Schristos saddr_ntoa(const struct sockaddr *sa)
145fc1a5246Sthorpej {
146fc1a5246Sthorpej return (sa == 0) ? "?" : naddr_ntoa(S_ADDR(sa));
147fc1a5246Sthorpej }
148fc1a5246Sthorpej
149fc1a5246Sthorpej
150fc1a5246Sthorpej static char *
ts(time_t secs)151fc1a5246Sthorpej ts(time_t secs) {
152fc1a5246Sthorpej static char s[20];
153fc1a5246Sthorpej
154fc1a5246Sthorpej secs += epoch.tv_sec;
15594b2d428Schristos memcpy(s, ctime(&secs)+11, 8);
156fc1a5246Sthorpej s[8] = '\0';
157*79d830d6Smaya
158fc1a5246Sthorpej return s;
15961f28255Scgd }
16061f28255Scgd
16161f28255Scgd
162fc1a5246Sthorpej /* On each event, display a time stamp.
163fc1a5246Sthorpej * This assumes that 'now' is update once for each event, and
164fc1a5246Sthorpej * that at least now.tv_usec changes.
16561f28255Scgd */
166e7512e5aSchristos static struct timeval lastlog_time;
167e7512e5aSchristos
168e072e2aeScgd void
lastlog(void)169fc1a5246Sthorpej lastlog(void)
17061f28255Scgd {
171e7512e5aSchristos if (lastlog_time.tv_sec != now.tv_sec
172e7512e5aSchristos || lastlog_time.tv_usec != now.tv_usec) {
173fc1a5246Sthorpej (void)fprintf(ftrace, "-- %s --\n", ts(now.tv_sec));
174e7512e5aSchristos lastlog_time = now;
17561f28255Scgd }
176fc1a5246Sthorpej }
177fc1a5246Sthorpej
178fc1a5246Sthorpej
179fc1a5246Sthorpej static void
tmsg(const char * p,...)180756b1291Schristos tmsg(const char *p, ...)
181fc1a5246Sthorpej {
182fc1a5246Sthorpej va_list args;
183fc1a5246Sthorpej
184fc1a5246Sthorpej if (ftrace != 0) {
185fc1a5246Sthorpej lastlog();
186fc1a5246Sthorpej va_start(args, p);
187fc1a5246Sthorpej vfprintf(ftrace, p, args);
1884c999163Swiz va_end(args);
189e7512e5aSchristos (void)fputc('\n',ftrace);
19061f28255Scgd fflush(ftrace);
19161f28255Scgd }
192fc1a5246Sthorpej }
193fc1a5246Sthorpej
194fc1a5246Sthorpej
1956d8ef4dfSthorpej void
trace_close(int zap_stdio)1966d8ef4dfSthorpej trace_close(int zap_stdio)
197fc1a5246Sthorpej {
198fc1a5246Sthorpej int fd;
199fc1a5246Sthorpej
200fc1a5246Sthorpej
201fc1a5246Sthorpej fflush(stdout);
202fc1a5246Sthorpej fflush(stderr);
203fc1a5246Sthorpej
2046d8ef4dfSthorpej if (ftrace != 0 && zap_stdio) {
205e7512e5aSchristos if (ftrace != stdout)
206e7512e5aSchristos fclose(ftrace);
207e7512e5aSchristos ftrace = 0;
208fc1a5246Sthorpej fd = open(_PATH_DEVNULL, O_RDWR);
2099a02e676Schristos if (fd == -1)
2109a02e676Schristos return;
2116d8ef4dfSthorpej if (isatty(STDIN_FILENO))
212e7512e5aSchristos (void)dup2(fd, STDIN_FILENO);
2136d8ef4dfSthorpej if (isatty(STDOUT_FILENO))
214fc1a5246Sthorpej (void)dup2(fd, STDOUT_FILENO);
2156d8ef4dfSthorpej if (isatty(STDERR_FILENO))
216fc1a5246Sthorpej (void)dup2(fd, STDERR_FILENO);
217fc1a5246Sthorpej (void)close(fd);
218fc1a5246Sthorpej }
219e7512e5aSchristos lastlog_time.tv_sec = 0;
220fc1a5246Sthorpej }
221fc1a5246Sthorpej
22261f28255Scgd
223e072e2aeScgd void
trace_flush(void)224fc1a5246Sthorpej trace_flush(void)
22561f28255Scgd {
226fc1a5246Sthorpej if (ftrace != 0) {
227fc1a5246Sthorpej fflush(ftrace);
228fc1a5246Sthorpej if (ferror(ftrace))
229756b1291Schristos trace_off("tracing off: %s", strerror(ferror(ftrace)));
230fc1a5246Sthorpej }
231fc1a5246Sthorpej }
23261f28255Scgd
233fc1a5246Sthorpej
234fc1a5246Sthorpej void
trace_off(const char * p,...)235756b1291Schristos trace_off(const char *p, ...)
236fc1a5246Sthorpej {
237fc1a5246Sthorpej va_list args;
238fc1a5246Sthorpej
239fc1a5246Sthorpej
240fc1a5246Sthorpej if (ftrace != 0) {
241fc1a5246Sthorpej lastlog();
242fc1a5246Sthorpej va_start(args, p);
243fc1a5246Sthorpej vfprintf(ftrace, p, args);
2444c999163Swiz va_end(args);
245e7512e5aSchristos (void)fputc('\n',ftrace);
246fc1a5246Sthorpej }
2476d8ef4dfSthorpej trace_close(file_trace);
248fc1a5246Sthorpej
249fc1a5246Sthorpej new_tracelevel = tracelevel = 0;
250fc1a5246Sthorpej }
251fc1a5246Sthorpej
252fc1a5246Sthorpej
253e7512e5aSchristos /* log a change in tracing
254fc1a5246Sthorpej */
255fc1a5246Sthorpej void
tracelevel_msg(const char * pat,int dump)256756b1291Schristos tracelevel_msg(const char *pat,
257e7512e5aSchristos int dump) /* -1=no dump, 0=default, 1=force */
258fc1a5246Sthorpej {
259756b1291Schristos static const char *off_msgs[MAX_TRACELEVEL] = {
260fc1a5246Sthorpej "Tracing actions stopped",
261fc1a5246Sthorpej "Tracing packets stopped",
262fc1a5246Sthorpej "Tracing packet contents stopped",
2634d3fba59Schristos "Tracing kernel changes stopped",
26461f28255Scgd };
265756b1291Schristos static const char *on_msgs[MAX_TRACELEVEL] = {
266fc1a5246Sthorpej "Tracing actions started",
267fc1a5246Sthorpej "Tracing packets started",
268fc1a5246Sthorpej "Tracing packet contents started",
2694d3fba59Schristos "Tracing kernel changes started",
270fc1a5246Sthorpej };
271e7512e5aSchristos u_int old_tracelevel = tracelevel;
27261f28255Scgd
273fc1a5246Sthorpej
274e7512e5aSchristos if (new_tracelevel < 0)
275e7512e5aSchristos new_tracelevel = 0;
276e7512e5aSchristos else if (new_tracelevel > MAX_TRACELEVEL)
277fc1a5246Sthorpej new_tracelevel = MAX_TRACELEVEL;
278e7512e5aSchristos
279e7512e5aSchristos if (new_tracelevel < tracelevel) {
280e7512e5aSchristos if (new_tracelevel <= 0) {
281e7512e5aSchristos trace_off(pat, off_msgs[0]);
282e7512e5aSchristos } else do {
283e7512e5aSchristos tmsg(pat, off_msgs[tracelevel]);
284e7512e5aSchristos }
285e7512e5aSchristos while (--tracelevel != new_tracelevel);
286e7512e5aSchristos
287e7512e5aSchristos } else if (new_tracelevel > tracelevel) {
288e7512e5aSchristos do {
289e7512e5aSchristos tmsg(pat, on_msgs[tracelevel++]);
290e7512e5aSchristos } while (tracelevel != new_tracelevel);
291e7512e5aSchristos }
292e7512e5aSchristos
293e7512e5aSchristos if (dump > 0
294e7512e5aSchristos || (dump == 0 && old_tracelevel == 0 && tracelevel != 0))
295e7512e5aSchristos trace_dump();
296e7512e5aSchristos }
297e7512e5aSchristos
298e7512e5aSchristos
299e7512e5aSchristos void
set_tracefile(const char * filename,const char * pat,int dump)300756b1291Schristos set_tracefile(const char *filename,
301756b1291Schristos const char *pat,
302e7512e5aSchristos int dump) /* -1=no dump, 0=default, 1=force */
303e7512e5aSchristos {
304e7512e5aSchristos struct stat stbuf;
305e7512e5aSchristos FILE *n_ftrace;
306756b1291Schristos const char *fn;
307e7512e5aSchristos
308e7512e5aSchristos
309e7512e5aSchristos /* Allow a null filename to increase the level if the trace file
310e7512e5aSchristos * is already open or if coming from a trusted source, such as
311e7512e5aSchristos * a signal or the command line.
312e7512e5aSchristos */
313e7512e5aSchristos if (filename == 0 || filename[0] == '\0') {
314e7512e5aSchristos filename = 0;
315e7512e5aSchristos if (ftrace == 0) {
316e7512e5aSchristos if (inittracename[0] == '\0') {
317e7512e5aSchristos msglog("missing trace file name");
31861f28255Scgd return;
31961f28255Scgd }
320e7512e5aSchristos fn = inittracename;
321fc1a5246Sthorpej } else {
322e7512e5aSchristos fn = 0;
323fc1a5246Sthorpej }
324e7512e5aSchristos
325e7512e5aSchristos } else if (!strcmp(filename,"dump/../table")) {
326e7512e5aSchristos trace_dump();
327e7512e5aSchristos return;
328e7512e5aSchristos
329e7512e5aSchristos } else {
330e7512e5aSchristos /* Allow the file specified with "-T file" to be reopened,
331e7512e5aSchristos * but require all other names specified over the net to
332e7512e5aSchristos * match the official path. The path can specify a directory
333e7512e5aSchristos * in which the file is to be created.
334e7512e5aSchristos */
335e7512e5aSchristos if (strcmp(filename, inittracename)
336e7512e5aSchristos #ifdef _PATH_TRACE
337e7512e5aSchristos && (strncmp(filename, _PATH_TRACE, sizeof(_PATH_TRACE)-1)
338e7512e5aSchristos || strstr(filename,"../")
339e7512e5aSchristos || 0 > stat(_PATH_TRACE, &stbuf))
340e7512e5aSchristos #endif
341e7512e5aSchristos ) {
342e7512e5aSchristos msglog("wrong trace file \"%s\"", filename);
343e7512e5aSchristos return;
344fc1a5246Sthorpej }
345e7512e5aSchristos
346e7512e5aSchristos /* If the new tracefile exists, it must be a regular file.
347e7512e5aSchristos */
348b247da14Smycroft if (stat(filename, &stbuf) >= 0 && !S_ISREG(stbuf.st_mode)) {
349e7512e5aSchristos msglog("wrong type (%#x) of trace file \"%s\"",
350e7512e5aSchristos stbuf.st_mode, filename);
351e7512e5aSchristos return;
352fc1a5246Sthorpej }
353e7512e5aSchristos
354e7512e5aSchristos fn = filename;
355e7512e5aSchristos }
356e7512e5aSchristos
357e7512e5aSchristos if (fn != 0) {
358e7512e5aSchristos n_ftrace = fopen(fn, "a");
359e7512e5aSchristos if (n_ftrace == 0) {
360e7512e5aSchristos msglog("failed to open trace file \"%s\" %s",
361e7512e5aSchristos fn, strerror(errno));
362e7512e5aSchristos if (fn == inittracename)
363e7512e5aSchristos inittracename[0] = '\0';
364e7512e5aSchristos return;
365e7512e5aSchristos }
366e7512e5aSchristos
367e7512e5aSchristos tmsg("switch to trace file %s", fn);
368e7512e5aSchristos
3696d8ef4dfSthorpej trace_close(file_trace = 1);
370e7512e5aSchristos
371e7512e5aSchristos if (fn != savetracename)
37216435cdbSitojun strlcpy(savetracename, fn, sizeof(savetracename));
373e7512e5aSchristos ftrace = n_ftrace;
374e7512e5aSchristos
375e7512e5aSchristos fflush(stdout);
376e7512e5aSchristos fflush(stderr);
377e7512e5aSchristos dup2(fileno(ftrace), STDOUT_FILENO);
378e7512e5aSchristos dup2(fileno(ftrace), STDERR_FILENO);
379e7512e5aSchristos }
380e7512e5aSchristos
381e7512e5aSchristos if (new_tracelevel == 0 || filename == 0)
382e7512e5aSchristos new_tracelevel++;
383e7512e5aSchristos tracelevel_msg(pat, dump != 0 ? dump : (filename != 0));
384e7512e5aSchristos }
385e7512e5aSchristos
386e7512e5aSchristos
387e7512e5aSchristos /* ARGSUSED */
388e7512e5aSchristos void
sigtrace_on(int s UNUSED)389756b1291Schristos sigtrace_on(int s UNUSED)
390e7512e5aSchristos {
391e7512e5aSchristos new_tracelevel++;
392e7512e5aSchristos sigtrace_pat = "SIGUSR1: %s";
393e7512e5aSchristos }
394e7512e5aSchristos
395e7512e5aSchristos
396e7512e5aSchristos /* ARGSUSED */
397e7512e5aSchristos void
sigtrace_off(int s UNUSED)398756b1291Schristos sigtrace_off(int s UNUSED)
399e7512e5aSchristos {
400e7512e5aSchristos new_tracelevel--;
401e7512e5aSchristos sigtrace_pat = "SIGUSR2: %s";
402e7512e5aSchristos }
403e7512e5aSchristos
404e7512e5aSchristos
405e7512e5aSchristos /* Set tracing after a signal.
406e7512e5aSchristos */
407e7512e5aSchristos void
set_tracelevel(void)408e7512e5aSchristos set_tracelevel(void)
409e7512e5aSchristos {
410e7512e5aSchristos if (new_tracelevel == tracelevel)
411e7512e5aSchristos return;
412e7512e5aSchristos
413e7512e5aSchristos /* If tracing entirely off, and there was no tracefile specified
414e7512e5aSchristos * on the command line, then leave it off.
415e7512e5aSchristos */
416e7512e5aSchristos if (new_tracelevel > tracelevel && ftrace == 0) {
417e7512e5aSchristos if (savetracename[0] != '\0') {
418e7512e5aSchristos set_tracefile(savetracename,sigtrace_pat,0);
419e7512e5aSchristos } else if (inittracename[0] != '\0') {
420e7512e5aSchristos set_tracefile(inittracename,sigtrace_pat,0);
421e7512e5aSchristos } else {
422e7512e5aSchristos new_tracelevel = 0;
423e7512e5aSchristos return;
424e7512e5aSchristos }
425e7512e5aSchristos } else {
426e7512e5aSchristos tracelevel_msg(sigtrace_pat, 0);
427e7512e5aSchristos }
428fc1a5246Sthorpej }
429fc1a5246Sthorpej
430fc1a5246Sthorpej
431fc1a5246Sthorpej /* display an address
432fc1a5246Sthorpej */
433fc1a5246Sthorpej char *
addrname(naddr addr,naddr mask,int force)434fc1a5246Sthorpej addrname(naddr addr, /* in network byte order */
435fc1a5246Sthorpej naddr mask,
436fc1a5246Sthorpej int force) /* 0=show mask if nonstandard, */
437fc1a5246Sthorpej { /* 1=always show mask, 2=never */
4384d3fba59Schristos #define NUM_BUFS 4
4394d3fba59Schristos static int bufno;
4404d3fba59Schristos static struct {
4414d3fba59Schristos char str[15+20];
4424d3fba59Schristos } bufs[NUM_BUFS];
4434d3fba59Schristos char *s, *sp;
444fc1a5246Sthorpej naddr dmask;
44516435cdbSitojun size_t l;
446fc1a5246Sthorpej int i;
447fc1a5246Sthorpej
44816435cdbSitojun strlcpy(bufs[bufno].str, naddr_ntoa(addr), sizeof(bufs[bufno].str));
44916435cdbSitojun s = bufs[bufno].str;
45016435cdbSitojun l = sizeof(bufs[bufno].str);
4514d3fba59Schristos bufno = (bufno+1) % NUM_BUFS;
452fc1a5246Sthorpej
453fc1a5246Sthorpej if (force == 1 || (force == 0 && mask != std_mask(addr))) {
454fc1a5246Sthorpej sp = &s[strlen(s)];
455fc1a5246Sthorpej
456fc1a5246Sthorpej dmask = mask & -mask;
457fc1a5246Sthorpej if (mask + dmask == 0) {
458fc1a5246Sthorpej for (i = 0; i != 32 && ((1<<i) & mask) == 0; i++)
45961f28255Scgd continue;
46016435cdbSitojun (void)snprintf(sp, s + l - sp, "/%d", 32-i);
461fc1a5246Sthorpej
462fc1a5246Sthorpej } else {
46316435cdbSitojun (void)snprintf(sp, s + l - sp, " (mask %#x)",
46416435cdbSitojun (u_int)mask);
46561f28255Scgd }
46661f28255Scgd }
467fc1a5246Sthorpej
468fc1a5246Sthorpej return s;
4694d3fba59Schristos #undef NUM_BUFS
47061f28255Scgd }
471fc1a5246Sthorpej
472fc1a5246Sthorpej
473fc1a5246Sthorpej /* display a bit-field
474fc1a5246Sthorpej */
475fc1a5246Sthorpej struct bits {
476756b1291Schristos u_int bits_mask;
477756b1291Schristos u_int bits_clear;
478756b1291Schristos const char *bits_name;
479fc1a5246Sthorpej };
480fc1a5246Sthorpej
481fc1a5246Sthorpej static struct bits if_bits[] = {
482fc1a5246Sthorpej { IFF_LOOPBACK, 0, "LOOPBACK" },
483fc1a5246Sthorpej { IFF_POINTOPOINT, 0, "PT-TO-PT" },
484fc1a5246Sthorpej { 0, 0, 0}
485fc1a5246Sthorpej };
486fc1a5246Sthorpej
487fc1a5246Sthorpej static struct bits is_bits[] = {
488e7512e5aSchristos { IS_ALIAS, 0, "ALIAS" },
489fc1a5246Sthorpej { IS_SUBNET, 0, "" },
490e7512e5aSchristos { IS_REMOTE, (IS_NO_RDISC
491e7512e5aSchristos | IS_BCAST_RDISC), "REMOTE" },
492fc1a5246Sthorpej { IS_PASSIVE, (IS_NO_RDISC
493fc1a5246Sthorpej | IS_NO_RIP
494fc1a5246Sthorpej | IS_NO_SUPER_AG
495fc1a5246Sthorpej | IS_PM_RDISC
496fc1a5246Sthorpej | IS_NO_AG), "PASSIVE" },
497fc1a5246Sthorpej { IS_EXTERNAL, 0, "EXTERNAL" },
498fc1a5246Sthorpej { IS_CHECKED, 0, "" },
499fc1a5246Sthorpej { IS_ALL_HOSTS, 0, "" },
500fc1a5246Sthorpej { IS_ALL_ROUTERS, 0, "" },
501e7512e5aSchristos { IS_DISTRUST, 0, "DISTRUST" },
502fc1a5246Sthorpej { IS_BROKE, IS_SICK, "BROKEN" },
503fc1a5246Sthorpej { IS_SICK, 0, "SICK" },
504e7512e5aSchristos { IS_DUP, 0, "DUPLICATE" },
505e7512e5aSchristos { IS_REDIRECT_OK, 0, "REDIRECT_OK" },
506fc1a5246Sthorpej { IS_NEED_NET_SYN, 0, "" },
507fc1a5246Sthorpej { IS_NO_AG, IS_NO_SUPER_AG, "NO_AG" },
508fc1a5246Sthorpej { IS_NO_SUPER_AG, 0, "NO_SUPER_AG" },
509fc1a5246Sthorpej { (IS_NO_RIPV1_IN
510fc1a5246Sthorpej | IS_NO_RIPV2_IN
511fc1a5246Sthorpej | IS_NO_RIPV1_OUT
512fc1a5246Sthorpej | IS_NO_RIPV2_OUT), 0, "NO_RIP" },
513fc1a5246Sthorpej { (IS_NO_RIPV1_IN
514fc1a5246Sthorpej | IS_NO_RIPV1_OUT), 0, "RIPV2" },
515fc1a5246Sthorpej { IS_NO_RIPV1_IN, 0, "NO_RIPV1_IN" },
516fc1a5246Sthorpej { IS_NO_RIPV2_IN, 0, "NO_RIPV2_IN" },
517fc1a5246Sthorpej { IS_NO_RIPV1_OUT, 0, "NO_RIPV1_OUT" },
518fc1a5246Sthorpej { IS_NO_RIPV2_OUT, 0, "NO_RIPV2_OUT" },
519fc1a5246Sthorpej { (IS_NO_ADV_IN
520fc1a5246Sthorpej | IS_NO_SOL_OUT
521fc1a5246Sthorpej | IS_NO_ADV_OUT), IS_BCAST_RDISC, "NO_RDISC" },
522fc1a5246Sthorpej { IS_NO_SOL_OUT, 0, "NO_SOLICIT" },
523fc1a5246Sthorpej { IS_SOL_OUT, 0, "SEND_SOLICIT" },
524fc1a5246Sthorpej { IS_NO_ADV_OUT, IS_BCAST_RDISC, "NO_RDISC_ADV" },
525fc1a5246Sthorpej { IS_ADV_OUT, 0, "RDISC_ADV" },
526fc1a5246Sthorpej { IS_BCAST_RDISC, 0, "BCAST_RDISC" },
527e7512e5aSchristos { IS_PM_RDISC, 0, "" },
528fc1a5246Sthorpej { 0, 0, "%#x"}
529fc1a5246Sthorpej };
530fc1a5246Sthorpej
531fc1a5246Sthorpej static struct bits rs_bits[] = {
532fc1a5246Sthorpej { RS_IF, 0, "IF" },
533fc1a5246Sthorpej { RS_NET_INT, RS_NET_SYN, "NET_INT" },
534fc1a5246Sthorpej { RS_NET_SYN, 0, "NET_SYN" },
535fc1a5246Sthorpej { RS_SUBNET, 0, "" },
536fc1a5246Sthorpej { RS_LOCAL, 0, "LOCAL" },
537fc1a5246Sthorpej { RS_MHOME, 0, "MHOME" },
538fc1a5246Sthorpej { RS_STATIC, 0, "STATIC" },
539fc1a5246Sthorpej { RS_RDISC, 0, "RDISC" },
540fc1a5246Sthorpej { 0, 0, "%#x"}
541fc1a5246Sthorpej };
542fc1a5246Sthorpej
543fc1a5246Sthorpej
544fc1a5246Sthorpej static void
trace_bits(const struct bits * tbl,u_int field,int force)545756b1291Schristos trace_bits(const struct bits *tbl,
546fc1a5246Sthorpej u_int field,
547fc1a5246Sthorpej int force)
548fc1a5246Sthorpej {
549756b1291Schristos u_int b;
550fc1a5246Sthorpej char c;
551fc1a5246Sthorpej
552fc1a5246Sthorpej if (force) {
553fc1a5246Sthorpej (void)putc('<', ftrace);
554fc1a5246Sthorpej c = 0;
555fc1a5246Sthorpej } else {
556fc1a5246Sthorpej c = '<';
55761f28255Scgd }
558fc1a5246Sthorpej
559fc1a5246Sthorpej while (field != 0
560fc1a5246Sthorpej && (b = tbl->bits_mask) != 0) {
561fc1a5246Sthorpej if ((b & field) == b) {
562fc1a5246Sthorpej if (tbl->bits_name[0] != '\0') {
563fc1a5246Sthorpej if (c)
564fc1a5246Sthorpej (void)putc(c, ftrace);
565fc1a5246Sthorpej (void)fprintf(ftrace, "%s", tbl->bits_name);
566fc1a5246Sthorpej c = '|';
56761f28255Scgd }
568fc1a5246Sthorpej if (0 == (field &= ~(b | tbl->bits_clear)))
569fc1a5246Sthorpej break;
570fc1a5246Sthorpej }
571fc1a5246Sthorpej tbl++;
572fc1a5246Sthorpej }
573fc1a5246Sthorpej if (field != 0 && tbl->bits_name != 0) {
574fc1a5246Sthorpej if (c)
575fc1a5246Sthorpej (void)putc(c, ftrace);
576fc1a5246Sthorpej (void)fprintf(ftrace, tbl->bits_name, field);
577fc1a5246Sthorpej c = '|';
578fc1a5246Sthorpej }
579fc1a5246Sthorpej
580fc1a5246Sthorpej if (c != '<' || force)
581fc1a5246Sthorpej (void)fputs("> ", ftrace);
582fc1a5246Sthorpej }
583fc1a5246Sthorpej
584fc1a5246Sthorpej
585e7512e5aSchristos char *
rtname(naddr dst,naddr mask,naddr gate)586e7512e5aSchristos rtname(naddr dst,
587fc1a5246Sthorpej naddr mask,
588e7512e5aSchristos naddr gate)
589fc1a5246Sthorpej {
590fc1a5246Sthorpej static char buf[3*4+3+1+2+3 /* "xxx.xxx.xxx.xxx/xx-->" */
591fc1a5246Sthorpej +3*4+3+1]; /* "xxx.xxx.xxx.xxx" */
592fc1a5246Sthorpej int i;
593fc1a5246Sthorpej
59416435cdbSitojun i = snprintf(buf, sizeof(buf), "%-16s-->", addrname(dst, mask, 0));
59573a6ad29Slukem if (i < 0 || (size_t)i >= sizeof(buf))
59616435cdbSitojun return buf;
59716435cdbSitojun (void)snprintf(&buf[i], sizeof(buf) - i, "%-*s", 15+20-MAX(20, i),
59816435cdbSitojun naddr_ntoa(gate));
599fc1a5246Sthorpej return buf;
600fc1a5246Sthorpej }
601fc1a5246Sthorpej
60261f28255Scgd
603e7512e5aSchristos static void
print_rts(struct rt_spare * rts,int force_metric,int force_ifp,int force_router,int force_tag,int force_time)604e7512e5aSchristos print_rts(struct rt_spare *rts,
605e7512e5aSchristos int force_metric, /* -1=suppress, 0=default */
606e7512e5aSchristos int force_ifp, /* -1=suppress, 0=default */
607e7512e5aSchristos int force_router, /* -1=suppress, 0=default, 1=display */
608e7512e5aSchristos int force_tag, /* -1=suppress, 0=default, 1=display */
609e7512e5aSchristos int force_time) /* 0=suppress, 1=display */
610e7512e5aSchristos {
6116d8ef4dfSthorpej int i;
6126d8ef4dfSthorpej
6136d8ef4dfSthorpej
614e7512e5aSchristos if (force_metric >= 0)
615e7512e5aSchristos (void)fprintf(ftrace, "metric=%-2d ", rts->rts_metric);
616e7512e5aSchristos if (force_ifp >= 0)
617e7512e5aSchristos (void)fprintf(ftrace, "%s ", (rts->rts_ifp == 0 ?
618e7512e5aSchristos "if?" : rts->rts_ifp->int_name));
619e7512e5aSchristos if (force_router > 0
620e7512e5aSchristos || (force_router == 0 && rts->rts_router != rts->rts_gate))
621e7512e5aSchristos (void)fprintf(ftrace, "router=%s ",
622e7512e5aSchristos naddr_ntoa(rts->rts_router));
623e7512e5aSchristos if (force_time > 0)
624e7512e5aSchristos (void)fprintf(ftrace, "%s ", ts(rts->rts_time));
625e7512e5aSchristos if (force_tag > 0
626e7512e5aSchristos || (force_tag == 0 && rts->rts_tag != 0))
6276d8ef4dfSthorpej (void)fprintf(ftrace, "tag=%#x ", ntohs(rts->rts_tag));
6286d8ef4dfSthorpej if (rts->rts_de_ag != 0) {
629756b1291Schristos for (i = 1; (u_int)(1 << i) <= rts->rts_de_ag; i++)
6306d8ef4dfSthorpej continue;
6316d8ef4dfSthorpej (void)fprintf(ftrace, "de_ag=%d ", i);
6326d8ef4dfSthorpej }
6336d8ef4dfSthorpej
634e7512e5aSchristos }
635e7512e5aSchristos
636e7512e5aSchristos
637e072e2aeScgd void
trace_if(const char * act,struct interface * ifp)638756b1291Schristos trace_if(const char *act,
639fc1a5246Sthorpej struct interface *ifp)
64061f28255Scgd {
641fc1a5246Sthorpej if (!TRACEACTIONS || ftrace == 0)
64261f28255Scgd return;
643fc1a5246Sthorpej
644fc1a5246Sthorpej lastlog();
645e7512e5aSchristos (void)fprintf(ftrace, "%-3s interface %-4s ", act, ifp->int_name);
6464d3fba59Schristos (void)fprintf(ftrace, "%-15s-->%-15s ",
647fc1a5246Sthorpej naddr_ntoa(ifp->int_addr),
648e7512e5aSchristos addrname(((ifp->int_if_flags & IFF_POINTOPOINT)
6494d3fba59Schristos ? ifp->int_dstaddr
650e7512e5aSchristos : htonl(ifp->int_net)),
6514d3fba59Schristos ifp->int_mask, 1));
6524d3fba59Schristos if (ifp->int_metric != 0)
653fc1a5246Sthorpej (void)fprintf(ftrace, "metric=%d ", ifp->int_metric);
654f93fe60aSchristos if (ifp->int_adj_inmetric != 0)
655f93fe60aSchristos (void)fprintf(ftrace, "adj_inmetric=%u ",
656f93fe60aSchristos ifp->int_adj_inmetric);
657f93fe60aSchristos if (ifp->int_adj_outmetric != 0)
658f93fe60aSchristos (void)fprintf(ftrace, "adj_outmetric=%u ",
659f93fe60aSchristos ifp->int_adj_outmetric);
660e7512e5aSchristos if (!IS_RIP_OUT_OFF(ifp->int_state)
661e7512e5aSchristos && ifp->int_d_metric != 0)
662f93fe60aSchristos (void)fprintf(ftrace, "fake_default=%u ", ifp->int_d_metric);
663fc1a5246Sthorpej trace_bits(if_bits, ifp->int_if_flags, 0);
664fc1a5246Sthorpej trace_bits(is_bits, ifp->int_state, 0);
665fc1a5246Sthorpej (void)fputc('\n',ftrace);
66661f28255Scgd }
667fc1a5246Sthorpej
66861f28255Scgd
669e072e2aeScgd void
trace_upslot(struct rt_entry * rt,struct rt_spare * rts,struct rt_spare * new)670fc1a5246Sthorpej trace_upslot(struct rt_entry *rt,
671fc1a5246Sthorpej struct rt_spare *rts,
6726d8ef4dfSthorpej struct rt_spare *new)
67361f28255Scgd {
674fc1a5246Sthorpej if (!TRACEACTIONS || ftrace == 0)
675fc1a5246Sthorpej return;
676e7512e5aSchristos
6776d8ef4dfSthorpej if (rts->rts_gate == new->rts_gate
6786d8ef4dfSthorpej && rts->rts_router == new->rts_router
6796d8ef4dfSthorpej && rts->rts_metric == new->rts_metric
6806d8ef4dfSthorpej && rts->rts_tag == new->rts_tag
6816d8ef4dfSthorpej && rts->rts_de_ag == new->rts_de_ag)
682fc1a5246Sthorpej return;
683fc1a5246Sthorpej
684fc1a5246Sthorpej lastlog();
6856d8ef4dfSthorpej if (new->rts_gate == 0) {
686e7512e5aSchristos (void)fprintf(ftrace, "Del #%d %-35s ",
68752ff5d8fSmrg (int)(rts - rt->rt_spares),
688e7512e5aSchristos rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
6896d8ef4dfSthorpej print_rts(rts, 0,0,0,0,
6906d8ef4dfSthorpej (rts != rt->rt_spares
6916d8ef4dfSthorpej || AGE_RT(rt->rt_state,new->rts_ifp)));
692e7512e5aSchristos
693e7512e5aSchristos } else if (rts->rts_gate != RIP_DEFAULT) {
694fc1a5246Sthorpej (void)fprintf(ftrace, "Chg #%d %-35s ",
69552ff5d8fSmrg (int)(rts - rt->rt_spares),
696e7512e5aSchristos rtname(rt->rt_dst, rt->rt_mask, rts->rts_gate));
697e7512e5aSchristos print_rts(rts, 0,0,
6986d8ef4dfSthorpej rts->rts_gate != new->rts_gate,
6996d8ef4dfSthorpej rts->rts_tag != new->rts_tag,
700e7512e5aSchristos rts != rt->rt_spares || AGE_RT(rt->rt_state,
701e7512e5aSchristos rt->rt_ifp));
702fc1a5246Sthorpej
703e7512e5aSchristos (void)fprintf(ftrace, "\n %19s%-16s ", "",
7046d8ef4dfSthorpej (new->rts_gate != rts->rts_gate
7056d8ef4dfSthorpej ? naddr_ntoa(new->rts_gate) : ""));
7066d8ef4dfSthorpej print_rts(new,
7076d8ef4dfSthorpej -(new->rts_metric == rts->rts_metric),
7086d8ef4dfSthorpej -(new->rts_ifp == rts->rts_ifp),
709e7512e5aSchristos 0,
7106d8ef4dfSthorpej rts->rts_tag != new->rts_tag,
7116d8ef4dfSthorpej (new->rts_time != rts->rts_time
7126d8ef4dfSthorpej && (rts != rt->rt_spares
7136d8ef4dfSthorpej || AGE_RT(rt->rt_state, new->rts_ifp))));
714fc1a5246Sthorpej
715fc1a5246Sthorpej } else {
716fc1a5246Sthorpej (void)fprintf(ftrace, "Add #%d %-35s ",
71752ff5d8fSmrg (int)(rts - rt->rt_spares),
7186d8ef4dfSthorpej rtname(rt->rt_dst, rt->rt_mask, new->rts_gate));
7196d8ef4dfSthorpej print_rts(new, 0,0,0,0,
7206d8ef4dfSthorpej (rts != rt->rt_spares
7216d8ef4dfSthorpej || AGE_RT(rt->rt_state,new->rts_ifp)));
72261f28255Scgd }
723e7512e5aSchristos (void)fputc('\n',ftrace);
72461f28255Scgd }
72561f28255Scgd
726fc1a5246Sthorpej
7276d8ef4dfSthorpej /* miscellaneous message checked by the caller
7284d3fba59Schristos */
7294d3fba59Schristos void
trace_misc(const char * p,...)730756b1291Schristos trace_misc(const char *p, ...)
7314d3fba59Schristos {
7324d3fba59Schristos va_list args;
7334d3fba59Schristos
7346d8ef4dfSthorpej if (ftrace == 0)
7354d3fba59Schristos return;
7364d3fba59Schristos
7374d3fba59Schristos lastlog();
7384d3fba59Schristos va_start(args, p);
7394d3fba59Schristos vfprintf(ftrace, p, args);
7404c999163Swiz va_end(args);
741e7512e5aSchristos (void)fputc('\n',ftrace);
7424d3fba59Schristos }
7434d3fba59Schristos
7444d3fba59Schristos
745fc1a5246Sthorpej /* display a message if tracing actions
746fc1a5246Sthorpej */
747fc1a5246Sthorpej void
trace_act(const char * p,...)748756b1291Schristos trace_act(const char *p, ...)
749fc1a5246Sthorpej {
750fc1a5246Sthorpej va_list args;
751fc1a5246Sthorpej
752fc1a5246Sthorpej if (!TRACEACTIONS || ftrace == 0)
753fc1a5246Sthorpej return;
754fc1a5246Sthorpej
755fc1a5246Sthorpej lastlog();
756fc1a5246Sthorpej va_start(args, p);
757fc1a5246Sthorpej vfprintf(ftrace, p, args);
7584c999163Swiz va_end(args);
759e7512e5aSchristos (void)fputc('\n',ftrace);
760fc1a5246Sthorpej }
761fc1a5246Sthorpej
762fc1a5246Sthorpej
763fc1a5246Sthorpej /* display a message if tracing packets
764fc1a5246Sthorpej */
765fc1a5246Sthorpej void
trace_pkt(const char * p,...)766756b1291Schristos trace_pkt(const char *p, ...)
767fc1a5246Sthorpej {
768fc1a5246Sthorpej va_list args;
769fc1a5246Sthorpej
770fc1a5246Sthorpej if (!TRACEPACKETS || ftrace == 0)
771fc1a5246Sthorpej return;
772fc1a5246Sthorpej
773fc1a5246Sthorpej lastlog();
774fc1a5246Sthorpej va_start(args, p);
775fc1a5246Sthorpej vfprintf(ftrace, p, args);
7764c999163Swiz va_end(args);
777e7512e5aSchristos (void)fputc('\n',ftrace);
778fc1a5246Sthorpej }
779fc1a5246Sthorpej
780fc1a5246Sthorpej
781e072e2aeScgd void
trace_change(struct rt_entry * rt,u_int state,struct rt_spare * new,const char * label)782fc1a5246Sthorpej trace_change(struct rt_entry *rt,
783fc1a5246Sthorpej u_int state,
7846d8ef4dfSthorpej struct rt_spare *new,
785756b1291Schristos const char *label)
78661f28255Scgd {
787fc1a5246Sthorpej if (ftrace == 0)
78861f28255Scgd return;
789fc1a5246Sthorpej
7906d8ef4dfSthorpej if (rt->rt_metric == new->rts_metric
7916d8ef4dfSthorpej && rt->rt_gate == new->rts_gate
7926d8ef4dfSthorpej && rt->rt_router == new->rts_router
793fc1a5246Sthorpej && rt->rt_state == state
7946d8ef4dfSthorpej && rt->rt_tag == new->rts_tag
7956d8ef4dfSthorpej && rt->rt_de_ag == new->rts_de_ag)
796fc1a5246Sthorpej return;
797fc1a5246Sthorpej
798fc1a5246Sthorpej lastlog();
799e7512e5aSchristos (void)fprintf(ftrace, "%s %-35s ",
800fc1a5246Sthorpej label,
801e7512e5aSchristos rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
802e7512e5aSchristos print_rts(rt->rt_spares,
803e7512e5aSchristos 0,0,0,0, AGE_RT(rt->rt_state, rt->rt_ifp));
804fc1a5246Sthorpej trace_bits(rs_bits, rt->rt_state, rt->rt_state != state);
805fc1a5246Sthorpej
806e7512e5aSchristos (void)fprintf(ftrace, "\n%*s %19s%-16s ",
80752ff5d8fSmrg (int)strlen(label), "", "",
8086d8ef4dfSthorpej (rt->rt_gate != new->rts_gate
8096d8ef4dfSthorpej ? naddr_ntoa(new->rts_gate) : ""));
8106d8ef4dfSthorpej print_rts(new,
8116d8ef4dfSthorpej -(new->rts_metric == rt->rt_metric),
8126d8ef4dfSthorpej -(new->rts_ifp == rt->rt_ifp),
813e7512e5aSchristos 0,
8146d8ef4dfSthorpej rt->rt_tag != new->rts_tag,
8156d8ef4dfSthorpej (rt->rt_time != new->rts_time
8166d8ef4dfSthorpej && AGE_RT(rt->rt_state,new->rts_ifp)));
817fc1a5246Sthorpej if (rt->rt_state != state)
818fc1a5246Sthorpej trace_bits(rs_bits, state, 1);
819e7512e5aSchristos (void)fputc('\n',ftrace);
82061f28255Scgd }
821fc1a5246Sthorpej
82261f28255Scgd
823e072e2aeScgd void
trace_add_del(const char * action,struct rt_entry * rt)824756b1291Schristos trace_add_del(const char * action, struct rt_entry *rt)
82561f28255Scgd {
826fc1a5246Sthorpej if (ftrace == 0)
82761f28255Scgd return;
828fc1a5246Sthorpej
829fc1a5246Sthorpej lastlog();
830e7512e5aSchristos (void)fprintf(ftrace, "%s %-35s ",
831fc1a5246Sthorpej action,
832e7512e5aSchristos rtname(rt->rt_dst, rt->rt_mask, rt->rt_gate));
833e7512e5aSchristos print_rts(rt->rt_spares, 0,0,0,0,AGE_RT(rt->rt_state,rt->rt_ifp));
834e7512e5aSchristos trace_bits(rs_bits, rt->rt_state, 0);
835e7512e5aSchristos (void)fputc('\n',ftrace);
836fc1a5246Sthorpej }
837fc1a5246Sthorpej
838fc1a5246Sthorpej
8394d3fba59Schristos /* ARGSUSED */
8404d3fba59Schristos static int
walk_trace(struct radix_node * rn,struct walkarg * w UNUSED)8414d3fba59Schristos walk_trace(struct radix_node *rn,
842756b1291Schristos struct walkarg *w UNUSED)
8434d3fba59Schristos {
8444d3fba59Schristos #define RT ((struct rt_entry *)rn)
8454d3fba59Schristos struct rt_spare *rts;
846756b1291Schristos int i;
8474d3fba59Schristos
848e7512e5aSchristos (void)fprintf(ftrace, " %-35s ",
849e7512e5aSchristos rtname(RT->rt_dst, RT->rt_mask, RT->rt_gate));
850756b1291Schristos print_rts(&RT->rt_spares[0], 0,0,0,0, AGE_RT(RT->rt_state, RT->rt_ifp));
8514d3fba59Schristos trace_bits(rs_bits, RT->rt_state, 0);
852e7512e5aSchristos if (RT->rt_poison_time >= now_garbage
853e7512e5aSchristos && RT->rt_poison_metric < RT->rt_metric)
854e7512e5aSchristos (void)fprintf(ftrace, "pm=%d@%s",
855e7512e5aSchristos RT->rt_poison_metric, ts(RT->rt_poison_time));
8564d3fba59Schristos
8574d3fba59Schristos rts = &RT->rt_spares[1];
8584d3fba59Schristos for (i = 1; i < NUM_SPARES; i++, rts++) {
859e7512e5aSchristos if (rts->rts_gate != RIP_DEFAULT) {
860e7512e5aSchristos (void)fprintf(ftrace,"\n #%d%15s%-16s ",
861e7512e5aSchristos i, "", naddr_ntoa(rts->rts_gate));
862e7512e5aSchristos print_rts(rts, 0,0,0,0,1);
8634d3fba59Schristos }
8644d3fba59Schristos }
8654d3fba59Schristos (void)fputc('\n',ftrace);
8664d3fba59Schristos
8674d3fba59Schristos return 0;
8684d3fba59Schristos }
8694d3fba59Schristos
8704d3fba59Schristos
8714d3fba59Schristos static void
trace_dump(void)8724d3fba59Schristos trace_dump(void)
8734d3fba59Schristos {
874e7512e5aSchristos struct interface *ifp;
875e7512e5aSchristos
8764d3fba59Schristos if (ftrace == 0)
8774d3fba59Schristos return;
8784d3fba59Schristos lastlog();
8794d3fba59Schristos
880e7512e5aSchristos (void)fputs("current daemon state:\n", ftrace);
881e7512e5aSchristos for (ifp = ifnet; ifp != 0; ifp = ifp->int_next)
882e7512e5aSchristos trace_if("", ifp);
8834d3fba59Schristos (void)rn_walktree(rhead, walk_trace, 0);
8844d3fba59Schristos }
8854d3fba59Schristos
8864d3fba59Schristos
887fc1a5246Sthorpej void
trace_rip(const char * dir1,const char * dir2,struct sockaddr_in * who,struct interface * ifp,struct rip * msg,int size)888756b1291Schristos trace_rip(const char *dir1, const char *dir2,
889fc1a5246Sthorpej struct sockaddr_in *who,
890fc1a5246Sthorpej struct interface *ifp,
891fc1a5246Sthorpej struct rip *msg,
892fc1a5246Sthorpej int size) /* total size of message */
893fc1a5246Sthorpej {
894fc1a5246Sthorpej struct netinfo *n, *lim;
89594b2d428Schristos # define NA ((struct netauth*)n)
896e7512e5aSchristos int i, seen_route;
897fc1a5246Sthorpej
898fc1a5246Sthorpej if (!TRACEPACKETS || ftrace == 0)
899fc1a5246Sthorpej return;
900fc1a5246Sthorpej
901fc1a5246Sthorpej lastlog();
902fc1a5246Sthorpej if (msg->rip_cmd >= RIPCMD_MAX
903fc1a5246Sthorpej || msg->rip_vers == 0) {
9044d3fba59Schristos (void)fprintf(ftrace, "%s bad RIPv%d cmd=%d %s"
9054d3fba59Schristos " %s.%d size=%d\n",
906fc1a5246Sthorpej dir1, msg->rip_vers, msg->rip_cmd, dir2,
907fc1a5246Sthorpej naddr_ntoa(who->sin_addr.s_addr),
9084d3fba59Schristos ntohs(who->sin_port),
9094d3fba59Schristos size);
91061f28255Scgd return;
91161f28255Scgd }
912fc1a5246Sthorpej
913fc1a5246Sthorpej (void)fprintf(ftrace, "%s RIPv%d %s %s %s.%d%s%s\n",
914fc1a5246Sthorpej dir1, msg->rip_vers, ripcmds[msg->rip_cmd], dir2,
915fc1a5246Sthorpej naddr_ntoa(who->sin_addr.s_addr), ntohs(who->sin_port),
916fc1a5246Sthorpej ifp ? " via " : "", ifp ? ifp->int_name : "");
917fc1a5246Sthorpej if (!TRACECONTENTS)
91861f28255Scgd return;
919fc1a5246Sthorpej
920e7512e5aSchristos seen_route = 0;
92161f28255Scgd switch (msg->rip_cmd) {
92261f28255Scgd case RIPCMD_REQUEST:
92361f28255Scgd case RIPCMD_RESPONSE:
92461f28255Scgd n = msg->rip_nets;
925fc1a5246Sthorpej lim = (struct netinfo *)((char*)msg + size);
926fc1a5246Sthorpej for (; n < lim; n++) {
927e7512e5aSchristos if (!seen_route
928e7512e5aSchristos && n->n_family == RIP_AF_UNSPEC
929fc1a5246Sthorpej && ntohl(n->n_metric) == HOPCNT_INFINITY
930e7512e5aSchristos && msg->rip_cmd == RIPCMD_REQUEST
931e7512e5aSchristos && (n+1 == lim
932e7512e5aSchristos || (n+2 == lim
933e7512e5aSchristos && (n+1)->n_family == RIP_AF_AUTH))) {
934fc1a5246Sthorpej (void)fputs("\tQUERY ", ftrace);
935fc1a5246Sthorpej if (n->n_dst != 0)
936fc1a5246Sthorpej (void)fprintf(ftrace, "%s ",
937fc1a5246Sthorpej naddr_ntoa(n->n_dst));
938fc1a5246Sthorpej if (n->n_mask != 0)
939fc1a5246Sthorpej (void)fprintf(ftrace, "mask=%#x ",
9404d3fba59Schristos (u_int)ntohl(n->n_mask));
941fc1a5246Sthorpej if (n->n_nhop != 0)
942fc1a5246Sthorpej (void)fprintf(ftrace, "nhop=%s ",
943fc1a5246Sthorpej naddr_ntoa(n->n_nhop));
944fc1a5246Sthorpej if (n->n_tag != 0)
945fc1a5246Sthorpej (void)fprintf(ftrace, "tag=%#x ",
9464d3fba59Schristos ntohs(n->n_tag));
947fc1a5246Sthorpej (void)fputc('\n',ftrace);
948fc1a5246Sthorpej continue;
949fc1a5246Sthorpej }
950fc1a5246Sthorpej
951fc1a5246Sthorpej if (n->n_family == RIP_AF_AUTH) {
952e7512e5aSchristos if (NA->a_type == RIP_AUTH_PW
953e7512e5aSchristos && n == msg->rip_nets) {
954e7512e5aSchristos (void)fprintf(ftrace, "\tPassword"
955e7512e5aSchristos " Authentication:"
956e7512e5aSchristos " \"%s\"\n",
957e7512e5aSchristos qstring(NA->au.au_pw,
958e7512e5aSchristos RIP_AUTH_PW_LEN));
959e7512e5aSchristos continue;
960e7512e5aSchristos }
961e7512e5aSchristos
962e7512e5aSchristos if (NA->a_type == RIP_AUTH_MD5
963e7512e5aSchristos && n == msg->rip_nets) {
964fc1a5246Sthorpej (void)fprintf(ftrace,
96594b2d428Schristos "\tMD5 Auth"
96694b2d428Schristos " pkt_len=%d KeyID=%u"
96794b2d428Schristos " auth_len=%d"
96894b2d428Schristos " seqno=%#x"
969e7512e5aSchristos " rsvd=%#x,%#x\n",
97094b2d428Schristos ntohs(NA->au.a_md5.md5_pkt_len),
971e7512e5aSchristos NA->au.a_md5.md5_keyid,
97294b2d428Schristos NA->au.a_md5.md5_auth_len,
973a221b1baSchristos (int)ntohl(NA->au.a_md5.md5_seqno),
974a221b1baSchristos (int)ntohs(NA->au.a_md5.rsvd[0]),
975a221b1baSchristos (int)ntohs(NA->au.a_md5.rsvd[1]));
976e7512e5aSchristos continue;
977e7512e5aSchristos }
978e7512e5aSchristos (void)fprintf(ftrace,
97994b2d428Schristos "\tAuthentication type %d: ",
980e7512e5aSchristos ntohs(NA->a_type));
981fc1a5246Sthorpej for (i = 0;
982756b1291Schristos i < (int)sizeof(NA->au.au_pw);
983fc1a5246Sthorpej i++)
984fc1a5246Sthorpej (void)fprintf(ftrace, "%02x ",
985e7512e5aSchristos NA->au.au_pw[i]);
986fc1a5246Sthorpej (void)fputc('\n',ftrace);
987fc1a5246Sthorpej continue;
988fc1a5246Sthorpej }
989fc1a5246Sthorpej
990e7512e5aSchristos seen_route = 1;
991fc1a5246Sthorpej if (n->n_family != RIP_AF_INET) {
992fc1a5246Sthorpej (void)fprintf(ftrace,
993fc1a5246Sthorpej "\t(af %d) %-18s mask=%#x ",
994fc1a5246Sthorpej ntohs(n->n_family),
995fc1a5246Sthorpej naddr_ntoa(n->n_dst),
9964d3fba59Schristos (u_int)ntohl(n->n_mask));
997fc1a5246Sthorpej } else if (msg->rip_vers == RIPv1) {
998fc1a5246Sthorpej (void)fprintf(ftrace, "\t%-18s ",
999fc1a5246Sthorpej addrname(n->n_dst,
1000fc1a5246Sthorpej ntohl(n->n_mask),
1001fc1a5246Sthorpej n->n_mask==0 ? 2 : 1));
1002fc1a5246Sthorpej } else {
1003fc1a5246Sthorpej (void)fprintf(ftrace, "\t%-18s ",
1004fc1a5246Sthorpej addrname(n->n_dst,
1005fc1a5246Sthorpej ntohl(n->n_mask),
1006fc1a5246Sthorpej n->n_mask==0 ? 2 : 0));
1007fc1a5246Sthorpej }
1008fc1a5246Sthorpej (void)fprintf(ftrace, "metric=%-2d ",
10094d3fba59Schristos (u_int)ntohl(n->n_metric));
1010fc1a5246Sthorpej if (n->n_nhop != 0)
1011fc1a5246Sthorpej (void)fprintf(ftrace, " nhop=%s ",
1012fc1a5246Sthorpej naddr_ntoa(n->n_nhop));
1013fc1a5246Sthorpej if (n->n_tag != 0)
1014fc1a5246Sthorpej (void)fprintf(ftrace, "tag=%#x",
10154d3fba59Schristos ntohs(n->n_tag));
1016fc1a5246Sthorpej (void)fputc('\n',ftrace);
1017fc1a5246Sthorpej }
1018fc1a5246Sthorpej if (size != (char *)n - (char *)msg)
1019fc1a5246Sthorpej (void)fprintf(ftrace, "truncated record, len %d\n",
102061f28255Scgd size);
102161f28255Scgd break;
102261f28255Scgd
102361f28255Scgd case RIPCMD_TRACEON:
1024e7512e5aSchristos fprintf(ftrace, "\tfile=\"%.*s\"\n", size-4,
1025e7512e5aSchristos msg->rip_tracefile);
102661f28255Scgd break;
102761f28255Scgd
102861f28255Scgd case RIPCMD_TRACEOFF:
102961f28255Scgd break;
103061f28255Scgd }
103161f28255Scgd }
1032