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