1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __UM_CHECKSUM_H
3 #define __UM_CHECKSUM_H
5 #include <linux/string.h>
7 #include <linux/uaccess.h>
10 * computes the checksum of a memory block at buff, length len,
11 * and adds in "sum" (32-bit)
13 * returns a 32-bit number suitable for feeding into itself
14 * or csum_tcpudp_magic
16 * this function must be called with even lengths, except
17 * for the last fragment, which may be odd
19 * it's best to have buff aligned on a 32-bit boundary
21 extern __wsum
csum_partial(const void *buff
, int len
, __wsum sum
);
24 * csum_fold - Fold and invert a 32bit checksum.
25 * sum: 32bit unfolded sum
27 * Fold a 32bit running checksum to 16bit and invert it. This is usually
28 * the last step before putting a checksum into a packet.
29 * Make sure not to mix with 64bit checksums.
31 static inline __sum16
csum_fold(__wsum sum
)
37 : "r" ((__force u32
)sum
<< 16),
38 "0" ((__force u32
)sum
& 0xffff0000)
40 return (__force __sum16
)(~(__force u32
)sum
>> 16);
44 * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
45 * @saddr: source address
46 * @daddr: destination address
47 * @len: length of packet
48 * @proto: ip protocol of packet
49 * @sum: initial sum to be added in (32bit unfolded)
51 * Returns the pseudo header checksum the input data. Result is
55 csum_tcpudp_nofold(__be32 saddr
, __be32 daddr
, __u32 len
,
56 __u8 proto
, __wsum sum
)
63 : "g" (daddr
), "g" (saddr
), "g" ((len
+ proto
) << 8), "0" (sum
));
68 * computes the checksum of the TCP/UDP pseudo-header
69 * returns a 16-bit checksum, already complemented
71 static inline __sum16
csum_tcpudp_magic(__be32 saddr
, __be32 daddr
,
72 __u32 len
, __u8 proto
,
75 return csum_fold(csum_tcpudp_nofold(saddr
,daddr
,len
,proto
,sum
));
79 * ip_fast_csum - Compute the IPv4 header checksum efficiently.
81 * ihl: length of header / 4
83 static inline __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
)
87 asm( " movl (%1), %0\n"
93 "1: adcl 16(%1), %0\n"
104 /* Since the input registers which are loaded with iph and ipl
105 are modified, we must also specify them as outputs, or gcc
106 will assume they contain their original values. */
107 : "=r" (sum
), "=r" (iph
), "=r" (ihl
)
108 : "1" (iph
), "2" (ihl
)
110 return (__force __sum16
)sum
;
114 # include "checksum_32.h"
116 # include "checksum_64.h"