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.
28 __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
35 #include <tcpdump-stdinc.h>
41 #include "interface.h"
42 #include "addrtoname.h"
58 #include <openssl/md5.h>
59 #include <signature.h>
61 static int tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
62 const u_char
*data
, int length
, const u_char
*rcvsig
);
65 static void print_tcp_rst_data(register const u_char
*sp
, u_int length
);
67 #define MAX_RST_DATA_LEN 30
77 struct tcp_seq_hash
*nxt
;
90 struct tcp_seq_hash6
{
91 struct tcp_seq_hash6
*nxt
;
98 #define TSEQ_HASHSIZE 919
100 /* These tcp optinos do not have the size octet */
101 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
103 static struct tcp_seq_hash tcp_seq_hash4
[TSEQ_HASHSIZE
];
105 static struct tcp_seq_hash6 tcp_seq_hash6
[TSEQ_HASHSIZE
];
108 static const struct tok tcp_flag_values
[] = {
120 static const struct tok tcp_option_values
[] = {
121 { TCPOPT_EOL
, "eol" },
122 { TCPOPT_NOP
, "nop" },
123 { TCPOPT_MAXSEG
, "mss" },
124 { TCPOPT_WSCALE
, "wscale" },
125 { TCPOPT_SACKOK
, "sackOK" },
126 { TCPOPT_SACK
, "sack" },
127 { TCPOPT_ECHO
, "echo" },
128 { TCPOPT_ECHOREPLY
, "echoreply" },
129 { TCPOPT_TIMESTAMP
, "TS" },
131 { TCPOPT_CCNEW
, "ccnew" },
132 { TCPOPT_CCECHO
, "" },
133 { TCPOPT_SIGNATURE
, "md5" },
134 { TCPOPT_AUTH
, "enhanced auth" },
135 { TCPOPT_UTO
, "uto" },
136 { TCPOPT_MPTCP
, "mptcp" },
137 { TCPOPT_EXPERIMENT2
, "exp" },
141 static int tcp_cksum(register const struct ip
*ip
,
142 register const struct tcphdr
*tp
,
145 return (nextproto4_cksum(ip
, (const u_int8_t
*)tp
, len
,
150 tcp_print(register const u_char
*bp
, register u_int length
,
151 register const u_char
*bp2
, int fragmented
)
153 register const struct tcphdr
*tp
;
154 register const struct ip
*ip
;
155 register u_char flags
;
158 u_int16_t sport
, dport
, win
, urp
;
159 u_int32_t seq
, ack
, thseq
, thack
;
164 register const struct ip6_hdr
*ip6
;
167 tp
= (struct tcphdr
*)bp
;
168 ip
= (struct ip
*)bp2
;
171 ip6
= (struct ip6_hdr
*)bp2
;
176 if (!TTEST(tp
->th_dport
)) {
177 (void)printf("%s > %s: [|tcp]",
178 ipaddr_string(&ip
->ip_src
),
179 ipaddr_string(&ip
->ip_dst
));
183 sport
= EXTRACT_16BITS(&tp
->th_sport
);
184 dport
= EXTRACT_16BITS(&tp
->th_dport
);
186 hlen
= TH_OFF(tp
) * 4;
190 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
191 (void)printf("%s.%s > %s.%s: ",
192 ip6addr_string(&ip6
->ip6_src
),
193 tcpport_string(sport
),
194 ip6addr_string(&ip6
->ip6_dst
),
195 tcpport_string(dport
));
197 (void)printf("%s > %s: ",
198 tcpport_string(sport
), tcpport_string(dport
));
203 if (ip
->ip_p
== IPPROTO_TCP
) {
204 (void)printf("%s.%s > %s.%s: ",
205 ipaddr_string(&ip
->ip_src
),
206 tcpport_string(sport
),
207 ipaddr_string(&ip
->ip_dst
),
208 tcpport_string(dport
));
210 (void)printf("%s > %s: ",
211 tcpport_string(sport
), tcpport_string(dport
));
215 if (hlen
< sizeof(*tp
)) {
216 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
217 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
223 seq
= EXTRACT_32BITS(&tp
->th_seq
);
224 ack
= EXTRACT_32BITS(&tp
->th_ack
);
225 win
= EXTRACT_16BITS(&tp
->th_win
);
226 urp
= EXTRACT_16BITS(&tp
->th_urp
);
229 (void)printf("tcp %d", length
- hlen
);
231 (void)printf(" [bad hdr length %u - too long, > %u]",
237 flags
= tp
->th_flags
;
238 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
240 if (!Sflag
&& (flags
& TH_ACK
)) {
242 * Find (or record) the initial sequence numbers for
243 * this conversation. (we pick an arbitrary
244 * collating order so there's only one entry for
250 register struct tcp_seq_hash6
*th
;
251 struct tcp_seq_hash6
*tcp_seq_hash
;
252 const struct in6_addr
*src
, *dst
;
255 tcp_seq_hash
= tcp_seq_hash6
;
260 else if (sport
== dport
) {
261 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
265 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
266 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
267 tha
.port
= dport
<< 16 | sport
;
269 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
270 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
271 tha
.port
= sport
<< 16 | dport
;
274 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
275 th
->nxt
; th
= th
->nxt
)
276 if (memcmp((char *)&tha
, (char *)&th
->addr
,
277 sizeof(th
->addr
)) == 0)
280 if (!th
->nxt
|| (flags
& TH_SYN
)) {
281 /* didn't find it or new conversation */
282 if (th
->nxt
== NULL
) {
283 th
->nxt
= (struct tcp_seq_hash6
*)
284 calloc(1, sizeof(*th
));
286 error("tcp_print: calloc");
290 th
->ack
= seq
, th
->seq
= ack
- 1;
292 th
->seq
= seq
, th
->ack
= ack
- 1;
295 seq
-= th
->ack
, ack
-= th
->seq
;
297 seq
-= th
->seq
, ack
-= th
->ack
;
306 register struct tcp_seq_hash
*th
;
307 struct tcp_seq_hash
*tcp_seq_hash
;
308 const struct in_addr
*src
, *dst
;
311 tcp_seq_hash
= tcp_seq_hash4
;
316 else if (sport
== dport
) {
317 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
321 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
322 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
323 tha
.port
= dport
<< 16 | sport
;
325 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
326 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
327 tha
.port
= sport
<< 16 | dport
;
330 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
331 th
->nxt
; th
= th
->nxt
)
332 if (memcmp((char *)&tha
, (char *)&th
->addr
,
333 sizeof(th
->addr
)) == 0)
336 if (!th
->nxt
|| (flags
& TH_SYN
)) {
337 /* didn't find it or new conversation */
338 if (th
->nxt
== NULL
) {
339 th
->nxt
= (struct tcp_seq_hash
*)
340 calloc(1, sizeof(*th
));
342 error("tcp_print: calloc");
346 th
->ack
= seq
, th
->seq
= ack
- 1;
348 th
->seq
= seq
, th
->ack
= ack
- 1;
351 seq
-= th
->ack
, ack
-= th
->seq
;
353 seq
-= th
->seq
, ack
-= th
->ack
;
361 thseq
= thack
= rev
= 0;
364 (void)printf(" [bad hdr length %u - too long, > %u]",
369 if (vflag
&& !Kflag
&& !fragmented
) {
370 /* Check the checksum, if possible. */
371 u_int16_t sum
, tcp_sum
;
374 if (TTEST2(tp
->th_sport
, length
)) {
375 sum
= tcp_cksum(ip
, tp
, length
);
376 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
378 (void)printf(", cksum 0x%04x", tcp_sum
);
380 (void)printf(" (incorrect -> 0x%04x)",
381 in_cksum_shouldbe(tcp_sum
, sum
));
383 (void)printf(" (correct)");
387 else if (IP_V(ip
) == 6 && ip6
->ip6_plen
) {
388 if (TTEST2(tp
->th_sport
, length
)) {
389 sum
= nextproto6_cksum(ip6
, (const u_int8_t
*)tp
, length
, IPPROTO_TCP
);
390 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
392 (void)printf(", cksum 0x%04x", tcp_sum
);
394 (void)printf(" (incorrect -> 0x%04x)",
395 in_cksum_shouldbe(tcp_sum
, sum
));
397 (void)printf(" (correct)");
405 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
406 (void)printf(", seq %u", seq
);
409 (void)printf(":%u", seq
+ length
);
413 if (flags
& TH_ACK
) {
414 (void)printf(", ack %u", ack
);
417 (void)printf(", win %d", win
);
420 (void)printf(", urg %d", urp
);
422 * Handle any options.
424 if (hlen
> sizeof(*tp
)) {
425 register const u_char
*cp
;
426 register u_int i
, opt
, datalen
;
430 cp
= (const u_char
*)tp
+ sizeof(*tp
);
431 printf(", options [");
441 len
= *cp
++; /* total including type, len */
442 if (len
< 2 || len
> hlen
)
444 --hlen
; /* account for length byte */
446 --hlen
; /* account for type byte */
449 /* Bail if "l" bytes of data are not left or were not captured */
450 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
453 printf("%s", tok2str(tcp_option_values
, "unknown-%u", opt
));
460 (void)printf(" %u", EXTRACT_16BITS(cp
));
466 (void)printf(" %u", *cp
);
471 if (datalen
% 8 != 0) {
472 (void)printf("malformed sack");
476 (void)printf(" %d ", datalen
/ 8);
477 for (i
= 0; i
< datalen
; i
+= 8) {
479 s
= EXTRACT_32BITS(cp
+ i
);
481 e
= EXTRACT_32BITS(cp
+ i
+ 4);
489 (void)printf("{%u:%u}", s
, e
);
498 case TCPOPT_ECHOREPLY
:
501 * those options share their semantics.
506 (void)printf(" %u", EXTRACT_32BITS(cp
));
509 case TCPOPT_TIMESTAMP
:
512 (void)printf(" val %u ecr %u",
514 EXTRACT_32BITS(cp
+ 4));
517 case TCPOPT_SIGNATURE
:
518 datalen
= TCP_SIGLEN
;
520 #ifdef HAVE_LIBCRYPTO
521 switch (tcp_verify_signature(ip
, tp
,
522 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
524 case SIGNATURE_VALID
:
525 (void)printf("valid");
528 case SIGNATURE_INVALID
:
529 (void)printf("invalid");
532 case CANT_CHECK_SIGNATURE
:
533 (void)printf("can't check - ");
534 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
535 (void)printf("%02x", cp
[i
]);
539 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
540 (void)printf("%02x", cp
[i
]);
545 (void)printf("keyid %d", *cp
++);
547 for (i
= 0; i
< datalen
; ++i
) {
549 (void)printf("%02x", cp
[i
]);
558 * Nothing interesting.
566 utoval
= EXTRACT_16BITS(cp
);
567 (void)printf("0x%x", utoval
);
569 utoval
= (utoval
>> 1) * 60;
572 (void)printf(" %u", utoval
);
578 if (!mptcp_print(cp
-2, len
, flags
))
582 case TCPOPT_EXPERIMENT2
:
588 magic
= EXTRACT_16BITS(cp
);
594 /* TCP Fast Open: draft-ietf-tcpm-fastopen-04 */
596 /* Fast Open Cookie Request */
597 (void)printf("tfo cookiereq");
599 /* Fast Open Cookie */
600 if (datalen
% 2 != 0 || datalen
< 6 || datalen
> 18) {
601 (void)printf("tfo malformed");
603 (void)printf("tfo cookie ");
604 for (i
= 2; i
< datalen
; ++i
)
605 (void)printf("%02x", cp
[i
]);
611 /* Unknown magic number */
612 (void)printf("%04x", magic
);
621 for (i
= 0; i
< datalen
; ++i
) {
623 (void)printf("%02x", cp
[i
]);
628 /* Account for data printed */
632 /* Check specification against observed length */
633 ++datalen
; /* option octet */
634 if (!ZEROLENOPT(opt
))
635 ++datalen
; /* size octet */
637 (void)printf("[len %d]", len
);
639 if (opt
== TCPOPT_EOL
)
646 * Print length field before crawling down the stack.
648 printf(", length %u", length
);
654 * Decode payload if necessary.
656 bp
+= TH_OFF(tp
) * 4;
657 if ((flags
& TH_RST
) && vflag
) {
658 print_tcp_rst_data(bp
, length
);
663 switch (packettype
) {
665 zmtp1_print(bp
, length
);
671 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
673 telnet_print(bp
, length
);
674 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
675 bgp_print(bp
, length
);
676 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
678 #ifdef TCPDUMP_DO_SMB
679 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
680 nbt_tcp_print(bp
, length
);
681 else if (sport
== SMB_PORT
|| dport
== SMB_PORT
)
682 smb_tcp_print(bp
, length
);
684 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
685 beep_print(bp
, length
);
686 else if (sport
== OPENFLOW_PORT
|| dport
== OPENFLOW_PORT
)
687 openflow_print(bp
, length
);
688 else if (length
> 2 &&
689 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
690 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
692 * TCP DNS query has 2byte length at the head.
693 * XXX packet could be unaligned, it can go strange
695 ns_print(bp
+ 2, length
- 2, 0);
696 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
697 msdp_print(bp
, length
);
698 } else if (sport
== RPKI_RTR_PORT
|| dport
== RPKI_RTR_PORT
) {
699 rpki_rtr_print(bp
, length
);
701 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
702 ldp_print(bp
, length
);
704 else if ((sport
== NFS_PORT
|| dport
== NFS_PORT
) &&
705 length
>= 4 && TTEST2(*bp
, 4)) {
707 * If data present, header length valid, and NFS port used,
709 * Pass offset of data plus 4 bytes for RPC TCP msg length
710 * to NFS print routines.
713 register struct sunrpc_msg
*rp
;
714 enum sunrpc_msg_type direction
;
716 fraglen
= EXTRACT_32BITS(bp
) & 0x7FFFFFFF;
717 if (fraglen
> (length
) - 4)
718 fraglen
= (length
) - 4;
719 rp
= (struct sunrpc_msg
*)(bp
+ 4);
720 if (TTEST(rp
->rm_direction
)) {
721 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
722 if (dport
== NFS_PORT
&& direction
== SUNRPC_CALL
) {
723 (void)printf(": NFS request xid %u ", EXTRACT_32BITS(&rp
->rm_xid
));
724 nfsreq_print_noaddr((u_char
*)rp
, fraglen
, (u_char
*)ip
);
727 if (sport
== NFS_PORT
&& direction
== SUNRPC_REPLY
) {
728 (void)printf(": NFS reply xid %u ", EXTRACT_32BITS(&rp
->rm_xid
));
729 nfsreply_print_noaddr((u_char
*)rp
, fraglen
, (u_char
*)ip
);
737 fputs("[bad opt]", stdout
);
742 fputs("[|tcp]", stdout
);
748 * RFC1122 says the following on data in RST segments:
750 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
752 * A TCP SHOULD allow a received RST segment to include data.
755 * It has been suggested that a RST segment could contain
756 * ASCII text that encoded and explained the cause of the
757 * RST. No standard has yet been established for such
763 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
767 if (TTEST2(*sp
, length
))
771 if (length
> MAX_RST_DATA_LEN
) {
772 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
773 putchar('+'); /* indicate we truncate */
776 while (length
-- && sp
<= snapend
) {
783 #ifdef HAVE_LIBCRYPTO
784 USES_APPLE_DEPRECATED_API
786 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
787 const u_char
*data
, int length
, const u_char
*rcvsig
)
790 u_char sig
[TCP_SIGLEN
];
793 u_int16_t savecsum
, tlen
;
800 if (data
+ length
> snapend
) {
801 printf("snaplen too short, ");
802 return (CANT_CHECK_SIGNATURE
);
807 if (sigsecret
== NULL
) {
808 printf("shared secret not supplied with -M, ");
809 return (CANT_CHECK_SIGNATURE
);
814 * Step 1: Update MD5 hash with IP pseudo-header.
817 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
818 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
819 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
820 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
821 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
823 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
825 } else if (IP_V(ip
) == 6) {
826 ip6
= (struct ip6_hdr
*)ip
;
827 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
828 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
829 len32
= htonl(EXTRACT_16BITS(&ip6
->ip6_plen
));
830 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
832 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
833 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
834 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
836 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
840 printf("IP version not 4 or 6, ");
842 printf("IP version not 4, ");
844 return (CANT_CHECK_SIGNATURE
);
848 * Step 2: Update MD5 hash with TCP header, excluding options.
849 * The TCP checksum must be set to zero.
851 savecsum
= tp1
.th_sum
;
853 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
854 tp1
.th_sum
= savecsum
;
856 * Step 3: Update MD5 hash with TCP segment data, if present.
859 MD5_Update(&ctx
, data
, length
);
861 * Step 4: Update MD5 hash with shared secret.
863 MD5_Update(&ctx
, sigsecret
, strlen(sigsecret
));
864 MD5_Final(sig
, &ctx
);
866 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
867 return (SIGNATURE_VALID
);
869 return (SIGNATURE_INVALID
);
872 #endif /* HAVE_LIBCRYPTO */
876 * c-style: whitesmith