xref: /linux/include/net/checksum.h (revision cc44c17baf7f3f833d36b2f2a1edb1cc0b6f2cc4)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4  *		operating system.  INET is implemented using the  BSD Socket
5  *		interface as the means of communication with the user level.
6  *
7  *		Checksumming functions for IP, TCP, UDP and so on
8  *
9  * Authors:	Jorge Cwik, <jorge@laser.satlink.net>
10  *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
11  *		Borrows very liberally from tcp.c and ip.c, see those
12  *		files for more names.
13  */
14 
15 #ifndef _CHECKSUM_H
16 #define _CHECKSUM_H
17 
18 #include <linux/errno.h>
19 #include <asm/types.h>
20 #include <asm/byteorder.h>
21 #include <linux/uaccess.h>
22 #include <asm/checksum.h>
23 
24 #ifndef _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
25 static inline
26 __wsum csum_and_copy_from_user (const void __user *src, void *dst,
27 				      int len, __wsum sum, int *err_ptr)
28 {
29 	if (copy_from_user(dst, src, len))
30 		*err_ptr = -EFAULT;
31 	return csum_partial(dst, len, sum);
32 }
33 #endif
34 
35 #ifndef HAVE_CSUM_COPY_USER
36 static __inline__ __wsum csum_and_copy_to_user
37 (const void *src, void __user *dst, int len, __wsum sum, int *err_ptr)
38 {
39 	sum = csum_partial(src, len, sum);
40 
41 	if (copy_to_user(dst, src, len) == 0)
42 		return sum;
43 	if (len)
44 		*err_ptr = -EFAULT;
45 
46 	return (__force __wsum)-1; /* invalid checksum */
47 }
48 #endif
49 
50 #ifndef _HAVE_ARCH_CSUM_AND_COPY
51 static inline __wsum
52 csum_partial_copy_nocheck(const void *src, void *dst, int len)
53 {
54 	memcpy(dst, src, len);
55 	return csum_partial(dst, len, 0);
56 }
57 #endif
58 
59 #ifndef HAVE_ARCH_CSUM_ADD
60 static inline __wsum csum_add(__wsum csum, __wsum addend)
61 {
62 	u32 res = (__force u32)csum;
63 	res += (__force u32)addend;
64 	return (__force __wsum)(res + (res < (__force u32)addend));
65 }
66 #endif
67 
68 static inline __wsum csum_sub(__wsum csum, __wsum addend)
69 {
70 	return csum_add(csum, ~addend);
71 }
72 
73 static inline __sum16 csum16_add(__sum16 csum, __be16 addend)
74 {
75 	u16 res = (__force u16)csum;
76 
77 	res += (__force u16)addend;
78 	return (__force __sum16)(res + (res < (__force u16)addend));
79 }
80 
81 static inline __sum16 csum16_sub(__sum16 csum, __be16 addend)
82 {
83 	return csum16_add(csum, ~addend);
84 }
85 
86 static inline __wsum
87 csum_block_add(__wsum csum, __wsum csum2, int offset)
88 {
89 	u32 sum = (__force u32)csum2;
90 
91 	/* rotate sum to align it with a 16b boundary */
92 	if (offset & 1)
93 		sum = ror32(sum, 8);
94 
95 	return csum_add(csum, (__force __wsum)sum);
96 }
97 
98 static inline __wsum
99 csum_block_add_ext(__wsum csum, __wsum csum2, int offset, int len)
100 {
101 	return csum_block_add(csum, csum2, offset);
102 }
103 
104 static inline __wsum
105 csum_block_sub(__wsum csum, __wsum csum2, int offset)
106 {
107 	return csum_block_add(csum, ~csum2, offset);
108 }
109 
110 static inline __wsum csum_unfold(__sum16 n)
111 {
112 	return (__force __wsum)n;
113 }
114 
115 static inline __wsum csum_partial_ext(const void *buff, int len, __wsum sum)
116 {
117 	return csum_partial(buff, len, sum);
118 }
119 
120 #define CSUM_MANGLED_0 ((__force __sum16)0xffff)
121 
122 static inline void csum_replace_by_diff(__sum16 *sum, __wsum diff)
123 {
124 	*sum = csum_fold(csum_add(diff, ~csum_unfold(*sum)));
125 }
126 
127 static inline void csum_replace4(__sum16 *sum, __be32 from, __be32 to)
128 {
129 	__wsum tmp = csum_sub(~csum_unfold(*sum), (__force __wsum)from);
130 
131 	*sum = csum_fold(csum_add(tmp, (__force __wsum)to));
132 }
133 
134 /* Implements RFC 1624 (Incremental Internet Checksum)
135  * 3. Discussion states :
136  *     HC' = ~(~HC + ~m + m')
137  *  m : old value of a 16bit field
138  *  m' : new value of a 16bit field
139  */
140 static inline void csum_replace2(__sum16 *sum, __be16 old, __be16 new)
141 {
142 	*sum = ~csum16_add(csum16_sub(~(*sum), old), new);
143 }
144 
145 struct sk_buff;
146 void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
147 			      __be32 from, __be32 to, bool pseudohdr);
148 void inet_proto_csum_replace16(__sum16 *sum, struct sk_buff *skb,
149 			       const __be32 *from, const __be32 *to,
150 			       bool pseudohdr);
151 void inet_proto_csum_replace_by_diff(__sum16 *sum, struct sk_buff *skb,
152 				     __wsum diff, bool pseudohdr);
153 
154 static inline void inet_proto_csum_replace2(__sum16 *sum, struct sk_buff *skb,
155 					    __be16 from, __be16 to,
156 					    bool pseudohdr)
157 {
158 	inet_proto_csum_replace4(sum, skb, (__force __be32)from,
159 				 (__force __be32)to, pseudohdr);
160 }
161 
162 static inline __wsum remcsum_adjust(void *ptr, __wsum csum,
163 				    int start, int offset)
164 {
165 	__sum16 *psum = (__sum16 *)(ptr + offset);
166 	__wsum delta;
167 
168 	/* Subtract out checksum up to start */
169 	csum = csum_sub(csum, csum_partial(ptr, start, 0));
170 
171 	/* Set derived checksum in packet */
172 	delta = csum_sub((__force __wsum)csum_fold(csum),
173 			 (__force __wsum)*psum);
174 	*psum = csum_fold(csum);
175 
176 	return delta;
177 }
178 
179 static inline void remcsum_unadjust(__sum16 *psum, __wsum delta)
180 {
181 	*psum = csum_fold(csum_sub(delta, (__force __wsum)*psum));
182 }
183 
184 #endif
185