1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2016 ARM Ltd.
4 * Copyright (C) 2023 Loongson Technology Corporation Limited
6 #ifndef __ASM_CHECKSUM_H
7 #define __ASM_CHECKSUM_H
9 #include <linux/bitops.h>
10 #include <linux/in6.h>
12 #define _HAVE_ARCH_IPV6_CSUM
13 __sum16
csum_ipv6_magic(const struct in6_addr
*saddr
,
14 const struct in6_addr
*daddr
,
15 __u32 len
, __u8 proto
, __wsum sum
);
18 * turns a 32-bit partial checksum (e.g. from csum_partial) into a
19 * 1's complement 16-bit checksum.
21 static inline __sum16
csum_fold(__wsum sum
)
23 u32 tmp
= (__force u32
)sum
;
26 * swap the two 16-bit halves of sum
27 * if there is a carry from adding the two 16-bit halves,
28 * it will carry from the lower half into the upper half,
29 * giving us the correct sum in the upper half.
31 return (__force __sum16
)(~(tmp
+ rol32(tmp
, 16)) >> 16);
33 #define csum_fold csum_fold
36 * This is a version of ip_compute_csum() optimized for IP headers,
37 * which always checksum on 4 octet boundaries. ihl is the number
38 * of 32-bit words and is always >= 5.
40 static inline __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
)
44 int n
= ihl
; /* we want it signed */
46 tmp
= *(const __uint128_t
*)iph
;
49 tmp
+= ((tmp
>> 64) | (tmp
<< 64));
52 sum
+= *(const u32
*)iph
;
56 sum
+= ror64(sum
, 32);
57 return csum_fold((__force __wsum
)(sum
>> 32));
59 #define ip_fast_csum ip_fast_csum
61 extern unsigned int do_csum(const unsigned char *buff
, int len
);
62 #define do_csum do_csum
64 #include <asm-generic/checksum.h>
66 #endif /* __ASM_CHECKSUM_H */