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