blob: 0d67bb80130f019b48134fb675029b265d825f48 [file] [log] [blame]
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001/* (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>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040011#include <linux/export.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080012#include <linux/ip.h>
13#include <linux/tcp.h>
14
15#include <linux/netfilter.h>
16#include <linux/netfilter/nfnetlink_conntrack.h>
17#include <net/netfilter/nf_nat.h>
18#include <net/netfilter/nf_nat_rule.h>
19#include <net/netfilter/nf_nat_protocol.h>
20#include <net/netfilter/nf_nat_core.h>
21
Patrick McHardy937e0df2008-03-20 15:15:47 +010022static u_int16_t tcp_port_rover;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080023
Changli Gaof43dc982010-08-02 17:20:54 +020024static void
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080025tcp_unique_tuple(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{
Changli Gaof43dc982010-08-02 17:20:54 +020030 nf_nat_proto_unique_tuple(tuple, range, maniptype, ct, &tcp_port_rover);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080031}
32
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020033static bool
Herbert Xu3db05fe2007-10-15 00:53:15 -070034tcp_manip_pkt(struct sk_buff *skb,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080035 unsigned int iphdroff,
36 const struct nf_conntrack_tuple *tuple,
37 enum nf_nat_manip_type maniptype)
38{
Jan Engelhardt82f568f2008-01-31 04:52:07 -080039 const struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080040 struct tcphdr *hdr;
41 unsigned int hdroff = iphdroff + iph->ihl*4;
42 __be32 oldip, newip;
43 __be16 *portptr, newport, oldport;
44 int hdrsize = 8; /* TCP connection tracking guarantees this much */
45
46 /* this could be a inner header returned in icmp packet; in such
47 cases we cannot update the checksum field since it is outside of
48 the 8 bytes of transport layer headers we are guaranteed */
Herbert Xu3db05fe2007-10-15 00:53:15 -070049 if (skb->len >= hdroff + sizeof(struct tcphdr))
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080050 hdrsize = sizeof(struct tcphdr);
51
Herbert Xu3db05fe2007-10-15 00:53:15 -070052 if (!skb_make_writable(skb, hdroff + hdrsize))
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020053 return false;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080054
Herbert Xu3db05fe2007-10-15 00:53:15 -070055 iph = (struct iphdr *)(skb->data + iphdroff);
56 hdr = (struct tcphdr *)(skb->data + hdroff);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080057
58 if (maniptype == IP_NAT_MANIP_SRC) {
59 /* Get rid of src ip and src pt */
60 oldip = iph->saddr;
61 newip = tuple->src.u3.ip;
62 newport = tuple->src.u.tcp.port;
63 portptr = &hdr->source;
64 } else {
65 /* Get rid of dst ip and dst pt */
66 oldip = iph->daddr;
67 newip = tuple->dst.u3.ip;
68 newport = tuple->dst.u.tcp.port;
69 portptr = &hdr->dest;
70 }
71
72 oldport = *portptr;
73 *portptr = newport;
74
75 if (hdrsize < sizeof(*hdr))
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020076 return true;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080077
Patrick McHardybe0ea7d2007-11-30 01:17:11 +110078 inet_proto_csum_replace4(&hdr->check, skb, oldip, newip, 1);
79 inet_proto_csum_replace2(&hdr->check, skb, oldport, newport, 0);
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020080 return true;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080081}
82
Patrick McHardy2b628a02007-12-17 22:37:36 -080083const struct nf_nat_protocol nf_nat_protocol_tcp = {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080084 .protonum = IPPROTO_TCP,
85 .me = THIS_MODULE,
86 .manip_pkt = tcp_manip_pkt,
Patrick McHardy937e0df2008-03-20 15:15:47 +010087 .in_range = nf_nat_proto_in_range,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080088 .unique_tuple = tcp_unique_tuple,
Patrick McHardye281db5c2007-03-04 15:57:25 -080089#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardy535b57c2008-04-14 11:15:47 +020090 .range_to_nlattr = nf_nat_proto_range_to_nlattr,
91 .nlattr_to_range = nf_nat_proto_nlattr_to_range,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080092#endif
93};