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 * Copyright (c) 1999-2004 The tcpdump.org project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 static const char rcsid
[] _U_
=
26 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.120.2.3 2005/10/16 06:05:46 guy Exp $ (LBL)";
33 #include <tcpdump-stdinc.h>
39 #include "interface.h"
40 #include "addrtoname.h"
56 #include <openssl/md5.h>
58 #define SIGNATURE_VALID 0
59 #define SIGNATURE_INVALID 1
60 #define CANT_CHECK_SIGNATURE 2
62 static int tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
63 const u_char
*data
, int length
, const u_char
*rcvsig
);
66 static void print_tcp_rst_data(register const u_char
*sp
, u_int length
);
68 #define MAX_RST_DATA_LEN 30
83 struct tcp_seq_hash
*nxt
;
89 #define TSEQ_HASHSIZE 919
91 /* These tcp optinos do not have the size octet */
92 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
94 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
98 #define TELNET_PORT 23
103 #define NETBIOS_SSN_PORT 139
105 #define PPTP_PORT 1723
107 #define BEEP_PORT 10288
109 #define NFS_PORT 2049
111 #define MSDP_PORT 639
114 static int tcp_cksum(register const struct ip
*ip
,
115 register const struct tcphdr
*tp
,
130 /* pseudo-header.. */
131 phu
.ph
.len
= htons((u_int16_t
)len
);
133 phu
.ph
.proto
= IPPROTO_TCP
;
134 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
136 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
138 phu
.ph
.dst
= ip_finddst(ip
);
141 return in_cksum((u_short
*)tp
, len
,
142 sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
146 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
150 register const u_int16_t
*sp
;
154 struct in6_addr ph_src
;
155 struct in6_addr ph_dst
;
164 memset(&phu
, 0, sizeof(phu
));
165 phu
.ph
.ph_src
= ip6
->ip6_src
;
166 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
167 phu
.ph
.ph_len
= htonl(len
);
168 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
171 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
174 sp
= (const u_int16_t
*)tp
;
176 for (i
= 0; i
< (len
& ~1); i
+= 2)
180 sum
+= htons((*(const u_int8_t
*)sp
) << 8);
183 sum
= (sum
& 0xffff) + (sum
>> 16);
191 tcp_print(register const u_char
*bp
, register u_int length
,
192 register const u_char
*bp2
, int fragmented
)
194 register const struct tcphdr
*tp
;
195 register const struct ip
*ip
;
196 register u_char flags
;
199 u_int16_t sport
, dport
, win
, urp
;
200 u_int32_t seq
, ack
, thseq
, thack
;
203 register const struct ip6_hdr
*ip6
;
206 tp
= (struct tcphdr
*)bp
;
207 ip
= (struct ip
*)bp2
;
210 ip6
= (struct ip6_hdr
*)bp2
;
215 if (!TTEST(tp
->th_dport
)) {
216 (void)printf("%s > %s: [|tcp]",
217 ipaddr_string(&ip
->ip_src
),
218 ipaddr_string(&ip
->ip_dst
));
222 sport
= EXTRACT_16BITS(&tp
->th_sport
);
223 dport
= EXTRACT_16BITS(&tp
->th_dport
);
225 hlen
= TH_OFF(tp
) * 4;
228 * If data present, header length valid, and NFS port used,
230 * Pass offset of data plus 4 bytes for RPC TCP msg length
231 * to NFS print routines.
233 if (!qflag
&& hlen
>= sizeof(*tp
) && hlen
<= length
) {
234 if ((u_char
*)tp
+ 4 + sizeof(struct sunrpc_msg
) <= snapend
&&
236 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
239 } else if ((u_char
*)tp
+ 4 + sizeof(struct sunrpc_msg
)
242 nfsreply_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
249 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
250 (void)printf("%s.%s > %s.%s: ",
251 ip6addr_string(&ip6
->ip6_src
),
252 tcpport_string(sport
),
253 ip6addr_string(&ip6
->ip6_dst
),
254 tcpport_string(dport
));
256 (void)printf("%s > %s: ",
257 tcpport_string(sport
), tcpport_string(dport
));
262 if (ip
->ip_p
== IPPROTO_TCP
) {
263 (void)printf("%s.%s > %s.%s: ",
264 ipaddr_string(&ip
->ip_src
),
265 tcpport_string(sport
),
266 ipaddr_string(&ip
->ip_dst
),
267 tcpport_string(dport
));
269 (void)printf("%s > %s: ",
270 tcpport_string(sport
), tcpport_string(dport
));
274 if (hlen
< sizeof(*tp
)) {
275 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
276 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
282 seq
= EXTRACT_32BITS(&tp
->th_seq
);
283 ack
= EXTRACT_32BITS(&tp
->th_ack
);
284 win
= EXTRACT_16BITS(&tp
->th_win
);
285 urp
= EXTRACT_16BITS(&tp
->th_urp
);
288 (void)printf("tcp %d", length
- hlen
);
290 (void)printf(" [bad hdr length %u - too long, > %u]",
295 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
296 TH_ECNECHO
|TH_CWR
)) {
306 putchar('W'); /* congestion _W_indow reduced (ECN) */
307 if (flags
& TH_ECNECHO
)
308 putchar('E'); /* ecn _E_cho sent (ECN) */
312 if (!Sflag
&& (flags
& TH_ACK
)) {
313 register struct tcp_seq_hash
*th
;
314 const void *src
, *dst
;
318 * Find (or record) the initial sequence numbers for
319 * this conversation. (we pick an arbitrary
320 * collating order so there's only one entry for
324 memset(&tha
, 0, sizeof(tha
));
331 else if (sport
== dport
) {
332 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
336 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
337 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
338 tha
.port
= dport
<< 16 | sport
;
340 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
341 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
342 tha
.port
= sport
<< 16 | dport
;
349 else if (sport
== dport
) {
350 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
354 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
355 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
356 tha
.port
= dport
<< 16 | sport
;
358 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
359 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
360 tha
.port
= sport
<< 16 | dport
;
369 else if (sport
== dport
) {
370 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
374 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
375 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
376 tha
.port
= dport
<< 16 | sport
;
378 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
379 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
380 tha
.port
= sport
<< 16 | dport
;
385 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
386 th
->nxt
; th
= th
->nxt
)
387 if (memcmp((char *)&tha
, (char *)&th
->addr
,
388 sizeof(th
->addr
)) == 0)
391 if (!th
->nxt
|| (flags
& TH_SYN
)) {
392 /* didn't find it or new conversation */
393 if (th
->nxt
== NULL
) {
394 th
->nxt
= (struct tcp_seq_hash
*)
395 calloc(1, sizeof(*th
));
397 error("tcp_print: calloc");
401 th
->ack
= seq
, th
->seq
= ack
- 1;
403 th
->seq
= seq
, th
->ack
= ack
- 1;
406 seq
-= th
->ack
, ack
-= th
->seq
;
408 seq
-= th
->seq
, ack
-= th
->ack
;
415 thseq
= thack
= threv
= 0;
418 (void)printf(" [bad hdr length %u - too long, > %u]",
423 if (IP_V(ip
) == 4 && vflag
&& !fragmented
) {
424 u_int16_t sum
, tcp_sum
;
425 if (TTEST2(tp
->th_sport
, length
)) {
426 sum
= tcp_cksum(ip
, tp
, length
);
428 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
430 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
431 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum
, sum
));
433 (void)printf(" (correct),");
437 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !fragmented
) {
438 u_int16_t sum
,tcp_sum
;
439 if (TTEST2(tp
->th_sport
, length
)) {
440 sum
= tcp6_cksum(ip6
, tp
, length
);
441 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
443 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
444 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum
, sum
));
446 (void)printf(" (correct),");
453 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
454 (void)printf(" %u:%u(%u)", seq
, seq
+ length
, length
);
456 (void)printf(" ack %u", ack
);
458 (void)printf(" win %d", win
);
461 (void)printf(" urg %d", urp
);
463 * Handle any options.
465 if (hlen
> sizeof(*tp
)) {
466 register const u_char
*cp
;
467 register u_int i
, opt
, datalen
;
471 cp
= (const u_char
*)tp
+ sizeof(*tp
);
482 len
= *cp
++; /* total including type, len */
483 if (len
< 2 || len
> hlen
)
485 --hlen
; /* account for length byte */
487 --hlen
; /* account for type byte */
490 /* Bail if "l" bytes of data are not left or were not captured */
491 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
499 (void)printf(" %u", EXTRACT_16BITS(cp
));
512 (void)printf("wscale");
515 (void)printf(" %u", *cp
);
519 (void)printf("sackOK");
524 if (datalen
% 8 != 0) {
525 (void)printf("malformed sack");
529 (void)printf("sack %d ", datalen
/ 8);
530 for (i
= 0; i
< datalen
; i
+= 8) {
532 s
= EXTRACT_32BITS(cp
+ i
);
534 e
= EXTRACT_32BITS(cp
+ i
+ 4);
542 (void)printf("{%u:%u}", s
, e
);
548 (void)printf("echo");
551 (void)printf(" %u", EXTRACT_32BITS(cp
));
554 case TCPOPT_ECHOREPLY
:
555 (void)printf("echoreply");
558 (void)printf(" %u", EXTRACT_32BITS(cp
));
561 case TCPOPT_TIMESTAMP
:
562 (void)printf("timestamp");
565 (void)printf(" %u", EXTRACT_32BITS(cp
));
567 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
574 (void)printf(" %u", EXTRACT_32BITS(cp
));
578 (void)printf("ccnew");
581 (void)printf(" %u", EXTRACT_32BITS(cp
));
585 (void)printf("ccecho");
588 (void)printf(" %u", EXTRACT_32BITS(cp
));
591 case TCPOPT_SIGNATURE
:
592 (void)printf("md5:");
593 datalen
= TCP_SIGLEN
;
595 #ifdef HAVE_LIBCRYPTO
596 switch (tcp_verify_signature(ip
, tp
,
597 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
599 case SIGNATURE_VALID
:
600 (void)printf("valid");
603 case SIGNATURE_INVALID
:
604 (void)printf("invalid");
607 case CANT_CHECK_SIGNATURE
:
608 (void)printf("can't check - ");
609 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
610 (void)printf("%02x", cp
[i
]);
614 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
615 (void)printf("%02x", cp
[i
]);
620 (void)printf("opt-%u:", opt
);
622 for (i
= 0; i
< datalen
; ++i
) {
624 (void)printf("%02x", cp
[i
]);
629 /* Account for data printed */
633 /* Check specification against observed length */
634 ++datalen
; /* option octet */
635 if (!ZEROLENOPT(opt
))
636 ++datalen
; /* size octet */
638 (void)printf("[len %d]", len
);
640 if (opt
== TCPOPT_EOL
)
650 * Decode payload if necessary.
652 bp
+= TH_OFF(tp
) * 4;
653 if (flags
& TH_RST
) {
655 print_tcp_rst_data(bp
, length
);
657 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
659 telnet_print(bp
, length
);
660 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
661 bgp_print(bp
, length
);
662 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
664 #ifdef TCPDUMP_DO_SMB
665 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
666 nbt_tcp_print(bp
, length
);
668 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
669 beep_print(bp
, length
);
670 else if (length
> 2 &&
671 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
672 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
674 * TCP DNS query has 2byte length at the head.
675 * XXX packet could be unaligned, it can go strange
677 ns_print(bp
+ 2, length
- 2, 0);
678 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
679 msdp_print(bp
, length
);
681 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
682 ldp_print(bp
, length
);
687 fputs("[bad opt]", stdout
);
692 fputs("[|tcp]", stdout
);
698 * RFC1122 says the following on data in RST segments:
700 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
702 * A TCP SHOULD allow a received RST segment to include data.
705 * It has been suggested that a RST segment could contain
706 * ASCII text that encoded and explained the cause of the
707 * RST. No standard has yet been established for such
713 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
717 if (TTEST2(*sp
, length
))
721 if (length
> MAX_RST_DATA_LEN
) {
722 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
723 putchar('+'); /* indicate we truncate */
726 while (length
-- && sp
<= snapend
) {
733 #ifdef HAVE_LIBCRYPTO
735 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
736 const u_char
*data
, int length
, const u_char
*rcvsig
)
739 u_char sig
[TCP_SIGLEN
];
742 u_int16_t savecsum
, tlen
;
751 if (tcpmd5secret
== NULL
)
752 return (CANT_CHECK_SIGNATURE
);
756 * Step 1: Update MD5 hash with IP pseudo-header.
759 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
760 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
761 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
762 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
763 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
765 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
767 } else if (IP_V(ip
) == 6) {
768 ip6
= (struct ip6_hdr
*)ip
;
769 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
770 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
771 len32
= htonl(ntohs(ip6
->ip6_plen
));
772 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
774 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
775 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
776 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
778 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
781 return (CANT_CHECK_SIGNATURE
);
784 * Step 2: Update MD5 hash with TCP header, excluding options.
785 * The TCP checksum must be set to zero.
787 savecsum
= tp1
.th_sum
;
789 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
790 tp1
.th_sum
= savecsum
;
792 * Step 3: Update MD5 hash with TCP segment data, if present.
795 MD5_Update(&ctx
, data
, length
);
797 * Step 4: Update MD5 hash with shared secret.
799 MD5_Update(&ctx
, tcpmd5secret
, strlen(tcpmd5secret
));
800 MD5_Final(sig
, &ctx
);
802 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
803 return (SIGNATURE_VALID
);
805 return (SIGNATURE_INVALID
);
807 #endif /* HAVE_LIBCRYPTO */