1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) IBM Corporation, 2010
6 * Author: Anton Blanchard <anton@au.ibm.com>
8 #include <linux/export.h>
9 #include <linux/compiler.h>
10 #include <linux/types.h>
11 #include <asm/checksum.h>
12 #include <linux/uaccess.h>
14 __wsum
csum_and_copy_from_user(const void __user
*src
, void *dst
,
15 int len
, __wsum sum
, int *err_ptr
)
20 allow_read_from_user(src
, len
);
29 if (unlikely((len
< 0) || !access_ok(src
, len
))) {
31 csum
= (__force
unsigned int)sum
;
35 csum
= csum_partial_copy_generic((void __force
*)src
, dst
,
36 len
, sum
, err_ptr
, NULL
);
38 if (unlikely(*err_ptr
)) {
39 int missing
= __copy_from_user(dst
, src
, len
);
42 memset(dst
+ len
- missing
, 0, missing
);
48 csum
= csum_partial(dst
, len
, sum
);
52 prevent_read_from_user(src
, len
);
53 return (__force __wsum
)csum
;
55 EXPORT_SYMBOL(csum_and_copy_from_user
);
57 __wsum
csum_and_copy_to_user(const void *src
, void __user
*dst
, int len
,
58 __wsum sum
, int *err_ptr
)
63 allow_write_to_user(dst
, len
);
72 if (unlikely((len
< 0) || !access_ok(dst
, len
))) {
74 csum
= -1; /* invalid checksum */
78 csum
= csum_partial_copy_generic(src
, (void __force
*)dst
,
79 len
, sum
, NULL
, err_ptr
);
81 if (unlikely(*err_ptr
)) {
82 csum
= csum_partial(src
, len
, sum
);
84 if (copy_to_user(dst
, src
, len
)) {
86 csum
= -1; /* invalid checksum */
91 prevent_write_to_user(dst
, len
);
92 return (__force __wsum
)csum
;
94 EXPORT_SYMBOL(csum_and_copy_to_user
);