1 /* $NetBSD: in4_cksum.c,v 1.9 2002/07/29 09:14:37 itojun Exp $ */ 2 3 /* 4 * Copyright (C) 1999 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 32 /* 33 * Copyright (c) 1988, 1992, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed by the University of 47 * California, Berkeley and its contributors. 48 * 4. Neither the name of the University nor the names of its contributors 49 * may be used to endorse or promote products derived from this software 50 * without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 * 64 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93 65 */ 66 67 #include <sys/cdefs.h> 68 __KERNEL_RCSID(0, "$NetBSD: in4_cksum.c,v 1.9 2002/07/29 09:14:37 itojun Exp $"); 69 70 #include <sys/param.h> 71 #include <sys/mbuf.h> 72 #include <sys/systm.h> 73 #include <sys/socket.h> 74 #include <net/route.h> 75 #include <netinet/in.h> 76 #include <netinet/in_systm.h> 77 #include <netinet/ip.h> 78 #include <netinet/ip_var.h> 79 80 /* 81 * Checksum routine for Internet Protocol family headers (Portable Version). 82 * This is only for IPv4 pseudo header checksum. 83 * No need to clear non-pseudo-header fields in IPv4 header. 84 * len is for actual payload size, and does not include IPv4 header and 85 * skipped header chain (off + len should be equal to the whole packet). 86 * 87 * This routine is very heavily used in the network 88 * code and should be modified for each CPU to be as fast as possible. 89 */ 90 91 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x) 92 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);} 93 94 int 95 in4_cksum(m, nxt, off, len) 96 struct mbuf *m; 97 u_int8_t nxt; 98 int off, len; 99 { 100 u_int16_t *w; 101 int sum = 0; 102 int mlen = 0; 103 int byte_swapped = 0; 104 union { 105 struct ipovly ipov; 106 u_int16_t w[10]; 107 } u; 108 union { 109 u_int8_t c[2]; 110 u_int16_t s; 111 } s_util; 112 union { 113 u_int16_t s[2]; 114 u_int32_t l; 115 } l_util; 116 117 if (nxt != 0) { 118 /* pseudo header */ 119 if (off < sizeof(struct ipovly)) 120 panic("in4_cksum: offset too short"); 121 if (m->m_len < sizeof(struct ip)) 122 panic("in4_cksum: bad mbuf chain"); 123 bzero(&u.ipov, sizeof(u.ipov)); 124 u.ipov.ih_len = htons(len); 125 u.ipov.ih_pr = nxt; 126 u.ipov.ih_src = mtod(m, struct ip *)->ip_src; 127 u.ipov.ih_dst = mtod(m, struct ip *)->ip_dst; 128 w = u.w; 129 /* assumes sizeof(ipov) == 20 */ 130 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4]; 131 sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9]; 132 } 133 134 /* skip unnecessary part */ 135 while (m && off > 0) { 136 if (m->m_len > off) 137 break; 138 off -= m->m_len; 139 m = m->m_next; 140 } 141 142 for (;m && len; m = m->m_next) { 143 if (m->m_len == 0) 144 continue; 145 w = (u_int16_t *)(mtod(m, caddr_t) + off); 146 if (mlen == -1) { 147 /* 148 * The first byte of this mbuf is the continuation 149 * of a word spanning between this mbuf and the 150 * last mbuf. 151 * 152 * s_util.c[0] is already saved when scanning previous 153 * mbuf. 154 */ 155 s_util.c[1] = *(u_int8_t *)w; 156 sum += s_util.s; 157 w = (u_int16_t *)((u_int8_t *)w + 1); 158 mlen = m->m_len - off - 1; 159 len--; 160 } else 161 mlen = m->m_len - off; 162 off = 0; 163 if (len < mlen) 164 mlen = len; 165 len -= mlen; 166 /* 167 * Force to even boundary. 168 */ 169 if ((1 & (long) w) && (mlen > 0)) { 170 REDUCE; 171 sum <<= 8; 172 s_util.c[0] = *(u_int8_t *)w; 173 w = (u_int16_t *)((int8_t *)w + 1); 174 mlen--; 175 byte_swapped = 1; 176 } 177 /* 178 * Unroll the loop to make overhead from 179 * branches &c small. 180 */ 181 while ((mlen -= 32) >= 0) { 182 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 183 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7]; 184 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11]; 185 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15]; 186 w += 16; 187 } 188 mlen += 32; 189 while ((mlen -= 8) >= 0) { 190 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 191 w += 4; 192 } 193 mlen += 8; 194 if (mlen == 0 && byte_swapped == 0) 195 continue; 196 REDUCE; 197 while ((mlen -= 2) >= 0) { 198 sum += *w++; 199 } 200 if (byte_swapped) { 201 REDUCE; 202 sum <<= 8; 203 byte_swapped = 0; 204 if (mlen == -1) { 205 s_util.c[1] = *(u_int8_t *)w; 206 sum += s_util.s; 207 mlen = 0; 208 } else 209 mlen = -1; 210 } else if (mlen == -1) 211 s_util.c[0] = *(u_int8_t *)w; 212 } 213 if (len) 214 printf("cksum4: out of data\n"); 215 if (mlen == -1) { 216 /* The last mbuf has odd # of bytes. Follow the 217 standard (the odd byte may be shifted left by 8 bits 218 or not as determined by endian-ness of the machine) */ 219 s_util.c[1] = 0; 220 sum += s_util.s; 221 } 222 REDUCE; 223 return (~sum & 0xffff); 224 } 225