blob: 42d020b7dfbaca99e930aa7600a564702512c113 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King4baa9922008-08-02 10:55:55 +01002 * arch/arm/include/asm/checksum.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * IP checksum routines
5 *
6 * Copyright (C) Original authors of ../asm-i386/checksum.h
7 * Copyright (C) 1996-1999 Russell King
8 */
9#ifndef __ASM_ARM_CHECKSUM_H
10#define __ASM_ARM_CHECKSUM_H
11
12#include <linux/in6.h>
13
14/*
15 * computes the checksum of a memory block at buff, length len,
16 * and adds in "sum" (32-bit)
17 *
18 * returns a 32-bit number suitable for feeding into itself
19 * or csum_tcpudp_magic
20 *
21 * this function must be called with even lengths, except
22 * for the last fragment, which may be odd
23 *
24 * it's best to have buff aligned on a 32-bit boundary
25 */
Al Viroeb5a9652006-11-14 21:20:28 -080026__wsum csum_partial(const void *buff, int len, __wsum sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * the same as csum_partial, but copies from src while it
30 * checksums, and handles user-space pointer exceptions correctly, when needed.
31 *
32 * here even more important to align src and dst on a 32-bit (or even
33 * better 64-bit) boundary
34 */
35
Al Viroeb5a9652006-11-14 21:20:28 -080036__wsum
37csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Al Viroeb5a9652006-11-14 21:20:28 -080039__wsum
40csum_partial_copy_from_user(const void __user *src, void *dst, int len, __wsum sum, int *err_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
Russell King7ef416c2006-12-21 20:59:37 +000043 * Fold a partial checksum without adding pseudo headers
44 */
45static inline __sum16 csum_fold(__wsum sum)
46{
47 __asm__(
48 "add %0, %1, %1, ror #16 @ csum_fold"
49 : "=r" (sum)
50 : "r" (sum)
51 : "cc");
52 return (__force __sum16)(~(__force u32)sum >> 16);
53}
54
55/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * This is a version of ip_compute_csum() optimized for IP headers,
57 * which always checksum on 4 octet boundaries.
58 */
Al Viroeb5a9652006-11-14 21:20:28 -080059static inline __sum16
60ip_fast_csum(const void *iph, unsigned int ihl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Russell King7ef416c2006-12-21 20:59:37 +000062 unsigned int tmp1;
63 __wsum sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 __asm__ __volatile__(
66 "ldr %0, [%1], #4 @ ip_fast_csum \n\
67 ldr %3, [%1], #4 \n\
68 sub %2, %2, #5 \n\
69 adds %0, %0, %3 \n\
70 ldr %3, [%1], #4 \n\
71 adcs %0, %0, %3 \n\
72 ldr %3, [%1], #4 \n\
731: adcs %0, %0, %3 \n\
74 ldr %3, [%1], #4 \n\
75 tst %2, #15 @ do this carefully \n\
76 subne %2, %2, #1 @ without destroying \n\
77 bne 1b @ the carry flag \n\
78 adcs %0, %0, %3 \n\
Russell King7ef416c2006-12-21 20:59:37 +000079 adc %0, %0, #0"
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 : "=r" (sum), "=r" (iph), "=r" (ihl), "=r" (tmp1)
81 : "1" (iph), "2" (ihl)
Richard Purdie62500d12006-02-01 19:26:00 +000082 : "cc", "memory");
Russell King7ef416c2006-12-21 20:59:37 +000083 return csum_fold(sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Al Viroeb5a9652006-11-14 21:20:28 -080086static inline __wsum
Alexander Duyck01cfbad2016-03-11 14:05:34 -080087csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
88 __u8 proto, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Alexander Duyck01cfbad2016-03-11 14:05:34 -080090 u32 lenprot = len + proto;
Russell Kingd46cda12013-12-12 22:49:14 +000091 if (__builtin_constant_p(sum) && sum == 0) {
92 __asm__(
93 "adds %0, %1, %2 @ csum_tcpudp_nofold0 \n\t"
Al Viroeb5a9652006-11-14 21:20:28 -080094#ifdef __ARMEB__
Russell Kingd46cda12013-12-12 22:49:14 +000095 "adcs %0, %0, %3 \n\t"
Al Viroeb5a9652006-11-14 21:20:28 -080096#else
Russell Kingd46cda12013-12-12 22:49:14 +000097 "adcs %0, %0, %3, ror #8 \n\t"
Al Viroeb5a9652006-11-14 21:20:28 -080098#endif
Russell Kingd46cda12013-12-12 22:49:14 +000099 "adc %0, %0, #0"
100 : "=&r" (sum)
101 : "r" (daddr), "r" (saddr), "r" (lenprot)
102 : "cc");
103 } else {
104 __asm__(
105 "adds %0, %1, %2 @ csum_tcpudp_nofold \n\t"
106 "adcs %0, %0, %3 \n\t"
107#ifdef __ARMEB__
108 "adcs %0, %0, %4 \n\t"
109#else
110 "adcs %0, %0, %4, ror #8 \n\t"
111#endif
112 "adc %0, %0, #0"
113 : "=&r"(sum)
114 : "r" (sum), "r" (daddr), "r" (saddr), "r" (lenprot)
115 : "cc");
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return sum;
118}
119/*
120 * computes the checksum of the TCP/UDP pseudo-header
121 * returns a 16-bit checksum, already complemented
122 */
Al Viroeb5a9652006-11-14 21:20:28 -0800123static inline __sum16
Alexander Duyck01cfbad2016-03-11 14:05:34 -0800124csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
125 __u8 proto, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Russell King7ef416c2006-12-21 20:59:37 +0000127 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130
131/*
132 * this routine is used for miscellaneous IP-like checksums, mainly
133 * in icmp.c
134 */
Al Viroeb5a9652006-11-14 21:20:28 -0800135static inline __sum16
136ip_compute_csum(const void *buff, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 return csum_fold(csum_partial(buff, len, 0));
139}
140
141#define _HAVE_ARCH_IPV6_CSUM
Al Viroeb5a9652006-11-14 21:20:28 -0800142extern __wsum
143__csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __be32 len,
144 __be32 proto, __wsum sum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Al Viroeb5a9652006-11-14 21:20:28 -0800146static inline __sum16
147csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __u32 len,
148 unsigned short proto, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 return csum_fold(__csum_ipv6_magic(saddr, daddr, htonl(len),
151 htonl(proto), sum));
152}
153#endif