blob: b57f8007db14f78565200134f6f519165d3970ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/alpha/lib/checksum.c
3 *
4 * This file contains network checksum routines that are better done
5 * in an architecture-specific manner due to speed..
6 * Comments in other versions indicate that the algorithms are from RFC1071
7 *
Simon Arlottc3a2dde2007-10-20 01:04:37 +02008 * accelerated versions (and 21264 assembly versions ) contributed by
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Rick Gorton <rick.gorton@alpha-processor.com>
10 */
11
12#include <linux/module.h>
13#include <linux/string.h>
14
15#include <asm/byteorder.h>
16
17static inline unsigned short from64to16(unsigned long x)
18{
19 /* Using extract instructions is a bit more efficient
20 than the original shift/bitmask version. */
21
22 union {
23 unsigned long ul;
24 unsigned int ui[2];
25 unsigned short us[4];
26 } in_v, tmp_v, out_v;
27
28 in_v.ul = x;
29 tmp_v.ul = (unsigned long) in_v.ui[0] + (unsigned long) in_v.ui[1];
30
31 /* Since the bits of tmp_v.sh[3] are going to always be zero,
32 we don't have to bother to add that in. */
33 out_v.ul = (unsigned long) tmp_v.us[0] + (unsigned long) tmp_v.us[1]
34 + (unsigned long) tmp_v.us[2];
35
36 /* Similarly, out_v.us[2] is always zero for the final add. */
37 return out_v.us[0] + out_v.us[1];
38}
39
40/*
41 * computes the checksum of the TCP/UDP pseudo-header
42 * returns a 16-bit checksum, already complemented.
43 */
Al Viro9be259a2006-11-14 21:14:53 -080044__sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
Alexander Duyck01cfbad2016-03-11 14:05:34 -080045 __u32 len, __u8 proto, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Al Viro9be259a2006-11-14 21:14:53 -080047 return (__force __sum16)~from64to16(
48 (__force u64)saddr + (__force u64)daddr +
49 (__force u64)sum + ((len + proto) << 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
Al Viro00fc0e02016-01-11 09:51:29 -050051EXPORT_SYMBOL(csum_tcpudp_magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Al Viro9be259a2006-11-14 21:14:53 -080053__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
Alexander Duyck01cfbad2016-03-11 14:05:34 -080054 __u32 len, __u8 proto, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 unsigned long result;
57
Al Viro9be259a2006-11-14 21:14:53 -080058 result = (__force u64)saddr + (__force u64)daddr +
59 (__force u64)sum + ((len + proto) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 /* Fold down to 32-bits so we don't lose in the typedef-less
62 network stack. */
63 /* 64 to 33 */
64 result = (result & 0xffffffff) + (result >> 32);
65 /* 33 to 32 */
66 result = (result & 0xffffffff) + (result >> 32);
Al Viro9be259a2006-11-14 21:14:53 -080067 return (__force __wsum)result;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
Al Viro547c1782007-07-17 08:49:35 +010069EXPORT_SYMBOL(csum_tcpudp_nofold);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * Do a 64-bit checksum on an arbitrary memory area..
73 *
74 * This isn't a great routine, but it's not _horrible_ either. The
75 * inner loop could be unrolled a bit further, and there are better
76 * ways to do the carry, but this is reasonable.
77 */
78static inline unsigned long do_csum(const unsigned char * buff, int len)
79{
80 int odd, count;
81 unsigned long result = 0;
82
83 if (len <= 0)
84 goto out;
85 odd = 1 & (unsigned long) buff;
86 if (odd) {
87 result = *buff << 8;
88 len--;
89 buff++;
90 }
91 count = len >> 1; /* nr of 16-bit words.. */
92 if (count) {
93 if (2 & (unsigned long) buff) {
94 result += *(unsigned short *) buff;
95 count--;
96 len -= 2;
97 buff += 2;
98 }
99 count >>= 1; /* nr of 32-bit words.. */
100 if (count) {
101 if (4 & (unsigned long) buff) {
102 result += *(unsigned int *) buff;
103 count--;
104 len -= 4;
105 buff += 4;
106 }
107 count >>= 1; /* nr of 64-bit words.. */
108 if (count) {
109 unsigned long carry = 0;
110 do {
111 unsigned long w = *(unsigned long *) buff;
112 count--;
113 buff += 8;
114 result += carry;
115 result += w;
116 carry = (w > result);
117 } while (count);
118 result += carry;
119 result = (result & 0xffffffff) + (result >> 32);
120 }
121 if (len & 4) {
122 result += *(unsigned int *) buff;
123 buff += 4;
124 }
125 }
126 if (len & 2) {
127 result += *(unsigned short *) buff;
128 buff += 2;
129 }
130 }
131 if (len & 1)
132 result += *buff;
133 result = from64to16(result);
134 if (odd)
135 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
136out:
137 return result;
138}
139
140/*
141 * This is a version of ip_compute_csum() optimized for IP headers,
142 * which always checksum on 4 octet boundaries.
143 */
Al Viro9be259a2006-11-14 21:14:53 -0800144__sum16 ip_fast_csum(const void *iph, unsigned int ihl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Al Viro9be259a2006-11-14 21:14:53 -0800146 return (__force __sum16)~do_csum(iph,ihl*4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
Al Viro00fc0e02016-01-11 09:51:29 -0500148EXPORT_SYMBOL(ip_fast_csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/*
151 * computes the checksum of a memory block at buff, length len,
152 * and adds in "sum" (32-bit)
153 *
154 * returns a 32-bit number suitable for feeding into itself
155 * or csum_tcpudp_magic
156 *
157 * this function must be called with even lengths, except
158 * for the last fragment, which may be odd
159 *
160 * it's best to have buff aligned on a 32-bit boundary
161 */
Al Viro9be259a2006-11-14 21:14:53 -0800162__wsum csum_partial(const void *buff, int len, __wsum sum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 unsigned long result = do_csum(buff, len);
165
166 /* add in old sum, and carry.. */
Al Viro9be259a2006-11-14 21:14:53 -0800167 result += (__force u32)sum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* 32+c bits -> 32 bits */
169 result = (result & 0xffffffff) + (result >> 32);
Al Viro9be259a2006-11-14 21:14:53 -0800170 return (__force __wsum)result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173EXPORT_SYMBOL(csum_partial);
174
175/*
176 * this routine is used for miscellaneous IP-like checksums, mainly
177 * in icmp.c
178 */
Al Viro9be259a2006-11-14 21:14:53 -0800179__sum16 ip_compute_csum(const void *buff, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Al Viro9be259a2006-11-14 21:14:53 -0800181 return (__force __sum16)~from64to16(do_csum(buff,len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
Al Viro00fc0e02016-01-11 09:51:29 -0500183EXPORT_SYMBOL(ip_compute_csum);