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 #include <linux/module.h>
18 #include <linux/types.h>
20 #include <net/checksum.h>
21 #include <asm/byteorder.h>
22 #include <asm/string.h>
23 #include <linux/uaccess.h>
26 __asm__ __volatile__ ( \
28 " addc %0, %%r0, %0\n" \
32 static inline unsigned short from32to16(unsigned int x
)
34 /* 32 bits --> 16 bits + carry */
35 x
= (x
& 0xffff) + (x
>> 16);
36 /* 16 bits + carry --> 16 bits including carry */
37 x
= (x
& 0xffff) + (x
>> 16);
38 return (unsigned short)x
;
41 static inline unsigned int do_csum(const unsigned char * buff
, int len
)
44 unsigned int result
= 0;
48 odd
= 1 & (unsigned long) buff
;
50 result
= be16_to_cpu(*buff
);
54 count
= len
>> 1; /* nr of 16-bit words.. */
56 if (2 & (unsigned long) buff
) {
57 result
+= *(unsigned short *) buff
;
62 count
>>= 1; /* nr of 32-bit words.. */
65 unsigned int r1
, r2
, r3
, r4
;
66 r1
= *(unsigned int *)(buff
+ 0);
67 r2
= *(unsigned int *)(buff
+ 4);
68 r3
= *(unsigned int *)(buff
+ 8);
69 r4
= *(unsigned int *)(buff
+ 12);
78 unsigned int w
= *(unsigned int *) buff
;
83 result
= (result
& 0xffff) + (result
>> 16);
86 result
+= *(unsigned short *) buff
;
91 result
+= le16_to_cpu(*buff
);
92 result
= from32to16(result
);
94 result
= swab16(result
);
100 * computes a partial checksum, e.g. for TCP/UDP fragments
103 * why bother folding?
105 __wsum
csum_partial(const void *buff
, int len
, __wsum sum
)
107 unsigned int result
= do_csum(buff
, len
);
109 return (__force __wsum
)from32to16(result
);
112 EXPORT_SYMBOL(csum_partial
);
115 * copy while checksumming, otherwise like csum_partial
117 __wsum
csum_partial_copy_nocheck(const void *src
, void *dst
,
121 * It's 2:30 am and I don't feel like doing it real ...
122 * This is lots slower than the real thing (tm)
124 sum
= csum_partial(src
, len
, sum
);
125 memcpy(dst
, src
, len
);
129 EXPORT_SYMBOL(csum_partial_copy_nocheck
);
132 * Copy from userspace and compute checksum. If we catch an exception
133 * then zero the rest of the buffer.
135 __wsum
csum_partial_copy_from_user(const void __user
*src
,
137 __wsum sum
, int *err_ptr
)
141 missing
= copy_from_user(dst
, src
, len
);
143 memset(dst
+ len
- missing
, 0, missing
);
147 return csum_partial(dst
, len
, sum
);
149 EXPORT_SYMBOL(csum_partial_copy_from_user
);