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 static const struct tok tcp_flag_values
[] = {
109 static const 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" },
125 { TCPOPT_MPTCP
, "mptcp" },
126 { TCPOPT_EXPERIMENT2
, "exp" },
130 static int tcp_cksum(register const struct ip
*ip
,
131 register const struct tcphdr
*tp
,
134 return (nextproto4_cksum(ip
, (const u_int8_t
*)tp
, len
,
139 tcp_print(register const u_char
*bp
, register u_int length
,
140 register const u_char
*bp2
, int fragmented
)
142 register const struct tcphdr
*tp
;
143 register const struct ip
*ip
;
144 register u_char flags
;
147 u_int16_t sport
, dport
, win
, urp
;
148 u_int32_t seq
, ack
, thseq
, thack
;
153 register const struct ip6_hdr
*ip6
;
156 tp
= (struct tcphdr
*)bp
;
157 ip
= (struct ip
*)bp2
;
160 ip6
= (struct ip6_hdr
*)bp2
;
165 if (!TTEST(tp
->th_dport
)) {
166 (void)printf("%s > %s: [|tcp]",
167 ipaddr_string(&ip
->ip_src
),
168 ipaddr_string(&ip
->ip_dst
));
172 sport
= EXTRACT_16BITS(&tp
->th_sport
);
173 dport
= EXTRACT_16BITS(&tp
->th_dport
);
175 hlen
= TH_OFF(tp
) * 4;
178 * If data present, header length valid, and NFS port used,
180 * Pass offset of data plus 4 bytes for RPC TCP msg length
181 * to NFS print routines.
183 if (!qflag
&& hlen
>= sizeof(*tp
) && hlen
<= length
&&
184 (length
- hlen
) >= 4) {
187 register struct sunrpc_msg
*rp
;
188 enum sunrpc_msg_type direction
;
190 fraglenp
= (u_char
*)tp
+ hlen
;
191 if (TTEST2(*fraglenp
, 4)) {
192 fraglen
= EXTRACT_32BITS(fraglenp
) & 0x7FFFFFFF;
193 if (fraglen
> (length
- hlen
) - 4)
194 fraglen
= (length
- hlen
) - 4;
195 rp
= (struct sunrpc_msg
*)(fraglenp
+ 4);
196 if (TTEST(rp
->rm_direction
)) {
197 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
198 if (dport
== NFS_PORT
&&
199 direction
== SUNRPC_CALL
) {
200 nfsreq_print((u_char
*)rp
, fraglen
,
204 if (sport
== NFS_PORT
&&
205 direction
== SUNRPC_REPLY
) {
206 nfsreply_print((u_char
*)rp
, fraglen
,
215 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
216 (void)printf("%s.%s > %s.%s: ",
217 ip6addr_string(&ip6
->ip6_src
),
218 tcpport_string(sport
),
219 ip6addr_string(&ip6
->ip6_dst
),
220 tcpport_string(dport
));
222 (void)printf("%s > %s: ",
223 tcpport_string(sport
), tcpport_string(dport
));
228 if (ip
->ip_p
== IPPROTO_TCP
) {
229 (void)printf("%s.%s > %s.%s: ",
230 ipaddr_string(&ip
->ip_src
),
231 tcpport_string(sport
),
232 ipaddr_string(&ip
->ip_dst
),
233 tcpport_string(dport
));
235 (void)printf("%s > %s: ",
236 tcpport_string(sport
), tcpport_string(dport
));
240 if (hlen
< sizeof(*tp
)) {
241 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
242 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
248 seq
= EXTRACT_32BITS(&tp
->th_seq
);
249 ack
= EXTRACT_32BITS(&tp
->th_ack
);
250 win
= EXTRACT_16BITS(&tp
->th_win
);
251 urp
= EXTRACT_16BITS(&tp
->th_urp
);
254 (void)printf("tcp %d", length
- hlen
);
256 (void)printf(" [bad hdr length %u - too long, > %u]",
262 flags
= tp
->th_flags
;
263 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
265 if (!Sflag
&& (flags
& TH_ACK
)) {
266 register struct tcp_seq_hash
*th
;
267 const void *src
, *dst
;
271 * Find (or record) the initial sequence numbers for
272 * this conversation. (we pick an arbitrary
273 * collating order so there's only one entry for
283 else if (sport
== dport
) {
284 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
288 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
289 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
290 tha
.port
= dport
<< 16 | sport
;
292 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
293 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
294 tha
.port
= sport
<< 16 | dport
;
298 * Zero out the tha structure; the src and dst
299 * fields are big enough to hold an IPv6
300 * address, but we only have IPv4 addresses
301 * and thus must clear out the remaining 124
304 * XXX - should we just clear those bytes after
305 * copying the IPv4 addresses, rather than
306 * zeroing out the entire structure and then
307 * overwriting some of the zeroes?
309 * XXX - this could fail if we see TCP packets
310 * with an IPv6 address with the lower 124 bits
311 * all zero and also see TCP packes with an
312 * IPv4 address with the same 32 bits as the
313 * upper 32 bits of the IPv6 address in question.
314 * Can that happen? Is it likely enough to be
317 memset(&tha
, 0, sizeof(tha
));
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 (vflag
&& !Kflag
&& !fragmented
) {
397 /* Check the checksum, if possible. */
398 u_int16_t sum
, tcp_sum
;
401 if (TTEST2(tp
->th_sport
, length
)) {
402 sum
= tcp_cksum(ip
, tp
, length
);
403 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
405 (void)printf(", cksum 0x%04x", tcp_sum
);
407 (void)printf(" (incorrect -> 0x%04x)",
408 in_cksum_shouldbe(tcp_sum
, sum
));
410 (void)printf(" (correct)");
414 else if (IP_V(ip
) == 6 && ip6
->ip6_plen
) {
415 if (TTEST2(tp
->th_sport
, length
)) {
416 sum
= nextproto6_cksum(ip6
, (const u_int8_t
*)tp
, length
, IPPROTO_TCP
);
417 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
419 (void)printf(", cksum 0x%04x", tcp_sum
);
421 (void)printf(" (incorrect -> 0x%04x)",
422 in_cksum_shouldbe(tcp_sum
, sum
));
424 (void)printf(" (correct)");
432 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
433 (void)printf(", seq %u", seq
);
436 (void)printf(":%u", seq
+ length
);
440 if (flags
& TH_ACK
) {
441 (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
);
458 printf(", options [");
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); }
480 printf("%s", tok2str(tcp_option_values
, "Unknown Option %u", opt
));
487 (void)printf(" %u", EXTRACT_16BITS(cp
));
493 (void)printf(" %u", *cp
);
498 if (datalen
% 8 != 0) {
499 (void)printf("malformed sack");
503 (void)printf(" %d ", datalen
/ 8);
504 for (i
= 0; i
< datalen
; i
+= 8) {
506 s
= EXTRACT_32BITS(cp
+ i
);
508 e
= EXTRACT_32BITS(cp
+ i
+ 4);
516 (void)printf("{%u:%u}", s
, e
);
525 case TCPOPT_ECHOREPLY
:
528 * those options share their semantics.
533 (void)printf(" %u", EXTRACT_32BITS(cp
));
536 case TCPOPT_TIMESTAMP
:
539 (void)printf(" val %u ecr %u",
541 EXTRACT_32BITS(cp
+ 4));
544 case TCPOPT_SIGNATURE
:
545 datalen
= TCP_SIGLEN
;
547 #ifdef HAVE_LIBCRYPTO
548 switch (tcp_verify_signature(ip
, tp
,
549 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
551 case SIGNATURE_VALID
:
552 (void)printf("valid");
555 case SIGNATURE_INVALID
:
556 (void)printf("invalid");
559 case CANT_CHECK_SIGNATURE
:
560 (void)printf("can't check - ");
561 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
562 (void)printf("%02x", cp
[i
]);
566 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
567 (void)printf("%02x", cp
[i
]);
572 (void)printf("keyid %d", *cp
++);
574 for (i
= 0; i
< datalen
; ++i
) {
576 (void)printf("%02x", cp
[i
]);
585 * Nothing interesting.
593 utoval
= EXTRACT_16BITS(cp
);
594 (void)printf("0x%x", utoval
);
596 utoval
= (utoval
>> 1) * 60;
599 (void)printf(" %u", utoval
);
605 if (!mptcp_print(cp
-2, len
, flags
))
609 case TCPOPT_EXPERIMENT2
:
614 magic
= EXTRACT_16BITS(cp
);
620 /* TCP Fast Open: draft-ietf-tcpm-fastopen-04 */
622 /* Fast Open Cookie Request */
623 (void)printf("tfo cookiereq");
625 /* Fast Open Cookie */
626 if (datalen
% 2 != 0 || datalen
< 6 || datalen
> 18) {
627 (void)printf("tfo malformed");
629 (void)printf("tfo cookie ");
630 for (i
= 2; i
< datalen
; ++i
)
631 (void)printf("%02x", cp
[i
]);
637 /* Unknown magic number */
638 (void)printf("%04x", magic
);
645 for (i
= 0; i
< datalen
; ++i
) {
647 (void)printf("%02x", cp
[i
]);
652 /* Account for data printed */
656 /* Check specification against observed length */
657 ++datalen
; /* option octet */
658 if (!ZEROLENOPT(opt
))
659 ++datalen
; /* size octet */
661 (void)printf("[len %d]", len
);
663 if (opt
== TCPOPT_EOL
)
670 * Print length field before crawling down the stack.
672 printf(", length %u", length
);
678 * Decode payload if necessary.
680 bp
+= TH_OFF(tp
) * 4;
681 if ((flags
& TH_RST
) && vflag
) {
682 print_tcp_rst_data(bp
, length
);
687 switch (packettype
) {
689 zmtp1_print(bp
, length
);
695 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
697 telnet_print(bp
, length
);
698 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
699 bgp_print(bp
, length
);
700 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
702 #ifdef TCPDUMP_DO_SMB
703 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
704 nbt_tcp_print(bp
, length
);
705 else if (sport
== SMB_PORT
|| dport
== SMB_PORT
)
706 smb_tcp_print(bp
, length
);
708 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
709 beep_print(bp
, length
);
710 else if (sport
== OPENFLOW_PORT
|| dport
== OPENFLOW_PORT
)
711 openflow_print(bp
, length
);
712 else if (length
> 2 &&
713 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
714 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
716 * TCP DNS query has 2byte length at the head.
717 * XXX packet could be unaligned, it can go strange
719 ns_print(bp
+ 2, length
- 2, 0);
720 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
721 msdp_print(bp
, length
);
722 } else if (sport
== RPKI_RTR_PORT
|| dport
== RPKI_RTR_PORT
) {
723 rpki_rtr_print(bp
, length
);
725 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
726 ldp_print(bp
, length
);
731 fputs("[bad opt]", stdout
);
736 fputs("[|tcp]", stdout
);
742 * RFC1122 says the following on data in RST segments:
744 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
746 * A TCP SHOULD allow a received RST segment to include data.
749 * It has been suggested that a RST segment could contain
750 * ASCII text that encoded and explained the cause of the
751 * RST. No standard has yet been established for such
757 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
761 if (TTEST2(*sp
, length
))
765 if (length
> MAX_RST_DATA_LEN
) {
766 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
767 putchar('+'); /* indicate we truncate */
770 while (length
-- && sp
<= snapend
) {
777 #ifdef HAVE_LIBCRYPTO
779 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
780 const u_char
*data
, int length
, const u_char
*rcvsig
)
783 u_char sig
[TCP_SIGLEN
];
786 u_int16_t savecsum
, tlen
;
793 if (data
+ length
> snapend
) {
794 printf("snaplen too short, ");
795 return (CANT_CHECK_SIGNATURE
);
800 if (sigsecret
== NULL
) {
801 printf("shared secret not supplied with -M, ");
802 return (CANT_CHECK_SIGNATURE
);
807 * Step 1: Update MD5 hash with IP pseudo-header.
810 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
811 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
812 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
813 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
814 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
816 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
818 } else if (IP_V(ip
) == 6) {
819 ip6
= (struct ip6_hdr
*)ip
;
820 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
821 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
822 len32
= htonl(EXTRACT_16BITS(&ip6
->ip6_plen
));
823 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
825 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
826 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
827 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
829 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
833 printf("IP version not 4 or 6, ");
835 printf("IP version not 4, ");
837 return (CANT_CHECK_SIGNATURE
);
841 * Step 2: Update MD5 hash with TCP header, excluding options.
842 * The TCP checksum must be set to zero.
844 savecsum
= tp1
.th_sum
;
846 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
847 tp1
.th_sum
= savecsum
;
849 * Step 3: Update MD5 hash with TCP segment data, if present.
852 MD5_Update(&ctx
, data
, length
);
854 * Step 4: Update MD5 hash with shared secret.
856 MD5_Update(&ctx
, sigsecret
, strlen(sigsecret
));
857 MD5_Final(sig
, &ctx
);
859 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
860 return (SIGNATURE_VALID
);
862 return (SIGNATURE_INVALID
);
864 #endif /* HAVE_LIBCRYPTO */
868 * c-style: whitesmith