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.
6 * MIPS specific IP/TCP/UDP checksumming routines
8 * Authors: Ralf Baechle, <ralf@waldorf-gmbh.de>
9 * Lots of code moved from tcp.c and ip.c; see those files
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 * $Id: checksum.c,v 1.3 1997/12/01 17:57:34 ralf Exp $
19 #include <linux/module.h>
20 #include <linux/types.h>
22 #include <net/checksum.h>
23 #include <asm/byteorder.h>
24 #include <asm/string.h>
25 #include <asm/uaccess.h>
28 __asm__ __volatile__ ( \
30 " addc %0, %%r0, %0\n" \
34 static inline unsigned short from32to16(unsigned int x
)
36 /* 32 bits --> 16 bits + carry */
37 x
= (x
& 0xffff) + (x
>> 16);
38 /* 16 bits + carry --> 16 bits including carry */
39 x
= (x
& 0xffff) + (x
>> 16);
40 return (unsigned short)x
;
43 static inline unsigned int do_csum(const unsigned char * buff
, int len
)
46 unsigned int result
= 0;
50 odd
= 1 & (unsigned long) buff
;
52 result
= be16_to_cpu(*buff
);
56 count
= len
>> 1; /* nr of 16-bit words.. */
58 if (2 & (unsigned long) buff
) {
59 result
+= *(unsigned short *) buff
;
64 count
>>= 1; /* nr of 32-bit words.. */
67 unsigned int r1
, r2
, r3
, r4
;
68 r1
= *(unsigned int *)(buff
+ 0);
69 r2
= *(unsigned int *)(buff
+ 4);
70 r3
= *(unsigned int *)(buff
+ 8);
71 r4
= *(unsigned int *)(buff
+ 12);
80 unsigned int w
= *(unsigned int *) buff
;
85 result
= (result
& 0xffff) + (result
>> 16);
88 result
+= *(unsigned short *) buff
;
93 result
+= le16_to_cpu(*buff
);
94 result
= from32to16(result
);
96 result
= swab16(result
);
102 * computes a partial checksum, e.g. for TCP/UDP fragments
104 unsigned int csum_partial(const unsigned char *buff
, int len
, unsigned int sum
)
106 unsigned int result
= do_csum(buff
, len
);
108 return from32to16(result
);
111 EXPORT_SYMBOL(csum_partial
);
114 * copy while checksumming, otherwise like csum_partial
116 unsigned int csum_partial_copy_nocheck(const unsigned char *src
, unsigned char *dst
,
117 int len
, unsigned int sum
)
120 * It's 2:30 am and I don't feel like doing it real ...
121 * This is lots slower than the real thing (tm)
123 sum
= csum_partial(src
, len
, sum
);
124 memcpy(dst
, src
, len
);
128 EXPORT_SYMBOL(csum_partial_copy_nocheck
);
131 * Copy from userspace and compute checksum. If we catch an exception
132 * then zero the rest of the buffer.
134 unsigned int csum_partial_copy_from_user(const unsigned char __user
*src
,
135 unsigned char *dst
, int len
,
136 unsigned int sum
, int *err_ptr
)
140 missing
= copy_from_user(dst
, src
, len
);
142 memset(dst
+ len
- missing
, 0, missing
);
146 return csum_partial(dst
, len
, sum
);
148 EXPORT_SYMBOL(csum_partial_copy_from_user
);