xref: /linux/arch/arm/include/asm/checksum.h (revision cc44c17baf7f3f833d36b2f2a1edb1cc0b6f2cc4)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  arch/arm/include/asm/checksum.h
4  *
5  * IP checksum routines
6  *
7  * Copyright (C) Original authors of ../asm-i386/checksum.h
8  * Copyright (C) 1996-1999 Russell King
9  */
10 #ifndef __ASM_ARM_CHECKSUM_H
11 #define __ASM_ARM_CHECKSUM_H
12 
13 #include <linux/in6.h>
14 
15 /*
16  * computes the checksum of a memory block at buff, length len,
17  * and adds in "sum" (32-bit)
18  *
19  * returns a 32-bit number suitable for feeding into itself
20  * or csum_tcpudp_magic
21  *
22  * this function must be called with even lengths, except
23  * for the last fragment, which may be odd
24  *
25  * it's best to have buff aligned on a 32-bit boundary
26  */
27 __wsum csum_partial(const void *buff, int len, __wsum sum);
28 
29 /*
30  * the same as csum_partial, but copies from src while it
31  * checksums, and handles user-space pointer exceptions correctly, when needed.
32  *
33  * here even more important to align src and dst on a 32-bit (or even
34  * better 64-bit) boundary
35  */
36 
37 __wsum
38 csum_partial_copy_nocheck(const void *src, void *dst, int len);
39 
40 __wsum
41 csum_partial_copy_from_user(const void __user *src, void *dst, int len, __wsum sum, int *err_ptr);
42 
43 #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
44 #define _HAVE_ARCH_CSUM_AND_COPY
45 static inline
46 __wsum csum_and_copy_from_user (const void __user *src, void *dst,
47 				      int len, __wsum sum, int *err_ptr)
48 {
49 	if (access_ok(src, len))
50 		return csum_partial_copy_from_user(src, dst, len, sum, err_ptr);
51 
52 	if (len)
53 		*err_ptr = -EFAULT;
54 
55 	return sum;
56 }
57 
58 /*
59  * 	Fold a partial checksum without adding pseudo headers
60  */
61 static inline __sum16 csum_fold(__wsum sum)
62 {
63 	__asm__(
64 	"add	%0, %1, %1, ror #16	@ csum_fold"
65 	: "=r" (sum)
66 	: "r" (sum)
67 	: "cc");
68 	return (__force __sum16)(~(__force u32)sum >> 16);
69 }
70 
71 /*
72  *	This is a version of ip_compute_csum() optimized for IP headers,
73  *	which always checksum on 4 octet boundaries.
74  */
75 static inline __sum16
76 ip_fast_csum(const void *iph, unsigned int ihl)
77 {
78 	unsigned int tmp1;
79 	__wsum sum;
80 
81 	__asm__ __volatile__(
82 	"ldr	%0, [%1], #4		@ ip_fast_csum		\n\
83 	ldr	%3, [%1], #4					\n\
84 	sub	%2, %2, #5					\n\
85 	adds	%0, %0, %3					\n\
86 	ldr	%3, [%1], #4					\n\
87 	adcs	%0, %0, %3					\n\
88 	ldr	%3, [%1], #4					\n\
89 1:	adcs	%0, %0, %3					\n\
90 	ldr	%3, [%1], #4					\n\
91 	tst	%2, #15			@ do this carefully	\n\
92 	subne	%2, %2, #1		@ without destroying	\n\
93 	bne	1b			@ the carry flag	\n\
94 	adcs	%0, %0, %3					\n\
95 	adc	%0, %0, #0"
96 	: "=r" (sum), "=r" (iph), "=r" (ihl), "=r" (tmp1)
97 	: "1" (iph), "2" (ihl)
98 	: "cc", "memory");
99 	return csum_fold(sum);
100 }
101 
102 static inline __wsum
103 csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
104 		   __u8 proto, __wsum sum)
105 {
106 	u32 lenprot = len + proto;
107 	if (__builtin_constant_p(sum) && sum == 0) {
108 		__asm__(
109 		"adds	%0, %1, %2	@ csum_tcpudp_nofold0	\n\t"
110 #ifdef __ARMEB__
111 		"adcs	%0, %0, %3				\n\t"
112 #else
113 		"adcs	%0, %0, %3, ror #8			\n\t"
114 #endif
115 		"adc	%0, %0, #0"
116 		: "=&r" (sum)
117 		: "r" (daddr), "r" (saddr), "r" (lenprot)
118 		: "cc");
119 	} else {
120 		__asm__(
121 		"adds	%0, %1, %2	@ csum_tcpudp_nofold	\n\t"
122 		"adcs	%0, %0, %3				\n\t"
123 #ifdef __ARMEB__
124 		"adcs	%0, %0, %4				\n\t"
125 #else
126 		"adcs	%0, %0, %4, ror #8			\n\t"
127 #endif
128 		"adc	%0, %0, #0"
129 		: "=&r"(sum)
130 		: "r" (sum), "r" (daddr), "r" (saddr), "r" (lenprot)
131 		: "cc");
132 	}
133 	return sum;
134 }
135 /*
136  * computes the checksum of the TCP/UDP pseudo-header
137  * returns a 16-bit checksum, already complemented
138  */
139 static inline __sum16
140 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
141 		  __u8 proto, __wsum sum)
142 {
143 	return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
144 }
145 
146 
147 /*
148  * this routine is used for miscellaneous IP-like checksums, mainly
149  * in icmp.c
150  */
151 static inline __sum16
152 ip_compute_csum(const void *buff, int len)
153 {
154 	return csum_fold(csum_partial(buff, len, 0));
155 }
156 
157 #define _HAVE_ARCH_IPV6_CSUM
158 extern __wsum
159 __csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __be32 len,
160 		__be32 proto, __wsum sum);
161 
162 static inline __sum16
163 csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr,
164 		__u32 len, __u8 proto, __wsum sum)
165 {
166 	return csum_fold(__csum_ipv6_magic(saddr, daddr, htonl(len),
167 					   htonl(proto), sum));
168 }
169 #endif
170