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
79 struct tcp_seq_hash
*nxt
;
92 struct tcp_seq_hash6
{
93 struct tcp_seq_hash6
*nxt
;
100 #define TSEQ_HASHSIZE 919
102 /* These tcp optinos do not have the size octet */
103 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
105 static struct tcp_seq_hash tcp_seq_hash4
[TSEQ_HASHSIZE
];
107 static struct tcp_seq_hash6 tcp_seq_hash6
[TSEQ_HASHSIZE
];
110 static const struct tok tcp_flag_values
[] = {
122 static const struct tok tcp_option_values
[] = {
123 { TCPOPT_EOL
, "eol" },
124 { TCPOPT_NOP
, "nop" },
125 { TCPOPT_MAXSEG
, "mss" },
126 { TCPOPT_WSCALE
, "wscale" },
127 { TCPOPT_SACKOK
, "sackOK" },
128 { TCPOPT_SACK
, "sack" },
129 { TCPOPT_ECHO
, "echo" },
130 { TCPOPT_ECHOREPLY
, "echoreply" },
131 { TCPOPT_TIMESTAMP
, "TS" },
133 { TCPOPT_CCNEW
, "ccnew" },
134 { TCPOPT_CCECHO
, "" },
135 { TCPOPT_SIGNATURE
, "md5" },
136 { TCPOPT_AUTH
, "enhanced auth" },
137 { TCPOPT_UTO
, "uto" },
138 { TCPOPT_MPTCP
, "mptcp" },
139 { TCPOPT_EXPERIMENT2
, "exp" },
143 static int tcp_cksum(register const struct ip
*ip
,
144 register const struct tcphdr
*tp
,
147 return (nextproto4_cksum(ip
, (const u_int8_t
*)tp
, len
,
152 tcp_print(register const u_char
*bp
, register u_int length
,
153 register const u_char
*bp2
, int fragmented
)
155 register const struct tcphdr
*tp
;
156 register const struct ip
*ip
;
157 register u_char flags
;
160 u_int16_t sport
, dport
, win
, urp
;
161 u_int32_t seq
, ack
, thseq
, thack
;
166 register const struct ip6_hdr
*ip6
;
169 tp
= (struct tcphdr
*)bp
;
170 ip
= (struct ip
*)bp2
;
173 ip6
= (struct ip6_hdr
*)bp2
;
178 if (!TTEST(tp
->th_dport
)) {
179 (void)printf("%s > %s: [|tcp]",
180 ipaddr_string(&ip
->ip_src
),
181 ipaddr_string(&ip
->ip_dst
));
185 sport
= EXTRACT_16BITS(&tp
->th_sport
);
186 dport
= EXTRACT_16BITS(&tp
->th_dport
);
188 hlen
= TH_OFF(tp
) * 4;
192 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
193 (void)printf("%s.%s > %s.%s: ",
194 ip6addr_string(&ip6
->ip6_src
),
195 tcpport_string(sport
),
196 ip6addr_string(&ip6
->ip6_dst
),
197 tcpport_string(dport
));
199 (void)printf("%s > %s: ",
200 tcpport_string(sport
), tcpport_string(dport
));
205 if (ip
->ip_p
== IPPROTO_TCP
) {
206 (void)printf("%s.%s > %s.%s: ",
207 ipaddr_string(&ip
->ip_src
),
208 tcpport_string(sport
),
209 ipaddr_string(&ip
->ip_dst
),
210 tcpport_string(dport
));
212 (void)printf("%s > %s: ",
213 tcpport_string(sport
), tcpport_string(dport
));
217 if (hlen
< sizeof(*tp
)) {
218 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
219 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
225 seq
= EXTRACT_32BITS(&tp
->th_seq
);
226 ack
= EXTRACT_32BITS(&tp
->th_ack
);
227 win
= EXTRACT_16BITS(&tp
->th_win
);
228 urp
= EXTRACT_16BITS(&tp
->th_urp
);
231 (void)printf("tcp %d", length
- hlen
);
233 (void)printf(" [bad hdr length %u - too long, > %u]",
239 flags
= tp
->th_flags
;
240 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
242 if (!Sflag
&& (flags
& TH_ACK
)) {
244 * Find (or record) the initial sequence numbers for
245 * this conversation. (we pick an arbitrary
246 * collating order so there's only one entry for
252 register struct tcp_seq_hash6
*th
;
253 struct tcp_seq_hash6
*tcp_seq_hash
;
254 const struct in6_addr
*src
, *dst
;
257 tcp_seq_hash
= tcp_seq_hash6
;
262 else if (sport
== dport
) {
263 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
267 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
268 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
269 tha
.port
= dport
<< 16 | sport
;
271 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
272 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
273 tha
.port
= sport
<< 16 | dport
;
276 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
277 th
->nxt
; th
= th
->nxt
)
278 if (memcmp((char *)&tha
, (char *)&th
->addr
,
279 sizeof(th
->addr
)) == 0)
282 if (!th
->nxt
|| (flags
& TH_SYN
)) {
283 /* didn't find it or new conversation */
284 if (th
->nxt
== NULL
) {
285 th
->nxt
= (struct tcp_seq_hash6
*)
286 calloc(1, sizeof(*th
));
288 error("tcp_print: calloc");
292 th
->ack
= seq
, th
->seq
= ack
- 1;
294 th
->seq
= seq
, th
->ack
= ack
- 1;
297 seq
-= th
->ack
, ack
-= th
->seq
;
299 seq
-= th
->seq
, ack
-= th
->ack
;
308 register struct tcp_seq_hash
*th
;
309 struct tcp_seq_hash
*tcp_seq_hash
;
310 const struct in_addr
*src
, *dst
;
313 tcp_seq_hash
= tcp_seq_hash4
;
318 else if (sport
== dport
) {
319 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
323 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
324 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
325 tha
.port
= dport
<< 16 | sport
;
327 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
328 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
329 tha
.port
= sport
<< 16 | dport
;
332 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
333 th
->nxt
; th
= th
->nxt
)
334 if (memcmp((char *)&tha
, (char *)&th
->addr
,
335 sizeof(th
->addr
)) == 0)
338 if (!th
->nxt
|| (flags
& TH_SYN
)) {
339 /* didn't find it or new conversation */
340 if (th
->nxt
== NULL
) {
341 th
->nxt
= (struct tcp_seq_hash
*)
342 calloc(1, sizeof(*th
));
344 error("tcp_print: calloc");
348 th
->ack
= seq
, th
->seq
= ack
- 1;
350 th
->seq
= seq
, th
->ack
= ack
- 1;
353 seq
-= th
->ack
, ack
-= th
->seq
;
355 seq
-= th
->seq
, ack
-= th
->ack
;
363 thseq
= thack
= rev
= 0;
366 (void)printf(" [bad hdr length %u - too long, > %u]",
371 if (vflag
&& !Kflag
&& !fragmented
) {
372 /* Check the checksum, if possible. */
373 u_int16_t sum
, tcp_sum
;
376 if (TTEST2(tp
->th_sport
, length
)) {
377 sum
= tcp_cksum(ip
, tp
, length
);
378 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
380 (void)printf(", cksum 0x%04x", tcp_sum
);
382 (void)printf(" (incorrect -> 0x%04x)",
383 in_cksum_shouldbe(tcp_sum
, sum
));
385 (void)printf(" (correct)");
389 else if (IP_V(ip
) == 6 && ip6
->ip6_plen
) {
390 if (TTEST2(tp
->th_sport
, length
)) {
391 sum
= nextproto6_cksum(ip6
, (const u_int8_t
*)tp
, length
, IPPROTO_TCP
);
392 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
394 (void)printf(", cksum 0x%04x", tcp_sum
);
396 (void)printf(" (incorrect -> 0x%04x)",
397 in_cksum_shouldbe(tcp_sum
, sum
));
399 (void)printf(" (correct)");
407 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
408 (void)printf(", seq %u", seq
);
411 (void)printf(":%u", seq
+ length
);
415 if (flags
& TH_ACK
) {
416 (void)printf(", ack %u", ack
);
419 (void)printf(", win %d", win
);
422 (void)printf(", urg %d", urp
);
424 * Handle any options.
426 if (hlen
> sizeof(*tp
)) {
427 register const u_char
*cp
;
428 register u_int i
, opt
, datalen
;
432 cp
= (const u_char
*)tp
+ sizeof(*tp
);
433 printf(", options [");
443 len
= *cp
++; /* total including type, len */
444 if (len
< 2 || len
> hlen
)
446 --hlen
; /* account for length byte */
448 --hlen
; /* account for type byte */
451 /* Bail if "l" bytes of data are not left or were not captured */
452 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
455 printf("%s", tok2str(tcp_option_values
, "unknown-%u", opt
));
462 (void)printf(" %u", EXTRACT_16BITS(cp
));
468 (void)printf(" %u", *cp
);
473 if (datalen
% 8 != 0) {
474 (void)printf("malformed sack");
478 (void)printf(" %d ", datalen
/ 8);
479 for (i
= 0; i
< datalen
; i
+= 8) {
481 s
= EXTRACT_32BITS(cp
+ i
);
483 e
= EXTRACT_32BITS(cp
+ i
+ 4);
491 (void)printf("{%u:%u}", s
, e
);
500 case TCPOPT_ECHOREPLY
:
503 * those options share their semantics.
508 (void)printf(" %u", EXTRACT_32BITS(cp
));
511 case TCPOPT_TIMESTAMP
:
514 (void)printf(" val %u ecr %u",
516 EXTRACT_32BITS(cp
+ 4));
519 case TCPOPT_SIGNATURE
:
520 datalen
= TCP_SIGLEN
;
522 #ifdef HAVE_LIBCRYPTO
523 switch (tcp_verify_signature(ip
, tp
,
524 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
526 case SIGNATURE_VALID
:
527 (void)printf("valid");
530 case SIGNATURE_INVALID
:
531 (void)printf("invalid");
534 case CANT_CHECK_SIGNATURE
:
535 (void)printf("can't check - ");
536 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
537 (void)printf("%02x", cp
[i
]);
541 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
542 (void)printf("%02x", cp
[i
]);
547 (void)printf("keyid %d", *cp
++);
549 for (i
= 0; i
< datalen
; ++i
) {
551 (void)printf("%02x", cp
[i
]);
560 * Nothing interesting.
568 utoval
= EXTRACT_16BITS(cp
);
569 (void)printf("0x%x", utoval
);
571 utoval
= (utoval
>> 1) * 60;
574 (void)printf(" %u", utoval
);
580 if (!mptcp_print(cp
-2, len
, flags
))
584 case TCPOPT_EXPERIMENT2
:
590 magic
= EXTRACT_16BITS(cp
);
596 /* TCP Fast Open: draft-ietf-tcpm-fastopen-04 */
598 /* Fast Open Cookie Request */
599 (void)printf("tfo cookiereq");
601 /* Fast Open Cookie */
602 if (datalen
% 2 != 0 || datalen
< 6 || datalen
> 18) {
603 (void)printf("tfo malformed");
605 (void)printf("tfo cookie ");
606 for (i
= 2; i
< datalen
; ++i
)
607 (void)printf("%02x", cp
[i
]);
613 /* Unknown magic number */
614 (void)printf("%04x", magic
);
623 for (i
= 0; i
< datalen
; ++i
) {
625 (void)printf("%02x", cp
[i
]);
630 /* Account for data printed */
634 /* Check specification against observed length */
635 ++datalen
; /* option octet */
636 if (!ZEROLENOPT(opt
))
637 ++datalen
; /* size octet */
639 (void)printf("[len %d]", len
);
641 if (opt
== TCPOPT_EOL
)
648 * Print length field before crawling down the stack.
650 printf(", length %u", length
);
656 * Decode payload if necessary.
658 bp
+= TH_OFF(tp
) * 4;
659 if ((flags
& TH_RST
) && vflag
) {
660 print_tcp_rst_data(bp
, length
);
665 switch (packettype
) {
667 zmtp1_print(bp
, length
);
673 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
675 telnet_print(bp
, length
);
676 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
677 bgp_print(bp
, length
);
678 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
680 #ifdef TCPDUMP_DO_SMB
681 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
682 nbt_tcp_print(bp
, length
);
683 else if (sport
== SMB_PORT
|| dport
== SMB_PORT
)
684 smb_tcp_print(bp
, length
);
686 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
687 beep_print(bp
, length
);
688 else if (sport
== OPENFLOW_PORT
|| dport
== OPENFLOW_PORT
)
689 openflow_print(bp
, length
);
690 else if (length
> 2 &&
691 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
692 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
694 * TCP DNS query has 2byte length at the head.
695 * XXX packet could be unaligned, it can go strange
697 ns_print(bp
+ 2, length
- 2, 0);
698 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
699 msdp_print(bp
, length
);
700 } else if (sport
== RPKI_RTR_PORT
|| dport
== RPKI_RTR_PORT
) {
701 rpki_rtr_print(bp
, length
);
703 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
704 ldp_print(bp
, length
);
706 else if ((sport
== NFS_PORT
|| dport
== NFS_PORT
) &&
707 length
>= 4 && TTEST2(*bp
, 4)) {
709 * If data present, header length valid, and NFS port used,
711 * Pass offset of data plus 4 bytes for RPC TCP msg length
712 * to NFS print routines.
715 register struct sunrpc_msg
*rp
;
716 enum sunrpc_msg_type direction
;
718 fraglen
= EXTRACT_32BITS(bp
) & 0x7FFFFFFF;
719 if (fraglen
> (length
) - 4)
720 fraglen
= (length
) - 4;
721 rp
= (struct sunrpc_msg
*)(bp
+ 4);
722 if (TTEST(rp
->rm_direction
)) {
723 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
724 if (dport
== NFS_PORT
&& direction
== SUNRPC_CALL
) {
725 (void)printf(": NFS request xid %u ", EXTRACT_32BITS(&rp
->rm_xid
));
726 nfsreq_print_noaddr((u_char
*)rp
, fraglen
, (u_char
*)ip
);
729 if (sport
== NFS_PORT
&& direction
== SUNRPC_REPLY
) {
730 (void)printf(": NFS reply xid %u ", EXTRACT_32BITS(&rp
->rm_xid
));
731 nfsreply_print_noaddr((u_char
*)rp
, fraglen
, (u_char
*)ip
);
739 fputs("[bad opt]", stdout
);
744 fputs("[|tcp]", stdout
);
750 * RFC1122 says the following on data in RST segments:
752 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
754 * A TCP SHOULD allow a received RST segment to include data.
757 * It has been suggested that a RST segment could contain
758 * ASCII text that encoded and explained the cause of the
759 * RST. No standard has yet been established for such
765 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
769 if (TTEST2(*sp
, length
))
773 if (length
> MAX_RST_DATA_LEN
) {
774 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
775 putchar('+'); /* indicate we truncate */
778 while (length
-- && sp
<= snapend
) {
785 #ifdef HAVE_LIBCRYPTO
786 USES_APPLE_DEPRECATED_API
788 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
789 const u_char
*data
, int length
, const u_char
*rcvsig
)
792 u_char sig
[TCP_SIGLEN
];
795 u_int16_t savecsum
, tlen
;
802 if (data
+ length
> snapend
) {
803 printf("snaplen too short, ");
804 return (CANT_CHECK_SIGNATURE
);
809 if (sigsecret
== NULL
) {
810 printf("shared secret not supplied with -M, ");
811 return (CANT_CHECK_SIGNATURE
);
816 * Step 1: Update MD5 hash with IP pseudo-header.
819 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
820 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
821 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
822 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
823 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
825 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
827 } else if (IP_V(ip
) == 6) {
828 ip6
= (struct ip6_hdr
*)ip
;
829 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
830 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
831 len32
= htonl(EXTRACT_16BITS(&ip6
->ip6_plen
));
832 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
834 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
835 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
836 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
838 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
842 printf("IP version not 4 or 6, ");
844 printf("IP version not 4, ");
846 return (CANT_CHECK_SIGNATURE
);
850 * Step 2: Update MD5 hash with TCP header, excluding options.
851 * The TCP checksum must be set to zero.
853 savecsum
= tp1
.th_sum
;
855 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
856 tp1
.th_sum
= savecsum
;
858 * Step 3: Update MD5 hash with TCP segment data, if present.
861 MD5_Update(&ctx
, data
, length
);
863 * Step 4: Update MD5 hash with shared secret.
865 MD5_Update(&ctx
, sigsecret
, strlen(sigsecret
));
866 MD5_Final(sig
, &ctx
);
868 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
869 return (SIGNATURE_VALID
);
871 return (SIGNATURE_INVALID
);
874 #endif /* HAVE_LIBCRYPTO */
878 * c-style: whitesmith