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.66 2000-06-01 01:09:32 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>
51 #include <netinet/ip6.h>
54 #include "interface.h"
55 #include "addrtoname.h"
60 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
63 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
66 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
69 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
71 #ifndef TCPOPT_ECHOREPLY
72 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
74 #ifndef TCPOPT_TIMESTAMP
75 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
78 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
81 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
84 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
88 * Definitions required for ECN
89 * for use if the OS running tcpdump does not have ECN
92 #define TH_ECNECHO 0x40 /* ECN Echo in tcp header */
95 #define TH_CWR 0x80 /* ECN Cwnd Reduced in tcp header*/
109 struct tcp_seq_hash
{
110 struct tcp_seq_hash
*nxt
;
116 #define TSEQ_HASHSIZE 919
118 /* These tcp optinos do not have the size octet */
119 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
121 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
125 #define TELNET_PORT 23
130 #define NETBIOS_SSN_PORT 139
132 #define NFS_PORT 2049
136 tcp_print(register const u_char
*bp
, register u_int length
,
137 register const u_char
*bp2
)
139 register const struct tcphdr
*tp
;
140 register const struct ip
*ip
;
141 register u_char flags
;
144 u_int16_t sport
, dport
, win
, urp
;
145 u_int32_t seq
, ack
, thseq
, thack
;
148 register const struct ip6_hdr
*ip6
;
151 tp
= (struct tcphdr
*)bp
;
152 ip
= (struct ip
*)bp2
;
155 ip6
= (struct ip6_hdr
*)bp2
;
160 if (!TTEST(tp
->th_dport
)) {
161 (void)printf("%s > %s: [|tcp]",
162 ipaddr_string(&ip
->ip_src
),
163 ipaddr_string(&ip
->ip_dst
));
167 sport
= ntohs(tp
->th_sport
);
168 dport
= ntohs(tp
->th_dport
);
171 hlen
= tp
->th_off
* 4;
174 * If data present and NFS port used, assume NFS.
175 * Pass offset of data plus 4 bytes for RPC TCP msg length
176 * to NFS print routines.
179 if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
) <= snapend
&&
181 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
-hlen
,
184 } else if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
)
187 nfsreply_print((u_char
*)tp
+ hlen
+ 4,length
-hlen
,
194 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
195 (void)printf("%s.%s > %s.%s: ",
196 ip6addr_string(&ip6
->ip6_src
),
197 tcpport_string(sport
),
198 ip6addr_string(&ip6
->ip6_dst
),
199 tcpport_string(dport
));
201 (void)printf("%s > %s: ",
202 tcpport_string(sport
), tcpport_string(dport
));
207 if (ip
->ip_p
== IPPROTO_TCP
) {
208 (void)printf("%s.%s > %s.%s: ",
209 ipaddr_string(&ip
->ip_src
),
210 tcpport_string(sport
),
211 ipaddr_string(&ip
->ip_dst
),
212 tcpport_string(dport
));
214 (void)printf("%s > %s: ",
215 tcpport_string(sport
), tcpport_string(dport
));
221 seq
= (u_int32_t
)ntohl(tp
->th_seq
);
222 ack
= (u_int32_t
)ntohl(tp
->th_ack
);
223 win
= ntohs(tp
->th_win
);
224 urp
= ntohs(tp
->th_urp
);
227 (void)printf("tcp %d", length
- tp
->th_off
* 4);
230 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
231 TH_ECNECHO
|TH_CWR
)) {
241 putchar('W'); /* congestion _W_indow reduced (ECN) */
242 if (flags
& TH_ECNECHO
)
243 putchar('E'); /* ecn _E_cho sent (ECN) */
247 if (!Sflag
&& (flags
& TH_ACK
)) {
248 register struct tcp_seq_hash
*th
;
252 * Find (or record) the initial sequence numbers for
253 * this conversation. (we pick an arbitrary
254 * collating order so there's only one entry for
258 bzero(&tha
, sizeof(tha
));
263 } else if (sport
== dport
) {
266 for (i
= 0; i
< 4; i
++) {
267 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
268 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
275 tha
.src
= ip6
->ip6_dst
;
276 tha
.dst
= ip6
->ip6_src
;
277 tha
.port
= dport
<< 16 | sport
;
279 tha
.dst
= ip6
->ip6_dst
;
280 tha
.src
= ip6
->ip6_src
;
281 tha
.port
= sport
<< 16 | dport
;
286 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
290 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
291 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
292 tha
.port
= dport
<< 16 | sport
;
294 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
295 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
296 tha
.port
= sport
<< 16 | dport
;
302 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
303 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
304 tha
.port
= sport
<< 16 | dport
;
307 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
308 tha
.port
= dport
<< 16 | sport
;
314 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
315 th
->nxt
; th
= th
->nxt
)
316 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
320 if (!th
->nxt
|| (flags
& TH_SYN
)) {
321 /* didn't find it or new conversation */
322 if (th
->nxt
== NULL
) {
323 th
->nxt
= (struct tcp_seq_hash
*)
324 calloc(1, sizeof(*th
));
326 error("tcp_print: calloc");
330 th
->ack
= seq
, th
->seq
= ack
- 1;
332 th
->seq
= seq
, th
->ack
= ack
- 1;
336 seq
-= th
->ack
, ack
-= th
->seq
;
338 seq
-= th
->seq
, ack
-= th
->ack
;
345 thseq
= thack
= threv
= 0;
348 (void)printf(" [bad hdr length]");
352 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
353 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
355 (void)printf(" ack %u", ack
);
357 (void)printf(" win %d", win
);
360 (void)printf(" urg %d", urp
);
362 * Handle any options.
364 if ((hlen
-= sizeof(*tp
)) > 0) {
365 register const u_char
*cp
;
366 register int i
, opt
, len
, datalen
;
368 cp
= (const u_char
*)tp
+ sizeof(*tp
);
379 len
= *cp
++; /* total including type, len */
380 if (len
< 2 || len
> hlen
)
382 --hlen
; /* account for length byte */
384 --hlen
; /* account for type byte */
387 /* Bail if "l" bytes of data are not left or were not captured */
388 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
396 (void)printf(" %u", EXTRACT_16BITS(cp
));
409 (void)printf("wscale");
412 (void)printf(" %u", *cp
);
416 (void)printf("sackOK");
420 (void)printf("sack");
422 if (datalen
% 8 != 0) {
423 (void)printf(" malformed sack ");
427 (void)printf(" sack %d ", datalen
/ 8);
428 for (i
= 0; i
< datalen
; i
+= 8) {
430 s
= EXTRACT_32BITS(cp
+ i
);
432 e
= EXTRACT_32BITS(cp
+ i
+ 4);
440 (void)printf("{%u:%u}", s
, e
);
447 (void)printf("echo");
450 (void)printf(" %u", EXTRACT_32BITS(cp
));
453 case TCPOPT_ECHOREPLY
:
454 (void)printf("echoreply");
457 (void)printf(" %u", EXTRACT_32BITS(cp
));
460 case TCPOPT_TIMESTAMP
:
461 (void)printf("timestamp");
464 (void)printf(" %u", EXTRACT_32BITS(cp
));
466 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
473 (void)printf(" %u", EXTRACT_32BITS(cp
));
477 (void)printf("ccnew");
480 (void)printf(" %u", EXTRACT_32BITS(cp
));
484 (void)printf("ccecho");
487 (void)printf(" %u", EXTRACT_32BITS(cp
));
491 (void)printf("opt-%d:", opt
);
493 for (i
= 0; i
< datalen
; ++i
) {
495 (void)printf("%02x", cp
[i
]);
500 /* Account for data printed */
504 /* Check specification against observed length */
505 ++datalen
; /* option octet */
506 if (!ZEROLENOPT(opt
))
507 ++datalen
; /* size octet */
509 (void)printf("[len %d]", len
);
511 if (opt
== TCPOPT_EOL
)
521 * Decode payload if necessary.
523 bp
+= (tp
->th_off
* 4);
524 if (!qflag
&& vflag
&& length
> 0
525 && (sport
== TELNET_PORT
|| dport
== TELNET_PORT
))
526 telnet_print(bp
, length
);
527 else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
528 bgp_print(bp
, length
);
529 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
530 nbt_tcp_print(bp
, length
);
533 fputs("[bad opt]", stdout
);
538 fputs("[|tcp]", stdout
);