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