1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
32*7c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California.
33*7c478bd9Sstevel@tonic-gate */
34*7c478bd9Sstevel@tonic-gate
35*7c478bd9Sstevel@tonic-gate /*
36*7c478bd9Sstevel@tonic-gate * Routing Table Management Daemon
37*7c478bd9Sstevel@tonic-gate */
38*7c478bd9Sstevel@tonic-gate #include "defs.h"
39*7c478bd9Sstevel@tonic-gate
40*7c478bd9Sstevel@tonic-gate #define NRECORDS 50 /* size of circular trace buffer */
41*7c478bd9Sstevel@tonic-gate
42*7c478bd9Sstevel@tonic-gate boolean_t tracepackets; /* watch packets as they go by */
43*7c478bd9Sstevel@tonic-gate int tracing; /* bitmask: */
44*7c478bd9Sstevel@tonic-gate FILE *ftrace; /* output trace file */
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate static int iftraceinit(struct interface *ifp, struct ifdebug *ifd);
47*7c478bd9Sstevel@tonic-gate static void dumpif(FILE *fp, struct interface *ifp);
48*7c478bd9Sstevel@tonic-gate static void dumptrace(FILE *fp, char *dir, struct ifdebug *ifd);
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate void
traceinit(struct interface * ifp)51*7c478bd9Sstevel@tonic-gate traceinit(struct interface *ifp)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate if (iftraceinit(ifp, &ifp->int_input) &&
54*7c478bd9Sstevel@tonic-gate iftraceinit(ifp, &ifp->int_output))
55*7c478bd9Sstevel@tonic-gate return;
56*7c478bd9Sstevel@tonic-gate tracing = 0;
57*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "traceinit: can't init %s\n",
58*7c478bd9Sstevel@tonic-gate (ifp->int_name != NULL) ? ifp->int_name : "(noname)");
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate static int
iftraceinit(struct interface * ifp,struct ifdebug * ifd)62*7c478bd9Sstevel@tonic-gate iftraceinit(struct interface *ifp, struct ifdebug *ifd)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate struct iftrace *t;
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate ifd->ifd_records = (struct iftrace *)
67*7c478bd9Sstevel@tonic-gate malloc((size_t)NRECORDS * sizeof (struct iftrace));
68*7c478bd9Sstevel@tonic-gate if (ifd->ifd_records == NULL)
69*7c478bd9Sstevel@tonic-gate return (0);
70*7c478bd9Sstevel@tonic-gate ifd->ifd_front = ifd->ifd_records;
71*7c478bd9Sstevel@tonic-gate ifd->ifd_count = 0;
72*7c478bd9Sstevel@tonic-gate for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
73*7c478bd9Sstevel@tonic-gate t->ift_size = 0;
74*7c478bd9Sstevel@tonic-gate t->ift_packet = NULL;
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate ifd->ifd_if = ifp;
77*7c478bd9Sstevel@tonic-gate return (1);
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate void
traceon(char * file)81*7c478bd9Sstevel@tonic-gate traceon(char *file)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate struct stat stbuf;
84*7c478bd9Sstevel@tonic-gate
85*7c478bd9Sstevel@tonic-gate if (ftrace != NULL)
86*7c478bd9Sstevel@tonic-gate return;
87*7c478bd9Sstevel@tonic-gate if (stat(file, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) != S_IFREG)
88*7c478bd9Sstevel@tonic-gate return;
89*7c478bd9Sstevel@tonic-gate ftrace = fopen(file, "a");
90*7c478bd9Sstevel@tonic-gate if (ftrace == NULL)
91*7c478bd9Sstevel@tonic-gate return;
92*7c478bd9Sstevel@tonic-gate (void) dup2(fileno(ftrace), 1);
93*7c478bd9Sstevel@tonic-gate (void) dup2(fileno(ftrace), 2);
94*7c478bd9Sstevel@tonic-gate }
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gate void
traceonfp(FILE * fp)97*7c478bd9Sstevel@tonic-gate traceonfp(FILE *fp)
98*7c478bd9Sstevel@tonic-gate {
99*7c478bd9Sstevel@tonic-gate if (ftrace != NULL)
100*7c478bd9Sstevel@tonic-gate return;
101*7c478bd9Sstevel@tonic-gate ftrace = fp;
102*7c478bd9Sstevel@tonic-gate if (ftrace == NULL)
103*7c478bd9Sstevel@tonic-gate return;
104*7c478bd9Sstevel@tonic-gate (void) dup2(fileno(ftrace), 1);
105*7c478bd9Sstevel@tonic-gate (void) dup2(fileno(ftrace), 2);
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate
108*7c478bd9Sstevel@tonic-gate void
trace(struct ifdebug * ifd,struct sockaddr_in6 * who,char * p,int len,int m)109*7c478bd9Sstevel@tonic-gate trace(struct ifdebug *ifd, struct sockaddr_in6 *who, char *p, int len, int m)
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate struct iftrace *t;
112*7c478bd9Sstevel@tonic-gate
113*7c478bd9Sstevel@tonic-gate if (ifd->ifd_records == 0)
114*7c478bd9Sstevel@tonic-gate return;
115*7c478bd9Sstevel@tonic-gate t = ifd->ifd_front++;
116*7c478bd9Sstevel@tonic-gate if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
117*7c478bd9Sstevel@tonic-gate ifd->ifd_front = ifd->ifd_records;
118*7c478bd9Sstevel@tonic-gate if (ifd->ifd_count < NRECORDS)
119*7c478bd9Sstevel@tonic-gate ifd->ifd_count++;
120*7c478bd9Sstevel@tonic-gate if (t->ift_size > 0 && t->ift_size < len && t->ift_packet != NULL) {
121*7c478bd9Sstevel@tonic-gate free(t->ift_packet);
122*7c478bd9Sstevel@tonic-gate t->ift_packet = NULL;
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate (void) time(&t->ift_stamp);
125*7c478bd9Sstevel@tonic-gate t->ift_who = *who;
126*7c478bd9Sstevel@tonic-gate if (len > 0 && t->ift_packet == NULL) {
127*7c478bd9Sstevel@tonic-gate t->ift_packet = (char *)malloc((size_t)len);
128*7c478bd9Sstevel@tonic-gate if (t->ift_packet == NULL)
129*7c478bd9Sstevel@tonic-gate len = 0;
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate if (len > 0)
132*7c478bd9Sstevel@tonic-gate bcopy(p, t->ift_packet, len);
133*7c478bd9Sstevel@tonic-gate t->ift_size = len;
134*7c478bd9Sstevel@tonic-gate t->ift_metric = m;
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate void
traceaction(FILE * fp,char * action,struct rt_entry * rt)138*7c478bd9Sstevel@tonic-gate traceaction(FILE *fp, char *action, struct rt_entry *rt)
139*7c478bd9Sstevel@tonic-gate {
140*7c478bd9Sstevel@tonic-gate static struct bits {
141*7c478bd9Sstevel@tonic-gate ulong_t t_bits;
142*7c478bd9Sstevel@tonic-gate char *t_name;
143*7c478bd9Sstevel@tonic-gate } flagbits[] = {
144*7c478bd9Sstevel@tonic-gate /* BEGIN CSTYLED */
145*7c478bd9Sstevel@tonic-gate { RTF_UP, "UP" },
146*7c478bd9Sstevel@tonic-gate { RTF_GATEWAY, "GATEWAY" },
147*7c478bd9Sstevel@tonic-gate { RTF_HOST, "HOST" },
148*7c478bd9Sstevel@tonic-gate { 0, NULL }
149*7c478bd9Sstevel@tonic-gate /* END CSTYLED */
150*7c478bd9Sstevel@tonic-gate }, statebits[] = {
151*7c478bd9Sstevel@tonic-gate /* BEGIN CSTYLED */
152*7c478bd9Sstevel@tonic-gate { RTS_INTERFACE, "INTERFACE" },
153*7c478bd9Sstevel@tonic-gate { RTS_CHANGED, "CHANGED" },
154*7c478bd9Sstevel@tonic-gate { RTS_PRIVATE, "PRIVATE" },
155*7c478bd9Sstevel@tonic-gate { 0, NULL }
156*7c478bd9Sstevel@tonic-gate /* END CSTYLED */
157*7c478bd9Sstevel@tonic-gate };
158*7c478bd9Sstevel@tonic-gate struct bits *p;
159*7c478bd9Sstevel@tonic-gate boolean_t first;
160*7c478bd9Sstevel@tonic-gate char c;
161*7c478bd9Sstevel@tonic-gate time_t t;
162*7c478bd9Sstevel@tonic-gate
163*7c478bd9Sstevel@tonic-gate if (fp == NULL)
164*7c478bd9Sstevel@tonic-gate return;
165*7c478bd9Sstevel@tonic-gate (void) time(&t);
166*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%.15s %s ", ctime(&t) + 4, action);
167*7c478bd9Sstevel@tonic-gate if (rt != NULL) {
168*7c478bd9Sstevel@tonic-gate char buf1[INET6_ADDRSTRLEN];
169*7c478bd9Sstevel@tonic-gate
170*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "prefix %s/%d ",
171*7c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&rt->rt_dst, buf1,
172*7c478bd9Sstevel@tonic-gate sizeof (buf1)),
173*7c478bd9Sstevel@tonic-gate rt->rt_prefix_length);
174*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "via %s metric %d",
175*7c478bd9Sstevel@tonic-gate inet_ntop(AF_INET6, (void *)&rt->rt_router, buf1,
176*7c478bd9Sstevel@tonic-gate sizeof (buf1)),
177*7c478bd9Sstevel@tonic-gate rt->rt_metric);
178*7c478bd9Sstevel@tonic-gate if (rt->rt_ifp != NULL) {
179*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " if %s",
180*7c478bd9Sstevel@tonic-gate (rt->rt_ifp->int_name != NULL) ?
181*7c478bd9Sstevel@tonic-gate rt->rt_ifp->int_name : "(noname)");
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " state");
184*7c478bd9Sstevel@tonic-gate c = ' ';
185*7c478bd9Sstevel@tonic-gate for (first = _B_TRUE, p = statebits; p->t_bits > 0; p++) {
186*7c478bd9Sstevel@tonic-gate if ((rt->rt_state & p->t_bits) == 0)
187*7c478bd9Sstevel@tonic-gate continue;
188*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%c%s", c, p->t_name);
189*7c478bd9Sstevel@tonic-gate if (first) {
190*7c478bd9Sstevel@tonic-gate c = '|';
191*7c478bd9Sstevel@tonic-gate first = _B_FALSE;
192*7c478bd9Sstevel@tonic-gate }
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate if (first)
195*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, " 0");
196*7c478bd9Sstevel@tonic-gate if (rt->rt_flags & (RTF_UP | RTF_GATEWAY)) {
197*7c478bd9Sstevel@tonic-gate c = ' ';
198*7c478bd9Sstevel@tonic-gate for (first = _B_TRUE, p = flagbits; p->t_bits > 0;
199*7c478bd9Sstevel@tonic-gate p++) {
200*7c478bd9Sstevel@tonic-gate if ((rt->rt_flags & p->t_bits) == 0)
201*7c478bd9Sstevel@tonic-gate continue;
202*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%c%s", c, p->t_name);
203*7c478bd9Sstevel@tonic-gate if (first) {
204*7c478bd9Sstevel@tonic-gate c = '|';
205*7c478bd9Sstevel@tonic-gate first = _B_FALSE;
206*7c478bd9Sstevel@tonic-gate }
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate (void) putc('\n', fp);
211*7c478bd9Sstevel@tonic-gate if (!tracepackets && rt != NULL && rt->rt_ifp != NULL)
212*7c478bd9Sstevel@tonic-gate dumpif(fp, rt->rt_ifp);
213*7c478bd9Sstevel@tonic-gate (void) fflush(fp);
214*7c478bd9Sstevel@tonic-gate }
215*7c478bd9Sstevel@tonic-gate
216*7c478bd9Sstevel@tonic-gate static void
dumpif(FILE * fp,struct interface * ifp)217*7c478bd9Sstevel@tonic-gate dumpif(FILE *fp, struct interface *ifp)
218*7c478bd9Sstevel@tonic-gate {
219*7c478bd9Sstevel@tonic-gate if (ifp->int_input.ifd_count != 0 || ifp->int_output.ifd_count != 0) {
220*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "*** Packet history for interface %s ***\n",
221*7c478bd9Sstevel@tonic-gate (ifp->int_name != NULL) ? ifp->int_name : "(noname)");
222*7c478bd9Sstevel@tonic-gate dumptrace(fp, "to", &ifp->int_output);
223*7c478bd9Sstevel@tonic-gate dumptrace(fp, "from", &ifp->int_input);
224*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "*** end packet history ***\n");
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate (void) fflush(fp);
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate
229*7c478bd9Sstevel@tonic-gate static void
dumptrace(FILE * fp,char * dir,struct ifdebug * ifd)230*7c478bd9Sstevel@tonic-gate dumptrace(FILE *fp, char *dir, struct ifdebug *ifd)
231*7c478bd9Sstevel@tonic-gate {
232*7c478bd9Sstevel@tonic-gate struct iftrace *t;
233*7c478bd9Sstevel@tonic-gate char *cp = (strcmp(dir, "to") != 0) ? "Output" : "Input";
234*7c478bd9Sstevel@tonic-gate
235*7c478bd9Sstevel@tonic-gate if (ifd->ifd_front == ifd->ifd_records &&
236*7c478bd9Sstevel@tonic-gate ifd->ifd_front->ift_size == 0) {
237*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%s: no packets.\n", cp);
238*7c478bd9Sstevel@tonic-gate (void) fflush(fp);
239*7c478bd9Sstevel@tonic-gate return;
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%s trace:\n", cp);
242*7c478bd9Sstevel@tonic-gate t = ifd->ifd_front - ifd->ifd_count;
243*7c478bd9Sstevel@tonic-gate if (t < ifd->ifd_records)
244*7c478bd9Sstevel@tonic-gate t += NRECORDS;
245*7c478bd9Sstevel@tonic-gate for (; ifd->ifd_count; ifd->ifd_count--, t++) {
246*7c478bd9Sstevel@tonic-gate if (t >= ifd->ifd_records + NRECORDS)
247*7c478bd9Sstevel@tonic-gate t = ifd->ifd_records;
248*7c478bd9Sstevel@tonic-gate if (t->ift_size == 0)
249*7c478bd9Sstevel@tonic-gate continue;
250*7c478bd9Sstevel@tonic-gate (void) fprintf(fp, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
251*7c478bd9Sstevel@tonic-gate t->ift_metric);
252*7c478bd9Sstevel@tonic-gate dumppacket(fp, dir, (struct sockaddr_in6 *)&t->ift_who,
253*7c478bd9Sstevel@tonic-gate t->ift_packet, t->ift_size);
254*7c478bd9Sstevel@tonic-gate }
255*7c478bd9Sstevel@tonic-gate }
256*7c478bd9Sstevel@tonic-gate
257*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
258*7c478bd9Sstevel@tonic-gate void
dumppacket(FILE * fp,char * dir,struct sockaddr_in6 * who,char * cp,int size)259*7c478bd9Sstevel@tonic-gate dumppacket(FILE *fp, char *dir, struct sockaddr_in6 *who, char *cp, int size)
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate /* XXX Output contents of the RIP packet */
262*7c478bd9Sstevel@tonic-gate }
263