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