in6_cksum.c revision 1.23 1 1.23 joerg /* $NetBSD: in6_cksum.c,v 1.23 2008/02/12 13:02:24 joerg Exp $ */
2 1.3 thorpej
3 1.23 joerg /*-
4 1.23 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.8 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.23 joerg * notice, this list of conditions and the following disclaimer in
15 1.23 joerg * the documentation and/or other materials provided with the
16 1.23 joerg * distribution.
17 1.23 joerg *
18 1.23 joerg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 1.23 joerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 1.23 joerg * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 1.23 joerg * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 1.23 joerg * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.23 joerg * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 1.23 joerg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.23 joerg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 1.23 joerg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 1.23 joerg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 1.23 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.12 lukem
32 1.12 lukem #include <sys/cdefs.h>
33 1.23 joerg __KERNEL_RCSID(0, "$NetBSD: in6_cksum.c,v 1.23 2008/02/12 13:02:24 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.7 itojun #include <netinet/ip6.h>
39 1.6 itojun
40 1.2 itojun /*
41 1.23 joerg * Checksum of the IPv6 pseudo header.
42 1.2 itojun *
43 1.23 joerg * off is supposed to be the skipped IPv6 header, len is the payload size.
44 1.2 itojun */
45 1.2 itojun
46 1.2 itojun int
47 1.23 joerg in6_cksum(struct mbuf *m, u_int8_t nxt, uint32_t off, uint32_t len)
48 1.2 itojun {
49 1.9 itojun union {
50 1.23 joerg uint16_t words[16];
51 1.9 itojun struct {
52 1.23 joerg struct in6_addr ip6_src;
53 1.23 joerg struct in6_addr ip6_dst;
54 1.23 joerg } addrs;
55 1.23 joerg } u;
56 1.23 joerg struct in6_addr *in6_src, *in6_dst;
57 1.23 joerg uint32_t sum;
58 1.23 joerg uint16_t *w;
59 1.23 joerg
60 1.23 joerg if (off < sizeof(struct ip6_hdr))
61 1.23 joerg panic("in6_cksum: offset too short for IPv6 header");
62 1.23 joerg if (m->m_len < sizeof(struct ip6_hdr))
63 1.23 joerg panic("in6_cksum: mbuf too short for IPv6 header");
64 1.9 itojun
65 1.11 thorpej if (nxt == 0)
66 1.23 joerg return cpu_in_cksum(m, len, off, 0);
67 1.2 itojun
68 1.2 itojun /*
69 1.23 joerg * Compute the equivalent of:
70 1.23 joerg * struct ip6_hdr_pseudo ip6;
71 1.23 joerg *
72 1.23 joerg * bzero(sizeof(*ip6));
73 1.23 joerg * ip6.ip6ph_nxt = nxt;
74 1.23 joerg * ip6.ip6ph_len = htonl(len);
75 1.23 joerg * ipv6.ip6ph_src = mtod(m, struct ip6_hdr *)->ip6_src;
76 1.23 joerg * in6_clearscope(&ip6->ip6ph_src);
77 1.23 joerg * ipv6.ip6ph_dst = mtod(m, struct ip6_hdr *)->ip6_dst;
78 1.23 joerg * in6_clearscope(&ip6->ip6ph_dst);
79 1.23 joerg * sum = one_add(&ip6);
80 1.2 itojun */
81 1.2 itojun
82 1.23 joerg #if BYTE_ORDER == LITTLE_ENDIAN
83 1.23 joerg sum = ((len & 0xffff) + ((len >> 16) & 0xffff) + nxt) << 8;
84 1.23 joerg #else
85 1.23 joerg sum = (len & 0xffff) + ((len >> 16) & 0xffff) + nxt;
86 1.23 joerg #endif
87 1.23 joerg w = (uint16_t *)(mtod(m, char *) + offsetof(struct ip6_hdr, ip6_src));
88 1.23 joerg if (__predict_true((uintptr_t)w % 2 == 0)) {
89 1.23 joerg in6_src = &mtod(m, struct ip6_hdr *)->ip6_src;
90 1.23 joerg in6_dst = &mtod(m, struct ip6_hdr *)->ip6_dst;
91 1.23 joerg } else {
92 1.23 joerg memcpy(&u, &mtod(m, struct ip6_hdr *)->ip6_src, 32);
93 1.23 joerg w = u.words;
94 1.23 joerg in6_src = &u.addrs.ip6_src;
95 1.23 joerg in6_dst = &u.addrs.ip6_dst;
96 1.23 joerg }
97 1.23 joerg
98 1.23 joerg sum += w[0];
99 1.23 joerg if (!IN6_IS_SCOPE_LINKLOCAL(in6_src) &&
100 1.23 joerg !IN6_IS_ADDR_MC_INTFACELOCAL(in6_src))
101 1.23 joerg sum += w[1];
102 1.23 joerg sum += w[2];
103 1.23 joerg sum += w[3];
104 1.23 joerg sum += w[4];
105 1.23 joerg sum += w[5];
106 1.23 joerg sum += w[6];
107 1.23 joerg sum += w[7];
108 1.23 joerg w += 8;
109 1.23 joerg sum += w[0];
110 1.23 joerg if (!IN6_IS_SCOPE_LINKLOCAL(in6_dst) &&
111 1.23 joerg !IN6_IS_ADDR_MC_INTFACELOCAL(in6_dst))
112 1.23 joerg sum += w[1];
113 1.23 joerg sum += w[2];
114 1.23 joerg sum += w[3];
115 1.23 joerg sum += w[4];
116 1.23 joerg sum += w[5];
117 1.23 joerg sum += w[6];
118 1.23 joerg sum += w[7];
119 1.2 itojun
120 1.22 joerg return cpu_in_cksum(m, len, off, sum);
121 1.2 itojun }
122