]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
From Steiner Haug: handle LDP-over-TCP, and do more complete LDP
[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.115 2004-05-27 21:20:50 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, const 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
410 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
411 if (sum != 0) {
412 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
413 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum, sum));
414 } else
415 (void)printf(" (correct),");
416 }
417 }
418 #ifdef INET6
419 if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !fragmented) {
420 u_int16_t sum,tcp_sum;
421 if (TTEST2(tp->th_sport, length)) {
422 sum = tcp6_cksum(ip6, tp, length);
423 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
424 if (sum != 0) {
425 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
426 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum, sum));
427 } else
428 (void)printf(" (correct),");
429
430 }
431 }
432 #endif
433
434 length -= hlen;
435 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
436 (void)printf(" %u:%u(%u)", seq, seq + length, length);
437 if (flags & TH_ACK)
438 (void)printf(" ack %u", ack);
439
440 (void)printf(" win %d", win);
441
442 if (flags & TH_URG)
443 (void)printf(" urg %d", urp);
444 /*
445 * Handle any options.
446 */
447 if (hlen > sizeof(*tp)) {
448 register const u_char *cp;
449 register u_int i, opt, datalen;
450 register u_int len;
451
452 hlen -= sizeof(*tp);
453 cp = (const u_char *)tp + sizeof(*tp);
454 putchar(' ');
455 ch = '<';
456 while (hlen > 0) {
457 putchar(ch);
458 TCHECK(*cp);
459 opt = *cp++;
460 if (ZEROLENOPT(opt))
461 len = 1;
462 else {
463 TCHECK(*cp);
464 len = *cp++; /* total including type, len */
465 if (len < 2 || len > hlen)
466 goto bad;
467 --hlen; /* account for length byte */
468 }
469 --hlen; /* account for type byte */
470 datalen = 0;
471
472 /* Bail if "l" bytes of data are not left or were not captured */
473 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
474
475 switch (opt) {
476
477 case TCPOPT_MAXSEG:
478 (void)printf("mss");
479 datalen = 2;
480 LENCHECK(datalen);
481 (void)printf(" %u", EXTRACT_16BITS(cp));
482
483 break;
484
485 case TCPOPT_EOL:
486 (void)printf("eol");
487 break;
488
489 case TCPOPT_NOP:
490 (void)printf("nop");
491 break;
492
493 case TCPOPT_WSCALE:
494 (void)printf("wscale");
495 datalen = 1;
496 LENCHECK(datalen);
497 (void)printf(" %u", *cp);
498 break;
499
500 case TCPOPT_SACKOK:
501 (void)printf("sackOK");
502 break;
503
504 case TCPOPT_SACK:
505 (void)printf("sack");
506 datalen = len - 2;
507 if (datalen % 8 != 0) {
508 (void)printf(" malformed sack ");
509 } else {
510 u_int32_t s, e;
511
512 (void)printf(" sack %d ", datalen / 8);
513 for (i = 0; i < datalen; i += 8) {
514 LENCHECK(i + 4);
515 s = EXTRACT_32BITS(cp + i);
516 LENCHECK(i + 8);
517 e = EXTRACT_32BITS(cp + i + 4);
518 if (threv) {
519 s -= thseq;
520 e -= thseq;
521 } else {
522 s -= thack;
523 e -= thack;
524 }
525 (void)printf("{%u:%u}", s, e);
526 }
527 (void)printf(" ");
528 }
529 break;
530
531 case TCPOPT_ECHO:
532 (void)printf("echo");
533 datalen = 4;
534 LENCHECK(datalen);
535 (void)printf(" %u", EXTRACT_32BITS(cp));
536 break;
537
538 case TCPOPT_ECHOREPLY:
539 (void)printf("echoreply");
540 datalen = 4;
541 LENCHECK(datalen);
542 (void)printf(" %u", EXTRACT_32BITS(cp));
543 break;
544
545 case TCPOPT_TIMESTAMP:
546 (void)printf("timestamp");
547 datalen = 8;
548 LENCHECK(4);
549 (void)printf(" %u", EXTRACT_32BITS(cp));
550 LENCHECK(datalen);
551 (void)printf(" %u", EXTRACT_32BITS(cp + 4));
552 break;
553
554 case TCPOPT_CC:
555 (void)printf("cc");
556 datalen = 4;
557 LENCHECK(datalen);
558 (void)printf(" %u", EXTRACT_32BITS(cp));
559 break;
560
561 case TCPOPT_CCNEW:
562 (void)printf("ccnew");
563 datalen = 4;
564 LENCHECK(datalen);
565 (void)printf(" %u", EXTRACT_32BITS(cp));
566 break;
567
568 case TCPOPT_CCECHO:
569 (void)printf("ccecho");
570 datalen = 4;
571 LENCHECK(datalen);
572 (void)printf(" %u", EXTRACT_32BITS(cp));
573 break;
574
575 case TCPOPT_SIGNATURE:
576 (void)printf("md5:");
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 (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) {
654 ldp_print(bp, length);
655 }
656 }
657 return;
658 bad:
659 fputs("[bad opt]", stdout);
660 if (ch != '\0')
661 putchar('>');
662 return;
663 trunc:
664 fputs("[|tcp]", stdout);
665 if (ch != '\0')
666 putchar('>');
667 }
668
669 /*
670 * RFC1122 says the following on data in RST segments:
671 *
672 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
673 *
674 * A TCP SHOULD allow a received RST segment to include data.
675 *
676 * DISCUSSION
677 * It has been suggested that a RST segment could contain
678 * ASCII text that encoded and explained the cause of the
679 * RST. No standard has yet been established for such
680 * data.
681 *
682 */
683
684 static void
685 print_tcp_rst_data(register const u_char *sp, u_int length)
686 {
687 int c;
688
689 if (TTEST2(*sp, length))
690 printf(" [RST");
691 else
692 printf(" [!RST");
693 if (length > MAX_RST_DATA_LEN) {
694 length = MAX_RST_DATA_LEN; /* can use -X for longer */
695 putchar('+'); /* indicate we truncate */
696 }
697 putchar(' ');
698 while (length-- && sp <= snapend) {
699 c = *sp++;
700 safeputchar(c);
701 }
702 putchar(']');
703 }
704
705 #ifdef HAVE_LIBCRYPTO
706 static int
707 tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
708 const u_char *data, int length, const u_char *rcvsig)
709 {
710 struct tcphdr tp1;
711 char sig[TCP_SIGLEN];
712 char zero_proto = 0;
713 MD5_CTX ctx;
714 u_int16_t savecsum, tlen;
715 struct ip6_hdr *ip6;
716 u_int32_t len32;
717 u_int8_t nxt;
718
719 tp1 = *tp;
720
721 if (tcpmd5secret == NULL)
722 return (-1);
723
724 MD5_Init(&ctx);
725 /*
726 * Step 1: Update MD5 hash with IP pseudo-header.
727 */
728 if (IP_V(ip) == 4) {
729 MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src));
730 MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst));
731 MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto));
732 MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p));
733 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
734 tlen = htons(tlen);
735 MD5_Update(&ctx, (char *)&tlen, sizeof(tlen));
736 } else if (IP_V(ip) == 6) {
737 ip6 = (struct ip6_hdr *)ip;
738 MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
739 MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
740 len32 = htonl(ntohs(ip6->ip6_plen));
741 MD5_Update(&ctx, (char *)&len32, sizeof(len32));
742 nxt = 0;
743 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
744 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
745 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
746 nxt = IPPROTO_TCP;
747 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
748 } else
749 return (-1);
750
751 /*
752 * Step 2: Update MD5 hash with TCP header, excluding options.
753 * The TCP checksum must be set to zero.
754 */
755 savecsum = tp1.th_sum;
756 tp1.th_sum = 0;
757 MD5_Update(&ctx, (char *)&tp1, sizeof(struct tcphdr));
758 tp1.th_sum = savecsum;
759 /*
760 * Step 3: Update MD5 hash with TCP segment data, if present.
761 */
762 if (length > 0)
763 MD5_Update(&ctx, data, length);
764 /*
765 * Step 4: Update MD5 hash with shared secret.
766 */
767 MD5_Update(&ctx, tcpmd5secret, strlen(tcpmd5secret));
768 MD5_Final(sig, &ctx);
769
770 return (memcmp(rcvsig, sig, 16));
771 }
772 #endif /* HAVE_LIBCRYPTO */