aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/net/net_namespace.h2
-rw-r--r--include/net/netns/generic.h49
-rw-r--r--net/core/net_namespace.c62
3 files changed, 113 insertions, 0 deletions
diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h
index 72fad1a0956b..f880b0f9f107 100644
--- a/include/net/net_namespace.h
+++ b/include/net/net_namespace.h
@@ -20,6 +20,7 @@ struct proc_dir_entry;
20struct net_device; 20struct net_device;
21struct sock; 21struct sock;
22struct ctl_table_header; 22struct ctl_table_header;
23struct net_generic;
23 24
24struct net { 25struct net {
25 atomic_t count; /* To decided when the network 26 atomic_t count; /* To decided when the network
@@ -61,6 +62,7 @@ struct net {
61#ifdef CONFIG_NETFILTER 62#ifdef CONFIG_NETFILTER
62 struct netns_xt xt; 63 struct netns_xt xt;
63#endif 64#endif
65 struct net_generic *gen;
64}; 66};
65 67
66 68
diff --git a/include/net/netns/generic.h b/include/net/netns/generic.h
new file mode 100644
index 000000000000..0c04fd2a700b
--- /dev/null
+++ b/include/net/netns/generic.h
@@ -0,0 +1,49 @@
1/*
2 * generic net pointers
3 */
4
5#ifndef __NET_GENERIC_H__
6#define __NET_GENERIC_H__
7
8#include <linux/rcupdate.h>
9
10/*
11 * Generic net pointers are to be used by modules to put some private
12 * stuff on the struct net without explicit struct net modification
13 *
14 * The rules are simple:
15 * 1. register the ops with register_pernet_gen_device to get the id
16 * of your private pointer;
17 * 2. call net_assign_generic() to put the private data on the struct
18 * net (most preferably this should be done in the ->init callback
19 * of the ops registered);
20 * 3. do not change this pointer while the net is alive;
21 * 4. do not try to have any private reference on the net_generic object.
22 *
23 * After accomplishing all of the above, the private pointer can be
24 * accessed with the net_generic() call.
25 */
26
27struct net_generic {
28 unsigned int len;
29 struct rcu_head rcu;
30
31 void *ptr[0];
32};
33
34static inline void *net_generic(struct net *net, int id)
35{
36 struct net_generic *ng;
37 void *ptr;
38
39 rcu_read_lock();
40 ng = rcu_dereference(net->gen);
41 BUG_ON(id == 0 || id > ng->len);
42 ptr = ng->ptr[id - 1];
43 rcu_read_unlock();
44
45 return ptr;
46}
47
48extern int net_assign_generic(struct net *net, int id, void *data);
49#endif
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index 2197d51aef3b..763674e1e593 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -7,6 +7,7 @@
7#include <linux/sched.h> 7#include <linux/sched.h>
8#include <linux/idr.h> 8#include <linux/idr.h>
9#include <net/net_namespace.h> 9#include <net/net_namespace.h>
10#include <net/netns/generic.h>
10 11
11/* 12/*
12 * Our network namespace constructor/destructor lists 13 * Our network namespace constructor/destructor lists
@@ -21,6 +22,8 @@ LIST_HEAD(net_namespace_list);
21struct net init_net; 22struct net init_net;
22EXPORT_SYMBOL(init_net); 23EXPORT_SYMBOL(init_net);
23 24
25#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
26
24/* 27/*
25 * setup_net runs the initializers for the network namespace object. 28 * setup_net runs the initializers for the network namespace object.
26 */ 29 */
@@ -29,10 +32,21 @@ static __net_init int setup_net(struct net *net)
29 /* Must be called with net_mutex held */ 32 /* Must be called with net_mutex held */
30 struct pernet_operations *ops; 33 struct pernet_operations *ops;
31 int error; 34 int error;
35 struct net_generic *ng;
32 36
33 atomic_set(&net->count, 1); 37 atomic_set(&net->count, 1);
34 atomic_set(&net->use_count, 0); 38 atomic_set(&net->use_count, 0);
35 39
40 error = -ENOMEM;
41 ng = kzalloc(sizeof(struct net_generic) +
42 INITIAL_NET_GEN_PTRS * sizeof(void *), GFP_KERNEL);
43 if (ng == NULL)
44 goto out;
45
46 ng->len = INITIAL_NET_GEN_PTRS;
47 INIT_RCU_HEAD(&ng->rcu);
48 rcu_assign_pointer(net->gen, ng);
49
36 error = 0; 50 error = 0;
37 list_for_each_entry(ops, &pernet_list, list) { 51 list_for_each_entry(ops, &pernet_list, list) {
38 if (ops->init) { 52 if (ops->init) {
@@ -54,6 +68,7 @@ out_undo:
54 } 68 }
55 69
56 rcu_barrier(); 70 rcu_barrier();
71 kfree(ng);
57 goto out; 72 goto out;
58} 73}
59 74
@@ -386,3 +401,50 @@ void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
386 mutex_unlock(&net_mutex); 401 mutex_unlock(&net_mutex);
387} 402}
388EXPORT_SYMBOL_GPL(unregister_pernet_gen_device); 403EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);
404
405static void net_generic_release(struct rcu_head *rcu)
406{
407 struct net_generic *ng;
408
409 ng = container_of(rcu, struct net_generic, rcu);
410 kfree(ng);
411}
412
413int net_assign_generic(struct net *net, int id, void *data)
414{
415 struct net_generic *ng, *old_ng;
416
417 BUG_ON(!mutex_is_locked(&net_mutex));
418 BUG_ON(id == 0);
419
420 ng = old_ng = net->gen;
421 if (old_ng->len >= id)
422 goto assign;
423
424 ng = kzalloc(sizeof(struct net_generic) +
425 id * sizeof(void *), GFP_KERNEL);
426 if (ng == NULL)
427 return -ENOMEM;
428
429 /*
430 * Some synchronisation notes:
431 *
432 * The net_generic explores the net->gen array inside rcu
433 * read section. Besides once set the net->gen->ptr[x]
434 * pointer never changes (see rules in netns/generic.h).
435 *
436 * That said, we simply duplicate this array and schedule
437 * the old copy for kfree after a grace period.
438 */
439
440 ng->len = id;
441 INIT_RCU_HEAD(&ng->rcu);
442 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len);
443
444 rcu_assign_pointer(net->gen, ng);
445 call_rcu(&old_ng->rcu, net_generic_release);
446assign:
447 ng->ptr[id - 1] = data;
448 return 0;
449}
450EXPORT_SYMBOL_GPL(net_assign_generic);