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