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> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 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> |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 17 | #include <net/netfilter/nf_nat_l3proto.h> |
| 18 | #include <net/netfilter/nf_nat_l4proto.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 19 | #include <net/netfilter/nf_nat_core.h> |
| 20 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 21 | static u16 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 |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 24 | tcp_unique_tuple(const struct nf_nat_l3proto *l3proto, |
| 25 | struct nf_conntrack_tuple *tuple, |
| 26 | const struct nf_nat_range *range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 27 | enum nf_nat_manip_type maniptype, |
| 28 | const struct nf_conn *ct) |
| 29 | { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 30 | nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct, |
| 31 | &tcp_port_rover); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 34 | static bool |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 35 | tcp_manip_pkt(struct sk_buff *skb, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 36 | const struct nf_nat_l3proto *l3proto, |
| 37 | unsigned int iphdroff, unsigned int hdroff, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 38 | const struct nf_conntrack_tuple *tuple, |
| 39 | enum nf_nat_manip_type maniptype) |
| 40 | { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 41 | struct tcphdr *hdr; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 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 | hdr = (struct tcphdr *)(skb->data + hdroff); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 55 | |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 56 | if (maniptype == NF_NAT_MANIP_SRC) { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 57 | /* Get rid of src port */ |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 58 | newport = tuple->src.u.tcp.port; |
| 59 | portptr = &hdr->source; |
| 60 | } else { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 61 | /* Get rid of dst port */ |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 62 | newport = tuple->dst.u.tcp.port; |
| 63 | portptr = &hdr->dest; |
| 64 | } |
| 65 | |
| 66 | oldport = *portptr; |
| 67 | *portptr = newport; |
| 68 | |
| 69 | if (hdrsize < sizeof(*hdr)) |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 70 | return true; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 71 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 72 | l3proto->csum_update(skb, iphdroff, &hdr->check, tuple, maniptype); |
Tom Herbert | 4b048d6 | 2015-08-17 13:42:25 -0700 | [diff] [blame] | 73 | inet_proto_csum_replace2(&hdr->check, skb, oldport, newport, false); |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 74 | return true; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 77 | const struct nf_nat_l4proto nf_nat_l4proto_tcp = { |
| 78 | .l4proto = IPPROTO_TCP, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 79 | .manip_pkt = tcp_manip_pkt, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 80 | .in_range = nf_nat_l4proto_in_range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 81 | .unique_tuple = tcp_unique_tuple, |
Duan Jiong | 24de3d3 | 2014-06-30 09:19:32 +0800 | [diff] [blame] | 82 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 83 | .nlattr_to_range = nf_nat_l4proto_nlattr_to_range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 84 | #endif |
| 85 | }; |