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