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> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 19 | #include <linux/kernel.h> |
| 20 | #include <linux/netdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 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> |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 25 | #include <net/netfilter/nf_conntrack_extend.h> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 26 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 27 | static DEFINE_MUTEX(nf_ct_ecache_mutex); |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 28 | |
Arnd Bergmann | 0906a37 | 2010-03-09 20:59:15 +0100 | [diff] [blame] | 29 | struct nf_ct_event_notifier __rcu *nf_conntrack_event_cb __read_mostly; |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 30 | EXPORT_SYMBOL_GPL(nf_conntrack_event_cb); |
| 31 | |
Arnd Bergmann | 0906a37 | 2010-03-09 20:59:15 +0100 | [diff] [blame] | 32 | struct nf_exp_event_notifier __rcu *nf_expect_event_cb __read_mostly; |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 33 | EXPORT_SYMBOL_GPL(nf_expect_event_cb); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 34 | |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 35 | /* deliver cached events and clear cache entry - must be called with locally |
| 36 | * disabled softirqs */ |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 37 | void nf_ct_deliver_cached_events(struct nf_conn *ct) |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 38 | { |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 39 | unsigned long events; |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 40 | struct nf_ct_event_notifier *notify; |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 41 | struct nf_conntrack_ecache *e; |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 42 | |
| 43 | rcu_read_lock(); |
| 44 | notify = rcu_dereference(nf_conntrack_event_cb); |
| 45 | if (notify == NULL) |
| 46 | goto out_unlock; |
| 47 | |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 48 | e = nf_ct_ecache_find(ct); |
| 49 | if (e == NULL) |
| 50 | goto out_unlock; |
| 51 | |
| 52 | events = xchg(&e->cache, 0); |
| 53 | |
| 54 | if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct) && events) { |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 55 | struct nf_ct_event item = { |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 56 | .ct = ct, |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 57 | .pid = 0, |
| 58 | .report = 0 |
| 59 | }; |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 60 | int ret; |
| 61 | /* We make a copy of the missed event cache without taking |
| 62 | * the lock, thus we may send missed events twice. However, |
| 63 | * this does not harm and it happens very rarely. */ |
| 64 | unsigned long missed = e->missed; |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 65 | |
Pablo Neira Ayuso | 3db7e93 | 2011-02-01 16:06:30 +0100 | [diff] [blame] | 66 | if (!((events | missed) & e->ctmask)) |
| 67 | goto out_unlock; |
| 68 | |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 69 | ret = notify->fcn(events | missed, &item); |
| 70 | if (unlikely(ret < 0 || missed)) { |
| 71 | spin_lock_bh(&ct->lock); |
| 72 | if (ret < 0) |
| 73 | e->missed |= events; |
| 74 | else |
| 75 | e->missed &= ~missed; |
| 76 | spin_unlock_bh(&ct->lock); |
| 77 | } |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 78 | } |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 79 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 80 | out_unlock: |
| 81 | rcu_read_unlock(); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 82 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 83 | EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 84 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 85 | int nf_conntrack_register_notifier(struct nf_ct_event_notifier *new) |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 86 | { |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 87 | int ret = 0; |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 88 | struct nf_ct_event_notifier *notify; |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 89 | |
| 90 | mutex_lock(&nf_ct_ecache_mutex); |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 91 | notify = rcu_dereference_protected(nf_conntrack_event_cb, |
| 92 | lockdep_is_held(&nf_ct_ecache_mutex)); |
| 93 | if (notify != NULL) { |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 94 | ret = -EBUSY; |
| 95 | goto out_unlock; |
| 96 | } |
| 97 | rcu_assign_pointer(nf_conntrack_event_cb, new); |
| 98 | mutex_unlock(&nf_ct_ecache_mutex); |
| 99 | return ret; |
| 100 | |
| 101 | out_unlock: |
| 102 | mutex_unlock(&nf_ct_ecache_mutex); |
| 103 | return ret; |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 104 | } |
| 105 | EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier); |
| 106 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 107 | void nf_conntrack_unregister_notifier(struct nf_ct_event_notifier *new) |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 108 | { |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 109 | struct nf_ct_event_notifier *notify; |
| 110 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 111 | mutex_lock(&nf_ct_ecache_mutex); |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 112 | notify = rcu_dereference_protected(nf_conntrack_event_cb, |
| 113 | lockdep_is_held(&nf_ct_ecache_mutex)); |
| 114 | BUG_ON(notify != new); |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 115 | rcu_assign_pointer(nf_conntrack_event_cb, NULL); |
| 116 | mutex_unlock(&nf_ct_ecache_mutex); |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 117 | } |
| 118 | EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier); |
| 119 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 120 | int nf_ct_expect_register_notifier(struct nf_exp_event_notifier *new) |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 121 | { |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 122 | int ret = 0; |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 123 | struct nf_exp_event_notifier *notify; |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 124 | |
| 125 | mutex_lock(&nf_ct_ecache_mutex); |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 126 | notify = rcu_dereference_protected(nf_expect_event_cb, |
| 127 | lockdep_is_held(&nf_ct_ecache_mutex)); |
| 128 | if (notify != NULL) { |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 129 | ret = -EBUSY; |
| 130 | goto out_unlock; |
| 131 | } |
| 132 | rcu_assign_pointer(nf_expect_event_cb, new); |
| 133 | mutex_unlock(&nf_ct_ecache_mutex); |
| 134 | return ret; |
| 135 | |
| 136 | out_unlock: |
| 137 | mutex_unlock(&nf_ct_ecache_mutex); |
| 138 | return ret; |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 139 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 140 | EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier); |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 141 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 142 | void nf_ct_expect_unregister_notifier(struct nf_exp_event_notifier *new) |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 143 | { |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 144 | struct nf_exp_event_notifier *notify; |
| 145 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 146 | mutex_lock(&nf_ct_ecache_mutex); |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 147 | notify = rcu_dereference_protected(nf_expect_event_cb, |
| 148 | lockdep_is_held(&nf_ct_ecache_mutex)); |
| 149 | BUG_ON(notify != new); |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 150 | rcu_assign_pointer(nf_expect_event_cb, NULL); |
| 151 | mutex_unlock(&nf_ct_ecache_mutex); |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 152 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 153 | EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier); |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 154 | |
| 155 | #define NF_CT_EVENTS_DEFAULT 1 |
| 156 | static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT; |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 157 | static int nf_ct_events_retry_timeout __read_mostly = 15*HZ; |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 158 | |
| 159 | #ifdef CONFIG_SYSCTL |
| 160 | static struct ctl_table event_sysctl_table[] = { |
| 161 | { |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 162 | .procname = "nf_conntrack_events", |
| 163 | .data = &init_net.ct.sysctl_events, |
| 164 | .maxlen = sizeof(unsigned int), |
| 165 | .mode = 0644, |
| 166 | .proc_handler = proc_dointvec, |
| 167 | }, |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 168 | { |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 169 | .procname = "nf_conntrack_events_retry_timeout", |
| 170 | .data = &init_net.ct.sysctl_events_retry_timeout, |
| 171 | .maxlen = sizeof(unsigned int), |
| 172 | .mode = 0644, |
| 173 | .proc_handler = proc_dointvec_jiffies, |
| 174 | }, |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 175 | {} |
| 176 | }; |
| 177 | #endif /* CONFIG_SYSCTL */ |
| 178 | |
| 179 | static struct nf_ct_ext_type event_extend __read_mostly = { |
| 180 | .len = sizeof(struct nf_conntrack_ecache), |
| 181 | .align = __alignof__(struct nf_conntrack_ecache), |
| 182 | .id = NF_CT_EXT_ECACHE, |
| 183 | }; |
| 184 | |
| 185 | #ifdef CONFIG_SYSCTL |
| 186 | static int nf_conntrack_event_init_sysctl(struct net *net) |
| 187 | { |
| 188 | struct ctl_table *table; |
| 189 | |
| 190 | table = kmemdup(event_sysctl_table, sizeof(event_sysctl_table), |
| 191 | GFP_KERNEL); |
| 192 | if (!table) |
| 193 | goto out; |
| 194 | |
| 195 | table[0].data = &net->ct.sysctl_events; |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 196 | table[1].data = &net->ct.sysctl_events_retry_timeout; |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 197 | |
| 198 | net->ct.event_sysctl_header = |
| 199 | register_net_sysctl_table(net, |
| 200 | nf_net_netfilter_sysctl_path, table); |
| 201 | if (!net->ct.event_sysctl_header) { |
| 202 | printk(KERN_ERR "nf_ct_event: can't register to sysctl.\n"); |
| 203 | goto out_register; |
| 204 | } |
| 205 | return 0; |
| 206 | |
| 207 | out_register: |
| 208 | kfree(table); |
| 209 | out: |
| 210 | return -ENOMEM; |
| 211 | } |
| 212 | |
| 213 | static void nf_conntrack_event_fini_sysctl(struct net *net) |
| 214 | { |
| 215 | struct ctl_table *table; |
| 216 | |
| 217 | table = net->ct.event_sysctl_header->ctl_table_arg; |
| 218 | unregister_net_sysctl_table(net->ct.event_sysctl_header); |
| 219 | kfree(table); |
| 220 | } |
| 221 | #else |
| 222 | static int nf_conntrack_event_init_sysctl(struct net *net) |
| 223 | { |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static void nf_conntrack_event_fini_sysctl(struct net *net) |
| 228 | { |
| 229 | } |
| 230 | #endif /* CONFIG_SYSCTL */ |
| 231 | |
| 232 | int nf_conntrack_ecache_init(struct net *net) |
| 233 | { |
| 234 | int ret; |
| 235 | |
| 236 | net->ct.sysctl_events = nf_ct_events; |
Pablo Neira Ayuso | dd7669a | 2009-06-13 12:30:52 +0200 | [diff] [blame] | 237 | net->ct.sysctl_events_retry_timeout = nf_ct_events_retry_timeout; |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 238 | |
| 239 | if (net_eq(net, &init_net)) { |
| 240 | ret = nf_ct_extend_register(&event_extend); |
| 241 | if (ret < 0) { |
| 242 | printk(KERN_ERR "nf_ct_event: Unable to register " |
| 243 | "event extension.\n"); |
| 244 | goto out_extend_register; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | ret = nf_conntrack_event_init_sysctl(net); |
| 249 | if (ret < 0) |
| 250 | goto out_sysctl; |
| 251 | |
| 252 | return 0; |
| 253 | |
| 254 | out_sysctl: |
| 255 | if (net_eq(net, &init_net)) |
| 256 | nf_ct_extend_unregister(&event_extend); |
| 257 | out_extend_register: |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | void nf_conntrack_ecache_fini(struct net *net) |
| 262 | { |
| 263 | nf_conntrack_event_fini_sysctl(net); |
| 264 | if (net_eq(net, &init_net)) |
| 265 | nf_ct_extend_unregister(&event_extend); |
| 266 | } |