1163953Srrs/*- 2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 3235828Stuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 4235828Stuexen * Copyright (c) 2008-2012, 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#include <sys/cdefs.h> 34163953Srrs__FBSDID("$FreeBSD$"); 35163953Srrs 36191845Szec#include <netinet/sctp_os.h> 37188067Srrs#include <netinet/sctp.h> 38163953Srrs#include <netinet/sctp_crc32.h> 39188067Srrs#include <netinet/sctp_pcb.h> 40163953Srrs 41191891Srrs 42188067Srrs#if !defined(SCTP_WITH_NO_CSUM) 43163953Srrs 44163953Srrsstatic uint32_t 45188605Srrssctp_finalize_crc32c(uint32_t crc32c) 46163953Srrs{ 47163953Srrs uint32_t result; 48163953Srrs 49163953Srrs#if BYTE_ORDER == BIG_ENDIAN 50163953Srrs uint8_t byte0, byte1, byte2, byte3; 51163953Srrs 52163953Srrs#endif 53163953Srrs /* Complement the result */ 54168709Srrs result = ~crc32c; 55163953Srrs#if BYTE_ORDER == BIG_ENDIAN 56163953Srrs /* 57163953Srrs * For BIG-ENDIAN.. aka Motorola byte order the result is in 58163953Srrs * little-endian form. So we must manually swap the bytes. Then we 59163953Srrs * can call htonl() which does nothing... 60163953Srrs */ 61163953Srrs byte0 = result & 0x000000ff; 62163953Srrs byte1 = (result >> 8) & 0x000000ff; 63163953Srrs byte2 = (result >> 16) & 0x000000ff; 64163953Srrs byte3 = (result >> 24) & 0x000000ff; 65168709Srrs crc32c = ((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3); 66163953Srrs#else 67163953Srrs /* 68163953Srrs * For INTEL platforms the result comes out in network order. No 69163953Srrs * htonl is required or the swap above. So we optimize out both the 70163953Srrs * htonl and the manual swap above. 71163953Srrs */ 72168709Srrs crc32c = result; 73163953Srrs#endif 74168709Srrs return (crc32c); 75163953Srrs} 76188388Srrs 77188299Spisouint32_t 78188067Srrssctp_calculate_cksum(struct mbuf *m, uint32_t offset) 79188067Srrs{ 80188067Srrs /* 81188067Srrs * given a mbuf chain with a packetheader offset by 'offset' 82188067Srrs * pointing at a sctphdr (with csum set to 0) go through the chain 83188067Srrs * of SCTP_BUF_NEXT()'s and calculate the SCTP checksum. This also 84188067Srrs * has a side bonus as it will calculate the total length of the 85188067Srrs * mbuf chain. Note: if offset is greater than the total mbuf 86188067Srrs * length, checksum=1, pktlen=0 is returned (ie. no real error code) 87188067Srrs */ 88188067Srrs uint32_t base = 0xffffffff; 89188067Srrs struct mbuf *at; 90188067Srrs 91188067Srrs at = m; 92188067Srrs /* find the correct mbuf and offset into mbuf */ 93188067Srrs while ((at != NULL) && (offset > (uint32_t) SCTP_BUF_LEN(at))) { 94188067Srrs offset -= SCTP_BUF_LEN(at); /* update remaining offset 95188067Srrs * left */ 96188067Srrs at = SCTP_BUF_NEXT(at); 97188067Srrs } 98188067Srrs while (at != NULL) { 99188067Srrs if ((SCTP_BUF_LEN(at) - offset) > 0) { 100188605Srrs base = calculate_crc32c(base, 101188605Srrs (unsigned char *)(SCTP_BUF_AT(at, offset)), 102188605Srrs (unsigned int)(SCTP_BUF_LEN(at) - offset)); 103188067Srrs } 104188067Srrs if (offset) { 105188605Srrs /* we only offset once into the first mbuf */ 106188067Srrs if (offset < (uint32_t) SCTP_BUF_LEN(at)) 107188067Srrs offset = 0; 108188067Srrs else 109188067Srrs offset -= SCTP_BUF_LEN(at); 110188067Srrs } 111188067Srrs at = SCTP_BUF_NEXT(at); 112188067Srrs } 113188605Srrs base = sctp_finalize_crc32c(base); 114188067Srrs return (base); 115188067Srrs} 116188067Srrs 117188605Srrs#endif /* !defined(SCTP_WITH_NO_CSUM) */ 118188605Srrs 119188605Srrs 120188067Srrsvoid 121205104Srrssctp_delayed_cksum(struct mbuf *m, uint32_t offset) 122188067Srrs{ 123211969Stuexen#if defined(SCTP_WITH_NO_CSUM) 124234996Stuexen#ifdef INVARIANTS 125211969Stuexen panic("sctp_delayed_cksum() called when using no SCTP CRC."); 126234996Stuexen#endif 127211969Stuexen#else 128188067Srrs uint32_t checksum; 129188067Srrs 130188067Srrs checksum = sctp_calculate_cksum(m, offset); 131188067Srrs SCTP_STAT_DECR(sctps_sendhwcrc); 132188067Srrs SCTP_STAT_INCR(sctps_sendswcrc); 133188067Srrs offset += offsetof(struct sctphdr, checksum); 134188067Srrs 135188067Srrs if (offset + sizeof(uint32_t) > (uint32_t) (m->m_len)) { 136234995Stuexen SCTP_PRINTF("sctp_delayed_cksum(): m->len: %d, off: %d.\n", 137215301Stuexen (uint32_t) m->m_len, offset); 138188067Srrs /* 139188067Srrs * XXX this shouldn't happen, but if it does, the correct 140188067Srrs * behavior may be to insert the checksum in the appropriate 141188067Srrs * next mbuf in the chain. 142188067Srrs */ 143188067Srrs return; 144188067Srrs } 145188067Srrs *(uint32_t *) (m->m_data + offset) = checksum; 146211969Stuexen#endif 147188067Srrs} 148