Home | History | Annotate | Line # | Download | only in netinet
in4_cksum.c revision 1.19.12.1
      1  1.19.12.1    martin /*	$NetBSD: in4_cksum.c,v 1.19.12.1 2014/12/01 13:08:14 martin Exp $	*/
      2        1.2    itojun 
      3       1.16     joerg /*-
      4       1.16     joerg  * Copyright (c) 2008 Joerg Sonnenberger <joerg (at) NetBSD.org>.
      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  *
     11        1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     12        1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     13        1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.16     joerg  *    notice, this list of conditions and the following disclaimer in
     15       1.16     joerg  *    the documentation and/or other materials provided with the
     16       1.16     joerg  *    distribution.
     17        1.2    itojun  *
     18       1.16     joerg  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19       1.16     joerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20       1.16     joerg  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     21       1.16     joerg  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
     22       1.16     joerg  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     23       1.16     joerg  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24       1.16     joerg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25       1.16     joerg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26       1.16     joerg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27       1.16     joerg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     28       1.16     joerg  * 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.7     lukem 
     32        1.7     lukem #include <sys/cdefs.h>
     33  1.19.12.1    martin __KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.19.12.1 2014/12/01 13:08:14 martin Exp $");
     34        1.2    itojun 
     35        1.2    itojun #include <sys/param.h>
     36        1.2    itojun #include <sys/mbuf.h>
     37        1.2    itojun #include <netinet/in.h>
     38        1.2    itojun #include <netinet/in_systm.h>
     39        1.2    itojun #include <netinet/ip.h>
     40        1.2    itojun 
     41        1.2    itojun /*
     42       1.16     joerg  * Checksum of the IPv4 pseudo header.
     43       1.16     joerg  *
     44       1.16     joerg  * off is supposed to be the skipped IPv4 header, len is the payload size.
     45        1.2    itojun  */
     46       1.19  christos #ifdef DIAGNOSTIC
     47       1.19  christos #define PANIC(a,...)	panic(a, __VA_ARGS__)
     48       1.19  christos #else
     49       1.19  christos #define PANIC(a,...)	do { \
     50       1.19  christos 	printf(a, __VA_ARGS__); \
     51       1.19  christos 	return -1; \
     52       1.19  christos } while (/*CONSTCOND*/0)
     53       1.19  christos #endif
     54        1.2    itojun 
     55        1.2    itojun int
     56       1.11     perry in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len)
     57        1.2    itojun {
     58       1.15     joerg 	uint32_t sum;
     59       1.15     joerg 	uint16_t *w;
     60       1.15     joerg 
     61       1.19  christos 	if (__predict_false(m->m_len < sizeof(struct ip)))
     62       1.19  christos 		PANIC("%s: mbuf %d too short for IP header %zu", __func__,
     63       1.19  christos 		    m->m_len, sizeof(struct ip));
     64       1.19  christos 
     65       1.18      yamt 	if (nxt == 0)
     66       1.18      yamt 		return cpu_in_cksum(m, len, off, 0);
     67       1.18      yamt 
     68  1.19.12.1    martin 	if (__predict_false(off < sizeof(struct ip)))
     69  1.19.12.1    martin 		PANIC("%s: offset %d too short for IP header %zu", __func__,
     70  1.19.12.1    martin 		    off, sizeof(struct ip));
     71  1.19.12.1    martin 
     72       1.16     joerg 	/*
     73       1.16     joerg 	 * Compute the equivalent of:
     74       1.16     joerg 	 * struct ipovly ip;
     75       1.16     joerg 	 *
     76       1.16     joerg 	 * bzero(sizeof(*ip));
     77       1.16     joerg 	 * ip.ih_pr = nxt;
     78       1.16     joerg 	 * ip.ip_len = htons(len);
     79       1.16     joerg 	 * ip.ih_src = mtod(m, struct ip *)->ip_src;
     80       1.16     joerg 	 * ip.ih_dst = mtod(m, struct ip *)->ip_dst;
     81       1.16     joerg 	 * sum = one_add(&ip);
     82       1.16     joerg 	 */
     83       1.16     joerg 
     84       1.16     joerg #if BYTE_ORDER == LITTLE_ENDIAN
     85       1.16     joerg 	sum = ((len & 0xffff) + nxt) << 8;
     86       1.16     joerg #else
     87       1.16     joerg 	sum = (len & 0xffff) + nxt;
     88       1.16     joerg #endif
     89       1.16     joerg 	w = (uint16_t *)(mtod(m, char  *) + offsetof(struct ip, ip_src));
     90       1.16     joerg 	if (__predict_true((uintptr_t)w % 2 == 0)) {
     91       1.16     joerg 		sum += w[0];
     92       1.16     joerg 		sum += w[1];
     93       1.16     joerg 		sum += w[2];
     94       1.16     joerg 		sum += w[3];
     95       1.16     joerg 	} else {
     96       1.16     joerg 		uint32_t partial;
     97       1.16     joerg 		w = (void *)((uintptr_t)w - 1);
     98       1.16     joerg #if BYTE_ORDER == LITTLE_ENDIAN
     99       1.16     joerg 		partial = w[0] & 0xff00;
    100       1.16     joerg #else
    101       1.16     joerg 		partial = w[0] & 0x00ff;
    102       1.16     joerg #endif
    103       1.16     joerg 		partial += w[1];
    104       1.16     joerg 		partial += w[2];
    105       1.16     joerg 		partial += w[3];
    106       1.16     joerg #if BYTE_ORDER == LITTLE_ENDIAN
    107       1.16     joerg 		partial += w[4] & 0x00ff;
    108       1.16     joerg #else
    109       1.16     joerg 		partial += w[4] & 0xff00;
    110       1.16     joerg #endif
    111       1.16     joerg 		sum += partial << 8;
    112       1.16     joerg 	}
    113       1.15     joerg 
    114       1.15     joerg 	return cpu_in_cksum(m, len, off, sum);
    115        1.2    itojun }
    116