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