netfilter: conntrack: move helper destruction to nf_ct_helper_destroy()
[pandora-kernel.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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/types.h>
15 #include <linux/netfilter.h>
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/proc_fs.h>
19 #include <linux/vmalloc.h>
20 #include <linux/stddef.h>
21 #include <linux/slab.h>
22 #include <linux/random.h>
23 #include <linux/jhash.h>
24 #include <linux/err.h>
25 #include <linux/percpu.h>
26 #include <linux/moduleparam.h>
27 #include <linux/notifier.h>
28 #include <linux/kernel.h>
29 #include <linux/netdevice.h>
30 #include <linux/socket.h>
31 #include <linux/mm.h>
32 #include <linux/rculist_nulls.h>
33
34 #include <net/netfilter/nf_conntrack.h>
35 #include <net/netfilter/nf_conntrack_l3proto.h>
36 #include <net/netfilter/nf_conntrack_l4proto.h>
37 #include <net/netfilter/nf_conntrack_expect.h>
38 #include <net/netfilter/nf_conntrack_helper.h>
39 #include <net/netfilter/nf_conntrack_core.h>
40 #include <net/netfilter/nf_conntrack_extend.h>
41 #include <net/netfilter/nf_conntrack_acct.h>
42 #include <net/netfilter/nf_conntrack_ecache.h>
43 #include <net/netfilter/nf_nat.h>
44 #include <net/netfilter/nf_nat_core.h>
45
46 #define NF_CONNTRACK_VERSION    "0.5.0"
47
48 int (*nfnetlink_parse_nat_setup_hook)(struct nf_conn *ct,
49                                       enum nf_nat_manip_type manip,
50                                       struct nlattr *attr) __read_mostly;
51 EXPORT_SYMBOL_GPL(nfnetlink_parse_nat_setup_hook);
52
53 DEFINE_SPINLOCK(nf_conntrack_lock);
54 EXPORT_SYMBOL_GPL(nf_conntrack_lock);
55
56 unsigned int nf_conntrack_htable_size __read_mostly;
57 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
58
59 unsigned int nf_conntrack_max __read_mostly;
60 EXPORT_SYMBOL_GPL(nf_conntrack_max);
61
62 struct nf_conn nf_conntrack_untracked __read_mostly;
63 EXPORT_SYMBOL_GPL(nf_conntrack_untracked);
64
65 static struct kmem_cache *nf_conntrack_cachep __read_mostly;
66
67 static int nf_conntrack_hash_rnd_initted;
68 static unsigned int nf_conntrack_hash_rnd;
69
70 static u_int32_t __hash_conntrack(const struct nf_conntrack_tuple *tuple,
71                                   unsigned int size, unsigned int rnd)
72 {
73         unsigned int n;
74         u_int32_t h;
75
76         /* The direction must be ignored, so we hash everything up to the
77          * destination ports (which is a multiple of 4) and treat the last
78          * three bytes manually.
79          */
80         n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
81         h = jhash2((u32 *)tuple, n,
82                    rnd ^ (((__force __u16)tuple->dst.u.all << 16) |
83                           tuple->dst.protonum));
84
85         return ((u64)h * size) >> 32;
86 }
87
88 static inline u_int32_t hash_conntrack(const struct nf_conntrack_tuple *tuple)
89 {
90         return __hash_conntrack(tuple, nf_conntrack_htable_size,
91                                 nf_conntrack_hash_rnd);
92 }
93
94 bool
95 nf_ct_get_tuple(const struct sk_buff *skb,
96                 unsigned int nhoff,
97                 unsigned int dataoff,
98                 u_int16_t l3num,
99                 u_int8_t protonum,
100                 struct nf_conntrack_tuple *tuple,
101                 const struct nf_conntrack_l3proto *l3proto,
102                 const struct nf_conntrack_l4proto *l4proto)
103 {
104         memset(tuple, 0, sizeof(*tuple));
105
106         tuple->src.l3num = l3num;
107         if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
108                 return false;
109
110         tuple->dst.protonum = protonum;
111         tuple->dst.dir = IP_CT_DIR_ORIGINAL;
112
113         return l4proto->pkt_to_tuple(skb, dataoff, tuple);
114 }
115 EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
116
117 bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
118                        u_int16_t l3num, struct nf_conntrack_tuple *tuple)
119 {
120         struct nf_conntrack_l3proto *l3proto;
121         struct nf_conntrack_l4proto *l4proto;
122         unsigned int protoff;
123         u_int8_t protonum;
124         int ret;
125
126         rcu_read_lock();
127
128         l3proto = __nf_ct_l3proto_find(l3num);
129         ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum);
130         if (ret != NF_ACCEPT) {
131                 rcu_read_unlock();
132                 return false;
133         }
134
135         l4proto = __nf_ct_l4proto_find(l3num, protonum);
136
137         ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, tuple,
138                               l3proto, l4proto);
139
140         rcu_read_unlock();
141         return ret;
142 }
143 EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
144
145 bool
146 nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
147                    const struct nf_conntrack_tuple *orig,
148                    const struct nf_conntrack_l3proto *l3proto,
149                    const struct nf_conntrack_l4proto *l4proto)
150 {
151         memset(inverse, 0, sizeof(*inverse));
152
153         inverse->src.l3num = orig->src.l3num;
154         if (l3proto->invert_tuple(inverse, orig) == 0)
155                 return false;
156
157         inverse->dst.dir = !orig->dst.dir;
158
159         inverse->dst.protonum = orig->dst.protonum;
160         return l4proto->invert_tuple(inverse, orig);
161 }
162 EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
163
164 static void
165 clean_from_lists(struct nf_conn *ct)
166 {
167         pr_debug("clean_from_lists(%p)\n", ct);
168         hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
169         hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode);
170
171         /* Destroy all pending expectations */
172         nf_ct_remove_expectations(ct);
173 }
174
175 static void
176 destroy_conntrack(struct nf_conntrack *nfct)
177 {
178         struct nf_conn *ct = (struct nf_conn *)nfct;
179         struct net *net = nf_ct_net(ct);
180         struct nf_conntrack_l4proto *l4proto;
181
182         pr_debug("destroy_conntrack(%p)\n", ct);
183         NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
184         NF_CT_ASSERT(!timer_pending(&ct->timeout));
185
186         if (!test_bit(IPS_DYING_BIT, &ct->status))
187                 nf_conntrack_event(IPCT_DESTROY, ct);
188         set_bit(IPS_DYING_BIT, &ct->status);
189
190         /* To make sure we don't get any weird locking issues here:
191          * destroy_conntrack() MUST NOT be called with a write lock
192          * to nf_conntrack_lock!!! -HW */
193         rcu_read_lock();
194         l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
195         if (l4proto && l4proto->destroy)
196                 l4proto->destroy(ct);
197
198         rcu_read_unlock();
199
200         spin_lock_bh(&nf_conntrack_lock);
201         /* Expectations will have been removed in clean_from_lists,
202          * except TFTP can create an expectation on the first packet,
203          * before connection is in the list, so we need to clean here,
204          * too. */
205         nf_ct_remove_expectations(ct);
206
207         /* We overload first tuple to link into unconfirmed list. */
208         if (!nf_ct_is_confirmed(ct)) {
209                 BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode));
210                 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
211         }
212
213         NF_CT_STAT_INC(net, delete);
214         spin_unlock_bh(&nf_conntrack_lock);
215
216         if (ct->master)
217                 nf_ct_put(ct->master);
218
219         pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct);
220         nf_conntrack_free(ct);
221 }
222
223 static void death_by_timeout(unsigned long ul_conntrack)
224 {
225         struct nf_conn *ct = (void *)ul_conntrack;
226         struct net *net = nf_ct_net(ct);
227
228         nf_ct_helper_destroy(ct);
229         spin_lock_bh(&nf_conntrack_lock);
230         /* Inside lock so preempt is disabled on module removal path.
231          * Otherwise we can get spurious warnings. */
232         NF_CT_STAT_INC(net, delete_list);
233         clean_from_lists(ct);
234         spin_unlock_bh(&nf_conntrack_lock);
235         nf_ct_put(ct);
236 }
237
238 /*
239  * Warning :
240  * - Caller must take a reference on returned object
241  *   and recheck nf_ct_tuple_equal(tuple, &h->tuple)
242  * OR
243  * - Caller must lock nf_conntrack_lock before calling this function
244  */
245 struct nf_conntrack_tuple_hash *
246 __nf_conntrack_find(struct net *net, const struct nf_conntrack_tuple *tuple)
247 {
248         struct nf_conntrack_tuple_hash *h;
249         struct hlist_nulls_node *n;
250         unsigned int hash = hash_conntrack(tuple);
251
252         /* Disable BHs the entire time since we normally need to disable them
253          * at least once for the stats anyway.
254          */
255         local_bh_disable();
256 begin:
257         hlist_nulls_for_each_entry_rcu(h, n, &net->ct.hash[hash], hnnode) {
258                 if (nf_ct_tuple_equal(tuple, &h->tuple)) {
259                         NF_CT_STAT_INC(net, found);
260                         local_bh_enable();
261                         return h;
262                 }
263                 NF_CT_STAT_INC(net, searched);
264         }
265         /*
266          * if the nulls value we got at the end of this lookup is
267          * not the expected one, we must restart lookup.
268          * We probably met an item that was moved to another chain.
269          */
270         if (get_nulls_value(n) != hash)
271                 goto begin;
272         local_bh_enable();
273
274         return NULL;
275 }
276 EXPORT_SYMBOL_GPL(__nf_conntrack_find);
277
278 /* Find a connection corresponding to a tuple. */
279 struct nf_conntrack_tuple_hash *
280 nf_conntrack_find_get(struct net *net, const struct nf_conntrack_tuple *tuple)
281 {
282         struct nf_conntrack_tuple_hash *h;
283         struct nf_conn *ct;
284
285         rcu_read_lock();
286 begin:
287         h = __nf_conntrack_find(net, tuple);
288         if (h) {
289                 ct = nf_ct_tuplehash_to_ctrack(h);
290                 if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
291                         h = NULL;
292                 else {
293                         if (unlikely(!nf_ct_tuple_equal(tuple, &h->tuple))) {
294                                 nf_ct_put(ct);
295                                 goto begin;
296                         }
297                 }
298         }
299         rcu_read_unlock();
300
301         return h;
302 }
303 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
304
305 static void __nf_conntrack_hash_insert(struct nf_conn *ct,
306                                        unsigned int hash,
307                                        unsigned int repl_hash)
308 {
309         struct net *net = nf_ct_net(ct);
310
311         hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
312                            &net->ct.hash[hash]);
313         hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
314                            &net->ct.hash[repl_hash]);
315 }
316
317 void nf_conntrack_hash_insert(struct nf_conn *ct)
318 {
319         unsigned int hash, repl_hash;
320
321         hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
322         repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
323
324         __nf_conntrack_hash_insert(ct, hash, repl_hash);
325 }
326 EXPORT_SYMBOL_GPL(nf_conntrack_hash_insert);
327
328 /* Confirm a connection given skb; places it in hash table */
329 int
330 __nf_conntrack_confirm(struct sk_buff *skb)
331 {
332         unsigned int hash, repl_hash;
333         struct nf_conntrack_tuple_hash *h;
334         struct nf_conn *ct;
335         struct nf_conn_help *help;
336         struct hlist_nulls_node *n;
337         enum ip_conntrack_info ctinfo;
338         struct net *net;
339
340         ct = nf_ct_get(skb, &ctinfo);
341         net = nf_ct_net(ct);
342
343         /* ipt_REJECT uses nf_conntrack_attach to attach related
344            ICMP/TCP RST packets in other direction.  Actual packet
345            which created connection will be IP_CT_NEW or for an
346            expected connection, IP_CT_RELATED. */
347         if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
348                 return NF_ACCEPT;
349
350         hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
351         repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
352
353         /* We're not in hash table, and we refuse to set up related
354            connections for unconfirmed conns.  But packet copies and
355            REJECT will give spurious warnings here. */
356         /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
357
358         /* No external references means noone else could have
359            confirmed us. */
360         NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
361         pr_debug("Confirming conntrack %p\n", ct);
362
363         spin_lock_bh(&nf_conntrack_lock);
364
365         /* See if there's one in the list already, including reverse:
366            NAT could have grabbed it without realizing, since we're
367            not in the hash.  If there is, we lost race. */
368         hlist_nulls_for_each_entry(h, n, &net->ct.hash[hash], hnnode)
369                 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
370                                       &h->tuple))
371                         goto out;
372         hlist_nulls_for_each_entry(h, n, &net->ct.hash[repl_hash], hnnode)
373                 if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
374                                       &h->tuple))
375                         goto out;
376
377         /* Remove from unconfirmed list */
378         hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
379
380         __nf_conntrack_hash_insert(ct, hash, repl_hash);
381         /* Timer relative to confirmation time, not original
382            setting time, otherwise we'd get timer wrap in
383            weird delay cases. */
384         ct->timeout.expires += jiffies;
385         add_timer(&ct->timeout);
386         atomic_inc(&ct->ct_general.use);
387         set_bit(IPS_CONFIRMED_BIT, &ct->status);
388         NF_CT_STAT_INC(net, insert);
389         spin_unlock_bh(&nf_conntrack_lock);
390         help = nfct_help(ct);
391         if (help && help->helper)
392                 nf_conntrack_event_cache(IPCT_HELPER, ct);
393
394         nf_conntrack_event_cache(master_ct(ct) ?
395                                  IPCT_RELATED : IPCT_NEW, ct);
396         return NF_ACCEPT;
397
398 out:
399         NF_CT_STAT_INC(net, insert_failed);
400         spin_unlock_bh(&nf_conntrack_lock);
401         return NF_DROP;
402 }
403 EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
404
405 /* Returns true if a connection correspondings to the tuple (required
406    for NAT). */
407 int
408 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
409                          const struct nf_conn *ignored_conntrack)
410 {
411         struct net *net = nf_ct_net(ignored_conntrack);
412         struct nf_conntrack_tuple_hash *h;
413         struct hlist_nulls_node *n;
414         unsigned int hash = hash_conntrack(tuple);
415
416         /* Disable BHs the entire time since we need to disable them at
417          * least once for the stats anyway.
418          */
419         rcu_read_lock_bh();
420         hlist_nulls_for_each_entry_rcu(h, n, &net->ct.hash[hash], hnnode) {
421                 if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
422                     nf_ct_tuple_equal(tuple, &h->tuple)) {
423                         NF_CT_STAT_INC(net, found);
424                         rcu_read_unlock_bh();
425                         return 1;
426                 }
427                 NF_CT_STAT_INC(net, searched);
428         }
429         rcu_read_unlock_bh();
430
431         return 0;
432 }
433 EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
434
435 #define NF_CT_EVICTION_RANGE    8
436
437 /* There's a small race here where we may free a just-assured
438    connection.  Too bad: we're in trouble anyway. */
439 static noinline int early_drop(struct net *net, unsigned int hash)
440 {
441         /* Use oldest entry, which is roughly LRU */
442         struct nf_conntrack_tuple_hash *h;
443         struct nf_conn *ct = NULL, *tmp;
444         struct hlist_nulls_node *n;
445         unsigned int i, cnt = 0;
446         int dropped = 0;
447
448         rcu_read_lock();
449         for (i = 0; i < nf_conntrack_htable_size; i++) {
450                 hlist_nulls_for_each_entry_rcu(h, n, &net->ct.hash[hash],
451                                          hnnode) {
452                         tmp = nf_ct_tuplehash_to_ctrack(h);
453                         if (!test_bit(IPS_ASSURED_BIT, &tmp->status))
454                                 ct = tmp;
455                         cnt++;
456                 }
457
458                 if (ct && unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
459                         ct = NULL;
460                 if (ct || cnt >= NF_CT_EVICTION_RANGE)
461                         break;
462                 hash = (hash + 1) % nf_conntrack_htable_size;
463         }
464         rcu_read_unlock();
465
466         if (!ct)
467                 return dropped;
468
469         if (del_timer(&ct->timeout)) {
470                 death_by_timeout((unsigned long)ct);
471                 dropped = 1;
472                 NF_CT_STAT_INC_ATOMIC(net, early_drop);
473         }
474         nf_ct_put(ct);
475         return dropped;
476 }
477
478 struct nf_conn *nf_conntrack_alloc(struct net *net,
479                                    const struct nf_conntrack_tuple *orig,
480                                    const struct nf_conntrack_tuple *repl,
481                                    gfp_t gfp)
482 {
483         struct nf_conn *ct;
484
485         if (unlikely(!nf_conntrack_hash_rnd_initted)) {
486                 get_random_bytes(&nf_conntrack_hash_rnd,
487                                 sizeof(nf_conntrack_hash_rnd));
488                 nf_conntrack_hash_rnd_initted = 1;
489         }
490
491         /* We don't want any race condition at early drop stage */
492         atomic_inc(&net->ct.count);
493
494         if (nf_conntrack_max &&
495             unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
496                 unsigned int hash = hash_conntrack(orig);
497                 if (!early_drop(net, hash)) {
498                         atomic_dec(&net->ct.count);
499                         if (net_ratelimit())
500                                 printk(KERN_WARNING
501                                        "nf_conntrack: table full, dropping"
502                                        " packet.\n");
503                         return ERR_PTR(-ENOMEM);
504                 }
505         }
506
507         ct = kmem_cache_zalloc(nf_conntrack_cachep, gfp);
508         if (ct == NULL) {
509                 pr_debug("nf_conntrack_alloc: Can't alloc conntrack.\n");
510                 atomic_dec(&net->ct.count);
511                 return ERR_PTR(-ENOMEM);
512         }
513
514         spin_lock_init(&ct->lock);
515         atomic_set(&ct->ct_general.use, 1);
516         ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
517         ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
518         /* Don't set timer yet: wait for confirmation */
519         setup_timer(&ct->timeout, death_by_timeout, (unsigned long)ct);
520 #ifdef CONFIG_NET_NS
521         ct->ct_net = net;
522 #endif
523
524         return ct;
525 }
526 EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
527
528 void nf_conntrack_free(struct nf_conn *ct)
529 {
530         struct net *net = nf_ct_net(ct);
531
532         nf_ct_ext_destroy(ct);
533         atomic_dec(&net->ct.count);
534         nf_ct_ext_free(ct);
535         kmem_cache_free(nf_conntrack_cachep, ct);
536 }
537 EXPORT_SYMBOL_GPL(nf_conntrack_free);
538
539 /* Allocate a new conntrack: we return -ENOMEM if classification
540    failed due to stress.  Otherwise it really is unclassifiable. */
541 static struct nf_conntrack_tuple_hash *
542 init_conntrack(struct net *net,
543                const struct nf_conntrack_tuple *tuple,
544                struct nf_conntrack_l3proto *l3proto,
545                struct nf_conntrack_l4proto *l4proto,
546                struct sk_buff *skb,
547                unsigned int dataoff)
548 {
549         struct nf_conn *ct;
550         struct nf_conn_help *help;
551         struct nf_conntrack_tuple repl_tuple;
552         struct nf_conntrack_expect *exp;
553
554         if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
555                 pr_debug("Can't invert tuple.\n");
556                 return NULL;
557         }
558
559         ct = nf_conntrack_alloc(net, tuple, &repl_tuple, GFP_ATOMIC);
560         if (IS_ERR(ct)) {
561                 pr_debug("Can't allocate conntrack.\n");
562                 return (struct nf_conntrack_tuple_hash *)ct;
563         }
564
565         if (!l4proto->new(ct, skb, dataoff)) {
566                 nf_conntrack_free(ct);
567                 pr_debug("init conntrack: can't track with proto module\n");
568                 return NULL;
569         }
570
571         nf_ct_acct_ext_add(ct, GFP_ATOMIC);
572         nf_ct_ecache_ext_add(ct, GFP_ATOMIC);
573
574         spin_lock_bh(&nf_conntrack_lock);
575         exp = nf_ct_find_expectation(net, tuple);
576         if (exp) {
577                 pr_debug("conntrack: expectation arrives ct=%p exp=%p\n",
578                          ct, exp);
579                 /* Welcome, Mr. Bond.  We've been expecting you... */
580                 __set_bit(IPS_EXPECTED_BIT, &ct->status);
581                 ct->master = exp->master;
582                 if (exp->helper) {
583                         help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
584                         if (help)
585                                 rcu_assign_pointer(help->helper, exp->helper);
586                 }
587
588 #ifdef CONFIG_NF_CONNTRACK_MARK
589                 ct->mark = exp->master->mark;
590 #endif
591 #ifdef CONFIG_NF_CONNTRACK_SECMARK
592                 ct->secmark = exp->master->secmark;
593 #endif
594                 nf_conntrack_get(&ct->master->ct_general);
595                 NF_CT_STAT_INC(net, expect_new);
596         } else {
597                 __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
598                 NF_CT_STAT_INC(net, new);
599         }
600
601         /* Overload tuple linked list to put us in unconfirmed list. */
602         hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
603                        &net->ct.unconfirmed);
604
605         spin_unlock_bh(&nf_conntrack_lock);
606
607         if (exp) {
608                 if (exp->expectfn)
609                         exp->expectfn(ct, exp);
610                 nf_ct_expect_put(exp);
611         }
612
613         return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
614 }
615
616 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
617 static inline struct nf_conn *
618 resolve_normal_ct(struct net *net,
619                   struct sk_buff *skb,
620                   unsigned int dataoff,
621                   u_int16_t l3num,
622                   u_int8_t protonum,
623                   struct nf_conntrack_l3proto *l3proto,
624                   struct nf_conntrack_l4proto *l4proto,
625                   int *set_reply,
626                   enum ip_conntrack_info *ctinfo)
627 {
628         struct nf_conntrack_tuple tuple;
629         struct nf_conntrack_tuple_hash *h;
630         struct nf_conn *ct;
631
632         if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
633                              dataoff, l3num, protonum, &tuple, l3proto,
634                              l4proto)) {
635                 pr_debug("resolve_normal_ct: Can't get tuple\n");
636                 return NULL;
637         }
638
639         /* look for tuple match */
640         h = nf_conntrack_find_get(net, &tuple);
641         if (!h) {
642                 h = init_conntrack(net, &tuple, l3proto, l4proto, skb, dataoff);
643                 if (!h)
644                         return NULL;
645                 if (IS_ERR(h))
646                         return (void *)h;
647         }
648         ct = nf_ct_tuplehash_to_ctrack(h);
649
650         /* It exists; we have (non-exclusive) reference. */
651         if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
652                 *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
653                 /* Please set reply bit if this packet OK */
654                 *set_reply = 1;
655         } else {
656                 /* Once we've had two way comms, always ESTABLISHED. */
657                 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
658                         pr_debug("nf_conntrack_in: normal packet for %p\n", ct);
659                         *ctinfo = IP_CT_ESTABLISHED;
660                 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
661                         pr_debug("nf_conntrack_in: related packet for %p\n",
662                                  ct);
663                         *ctinfo = IP_CT_RELATED;
664                 } else {
665                         pr_debug("nf_conntrack_in: new packet for %p\n", ct);
666                         *ctinfo = IP_CT_NEW;
667                 }
668                 *set_reply = 0;
669         }
670         skb->nfct = &ct->ct_general;
671         skb->nfctinfo = *ctinfo;
672         return ct;
673 }
674
675 unsigned int
676 nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
677                 struct sk_buff *skb)
678 {
679         struct nf_conn *ct;
680         enum ip_conntrack_info ctinfo;
681         struct nf_conntrack_l3proto *l3proto;
682         struct nf_conntrack_l4proto *l4proto;
683         unsigned int dataoff;
684         u_int8_t protonum;
685         int set_reply = 0;
686         int ret;
687
688         /* Previously seen (loopback or untracked)?  Ignore. */
689         if (skb->nfct) {
690                 NF_CT_STAT_INC_ATOMIC(net, ignore);
691                 return NF_ACCEPT;
692         }
693
694         /* rcu_read_lock()ed by nf_hook_slow */
695         l3proto = __nf_ct_l3proto_find(pf);
696         ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
697                                    &dataoff, &protonum);
698         if (ret <= 0) {
699                 pr_debug("not prepared to track yet or error occured\n");
700                 NF_CT_STAT_INC_ATOMIC(net, error);
701                 NF_CT_STAT_INC_ATOMIC(net, invalid);
702                 return -ret;
703         }
704
705         l4proto = __nf_ct_l4proto_find(pf, protonum);
706
707         /* It may be an special packet, error, unclean...
708          * inverse of the return code tells to the netfilter
709          * core what to do with the packet. */
710         if (l4proto->error != NULL) {
711                 ret = l4proto->error(net, skb, dataoff, &ctinfo, pf, hooknum);
712                 if (ret <= 0) {
713                         NF_CT_STAT_INC_ATOMIC(net, error);
714                         NF_CT_STAT_INC_ATOMIC(net, invalid);
715                         return -ret;
716                 }
717         }
718
719         ct = resolve_normal_ct(net, skb, dataoff, pf, protonum,
720                                l3proto, l4proto, &set_reply, &ctinfo);
721         if (!ct) {
722                 /* Not valid part of a connection */
723                 NF_CT_STAT_INC_ATOMIC(net, invalid);
724                 return NF_ACCEPT;
725         }
726
727         if (IS_ERR(ct)) {
728                 /* Too stressed to deal. */
729                 NF_CT_STAT_INC_ATOMIC(net, drop);
730                 return NF_DROP;
731         }
732
733         NF_CT_ASSERT(skb->nfct);
734
735         ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum);
736         if (ret <= 0) {
737                 /* Invalid: inverse of the return code tells
738                  * the netfilter core what to do */
739                 pr_debug("nf_conntrack_in: Can't track with proto module\n");
740                 nf_conntrack_put(skb->nfct);
741                 skb->nfct = NULL;
742                 NF_CT_STAT_INC_ATOMIC(net, invalid);
743                 if (ret == -NF_DROP)
744                         NF_CT_STAT_INC_ATOMIC(net, drop);
745                 return -ret;
746         }
747
748         if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
749                 nf_conntrack_event_cache(IPCT_STATUS, ct);
750
751         return ret;
752 }
753 EXPORT_SYMBOL_GPL(nf_conntrack_in);
754
755 bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
756                           const struct nf_conntrack_tuple *orig)
757 {
758         bool ret;
759
760         rcu_read_lock();
761         ret = nf_ct_invert_tuple(inverse, orig,
762                                  __nf_ct_l3proto_find(orig->src.l3num),
763                                  __nf_ct_l4proto_find(orig->src.l3num,
764                                                       orig->dst.protonum));
765         rcu_read_unlock();
766         return ret;
767 }
768 EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
769
770 /* Alter reply tuple (maybe alter helper).  This is for NAT, and is
771    implicitly racy: see __nf_conntrack_confirm */
772 void nf_conntrack_alter_reply(struct nf_conn *ct,
773                               const struct nf_conntrack_tuple *newreply)
774 {
775         struct nf_conn_help *help = nfct_help(ct);
776
777         /* Should be unconfirmed, so not in hash table yet */
778         NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
779
780         pr_debug("Altering reply tuple of %p to ", ct);
781         nf_ct_dump_tuple(newreply);
782
783         ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
784         if (ct->master || (help && !hlist_empty(&help->expectations)))
785                 return;
786
787         rcu_read_lock();
788         __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
789         rcu_read_unlock();
790 }
791 EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
792
793 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
794 void __nf_ct_refresh_acct(struct nf_conn *ct,
795                           enum ip_conntrack_info ctinfo,
796                           const struct sk_buff *skb,
797                           unsigned long extra_jiffies,
798                           int do_acct)
799 {
800         NF_CT_ASSERT(ct->timeout.data == (unsigned long)ct);
801         NF_CT_ASSERT(skb);
802
803         /* Only update if this is not a fixed timeout */
804         if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
805                 goto acct;
806
807         /* If not in hash table, timer will not be active yet */
808         if (!nf_ct_is_confirmed(ct)) {
809                 ct->timeout.expires = extra_jiffies;
810         } else {
811                 unsigned long newtime = jiffies + extra_jiffies;
812
813                 /* Only update the timeout if the new timeout is at least
814                    HZ jiffies from the old timeout. Need del_timer for race
815                    avoidance (may already be dying). */
816                 if (newtime - ct->timeout.expires >= HZ)
817                         mod_timer_pending(&ct->timeout, newtime);
818         }
819
820 acct:
821         if (do_acct) {
822                 struct nf_conn_counter *acct;
823
824                 acct = nf_conn_acct_find(ct);
825                 if (acct) {
826                         spin_lock_bh(&ct->lock);
827                         acct[CTINFO2DIR(ctinfo)].packets++;
828                         acct[CTINFO2DIR(ctinfo)].bytes +=
829                                 skb->len - skb_network_offset(skb);
830                         spin_unlock_bh(&ct->lock);
831                 }
832         }
833 }
834 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
835
836 bool __nf_ct_kill_acct(struct nf_conn *ct,
837                        enum ip_conntrack_info ctinfo,
838                        const struct sk_buff *skb,
839                        int do_acct)
840 {
841         if (do_acct) {
842                 struct nf_conn_counter *acct;
843
844                 acct = nf_conn_acct_find(ct);
845                 if (acct) {
846                         spin_lock_bh(&ct->lock);
847                         acct[CTINFO2DIR(ctinfo)].packets++;
848                         acct[CTINFO2DIR(ctinfo)].bytes +=
849                                 skb->len - skb_network_offset(skb);
850                         spin_unlock_bh(&ct->lock);
851                 }
852         }
853
854         if (del_timer(&ct->timeout)) {
855                 ct->timeout.function((unsigned long)ct);
856                 return true;
857         }
858         return false;
859 }
860 EXPORT_SYMBOL_GPL(__nf_ct_kill_acct);
861
862 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
863
864 #include <linux/netfilter/nfnetlink.h>
865 #include <linux/netfilter/nfnetlink_conntrack.h>
866 #include <linux/mutex.h>
867
868 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
869  * in ip_conntrack_core, since we don't want the protocols to autoload
870  * or depend on ctnetlink */
871 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
872                                const struct nf_conntrack_tuple *tuple)
873 {
874         NLA_PUT_BE16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port);
875         NLA_PUT_BE16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port);
876         return 0;
877
878 nla_put_failure:
879         return -1;
880 }
881 EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
882
883 const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
884         [CTA_PROTO_SRC_PORT]  = { .type = NLA_U16 },
885         [CTA_PROTO_DST_PORT]  = { .type = NLA_U16 },
886 };
887 EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
888
889 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
890                                struct nf_conntrack_tuple *t)
891 {
892         if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
893                 return -EINVAL;
894
895         t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
896         t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
897
898         return 0;
899 }
900 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
901
902 int nf_ct_port_nlattr_tuple_size(void)
903 {
904         return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
905 }
906 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
907 #endif
908
909 /* Used by ipt_REJECT and ip6t_REJECT. */
910 static void nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
911 {
912         struct nf_conn *ct;
913         enum ip_conntrack_info ctinfo;
914
915         /* This ICMP is in reverse direction to the packet which caused it */
916         ct = nf_ct_get(skb, &ctinfo);
917         if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
918                 ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
919         else
920                 ctinfo = IP_CT_RELATED;
921
922         /* Attach to new skbuff, and increment count */
923         nskb->nfct = &ct->ct_general;
924         nskb->nfctinfo = ctinfo;
925         nf_conntrack_get(nskb->nfct);
926 }
927
928 /* Bring out ya dead! */
929 static struct nf_conn *
930 get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
931                 void *data, unsigned int *bucket)
932 {
933         struct nf_conntrack_tuple_hash *h;
934         struct nf_conn *ct;
935         struct hlist_nulls_node *n;
936
937         spin_lock_bh(&nf_conntrack_lock);
938         for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
939                 hlist_nulls_for_each_entry(h, n, &net->ct.hash[*bucket], hnnode) {
940                         ct = nf_ct_tuplehash_to_ctrack(h);
941                         if (iter(ct, data))
942                                 goto found;
943                 }
944         }
945         hlist_nulls_for_each_entry(h, n, &net->ct.unconfirmed, hnnode) {
946                 ct = nf_ct_tuplehash_to_ctrack(h);
947                 if (iter(ct, data))
948                         set_bit(IPS_DYING_BIT, &ct->status);
949         }
950         spin_unlock_bh(&nf_conntrack_lock);
951         return NULL;
952 found:
953         atomic_inc(&ct->ct_general.use);
954         spin_unlock_bh(&nf_conntrack_lock);
955         return ct;
956 }
957
958 void nf_ct_iterate_cleanup(struct net *net,
959                            int (*iter)(struct nf_conn *i, void *data),
960                            void *data)
961 {
962         struct nf_conn *ct;
963         unsigned int bucket = 0;
964
965         while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
966                 /* Time to push up daises... */
967                 if (del_timer(&ct->timeout))
968                         death_by_timeout((unsigned long)ct);
969                 /* ... else the timer will get him soon. */
970
971                 nf_ct_put(ct);
972         }
973 }
974 EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
975
976 struct __nf_ct_flush_report {
977         u32 pid;
978         int report;
979 };
980
981 static int kill_report(struct nf_conn *i, void *data)
982 {
983         struct __nf_ct_flush_report *fr = (struct __nf_ct_flush_report *)data;
984
985         /* get_next_corpse sets the dying bit for us */
986         nf_conntrack_event_report(IPCT_DESTROY,
987                                   i,
988                                   fr->pid,
989                                   fr->report);
990         return 1;
991 }
992
993 static int kill_all(struct nf_conn *i, void *data)
994 {
995         return 1;
996 }
997
998 void nf_ct_free_hashtable(void *hash, int vmalloced, unsigned int size)
999 {
1000         if (vmalloced)
1001                 vfree(hash);
1002         else
1003                 free_pages((unsigned long)hash,
1004                            get_order(sizeof(struct hlist_head) * size));
1005 }
1006 EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
1007
1008 void nf_conntrack_flush_report(struct net *net, u32 pid, int report)
1009 {
1010         struct __nf_ct_flush_report fr = {
1011                 .pid    = pid,
1012                 .report = report,
1013         };
1014         nf_ct_iterate_cleanup(net, kill_report, &fr);
1015 }
1016 EXPORT_SYMBOL_GPL(nf_conntrack_flush_report);
1017
1018 static void nf_conntrack_cleanup_init_net(void)
1019 {
1020         nf_conntrack_helper_fini();
1021         nf_conntrack_proto_fini();
1022         kmem_cache_destroy(nf_conntrack_cachep);
1023 }
1024
1025 static void nf_conntrack_cleanup_net(struct net *net)
1026 {
1027  i_see_dead_people:
1028         nf_ct_iterate_cleanup(net, kill_all, NULL);
1029         if (atomic_read(&net->ct.count) != 0) {
1030                 schedule();
1031                 goto i_see_dead_people;
1032         }
1033         /* wait until all references to nf_conntrack_untracked are dropped */
1034         while (atomic_read(&nf_conntrack_untracked.ct_general.use) > 1)
1035                 schedule();
1036
1037         nf_ct_free_hashtable(net->ct.hash, net->ct.hash_vmalloc,
1038                              nf_conntrack_htable_size);
1039         nf_conntrack_ecache_fini(net);
1040         nf_conntrack_acct_fini(net);
1041         nf_conntrack_expect_fini(net);
1042         free_percpu(net->ct.stat);
1043 }
1044
1045 /* Mishearing the voices in his head, our hero wonders how he's
1046    supposed to kill the mall. */
1047 void nf_conntrack_cleanup(struct net *net)
1048 {
1049         if (net_eq(net, &init_net))
1050                 rcu_assign_pointer(ip_ct_attach, NULL);
1051
1052         /* This makes sure all current packets have passed through
1053            netfilter framework.  Roll on, two-stage module
1054            delete... */
1055         synchronize_net();
1056
1057         nf_conntrack_cleanup_net(net);
1058
1059         if (net_eq(net, &init_net)) {
1060                 rcu_assign_pointer(nf_ct_destroy, NULL);
1061                 nf_conntrack_cleanup_init_net();
1062         }
1063 }
1064
1065 void *nf_ct_alloc_hashtable(unsigned int *sizep, int *vmalloced, int nulls)
1066 {
1067         struct hlist_nulls_head *hash;
1068         unsigned int nr_slots, i;
1069         size_t sz;
1070
1071         *vmalloced = 0;
1072
1073         BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
1074         nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
1075         sz = nr_slots * sizeof(struct hlist_nulls_head);
1076         hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1077                                         get_order(sz));
1078         if (!hash) {
1079                 *vmalloced = 1;
1080                 printk(KERN_WARNING "nf_conntrack: falling back to vmalloc.\n");
1081                 hash = __vmalloc(sz, GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL);
1082         }
1083
1084         if (hash && nulls)
1085                 for (i = 0; i < nr_slots; i++)
1086                         INIT_HLIST_NULLS_HEAD(&hash[i], i);
1087
1088         return hash;
1089 }
1090 EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
1091
1092 int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
1093 {
1094         int i, bucket, vmalloced, old_vmalloced;
1095         unsigned int hashsize, old_size;
1096         int rnd;
1097         struct hlist_nulls_head *hash, *old_hash;
1098         struct nf_conntrack_tuple_hash *h;
1099
1100         /* On boot, we can set this without any fancy locking. */
1101         if (!nf_conntrack_htable_size)
1102                 return param_set_uint(val, kp);
1103
1104         hashsize = simple_strtoul(val, NULL, 0);
1105         if (!hashsize)
1106                 return -EINVAL;
1107
1108         hash = nf_ct_alloc_hashtable(&hashsize, &vmalloced, 1);
1109         if (!hash)
1110                 return -ENOMEM;
1111
1112         /* We have to rehahs for the new table anyway, so we also can
1113          * use a newrandom seed */
1114         get_random_bytes(&rnd, sizeof(rnd));
1115
1116         /* Lookups in the old hash might happen in parallel, which means we
1117          * might get false negatives during connection lookup. New connections
1118          * created because of a false negative won't make it into the hash
1119          * though since that required taking the lock.
1120          */
1121         spin_lock_bh(&nf_conntrack_lock);
1122         for (i = 0; i < nf_conntrack_htable_size; i++) {
1123                 while (!hlist_nulls_empty(&init_net.ct.hash[i])) {
1124                         h = hlist_nulls_entry(init_net.ct.hash[i].first,
1125                                         struct nf_conntrack_tuple_hash, hnnode);
1126                         hlist_nulls_del_rcu(&h->hnnode);
1127                         bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
1128                         hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
1129                 }
1130         }
1131         old_size = nf_conntrack_htable_size;
1132         old_vmalloced = init_net.ct.hash_vmalloc;
1133         old_hash = init_net.ct.hash;
1134
1135         nf_conntrack_htable_size = hashsize;
1136         init_net.ct.hash_vmalloc = vmalloced;
1137         init_net.ct.hash = hash;
1138         nf_conntrack_hash_rnd = rnd;
1139         spin_unlock_bh(&nf_conntrack_lock);
1140
1141         nf_ct_free_hashtable(old_hash, old_vmalloced, old_size);
1142         return 0;
1143 }
1144 EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
1145
1146 module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
1147                   &nf_conntrack_htable_size, 0600);
1148
1149 static int nf_conntrack_init_init_net(void)
1150 {
1151         int max_factor = 8;
1152         int ret;
1153
1154         /* Idea from tcp.c: use 1/16384 of memory.  On i386: 32MB
1155          * machine has 512 buckets. >= 1GB machines have 16384 buckets. */
1156         if (!nf_conntrack_htable_size) {
1157                 nf_conntrack_htable_size
1158                         = (((num_physpages << PAGE_SHIFT) / 16384)
1159                            / sizeof(struct hlist_head));
1160                 if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
1161                         nf_conntrack_htable_size = 16384;
1162                 if (nf_conntrack_htable_size < 32)
1163                         nf_conntrack_htable_size = 32;
1164
1165                 /* Use a max. factor of four by default to get the same max as
1166                  * with the old struct list_heads. When a table size is given
1167                  * we use the old value of 8 to avoid reducing the max.
1168                  * entries. */
1169                 max_factor = 4;
1170         }
1171         nf_conntrack_max = max_factor * nf_conntrack_htable_size;
1172
1173         printk("nf_conntrack version %s (%u buckets, %d max)\n",
1174                NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1175                nf_conntrack_max);
1176
1177         nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
1178                                                 sizeof(struct nf_conn),
1179                                                 0, SLAB_DESTROY_BY_RCU, NULL);
1180         if (!nf_conntrack_cachep) {
1181                 printk(KERN_ERR "Unable to create nf_conn slab cache\n");
1182                 ret = -ENOMEM;
1183                 goto err_cache;
1184         }
1185
1186         ret = nf_conntrack_proto_init();
1187         if (ret < 0)
1188                 goto err_proto;
1189
1190         ret = nf_conntrack_helper_init();
1191         if (ret < 0)
1192                 goto err_helper;
1193
1194         return 0;
1195
1196 err_helper:
1197         nf_conntrack_proto_fini();
1198 err_proto:
1199         kmem_cache_destroy(nf_conntrack_cachep);
1200 err_cache:
1201         return ret;
1202 }
1203
1204 static int nf_conntrack_init_net(struct net *net)
1205 {
1206         int ret;
1207
1208         atomic_set(&net->ct.count, 0);
1209         INIT_HLIST_NULLS_HEAD(&net->ct.unconfirmed, 0);
1210         net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
1211         if (!net->ct.stat) {
1212                 ret = -ENOMEM;
1213                 goto err_stat;
1214         }
1215         net->ct.hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size,
1216                                              &net->ct.hash_vmalloc, 1);
1217         if (!net->ct.hash) {
1218                 ret = -ENOMEM;
1219                 printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
1220                 goto err_hash;
1221         }
1222         ret = nf_conntrack_expect_init(net);
1223         if (ret < 0)
1224                 goto err_expect;
1225         ret = nf_conntrack_acct_init(net);
1226         if (ret < 0)
1227                 goto err_acct;
1228         ret = nf_conntrack_ecache_init(net);
1229         if (ret < 0)
1230                 goto err_ecache;
1231
1232         /* Set up fake conntrack:
1233             - to never be deleted, not in any hashes */
1234 #ifdef CONFIG_NET_NS
1235         nf_conntrack_untracked.ct_net = &init_net;
1236 #endif
1237         atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
1238         /*  - and look it like as a confirmed connection */
1239         set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
1240
1241         return 0;
1242
1243 err_ecache:
1244         nf_conntrack_acct_fini(net);
1245 err_acct:
1246         nf_conntrack_expect_fini(net);
1247 err_expect:
1248         nf_ct_free_hashtable(net->ct.hash, net->ct.hash_vmalloc,
1249                              nf_conntrack_htable_size);
1250 err_hash:
1251         free_percpu(net->ct.stat);
1252 err_stat:
1253         return ret;
1254 }
1255
1256 int nf_conntrack_init(struct net *net)
1257 {
1258         int ret;
1259
1260         if (net_eq(net, &init_net)) {
1261                 ret = nf_conntrack_init_init_net();
1262                 if (ret < 0)
1263                         goto out_init_net;
1264         }
1265         ret = nf_conntrack_init_net(net);
1266         if (ret < 0)
1267                 goto out_net;
1268
1269         if (net_eq(net, &init_net)) {
1270                 /* For use by REJECT target */
1271                 rcu_assign_pointer(ip_ct_attach, nf_conntrack_attach);
1272                 rcu_assign_pointer(nf_ct_destroy, destroy_conntrack);
1273         }
1274         return 0;
1275
1276 out_net:
1277         if (net_eq(net, &init_net))
1278                 nf_conntrack_cleanup_init_net();
1279 out_init_net:
1280         return ret;
1281 }