Path: blob/master/arch/um/sys-x86_64/shared/sysdep/checksum.h
10820 views
/*1* Licensed under the GPL2*/34#ifndef __UM_SYSDEP_CHECKSUM_H5#define __UM_SYSDEP_CHECKSUM_H67#include "linux/string.h"8#include "linux/in6.h"9#include "asm/uaccess.h"1011extern __wsum csum_partial(const void *buff, int len, __wsum sum);1213/*14* Note: when you get a NULL pointer exception here this means someone15* passed in an incorrect kernel address to one of these functions.16*17* If you use these functions directly please don't forget the18* access_ok().19*/2021static __inline__22__wsum csum_partial_copy_nocheck(const void *src, void *dst,23int len, __wsum sum)24{25memcpy(dst, src, len);26return(csum_partial(dst, len, sum));27}2829static __inline__30__wsum csum_partial_copy_from_user(const void __user *src,31void *dst, int len, __wsum sum,32int *err_ptr)33{34if (copy_from_user(dst, src, len)) {35*err_ptr = -EFAULT;36return (__force __wsum)-1;37}38return csum_partial(dst, len, sum);39}4041/**42* csum_fold - Fold and invert a 32bit checksum.43* sum: 32bit unfolded sum44*45* Fold a 32bit running checksum to 16bit and invert it. This is usually46* the last step before putting a checksum into a packet.47* Make sure not to mix with 64bit checksums.48*/49static inline __sum16 csum_fold(__wsum sum)50{51__asm__(52" addl %1,%0\n"53" adcl $0xffff,%0"54: "=r" (sum)55: "r" ((__force u32)sum << 16),56"0" ((__force u32)sum & 0xffff0000)57);58return (__force __sum16)(~(__force u32)sum >> 16);59}6061/**62* csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.63* @saddr: source address64* @daddr: destination address65* @len: length of packet66* @proto: ip protocol of packet67* @sum: initial sum to be added in (32bit unfolded)68*69* Returns the pseudo header checksum the input data. Result is70* 32bit unfolded.71*/72static inline __wsum73csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len,74unsigned short proto, __wsum sum)75{76asm(" addl %1, %0\n"77" adcl %2, %0\n"78" adcl %3, %0\n"79" adcl $0, %0\n"80: "=r" (sum)81: "g" (daddr), "g" (saddr), "g" ((len + proto) << 8), "0" (sum));82return sum;83}8485/*86* computes the checksum of the TCP/UDP pseudo-header87* returns a 16-bit checksum, already complemented88*/89static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,90unsigned short len,91unsigned short proto,92__wsum sum)93{94return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));95}9697/**98* ip_fast_csum - Compute the IPv4 header checksum efficiently.99* iph: ipv4 header100* ihl: length of header / 4101*/102static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)103{104unsigned int sum;105106asm( " movl (%1), %0\n"107" subl $4, %2\n"108" jbe 2f\n"109" addl 4(%1), %0\n"110" adcl 8(%1), %0\n"111" adcl 12(%1), %0\n"112"1: adcl 16(%1), %0\n"113" lea 4(%1), %1\n"114" decl %2\n"115" jne 1b\n"116" adcl $0, %0\n"117" movl %0, %2\n"118" shrl $16, %0\n"119" addw %w2, %w0\n"120" adcl $0, %0\n"121" notl %0\n"122"2:"123/* Since the input registers which are loaded with iph and ipl124are modified, we must also specify them as outputs, or gcc125will assume they contain their original values. */126: "=r" (sum), "=r" (iph), "=r" (ihl)127: "1" (iph), "2" (ihl)128: "memory");129return (__force __sum16)sum;130}131132static inline unsigned add32_with_carry(unsigned a, unsigned b)133{134asm("addl %2,%0\n\t"135"adcl $0,%0"136: "=r" (a)137: "0" (a), "r" (b));138return a;139}140141extern __sum16 ip_compute_csum(const void *buff, int len);142143#endif144145146