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.63 1999/12/22 15:44:10 itojun Exp $ (LBL)";
31 #include <sys/param.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_var.h>
38 #include <netinet/tcp.h>
49 #include <netinet/ip6.h>
52 #include "interface.h"
53 #include "addrtoname.h"
58 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
61 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
64 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
67 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
69 #ifndef TCPOPT_ECHOREPLY
70 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
72 #ifndef TCPOPT_TIMESTAMP
73 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
76 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
79 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
82 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
97 struct tcp_seq_hash *nxt;
103 #define TSEQ_HASHSIZE 919
105 /* These tcp optinos do not have the size octet */
106 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
108 static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
112 #define TELNET_PORT 23
117 #define NETBIOS_SSN_PORT 139
120 tcp_print(register const u_char *bp, register u_int length,
121 register const u_char *bp2)
123 register const struct tcphdr *tp;
124 register const struct ip *ip;
125 register u_char flags;
128 u_short sport, dport, win, urp;
129 u_int32_t seq, ack, thseq, thack;
132 register const struct ip6_hdr *ip6;
135 tp = (struct tcphdr *)bp;
136 ip = (struct ip *)bp2;
139 ip6 = (struct ip6_hdr *)bp2;
144 if (!TTEST(tp->th_dport)) {
145 (void)printf("%s > %s: [|tcp]",
146 ipaddr_string(&ip->ip_src),
147 ipaddr_string(&ip->ip_dst));
151 sport = ntohs(tp->th_sport);
152 dport = ntohs(tp->th_dport);
156 if (ip6->ip6_nxt == IPPROTO_TCP) {
157 (void)printf("%s.%s > %s.%s: ",
158 ip6addr_string(&ip6->ip6_src),
159 tcpport_string(sport),
160 ip6addr_string(&ip6->ip6_dst),
161 tcpport_string(dport));
163 (void)printf("%s > %s: ",
164 tcpport_string(sport), tcpport_string(dport));
169 if (ip->ip_p == IPPROTO_TCP) {
170 (void)printf("%s.%s > %s.%s: ",
171 ipaddr_string(&ip->ip_src),
172 tcpport_string(sport),
173 ipaddr_string(&ip->ip_dst),
174 tcpport_string(dport));
176 (void)printf("%s > %s: ",
177 tcpport_string(sport), tcpport_string(dport));
183 seq = ntohl(tp->th_seq);
184 ack = ntohl(tp->th_ack);
185 win = ntohs(tp->th_win);
186 urp = ntohs(tp->th_urp);
189 (void)printf("tcp %d", length - tp->th_off * 4);
193 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|TH_ECN))
195 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH))
218 printf("%sCWR", (flags&0x40) ? "," : "");
222 if (!Sflag && (flags & TH_ACK)) {
223 register struct tcp_seq_hash *th;
227 * Find (or record) the initial sequence numbers for
228 * this conversation. (we pick an arbitrary
229 * collating order so there's only one entry for
233 bzero(&tha, sizeof(tha));
238 } else if (sport == dport) {
241 for (i = 0; i < 4; i++) {
242 if (((u_int32_t *)(&ip6->ip6_src))[i] >
243 ((u_int32_t *)(&ip6->ip6_dst))[i]) {
250 tha.src = ip6->ip6_dst;
251 tha.dst = ip6->ip6_src;
252 tha.port = dport << 16 | sport;
254 tha.dst = ip6->ip6_dst;
255 tha.src = ip6->ip6_src;
256 tha.port = sport << 16 | dport;
261 ip->ip_src.s_addr > ip->ip_dst.s_addr)) {
265 *(struct in_addr *)&tha.src = ip->ip_dst;
266 *(struct in_addr *)&tha.dst = ip->ip_src;
267 tha.port = dport << 16 | sport;
269 *(struct in_addr *)&tha.dst = ip->ip_dst;
270 *(struct in_addr *)&tha.src = ip->ip_src;
271 tha.port = sport << 16 | dport;
277 ip->ip_src.s_addr < ip->ip_dst.s_addr)) {
278 tha.src = ip->ip_src, tha.dst = ip->ip_dst;
279 tha.port = sport << 16 | dport;
282 tha.src = ip->ip_dst, tha.dst = ip->ip_src;
283 tha.port = dport << 16 | sport;
289 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
290 th->nxt; th = th->nxt)
291 if (!memcmp((char *)&tha, (char *)&th->addr,
295 if (!th->nxt || flags & TH_SYN) {
296 /* didn't find it or new conversation */
297 if (th->nxt == NULL) {
298 th->nxt = (struct tcp_seq_hash *)
299 calloc(1, sizeof(*th));
301 error("tcp_print: calloc");
305 th->ack = seq, th->seq = ack - 1;
307 th->seq = seq, th->ack = ack - 1;
314 seq -= th->ack, ack -= th->seq;
316 seq -= th->seq, ack -= th->ack;
319 hlen = tp->th_off * 4;
321 (void)printf(" [bad hdr length]");
325 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
326 (void)printf(" %u:%u(%d)", seq, seq + length, length);
328 (void)printf(" ack %u", ack);
330 (void)printf(" win %d", win);
333 (void)printf(" urg %d", urp);
335 * Handle any options.
337 if ((hlen -= sizeof(*tp)) > 0) {
338 register const u_char *cp;
339 register int i, opt, len, datalen;
341 cp = (const u_char *)tp + sizeof(*tp);
352 len = *cp++; /* total including type, len */
353 if (len < 2 || len > hlen)
355 --hlen; /* account for length byte */
357 --hlen; /* account for type byte */
360 /* Bail if "l" bytes of data are not left or were not captured */
361 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
369 (void)printf(" %u", EXTRACT_16BITS(cp));
382 (void)printf("wscale");
385 (void)printf(" %u", *cp);
389 (void)printf("sackOK");
393 (void)printf("sack");
395 if (datalen % 8 != 0) {
396 (void)printf(" malformed sack ");
400 (void)printf(" sack %d ", datalen / 8);
401 for (i = 0; i < datalen; i += 8) {
403 s = EXTRACT_32BITS(cp + i);
405 e = EXTRACT_32BITS(cp + i + 4);
413 (void)printf("{%u:%u}", s, e);
420 (void)printf("echo");
423 (void)printf(" %u", EXTRACT_32BITS(cp));
426 case TCPOPT_ECHOREPLY:
427 (void)printf("echoreply");
430 (void)printf(" %u", EXTRACT_32BITS(cp));
433 case TCPOPT_TIMESTAMP:
434 (void)printf("timestamp");
437 (void)printf(" %u", EXTRACT_32BITS(cp));
439 (void)printf(" %u", EXTRACT_32BITS(cp + 4));
446 (void)printf(" %u", EXTRACT_32BITS(cp));
450 (void)printf("ccnew");
453 (void)printf(" %u", EXTRACT_32BITS(cp));
457 (void)printf("ccecho");
460 (void)printf(" %u", EXTRACT_32BITS(cp));
464 (void)printf("opt-%d:", opt);
466 for (i = 0; i < datalen; ++i) {
468 (void)printf("%02x", cp[i]);
473 /* Account for data printed */
477 /* Check specification against observed length */
478 ++datalen; /* option octet */
479 if (!ZEROLENOPT(opt))
480 ++datalen; /* size octet */
482 (void)printf("[len %d]", len);
484 if (opt == TCPOPT_EOL)
494 * Decode payload if necessary.
496 bp += (tp->th_off * 4);
497 if (!qflag && vflag && length > 0
498 && (sport == TELNET_PORT || dport == TELNET_PORT))
499 telnet_print(bp, length);
500 else if (sport == BGP_PORT || dport == BGP_PORT)
501 bgp_print(bp, length);
502 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
503 nbt_tcp_print(bp, length);
506 fputs("[bad opt]", stdout);
511 fputs("[|tcp]", stdout);