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