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.68 2000-07-29 08:03:05 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>
48 #include <netinet/ip6.h>
51 #include "interface.h"
52 #include "addrtoname.h"
57 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
60 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
63 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
66 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
68 #ifndef TCPOPT_ECHOREPLY
69 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
71 #ifndef TCPOPT_TIMESTAMP
72 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
75 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
78 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
81 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
85 * Definitions required for ECN
86 * for use if the OS running tcpdump does not have ECN
89 #define TH_ECNECHO 0x40 /* ECN Echo in tcp header */
92 #define TH_CWR 0x80 /* ECN Cwnd Reduced in tcp header*/
106 struct tcp_seq_hash
{
107 struct tcp_seq_hash
*nxt
;
113 #define TSEQ_HASHSIZE 919
115 /* These tcp optinos do not have the size octet */
116 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
118 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
122 #define TELNET_PORT 23
127 #define NETBIOS_SSN_PORT 139
129 #define NFS_PORT 2049
133 tcp_print(register const u_char
*bp
, register u_int length
,
134 register const u_char
*bp2
)
136 register const struct tcphdr
*tp
;
137 register const struct ip
*ip
;
138 register u_char flags
;
141 u_int16_t sport
, dport
, win
, urp
;
142 u_int32_t seq
, ack
, thseq
, thack
;
145 register const struct ip6_hdr
*ip6
;
148 tp
= (struct tcphdr
*)bp
;
149 ip
= (struct ip
*)bp2
;
152 ip6
= (struct ip6_hdr
*)bp2
;
157 if (!TTEST(tp
->th_dport
)) {
158 (void)printf("%s > %s: [|tcp]",
159 ipaddr_string(&ip
->ip_src
),
160 ipaddr_string(&ip
->ip_dst
));
164 sport
= ntohs(tp
->th_sport
);
165 dport
= ntohs(tp
->th_dport
);
168 hlen
= tp
->th_off
* 4;
171 * If data present and NFS port used, assume NFS.
172 * Pass offset of data plus 4 bytes for RPC TCP msg length
173 * to NFS print routines.
176 if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
) <= snapend
&&
178 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
-hlen
,
181 } else if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
)
184 nfsreply_print((u_char
*)tp
+ hlen
+ 4,length
-hlen
,
191 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
192 (void)printf("%s.%s > %s.%s: ",
193 ip6addr_string(&ip6
->ip6_src
),
194 tcpport_string(sport
),
195 ip6addr_string(&ip6
->ip6_dst
),
196 tcpport_string(dport
));
198 (void)printf("%s > %s: ",
199 tcpport_string(sport
), tcpport_string(dport
));
204 if (ip
->ip_p
== IPPROTO_TCP
) {
205 (void)printf("%s.%s > %s.%s: ",
206 ipaddr_string(&ip
->ip_src
),
207 tcpport_string(sport
),
208 ipaddr_string(&ip
->ip_dst
),
209 tcpport_string(dport
));
211 (void)printf("%s > %s: ",
212 tcpport_string(sport
), tcpport_string(dport
));
218 seq
= (u_int32_t
)ntohl(tp
->th_seq
);
219 ack
= (u_int32_t
)ntohl(tp
->th_ack
);
220 win
= ntohs(tp
->th_win
);
221 urp
= ntohs(tp
->th_urp
);
224 (void)printf("tcp %d", length
- tp
->th_off
* 4);
227 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
228 TH_ECNECHO
|TH_CWR
)) {
238 putchar('W'); /* congestion _W_indow reduced (ECN) */
239 if (flags
& TH_ECNECHO
)
240 putchar('E'); /* ecn _E_cho sent (ECN) */
244 if (!Sflag
&& (flags
& TH_ACK
)) {
245 register struct tcp_seq_hash
*th
;
249 * Find (or record) the initial sequence numbers for
250 * this conversation. (we pick an arbitrary
251 * collating order so there's only one entry for
255 memset(&tha
, 0, sizeof(tha
));
260 } else if (sport
== dport
) {
263 for (i
= 0; i
< 4; i
++) {
264 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
265 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
272 tha
.src
= ip6
->ip6_dst
;
273 tha
.dst
= ip6
->ip6_src
;
274 tha
.port
= dport
<< 16 | sport
;
276 tha
.dst
= ip6
->ip6_dst
;
277 tha
.src
= ip6
->ip6_src
;
278 tha
.port
= sport
<< 16 | dport
;
283 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
287 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
288 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
289 tha
.port
= dport
<< 16 | sport
;
291 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
292 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
293 tha
.port
= sport
<< 16 | dport
;
299 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
300 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
301 tha
.port
= sport
<< 16 | dport
;
304 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
305 tha
.port
= dport
<< 16 | sport
;
311 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
312 th
->nxt
; th
= th
->nxt
)
313 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
317 if (!th
->nxt
|| (flags
& TH_SYN
)) {
318 /* didn't find it or new conversation */
319 if (th
->nxt
== NULL
) {
320 th
->nxt
= (struct tcp_seq_hash
*)
321 calloc(1, sizeof(*th
));
323 error("tcp_print: calloc");
327 th
->ack
= seq
, th
->seq
= ack
- 1;
329 th
->seq
= seq
, th
->ack
= ack
- 1;
333 seq
-= th
->ack
, ack
-= th
->seq
;
335 seq
-= th
->seq
, ack
-= th
->ack
;
342 thseq
= thack
= threv
= 0;
345 (void)printf(" [bad hdr length]");
349 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
350 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
352 (void)printf(" ack %u", ack
);
354 (void)printf(" win %d", win
);
357 (void)printf(" urg %d", urp
);
359 * Handle any options.
361 if ((hlen
-= sizeof(*tp
)) > 0) {
362 register const u_char
*cp
;
363 register int i
, opt
, len
, datalen
;
365 cp
= (const u_char
*)tp
+ sizeof(*tp
);
376 len
= *cp
++; /* total including type, len */
377 if (len
< 2 || len
> hlen
)
379 --hlen
; /* account for length byte */
381 --hlen
; /* account for type byte */
384 /* Bail if "l" bytes of data are not left or were not captured */
385 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
393 (void)printf(" %u", EXTRACT_16BITS(cp
));
406 (void)printf("wscale");
409 (void)printf(" %u", *cp
);
413 (void)printf("sackOK");
417 (void)printf("sack");
419 if (datalen
% 8 != 0) {
420 (void)printf(" malformed sack ");
424 (void)printf(" sack %d ", datalen
/ 8);
425 for (i
= 0; i
< datalen
; i
+= 8) {
427 s
= EXTRACT_32BITS(cp
+ i
);
429 e
= EXTRACT_32BITS(cp
+ i
+ 4);
437 (void)printf("{%u:%u}", s
, e
);
444 (void)printf("echo");
447 (void)printf(" %u", EXTRACT_32BITS(cp
));
450 case TCPOPT_ECHOREPLY
:
451 (void)printf("echoreply");
454 (void)printf(" %u", EXTRACT_32BITS(cp
));
457 case TCPOPT_TIMESTAMP
:
458 (void)printf("timestamp");
461 (void)printf(" %u", EXTRACT_32BITS(cp
));
463 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
470 (void)printf(" %u", EXTRACT_32BITS(cp
));
474 (void)printf("ccnew");
477 (void)printf(" %u", EXTRACT_32BITS(cp
));
481 (void)printf("ccecho");
484 (void)printf(" %u", EXTRACT_32BITS(cp
));
488 (void)printf("opt-%d:", opt
);
490 for (i
= 0; i
< datalen
; ++i
) {
492 (void)printf("%02x", cp
[i
]);
497 /* Account for data printed */
501 /* Check specification against observed length */
502 ++datalen
; /* option octet */
503 if (!ZEROLENOPT(opt
))
504 ++datalen
; /* size octet */
506 (void)printf("[len %d]", len
);
508 if (opt
== TCPOPT_EOL
)
518 * Decode payload if necessary.
520 bp
+= (tp
->th_off
* 4);
521 if (!qflag
&& vflag
&& length
> 0
522 && (sport
== TELNET_PORT
|| dport
== TELNET_PORT
))
523 telnet_print(bp
, length
);
524 else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
525 bgp_print(bp
, length
);
526 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
527 nbt_tcp_print(bp
, length
);
530 fputs("[bad opt]", stdout
);
535 fputs("[|tcp]", stdout
);