Skip to content

Commit 07e9896

Browse files
ebiedermgregkh
authored andcommitted
kobject: Send hotplug events in all network namespaces
Open a copy of the uevent kernel socket in each network namespace so we can send uevents in all network namespaces. Signed-off-by: Eric W. Biederman <[email protected]> Acked-by: David S. Miller <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 1d9e882 commit 07e9896

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

lib/kobject_uevent.c

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@
2424
#include <linux/skbuff.h>
2525
#include <linux/netlink.h>
2626
#include <net/sock.h>
27+
#include <net/net_namespace.h>
2728

2829

2930
u64 uevent_seqnum;
3031
char uevent_helper[UEVENT_HELPER_PATH_LEN] = CONFIG_UEVENT_HELPER_PATH;
3132
static DEFINE_SPINLOCK(sequence_lock);
32-
#if defined(CONFIG_NET)
33-
static struct sock *uevent_sock;
33+
#ifdef CONFIG_NET
34+
struct uevent_sock {
35+
struct list_head list;
36+
struct sock *sk;
37+
};
38+
static LIST_HEAD(uevent_sock_list);
39+
static DEFINE_MUTEX(uevent_sock_mutex);
3440
#endif
3541

3642
/* the strings here must match the enum in include/linux/kobject.h */
@@ -100,6 +106,9 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
100106
u64 seq;
101107
int i = 0;
102108
int retval = 0;
109+
#ifdef CONFIG_NET
110+
struct uevent_sock *ue_sk;
111+
#endif
103112

104113
pr_debug("kobject: '%s' (%p): %s\n",
105114
kobject_name(kobj), kobj, __func__);
@@ -211,7 +220,9 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
211220

212221
#if defined(CONFIG_NET)
213222
/* send netlink message */
214-
if (uevent_sock) {
223+
mutex_lock(&uevent_sock_mutex);
224+
list_for_each_entry(ue_sk, &uevent_sock_list, list) {
225+
struct sock *uevent_sock = ue_sk->sk;
215226
struct sk_buff *skb;
216227
size_t len;
217228

@@ -241,6 +252,7 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
241252
} else
242253
retval = -ENOMEM;
243254
}
255+
mutex_unlock(&uevent_sock_mutex);
244256
#endif
245257

246258
/* call uevent_helper, usually only enabled during early boot */
@@ -320,18 +332,58 @@ int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
320332
EXPORT_SYMBOL_GPL(add_uevent_var);
321333

322334
#if defined(CONFIG_NET)
323-
static int __init kobject_uevent_init(void)
335+
static int uevent_net_init(struct net *net)
324336
{
325-
uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
326-
1, NULL, NULL, THIS_MODULE);
327-
if (!uevent_sock) {
337+
struct uevent_sock *ue_sk;
338+
339+
ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL);
340+
if (!ue_sk)
341+
return -ENOMEM;
342+
343+
ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT,
344+
1, NULL, NULL, THIS_MODULE);
345+
if (!ue_sk->sk) {
328346
printk(KERN_ERR
329347
"kobject_uevent: unable to create netlink socket!\n");
330348
return -ENODEV;
331349
}
332-
netlink_set_nonroot(NETLINK_KOBJECT_UEVENT, NL_NONROOT_RECV);
350+
mutex_lock(&uevent_sock_mutex);
351+
list_add_tail(&ue_sk->list, &uevent_sock_list);
352+
mutex_unlock(&uevent_sock_mutex);
333353
return 0;
334354
}
335355

356+
static void uevent_net_exit(struct net *net)
357+
{
358+
struct uevent_sock *ue_sk;
359+
360+
mutex_lock(&uevent_sock_mutex);
361+
list_for_each_entry(ue_sk, &uevent_sock_list, list) {
362+
if (sock_net(ue_sk->sk) == net)
363+
goto found;
364+
}
365+
mutex_unlock(&uevent_sock_mutex);
366+
return;
367+
368+
found:
369+
list_del(&ue_sk->list);
370+
mutex_unlock(&uevent_sock_mutex);
371+
372+
netlink_kernel_release(ue_sk->sk);
373+
kfree(ue_sk);
374+
}
375+
376+
static struct pernet_operations uevent_net_ops = {
377+
.init = uevent_net_init,
378+
.exit = uevent_net_exit,
379+
};
380+
381+
static int __init kobject_uevent_init(void)
382+
{
383+
netlink_set_nonroot(NETLINK_KOBJECT_UEVENT, NL_NONROOT_RECV);
384+
return register_pernet_subsys(&uevent_net_ops);
385+
}
386+
387+
336388
postcore_initcall(kobject_uevent_init);
337389
#endif

0 commit comments

Comments
 (0)