1 /* $NetBSD: in_cksum.c,v 1.6 2002/08/05 02:56:58 enami Exp $ */ 2 3 /* 4 * Copyright 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Simon Burge and Eduardo Horvath for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 #include <sys/param.h> 39 #include <sys/mbuf.h> 40 #include <sys/systm.h> 41 #include <netinet/in_systm.h> 42 #include <netinet/in.h> 43 #include <netinet/ip.h> 44 #include <netinet/ip_var.h> 45 46 /* 47 * Checksum routine for Internet Protocol family headers. 48 * 49 * This routine is very heavily used in the network 50 * code and should be modified for each CPU to be as fast as possible. 51 * 52 * PowerPC version. 53 */ 54 55 #define REDUCE1 sum = (sum & 0xffff) + (sum >> 16) 56 /* Two REDUCE1s is faster than REDUCE1; if (sum > 65535) sum -= 65536; */ 57 #define REDUCE { REDUCE1; REDUCE1; } 58 59 static __inline__ int 60 in_cksum_internal(struct mbuf *m, int off, int len, u_int sum) 61 { 62 uint8_t *w; 63 int mlen = 0; 64 int byte_swapped = 0; 65 int n; 66 67 union { 68 uint8_t c[2]; 69 uint16_t s; 70 } s_util; 71 72 for (;m && len; m = m->m_next) { 73 if (m->m_len == 0) 74 continue; 75 w = mtod(m, uint8_t *) + off; 76 77 /* 78 * 'off' can only be non-zero on the first pass of this 79 * loop when mlen != -1, so we don't need to worry about 80 * 'off' in the if clause below. 81 */ 82 if (mlen == -1) { 83 /* 84 * The first byte of this mbuf is the continuation 85 * of a word spanning between this mbuf and the 86 * last mbuf. 87 * 88 * s_util.c[0] is already saved when scanning previous 89 * mbuf. 90 */ 91 s_util.c[1] = *w++; 92 sum += s_util.s; 93 mlen = m->m_len - 1; 94 len--; 95 } else { 96 mlen = m->m_len - off; 97 off = 0; 98 } 99 if (len < mlen) 100 mlen = len; 101 len -= mlen; 102 103 /* 104 * Force to a word boundary. 105 */ 106 if ((3 & (long) w) && (mlen > 0)) { 107 if ((1 & (long) w)) { 108 REDUCE; 109 sum <<= 8; 110 s_util.c[0] = *w++; 111 mlen--; 112 byte_swapped = 1; 113 } 114 if ((2 & (long) w) && (mlen > 1)) { 115 /* 116 * Since the `sum' may contain full 32 bit 117 * value, we can't simply add any value. 118 */ 119 __asm __volatile( 120 "lhz 7,0(%1);" /* load current data 121 half word */ 122 "addc %0,%0,7;" /* add to sum */ 123 "addze %0,%0;" /* add carry bit */ 124 : "+r"(sum) 125 : "b"(w) 126 : "7"); /* clobber r7 */ 127 w += 2; 128 mlen -= 2; 129 } 130 } 131 132 if (mlen >= 64) { 133 n = mlen >> 6; 134 __asm __volatile( 135 "addic 0,0,0;" /* clear carry */ 136 "mtctr %1;" /* load loop count */ 137 "1:" 138 "lwz 7,4(%2);" /* load current data 139 word */ 140 "lwz 8,8(%2);" 141 "lwz 9,12(%2);" 142 "lwz 10,16(%2);" 143 "adde %0,%0,7;" /* add to sum */ 144 "adde %0,%0,8;" 145 "adde %0,%0,9;" 146 "adde %0,%0,10;" 147 "lwz 7,20(%2);" 148 "lwz 8,24(%2);" 149 "lwz 9,28(%2);" 150 "lwz 10,32(%2);" 151 "adde %0,%0,7;" 152 "adde %0,%0,8;" 153 "adde %0,%0,9;" 154 "adde %0,%0,10;" 155 "lwz 7,36(%2);" 156 "lwz 8,40(%2);" 157 "lwz 9,44(%2);" 158 "lwz 10,48(%2);" 159 "adde %0,%0,7;" 160 "adde %0,%0,8;" 161 "adde %0,%0,9;" 162 "adde %0,%0,10;" 163 "lwz 7,52(%2);" 164 "lwz 8,56(%2);" 165 "lwz 9,60(%2);" 166 "lwzu 10,64(%2);" 167 "adde %0,%0,7;" 168 "adde %0,%0,8;" 169 "adde %0,%0,9;" 170 "adde %0,%0,10;" 171 "bdnz 1b;" /* loop */ 172 "addze %0,%0;" /* add carry bit */ 173 : "+r"(sum) 174 : "r"(n), "b"(w - 4) 175 : "7", "8", "9", "10"); /* clobber r7, r8, r9, 176 r10 */ 177 w += n * 64; 178 mlen -= n * 64; 179 } 180 181 if (mlen >= 8) { 182 n = mlen >> 3; 183 __asm __volatile( 184 "addic 0,0,0;" /* clear carry */ 185 "mtctr %1;" /* load loop count */ 186 "1:" 187 "lwz 7,4(%2);" /* load current data 188 word */ 189 "lwzu 8,8(%2);" 190 "adde %0,%0,7;" /* add to sum */ 191 "adde %0,%0,8;" 192 "bdnz 1b;" /* loop */ 193 "addze %0,%0;" /* add carry bit */ 194 : "+r"(sum) 195 : "r"(n), "b"(w - 4) 196 : "7", "8"); /* clobber r7, r8 */ 197 w += n * 8; 198 mlen -= n * 8; 199 } 200 201 if (mlen == 0 && byte_swapped == 0) 202 continue; 203 REDUCE; 204 205 while ((mlen -= 2) >= 0) { 206 sum += *(uint16_t *)w; 207 w += 2; 208 } 209 210 if (byte_swapped) { 211 REDUCE; 212 sum <<= 8; 213 byte_swapped = 0; 214 if (mlen == -1) { 215 s_util.c[1] = *w; 216 sum += s_util.s; 217 mlen = 0; 218 } else 219 mlen = -1; 220 } else if (mlen == -1) 221 s_util.c[0] = *w; 222 } 223 if (len) 224 printf("cksum: out of data\n"); 225 if (mlen == -1) { 226 /* The last mbuf has odd # of bytes. Follow the 227 standard (the odd byte may be shifted left by 8 bits 228 or not as determined by endian-ness of the machine) */ 229 s_util.c[1] = 0; 230 sum += s_util.s; 231 } 232 REDUCE; 233 return (~sum & 0xffff); 234 } 235 236 int 237 in_cksum(struct mbuf *m, int len) 238 { 239 240 return (in_cksum_internal(m, 0, len, 0)); 241 } 242 243 int 244 in4_cksum(struct mbuf *m, uint8_t nxt, int off, int len) 245 { 246 uint16_t *w; 247 u_int sum = 0; 248 union { 249 struct ipovly ipov; 250 u_int16_t w[10]; 251 } u; 252 253 if (nxt != 0) { 254 /* pseudo header */ 255 memset(&u.ipov, 0, sizeof(u.ipov)); 256 u.ipov.ih_len = htons(len); 257 u.ipov.ih_pr = nxt; 258 u.ipov.ih_src = mtod(m, struct ip *)->ip_src; 259 u.ipov.ih_dst = mtod(m, struct ip *)->ip_dst; 260 w = u.w; 261 /* assumes sizeof(ipov) == 20 */ 262 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4]; 263 sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9]; 264 } 265 266 /* skip unnecessary part */ 267 while (m && off > 0) { 268 if (m->m_len > off) 269 break; 270 off -= m->m_len; 271 m = m->m_next; 272 } 273 274 return (in_cksum_internal(m, off, len, sum)); 275 } 276