Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 1 | /* Event cache for netfilter. */ |
| 2 | |
Patrick McHardy | f229f6c | 2013-04-06 15:24:29 +0200 | [diff] [blame] | 3 | /* |
| 4 | * (C) 2005 Harald Welte <laforge@gnumonks.org> |
| 5 | * (C) 2005 Patrick McHardy <kaber@trash.net> |
| 6 | * (C) 2005-2006 Netfilter Core Team <coreteam@netfilter.org> |
| 7 | * (C) 2005 USAGI/WIDE Project <http://www.linux-ipv6.org> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/netfilter.h> |
| 16 | #include <linux/skbuff.h> |
| 17 | #include <linux/vmalloc.h> |
| 18 | #include <linux/stddef.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/percpu.h> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 21 | #include <linux/kernel.h> |
| 22 | #include <linux/netdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 24 | #include <linux/export.h> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 25 | |
| 26 | #include <net/netfilter/nf_conntrack.h> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 27 | #include <net/netfilter/nf_conntrack_core.h> |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 28 | #include <net/netfilter/nf_conntrack_extend.h> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 29 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 30 | static DEFINE_MUTEX(nf_ct_ecache_mutex); |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 31 | |
Florian Westphal | 9500507 | 2014-06-10 23:12:56 +0200 | [diff] [blame] | 32 | #define ECACHE_RETRY_WAIT (HZ/10) |
| 33 | |
| 34 | enum retry_state { |
| 35 | STATE_CONGESTED, |
| 36 | STATE_RESTART, |
| 37 | STATE_DONE, |
| 38 | }; |
| 39 | |
| 40 | static enum retry_state ecache_work_evict_list(struct ct_pcpu *pcpu) |
| 41 | { |
| 42 | struct nf_conn *refs[16]; |
| 43 | struct nf_conntrack_tuple_hash *h; |
| 44 | struct hlist_nulls_node *n; |
| 45 | unsigned int evicted = 0; |
| 46 | enum retry_state ret = STATE_DONE; |
| 47 | |
| 48 | spin_lock(&pcpu->lock); |
| 49 | |
| 50 | hlist_nulls_for_each_entry(h, n, &pcpu->dying, hnnode) { |
| 51 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); |
Florian Westphal | 616b14b | 2016-08-25 15:33:30 +0200 | [diff] [blame] | 52 | struct nf_conntrack_ecache *e; |
Florian Westphal | 9500507 | 2014-06-10 23:12:56 +0200 | [diff] [blame] | 53 | |
Florian Westphal | 616b14b | 2016-08-25 15:33:30 +0200 | [diff] [blame] | 54 | if (!nf_ct_is_confirmed(ct)) |
| 55 | continue; |
| 56 | |
| 57 | e = nf_ct_ecache_find(ct); |
| 58 | if (!e || e->state != NFCT_ECACHE_DESTROY_FAIL) |
Florian Westphal | 9500507 | 2014-06-10 23:12:56 +0200 | [diff] [blame] | 59 | continue; |
| 60 | |
| 61 | if (nf_conntrack_event(IPCT_DESTROY, ct)) { |
| 62 | ret = STATE_CONGESTED; |
| 63 | break; |
| 64 | } |
| 65 | |
Florian Westphal | 616b14b | 2016-08-25 15:33:30 +0200 | [diff] [blame] | 66 | e->state = NFCT_ECACHE_DESTROY_SENT; |
Florian Westphal | 9500507 | 2014-06-10 23:12:56 +0200 | [diff] [blame] | 67 | refs[evicted] = ct; |
| 68 | |
| 69 | if (++evicted >= ARRAY_SIZE(refs)) { |
| 70 | ret = STATE_RESTART; |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | spin_unlock(&pcpu->lock); |
| 76 | |
| 77 | /* can't _put while holding lock */ |
| 78 | while (evicted) |
| 79 | nf_ct_put(refs[--evicted]); |
| 80 | |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | static void ecache_work(struct work_struct *work) |
| 85 | { |
| 86 | struct netns_ct *ctnet = |
| 87 | container_of(work, struct netns_ct, ecache_dwork.work); |
| 88 | int cpu, delay = -1; |
| 89 | struct ct_pcpu *pcpu; |
| 90 | |
| 91 | local_bh_disable(); |
| 92 | |
| 93 | for_each_possible_cpu(cpu) { |
| 94 | enum retry_state ret; |
| 95 | |
| 96 | pcpu = per_cpu_ptr(ctnet->pcpu_lists, cpu); |
| 97 | |
| 98 | ret = ecache_work_evict_list(pcpu); |
| 99 | |
| 100 | switch (ret) { |
| 101 | case STATE_CONGESTED: |
| 102 | delay = ECACHE_RETRY_WAIT; |
| 103 | goto out; |
| 104 | case STATE_RESTART: |
| 105 | delay = 0; |
| 106 | break; |
| 107 | case STATE_DONE: |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | out: |
| 113 | local_bh_enable(); |
| 114 | |
| 115 | ctnet->ecache_dwork_pending = delay > 0; |
| 116 | if (delay >= 0) |
| 117 | schedule_delayed_work(&ctnet->ecache_dwork, delay); |
| 118 | } |
| 119 | |
Florian Westphal | 3c435e2 | 2016-04-11 21:52:35 +0200 | [diff] [blame] | 120 | int nf_conntrack_eventmask_report(unsigned int eventmask, struct nf_conn *ct, |
| 121 | u32 portid, int report) |
| 122 | { |
| 123 | int ret = 0; |
| 124 | struct net *net = nf_ct_net(ct); |
| 125 | struct nf_ct_event_notifier *notify; |
| 126 | struct nf_conntrack_ecache *e; |
| 127 | |
| 128 | rcu_read_lock(); |
| 129 | notify = rcu_dereference(net->ct.nf_conntrack_event_cb); |
| 130 | if (!notify) |
| 131 | goto out_unlock; |
| 132 | |
| 133 | e = nf_ct_ecache_find(ct); |
| 134 | if (!e) |
| 135 | goto out_unlock; |
| 136 | |
Florian Westphal | 616b14b | 2016-08-25 15:33:30 +0200 | [diff] [blame] | 137 | if (nf_ct_is_confirmed(ct)) { |
Florian Westphal | 3c435e2 | 2016-04-11 21:52:35 +0200 | [diff] [blame] | 138 | struct nf_ct_event item = { |
| 139 | .ct = ct, |
| 140 | .portid = e->portid ? e->portid : portid, |
| 141 | .report = report |
| 142 | }; |
| 143 | /* This is a resent of a destroy event? If so, skip missed */ |
| 144 | unsigned long missed = e->portid ? 0 : e->missed; |
| 145 | |
| 146 | if (!((eventmask | missed) & e->ctmask)) |
| 147 | goto out_unlock; |
| 148 | |
| 149 | ret = notify->fcn(eventmask | missed, &item); |
| 150 | if (unlikely(ret < 0 || missed)) { |
| 151 | spin_lock_bh(&ct->lock); |
| 152 | if (ret < 0) { |
| 153 | /* This is a destroy event that has been |
| 154 | * triggered by a process, we store the PORTID |
| 155 | * to include it in the retransmission. |
| 156 | */ |
Florian Westphal | 616b14b | 2016-08-25 15:33:30 +0200 | [diff] [blame] | 157 | if (eventmask & (1 << IPCT_DESTROY)) { |
| 158 | if (e->portid == 0 && portid != 0) |
| 159 | e->portid = portid; |
| 160 | e->state = NFCT_ECACHE_DESTROY_FAIL; |
| 161 | } else { |
Florian Westphal | 3c435e2 | 2016-04-11 21:52:35 +0200 | [diff] [blame] | 162 | e->missed |= eventmask; |
Florian Westphal | 616b14b | 2016-08-25 15:33:30 +0200 | [diff] [blame] | 163 | } |
Florian Westphal | 3c435e2 | 2016-04-11 21:52:35 +0200 | [diff] [blame] | 164 | } else { |
| 165 | e->missed &= ~missed; |
| 166 | } |
| 167 | spin_unlock_bh(&ct->lock); |
| 168 | } |
| 169 | } |
| 170 | out_unlock: |
| 171 | rcu_read_unlock(); |
| 172 | return ret; |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report); |
| 175 | |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 176 | /* deliver cached events and clear cache entry - must be called with locally |
| 177 | * disabled softirqs */ |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 178 | void nf_ct_deliver_cached_events(struct nf_conn *ct) |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 179 | { |
Pablo Neira Ayuso | 70e9942 | 2011-11-22 00:16:51 +0100 | [diff] [blame] | 180 | struct net *net = nf_ct_net(ct); |
Tony Zelenoff | 58020f7 | 2012-02-22 10:48:01 +0400 | [diff] [blame] | 181 | unsigned long events, missed; |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 182 | struct nf_ct_event_notifier *notify; |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 183 | struct nf_conntrack_ecache *e; |
Tony Zelenoff | 58020f7 | 2012-02-22 10:48:01 +0400 | [diff] [blame] | 184 | struct nf_ct_event item; |
| 185 | int ret; |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 186 | |
| 187 | rcu_read_lock(); |
Pablo Neira Ayuso | 70e9942 | 2011-11-22 00:16:51 +0100 | [diff] [blame] | 188 | notify = rcu_dereference(net->ct.nf_conntrack_event_cb); |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 189 | if (notify == NULL) |
| 190 | goto out_unlock; |
| 191 | |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 192 | e = nf_ct_ecache_find(ct); |
| 193 | if (e == NULL) |
| 194 | goto out_unlock; |
| 195 | |
| 196 | events = xchg(&e->cache, 0); |
| 197 | |
Tony Zelenoff | 58020f7 | 2012-02-22 10:48:01 +0400 | [diff] [blame] | 198 | if (!nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct) || !events) |
| 199 | goto out_unlock; |
Pablo Neira Ayuso | 19abb7b | 2008-11-18 11:56:20 +0100 | [diff] [blame] | 200 | |
Tony Zelenoff | 58020f7 | 2012-02-22 10:48:01 +0400 | [diff] [blame] | 201 | /* We make a copy of the missed event cache without taking |
| 202 | * the lock, thus we may send missed events twice. However, |
| 203 | * this does not harm and it happens very rarely. */ |
| 204 | missed = e->missed; |
Pablo Neira Ayuso | 3db7e93 | 2011-02-01 16:06:30 +0100 | [diff] [blame] | 205 | |
Tony Zelenoff | 58020f7 | 2012-02-22 10:48:01 +0400 | [diff] [blame] | 206 | if (!((events | missed) & e->ctmask)) |
| 207 | goto out_unlock; |
| 208 | |
| 209 | item.ct = ct; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 210 | item.portid = 0; |
Tony Zelenoff | 58020f7 | 2012-02-22 10:48:01 +0400 | [diff] [blame] | 211 | item.report = 0; |
| 212 | |
| 213 | ret = notify->fcn(events | missed, &item); |
| 214 | |
| 215 | if (likely(ret >= 0 && !missed)) |
| 216 | goto out_unlock; |
| 217 | |
| 218 | spin_lock_bh(&ct->lock); |
| 219 | if (ret < 0) |
| 220 | e->missed |= events; |
| 221 | else |
| 222 | e->missed &= ~missed; |
| 223 | spin_unlock_bh(&ct->lock); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 224 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 225 | out_unlock: |
| 226 | rcu_read_unlock(); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 227 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 228 | EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events); |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 229 | |
Florian Westphal | ecdfb48 | 2016-04-11 21:52:36 +0200 | [diff] [blame] | 230 | void nf_ct_expect_event_report(enum ip_conntrack_expect_events event, |
| 231 | struct nf_conntrack_expect *exp, |
| 232 | u32 portid, int report) |
| 233 | |
| 234 | { |
| 235 | struct net *net = nf_ct_exp_net(exp); |
| 236 | struct nf_exp_event_notifier *notify; |
| 237 | struct nf_conntrack_ecache *e; |
| 238 | |
| 239 | rcu_read_lock(); |
| 240 | notify = rcu_dereference(net->ct.nf_expect_event_cb); |
| 241 | if (!notify) |
| 242 | goto out_unlock; |
| 243 | |
| 244 | e = nf_ct_ecache_find(exp->master); |
| 245 | if (!e) |
| 246 | goto out_unlock; |
| 247 | |
| 248 | if (e->expmask & (1 << event)) { |
| 249 | struct nf_exp_event item = { |
| 250 | .exp = exp, |
| 251 | .portid = portid, |
| 252 | .report = report |
| 253 | }; |
| 254 | notify->fcn(1 << event, &item); |
| 255 | } |
| 256 | out_unlock: |
| 257 | rcu_read_unlock(); |
| 258 | } |
| 259 | |
Pablo Neira Ayuso | 70e9942 | 2011-11-22 00:16:51 +0100 | [diff] [blame] | 260 | int nf_conntrack_register_notifier(struct net *net, |
| 261 | struct nf_ct_event_notifier *new) |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 262 | { |
Tony Zelenoff | 031d770 | 2012-03-08 23:35:39 +0000 | [diff] [blame] | 263 | int ret; |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 264 | struct nf_ct_event_notifier *notify; |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 265 | |
| 266 | mutex_lock(&nf_ct_ecache_mutex); |
Pablo Neira Ayuso | 70e9942 | 2011-11-22 00:16:51 +0100 | [diff] [blame] | 267 | notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb, |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 268 | lockdep_is_held(&nf_ct_ecache_mutex)); |
| 269 | if (notify != NULL) { |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 270 | ret = -EBUSY; |
| 271 | goto out_unlock; |
| 272 | } |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 273 | rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new); |
Tony Zelenoff | 031d770 | 2012-03-08 23:35:39 +0000 | [diff] [blame] | 274 | ret = 0; |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 275 | |
| 276 | out_unlock: |
| 277 | mutex_unlock(&nf_ct_ecache_mutex); |
| 278 | return ret; |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 279 | } |
| 280 | EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier); |
| 281 | |
Pablo Neira Ayuso | 70e9942 | 2011-11-22 00:16:51 +0100 | [diff] [blame] | 282 | void nf_conntrack_unregister_notifier(struct net *net, |
| 283 | struct nf_ct_event_notifier *new) |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 284 | { |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 285 | struct nf_ct_event_notifier *notify; |
| 286 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 287 | mutex_lock(&nf_ct_ecache_mutex); |
Pablo Neira Ayuso | 70e9942 | 2011-11-22 00:16:51 +0100 | [diff] [blame] | 288 | notify = rcu_dereference_protected(net->ct.nf_conntrack_event_cb, |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 289 | lockdep_is_held(&nf_ct_ecache_mutex)); |
| 290 | BUG_ON(notify != new); |
Pablo Neira Ayuso | 70e9942 | 2011-11-22 00:16:51 +0100 | [diff] [blame] | 291 | RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL); |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 292 | mutex_unlock(&nf_ct_ecache_mutex); |
Liping Zhang | 146561a | 2017-03-25 08:53:12 +0800 | [diff] [blame] | 293 | /* synchronize_rcu() is called from ctnetlink_exit. */ |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 294 | } |
| 295 | EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier); |
| 296 | |
Pablo Neira Ayuso | 70e9942 | 2011-11-22 00:16:51 +0100 | [diff] [blame] | 297 | int nf_ct_expect_register_notifier(struct net *net, |
| 298 | struct nf_exp_event_notifier *new) |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 299 | { |
Tony Zelenoff | 031d770 | 2012-03-08 23:35:39 +0000 | [diff] [blame] | 300 | int ret; |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 301 | struct nf_exp_event_notifier *notify; |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 302 | |
| 303 | mutex_lock(&nf_ct_ecache_mutex); |
Pablo Neira Ayuso | 70e9942 | 2011-11-22 00:16:51 +0100 | [diff] [blame] | 304 | notify = rcu_dereference_protected(net->ct.nf_expect_event_cb, |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 305 | lockdep_is_held(&nf_ct_ecache_mutex)); |
| 306 | if (notify != NULL) { |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 307 | ret = -EBUSY; |
| 308 | goto out_unlock; |
| 309 | } |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 310 | rcu_assign_pointer(net->ct.nf_expect_event_cb, new); |
Tony Zelenoff | 031d770 | 2012-03-08 23:35:39 +0000 | [diff] [blame] | 311 | ret = 0; |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 312 | |
| 313 | out_unlock: |
| 314 | mutex_unlock(&nf_ct_ecache_mutex); |
| 315 | return ret; |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 316 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 317 | EXPORT_SYMBOL_GPL(nf_ct_expect_register_notifier); |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 318 | |
Pablo Neira Ayuso | 70e9942 | 2011-11-22 00:16:51 +0100 | [diff] [blame] | 319 | void nf_ct_expect_unregister_notifier(struct net *net, |
| 320 | struct nf_exp_event_notifier *new) |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 321 | { |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 322 | struct nf_exp_event_notifier *notify; |
| 323 | |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 324 | mutex_lock(&nf_ct_ecache_mutex); |
Pablo Neira Ayuso | 70e9942 | 2011-11-22 00:16:51 +0100 | [diff] [blame] | 325 | notify = rcu_dereference_protected(net->ct.nf_expect_event_cb, |
Patrick McHardy | b56f2d5 | 2010-05-10 18:47:57 +0200 | [diff] [blame] | 326 | lockdep_is_held(&nf_ct_ecache_mutex)); |
| 327 | BUG_ON(notify != new); |
Pablo Neira Ayuso | 70e9942 | 2011-11-22 00:16:51 +0100 | [diff] [blame] | 328 | RCU_INIT_POINTER(net->ct.nf_expect_event_cb, NULL); |
Pablo Neira Ayuso | e34d5c1 | 2009-06-03 10:32:06 +0200 | [diff] [blame] | 329 | mutex_unlock(&nf_ct_ecache_mutex); |
Liping Zhang | 146561a | 2017-03-25 08:53:12 +0800 | [diff] [blame] | 330 | /* synchronize_rcu() is called from ctnetlink_exit. */ |
Patrick McHardy | 010c7d6 | 2007-03-14 16:40:10 -0700 | [diff] [blame] | 331 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 332 | EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier); |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 333 | |
| 334 | #define NF_CT_EVENTS_DEFAULT 1 |
| 335 | static int nf_ct_events __read_mostly = NF_CT_EVENTS_DEFAULT; |
| 336 | |
| 337 | #ifdef CONFIG_SYSCTL |
| 338 | static struct ctl_table event_sysctl_table[] = { |
| 339 | { |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 340 | .procname = "nf_conntrack_events", |
| 341 | .data = &init_net.ct.sysctl_events, |
| 342 | .maxlen = sizeof(unsigned int), |
| 343 | .mode = 0644, |
| 344 | .proc_handler = proc_dointvec, |
| 345 | }, |
| 346 | {} |
| 347 | }; |
| 348 | #endif /* CONFIG_SYSCTL */ |
| 349 | |
| 350 | static struct nf_ct_ext_type event_extend __read_mostly = { |
| 351 | .len = sizeof(struct nf_conntrack_ecache), |
| 352 | .align = __alignof__(struct nf_conntrack_ecache), |
| 353 | .id = NF_CT_EXT_ECACHE, |
| 354 | }; |
| 355 | |
| 356 | #ifdef CONFIG_SYSCTL |
| 357 | static int nf_conntrack_event_init_sysctl(struct net *net) |
| 358 | { |
| 359 | struct ctl_table *table; |
| 360 | |
| 361 | table = kmemdup(event_sysctl_table, sizeof(event_sysctl_table), |
| 362 | GFP_KERNEL); |
| 363 | if (!table) |
| 364 | goto out; |
| 365 | |
| 366 | table[0].data = &net->ct.sysctl_events; |
| 367 | |
Eric W. Biederman | 464dc80 | 2012-11-16 03:02:59 +0000 | [diff] [blame] | 368 | /* Don't export sysctls to unprivileged users */ |
| 369 | if (net->user_ns != &init_user_ns) |
| 370 | table[0].procname = NULL; |
| 371 | |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 372 | net->ct.event_sysctl_header = |
Eric W. Biederman | ec8f23c | 2012-04-19 13:44:49 +0000 | [diff] [blame] | 373 | register_net_sysctl(net, "net/netfilter", table); |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 374 | if (!net->ct.event_sysctl_header) { |
| 375 | printk(KERN_ERR "nf_ct_event: can't register to sysctl.\n"); |
| 376 | goto out_register; |
| 377 | } |
| 378 | return 0; |
| 379 | |
| 380 | out_register: |
| 381 | kfree(table); |
| 382 | out: |
| 383 | return -ENOMEM; |
| 384 | } |
| 385 | |
| 386 | static void nf_conntrack_event_fini_sysctl(struct net *net) |
| 387 | { |
| 388 | struct ctl_table *table; |
| 389 | |
| 390 | table = net->ct.event_sysctl_header->ctl_table_arg; |
| 391 | unregister_net_sysctl_table(net->ct.event_sysctl_header); |
| 392 | kfree(table); |
| 393 | } |
| 394 | #else |
| 395 | static int nf_conntrack_event_init_sysctl(struct net *net) |
| 396 | { |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static void nf_conntrack_event_fini_sysctl(struct net *net) |
| 401 | { |
| 402 | } |
| 403 | #endif /* CONFIG_SYSCTL */ |
| 404 | |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 405 | int nf_conntrack_ecache_pernet_init(struct net *net) |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 406 | { |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 407 | net->ct.sysctl_events = nf_ct_events; |
Florian Westphal | 9500507 | 2014-06-10 23:12:56 +0200 | [diff] [blame] | 408 | INIT_DELAYED_WORK(&net->ct.ecache_dwork, ecache_work); |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 409 | return nf_conntrack_event_init_sysctl(net); |
| 410 | } |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 411 | |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 412 | void nf_conntrack_ecache_pernet_fini(struct net *net) |
| 413 | { |
Florian Westphal | 9500507 | 2014-06-10 23:12:56 +0200 | [diff] [blame] | 414 | cancel_delayed_work_sync(&net->ct.ecache_dwork); |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 415 | nf_conntrack_event_fini_sysctl(net); |
| 416 | } |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 417 | |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 418 | int nf_conntrack_ecache_init(void) |
| 419 | { |
| 420 | int ret = nf_ct_extend_register(&event_extend); |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 421 | if (ret < 0) |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 422 | pr_err("nf_ct_event: Unable to register event extension.\n"); |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 423 | return ret; |
| 424 | } |
| 425 | |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 426 | void nf_conntrack_ecache_fini(void) |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 427 | { |
Gao feng | 3fe0f94 | 2013-01-21 22:10:28 +0000 | [diff] [blame] | 428 | nf_ct_extend_unregister(&event_extend); |
Pablo Neira Ayuso | a0891aa | 2009-06-13 12:26:29 +0200 | [diff] [blame] | 429 | } |