Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 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/ip.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_rule.h> |
| 18 | #include <net/netfilter/nf_nat_protocol.h> |
| 19 | #include <net/netfilter/nf_nat_core.h> |
| 20 | |
Patrick McHardy | 937e0df | 2008-03-20 15:15:47 +0100 | [diff] [blame] | 21 | static u_int16_t tcp_port_rover; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 22 | |
Changli Gao | f43dc98 | 2010-08-02 17:20:54 +0200 | [diff] [blame^] | 23 | static void |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 24 | tcp_unique_tuple(struct nf_conntrack_tuple *tuple, |
| 25 | const struct nf_nat_range *range, |
| 26 | enum nf_nat_manip_type maniptype, |
| 27 | const struct nf_conn *ct) |
| 28 | { |
Changli Gao | f43dc98 | 2010-08-02 17:20:54 +0200 | [diff] [blame^] | 29 | nf_nat_proto_unique_tuple(tuple, range, maniptype, ct, &tcp_port_rover); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 32 | static bool |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 33 | tcp_manip_pkt(struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 34 | unsigned int iphdroff, |
| 35 | const struct nf_conntrack_tuple *tuple, |
| 36 | enum nf_nat_manip_type maniptype) |
| 37 | { |
Jan Engelhardt | 82f568f | 2008-01-31 04:52:07 -0800 | [diff] [blame] | 38 | const struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 39 | struct tcphdr *hdr; |
| 40 | unsigned int hdroff = iphdroff + iph->ihl*4; |
| 41 | __be32 oldip, newip; |
| 42 | __be16 *portptr, newport, oldport; |
| 43 | int hdrsize = 8; /* TCP connection tracking guarantees this much */ |
| 44 | |
| 45 | /* this could be a inner header returned in icmp packet; in such |
| 46 | cases we cannot update the checksum field since it is outside of |
| 47 | the 8 bytes of transport layer headers we are guaranteed */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 48 | if (skb->len >= hdroff + sizeof(struct tcphdr)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 49 | hdrsize = sizeof(struct tcphdr); |
| 50 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 51 | if (!skb_make_writable(skb, hdroff + hdrsize)) |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 52 | return false; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 53 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 54 | iph = (struct iphdr *)(skb->data + iphdroff); |
| 55 | hdr = (struct tcphdr *)(skb->data + hdroff); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 56 | |
| 57 | if (maniptype == IP_NAT_MANIP_SRC) { |
| 58 | /* Get rid of src ip and src pt */ |
| 59 | oldip = iph->saddr; |
| 60 | newip = tuple->src.u3.ip; |
| 61 | newport = tuple->src.u.tcp.port; |
| 62 | portptr = &hdr->source; |
| 63 | } else { |
| 64 | /* Get rid of dst ip and dst pt */ |
| 65 | oldip = iph->daddr; |
| 66 | newip = tuple->dst.u3.ip; |
| 67 | newport = tuple->dst.u.tcp.port; |
| 68 | portptr = &hdr->dest; |
| 69 | } |
| 70 | |
| 71 | oldport = *portptr; |
| 72 | *portptr = newport; |
| 73 | |
| 74 | if (hdrsize < sizeof(*hdr)) |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 75 | return true; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 76 | |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 77 | inet_proto_csum_replace4(&hdr->check, skb, oldip, newip, 1); |
| 78 | inet_proto_csum_replace2(&hdr->check, skb, oldport, newport, 0); |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 79 | return true; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Patrick McHardy | 2b628a0 | 2007-12-17 22:37:36 -0800 | [diff] [blame] | 82 | const struct nf_nat_protocol nf_nat_protocol_tcp = { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 83 | .protonum = IPPROTO_TCP, |
| 84 | .me = THIS_MODULE, |
| 85 | .manip_pkt = tcp_manip_pkt, |
Patrick McHardy | 937e0df | 2008-03-20 15:15:47 +0100 | [diff] [blame] | 86 | .in_range = nf_nat_proto_in_range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 87 | .unique_tuple = tcp_unique_tuple, |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 88 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Patrick McHardy | 535b57c | 2008-04-14 11:15:47 +0200 | [diff] [blame] | 89 | .range_to_nlattr = nf_nat_proto_range_to_nlattr, |
| 90 | .nlattr_to_range = nf_nat_proto_nlattr_to_range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 91 | #endif |
| 92 | }; |