netns: Deduplicate and fix copy_net_ns when !CONFIG_NET_NS
[linux-2.6-block.git] / include / net / net_namespace.h
CommitLineData
5f256bec
EB
1/*
2 * Operations on the network namespace
3 */
4#ifndef __NET_NET_NAMESPACE_H
5#define __NET_NET_NAMESPACE_H
6
60063497 7#include <linux/atomic.h>
5f256bec
EB
8#include <linux/workqueue.h>
9#include <linux/list.h>
bee95250 10#include <linux/sysctl.h>
5f256bec 11
8efa6e93 12#include <net/netns/core.h>
852566f5 13#include <net/netns/mib.h>
a0a53c8b 14#include <net/netns/unix.h>
2aaef4e4 15#include <net/netns/packet.h>
8afd351c 16#include <net/netns/ipv4.h>
b0f159db 17#include <net/netns/ipv6.h>
4db67e80 18#include <net/netns/sctp.h>
67019cc9 19#include <net/netns/dccp.h>
8d870052 20#include <net/netns/x_tables.h>
dfdb8d79
AD
21#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
22#include <net/netns/conntrack.h>
23#endif
d62ddc21 24#include <net/netns/xfrm.h>
a0a53c8b 25
457c4cbc 26struct proc_dir_entry;
2774c7ab 27struct net_device;
97c53cac 28struct sock;
1597fbc0 29struct ctl_table_header;
dec827d1 30struct net_generic;
134e6375 31struct sock;
2553d064 32struct netns_ipvs;
1597fbc0 33
7c28bd0b
ED
34
35#define NETDEV_HASHBITS 8
36#define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
37
5f256bec 38struct net {
a685e089
AV
39 atomic_t passive; /* To decided when the network
40 * namespace should be freed.
41 */
5f256bec 42 atomic_t count; /* To decided when the network
a685e089 43 * namespace should be shut down.
5f256bec 44 */
5d1e4468 45#ifdef NETNS_REFCNT_DEBUG
5f256bec
EB
46 atomic_t use_count; /* To track references we
47 * destroy on demand
48 */
5d1e4468 49#endif
8e602ce2
ED
50 spinlock_t rules_mod_lock;
51
5f256bec 52 struct list_head list; /* list of network namespaces */
2b035b39 53 struct list_head cleanup_list; /* namespaces on death row */
72ad937a 54 struct list_head exit_list; /* Use only net_mutex */
457c4cbc
EB
55
56 struct proc_dir_entry *proc_net;
57 struct proc_dir_entry *proc_net_stat;
881d966b 58
73455092
AV
59#ifdef CONFIG_SYSCTL
60 struct ctl_table_set sysctls;
61#endif
95bdfccb 62
8e602ce2
ED
63 struct sock *rtnl; /* rtnetlink socket */
64 struct sock *genl_sock;
2774c7ab 65
881d966b
EB
66 struct list_head dev_base_head;
67 struct hlist_head *dev_name_head;
68 struct hlist_head *dev_index_head;
4e985ada 69 unsigned int dev_base_seq; /* protected by rtnl_mutex */
aa79e66e 70 int ifindex;
97c53cac 71
5fd30ee7
DL
72 /* core fib_rules */
73 struct list_head rules_ops;
5fd30ee7 74
d12d01d6 75
8e602ce2 76 struct net_device *loopback_dev; /* The loopback */
8efa6e93 77 struct netns_core core;
852566f5 78 struct netns_mib mib;
2aaef4e4 79 struct netns_packet packet;
a0a53c8b 80 struct netns_unix unx;
8afd351c 81 struct netns_ipv4 ipv4;
dfd56b8b 82#if IS_ENABLED(CONFIG_IPV6)
b0f159db
DL
83 struct netns_ipv6 ipv6;
84#endif
4db67e80
EB
85#if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
86 struct netns_sctp sctp;
87#endif
67019cc9
PE
88#if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
89 struct netns_dccp dccp;
90#endif
8d870052
AD
91#ifdef CONFIG_NETFILTER
92 struct netns_xt xt;
dfdb8d79
AD
93#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
94 struct netns_ct ct;
c038a767
AW
95#endif
96#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
97 struct netns_nf_frag nf_frag;
dfdb8d79 98#endif
cd8c20b6
AD
99 struct sock *nfnl;
100 struct sock *nfnl_stash;
d62ddc21 101#endif
3d23e349 102#ifdef CONFIG_WEXT_CORE
b333b3d2 103 struct sk_buff_head wext_nlevents;
8d870052 104#endif
1c87733d 105 struct net_generic __rcu *gen;
8e602ce2
ED
106
107 /* Note : following structs are cache line aligned */
108#ifdef CONFIG_XFRM
109 struct netns_xfrm xfrm;
110#endif
61b1ab45 111 struct netns_ipvs *ipvs;
51d7cccf 112 struct sock *diag_nlsk;
b42664f8 113 atomic_t rt_genid;
5f256bec
EB
114};
115
1fb9489b
PE
116/*
117 * ifindex generation is per-net namespace, and loopback is
118 * always the 1st device in ns (see net_dev_init), thus any
119 * loopback device should get ifindex 1
120 */
121
122#define LOOPBACK_IFINDEX 1
225c0a01 123
c0f39322
DL
124#include <linux/seq_file_net.h>
125
4fabcd71 126/* Init's network namespace */
5f256bec 127extern struct net init_net;
a4aa834a 128
d727abcb 129#ifdef CONFIG_NET_NS
9dd776b6 130extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
225c0a01 131
d727abcb
EB
132#else /* CONFIG_NET_NS */
133#include <linux/sched.h>
134#include <linux/nsproxy.h>
135static inline struct net *copy_net_ns(unsigned long flags, struct net *old_net)
9dd776b6 136{
d727abcb
EB
137 if (flags & CLONE_NEWNET)
138 return ERR_PTR(-EINVAL);
139 return old_net;
9dd776b6 140}
d727abcb 141#endif /* CONFIG_NET_NS */
225c0a01
DL
142
143
144extern struct list_head net_namespace_list;
9dd776b6 145
30ffee84 146extern struct net *get_net_ns_by_pid(pid_t pid);
f0630529 147extern struct net *get_net_ns_by_fd(int pid);
30ffee84 148
d4655795 149#ifdef CONFIG_NET_NS
5f256bec
EB
150extern void __put_net(struct net *net);
151
152static inline struct net *get_net(struct net *net)
153{
154 atomic_inc(&net->count);
155 return net;
156}
157
077130c0
EB
158static inline struct net *maybe_get_net(struct net *net)
159{
160 /* Used when we know struct net exists but we
161 * aren't guaranteed a previous reference count
162 * exists. If the reference count is zero this
163 * function fails and returns NULL.
164 */
165 if (!atomic_inc_not_zero(&net->count))
166 net = NULL;
167 return net;
168}
169
5f256bec
EB
170static inline void put_net(struct net *net)
171{
172 if (atomic_dec_and_test(&net->count))
173 __put_net(net);
174}
175
878628fb
YH
176static inline
177int net_eq(const struct net *net1, const struct net *net2)
178{
179 return net1 == net2;
180}
a685e089
AV
181
182extern void net_drop_ns(void *);
183
d4655795 184#else
b9f75f45 185
d4655795
PE
186static inline struct net *get_net(struct net *net)
187{
188 return net;
189}
190
191static inline void put_net(struct net *net)
192{
193}
194
5d1e4468
DL
195static inline struct net *maybe_get_net(struct net *net)
196{
197 return net;
198}
199
200static inline
201int net_eq(const struct net *net1, const struct net *net2)
202{
203 return 1;
204}
a685e089
AV
205
206#define net_drop_ns NULL
5d1e4468
DL
207#endif
208
209
210#ifdef NETNS_REFCNT_DEBUG
d4655795
PE
211static inline struct net *hold_net(struct net *net)
212{
5d1e4468
DL
213 if (net)
214 atomic_inc(&net->use_count);
d4655795
PE
215 return net;
216}
217
218static inline void release_net(struct net *net)
219{
5d1e4468
DL
220 if (net)
221 atomic_dec(&net->use_count);
d4655795 222}
5d1e4468
DL
223#else
224static inline struct net *hold_net(struct net *net)
d4655795
PE
225{
226 return net;
227}
878628fb 228
5d1e4468 229static inline void release_net(struct net *net)
878628fb 230{
878628fb 231}
d4655795 232#endif
5f256bec 233
8f424b5f
ED
234#ifdef CONFIG_NET_NS
235
236static inline void write_pnet(struct net **pnet, struct net *net)
237{
238 *pnet = net;
239}
240
241static inline struct net *read_pnet(struct net * const *pnet)
242{
243 return *pnet;
244}
245
246#else
247
248#define write_pnet(pnet, net) do { (void)(net);} while (0)
249#define read_pnet(pnet) (&init_net)
250
251#endif
5d1e4468 252
5f256bec
EB
253#define for_each_net(VAR) \
254 list_for_each_entry(VAR, &net_namespace_list, list)
255
11a28d37
JB
256#define for_each_net_rcu(VAR) \
257 list_for_each_entry_rcu(VAR, &net_namespace_list, list)
258
4665079c
PE
259#ifdef CONFIG_NET_NS
260#define __net_init
261#define __net_exit
022cbae6 262#define __net_initdata
04a6f82c 263#define __net_initconst
4665079c
PE
264#else
265#define __net_init __init
266#define __net_exit __exit_refok
022cbae6 267#define __net_initdata __initdata
04a6f82c 268#define __net_initconst __initconst
4665079c 269#endif
5f256bec
EB
270
271struct pernet_operations {
272 struct list_head list;
273 int (*init)(struct net *net);
274 void (*exit)(struct net *net);
72ad937a 275 void (*exit_batch)(struct list_head *net_exit_list);
f875bae0
EB
276 int *id;
277 size_t size;
5f256bec
EB
278};
279
17edde52
EB
280/*
281 * Use these carefully. If you implement a network device and it
282 * needs per network namespace operations use device pernet operations,
283 * otherwise use pernet subsys operations.
284 *
4edf547b
JB
285 * Network interfaces need to be removed from a dying netns _before_
286 * subsys notifiers can be called, as most of the network code cleanup
287 * (which is done from subsys notifiers) runs with the assumption that
288 * dev_remove_pack has been called so no new packets will arrive during
289 * and after the cleanup functions have been called. dev_remove_pack
290 * is not per namespace so instead the guarantee of no more packets
291 * arriving in a network namespace is provided by ensuring that all
292 * network devices and all sockets have left the network namespace
293 * before the cleanup methods are called.
17edde52
EB
294 *
295 * For the longest time the ipv4 icmp code was registered as a pernet
296 * device which caused kernel oops, and panics during network
297 * namespace cleanup. So please don't get this wrong.
298 */
5f256bec
EB
299extern int register_pernet_subsys(struct pernet_operations *);
300extern void unregister_pernet_subsys(struct pernet_operations *);
301extern int register_pernet_device(struct pernet_operations *);
302extern void unregister_pernet_device(struct pernet_operations *);
f875bae0 303
95bdfccb
EB
304struct ctl_table;
305struct ctl_table_header;
d62c612e 306
2ca794e5
EB
307#ifdef CONFIG_SYSCTL
308extern int net_sysctl_init(void);
ab41a2ca
EB
309extern struct ctl_table_header *register_net_sysctl(struct net *net,
310 const char *path, struct ctl_table *table);
95bdfccb 311extern void unregister_net_sysctl_table(struct ctl_table_header *header);
48c74958
EB
312#else
313static inline int net_sysctl_init(void) { return 0; }
314static inline struct ctl_table_header *register_net_sysctl(struct net *net,
315 const char *path, struct ctl_table *table)
316{
317 return NULL;
318}
319static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
320{
321}
322#endif
323
b42664f8
ND
324static inline int rt_genid(struct net *net)
325{
326 return atomic_read(&net->rt_genid);
327}
328
329static inline void rt_genid_bump(struct net *net)
330{
331 atomic_inc(&net->rt_genid);
332}
95bdfccb 333
5f256bec 334#endif /* __NET_NET_NAMESPACE_H */