1 #ifndef __UM_CHECKSUM_H
2 #define __UM_CHECKSUM_H
4 #include <linux/string.h>
8 * computes the checksum of a memory block at buff, length len,
9 * and adds in "sum" (32-bit)
11 * returns a 32-bit number suitable for feeding into itself
12 * or csum_tcpudp_magic
14 * this function must be called with even lengths, except
15 * for the last fragment, which may be odd
17 * it's best to have buff aligned on a 32-bit boundary
19 extern __wsum
csum_partial(const void *buff
, int len
, __wsum sum
);
22 * Note: when you get a NULL pointer exception here this means someone
23 * passed in an incorrect kernel address to one of these functions.
25 * If you use these functions directly please don't forget the
30 __wsum
csum_partial_copy_nocheck(const void *src
, void *dst
,
33 memcpy(dst
, src
, len
);
34 return csum_partial(dst
, len
, sum
);
38 * the same as csum_partial, but copies from src while it
39 * checksums, and handles user-space pointer exceptions correctly, when needed.
41 * here even more important to align src and dst on a 32-bit (or even
42 * better 64-bit) boundary
46 __wsum
csum_partial_copy_from_user(const void __user
*src
, void *dst
,
47 int len
, __wsum sum
, int *err_ptr
)
49 if (copy_from_user(dst
, src
, len
)) {
51 return (__force __wsum
)-1;
54 return csum_partial(dst
, len
, sum
);
58 * csum_fold - Fold and invert a 32bit checksum.
59 * sum: 32bit unfolded sum
61 * Fold a 32bit running checksum to 16bit and invert it. This is usually
62 * the last step before putting a checksum into a packet.
63 * Make sure not to mix with 64bit checksums.
65 static inline __sum16
csum_fold(__wsum sum
)
71 : "r" ((__force u32
)sum
<< 16),
72 "0" ((__force u32
)sum
& 0xffff0000)
74 return (__force __sum16
)(~(__force u32
)sum
>> 16);
78 * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
79 * @saddr: source address
80 * @daddr: destination address
81 * @len: length of packet
82 * @proto: ip protocol of packet
83 * @sum: initial sum to be added in (32bit unfolded)
85 * Returns the pseudo header checksum the input data. Result is
89 csum_tcpudp_nofold(__be32 saddr
, __be32 daddr
, unsigned short len
,
90 unsigned short proto
, __wsum sum
)
97 : "g" (daddr
), "g" (saddr
), "g" ((len
+ proto
) << 8), "0" (sum
));
102 * computes the checksum of the TCP/UDP pseudo-header
103 * returns a 16-bit checksum, already complemented
105 static inline __sum16
csum_tcpudp_magic(__be32 saddr
, __be32 daddr
,
107 unsigned short proto
,
110 return csum_fold(csum_tcpudp_nofold(saddr
,daddr
,len
,proto
,sum
));
114 * ip_fast_csum - Compute the IPv4 header checksum efficiently.
116 * ihl: length of header / 4
118 static inline __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
)
122 asm( " movl (%1), %0\n"
128 "1: adcl 16(%1), %0\n"
139 /* Since the input registers which are loaded with iph and ipl
140 are modified, we must also specify them as outputs, or gcc
141 will assume they contain their original values. */
142 : "=r" (sum
), "=r" (iph
), "=r" (ihl
)
143 : "1" (iph
), "2" (ihl
)
145 return (__force __sum16
)sum
;
149 # include "checksum_32.h"
151 # include "checksum_64.h"