1 /* $OpenBSD: in6_cksum.c,v 1.9 2001/06/09 06:43:37 angelos Exp $ */ 2 /* $KAME: in6_cksum.c,v 1.10 2000/12/03 00:53:59 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/param.h> 69 #include <sys/mbuf.h> 70 #include <sys/systm.h> 71 #include <netinet/in.h> 72 #include <netinet/ip6.h> 73 74 /* 75 * Checksum routine for Internet Protocol family headers (Portable Version). 76 * 77 * This routine is very heavily used in the network 78 * code and should be modified for each CPU to be as fast as possible. 79 */ 80 81 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x) 82 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);} 83 84 /* 85 * m MUST contain a continuous IP6 header. 86 * off is a offset where TCP/UDP/ICMP6 header starts. 87 * len is a total length of a transport segment. 88 * (e.g. TCP header + TCP payload) 89 */ 90 91 int 92 in6_cksum(m, nxt, off, len) 93 struct mbuf *m; 94 u_int8_t nxt; 95 u_int32_t off, len; 96 { 97 u_int16_t *w; 98 int sum = 0; 99 int mlen = 0; 100 int byte_swapped = 0; 101 #if 0 102 int srcifid = 0, dstifid = 0; 103 #endif 104 struct ip6_hdr *ip6; 105 union { 106 u_int16_t phs[4]; 107 struct { 108 u_int32_t ph_len; 109 u_int8_t ph_zero[3]; 110 u_int8_t ph_nxt; 111 } ph __attribute__((__packed__)); 112 } uph; 113 union { 114 u_int8_t c[2]; 115 u_int16_t s; 116 } s_util; 117 union { 118 u_int16_t s[2]; 119 u_int32_t l; 120 } l_util; 121 122 /* sanity check */ 123 if (m->m_pkthdr.len < off + len) { 124 panic("in6_cksum: mbuf len (%d) < off+len (%d+%d)\n", 125 m->m_pkthdr.len, off, len); 126 } 127 128 bzero(&uph, sizeof(uph)); 129 130 /* 131 * First create IP6 pseudo header and calculate a summary. 132 */ 133 ip6 = mtod(m, struct ip6_hdr *); 134 #if 0 135 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 136 srcifid = ip6->ip6_src.s6_addr16[1]; 137 ip6->ip6_src.s6_addr16[1] = 0; 138 } 139 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 140 dstifid = ip6->ip6_dst.s6_addr16[1]; 141 ip6->ip6_dst.s6_addr16[1] = 0; 142 } 143 #endif 144 w = (u_int16_t *)&ip6->ip6_src; 145 uph.ph.ph_len = htonl(len); 146 uph.ph.ph_nxt = nxt; 147 148 /* IPv6 source address */ 149 sum += w[0]; 150 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 151 sum += w[1]; 152 sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5]; 153 sum += w[6]; sum += w[7]; 154 /* IPv6 destination address */ 155 sum += w[8]; 156 if (!IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 157 sum += w[9]; 158 sum += w[10]; sum += w[11]; sum += w[12]; sum += w[13]; 159 sum += w[14]; sum += w[15]; 160 /* Payload length and upper layer identifier */ 161 sum += uph.phs[0]; sum += uph.phs[1]; 162 sum += uph.phs[2]; sum += uph.phs[3]; 163 164 #if 0 165 if (srcifid) 166 ip6->ip6_src.s6_addr16[1] = srcifid; 167 if (dstifid) 168 ip6->ip6_dst.s6_addr16[1] = dstifid; 169 #endif 170 /* 171 * Secondly calculate a summary of the first mbuf excluding offset. 172 */ 173 while (m != NULL && off > 0) { 174 if (m->m_len <= off) 175 off -= m->m_len; 176 else 177 break; 178 m = m->m_next; 179 } 180 w = (u_int16_t *)(mtod(m, u_char *) + off); 181 mlen = m->m_len - off; 182 if (len < mlen) 183 mlen = len; 184 len -= mlen; 185 /* 186 * Force to even boundary. 187 */ 188 if ((1 & (long) w) && (mlen > 0)) { 189 REDUCE; 190 sum <<= 8; 191 s_util.c[0] = *(u_char *)w; 192 w = (u_int16_t *)((char *)w + 1); 193 mlen--; 194 byte_swapped = 1; 195 } 196 /* 197 * Unroll the loop to make overhead from 198 * branches &c small. 199 */ 200 while ((mlen -= 32) >= 0) { 201 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 202 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7]; 203 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11]; 204 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15]; 205 w += 16; 206 } 207 mlen += 32; 208 while ((mlen -= 8) >= 0) { 209 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 210 w += 4; 211 } 212 mlen += 8; 213 if (mlen == 0 && byte_swapped == 0) 214 goto next; 215 REDUCE; 216 while ((mlen -= 2) >= 0) { 217 sum += *w++; 218 } 219 if (byte_swapped) { 220 REDUCE; 221 sum <<= 8; 222 byte_swapped = 0; 223 if (mlen == -1) { 224 s_util.c[1] = *(char *)w; 225 sum += s_util.s; 226 mlen = 0; 227 } else 228 mlen = -1; 229 } else if (mlen == -1) 230 s_util.c[0] = *(char *)w; 231 next: 232 m = m->m_next; 233 234 /* 235 * Lastly calculate a summary of the rest of mbufs. 236 */ 237 238 for (;m && len; m = m->m_next) { 239 if (m->m_len == 0) 240 continue; 241 w = mtod(m, u_int16_t *); 242 if (mlen == -1) { 243 /* 244 * The first byte of this mbuf is the continuation 245 * of a word spanning between this mbuf and the 246 * last mbuf. 247 * 248 * s_util.c[0] is already saved when scanning previous 249 * mbuf. 250 */ 251 s_util.c[1] = *(char *)w; 252 sum += s_util.s; 253 w = (u_int16_t *)((char *)w + 1); 254 mlen = m->m_len - 1; 255 len--; 256 } else 257 mlen = m->m_len; 258 if (len < mlen) 259 mlen = len; 260 len -= mlen; 261 /* 262 * Force to even boundary. 263 */ 264 if ((1 & (long) w) && (mlen > 0)) { 265 REDUCE; 266 sum <<= 8; 267 s_util.c[0] = *(u_char *)w; 268 w = (u_int16_t *)((char *)w + 1); 269 mlen--; 270 byte_swapped = 1; 271 } 272 /* 273 * Unroll the loop to make overhead from 274 * branches &c small. 275 */ 276 while ((mlen -= 32) >= 0) { 277 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 278 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7]; 279 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11]; 280 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15]; 281 w += 16; 282 } 283 mlen += 32; 284 while ((mlen -= 8) >= 0) { 285 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 286 w += 4; 287 } 288 mlen += 8; 289 if (mlen == 0 && byte_swapped == 0) 290 continue; 291 REDUCE; 292 while ((mlen -= 2) >= 0) { 293 sum += *w++; 294 } 295 if (byte_swapped) { 296 REDUCE; 297 sum <<= 8; 298 byte_swapped = 0; 299 if (mlen == -1) { 300 s_util.c[1] = *(char *)w; 301 sum += s_util.s; 302 mlen = 0; 303 } else 304 mlen = -1; 305 } else if (mlen == -1) 306 s_util.c[0] = *(char *)w; 307 } 308 if (len) 309 panic("in6_cksum: out of data\n"); 310 if (mlen == -1) { 311 /* The last mbuf has odd # of bytes. Follow the 312 standard (the odd byte may be shifted left by 8 bits 313 or not as determined by endian-ness of the machine) */ 314 s_util.c[1] = 0; 315 sum += s_util.s; 316 } 317 REDUCE; 318 return (~sum & 0xffff); 319 } 320