Skip to content

Commit c14b78e

Browse files
committed
netfilter: nfnetlink: add mutex per subsystem
This patch replaces the global lock to one lock per subsystem. The per-subsystem lock avoids that processes operating with different subsystems are synchronized. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 5474f57 commit c14b78e

4 files changed

Lines changed: 53 additions & 41 deletions

File tree

‎include/linux/netfilter/nfnetlink.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ extern int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigne
3434
extern int nfnetlink_set_err(struct net *net, u32 pid, u32 group, int error);
3535
extern int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u_int32_t pid, int flags);
3636

37-
extern void nfnl_lock(void);
38-
extern void nfnl_unlock(void);
37+
extern void nfnl_lock(__u8 subsys_id);
38+
extern void nfnl_unlock(__u8 subsys_id);
3939

4040
#define MODULE_ALIAS_NFNL_SUBSYS(subsys) \
4141
MODULE_ALIAS("nfnetlink-subsys-" __stringify(subsys))

‎net/netfilter/ipset/ip_set_core.c‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ find_set_type(const char *name, u8 family, u8 revision)
8888
static bool
8989
load_settype(const char *name)
9090
{
91-
nfnl_unlock();
91+
nfnl_unlock(NFNL_SUBSYS_IPSET);
9292
pr_debug("try to load ip_set_%s\n", name);
9393
if (request_module("ip_set_%s", name) < 0) {
9494
pr_warning("Can't find ip_set type %s\n", name);
95-
nfnl_lock();
95+
nfnl_lock(NFNL_SUBSYS_IPSET);
9696
return false;
9797
}
98-
nfnl_lock();
98+
nfnl_lock(NFNL_SUBSYS_IPSET);
9999
return true;
100100
}
101101

@@ -532,7 +532,7 @@ ip_set_nfnl_get(const char *name)
532532
ip_set_id_t i, index = IPSET_INVALID_ID;
533533
struct ip_set *s;
534534

535-
nfnl_lock();
535+
nfnl_lock(NFNL_SUBSYS_IPSET);
536536
for (i = 0; i < ip_set_max; i++) {
537537
s = nfnl_set(i);
538538
if (s != NULL && STREQ(s->name, name)) {
@@ -541,7 +541,7 @@ ip_set_nfnl_get(const char *name)
541541
break;
542542
}
543543
}
544-
nfnl_unlock();
544+
nfnl_unlock(NFNL_SUBSYS_IPSET);
545545

546546
return index;
547547
}
@@ -561,13 +561,13 @@ ip_set_nfnl_get_byindex(ip_set_id_t index)
561561
if (index > ip_set_max)
562562
return IPSET_INVALID_ID;
563563

564-
nfnl_lock();
564+
nfnl_lock(NFNL_SUBSYS_IPSET);
565565
set = nfnl_set(index);
566566
if (set)
567567
__ip_set_get(set);
568568
else
569569
index = IPSET_INVALID_ID;
570-
nfnl_unlock();
570+
nfnl_unlock(NFNL_SUBSYS_IPSET);
571571

572572
return index;
573573
}
@@ -584,11 +584,11 @@ void
584584
ip_set_nfnl_put(ip_set_id_t index)
585585
{
586586
struct ip_set *set;
587-
nfnl_lock();
587+
nfnl_lock(NFNL_SUBSYS_IPSET);
588588
set = nfnl_set(index);
589589
if (set != NULL)
590590
__ip_set_put(set);
591-
nfnl_unlock();
591+
nfnl_unlock(NFNL_SUBSYS_IPSET);
592592
}
593593
EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
594594

@@ -1763,10 +1763,10 @@ ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
17631763
goto done;
17641764
}
17651765
req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
1766-
nfnl_lock();
1766+
nfnl_lock(NFNL_SUBSYS_IPSET);
17671767
find_set_and_id(req_get->set.name, &id);
17681768
req_get->set.index = id;
1769-
nfnl_unlock();
1769+
nfnl_unlock(NFNL_SUBSYS_IPSET);
17701770
goto copy;
17711771
}
17721772
case IP_SET_OP_GET_BYINDEX: {
@@ -1778,11 +1778,11 @@ ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
17781778
ret = -EINVAL;
17791779
goto done;
17801780
}
1781-
nfnl_lock();
1781+
nfnl_lock(NFNL_SUBSYS_IPSET);
17821782
set = nfnl_set(req_get->set.index);
17831783
strncpy(req_get->set.name, set ? set->name : "",
17841784
IPSET_MAXNAMELEN);
1785-
nfnl_unlock();
1785+
nfnl_unlock(NFNL_SUBSYS_IPSET);
17861786
goto copy;
17871787
}
17881788
default:

