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.69 2000-07-29 09:06:23 guy 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>
48 #include <netinet/ip6.h>
51 #include "interface.h"
52 #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
133 #define NFS_PORT 2049
137 tcp_print(register const u_char
*bp
, register u_int length
,
138 register const u_char
*bp2
)
140 register const struct tcphdr
*tp
;
141 register const struct ip
*ip
;
142 register u_char flags
;
145 u_int16_t sport
, dport
, win
, urp
;
146 u_int32_t seq
, ack
, thseq
, thack
;
149 register const struct ip6_hdr
*ip6
;
152 tp
= (struct tcphdr
*)bp
;
153 ip
= (struct ip
*)bp2
;
156 ip6
= (struct ip6_hdr
*)bp2
;
161 if (!TTEST(tp
->th_dport
)) {
162 (void)printf("%s > %s: [|tcp]",
163 ipaddr_string(&ip
->ip_src
),
164 ipaddr_string(&ip
->ip_dst
));
168 sport
= ntohs(tp
->th_sport
);
169 dport
= ntohs(tp
->th_dport
);
172 hlen
= tp
->th_off
* 4;
175 * If data present and NFS port used, assume NFS.
176 * Pass offset of data plus 4 bytes for RPC TCP msg length
177 * to NFS print routines.
180 if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
) <= snapend
&&
182 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
-hlen
,
185 } else if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
)
188 nfsreply_print((u_char
*)tp
+ hlen
+ 4,length
-hlen
,
195 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
196 (void)printf("%s.%s > %s.%s: ",
197 ip6addr_string(&ip6
->ip6_src
),
198 tcpport_string(sport
),
199 ip6addr_string(&ip6
->ip6_dst
),
200 tcpport_string(dport
));
202 (void)printf("%s > %s: ",
203 tcpport_string(sport
), tcpport_string(dport
));
208 if (ip
->ip_p
== IPPROTO_TCP
) {
209 (void)printf("%s.%s > %s.%s: ",
210 ipaddr_string(&ip
->ip_src
),
211 tcpport_string(sport
),
212 ipaddr_string(&ip
->ip_dst
),
213 tcpport_string(dport
));
215 (void)printf("%s > %s: ",
216 tcpport_string(sport
), tcpport_string(dport
));
222 seq
= (u_int32_t
)ntohl(tp
->th_seq
);
223 ack
= (u_int32_t
)ntohl(tp
->th_ack
);
224 win
= ntohs(tp
->th_win
);
225 urp
= ntohs(tp
->th_urp
);
228 (void)printf("tcp %d", length
- tp
->th_off
* 4);
231 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
232 TH_ECNECHO
|TH_CWR
)) {
242 putchar('W'); /* congestion _W_indow reduced (ECN) */
243 if (flags
& TH_ECNECHO
)
244 putchar('E'); /* ecn _E_cho sent (ECN) */
248 if (!Sflag
&& (flags
& TH_ACK
)) {
249 register struct tcp_seq_hash
*th
;
253 * Find (or record) the initial sequence numbers for
254 * this conversation. (we pick an arbitrary
255 * collating order so there's only one entry for
259 memset(&tha
, 0, sizeof(tha
));
264 } else if (sport
== dport
) {
267 for (i
= 0; i
< 4; i
++) {
268 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
269 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
276 tha
.src
= ip6
->ip6_dst
;
277 tha
.dst
= ip6
->ip6_src
;
278 tha
.port
= dport
<< 16 | sport
;
280 tha
.dst
= ip6
->ip6_dst
;
281 tha
.src
= ip6
->ip6_src
;
282 tha
.port
= sport
<< 16 | dport
;
287 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
291 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
292 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
293 tha
.port
= dport
<< 16 | sport
;
295 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
296 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
297 tha
.port
= sport
<< 16 | dport
;
303 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
304 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
305 tha
.port
= sport
<< 16 | dport
;
308 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
309 tha
.port
= dport
<< 16 | sport
;
315 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
316 th
->nxt
; th
= th
->nxt
)
317 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
321 if (!th
->nxt
|| (flags
& TH_SYN
)) {
322 /* didn't find it or new conversation */
323 if (th
->nxt
== NULL
) {
324 th
->nxt
= (struct tcp_seq_hash
*)
325 calloc(1, sizeof(*th
));
327 error("tcp_print: calloc");
331 th
->ack
= seq
, th
->seq
= ack
- 1;
333 th
->seq
= seq
, th
->ack
= ack
- 1;
337 seq
-= th
->ack
, ack
-= th
->seq
;
339 seq
-= th
->seq
, ack
-= th
->ack
;
346 thseq
= thack
= threv
= 0;
349 (void)printf(" [bad hdr length]");
353 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
354 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
356 (void)printf(" ack %u", ack
);
358 (void)printf(" win %d", win
);
361 (void)printf(" urg %d", urp
);
363 * Handle any options.
365 if ((hlen
-= sizeof(*tp
)) > 0) {
366 register const u_char
*cp
;
367 register int i
, opt
, len
, datalen
;
369 cp
= (const u_char
*)tp
+ sizeof(*tp
);
380 len
= *cp
++; /* total including type, len */
381 if (len
< 2 || len
> hlen
)
383 --hlen
; /* account for length byte */
385 --hlen
; /* account for type byte */
388 /* Bail if "l" bytes of data are not left or were not captured */
389 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
397 (void)printf(" %u", EXTRACT_16BITS(cp
));
410 (void)printf("wscale");
413 (void)printf(" %u", *cp
);
417 (void)printf("sackOK");
421 (void)printf("sack");
423 if (datalen
% 8 != 0) {
424 (void)printf(" malformed sack ");
428 (void)printf(" sack %d ", datalen
/ 8);
429 for (i
= 0; i
< datalen
; i
+= 8) {
431 s
= EXTRACT_32BITS(cp
+ i
);
433 e
= EXTRACT_32BITS(cp
+ i
+ 4);
441 (void)printf("{%u:%u}", s
, e
);
448 (void)printf("echo");
451 (void)printf(" %u", EXTRACT_32BITS(cp
));
454 case TCPOPT_ECHOREPLY
:
455 (void)printf("echoreply");
458 (void)printf(" %u", EXTRACT_32BITS(cp
));
461 case TCPOPT_TIMESTAMP
:
462 (void)printf("timestamp");
465 (void)printf(" %u", EXTRACT_32BITS(cp
));
467 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
474 (void)printf(" %u", EXTRACT_32BITS(cp
));
478 (void)printf("ccnew");
481 (void)printf(" %u", EXTRACT_32BITS(cp
));
485 (void)printf("ccecho");
488 (void)printf(" %u", EXTRACT_32BITS(cp
));
492 (void)printf("opt-%d:", opt
);
494 for (i
= 0; i
< datalen
; ++i
) {
496 (void)printf("%02x", cp
[i
]);
501 /* Account for data printed */
505 /* Check specification against observed length */
506 ++datalen
; /* option octet */
507 if (!ZEROLENOPT(opt
))
508 ++datalen
; /* size octet */
510 (void)printf("[len %d]", len
);
512 if (opt
== TCPOPT_EOL
)
522 * Decode payload if necessary.
524 bp
+= (tp
->th_off
* 4);
525 if (flags
& TH_RST
) {
527 print_tcp_rst_data(bp
, length
);
529 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
531 telnet_print(bp
, length
);
532 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
533 bgp_print(bp
, length
);
534 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
535 nbt_tcp_print(bp
, length
);
539 fputs("[bad opt]", stdout
);
544 fputs("[|tcp]", stdout
);
550 * RFC1122 says the following on data in RST segments:
552 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
554 * A TCP SHOULD allow a received RST segment to include data.
557 * It has been suggested that a RST segment could contain
558 * ASCII text that encoded and explained the cause of the
559 * RST. No standard has yet been established for such
565 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
569 if (TTEST2(*sp
, length
))
573 if (length
> MAX_RST_DATA_LEN
) {
574 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
575 putchar('+'); /* indicate we truncate */
578 while (length
-- && sp
<= snapend
) {