]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/in_cksum.c
libc: Fix most issues reported by mandoc
[FreeBSD/FreeBSD.git] / sys / mips / mips / in_cksum.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1988, 1992, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1996
7  *      Matt Thomas <matt@3am-software.com>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *      @(#)in_cksum.c  8.1 (Berkeley) 6/10/93
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <sys/param.h>
44 #include <sys/mbuf.h>
45 #include <sys/systm.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/in.h>
48 #include <netinet/ip.h>
49 #include <machine/in_cksum.h>
50
51 /*
52  * Checksum routine for Internet Protocol family headers
53  *    (Portable Alpha version).
54  *
55  * This routine is very heavily used in the network
56  * code and should be modified for each CPU to be as fast as possible.
57  */
58
59 #define ADDCARRY(x)  (x > 65535 ? x -= 65535 : x)
60 #define REDUCE32                                                          \
61     {                                                                     \
62         q_util.q = sum;                                                   \
63         sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3];      \
64     }
65 #define REDUCE16                                                          \
66     {                                                                     \
67         q_util.q = sum;                                                   \
68         l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
69         sum = l_util.s[0] + l_util.s[1];                                  \
70         ADDCARRY(sum);                                                    \
71     }
72
73 static const u_int32_t in_masks[] = {
74 #if _BYTE_ORDER == _LITTLE_ENDIAN
75         /*0 bytes*/ /*1 byte*/  /*2 bytes*/ /*3 bytes*/
76         0x00000000, 0x000000FF, 0x0000FFFF, 0x00FFFFFF, /* offset 0 */
77         0x00000000, 0x0000FF00, 0x00FFFF00, 0xFFFFFF00, /* offset 1 */
78         0x00000000, 0x00FF0000, 0xFFFF0000, 0xFFFF0000, /* offset 2 */
79         0x00000000, 0xFF000000, 0xFF000000, 0xFF000000, /* offset 3 */
80 #else
81         /*0 bytes*/ /*1 byte*/  /*2 bytes*/ /*3 bytes*/
82         0x00000000, 0xFF000000, 0xFFFF0000, 0xFFFFFF00, /* offset 0 */
83         0x00000000, 0x00FF0000, 0x00FFFF00, 0x00FFFFFF, /* offset 1 */
84         0x00000000, 0x0000FF00, 0x0000FFFF, 0x0000FFFF, /* offset 2 */
85         0x00000000, 0x000000FF, 0x000000FF, 0x000000FF, /* offset 3 */
86 #endif
87 };
88
89 union l_util {
90         u_int16_t s[2];
91         u_int32_t l;
92 };
93 union q_util {
94         u_int16_t s[4];
95         u_int32_t l[2];
96         u_int64_t q;
97 };
98
99 static u_int64_t
100 in_cksumdata(const void *buf, int len)
101 {
102         const u_int32_t *lw = (const u_int32_t *) buf;
103         u_int64_t sum = 0;
104         u_int64_t prefilled;
105         int offset;
106         union q_util q_util;
107
108         if ((3 & (long) lw) == 0 && len == 20) {
109                 sum = (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3] + lw[4];
110                 REDUCE32;
111                 return sum;
112         }
113
114         if ((offset = 3 & (long) lw) != 0) {
115                 const u_int32_t *masks = in_masks + (offset << 2);
116                 lw = (u_int32_t *) (((long) lw) - offset);
117                 sum = *lw++ & masks[len >= 3 ? 3 : len];
118                 len -= 4 - offset;
119                 if (len <= 0) {
120                         REDUCE32;
121                         return sum;
122                 }
123         }
124 #if 0
125         /*
126          * Force to cache line boundary.
127          */
128         offset = 32 - (0x1f & (long) lw);
129         if (offset < 32 && len > offset) {
130                 len -= offset;
131                 if (4 & offset) {
132                         sum += (u_int64_t) lw[0];
133                         lw += 1;
134                 }
135                 if (8 & offset) {
136                         sum += (u_int64_t) lw[0] + lw[1];
137                         lw += 2;
138                 }
139                 if (16 & offset) {
140                         sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
141                         lw += 4;
142                 }
143         }
144 #endif
145         /*
146          * access prefilling to start load of next cache line.
147          * then add current cache line
148          * save result of prefilling for loop iteration.
149          */
150         prefilled = lw[0];
151         while ((len -= 32) >= 4) {
152                 u_int64_t prefilling = lw[8];
153                 sum += prefilled + lw[1] + lw[2] + lw[3]
154                         + lw[4] + lw[5] + lw[6] + lw[7];
155                 lw += 8;
156                 prefilled = prefilling;
157         }
158         if (len >= 0) {
159                 sum += prefilled + lw[1] + lw[2] + lw[3]
160                         + lw[4] + lw[5] + lw[6] + lw[7];
161                 lw += 8;
162         } else {
163                 len += 32;
164         }
165         while ((len -= 16) >= 0) {
166                 sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];
167                 lw += 4;
168         }
169         len += 16;
170         while ((len -= 4) >= 0) {
171                 sum += (u_int64_t) *lw++;
172         }
173         len += 4;
174         if (len > 0)
175                 sum += (u_int64_t) (in_masks[len] & *lw);
176         REDUCE32;
177         return sum;
178 }
179
180 u_short
181 in_addword(u_short a, u_short b)
182 {
183         u_int64_t sum = a + b;
184
185         ADDCARRY(sum);
186         return (sum);
187 }
188
189 u_short
190 in_pseudo(u_int32_t a, u_int32_t b, u_int32_t c)
191 {
192         u_int64_t sum;
193         union q_util q_util;
194         union l_util l_util;
195
196         sum = (u_int64_t) a + b + c;
197         REDUCE16;
198         return (sum);
199 }
200
201 u_short
202 in_cksum_skip(struct mbuf *m, int len, int skip)
203 {
204         u_int64_t sum = 0;
205         int mlen = 0;
206         int clen = 0;
207         caddr_t addr;
208         union q_util q_util;
209         union l_util l_util;
210
211         len -= skip;
212         for (; skip && m; m = m->m_next) {
213                 if (m->m_len > skip) {
214                         mlen = m->m_len - skip;
215                         addr = mtod(m, caddr_t) + skip;
216                         goto skip_start;
217                 } else {
218                         skip -= m->m_len;
219                 }
220         }
221
222         for (; m && len; m = m->m_next) {
223                 if (m->m_len == 0)
224                         continue;
225                 mlen = m->m_len;
226                 addr = mtod(m, caddr_t);
227 skip_start:
228                 if (len < mlen)
229                         mlen = len;
230
231                 if ((clen ^ (uintptr_t) addr) & 1)
232                         sum += in_cksumdata(addr, mlen) << 8;
233                 else
234                         sum += in_cksumdata(addr, mlen);
235
236                 clen += mlen;
237                 len -= mlen;
238         }
239         REDUCE16;
240         return (~sum & 0xffff);
241 }
242
243 u_int in_cksum_hdr(const struct ip *ip)
244 {
245         u_int64_t sum = in_cksumdata(ip, sizeof(struct ip));
246         union q_util q_util;
247         union l_util l_util;
248         REDUCE16;
249         return (~sum & 0xffff);
250 }