Skip to content

Commit 024ec3d

Browse files
joestringerdavem330
authored andcommitted
net/sctp: Refactor SCTP skb checksum computation
This patch consolidates the SCTP checksum calculation code from various places to a single new function, sctp_compute_cksum(skb, offset). Signed-off-by: Joe Stringer <joe@wand.net.nz> Reviewed-by: Julian Anastasov <ja@ssi.bg> Acked-by: Simon Horman <horms@verge.net.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent e7428e9 commit 024ec3d

File tree

4 files changed

+20
-36
lines changed

4 files changed

+20
-36
lines changed

include/net/sctp/checksum.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,19 @@ static inline __le32 sctp_end_cksum(__u32 crc32)
8585
return cpu_to_le32(~crc32);
8686
}
8787

88+
/* Calculate the CRC32C checksum of an SCTP packet. */
89+
static inline __le32 sctp_compute_cksum(const struct sk_buff *skb,
90+
unsigned int offset)
91+
{
92+
const struct sk_buff *iter;
93+
94+
__u32 crc32 = sctp_start_cksum(skb->data + offset,
95+
skb_headlen(skb) - offset);
96+
skb_walk_frags(skb, iter)
97+
crc32 = sctp_update_cksum((__u8 *) iter->data,
98+
skb_headlen(iter), crc32);
99+
100+
return sctp_end_cksum(crc32);
101+
}
102+
88103
#endif /* __sctp_checksum_h__ */

net/netfilter/ipvs/ip_vs_proto_sctp.c

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,7 @@ sctp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
6666
static void sctp_nat_csum(struct sk_buff *skb, sctp_sctphdr_t *sctph,
6767
unsigned int sctphoff)
6868
{
69-
__u32 crc32;
70-
struct sk_buff *iter;
71-
72-
crc32 = sctp_start_cksum((__u8 *)sctph, skb_headlen(skb) - sctphoff);
73-
skb_walk_frags(skb, iter)
74-
crc32 = sctp_update_cksum((u8 *) iter->data,
75-
skb_headlen(iter), crc32);
76-
sctph->checksum = sctp_end_cksum(crc32);
77-
69+
sctph->checksum = sctp_compute_cksum(skb, sctphoff);
7870
skb->ip_summed = CHECKSUM_UNNECESSARY;
7971
}
8072

@@ -151,10 +143,7 @@ sctp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
151143
{
152144
unsigned int sctphoff;
153145
struct sctphdr *sh, _sctph;
154-
struct sk_buff *iter;
155-
__le32 cmp;
156-
__le32 val;
157-
__u32 tmp;
146+
__le32 cmp, val;
158147

159148
#ifdef CONFIG_IP_VS_IPV6
160149
if (af == AF_INET6)
@@ -168,13 +157,7 @@ sctp_csum_check(int af, struct sk_buff *skb, struct ip_vs_protocol *pp)
168157
return 0;
169158

170159
cmp = sh->checksum;
171-
172-
tmp = sctp_start_cksum((__u8 *) sh, skb_headlen(skb));
173-
skb_walk_frags(skb, iter)
174-
tmp = sctp_update_cksum((__u8 *) iter->data,
175-
skb_headlen(iter), tmp);
176-
177-
val = sctp_end_cksum(tmp);
160+
val = sctp_compute_cksum(skb, sctphoff);
178161

179162
if (val != cmp) {
180163
/* CRC failure, dump it. */

net/netfilter/nf_nat_proto_sctp.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ sctp_manip_pkt(struct sk_buff *skb,
3434
const struct nf_conntrack_tuple *tuple,
3535
enum nf_nat_manip_type maniptype)
3636
{
37-
struct sk_buff *frag;
3837
sctp_sctphdr_t *hdr;
39-
__u32 crc32;
4038

4139
if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
4240
return false;
@@ -51,11 +49,7 @@ sctp_manip_pkt(struct sk_buff *skb,
5149
hdr->dest = tuple->dst.u.sctp.port;
5250
}
5351

54-
crc32 = sctp_start_cksum((u8 *)hdr, skb_headlen(skb) - hdroff);
55-
skb_walk_frags(skb, frag)
56-
crc32 = sctp_update_cksum((u8 *)frag->data, skb_headlen(frag),
57-
crc32);
58-
hdr->checksum = sctp_end_cksum(crc32);
52+
hdr->checksum = sctp_compute_cksum(skb, hdroff);
5953

6054
return true;
6155
}

net/sctp/input.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,7 @@ static inline int sctp_rcv_checksum(struct net *net, struct sk_buff *skb)
8787
{
8888
struct sctphdr *sh = sctp_hdr(skb);
8989
__le32 cmp = sh->checksum;
90-
struct sk_buff *list;
91-
__le32 val;
92-
__u32 tmp = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
93-
94-
skb_walk_frags(skb, list)
95-
tmp = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
96-
tmp);
97-
98-
val = sctp_end_cksum(tmp);
90+
__le32 val = sctp_compute_cksum(skb, 0);
9991

10092
if (val != cmp) {
10193
/* CRC failure, dump it. */

0 commit comments

Comments
 (0)