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
|
/* $OpenBSD: trpt.c,v 1.12 2002/03/14 16:44:25 mpech Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* 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 NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/*
* Copyright (c) 1983, 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* 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
char copyright[] =
"@(#) Copyright (c) 1983, 1988, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)trpt.c 8.1 (Berkeley) 6/6/93";
#endif /* not lint */
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#define PRUREQUESTS
#include <sys/protosw.h>
#include <sys/file.h>
#include <net/route.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>
#include <netinet/ip_var.h>
#include <netinet/tcp.h>
#define TCPSTATES
#include <netinet/tcp_fsm.h>
#include <netinet/tcp_seq.h>
#define TCPTIMERS
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#include <netinet/tcpip.h>
#define TANAMES
#include <netinet/tcp_debug.h>
#include <arpa/inet.h>
#include <err.h>
#include <stdio.h>
#include <errno.h>
#include <kvm.h>
#include <nlist.h>
#include <paths.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
struct nlist nl[] = {
#define N_TCP_DEBUG 0
{ "_tcp_debug" },
#define N_TCP_DEBX 1
{ "_tcp_debx" },
{ NULL },
};
static caddr_t tcp_pcbs[TCP_NDEBUG];
static n_time ntime;
static int aflag, follow, sflag, tflag;
extern char *__progname;
int main(int, char *[]);
void dotrace(caddr_t);
void tcp_trace(short, short, struct tcpcb *, struct tcpcb *,
struct tcpiphdr *, int);
int numeric(const void *, const void *);
void usage(void);
kvm_t *kd;
int
main(argc, argv)
int argc;
char *argv[];
{
int ch, i, jflag, npcbs;
char *system, *core, *cp, errbuf[_POSIX2_LINE_MAX];
system = core = NULL;
jflag = npcbs = 0;
while ((ch = getopt(argc, argv, "afjM:N:p:st")) != -1) {
switch (ch) {
case 'a':
++aflag;
break;
case 'f':
++follow;
setlinebuf(stdout);
break;
case 'j':
++jflag;
break;
case 'p':
if (npcbs >= TCP_NDEBUG)
errx(1, "too many pcbs specified");
errno = 0;
tcp_pcbs[npcbs++] = (caddr_t)strtoul(optarg, &cp, 16);
if (*cp != '\0' || errno == ERANGE)
errx(1, "invalid address: %s", optarg);
break;
case 's':
++sflag;
break;
case 't':
++tflag;
break;
case 'N':
system = optarg;
break;
case 'M':
core = optarg;
break;
case '?':
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc)
usage();
/*
* Discard setgid privileged if not the running kernel so that bad
* guys can't print interesting stuff from kernel memory.
*/
if (core != NULL || system != NULL) {
setegid(getgid());
setgid(getgid());
}
kd = kvm_openfiles(system, core, NULL, O_RDONLY, errbuf);
if (kd == NULL)
errx(1, "can't open kmem: %s", errbuf);
setegid(getgid());
setgid(getgid());
if (kvm_nlist(kd, nl))
errx(2, "%s: no namelist", system ? system : _PATH_UNIX);
if (kvm_read(kd, nl[N_TCP_DEBX].n_value, (char *)&tcp_debx,
sizeof(tcp_debx)) != sizeof(tcp_debx))
errx(3, "tcp_debx: %s", kvm_geterr(kd));
if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
sizeof(tcp_debug)) != sizeof(tcp_debug))
errx(3, "tcp_debug: %s", kvm_geterr(kd));
/*
* If no control blocks have been specified, figure
* out how many distinct one we have and summarize
* them in tcp_pcbs for sorting the trace records
* below.
*/
if (npcbs == 0) {
for (i = 0; i < TCP_NDEBUG; i++) {
struct tcp_debug *td = &tcp_debug[i];
int j;
if (td->td_tcb == 0)
continue;
for (j = 0; j < npcbs; j++)
if (tcp_pcbs[j] == td->td_tcb)
break;
if (j >= npcbs)
tcp_pcbs[npcbs++] = td->td_tcb;
}
if (npcbs == 0)
exit(0);
}
qsort(tcp_pcbs, npcbs, sizeof(caddr_t), numeric);
if (jflag) {
for (i = 0;;) {
printf("%lx", (long)tcp_pcbs[i]);
if (++i == npcbs)
break;
fputs(", ", stdout);
}
putchar('\n');
} else {
for (i = 0; i < npcbs; i++) {
printf("\n%lx:\n", (long)tcp_pcbs[i]);
dotrace(tcp_pcbs[i]);
}
}
exit(0);
}
void
dotrace(tcpcb)
caddr_t tcpcb;
{
struct tcp_debug *td;
int prev_debx = tcp_debx;
int i;
again:
if (--tcp_debx < 0)
tcp_debx = TCP_NDEBUG - 1;
for (i = prev_debx % TCP_NDEBUG; i < TCP_NDEBUG; i++) {
td = &tcp_debug[i];
if (tcpcb && td->td_tcb != tcpcb)
continue;
ntime = ntohl(td->td_time);
tcp_trace(td->td_act, td->td_ostate,
(struct tcpcb *)td->td_tcb, &td->td_cb, &td->td_ti,
td->td_req);
if (i == tcp_debx)
goto done;
}
for (i = 0; i <= tcp_debx % TCP_NDEBUG; i++) {
td = &tcp_debug[i];
if (tcpcb && td->td_tcb != tcpcb)
continue;
ntime = ntohl(td->td_time);
tcp_trace(td->td_act, td->td_ostate,
(struct tcpcb *)td->td_tcb, &td->td_cb, &td->td_ti,
td->td_req);
}
done:
if (follow) {
prev_debx = tcp_debx + 1;
if (prev_debx >= TCP_NDEBUG)
prev_debx = 0;
do {
sleep(1);
if (kvm_read(kd, nl[N_TCP_DEBX].n_value,
(char *)&tcp_debx, sizeof(tcp_debx)) !=
sizeof(tcp_debx))
errx(3, "tcp_debx: %s", kvm_geterr(kd));
} while (tcp_debx == prev_debx);
if (kvm_read(kd, nl[N_TCP_DEBUG].n_value, (char *)tcp_debug,
sizeof(tcp_debug)) != sizeof(tcp_debug))
errx(3, "tcp_debug: %s", kvm_geterr(kd));
goto again;
}
}
/*
* Tcp debug routines
*/
/*ARGSUSED*/
void
tcp_trace(act, ostate, atp, tp, ti, req)
short act, ostate;
struct tcpcb *atp, *tp;
struct tcpiphdr *ti;
int req;
{
tcp_seq seq, ack;
int flags, len, win, timer;
printf("%03d %s:%s ", (ntime/10) % 1000, tcpstates[ostate],
tanames[act]);
switch (act) {
case TA_INPUT:
case TA_OUTPUT:
case TA_DROP:
if (aflag) {
printf("(src=%s,%u, ",
inet_ntoa(ti->ti_src), ntohs(ti->ti_sport));
printf("dst=%s,%u)",
inet_ntoa(ti->ti_dst), ntohs(ti->ti_dport));
}
seq = ti->ti_seq;
ack = ti->ti_ack;
len = ti->ti_len;
win = ti->ti_win;
if (act == TA_OUTPUT) {
NTOHL(seq);
NTOHL(ack);
NTOHS(len);
NTOHS(win);
}
if (act == TA_OUTPUT)
len -= sizeof(struct tcphdr);
if (len)
printf("[%x..%x)", seq, seq + len);
else
printf("%x", seq);
printf("@%x", ack);
if (win)
printf("(win=%x)", win);
flags = ti->ti_flags;
if (flags) {
char *cp = "<";
#define pf(flag, string) { \
if (ti->ti_flags&flag) { \
(void)printf("%s%s", cp, string); \
cp = ","; \
} \
}
pf(TH_SYN, "SYN");
pf(TH_ACK, "ACK");
pf(TH_FIN, "FIN");
pf(TH_RST, "RST");
pf(TH_PUSH, "PUSH");
pf(TH_URG, "URG");
printf(">");
}
break;
case TA_USER:
timer = req >> 8;
req &= 0xff;
printf("%s", prurequests[req]);
if (req == PRU_SLOWTIMO || req == PRU_FASTTIMO)
printf("<%s>", tcptimers[timer]);
break;
}
printf(" -> %s", tcpstates[tp->t_state]);
/* print out internal state of tp !?! */
printf("\n");
if (sflag) {
printf("\trcv_nxt %x rcv_wnd %lx snd_una %x snd_nxt %x snd_max %x\n",
tp->rcv_nxt, tp->rcv_wnd, tp->snd_una, tp->snd_nxt,
tp->snd_max);
printf("\tsnd_wl1 %x snd_wl2 %x snd_wnd %lx\n", tp->snd_wl1,
tp->snd_wl2, tp->snd_wnd);
}
/* print out timers? */
if (tflag) {
char *cp = "\t";
int i;
for (i = 0; i < TCPT_NTIMERS; i++) {
if (timeout_pending(&tp->t_timer[i]))
continue;
printf("%s%s=%d", cp, tcptimers[i],
tp->t_timer[i].to_time);
if (i == TCPT_REXMT)
printf(" (t_rxtshft=%d)", tp->t_rxtshift);
cp = ", ";
}
if (*cp != '\t')
putchar('\n');
}
}
int
numeric(v1, v2)
const void *v1, *v2;
{
const caddr_t *c1 = v1;
const caddr_t *c2 = v2;
int rv;
if (*c1 < *c2)
rv = -1;
else if (*c1 > *c2)
rv = 1;
else
rv = 0;
return (rv);
}
void
usage()
{
(void) fprintf(stderr, "usage: %s [-afjst] [-p hex-address]"
" [-N system] [-M core]\n", __progname);
exit(1);
}
|