GNU Linux-libre 4.19.264-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/skbuff.h>
14 #include <linux/dccp.h>
15
16 #include <net/netfilter/nf_conntrack.h>
17 #include <net/netfilter/nf_nat.h>
18 #include <net/netfilter/nf_nat_l3proto.h>
19 #include <net/netfilter/nf_nat_l4proto.h>
20
21 static void
22 dccp_unique_tuple(const struct nf_nat_l3proto *l3proto,
23                   struct nf_conntrack_tuple *tuple,
24                   const struct nf_nat_range2 *range,
25                   enum nf_nat_manip_type maniptype,
26                   const struct nf_conn *ct)
27 {
28         nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct);
29 }
30
31 static bool
32 dccp_manip_pkt(struct sk_buff *skb,
33                const struct nf_nat_l3proto *l3proto,
34                unsigned int iphdroff, unsigned int hdroff,
35                const struct nf_conntrack_tuple *tuple,
36                enum nf_nat_manip_type maniptype)
37 {
38         struct dccp_hdr *hdr;
39         __be16 *portptr, oldport, newport;
40         int hdrsize = 8; /* DCCP connection tracking guarantees this much */
41
42         if (skb->len >= hdroff + sizeof(struct dccp_hdr))
43                 hdrsize = sizeof(struct dccp_hdr);
44
45         if (!skb_make_writable(skb, hdroff + hdrsize))
46                 return false;
47
48         hdr = (struct dccp_hdr *)(skb->data + hdroff);
49
50         if (maniptype == NF_NAT_MANIP_SRC) {
51                 newport = tuple->src.u.dccp.port;
52                 portptr = &hdr->dccph_sport;
53         } else {
54                 newport = tuple->dst.u.dccp.port;
55                 portptr = &hdr->dccph_dport;
56         }
57
58         oldport = *portptr;
59         *portptr = newport;
60
61         if (hdrsize < sizeof(*hdr))
62                 return true;
63
64         l3proto->csum_update(skb, iphdroff, &hdr->dccph_checksum,
65                              tuple, maniptype);
66         inet_proto_csum_replace2(&hdr->dccph_checksum, skb, oldport, newport,
67                                  false);
68         return true;
69 }
70
71 const struct nf_nat_l4proto nf_nat_l4proto_dccp = {
72         .l4proto                = IPPROTO_DCCP,
73         .manip_pkt              = dccp_manip_pkt,
74         .in_range               = nf_nat_l4proto_in_range,
75         .unique_tuple           = dccp_unique_tuple,
76 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
77         .nlattr_to_range        = nf_nat_l4proto_nlattr_to_range,
78 #endif
79 };