in4_cksum.c revision 126258
1126258Smlaier/* $OpenBSD: in4_cksum.c,v 1.7 2003/06/02 23:28:13 millert Exp $ */ 2126258Smlaier/* $KAME: in4_cksum.c,v 1.10 2001/11/30 10:06:15 itojun Exp $ */ 3126258Smlaier/* $NetBSD: in_cksum.c,v 1.13 1996/10/13 02:03:03 christos Exp $ */ 4126258Smlaier 5126258Smlaier/* 6126258Smlaier * Copyright (C) 1999 WIDE Project. 7126258Smlaier * All rights reserved. 8126258Smlaier * 9126258Smlaier * Redistribution and use in source and binary forms, with or without 10126258Smlaier * modification, are permitted provided that the following conditions 11126258Smlaier * are met: 12126258Smlaier * 1. Redistributions of source code must retain the above copyright 13126258Smlaier * notice, this list of conditions and the following disclaimer. 14126258Smlaier * 2. Redistributions in binary form must reproduce the above copyright 15126258Smlaier * notice, this list of conditions and the following disclaimer in the 16126258Smlaier * documentation and/or other materials provided with the distribution. 17126258Smlaier * 3. Neither the name of the project nor the names of its contributors 18126258Smlaier * may be used to endorse or promote products derived from this software 19126258Smlaier * without specific prior written permission. 20126258Smlaier * 21126258Smlaier * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 22126258Smlaier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23126258Smlaier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24126258Smlaier * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 25126258Smlaier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26126258Smlaier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27126258Smlaier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28126258Smlaier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29126258Smlaier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30126258Smlaier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31126258Smlaier * SUCH DAMAGE. 32126258Smlaier */ 33126258Smlaier 34126258Smlaier/* 35126258Smlaier * Copyright (c) 1988, 1992, 1993 36126258Smlaier * The Regents of the University of California. All rights reserved. 37126258Smlaier * 38126258Smlaier * Redistribution and use in source and binary forms, with or without 39126258Smlaier * modification, are permitted provided that the following conditions 40126258Smlaier * are met: 41126258Smlaier * 1. Redistributions of source code must retain the above copyright 42126258Smlaier * notice, this list of conditions and the following disclaimer. 43126258Smlaier * 2. Redistributions in binary form must reproduce the above copyright 44126258Smlaier * notice, this list of conditions and the following disclaimer in the 45126258Smlaier * documentation and/or other materials provided with the distribution. 46126258Smlaier * 3. Neither the name of the University nor the names of its contributors 47126258Smlaier * may be used to endorse or promote products derived from this software 48126258Smlaier * without specific prior written permission. 49126258Smlaier * 50126258Smlaier * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51126258Smlaier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52126258Smlaier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53126258Smlaier * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54126258Smlaier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55126258Smlaier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56126258Smlaier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57126258Smlaier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58126258Smlaier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59126258Smlaier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60126258Smlaier * SUCH DAMAGE. 61126258Smlaier * 62126258Smlaier * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93 63126258Smlaier */ 64126258Smlaier 65126258Smlaier#include <sys/param.h> 66126258Smlaier#include <sys/mbuf.h> 67126258Smlaier#include <sys/systm.h> 68126258Smlaier#include <sys/socket.h> 69126258Smlaier#include <net/route.h> 70126258Smlaier#include <netinet/in.h> 71126258Smlaier#include <netinet/in_systm.h> 72126258Smlaier#include <netinet/ip.h> 73126258Smlaier#include <netinet/ip_var.h> 74126258Smlaier 75126258Smlaier/* 76126258Smlaier * Checksum routine for Internet Protocol family headers (Portable Version). 77126258Smlaier * This is only for IPv4 pseudo header checksum. 78126258Smlaier * No need to clear non-pseudo-header fields in IPv4 header. 79126258Smlaier * len is for actual payload size, and does not include IPv4 header and 80126258Smlaier * skipped header chain (off + len should be equal to the whole packet). 81126258Smlaier * 82126258Smlaier * This routine is very heavily used in the network 83126258Smlaier * code and should be modified for each CPU to be as fast as possible. 84126258Smlaier */ 85126258Smlaier 86126258Smlaier#define ADDCARRY(x) (x > 65535 ? x -= 65535 : x) 87126258Smlaier#define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);} 88126258Smlaier 89126258Smlaierint 90126258Smlaierin4_cksum(m, nxt, off, len) 91126258Smlaier struct mbuf *m; 92126258Smlaier u_int8_t nxt; 93126258Smlaier int off, len; 94126258Smlaier{ 95126258Smlaier u_int16_t *w; 96126258Smlaier int sum = 0; 97126258Smlaier int mlen = 0; 98126258Smlaier int byte_swapped = 0; 99126258Smlaier union { 100126258Smlaier struct ipovly ipov; 101126258Smlaier u_int16_t w[10]; 102126258Smlaier } u; 103126258Smlaier union { 104126258Smlaier u_int8_t c[2]; 105126258Smlaier u_int16_t s; 106126258Smlaier } s_util; 107126258Smlaier union { 108126258Smlaier u_int16_t s[2]; 109126258Smlaier u_int32_t l; 110126258Smlaier } l_util; 111126258Smlaier 112126258Smlaier if (nxt != 0) { 113126258Smlaier /* pseudo header */ 114126258Smlaier if (off < sizeof(struct ipovly)) 115126258Smlaier panic("in4_cksum: offset too short"); 116126258Smlaier if (m->m_len < sizeof(struct ip)) 117126258Smlaier panic("in4_cksum: bad mbuf chain"); 118126258Smlaier bzero(&u.ipov, sizeof(u.ipov)); 119126258Smlaier u.ipov.ih_len = htons(len); 120126258Smlaier u.ipov.ih_pr = nxt; 121126258Smlaier u.ipov.ih_src = mtod(m, struct ip *)->ip_src; 122126258Smlaier u.ipov.ih_dst = mtod(m, struct ip *)->ip_dst; 123126258Smlaier w = u.w; 124126258Smlaier /* assumes sizeof(ipov) == 20 */ 125126258Smlaier sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4]; 126126258Smlaier sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9]; 127126258Smlaier } 128126258Smlaier 129126258Smlaier /* skip unnecessary part */ 130126258Smlaier while (m && off > 0) { 131126258Smlaier if (m->m_len > off) 132126258Smlaier break; 133126258Smlaier off -= m->m_len; 134126258Smlaier m = m->m_next; 135126258Smlaier } 136126258Smlaier 137126258Smlaier for (;m && len; m = m->m_next) { 138126258Smlaier if (m->m_len == 0) 139126258Smlaier continue; 140126258Smlaier w = (u_int16_t *)(mtod(m, caddr_t) + off); 141126258Smlaier if (mlen == -1) { 142126258Smlaier /* 143126258Smlaier * The first byte of this mbuf is the continuation 144126258Smlaier * of a word spanning between this mbuf and the 145126258Smlaier * last mbuf. 146126258Smlaier * 147126258Smlaier * s_util.c[0] is already saved when scanning previous 148126258Smlaier * mbuf. 149126258Smlaier */ 150126258Smlaier s_util.c[1] = *(u_int8_t *)w; 151126258Smlaier sum += s_util.s; 152126258Smlaier w = (u_int16_t *)((u_int8_t *)w + 1); 153126258Smlaier mlen = m->m_len - off - 1; 154126258Smlaier len--; 155126258Smlaier } else 156126258Smlaier mlen = m->m_len - off; 157126258Smlaier off = 0; 158126258Smlaier if (len < mlen) 159126258Smlaier mlen = len; 160126258Smlaier len -= mlen; 161126258Smlaier /* 162126258Smlaier * Force to even boundary. 163126258Smlaier */ 164126258Smlaier if ((1 & (long) w) && (mlen > 0)) { 165126258Smlaier REDUCE; 166126258Smlaier sum <<= 8; 167126258Smlaier s_util.c[0] = *(u_int8_t *)w; 168126258Smlaier w = (u_int16_t *)((int8_t *)w + 1); 169126258Smlaier mlen--; 170126258Smlaier byte_swapped = 1; 171126258Smlaier } 172126258Smlaier /* 173126258Smlaier * Unroll the loop to make overhead from 174126258Smlaier * branches &c small. 175126258Smlaier */ 176126258Smlaier while ((mlen -= 32) >= 0) { 177126258Smlaier sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 178126258Smlaier sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7]; 179126258Smlaier sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11]; 180126258Smlaier sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15]; 181126258Smlaier w += 16; 182126258Smlaier } 183126258Smlaier mlen += 32; 184126258Smlaier while ((mlen -= 8) >= 0) { 185126258Smlaier sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; 186126258Smlaier w += 4; 187126258Smlaier } 188126258Smlaier mlen += 8; 189126258Smlaier if (mlen == 0 && byte_swapped == 0) 190126258Smlaier continue; 191126258Smlaier REDUCE; 192126258Smlaier while ((mlen -= 2) >= 0) { 193126258Smlaier sum += *w++; 194126258Smlaier } 195126258Smlaier if (byte_swapped) { 196126258Smlaier REDUCE; 197126258Smlaier sum <<= 8; 198126258Smlaier byte_swapped = 0; 199126258Smlaier if (mlen == -1) { 200126258Smlaier s_util.c[1] = *(u_int8_t *)w; 201126258Smlaier sum += s_util.s; 202126258Smlaier mlen = 0; 203126258Smlaier } else 204126258Smlaier mlen = -1; 205126258Smlaier } else if (mlen == -1) 206126258Smlaier s_util.c[0] = *(u_int8_t *)w; 207126258Smlaier } 208126258Smlaier if (len) 209126258Smlaier printf("cksum4: out of data\n"); 210126258Smlaier if (mlen == -1) { 211126258Smlaier /* The last mbuf has odd # of bytes. Follow the 212126258Smlaier standard (the odd byte may be shifted left by 8 bits 213126258Smlaier or not as determined by endian-ness of the machine) */ 214126258Smlaier s_util.c[1] = 0; 215126258Smlaier sum += s_util.s; 216126258Smlaier } 217126258Smlaier REDUCE; 218126258Smlaier return (~sum & 0xffff); 219126258Smlaier} 220