Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * arch/sh64/lib/c-checksum.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * This file contains network checksum routines that are better done |
| 5 | * in an architecture-specific manner due to speed.. |
| 6 | */ |
| 7 | |
| 8 | #undef DEBUG |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/string.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <asm/byteorder.h> |
| 14 | #include <asm/uaccess.h> |
| 15 | |
| 16 | static inline unsigned short from64to16(unsigned long long x) |
| 17 | { |
| 18 | /* add up 32-bit words for 33 bits */ |
| 19 | x = (x & 0xffffffff) + (x >> 32); |
| 20 | /* add up 16-bit and 17-bit words for 17+c bits */ |
| 21 | x = (x & 0xffff) + (x >> 16); |
| 22 | /* add up 16-bit and 2-bit for 16+c bit */ |
| 23 | x = (x & 0xffff) + (x >> 16); |
| 24 | /* add up carry.. */ |
| 25 | x = (x & 0xffff) + (x >> 16); |
| 26 | return x; |
| 27 | } |
| 28 | |
| 29 | static inline unsigned short foldto16(unsigned long x) |
| 30 | { |
| 31 | /* add up 16-bit for 17 bits */ |
| 32 | x = (x & 0xffff) + (x >> 16); |
| 33 | /* add up carry.. */ |
| 34 | x = (x & 0xffff) + (x >> 16); |
| 35 | return x; |
| 36 | } |
| 37 | |
| 38 | static inline unsigned short myfoldto16(unsigned long long x) |
| 39 | { |
| 40 | /* Fold down to 32-bits so we don't loose in the typedef-less |
| 41 | network stack. */ |
| 42 | /* 64 to 33 */ |
| 43 | x = (x & 0xffffffff) + (x >> 32); |
| 44 | /* 33 to 32 */ |
| 45 | x = (x & 0xffffffff) + (x >> 32); |
| 46 | |
| 47 | /* add up 16-bit for 17 bits */ |
| 48 | x = (x & 0xffff) + (x >> 16); |
| 49 | /* add up carry.. */ |
| 50 | x = (x & 0xffff) + (x >> 16); |
| 51 | return x; |
| 52 | } |
| 53 | |
| 54 | #define odd(x) ((x)&1) |
| 55 | #define U16(x) ntohs(x) |
| 56 | |
| 57 | static unsigned long do_csum(const unsigned char *buff, int len) |
| 58 | { |
| 59 | int odd, count; |
| 60 | unsigned long result = 0; |
| 61 | |
| 62 | pr_debug("do_csum buff %p, len %d (0x%x)\n", buff, len, len); |
| 63 | #ifdef DEBUG |
| 64 | for (i = 0; i < len; i++) { |
| 65 | if ((i % 26) == 0) |
| 66 | printk("\n"); |
| 67 | printk("%02X ", buff[i]); |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | if (len <= 0) |
| 72 | goto out; |
| 73 | |
| 74 | odd = 1 & (unsigned long) buff; |
| 75 | if (odd) { |
| 76 | result = *buff << 8; |
| 77 | len--; |
| 78 | buff++; |
| 79 | } |
| 80 | count = len >> 1; /* nr of 16-bit words.. */ |
| 81 | if (count) { |
| 82 | if (2 & (unsigned long) buff) { |
| 83 | result += *(unsigned short *) buff; |
| 84 | count--; |
| 85 | len -= 2; |
| 86 | buff += 2; |
| 87 | } |
| 88 | count >>= 1; /* nr of 32-bit words.. */ |
| 89 | if (count) { |
| 90 | unsigned long carry = 0; |
| 91 | do { |
| 92 | unsigned long w = *(unsigned long *) buff; |
| 93 | buff += 4; |
| 94 | count--; |
| 95 | result += carry; |
| 96 | result += w; |
| 97 | carry = (w > result); |
| 98 | } while (count); |
| 99 | result += carry; |
| 100 | result = (result & 0xffff) + (result >> 16); |
| 101 | } |
| 102 | if (len & 2) { |
| 103 | result += *(unsigned short *) buff; |
| 104 | buff += 2; |
| 105 | } |
| 106 | } |
| 107 | if (len & 1) |
| 108 | result += *buff; |
| 109 | result = foldto16(result); |
| 110 | if (odd) |
| 111 | result = ((result >> 8) & 0xff) | ((result & 0xff) << 8); |
| 112 | |
| 113 | pr_debug("\nCHECKSUM is 0x%x\n", result); |
| 114 | |
| 115 | out: |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | /* computes the checksum of a memory block at buff, length len, |
| 120 | and adds in "sum" (32-bit) */ |
| 121 | unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum) |
| 122 | { |
| 123 | unsigned long long result = do_csum(buff, len); |
| 124 | |
| 125 | /* add in old sum, and carry.. */ |
| 126 | result += sum; |
| 127 | /* 32+c bits -> 32 bits */ |
| 128 | result = (result & 0xffffffff) + (result >> 32); |
| 129 | |
| 130 | pr_debug("csum_partial, buff %p len %d sum 0x%x result=0x%016Lx\n", |
| 131 | buff, len, sum, result); |
| 132 | |
| 133 | return result; |
| 134 | } |
| 135 | |
| 136 | /* Copy while checksumming, otherwise like csum_partial. */ |
| 137 | unsigned int |
| 138 | csum_partial_copy(const unsigned char *src, unsigned char *dst, int len, unsigned int sum) |
| 139 | { |
| 140 | sum = csum_partial(src, len, sum); |
| 141 | memcpy(dst, src, len); |
| 142 | |
| 143 | return sum; |
| 144 | } |
| 145 | |
| 146 | /* Copy from userspace and compute checksum. If we catch an exception |
| 147 | then zero the rest of the buffer. */ |
| 148 | unsigned int |
| 149 | csum_partial_copy_from_user(const unsigned char *src, unsigned char *dst, int len, |
| 150 | unsigned int sum, int *err_ptr) |
| 151 | { |
| 152 | int missing; |
| 153 | |
| 154 | pr_debug |
| 155 | ("csum_partial_copy_from_user src %p, dest %p, len %d, sum %08x, err_ptr %p\n", |
| 156 | src, dst, len, sum, err_ptr); |
| 157 | missing = copy_from_user(dst, src, len); |
| 158 | pr_debug(" access_ok %d\n", __access_ok((unsigned long) src, len)); |
| 159 | pr_debug(" missing %d\n", missing); |
| 160 | if (missing) { |
| 161 | memset(dst + len - missing, 0, missing); |
| 162 | *err_ptr = -EFAULT; |
| 163 | } |
| 164 | |
| 165 | return csum_partial(dst, len, sum); |
| 166 | } |
| 167 | |
| 168 | /* Copy to userspace and compute checksum. */ |
| 169 | unsigned int |
| 170 | csum_partial_copy_to_user(const unsigned char *src, unsigned char *dst, int len, |
| 171 | unsigned int sum, int *err_ptr) |
| 172 | { |
| 173 | sum = csum_partial(src, len, sum); |
| 174 | |
| 175 | if (copy_to_user(dst, src, len)) |
| 176 | *err_ptr = -EFAULT; |
| 177 | |
| 178 | return sum; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * This is a version of ip_compute_csum() optimized for IP headers, |
| 183 | * which always checksum on 4 octet boundaries. |
| 184 | */ |
| 185 | unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl) |
| 186 | { |
| 187 | pr_debug("ip_fast_csum %p,%d\n", iph, ihl); |
| 188 | |
| 189 | return ~do_csum(iph, ihl * 4); |
| 190 | } |
| 191 | |
| 192 | unsigned int csum_tcpudp_nofold(unsigned long saddr, |
| 193 | unsigned long daddr, |
| 194 | unsigned short len, |
| 195 | unsigned short proto, unsigned int sum) |
| 196 | { |
| 197 | unsigned long long result; |
| 198 | |
| 199 | pr_debug("ntohs(0x%x)=0x%x\n", 0xdead, ntohs(0xdead)); |
| 200 | pr_debug("htons(0x%x)=0x%x\n", 0xdead, htons(0xdead)); |
| 201 | |
| 202 | result = ((unsigned long long) saddr + |
| 203 | (unsigned long long) daddr + |
| 204 | (unsigned long long) sum + |
| 205 | ((unsigned long long) ntohs(len) << 16) + |
| 206 | ((unsigned long long) proto << 8)); |
| 207 | |
| 208 | /* Fold down to 32-bits so we don't loose in the typedef-less |
| 209 | network stack. */ |
| 210 | /* 64 to 33 */ |
| 211 | result = (result & 0xffffffff) + (result >> 32); |
| 212 | /* 33 to 32 */ |
| 213 | result = (result & 0xffffffff) + (result >> 32); |
| 214 | |
| 215 | pr_debug("%s saddr %x daddr %x len %x proto %x sum %x result %08Lx\n", |
| 216 | __FUNCTION__, saddr, daddr, len, proto, sum, result); |
| 217 | |
| 218 | return result; |
| 219 | } |
| 220 | |
| 221 | // Post SIM: |
| 222 | unsigned int |
| 223 | csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst, int len, unsigned int sum) |
| 224 | { |
| 225 | // unsigned dummy; |
| 226 | pr_debug("csum_partial_copy_nocheck src %p dst %p len %d\n", src, dst, |
| 227 | len); |
| 228 | |
| 229 | return csum_partial_copy(src, dst, len, sum); |
| 230 | } |