sctp_crc32.c revision 234995
1/*-
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.
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/* $KAME: sctp_crc32.c,v 1.12 2005/03/06 16:04:17 itojun Exp $	 */
34
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/netinet/sctp_crc32.c 234995 2012-05-04 09:27:00Z tuexen $");
38
39#include <netinet/sctp_os.h>
40#include <netinet/sctp.h>
41#include <netinet/sctp_crc32.h>
42#include <netinet/sctp_pcb.h>
43
44
45#if !defined(SCTP_WITH_NO_CSUM)
46
47static uint32_t
48sctp_finalize_crc32c(uint32_t crc32c)
49{
50	uint32_t result;
51
52#if BYTE_ORDER == BIG_ENDIAN
53	uint8_t byte0, byte1, byte2, byte3;
54
55#endif
56	/* Complement the result */
57	result = ~crc32c;
58#if BYTE_ORDER == BIG_ENDIAN
59	/*
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...
63	 */
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);
69#else
70	/*
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.
74	 */
75	crc32c = result;
76#endif
77	return (crc32c);
78}
79
80uint32_t
81sctp_calculate_cksum(struct mbuf *m, uint32_t offset)
82{
83	/*
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)
90	 */
91	uint32_t base = 0xffffffff;
92	struct mbuf *at;
93
94	at = m;
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
98						 * left */
99		at = SCTP_BUF_NEXT(at);
100	}
101	while (at != NULL) {
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));
106		}
107		if (offset) {
108			/* we only offset once into the first mbuf */
109			if (offset < (uint32_t) SCTP_BUF_LEN(at))
110				offset = 0;
111			else
112				offset -= SCTP_BUF_LEN(at);
113		}
114		at = SCTP_BUF_NEXT(at);
115	}
116	base = sctp_finalize_crc32c(base);
117	return (base);
118}
119
120#endif				/* !defined(SCTP_WITH_NO_CSUM) */
121
122
123void
124sctp_delayed_cksum(struct mbuf *m, uint32_t offset)
125{
126#if defined(SCTP_WITH_NO_CSUM)
127	panic("sctp_delayed_cksum() called when using no SCTP CRC.");
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