xref: /openbmc/linux/arch/s390/include/asm/checksum.h (revision f4356947)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *    S390 fast network checksum routines
4  *
5  *  S390 version
6  *    Copyright IBM Corp. 1999
7  *    Author(s): Ulrich Hild        (first version)
8  *               Martin Schwidefsky (heavily optimized CKSM version)
9  *               D.J. Barrow        (third attempt)
10  */
11 
12 #ifndef _S390_CHECKSUM_H
13 #define _S390_CHECKSUM_H
14 
15 #include <linux/kasan-checks.h>
16 #include <linux/in6.h>
17 
18 /*
19  * Computes the checksum of a memory block at buff, length len,
20  * and adds in "sum" (32-bit).
21  *
22  * Returns a 32-bit number suitable for feeding into itself
23  * or csum_tcpudp_magic.
24  *
25  * This function must be called with even lengths, except
26  * for the last fragment, which may be odd.
27  *
28  * It's best to have buff aligned on a 32-bit boundary.
29  */
30 static inline __wsum csum_partial(const void *buff, int len, __wsum sum)
31 {
32 	union register_pair rp = {
33 		.even = (unsigned long) buff,
34 		.odd = (unsigned long) len,
35 	};
36 
37 	kasan_check_read(buff, len);
38 	asm volatile(
39 		"0:	cksm	%[sum],%[rp]\n"
40 		"	jo	0b\n"
41 		: [sum] "+&d" (sum), [rp] "+&d" (rp.pair) : : "cc", "memory");
42 	return sum;
43 }
44 
45 /*
46  * Fold a partial checksum without adding pseudo headers.
47  */
48 static inline __sum16 csum_fold(__wsum sum)
49 {
50 	u32 csum = (__force u32) sum;
51 
52 	csum += (csum >> 16) | (csum << 16);
53 	csum >>= 16;
54 	return (__force __sum16) ~csum;
55 }
56 
57 /*
58  * This is a version of ip_compute_csum() optimized for IP headers,
59  * which always checksums on 4 octet boundaries.
60  */
61 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
62 {
63 	__u64 csum = 0;
64 	__u32 *ptr = (u32 *)iph;
65 
66 	csum += *ptr++;
67 	csum += *ptr++;
68 	csum += *ptr++;
69 	csum += *ptr++;
70 	ihl -= 4;
71 	while (ihl--)
72 		csum += *ptr++;
73 	csum += (csum >> 32) | (csum << 32);
74 	return csum_fold((__force __wsum)(csum >> 32));
75 }
76 
77 /*
78  * Computes the checksum of the TCP/UDP pseudo-header.
79  * Returns a 32-bit checksum.
80  */
81 static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
82 					__u8 proto, __wsum sum)
83 {
84 	__u64 csum = (__force __u64)sum;
85 
86 	csum += (__force __u32)saddr;
87 	csum += (__force __u32)daddr;
88 	csum += len;
89 	csum += proto;
90 	csum += (csum >> 32) | (csum << 32);
91 	return (__force __wsum)(csum >> 32);
92 }
93 
94 /*
95  * Computes the checksum of the TCP/UDP pseudo-header.
96  * Returns a 16-bit checksum, already complemented.
97  */
98 static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
99 					__u8 proto, __wsum sum)
100 {
101 	return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
102 }
103 
104 /*
105  * Used for miscellaneous IP-like checksums, mainly icmp.
106  */
107 static inline __sum16 ip_compute_csum(const void *buff, int len)
108 {
109 	return csum_fold(csum_partial(buff, len, 0));
110 }
111 
112 #define _HAVE_ARCH_IPV6_CSUM
113 static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
114 				      const struct in6_addr *daddr,
115 				      __u32 len, __u8 proto, __wsum csum)
116 {
117 	__u64 sum = (__force __u64)csum;
118 
119 	sum += (__force __u32)saddr->s6_addr32[0];
120 	sum += (__force __u32)saddr->s6_addr32[1];
121 	sum += (__force __u32)saddr->s6_addr32[2];
122 	sum += (__force __u32)saddr->s6_addr32[3];
123 	sum += (__force __u32)daddr->s6_addr32[0];
124 	sum += (__force __u32)daddr->s6_addr32[1];
125 	sum += (__force __u32)daddr->s6_addr32[2];
126 	sum += (__force __u32)daddr->s6_addr32[3];
127 	sum += len;
128 	sum += proto;
129 	sum += (sum >> 32) | (sum << 32);
130 	return csum_fold((__force __wsum)(sum >> 32));
131 }
132 
133 #endif /* _S390_CHECKSUM_H */
134