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.115 2004-05-27 21:20:50 guy Exp $ (LBL)";
31 #include <tcpdump-stdinc.h>
39 #include "interface.h"
40 #include "addrtoname.h"
54 #include <openssl/md5.h>
56 static int tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
57 const u_char
*data
, int length
, const u_char
*rcvsig
);
60 static void print_tcp_rst_data(register const u_char
*sp
, u_int length
);
62 #define MAX_RST_DATA_LEN 30
77 struct tcp_seq_hash
*nxt
;
83 #define TSEQ_HASHSIZE 919
85 /* These tcp optinos do not have the size octet */
86 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
88 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
92 #define TELNET_PORT 23
97 #define NETBIOS_SSN_PORT 139
99 #define PPTP_PORT 1723
101 #define BEEP_PORT 10288
103 #define NFS_PORT 2049
105 #define MSDP_PORT 639
108 static int tcp_cksum(register const struct ip
*ip
,
109 register const struct tcphdr
*tp
,
124 /* pseudo-header.. */
125 phu
.ph
.len
= htons((u_int16_t
)len
);
127 phu
.ph
.proto
= IPPROTO_TCP
;
128 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
130 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
132 phu
.ph
.dst
= ip_finddst(ip
);
135 return in_cksum((u_short
*)tp
, len
,
136 sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
140 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
144 register const u_int16_t
*sp
;
148 struct in6_addr ph_src
;
149 struct in6_addr ph_dst
;
158 memset(&phu
, 0, sizeof(phu
));
159 phu
.ph
.ph_src
= ip6
->ip6_src
;
160 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
161 phu
.ph
.ph_len
= htonl(len
);
162 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
165 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
168 sp
= (const u_int16_t
*)tp
;
170 for (i
= 0; i
< (len
& ~1); i
+= 2)
174 sum
+= htons((*(const u_int8_t
*)sp
) << 8);
177 sum
= (sum
& 0xffff) + (sum
>> 16);
185 tcp_print(register const u_char
*bp
, register u_int length
,
186 register const u_char
*bp2
, int fragmented
)
188 register const struct tcphdr
*tp
;
189 register const struct ip
*ip
;
190 register u_char flags
;
193 u_int16_t sport
, dport
, win
, urp
;
194 u_int32_t seq
, ack
, thseq
, thack
;
197 register const struct ip6_hdr
*ip6
;
200 tp
= (struct tcphdr
*)bp
;
201 ip
= (struct ip
*)bp2
;
204 ip6
= (struct ip6_hdr
*)bp2
;
209 if (!TTEST(tp
->th_dport
)) {
210 (void)printf("%s > %s: [|tcp]",
211 ipaddr_string(&ip
->ip_src
),
212 ipaddr_string(&ip
->ip_dst
));
216 sport
= EXTRACT_16BITS(&tp
->th_sport
);
217 dport
= EXTRACT_16BITS(&tp
->th_dport
);
219 hlen
= TH_OFF(tp
) * 4;
222 * If data present and NFS port used, assume NFS.
223 * Pass offset of data plus 4 bytes for RPC TCP msg length
224 * to NFS print routines.
227 if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
) <= snapend
&&
229 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
232 } else if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
)
235 nfsreply_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
242 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
243 (void)printf("%s.%s > %s.%s: ",
244 ip6addr_string(&ip6
->ip6_src
),
245 tcpport_string(sport
),
246 ip6addr_string(&ip6
->ip6_dst
),
247 tcpport_string(dport
));
249 (void)printf("%s > %s: ",
250 tcpport_string(sport
), tcpport_string(dport
));
255 if (ip
->ip_p
== IPPROTO_TCP
) {
256 (void)printf("%s.%s > %s.%s: ",
257 ipaddr_string(&ip
->ip_src
),
258 tcpport_string(sport
),
259 ipaddr_string(&ip
->ip_dst
),
260 tcpport_string(dport
));
262 (void)printf("%s > %s: ",
263 tcpport_string(sport
), tcpport_string(dport
));
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
- TH_OFF(tp
) * 4);
278 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
279 TH_ECNECHO
|TH_CWR
)) {
289 putchar('W'); /* congestion _W_indow reduced (ECN) */
290 if (flags
& TH_ECNECHO
)
291 putchar('E'); /* ecn _E_cho sent (ECN) */
295 if (!Sflag
&& (flags
& TH_ACK
)) {
296 register struct tcp_seq_hash
*th
;
297 const void *src
, *dst
;
301 * Find (or record) the initial sequence numbers for
302 * this conversation. (we pick an arbitrary
303 * collating order so there's only one entry for
307 memset(&tha
, 0, sizeof(tha
));
314 else if (sport
== dport
) {
315 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
319 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
320 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
321 tha
.port
= dport
<< 16 | sport
;
323 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
324 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
325 tha
.port
= sport
<< 16 | dport
;
332 else if (sport
== dport
) {
333 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
337 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
338 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
339 tha
.port
= dport
<< 16 | sport
;
341 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
342 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
343 tha
.port
= sport
<< 16 | dport
;
352 else if (sport
== dport
) {
353 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
357 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
358 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
359 tha
.port
= dport
<< 16 | sport
;
361 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
362 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
363 tha
.port
= sport
<< 16 | dport
;
368 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
369 th
->nxt
; th
= th
->nxt
)
370 if (memcmp((char *)&tha
, (char *)&th
->addr
,
371 sizeof(th
->addr
)) == 0)
374 if (!th
->nxt
|| (flags
& TH_SYN
)) {
375 /* didn't find it or new conversation */
376 if (th
->nxt
== NULL
) {
377 th
->nxt
= (struct tcp_seq_hash
*)
378 calloc(1, sizeof(*th
));
380 error("tcp_print: calloc");
384 th
->ack
= seq
, th
->seq
= ack
- 1;
386 th
->seq
= seq
, th
->ack
= ack
- 1;
389 seq
-= th
->ack
, ack
-= th
->seq
;
391 seq
-= th
->seq
, ack
-= th
->ack
;
398 thseq
= thack
= threv
= 0;
401 (void)printf(" [bad hdr length]");
405 if (IP_V(ip
) == 4 && vflag
&& !fragmented
) {
406 u_int16_t sum
, tcp_sum
;
407 if (TTEST2(tp
->th_sport
, length
)) {
408 sum
= tcp_cksum(ip
, tp
, length
);
410 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
412 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
413 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum
, sum
));
415 (void)printf(" (correct),");
419 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !fragmented
) {
420 u_int16_t sum
,tcp_sum
;
421 if (TTEST2(tp
->th_sport
, length
)) {
422 sum
= tcp6_cksum(ip6
, tp
, length
);
423 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
425 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
426 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum
, sum
));
428 (void)printf(" (correct),");
435 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
436 (void)printf(" %u:%u(%u)", seq
, seq
+ length
, length
);
438 (void)printf(" ack %u", ack
);
440 (void)printf(" win %d", win
);
443 (void)printf(" urg %d", urp
);
445 * Handle any options.
447 if (hlen
> sizeof(*tp
)) {
448 register const u_char
*cp
;
449 register u_int i
, opt
, datalen
;
453 cp
= (const u_char
*)tp
+ sizeof(*tp
);
464 len
= *cp
++; /* total including type, len */
465 if (len
< 2 || len
> hlen
)
467 --hlen
; /* account for length byte */
469 --hlen
; /* account for type byte */
472 /* Bail if "l" bytes of data are not left or were not captured */
473 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
481 (void)printf(" %u", EXTRACT_16BITS(cp
));
494 (void)printf("wscale");
497 (void)printf(" %u", *cp
);
501 (void)printf("sackOK");
505 (void)printf("sack");
507 if (datalen
% 8 != 0) {
508 (void)printf(" malformed sack ");
512 (void)printf(" sack %d ", datalen
/ 8);
513 for (i
= 0; i
< datalen
; i
+= 8) {
515 s
= EXTRACT_32BITS(cp
+ i
);
517 e
= EXTRACT_32BITS(cp
+ i
+ 4);
525 (void)printf("{%u:%u}", s
, e
);
532 (void)printf("echo");
535 (void)printf(" %u", EXTRACT_32BITS(cp
));
538 case TCPOPT_ECHOREPLY
:
539 (void)printf("echoreply");
542 (void)printf(" %u", EXTRACT_32BITS(cp
));
545 case TCPOPT_TIMESTAMP
:
546 (void)printf("timestamp");
549 (void)printf(" %u", EXTRACT_32BITS(cp
));
551 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
558 (void)printf(" %u", EXTRACT_32BITS(cp
));
562 (void)printf("ccnew");
565 (void)printf(" %u", EXTRACT_32BITS(cp
));
569 (void)printf("ccecho");
572 (void)printf(" %u", EXTRACT_32BITS(cp
));
575 case TCPOPT_SIGNATURE
:
576 (void)printf("md5:");
577 datalen
= TCP_SIGLEN
;
579 #ifdef HAVE_LIBCRYPTO
580 if (tcp_verify_signature(ip
, tp
,
581 bp
+ TH_OFF(tp
) * 4, length
, cp
) == 0)
582 (void)printf("valid");
584 (void)printf("invalid");
586 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
587 (void)printf("%02x", cp
[i
]);
592 (void)printf("opt-%u:", opt
);
594 for (i
= 0; i
< datalen
; ++i
) {
596 (void)printf("%02x", cp
[i
]);
601 /* Account for data printed */
605 /* Check specification against observed length */
606 ++datalen
; /* option octet */
607 if (!ZEROLENOPT(opt
))
608 ++datalen
; /* size octet */
610 (void)printf("[len %d]", len
);
612 if (opt
== TCPOPT_EOL
)
622 * Decode payload if necessary.
624 bp
+= TH_OFF(tp
) * 4;
625 if (flags
& TH_RST
) {
627 print_tcp_rst_data(bp
, length
);
629 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
631 telnet_print(bp
, length
);
632 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
633 bgp_print(bp
, length
);
634 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
636 #ifdef TCPDUMP_DO_SMB
637 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
638 nbt_tcp_print(bp
, length
);
640 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
641 beep_print(bp
, length
);
642 else if (length
> 2 &&
643 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
644 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
646 * TCP DNS query has 2byte length at the head.
647 * XXX packet could be unaligned, it can go strange
649 ns_print(bp
+ 2, length
- 2, 0);
650 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
651 msdp_print(bp
, length
);
653 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
654 ldp_print(bp
, length
);
659 fputs("[bad opt]", stdout
);
664 fputs("[|tcp]", stdout
);
670 * RFC1122 says the following on data in RST segments:
672 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
674 * A TCP SHOULD allow a received RST segment to include data.
677 * It has been suggested that a RST segment could contain
678 * ASCII text that encoded and explained the cause of the
679 * RST. No standard has yet been established for such
685 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
689 if (TTEST2(*sp
, length
))
693 if (length
> MAX_RST_DATA_LEN
) {
694 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
695 putchar('+'); /* indicate we truncate */
698 while (length
-- && sp
<= snapend
) {
705 #ifdef HAVE_LIBCRYPTO
707 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
708 const u_char
*data
, int length
, const u_char
*rcvsig
)
711 char sig
[TCP_SIGLEN
];
714 u_int16_t savecsum
, tlen
;
721 if (tcpmd5secret
== NULL
)
726 * Step 1: Update MD5 hash with IP pseudo-header.
729 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
730 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
731 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
732 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
733 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
735 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
736 } else if (IP_V(ip
) == 6) {
737 ip6
= (struct ip6_hdr
*)ip
;
738 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
739 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
740 len32
= htonl(ntohs(ip6
->ip6_plen
));
741 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
743 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
744 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
745 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
747 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
752 * Step 2: Update MD5 hash with TCP header, excluding options.
753 * The TCP checksum must be set to zero.
755 savecsum
= tp1
.th_sum
;
757 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
758 tp1
.th_sum
= savecsum
;
760 * Step 3: Update MD5 hash with TCP segment data, if present.
763 MD5_Update(&ctx
, data
, length
);
765 * Step 4: Update MD5 hash with shared secret.
767 MD5_Update(&ctx
, tcpmd5secret
, strlen(tcpmd5secret
));
768 MD5_Final(sig
, &ctx
);
770 return (memcmp(rcvsig
, sig
, 16));
772 #endif /* HAVE_LIBCRYPTO */