sctp_crc32.c revision 234996
1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3218319Srrs * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4218319Srrs * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
5163953Srrs *
6163953Srrs * Redistribution and use in source and binary forms, with or without
7163953Srrs * modification, are permitted provided that the following conditions are met:
8163953Srrs *
9163953Srrs * a) Redistributions of source code must retain the above copyright notice,
10163953Srrs *   this list of conditions and the following disclaimer.
11163953Srrs *
12163953Srrs * b) Redistributions in binary form must reproduce the above copyright
13163953Srrs *    notice, this list of conditions and the following disclaimer in
14163953Srrs *   the documentation and/or other materials provided with the distribution.
15163953Srrs *
16163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
17163953Srrs *    contributors may be used to endorse or promote products derived
18163953Srrs *    from this software without specific prior written permission.
19163953Srrs *
20163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
31163953Srrs */
32163953Srrs
33163953Srrs/* $KAME: sctp_crc32.c,v 1.12 2005/03/06 16:04:17 itojun Exp $	 */
34163953Srrs
35163953Srrs
36163953Srrs#include <sys/cdefs.h>
37163953Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctp_crc32.c 234996 2012-05-04 10:06:08Z tuexen $");
38163953Srrs
39191845Szec#include <netinet/sctp_os.h>
40188067Srrs#include <netinet/sctp.h>
41163953Srrs#include <netinet/sctp_crc32.h>
42188067Srrs#include <netinet/sctp_pcb.h>
43163953Srrs
44191891Srrs
45188067Srrs#if !defined(SCTP_WITH_NO_CSUM)
46163953Srrs
47163953Srrsstatic uint32_t
48188605Srrssctp_finalize_crc32c(uint32_t crc32c)
49163953Srrs{
50163953Srrs	uint32_t result;
51163953Srrs
52163953Srrs#if BYTE_ORDER == BIG_ENDIAN
53163953Srrs	uint8_t byte0, byte1, byte2, byte3;
54163953Srrs
55163953Srrs#endif
56163953Srrs	/* Complement the result */
57168709Srrs	result = ~crc32c;
58163953Srrs#if BYTE_ORDER == BIG_ENDIAN
59163953Srrs	/*
60163953Srrs	 * For BIG-ENDIAN.. aka Motorola byte order the result is in
61163953Srrs	 * little-endian form. So we must manually swap the bytes. Then we
62163953Srrs	 * can call htonl() which does nothing...
63163953Srrs	 */
64163953Srrs	byte0 = result & 0x000000ff;
65163953Srrs	byte1 = (result >> 8) & 0x000000ff;
66163953Srrs	byte2 = (result >> 16) & 0x000000ff;
67163953Srrs	byte3 = (result >> 24) & 0x000000ff;
68168709Srrs	crc32c = ((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3);
69163953Srrs#else
70163953Srrs	/*
71163953Srrs	 * For INTEL platforms the result comes out in network order. No
72163953Srrs	 * htonl is required or the swap above. So we optimize out both the
73163953Srrs	 * htonl and the manual swap above.
74163953Srrs	 */
75168709Srrs	crc32c = result;
76163953Srrs#endif
77168709Srrs	return (crc32c);
78163953Srrs}
79188388Srrs
80188299Spisouint32_t
81188067Srrssctp_calculate_cksum(struct mbuf *m, uint32_t offset)
82188067Srrs{
83188067Srrs	/*
84188067Srrs	 * given a mbuf chain with a packetheader offset by 'offset'
85188067Srrs	 * pointing at a sctphdr (with csum set to 0) go through the chain
86188067Srrs	 * of SCTP_BUF_NEXT()'s and calculate the SCTP checksum. This also
87188067Srrs	 * has a side bonus as it will calculate the total length of the
88188067Srrs	 * mbuf chain. Note: if offset is greater than the total mbuf
89188067Srrs	 * length, checksum=1, pktlen=0 is returned (ie. no real error code)
90188067Srrs	 */
91188067Srrs	uint32_t base = 0xffffffff;
92188067Srrs	struct mbuf *at;
93188067Srrs
94188067Srrs	at = m;
95188067Srrs	/* find the correct mbuf and offset into mbuf */
96188067Srrs	while ((at != NULL) && (offset > (uint32_t) SCTP_BUF_LEN(at))) {
97188067Srrs		offset -= SCTP_BUF_LEN(at);	/* update remaining offset
98188067Srrs						 * left */
99188067Srrs		at = SCTP_BUF_NEXT(at);
100188067Srrs	}
101188067Srrs	while (at != NULL) {
102188067Srrs		if ((SCTP_BUF_LEN(at) - offset) > 0) {
103188605Srrs			base = calculate_crc32c(base,
104188605Srrs			    (unsigned char *)(SCTP_BUF_AT(at, offset)),
105188605Srrs			    (unsigned int)(SCTP_BUF_LEN(at) - offset));
106188067Srrs		}
107188067Srrs		if (offset) {
108188605Srrs			/* we only offset once into the first mbuf */
109188067Srrs			if (offset < (uint32_t) SCTP_BUF_LEN(at))
110188067Srrs				offset = 0;
111188067Srrs			else
112188067Srrs				offset -= SCTP_BUF_LEN(at);
113188067Srrs		}
114188067Srrs		at = SCTP_BUF_NEXT(at);
115188067Srrs	}
116188605Srrs	base = sctp_finalize_crc32c(base);
117188067Srrs	return (base);
118188067Srrs}
119188067Srrs
120188605Srrs#endif				/* !defined(SCTP_WITH_NO_CSUM) */
121188605Srrs
122188605Srrs
123188067Srrsvoid
124205104Srrssctp_delayed_cksum(struct mbuf *m, uint32_t offset)
125188067Srrs{
126211969Stuexen#if defined(SCTP_WITH_NO_CSUM)
127234996Stuexen#ifdef INVARIANTS
128211969Stuexen	panic("sctp_delayed_cksum() called when using no SCTP CRC.");
129234996Stuexen#endif
130211969Stuexen#else
131188067Srrs	uint32_t checksum;
132188067Srrs
133188067Srrs	checksum = sctp_calculate_cksum(m, offset);
134188067Srrs	SCTP_STAT_DECR(sctps_sendhwcrc);
135188067Srrs	SCTP_STAT_INCR(sctps_sendswcrc);
136188067Srrs	offset += offsetof(struct sctphdr, checksum);
137188067Srrs
138188067Srrs	if (offset + sizeof(uint32_t) > (uint32_t) (m->m_len)) {
139234995Stuexen		SCTP_PRINTF("sctp_delayed_cksum(): m->len: %d,  off: %d.\n",
140215301Stuexen		    (uint32_t) m->m_len, offset);
141188067Srrs		/*
142188067Srrs		 * XXX this shouldn't happen, but if it does, the correct
143188067Srrs		 * behavior may be to insert the checksum in the appropriate
144188067Srrs		 * next mbuf in the chain.
145188067Srrs		 */
146188067Srrs		return;
147188067Srrs	}
148188067Srrs	*(uint32_t *) (m->m_data + offset) = checksum;
149211969Stuexen#endif
150188067Srrs}
151