1 /* $NetBSD: print-tcp.c,v 1.9 2007/07/26 18:15:12 plunky Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
7 * Copyright (c) 1999-2004 The tcpdump.org project
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that: (1) source code distributions
11 * retain the above copyright notice and this paragraph in its entirety, (2)
12 * distributions including binary code include the above copyright notice and
13 * this paragraph in its entirety in the documentation or other materials
14 * provided with the distribution, and (3) all advertising materials mentioning
15 * features or use of this software display the following acknowledgement:
16 * ``This product includes software developed by the University of California,
17 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
18 * the University nor the names of its contributors may be used to endorse
19 * or promote products derived from this software without specific prior
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 static const char rcsid
[] _U_
=
28 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.135 2008-11-09 23:35:03 mcr Exp $ (LBL)";
30 __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
37 #include <tcpdump-stdinc.h>
43 #include "interface.h"
44 #include "addrtoname.h"
60 #include <openssl/md5.h>
61 #include <signature.h>
63 static int tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
64 const u_char
*data
, int length
, const u_char
*rcvsig
);
67 static void print_tcp_rst_data(register const u_char
*sp
, u_int length
);
69 #define MAX_RST_DATA_LEN 30
84 struct tcp_seq_hash
*nxt
;
90 #define TSEQ_HASHSIZE 919
92 /* These tcp optinos do not have the size octet */
93 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
95 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
97 struct tok tcp_flag_values
[] = {
109 struct tok tcp_option_values
[] = {
110 { TCPOPT_EOL
, "eol" },
111 { TCPOPT_NOP
, "nop" },
112 { TCPOPT_MAXSEG
, "mss" },
113 { TCPOPT_WSCALE
, "wscale" },
114 { TCPOPT_SACKOK
, "sackOK" },
115 { TCPOPT_SACK
, "sack" },
116 { TCPOPT_ECHO
, "echo" },
117 { TCPOPT_ECHOREPLY
, "echoreply" },
118 { TCPOPT_TIMESTAMP
, "TS" },
120 { TCPOPT_CCNEW
, "ccnew" },
121 { TCPOPT_CCECHO
, "" },
122 { TCPOPT_SIGNATURE
, "md5" },
123 { TCPOPT_AUTH
, "enhanced auth" },
124 { TCPOPT_UTO
, "uto" },
128 static int tcp_cksum(register const struct ip
*ip
,
129 register const struct tcphdr
*tp
,
144 /* pseudo-header.. */
145 phu
.ph
.len
= htons((u_int16_t
)len
);
147 phu
.ph
.proto
= IPPROTO_TCP
;
148 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
150 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
152 phu
.ph
.dst
= ip_finddst(ip
);
155 return in_cksum((u_short
*)tp
, len
,
156 sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
160 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
167 struct in6_addr ph_src
;
168 struct in6_addr ph_dst
;
177 memset(&phu
, 0, sizeof(phu
));
178 phu
.ph
.ph_src
= ip6
->ip6_src
;
179 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
180 phu
.ph
.ph_len
= htonl(len
);
181 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
183 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
186 return in_cksum((u_short
*)tp
, len
, sum
);
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
;
204 register const struct ip6_hdr
*ip6
;
207 tp
= (struct tcphdr
*)bp
;
208 ip
= (struct ip
*)bp2
;
211 ip6
= (struct ip6_hdr
*)bp2
;
216 if (!TTEST(tp
->th_dport
)) {
217 (void)printf("%s > %s: [|tcp]",
218 ipaddr_string(&ip
->ip_src
),
219 ipaddr_string(&ip
->ip_dst
));
223 sport
= EXTRACT_16BITS(&tp
->th_sport
);
224 dport
= EXTRACT_16BITS(&tp
->th_dport
);
226 hlen
= TH_OFF(tp
) * 4;
229 * If data present, header length valid, and NFS port used,
231 * Pass offset of data plus 4 bytes for RPC TCP msg length
232 * to NFS print routines.
234 if (!qflag
&& hlen
>= sizeof(*tp
) && hlen
<= length
&&
235 (length
- hlen
) >= 4) {
238 register struct sunrpc_msg
*rp
;
239 enum sunrpc_msg_type direction
;
241 fraglenp
= (u_char
*)tp
+ hlen
;
242 if (TTEST2(*fraglenp
, 4)) {
243 fraglen
= EXTRACT_32BITS(fraglenp
) & 0x7FFFFFFF;
244 if (fraglen
> (length
- hlen
) - 4)
245 fraglen
= (length
- hlen
) - 4;
246 rp
= (struct sunrpc_msg
*)(fraglenp
+ 4);
247 if (TTEST(rp
->rm_direction
)) {
248 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
249 if (dport
== NFS_PORT
&&
250 direction
== SUNRPC_CALL
) {
251 nfsreq_print((u_char
*)rp
, fraglen
,
255 if (sport
== NFS_PORT
&&
256 direction
== SUNRPC_REPLY
) {
257 nfsreply_print((u_char
*)rp
, fraglen
,
266 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
267 (void)printf("%s.%s > %s.%s: ",
268 ip6addr_string(&ip6
->ip6_src
),
269 tcpport_string(sport
),
270 ip6addr_string(&ip6
->ip6_dst
),
271 tcpport_string(dport
));
273 (void)printf("%s > %s: ",
274 tcpport_string(sport
), tcpport_string(dport
));
279 if (ip
->ip_p
== IPPROTO_TCP
) {
280 (void)printf("%s.%s > %s.%s: ",
281 ipaddr_string(&ip
->ip_src
),
282 tcpport_string(sport
),
283 ipaddr_string(&ip
->ip_dst
),
284 tcpport_string(dport
));
286 (void)printf("%s > %s: ",
287 tcpport_string(sport
), tcpport_string(dport
));
291 if (hlen
< sizeof(*tp
)) {
292 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
293 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
299 seq
= EXTRACT_32BITS(&tp
->th_seq
);
300 ack
= EXTRACT_32BITS(&tp
->th_ack
);
301 win
= EXTRACT_16BITS(&tp
->th_win
);
302 urp
= EXTRACT_16BITS(&tp
->th_urp
);
305 (void)printf("tcp %d", length
- hlen
);
307 (void)printf(" [bad hdr length %u - too long, > %u]",
313 flags
= tp
->th_flags
;
314 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
316 if (!Sflag
&& (flags
& TH_ACK
)) {
317 register struct tcp_seq_hash
*th
;
318 const void *src
, *dst
;
322 * Find (or record) the initial sequence numbers for
323 * this conversation. (we pick an arbitrary
324 * collating order so there's only one entry for
328 memset(&tha
, 0, sizeof(tha
));
335 else if (sport
== dport
) {
336 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
340 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
341 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
342 tha
.port
= dport
<< 16 | sport
;
344 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
345 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
346 tha
.port
= sport
<< 16 | dport
;
353 else if (sport
== dport
) {
354 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
358 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
359 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
360 tha
.port
= dport
<< 16 | sport
;
362 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
363 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
364 tha
.port
= sport
<< 16 | dport
;
373 else if (sport
== dport
) {
374 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
378 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
379 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
380 tha
.port
= dport
<< 16 | sport
;
382 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
383 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
384 tha
.port
= sport
<< 16 | dport
;
389 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
390 th
->nxt
; th
= th
->nxt
)
391 if (memcmp((char *)&tha
, (char *)&th
->addr
,
392 sizeof(th
->addr
)) == 0)
395 if (!th
->nxt
|| (flags
& TH_SYN
)) {
396 /* didn't find it or new conversation */
397 if (th
->nxt
== NULL
) {
398 th
->nxt
= (struct tcp_seq_hash
*)
399 calloc(1, sizeof(*th
));
401 error("tcp_print: calloc");
405 th
->ack
= seq
, th
->seq
= ack
- 1;
407 th
->seq
= seq
, th
->ack
= ack
- 1;
410 seq
-= th
->ack
, ack
-= th
->seq
;
412 seq
-= th
->seq
, ack
-= th
->ack
;
419 thseq
= thack
= threv
= 0;
422 (void)printf(" [bad hdr length %u - too long, > %u]",
427 if (IP_V(ip
) == 4 && vflag
&& !Kflag
&& !fragmented
) {
428 u_int16_t sum
, tcp_sum
;
429 if (TTEST2(tp
->th_sport
, length
)) {
430 sum
= tcp_cksum(ip
, tp
, length
);
432 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
434 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
435 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum
, sum
));
437 (void)printf(" (correct)");
441 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !Kflag
&& !fragmented
) {
442 u_int16_t sum
,tcp_sum
;
443 if (TTEST2(tp
->th_sport
, length
)) {
444 sum
= tcp6_cksum(ip6
, tp
, length
);
445 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
447 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
448 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum
, sum
));
450 (void)printf(" (correct)");
457 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
458 (void)printf(", seq %u", seq
);
461 (void)printf(":%u", seq
+ length
);
465 if (flags
& TH_ACK
) {
466 (void)printf(", ack %u", ack
);
469 (void)printf(", win %d", win
);
472 (void)printf(", urg %d", urp
);
474 * Handle any options.
476 if (hlen
> sizeof(*tp
)) {
477 register const u_char
*cp
;
478 register u_int i
, opt
, datalen
;
482 cp
= (const u_char
*)tp
+ sizeof(*tp
);
483 printf(", options [");
493 len
= *cp
++; /* total including type, len */
494 if (len
< 2 || len
> hlen
)
496 --hlen
; /* account for length byte */
498 --hlen
; /* account for type byte */
501 /* Bail if "l" bytes of data are not left or were not captured */
502 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
505 printf("%s", tok2str(tcp_option_values
, "Unknown Option %u", opt
));
512 (void)printf(" %u", EXTRACT_16BITS(cp
));
518 (void)printf(" %u", *cp
);
523 if (datalen
% 8 != 0) {
524 (void)printf("malformed sack");
528 (void)printf(" %d ", datalen
/ 8);
529 for (i
= 0; i
< datalen
; i
+= 8) {
531 s
= EXTRACT_32BITS(cp
+ i
);
533 e
= EXTRACT_32BITS(cp
+ i
+ 4);
541 (void)printf("{%u:%u}", s
, e
);
550 case TCPOPT_ECHOREPLY
:
553 * those options share their semantics.
558 (void)printf(" %u", EXTRACT_32BITS(cp
));
561 case TCPOPT_TIMESTAMP
:
564 (void)printf(" val %u ecr %u",
566 EXTRACT_32BITS(cp
+ 4));
569 case TCPOPT_SIGNATURE
:
570 datalen
= TCP_SIGLEN
;
572 #ifdef HAVE_LIBCRYPTO
573 switch (tcp_verify_signature(ip
, tp
,
574 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
576 case SIGNATURE_VALID
:
577 (void)printf("valid");
580 case SIGNATURE_INVALID
:
581 (void)printf("invalid");
584 case CANT_CHECK_SIGNATURE
:
585 (void)printf("can't check - ");
586 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
587 (void)printf("%02x", cp
[i
]);
591 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
592 (void)printf("%02x", cp
[i
]);
597 (void)printf("keyid %d", *cp
++);
599 for (i
= 0; i
< datalen
; ++i
) {
601 (void)printf("%02x", cp
[i
]);
610 * Nothing interesting.
618 utoval
= EXTRACT_16BITS(cp
);
619 (void)printf("0x%x", utoval
);
621 utoval
= (utoval
>> 1) * 60;
624 (void)printf(" %u", utoval
);
629 for (i
= 0; i
< datalen
; ++i
) {
631 (void)printf("%02x", cp
[i
]);
636 /* Account for data printed */
640 /* Check specification against observed length */
641 ++datalen
; /* option octet */
642 if (!ZEROLENOPT(opt
))
643 ++datalen
; /* size octet */
645 (void)printf("[len %d]", len
);
647 if (opt
== TCPOPT_EOL
)
654 * Print length field before crawling down the stack.
656 printf(", length %u", length
);
662 * Decode payload if necessary.
664 bp
+= TH_OFF(tp
) * 4;
665 if ((flags
& TH_RST
) && vflag
) {
666 print_tcp_rst_data(bp
, length
);
670 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
672 telnet_print(bp
, length
);
673 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
674 bgp_print(bp
, length
);
675 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
677 #ifdef TCPDUMP_DO_SMB
678 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
679 nbt_tcp_print(bp
, length
);
680 else if (sport
== SMB_PORT
|| dport
== SMB_PORT
)
681 smb_tcp_print(bp
, length
);
683 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
684 beep_print(bp
, length
);
685 else if (length
> 2 &&
686 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
687 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
689 * TCP DNS query has 2byte length at the head.
690 * XXX packet could be unaligned, it can go strange
692 ns_print(bp
+ 2, length
- 2, 0);
693 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
694 msdp_print(bp
, length
);
696 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
697 ldp_print(bp
, length
);
702 fputs("[bad opt]", stdout
);
707 fputs("[|tcp]", stdout
);
713 * RFC1122 says the following on data in RST segments:
715 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
717 * A TCP SHOULD allow a received RST segment to include data.
720 * It has been suggested that a RST segment could contain
721 * ASCII text that encoded and explained the cause of the
722 * RST. No standard has yet been established for such
728 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
732 if (TTEST2(*sp
, length
))
736 if (length
> MAX_RST_DATA_LEN
) {
737 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
738 putchar('+'); /* indicate we truncate */
741 while (length
-- && sp
<= snapend
) {
748 #ifdef HAVE_LIBCRYPTO
750 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
751 const u_char
*data
, int length
, const u_char
*rcvsig
)
754 u_char sig
[TCP_SIGLEN
];
757 u_int16_t savecsum
, tlen
;
764 if (data
+ length
> snapend
) {
765 printf("snaplen too short, ");
766 return (CANT_CHECK_SIGNATURE
);
771 if (sigsecret
== NULL
) {
772 printf("shared secret not supplied with -M, ");
773 return (CANT_CHECK_SIGNATURE
);
778 * Step 1: Update MD5 hash with IP pseudo-header.
781 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
782 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
783 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
784 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
785 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
787 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
789 } else if (IP_V(ip
) == 6) {
790 ip6
= (struct ip6_hdr
*)ip
;
791 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
792 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
793 len32
= htonl(ntohs(ip6
->ip6_plen
));
794 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
796 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
797 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
798 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
800 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
804 printf("IP version not 4 or 6, ");
806 printf("IP version not 4, ");
808 return (CANT_CHECK_SIGNATURE
);
812 * Step 2: Update MD5 hash with TCP header, excluding options.
813 * The TCP checksum must be set to zero.
815 savecsum
= tp1
.th_sum
;
817 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
818 tp1
.th_sum
= savecsum
;
820 * Step 3: Update MD5 hash with TCP segment data, if present.
823 MD5_Update(&ctx
, data
, length
);
825 * Step 4: Update MD5 hash with shared secret.
827 MD5_Update(&ctx
, sigsecret
, strlen(sigsecret
));
828 MD5_Final(sig
, &ctx
);
830 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
831 return (SIGNATURE_VALID
);
833 return (SIGNATURE_INVALID
);
835 #endif /* HAVE_LIBCRYPTO */
839 * c-style: whitesmith