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.126 2006-11-02 08:56:16 hannes 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
,
153 struct in6_addr ph_src
;
154 struct in6_addr ph_dst
;
163 memset(&phu
, 0, sizeof(phu
));
164 phu
.ph
.ph_src
= ip6
->ip6_src
;
165 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
166 phu
.ph
.ph_len
= htonl(len
);
167 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
169 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
172 return in_cksum((u_short
*)tp
, len
, sum
);
177 tcp_print(register const u_char
*bp
, register u_int length
,
178 register const u_char
*bp2
, int fragmented
)
180 register const struct tcphdr
*tp
;
181 register const struct ip
*ip
;
182 register u_char flags
;
185 u_int16_t sport
, dport
, win
, urp
;
186 u_int32_t seq
, ack
, thseq
, thack
;
189 register const struct ip6_hdr
*ip6
;
192 tp
= (struct tcphdr
*)bp
;
193 ip
= (struct ip
*)bp2
;
196 ip6
= (struct ip6_hdr
*)bp2
;
201 if (!TTEST(tp
->th_dport
)) {
202 (void)printf("%s > %s: [|tcp]",
203 ipaddr_string(&ip
->ip_src
),
204 ipaddr_string(&ip
->ip_dst
));
208 sport
= EXTRACT_16BITS(&tp
->th_sport
);
209 dport
= EXTRACT_16BITS(&tp
->th_dport
);
211 hlen
= TH_OFF(tp
) * 4;
214 * If data present, header length valid, and NFS port used,
216 * Pass offset of data plus 4 bytes for RPC TCP msg length
217 * to NFS print routines.
219 if (!qflag
&& hlen
>= sizeof(*tp
) && hlen
<= length
) {
220 if ((u_char
*)tp
+ 4 + sizeof(struct sunrpc_msg
) <= snapend
&&
222 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
225 } else if ((u_char
*)tp
+ 4 + sizeof(struct sunrpc_msg
)
228 nfsreply_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
235 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
236 (void)printf("%s.%s > %s.%s: ",
237 ip6addr_string(&ip6
->ip6_src
),
238 tcpport_string(sport
),
239 ip6addr_string(&ip6
->ip6_dst
),
240 tcpport_string(dport
));
242 (void)printf("%s > %s: ",
243 tcpport_string(sport
), tcpport_string(dport
));
248 if (ip
->ip_p
== IPPROTO_TCP
) {
249 (void)printf("%s.%s > %s.%s: ",
250 ipaddr_string(&ip
->ip_src
),
251 tcpport_string(sport
),
252 ipaddr_string(&ip
->ip_dst
),
253 tcpport_string(dport
));
255 (void)printf("%s > %s: ",
256 tcpport_string(sport
), tcpport_string(dport
));
260 if (hlen
< sizeof(*tp
)) {
261 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
262 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
268 seq
= EXTRACT_32BITS(&tp
->th_seq
);
269 ack
= EXTRACT_32BITS(&tp
->th_ack
);
270 win
= EXTRACT_16BITS(&tp
->th_win
);
271 urp
= EXTRACT_16BITS(&tp
->th_urp
);
274 (void)printf("tcp %d", length
- hlen
);
276 (void)printf(" [bad hdr length %u - too long, > %u]",
281 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
282 TH_ECNECHO
|TH_CWR
)) {
292 putchar('W'); /* congestion _W_indow reduced (ECN) */
293 if (flags
& TH_ECNECHO
)
294 putchar('E'); /* ecn _E_cho sent (ECN) */
298 if (!Sflag
&& (flags
& TH_ACK
)) {
299 register struct tcp_seq_hash
*th
;
300 const void *src
, *dst
;
304 * Find (or record) the initial sequence numbers for
305 * this conversation. (we pick an arbitrary
306 * collating order so there's only one entry for
310 memset(&tha
, 0, sizeof(tha
));
317 else if (sport
== dport
) {
318 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
322 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
323 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
324 tha
.port
= dport
<< 16 | sport
;
326 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
327 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
328 tha
.port
= sport
<< 16 | dport
;
335 else if (sport
== dport
) {
336 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
340 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
341 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
342 tha
.port
= dport
<< 16 | sport
;
344 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
345 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
346 tha
.port
= sport
<< 16 | dport
;
355 else if (sport
== dport
) {
356 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
360 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
361 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
362 tha
.port
= dport
<< 16 | sport
;
364 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
365 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
366 tha
.port
= sport
<< 16 | dport
;
371 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
372 th
->nxt
; th
= th
->nxt
)
373 if (memcmp((char *)&tha
, (char *)&th
->addr
,
374 sizeof(th
->addr
)) == 0)
377 if (!th
->nxt
|| (flags
& TH_SYN
)) {
378 /* didn't find it or new conversation */
379 if (th
->nxt
== NULL
) {
380 th
->nxt
= (struct tcp_seq_hash
*)
381 calloc(1, sizeof(*th
));
383 error("tcp_print: calloc");
387 th
->ack
= seq
, th
->seq
= ack
- 1;
389 th
->seq
= seq
, th
->ack
= ack
- 1;
392 seq
-= th
->ack
, ack
-= th
->seq
;
394 seq
-= th
->seq
, ack
-= th
->ack
;
401 thseq
= thack
= threv
= 0;
404 (void)printf(" [bad hdr length %u - too long, > %u]",
409 if (IP_V(ip
) == 4 && vflag
&& !Kflag
&& !fragmented
) {
410 u_int16_t sum
, tcp_sum
;
411 if (TTEST2(tp
->th_sport
, length
)) {
412 sum
= tcp_cksum(ip
, tp
, length
);
414 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
416 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
417 (void)printf(" (incorrect -> 0x%04x),",in_cksum_shouldbe(tcp_sum
, sum
));
419 (void)printf(" (correct),");
423 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !Kflag
&& !fragmented
) {
424 u_int16_t sum
,tcp_sum
;
425 if (TTEST2(tp
->th_sport
, length
)) {
426 sum
= tcp6_cksum(ip6
, tp
, length
);
427 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
429 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
430 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum
, sum
));
432 (void)printf(" (correct),");
439 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
440 (void)printf(" %u:%u(%u)", seq
, seq
+ length
, length
);
442 (void)printf(" ack %u", ack
);
444 (void)printf(" win %d", win
);
447 (void)printf(" urg %d", urp
);
449 * Handle any options.
451 if (hlen
> sizeof(*tp
)) {
452 register const u_char
*cp
;
453 register u_int i
, opt
, datalen
;
457 cp
= (const u_char
*)tp
+ sizeof(*tp
);
468 len
= *cp
++; /* total including type, len */
469 if (len
< 2 || len
> hlen
)
471 --hlen
; /* account for length byte */
473 --hlen
; /* account for type byte */
476 /* Bail if "l" bytes of data are not left or were not captured */
477 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
485 (void)printf(" %u", EXTRACT_16BITS(cp
));
498 (void)printf("wscale");
501 (void)printf(" %u", *cp
);
505 (void)printf("sackOK");
510 if (datalen
% 8 != 0) {
511 (void)printf("malformed sack");
515 (void)printf("sack %d ", datalen
/ 8);
516 for (i
= 0; i
< datalen
; i
+= 8) {
518 s
= EXTRACT_32BITS(cp
+ i
);
520 e
= EXTRACT_32BITS(cp
+ i
+ 4);
528 (void)printf("{%u:%u}", s
, e
);
534 (void)printf("echo");
537 (void)printf(" %u", EXTRACT_32BITS(cp
));
540 case TCPOPT_ECHOREPLY
:
541 (void)printf("echoreply");
544 (void)printf(" %u", EXTRACT_32BITS(cp
));
547 case TCPOPT_TIMESTAMP
:
548 (void)printf("timestamp");
551 (void)printf(" %u", EXTRACT_32BITS(cp
));
553 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
560 (void)printf(" %u", EXTRACT_32BITS(cp
));
564 (void)printf("ccnew");
567 (void)printf(" %u", EXTRACT_32BITS(cp
));
571 (void)printf("ccecho");
574 (void)printf(" %u", EXTRACT_32BITS(cp
));
577 case TCPOPT_SIGNATURE
:
578 (void)printf("md5:");
579 datalen
= TCP_SIGLEN
;
581 #ifdef HAVE_LIBCRYPTO
582 switch (tcp_verify_signature(ip
, tp
,
583 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
585 case SIGNATURE_VALID
:
586 (void)printf("valid");
589 case SIGNATURE_INVALID
:
590 (void)printf("invalid");
593 case CANT_CHECK_SIGNATURE
:
594 (void)printf("can't check - ");
595 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
596 (void)printf("%02x", cp
[i
]);
600 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
601 (void)printf("%02x", cp
[i
]);
606 (void)printf("Enhanced Auth: keyid %d", *cp
++);
608 for (i
= 0; i
< datalen
; ++i
) {
610 (void)printf("%02x", cp
[i
]);
615 (void)printf("opt-%u:", opt
);
617 for (i
= 0; i
< datalen
; ++i
) {
619 (void)printf("%02x", cp
[i
]);
624 /* Account for data printed */
628 /* Check specification against observed length */
629 ++datalen
; /* option octet */
630 if (!ZEROLENOPT(opt
))
631 ++datalen
; /* size octet */
633 (void)printf("[len %d]", len
);
635 if (opt
== TCPOPT_EOL
)
645 * Decode payload if necessary.
647 bp
+= TH_OFF(tp
) * 4;
648 if (flags
& TH_RST
) {
650 print_tcp_rst_data(bp
, length
);
652 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
654 telnet_print(bp
, length
);
655 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
656 bgp_print(bp
, length
);
657 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
659 #ifdef TCPDUMP_DO_SMB
660 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
661 nbt_tcp_print(bp
, length
);
663 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
664 beep_print(bp
, length
);
665 else if (length
> 2 &&
666 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
667 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
669 * TCP DNS query has 2byte length at the head.
670 * XXX packet could be unaligned, it can go strange
672 ns_print(bp
+ 2, length
- 2, 0);
673 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
674 msdp_print(bp
, length
);
676 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
677 ldp_print(bp
, length
);
682 fputs("[bad opt]", stdout
);
687 fputs("[|tcp]", stdout
);
693 * RFC1122 says the following on data in RST segments:
695 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
697 * A TCP SHOULD allow a received RST segment to include data.
700 * It has been suggested that a RST segment could contain
701 * ASCII text that encoded and explained the cause of the
702 * RST. No standard has yet been established for such
708 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
712 if (TTEST2(*sp
, length
))
716 if (length
> MAX_RST_DATA_LEN
) {
717 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
718 putchar('+'); /* indicate we truncate */
721 while (length
-- && sp
<= snapend
) {
728 #ifdef HAVE_LIBCRYPTO
730 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
731 const u_char
*data
, int length
, const u_char
*rcvsig
)
734 u_char sig
[TCP_SIGLEN
];
737 u_int16_t savecsum
, tlen
;
746 if (tcpmd5secret
== NULL
)
747 return (CANT_CHECK_SIGNATURE
);
751 * Step 1: Update MD5 hash with IP pseudo-header.
754 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
755 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
756 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
757 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
758 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
760 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
762 } else if (IP_V(ip
) == 6) {
763 ip6
= (struct ip6_hdr
*)ip
;
764 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
765 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
766 len32
= htonl(ntohs(ip6
->ip6_plen
));
767 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
769 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
770 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
771 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
773 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
776 return (CANT_CHECK_SIGNATURE
);
779 * Step 2: Update MD5 hash with TCP header, excluding options.
780 * The TCP checksum must be set to zero.
782 savecsum
= tp1
.th_sum
;
784 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
785 tp1
.th_sum
= savecsum
;
787 * Step 3: Update MD5 hash with TCP segment data, if present.
790 MD5_Update(&ctx
, data
, length
);
792 * Step 4: Update MD5 hash with shared secret.
794 MD5_Update(&ctx
, tcpmd5secret
, strlen(tcpmd5secret
));
795 MD5_Final(sig
, &ctx
);
797 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
798 return (SIGNATURE_VALID
);
800 return (SIGNATURE_INVALID
);
802 #endif /* HAVE_LIBCRYPTO */