xref: /openbmc/linux/include/net/sctp/checksum.h (revision 273160ff)
19ad0977fSVlad Yasevich /* SCTP kernel reference Implementation
29ad0977fSVlad Yasevich  * Copyright (c) 1999-2001 Motorola, Inc.
39ad0977fSVlad Yasevich  * Copyright (c) 2001-2003 International Business Machines, Corp.
49ad0977fSVlad Yasevich  *
59ad0977fSVlad Yasevich  * This file is part of the SCTP kernel reference Implementation
69ad0977fSVlad Yasevich  *
79ad0977fSVlad Yasevich  * SCTP Checksum functions
89ad0977fSVlad Yasevich  *
99ad0977fSVlad Yasevich  * The SCTP reference implementation is free software;
109ad0977fSVlad Yasevich  * you can redistribute it and/or modify it under the terms of
119ad0977fSVlad Yasevich  * the GNU General Public License as published by
129ad0977fSVlad Yasevich  * the Free Software Foundation; either version 2, or (at your option)
139ad0977fSVlad Yasevich  * any later version.
149ad0977fSVlad Yasevich  *
159ad0977fSVlad Yasevich  * The SCTP reference implementation is distributed in the hope that it
169ad0977fSVlad Yasevich  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
179ad0977fSVlad Yasevich  *                 ************************
189ad0977fSVlad Yasevich  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
199ad0977fSVlad Yasevich  * See the GNU General Public License for more details.
209ad0977fSVlad Yasevich  *
219ad0977fSVlad Yasevich  * You should have received a copy of the GNU General Public License
22a6227e26SJeff Kirsher  * along with GNU CC; see the file COPYING.  If not, see
23a6227e26SJeff Kirsher  * <http://www.gnu.org/licenses/>.
249ad0977fSVlad Yasevich  *
259ad0977fSVlad Yasevich  * Please send any bug reports or fixes you make to the
269ad0977fSVlad Yasevich  * email address(es):
2791705c61SDaniel Borkmann  *    lksctp developers <linux-sctp@vger.kernel.org>
289ad0977fSVlad Yasevich  *
299ad0977fSVlad Yasevich  * Written or modified by:
309ad0977fSVlad Yasevich  *    Dinakaran Joseph
319ad0977fSVlad Yasevich  *    Jon Grimm <jgrimm@us.ibm.com>
329ad0977fSVlad Yasevich  *    Sridhar Samudrala <sri@us.ibm.com>
339ad0977fSVlad Yasevich  *
349ad0977fSVlad Yasevich  * Rewritten to use libcrc32c by:
359ad0977fSVlad Yasevich  *    Vlad Yasevich <vladislav.yasevich@hp.com>
369ad0977fSVlad Yasevich  */
379ad0977fSVlad Yasevich 
38a05b1019SDaniel Borkmann #ifndef __sctp_checksum_h__
39a05b1019SDaniel Borkmann #define __sctp_checksum_h__
40a05b1019SDaniel Borkmann 
419ad0977fSVlad Yasevich #include <linux/types.h>
429ad0977fSVlad Yasevich #include <net/sctp/sctp.h>
439ad0977fSVlad Yasevich #include <linux/crc32c.h>
44e6d8b64bSDaniel Borkmann #include <linux/crc32.h>
459ad0977fSVlad Yasevich 
46e6d8b64bSDaniel Borkmann static inline __wsum sctp_csum_update(const void *buff, int len, __wsum sum)
479ad0977fSVlad Yasevich {
48e6d8b64bSDaniel Borkmann 	/* This uses the crypto implementation of crc32c, which is either
49e6d8b64bSDaniel Borkmann 	 * implemented w/ hardware support or resolves to __crc32c_le().
509ad0977fSVlad Yasevich 	 */
51af2697a0SXin Long 	return (__force __wsum)crc32c((__force __u32)sum, buff, len);
529ad0977fSVlad Yasevich }
539ad0977fSVlad Yasevich 
54e6d8b64bSDaniel Borkmann static inline __wsum sctp_csum_combine(__wsum csum, __wsum csum2,
55e6d8b64bSDaniel Borkmann 				       int offset, int len)
569ad0977fSVlad Yasevich {
57af2697a0SXin Long 	return (__force __wsum)__crc32c_le_combine((__force __u32)csum,
58af2697a0SXin Long 						   (__force __u32)csum2, len);
599ad0977fSVlad Yasevich }
609ad0977fSVlad Yasevich 
61024ec3deSJoe Stringer static inline __le32 sctp_compute_cksum(const struct sk_buff *skb,
62024ec3deSJoe Stringer 					unsigned int offset)
63024ec3deSJoe Stringer {
64273160ffSXin Long 	struct sctphdr *sh = (struct sctphdr *)(skb->data + offset);
65e6d8b64bSDaniel Borkmann 	const struct skb_checksum_ops ops = {
66e6d8b64bSDaniel Borkmann 		.update  = sctp_csum_update,
67e6d8b64bSDaniel Borkmann 		.combine = sctp_csum_combine,
68e6d8b64bSDaniel Borkmann 	};
69af2697a0SXin Long 	__le32 old = sh->checksum;
70af2697a0SXin Long 	__wsum new;
71024ec3deSJoe Stringer 
72e6d8b64bSDaniel Borkmann 	sh->checksum = 0;
73af2697a0SXin Long 	new = ~__skb_checksum(skb, offset, skb->len - offset, ~(__wsum)0, &ops);
74e6d8b64bSDaniel Borkmann 	sh->checksum = old;
75024ec3deSJoe Stringer 
76af2697a0SXin Long 	return cpu_to_le32((__force __u32)new);
77024ec3deSJoe Stringer }
78024ec3deSJoe Stringer 
79a05b1019SDaniel Borkmann #endif /* __sctp_checksum_h__ */
80