/*1* INET An implementation of the TCP/IP protocol suite for the LINUX2* operating system. INET is implemented using the BSD Socket3* interface as the means of communication with the user level.4*5* IP/TCP/UDP checksumming routines6*7* Authors: Jorge Cwik, <[email protected]>8* Arnt Gulbrandsen, <[email protected]>9* Tom May, <[email protected]>10* Andreas Schwab, <[email protected]>11* Lots of code moved from tcp.c and ip.c; see those files12* for more names.13*14* 03/02/96 Jes Sorensen, Andreas Schwab, Roman Hodek:15* Fixed some nasty bugs, causing some horrible crashes.16* A: At some points, the sum (%0) was used as17* length-counter instead of the length counter18* (%1). Thanks to Roman Hodek for pointing this out.19* B: GCC seems to mess up if one uses too many20* data-registers to hold input values and one tries to21* specify d0 and d1 as scratch registers. Letting gcc choose these22* registers itself solves the problem.23*24* This program is free software; you can redistribute it and/or25* modify it under the terms of the GNU General Public License26* as published by the Free Software Foundation; either version27* 2 of the License, or (at your option) any later version.28*/2930/* Revised by Kenneth Albanowski for m68knommu. Basic problem: unaligned access kills, so most31of the assembly has to go. */3233#include <net/checksum.h>34#include <linux/module.h>3536static inline unsigned short from32to16(unsigned long x)37{38/* add up 16-bit and 16-bit for 16+c bit */39x = (x & 0xffff) + (x >> 16);40/* add up carry.. */41x = (x & 0xffff) + (x >> 16);42return x;43}4445static unsigned long do_csum(const unsigned char * buff, int len)46{47int odd, count;48unsigned long result = 0;4950if (len <= 0)51goto out;52odd = 1 & (unsigned long) buff;53if (odd) {54result = *buff;55len--;56buff++;57}58count = len >> 1; /* nr of 16-bit words.. */59if (count) {60if (2 & (unsigned long) buff) {61result += *(unsigned short *) buff;62count--;63len -= 2;64buff += 2;65}66count >>= 1; /* nr of 32-bit words.. */67if (count) {68unsigned long carry = 0;69do {70unsigned long w = *(unsigned long *) buff;71count--;72buff += 4;73result += carry;74result += w;75carry = (w > result);76} while (count);77result += carry;78result = (result & 0xffff) + (result >> 16);79}80if (len & 2) {81result += *(unsigned short *) buff;82buff += 2;83}84}85if (len & 1)86result += (*buff << 8);87result = from32to16(result);88if (odd)89result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);90out:91return result;92}9394/*95* computes the checksum of a memory block at buff, length len,96* and adds in "sum" (32-bit)97*98* returns a 32-bit number suitable for feeding into itself99* or csum_tcpudp_magic100*101* this function must be called with even lengths, except102* for the last fragment, which may be odd103*104* it's best to have buff aligned on a 32-bit boundary105*/106__wsum csum_partial(const void *buff, int len, __wsum sum)107{108unsigned int result = do_csum(buff, len);109110/* add in old sum, and carry.. */111result += (__force u32)sum;112if ((__force u32)sum > result)113result += 1;114return (__force __wsum)result;115}116117EXPORT_SYMBOL(csum_partial);118119/*120* this routine is used for miscellaneous IP-like checksums, mainly121* in icmp.c122*/123__sum16 ip_compute_csum(const void *buff, int len)124{125return (__force __sum16)~do_csum(buff, len);126}127128EXPORT_SYMBOL(ip_compute_csum);129130/*131* copy from fs while checksumming, otherwise like csum_partial132*/133__wsum134csum_partial_copy_from_user(const void __user *src, void *dst,135int len, __wsum sum, int *csum_err)136{137int rem;138139if (csum_err)140*csum_err = 0;141142rem = copy_from_user(dst, src, len);143if (rem != 0) {144if (csum_err)145*csum_err = -EFAULT;146memset(dst + len - rem, 0, rem);147len = rem;148}149150return csum_partial(dst, len, sum);151}152153EXPORT_SYMBOL(csum_partial_copy_from_user);154155/*156* copy from ds while checksumming, otherwise like csum_partial157*/158__wsum159csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)160{161memcpy(dst, src, len);162return csum_partial(dst, len, sum);163}164165EXPORT_SYMBOL(csum_partial_copy_nocheck);166167168