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 | |
| 32 | DEFINE_PER_CPU(struct nf_conntrack_ecache, nf_conntrack_ecache); |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 33 | EXPORT_PER_CPU_SYMBOL_GPL(nf_conntrack_ecache); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 34 | |
| 35 | /* deliver cached events and clear cache entry - must be called with locally |
| 36 | * disabled softirqs */ |
| 37 | static inline void |
| 38 | __nf_ct_deliver_cached_events(struct nf_conntrack_ecache *ecache) |
| 39 | { |
| 40 | if (nf_ct_is_confirmed(ecache->ct) && !nf_ct_is_dying(ecache->ct) |
| 41 | && ecache->events) |
| 42 | atomic_notifier_call_chain(&nf_conntrack_chain, ecache->events, |
| 43 | ecache->ct); |
| 44 | |
| 45 | ecache->events = 0; |
| 46 | nf_ct_put(ecache->ct); |
| 47 | ecache->ct = NULL; |
| 48 | } |
| 49 | |
| 50 | /* Deliver all cached events for a particular conntrack. This is called |
| 51 | * by code prior to async packet handling for freeing the skb */ |
| 52 | void nf_ct_deliver_cached_events(const struct nf_conn *ct) |
| 53 | { |
| 54 | struct nf_conntrack_ecache *ecache; |
| 55 | |
| 56 | local_bh_disable(); |
| 57 | ecache = &__get_cpu_var(nf_conntrack_ecache); |
| 58 | if (ecache->ct == ct) |
| 59 | __nf_ct_deliver_cached_events(ecache); |
| 60 | local_bh_enable(); |
| 61 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 62 | EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 63 | |
| 64 | /* Deliver cached events for old pending events, if current conntrack != old */ |
| 65 | void __nf_ct_event_cache_init(struct nf_conn *ct) |
| 66 | { |
| 67 | struct nf_conntrack_ecache *ecache; |
| 68 | |
| 69 | /* take care of delivering potentially old events */ |
| 70 | ecache = &__get_cpu_var(nf_conntrack_ecache); |
| 71 | BUG_ON(ecache->ct == ct); |
| 72 | if (ecache->ct) |
| 73 | __nf_ct_deliver_cached_events(ecache); |
| 74 | /* initialize for this conntrack/packet */ |
| 75 | ecache->ct = ct; |
| 76 | nf_conntrack_get(&ct->ct_general); |
| 77 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 78 | EXPORT_SYMBOL_GPL(__nf_ct_event_cache_init); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 79 | |
| 80 | /* flush the event cache - touches other CPU's data and must not be called |
| 81 | * while packets are still passing through the code */ |
| 82 | void nf_ct_event_cache_flush(void) |
| 83 | { |
| 84 | struct nf_conntrack_ecache *ecache; |
| 85 | int cpu; |
| 86 | |
| 87 | for_each_possible_cpu(cpu) { |
| 88 | ecache = &per_cpu(nf_conntrack_ecache, cpu); |
| 89 | if (ecache->ct) |
| 90 | nf_ct_put(ecache->ct); |
| 91 | } |
| 92 | } |
| 93 | |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 94 | int nf_conntrack_register_notifier(struct notifier_block *nb) |
| 95 | { |
| 96 | return atomic_notifier_chain_register(&nf_conntrack_chain, nb); |
| 97 | } |
| 98 | EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier); |
| 99 | |
| 100 | int nf_conntrack_unregister_notifier(struct notifier_block *nb) |
| 101 | { |
| 102 | return atomic_notifier_chain_unregister(&nf_conntrack_chain, nb); |
| 103 | } |
| 104 | EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier); |
| 105 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 106 | int nf_ct_expect_register_notifier(struct notifier_block *nb) |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 107 | { |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 108 | return atomic_notifier_chain_register(&nf_ct_expect_chain, nb); |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 109 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 110 | EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier); |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 111 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 112 | int nf_ct_expect_unregister_notifier(struct notifier_block *nb) |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 113 | { |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 114 | return atomic_notifier_chain_unregister(&nf_ct_expect_chain, nb); |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 115 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 116 | EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier); |