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