]> git.saurik.com Git - apple/network_cmds.git/blame - trpt.tproj/trpt.c
network_cmds-245.1.3.tar.gz
[apple/network_cmds.git] / trpt.tproj / trpt.c
CommitLineData
b7080c8e
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
ffda1f4a
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
b7080c8e
A
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
ffda1f4a
A
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
b7080c8e
A
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * Copyright (c) 1983, 1988, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56#ifndef lint
57static char copyright[] =
58"@(#) Copyright (c) 1983, 1988, 1993\n\
59 The Regents of the University of California. All rights reserved.\n";
60#endif /* not lint */
61
62#ifndef lint
63static char sccsid[] = "@(#)trpt.c 8.1 (Berkeley) 6/6/93";
64#endif /* not lint */
65
66#include <sys/param.h>
67#if BSD >= 199103
68#define NEWVM
69#endif
70#ifndef NEWVM
71#include <machine/pte.h>
72#include <sys/vmmac.h>
73#endif
74#include <sys/socket.h>
75#include <sys/socketvar.h>
76#define PRUREQUESTS
77#include <sys/protosw.h>
78#include <sys/file.h>
79
80#include <net/route.h>
81#include <net/if.h>
82
83#include <netinet/in.h>
84#include <netinet/in_systm.h>
85#include <netinet/ip.h>
86#include <netinet/in_pcb.h>
87#include <netinet/ip_var.h>
88#include <netinet/tcp.h>
89#define TCPSTATES
90#include <netinet/tcp_fsm.h>
91#include <netinet/tcp_seq.h>
92#define TCPTIMERS
93#include <netinet/tcp_timer.h>
94#include <netinet/tcp_var.h>
95#include <netinet/tcpip.h>
96#define TANAMES
97#include <netinet/tcp_debug.h>
98
99#include <arpa/inet.h>
100
101#include <stdio.h>
102#include <errno.h>
103#include <nlist.h>
104#include <paths.h>
105
106struct tcp_debug tcp_debug[TCP_NDEBUG];
107int tcp_debx;
108
109struct nlist nl[] = {
110#define N_TCP_DEBUG 0
111 { "_tcp_debug" },
112#define N_TCP_DEBX 1
113 { "_tcp_debx" },
114#ifndef NEWVM
115#define N_SYSMAP 2
116 { "_Sysmap" },
117#define N_SYSSIZE 3
118 { "_Syssize" },
119#endif
120 { "" },
121};
122
123#ifndef NEWVM
124static struct pte *Sysmap;
125#endif
126static caddr_t tcp_pcbs[TCP_NDEBUG];
127static n_time ntime;
128static int aflag, kflag, memf, follow, sflag, tflag;
129
130main(argc, argv)
131 int argc;
132 char **argv;
133{
134 extern char *optarg;
135 extern int optind;
136 int ch, i, jflag, npcbs, numeric();
137 char *system, *core, *malloc();
138 off_t lseek();
139
140 jflag = npcbs = 0;
141 while ((ch = getopt(argc, argv, "afjp:st")) != EOF)
142 switch (ch) {
143 case 'a':
144 ++aflag;
145 break;
146 case 'f':
147 ++follow;
148 setlinebuf(stdout);
149 break;
150 case 'j':
151 ++jflag;
152 break;
153 case 'p':
154 if (npcbs >= TCP_NDEBUG) {
155 fputs("trpt: too many pcb's specified\n",
156 stderr);
157 exit(1);
158 }
159 (void)sscanf(optarg, "%x", (int *)&tcp_pcbs[npcbs++]);
160 break;
161 case 's':
162 ++sflag;
163 break;
164 case 't':
165 ++tflag;
166 break;
167 case '?':
168 default:
169 (void)fprintf(stderr,
170"usage: trpt [-afjst] [-p hex-address] [system [core]]\n");
171 exit(1);
172 }
173 argc -= optind;
174 argv += optind;
175
176 core = _PATH_KMEM;
177 if (argc > 0) {
178 system = *argv;
179 argc--, argv++;
180 if (argc > 0) {
181 core = *argv;
182 argc--, argv++;
183 ++kflag;
184 }
185 /*
186 * Discard setgid privileges if not the running kernel so that
187 * bad guys can't print interesting stuff from kernel memory.
188 */
189 setgid(getgid());
190 }
191 else
192 system = _PATH_UNIX;
193
194 if (nlist(system, nl) < 0 || !nl[0].n_value) {
195 fprintf(stderr, "trpt: %s: no namelist\n", system);
196 exit(1);
197 }
198 if ((memf = open(core, O_RDONLY)) < 0) {
199 perror(core);
200 exit(2);
201 }
202 if (kflag) {
203#ifdef NEWVM
204 fputs("trpt: can't do core files yet\n", stderr);
205 exit(1);
206#else
207 off_t off;
208
209 Sysmap = (struct pte *)
210 malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
211 if (!Sysmap) {
212 fputs("trpt: can't get memory for Sysmap.\n", stderr);
213 exit(1);
214 }
215 off = nl[N_SYSMAP].n_value & ~KERNBASE;
216 (void)lseek(memf, off, L_SET);
217 (void)read(memf, (char *)Sysmap,
218 (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
219#endif
220 }
221 (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
222 if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
223 sizeof(tcp_debx)) {
224 perror("trpt: tcp_debx");
225 exit(3);
226 }
227 (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
228 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
229 sizeof(tcp_debug)) {
230 perror("trpt: tcp_debug");
231 exit(3);
232 }
233 /*
234 * If no control blocks have been specified, figure
235 * out how many distinct one we have and summarize
236 * them in tcp_pcbs for sorting the trace records
237 * below.
238 */
239 if (!npcbs) {
240 for (i = 0; i < TCP_NDEBUG; i++) {
241 register struct tcp_debug *td = &tcp_debug[i];
242 register int j;
243
244 if (td->td_tcb == 0)
245 continue;
246 for (j = 0; j < npcbs; j++)
247 if (tcp_pcbs[j] == td->td_tcb)
248 break;
249 if (j >= npcbs)
250 tcp_pcbs[npcbs++] = td->td_tcb;
251 }
252 if (!npcbs)
253 exit(0);
254 }
255 qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
256 if (jflag) {
257 for (i = 0;;) {
258 printf("%x", (int)tcp_pcbs[i]);
259 if (++i == npcbs)
260 break;
261 fputs(", ", stdout);
262 }
263 putchar('\n');
264 }
265 else for (i = 0; i < npcbs; i++) {
266 printf("\n%x:\n", (int)tcp_pcbs[i]);
267 dotrace(tcp_pcbs[i]);
268 }
269 exit(0);
270}
271
272dotrace(tcpcb)
273 register caddr_t tcpcb;
274{
275 register struct tcp_debug *td;
276 register int i;
277 int prev_debx = tcp_debx;
278
279again: if (--tcp_debx < 0)
280 tcp_debx = TCP_NDEBUG - 1;
281 for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
282 td = &tcp_debug[i];
283 if (tcpcb && td->td_tcb != tcpcb)
284 continue;
285 ntime = ntohl(td->td_time);
286 tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
7ba0088d 287 &td->td_ti.ti_t, td->td_req);
b7080c8e
A
288 if (i == tcp_debx)
289 goto done;
290 }
291 for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
292 td = &tcp_debug[i];
293 if (tcpcb && td->td_tcb != tcpcb)
294 continue;
295 ntime = ntohl(td->td_time);
296 tcp_trace(td->td_act, td->td_ostate, td->td_tcb, &td->td_cb,
7ba0088d 297 &td->td_ti.ti_t, td->td_req);
b7080c8e
A
298 }
299done: if (follow) {
300 prev_debx = tcp_debx + 1;
301 if (prev_debx >= TCP_NDEBUG)
302 prev_debx = 0;
303 do {
304 sleep(1);
305 (void)klseek(memf, (off_t)nl[N_TCP_DEBX].n_value, L_SET);
306 if (read(memf, (char *)&tcp_debx, sizeof(tcp_debx)) !=
307 sizeof(tcp_debx)) {
308 perror("trpt: tcp_debx");
309 exit(3);
310 }
311 } while (tcp_debx == prev_debx);
312 (void)klseek(memf, (off_t)nl[N_TCP_DEBUG].n_value, L_SET);
313 if (read(memf, (char *)tcp_debug, sizeof(tcp_debug)) !=
314 sizeof(tcp_debug)) {
315 perror("trpt: tcp_debug");
316 exit(3);
317 }
318 goto again;
319 }
320}
321
322/*
323 * Tcp debug routines
324 */
325/*ARGSUSED*/
326tcp_trace(act, ostate, atp, tp, ti, req)
327 short act, ostate;
328 struct tcpcb *atp, *tp;
329 struct tcpiphdr *ti;
330 int req;
331{
332 tcp_seq seq, ack;
333 int flags, len, win, timer;
334
335 printf("%03ld %s:%s ",(ntime/10) % 1000, tcpstates[ostate],
336 tanames[act]);
337 switch (act) {
338 case TA_INPUT:
339 case TA_OUTPUT:
340 case TA_DROP:
341 if (aflag) {
342 printf("(src=%s,%u, ",
343 inet_ntoa(ti->ti_src), ntohs(ti->ti_sport));
344 printf("dst=%s,%u)",
345 inet_ntoa(ti->ti_dst), ntohs(ti->ti_dport));
346 }
347 seq = ti->ti_seq;
348 ack = ti->ti_ack;
349 len = ti->ti_len;
350 win = ti->ti_win;
351 if (act == TA_OUTPUT) {
352 seq = ntohl(seq);
353 ack = ntohl(ack);
354 len = ntohs(len);
355 win = ntohs(win);
356 }
357 if (act == TA_OUTPUT)
358 len -= sizeof(struct tcphdr);
359 if (len)
360 printf("[%lx..%lx)", seq, seq + len);
361 else
362 printf("%lx", seq);
363 printf("@%lx", ack);
364 if (win)
365 printf("(win=%x)", win);
366 flags = ti->ti_flags;
367 if (flags) {
368 register char *cp = "<";
369#define pf(flag, string) { \
370 if (ti->ti_flags&flag) { \
371 (void)printf("%s%s", cp, string); \
372 cp = ","; \
373 } \
374}
375 pf(TH_SYN, "SYN");
376 pf(TH_ACK, "ACK");
377 pf(TH_FIN, "FIN");
378 pf(TH_RST, "RST");
379 pf(TH_PUSH, "PUSH");
380 pf(TH_URG, "URG");
381 printf(">");
382 }
383 break;
384 case TA_USER:
385 timer = req >> 8;
386 req &= 0xff;
387 printf("%s", prurequests[req]);
388 if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
389 printf("<%s>", tcptimers[timer]);
390 break;
391 }
392 printf(" -> %s", tcpstates[tp->t_state]);
393 /* print out internal state of tp !?! */
394 printf("\n");
395 if (sflag) {
396 printf("\trcv_nxt %lx rcv_wnd %x snd_una %lx snd_nxt %lx snd_max %lx\n",
397 tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
398 tp->snd_max);
399 printf("\tsnd_wl1 %lx snd_wl2 %lx snd_wnd %x\n", tp->snd_wl1,
400 tp->snd_wl2, tp->snd_wnd);
401 }
402 /* print out timers? */
403 if (tflag) {
404 register char *cp = "\t";
405 register int i;
406
407 for (i = 0; i < TCPT_NTIMERS; i++) {
408 if (tp->t_timer[i] == 0)
409 continue;
410 printf("%s%s=%d", cp, tcptimers[i], tp->t_timer[i]);
411 if (i == TCPT_REXMT)
412 printf(" (t_rxtshft=%d)", tp->t_rxtshift);
413 cp = ", ";
414 }
415 if (*cp != '\t')
416 putchar('\n');
417 }
418}
419
420numeric(c1, c2)
421 caddr_t *c1, *c2;
422{
423 return(*c1 - *c2);
424}
425
426klseek(fd, base, off)
427 int fd, off;
428 off_t base;
429{
430 off_t lseek();
431
432#ifndef NEWVM
433 if (kflag) { /* get kernel pte */
434 base &= ~KERNBASE;
435 base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
436 }
437#endif
438 (void)lseek(fd, base, off);
439}