Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/include/asm-arm/checksum.h |
| 3 | * |
| 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 Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 26 | __wsum csum_partial(const void *buff, int len, __wsum sum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 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 Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 36 | __wsum |
| 37 | csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 39 | __wsum |
| 40 | csum_partial_copy_from_user(const void __user *src, void *dst, int len, __wsum sum, int *err_ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * This is a version of ip_compute_csum() optimized for IP headers, |
| 44 | * which always checksum on 4 octet boundaries. |
| 45 | */ |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 46 | static inline __sum16 |
| 47 | ip_fast_csum(const void *iph, unsigned int ihl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | unsigned int sum, tmp1; |
| 50 | |
| 51 | __asm__ __volatile__( |
| 52 | "ldr %0, [%1], #4 @ ip_fast_csum \n\ |
| 53 | ldr %3, [%1], #4 \n\ |
| 54 | sub %2, %2, #5 \n\ |
| 55 | adds %0, %0, %3 \n\ |
| 56 | ldr %3, [%1], #4 \n\ |
| 57 | adcs %0, %0, %3 \n\ |
| 58 | ldr %3, [%1], #4 \n\ |
| 59 | 1: adcs %0, %0, %3 \n\ |
| 60 | ldr %3, [%1], #4 \n\ |
| 61 | tst %2, #15 @ do this carefully \n\ |
| 62 | subne %2, %2, #1 @ without destroying \n\ |
| 63 | bne 1b @ the carry flag \n\ |
| 64 | adcs %0, %0, %3 \n\ |
| 65 | adc %0, %0, #0 \n\ |
| 66 | adds %0, %0, %0, lsl #16 \n\ |
| 67 | addcs %0, %0, #0x10000 \n\ |
| 68 | mvn %0, %0 \n\ |
| 69 | mov %0, %0, lsr #16" |
| 70 | : "=r" (sum), "=r" (iph), "=r" (ihl), "=r" (tmp1) |
| 71 | : "1" (iph), "2" (ihl) |
Richard Purdie | 62500d1 | 2006-02-01 19:26:00 +0000 | [diff] [blame] | 72 | : "cc", "memory"); |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 73 | return (__force __sum16)sum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Fold a partial checksum without adding pseudo headers |
| 78 | */ |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 79 | static inline __sum16 csum_fold(__wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | __asm__( |
| 82 | "adds %0, %1, %1, lsl #16 @ csum_fold \n\ |
| 83 | addcs %0, %0, #0x10000" |
| 84 | : "=r" (sum) |
| 85 | : "r" (sum) |
| 86 | : "cc"); |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 87 | return (__force __sum16)(~(__force u32)sum >> 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 90 | static inline __wsum |
| 91 | csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len, |
| 92 | unsigned short proto, __wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
| 94 | __asm__( |
| 95 | "adds %0, %1, %2 @ csum_tcpudp_nofold \n\ |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 96 | adcs %0, %0, %3 \n" |
| 97 | #ifdef __ARMEB__ |
| 98 | "adcs %0, %0, %4 \n" |
| 99 | #else |
| 100 | "adcs %0, %0, %4, lsl #8 \n" |
| 101 | #endif |
| 102 | "adcs %0, %0, %5 \n\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | adc %0, %0, #0" |
| 104 | : "=&r"(sum) |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 105 | : "r" (sum), "r" (daddr), "r" (saddr), "r" (len), "Ir" (htons(proto)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | : "cc"); |
| 107 | return sum; |
| 108 | } |
| 109 | /* |
| 110 | * computes the checksum of the TCP/UDP pseudo-header |
| 111 | * returns a 16-bit checksum, already complemented |
| 112 | */ |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 113 | static inline __sum16 |
| 114 | csum_tcpudp_magic(__be32 saddr, __be32 daddr, unsigned short len, |
| 115 | unsigned short proto, __wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | __asm__( |
| 118 | "adds %0, %1, %2 @ csum_tcpudp_magic \n\ |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 119 | adcs %0, %0, %3 \n" |
| 120 | #ifdef __ARMEB__ |
| 121 | "adcs %0, %0, %4 \n" |
| 122 | #else |
| 123 | "adcs %0, %0, %4, lsl #8 \n" |
| 124 | #endif |
| 125 | "adcs %0, %0, %5 \n\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | adc %0, %0, #0 \n\ |
| 127 | adds %0, %0, %0, lsl #16 \n\ |
| 128 | addcs %0, %0, #0x10000 \n\ |
| 129 | mvn %0, %0" |
| 130 | : "=&r"(sum) |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 131 | : "r" (sum), "r" (daddr), "r" (saddr), "r" (len), "Ir" (htons(proto)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | : "cc"); |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 133 | return (__force __sum16)((__force u32)sum >> 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
| 137 | /* |
| 138 | * this routine is used for miscellaneous IP-like checksums, mainly |
| 139 | * in icmp.c |
| 140 | */ |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 141 | static inline __sum16 |
| 142 | ip_compute_csum(const void *buff, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | return csum_fold(csum_partial(buff, len, 0)); |
| 145 | } |
| 146 | |
| 147 | #define _HAVE_ARCH_IPV6_CSUM |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 148 | extern __wsum |
| 149 | __csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __be32 len, |
| 150 | __be32 proto, __wsum sum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Al Viro | eb5a965 | 2006-11-14 21:20:28 -0800 | [diff] [blame] | 152 | static inline __sum16 |
| 153 | csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __u32 len, |
| 154 | unsigned short proto, __wsum sum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { |
| 156 | return csum_fold(__csum_ipv6_magic(saddr, daddr, htonl(len), |
| 157 | htonl(proto), sum)); |
| 158 | } |
| 159 | #endif |