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.85 2001-03-09 05:38:21 guy 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
135 #define PPTP_PORT 1723
137 #define BXXP_PORT 10288
139 #define NFS_PORT 2049
142 static int tcp_cksum(register const struct ip
*ip
,
143 register const struct tcphdr
*tp
,
157 register const u_int16_t
*sp
;
159 tlen
= ntohs(ip
->ip_len
) - ((const char *)tp
-(const char*)ip
);
161 /* pseudo-header.. */
162 phu
.ph
.len
= htons(tlen
);
164 phu
.ph
.proto
= IPPROTO_TCP
;
165 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
166 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
169 sum
= sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5];
171 sp
= (const u_int16_t
*)tp
;
173 for (i
=0; i
<(tlen
&~1); i
+= 2)
177 sum
+= htons( (*(const u_int8_t
*)sp
) << 8);
181 sum
= (sum
& 0xffff) + (sum
>> 16);
188 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
192 register const u_int16_t
*sp
;
196 struct in6_addr ph_src
;
197 struct in6_addr ph_dst
;
205 tlen
= ntohs(ip6
->ip6_plen
) + sizeof(struct ip6_hdr
) -
206 ((const char *)tp
- (const char*)ip6
);
209 memset(&phu
, 0, sizeof(phu
));
210 phu
.ph
.ph_src
= ip6
->ip6_src
;
211 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
212 phu
.ph
.ph_len
= htonl(tlen
);
213 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
216 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
219 sp
= (const u_int16_t
*)tp
;
221 for (i
= 0; i
< (tlen
& ~1); i
+= 2)
225 sum
+= htons((*(const u_int8_t
*)sp
) << 8);
228 sum
= (sum
& 0xffff) + (sum
>> 16);
236 tcp_print(register const u_char
*bp
, register u_int length
,
237 register const u_char
*bp2
, int fragmented
)
239 register const struct tcphdr
*tp
;
240 register const struct ip
*ip
;
241 register u_char flags
;
244 u_int16_t sport
, dport
, win
, urp
;
245 u_int32_t seq
, ack
, thseq
, thack
;
248 register const struct ip6_hdr
*ip6
;
251 tp
= (struct tcphdr
*)bp
;
252 ip
= (struct ip
*)bp2
;
255 ip6
= (struct ip6_hdr
*)bp2
;
260 if (!TTEST(tp
->th_dport
)) {
261 (void)printf("%s > %s: [|tcp]",
262 ipaddr_string(&ip
->ip_src
),
263 ipaddr_string(&ip
->ip_dst
));
267 sport
= ntohs(tp
->th_sport
);
268 dport
= ntohs(tp
->th_dport
);
271 hlen
= TH_OFF(tp
) * 4;
274 * If data present and NFS port used, assume NFS.
275 * Pass offset of data plus 4 bytes for RPC TCP msg length
276 * to NFS print routines.
279 if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
) <= snapend
&&
281 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
284 } else if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
)
287 nfsreply_print((u_char
*)tp
+ hlen
+ 4,length
-hlen
,
294 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
295 (void)printf("%s.%s > %s.%s: ",
296 ip6addr_string(&ip6
->ip6_src
),
297 tcpport_string(sport
),
298 ip6addr_string(&ip6
->ip6_dst
),
299 tcpport_string(dport
));
301 (void)printf("%s > %s: ",
302 tcpport_string(sport
), tcpport_string(dport
));
307 if (ip
->ip_p
== IPPROTO_TCP
) {
308 (void)printf("%s.%s > %s.%s: ",
309 ipaddr_string(&ip
->ip_src
),
310 tcpport_string(sport
),
311 ipaddr_string(&ip
->ip_dst
),
312 tcpport_string(dport
));
314 (void)printf("%s > %s: ",
315 tcpport_string(sport
), tcpport_string(dport
));
321 seq
= (u_int32_t
)ntohl(tp
->th_seq
);
322 ack
= (u_int32_t
)ntohl(tp
->th_ack
);
323 win
= ntohs(tp
->th_win
);
324 urp
= ntohs(tp
->th_urp
);
327 (void)printf("tcp %d", length
- TH_OFF(tp
) * 4);
330 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
331 TH_ECNECHO
|TH_CWR
)) {
341 putchar('W'); /* congestion _W_indow reduced (ECN) */
342 if (flags
& TH_ECNECHO
)
343 putchar('E'); /* ecn _E_cho sent (ECN) */
347 if (!Sflag
&& (flags
& TH_ACK
)) {
348 register struct tcp_seq_hash
*th
;
352 * Find (or record) the initial sequence numbers for
353 * this conversation. (we pick an arbitrary
354 * collating order so there's only one entry for
358 memset(&tha
, 0, sizeof(tha
));
363 else if (sport
== dport
) {
366 for (i
= 0; i
< 4; i
++) {
367 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
368 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
375 tha
.src
= ip6
->ip6_dst
;
376 tha
.dst
= ip6
->ip6_src
;
377 tha
.port
= dport
<< 16 | sport
;
379 tha
.dst
= ip6
->ip6_dst
;
380 tha
.src
= ip6
->ip6_src
;
381 tha
.port
= sport
<< 16 | dport
;
386 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
390 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
391 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
392 tha
.port
= dport
<< 16 | sport
;
394 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
395 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
396 tha
.port
= sport
<< 16 | dport
;
402 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
403 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
404 tha
.port
= sport
<< 16 | dport
;
407 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
408 tha
.port
= dport
<< 16 | sport
;
414 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
415 th
->nxt
; th
= th
->nxt
)
416 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
420 if (!th
->nxt
|| (flags
& TH_SYN
)) {
421 /* didn't find it or new conversation */
422 if (th
->nxt
== NULL
) {
423 th
->nxt
= (struct tcp_seq_hash
*)
424 calloc(1, sizeof(*th
));
426 error("tcp_print: calloc");
430 th
->ack
= seq
, th
->seq
= ack
- 1;
432 th
->seq
= seq
, th
->ack
= ack
- 1;
435 seq
-= th
->ack
, ack
-= th
->seq
;
437 seq
-= th
->seq
, ack
-= th
->ack
;
444 thseq
= thack
= threv
= 0;
447 (void)printf(" [bad hdr length]");
451 if (IP_V(ip
) == 4 && vflag
&& !fragmented
) {
453 if (TTEST2(tp
->th_sport
, length
)) {
454 sum
= tcp_cksum(ip
, tp
, length
);
456 (void)printf(" [bad tcp cksum %x!]", sum
);
458 (void)printf(" [tcp sum ok]");
462 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !fragmented
) {
464 if (TTEST2(tp
->th_sport
, length
)) {
465 sum
= tcp6_cksum(ip6
, tp
, length
);
467 (void)printf(" [bad tcp cksum %x!]", sum
);
469 (void)printf(" [tcp sum ok]");
475 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
476 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
478 (void)printf(" ack %u", ack
);
480 (void)printf(" win %d", win
);
483 (void)printf(" urg %d", urp
);
485 * Handle any options.
487 if ((hlen
-= sizeof(*tp
)) > 0) {
488 register const u_char
*cp
;
489 register int i
, opt
, len
, datalen
;
491 cp
= (const u_char
*)tp
+ sizeof(*tp
);
502 len
= *cp
++; /* total including type, len */
503 if (len
< 2 || len
> hlen
)
505 --hlen
; /* account for length byte */
507 --hlen
; /* account for type byte */
510 /* Bail if "l" bytes of data are not left or were not captured */
511 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
519 (void)printf(" %u", EXTRACT_16BITS(cp
));
532 (void)printf("wscale");
535 (void)printf(" %u", *cp
);
539 (void)printf("sackOK");
543 (void)printf("sack");
545 if (datalen
% 8 != 0) {
546 (void)printf(" malformed sack ");
550 (void)printf(" sack %d ", datalen
/ 8);
551 for (i
= 0; i
< datalen
; i
+= 8) {
553 s
= EXTRACT_32BITS(cp
+ i
);
555 e
= EXTRACT_32BITS(cp
+ i
+ 4);
563 (void)printf("{%u:%u}", s
, e
);
570 (void)printf("echo");
573 (void)printf(" %u", EXTRACT_32BITS(cp
));
576 case TCPOPT_ECHOREPLY
:
577 (void)printf("echoreply");
580 (void)printf(" %u", EXTRACT_32BITS(cp
));
583 case TCPOPT_TIMESTAMP
:
584 (void)printf("timestamp");
587 (void)printf(" %u", EXTRACT_32BITS(cp
));
589 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
596 (void)printf(" %u", EXTRACT_32BITS(cp
));
600 (void)printf("ccnew");
603 (void)printf(" %u", EXTRACT_32BITS(cp
));
607 (void)printf("ccecho");
610 (void)printf(" %u", EXTRACT_32BITS(cp
));
614 (void)printf("opt-%d:", opt
);
616 for (i
= 0; i
< datalen
; ++i
) {
618 (void)printf("%02x", cp
[i
]);
623 /* Account for data printed */
627 /* Check specification against observed length */
628 ++datalen
; /* option octet */
629 if (!ZEROLENOPT(opt
))
630 ++datalen
; /* size octet */
632 (void)printf("[len %d]", len
);
634 if (opt
== TCPOPT_EOL
)
644 * Decode payload if necessary.
646 bp
+= TH_OFF(tp
) * 4;
647 if (flags
& TH_RST
) {
649 print_tcp_rst_data(bp
, length
);
651 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
653 telnet_print(bp
, length
);
654 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
655 bgp_print(bp
, length
);
656 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
657 pptp_print(bp
, length
);
658 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
659 nbt_tcp_print(bp
, length
);
660 else if (sport
== BXXP_PORT
|| dport
== BXXP_PORT
)
661 bxxp_print(bp
, length
);
662 else if (length
> 2 &&
663 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
)) {
664 /* TCP DNS query has 2byte length at the head */
665 ns_print(bp
+ 2, length
- 2);
670 fputs("[bad opt]", stdout
);
675 fputs("[|tcp]", stdout
);
681 * RFC1122 says the following on data in RST segments:
683 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
685 * A TCP SHOULD allow a received RST segment to include data.
688 * It has been suggested that a RST segment could contain
689 * ASCII text that encoded and explained the cause of the
690 * RST. No standard has yet been established for such
696 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
700 if (TTEST2(*sp
, length
))
704 if (length
> MAX_RST_DATA_LEN
) {
705 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
706 putchar('+'); /* indicate we truncate */
709 while (length
-- && sp
<= snapend
) {