1f8829a4aSRandall Stewart /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
4b1006367SRandall Stewart * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5807aad63SMichael Tuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6807aad63SMichael Tuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7f8829a4aSRandall Stewart *
8f8829a4aSRandall Stewart * Redistribution and use in source and binary forms, with or without
9f8829a4aSRandall Stewart * modification, are permitted provided that the following conditions are met:
10f8829a4aSRandall Stewart *
11f8829a4aSRandall Stewart * a) Redistributions of source code must retain the above copyright notice,
12f8829a4aSRandall Stewart * this list of conditions and the following disclaimer.
13f8829a4aSRandall Stewart *
14f8829a4aSRandall Stewart * b) Redistributions in binary form must reproduce the above copyright
15f8829a4aSRandall Stewart * notice, this list of conditions and the following disclaimer in
16f8829a4aSRandall Stewart * the documentation and/or other materials provided with the distribution.
17f8829a4aSRandall Stewart *
18f8829a4aSRandall Stewart * c) Neither the name of Cisco Systems, Inc. nor the names of its
19f8829a4aSRandall Stewart * contributors may be used to endorse or promote products derived
20f8829a4aSRandall Stewart * from this software without specific prior written permission.
21f8829a4aSRandall Stewart *
22f8829a4aSRandall Stewart * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23f8829a4aSRandall Stewart * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24f8829a4aSRandall Stewart * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f8829a4aSRandall Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26f8829a4aSRandall Stewart * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27f8829a4aSRandall Stewart * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28f8829a4aSRandall Stewart * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29f8829a4aSRandall Stewart * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30f8829a4aSRandall Stewart * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31f8829a4aSRandall Stewart * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32f8829a4aSRandall Stewart * THE POSSIBILITY OF SUCH DAMAGE.
33f8829a4aSRandall Stewart */
34f8829a4aSRandall Stewart
35f8829a4aSRandall Stewart #include <sys/cdefs.h>
36cd901504SMichael Tuexen #include "opt_sctp.h"
37cd901504SMichael Tuexen
38cd901504SMichael Tuexen #include <sys/param.h>
39cd901504SMichael Tuexen #include <sys/systm.h>
4095033af9SMark Johnston #include <sys/gsb_crc32.h>
41cd901504SMichael Tuexen #include <sys/mbuf.h>
42f8829a4aSRandall Stewart
4395033af9SMark Johnston #include <netinet/sctp.h>
44cd901504SMichael Tuexen #include <netinet/sctp_crc32.h>
45e6db509dSMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
4695033af9SMark Johnston #include <netinet/sctp_os.h>
4795033af9SMark Johnston #include <netinet/sctp_pcb.h>
48cd901504SMichael Tuexen #endif
49a99b6783SRandall Stewart
50f8829a4aSRandall Stewart static uint32_t
sctp_finalize_crc32c(uint32_t crc32c)51b0471b4bSMichael Tuexen sctp_finalize_crc32c(uint32_t crc32c)
52b0471b4bSMichael Tuexen {
53f8829a4aSRandall Stewart #if BYTE_ORDER == BIG_ENDIAN
5476e03cc9SMichael Tuexen uint32_t byte0, byte1, byte2, byte3;
55f8829a4aSRandall Stewart #endif
56cd901504SMichael Tuexen
57f8829a4aSRandall Stewart #if BYTE_ORDER == BIG_ENDIAN
58f8829a4aSRandall Stewart /*
5976e03cc9SMichael Tuexen * For BIG-ENDIAN platforms, the result is in LITTLE-ENDIAN byte
6076e03cc9SMichael Tuexen * order. For LITTLE-ENDIAN platforms, the result is in in
6176e03cc9SMichael Tuexen * BIG-ENDIAN byte order. So for BIG-ENDIAN platforms the bytes must
6276e03cc9SMichael Tuexen * be swapped to return the result always in network byte order (aka
6376e03cc9SMichael Tuexen * BIG-ENDIAN).
64f8829a4aSRandall Stewart */
6576e03cc9SMichael Tuexen byte0 = crc32c & 0x000000ff;
6676e03cc9SMichael Tuexen byte1 = (crc32c >> 8) & 0x000000ff;
6776e03cc9SMichael Tuexen byte2 = (crc32c >> 16) & 0x000000ff;
6876e03cc9SMichael Tuexen byte3 = (crc32c >> 24) & 0x000000ff;
69c105859eSRandall Stewart crc32c = ((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3);
70f8829a4aSRandall Stewart #endif
7176e03cc9SMichael Tuexen return (~crc32c);
72f8829a4aSRandall Stewart }
7374246b27SRandall Stewart
74b4d758a0SMark Johnston static int
sctp_calculate_cksum_cb(void * arg,void * data,u_int len)75b4d758a0SMark Johnston sctp_calculate_cksum_cb(void *arg, void *data, u_int len)
76b4d758a0SMark Johnston {
77b4d758a0SMark Johnston uint32_t *basep;
78b4d758a0SMark Johnston
79b4d758a0SMark Johnston basep = arg;
80b4d758a0SMark Johnston *basep = calculate_crc32c(*basep, data, len);
81b4d758a0SMark Johnston return (0);
82b4d758a0SMark Johnston }
83b4d758a0SMark Johnston
84cd901504SMichael Tuexen /*
85cd901504SMichael Tuexen * Compute the SCTP checksum in network byte order for a given mbuf chain m
86cd901504SMichael Tuexen * which contains an SCTP packet starting at offset.
87cd901504SMichael Tuexen * Since this function is also called by ipfw, don't assume that
88cd901504SMichael Tuexen * it is compiled on a kernel with SCTP support.
89cd901504SMichael Tuexen */
90a99b6783SRandall Stewart uint32_t
sctp_calculate_cksum(struct mbuf * m,int32_t offset)91*11c4d4b9SMichael Tuexen sctp_calculate_cksum(struct mbuf *m, int32_t offset)
92b0471b4bSMichael Tuexen {
93b4d758a0SMark Johnston uint32_t base;
94b4d758a0SMark Johnston int len;
95a99b6783SRandall Stewart
96b4d758a0SMark Johnston M_ASSERTPKTHDR(m);
97b4d758a0SMark Johnston KASSERT(offset < m->m_pkthdr.len,
98b4d758a0SMark Johnston ("%s: invalid offset %u into mbuf %p", __func__, offset, m));
99b4d758a0SMark Johnston
100b4d758a0SMark Johnston base = 0xffffffff;
101b4d758a0SMark Johnston len = m->m_pkthdr.len - offset;
102b4d758a0SMark Johnston (void)m_apply(m, offset, len, sctp_calculate_cksum_cb, &base);
103b4d758a0SMark Johnston return (sctp_finalize_crc32c(base));
104a99b6783SRandall Stewart }
105eef9e53eSRandall Stewart
10695033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
107e6db509dSMark Johnston
108e6db509dSMark Johnston VNET_DEFINE(struct sctp_base_info, system_base_info);
109e6db509dSMark Johnston
110cd901504SMichael Tuexen /*
111cd901504SMichael Tuexen * Compute and insert the SCTP checksum in network byte order for a given
112cd901504SMichael Tuexen * mbuf chain m which contains an SCTP packet starting at offset.
113cd901504SMichael Tuexen */
114a99b6783SRandall Stewart void
sctp_delayed_cksum(struct mbuf * m,uint32_t offset)1151966e5b5SRandall Stewart sctp_delayed_cksum(struct mbuf *m, uint32_t offset)
116a99b6783SRandall Stewart {
117a99b6783SRandall Stewart uint32_t checksum;
118a99b6783SRandall Stewart
119a99b6783SRandall Stewart checksum = sctp_calculate_cksum(m, offset);
120a99b6783SRandall Stewart SCTP_STAT_DECR(sctps_sendhwcrc);
121a99b6783SRandall Stewart SCTP_STAT_INCR(sctps_sendswcrc);
122a99b6783SRandall Stewart offset += offsetof(struct sctphdr, checksum);
123a99b6783SRandall Stewart
12452f6a809SMichael Tuexen if (offset + sizeof(uint32_t) > (uint32_t)(m->m_pkthdr.len)) {
125cd901504SMichael Tuexen #ifdef INVARIANTS
12652f6a809SMichael Tuexen panic("sctp_delayed_cksum(): m->m_pkthdr.len: %d, offset: %u.",
12752f6a809SMichael Tuexen m->m_pkthdr.len, offset);
128cd901504SMichael Tuexen #else
12952f6a809SMichael Tuexen SCTP_PRINTF("sctp_delayed_cksum(): m->m_pkthdr.len: %d, offset: %u.\n",
13052f6a809SMichael Tuexen m->m_pkthdr.len, offset);
131cd901504SMichael Tuexen #endif
132a99b6783SRandall Stewart return;
133a99b6783SRandall Stewart }
13452f6a809SMichael Tuexen m_copyback(m, (int)offset, (int)sizeof(uint32_t), (caddr_t)&checksum);
135a99b6783SRandall Stewart }
136cd901504SMichael Tuexen #endif
137