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"
46 #include "diag-control.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
, u_int length
, const u_char
*rcvsig
);
65 static void print_tcp_rst_data(netdissect_options
*, const u_char
*sp
, u_int length
);
66 static void print_tcp_fastopen_option(netdissect_options
*ndo
, const u_char
*cp
,
67 u_int datalen
, int exp
);
69 #define MAX_RST_DATA_LEN 30
79 struct tcp_seq_hash
*nxt
;
91 struct tcp_seq_hash6
{
92 struct tcp_seq_hash6
*nxt
;
98 #define TSEQ_HASHSIZE 919
100 /* These tcp options do not have the size octet */
101 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
103 static struct tcp_seq_hash tcp_seq_hash4
[TSEQ_HASHSIZE
];
104 static struct tcp_seq_hash6 tcp_seq_hash6
[TSEQ_HASHSIZE
];
106 static const struct tok tcp_flag_values
[] = {
118 static const struct tok tcp_option_values
[] = {
119 { TCPOPT_EOL
, "eol" },
120 { TCPOPT_NOP
, "nop" },
121 { TCPOPT_MAXSEG
, "mss" },
122 { TCPOPT_WSCALE
, "wscale" },
123 { TCPOPT_SACKOK
, "sackOK" },
124 { TCPOPT_SACK
, "sack" },
125 { TCPOPT_ECHO
, "echo" },
126 { TCPOPT_ECHOREPLY
, "echoreply" },
127 { TCPOPT_TIMESTAMP
, "TS" },
129 { TCPOPT_CCNEW
, "ccnew" },
130 { TCPOPT_CCECHO
, "" },
131 { TCPOPT_SIGNATURE
, "md5" },
132 { TCPOPT_SCPS
, "scps" },
133 { TCPOPT_UTO
, "uto" },
134 { TCPOPT_TCPAO
, "tcp-ao" },
135 { TCPOPT_MPTCP
, "mptcp" },
136 { TCPOPT_FASTOPEN
, "tfo" },
137 { TCPOPT_EXPERIMENT2
, "exp" },
142 tcp_cksum(netdissect_options
*ndo
,
144 const struct tcphdr
*tp
,
147 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)tp
, len
, len
,
152 tcp6_cksum(netdissect_options
*ndo
,
153 const struct ip6_hdr
*ip6
,
154 const struct tcphdr
*tp
,
157 return nextproto6_cksum(ndo
, ip6
, (const uint8_t *)tp
, len
, len
,
162 tcp_print(netdissect_options
*ndo
,
163 const u_char
*bp
, u_int length
,
164 const u_char
*bp2
, int fragmented
)
166 const struct tcphdr
*tp
;
171 uint16_t sport
, dport
, win
, urp
;
172 uint32_t seq
, ack
, thseq
, thack
;
176 const struct ip6_hdr
*ip6
;
177 u_int header_len
; /* Header length in bytes */
179 ndo
->ndo_protocol
= "tcp";
180 tp
= (const struct tcphdr
*)bp
;
181 ip
= (const struct ip
*)bp2
;
183 ip6
= (const struct ip6_hdr
*)bp2
;
187 if (!ND_TTEST_2(tp
->th_dport
)) {
190 GET_IP6ADDR_STRING(ip6
->ip6_src
),
191 GET_IP6ADDR_STRING(ip6
->ip6_dst
));
194 GET_IPADDR_STRING(ip
->ip_src
),
195 GET_IPADDR_STRING(ip
->ip_dst
));
201 sport
= GET_BE_U_2(tp
->th_sport
);
202 dport
= GET_BE_U_2(tp
->th_dport
);
205 if (GET_U_1(ip6
->ip6_nxt
) == IPPROTO_TCP
) {
206 ND_PRINT("%s.%s > %s.%s: ",
207 GET_IP6ADDR_STRING(ip6
->ip6_src
),
208 tcpport_string(ndo
, sport
),
209 GET_IP6ADDR_STRING(ip6
->ip6_dst
),
210 tcpport_string(ndo
, dport
));
212 ND_PRINT("%s > %s: ",
213 tcpport_string(ndo
, sport
), tcpport_string(ndo
, dport
));
216 if (GET_U_1(ip
->ip_p
) == IPPROTO_TCP
) {
217 ND_PRINT("%s.%s > %s.%s: ",
218 GET_IPADDR_STRING(ip
->ip_src
),
219 tcpport_string(ndo
, sport
),
220 GET_IPADDR_STRING(ip
->ip_dst
),
221 tcpport_string(ndo
, dport
));
223 ND_PRINT("%s > %s: ",
224 tcpport_string(ndo
, sport
), tcpport_string(ndo
, dport
));
230 hlen
= TH_OFF(tp
) * 4;
232 if (hlen
< sizeof(*tp
)) {
233 ND_PRINT(" tcp %u [bad hdr length %u - too short, < %zu]",
234 length
- hlen
, hlen
, sizeof(*tp
));
238 seq
= GET_BE_U_4(tp
->th_seq
);
239 ack
= GET_BE_U_4(tp
->th_ack
);
240 win
= GET_BE_U_2(tp
->th_win
);
241 urp
= GET_BE_U_2(tp
->th_urp
);
243 if (ndo
->ndo_qflag
) {
244 ND_PRINT("tcp %u", length
- hlen
);
246 ND_PRINT(" [bad hdr length %u - too long, > %u]",
252 flags
= GET_U_1(tp
->th_flags
);
253 ND_PRINT("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
255 if (!ndo
->ndo_Sflag
&& (flags
& TH_ACK
)) {
257 * Find (or record) the initial sequence numbers for
258 * this conversation. (we pick an arbitrary
259 * collating order so there's only one entry for
264 struct tcp_seq_hash6
*th
;
265 struct tcp_seq_hash6
*tcp_seq_hash
;
266 const void *src
, *dst
;
269 tcp_seq_hash
= tcp_seq_hash6
;
270 src
= (const void *)ip6
->ip6_src
;
271 dst
= (const void *)ip6
->ip6_dst
;
274 else if (sport
== dport
) {
275 if (UNALIGNED_MEMCMP(src
, dst
, sizeof(ip6
->ip6_dst
)) > 0)
279 UNALIGNED_MEMCPY(&tha
.src
, dst
, sizeof(ip6
->ip6_dst
));
280 UNALIGNED_MEMCPY(&tha
.dst
, src
, sizeof(ip6
->ip6_src
));
281 tha
.port
= ((u_int
)dport
) << 16 | sport
;
283 UNALIGNED_MEMCPY(&tha
.dst
, dst
, sizeof(ip6
->ip6_dst
));
284 UNALIGNED_MEMCPY(&tha
.src
, src
, sizeof(ip6
->ip6_src
));
285 tha
.port
= ((u_int
)sport
) << 16 | dport
;
288 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
289 th
->nxt
; th
= th
->nxt
)
290 if (memcmp((char *)&tha
, (char *)&th
->addr
,
291 sizeof(th
->addr
)) == 0)
294 if (!th
->nxt
|| (flags
& TH_SYN
)) {
295 /* didn't find it or new conversation */
296 /* calloc() return used by the 'tcp_seq_hash6'
297 hash table: do not free() */
298 if (th
->nxt
== NULL
) {
299 th
->nxt
= (struct tcp_seq_hash6
*)
300 calloc(1, sizeof(*th
));
302 (*ndo
->ndo_error
)(ndo
,
304 "%s: calloc", __func__
);
308 th
->ack
= seq
, th
->seq
= ack
- 1;
310 th
->seq
= seq
, th
->ack
= ack
- 1;
313 seq
-= th
->ack
, ack
-= th
->seq
;
315 seq
-= th
->seq
, ack
-= th
->ack
;
321 struct tcp_seq_hash
*th
;
322 struct tcp_seq_hash
*tcp_seq_hash
;
325 tcp_seq_hash
= tcp_seq_hash4
;
328 else if (sport
== dport
) {
329 if (UNALIGNED_MEMCMP(ip
->ip_src
, ip
->ip_dst
, sizeof(ip
->ip_dst
)) > 0)
333 UNALIGNED_MEMCPY(&tha
.src
, ip
->ip_dst
,
335 UNALIGNED_MEMCPY(&tha
.dst
, ip
->ip_src
,
337 tha
.port
= ((u_int
)dport
) << 16 | sport
;
339 UNALIGNED_MEMCPY(&tha
.dst
, ip
->ip_dst
,
341 UNALIGNED_MEMCPY(&tha
.src
, ip
->ip_src
,
343 tha
.port
= ((u_int
)sport
) << 16 | dport
;
346 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
347 th
->nxt
; th
= th
->nxt
)
348 if (memcmp((char *)&tha
, (char *)&th
->addr
,
349 sizeof(th
->addr
)) == 0)
352 if (!th
->nxt
|| (flags
& TH_SYN
)) {
353 /* didn't find it or new conversation */
354 /* calloc() return used by the 'tcp_seq_hash4'
355 hash table: do not free() */
356 if (th
->nxt
== NULL
) {
357 th
->nxt
= (struct tcp_seq_hash
*)
358 calloc(1, sizeof(*th
));
360 (*ndo
->ndo_error
)(ndo
,
362 "%s: calloc", __func__
);
366 th
->ack
= seq
, th
->seq
= ack
- 1;
368 th
->seq
= seq
, th
->ack
= ack
- 1;
371 seq
-= th
->ack
, ack
-= th
->seq
;
373 seq
-= th
->seq
, ack
-= th
->ack
;
381 thseq
= thack
= rev
= 0;
384 ND_PRINT(" [bad hdr length %u - too long, > %u]",
389 if (ndo
->ndo_vflag
&& !ndo
->ndo_Kflag
&& !fragmented
) {
390 /* Check the checksum, if possible. */
391 uint16_t sum
, tcp_sum
;
394 if (ND_TTEST_LEN(tp
->th_sport
, length
)) {
395 sum
= tcp_cksum(ndo
, ip
, tp
, length
);
396 tcp_sum
= GET_BE_U_2(tp
->th_sum
);
398 ND_PRINT(", cksum 0x%04x", tcp_sum
);
400 ND_PRINT(" (incorrect -> 0x%04x)",
401 in_cksum_shouldbe(tcp_sum
, sum
));
403 ND_PRINT(" (correct)");
405 } else if (IP_V(ip
) == 6) {
406 if (ND_TTEST_LEN(tp
->th_sport
, length
)) {
407 sum
= tcp6_cksum(ndo
, ip6
, tp
, length
);
408 tcp_sum
= GET_BE_U_2(tp
->th_sum
);
410 ND_PRINT(", cksum 0x%04x", tcp_sum
);
412 ND_PRINT(" (incorrect -> 0x%04x)",
413 in_cksum_shouldbe(tcp_sum
, sum
));
415 ND_PRINT(" (correct)");
422 if (ndo
->ndo_vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
423 ND_PRINT(", seq %u", seq
);
426 ND_PRINT(":%u", seq
+ length
);
430 if (flags
& TH_ACK
) {
431 ND_PRINT(", ack %u", ack
);
434 ND_PRINT(", win %u", win
);
437 ND_PRINT(", urg %u", urp
);
439 * Handle any options.
441 if (hlen
> sizeof(*tp
)) {
443 u_int i
, opt
, datalen
;
447 cp
= (const u_char
*)tp
+ sizeof(*tp
);
448 ND_PRINT(", options [");
458 cp
++; /* total including type, len */
459 if (len
< 2 || len
> hlen
)
461 --hlen
; /* account for length byte */
463 --hlen
; /* account for type byte */
466 /* Bail if "l" bytes of data are not left or were not captured */
467 #define LENCHECK(l) { if ((l) > hlen) goto bad; ND_TCHECK_LEN(cp, l); }
470 ND_PRINT("%s", tok2str(tcp_option_values
, "unknown-%u", opt
));
477 ND_PRINT(" %u", GET_BE_U_2(cp
));
483 ND_PRINT(" %u", GET_U_1(cp
));
488 if (datalen
% 8 != 0) {
489 ND_PRINT(" invalid sack");
493 ND_PRINT(" %u ", datalen
/ 8);
494 for (i
= 0; i
< datalen
; i
+= 8) {
496 s
= GET_BE_U_4(cp
+ i
);
498 e
= GET_BE_U_4(cp
+ i
+ 4);
506 ND_PRINT("{%u:%u}", s
, e
);
515 case TCPOPT_ECHOREPLY
:
518 * those options share their semantics.
523 ND_PRINT(" %u", GET_BE_U_4(cp
));
526 case TCPOPT_TIMESTAMP
:
529 ND_PRINT(" val %u ecr %u",
534 case TCPOPT_SIGNATURE
:
535 datalen
= TCP_SIGLEN
;
538 #ifdef HAVE_LIBCRYPTO
539 switch (tcp_verify_signature(ndo
, ip
, tp
,
540 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
542 case SIGNATURE_VALID
:
546 case SIGNATURE_INVALID
:
547 nd_print_invalid(ndo
);
550 case CANT_CHECK_SIGNATURE
:
551 ND_PRINT("can't check - ");
552 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
558 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
559 ND_PRINT("%02x", GET_U_1(cp
+ i
));
566 ND_PRINT(" cap %02x id %u", GET_U_1(cp
),
572 /* RFC 5925 Section 2.2:
573 * "The Length value MUST be greater than or equal to 4."
574 * (This includes the Kind and Length fields already processed
578 nd_print_invalid(ndo
);
581 ND_PRINT(" keyid %u", GET_U_1(cp
));
583 ND_PRINT(" rnextkeyid %u",
587 for (i
= 2; i
< datalen
; i
++) {
600 * Nothing interesting.
608 utoval
= GET_BE_U_2(cp
);
609 ND_PRINT(" 0x%x", utoval
);
611 utoval
= (utoval
>> 1) * 60;
614 ND_PRINT(" %u", utoval
);
619 const u_char
*snapend_save
;
624 /* Update the snapend to the end of the option
625 * before calling mptcp_print(). Some options
626 * (MPTCP or others) may be present after a
627 * MPTCP option. This prevents that, in
628 * mptcp_print(), the remaining length < the
631 snapend_save
= ndo
->ndo_snapend
;
632 ndo
->ndo_snapend
= ND_MIN(cp
- 2 + len
,
634 ret
= mptcp_print(ndo
, cp
- 2, len
, flags
);
635 ndo
->ndo_snapend
= snapend_save
;
641 case TCPOPT_FASTOPEN
:
645 print_tcp_fastopen_option(ndo
, cp
, datalen
, FALSE
);
648 case TCPOPT_EXPERIMENT2
:
654 magic
= GET_BE_U_2(cp
);
659 case 0xf989: /* TCP Fast Open RFC 7413 */
660 print_tcp_fastopen_option(ndo
, cp
+ 2, datalen
- 2, TRUE
);
664 /* Unknown magic number */
665 ND_PRINT("%04x", magic
);
674 for (i
= 0; i
< datalen
; ++i
) {
676 ND_PRINT("%02x", GET_U_1(cp
+ i
));
681 /* Account for data printed */
685 /* Check specification against observed length */
686 ++datalen
; /* option octet */
687 if (!ZEROLENOPT(opt
))
688 ++datalen
; /* size octet */
690 ND_PRINT("[len %u]", len
);
692 if (opt
== TCPOPT_EOL
)
699 * Print length field before crawling down the stack.
701 ND_PRINT(", length %u", length
);
707 * Decode payload if necessary.
709 header_len
= TH_OFF(tp
) * 4;
711 * Do a bounds check before decoding the payload.
712 * At least the header data is required.
714 if (!ND_TTEST_LEN(bp
, header_len
)) {
715 ND_PRINT(" [remaining caplen(%u) < header length(%u)]",
716 ND_BYTES_AVAILABLE_AFTER(bp
), header_len
);
717 nd_trunc_longjmp(ndo
);
720 if ((flags
& TH_RST
) && ndo
->ndo_vflag
) {
721 print_tcp_rst_data(ndo
, bp
, length
);
725 if (ndo
->ndo_packettype
) {
726 switch (ndo
->ndo_packettype
) {
728 zmtp1_print(ndo
, bp
, length
);
731 resp_print(ndo
, bp
, length
);
734 /* over_tcp: TRUE, is_mdns: FALSE */
735 domain_print(ndo
, bp
, length
, TRUE
, FALSE
);
741 if (IS_SRC_OR_DST_PORT(TELNET_PORT
)) {
742 telnet_print(ndo
, bp
, length
);
743 } else if (IS_SRC_OR_DST_PORT(SMTP_PORT
)) {
745 smtp_print(ndo
, bp
, length
);
746 } else if (IS_SRC_OR_DST_PORT(WHOIS_PORT
)) {
748 whois_print(ndo
, bp
, length
);
749 } else if (IS_SRC_OR_DST_PORT(BGP_PORT
))
750 bgp_print(ndo
, bp
, length
);
751 else if (IS_SRC_OR_DST_PORT(PPTP_PORT
))
753 else if (IS_SRC_OR_DST_PORT(REDIS_PORT
))
754 resp_print(ndo
, bp
, length
);
755 else if (IS_SRC_OR_DST_PORT(SSH_PORT
))
756 ssh_print(ndo
, bp
, length
);
758 else if (IS_SRC_OR_DST_PORT(NETBIOS_SSN_PORT
))
759 nbt_tcp_print(ndo
, bp
, length
);
760 else if (IS_SRC_OR_DST_PORT(SMB_PORT
))
761 smb_tcp_print(ndo
, bp
, length
);
763 else if (IS_SRC_OR_DST_PORT(BEEP_PORT
))
764 beep_print(ndo
, bp
, length
);
765 else if (IS_SRC_OR_DST_PORT(OPENFLOW_PORT_OLD
) || IS_SRC_OR_DST_PORT(OPENFLOW_PORT_IANA
))
766 openflow_print(ndo
, bp
, length
);
767 else if (IS_SRC_OR_DST_PORT(FTP_PORT
)) {
769 ftp_print(ndo
, bp
, length
);
770 } else if (IS_SRC_OR_DST_PORT(HTTP_PORT
) || IS_SRC_OR_DST_PORT(HTTP_PORT_ALT
)) {
772 http_print(ndo
, bp
, length
);
773 } else if (IS_SRC_OR_DST_PORT(RTSP_PORT
) || IS_SRC_OR_DST_PORT(RTSP_PORT_ALT
)) {
775 rtsp_print(ndo
, bp
, length
);
776 } else if (IS_SRC_OR_DST_PORT(NAMESERVER_PORT
)) {
777 /* over_tcp: TRUE, is_mdns: FALSE */
778 domain_print(ndo
, bp
, length
, TRUE
, FALSE
);
779 } else if (IS_SRC_OR_DST_PORT(MSDP_PORT
)) {
780 msdp_print(ndo
, bp
, length
);
781 } else if (IS_SRC_OR_DST_PORT(RPKI_RTR_PORT
)) {
782 rpki_rtr_print(ndo
, bp
, length
);
783 } else if (IS_SRC_OR_DST_PORT(LDP_PORT
)) {
784 ldp_print(ndo
, bp
, length
);
785 } else if ((IS_SRC_OR_DST_PORT(NFS_PORT
)) &&
786 length
>= 4 && ND_TTEST_4(bp
)) {
788 * If data present, header length valid, and NFS port used,
790 * Pass offset of data plus 4 bytes for RPC TCP msg length
791 * to NFS print routines.
794 const struct sunrpc_msg
*rp
;
795 enum sunrpc_msg_type direction
;
797 fraglen
= GET_BE_U_4(bp
) & 0x7FFFFFFF;
798 if (fraglen
> (length
) - 4)
799 fraglen
= (length
) - 4;
800 rp
= (const struct sunrpc_msg
*)(bp
+ 4);
801 if (ND_TTEST_4(rp
->rm_direction
)) {
802 direction
= (enum sunrpc_msg_type
) GET_BE_U_4(rp
->rm_direction
);
803 if (dport
== NFS_PORT
&& direction
== SUNRPC_CALL
) {
804 ND_PRINT(": NFS request xid %u ",
805 GET_BE_U_4(rp
->rm_xid
));
806 nfsreq_noaddr_print(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
809 if (sport
== NFS_PORT
&& direction
== SUNRPC_REPLY
) {
810 ND_PRINT(": NFS reply xid %u ",
811 GET_BE_U_4(rp
->rm_xid
));
812 nfsreply_noaddr_print(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
820 ND_PRINT("[bad opt]");
831 * RFC1122 says the following on data in RST segments:
833 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
835 * A TCP SHOULD allow a received RST segment to include data.
838 * It has been suggested that a RST segment could contain
839 * ASCII text that encoded and explained the cause of the
840 * RST. No standard has yet been established for such
846 print_tcp_rst_data(netdissect_options
*ndo
,
847 const u_char
*sp
, u_int length
)
851 ND_PRINT(ND_TTEST_LEN(sp
, length
) ? " [RST" : " [!RST");
852 if (length
> MAX_RST_DATA_LEN
) {
853 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
854 ND_PRINT("+"); /* indicate we truncate */
857 while (length
&& sp
< ndo
->ndo_snapend
) {
860 fn_print_char(ndo
, c
);
867 print_tcp_fastopen_option(netdissect_options
*ndo
, const u_char
*cp
,
868 u_int datalen
, int exp
)
876 /* Fast Open Cookie Request */
877 ND_PRINT(" cookiereq");
879 /* Fast Open Cookie */
880 if (datalen
% 2 != 0 || datalen
< 4 || datalen
> 16) {
881 nd_print_invalid(ndo
);
883 ND_PRINT(" cookie ");
884 for (i
= 0; i
< datalen
; ++i
)
885 ND_PRINT("%02x", GET_U_1(cp
+ i
));
890 #ifdef HAVE_LIBCRYPTO
893 tcp_verify_signature(netdissect_options
*ndo
,
894 const struct ip
*ip
, const struct tcphdr
*tp
,
895 const u_char
*data
, u_int length
, const u_char
*rcvsig
)
898 u_char sig
[TCP_SIGLEN
];
901 uint16_t savecsum
, tlen
;
902 const struct ip6_hdr
*ip6
;
906 if (data
+ length
> ndo
->ndo_snapend
) {
907 ND_PRINT("snaplen too short, ");
908 return (CANT_CHECK_SIGNATURE
);
913 if (ndo
->ndo_sigsecret
== NULL
) {
914 ND_PRINT("shared secret not supplied with -M, ");
915 return (CANT_CHECK_SIGNATURE
);
920 * Step 1: Update MD5 hash with IP pseudo-header.
923 MD5_Update(&ctx
, (const char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
924 MD5_Update(&ctx
, (const char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
925 MD5_Update(&ctx
, (const char *)&zero_proto
, sizeof(zero_proto
));
926 MD5_Update(&ctx
, (const char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
927 tlen
= GET_BE_U_2(ip
->ip_len
) - IP_HL(ip
) * 4;
929 MD5_Update(&ctx
, (const char *)&tlen
, sizeof(tlen
));
930 } else if (IP_V(ip
) == 6) {
931 ip6
= (const struct ip6_hdr
*)ip
;
932 MD5_Update(&ctx
, (const char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
933 MD5_Update(&ctx
, (const char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
934 len32
= htonl(GET_BE_U_2(ip6
->ip6_plen
));
935 MD5_Update(&ctx
, (const char *)&len32
, sizeof(len32
));
937 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
938 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
939 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
941 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
943 ND_PRINT("IP version not 4 or 6, ");
944 return (CANT_CHECK_SIGNATURE
);
948 * Step 2: Update MD5 hash with TCP header, excluding options.
949 * The TCP checksum must be set to zero.
951 memcpy(&savecsum
, tp1
.th_sum
, sizeof(savecsum
));
952 memset(tp1
.th_sum
, 0, sizeof(tp1
.th_sum
));
953 MD5_Update(&ctx
, (const char *)&tp1
, sizeof(struct tcphdr
));
954 memcpy(tp1
.th_sum
, &savecsum
, sizeof(tp1
.th_sum
));
956 * Step 3: Update MD5 hash with TCP segment data, if present.
959 MD5_Update(&ctx
, data
, length
);
961 * Step 4: Update MD5 hash with shared secret.
963 MD5_Update(&ctx
, ndo
->ndo_sigsecret
, strlen(ndo
->ndo_sigsecret
));
964 MD5_Final(sig
, &ctx
);
966 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
967 return (SIGNATURE_VALID
);
969 return (SIGNATURE_INVALID
);
972 #endif /* HAVE_LIBCRYPTO */