]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
The second argument to "rsvp_obj_print()" is just a character string
[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.117 2004-07-15 00:13:01 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, header length valid, and NFS port used,
223 * assume NFS.
224 * Pass offset of data plus 4 bytes for RPC TCP msg length
225 * to NFS print routines.
226 */
227 if (!qflag && hlen >= sizeof(*tp) && hlen <= length) {
228 if ((u_char *)tp + 4 + sizeof(struct rpc_msg) <= snapend &&
229 dport == NFS_PORT) {
230 nfsreq_print((u_char *)tp + hlen + 4, length - hlen,
231 (u_char *)ip);
232 return;
233 } else if ((u_char *)tp + 4 + sizeof(struct rpc_msg)
234 <= snapend &&
235 sport == NFS_PORT) {
236 nfsreply_print((u_char *)tp + hlen + 4, length - hlen,
237 (u_char *)ip);
238 return;
239 }
240 }
241 #ifdef INET6
242 if (ip6) {
243 if (ip6->ip6_nxt == IPPROTO_TCP) {
244 (void)printf("%s.%s > %s.%s: ",
245 ip6addr_string(&ip6->ip6_src),
246 tcpport_string(sport),
247 ip6addr_string(&ip6->ip6_dst),
248 tcpport_string(dport));
249 } else {
250 (void)printf("%s > %s: ",
251 tcpport_string(sport), tcpport_string(dport));
252 }
253 } else
254 #endif /*INET6*/
255 {
256 if (ip->ip_p == IPPROTO_TCP) {
257 (void)printf("%s.%s > %s.%s: ",
258 ipaddr_string(&ip->ip_src),
259 tcpport_string(sport),
260 ipaddr_string(&ip->ip_dst),
261 tcpport_string(dport));
262 } else {
263 (void)printf("%s > %s: ",
264 tcpport_string(sport), tcpport_string(dport));
265 }
266 }
267
268 if (hlen < sizeof(*tp)) {
269 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
270 length - hlen, hlen, (unsigned long)sizeof(*tp));
271 return;
272 }
273
274 TCHECK(*tp);
275
276 seq = EXTRACT_32BITS(&tp->th_seq);
277 ack = EXTRACT_32BITS(&tp->th_ack);
278 win = EXTRACT_16BITS(&tp->th_win);
279 urp = EXTRACT_16BITS(&tp->th_urp);
280
281 if (qflag) {
282 (void)printf("tcp %d", length - hlen);
283 if (hlen > length) {
284 (void)printf(" [bad hdr length %u - too long, > %u]",
285 hlen, length);
286 }
287 return;
288 }
289 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|
290 TH_ECNECHO|TH_CWR)) {
291 if (flags & TH_SYN)
292 putchar('S');
293 if (flags & TH_FIN)
294 putchar('F');
295 if (flags & TH_RST)
296 putchar('R');
297 if (flags & TH_PUSH)
298 putchar('P');
299 if (flags & TH_CWR)
300 putchar('W'); /* congestion _W_indow reduced (ECN) */
301 if (flags & TH_ECNECHO)
302 putchar('E'); /* ecn _E_cho sent (ECN) */
303 } else
304 putchar('.');
305
306 if (!Sflag && (flags & TH_ACK)) {
307 register struct tcp_seq_hash *th;
308 const void *src, *dst;
309 register int rev;
310 struct tha tha;
311 /*
312 * Find (or record) the initial sequence numbers for
313 * this conversation. (we pick an arbitrary
314 * collating order so there's only one entry for
315 * both directions).
316 */
317 #ifdef INET6
318 memset(&tha, 0, sizeof(tha));
319 rev = 0;
320 if (ip6) {
321 src = &ip6->ip6_src;
322 dst = &ip6->ip6_dst;
323 if (sport > dport)
324 rev = 1;
325 else if (sport == dport) {
326 if (memcmp(src, dst, sizeof ip6->ip6_dst) > 0)
327 rev = 1;
328 }
329 if (rev) {
330 memcpy(&tha.src, dst, sizeof ip6->ip6_dst);
331 memcpy(&tha.dst, src, sizeof ip6->ip6_src);
332 tha.port = dport << 16 | sport;
333 } else {
334 memcpy(&tha.dst, dst, sizeof ip6->ip6_dst);
335 memcpy(&tha.src, src, sizeof ip6->ip6_src);
336 tha.port = sport << 16 | dport;
337 }
338 } else {
339 src = &ip->ip_src;
340 dst = &ip->ip_dst;
341 if (sport > dport)
342 rev = 1;
343 else if (sport == dport) {
344 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
345 rev = 1;
346 }
347 if (rev) {
348 memcpy(&tha.src, dst, sizeof ip->ip_dst);
349 memcpy(&tha.dst, src, sizeof ip->ip_src);
350 tha.port = dport << 16 | sport;
351 } else {
352 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
353 memcpy(&tha.src, src, sizeof ip->ip_src);
354 tha.port = sport << 16 | dport;
355 }
356 }
357 #else
358 rev = 0;
359 src = &ip->ip_src;
360 dst = &ip->ip_dst;
361 if (sport > dport)
362 rev = 1;
363 else if (sport == dport) {
364 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
365 rev = 1;
366 }
367 if (rev) {
368 memcpy(&tha.src, dst, sizeof ip->ip_dst);
369 memcpy(&tha.dst, src, sizeof ip->ip_src);
370 tha.port = dport << 16 | sport;
371 } else {
372 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
373 memcpy(&tha.src, src, sizeof ip->ip_src);
374 tha.port = sport << 16 | dport;
375 }
376 #endif
377
378 threv = rev;
379 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
380 th->nxt; th = th->nxt)
381 if (memcmp((char *)&tha, (char *)&th->addr,
382 sizeof(th->addr)) == 0)
383 break;
384
385 if (!th->nxt || (flags & TH_SYN)) {
386 /* didn't find it or new conversation */
387 if (th->nxt == NULL) {
388 th->nxt = (struct tcp_seq_hash *)
389 calloc(1, sizeof(*th));
390 if (th->nxt == NULL)
391 error("tcp_print: calloc");
392 }
393 th->addr = tha;
394 if (rev)
395 th->ack = seq, th->seq = ack - 1;
396 else
397 th->seq = seq, th->ack = ack - 1;
398 } else {
399 if (rev)
400 seq -= th->ack, ack -= th->seq;
401 else
402 seq -= th->seq, ack -= th->ack;
403 }
404
405 thseq = th->seq;
406 thack = th->ack;
407 } else {
408 /*fool gcc*/
409 thseq = thack = threv = 0;
410 }
411 if (hlen > length) {
412 (void)printf(" [bad hdr length %u - too long, > %u]",
413 hlen, length);
414 return;
415 }
416
417 if (IP_V(ip) == 4 && vflag && !fragmented) {
418 u_int16_t sum, tcp_sum;
419 if (TTEST2(tp->th_sport, length)) {
420 sum = tcp_cksum(ip, tp, length);
421
422 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
423 if (sum != 0) {
424 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
425 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum, sum));
426 } else
427 (void)printf(" (correct),");
428 }
429 }
430 #ifdef INET6
431 if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !fragmented) {
432 u_int16_t sum,tcp_sum;
433 if (TTEST2(tp->th_sport, length)) {
434 sum = tcp6_cksum(ip6, tp, length);
435 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
436 if (sum != 0) {
437 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
438 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum, sum));
439 } else
440 (void)printf(" (correct),");
441
442 }
443 }
444 #endif
445
446 length -= hlen;
447 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
448 (void)printf(" %u:%u(%u)", seq, seq + length, length);
449 if (flags & TH_ACK)
450 (void)printf(" ack %u", ack);
451
452 (void)printf(" win %d", win);
453
454 if (flags & TH_URG)
455 (void)printf(" urg %d", urp);
456 /*
457 * Handle any options.
458 */
459 if (hlen > sizeof(*tp)) {
460 register const u_char *cp;
461 register u_int i, opt, datalen;
462 register u_int len;
463
464 hlen -= sizeof(*tp);
465 cp = (const u_char *)tp + sizeof(*tp);
466 putchar(' ');
467 ch = '<';
468 while (hlen > 0) {
469 putchar(ch);
470 TCHECK(*cp);
471 opt = *cp++;
472 if (ZEROLENOPT(opt))
473 len = 1;
474 else {
475 TCHECK(*cp);
476 len = *cp++; /* total including type, len */
477 if (len < 2 || len > hlen)
478 goto bad;
479 --hlen; /* account for length byte */
480 }
481 --hlen; /* account for type byte */
482 datalen = 0;
483
484 /* Bail if "l" bytes of data are not left or were not captured */
485 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
486
487 switch (opt) {
488
489 case TCPOPT_MAXSEG:
490 (void)printf("mss");
491 datalen = 2;
492 LENCHECK(datalen);
493 (void)printf(" %u", EXTRACT_16BITS(cp));
494
495 break;
496
497 case TCPOPT_EOL:
498 (void)printf("eol");
499 break;
500
501 case TCPOPT_NOP:
502 (void)printf("nop");
503 break;
504
505 case TCPOPT_WSCALE:
506 (void)printf("wscale");
507 datalen = 1;
508 LENCHECK(datalen);
509 (void)printf(" %u", *cp);
510 break;
511
512 case TCPOPT_SACKOK:
513 (void)printf("sackOK");
514 break;
515
516 case TCPOPT_SACK:
517 (void)printf("sack");
518 datalen = len - 2;
519 if (datalen % 8 != 0) {
520 (void)printf(" malformed sack ");
521 } else {
522 u_int32_t s, e;
523
524 (void)printf(" sack %d ", datalen / 8);
525 for (i = 0; i < datalen; i += 8) {
526 LENCHECK(i + 4);
527 s = EXTRACT_32BITS(cp + i);
528 LENCHECK(i + 8);
529 e = EXTRACT_32BITS(cp + i + 4);
530 if (threv) {
531 s -= thseq;
532 e -= thseq;
533 } else {
534 s -= thack;
535 e -= thack;
536 }
537 (void)printf("{%u:%u}", s, e);
538 }
539 (void)printf(" ");
540 }
541 break;
542
543 case TCPOPT_ECHO:
544 (void)printf("echo");
545 datalen = 4;
546 LENCHECK(datalen);
547 (void)printf(" %u", EXTRACT_32BITS(cp));
548 break;
549
550 case TCPOPT_ECHOREPLY:
551 (void)printf("echoreply");
552 datalen = 4;
553 LENCHECK(datalen);
554 (void)printf(" %u", EXTRACT_32BITS(cp));
555 break;
556
557 case TCPOPT_TIMESTAMP:
558 (void)printf("timestamp");
559 datalen = 8;
560 LENCHECK(4);
561 (void)printf(" %u", EXTRACT_32BITS(cp));
562 LENCHECK(datalen);
563 (void)printf(" %u", EXTRACT_32BITS(cp + 4));
564 break;
565
566 case TCPOPT_CC:
567 (void)printf("cc");
568 datalen = 4;
569 LENCHECK(datalen);
570 (void)printf(" %u", EXTRACT_32BITS(cp));
571 break;
572
573 case TCPOPT_CCNEW:
574 (void)printf("ccnew");
575 datalen = 4;
576 LENCHECK(datalen);
577 (void)printf(" %u", EXTRACT_32BITS(cp));
578 break;
579
580 case TCPOPT_CCECHO:
581 (void)printf("ccecho");
582 datalen = 4;
583 LENCHECK(datalen);
584 (void)printf(" %u", EXTRACT_32BITS(cp));
585 break;
586
587 case TCPOPT_SIGNATURE:
588 (void)printf("md5:");
589 datalen = TCP_SIGLEN;
590 LENCHECK(datalen);
591 #ifdef HAVE_LIBCRYPTO
592 if (tcp_verify_signature(ip, tp,
593 bp + TH_OFF(tp) * 4, length, cp) == 0)
594 (void)printf("valid");
595 else
596 (void)printf("invalid");
597 #else
598 for (i = 0; i < TCP_SIGLEN; ++i)
599 (void)printf("%02x", cp[i]);
600 #endif
601 break;
602
603 default:
604 (void)printf("opt-%u:", opt);
605 datalen = len - 2;
606 for (i = 0; i < datalen; ++i) {
607 LENCHECK(i);
608 (void)printf("%02x", cp[i]);
609 }
610 break;
611 }
612
613 /* Account for data printed */
614 cp += datalen;
615 hlen -= datalen;
616
617 /* Check specification against observed length */
618 ++datalen; /* option octet */
619 if (!ZEROLENOPT(opt))
620 ++datalen; /* size octet */
621 if (datalen != len)
622 (void)printf("[len %d]", len);
623 ch = ',';
624 if (opt == TCPOPT_EOL)
625 break;
626 }
627 putchar('>');
628 }
629
630 if (length <= 0)
631 return;
632
633 /*
634 * Decode payload if necessary.
635 */
636 bp += TH_OFF(tp) * 4;
637 if (flags & TH_RST) {
638 if (vflag)
639 print_tcp_rst_data(bp, length);
640 } else {
641 if (sport == TELNET_PORT || dport == TELNET_PORT) {
642 if (!qflag && vflag)
643 telnet_print(bp, length);
644 } else if (sport == BGP_PORT || dport == BGP_PORT)
645 bgp_print(bp, length);
646 else if (sport == PPTP_PORT || dport == PPTP_PORT)
647 pptp_print(bp);
648 #ifdef TCPDUMP_DO_SMB
649 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
650 nbt_tcp_print(bp, length);
651 #endif
652 else if (sport == BEEP_PORT || dport == BEEP_PORT)
653 beep_print(bp, length);
654 else if (length > 2 &&
655 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT ||
656 sport == MULTICASTDNS_PORT || dport == MULTICASTDNS_PORT)) {
657 /*
658 * TCP DNS query has 2byte length at the head.
659 * XXX packet could be unaligned, it can go strange
660 */
661 ns_print(bp + 2, length - 2, 0);
662 } else if (sport == MSDP_PORT || dport == MSDP_PORT) {
663 msdp_print(bp, length);
664 }
665 else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) {
666 ldp_print(bp, length);
667 }
668 }
669 return;
670 bad:
671 fputs("[bad opt]", stdout);
672 if (ch != '\0')
673 putchar('>');
674 return;
675 trunc:
676 fputs("[|tcp]", stdout);
677 if (ch != '\0')
678 putchar('>');
679 }
680
681 /*
682 * RFC1122 says the following on data in RST segments:
683 *
684 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
685 *
686 * A TCP SHOULD allow a received RST segment to include data.
687 *
688 * DISCUSSION
689 * It has been suggested that a RST segment could contain
690 * ASCII text that encoded and explained the cause of the
691 * RST. No standard has yet been established for such
692 * data.
693 *
694 */
695
696 static void
697 print_tcp_rst_data(register const u_char *sp, u_int length)
698 {
699 int c;
700
701 if (TTEST2(*sp, length))
702 printf(" [RST");
703 else
704 printf(" [!RST");
705 if (length > MAX_RST_DATA_LEN) {
706 length = MAX_RST_DATA_LEN; /* can use -X for longer */
707 putchar('+'); /* indicate we truncate */
708 }
709 putchar(' ');
710 while (length-- && sp <= snapend) {
711 c = *sp++;
712 safeputchar(c);
713 }
714 putchar(']');
715 }
716
717 #ifdef HAVE_LIBCRYPTO
718 static int
719 tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
720 const u_char *data, int length, const u_char *rcvsig)
721 {
722 struct tcphdr tp1;
723 char sig[TCP_SIGLEN];
724 char zero_proto = 0;
725 MD5_CTX ctx;
726 u_int16_t savecsum, tlen;
727 struct ip6_hdr *ip6;
728 u_int32_t len32;
729 u_int8_t nxt;
730
731 tp1 = *tp;
732
733 if (tcpmd5secret == NULL)
734 return (-1);
735
736 MD5_Init(&ctx);
737 /*
738 * Step 1: Update MD5 hash with IP pseudo-header.
739 */
740 if (IP_V(ip) == 4) {
741 MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src));
742 MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst));
743 MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto));
744 MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p));
745 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
746 tlen = htons(tlen);
747 MD5_Update(&ctx, (char *)&tlen, sizeof(tlen));
748 } else if (IP_V(ip) == 6) {
749 ip6 = (struct ip6_hdr *)ip;
750 MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
751 MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
752 len32 = htonl(ntohs(ip6->ip6_plen));
753 MD5_Update(&ctx, (char *)&len32, sizeof(len32));
754 nxt = 0;
755 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
756 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
757 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
758 nxt = IPPROTO_TCP;
759 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
760 } else
761 return (-1);
762
763 /*
764 * Step 2: Update MD5 hash with TCP header, excluding options.
765 * The TCP checksum must be set to zero.
766 */
767 savecsum = tp1.th_sum;
768 tp1.th_sum = 0;
769 MD5_Update(&ctx, (char *)&tp1, sizeof(struct tcphdr));
770 tp1.th_sum = savecsum;
771 /*
772 * Step 3: Update MD5 hash with TCP segment data, if present.
773 */
774 if (length > 0)
775 MD5_Update(&ctx, data, length);
776 /*
777 * Step 4: Update MD5 hash with shared secret.
778 */
779 MD5_Update(&ctx, tcpmd5secret, strlen(tcpmd5secret));
780 MD5_Final(sig, &ctx);
781
782 return (memcmp(rcvsig, sig, 16));
783 }
784 #endif /* HAVE_LIBCRYPTO */