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.129 2007-04-03 20:02:56 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
];
96 struct tok tcp_flag_values
[] = {
108 struct tok tcp_option_values
[] = {
109 { TCPOPT_EOL
, "eol" },
110 { TCPOPT_NOP
, "nop" },
111 { TCPOPT_MAXSEG
, "mss" },
112 { TCPOPT_WSCALE
, "wscale" },
113 { TCPOPT_SACKOK
, "sackOK" },
114 { TCPOPT_SACK
, "sack" },
115 { TCPOPT_ECHO
, "echo" },
116 { TCPOPT_ECHOREPLY
, "echoreply" },
117 { TCPOPT_TIMESTAMP
, "TS" },
119 { TCPOPT_CCNEW
, "ccnew" },
120 { TCPOPT_CCECHO
, "" },
121 { TCPOPT_SIGNATURE
, "md5" },
122 { TCPOPT_AUTH
, "enhanced auth" },
126 static int tcp_cksum(register const struct ip
*ip
,
127 register const struct tcphdr
*tp
,
142 /* pseudo-header.. */
143 phu
.ph
.len
= htons((u_int16_t
)len
);
145 phu
.ph
.proto
= IPPROTO_TCP
;
146 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
148 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
150 phu
.ph
.dst
= ip_finddst(ip
);
153 return in_cksum((u_short
*)tp
, len
,
154 sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
158 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
165 struct in6_addr ph_src
;
166 struct in6_addr ph_dst
;
175 memset(&phu
, 0, sizeof(phu
));
176 phu
.ph
.ph_src
= ip6
->ip6_src
;
177 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
178 phu
.ph
.ph_len
= htonl(len
);
179 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
181 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
184 return in_cksum((u_short
*)tp
, len
, sum
);
189 tcp_print(register const u_char
*bp
, register u_int length
,
190 register const u_char
*bp2
, int fragmented
)
192 register const struct tcphdr
*tp
;
193 register const struct ip
*ip
;
194 register u_char flags
;
197 u_int16_t sport
, dport
, win
, urp
;
198 u_int32_t seq
, ack
, thseq
, thack
;
201 register const struct ip6_hdr
*ip6
;
204 tp
= (struct tcphdr
*)bp
;
205 ip
= (struct ip
*)bp2
;
208 ip6
= (struct ip6_hdr
*)bp2
;
213 if (!TTEST(tp
->th_dport
)) {
214 (void)printf("%s > %s: [|tcp]",
215 ipaddr_string(&ip
->ip_src
),
216 ipaddr_string(&ip
->ip_dst
));
220 sport
= EXTRACT_16BITS(&tp
->th_sport
);
221 dport
= EXTRACT_16BITS(&tp
->th_dport
);
223 hlen
= TH_OFF(tp
) * 4;
226 * If data present, header length valid, and NFS port used,
228 * Pass offset of data plus 4 bytes for RPC TCP msg length
229 * to NFS print routines.
231 if (!qflag
&& hlen
>= sizeof(*tp
) && hlen
<= length
) {
232 if ((u_char
*)tp
+ 4 + sizeof(struct sunrpc_msg
) <= snapend
&&
234 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
237 } else if ((u_char
*)tp
+ 4 + sizeof(struct sunrpc_msg
)
240 nfsreply_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
247 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
248 (void)printf("%s.%s > %s.%s: ",
249 ip6addr_string(&ip6
->ip6_src
),
250 tcpport_string(sport
),
251 ip6addr_string(&ip6
->ip6_dst
),
252 tcpport_string(dport
));
254 (void)printf("%s > %s: ",
255 tcpport_string(sport
), tcpport_string(dport
));
260 if (ip
->ip_p
== IPPROTO_TCP
) {
261 (void)printf("%s.%s > %s.%s: ",
262 ipaddr_string(&ip
->ip_src
),
263 tcpport_string(sport
),
264 ipaddr_string(&ip
->ip_dst
),
265 tcpport_string(dport
));
267 (void)printf("%s > %s: ",
268 tcpport_string(sport
), tcpport_string(dport
));
272 if (hlen
< sizeof(*tp
)) {
273 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
274 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
280 seq
= EXTRACT_32BITS(&tp
->th_seq
);
281 ack
= EXTRACT_32BITS(&tp
->th_ack
);
282 win
= EXTRACT_16BITS(&tp
->th_win
);
283 urp
= EXTRACT_16BITS(&tp
->th_urp
);
286 (void)printf("tcp %d", length
- hlen
);
288 (void)printf(" [bad hdr length %u - too long, > %u]",
294 flags
= tp
->th_flags
;
295 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
297 if (!Sflag
&& (flags
& TH_ACK
)) {
298 register struct tcp_seq_hash
*th
;
299 const void *src
, *dst
;
303 * Find (or record) the initial sequence numbers for
304 * this conversation. (we pick an arbitrary
305 * collating order so there's only one entry for
309 memset(&tha
, 0, sizeof(tha
));
316 else if (sport
== dport
) {
317 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
321 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
322 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
323 tha
.port
= dport
<< 16 | sport
;
325 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
326 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
327 tha
.port
= sport
<< 16 | dport
;
334 else if (sport
== dport
) {
335 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
339 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
340 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
341 tha
.port
= dport
<< 16 | sport
;
343 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
344 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
345 tha
.port
= sport
<< 16 | dport
;
354 else if (sport
== dport
) {
355 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
359 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
360 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
361 tha
.port
= dport
<< 16 | sport
;
363 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
364 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
365 tha
.port
= sport
<< 16 | dport
;
370 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
371 th
->nxt
; th
= th
->nxt
)
372 if (memcmp((char *)&tha
, (char *)&th
->addr
,
373 sizeof(th
->addr
)) == 0)
376 if (!th
->nxt
|| (flags
& TH_SYN
)) {
377 /* didn't find it or new conversation */
378 if (th
->nxt
== NULL
) {
379 th
->nxt
= (struct tcp_seq_hash
*)
380 calloc(1, sizeof(*th
));
382 error("tcp_print: calloc");
386 th
->ack
= seq
, th
->seq
= ack
- 1;
388 th
->seq
= seq
, th
->ack
= ack
- 1;
391 seq
-= th
->ack
, ack
-= th
->seq
;
393 seq
-= th
->seq
, ack
-= th
->ack
;
400 thseq
= thack
= threv
= 0;
403 (void)printf(" [bad hdr length %u - too long, > %u]",
408 if (IP_V(ip
) == 4 && vflag
&& !Kflag
&& !fragmented
) {
409 u_int16_t sum
, tcp_sum
;
410 if (TTEST2(tp
->th_sport
, length
)) {
411 sum
= tcp_cksum(ip
, tp
, length
);
413 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
415 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
416 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum
, sum
));
418 (void)printf(" (correct)");
422 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !Kflag
&& !fragmented
) {
423 u_int16_t sum
,tcp_sum
;
424 if (TTEST2(tp
->th_sport
, length
)) {
425 sum
= tcp6_cksum(ip6
, tp
, length
);
426 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
428 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
429 (void)printf(" (incorrect (-> 0x%04x)",in_cksum_shouldbe(tcp_sum
, sum
));
431 (void)printf(" (correct)");
438 if (vflag
> 1 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
439 (void)printf(", seq %u", seq
);
442 (void)printf(":%u", seq
+ length
);
446 if (flags
& TH_ACK
) {
447 (void)printf(", ack %u", ack
);
450 (void)printf(", win %d", win
);
453 (void)printf(", urg %d", urp
);
455 * Handle any options.
457 if (hlen
> sizeof(*tp
)) {
458 register const u_char
*cp
;
459 register u_int i
, opt
, datalen
;
463 cp
= (const u_char
*)tp
+ sizeof(*tp
);
464 printf(", options [");
474 len
= *cp
++; /* total including type, len */
475 if (len
< 2 || len
> hlen
)
477 --hlen
; /* account for length byte */
479 --hlen
; /* account for type byte */
482 /* Bail if "l" bytes of data are not left or were not captured */
483 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
486 printf("%s", tok2str(tcp_option_values
, "Unknown Option %u", opt
));
493 (void)printf(" %u", EXTRACT_16BITS(cp
));
499 (void)printf(" %u", *cp
);
504 if (datalen
% 8 != 0) {
505 (void)printf("malformed sack");
509 (void)printf(" %d ", datalen
/ 8);
510 for (i
= 0; i
< datalen
; i
+= 8) {
512 s
= EXTRACT_32BITS(cp
+ i
);
514 e
= EXTRACT_32BITS(cp
+ i
+ 4);
522 (void)printf("{%u:%u}", s
, e
);
531 case TCPOPT_ECHOREPLY
:
534 * those options share their semantics.
539 (void)printf(" %u", EXTRACT_32BITS(cp
));
542 case TCPOPT_TIMESTAMP
:
545 (void)printf(" val %u ecr %u",
547 EXTRACT_32BITS(cp
+ 4));
550 case TCPOPT_SIGNATURE
:
551 datalen
= TCP_SIGLEN
;
553 #ifdef HAVE_LIBCRYPTO
554 switch (tcp_verify_signature(ip
, tp
,
555 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
557 case SIGNATURE_VALID
:
558 (void)printf("valid");
561 case SIGNATURE_INVALID
:
562 (void)printf("invalid");
565 case CANT_CHECK_SIGNATURE
:
566 (void)printf("can't check - ");
567 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
568 (void)printf("%02x", cp
[i
]);
572 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
573 (void)printf("%02x", cp
[i
]);
578 (void)printf("keyid %d", *cp
++);
580 for (i
= 0; i
< datalen
; ++i
) {
582 (void)printf("%02x", cp
[i
]);
591 * Nothing interesting.
598 for (i
= 0; i
< datalen
; ++i
) {
600 (void)printf("%02x", cp
[i
]);
605 /* Account for data printed */
609 /* Check specification against observed length */
610 ++datalen
; /* option octet */
611 if (!ZEROLENOPT(opt
))
612 ++datalen
; /* size octet */
614 (void)printf("[len %d]", len
);
616 if (opt
== TCPOPT_EOL
)
623 * Print length field before crawling down the stack.
625 printf(", length %u", length
);
631 * Decode payload if necessary.
633 bp
+= TH_OFF(tp
) * 4;
634 if ((flags
& TH_RST
) && vflag
) {
635 print_tcp_rst_data(bp
, length
);
639 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
641 telnet_print(bp
, length
);
642 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
643 bgp_print(bp
, length
);
644 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
646 #ifdef TCPDUMP_DO_SMB
647 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
648 nbt_tcp_print(bp
, length
);
650 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
651 beep_print(bp
, length
);
652 else if (length
> 2 &&
653 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
654 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
656 * TCP DNS query has 2byte length at the head.
657 * XXX packet could be unaligned, it can go strange
659 ns_print(bp
+ 2, length
- 2, 0);
660 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
661 msdp_print(bp
, length
);
663 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
664 ldp_print(bp
, length
);
669 fputs("[bad opt]", stdout
);
674 fputs("[|tcp]", stdout
);
680 * RFC1122 says the following on data in RST segments:
682 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
684 * A TCP SHOULD allow a received RST segment to include data.
687 * It has been suggested that a RST segment could contain
688 * ASCII text that encoded and explained the cause of the
689 * RST. No standard has yet been established for such
695 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
699 if (TTEST2(*sp
, length
))
703 if (length
> MAX_RST_DATA_LEN
) {
704 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
705 putchar('+'); /* indicate we truncate */
708 while (length
-- && sp
<= snapend
) {
715 #ifdef HAVE_LIBCRYPTO
717 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
718 const u_char
*data
, int length
, const u_char
*rcvsig
)
721 u_char sig
[TCP_SIGLEN
];
724 u_int16_t savecsum
, tlen
;
733 if (tcpmd5secret
== NULL
)
734 return (CANT_CHECK_SIGNATURE
);
738 * Step 1: Update MD5 hash with IP pseudo-header.
741 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
742 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
743 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
744 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
745 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
747 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
749 } else if (IP_V(ip
) == 6) {
750 ip6
= (struct ip6_hdr
*)ip
;
751 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
752 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
753 len32
= htonl(ntohs(ip6
->ip6_plen
));
754 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
756 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
757 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
758 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
760 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
763 return (CANT_CHECK_SIGNATURE
);
766 * Step 2: Update MD5 hash with TCP header, excluding options.
767 * The TCP checksum must be set to zero.
769 savecsum
= tp1
.th_sum
;
771 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
772 tp1
.th_sum
= savecsum
;
774 * Step 3: Update MD5 hash with TCP segment data, if present.
777 MD5_Update(&ctx
, data
, length
);
779 * Step 4: Update MD5 hash with shared secret.
781 MD5_Update(&ctx
, tcpmd5secret
, strlen(tcpmd5secret
));
782 MD5_Final(sig
, &ctx
);
784 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
785 return (SIGNATURE_VALID
);
787 return (SIGNATURE_INVALID
);
789 #endif /* HAVE_LIBCRYPTO */
793 * c-style: whitesmith