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 #ifdef CONFIG_GENERIC_CSUM
16 #include <asm-generic/checksum.h>
19 #include <linux/in6.h>
21 #include <linux/uaccess.h>
24 * computes the checksum of a memory block at buff, length len,
25 * and adds in "sum" (32-bit)
27 * returns a 32-bit number suitable for feeding into itself
28 * or csum_tcpudp_magic
30 * this function must be called with even lengths, except
31 * for the last fragment, which may be odd
33 * it's best to have buff aligned on a 32-bit boundary
35 __wsum
csum_partial(const void *buff
, int len
, __wsum sum
);
37 __wsum
__csum_partial_copy_from_user(const void __user
*src
, void *dst
, int len
);
38 __wsum
__csum_partial_copy_to_user(const void *src
, void __user
*dst
, int len
);
40 #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
42 __wsum
csum_and_copy_from_user(const void __user
*src
, void *dst
, int len
)
45 if (!access_ok(src
, len
))
47 return __csum_partial_copy_from_user(src
, dst
, len
);
51 * Copy and checksum to user
53 #define HAVE_CSUM_COPY_USER
55 __wsum
csum_and_copy_to_user(const void *src
, void __user
*dst
, int len
)
58 if (!access_ok(dst
, len
))
60 return __csum_partial_copy_to_user(src
, dst
, len
);
64 * the same as csum_partial, but copies from user space (but on MIPS
65 * we have just one address space, so this is identical to the above)
67 #define _HAVE_ARCH_CSUM_AND_COPY
68 __wsum
__csum_partial_copy_nocheck(const void *src
, void *dst
, int len
);
69 static inline __wsum
csum_partial_copy_nocheck(const void *src
, void *dst
, int len
)
71 return __csum_partial_copy_nocheck(src
, dst
, len
);
75 * Fold a partial checksum without adding pseudo headers
77 static inline __sum16
csum_fold(__wsum csum
)
79 u32 sum
= (__force u32
)csum
;
82 csum
= (__force __wsum
)(sum
< (__force u32
)csum
);
84 sum
+= (__force u32
)csum
;
86 return (__force __sum16
)~sum
;
88 #define csum_fold csum_fold
91 * This is a version of ip_compute_csum() optimized for IP headers,
92 * which always checksum on 4 octet boundaries.
94 * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
97 static inline __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
)
99 const unsigned int *word
= iph
;
100 const unsigned int *stop
= word
+ ihl
;
106 carry
= (csum
< word
[1]);
110 carry
= (csum
< word
[2]);
114 carry
= (csum
< word
[3]);
120 carry
= (csum
< *word
);
123 } while (word
!= stop
);
125 return csum_fold(csum
);
127 #define ip_fast_csum ip_fast_csum
129 static inline __wsum
csum_tcpudp_nofold(__be32 saddr
, __be32 daddr
,
130 __u32 len
, __u8 proto
,
133 const unsigned int sh32
= IS_ENABLED(CONFIG_64BIT
) ? 32 : 0;
134 unsigned long sum
= (__force
unsigned long)daddr
;
138 tmp
= (__force
unsigned long)saddr
;
141 if (IS_ENABLED(CONFIG_32BIT
))
145 * We know PROTO + LEN has the sign bit clear, so cast to a signed
146 * type to avoid an extraneous zero-extension where TMP is 64-bit.
148 tmp
= (__s32
)(proto
+ len
);
149 tmp
<<= IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN
) ? 8 : 0;
151 if (IS_ENABLED(CONFIG_32BIT
))
154 tmp
= (__force
unsigned long)isum
;
157 if (IS_ENABLED(CONFIG_32BIT
)) {
160 } else if (IS_ENABLED(CONFIG_64BIT
)) {
169 return (__force __wsum
)osum
;
171 #define csum_tcpudp_nofold csum_tcpudp_nofold
174 * this routine is used for miscellaneous IP-like checksums, mainly
177 static inline __sum16
ip_compute_csum(const void *buff
, int len
)
179 return csum_fold(csum_partial(buff
, len
, 0));
182 #define _HAVE_ARCH_IPV6_CSUM
183 static __inline__ __sum16
csum_ipv6_magic(const struct in6_addr
*saddr
,
184 const struct in6_addr
*daddr
,
185 __u32 len
, __u8 proto
,
191 " .set push # csum_ipv6_magic\n"
194 " addu %0, %5 # proto (long in network byte order)\n"
195 " sltu $1, %0, %5 \n"
198 " addu %0, %6 # csum\n"
199 " sltu $1, %0, %6 \n"
200 " lw %1, 0(%2) # four words source address\n"
203 " sltu $1, %0, %1 \n"
208 " sltu $1, %0, %1 \n"
213 " sltu $1, %0, %1 \n"
218 " sltu $1, %0, %1 \n"
223 " sltu $1, %0, %1 \n"
228 " sltu $1, %0, %1 \n"
233 " sltu $1, %0, %1 \n"
238 " sltu $1, %0, %1 \n"
240 " addu %0, $1 # Add final carry\n"
242 : "=&r" (sum
), "=&r" (tmp
)
243 : "r" (saddr
), "r" (daddr
),
244 "0" (htonl(len
)), "r" (htonl(proto
)), "r" (sum
)
247 return csum_fold(sum
);
250 #include <asm-generic/checksum.h>
251 #endif /* CONFIG_GENERIC_CSUM */
253 #endif /* _ASM_CHECKSUM_H */