1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * arch/alpha/lib/checksum.c 4 * 5 * This file contains network checksum routines that are better done 6 * in an architecture-specific manner due to speed.. 7 * Comments in other versions indicate that the algorithms are from RFC1071 8 * 9 * accelerated versions (and 21264 assembly versions ) contributed by 10 * Rick Gorton <rick.gorton@alpha-processor.com> 11 */ 12 13 #include <linux/module.h> 14 #include <linux/string.h> 15 #include <net/checksum.h> 16 17 #include <asm/byteorder.h> 18 19 static inline unsigned short from64to16(unsigned long x) 20 { 21 /* Using extract instructions is a bit more efficient 22 than the original shift/bitmask version. */ 23 24 union { 25 unsigned long ul; 26 unsigned int ui[2]; 27 unsigned short us[4]; 28 } in_v, tmp_v, out_v; 29 30 in_v.ul = x; 31 tmp_v.ul = (unsigned long) in_v.ui[0] + (unsigned long) in_v.ui[1]; 32 33 /* Since the bits of tmp_v.sh[3] are going to always be zero, 34 we don't have to bother to add that in. */ 35 out_v.ul = (unsigned long) tmp_v.us[0] + (unsigned long) tmp_v.us[1] 36 + (unsigned long) tmp_v.us[2]; 37 38 /* Similarly, out_v.us[2] is always zero for the final add. */ 39 return out_v.us[0] + out_v.us[1]; 40 } 41 42 /* 43 * computes the checksum of the TCP/UDP pseudo-header 44 * returns a 16-bit checksum, already complemented. 45 */ 46 __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, 47 __u32 len, __u8 proto, __wsum sum) 48 { 49 return (__force __sum16)~from64to16( 50 (__force u64)saddr + (__force u64)daddr + 51 (__force u64)sum + ((len + proto) << 8)); 52 } 53 EXPORT_SYMBOL(csum_tcpudp_magic); 54 55 __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, 56 __u32 len, __u8 proto, __wsum sum) 57 { 58 unsigned long result; 59 60 result = (__force u64)saddr + (__force u64)daddr + 61 (__force u64)sum + ((len + proto) << 8); 62 63 /* Fold down to 32-bits so we don't lose in the typedef-less 64 network stack. */ 65 /* 64 to 33 */ 66 result = (result & 0xffffffff) + (result >> 32); 67 /* 33 to 32 */ 68 result = (result & 0xffffffff) + (result >> 32); 69 return (__force __wsum)result; 70 } 71 EXPORT_SYMBOL(csum_tcpudp_nofold); 72 73 /* 74 * Do a 64-bit checksum on an arbitrary memory area.. 75 * 76 * This isn't a great routine, but it's not _horrible_ either. The 77 * inner loop could be unrolled a bit further, and there are better 78 * ways to do the carry, but this is reasonable. 79 */ 80 static inline unsigned long do_csum(const unsigned char * buff, int len) 81 { 82 int odd, count; 83 unsigned long result = 0; 84 85 if (len <= 0) 86 goto out; 87 odd = 1 & (unsigned long) buff; 88 if (odd) { 89 result = *buff << 8; 90 len--; 91 buff++; 92 } 93 count = len >> 1; /* nr of 16-bit words.. */ 94 if (count) { 95 if (2 & (unsigned long) buff) { 96 result += *(unsigned short *) buff; 97 count--; 98 len -= 2; 99 buff += 2; 100 } 101 count >>= 1; /* nr of 32-bit words.. */ 102 if (count) { 103 if (4 & (unsigned long) buff) { 104 result += *(unsigned int *) buff; 105 count--; 106 len -= 4; 107 buff += 4; 108 } 109 count >>= 1; /* nr of 64-bit words.. */ 110 if (count) { 111 unsigned long carry = 0; 112 do { 113 unsigned long w = *(unsigned long *) buff; 114 count--; 115 buff += 8; 116 result += carry; 117 result += w; 118 carry = (w > result); 119 } while (count); 120 result += carry; 121 result = (result & 0xffffffff) + (result >> 32); 122 } 123 if (len & 4) { 124 result += *(unsigned int *) buff; 125 buff += 4; 126 } 127 } 128 if (len & 2) { 129 result += *(unsigned short *) buff; 130 buff += 2; 131 } 132 } 133 if (len & 1) 134 result += *buff; 135 result = from64to16(result); 136 if (odd) 137 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8); 138 out: 139 return result; 140 } 141 142 /* 143 * This is a version of ip_compute_csum() optimized for IP headers, 144 * which always checksum on 4 octet boundaries. 145 */ 146 __sum16 ip_fast_csum(const void *iph, unsigned int ihl) 147 { 148 return (__force __sum16)~do_csum(iph,ihl*4); 149 } 150 EXPORT_SYMBOL(ip_fast_csum); 151 152 /* 153 * computes the checksum of a memory block at buff, length len, 154 * and adds in "sum" (32-bit) 155 * 156 * returns a 32-bit number suitable for feeding into itself 157 * or csum_tcpudp_magic 158 * 159 * this function must be called with even lengths, except 160 * for the last fragment, which may be odd 161 * 162 * it's best to have buff aligned on a 32-bit boundary 163 */ 164 __wsum csum_partial(const void *buff, int len, __wsum sum) 165 { 166 unsigned long result = do_csum(buff, len); 167 168 /* add in old sum, and carry.. */ 169 result += (__force u32)sum; 170 /* 32+c bits -> 32 bits */ 171 result = (result & 0xffffffff) + (result >> 32); 172 return (__force __wsum)result; 173 } 174 175 EXPORT_SYMBOL(csum_partial); 176 177 /* 178 * this routine is used for miscellaneous IP-like checksums, mainly 179 * in icmp.c 180 */ 181 __sum16 ip_compute_csum(const void *buff, int len) 182 { 183 return (__force __sum16)~from64to16(do_csum(buff,len)); 184 } 185 EXPORT_SYMBOL(ip_compute_csum); 186