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 2007-08-29 02:31:44 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>
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 if ((u_char
*)tp
+ 4 + sizeof(struct sunrpc_msg
) <= snapend
&&
238 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
241 } else if ((u_char
*)tp
+ 4 + sizeof(struct sunrpc_msg
)
244 nfsreply_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
251 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
252 (void)printf("%s.%s > %s.%s: ",
253 ip6addr_string(&ip6
->ip6_src
),
254 tcpport_string(sport
),
255 ip6addr_string(&ip6
->ip6_dst
),
256 tcpport_string(dport
));
258 (void)printf("%s > %s: ",
259 tcpport_string(sport
), tcpport_string(dport
));
264 if (ip
->ip_p
== IPPROTO_TCP
) {
265 (void)printf("%s.%s > %s.%s: ",
266 ipaddr_string(&ip
->ip_src
),
267 tcpport_string(sport
),
268 ipaddr_string(&ip
->ip_dst
),
269 tcpport_string(dport
));
271 (void)printf("%s > %s: ",
272 tcpport_string(sport
), tcpport_string(dport
));
276 if (hlen
< sizeof(*tp
)) {
277 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
278 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
284 seq
= EXTRACT_32BITS(&tp
->th_seq
);
285 ack
= EXTRACT_32BITS(&tp
->th_ack
);
286 win
= EXTRACT_16BITS(&tp
->th_win
);
287 urp
= EXTRACT_16BITS(&tp
->th_urp
);
290 (void)printf("tcp %d", length
- hlen
);
292 (void)printf(" [bad hdr length %u - too long, > %u]",
298 flags
= tp
->th_flags
;
299 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
301 if (!Sflag
&& (flags
& TH_ACK
)) {
302 register struct tcp_seq_hash
*th
;
303 const void *src
, *dst
;
307 * Find (or record) the initial sequence numbers for
308 * this conversation. (we pick an arbitrary
309 * collating order so there's only one entry for
313 memset(&tha
, 0, sizeof(tha
));
320 else if (sport
== dport
) {
321 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
325 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
326 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
327 tha
.port
= dport
<< 16 | sport
;
329 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
330 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
331 tha
.port
= sport
<< 16 | dport
;
338 else if (sport
== dport
) {
339 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
343 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
344 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
345 tha
.port
= dport
<< 16 | sport
;
347 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
348 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
349 tha
.port
= sport
<< 16 | dport
;
358 else if (sport
== dport
) {
359 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
363 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
364 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
365 tha
.port
= dport
<< 16 | sport
;
367 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
368 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
369 tha
.port
= sport
<< 16 | dport
;
374 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
375 th
->nxt
; th
= th
->nxt
)
376 if (memcmp((char *)&tha
, (char *)&th
->addr
,
377 sizeof(th
->addr
)) == 0)
380 if (!th
->nxt
|| (flags
& TH_SYN
)) {
381 /* didn't find it or new conversation */
382 if (th
->nxt
== NULL
) {
383 th
->nxt
= (struct tcp_seq_hash
*)
384 calloc(1, sizeof(*th
));
386 error("tcp_print: calloc");
390 th
->ack
= seq
, th
->seq
= ack
- 1;
392 th
->seq
= seq
, th
->ack
= ack
- 1;
395 seq
-= th
->ack
, ack
-= th
->seq
;
397 seq
-= th
->seq
, ack
-= th
->ack
;
404 thseq
= thack
= threv
= 0;
407 (void)printf(" [bad hdr length %u - too long, > %u]",
412 if (IP_V(ip
) == 4 && vflag
&& !Kflag
&& !fragmented
) {
413 u_int16_t sum
, tcp_sum
;
414 if (TTEST2(tp
->th_sport
, length
)) {
415 sum
= tcp_cksum(ip
, tp
, length
);
417 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
419 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
420 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum
, sum
));
422 (void)printf(" (correct)");
426 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !Kflag
&& !fragmented
) {
427 u_int16_t sum
,tcp_sum
;
428 if (TTEST2(tp
->th_sport
, length
)) {
429 sum
= tcp6_cksum(ip6
, 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)");
442 if (vflag
> 1 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
443 (void)printf(", seq %u", seq
);
446 (void)printf(":%u", seq
+ length
);
450 if (flags
& TH_ACK
) {
451 (void)printf(", ack %u", ack
);
454 (void)printf(", win %d", win
);
457 (void)printf(", urg %d", urp
);
459 * Handle any options.
461 if (hlen
> sizeof(*tp
)) {
462 register const u_char
*cp
;
463 register u_int i
, opt
, datalen
;
467 cp
= (const u_char
*)tp
+ sizeof(*tp
);
468 printf(", options [");
478 len
= *cp
++; /* total including type, len */
479 if (len
< 2 || len
> hlen
)
481 --hlen
; /* account for length byte */
483 --hlen
; /* account for type byte */
486 /* Bail if "l" bytes of data are not left or were not captured */
487 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
490 printf("%s", tok2str(tcp_option_values
, "Unknown Option %u", opt
));
497 (void)printf(" %u", EXTRACT_16BITS(cp
));
503 (void)printf(" %u", *cp
);
508 if (datalen
% 8 != 0) {
509 (void)printf("malformed sack");
513 (void)printf(" %d ", datalen
/ 8);
514 for (i
= 0; i
< datalen
; i
+= 8) {
516 s
= EXTRACT_32BITS(cp
+ i
);
518 e
= EXTRACT_32BITS(cp
+ i
+ 4);
526 (void)printf("{%u:%u}", s
, e
);
535 case TCPOPT_ECHOREPLY
:
538 * those options share their semantics.
543 (void)printf(" %u", EXTRACT_32BITS(cp
));
546 case TCPOPT_TIMESTAMP
:
549 (void)printf(" val %u ecr %u",
551 EXTRACT_32BITS(cp
+ 4));
554 case TCPOPT_SIGNATURE
:
555 datalen
= TCP_SIGLEN
;
557 #ifdef HAVE_LIBCRYPTO
558 switch (tcp_verify_signature(ip
, tp
,
559 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
561 case SIGNATURE_VALID
:
562 (void)printf("valid");
565 case SIGNATURE_INVALID
:
566 (void)printf("invalid");
569 case CANT_CHECK_SIGNATURE
:
570 (void)printf("can't check - ");
571 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
572 (void)printf("%02x", cp
[i
]);
576 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
577 (void)printf("%02x", cp
[i
]);
582 (void)printf("keyid %d", *cp
++);
584 for (i
= 0; i
< datalen
; ++i
) {
586 (void)printf("%02x", cp
[i
]);
595 * Nothing interesting.
602 for (i
= 0; i
< datalen
; ++i
) {
604 (void)printf("%02x", cp
[i
]);
609 /* Account for data printed */
613 /* Check specification against observed length */
614 ++datalen
; /* option octet */
615 if (!ZEROLENOPT(opt
))
616 ++datalen
; /* size octet */
618 (void)printf("[len %d]", len
);
620 if (opt
== TCPOPT_EOL
)
627 * Print length field before crawling down the stack.
629 printf(", length %u", length
);
635 * Decode payload if necessary.
637 bp
+= TH_OFF(tp
) * 4;
638 if ((flags
& TH_RST
) && vflag
) {
639 print_tcp_rst_data(bp
, length
);
643 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
645 telnet_print(bp
, length
);
646 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
647 bgp_print(bp
, length
);
648 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
650 #ifdef TCPDUMP_DO_SMB
651 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
652 nbt_tcp_print(bp
, length
);
654 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
655 beep_print(bp
, length
);
656 else if (length
> 2 &&
657 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
658 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
660 * TCP DNS query has 2byte length at the head.
661 * XXX packet could be unaligned, it can go strange
663 ns_print(bp
+ 2, length
- 2, 0);
664 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
665 msdp_print(bp
, length
);
667 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
668 ldp_print(bp
, length
);
673 fputs("[bad opt]", stdout
);
678 fputs("[|tcp]", stdout
);
684 * RFC1122 says the following on data in RST segments:
686 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
688 * A TCP SHOULD allow a received RST segment to include data.
691 * It has been suggested that a RST segment could contain
692 * ASCII text that encoded and explained the cause of the
693 * RST. No standard has yet been established for such
699 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
703 if (TTEST2(*sp
, length
))
707 if (length
> MAX_RST_DATA_LEN
) {
708 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
709 putchar('+'); /* indicate we truncate */
712 while (length
-- && sp
<= snapend
) {
719 #ifdef HAVE_LIBCRYPTO
721 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
722 const u_char
*data
, int length
, const u_char
*rcvsig
)
725 u_char sig
[TCP_SIGLEN
];
728 u_int16_t savecsum
, tlen
;
737 if (tcpmd5secret
== NULL
)
738 return (CANT_CHECK_SIGNATURE
);
742 * Step 1: Update MD5 hash with IP pseudo-header.
745 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
746 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
747 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
748 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
749 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
751 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
753 } else if (IP_V(ip
) == 6) {
754 ip6
= (struct ip6_hdr
*)ip
;
755 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
756 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
757 len32
= htonl(ntohs(ip6
->ip6_plen
));
758 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
760 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
761 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
762 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
764 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
767 return (CANT_CHECK_SIGNATURE
);
770 * Step 2: Update MD5 hash with TCP header, excluding options.
771 * The TCP checksum must be set to zero.
773 savecsum
= tp1
.th_sum
;
775 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
776 tp1
.th_sum
= savecsum
;
778 * Step 3: Update MD5 hash with TCP segment data, if present.
781 MD5_Update(&ctx
, data
, length
);
783 * Step 4: Update MD5 hash with shared secret.
785 MD5_Update(&ctx
, tcpmd5secret
, strlen(tcpmd5secret
));
786 MD5_Final(sig
, &ctx
);
788 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
789 return (SIGNATURE_VALID
);
791 return (SIGNATURE_INVALID
);
793 #endif /* HAVE_LIBCRYPTO */
797 * c-style: whitesmith