2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.81 2000/12/23 20:55:22 guy Exp $ (LBL)";
31 #include <sys/param.h>
36 #include <netinet/in.h>
44 #include "interface.h"
45 #include "addrtoname.h"
55 static void print_tcp_rst_data(register const u_char *sp, u_int length);
57 #define MAX_RST_DATA_LEN 30
61 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
64 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
67 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
70 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
72 #ifndef TCPOPT_ECHOREPLY
73 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
75 #ifndef TCPOPT_TIMESTAMP
76 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
79 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
82 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
85 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
89 * Definitions required for ECN
90 * for use if the OS running tcpdump does not have ECN
93 #define TH_ECNECHO 0x40 /* ECN Echo in tcp header */
96 #define TH_CWR 0x80 /* ECN Cwnd Reduced in tcp header*/
110 struct tcp_seq_hash {
111 struct tcp_seq_hash *nxt;
117 #define TSEQ_HASHSIZE 919
119 /* These tcp optinos do not have the size octet */
120 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
122 static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
126 #define TELNET_PORT 23
131 #define NETBIOS_SSN_PORT 139
132 #define BXXP_PORT 10288
134 #define NFS_PORT 2049
137 static int tcp_cksum(register const struct ip *ip,
138 register const struct tcphdr *tp,
152 register const u_int16_t *sp;
154 tlen = ntohs(ip->ip_len) - ((const char *)tp-(const char*)ip);
156 /* pseudo-header.. */
157 phu.ph.len = htons(tlen);
159 phu.ph.proto = IPPROTO_TCP;
160 memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
161 memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
164 sum = sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5];
166 sp = (const u_int16_t *)tp;
168 for (i=0; i<(tlen&~1); i+= 2)
172 sum += htons( (*(const u_int8_t *)sp) << 8);
176 sum = (sum & 0xffff) + (sum >> 16);
183 static int tcp6_cksum(const struct ip6_hdr *ip6, const struct tcphdr *tp,
187 register const u_int16_t *sp;
191 struct in6_addr ph_src;
192 struct in6_addr ph_dst;
200 tlen = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr) -
201 ((const char *)tp - (const char*)ip6);
204 memset(&phu, 0, sizeof(phu));
205 phu.ph.ph_src = ip6->ip6_src;
206 phu.ph.ph_dst = ip6->ip6_dst;
207 phu.ph.ph_len = htonl(tlen);
208 phu.ph.ph_nxt = IPPROTO_TCP;
211 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
214 sp = (const u_int16_t *)tp;
216 for (i = 0; i < (tlen & ~1); i += 2)
220 sum += htons((*(const u_int8_t *)sp) << 8);
223 sum = (sum & 0xffff) + (sum >> 16);
231 tcp_print(register const u_char *bp, register u_int length,
232 register const u_char *bp2, int fragmented)
234 register const struct tcphdr *tp;
235 register const struct ip *ip;
236 register u_char flags;
239 u_int16_t sport, dport, win, urp;
240 u_int32_t seq, ack, thseq, thack;
243 register const struct ip6_hdr *ip6;
246 tp = (struct tcphdr *)bp;
247 ip = (struct ip *)bp2;
250 ip6 = (struct ip6_hdr *)bp2;
255 if (!TTEST(tp->th_dport)) {
256 (void)printf("%s > %s: [|tcp]",
257 ipaddr_string(&ip->ip_src),
258 ipaddr_string(&ip->ip_dst));
262 sport = ntohs(tp->th_sport);
263 dport = ntohs(tp->th_dport);
266 hlen = TH_OFF(tp) * 4;
269 * If data present and NFS port used, assume NFS.
270 * Pass offset of data plus 4 bytes for RPC TCP msg length
271 * to NFS print routines.
274 if ((u_char *)tp + 4 + sizeof(struct rpc_msg) <= snapend &&
276 nfsreq_print((u_char *)tp + hlen + 4, length-hlen,
279 } else if ((u_char *)tp + 4 + sizeof(struct rpc_msg)
282 nfsreply_print((u_char *)tp + hlen + 4,length-hlen,
289 if (ip6->ip6_nxt == IPPROTO_TCP) {
290 (void)printf("%s.%s > %s.%s: ",
291 ip6addr_string(&ip6->ip6_src),
292 tcpport_string(sport),
293 ip6addr_string(&ip6->ip6_dst),
294 tcpport_string(dport));
296 (void)printf("%s > %s: ",
297 tcpport_string(sport), tcpport_string(dport));
302 if (ip->ip_p == IPPROTO_TCP) {
303 (void)printf("%s.%s > %s.%s: ",
304 ipaddr_string(&ip->ip_src),
305 tcpport_string(sport),
306 ipaddr_string(&ip->ip_dst),
307 tcpport_string(dport));
309 (void)printf("%s > %s: ",
310 tcpport_string(sport), tcpport_string(dport));
316 seq = (u_int32_t)ntohl(tp->th_seq);
317 ack = (u_int32_t)ntohl(tp->th_ack);
318 win = ntohs(tp->th_win);
319 urp = ntohs(tp->th_urp);
322 (void)printf("tcp %d", length - TH_OFF(tp) * 4);
325 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|
326 TH_ECNECHO|TH_CWR)) {
336 putchar('W'); /* congestion _W_indow reduced (ECN) */
337 if (flags & TH_ECNECHO)
338 putchar('E'); /* ecn _E_cho sent (ECN) */
342 if (!Sflag && (flags & TH_ACK)) {
343 register struct tcp_seq_hash *th;
347 * Find (or record) the initial sequence numbers for
348 * this conversation. (we pick an arbitrary
349 * collating order so there's only one entry for
353 memset(&tha, 0, sizeof(tha));
358 } else if (sport == dport) {
361 for (i = 0; i < 4; i++) {
362 if (((u_int32_t *)(&ip6->ip6_src))[i] >
363 ((u_int32_t *)(&ip6->ip6_dst))[i]) {
370 tha.src = ip6->ip6_dst;
371 tha.dst = ip6->ip6_src;
372 tha.port = dport << 16 | sport;
374 tha.dst = ip6->ip6_dst;
375 tha.src = ip6->ip6_src;
376 tha.port = sport << 16 | dport;
381 ip->ip_src.s_addr > ip->ip_dst.s_addr)) {
385 *(struct in_addr *)&tha.src = ip->ip_dst;
386 *(struct in_addr *)&tha.dst = ip->ip_src;
387 tha.port = dport << 16 | sport;
389 *(struct in_addr *)&tha.dst = ip->ip_dst;
390 *(struct in_addr *)&tha.src = ip->ip_src;
391 tha.port = sport << 16 | dport;
397 ip->ip_src.s_addr < ip->ip_dst.s_addr)) {
398 tha.src = ip->ip_src, tha.dst = ip->ip_dst;
399 tha.port = sport << 16 | dport;
402 tha.src = ip->ip_dst, tha.dst = ip->ip_src;
403 tha.port = dport << 16 | sport;
409 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
410 th->nxt; th = th->nxt)
411 if (!memcmp((char *)&tha, (char *)&th->addr,
415 if (!th->nxt || (flags & TH_SYN)) {
416 /* didn't find it or new conversation */
417 if (th->nxt == NULL) {
418 th->nxt = (struct tcp_seq_hash *)
419 calloc(1, sizeof(*th));
421 error("tcp_print: calloc");
425 th->ack = seq, th->seq = ack - 1;
427 th->seq = seq, th->ack = ack - 1;
431 seq -= th->ack, ack -= th->seq;
433 seq -= th->seq, ack -= th->ack;
440 thseq = thack = threv = 0;
443 (void)printf(" [bad hdr length]");
447 if (IP_V(ip) == 4 && vflag && !fragmented) {
449 if (TTEST2(tp->th_sport, length)) {
450 sum = tcp_cksum(ip, tp, length);
452 (void)printf(" [bad tcp cksum %x!]", sum);
454 (void)printf(" [tcp sum ok]");
458 if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !fragmented) {
460 if (TTEST2(tp->th_sport, length)) {
461 sum = tcp6_cksum(ip6, tp, length);
463 (void)printf(" [bad tcp cksum %x!]", sum);
465 (void)printf(" [tcp sum ok]");
471 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
472 (void)printf(" %u:%u(%d)", seq, seq + length, length);
474 (void)printf(" ack %u", ack);
476 (void)printf(" win %d", win);
479 (void)printf(" urg %d", urp);
481 * Handle any options.
483 if ((hlen -= sizeof(*tp)) > 0) {
484 register const u_char *cp;
485 register int i, opt, len, datalen;
487 cp = (const u_char *)tp + sizeof(*tp);
498 len = *cp++; /* total including type, len */
499 if (len < 2 || len > hlen)
501 --hlen; /* account for length byte */
503 --hlen; /* account for type byte */
506 /* Bail if "l" bytes of data are not left or were not captured */
507 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
515 (void)printf(" %u", EXTRACT_16BITS(cp));
528 (void)printf("wscale");
531 (void)printf(" %u", *cp);
535 (void)printf("sackOK");
539 (void)printf("sack");
541 if (datalen % 8 != 0) {
542 (void)printf(" malformed sack ");
546 (void)printf(" sack %d ", datalen / 8);
547 for (i = 0; i < datalen; i += 8) {
549 s = EXTRACT_32BITS(cp + i);
551 e = EXTRACT_32BITS(cp + i + 4);
559 (void)printf("{%u:%u}", s, e);
566 (void)printf("echo");
569 (void)printf(" %u", EXTRACT_32BITS(cp));
572 case TCPOPT_ECHOREPLY:
573 (void)printf("echoreply");
576 (void)printf(" %u", EXTRACT_32BITS(cp));
579 case TCPOPT_TIMESTAMP:
580 (void)printf("timestamp");
583 (void)printf(" %u", EXTRACT_32BITS(cp));
585 (void)printf(" %u", EXTRACT_32BITS(cp + 4));
592 (void)printf(" %u", EXTRACT_32BITS(cp));
596 (void)printf("ccnew");
599 (void)printf(" %u", EXTRACT_32BITS(cp));
603 (void)printf("ccecho");
606 (void)printf(" %u", EXTRACT_32BITS(cp));
610 (void)printf("opt-%d:", opt);
612 for (i = 0; i < datalen; ++i) {
614 (void)printf("%02x", cp[i]);
619 /* Account for data printed */
623 /* Check specification against observed length */
624 ++datalen; /* option octet */
625 if (!ZEROLENOPT(opt))
626 ++datalen; /* size octet */
628 (void)printf("[len %d]", len);
630 if (opt == TCPOPT_EOL)
640 * Decode payload if necessary.
642 bp += TH_OFF(tp) * 4;
643 if (flags & TH_RST) {
645 print_tcp_rst_data(bp, length);
647 if (sport == TELNET_PORT || dport == TELNET_PORT) {
649 telnet_print(bp, length);
650 } else if (sport == BGP_PORT || dport == BGP_PORT)
651 bgp_print(bp, length);
652 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
653 nbt_tcp_print(bp, length);
654 else if (sport == BXXP_PORT || dport == BXXP_PORT)
655 bxxp_print(bp, length);
659 fputs("[bad opt]", stdout);
664 fputs("[|tcp]", stdout);
670 * RFC1122 says the following on data in RST segments:
672 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
674 * A TCP SHOULD allow a received RST segment to include data.
677 * It has been suggested that a RST segment could contain
678 * ASCII text that encoded and explained the cause of the
679 * RST. No standard has yet been established for such
685 print_tcp_rst_data(register const u_char *sp, u_int length)
689 if (TTEST2(*sp, length))
693 if (length > MAX_RST_DATA_LEN) {
694 length = MAX_RST_DATA_LEN; /* can use -X for longer */
695 putchar('+'); /* indicate we truncate */
698 while (length-- && sp <= snapend) {