| /* |
| * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU General Public License version 2 as |
| * published by the Free Software Foundation. |
| * |
| * For ARC: Amit Shah <amitshah@gmx.net> |
| * People who wrote arch/m68knommu/lib/checksum.c |
| */ |
| |
| #include <linux/module.h> |
| #include <net/checksum.h> |
| #include <asm/checksum.h> |
| |
| static inline unsigned short from32to16(unsigned long x) |
| { |
| /* add up 16-bit and 16-bit for 16+c bit */ |
| x = (x & 0xffff) + (x >> 16); |
| /* add up carry.. */ |
| x = (x & 0xffff) + (x >> 16); |
| return x; |
| } |
| |
| static unsigned long do_csum(const unsigned char *buff, int len) |
| { |
| int odd, count; |
| unsigned long result = 0; |
| |
| if (len <= 0) |
| goto out; |
| odd = 1 & (unsigned long)buff; |
| if (odd) { |
| result = be16_to_cpu(*buff); |
| len--; |
| buff++; |
| } |
| count = len >> 1; /* nr of 16-bit words.. */ |
| if (count) { |
| if (2 & (unsigned long)buff) { |
| result += *(unsigned short *)buff; |
| count--; |
| len -= 2; |
| buff += 2; |
| } |
| count >>= 1; /* nr of 32-bit words.. */ |
| if (count) { |
| unsigned long carry = 0; |
| do { |
| unsigned long w = *(unsigned long *)buff; |
| count--; |
| buff += 4; |
| result += carry; |
| result += w; |
| carry = (w > result); |
| } while (count); |
| result += carry; |
| result = (result & 0xffff) + (result >> 16); |
| } |
| if (len & 2) { |
| result += *(unsigned short *)buff; |
| buff += 2; |
| } |
| } |
| if (len & 1) |
| result += le16_to_cpu(*buff); |
| result = from32to16(result); |
| if (odd) |
| result = ((result >> 8) & 0xff) | ((result & 0xff) << 8); |
| out: |
| return result; |
| } |
| |
| /* |
| * computes the checksum of a memory block at buff, length len, |
| * and adds in "sum" (32-bit) |
| * |
| * returns a 32-bit number suitable for feeding into itself |
| * or csum_tcpudp_magic |
| * |
| * this function must be called with even lengths, except |
| * for the last fragment, which may be odd |
| * |
| * it's best to have buff aligned on a 32-bit boundary |
| */ |
| unsigned int csum_partial(const void *buff, int len, unsigned int sum) |
| { |
| unsigned int result = do_csum(buff, len); |
| |
| /* add in old sum, and carry.. */ |
| result += sum; |
| if (sum > result) |
| result += 1; |
| return result; |
| } |
| EXPORT_SYMBOL(csum_partial); |
| |
| /* |
| * this routine is used for miscellaneous IP-like checksums, mainly |
| * in icmp.c |
| */ |
| unsigned short ip_compute_csum(const unsigned char *buff, int len) |
| { |
| return ~do_csum(buff, len); |
| } |
| EXPORT_SYMBOL(ip_compute_csum); |
| |
| /* |
| * copy from ds while checksumming, otherwise like csum_partial |
| */ |
| unsigned int csum_partial_copy(const char *src, char *dst, int len, int sum) |
| { |
| memcpy(dst, src, len); |
| return csum_partial(dst, len, sum); |
| } |
| |
| /* |
| * copy from fs while checksumming, otherwise like csum_partial |
| */ |
| unsigned int |
| csum_partial_copy_from_user(const char *src, char *dst, int len, int sum, |
| int *csum_err) |
| { |
| int missing; |
| |
| missing = copy_from_user(dst, src, len); |
| |
| if (missing) { |
| memset(dst + len - missing, 0, missing); |
| *csum_err = -EFAULT; |
| } |
| |
| return csum_partial(dst, len, sum); |
| |
| } |