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
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>
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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/siphash.h>
29 #include <linux/err.h>
30 #include <linux/percpu.h>
31 #include <linux/moduleparam.h>
32 #include <linux/notifier.h>
33 #include <linux/kernel.h>
34 #include <linux/netdevice.h>
35 #include <linux/socket.h>
37 #include <linux/nsproxy.h>
38 #include <linux/rculist_nulls.h>
40 #include <net/netfilter/nf_conntrack.h>
41 #include <net/netfilter/nf_conntrack_l3proto.h>
42 #include <net/netfilter/nf_conntrack_l4proto.h>
43 #include <net/netfilter/nf_conntrack_expect.h>
44 #include <net/netfilter/nf_conntrack_helper.h>
45 #include <net/netfilter/nf_conntrack_seqadj.h>
46 #include <net/netfilter/nf_conntrack_core.h>
47 #include <net/netfilter/nf_conntrack_extend.h>
48 #include <net/netfilter/nf_conntrack_acct.h>
49 #include <net/netfilter/nf_conntrack_ecache.h>
50 #include <net/netfilter/nf_conntrack_zones.h>
51 #include <net/netfilter/nf_conntrack_timestamp.h>
52 #include <net/netfilter/nf_conntrack_timeout.h>
53 #include <net/netfilter/nf_conntrack_labels.h>
54 #include <net/netfilter/nf_conntrack_synproxy.h>
55 #include <net/netfilter/nf_nat.h>
56 #include <net/netfilter/nf_nat_core.h>
57 #include <net/netfilter/nf_nat_helper.h>
58 #include <net/netns/hash.h>
60 #define NF_CONNTRACK_VERSION "0.5.0"
62 int (*nfnetlink_parse_nat_setup_hook)(struct nf_conn *ct,
63 enum nf_nat_manip_type manip,
64 const struct nlattr *attr) __read_mostly;
65 EXPORT_SYMBOL_GPL(nfnetlink_parse_nat_setup_hook);
67 __cacheline_aligned_in_smp spinlock_t nf_conntrack_locks[CONNTRACK_LOCKS];
68 EXPORT_SYMBOL_GPL(nf_conntrack_locks);
70 __cacheline_aligned_in_smp DEFINE_SPINLOCK(nf_conntrack_expect_lock);
71 EXPORT_SYMBOL_GPL(nf_conntrack_expect_lock);
73 struct hlist_nulls_head *nf_conntrack_hash __read_mostly;
74 EXPORT_SYMBOL_GPL(nf_conntrack_hash);
76 struct conntrack_gc_work {
77 struct delayed_work dwork;
83 static __read_mostly struct kmem_cache *nf_conntrack_cachep;
84 static __read_mostly spinlock_t nf_conntrack_locks_all_lock;
85 static __read_mostly DEFINE_SPINLOCK(nf_conntrack_locks_all_lock);
86 static __read_mostly bool nf_conntrack_locks_all;
88 /* every gc cycle scans at most 1/GC_MAX_BUCKETS_DIV part of table */
89 #define GC_MAX_BUCKETS_DIV 128u
90 /* upper bound of full table scan */
91 #define GC_MAX_SCAN_JIFFIES (16u * HZ)
92 /* desired ratio of entries found to be expired */
93 #define GC_EVICT_RATIO 50u
95 static struct conntrack_gc_work conntrack_gc_work;
97 void nf_conntrack_lock(spinlock_t *lock) __acquires(lock)
99 /* 1) Acquire the lock */
102 /* 2) read nf_conntrack_locks_all, with ACQUIRE semantics
103 * It pairs with the smp_store_release() in nf_conntrack_all_unlock()
105 if (likely(smp_load_acquire(&nf_conntrack_locks_all) == false))
108 /* fast path failed, unlock */
111 /* Slow path 1) get global lock */
112 spin_lock(&nf_conntrack_locks_all_lock);
114 /* Slow path 2) get the lock we want */
117 /* Slow path 3) release the global lock */
118 spin_unlock(&nf_conntrack_locks_all_lock);
120 EXPORT_SYMBOL_GPL(nf_conntrack_lock);
122 static void nf_conntrack_double_unlock(unsigned int h1, unsigned int h2)
124 h1 %= CONNTRACK_LOCKS;
125 h2 %= CONNTRACK_LOCKS;
126 spin_unlock(&nf_conntrack_locks[h1]);
128 spin_unlock(&nf_conntrack_locks[h2]);
131 /* return true if we need to recompute hashes (in case hash table was resized) */
132 static bool nf_conntrack_double_lock(struct net *net, unsigned int h1,
133 unsigned int h2, unsigned int sequence)
135 h1 %= CONNTRACK_LOCKS;
136 h2 %= CONNTRACK_LOCKS;
138 nf_conntrack_lock(&nf_conntrack_locks[h1]);
140 spin_lock_nested(&nf_conntrack_locks[h2],
141 SINGLE_DEPTH_NESTING);
143 nf_conntrack_lock(&nf_conntrack_locks[h2]);
144 spin_lock_nested(&nf_conntrack_locks[h1],
145 SINGLE_DEPTH_NESTING);
147 if (read_seqcount_retry(&nf_conntrack_generation, sequence)) {
148 nf_conntrack_double_unlock(h1, h2);
154 static void nf_conntrack_all_lock(void)
158 spin_lock(&nf_conntrack_locks_all_lock);
160 nf_conntrack_locks_all = true;
162 for (i = 0; i < CONNTRACK_LOCKS; i++) {
163 spin_lock(&nf_conntrack_locks[i]);
165 /* This spin_unlock provides the "release" to ensure that
166 * nf_conntrack_locks_all==true is visible to everyone that
167 * acquired spin_lock(&nf_conntrack_locks[]).
169 spin_unlock(&nf_conntrack_locks[i]);
173 static void nf_conntrack_all_unlock(void)
175 /* All prior stores must be complete before we clear
176 * 'nf_conntrack_locks_all'. Otherwise nf_conntrack_lock()
177 * might observe the false value but not the entire
179 * It pairs with the smp_load_acquire() in nf_conntrack_lock()
181 smp_store_release(&nf_conntrack_locks_all, false);
182 spin_unlock(&nf_conntrack_locks_all_lock);
185 unsigned int nf_conntrack_htable_size __read_mostly;
186 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
188 unsigned int nf_conntrack_max __read_mostly;
189 seqcount_t nf_conntrack_generation __read_mostly;
191 DEFINE_PER_CPU(struct nf_conn, nf_conntrack_untracked);
192 EXPORT_PER_CPU_SYMBOL(nf_conntrack_untracked);
194 static unsigned int nf_conntrack_hash_rnd __read_mostly;
196 static u32 hash_conntrack_raw(const struct nf_conntrack_tuple *tuple,
197 const struct net *net)
202 get_random_once(&nf_conntrack_hash_rnd, sizeof(nf_conntrack_hash_rnd));
204 /* The direction must be ignored, so we hash everything up to the
205 * destination ports (which is a multiple of 4) and treat the last
206 * three bytes manually.
208 seed = nf_conntrack_hash_rnd ^ net_hash_mix(net);
209 n = (sizeof(tuple->src) + sizeof(tuple->dst.u3)) / sizeof(u32);
210 return jhash2((u32 *)tuple, n, seed ^
211 (((__force __u16)tuple->dst.u.all << 16) |
212 tuple->dst.protonum));
215 static u32 scale_hash(u32 hash)
217 return reciprocal_scale(hash, nf_conntrack_htable_size);
220 static u32 __hash_conntrack(const struct net *net,
221 const struct nf_conntrack_tuple *tuple,
224 return reciprocal_scale(hash_conntrack_raw(tuple, net), size);
227 static u32 hash_conntrack(const struct net *net,
228 const struct nf_conntrack_tuple *tuple)
230 return scale_hash(hash_conntrack_raw(tuple, net));
234 nf_ct_get_tuple(const struct sk_buff *skb,
236 unsigned int dataoff,
240 struct nf_conntrack_tuple *tuple,
241 const struct nf_conntrack_l3proto *l3proto,
242 const struct nf_conntrack_l4proto *l4proto)
244 memset(tuple, 0, sizeof(*tuple));
246 tuple->src.l3num = l3num;
247 if (l3proto->pkt_to_tuple(skb, nhoff, tuple) == 0)
250 tuple->dst.protonum = protonum;
251 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
253 return l4proto->pkt_to_tuple(skb, dataoff, net, tuple);
255 EXPORT_SYMBOL_GPL(nf_ct_get_tuple);
257 bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
259 struct net *net, struct nf_conntrack_tuple *tuple)
261 struct nf_conntrack_l3proto *l3proto;
262 struct nf_conntrack_l4proto *l4proto;
263 unsigned int protoff;
269 l3proto = __nf_ct_l3proto_find(l3num);
270 ret = l3proto->get_l4proto(skb, nhoff, &protoff, &protonum);
271 if (ret != NF_ACCEPT) {
276 l4proto = __nf_ct_l4proto_find(l3num, protonum);
278 ret = nf_ct_get_tuple(skb, nhoff, protoff, l3num, protonum, net, tuple,
284 EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
287 nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
288 const struct nf_conntrack_tuple *orig,
289 const struct nf_conntrack_l3proto *l3proto,
290 const struct nf_conntrack_l4proto *l4proto)
292 memset(inverse, 0, sizeof(*inverse));
294 inverse->src.l3num = orig->src.l3num;
295 if (l3proto->invert_tuple(inverse, orig) == 0)
298 inverse->dst.dir = !orig->dst.dir;
300 inverse->dst.protonum = orig->dst.protonum;
301 return l4proto->invert_tuple(inverse, orig);
303 EXPORT_SYMBOL_GPL(nf_ct_invert_tuple);
305 /* Generate a almost-unique pseudo-id for a given conntrack.
307 * intentionally doesn't re-use any of the seeds used for hash
308 * table location, we assume id gets exposed to userspace.
310 * Following nf_conn items do not change throughout lifetime
314 * 2. nf_conn->master address (normally NULL)
315 * 3. the associated net namespace
316 * 4. the original direction tuple
318 u32 nf_ct_get_id(const struct nf_conn *ct)
320 static __read_mostly siphash_key_t ct_id_seed;
321 unsigned long a, b, c, d;
323 net_get_random_once(&ct_id_seed, sizeof(ct_id_seed));
325 a = (unsigned long)ct;
326 b = (unsigned long)ct->master;
327 c = (unsigned long)nf_ct_net(ct);
328 d = (unsigned long)siphash(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
329 sizeof(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple),
332 return siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &ct_id_seed);
334 return siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &ct_id_seed);
337 EXPORT_SYMBOL_GPL(nf_ct_get_id);
340 clean_from_lists(struct nf_conn *ct)
342 pr_debug("clean_from_lists(%p)\n", ct);
343 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
344 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode);
346 /* Destroy all pending expectations */
347 nf_ct_remove_expectations(ct);
350 /* must be called with local_bh_disable */
351 static void nf_ct_add_to_dying_list(struct nf_conn *ct)
353 struct ct_pcpu *pcpu;
355 /* add this conntrack to the (per cpu) dying list */
356 ct->cpu = smp_processor_id();
357 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
359 spin_lock(&pcpu->lock);
360 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
362 spin_unlock(&pcpu->lock);
365 /* must be called with local_bh_disable */
366 static void nf_ct_add_to_unconfirmed_list(struct nf_conn *ct)
368 struct ct_pcpu *pcpu;
370 /* add this conntrack to the (per cpu) unconfirmed list */
371 ct->cpu = smp_processor_id();
372 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
374 spin_lock(&pcpu->lock);
375 hlist_nulls_add_head(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
377 spin_unlock(&pcpu->lock);
380 /* must be called with local_bh_disable */
381 static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct)
383 struct ct_pcpu *pcpu;
385 /* We overload first tuple to link into unconfirmed or dying list.*/
386 pcpu = per_cpu_ptr(nf_ct_net(ct)->ct.pcpu_lists, ct->cpu);
388 spin_lock(&pcpu->lock);
389 BUG_ON(hlist_nulls_unhashed(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode));
390 hlist_nulls_del_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode);
391 spin_unlock(&pcpu->lock);
394 /* Released via destroy_conntrack() */
395 struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
396 const struct nf_conntrack_zone *zone,
399 struct nf_conn *tmpl;
401 tmpl = kzalloc(sizeof(*tmpl), flags);
405 tmpl->status = IPS_TEMPLATE;
406 write_pnet(&tmpl->ct_net, net);
407 nf_ct_zone_add(tmpl, zone);
408 atomic_set(&tmpl->ct_general.use, 0);
412 EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc);
414 void nf_ct_tmpl_free(struct nf_conn *tmpl)
416 nf_ct_ext_destroy(tmpl);
417 nf_ct_ext_free(tmpl);
420 EXPORT_SYMBOL_GPL(nf_ct_tmpl_free);
423 destroy_conntrack(struct nf_conntrack *nfct)
425 struct nf_conn *ct = (struct nf_conn *)nfct;
426 struct nf_conntrack_l4proto *l4proto;
428 pr_debug("destroy_conntrack(%p)\n", ct);
429 NF_CT_ASSERT(atomic_read(&nfct->use) == 0);
431 if (unlikely(nf_ct_is_template(ct))) {
436 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
437 if (l4proto->destroy)
438 l4proto->destroy(ct);
443 /* Expectations will have been removed in clean_from_lists,
444 * except TFTP can create an expectation on the first packet,
445 * before connection is in the list, so we need to clean here,
448 nf_ct_remove_expectations(ct);
450 nf_ct_del_from_dying_or_unconfirmed_list(ct);
455 nf_ct_put(ct->master);
457 pr_debug("destroy_conntrack: returning ct=%p to slab\n", ct);
458 nf_conntrack_free(ct);
461 static void nf_ct_delete_from_lists(struct nf_conn *ct)
463 struct net *net = nf_ct_net(ct);
464 unsigned int hash, reply_hash;
465 unsigned int sequence;
467 nf_ct_helper_destroy(ct);
471 sequence = read_seqcount_begin(&nf_conntrack_generation);
472 hash = hash_conntrack(net,
473 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
474 reply_hash = hash_conntrack(net,
475 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
476 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
478 clean_from_lists(ct);
479 nf_conntrack_double_unlock(hash, reply_hash);
481 nf_ct_add_to_dying_list(ct);
486 bool nf_ct_delete(struct nf_conn *ct, u32 portid, int report)
488 struct nf_conn_tstamp *tstamp;
490 if (test_and_set_bit(IPS_DYING_BIT, &ct->status))
493 tstamp = nf_conn_tstamp_find(ct);
495 s32 timeout = ct->timeout - nfct_time_stamp;
497 tstamp->stop = ktime_get_real_ns();
499 tstamp->stop -= jiffies_to_nsecs(-timeout);
502 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
503 portid, report) < 0) {
504 /* destroy event was not delivered. nf_ct_put will
505 * be done by event cache worker on redelivery.
507 nf_ct_delete_from_lists(ct);
508 nf_conntrack_ecache_delayed_work(nf_ct_net(ct));
512 nf_conntrack_ecache_work(nf_ct_net(ct));
513 nf_ct_delete_from_lists(ct);
517 EXPORT_SYMBOL_GPL(nf_ct_delete);
520 nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
521 const struct nf_conntrack_tuple *tuple,
522 const struct nf_conntrack_zone *zone,
523 const struct net *net)
525 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
527 /* A conntrack can be recreated with the equal tuple,
528 * so we need to check that the conntrack is confirmed
530 return nf_ct_tuple_equal(tuple, &h->tuple) &&
531 nf_ct_zone_equal(ct, zone, NF_CT_DIRECTION(h)) &&
532 nf_ct_is_confirmed(ct) &&
533 net_eq(net, nf_ct_net(ct));
536 /* caller must hold rcu readlock and none of the nf_conntrack_locks */
537 static void nf_ct_gc_expired(struct nf_conn *ct)
539 if (!atomic_inc_not_zero(&ct->ct_general.use))
542 if (nf_ct_should_gc(ct))
550 * - Caller must take a reference on returned object
551 * and recheck nf_ct_tuple_equal(tuple, &h->tuple)
553 static struct nf_conntrack_tuple_hash *
554 ____nf_conntrack_find(struct net *net, const struct nf_conntrack_zone *zone,
555 const struct nf_conntrack_tuple *tuple, u32 hash)
557 struct nf_conntrack_tuple_hash *h;
558 struct hlist_nulls_head *ct_hash;
559 struct hlist_nulls_node *n;
560 unsigned int bucket, hsize;
563 nf_conntrack_get_ht(&ct_hash, &hsize);
564 bucket = reciprocal_scale(hash, hsize);
566 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[bucket], hnnode) {
569 ct = nf_ct_tuplehash_to_ctrack(h);
570 if (nf_ct_is_expired(ct)) {
571 nf_ct_gc_expired(ct);
575 if (nf_ct_is_dying(ct))
578 if (nf_ct_key_equal(h, tuple, zone, net))
582 * if the nulls value we got at the end of this lookup is
583 * not the expected one, we must restart lookup.
584 * We probably met an item that was moved to another chain.
586 if (get_nulls_value(n) != bucket) {
587 NF_CT_STAT_INC_ATOMIC(net, search_restart);
594 /* Find a connection corresponding to a tuple. */
595 static struct nf_conntrack_tuple_hash *
596 __nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
597 const struct nf_conntrack_tuple *tuple, u32 hash)
599 struct nf_conntrack_tuple_hash *h;
604 h = ____nf_conntrack_find(net, zone, tuple, hash);
606 ct = nf_ct_tuplehash_to_ctrack(h);
607 if (unlikely(nf_ct_is_dying(ct) ||
608 !atomic_inc_not_zero(&ct->ct_general.use)))
611 if (unlikely(!nf_ct_key_equal(h, tuple, zone, net))) {
622 struct nf_conntrack_tuple_hash *
623 nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,
624 const struct nf_conntrack_tuple *tuple)
626 return __nf_conntrack_find_get(net, zone, tuple,
627 hash_conntrack_raw(tuple, net));
629 EXPORT_SYMBOL_GPL(nf_conntrack_find_get);
631 static void __nf_conntrack_hash_insert(struct nf_conn *ct,
633 unsigned int reply_hash)
635 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode,
636 &nf_conntrack_hash[hash]);
637 hlist_nulls_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode,
638 &nf_conntrack_hash[reply_hash]);
642 nf_conntrack_hash_check_insert(struct nf_conn *ct)
644 const struct nf_conntrack_zone *zone;
645 struct net *net = nf_ct_net(ct);
646 unsigned int hash, reply_hash;
647 struct nf_conntrack_tuple_hash *h;
648 struct hlist_nulls_node *n;
649 unsigned int sequence;
651 zone = nf_ct_zone(ct);
655 sequence = read_seqcount_begin(&nf_conntrack_generation);
656 hash = hash_conntrack(net,
657 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
658 reply_hash = hash_conntrack(net,
659 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
660 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
662 /* See if there's one in the list already, including reverse */
663 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
664 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
668 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
669 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
674 /* The caller holds a reference to this object */
675 atomic_set(&ct->ct_general.use, 2);
676 __nf_conntrack_hash_insert(ct, hash, reply_hash);
677 nf_conntrack_double_unlock(hash, reply_hash);
678 NF_CT_STAT_INC(net, insert);
683 nf_conntrack_double_unlock(hash, reply_hash);
684 NF_CT_STAT_INC(net, insert_failed);
688 EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert);
690 static inline void nf_ct_acct_update(struct nf_conn *ct,
691 enum ip_conntrack_info ctinfo,
694 struct nf_conn_acct *acct;
696 acct = nf_conn_acct_find(ct);
698 struct nf_conn_counter *counter = acct->counter;
700 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
701 atomic64_add(len, &counter[CTINFO2DIR(ctinfo)].bytes);
705 static void nf_ct_acct_merge(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
706 const struct nf_conn *loser_ct)
708 struct nf_conn_acct *acct;
710 acct = nf_conn_acct_find(loser_ct);
712 struct nf_conn_counter *counter = acct->counter;
715 /* u32 should be fine since we must have seen one packet. */
716 bytes = atomic64_read(&counter[CTINFO2DIR(ctinfo)].bytes);
717 nf_ct_acct_update(ct, ctinfo, bytes);
721 /* Resolve race on insertion if this protocol allows this. */
722 static int nf_ct_resolve_clash(struct net *net, struct sk_buff *skb,
723 enum ip_conntrack_info ctinfo,
724 struct nf_conntrack_tuple_hash *h)
726 /* This is the conntrack entry already in hashes that won race. */
727 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
728 struct nf_conntrack_l4proto *l4proto;
730 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
731 if (l4proto->allow_clash &&
732 ((ct->status & IPS_NAT_DONE_MASK) == 0) &&
733 !nf_ct_is_dying(ct) &&
734 atomic_inc_not_zero(&ct->ct_general.use)) {
735 nf_ct_acct_merge(ct, ctinfo, (struct nf_conn *)skb->nfct);
736 nf_conntrack_put(skb->nfct);
737 /* Assign conntrack already in hashes to this skbuff. Don't
738 * modify skb->nfctinfo to ensure consistent stateful filtering.
740 skb->nfct = &ct->ct_general;
743 NF_CT_STAT_INC(net, drop);
747 /* Confirm a connection given skb; places it in hash table */
749 __nf_conntrack_confirm(struct sk_buff *skb)
751 const struct nf_conntrack_zone *zone;
752 unsigned int hash, reply_hash;
753 struct nf_conntrack_tuple_hash *h;
755 struct nf_conn_help *help;
756 struct nf_conn_tstamp *tstamp;
757 struct hlist_nulls_node *n;
758 enum ip_conntrack_info ctinfo;
760 unsigned int sequence;
763 ct = nf_ct_get(skb, &ctinfo);
766 /* ipt_REJECT uses nf_conntrack_attach to attach related
767 ICMP/TCP RST packets in other direction. Actual packet
768 which created connection will be IP_CT_NEW or for an
769 expected connection, IP_CT_RELATED. */
770 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
773 zone = nf_ct_zone(ct);
777 sequence = read_seqcount_begin(&nf_conntrack_generation);
778 /* reuse the hash saved before */
779 hash = *(unsigned long *)&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev;
780 hash = scale_hash(hash);
781 reply_hash = hash_conntrack(net,
782 &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
784 } while (nf_conntrack_double_lock(net, hash, reply_hash, sequence));
786 /* We're not in hash table, and we refuse to set up related
787 * connections for unconfirmed conns. But packet copies and
788 * REJECT will give spurious warnings here.
790 /* NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
792 /* No external references means no one else could have
795 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
796 pr_debug("Confirming conntrack %p\n", ct);
797 /* We have to check the DYING flag after unlink to prevent
798 * a race against nf_ct_get_next_corpse() possibly called from
799 * user context, else we insert an already 'dead' hash, blocking
800 * further use of that particular connection -JM.
802 nf_ct_del_from_dying_or_unconfirmed_list(ct);
804 if (unlikely(nf_ct_is_dying(ct))) {
805 nf_ct_add_to_dying_list(ct);
809 /* See if there's one in the list already, including reverse:
810 NAT could have grabbed it without realizing, since we're
811 not in the hash. If there is, we lost race. */
812 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[hash], hnnode)
813 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
817 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[reply_hash], hnnode)
818 if (nf_ct_key_equal(h, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
822 /* Timer relative to confirmation time, not original
823 setting time, otherwise we'd get timer wrap in
824 weird delay cases. */
825 ct->timeout += nfct_time_stamp;
826 atomic_inc(&ct->ct_general.use);
827 ct->status |= IPS_CONFIRMED;
829 /* set conntrack timestamp, if enabled. */
830 tstamp = nf_conn_tstamp_find(ct);
832 if (skb->tstamp.tv64 == 0)
833 __net_timestamp(skb);
835 tstamp->start = ktime_to_ns(skb->tstamp);
837 /* Since the lookup is lockless, hash insertion must be done after
838 * starting the timer and setting the CONFIRMED bit. The RCU barriers
839 * guarantee that no other CPU can find the conntrack before the above
840 * stores are visible.
842 __nf_conntrack_hash_insert(ct, hash, reply_hash);
843 nf_conntrack_double_unlock(hash, reply_hash);
846 help = nfct_help(ct);
847 if (help && help->helper)
848 nf_conntrack_event_cache(IPCT_HELPER, ct);
850 nf_conntrack_event_cache(master_ct(ct) ?
851 IPCT_RELATED : IPCT_NEW, ct);
855 nf_ct_add_to_dying_list(ct);
856 ret = nf_ct_resolve_clash(net, skb, ctinfo, h);
858 nf_conntrack_double_unlock(hash, reply_hash);
859 NF_CT_STAT_INC(net, insert_failed);
863 EXPORT_SYMBOL_GPL(__nf_conntrack_confirm);
865 /* Returns true if a connection correspondings to the tuple (required
868 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
869 const struct nf_conn *ignored_conntrack)
871 struct net *net = nf_ct_net(ignored_conntrack);
872 const struct nf_conntrack_zone *zone;
873 struct nf_conntrack_tuple_hash *h;
874 struct hlist_nulls_head *ct_hash;
875 unsigned int hash, hsize;
876 struct hlist_nulls_node *n;
879 zone = nf_ct_zone(ignored_conntrack);
883 nf_conntrack_get_ht(&ct_hash, &hsize);
884 hash = __hash_conntrack(net, tuple, hsize);
886 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[hash], hnnode) {
887 ct = nf_ct_tuplehash_to_ctrack(h);
889 if (ct == ignored_conntrack)
892 if (nf_ct_is_expired(ct)) {
893 nf_ct_gc_expired(ct);
897 if (nf_ct_key_equal(h, tuple, zone, net)) {
898 /* Tuple is taken already, so caller will need to find
899 * a new source port to use.
902 * If the *original tuples* are identical, then both
903 * conntracks refer to the same flow.
904 * This is a rare situation, it can occur e.g. when
905 * more than one UDP packet is sent from same socket
906 * in different threads.
908 * Let nf_ct_resolve_clash() deal with this later.
910 if (nf_ct_tuple_equal(&ignored_conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
911 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple) &&
912 nf_ct_zone_equal(ct, zone, IP_CT_DIR_ORIGINAL))
915 NF_CT_STAT_INC_ATOMIC(net, found);
921 if (get_nulls_value(n) != hash) {
922 NF_CT_STAT_INC_ATOMIC(net, search_restart);
930 EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
932 #define NF_CT_EVICTION_RANGE 8
934 /* There's a small race here where we may free a just-assured
935 connection. Too bad: we're in trouble anyway. */
936 static unsigned int early_drop_list(struct net *net,
937 struct hlist_nulls_head *head)
939 struct nf_conntrack_tuple_hash *h;
940 struct hlist_nulls_node *n;
941 unsigned int drops = 0;
944 hlist_nulls_for_each_entry_rcu(h, n, head, hnnode) {
945 tmp = nf_ct_tuplehash_to_ctrack(h);
947 if (nf_ct_is_expired(tmp)) {
948 nf_ct_gc_expired(tmp);
952 if (test_bit(IPS_ASSURED_BIT, &tmp->status) ||
953 !net_eq(nf_ct_net(tmp), net) ||
957 if (!atomic_inc_not_zero(&tmp->ct_general.use))
960 /* kill only if still in same netns -- might have moved due to
961 * SLAB_DESTROY_BY_RCU rules.
963 * We steal the timer reference. If that fails timer has
964 * already fired or someone else deleted it. Just drop ref
965 * and move to next entry.
967 if (net_eq(nf_ct_net(tmp), net) &&
968 nf_ct_is_confirmed(tmp) &&
969 nf_ct_delete(tmp, 0, 0))
978 static noinline int early_drop(struct net *net, unsigned int hash)
980 unsigned int i, bucket;
982 for (i = 0; i < NF_CT_EVICTION_RANGE; i++) {
983 struct hlist_nulls_head *ct_hash;
984 unsigned int hsize, drops;
987 nf_conntrack_get_ht(&ct_hash, &hsize);
989 bucket = reciprocal_scale(hash, hsize);
991 bucket = (bucket + 1) % hsize;
993 drops = early_drop_list(net, &ct_hash[bucket]);
997 NF_CT_STAT_ADD_ATOMIC(net, early_drop, drops);
1005 static void gc_worker(struct work_struct *work)
1007 unsigned int min_interval = max(HZ / GC_MAX_BUCKETS_DIV, 1u);
1008 unsigned int i, goal, buckets = 0, expired_count = 0;
1009 struct conntrack_gc_work *gc_work;
1010 unsigned int ratio, scanned = 0;
1011 unsigned long next_run;
1013 gc_work = container_of(work, struct conntrack_gc_work, dwork.work);
1015 goal = nf_conntrack_htable_size / GC_MAX_BUCKETS_DIV;
1016 i = gc_work->last_bucket;
1019 struct nf_conntrack_tuple_hash *h;
1020 struct hlist_nulls_head *ct_hash;
1021 struct hlist_nulls_node *n;
1022 unsigned int hashsz;
1023 struct nf_conn *tmp;
1028 nf_conntrack_get_ht(&ct_hash, &hashsz);
1032 hlist_nulls_for_each_entry_rcu(h, n, &ct_hash[i], hnnode) {
1033 tmp = nf_ct_tuplehash_to_ctrack(h);
1036 if (nf_ct_is_expired(tmp)) {
1037 nf_ct_gc_expired(tmp);
1043 /* could check get_nulls_value() here and restart if ct
1044 * was moved to another chain. But given gc is best-effort
1045 * we will just continue with next hash slot.
1048 cond_resched_rcu_qs();
1049 } while (++buckets < goal);
1051 if (gc_work->exiting)
1055 * Eviction will normally happen from the packet path, and not
1056 * from this gc worker.
1058 * This worker is only here to reap expired entries when system went
1059 * idle after a busy period.
1061 * The heuristics below are supposed to balance conflicting goals:
1063 * 1. Minimize time until we notice a stale entry
1064 * 2. Maximize scan intervals to not waste cycles
1066 * Normally, expire ratio will be close to 0.
1068 * As soon as a sizeable fraction of the entries have expired
1069 * increase scan frequency.
1071 ratio = scanned ? expired_count * 100 / scanned : 0;
1072 if (ratio > GC_EVICT_RATIO) {
1073 gc_work->next_gc_run = min_interval;
1075 unsigned int max = GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV;
1077 BUILD_BUG_ON((GC_MAX_SCAN_JIFFIES / GC_MAX_BUCKETS_DIV) == 0);
1079 gc_work->next_gc_run += min_interval;
1080 if (gc_work->next_gc_run > max)
1081 gc_work->next_gc_run = max;
1084 next_run = gc_work->next_gc_run;
1085 gc_work->last_bucket = i;
1086 queue_delayed_work(system_long_wq, &gc_work->dwork, next_run);
1089 static void conntrack_gc_work_init(struct conntrack_gc_work *gc_work)
1091 INIT_DELAYED_WORK(&gc_work->dwork, gc_worker);
1092 gc_work->next_gc_run = HZ;
1093 gc_work->exiting = false;
1096 static struct nf_conn *
1097 __nf_conntrack_alloc(struct net *net,
1098 const struct nf_conntrack_zone *zone,
1099 const struct nf_conntrack_tuple *orig,
1100 const struct nf_conntrack_tuple *repl,
1101 gfp_t gfp, u32 hash)
1105 /* We don't want any race condition at early drop stage */
1106 atomic_inc(&net->ct.count);
1108 if (nf_conntrack_max &&
1109 unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
1110 if (!early_drop(net, hash)) {
1111 atomic_dec(&net->ct.count);
1112 net_warn_ratelimited("nf_conntrack: table full, dropping packet\n");
1113 return ERR_PTR(-ENOMEM);
1118 * Do not use kmem_cache_zalloc(), as this cache uses
1119 * SLAB_DESTROY_BY_RCU.
1121 ct = kmem_cache_alloc(nf_conntrack_cachep, gfp);
1125 spin_lock_init(&ct->lock);
1126 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *orig;
1127 ct->tuplehash[IP_CT_DIR_ORIGINAL].hnnode.pprev = NULL;
1128 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
1129 /* save hash for reusing when confirming */
1130 *(unsigned long *)(&ct->tuplehash[IP_CT_DIR_REPLY].hnnode.pprev) = hash;
1132 write_pnet(&ct->ct_net, net);
1133 memset(&ct->__nfct_init_offset, 0,
1134 offsetof(struct nf_conn, proto) -
1135 offsetof(struct nf_conn, __nfct_init_offset));
1137 nf_ct_zone_add(ct, zone);
1139 /* Because we use RCU lookups, we set ct_general.use to zero before
1140 * this is inserted in any list.
1142 atomic_set(&ct->ct_general.use, 0);
1145 atomic_dec(&net->ct.count);
1146 return ERR_PTR(-ENOMEM);
1149 struct nf_conn *nf_conntrack_alloc(struct net *net,
1150 const struct nf_conntrack_zone *zone,
1151 const struct nf_conntrack_tuple *orig,
1152 const struct nf_conntrack_tuple *repl,
1155 return __nf_conntrack_alloc(net, zone, orig, repl, gfp, 0);
1157 EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
1159 void nf_conntrack_free(struct nf_conn *ct)
1161 struct net *net = nf_ct_net(ct);
1163 /* A freed object has refcnt == 0, that's
1164 * the golden rule for SLAB_DESTROY_BY_RCU
1166 NF_CT_ASSERT(atomic_read(&ct->ct_general.use) == 0);
1168 nf_ct_ext_destroy(ct);
1170 kmem_cache_free(nf_conntrack_cachep, ct);
1171 smp_mb__before_atomic();
1172 atomic_dec(&net->ct.count);
1174 EXPORT_SYMBOL_GPL(nf_conntrack_free);
1177 /* Allocate a new conntrack: we return -ENOMEM if classification
1178 failed due to stress. Otherwise it really is unclassifiable. */
1179 static struct nf_conntrack_tuple_hash *
1180 init_conntrack(struct net *net, struct nf_conn *tmpl,
1181 const struct nf_conntrack_tuple *tuple,
1182 struct nf_conntrack_l3proto *l3proto,
1183 struct nf_conntrack_l4proto *l4proto,
1184 struct sk_buff *skb,
1185 unsigned int dataoff, u32 hash)
1188 struct nf_conn_help *help;
1189 struct nf_conntrack_tuple repl_tuple;
1190 struct nf_conntrack_ecache *ecache;
1191 struct nf_conntrack_expect *exp = NULL;
1192 const struct nf_conntrack_zone *zone;
1193 struct nf_conn_timeout *timeout_ext;
1194 struct nf_conntrack_zone tmp;
1195 unsigned int *timeouts;
1197 if (!nf_ct_invert_tuple(&repl_tuple, tuple, l3proto, l4proto)) {
1198 pr_debug("Can't invert tuple.\n");
1202 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
1203 ct = __nf_conntrack_alloc(net, zone, tuple, &repl_tuple, GFP_ATOMIC,
1206 return (struct nf_conntrack_tuple_hash *)ct;
1208 if (!nf_ct_add_synproxy(ct, tmpl)) {
1209 nf_conntrack_free(ct);
1210 return ERR_PTR(-ENOMEM);
1213 timeout_ext = tmpl ? nf_ct_timeout_find(tmpl) : NULL;
1215 timeouts = nf_ct_timeout_data(timeout_ext);
1216 if (unlikely(!timeouts))
1217 timeouts = l4proto->get_timeouts(net);
1219 timeouts = l4proto->get_timeouts(net);
1222 if (!l4proto->new(ct, skb, dataoff, timeouts)) {
1223 nf_conntrack_free(ct);
1224 pr_debug("can't track with proto module\n");
1229 nf_ct_timeout_ext_add(ct, rcu_dereference(timeout_ext->timeout),
1232 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1233 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1234 nf_ct_labels_ext_add(ct);
1236 ecache = tmpl ? nf_ct_ecache_find(tmpl) : NULL;
1237 nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
1238 ecache ? ecache->expmask : 0,
1242 if (net->ct.expect_count) {
1243 spin_lock(&nf_conntrack_expect_lock);
1244 exp = nf_ct_find_expectation(net, zone, tuple);
1246 pr_debug("expectation arrives ct=%p exp=%p\n",
1248 /* Welcome, Mr. Bond. We've been expecting you... */
1249 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1250 /* exp->master safe, refcnt bumped in nf_ct_find_expectation */
1251 ct->master = exp->master;
1253 help = nf_ct_helper_ext_add(ct, exp->helper,
1256 rcu_assign_pointer(help->helper, exp->helper);
1259 #ifdef CONFIG_NF_CONNTRACK_MARK
1260 ct->mark = exp->master->mark;
1262 #ifdef CONFIG_NF_CONNTRACK_SECMARK
1263 ct->secmark = exp->master->secmark;
1265 NF_CT_STAT_INC(net, expect_new);
1267 spin_unlock(&nf_conntrack_expect_lock);
1270 __nf_ct_try_assign_helper(ct, tmpl, GFP_ATOMIC);
1272 /* Now it is inserted into the unconfirmed list, bump refcount */
1273 nf_conntrack_get(&ct->ct_general);
1274 nf_ct_add_to_unconfirmed_list(ct);
1280 exp->expectfn(ct, exp);
1281 nf_ct_expect_put(exp);
1284 return &ct->tuplehash[IP_CT_DIR_ORIGINAL];
1287 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
1288 static inline struct nf_conn *
1289 resolve_normal_ct(struct net *net, struct nf_conn *tmpl,
1290 struct sk_buff *skb,
1291 unsigned int dataoff,
1294 struct nf_conntrack_l3proto *l3proto,
1295 struct nf_conntrack_l4proto *l4proto,
1297 enum ip_conntrack_info *ctinfo)
1299 const struct nf_conntrack_zone *zone;
1300 struct nf_conntrack_tuple tuple;
1301 struct nf_conntrack_tuple_hash *h;
1302 struct nf_conntrack_zone tmp;
1306 if (!nf_ct_get_tuple(skb, skb_network_offset(skb),
1307 dataoff, l3num, protonum, net, &tuple, l3proto,
1309 pr_debug("Can't get tuple\n");
1313 /* look for tuple match */
1314 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
1315 hash = hash_conntrack_raw(&tuple, net);
1316 h = __nf_conntrack_find_get(net, zone, &tuple, hash);
1318 h = init_conntrack(net, tmpl, &tuple, l3proto, l4proto,
1319 skb, dataoff, hash);
1325 ct = nf_ct_tuplehash_to_ctrack(h);
1327 /* It exists; we have (non-exclusive) reference. */
1328 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
1329 *ctinfo = IP_CT_ESTABLISHED_REPLY;
1330 /* Please set reply bit if this packet OK */
1333 /* Once we've had two way comms, always ESTABLISHED. */
1334 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
1335 pr_debug("normal packet for %p\n", ct);
1336 *ctinfo = IP_CT_ESTABLISHED;
1337 } else if (test_bit(IPS_EXPECTED_BIT, &ct->status)) {
1338 pr_debug("related packet for %p\n", ct);
1339 *ctinfo = IP_CT_RELATED;
1341 pr_debug("new packet for %p\n", ct);
1342 *ctinfo = IP_CT_NEW;
1346 skb->nfct = &ct->ct_general;
1347 skb->nfctinfo = *ctinfo;
1352 nf_conntrack_in(struct net *net, u_int8_t pf, unsigned int hooknum,
1353 struct sk_buff *skb)
1355 struct nf_conn *ct, *tmpl = NULL;
1356 enum ip_conntrack_info ctinfo;
1357 struct nf_conntrack_l3proto *l3proto;
1358 struct nf_conntrack_l4proto *l4proto;
1359 unsigned int *timeouts;
1360 unsigned int dataoff;
1366 /* Previously seen (loopback or untracked)? Ignore. */
1367 tmpl = (struct nf_conn *)skb->nfct;
1368 if (!nf_ct_is_template(tmpl)) {
1369 NF_CT_STAT_INC_ATOMIC(net, ignore);
1375 /* rcu_read_lock()ed by nf_hook_thresh */
1376 l3proto = __nf_ct_l3proto_find(pf);
1377 ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
1378 &dataoff, &protonum);
1380 pr_debug("not prepared to track yet or error occurred\n");
1381 NF_CT_STAT_INC_ATOMIC(net, error);
1382 NF_CT_STAT_INC_ATOMIC(net, invalid);
1387 l4proto = __nf_ct_l4proto_find(pf, protonum);
1389 /* It may be an special packet, error, unclean...
1390 * inverse of the return code tells to the netfilter
1391 * core what to do with the packet. */
1392 if (l4proto->error != NULL) {
1393 ret = l4proto->error(net, tmpl, skb, dataoff, &ctinfo,
1396 NF_CT_STAT_INC_ATOMIC(net, error);
1397 NF_CT_STAT_INC_ATOMIC(net, invalid);
1401 /* ICMP[v6] protocol trackers may assign one conntrack. */
1406 ct = resolve_normal_ct(net, tmpl, skb, dataoff, pf, protonum,
1407 l3proto, l4proto, &set_reply, &ctinfo);
1409 /* Not valid part of a connection */
1410 NF_CT_STAT_INC_ATOMIC(net, invalid);
1416 /* Too stressed to deal. */
1417 NF_CT_STAT_INC_ATOMIC(net, drop);
1422 NF_CT_ASSERT(skb->nfct);
1424 /* Decide what timeout policy we want to apply to this flow. */
1425 timeouts = nf_ct_timeout_lookup(net, ct, l4proto);
1427 ret = l4proto->packet(ct, skb, dataoff, ctinfo, pf, hooknum, timeouts);
1429 /* Invalid: inverse of the return code tells
1430 * the netfilter core what to do */
1431 pr_debug("nf_conntrack_in: Can't track with proto module\n");
1432 nf_conntrack_put(skb->nfct);
1434 NF_CT_STAT_INC_ATOMIC(net, invalid);
1435 if (ret == -NF_DROP)
1436 NF_CT_STAT_INC_ATOMIC(net, drop);
1441 if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
1442 nf_conntrack_event_cache(IPCT_REPLY, ct);
1445 /* Special case: we have to repeat this hook, assign the
1446 * template again to this packet. We assume that this packet
1447 * has no conntrack assigned. This is used by nf_ct_tcp. */
1448 if (ret == NF_REPEAT)
1449 skb->nfct = (struct nf_conntrack *)tmpl;
1456 EXPORT_SYMBOL_GPL(nf_conntrack_in);
1458 bool nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse,
1459 const struct nf_conntrack_tuple *orig)
1464 ret = nf_ct_invert_tuple(inverse, orig,
1465 __nf_ct_l3proto_find(orig->src.l3num),
1466 __nf_ct_l4proto_find(orig->src.l3num,
1467 orig->dst.protonum));
1471 EXPORT_SYMBOL_GPL(nf_ct_invert_tuplepr);
1473 /* Alter reply tuple (maybe alter helper). This is for NAT, and is
1474 implicitly racy: see __nf_conntrack_confirm */
1475 void nf_conntrack_alter_reply(struct nf_conn *ct,
1476 const struct nf_conntrack_tuple *newreply)
1478 struct nf_conn_help *help = nfct_help(ct);
1480 /* Should be unconfirmed, so not in hash table yet */
1481 NF_CT_ASSERT(!nf_ct_is_confirmed(ct));
1483 pr_debug("Altering reply tuple of %p to ", ct);
1484 nf_ct_dump_tuple(newreply);
1486 ct->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
1487 if (ct->master || (help && !hlist_empty(&help->expectations)))
1491 __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1494 EXPORT_SYMBOL_GPL(nf_conntrack_alter_reply);
1496 /* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */
1497 void __nf_ct_refresh_acct(struct nf_conn *ct,
1498 enum ip_conntrack_info ctinfo,
1499 const struct sk_buff *skb,
1500 unsigned long extra_jiffies,
1505 /* Only update if this is not a fixed timeout */
1506 if (test_bit(IPS_FIXED_TIMEOUT_BIT, &ct->status))
1509 /* If not in hash table, timer will not be active yet */
1510 if (nf_ct_is_confirmed(ct))
1511 extra_jiffies += nfct_time_stamp;
1513 ct->timeout = extra_jiffies;
1516 nf_ct_acct_update(ct, ctinfo, skb->len);
1518 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
1520 bool nf_ct_kill_acct(struct nf_conn *ct,
1521 enum ip_conntrack_info ctinfo,
1522 const struct sk_buff *skb)
1524 nf_ct_acct_update(ct, ctinfo, skb->len);
1526 return nf_ct_delete(ct, 0, 0);
1528 EXPORT_SYMBOL_GPL(nf_ct_kill_acct);
1530 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
1532 #include <linux/netfilter/nfnetlink.h>
1533 #include <linux/netfilter/nfnetlink_conntrack.h>
1534 #include <linux/mutex.h>
1536 /* Generic function for tcp/udp/sctp/dccp and alike. This needs to be
1537 * in ip_conntrack_core, since we don't want the protocols to autoload
1538 * or depend on ctnetlink */
1539 int nf_ct_port_tuple_to_nlattr(struct sk_buff *skb,
1540 const struct nf_conntrack_tuple *tuple)
1542 if (nla_put_be16(skb, CTA_PROTO_SRC_PORT, tuple->src.u.tcp.port) ||
1543 nla_put_be16(skb, CTA_PROTO_DST_PORT, tuple->dst.u.tcp.port))
1544 goto nla_put_failure;
1550 EXPORT_SYMBOL_GPL(nf_ct_port_tuple_to_nlattr);
1552 const struct nla_policy nf_ct_port_nla_policy[CTA_PROTO_MAX+1] = {
1553 [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
1554 [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
1556 EXPORT_SYMBOL_GPL(nf_ct_port_nla_policy);
1558 int nf_ct_port_nlattr_to_tuple(struct nlattr *tb[],
1559 struct nf_conntrack_tuple *t)
1561 if (!tb[CTA_PROTO_SRC_PORT] || !tb[CTA_PROTO_DST_PORT])
1564 t->src.u.tcp.port = nla_get_be16(tb[CTA_PROTO_SRC_PORT]);
1565 t->dst.u.tcp.port = nla_get_be16(tb[CTA_PROTO_DST_PORT]);
1569 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_to_tuple);
1571 int nf_ct_port_nlattr_tuple_size(void)
1573 return nla_policy_len(nf_ct_port_nla_policy, CTA_PROTO_MAX + 1);
1575 EXPORT_SYMBOL_GPL(nf_ct_port_nlattr_tuple_size);
1578 /* Used by ipt_REJECT and ip6t_REJECT. */
1579 static void nf_conntrack_attach(struct sk_buff *nskb, const struct sk_buff *skb)
1582 enum ip_conntrack_info ctinfo;
1584 /* This ICMP is in reverse direction to the packet which caused it */
1585 ct = nf_ct_get(skb, &ctinfo);
1586 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
1587 ctinfo = IP_CT_RELATED_REPLY;
1589 ctinfo = IP_CT_RELATED;
1591 /* Attach to new skbuff, and increment count */
1592 nskb->nfct = &ct->ct_general;
1593 nskb->nfctinfo = ctinfo;
1594 nf_conntrack_get(nskb->nfct);
1597 /* Bring out ya dead! */
1598 static struct nf_conn *
1599 get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
1600 void *data, unsigned int *bucket)
1602 struct nf_conntrack_tuple_hash *h;
1604 struct hlist_nulls_node *n;
1607 for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
1608 lockp = &nf_conntrack_locks[*bucket % CONNTRACK_LOCKS];
1610 nf_conntrack_lock(lockp);
1611 if (*bucket < nf_conntrack_htable_size) {
1612 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnnode) {
1613 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1615 ct = nf_ct_tuplehash_to_ctrack(h);
1616 if (net_eq(nf_ct_net(ct), net) &&
1628 atomic_inc(&ct->ct_general.use);
1635 __nf_ct_unconfirmed_destroy(struct net *net)
1639 for_each_possible_cpu(cpu) {
1640 struct nf_conntrack_tuple_hash *h;
1641 struct hlist_nulls_node *n;
1642 struct ct_pcpu *pcpu;
1644 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1646 spin_lock_bh(&pcpu->lock);
1647 hlist_nulls_for_each_entry(h, n, &pcpu->unconfirmed, hnnode) {
1650 ct = nf_ct_tuplehash_to_ctrack(h);
1652 /* we cannot call iter() on unconfirmed list, the
1653 * owning cpu can reallocate ct->ext at any time.
1655 set_bit(IPS_DYING_BIT, &ct->status);
1657 spin_unlock_bh(&pcpu->lock);
1662 void nf_ct_iterate_cleanup(struct net *net,
1663 int (*iter)(struct nf_conn *i, void *data),
1664 void *data, u32 portid, int report)
1667 unsigned int bucket = 0;
1671 if (atomic_read(&net->ct.count) == 0)
1674 __nf_ct_unconfirmed_destroy(net);
1678 while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
1679 /* Time to push up daises... */
1681 nf_ct_delete(ct, portid, report);
1686 EXPORT_SYMBOL_GPL(nf_ct_iterate_cleanup);
1688 static int kill_all(struct nf_conn *i, void *data)
1693 void nf_ct_free_hashtable(void *hash, unsigned int size)
1695 if (is_vmalloc_addr(hash))
1698 free_pages((unsigned long)hash,
1699 get_order(sizeof(struct hlist_head) * size));
1701 EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
1703 static int untrack_refs(void)
1707 for_each_possible_cpu(cpu) {
1708 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
1710 cnt += atomic_read(&ct->ct_general.use) - 1;
1715 void nf_conntrack_cleanup_start(void)
1717 conntrack_gc_work.exiting = true;
1718 RCU_INIT_POINTER(ip_ct_attach, NULL);
1721 void nf_conntrack_cleanup_end(void)
1723 RCU_INIT_POINTER(nf_ct_destroy, NULL);
1724 while (untrack_refs() > 0)
1727 cancel_delayed_work_sync(&conntrack_gc_work.dwork);
1728 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
1730 nf_conntrack_proto_fini();
1731 nf_conntrack_seqadj_fini();
1732 nf_conntrack_labels_fini();
1733 nf_conntrack_helper_fini();
1734 nf_conntrack_timeout_fini();
1735 nf_conntrack_ecache_fini();
1736 nf_conntrack_tstamp_fini();
1737 nf_conntrack_acct_fini();
1738 nf_conntrack_expect_fini();
1740 kmem_cache_destroy(nf_conntrack_cachep);
1744 * Mishearing the voices in his head, our hero wonders how he's
1745 * supposed to kill the mall.
1747 void nf_conntrack_cleanup_net(struct net *net)
1751 list_add(&net->exit_list, &single);
1752 nf_conntrack_cleanup_net_list(&single);
1755 void nf_conntrack_cleanup_net_list(struct list_head *net_exit_list)
1761 * This makes sure all current packets have passed through
1762 * netfilter framework. Roll on, two-stage module
1768 list_for_each_entry(net, net_exit_list, exit_list) {
1769 nf_ct_iterate_cleanup(net, kill_all, NULL, 0, 0);
1770 if (atomic_read(&net->ct.count) != 0)
1775 goto i_see_dead_people;
1778 list_for_each_entry(net, net_exit_list, exit_list) {
1779 nf_conntrack_proto_pernet_fini(net);
1780 nf_conntrack_helper_pernet_fini(net);
1781 nf_conntrack_ecache_pernet_fini(net);
1782 nf_conntrack_tstamp_pernet_fini(net);
1783 nf_conntrack_acct_pernet_fini(net);
1784 nf_conntrack_expect_pernet_fini(net);
1785 free_percpu(net->ct.stat);
1786 free_percpu(net->ct.pcpu_lists);
1790 void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls)
1792 struct hlist_nulls_head *hash;
1793 unsigned int nr_slots, i;
1796 if (*sizep > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1799 BUILD_BUG_ON(sizeof(struct hlist_nulls_head) != sizeof(struct hlist_head));
1800 nr_slots = *sizep = roundup(*sizep, PAGE_SIZE / sizeof(struct hlist_nulls_head));
1802 if (nr_slots > (UINT_MAX / sizeof(struct hlist_nulls_head)))
1805 sz = nr_slots * sizeof(struct hlist_nulls_head);
1806 hash = (void *)__get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
1812 for (i = 0; i < nr_slots; i++)
1813 INIT_HLIST_NULLS_HEAD(&hash[i], i);
1817 EXPORT_SYMBOL_GPL(nf_ct_alloc_hashtable);
1819 int nf_conntrack_hash_resize(unsigned int hashsize)
1822 unsigned int old_size;
1823 struct hlist_nulls_head *hash, *old_hash;
1824 struct nf_conntrack_tuple_hash *h;
1830 hash = nf_ct_alloc_hashtable(&hashsize, 1);
1834 old_size = nf_conntrack_htable_size;
1835 if (old_size == hashsize) {
1836 nf_ct_free_hashtable(hash, hashsize);
1841 nf_conntrack_all_lock();
1842 write_seqcount_begin(&nf_conntrack_generation);
1844 /* Lookups in the old hash might happen in parallel, which means we
1845 * might get false negatives during connection lookup. New connections
1846 * created because of a false negative won't make it into the hash
1847 * though since that required taking the locks.
1850 for (i = 0; i < nf_conntrack_htable_size; i++) {
1851 while (!hlist_nulls_empty(&nf_conntrack_hash[i])) {
1852 h = hlist_nulls_entry(nf_conntrack_hash[i].first,
1853 struct nf_conntrack_tuple_hash, hnnode);
1854 ct = nf_ct_tuplehash_to_ctrack(h);
1855 hlist_nulls_del_rcu(&h->hnnode);
1856 bucket = __hash_conntrack(nf_ct_net(ct),
1857 &h->tuple, hashsize);
1858 hlist_nulls_add_head_rcu(&h->hnnode, &hash[bucket]);
1861 old_size = nf_conntrack_htable_size;
1862 old_hash = nf_conntrack_hash;
1864 nf_conntrack_hash = hash;
1865 nf_conntrack_htable_size = hashsize;
1867 write_seqcount_end(&nf_conntrack_generation);
1868 nf_conntrack_all_unlock();
1872 nf_ct_free_hashtable(old_hash, old_size);
1876 int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
1878 unsigned int hashsize;
1881 if (current->nsproxy->net_ns != &init_net)
1884 /* On boot, we can set this without any fancy locking. */
1885 if (!nf_conntrack_hash)
1886 return param_set_uint(val, kp);
1888 rc = kstrtouint(val, 0, &hashsize);
1892 return nf_conntrack_hash_resize(hashsize);
1894 EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
1896 module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
1897 &nf_conntrack_htable_size, 0600);
1899 void nf_ct_untracked_status_or(unsigned long bits)
1903 for_each_possible_cpu(cpu)
1904 per_cpu(nf_conntrack_untracked, cpu).status |= bits;
1906 EXPORT_SYMBOL_GPL(nf_ct_untracked_status_or);
1908 int nf_conntrack_init_start(void)
1914 seqcount_init(&nf_conntrack_generation);
1916 for (i = 0; i < CONNTRACK_LOCKS; i++)
1917 spin_lock_init(&nf_conntrack_locks[i]);
1919 if (!nf_conntrack_htable_size) {
1920 /* Idea from tcp.c: use 1/16384 of memory.
1921 * On i386: 32MB machine has 512 buckets.
1922 * >= 1GB machines have 16384 buckets.
1923 * >= 4GB machines have 65536 buckets.
1925 nf_conntrack_htable_size
1926 = (((totalram_pages << PAGE_SHIFT) / 16384)
1927 / sizeof(struct hlist_head));
1928 if (totalram_pages > (4 * (1024 * 1024 * 1024 / PAGE_SIZE)))
1929 nf_conntrack_htable_size = 65536;
1930 else if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE))
1931 nf_conntrack_htable_size = 16384;
1932 if (nf_conntrack_htable_size < 32)
1933 nf_conntrack_htable_size = 32;
1935 /* Use a max. factor of four by default to get the same max as
1936 * with the old struct list_heads. When a table size is given
1937 * we use the old value of 8 to avoid reducing the max.
1942 nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size, 1);
1943 if (!nf_conntrack_hash)
1946 nf_conntrack_max = max_factor * nf_conntrack_htable_size;
1948 nf_conntrack_cachep = kmem_cache_create("nf_conntrack",
1949 sizeof(struct nf_conn), 0,
1950 SLAB_DESTROY_BY_RCU | SLAB_HWCACHE_ALIGN, NULL);
1951 if (!nf_conntrack_cachep)
1954 printk(KERN_INFO "nf_conntrack version %s (%u buckets, %d max)\n",
1955 NF_CONNTRACK_VERSION, nf_conntrack_htable_size,
1958 ret = nf_conntrack_expect_init();
1962 ret = nf_conntrack_acct_init();
1966 ret = nf_conntrack_tstamp_init();
1970 ret = nf_conntrack_ecache_init();
1974 ret = nf_conntrack_timeout_init();
1978 ret = nf_conntrack_helper_init();
1982 ret = nf_conntrack_labels_init();
1986 ret = nf_conntrack_seqadj_init();
1990 ret = nf_conntrack_proto_init();
1994 /* Set up fake conntrack: to never be deleted, not in any hashes */
1995 for_each_possible_cpu(cpu) {
1996 struct nf_conn *ct = &per_cpu(nf_conntrack_untracked, cpu);
1997 write_pnet(&ct->ct_net, &init_net);
1998 atomic_set(&ct->ct_general.use, 1);
2000 /* - and look it like as a confirmed connection */
2001 nf_ct_untracked_status_or(IPS_CONFIRMED | IPS_UNTRACKED);
2003 conntrack_gc_work_init(&conntrack_gc_work);
2004 queue_delayed_work(system_long_wq, &conntrack_gc_work.dwork, HZ);
2009 nf_conntrack_seqadj_fini();
2011 nf_conntrack_labels_fini();
2013 nf_conntrack_helper_fini();
2015 nf_conntrack_timeout_fini();
2017 nf_conntrack_ecache_fini();
2019 nf_conntrack_tstamp_fini();
2021 nf_conntrack_acct_fini();
2023 nf_conntrack_expect_fini();
2025 kmem_cache_destroy(nf_conntrack_cachep);
2027 nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_htable_size);
2031 void nf_conntrack_init_end(void)
2033 /* For use by REJECT target */
2034 RCU_INIT_POINTER(ip_ct_attach, nf_conntrack_attach);
2035 RCU_INIT_POINTER(nf_ct_destroy, destroy_conntrack);
2039 * We need to use special "null" values, not used in hash table
2041 #define UNCONFIRMED_NULLS_VAL ((1<<30)+0)
2042 #define DYING_NULLS_VAL ((1<<30)+1)
2043 #define TEMPLATE_NULLS_VAL ((1<<30)+2)
2045 int nf_conntrack_init_net(struct net *net)
2050 atomic_set(&net->ct.count, 0);
2052 net->ct.pcpu_lists = alloc_percpu(struct ct_pcpu);
2053 if (!net->ct.pcpu_lists)
2056 for_each_possible_cpu(cpu) {
2057 struct ct_pcpu *pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
2059 spin_lock_init(&pcpu->lock);
2060 INIT_HLIST_NULLS_HEAD(&pcpu->unconfirmed, UNCONFIRMED_NULLS_VAL);
2061 INIT_HLIST_NULLS_HEAD(&pcpu->dying, DYING_NULLS_VAL);
2064 net->ct.stat = alloc_percpu(struct ip_conntrack_stat);
2066 goto err_pcpu_lists;
2068 ret = nf_conntrack_expect_pernet_init(net);
2071 ret = nf_conntrack_acct_pernet_init(net);
2074 ret = nf_conntrack_tstamp_pernet_init(net);
2077 ret = nf_conntrack_ecache_pernet_init(net);
2080 ret = nf_conntrack_helper_pernet_init(net);
2083 ret = nf_conntrack_proto_pernet_init(net);
2089 nf_conntrack_helper_pernet_fini(net);
2091 nf_conntrack_ecache_pernet_fini(net);
2093 nf_conntrack_tstamp_pernet_fini(net);
2095 nf_conntrack_acct_pernet_fini(net);
2097 nf_conntrack_expect_pernet_fini(net);
2099 free_percpu(net->ct.stat);
2101 free_percpu(net->ct.pcpu_lists);