Path: blob/master/arch/x86/include/asm/checksum_32.h
10821 views
#ifndef _ASM_X86_CHECKSUM_32_H1#define _ASM_X86_CHECKSUM_32_H23#include <linux/in6.h>45#include <asm/uaccess.h>67/*8* computes the checksum of a memory block at buff, length len,9* and adds in "sum" (32-bit)10*11* returns a 32-bit number suitable for feeding into itself12* or csum_tcpudp_magic13*14* this function must be called with even lengths, except15* for the last fragment, which may be odd16*17* it's best to have buff aligned on a 32-bit boundary18*/19asmlinkage __wsum csum_partial(const void *buff, int len, __wsum sum);2021/*22* the same as csum_partial, but copies from src while it23* checksums, and handles user-space pointer exceptions correctly, when needed.24*25* here even more important to align src and dst on a 32-bit (or even26* better 64-bit) boundary27*/2829asmlinkage __wsum csum_partial_copy_generic(const void *src, void *dst,30int len, __wsum sum,31int *src_err_ptr, int *dst_err_ptr);3233/*34* Note: when you get a NULL pointer exception here this means someone35* passed in an incorrect kernel address to one of these functions.36*37* If you use these functions directly please don't forget the38* access_ok().39*/40static inline __wsum csum_partial_copy_nocheck(const void *src, void *dst,41int len, __wsum sum)42{43return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);44}4546static inline __wsum csum_partial_copy_from_user(const void __user *src,47void *dst,48int len, __wsum sum,49int *err_ptr)50{51might_sleep();52return csum_partial_copy_generic((__force void *)src, dst,53len, sum, err_ptr, NULL);54}5556/*57* This is a version of ip_compute_csum() optimized for IP headers,58* which always checksum on 4 octet boundaries.59*60* By Jorge Cwik <[email protected]>, adapted for linux by61* Arnt Gulbrandsen.62*/63static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)64{65unsigned int sum;6667asm volatile("movl (%1), %0 ;\n"68"subl $4, %2 ;\n"69"jbe 2f ;\n"70"addl 4(%1), %0 ;\n"71"adcl 8(%1), %0 ;\n"72"adcl 12(%1), %0;\n"73"1: adcl 16(%1), %0 ;\n"74"lea 4(%1), %1 ;\n"75"decl %2 ;\n"76"jne 1b ;\n"77"adcl $0, %0 ;\n"78"movl %0, %2 ;\n"79"shrl $16, %0 ;\n"80"addw %w2, %w0 ;\n"81"adcl $0, %0 ;\n"82"notl %0 ;\n"83"2: ;\n"84/* Since the input registers which are loaded with iph and ihl85are modified, we must also specify them as outputs, or gcc86will assume they contain their original values. */87: "=r" (sum), "=r" (iph), "=r" (ihl)88: "1" (iph), "2" (ihl)89: "memory");90return (__force __sum16)sum;91}9293/*94* Fold a partial checksum95*/9697static inline __sum16 csum_fold(__wsum sum)98{99asm("addl %1, %0 ;\n"100"adcl $0xffff, %0 ;\n"101: "=r" (sum)102: "r" ((__force u32)sum << 16),103"0" ((__force u32)sum & 0xffff0000));104return (__force __sum16)(~(__force u32)sum >> 16);105}106107static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,108unsigned short len,109unsigned short proto,110__wsum sum)111{112asm("addl %1, %0 ;\n"113"adcl %2, %0 ;\n"114"adcl %3, %0 ;\n"115"adcl $0, %0 ;\n"116: "=r" (sum)117: "g" (daddr), "g"(saddr),118"g" ((len + proto) << 8), "0" (sum));119return sum;120}121122/*123* computes the checksum of the TCP/UDP pseudo-header124* returns a 16-bit checksum, already complemented125*/126static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,127unsigned short len,128unsigned short proto,129__wsum sum)130{131return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));132}133134/*135* this routine is used for miscellaneous IP-like checksums, mainly136* in icmp.c137*/138139static inline __sum16 ip_compute_csum(const void *buff, int len)140{141return csum_fold(csum_partial(buff, len, 0));142}143144#define _HAVE_ARCH_IPV6_CSUM145static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,146const struct in6_addr *daddr,147__u32 len, unsigned short proto,148__wsum sum)149{150asm("addl 0(%1), %0 ;\n"151"adcl 4(%1), %0 ;\n"152"adcl 8(%1), %0 ;\n"153"adcl 12(%1), %0 ;\n"154"adcl 0(%2), %0 ;\n"155"adcl 4(%2), %0 ;\n"156"adcl 8(%2), %0 ;\n"157"adcl 12(%2), %0 ;\n"158"adcl %3, %0 ;\n"159"adcl %4, %0 ;\n"160"adcl $0, %0 ;\n"161: "=&r" (sum)162: "r" (saddr), "r" (daddr),163"r" (htonl(len)), "r" (htonl(proto)), "0" (sum)164: "memory");165166return csum_fold(sum);167}168169/*170* Copy and checksum to user171*/172#define HAVE_CSUM_COPY_USER173static inline __wsum csum_and_copy_to_user(const void *src,174void __user *dst,175int len, __wsum sum,176int *err_ptr)177{178might_sleep();179if (access_ok(VERIFY_WRITE, dst, len))180return csum_partial_copy_generic(src, (__force void *)dst,181len, sum, NULL, err_ptr);182183if (len)184*err_ptr = -EFAULT;185186return (__force __wsum)-1; /* invalid checksum */187}188189#endif /* _ASM_X86_CHECKSUM_32_H */190191192