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