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.130.2.3 2007-12-22 03:08:45 guy 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>
62 #define SIGNATURE_VALID 0
63 #define SIGNATURE_INVALID 1
64 #define CANT_CHECK_SIGNATURE 2
66 static int tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
67 const u_char
*data
, int length
, const u_char
*rcvsig
);
70 static void print_tcp_rst_data(register const u_char
*sp
, u_int length
);
72 #define MAX_RST_DATA_LEN 30
87 struct tcp_seq_hash
*nxt
;
93 #define TSEQ_HASHSIZE 919
95 /* These tcp optinos do not have the size octet */
96 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
98 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
100 struct tok tcp_flag_values
[] = {
112 struct tok tcp_option_values
[] = {
113 { TCPOPT_EOL
, "eol" },
114 { TCPOPT_NOP
, "nop" },
115 { TCPOPT_MAXSEG
, "mss" },
116 { TCPOPT_WSCALE
, "wscale" },
117 { TCPOPT_SACKOK
, "sackOK" },
118 { TCPOPT_SACK
, "sack" },
119 { TCPOPT_ECHO
, "echo" },
120 { TCPOPT_ECHOREPLY
, "echoreply" },
121 { TCPOPT_TIMESTAMP
, "TS" },
123 { TCPOPT_CCNEW
, "ccnew" },
124 { TCPOPT_CCECHO
, "" },
125 { TCPOPT_SIGNATURE
, "md5" },
126 { TCPOPT_AUTH
, "enhanced auth" },
130 static int tcp_cksum(register const struct ip
*ip
,
131 register const struct tcphdr
*tp
,
146 /* pseudo-header.. */
147 phu
.ph
.len
= htons((u_int16_t
)len
);
149 phu
.ph
.proto
= IPPROTO_TCP
;
150 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
152 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
154 phu
.ph
.dst
= ip_finddst(ip
);
157 return in_cksum((u_short
*)tp
, len
,
158 sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
162 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
169 struct in6_addr ph_src
;
170 struct in6_addr ph_dst
;
179 memset(&phu
, 0, sizeof(phu
));
180 phu
.ph
.ph_src
= ip6
->ip6_src
;
181 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
182 phu
.ph
.ph_len
= htonl(len
);
183 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
185 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
188 return in_cksum((u_short
*)tp
, len
, sum
);
193 tcp_print(register const u_char
*bp
, register u_int length
,
194 register const u_char
*bp2
, int fragmented
)
196 register const struct tcphdr
*tp
;
197 register const struct ip
*ip
;
198 register u_char flags
;
201 u_int16_t sport
, dport
, win
, urp
;
202 u_int32_t seq
, ack
, thseq
, thack
;
205 register const struct ip6_hdr
*ip6
;
208 tp
= (struct tcphdr
*)bp
;
209 ip
= (struct ip
*)bp2
;
212 ip6
= (struct ip6_hdr
*)bp2
;
217 if (!TTEST(tp
->th_dport
)) {
218 (void)printf("%s > %s: [|tcp]",
219 ipaddr_string(&ip
->ip_src
),
220 ipaddr_string(&ip
->ip_dst
));
224 sport
= EXTRACT_16BITS(&tp
->th_sport
);
225 dport
= EXTRACT_16BITS(&tp
->th_dport
);
227 hlen
= TH_OFF(tp
) * 4;
230 * If data present, header length valid, and NFS port used,
232 * Pass offset of data plus 4 bytes for RPC TCP msg length
233 * to NFS print routines.
235 if (!qflag
&& hlen
>= sizeof(*tp
) && hlen
<= length
&&
236 (length
- hlen
) >= 4) {
239 register struct sunrpc_msg
*rp
;
240 enum sunrpc_msg_type direction
;
242 fraglenp
= (u_char
*)tp
+ hlen
;
243 if (TTEST2(*fraglenp
, 4)) {
244 fraglen
= EXTRACT_32BITS(fraglenp
) & 0x7FFFFFFF;
245 if (fraglen
> (length
- hlen
) - 4)
246 fraglen
= (length
- hlen
) - 4;
247 rp
= (struct sunrpc_msg
*)(fraglenp
+ 4);
248 if (TTEST(rp
->rm_direction
)) {
249 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
250 if (dport
== NFS_PORT
&&
251 direction
== SUNRPC_CALL
) {
252 nfsreq_print((u_char
*)rp
, fraglen
,
256 if (sport
== NFS_PORT
&&
257 direction
== SUNRPC_REPLY
) {
258 nfsreply_print((u_char
*)rp
, fraglen
,
267 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
268 (void)printf("%s.%s > %s.%s: ",
269 ip6addr_string(&ip6
->ip6_src
),
270 tcpport_string(sport
),
271 ip6addr_string(&ip6
->ip6_dst
),
272 tcpport_string(dport
));
274 (void)printf("%s > %s: ",
275 tcpport_string(sport
), tcpport_string(dport
));
280 if (ip
->ip_p
== IPPROTO_TCP
) {
281 (void)printf("%s.%s > %s.%s: ",
282 ipaddr_string(&ip
->ip_src
),
283 tcpport_string(sport
),
284 ipaddr_string(&ip
->ip_dst
),
285 tcpport_string(dport
));
287 (void)printf("%s > %s: ",
288 tcpport_string(sport
), tcpport_string(dport
));
292 if (hlen
< sizeof(*tp
)) {
293 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
294 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
300 seq
= EXTRACT_32BITS(&tp
->th_seq
);
301 ack
= EXTRACT_32BITS(&tp
->th_ack
);
302 win
= EXTRACT_16BITS(&tp
->th_win
);
303 urp
= EXTRACT_16BITS(&tp
->th_urp
);
306 (void)printf("tcp %d", length
- hlen
);
308 (void)printf(" [bad hdr length %u - too long, > %u]",
314 flags
= tp
->th_flags
;
315 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
317 if (!Sflag
&& (flags
& TH_ACK
)) {
318 register struct tcp_seq_hash
*th
;
319 const void *src
, *dst
;
323 * Find (or record) the initial sequence numbers for
324 * this conversation. (we pick an arbitrary
325 * collating order so there's only one entry for
329 memset(&tha
, 0, sizeof(tha
));
336 else if (sport
== dport
) {
337 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
341 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
342 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
343 tha
.port
= dport
<< 16 | sport
;
345 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
346 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
347 tha
.port
= sport
<< 16 | dport
;
354 else if (sport
== dport
) {
355 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
359 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
360 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
361 tha
.port
= dport
<< 16 | sport
;
363 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
364 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
365 tha
.port
= sport
<< 16 | dport
;
374 else if (sport
== dport
) {
375 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
379 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
380 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
381 tha
.port
= dport
<< 16 | sport
;
383 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
384 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
385 tha
.port
= sport
<< 16 | dport
;
390 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
391 th
->nxt
; th
= th
->nxt
)
392 if (memcmp((char *)&tha
, (char *)&th
->addr
,
393 sizeof(th
->addr
)) == 0)
396 if (!th
->nxt
|| (flags
& TH_SYN
)) {
397 /* didn't find it or new conversation */
398 if (th
->nxt
== NULL
) {
399 th
->nxt
= (struct tcp_seq_hash
*)
400 calloc(1, sizeof(*th
));
402 error("tcp_print: calloc");
406 th
->ack
= seq
, th
->seq
= ack
- 1;
408 th
->seq
= seq
, th
->ack
= ack
- 1;
411 seq
-= th
->ack
, ack
-= th
->seq
;
413 seq
-= th
->seq
, ack
-= th
->ack
;
420 thseq
= thack
= threv
= 0;
423 (void)printf(" [bad hdr length %u - too long, > %u]",
428 if (IP_V(ip
) == 4 && vflag
&& !Kflag
&& !fragmented
) {
429 u_int16_t sum
, tcp_sum
;
430 if (TTEST2(tp
->th_sport
, length
)) {
431 sum
= tcp_cksum(ip
, tp
, length
);
433 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
435 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
436 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum
, sum
));
438 (void)printf(" (correct)");
442 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !Kflag
&& !fragmented
) {
443 u_int16_t sum
,tcp_sum
;
444 if (TTEST2(tp
->th_sport
, length
)) {
445 sum
= tcp6_cksum(ip6
, tp
, length
);
446 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
448 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
449 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum
, sum
));
451 (void)printf(" (correct)");
458 if (vflag
> 1 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
459 (void)printf(", seq %u", seq
);
462 (void)printf(":%u", seq
+ length
);
466 if (flags
& TH_ACK
) {
467 (void)printf(", ack %u", ack
);
470 (void)printf(", win %d", win
);
473 (void)printf(", urg %d", urp
);
475 * Handle any options.
477 if (hlen
> sizeof(*tp
)) {
478 register const u_char
*cp
;
479 register u_int i
, opt
, datalen
;
483 cp
= (const u_char
*)tp
+ sizeof(*tp
);
484 printf(", options [");
494 len
= *cp
++; /* total including type, len */
495 if (len
< 2 || len
> hlen
)
497 --hlen
; /* account for length byte */
499 --hlen
; /* account for type byte */
502 /* Bail if "l" bytes of data are not left or were not captured */
503 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
506 printf("%s", tok2str(tcp_option_values
, "Unknown Option %u", opt
));
513 (void)printf(" %u", EXTRACT_16BITS(cp
));
519 (void)printf(" %u", *cp
);
524 if (datalen
% 8 != 0) {
525 (void)printf("malformed sack");
529 (void)printf(" %d ", datalen
/ 8);
530 for (i
= 0; i
< datalen
; i
+= 8) {
532 s
= EXTRACT_32BITS(cp
+ i
);
534 e
= EXTRACT_32BITS(cp
+ i
+ 4);
542 (void)printf("{%u:%u}", s
, e
);
551 case TCPOPT_ECHOREPLY
:
554 * those options share their semantics.
559 (void)printf(" %u", EXTRACT_32BITS(cp
));
562 case TCPOPT_TIMESTAMP
:
565 (void)printf(" val %u ecr %u",
567 EXTRACT_32BITS(cp
+ 4));
570 case TCPOPT_SIGNATURE
:
571 datalen
= TCP_SIGLEN
;
573 #ifdef HAVE_LIBCRYPTO
574 switch (tcp_verify_signature(ip
, tp
,
575 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
577 case SIGNATURE_VALID
:
578 (void)printf("valid");
581 case SIGNATURE_INVALID
:
582 (void)printf("invalid");
585 case CANT_CHECK_SIGNATURE
:
586 (void)printf("can't check - ");
587 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
588 (void)printf("%02x", cp
[i
]);
592 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
593 (void)printf("%02x", cp
[i
]);
598 (void)printf("keyid %d", *cp
++);
600 for (i
= 0; i
< datalen
; ++i
) {
602 (void)printf("%02x", cp
[i
]);
611 * Nothing interesting.
618 for (i
= 0; i
< datalen
; ++i
) {
620 (void)printf("%02x", cp
[i
]);
625 /* Account for data printed */
629 /* Check specification against observed length */
630 ++datalen
; /* option octet */
631 if (!ZEROLENOPT(opt
))
632 ++datalen
; /* size octet */
634 (void)printf("[len %d]", len
);
636 if (opt
== TCPOPT_EOL
)
643 * Print length field before crawling down the stack.
645 printf(", length %u", length
);
651 * Decode payload if necessary.
653 bp
+= TH_OFF(tp
) * 4;
654 if ((flags
& TH_RST
) && vflag
) {
655 print_tcp_rst_data(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
);
685 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
686 ldp_print(bp
, length
);
691 fputs("[bad opt]", stdout
);
696 fputs("[|tcp]", stdout
);
702 * RFC1122 says the following on data in RST segments:
704 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
706 * A TCP SHOULD allow a received RST segment to include data.
709 * It has been suggested that a RST segment could contain
710 * ASCII text that encoded and explained the cause of the
711 * RST. No standard has yet been established for such
717 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
721 if (TTEST2(*sp
, length
))
725 if (length
> MAX_RST_DATA_LEN
) {
726 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
727 putchar('+'); /* indicate we truncate */
730 while (length
-- && sp
<= snapend
) {
737 #ifdef HAVE_LIBCRYPTO
739 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
740 const u_char
*data
, int length
, const u_char
*rcvsig
)
743 u_char sig
[TCP_SIGLEN
];
746 u_int16_t savecsum
, tlen
;
755 if (tcpmd5secret
== NULL
)
756 return (CANT_CHECK_SIGNATURE
);
760 * Step 1: Update MD5 hash with IP pseudo-header.
763 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
764 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
765 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
766 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
767 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
769 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
771 } else if (IP_V(ip
) == 6) {
772 ip6
= (struct ip6_hdr
*)ip
;
773 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
774 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
775 len32
= htonl(ntohs(ip6
->ip6_plen
));
776 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
778 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
779 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
780 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
782 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
785 return (CANT_CHECK_SIGNATURE
);
788 * Step 2: Update MD5 hash with TCP header, excluding options.
789 * The TCP checksum must be set to zero.
791 savecsum
= tp1
.th_sum
;
793 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
794 tp1
.th_sum
= savecsum
;
796 * Step 3: Update MD5 hash with TCP segment data, if present.
799 MD5_Update(&ctx
, data
, length
);
801 * Step 4: Update MD5 hash with shared secret.
803 MD5_Update(&ctx
, tcpmd5secret
, strlen(tcpmd5secret
));
804 MD5_Final(sig
, &ctx
);
806 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
807 return (SIGNATURE_VALID
);
809 return (SIGNATURE_INVALID
);
811 #endif /* HAVE_LIBCRYPTO */
815 * c-style: whitesmith