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