]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
-Wall -Werror clean.
[tcpdump] / print-tcp.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.64 2000-04-27 11:09:08 itojun Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33
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>
39
40 #ifdef HAVE_MEMORY_H
41 #include <memory.h>
42 #endif
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #ifdef INET6
49 #include <netinet/ip6.h>
50 #endif
51
52 #include "interface.h"
53 #include "addrtoname.h"
54 #include "extract.h"
55
56 /* Compatibility */
57 #ifndef TCPOPT_WSCALE
58 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
59 #endif
60 #ifndef TCPOPT_SACKOK
61 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
62 #endif
63 #ifndef TCPOPT_SACK
64 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
65 #endif
66 #ifndef TCPOPT_ECHO
67 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
68 #endif
69 #ifndef TCPOPT_ECHOREPLY
70 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
71 #endif
72 #ifndef TCPOPT_TIMESTAMP
73 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
74 #endif
75 #ifndef TCPOPT_CC
76 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
77 #endif
78 #ifndef TCPOPT_CCNEW
79 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
80 #endif
81 #ifndef TCPOPT_CCECHO
82 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
83 #endif
84
85 /*
86 * Definitions required for ECN
87 * for use if the OS running tcpdump does not have ECN
88 */
89 #ifndef TH_ECNECHO
90 #define TH_ECNECHO 0x40 /* ECN Echo in tcp header */
91 #endif
92 #ifndef TH_CWR
93 #define TH_CWR 0x80 /* ECN Cwnd Reduced in tcp header*/
94 #endif
95
96 struct tha {
97 #ifndef INET6
98 struct in_addr src;
99 struct in_addr dst;
100 #else
101 struct in6_addr src;
102 struct in6_addr dst;
103 #endif /*INET6*/
104 u_int port;
105 };
106
107 struct tcp_seq_hash {
108 struct tcp_seq_hash *nxt;
109 struct tha addr;
110 tcp_seq seq;
111 tcp_seq ack;
112 };
113
114 #define TSEQ_HASHSIZE 919
115
116 /* These tcp optinos do not have the size octet */
117 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
118
119 static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
120
121
122 #ifndef TELNET_PORT
123 #define TELNET_PORT 23
124 #endif
125 #ifndef BGP_PORT
126 #define BGP_PORT 179
127 #endif
128 #define NETBIOS_SSN_PORT 139
129
130 void
131 tcp_print(register const u_char *bp, register u_int length,
132 register const u_char *bp2)
133 {
134 register const struct tcphdr *tp;
135 register const struct ip *ip;
136 register u_char flags;
137 register int hlen;
138 register char ch;
139 u_int16_t sport, dport, win, urp;
140 u_int32_t seq, ack, thseq, thack;
141 int threv;
142 #ifdef INET6
143 register const struct ip6_hdr *ip6;
144 #endif
145
146 tp = (struct tcphdr *)bp;
147 ip = (struct ip *)bp2;
148 #ifdef INET6
149 if (ip->ip_v == 6)
150 ip6 = (struct ip6_hdr *)bp2;
151 else
152 ip6 = NULL;
153 #endif /*INET6*/
154 ch = '\0';
155 if (!TTEST(tp->th_dport)) {
156 (void)printf("%s > %s: [|tcp]",
157 ipaddr_string(&ip->ip_src),
158 ipaddr_string(&ip->ip_dst));
159 return;
160 }
161
162 sport = ntohs(tp->th_sport);
163 dport = ntohs(tp->th_dport);
164
165 #ifdef INET6
166 if (ip6) {
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));
173 } else {
174 (void)printf("%s > %s: ",
175 tcpport_string(sport), tcpport_string(dport));
176 }
177 } else
178 #endif /*INET6*/
179 {
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));
186 } else {
187 (void)printf("%s > %s: ",
188 tcpport_string(sport), tcpport_string(dport));
189 }
190 }
191
192 TCHECK(*tp);
193
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);
198
199 if (qflag) {
200 (void)printf("tcp %d", length - tp->th_off * 4);
201 return;
202 }
203 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|
204 TH_ECNECHO|TH_CWR)) {
205 if (flags & TH_SYN)
206 putchar('S');
207 if (flags & TH_FIN)
208 putchar('F');
209 if (flags & TH_RST)
210 putchar('R');
211 if (flags & TH_PUSH)
212 putchar('P');
213 if (flags & TH_CWR)
214 putchar('W'); /* congestion _W_indow reduced (ECN) */
215 if (flags & TH_ECNECHO)
216 putchar('E'); /* ecn _E_cho sent (ECN) */
217 } else
218 putchar('.');
219
220 if (!Sflag && (flags & TH_ACK)) {
221 register struct tcp_seq_hash *th;
222 register int rev;
223 struct tha tha;
224 /*
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
228 * both directions).
229 */
230 #ifdef INET6
231 bzero(&tha, sizeof(tha));
232 rev = 0;
233 if (ip6) {
234 if (sport > dport) {
235 rev = 1;
236 } else if (sport == dport) {
237 int i;
238
239 for (i = 0; i < 4; i++) {
240 if (((u_int32_t *)(&ip6->ip6_src))[i] >
241 ((u_int32_t *)(&ip6->ip6_dst))[i]) {
242 rev = 1;
243 break;
244 }
245 }
246 }
247 if (rev) {
248 tha.src = ip6->ip6_dst;
249 tha.dst = ip6->ip6_src;
250 tha.port = dport << 16 | sport;
251 } else {
252 tha.dst = ip6->ip6_dst;
253 tha.src = ip6->ip6_src;
254 tha.port = sport << 16 | dport;
255 }
256 } else {
257 if (sport > dport ||
258 (sport == dport &&
259 ip->ip_src.s_addr > ip->ip_dst.s_addr)) {
260 rev = 1;
261 }
262 if (rev) {
263 *(struct in_addr *)&tha.src = ip->ip_dst;
264 *(struct in_addr *)&tha.dst = ip->ip_src;
265 tha.port = dport << 16 | sport;
266 } else {
267 *(struct in_addr *)&tha.dst = ip->ip_dst;
268 *(struct in_addr *)&tha.src = ip->ip_src;
269 tha.port = sport << 16 | dport;
270 }
271 }
272 #else
273 if (sport < dport ||
274 (sport == 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;
278 rev = 0;
279 } else {
280 tha.src = ip->ip_dst, tha.dst = ip->ip_src;
281 tha.port = dport << 16 | sport;
282 rev = 1;
283 }
284 #endif
285
286 threv = rev;
287 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
288 th->nxt; th = th->nxt)
289 if (!memcmp((char *)&tha, (char *)&th->addr,
290 sizeof(th->addr)))
291 break;
292
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));
298 if (th->nxt == NULL)
299 error("tcp_print: calloc");
300 }
301 th->addr = tha;
302 if (rev)
303 th->ack = seq, th->seq = ack - 1;
304 else
305 th->seq = seq, th->ack = ack - 1;
306 } else {
307
308 thseq = th->seq;
309 thack = th->ack;
310
311 if (rev)
312 seq -= th->ack, ack -= th->seq;
313 else
314 seq -= th->seq, ack -= th->ack;
315 }
316 }
317 hlen = tp->th_off * 4;
318 if (hlen > length) {
319 (void)printf(" [bad hdr length]");
320 return;
321 }
322 length -= hlen;
323 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
324 (void)printf(" %u:%u(%d)", seq, seq + length, length);
325 if (flags & TH_ACK)
326 (void)printf(" ack %u", ack);
327
328 (void)printf(" win %d", win);
329
330 if (flags & TH_URG)
331 (void)printf(" urg %d", urp);
332 /*
333 * Handle any options.
334 */
335 if ((hlen -= sizeof(*tp)) > 0) {
336 register const u_char *cp;
337 register int i, opt, len, datalen;
338
339 cp = (const u_char *)tp + sizeof(*tp);
340 putchar(' ');
341 ch = '<';
342 while (hlen > 0) {
343 putchar(ch);
344 TCHECK(*cp);
345 opt = *cp++;
346 if (ZEROLENOPT(opt))
347 len = 1;
348 else {
349 TCHECK(*cp);
350 len = *cp++; /* total including type, len */
351 if (len < 2 || len > hlen)
352 goto bad;
353 --hlen; /* account for length byte */
354 }
355 --hlen; /* account for type byte */
356 datalen = 0;
357
358 /* Bail if "l" bytes of data are not left or were not captured */
359 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
360
361 switch (opt) {
362
363 case TCPOPT_MAXSEG:
364 (void)printf("mss");
365 datalen = 2;
366 LENCHECK(datalen);
367 (void)printf(" %u", EXTRACT_16BITS(cp));
368
369 break;
370
371 case TCPOPT_EOL:
372 (void)printf("eol");
373 break;
374
375 case TCPOPT_NOP:
376 (void)printf("nop");
377 break;
378
379 case TCPOPT_WSCALE:
380 (void)printf("wscale");
381 datalen = 1;
382 LENCHECK(datalen);
383 (void)printf(" %u", *cp);
384 break;
385
386 case TCPOPT_SACKOK:
387 (void)printf("sackOK");
388 break;
389
390 case TCPOPT_SACK:
391 (void)printf("sack");
392 datalen = len - 2;
393 if (datalen % 8 != 0) {
394 (void)printf(" malformed sack ");
395 } else {
396 u_int32_t s, e;
397
398 (void)printf(" sack %d ", datalen / 8);
399 for (i = 0; i < datalen; i += 8) {
400 LENCHECK(i + 4);
401 s = EXTRACT_32BITS(cp + i);
402 LENCHECK(i + 8);
403 e = EXTRACT_32BITS(cp + i + 4);
404 if (threv) {
405 s -= thseq;
406 e -= thseq;
407 } else {
408 s -= thack;
409 e -= thack;
410 }
411 (void)printf("{%u:%u}", s, e);
412 }
413 (void)printf(" ");
414 }
415 break;
416
417 case TCPOPT_ECHO:
418 (void)printf("echo");
419 datalen = 4;
420 LENCHECK(datalen);
421 (void)printf(" %u", EXTRACT_32BITS(cp));
422 break;
423
424 case TCPOPT_ECHOREPLY:
425 (void)printf("echoreply");
426 datalen = 4;
427 LENCHECK(datalen);
428 (void)printf(" %u", EXTRACT_32BITS(cp));
429 break;
430
431 case TCPOPT_TIMESTAMP:
432 (void)printf("timestamp");
433 datalen = 8;
434 LENCHECK(4);
435 (void)printf(" %u", EXTRACT_32BITS(cp));
436 LENCHECK(datalen);
437 (void)printf(" %u", EXTRACT_32BITS(cp + 4));
438 break;
439
440 case TCPOPT_CC:
441 (void)printf("cc");
442 datalen = 4;
443 LENCHECK(datalen);
444 (void)printf(" %u", EXTRACT_32BITS(cp));
445 break;
446
447 case TCPOPT_CCNEW:
448 (void)printf("ccnew");
449 datalen = 4;
450 LENCHECK(datalen);
451 (void)printf(" %u", EXTRACT_32BITS(cp));
452 break;
453
454 case TCPOPT_CCECHO:
455 (void)printf("ccecho");
456 datalen = 4;
457 LENCHECK(datalen);
458 (void)printf(" %u", EXTRACT_32BITS(cp));
459 break;
460
461 default:
462 (void)printf("opt-%d:", opt);
463 datalen = len - 2;
464 for (i = 0; i < datalen; ++i) {
465 LENCHECK(i);
466 (void)printf("%02x", cp[i]);
467 }
468 break;
469 }
470
471 /* Account for data printed */
472 cp += datalen;
473 hlen -= datalen;
474
475 /* Check specification against observed length */
476 ++datalen; /* option octet */
477 if (!ZEROLENOPT(opt))
478 ++datalen; /* size octet */
479 if (datalen != len)
480 (void)printf("[len %d]", len);
481 ch = ',';
482 if (opt == TCPOPT_EOL)
483 break;
484 }
485 putchar('>');
486 }
487
488 if (length <= 0)
489 return;
490
491 /*
492 * Decode payload if necessary.
493 */
494 bp += (tp->th_off * 4);
495 if (!qflag && vflag && length > 0
496 && (sport == TELNET_PORT || dport == TELNET_PORT))
497 telnet_print(bp, length);
498 else if (sport == BGP_PORT || dport == BGP_PORT)
499 bgp_print(bp, length);
500 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
501 nbt_tcp_print(bp, length);
502 return;
503 bad:
504 fputs("[bad opt]", stdout);
505 if (ch != '\0')
506 putchar('>');
507 return;
508 trunc:
509 fputs("[|tcp]", stdout);
510 if (ch != '\0')
511 putchar('>');
512 }
513