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.61 1999-11-22 04:27:10 fenner 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) */
97 struct tcp_seq_hash
*nxt
;
103 #define TSEQ_HASHSIZE 919
105 /* These tcp optinos do not have the size octet */
106 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
108 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
111 #define NETBIOS_SSN_PORT 139
114 tcp_print(register const u_char
*bp
, register u_int length
,
115 register const u_char
*bp2
)
117 register const struct tcphdr
*tp
;
118 register const struct ip
*ip
;
119 register u_char flags
;
122 u_short sport
, dport
, win
, urp
;
123 u_int32_t seq
, ack
, thseq
, thack
;
126 register const struct ip6_hdr
*ip6
;
129 tp
= (struct tcphdr
*)bp
;
130 ip
= (struct ip
*)bp2
;
133 ip6
= (struct ip6_hdr
*)bp2
;
138 if (!TTEST(tp
->th_dport
)) {
139 (void)printf("%s > %s: [|tcp]",
140 ipaddr_string(&ip
->ip_src
),
141 ipaddr_string(&ip
->ip_dst
));
145 sport
= ntohs(tp
->th_sport
);
146 dport
= ntohs(tp
->th_dport
);
150 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
151 (void)printf("%s.%s > %s.%s: ",
152 ip6addr_string(&ip6
->ip6_src
),
153 tcpport_string(sport
),
154 ip6addr_string(&ip6
->ip6_dst
),
155 tcpport_string(dport
));
157 (void)printf("%s > %s: ",
158 tcpport_string(sport
), tcpport_string(dport
));
163 if (ip
->ip_p
== IPPROTO_TCP
) {
164 (void)printf("%s.%s > %s.%s: ",
165 ipaddr_string(&ip
->ip_src
),
166 tcpport_string(sport
),
167 ipaddr_string(&ip
->ip_dst
),
168 tcpport_string(dport
));
170 (void)printf("%s > %s: ",
171 tcpport_string(sport
), tcpport_string(dport
));
177 seq
= ntohl(tp
->th_seq
);
178 ack
= ntohl(tp
->th_ack
);
179 win
= ntohs(tp
->th_win
);
180 urp
= ntohs(tp
->th_urp
);
183 (void)printf("tcp %d", length
- tp
->th_off
* 4);
187 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|TH_ECN
)) {
189 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
)) {
211 printf("%sCWR", (flags
&0x40) ? "," : "");
215 if (!Sflag
&& (flags
& TH_ACK
)) {
216 register struct tcp_seq_hash
*th
;
220 * Find (or record) the initial sequence numbers for
221 * this conversation. (we pick an arbitrary
222 * collating order so there's only one entry for
226 bzero(&tha
, sizeof(tha
));
231 } else if (sport
== dport
) {
234 for (i
= 0; i
< 4; i
++) {
235 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
236 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
243 tha
.src
= ip6
->ip6_dst
;
244 tha
.dst
= ip6
->ip6_src
;
245 tha
.port
= dport
<< 16 | sport
;
247 tha
.dst
= ip6
->ip6_dst
;
248 tha
.src
= ip6
->ip6_src
;
249 tha
.port
= sport
<< 16 | dport
;
254 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
258 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
259 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
260 tha
.port
= dport
<< 16 | sport
;
262 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
263 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
264 tha
.port
= sport
<< 16 | dport
;
270 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
271 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
272 tha
.port
= sport
<< 16 | dport
;
275 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
276 tha
.port
= dport
<< 16 | sport
;
282 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
283 th
->nxt
; th
= th
->nxt
)
284 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
288 if (!th
->nxt
|| flags
& TH_SYN
) {
289 /* didn't find it or new conversation */
290 if (th
->nxt
== NULL
) {
291 th
->nxt
= (struct tcp_seq_hash
*)
292 calloc(1, sizeof(*th
));
294 error("tcp_print: calloc");
298 th
->ack
= seq
, th
->seq
= ack
- 1;
300 th
->seq
= seq
, th
->ack
= ack
- 1;
307 seq
-= th
->ack
, ack
-= th
->seq
;
309 seq
-= th
->seq
, ack
-= th
->ack
;
312 hlen
= tp
->th_off
* 4;
314 (void)printf(" [bad hdr length]");
318 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
319 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
321 (void)printf(" ack %u", ack
);
323 (void)printf(" win %d", win
);
326 (void)printf(" urg %d", urp
);
328 * Handle any options.
330 if ((hlen
-= sizeof(*tp
)) > 0) {
331 register const u_char
*cp
;
332 register int i
, opt
, len
, datalen
;
334 cp
= (const u_char
*)tp
+ sizeof(*tp
);
345 len
= *cp
++; /* total including type, len */
346 if (len
< 2 || len
> hlen
)
348 --hlen
; /* account for length byte */
350 --hlen
; /* account for type byte */
353 /* Bail if "l" bytes of data are not left or were not captured */
354 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
362 (void)printf(" %u", EXTRACT_16BITS(cp
));
375 (void)printf("wscale");
378 (void)printf(" %u", *cp
);
382 (void)printf("sackOK");
386 (void)printf("sack");
388 if (datalen
% 8 != 0) {
389 (void)printf(" malformed sack ");
393 (void)printf(" sack %d ", datalen
/ 8);
394 for (i
= 0; i
< datalen
; i
+= 8) {
396 s
= EXTRACT_32BITS(cp
+ i
);
398 e
= EXTRACT_32BITS(cp
+ i
+ 4);
406 (void)printf("{%u:%u}", s
, e
);
413 (void)printf("echo");
416 (void)printf(" %u", EXTRACT_32BITS(cp
));
419 case TCPOPT_ECHOREPLY
:
420 (void)printf("echoreply");
423 (void)printf(" %u", EXTRACT_32BITS(cp
));
426 case TCPOPT_TIMESTAMP
:
427 (void)printf("timestamp");
430 (void)printf(" %u", EXTRACT_32BITS(cp
));
432 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
439 (void)printf(" %u", EXTRACT_32BITS(cp
));
443 (void)printf("ccnew");
446 (void)printf(" %u", EXTRACT_32BITS(cp
));
450 (void)printf("ccecho");
453 (void)printf(" %u", EXTRACT_32BITS(cp
));
457 (void)printf("opt-%d:", opt
);
459 for (i
= 0; i
< datalen
; ++i
) {
461 (void)printf("%02x", cp
[i
]);
466 /* Account for data printed */
470 /* Check specification against observed length */
471 ++datalen
; /* option octet */
472 if (!ZEROLENOPT(opt
))
473 ++datalen
; /* size octet */
475 (void)printf("[len %d]", len
);
477 if (opt
== TCPOPT_EOL
)
487 * Decode payload if necessary.
489 bp
+= (tp
->th_off
* 4);
490 if (sport
== 179 || dport
== 179)
491 bgp_print(bp
, length
);
492 if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
493 nbt_tcp_print(bp
, length
);
496 fputs("[bad opt]", stdout
);
501 fputs("[|tcp]", stdout
);