summaryrefslogtreecommitdiffstats
path: root/usr.sbin/IPXrouted/trace.c
blob: d9e74b84fc50dfac3b4c47bcbc85252a628de825 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
 * Copyright (c) 1985, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Copyright (c) 1995 John Hay.  All rights reserved.
 *
 * This file includes significant work done at Cornell University by
 * Bill Nesheim.  That work included by permission.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
#if 0
static char sccsid[] = "@(#)trace.c	8.1 (Berkeley) 6/5/93";
#endif
static const char rcsid[] =
  "$FreeBSD$";
#endif /* not lint */

/*
 * Routing Table Management Daemon
 */
#define	RIPCMDS
#define	SAPCMDS
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include "defs.h"

#define	NRECORDS	50		/* size of circular trace buffer */
#ifdef DEBUG
FILE	*ftrace = stdout;
int	tracing = 1;
#else /* DEBUG */
FILE	*ftrace = NULL;
int	tracing = 0;
#endif

void dumpif(FILE *fd, struct interface *ifp);
void dumptrace(FILE *fd, char *dir, struct ifdebug *ifd);
static int iftraceinit(struct interface *ifp, struct ifdebug *ifd);

void
traceinit(ifp)
	register struct interface *ifp;
{
	if (iftraceinit(ifp, &ifp->int_input) &&
	    iftraceinit(ifp, &ifp->int_output))
		return;
	tracing = 0;
	syslog(LOG_ERR, "traceinit: can't init %s\n", ifp->int_name);
}

static int
iftraceinit(ifp, ifd)
	struct interface *ifp;
	register struct ifdebug *ifd;
{
	register struct iftrace *t;

	ifd->ifd_records =
	  (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
	if (ifd->ifd_records == 0)
		return (0);
	ifd->ifd_front = ifd->ifd_records;
	ifd->ifd_count = 0;
	for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
		t->ift_size = 0;
		t->ift_packet = 0;
	}
	ifd->ifd_if = ifp;
	return (1);
}

void
traceon(file)
	char *file;
{

	if (ftrace != NULL)
		return;
	ftrace = fopen(file, "a");
	if (ftrace == NULL)
		return;
	dup2(fileno(ftrace), 1);
	dup2(fileno(ftrace), 2);
	tracing = 1;
}

void
traceoff(void)
{
	if (!tracing)
		return;
	if (ftrace != NULL)
		fclose(ftrace);
	ftrace = NULL;
	tracing = 0;
}

void
trace(ifd, who, p, len, m)
	register struct ifdebug *ifd;
	struct sockaddr *who;
	char *p;
	int len, m;
{
	register struct iftrace *t;

	if (ifd->ifd_records == 0)
		return;
	t = ifd->ifd_front++;
	if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
		ifd->ifd_front = ifd->ifd_records;
	if (ifd->ifd_count < NRECORDS)
		ifd->ifd_count++;
	if (t->ift_size > 0 && t->ift_packet)
		free(t->ift_packet);
	t->ift_packet = 0;
	t->ift_stamp = time(0);
	t->ift_who = *who;
	if (len > 0) {
		t->ift_packet = malloc(len);
		if (t->ift_packet)
			bcopy(p, t->ift_packet, len);
		else
			len = 0;
	}
	t->ift_size = len;
	t->ift_metric = m;
}

void
traceaction(fd, action, rt)
	FILE *fd;
	char *action;
	struct rt_entry *rt;
{
	struct sockaddr_ipx *dst, *gate;
	static struct bits {
		int	t_bits;
		char	*t_name;
	} flagbits[] = {
		{ RTF_UP,	"UP" },
		{ RTF_GATEWAY,	"GATEWAY" },
		{ RTF_HOST,	"HOST" },
		{ 0 }
	}, statebits[] = {
		{ RTS_PASSIVE,	"PASSIVE" },
		{ RTS_REMOTE,	"REMOTE" },
		{ RTS_INTERFACE,"INTERFACE" },
		{ RTS_CHANGED,	"CHANGED" },
		{ 0 }
	};
	register struct bits *p;
	register int first;
	char *cp;

