blob: d2a887fc8d9b1b53f1539bcfc8c446d248466522 [file] [log] [blame]
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001/* NAT for netfilter; shared with compatibility layer. */
2
3/* (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/types.h>
13#include <linux/timer.h>
14#include <linux/skbuff.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080015#include <net/checksum.h>
16#include <net/icmp.h>
17#include <net/ip.h>
18#include <net/tcp.h> /* For tcp_prot in getorigdst */
19#include <linux/icmp.h>
20#include <linux/udp.h>
21#include <linux/jhash.h>
22
23#include <linux/netfilter_ipv4.h>
24#include <net/netfilter/nf_conntrack.h>
25#include <net/netfilter/nf_conntrack_core.h>
26#include <net/netfilter/nf_nat.h>
27#include <net/netfilter/nf_nat_protocol.h>
28#include <net/netfilter/nf_nat_core.h>
29#include <net/netfilter/nf_nat_helper.h>
30#include <net/netfilter/nf_conntrack_helper.h>
31#include <net/netfilter/nf_conntrack_l3proto.h>
32#include <net/netfilter/nf_conntrack_l4proto.h>
33
Patrick McHardy02502f62008-01-31 04:43:06 -080034static DEFINE_SPINLOCK(nf_nat_lock);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080035
Patrick McHardyce4b1ce2007-12-17 22:37:52 -080036static struct nf_conntrack_l3proto *l3proto __read_mostly;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080037
38/* Calculated at init based on memory size */
Patrick McHardyce4b1ce2007-12-17 22:37:52 -080039static unsigned int nf_nat_htable_size __read_mostly;
Patrick McHardy53aba592007-07-07 22:30:27 -070040static int nf_nat_vmalloced;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080041
Patrick McHardyce4b1ce2007-12-17 22:37:52 -080042static struct hlist_head *bysource __read_mostly;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080043
44#define MAX_IP_NAT_PROTO 256
Patrick McHardyce4b1ce2007-12-17 22:37:52 -080045static const struct nf_nat_protocol *nf_nat_protos[MAX_IP_NAT_PROTO]
46 __read_mostly;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080047
Patrick McHardy2b628a02007-12-17 22:37:36 -080048static inline const struct nf_nat_protocol *
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080049__nf_nat_proto_find(u_int8_t protonum)
50{
Patrick McHardye22a0542007-02-12 11:12:26 -080051 return rcu_dereference(nf_nat_protos[protonum]);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080052}
53
Patrick McHardy2b628a02007-12-17 22:37:36 -080054const struct nf_nat_protocol *
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080055nf_nat_proto_find_get(u_int8_t protonum)
56{
Patrick McHardy2b628a02007-12-17 22:37:36 -080057 const struct nf_nat_protocol *p;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080058
Patrick McHardye22a0542007-02-12 11:12:26 -080059 rcu_read_lock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080060 p = __nf_nat_proto_find(protonum);
61 if (!try_module_get(p->me))
62 p = &nf_nat_unknown_protocol;
Patrick McHardye22a0542007-02-12 11:12:26 -080063 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080064
65 return p;
66}
67EXPORT_SYMBOL_GPL(nf_nat_proto_find_get);
68
69void
Patrick McHardy2b628a02007-12-17 22:37:36 -080070nf_nat_proto_put(const struct nf_nat_protocol *p)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080071{
72 module_put(p->me);
73}
74EXPORT_SYMBOL_GPL(nf_nat_proto_put);
75
76/* We keep an extra hash for each conntrack, for fast searching. */
77static inline unsigned int
78hash_by_src(const struct nf_conntrack_tuple *tuple)
79{
Patrick McHardy34498822007-12-17 22:45:52 -080080 unsigned int hash;
81
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080082 /* Original src, to ensure we map it consistently if poss. */
Patrick McHardy34498822007-12-17 22:45:52 -080083 hash = jhash_3words((__force u32)tuple->src.u3.ip,
Al Viroa34c4582007-07-26 17:33:19 +010084 (__force u32)tuple->src.u.all,
Patrick McHardy34498822007-12-17 22:45:52 -080085 tuple->dst.protonum, 0);
86 return ((u64)hash * nf_nat_htable_size) >> 32;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080087}
88
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080089/* Is this tuple already taken? (not by us) */
90int
91nf_nat_used_tuple(const struct nf_conntrack_tuple *tuple,
92 const struct nf_conn *ignored_conntrack)
93{
94 /* Conntrack tracking doesn't keep track of outgoing tuples; only
95 incoming ones. NAT means they don't have a fixed mapping,
96 so we invert the tuple and look for the incoming reply.
97
98 We could keep a separate hash if this proves too slow. */
99 struct nf_conntrack_tuple reply;
100
101 nf_ct_invert_tuplepr(&reply, tuple);
102 return nf_conntrack_tuple_taken(&reply, ignored_conntrack);
103}
104EXPORT_SYMBOL(nf_nat_used_tuple);
105
106/* If we source map this tuple so reply looks like reply_tuple, will
107 * that meet the constraints of range. */
108static int
109in_range(const struct nf_conntrack_tuple *tuple,
110 const struct nf_nat_range *range)
111{
Patrick McHardy2b628a02007-12-17 22:37:36 -0800112 const struct nf_nat_protocol *proto;
Patrick McHardye22a0542007-02-12 11:12:26 -0800113 int ret = 0;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800114
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800115 /* If we are supposed to map IPs, then we must be in the
116 range specified, otherwise let this drag us onto a new src IP. */
117 if (range->flags & IP_NAT_RANGE_MAP_IPS) {
118 if (ntohl(tuple->src.u3.ip) < ntohl(range->min_ip) ||
119 ntohl(tuple->src.u3.ip) > ntohl(range->max_ip))
120 return 0;
121 }
122
Patrick McHardye22a0542007-02-12 11:12:26 -0800123 rcu_read_lock();
124 proto = __nf_nat_proto_find(tuple->dst.protonum);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800125 if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED) ||
126 proto->in_range(tuple, IP_NAT_MANIP_SRC,
127 &range->min, &range->max))
Patrick McHardye22a0542007-02-12 11:12:26 -0800128 ret = 1;
129 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800130
Patrick McHardye22a0542007-02-12 11:12:26 -0800131 return ret;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800132}
133
134static inline int
135same_src(const struct nf_conn *ct,
136 const struct nf_conntrack_tuple *tuple)
137{
138 const struct nf_conntrack_tuple *t;
139
140 t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
141 return (t->dst.protonum == tuple->dst.protonum &&
142 t->src.u3.ip == tuple->src.u3.ip &&
143 t->src.u.all == tuple->src.u.all);
144}
145
146/* Only called for SRC manip */
147static int
148find_appropriate_src(const struct nf_conntrack_tuple *tuple,
149 struct nf_conntrack_tuple *result,
150 const struct nf_nat_range *range)
151{
152 unsigned int h = hash_by_src(tuple);
Jan Engelhardt72b72942008-04-14 11:15:42 +0200153 const struct nf_conn_nat *nat;
154 const struct nf_conn *ct;
155 const struct hlist_node *n;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800156
Patrick McHardy4d354c52008-01-31 04:42:37 -0800157 rcu_read_lock();
158 hlist_for_each_entry_rcu(nat, n, &bysource[h], bysource) {
Yasuyuki Kozakaib6b84d42007-07-07 22:26:35 -0700159 ct = nat->ct;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800160 if (same_src(ct, tuple)) {
161 /* Copy source part from reply tuple. */
162 nf_ct_invert_tuplepr(result,
163 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
164 result->dst = tuple->dst;
165
166 if (in_range(result, range)) {
Patrick McHardy4d354c52008-01-31 04:42:37 -0800167 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800168 return 1;
169 }
170 }
171 }
Patrick McHardy4d354c52008-01-31 04:42:37 -0800172 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800173 return 0;
174}
175
176/* For [FUTURE] fragmentation handling, we want the least-used
177 src-ip/dst-ip/proto triple. Fairness doesn't come into it. Thus
178 if the range specifies 1.2.3.4 ports 10000-10005 and 1.2.3.5 ports
179 1-65535, we don't do pro-rata allocation based on ports; we choose
180 the ip with the lowest src-ip/dst-ip/proto usage.
181*/
182static void
183find_best_ips_proto(struct nf_conntrack_tuple *tuple,
184 const struct nf_nat_range *range,
185 const struct nf_conn *ct,
186 enum nf_nat_manip_type maniptype)
187{
188 __be32 *var_ipp;
189 /* Host order */
190 u_int32_t minip, maxip, j;
191
192 /* No IP mapping? Do nothing. */
193 if (!(range->flags & IP_NAT_RANGE_MAP_IPS))
194 return;
195
196 if (maniptype == IP_NAT_MANIP_SRC)
197 var_ipp = &tuple->src.u3.ip;
198 else
199 var_ipp = &tuple->dst.u3.ip;
200
201 /* Fast path: only one choice. */
202 if (range->min_ip == range->max_ip) {
203 *var_ipp = range->min_ip;
204 return;
205 }
206
207 /* Hashing source and destination IPs gives a fairly even
208 * spread in practice (if there are a small number of IPs
209 * involved, there usually aren't that many connections
210 * anyway). The consistency means that servers see the same
211 * client coming from the same IP (some Internet Banking sites
212 * like this), even across reboots. */
213 minip = ntohl(range->min_ip);
214 maxip = ntohl(range->max_ip);
215 j = jhash_2words((__force u32)tuple->src.u3.ip,
216 (__force u32)tuple->dst.u3.ip, 0);
Patrick McHardy34498822007-12-17 22:45:52 -0800217 j = ((u64)j * (maxip - minip + 1)) >> 32;
218 *var_ipp = htonl(minip + j);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800219}
220
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800221/* Manipulate the tuple into the range given. For NF_INET_POST_ROUTING,
222 * we change the source to map into the range. For NF_INET_PRE_ROUTING
223 * and NF_INET_LOCAL_OUT, we change the destination to map into the
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800224 * range. It might not be possible to get a unique tuple, but we try.
225 * At worst (or if we race), we will end up with a final duplicate in
226 * __ip_conntrack_confirm and drop the packet. */
227static void
228get_unique_tuple(struct nf_conntrack_tuple *tuple,
229 const struct nf_conntrack_tuple *orig_tuple,
230 const struct nf_nat_range *range,
231 struct nf_conn *ct,
232 enum nf_nat_manip_type maniptype)
233{
Patrick McHardy2b628a02007-12-17 22:37:36 -0800234 const struct nf_nat_protocol *proto;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800235
236 /* 1) If this srcip/proto/src-proto-part is currently mapped,
237 and that same mapping gives a unique tuple within the given
238 range, use that.
239
240 This is only required for source (ie. NAT/masq) mappings.
241 So far, we don't do local source mappings, so multiple
242 manips not an issue. */
243 if (maniptype == IP_NAT_MANIP_SRC) {
244 if (find_appropriate_src(orig_tuple, tuple, range)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700245 pr_debug("get_unique_tuple: Found current src map\n");
Eric Leblond41f46892007-02-07 15:10:09 -0800246 if (!(range->flags & IP_NAT_RANGE_PROTO_RANDOM))
247 if (!nf_nat_used_tuple(tuple, ct))
248 return;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800249 }
250 }
251
252 /* 2) Select the least-used IP/proto combination in the given
253 range. */
254 *tuple = *orig_tuple;
255 find_best_ips_proto(tuple, range, ct, maniptype);
256
257 /* 3) The per-protocol part of the manip is made to map into
258 the range to make a unique tuple. */
259
Patrick McHardye22a0542007-02-12 11:12:26 -0800260 rcu_read_lock();
261 proto = __nf_nat_proto_find(orig_tuple->dst.protonum);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800262
Eric Leblond41f46892007-02-07 15:10:09 -0800263 /* Change protocol info to have some randomization */
264 if (range->flags & IP_NAT_RANGE_PROTO_RANDOM) {
265 proto->unique_tuple(tuple, range, maniptype, ct);
Patrick McHardye22a0542007-02-12 11:12:26 -0800266 goto out;
Eric Leblond41f46892007-02-07 15:10:09 -0800267 }
268
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800269 /* Only bother mapping if it's not already in range and unique */
270 if ((!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED) ||
271 proto->in_range(tuple, maniptype, &range->min, &range->max)) &&
Patrick McHardye22a0542007-02-12 11:12:26 -0800272 !nf_nat_used_tuple(tuple, ct))
273 goto out;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800274
275 /* Last change: get protocol to try to obtain unique tuple. */
276 proto->unique_tuple(tuple, range, maniptype, ct);
Patrick McHardye22a0542007-02-12 11:12:26 -0800277out:
278 rcu_read_unlock();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800279}
280
281unsigned int
282nf_nat_setup_info(struct nf_conn *ct,
283 const struct nf_nat_range *range,
Patrick McHardycc01dcb2007-12-17 22:38:20 -0800284 enum nf_nat_manip_type maniptype)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800285{
286 struct nf_conntrack_tuple curr_tuple, new_tuple;
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700287 struct nf_conn_nat *nat;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800288 int have_to_hash = !(ct->status & IPS_NAT_DONE_MASK);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800289
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700290 /* nat helper or nfctnetlink also setup binding */
291 nat = nfct_nat(ct);
292 if (!nat) {
293 nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
294 if (nat == NULL) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700295 pr_debug("failed to add NAT extension\n");
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700296 return NF_ACCEPT;
297 }
298 }
299
Patrick McHardycc01dcb2007-12-17 22:38:20 -0800300 NF_CT_ASSERT(maniptype == IP_NAT_MANIP_SRC ||
301 maniptype == IP_NAT_MANIP_DST);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800302 BUG_ON(nf_nat_initialized(ct, maniptype));
303
304 /* What we've got will look like inverse of reply. Normally
305 this is what is in the conntrack, except for prior
306 manipulations (future optimization: if num_manips == 0,
307 orig_tp =
308 conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple) */
309 nf_ct_invert_tuplepr(&curr_tuple,
310 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
311
312 get_unique_tuple(&new_tuple, &curr_tuple, range, ct, maniptype);
313
314 if (!nf_ct_tuple_equal(&new_tuple, &curr_tuple)) {
315 struct nf_conntrack_tuple reply;
316
317 /* Alter conntrack table so will recognize replies. */
318 nf_ct_invert_tuplepr(&reply, &new_tuple);
319 nf_conntrack_alter_reply(ct, &reply);
320
321 /* Non-atomic: we own this at the moment. */
322 if (maniptype == IP_NAT_MANIP_SRC)
323 ct->status |= IPS_SRC_NAT;
324 else
325 ct->status |= IPS_DST_NAT;
326 }
327
328 /* Place in source hash if this is the first time. */
329 if (have_to_hash) {
330 unsigned int srchash;
331
332 srchash = hash_by_src(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Patrick McHardy02502f62008-01-31 04:43:06 -0800333 spin_lock_bh(&nf_nat_lock);
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700334 /* nf_conntrack_alter_reply might re-allocate exntension aera */
Yasuyuki Kozakaib6b84d42007-07-07 22:26:35 -0700335 nat = nfct_nat(ct);
336 nat->ct = ct;
Patrick McHardy4d354c52008-01-31 04:42:37 -0800337 hlist_add_head_rcu(&nat->bysource, &bysource[srchash]);
Patrick McHardy02502f62008-01-31 04:43:06 -0800338 spin_unlock_bh(&nf_nat_lock);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800339 }
340
341 /* It's done. */
342 if (maniptype == IP_NAT_MANIP_DST)
343 set_bit(IPS_DST_NAT_DONE_BIT, &ct->status);
344 else
345 set_bit(IPS_SRC_NAT_DONE_BIT, &ct->status);
346
347 return NF_ACCEPT;
348}
349EXPORT_SYMBOL(nf_nat_setup_info);
350
351/* Returns true if succeeded. */
Jan Engelhardtf2ea8252008-04-14 11:15:53 +0200352static bool
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800353manip_pkt(u_int16_t proto,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700354 struct sk_buff *skb,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800355 unsigned int iphdroff,
356 const struct nf_conntrack_tuple *target,
357 enum nf_nat_manip_type maniptype)
358{
359 struct iphdr *iph;
Patrick McHardy2b628a02007-12-17 22:37:36 -0800360 const struct nf_nat_protocol *p;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800361
Herbert Xu3db05fe2007-10-15 00:53:15 -0700362 if (!skb_make_writable(skb, iphdroff + sizeof(*iph)))
Jan Engelhardtf2ea8252008-04-14 11:15:53 +0200363 return false;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800364
Herbert Xu3db05fe2007-10-15 00:53:15 -0700365 iph = (void *)skb->data + iphdroff;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800366
367 /* Manipulate protcol part. */
Patrick McHardye22a0542007-02-12 11:12:26 -0800368
369 /* rcu_read_lock()ed by nf_hook_slow */
370 p = __nf_nat_proto_find(proto);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700371 if (!p->manip_pkt(skb, iphdroff, target, maniptype))
Jan Engelhardtf2ea8252008-04-14 11:15:53 +0200372 return false;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800373
Herbert Xu3db05fe2007-10-15 00:53:15 -0700374 iph = (void *)skb->data + iphdroff;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800375
376 if (maniptype == IP_NAT_MANIP_SRC) {
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100377 csum_replace4(&iph->check, iph->saddr, target->src.u3.ip);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800378 iph->saddr = target->src.u3.ip;
379 } else {
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100380 csum_replace4(&iph->check, iph->daddr, target->dst.u3.ip);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800381 iph->daddr = target->dst.u3.ip;
382 }
Jan Engelhardtf2ea8252008-04-14 11:15:53 +0200383 return true;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800384}
385
386/* Do packet manipulations according to nf_nat_setup_info. */
387unsigned int nf_nat_packet(struct nf_conn *ct,
388 enum ip_conntrack_info ctinfo,
389 unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700390 struct sk_buff *skb)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800391{
392 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
393 unsigned long statusbit;
394 enum nf_nat_manip_type mtype = HOOK2MANIP(hooknum);
395
396 if (mtype == IP_NAT_MANIP_SRC)
397 statusbit = IPS_SRC_NAT;
398 else
399 statusbit = IPS_DST_NAT;
400
401 /* Invert if this is reply dir. */
402 if (dir == IP_CT_DIR_REPLY)
403 statusbit ^= IPS_NAT_MASK;
404
405 /* Non-atomic: these bits don't change. */
406 if (ct->status & statusbit) {
407 struct nf_conntrack_tuple target;
408
409 /* We are aiming to look like inverse of other direction. */
410 nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
411
Herbert Xu3db05fe2007-10-15 00:53:15 -0700412 if (!manip_pkt(target.dst.protonum, skb, 0, &target, mtype))
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800413 return NF_DROP;
414 }
415 return NF_ACCEPT;
416}
417EXPORT_SYMBOL_GPL(nf_nat_packet);
418
419/* Dir is direction ICMP is coming from (opposite to packet it contains) */
420int nf_nat_icmp_reply_translation(struct nf_conn *ct,
421 enum ip_conntrack_info ctinfo,
422 unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700423 struct sk_buff *skb)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800424{
425 struct {
426 struct icmphdr icmp;
427 struct iphdr ip;
428 } *inside;
Jan Engelhardt72b72942008-04-14 11:15:42 +0200429 const struct nf_conntrack_l4proto *l4proto;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800430 struct nf_conntrack_tuple inner, target;
Herbert Xu3db05fe2007-10-15 00:53:15 -0700431 int hdrlen = ip_hdrlen(skb);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800432 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
433 unsigned long statusbit;
434 enum nf_nat_manip_type manip = HOOK2MANIP(hooknum);
435
Herbert Xu3db05fe2007-10-15 00:53:15 -0700436 if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800437 return 0;
438
Herbert Xu3db05fe2007-10-15 00:53:15 -0700439 inside = (void *)skb->data + ip_hdrlen(skb);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800440
441 /* We're actually going to mangle it beyond trivial checksum
442 adjustment, so make sure the current checksum is correct. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700443 if (nf_ip_checksum(skb, hooknum, hdrlen, 0))
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800444 return 0;
445
446 /* Must be RELATED */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700447 NF_CT_ASSERT(skb->nfctinfo == IP_CT_RELATED ||
448 skb->nfctinfo == IP_CT_RELATED+IP_CT_IS_REPLY);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800449
450 /* Redirects on non-null nats must be dropped, else they'll
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900451 start talking to each other without our translation, and be
452 confused... --RR */
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800453 if (inside->icmp.type == ICMP_REDIRECT) {
454 /* If NAT isn't finished, assume it and drop. */
455 if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK)
456 return 0;
457
458 if (ct->status & IPS_NAT_MASK)
459 return 0;
460 }
461
Patrick McHardy0d537782007-07-07 22:39:38 -0700462 pr_debug("icmp_reply_translation: translating error %p manip %u "
Herbert Xu3db05fe2007-10-15 00:53:15 -0700463 "dir %s\n", skb, manip,
Patrick McHardy0d537782007-07-07 22:39:38 -0700464 dir == IP_CT_DIR_ORIGINAL ? "ORIG" : "REPLY");
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800465
Patrick McHardy923f4902007-02-12 11:12:57 -0800466 /* rcu_read_lock()ed by nf_hook_slow */
467 l4proto = __nf_ct_l4proto_find(PF_INET, inside->ip.protocol);
468
Herbert Xu3db05fe2007-10-15 00:53:15 -0700469 if (!nf_ct_get_tuple(skb,
470 ip_hdrlen(skb) + sizeof(struct icmphdr),
471 (ip_hdrlen(skb) +
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300472 sizeof(struct icmphdr) + inside->ip.ihl * 4),
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900473 (u_int16_t)AF_INET,
474 inside->ip.protocol,
Patrick McHardy923f4902007-02-12 11:12:57 -0800475 &inner, l3proto, l4proto))
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800476 return 0;
477
478 /* Change inner back to look like incoming packet. We do the
479 opposite manip on this hook to normal, because it might not
480 pass all hooks (locally-generated ICMP). Consider incoming
481 packet: PREROUTING (DST manip), routing produces ICMP, goes
482 through POSTROUTING (which must correct the DST manip). */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700483 if (!manip_pkt(inside->ip.protocol, skb,
484 ip_hdrlen(skb) + sizeof(inside->icmp),
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800485 &ct->tuplehash[!dir].tuple,
486 !manip))
487 return 0;
488
Herbert Xu3db05fe2007-10-15 00:53:15 -0700489 if (skb->ip_summed != CHECKSUM_PARTIAL) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800490 /* Reloading "inside" here since manip_pkt inner. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700491 inside = (void *)skb->data + ip_hdrlen(skb);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800492 inside->icmp.checksum = 0;
493 inside->icmp.checksum =
Herbert Xu3db05fe2007-10-15 00:53:15 -0700494 csum_fold(skb_checksum(skb, hdrlen,
495 skb->len - hdrlen, 0));
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800496 }
497
498 /* Change outer to look the reply to an incoming packet
499 * (proto 0 means don't invert per-proto part). */
500 if (manip == IP_NAT_MANIP_SRC)
501 statusbit = IPS_SRC_NAT;
502 else
503 statusbit = IPS_DST_NAT;
504
505 /* Invert if this is reply dir. */
506 if (dir == IP_CT_DIR_REPLY)
507 statusbit ^= IPS_NAT_MASK;
508
509 if (ct->status & statusbit) {
510 nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700511 if (!manip_pkt(0, skb, 0, &target, manip))
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800512 return 0;
513 }
514
515 return 1;
516}
517EXPORT_SYMBOL_GPL(nf_nat_icmp_reply_translation);
518
519/* Protocol registration. */
Patrick McHardy2b628a02007-12-17 22:37:36 -0800520int nf_nat_protocol_register(const struct nf_nat_protocol *proto)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800521{
522 int ret = 0;
523
Patrick McHardy02502f62008-01-31 04:43:06 -0800524 spin_lock_bh(&nf_nat_lock);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800525 if (nf_nat_protos[proto->protonum] != &nf_nat_unknown_protocol) {
526 ret = -EBUSY;
527 goto out;
528 }
Patrick McHardye22a0542007-02-12 11:12:26 -0800529 rcu_assign_pointer(nf_nat_protos[proto->protonum], proto);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800530 out:
Patrick McHardy02502f62008-01-31 04:43:06 -0800531 spin_unlock_bh(&nf_nat_lock);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800532 return ret;
533}
534EXPORT_SYMBOL(nf_nat_protocol_register);
535
536/* Noone stores the protocol anywhere; simply delete it. */
Patrick McHardy2b628a02007-12-17 22:37:36 -0800537void nf_nat_protocol_unregister(const struct nf_nat_protocol *proto)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800538{
Patrick McHardy02502f62008-01-31 04:43:06 -0800539 spin_lock_bh(&nf_nat_lock);
Patrick McHardye22a0542007-02-12 11:12:26 -0800540 rcu_assign_pointer(nf_nat_protos[proto->protonum],
541 &nf_nat_unknown_protocol);
Patrick McHardy02502f62008-01-31 04:43:06 -0800542 spin_unlock_bh(&nf_nat_lock);
Patrick McHardye22a0542007-02-12 11:12:26 -0800543 synchronize_rcu();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800544}
545EXPORT_SYMBOL(nf_nat_protocol_unregister);
546
Yasuyuki Kozakaid8a05092007-07-07 22:26:16 -0700547/* Noone using conntrack by the time this called. */
548static void nf_nat_cleanup_conntrack(struct nf_conn *ct)
549{
550 struct nf_conn_nat *nat = nf_ct_ext_find(ct, NF_CT_EXT_NAT);
551
Yasuyuki Kozakaib6b84d42007-07-07 22:26:35 -0700552 if (nat == NULL || nat->ct == NULL)
Yasuyuki Kozakaid8a05092007-07-07 22:26:16 -0700553 return;
554
Yasuyuki Kozakaib6b84d42007-07-07 22:26:35 -0700555 NF_CT_ASSERT(nat->ct->status & IPS_NAT_DONE_MASK);
Yasuyuki Kozakaid8a05092007-07-07 22:26:16 -0700556
Patrick McHardy02502f62008-01-31 04:43:06 -0800557 spin_lock_bh(&nf_nat_lock);
Patrick McHardy4d354c52008-01-31 04:42:37 -0800558 hlist_del_rcu(&nat->bysource);
Patrick McHardy02502f62008-01-31 04:43:06 -0800559 spin_unlock_bh(&nf_nat_lock);
Yasuyuki Kozakaid8a05092007-07-07 22:26:16 -0700560}
561
Patrick McHardy86577c62008-02-07 17:56:34 -0800562static void nf_nat_move_storage(void *new, void *old)
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700563{
Patrick McHardy86577c62008-02-07 17:56:34 -0800564 struct nf_conn_nat *new_nat = new;
565 struct nf_conn_nat *old_nat = old;
Yasuyuki Kozakaib6b84d42007-07-07 22:26:35 -0700566 struct nf_conn *ct = old_nat->ct;
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700567
Evgeniy Polyakov1f305322007-11-20 04:27:35 -0800568 if (!ct || !(ct->status & IPS_NAT_DONE_MASK))
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700569 return;
570
Patrick McHardy02502f62008-01-31 04:43:06 -0800571 spin_lock_bh(&nf_nat_lock);
Yasuyuki Kozakaib6b84d42007-07-07 22:26:35 -0700572 new_nat->ct = ct;
Patrick McHardy68b80f12008-06-17 15:51:47 -0700573 hlist_replace_rcu(&old_nat->bysource, &new_nat->bysource);
Patrick McHardy02502f62008-01-31 04:43:06 -0800574 spin_unlock_bh(&nf_nat_lock);
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700575}
576
Patrick McHardy61eb3102007-07-07 22:27:06 -0700577static struct nf_ct_ext_type nat_extend __read_mostly = {
Yasuyuki Kozakaid8a05092007-07-07 22:26:16 -0700578 .len = sizeof(struct nf_conn_nat),
579 .align = __alignof__(struct nf_conn_nat),
580 .destroy = nf_nat_cleanup_conntrack,
581 .move = nf_nat_move_storage,
582 .id = NF_CT_EXT_NAT,
583 .flags = NF_CT_EXT_F_PREALLOC,
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700584};
585
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800586static int __init nf_nat_init(void)
587{
588 size_t i;
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700589 int ret;
590
Jan Engelhardt475959d2008-04-09 15:14:58 -0700591 need_ipv4_conntrack();
592
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700593 ret = nf_ct_extend_register(&nat_extend);
594 if (ret < 0) {
595 printk(KERN_ERR "nf_nat_core: Unable to register extension\n");
596 return ret;
597 }
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800598
599 /* Leave them the same for the moment. */
600 nf_nat_htable_size = nf_conntrack_htable_size;
601
Patrick McHardy53aba592007-07-07 22:30:27 -0700602 bysource = nf_ct_alloc_hashtable(&nf_nat_htable_size,
603 &nf_nat_vmalloced);
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700604 if (!bysource) {
605 ret = -ENOMEM;
606 goto cleanup_extend;
607 }
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800608
609 /* Sew in builtin protocols. */
Patrick McHardy02502f62008-01-31 04:43:06 -0800610 spin_lock_bh(&nf_nat_lock);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800611 for (i = 0; i < MAX_IP_NAT_PROTO; i++)
Patrick McHardye22a0542007-02-12 11:12:26 -0800612 rcu_assign_pointer(nf_nat_protos[i], &nf_nat_unknown_protocol);
613 rcu_assign_pointer(nf_nat_protos[IPPROTO_TCP], &nf_nat_protocol_tcp);
614 rcu_assign_pointer(nf_nat_protos[IPPROTO_UDP], &nf_nat_protocol_udp);
615 rcu_assign_pointer(nf_nat_protos[IPPROTO_ICMP], &nf_nat_protocol_icmp);
Patrick McHardy02502f62008-01-31 04:43:06 -0800616 spin_unlock_bh(&nf_nat_lock);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800617
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800618 /* Initialize fake conntrack so that NAT will skip it */
619 nf_conntrack_untracked.status |= IPS_NAT_DONE_MASK;
620
621 l3proto = nf_ct_l3proto_find_get((u_int16_t)AF_INET);
Patrick McHardydd13b012008-04-14 11:15:52 +0200622
623 BUG_ON(nf_nat_seq_adjust_hook != NULL);
624 rcu_assign_pointer(nf_nat_seq_adjust_hook, nf_nat_seq_adjust);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800625 return 0;
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700626
627 cleanup_extend:
628 nf_ct_extend_unregister(&nat_extend);
629 return ret;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800630}
631
632/* Clear NAT section of all conntracks, in case we're loaded again. */
633static int clean_nat(struct nf_conn *i, void *data)
634{
635 struct nf_conn_nat *nat = nfct_nat(i);
636
637 if (!nat)
638 return 0;
Li Zefane0bf9cf2007-11-13 02:57:16 -0800639 memset(nat, 0, sizeof(*nat));
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800640 i->status &= ~(IPS_NAT_MASK | IPS_NAT_DONE_MASK | IPS_SEQ_ADJUST);
641 return 0;
642}
643
644static void __exit nf_nat_cleanup(void)
645{
646 nf_ct_iterate_cleanup(&clean_nat, NULL);
Patrick McHardy982d9a92007-02-12 11:14:11 -0800647 synchronize_rcu();
Patrick McHardy53aba592007-07-07 22:30:27 -0700648 nf_ct_free_hashtable(bysource, nf_nat_vmalloced, nf_nat_htable_size);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800649 nf_ct_l3proto_put(l3proto);
Yasuyuki Kozakai2d59e5c2007-07-07 22:24:28 -0700650 nf_ct_extend_unregister(&nat_extend);
Patrick McHardydd13b012008-04-14 11:15:52 +0200651 rcu_assign_pointer(nf_nat_seq_adjust_hook, NULL);
652 synchronize_net();
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800653}
654
655MODULE_LICENSE("GPL");
656
657module_init(nf_nat_init);
658module_exit(nf_nat_cleanup);