2008-01-26 00:12:10 +03:00
|
|
|
/* $NetBSD: in4_cksum.c,v 1.15 2008/01/25 21:12:15 joerg Exp $ */
|
1999-11-30 16:34:47 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 1999 WIDE Project.
|
|
|
|
* All rights reserved.
|
2001-12-21 05:50:28 +03:00
|
|
|
*
|
1999-11-30 16:34:47 +03:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2001-12-21 05:50:28 +03:00
|
|
|
*
|
1999-11-30 16:34:47 +03:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1988, 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1999-11-30 16:34:47 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
|
|
|
|
*/
|
|
|
|
|
2001-11-13 03:32:34 +03:00
|
|
|
#include <sys/cdefs.h>
|
2008-01-26 00:12:10 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.15 2008/01/25 21:12:15 joerg Exp $");
|
2001-11-13 03:32:34 +03:00
|
|
|
|
1999-11-30 16:34:47 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/systm.h>
|
1999-12-13 18:17:17 +03:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/route.h>
|
1999-11-30 16:34:47 +03:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1999-12-13 18:17:17 +03:00
|
|
|
#include <netinet/ip_var.h>
|
1999-11-30 16:34:47 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Checksum routine for Internet Protocol family headers (Portable Version).
|
|
|
|
* This is only for IPv4 pseudo header checksum.
|
|
|
|
* No need to clear non-pseudo-header fields in IPv4 header.
|
|
|
|
* len is for actual payload size, and does not include IPv4 header and
|
|
|
|
* skipped header chain (off + len should be equal to the whole packet).
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2005-02-03 06:49:01 +03:00
|
|
|
in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len)
|
1999-11-30 16:34:47 +03:00
|
|
|
{
|
2008-01-26 00:12:10 +03:00
|
|
|
uint32_t sum;
|
2002-07-29 13:14:36 +04:00
|
|
|
union {
|
|
|
|
struct ipovly ipov;
|
2008-01-26 00:12:10 +03:00
|
|
|
uint16_t w[10];
|
2002-07-29 13:14:36 +04:00
|
|
|
} u;
|
2008-01-26 00:12:10 +03:00
|
|
|
uint16_t *w;
|
|
|
|
|
|
|
|
sum = 0;
|
1999-11-30 16:34:47 +03:00
|
|
|
|
2008-01-26 00:12:10 +03:00
|
|
|
if (nxt == 0)
|
|
|
|
goto skip_phdr;
|
1999-11-30 16:34:47 +03:00
|
|
|
|
2008-01-26 00:12:10 +03:00
|
|
|
/* pseudo header */
|
|
|
|
if (off < sizeof(struct ipovly))
|
|
|
|
panic("in4_cksum: offset too short");
|
|
|
|
if (m->m_len < sizeof(struct ip))
|
|
|
|
panic("in4_cksum: bad mbuf chain");
|
|
|
|
bzero(&u.ipov, sizeof(u.ipov));
|
|
|
|
u.ipov.ih_len = htons(len);
|
|
|
|
u.ipov.ih_pr = nxt;
|
|
|
|
u.ipov.ih_src = mtod(m, struct ip *)->ip_src;
|
|
|
|
u.ipov.ih_dst = mtod(m, struct ip *)->ip_dst;
|
|
|
|
w = u.w;
|
|
|
|
/* assumes sizeof(ipov) == 20 */
|
|
|
|
sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4];
|
|
|
|
sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9];
|
1999-11-30 16:34:47 +03:00
|
|
|
|
2008-01-26 00:12:10 +03:00
|
|
|
skip_phdr:
|
|
|
|
return cpu_in_cksum(m, len, off, sum);
|
1999-11-30 16:34:47 +03:00
|
|
|
}
|