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.95 2001/12/10 08:21:24 guy Exp $ (LBL)";
31 #include <sys/param.h>
36 #include <netinet/in.h>
44 #include "interface.h"
45 #include "addrtoname.h"
57 static void print_tcp_rst_data(register const u_char *sp, u_int length);
59 #define MAX_RST_DATA_LEN 30
74 struct tcp_seq_hash *nxt;
80 #define TSEQ_HASHSIZE 919
82 /* These tcp optinos do not have the size octet */
83 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
85 static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
89 #define TELNET_PORT 23
94 #define NETBIOS_SSN_PORT 139
96 #define PPTP_PORT 1723
98 #define BEEP_PORT 10288
100 #define NFS_PORT 2049
102 #define MSDP_PORT 639
104 static int tcp_cksum(register const struct ip *ip,
105 register const struct tcphdr *tp,
120 /* pseudo-header.. */
121 phu.ph.len = htons(len); /* XXX */
123 phu.ph.proto = IPPROTO_TCP;
124 memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
125 memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
128 return in_cksum((u_short *)tp, len,
129 sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5]);
133 static int tcp6_cksum(const struct ip6_hdr *ip6, const struct tcphdr *tp,
137 register const u_int16_t *sp;
141 struct in6_addr ph_src;
142 struct in6_addr ph_dst;
150 tlen = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr) -
151 ((const char *)tp - (const char*)ip6);
154 memset(&phu, 0, sizeof(phu));
155 phu.ph.ph_src = ip6->ip6_src;
156 phu.ph.ph_dst = ip6->ip6_dst;
157 phu.ph.ph_len = htonl(tlen);
158 phu.ph.ph_nxt = IPPROTO_TCP;
161 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
164 sp = (const u_int16_t *)tp;
166 for (i = 0; i < (tlen & ~1); i += 2)
170 sum += htons((*(const u_int8_t *)sp) << 8);
173 sum = (sum & 0xffff) + (sum >> 16);
181 tcp_print(register const u_char *bp, register u_int length,
182 register const u_char *bp2, int fragmented)
184 register const struct tcphdr *tp;
185 register const struct ip *ip;
186 register u_char flags;
189 u_int16_t sport, dport, win, urp;
190 u_int32_t seq, ack, thseq, thack;
193 register const struct ip6_hdr *ip6;
196 tp = (struct tcphdr *)bp;
197 ip = (struct ip *)bp2;
200 ip6 = (struct ip6_hdr *)bp2;
205 if (!TTEST(tp->th_dport)) {
206 (void)printf("%s > %s: [|tcp]",
207 ipaddr_string(&ip->ip_src),
208 ipaddr_string(&ip->ip_dst));
212 sport = ntohs(tp->th_sport);
213 dport = ntohs(tp->th_dport);
215 hlen = TH_OFF(tp) * 4;
218 * If data present and NFS port used, assume NFS.
219 * Pass offset of data plus 4 bytes for RPC TCP msg length
220 * to NFS print routines.
223 if ((u_char *)tp + 4 + sizeof(struct rpc_msg) <= snapend &&
225 nfsreq_print((u_char *)tp + hlen + 4, length - hlen,
228 } else if ((u_char *)tp + 4 + sizeof(struct rpc_msg)
231 nfsreply_print((u_char *)tp + hlen + 4, length - hlen,
238 if (ip6->ip6_nxt == IPPROTO_TCP) {
239 (void)printf("%s.%s > %s.%s: ",
240 ip6addr_string(&ip6->ip6_src),
241 tcpport_string(sport),
242 ip6addr_string(&ip6->ip6_dst),
243 tcpport_string(dport));
245 (void)printf("%s > %s: ",
246 tcpport_string(sport), tcpport_string(dport));
251 if (ip->ip_p == IPPROTO_TCP) {
252 (void)printf("%s.%s > %s.%s: ",
253 ipaddr_string(&ip->ip_src),
254 tcpport_string(sport),
255 ipaddr_string(&ip->ip_dst),
256 tcpport_string(dport));
258 (void)printf("%s > %s: ",
259 tcpport_string(sport), tcpport_string(dport));
265 seq = (u_int32_t)ntohl(tp->th_seq);
266 ack = (u_int32_t)ntohl(tp->th_ack);
267 win = ntohs(tp->th_win);
268 urp = ntohs(tp->th_urp);
271 (void)printf("tcp %d", length - TH_OFF(tp) * 4);
274 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|
275 TH_ECNECHO|TH_CWR)) {
285 putchar('W'); /* congestion _W_indow reduced (ECN) */
286 if (flags & TH_ECNECHO)
287 putchar('E'); /* ecn _E_cho sent (ECN) */
291 if (!Sflag && (flags & TH_ACK)) {
292 register struct tcp_seq_hash *th;
296 * Find (or record) the initial sequence numbers for
297 * this conversation. (we pick an arbitrary
298 * collating order so there's only one entry for
302 memset(&tha, 0, sizeof(tha));
307 else if (sport == dport) {
310 for (i = 0; i < 4; i++) {
311 if (((u_int32_t *)(&ip6->ip6_src))[i] >
312 ((u_int32_t *)(&ip6->ip6_dst))[i]) {
319 tha.src = ip6->ip6_dst;
320 tha.dst = ip6->ip6_src;
321 tha.port = dport << 16 | sport;
323 tha.dst = ip6->ip6_dst;
324 tha.src = ip6->ip6_src;
325 tha.port = sport << 16 | dport;
330 ip->ip_src.s_addr > ip->ip_dst.s_addr)) {
334 *(struct in_addr *)&tha.src = ip->ip_dst;
335 *(struct in_addr *)&tha.dst = ip->ip_src;
336 tha.port = dport << 16 | sport;
338 *(struct in_addr *)&tha.dst = ip->ip_dst;
339 *(struct in_addr *)&tha.src = ip->ip_src;
340 tha.port = sport << 16 | dport;
346 ip->ip_src.s_addr < ip->ip_dst.s_addr)) {
347 tha.src = ip->ip_src, tha.dst = ip->ip_dst;
348 tha.port = sport << 16 | dport;
351 tha.src = ip->ip_dst, tha.dst = ip->ip_src;
352 tha.port = dport << 16 | sport;
358 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
359 th->nxt; th = th->nxt)
360 if (!memcmp((char *)&tha, (char *)&th->addr,
364 if (!th->nxt || (flags & TH_SYN)) {
365 /* didn't find it or new conversation */
366 if (th->nxt == NULL) {
367 th->nxt = (struct tcp_seq_hash *)
368 calloc(1, sizeof(*th));
370 error("tcp_print: calloc");
374 th->ack = seq, th->seq = ack - 1;
376 th->seq = seq, th->ack = ack - 1;
379 seq -= th->ack, ack -= th->seq;
381 seq -= th->seq, ack -= th->ack;
388 thseq = thack = threv = 0;
391 (void)printf(" [bad hdr length]");
395 if (IP_V(ip) == 4 && vflag && !fragmented) {
397 if (TTEST2(tp->th_sport, length)) {
398 sum = tcp_cksum(ip, tp, length);
400 (void)printf(" [bad tcp cksum %x!]", sum);
402 (void)printf(" [tcp sum ok]");
406 if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !fragmented) {
408 if (TTEST2(tp->th_sport, length)) {
409 sum = tcp6_cksum(ip6, tp, length);
411 (void)printf(" [bad tcp cksum %x!]", sum);
413 (void)printf(" [tcp sum ok]");
419 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
420 (void)printf(" %u:%u(%u)", seq, seq + length, length);
422 (void)printf(" ack %u", ack);
424 (void)printf(" win %d", win);
427 (void)printf(" urg %d", urp);
429 * Handle any options.
431 if ((hlen -= sizeof(*tp)) > 0) {
432 register const u_char *cp;
433 register int i, opt, len, datalen;
435 cp = (const u_char *)tp + sizeof(*tp);
446 len = *cp++; /* total including type, len */
447 if (len < 2 || len > hlen)
449 --hlen; /* account for length byte */
451 --hlen; /* account for type byte */
454 /* Bail if "l" bytes of data are not left or were not captured */
455 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
463 (void)printf(" %u", EXTRACT_16BITS(cp));
476 (void)printf("wscale");
479 (void)printf(" %u", *cp);
483 (void)printf("sackOK");
487 (void)printf("sack");
489 if (datalen % 8 != 0) {
490 (void)printf(" malformed sack ");
494 (void)printf(" sack %d ", datalen / 8);
495 for (i = 0; i < datalen; i += 8) {
497 s = EXTRACT_32BITS(cp + i);
499 e = EXTRACT_32BITS(cp + i + 4);
507 (void)printf("{%u:%u}", s, e);
514 (void)printf("echo");
517 (void)printf(" %u", EXTRACT_32BITS(cp));
520 case TCPOPT_ECHOREPLY:
521 (void)printf("echoreply");
524 (void)printf(" %u", EXTRACT_32BITS(cp));
527 case TCPOPT_TIMESTAMP:
528 (void)printf("timestamp");
531 (void)printf(" %u", EXTRACT_32BITS(cp));
533 (void)printf(" %u", EXTRACT_32BITS(cp + 4));
540 (void)printf(" %u", EXTRACT_32BITS(cp));
544 (void)printf("ccnew");
547 (void)printf(" %u", EXTRACT_32BITS(cp));
551 (void)printf("ccecho");
554 (void)printf(" %u", EXTRACT_32BITS(cp));
558 (void)printf("opt-%d:", opt);
560 for (i = 0; i < datalen; ++i) {
562 (void)printf("%02x", cp[i]);
567 /* Account for data printed */
571 /* Check specification against observed length */
572 ++datalen; /* option octet */
573 if (!ZEROLENOPT(opt))
574 ++datalen; /* size octet */
576 (void)printf("[len %d]", len);
578 if (opt == TCPOPT_EOL)
588 * Decode payload if necessary.
590 bp += TH_OFF(tp) * 4;
591 if (flags & TH_RST) {
593 print_tcp_rst_data(bp, length);
595 if (sport == TELNET_PORT || dport == TELNET_PORT) {
597 telnet_print(bp, length);
598 } else if (sport == BGP_PORT || dport == BGP_PORT)
599 bgp_print(bp, length);
600 else if (sport == PPTP_PORT || dport == PPTP_PORT)
601 pptp_print(bp, length);
602 #ifdef TCPDUMP_DO_SMB
603 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
604 nbt_tcp_print(bp, length);
606 else if (sport == BEEP_PORT || dport == BEEP_PORT)
607 beep_print(bp, length);
608 else if (length > 2 &&
609 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT)) {
611 * TCP DNS query has 2byte length at the head.
612 * XXX packet could be unaligned, it can go strange
614 ns_print(bp + 2, length - 2);
615 } else if (sport == MSDP_PORT || dport == MSDP_PORT) {
616 msdp_print(bp, length);
621 fputs("[bad opt]", stdout);
626 fputs("[|tcp]", stdout);
632 * RFC1122 says the following on data in RST segments:
634 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
636 * A TCP SHOULD allow a received RST segment to include data.
639 * It has been suggested that a RST segment could contain
640 * ASCII text that encoded and explained the cause of the
641 * RST. No standard has yet been established for such
647 print_tcp_rst_data(register const u_char *sp, u_int length)
651 if (TTEST2(*sp, length))
655 if (length > MAX_RST_DATA_LEN) {
656 length = MAX_RST_DATA_LEN; /* can use -X for longer */
657 putchar('+'); /* indicate we truncate */
660 while (length-- && sp <= snapend) {