2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
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.
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.
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.
33 /* $KAME: sctp_crc32.c,v 1.12 2005/03/06 16:04:17 itojun Exp $ */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <netinet/sctp_os.h>
40 #include <netinet/sctp.h>
41 #include <netinet/sctp_crc32.h>
42 #include <netinet/sctp_pcb.h>
45 #if !defined(SCTP_WITH_NO_CSUM)
48 sctp_finalize_crc32c(uint32_t crc32c)
52 #if BYTE_ORDER == BIG_ENDIAN
53 uint8_t byte0, byte1, byte2, byte3;
56 /* Complement the result */
58 #if BYTE_ORDER == BIG_ENDIAN
60 * For BIG-ENDIAN.. aka Motorola byte order the result is in
61 * little-endian form. So we must manually swap the bytes. Then we
62 * can call htonl() which does nothing...
64 byte0 = result & 0x000000ff;
65 byte1 = (result >> 8) & 0x000000ff;
66 byte2 = (result >> 16) & 0x000000ff;
67 byte3 = (result >> 24) & 0x000000ff;
68 crc32c = ((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3);
71 * For INTEL platforms the result comes out in network order. No
72 * htonl is required or the swap above. So we optimize out both the
73 * htonl and the manual swap above.
81 sctp_calculate_cksum(struct mbuf *m, uint32_t offset)
84 * given a mbuf chain with a packetheader offset by 'offset'
85 * pointing at a sctphdr (with csum set to 0) go through the chain
86 * of SCTP_BUF_NEXT()'s and calculate the SCTP checksum. This also
87 * has a side bonus as it will calculate the total length of the
88 * mbuf chain. Note: if offset is greater than the total mbuf
89 * length, checksum=1, pktlen=0 is returned (ie. no real error code)
91 uint32_t base = 0xffffffff;
95 /* find the correct mbuf and offset into mbuf */
96 while ((at != NULL) && (offset > (uint32_t) SCTP_BUF_LEN(at))) {
97 offset -= SCTP_BUF_LEN(at); /* update remaining offset
99 at = SCTP_BUF_NEXT(at);
102 if ((SCTP_BUF_LEN(at) - offset) > 0) {
103 base = calculate_crc32c(base,
104 (unsigned char *)(SCTP_BUF_AT(at, offset)),
105 (unsigned int)(SCTP_BUF_LEN(at) - offset));
108 /* we only offset once into the first mbuf */
109 if (offset < (uint32_t) SCTP_BUF_LEN(at))
112 offset -= SCTP_BUF_LEN(at);
114 at = SCTP_BUF_NEXT(at);
116 base = sctp_finalize_crc32c(base);
120 #endif /* !defined(SCTP_WITH_NO_CSUM) */
124 sctp_delayed_cksum(struct mbuf *m, uint32_t offset)
126 #if defined(SCTP_WITH_NO_CSUM)
127 panic("sctp_delayed_cksum() called when using no SCTP CRC.");
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);
136 if (offset + sizeof(uint32_t) > (uint32_t) (m->m_len)) {
137 printf("sctp_delayed_cksum(): m->len: %d, off: %d.\n",
138 (uint32_t) m->m_len, offset);
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.
146 *(uint32_t *) (m->m_data + offset) = checksum;