18da0af6192f2a667f66731f797435d34cecc539
[safe/jmp/linux-2.6] / include / net / net_namespace.h
1 /*
2  * Operations on the network namespace
3  */
4 #ifndef __NET_NET_NAMESPACE_H
5 #define __NET_NET_NAMESPACE_H
6
7 #include <asm/atomic.h>
8 #include <linux/workqueue.h>
9 #include <linux/list.h>
10
11 #include <net/netns/unix.h>
12 #include <net/netns/packet.h>
13
14 struct proc_dir_entry;
15 struct net_device;
16 struct sock;
17 struct ctl_table_header;
18
19 struct net {
20         atomic_t                count;          /* To decided when the network
21                                                  *  namespace should be freed.
22                                                  */
23         atomic_t                use_count;      /* To track references we
24                                                  * destroy on demand
25                                                  */
26         struct list_head        list;           /* list of network namespaces */
27         struct work_struct      work;           /* work struct for freeing */
28
29         struct proc_dir_entry   *proc_net;
30         struct proc_dir_entry   *proc_net_stat;
31         struct proc_dir_entry   *proc_net_root;
32
33         struct list_head        sysctl_table_headers;
34
35         struct net_device       *loopback_dev;          /* The loopback */
36
37         struct list_head        dev_base_head;
38         struct hlist_head       *dev_name_head;
39         struct hlist_head       *dev_index_head;
40
41         struct sock             *rtnl;                  /* rtnetlink socket */
42
43         /* core sysctls */
44         struct ctl_table_header *sysctl_core_hdr;
45         int                     sysctl_somaxconn;
46
47         struct netns_packet     packet;
48         struct netns_unix       unx;
49 };
50
51 #ifdef CONFIG_NET
52 /* Init's network namespace */
53 extern struct net init_net;
54 #define INIT_NET_NS(net_ns) .net_ns = &init_net,
55 #else
56 #define INIT_NET_NS(net_ns)
57 #endif
58
59 extern struct list_head net_namespace_list;
60
61 #ifdef CONFIG_NET
62 extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
63 #else
64 static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
65 {
66         /* There is nothing to copy so this is a noop */
67         return net_ns;
68 }
69 #endif
70
71 #ifdef CONFIG_NET_NS
72 extern void __put_net(struct net *net);
73
74 static inline struct net *get_net(struct net *net)
75 {
76         atomic_inc(&net->count);
77         return net;
78 }
79
80 static inline struct net *maybe_get_net(struct net *net)
81 {
82         /* Used when we know struct net exists but we
83          * aren't guaranteed a previous reference count
84          * exists.  If the reference count is zero this
85          * function fails and returns NULL.
86          */
87         if (!atomic_inc_not_zero(&net->count))
88                 net = NULL;
89         return net;
90 }
91
92 static inline void put_net(struct net *net)
93 {
94         if (atomic_dec_and_test(&net->count))
95                 __put_net(net);
96 }
97
98 static inline struct net *hold_net(struct net *net)
99 {
100         atomic_inc(&net->use_count);
101         return net;
102 }
103
104 static inline void release_net(struct net *net)
105 {
106         atomic_dec(&net->use_count);
107 }
108 #else
109 static inline struct net *get_net(struct net *net)
110 {
111         return net;
112 }
113
114 static inline void put_net(struct net *net)
115 {
116 }
117
118 static inline struct net *hold_net(struct net *net)
119 {
120         return net;
121 }
122
123 static inline void release_net(struct net *net)
124 {
125 }
126
127 static inline struct net *maybe_get_net(struct net *net)
128 {
129         return net;
130 }
131 #endif
132
133 #define for_each_net(VAR)                               \
134         list_for_each_entry(VAR, &net_namespace_list, list)
135
136 #ifdef CONFIG_NET_NS
137 #define __net_init
138 #define __net_exit
139 #define __net_initdata
140 #else
141 #define __net_init      __init
142 #define __net_exit      __exit_refok
143 #define __net_initdata  __initdata
144 #endif
145
146 struct pernet_operations {
147         struct list_head list;
148         int (*init)(struct net *net);
149         void (*exit)(struct net *net);
150 };
151
152 extern int register_pernet_subsys(struct pernet_operations *);
153 extern void unregister_pernet_subsys(struct pernet_operations *);
154 extern int register_pernet_device(struct pernet_operations *);
155 extern void unregister_pernet_device(struct pernet_operations *);
156
157 struct ctl_path;
158 struct ctl_table;
159 struct ctl_table_header;
160 extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
161         const struct ctl_path *path, struct ctl_table *table);
162 extern void unregister_net_sysctl_table(struct ctl_table_header *header);
163
164 #endif /* __NET_NET_NAMESPACE_H */