in4_cksum.c revision 1.17 1 1.17 joerg /* $NetBSD: in4_cksum.c,v 1.17 2008/02/12 13:05:55 joerg 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.17 joerg __KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.17 2008/02/12 13:05:55 joerg 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.2 itojun
47 1.2 itojun int
48 1.11 perry in4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len)
49 1.2 itojun {
50 1.15 joerg uint32_t sum;
51 1.15 joerg uint16_t *w;
52 1.15 joerg
53 1.17 joerg if (__predict_false(off < sizeof(struct ip)))
54 1.16 joerg panic("in4_cksum: offset too short for IP header");
55 1.17 joerg if (__predict_false(m->m_len < sizeof(struct ip)))
56 1.16 joerg panic("in4_cksum: mbuf too short for IP header");
57 1.15 joerg
58 1.15 joerg if (nxt == 0)
59 1.16 joerg return cpu_in_cksum(m, len, off, 0);
60 1.15 joerg
61 1.16 joerg /*
62 1.16 joerg * Compute the equivalent of:
63 1.16 joerg * struct ipovly ip;
64 1.16 joerg *
65 1.16 joerg * bzero(sizeof(*ip));
66 1.16 joerg * ip.ih_pr = nxt;
67 1.16 joerg * ip.ip_len = htons(len);
68 1.16 joerg * ip.ih_src = mtod(m, struct ip *)->ip_src;
69 1.16 joerg * ip.ih_dst = mtod(m, struct ip *)->ip_dst;
70 1.16 joerg * sum = one_add(&ip);
71 1.16 joerg */
72 1.16 joerg
73 1.16 joerg #if BYTE_ORDER == LITTLE_ENDIAN
74 1.16 joerg sum = ((len & 0xffff) + nxt) << 8;
75 1.16 joerg #else
76 1.16 joerg sum = (len & 0xffff) + nxt;
77 1.16 joerg #endif
78 1.16 joerg w = (uint16_t *)(mtod(m, char *) + offsetof(struct ip, ip_src));
79 1.16 joerg if (__predict_true((uintptr_t)w % 2 == 0)) {
80 1.16 joerg sum += w[0];
81 1.16 joerg sum += w[1];
82 1.16 joerg sum += w[2];
83 1.16 joerg sum += w[3];
84 1.16 joerg } else {
85 1.16 joerg uint32_t partial;
86 1.16 joerg w = (void *)((uintptr_t)w - 1);
87 1.16 joerg #if BYTE_ORDER == LITTLE_ENDIAN
88 1.16 joerg partial = w[0] & 0xff00;
89 1.16 joerg #else
90 1.16 joerg partial = w[0] & 0x00ff;
91 1.16 joerg #endif
92 1.16 joerg partial += w[1];
93 1.16 joerg partial += w[2];
94 1.16 joerg partial += w[3];
95 1.16 joerg #if BYTE_ORDER == LITTLE_ENDIAN
96 1.16 joerg partial += w[4] & 0x00ff;
97 1.16 joerg #else
98 1.16 joerg partial += w[4] & 0xff00;
99 1.16 joerg #endif
100 1.16 joerg sum += partial << 8;
101 1.16 joerg }
102 1.15 joerg
103 1.15 joerg return cpu_in_cksum(m, len, off, sum);
104 1.2 itojun }
105