2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.57 1999-10-30 05:11:21 itojun Exp $ (LBL)";
27 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
33 #include <netinet/ip_var.h>
34 #include <netinet/tcp.h>
45 #include <netinet/ip6.h>
48 #include "interface.h"
49 #include "addrtoname.h"
54 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
57 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
60 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
63 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
65 #ifndef TCPOPT_ECHOREPLY
66 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
68 #ifndef TCPOPT_TIMESTAMP
69 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
72 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
75 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
78 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
93 struct tcp_seq_hash
*nxt
;
99 #define TSEQ_HASHSIZE 919
101 /* These tcp optinos do not have the size octet */
102 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
104 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
108 tcp_print(register const u_char
*bp
, register u_int length
,
109 register const u_char
*bp2
)
111 register const struct tcphdr
*tp
;
112 register const struct ip
*ip
;
113 register u_char flags
;
116 u_short sport
, dport
, win
, urp
;
119 register const struct ip6_hdr
*ip6
;
122 tp
= (struct tcphdr
*)bp
;
123 ip
= (struct ip
*)bp2
;
126 ip6
= (struct ip6_hdr
*)bp2
;
132 if (length
< sizeof(*tp
)) {
133 (void)printf("truncated-tcp %d", length
);
137 sport
= ntohs(tp
->th_sport
);
138 dport
= ntohs(tp
->th_dport
);
139 seq
= ntohl(tp
->th_seq
);
140 ack
= ntohl(tp
->th_ack
);
141 win
= ntohs(tp
->th_win
);
142 urp
= ntohs(tp
->th_urp
);
146 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
147 (void)printf("%s.%s > %s.%s: ",
148 ip6addr_string(&ip6
->ip6_src
),
149 tcpport_string(sport
),
150 ip6addr_string(&ip6
->ip6_dst
),
151 tcpport_string(dport
));
153 (void)printf("%s > %s: ",
154 tcpport_string(sport
), tcpport_string(dport
));
159 if (ip
->ip_p
== IPPROTO_TCP
) {
160 (void)printf("%s.%s > %s.%s: ",
161 ipaddr_string(&ip
->ip_src
),
162 tcpport_string(sport
),
163 ipaddr_string(&ip
->ip_dst
),
164 tcpport_string(dport
));
166 (void)printf("%s > %s: ",
167 tcpport_string(sport
), tcpport_string(dport
));
172 (void)printf("tcp %d", length
- tp
->th_off
* 4);
176 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|TH_ECN
)) {
178 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
)) {
195 if (!Sflag
&& (flags
& TH_ACK
)) {
196 register struct tcp_seq_hash
*th
;
200 * Find (or record) the initial sequence numbers for
201 * this conversation. (we pick an arbitrary
202 * collating order so there's only one entry for
206 bzero(&tha
, sizeof(tha
));
211 } else if (sport
== dport
) {
214 for (i
= 0; i
< 4; i
++) {
215 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
216 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
223 tha
.src
= ip6
->ip6_dst
;
224 tha
.dst
= ip6
->ip6_src
;
225 tha
.port
= dport
<< 16 | sport
;
227 tha
.dst
= ip6
->ip6_dst
;
228 tha
.src
= ip6
->ip6_src
;
229 tha
.port
= sport
<< 16 | dport
;
234 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
238 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
239 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
240 tha
.port
= dport
<< 16 | sport
;
242 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
243 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
244 tha
.port
= sport
<< 16 | dport
;
250 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
251 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
252 tha
.port
= sport
<< 16 | dport
;
255 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
256 tha
.port
= dport
<< 16 | sport
;
261 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
262 th
->nxt
; th
= th
->nxt
)
263 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
267 if (!th
->nxt
|| flags
& TH_SYN
) {
268 /* didn't find it or new conversation */
269 if (th
->nxt
== NULL
) {
270 th
->nxt
= (struct tcp_seq_hash
*)
271 calloc(1, sizeof(*th
));
273 error("tcp_print: calloc");
277 th
->ack
= seq
, th
->seq
= ack
- 1;
279 th
->seq
= seq
, th
->ack
= ack
- 1;
282 seq
-= th
->ack
, ack
-= th
->seq
;
284 seq
-= th
->seq
, ack
-= th
->ack
;
287 hlen
= tp
->th_off
* 4;
289 (void)printf(" [bad hdr length]");
293 if (length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
294 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
296 (void)printf(" ack %u", ack
);
298 (void)printf(" win %d", win
);
301 (void)printf(" urg %d", urp
);
303 * Handle any options.
305 if ((hlen
-= sizeof(*tp
)) > 0) {
306 register const u_char
*cp
;
307 register int i
, opt
, len
, datalen
;
309 cp
= (const u_char
*)tp
+ sizeof(*tp
);
320 len
= *cp
++; /* total including type, len */
321 if (len
< 2 || len
> hlen
)
323 --hlen
; /* account for length byte */
325 --hlen
; /* account for type byte */
328 /* Bail if "l" bytes of data are not left or were not captured */
329 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
337 (void)printf(" %u", EXTRACT_16BITS(cp
));
350 (void)printf("wscale");
353 (void)printf(" %u", *cp
);
357 (void)printf("sackOK");
361 (void)printf("sack");
363 for (i
= 0; i
< datalen
; i
+= 4) {
365 /* block-size@relative-origin */
366 (void)printf(" %u@%u",
367 EXTRACT_16BITS(cp
+ i
+ 2),
368 EXTRACT_16BITS(cp
+ i
));
371 (void)printf("[len %d]", len
);
375 (void)printf("echo");
378 (void)printf(" %u", EXTRACT_32BITS(cp
));
381 case TCPOPT_ECHOREPLY
:
382 (void)printf("echoreply");
385 (void)printf(" %u", EXTRACT_32BITS(cp
));
388 case TCPOPT_TIMESTAMP
:
389 (void)printf("timestamp");
392 (void)printf(" %u", EXTRACT_32BITS(cp
));
394 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
401 (void)printf(" %u", EXTRACT_32BITS(cp
));
405 (void)printf("ccnew");
408 (void)printf(" %u", EXTRACT_32BITS(cp
));
412 (void)printf("ccecho");
415 (void)printf(" %u", EXTRACT_32BITS(cp
));
419 (void)printf("opt-%d:", opt
);
421 for (i
= 0; i
< datalen
; ++i
) {
423 (void)printf("%02x", cp
[i
]);
428 /* Account for data printed */
432 /* Check specification against observed length */
433 ++datalen
; /* option octet */
434 if (!ZEROLENOPT(opt
))
435 ++datalen
; /* size octet */
437 (void)printf("[len %d]", len
);
439 if (opt
== TCPOPT_EOL
)
449 * Decode payload if necessary.
451 bp
+= (tp
->th_off
* 4);
452 if (sport
== 179 || dport
== 179)
453 bgp_print(bp
, length
);
456 fputs("[bad opt]", stdout
);
461 fputs("[|tcp]", stdout
);