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