^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) #ifndef __UM_CHECKSUM_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define __UM_CHECKSUM_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * computes the checksum of a memory block at buff, length len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * and adds in "sum" (32-bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * returns a 32-bit number suitable for feeding into itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * or csum_tcpudp_magic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * this function must be called with even lengths, except
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * for the last fragment, which may be odd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * it's best to have buff aligned on a 32-bit boundary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) extern __wsum csum_partial(const void *buff, int len, __wsum sum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * csum_fold - Fold and invert a 32bit checksum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * sum: 32bit unfolded sum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Fold a 32bit running checksum to 16bit and invert it. This is usually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * the last step before putting a checksum into a packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * Make sure not to mix with 64bit checksums.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static inline __sum16 csum_fold(__wsum sum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) __asm__(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) " addl %1,%0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) " adcl $0xffff,%0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) : "=r" (sum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) : "r" ((__force u32)sum << 16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) "0" ((__force u32)sum & 0xffff0000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return (__force __sum16)(~(__force u32)sum >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * @saddr: source address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * @daddr: destination address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * @len: length of packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * @proto: ip protocol of packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @sum: initial sum to be added in (32bit unfolded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * Returns the pseudo header checksum the input data. Result is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * 32bit unfolded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static inline __wsum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) __u8 proto, __wsum sum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) asm(" addl %1, %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) " adcl %2, %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) " adcl %3, %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) " adcl $0, %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) : "=r" (sum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) : "g" (daddr), "g" (saddr), "g" ((len + proto) << 8), "0" (sum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * computes the checksum of the TCP/UDP pseudo-header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * returns a 16-bit checksum, already complemented
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) __u32 len, __u8 proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) __wsum sum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * ip_fast_csum - Compute the IPv4 header checksum efficiently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * iph: ipv4 header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * ihl: length of header / 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) unsigned int sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) asm( " movl (%1), %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) " subl $4, %2\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) " jbe 2f\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) " addl 4(%1), %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) " adcl 8(%1), %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) " adcl 12(%1), %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) "1: adcl 16(%1), %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) " lea 4(%1), %1\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) " decl %2\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) " jne 1b\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) " adcl $0, %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) " movl %0, %2\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) " shrl $16, %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) " addw %w2, %w0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) " adcl $0, %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) " notl %0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) "2:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* Since the input registers which are loaded with iph and ipl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) are modified, we must also specify them as outputs, or gcc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) will assume they contain their original values. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) : "=r" (sum), "=r" (iph), "=r" (ihl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) : "1" (iph), "2" (ihl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) : "memory");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return (__force __sum16)sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #ifdef CONFIG_X86_32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) # include "checksum_32.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) # include "checksum_64.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #endif