2 * Licensed under the GPL
5 #ifndef __UM_SYSDEP_CHECKSUM_H
6 #define __UM_SYSDEP_CHECKSUM_H
9 #include "linux/string.h"
12 * computes the checksum of a memory block at buff, length len,
13 * and adds in "sum" (32-bit)
15 * returns a 32-bit number suitable for feeding into itself
16 * or csum_tcpudp_magic
18 * this function must be called with even lengths, except
19 * for the last fragment, which may be odd
21 * it's best to have buff aligned on a 32-bit boundary
23 unsigned int csum_partial(const unsigned char * buff
, int len
,
27 * Note: when you get a NULL pointer exception here this means someone
28 * passed in an incorrect kernel address to one of these functions.
30 * If you use these functions directly please don't forget the
35 unsigned int csum_partial_copy_nocheck(const unsigned char *src
, unsigned char *dst
,
38 memcpy(dst
, src
, len
);
39 return csum_partial(dst
, len
, sum
);
43 * the same as csum_partial, but copies from src while it
44 * checksums, and handles user-space pointer exceptions correctly, when needed.
46 * here even more important to align src and dst on a 32-bit (or even
47 * better 64-bit) boundary
51 unsigned int csum_partial_copy_from_user(const unsigned char *src
, unsigned char *dst
,
52 int len
, int sum
, int *err_ptr
)
54 if(copy_from_user(dst
, src
, len
)){
59 return csum_partial(dst
, len
, sum
);
63 * These are the old (and unsafe) way of doing checksums, a warning message
64 * will be printed if they are used and an exception occurs.
66 * these functions should go away after some time.
69 #define csum_partial_copy_fromuser csum_partial_copy_from_user
72 * This is a version of ip_compute_csum() optimized for IP headers,
73 * which always checksum on 4 octet boundaries.
75 * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
78 static inline unsigned short ip_fast_csum(unsigned char * iph
,
90 "1: adcl 16(%1), %0 ;\n"
101 /* Since the input registers which are loaded with iph and ipl
102 are modified, we must also specify them as outputs, or gcc
103 will assume they contain their original values. */
104 : "=r" (sum
), "=r" (iph
), "=r" (ihl
)
105 : "1" (iph
), "2" (ihl
)
111 * Fold a partial checksum
114 static inline unsigned int csum_fold(unsigned int sum
)
118 "adcl $0xffff, %0 ;\n"
120 : "r" (sum
<< 16), "0" (sum
& 0xffff0000)
125 static inline unsigned long csum_tcpudp_nofold(unsigned long saddr
,
128 unsigned short proto
,
137 : "g" (daddr
), "g"(saddr
), "g"((ntohs(len
)<<16)+proto
*256), "0"(sum
));
142 * computes the checksum of the TCP/UDP pseudo-header
143 * returns a 16-bit checksum, already complemented
145 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr
,
148 unsigned short proto
,
151 return csum_fold(csum_tcpudp_nofold(saddr
,daddr
,len
,proto
,sum
));
155 * this routine is used for miscellaneous IP-like checksums, mainly
159 static inline unsigned short ip_compute_csum(unsigned char * buff
, int len
)
161 return csum_fold (csum_partial(buff
, len
, 0));
164 #define _HAVE_ARCH_IPV6_CSUM
165 static __inline__
unsigned short int csum_ipv6_magic(struct in6_addr
*saddr
,
166 struct in6_addr
*daddr
,
168 unsigned short proto
,
175 "adcl 12(%1), %0 ;\n"
179 "adcl 12(%2), %0 ;\n"
184 : "r" (saddr
), "r" (daddr
),
185 "r"(htonl(len
)), "r"(htonl(proto
)), "0"(sum
));
187 return csum_fold(sum
);
191 * Copy and checksum to user
193 #define HAVE_CSUM_COPY_USER
194 static __inline__
unsigned int csum_and_copy_to_user(const unsigned char *src
,
196 int len
, int sum
, int *err_ptr
)
198 if (access_ok(VERIFY_WRITE
, dst
, len
)){
199 if(copy_to_user(dst
, src
, len
)){
204 return csum_partial(src
, len
, sum
);
210 return -1; /* invalid checksum */
216 * Overrides for Emacs so that we follow Linus's tabbing style.
217 * Emacs will notice this stuff at the end of the file and automatically
218 * adjust the settings for this buffer only. This must remain at the end
220 * ---------------------------------------------------------------------------
222 * c-file-style: "linux"