2 * Licensed under the GPL
5 #ifndef __UM_SYSDEP_CHECKSUM_H
6 #define __UM_SYSDEP_CHECKSUM_H
8 #include "linux/string.h"
11 * computes the checksum of a memory block at buff, length len,
12 * and adds in "sum" (32-bit)
14 * returns a 32-bit number suitable for feeding into itself
15 * or csum_tcpudp_magic
17 * this function must be called with even lengths, except
18 * for the last fragment, which may be odd
20 * it's best to have buff aligned on a 32-bit boundary
22 unsigned int csum_partial(const unsigned char * buff, int len,
26 * the same as csum_partial, but copies from src while it
27 * checksums, and handles user-space pointer exceptions correctly, when needed.
29 * here even more important to align src and dst on a 32-bit (or even
30 * better 64-bit) boundary
33 unsigned int csum_partial_copy_to(const char *src, char *dst, int len,
34 int sum, int *err_ptr);
35 unsigned int csum_partial_copy_from(const char *src, char *dst, int len,
36 int sum, int *err_ptr);
39 * Note: when you get a NULL pointer exception here this means someone
40 * passed in an incorrect kernel address to one of these functions.
42 * If you use these functions directly please don't forget the
47 unsigned int csum_partial_copy_nocheck(const char *src, char *dst,
50 memcpy(dst, src, len);
51 return(csum_partial(dst, len, sum));
55 unsigned int csum_partial_copy_from_user(const char *src, char *dst,
56 int len, int sum, int *err_ptr)
58 return csum_partial_copy_from(src, dst, len, sum, err_ptr);
62 * These are the old (and unsafe) way of doing checksums, a warning message
63 * will be printed if they are used and an exception occurs.
65 * these functions should go away after some time.
68 #define csum_partial_copy_fromuser csum_partial_copy_from_user
69 unsigned int csum_partial_copy( const char *src, char *dst, int len, int sum);
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));
110 * Fold a partial checksum
113 static inline unsigned int csum_fold(unsigned int sum)
117 "adcl $0xffff, %0 ;\n"
119 : "r" (sum << 16), "0" (sum & 0xffff0000)
124 static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
127 unsigned short proto,
136 : "g" (daddr), "g"(saddr), "g"((ntohs(len)<<16)+proto*256), "0"(sum));
141 * computes the checksum of the TCP/UDP pseudo-header
142 * returns a 16-bit checksum, already complemented
144 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
147 unsigned short proto,
150 return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
154 * this routine is used for miscellaneous IP-like checksums, mainly
158 static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
160 return csum_fold (csum_partial(buff, len, 0));
163 #define _HAVE_ARCH_IPV6_CSUM
164 static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
165 struct in6_addr *daddr,
167 unsigned short proto,
174 "adcl 12(%1), %0 ;\n"
178 "adcl 12(%2), %0 ;\n"
183 : "r" (saddr), "r" (daddr),
184 "r"(htonl(len)), "r"(htonl(proto)), "0"(sum));
186 return csum_fold(sum);
190 * Copy and checksum to user
192 #define HAVE_CSUM_COPY_USER
193 static __inline__ unsigned int csum_and_copy_to_user(const char *src,
195 int sum, int *err_ptr)
197 if (access_ok(VERIFY_WRITE, dst, len))
198 return(csum_partial_copy_to(src, dst, len, sum, err_ptr));
203 return -1; /* invalid checksum */
209 * Overrides for Emacs so that we follow Linus's tabbing style.
210 * Emacs will notice this stuff at the end of the file and automatically
211 * adjust the settings for this buffer only. This must remain at the end
213 * ---------------------------------------------------------------------------
215 * c-file-style: "linux"