2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1995, 96, 97, 98, 99, 2001 by Ralf Baechle
7 * Copyright (C) 1999 Silicon Graphics, Inc.
8 * Copyright (C) 2001 Thiemo Seufer.
9 * Copyright (C) 2002 Maciej W. Rozycki
11 #ifndef _ASM_CHECKSUM_H
12 #define _ASM_CHECKSUM_H
14 #include <linux/in6.h>
16 #include <asm/uaccess.h>
19 * computes the checksum of a memory block at buff, length len,
20 * and adds in "sum" (32-bit)
22 * returns a 32-bit number suitable for feeding into itself
23 * or csum_tcpudp_magic
25 * this function must be called with even lengths, except
26 * for the last fragment, which may be odd
28 * it's best to have buff aligned on a 32-bit boundary
30 unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum);
33 * this is a new version of the above that records errors it finds in *errp,
34 * but continues and zeros the rest of the buffer.
36 unsigned int csum_partial_copy_from_user(const unsigned char __user *src,
37 unsigned char *dst, int len,
38 unsigned int sum, int *errp);
41 * Copy and checksum to user
43 #define HAVE_CSUM_COPY_USER
44 static inline unsigned int csum_and_copy_to_user (const unsigned char *src,
45 unsigned char __user *dst,
50 sum = csum_partial(src, len, sum);
52 if (copy_to_user(dst, src, len)) {
61 * the same as csum_partial, but copies from user space (but on MIPS
62 * we have just one address space, so this is identical to the above)
64 unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst,
65 int len, unsigned int sum);
68 * Fold a partial checksum without adding pseudo headers
70 static inline unsigned short int csum_fold(unsigned int sum)
73 " .set push # csum_fold\n"
89 * This is a version of ip_compute_csum() optimized for IP headers,
90 * which always checksum on 4 octet boundaries.
92 * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
95 static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
97 unsigned int *word = (unsigned int *) iph;
98 unsigned int *stop = word + ihl;
104 carry = (csum < word[1]);
108 carry = (csum < word[2]);
112 carry = (csum < word[3]);
118 carry = (csum < *word);
121 } while (word != stop);
123 return csum_fold(csum);
126 static inline unsigned int csum_tcpudp_nofold(unsigned long saddr,
127 unsigned long daddr, unsigned short len, unsigned short proto,
131 " .set push # csum_tcpudp_nofold\n"
135 " sltu $1, %0, %2 \n"
139 " sltu $1, %0, %3 \n"
143 " sltu $1, %0, %4 \n"
150 " dsll32 $1, %0, 0 \n"
152 " dsra32 %0, %0, 0 \n"
156 : "0" (daddr), "r"(saddr),
158 "r" (((unsigned long)htons(len)<<16) + proto*256),
160 "r" (((unsigned long)(proto)<<16) + len),
168 * computes the checksum of the TCP/UDP pseudo-header
169 * returns a 16-bit checksum, already complemented
171 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
174 unsigned short proto,
177 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
181 * this routine is used for miscellaneous IP-like checksums, mainly
184 static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
186 return csum_fold(csum_partial(buff, len, 0));
189 #define _HAVE_ARCH_IPV6_CSUM
190 static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
191 struct in6_addr *daddr,
193 unsigned short proto,
197 " .set push # csum_ipv6_magic\n"
200 " addu %0, %5 # proto (long in network byte order)\n"
201 " sltu $1, %0, %5 \n"
204 " addu %0, %6 # csum\n"
205 " sltu $1, %0, %6 \n"
206 " lw %1, 0(%2) # four words source address\n"
209 " sltu $1, %0, %1 \n"
214 " sltu $1, %0, %1 \n"
219 " sltu $1, %0, %1 \n"
224 " sltu $1, %0, %1 \n"
229 " sltu $1, %0, %1 \n"
234 " sltu $1, %0, %1 \n"
239 " sltu $1, %0, %1 \n"
244 " sltu $1, %0, %1 \n"
246 " addu %0, $1 # Add final carry\n"
248 : "=r" (sum), "=r" (proto)
249 : "r" (saddr), "r" (daddr),
250 "0" (htonl(len)), "1" (htonl(proto)), "r" (sum));
252 return csum_fold(sum);
255 #endif /* _ASM_CHECKSUM_H */