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
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 * Copyright (C) 2014 Imagination Technologies Ltd.
12 #ifndef _ASM_CHECKSUM_H
13 #define _ASM_CHECKSUM_H
15 #include <linux/in6.h>
17 #include <asm/uaccess.h>
20 * computes the checksum of a memory block at buff, length len,
21 * and adds in "sum" (32-bit)
23 * returns a 32-bit number suitable for feeding into itself
24 * or csum_tcpudp_magic
26 * this function must be called with even lengths, except
27 * for the last fragment, which may be odd
29 * it's best to have buff aligned on a 32-bit boundary
31 __wsum csum_partial(const void *buff, int len, __wsum sum);
33 __wsum __csum_partial_copy_kernel(const void *src, void *dst,
34 int len, __wsum sum, int *err_ptr);
36 __wsum __csum_partial_copy_from_user(const void *src, void *dst,
37 int len, __wsum sum, int *err_ptr);
38 __wsum __csum_partial_copy_to_user(const void *src, void *dst,
39 int len, __wsum sum, int *err_ptr);
41 * this is a new version of the above that records errors it finds in *errp,
42 * but continues and zeros the rest of the buffer.
45 __wsum csum_partial_copy_from_user(const void __user *src, void *dst, int len,
46 __wsum sum, int *err_ptr)
49 if (segment_eq(get_fs(), get_ds()))
50 return __csum_partial_copy_kernel((__force void *)src, dst,
53 return __csum_partial_copy_from_user((__force void *)src, dst,
57 #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
59 __wsum csum_and_copy_from_user(const void __user *src, void *dst,
60 int len, __wsum sum, int *err_ptr)
62 if (access_ok(VERIFY_READ, src, len))
63 return csum_partial_copy_from_user(src, dst, len, sum,
72 * Copy and checksum to user
74 #define HAVE_CSUM_COPY_USER
76 __wsum csum_and_copy_to_user(const void *src, void __user *dst, int len,
77 __wsum sum, int *err_ptr)
80 if (access_ok(VERIFY_WRITE, dst, len)) {
81 if (segment_eq(get_fs(), get_ds()))
82 return __csum_partial_copy_kernel(src,
86 return __csum_partial_copy_to_user(src,
93 return (__force __wsum)-1; /* invalid checksum */
97 * the same as csum_partial, but copies from user space (but on MIPS
98 * we have just one address space, so this is identical to the above)
100 __wsum csum_partial_copy_nocheck(const void *src, void *dst,
101 int len, __wsum sum);
102 #define csum_partial_copy_nocheck csum_partial_copy_nocheck
105 * Fold a partial checksum without adding pseudo headers
107 static inline __sum16 csum_fold(__wsum csum)
109 u32 sum = (__force u32)csum;;
116 return (__force __sum16)~sum;
118 #define csum_fold csum_fold
121 * This is a version of ip_compute_csum() optimized for IP headers,
122 * which always checksum on 4 octet boundaries.
124 * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
127 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
129 const unsigned int *word = iph;
130 const unsigned int *stop = word + ihl;
136 carry = (csum < word[1]);
140 carry = (csum < word[2]);
144 carry = (csum < word[3]);
150 carry = (csum < *word);
153 } while (word != stop);
155 return csum_fold(csum);
157 #define ip_fast_csum ip_fast_csum
159 static inline __wsum csum_tcpudp_nofold(__be32 saddr,
160 __be32 daddr, unsigned short len, unsigned short proto,
164 " .set push # csum_tcpudp_nofold\n"
168 " sltu $1, %0, %2 \n"
172 " sltu $1, %0, %3 \n"
176 " sltu $1, %0, %4 \n"
183 " dsll32 $1, %0, 0 \n"
185 " dsra32 %0, %0, 0 \n"
189 : "0" ((__force unsigned long)daddr),
190 "r" ((__force unsigned long)saddr),
192 "r" ((proto + len) << 8),
196 "r" ((__force unsigned long)sum));
200 #define csum_tcpudp_nofold csum_tcpudp_nofold
203 * computes the checksum of the TCP/UDP pseudo-header
204 * returns a 16-bit checksum, already complemented
206 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
208 unsigned short proto,
211 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
213 #define csum_tcpudp_magic csum_tcpudp_magic
216 * this routine is used for miscellaneous IP-like checksums, mainly
219 static inline __sum16 ip_compute_csum(const void *buff, int len)
221 return csum_fold(csum_partial(buff, len, 0));
224 #define _HAVE_ARCH_IPV6_CSUM
225 static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
226 const struct in6_addr *daddr,
227 __u32 len, unsigned short proto,
231 " .set push # csum_ipv6_magic\n"
234 " addu %0, %5 # proto (long in network byte order)\n"
235 " sltu $1, %0, %5 \n"
238 " addu %0, %6 # csum\n"
239 " sltu $1, %0, %6 \n"
240 " lw %1, 0(%2) # four words source address\n"
243 " sltu $1, %0, %1 \n"
248 " sltu $1, %0, %1 \n"
253 " sltu $1, %0, %1 \n"
258 " sltu $1, %0, %1 \n"
263 " sltu $1, %0, %1 \n"
268 " sltu $1, %0, %1 \n"
273 " sltu $1, %0, %1 \n"
278 " sltu $1, %0, %1 \n"
280 " addu %0, $1 # Add final carry\n"
282 : "=r" (sum), "=r" (proto)
283 : "r" (saddr), "r" (daddr),
284 "0" (htonl(len)), "1" (htonl(proto)), "r" (sum));
286 return csum_fold(sum);
289 #include <asm-generic/checksum.h>
291 #endif /* _ASM_CHECKSUM_H */