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.58 1999-11-21 03:50:02 assar Exp $ (LBL)";
27 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
33 #include <netinet/ip_var.h>
34 #include <netinet/tcp.h>
45 #include <netinet/ip6.h>
48 #include "interface.h"
49 #include "addrtoname.h"
54 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
57 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
60 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
63 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
65 #ifndef TCPOPT_ECHOREPLY
66 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
68 #ifndef TCPOPT_TIMESTAMP
69 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
72 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
75 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
78 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
93 struct tcp_seq_hash
*nxt
;
99 #define TSEQ_HASHSIZE 919
101 /* These tcp optinos do not have the size octet */
102 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
104 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
108 tcp_print(register const u_char
*bp
, register u_int length
,
109 register const u_char
*bp2
)
111 register const struct tcphdr
*tp
;
112 register const struct ip
*ip
;
113 register u_char flags
;
116 u_short sport
, dport
, win
, urp
;
117 u_int32_t seq
, ack
, thseq
, thack
;
120 register const struct ip6_hdr
*ip6
;
123 tp
= (struct tcphdr
*)bp
;
124 ip
= (struct ip
*)bp2
;
127 ip6
= (struct ip6_hdr
*)bp2
;
133 if (length
< sizeof(*tp
)) {
134 (void)printf("truncated-tcp %d", length
);
138 sport
= ntohs(tp
->th_sport
);
139 dport
= ntohs(tp
->th_dport
);
140 seq
= ntohl(tp
->th_seq
);
141 ack
= ntohl(tp
->th_ack
);
142 win
= ntohs(tp
->th_win
);
143 urp
= ntohs(tp
->th_urp
);
147 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
148 (void)printf("%s.%s > %s.%s: ",
149 ip6addr_string(&ip6
->ip6_src
),
150 tcpport_string(sport
),
151 ip6addr_string(&ip6
->ip6_dst
),
152 tcpport_string(dport
));
154 (void)printf("%s > %s: ",
155 tcpport_string(sport
), tcpport_string(dport
));
160 if (ip
->ip_p
== IPPROTO_TCP
) {
161 (void)printf("%s.%s > %s.%s: ",
162 ipaddr_string(&ip
->ip_src
),
163 tcpport_string(sport
),
164 ipaddr_string(&ip
->ip_dst
),
165 tcpport_string(dport
));
167 (void)printf("%s > %s: ",
168 tcpport_string(sport
), tcpport_string(dport
));
173 (void)printf("tcp %d", length
- tp
->th_off
* 4);
177 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|TH_ECN
)) {
179 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
)) {
201 printf("%sCWR", (flags
&0x40) ? "," : "");
205 if (!Sflag
&& (flags
& TH_ACK
)) {
206 register struct tcp_seq_hash
*th
;
210 * Find (or record) the initial sequence numbers for
211 * this conversation. (we pick an arbitrary
212 * collating order so there's only one entry for
216 bzero(&tha
, sizeof(tha
));
221 } else if (sport
== dport
) {
224 for (i
= 0; i
< 4; i
++) {
225 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
226 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
233 tha
.src
= ip6
->ip6_dst
;
234 tha
.dst
= ip6
->ip6_src
;
235 tha
.port
= dport
<< 16 | sport
;
237 tha
.dst
= ip6
->ip6_dst
;
238 tha
.src
= ip6
->ip6_src
;
239 tha
.port
= sport
<< 16 | dport
;
244 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
248 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
249 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
250 tha
.port
= dport
<< 16 | sport
;
252 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
253 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
254 tha
.port
= sport
<< 16 | dport
;
260 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
261 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
262 tha
.port
= sport
<< 16 | dport
;
265 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
266 tha
.port
= dport
<< 16 | sport
;
272 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
273 th
->nxt
; th
= th
->nxt
)
274 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
278 if (!th
->nxt
|| flags
& TH_SYN
) {
279 /* didn't find it or new conversation */
280 if (th
->nxt
== NULL
) {
281 th
->nxt
= (struct tcp_seq_hash
*)
282 calloc(1, sizeof(*th
));
284 error("tcp_print: calloc");
288 th
->ack
= seq
, th
->seq
= ack
- 1;
290 th
->seq
= seq
, th
->ack
= ack
- 1;
297 seq
-= th
->ack
, ack
-= th
->seq
;
299 seq
-= th
->seq
, ack
-= th
->ack
;
302 hlen
= tp
->th_off
* 4;
304 (void)printf(" [bad hdr length]");
308 if (length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
309 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
311 (void)printf(" ack %u", ack
);
313 (void)printf(" win %d", win
);
316 (void)printf(" urg %d", urp
);
318 * Handle any options.
320 if ((hlen
-= sizeof(*tp
)) > 0) {
321 register const u_char
*cp
;
322 register int i
, opt
, len
, datalen
;
324 cp
= (const u_char
*)tp
+ sizeof(*tp
);
335 len
= *cp
++; /* total including type, len */
336 if (len
< 2 || len
> hlen
)
338 --hlen
; /* account for length byte */
340 --hlen
; /* account for type byte */
343 /* Bail if "l" bytes of data are not left or were not captured */
344 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
352 (void)printf(" %u", EXTRACT_16BITS(cp
));
365 (void)printf("wscale");
368 (void)printf(" %u", *cp
);
372 (void)printf("sackOK");
376 (void)printf("sack");
378 if (datalen
% 8 != 0) {
379 (void)printf(" malformed sack ");
383 (void)printf(" sack %d ", datalen
/ 8);
384 for (i
= 0; i
< datalen
; i
+= 8) {
386 s
= EXTRACT_32BITS(cp
+ i
);
388 e
= EXTRACT_32BITS(cp
+ i
+ 4);
396 (void)printf("{%u:%u}", s
, e
);
403 (void)printf("echo");
406 (void)printf(" %u", EXTRACT_32BITS(cp
));
409 case TCPOPT_ECHOREPLY
:
410 (void)printf("echoreply");
413 (void)printf(" %u", EXTRACT_32BITS(cp
));
416 case TCPOPT_TIMESTAMP
:
417 (void)printf("timestamp");
420 (void)printf(" %u", EXTRACT_32BITS(cp
));
422 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
429 (void)printf(" %u", EXTRACT_32BITS(cp
));
433 (void)printf("ccnew");
436 (void)printf(" %u", EXTRACT_32BITS(cp
));
440 (void)printf("ccecho");
443 (void)printf(" %u", EXTRACT_32BITS(cp
));
447 (void)printf("opt-%d:", opt
);
449 for (i
= 0; i
< datalen
; ++i
) {
451 (void)printf("%02x", cp
[i
]);
456 /* Account for data printed */
460 /* Check specification against observed length */
461 ++datalen
; /* option octet */
462 if (!ZEROLENOPT(opt
))
463 ++datalen
; /* size octet */
465 (void)printf("[len %d]", len
);
467 if (opt
== TCPOPT_EOL
)
477 * Decode payload if necessary.
479 bp
+= (tp
->th_off
* 4);
480 if (sport
== 179 || dport
== 179)
481 bgp_print(bp
, length
);
484 fputs("[bad opt]", stdout
);
489 fputs("[|tcp]", stdout
);