Skip to content

Commit 039b40e

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nf_queue: only call synchronize_net twice if nf_queue is active
nf_unregister_net_hook(s) can avoid a second call to synchronize_net, provided there is no nfqueue active in that net namespace (which is the common case). This also gets rid of the extra arg to nf_queue_nf_hook_drop(), normally this gets called during netns cleanup so no packets should be queued. For the rare case of base chain being unregistered or module removal while nfqueue is in use the extra hiccup due to the packet drops isn't a big deal. Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent c83fa19 commit 039b40e

5 files changed

Lines changed: 27 additions & 24 deletions

File tree

‎include/net/netfilter/nf_queue.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ struct nf_queue_entry {
2424
struct nf_queue_handler {
2525
int (*outfn)(struct nf_queue_entry *entry,
2626
unsigned int queuenum);
27-
void (*nf_hook_drop)(struct net *net,
28-
const struct nf_hook_entry *hooks);
27+
unsigned int (*nf_hook_drop)(struct net *net);
2928
};
3029

3130
void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh);

‎net/netfilter/core.c‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,17 @@ __nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
162162
void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
163163
{
164164
struct nf_hook_entry *p = __nf_unregister_net_hook(net, reg);
165+
unsigned int nfq;
165166

166167
if (!p)
167168
return;
168169

169170
synchronize_net();
170-
nf_queue_nf_hook_drop(net, p);
171+
171172
/* other cpu might still process nfqueue verdict that used reg */
172-
synchronize_net();
173+
nfq = nf_queue_nf_hook_drop(net);
174+
if (nfq)
175+
synchronize_net();
173176
kfree(p);
174177
}
175178
EXPORT_SYMBOL(nf_unregister_net_hook);
@@ -198,7 +201,7 @@ void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
198201
unsigned int hookcount)
199202
{
200203
struct nf_hook_entry *to_free[16];
201-
unsigned int i, n;
204+
unsigned int i, n, nfq;
202205

203206
do {
204207
n = min_t(unsigned int, hookcount, ARRAY_SIZE(to_free));
@@ -208,12 +211,12 @@ void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
208211

209212
synchronize_net();
210213

211-
for (i = 0; i < n; i++) {
212-
if (to_free[i])
213-
nf_queue_nf_hook_drop(net, to_free[i]);
214-
}
215-
216-
synchronize_net();
214+
/* need 2nd synchronize_net() if nfqueue is used, skb
215+
* can get reinjected right before nf_queue_hook_drop()
216+
*/
217+
nfq = nf_queue_nf_hook_drop(net);
218+
if (nfq)
219+
synchronize_net();
217220

218221
for (i = 0; i < n; i++)
219222
kfree(to_free[i]);

‎net/netfilter/nf_internals.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/* nf_queue.c */
1515
int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
1616
struct nf_hook_entry **entryp, unsigned int verdict);
17-
void nf_queue_nf_hook_drop(struct net *net, const struct nf_hook_entry *entry);
17+
unsigned int nf_queue_nf_hook_drop(struct net *net);
1818
int __init netfilter_queue_init(void);
1919

2020
/* nf_log.c */

‎net/netfilter/nf_queue.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,18 @@ void nf_queue_entry_get_refs(struct nf_queue_entry *entry)
9696
}
9797
EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
9898

99-
void nf_queue_nf_hook_drop(struct net *net, const struct nf_hook_entry *entry)
99+
unsigned int nf_queue_nf_hook_drop(struct net *net)
100100
{
101101
const struct nf_queue_handler *qh;
102+
unsigned int count = 0;
102103

103104
rcu_read_lock();
104105
qh = rcu_dereference(net->nf.queue_handler);
105106
if (qh)
106-
qh->nf_hook_drop(net, entry);
107+
count = qh->nf_hook_drop(net);
107108
rcu_read_unlock();
109+
110+
return count;
108111
}
109112

110113
static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,

‎net/netfilter/nfnetlink_queue.c‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -922,27 +922,25 @@ static struct notifier_block nfqnl_dev_notifier = {
922922
.notifier_call = nfqnl_rcv_dev_event,
923923
};
924924

925-
static int nf_hook_cmp(struct nf_queue_entry *entry, unsigned long entry_ptr)
926-
{
927-
return rcu_access_pointer(entry->hook) ==
928-
(struct nf_hook_entry *)entry_ptr;
929-
}
930-
931-
static void nfqnl_nf_hook_drop(struct net *net,
932-
const struct nf_hook_entry *hook)
925+
static unsigned int nfqnl_nf_hook_drop(struct net *net)
933926
{
934927
struct nfnl_queue_net *q = nfnl_queue_pernet(net);
928+
unsigned int instances = 0;
935929
int i;
936930

937931
rcu_read_lock();
938932
for (i = 0; i < INSTANCE_BUCKETS; i++) {
939933
struct nfqnl_instance *inst;
940934
struct hlist_head *head = &q->instance_table[i];
941935

942-
hlist_for_each_entry_rcu(inst, head, hlist)
943-
nfqnl_flush(inst, nf_hook_cmp, (unsigned long)hook);
936+
hlist_for_each_entry_rcu(inst, head, hlist) {
937+
nfqnl_flush(inst, NULL, 0);
938+
instances++;
939+
}
944940
}
945941
rcu_read_unlock();
942+
943+
return instances;
946944
}
947945

948946
static int

0 commit comments

Comments
 (0)