46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync/*-
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * Copyright (c) 1990 The Regents of the University of California.
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * All rights reserved.
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync *
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * Redistribution and use in source and binary forms, with or without
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * modification, are permitted provided that the following conditions
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * are met:
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * 1. Redistributions of source code must retain the above copyright
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * notice, this list of conditions and the following disclaimer.
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * 2. Redistributions in binary form must reproduce the above copyright
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * notice, this list of conditions and the following disclaimer in the
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * documentation and/or other materials provided with the distribution.
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * 4. Neither the name of the University nor the names of its contributors
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * may be used to endorse or promote products derived from this software
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * without specific prior written permission.
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync *
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * SUCH DAMAGE.
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync *
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * from tahoe: in_cksum.c 1.2 86/01/05
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * from: @(#)in_cksum.c 1.3 (Berkeley) 1/19/91
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * from: Id: in_cksum.c,v 1.8 1995/12/03 18:35:19 bde Exp
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * $FreeBSD: src/sys/i386/include/in_cksum.h,v 1.17.10.1.6.1 2009/04/15 03:14:26 kensmith Exp $
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync */
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#ifndef _MACHINE_IN_CKSUM_H_
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#define _MACHINE_IN_CKSUM_H_ 1
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#ifndef VBOX
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#ifndef _SYS_CDEFS_H_
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#error this file needs sys/cdefs.h as a prerequisite
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#endif
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync/*
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * MP safe (alfred)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync */
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#include <sys/cdefs.h>
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#else
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync# include "slirp.h"
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#endif
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#define in_cksum(m, len) in_cksum_skip(m, len, 0)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync/*
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * It it useful to have an Internet checksum routine which is inlineable
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * and optimized specifically for the task of computing IP header checksums
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * in the normal case (where there are no options and the header length is
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync * therefore always exactly five 32-bit words.
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync */
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#if defined(__GNUCLIKE_ASM) && !defined(__INTEL_COMPILER)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsyncstatic __inline u_int
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsyncin_cksum_hdr(const struct ip *ip)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync{
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync register u_int sum = 0;
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync __asm __volatile (
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "addl %1, %0\n"
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "adcl %2, %0\n"
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "adcl %3, %0\n"
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "adcl %4, %0\n"
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "adcl %5, %0\n"
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "adcl $0, %0"
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync : "+r" (sum)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync : "g" (((const u_int32_t *)ip)[0]),
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "g" (((const u_int32_t *)ip)[1]),
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "g" (((const u_int32_t *)ip)[2]),
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "g" (((const u_int32_t *)ip)[3]),
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "g" (((const u_int32_t *)ip)[4])
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync );
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync sum = (sum & 0xffff) + (sum >> 16);
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync if (sum > 0xffff)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync sum -= 0xffff;
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync return ~sum & 0xffff;
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync}
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsyncstatic __inline void
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsyncin_cksum_update(struct ip *ip)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync{
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync int __tmpsum;
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync __tmpsum = (int)ntohs(ip->ip_sum) + 256;
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16));
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync}
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsyncstatic __inline u_short
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsyncin_addword(u_short sum, u_short b)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync{
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync /* __volatile is necessary because the condition codes are used. */
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync __asm __volatile (
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "addw %1, %0\n"
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "adcw $0, %0"
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync : "+r" (sum)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync : "r" (b)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync );
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync return (sum);
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync}
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsyncstatic __inline u_short
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsyncin_pseudo(u_int sum, u_int b, u_int c)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync{
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync /* __volatile is necessary because the condition codes are used. */
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync __asm __volatile (
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "addl %1, %0\n"
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "adcl %2, %0\n"
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "adcl $0, %0"
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync : "+r" (sum)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync : "g" (b),
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync "g" (c)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync );
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync sum = (sum & 0xffff) + (sum >> 16);
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync if (sum > 0xffff)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync sum -= 0xffff;
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync return (sum);
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync}
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#else
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#define in_cksum_update(ip) \
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync do { \
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync int __tmpsum; \
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync __tmpsum = (int)ntohs(ip->ip_sum) + 256; \
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync ip->ip_sum = htons(__tmpsum + (__tmpsum >> 16)); \
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync } while(0)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#endif
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#if defined(_KERNEL) || defined(VBOX)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#if !defined(__GNUCLIKE_ASM) || defined(__INTEL_COMPILER)
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsyncu_int in_cksum_hdr(const struct ip *ip);
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsyncu_short in_addword(u_short sum, u_short b);
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsyncu_short in_pseudo(u_int sum, u_int b, u_int c);
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#endif
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsyncu_short in_cksum_skip(struct mbuf *m, int len, int skip);
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#endif /* _KERNEL */
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync
46192b2e5dd6b322ba3d1aae79e97074d50cdf89vboxsync#endif /* _MACHINE_IN_CKSUM_H_ */