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" },
125 { TCPOPT_MPTCP
, "mptcp" },
129 static int tcp_cksum(register const struct ip
*ip
,
130 register const struct tcphdr
*tp
,
133 return (nextproto4_cksum(ip
, (const u_int8_t
*)tp
, len
,
138 tcp_print(register const u_char
*bp
, register u_int length
,
139 register const u_char
*bp2
, int fragmented
)
141 register const struct tcphdr
*tp
;
142 register const struct ip
*ip
;
143 register u_char flags
;
146 u_int16_t sport
, dport
, win
, urp
;
147 u_int32_t seq
, ack
, thseq
, thack
;
151 register const struct ip6_hdr
*ip6
;
154 tp
= (struct tcphdr
*)bp
;
155 ip
= (struct ip
*)bp2
;
158 ip6
= (struct ip6_hdr
*)bp2
;
163 if (!TTEST(tp
->th_dport
)) {
164 (void)printf("%s > %s: [|tcp]",
165 ipaddr_string(&ip
->ip_src
),
166 ipaddr_string(&ip
->ip_dst
));
170 sport
= EXTRACT_16BITS(&tp
->th_sport
);
171 dport
= EXTRACT_16BITS(&tp
->th_dport
);
173 hlen
= TH_OFF(tp
) * 4;
176 * If data present, header length valid, and NFS port used,
178 * Pass offset of data plus 4 bytes for RPC TCP msg length
179 * to NFS print routines.
181 if (!qflag
&& hlen
>= sizeof(*tp
) && hlen
<= length
&&
182 (length
- hlen
) >= 4) {
185 register struct sunrpc_msg
*rp
;
186 enum sunrpc_msg_type direction
;
188 fraglenp
= (u_char
*)tp
+ hlen
;
189 if (TTEST2(*fraglenp
, 4)) {
190 fraglen
= EXTRACT_32BITS(fraglenp
) & 0x7FFFFFFF;
191 if (fraglen
> (length
- hlen
) - 4)
192 fraglen
= (length
- hlen
) - 4;
193 rp
= (struct sunrpc_msg
*)(fraglenp
+ 4);
194 if (TTEST(rp
->rm_direction
)) {
195 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
196 if (dport
== NFS_PORT
&&
197 direction
== SUNRPC_CALL
) {
198 nfsreq_print((u_char
*)rp
, fraglen
,
202 if (sport
== NFS_PORT
&&
203 direction
== SUNRPC_REPLY
) {
204 nfsreply_print((u_char
*)rp
, fraglen
,
213 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
214 (void)printf("%s.%s > %s.%s: ",
215 ip6addr_string(&ip6
->ip6_src
),
216 tcpport_string(sport
),
217 ip6addr_string(&ip6
->ip6_dst
),
218 tcpport_string(dport
));
220 (void)printf("%s > %s: ",
221 tcpport_string(sport
), tcpport_string(dport
));
226 if (ip
->ip_p
== IPPROTO_TCP
) {
227 (void)printf("%s.%s > %s.%s: ",
228 ipaddr_string(&ip
->ip_src
),
229 tcpport_string(sport
),
230 ipaddr_string(&ip
->ip_dst
),
231 tcpport_string(dport
));
233 (void)printf("%s > %s: ",
234 tcpport_string(sport
), tcpport_string(dport
));
238 if (hlen
< sizeof(*tp
)) {
239 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
240 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
246 seq
= EXTRACT_32BITS(&tp
->th_seq
);
247 ack
= EXTRACT_32BITS(&tp
->th_ack
);
248 win
= EXTRACT_16BITS(&tp
->th_win
);
249 urp
= EXTRACT_16BITS(&tp
->th_urp
);
252 (void)printf("tcp %d", length
- hlen
);
254 (void)printf(" [bad hdr length %u - too long, > %u]",
260 flags
= tp
->th_flags
;
261 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
263 if (!Sflag
&& (flags
& TH_ACK
)) {
264 register struct tcp_seq_hash
*th
;
265 const void *src
, *dst
;
269 * Find (or record) the initial sequence numbers for
270 * this conversation. (we pick an arbitrary
271 * collating order so there's only one entry for
281 else if (sport
== dport
) {
282 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
286 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
287 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
288 tha
.port
= dport
<< 16 | sport
;
290 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
291 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
292 tha
.port
= sport
<< 16 | dport
;
296 * Zero out the tha structure; the src and dst
297 * fields are big enough to hold an IPv6
298 * address, but we only have IPv4 addresses
299 * and thus must clear out the remaining 124
302 * XXX - should we just clear those bytes after
303 * copying the IPv4 addresses, rather than
304 * zeroing out the entire structure and then
305 * overwriting some of the zeroes?
307 * XXX - this could fail if we see TCP packets
308 * with an IPv6 address with the lower 124 bits
309 * all zero and also see TCP packes with an
310 * IPv4 address with the same 32 bits as the
311 * upper 32 bits of the IPv6 address in question.
312 * Can that happen? Is it likely enough to be
315 memset(&tha
, 0, sizeof(tha
));
320 else if (sport
== dport
) {
321 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
325 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
326 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
327 tha
.port
= dport
<< 16 | sport
;
329 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
330 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
331 tha
.port
= sport
<< 16 | dport
;
340 else if (sport
== dport
) {
341 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
345 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
346 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
347 tha
.port
= dport
<< 16 | sport
;
349 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
350 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
351 tha
.port
= sport
<< 16 | dport
;
356 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
357 th
->nxt
; th
= th
->nxt
)
358 if (memcmp((char *)&tha
, (char *)&th
->addr
,
359 sizeof(th
->addr
)) == 0)
362 if (!th
->nxt
|| (flags
& TH_SYN
)) {
363 /* didn't find it or new conversation */
364 if (th
->nxt
== NULL
) {
365 th
->nxt
= (struct tcp_seq_hash
*)
366 calloc(1, sizeof(*th
));
368 error("tcp_print: calloc");
372 th
->ack
= seq
, th
->seq
= ack
- 1;
374 th
->seq
= seq
, th
->ack
= ack
- 1;
377 seq
-= th
->ack
, ack
-= th
->seq
;
379 seq
-= th
->seq
, ack
-= th
->ack
;
386 thseq
= thack
= threv
= 0;
389 (void)printf(" [bad hdr length %u - too long, > %u]",
394 if (vflag
&& !Kflag
&& !fragmented
) {
395 /* Check the checksum, if possible. */
396 u_int16_t sum
, tcp_sum
;
399 if (TTEST2(tp
->th_sport
, length
)) {
400 sum
= tcp_cksum(ip
, tp
, length
);
401 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
403 (void)printf(", cksum 0x%04x", tcp_sum
);
405 (void)printf(" (incorrect -> 0x%04x)",
406 in_cksum_shouldbe(tcp_sum
, sum
));
408 (void)printf(" (correct)");
412 else if (IP_V(ip
) == 6 && ip6
->ip6_plen
) {
413 if (TTEST2(tp
->th_sport
, length
)) {
414 sum
= nextproto6_cksum(ip6
, (const u_int8_t
*)tp
, length
, IPPROTO_TCP
);
415 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
417 (void)printf(", cksum 0x%04x", tcp_sum
);
419 (void)printf(" (incorrect -> 0x%04x)",
420 in_cksum_shouldbe(tcp_sum
, sum
));
422 (void)printf(" (correct)");
430 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
431 (void)printf(", seq %u", seq
);
434 (void)printf(":%u", seq
+ length
);
438 if (flags
& TH_ACK
) {
439 (void)printf(", ack %u", ack
);
442 (void)printf(", win %d", win
);
445 (void)printf(", urg %d", urp
);
447 * Handle any options.
449 if (hlen
> sizeof(*tp
)) {
450 register const u_char
*cp
;
451 register u_int i
, opt
, datalen
;
455 cp
= (const u_char
*)tp
+ sizeof(*tp
);
456 printf(", options [");
466 len
= *cp
++; /* total including type, len */
467 if (len
< 2 || len
> hlen
)
469 --hlen
; /* account for length byte */
471 --hlen
; /* account for type byte */
474 /* Bail if "l" bytes of data are not left or were not captured */
475 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
478 printf("%s", tok2str(tcp_option_values
, "Unknown Option %u", opt
));
485 (void)printf(" %u", EXTRACT_16BITS(cp
));
491 (void)printf(" %u", *cp
);
496 if (datalen
% 8 != 0) {
497 (void)printf("malformed sack");
501 (void)printf(" %d ", datalen
/ 8);
502 for (i
= 0; i
< datalen
; i
+= 8) {
504 s
= EXTRACT_32BITS(cp
+ i
);
506 e
= EXTRACT_32BITS(cp
+ i
+ 4);
514 (void)printf("{%u:%u}", s
, e
);
523 case TCPOPT_ECHOREPLY
:
526 * those options share their semantics.
531 (void)printf(" %u", EXTRACT_32BITS(cp
));
534 case TCPOPT_TIMESTAMP
:
537 (void)printf(" val %u ecr %u",
539 EXTRACT_32BITS(cp
+ 4));
542 case TCPOPT_SIGNATURE
:
543 datalen
= TCP_SIGLEN
;
545 #ifdef HAVE_LIBCRYPTO
546 switch (tcp_verify_signature(ip
, tp
,
547 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
549 case SIGNATURE_VALID
:
550 (void)printf("valid");
553 case SIGNATURE_INVALID
:
554 (void)printf("invalid");
557 case CANT_CHECK_SIGNATURE
:
558 (void)printf("can't check - ");
559 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
560 (void)printf("%02x", cp
[i
]);
564 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
565 (void)printf("%02x", cp
[i
]);
570 (void)printf("keyid %d", *cp
++);
572 for (i
= 0; i
< datalen
; ++i
) {
574 (void)printf("%02x", cp
[i
]);
583 * Nothing interesting.
591 utoval
= EXTRACT_16BITS(cp
);
592 (void)printf("0x%x", utoval
);
594 utoval
= (utoval
>> 1) * 60;
597 (void)printf(" %u", utoval
);
603 if (!mptcp_print(cp
-2, len
, flags
))
609 for (i
= 0; i
< datalen
; ++i
) {
611 (void)printf("%02x", cp
[i
]);
616 /* Account for data printed */
620 /* Check specification against observed length */
621 ++datalen
; /* option octet */
622 if (!ZEROLENOPT(opt
))
623 ++datalen
; /* size octet */
625 (void)printf("[len %d]", len
);
627 if (opt
== TCPOPT_EOL
)
634 * Print length field before crawling down the stack.
636 printf(", length %u", length
);
642 * Decode payload if necessary.
644 bp
+= TH_OFF(tp
) * 4;
645 if ((flags
& TH_RST
) && vflag
) {
646 print_tcp_rst_data(bp
, length
);
651 switch (packettype
) {
653 zmtp1_print(bp
, length
);
659 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
661 telnet_print(bp
, length
);
662 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
663 bgp_print(bp
, length
);
664 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
666 #ifdef TCPDUMP_DO_SMB
667 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
668 nbt_tcp_print(bp
, length
);
669 else if (sport
== SMB_PORT
|| dport
== SMB_PORT
)
670 smb_tcp_print(bp
, length
);
672 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
673 beep_print(bp
, length
);
674 else if (length
> 2 &&
675 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
676 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
678 * TCP DNS query has 2byte length at the head.
679 * XXX packet could be unaligned, it can go strange
681 ns_print(bp
+ 2, length
- 2, 0);
682 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
683 msdp_print(bp
, length
);
684 } else if (sport
== RPKI_RTR_PORT
|| dport
== RPKI_RTR_PORT
) {
685 rpki_rtr_print(bp
, length
);
687 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
688 ldp_print(bp
, length
);
693 fputs("[bad opt]", stdout
);
698 fputs("[|tcp]", stdout
);
704 * RFC1122 says the following on data in RST segments:
706 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
708 * A TCP SHOULD allow a received RST segment to include data.
711 * It has been suggested that a RST segment could contain
712 * ASCII text that encoded and explained the cause of the
713 * RST. No standard has yet been established for such
719 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
723 if (TTEST2(*sp
, length
))
727 if (length
> MAX_RST_DATA_LEN
) {
728 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
729 putchar('+'); /* indicate we truncate */
732 while (length
-- && sp
<= snapend
) {
739 #ifdef HAVE_LIBCRYPTO
741 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
742 const u_char
*data
, int length
, const u_char
*rcvsig
)
745 u_char sig
[TCP_SIGLEN
];
748 u_int16_t savecsum
, tlen
;
755 if (data
+ length
> snapend
) {
756 printf("snaplen too short, ");
757 return (CANT_CHECK_SIGNATURE
);
762 if (sigsecret
== NULL
) {
763 printf("shared secret not supplied with -M, ");
764 return (CANT_CHECK_SIGNATURE
);
769 * Step 1: Update MD5 hash with IP pseudo-header.
772 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
773 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
774 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
775 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
776 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
778 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
780 } else if (IP_V(ip
) == 6) {
781 ip6
= (struct ip6_hdr
*)ip
;
782 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
783 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
784 len32
= htonl(EXTRACT_16BITS(&ip6
->ip6_plen
));
785 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
787 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
788 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
789 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
791 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
795 printf("IP version not 4 or 6, ");
797 printf("IP version not 4, ");
799 return (CANT_CHECK_SIGNATURE
);
803 * Step 2: Update MD5 hash with TCP header, excluding options.
804 * The TCP checksum must be set to zero.
806 savecsum
= tp1
.th_sum
;
808 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
809 tp1
.th_sum
= savecsum
;
811 * Step 3: Update MD5 hash with TCP segment data, if present.
814 MD5_Update(&ctx
, data
, length
);
816 * Step 4: Update MD5 hash with shared secret.
818 MD5_Update(&ctx
, sigsecret
, strlen(sigsecret
));
819 MD5_Final(sig
, &ctx
);
821 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
822 return (SIGNATURE_VALID
);
824 return (SIGNATURE_INVALID
);
826 #endif /* HAVE_LIBCRYPTO */
830 * c-style: whitesmith