2006-11-03 15:23:16 +00:00
|
|
|
/*-
|
2017-11-20 19:43:44 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2007-05-08 17:01:12 +00:00
|
|
|
* Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
|
2012-05-23 11:26:28 +00:00
|
|
|
* Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
|
|
|
|
* Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
|
2006-11-03 15:23:16 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* a) Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* b) Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* c) Neither the name of Cisco Systems, Inc. nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2017-12-26 12:35:02 +00:00
|
|
|
#include "opt_sctp.h"
|
|
|
|
|
|
|
|
#ifdef SCTP
|
2009-05-06 08:49:39 +00:00
|
|
|
#include <netinet/sctp_os.h>
|
2009-02-03 11:04:03 +00:00
|
|
|
#include <netinet/sctp.h>
|
2006-11-03 15:23:16 +00:00
|
|
|
#include <netinet/sctp_crc32.h>
|
2009-02-03 11:04:03 +00:00
|
|
|
#include <netinet/sctp_pcb.h>
|
2017-12-26 12:35:02 +00:00
|
|
|
#else
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/mbuf.h>
|
2006-11-03 15:23:16 +00:00
|
|
|
|
2017-12-26 12:35:02 +00:00
|
|
|
#include <netinet/sctp_crc32.h>
|
|
|
|
#endif
|
2006-11-03 15:23:16 +00:00
|
|
|
|
2009-02-03 11:04:03 +00:00
|
|
|
static uint32_t
|
2018-07-19 20:11:14 +00:00
|
|
|
sctp_finalize_crc32c(uint32_t crc32c)
|
|
|
|
{
|
2006-11-03 15:23:16 +00:00
|
|
|
uint32_t result;
|
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
uint8_t byte0, byte1, byte2, byte3;
|
|
|
|
#endif
|
2017-12-26 12:35:02 +00:00
|
|
|
|
2006-11-03 15:23:16 +00:00
|
|
|
/* Complement the result */
|
2007-04-14 09:44:09 +00:00
|
|
|
result = ~crc32c;
|
2006-11-03 15:23:16 +00:00
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
/*
|
2017-12-26 12:35:02 +00:00
|
|
|
* For BIG-ENDIAN platforms the result is in little-endian form. So
|
|
|
|
* we must swap the bytes to return the result in network byte
|
|
|
|
* order.
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
|
|
|
byte0 = result & 0x000000ff;
|
|
|
|
byte1 = (result >> 8) & 0x000000ff;
|
|
|
|
byte2 = (result >> 16) & 0x000000ff;
|
|
|
|
byte3 = (result >> 24) & 0x000000ff;
|
2007-04-14 09:44:09 +00:00
|
|
|
crc32c = ((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3);
|
2006-11-03 15:23:16 +00:00
|
|
|
#else
|
|
|
|
/*
|
2017-12-26 12:35:02 +00:00
|
|
|
* For LITTLE ENDIAN platforms the result is in already in network
|
|
|
|
* byte order.
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
2007-04-14 09:44:09 +00:00
|
|
|
crc32c = result;
|
2006-11-03 15:23:16 +00:00
|
|
|
#endif
|
2007-04-14 09:44:09 +00:00
|
|
|
return (crc32c);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
2009-02-09 11:42:23 +00:00
|
|
|
|
2017-12-26 12:35:02 +00:00
|
|
|
/*
|
|
|
|
* Compute the SCTP checksum in network byte order for a given mbuf chain m
|
|
|
|
* which contains an SCTP packet starting at offset.
|
|
|
|
* Since this function is also called by ipfw, don't assume that
|
|
|
|
* it is compiled on a kernel with SCTP support.
|
|
|
|
*/
|
2009-02-03 11:04:03 +00:00
|
|
|
uint32_t
|
2018-07-19 20:11:14 +00:00
|
|
|
sctp_calculate_cksum(struct mbuf *m, uint32_t offset)
|
|
|
|
{
|
2009-02-03 11:04:03 +00:00
|
|
|
uint32_t base = 0xffffffff;
|
|
|
|
|
2017-12-26 12:35:02 +00:00
|
|
|
while (offset > 0) {
|
|
|
|
KASSERT(m != NULL, ("sctp_calculate_cksum, offset > length of mbuf chain"));
|
2018-07-19 20:11:14 +00:00
|
|
|
if (offset < (uint32_t)m->m_len) {
|
2017-12-26 12:35:02 +00:00
|
|
|
break;
|
2009-02-03 11:04:03 +00:00
|
|
|
}
|
2017-12-26 12:35:02 +00:00
|
|
|
offset -= m->m_len;
|
|
|
|
m = m->m_next;
|
|
|
|
}
|
|
|
|
if (offset > 0) {
|
|
|
|
base = calculate_crc32c(base,
|
|
|
|
(unsigned char *)(m->m_data + offset),
|
|
|
|
(unsigned int)(m->m_len - offset));
|
|
|
|
m = m->m_next;
|
|
|
|
}
|
|
|
|
while (m != NULL) {
|
|
|
|
base = calculate_crc32c(base,
|
|
|
|
(unsigned char *)m->m_data,
|
|
|
|
(unsigned int)m->m_len);
|
|
|
|
m = m->m_next;
|
2009-02-03 11:04:03 +00:00
|
|
|
}
|
2009-02-14 11:34:57 +00:00
|
|
|
base = sctp_finalize_crc32c(base);
|
2009-02-03 11:04:03 +00:00
|
|
|
return (base);
|
|
|
|
}
|
2009-02-14 11:34:57 +00:00
|
|
|
|
2017-12-26 12:35:02 +00:00
|
|
|
#ifdef SCTP
|
|
|
|
/*
|
|
|
|
* Compute and insert the SCTP checksum in network byte order for a given
|
|
|
|
* mbuf chain m which contains an SCTP packet starting at offset.
|
|
|
|
*/
|
2009-02-03 11:04:03 +00:00
|
|
|
void
|
2010-03-12 22:58:52 +00:00
|
|
|
sctp_delayed_cksum(struct mbuf *m, uint32_t offset)
|
2009-02-03 11:04:03 +00:00
|
|
|
{
|
|
|
|
uint32_t checksum;
|
|
|
|
|
|
|
|
checksum = sctp_calculate_cksum(m, offset);
|
|
|
|
SCTP_STAT_DECR(sctps_sendhwcrc);
|
|
|
|
SCTP_STAT_INCR(sctps_sendswcrc);
|
|
|
|
offset += offsetof(struct sctphdr, checksum);
|
|
|
|
|
2018-07-19 20:11:14 +00:00
|
|
|
if (offset + sizeof(uint32_t) > (uint32_t)(m->m_len)) {
|
2017-12-26 12:35:02 +00:00
|
|
|
#ifdef INVARIANTS
|
|
|
|
panic("sctp_delayed_cksum(): m->m_len: %d, offset: %u.",
|
|
|
|
m->m_len, offset);
|
|
|
|
#else
|
|
|
|
SCTP_PRINTF("sctp_delayed_cksum(): m->m_len: %d, offset: %u.\n",
|
|
|
|
m->m_len, offset);
|
|
|
|
#endif
|
2009-02-03 11:04:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-19 20:11:14 +00:00
|
|
|
*(uint32_t *)(m->m_data + offset) = checksum;
|
2009-02-03 11:04:03 +00:00
|
|
|
}
|
2017-12-26 12:35:02 +00:00
|
|
|
#endif
|