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