netfilter: nat: remove nf_nat_l4proto struct
[linux-2.6-block.git] / net / netfilter / nf_nat_core.c
1 /*
2  * (C) 1999-2001 Paul `Rusty' Russell
3  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
4  * (C) 2011 Patrick McHardy <kaber@trash.net>
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/timer.h>
16 #include <linux/skbuff.h>
17 #include <linux/gfp.h>
18 #include <net/xfrm.h>
19 #include <linux/jhash.h>
20 #include <linux/rtnetlink.h>
21
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_conntrack_core.h>
24 #include <net/netfilter/nf_nat.h>
25 #include <net/netfilter/nf_nat_l3proto.h>
26 #include <net/netfilter/nf_nat_core.h>
27 #include <net/netfilter/nf_nat_helper.h>
28 #include <net/netfilter/nf_conntrack_helper.h>
29 #include <net/netfilter/nf_conntrack_seqadj.h>
30 #include <net/netfilter/nf_conntrack_zones.h>
31 #include <linux/netfilter/nf_nat.h>
32
33 #include "nf_internals.h"
34
35 static spinlock_t nf_nat_locks[CONNTRACK_LOCKS];
36
37 static DEFINE_MUTEX(nf_nat_proto_mutex);
38 static const struct nf_nat_l3proto __rcu *nf_nat_l3protos[NFPROTO_NUMPROTO]
39                                                 __read_mostly;
40 static unsigned int nat_net_id __read_mostly;
41
42 static struct hlist_head *nf_nat_bysource __read_mostly;
43 static unsigned int nf_nat_htable_size __read_mostly;
44 static unsigned int nf_nat_hash_rnd __read_mostly;
45
46 struct nf_nat_lookup_hook_priv {
47         struct nf_hook_entries __rcu *entries;
48
49         struct rcu_head rcu_head;
50 };
51
52 struct nf_nat_hooks_net {
53         struct nf_hook_ops *nat_hook_ops;
54         unsigned int users;
55 };
56
57 struct nat_net {
58         struct nf_nat_hooks_net nat_proto_net[NFPROTO_NUMPROTO];
59 };
60
61 inline const struct nf_nat_l3proto *
62 __nf_nat_l3proto_find(u8 family)
63 {
64         return rcu_dereference(nf_nat_l3protos[family]);
65 }
66
67 #ifdef CONFIG_XFRM
68 static void __nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl)
69 {
70         const struct nf_nat_l3proto *l3proto;
71         const struct nf_conn *ct;
72         enum ip_conntrack_info ctinfo;
73         enum ip_conntrack_dir dir;
74         unsigned  long statusbit;
75         u8 family;
76
77         ct = nf_ct_get(skb, &ctinfo);
78         if (ct == NULL)
79                 return;
80
81         family = nf_ct_l3num(ct);
82         l3proto = __nf_nat_l3proto_find(family);
83         if (l3proto == NULL)
84                 return;
85
86         dir = CTINFO2DIR(ctinfo);
87         if (dir == IP_CT_DIR_ORIGINAL)
88                 statusbit = IPS_DST_NAT;
89         else
90                 statusbit = IPS_SRC_NAT;
91
92         l3proto->decode_session(skb, ct, dir, statusbit, fl);
93 }
94
95 int nf_xfrm_me_harder(struct net *net, struct sk_buff *skb, unsigned int family)
96 {
97         struct flowi fl;
98         unsigned int hh_len;
99         struct dst_entry *dst;
100         struct sock *sk = skb->sk;
101         int err;
102
103         err = xfrm_decode_session(skb, &fl, family);
104         if (err < 0)
105                 return err;
106
107         dst = skb_dst(skb);
108         if (dst->xfrm)
109                 dst = ((struct xfrm_dst *)dst)->route;
110         dst_hold(dst);
111
112         if (sk && !net_eq(net, sock_net(sk)))
113                 sk = NULL;
114
115         dst = xfrm_lookup(net, dst, &fl, sk, 0);
116         if (IS_ERR(dst))
117                 return PTR_ERR(dst);
118
119         skb_dst_drop(skb);
120         skb_dst_set(skb, dst);
121
122         /* Change in oif may mean change in hh_len. */
123         hh_len = skb_dst(skb)->dev->hard_header_len;
124         if (skb_headroom(skb) < hh_len &&
125             pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
126                 return -ENOMEM;
127         return 0;
128 }
129 EXPORT_SYMBOL(nf_xfrm_me_harder);
130 #endif /* CONFIG_XFRM */
131
132 /* We keep an extra hash for each conntrack, for fast searching. */
133 static unsigned int
134 hash_by_src(const struct net *n, const struct nf_conntrack_tuple *tuple)
135 {
136         unsigned int hash;
137
138         get_random_once(&nf_nat_hash_rnd, sizeof(nf_nat_hash_rnd));
139
140         /* Original src, to ensure we map it consistently if poss. */
141         hash = jhash2((u32 *)&tuple->src, sizeof(tuple->src) / sizeof(u32),
142                       tuple->dst.protonum ^ nf_nat_hash_rnd ^ net_hash_mix(n));
143
144         return reciprocal_scale(hash, nf_nat_htable_size);
145 }
146
147 /* Is this tuple already taken? (not by us) */
148 int
149 nf_nat_used_tuple(const struct nf_conntrack_tuple *tuple,
150                   const struct nf_conn *ignored_conntrack)
151 {
152         /* Conntrack tracking doesn't keep track of outgoing tuples; only
153          * incoming ones.  NAT means they don't have a fixed mapping,
154          * so we invert the tuple and look for the incoming reply.
155          *
156          * We could keep a separate hash if this proves too slow.
157          */
158         struct nf_conntrack_tuple reply;
159
160         nf_ct_invert_tuplepr(&reply, tuple);
161         return nf_conntrack_tuple_taken(&reply, ignored_conntrack);
162 }
163 EXPORT_SYMBOL(nf_nat_used_tuple);
164
165 static bool nf_nat_inet_in_range(const struct nf_conntrack_tuple *t,
166                                  const struct nf_nat_range2 *range)
167 {
168         if (t->src.l3num == NFPROTO_IPV4)
169                 return ntohl(t->src.u3.ip) >= ntohl(range->min_addr.ip) &&
170                        ntohl(t->src.u3.ip) <= ntohl(range->max_addr.ip);
171
172         return ipv6_addr_cmp(&t->src.u3.in6, &range->min_addr.in6) >= 0 &&
173                ipv6_addr_cmp(&t->src.u3.in6, &range->max_addr.in6) <= 0;
174 }
175
176 /* Is the manipable part of the tuple between min and max incl? */
177 static bool l4proto_in_range(const struct nf_conntrack_tuple *tuple,
178                              enum nf_nat_manip_type maniptype,
179                              const union nf_conntrack_man_proto *min,
180                              const union nf_conntrack_man_proto *max)
181 {
182         __be16 port;
183
184         switch (tuple->dst.protonum) {
185         case IPPROTO_ICMP: /* fallthrough */
186         case IPPROTO_ICMPV6:
187                 return ntohs(tuple->src.u.icmp.id) >= ntohs(min->icmp.id) &&
188                        ntohs(tuple->src.u.icmp.id) <= ntohs(max->icmp.id);
189         case IPPROTO_GRE: /* all fall though */
190         case IPPROTO_TCP:
191         case IPPROTO_UDP:
192         case IPPROTO_UDPLITE:
193         case IPPROTO_DCCP:
194         case IPPROTO_SCTP:
195                 if (maniptype == NF_NAT_MANIP_SRC)
196                         port = tuple->src.u.all;
197                 else
198                         port = tuple->dst.u.all;
199
200                 return ntohs(port) >= ntohs(min->all) &&
201                        ntohs(port) <= ntohs(max->all);
202         default:
203                 return true;
204         }
205 }
206
207 /* If we source map this tuple so reply looks like reply_tuple, will
208  * that meet the constraints of range.
209  */
210 static int in_range(const struct nf_conntrack_tuple *tuple,
211                     const struct nf_nat_range2 *range)
212 {
213         /* If we are supposed to map IPs, then we must be in the
214          * range specified, otherwise let this drag us onto a new src IP.
215          */
216         if (range->flags & NF_NAT_RANGE_MAP_IPS &&
217             !nf_nat_inet_in_range(tuple, range))
218                 return 0;
219
220         if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED))
221                 return 1;
222
223         return l4proto_in_range(tuple, NF_NAT_MANIP_SRC,
224                                 &range->min_proto, &range->max_proto);
225 }
226
227 static inline int
228 same_src(const struct nf_conn *ct,
229          const struct nf_conntrack_tuple *tuple)
230 {
231         const struct nf_conntrack_tuple *t;
232
233         t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
234         return (t->dst.protonum == tuple->dst.protonum &&
235                 nf_inet_addr_cmp(&t->src.u3, &tuple->src.u3) &&
236                 t->src.u.all == tuple->src.u.all);
237 }
238
239 /* Only called for SRC manip */
240 static int
241 find_appropriate_src(struct net *net,
242                      const struct nf_conntrack_zone *zone,
243                      const struct nf_conntrack_tuple *tuple,
244                      struct nf_conntrack_tuple *result,
245                      const struct nf_nat_range2 *range)
246 {
247         unsigned int h = hash_by_src(net, tuple);
248         const struct nf_conn *ct;
249
250         hlist_for_each_entry_rcu(ct, &nf_nat_bysource[h], nat_bysource) {
251                 if (same_src(ct, tuple) &&
252                     net_eq(net, nf_ct_net(ct)) &&
253                     nf_ct_zone_equal(ct, zone, IP_CT_DIR_ORIGINAL)) {
254                         /* Copy source part from reply tuple. */
255                         nf_ct_invert_tuplepr(result,
256                                        &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
257                         result->dst = tuple->dst;
258
259                         if (in_range(result, range))
260                                 return 1;
261                 }
262         }
263         return 0;
264 }
265
266 /* For [FUTURE] fragmentation handling, we want the least-used
267  * src-ip/dst-ip/proto triple.  Fairness doesn't come into it.  Thus
268  * if the range specifies 1.2.3.4 ports 10000-10005 and 1.2.3.5 ports
269  * 1-65535, we don't do pro-rata allocation based on ports; we choose
270  * the ip with the lowest src-ip/dst-ip/proto usage.
271  */
272 static void
273 find_best_ips_proto(const struct nf_conntrack_zone *zone,
274                     struct nf_conntrack_tuple *tuple,
275                     const struct nf_nat_range2 *range,
276                     const struct nf_conn *ct,
277                     enum nf_nat_manip_type maniptype)
278 {
279         union nf_inet_addr *var_ipp;
280         unsigned int i, max;
281         /* Host order */
282         u32 minip, maxip, j, dist;
283         bool full_range;
284
285         /* No IP mapping?  Do nothing. */
286         if (!(range->flags & NF_NAT_RANGE_MAP_IPS))
287                 return;
288
289         if (maniptype == NF_NAT_MANIP_SRC)
290                 var_ipp = &tuple->src.u3;
291         else
292                 var_ipp = &tuple->dst.u3;
293
294         /* Fast path: only one choice. */
295         if (nf_inet_addr_cmp(&range->min_addr, &range->max_addr)) {
296                 *var_ipp = range->min_addr;
297                 return;
298         }
299
300         if (nf_ct_l3num(ct) == NFPROTO_IPV4)
301                 max = sizeof(var_ipp->ip) / sizeof(u32) - 1;
302         else
303                 max = sizeof(var_ipp->ip6) / sizeof(u32) - 1;
304
305         /* Hashing source and destination IPs gives a fairly even
306          * spread in practice (if there are a small number of IPs
307          * involved, there usually aren't that many connections
308          * anyway).  The consistency means that servers see the same
309          * client coming from the same IP (some Internet Banking sites
310          * like this), even across reboots.
311          */
312         j = jhash2((u32 *)&tuple->src.u3, sizeof(tuple->src.u3) / sizeof(u32),
313                    range->flags & NF_NAT_RANGE_PERSISTENT ?
314                         0 : (__force u32)tuple->dst.u3.all[max] ^ zone->id);
315
316         full_range = false;
317         for (i = 0; i <= max; i++) {
318                 /* If first bytes of the address are at the maximum, use the
319                  * distance. Otherwise use the full range.
320                  */
321                 if (!full_range) {
322                         minip = ntohl((__force __be32)range->min_addr.all[i]);
323                         maxip = ntohl((__force __be32)range->max_addr.all[i]);
324                         dist  = maxip - minip + 1;
325                 } else {
326                         minip = 0;
327                         dist  = ~0;
328                 }
329
330                 var_ipp->all[i] = (__force __u32)
331                         htonl(minip + reciprocal_scale(j, dist));
332                 if (var_ipp->all[i] != range->max_addr.all[i])
333                         full_range = true;
334
335                 if (!(range->flags & NF_NAT_RANGE_PERSISTENT))
336                         j ^= (__force u32)tuple->dst.u3.all[i];
337         }
338 }
339
340 /* Alter the per-proto part of the tuple (depending on maniptype), to
341  * give a unique tuple in the given range if possible.
342  *
343  * Per-protocol part of tuple is initialized to the incoming packet.
344  */
345 static void nf_nat_l4proto_unique_tuple(struct nf_conntrack_tuple *tuple,
346                                         const struct nf_nat_range2 *range,
347                                         enum nf_nat_manip_type maniptype,
348                                         const struct nf_conn *ct)
349 {
350         unsigned int range_size, min, max, i, attempts;
351         __be16 *keyptr;
352         u16 off;
353         static const unsigned int max_attempts = 128;
354
355         switch (tuple->dst.protonum) {
356         case IPPROTO_ICMP: /* fallthrough */
357         case IPPROTO_ICMPV6:
358                 /* id is same for either direction... */
359                 keyptr = &tuple->src.u.icmp.id;
360                 min = range->min_proto.icmp.id;
361                 range_size = ntohs(range->max_proto.icmp.id) -
362                              ntohs(range->min_proto.icmp.id) + 1;
363                 goto find_free_id;
364 #if IS_ENABLED(CONFIG_NF_CT_PROTO_GRE)
365         case IPPROTO_GRE:
366                 /* If there is no master conntrack we are not PPTP,
367                    do not change tuples */
368                 if (!ct->master)
369                         return;
370
371                 if (maniptype == NF_NAT_MANIP_SRC)
372                         keyptr = &tuple->src.u.gre.key;
373                 else
374                         keyptr = &tuple->dst.u.gre.key;
375
376                 if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
377                         min = 1;
378                         range_size = 65535;
379                 } else {
380                         min = ntohs(range->min_proto.gre.key);
381                         range_size = ntohs(range->max_proto.gre.key) - min + 1;
382                 }
383                 goto find_free_id;
384 #endif
385         case IPPROTO_UDP:       /* fallthrough */
386         case IPPROTO_UDPLITE:   /* fallthrough */
387         case IPPROTO_TCP:       /* fallthrough */
388         case IPPROTO_SCTP:      /* fallthrough */
389         case IPPROTO_DCCP:      /* fallthrough */
390                 if (maniptype == NF_NAT_MANIP_SRC)
391                         keyptr = &tuple->src.u.all;
392                 else
393                         keyptr = &tuple->dst.u.all;
394
395                 break;
396         default:
397                 return;
398         }
399
400         /* If no range specified... */
401         if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) {
402                 /* If it's dst rewrite, can't change port */
403                 if (maniptype == NF_NAT_MANIP_DST)
404                         return;
405
406                 if (ntohs(*keyptr) < 1024) {
407                         /* Loose convention: >> 512 is credential passing */
408                         if (ntohs(*keyptr) < 512) {
409                                 min = 1;
410                                 range_size = 511 - min + 1;
411                         } else {
412                                 min = 600;
413                                 range_size = 1023 - min + 1;
414                         }
415                 } else {
416                         min = 1024;
417                         range_size = 65535 - 1024 + 1;
418                 }
419         } else {
420                 min = ntohs(range->min_proto.all);
421                 max = ntohs(range->max_proto.all);
422                 if (unlikely(max < min))
423                         swap(max, min);
424                 range_size = max - min + 1;
425         }
426
427 find_free_id:
428         if (range->flags & NF_NAT_RANGE_PROTO_OFFSET)
429                 off = (ntohs(*keyptr) - ntohs(range->base_proto.all));
430         else
431                 off = prandom_u32();
432
433         attempts = range_size;
434         if (attempts > max_attempts)
435                 attempts = max_attempts;
436
437         /* We are in softirq; doing a search of the entire range risks
438          * soft lockup when all tuples are already used.
439          *
440          * If we can't find any free port from first offset, pick a new
441          * one and try again, with ever smaller search window.
442          */
443 another_round:
444         for (i = 0; i < attempts; i++, off++) {
445                 *keyptr = htons(min + off % range_size);
446                 if (!nf_nat_used_tuple(tuple, ct))
447                         return;
448         }
449
450         if (attempts >= range_size || attempts < 16)
451                 return;
452         attempts /= 2;
453         off = prandom_u32();
454         goto another_round;
455 }
456
457 /* Manipulate the tuple into the range given. For NF_INET_POST_ROUTING,
458  * we change the source to map into the range. For NF_INET_PRE_ROUTING
459  * and NF_INET_LOCAL_OUT, we change the destination to map into the
460  * range. It might not be possible to get a unique tuple, but we try.
461  * At worst (or if we race), we will end up with a final duplicate in
462  * __ip_conntrack_confirm and drop the packet. */
463 static void
464 get_unique_tuple(struct nf_conntrack_tuple *tuple,
465                  const struct nf_conntrack_tuple *orig_tuple,
466                  const struct nf_nat_range2 *range,
467                  struct nf_conn *ct,
468                  enum nf_nat_manip_type maniptype)
469 {
470         const struct nf_conntrack_zone *zone;
471         struct net *net = nf_ct_net(ct);
472
473         zone = nf_ct_zone(ct);
474
475         /* 1) If this srcip/proto/src-proto-part is currently mapped,
476          * and that same mapping gives a unique tuple within the given
477          * range, use that.
478          *
479          * This is only required for source (ie. NAT/masq) mappings.
480          * So far, we don't do local source mappings, so multiple
481          * manips not an issue.
482          */
483         if (maniptype == NF_NAT_MANIP_SRC &&
484             !(range->flags & NF_NAT_RANGE_PROTO_RANDOM_ALL)) {
485                 /* try the original tuple first */
486                 if (in_range(orig_tuple, range)) {
487                         if (!nf_nat_used_tuple(orig_tuple, ct)) {
488                                 *tuple = *orig_tuple;
489                                 return;
490                         }
491                 } else if (find_appropriate_src(net, zone,
492                                                 orig_tuple, tuple, range)) {
493                         pr_debug("get_unique_tuple: Found current src map\n");
494                         if (!nf_nat_used_tuple(tuple, ct))
495                                 return;
496                 }
497         }
498
499         /* 2) Select the least-used IP/proto combination in the given range */
500         *tuple = *orig_tuple;
501         find_best_ips_proto(zone, tuple, range, ct, maniptype);
502
503         /* 3) The per-protocol part of the manip is made to map into
504          * the range to make a unique tuple.
505          */
506
507         /* Only bother mapping if it's not already in range and unique */
508         if (!(range->flags & NF_NAT_RANGE_PROTO_RANDOM_ALL)) {
509                 if (range->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
510                         if (!(range->flags & NF_NAT_RANGE_PROTO_OFFSET) &&
511                             l4proto_in_range(tuple, maniptype,
512                                   &range->min_proto,
513                                   &range->max_proto) &&
514                             (range->min_proto.all == range->max_proto.all ||
515                              !nf_nat_used_tuple(tuple, ct)))
516                                 return;
517                 } else if (!nf_nat_used_tuple(tuple, ct)) {
518                         return;
519                 }
520         }
521
522         /* Last chance: get protocol to try to obtain unique tuple. */
523         nf_nat_l4proto_unique_tuple(tuple, range, maniptype, ct);
524 }
525
526 struct nf_conn_nat *nf_ct_nat_ext_add(struct nf_conn *ct)
527 {
528         struct nf_conn_nat *nat = nfct_nat(ct);
529         if (nat)
530                 return nat;
531
532         if (!nf_ct_is_confirmed(ct))
533                 nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
534
535         return nat;
536 }
537 EXPORT_SYMBOL_GPL(nf_ct_nat_ext_add);
538
539 unsigned int
540 nf_nat_setup_info(struct nf_conn *ct,
541                   const struct nf_nat_range2 *range,
542                   enum nf_nat_manip_type maniptype)
543 {
544         struct net *net = nf_ct_net(ct);
545         struct nf_conntrack_tuple curr_tuple, new_tuple;
546
547         /* Can't setup nat info for confirmed ct. */
548         if (nf_ct_is_confirmed(ct))
549                 return NF_ACCEPT;
550
551         WARN_ON(maniptype != NF_NAT_MANIP_SRC &&
552                 maniptype != NF_NAT_MANIP_DST);
553
554         if (WARN_ON(nf_nat_initialized(ct, maniptype)))
555                 return NF_DROP;
556
557         /* What we've got will look like inverse of reply. Normally
558          * this is what is in the conntrack, except for prior
559          * manipulations (future optimization: if num_manips == 0,
560          * orig_tp = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
561          */
562         nf_ct_invert_tuplepr(&curr_tuple,
563                              &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
564
565         get_unique_tuple(&new_tuple, &curr_tuple, range, ct, maniptype);
566
567         if (!nf_ct_tuple_equal(&new_tuple, &curr_tuple)) {
568                 struct nf_conntrack_tuple reply;
569
570                 /* Alter conntrack table so will recognize replies. */
571                 nf_ct_invert_tuplepr(&reply, &new_tuple);
572                 nf_conntrack_alter_reply(ct, &reply);
573
574                 /* Non-atomic: we own this at the moment. */
575                 if (maniptype == NF_NAT_MANIP_SRC)
576                         ct->status |= IPS_SRC_NAT;
577                 else
578                         ct->status |= IPS_DST_NAT;
579
580                 if (nfct_help(ct) && !nfct_seqadj(ct))
581                         if (!nfct_seqadj_ext_add(ct))
582                                 return NF_DROP;
583         }
584
585         if (maniptype == NF_NAT_MANIP_SRC) {
586                 unsigned int srchash;
587                 spinlock_t *lock;
588
589                 srchash = hash_by_src(net,
590                                       &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
591                 lock = &nf_nat_locks[srchash % CONNTRACK_LOCKS];
592                 spin_lock_bh(lock);
593                 hlist_add_head_rcu(&ct->nat_bysource,
594                                    &nf_nat_bysource[srchash]);
595                 spin_unlock_bh(lock);
596         }
597
598         /* It's done. */
599         if (maniptype == NF_NAT_MANIP_DST)
600                 ct->status |= IPS_DST_NAT_DONE;
601         else
602                 ct->status |= IPS_SRC_NAT_DONE;
603
604         return NF_ACCEPT;
605 }
606 EXPORT_SYMBOL(nf_nat_setup_info);
607
608 static unsigned int
609 __nf_nat_alloc_null_binding(struct nf_conn *ct, enum nf_nat_manip_type manip)
610 {
611         /* Force range to this IP; let proto decide mapping for
612          * per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
613          * Use reply in case it's already been mangled (eg local packet).
614          */
615         union nf_inet_addr ip =
616                 (manip == NF_NAT_MANIP_SRC ?
617                 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3 :
618                 ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3);
619         struct nf_nat_range2 range = {
620                 .flags          = NF_NAT_RANGE_MAP_IPS,
621                 .min_addr       = ip,
622                 .max_addr       = ip,
623         };
624         return nf_nat_setup_info(ct, &range, manip);
625 }
626
627 unsigned int
628 nf_nat_alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
629 {
630         return __nf_nat_alloc_null_binding(ct, HOOK2MANIP(hooknum));
631 }
632 EXPORT_SYMBOL_GPL(nf_nat_alloc_null_binding);
633
634 static unsigned int nf_nat_manip_pkt(struct sk_buff *skb, struct nf_conn *ct,
635                                      enum nf_nat_manip_type mtype,
636                                      enum ip_conntrack_dir dir)
637 {
638         const struct nf_nat_l3proto *l3proto;
639         struct nf_conntrack_tuple target;
640
641         /* We are aiming to look like inverse of other direction. */
642         nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
643
644         l3proto = __nf_nat_l3proto_find(target.src.l3num);
645         if (!l3proto->manip_pkt(skb, 0, &target, mtype))
646                 return NF_DROP;
647
648         return NF_ACCEPT;
649 }
650
651 /* Do packet manipulations according to nf_nat_setup_info. */
652 unsigned int nf_nat_packet(struct nf_conn *ct,
653                            enum ip_conntrack_info ctinfo,
654                            unsigned int hooknum,
655                            struct sk_buff *skb)
656 {
657         enum nf_nat_manip_type mtype = HOOK2MANIP(hooknum);
658         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
659         unsigned int verdict = NF_ACCEPT;
660         unsigned long statusbit;
661
662         if (mtype == NF_NAT_MANIP_SRC)
663                 statusbit = IPS_SRC_NAT;
664         else
665                 statusbit = IPS_DST_NAT;
666
667         /* Invert if this is reply dir. */
668         if (dir == IP_CT_DIR_REPLY)
669                 statusbit ^= IPS_NAT_MASK;
670
671         /* Non-atomic: these bits don't change. */
672         if (ct->status & statusbit)
673                 verdict = nf_nat_manip_pkt(skb, ct, mtype, dir);
674
675         return verdict;
676 }
677 EXPORT_SYMBOL_GPL(nf_nat_packet);
678
679 unsigned int
680 nf_nat_inet_fn(void *priv, struct sk_buff *skb,
681                const struct nf_hook_state *state)
682 {
683         struct nf_conn *ct;
684         enum ip_conntrack_info ctinfo;
685         struct nf_conn_nat *nat;
686         /* maniptype == SRC for postrouting. */
687         enum nf_nat_manip_type maniptype = HOOK2MANIP(state->hook);
688
689         ct = nf_ct_get(skb, &ctinfo);
690         /* Can't track?  It's not due to stress, or conntrack would
691          * have dropped it.  Hence it's the user's responsibilty to
692          * packet filter it out, or implement conntrack/NAT for that
693          * protocol. 8) --RR
694          */
695         if (!ct)
696                 return NF_ACCEPT;
697
698         nat = nfct_nat(ct);
699
700         switch (ctinfo) {
701         case IP_CT_RELATED:
702         case IP_CT_RELATED_REPLY:
703                 /* Only ICMPs can be IP_CT_IS_REPLY.  Fallthrough */
704         case IP_CT_NEW:
705                 /* Seen it before?  This can happen for loopback, retrans,
706                  * or local packets.
707                  */
708                 if (!nf_nat_initialized(ct, maniptype)) {
709                         struct nf_nat_lookup_hook_priv *lpriv = priv;
710                         struct nf_hook_entries *e = rcu_dereference(lpriv->entries);
711                         unsigned int ret;
712                         int i;
713
714                         if (!e)
715                                 goto null_bind;
716
717                         for (i = 0; i < e->num_hook_entries; i++) {
718                                 ret = e->hooks[i].hook(e->hooks[i].priv, skb,
719                                                        state);
720                                 if (ret != NF_ACCEPT)
721                                         return ret;
722                                 if (nf_nat_initialized(ct, maniptype))
723                                         goto do_nat;
724                         }
725 null_bind:
726                         ret = nf_nat_alloc_null_binding(ct, state->hook);
727                         if (ret != NF_ACCEPT)
728                                 return ret;
729                 } else {
730                         pr_debug("Already setup manip %s for ct %p (status bits 0x%lx)\n",
731                                  maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
732                                  ct, ct->status);
733                         if (nf_nat_oif_changed(state->hook, ctinfo, nat,
734                                                state->out))
735                                 goto oif_changed;
736                 }
737                 break;
738         default:
739                 /* ESTABLISHED */
740                 WARN_ON(ctinfo != IP_CT_ESTABLISHED &&
741                         ctinfo != IP_CT_ESTABLISHED_REPLY);
742                 if (nf_nat_oif_changed(state->hook, ctinfo, nat, state->out))
743                         goto oif_changed;
744         }
745 do_nat:
746         return nf_nat_packet(ct, ctinfo, state->hook, skb);
747
748 oif_changed:
749         nf_ct_kill_acct(ct, ctinfo, skb);
750         return NF_DROP;
751 }
752 EXPORT_SYMBOL_GPL(nf_nat_inet_fn);
753
754 struct nf_nat_proto_clean {
755         u8      l3proto;
756         u8      l4proto;
757 };
758
759 /* kill conntracks with affected NAT section */
760 static int nf_nat_proto_remove(struct nf_conn *i, void *data)
761 {
762         const struct nf_nat_proto_clean *clean = data;
763
764         if ((clean->l3proto && nf_ct_l3num(i) != clean->l3proto) ||
765             (clean->l4proto && nf_ct_protonum(i) != clean->l4proto))
766                 return 0;
767
768         return i->status & IPS_NAT_MASK ? 1 : 0;
769 }
770
771 static void __nf_nat_cleanup_conntrack(struct nf_conn *ct)
772 {
773         unsigned int h;
774
775         h = hash_by_src(nf_ct_net(ct), &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
776         spin_lock_bh(&nf_nat_locks[h % CONNTRACK_LOCKS]);
777         hlist_del_rcu(&ct->nat_bysource);
778         spin_unlock_bh(&nf_nat_locks[h % CONNTRACK_LOCKS]);
779 }
780
781 static int nf_nat_proto_clean(struct nf_conn *ct, void *data)
782 {
783         if (nf_nat_proto_remove(ct, data))
784                 return 1;
785
786         /* This module is being removed and conntrack has nat null binding.
787          * Remove it from bysource hash, as the table will be freed soon.
788          *
789          * Else, when the conntrack is destoyed, nf_nat_cleanup_conntrack()
790          * will delete entry from already-freed table.
791          */
792         if (test_and_clear_bit(IPS_SRC_NAT_DONE_BIT, &ct->status))
793                 __nf_nat_cleanup_conntrack(ct);
794
795         /* don't delete conntrack.  Although that would make things a lot
796          * simpler, we'd end up flushing all conntracks on nat rmmod.
797          */
798         return 0;
799 }
800
801 static void nf_nat_l3proto_clean(u8 l3proto)
802 {
803         struct nf_nat_proto_clean clean = {
804                 .l3proto = l3proto,
805         };
806
807         nf_ct_iterate_destroy(nf_nat_proto_remove, &clean);
808 }
809
810 int nf_nat_l3proto_register(const struct nf_nat_l3proto *l3proto)
811 {
812         RCU_INIT_POINTER(nf_nat_l3protos[l3proto->l3proto], l3proto);
813         return 0;
814 }
815 EXPORT_SYMBOL_GPL(nf_nat_l3proto_register);
816
817 void nf_nat_l3proto_unregister(const struct nf_nat_l3proto *l3proto)
818 {
819         mutex_lock(&nf_nat_proto_mutex);
820         RCU_INIT_POINTER(nf_nat_l3protos[l3proto->l3proto], NULL);
821         mutex_unlock(&nf_nat_proto_mutex);
822         synchronize_rcu();
823
824         nf_nat_l3proto_clean(l3proto->l3proto);
825 }
826 EXPORT_SYMBOL_GPL(nf_nat_l3proto_unregister);
827
828 /* No one using conntrack by the time this called. */
829 static void nf_nat_cleanup_conntrack(struct nf_conn *ct)
830 {
831         if (ct->status & IPS_SRC_NAT_DONE)
832                 __nf_nat_cleanup_conntrack(ct);
833 }
834
835 static struct nf_ct_ext_type nat_extend __read_mostly = {
836         .len            = sizeof(struct nf_conn_nat),
837         .align          = __alignof__(struct nf_conn_nat),
838         .destroy        = nf_nat_cleanup_conntrack,
839         .id             = NF_CT_EXT_NAT,
840 };
841
842 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
843
844 #include <linux/netfilter/nfnetlink.h>
845 #include <linux/netfilter/nfnetlink_conntrack.h>
846
847 static const struct nla_policy protonat_nla_policy[CTA_PROTONAT_MAX+1] = {
848         [CTA_PROTONAT_PORT_MIN] = { .type = NLA_U16 },
849         [CTA_PROTONAT_PORT_MAX] = { .type = NLA_U16 },
850 };
851
852 static int nf_nat_l4proto_nlattr_to_range(struct nlattr *tb[],
853                                           struct nf_nat_range2 *range)
854 {
855         if (tb[CTA_PROTONAT_PORT_MIN]) {
856                 range->min_proto.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MIN]);
857                 range->max_proto.all = range->min_proto.all;
858                 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
859         }
860         if (tb[CTA_PROTONAT_PORT_MAX]) {
861                 range->max_proto.all = nla_get_be16(tb[CTA_PROTONAT_PORT_MAX]);
862                 range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
863         }
864         return 0;
865 }
866
867 static int nfnetlink_parse_nat_proto(struct nlattr *attr,
868                                      const struct nf_conn *ct,
869                                      struct nf_nat_range2 *range)
870 {
871         struct nlattr *tb[CTA_PROTONAT_MAX+1];
872         int err;
873
874         err = nla_parse_nested(tb, CTA_PROTONAT_MAX, attr,
875                                protonat_nla_policy, NULL);
876         if (err < 0)
877                 return err;
878
879         return nf_nat_l4proto_nlattr_to_range(tb, range);
880 }
881
882 static const struct nla_policy nat_nla_policy[CTA_NAT_MAX+1] = {
883         [CTA_NAT_V4_MINIP]      = { .type = NLA_U32 },
884         [CTA_NAT_V4_MAXIP]      = { .type = NLA_U32 },
885         [CTA_NAT_V6_MINIP]      = { .len = sizeof(struct in6_addr) },
886         [CTA_NAT_V6_MAXIP]      = { .len = sizeof(struct in6_addr) },
887         [CTA_NAT_PROTO]         = { .type = NLA_NESTED },
888 };
889
890 static int
891 nfnetlink_parse_nat(const struct nlattr *nat,
892                     const struct nf_conn *ct, struct nf_nat_range2 *range,
893                     const struct nf_nat_l3proto *l3proto)
894 {
895         struct nlattr *tb[CTA_NAT_MAX+1];
896         int err;
897
898         memset(range, 0, sizeof(*range));
899
900         err = nla_parse_nested(tb, CTA_NAT_MAX, nat, nat_nla_policy, NULL);
901         if (err < 0)
902                 return err;
903
904         err = l3proto->nlattr_to_range(tb, range);
905         if (err < 0)
906                 return err;
907
908         if (!tb[CTA_NAT_PROTO])
909                 return 0;
910
911         return nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO], ct, range);
912 }
913
914 /* This function is called under rcu_read_lock() */
915 static int
916 nfnetlink_parse_nat_setup(struct nf_conn *ct,
917                           enum nf_nat_manip_type manip,
918                           const struct nlattr *attr)
919 {
920         struct nf_nat_range2 range;
921         const struct nf_nat_l3proto *l3proto;
922         int err;
923
924         /* Should not happen, restricted to creating new conntracks
925          * via ctnetlink.
926          */
927         if (WARN_ON_ONCE(nf_nat_initialized(ct, manip)))
928                 return -EEXIST;
929
930         /* Make sure that L3 NAT is there by when we call nf_nat_setup_info to
931          * attach the null binding, otherwise this may oops.
932          */
933         l3proto = __nf_nat_l3proto_find(nf_ct_l3num(ct));
934         if (l3proto == NULL)
935                 return -EAGAIN;
936
937         /* No NAT information has been passed, allocate the null-binding */
938         if (attr == NULL)
939                 return __nf_nat_alloc_null_binding(ct, manip) == NF_DROP ? -ENOMEM : 0;
940
941         err = nfnetlink_parse_nat(attr, ct, &range, l3proto);
942         if (err < 0)
943                 return err;
944
945         return nf_nat_setup_info(ct, &range, manip) == NF_DROP ? -ENOMEM : 0;
946 }
947 #else
948 static int
949 nfnetlink_parse_nat_setup(struct nf_conn *ct,
950                           enum nf_nat_manip_type manip,
951                           const struct nlattr *attr)
952 {
953         return -EOPNOTSUPP;
954 }
955 #endif
956
957 static struct nf_ct_helper_expectfn follow_master_nat = {
958         .name           = "nat-follow-master",
959         .expectfn       = nf_nat_follow_master,
960 };
961
962 int nf_nat_register_fn(struct net *net, const struct nf_hook_ops *ops,
963                        const struct nf_hook_ops *orig_nat_ops, unsigned int ops_count)
964 {
965         struct nat_net *nat_net = net_generic(net, nat_net_id);
966         struct nf_nat_hooks_net *nat_proto_net;
967         struct nf_nat_lookup_hook_priv *priv;
968         unsigned int hooknum = ops->hooknum;
969         struct nf_hook_ops *nat_ops;
970         int i, ret;
971
972         if (WARN_ON_ONCE(ops->pf >= ARRAY_SIZE(nat_net->nat_proto_net)))
973                 return -EINVAL;
974
975         nat_proto_net = &nat_net->nat_proto_net[ops->pf];
976
977         for (i = 0; i < ops_count; i++) {
978                 if (WARN_ON(orig_nat_ops[i].pf != ops->pf))
979                         return -EINVAL;
980                 if (orig_nat_ops[i].hooknum == hooknum) {
981                         hooknum = i;
982                         break;
983                 }
984         }
985
986         if (WARN_ON_ONCE(i == ops_count))
987                 return -EINVAL;
988
989         mutex_lock(&nf_nat_proto_mutex);
990         if (!nat_proto_net->nat_hook_ops) {
991                 WARN_ON(nat_proto_net->users != 0);
992
993                 nat_ops = kmemdup(orig_nat_ops, sizeof(*orig_nat_ops) * ops_count, GFP_KERNEL);
994                 if (!nat_ops) {
995                         mutex_unlock(&nf_nat_proto_mutex);
996                         return -ENOMEM;
997                 }
998
999                 for (i = 0; i < ops_count; i++) {
1000                         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1001                         if (priv) {
1002                                 nat_ops[i].priv = priv;
1003                                 continue;
1004                         }
1005                         mutex_unlock(&nf_nat_proto_mutex);
1006                         while (i)
1007                                 kfree(nat_ops[--i].priv);
1008                         kfree(nat_ops);
1009                         return -ENOMEM;
1010                 }
1011
1012                 ret = nf_register_net_hooks(net, nat_ops, ops_count);
1013                 if (ret < 0) {
1014                         mutex_unlock(&nf_nat_proto_mutex);
1015                         for (i = 0; i < ops_count; i++)
1016                                 kfree(nat_ops[i].priv);
1017                         kfree(nat_ops);
1018                         return ret;
1019                 }
1020
1021                 nat_proto_net->nat_hook_ops = nat_ops;
1022         }
1023
1024         nat_ops = nat_proto_net->nat_hook_ops;
1025         priv = nat_ops[hooknum].priv;
1026         if (WARN_ON_ONCE(!priv)) {
1027                 mutex_unlock(&nf_nat_proto_mutex);
1028                 return -EOPNOTSUPP;
1029         }
1030
1031         ret = nf_hook_entries_insert_raw(&priv->entries, ops);
1032         if (ret == 0)
1033                 nat_proto_net->users++;
1034
1035         mutex_unlock(&nf_nat_proto_mutex);
1036         return ret;
1037 }
1038 EXPORT_SYMBOL_GPL(nf_nat_register_fn);
1039
1040 void nf_nat_unregister_fn(struct net *net, const struct nf_hook_ops *ops,
1041                           unsigned int ops_count)
1042 {
1043         struct nat_net *nat_net = net_generic(net, nat_net_id);
1044         struct nf_nat_hooks_net *nat_proto_net;
1045         struct nf_nat_lookup_hook_priv *priv;
1046         struct nf_hook_ops *nat_ops;
1047         int hooknum = ops->hooknum;
1048         int i;
1049
1050         if (ops->pf >= ARRAY_SIZE(nat_net->nat_proto_net))
1051                 return;
1052
1053         nat_proto_net = &nat_net->nat_proto_net[ops->pf];
1054
1055         mutex_lock(&nf_nat_proto_mutex);
1056         if (WARN_ON(nat_proto_net->users == 0))
1057                 goto unlock;
1058
1059         nat_proto_net->users--;
1060
1061         nat_ops = nat_proto_net->nat_hook_ops;
1062         for (i = 0; i < ops_count; i++) {
1063                 if (nat_ops[i].hooknum == hooknum) {
1064                         hooknum = i;
1065                         break;
1066                 }
1067         }
1068         if (WARN_ON_ONCE(i == ops_count))
1069                 goto unlock;
1070         priv = nat_ops[hooknum].priv;
1071         nf_hook_entries_delete_raw(&priv->entries, ops);
1072
1073         if (nat_proto_net->users == 0) {
1074                 nf_unregister_net_hooks(net, nat_ops, ops_count);
1075
1076                 for (i = 0; i < ops_count; i++) {
1077                         priv = nat_ops[i].priv;
1078                         kfree_rcu(priv, rcu_head);
1079                 }
1080
1081                 nat_proto_net->nat_hook_ops = NULL;
1082                 kfree(nat_ops);
1083         }
1084 unlock:
1085         mutex_unlock(&nf_nat_proto_mutex);
1086 }
1087 EXPORT_SYMBOL_GPL(nf_nat_unregister_fn);
1088
1089 static struct pernet_operations nat_net_ops = {
1090         .id = &nat_net_id,
1091         .size = sizeof(struct nat_net),
1092 };
1093
1094 static struct nf_nat_hook nat_hook = {
1095         .parse_nat_setup        = nfnetlink_parse_nat_setup,
1096 #ifdef CONFIG_XFRM
1097         .decode_session         = __nf_nat_decode_session,
1098 #endif
1099         .manip_pkt              = nf_nat_manip_pkt,
1100 };
1101
1102 static int __init nf_nat_init(void)
1103 {
1104         int ret, i;
1105
1106         /* Leave them the same for the moment. */
1107         nf_nat_htable_size = nf_conntrack_htable_size;
1108         if (nf_nat_htable_size < CONNTRACK_LOCKS)
1109                 nf_nat_htable_size = CONNTRACK_LOCKS;
1110
1111         nf_nat_bysource = nf_ct_alloc_hashtable(&nf_nat_htable_size, 0);
1112         if (!nf_nat_bysource)
1113                 return -ENOMEM;
1114
1115         ret = nf_ct_extend_register(&nat_extend);
1116         if (ret < 0) {
1117                 kvfree(nf_nat_bysource);
1118                 pr_err("Unable to register extension\n");
1119                 return ret;
1120         }
1121
1122         for (i = 0; i < CONNTRACK_LOCKS; i++)
1123                 spin_lock_init(&nf_nat_locks[i]);
1124
1125         ret = register_pernet_subsys(&nat_net_ops);
1126         if (ret < 0) {
1127                 nf_ct_extend_unregister(&nat_extend);
1128                 return ret;
1129         }
1130
1131         nf_ct_helper_expectfn_register(&follow_master_nat);
1132
1133         WARN_ON(nf_nat_hook != NULL);
1134         RCU_INIT_POINTER(nf_nat_hook, &nat_hook);
1135
1136         return 0;
1137 }
1138
1139 static void __exit nf_nat_cleanup(void)
1140 {
1141         struct nf_nat_proto_clean clean = {};
1142
1143         nf_ct_iterate_destroy(nf_nat_proto_clean, &clean);
1144
1145         nf_ct_extend_unregister(&nat_extend);
1146         nf_ct_helper_expectfn_unregister(&follow_master_nat);
1147         RCU_INIT_POINTER(nf_nat_hook, NULL);
1148
1149         synchronize_net();
1150         kvfree(nf_nat_bysource);
1151         unregister_pernet_subsys(&nat_net_ops);
1152 }
1153
1154 MODULE_LICENSE("GPL");
1155
1156 module_init(nf_nat_init);
1157 module_exit(nf_nat_cleanup);