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, 1996, 1997, 1998, 2001 by Ralf Baechle
8 #ifndef _ASM_CHECKSUM_H
9 #define _ASM_CHECKSUM_H
11 #include <asm/uaccess.h>
12 #include <linux/in6.h>
15 * computes the checksum of a memory block at buff, length len,
16 * and adds in "sum" (32-bit)
18 * returns a 32-bit number suitable for feeding into itself
19 * or csum_tcpudp_magic
21 * this function must be called with even lengths, except
22 * for the last fragment, which may be odd
24 * it's best to have buff aligned on a 32-bit boundary
26 unsigned int csum_partial(const unsigned char *buff
, int len
, unsigned int sum
);
29 * this is a new version of the above that records errors it finds in *errp,
30 * but continues and zeros the rest of the buffer.
32 unsigned int csum_partial_copy_from_user(const char *src
, char *dst
, int len
,
33 unsigned int sum
, int *errp
);
36 * Copy and checksum to user
38 #define HAVE_CSUM_COPY_USER
39 static inline unsigned int csum_and_copy_to_user (const char *src
, char *dst
,
43 sum
= csum_partial(src
, len
, sum
);
45 if (copy_to_user(dst
, src
, len
)) {
54 * the same as csum_partial, but copies from user space (but on MIPS
55 * we have just one address space, so this is identical to the above)
57 unsigned int csum_partial_copy_nocheck(const char *src
, char *dst
, int len
,
61 * Fold a partial checksum without adding pseudo headers
63 static inline unsigned short int csum_fold(unsigned int sum
)
66 ".set\tnoat\t\t\t# csum_fold\n\t"
81 * This is a version of ip_compute_csum() optimized for IP headers,
82 * which always checksum on 4 octet boundaries.
84 * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
87 static inline unsigned short ip_fast_csum(unsigned char *iph
,
94 * This is for 32-bit MIPS processors.
97 ".set\tnoreorder\t\t\t# ip_fast_csum\n\t"
106 "sltu\t$1, %0, %3\n\t"
110 "sltu\t$1, %0, %3\n\t"
114 "sltu\t$1, %0, %3\n\t"
117 "1:\tlw\t%3, 16(%1)\n\t"
120 "sltu\t$1, %0, %3\n\t"
121 "bne\t%2, %1, 1b\n\t"
126 : "=&r" (sum
), "=&r" (iph
), "=&r" (ihl
), "=&r" (dummy
)
127 : "1" (iph
), "2" (ihl
));
129 return csum_fold(sum
);
133 * computes the checksum of the TCP/UDP pseudo-header
134 * returns a 16-bit checksum, already complemented
136 static inline unsigned long csum_tcpudp_nofold(unsigned long saddr
,
139 unsigned short proto
,
143 ".set\tnoat\t\t\t# csum_tcpudp_nofold\n\t"
145 "sltu\t$1, %0, %2\n\t"
149 "sltu\t$1, %0, %3\n\t"
153 "sltu\t$1, %0, %4\n\t"
157 : "0" (daddr
), "r"(saddr
),
159 "r" ((ntohs(len
)<<16)+proto
*256),
161 "r" (((proto
)<<16)+len
),
169 * computes the checksum of the TCP/UDP pseudo-header
170 * returns a 16-bit checksum, already complemented
172 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr
,
175 unsigned short proto
,
178 return csum_fold(csum_tcpudp_nofold(saddr
, daddr
, len
, proto
, sum
));
182 * this routine is used for miscellaneous IP-like checksums, mainly
185 static inline unsigned short ip_compute_csum(unsigned char * buff
, int len
)
187 return csum_fold(csum_partial(buff
, len
, 0));
190 #define _HAVE_ARCH_IPV6_CSUM
191 static __inline__
unsigned short int csum_ipv6_magic(struct in6_addr
*saddr
,
192 struct in6_addr
*daddr
,
194 unsigned short proto
,
198 ".set\tpush\t\t\t# csum_ipv6_magic\n\t"
199 ".set\tnoreorder\n\t"
201 "addu\t%0, %5\t\t\t# proto (long in network byte order)\n\t"
202 "sltu\t$1, %0, %5\n\t"
205 "addu\t%0, %6\t\t\t# csum\n\t"
206 "sltu\t$1, %0, %6\n\t"
207 "lw\t%1, 0(%2)\t\t\t# four words source address\n\t"
210 "sltu\t$1, %0, %1\n\t"
215 "sltu\t$1, %0, %1\n\t"
220 "sltu\t$1, %0, %1\n\t"
225 "sltu\t$1, %0, %1\n\t"
230 "sltu\t$1, %0, %1\n\t"
235 "sltu\t$1, %0, %1\n\t"
240 "sltu\t$1, %0, %1\n\t"
245 "sltu\t$1, %0, %1\n\t"
247 "addu\t%0, $1\t\t\t# Add final carry\n\t"
249 : "=r" (sum
), "=r" (proto
)
250 : "r" (saddr
), "r" (daddr
),
251 "0" (htonl(len
)), "1" (htonl(proto
)), "r" (sum
));
253 return csum_fold(sum
);
256 #endif /* _ASM_CHECKSUM_H */