Skip to content

Commit 585426f

Browse files
Yasuyuki KozakaiDavid S. Miller
authored andcommitted
[NETFILTER]: nf_queue: Use RCU and mutex for queue handlers
Queue handlers are registered/unregistered in only process context. Signed-off-by: Yasuyuki Kozakai <yasuyuki.kozakai@toshiba.co.jp> Signed-off-by: Patrick McHardy <kaber@trash.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ce7663d commit 585426f

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

‎net/netfilter/nf_queue.c‎

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
static struct nf_queue_handler *queue_handler[NPROTO];
1919

20-
static DEFINE_RWLOCK(queue_handler_lock);
20+
static DEFINE_MUTEX(queue_handler_mutex);
2121

2222
/* return EBUSY when somebody else is registered, return EEXIST if the
2323
* 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)
2828
if (pf >= NPROTO)
2929
return -EINVAL;
3030

31-
write_lock_bh(&queue_handler_lock);
31+
mutex_lock(&queue_handler_mutex);
3232
if (queue_handler[pf] == qh)
3333
ret = -EEXIST;
3434
else if (queue_handler[pf])
3535
ret = -EBUSY;
3636
else {
37-
queue_handler[pf] = qh;
37+
rcu_assign_pointer(queue_handler[pf], qh);
3838
ret = 0;
3939
}
40-
write_unlock_bh(&queue_handler_lock);
40+
mutex_unlock(&queue_handler_mutex);
4141

4242
return ret;
4343
}
@@ -49,14 +49,16 @@ int nf_unregister_queue_handler(int pf, struct nf_queue_handler *qh)
4949
if (pf >= NPROTO)
5050
return -EINVAL;
5151

52-
write_lock_bh(&queue_handler_lock);
52+
mutex_lock(&queue_handler_mutex);
5353
if (queue_handler[pf] != qh) {
54-
write_unlock_bh(&queue_handler_lock);
54+
mutex_unlock(&queue_handler_mutex);
5555
return -EINVAL;
5656
}
5757

58-
queue_handler[pf] = NULL;
59-
write_unlock_bh(&queue_handler_lock);
58+
rcu_assign_pointer(queue_handler[pf], NULL);
59+
mutex_unlock(&queue_handler_mutex);
60+
61+
synchronize_rcu();
6062

6163
return 0;
6264
}
@@ -66,12 +68,14 @@ void nf_unregister_queue_handlers(struct nf_queue_handler *qh)
6668
{
6769
int pf;
6870

69-
write_lock_bh(&queue_handler_lock);
71+
mutex_lock(&queue_handler_mutex);
7072
for (pf = 0; pf < NPROTO; pf++) {
7173
if (queue_handler[pf] == qh)
72-
queue_handler[pf] = NULL;
74+
rcu_assign_pointer(queue_handler[pf], NULL);
7375
}
74-
write_unlock_bh(&queue_handler_lock);
76+
mutex_unlock(&queue_handler_mutex);
77+
78+
synchronize_rcu();
7579
}
7680
EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
7781

@@ -94,18 +98,21 @@ static int __nf_queue(struct sk_buff *skb,
9498
struct net_device *physoutdev = NULL;
9599
#endif
96100
struct nf_afinfo *afinfo;
101+
struct nf_queue_handler *qh;
97102

98103
/* QUEUE == DROP if noone is waiting, to be safe. */
99-
read_lock(&queue_handler_lock);
100-
if (!queue_handler[pf]) {
101-
read_unlock(&queue_handler_lock);
104+
rcu_read_lock();
105+
106+
qh = rcu_dereference(queue_handler[pf]);
107+
if (!qh) {
108+
rcu_read_unlock();
102109
kfree_skb(skb);
103110
return 1;
104111
}
105112

106113
afinfo = nf_get_afinfo(pf);
107114
if (!afinfo) {
108-
read_unlock(&queue_handler_lock);
115+
rcu_read_unlock();
109116
kfree_skb(skb);
110117
return 1;
111118
}
@@ -115,7 +122,7 @@ static int __nf_queue(struct sk_buff *skb,
115122
if (net_ratelimit())
116123
printk(KERN_ERR "OOM queueing packet %p\n",
117124
skb);
118-
read_unlock(&queue_handler_lock);
125+
rcu_read_unlock();
119126
kfree_skb(skb);
120127
return 1;
121128
}
@@ -125,7 +132,7 @@ static int __nf_queue(struct sk_buff *skb,
125132

126133
/* If it's going away, ignore hook. */
127134
if (!try_module_get(info->elem->owner)) {
128-
read_unlock(&queue_handler_lock);
135+
rcu_read_unlock();
129136
kfree(info);
130137
return 0;
131138
}
@@ -143,10 +150,9 @@ static int __nf_queue(struct sk_buff *skb,
143150
}
144151
#endif
145152
afinfo->saveroute(skb, info);
146-
status = queue_handler[pf]->outfn(skb, info, queuenum,
147-
queue_handler[pf]->data);
153+
status = qh->outfn(skb, info, queuenum, qh->data);
148154

149-
read_unlock(&queue_handler_lock);
155+
rcu_read_unlock();
150156

151157
if (status < 0) {
152158
/* James M doesn't say fuck enough. */
@@ -313,13 +319,13 @@ static int seq_show(struct seq_file *s, void *v)
313319
loff_t *pos = v;
314320
struct nf_queue_handler *qh;
315321

316-
read_lock_bh(&queue_handler_lock);
317-
qh = queue_handler[*pos];
322+
rcu_read_lock();
323+
qh = rcu_dereference(queue_handler[*pos]);
318324
if (!qh)
319325
ret = seq_printf(s, "%2lld NONE\n", *pos);
320326
else
321327
ret = seq_printf(s, "%2lld %s\n", *pos, qh->name);
322-
read_unlock_bh(&queue_handler_lock);
328+
rcu_read_unlock();
323329

324330
return ret;
325331
}

0 commit comments

Comments
 (0)