1 /* SPDX-License-Identifier: GPL-2.0 */
3 * arch/arm/include/asm/checksum.h
7 * Copyright (C) Original authors of ../asm-i386/checksum.h
8 * Copyright (C) 1996-1999 Russell King
10 #ifndef __ASM_ARM_CHECKSUM_H
11 #define __ASM_ARM_CHECKSUM_H
13 #include <linux/in6.h>
14 #include <linux/uaccess.h>
17 * computes the checksum of a memory block at buff, length len,
18 * and adds in "sum" (32-bit)
20 * returns a 32-bit number suitable for feeding into itself
21 * or csum_tcpudp_magic
23 * this function must be called with even lengths, except
24 * for the last fragment, which may be odd
26 * it's best to have buff aligned on a 32-bit boundary
28 __wsum csum_partial(const void *buff, int len, __wsum sum);
31 * the same as csum_partial, but copies from src while it
32 * checksums, and handles user-space pointer exceptions correctly, when needed.
34 * here even more important to align src and dst on a 32-bit (or even
35 * better 64-bit) boundary
39 csum_partial_copy_nocheck(const void *src, void *dst, int len);
42 csum_partial_copy_from_user(const void __user *src, void *dst, int len);
44 #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
45 #define _HAVE_ARCH_CSUM_AND_COPY
47 __wsum csum_and_copy_from_user(const void __user *src, void *dst, int len)
49 if (!access_ok(src, len))
52 return csum_partial_copy_from_user(src, dst, len);
56 * Fold a partial checksum without adding pseudo headers
58 static inline __sum16 csum_fold(__wsum sum)
61 "add %0, %1, %1, ror #16 @ csum_fold"
65 return (__force __sum16)(~(__force u32)sum >> 16);
69 * This is a version of ip_compute_csum() optimized for IP headers,
70 * which always checksum on 4 octet boundaries.
73 ip_fast_csum(const void *iph, unsigned int ihl)
79 "ldr %0, [%1], #4 @ ip_fast_csum \n\
86 1: adcs %0, %0, %3 \n\
88 tst %2, #15 @ do this carefully \n\
89 subne %2, %2, #1 @ without destroying \n\
90 bne 1b @ the carry flag \n\
93 : "=r" (sum), "=r" (iph), "=r" (ihl), "=r" (tmp1)
94 : "1" (iph), "2" (ihl)
96 return csum_fold(sum);
100 csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
101 __u8 proto, __wsum sum)
103 u32 lenprot = len + proto;
104 if (__builtin_constant_p(sum) && sum == 0) {
106 "adds %0, %1, %2 @ csum_tcpudp_nofold0 \n\t"
108 "adcs %0, %0, %3 \n\t"
110 "adcs %0, %0, %3, ror #8 \n\t"
114 : "r" (daddr), "r" (saddr), "r" (lenprot)
118 "adds %0, %1, %2 @ csum_tcpudp_nofold \n\t"
119 "adcs %0, %0, %3 \n\t"
121 "adcs %0, %0, %4 \n\t"
123 "adcs %0, %0, %4, ror #8 \n\t"
127 : "r" (sum), "r" (daddr), "r" (saddr), "r" (lenprot)
133 * computes the checksum of the TCP/UDP pseudo-header
134 * returns a 16-bit checksum, already complemented
136 static inline __sum16
137 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
138 __u8 proto, __wsum sum)
140 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
145 * this routine is used for miscellaneous IP-like checksums, mainly
148 static inline __sum16
149 ip_compute_csum(const void *buff, int len)
151 return csum_fold(csum_partial(buff, len, 0));
154 #define _HAVE_ARCH_IPV6_CSUM
156 __csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __be32 len,
157 __be32 proto, __wsum sum);
159 static inline __sum16
160 csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr,
161 __u32 len, __u8 proto, __wsum sum)
163 return csum_fold(__csum_ipv6_magic(saddr, daddr, htonl(len),