Commit b4ff4f04 authored by Johannes Berg's avatar Johannes Berg Committed by David S. Miller

[NETLINK]: allocate group bitmaps dynamically

Allow changing the number of groups for a netlink family
after it has been created, use RCU to protect the listeners
bitmap keeping netlink_has_listeners() lock-free.
Signed-off-by: default avatarJohannes Berg <johannes@sipsolutions.net>
Acked-by: default avatarPatrick McHardy <kaber@trash.net>
Acked-by: default avatarJamal Hadi Salim <hadi@cyberus.ca>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent c81de6ad
...@@ -161,6 +161,7 @@ extern struct sock *netlink_kernel_create(int unit, unsigned int groups, ...@@ -161,6 +161,7 @@ extern struct sock *netlink_kernel_create(int unit, unsigned int groups,
void (*input)(struct sock *sk, int len), void (*input)(struct sock *sk, int len),
struct mutex *cb_mutex, struct mutex *cb_mutex,
struct module *module); struct module *module);
extern int netlink_change_ngroups(struct sock *sk, unsigned int groups);
extern void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err); extern void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err);
extern int netlink_has_listeners(struct sock *sk, unsigned int group); extern int netlink_has_listeners(struct sock *sk, unsigned int group);
extern int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 pid, int nonblock); extern int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 pid, int nonblock);
......
...@@ -62,6 +62,7 @@ ...@@ -62,6 +62,7 @@
#include <net/netlink.h> #include <net/netlink.h>
#define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8) #define NLGRPSZ(x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)
#define NLGRPLONGS(x) (NLGRPSZ(x)/sizeof(unsigned long))
struct netlink_sock { struct netlink_sock {
/* struct sock has to be the first member of netlink_sock */ /* struct sock has to be the first member of netlink_sock */
...@@ -314,10 +315,12 @@ netlink_update_listeners(struct sock *sk) ...@@ -314,10 +315,12 @@ netlink_update_listeners(struct sock *sk)
unsigned long mask; unsigned long mask;
unsigned int i; unsigned int i;
for (i = 0; i < NLGRPSZ(tbl->groups)/sizeof(unsigned long); i++) { for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
mask = 0; mask = 0;
sk_for_each_bound(sk, node, &tbl->mc_list) sk_for_each_bound(sk, node, &tbl->mc_list) {
if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
mask |= nlk_sk(sk)->groups[i]; mask |= nlk_sk(sk)->groups[i];
}
tbl->listeners[i] = mask; tbl->listeners[i] = mask;
} }
/* this function is only called with the netlink table "grabbed", which /* this function is only called with the netlink table "grabbed", which
...@@ -555,26 +558,37 @@ netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions) ...@@ -555,26 +558,37 @@ netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
nlk->subscriptions = subscriptions; nlk->subscriptions = subscriptions;
} }
static int netlink_alloc_groups(struct sock *sk) static int netlink_realloc_groups(struct sock *sk)
{ {
struct netlink_sock *nlk = nlk_sk(sk); struct netlink_sock *nlk = nlk_sk(sk);
unsigned int groups; unsigned int groups;
unsigned long *new_groups;
int err = 0; int err = 0;
netlink_lock_table(); netlink_table_grab();
groups = nl_table[sk->sk_protocol].groups; groups = nl_table[sk->sk_protocol].groups;
if (!nl_table[sk->sk_protocol].registered) if (!nl_table[sk->sk_protocol].registered) {
err = -ENOENT; err = -ENOENT;
netlink_unlock_table(); goto out_unlock;
}
if (err) if (nlk->ngroups >= groups)
return err; goto out_unlock;
nlk->groups = kzalloc(NLGRPSZ(groups), GFP_KERNEL); new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
if (nlk->groups == NULL) if (new_groups == NULL) {
return -ENOMEM; err = -ENOMEM;
goto out_unlock;
}
memset((char*)new_groups + NLGRPSZ(nlk->ngroups), 0,
NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
nlk->groups = new_groups;
nlk->ngroups = groups; nlk->ngroups = groups;
return 0; out_unlock:
netlink_table_ungrab();
return err;
} }
static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len) static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
...@@ -591,12 +605,10 @@ static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len ...@@ -591,12 +605,10 @@ static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len
if (nladdr->nl_groups) { if (nladdr->nl_groups) {
if (!netlink_capable(sock, NL_NONROOT_RECV)) if (!netlink_capable(sock, NL_NONROOT_RECV))
return -EPERM; return -EPERM;
if (nlk->groups == NULL) { err = netlink_realloc_groups(sk);
err = netlink_alloc_groups(sk);
if (err) if (err)
return err; return err;
} }
}
if (nlk->pid) { if (nlk->pid) {
if (nladdr->nl_pid != nlk->pid) if (nladdr->nl_pid != nlk->pid)
...@@ -839,10 +851,18 @@ int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock ...@@ -839,10 +851,18 @@ int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock
int netlink_has_listeners(struct sock *sk, unsigned int group) int netlink_has_listeners(struct sock *sk, unsigned int group)
{ {
int res = 0; int res = 0;
unsigned long *listeners;
BUG_ON(!(nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET)); BUG_ON(!(nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET));
rcu_read_lock();
listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
if (group - 1 < nl_table[sk->sk_protocol].groups) if (group - 1 < nl_table[sk->sk_protocol].groups)
res = test_bit(group - 1, nl_table[sk->sk_protocol].listeners); res = test_bit(group - 1, listeners);
rcu_read_unlock();
return res; return res;
} }
EXPORT_SYMBOL_GPL(netlink_has_listeners); EXPORT_SYMBOL_GPL(netlink_has_listeners);
...@@ -1037,11 +1057,9 @@ static int netlink_setsockopt(struct socket *sock, int level, int optname, ...@@ -1037,11 +1057,9 @@ static int netlink_setsockopt(struct socket *sock, int level, int optname,
if (!netlink_capable(sock, NL_NONROOT_RECV)) if (!netlink_capable(sock, NL_NONROOT_RECV))
return -EPERM; return -EPERM;
if (nlk->groups == NULL) { err = netlink_realloc_groups(sk);
err = netlink_alloc_groups(sk);
if (err) if (err)
return err; return err;
}
if (!val || val - 1 >= nlk->ngroups) if (!val || val - 1 >= nlk->ngroups)
return -EINVAL; return -EINVAL;
netlink_table_grab(); netlink_table_grab();
...@@ -1328,6 +1346,46 @@ netlink_kernel_create(int unit, unsigned int groups, ...@@ -1328,6 +1346,46 @@ netlink_kernel_create(int unit, unsigned int groups,
return NULL; return NULL;
} }
/**
* netlink_change_ngroups - change number of multicast groups
*
* This changes the number of multicast groups that are available
* on a certain netlink family. Note that it is not possible to
* change the number of groups to below 32.
*
* @sk: The kernel netlink socket, as returned by netlink_kernel_create().
* @groups: The new number of groups.
*/
int netlink_change_ngroups(struct sock *sk, unsigned int groups)
{
unsigned long *listeners, *old = NULL;
struct netlink_table *tbl = &nl_table[sk->sk_protocol];
int err = 0;
if (groups < 32)
groups = 32;
netlink_table_grab();
if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
listeners = kzalloc(NLGRPSZ(groups), GFP_ATOMIC);
if (!listeners) {
err = -ENOMEM;
goto out_ungrab;
}
old = tbl->listeners;
memcpy(listeners, old, NLGRPSZ(tbl->groups));
rcu_assign_pointer(tbl->listeners, listeners);
}
tbl->groups = groups;
out_ungrab:
netlink_table_ungrab();
synchronize_rcu();
kfree(old);
return err;
}
EXPORT_SYMBOL(netlink_change_ngroups);
void netlink_set_nonroot(int protocol, unsigned int flags) void netlink_set_nonroot(int protocol, unsigned int flags)
{ {
if ((unsigned int)protocol < MAX_LINKS) if ((unsigned int)protocol < MAX_LINKS)
......
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