GNU Linux-libre 4.9.337-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/init.h>
11 #include <linux/sctp.h>
12 #include <linux/module.h>
13 #include <net/sctp/checksum.h>
14
15 #include <net/netfilter/nf_nat_l4proto.h>
16
17 static void
18 sctp_unique_tuple(const struct nf_nat_l3proto *l3proto,
19                   struct nf_conntrack_tuple *tuple,
20                   const struct nf_nat_range *range,
21                   enum nf_nat_manip_type maniptype,
22                   const struct nf_conn *ct)
23 {
24         nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct);
25 }
26
27 static bool
28 sctp_manip_pkt(struct sk_buff *skb,
29                const struct nf_nat_l3proto *l3proto,
30                unsigned int iphdroff, unsigned int hdroff,
31                const struct nf_conntrack_tuple *tuple,
32                enum nf_nat_manip_type maniptype)
33 {
34         sctp_sctphdr_t *hdr;
35
36         if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
37                 return false;
38
39         hdr = (struct sctphdr *)(skb->data + hdroff);
40
41         if (maniptype == NF_NAT_MANIP_SRC) {
42                 /* Get rid of src port */
43                 hdr->source = tuple->src.u.sctp.port;
44         } else {
45                 /* Get rid of dst port */
46                 hdr->dest = tuple->dst.u.sctp.port;
47         }
48
49         hdr->checksum = sctp_compute_cksum(skb, hdroff);
50
51         return true;
52 }
53
54 static const struct nf_nat_l4proto nf_nat_l4proto_sctp = {
55         .l4proto                = IPPROTO_SCTP,
56         .manip_pkt              = sctp_manip_pkt,
57         .in_range               = nf_nat_l4proto_in_range,
58         .unique_tuple           = sctp_unique_tuple,
59 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
60         .nlattr_to_range        = nf_nat_l4proto_nlattr_to_range,
61 #endif
62 };
63
64 static int __init nf_nat_proto_sctp_init(void)
65 {
66         int err;
67
68         err = nf_nat_l4proto_register(NFPROTO_IPV4, &nf_nat_l4proto_sctp);
69         if (err < 0)
70                 goto err1;
71         err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_sctp);
72         if (err < 0)
73                 goto err2;
74         return 0;
75
76 err2:
77         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_sctp);
78 err1:
79         return err;
80 }
81
82 static void __exit nf_nat_proto_sctp_exit(void)
83 {
84         nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_sctp);
85         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_sctp);
86 }
87
88 module_init(nf_nat_proto_sctp_init);
89 module_exit(nf_nat_proto_sctp_exit);
90
91 MODULE_LICENSE("GPL");
92 MODULE_DESCRIPTION("SCTP NAT protocol helper");
93 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");