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