Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: arch/blackfin/lib/checksum.c |
| 3 | * Based on: none - original work |
| 4 | * Author: |
| 5 | * |
| 6 | * Created: |
| 7 | * Description: An implementation of the TCP/IP protocol suite for the LINUX |
| 8 | * operating system. INET is implemented using the BSD Socket |
| 9 | * interface as the means of communication with the user level. |
| 10 | * |
| 11 | * Modified: |
| 12 | * Copyright 2004-2006 Analog Devices Inc. |
| 13 | * |
| 14 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2 of the License, or |
| 19 | * (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, see the file COPYING, or write |
| 28 | * to the Free Software Foundation, Inc., |
| 29 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 30 | */ |
| 31 | |
Mike Frysinger | fe8015c | 2008-10-28 11:07:15 +0800 | [diff] [blame] | 32 | #include <linux/module.h> |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 33 | #include <net/checksum.h> |
| 34 | #include <asm/checksum.h> |
| 35 | |
| 36 | #ifdef CONFIG_IP_CHECKSUM_L1 |
| 37 | static unsigned short do_csum(const unsigned char *buff, int len)__attribute__((l1_text)); |
| 38 | #endif |
| 39 | |
| 40 | static unsigned short do_csum(const unsigned char *buff, int len) |
| 41 | { |
| 42 | register unsigned long sum = 0; |
| 43 | int swappem = 0; |
| 44 | |
| 45 | if (1 & (unsigned long)buff) { |
| 46 | sum = *buff << 8; |
| 47 | buff++; |
| 48 | len--; |
| 49 | ++swappem; |
| 50 | } |
| 51 | |
| 52 | while (len > 1) { |
| 53 | sum += *(unsigned short *)buff; |
| 54 | buff += 2; |
| 55 | len -= 2; |
| 56 | } |
| 57 | |
| 58 | if (len > 0) |
| 59 | sum += *buff; |
| 60 | |
| 61 | /* Fold 32-bit sum to 16 bits */ |
| 62 | while (sum >> 16) |
| 63 | sum = (sum & 0xffff) + (sum >> 16); |
| 64 | |
| 65 | if (swappem) |
| 66 | sum = ((sum & 0xff00) >> 8) + ((sum & 0x00ff) << 8); |
| 67 | |
| 68 | return sum; |
| 69 | |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * This is a version of ip_compute_csum() optimized for IP headers, |
| 74 | * which always checksum on 4 octet boundaries. |
| 75 | */ |
Al Viro | 45b3947 | 2008-05-12 11:55:10 +0800 | [diff] [blame] | 76 | __sum16 ip_fast_csum(unsigned char *iph, unsigned int ihl) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 77 | { |
Al Viro | 45b3947 | 2008-05-12 11:55:10 +0800 | [diff] [blame] | 78 | return (__force __sum16)~do_csum(iph, ihl * 4); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 79 | } |
Mike Frysinger | fe8015c | 2008-10-28 11:07:15 +0800 | [diff] [blame] | 80 | EXPORT_SYMBOL(ip_fast_csum); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * computes the checksum of a memory block at buff, length len, |
| 84 | * and adds in "sum" (32-bit) |
| 85 | * |
| 86 | * returns a 32-bit number suitable for feeding into itself |
| 87 | * or csum_tcpudp_magic |
| 88 | * |
| 89 | * this function must be called with even lengths, except |
| 90 | * for the last fragment, which may be odd |
| 91 | * |
| 92 | * it's best to have buff aligned on a 32-bit boundary |
| 93 | */ |
Al Viro | 45b3947 | 2008-05-12 11:55:10 +0800 | [diff] [blame] | 94 | __wsum csum_partial(const void *buff, int len, __wsum sum) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 95 | { |
| 96 | /* |
| 97 | * Just in case we get nasty checksum data... |
| 98 | * Like 0xffff6ec3 in the case of our IPv6 multicast header. |
| 99 | * We fold to begin with, as well as at the end. |
| 100 | */ |
| 101 | sum = (sum & 0xffff) + (sum >> 16); |
| 102 | |
| 103 | sum += do_csum(buff, len); |
| 104 | |
| 105 | sum = (sum & 0xffff) + (sum >> 16); |
| 106 | |
| 107 | return sum; |
| 108 | } |
Mike Frysinger | fe8015c | 2008-10-28 11:07:15 +0800 | [diff] [blame] | 109 | EXPORT_SYMBOL(csum_partial); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * this routine is used for miscellaneous IP-like checksums, mainly |
| 113 | * in icmp.c |
| 114 | */ |
Al Viro | 45b3947 | 2008-05-12 11:55:10 +0800 | [diff] [blame] | 115 | __sum16 ip_compute_csum(const void *buff, int len) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 116 | { |
Al Viro | 45b3947 | 2008-05-12 11:55:10 +0800 | [diff] [blame] | 117 | return (__force __sum16)~do_csum(buff, len); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* |
| 121 | * copy from fs while checksumming, otherwise like csum_partial |
| 122 | */ |
| 123 | |
Al Viro | 45b3947 | 2008-05-12 11:55:10 +0800 | [diff] [blame] | 124 | __wsum |
| 125 | csum_partial_copy_from_user(const void __user *src, void *dst, |
| 126 | int len, __wsum sum, int *csum_err) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 127 | { |
| 128 | if (csum_err) |
| 129 | *csum_err = 0; |
Al Viro | 45b3947 | 2008-05-12 11:55:10 +0800 | [diff] [blame] | 130 | memcpy(dst, (__force void *)src, len); |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 131 | return csum_partial(dst, len, sum); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * copy from ds while checksumming, otherwise like csum_partial |
| 136 | */ |
| 137 | |
Al Viro | 45b3947 | 2008-05-12 11:55:10 +0800 | [diff] [blame] | 138 | __wsum csum_partial_copy(const void *src, void *dst, int len, __wsum sum) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 139 | { |
| 140 | memcpy(dst, src, len); |
| 141 | return csum_partial(dst, len, sum); |
| 142 | } |
Mike Frysinger | fe8015c | 2008-10-28 11:07:15 +0800 | [diff] [blame] | 143 | EXPORT_SYMBOL(csum_partial_copy); |