/* SPDX-License-Identifier: GPL-2.0 */1#ifndef _ASM_X86_CHECKSUM_64_H2#define _ASM_X86_CHECKSUM_64_H34/*5* Checksums for x86-646* Copyright 2002 by Andi Kleen, SuSE Labs7* with some code from asm-x86/checksum.h8*/910#include <linux/compiler.h>11#include <linux/in6.h>12#include <asm/byteorder.h>1314/**15* csum_fold - Fold and invert a 32bit checksum.16* sum: 32bit unfolded sum17*18* Fold a 32bit running checksum to 16bit and invert it. This is usually19* the last step before putting a checksum into a packet.20* Make sure not to mix with 64bit checksums.21*/22static inline __sum16 csum_fold(__wsum sum)23{24asm(" addl %1,%0\n"25" adcl $0xffff,%0"26: "=r" (sum)27: "r" ((__force u32)sum << 16),28"0" ((__force u32)sum & 0xffff0000));29return (__force __sum16)(~(__force u32)sum >> 16);30}3132/*33* This is a version of ip_compute_csum() optimized for IP headers,34* which always checksum on 4 octet boundaries.35*36* By Jorge Cwik <[email protected]>, adapted for linux by37* Arnt Gulbrandsen.38*/3940/**41* ip_fast_csum - Compute the IPv4 header checksum efficiently.42* iph: ipv4 header43* ihl: length of header / 444*/45static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)46{47unsigned int sum;4849asm(" movl (%1), %0\n"50" subl $4, %2\n"51" jbe 2f\n"52" addl 4(%1), %0\n"53" adcl 8(%1), %0\n"54" adcl 12(%1), %0\n"55"1: adcl 16(%1), %0\n"56" lea 4(%1), %1\n"57" decl %2\n"58" jne 1b\n"59" adcl $0, %0\n"60" movl %0, %2\n"61" shrl $16, %0\n"62" addw %w2, %w0\n"63" adcl $0, %0\n"64" notl %0\n"65"2:"66/* Since the input registers which are loaded with iph and ihl67are modified, we must also specify them as outputs, or gcc68will assume they contain their original values. */69: "=r" (sum), "=r" (iph), "=r" (ihl)70: "1" (iph), "2" (ihl)71: "memory");72return (__force __sum16)sum;73}7475/**76* csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.77* @saddr: source address78* @daddr: destination address79* @len: length of packet80* @proto: ip protocol of packet81* @sum: initial sum to be added in (32bit unfolded)82*83* Returns the pseudo header checksum the input data. Result is84* 32bit unfolded.85*/86static inline __wsum87csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,88__u8 proto, __wsum sum)89{90asm(" addl %1, %0\n"91" adcl %2, %0\n"92" adcl %3, %0\n"93" adcl $0, %0\n"94: "=r" (sum)95: "g" (daddr), "g" (saddr),96"g" ((len + proto)<<8), "0" (sum));97return sum;98}99100101/**102* csum_tcpup_magic - Compute an IPv4 pseudo header checksum.103* @saddr: source address104* @daddr: destination address105* @len: length of packet106* @proto: ip protocol of packet107* @sum: initial sum to be added in (32bit unfolded)108*109* Returns the 16bit pseudo header checksum the input data already110* complemented and ready to be filled in.111*/112static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,113__u32 len, __u8 proto,114__wsum sum)115{116return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));117}118119/**120* csum_partial - Compute an internet checksum.121* @buff: buffer to be checksummed122* @len: length of buffer.123* @sum: initial sum to be added in (32bit unfolded)124*125* Returns the 32bit unfolded internet checksum of the buffer.126* Before filling it in it needs to be csum_fold()'ed.127* buff should be aligned to a 64bit boundary if possible.128*/129extern __wsum csum_partial(const void *buff, int len, __wsum sum);130131/* Do not call this directly. Use the wrappers below */132extern __visible __wsum csum_partial_copy_generic(const void *src, void *dst, int len);133134extern __wsum csum_and_copy_from_user(const void __user *src, void *dst, int len);135extern __wsum csum_and_copy_to_user(const void *src, void __user *dst, int len);136extern __wsum csum_partial_copy_nocheck(const void *src, void *dst, int len);137138/**139* ip_compute_csum - Compute an 16bit IP checksum.140* @buff: buffer address.141* @len: length of buffer.142*143* Returns the 16bit folded/inverted checksum of the passed buffer.144* Ready to fill in.145*/146extern __sum16 ip_compute_csum(const void *buff, int len);147148static inline unsigned add32_with_carry(unsigned a, unsigned b)149{150asm("addl %2,%0\n\t"151"adcl $0,%0"152: "=r" (a)153: "0" (a), "rm" (b));154return a;155}156157#define _HAVE_ARCH_IPV6_CSUM 1158159/**160* csum_ipv6_magic - Compute checksum of an IPv6 pseudo header.161* @saddr: source address162* @daddr: destination address163* @len: length of packet164* @proto: protocol of packet165* @sum: initial sum (32bit unfolded) to be added in166*167* Computes an IPv6 pseudo header checksum. This sum is added the checksum168* into UDP/TCP packets and contains some link layer information.169* Returns the unfolded 32bit checksum.170*/171172static inline __sum16 csum_ipv6_magic(173const struct in6_addr *_saddr, const struct in6_addr *_daddr,174__u32 len, __u8 proto, __wsum sum)175{176const unsigned long *saddr = (const unsigned long *)_saddr;177const unsigned long *daddr = (const unsigned long *)_daddr;178__u64 sum64;179180sum64 = (__force __u64)htonl(len) + (__force __u64)htons(proto) +181(__force __u64)sum;182183asm(" addq %1,%[sum64]\n"184" adcq %2,%[sum64]\n"185" adcq %3,%[sum64]\n"186" adcq %4,%[sum64]\n"187" adcq $0,%[sum64]\n"188189: [sum64] "+r" (sum64)190: "m" (saddr[0]), "m" (saddr[1]),191"m" (daddr[0]), "m" (daddr[1]));192193return csum_fold(194(__force __wsum)add32_with_carry(sum64 & 0xffffffff, sum64>>32));195}196197#define HAVE_ARCH_CSUM_ADD198static inline __wsum csum_add(__wsum csum, __wsum addend)199{200return (__force __wsum)add32_with_carry((__force unsigned)csum,201(__force unsigned)addend);202}203204#endif /* _ASM_X86_CHECKSUM_64_H */205206207