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.65 2000-04-28 11:34:13 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) */
86 * Definitions required for ECN
87 * for use if the OS running tcpdump does not have ECN
90 #define TH_ECNECHO 0x40 /* ECN Echo in tcp header */
93 #define TH_CWR 0x80 /* ECN Cwnd Reduced in tcp header*/
107 struct tcp_seq_hash
{
108 struct tcp_seq_hash
*nxt
;
114 #define TSEQ_HASHSIZE 919
116 /* These tcp optinos do not have the size octet */
117 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
119 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
123 #define TELNET_PORT 23
128 #define NETBIOS_SSN_PORT 139
131 tcp_print(register const u_char
*bp
, register u_int length
,
132 register const u_char
*bp2
)
134 register const struct tcphdr
*tp
;
135 register const struct ip
*ip
;
136 register u_char flags
;
139 u_int16_t sport
, dport
, win
, urp
;
140 u_int32_t seq
, ack
, thseq
, thack
;
143 register const struct ip6_hdr
*ip6
;
146 tp
= (struct tcphdr
*)bp
;
147 ip
= (struct ip
*)bp2
;
150 ip6
= (struct ip6_hdr
*)bp2
;
155 if (!TTEST(tp
->th_dport
)) {
156 (void)printf("%s > %s: [|tcp]",
157 ipaddr_string(&ip
->ip_src
),
158 ipaddr_string(&ip
->ip_dst
));
162 sport
= ntohs(tp
->th_sport
);
163 dport
= ntohs(tp
->th_dport
);
167 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
168 (void)printf("%s.%s > %s.%s: ",
169 ip6addr_string(&ip6
->ip6_src
),
170 tcpport_string(sport
),
171 ip6addr_string(&ip6
->ip6_dst
),
172 tcpport_string(dport
));
174 (void)printf("%s > %s: ",
175 tcpport_string(sport
), tcpport_string(dport
));
180 if (ip
->ip_p
== IPPROTO_TCP
) {
181 (void)printf("%s.%s > %s.%s: ",
182 ipaddr_string(&ip
->ip_src
),
183 tcpport_string(sport
),
184 ipaddr_string(&ip
->ip_dst
),
185 tcpport_string(dport
));
187 (void)printf("%s > %s: ",
188 tcpport_string(sport
), tcpport_string(dport
));
194 seq
= (u_int32_t
)ntohl(tp
->th_seq
);
195 ack
= (u_int32_t
)ntohl(tp
->th_ack
);
196 win
= ntohs(tp
->th_win
);
197 urp
= ntohs(tp
->th_urp
);
200 (void)printf("tcp %d", length
- tp
->th_off
* 4);
203 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
204 TH_ECNECHO
|TH_CWR
)) {
214 putchar('W'); /* congestion _W_indow reduced (ECN) */
215 if (flags
& TH_ECNECHO
)
216 putchar('E'); /* ecn _E_cho sent (ECN) */
220 if (!Sflag
&& (flags
& TH_ACK
)) {
221 register struct tcp_seq_hash
*th
;
225 * Find (or record) the initial sequence numbers for
226 * this conversation. (we pick an arbitrary
227 * collating order so there's only one entry for
231 bzero(&tha
, sizeof(tha
));
236 } else if (sport
== dport
) {
239 for (i
= 0; i
< 4; i
++) {
240 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
241 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
248 tha
.src
= ip6
->ip6_dst
;
249 tha
.dst
= ip6
->ip6_src
;
250 tha
.port
= dport
<< 16 | sport
;
252 tha
.dst
= ip6
->ip6_dst
;
253 tha
.src
= ip6
->ip6_src
;
254 tha
.port
= sport
<< 16 | dport
;
259 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
263 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
264 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
265 tha
.port
= dport
<< 16 | sport
;
267 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
268 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
269 tha
.port
= sport
<< 16 | dport
;
275 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
276 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
277 tha
.port
= sport
<< 16 | dport
;
280 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
281 tha
.port
= dport
<< 16 | sport
;
287 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
288 th
->nxt
; th
= th
->nxt
)
289 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
293 if (!th
->nxt
|| (flags
& TH_SYN
)) {
294 /* didn't find it or new conversation */
295 if (th
->nxt
== NULL
) {
296 th
->nxt
= (struct tcp_seq_hash
*)
297 calloc(1, sizeof(*th
));
299 error("tcp_print: calloc");
303 th
->ack
= seq
, th
->seq
= ack
- 1;
305 th
->seq
= seq
, th
->ack
= ack
- 1;
309 seq
-= th
->ack
, ack
-= th
->seq
;
311 seq
-= th
->seq
, ack
-= th
->ack
;
318 thseq
= thack
= threv
= 0;
320 hlen
= tp
->th_off
* 4;
322 (void)printf(" [bad hdr length]");
326 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
327 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
329 (void)printf(" ack %u", ack
);
331 (void)printf(" win %d", win
);
334 (void)printf(" urg %d", urp
);
336 * Handle any options.
338 if ((hlen
-= sizeof(*tp
)) > 0) {
339 register const u_char
*cp
;
340 register int i
, opt
, len
, datalen
;
342 cp
= (const u_char
*)tp
+ sizeof(*tp
);
353 len
= *cp
++; /* total including type, len */
354 if (len
< 2 || len
> hlen
)
356 --hlen
; /* account for length byte */
358 --hlen
; /* account for type byte */
361 /* Bail if "l" bytes of data are not left or were not captured */
362 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
370 (void)printf(" %u", EXTRACT_16BITS(cp
));
383 (void)printf("wscale");
386 (void)printf(" %u", *cp
);
390 (void)printf("sackOK");
394 (void)printf("sack");
396 if (datalen
% 8 != 0) {
397 (void)printf(" malformed sack ");
401 (void)printf(" sack %d ", datalen
/ 8);
402 for (i
= 0; i
< datalen
; i
+= 8) {
404 s
= EXTRACT_32BITS(cp
+ i
);
406 e
= EXTRACT_32BITS(cp
+ i
+ 4);
414 (void)printf("{%u:%u}", s
, e
);
421 (void)printf("echo");
424 (void)printf(" %u", EXTRACT_32BITS(cp
));
427 case TCPOPT_ECHOREPLY
:
428 (void)printf("echoreply");
431 (void)printf(" %u", EXTRACT_32BITS(cp
));
434 case TCPOPT_TIMESTAMP
:
435 (void)printf("timestamp");
438 (void)printf(" %u", EXTRACT_32BITS(cp
));
440 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
447 (void)printf(" %u", EXTRACT_32BITS(cp
));
451 (void)printf("ccnew");
454 (void)printf(" %u", EXTRACT_32BITS(cp
));
458 (void)printf("ccecho");
461 (void)printf(" %u", EXTRACT_32BITS(cp
));
465 (void)printf("opt-%d:", opt
);
467 for (i
= 0; i
< datalen
; ++i
) {
469 (void)printf("%02x", cp
[i
]);
474 /* Account for data printed */
478 /* Check specification against observed length */
479 ++datalen
; /* option octet */
480 if (!ZEROLENOPT(opt
))
481 ++datalen
; /* size octet */
483 (void)printf("[len %d]", len
);
485 if (opt
== TCPOPT_EOL
)
495 * Decode payload if necessary.
497 bp
+= (tp
->th_off
* 4);
498 if (!qflag
&& vflag
&& length
> 0
499 && (sport
== TELNET_PORT
|| dport
== TELNET_PORT
))
500 telnet_print(bp
, length
);
501 else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
502 bgp_print(bp
, length
);
503 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
504 nbt_tcp_print(bp
, length
);
507 fputs("[bad opt]", stdout
);
512 fputs("[|tcp]", stdout
);