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.74 2000-09-23 08:54:41 guy Exp $ (LBL)";
31 #include <sys/param.h>
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
46 #include <netinet/ip6.h>
49 #include "interface.h"
50 #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 NFS_PORT 2049
138 static int tcp_cksum(register const struct ip
*ip
,
139 register const struct tcphdr
*tp
,
153 register const u_int16_t
*sp
;
155 tlen
= ntohs(ip
->ip_len
) - ((const char *)tp
-(const char*)ip
);
157 /* pseudo-header.. */
158 phu
.ph
.len
= htons(tlen
);
160 phu
.ph
.proto
= IPPROTO_TCP
;
161 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
162 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
165 sum
= sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5];
167 sp
= (const u_int16_t
*)tp
;
169 for (i
=0; i
<(tlen
&~1); i
+= 2)
173 sum
+= htons( (*(const char *)sp
) << 8);
177 sum
= (sum
& 0xffff) + (sum
>> 16);
184 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
188 register const u_int16_t
*sp
;
192 struct in6_addr ph_src
;
193 struct in6_addr ph_dst
;
201 tlen
= ntohs(ip6
->ip6_plen
) + sizeof(struct ip6_hdr
) -
202 ((const char *)tp
- (const char*)ip6
);
205 memset(&phu
, 0, sizeof(phu
));
206 phu
.ph
.ph_src
= ip6
->ip6_src
;
207 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
208 phu
.ph
.ph_len
= htonl(tlen
);
209 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
212 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
215 sp
= (const u_int16_t
*)tp
;
217 for (i
= 0; i
< (tlen
& ~1); i
+= 2)
221 sum
+= htons((*(const char *)sp
) << 8);
224 sum
= (sum
& 0xffff) + (sum
>> 16);
232 tcp_print(register const u_char
*bp
, register u_int length
,
233 register const u_char
*bp2
)
235 register const struct tcphdr
*tp
;
236 register const struct ip
*ip
;
237 register u_char flags
;
240 u_int16_t sport
, dport
, win
, urp
;
241 u_int32_t seq
, ack
, thseq
, thack
;
244 register const struct ip6_hdr
*ip6
;
247 tp
= (struct tcphdr
*)bp
;
248 ip
= (struct ip
*)bp2
;
251 ip6
= (struct ip6_hdr
*)bp2
;
256 if (!TTEST(tp
->th_dport
)) {
257 (void)printf("%s > %s: [|tcp]",
258 ipaddr_string(&ip
->ip_src
),
259 ipaddr_string(&ip
->ip_dst
));
263 sport
= ntohs(tp
->th_sport
);
264 dport
= ntohs(tp
->th_dport
);
267 hlen
= tp
->th_off
* 4;
270 * If data present and NFS port used, assume NFS.
271 * Pass offset of data plus 4 bytes for RPC TCP msg length
272 * to NFS print routines.
275 if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
) <= snapend
&&
277 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
-hlen
,
280 } else if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
)
283 nfsreply_print((u_char
*)tp
+ hlen
+ 4,length
-hlen
,
290 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
291 (void)printf("%s.%s > %s.%s: ",
292 ip6addr_string(&ip6
->ip6_src
),
293 tcpport_string(sport
),
294 ip6addr_string(&ip6
->ip6_dst
),
295 tcpport_string(dport
));
297 (void)printf("%s > %s: ",
298 tcpport_string(sport
), tcpport_string(dport
));
303 if (ip
->ip_p
== IPPROTO_TCP
) {
304 (void)printf("%s.%s > %s.%s: ",
305 ipaddr_string(&ip
->ip_src
),
306 tcpport_string(sport
),
307 ipaddr_string(&ip
->ip_dst
),
308 tcpport_string(dport
));
310 (void)printf("%s > %s: ",
311 tcpport_string(sport
), tcpport_string(dport
));
317 seq
= (u_int32_t
)ntohl(tp
->th_seq
);
318 ack
= (u_int32_t
)ntohl(tp
->th_ack
);
319 win
= ntohs(tp
->th_win
);
320 urp
= ntohs(tp
->th_urp
);
323 (void)printf("tcp %d", length
- tp
->th_off
* 4);
326 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
327 TH_ECNECHO
|TH_CWR
)) {
337 putchar('W'); /* congestion _W_indow reduced (ECN) */
338 if (flags
& TH_ECNECHO
)
339 putchar('E'); /* ecn _E_cho sent (ECN) */
343 if (!Sflag
&& (flags
& TH_ACK
)) {
344 register struct tcp_seq_hash
*th
;
348 * Find (or record) the initial sequence numbers for
349 * this conversation. (we pick an arbitrary
350 * collating order so there's only one entry for
354 memset(&tha
, 0, sizeof(tha
));
359 } else if (sport
== dport
) {
362 for (i
= 0; i
< 4; i
++) {
363 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
364 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
371 tha
.src
= ip6
->ip6_dst
;
372 tha
.dst
= ip6
->ip6_src
;
373 tha
.port
= dport
<< 16 | sport
;
375 tha
.dst
= ip6
->ip6_dst
;
376 tha
.src
= ip6
->ip6_src
;
377 tha
.port
= sport
<< 16 | dport
;
382 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
386 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
387 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
388 tha
.port
= dport
<< 16 | sport
;
390 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
391 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
392 tha
.port
= sport
<< 16 | dport
;
398 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
399 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
400 tha
.port
= sport
<< 16 | dport
;
403 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
404 tha
.port
= dport
<< 16 | sport
;
410 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
411 th
->nxt
; th
= th
->nxt
)
412 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
416 if (!th
->nxt
|| (flags
& TH_SYN
)) {
417 /* didn't find it or new conversation */
418 if (th
->nxt
== NULL
) {
419 th
->nxt
= (struct tcp_seq_hash
*)
420 calloc(1, sizeof(*th
));
422 error("tcp_print: calloc");
426 th
->ack
= seq
, th
->seq
= ack
- 1;
428 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
->ip_v
== 4 && vflag
) {
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
->ip_v
== 6 && ip6
->ip6_plen
&& vflag
) {
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
+= (tp
->th_off
* 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
);
658 fputs("[bad opt]", stdout
);
663 fputs("[|tcp]", stdout
);
669 * RFC1122 says the following on data in RST segments:
671 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
673 * A TCP SHOULD allow a received RST segment to include data.
676 * It has been suggested that a RST segment could contain
677 * ASCII text that encoded and explained the cause of the
678 * RST. No standard has yet been established for such
684 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
688 if (TTEST2(*sp
, length
))
692 if (length
> MAX_RST_DATA_LEN
) {
693 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
694 putchar('+'); /* indicate we truncate */
697 while (length
-- && sp
<= snapend
) {