GNU Linux-libre 4.9.309-gnu1
[releases.git] / net / netfilter / nf_nat_proto_tcp.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
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/export.h>
12 #include <linux/tcp.h>
13
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nfnetlink_conntrack.h>
16 #include <net/netfilter/nf_nat.h>
17 #include <net/netfilter/nf_nat_l3proto.h>
18 #include <net/netfilter/nf_nat_l4proto.h>
19 #include <net/netfilter/nf_nat_core.h>
20
21 static void
22 tcp_unique_tuple(const struct nf_nat_l3proto *l3proto,
23                  struct nf_conntrack_tuple *tuple,
24                  const struct nf_nat_range *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 tcp_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 tcphdr *hdr;
39         __be16 *portptr, newport, oldport;
40         int hdrsize = 8; /* TCP connection tracking guarantees this much */
41
42         /* this could be a inner header returned in icmp packet; in such
43            cases we cannot update the checksum field since it is outside of
44            the 8 bytes of transport layer headers we are guaranteed */
45         if (skb->len >= hdroff + sizeof(struct tcphdr))
46                 hdrsize = sizeof(struct tcphdr);
47
48         if (!skb_make_writable(skb, hdroff + hdrsize))
49                 return false;
50
51         hdr = (struct tcphdr *)(skb->data + hdroff);
52
53         if (maniptype == NF_NAT_MANIP_SRC) {
54                 /* Get rid of src port */
55                 newport = tuple->src.u.tcp.port;
56                 portptr = &hdr->source;
57         } else {
58                 /* Get rid of dst port */
59                 newport = tuple->dst.u.tcp.port;
60                 portptr = &hdr->dest;
61         }
62
63         oldport = *portptr;
64         *portptr = newport;
65
66         if (hdrsize < sizeof(*hdr))
67                 return true;
68
69         l3proto->csum_update(skb, iphdroff, &hdr->check, tuple, maniptype);
70         inet_proto_csum_replace2(&hdr->check, skb, oldport, newport, false);
71         return true;
72 }
73
74 const struct nf_nat_l4proto nf_nat_l4proto_tcp = {
75         .l4proto                = IPPROTO_TCP,
76         .manip_pkt              = tcp_manip_pkt,
77         .in_range               = nf_nat_l4proto_in_range,
78         .unique_tuple           = tcp_unique_tuple,
79 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
80         .nlattr_to_range        = nf_nat_l4proto_nlattr_to_range,
81 #endif
82 };