| [2] | 1 | /* |
|---|
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
|---|
| 3 | * License. See the file "COPYING" in the main directory of this archive |
|---|
| 4 | * for more details. |
|---|
| 5 | * |
|---|
| 6 | * Copyright (C) 1995, 96, 97, 98, 99, 2001 by Ralf Baechle |
|---|
| 7 | * Copyright (C) 1999 Silicon Graphics, Inc. |
|---|
| 8 | * Copyright (C) 2001 Thiemo Seufer. |
|---|
| 9 | * Copyright (C) 2002 Maciej W. Rozycki |
|---|
| 10 | */ |
|---|
| 11 | #ifndef _ASM_CHECKSUM_H |
|---|
| 12 | #define _ASM_CHECKSUM_H |
|---|
| 13 | |
|---|
| 14 | #include <linux/in6.h> |
|---|
| 15 | |
|---|
| 16 | #include <asm/uaccess.h> |
|---|
| 17 | |
|---|
| 18 | /* |
|---|
| 19 | * computes the checksum of a memory block at buff, length len, |
|---|
| 20 | * and adds in "sum" (32-bit) |
|---|
| 21 | * |
|---|
| 22 | * returns a 32-bit number suitable for feeding into itself |
|---|
| 23 | * or csum_tcpudp_magic |
|---|
| 24 | * |
|---|
| 25 | * this function must be called with even lengths, except |
|---|
| 26 | * for the last fragment, which may be odd |
|---|
| 27 | * |
|---|
| 28 | * it's best to have buff aligned on a 32-bit boundary |
|---|
| 29 | */ |
|---|
| 30 | unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum); |
|---|
| 31 | |
|---|
| 32 | /* |
|---|
| 33 | * this is a new version of the above that records errors it finds in *errp, |
|---|
| 34 | * but continues and zeros the rest of the buffer. |
|---|
| 35 | */ |
|---|
| 36 | unsigned int csum_partial_copy_from_user(const char *src, char *dst, int len, |
|---|
| 37 | unsigned int sum, int *errp); |
|---|
| 38 | |
|---|
| 39 | /* |
|---|
| 40 | * Copy and checksum to user |
|---|
| 41 | */ |
|---|
| 42 | #define HAVE_CSUM_COPY_USER |
|---|
| 43 | static inline unsigned int csum_and_copy_to_user (const char *src, char *dst, |
|---|
| 44 | int len, int sum, |
|---|
| 45 | int *err_ptr) |
|---|
| 46 | { |
|---|
| 47 | might_sleep(); |
|---|
| 48 | sum = csum_partial(src, len, sum); |
|---|
| 49 | |
|---|
| 50 | if (copy_to_user(dst, src, len)) { |
|---|
| 51 | *err_ptr = -EFAULT; |
|---|
| 52 | return -1; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | return sum; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /* |
|---|
| 59 | * the same as csum_partial, but copies from user space (but on MIPS |
|---|
| 60 | * we have just one address space, so this is identical to the above) |
|---|
| 61 | */ |
|---|
| 62 | unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst, |
|---|
| 63 | int len, unsigned int sum); |
|---|
| 64 | |
|---|
| 65 | /* |
|---|
| 66 | * Fold a partial checksum without adding pseudo headers |
|---|
| 67 | */ |
|---|
| 68 | static inline unsigned short int csum_fold(unsigned int sum) |
|---|
| 69 | { |
|---|
| 70 | __asm__( |
|---|
| 71 | ".set\tnoat\t\t\t# csum_fold\n\t" |
|---|
| 72 | "sll\t$1,%0,16\n\t" |
|---|
| 73 | "addu\t%0,$1\n\t" |
|---|
| 74 | "sltu\t$1,%0,$1\n\t" |
|---|
| 75 | "srl\t%0,%0,16\n\t" |
|---|
| 76 | "addu\t%0,$1\n\t" |
|---|
| 77 | "xori\t%0,0xffff\n\t" |
|---|
| 78 | ".set\tat" |
|---|
| 79 | : "=r" (sum) |
|---|
| 80 | : "0" (sum)); |
|---|
| 81 | |
|---|
| 82 | return sum; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /* |
|---|
| 86 | * This is a version of ip_compute_csum() optimized for IP headers, |
|---|
| 87 | * which always checksum on 4 octet boundaries. |
|---|
| 88 | * |
|---|
| 89 | * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by |
|---|
| 90 | * Arnt Gulbrandsen. |
|---|
| 91 | */ |
|---|
| 92 | static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl) |
|---|
| 93 | { |
|---|
| 94 | unsigned int *word = (unsigned int *) iph; |
|---|
| 95 | unsigned int *stop = word + ihl; |
|---|
| 96 | unsigned int csum; |
|---|
| 97 | int carry; |
|---|
| 98 | |
|---|
| 99 | csum = word[0]; |
|---|
| 100 | csum += word[1]; |
|---|
| 101 | carry = (csum < word[1]); |
|---|
| 102 | csum += carry; |
|---|
| 103 | |
|---|
| 104 | csum += word[2]; |
|---|
| 105 | carry = (csum < word[2]); |
|---|
| 106 | csum += carry; |
|---|
| 107 | |
|---|
| 108 | csum += word[3]; |
|---|
| 109 | carry = (csum < word[3]); |
|---|
| 110 | csum += carry; |
|---|
| 111 | |
|---|
| 112 | word += 4; |
|---|
| 113 | do { |
|---|
| 114 | csum += *word; |
|---|
| 115 | carry = (csum < *word); |
|---|
| 116 | csum += carry; |
|---|
| 117 | word++; |
|---|
| 118 | } while (word != stop); |
|---|
| 119 | |
|---|
| 120 | return csum_fold(csum); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | static inline unsigned int csum_tcpudp_nofold(unsigned long saddr, |
|---|
| 124 | unsigned long daddr, unsigned short len, unsigned short proto, |
|---|
| 125 | unsigned int sum) |
|---|
| 126 | { |
|---|
| 127 | __asm__( |
|---|
| 128 | ".set\tnoat\t\t\t# csum_tcpudp_nofold\n\t" |
|---|
| 129 | #ifndef __mips64 |
|---|
| 130 | "addu\t%0, %2\n\t" |
|---|
| 131 | "sltu\t$1, %0, %2\n\t" |
|---|
| 132 | "addu\t%0, $1\n\t" |
|---|
| 133 | |
|---|
| 134 | "addu\t%0, %3\n\t" |
|---|
| 135 | "sltu\t$1, %0, %3\n\t" |
|---|
| 136 | "addu\t%0, $1\n\t" |
|---|
| 137 | |
|---|
| 138 | "addu\t%0, %4\n\t" |
|---|
| 139 | "sltu\t$1, %0, %4\n\t" |
|---|
| 140 | "addu\t%0, $1\n\t" |
|---|
| 141 | #endif |
|---|
| 142 | #ifdef __mips64 |
|---|
| 143 | "daddu\t%0, %2\n\t" |
|---|
| 144 | "daddu\t%0, %3\n\t" |
|---|
| 145 | "daddu\t%0, %4\n\t" |
|---|
| 146 | "dsll32\t$1, %0, 0\n\t" |
|---|
| 147 | "daddu\t%0, $1\n\t" |
|---|
| 148 | "dsrl32\t%0, %0, 0\n\t" |
|---|
| 149 | #endif |
|---|
| 150 | ".set\tat" |
|---|
| 151 | : "=r" (sum) |
|---|
| 152 | : "0" (daddr), "r"(saddr), |
|---|
| 153 | #ifdef __MIPSEL__ |
|---|
| 154 | "r" (((unsigned long)htons(len)<<16) + proto*256), |
|---|
| 155 | #else |
|---|
| 156 | "r" (((unsigned long)(proto)<<16) + len), |
|---|
| 157 | #endif |
|---|
| 158 | "r" (sum)); |
|---|
| 159 | |
|---|
| 160 | return sum; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | /* |
|---|
| 164 | * computes the checksum of the TCP/UDP pseudo-header |
|---|
| 165 | * returns a 16-bit checksum, already complemented |
|---|
| 166 | */ |
|---|
| 167 | static inline unsigned short int csum_tcpudp_magic(unsigned long saddr, |
|---|
| 168 | unsigned long daddr, |
|---|
| 169 | unsigned short len, |
|---|
| 170 | unsigned short proto, |
|---|
| 171 | unsigned int sum) |
|---|
| 172 | { |
|---|
| 173 | return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum)); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | /* |
|---|
| 177 | * this routine is used for miscellaneous IP-like checksums, mainly |
|---|
| 178 | * in icmp.c |
|---|
| 179 | */ |
|---|
| 180 | static inline unsigned short ip_compute_csum(unsigned char * buff, int len) |
|---|
| 181 | { |
|---|
| 182 | return csum_fold(csum_partial(buff, len, 0)); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | #define _HAVE_ARCH_IPV6_CSUM |
|---|
| 186 | static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr, |
|---|
| 187 | struct in6_addr *daddr, |
|---|
| 188 | __u32 len, |
|---|
| 189 | unsigned short proto, |
|---|
| 190 | unsigned int sum) |
|---|
| 191 | { |
|---|
| 192 | __asm__( |
|---|
| 193 | ".set\tpush\t\t\t# csum_ipv6_magic\n\t" |
|---|
| 194 | ".set\tnoreorder\n\t" |
|---|
| 195 | ".set\tnoat\n\t" |
|---|
| 196 | "addu\t%0, %5\t\t\t# proto (long in network byte order)\n\t" |
|---|
| 197 | "sltu\t$1, %0, %5\n\t" |
|---|
| 198 | "addu\t%0, $1\n\t" |
|---|
| 199 | |
|---|
| 200 | "addu\t%0, %6\t\t\t# csum\n\t" |
|---|
| 201 | "sltu\t$1, %0, %6\n\t" |
|---|
| 202 | "lw\t%1, 0(%2)\t\t\t# four words source address\n\t" |
|---|
| 203 | "addu\t%0, $1\n\t" |
|---|
| 204 | "addu\t%0, %1\n\t" |
|---|
| 205 | "sltu\t$1, %0, %1\n\t" |
|---|
| 206 | |
|---|
| 207 | "lw\t%1, 4(%2)\n\t" |
|---|
| 208 | "addu\t%0, $1\n\t" |
|---|
| 209 | "addu\t%0, %1\n\t" |
|---|
| 210 | "sltu\t$1, %0, %1\n\t" |
|---|
| 211 | |
|---|
| 212 | "lw\t%1, 8(%2)\n\t" |
|---|
| 213 | "addu\t%0, $1\n\t" |
|---|
| 214 | "addu\t%0, %1\n\t" |
|---|
| 215 | "sltu\t$1, %0, %1\n\t" |
|---|
| 216 | |
|---|
| 217 | "lw\t%1, 12(%2)\n\t" |
|---|
| 218 | "addu\t%0, $1\n\t" |
|---|
| 219 | "addu\t%0, %1\n\t" |
|---|
| 220 | "sltu\t$1, %0, %1\n\t" |
|---|
| 221 | |
|---|
| 222 | "lw\t%1, 0(%3)\n\t" |
|---|
| 223 | "addu\t%0, $1\n\t" |
|---|
| 224 | "addu\t%0, %1\n\t" |
|---|
| 225 | "sltu\t$1, %0, %1\n\t" |
|---|
| 226 | |
|---|
| 227 | "lw\t%1, 4(%3)\n\t" |
|---|
| 228 | "addu\t%0, $1\n\t" |
|---|
| 229 | "addu\t%0, %1\n\t" |
|---|
| 230 | "sltu\t$1, %0, %1\n\t" |
|---|
| 231 | |
|---|
| 232 | "lw\t%1, 8(%3)\n\t" |
|---|
| 233 | "addu\t%0, $1\n\t" |
|---|
| 234 | "addu\t%0, %1\n\t" |
|---|
| 235 | "sltu\t$1, %0, %1\n\t" |
|---|
| 236 | |
|---|
| 237 | "lw\t%1, 12(%3)\n\t" |
|---|
| 238 | "addu\t%0, $1\n\t" |
|---|
| 239 | "addu\t%0, %1\n\t" |
|---|
| 240 | "sltu\t$1, %0, %1\n\t" |
|---|
| 241 | |
|---|
| 242 | "addu\t%0, $1\t\t\t# Add final carry\n\t" |
|---|
| 243 | ".set\tpop" |
|---|
| 244 | : "=r" (sum), "=r" (proto) |
|---|
| 245 | : "r" (saddr), "r" (daddr), |
|---|
| 246 | "0" (htonl(len)), "1" (htonl(proto)), "r" (sum)); |
|---|
| 247 | |
|---|
| 248 | return csum_fold(sum); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | #endif /* _ASM_CHECKSUM_H */ |
|---|