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