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.104 2002-12-11 07:14:09 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
= EXTRACT_16BITS(&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
= EXTRACT_16BITS(&tp
->th_sport
);
212 dport
= EXTRACT_16BITS(&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
= EXTRACT_32BITS(&tp
->th_seq
);
265 ack
= EXTRACT_32BITS(&tp
->th_ack
);
266 win
= EXTRACT_16BITS(&tp
->th_win
);
267 urp
= EXTRACT_16BITS(&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
;
292 const void *src
, *dst
;
296 * Find (or record) the initial sequence numbers for
297 * this conversation. (we pick an arbitrary
298 * collating order so there's only one entry for
302 memset(&tha
, 0, sizeof(tha
));
309 else if (sport
== dport
) {
310 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
314 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
315 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
316 tha
.port
= dport
<< 16 | sport
;
318 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
319 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
320 tha
.port
= sport
<< 16 | dport
;
327 else if (sport
== dport
) {
328 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
332 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
333 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
334 tha
.port
= dport
<< 16 | sport
;
336 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
337 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
338 tha
.port
= sport
<< 16 | dport
;
347 else if (sport
== dport
) {
348 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
352 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
353 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
354 tha
.port
= dport
<< 16 | sport
;
356 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
357 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
358 tha
.port
= sport
<< 16 | dport
;
363 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
364 th
->nxt
; th
= th
->nxt
)
365 if (memcmp((char *)&tha
, (char *)&th
->addr
,
366 sizeof(th
->addr
)) == 0)
369 if (!th
->nxt
|| (flags
& TH_SYN
)) {
370 /* didn't find it or new conversation */
371 if (th
->nxt
== NULL
) {
372 th
->nxt
= (struct tcp_seq_hash
*)
373 calloc(1, sizeof(*th
));
375 error("tcp_print: calloc");
379 th
->ack
= seq
, th
->seq
= ack
- 1;
381 th
->seq
= seq
, th
->ack
= ack
- 1;
384 seq
-= th
->ack
, ack
-= th
->seq
;
386 seq
-= th
->seq
, ack
-= th
->ack
;
393 thseq
= thack
= threv
= 0;
396 (void)printf(" [bad hdr length]");
400 if (IP_V(ip
) == 4 && vflag
&& !fragmented
) {
401 u_int16_t sum
, tcp_sum
;
402 if (TTEST2(tp
->th_sport
, length
)) {
403 sum
= tcp_cksum(ip
, tp
, length
);
405 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
406 (void)printf(" [bad tcp cksum %x (->%x)!]",
407 tcp_sum
, in_cksum_shouldbe(tcp_sum
, sum
));
409 (void)printf(" [tcp sum ok]");
413 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !fragmented
) {
415 if (TTEST2(tp
->th_sport
, length
)) {
416 sum
= tcp6_cksum(ip6
, tp
, length
);
418 (void)printf(" [bad tcp cksum %x!]", sum
);
420 (void)printf(" [tcp sum ok]");
426 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
427 (void)printf(" %u:%u(%u)", seq
, seq
+ length
, length
);
429 (void)printf(" ack %u", ack
);
431 (void)printf(" win %d", win
);
434 (void)printf(" urg %d", urp
);
436 * Handle any options.
438 if (hlen
> sizeof(*tp
)) {
439 register const u_char
*cp
;
440 register u_int i
, opt
, datalen
;
444 cp
= (const u_char
*)tp
+ sizeof(*tp
);
455 len
= *cp
++; /* total including type, len */
456 if (len
< 2 || len
> hlen
)
458 --hlen
; /* account for length byte */
460 --hlen
; /* account for type byte */
463 /* Bail if "l" bytes of data are not left or were not captured */
464 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
472 (void)printf(" %u", EXTRACT_16BITS(cp
));
485 (void)printf("wscale");
488 (void)printf(" %u", *cp
);
492 (void)printf("sackOK");
496 (void)printf("sack");
498 if (datalen
% 8 != 0) {
499 (void)printf(" malformed sack ");
503 (void)printf(" sack %d ", datalen
/ 8);
504 for (i
= 0; i
< datalen
; i
+= 8) {
506 s
= EXTRACT_32BITS(cp
+ i
);
508 e
= EXTRACT_32BITS(cp
+ i
+ 4);
516 (void)printf("{%u:%u}", s
, e
);
523 (void)printf("echo");
526 (void)printf(" %u", EXTRACT_32BITS(cp
));
529 case TCPOPT_ECHOREPLY
:
530 (void)printf("echoreply");
533 (void)printf(" %u", EXTRACT_32BITS(cp
));
536 case TCPOPT_TIMESTAMP
:
537 (void)printf("timestamp");
540 (void)printf(" %u", EXTRACT_32BITS(cp
));
542 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
549 (void)printf(" %u", EXTRACT_32BITS(cp
));
553 (void)printf("ccnew");
556 (void)printf(" %u", EXTRACT_32BITS(cp
));
560 (void)printf("ccecho");
563 (void)printf(" %u", EXTRACT_32BITS(cp
));
567 (void)printf("opt-%u:", opt
);
569 for (i
= 0; i
< datalen
; ++i
) {
571 (void)printf("%02x", cp
[i
]);
576 /* Account for data printed */
580 /* Check specification against observed length */
581 ++datalen
; /* option octet */
582 if (!ZEROLENOPT(opt
))
583 ++datalen
; /* size octet */
585 (void)printf("[len %d]", len
);
587 if (opt
== TCPOPT_EOL
)
597 * Decode payload if necessary.
599 bp
+= TH_OFF(tp
) * 4;
600 if (flags
& TH_RST
) {
602 print_tcp_rst_data(bp
, length
);
604 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
606 telnet_print(bp
, length
);
607 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
608 bgp_print(bp
, length
);
609 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
611 #ifdef TCPDUMP_DO_SMB
612 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
613 nbt_tcp_print(bp
, length
);
615 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
616 beep_print(bp
, length
);
617 else if (length
> 2 &&
618 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
619 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
621 * TCP DNS query has 2byte length at the head.
622 * XXX packet could be unaligned, it can go strange
624 ns_print(bp
+ 2, length
- 2);
625 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
626 msdp_print(bp
, length
);
628 else if (sport
== LDP_PORT
|| dport
== LDP_PORT
)
629 printf(": LDP, length: %u", length
);
633 fputs("[bad opt]", stdout
);
638 fputs("[|tcp]", stdout
);
644 * RFC1122 says the following on data in RST segments:
646 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
648 * A TCP SHOULD allow a received RST segment to include data.
651 * It has been suggested that a RST segment could contain
652 * ASCII text that encoded and explained the cause of the
653 * RST. No standard has yet been established for such
659 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
663 if (TTEST2(*sp
, length
))
667 if (length
> MAX_RST_DATA_LEN
) {
668 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
669 putchar('+'); /* indicate we truncate */
672 while (length
-- && sp
<= snapend
) {