Skip to content

Commit 336d326

Browse files
hharrisondavem330
authored andcommitted
sctp: remove unnecessary byteshifting, calculate directly in big-endian
Signed-off-by: Harvey Harrison <[email protected]> Signed-off-by: Vlad Yasevich <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 4e54064 commit 336d326

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

include/net/sctp/checksum.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,38 @@
4646
#include <net/sctp/sctp.h>
4747
#include <linux/crc32c.h>
4848

49-
static inline __u32 sctp_start_cksum(__u8 *buffer, __u16 length)
49+
static inline __be32 sctp_crc32c(__be32 crc, u8 *buffer, u16 length)
5050
{
51-
__u32 crc = ~(__u32) 0;
51+
return (__force __be32)crc32c((__force u32)crc, buffer, length);
52+
}
53+
54+
static inline __be32 sctp_start_cksum(__u8 *buffer, __u16 length)
55+
{
56+
__be32 crc = ~cpu_to_be32(0);
5257
__u8 zero[sizeof(__u32)] = {0};
5358

5459
/* Optimize this routine to be SCTP specific, knowing how
5560
* to skip the checksum field of the SCTP header.
5661
*/
5762

5863
/* Calculate CRC up to the checksum. */
59-
crc = crc32c(crc, buffer, sizeof(struct sctphdr) - sizeof(__u32));
64+
crc = sctp_crc32c(crc, buffer, sizeof(struct sctphdr) - sizeof(__u32));
6065

6166
/* Skip checksum field of the header. */
62-
crc = crc32c(crc, zero, sizeof(__u32));
67+
crc = sctp_crc32c(crc, zero, sizeof(__u32));
6368

6469
/* Calculate the rest of the CRC. */
65-
crc = crc32c(crc, &buffer[sizeof(struct sctphdr)],
70+
crc = sctp_crc32c(crc, &buffer[sizeof(struct sctphdr)],
6671
length - sizeof(struct sctphdr));
6772
return crc;
6873
}
6974

70-
static inline __u32 sctp_update_cksum(__u8 *buffer, __u16 length, __u32 crc32)
75+
static inline __be32 sctp_update_cksum(__u8 *buffer, __u16 length, __be32 crc32)
7176
{
72-
return crc32c(crc32, buffer, length);
77+
return sctp_crc32c(crc32, buffer, length);
7378
}
7479

75-
static inline __u32 sctp_end_cksum(__u32 crc32)
80+
static inline __be32 sctp_end_cksum(__be32 crc32)
7681
{
77-
return ntohl(~crc32);
82+
return ~crc32;
7883
}

net/ipv4/netfilter/nf_nat_proto_sctp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ sctp_manip_pkt(struct sk_buff *skb,
3636
sctp_sctphdr_t *hdr;
3737
unsigned int hdroff = iphdroff + iph->ihl*4;
3838
__be32 oldip, newip;
39-
u32 crc32;
39+
__be32 crc32;
4040

4141
if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
4242
return false;
@@ -61,7 +61,7 @@ sctp_manip_pkt(struct sk_buff *skb,
6161
crc32 = sctp_update_cksum((u8 *)skb->data, skb_headlen(skb),
6262
crc32);
6363
crc32 = sctp_end_cksum(crc32);
64-
hdr->checksum = htonl(crc32);
64+
hdr->checksum = crc32;
6565

6666
return true;
6767
}

net/sctp/input.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ static inline int sctp_rcv_checksum(struct sk_buff *skb)
8383
{
8484
struct sk_buff *list = skb_shinfo(skb)->frag_list;
8585
struct sctphdr *sh = sctp_hdr(skb);
86-
__u32 cmp = ntohl(sh->checksum);
87-
__u32 val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
86+
__be32 cmp = sh->checksum;
87+
__be32 val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
8888

8989
for (; list; list = list->next)
9090
val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),

net/sctp/output.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ int sctp_packet_transmit(struct sctp_packet *packet)
365365
struct sctp_transport *tp = packet->transport;
366366
struct sctp_association *asoc = tp->asoc;
367367
struct sctphdr *sh;
368-
__u32 crc32 = 0;
368+
__be32 crc32 = __constant_cpu_to_be32(0);
369369
struct sk_buff *nskb;
370370
struct sctp_chunk *chunk, *tmp;
371371
struct sock *sk;
@@ -538,7 +538,7 @@ int sctp_packet_transmit(struct sctp_packet *packet)
538538
/* 3) Put the resultant value into the checksum field in the
539539
* common header, and leave the rest of the bits unchanged.
540540
*/
541-
sh->checksum = htonl(crc32);
541+
sh->checksum = crc32;
542542

543543
/* IP layer ECN support
544544
* From RFC 2481

0 commit comments

Comments
 (0)