1 // SPDX-License-Identifier: GPL-2.0
3 * Network checksum routines
5 * Copyright (C) 1999, 2003 Hewlett-Packard Co
6 * Stephane Eranian <eranian@hpl.hp.com>
8 * Most of the code coming from arch/alpha/lib/checksum.c
10 * This file contains network checksum routines that are better done
11 * in an architecture-specific manner due to speed..
14 #include <linux/module.h>
15 #include <linux/string.h>
17 #include <asm/byteorder.h>
19 static inline unsigned short
20 from64to16 (unsigned long x
)
22 /* add up 32-bit words for 33 bits */
23 x
= (x
& 0xffffffff) + (x
>> 32);
24 /* add up 16-bit and 17-bit words for 17+c bits */
25 x
= (x
& 0xffff) + (x
>> 16);
26 /* add up 16-bit and 2-bit for 16+c bit */
27 x
= (x
& 0xffff) + (x
>> 16);
29 x
= (x
& 0xffff) + (x
>> 16);
34 * computes the checksum of the TCP/UDP pseudo-header
35 * returns a 16-bit checksum, already complemented.
38 csum_tcpudp_magic(__be32 saddr
, __be32 daddr
, __u32 len
,
39 __u8 proto
, __wsum sum
)
41 return (__force __sum16
)~from64to16(
42 (__force u64
)saddr
+ (__force u64
)daddr
+
43 (__force u64
)sum
+ ((len
+ proto
) << 8));
46 EXPORT_SYMBOL(csum_tcpudp_magic
);
49 csum_tcpudp_nofold(__be32 saddr
, __be32 daddr
, __u32 len
,
50 __u8 proto
, __wsum sum
)
54 result
= (__force u64
)saddr
+ (__force u64
)daddr
+
55 (__force u64
)sum
+ ((len
+ proto
) << 8);
57 /* Fold down to 32-bits so we don't lose in the typedef-less network stack. */
59 result
= (result
& 0xffffffff) + (result
>> 32);
61 result
= (result
& 0xffffffff) + (result
>> 32);
62 return (__force __wsum
)result
;
64 EXPORT_SYMBOL(csum_tcpudp_nofold
);
66 extern unsigned long do_csum (const unsigned char *, long);
69 * computes the checksum of a memory block at buff, length len,
70 * and adds in "sum" (32-bit)
72 * returns a 32-bit number suitable for feeding into itself
73 * or csum_tcpudp_magic
75 * this function must be called with even lengths, except
76 * for the last fragment, which may be odd
78 * it's best to have buff aligned on a 32-bit boundary
80 __wsum
csum_partial(const void *buff
, int len
, __wsum sum
)
82 u64 result
= do_csum(buff
, len
);
84 /* add in old sum, and carry.. */
85 result
+= (__force u32
)sum
;
86 /* 32+c bits -> 32 bits */
87 result
= (result
& 0xffffffff) + (result
>> 32);
88 return (__force __wsum
)result
;
91 EXPORT_SYMBOL(csum_partial
);
94 * this routine is used for miscellaneous IP-like checksums, mainly
97 __sum16
ip_compute_csum (const void *buff
, int len
)
99 return (__force __sum16
)~do_csum(buff
,len
);
102 EXPORT_SYMBOL(ip_compute_csum
);