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.70 2000-07-30 05:56:11 assar Exp $ (LBL)";
31 #include <sys/param.h>
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/ip.h>
39 #include <netinet/ip_var.h>
40 #include <netinet/tcp.h>
49 #include <netinet/ip6.h>
52 #include "interface.h"
53 #include "addrtoname.h"
56 static void print_tcp_rst_data(register const u_char
*sp
, u_int length
);
58 #define MAX_RST_DATA_LEN 30
62 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
65 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
68 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
71 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
73 #ifndef TCPOPT_ECHOREPLY
74 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
76 #ifndef TCPOPT_TIMESTAMP
77 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
80 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
83 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
86 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
90 * Definitions required for ECN
91 * for use if the OS running tcpdump does not have ECN
94 #define TH_ECNECHO 0x40 /* ECN Echo in tcp header */
97 #define TH_CWR 0x80 /* ECN Cwnd Reduced in tcp header*/
111 struct tcp_seq_hash
{
112 struct tcp_seq_hash
*nxt
;
118 #define TSEQ_HASHSIZE 919
120 /* These tcp optinos do not have the size octet */
121 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
123 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
127 #define TELNET_PORT 23
132 #define NETBIOS_SSN_PORT 139
134 #define NFS_PORT 2049
138 tcp_print(register const u_char
*bp
, register u_int length
,
139 register const u_char
*bp2
)
141 register const struct tcphdr
*tp
;
142 register const struct ip
*ip
;
143 register u_char flags
;
146 u_int16_t sport
, dport
, win
, urp
;
147 u_int32_t seq
, ack
, thseq
, thack
;
150 register const struct ip6_hdr
*ip6
;
153 tp
= (struct tcphdr
*)bp
;
154 ip
= (struct ip
*)bp2
;
157 ip6
= (struct ip6_hdr
*)bp2
;
162 if (!TTEST(tp
->th_dport
)) {
163 (void)printf("%s > %s: [|tcp]",
164 ipaddr_string(&ip
->ip_src
),
165 ipaddr_string(&ip
->ip_dst
));
169 sport
= ntohs(tp
->th_sport
);
170 dport
= ntohs(tp
->th_dport
);
173 hlen
= tp
->th_off
* 4;
176 * If data present and NFS port used, assume NFS.
177 * Pass offset of data plus 4 bytes for RPC TCP msg length
178 * to NFS print routines.
181 if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
) <= snapend
&&
183 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
-hlen
,
186 } else if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
)
189 nfsreply_print((u_char
*)tp
+ hlen
+ 4,length
-hlen
,
196 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
197 (void)printf("%s.%s > %s.%s: ",
198 ip6addr_string(&ip6
->ip6_src
),
199 tcpport_string(sport
),
200 ip6addr_string(&ip6
->ip6_dst
),
201 tcpport_string(dport
));
203 (void)printf("%s > %s: ",
204 tcpport_string(sport
), tcpport_string(dport
));
209 if (ip
->ip_p
== IPPROTO_TCP
) {
210 (void)printf("%s.%s > %s.%s: ",
211 ipaddr_string(&ip
->ip_src
),
212 tcpport_string(sport
),
213 ipaddr_string(&ip
->ip_dst
),
214 tcpport_string(dport
));
216 (void)printf("%s > %s: ",
217 tcpport_string(sport
), tcpport_string(dport
));
223 seq
= (u_int32_t
)ntohl(tp
->th_seq
);
224 ack
= (u_int32_t
)ntohl(tp
->th_ack
);
225 win
= ntohs(tp
->th_win
);
226 urp
= ntohs(tp
->th_urp
);
229 (void)printf("tcp %d", length
- tp
->th_off
* 4);
232 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
233 TH_ECNECHO
|TH_CWR
)) {
243 putchar('W'); /* congestion _W_indow reduced (ECN) */
244 if (flags
& TH_ECNECHO
)
245 putchar('E'); /* ecn _E_cho sent (ECN) */
249 if (!Sflag
&& (flags
& TH_ACK
)) {
250 register struct tcp_seq_hash
*th
;
254 * Find (or record) the initial sequence numbers for
255 * this conversation. (we pick an arbitrary
256 * collating order so there's only one entry for
260 memset(&tha
, 0, sizeof(tha
));
265 } else if (sport
== dport
) {
268 for (i
= 0; i
< 4; i
++) {
269 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
270 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
277 tha
.src
= ip6
->ip6_dst
;
278 tha
.dst
= ip6
->ip6_src
;
279 tha
.port
= dport
<< 16 | sport
;
281 tha
.dst
= ip6
->ip6_dst
;
282 tha
.src
= ip6
->ip6_src
;
283 tha
.port
= sport
<< 16 | dport
;
288 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
292 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
293 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
294 tha
.port
= dport
<< 16 | sport
;
296 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
297 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
298 tha
.port
= sport
<< 16 | dport
;
304 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
305 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
306 tha
.port
= sport
<< 16 | dport
;
309 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
310 tha
.port
= dport
<< 16 | sport
;
316 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
317 th
->nxt
; th
= th
->nxt
)
318 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
322 if (!th
->nxt
|| (flags
& TH_SYN
)) {
323 /* didn't find it or new conversation */
324 if (th
->nxt
== NULL
) {
325 th
->nxt
= (struct tcp_seq_hash
*)
326 calloc(1, sizeof(*th
));
328 error("tcp_print: calloc");
332 th
->ack
= seq
, th
->seq
= ack
- 1;
334 th
->seq
= seq
, th
->ack
= ack
- 1;
338 seq
-= th
->ack
, ack
-= th
->seq
;
340 seq
-= th
->seq
, ack
-= th
->ack
;
347 thseq
= thack
= threv
= 0;
350 (void)printf(" [bad hdr length]");
354 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
355 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
357 (void)printf(" ack %u", ack
);
359 (void)printf(" win %d", win
);
362 (void)printf(" urg %d", urp
);
364 * Handle any options.
366 if ((hlen
-= sizeof(*tp
)) > 0) {
367 register const u_char
*cp
;
368 register int i
, opt
, len
, datalen
;
370 cp
= (const u_char
*)tp
+ sizeof(*tp
);
381 len
= *cp
++; /* total including type, len */
382 if (len
< 2 || len
> hlen
)
384 --hlen
; /* account for length byte */
386 --hlen
; /* account for type byte */
389 /* Bail if "l" bytes of data are not left or were not captured */
390 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
398 (void)printf(" %u", EXTRACT_16BITS(cp
));
411 (void)printf("wscale");
414 (void)printf(" %u", *cp
);
418 (void)printf("sackOK");
422 (void)printf("sack");
424 if (datalen
% 8 != 0) {
425 (void)printf(" malformed sack ");
429 (void)printf(" sack %d ", datalen
/ 8);
430 for (i
= 0; i
< datalen
; i
+= 8) {
432 s
= EXTRACT_32BITS(cp
+ i
);
434 e
= EXTRACT_32BITS(cp
+ i
+ 4);
442 (void)printf("{%u:%u}", s
, e
);
449 (void)printf("echo");
452 (void)printf(" %u", EXTRACT_32BITS(cp
));
455 case TCPOPT_ECHOREPLY
:
456 (void)printf("echoreply");
459 (void)printf(" %u", EXTRACT_32BITS(cp
));
462 case TCPOPT_TIMESTAMP
:
463 (void)printf("timestamp");
466 (void)printf(" %u", EXTRACT_32BITS(cp
));
468 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
475 (void)printf(" %u", EXTRACT_32BITS(cp
));
479 (void)printf("ccnew");
482 (void)printf(" %u", EXTRACT_32BITS(cp
));
486 (void)printf("ccecho");
489 (void)printf(" %u", EXTRACT_32BITS(cp
));
493 (void)printf("opt-%d:", opt
);
495 for (i
= 0; i
< datalen
; ++i
) {
497 (void)printf("%02x", cp
[i
]);
502 /* Account for data printed */
506 /* Check specification against observed length */
507 ++datalen
; /* option octet */
508 if (!ZEROLENOPT(opt
))
509 ++datalen
; /* size octet */
511 (void)printf("[len %d]", len
);
513 if (opt
== TCPOPT_EOL
)
523 * Decode payload if necessary.
525 bp
+= (tp
->th_off
* 4);
526 if (flags
& TH_RST
) {
528 print_tcp_rst_data(bp
, length
);
530 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
532 telnet_print(bp
, length
);
533 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
534 bgp_print(bp
, length
);
535 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
536 nbt_tcp_print(bp
, length
);
540 fputs("[bad opt]", stdout
);
545 fputs("[|tcp]", stdout
);
551 * RFC1122 says the following on data in RST segments:
553 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
555 * A TCP SHOULD allow a received RST segment to include data.
558 * It has been suggested that a RST segment could contain
559 * ASCII text that encoded and explained the cause of the
560 * RST. No standard has yet been established for such
566 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
570 if (TTEST2(*sp
, length
))
574 if (length
> MAX_RST_DATA_LEN
) {
575 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
576 putchar('+'); /* indicate we truncate */
579 while (length
-- && sp
<= snapend
) {