xref: /netbsd/sys/netinet/in4_cksum.c (revision 6550d01e)
1 /*	$NetBSD: in4_cksum.c,v 1.17 2008/02/12 13:05:55 joerg Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.17 2008/02/12 13:05:55 joerg Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/mbuf.h>
37 #include <netinet/in.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/ip.h>
40 
41 /*
42  * Checksum of the IPv4 pseudo header.
43  *
44  * off is supposed to be the skipped IPv4 header, len is the payload size.
45  */
46 
47 int
48 in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len)
49 {
50 	uint32_t sum;
51 	uint16_t *w;
52 
53 	if (__predict_false(off < sizeof(struct ip)))
54 		panic("in4_cksum: offset too short for IP header");
55 	if (__predict_false(m->m_len < sizeof(struct ip)))
56 		panic("in4_cksum: mbuf too short for IP header");
57 
58 	if (nxt == 0)
59 		return cpu_in_cksum(m, len, off, 0);
60 
61 	/*
62 	 * Compute the equivalent of:
63 	 * struct ipovly ip;
64 	 *
65 	 * bzero(sizeof(*ip));
66 	 * ip.ih_pr = nxt;
67 	 * ip.ip_len = htons(len);
68 	 * ip.ih_src = mtod(m, struct ip *)->ip_src;
69 	 * ip.ih_dst = mtod(m, struct ip *)->ip_dst;
70 	 * sum = one_add(&ip);
71 	 */
72 
73 #if BYTE_ORDER == LITTLE_ENDIAN
74 	sum = ((len & 0xffff) + nxt) << 8;
75 #else
76 	sum = (len & 0xffff) + nxt;
77 #endif
78 	w = (uint16_t *)(mtod(m, char  *) + offsetof(struct ip, ip_src));
79 	if (__predict_true((uintptr_t)w % 2 == 0)) {
80 		sum += w[0];
81 		sum += w[1];
82 		sum += w[2];
83 		sum += w[3];
84 	} else {
85 		uint32_t partial;
86 		w = (void *)((uintptr_t)w - 1);
87 #if BYTE_ORDER == LITTLE_ENDIAN
88 		partial = w[0] & 0xff00;
89 #else
90 		partial = w[0] & 0x00ff;
91 #endif
92 		partial += w[1];
93 		partial += w[2];
94 		partial += w[3];
95 #if BYTE_ORDER == LITTLE_ENDIAN
96 		partial += w[4] & 0x00ff;
97 #else
98 		partial += w[4] & 0xff00;
99 #endif
100 		sum += partial << 8;
101 	}
102 
103 	return cpu_in_cksum(m, len, off, sum);
104 }
105