2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1995 John Hay. All rights reserved.
7 * This file includes significant work done at Cornell University by
8 * Bill Nesheim. That work included by permission.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * $FreeBSD: src/usr.sbin/IPXrouted/trace.c,v 1.6.2.1 2000/07/20 10:35:22 kris Exp $
39 * $DragonFly: src/usr.sbin/IPXrouted/trace.c,v 1.6 2006/01/22 03:43:38 swildner Exp $
41 * @(#)trace.c 8.1 (Berkeley) 6/5/93
45 * Routing Table Management Daemon
51 #include <sys/types.h>
55 #define NRECORDS 50 /* size of circular trace buffer */
57 FILE *ftrace = stdout;
64 void dumpif(FILE *fd, struct interface *ifp);
65 void dumptrace(FILE *fd, char *dir, struct ifdebug *ifd);
67 static int iftraceinit(struct interface *, struct ifdebug *);
70 traceinit(struct interface *ifp)
72 if (iftraceinit(ifp, &ifp->int_input) &&
73 iftraceinit(ifp, &ifp->int_output))
76 syslog(LOG_ERR, "traceinit: can't init %s\n", ifp->int_name);
80 iftraceinit(struct interface *ifp, struct ifdebug *ifd)
85 (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
86 if (ifd->ifd_records == 0)
88 ifd->ifd_front = ifd->ifd_records;
90 for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
104 ftrace = fopen(file, "a");
107 dup2(fileno(ftrace), 1);
108 dup2(fileno(ftrace), 2);
124 trace(struct ifdebug *ifd, struct sockaddr *who, char *p, int len, int m)
128 if (ifd->ifd_records == 0)
130 t = ifd->ifd_front++;
131 if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
132 ifd->ifd_front = ifd->ifd_records;
133 if (ifd->ifd_count < NRECORDS)
135 if (t->ift_size > 0 && t->ift_packet)
138 t->ift_stamp = time(0);
141 t->ift_packet = malloc(len);
143 bcopy(p, t->ift_packet, len);
152 traceaction(FILE *fd, char *action, struct rt_entry *rt)
154 struct sockaddr_ipx *dst, *gate;
160 { RTF_GATEWAY, "GATEWAY" },
161 { RTF_HOST, "HOST" },
164 { RTS_PASSIVE, "PASSIVE" },
165 { RTS_REMOTE, "REMOTE" },
166 { RTS_INTERFACE,"INTERFACE" },
167 { RTS_CHANGED, "CHANGED" },
176 fprintf(fd, "%s ", action);
177 dst = (struct sockaddr_ipx *)&rt->rt_dst;
178 gate = (struct sockaddr_ipx *)&rt->rt_router;
179 fprintf(fd, "dst %s, ", ipxdp_ntoa(&dst->sipx_addr));
180 fprintf(fd, "router %s, metric %d, ticks %d, flags",
181 ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
183 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
184 if ((rt->rt_flags & p->t_bits) == 0)
186 fprintf(fd, cp, p->t_name);
192 fprintf(fd, " state");
194 for (first = 1, p = statebits; p->t_bits > 0; p++) {
195 if ((rt->rt_state & p->t_bits) == 0)
197 fprintf(fd, cp, p->t_name);
204 if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
205 dumpif(fd, rt->rt_ifp);
210 traceactionlog(char *action, struct rt_entry *rt)
212 struct sockaddr_ipx *dst, *gate;
218 { RTF_GATEWAY, "GATEWAY" },
219 { RTF_HOST, "HOST" },
222 { RTS_PASSIVE, "PASSIVE" },
223 { RTS_REMOTE, "REMOTE" },
224 { RTS_INTERFACE,"INTERFACE" },
225 { RTS_CHANGED, "CHANGED" },
233 dst = (struct sockaddr_ipx *)&rt->rt_dst;
234 gate = (struct sockaddr_ipx *)&rt->rt_router;
235 asprintf(&lstr, "%s dst %s,", action, ipxdp_ntoa(&dst->sipx_addr));
237 asprintf(&lstr, "%s router %s, metric %d, ticks %d, flags",
238 olstr, ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
242 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
243 if ((rt->rt_flags & p->t_bits) == 0)
245 asprintf(&lstr, cp, olstr, p->t_name);
253 asprintf(&lstr, "%s state", olstr);
257 for (first = 1, p = statebits; p->t_bits > 0; p++) {
258 if ((rt->rt_state & p->t_bits) == 0)
260 asprintf(&lstr, cp, olstr, p->t_name);
268 syslog(LOG_DEBUG, "%s", lstr);
273 tracesapactionlog(char *action, struct sap_entry *sap)
275 syslog(LOG_DEBUG, "%-12.12s service %04X %-20.20s "
276 "addr %s.%04X %c metric %d\n",
278 ntohs(sap->sap.ServType),
280 ipxdp_ntoa(&sap->sap.ipx),
281 ntohs(sap->sap.ipx.x_port),
282 (sap->clone ? 'C' : ' '),
283 ntohs(sap->sap.hops));
287 dumpif(FILE *fd, struct interface *ifp)
289 if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
290 fprintf(fd, "*** Packet history for interface %s ***\n",
292 dumptrace(fd, "to", &ifp->int_output);
293 dumptrace(fd, "from", &ifp->int_input);
294 fprintf(fd, "*** end packet history ***\n");
299 dumptrace(FILE *fd, char *dir, struct ifdebug *ifd)
304 cp = !strcmp(dir, "to") ? "Output" : "Input";
305 if (ifd->ifd_front == ifd->ifd_records &&
306 ifd->ifd_front->ift_size == 0) {
307 fprintf(fd, "%s: no packets.\n", cp);
310 fprintf(fd, "%s trace:\n", cp);
311 t = ifd->ifd_front - ifd->ifd_count;
312 if (t < ifd->ifd_records)
314 for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
315 if (t >= ifd->ifd_records + NRECORDS)
316 t = ifd->ifd_records;
317 if (t->ift_size == 0)
319 fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
321 dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
326 dumppacket(FILE *fd, char *dir, struct sockaddr *source, char *cp, int size)
328 struct rip *msg = (struct rip *)cp;
330 struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
332 if (msg->rip_cmd && ntohs(msg->rip_cmd) < RIPCMD_MAX)
333 fprintf(fd, "%s %s %s#%x", ripcmds[ntohs(msg->rip_cmd)],
334 dir, ipxdp_ntoa(&who->sipx_addr),
335 ntohs(who->sipx_addr.x_port));
337 fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->rip_cmd),
338 dir, ipxdp_ntoa(&who->sipx_addr),
339 ntohs(who->sipx_addr.x_port));
340 fprintf(fd, "size=%d cp=%x packet=%x\n", size,
341 (u_int)cp, (u_int)packet);
344 switch (ntohs(msg->rip_cmd)) {
347 case RIPCMD_RESPONSE:
349 size -= sizeof (u_short);
351 for (; size > 0; n++, size -= sizeof (struct netinfo)) {
352 if (size < sizeof (struct netinfo))
354 fprintf(fd, "\tnet %s metric %d ticks %d\n",
355 ipxdp_nettoa(n->rip_dst),
356 ntohs(n->rip_metric),
357 ntohs(n->rip_ticks));
365 dumpsappacket(FILE *fd, char *dir, struct sockaddr *source, char *cp, int size)
367 struct sap_packet *msg = (struct sap_packet *)cp;
369 struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
371 if (msg->sap_cmd && ntohs(msg->sap_cmd) < SAPCMD_MAX)
372 fprintf(fd, "%s %s %s#%x", sapcmds[ntohs(msg->sap_cmd)],
373 dir, ipxdp_ntoa(&who->sipx_addr),
374 ntohs(who->sipx_addr.x_port));
376 fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->sap_cmd),
377 dir, ipxdp_ntoa(&who->sipx_addr),
378 ntohs(who->sipx_addr.x_port));
379 fprintf(fd, "size=%d cp=%x packet=%x\n", size,
380 (u_int)cp, (u_int)packet);
383 switch (ntohs(msg->sap_cmd)) {
390 size -= sizeof (u_short);
392 for (; size > 0; n++, size -= sizeof (struct sap_info)) {
393 if (size < sizeof (struct sap_info))
395 fprintf(fd, " service %04X %-20.20s "
396 "addr %s.%04X metric %d\n",
400 ntohs(n->ipx.x_port),
409 dumpsaptable(FILE *fd, struct sap_hash *sh)
411 struct sap_entry *sap;
412 struct sap_hash *hash;
415 fprintf(fd, "------- SAP table dump. -------\n");
416 for (hash = sh; hash < &sh[SAPHASHSIZ]; hash++, x++) {
417 fprintf(fd, "HASH %d\n", x);
419 for (; sap != (struct sap_entry *)hash; sap = sap->forw) {
420 fprintf(fd, " service %04X %-20.20s "
421 "addr %s.%04X %c metric %d\n",
422 ntohs(sap->sap.ServType),
424 ipxdp_ntoa(&sap->sap.ipx),
425 ntohs(sap->sap.ipx.x_port),
426 (sap->clone ? 'C' : ' '),
427 ntohs(sap->sap.hops));
434 dumpriptable(FILE *fd)
436 struct rt_entry *rip;
439 struct rthash *rh = nethash;
441 fprintf(fd, "------- RIP table dump. -------\n");
443 fprintf(fd, "Network table.\n");
445 for (hash = rh; hash < &rh[ROUTEHASHSIZ]; hash++, x++) {
446 fprintf(fd, "HASH %d\n", x);
448 for (; rip != (struct rt_entry *)hash; rip = rip->rt_forw) {
449 fprintf(fd, " dest %s\t",
450 ipxdp_ntoa(&satoipx_addr(rip->rt_dst)));
451 fprintf(fd, "%s metric %d, ticks %d\n",
452 ipxdp_ntoa(&satoipx_addr(rip->rt_router)),
463 ipxdp_nettoa(union ipx_net val)
465 static char buf[100];
468 sprintf(buf, "%lx", ntohl(net.long_e));
474 ipxdp_ntoa(struct ipx_addr *addr)
476 static char buf[100];
478 sprintf(buf, "%s#%x:%x:%x:%x:%x:%x",
479 ipxdp_nettoa(addr->x_net),
480 addr->x_host.c_host[0], addr->x_host.c_host[1],
481 addr->x_host.c_host[2], addr->x_host.c_host[3],
482 addr->x_host.c_host[4], addr->x_host.c_host[5]);