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.
26 /* \summary: TCP printer */
30 __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
37 #include "netdissect-stdinc.h"
42 #define ND_LONGJMP_FROM_TCHECK
43 #include "netdissect.h"
44 #include "addrtoname.h"
56 #include <openssl/md5.h>
57 #include "signature.h"
59 static int tcp_verify_signature(netdissect_options
*ndo
,
60 const struct ip
*ip
, const struct tcphdr
*tp
,
61 const u_char
*data
, u_int length
, const u_char
*rcvsig
);
64 static void print_tcp_rst_data(netdissect_options
*, const u_char
*sp
, u_int length
);
65 static void print_tcp_fastopen_option(netdissect_options
*ndo
, const u_char
*cp
,
68 #define MAX_RST_DATA_LEN 30
78 struct tcp_seq_hash
*nxt
;
90 struct tcp_seq_hash6
{
91 struct tcp_seq_hash6
*nxt
;
97 #define TSEQ_HASHSIZE 919
99 /* These tcp options do not have the size octet */
100 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
102 static struct tcp_seq_hash tcp_seq_hash4
[TSEQ_HASHSIZE
];
103 static struct tcp_seq_hash6 tcp_seq_hash6
[TSEQ_HASHSIZE
];
105 const struct tok tcp_flag_values
[] = {
117 static const struct tok tcp_option_values
[] = {
118 { TCPOPT_EOL
, "eol" },
119 { TCPOPT_NOP
, "nop" },
120 { TCPOPT_MAXSEG
, "mss" },
121 { TCPOPT_WSCALE
, "wscale" },
122 { TCPOPT_SACKOK
, "sackOK" },
123 { TCPOPT_SACK
, "sack" },
124 { TCPOPT_ECHO
, "echo" },
125 { TCPOPT_ECHOREPLY
, "echoreply" },
126 { TCPOPT_TIMESTAMP
, "TS" },
128 { TCPOPT_CCNEW
, "ccnew" },
129 { TCPOPT_CCECHO
, "" },
130 { TCPOPT_SIGNATURE
, "md5" },
131 { TCPOPT_SCPS
, "scps" },
132 { TCPOPT_UTO
, "uto" },
133 { TCPOPT_TCPAO
, "tcp-ao" },
134 { TCPOPT_MPTCP
, "mptcp" },
135 { TCPOPT_FASTOPEN
, "tfo" },
136 { TCPOPT_EXPERIMENT2
, "exp" },
141 tcp_cksum(netdissect_options
*ndo
,
143 const struct tcphdr
*tp
,
146 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)tp
, len
, len
,
151 tcp6_cksum(netdissect_options
*ndo
,
152 const struct ip6_hdr
*ip6
,
153 const struct tcphdr
*tp
,
156 return nextproto6_cksum(ndo
, ip6
, (const uint8_t *)tp
, len
, len
,
161 tcp_print(netdissect_options
*ndo
,
162 const u_char
*bp
, u_int length
,
163 const u_char
*bp2
, int fragmented
)
165 const struct tcphdr
*tp
;
170 uint16_t sport
, dport
, win
, urp
;
171 uint32_t seq
, ack
, thseq
, thack
;
175 const struct ip6_hdr
*ip6
;
177 ndo
->ndo_protocol
= "tcp";
178 tp
= (const struct tcphdr
*)bp
;
179 ip
= (const struct ip
*)bp2
;
181 ip6
= (const struct ip6_hdr
*)bp2
;
185 if (!ND_TTEST_2(tp
->th_dport
)) {
188 GET_IP6ADDR_STRING(ip6
->ip6_src
),
189 GET_IP6ADDR_STRING(ip6
->ip6_dst
));
192 GET_IPADDR_STRING(ip
->ip_src
),
193 GET_IPADDR_STRING(ip
->ip_dst
));
195 nd_trunc_longjmp(ndo
);
198 sport
= GET_BE_U_2(tp
->th_sport
);
199 dport
= GET_BE_U_2(tp
->th_dport
);
202 if (GET_U_1(ip6
->ip6_nxt
) == IPPROTO_TCP
) {
203 ND_PRINT("%s.%s > %s.%s: ",
204 GET_IP6ADDR_STRING(ip6
->ip6_src
),
205 tcpport_string(ndo
, sport
),
206 GET_IP6ADDR_STRING(ip6
->ip6_dst
),
207 tcpport_string(ndo
, dport
));
209 ND_PRINT("%s > %s: ",
210 tcpport_string(ndo
, sport
), tcpport_string(ndo
, dport
));
213 if (GET_U_1(ip
->ip_p
) == IPPROTO_TCP
) {
214 ND_PRINT("%s.%s > %s.%s: ",
215 GET_IPADDR_STRING(ip
->ip_src
),
216 tcpport_string(ndo
, sport
),
217 GET_IPADDR_STRING(ip
->ip_dst
),
218 tcpport_string(ndo
, dport
));
220 ND_PRINT("%s > %s: ",
221 tcpport_string(ndo
, sport
), tcpport_string(ndo
, dport
));
225 hlen
= TH_OFF(tp
) * 4;
227 if (hlen
< sizeof(*tp
)) {
228 ND_PRINT(" tcp %u [bad hdr length %u - too short, < %zu]",
229 length
- hlen
, hlen
, sizeof(*tp
));
233 seq
= GET_BE_U_4(tp
->th_seq
);
234 ack
= GET_BE_U_4(tp
->th_ack
);
235 win
= GET_BE_U_2(tp
->th_win
);
236 urp
= GET_BE_U_2(tp
->th_urp
);
238 if (ndo
->ndo_qflag
) {
239 ND_PRINT("tcp %u", length
- hlen
);
241 ND_PRINT(" [bad hdr length %u - too long, > %u]",
248 flags
= GET_U_1(tp
->th_flags
);
249 ND_PRINT("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
251 if (!ndo
->ndo_Sflag
&& (flags
& TH_ACK
)) {
253 * Find (or record) the initial sequence numbers for
254 * this conversation. (we pick an arbitrary
255 * collating order so there's only one entry for
260 struct tcp_seq_hash6
*th
;
261 struct tcp_seq_hash6
*tcp_seq_hash
;
262 const void *src
, *dst
;
265 tcp_seq_hash
= tcp_seq_hash6
;
266 src
= (const void *)ip6
->ip6_src
;
267 dst
= (const void *)ip6
->ip6_dst
;
270 else if (sport
== dport
) {
271 if (UNALIGNED_MEMCMP(src
, dst
, sizeof(ip6
->ip6_dst
)) > 0)
275 UNALIGNED_MEMCPY(&tha
.src
, dst
, sizeof(ip6
->ip6_dst
));
276 UNALIGNED_MEMCPY(&tha
.dst
, src
, sizeof(ip6
->ip6_src
));
277 tha
.port
= ((u_int
)dport
) << 16 | sport
;
279 UNALIGNED_MEMCPY(&tha
.dst
, dst
, sizeof(ip6
->ip6_dst
));
280 UNALIGNED_MEMCPY(&tha
.src
, src
, sizeof(ip6
->ip6_src
));
281 tha
.port
= ((u_int
)sport
) << 16 | dport
;
284 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
285 th
->nxt
; th
= th
->nxt
)
286 if (memcmp((char *)&tha
, (char *)&th
->addr
,
287 sizeof(th
->addr
)) == 0)
290 if (!th
->nxt
|| (flags
& TH_SYN
)) {
291 /* didn't find it or new conversation */
292 /* calloc() return used by the 'tcp_seq_hash6'
293 hash table: do not free() */
294 if (th
->nxt
== NULL
) {
295 th
->nxt
= (struct tcp_seq_hash6
*)
296 calloc(1, sizeof(*th
));
298 (*ndo
->ndo_error
)(ndo
,
300 "%s: calloc", __func__
);
304 th
->ack
= seq
, th
->seq
= ack
- 1;
306 th
->seq
= seq
, th
->ack
= ack
- 1;
309 seq
-= th
->ack
, ack
-= th
->seq
;
311 seq
-= th
->seq
, ack
-= th
->ack
;
317 struct tcp_seq_hash
*th
;
318 struct tcp_seq_hash
*tcp_seq_hash
;
321 tcp_seq_hash
= tcp_seq_hash4
;
324 else if (sport
== dport
) {
325 if (UNALIGNED_MEMCMP(ip
->ip_src
, ip
->ip_dst
, sizeof(ip
->ip_dst
)) > 0)
329 UNALIGNED_MEMCPY(&tha
.src
, ip
->ip_dst
,
331 UNALIGNED_MEMCPY(&tha
.dst
, ip
->ip_src
,
333 tha
.port
= ((u_int
)dport
) << 16 | sport
;
335 UNALIGNED_MEMCPY(&tha
.dst
, ip
->ip_dst
,
337 UNALIGNED_MEMCPY(&tha
.src
, ip
->ip_src
,
339 tha
.port
= ((u_int
)sport
) << 16 | dport
;
342 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
343 th
->nxt
; th
= th
->nxt
)
344 if (memcmp((char *)&tha
, (char *)&th
->addr
,
345 sizeof(th
->addr
)) == 0)
348 if (!th
->nxt
|| (flags
& TH_SYN
)) {
349 /* didn't find it or new conversation */
350 /* calloc() return used by the 'tcp_seq_hash4'
351 hash table: do not free() */
352 if (th
->nxt
== NULL
) {
353 th
->nxt
= (struct tcp_seq_hash
*)
354 calloc(1, sizeof(*th
));
356 (*ndo
->ndo_error
)(ndo
,
358 "%s: calloc", __func__
);
362 th
->ack
= seq
, th
->seq
= ack
- 1;
364 th
->seq
= seq
, th
->ack
= ack
- 1;
367 seq
-= th
->ack
, ack
-= th
->seq
;
369 seq
-= th
->seq
, ack
-= th
->ack
;
377 thseq
= thack
= rev
= 0;
380 ND_PRINT(" [bad hdr length %u - too long, > %u]",
385 if (ndo
->ndo_vflag
&& !ndo
->ndo_Kflag
&& !fragmented
) {
386 /* Check the checksum, if possible. */
387 uint16_t sum
, tcp_sum
;
390 if (ND_TTEST_LEN(tp
->th_sport
, length
)) {
391 sum
= tcp_cksum(ndo
, ip
, tp
, length
);
392 tcp_sum
= GET_BE_U_2(tp
->th_sum
);
394 ND_PRINT(", cksum 0x%04x", tcp_sum
);
396 ND_PRINT(" (incorrect -> 0x%04x)",
397 in_cksum_shouldbe(tcp_sum
, sum
));
399 ND_PRINT(" (correct)");
401 } else if (IP_V(ip
) == 6) {
402 if (ND_TTEST_LEN(tp
->th_sport
, length
)) {
403 sum
= tcp6_cksum(ndo
, ip6
, tp
, length
);
404 tcp_sum
= GET_BE_U_2(tp
->th_sum
);
406 ND_PRINT(", cksum 0x%04x", tcp_sum
);
408 ND_PRINT(" (incorrect -> 0x%04x)",
409 in_cksum_shouldbe(tcp_sum
, sum
));
411 ND_PRINT(" (correct)");
418 if (ndo
->ndo_vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
419 ND_PRINT(", seq %u", seq
);
422 ND_PRINT(":%u", seq
+ length
);
426 if (flags
& TH_ACK
) {
427 ND_PRINT(", ack %u", ack
);
430 ND_PRINT(", win %u", win
);
433 ND_PRINT(", urg %u", urp
);
435 * Handle any options.
437 if (hlen
> sizeof(*tp
)) {
439 u_int i
, opt
, datalen
;
443 cp
= (const u_char
*)tp
+ sizeof(*tp
);
444 ND_PRINT(", options [");
454 cp
++; /* total including type, len */
455 if (len
< 2 || len
> hlen
)
457 --hlen
; /* account for length byte */
459 --hlen
; /* account for type byte */
462 /* Bail if "l" bytes of data are not left or were not captured */
463 #define LENCHECK(l) { if ((l) > hlen) goto bad; }
466 ND_PRINT("%s", tok2str(tcp_option_values
, "unknown-%u", opt
));
473 ND_PRINT(" %u", GET_BE_U_2(cp
));
479 ND_PRINT(" %u", GET_U_1(cp
));
484 if (datalen
% 8 != 0) {
485 ND_PRINT(" invalid sack");
489 ND_PRINT(" %u ", datalen
/ 8);
490 for (i
= 0; i
< datalen
; i
+= 8) {
492 s
= GET_BE_U_4(cp
+ i
);
494 e
= GET_BE_U_4(cp
+ i
+ 4);
502 ND_PRINT("{%u:%u}", s
, e
);
511 case TCPOPT_ECHOREPLY
:
514 * those options share their semantics.
519 ND_PRINT(" %u", GET_BE_U_4(cp
));
522 case TCPOPT_TIMESTAMP
:
525 ND_PRINT(" val %u ecr %u",
530 case TCPOPT_SIGNATURE
:
531 datalen
= TCP_SIGLEN
;
533 ND_TCHECK_LEN(cp
, datalen
);
535 #ifdef HAVE_LIBCRYPTO
536 switch (tcp_verify_signature(ndo
, ip
, tp
,
537 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
539 case SIGNATURE_VALID
:
543 case SIGNATURE_INVALID
:
544 nd_print_invalid(ndo
);
547 case CANT_CHECK_SIGNATURE
:
548 ND_PRINT("can't check - ");
549 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
555 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
556 ND_PRINT("%02x", GET_U_1(cp
+ i
));
563 ND_PRINT(" cap %02x id %u", GET_U_1(cp
),
569 /* RFC 5925 Section 2.2:
570 * "The Length value MUST be greater than or equal to 4."
571 * (This includes the Kind and Length fields already processed
575 nd_print_invalid(ndo
);
578 ND_PRINT(" keyid %u", GET_U_1(cp
));
580 ND_PRINT(" rnextkeyid %u",
584 for (i
= 2; i
< datalen
; i
++) {
597 * Nothing interesting.
605 utoval
= GET_BE_U_2(cp
);
606 ND_PRINT(" 0x%x", utoval
);
608 utoval
= (utoval
>> 1) * 60;
611 ND_PRINT(" %u", utoval
);
617 /* FIXME: Proof-read mptcp_print() and if it
618 * always covers all bytes when it returns 1,
619 * only do ND_TCHECK_LEN() if it returned 0.
621 ND_TCHECK_LEN(cp
, datalen
);
622 if (!mptcp_print(ndo
, cp
-2, len
, flags
))
626 case TCPOPT_FASTOPEN
:
629 ND_TCHECK_LEN(cp
, datalen
);
631 print_tcp_fastopen_option(ndo
, cp
, datalen
);
634 case TCPOPT_EXPERIMENT2
:
637 ND_TCHECK_LEN(cp
, datalen
);
641 magic
= GET_BE_U_2(cp
);
646 case 0xf989: /* TCP Fast Open RFC 7413 */
648 print_tcp_fastopen_option(ndo
, cp
+ 2, datalen
- 2);
652 /* Unknown magic number */
653 ND_PRINT("%04x", magic
);
662 for (i
= 0; i
< datalen
; ++i
) {
664 ND_PRINT("%02x", GET_U_1(cp
+ i
));
669 /* Account for data printed */
673 /* Check specification against observed length */
674 ++datalen
; /* option octet */
675 if (!ZEROLENOPT(opt
))
676 ++datalen
; /* size octet */
678 ND_PRINT("[len %u]", len
);
680 if (opt
== TCPOPT_EOL
)
687 * Print length field before crawling down the stack.
689 ND_PRINT(", length %u", length
);
695 * Decode payload if necessary.
697 bp
+= TH_OFF(tp
) * 4;
698 if ((flags
& TH_RST
) && ndo
->ndo_vflag
) {
699 print_tcp_rst_data(ndo
, bp
, length
);
703 if (ndo
->ndo_packettype
) {
704 switch (ndo
->ndo_packettype
) {
706 zmtp1_print(ndo
, bp
, length
);
709 resp_print(ndo
, bp
, length
);
712 /* over_tcp: TRUE, is_mdns: FALSE */
713 domain_print(ndo
, bp
, length
, TRUE
, FALSE
);
719 if (IS_SRC_OR_DST_PORT(TELNET_PORT
)) {
720 telnet_print(ndo
, bp
, length
);
721 } else if (IS_SRC_OR_DST_PORT(SMTP_PORT
)) {
723 smtp_print(ndo
, bp
, length
);
724 } else if (IS_SRC_OR_DST_PORT(WHOIS_PORT
)) {
726 ndo
->ndo_protocol
= "whois"; /* needed by txtproto_print() */
727 txtproto_print(ndo
, bp
, length
, NULL
, 0); /* RFC 3912 */
728 } else if (IS_SRC_OR_DST_PORT(BGP_PORT
))
729 bgp_print(ndo
, bp
, length
);
730 else if (IS_SRC_OR_DST_PORT(PPTP_PORT
))
732 else if (IS_SRC_OR_DST_PORT(REDIS_PORT
))
733 resp_print(ndo
, bp
, length
);
734 else if (IS_SRC_OR_DST_PORT(SSH_PORT
))
735 ssh_print(ndo
, bp
, length
);
737 else if (IS_SRC_OR_DST_PORT(NETBIOS_SSN_PORT
))
738 nbt_tcp_print(ndo
, bp
, length
);
739 else if (IS_SRC_OR_DST_PORT(SMB_PORT
))
740 smb_tcp_print(ndo
, bp
, length
);
742 else if (IS_SRC_OR_DST_PORT(BEEP_PORT
))
743 beep_print(ndo
, bp
, length
);
744 else if (IS_SRC_OR_DST_PORT(OPENFLOW_PORT_OLD
) || IS_SRC_OR_DST_PORT(OPENFLOW_PORT_IANA
))
745 openflow_print(ndo
, bp
, length
);
746 else if (IS_SRC_OR_DST_PORT(FTP_PORT
)) {
748 ftp_print(ndo
, bp
, length
);
749 } else if (IS_SRC_OR_DST_PORT(HTTP_PORT
) || IS_SRC_OR_DST_PORT(HTTP_PORT_ALT
)) {
751 http_print(ndo
, bp
, length
);
752 } else if (IS_SRC_OR_DST_PORT(RTSP_PORT
) || IS_SRC_OR_DST_PORT(RTSP_PORT_ALT
)) {
754 rtsp_print(ndo
, bp
, length
);
755 } else if (IS_SRC_OR_DST_PORT(NAMESERVER_PORT
)) {
756 /* over_tcp: TRUE, is_mdns: FALSE */
757 domain_print(ndo
, bp
, length
, TRUE
, FALSE
);
758 } else if (IS_SRC_OR_DST_PORT(MSDP_PORT
)) {
759 msdp_print(ndo
, bp
, length
);
760 } else if (IS_SRC_OR_DST_PORT(RPKI_RTR_PORT
)) {
761 rpki_rtr_print(ndo
, bp
, length
);
762 } else if (IS_SRC_OR_DST_PORT(LDP_PORT
)) {
763 ldp_print(ndo
, bp
, length
);
764 } else if ((IS_SRC_OR_DST_PORT(NFS_PORT
)) &&
767 * If data present, header length valid, and NFS port used,
769 * Pass offset of data plus 4 bytes for RPC TCP msg length
770 * to NFS print routines.
773 const struct sunrpc_msg
*rp
;
774 enum sunrpc_msg_type direction
;
776 fraglen
= GET_BE_U_4(bp
) & 0x7FFFFFFF;
777 if (fraglen
> (length
) - 4)
778 fraglen
= (length
) - 4;
779 rp
= (const struct sunrpc_msg
*)(bp
+ 4);
780 if (ND_TTEST_4(rp
->rm_direction
)) {
781 direction
= (enum sunrpc_msg_type
) GET_BE_U_4(rp
->rm_direction
);
782 if (dport
== NFS_PORT
&& direction
== SUNRPC_CALL
) {
783 ND_PRINT(": NFS request xid %u ",
784 GET_BE_U_4(rp
->rm_xid
));
785 nfsreq_noaddr_print(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
788 if (sport
== NFS_PORT
&& direction
== SUNRPC_REPLY
) {
789 ND_PRINT(": NFS reply xid %u ",
790 GET_BE_U_4(rp
->rm_xid
));
791 nfsreply_noaddr_print(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
799 ND_PRINT("[bad opt]");
804 nd_print_invalid(ndo
);
808 * RFC1122 says the following on data in RST segments:
810 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
812 * A TCP SHOULD allow a received RST segment to include data.
815 * It has been suggested that a RST segment could contain
816 * ASCII text that encoded and explained the cause of the
817 * RST. No standard has yet been established for such
823 print_tcp_rst_data(netdissect_options
*ndo
,
824 const u_char
*sp
, u_int length
)
826 ND_PRINT(ND_TTEST_LEN(sp
, length
) ? " [RST" : " [!RST");
827 if (length
> MAX_RST_DATA_LEN
) {
828 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
829 ND_PRINT("+"); /* indicate we truncate */
832 (void)nd_printn(ndo
, sp
, length
, ndo
->ndo_snapend
);
837 print_tcp_fastopen_option(netdissect_options
*ndo
, const u_char
*cp
,
843 /* Fast Open Cookie Request */
844 ND_PRINT(" cookiereq");
846 /* Fast Open Cookie */
847 if (datalen
% 2 != 0 || datalen
< 4 || datalen
> 16) {
848 nd_print_invalid(ndo
);
850 ND_PRINT(" cookie ");
851 for (i
= 0; i
< datalen
; ++i
)
852 ND_PRINT("%02x", GET_U_1(cp
+ i
));
857 #ifdef HAVE_LIBCRYPTO
858 USES_APPLE_DEPRECATED_API
860 tcp_verify_signature(netdissect_options
*ndo
,
861 const struct ip
*ip
, const struct tcphdr
*tp
,
862 const u_char
*data
, u_int length
, const u_char
*rcvsig
)
865 u_char sig
[TCP_SIGLEN
];
868 uint16_t savecsum
, tlen
;
869 const struct ip6_hdr
*ip6
;
873 if (!ND_TTEST_LEN(data
, length
)) {
874 ND_PRINT("snaplen too short, ");
875 return (CANT_CHECK_SIGNATURE
);
880 if (ndo
->ndo_sigsecret
== NULL
) {
881 ND_PRINT("shared secret not supplied with -M, ");
882 return (CANT_CHECK_SIGNATURE
);
887 * Step 1: Update MD5 hash with IP pseudo-header.
890 MD5_Update(&ctx
, (const char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
891 MD5_Update(&ctx
, (const char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
892 MD5_Update(&ctx
, (const char *)&zero_proto
, sizeof(zero_proto
));
893 MD5_Update(&ctx
, (const char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
894 tlen
= GET_BE_U_2(ip
->ip_len
) - IP_HL(ip
) * 4;
896 MD5_Update(&ctx
, (const char *)&tlen
, sizeof(tlen
));
897 } else if (IP_V(ip
) == 6) {
898 ip6
= (const struct ip6_hdr
*)ip
;
899 MD5_Update(&ctx
, (const char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
900 MD5_Update(&ctx
, (const char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
901 len32
= htonl(GET_BE_U_2(ip6
->ip6_plen
));
902 MD5_Update(&ctx
, (const char *)&len32
, sizeof(len32
));
904 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
905 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
906 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
908 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
910 ND_PRINT("IP version not 4 or 6, ");
911 return (CANT_CHECK_SIGNATURE
);
915 * Step 2: Update MD5 hash with TCP header, excluding options.
916 * The TCP checksum must be set to zero.
918 memcpy(&savecsum
, tp1
.th_sum
, sizeof(savecsum
));
919 memset(tp1
.th_sum
, 0, sizeof(tp1
.th_sum
));
920 MD5_Update(&ctx
, (const char *)&tp1
, sizeof(struct tcphdr
));
921 memcpy(tp1
.th_sum
, &savecsum
, sizeof(tp1
.th_sum
));
923 * Step 3: Update MD5 hash with TCP segment data, if present.
926 MD5_Update(&ctx
, data
, length
);
928 * Step 4: Update MD5 hash with shared secret.
930 MD5_Update(&ctx
, ndo
->ndo_sigsecret
, strlen(ndo
->ndo_sigsecret
));
931 MD5_Final(sig
, &ctx
);
933 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
934 return (SIGNATURE_VALID
);
936 return (SIGNATURE_INVALID
);
939 #endif /* HAVE_LIBCRYPTO */