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> |
Eric Leblond | 41f4689 | 2007-02-07 15:10:09 -0800 | [diff] [blame] | 11 | #include <linux/random.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 12 | #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 | |
| 22 | static int |
| 23 | tcp_in_range(const struct nf_conntrack_tuple *tuple, |
| 24 | enum nf_nat_manip_type maniptype, |
| 25 | const union nf_conntrack_man_proto *min, |
| 26 | const union nf_conntrack_man_proto *max) |
| 27 | { |
| 28 | __be16 port; |
| 29 | |
| 30 | if (maniptype == IP_NAT_MANIP_SRC) |
| 31 | port = tuple->src.u.tcp.port; |
| 32 | else |
| 33 | port = tuple->dst.u.tcp.port; |
| 34 | |
| 35 | return ntohs(port) >= ntohs(min->tcp.port) && |
| 36 | ntohs(port) <= ntohs(max->tcp.port); |
| 37 | } |
| 38 | |
| 39 | static int |
| 40 | tcp_unique_tuple(struct nf_conntrack_tuple *tuple, |
| 41 | const struct nf_nat_range *range, |
| 42 | enum nf_nat_manip_type maniptype, |
| 43 | const struct nf_conn *ct) |
| 44 | { |
| 45 | static u_int16_t port; |
| 46 | __be16 *portptr; |
| 47 | unsigned int range_size, min, i; |
| 48 | |
| 49 | if (maniptype == IP_NAT_MANIP_SRC) |
| 50 | portptr = &tuple->src.u.tcp.port; |
| 51 | else |
| 52 | portptr = &tuple->dst.u.tcp.port; |
| 53 | |
| 54 | /* If no range specified... */ |
| 55 | if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) { |
| 56 | /* If it's dst rewrite, can't change port */ |
| 57 | if (maniptype == IP_NAT_MANIP_DST) |
| 58 | return 0; |
| 59 | |
| 60 | /* Map privileged onto privileged. */ |
| 61 | if (ntohs(*portptr) < 1024) { |
| 62 | /* Loose convention: >> 512 is credential passing */ |
| 63 | if (ntohs(*portptr)<512) { |
| 64 | min = 1; |
| 65 | range_size = 511 - min + 1; |
| 66 | } else { |
| 67 | min = 600; |
| 68 | range_size = 1023 - min + 1; |
| 69 | } |
| 70 | } else { |
| 71 | min = 1024; |
| 72 | range_size = 65535 - 1024 + 1; |
| 73 | } |
| 74 | } else { |
| 75 | min = ntohs(range->min.tcp.port); |
| 76 | range_size = ntohs(range->max.tcp.port) - min + 1; |
| 77 | } |
| 78 | |
Eric Leblond | 41f4689 | 2007-02-07 15:10:09 -0800 | [diff] [blame] | 79 | if (range->flags & IP_NAT_RANGE_PROTO_RANDOM) |
| 80 | port = net_random(); |
| 81 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 82 | for (i = 0; i < range_size; i++, port++) { |
| 83 | *portptr = htons(min + port % range_size); |
| 84 | if (!nf_nat_used_tuple(tuple, ct)) |
| 85 | return 1; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int |
| 91 | tcp_manip_pkt(struct sk_buff **pskb, |
| 92 | unsigned int iphdroff, |
| 93 | const struct nf_conntrack_tuple *tuple, |
| 94 | enum nf_nat_manip_type maniptype) |
| 95 | { |
| 96 | struct iphdr *iph = (struct iphdr *)((*pskb)->data + iphdroff); |
| 97 | struct tcphdr *hdr; |
| 98 | unsigned int hdroff = iphdroff + iph->ihl*4; |
| 99 | __be32 oldip, newip; |
| 100 | __be16 *portptr, newport, oldport; |
| 101 | int hdrsize = 8; /* TCP connection tracking guarantees this much */ |
| 102 | |
| 103 | /* this could be a inner header returned in icmp packet; in such |
| 104 | cases we cannot update the checksum field since it is outside of |
| 105 | the 8 bytes of transport layer headers we are guaranteed */ |
| 106 | if ((*pskb)->len >= hdroff + sizeof(struct tcphdr)) |
| 107 | hdrsize = sizeof(struct tcphdr); |
| 108 | |
| 109 | if (!skb_make_writable(pskb, hdroff + hdrsize)) |
| 110 | return 0; |
| 111 | |
| 112 | iph = (struct iphdr *)((*pskb)->data + iphdroff); |
| 113 | hdr = (struct tcphdr *)((*pskb)->data + hdroff); |
| 114 | |
| 115 | if (maniptype == IP_NAT_MANIP_SRC) { |
| 116 | /* Get rid of src ip and src pt */ |
| 117 | oldip = iph->saddr; |
| 118 | newip = tuple->src.u3.ip; |
| 119 | newport = tuple->src.u.tcp.port; |
| 120 | portptr = &hdr->source; |
| 121 | } else { |
| 122 | /* Get rid of dst ip and dst pt */ |
| 123 | oldip = iph->daddr; |
| 124 | newip = tuple->dst.u3.ip; |
| 125 | newport = tuple->dst.u.tcp.port; |
| 126 | portptr = &hdr->dest; |
| 127 | } |
| 128 | |
| 129 | oldport = *portptr; |
| 130 | *portptr = newport; |
| 131 | |
| 132 | if (hdrsize < sizeof(*hdr)) |
| 133 | return 1; |
| 134 | |
| 135 | nf_proto_csum_replace4(&hdr->check, *pskb, oldip, newip, 1); |
| 136 | nf_proto_csum_replace2(&hdr->check, *pskb, oldport, newport, 0); |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | struct nf_nat_protocol nf_nat_protocol_tcp = { |
| 141 | .name = "TCP", |
| 142 | .protonum = IPPROTO_TCP, |
| 143 | .me = THIS_MODULE, |
| 144 | .manip_pkt = tcp_manip_pkt, |
| 145 | .in_range = tcp_in_range, |
| 146 | .unique_tuple = tcp_unique_tuple, |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 147 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame^] | 148 | .range_to_nlattr = nf_nat_port_range_to_nlattr, |
| 149 | .nlattr_to_range = nf_nat_port_nlattr_to_range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 150 | #endif |
| 151 | }; |