Loading...
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/* SCTP kernel reference Implementation
3 * Copyright (c) 1999-2001 Motorola, Inc.
4 * Copyright (c) 2001-2003 International Business Machines, Corp.
5 *
6 * This file is part of the SCTP kernel reference Implementation
7 *
8 * SCTP Checksum functions
9 *
10 * Please send any bug reports or fixes you make to the
11 * email address(es):
12 * lksctp developers <linux-sctp@vger.kernel.org>
13 *
14 * Written or modified by:
15 * Dinakaran Joseph
16 * Jon Grimm <jgrimm@us.ibm.com>
17 * Sridhar Samudrala <sri@us.ibm.com>
18 *
19 * Rewritten to use libcrc32c by:
20 * Vlad Yasevich <vladislav.yasevich@hp.com>
21 */
22
23#ifndef __sctp_checksum_h__
24#define __sctp_checksum_h__
25
26#include <linux/types.h>
27#include <net/sctp/sctp.h>
28#include <linux/crc32c.h>
29#include <linux/crc32.h>
30
31static inline __wsum sctp_csum_update(const void *buff, int len, __wsum sum)
32{
33 /* This uses the crypto implementation of crc32c, which is either
34 * implemented w/ hardware support or resolves to __crc32c_le().
35 */
36 return (__force __wsum)crc32c((__force __u32)sum, buff, len);
37}
38
39static inline __wsum sctp_csum_combine(__wsum csum, __wsum csum2,
40 int offset, int len)
41{
42 return (__force __wsum)__crc32c_le_combine((__force __u32)csum,
43 (__force __u32)csum2, len);
44}
45
46static const struct skb_checksum_ops sctp_csum_ops = {
47 .update = sctp_csum_update,
48 .combine = sctp_csum_combine,
49};
50
51static inline __le32 sctp_compute_cksum(const struct sk_buff *skb,
52 unsigned int offset)
53{
54 struct sctphdr *sh = (struct sctphdr *)(skb->data + offset);
55 __le32 old = sh->checksum;
56 __wsum new;
57
58 sh->checksum = 0;
59 new = ~__skb_checksum(skb, offset, skb->len - offset, ~(__wsum)0,
60 &sctp_csum_ops);
61 sh->checksum = old;
62
63 return cpu_to_le32((__force __u32)new);
64}
65
66#endif /* __sctp_checksum_h__ */
1/* SCTP kernel reference Implementation
2 * Copyright (c) 1999-2001 Motorola, Inc.
3 * Copyright (c) 2001-2003 International Business Machines, Corp.
4 *
5 * This file is part of the SCTP kernel reference Implementation
6 *
7 * SCTP Checksum functions
8 *
9 * The SCTP reference implementation is free software;
10 * you can redistribute it and/or modify it under the terms of
11 * the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * The SCTP reference implementation is distributed in the hope that it
16 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
17 * ************************
18 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 * See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with GNU CC; see the file COPYING. If not, write to
23 * the Free Software Foundation, 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
25 *
26 * Please send any bug reports or fixes you make to the
27 * email address(es):
28 * lksctp developers <lksctp-developers@lists.sourceforge.net>
29 *
30 * Or submit a bug report through the following website:
31 * http://www.sf.net/projects/lksctp
32 *
33 * Written or modified by:
34 * Dinakaran Joseph
35 * Jon Grimm <jgrimm@us.ibm.com>
36 * Sridhar Samudrala <sri@us.ibm.com>
37 *
38 * Rewritten to use libcrc32c by:
39 * Vlad Yasevich <vladislav.yasevich@hp.com>
40 *
41 * Any bugs reported given to us we will try to fix... any fixes shared will
42 * be incorporated into the next SCTP release.
43 */
44
45#include <linux/types.h>
46#include <net/sctp/sctp.h>
47#include <linux/crc32c.h>
48
49static inline __u32 sctp_crc32c(__u32 crc, u8 *buffer, u16 length)
50{
51 return crc32c(crc, buffer, length);
52}
53
54static inline __u32 sctp_start_cksum(__u8 *buffer, __u16 length)
55{
56 __u32 crc = ~(__u32)0;
57 __u8 zero[sizeof(__u32)] = {0};
58
59 /* Optimize this routine to be SCTP specific, knowing how
60 * to skip the checksum field of the SCTP header.
61 */
62
63 /* Calculate CRC up to the checksum. */
64 crc = sctp_crc32c(crc, buffer, sizeof(struct sctphdr) - sizeof(__u32));
65
66 /* Skip checksum field of the header. */
67 crc = sctp_crc32c(crc, zero, sizeof(__u32));
68
69 /* Calculate the rest of the CRC. */
70 crc = sctp_crc32c(crc, &buffer[sizeof(struct sctphdr)],
71 length - sizeof(struct sctphdr));
72 return crc;
73}
74
75static inline __u32 sctp_update_cksum(__u8 *buffer, __u16 length, __u32 crc32)
76{
77 return sctp_crc32c(crc32, buffer, length);
78}
79
80static inline __le32 sctp_end_cksum(__be32 crc32)
81{
82 return cpu_to_le32(~crc32);
83}