Home | History | Annotate | Line # | Download | only in netinet
in4_cksum.c revision 1.15
      1  1.15   joerg /*	$NetBSD: in4_cksum.c,v 1.15 2008/01/25 21:12:15 joerg Exp $	*/
      2   1.2  itojun 
      3   1.2  itojun /*
      4   1.2  itojun  * Copyright (C) 1999 WIDE Project.
      5   1.2  itojun  * All rights reserved.
      6   1.8  itojun  *
      7   1.2  itojun  * Redistribution and use in source and binary forms, with or without
      8   1.2  itojun  * modification, are permitted provided that the following conditions
      9   1.2  itojun  * are met:
     10   1.2  itojun  * 1. Redistributions of source code must retain the above copyright
     11   1.2  itojun  *    notice, this list of conditions and the following disclaimer.
     12   1.2  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.2  itojun  *    notice, this list of conditions and the following disclaimer in the
     14   1.2  itojun  *    documentation and/or other materials provided with the distribution.
     15   1.2  itojun  * 3. Neither the name of the project nor the names of its contributors
     16   1.2  itojun  *    may be used to endorse or promote products derived from this software
     17   1.2  itojun  *    without specific prior written permission.
     18   1.8  itojun  *
     19   1.2  itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20   1.2  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.2  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.2  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23   1.2  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.2  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.2  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.2  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.2  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.2  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.2  itojun  * SUCH DAMAGE.
     30   1.2  itojun  */
     31   1.2  itojun 
     32   1.2  itojun /*
     33   1.2  itojun  * Copyright (c) 1988, 1992, 1993
     34   1.2  itojun  *	The Regents of the University of California.  All rights reserved.
     35   1.2  itojun  *
     36   1.2  itojun  * Redistribution and use in source and binary forms, with or without
     37   1.2  itojun  * modification, are permitted provided that the following conditions
     38   1.2  itojun  * are met:
     39   1.2  itojun  * 1. Redistributions of source code must retain the above copyright
     40   1.2  itojun  *    notice, this list of conditions and the following disclaimer.
     41   1.2  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.2  itojun  *    notice, this list of conditions and the following disclaimer in the
     43   1.2  itojun  *    documentation and/or other materials provided with the distribution.
     44  1.10     agc  * 3. Neither the name of the University nor the names of its contributors
     45   1.2  itojun  *    may be used to endorse or promote products derived from this software
     46   1.2  itojun  *    without specific prior written permission.
     47   1.2  itojun  *
     48   1.2  itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49   1.2  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50   1.2  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51   1.2  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52   1.2  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53   1.2  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54   1.2  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55   1.2  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56   1.2  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57   1.2  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58   1.2  itojun  * SUCH DAMAGE.
     59   1.2  itojun  *
     60   1.2  itojun  *	@(#)in_cksum.c	8.1 (Berkeley) 6/10/93
     61   1.2  itojun  */
     62   1.7   lukem 
     63   1.7   lukem #include <sys/cdefs.h>
     64  1.15   joerg __KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.15 2008/01/25 21:12:15 joerg Exp $");
     65   1.2  itojun 
     66   1.2  itojun #include <sys/param.h>
     67   1.2  itojun #include <sys/mbuf.h>
     68   1.2  itojun #include <sys/systm.h>
     69   1.2  itojun #include <sys/socket.h>
     70   1.2  itojun #include <net/route.h>
     71   1.2  itojun #include <netinet/in.h>
     72   1.2  itojun #include <netinet/in_systm.h>
     73   1.2  itojun #include <netinet/ip.h>
     74   1.2  itojun #include <netinet/ip_var.h>
     75   1.2  itojun 
     76   1.2  itojun /*
     77   1.2  itojun  * Checksum routine for Internet Protocol family headers (Portable Version).
     78   1.2  itojun  * This is only for IPv4 pseudo header checksum.
     79   1.2  itojun  * No need to clear non-pseudo-header fields in IPv4 header.
     80   1.2  itojun  * len is for actual payload size, and does not include IPv4 header and
     81   1.2  itojun  * skipped header chain (off + len should be equal to the whole packet).
     82   1.2  itojun  */
     83   1.2  itojun 
     84   1.2  itojun int
     85  1.11   perry in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len)
     86   1.2  itojun {
     87  1.15   joerg 	uint32_t sum;
     88   1.9  itojun 	union {
     89   1.9  itojun 		struct ipovly ipov;
     90  1.15   joerg 		uint16_t w[10];
     91   1.9  itojun 	} u;
     92  1.15   joerg 	uint16_t *w;
     93  1.15   joerg 
     94  1.15   joerg 	sum = 0;
     95  1.15   joerg 
     96  1.15   joerg 	if (nxt == 0)
     97  1.15   joerg 		goto skip_phdr;
     98  1.15   joerg 
     99  1.15   joerg 	/* pseudo header */
    100  1.15   joerg 	if (off < sizeof(struct ipovly))
    101  1.15   joerg 		panic("in4_cksum: offset too short");
    102  1.15   joerg 	if (m->m_len < sizeof(struct ip))
    103  1.15   joerg 		panic("in4_cksum: bad mbuf chain");
    104  1.15   joerg 	bzero(&u.ipov, sizeof(u.ipov));
    105  1.15   joerg 	u.ipov.ih_len = htons(len);
    106  1.15   joerg 	u.ipov.ih_pr = nxt;
    107  1.15   joerg 	u.ipov.ih_src = mtod(m, struct ip *)->ip_src;
    108  1.15   joerg 	u.ipov.ih_dst = mtod(m, struct ip *)->ip_dst;
    109  1.15   joerg 	w = u.w;
    110  1.15   joerg 	/* assumes sizeof(ipov) == 20 */
    111  1.15   joerg 	sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4];
    112  1.15   joerg 	sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9];
    113  1.15   joerg 
    114  1.15   joerg  skip_phdr:
    115  1.15   joerg 	return cpu_in_cksum(m, len, off, sum);
    116   1.2  itojun }
    117