xref: /freebsd/sys/netinet/sctp_crc32.c (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  *
14  * b) Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the distribution.
17  *
18  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp.h>
40 #include <netinet/sctp_crc32.h>
41 #include <netinet/sctp_pcb.h>
42 
43 
44 #if !defined(SCTP_WITH_NO_CSUM)
45 
46 static uint32_t
47 sctp_finalize_crc32c(uint32_t crc32c)
48 {
49 	uint32_t result;
50 
51 #if BYTE_ORDER == BIG_ENDIAN
52 	uint8_t byte0, byte1, byte2, byte3;
53 
54 #endif
55 	/* Complement the result */
56 	result = ~crc32c;
57 #if BYTE_ORDER == BIG_ENDIAN
58 	/*
59 	 * For BIG-ENDIAN.. aka Motorola byte order the result is in
60 	 * little-endian form. So we must manually swap the bytes. Then we
61 	 * can call htonl() which does nothing...
62 	 */
63 	byte0 = result & 0x000000ff;
64 	byte1 = (result >> 8) & 0x000000ff;
65 	byte2 = (result >> 16) & 0x000000ff;
66 	byte3 = (result >> 24) & 0x000000ff;
67 	crc32c = ((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3);
68 #else
69 	/*
70 	 * For INTEL platforms the result comes out in network order. No
71 	 * htonl is required or the swap above. So we optimize out both the
72 	 * htonl and the manual swap above.
73 	 */
74 	crc32c = result;
75 #endif
76 	return (crc32c);
77 }
78 
79 uint32_t
80 sctp_calculate_cksum(struct mbuf *m, uint32_t offset)
81 {
82 	/*
83 	 * given a mbuf chain with a packetheader offset by 'offset'
84 	 * pointing at a sctphdr (with csum set to 0) go through the chain
85 	 * of SCTP_BUF_NEXT()'s and calculate the SCTP checksum. This also
86 	 * has a side bonus as it will calculate the total length of the
87 	 * mbuf chain. Note: if offset is greater than the total mbuf
88 	 * length, checksum=1, pktlen=0 is returned (ie. no real error code)
89 	 */
90 	uint32_t base = 0xffffffff;
91 	struct mbuf *at;
92 
93 	at = m;
94 	/* find the correct mbuf and offset into mbuf */
95 	while ((at != NULL) && (offset > (uint32_t)SCTP_BUF_LEN(at))) {
96 		offset -= SCTP_BUF_LEN(at);	/* update remaining offset
97 						 * left */
98 		at = SCTP_BUF_NEXT(at);
99 	}
100 	while (at != NULL) {
101 		if ((SCTP_BUF_LEN(at) - offset) > 0) {
102 			base = calculate_crc32c(base,
103 			    (unsigned char *)(SCTP_BUF_AT(at, offset)),
104 			    (unsigned int)(SCTP_BUF_LEN(at) - offset));
105 		}
106 		if (offset) {
107 			/* we only offset once into the first mbuf */
108 			if (offset < (uint32_t)SCTP_BUF_LEN(at))
109 				offset = 0;
110 			else
111 				offset -= SCTP_BUF_LEN(at);
112 		}
113 		at = SCTP_BUF_NEXT(at);
114 	}
115 	base = sctp_finalize_crc32c(base);
116 	return (base);
117 }
118 #endif				/* !defined(SCTP_WITH_NO_CSUM) */
119 
120 
121 void
122 sctp_delayed_cksum(struct mbuf *m, uint32_t offset)
123 {
124 #if defined(SCTP_WITH_NO_CSUM)
125 #ifdef INVARIANTS
126 	panic("sctp_delayed_cksum() called when using no SCTP CRC.");
127 #endif
128 #else
129 	uint32_t checksum;
130 
131 	checksum = sctp_calculate_cksum(m, offset);
132 	SCTP_STAT_DECR(sctps_sendhwcrc);
133 	SCTP_STAT_INCR(sctps_sendswcrc);
134 	offset += offsetof(struct sctphdr, checksum);
135 
136 	if (offset + sizeof(uint32_t) > (uint32_t)(m->m_len)) {
137 		SCTP_PRINTF("sctp_delayed_cksum(): m->len: %d,  off: %d.\n",
138 		    (uint32_t)m->m_len, offset);
139 		/*
140 		 * XXX this shouldn't happen, but if it does, the correct
141 		 * behavior may be to insert the checksum in the appropriate
142 		 * next mbuf in the chain.
143 		 */
144 		return;
145 	}
146 	*(uint32_t *)(m->m_data + offset) = checksum;
147 #endif
148 }
149