	if (fd == NULL)
		return;
	fprintf(fd, "%s ", action);
	dst = (struct sockaddr_ipx *)&rt->rt_dst;
	gate = (struct sockaddr_ipx *)&rt->rt_router;
	fprintf(fd, "dst %s, ", ipxdp_ntoa(&dst->sipx_addr));
	fprintf(fd, "router %s, metric %d, ticks %d, flags",
	     ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
	cp = " %s";
	for (first = 1, p = flagbits; p->t_bits > 0; p++) {
		if ((rt->rt_flags & p->t_bits) == 0)
			continue;
		fprintf(fd, cp, p->t_name);
		if (first) {
			cp = "|%s";
			first = 0;
		}
	}
	fprintf(fd, " state");
	cp = " %s";
	for (first = 1, p = statebits; p->t_bits > 0; p++) {
		if ((rt->rt_state & p->t_bits) == 0)
			continue;
		fprintf(fd, cp, p->t_name);
		if (first) {
			cp = "|%s";
			first = 0;
		}
	}
	putc('\n', fd);
	if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
		dumpif(fd, rt->rt_ifp);
	fflush(fd);
}

void
traceactionlog(action, rt)
	char *action;
	struct rt_entry *rt;
{
	struct sockaddr_ipx *dst, *gate;
	static struct bits {
		int	t_bits;
		char	*t_name;
	} flagbits[] = {
		{ RTF_UP,	"UP" },
		{ RTF_GATEWAY,	"GATEWAY" },
		{ RTF_HOST,	"HOST" },
		{ 0 }
	}, statebits[] = {
		{ RTS_PASSIVE,	"PASSIVE" },
		{ RTS_REMOTE,	"REMOTE" },
		{ RTS_INTERFACE,"INTERFACE" },
		{ RTS_CHANGED,	"CHANGED" },
		{ 0 }
	};
	register struct bits *p;
	register int first;
	char *cp;
	char *lstr, *olstr;

	dst = (struct sockaddr_ipx *)&rt->rt_dst;
	gate = (struct sockaddr_ipx *)&rt->rt_router;
	asprintf(&lstr, "%s dst %s,", action, ipxdp_ntoa(&dst->sipx_addr));
	olstr = lstr;
	asprintf(&lstr, "%s router %s, metric %d, ticks %d, flags",
	     olstr, ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
	free(olstr);
	olstr = lstr;
	cp = "%s %s";
	for (first = 1, p = flagbits; p->t_bits > 0; p++) {
		if ((rt->rt_flags & p->t_bits) == 0)
			continue;
		asprintf(&lstr, cp, olstr, p->t_name);
		free(olstr);
		olstr = lstr;
		if (first) {
			cp = "%s|%s";
			first = 0;
		}
	}
	asprintf(&lstr, "%s state", olstr);
	free(olstr);
	olstr = lstr;
	cp = "%s %s";
	for (first = 1, p = statebits; p->t_bits > 0; p++) {
		if ((rt->rt_state & p->t_bits) == 0)
			continue;
		asprintf(&lstr, cp, olstr, p->t_name);
		free(olstr);
		olstr = lstr;
		if (first) {
			cp = "%s|%s";
			first = 0;
		}
	}
	syslog(LOG_DEBUG, "%s", lstr);
	free(lstr);
}

void
tracesapactionlog(action, sap)
	char *action;
	struct sap_entry *sap;
{
	syslog(LOG_DEBUG, "%-12.12s  service %04X %-20.20s "
		    "addr %s.%04X %c metric %d\n",
		     action,
		     ntohs(sap->sap.ServType),
		     sap->sap.ServName,
		     ipxdp_ntoa(&sap->sap.ipx),
		     ntohs(sap->sap.ipx.x_port),
		     (sap->clone ? 'C' : ' '),
		     ntohs(sap->sap.hops));
}

void
dumpif(fd, ifp)
	register struct interface *ifp;
	FILE *fd;
{
	if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
		fprintf(fd, "*** Packet history for interface %s ***\n",
			ifp->int_name);
		dumptrace(fd, "to", &ifp->int_output);
		dumptrace(fd, "from", &ifp->int_input);
		fprintf(fd, "*** end packet history ***\n");
	}
}

void
dumptrace(fd, dir, ifd)
	FILE *fd;
	char *dir;
	register struct ifdebug *ifd;
{
	register struct iftrace *t;
	char *cp = !strcmp(dir, "to") ? "Output" : "Input";

	if (ifd->ifd_front == ifd->ifd_records &&
	    ifd->ifd_front->ift_size == 0) {
		fprintf(fd, "%s: no packets.\n", cp);
		return;
	}
	fprintf(fd, "%s trace:\n", cp);
	t = ifd->ifd_front - ifd->ifd_count;
	if (t < ifd->ifd_records)
		t += NRECORDS;
	for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
		if (t >= ifd->ifd_records + NRECORDS)
			t = ifd->ifd_records;
		if (t->ift_size == 0)
			continue;
		fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
			t->ift_metric);
		dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
	}
}

void
dumppacket(fd, dir, source, cp, size)
	FILE *fd;
	char *dir;
	struct sockaddr *source;
	char *cp;
	register int size;
{
	register struct rip *msg = (struct rip *)cp;
	register struct netinfo *n;
	struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;

	if (msg->rip_cmd && ntohs(msg->rip_cmd) < RIPCMD_MAX)
		fprintf(fd, "%s %s %s#%x", ripcmds[ntohs(msg->rip_cmd)],
		    dir, ipxdp_ntoa(&who->sipx_addr), 
		    ntohs(who->sipx_addr.x_port));
	else {
		fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->rip_cmd),
		    dir, ipxdp_ntoa(&who->sipx_addr), 
		    ntohs(who->sipx_addr.x_port));
		fprintf(fd, "size=%d cp=%p packet=%p\n", size, 
			cp, packet);
		return;
	}
	switch (ntohs(msg->rip_cmd)) {

	case RIPCMD_REQUEST:
	case RIPCMD_RESPONSE:
		fprintf(fd, ":\n");
		size -= sizeof (u_short);
		n = msg->rip_nets;
		for (; size > 0; n++, size -= sizeof (struct netinfo)) {
			if (size < sizeof (struct netinfo))
				break;
			fprintf(fd, "\tnet %s metric %d ticks %d\n",
			     ipxdp_nettoa(n->rip_dst),
			     ntohs(n->rip_metric),
			     ntohs(n->rip_ticks));
		}
		break;

	}
}

