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 tcp_print(register const u_char
*bp
, register u_int length
,
161 register const u_char
*bp2
, int fragmented
)
163 register const struct tcphdr
*tp
;
164 register const struct ip
*ip
;
165 register u_char flags
;
168 u_int16_t sport
, dport
, win
, urp
;
169 u_int32_t seq
, ack
, thseq
, thack
;
173 register const struct ip6_hdr
*ip6
;
176 tp
= (struct tcphdr
*)bp
;
177 ip
= (struct ip
*)bp2
;
180 ip6
= (struct ip6_hdr
*)bp2
;
185 if (!TTEST(tp
->th_dport
)) {
186 (void)printf("%s > %s: [|tcp]",
187 ipaddr_string(&ip
->ip_src
),
188 ipaddr_string(&ip
->ip_dst
));
192 sport
= EXTRACT_16BITS(&tp
->th_sport
);
193 dport
= EXTRACT_16BITS(&tp
->th_dport
);
195 hlen
= TH_OFF(tp
) * 4;
198 * If data present, header length valid, and NFS port used,
200 * Pass offset of data plus 4 bytes for RPC TCP msg length
201 * to NFS print routines.
203 if (!qflag
&& hlen
>= sizeof(*tp
) && hlen
<= length
&&
204 (length
- hlen
) >= 4) {
207 register struct sunrpc_msg
*rp
;
208 enum sunrpc_msg_type direction
;
210 fraglenp
= (u_char
*)tp
+ hlen
;
211 if (TTEST2(*fraglenp
, 4)) {
212 fraglen
= EXTRACT_32BITS(fraglenp
) & 0x7FFFFFFF;
213 if (fraglen
> (length
- hlen
) - 4)
214 fraglen
= (length
- hlen
) - 4;
215 rp
= (struct sunrpc_msg
*)(fraglenp
+ 4);
216 if (TTEST(rp
->rm_direction
)) {
217 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
218 if (dport
== NFS_PORT
&&
219 direction
== SUNRPC_CALL
) {
220 nfsreq_print((u_char
*)rp
, fraglen
,
224 if (sport
== NFS_PORT
&&
225 direction
== SUNRPC_REPLY
) {
226 nfsreply_print((u_char
*)rp
, fraglen
,
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]",
282 flags
= tp
->th_flags
;
283 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
285 if (!Sflag
&& (flags
& TH_ACK
)) {
286 register struct tcp_seq_hash
*th
;
287 const void *src
, *dst
;
291 * Find (or record) the initial sequence numbers for
292 * this conversation. (we pick an arbitrary
293 * collating order so there's only one entry for
297 memset(&tha
, 0, sizeof(tha
));
304 else if (sport
== dport
) {
305 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
309 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
310 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
311 tha
.port
= dport
<< 16 | sport
;
313 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
314 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
315 tha
.port
= sport
<< 16 | dport
;
322 else if (sport
== dport
) {
323 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
327 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
328 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
329 tha
.port
= dport
<< 16 | sport
;
331 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
332 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
333 tha
.port
= sport
<< 16 | dport
;
342 else if (sport
== dport
) {
343 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
347 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
348 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
349 tha
.port
= dport
<< 16 | sport
;
351 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
352 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
353 tha
.port
= sport
<< 16 | dport
;
358 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
359 th
->nxt
; th
= th
->nxt
)
360 if (memcmp((char *)&tha
, (char *)&th
->addr
,
361 sizeof(th
->addr
)) == 0)
364 if (!th
->nxt
|| (flags
& TH_SYN
)) {
365 /* didn't find it or new conversation */
366 if (th
->nxt
== NULL
) {
367 th
->nxt
= (struct tcp_seq_hash
*)
368 calloc(1, sizeof(*th
));
370 error("tcp_print: calloc");
374 th
->ack
= seq
, th
->seq
= ack
- 1;
376 th
->seq
= seq
, th
->ack
= ack
- 1;
379 seq
-= th
->ack
, ack
-= th
->seq
;
381 seq
-= th
->seq
, ack
-= th
->ack
;
388 thseq
= thack
= threv
= 0;
391 (void)printf(" [bad hdr length %u - too long, > %u]",
396 if (IP_V(ip
) == 4 && vflag
&& !Kflag
&& !fragmented
) {
397 u_int16_t sum
, tcp_sum
;
398 if (TTEST2(tp
->th_sport
, length
)) {
399 sum
= tcp_cksum(ip
, tp
, length
);
401 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
403 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
404 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum
, sum
));
406 (void)printf(" (correct)");
410 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !Kflag
&& !fragmented
) {
411 u_int16_t sum
,tcp_sum
;
412 if (TTEST2(tp
->th_sport
, length
)) {
413 sum
= nextproto6_cksum(ip6
, (u_short
*)tp
, length
, IPPROTO_TCP
);
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)");
426 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
427 (void)printf(", seq %u", seq
);
430 (void)printf(":%u", seq
+ length
);
434 if (flags
& TH_ACK
) {
435 (void)printf(", ack %u", ack
);
438 (void)printf(", win %d", win
);
441 (void)printf(", urg %d", urp
);
443 * Handle any options.
445 if (hlen
> sizeof(*tp
)) {
446 register const u_char
*cp
;
447 register u_int i
, opt
, datalen
;
451 cp
= (const u_char
*)tp
+ sizeof(*tp
);
452 printf(", options [");
462 len
= *cp
++; /* total including type, len */
463 if (len
< 2 || len
> hlen
)
465 --hlen
; /* account for length byte */
467 --hlen
; /* account for type byte */
470 /* Bail if "l" bytes of data are not left or were not captured */
471 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
474 printf("%s", tok2str(tcp_option_values
, "Unknown Option %u", opt
));
481 (void)printf(" %u", EXTRACT_16BITS(cp
));
487 (void)printf(" %u", *cp
);
492 if (datalen
% 8 != 0) {
493 (void)printf("malformed sack");
497 (void)printf(" %d ", datalen
/ 8);
498 for (i
= 0; i
< datalen
; i
+= 8) {
500 s
= EXTRACT_32BITS(cp
+ i
);
502 e
= EXTRACT_32BITS(cp
+ i
+ 4);
510 (void)printf("{%u:%u}", s
, e
);
519 case TCPOPT_ECHOREPLY
:
522 * those options share their semantics.
527 (void)printf(" %u", EXTRACT_32BITS(cp
));
530 case TCPOPT_TIMESTAMP
:
533 (void)printf(" val %u ecr %u",
535 EXTRACT_32BITS(cp
+ 4));
538 case TCPOPT_SIGNATURE
:
539 datalen
= TCP_SIGLEN
;
541 #ifdef HAVE_LIBCRYPTO
542 switch (tcp_verify_signature(ip
, tp
,
543 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
545 case SIGNATURE_VALID
:
546 (void)printf("valid");
549 case SIGNATURE_INVALID
:
550 (void)printf("invalid");
553 case CANT_CHECK_SIGNATURE
:
554 (void)printf("can't check - ");
555 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
556 (void)printf("%02x", cp
[i
]);
560 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
561 (void)printf("%02x", cp
[i
]);
566 (void)printf("keyid %d", *cp
++);
568 for (i
= 0; i
< datalen
; ++i
) {
570 (void)printf("%02x", cp
[i
]);
579 * Nothing interesting.
587 utoval
= EXTRACT_16BITS(cp
);
588 (void)printf("0x%x", utoval
);
590 utoval
= (utoval
>> 1) * 60;
593 (void)printf(" %u", utoval
);
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
);
649 else if (sport
== SMB_PORT
|| dport
== SMB_PORT
)
650 smb_tcp_print(bp
, length
);
652 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
653 beep_print(bp
, length
);
654 else if (length
> 2 &&
655 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
656 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
658 * TCP DNS query has 2byte length at the head.
659 * XXX packet could be unaligned, it can go strange
661 ns_print(bp
+ 2, length
- 2, 0);
662 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
663 msdp_print(bp
, length
);
665 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
666 ldp_print(bp
, length
);
671 fputs("[bad opt]", stdout
);
676 fputs("[|tcp]", stdout
);
682 * RFC1122 says the following on data in RST segments:
684 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
686 * A TCP SHOULD allow a received RST segment to include data.
689 * It has been suggested that a RST segment could contain
690 * ASCII text that encoded and explained the cause of the
691 * RST. No standard has yet been established for such
697 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
701 if (TTEST2(*sp
, length
))
705 if (length
> MAX_RST_DATA_LEN
) {
706 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
707 putchar('+'); /* indicate we truncate */
710 while (length
-- && sp
<= snapend
) {
717 #ifdef HAVE_LIBCRYPTO
719 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
720 const u_char
*data
, int length
, const u_char
*rcvsig
)
723 u_char sig
[TCP_SIGLEN
];
726 u_int16_t savecsum
, tlen
;
733 if (data
+ length
> snapend
) {
734 printf("snaplen too short, ");
735 return (CANT_CHECK_SIGNATURE
);
740 if (sigsecret
== NULL
) {
741 printf("shared secret not supplied with -M, ");
742 return (CANT_CHECK_SIGNATURE
);
747 * Step 1: Update MD5 hash with IP pseudo-header.
750 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
751 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
752 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
753 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
754 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
756 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
758 } else if (IP_V(ip
) == 6) {
759 ip6
= (struct ip6_hdr
*)ip
;
760 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
761 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
762 len32
= htonl(EXTRACT_16BITS(&ip6
->ip6_plen
));
763 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
765 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
766 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
767 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
769 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
773 printf("IP version not 4 or 6, ");
775 printf("IP version not 4, ");
777 return (CANT_CHECK_SIGNATURE
);
781 * Step 2: Update MD5 hash with TCP header, excluding options.
782 * The TCP checksum must be set to zero.
784 savecsum
= tp1
.th_sum
;
786 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
787 tp1
.th_sum
= savecsum
;
789 * Step 3: Update MD5 hash with TCP segment data, if present.
792 MD5_Update(&ctx
, data
, length
);
794 * Step 4: Update MD5 hash with shared secret.
796 MD5_Update(&ctx
, sigsecret
, strlen(sigsecret
));
797 MD5_Final(sig
, &ctx
);
799 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
800 return (SIGNATURE_VALID
);
802 return (SIGNATURE_INVALID
);
804 #endif /* HAVE_LIBCRYPTO */
808 * c-style: whitesmith