]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
Add the ndo_protocol field in the netdissect_options structure
[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 ndo->ndo_protocol = "tcp";
177 tp = (const struct tcphdr *)bp;
178 ip = (const struct ip *)bp2;
179 if (IP_V(ip) == 6)
180 ip6 = (const struct ip6_hdr *)bp2;
181 else
182 ip6 = NULL;
183 ch = '\0';
184 if (!ND_TTEST_2(tp->th_dport)) {
185 if (ip6) {
186 ND_PRINT("%s > %s: ",
187 ip6addr_string(ndo, ip6->ip6_src),
188 ip6addr_string(ndo, ip6->ip6_dst));
189 } else {
190 ND_PRINT("%s > %s: [|tcp]",
191 ipaddr_string(ndo, ip->ip_src),
192 ipaddr_string(ndo, ip->ip_dst));
193 }
194 return;
195 }
196
197 sport = EXTRACT_BE_U_2(tp->th_sport);
198 dport = EXTRACT_BE_U_2(tp->th_dport);
199
200 if (ip6) {
201 if (EXTRACT_U_1(ip6->ip6_nxt) == IPPROTO_TCP) {
202 ND_PRINT("%s.%s > %s.%s: ",
203 ip6addr_string(ndo, ip6->ip6_src),
204 tcpport_string(ndo, sport),
205 ip6addr_string(ndo, ip6->ip6_dst),
206 tcpport_string(ndo, dport));
207 } else {
208 ND_PRINT("%s > %s: ",
209 tcpport_string(ndo, sport), tcpport_string(ndo, dport));
210 }
211 } else {
212 if (EXTRACT_U_1(ip->ip_p) == IPPROTO_TCP) {
213 ND_PRINT("%s.%s > %s.%s: ",
214 ipaddr_string(ndo, ip->ip_src),
215 tcpport_string(ndo, sport),
216 ipaddr_string(ndo, ip->ip_dst),
217 tcpport_string(ndo, dport));
218 } else {
219 ND_PRINT("%s > %s: ",
220 tcpport_string(ndo, sport), tcpport_string(ndo, dport));
221 }
222 }
223
224 ND_TCHECK_SIZE(tp);
225
226 hlen = TH_OFF(tp) * 4;
227
228 if (hlen < sizeof(*tp)) {
229 ND_PRINT(" tcp %u [bad hdr length %u - too short, < %lu]",
230 length - hlen, hlen, (unsigned long)sizeof(*tp));
231 return;
232 }
233
234 seq = EXTRACT_BE_U_4(tp->th_seq);
235 ack = EXTRACT_BE_U_4(tp->th_ack);
236 win = EXTRACT_BE_U_2(tp->th_win);
237 urp = EXTRACT_BE_U_2(tp->th_urp);
238
239 if (ndo->ndo_qflag) {
240 ND_PRINT("tcp %u", length - hlen);
241 if (hlen > length) {
242 ND_PRINT(" [bad hdr length %u - too long, > %u]",
243 hlen, length);
244 }
245 return;
246 }
247
248 flags = EXTRACT_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 = 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 = 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 "tcp_print: calloc");
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 = 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 = 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 "tcp_print: calloc");
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 return;
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 = EXTRACT_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 && ip6->ip6_plen) {
402 if (ND_TTEST_LEN(tp->th_sport, length)) {
403 sum = tcp6_cksum(ndo, ip6, tp, length);
404 tcp_sum = EXTRACT_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 ND_TCHECK_1(cp);
449 opt = EXTRACT_U_1(cp);
450 cp++;
451 if (ZEROLENOPT(opt))
452 len = 1;
453 else {
454 ND_TCHECK_1(cp);
455 len = EXTRACT_U_1(cp);
456 cp++; /* total including type, len */
457 if (len < 2 || len > hlen)
458 goto bad;
459 --hlen; /* account for length byte */
460 }
461 --hlen; /* account for type byte */
462 datalen = 0;
463
464 /* Bail if "l" bytes of data are not left or were not captured */
465 #define LENCHECK(l) { if ((l) > hlen) goto bad; ND_TCHECK_LEN(cp, l); }
466
467
468 ND_PRINT("%s", tok2str(tcp_option_values, "unknown-%u", opt));
469
470 switch (opt) {
471
472 case TCPOPT_MAXSEG:
473 datalen = 2;
474 LENCHECK(datalen);
475 ND_PRINT(" %u", EXTRACT_BE_U_2(cp));
476 break;
477
478 case TCPOPT_WSCALE:
479 datalen = 1;
480 LENCHECK(datalen);
481 ND_PRINT(" %u", EXTRACT_U_1(cp));
482 break;
483
484 case TCPOPT_SACK:
485 datalen = len - 2;
486 if (datalen % 8 != 0) {
487 ND_PRINT(" invalid sack");
488 } else {
489 uint32_t s, e;
490
491 ND_PRINT(" %u ", datalen / 8);
492 for (i = 0; i < datalen; i += 8) {
493 LENCHECK(i + 4);
494 s = EXTRACT_BE_U_4(cp + i);
495 LENCHECK(i + 8);
496 e = EXTRACT_BE_U_4(cp + i + 4);
497 if (rev) {
498 s -= thseq;
499 e -= thseq;
500 } else {
501 s -= thack;
502 e -= thack;
503 }
504 ND_PRINT("{%u:%u}", s, e);
505 }
506 }
507 break;
508
509 case TCPOPT_CC:
510 case TCPOPT_CCNEW:
511 case TCPOPT_CCECHO:
512 case TCPOPT_ECHO:
513 case TCPOPT_ECHOREPLY:
514
515 /*
516 * those options share their semantics.
517 * fall through
518 */
519 datalen = 4;
520 LENCHECK(datalen);
521 ND_PRINT(" %u", EXTRACT_BE_U_4(cp));
522 break;
523
524 case TCPOPT_TIMESTAMP:
525 datalen = 8;
526 LENCHECK(datalen);
527 ND_PRINT(" val %u ecr %u",
528 EXTRACT_BE_U_4(cp),
529 EXTRACT_BE_U_4(cp + 4));
530 break;
531
532 case TCPOPT_SIGNATURE:
533 datalen = TCP_SIGLEN;
534 LENCHECK(datalen);
535 ND_PRINT(" ");
536 #ifdef HAVE_LIBCRYPTO
537 switch (tcp_verify_signature(ndo, ip, tp,
538 bp + TH_OFF(tp) * 4, length, cp)) {
539
540 case SIGNATURE_VALID:
541 ND_PRINT("valid");
542 break;
543
544 case SIGNATURE_INVALID:
545 ND_PRINT("invalid");
546 break;
547
548 case CANT_CHECK_SIGNATURE:
549 ND_PRINT("can't check - ");
550 for (i = 0; i < TCP_SIGLEN; ++i)
551 ND_PRINT("%02x", EXTRACT_U_1(cp + i));
552 break;
553 }
554 #else
555 for (i = 0; i < TCP_SIGLEN; ++i)
556 ND_PRINT("%02x", EXTRACT_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", EXTRACT_U_1(cp), EXTRACT_U_1(cp + 1));
564 break;
565
566 case TCPOPT_TCPAO:
567 datalen = len - 2;
568 /* RFC 5925 Section 2.2:
569 * "The Length value MUST be greater than or equal to 4."
570 * (This includes the Kind and Length fields already processed
571 * at this point.)
572 */
573 if (datalen < 2) {
574 ND_PRINT(" invalid");
575 } else {
576 LENCHECK(1);
577 ND_PRINT(" keyid %u", EXTRACT_U_1(cp));
578 LENCHECK(2);
579 ND_PRINT(" rnextkeyid %u", EXTRACT_U_1(cp + 1));
580 if (datalen > 2) {
581 ND_PRINT(" mac 0x");
582 for (i = 2; i < datalen; i++) {
583 LENCHECK(i + 1);
584 ND_PRINT("%02x", EXTRACT_U_1(cp + i));
585 }
586 }
587 }
588 break;
589
590 case TCPOPT_EOL:
591 case TCPOPT_NOP:
592 case TCPOPT_SACKOK:
593 /*
594 * Nothing interesting.
595 * fall through
596 */
597 break;
598
599 case TCPOPT_UTO:
600 datalen = 2;
601 LENCHECK(datalen);
602 utoval = EXTRACT_BE_U_2(cp);
603 ND_PRINT(" 0x%x", utoval);
604 if (utoval & 0x0001)
605 utoval = (utoval >> 1) * 60;
606 else
607 utoval >>= 1;
608 ND_PRINT(" %u", utoval);
609 break;
610
611 case TCPOPT_MPTCP:
612 datalen = len - 2;
613 LENCHECK(datalen);
614 if (!mptcp_print(ndo, cp-2, len, flags))
615 goto bad;
616 break;
617
618 case TCPOPT_FASTOPEN:
619 datalen = len - 2;
620 LENCHECK(datalen);
621 ND_PRINT(" ");
622 print_tcp_fastopen_option(ndo, cp, datalen, FALSE);
623 break;
624
625 case TCPOPT_EXPERIMENT2:
626 datalen = len - 2;
627 LENCHECK(datalen);
628 if (datalen < 2)
629 goto bad;
630 /* RFC6994 */
631 magic = EXTRACT_BE_U_2(cp);
632 ND_PRINT("-");
633
634 switch(magic) {
635
636 case 0xf989: /* TCP Fast Open RFC 7413 */
637 print_tcp_fastopen_option(ndo, cp + 2, datalen - 2, TRUE);
638 break;
639
640 default:
641 /* Unknown magic number */
642 ND_PRINT("%04x", magic);
643 break;
644 }
645 break;
646
647 default:
648 datalen = len - 2;
649 if (datalen)
650 ND_PRINT(" 0x");
651 for (i = 0; i < datalen; ++i) {
652 LENCHECK(i + 1);
653 ND_PRINT("%02x", EXTRACT_U_1(cp + i));
654 }
655 break;
656 }
657
658 /* Account for data printed */
659 cp += datalen;
660 hlen -= datalen;
661
662 /* Check specification against observed length */
663 ++datalen; /* option octet */
664 if (!ZEROLENOPT(opt))
665 ++datalen; /* size octet */
666 if (datalen != len)
667 ND_PRINT("[len %u]", len);
668 ch = ',';
669 if (opt == TCPOPT_EOL)
670 break;
671 }
672 ND_PRINT("]");
673 }
674
675 /*
676 * Print length field before crawling down the stack.
677 */
678 ND_PRINT(", length %u", length);
679
680 if (length <= 0)
681 return;
682
683 /*
684 * Decode payload if necessary.
685 */
686 bp += TH_OFF(tp) * 4;
687 if ((flags & TH_RST) && ndo->ndo_vflag) {
688 print_tcp_rst_data(ndo, bp, length);
689 return;
690 }
691
692 if (ndo->ndo_packettype) {
693 switch (ndo->ndo_packettype) {
694 case PT_ZMTP1:
695 zmtp1_print(ndo, bp, length);
696 break;
697 case PT_RESP:
698 resp_print(ndo, bp, length);
699 break;
700 }
701 return;
702 }
703
704 if (IS_SRC_OR_DST_PORT(TELNET_PORT)) {
705 telnet_print(ndo, bp, length);
706 } else if (IS_SRC_OR_DST_PORT(SMTP_PORT)) {
707 ND_PRINT(": ");
708 smtp_print(ndo, bp, length);
709 } else if (IS_SRC_OR_DST_PORT(WHOIS_PORT)) {
710 ND_PRINT(": ");
711 txtproto_print(ndo, bp, length, "whois", NULL, 0); /* RFC 3912 */
712 } else if (IS_SRC_OR_DST_PORT(BGP_PORT))
713 bgp_print(ndo, bp, length);
714 else if (IS_SRC_OR_DST_PORT(PPTP_PORT))
715 pptp_print(ndo, bp);
716 else if (IS_SRC_OR_DST_PORT(REDIS_PORT))
717 resp_print(ndo, bp, length);
718 #ifdef ENABLE_SMB
719 else if (IS_SRC_OR_DST_PORT(NETBIOS_SSN_PORT))
720 nbt_tcp_print(ndo, bp, length);
721 else if (IS_SRC_OR_DST_PORT(SMB_PORT))
722 smb_tcp_print(ndo, bp, length);
723 #endif
724 else if (IS_SRC_OR_DST_PORT(BEEP_PORT))
725 beep_print(ndo, bp, length);
726 else if (IS_SRC_OR_DST_PORT(OPENFLOW_PORT_OLD) || IS_SRC_OR_DST_PORT(OPENFLOW_PORT_IANA))
727 openflow_print(ndo, bp, length);
728 else if (IS_SRC_OR_DST_PORT(FTP_PORT)) {
729 ND_PRINT(": ");
730 ftp_print(ndo, bp, length);
731 } else if (IS_SRC_OR_DST_PORT(HTTP_PORT) || IS_SRC_OR_DST_PORT(HTTP_PORT_ALT)) {
732 ND_PRINT(": ");
733 http_print(ndo, bp, length);
734 } else if (IS_SRC_OR_DST_PORT(RTSP_PORT) || IS_SRC_OR_DST_PORT(RTSP_PORT_ALT)) {
735 ND_PRINT(": ");
736 rtsp_print(ndo, bp, length);
737 } else if (length > 2 &&
738 (IS_SRC_OR_DST_PORT(NAMESERVER_PORT))) {
739 /*
740 * TCP DNS query has 2byte length at the head.
741 * XXX packet could be unaligned, it can go strange
742 */
743 domain_print(ndo, bp + 2, length - 2, 0);
744 } else if (IS_SRC_OR_DST_PORT(MSDP_PORT)) {
745 msdp_print(ndo, bp, length);
746 } else if (IS_SRC_OR_DST_PORT(RPKI_RTR_PORT)) {
747 rpki_rtr_print(ndo, bp, length);
748 }
749 else if (length > 0 && (IS_SRC_OR_DST_PORT(LDP_PORT))) {
750 ldp_print(ndo, bp, length);
751 }
752 else if ((IS_SRC_OR_DST_PORT(NFS_PORT)) &&
753 length >= 4 && ND_TTEST_4(bp)) {
754 /*
755 * If data present, header length valid, and NFS port used,
756 * assume NFS.
757 * Pass offset of data plus 4 bytes for RPC TCP msg length
758 * to NFS print routines.
759 */
760 uint32_t fraglen;
761 const struct sunrpc_msg *rp;
762 enum sunrpc_msg_type direction;
763
764 fraglen = EXTRACT_BE_U_4(bp) & 0x7FFFFFFF;
765 if (fraglen > (length) - 4)
766 fraglen = (length) - 4;
767 rp = (const struct sunrpc_msg *)(bp + 4);
768 if (ND_TTEST_4(rp->rm_direction)) {
769 direction = (enum sunrpc_msg_type) EXTRACT_BE_U_4(rp->rm_direction);
770 if (dport == NFS_PORT && direction == SUNRPC_CALL) {
771 ND_PRINT(": NFS request xid %u ",
772 EXTRACT_BE_U_4(rp->rm_xid));
773 nfsreq_noaddr_print(ndo, (const u_char *)rp, fraglen, (const u_char *)ip);
774 return;
775 }
776 if (sport == NFS_PORT && direction == SUNRPC_REPLY) {
777 ND_PRINT(": NFS reply xid %u ",
778 EXTRACT_BE_U_4(rp->rm_xid));
779 nfsreply_noaddr_print(ndo, (const u_char *)rp, fraglen, (const u_char *)ip);
780 return;
781 }
782 }
783 }
784
785 return;
786 bad:
787 ND_PRINT("[bad opt]");
788 if (ch != '\0')
789 ND_PRINT(">");
790 return;
791 trunc:
792 ND_PRINT("[|tcp]");
793 if (ch != '\0')
794 ND_PRINT(">");
795 }
796
797 /*
798 * RFC1122 says the following on data in RST segments:
799 *
800 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
801 *
802 * A TCP SHOULD allow a received RST segment to include data.
803 *
804 * DISCUSSION
805 * It has been suggested that a RST segment could contain
806 * ASCII text that encoded and explained the cause of the
807 * RST. No standard has yet been established for such
808 * data.
809 *
810 */
811
812 static void
813 print_tcp_rst_data(netdissect_options *ndo,
814 const u_char *sp, u_int length)
815 {
816 int c;
817
818 ND_PRINT(ND_TTEST_LEN(sp, length) ? " [RST" : " [!RST");
819 if (length > MAX_RST_DATA_LEN) {
820 length = MAX_RST_DATA_LEN; /* can use -X for longer */
821 ND_PRINT("+"); /* indicate we truncate */
822 }
823 ND_PRINT(" ");
824 while (length-- && sp < ndo->ndo_snapend) {
825 c = EXTRACT_U_1(sp);
826 sp++;
827 safeputchar(ndo, c);
828 }
829 ND_PRINT("]");
830 }
831
832 static void
833 print_tcp_fastopen_option(netdissect_options *ndo, const u_char *cp,
834 u_int datalen, int exp)
835 {
836 u_int i;
837
838 if (exp)
839 ND_PRINT("tfo");
840
841 if (datalen == 0) {
842 /* Fast Open Cookie Request */
843 ND_PRINT(" cookiereq");
844 } else {
845 /* Fast Open Cookie */
846 if (datalen % 2 != 0 || datalen < 4 || datalen > 16) {
847 ND_PRINT(" invalid");
848 } else {
849 ND_PRINT(" cookie ");
850 for (i = 0; i < datalen; ++i)
851 ND_PRINT("%02x", EXTRACT_U_1(cp + i));
852 }
853 }
854 }
855
856 #ifdef HAVE_LIBCRYPTO
857 USES_APPLE_DEPRECATED_API
858 static int
859 tcp_verify_signature(netdissect_options *ndo,
860 const struct ip *ip, const struct tcphdr *tp,
861 const u_char *data, u_int length, const u_char *rcvsig)
862 {
863 struct tcphdr tp1;
864 u_char sig[TCP_SIGLEN];
865 char zero_proto = 0;
866 MD5_CTX ctx;
867 uint16_t savecsum, tlen;
868 const struct ip6_hdr *ip6;
869 uint32_t len32;
870 uint8_t nxt;
871
872 if (data + length > ndo->ndo_snapend) {
873 ND_PRINT("snaplen too short, ");
874 return (CANT_CHECK_SIGNATURE);
875 }
876
877 tp1 = *tp;
878
879 if (ndo->ndo_sigsecret == NULL) {
880 ND_PRINT("shared secret not supplied with -M, ");
881 return (CANT_CHECK_SIGNATURE);
882 }
883
884 MD5_Init(&ctx);
885 /*
886 * Step 1: Update MD5 hash with IP pseudo-header.
887 */
888 if (IP_V(ip) == 4) {
889 MD5_Update(&ctx, (const char *)&ip->ip_src, sizeof(ip->ip_src));
890 MD5_Update(&ctx, (const char *)&ip->ip_dst, sizeof(ip->ip_dst));
891 MD5_Update(&ctx, (const char *)&zero_proto, sizeof(zero_proto));
892 MD5_Update(&ctx, (const char *)&ip->ip_p, sizeof(ip->ip_p));
893 tlen = EXTRACT_BE_U_2(ip->ip_len) - IP_HL(ip) * 4;
894 tlen = htons(tlen);
895 MD5_Update(&ctx, (const char *)&tlen, sizeof(tlen));
896 } else if (IP_V(ip) == 6) {
897 ip6 = (const struct ip6_hdr *)ip;
898 MD5_Update(&ctx, (const char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
899 MD5_Update(&ctx, (const char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
900 len32 = htonl(EXTRACT_BE_U_2(ip6->ip6_plen));
901 MD5_Update(&ctx, (const char *)&len32, sizeof(len32));
902 nxt = 0;
903 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
904 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
905 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
906 nxt = IPPROTO_TCP;
907 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
908 } else {
909 ND_PRINT("IP version not 4 or 6, ");
910 return (CANT_CHECK_SIGNATURE);
911 }
912
913 /*
914 * Step 2: Update MD5 hash with TCP header, excluding options.
915 * The TCP checksum must be set to zero.
916 */
917 memcpy(&savecsum, tp1.th_sum, sizeof(savecsum));
918 memset(tp1.th_sum, 0, sizeof(tp1.th_sum));
919 MD5_Update(&ctx, (const char *)&tp1, sizeof(struct tcphdr));
920 memcpy(tp1.th_sum, &savecsum, sizeof(tp1.th_sum));
921 /*
922 * Step 3: Update MD5 hash with TCP segment data, if present.
923 */
924 if (length > 0)
925 MD5_Update(&ctx, data, length);
926 /*
927 * Step 4: Update MD5 hash with shared secret.
928 */
929 MD5_Update(&ctx, ndo->ndo_sigsecret, strlen(ndo->ndo_sigsecret));
930 MD5_Final(sig, &ctx);
931
932 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
933 return (SIGNATURE_VALID);
934 else
935 return (SIGNATURE_INVALID);
936 }
937 USES_APPLE_RST
938 #endif /* HAVE_LIBCRYPTO */
939
940 /*
941 * Local Variables:
942 * c-style: whitesmith
943 * c-basic-offset: 8
944 * End:
945 */