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