GNU Linux-libre 4.14.266-gnu1
[releases.git] / net / netfilter / nf_nat_proto_sctp.c
1 /*
2  * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 #include <linux/types.h>
10 #include <linux/sctp.h>
11 #include <net/sctp/checksum.h>
12
13 #include <net/netfilter/nf_nat_l4proto.h>
14
15 static void
16 sctp_unique_tuple(const struct nf_nat_l3proto *l3proto,
17                   struct nf_conntrack_tuple *tuple,
18                   const struct nf_nat_range *range,
19                   enum nf_nat_manip_type maniptype,
20                   const struct nf_conn *ct)
21 {
22         nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct);
23 }
24
25 static bool
26 sctp_manip_pkt(struct sk_buff *skb,
27                const struct nf_nat_l3proto *l3proto,
28                unsigned int iphdroff, unsigned int hdroff,
29                const struct nf_conntrack_tuple *tuple,
30                enum nf_nat_manip_type maniptype)
31 {
32         struct sctphdr *hdr;
33         int hdrsize = 8;
34
35         /* This could be an inner header returned in imcp packet; in such
36          * cases we cannot update the checksum field since it is outside
37          * of the 8 bytes of transport layer headers we are guaranteed.
38          */
39         if (skb->len >= hdroff + sizeof(*hdr))
40                 hdrsize = sizeof(*hdr);
41
42         if (!skb_make_writable(skb, hdroff + hdrsize))
43                 return false;
44
45         hdr = (struct sctphdr *)(skb->data + hdroff);
46
47         if (maniptype == NF_NAT_MANIP_SRC) {
48                 /* Get rid of src port */
49                 hdr->source = tuple->src.u.sctp.port;
50         } else {
51                 /* Get rid of dst port */
52                 hdr->dest = tuple->dst.u.sctp.port;
53         }
54
55         if (hdrsize < sizeof(*hdr))
56                 return true;
57
58         if (skb->ip_summed != CHECKSUM_PARTIAL) {
59                 hdr->checksum = sctp_compute_cksum(skb, hdroff);
60                 skb->ip_summed = CHECKSUM_NONE;
61         }
62
63         return true;
64 }
65
66 const struct nf_nat_l4proto nf_nat_l4proto_sctp = {
67         .l4proto                = IPPROTO_SCTP,
68         .manip_pkt              = sctp_manip_pkt,
69         .in_range               = nf_nat_l4proto_in_range,
70         .unique_tuple           = sctp_unique_tuple,
71 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
72         .nlattr_to_range        = nf_nat_l4proto_nlattr_to_range,
73 #endif
74 };