/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the project nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*30* $KAME: in6_cksum.c,v 1.10 2000/12/03 00:53:59 itojun Exp $31*/3233/*-34* Copyright (c) 1988, 1992, 199335* The Regents of the University of California. All rights reserved.36*37* Redistribution and use in source and binary forms, with or without38* modification, are permitted provided that the following conditions39* are met:40* 1. Redistributions of source code must retain the above copyright41* notice, this list of conditions and the following disclaimer.42* 2. Redistributions in binary form must reproduce the above copyright43* notice, this list of conditions and the following disclaimer in the44* documentation and/or other materials provided with the distribution.45* 3. Neither the name of the University nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND50* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE51* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE52* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE53* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL54* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS55* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)56* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT57* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY58* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF59* SUCH DAMAGE.60*/6162#include <sys/param.h>63#include <sys/mbuf.h>64#include <sys/systm.h>65#include <netinet/in.h>66#include <netinet/ip6.h>67#include <netinet6/scope6_var.h>6869/*70* Checksum routine for Internet Protocol family headers (Portable Version).71*72* This routine is very heavily used in the network73* code and should be modified for each CPU to be as fast as possible.74*/7576#define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)77#define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; (void)ADDCARRY(sum);}7879union l_util {80uint16_t s[2];81uint32_t l;82};8384union s_util {85uint8_t c[2];86uint16_t s;87};8889static int90_in6_cksum_pseudo(struct ip6_hdr *ip6, uint32_t len, uint8_t nxt, uint16_t csum)91{92int sum;93uint16_t scope, *w;94union {95u_int16_t phs[4];96struct {97u_int32_t ph_len;98u_int8_t ph_zero[3];99u_int8_t ph_nxt;100} __packed ph;101} uph;102103sum = csum;104105/*106* First create IP6 pseudo header and calculate a summary.107*/108uph.ph.ph_len = htonl(len);109uph.ph.ph_zero[0] = uph.ph.ph_zero[1] = uph.ph.ph_zero[2] = 0;110uph.ph.ph_nxt = nxt;111112/* Payload length and upper layer identifier. */113sum += uph.phs[0]; sum += uph.phs[1];114sum += uph.phs[2]; sum += uph.phs[3];115116/* IPv6 source address. */117scope = in6_getscope(&ip6->ip6_src);118w = (u_int16_t *)&ip6->ip6_src;119sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];120sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];121if (scope != 0)122sum -= scope;123124/* IPv6 destination address. */125scope = in6_getscope(&ip6->ip6_dst);126w = (u_int16_t *)&ip6->ip6_dst;127sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];128sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];129if (scope != 0)130sum -= scope;131132return (sum);133}134135int136in6_cksum_pseudo(struct ip6_hdr *ip6, uint32_t len, uint8_t nxt, uint16_t csum)137{138union l_util l_util;139int sum;140141sum = _in6_cksum_pseudo(ip6, len, nxt, csum);142REDUCE;143return (sum);144}145146static int147in6_cksumdata(void *data, int *lenp, uint8_t *residp, int rlen)148{149union l_util l_util;150union s_util s_util;151uint16_t *w;152int len, sum;153bool byte_swapped;154155KASSERT(*lenp >= 0, ("%s: negative len %d", __func__, *lenp));156KASSERT(rlen == 0 || rlen == 1, ("%s: rlen %d", __func__, rlen));157158len = *lenp;159sum = 0;160161if (len == 0) {162len = rlen;163goto out;164}165166byte_swapped = false;167w = data;168169/*170* Do we have a residual byte left over from the previous buffer?171*/172if (rlen == 1) {173s_util.c[0] = *residp;174s_util.c[1] = *(uint8_t *)w;175sum += s_util.s;176w = (uint16_t *)((uint8_t *)w + 1);177len--;178rlen = 0;179}180181/*182* Force to even boundary.183*/184if ((1 & (uintptr_t)w) && len > 0) {185REDUCE;186sum <<= 8;187s_util.c[0] = *(uint8_t *)w;188w = (uint16_t *)((uint8_t *)w + 1);189len--;190byte_swapped = true;191}192193/*194* Unroll the loop to make overhead from branches &c small.195*/196while ((len -= 32) >= 0) {197sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];198sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];199sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];200sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];201w += 16;202}203len += 32;204while ((len -= 8) >= 0) {205sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];206w += 4;207}208len += 8;209if (len == 0 && !byte_swapped)210goto out;211REDUCE;212while ((len -= 2) >= 0) {213sum += *w++;214}215if (byte_swapped) {216REDUCE;217sum <<= 8;218if (len == -1) {219s_util.c[1] = *(uint8_t *)w;220sum += s_util.s;221} else /* len == -2 */222*residp = s_util.c[0];223len++;224} else if (len == -1)225*residp = *(uint8_t *)w;226out:227*lenp = len & 1;228return (sum);229}230231struct in6_cksum_partial_arg {232int sum;233int rlen;234uint8_t resid;235};236237static int238in6_cksum_partial_one(void *_arg, void *data, u_int len)239{240struct in6_cksum_partial_arg *arg = _arg;241242arg->sum += in6_cksumdata(data, &len, &arg->resid, arg->rlen);243arg->rlen = len;244return (0);245}246247/*248* m MUST contain a contiguous IP6 header.249* off_l3 is an offset where ipv6 header starts.250* off_l4 is an offset where TCP/UDP/ICMP6 header starts.251* len is a total length of a transport segment.252* (e.g. TCP header + TCP payload)253* cov is the number of bytes to be taken into account for the checksum254*/255int256in6_cksum_partial_l2(struct mbuf *m, uint8_t nxt, uint32_t off_l3,257uint32_t off_l4, uint32_t len, uint32_t cov)258{259struct in6_cksum_partial_arg arg;260union l_util l_util;261union s_util s_util;262struct ip6_hdr *ip6;263uint16_t *w, scope;264int sum;265union {266uint16_t phs[4];267struct {268uint32_t ph_len;269uint8_t ph_zero[3];270uint8_t ph_nxt;271} __packed ph;272} uph;273274/* Sanity check. */275KASSERT(m->m_pkthdr.len >= off_l4 + len,276("%s: mbuf len (%d) < off(%d)+len(%d)",277__func__, m->m_pkthdr.len, off_l4, len));278KASSERT(m->m_len >= off_l3 + sizeof(*ip6),279("%s: mbuf len %d < sizeof(ip6)", __func__, m->m_len));280281/*282* First create IP6 pseudo header and calculate a summary.283*/284uph.ph.ph_len = htonl(len);285uph.ph.ph_zero[0] = uph.ph.ph_zero[1] = uph.ph.ph_zero[2] = 0;286uph.ph.ph_nxt = nxt;287288/* Payload length and upper layer identifier. */289sum = uph.phs[0]; sum += uph.phs[1];290sum += uph.phs[2]; sum += uph.phs[3];291292ip6 = mtodo(m, off_l3);293294/* IPv6 source address. */295scope = in6_getscope(&ip6->ip6_src);296w = (uint16_t *)&ip6->ip6_src;297sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];298sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];299if (scope != 0)300sum -= scope;301302/* IPv6 destination address. */303scope = in6_getscope(&ip6->ip6_dst);304w = (uint16_t *)&ip6->ip6_dst;305sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];306sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];307if (scope != 0)308sum -= scope;309310/*311* Loop over the rest of the mbuf chain and compute the rest of the312* checksum. m_apply() handles unmapped mbufs.313*/314arg.sum = sum;315arg.rlen = 0;316(void)m_apply(m, off_l4, cov, in6_cksum_partial_one, &arg);317sum = arg.sum;318319/*320* Handle a residual byte.321*/322if (arg.rlen == 1) {323s_util.c[0] = arg.resid;324s_util.c[1] = 0;325sum += s_util.s;326}327REDUCE;328return (~sum & 0xffff);329}330331int332in6_cksum_partial(struct mbuf *m, uint8_t nxt, uint32_t off, uint32_t len,333uint32_t cov)334{335return (in6_cksum_partial_l2(m, nxt, 0, off, len, cov));336}337338int339in6_cksum(struct mbuf *m, uint8_t nxt, uint32_t off, uint32_t len)340{341return (in6_cksum_partial(m, nxt, off, len, len));342}343344345