in4_cksum.c revision 1.11.6.3 1 1.11.6.3 yamt /* $NetBSD: in4_cksum.c,v 1.11.6.3 2008/02/11 15:00:04 yamt Exp $ */
2 1.2 itojun
3 1.11.6.3 yamt /*-
4 1.11.6.3 yamt * 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.11.6.3 yamt * notice, this list of conditions and the following disclaimer in
15 1.11.6.3 yamt * the documentation and/or other materials provided with the
16 1.11.6.3 yamt * distribution.
17 1.2 itojun *
18 1.11.6.3 yamt * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 1.11.6.3 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 1.11.6.3 yamt * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 1.11.6.3 yamt * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 1.11.6.3 yamt * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.11.6.3 yamt * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 1.11.6.3 yamt * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.11.6.3 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 1.11.6.3 yamt * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 1.11.6.3 yamt * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 1.11.6.3 yamt * 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.11.6.3 yamt __KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.11.6.3 2008/02/11 15:00:04 yamt 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.11.6.3 yamt * Checksum of the IPv4 pseudo header.
43 1.11.6.3 yamt *
44 1.11.6.3 yamt * 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.11.6.2 yamt uint32_t sum;
51 1.11.6.2 yamt uint16_t *w;
52 1.11.6.2 yamt
53 1.11.6.3 yamt if (off < sizeof(struct ip))
54 1.11.6.3 yamt panic("in4_cksum: offset too short for IP header");
55 1.11.6.3 yamt if (m->m_len < sizeof(struct ip))
56 1.11.6.3 yamt panic("in4_cksum: mbuf too short for IP header");
57 1.11.6.2 yamt
58 1.11.6.2 yamt if (nxt == 0)
59 1.11.6.3 yamt return cpu_in_cksum(m, len, off, 0);
60 1.11.6.2 yamt
61 1.11.6.3 yamt /*
62 1.11.6.3 yamt * Compute the equivalent of:
63 1.11.6.3 yamt * struct ipovly ip;
64 1.11.6.3 yamt *
65 1.11.6.3 yamt * bzero(sizeof(*ip));
66 1.11.6.3 yamt * ip.ih_pr = nxt;
67 1.11.6.3 yamt * ip.ip_len = htons(len);
68 1.11.6.3 yamt * ip.ih_src = mtod(m, struct ip *)->ip_src;
69 1.11.6.3 yamt * ip.ih_dst = mtod(m, struct ip *)->ip_dst;
70 1.11.6.3 yamt * sum = one_add(&ip);
71 1.11.6.3 yamt */
72 1.11.6.3 yamt
73 1.11.6.3 yamt #if BYTE_ORDER == LITTLE_ENDIAN
74 1.11.6.3 yamt sum = ((len & 0xffff) + nxt) << 8;
75 1.11.6.3 yamt #else
76 1.11.6.3 yamt sum = (len & 0xffff) + nxt;
77 1.11.6.3 yamt #endif
78 1.11.6.3 yamt w = (uint16_t *)(mtod(m, char *) + offsetof(struct ip, ip_src));
79 1.11.6.3 yamt if (__predict_true((uintptr_t)w % 2 == 0)) {
80 1.11.6.3 yamt sum += w[0];
81 1.11.6.3 yamt sum += w[1];
82 1.11.6.3 yamt sum += w[2];
83 1.11.6.3 yamt sum += w[3];
84 1.11.6.3 yamt } else {
85 1.11.6.3 yamt uint32_t partial;
86 1.11.6.3 yamt w = (void *)((uintptr_t)w - 1);
87 1.11.6.3 yamt #if BYTE_ORDER == LITTLE_ENDIAN
88 1.11.6.3 yamt partial = w[0] & 0xff00;
89 1.11.6.3 yamt #else
90 1.11.6.3 yamt partial = w[0] & 0x00ff;
91 1.11.6.3 yamt #endif
92 1.11.6.3 yamt partial += w[1];
93 1.11.6.3 yamt partial += w[2];
94 1.11.6.3 yamt partial += w[3];
95 1.11.6.3 yamt #if BYTE_ORDER == LITTLE_ENDIAN
96 1.11.6.3 yamt partial += w[4] & 0x00ff;
97 1.11.6.3 yamt #else
98 1.11.6.3 yamt partial += w[4] & 0xff00;
99 1.11.6.3 yamt #endif
100 1.11.6.3 yamt sum += partial << 8;
101 1.11.6.3 yamt }
102 1.11.6.2 yamt
103 1.11.6.2 yamt return cpu_in_cksum(m, len, off, sum);
104 1.2 itojun }
105