/*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 <linux/module.h>34#include <net/checksum.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#ifdef CONFIG_COLDFIRE95/*96* This is a version of ip_compute_csum() optimized for IP headers,97* which always checksum on 4 octet boundaries.98*/99__sum16 ip_fast_csum(const void *iph, unsigned int ihl)100{101return (__force __sum16)~do_csum(iph,ihl*4);102}103EXPORT_SYMBOL(ip_fast_csum);104#endif105106/*107* computes the checksum of a memory block at buff, length len,108* and adds in "sum" (32-bit)109*110* returns a 32-bit number suitable for feeding into itself111* or csum_tcpudp_magic112*113* this function must be called with even lengths, except114* for the last fragment, which may be odd115*116* it's best to have buff aligned on a 32-bit boundary117*/118__wsum csum_partial(const void *buff, int len, __wsum sum)119{120unsigned int result = do_csum(buff, len);121122/* add in old sum, and carry.. */123result += (__force u32)sum;124if ((__force u32)sum > result)125result += 1;126return (__force __wsum)result;127}128129EXPORT_SYMBOL(csum_partial);130131/*132* copy from fs while checksumming, otherwise like csum_partial133*/134135__wsum136csum_partial_copy_from_user(const void __user *src, void *dst,137int len, __wsum sum, int *csum_err)138{139if (csum_err) *csum_err = 0;140memcpy(dst, (__force const void *)src, len);141return csum_partial(dst, len, sum);142}143EXPORT_SYMBOL(csum_partial_copy_from_user);144145/*146* copy from ds while checksumming, otherwise like csum_partial147*/148149__wsum150csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)151{152memcpy(dst, src, len);153return csum_partial(dst, len, sum);154}155EXPORT_SYMBOL(csum_partial_copy_nocheck);156157158