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.84 2001-02-03 05:04:49 itojun Exp $ (LBL)";
31 #include <sys/param.h>
36 #include <netinet/in.h>
44 #include "interface.h"
45 #include "addrtoname.h"
57 static void print_tcp_rst_data(register const u_char
*sp
, u_int length
);
59 #define MAX_RST_DATA_LEN 30
63 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
66 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
69 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
72 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
74 #ifndef TCPOPT_ECHOREPLY
75 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
77 #ifndef TCPOPT_TIMESTAMP
78 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
81 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
84 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
87 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
91 * Definitions required for ECN
92 * for use if the OS running tcpdump does not have ECN
95 #define TH_ECNECHO 0x40 /* ECN Echo in tcp header */
98 #define TH_CWR 0x80 /* ECN Cwnd Reduced in tcp header*/
112 struct tcp_seq_hash
{
113 struct tcp_seq_hash
*nxt
;
119 #define TSEQ_HASHSIZE 919
121 /* These tcp optinos do not have the size octet */
122 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
124 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
128 #define TELNET_PORT 23
133 #define NETBIOS_SSN_PORT 139
134 #define BXXP_PORT 10288
136 #define NFS_PORT 2049
139 static int tcp_cksum(register const struct ip
*ip
,
140 register const struct tcphdr
*tp
,
154 register const u_int16_t
*sp
;
156 tlen
= ntohs(ip
->ip_len
) - ((const char *)tp
-(const char*)ip
);
158 /* pseudo-header.. */
159 phu
.ph
.len
= htons(tlen
);
161 phu
.ph
.proto
= IPPROTO_TCP
;
162 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
163 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
166 sum
= sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5];
168 sp
= (const u_int16_t
*)tp
;
170 for (i
=0; i
<(tlen
&~1); i
+= 2)
174 sum
+= htons( (*(const u_int8_t
*)sp
) << 8);
178 sum
= (sum
& 0xffff) + (sum
>> 16);
185 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
189 register const u_int16_t
*sp
;
193 struct in6_addr ph_src
;
194 struct in6_addr ph_dst
;
202 tlen
= ntohs(ip6
->ip6_plen
) + sizeof(struct ip6_hdr
) -
203 ((const char *)tp
- (const char*)ip6
);
206 memset(&phu
, 0, sizeof(phu
));
207 phu
.ph
.ph_src
= ip6
->ip6_src
;
208 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
209 phu
.ph
.ph_len
= htonl(tlen
);
210 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
213 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
216 sp
= (const u_int16_t
*)tp
;
218 for (i
= 0; i
< (tlen
& ~1); i
+= 2)
222 sum
+= htons((*(const u_int8_t
*)sp
) << 8);
225 sum
= (sum
& 0xffff) + (sum
>> 16);
233 tcp_print(register const u_char
*bp
, register u_int length
,
234 register const u_char
*bp2
, int fragmented
)
236 register const struct tcphdr
*tp
;
237 register const struct ip
*ip
;
238 register u_char flags
;
241 u_int16_t sport
, dport
, win
, urp
;
242 u_int32_t seq
, ack
, thseq
, thack
;
245 register const struct ip6_hdr
*ip6
;
248 tp
= (struct tcphdr
*)bp
;
249 ip
= (struct ip
*)bp2
;
252 ip6
= (struct ip6_hdr
*)bp2
;
257 if (!TTEST(tp
->th_dport
)) {
258 (void)printf("%s > %s: [|tcp]",
259 ipaddr_string(&ip
->ip_src
),
260 ipaddr_string(&ip
->ip_dst
));
264 sport
= ntohs(tp
->th_sport
);
265 dport
= ntohs(tp
->th_dport
);
268 hlen
= TH_OFF(tp
) * 4;
271 * If data present and NFS port used, assume NFS.
272 * Pass offset of data plus 4 bytes for RPC TCP msg length
273 * to NFS print routines.
276 if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
) <= snapend
&&
278 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
281 } else if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
)
284 nfsreply_print((u_char
*)tp
+ hlen
+ 4,length
-hlen
,
291 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
292 (void)printf("%s.%s > %s.%s: ",
293 ip6addr_string(&ip6
->ip6_src
),
294 tcpport_string(sport
),
295 ip6addr_string(&ip6
->ip6_dst
),
296 tcpport_string(dport
));
298 (void)printf("%s > %s: ",
299 tcpport_string(sport
), tcpport_string(dport
));
304 if (ip
->ip_p
== IPPROTO_TCP
) {
305 (void)printf("%s.%s > %s.%s: ",
306 ipaddr_string(&ip
->ip_src
),
307 tcpport_string(sport
),
308 ipaddr_string(&ip
->ip_dst
),
309 tcpport_string(dport
));
311 (void)printf("%s > %s: ",
312 tcpport_string(sport
), tcpport_string(dport
));
318 seq
= (u_int32_t
)ntohl(tp
->th_seq
);
319 ack
= (u_int32_t
)ntohl(tp
->th_ack
);
320 win
= ntohs(tp
->th_win
);
321 urp
= ntohs(tp
->th_urp
);
324 (void)printf("tcp %d", length
- TH_OFF(tp
) * 4);
327 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
328 TH_ECNECHO
|TH_CWR
)) {
338 putchar('W'); /* congestion _W_indow reduced (ECN) */
339 if (flags
& TH_ECNECHO
)
340 putchar('E'); /* ecn _E_cho sent (ECN) */
344 if (!Sflag
&& (flags
& TH_ACK
)) {
345 register struct tcp_seq_hash
*th
;
349 * Find (or record) the initial sequence numbers for
350 * this conversation. (we pick an arbitrary
351 * collating order so there's only one entry for
355 memset(&tha
, 0, sizeof(tha
));
360 else if (sport
== dport
) {
363 for (i
= 0; i
< 4; i
++) {
364 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
365 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
372 tha
.src
= ip6
->ip6_dst
;
373 tha
.dst
= ip6
->ip6_src
;
374 tha
.port
= dport
<< 16 | sport
;
376 tha
.dst
= ip6
->ip6_dst
;
377 tha
.src
= ip6
->ip6_src
;
378 tha
.port
= sport
<< 16 | dport
;
383 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
387 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
388 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
389 tha
.port
= dport
<< 16 | sport
;
391 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
392 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
393 tha
.port
= sport
<< 16 | dport
;
399 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
400 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
401 tha
.port
= sport
<< 16 | dport
;
404 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
405 tha
.port
= dport
<< 16 | sport
;
411 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
412 th
->nxt
; th
= th
->nxt
)
413 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
417 if (!th
->nxt
|| (flags
& TH_SYN
)) {
418 /* didn't find it or new conversation */
419 if (th
->nxt
== NULL
) {
420 th
->nxt
= (struct tcp_seq_hash
*)
421 calloc(1, sizeof(*th
));
423 error("tcp_print: calloc");
427 th
->ack
= seq
, th
->seq
= ack
- 1;
429 th
->seq
= seq
, th
->ack
= ack
- 1;
432 seq
-= th
->ack
, ack
-= th
->seq
;
434 seq
-= th
->seq
, ack
-= th
->ack
;
441 thseq
= thack
= threv
= 0;
444 (void)printf(" [bad hdr length]");
448 if (IP_V(ip
) == 4 && vflag
&& !fragmented
) {
450 if (TTEST2(tp
->th_sport
, length
)) {
451 sum
= tcp_cksum(ip
, tp
, length
);
453 (void)printf(" [bad tcp cksum %x!]", sum
);
455 (void)printf(" [tcp sum ok]");
459 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !fragmented
) {
461 if (TTEST2(tp
->th_sport
, length
)) {
462 sum
= tcp6_cksum(ip6
, tp
, length
);
464 (void)printf(" [bad tcp cksum %x!]", sum
);
466 (void)printf(" [tcp sum ok]");
472 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
473 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
475 (void)printf(" ack %u", ack
);
477 (void)printf(" win %d", win
);
480 (void)printf(" urg %d", urp
);
482 * Handle any options.
484 if ((hlen
-= sizeof(*tp
)) > 0) {
485 register const u_char
*cp
;
486 register int i
, opt
, len
, datalen
;
488 cp
= (const u_char
*)tp
+ sizeof(*tp
);
499 len
= *cp
++; /* total including type, len */
500 if (len
< 2 || len
> hlen
)
502 --hlen
; /* account for length byte */
504 --hlen
; /* account for type byte */
507 /* Bail if "l" bytes of data are not left or were not captured */
508 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
516 (void)printf(" %u", EXTRACT_16BITS(cp
));
529 (void)printf("wscale");
532 (void)printf(" %u", *cp
);
536 (void)printf("sackOK");
540 (void)printf("sack");
542 if (datalen
% 8 != 0) {
543 (void)printf(" malformed sack ");
547 (void)printf(" sack %d ", datalen
/ 8);
548 for (i
= 0; i
< datalen
; i
+= 8) {
550 s
= EXTRACT_32BITS(cp
+ i
);
552 e
= EXTRACT_32BITS(cp
+ i
+ 4);
560 (void)printf("{%u:%u}", s
, e
);
567 (void)printf("echo");
570 (void)printf(" %u", EXTRACT_32BITS(cp
));
573 case TCPOPT_ECHOREPLY
:
574 (void)printf("echoreply");
577 (void)printf(" %u", EXTRACT_32BITS(cp
));
580 case TCPOPT_TIMESTAMP
:
581 (void)printf("timestamp");
584 (void)printf(" %u", EXTRACT_32BITS(cp
));
586 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
593 (void)printf(" %u", EXTRACT_32BITS(cp
));
597 (void)printf("ccnew");
600 (void)printf(" %u", EXTRACT_32BITS(cp
));
604 (void)printf("ccecho");
607 (void)printf(" %u", EXTRACT_32BITS(cp
));
611 (void)printf("opt-%d:", opt
);
613 for (i
= 0; i
< datalen
; ++i
) {
615 (void)printf("%02x", cp
[i
]);
620 /* Account for data printed */
624 /* Check specification against observed length */
625 ++datalen
; /* option octet */
626 if (!ZEROLENOPT(opt
))
627 ++datalen
; /* size octet */
629 (void)printf("[len %d]", len
);
631 if (opt
== TCPOPT_EOL
)
641 * Decode payload if necessary.
643 bp
+= TH_OFF(tp
) * 4;
644 if (flags
& TH_RST
) {
646 print_tcp_rst_data(bp
, length
);
648 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
650 telnet_print(bp
, length
);
651 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
652 bgp_print(bp
, length
);
653 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
654 nbt_tcp_print(bp
, length
);
655 else if (sport
== BXXP_PORT
|| dport
== BXXP_PORT
)
656 bxxp_print(bp
, length
);
657 else if (length
> 2 &&
658 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
)) {
659 /* TCP DNS query has 2byte length at the head */
660 ns_print(bp
+ 2, length
- 2);
665 fputs("[bad opt]", stdout
);
670 fputs("[|tcp]", stdout
);
676 * RFC1122 says the following on data in RST segments:
678 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
680 * A TCP SHOULD allow a received RST segment to include data.
683 * It has been suggested that a RST segment could contain
684 * ASCII text that encoded and explained the cause of the
685 * RST. No standard has yet been established for such
691 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
695 if (TTEST2(*sp
, length
))
699 if (length
> MAX_RST_DATA_LEN
) {
700 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
701 putchar('+'); /* indicate we truncate */
704 while (length
-- && sp
<= snapend
) {