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 #include "netdissect.h"
43 #include "addrtoname.h"
55 #include <openssl/md5.h>
56 #include "signature.h"
58 static int tcp_verify_signature(netdissect_options
*ndo
,
59 const struct ip
*ip
, const struct tcphdr
*tp
,
60 const u_char
*data
, u_int length
, const u_char
*rcvsig
);
63 static void print_tcp_rst_data(netdissect_options
*, const u_char
*sp
, u_int length
);
64 static void print_tcp_fastopen_option(netdissect_options
*ndo
, const u_char
*cp
,
65 u_int datalen
, int exp
);
67 #define MAX_RST_DATA_LEN 30
77 struct tcp_seq_hash
*nxt
;
89 struct tcp_seq_hash6
{
90 struct tcp_seq_hash6
*nxt
;
96 #define TSEQ_HASHSIZE 919
98 /* These tcp optinos do not have the size octet */
99 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
101 static struct tcp_seq_hash tcp_seq_hash4
[TSEQ_HASHSIZE
];
102 static struct tcp_seq_hash6 tcp_seq_hash6
[TSEQ_HASHSIZE
];
104 static const struct tok tcp_flag_values
[] = {
116 static const struct tok tcp_option_values
[] = {
117 { TCPOPT_EOL
, "eol" },
118 { TCPOPT_NOP
, "nop" },
119 { TCPOPT_MAXSEG
, "mss" },
120 { TCPOPT_WSCALE
, "wscale" },
121 { TCPOPT_SACKOK
, "sackOK" },
122 { TCPOPT_SACK
, "sack" },
123 { TCPOPT_ECHO
, "echo" },
124 { TCPOPT_ECHOREPLY
, "echoreply" },
125 { TCPOPT_TIMESTAMP
, "TS" },
127 { TCPOPT_CCNEW
, "ccnew" },
128 { TCPOPT_CCECHO
, "" },
129 { TCPOPT_SIGNATURE
, "md5" },
130 { TCPOPT_SCPS
, "scps" },
131 { TCPOPT_UTO
, "uto" },
132 { TCPOPT_TCPAO
, "tcp-ao" },
133 { TCPOPT_MPTCP
, "mptcp" },
134 { TCPOPT_FASTOPEN
, "tfo" },
135 { TCPOPT_EXPERIMENT2
, "exp" },
140 tcp_cksum(netdissect_options
*ndo
,
142 const struct tcphdr
*tp
,
145 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)tp
, len
, len
,
150 tcp6_cksum(netdissect_options
*ndo
,
151 const struct ip6_hdr
*ip6
,
152 const struct tcphdr
*tp
,
155 return nextproto6_cksum(ndo
, ip6
, (const uint8_t *)tp
, len
, len
,
160 tcp_print(netdissect_options
*ndo
,
161 const u_char
*bp
, u_int length
,
162 const u_char
*bp2
, int fragmented
)
164 const struct tcphdr
*tp
;
169 uint16_t sport
, dport
, win
, urp
;
170 uint32_t seq
, ack
, thseq
, thack
;
174 const struct ip6_hdr
*ip6
;
176 tp
= (const struct tcphdr
*)bp
;
177 ip
= (const struct ip
*)bp2
;
179 ip6
= (const struct ip6_hdr
*)bp2
;
183 if (!ND_TTEST_2(tp
->th_dport
)) {
185 ND_PRINT("%s > %s: ",
186 ip6addr_string(ndo
, ip6
->ip6_src
),
187 ip6addr_string(ndo
, ip6
->ip6_dst
));
189 ND_PRINT("%s > %s: [|tcp]",
190 ipaddr_string(ndo
, ip
->ip_src
),
191 ipaddr_string(ndo
, ip
->ip_dst
));
196 sport
= EXTRACT_BE_U_2(tp
->th_sport
);
197 dport
= EXTRACT_BE_U_2(tp
->th_dport
);
200 if (EXTRACT_U_1(ip6
->ip6_nxt
) == IPPROTO_TCP
) {
201 ND_PRINT("%s.%s > %s.%s: ",
202 ip6addr_string(ndo
, ip6
->ip6_src
),
203 tcpport_string(ndo
, sport
),
204 ip6addr_string(ndo
, ip6
->ip6_dst
),
205 tcpport_string(ndo
, dport
));
207 ND_PRINT("%s > %s: ",
208 tcpport_string(ndo
, sport
), tcpport_string(ndo
, dport
));
211 if (EXTRACT_U_1(ip
->ip_p
) == IPPROTO_TCP
) {
212 ND_PRINT("%s.%s > %s.%s: ",
213 ipaddr_string(ndo
, ip
->ip_src
),
214 tcpport_string(ndo
, sport
),
215 ipaddr_string(ndo
, ip
->ip_dst
),
216 tcpport_string(ndo
, dport
));
218 ND_PRINT("%s > %s: ",
219 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, < %lu]",
229 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
));
233 seq
= EXTRACT_BE_U_4(tp
->th_seq
);
234 ack
= EXTRACT_BE_U_4(tp
->th_ack
);
235 win
= EXTRACT_BE_U_2(tp
->th_win
);
236 urp
= EXTRACT_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]",
247 flags
= EXTRACT_U_1(tp
->th_flags
);
248 ND_PRINT("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
250 if (!ndo
->ndo_Sflag
&& (flags
& TH_ACK
)) {
252 * Find (or record) the initial sequence numbers for
253 * this conversation. (we pick an arbitrary
254 * collating order so there's only one entry for
259 struct tcp_seq_hash6
*th
;
260 struct tcp_seq_hash6
*tcp_seq_hash
;
261 const void *src
, *dst
;
264 tcp_seq_hash
= tcp_seq_hash6
;
265 src
= (const void *)ip6
->ip6_src
;
266 dst
= (const void *)ip6
->ip6_dst
;
269 else if (sport
== dport
) {
270 if (UNALIGNED_MEMCMP(src
, dst
, sizeof(ip6
->ip6_dst
)) > 0)
274 UNALIGNED_MEMCPY(&tha
.src
, dst
, sizeof(ip6
->ip6_dst
));
275 UNALIGNED_MEMCPY(&tha
.dst
, src
, sizeof(ip6
->ip6_src
));
276 tha
.port
= dport
<< 16 | sport
;
278 UNALIGNED_MEMCPY(&tha
.dst
, dst
, sizeof(ip6
->ip6_dst
));
279 UNALIGNED_MEMCPY(&tha
.src
, src
, sizeof(ip6
->ip6_src
));
280 tha
.port
= sport
<< 16 | dport
;
283 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
284 th
->nxt
; th
= th
->nxt
)
285 if (memcmp((char *)&tha
, (char *)&th
->addr
,
286 sizeof(th
->addr
)) == 0)
289 if (!th
->nxt
|| (flags
& TH_SYN
)) {
290 /* didn't find it or new conversation */
291 /* calloc() return used by the 'tcp_seq_hash6'
292 hash table: do not free() */
293 if (th
->nxt
== NULL
) {
294 th
->nxt
= (struct tcp_seq_hash6
*)
295 calloc(1, sizeof(*th
));
297 (*ndo
->ndo_error
)(ndo
,
298 "tcp_print: calloc");
302 th
->ack
= seq
, th
->seq
= ack
- 1;
304 th
->seq
= seq
, th
->ack
= ack
- 1;
307 seq
-= th
->ack
, ack
-= th
->seq
;
309 seq
-= th
->seq
, ack
-= th
->ack
;
315 struct tcp_seq_hash
*th
;
316 struct tcp_seq_hash
*tcp_seq_hash
;
319 tcp_seq_hash
= tcp_seq_hash4
;
322 else if (sport
== dport
) {
323 if (UNALIGNED_MEMCMP(ip
->ip_src
, ip
->ip_dst
, sizeof(ip
->ip_dst
)) > 0)
327 UNALIGNED_MEMCPY(&tha
.src
, ip
->ip_dst
,
329 UNALIGNED_MEMCPY(&tha
.dst
, ip
->ip_src
,
331 tha
.port
= dport
<< 16 | sport
;
333 UNALIGNED_MEMCPY(&tha
.dst
, ip
->ip_dst
,
335 UNALIGNED_MEMCPY(&tha
.src
, ip
->ip_src
,
337 tha
.port
= sport
<< 16 | dport
;
340 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
341 th
->nxt
; th
= th
->nxt
)
342 if (memcmp((char *)&tha
, (char *)&th
->addr
,
343 sizeof(th
->addr
)) == 0)
346 if (!th
->nxt
|| (flags
& TH_SYN
)) {
347 /* didn't find it or new conversation */
348 /* calloc() return used by the 'tcp_seq_hash4'
349 hash table: do not free() */
350 if (th
->nxt
== NULL
) {
351 th
->nxt
= (struct tcp_seq_hash
*)
352 calloc(1, sizeof(*th
));
354 (*ndo
->ndo_error
)(ndo
,
355 "tcp_print: calloc");
359 th
->ack
= seq
, th
->seq
= ack
- 1;
361 th
->seq
= seq
, th
->ack
= ack
- 1;
364 seq
-= th
->ack
, ack
-= th
->seq
;
366 seq
-= th
->seq
, ack
-= th
->ack
;
374 thseq
= thack
= rev
= 0;
377 ND_PRINT(" [bad hdr length %u - too long, > %u]",
382 if (ndo
->ndo_vflag
&& !ndo
->ndo_Kflag
&& !fragmented
) {
383 /* Check the checksum, if possible. */
384 uint16_t sum
, tcp_sum
;
387 if (ND_TTEST_LEN(tp
->th_sport
, length
)) {
388 sum
= tcp_cksum(ndo
, ip
, tp
, length
);
389 tcp_sum
= EXTRACT_BE_U_2(tp
->th_sum
);
391 ND_PRINT(", cksum 0x%04x", tcp_sum
);
393 ND_PRINT(" (incorrect -> 0x%04x)",
394 in_cksum_shouldbe(tcp_sum
, sum
));
396 ND_PRINT(" (correct)");
398 } else if (IP_V(ip
) == 6 && ip6
->ip6_plen
) {
399 if (ND_TTEST_LEN(tp
->th_sport
, length
)) {
400 sum
= tcp6_cksum(ndo
, ip6
, tp
, length
);
401 tcp_sum
= EXTRACT_BE_U_2(tp
->th_sum
);
403 ND_PRINT(", cksum 0x%04x", tcp_sum
);
405 ND_PRINT(" (incorrect -> 0x%04x)",
406 in_cksum_shouldbe(tcp_sum
, sum
));
408 ND_PRINT(" (correct)");
415 if (ndo
->ndo_vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
416 ND_PRINT(", seq %u", seq
);
419 ND_PRINT(":%u", seq
+ length
);
423 if (flags
& TH_ACK
) {
424 ND_PRINT(", ack %u", ack
);
427 ND_PRINT(", win %u", win
);
430 ND_PRINT(", urg %u", urp
);
432 * Handle any options.
434 if (hlen
> sizeof(*tp
)) {
436 u_int i
, opt
, datalen
;
440 cp
= (const u_char
*)tp
+ sizeof(*tp
);
441 ND_PRINT(", options [");
446 opt
= EXTRACT_U_1(cp
);
452 len
= EXTRACT_U_1(cp
);
453 cp
++; /* total including type, len */
454 if (len
< 2 || len
> hlen
)
456 --hlen
; /* account for length byte */
458 --hlen
; /* account for type byte */
461 /* Bail if "l" bytes of data are not left or were not captured */
462 #define LENCHECK(l) { if ((l) > hlen) goto bad; ND_TCHECK_LEN(cp, l); }
465 ND_PRINT("%s", tok2str(tcp_option_values
, "unknown-%u", opt
));
472 ND_PRINT(" %u", EXTRACT_BE_U_2(cp
));
478 ND_PRINT(" %u", EXTRACT_U_1(cp
));
483 if (datalen
% 8 != 0) {
484 ND_PRINT(" invalid sack");
488 ND_PRINT(" %u ", datalen
/ 8);
489 for (i
= 0; i
< datalen
; i
+= 8) {
491 s
= EXTRACT_BE_U_4(cp
+ i
);
493 e
= EXTRACT_BE_U_4(cp
+ i
+ 4);
501 ND_PRINT("{%u:%u}", s
, e
);
510 case TCPOPT_ECHOREPLY
:
513 * those options share their semantics.
518 ND_PRINT(" %u", EXTRACT_BE_U_4(cp
));
521 case TCPOPT_TIMESTAMP
:
524 ND_PRINT(" val %u ecr %u",
526 EXTRACT_BE_U_4(cp
+ 4));
529 case TCPOPT_SIGNATURE
:
530 datalen
= TCP_SIGLEN
;
533 #ifdef HAVE_LIBCRYPTO
534 switch (tcp_verify_signature(ndo
, ip
, tp
,
535 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
537 case SIGNATURE_VALID
:
541 case SIGNATURE_INVALID
:
545 case CANT_CHECK_SIGNATURE
:
546 ND_PRINT("can't check - ");
547 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
548 ND_PRINT("%02x", EXTRACT_U_1(cp
+ i
));
552 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
553 ND_PRINT("%02x", EXTRACT_U_1(cp
+ i
));
560 ND_PRINT(" cap %02x id %u", EXTRACT_U_1(cp
), EXTRACT_U_1(cp
+ 1));
565 /* RFC 5925 Section 2.2:
566 * "The Length value MUST be greater than or equal to 4."
567 * (This includes the Kind and Length fields already processed
571 ND_PRINT(" invalid");
574 ND_PRINT(" keyid %u", EXTRACT_U_1(cp
));
576 ND_PRINT(" rnextkeyid %u", EXTRACT_U_1(cp
+ 1));
579 for (i
= 2; i
< datalen
; i
++) {
581 ND_PRINT("%02x", EXTRACT_U_1(cp
+ i
));
591 * Nothing interesting.
599 utoval
= EXTRACT_BE_U_2(cp
);
600 ND_PRINT(" 0x%x", utoval
);
602 utoval
= (utoval
>> 1) * 60;
605 ND_PRINT(" %u", utoval
);
611 if (!mptcp_print(ndo
, cp
-2, len
, flags
))
615 case TCPOPT_FASTOPEN
:
619 print_tcp_fastopen_option(ndo
, cp
, datalen
, FALSE
);
622 case TCPOPT_EXPERIMENT2
:
628 magic
= EXTRACT_BE_U_2(cp
);
633 case 0xf989: /* TCP Fast Open RFC 7413 */
634 print_tcp_fastopen_option(ndo
, cp
+ 2, datalen
- 2, TRUE
);
638 /* Unknown magic number */
639 ND_PRINT("%04x", magic
);
648 for (i
= 0; i
< datalen
; ++i
) {
650 ND_PRINT("%02x", EXTRACT_U_1(cp
+ i
));
655 /* Account for data printed */
659 /* Check specification against observed length */
660 ++datalen
; /* option octet */
661 if (!ZEROLENOPT(opt
))
662 ++datalen
; /* size octet */
664 ND_PRINT("[len %u]", len
);
666 if (opt
== TCPOPT_EOL
)
673 * Print length field before crawling down the stack.
675 ND_PRINT(", length %u", length
);
681 * Decode payload if necessary.
683 bp
+= TH_OFF(tp
) * 4;
684 if ((flags
& TH_RST
) && ndo
->ndo_vflag
) {
685 print_tcp_rst_data(ndo
, bp
, length
);
689 if (ndo
->ndo_packettype
) {
690 switch (ndo
->ndo_packettype
) {
692 zmtp1_print(ndo
, bp
, length
);
695 resp_print(ndo
, bp
, length
);
701 if (IS_SRC_OR_DST_PORT(TELNET_PORT
)) {
702 telnet_print(ndo
, bp
, length
);
703 } else if (IS_SRC_OR_DST_PORT(SMTP_PORT
)) {
705 smtp_print(ndo
, bp
, length
);
706 } else if (IS_SRC_OR_DST_PORT(WHOIS_PORT
)) {
708 txtproto_print(ndo
, bp
, length
, "whois", NULL
, 0); /* RFC 3912 */
709 } else if (IS_SRC_OR_DST_PORT(BGP_PORT
))
710 bgp_print(ndo
, bp
, length
);
711 else if (IS_SRC_OR_DST_PORT(PPTP_PORT
))
713 else if (IS_SRC_OR_DST_PORT(REDIS_PORT
))
714 resp_print(ndo
, bp
, length
);
716 else if (IS_SRC_OR_DST_PORT(NETBIOS_SSN_PORT
))
717 nbt_tcp_print(ndo
, bp
, length
);
718 else if (IS_SRC_OR_DST_PORT(SMB_PORT
))
719 smb_tcp_print(ndo
, bp
, length
);
721 else if (IS_SRC_OR_DST_PORT(BEEP_PORT
))
722 beep_print(ndo
, bp
, length
);
723 else if (IS_SRC_OR_DST_PORT(OPENFLOW_PORT_OLD
) || IS_SRC_OR_DST_PORT(OPENFLOW_PORT_IANA
))
724 openflow_print(ndo
, bp
, length
);
725 else if (IS_SRC_OR_DST_PORT(FTP_PORT
)) {
727 ftp_print(ndo
, bp
, length
);
728 } else if (IS_SRC_OR_DST_PORT(HTTP_PORT
) || IS_SRC_OR_DST_PORT(HTTP_PORT_ALT
)) {
730 http_print(ndo
, bp
, length
);
731 } else if (IS_SRC_OR_DST_PORT(RTSP_PORT
) || IS_SRC_OR_DST_PORT(RTSP_PORT_ALT
)) {
733 rtsp_print(ndo
, bp
, length
);
734 } else if (length
> 2 &&
735 (IS_SRC_OR_DST_PORT(NAMESERVER_PORT
))) {
737 * TCP DNS query has 2byte length at the head.
738 * XXX packet could be unaligned, it can go strange
740 domain_print(ndo
, bp
+ 2, length
- 2, 0);
741 } else if (IS_SRC_OR_DST_PORT(MSDP_PORT
)) {
742 msdp_print(ndo
, bp
, length
);
743 } else if (IS_SRC_OR_DST_PORT(RPKI_RTR_PORT
)) {
744 rpki_rtr_print(ndo
, bp
, length
);
746 else if (length
> 0 && (IS_SRC_OR_DST_PORT(LDP_PORT
))) {
747 ldp_print(ndo
, bp
, length
);
749 else if ((IS_SRC_OR_DST_PORT(NFS_PORT
)) &&
750 length
>= 4 && ND_TTEST_4(bp
)) {
752 * If data present, header length valid, and NFS port used,
754 * Pass offset of data plus 4 bytes for RPC TCP msg length
755 * to NFS print routines.
758 const struct sunrpc_msg
*rp
;
759 enum sunrpc_msg_type direction
;
761 fraglen
= EXTRACT_BE_U_4(bp
) & 0x7FFFFFFF;
762 if (fraglen
> (length
) - 4)
763 fraglen
= (length
) - 4;
764 rp
= (const struct sunrpc_msg
*)(bp
+ 4);
765 if (ND_TTEST_4(rp
->rm_direction
)) {
766 direction
= (enum sunrpc_msg_type
) EXTRACT_BE_U_4(rp
->rm_direction
);
767 if (dport
== NFS_PORT
&& direction
== SUNRPC_CALL
) {
768 ND_PRINT(": NFS request xid %u ",
769 EXTRACT_BE_U_4(rp
->rm_xid
));
770 nfsreq_noaddr_print(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
773 if (sport
== NFS_PORT
&& direction
== SUNRPC_REPLY
) {
774 ND_PRINT(": NFS reply xid %u ",
775 EXTRACT_BE_U_4(rp
->rm_xid
));
776 nfsreply_noaddr_print(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
784 ND_PRINT("[bad opt]");
795 * RFC1122 says the following on data in RST segments:
797 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
799 * A TCP SHOULD allow a received RST segment to include data.
802 * It has been suggested that a RST segment could contain
803 * ASCII text that encoded and explained the cause of the
804 * RST. No standard has yet been established for such
810 print_tcp_rst_data(netdissect_options
*ndo
,
811 const u_char
*sp
, u_int length
)
815 ND_PRINT(ND_TTEST_LEN(sp
, length
) ? " [RST" : " [!RST");
816 if (length
> MAX_RST_DATA_LEN
) {
817 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
818 ND_PRINT("+"); /* indicate we truncate */
821 while (length
-- && sp
< ndo
->ndo_snapend
) {
830 print_tcp_fastopen_option(netdissect_options
*ndo
, const u_char
*cp
,
831 u_int datalen
, int exp
)
839 /* Fast Open Cookie Request */
840 ND_PRINT(" cookiereq");
842 /* Fast Open Cookie */
843 if (datalen
% 2 != 0 || datalen
< 4 || datalen
> 16) {
844 ND_PRINT(" invalid");
846 ND_PRINT(" cookie ");
847 for (i
= 0; i
< datalen
; ++i
)
848 ND_PRINT("%02x", EXTRACT_U_1(cp
+ i
));
853 #ifdef HAVE_LIBCRYPTO
854 USES_APPLE_DEPRECATED_API
856 tcp_verify_signature(netdissect_options
*ndo
,
857 const struct ip
*ip
, const struct tcphdr
*tp
,
858 const u_char
*data
, u_int length
, const u_char
*rcvsig
)
861 u_char sig
[TCP_SIGLEN
];
864 uint16_t savecsum
, tlen
;
865 const struct ip6_hdr
*ip6
;
869 if (data
+ length
> ndo
->ndo_snapend
) {
870 ND_PRINT("snaplen too short, ");
871 return (CANT_CHECK_SIGNATURE
);
876 if (ndo
->ndo_sigsecret
== NULL
) {
877 ND_PRINT("shared secret not supplied with -M, ");
878 return (CANT_CHECK_SIGNATURE
);
883 * Step 1: Update MD5 hash with IP pseudo-header.
886 MD5_Update(&ctx
, (const char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
887 MD5_Update(&ctx
, (const char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
888 MD5_Update(&ctx
, (const char *)&zero_proto
, sizeof(zero_proto
));
889 MD5_Update(&ctx
, (const char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
890 tlen
= EXTRACT_BE_U_2(ip
->ip_len
) - IP_HL(ip
) * 4;
892 MD5_Update(&ctx
, (const char *)&tlen
, sizeof(tlen
));
893 } else if (IP_V(ip
) == 6) {
894 ip6
= (const struct ip6_hdr
*)ip
;
895 MD5_Update(&ctx
, (const char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
896 MD5_Update(&ctx
, (const char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
897 len32
= htonl(EXTRACT_BE_U_2(ip6
->ip6_plen
));
898 MD5_Update(&ctx
, (const char *)&len32
, sizeof(len32
));
900 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
901 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
902 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
904 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
906 ND_PRINT("IP version not 4 or 6, ");
907 return (CANT_CHECK_SIGNATURE
);
911 * Step 2: Update MD5 hash with TCP header, excluding options.
912 * The TCP checksum must be set to zero.
914 memcpy(&savecsum
, tp1
.th_sum
, sizeof(savecsum
));
915 memset(tp1
.th_sum
, 0, sizeof(tp1
.th_sum
));
916 MD5_Update(&ctx
, (const char *)&tp1
, sizeof(struct tcphdr
));
917 memcpy(tp1
.th_sum
, &savecsum
, sizeof(tp1
.th_sum
));
919 * Step 3: Update MD5 hash with TCP segment data, if present.
922 MD5_Update(&ctx
, data
, length
);
924 * Step 4: Update MD5 hash with shared secret.
926 MD5_Update(&ctx
, ndo
->ndo_sigsecret
, strlen(ndo
->ndo_sigsecret
));
927 MD5_Final(sig
, &ctx
);
929 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
930 return (SIGNATURE_VALID
);
932 return (SIGNATURE_INVALID
);
935 #endif /* HAVE_LIBCRYPTO */
939 * c-style: whitesmith