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" },
127 static int tcp_cksum(register const struct ip
*ip
,
128 register const struct tcphdr
*tp
,
143 /* pseudo-header.. */
144 phu
.ph
.len
= htons((u_int16_t
)len
);
146 phu
.ph
.proto
= IPPROTO_TCP
;
147 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
149 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
151 phu
.ph
.dst
= ip_finddst(ip
);
154 return in_cksum((u_short
*)tp
, len
,
155 sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
159 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
166 struct in6_addr ph_src
;
167 struct in6_addr ph_dst
;
176 memset(&phu
, 0, sizeof(phu
));
177 phu
.ph
.ph_src
= ip6
->ip6_src
;
178 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
179 phu
.ph
.ph_len
= htonl(len
);
180 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
182 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
185 return in_cksum((u_short
*)tp
, len
, sum
);
190 tcp_print(register const u_char
*bp
, register u_int length
,
191 register const u_char
*bp2
, int fragmented
)
193 register const struct tcphdr
*tp
;
194 register const struct ip
*ip
;
195 register u_char flags
;
198 u_int16_t sport
, dport
, win
, urp
;
199 u_int32_t seq
, ack
, thseq
, thack
;
202 register const struct ip6_hdr
*ip6
;
205 tp
= (struct tcphdr
*)bp
;
206 ip
= (struct ip
*)bp2
;
209 ip6
= (struct ip6_hdr
*)bp2
;
214 if (!TTEST(tp
->th_dport
)) {
215 (void)printf("%s > %s: [|tcp]",
216 ipaddr_string(&ip
->ip_src
),
217 ipaddr_string(&ip
->ip_dst
));
221 sport
= EXTRACT_16BITS(&tp
->th_sport
);
222 dport
= EXTRACT_16BITS(&tp
->th_dport
);
224 hlen
= TH_OFF(tp
) * 4;
227 * If data present, header length valid, and NFS port used,
229 * Pass offset of data plus 4 bytes for RPC TCP msg length
230 * to NFS print routines.
232 if (!qflag
&& hlen
>= sizeof(*tp
) && hlen
<= length
&&
233 (length
- hlen
) >= 4) {
236 register struct sunrpc_msg
*rp
;
237 enum sunrpc_msg_type direction
;
239 fraglenp
= (u_char
*)tp
+ hlen
;
240 if (TTEST2(*fraglenp
, 4)) {
241 fraglen
= EXTRACT_32BITS(fraglenp
) & 0x7FFFFFFF;
242 if (fraglen
> (length
- hlen
) - 4)
243 fraglen
= (length
- hlen
) - 4;
244 rp
= (struct sunrpc_msg
*)(fraglenp
+ 4);
245 if (TTEST(rp
->rm_direction
)) {
246 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
247 if (dport
== NFS_PORT
&&
248 direction
== SUNRPC_CALL
) {
249 nfsreq_print((u_char
*)rp
, fraglen
,
253 if (sport
== NFS_PORT
&&
254 direction
== SUNRPC_REPLY
) {
255 nfsreply_print((u_char
*)rp
, fraglen
,
264 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
265 (void)printf("%s.%s > %s.%s: ",
266 ip6addr_string(&ip6
->ip6_src
),
267 tcpport_string(sport
),
268 ip6addr_string(&ip6
->ip6_dst
),
269 tcpport_string(dport
));
271 (void)printf("%s > %s: ",
272 tcpport_string(sport
), tcpport_string(dport
));
277 if (ip
->ip_p
== IPPROTO_TCP
) {
278 (void)printf("%s.%s > %s.%s: ",
279 ipaddr_string(&ip
->ip_src
),
280 tcpport_string(sport
),
281 ipaddr_string(&ip
->ip_dst
),
282 tcpport_string(dport
));
284 (void)printf("%s > %s: ",
285 tcpport_string(sport
), tcpport_string(dport
));
289 if (hlen
< sizeof(*tp
)) {
290 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
291 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
297 seq
= EXTRACT_32BITS(&tp
->th_seq
);
298 ack
= EXTRACT_32BITS(&tp
->th_ack
);
299 win
= EXTRACT_16BITS(&tp
->th_win
);
300 urp
= EXTRACT_16BITS(&tp
->th_urp
);
303 (void)printf("tcp %d", length
- hlen
);
305 (void)printf(" [bad hdr length %u - too long, > %u]",
311 flags
= tp
->th_flags
;
312 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
314 if (!Sflag
&& (flags
& TH_ACK
)) {
315 register struct tcp_seq_hash
*th
;
316 const void *src
, *dst
;
320 * Find (or record) the initial sequence numbers for
321 * this conversation. (we pick an arbitrary
322 * collating order so there's only one entry for
326 memset(&tha
, 0, sizeof(tha
));
333 else if (sport
== dport
) {
334 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
338 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
339 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
340 tha
.port
= dport
<< 16 | sport
;
342 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
343 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
344 tha
.port
= sport
<< 16 | dport
;
351 else if (sport
== dport
) {
352 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
356 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
357 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
358 tha
.port
= dport
<< 16 | sport
;
360 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
361 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
362 tha
.port
= sport
<< 16 | dport
;
371 else if (sport
== dport
) {
372 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
376 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
377 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
378 tha
.port
= dport
<< 16 | sport
;
380 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
381 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
382 tha
.port
= sport
<< 16 | dport
;
387 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
388 th
->nxt
; th
= th
->nxt
)
389 if (memcmp((char *)&tha
, (char *)&th
->addr
,
390 sizeof(th
->addr
)) == 0)
393 if (!th
->nxt
|| (flags
& TH_SYN
)) {
394 /* didn't find it or new conversation */
395 if (th
->nxt
== NULL
) {
396 th
->nxt
= (struct tcp_seq_hash
*)
397 calloc(1, sizeof(*th
));
399 error("tcp_print: calloc");
403 th
->ack
= seq
, th
->seq
= ack
- 1;
405 th
->seq
= seq
, th
->ack
= ack
- 1;
408 seq
-= th
->ack
, ack
-= th
->seq
;
410 seq
-= th
->seq
, ack
-= th
->ack
;
417 thseq
= thack
= threv
= 0;
420 (void)printf(" [bad hdr length %u - too long, > %u]",
425 if (IP_V(ip
) == 4 && vflag
&& !Kflag
&& !fragmented
) {
426 u_int16_t sum
, tcp_sum
;
427 if (TTEST2(tp
->th_sport
, length
)) {
428 sum
= tcp_cksum(ip
, tp
, length
);
430 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
432 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
433 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum
, sum
));
435 (void)printf(" (correct)");
439 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !Kflag
&& !fragmented
) {
440 u_int16_t sum
,tcp_sum
;
441 if (TTEST2(tp
->th_sport
, length
)) {
442 sum
= tcp6_cksum(ip6
, tp
, length
);
443 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
445 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
446 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum
, sum
));
448 (void)printf(" (correct)");
455 if (vflag
> 1 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
456 (void)printf(", seq %u", seq
);
459 (void)printf(":%u", seq
+ length
);
463 if (flags
& TH_ACK
) {
464 (void)printf(", ack %u", ack
);
467 (void)printf(", win %d", win
);
470 (void)printf(", urg %d", urp
);
472 * Handle any options.
474 if (hlen
> sizeof(*tp
)) {
475 register const u_char
*cp
;
476 register u_int i
, opt
, datalen
;
480 cp
= (const u_char
*)tp
+ sizeof(*tp
);
481 printf(", options [");
491 len
= *cp
++; /* total including type, len */
492 if (len
< 2 || len
> hlen
)
494 --hlen
; /* account for length byte */
496 --hlen
; /* account for type byte */
499 /* Bail if "l" bytes of data are not left or were not captured */
500 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
503 printf("%s", tok2str(tcp_option_values
, "Unknown Option %u", opt
));
510 (void)printf(" %u", EXTRACT_16BITS(cp
));
516 (void)printf(" %u", *cp
);
521 if (datalen
% 8 != 0) {
522 (void)printf("malformed sack");
526 (void)printf(" %d ", datalen
/ 8);
527 for (i
= 0; i
< datalen
; i
+= 8) {
529 s
= EXTRACT_32BITS(cp
+ i
);
531 e
= EXTRACT_32BITS(cp
+ i
+ 4);
539 (void)printf("{%u:%u}", s
, e
);
548 case TCPOPT_ECHOREPLY
:
551 * those options share their semantics.
556 (void)printf(" %u", EXTRACT_32BITS(cp
));
559 case TCPOPT_TIMESTAMP
:
562 (void)printf(" val %u ecr %u",
564 EXTRACT_32BITS(cp
+ 4));
567 case TCPOPT_SIGNATURE
:
568 datalen
= TCP_SIGLEN
;
570 #ifdef HAVE_LIBCRYPTO
571 switch (tcp_verify_signature(ip
, tp
,
572 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
574 case SIGNATURE_VALID
:
575 (void)printf("valid");
578 case SIGNATURE_INVALID
:
579 (void)printf("invalid");
582 case CANT_CHECK_SIGNATURE
:
583 (void)printf("can't check - ");
584 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
585 (void)printf("%02x", cp
[i
]);
589 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
590 (void)printf("%02x", cp
[i
]);
595 (void)printf("keyid %d", *cp
++);
597 for (i
= 0; i
< datalen
; ++i
) {
599 (void)printf("%02x", cp
[i
]);
608 * Nothing interesting.
615 for (i
= 0; i
< datalen
; ++i
) {
617 (void)printf("%02x", cp
[i
]);
622 /* Account for data printed */
626 /* Check specification against observed length */
627 ++datalen
; /* option octet */
628 if (!ZEROLENOPT(opt
))
629 ++datalen
; /* size octet */
631 (void)printf("[len %d]", len
);
633 if (opt
== TCPOPT_EOL
)
640 * Print length field before crawling down the stack.
642 printf(", length %u", length
);
648 * Decode payload if necessary.
650 bp
+= TH_OFF(tp
) * 4;
651 if ((flags
& TH_RST
) && vflag
) {
652 print_tcp_rst_data(bp
, length
);
656 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
658 telnet_print(bp
, length
);
659 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
660 bgp_print(bp
, length
);
661 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
663 #ifdef TCPDUMP_DO_SMB
664 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
665 nbt_tcp_print(bp
, length
);
666 else if (sport
== SMB_PORT
|| dport
== SMB_PORT
)
667 smb_tcp_print(bp
, length
);
669 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
670 beep_print(bp
, length
);
671 else if (length
> 2 &&
672 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
673 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
675 * TCP DNS query has 2byte length at the head.
676 * XXX packet could be unaligned, it can go strange
678 ns_print(bp
+ 2, length
- 2, 0);
679 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
680 msdp_print(bp
, length
);
682 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
683 ldp_print(bp
, length
);
688 fputs("[bad opt]", stdout
);
693 fputs("[|tcp]", stdout
);
699 * RFC1122 says the following on data in RST segments:
701 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
703 * A TCP SHOULD allow a received RST segment to include data.
706 * It has been suggested that a RST segment could contain
707 * ASCII text that encoded and explained the cause of the
708 * RST. No standard has yet been established for such
714 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
718 if (TTEST2(*sp
, length
))
722 if (length
> MAX_RST_DATA_LEN
) {
723 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
724 putchar('+'); /* indicate we truncate */
727 while (length
-- && sp
<= snapend
) {
734 #ifdef HAVE_LIBCRYPTO
736 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
737 const u_char
*data
, int length
, const u_char
*rcvsig
)
740 u_char sig
[TCP_SIGLEN
];
743 u_int16_t savecsum
, tlen
;
750 if (data
+ length
> snapend
) {
751 printf("snaplen too short, ");
752 return (CANT_CHECK_SIGNATURE
);
757 if (sigsecret
== NULL
) {
758 printf("shared secret not supplied with -M, ");
759 return (CANT_CHECK_SIGNATURE
);
764 * Step 1: Update MD5 hash with IP pseudo-header.
767 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
768 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
769 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
770 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
771 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
773 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
775 } else if (IP_V(ip
) == 6) {
776 ip6
= (struct ip6_hdr
*)ip
;
777 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
778 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
779 len32
= htonl(ntohs(ip6
->ip6_plen
));
780 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
782 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
783 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
784 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
786 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
790 printf("IP version not 4 or 6, ");
792 printf("IP version not 4, ");
794 return (CANT_CHECK_SIGNATURE
);
798 * Step 2: Update MD5 hash with TCP header, excluding options.
799 * The TCP checksum must be set to zero.
801 savecsum
= tp1
.th_sum
;
803 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
804 tp1
.th_sum
= savecsum
;
806 * Step 3: Update MD5 hash with TCP segment data, if present.
809 MD5_Update(&ctx
, data
, length
);
811 * Step 4: Update MD5 hash with shared secret.
813 MD5_Update(&ctx
, sigsecret
, strlen(sigsecret
));
814 MD5_Final(sig
, &ctx
);
816 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
817 return (SIGNATURE_VALID
);
819 return (SIGNATURE_INVALID
);
821 #endif /* HAVE_LIBCRYPTO */
825 * c-style: whitesmith