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" },
127 { TCPOPT_UTO, "uto" },
131 static int tcp_cksum(register const struct ip *ip,
132 register const struct tcphdr *tp,
147 /* pseudo-header.. */
148 phu.ph.len = htons((u_int16_t)len);
150 phu.ph.proto = IPPROTO_TCP;
151 memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
153 memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
155 phu.ph.dst = ip_finddst(ip);
158 return in_cksum((u_short *)tp, len,
159 sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5]);
163 static int tcp6_cksum(const struct ip6_hdr *ip6, const struct tcphdr *tp,
170 struct in6_addr ph_src;
171 struct in6_addr ph_dst;
180 memset(&phu, 0, sizeof(phu));
181 phu.ph.ph_src = ip6->ip6_src;
182 phu.ph.ph_dst = ip6->ip6_dst;
183 phu.ph.ph_len = htonl(len);
184 phu.ph.ph_nxt = IPPROTO_TCP;
186 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
189 return in_cksum((u_short *)tp, len, sum);
194 tcp_print(register const u_char *bp, register u_int length,
195 register const u_char *bp2, int fragmented)
197 register const struct tcphdr *tp;
198 register const struct ip *ip;
199 register u_char flags;
202 u_int16_t sport, dport, win, urp;
203 u_int32_t seq, ack, thseq, thack;
206 register const struct ip6_hdr *ip6;
209 tp = (struct tcphdr *)bp;
210 ip = (struct ip *)bp2;
213 ip6 = (struct ip6_hdr *)bp2;
218 if (!TTEST(tp->th_dport)) {
219 (void)printf("%s > %s: [|tcp]",
220 ipaddr_string(&ip->ip_src),
221 ipaddr_string(&ip->ip_dst));
225 sport = EXTRACT_16BITS(&tp->th_sport);
226 dport = EXTRACT_16BITS(&tp->th_dport);
228 hlen = TH_OFF(tp) * 4;
231 * If data present, header length valid, and NFS port used,
233 * Pass offset of data plus 4 bytes for RPC TCP msg length
234 * to NFS print routines.
236 if (!qflag && hlen >= sizeof(*tp) && hlen <= length &&
237 (length - hlen) >= 4) {
240 register struct sunrpc_msg *rp;
241 enum sunrpc_msg_type direction;
243 fraglenp = (u_char *)tp + hlen;
244 if (TTEST2(*fraglenp, 4)) {
245 fraglen = EXTRACT_32BITS(fraglenp) & 0x7FFFFFFF;
246 if (fraglen > (length - hlen) - 4)
247 fraglen = (length - hlen) - 4;
248 rp = (struct sunrpc_msg *)(fraglenp + 4);
249 if (TTEST(rp->rm_direction)) {
250 direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction);
251 if (dport == NFS_PORT &&
252 direction == SUNRPC_CALL) {
253 nfsreq_print((u_char *)rp, fraglen,
257 if (sport == NFS_PORT &&
258 direction == SUNRPC_REPLY) {
259 nfsreply_print((u_char *)rp, fraglen,
268 if (ip6->ip6_nxt == IPPROTO_TCP) {
269 (void)printf("%s.%s > %s.%s: ",
270 ip6addr_string(&ip6->ip6_src),
271 tcpport_string(sport),
272 ip6addr_string(&ip6->ip6_dst),
273 tcpport_string(dport));
275 (void)printf("%s > %s: ",
276 tcpport_string(sport), tcpport_string(dport));
281 if (ip->ip_p == IPPROTO_TCP) {
282 (void)printf("%s.%s > %s.%s: ",
283 ipaddr_string(&ip->ip_src),
284 tcpport_string(sport),
285 ipaddr_string(&ip->ip_dst),
286 tcpport_string(dport));
288 (void)printf("%s > %s: ",
289 tcpport_string(sport), tcpport_string(dport));
293 if (hlen < sizeof(*tp)) {
294 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
295 length - hlen, hlen, (unsigned long)sizeof(*tp));
301 seq = EXTRACT_32BITS(&tp->th_seq);
302 ack = EXTRACT_32BITS(&tp->th_ack);
303 win = EXTRACT_16BITS(&tp->th_win);
304 urp = EXTRACT_16BITS(&tp->th_urp);
307 (void)printf("tcp %d", length - hlen);
309 (void)printf(" [bad hdr length %u - too long, > %u]",
315 flags = tp->th_flags;
316 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags));
318 if (!Sflag && (flags & TH_ACK)) {
319 register struct tcp_seq_hash *th;
320 const void *src, *dst;
324 * Find (or record) the initial sequence numbers for
325 * this conversation. (we pick an arbitrary
326 * collating order so there's only one entry for
330 memset(&tha, 0, sizeof(tha));
337 else if (sport == dport) {
338 if (memcmp(src, dst, sizeof ip6->ip6_dst) > 0)
342 memcpy(&tha.src, dst, sizeof ip6->ip6_dst);
343 memcpy(&tha.dst, src, sizeof ip6->ip6_src);
344 tha.port = dport << 16 | sport;
346 memcpy(&tha.dst, dst, sizeof ip6->ip6_dst);
347 memcpy(&tha.src, src, sizeof ip6->ip6_src);
348 tha.port = sport << 16 | dport;
355 else if (sport == dport) {
356 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
360 memcpy(&tha.src, dst, sizeof ip->ip_dst);
361 memcpy(&tha.dst, src, sizeof ip->ip_src);
362 tha.port = dport << 16 | sport;
364 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
365 memcpy(&tha.src, src, sizeof ip->ip_src);
366 tha.port = sport << 16 | dport;
375 else if (sport == dport) {
376 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
380 memcpy(&tha.src, dst, sizeof ip->ip_dst);
381 memcpy(&tha.dst, src, sizeof ip->ip_src);
382 tha.port = dport << 16 | sport;
384 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
385 memcpy(&tha.src, src, sizeof ip->ip_src);
386 tha.port = sport << 16 | dport;
391 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
392 th->nxt; th = th->nxt)
393 if (memcmp((char *)&tha, (char *)&th->addr,
394 sizeof(th->addr)) == 0)
397 if (!th->nxt || (flags & TH_SYN)) {
398 /* didn't find it or new conversation */
399 if (th->nxt == NULL) {
400 th->nxt = (struct tcp_seq_hash *)
401 calloc(1, sizeof(*th));
403 error("tcp_print: calloc");
407 th->ack = seq, th->seq = ack - 1;
409 th->seq = seq, th->ack = ack - 1;
412 seq -= th->ack, ack -= th->seq;
414 seq -= th->seq, ack -= th->ack;
421 thseq = thack = threv = 0;
424 (void)printf(" [bad hdr length %u - too long, > %u]",
429 if (IP_V(ip) == 4 && vflag && !Kflag && !fragmented) {
430 u_int16_t sum, tcp_sum;
431 if (TTEST2(tp->th_sport, length)) {
432 sum = tcp_cksum(ip, tp, length);
434 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
436 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
437 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum, sum));
439 (void)printf(" (correct)");
443 if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !Kflag && !fragmented) {
444 u_int16_t sum,tcp_sum;
445 if (TTEST2(tp->th_sport, length)) {
446 sum = tcp6_cksum(ip6, tp, length);
447 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
449 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
450 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum, sum));
452 (void)printf(" (correct)");
459 if (vflag > 1 || flags & (TH_SYN | TH_FIN | TH_RST)) {
460 (void)printf(", seq %u", seq);
463 (void)printf(":%u", seq + length);
467 if (flags & TH_ACK) {
468 (void)printf(", ack %u", ack);
471 (void)printf(", win %d", win);
474 (void)printf(", urg %d", urp);
476 * Handle any options.
478 if (hlen > sizeof(*tp)) {
479 register const u_char *cp;
480 register u_int i, opt, datalen;
484 cp = (const u_char *)tp + sizeof(*tp);
485 printf(", options [");
495 len = *cp++; /* total including type, len */
496 if (len < 2 || len > hlen)
498 --hlen; /* account for length byte */
500 --hlen; /* account for type byte */
503 /* Bail if "l" bytes of data are not left or were not captured */
504 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
507 printf("%s", tok2str(tcp_option_values, "Unknown Option %u", opt));
514 (void)printf(" %u", EXTRACT_16BITS(cp));
520 (void)printf(" %u", *cp);
525 if (datalen % 8 != 0) {
526 (void)printf("malformed sack");
530 (void)printf(" %d ", datalen / 8);
531 for (i = 0; i < datalen; i += 8) {
533 s = EXTRACT_32BITS(cp + i);
535 e = EXTRACT_32BITS(cp + i + 4);
543 (void)printf("{%u:%u}", s, e);
552 case TCPOPT_ECHOREPLY:
555 * those options share their semantics.
560 (void)printf(" %u", EXTRACT_32BITS(cp));
563 case TCPOPT_TIMESTAMP:
566 (void)printf(" val %u ecr %u",
568 EXTRACT_32BITS(cp + 4));
571 case TCPOPT_SIGNATURE:
572 datalen = TCP_SIGLEN;
574 #ifdef HAVE_LIBCRYPTO
575 switch (tcp_verify_signature(ip, tp,
576 bp + TH_OFF(tp) * 4, length, cp)) {
578 case SIGNATURE_VALID:
579 (void)printf("valid");
582 case SIGNATURE_INVALID:
583 (void)printf("invalid");
586 case CANT_CHECK_SIGNATURE:
587 (void)printf("can't check - ");
588 for (i = 0; i < TCP_SIGLEN; ++i)
589 (void)printf("%02x", cp[i]);
593 for (i = 0; i < TCP_SIGLEN; ++i)
594 (void)printf("%02x", cp[i]);
599 (void)printf("keyid %d", *cp++);
601 for (i = 0; i < datalen; ++i) {
603 (void)printf("%02x", cp[i]);
612 * Nothing interesting.
620 uint utoval = EXTRACT_16BITS(cp);
622 utoval = (utoval >> 1) * 60;
625 (void)printf(" %u", utoval);
630 for (i = 0; i < datalen; ++i) {
632 (void)printf("%02x", cp[i]);
637 /* Account for data printed */
641 /* Check specification against observed length */
642 ++datalen; /* option octet */
643 if (!ZEROLENOPT(opt))
644 ++datalen; /* size octet */
646 (void)printf("[len %d]", len);
648 if (opt == TCPOPT_EOL)
655 * Print length field before crawling down the stack.
657 printf(", length %u", length);
663 * Decode payload if necessary.
665 bp += TH_OFF(tp) * 4;
666 if ((flags & TH_RST) && vflag) {
667 print_tcp_rst_data(bp, length);
671 if (sport == TELNET_PORT || dport == TELNET_PORT) {
673 telnet_print(bp, length);
674 } else if (sport == BGP_PORT || dport == BGP_PORT)
675 bgp_print(bp, length);
676 else if (sport == PPTP_PORT || dport == PPTP_PORT)
678 #ifdef TCPDUMP_DO_SMB
679 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
680 nbt_tcp_print(bp, length);
681 else if (sport == SMB_PORT || dport == SMB_PORT)
682 smb_tcp_print(bp, length);
684 else if (sport == BEEP_PORT || dport == BEEP_PORT)
685 beep_print(bp, length);
686 else if (length > 2 &&
687 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT ||
688 sport == MULTICASTDNS_PORT || dport == MULTICASTDNS_PORT)) {
690 * TCP DNS query has 2byte length at the head.
691 * XXX packet could be unaligned, it can go strange
693 ns_print(bp + 2, length - 2, 0);
694 } else if (sport == MSDP_PORT || dport == MSDP_PORT) {
695 msdp_print(bp, length);
697 else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) {
698 ldp_print(bp, length);
703 fputs("[bad opt]", stdout);
708 fputs("[|tcp]", stdout);
714 * RFC1122 says the following on data in RST segments:
716 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
718 * A TCP SHOULD allow a received RST segment to include data.
721 * It has been suggested that a RST segment could contain
722 * ASCII text that encoded and explained the cause of the
723 * RST. No standard has yet been established for such
729 print_tcp_rst_data(register const u_char *sp, u_int length)
733 if (TTEST2(*sp, length))
737 if (length > MAX_RST_DATA_LEN) {
738 length = MAX_RST_DATA_LEN; /* can use -X for longer */
739 putchar('+'); /* indicate we truncate */
742 while (length-- && sp <= snapend) {
749 #ifdef HAVE_LIBCRYPTO
751 tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
752 const u_char *data, int length, const u_char *rcvsig)
755 u_char sig[TCP_SIGLEN];
758 u_int16_t savecsum, tlen;
767 if (tcpmd5secret == NULL)
768 return (CANT_CHECK_SIGNATURE);
772 * Step 1: Update MD5 hash with IP pseudo-header.
775 MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src));
776 MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst));
777 MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto));
778 MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p));
779 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
781 MD5_Update(&ctx, (char *)&tlen, sizeof(tlen));
783 } else if (IP_V(ip) == 6) {
784 ip6 = (struct ip6_hdr *)ip;
785 MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
786 MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
787 len32 = htonl(ntohs(ip6->ip6_plen));
788 MD5_Update(&ctx, (char *)&len32, sizeof(len32));
790 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
791 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
792 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
794 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
797 return (CANT_CHECK_SIGNATURE);
800 * Step 2: Update MD5 hash with TCP header, excluding options.
801 * The TCP checksum must be set to zero.
803 savecsum = tp1.th_sum;
805 MD5_Update(&ctx, (char *)&tp1, sizeof(struct tcphdr));
806 tp1.th_sum = savecsum;
808 * Step 3: Update MD5 hash with TCP segment data, if present.
811 MD5_Update(&ctx, data, length);
813 * Step 4: Update MD5 hash with shared secret.
815 MD5_Update(&ctx, tcpmd5secret, strlen(tcpmd5secret));
816 MD5_Final(sig, &ctx);
818 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
819 return (SIGNATURE_VALID);
821 return (SIGNATURE_INVALID);
823 #endif /* HAVE_LIBCRYPTO */
827 * c-style: whitesmith