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" },
128 static int tcp_cksum(register const struct ip
*ip
,
129 register const struct tcphdr
*tp
,
144 /* pseudo-header.. */
145 phu
.ph
.len
= htons((u_int16_t
)len
);
147 phu
.ph
.proto
= IPPROTO_TCP
;
148 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
150 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
152 phu
.ph
.dst
= ip_finddst(ip
);
155 return in_cksum((u_short
*)tp
, len
,
156 sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
160 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
167 struct in6_addr ph_src
;
168 struct in6_addr ph_dst
;
177 memset(&phu
, 0, sizeof(phu
));
178 phu
.ph
.ph_src
= ip6
->ip6_src
;
179 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
180 phu
.ph
.ph_len
= htonl(len
);
181 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
183 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
186 return in_cksum((u_short
*)tp
, len
, sum
);
191 tcp_print(register const u_char
*bp
, register u_int length
,
192 register const u_char
*bp2
, int fragmented
)
194 register const struct tcphdr
*tp
;
195 register const struct ip
*ip
;
196 register u_char flags
;
199 u_int16_t sport
, dport
, win
, urp
;
200 u_int32_t seq
, ack
, thseq
, thack
;
203 register const struct ip6_hdr
*ip6
;
206 tp
= (struct tcphdr
*)bp
;
207 ip
= (struct ip
*)bp2
;
210 ip6
= (struct ip6_hdr
*)bp2
;
215 if (!TTEST(tp
->th_dport
)) {
216 (void)printf("%s > %s: [|tcp]",
217 ipaddr_string(&ip
->ip_src
),
218 ipaddr_string(&ip
->ip_dst
));
222 sport
= EXTRACT_16BITS(&tp
->th_sport
);
223 dport
= EXTRACT_16BITS(&tp
->th_dport
);
225 hlen
= TH_OFF(tp
) * 4;
228 * If data present, header length valid, and NFS port used,
230 * Pass offset of data plus 4 bytes for RPC TCP msg length
231 * to NFS print routines.
233 if (!qflag
&& hlen
>= sizeof(*tp
) && hlen
<= length
&&
234 (length
- hlen
) >= 4) {
237 register struct sunrpc_msg
*rp
;
238 enum sunrpc_msg_type direction
;
240 fraglenp
= (u_char
*)tp
+ hlen
;
241 if (TTEST2(*fraglenp
, 4)) {
242 fraglen
= EXTRACT_32BITS(fraglenp
) & 0x7FFFFFFF;
243 if (fraglen
> (length
- hlen
) - 4)
244 fraglen
= (length
- hlen
) - 4;
245 rp
= (struct sunrpc_msg
*)(fraglenp
+ 4);
246 if (TTEST(rp
->rm_direction
)) {
247 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
248 if (dport
== NFS_PORT
&&
249 direction
== SUNRPC_CALL
) {
250 nfsreq_print((u_char
*)rp
, fraglen
,
254 if (sport
== NFS_PORT
&&
255 direction
== SUNRPC_REPLY
) {
256 nfsreply_print((u_char
*)rp
, fraglen
,
265 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
266 (void)printf("%s.%s > %s.%s: ",
267 ip6addr_string(&ip6
->ip6_src
),
268 tcpport_string(sport
),
269 ip6addr_string(&ip6
->ip6_dst
),
270 tcpport_string(dport
));
272 (void)printf("%s > %s: ",
273 tcpport_string(sport
), tcpport_string(dport
));
278 if (ip
->ip_p
== IPPROTO_TCP
) {
279 (void)printf("%s.%s > %s.%s: ",
280 ipaddr_string(&ip
->ip_src
),
281 tcpport_string(sport
),
282 ipaddr_string(&ip
->ip_dst
),
283 tcpport_string(dport
));
285 (void)printf("%s > %s: ",
286 tcpport_string(sport
), tcpport_string(dport
));
290 if (hlen
< sizeof(*tp
)) {
291 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
292 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
298 seq
= EXTRACT_32BITS(&tp
->th_seq
);
299 ack
= EXTRACT_32BITS(&tp
->th_ack
);
300 win
= EXTRACT_16BITS(&tp
->th_win
);
301 urp
= EXTRACT_16BITS(&tp
->th_urp
);
304 (void)printf("tcp %d", length
- hlen
);
306 (void)printf(" [bad hdr length %u - too long, > %u]",
312 flags
= tp
->th_flags
;
313 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
315 if (!Sflag
&& (flags
& TH_ACK
)) {
316 register struct tcp_seq_hash
*th
;
317 const void *src
, *dst
;
321 * Find (or record) the initial sequence numbers for
322 * this conversation. (we pick an arbitrary
323 * collating order so there's only one entry for
327 memset(&tha
, 0, sizeof(tha
));
334 else if (sport
== dport
) {
335 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
339 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
340 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
341 tha
.port
= dport
<< 16 | sport
;
343 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
344 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
345 tha
.port
= sport
<< 16 | dport
;
352 else if (sport
== dport
) {
353 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
357 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
358 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
359 tha
.port
= dport
<< 16 | sport
;
361 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
362 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
363 tha
.port
= sport
<< 16 | dport
;
372 else if (sport
== dport
) {
373 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
377 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
378 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
379 tha
.port
= dport
<< 16 | sport
;
381 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
382 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
383 tha
.port
= sport
<< 16 | dport
;
388 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
389 th
->nxt
; th
= th
->nxt
)
390 if (memcmp((char *)&tha
, (char *)&th
->addr
,
391 sizeof(th
->addr
)) == 0)
394 if (!th
->nxt
|| (flags
& TH_SYN
)) {
395 /* didn't find it or new conversation */
396 if (th
->nxt
== NULL
) {
397 th
->nxt
= (struct tcp_seq_hash
*)
398 calloc(1, sizeof(*th
));
400 error("tcp_print: calloc");
404 th
->ack
= seq
, th
->seq
= ack
- 1;
406 th
->seq
= seq
, th
->ack
= ack
- 1;
409 seq
-= th
->ack
, ack
-= th
->seq
;
411 seq
-= th
->seq
, ack
-= th
->ack
;
418 thseq
= thack
= threv
= 0;
421 (void)printf(" [bad hdr length %u - too long, > %u]",
426 if (IP_V(ip
) == 4 && vflag
&& !Kflag
&& !fragmented
) {
427 u_int16_t sum
, tcp_sum
;
428 if (TTEST2(tp
->th_sport
, length
)) {
429 sum
= tcp_cksum(ip
, tp
, length
);
431 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
433 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
434 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum
, sum
));
436 (void)printf(" (correct)");
440 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !Kflag
&& !fragmented
) {
441 u_int16_t sum
,tcp_sum
;
442 if (TTEST2(tp
->th_sport
, length
)) {
443 sum
= tcp6_cksum(ip6
, tp
, length
);
444 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
446 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
447 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum
, sum
));
449 (void)printf(" (correct)");
456 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
457 (void)printf(", seq %u", seq
);
460 (void)printf(":%u", seq
+ length
);
464 if (flags
& TH_ACK
) {
465 (void)printf(", ack %u", ack
);
468 (void)printf(", win %d", win
);
471 (void)printf(", urg %d", urp
);
473 * Handle any options.
475 if (hlen
> sizeof(*tp
)) {
476 register const u_char
*cp
;
477 register u_int i
, opt
, datalen
;
481 cp
= (const u_char
*)tp
+ sizeof(*tp
);
482 printf(", options [");
492 len
= *cp
++; /* total including type, len */
493 if (len
< 2 || len
> hlen
)
495 --hlen
; /* account for length byte */
497 --hlen
; /* account for type byte */
500 /* Bail if "l" bytes of data are not left or were not captured */
501 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
504 printf("%s", tok2str(tcp_option_values
, "Unknown Option %u", opt
));
511 (void)printf(" %u", EXTRACT_16BITS(cp
));
517 (void)printf(" %u", *cp
);
522 if (datalen
% 8 != 0) {
523 (void)printf("malformed sack");
527 (void)printf(" %d ", datalen
/ 8);
528 for (i
= 0; i
< datalen
; i
+= 8) {
530 s
= EXTRACT_32BITS(cp
+ i
);
532 e
= EXTRACT_32BITS(cp
+ i
+ 4);
540 (void)printf("{%u:%u}", s
, e
);
549 case TCPOPT_ECHOREPLY
:
552 * those options share their semantics.
557 (void)printf(" %u", EXTRACT_32BITS(cp
));
560 case TCPOPT_TIMESTAMP
:
563 (void)printf(" val %u ecr %u",
565 EXTRACT_32BITS(cp
+ 4));
568 case TCPOPT_SIGNATURE
:
569 datalen
= TCP_SIGLEN
;
571 #ifdef HAVE_LIBCRYPTO
572 switch (tcp_verify_signature(ip
, tp
,
573 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
575 case SIGNATURE_VALID
:
576 (void)printf("valid");
579 case SIGNATURE_INVALID
:
580 (void)printf("invalid");
583 case CANT_CHECK_SIGNATURE
:
584 (void)printf("can't check - ");
585 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
586 (void)printf("%02x", cp
[i
]);
590 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
591 (void)printf("%02x", cp
[i
]);
596 (void)printf("keyid %d", *cp
++);
598 for (i
= 0; i
< datalen
; ++i
) {
600 (void)printf("%02x", cp
[i
]);
609 * Nothing interesting.
617 uint utoval
= EXTRACT_16BITS(cp
);
618 (void)printf("0x%x", utoval
);
620 utoval
= (utoval
>> 1) * 60;
623 (void)printf(" %u", utoval
);
628 for (i
= 0; i
< datalen
; ++i
) {
630 (void)printf("%02x", cp
[i
]);
635 /* Account for data printed */
639 /* Check specification against observed length */
640 ++datalen
; /* option octet */
641 if (!ZEROLENOPT(opt
))
642 ++datalen
; /* size octet */
644 (void)printf("[len %d]", len
);
646 if (opt
== TCPOPT_EOL
)
653 * Print length field before crawling down the stack.
655 printf(", length %u", length
);
661 * Decode payload if necessary.
663 bp
+= TH_OFF(tp
) * 4;
664 if ((flags
& TH_RST
) && vflag
) {
665 print_tcp_rst_data(bp
, length
);
669 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
671 telnet_print(bp
, length
);
672 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
673 bgp_print(bp
, length
);
674 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
676 #ifdef TCPDUMP_DO_SMB
677 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
678 nbt_tcp_print(bp
, length
);
679 else if (sport
== SMB_PORT
|| dport
== SMB_PORT
)
680 smb_tcp_print(bp
, length
);
682 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
683 beep_print(bp
, length
);
684 else if (length
> 2 &&
685 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
686 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
688 * TCP DNS query has 2byte length at the head.
689 * XXX packet could be unaligned, it can go strange
691 ns_print(bp
+ 2, length
- 2, 0);
692 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
693 msdp_print(bp
, length
);
695 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
696 ldp_print(bp
, length
);
701 fputs("[bad opt]", stdout
);
706 fputs("[|tcp]", stdout
);
712 * RFC1122 says the following on data in RST segments:
714 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
716 * A TCP SHOULD allow a received RST segment to include data.
719 * It has been suggested that a RST segment could contain
720 * ASCII text that encoded and explained the cause of the
721 * RST. No standard has yet been established for such
727 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
731 if (TTEST2(*sp
, length
))
735 if (length
> MAX_RST_DATA_LEN
) {
736 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
737 putchar('+'); /* indicate we truncate */
740 while (length
-- && sp
<= snapend
) {
747 #ifdef HAVE_LIBCRYPTO
749 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
750 const u_char
*data
, int length
, const u_char
*rcvsig
)
753 u_char sig
[TCP_SIGLEN
];
756 u_int16_t savecsum
, tlen
;
763 if (data
+ length
> snapend
) {
764 printf("snaplen too short, ");
765 return (CANT_CHECK_SIGNATURE
);
770 if (sigsecret
== NULL
) {
771 printf("shared secret not supplied with -M, ");
772 return (CANT_CHECK_SIGNATURE
);
777 * Step 1: Update MD5 hash with IP pseudo-header.
780 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
781 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
782 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
783 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
784 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
786 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
788 } else if (IP_V(ip
) == 6) {
789 ip6
= (struct ip6_hdr
*)ip
;
790 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
791 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
792 len32
= htonl(ntohs(ip6
->ip6_plen
));
793 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
795 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
796 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
797 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
799 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
803 printf("IP version not 4 or 6, ");
805 printf("IP version not 4, ");
807 return (CANT_CHECK_SIGNATURE
);
811 * Step 2: Update MD5 hash with TCP header, excluding options.
812 * The TCP checksum must be set to zero.
814 savecsum
= tp1
.th_sum
;
816 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
817 tp1
.th_sum
= savecsum
;
819 * Step 3: Update MD5 hash with TCP segment data, if present.
822 MD5_Update(&ctx
, data
, length
);
824 * Step 4: Update MD5 hash with shared secret.
826 MD5_Update(&ctx
, sigsecret
, strlen(sigsecret
));
827 MD5_Final(sig
, &ctx
);
829 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
830 return (SIGNATURE_VALID
);
832 return (SIGNATURE_INVALID
);
834 #endif /* HAVE_LIBCRYPTO */
838 * c-style: whitesmith