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