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.102 2002-09-05 00:00:22 guy Exp $ (LBL)";
31 #include <tcpdump-stdinc.h>
39 #include "interface.h"
40 #include "addrtoname.h"
52 static void print_tcp_rst_data(register const u_char
*sp
, u_int length
);
54 #define MAX_RST_DATA_LEN 30
69 struct tcp_seq_hash
*nxt
;
75 #define TSEQ_HASHSIZE 919
77 /* These tcp optinos do not have the size octet */
78 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
80 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
84 #define TELNET_PORT 23
89 #define NETBIOS_SSN_PORT 139
91 #define PPTP_PORT 1723
93 #define BEEP_PORT 10288
100 static int tcp_cksum(register const struct ip
*ip
,
101 register const struct tcphdr
*tp
,
116 /* pseudo-header.. */
117 phu
.ph
.len
= htons(len
); /* XXX */
119 phu
.ph
.proto
= IPPROTO_TCP
;
120 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
122 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
124 phu
.ph
.dst
= ip_finddst(ip
);
127 return in_cksum((u_short
*)tp
, len
,
128 sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
132 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
136 register const u_int16_t
*sp
;
140 struct in6_addr ph_src
;
141 struct in6_addr ph_dst
;
149 tlen
= ntohs(ip6
->ip6_plen
) + sizeof(struct ip6_hdr
) -
150 ((const char *)tp
- (const char*)ip6
);
153 memset(&phu
, 0, sizeof(phu
));
154 phu
.ph
.ph_src
= ip6
->ip6_src
;
155 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
156 phu
.ph
.ph_len
= htonl(tlen
);
157 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
160 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
163 sp
= (const u_int16_t
*)tp
;
165 for (i
= 0; i
< (tlen
& ~1); i
+= 2)
169 sum
+= htons((*(const u_int8_t
*)sp
) << 8);
172 sum
= (sum
& 0xffff) + (sum
>> 16);
180 tcp_print(register const u_char
*bp
, register u_int length
,
181 register const u_char
*bp2
, int fragmented
)
183 register const struct tcphdr
*tp
;
184 register const struct ip
*ip
;
185 register u_char flags
;
188 u_int16_t sport
, dport
, win
, urp
;
189 u_int32_t seq
, ack
, thseq
, thack
;
192 register const struct ip6_hdr
*ip6
;
195 tp
= (struct tcphdr
*)bp
;
196 ip
= (struct ip
*)bp2
;
199 ip6
= (struct ip6_hdr
*)bp2
;
204 if (!TTEST(tp
->th_dport
)) {
205 (void)printf("%s > %s: [|tcp]",
206 ipaddr_string(&ip
->ip_src
),
207 ipaddr_string(&ip
->ip_dst
));
211 sport
= ntohs(tp
->th_sport
);
212 dport
= ntohs(tp
->th_dport
);
214 hlen
= TH_OFF(tp
) * 4;
217 * If data present and NFS port used, assume NFS.
218 * Pass offset of data plus 4 bytes for RPC TCP msg length
219 * to NFS print routines.
222 if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
) <= snapend
&&
224 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
227 } else if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
)
230 nfsreply_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
237 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
238 (void)printf("%s.%s > %s.%s: ",
239 ip6addr_string(&ip6
->ip6_src
),
240 tcpport_string(sport
),
241 ip6addr_string(&ip6
->ip6_dst
),
242 tcpport_string(dport
));
244 (void)printf("%s > %s: ",
245 tcpport_string(sport
), tcpport_string(dport
));
250 if (ip
->ip_p
== IPPROTO_TCP
) {
251 (void)printf("%s.%s > %s.%s: ",
252 ipaddr_string(&ip
->ip_src
),
253 tcpport_string(sport
),
254 ipaddr_string(&ip
->ip_dst
),
255 tcpport_string(dport
));
257 (void)printf("%s > %s: ",
258 tcpport_string(sport
), tcpport_string(dport
));
264 seq
= (u_int32_t
)ntohl(tp
->th_seq
);
265 ack
= (u_int32_t
)ntohl(tp
->th_ack
);
266 win
= ntohs(tp
->th_win
);
267 urp
= ntohs(tp
->th_urp
);
270 (void)printf("tcp %d", length
- TH_OFF(tp
) * 4);
273 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
274 TH_ECNECHO
|TH_CWR
)) {
284 putchar('W'); /* congestion _W_indow reduced (ECN) */
285 if (flags
& TH_ECNECHO
)
286 putchar('E'); /* ecn _E_cho sent (ECN) */
290 if (!Sflag
&& (flags
& TH_ACK
)) {
291 register struct tcp_seq_hash
*th
;
295 * Find (or record) the initial sequence numbers for
296 * this conversation. (we pick an arbitrary
297 * collating order so there's only one entry for
301 memset(&tha
, 0, sizeof(tha
));
306 else if (sport
== dport
) {
309 for (i
= 0; i
< 4; i
++) {
310 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
311 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
318 tha
.src
= ip6
->ip6_dst
;
319 tha
.dst
= ip6
->ip6_src
;
320 tha
.port
= dport
<< 16 | sport
;
322 tha
.dst
= ip6
->ip6_dst
;
323 tha
.src
= ip6
->ip6_src
;
324 tha
.port
= sport
<< 16 | dport
;
329 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
333 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
334 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
335 tha
.port
= dport
<< 16 | sport
;
337 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
338 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
339 tha
.port
= sport
<< 16 | dport
;
345 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
346 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
347 tha
.port
= sport
<< 16 | dport
;
350 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
351 tha
.port
= dport
<< 16 | sport
;
357 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
358 th
->nxt
; th
= th
->nxt
)
359 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
363 if (!th
->nxt
|| (flags
& TH_SYN
)) {
364 /* didn't find it or new conversation */
365 if (th
->nxt
== NULL
) {
366 th
->nxt
= (struct tcp_seq_hash
*)
367 calloc(1, sizeof(*th
));
369 error("tcp_print: calloc");
373 th
->ack
= seq
, th
->seq
= ack
- 1;
375 th
->seq
= seq
, th
->ack
= ack
- 1;
378 seq
-= th
->ack
, ack
-= th
->seq
;
380 seq
-= th
->seq
, ack
-= th
->ack
;
387 thseq
= thack
= threv
= 0;
390 (void)printf(" [bad hdr length]");
394 if (IP_V(ip
) == 4 && vflag
&& !fragmented
) {
395 u_int16_t sum
, tcp_sum
;
396 if (TTEST2(tp
->th_sport
, length
)) {
397 sum
= tcp_cksum(ip
, tp
, length
);
399 tcp_sum
= ntohs(tp
->th_sum
);
400 (void)printf(" [bad tcp cksum %x (->%x)!]",
401 tcp_sum
, in_cksum_shouldbe(tcp_sum
, sum
));
403 (void)printf(" [tcp sum ok]");
407 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !fragmented
) {
409 if (TTEST2(tp
->th_sport
, length
)) {
410 sum
= tcp6_cksum(ip6
, tp
, length
);
412 (void)printf(" [bad tcp cksum %x!]", sum
);
414 (void)printf(" [tcp sum ok]");
420 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
421 (void)printf(" %u:%u(%u)", seq
, seq
+ length
, length
);
423 (void)printf(" ack %u", ack
);
425 (void)printf(" win %d", win
);
428 (void)printf(" urg %d", urp
);
430 * Handle any options.
432 if (hlen
> sizeof(*tp
)) {
433 register const u_char
*cp
;
434 register u_int i
, opt
, datalen
;
438 cp
= (const u_char
*)tp
+ sizeof(*tp
);
449 len
= *cp
++; /* total including type, len */
450 if (len
< 2 || len
> hlen
)
452 --hlen
; /* account for length byte */
454 --hlen
; /* account for type byte */
457 /* Bail if "l" bytes of data are not left or were not captured */
458 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
466 (void)printf(" %u", EXTRACT_16BITS(cp
));
479 (void)printf("wscale");
482 (void)printf(" %u", *cp
);
486 (void)printf("sackOK");
490 (void)printf("sack");
492 if (datalen
% 8 != 0) {
493 (void)printf(" malformed sack ");
497 (void)printf(" sack %d ", datalen
/ 8);
498 for (i
= 0; i
< datalen
; i
+= 8) {
500 s
= EXTRACT_32BITS(cp
+ i
);
502 e
= EXTRACT_32BITS(cp
+ i
+ 4);
510 (void)printf("{%u:%u}", s
, e
);
517 (void)printf("echo");
520 (void)printf(" %u", EXTRACT_32BITS(cp
));
523 case TCPOPT_ECHOREPLY
:
524 (void)printf("echoreply");
527 (void)printf(" %u", EXTRACT_32BITS(cp
));
530 case TCPOPT_TIMESTAMP
:
531 (void)printf("timestamp");
534 (void)printf(" %u", EXTRACT_32BITS(cp
));
536 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
543 (void)printf(" %u", EXTRACT_32BITS(cp
));
547 (void)printf("ccnew");
550 (void)printf(" %u", EXTRACT_32BITS(cp
));
554 (void)printf("ccecho");
557 (void)printf(" %u", EXTRACT_32BITS(cp
));
561 (void)printf("opt-%u:", opt
);
563 for (i
= 0; i
< datalen
; ++i
) {
565 (void)printf("%02x", cp
[i
]);
570 /* Account for data printed */
574 /* Check specification against observed length */
575 ++datalen
; /* option octet */
576 if (!ZEROLENOPT(opt
))
577 ++datalen
; /* size octet */
579 (void)printf("[len %d]", len
);
581 if (opt
== TCPOPT_EOL
)
591 * Decode payload if necessary.
593 bp
+= TH_OFF(tp
) * 4;
594 if (flags
& TH_RST
) {
596 print_tcp_rst_data(bp
, length
);
598 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
600 telnet_print(bp
, length
);
601 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
602 bgp_print(bp
, length
);
603 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
604 pptp_print(bp
, length
);
605 #ifdef TCPDUMP_DO_SMB
606 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
607 nbt_tcp_print(bp
, length
);
609 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
610 beep_print(bp
, length
);
611 else if (length
> 2 &&
612 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
613 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
615 * TCP DNS query has 2byte length at the head.
616 * XXX packet could be unaligned, it can go strange
618 ns_print(bp
+ 2, length
- 2);
619 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
620 msdp_print(bp
, length
);
622 else if (sport
== LDP_PORT
|| dport
== LDP_PORT
)
623 printf(": LDP, length: %u", length
);
627 fputs("[bad opt]", stdout
);
632 fputs("[|tcp]", stdout
);
638 * RFC1122 says the following on data in RST segments:
640 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
642 * A TCP SHOULD allow a received RST segment to include data.
645 * It has been suggested that a RST segment could contain
646 * ASCII text that encoded and explained the cause of the
647 * RST. No standard has yet been established for such
653 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
657 if (TTEST2(*sp
, length
))
661 if (length
> MAX_RST_DATA_LEN
) {
662 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
663 putchar('+'); /* indicate we truncate */
666 while (length
-- && sp
<= snapend
) {