/* $OpenBSD: in4_cksum.c,v 1.7 2003/06/02 23:28:13 millert Exp $ */1/* $KAME: in4_cksum.c,v 1.10 2001/11/30 10:06:15 itojun Exp $ */2/* $NetBSD: in_cksum.c,v 1.13 1996/10/13 02:03:03 christos Exp $ */34/*-5* SPDX-License-Identifier: BSD-3-Clause6*7* Copyright (C) 1999 WIDE Project.8* All rights reserved.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18* 3. Neither the name of the project nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435/*36* Copyright (c) 1988, 1992, 199337* The Regents of the University of California. All rights reserved.38*39* Redistribution and use in source and binary forms, with or without40* modification, are permitted provided that the following conditions41* are met:42* 1. Redistributions of source code must retain the above copyright43* notice, this list of conditions and the following disclaimer.44* 2. Redistributions in binary form must reproduce the above copyright45* notice, this list of conditions and the following disclaimer in the46* documentation and/or other materials provided with the distribution.47* 3. Neither the name of the University nor the names of its contributors48* may be used to endorse or promote products derived from this software49* without specific prior written permission.50*51* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND52* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE53* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE54* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE55* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL56* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS57* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)58* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT59* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY60* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF61* SUCH DAMAGE.62*/6364#include <sys/param.h>65#include <sys/systm.h>66#include <sys/mbuf.h>6768#include <netinet/in.h>69#include <netinet/in_systm.h>70#include <netinet/ip.h>71#include <netinet/ip_var.h>7273#include <machine/in_cksum.h>7475#define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)76#define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; (void)ADDCARRY(sum);}7778int in4_cksum(struct mbuf *, u_int8_t, int, int);7980int81in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len)82{83union {84struct ipovly ipov;85u_int16_t w[10];86} u;87union {88u_int16_t s[2];89u_int32_t l;90} l_util;9192u_int16_t *w;93int psum;94int sum = 0;9596if (nxt != 0) {97/* pseudo header */98if (off < sizeof(struct ipovly))99panic("in4_cksum: offset too short");100if (m->m_len < sizeof(struct ip))101panic("in4_cksum: bad mbuf chain");102bzero(&u.ipov, sizeof(u.ipov));103u.ipov.ih_len = htons(len);104u.ipov.ih_pr = nxt;105u.ipov.ih_src = mtod(m, struct ip *)->ip_src;106u.ipov.ih_dst = mtod(m, struct ip *)->ip_dst;107w = u.w;108/* assumes sizeof(ipov) == 20 */109sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4];110sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9];111}112113psum = in_cksum_skip(m, len + off, off);114psum = ~psum & 0xffff;115sum += psum;116REDUCE;117return (~sum & 0xffff);118}119120121