]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
add <ctype.h>
[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.70 2000-07-30 05:56:11 assar 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 <rpc/rpc.h>
35
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>
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <ctype.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 static void print_tcp_rst_data(register const u_char *sp, u_int length);
57
58 #define MAX_RST_DATA_LEN 30
59
60 /* Compatibility */
61 #ifndef TCPOPT_WSCALE
62 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
63 #endif
64 #ifndef TCPOPT_SACKOK
65 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
66 #endif
67 #ifndef TCPOPT_SACK
68 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
69 #endif
70 #ifndef TCPOPT_ECHO
71 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
72 #endif
73 #ifndef TCPOPT_ECHOREPLY
74 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
75 #endif
76 #ifndef TCPOPT_TIMESTAMP
77 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
78 #endif
79 #ifndef TCPOPT_CC
80 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
81 #endif
82 #ifndef TCPOPT_CCNEW
83 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
84 #endif
85 #ifndef TCPOPT_CCECHO
86 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
87 #endif
88
89 /*
90 * Definitions required for ECN
91 * for use if the OS running tcpdump does not have ECN
92 */
93 #ifndef TH_ECNECHO
94 #define TH_ECNECHO 0x40 /* ECN Echo in tcp header */
95 #endif
96 #ifndef TH_CWR
97 #define TH_CWR 0x80 /* ECN Cwnd Reduced in tcp header*/
98 #endif
99
100 struct tha {
101 #ifndef INET6
102 struct in_addr src;
103 struct in_addr dst;
104 #else
105 struct in6_addr src;
106 struct in6_addr dst;
107 #endif /*INET6*/
108 u_int port;
109 };
110
111 struct tcp_seq_hash {
112 struct tcp_seq_hash *nxt;
113 struct tha addr;
114 tcp_seq seq;
115 tcp_seq ack;
116 };
117
118 #define TSEQ_HASHSIZE 919
119
120 /* These tcp optinos do not have the size octet */
121 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
122
123 static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
124
125
126 #ifndef TELNET_PORT
127 #define TELNET_PORT 23
128 #endif
129 #ifndef BGP_PORT
130 #define BGP_PORT 179
131 #endif
132 #define NETBIOS_SSN_PORT 139
133 #ifndef NFS_PORT
134 #define NFS_PORT 2049
135 #endif
136
137 void
138 tcp_print(register const u_char *bp, register u_int length,
139 register const u_char *bp2)
140 {
141 register const struct tcphdr *tp;
142 register const struct ip *ip;
143 register u_char flags;
144 register int hlen;
145 register char ch;
146 u_int16_t sport, dport, win, urp;
147 u_int32_t seq, ack, thseq, thack;
148 int threv;
149 #ifdef INET6
150 register const struct ip6_hdr *ip6;
151 #endif
152
153 tp = (struct tcphdr *)bp;
154 ip = (struct ip *)bp2;
155 #ifdef INET6
156 if (ip->ip_v == 6)
157 ip6 = (struct ip6_hdr *)bp2;
158 else
159 ip6 = NULL;
160 #endif /*INET6*/
161 ch = '\0';
162 if (!TTEST(tp->th_dport)) {
163 (void)printf("%s > %s: [|tcp]",
164 ipaddr_string(&ip->ip_src),
165 ipaddr_string(&ip->ip_dst));
166 return;
167 }
168
169 sport = ntohs(tp->th_sport);
170 dport = ntohs(tp->th_dport);
171
172
173 hlen = tp->th_off * 4;
174
175 /*
176 * If data present and NFS port used, assume NFS.
177 * Pass offset of data plus 4 bytes for RPC TCP msg length
178 * to NFS print routines.
179 */
180 if (!qflag) {
181 if ((u_char *)tp + 4 + sizeof(struct rpc_msg) <= snapend &&
182 dport == NFS_PORT) {
183 nfsreq_print((u_char *)tp + hlen + 4, length-hlen,
184 (u_char *)ip);
185 return;
186 } else if ((u_char *)tp + 4 + sizeof(struct rpc_msg)
187 <= snapend &&
188 sport == NFS_PORT) {
189 nfsreply_print((u_char *)tp + hlen + 4,length-hlen,
190 (u_char *)ip);
191 return;
192 }
193 }
194 #ifdef INET6
195 if (ip6) {
196 if (ip6->ip6_nxt == IPPROTO_TCP) {
197 (void)printf("%s.%s > %s.%s: ",
198 ip6addr_string(&ip6->ip6_src),
199 tcpport_string(sport),
200 ip6addr_string(&ip6->ip6_dst),
201 tcpport_string(dport));
202 } else {
203 (void)printf("%s > %s: ",
204 tcpport_string(sport), tcpport_string(dport));
205 }
206 } else
207 #endif /*INET6*/
208 {
209 if (ip->ip_p == IPPROTO_TCP) {
210 (void)printf("%s.%s > %s.%s: ",
211 ipaddr_string(&ip->ip_src),
212 tcpport_string(sport),
213 ipaddr_string(&ip->ip_dst),
214 tcpport_string(dport));
215 } else {
216 (void)printf("%s > %s: ",
217 tcpport_string(sport), tcpport_string(dport));
218 }
219 }
220
221 TCHECK(*tp);
222
223 seq = (u_int32_t)ntohl(tp->th_seq);
224 ack = (u_int32_t)ntohl(tp->th_ack);
225 win = ntohs(tp->th_win);
226 urp = ntohs(tp->th_urp);
227
228 if (qflag) {
229 (void)printf("tcp %d", length - tp->th_off * 4);
230 return;
231 }
232 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|
233 TH_ECNECHO|TH_CWR)) {
234 if (flags & TH_SYN)
235 putchar('S');
236 if (flags & TH_FIN)
237 putchar('F');
238 if (flags & TH_RST)
239 putchar('R');
240 if (flags & TH_PUSH)
241 putchar('P');
242 if (flags & TH_CWR)
243 putchar('W'); /* congestion _W_indow reduced (ECN) */
244 if (flags & TH_ECNECHO)
245 putchar('E'); /* ecn _E_cho sent (ECN) */
246 } else
247 putchar('.');
248
249 if (!Sflag && (flags & TH_ACK)) {
250 register struct tcp_seq_hash *th;
251 register int rev;
252 struct tha tha;
253 /*
254 * Find (or record) the initial sequence numbers for
255 * this conversation. (we pick an arbitrary
256 * collating order so there's only one entry for
257 * both directions).
258 */
259 #ifdef INET6
260 memset(&tha, 0, sizeof(tha));
261 rev = 0;
262 if (ip6) {
263 if (sport > dport) {
264 rev = 1;
265 } else if (sport == dport) {
266 int i;
267
268 for (i = 0; i < 4; i++) {
269 if (((u_int32_t *)(&ip6->ip6_src))[i] >
270 ((u_int32_t *)(&ip6->ip6_dst))[i]) {
271 rev = 1;
272 break;
273 }
274 }
275 }
276 if (rev) {
277 tha.src = ip6->ip6_dst;
278 tha.dst = ip6->ip6_src;
279 tha.port = dport << 16 | sport;
280 } else {
281 tha.dst = ip6->ip6_dst;
282 tha.src = ip6->ip6_src;
283 tha.port = sport << 16 | dport;
284 }
285 } else {
286 if (sport > dport ||
287 (sport == dport &&
288 ip->ip_src.s_addr > ip->ip_dst.s_addr)) {
289 rev = 1;
290 }
291 if (rev) {
292 *(struct in_addr *)&tha.src = ip->ip_dst;
293 *(struct in_addr *)&tha.dst = ip->ip_src;
294 tha.port = dport << 16 | sport;
295 } else {
296 *(struct in_addr *)&tha.dst = ip->ip_dst;
297 *(struct in_addr *)&tha.src = ip->ip_src;
298 tha.port = sport << 16 | dport;
299 }
300 }
301 #else
302 if (sport < dport ||
303 (sport == dport &&
304 ip->ip_src.s_addr < ip->ip_dst.s_addr)) {
305 tha.src = ip->ip_src, tha.dst = ip->ip_dst;
306 tha.port = sport << 16 | dport;
307 rev = 0;
308 } else {
309 tha.src = ip->ip_dst, tha.dst = ip->ip_src;
310 tha.port = dport << 16 | sport;
311 rev = 1;
312 }
313 #endif
314
315 threv = rev;
316 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
317 th->nxt; th = th->nxt)
318 if (!memcmp((char *)&tha, (char *)&th->addr,
319 sizeof(th->addr)))
320 break;
321
322 if (!th->nxt || (flags & TH_SYN)) {
323 /* didn't find it or new conversation */
324 if (th->nxt == NULL) {
325 th->nxt = (struct tcp_seq_hash *)
326 calloc(1, sizeof(*th));
327 if (th->nxt == NULL)
328 error("tcp_print: calloc");
329 }
330 th->addr = tha;
331 if (rev)
332 th->ack = seq, th->seq = ack - 1;
333 else
334 th->seq = seq, th->ack = ack - 1;
335
336 } else {
337 if (rev)
338 seq -= th->ack, ack -= th->seq;
339 else
340 seq -= th->seq, ack -= th->ack;
341 }
342
343 thseq = th->seq;
344 thack = th->ack;
345 } else {
346 /*fool gcc*/
347 thseq = thack = threv = 0;
348 }
349 if (hlen > length) {
350 (void)printf(" [bad hdr length]");
351 return;
352 }
353 length -= hlen;
354 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
355 (void)printf(" %u:%u(%d)", seq, seq + length, length);
356 if (flags & TH_ACK)
357 (void)printf(" ack %u", ack);
358
359 (void)printf(" win %d", win);
360
361 if (flags & TH_URG)
362 (void)printf(" urg %d", urp);
363 /*
364 * Handle any options.
365 */
366 if ((hlen -= sizeof(*tp)) > 0) {
367 register const u_char *cp;
368 register int i, opt, len, datalen;
369
370 cp = (const u_char *)tp + sizeof(*tp);
371 putchar(' ');
372 ch = '<';
373 while (hlen > 0) {
374 putchar(ch);
375 TCHECK(*cp);
376 opt = *cp++;
377 if (ZEROLENOPT(opt))
378 len = 1;
379 else {
380 TCHECK(*cp);
381 len = *cp++; /* total including type, len */
382 if (len < 2 || len > hlen)
383 goto bad;
384 --hlen; /* account for length byte */
385 }
386 --hlen; /* account for type byte */
387 datalen = 0;
388
389 /* Bail if "l" bytes of data are not left or were not captured */
390 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
391
392 switch (opt) {
393
394 case TCPOPT_MAXSEG:
395 (void)printf("mss");
396 datalen = 2;
397 LENCHECK(datalen);
398 (void)printf(" %u", EXTRACT_16BITS(cp));
399
400 break;
401
402 case TCPOPT_EOL:
403 (void)printf("eol");
404 break;
405
406 case TCPOPT_NOP:
407 (void)printf("nop");
408 break;
409
410 case TCPOPT_WSCALE:
411 (void)printf("wscale");
412 datalen = 1;
413 LENCHECK(datalen);
414 (void)printf(" %u", *cp);
415 break;
416
417 case TCPOPT_SACKOK:
418 (void)printf("sackOK");
419 break;
420
421 case TCPOPT_SACK:
422 (void)printf("sack");
423 datalen = len - 2;
424 if (datalen % 8 != 0) {
425 (void)printf(" malformed sack ");
426 } else {
427 u_int32_t s, e;
428
429 (void)printf(" sack %d ", datalen / 8);
430 for (i = 0; i < datalen; i += 8) {
431 LENCHECK(i + 4);
432 s = EXTRACT_32BITS(cp + i);
433 LENCHECK(i + 8);
434 e = EXTRACT_32BITS(cp + i + 4);
435 if (threv) {
436 s -= thseq;
437 e -= thseq;
438 } else {
439 s -= thack;
440 e -= thack;
441 }
442 (void)printf("{%u:%u}", s, e);
443 }
444 (void)printf(" ");
445 }
446 break;
447
448 case TCPOPT_ECHO:
449 (void)printf("echo");
450 datalen = 4;
451 LENCHECK(datalen);
452 (void)printf(" %u", EXTRACT_32BITS(cp));
453 break;
454
455 case TCPOPT_ECHOREPLY:
456 (void)printf("echoreply");
457 datalen = 4;
458 LENCHECK(datalen);
459 (void)printf(" %u", EXTRACT_32BITS(cp));
460 break;
461
462 case TCPOPT_TIMESTAMP:
463 (void)printf("timestamp");
464 datalen = 8;
465 LENCHECK(4);
466 (void)printf(" %u", EXTRACT_32BITS(cp));
467 LENCHECK(datalen);
468 (void)printf(" %u", EXTRACT_32BITS(cp + 4));
469 break;
470
471 case TCPOPT_CC:
472 (void)printf("cc");
473 datalen = 4;
474 LENCHECK(datalen);
475 (void)printf(" %u", EXTRACT_32BITS(cp));
476 break;
477
478 case TCPOPT_CCNEW:
479 (void)printf("ccnew");
480 datalen = 4;
481 LENCHECK(datalen);
482 (void)printf(" %u", EXTRACT_32BITS(cp));
483 break;
484
485 case TCPOPT_CCECHO:
486 (void)printf("ccecho");
487 datalen = 4;
488 LENCHECK(datalen);
489 (void)printf(" %u", EXTRACT_32BITS(cp));
490 break;
491
492 default:
493 (void)printf("opt-%d:", opt);
494 datalen = len - 2;
495 for (i = 0; i < datalen; ++i) {
496 LENCHECK(i);
497 (void)printf("%02x", cp[i]);
498 }
499 break;
500 }
501
502 /* Account for data printed */
503 cp += datalen;
504 hlen -= datalen;
505
506 /* Check specification against observed length */
507 ++datalen; /* option octet */
508 if (!ZEROLENOPT(opt))
509 ++datalen; /* size octet */
510 if (datalen != len)
511 (void)printf("[len %d]", len);
512 ch = ',';
513 if (opt == TCPOPT_EOL)
514 break;
515 }
516 putchar('>');
517 }
518
519 if (length <= 0)
520 return;
521
522 /*
523 * Decode payload if necessary.
524 */
525 bp += (tp->th_off * 4);
526 if (flags & TH_RST) {
527 if (vflag)
528 print_tcp_rst_data(bp, length);
529 } else {
530 if (sport == TELNET_PORT || dport == TELNET_PORT) {
531 if (!qflag && vflag)
532 telnet_print(bp, length);
533 } else if (sport == BGP_PORT || dport == BGP_PORT)
534 bgp_print(bp, length);
535 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
536 nbt_tcp_print(bp, length);
537 }
538 return;
539 bad:
540 fputs("[bad opt]", stdout);
541 if (ch != '\0')
542 putchar('>');
543 return;
544 trunc:
545 fputs("[|tcp]", stdout);
546 if (ch != '\0')
547 putchar('>');
548 }
549
550 /*
551 * RFC1122 says the following on data in RST segments:
552 *
553 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
554 *
555 * A TCP SHOULD allow a received RST segment to include data.
556 *
557 * DISCUSSION
558 * It has been suggested that a RST segment could contain
559 * ASCII text that encoded and explained the cause of the
560 * RST. No standard has yet been established for such
561 * data.
562 *
563 */
564
565 static void
566 print_tcp_rst_data(register const u_char *sp, u_int length)
567 {
568 int c;
569
570 if (TTEST2(*sp, length))
571 printf(" [RST");
572 else
573 printf(" [!RST");
574 if (length > MAX_RST_DATA_LEN) {
575 length = MAX_RST_DATA_LEN; /* can use -X for longer */
576 putchar('+'); /* indicate we truncate */
577 }
578 putchar(' ');
579 while (length-- && sp <= snapend) {
580 c = *sp++;
581 if (isprint(c))
582 putchar(c);
583 else
584 putchar('.');
585 }
586 putchar(']');
587 }