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