Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 1 | /* Event cache for netfilter. */ |
| 2 | |
| 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> |
| 6 | * |
| 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. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/netfilter.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/stddef.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/percpu.h> |
| 19 | #include <linux/notifier.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/netdevice.h> |
| 22 | |
| 23 | #include <net/netfilter/nf_conntrack.h> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 24 | #include <net/netfilter/nf_conntrack_core.h> |
| 25 | |
| 26 | ATOMIC_NOTIFIER_HEAD(nf_conntrack_chain); |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 27 | EXPORT_SYMBOL_GPL(nf_conntrack_chain); |
| 28 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 29 | ATOMIC_NOTIFIER_HEAD(nf_ct_expect_chain); |
| 30 | EXPORT_SYMBOL_GPL(nf_ct_expect_chain); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 31 | |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 32 | /* deliver cached events and clear cache entry - must be called with locally |
| 33 | * disabled softirqs */ |
| 34 | static inline void |
| 35 | __nf_ct_deliver_cached_events(struct nf_conntrack_ecache *ecache) |
| 36 | { |
| 37 | if (nf_ct_is_confirmed(ecache->ct) && !nf_ct_is_dying(ecache->ct) |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 38 | && ecache->events) { |
| 39 | struct nf_ct_event item = { |
| 40 | .ct = ecache->ct, |
| 41 | .pid = 0, |
| 42 | .report = 0 |
| 43 | }; |
| 44 | |
| 45 | atomic_notifier_call_chain(&nf_conntrack_chain, |
| 46 | ecache->events, |
| 47 | &item); |
| 48 | } |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 49 | |
| 50 | ecache->events = 0; |
| 51 | nf_ct_put(ecache->ct); |
| 52 | ecache->ct = NULL; |
| 53 | } |
| 54 | |
| 55 | /* Deliver all cached events for a particular conntrack. This is called |
| 56 | * by code prior to async packet handling for freeing the skb */ |
| 57 | void nf_ct_deliver_cached_events(const struct nf_conn *ct) |
| 58 | { |
Alexey Dobriyan | 6058fa6 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 59 | struct net *net = nf_ct_net(ct); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 60 | struct nf_conntrack_ecache *ecache; |
| 61 | |
| 62 | local_bh_disable(); |
Alexey Dobriyan | 6058fa6 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 63 | ecache = per_cpu_ptr(net->ct.ecache, raw_smp_processor_id()); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 64 | if (ecache->ct == ct) |
| 65 | __nf_ct_deliver_cached_events(ecache); |
| 66 | local_bh_enable(); |
| 67 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 68 | EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 69 | |
| 70 | /* Deliver cached events for old pending events, if current conntrack != old */ |
| 71 | void __nf_ct_event_cache_init(struct nf_conn *ct) |
| 72 | { |
Alexey Dobriyan | 6058fa6 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 73 | struct net *net = nf_ct_net(ct); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 74 | struct nf_conntrack_ecache *ecache; |
| 75 | |
| 76 | /* take care of delivering potentially old events */ |
Alexey Dobriyan | 6058fa6 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 77 | ecache = per_cpu_ptr(net->ct.ecache, raw_smp_processor_id()); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 78 | BUG_ON(ecache->ct == ct); |
| 79 | if (ecache->ct) |
| 80 | __nf_ct_deliver_cached_events(ecache); |
| 81 | /* initialize for this conntrack/packet */ |
| 82 | ecache->ct = ct; |
| 83 | nf_conntrack_get(&ct->ct_general); |
| 84 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 85 | EXPORT_SYMBOL_GPL(__nf_ct_event_cache_init); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 86 | |
| 87 | /* flush the event cache - touches other CPU's data and must not be called |
| 88 | * while packets are still passing through the code */ |
Alexey Dobriyan | 6058fa6 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 89 | void nf_ct_event_cache_flush(struct net *net) |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 90 | { |
| 91 | struct nf_conntrack_ecache *ecache; |
| 92 | int cpu; |
| 93 | |
| 94 | for_each_possible_cpu(cpu) { |
Alexey Dobriyan | 6058fa6 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 95 | ecache = per_cpu_ptr(net->ct.ecache, cpu); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 96 | if (ecache->ct) |
| 97 | nf_ct_put(ecache->ct); |
| 98 | } |
| 99 | } |
| 100 | |
Alexey Dobriyan | 6058fa6 | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 101 | int nf_conntrack_ecache_init(struct net *net) |
| 102 | { |
| 103 | net->ct.ecache = alloc_percpu(struct nf_conntrack_ecache); |
| 104 | if (!net->ct.ecache) |
| 105 | return -ENOMEM; |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | void nf_conntrack_ecache_fini(struct net *net) |
| 110 | { |
| 111 | free_percpu(net->ct.ecache); |
| 112 | } |
| 113 | |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 114 | int nf_conntrack_register_notifier(struct notifier_block *nb) |
| 115 | { |
| 116 | return atomic_notifier_chain_register(&nf_conntrack_chain, nb); |
| 117 | } |
| 118 | EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier); |
| 119 | |
| 120 | int nf_conntrack_unregister_notifier(struct notifier_block *nb) |
| 121 | { |
| 122 | return atomic_notifier_chain_unregister(&nf_conntrack_chain, nb); |
| 123 | } |
| 124 | EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier); |
| 125 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 126 | int nf_ct_expect_register_notifier(struct notifier_block *nb) |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 127 | { |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 128 | return atomic_notifier_chain_register(&nf_ct_expect_chain, nb); |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 129 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 130 | EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier); |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 131 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 132 | int nf_ct_expect_unregister_notifier(struct notifier_block *nb) |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 133 | { |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 134 | return atomic_notifier_chain_unregister(&nf_ct_expect_chain, nb); |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 135 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 136 | EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier); |