GNU Linux-libre 4.9.309-gnu1
[releases.git] / net / netfilter / nf_nat_proto_dccp.c
1 /*
2  * DCCP NAT protocol helper
3  *
4  * Copyright (c) 2005, 2006, 2008 Patrick McHardy <kaber@trash.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/skbuff.h>
16 #include <linux/dccp.h>
17
18 #include <net/netfilter/nf_conntrack.h>
19 #include <net/netfilter/nf_nat.h>
20 #include <net/netfilter/nf_nat_l3proto.h>
21 #include <net/netfilter/nf_nat_l4proto.h>
22
23 static void
24 dccp_unique_tuple(const struct nf_nat_l3proto *l3proto,
25                   struct nf_conntrack_tuple *tuple,
26                   const struct nf_nat_range *range,
27                   enum nf_nat_manip_type maniptype,
28                   const struct nf_conn *ct)
29 {
30         nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct);
31 }
32
33 static bool
34 dccp_manip_pkt(struct sk_buff *skb,
35                const struct nf_nat_l3proto *l3proto,
36                unsigned int iphdroff, unsigned int hdroff,
37                const struct nf_conntrack_tuple *tuple,
38                enum nf_nat_manip_type maniptype)
39 {
40         struct dccp_hdr *hdr;
41         __be16 *portptr, oldport, newport;
42         int hdrsize = 8; /* DCCP connection tracking guarantees this much */
43
44         if (skb->len >= hdroff + sizeof(struct dccp_hdr))
45                 hdrsize = sizeof(struct dccp_hdr);
46
47         if (!skb_make_writable(skb, hdroff + hdrsize))
48                 return false;
49
50         hdr = (struct dccp_hdr *)(skb->data + hdroff);
51
52         if (maniptype == NF_NAT_MANIP_SRC) {
53                 newport = tuple->src.u.dccp.port;
54                 portptr = &hdr->dccph_sport;
55         } else {
56                 newport = tuple->dst.u.dccp.port;
57                 portptr = &hdr->dccph_dport;
58         }
59
60         oldport = *portptr;
61         *portptr = newport;
62
63         if (hdrsize < sizeof(*hdr))
64                 return true;
65
66         l3proto->csum_update(skb, iphdroff, &hdr->dccph_checksum,
67                              tuple, maniptype);
68         inet_proto_csum_replace2(&hdr->dccph_checksum, skb, oldport, newport,
69                                  false);
70         return true;
71 }
72
73 static const struct nf_nat_l4proto nf_nat_l4proto_dccp = {
74         .l4proto                = IPPROTO_DCCP,
75         .manip_pkt              = dccp_manip_pkt,
76         .in_range               = nf_nat_l4proto_in_range,
77         .unique_tuple           = dccp_unique_tuple,
78 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
79         .nlattr_to_range        = nf_nat_l4proto_nlattr_to_range,
80 #endif
81 };
82
83 static int __init nf_nat_proto_dccp_init(void)
84 {
85         int err;
86
87         err = nf_nat_l4proto_register(NFPROTO_IPV4, &nf_nat_l4proto_dccp);
88         if (err < 0)
89                 goto err1;
90         err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_dccp);
91         if (err < 0)
92                 goto err2;
93         return 0;
94
95 err2:
96         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_dccp);
97 err1:
98         return err;
99 }
100
101 static void __exit nf_nat_proto_dccp_fini(void)
102 {
103         nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_dccp);
104         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_dccp);
105
106 }
107
108 module_init(nf_nat_proto_dccp_init);
109 module_exit(nf_nat_proto_dccp_fini);
110
111 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
112 MODULE_DESCRIPTION("DCCP NAT protocol helper");
113 MODULE_LICENSE("GPL");