in4_cksum.c revision 133181
1233237Sjkim/* $FreeBSD: head/sys/contrib/pf/netinet/in4_cksum.c 133181 2004-08-05 20:41:38Z mlaier $ */ 2233237Sjkim/* $OpenBSD: in4_cksum.c,v 1.7 2003/06/02 23:28:13 millert Exp $ */ 3233237Sjkim/* $KAME: in4_cksum.c,v 1.10 2001/11/30 10:06:15 itojun Exp $ */ 4233237Sjkim/* $NetBSD: in_cksum.c,v 1.13 1996/10/13 02:03:03 christos Exp $ */ 5233237Sjkim 6233237Sjkim/* 7316303Sjkim * Copyright (C) 1999 WIDE Project. 8316303Sjkim * All rights reserved. 9316303Sjkim * 10316303Sjkim * Redistribution and use in source and binary forms, with or without 11316303Sjkim * modification, are permitted provided that the following conditions 12233237Sjkim * are met: 13233237Sjkim * 1. Redistributions of source code must retain the above copyright 14316303Sjkim * notice, this list of conditions and the following disclaimer. 15316303Sjkim * 2. Redistributions in binary form must reproduce the above copyright 16316303Sjkim * notice, this list of conditions and the following disclaimer in the 17316303Sjkim * documentation and/or other materials provided with the distribution. 18316303Sjkim * 3. Neither the name of the project nor the names of its contributors 19316303Sjkim * may be used to endorse or promote products derived from this software 20316303Sjkim * without specific prior written permission. 21316303Sjkim * 22316303Sjkim * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 23316303Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24316303Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25316303Sjkim * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 26316303Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27316303Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28316303Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29316303Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30316303Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31316303Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32316303Sjkim * SUCH DAMAGE. 33316303Sjkim */ 34316303Sjkim 35316303Sjkim/* 36316303Sjkim * Copyright (c) 1988, 1992, 1993 37316303Sjkim * The Regents of the University of California. All rights reserved. 38316303Sjkim * 39316303Sjkim * Redistribution and use in source and binary forms, with or without 40316303Sjkim * modification, are permitted provided that the following conditions 41316303Sjkim * are met: 42316303Sjkim * 1. Redistributions of source code must retain the above copyright 43316303Sjkim * notice, this list of conditions and the following disclaimer. 44316303Sjkim * 2. Redistributions in binary form must reproduce the above copyright 45316303Sjkim * notice, this list of conditions and the following disclaimer in the 46316303Sjkim * documentation and/or other materials provided with the distribution. 47316303Sjkim * 3. Neither the name of the University nor the names of its contributors 48316303Sjkim * may be used to endorse or promote products derived from this software 49316303Sjkim * without specific prior written permission. 50316303Sjkim * 51316303Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52316303Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53316303Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54316303Sjkim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55316303Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56316303Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57316303Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58316303Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59316303Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60316303Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61316303Sjkim * SUCH DAMAGE. 62316303Sjkim * 63316303Sjkim * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93 64316303Sjkim */ 65316303Sjkim 66316303Sjkim#include <sys/param.h> 67316303Sjkim#include <sys/systm.h> 68316303Sjkim#include <sys/mbuf.h> 69316303Sjkim 70316303Sjkim#include <netinet/in.h> 71316303Sjkim#include <netinet/in_systm.h> 72316303Sjkim#include <netinet/ip.h> 73316303Sjkim#include <netinet/ip_var.h> 74316303Sjkim 75316303Sjkim#include <machine/in_cksum.h> 76316303Sjkim 77316303Sjkim#define ADDCARRY(x) (x > 65535 ? x -= 65535 : x) 78316303Sjkim#define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);} 79316303Sjkim 80316303Sjkimint in4_cksum(struct mbuf *, u_int8_t, int, int); 81316303Sjkim 82316303Sjkimint 83316303Sjkimin4_cksum(struct mbuf *m, u_int8_t nxt, int off, int len) 84316303Sjkim{ 85316303Sjkim union { 86316303Sjkim struct ipovly ipov; 87316303Sjkim u_int16_t w[10]; 88316303Sjkim } u; 89316303Sjkim union { 90316303Sjkim u_int16_t s[2]; 91316303Sjkim u_int32_t l; 92316303Sjkim } l_util; 93316303Sjkim 94316303Sjkim u_int16_t *w; 95316303Sjkim int psum; 96316303Sjkim int sum = 0; 97316303Sjkim 98316303Sjkim if (nxt != 0) { 99316303Sjkim /* pseudo header */ 100316303Sjkim if (off < sizeof(struct ipovly)) 101316303Sjkim panic("in4_cksum: offset too short"); 102316303Sjkim if (m->m_len < sizeof(struct ip)) 103316303Sjkim panic("in4_cksum: bad mbuf chain"); 104316303Sjkim bzero(&u.ipov, sizeof(u.ipov)); 105316303Sjkim u.ipov.ih_len = htons(len); 106316303Sjkim u.ipov.ih_pr = nxt; 107316303Sjkim u.ipov.ih_src = mtod(m, struct ip *)->ip_src; 108316303Sjkim u.ipov.ih_dst = mtod(m, struct ip *)->ip_dst; 109316303Sjkim w = u.w; 110316303Sjkim /* assumes sizeof(ipov) == 20 */ 111316303Sjkim sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3]; sum += w[4]; 112316303Sjkim sum += w[5]; sum += w[6]; sum += w[7]; sum += w[8]; sum += w[9]; 113316303Sjkim } 114316303Sjkim 115316303Sjkim psum = in_cksum_skip(m, len + off, off); 116316303Sjkim psum = ~psum & 0xffff; 117316303Sjkim sum += psum; 118316303Sjkim REDUCE; 119233237Sjkim return (~sum & 0xffff); 120233237Sjkim} 121233237Sjkim