]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
12d24b5ca3a3552e002e4113f384d213bee72f0e
[tcpdump] / print-tcp.c
1 /* $NetBSD: print-tcp.c,v 1.9 2007/07/26 18:15:12 plunky Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Copyright (c) 1999-2004 The tcpdump.org project
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that: (1) source code distributions
11 * retain the above copyright notice and this paragraph in its entirety, (2)
12 * distributions including binary code include the above copyright notice and
13 * this paragraph in its entirety in the documentation or other materials
14 * provided with the distribution, and (3) all advertising materials mentioning
15 * features or use of this software display the following acknowledgement:
16 * ``This product includes software developed by the University of California,
17 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
18 * the University nor the names of its contributors may be used to endorse
19 * or promote products derived from this software without specific prior
20 * written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 */
25
26 /* \summary: TCP printer */
27
28 #ifndef lint
29 #else
30 __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
31 #endif
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include "netdissect-stdinc.h"
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 #define ND_LONGJMP_FROM_TCHECK
43 #include "netdissect.h"
44 #include "addrtoname.h"
45 #include "extract.h"
46
47 #include "tcp.h"
48
49 #include "ip.h"
50 #include "ip6.h"
51 #include "ipproto.h"
52 #include "rpc_auth.h"
53 #include "rpc_msg.h"
54
55 #ifdef HAVE_LIBCRYPTO
56 #include <openssl/md5.h>
57 #include "signature.h"
58
59 static int tcp_verify_signature(netdissect_options *ndo,
60 const struct ip *ip, const struct tcphdr *tp,
61 const u_char *data, u_int length, const u_char *rcvsig);
62 #endif
63
64 static void print_tcp_rst_data(netdissect_options *, const u_char *sp, u_int length);
65 static void print_tcp_fastopen_option(netdissect_options *ndo, const u_char *cp,
66 u_int datalen);
67
68 #define MAX_RST_DATA_LEN 30
69
70
71 struct tha {
72 nd_ipv4 src;
73 nd_ipv4 dst;
74 u_int port;
75 };
76
77 struct tcp_seq_hash {
78 struct tcp_seq_hash *nxt;
79 struct tha addr;
80 uint32_t seq;
81 uint32_t ack;
82 };
83
84 struct tha6 {
85 nd_ipv6 src;
86 nd_ipv6 dst;
87 u_int port;
88 };
89
90 struct tcp_seq_hash6 {
91 struct tcp_seq_hash6 *nxt;
92 struct tha6 addr;
93 uint32_t seq;
94 uint32_t ack;
95 };
96
97 #define TSEQ_HASHSIZE 919
98
99 /* These tcp options do not have the size octet */
100 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
101
102 static struct tcp_seq_hash tcp_seq_hash4[TSEQ_HASHSIZE];
103 static struct tcp_seq_hash6 tcp_seq_hash6[TSEQ_HASHSIZE];
104
105 const struct tok tcp_flag_values[] = {
106 { TH_FIN, "F" },
107 { TH_SYN, "S" },
108 { TH_RST, "R" },
109 { TH_PUSH, "P" },
110 { TH_ACK, "." },
111 { TH_URG, "U" },
112 { TH_ECNECHO, "E" },
113 { TH_CWR, "W" },
114 { 0, NULL }
115 };
116
117 static const struct tok tcp_option_values[] = {
118 { TCPOPT_EOL, "eol" },
119 { TCPOPT_NOP, "nop" },
120 { TCPOPT_MAXSEG, "mss" },
121 { TCPOPT_WSCALE, "wscale" },
122 { TCPOPT_SACKOK, "sackOK" },
123 { TCPOPT_SACK, "sack" },
124 { TCPOPT_ECHO, "echo" },
125 { TCPOPT_ECHOREPLY, "echoreply" },
126 { TCPOPT_TIMESTAMP, "TS" },
127 { TCPOPT_CC, "cc" },
128 { TCPOPT_CCNEW, "ccnew" },
129 { TCPOPT_CCECHO, "" },
130 { TCPOPT_SIGNATURE, "md5" },
131 { TCPOPT_SCPS, "scps" },
132 { TCPOPT_UTO, "uto" },
133 { TCPOPT_TCPAO, "tcp-ao" },
134 { TCPOPT_MPTCP, "mptcp" },
135 { TCPOPT_FASTOPEN, "tfo" },
136 { TCPOPT_EXPERIMENT2, "exp" },
137 { 0, NULL }
138 };
139
140 static uint16_t
141 tcp_cksum(netdissect_options *ndo,
142 const struct ip *ip,
143 const struct tcphdr *tp,
144 u_int len)
145 {
146 return nextproto4_cksum(ndo, ip, (const uint8_t *)tp, len, len,
147 IPPROTO_TCP);
148 }
149
150 static uint16_t
151 tcp6_cksum(netdissect_options *ndo,
152 const struct ip6_hdr *ip6,
153 const struct tcphdr *tp,
154 u_int len)
155 {
156 return nextproto6_cksum(ndo, ip6, (const uint8_t *)tp, len, len,
157 IPPROTO_TCP);
158 }
159
160 void
161 tcp_print(netdissect_options *ndo,
162 const u_char *bp, u_int length,
163 const u_char *bp2, int fragmented)
164 {
165 const struct tcphdr *tp;
166 const struct ip *ip;
167 u_char flags;
168 u_int hlen;
169 char ch;
170 uint16_t sport, dport, win, urp;
171 uint32_t seq, ack, thseq, thack;
172 u_int utoval;
173 uint16_t magic;
174 int rev;
175 const struct ip6_hdr *ip6;
176 u_int header_len; /* Header length in bytes */
177
178 ndo->ndo_protocol = "tcp";
179 tp = (const struct tcphdr *)bp;
180 ip = (const struct ip *)bp2;
181 if (IP_V(ip) == 6)
182 ip6 = (const struct ip6_hdr *)bp2;
183 else
184 ip6 = NULL;
185 ch = '\0';
186 if (!ND_TTEST_2(tp->th_dport)) {
187 if (ip6) {
188 ND_PRINT("%s > %s:",
189 GET_IP6ADDR_STRING(ip6->ip6_src),
190 GET_IP6ADDR_STRING(ip6->ip6_dst));
191 } else {
192 ND_PRINT("%s > %s:",
193 GET_IPADDR_STRING(ip->ip_src),
194 GET_IPADDR_STRING(ip->ip_dst));
195 }
196 nd_trunc_longjmp(ndo);
197 }
198
199 sport = GET_BE_U_2(tp->th_sport);
200 dport = GET_BE_U_2(tp->th_dport);
201
202 if (ip6) {
203 if (GET_U_1(ip6->ip6_nxt) == IPPROTO_TCP) {
204 ND_PRINT("%s.%s > %s.%s: ",
205 GET_IP6ADDR_STRING(ip6->ip6_src),
206 tcpport_string(ndo, sport),
207 GET_IP6ADDR_STRING(ip6->ip6_dst),
208 tcpport_string(ndo, dport));
209 } else {
210 ND_PRINT("%s > %s: ",
211 tcpport_string(ndo, sport), tcpport_string(ndo, dport));
212 }
213 } else {
214 if (GET_U_1(ip->ip_p) == IPPROTO_TCP) {
215 ND_PRINT("%s.%s > %s.%s: ",
216 GET_IPADDR_STRING(ip->ip_src),
217 tcpport_string(ndo, sport),
218 GET_IPADDR_STRING(ip->ip_dst),
219 tcpport_string(ndo, dport));
220 } else {
221 ND_PRINT("%s > %s: ",
222 tcpport_string(ndo, sport), tcpport_string(ndo, dport));
223 }
224 }
225
226 hlen = TH_OFF(tp) * 4;
227
228 if (hlen < sizeof(*tp)) {
229 ND_PRINT(" tcp %u [bad hdr length %u - too short, < %zu]",
230 length - hlen, hlen, sizeof(*tp));
231 goto invalid;
232 }
233
234 seq = GET_BE_U_4(tp->th_seq);
235 ack = GET_BE_U_4(tp->th_ack);
236 win = GET_BE_U_2(tp->th_win);
237 urp = GET_BE_U_2(tp->th_urp);
238
239 if (ndo->ndo_qflag) {
240 ND_PRINT("tcp %u", length - hlen);
241 if (hlen > length) {
242 ND_PRINT(" [bad hdr length %u - too long, > %u]",
243 hlen, length);
244 goto invalid;
245 }
246 return;
247 }
248
249 flags = GET_U_1(tp->th_flags);
250 ND_PRINT("Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags));
251
252 if (!ndo->ndo_Sflag && (flags & TH_ACK)) {
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 rev = 0;
260 if (ip6) {
261 struct tcp_seq_hash6 *th;
262 struct tcp_seq_hash6 *tcp_seq_hash;
263 const void *src, *dst;
264 struct tha6 tha;
265
266 tcp_seq_hash = tcp_seq_hash6;
267 src = (const void *)ip6->ip6_src;
268 dst = (const void *)ip6->ip6_dst;
269 if (sport > dport)
270 rev = 1;
271 else if (sport == dport) {
272 if (UNALIGNED_MEMCMP(src, dst, sizeof(ip6->ip6_dst)) > 0)
273 rev = 1;
274 }
275 if (rev) {
276 UNALIGNED_MEMCPY(&tha.src, dst, sizeof(ip6->ip6_dst));
277 UNALIGNED_MEMCPY(&tha.dst, src, sizeof(ip6->ip6_src));
278 tha.port = ((u_int)dport) << 16 | sport;
279 } else {
280 UNALIGNED_MEMCPY(&tha.dst, dst, sizeof(ip6->ip6_dst));
281 UNALIGNED_MEMCPY(&tha.src, src, sizeof(ip6->ip6_src));
282 tha.port = ((u_int)sport) << 16 | dport;
283 }
284
285 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
286 th->nxt; th = th->nxt)
287 if (memcmp((char *)&tha, (char *)&th->addr,
288 sizeof(th->addr)) == 0)
289 break;
290
291 if (!th->nxt || (flags & TH_SYN)) {
292 /* didn't find it or new conversation */
293 /* calloc() return used by the 'tcp_seq_hash6'
294 hash table: do not free() */
295 if (th->nxt == NULL) {
296 th->nxt = (struct tcp_seq_hash6 *)
297 calloc(1, sizeof(*th));
298 if (th->nxt == NULL)
299 (*ndo->ndo_error)(ndo,
300 S_ERR_ND_MEM_ALLOC,
301 "%s: calloc", __func__);
302 }
303 th->addr = tha;
304 if (rev)
305 th->ack = seq, th->seq = ack - 1;
306 else
307 th->seq = seq, th->ack = ack - 1;
308 } else {
309 if (rev)
310 seq -= th->ack, ack -= th->seq;
311 else
312 seq -= th->seq, ack -= th->ack;
313 }
314
315 thseq = th->seq;
316 thack = th->ack;
317 } else {
318 struct tcp_seq_hash *th;
319 struct tcp_seq_hash *tcp_seq_hash;
320 struct tha tha;
321
322 tcp_seq_hash = tcp_seq_hash4;
323 if (sport > dport)
324 rev = 1;
325 else if (sport == dport) {
326 if (UNALIGNED_MEMCMP(ip->ip_src, ip->ip_dst, sizeof(ip->ip_dst)) > 0)
327 rev = 1;
328 }
329 if (rev) {
330 UNALIGNED_MEMCPY(&tha.src, ip->ip_dst,
331 sizeof(ip->ip_dst));
332 UNALIGNED_MEMCPY(&tha.dst, ip->ip_src,
333 sizeof(ip->ip_src));
334 tha.port = ((u_int)dport) << 16 | sport;
335 } else {
336 UNALIGNED_MEMCPY(&tha.dst, ip->ip_dst,
337 sizeof(ip->ip_dst));
338 UNALIGNED_MEMCPY(&tha.src, ip->ip_src,
339 sizeof(ip->ip_src));
340 tha.port = ((u_int)sport) << 16 | dport;
341 }
342
343 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
344 th->nxt; th = th->nxt)
345 if (memcmp((char *)&tha, (char *)&th->addr,
346 sizeof(th->addr)) == 0)
347 break;
348
349 if (!th->nxt || (flags & TH_SYN)) {
350 /* didn't find it or new conversation */
351 /* calloc() return used by the 'tcp_seq_hash4'
352 hash table: do not free() */
353 if (th->nxt == NULL) {
354 th->nxt = (struct tcp_seq_hash *)
355 calloc(1, sizeof(*th));
356 if (th->nxt == NULL)
357 (*ndo->ndo_error)(ndo,
358 S_ERR_ND_MEM_ALLOC,
359 "%s: calloc", __func__);
360 }
361 th->addr = tha;
362 if (rev)
363 th->ack = seq, th->seq = ack - 1;
364 else
365 th->seq = seq, th->ack = ack - 1;
366 } else {
367 if (rev)
368 seq -= th->ack, ack -= th->seq;
369 else
370 seq -= th->seq, ack -= th->ack;
371 }
372
373 thseq = th->seq;
374 thack = th->ack;
375 }
376 } else {
377 /*fool gcc*/
378 thseq = thack = rev = 0;
379 }
380 if (hlen > length) {
381 ND_PRINT(" [bad hdr length %u - too long, > %u]",
382 hlen, length);
383 goto invalid;
384 }
385
386 if (ndo->ndo_vflag && !ndo->ndo_Kflag && !fragmented) {
387 /* Check the checksum, if possible. */
388 uint16_t sum, tcp_sum;
389
390 if (IP_V(ip) == 4) {
391 if (ND_TTEST_LEN(tp->th_sport, length)) {
392 sum = tcp_cksum(ndo, ip, tp, length);
393 tcp_sum = GET_BE_U_2(tp->th_sum);
394
395 ND_PRINT(", cksum 0x%04x", tcp_sum);
396 if (sum != 0)
397 ND_PRINT(" (incorrect -> 0x%04x)",
398 in_cksum_shouldbe(tcp_sum, sum));
399 else
400 ND_PRINT(" (correct)");
401 }
402 } else if (IP_V(ip) == 6) {
403 if (ND_TTEST_LEN(tp->th_sport, length)) {
404 sum = tcp6_cksum(ndo, ip6, tp, length);
405 tcp_sum = GET_BE_U_2(tp->th_sum);
406
407 ND_PRINT(", cksum 0x%04x", tcp_sum);
408 if (sum != 0)
409 ND_PRINT(" (incorrect -> 0x%04x)",
410 in_cksum_shouldbe(tcp_sum, sum));
411 else
412 ND_PRINT(" (correct)");
413
414 }
415 }
416 }
417
418 length -= hlen;
419 if (ndo->ndo_vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST)) {
420 ND_PRINT(", seq %u", seq);
421
422 if (length > 0) {
423 ND_PRINT(":%u", seq + length);
424 }
425 }
426
427 if (flags & TH_ACK) {
428 ND_PRINT(", ack %u", ack);
429 }
430
431 ND_PRINT(", win %u", win);
432
433 if (flags & TH_URG)
434 ND_PRINT(", urg %u", urp);
435 /*
436 * Handle any options.
437 */
438 if (hlen > sizeof(*tp)) {
439 const u_char *cp;
440 u_int i, opt, datalen;
441 u_int len;
442
443 hlen -= sizeof(*tp);
444 cp = (const u_char *)tp + sizeof(*tp);
445 ND_PRINT(", options [");
446 while (hlen > 0) {
447 if (ch != '\0')
448 ND_PRINT("%c", ch);
449 opt = GET_U_1(cp);
450 cp++;
451 if (ZEROLENOPT(opt))
452 len = 1;
453 else {
454 len = GET_U_1(cp);
455 cp++; /* total including type, len */
456 if (len < 2 || len > hlen)
457 goto bad;
458 --hlen; /* account for length byte */
459 }
460 --hlen; /* account for type byte */
461 datalen = 0;
462
463 /* Bail if "l" bytes of data are not left or were not captured */
464 #define LENCHECK(l) { if ((l) > hlen) goto bad; }
465
466
467 ND_PRINT("%s", tok2str(tcp_option_values, "unknown-%u", opt));
468
469 switch (opt) {
470
471 case TCPOPT_MAXSEG:
472 datalen = 2;
473 LENCHECK(datalen);
474 ND_PRINT(" %u", GET_BE_U_2(cp));
475 break;
476
477 case TCPOPT_WSCALE:
478 datalen = 1;
479 LENCHECK(datalen);
480 ND_PRINT(" %u", GET_U_1(cp));
481 break;
482
483 case TCPOPT_SACK:
484 datalen = len - 2;
485 if (datalen % 8 != 0) {
486 ND_PRINT(" invalid sack");
487 } else {
488 uint32_t s, e;
489
490 ND_PRINT(" %u ", datalen / 8);
491 for (i = 0; i < datalen; i += 8) {
492 LENCHECK(i + 4);
493 s = GET_BE_U_4(cp + i);
494 LENCHECK(i + 8);
495 e = GET_BE_U_4(cp + i + 4);
496 if (rev) {
497 s -= thseq;
498 e -= thseq;
499 } else {
500 s -= thack;
501 e -= thack;
502 }
503 ND_PRINT("{%u:%u}", s, e);
504 }
505 }
506 break;
507
508 case TCPOPT_CC:
509 case TCPOPT_CCNEW:
510 case TCPOPT_CCECHO:
511 case TCPOPT_ECHO:
512 case TCPOPT_ECHOREPLY:
513
514 /*
515 * those options share their semantics.
516 * fall through
517 */
518 datalen = 4;
519 LENCHECK(datalen);
520 ND_PRINT(" %u", GET_BE_U_4(cp));
521 break;
522
523 case TCPOPT_TIMESTAMP:
524 datalen = 8;
525 LENCHECK(datalen);
526 ND_PRINT(" val %u ecr %u",
527 GET_BE_U_4(cp),
528 GET_BE_U_4(cp + 4));
529 break;
530
531 case TCPOPT_SIGNATURE:
532 datalen = TCP_SIGLEN;
533 LENCHECK(datalen);
534 ND_TCHECK_LEN(cp, datalen);
535 ND_PRINT(" ");
536 #ifdef HAVE_LIBCRYPTO
537 switch (tcp_verify_signature(ndo, ip, tp,
538 bp + TH_OFF(tp) * 4, length, cp)) {
539
540 case SIGNATURE_VALID:
541 ND_PRINT("valid");
542 break;
543
544 case SIGNATURE_INVALID:
545 nd_print_invalid(ndo);
546 break;
547
548 case CANT_CHECK_SIGNATURE:
549 ND_PRINT("can't check - ");
550 for (i = 0; i < TCP_SIGLEN; ++i)
551 ND_PRINT("%02x",
552 GET_U_1(cp + i));
553 break;
554 }
555 #else
556 for (i = 0; i < TCP_SIGLEN; ++i)
557 ND_PRINT("%02x", GET_U_1(cp + i));
558 #endif
559 break;
560
561 case TCPOPT_SCPS:
562 datalen = 2;
563 LENCHECK(datalen);
564 ND_PRINT(" cap %02x id %u", GET_U_1(cp),
565 GET_U_1(cp + 1));
566 break;
567
568 case TCPOPT_TCPAO:
569 datalen = len - 2;
570 /* RFC 5925 Section 2.2:
571 * "The Length value MUST be greater than or equal to 4."
572 * (This includes the Kind and Length fields already processed
573 * at this point.)
574 */
575 if (datalen < 2) {
576 nd_print_invalid(ndo);
577 } else {
578 LENCHECK(1);
579 ND_PRINT(" keyid %u", GET_U_1(cp));
580 LENCHECK(2);
581 ND_PRINT(" rnextkeyid %u",
582 GET_U_1(cp + 1));
583 if (datalen > 2) {
584 ND_PRINT(" mac 0x");
585 for (i = 2; i < datalen; i++) {
586 LENCHECK(i + 1);
587 ND_PRINT("%02x",
588 GET_U_1(cp + i));
589 }
590 }
591 }
592 break;
593
594 case TCPOPT_EOL:
595 case TCPOPT_NOP:
596 case TCPOPT_SACKOK:
597 /*
598 * Nothing interesting.
599 * fall through
600 */
601 break;
602
603 case TCPOPT_UTO:
604 datalen = 2;
605 LENCHECK(datalen);
606 utoval = GET_BE_U_2(cp);
607 ND_PRINT(" 0x%x", utoval);
608 if (utoval & 0x0001)
609 utoval = (utoval >> 1) * 60;
610 else
611 utoval >>= 1;
612 ND_PRINT(" %u", utoval);
613 break;
614
615 case TCPOPT_MPTCP:
616 {
617 const u_char *snapend_save;
618 int ret;
619
620 datalen = len - 2;
621 LENCHECK(datalen);
622 /* FIXME: Proof-read mptcp_print() and if it
623 * always covers all bytes when it returns 1,
624 * only do ND_TCHECK_LEN() if it returned 0.
625 */
626 ND_TCHECK_LEN(cp, datalen);
627 snapend_save = ndo->ndo_snapend;
628 ndo->ndo_snapend = ND_MIN(cp - 2 + len,
629 ndo->ndo_snapend);
630 ret = mptcp_print(ndo, cp - 2, len, flags);
631 ndo->ndo_snapend = snapend_save;
632 if (!ret)
633 goto bad;
634 break;
635 }
636
637 case TCPOPT_FASTOPEN:
638 datalen = len - 2;
639 LENCHECK(datalen);
640 ND_TCHECK_LEN(cp, datalen);
641 ND_PRINT(" ");
642 print_tcp_fastopen_option(ndo, cp, datalen);
643 break;
644
645 case TCPOPT_EXPERIMENT2:
646 datalen = len - 2;
647 LENCHECK(datalen);
648 ND_TCHECK_LEN(cp, datalen);
649 if (datalen < 2)
650 goto bad;
651 /* RFC6994 */
652 magic = GET_BE_U_2(cp);
653 ND_PRINT("-");
654
655 switch(magic) {
656
657 case 0xf989: /* TCP Fast Open RFC 7413 */
658 ND_PRINT("tfo");
659 print_tcp_fastopen_option(ndo, cp + 2, datalen - 2);
660 break;
661
662 default:
663 /* Unknown magic number */
664 ND_PRINT("%04x", magic);
665 break;
666 }
667 break;
668
669 default:
670 datalen = len - 2;
671 if (datalen)
672 ND_PRINT(" 0x");
673 for (i = 0; i < datalen; ++i) {
674 LENCHECK(i + 1);
675 ND_PRINT("%02x", GET_U_1(cp + i));
676 }
677 break;
678 }
679
680 /* Account for data printed */
681 cp += datalen;
682 hlen -= datalen;
683
684 /* Check specification against observed length */
685 ++datalen; /* option octet */
686 if (!ZEROLENOPT(opt))
687 ++datalen; /* size octet */
688 if (datalen != len)
689 ND_PRINT("[len %u]", len);
690 ch = ',';
691 if (opt == TCPOPT_EOL)
692 break;
693 }
694 ND_PRINT("]");
695 }
696
697 /*
698 * Print length field before crawling down the stack.
699 */
700 ND_PRINT(", length %u", length);
701
702 if (length <= 0)
703 return;
704
705 /*
706 * Decode payload if necessary.
707 */
708 header_len = TH_OFF(tp) * 4;
709 /*
710 * Do a bounds check before decoding the payload.
711 * At least the header data is required.
712 */
713 if (!ND_TTEST_LEN(bp, header_len)) {
714 ND_PRINT(" [remaining caplen(%u) < header length(%u)]",
715 ND_BYTES_AVAILABLE_AFTER(bp), header_len);
716 nd_trunc_longjmp(ndo);
717 }
718 bp += header_len;
719 if ((flags & TH_RST) && ndo->ndo_vflag) {
720 print_tcp_rst_data(ndo, bp, length);
721 return;
722 }
723
724 if (ndo->ndo_packettype) {
725 switch (ndo->ndo_packettype) {
726 case PT_ZMTP1:
727 zmtp1_print(ndo, bp, length);
728 break;
729 case PT_RESP:
730 resp_print(ndo, bp, length);
731 break;
732 case PT_DOMAIN:
733 /* over_tcp: TRUE, is_mdns: FALSE */
734 domain_print(ndo, bp, length, TRUE, FALSE);
735 break;
736 }
737 return;
738 }
739
740 if (IS_SRC_OR_DST_PORT(TELNET_PORT)) {
741 telnet_print(ndo, bp, length);
742 } else if (IS_SRC_OR_DST_PORT(SMTP_PORT)) {
743 ND_PRINT(": ");
744 smtp_print(ndo, bp, length);
745 } else if (IS_SRC_OR_DST_PORT(WHOIS_PORT)) {
746 ND_PRINT(": ");
747 ndo->ndo_protocol = "whois"; /* needed by txtproto_print() */
748 txtproto_print(ndo, bp, length, NULL, 0); /* RFC 3912 */
749 } else if (IS_SRC_OR_DST_PORT(BGP_PORT))
750 bgp_print(ndo, bp, length);
751 else if (IS_SRC_OR_DST_PORT(PPTP_PORT))
752 pptp_print(ndo, bp);
753 else if (IS_SRC_OR_DST_PORT(REDIS_PORT))
754 resp_print(ndo, bp, length);
755 else if (IS_SRC_OR_DST_PORT(SSH_PORT))
756 ssh_print(ndo, bp, length);
757 #ifdef ENABLE_SMB
758 else if (IS_SRC_OR_DST_PORT(NETBIOS_SSN_PORT))
759 nbt_tcp_print(ndo, bp, length);
760 else if (IS_SRC_OR_DST_PORT(SMB_PORT))
761 smb_tcp_print(ndo, bp, length);
762 #endif
763 else if (IS_SRC_OR_DST_PORT(BEEP_PORT))
764 beep_print(ndo, bp, length);
765 else if (IS_SRC_OR_DST_PORT(OPENFLOW_PORT_OLD) || IS_SRC_OR_DST_PORT(OPENFLOW_PORT_IANA))
766 openflow_print(ndo, bp, length);
767 else if (IS_SRC_OR_DST_PORT(FTP_PORT)) {
768 ND_PRINT(": ");
769 ftp_print(ndo, bp, length);
770 } else if (IS_SRC_OR_DST_PORT(HTTP_PORT) || IS_SRC_OR_DST_PORT(HTTP_PORT_ALT)) {
771 ND_PRINT(": ");
772 http_print(ndo, bp, length);
773 } else if (IS_SRC_OR_DST_PORT(RTSP_PORT) || IS_SRC_OR_DST_PORT(RTSP_PORT_ALT)) {
774 ND_PRINT(": ");
775 rtsp_print(ndo, bp, length);
776 } else if (IS_SRC_OR_DST_PORT(NAMESERVER_PORT)) {
777 /* over_tcp: TRUE, is_mdns: FALSE */
778 domain_print(ndo, bp, length, TRUE, FALSE);
779 } else if (IS_SRC_OR_DST_PORT(MSDP_PORT)) {
780 msdp_print(ndo, bp, length);
781 } else if (IS_SRC_OR_DST_PORT(RPKI_RTR_PORT)) {
782 rpki_rtr_print(ndo, bp, length);
783 } else if (IS_SRC_OR_DST_PORT(LDP_PORT)) {
784 ldp_print(ndo, bp, length);
785 } else if ((IS_SRC_OR_DST_PORT(NFS_PORT)) &&
786 length >= 4) {
787 /*
788 * If data present, header length valid, and NFS port used,
789 * assume NFS.
790 * Pass offset of data plus 4 bytes for RPC TCP msg length
791 * to NFS print routines.
792 */
793 uint32_t fraglen;
794 const struct sunrpc_msg *rp;
795 enum sunrpc_msg_type direction;
796
797 fraglen = GET_BE_U_4(bp) & 0x7FFFFFFF;
798 if (fraglen > (length) - 4)
799 fraglen = (length) - 4;
800 rp = (const struct sunrpc_msg *)(bp + 4);
801 if (ND_TTEST_4(rp->rm_direction)) {
802 direction = (enum sunrpc_msg_type) GET_BE_U_4(rp->rm_direction);
803 if (dport == NFS_PORT && direction == SUNRPC_CALL) {
804 ND_PRINT(": NFS request xid %u ",
805 GET_BE_U_4(rp->rm_xid));
806 nfsreq_noaddr_print(ndo, (const u_char *)rp, fraglen, (const u_char *)ip);
807 return;
808 }
809 if (sport == NFS_PORT && direction == SUNRPC_REPLY) {
810 ND_PRINT(": NFS reply xid %u ",
811 GET_BE_U_4(rp->rm_xid));
812 nfsreply_noaddr_print(ndo, (const u_char *)rp, fraglen, (const u_char *)ip);
813 return;
814 }
815 }
816 }
817
818 return;
819 bad:
820 ND_PRINT("[bad opt]");
821 if (ch != '\0')
822 ND_PRINT("]");
823 return;
824 invalid:
825 nd_print_invalid(ndo);
826 }
827
828 /*
829 * RFC1122 says the following on data in RST segments:
830 *
831 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
832 *
833 * A TCP SHOULD allow a received RST segment to include data.
834 *
835 * DISCUSSION
836 * It has been suggested that a RST segment could contain
837 * ASCII text that encoded and explained the cause of the
838 * RST. No standard has yet been established for such
839 * data.
840 *
841 */
842
843 static void
844 print_tcp_rst_data(netdissect_options *ndo,
845 const u_char *sp, u_int length)
846 {
847 ND_PRINT(ND_TTEST_LEN(sp, length) ? " [RST" : " [!RST");
848 if (length > MAX_RST_DATA_LEN) {
849 length = MAX_RST_DATA_LEN; /* can use -X for longer */
850 ND_PRINT("+"); /* indicate we truncate */
851 }
852 ND_PRINT(" ");
853 (void)nd_printn(ndo, sp, length, ndo->ndo_snapend);
854 ND_PRINT("]");
855 }
856
857 static void
858 print_tcp_fastopen_option(netdissect_options *ndo, const u_char *cp,
859 u_int datalen)
860 {
861 u_int i;
862
863 if (datalen == 0) {
864 /* Fast Open Cookie Request */
865 ND_PRINT(" cookiereq");
866 } else {
867 /* Fast Open Cookie */
868 if (datalen % 2 != 0 || datalen < 4 || datalen > 16) {
869 nd_print_invalid(ndo);
870 } else {
871 ND_PRINT(" cookie ");
872 for (i = 0; i < datalen; ++i)
873 ND_PRINT("%02x", GET_U_1(cp + i));
874 }
875 }
876 }
877
878 #ifdef HAVE_LIBCRYPTO
879 USES_APPLE_DEPRECATED_API
880 static int
881 tcp_verify_signature(netdissect_options *ndo,
882 const struct ip *ip, const struct tcphdr *tp,
883 const u_char *data, u_int length, const u_char *rcvsig)
884 {
885 struct tcphdr tp1;
886 u_char sig[TCP_SIGLEN];
887 char zero_proto = 0;
888 MD5_CTX ctx;
889 uint16_t savecsum, tlen;
890 const struct ip6_hdr *ip6;
891 uint32_t len32;
892 uint8_t nxt;
893
894 if (!ND_TTEST_LEN(data, length)) {
895 ND_PRINT("snaplen too short, ");
896 return (CANT_CHECK_SIGNATURE);
897 }
898
899 tp1 = *tp;
900
901 if (ndo->ndo_sigsecret == NULL) {
902 ND_PRINT("shared secret not supplied with -M, ");
903 return (CANT_CHECK_SIGNATURE);
904 }
905
906 MD5_Init(&ctx);
907 /*
908 * Step 1: Update MD5 hash with IP pseudo-header.
909 */
910 if (IP_V(ip) == 4) {
911 MD5_Update(&ctx, (const char *)&ip->ip_src, sizeof(ip->ip_src));
912 MD5_Update(&ctx, (const char *)&ip->ip_dst, sizeof(ip->ip_dst));
913 MD5_Update(&ctx, (const char *)&zero_proto, sizeof(zero_proto));
914 MD5_Update(&ctx, (const char *)&ip->ip_p, sizeof(ip->ip_p));
915 tlen = GET_BE_U_2(ip->ip_len) - IP_HL(ip) * 4;
916 tlen = htons(tlen);
917 MD5_Update(&ctx, (const char *)&tlen, sizeof(tlen));
918 } else if (IP_V(ip) == 6) {
919 ip6 = (const struct ip6_hdr *)ip;
920 MD5_Update(&ctx, (const char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
921 MD5_Update(&ctx, (const char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
922 len32 = htonl(GET_BE_U_2(ip6->ip6_plen));
923 MD5_Update(&ctx, (const char *)&len32, sizeof(len32));
924 nxt = 0;
925 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
926 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
927 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
928 nxt = IPPROTO_TCP;
929 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
930 } else {
931 ND_PRINT("IP version not 4 or 6, ");
932 return (CANT_CHECK_SIGNATURE);
933 }
934
935 /*
936 * Step 2: Update MD5 hash with TCP header, excluding options.
937 * The TCP checksum must be set to zero.
938 */
939 memcpy(&savecsum, tp1.th_sum, sizeof(savecsum));
940 memset(tp1.th_sum, 0, sizeof(tp1.th_sum));
941 MD5_Update(&ctx, (const char *)&tp1, sizeof(struct tcphdr));
942 memcpy(tp1.th_sum, &savecsum, sizeof(tp1.th_sum));
943 /*
944 * Step 3: Update MD5 hash with TCP segment data, if present.
945 */
946 if (length > 0)
947 MD5_Update(&ctx, data, length);
948 /*
949 * Step 4: Update MD5 hash with shared secret.
950 */
951 MD5_Update(&ctx, ndo->ndo_sigsecret, strlen(ndo->ndo_sigsecret));
952 MD5_Final(sig, &ctx);
953
954 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
955 return (SIGNATURE_VALID);
956 else
957 return (SIGNATURE_INVALID);
958 }
959 USES_APPLE_RST
960 #endif /* HAVE_LIBCRYPTO */