1 /* $NetBSD: print-tcp.c,v 1.9 2007/07/26 18:15:12 plunky Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
7 * Copyright (c) 1999-2004 The tcpdump.org project
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
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.
28 __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
35 #include <tcpdump-stdinc.h>
40 #include "interface.h"
41 #include "addrtoname.h"
57 #include <openssl/md5.h>
58 #include <signature.h>
60 static int tcp_verify_signature(netdissect_options
*ndo
,
61 const struct ip
*ip
, const struct tcphdr
*tp
,
62 const u_char
*data
, int length
, const u_char
*rcvsig
);
65 static void print_tcp_rst_data(netdissect_options
*, register const u_char
*sp
, u_int length
);
66 static void print_tcp_fastopen_option(netdissect_options
*ndo
, register const u_char
*cp
,
67 u_int datalen
, int exp
);
69 #define MAX_RST_DATA_LEN 30
79 struct tcp_seq_hash
*nxt
;
92 struct tcp_seq_hash6
{
93 struct tcp_seq_hash6
*nxt
;
100 #define TSEQ_HASHSIZE 919
102 /* These tcp optinos do not have the size octet */
103 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
105 static struct tcp_seq_hash tcp_seq_hash4
[TSEQ_HASHSIZE
];
107 static struct tcp_seq_hash6 tcp_seq_hash6
[TSEQ_HASHSIZE
];
110 static const struct tok tcp_flag_values
[] = {
122 static const struct tok tcp_option_values
[] = {
123 { TCPOPT_EOL
, "eol" },
124 { TCPOPT_NOP
, "nop" },
125 { TCPOPT_MAXSEG
, "mss" },
126 { TCPOPT_WSCALE
, "wscale" },
127 { TCPOPT_SACKOK
, "sackOK" },
128 { TCPOPT_SACK
, "sack" },
129 { TCPOPT_ECHO
, "echo" },
130 { TCPOPT_ECHOREPLY
, "echoreply" },
131 { TCPOPT_TIMESTAMP
, "TS" },
133 { TCPOPT_CCNEW
, "ccnew" },
134 { TCPOPT_CCECHO
, "" },
135 { TCPOPT_SIGNATURE
, "md5" },
136 { TCPOPT_AUTH
, "enhanced auth" },
137 { TCPOPT_UTO
, "uto" },
138 { TCPOPT_MPTCP
, "mptcp" },
139 { TCPOPT_FASTOPEN
, "tfo" },
140 { TCPOPT_EXPERIMENT2
, "exp" },
145 tcp_cksum(netdissect_options
*ndo
,
146 register const struct ip
*ip
,
147 register const struct tcphdr
*tp
,
150 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)tp
, len
, len
,
155 tcp_print(netdissect_options
*ndo
,
156 register const u_char
*bp
, register u_int length
,
157 register const u_char
*bp2
, int fragmented
)
159 register const struct tcphdr
*tp
;
160 register const struct ip
*ip
;
161 register u_char flags
;
164 uint16_t sport
, dport
, win
, urp
;
165 uint32_t seq
, ack
, thseq
, thack
;
170 register const struct ip6_hdr
*ip6
;
173 tp
= (const struct tcphdr
*)bp
;
174 ip
= (const struct ip
*)bp2
;
177 ip6
= (const struct ip6_hdr
*)bp2
;
182 if (!ND_TTEST(tp
->th_dport
)) {
183 ND_PRINT((ndo
, "%s > %s: [|tcp]",
184 ipaddr_string(ndo
, &ip
->ip_src
),
185 ipaddr_string(ndo
, &ip
->ip_dst
)));
189 sport
= EXTRACT_16BITS(&tp
->th_sport
);
190 dport
= EXTRACT_16BITS(&tp
->th_dport
);
192 hlen
= TH_OFF(tp
) * 4;
196 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
197 ND_PRINT((ndo
, "%s.%s > %s.%s: ",
198 ip6addr_string(ndo
, &ip6
->ip6_src
),
199 tcpport_string(sport
),
200 ip6addr_string(ndo
, &ip6
->ip6_dst
),
201 tcpport_string(dport
)));
203 ND_PRINT((ndo
, "%s > %s: ",
204 tcpport_string(sport
), tcpport_string(dport
)));
209 if (ip
->ip_p
== IPPROTO_TCP
) {
210 ND_PRINT((ndo
, "%s.%s > %s.%s: ",
211 ipaddr_string(ndo
, &ip
->ip_src
),
212 tcpport_string(sport
),
213 ipaddr_string(ndo
, &ip
->ip_dst
),
214 tcpport_string(dport
)));
216 ND_PRINT((ndo
, "%s > %s: ",
217 tcpport_string(sport
), tcpport_string(dport
)));
221 if (hlen
< sizeof(*tp
)) {
222 ND_PRINT((ndo
, " tcp %d [bad hdr length %u - too short, < %lu]",
223 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
)));
229 seq
= EXTRACT_32BITS(&tp
->th_seq
);
230 ack
= EXTRACT_32BITS(&tp
->th_ack
);
231 win
= EXTRACT_16BITS(&tp
->th_win
);
232 urp
= EXTRACT_16BITS(&tp
->th_urp
);
234 if (ndo
->ndo_qflag
) {
235 ND_PRINT((ndo
, "tcp %d", length
- hlen
));
237 ND_PRINT((ndo
, " [bad hdr length %u - too long, > %u]",
243 flags
= tp
->th_flags
;
244 ND_PRINT((ndo
, "Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
)));
246 if (!ndo
->ndo_Sflag
&& (flags
& TH_ACK
)) {
248 * Find (or record) the initial sequence numbers for
249 * this conversation. (we pick an arbitrary
250 * collating order so there's only one entry for
256 register struct tcp_seq_hash6
*th
;
257 struct tcp_seq_hash6
*tcp_seq_hash
;
258 const struct in6_addr
*src
, *dst
;
261 tcp_seq_hash
= tcp_seq_hash6
;
266 else if (sport
== dport
) {
267 if (UNALIGNED_MEMCMP(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
271 UNALIGNED_MEMCPY(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
272 UNALIGNED_MEMCPY(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
273 tha
.port
= dport
<< 16 | sport
;
275 UNALIGNED_MEMCPY(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
276 UNALIGNED_MEMCPY(&tha
.src
, src
, sizeof ip6
->ip6_src
);
277 tha
.port
= sport
<< 16 | dport
;
280 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
281 th
->nxt
; th
= th
->nxt
)
282 if (memcmp((char *)&tha
, (char *)&th
->addr
,
283 sizeof(th
->addr
)) == 0)
286 if (!th
->nxt
|| (flags
& TH_SYN
)) {
287 /* didn't find it or new conversation */
288 if (th
->nxt
== NULL
) {
289 th
->nxt
= (struct tcp_seq_hash6
*)
290 calloc(1, sizeof(*th
));
292 error("tcp_print: calloc");
296 th
->ack
= seq
, th
->seq
= ack
- 1;
298 th
->seq
= seq
, th
->ack
= ack
- 1;
301 seq
-= th
->ack
, ack
-= th
->seq
;
303 seq
-= th
->seq
, ack
-= th
->ack
;
312 register struct tcp_seq_hash
*th
;
313 struct tcp_seq_hash
*tcp_seq_hash
;
314 const struct in_addr
*src
, *dst
;
317 tcp_seq_hash
= tcp_seq_hash4
;
322 else if (sport
== dport
) {
323 if (UNALIGNED_MEMCMP(src
, dst
, sizeof ip
->ip_dst
) > 0)
327 UNALIGNED_MEMCPY(&tha
.src
, dst
, sizeof ip
->ip_dst
);
328 UNALIGNED_MEMCPY(&tha
.dst
, src
, sizeof ip
->ip_src
);
329 tha
.port
= dport
<< 16 | sport
;
331 UNALIGNED_MEMCPY(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
332 UNALIGNED_MEMCPY(&tha
.src
, src
, sizeof ip
->ip_src
);
333 tha
.port
= sport
<< 16 | dport
;
336 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
337 th
->nxt
; th
= th
->nxt
)
338 if (memcmp((char *)&tha
, (char *)&th
->addr
,
339 sizeof(th
->addr
)) == 0)
342 if (!th
->nxt
|| (flags
& TH_SYN
)) {
343 /* didn't find it or new conversation */
344 if (th
->nxt
== NULL
) {
345 th
->nxt
= (struct tcp_seq_hash
*)
346 calloc(1, sizeof(*th
));
348 error("tcp_print: calloc");
352 th
->ack
= seq
, th
->seq
= ack
- 1;
354 th
->seq
= seq
, th
->ack
= ack
- 1;
357 seq
-= th
->ack
, ack
-= th
->seq
;
359 seq
-= th
->seq
, ack
-= th
->ack
;
367 thseq
= thack
= rev
= 0;
370 ND_PRINT((ndo
, " [bad hdr length %u - too long, > %u]",
375 if (ndo
->ndo_vflag
&& !ndo
->ndo_Kflag
&& !fragmented
) {
376 /* Check the checksum, if possible. */
377 uint16_t sum
, tcp_sum
;
380 if (ND_TTEST2(tp
->th_sport
, length
)) {
381 sum
= tcp_cksum(ndo
, ip
, tp
, length
);
382 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
384 ND_PRINT((ndo
, ", cksum 0x%04x", tcp_sum
));
386 ND_PRINT((ndo
, " (incorrect -> 0x%04x)",
387 in_cksum_shouldbe(tcp_sum
, sum
)));
389 ND_PRINT((ndo
, " (correct)"));
393 else if (IP_V(ip
) == 6 && ip6
->ip6_plen
) {
394 if (ND_TTEST2(tp
->th_sport
, length
)) {
395 sum
= nextproto6_cksum(ip6
, (const uint8_t *)tp
,
396 length
, length
, IPPROTO_TCP
);
397 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
399 ND_PRINT((ndo
, ", cksum 0x%04x", tcp_sum
));
401 ND_PRINT((ndo
, " (incorrect -> 0x%04x)",
402 in_cksum_shouldbe(tcp_sum
, sum
)));
404 ND_PRINT((ndo
, " (correct)"));
412 if (ndo
->ndo_vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
413 ND_PRINT((ndo
, ", seq %u", seq
));
416 ND_PRINT((ndo
, ":%u", seq
+ length
));
420 if (flags
& TH_ACK
) {
421 ND_PRINT((ndo
, ", ack %u", ack
));
424 ND_PRINT((ndo
, ", win %d", win
));
427 ND_PRINT((ndo
, ", urg %d", urp
));
429 * Handle any options.
431 if (hlen
> sizeof(*tp
)) {
432 register const u_char
*cp
;
433 register u_int i
, opt
, datalen
;
437 cp
= (const u_char
*)tp
+ sizeof(*tp
);
438 ND_PRINT((ndo
, ", options ["));
441 ND_PRINT((ndo
, "%c", ch
));
448 len
= *cp
++; /* total including type, len */
449 if (len
< 2 || len
> hlen
)
451 --hlen
; /* account for length byte */
453 --hlen
; /* account for type byte */
456 /* Bail if "l" bytes of data are not left or were not captured */
457 #define LENCHECK(l) { if ((l) > hlen) goto bad; ND_TCHECK2(*cp, l); }
460 ND_PRINT((ndo
, "%s", tok2str(tcp_option_values
, "unknown-%u", opt
)));
467 ND_PRINT((ndo
, " %u", EXTRACT_16BITS(cp
)));
473 ND_PRINT((ndo
, " %u", *cp
));
478 if (datalen
% 8 != 0) {
479 ND_PRINT((ndo
, "malformed sack"));
483 ND_PRINT((ndo
, " %d ", datalen
/ 8));
484 for (i
= 0; i
< datalen
; i
+= 8) {
486 s
= EXTRACT_32BITS(cp
+ i
);
488 e
= EXTRACT_32BITS(cp
+ i
+ 4);
496 ND_PRINT((ndo
, "{%u:%u}", s
, e
));
505 case TCPOPT_ECHOREPLY
:
508 * those options share their semantics.
513 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
516 case TCPOPT_TIMESTAMP
:
519 ND_PRINT((ndo
, " val %u ecr %u",
521 EXTRACT_32BITS(cp
+ 4)));
524 case TCPOPT_SIGNATURE
:
525 datalen
= TCP_SIGLEN
;
527 #ifdef HAVE_LIBCRYPTO
528 switch (tcp_verify_signature(ndo
, ip
, tp
,
529 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
531 case SIGNATURE_VALID
:
532 ND_PRINT((ndo
, "valid"));
535 case SIGNATURE_INVALID
:
536 ND_PRINT((ndo
, "invalid"));
539 case CANT_CHECK_SIGNATURE
:
540 ND_PRINT((ndo
, "can't check - "));
541 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
542 ND_PRINT((ndo
, "%02x", cp
[i
]));
546 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
547 ND_PRINT((ndo
, "%02x", cp
[i
]));
552 ND_PRINT((ndo
, "keyid %d", *cp
++));
554 for (i
= 0; i
< datalen
; ++i
) {
556 ND_PRINT((ndo
, "%02x", cp
[i
]));
565 * Nothing interesting.
573 utoval
= EXTRACT_16BITS(cp
);
574 ND_PRINT((ndo
, "0x%x", utoval
));
576 utoval
= (utoval
>> 1) * 60;
579 ND_PRINT((ndo
, " %u", utoval
));
585 if (!mptcp_print(ndo
, cp
-2, len
, flags
))
589 case TCPOPT_FASTOPEN
:
592 print_tcp_fastopen_option(ndo
, cp
, datalen
, FALSE
);
595 case TCPOPT_EXPERIMENT2
:
601 magic
= EXTRACT_16BITS(cp
);
602 ND_PRINT((ndo
, "-"));
606 case 0xf989: /* TCP Fast Open RFC 7413 */
607 print_tcp_fastopen_option(ndo
, cp
+ 2, datalen
- 2, TRUE
);
611 /* Unknown magic number */
612 ND_PRINT((ndo
, "%04x", magic
));
620 ND_PRINT((ndo
, " 0x"));
621 for (i
= 0; i
< datalen
; ++i
) {
623 ND_PRINT((ndo
, "%02x", cp
[i
]));
628 /* Account for data printed */
632 /* Check specification against observed length */
633 ++datalen
; /* option octet */
634 if (!ZEROLENOPT(opt
))
635 ++datalen
; /* size octet */
637 ND_PRINT((ndo
, "[len %d]", len
));
639 if (opt
== TCPOPT_EOL
)
642 ND_PRINT((ndo
, "]"));
646 * Print length field before crawling down the stack.
648 ND_PRINT((ndo
, ", length %u", length
));
654 * Decode payload if necessary.
656 bp
+= TH_OFF(tp
) * 4;
657 if ((flags
& TH_RST
) && ndo
->ndo_vflag
) {
658 print_tcp_rst_data(ndo
, bp
, length
);
662 if (ndo
->ndo_packettype
) {
663 switch (ndo
->ndo_packettype
) {
665 zmtp1_print(ndo
, bp
, length
);
671 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
672 telnet_print(ndo
, bp
, length
);
673 } else if (sport
== SMTP_PORT
|| dport
== SMTP_PORT
) {
674 ND_PRINT((ndo
, ": "));
675 smtp_print(ndo
, bp
, length
);
676 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
677 bgp_print(ndo
, bp
, length
);
678 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
680 #ifdef TCPDUMP_DO_SMB
681 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
682 nbt_tcp_print(ndo
, bp
, length
);
683 else if (sport
== SMB_PORT
|| dport
== SMB_PORT
)
684 smb_tcp_print(ndo
, bp
, length
);
686 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
687 beep_print(ndo
, bp
, length
);
688 else if (sport
== OPENFLOW_PORT_OLD
|| dport
== OPENFLOW_PORT_OLD
||
689 sport
== OPENFLOW_PORT_IANA
|| dport
== OPENFLOW_PORT_IANA
)
690 openflow_print(ndo
, bp
, length
);
691 else if (sport
== FTP_PORT
|| dport
== FTP_PORT
) {
692 ND_PRINT((ndo
, ": "));
693 ftp_print(ndo
, bp
, length
);
694 } else if (sport
== HTTP_PORT
|| dport
== HTTP_PORT
||
695 sport
== HTTP_PORT_ALT
|| dport
== HTTP_PORT_ALT
) {
696 ND_PRINT((ndo
, ": "));
697 http_print(ndo
, bp
, length
);
698 } else if (sport
== RTSP_PORT
|| dport
== RTSP_PORT
||
699 sport
== RTSP_PORT_ALT
|| dport
== RTSP_PORT_ALT
) {
700 ND_PRINT((ndo
, ": "));
701 rtsp_print(ndo
, bp
, length
);
702 } else if (length
> 2 &&
703 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
||
704 sport
== MULTICASTDNS_PORT
|| dport
== MULTICASTDNS_PORT
)) {
706 * TCP DNS query has 2byte length at the head.
707 * XXX packet could be unaligned, it can go strange
709 ns_print(ndo
, bp
+ 2, length
- 2, 0);
710 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
711 msdp_print(ndo
, bp
, length
);
712 } else if (sport
== RPKI_RTR_PORT
|| dport
== RPKI_RTR_PORT
) {
713 rpki_rtr_print(ndo
, bp
, length
);
715 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
716 ldp_print(ndo
, bp
, length
);
718 else if ((sport
== NFS_PORT
|| dport
== NFS_PORT
) &&
719 length
>= 4 && ND_TTEST2(*bp
, 4)) {
721 * If data present, header length valid, and NFS port used,
723 * Pass offset of data plus 4 bytes for RPC TCP msg length
724 * to NFS print routines.
727 register const struct sunrpc_msg
*rp
;
728 enum sunrpc_msg_type direction
;
730 fraglen
= EXTRACT_32BITS(bp
) & 0x7FFFFFFF;
731 if (fraglen
> (length
) - 4)
732 fraglen
= (length
) - 4;
733 rp
= (const struct sunrpc_msg
*)(bp
+ 4);
734 if (ND_TTEST(rp
->rm_direction
)) {
735 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
736 if (dport
== NFS_PORT
&& direction
== SUNRPC_CALL
) {
737 ND_PRINT((ndo
, ": NFS request xid %u ", EXTRACT_32BITS(&rp
->rm_xid
)));
738 nfsreq_print_noaddr(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
741 if (sport
== NFS_PORT
&& direction
== SUNRPC_REPLY
) {
742 ND_PRINT((ndo
, ": NFS reply xid %u ", EXTRACT_32BITS(&rp
->rm_xid
)));
743 nfsreply_print_noaddr(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
751 ND_PRINT((ndo
, "[bad opt]"));
753 ND_PRINT((ndo
, ">"));
756 ND_PRINT((ndo
, "[|tcp]"));
758 ND_PRINT((ndo
, ">"));
762 * RFC1122 says the following on data in RST segments:
764 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
766 * A TCP SHOULD allow a received RST segment to include data.
769 * It has been suggested that a RST segment could contain
770 * ASCII text that encoded and explained the cause of the
771 * RST. No standard has yet been established for such
777 print_tcp_rst_data(netdissect_options
*ndo
,
778 register const u_char
*sp
, u_int length
)
782 ND_PRINT((ndo
, ND_TTEST2(*sp
, length
) ? " [RST" : " [!RST"));
783 if (length
> MAX_RST_DATA_LEN
) {
784 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
785 ND_PRINT((ndo
, "+")); /* indicate we truncate */
787 ND_PRINT((ndo
, " "));
788 while (length
-- && sp
<= ndo
->ndo_snapend
) {
792 ND_PRINT((ndo
, "]"));
796 print_tcp_fastopen_option(netdissect_options
*ndo
, register const u_char
*cp
,
797 u_int datalen
, int exp
)
802 ND_PRINT((ndo
, "tfo"));
805 /* Fast Open Cookie Request */
806 ND_PRINT((ndo
, " cookiereq"));
808 /* Fast Open Cookie */
809 if (datalen
% 2 != 0 || datalen
< 4 || datalen
> 16) {
810 ND_PRINT((ndo
, " malformed"));
812 ND_PRINT((ndo
, " cookie "));
813 for (i
= 0; i
< datalen
; ++i
)
814 ND_PRINT((ndo
, "%02x", cp
[i
]));
819 #ifdef HAVE_LIBCRYPTO
820 USES_APPLE_DEPRECATED_API
822 tcp_verify_signature(netdissect_options
*ndo
,
823 const struct ip
*ip
, const struct tcphdr
*tp
,
824 const u_char
*data
, int length
, const u_char
*rcvsig
)
827 u_char sig
[TCP_SIGLEN
];
830 uint16_t savecsum
, tlen
;
832 const struct ip6_hdr
*ip6
;
837 if (data
+ length
> ndo
->ndo_snapend
) {
838 ND_PRINT((ndo
, "snaplen too short, "));
839 return (CANT_CHECK_SIGNATURE
);
844 if (ndo
->ndo_sigsecret
== NULL
) {
845 ND_PRINT((ndo
, "shared secret not supplied with -M, "));
846 return (CANT_CHECK_SIGNATURE
);
851 * Step 1: Update MD5 hash with IP pseudo-header.
854 MD5_Update(&ctx
, (const char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
855 MD5_Update(&ctx
, (const char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
856 MD5_Update(&ctx
, (const char *)&zero_proto
, sizeof(zero_proto
));
857 MD5_Update(&ctx
, (const char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
858 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
860 MD5_Update(&ctx
, (const char *)&tlen
, sizeof(tlen
));
862 } else if (IP_V(ip
) == 6) {
863 ip6
= (const struct ip6_hdr
*)ip
;
864 MD5_Update(&ctx
, (const char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
865 MD5_Update(&ctx
, (const char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
866 len32
= htonl(EXTRACT_16BITS(&ip6
->ip6_plen
));
867 MD5_Update(&ctx
, (const char *)&len32
, sizeof(len32
));
869 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
870 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
871 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
873 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
877 ND_PRINT((ndo
, "IP version not 4 or 6, "));
879 ND_PRINT((ndo
, "IP version not 4, "));
881 return (CANT_CHECK_SIGNATURE
);
885 * Step 2: Update MD5 hash with TCP header, excluding options.
886 * The TCP checksum must be set to zero.
888 savecsum
= tp1
.th_sum
;
890 MD5_Update(&ctx
, (const char *)&tp1
, sizeof(struct tcphdr
));
891 tp1
.th_sum
= savecsum
;
893 * Step 3: Update MD5 hash with TCP segment data, if present.
896 MD5_Update(&ctx
, data
, length
);
898 * Step 4: Update MD5 hash with shared secret.
900 MD5_Update(&ctx
, ndo
->ndo_sigsecret
, strlen(ndo
->ndo_sigsecret
));
901 MD5_Final(sig
, &ctx
);
903 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
904 return (SIGNATURE_VALID
);
906 return (SIGNATURE_INVALID
);
909 #endif /* HAVE_LIBCRYPTO */
913 * c-style: whitesmith