/*-1* SPDX-License-Identifier: BSD-4-Clause2*3* Copyright (c) 1988, 1992, 19934* The Regents of the University of California. All rights reserved.5* Copyright (c) 19966* Matt Thomas <[email protected]>7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16* 3. All advertising materials mentioning features or use of this software17* must display the following acknowledgement:18* This product includes software developed by the University of19* California, Berkeley and its contributors.20* 4. Neither the name of the University nor the names of its contributors21* may be used to endorse or promote products derived from this software22* without specific prior written permission.23*24* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND25* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE26* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE27* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE28* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL29* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS30* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)31* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT32* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY33* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF34* SUCH DAMAGE.35*/3637#include <sys/param.h>38#include <sys/mbuf.h>39#include <sys/systm.h>40#include <netinet/in_systm.h>41#include <netinet/in.h>42#include <netinet/ip.h>43#include <machine/in_cksum.h>4445/*46* These implementations may be overridden on a per-platform basis. On47* platforms with a direct map, the implementation of in_cksum() must handle48* unmapped mbufs.49*/50#ifndef HAVE_MD_IN_CKSUM5152/*53* Checksum routine for Internet Protocol family headers54* (Portable Alpha version).55*56* This routine is very heavily used in the network57* code and should be modified for each CPU to be as fast as possible.58*/5960#define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)61#define REDUCE32 \62{ \63q_util.q = sum; \64sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \65}66#define REDUCE16 \67{ \68q_util.q = sum; \69l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \70sum = l_util.s[0] + l_util.s[1]; \71ADDCARRY(sum); \72}7374static const u_int32_t in_masks[] = {75#if _BYTE_ORDER == _LITTLE_ENDIAN76/*0 bytes*/ /*1 byte*/ /*2 bytes*/ /*3 bytes*/770x00000000, 0x000000FF, 0x0000FFFF, 0x00FFFFFF, /* offset 0 */780x00000000, 0x0000FF00, 0x00FFFF00, 0xFFFFFF00, /* offset 1 */790x00000000, 0x00FF0000, 0xFFFF0000, 0xFFFF0000, /* offset 2 */800x00000000, 0xFF000000, 0xFF000000, 0xFF000000, /* offset 3 */81#else82/*0 bytes*/ /*1 byte*/ /*2 bytes*/ /*3 bytes*/830x00000000, 0xFF000000, 0xFFFF0000, 0xFFFFFF00, /* offset 0 */840x00000000, 0x00FF0000, 0x00FFFF00, 0x00FFFFFF, /* offset 1 */850x00000000, 0x0000FF00, 0x0000FFFF, 0x0000FFFF, /* offset 2 */860x00000000, 0x000000FF, 0x000000FF, 0x000000FF, /* offset 3 */87#endif88};8990union l_util {91u_int16_t s[2];92u_int32_t l;93};94union q_util {95u_int16_t s[4];96u_int32_t l[2];97u_int64_t q;98};99100static u_int64_t101in_cksumdata(const void *buf, int len)102{103const u_int32_t *lw = (const u_int32_t *) buf;104u_int64_t sum = 0;105u_int64_t prefilled;106int offset;107union q_util q_util;108109if ((3 & (long) lw) == 0 && len == 20) {110sum = (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3] + lw[4];111REDUCE32;112return sum;113}114115if ((offset = 3 & (long) lw) != 0) {116const u_int32_t *masks = in_masks + (offset << 2);117lw = (u_int32_t *) (((long) lw) - offset);118sum = *lw++ & masks[len >= 3 ? 3 : len];119len -= 4 - offset;120if (len <= 0) {121REDUCE32;122return sum;123}124}125#if 0126/*127* Force to cache line boundary.128*/129offset = 32 - (0x1f & (long) lw);130if (offset < 32 && len > offset) {131len -= offset;132if (4 & offset) {133sum += (u_int64_t) lw[0];134lw += 1;135}136if (8 & offset) {137sum += (u_int64_t) lw[0] + lw[1];138lw += 2;139}140if (16 & offset) {141sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];142lw += 4;143}144}145#endif146/*147* access prefilling to start load of next cache line.148* then add current cache line149* save result of prefilling for loop iteration.150*/151prefilled = lw[0];152while ((len -= 32) >= 4) {153u_int64_t prefilling = lw[8];154sum += prefilled + lw[1] + lw[2] + lw[3]155+ lw[4] + lw[5] + lw[6] + lw[7];156lw += 8;157prefilled = prefilling;158}159if (len >= 0) {160sum += prefilled + lw[1] + lw[2] + lw[3]161+ lw[4] + lw[5] + lw[6] + lw[7];162lw += 8;163} else {164len += 32;165}166while ((len -= 16) >= 0) {167sum += (u_int64_t) lw[0] + lw[1] + lw[2] + lw[3];168lw += 4;169}170len += 16;171while ((len -= 4) >= 0) {172sum += (u_int64_t) *lw++;173}174len += 4;175if (len > 0)176sum += (u_int64_t) (in_masks[len] & *lw);177REDUCE32;178return sum;179}180181u_short182in_addword(u_short a, u_short b)183{184u_int64_t sum = a + b;185186ADDCARRY(sum);187return (sum);188}189190u_short191in_pseudo(u_int32_t a, u_int32_t b, u_int32_t c)192{193u_int64_t sum;194union q_util q_util;195union l_util l_util;196197sum = (u_int64_t) a + b + c;198REDUCE16;199return (sum);200}201202struct cksum_skip_partial_args {203uint64_t csum;204int clen;205};206207static int208in_cksum_skip_partial(void *arg, void *data, u_int len)209{210struct cksum_skip_partial_args *a;211212a = arg;213if (((uintptr_t)data ^ a->clen) & 1)214a->csum += in_cksumdata(data, len) << 8;215else216a->csum += in_cksumdata(data, len);217a->clen += len;218return (0);219}220221u_short222in_cksum_skip(struct mbuf *m, int len, int skip)223{224struct cksum_skip_partial_args a;225union q_util q_util;226union l_util l_util;227uint64_t sum;228229len -= skip;230231/*232* The use of m_apply() allows this routine to operate on unmapped233* mbufs.234*/235a.csum = 0;236a.clen = 0;237(void)m_apply(m, skip, len, in_cksum_skip_partial, &a);238sum = a.csum;239REDUCE16;240return (~sum & 0xffff);241}242243u_int in_cksum_hdr(const struct ip *ip)244{245u_int64_t sum = in_cksumdata(ip, sizeof(struct ip));246union q_util q_util;247union l_util l_util;248REDUCE16;249return (~sum & 0xffff);250}251252#endif /* !HAVE_MD_IN_CKSUM */253254255