void
dumpsappacket(fd, dir, source, cp, size)
	FILE *fd;
	char *dir;
	struct sockaddr *source;
	char *cp;
	register int size;
{
	register struct sap_packet *msg = (struct sap_packet *)cp;
	register struct sap_info *n;
	struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;

	if (msg->sap_cmd && ntohs(msg->sap_cmd) < SAPCMD_MAX)
		fprintf(fd, "%s %s %s#%x", sapcmds[ntohs(msg->sap_cmd)],
		    dir, ipxdp_ntoa(&who->sipx_addr), 
		    ntohs(who->sipx_addr.x_port));
	else {
		fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->sap_cmd),
		    dir, ipxdp_ntoa(&who->sipx_addr), 
		    ntohs(who->sipx_addr.x_port));
		fprintf(fd, "size=%d cp=%p packet=%p\n", size, 
			cp, packet);
		return;
	}
	switch (ntohs(msg->sap_cmd)) {

	case SAP_REQ:
	case SAP_RESP:
	case SAP_REQ_NEAR:
	case SAP_RESP_NEAR:
		fprintf(fd, ":\n");
		size -= sizeof (u_short);
		n = msg->sap;
		for (; size > 0; n++, size -= sizeof (struct sap_info)) {
			if (size < sizeof (struct sap_info))
				break;
			fprintf(fd, "  service %04X %-20.20s "
				    "addr %s.%04X metric %d\n",
			     ntohs(n->ServType),
			     n->ServName,
			     ipxdp_ntoa(&n->ipx),
			     ntohs(n->ipx.x_port),
			     ntohs(n->hops));
		}
		break;

	}
}

void
dumpsaptable(fd, sh)
	FILE *fd;
	struct sap_hash *sh;
{
	register struct sap_entry *sap;
	struct sap_hash *hash;
	int x = 0;

	fprintf(fd, "------- SAP table dump. -------\n");
	for (hash = sh; hash < &sh[SAPHASHSIZ]; hash++, x++) {
		fprintf(fd, "HASH %d\n", x);
		sap = hash->forw;
		for (; sap != (struct sap_entry *)hash; sap = sap->forw) {
			fprintf(fd, "  service %04X %-20.20s "
				    "addr %s.%04X %c metric %d\n",
				     ntohs(sap->sap.ServType),
				     sap->sap.ServName,
				     ipxdp_ntoa(&sap->sap.ipx),
				     ntohs(sap->sap.ipx.x_port),
				     (sap->clone ? 'C' : ' '),
				     ntohs(sap->sap.hops));
		}
	}
	fprintf(fd, "\n");
}

void
dumpriptable(fd)
	FILE *fd;
{
	register struct rt_entry *rip;
	struct rthash *hash;
	int x;
	struct rthash *rh = nethash;

	fprintf(fd, "------- RIP table dump. -------\n");
	x = 0;
	fprintf(fd, "Network table.\n");

	for (hash = rh; hash < &rh[ROUTEHASHSIZ]; hash++, x++) {
		fprintf(fd, "HASH %d\n", x);
		rip = hash->rt_forw;
		for (; rip != (struct rt_entry *)hash; rip = rip->rt_forw) {
			fprintf(fd, "  dest %s\t", 
				ipxdp_ntoa(&satoipx_addr(rip->rt_dst)));
			fprintf(fd, "%s metric %d, ticks %d\n",
				ipxdp_ntoa(&satoipx_addr(rip->rt_router)),
				rip->rt_metric,
				rip->rt_ticks);
		}
	}
	fprintf(fd, "\n");
}

union ipx_net_u net;

char *
ipxdp_nettoa(val)
union ipx_net val;
{
	static char buf[100];
	net.net_e = val;
	(void)sprintf(buf, "%u", ntohl(net.long_e));
	return (buf);
}


char *
ipxdp_ntoa(addr)
struct ipx_addr *addr;
{
    static char buf[100];

    (void)sprintf(buf, "%s#%x:%x:%x:%x:%x:%x",
	ipxdp_nettoa(addr->x_net),
	addr->x_host.c_host[0], addr->x_host.c_host[1], 
	addr->x_host.c_host[2], addr->x_host.c_host[3], 
	addr->x_host.c_host[4], addr->x_host.c_host[5]);
	
    return(buf);
}
OpenPOWER on IntegriCloud