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