]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
from Gerrit Renker <[email protected]>:
[tcpdump] / print-tcp.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Copyright (c) 1999-2004 The tcpdump.org project
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 */
23
24 #ifndef lint
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.126 2006-11-02 08:56:16 hannes Exp $ (LBL)";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <tcpdump-stdinc.h>
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "interface.h"
40 #include "addrtoname.h"
41 #include "extract.h"
42
43 #include "tcp.h"
44
45 #include "ip.h"
46 #ifdef INET6
47 #include "ip6.h"
48 #endif
49 #include "ipproto.h"
50 #include "rpc_auth.h"
51 #include "rpc_msg.h"
52
53 #include "nameser.h"
54
55 #ifdef HAVE_LIBCRYPTO
56 #include <openssl/md5.h>
57
58 #define SIGNATURE_VALID 0
59 #define SIGNATURE_INVALID 1
60 #define CANT_CHECK_SIGNATURE 2
61
62 static int tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
63 const u_char *data, int length, const u_char *rcvsig);
64 #endif
65
66 static void print_tcp_rst_data(register const u_char *sp, u_int length);
67
68 #define MAX_RST_DATA_LEN 30
69
70
71 struct tha {
72 #ifndef INET6
73 struct in_addr src;
74 struct in_addr dst;
75 #else
76 struct in6_addr src;
77 struct in6_addr dst;
78 #endif /*INET6*/
79 u_int port;
80 };
81
82 struct tcp_seq_hash {
83 struct tcp_seq_hash *nxt;
84 struct tha addr;
85 tcp_seq seq;
86 tcp_seq ack;
87 };
88
89 #define TSEQ_HASHSIZE 919
90
91 /* These tcp optinos do not have the size octet */
92 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
93
94 static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
95
96
97 #ifndef TELNET_PORT
98 #define TELNET_PORT 23
99 #endif
100 #ifndef BGP_PORT
101 #define BGP_PORT 179
102 #endif
103 #define NETBIOS_SSN_PORT 139
104 #ifndef PPTP_PORT
105 #define PPTP_PORT 1723
106 #endif
107 #define BEEP_PORT 10288
108 #ifndef NFS_PORT
109 #define NFS_PORT 2049
110 #endif
111 #define MSDP_PORT 639
112 #define LDP_PORT 646
113
114 static int tcp_cksum(register const struct ip *ip,
115 register const struct tcphdr *tp,
116 register u_int len)
117 {
118 union phu {
119 struct phdr {
120 u_int32_t src;
121 u_int32_t dst;
122 u_char mbz;
123 u_char proto;
124 u_int16_t len;
125 } ph;
126 u_int16_t pa[6];
127 } phu;
128 const u_int16_t *sp;
129
130 /* pseudo-header.. */
131 phu.ph.len = htons((u_int16_t)len);
132 phu.ph.mbz = 0;
133 phu.ph.proto = IPPROTO_TCP;
134 memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
135 if (IP_HL(ip) == 5)
136 memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
137 else
138 phu.ph.dst = ip_finddst(ip);
139
140 sp = &phu.pa[0];
141 return in_cksum((u_short *)tp, len,
142 sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5]);
143 }
144
145 #ifdef INET6
146 static int tcp6_cksum(const struct ip6_hdr *ip6, const struct tcphdr *tp,
147 u_int len)
148 {
149 size_t i;
150 u_int32_t sum = 0;
151 union {
152 struct {
153 struct in6_addr ph_src;
154 struct in6_addr ph_dst;
155 u_int32_t ph_len;
156 u_int8_t ph_zero[3];
157 u_int8_t ph_nxt;
158 } ph;
159 u_int16_t pa[20];
160 } phu;
161
162 /* pseudo-header */
163 memset(&phu, 0, sizeof(phu));
164 phu.ph.ph_src = ip6->ip6_src;
165 phu.ph.ph_dst = ip6->ip6_dst;
166 phu.ph.ph_len = htonl(len);
167 phu.ph.ph_nxt = IPPROTO_TCP;
168
169 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
170 sum += phu.pa[i];
171
172 return in_cksum((u_short *)tp, len, sum);
173 }
174 #endif
175
176 void
177 tcp_print(register const u_char *bp, register u_int length,
178 register const u_char *bp2, int fragmented)
179 {
180 register const struct tcphdr *tp;
181 register const struct ip *ip;
182 register u_char flags;
183 register u_int hlen;
184 register char ch;
185 u_int16_t sport, dport, win, urp;
186 u_int32_t seq, ack, thseq, thack;
187 int threv;
188 #ifdef INET6
189 register const struct ip6_hdr *ip6;
190 #endif
191
192 tp = (struct tcphdr *)bp;
193 ip = (struct ip *)bp2;
194 #ifdef INET6
195 if (IP_V(ip) == 6)
196 ip6 = (struct ip6_hdr *)bp2;
197 else
198 ip6 = NULL;
199 #endif /*INET6*/
200 ch = '\0';
201 if (!TTEST(tp->th_dport)) {
202 (void)printf("%s > %s: [|tcp]",
203 ipaddr_string(&ip->ip_src),
204 ipaddr_string(&ip->ip_dst));
205 return;
206 }
207
208 sport = EXTRACT_16BITS(&tp->th_sport);
209 dport = EXTRACT_16BITS(&tp->th_dport);
210
211 hlen = TH_OFF(tp) * 4;
212
213 /*
214 * If data present, header length valid, and NFS port used,
215 * assume NFS.
216 * Pass offset of data plus 4 bytes for RPC TCP msg length
217 * to NFS print routines.
218 */
219 if (!qflag && hlen >= sizeof(*tp) && hlen <= length) {
220 if ((u_char *)tp + 4 + sizeof(struct sunrpc_msg) <= snapend &&
221 dport == NFS_PORT) {
222 nfsreq_print((u_char *)tp + hlen + 4, length - hlen,
223 (u_char *)ip);
224 return;
225 } else if ((u_char *)tp + 4 + sizeof(struct sunrpc_msg)
226 <= snapend &&
227 sport == NFS_PORT) {
228 nfsreply_print((u_char *)tp + hlen + 4, length - hlen,
229 (u_char *)ip);
230 return;
231 }
232 }
233 #ifdef INET6
234 if (ip6) {
235 if (ip6->ip6_nxt == IPPROTO_TCP) {
236 (void)printf("%s.%s > %s.%s: ",
237 ip6addr_string(&ip6->ip6_src),
238 tcpport_string(sport),
239 ip6addr_string(&ip6->ip6_dst),
240 tcpport_string(dport));
241 } else {
242 (void)printf("%s > %s: ",
243 tcpport_string(sport), tcpport_string(dport));
244 }
245 } else
246 #endif /*INET6*/
247 {
248 if (ip->ip_p == IPPROTO_TCP) {
249 (void)printf("%s.%s > %s.%s: ",
250 ipaddr_string(&ip->ip_src),
251 tcpport_string(sport),
252 ipaddr_string(&ip->ip_dst),
253 tcpport_string(dport));
254 } else {
255 (void)printf("%s > %s: ",
256 tcpport_string(sport), tcpport_string(dport));
257 }
258 }
259
260 if (hlen < sizeof(*tp)) {
261 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
262 length - hlen, hlen, (unsigned long)sizeof(*tp));
263 return;
264 }
265
266 TCHECK(*tp);
267
268 seq = EXTRACT_32BITS(&tp->th_seq);
269 ack = EXTRACT_32BITS(&tp->th_ack);
270 win = EXTRACT_16BITS(&tp->th_win);
271 urp = EXTRACT_16BITS(&tp->th_urp);
272
273 if (qflag) {
274 (void)printf("tcp %d", length - hlen);
275 if (hlen > length) {
276 (void)printf(" [bad hdr length %u - too long, > %u]",
277 hlen, length);
278 }
279 return;
280 }
281 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|
282 TH_ECNECHO|TH_CWR)) {
283 if (flags & TH_SYN)
284 putchar('S');
285 if (flags & TH_FIN)
286 putchar('F');
287 if (flags & TH_RST)
288 putchar('R');
289 if (flags & TH_PUSH)
290 putchar('P');
291 if (flags & TH_CWR)
292 putchar('W'); /* congestion _W_indow reduced (ECN) */
293 if (flags & TH_ECNECHO)
294 putchar('E'); /* ecn _E_cho sent (ECN) */
295 } else
296 putchar('.');
297
298 if (!Sflag && (flags & TH_ACK)) {
299 register struct tcp_seq_hash *th;
300 const void *src, *dst;
301 register int rev;
302 struct tha tha;
303 /*
304 * Find (or record) the initial sequence numbers for
305 * this conversation. (we pick an arbitrary
306 * collating order so there's only one entry for
307 * both directions).
308 */
309 #ifdef INET6
310 memset(&tha, 0, sizeof(tha));
311 rev = 0;
312 if (ip6) {
313 src = &ip6->ip6_src;
314 dst = &ip6->ip6_dst;
315 if (sport > dport)
316 rev = 1;
317 else if (sport == dport) {
318 if (memcmp(src, dst, sizeof ip6->ip6_dst) > 0)
319 rev = 1;
320 }
321 if (rev) {
322 memcpy(&tha.src, dst, sizeof ip6->ip6_dst);
323 memcpy(&tha.dst, src, sizeof ip6->ip6_src);
324 tha.port = dport << 16 | sport;
325 } else {
326 memcpy(&tha.dst, dst, sizeof ip6->ip6_dst);
327 memcpy(&tha.src, src, sizeof ip6->ip6_src);
328 tha.port = sport << 16 | dport;
329 }
330 } else {
331 src = &ip->ip_src;
332 dst = &ip->ip_dst;
333 if (sport > dport)
334 rev = 1;
335 else if (sport == dport) {
336 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
337 rev = 1;
338 }
339 if (rev) {
340 memcpy(&tha.src, dst, sizeof ip->ip_dst);
341 memcpy(&tha.dst, src, sizeof ip->ip_src);
342 tha.port = dport << 16 | sport;
343 } else {
344 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
345 memcpy(&tha.src, src, sizeof ip->ip_src);
346 tha.port = sport << 16 | dport;
347 }
348 }
349 #else
350 rev = 0;
351 src = &ip->ip_src;
352 dst = &ip->ip_dst;
353 if (sport > dport)
354 rev = 1;
355 else if (sport == dport) {
356 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
357 rev = 1;
358 }
359 if (rev) {
360 memcpy(&tha.src, dst, sizeof ip->ip_dst);
361 memcpy(&tha.dst, src, sizeof ip->ip_src);
362 tha.port = dport << 16 | sport;
363 } else {
364 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
365 memcpy(&tha.src, src, sizeof ip->ip_src);
366 tha.port = sport << 16 | dport;
367 }
368 #endif
369
370 threv = rev;
371 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
372 th->nxt; th = th->nxt)
373 if (memcmp((char *)&tha, (char *)&th->addr,
374 sizeof(th->addr)) == 0)
375 break;
376
377 if (!th->nxt || (flags & TH_SYN)) {
378 /* didn't find it or new conversation */
379 if (th->nxt == NULL) {
380 th->nxt = (struct tcp_seq_hash *)
381 calloc(1, sizeof(*th));
382 if (th->nxt == NULL)
383 error("tcp_print: calloc");
384 }
385 th->addr = tha;
386 if (rev)
387 th->ack = seq, th->seq = ack - 1;
388 else
389 th->seq = seq, th->ack = ack - 1;
390 } else {
391 if (rev)
392 seq -= th->ack, ack -= th->seq;
393 else
394 seq -= th->seq, ack -= th->ack;
395 }
396
397 thseq = th->seq;
398 thack = th->ack;
399 } else {
400 /*fool gcc*/
401 thseq = thack = threv = 0;
402 }
403 if (hlen > length) {
404 (void)printf(" [bad hdr length %u - too long, > %u]",
405 hlen, length);
406 return;
407 }
408
409 if (IP_V(ip) == 4 && vflag && !Kflag && !fragmented) {
410 u_int16_t sum, tcp_sum;
411 if (TTEST2(tp->th_sport, length)) {
412 sum = tcp_cksum(ip, tp, length);
413
414 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
415 if (sum != 0) {
416 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
417 (void)printf(" (incorrect -> 0x%04x),",in_cksum_shouldbe(tcp_sum, sum));
418 } else
419 (void)printf(" (correct),");
420 }
421 }
422 #ifdef INET6
423 if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !Kflag && !fragmented) {
424 u_int16_t sum,tcp_sum;
425 if (TTEST2(tp->th_sport, length)) {
426 sum = tcp6_cksum(ip6, tp, length);
427 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
428 if (sum != 0) {
429 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
430 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum, sum));
431 } else
432 (void)printf(" (correct),");
433
434 }
435 }
436 #endif
437
438 length -= hlen;
439 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
440 (void)printf(" %u:%u(%u)", seq, seq + length, length);
441 if (flags & TH_ACK)
442 (void)printf(" ack %u", ack);
443
444 (void)printf(" win %d", win);
445
446 if (flags & TH_URG)
447 (void)printf(" urg %d", urp);
448 /*
449 * Handle any options.
450 */
451 if (hlen > sizeof(*tp)) {
452 register const u_char *cp;
453 register u_int i, opt, datalen;
454 register u_int len;
455
456 hlen -= sizeof(*tp);
457 cp = (const u_char *)tp + sizeof(*tp);
458 putchar(' ');
459 ch = '<';
460 while (hlen > 0) {
461 putchar(ch);
462 TCHECK(*cp);
463 opt = *cp++;
464 if (ZEROLENOPT(opt))
465 len = 1;
466 else {
467 TCHECK(*cp);
468 len = *cp++; /* total including type, len */
469 if (len < 2 || len > hlen)
470 goto bad;
471 --hlen; /* account for length byte */
472 }
473 --hlen; /* account for type byte */
474 datalen = 0;
475
476 /* Bail if "l" bytes of data are not left or were not captured */
477 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
478
479 switch (opt) {
480
481 case TCPOPT_MAXSEG:
482 (void)printf("mss");
483 datalen = 2;
484 LENCHECK(datalen);
485 (void)printf(" %u", EXTRACT_16BITS(cp));
486
487 break;
488
489 case TCPOPT_EOL:
490 (void)printf("eol");
491 break;
492
493 case TCPOPT_NOP:
494 (void)printf("nop");
495 break;
496
497 case TCPOPT_WSCALE:
498 (void)printf("wscale");
499 datalen = 1;
500 LENCHECK(datalen);
501 (void)printf(" %u", *cp);
502 break;
503
504 case TCPOPT_SACKOK:
505 (void)printf("sackOK");
506 break;
507
508 case TCPOPT_SACK:
509 datalen = len - 2;
510 if (datalen % 8 != 0) {
511 (void)printf("malformed sack");
512 } else {
513 u_int32_t s, e;
514
515 (void)printf("sack %d ", datalen / 8);
516 for (i = 0; i < datalen; i += 8) {
517 LENCHECK(i + 4);
518 s = EXTRACT_32BITS(cp + i);
519 LENCHECK(i + 8);
520 e = EXTRACT_32BITS(cp + i + 4);
521 if (threv) {
522 s -= thseq;
523 e -= thseq;
524 } else {
525 s -= thack;
526 e -= thack;
527 }
528 (void)printf("{%u:%u}", s, e);
529 }
530 }
531 break;
532
533 case TCPOPT_ECHO:
534 (void)printf("echo");
535 datalen = 4;
536 LENCHECK(datalen);
537 (void)printf(" %u", EXTRACT_32BITS(cp));
538 break;
539
540 case TCPOPT_ECHOREPLY:
541 (void)printf("echoreply");
542 datalen = 4;
543 LENCHECK(datalen);
544 (void)printf(" %u", EXTRACT_32BITS(cp));
545 break;
546
547 case TCPOPT_TIMESTAMP:
548 (void)printf("timestamp");
549 datalen = 8;
550 LENCHECK(4);
551 (void)printf(" %u", EXTRACT_32BITS(cp));
552 LENCHECK(datalen);
553 (void)printf(" %u", EXTRACT_32BITS(cp + 4));
554 break;
555
556 case TCPOPT_CC:
557 (void)printf("cc");
558 datalen = 4;
559 LENCHECK(datalen);
560 (void)printf(" %u", EXTRACT_32BITS(cp));
561 break;
562
563 case TCPOPT_CCNEW:
564 (void)printf("ccnew");
565 datalen = 4;
566 LENCHECK(datalen);
567 (void)printf(" %u", EXTRACT_32BITS(cp));
568 break;
569
570 case TCPOPT_CCECHO:
571 (void)printf("ccecho");
572 datalen = 4;
573 LENCHECK(datalen);
574 (void)printf(" %u", EXTRACT_32BITS(cp));
575 break;
576
577 case TCPOPT_SIGNATURE:
578 (void)printf("md5:");
579 datalen = TCP_SIGLEN;
580 LENCHECK(datalen);
581 #ifdef HAVE_LIBCRYPTO
582 switch (tcp_verify_signature(ip, tp,
583 bp + TH_OFF(tp) * 4, length, cp)) {
584
585 case SIGNATURE_VALID:
586 (void)printf("valid");
587 break;
588
589 case SIGNATURE_INVALID:
590 (void)printf("invalid");
591 break;
592
593 case CANT_CHECK_SIGNATURE:
594 (void)printf("can't check - ");
595 for (i = 0; i < TCP_SIGLEN; ++i)
596 (void)printf("%02x", cp[i]);
597 break;
598 }
599 #else
600 for (i = 0; i < TCP_SIGLEN; ++i)
601 (void)printf("%02x", cp[i]);
602 #endif
603 break;
604
605 case TCPOPT_AUTH:
606 (void)printf("Enhanced Auth: keyid %d", *cp++);
607 datalen = len - 3;
608 for (i = 0; i < datalen; ++i) {
609 LENCHECK(i);
610 (void)printf("%02x", cp[i]);
611 }
612 break;
613
614 default:
615 (void)printf("opt-%u:", opt);
616 datalen = len - 2;
617 for (i = 0; i < datalen; ++i) {
618 LENCHECK(i);
619 (void)printf("%02x", cp[i]);
620 }
621 break;
622 }
623
624 /* Account for data printed */
625 cp += datalen;
626 hlen -= datalen;
627
628 /* Check specification against observed length */
629 ++datalen; /* option octet */
630 if (!ZEROLENOPT(opt))
631 ++datalen; /* size octet */
632 if (datalen != len)
633 (void)printf("[len %d]", len);
634 ch = ',';
635 if (opt == TCPOPT_EOL)
636 break;
637 }
638 putchar('>');
639 }
640
641 if (length <= 0)
642 return;
643
644 /*
645 * Decode payload if necessary.
646 */
647 bp += TH_OFF(tp) * 4;
648 if (flags & TH_RST) {
649 if (vflag)
650 print_tcp_rst_data(bp, length);
651 } else {
652 if (sport == TELNET_PORT || dport == TELNET_PORT) {
653 if (!qflag && vflag)
654 telnet_print(bp, length);
655 } else if (sport == BGP_PORT || dport == BGP_PORT)
656 bgp_print(bp, length);
657 else if (sport == PPTP_PORT || dport == PPTP_PORT)
658 pptp_print(bp);
659 #ifdef TCPDUMP_DO_SMB
660 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
661 nbt_tcp_print(bp, length);
662 #endif
663 else if (sport == BEEP_PORT || dport == BEEP_PORT)
664 beep_print(bp, length);
665 else if (length > 2 &&
666 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT ||
667 sport == MULTICASTDNS_PORT || dport == MULTICASTDNS_PORT)) {
668 /*
669 * TCP DNS query has 2byte length at the head.
670 * XXX packet could be unaligned, it can go strange
671 */
672 ns_print(bp + 2, length - 2, 0);
673 } else if (sport == MSDP_PORT || dport == MSDP_PORT) {
674 msdp_print(bp, length);
675 }
676 else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) {
677 ldp_print(bp, length);
678 }
679 }
680 return;
681 bad:
682 fputs("[bad opt]", stdout);
683 if (ch != '\0')
684 putchar('>');
685 return;
686 trunc:
687 fputs("[|tcp]", stdout);
688 if (ch != '\0')
689 putchar('>');
690 }
691
692 /*
693 * RFC1122 says the following on data in RST segments:
694 *
695 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
696 *
697 * A TCP SHOULD allow a received RST segment to include data.
698 *
699 * DISCUSSION
700 * It has been suggested that a RST segment could contain
701 * ASCII text that encoded and explained the cause of the
702 * RST. No standard has yet been established for such
703 * data.
704 *
705 */
706
707 static void
708 print_tcp_rst_data(register const u_char *sp, u_int length)
709 {
710 int c;
711
712 if (TTEST2(*sp, length))
713 printf(" [RST");
714 else
715 printf(" [!RST");
716 if (length > MAX_RST_DATA_LEN) {
717 length = MAX_RST_DATA_LEN; /* can use -X for longer */
718 putchar('+'); /* indicate we truncate */
719 }
720 putchar(' ');
721 while (length-- && sp <= snapend) {
722 c = *sp++;
723 safeputchar(c);
724 }
725 putchar(']');
726 }
727
728 #ifdef HAVE_LIBCRYPTO
729 static int
730 tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
731 const u_char *data, int length, const u_char *rcvsig)
732 {
733 struct tcphdr tp1;
734 u_char sig[TCP_SIGLEN];
735 char zero_proto = 0;
736 MD5_CTX ctx;
737 u_int16_t savecsum, tlen;
738 #ifdef INET6
739 struct ip6_hdr *ip6;
740 #endif
741 u_int32_t len32;
742 u_int8_t nxt;
743
744 tp1 = *tp;
745
746 if (tcpmd5secret == NULL)
747 return (CANT_CHECK_SIGNATURE);
748
749 MD5_Init(&ctx);
750 /*
751 * Step 1: Update MD5 hash with IP pseudo-header.
752 */
753 if (IP_V(ip) == 4) {
754 MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src));
755 MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst));
756 MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto));
757 MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p));
758 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
759 tlen = htons(tlen);
760 MD5_Update(&ctx, (char *)&tlen, sizeof(tlen));
761 #ifdef INET6
762 } else if (IP_V(ip) == 6) {
763 ip6 = (struct ip6_hdr *)ip;
764 MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
765 MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
766 len32 = htonl(ntohs(ip6->ip6_plen));
767 MD5_Update(&ctx, (char *)&len32, sizeof(len32));
768 nxt = 0;
769 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
770 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
771 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
772 nxt = IPPROTO_TCP;
773 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
774 #endif
775 } else
776 return (CANT_CHECK_SIGNATURE);
777
778 /*
779 * Step 2: Update MD5 hash with TCP header, excluding options.
780 * The TCP checksum must be set to zero.
781 */
782 savecsum = tp1.th_sum;
783 tp1.th_sum = 0;
784 MD5_Update(&ctx, (char *)&tp1, sizeof(struct tcphdr));
785 tp1.th_sum = savecsum;
786 /*
787 * Step 3: Update MD5 hash with TCP segment data, if present.
788 */
789 if (length > 0)
790 MD5_Update(&ctx, data, length);
791 /*
792 * Step 4: Update MD5 hash with shared secret.
793 */
794 MD5_Update(&ctx, tcpmd5secret, strlen(tcpmd5secret));
795 MD5_Final(sig, &ctx);
796
797 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
798 return (SIGNATURE_VALID);
799 else
800 return (SIGNATURE_INVALID);
801 }
802 #endif /* HAVE_LIBCRYPTO */