1 /* Expectation handling for nf_conntrack. */
3 /* (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/types.h>
13 #include <linux/netfilter.h>
14 #include <linux/skbuff.h>
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
17 #include <linux/stddef.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/percpu.h>
21 #include <linux/kernel.h>
22 #include <linux/jhash.h>
23 #include <linux/moduleparam.h>
24 #include <net/net_namespace.h>
26 #include <net/netfilter/nf_conntrack.h>
27 #include <net/netfilter/nf_conntrack_core.h>
28 #include <net/netfilter/nf_conntrack_expect.h>
29 #include <net/netfilter/nf_conntrack_helper.h>
30 #include <net/netfilter/nf_conntrack_tuple.h>
31 #include <net/netfilter/nf_conntrack_zones.h>
33 unsigned int nf_ct_expect_hsize __read_mostly;
34 EXPORT_SYMBOL_GPL(nf_ct_expect_hsize);
36 unsigned int nf_ct_expect_max __read_mostly;
38 static struct kmem_cache *nf_ct_expect_cachep __read_mostly;
40 static HLIST_HEAD(nf_ct_userspace_expect_list);
42 /* nf_conntrack_expect helper functions */
43 void nf_ct_unlink_expect_report(struct nf_conntrack_expect *exp,
46 struct nf_conn_help *master_help = nfct_help(exp->master);
47 struct net *net = nf_ct_exp_net(exp);
49 NF_CT_ASSERT(!timer_pending(&exp->timeout));
51 hlist_del_rcu(&exp->hnode);
52 net->ct.expect_count--;
54 hlist_del(&exp->lnode);
55 if (!(exp->flags & NF_CT_EXPECT_USERSPACE))
56 master_help->expecting[exp->class]--;
58 nf_ct_expect_event_report(IPEXP_DESTROY, exp, pid, report);
59 nf_ct_expect_put(exp);
61 NF_CT_STAT_INC(net, expect_delete);
63 EXPORT_SYMBOL_GPL(nf_ct_unlink_expect_report);
65 static void nf_ct_expectation_timed_out(unsigned long ul_expect)
67 struct nf_conntrack_expect *exp = (void *)ul_expect;
69 spin_lock_bh(&nf_conntrack_lock);
70 nf_ct_unlink_expect(exp);
71 spin_unlock_bh(&nf_conntrack_lock);
72 nf_ct_expect_put(exp);
75 static unsigned int nf_ct_expect_dst_hash(const struct nf_conntrack_tuple *tuple)
79 if (unlikely(!nf_conntrack_hash_rnd)) {
80 init_nf_conntrack_hash_rnd();
83 hash = jhash2(tuple->dst.u3.all, ARRAY_SIZE(tuple->dst.u3.all),
84 (((tuple->dst.protonum ^ tuple->src.l3num) << 16) |
85 (__force __u16)tuple->dst.u.all) ^ nf_conntrack_hash_rnd);
86 return ((u64)hash * nf_ct_expect_hsize) >> 32;
89 struct nf_conntrack_expect *
90 __nf_ct_expect_find(struct net *net, u16 zone,
91 const struct nf_conntrack_tuple *tuple)
93 struct nf_conntrack_expect *i;
97 if (!net->ct.expect_count)
100 h = nf_ct_expect_dst_hash(tuple);
101 hlist_for_each_entry_rcu(i, n, &net->ct.expect_hash[h], hnode) {
102 if (nf_ct_tuple_mask_cmp(tuple, &i->tuple, &i->mask) &&
103 nf_ct_zone(i->master) == zone)
108 EXPORT_SYMBOL_GPL(__nf_ct_expect_find);
110 /* Just find a expectation corresponding to a tuple. */
111 struct nf_conntrack_expect *
112 nf_ct_expect_find_get(struct net *net, u16 zone,
113 const struct nf_conntrack_tuple *tuple)
115 struct nf_conntrack_expect *i;
118 i = __nf_ct_expect_find(net, zone, tuple);
119 if (i && !atomic_inc_not_zero(&i->use))
125 EXPORT_SYMBOL_GPL(nf_ct_expect_find_get);
127 /* If an expectation for this connection is found, it gets delete from
128 * global list then returned. */
129 struct nf_conntrack_expect *
130 nf_ct_find_expectation(struct net *net, u16 zone,
131 const struct nf_conntrack_tuple *tuple)
133 struct nf_conntrack_expect *i, *exp = NULL;
134 struct hlist_node *n;
137 if (!net->ct.expect_count)
140 h = nf_ct_expect_dst_hash(tuple);
141 hlist_for_each_entry(i, n, &net->ct.expect_hash[h], hnode) {
142 if (!(i->flags & NF_CT_EXPECT_INACTIVE) &&
143 nf_ct_tuple_mask_cmp(tuple, &i->tuple, &i->mask) &&
144 nf_ct_zone(i->master) == zone) {
152 /* If master is not in hash table yet (ie. packet hasn't left
153 this machine yet), how can other end know about expected?
154 Hence these are not the droids you are looking for (if
155 master ct never got confirmed, we'd hold a reference to it
156 and weird things would happen to future packets). */
157 if (!nf_ct_is_confirmed(exp->master))
160 if (exp->flags & NF_CT_EXPECT_PERMANENT) {
161 atomic_inc(&exp->use);
163 } else if (del_timer(&exp->timeout)) {
164 nf_ct_unlink_expect(exp);
171 /* delete all expectations for this conntrack */
172 void nf_ct_remove_expectations(struct nf_conn *ct)
174 struct nf_conn_help *help = nfct_help(ct);
175 struct nf_conntrack_expect *exp;
176 struct hlist_node *n, *next;
178 /* Optimization: most connection never expect any others. */
182 hlist_for_each_entry_safe(exp, n, next, &help->expectations, lnode) {
183 if (del_timer(&exp->timeout)) {
184 nf_ct_unlink_expect(exp);
185 nf_ct_expect_put(exp);
189 EXPORT_SYMBOL_GPL(nf_ct_remove_expectations);
191 /* Would two expected things clash? */
192 static inline int expect_clash(const struct nf_conntrack_expect *a,
193 const struct nf_conntrack_expect *b)
195 /* Part covered by intersection of masks must be unequal,
196 otherwise they clash */
197 struct nf_conntrack_tuple_mask intersect_mask;
200 intersect_mask.src.u.all = a->mask.src.u.all & b->mask.src.u.all;
202 for (count = 0; count < NF_CT_TUPLE_L3SIZE; count++){
203 intersect_mask.src.u3.all[count] =
204 a->mask.src.u3.all[count] & b->mask.src.u3.all[count];
207 return nf_ct_tuple_mask_cmp(&a->tuple, &b->tuple, &intersect_mask);
210 static inline int expect_matches(const struct nf_conntrack_expect *a,
211 const struct nf_conntrack_expect *b)
213 return a->master == b->master && a->class == b->class &&
214 nf_ct_tuple_equal(&a->tuple, &b->tuple) &&
215 nf_ct_tuple_mask_equal(&a->mask, &b->mask) &&
216 nf_ct_zone(a->master) == nf_ct_zone(b->master);
219 /* Generally a bad idea to call this: could have matched already. */
220 void nf_ct_unexpect_related(struct nf_conntrack_expect *exp)
222 spin_lock_bh(&nf_conntrack_lock);
223 if (del_timer(&exp->timeout)) {
224 nf_ct_unlink_expect(exp);
225 nf_ct_expect_put(exp);
227 spin_unlock_bh(&nf_conntrack_lock);
229 EXPORT_SYMBOL_GPL(nf_ct_unexpect_related);
231 /* We don't increase the master conntrack refcount for non-fulfilled
232 * conntracks. During the conntrack destruction, the expectations are
233 * always killed before the conntrack itself */
234 struct nf_conntrack_expect *nf_ct_expect_alloc(struct nf_conn *me)
236 struct nf_conntrack_expect *new;
238 new = kmem_cache_alloc(nf_ct_expect_cachep, GFP_ATOMIC);
243 atomic_set(&new->use, 1);
246 EXPORT_SYMBOL_GPL(nf_ct_expect_alloc);
248 void nf_ct_expect_init(struct nf_conntrack_expect *exp, unsigned int class,
250 const union nf_inet_addr *saddr,
251 const union nf_inet_addr *daddr,
252 u_int8_t proto, const __be16 *src, const __be16 *dst)
256 if (family == AF_INET)
263 exp->expectfn = NULL;
265 exp->tuple.src.l3num = family;
266 exp->tuple.dst.protonum = proto;
269 memcpy(&exp->tuple.src.u3, saddr, len);
270 if (sizeof(exp->tuple.src.u3) > len)
271 /* address needs to be cleared for nf_ct_tuple_equal */
272 memset((void *)&exp->tuple.src.u3 + len, 0x00,
273 sizeof(exp->tuple.src.u3) - len);
274 memset(&exp->mask.src.u3, 0xFF, len);
275 if (sizeof(exp->mask.src.u3) > len)
276 memset((void *)&exp->mask.src.u3 + len, 0x00,
277 sizeof(exp->mask.src.u3) - len);
279 memset(&exp->tuple.src.u3, 0x00, sizeof(exp->tuple.src.u3));
280 memset(&exp->mask.src.u3, 0x00, sizeof(exp->mask.src.u3));
284 exp->tuple.src.u.all = *src;
285 exp->mask.src.u.all = htons(0xFFFF);
287 exp->tuple.src.u.all = 0;
288 exp->mask.src.u.all = 0;
291 memcpy(&exp->tuple.dst.u3, daddr, len);
292 if (sizeof(exp->tuple.dst.u3) > len)
293 /* address needs to be cleared for nf_ct_tuple_equal */
294 memset((void *)&exp->tuple.dst.u3 + len, 0x00,
295 sizeof(exp->tuple.dst.u3) - len);
297 exp->tuple.dst.u.all = *dst;
299 EXPORT_SYMBOL_GPL(nf_ct_expect_init);
301 static void nf_ct_expect_free_rcu(struct rcu_head *head)
303 struct nf_conntrack_expect *exp;
305 exp = container_of(head, struct nf_conntrack_expect, rcu);
306 kmem_cache_free(nf_ct_expect_cachep, exp);
309 void nf_ct_expect_put(struct nf_conntrack_expect *exp)
311 if (atomic_dec_and_test(&exp->use))
312 call_rcu(&exp->rcu, nf_ct_expect_free_rcu);
314 EXPORT_SYMBOL_GPL(nf_ct_expect_put);
316 static void nf_ct_expect_insert(struct nf_conntrack_expect *exp)
318 struct nf_conn_help *master_help = nfct_help(exp->master);
319 struct net *net = nf_ct_exp_net(exp);
320 const struct nf_conntrack_expect_policy *p;
321 unsigned int h = nf_ct_expect_dst_hash(&exp->tuple);
323 /* two references : one for hash insert, one for the timer */
324 atomic_add(2, &exp->use);
327 hlist_add_head(&exp->lnode, &master_help->expectations);
328 master_help->expecting[exp->class]++;
329 } else if (exp->flags & NF_CT_EXPECT_USERSPACE)
330 hlist_add_head(&exp->lnode, &nf_ct_userspace_expect_list);
332 hlist_add_head_rcu(&exp->hnode, &net->ct.expect_hash[h]);
333 net->ct.expect_count++;
335 setup_timer(&exp->timeout, nf_ct_expectation_timed_out,
338 p = &rcu_dereference_protected(
340 lockdep_is_held(&nf_conntrack_lock)
341 )->expect_policy[exp->class];
342 exp->timeout.expires = jiffies + p->timeout * HZ;
344 add_timer(&exp->timeout);
346 NF_CT_STAT_INC(net, expect_create);
349 /* Race with expectations being used means we could have none to find; OK. */
350 static void evict_oldest_expect(struct nf_conn *master,
351 struct nf_conntrack_expect *new)
353 struct nf_conn_help *master_help = nfct_help(master);
354 struct nf_conntrack_expect *exp, *last = NULL;
355 struct hlist_node *n;
357 hlist_for_each_entry(exp, n, &master_help->expectations, lnode) {
358 if (exp->class == new->class)
362 if (last && del_timer(&last->timeout)) {
363 nf_ct_unlink_expect(last);
364 nf_ct_expect_put(last);
368 static inline int refresh_timer(struct nf_conntrack_expect *i)
370 struct nf_conn_help *master_help = nfct_help(i->master);
371 const struct nf_conntrack_expect_policy *p;
373 if (!del_timer(&i->timeout))
376 p = &rcu_dereference_protected(
378 lockdep_is_held(&nf_conntrack_lock)
379 )->expect_policy[i->class];
380 i->timeout.expires = jiffies + p->timeout * HZ;
381 add_timer(&i->timeout);
385 static inline int __nf_ct_expect_check(struct nf_conntrack_expect *expect)
387 const struct nf_conntrack_expect_policy *p;
388 struct nf_conntrack_expect *i;
389 struct nf_conn *master = expect->master;
390 struct nf_conn_help *master_help = nfct_help(master);
391 struct net *net = nf_ct_exp_net(expect);
392 struct hlist_node *n;
396 /* Don't allow expectations created from kernel-space with no helper */
397 if (!(expect->flags & NF_CT_EXPECT_USERSPACE) &&
398 (!master_help || (master_help && !master_help->helper))) {
402 h = nf_ct_expect_dst_hash(&expect->tuple);
403 hlist_for_each_entry(i, n, &net->ct.expect_hash[h], hnode) {
404 if (expect_matches(i, expect)) {
405 /* Refresh timer: if it's dying, ignore.. */
406 if (refresh_timer(i)) {
410 } else if (expect_clash(i, expect)) {
415 /* Will be over limit? */
417 p = &rcu_dereference_protected(
419 lockdep_is_held(&nf_conntrack_lock)
420 )->expect_policy[expect->class];
421 if (p->max_expected &&
422 master_help->expecting[expect->class] >= p->max_expected) {
423 evict_oldest_expect(master, expect);
424 if (master_help->expecting[expect->class]
425 >= p->max_expected) {
432 if (net->ct.expect_count >= nf_ct_expect_max) {
435 "nf_conntrack: expectation table full\n");
442 int nf_ct_expect_related_report(struct nf_conntrack_expect *expect,
447 spin_lock_bh(&nf_conntrack_lock);
448 ret = __nf_ct_expect_check(expect);
453 nf_ct_expect_insert(expect);
454 spin_unlock_bh(&nf_conntrack_lock);
455 nf_ct_expect_event_report(IPEXP_NEW, expect, pid, report);
458 spin_unlock_bh(&nf_conntrack_lock);
461 EXPORT_SYMBOL_GPL(nf_ct_expect_related_report);
463 void nf_ct_remove_userspace_expectations(void)
465 struct nf_conntrack_expect *exp;
466 struct hlist_node *n, *next;
468 hlist_for_each_entry_safe(exp, n, next,
469 &nf_ct_userspace_expect_list, lnode) {
470 if (del_timer(&exp->timeout)) {
471 nf_ct_unlink_expect(exp);
472 nf_ct_expect_put(exp);
476 EXPORT_SYMBOL_GPL(nf_ct_remove_userspace_expectations);
478 #ifdef CONFIG_PROC_FS
479 struct ct_expect_iter_state {
480 struct seq_net_private p;
484 static struct hlist_node *ct_expect_get_first(struct seq_file *seq)
486 struct net *net = seq_file_net(seq);
487 struct ct_expect_iter_state *st = seq->private;
488 struct hlist_node *n;
490 for (st->bucket = 0; st->bucket < nf_ct_expect_hsize; st->bucket++) {
491 n = rcu_dereference(hlist_first_rcu(&net->ct.expect_hash[st->bucket]));
498 static struct hlist_node *ct_expect_get_next(struct seq_file *seq,
499 struct hlist_node *head)
501 struct net *net = seq_file_net(seq);
502 struct ct_expect_iter_state *st = seq->private;
504 head = rcu_dereference(hlist_next_rcu(head));
505 while (head == NULL) {
506 if (++st->bucket >= nf_ct_expect_hsize)
508 head = rcu_dereference(hlist_first_rcu(&net->ct.expect_hash[st->bucket]));
513 static struct hlist_node *ct_expect_get_idx(struct seq_file *seq, loff_t pos)
515 struct hlist_node *head = ct_expect_get_first(seq);
518 while (pos && (head = ct_expect_get_next(seq, head)))
520 return pos ? NULL : head;
523 static void *exp_seq_start(struct seq_file *seq, loff_t *pos)
527 return ct_expect_get_idx(seq, *pos);
530 static void *exp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
533 return ct_expect_get_next(seq, v);
536 static void exp_seq_stop(struct seq_file *seq, void *v)
542 static int exp_seq_show(struct seq_file *s, void *v)
544 struct nf_conntrack_expect *expect;
545 struct nf_conntrack_helper *helper;
546 struct hlist_node *n = v;
549 expect = hlist_entry(n, struct nf_conntrack_expect, hnode);
551 if (expect->timeout.function)
552 seq_printf(s, "%ld ", timer_pending(&expect->timeout)
553 ? (long)(expect->timeout.expires - jiffies)/HZ : 0);
556 seq_printf(s, "l3proto = %u proto=%u ",
557 expect->tuple.src.l3num,
558 expect->tuple.dst.protonum);
559 print_tuple(s, &expect->tuple,
560 __nf_ct_l3proto_find(expect->tuple.src.l3num),
561 __nf_ct_l4proto_find(expect->tuple.src.l3num,
562 expect->tuple.dst.protonum));
564 if (expect->flags & NF_CT_EXPECT_PERMANENT) {
565 seq_printf(s, "PERMANENT");
568 if (expect->flags & NF_CT_EXPECT_INACTIVE) {
569 seq_printf(s, "%sINACTIVE", delim);
572 if (expect->flags & NF_CT_EXPECT_USERSPACE)
573 seq_printf(s, "%sUSERSPACE", delim);
575 helper = rcu_dereference(nfct_help(expect->master)->helper);
577 seq_printf(s, "%s%s", expect->flags ? " " : "", helper->name);
578 if (helper->expect_policy[expect->class].name)
580 helper->expect_policy[expect->class].name);
583 return seq_putc(s, '\n');
586 static const struct seq_operations exp_seq_ops = {
587 .start = exp_seq_start,
588 .next = exp_seq_next,
589 .stop = exp_seq_stop,
593 static int exp_open(struct inode *inode, struct file *file)
595 return seq_open_net(inode, file, &exp_seq_ops,
596 sizeof(struct ct_expect_iter_state));
599 static const struct file_operations exp_file_ops = {
600 .owner = THIS_MODULE,
604 .release = seq_release_net,
606 #endif /* CONFIG_PROC_FS */
608 static int exp_proc_init(struct net *net)
610 #ifdef CONFIG_PROC_FS
611 struct proc_dir_entry *proc;
613 proc = proc_net_fops_create(net, "nf_conntrack_expect", 0440, &exp_file_ops);
616 #endif /* CONFIG_PROC_FS */
620 static void exp_proc_remove(struct net *net)
622 #ifdef CONFIG_PROC_FS
623 proc_net_remove(net, "nf_conntrack_expect");
624 #endif /* CONFIG_PROC_FS */
627 module_param_named(expect_hashsize, nf_ct_expect_hsize, uint, 0400);
629 int nf_conntrack_expect_init(struct net *net)
633 if (net_eq(net, &init_net)) {
634 if (!nf_ct_expect_hsize) {
635 nf_ct_expect_hsize = net->ct.htable_size / 256;
636 if (!nf_ct_expect_hsize)
637 nf_ct_expect_hsize = 1;
639 nf_ct_expect_max = nf_ct_expect_hsize * 4;
642 net->ct.expect_count = 0;
643 net->ct.expect_hash = nf_ct_alloc_hashtable(&nf_ct_expect_hsize, 0);
644 if (net->ct.expect_hash == NULL)
647 if (net_eq(net, &init_net)) {
648 nf_ct_expect_cachep = kmem_cache_create("nf_conntrack_expect",
649 sizeof(struct nf_conntrack_expect),
651 if (!nf_ct_expect_cachep)
655 err = exp_proc_init(net);
662 if (net_eq(net, &init_net))
663 kmem_cache_destroy(nf_ct_expect_cachep);
665 nf_ct_free_hashtable(net->ct.expect_hash, nf_ct_expect_hsize);
670 void nf_conntrack_expect_fini(struct net *net)
672 exp_proc_remove(net);
673 if (net_eq(net, &init_net)) {
674 rcu_barrier(); /* Wait for call_rcu() before destroy */
675 kmem_cache_destroy(nf_ct_expect_cachep);
677 nf_ct_free_hashtable(net->ct.expect_hash, nf_ct_expect_hsize);