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