‎net/netfilter/nf_conntrack_netlink.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,13 +1256,13 @@ ctnetlink_parse_nat_setup(struct nf_conn *ct,
12561256
if (!parse_nat_setup) {
12571257
#ifdef CONFIG_MODULES
12581258
rcu_read_unlock();
1259-
nfnl_unlock();
1259+
nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
12601260
if (request_module("nf-nat") < 0) {
1261-
nfnl_lock();
1261+
nfnl_lock(NFNL_SUBSYS_CTNETLINK);
12621262
rcu_read_lock();
12631263
return -EOPNOTSUPP;
12641264
}
1265-
nfnl_lock();
1265+
nfnl_lock(NFNL_SUBSYS_CTNETLINK);
12661266
rcu_read_lock();
12671267
if (nfnetlink_parse_nat_setup_hook)
12681268
return -EAGAIN;
@@ -1274,13 +1274,13 @@ ctnetlink_parse_nat_setup(struct nf_conn *ct,
12741274
if (err == -EAGAIN) {
12751275
#ifdef CONFIG_MODULES
12761276
rcu_read_unlock();
1277-
nfnl_unlock();
1277+
nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
12781278
if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1279-
nfnl_lock();
1279+
nfnl_lock(NFNL_SUBSYS_CTNETLINK);
12801280
rcu_read_lock();
12811281
return -EOPNOTSUPP;
12821282
}
1283-
nfnl_lock();
1283+
nfnl_lock(NFNL_SUBSYS_CTNETLINK);
12841284
rcu_read_lock();
12851285
#else
12861286
err = -EOPNOTSUPP;

‎net/netfilter/nfnetlink.c‎

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
3636

3737
static char __initdata nfversion[] = "0.30";
3838

39-
static const struct nfnetlink_subsystem __rcu *subsys_table[NFNL_SUBSYS_COUNT];
40-
static DEFINE_MUTEX(nfnl_mutex);
39+
static struct {
40+
struct mutex mutex;
41+
const struct nfnetlink_subsystem __rcu *subsys;
42+
} table[NFNL_SUBSYS_COUNT];
4143

4244
static const int nfnl_group2type[NFNLGRP_MAX+1] = {
4345
[NFNLGRP_CONNTRACK_NEW] = NFNL_SUBSYS_CTNETLINK,
@@ -48,37 +50,42 @@ static const int nfnl_group2type[NFNLGRP_MAX+1] = {
4850
[NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP,
4951
};
5052

51-
void nfnl_lock(void)
53+
void nfnl_lock(__u8 subsys_id)
5254
{
53-
mutex_lock(&nfnl_mutex);
55+
mutex_lock(&table[subsys_id].mutex);
5456
}
5557
EXPORT_SYMBOL_GPL(nfnl_lock);
5658

57-
void nfnl_unlock(void)
59+
void nfnl_unlock(__u8 subsys_id)
5860
{
59-
mutex_unlock(&nfnl_mutex);
61+
mutex_unlock(&table[subsys_id].mutex);
6062
}
6163
EXPORT_SYMBOL_GPL(nfnl_unlock);
6264

65+
static struct mutex *nfnl_get_lock(__u8 subsys_id)
66+
{
67+
return &table[subsys_id].mutex;
68+
}
69+
6370
int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
6471
{
65-
nfnl_lock();
66-
if (subsys_table[n->subsys_id]) {
67-
nfnl_unlock();
72+
nfnl_lock(n->subsys_id);
73+
if (table[n->subsys_id].subsys) {
74+
nfnl_unlock(n->subsys_id);
6875
return -EBUSY;
6976
}
70-
rcu_assign_pointer(subsys_table[n->subsys_id], n);
71-
nfnl_unlock();
77+
rcu_assign_pointer(table[n->subsys_id].subsys, n);
78+
nfnl_unlock(n->subsys_id);
7279

7380
return 0;
7481
}
7582
EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
7683

7784
int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
7885
{
79-
nfnl_lock();
80-
subsys_table[n->subsys_id] = NULL;
81-
nfnl_unlock();
86+
nfnl_lock(n->subsys_id);
87+
table[n->subsys_id].subsys = NULL;
88+
nfnl_unlock(n->subsys_id);
8289
synchronize_rcu();
8390
return 0;
8491
}
@@ -91,7 +98,7 @@ static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t t
9198
if (subsys_id >= NFNL_SUBSYS_COUNT)
9299
return NULL;
93100

94-
return rcu_dereference(subsys_table[subsys_id]);
101+
return rcu_dereference(table[subsys_id].subsys);
95102
}
96103

97104
static inline const struct nfnl_callback *
@@ -175,6 +182,7 @@ static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
175182
struct nlattr *cda[ss->cb[cb_id].attr_count + 1];
176183
struct nlattr *attr = (void *)nlh + min_len;
177184
int attrlen = nlh->nlmsg_len - min_len;
185+
__u8 subsys_id = NFNL_SUBSYS_ID(type);
178186

179187
err = nla_parse(cda, ss->cb[cb_id].attr_count,
180188
attr, attrlen, ss->cb[cb_id].policy);
@@ -189,18 +197,17 @@ static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
189197
rcu_read_unlock();
190198
} else {
191199
rcu_read_unlock();
192-
nfnl_lock();
193-
if (rcu_dereference_protected(
194-
subsys_table[NFNL_SUBSYS_ID(type)],
195-
lockdep_is_held(&nfnl_mutex)) != ss ||
200+
nfnl_lock(subsys_id);
201+
if (rcu_dereference_protected(table[subsys_id].subsys,
202+
lockdep_is_held(nfnl_get_lock(subsys_id))) != ss ||
196203
nfnetlink_find_client(type, ss) != nc)
197204
err = -EAGAIN;
198205
else if (nc->call)
199206
err = nc->call(net->nfnl, skb, nlh,
200207
(const struct nlattr **)cda);
201208
else
202209
err = -EINVAL;
203-
nfnl_unlock();
210+
nfnl_unlock(subsys_id);
204211
}
205212
if (err == -EAGAIN)
206213
goto replay;
@@ -267,6 +274,11 @@ static struct pernet_operations nfnetlink_net_ops = {
267274

268275
static int __init nfnetlink_init(void)
269276
{
277+
int i;
278+
279+
for (i=0; i<NFNL_SUBSYS_COUNT; i++)
280+
mutex_init(&table[i].mutex);
281+
270282
pr_info("Netfilter messages via NETLINK v%s.\n", nfversion);
271283
return register_pernet_subsys(&nfnetlink_net_ops);
272284
}

0 commit comments

Comments
 (0)