13b3a8eb9SGleb Smirnoff /* $OpenBSD: in4_cksum.c,v 1.7 2003/06/02 23:28:13 millert Exp $ */
23b3a8eb9SGleb Smirnoff /* $KAME: in4_cksum.c,v 1.10 2001/11/30 10:06:15 itojun Exp $ */
33b3a8eb9SGleb Smirnoff /* $NetBSD: in_cksum.c,v 1.13 1996/10/13 02:03:03 christos Exp $ */
43b3a8eb9SGleb Smirnoff
5*6e778a7eSPedro F. Giffuni /*-
651369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
751369649SPedro F. Giffuni *
83b3a8eb9SGleb Smirnoff * Copyright (C) 1999 WIDE Project.
93b3a8eb9SGleb Smirnoff * All rights reserved.
103b3a8eb9SGleb Smirnoff *
113b3a8eb9SGleb Smirnoff * Redistribution and use in source and binary forms, with or without
123b3a8eb9SGleb Smirnoff * modification, are permitted provided that the following conditions
133b3a8eb9SGleb Smirnoff * are met:
143b3a8eb9SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright
153b3a8eb9SGleb Smirnoff * notice, this list of conditions and the following disclaimer.
163b3a8eb9SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright
173b3a8eb9SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the
183b3a8eb9SGleb Smirnoff * documentation and/or other materials provided with the distribution.
193b3a8eb9SGleb Smirnoff * 3. Neither the name of the project nor the names of its contributors
203b3a8eb9SGleb Smirnoff * may be used to endorse or promote products derived from this software
213b3a8eb9SGleb Smirnoff * without specific prior written permission.
223b3a8eb9SGleb Smirnoff *
233b3a8eb9SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
243b3a8eb9SGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
253b3a8eb9SGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
263b3a8eb9SGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
273b3a8eb9SGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
283b3a8eb9SGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
293b3a8eb9SGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
303b3a8eb9SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
313b3a8eb9SGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
323b3a8eb9SGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
333b3a8eb9SGleb Smirnoff * SUCH DAMAGE.
343b3a8eb9SGleb Smirnoff */
353b3a8eb9SGleb Smirnoff
363b3a8eb9SGleb Smirnoff /*
373b3a8eb9SGleb Smirnoff * Copyright (c) 1988, 1992, 1993
383b3a8eb9SGleb Smirnoff * The Regents of the University of California. All rights reserved.
393b3a8eb9SGleb Smirnoff *
403b3a8eb9SGleb Smirnoff * Redistribution and use in source and binary forms, with or without
413b3a8eb9SGleb Smirnoff * modification, are permitted provided that the following conditions
423b3a8eb9SGleb Smirnoff * are met:
433b3a8eb9SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright
443b3a8eb9SGleb Smirnoff * notice, this list of conditions and the following disclaimer.
453b3a8eb9SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright
463b3a8eb9SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the
473b3a8eb9SGleb Smirnoff * documentation and/or other materials provided with the distribution.
483b3a8eb9SGleb Smirnoff * 3. Neither the name of the University nor the names of its contributors
493b3a8eb9SGleb Smirnoff * may be used to endorse or promote products derived from this software
503b3a8eb9SGleb Smirnoff * without specific prior written permission.
513b3a8eb9SGleb Smirnoff *
523b3a8eb9SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
533b3a8eb9SGleb Smirnoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
543b3a8eb9SGleb Smirnoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
553b3a8eb9SGleb Smirnoff * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
563b3a8eb9SGleb Smirnoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
573b3a8eb9SGleb Smirnoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
583b3a8eb9SGleb Smirnoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
593b3a8eb9SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
603b3a8eb9SGleb Smirnoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
613b3a8eb9SGleb Smirnoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
623b3a8eb9SGleb Smirnoff * SUCH DAMAGE.
633b3a8eb9SGleb Smirnoff */
643b3a8eb9SGleb Smirnoff
653b3a8eb9SGleb Smirnoff #include <sys/param.h>
663b3a8eb9SGleb Smirnoff #include <sys/systm.h>
673b3a8eb9SGleb Smirnoff #include <sys/mbuf.h>
683b3a8eb9SGleb Smirnoff
693b3a8eb9SGleb Smirnoff #include <netinet/in.h>
703b3a8eb9SGleb Smirnoff #include <netinet/in_systm.h>
713b3a8eb9SGleb Smirnoff #include <netinet/ip.h>
723b3a8eb9SGleb Smirnoff #include <netinet/ip_var.h>
733b3a8eb9SGleb Smirnoff
743b3a8eb9SGleb Smirnoff #include <machine/in_cksum.h>
753b3a8eb9SGleb Smirnoff
763b3a8eb9SGleb Smirnoff #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
773b3a8eb9SGleb Smirnoff #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; (void)ADDCARRY(sum);}
783b3a8eb9SGleb Smirnoff
793b3a8eb9SGleb Smirnoff int in4_cksum(struct mbuf *, u_int8_t, int, int);
803b3a8eb9SGleb Smirnoff
813b3a8eb9SGleb Smirnoff int
in4_cksum(struct mbuf * m,u_int8_t nxt,int off,int len)823b3a8eb9SGleb Smirnoff in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len)
833b3a8eb9SGleb Smirnoff {
843b3a8eb9SGleb Smirnoff union {
853b3a8eb9SGleb Smirnoff struct ipovly ipov;
863b3a8eb9SGleb Smirnoff u_int16_t w[10];
873b3a8eb9SGleb Smirnoff } u;
883b3a8eb9SGleb Smirnoff union {
893b3a8eb9SGleb Smirnoff u_int16_t s[2];
903b3a8eb9SGleb Smirnoff u_int32_t l;
913b3a8eb9SGleb Smirnoff } l_util;
923b3a8eb9SGleb Smirnoff
933b3a8eb9SGleb Smirnoff u_int16_t *w;
943b3a8eb9SGleb Smirnoff int psum;
953b3a8eb9SGleb Smirnoff int sum = 0;
963b3a8eb9SGleb Smirnoff
973b3a8eb9SGleb Smirnoff if (nxt != 0) {
983b3a8eb9SGleb Smirnoff /* pseudo header */
993b3a8eb9SGleb Smirnoff if (off < sizeof(struct ipovly))
1003b3a8eb9SGleb Smirnoff panic("in4_cksum: offset too short");
1013b3a8eb9SGleb Smirnoff if (m->m_len < sizeof(struct ip))
1023b3a8eb9SGleb Smirnoff panic("in4_cksum: bad mbuf chain");
1033b3a8eb9SGleb Smirnoff bzero(&u.ipov, sizeof(u.ipov));
1043b3a8eb9SGleb Smirnoff u.ipov.ih_len = htons(len);
1053b3a8eb9SGleb Smirnoff u.ipov.ih_pr = nxt;
1063b3a8eb9SGleb Smirnoff u.ipov.ih_src = mtod(m, struct ip *)->ip_src;
1073b3a8eb9SGleb Smirnoff u.ipov.ih_dst = mtod(m, struct ip *)->ip_dst;
1083b3a8eb9SGleb Smirnoff w = u.w;
1093b3a8eb9SGleb Smirnoff /* assumes sizeof(ipov) == 20 */
1103b3a8eb9SGleb Smirnoff sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4];
1113b3a8eb9SGleb Smirnoff sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9];
1123b3a8eb9SGleb Smirnoff }
1133b3a8eb9SGleb Smirnoff
1143b3a8eb9SGleb Smirnoff psum = in_cksum_skip(m, len + off, off);
1153b3a8eb9SGleb Smirnoff psum = ~psum & 0xffff;
1163b3a8eb9SGleb Smirnoff sum += psum;
1173b3a8eb9SGleb Smirnoff REDUCE;
1183b3a8eb9SGleb Smirnoff return (~sum & 0xffff);
1193b3a8eb9SGleb Smirnoff }
120