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