1 // SPDX-License-Identifier: GPL-2.0
3 * Network Checksum & Copy routine
5 * Copyright (C) 1999, 2003-2004 Hewlett-Packard Co
6 * Stephane Eranian <eranian@hpl.hp.com>
8 * Most of the code has been imported from Linux/Alpha
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/string.h>
15 #include <linux/uaccess.h>
18 * XXX Fixme: those 2 inlines are meant for debugging and will go away
20 static inline unsigned
21 short from64to16(unsigned long x
)
23 /* add up 32-bit words for 33 bits */
24 x
= (x
& 0xffffffff) + (x
>> 32);
25 /* add up 16-bit and 17-bit words for 17+c bits */
26 x
= (x
& 0xffff) + (x
>> 16);
27 /* add up 16-bit and 2-bit for 16+c bit */
28 x
= (x
& 0xffff) + (x
>> 16);
30 x
= (x
& 0xffff) + (x
>> 16);
35 unsigned long do_csum_c(const unsigned char * buff
, int len
, unsigned int psum
)
38 unsigned long result
= (unsigned long)psum
;
42 odd
= 1 & (unsigned long) buff
;
48 count
= len
>> 1; /* nr of 16-bit words.. */
50 if (2 & (unsigned long) buff
) {
51 result
+= *(unsigned short *) buff
;
56 count
>>= 1; /* nr of 32-bit words.. */
58 if (4 & (unsigned long) buff
) {
59 result
+= *(unsigned int *) buff
;
64 count
>>= 1; /* nr of 64-bit words.. */
66 unsigned long carry
= 0;
68 unsigned long w
= *(unsigned long *) buff
;
76 result
= (result
& 0xffffffff) + (result
>> 32);
79 result
+= *(unsigned int *) buff
;
84 result
+= *(unsigned short *) buff
;
91 result
= from64to16(result
);
94 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
103 * This is very ugly but temporary. THIS NEEDS SERIOUS ENHANCEMENTS.
104 * But it's very tricky to get right even in C.
106 extern unsigned long do_csum(const unsigned char *, long);
109 csum_partial_copy_from_user(const void __user
*src
, void *dst
,
110 int len
, __wsum psum
, int *errp
)
112 unsigned long result
;
115 * for now we separate the copy from checksum for obvious
116 * alignment difficulties. Look at the Alpha code and you'll be
120 if (__copy_from_user(dst
, src
, len
) != 0 && errp
)
123 result
= do_csum(dst
, len
);
125 /* add in old sum, and carry.. */
126 result
+= (__force u32
)psum
;
127 /* 32+c bits -> 32 bits */
128 result
= (result
& 0xffffffff) + (result
>> 32);
129 return (__force __wsum
)result
;
132 EXPORT_SYMBOL(csum_partial_copy_from_user
);
135 csum_partial_copy_nocheck(const void *src
, void *dst
, int len
, __wsum sum
)
137 return csum_partial_copy_from_user((__force
const void __user
*)src
,
138 dst
, len
, sum
, NULL
);
141 EXPORT_SYMBOL(csum_partial_copy_nocheck
);