1 /* $NetBSD: in6_cksum.c,v 1.15 2003/07/18 06:45:33 itojun Exp $ */ 2 /* $KAME: in6_cksum.c,v 1.9 2000/09/09 15:33:31 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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 the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1988, 1992, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: in6_cksum.c,v 1.15 2003/07/18 06:45:33 itojun Exp $"); 70 71 #include <sys/param.h> 72 #include <sys/mbuf.h> 73 #include <sys/systm.h> 74 #include <netinet/in.h> 75 #include <netinet/ip6.h> 76 77 #include <net/net_osdep.h> 78 79 /* 80 * Checksum routine for Internet Protocol family headers (Portable Version). 81 * 82 * This routine is very heavily used in the network 83 * code and should be modified for each CPU to be as fast as possible. 84 */ 85 86 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x) 87 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);} 88 89 /* 90 * m MUST contain a continuous IP6 header. 91 * off is a offset where TCP/UDP/ICMP6 header starts. 92 * len is a total length of a transport segment. 93 * (e.g. TCP header + TCP payload) 94 */ 95 96 int 97 in6_cksum(m, nxt, off, len) 98 struct mbuf *m; 99 u_int8_t nxt; 100 u_int32_t off, len; 101 { 102 u_int16_t *w; 103 int sum = 0; 104 int mlen = 0; 105 int byte_swapped = 0; 106 struct ip6_hdr *ip6; 107 union { 108 u_int16_t phs[4]; 109 struct { 110 u_int32_t ph_len; 111 u_int8_t ph_zero[3]; 112 u_int8_t ph_nxt; 113 } ph __attribute__((__packed__)); 114 } uph; 115 union { 116 u_int8_t c[2]; 117 u_int16_t s; 118 } s_util; 119 union { 120 u_int16_t s[2]; 121 u_int32_t l; 122 } l_util; 123 124 /* sanity check */ 125 if (m->m_pkthdr.len < off + len) { 126 panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)", 127 m->m_pkthdr.len, off, len); 128 } 129 130 /* Skip pseudo-header if nxt == 0. */ 131 if (nxt == 0) 132 goto skip_phdr; 133 134 bzero(&uph, sizeof(uph)); 135 136 /* 137 * First create IP6 pseudo header and calculate a summary. 138 */ 139 ip6 = mtod(m, struct ip6_hdr *); 140 w = (u_int16_t *)&ip6->ip6_src; 141 uph.ph.ph_len = htonl(len); 142 uph.ph.ph_nxt = nxt; 143 144 /* IPv6 source address */ 145 sum += w[0]; 146 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 147 sum += w[1]; 148 sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5]; 149 sum += w[6]; sum += w[7]; 150 /* IPv6 destination address */ 151 sum += w[8]; 152 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 153 sum += w[9]; 154 sum += w[10]; sum += w[11]; sum += w[12]; sum += w[13]; 155 sum += w[14]; sum += w[15]; 156 /* Payload length and upper layer identifier */ 157 sum += uph.phs[0]; sum += uph.phs[1]; 158 sum += uph.phs[2]; sum += uph.phs[3]; 159 160 skip_phdr: 161 /* 162 * Secondly calculate a summary of the first mbuf excluding offset. 163 */ 164 while (m != NULL && off > 0) { 165 if (m->m_len <= off) 166 off -= m->m_len; 167 else 168 break; 169 m = m->m_next; 170 } 171 w = (u_int16_t *)(mtod(m, u_char *) + off); 172 mlen = m->m_len - off; 173 if (len < mlen) 174 mlen = len; 175 len -= mlen; 176 /* 177 * Force to even boundary. 178 */ 179 if ((1 & (long) w) && (mlen > 0)) { 180 REDUCE; 181 sum <<= 8; 182 s_util.c[0] = *(u_char *)w; 183 w = (u_int16_t *)((char *)w + 1); 184 mlen--; 185 byte_swapped = 1; 186 } 187 /* 188 * Unroll the loop to make overhead from 189 * branches &c small. 190 */ 191 while ((mlen -= 32) >= 0) { 192 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 193 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7]; 194 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11]; 195 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15]; 196 w += 16; 197 } 198 mlen += 32; 199 while ((mlen -= 8) >= 0) { 200 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 201 w += 4; 202 } 203 mlen += 8; 204 if (mlen == 0 && byte_swapped == 0) 205 goto next; 206 REDUCE; 207 while ((mlen -= 2) >= 0) { 208 sum += *w++; 209 } 210 if (byte_swapped) { 211 REDUCE; 212 sum <<= 8; 213 byte_swapped = 0; 214 if (mlen == -1) { 215 s_util.c[1] = *(char *)w; 216 sum += s_util.s; 217 mlen = 0; 218 } else 219 mlen = -1; 220 } else if (mlen == -1) 221 s_util.c[0] = *(char *)w; 222 next: 223 m = m->m_next; 224 225 /* 226 * Lastly calculate a summary of the rest of mbufs. 227 */ 228 229 for (;m && len; m = m->m_next) { 230 if (m->m_len == 0) 231 continue; 232 w = mtod(m, u_int16_t *); 233 if (mlen == -1) { 234 /* 235 * The first byte of this mbuf is the continuation 236 * of a word spanning between this mbuf and the 237 * last mbuf. 238 * 239 * s_util.c[0] is already saved when scanning previous 240 * mbuf. 241 */ 242 s_util.c[1] = *(char *)w; 243 sum += s_util.s; 244 w = (u_int16_t *)((char *)w + 1); 245 mlen = m->m_len - 1; 246 len--; 247 } else 248 mlen = m->m_len; 249 if (len < mlen) 250 mlen = len; 251 len -= mlen; 252 /* 253 * Force to even boundary. 254 */ 255 if ((1 & (long) w) && (mlen > 0)) { 256 REDUCE; 257 sum <<= 8; 258 s_util.c[0] = *(u_char *)w; 259 w = (u_int16_t *)((char *)w + 1); 260 mlen--; 261 byte_swapped = 1; 262 } 263 /* 264 * Unroll the loop to make overhead from 265 * branches &c small. 266 */ 267 while ((mlen -= 32) >= 0) { 268 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 269 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7]; 270 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11]; 271 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15]; 272 w += 16; 273 } 274 mlen += 32; 275 while ((mlen -= 8) >= 0) { 276 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 277 w += 4; 278 } 279 mlen += 8; 280 if (mlen == 0 && byte_swapped == 0) 281 continue; 282 REDUCE; 283 while ((mlen -= 2) >= 0) { 284 sum += *w++; 285 } 286 if (byte_swapped) { 287 REDUCE; 288 sum <<= 8; 289 byte_swapped = 0; 290 if (mlen == -1) { 291 s_util.c[1] = *(char *)w; 292 sum += s_util.s; 293 mlen = 0; 294 } else 295 mlen = -1; 296 } else if (mlen == -1) 297 s_util.c[0] = *(char *)w; 298 } 299 if (len) 300 panic("in6_cksum: out of data"); 301 if (mlen == -1) { 302 /* The last mbuf has odd # of bytes. Follow the 303 standard (the odd byte may be shifted left by 8 bits 304 or not as determined by endian-ness of the machine) */ 305 s_util.c[1] = 0; 306 sum += s_util.s; 307 } 308 REDUCE; 309 return (~sum & 0xffff); 310 } 311