1 /* $NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner 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.
26 #include <sys/cdefs.h>
29 static const char rcsid
[] _U_
=
30 "@(#) Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.120.2.3 2005/10/16 06:05:46 guy Exp (LBL)";
32 __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
40 #include <tcpdump-stdinc.h>
46 #include "interface.h"
47 #include "addrtoname.h"
63 #include <openssl/md5.h>
65 #define SIGNATURE_VALID 0
66 #define SIGNATURE_INVALID 1
67 #define CANT_CHECK_SIGNATURE 2
69 static int tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
70 const u_char
*data
, int length
, const u_char
*rcvsig
);
73 static void print_tcp_rst_data(register const u_char
*sp
, u_int length
);
75 #define MAX_RST_DATA_LEN 30
90 struct tcp_seq_hash
*nxt
;
96 #define TSEQ_HASHSIZE 919
98 /* These tcp optinos do not have the size octet */
99 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
101 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
105 #define TELNET_PORT 23
110 #define NETBIOS_SSN_PORT 139
112 #define PPTP_PORT 1723
114 #define BEEP_PORT 10288
116 #define NFS_PORT 2049
118 #define MSDP_PORT 639
121 static int tcp_cksum(register const struct ip
*ip
,
122 register const struct tcphdr
*tp
,
137 /* pseudo-header.. */
138 phu
.ph
.len
= htons((u_int16_t
)len
);
140 phu
.ph
.proto
= IPPROTO_TCP
;
141 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
143 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
145 phu
.ph
.dst
= ip_finddst(ip
);
148 return in_cksum((u_short
*)tp
, len
,
149 sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
153 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
157 register const u_int16_t
*sp
;
161 struct in6_addr ph_src
;
162 struct in6_addr ph_dst
;
171 memset(&phu
, 0, sizeof(phu
));
172 phu
.ph
.ph_src
= ip6
->ip6_src
;
173 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
174 phu
.ph
.ph_len
= htonl(len
);
175 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
178 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
181 sp
= (const u_int16_t
*)tp
;
183 for (i
= 0; i
< (len
& ~1); i
+= 2)
187 sum
+= htons((*(const u_int8_t
*)sp
) << 8);
190 sum
= (sum
& 0xffff) + (sum
>> 16);
198 tcp_print(register const u_char
*bp
, register u_int length
,
199 register const u_char
*bp2
, int fragmented
)
201 register const struct tcphdr
*tp
;
202 register const struct ip
*ip
;
203 register u_char flags
;
206 u_int16_t sport
, dport
, win
, urp
;
207 u_int32_t seq
, ack
, thseq
, thack
;
210 register const struct ip6_hdr
*ip6
;
213 tp
= (struct tcphdr
*)bp
;
214 ip
= (struct ip
*)bp2
;
217 ip6
= (struct ip6_hdr
*)bp2
;
222 if (!TTEST(tp
->th_dport
)) {
223 (void)printf("%s > %s: [|tcp]",
224 ipaddr_string(&ip
->ip_src
),
225 ipaddr_string(&ip
->ip_dst
));
229 sport
= EXTRACT_16BITS(&tp
->th_sport
);
230 dport
= EXTRACT_16BITS(&tp
->th_dport
);
232 hlen
= TH_OFF(tp
) * 4;
235 * If data present, header length valid, and NFS port used,
237 * Pass offset of data plus 4 bytes for RPC TCP msg length
238 * to NFS print routines.
240 if (!qflag
&& hlen
>= sizeof(*tp
) && hlen
<= length
) {
241 if ((u_char
*)tp
+ 4 + sizeof(struct sunrpc_msg
) <= snapend
&&
243 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
246 } else if ((u_char
*)tp
+ 4 + sizeof(struct sunrpc_msg
)
249 nfsreply_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
256 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
257 (void)printf("%s.%s > %s.%s: ",
258 ip6addr_string(&ip6
->ip6_src
),
259 tcpport_string(sport
),
260 ip6addr_string(&ip6
->ip6_dst
),
261 tcpport_string(dport
));
263 (void)printf("%s > %s: ",
264 tcpport_string(sport
), tcpport_string(dport
));
269 if (ip
->ip_p
== IPPROTO_TCP
) {
270 (void)printf("%s.%s > %s.%s: ",
271 ipaddr_string(&ip
->ip_src
),
272 tcpport_string(sport
),
273 ipaddr_string(&ip
->ip_dst
),
274 tcpport_string(dport
));
276 (void)printf("%s > %s: ",
277 tcpport_string(sport
), tcpport_string(dport
));
281 if (hlen
< sizeof(*tp
)) {
282 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
283 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
289 seq
= EXTRACT_32BITS(&tp
->th_seq
);
290 ack
= EXTRACT_32BITS(&tp
->th_ack
);
291 win
= EXTRACT_16BITS(&tp
->th_win
);
292 urp
= EXTRACT_16BITS(&tp
->th_urp
);
295 (void)printf("tcp %d", length
- hlen
);
297 (void)printf(" [bad hdr length %u - too long, > %u]",
302 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
303 TH_ECNECHO
|TH_CWR
)) {
313 putchar('W'); /* congestion _W_indow reduced (ECN) */
314 if (flags
& TH_ECNECHO
)
315 putchar('E'); /* ecn _E_cho sent (ECN) */
319 if (!Sflag
&& (flags
& TH_ACK
)) {
320 register struct tcp_seq_hash
*th
;
321 const void *src
, *dst
;
325 * Find (or record) the initial sequence numbers for
326 * this conversation. (we pick an arbitrary
327 * collating order so there's only one entry for
331 memset(&tha
, 0, sizeof(tha
));
338 else if (sport
== dport
) {
339 if (memcmp(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
343 memcpy(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
344 memcpy(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
345 tha
.port
= dport
<< 16 | sport
;
347 memcpy(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
348 memcpy(&tha
.src
, src
, sizeof ip6
->ip6_src
);
349 tha
.port
= sport
<< 16 | dport
;
356 else if (sport
== dport
) {
357 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
361 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
362 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
363 tha
.port
= dport
<< 16 | sport
;
365 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
366 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
367 tha
.port
= sport
<< 16 | dport
;
376 else if (sport
== dport
) {
377 if (memcmp(src
, dst
, sizeof ip
->ip_dst
) > 0)
381 memcpy(&tha
.src
, dst
, sizeof ip
->ip_dst
);
382 memcpy(&tha
.dst
, src
, sizeof ip
->ip_src
);
383 tha
.port
= dport
<< 16 | sport
;
385 memcpy(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
386 memcpy(&tha
.src
, src
, sizeof ip
->ip_src
);
387 tha
.port
= sport
<< 16 | dport
;
392 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
393 th
->nxt
; th
= th
->nxt
)
394 if (memcmp((char *)&tha
, (char *)&th
->addr
,
395 sizeof(th
->addr
)) == 0)
398 if (!th
->nxt
|| (flags
& TH_SYN
)) {
399 /* didn't find it or new conversation */
400 if (th
->nxt
== NULL
) {
401 th
->nxt
= (struct tcp_seq_hash
*)
402 calloc(1, sizeof(*th
));
404 error("tcp_print: calloc");
408 th
->ack
= seq
, th
->seq
= ack
- 1;
410 th
->seq
= seq
, th
->ack
= ack
- 1;
413 seq
-= th
->ack
, ack
-= th
->seq
;
415 seq
-= th
->seq
, ack
-= th
->ack
;
422 thseq
= thack
= threv
= 0;
425 (void)printf(" [bad hdr length %u - too long, > %u]",
430 if (IP_V(ip
) == 4 && vflag
&& !fragmented
) {
431 u_int16_t sum
, tcp_sum
;
432 if (TTEST2(tp
->th_sport
, length
)) {
433 sum
= tcp_cksum(ip
, tp
, length
);
435 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
437 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
438 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum
, sum
));
440 (void)printf(" (correct),");
444 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !fragmented
) {
445 u_int16_t sum
,tcp_sum
;
446 if (TTEST2(tp
->th_sport
, length
)) {
447 sum
= tcp6_cksum(ip6
, tp
, length
);
448 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp
->th_sum
));
450 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
451 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum
, sum
));
453 (void)printf(" (correct),");
460 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
461 (void)printf(" %u:%u(%u)", seq
, seq
+ length
, length
);
463 (void)printf(" ack %u", ack
);
465 (void)printf(" win %d", win
);
468 (void)printf(" urg %d", urp
);
470 * Handle any options.
472 if (hlen
> sizeof(*tp
)) {
473 register const u_char
*cp
;
474 register u_int i
, opt
, datalen
;
478 cp
= (const u_char
*)tp
+ sizeof(*tp
);
489 len
= *cp
++; /* total including type, len */
490 if (len
< 2 || len
> hlen
)
492 --hlen
; /* account for length byte */
494 --hlen
; /* account for type byte */
497 /* Bail if "l" bytes of data are not left or were not captured */
498 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
506 (void)printf(" %u", EXTRACT_16BITS(cp
));
519 (void)printf("wscale");
522 (void)printf(" %u", *cp
);
526 (void)printf("sackOK");
531 if (datalen
% 8 != 0) {
532 (void)printf("malformed sack");
536 (void)printf("sack %d ", datalen
/ 8);
537 for (i
= 0; i
< datalen
; i
+= 8) {
539 s
= EXTRACT_32BITS(cp
+ i
);
541 e
= EXTRACT_32BITS(cp
+ i
+ 4);
549 (void)printf("{%u:%u}", s
, e
);
555 (void)printf("echo");
558 (void)printf(" %u", EXTRACT_32BITS(cp
));
561 case TCPOPT_ECHOREPLY
:
562 (void)printf("echoreply");
565 (void)printf(" %u", EXTRACT_32BITS(cp
));
568 case TCPOPT_TIMESTAMP
:
569 (void)printf("timestamp");
572 (void)printf(" %u", EXTRACT_32BITS(cp
));
574 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
581 (void)printf(" %u", EXTRACT_32BITS(cp
));
585 (void)printf("ccnew");
588 (void)printf(" %u", EXTRACT_32BITS(cp
));
592 (void)printf("ccecho");
595 (void)printf(" %u", EXTRACT_32BITS(cp
));
598 case TCPOPT_SIGNATURE
:
599 (void)printf("md5:");
600 datalen
= TCP_SIGLEN
;
602 #ifdef HAVE_LIBCRYPTO
603 switch (tcp_verify_signature(ip
, tp
,
604 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
606 case SIGNATURE_VALID
:
607 (void)printf("valid");
610 case SIGNATURE_INVALID
:
611 (void)printf("invalid");
614 case CANT_CHECK_SIGNATURE
:
615 (void)printf("can't check - ");
616 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
617 (void)printf("%02x", cp
[i
]);
621 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
622 (void)printf("%02x", cp
[i
]);
627 (void)printf("opt-%u:", opt
);
629 for (i
= 0; i
< datalen
; ++i
) {
631 (void)printf("%02x", cp
[i
]);
636 /* Account for data printed */
640 /* Check specification against observed length */
641 ++datalen
; /* option octet */
642 if (!ZEROLENOPT(opt
))
643 ++datalen
; /* size octet */
645 (void)printf("[len %d]", len
);
647 if (opt
== TCPOPT_EOL
)
657 * Decode payload if necessary.
659 bp
+= TH_OFF(tp
) * 4;
660 if (flags
& TH_RST
) {
662 print_tcp_rst_data(bp
, length
);
664 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
666 telnet_print(bp
, length
);
667 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
668 bgp_print(bp
, length
);
669 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
671 #ifdef TCPDUMP_DO_SMB
672 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
673 nbt_tcp_print(bp
, length
);
675 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
676 beep_print(bp
, length
);
677 else if (length
> 2 &&
678 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
679 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
681 * TCP DNS query has 2byte length at the head.
682 * XXX packet could be unaligned, it can go strange
684 ns_print(bp
+ 2, length
- 2, 0);
685 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
686 msdp_print(bp
, length
);
688 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
689 ldp_print(bp
, length
);
694 fputs("[bad opt]", stdout
);
699 fputs("[|tcp]", stdout
);
705 * RFC1122 says the following on data in RST segments:
707 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
709 * A TCP SHOULD allow a received RST segment to include data.
712 * It has been suggested that a RST segment could contain
713 * ASCII text that encoded and explained the cause of the
714 * RST. No standard has yet been established for such
720 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
724 if (TTEST2(*sp
, length
))
728 if (length
> MAX_RST_DATA_LEN
) {
729 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
730 putchar('+'); /* indicate we truncate */
733 while (length
-- && sp
<= snapend
) {
740 #ifdef HAVE_LIBCRYPTO
742 tcp_verify_signature(const struct ip
*ip
, const struct tcphdr
*tp
,
743 const u_char
*data
, int length
, const u_char
*rcvsig
)
746 u_char sig
[TCP_SIGLEN
];
749 u_int16_t savecsum
, tlen
;
758 if (tcpmd5secret
== NULL
)
759 return (CANT_CHECK_SIGNATURE
);
763 * Step 1: Update MD5 hash with IP pseudo-header.
766 MD5_Update(&ctx
, (char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
767 MD5_Update(&ctx
, (char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
768 MD5_Update(&ctx
, (char *)&zero_proto
, sizeof(zero_proto
));
769 MD5_Update(&ctx
, (char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
770 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
772 MD5_Update(&ctx
, (char *)&tlen
, sizeof(tlen
));
774 } else if (IP_V(ip
) == 6) {
775 ip6
= (struct ip6_hdr
*)ip
;
776 MD5_Update(&ctx
, (char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
777 MD5_Update(&ctx
, (char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
778 len32
= htonl(ntohs(ip6
->ip6_plen
));
779 MD5_Update(&ctx
, (char *)&len32
, sizeof(len32
));
781 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
782 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
783 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
785 MD5_Update(&ctx
, (char *)&nxt
, sizeof(nxt
));
788 return (CANT_CHECK_SIGNATURE
);
791 * Step 2: Update MD5 hash with TCP header, excluding options.
792 * The TCP checksum must be set to zero.
794 savecsum
= tp1
.th_sum
;
796 MD5_Update(&ctx
, (char *)&tp1
, sizeof(struct tcphdr
));
797 tp1
.th_sum
= savecsum
;
799 * Step 3: Update MD5 hash with TCP segment data, if present.
802 MD5_Update(&ctx
, data
, length
);
804 * Step 4: Update MD5 hash with shared secret.
806 MD5_Update(&ctx
, tcpmd5secret
, strlen(tcpmd5secret
));
807 MD5_Final(sig
, &ctx
);
809 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
810 return (SIGNATURE_VALID
);
812 return (SIGNATURE_INVALID
);
814 #endif /* HAVE_LIBCRYPTO */