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