xref: /openbmc/linux/include/net/sctp/checksum.h (revision b78c4162)
11924af04SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
29ad0977fSVlad Yasevich /* SCTP kernel reference Implementation
39ad0977fSVlad Yasevich  * Copyright (c) 1999-2001 Motorola, Inc.
49ad0977fSVlad Yasevich  * Copyright (c) 2001-2003 International Business Machines, Corp.
59ad0977fSVlad Yasevich  *
69ad0977fSVlad Yasevich  * This file is part of the SCTP kernel reference Implementation
79ad0977fSVlad Yasevich  *
89ad0977fSVlad Yasevich  * SCTP Checksum functions
99ad0977fSVlad Yasevich  *
109ad0977fSVlad Yasevich  * Please send any bug reports or fixes you make to the
119ad0977fSVlad Yasevich  * email address(es):
1291705c61SDaniel Borkmann  *    lksctp developers <linux-sctp@vger.kernel.org>
139ad0977fSVlad Yasevich  *
149ad0977fSVlad Yasevich  * Written or modified by:
159ad0977fSVlad Yasevich  *    Dinakaran Joseph
169ad0977fSVlad Yasevich  *    Jon Grimm <jgrimm@us.ibm.com>
179ad0977fSVlad Yasevich  *    Sridhar Samudrala <sri@us.ibm.com>
189ad0977fSVlad Yasevich  *
199ad0977fSVlad Yasevich  * Rewritten to use libcrc32c by:
209ad0977fSVlad Yasevich  *    Vlad Yasevich <vladislav.yasevich@hp.com>
219ad0977fSVlad Yasevich  */
229ad0977fSVlad Yasevich 
23a05b1019SDaniel Borkmann #ifndef __sctp_checksum_h__
24a05b1019SDaniel Borkmann #define __sctp_checksum_h__
25a05b1019SDaniel Borkmann 
269ad0977fSVlad Yasevich #include <linux/types.h>
27*b78c4162SXin Long #include <linux/sctp.h>
289ad0977fSVlad Yasevich #include <linux/crc32c.h>
29e6d8b64bSDaniel Borkmann #include <linux/crc32.h>
309ad0977fSVlad Yasevich 
sctp_csum_update(const void * buff,int len,__wsum sum)31e6d8b64bSDaniel Borkmann static inline __wsum sctp_csum_update(const void *buff, int len, __wsum sum)
329ad0977fSVlad Yasevich {
33e6d8b64bSDaniel Borkmann 	/* This uses the crypto implementation of crc32c, which is either
34e6d8b64bSDaniel Borkmann 	 * implemented w/ hardware support or resolves to __crc32c_le().
359ad0977fSVlad Yasevich 	 */
36af2697a0SXin Long 	return (__force __wsum)crc32c((__force __u32)sum, buff, len);
379ad0977fSVlad Yasevich }
389ad0977fSVlad Yasevich 
sctp_csum_combine(__wsum csum,__wsum csum2,int offset,int len)39e6d8b64bSDaniel Borkmann static inline __wsum sctp_csum_combine(__wsum csum, __wsum csum2,
40e6d8b64bSDaniel Borkmann 				       int offset, int len)
419ad0977fSVlad Yasevich {
42af2697a0SXin Long 	return (__force __wsum)__crc32c_le_combine((__force __u32)csum,
43af2697a0SXin Long 						   (__force __u32)csum2, len);
449ad0977fSVlad Yasevich }
459ad0977fSVlad Yasevich 
46c3e933a5SMatteo Croce static const struct skb_checksum_ops sctp_csum_ops = {
47c3e933a5SMatteo Croce 	.update  = sctp_csum_update,
48c3e933a5SMatteo Croce 	.combine = sctp_csum_combine,
49c3e933a5SMatteo Croce };
50c3e933a5SMatteo Croce 
sctp_compute_cksum(const struct sk_buff * skb,unsigned int offset)51024ec3deSJoe Stringer static inline __le32 sctp_compute_cksum(const struct sk_buff *skb,
52024ec3deSJoe Stringer 					unsigned int offset)
53024ec3deSJoe Stringer {
54273160ffSXin Long 	struct sctphdr *sh = (struct sctphdr *)(skb->data + offset);
55af2697a0SXin Long 	__le32 old = sh->checksum;
56af2697a0SXin Long 	__wsum new;
57024ec3deSJoe Stringer 
58e6d8b64bSDaniel Borkmann 	sh->checksum = 0;
59c3e933a5SMatteo Croce 	new = ~__skb_checksum(skb, offset, skb->len - offset, ~(__wsum)0,
60c3e933a5SMatteo Croce 			      &sctp_csum_ops);
61e6d8b64bSDaniel Borkmann 	sh->checksum = old;
62024ec3deSJoe Stringer 
63af2697a0SXin Long 	return cpu_to_le32((__force __u32)new);
64024ec3deSJoe Stringer }
65024ec3deSJoe Stringer 
66a05b1019SDaniel Borkmann #endif /* __sctp_checksum_h__ */
67