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