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