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
[] _U_
=
24 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.107.2.5 2004-07-28 20:11:31 guy Exp $ (LBL)";
31 #include <tcpdump-stdinc.h>
39 #include "interface.h"
40 #include "addrtoname.h"
53 static void print_tcp_rst_data(register const u_char
*sp
, u_int length
);
55 #define MAX_RST_DATA_LEN 30
70 struct tcp_seq_hash
*nxt
;
76 #define TSEQ_HASHSIZE 919
78 /* These tcp optinos do not have the size octet */
79 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
81 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
85 #define TELNET_PORT 23
90 #define NETBIOS_SSN_PORT 139
92 #define PPTP_PORT 1723
94 #define BEEP_PORT 10288
101 static int tcp_cksum(register const struct ip
*ip
,
102 register const struct tcphdr
*tp
,
117 /* pseudo-header.. */
118 phu
.ph
.len
= htons((u_int16_t
)len
);
120 phu
.ph
.proto
= IPPROTO_TCP
;
121 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
123 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
125 phu
.ph
.dst
= ip_finddst(ip
);
128 return in_cksum((u_short
*)tp
, len
,
129 sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
133 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
137 register const u_int16_t
*sp
;
141 struct in6_addr ph_src
;
142 struct in6_addr ph_dst
;
151 memset(&phu
, 0, sizeof(phu
));
152 phu
.ph
.ph_src
= ip6
->ip6_src
;
153 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
154 phu
.ph
.ph_len
= htonl(len
);
155 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
158 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
161 sp
= (const u_int16_t
*)tp
;
163 for (i
= 0; i
< (len
& ~1); i
+= 2)
167 sum
+= htons((*(const u_int8_t
*)sp
) << 8);
170 sum
= (sum
& 0xffff) + (sum
>> 16);
178 tcp_print(register const u_char
*bp
, register u_int length
,
179 register const u_char
*bp2
, int fragmented
)
181 register const struct tcphdr
*tp
;
182 register const struct ip
*ip
;
183 register u_char flags
;
186 u_int16_t sport
, dport
, win
, urp
;
187 u_int32_t seq
, ack
, thseq
, thack
;
190 register const struct ip6_hdr
*ip6
;
193 tp
= (struct tcphdr
*)bp
;
194 ip
= (struct ip
*)bp2
;
197 ip6
= (struct ip6_hdr
*)bp2
;
202 if (!TTEST(tp
->th_dport
)) {
203 (void)printf("%s > %s: [|tcp]",
204 ipaddr_string(&ip
->ip_src
),
205 ipaddr_string(&ip
->ip_dst
));
209 sport
= EXTRACT_16BITS(&tp
->th_sport
);
210 dport
= EXTRACT_16BITS(&tp
->th_dport
);
212 hlen
= TH_OFF(tp
) * 4;
215 * If data present, header length valid, and NFS port used,
217 * Pass offset of data plus 4 bytes for RPC TCP msg length
218 * to NFS print routines.
220 if (!qflag
&& hlen
>= sizeof(*tp
) && hlen
<= length
) {
221 if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
) <= snapend
&&
223 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
226 } else if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
)
229 nfsreply_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
236 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
237 (void)printf("%s.%s > %s.%s: ",
238 ip6addr_string(&ip6
->ip6_src
),
239 tcpport_string(sport
),
240 ip6addr_string(&ip6
->ip6_dst
),
241 tcpport_string(dport
));
243 (void)printf("%s > %s: ",
244 tcpport_string(sport
), tcpport_string(dport
));
249 if (ip
->ip_p
== IPPROTO_TCP
) {
250 (void)printf("%s.%s > %s.%s: ",
251 ipaddr_string(&ip
->ip_src
),
252 tcpport_string(sport
),
253 ipaddr_string(&ip
->ip_dst
),
254 tcpport_string(dport
));
256 (void)printf("%s > %s: ",
257 tcpport_string(sport
), tcpport_string(dport
));
261 if (hlen
< sizeof(*tp
)) {
262 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
263 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
269 seq
= EXTRACT_32BITS(&tp
->th_seq
);
270 ack
= EXTRACT_32BITS(&tp
->th_ack
);
271 win
= EXTRACT_16BITS(&tp
->th_win
);
272 urp
= EXTRACT_16BITS(&tp
->th_urp
);
275 (void)printf("tcp %d", length
- hlen
);
277 (void)printf(" [bad hdr length %u - too long, > %u]",
282 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
283 TH_ECNECHO
|TH_CWR
)) {
293 putchar('W'); /* congestion _W_indow reduced (ECN) */
294 if (flags
& TH_ECNECHO
)
295 putchar('E'); /* ecn _E_cho sent (ECN) */
299 if (!Sflag
&& (flags
& TH_ACK
)) {
300 register struct tcp_seq_hash
*th
;
301 const void *src
, *dst
;
305 * Find (or record) the initial sequence numbers for
306 * this conversation. (we pick an arbitrary
307 * collating order so there's only one entry for
311 memset(&tha
, 0, sizeof(tha
));
318 else if (sport
== dport
) {
319 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
323 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
324 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
325 tha
.port
= dport
<< 16 | sport
;
327 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
328 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
329 tha
.port
= sport
<< 16 | dport
;
336 else if (sport
== dport
) {
337 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
341 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
342 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
343 tha
.port
= dport
<< 16 | sport
;
345 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
346 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
347 tha
.port
= sport
<< 16 | dport
;
356 else if (sport
== dport
) {
357 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
361 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
362 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
363 tha
.port
= dport
<< 16 | sport
;
365 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
366 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
367 tha
.port
= sport
<< 16 | dport
;
372 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
373 th
->nxt
; th
= th
->nxt
)
374 if (memcmp((char *)&tha
, (char *)&th
->addr
,
375 sizeof(th
->addr
)) == 0)
378 if (!th
->nxt
|| (flags
& TH_SYN
)) {
379 /* didn't find it or new conversation */
380 if (th
->nxt
== NULL
) {
381 th
->nxt
= (struct tcp_seq_hash
*)
382 calloc(1, sizeof(*th
));
384 error("tcp_print: calloc");
388 th
->ack
= seq
, th
->seq
= ack
- 1;
390 th
->seq
= seq
, th
->ack
= ack
- 1;
393 seq
-= th
->ack
, ack
-= th
->seq
;
395 seq
-= th
->seq
, ack
-= th
->ack
;
402 thseq
= thack
= threv
= 0;
405 (void)printf(" [bad hdr length %u - too long, > %u]",
410 if (IP_V(ip
) == 4 && vflag
&& !fragmented
) {
411 u_int16_t sum
, tcp_sum
;
412 if (TTEST2(tp
->th_sport
, length
)) {
413 sum
= tcp_cksum(ip
, tp
, length
);
415 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
416 (void)printf(" [bad tcp cksum %x (->%x)!]",
417 tcp_sum
, in_cksum_shouldbe(tcp_sum
, sum
));
419 (void)printf(" [tcp sum ok]");
423 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !fragmented
) {
425 if (TTEST2(tp
->th_sport
, length
)) {
426 sum
= tcp6_cksum(ip6
, tp
, length
);
428 (void)printf(" [bad tcp cksum %x!]", sum
);
430 (void)printf(" [tcp sum ok]");
436 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
437 (void)printf(" %u:%u(%u)", seq
, seq
+ length
, length
);
439 (void)printf(" ack %u", ack
);
441 (void)printf(" win %d", win
);
444 (void)printf(" urg %d", urp
);
446 * Handle any options.
448 if (hlen
> sizeof(*tp
)) {
449 register const u_char
*cp
;
450 register u_int i
, opt
, datalen
;
454 cp
= (const u_char
*)tp
+ sizeof(*tp
);
465 len
= *cp
++; /* total including type, len */
466 if (len
< 2 || len
> hlen
)
468 --hlen
; /* account for length byte */
470 --hlen
; /* account for type byte */
473 /* Bail if "l" bytes of data are not left or were not captured */
474 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
482 (void)printf(" %u", EXTRACT_16BITS(cp
));
495 (void)printf("wscale");
498 (void)printf(" %u", *cp
);
502 (void)printf("sackOK");
506 (void)printf("sack");
508 if (datalen
% 8 != 0) {
509 (void)printf(" malformed sack ");
513 (void)printf(" sack %d ", datalen
/ 8);
514 for (i
= 0; i
< datalen
; i
+= 8) {
516 s
= EXTRACT_32BITS(cp
+ i
);
518 e
= EXTRACT_32BITS(cp
+ i
+ 4);
526 (void)printf("{%u:%u}", s
, e
);
533 (void)printf("echo");
536 (void)printf(" %u", EXTRACT_32BITS(cp
));
539 case TCPOPT_ECHOREPLY
:
540 (void)printf("echoreply");
543 (void)printf(" %u", EXTRACT_32BITS(cp
));
546 case TCPOPT_TIMESTAMP
:
547 (void)printf("timestamp");
550 (void)printf(" %u", EXTRACT_32BITS(cp
));
552 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
559 (void)printf(" %u", EXTRACT_32BITS(cp
));
563 (void)printf("ccnew");
566 (void)printf(" %u", EXTRACT_32BITS(cp
));
570 (void)printf("ccecho");
573 (void)printf(" %u", EXTRACT_32BITS(cp
));
577 (void)printf("opt-%u:", opt
);
579 for (i
= 0; i
< datalen
; ++i
) {
581 (void)printf("%02x", cp
[i
]);
586 /* Account for data printed */
590 /* Check specification against observed length */
591 ++datalen
; /* option octet */
592 if (!ZEROLENOPT(opt
))
593 ++datalen
; /* size octet */
595 (void)printf("[len %d]", len
);
597 if (opt
== TCPOPT_EOL
)
607 * Decode payload if necessary.
609 bp
+= TH_OFF(tp
) * 4;
610 if (flags
& TH_RST
) {
612 print_tcp_rst_data(bp
, length
);
614 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
616 telnet_print(bp
, length
);
617 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
618 bgp_print(bp
, length
);
619 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
621 #ifdef TCPDUMP_DO_SMB
622 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
623 nbt_tcp_print(bp
, length
);
625 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
626 beep_print(bp
, length
);
627 else if (length
> 2 &&
628 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
629 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
631 * TCP DNS query has 2byte length at the head.
632 * XXX packet could be unaligned, it can go strange
634 ns_print(bp
+ 2, length
- 2, 0);
635 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
636 msdp_print(bp
, length
);
638 else if (sport
== LDP_PORT
|| dport
== LDP_PORT
)
639 printf(": LDP, length: %u", length
);
643 fputs("[bad opt]", stdout
);
648 fputs("[|tcp]", stdout
);
654 * RFC1122 says the following on data in RST segments:
656 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
658 * A TCP SHOULD allow a received RST segment to include data.
661 * It has been suggested that a RST segment could contain
662 * ASCII text that encoded and explained the cause of the
663 * RST. No standard has yet been established for such
669 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
673 if (TTEST2(*sp
, length
))
677 if (length
> MAX_RST_DATA_LEN
) {
678 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
679 putchar('+'); /* indicate we truncate */
682 while (length
-- && sp
<= snapend
) {