1
2
3
4
5
6
7
8
9
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
20
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
32
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
37 return out_v.us[0] + out_v.us[1];
38}
39
40
41
42
43
44__sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
45 unsigned short len,
46 unsigned short proto,
47 __wsum sum)
48{
49 return (__force __sum16)~from64to16(
50 (__force u64)saddr + (__force u64)daddr +
51 (__force u64)sum + ((len + proto) << 8));
52}
53
54__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
55 unsigned short len,
56 unsigned short proto,
57 __wsum sum)
58{
59 unsigned long result;
60
61 result = (__force u64)saddr + (__force u64)daddr +
62 (__force u64)sum + ((len + proto) << 8);
63
64
65
66
67 result = (result & 0xffffffff) + (result >> 32);
68
69 result = (result & 0xffffffff) + (result >> 32);
70 return (__force __wsum)result;
71}
72EXPORT_SYMBOL(csum_tcpudp_nofold);
73
74
75
76
77
78
79
80
81static inline unsigned long do_csum(const unsigned char * buff, int len)
82{
83 int odd, count;
84 unsigned long result = 0;
85
86 if (len <= 0)
87 goto out;
88 odd = 1 & (unsigned long) buff;
89 if (odd) {
90 result = *buff << 8;
91 len--;
92 buff++;
93 }
94 count = len >> 1;
95 if (count) {
96 if (2 & (unsigned long) buff) {
97 result += *(unsigned short *) buff;
98 count--;
99 len -= 2;
100 buff += 2;
101 }
102 count >>= 1;
103 if (count) {
104 if (4 & (unsigned long) buff) {
105 result += *(unsigned int *) buff;
106 count--;
107 len -= 4;
108 buff += 4;
109 }
110 count >>= 1;
111 if (count) {
112 unsigned long carry = 0;
113 do {
114 unsigned long w = *(unsigned long *) buff;
115 count--;
116 buff += 8;
117 result += carry;
118 result += w;
119 carry = (w > result);
120 } while (count);
121 result += carry;
122 result = (result & 0xffffffff) + (result >> 32);
123 }
124 if (len & 4) {
125 result += *(unsigned int *) buff;
126 buff += 4;
127 }
128 }
129 if (len & 2) {
130 result += *(unsigned short *) buff;
131 buff += 2;
132 }
133 }
134 if (len & 1)
135 result += *buff;
136 result = from64to16(result);
137 if (odd)
138 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
139out:
140 return result;
141}
142
143
144
145
146
147__sum16 ip_fast_csum(const void *iph, unsigned int ihl)
148{
149 return (__force __sum16)~do_csum(iph,ihl*4);
150}
151
152
153
154
155
156
157
158
159
160
161
162
163
164__wsum csum_partial(const void *buff, int len, __wsum sum)
165{
166 unsigned long result = do_csum(buff, len);
167
168
169 result += (__force u32)sum;
170
171 result = (result & 0xffffffff) + (result >> 32);
172 return (__force __wsum)result;
173}
174
175EXPORT_SYMBOL(csum_partial);
176
177
178
179
180
181__sum16 ip_compute_csum(const void *buff, int len)
182{
183 return (__force __sum16)~from64to16(do_csum(buff,len));
184}
185