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