/*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* This is a version of ip_compute_csum() optimized for IP headers,96* which always checksum on 4 octet boundaries.97*/98__sum16 ip_fast_csum(const void *iph, unsigned int ihl)99{100return (__force __sum16)~do_csum(iph,ihl*4);101}102103/*104* computes the checksum of a memory block at buff, length len,105* and adds in "sum" (32-bit)106*107* returns a 32-bit number suitable for feeding into itself108* or csum_tcpudp_magic109*110* this function must be called with even lengths, except111* for the last fragment, which may be odd112*113* it's best to have buff aligned on a 32-bit boundary114*/115/*116* Egads... That thing apparently assumes that *all* checksums it ever sees will117* be folded. Very likely a bug.118*/119__wsum csum_partial(const void *buff, int len, __wsum sum)120{121unsigned int result = do_csum(buff, len);122123/* add in old sum, and carry.. */124result += (__force u32)sum;125/* 16+c bits -> 16 bits */126result = (result & 0xffff) + (result >> 16);127return (__force __wsum)result;128}129130EXPORT_SYMBOL(csum_partial);131132/*133* this routine is used for miscellaneous IP-like checksums, mainly134* in icmp.c135*/136__sum16 ip_compute_csum(const void *buff, int len)137{138return (__force __sum16)~do_csum(buff,len);139}140141/*142* copy from fs while checksumming, otherwise like csum_partial143*/144145__wsum146csum_partial_copy_from_user(const void __user *src, void *dst, int len,147__wsum sum, int *csum_err)148{149if (csum_err) *csum_err = 0;150memcpy(dst, (__force const void *)src, len);151return csum_partial(dst, len, sum);152}153154/*155* copy from ds while checksumming, otherwise like csum_partial156*/157158__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}164165166