1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * IP/TCP/UDP checksumming routines
7 *
8 * Authors: Jorge Cwik, <jorge@laser.satlink.net>
9 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
10 * Tom May, <ftom@netcom.com>
11 * Andreas Schwab, <schwab@issan.informatik.uni-dortmund.de>
12 * Lots of code moved from tcp.c and ip.c; see those files
13 * for more names.
14 *
15 * 03/02/96 Jes Sorensen, Andreas Schwab, Roman Hodek:
16 * Fixed some nasty bugs, causing some horrible crashes.
17 * A: At some points, the sum (%0) was used as
18 * length-counter instead of the length counter
19 * (%1). Thanks to Roman Hodek for pointing this out.
20 * B: GCC seems to mess up if one uses too many
21 * data-registers to hold input values and one tries to
22 * specify d0 and d1 as scratch registers. Letting gcc
23 * choose these registers itself solves the problem.
24 *
25 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation; either version
28 * 2 of the License, or (at your option) any later version.
29 *
30 * 1998/8/31 Andreas Schwab:
31 * Zero out rest of buffer on exception in
32 * csum_partial_copy_from_user.
33 */
34
35 #include <net/checksum.h>
36
37 /*
38 * computes a partial checksum, e.g. for TCP/UDP fragments
39 */
40
41 unsigned int
csum_partial(const unsigned char * buff,int len,unsigned int sum)42 csum_partial (const unsigned char *buff, int len, unsigned int sum)
43 {
44 unsigned long tmp1, tmp2;
45 /*
46 * Experiments with ethernet and slip connections show that buff
47 * is aligned on either a 2-byte or 4-byte boundary.
48 */
49 __asm__("movel %2,%3\n\t"
50 "btst #1,%3\n\t" /* Check alignment */
51 "jeq 2f\n\t"
52 "subql #2,%1\n\t" /* buff%4==2: treat first word */
53 "jgt 1f\n\t"
54 "addql #2,%1\n\t" /* len was == 2, treat only rest */
55 "jra 4f\n"
56 "1:\t"
57 "addw %2@+,%0\n\t" /* add first word to sum */
58 "clrl %3\n\t"
59 "addxl %3,%0\n" /* add X bit */
60 "2:\t"
61 /* unrolled loop for the main part: do 8 longs at once */
62 "movel %1,%3\n\t" /* save len in tmp1 */
63 "lsrl #5,%1\n\t" /* len/32 */
64 "jeq 2f\n\t" /* not enough... */
65 "subql #1,%1\n"
66 "1:\t"
67 "movel %2@+,%4\n\t"
68 "addxl %4,%0\n\t"
69 "movel %2@+,%4\n\t"
70 "addxl %4,%0\n\t"
71 "movel %2@+,%4\n\t"
72 "addxl %4,%0\n\t"
73 "movel %2@+,%4\n\t"
74 "addxl %4,%0\n\t"
75 "movel %2@+,%4\n\t"
76 "addxl %4,%0\n\t"
77 "movel %2@+,%4\n\t"
78 "addxl %4,%0\n\t"
79 "movel %2@+,%4\n\t"
80 "addxl %4,%0\n\t"
81 "movel %2@+,%4\n\t"
82 "addxl %4,%0\n\t"
83 "dbra %1,1b\n\t"
84 "clrl %4\n\t"
85 "addxl %4,%0\n\t" /* add X bit */
86 "clrw %1\n\t"
87 "subql #1,%1\n\t"
88 "jcc 1b\n"
89 "2:\t"
90 "movel %3,%1\n\t" /* restore len from tmp1 */
91 "andw #0x1c,%3\n\t" /* number of rest longs */
92 "jeq 4f\n\t"
93 "lsrw #2,%3\n\t"
94 "subqw #1,%3\n"
95 "3:\t"
96 /* loop for rest longs */
97 "movel %2@+,%4\n\t"
98 "addxl %4,%0\n\t"
99 "dbra %3,3b\n\t"
100 "clrl %4\n\t"
101 "addxl %4,%0\n" /* add X bit */
102 "4:\t"
103 /* now check for rest bytes that do not fit into longs */
104 "andw #3,%1\n\t"
105 "jeq 7f\n\t"
106 "clrl %4\n\t" /* clear tmp2 for rest bytes */
107 "subqw #2,%1\n\t"
108 "jlt 5f\n\t"
109 "movew %2@+,%4\n\t" /* have rest >= 2: get word */
110 "swap %4\n\t" /* into bits 16..31 */
111 "tstw %1\n\t" /* another byte? */
112 "jeq 6f\n"
113 "5:\t"
114 "moveb %2@,%4\n\t" /* have odd rest: get byte */
115 "lslw #8,%4\n\t" /* into bits 8..15; 16..31 untouched */
116 "6:\t"
117 "addl %4,%0\n\t" /* now add rest long to sum */
118 "clrl %4\n\t"
119 "addxl %4,%0\n" /* add X bit */
120 "7:\t"
121 : "=d" (sum), "=d" (len), "=a" (buff),
122 "=&d" (tmp1), "=&d" (tmp2)
123 : "0" (sum), "1" (len), "2" (buff)
124 );
125 return(sum);
126 }
127
128
129
130 /*
131 * copy from user space while checksumming, with exception handling.
132 */
133
134 unsigned int
csum_partial_copy_from_user(const char * src,char * dst,int len,int sum,int * csum_err)135 csum_partial_copy_from_user(const char *src, char *dst, int len,
136 int sum, int *csum_err)
137 {
138 /*
139 * GCC doesn't like more than 10 operands for the asm
140 * statements so we have to use tmp2 for the error
141 * code.
142 */
143 unsigned long tmp1, tmp2;
144
145 __asm__("movel %2,%4\n\t"
146 "btst #1,%4\n\t" /* Check alignment */
147 "jeq 2f\n\t"
148 "subql #2,%1\n\t" /* buff%4==2: treat first word */
149 "jgt 1f\n\t"
150 "addql #2,%1\n\t" /* len was == 2, treat only rest */
151 "jra 4f\n"
152 "1:\n"
153 "10:\t"
154 "movesw %2@+,%4\n\t" /* add first word to sum */
155 "addw %4,%0\n\t"
156 "movew %4,%3@+\n\t"
157 "clrl %4\n\t"
158 "addxl %4,%0\n" /* add X bit */
159 "2:\t"
160 /* unrolled loop for the main part: do 8 longs at once */
161 "movel %1,%4\n\t" /* save len in tmp1 */
162 "lsrl #5,%1\n\t" /* len/32 */
163 "jeq 2f\n\t" /* not enough... */
164 "subql #1,%1\n"
165 "1:\n"
166 "11:\t"
167 "movesl %2@+,%5\n\t"
168 "addxl %5,%0\n\t"
169 "movel %5,%3@+\n\t"
170 "12:\t"
171 "movesl %2@+,%5\n\t"
172 "addxl %5,%0\n\t"
173 "movel %5,%3@+\n\t"
174 "13:\t"
175 "movesl %2@+,%5\n\t"
176 "addxl %5,%0\n\t"
177 "movel %5,%3@+\n\t"
178 "14:\t"
179 "movesl %2@+,%5\n\t"
180 "addxl %5,%0\n\t"
181 "movel %5,%3@+\n\t"
182 "15:\t"
183 "movesl %2@+,%5\n\t"
184 "addxl %5,%0\n\t"
185 "movel %5,%3@+\n\t"
186 "16:\t"
187 "movesl %2@+,%5\n\t"
188 "addxl %5,%0\n\t"
189 "movel %5,%3@+\n\t"
190 "17:\t"
191 "movesl %2@+,%5\n\t"
192 "addxl %5,%0\n\t"
193 "movel %5,%3@+\n\t"
194 "18:\t"
195 "movesl %2@+,%5\n\t"
196 "addxl %5,%0\n\t"
197 "movel %5,%3@+\n\t"
198 "dbra %1,1b\n\t"
199 "clrl %5\n\t"
200 "addxl %5,%0\n\t" /* add X bit */
201 "clrw %1\n\t"
202 "subql #1,%1\n\t"
203 "jcc 1b\n"
204 "2:\t"
205 "movel %4,%1\n\t" /* restore len from tmp1 */
206 "andw #0x1c,%4\n\t" /* number of rest longs */
207 "jeq 4f\n\t"
208 "lsrw #2,%4\n\t"
209 "subqw #1,%4\n"
210 "3:\n"
211 /* loop for rest longs */
212 "19:\t"
213 "movesl %2@+,%5\n\t"
214 "addxl %5,%0\n\t"
215 "movel %5,%3@+\n\t"
216 "dbra %4,3b\n\t"
217 "clrl %5\n\t"
218 "addxl %5,%0\n" /* add X bit */
219 "4:\t"
220 /* now check for rest bytes that do not fit into longs */
221 "andw #3,%1\n\t"
222 "jeq 7f\n\t"
223 "clrl %5\n\t" /* clear tmp2 for rest bytes */
224 "subqw #2,%1\n\t"
225 "jlt 5f\n\t"
226 "20:\t"
227 "movesw %2@+,%5\n\t" /* have rest >= 2: get word */
228 "movew %5,%3@+\n\t"
229 "swap %5\n\t" /* into bits 16..31 */
230 "tstw %1\n\t" /* another byte? */
231 "jeq 6f\n"
232 "5:\n"
233 "21:\t"
234 "movesb %2@,%5\n\t" /* have odd rest: get byte */
235 "moveb %5,%3@+\n\t"
236 "lslw #8,%5\n\t" /* into bits 8..15; 16..31 untouched */
237 "6:\t"
238 "addl %5,%0\n\t" /* now add rest long to sum */
239 "clrl %5\n\t"
240 "addxl %5,%0\n\t" /* add X bit */
241 "7:\t"
242 "clrl %5\n" /* no error - clear return value */
243 "8:\n"
244 ".section .fixup,\"ax\"\n"
245 ".even\n"
246 /* If any execption occurs zero out the rest.
247 Similarities with the code above are intentional :-) */
248 "90:\t"
249 "clrw %3@+\n\t"
250 "movel %1,%4\n\t"
251 "lsrl #5,%1\n\t"
252 "jeq 1f\n\t"
253 "subql #1,%1\n"
254 "91:\t"
255 "clrl %3@+\n"
256 "92:\t"
257 "clrl %3@+\n"
258 "93:\t"
259 "clrl %3@+\n"
260 "94:\t"
261 "clrl %3@+\n"
262 "95:\t"
263 "clrl %3@+\n"
264 "96:\t"
265 "clrl %3@+\n"
266 "97:\t"
267 "clrl %3@+\n"
268 "98:\t"
269 "clrl %3@+\n\t"
270 "dbra %1,91b\n\t"
271 "clrw %1\n\t"
272 "subql #1,%1\n\t"
273 "jcc 91b\n"
274 "1:\t"
275 "movel %4,%1\n\t"
276 "andw #0x1c,%4\n\t"
277 "jeq 1f\n\t"
278 "lsrw #2,%4\n\t"
279 "subqw #1,%4\n"
280 "99:\t"
281 "clrl %3@+\n\t"
282 "dbra %4,99b\n\t"
283 "1:\t"
284 "andw #3,%1\n\t"
285 "jeq 9f\n"
286 "100:\t"
287 "clrw %3@+\n\t"
288 "tstw %1\n\t"
289 "jeq 9f\n"
290 "101:\t"
291 "clrb %3@+\n"
292 "9:\t"
293 #define STR(X) STR1(X)
294 #define STR1(X) #X
295 "moveq #-" STR(EFAULT) ",%5\n\t"
296 "jra 8b\n"
297 ".previous\n"
298 ".section __ex_table,\"a\"\n"
299 ".long 10b,90b\n"
300 ".long 11b,91b\n"
301 ".long 12b,92b\n"
302 ".long 13b,93b\n"
303 ".long 14b,94b\n"
304 ".long 15b,95b\n"
305 ".long 16b,96b\n"
306 ".long 17b,97b\n"
307 ".long 18b,98b\n"
308 ".long 19b,99b\n"
309 ".long 20b,100b\n"
310 ".long 21b,101b\n"
311 ".previous"
312 : "=d" (sum), "=d" (len), "=a" (src), "=a" (dst),
313 "=&d" (tmp1), "=d" (tmp2)
314 : "0" (sum), "1" (len), "2" (src), "3" (dst)
315 );
316
317 *csum_err = tmp2;
318
319 return(sum);
320 }
321
322 /*
323 * copy from kernel space while checksumming, otherwise like csum_partial
324 */
325
326 unsigned int
csum_partial_copy(const char * src,char * dst,int len,int sum)327 csum_partial_copy(const char *src, char *dst, int len, int sum)
328 {
329 unsigned long tmp1, tmp2;
330 __asm__("movel %2,%4\n\t"
331 "btst #1,%4\n\t" /* Check alignment */
332 "jeq 2f\n\t"
333 "subql #2,%1\n\t" /* buff%4==2: treat first word */
334 "jgt 1f\n\t"
335 "addql #2,%1\n\t" /* len was == 2, treat only rest */
336 "jra 4f\n"
337 "1:\t"
338 "movew %2@+,%4\n\t" /* add first word to sum */
339 "addw %4,%0\n\t"
340 "movew %4,%3@+\n\t"
341 "clrl %4\n\t"
342 "addxl %4,%0\n" /* add X bit */
343 "2:\t"
344 /* unrolled loop for the main part: do 8 longs at once */
345 "movel %1,%4\n\t" /* save len in tmp1 */
346 "lsrl #5,%1\n\t" /* len/32 */
347 "jeq 2f\n\t" /* not enough... */
348 "subql #1,%1\n"
349 "1:\t"
350 "movel %2@+,%5\n\t"
351 "addxl %5,%0\n\t"
352 "movel %5,%3@+\n\t"
353 "movel %2@+,%5\n\t"
354 "addxl %5,%0\n\t"
355 "movel %5,%3@+\n\t"
356 "movel %2@+,%5\n\t"
357 "addxl %5,%0\n\t"
358 "movel %5,%3@+\n\t"
359 "movel %2@+,%5\n\t"
360 "addxl %5,%0\n\t"
361 "movel %5,%3@+\n\t"
362 "movel %2@+,%5\n\t"
363 "addxl %5,%0\n\t"
364 "movel %5,%3@+\n\t"
365 "movel %2@+,%5\n\t"
366 "addxl %5,%0\n\t"
367 "movel %5,%3@+\n\t"
368 "movel %2@+,%5\n\t"
369 "addxl %5,%0\n\t"
370 "movel %5,%3@+\n\t"
371 "movel %2@+,%5\n\t"
372 "addxl %5,%0\n\t"
373 "movel %5,%3@+\n\t"
374 "dbra %1,1b\n\t"
375 "clrl %5\n\t"
376 "addxl %5,%0\n\t" /* add X bit */
377 "clrw %1\n\t"
378 "subql #1,%1\n\t"
379 "jcc 1b\n"
380 "2:\t"
381 "movel %4,%1\n\t" /* restore len from tmp1 */
382 "andw #0x1c,%4\n\t" /* number of rest longs */
383 "jeq 4f\n\t"
384 "lsrw #2,%4\n\t"
385 "subqw #1,%4\n"
386 "3:\t"
387 /* loop for rest longs */
388 "movel %2@+,%5\n\t"
389 "addxl %5,%0\n\t"
390 "movel %5,%3@+\n\t"
391 "dbra %4,3b\n\t"
392 "clrl %5\n\t"
393 "addxl %5,%0\n" /* add X bit */
394 "4:\t"
395 /* now check for rest bytes that do not fit into longs */
396 "andw #3,%1\n\t"
397 "jeq 7f\n\t"
398 "clrl %5\n\t" /* clear tmp2 for rest bytes */
399 "subqw #2,%1\n\t"
400 "jlt 5f\n\t"
401 "movew %2@+,%5\n\t" /* have rest >= 2: get word */
402 "movew %5,%3@+\n\t"
403 "swap %5\n\t" /* into bits 16..31 */
404 "tstw %1\n\t" /* another byte? */
405 "jeq 6f\n"
406 "5:\t"
407 "moveb %2@,%5\n\t" /* have odd rest: get byte */
408 "moveb %5,%3@+\n\t"
409 "lslw #8,%5\n" /* into bits 8..15; 16..31 untouched */
410 "6:\t"
411 "addl %5,%0\n\t" /* now add rest long to sum */
412 "clrl %5\n\t"
413 "addxl %5,%0\n" /* add X bit */
414 "7:\t"
415 : "=d" (sum), "=d" (len), "=a" (src), "=a" (dst),
416 "=&d" (tmp1), "=&d" (tmp2)
417 : "0" (sum), "1" (len), "2" (src), "3" (dst)
418 );
419 return(sum);
420 }
421