Commit 585426fd authored by Yasuyuki Kozakai's avatar Yasuyuki Kozakai Committed by David S. Miller

[NETFILTER]: nf_queue: Use RCU and mutex for queue handlers

Queue handlers are registered/unregistered in only process context.
Signed-off-by: default avatarYasuyuki Kozakai <yasuyuki.kozakai@toshiba.co.jp>
Signed-off-by: default avatarPatrick McHardy <kaber@trash.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent ce7663d8
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
*/ */
static struct nf_queue_handler *queue_handler[NPROTO]; static struct nf_queue_handler *queue_handler[NPROTO];
static DEFINE_RWLOCK(queue_handler_lock); static DEFINE_MUTEX(queue_handler_mutex);
/* return EBUSY when somebody else is registered, return EEXIST if the /* return EBUSY when somebody else is registered, return EEXIST if the
* same handler is registered, return 0 in case of success. */ * same handler is registered, return 0 in case of success. */
...@@ -28,16 +28,16 @@ int nf_register_queue_handler(int pf, struct nf_queue_handler *qh) ...@@ -28,16 +28,16 @@ int nf_register_queue_handler(int pf, struct nf_queue_handler *qh)
if (pf >= NPROTO) if (pf >= NPROTO)
return -EINVAL; return -EINVAL;
write_lock_bh(&queue_handler_lock); mutex_lock(&queue_handler_mutex);
if (queue_handler[pf] == qh) if (queue_handler[pf] == qh)
ret = -EEXIST; ret = -EEXIST;
else if (queue_handler[pf]) else if (queue_handler[pf])
ret = -EBUSY; ret = -EBUSY;
else { else {
queue_handler[pf] = qh; rcu_assign_pointer(queue_handler[pf], qh);
ret = 0; ret = 0;
} }
write_unlock_bh(&queue_handler_lock); mutex_unlock(&queue_handler_mutex);
return ret; return ret;
} }
...@@ -49,14 +49,16 @@ int nf_unregister_queue_handler(int pf, struct nf_queue_handler *qh) ...@@ -49,14 +49,16 @@ int nf_unregister_queue_handler(int pf, struct nf_queue_handler *qh)
if (pf >= NPROTO) if (pf >= NPROTO)
return -EINVAL; return -EINVAL;
write_lock_bh(&queue_handler_lock); mutex_lock(&queue_handler_mutex);
if (queue_handler[pf] != qh) { if (queue_handler[pf] != qh) {
write_unlock_bh(&queue_handler_lock); mutex_unlock(&queue_handler_mutex);
return -EINVAL; return -EINVAL;
} }
queue_handler[pf] = NULL; rcu_assign_pointer(queue_handler[pf], NULL);
write_unlock_bh(&queue_handler_lock); mutex_unlock(&queue_handler_mutex);
synchronize_rcu();
return 0; return 0;
} }
...@@ -66,12 +68,14 @@ void nf_unregister_queue_handlers(struct nf_queue_handler *qh) ...@@ -66,12 +68,14 @@ void nf_unregister_queue_handlers(struct nf_queue_handler *qh)
{ {
int pf; int pf;
write_lock_bh(&queue_handler_lock); mutex_lock(&queue_handler_mutex);
for (pf = 0; pf < NPROTO; pf++) { for (pf = 0; pf < NPROTO; pf++) {
if (queue_handler[pf] == qh) if (queue_handler[pf] == qh)
queue_handler[pf] = NULL; rcu_assign_pointer(queue_handler[pf], NULL);
} }
write_unlock_bh(&queue_handler_lock); mutex_unlock(&queue_handler_mutex);
synchronize_rcu();
} }
EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers); EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
...@@ -94,18 +98,21 @@ static int __nf_queue(struct sk_buff *skb, ...@@ -94,18 +98,21 @@ static int __nf_queue(struct sk_buff *skb,
struct net_device *physoutdev = NULL; struct net_device *physoutdev = NULL;
#endif #endif
struct nf_afinfo *afinfo; struct nf_afinfo *afinfo;
struct nf_queue_handler *qh;
/* QUEUE == DROP if noone is waiting, to be safe. */ /* QUEUE == DROP if noone is waiting, to be safe. */
read_lock(&queue_handler_lock); rcu_read_lock();
if (!queue_handler[pf]) {
read_unlock(&queue_handler_lock); qh = rcu_dereference(queue_handler[pf]);
if (!qh) {
rcu_read_unlock();
kfree_skb(skb); kfree_skb(skb);
return 1; return 1;
} }
afinfo = nf_get_afinfo(pf); afinfo = nf_get_afinfo(pf);
if (!afinfo) { if (!afinfo) {
read_unlock(&queue_handler_lock); rcu_read_unlock();
kfree_skb(skb); kfree_skb(skb);
return 1; return 1;
} }
...@@ -115,7 +122,7 @@ static int __nf_queue(struct sk_buff *skb, ...@@ -115,7 +122,7 @@ static int __nf_queue(struct sk_buff *skb,
if (net_ratelimit()) if (net_ratelimit())
printk(KERN_ERR "OOM queueing packet %p\n", printk(KERN_ERR "OOM queueing packet %p\n",
skb); skb);
read_unlock(&queue_handler_lock); rcu_read_unlock();
kfree_skb(skb); kfree_skb(skb);
return 1; return 1;
} }
...@@ -125,7 +132,7 @@ static int __nf_queue(struct sk_buff *skb, ...@@ -125,7 +132,7 @@ static int __nf_queue(struct sk_buff *skb,
/* If it's going away, ignore hook. */ /* If it's going away, ignore hook. */
if (!try_module_get(info->elem->owner)) { if (!try_module_get(info->elem->owner)) {
read_unlock(&queue_handler_lock); rcu_read_unlock();
kfree(info); kfree(info);
return 0; return 0;
} }
...@@ -143,10 +150,9 @@ static int __nf_queue(struct sk_buff *skb, ...@@ -143,10 +150,9 @@ static int __nf_queue(struct sk_buff *skb,
} }
#endif #endif
afinfo->saveroute(skb, info); afinfo->saveroute(skb, info);
status = queue_handler[pf]->outfn(skb, info, queuenum, status = qh->outfn(skb, info, queuenum, qh->data);
queue_handler[pf]->data);
read_unlock(&queue_handler_lock); rcu_read_unlock();
if (status < 0) { if (status < 0) {
/* James M doesn't say fuck enough. */ /* James M doesn't say fuck enough. */
...@@ -313,13 +319,13 @@ static int seq_show(struct seq_file *s, void *v) ...@@ -313,13 +319,13 @@ static int seq_show(struct seq_file *s, void *v)
loff_t *pos = v; loff_t *pos = v;
struct nf_queue_handler *qh; struct nf_queue_handler *qh;
read_lock_bh(&queue_handler_lock); rcu_read_lock();
qh = queue_handler[*pos]; qh = rcu_dereference(queue_handler[*pos]);
if (!qh) if (!qh)
ret = seq_printf(s, "%2lld NONE\n", *pos); ret = seq_printf(s, "%2lld NONE\n", *pos);
else else
ret = seq_printf(s, "%2lld %s\n", *pos, qh->name); ret = seq_printf(s, "%2lld %s\n", *pos, qh->name);
read_unlock_bh(&queue_handler_lock); rcu_read_unlock();
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment