netfilter: conntrack: check netns when comparing conntrack objects
[cascardo/linux.git] / net / netfilter / nf_conntrack_core.c
1 /* Connection state tracking for netfilter.  This is separated from,
2    but required by, the NAT layer; it can also be used by an iptables
3    extension. */
4
5 /* (C) 1999-2001 Paul `Rusty' Russell
6  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
7  * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
8  * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17 #include <linux/types.h>
18 #include <linux/netfilter.h>
19 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/skbuff.h>
22 #include <linux/proc_fs.h>
23 #include <linux/vmalloc.h>
24 #include <linux/stddef.h>
25 #include <linux/slab.h>
26 #include <linux/random.h>
27 #include <linux/jhash.h>
28 #include <linux/err.h>
29 #include <linux/percpu.h>
30 #include <linux/moduleparam.h>
31 #include <linux/notifier.h>
32 #include <linux/kernel.h>
33 #include <linux/netdevice.h>
34 #include <linux/socket.h>
35 #include <linux/mm.h>
36 #include <linux/nsproxy.h>
37 #include <linux/rculist_nulls.h>
38
39 #include <net/netfilter/nf_conntrack.h>
40 #include <net/netfilter/nf_conntrack_l3proto.h>
41 #include <net/netfilter/nf_conntrack_l4proto.h>
42 #include <net/netfilter/nf_conntrack_expect.h>
43 #include <net/netfilter/nf_conntrack_helper.h>
44 #include <net/netfilter/nf_conntrack_seqadj.h>
45 #include <net/netfilter/nf_conntrack_core.h>
46 #include <net/netfilter/nf_conntrack_extend.h>
47 #include <net/netfilter/nf_conntrack_acct.h>
48 #include <net/netfilter/nf_conntrack_ecache.h>
49 #include <net/netfilter/nf_conntrack_zones.h>
50 #include <net/netfilter/nf_conntrack_timestamp.h>
51 #include <net/netfilter/nf_conntrack_timeout.h>
52 #include <net/netfilter/nf_conntrack_labels.h>
53 #include <net/netfilter/nf_conntrack_synproxy.h>
54 #include <net/netfilter/nf_nat.h>
55 #include <net/netfilter/nf_nat_core.h>
56 #include <net/netfilter/nf_nat_helper.h>
57
58 #define NF_CONNTRACK_VERSION    "0.5.0"
59
60 int (*nfnetlink_parse_nat_setup_hook)(struct nf_conn *ct,
61                                       enum nf_nat_manip_type manip,
62                                       const struct nlattr *attr) __read_mostly;
63 EXPORT_SYMBOL_GPL(nfnetlink_parse_nat_setup_hook);
64
65 __cacheline_aligned_in_smp spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS];
66 EXPORT_SYMBOL_GPL(nf_conntrack_locks);
67
68 __cacheline_aligned_in_smp DEFINE_SPINLOCK(nf_conntrack_expect_lock);
69 EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock);
70
71 static __read_mostly spinlock_t nf_conntrack_locks_all_lock;
72 static __read_mostly seqcount_t nf_conntrack_generation;
73 static __read_mostly bool nf_conntrack_locks_all;
74
75 void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
76 {
77         spin_lock(lock);
78         while (unlikely(nf_conntrack_locks_all)) {
79                 spin_unlock(lock);
80                 spin_unlock_wait(&nf_conntrack_locks_all_lock);
81                 spin_lock(lock);
82         }
83 }
84 EXPORT_SYMBOL_GPL(nf_conntrack_lock);
85
86 static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2)
87 {
88         h1 %= CONNTRACK_LOCKS;
89         h2 %= CONNTRACK_LOCKS;
90         spin_unlock(&nf_conntrack_locks[h1]);
91         if (h1 != h2)
92                 spin_unlock(&nf_conntrack_locks[h2]);
93 }
94
95 /* return true if we need to recompute hashes (in case hash table was resized) */
96 static bool nf_conntrack_double_lock(struct net *net, unsigned int h1,
97                                      unsigned int h2, unsigned int sequence)
98 {
99         h1 %= CONNTRACK_LOCKS;
100         h2 %= CONNTRACK_LOCKS;
101         if (h1 <= h2) {
102                 nf_conntrack_lock(&nf_conntrack_locks[h1]);
103                 if (h1 != h2)
104                         spin_lock_nested(&nf_conntrack_locks[h2],
105                                          SINGLE_DEPTH_NESTING);
106         } else {
107                 nf_conntrack_lock(&nf_conntrack_locks[h2]);
108                 spin_lock_nested(&nf_conntrack_locks[h1],
109                                  SINGLE_DEPTH_NESTING);
110         }
111         if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
112                 nf_conntrack_double_unlock(h1, h2);
113                 return true;
114         }
115         return false;
116 }
117
118 static void nf_conntrack_all_lock(void)
119 {
120         int i;
121
122         spin_lock(&nf_conntrack_locks_all_lock);
123         nf_conntrack_locks_all = true;
124
125         for (i = 0; i < CONNTRACK_LOCKS; i++) {
126                 spin_unlock_wait(&nf_conntrack_locks[i]);
127         }
128 }
129
130 static void nf_conntrack_all_unlock(void)
131 {
132         nf_conntrack_locks_all = false;
133         spin_unlock(&nf_conntrack_locks_all_lock);
134 }
135
136 unsigned int nf_conntrack_htable_size __read_mostly;
137 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
138
139 unsigned int nf_conntrack_max __read_mostly;
140 EXPORT_SYMBOL_GPL(nf_conntrack_max);
141
142 DEFINE_PER_CPU(struct nf_conn, nf_conntrack_untracked);
143 EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);
144
145 static unsigned int nf_conntrack_hash_rnd __read_mostly;
146
147 static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple)
148 {
149         unsigned int n;
150
151         get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd));
152
153         /* The direction must be ignored, so we hash everything up to the
154          * destination ports (which is a multiple of 4) and treat the last
155          * three bytes manually.
156          */
157         n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
158         return jhash2((u32 *)tuple, n, nf_conntrack_hash_rnd ^
159                       (((__force __u16)tuple->dst.u.all << 16) |
160                       tuple->dst.protonum));
161 }
162
163 static u32 __hash_bucket(u32 hash, unsigned int size)
164 {
165         return reciprocal_scale(hash, size);
166 }
167
168 static u32 hash_bucket(u32 hash, const struct net *net)
169 {
170         return __hash_bucket(hash, net->ct.htable_size);
171 }
172
173 static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
174                                   unsigned int size)
175 {
176         return __hash_bucket(hash_conntrack_raw(tuple), size);
177 }
178
179 static inline u_int32_t hash_conntrack(const struct net *net,
180                                        const struct nf_conntrack_tuple *tuple)
181 {
182         return __hash_conntrack(tuple, net->ct.htable_size);
183 }
184
185 bool
186 nf_ct_get_tuple(const struct sk_buff *skb,
187                 unsigned int nhoff,
188                 unsigned int dataoff,
189                 u_int16_t l3num,
190                 u_int8_t protonum,
191                 struct net *net,
192                 struct nf_conntrack_tuple *tuple,
193                 const struct nf_conntrack_l3proto *l3proto,
194                 const struct nf_conntrack_l4proto *l4proto)
195 {
196         memset(tuple, 0, sizeof(*tuple));
197
198         tuple->src.l3num = l3num;
199         if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
200                 return false;
201
202         tuple->dst.protonum = protonum;
203         tuple->dst.dir = IP_CT_DIR_ORIGINAL;
204
205         return l4proto->pkt_to_tuple(skb, dataoff, net, tuple);
206 }
207 EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
208
209 bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
210                        u_int16_t l3num,
211                        struct net *net, struct nf_conntrack_tuple *tuple)
212 {
213         struct nf_conntrack_l3proto *l3proto;
214         struct nf_conntrack_l4proto *l4proto;
215         unsigned int protoff;
216         u_int8_t protonum;
217         int ret;
218
219         rcu_read_lock();
220
221         l3proto = __nf_ct_l3proto_find(l3num);
222         ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum);
223         if (ret != NF_ACCEPT) {
224                 rcu_read_unlock();
225                 return false;
226         }
227
228         l4proto = __nf_ct_l4proto_find(l3num, protonum);
229
230         ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple,
231                               l3proto, l4proto);
232
233         rcu_read_unlock();
234         return ret;
235 }
236 EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
237
238 bool
239 nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
240                    const struct nf_conntrack_tuple *orig,
241                    const struct nf_conntrack_l3proto *l3proto,
242                    const struct nf_conntrack_l4proto *l4proto)
243 {
244         memset(inverse, 0, sizeof(*inverse));
245
246         inverse->src.l3num = orig->src.l3num;
247         if (l3proto->invert_tuple(inverse, orig) == 0)
248                 return false;
249
250         inverse->dst.dir = !orig->dst.dir;
251
252         inverse->dst.protonum = orig->dst.protonum;
253         return l4proto->invert_tuple(inverse, orig);
254 }
255 EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
256
257 static void
258 clean_from_lists(struct nf_conn *ct)
259 {
260         pr_debug("clean_from_lists(%p)\n", ct);
261         hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
262         hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode);
263
264         /* Destroy all pending expectations */
265         nf_ct_remove_expectations(ct);
266 }
267
268 /* must be called with local_bh_disable */
269 static void nf_ct_add_to_dying_list(struct nf_conn *ct)
270 {
271         struct ct_pcpu *pcpu;
272
273         /* add this conntrack to the (per cpu) dying list */
274         ct->cpu = smp_processor_id();
275         pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
276
277         spin_lock(&pcpu->lock);
278         hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
279                              &pcpu->dying);
280         spin_unlock(&pcpu->lock);
281 }
282
283 /* must be called with local_bh_disable */
284 static void nf_ct_add_to_unconfirmed_list(struct nf_conn *ct)
285 {
286         struct ct_pcpu *pcpu;
287
288         /* add this conntrack to the (per cpu) unconfirmed list */
289         ct->cpu = smp_processor_id();
290         pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
291
292         spin_lock(&pcpu->lock);
293         hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
294                              &pcpu->unconfirmed);
295         spin_unlock(&pcpu->lock);
296 }
297
298 /* must be called with local_bh_disable */
299 static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct)
300 {
301         struct ct_pcpu *pcpu;
302
303         /* We overload first tuple to link into unconfirmed or dying list.*/
304         pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
305
306         spin_lock(&pcpu->lock);
307         BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode));
308         hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
309         spin_unlock(&pcpu->lock);
310 }
311
312 /* Released via destroy_conntrack() */
313 struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
314                                  const struct nf_conntrack_zone *zone,
315                                  gfp_t flags)
316 {
317         struct nf_conn *tmpl;
318
319         tmpl = kzalloc(sizeof(*tmpl), flags);
320         if (tmpl == NULL)
321                 return NULL;
322
323         tmpl->status = IPS_TEMPLATE;
324         write_pnet(&tmpl->ct_net, net);
325
326         if (nf_ct_zone_add(tmpl, flags, zone) < 0)
327                 goto out_free;
328
329         atomic_set(&tmpl->ct_general.use, 0);
330
331         return tmpl;
332 out_free:
333         kfree(tmpl);
334         return NULL;
335 }
336 EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc);
337
338 void nf_ct_tmpl_free(struct nf_conn *tmpl)
339 {
340         nf_ct_ext_destroy(tmpl);
341         nf_ct_ext_free(tmpl);
342         kfree(tmpl);
343 }
344 EXPORT_SYMBOL_GPL(nf_ct_tmpl_free);
345
346 static void
347 destroy_conntrack(struct nf_conntrack *nfct)
348 {
349         struct nf_conn *ct = (struct nf_conn *)nfct;
350         struct net *net = nf_ct_net(ct);
351         struct nf_conntrack_l4proto *l4proto;
352
353         pr_debug("destroy_conntrack(%p)\n", ct);
354         NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
355         NF_CT_ASSERT(!timer_pending(&ct->timeout));
356
357         if (unlikely(nf_ct_is_template(ct))) {
358                 nf_ct_tmpl_free(ct);
359                 return;
360         }
361         rcu_read_lock();
362         l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
363         if (l4proto && l4proto->destroy)
364                 l4proto->destroy(ct);
365
366         rcu_read_unlock();
367
368         local_bh_disable();
369         /* Expectations will have been removed in clean_from_lists,
370          * except TFTP can create an expectation on the first packet,
371          * before connection is in the list, so we need to clean here,
372          * too.
373          */
374         nf_ct_remove_expectations(ct);
375
376         nf_ct_del_from_dying_or_unconfirmed_list(ct);
377
378         NF_CT_STAT_INC(net, delete);
379         local_bh_enable();
380
381         if (ct->master)
382                 nf_ct_put(ct->master);
383
384         pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct);
385         nf_conntrack_free(ct);
386 }
387
388 static void nf_ct_delete_from_lists(struct nf_conn *ct)
389 {
390         struct net *net = nf_ct_net(ct);
391         unsigned int hash, reply_hash;
392         unsigned int sequence;
393
394         nf_ct_helper_destroy(ct);
395
396         local_bh_disable();
397         do {
398                 sequence = read_seqcount_begin(&nf_conntrack_generation);
399                 hash = hash_conntrack(net,
400                                       &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
401                 reply_hash = hash_conntrack(net,
402                                            &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
403         } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
404
405         clean_from_lists(ct);
406         nf_conntrack_double_unlock(hash, reply_hash);
407
408         nf_ct_add_to_dying_list(ct);
409
410         NF_CT_STAT_INC(net, delete_list);
411         local_bh_enable();
412 }
413
414 bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
415 {
416         struct nf_conn_tstamp *tstamp;
417
418         tstamp = nf_conn_tstamp_find(ct);
419         if (tstamp && tstamp->stop == 0)
420                 tstamp->stop = ktime_get_real_ns();
421
422         if (nf_ct_is_dying(ct))
423                 goto delete;
424
425         if (nf_conntrack_event_report(IPCT_DESTROY, ct,
426                                     portid, report) < 0) {
427                 /* destroy event was not delivered */
428                 nf_ct_delete_from_lists(ct);
429                 nf_conntrack_ecache_delayed_work(nf_ct_net(ct));
430                 return false;
431         }
432
433         nf_conntrack_ecache_work(nf_ct_net(ct));
434         set_bit(IPS_DYING_BIT, &ct->status);
435  delete:
436         nf_ct_delete_from_lists(ct);
437         nf_ct_put(ct);
438         return true;
439 }
440 EXPORT_SYMBOL_GPL(nf_ct_delete);
441
442 static void death_by_timeout(unsigned long ul_conntrack)
443 {
444         nf_ct_delete((struct nf_conn *)ul_conntrack, 0, 0);
445 }
446
447 static inline bool
448 nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
449                 const struct nf_conntrack_tuple *tuple,
450                 const struct nf_conntrack_zone *zone,
451                 const struct net *net)
452 {
453         struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
454
455         /* A conntrack can be recreated with the equal tuple,
456          * so we need to check that the conntrack is confirmed
457          */
458         return nf_ct_tuple_equal(tuple, &h->tuple) &&
459                nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
460                nf_ct_is_confirmed(ct) &&
461                net_eq(net, nf_ct_net(ct));
462 }
463
464 /*
465  * Warning :
466  * - Caller must take a reference on returned object
467  *   and recheck nf_ct_tuple_equal(tuple, &h->tuple)
468  */
469 static struct nf_conntrack_tuple_hash *
470 ____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,
471                       const struct nf_conntrack_tuple *tuple, u32 hash)
472 {
473         struct nf_conntrack_tuple_hash *h;
474         struct hlist_nulls_head *ct_hash;
475         struct hlist_nulls_node *n;
476         unsigned int bucket, sequence;
477
478 begin:
479         do {
480                 sequence = read_seqcount_begin(&nf_conntrack_generation);
481                 bucket = hash_bucket(hash, net);
482                 ct_hash = net->ct.hash;
483         } while (read_seqcount_retry(&nf_conntrack_generation, sequence));
484
485         hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
486                 if (nf_ct_key_equal(h, tuple, zone, net)) {
487                         NF_CT_STAT_INC_ATOMIC(net, found);
488                         return h;
489                 }
490                 NF_CT_STAT_INC_ATOMIC(net, searched);
491         }
492         /*
493          * if the nulls value we got at the end of this lookup is
494          * not the expected one, we must restart lookup.
495          * We probably met an item that was moved to another chain.
496          */
497         if (get_nulls_value(n) != bucket) {
498                 NF_CT_STAT_INC_ATOMIC(net, search_restart);
499                 goto begin;
500         }
501
502         return NULL;
503 }
504
505 /* Find a connection corresponding to a tuple. */
506 static struct nf_conntrack_tuple_hash *
507 __nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
508                         const struct nf_conntrack_tuple *tuple, u32 hash)
509 {
510         struct nf_conntrack_tuple_hash *h;
511         struct nf_conn *ct;
512
513         rcu_read_lock();
514 begin:
515         h = ____nf_conntrack_find(net, zone, tuple, hash);
516         if (h) {
517                 ct = nf_ct_tuplehash_to_ctrack(h);
518                 if (unlikely(nf_ct_is_dying(ct) ||
519                              !atomic_inc_not_zero(&ct->ct_general.use)))
520                         h = NULL;
521                 else {
522                         if (unlikely(!nf_ct_key_equal(h, tuple, zone, net))) {
523                                 nf_ct_put(ct);
524                                 goto begin;
525                         }
526                 }
527         }
528         rcu_read_unlock();
529
530         return h;
531 }
532
533 struct nf_conntrack_tuple_hash *
534 nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
535                       const struct nf_conntrack_tuple *tuple)
536 {
537         return __nf_conntrack_find_get(net, zone, tuple,
538                                        hash_conntrack_raw(tuple));
539 }
540 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
541
542 static void __nf_conntrack_hash_insert(struct nf_conn *ct,
543                                        unsigned int hash,
544                                        unsigned int reply_hash)
545 {
546         struct net *net = nf_ct_net(ct);
547
548         hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
549                            &net->ct.hash[hash]);
550         hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
551                            &net->ct.hash[reply_hash]);
552 }
553
554 int
555 nf_conntrack_hash_check_insert(struct nf_conn *ct)
556 {
557         const struct nf_conntrack_zone *zone;
558         struct net *net = nf_ct_net(ct);
559         unsigned int hash, reply_hash;
560         struct nf_conntrack_tuple_hash *h;
561         struct hlist_nulls_node *n;
562         unsigned int sequence;
563
564         zone = nf_ct_zone(ct);
565
566         local_bh_disable();
567         do {
568                 sequence = read_seqcount_begin(&nf_conntrack_generation);
569                 hash = hash_conntrack(net,
570                                       &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
571                 reply_hash = hash_conntrack(net,
572                                            &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
573         } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
574
575         /* See if there's one in the list already, including reverse */
576         hlist_nulls_for_each_entry(h, n, &net->ct.hash[hash], hnnode)
577                 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
578                                     zone, net))
579                         goto out;
580
581         hlist_nulls_for_each_entry(h, n, &net->ct.hash[reply_hash], hnnode)
582                 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
583                                     zone, net))
584                         goto out;
585
586         add_timer(&ct->timeout);
587         smp_wmb();
588         /* The caller holds a reference to this object */
589         atomic_set(&ct->ct_general.use, 2);
590         __nf_conntrack_hash_insert(ct, hash, reply_hash);
591         nf_conntrack_double_unlock(hash, reply_hash);
592         NF_CT_STAT_INC(net, insert);
593         local_bh_enable();
594         return 0;
595
596 out:
597         nf_conntrack_double_unlock(hash, reply_hash);
598         NF_CT_STAT_INC(net, insert_failed);
599         local_bh_enable();
600         return -EEXIST;
601 }
602 EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert);
603
604 /* Confirm a connection given skb; places it in hash table */
605 int
606 __nf_conntrack_confirm(struct sk_buff *skb)
607 {
608         const struct nf_conntrack_zone *zone;
609         unsigned int hash, reply_hash;
610         struct nf_conntrack_tuple_hash *h;
611         struct nf_conn *ct;
612         struct nf_conn_help *help;
613         struct nf_conn_tstamp *tstamp;
614         struct hlist_nulls_node *n;
615         enum ip_conntrack_info ctinfo;
616         struct net *net;
617         unsigned int sequence;
618
619         ct = nf_ct_get(skb, &ctinfo);
620         net = nf_ct_net(ct);
621
622         /* ipt_REJECT uses nf_conntrack_attach to attach related
623            ICMP/TCP RST packets in other direction.  Actual packet
624            which created connection will be IP_CT_NEW or for an
625            expected connection, IP_CT_RELATED. */
626         if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
627                 return NF_ACCEPT;
628
629         zone = nf_ct_zone(ct);
630         local_bh_disable();
631
632         do {
633                 sequence = read_seqcount_begin(&nf_conntrack_generation);
634                 /* reuse the hash saved before */
635                 hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev;
636                 hash = hash_bucket(hash, net);
637                 reply_hash = hash_conntrack(net,
638                                            &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
639
640         } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
641
642         /* We're not in hash table, and we refuse to set up related
643          * connections for unconfirmed conns.  But packet copies and
644          * REJECT will give spurious warnings here.
645          */
646         /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
647
648         /* No external references means no one else could have
649          * confirmed us.
650          */
651         NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
652         pr_debug("Confirming conntrack %p\n", ct);
653         /* We have to check the DYING flag after unlink to prevent
654          * a race against nf_ct_get_next_corpse() possibly called from
655          * user context, else we insert an already 'dead' hash, blocking
656          * further use of that particular connection -JM.
657          */
658         nf_ct_del_from_dying_or_unconfirmed_list(ct);
659
660         if (unlikely(nf_ct_is_dying(ct)))
661                 goto out;
662
663         /* See if there's one in the list already, including reverse:
664            NAT could have grabbed it without realizing, since we're
665            not in the hash.  If there is, we lost race. */
666         hlist_nulls_for_each_entry(h, n, &net->ct.hash[hash], hnnode)
667                 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
668                                     zone, net))
669                         goto out;
670
671         hlist_nulls_for_each_entry(h, n, &net->ct.hash[reply_hash], hnnode)
672                 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
673                                     zone, net))
674                         goto out;
675
676         /* Timer relative to confirmation time, not original
677            setting time, otherwise we'd get timer wrap in
678            weird delay cases. */
679         ct->timeout.expires += jiffies;
680         add_timer(&ct->timeout);
681         atomic_inc(&ct->ct_general.use);
682         ct->status |= IPS_CONFIRMED;
683
684         /* set conntrack timestamp, if enabled. */
685         tstamp = nf_conn_tstamp_find(ct);
686         if (tstamp) {
687                 if (skb->tstamp.tv64 == 0)
688                         __net_timestamp(skb);
689
690                 tstamp->start = ktime_to_ns(skb->tstamp);
691         }
692         /* Since the lookup is lockless, hash insertion must be done after
693          * starting the timer and setting the CONFIRMED bit. The RCU barriers
694          * guarantee that no other CPU can find the conntrack before the above
695          * stores are visible.
696          */
697         __nf_conntrack_hash_insert(ct, hash, reply_hash);
698         nf_conntrack_double_unlock(hash, reply_hash);
699         NF_CT_STAT_INC(net, insert);
700         local_bh_enable();
701
702         help = nfct_help(ct);
703         if (help && help->helper)
704                 nf_conntrack_event_cache(IPCT_HELPER, ct);
705
706         nf_conntrack_event_cache(master_ct(ct) ?
707                                  IPCT_RELATED : IPCT_NEW, ct);
708         return NF_ACCEPT;
709
710 out:
711         nf_ct_add_to_dying_list(ct);
712         nf_conntrack_double_unlock(hash, reply_hash);
713         NF_CT_STAT_INC(net, insert_failed);
714         local_bh_enable();
715         return NF_DROP;
716 }
717 EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
718
719 /* Returns true if a connection correspondings to the tuple (required
720    for NAT). */
721 int
722 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
723                          const struct nf_conn *ignored_conntrack)
724 {
725         struct net *net = nf_ct_net(ignored_conntrack);
726         const struct nf_conntrack_zone *zone;
727         struct nf_conntrack_tuple_hash *h;
728         struct hlist_nulls_head *ct_hash;
729         unsigned int hash, sequence;
730         struct hlist_nulls_node *n;
731         struct nf_conn *ct;
732
733         zone = nf_ct_zone(ignored_conntrack);
734
735         rcu_read_lock();
736         do {
737                 sequence = read_seqcount_begin(&nf_conntrack_generation);
738                 hash = hash_conntrack(net, tuple);
739                 ct_hash = net->ct.hash;
740         } while (read_seqcount_retry(&nf_conntrack_generation, sequence));
741
742         hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) {
743                 ct = nf_ct_tuplehash_to_ctrack(h);
744                 if (ct != ignored_conntrack &&
745                     nf_ct_key_equal(h, tuple, zone, net)) {
746                         NF_CT_STAT_INC_ATOMIC(net, found);
747                         rcu_read_unlock();
748                         return 1;
749                 }
750                 NF_CT_STAT_INC_ATOMIC(net, searched);
751         }
752         rcu_read_unlock();
753
754         return 0;
755 }
756 EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
757
758 #define NF_CT_EVICTION_RANGE    8
759
760 /* There's a small race here where we may free a just-assured
761    connection.  Too bad: we're in trouble anyway. */
762 static noinline int early_drop(struct net *net, unsigned int _hash)
763 {
764         /* Use oldest entry, which is roughly LRU */
765         struct nf_conntrack_tuple_hash *h;
766         struct nf_conn *ct = NULL, *tmp;
767         struct hlist_nulls_node *n;
768         unsigned int i = 0, cnt = 0;
769         int dropped = 0;
770         unsigned int hash, sequence;
771         spinlock_t *lockp;
772
773         local_bh_disable();
774 restart:
775         sequence = read_seqcount_begin(&nf_conntrack_generation);
776         hash = hash_bucket(_hash, net);
777         for (; i < net->ct.htable_size; i++) {
778                 lockp = &nf_conntrack_locks[hash % CONNTRACK_LOCKS];
779                 nf_conntrack_lock(lockp);
780                 if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
781                         spin_unlock(lockp);
782                         goto restart;
783                 }
784                 hlist_nulls_for_each_entry_rcu(h, n, &net->ct.hash[hash],
785                                          hnnode) {
786                         tmp = nf_ct_tuplehash_to_ctrack(h);
787                         if (!test_bit(IPS_ASSURED_BIT, &tmp->status) &&
788                             !nf_ct_is_dying(tmp) &&
789                             atomic_inc_not_zero(&tmp->ct_general.use)) {
790                                 ct = tmp;
791                                 break;
792                         }
793                         cnt++;
794                 }
795
796                 hash = (hash + 1) % net->ct.htable_size;
797                 spin_unlock(lockp);
798
799                 if (ct || cnt >= NF_CT_EVICTION_RANGE)
800                         break;
801
802         }
803         local_bh_enable();
804
805         if (!ct)
806                 return dropped;
807
808         if (del_timer(&ct->timeout)) {
809                 if (nf_ct_delete(ct, 0, 0)) {
810                         dropped = 1;
811                         NF_CT_STAT_INC_ATOMIC(net, early_drop);
812                 }
813         }
814         nf_ct_put(ct);
815         return dropped;
816 }
817
818 static struct nf_conn *
819 __nf_conntrack_alloc(struct net *net,
820                      const struct nf_conntrack_zone *zone,
821                      const struct nf_conntrack_tuple *orig,
822                      const struct nf_conntrack_tuple *repl,
823                      gfp_t gfp, u32 hash)
824 {
825         struct nf_conn *ct;
826
827         /* We don't want any race condition at early drop stage */
828         atomic_inc(&net->ct.count);
829
830         if (nf_conntrack_max &&
831             unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
832                 if (!early_drop(net, hash)) {
833                         atomic_dec(&net->ct.count);
834                         net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
835                         return ERR_PTR(-ENOMEM);
836                 }
837         }
838
839         /*
840          * Do not use kmem_cache_zalloc(), as this cache uses
841          * SLAB_DESTROY_BY_RCU.
842          */
843         ct = kmem_cache_alloc(net->ct.nf_conntrack_cachep, gfp);
844         if (ct == NULL)
845                 goto out;
846
847         spin_lock_init(&ct->lock);
848         ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
849         ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
850         ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
851         /* save hash for reusing when confirming */
852         *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
853         ct->status = 0;
854         /* Don't set timer yet: wait for confirmation */
855         setup_timer(&ct->timeout, death_by_timeout, (unsigned long)ct);
856         write_pnet(&ct->ct_net, net);
857         memset(&ct->__nfct_init_offset[0], 0,
858                offsetof(struct nf_conn, proto) -
859                offsetof(struct nf_conn, __nfct_init_offset[0]));
860
861         if (zone && nf_ct_zone_add(ct, GFP_ATOMIC, zone) < 0)
862                 goto out_free;
863
864         /* Because we use RCU lookups, we set ct_general.use to zero before
865          * this is inserted in any list.
866          */
867         atomic_set(&ct->ct_general.use, 0);
868         return ct;
869 out_free:
870         kmem_cache_free(net->ct.nf_conntrack_cachep, ct);
871 out:
872         atomic_dec(&net->ct.count);
873         return ERR_PTR(-ENOMEM);
874 }
875
876 struct nf_conn *nf_conntrack_alloc(struct net *net,
877                                    const struct nf_conntrack_zone *zone,
878                                    const struct nf_conntrack_tuple *orig,
879                                    const struct nf_conntrack_tuple *repl,
880                                    gfp_t gfp)
881 {
882         return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0);
883 }
884 EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
885
886 void nf_conntrack_free(struct nf_conn *ct)
887 {
888         struct net *net = nf_ct_net(ct);
889
890         /* A freed object has refcnt == 0, that's
891          * the golden rule for SLAB_DESTROY_BY_RCU
892          */
893         NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 0);
894
895         nf_ct_ext_destroy(ct);
896         nf_ct_ext_free(ct);
897         kmem_cache_free(net->ct.nf_conntrack_cachep, ct);
898         smp_mb__before_atomic();
899         atomic_dec(&net->ct.count);
900 }
901 EXPORT_SYMBOL_GPL(nf_conntrack_free);
902
903
904 /* Allocate a new conntrack: we return -ENOMEM if classification
905    failed due to stress.  Otherwise it really is unclassifiable. */
906 static struct nf_conntrack_tuple_hash *
907 init_conntrack(struct net *net, struct nf_conn *tmpl,
908                const struct nf_conntrack_tuple *tuple,
909                struct nf_conntrack_l3proto *l3proto,
910                struct nf_conntrack_l4proto *l4proto,
911                struct sk_buff *skb,
912                unsigned int dataoff, u32 hash)
913 {
914         struct nf_conn *ct;
915         struct nf_conn_help *help;
916         struct nf_conntrack_tuple repl_tuple;
917         struct nf_conntrack_ecache *ecache;
918         struct nf_conntrack_expect *exp = NULL;
919         const struct nf_conntrack_zone *zone;
920         struct nf_conn_timeout *timeout_ext;
921         struct nf_conntrack_zone tmp;
922         unsigned int *timeouts;
923
924         if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
925                 pr_debug("Can't invert tuple.\n");
926                 return NULL;
927         }
928
929         zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
930         ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC,
931                                   hash);
932         if (IS_ERR(ct))
933                 return (struct nf_conntrack_tuple_hash *)ct;
934
935         if (tmpl && nfct_synproxy(tmpl)) {
936                 nfct_seqadj_ext_add(ct);
937                 nfct_synproxy_ext_add(ct);
938         }
939
940         timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
941         if (timeout_ext) {
942                 timeouts = nf_ct_timeout_data(timeout_ext);
943                 if (unlikely(!timeouts))
944                         timeouts = l4proto->get_timeouts(net);
945         } else {
946                 timeouts = l4proto->get_timeouts(net);
947         }
948
949         if (!l4proto->new(ct, skb, dataoff, timeouts)) {
950                 nf_conntrack_free(ct);
951                 pr_debug("can't track with proto module\n");
952                 return NULL;
953         }
954
955         if (timeout_ext)
956                 nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout),
957                                       GFP_ATOMIC);
958
959         nf_ct_acct_ext_add(ct, GFP_ATOMIC);
960         nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
961         nf_ct_labels_ext_add(ct);
962
963         ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL;
964         nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
965                                  ecache ? ecache->expmask : 0,
966                              GFP_ATOMIC);
967
968         local_bh_disable();
969         if (net->ct.expect_count) {
970                 spin_lock(&nf_conntrack_expect_lock);
971                 exp = nf_ct_find_expectation(net, zone, tuple);
972                 if (exp) {
973                         pr_debug("expectation arrives ct=%p exp=%p\n",
974                                  ct, exp);
975                         /* Welcome, Mr. Bond.  We've been expecting you... */
976                         __set_bit(IPS_EXPECTED_BIT, &ct->status);
977                         /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
978                         ct->master = exp->master;
979                         if (exp->helper) {
980                                 help = nf_ct_helper_ext_add(ct, exp->helper,
981                                                             GFP_ATOMIC);
982                                 if (help)
983                                         rcu_assign_pointer(help->helper, exp->helper);
984                         }
985
986 #ifdef CONFIG_NF_CONNTRACK_MARK
987                         ct->mark = exp->master->mark;
988 #endif
989 #ifdef CONFIG_NF_CONNTRACK_SECMARK
990                         ct->secmark = exp->master->secmark;
991 #endif
992                         NF_CT_STAT_INC(net, expect_new);
993                 }
994                 spin_unlock(&nf_conntrack_expect_lock);
995         }
996         if (!exp) {
997                 __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC);
998                 NF_CT_STAT_INC(net, new);
999         }
1000
1001         /* Now it is inserted into the unconfirmed list, bump refcount */
1002         nf_conntrack_get(&ct->ct_general);
1003         nf_ct_add_to_unconfirmed_list(ct);
1004
1005         local_bh_enable();
1006
1007         if (exp) {
1008                 if (exp->expectfn)
1009                         exp->expectfn(ct, exp);
1010                 nf_ct_expect_put(exp);
1011         }
1012
1013         return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
1014 }
1015
1016 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
1017 static inline struct nf_conn *
1018 resolve_normal_ct(struct net *net, struct nf_conn *tmpl,
1019                   struct sk_buff *skb,
1020                   unsigned int dataoff,
1021                   u_int16_t l3num,
1022                   u_int8_t protonum,
1023                   struct nf_conntrack_l3proto *l3proto,
1024                   struct nf_conntrack_l4proto *l4proto,
1025                   int *set_reply,
1026                   enum ip_conntrack_info *ctinfo)
1027 {
1028         const struct nf_conntrack_zone *zone;
1029         struct nf_conntrack_tuple tuple;
1030         struct nf_conntrack_tuple_hash *h;
1031         struct nf_conntrack_zone tmp;
1032         struct nf_conn *ct;
1033         u32 hash;
1034
1035         if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
1036                              dataoff, l3num, protonum, net, &tuple, l3proto,
1037                              l4proto)) {
1038                 pr_debug("Can't get tuple\n");
1039                 return NULL;
1040         }
1041
1042         /* look for tuple match */
1043         zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
1044         hash = hash_conntrack_raw(&tuple);
1045         h = __nf_conntrack_find_get(net, zone, &tuple, hash);
1046         if (!h) {
1047                 h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
1048                                    skb, dataoff, hash);
1049                 if (!h)
1050                         return NULL;
1051                 if (IS_ERR(h))
1052                         return (void *)h;
1053         }
1054         ct = nf_ct_tuplehash_to_ctrack(h);
1055
1056         /* It exists; we have (non-exclusive) reference. */
1057         if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
1058                 *ctinfo = IP_CT_ESTABLISHED_REPLY;
1059                 /* Please set reply bit if this packet OK */
1060                 *set_reply = 1;
1061         } else {
1062                 /* Once we've had two way comms, always ESTABLISHED. */
1063                 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
1064                         pr_debug("normal packet for %p\n", ct);
1065                         *ctinfo = IP_CT_ESTABLISHED;
1066                 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
1067                         pr_debug("related packet for %p\n", ct);
1068                         *ctinfo = IP_CT_RELATED;
1069                 } else {
1070                         pr_debug("new packet for %p\n", ct);
1071                         *ctinfo = IP_CT_NEW;
1072                 }
1073                 *set_reply = 0;
1074         }
1075         skb->nfct = &ct->ct_general;
1076         skb->nfctinfo = *ctinfo;
1077         return ct;
1078 }
1079
1080 unsigned int
1081 nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
1082                 struct sk_buff *skb)
1083 {
1084         struct nf_conn *ct, *tmpl = NULL;
1085         enum ip_conntrack_info ctinfo;
1086         struct nf_conntrack_l3proto *l3proto;
1087         struct nf_conntrack_l4proto *l4proto;
1088         unsigned int *timeouts;
1089         unsigned int dataoff;
1090         u_int8_t protonum;
1091         int set_reply = 0;
1092         int ret;
1093
1094         if (skb->nfct) {
1095                 /* Previously seen (loopback or untracked)?  Ignore. */
1096                 tmpl = (struct nf_conn *)skb->nfct;
1097                 if (!nf_ct_is_template(tmpl)) {
1098                         NF_CT_STAT_INC_ATOMIC(net, ignore);
1099                         return NF_ACCEPT;
1100                 }
1101                 skb->nfct = NULL;
1102         }
1103
1104         /* rcu_read_lock()ed by nf_hook_slow */
1105         l3proto = __nf_ct_l3proto_find(pf);
1106         ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
1107                                    &dataoff, &protonum);
1108         if (ret <= 0) {
1109                 pr_debug("not prepared to track yet or error occurred\n");
1110                 NF_CT_STAT_INC_ATOMIC(net, error);
1111                 NF_CT_STAT_INC_ATOMIC(net, invalid);
1112                 ret = -ret;
1113                 goto out;
1114         }
1115
1116         l4proto = __nf_ct_l4proto_find(pf, protonum);
1117
1118         /* It may be an special packet, error, unclean...
1119          * inverse of the return code tells to the netfilter
1120          * core what to do with the packet. */
1121         if (l4proto->error != NULL) {
1122                 ret = l4proto->error(net, tmpl, skb, dataoff, &ctinfo,
1123                                      pf, hooknum);
1124                 if (ret <= 0) {
1125                         NF_CT_STAT_INC_ATOMIC(net, error);
1126                         NF_CT_STAT_INC_ATOMIC(net, invalid);
1127                         ret = -ret;
1128                         goto out;
1129                 }
1130                 /* ICMP[v6] protocol trackers may assign one conntrack. */
1131                 if (skb->nfct)
1132                         goto out;
1133         }
1134
1135         ct = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum,
1136                                l3proto, l4proto, &set_reply, &ctinfo);
1137         if (!ct) {
1138                 /* Not valid part of a connection */
1139                 NF_CT_STAT_INC_ATOMIC(net, invalid);
1140                 ret = NF_ACCEPT;
1141                 goto out;
1142         }
1143
1144         if (IS_ERR(ct)) {
1145                 /* Too stressed to deal. */
1146                 NF_CT_STAT_INC_ATOMIC(net, drop);
1147                 ret = NF_DROP;
1148                 goto out;
1149         }
1150
1151         NF_CT_ASSERT(skb->nfct);
1152
1153         /* Decide what timeout policy we want to apply to this flow. */
1154         timeouts = nf_ct_timeout_lookup(net, ct, l4proto);
1155
1156         ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum, timeouts);
1157         if (ret <= 0) {
1158                 /* Invalid: inverse of the return code tells
1159                  * the netfilter core what to do */
1160                 pr_debug("nf_conntrack_in: Can't track with proto module\n");
1161                 nf_conntrack_put(skb->nfct);
1162                 skb->nfct = NULL;
1163                 NF_CT_STAT_INC_ATOMIC(net, invalid);
1164                 if (ret == -NF_DROP)
1165                         NF_CT_STAT_INC_ATOMIC(net, drop);
1166                 ret = -ret;
1167                 goto out;
1168         }
1169
1170         if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
1171                 nf_conntrack_event_cache(IPCT_REPLY, ct);
1172 out:
1173         if (tmpl) {
1174                 /* Special case: we have to repeat this hook, assign the
1175                  * template again to this packet. We assume that this packet
1176                  * has no conntrack assigned. This is used by nf_ct_tcp. */
1177                 if (ret == NF_REPEAT)
1178                         skb->nfct = (struct nf_conntrack *)tmpl;
1179                 else
1180                         nf_ct_put(tmpl);
1181         }
1182
1183         return ret;
1184 }
1185 EXPORT_SYMBOL_GPL(nf_conntrack_in);
1186
1187 bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
1188                           const struct nf_conntrack_tuple *orig)
1189 {
1190         bool ret;
1191
1192         rcu_read_lock();
1193         ret = nf_ct_invert_tuple(inverse, orig,
1194                                  __nf_ct_l3proto_find(orig->src.l3num),
1195                                  __nf_ct_l4proto_find(orig->src.l3num,
1196                                                       orig->dst.protonum));
1197         rcu_read_unlock();
1198         return ret;
1199 }
1200 EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
1201
1202 /* Alter reply tuple (maybe alter helper).  This is for NAT, and is
1203    implicitly racy: see __nf_conntrack_confirm */
1204 void nf_conntrack_alter_reply(struct nf_conn *ct,
1205                               const struct nf_conntrack_tuple *newreply)
1206 {
1207         struct nf_conn_help *help = nfct_help(ct);
1208
1209         /* Should be unconfirmed, so not in hash table yet */
1210         NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
1211
1212         pr_debug("Altering reply tuple of %p to ", ct);
1213         nf_ct_dump_tuple(newreply);
1214
1215         ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
1216         if (ct->master || (help && !hlist_empty(&help->expectations)))
1217                 return;
1218
1219         rcu_read_lock();
1220         __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1221         rcu_read_unlock();
1222 }
1223 EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
1224
1225 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1226 void __nf_ct_refresh_acct(struct nf_conn *ct,
1227                           enum ip_conntrack_info ctinfo,
1228                           const struct sk_buff *skb,
1229                           unsigned long extra_jiffies,
1230                           int do_acct)
1231 {
1232         NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
1233         NF_CT_ASSERT(skb);
1234
1235         /* Only update if this is not a fixed timeout */
1236         if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
1237                 goto acct;
1238
1239         /* If not in hash table, timer will not be active yet */
1240         if (!nf_ct_is_confirmed(ct)) {
1241                 ct->timeout.expires = extra_jiffies;
1242         } else {
1243                 unsigned long newtime = jiffies + extra_jiffies;
1244
1245                 /* Only update the timeout if the new timeout is at least
1246                    HZ jiffies from the old timeout. Need del_timer for race
1247                    avoidance (may already be dying). */
1248                 if (newtime - ct->timeout.expires >= HZ)
1249                         mod_timer_pending(&ct->timeout, newtime);
1250         }
1251
1252 acct:
1253         if (do_acct) {
1254                 struct nf_conn_acct *acct;
1255
1256                 acct = nf_conn_acct_find(ct);
1257                 if (acct) {
1258                         struct nf_conn_counter *counter = acct->counter;
1259
1260                         atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
1261                         atomic64_add(skb->len, &counter[CTINFO2DIR(ctinfo)].bytes);
1262                 }
1263         }
1264 }
1265 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
1266
1267 bool __nf_ct_kill_acct(struct nf_conn *ct,
1268                        enum ip_conntrack_info ctinfo,
1269                        const struct sk_buff *skb,
1270                        int do_acct)
1271 {
1272         if (do_acct) {
1273                 struct nf_conn_acct *acct;
1274
1275                 acct = nf_conn_acct_find(ct);
1276                 if (acct) {
1277                         struct nf_conn_counter *counter = acct->counter;
1278
1279                         atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
1280                         atomic64_add(skb->len - skb_network_offset(skb),
1281                                      &counter[CTINFO2DIR(ctinfo)].bytes);
1282                 }
1283         }
1284
1285         if (del_timer(&ct->timeout)) {
1286                 ct->timeout.function((unsigned long)ct);
1287                 return true;
1288         }
1289         return false;
1290 }
1291 EXPORT_SYMBOL_GPL(__nf_ct_kill_acct);
1292
1293 #ifdef CONFIG_NF_CONNTRACK_ZONES
1294 static struct nf_ct_ext_type nf_ct_zone_extend __read_mostly = {
1295         .len    = sizeof(struct nf_conntrack_zone),
1296         .align  = __alignof__(struct nf_conntrack_zone),
1297         .id     = NF_CT_EXT_ZONE,
1298 };
1299 #endif
1300
1301 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1302
1303 #include <linux/netfilter/nfnetlink.h>
1304 #include <linux/netfilter/nfnetlink_conntrack.h>
1305 #include <linux/mutex.h>
1306
1307 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1308  * in ip_conntrack_core, since we don't want the protocols to autoload
1309  * or depend on ctnetlink */
1310 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
1311                                const struct nf_conntrack_tuple *tuple)
1312 {
1313         if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) ||
1314             nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port))
1315                 goto nla_put_failure;
1316         return 0;
1317
1318 nla_put_failure:
1319         return -1;
1320 }
1321 EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
1322
1323 const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
1324         [CTA_PROTO_SRC_PORT]  = { .type = NLA_U16 },
1325         [CTA_PROTO_DST_PORT]  = { .type = NLA_U16 },
1326 };
1327 EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
1328
1329 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
1330                                struct nf_conntrack_tuple *t)
1331 {
1332         if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
1333                 return -EINVAL;
1334
1335         t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1336         t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
1337
1338         return 0;
1339 }
1340 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
1341
1342 int nf_ct_port_nlattr_tuple_size(void)
1343 {
1344         return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1345 }
1346 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
1347 #endif
1348
1349 /* Used by ipt_REJECT and ip6t_REJECT. */
1350 static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
1351 {
1352         struct nf_conn *ct;
1353         enum ip_conntrack_info ctinfo;
1354
1355         /* This ICMP is in reverse direction to the packet which caused it */
1356         ct = nf_ct_get(skb, &ctinfo);
1357         if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
1358                 ctinfo = IP_CT_RELATED_REPLY;
1359         else
1360                 ctinfo = IP_CT_RELATED;
1361
1362         /* Attach to new skbuff, and increment count */
1363         nskb->nfct = &ct->ct_general;
1364         nskb->nfctinfo = ctinfo;
1365         nf_conntrack_get(nskb->nfct);
1366 }
1367
1368 /* Bring out ya dead! */
1369 static struct nf_conn *
1370 get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
1371                 void *data, unsigned int *bucket)
1372 {
1373         struct nf_conntrack_tuple_hash *h;
1374         struct nf_conn *ct;
1375         struct hlist_nulls_node *n;
1376         int cpu;
1377         spinlock_t *lockp;
1378
1379         for (; *bucket < net->ct.htable_size; (*bucket)++) {
1380                 lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS];
1381                 local_bh_disable();
1382                 nf_conntrack_lock(lockp);
1383                 if (*bucket < net->ct.htable_size) {
1384                         hlist_nulls_for_each_entry(h, n, &net->ct.hash[*bucket], hnnode) {
1385                                 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1386                                         continue;
1387                                 ct = nf_ct_tuplehash_to_ctrack(h);
1388                                 if (net_eq(nf_ct_net(ct), net) &&
1389                                     iter(ct, data))
1390                                         goto found;
1391                         }
1392                 }
1393                 spin_unlock(lockp);
1394                 local_bh_enable();
1395                 cond_resched();
1396         }
1397
1398         for_each_possible_cpu(cpu) {
1399                 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1400
1401                 spin_lock_bh(&pcpu->lock);
1402                 hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) {
1403                         ct = nf_ct_tuplehash_to_ctrack(h);
1404                         if (iter(ct, data))
1405                                 set_bit(IPS_DYING_BIT, &ct->status);
1406                 }
1407                 spin_unlock_bh(&pcpu->lock);
1408                 cond_resched();
1409         }
1410         return NULL;
1411 found:
1412         atomic_inc(&ct->ct_general.use);
1413         spin_unlock(lockp);
1414         local_bh_enable();
1415         return ct;
1416 }
1417
1418 void nf_ct_iterate_cleanup(struct net *net,
1419                            int (*iter)(struct nf_conn *i, void *data),
1420                            void *data, u32 portid, int report)
1421 {
1422         struct nf_conn *ct;
1423         unsigned int bucket = 0;
1424
1425         might_sleep();
1426
1427         if (atomic_read(&net->ct.count) == 0)
1428                 return;
1429
1430         while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
1431                 /* Time to push up daises... */
1432                 if (del_timer(&ct->timeout))
1433                         nf_ct_delete(ct, portid, report);
1434
1435                 /* ... else the timer will get him soon. */
1436
1437                 nf_ct_put(ct);
1438                 cond_resched();
1439         }
1440 }
1441 EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
1442
1443 static int kill_all(struct nf_conn *i, void *data)
1444 {
1445         return 1;
1446 }
1447
1448 void nf_ct_free_hashtable(void *hash, unsigned int size)
1449 {
1450         if (is_vmalloc_addr(hash))
1451                 vfree(hash);
1452         else
1453                 free_pages((unsigned long)hash,
1454                            get_order(sizeof(struct hlist_head) * size));
1455 }
1456 EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
1457
1458 static int untrack_refs(void)
1459 {
1460         int cnt = 0, cpu;
1461
1462         for_each_possible_cpu(cpu) {
1463                 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
1464
1465                 cnt += atomic_read(&ct->ct_general.use) - 1;
1466         }
1467         return cnt;
1468 }
1469
1470 void nf_conntrack_cleanup_start(void)
1471 {
1472         RCU_INIT_POINTER(ip_ct_attach, NULL);
1473 }
1474
1475 void nf_conntrack_cleanup_end(void)
1476 {
1477         RCU_INIT_POINTER(nf_ct_destroy, NULL);
1478         while (untrack_refs() > 0)
1479                 schedule();
1480
1481 #ifdef CONFIG_NF_CONNTRACK_ZONES
1482         nf_ct_extend_unregister(&nf_ct_zone_extend);
1483 #endif
1484         nf_conntrack_proto_fini();
1485         nf_conntrack_seqadj_fini();
1486         nf_conntrack_labels_fini();
1487         nf_conntrack_helper_fini();
1488         nf_conntrack_timeout_fini();
1489         nf_conntrack_ecache_fini();
1490         nf_conntrack_tstamp_fini();
1491         nf_conntrack_acct_fini();
1492         nf_conntrack_expect_fini();
1493 }
1494
1495 /*
1496  * Mishearing the voices in his head, our hero wonders how he's
1497  * supposed to kill the mall.
1498  */
1499 void nf_conntrack_cleanup_net(struct net *net)
1500 {
1501         LIST_HEAD(single);
1502
1503         list_add(&net->exit_list, &single);
1504         nf_conntrack_cleanup_net_list(&single);
1505 }
1506
1507 void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
1508 {
1509         int busy;
1510         struct net *net;
1511
1512         /*
1513          * This makes sure all current packets have passed through
1514          *  netfilter framework.  Roll on, two-stage module
1515          *  delete...
1516          */
1517         synchronize_net();
1518 i_see_dead_people:
1519         busy = 0;
1520         list_for_each_entry(net, net_exit_list, exit_list) {
1521                 nf_ct_iterate_cleanup(net, kill_all, NULL, 0, 0);
1522                 if (atomic_read(&net->ct.count) != 0)
1523                         busy = 1;
1524         }
1525         if (busy) {
1526                 schedule();
1527                 goto i_see_dead_people;
1528         }
1529
1530         list_for_each_entry(net, net_exit_list, exit_list) {
1531                 nf_ct_free_hashtable(net->ct.hash, net->ct.htable_size);
1532                 nf_conntrack_proto_pernet_fini(net);
1533                 nf_conntrack_helper_pernet_fini(net);
1534                 nf_conntrack_ecache_pernet_fini(net);
1535                 nf_conntrack_tstamp_pernet_fini(net);
1536                 nf_conntrack_acct_pernet_fini(net);
1537                 nf_conntrack_expect_pernet_fini(net);
1538                 kmem_cache_destroy(net->ct.nf_conntrack_cachep);
1539                 kfree(net->ct.slabname);
1540                 free_percpu(net->ct.stat);
1541                 free_percpu(net->ct.pcpu_lists);
1542         }
1543 }
1544
1545 void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
1546 {
1547         struct hlist_nulls_head *hash;
1548         unsigned int nr_slots, i;
1549         size_t sz;
1550
1551         BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
1552         nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
1553         sz = nr_slots * sizeof(struct hlist_nulls_head);
1554         hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1555                                         get_order(sz));
1556         if (!hash)
1557                 hash = vzalloc(sz);
1558
1559         if (hash && nulls)
1560                 for (i = 0; i < nr_slots; i++)
1561                         INIT_HLIST_NULLS_HEAD(&hash[i], i);
1562
1563         return hash;
1564 }
1565 EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
1566
1567 int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
1568 {
1569         int i, bucket, rc;
1570         unsigned int hashsize, old_size;
1571         struct hlist_nulls_head *hash, *old_hash;
1572         struct nf_conntrack_tuple_hash *h;
1573         struct nf_conn *ct;
1574
1575         if (current->nsproxy->net_ns != &init_net)
1576                 return -EOPNOTSUPP;
1577
1578         /* On boot, we can set this without any fancy locking. */
1579         if (!nf_conntrack_htable_size)
1580                 return param_set_uint(val, kp);
1581
1582         rc = kstrtouint(val, 0, &hashsize);
1583         if (rc)
1584                 return rc;
1585         if (!hashsize)
1586                 return -EINVAL;
1587
1588         hash = nf_ct_alloc_hashtable(&hashsize, 1);
1589         if (!hash)
1590                 return -ENOMEM;
1591
1592         local_bh_disable();
1593         nf_conntrack_all_lock();
1594         write_seqcount_begin(&nf_conntrack_generation);
1595
1596         /* Lookups in the old hash might happen in parallel, which means we
1597          * might get false negatives during connection lookup. New connections
1598          * created because of a false negative won't make it into the hash
1599          * though since that required taking the locks.
1600          */
1601
1602         for (i = 0; i < init_net.ct.htable_size; i++) {
1603                 while (!hlist_nulls_empty(&init_net.ct.hash[i])) {
1604                         h = hlist_nulls_entry(init_net.ct.hash[i].first,
1605                                         struct nf_conntrack_tuple_hash, hnnode);
1606                         ct = nf_ct_tuplehash_to_ctrack(h);
1607                         hlist_nulls_del_rcu(&h->hnnode);
1608                         bucket = __hash_conntrack(&h->tuple, hashsize);
1609                         hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
1610                 }
1611         }
1612         old_size = init_net.ct.htable_size;
1613         old_hash = init_net.ct.hash;
1614
1615         init_net.ct.htable_size = nf_conntrack_htable_size = hashsize;
1616         init_net.ct.hash = hash;
1617
1618         write_seqcount_end(&nf_conntrack_generation);
1619         nf_conntrack_all_unlock();
1620         local_bh_enable();
1621
1622         synchronize_net();
1623         nf_ct_free_hashtable(old_hash, old_size);
1624         return 0;
1625 }
1626 EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
1627
1628 module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
1629                   &nf_conntrack_htable_size, 0600);
1630
1631 void nf_ct_untracked_status_or(unsigned long bits)
1632 {
1633         int cpu;
1634
1635         for_each_possible_cpu(cpu)
1636                 per_cpu(nf_conntrack_untracked, cpu).status |= bits;
1637 }
1638 EXPORT_SYMBOL_GPL(nf_ct_untracked_status_or);
1639
1640 int nf_conntrack_init_start(void)
1641 {
1642         int max_factor = 8;
1643         int i, ret, cpu;
1644
1645         seqcount_init(&nf_conntrack_generation);
1646
1647         for (i = 0; i < CONNTRACK_LOCKS; i++)
1648                 spin_lock_init(&nf_conntrack_locks[i]);
1649
1650         if (!nf_conntrack_htable_size) {
1651                 /* Idea from tcp.c: use 1/16384 of memory.
1652                  * On i386: 32MB machine has 512 buckets.
1653                  * >= 1GB machines have 16384 buckets.
1654                  * >= 4GB machines have 65536 buckets.
1655                  */
1656                 nf_conntrack_htable_size
1657                         = (((totalram_pages << PAGE_SHIFT) / 16384)
1658                            / sizeof(struct hlist_head));
1659                 if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE)))
1660                         nf_conntrack_htable_size = 65536;
1661                 else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE))
1662                         nf_conntrack_htable_size = 16384;
1663                 if (nf_conntrack_htable_size < 32)
1664                         nf_conntrack_htable_size = 32;
1665
1666                 /* Use a max. factor of four by default to get the same max as
1667                  * with the old struct list_heads. When a table size is given
1668                  * we use the old value of 8 to avoid reducing the max.
1669                  * entries. */
1670                 max_factor = 4;
1671         }
1672         nf_conntrack_max = max_factor * nf_conntrack_htable_size;
1673
1674         printk(KERN_INFO "nf_conntrack version %s (%u buckets, %d max)\n",
1675                NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1676                nf_conntrack_max);
1677
1678         ret = nf_conntrack_expect_init();
1679         if (ret < 0)
1680                 goto err_expect;
1681
1682         ret = nf_conntrack_acct_init();
1683         if (ret < 0)
1684                 goto err_acct;
1685
1686         ret = nf_conntrack_tstamp_init();
1687         if (ret < 0)
1688                 goto err_tstamp;
1689
1690         ret = nf_conntrack_ecache_init();
1691         if (ret < 0)
1692                 goto err_ecache;
1693
1694         ret = nf_conntrack_timeout_init();
1695         if (ret < 0)
1696                 goto err_timeout;
1697
1698         ret = nf_conntrack_helper_init();
1699         if (ret < 0)
1700                 goto err_helper;
1701
1702         ret = nf_conntrack_labels_init();
1703         if (ret < 0)
1704                 goto err_labels;
1705
1706         ret = nf_conntrack_seqadj_init();
1707         if (ret < 0)
1708                 goto err_seqadj;
1709
1710 #ifdef CONFIG_NF_CONNTRACK_ZONES
1711         ret = nf_ct_extend_register(&nf_ct_zone_extend);
1712         if (ret < 0)
1713                 goto err_extend;
1714 #endif
1715         ret = nf_conntrack_proto_init();
1716         if (ret < 0)
1717                 goto err_proto;
1718
1719         /* Set up fake conntrack: to never be deleted, not in any hashes */
1720         for_each_possible_cpu(cpu) {
1721                 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
1722                 write_pnet(&ct->ct_net, &init_net);
1723                 atomic_set(&ct->ct_general.use, 1);
1724         }
1725         /*  - and look it like as a confirmed connection */
1726         nf_ct_untracked_status_or(IPS_CONFIRMED | IPS_UNTRACKED);
1727         return 0;
1728
1729 err_proto:
1730 #ifdef CONFIG_NF_CONNTRACK_ZONES
1731         nf_ct_extend_unregister(&nf_ct_zone_extend);
1732 err_extend:
1733 #endif
1734         nf_conntrack_seqadj_fini();
1735 err_seqadj:
1736         nf_conntrack_labels_fini();
1737 err_labels:
1738         nf_conntrack_helper_fini();
1739 err_helper:
1740         nf_conntrack_timeout_fini();
1741 err_timeout:
1742         nf_conntrack_ecache_fini();
1743 err_ecache:
1744         nf_conntrack_tstamp_fini();
1745 err_tstamp:
1746         nf_conntrack_acct_fini();
1747 err_acct:
1748         nf_conntrack_expect_fini();
1749 err_expect:
1750         return ret;
1751 }
1752
1753 void nf_conntrack_init_end(void)
1754 {
1755         /* For use by REJECT target */
1756         RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach);
1757         RCU_INIT_POINTER(nf_ct_destroy, destroy_conntrack);
1758 }
1759
1760 /*
1761  * We need to use special "null" values, not used in hash table
1762  */
1763 #define UNCONFIRMED_NULLS_VAL   ((1<<30)+0)
1764 #define DYING_NULLS_VAL         ((1<<30)+1)
1765 #define TEMPLATE_NULLS_VAL      ((1<<30)+2)
1766
1767 int nf_conntrack_init_net(struct net *net)
1768 {
1769         int ret = -ENOMEM;
1770         int cpu;
1771
1772         atomic_set(&net->ct.count, 0);
1773
1774         net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu);
1775         if (!net->ct.pcpu_lists)
1776                 goto err_stat;
1777
1778         for_each_possible_cpu(cpu) {
1779                 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1780
1781                 spin_lock_init(&pcpu->lock);
1782                 INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL);
1783                 INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL);
1784         }
1785
1786         net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
1787         if (!net->ct.stat)
1788                 goto err_pcpu_lists;
1789
1790         net->ct.slabname = kasprintf(GFP_KERNEL, "nf_conntrack_%p", net);
1791         if (!net->ct.slabname)
1792                 goto err_slabname;
1793
1794         net->ct.nf_conntrack_cachep = kmem_cache_create(net->ct.slabname,
1795                                                         sizeof(struct nf_conn), 0,
1796                                                         SLAB_DESTROY_BY_RCU, NULL);
1797         if (!net->ct.nf_conntrack_cachep) {
1798                 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1799                 goto err_cache;
1800         }
1801
1802         net->ct.htable_size = nf_conntrack_htable_size;
1803         net->ct.hash = nf_ct_alloc_hashtable(&net->ct.htable_size, 1);
1804         if (!net->ct.hash) {
1805                 printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
1806                 goto err_hash;
1807         }
1808         ret = nf_conntrack_expect_pernet_init(net);
1809         if (ret < 0)
1810                 goto err_expect;
1811         ret = nf_conntrack_acct_pernet_init(net);
1812         if (ret < 0)
1813                 goto err_acct;
1814         ret = nf_conntrack_tstamp_pernet_init(net);
1815         if (ret < 0)
1816                 goto err_tstamp;
1817         ret = nf_conntrack_ecache_pernet_init(net);
1818         if (ret < 0)
1819                 goto err_ecache;
1820         ret = nf_conntrack_helper_pernet_init(net);
1821         if (ret < 0)
1822                 goto err_helper;
1823         ret = nf_conntrack_proto_pernet_init(net);
1824         if (ret < 0)
1825                 goto err_proto;
1826         return 0;
1827
1828 err_proto:
1829         nf_conntrack_helper_pernet_fini(net);
1830 err_helper:
1831         nf_conntrack_ecache_pernet_fini(net);
1832 err_ecache:
1833         nf_conntrack_tstamp_pernet_fini(net);
1834 err_tstamp:
1835         nf_conntrack_acct_pernet_fini(net);
1836 err_acct:
1837         nf_conntrack_expect_pernet_fini(net);
1838 err_expect:
1839         nf_ct_free_hashtable(net->ct.hash, net->ct.htable_size);
1840 err_hash:
1841         kmem_cache_destroy(net->ct.nf_conntrack_cachep);
1842 err_cache:
1843         kfree(net->ct.slabname);
1844 err_slabname:
1845         free_percpu(net->ct.stat);
1846 err_pcpu_lists:
1847         free_percpu(net->ct.pcpu_lists);
1848 err_stat:
1849         return ret;
1850 }