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/uaccess.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 31 csum_partial(const void *buff, int len, __wsum sum) 32 { 33 register unsigned long reg2 asm("2") = (unsigned long) buff; 34 register unsigned long reg3 asm("3") = (unsigned long) len; 35 36 asm volatile( 37 "0: cksm %0,%1\n" /* do checksum on longs */ 38 " jo 0b\n" 39 : "+d" (sum), "+d" (reg2), "+d" (reg3) : : "cc", "memory"); 40 return sum; 41 } 42 43 static inline __wsum 44 csum_partial_copy_nocheck (const void *src, void *dst, int len, __wsum sum) 45 { 46 memcpy(dst,src,len); 47 return csum_partial(dst, len, sum); 48 } 49 50 /* 51 * Fold a partial checksum without adding pseudo headers 52 */ 53 static inline __sum16 csum_fold(__wsum sum) 54 { 55 u32 csum = (__force u32) sum; 56 57 csum += (csum >> 16) + (csum << 16); 58 csum >>= 16; 59 return (__force __sum16) ~csum; 60 } 61 62 /* 63 * This is a version of ip_compute_csum() optimized for IP headers, 64 * which always checksum on 4 octet boundaries. 65 * 66 */ 67 static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl) 68 { 69 return csum_fold(csum_partial(iph, ihl*4, 0)); 70 } 71 72 /* 73 * computes the checksum of the TCP/UDP pseudo-header 74 * returns a 32-bit checksum 75 */ 76 static inline __wsum 77 csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len, __u8 proto, 78 __wsum sum) 79 { 80 __u32 csum = (__force __u32)sum; 81 82 csum += (__force __u32)saddr; 83 if (csum < (__force __u32)saddr) 84 csum++; 85 86 csum += (__force __u32)daddr; 87 if (csum < (__force __u32)daddr) 88 csum++; 89 90 csum += len + proto; 91 if (csum < len + proto) 92 csum++; 93 94 return (__force __wsum)csum; 95 } 96 97 /* 98 * computes the checksum of the TCP/UDP pseudo-header 99 * returns a 16-bit checksum, already complemented 100 */ 101 102 static inline __sum16 103 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len, __u8 proto, 104 __wsum sum) 105 { 106 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum)); 107 } 108 109 /* 110 * this routine is used for miscellaneous IP-like checksums, mainly 111 * in icmp.c 112 */ 113 114 static inline __sum16 ip_compute_csum(const void *buff, int len) 115 { 116 return csum_fold(csum_partial(buff, len, 0)); 117 } 118 119 #define _HAVE_ARCH_IPV6_CSUM 120 static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr, 121 const struct in6_addr *daddr, 122 __u32 len, __u8 proto, __wsum csum) 123 { 124 __u64 sum = (__force __u64)csum; 125 126 sum += (__force __u32)saddr->s6_addr32[0]; 127 sum += (__force __u32)saddr->s6_addr32[1]; 128 sum += (__force __u32)saddr->s6_addr32[2]; 129 sum += (__force __u32)saddr->s6_addr32[3]; 130 sum += (__force __u32)daddr->s6_addr32[0]; 131 sum += (__force __u32)daddr->s6_addr32[1]; 132 sum += (__force __u32)daddr->s6_addr32[2]; 133 sum += (__force __u32)daddr->s6_addr32[3]; 134 sum += len; 135 sum += proto; 136 sum += (sum >> 32) | (sum << 32); 137 return csum_fold((__force __wsum)(sum >> 32)); 138 } 139 140 #endif /* _S390_CHECKSUM_H */ 141