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