Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 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): |
Daniel Borkmann | 91705c6 | 2013-07-23 14:51:47 +0200 | [diff] [blame] | 28 | * lksctp developers <linux-sctp@vger.kernel.org> |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 29 | * |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 30 | * Written or modified by: |
| 31 | * Dinakaran Joseph |
| 32 | * Jon Grimm <jgrimm@us.ibm.com> |
| 33 | * Sridhar Samudrala <sri@us.ibm.com> |
| 34 | * |
| 35 | * Rewritten to use libcrc32c by: |
| 36 | * Vlad Yasevich <vladislav.yasevich@hp.com> |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 37 | */ |
| 38 | |
Daniel Borkmann | a05b101 | 2013-07-01 18:10:36 +0200 | [diff] [blame] | 39 | #ifndef __sctp_checksum_h__ |
| 40 | #define __sctp_checksum_h__ |
| 41 | |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 42 | #include <linux/types.h> |
| 43 | #include <net/sctp/sctp.h> |
| 44 | #include <linux/crc32c.h> |
| 45 | |
Vlad Yasevich | 4458f04 | 2009-02-13 08:33:42 +0000 | [diff] [blame] | 46 | static inline __u32 sctp_crc32c(__u32 crc, u8 *buffer, u16 length) |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 47 | { |
Vlad Yasevich | 4458f04 | 2009-02-13 08:33:42 +0000 | [diff] [blame] | 48 | return crc32c(crc, buffer, length); |
Harvey Harrison | 336d326 | 2008-07-18 23:07:09 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Vlad Yasevich | 4458f04 | 2009-02-13 08:33:42 +0000 | [diff] [blame] | 51 | static inline __u32 sctp_start_cksum(__u8 *buffer, __u16 length) |
Harvey Harrison | 336d326 | 2008-07-18 23:07:09 -0700 | [diff] [blame] | 52 | { |
Vlad Yasevich | 4458f04 | 2009-02-13 08:33:42 +0000 | [diff] [blame] | 53 | __u32 crc = ~(__u32)0; |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 54 | __u8 zero[sizeof(__u32)] = {0}; |
| 55 | |
| 56 | /* Optimize this routine to be SCTP specific, knowing how |
| 57 | * to skip the checksum field of the SCTP header. |
| 58 | */ |
| 59 | |
| 60 | /* Calculate CRC up to the checksum. */ |
Harvey Harrison | 336d326 | 2008-07-18 23:07:09 -0700 | [diff] [blame] | 61 | crc = sctp_crc32c(crc, buffer, sizeof(struct sctphdr) - sizeof(__u32)); |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 62 | |
| 63 | /* Skip checksum field of the header. */ |
Harvey Harrison | 336d326 | 2008-07-18 23:07:09 -0700 | [diff] [blame] | 64 | crc = sctp_crc32c(crc, zero, sizeof(__u32)); |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 65 | |
| 66 | /* Calculate the rest of the CRC. */ |
Harvey Harrison | 336d326 | 2008-07-18 23:07:09 -0700 | [diff] [blame] | 67 | crc = sctp_crc32c(crc, &buffer[sizeof(struct sctphdr)], |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 68 | length - sizeof(struct sctphdr)); |
| 69 | return crc; |
| 70 | } |
| 71 | |
Vlad Yasevich | 4458f04 | 2009-02-13 08:33:42 +0000 | [diff] [blame] | 72 | static inline __u32 sctp_update_cksum(__u8 *buffer, __u16 length, __u32 crc32) |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 73 | { |
Harvey Harrison | 336d326 | 2008-07-18 23:07:09 -0700 | [diff] [blame] | 74 | return sctp_crc32c(crc32, buffer, length); |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Simon Horman | eee1d5a | 2013-04-19 10:54:58 +0900 | [diff] [blame] | 77 | static inline __le32 sctp_end_cksum(__u32 crc32) |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 78 | { |
Vlad Yasevich | 4458f04 | 2009-02-13 08:33:42 +0000 | [diff] [blame] | 79 | return cpu_to_le32(~crc32); |
Vlad Yasevich | 9ad0977 | 2007-12-16 14:06:41 -0800 | [diff] [blame] | 80 | } |
Daniel Borkmann | a05b101 | 2013-07-01 18:10:36 +0200 | [diff] [blame] | 81 | |
Joe Stringer | 024ec3d | 2013-07-25 10:52:05 +0900 | [diff] [blame] | 82 | /* Calculate the CRC32C checksum of an SCTP packet. */ |
| 83 | static inline __le32 sctp_compute_cksum(const struct sk_buff *skb, |
| 84 | unsigned int offset) |
| 85 | { |
| 86 | const struct sk_buff *iter; |
| 87 | |
| 88 | __u32 crc32 = sctp_start_cksum(skb->data + offset, |
| 89 | skb_headlen(skb) - offset); |
| 90 | skb_walk_frags(skb, iter) |
| 91 | crc32 = sctp_update_cksum((__u8 *) iter->data, |
| 92 | skb_headlen(iter), crc32); |
| 93 | |
| 94 | return sctp_end_cksum(crc32); |
| 95 | } |
| 96 | |
Daniel Borkmann | a05b101 | 2013-07-01 18:10:36 +0200 | [diff] [blame] | 97 | #endif /* __sctp_checksum_h__ */ |