Merge tag 'kbuild-fixes-v4.19' of git://git.kernel.org/pub/scm/linux/kernel/git/masah...
[linux-2.6/btrfs-unstable.git] / include / net / net_namespace.h
blob9b5fdc50519acfbc6ab9219579e742fe79193194
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Operations on the network namespace
4 */
5 #ifndef __NET_NET_NAMESPACE_H
6 #define __NET_NET_NAMESPACE_H
8 #include <linux/atomic.h>
9 #include <linux/refcount.h>
10 #include <linux/workqueue.h>
11 #include <linux/list.h>
12 #include <linux/sysctl.h>
13 #include <linux/uidgid.h>
15 #include <net/flow.h>
16 #include <net/netns/core.h>
17 #include <net/netns/mib.h>
18 #include <net/netns/unix.h>
19 #include <net/netns/packet.h>
20 #include <net/netns/ipv4.h>
21 #include <net/netns/ipv6.h>
22 #include <net/netns/ieee802154_6lowpan.h>
23 #include <net/netns/sctp.h>
24 #include <net/netns/dccp.h>
25 #include <net/netns/netfilter.h>
26 #include <net/netns/x_tables.h>
27 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
28 #include <net/netns/conntrack.h>
29 #endif
30 #include <net/netns/nftables.h>
31 #include <net/netns/xfrm.h>
32 #include <net/netns/mpls.h>
33 #include <net/netns/can.h>
34 #include <linux/ns_common.h>
35 #include <linux/idr.h>
36 #include <linux/skbuff.h>
38 struct user_namespace;
39 struct proc_dir_entry;
40 struct net_device;
41 struct sock;
42 struct ctl_table_header;
43 struct net_generic;
44 struct uevent_sock;
45 struct netns_ipvs;
48 #define NETDEV_HASHBITS 8
49 #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
51 struct net {
52 refcount_t passive; /* To decided when the network
53 * namespace should be freed.
55 refcount_t count; /* To decided when the network
56 * namespace should be shut down.
58 spinlock_t rules_mod_lock;
60 atomic64_t cookie_gen;
62 struct list_head list; /* list of network namespaces */
63 struct list_head exit_list; /* To linked to call pernet exit
64 * methods on dead net (
65 * pernet_ops_rwsem read locked),
66 * or to unregister pernet ops
67 * (pernet_ops_rwsem write locked).
69 struct llist_node cleanup_list; /* namespaces on death row */
71 struct user_namespace *user_ns; /* Owning user namespace */
72 struct ucounts *ucounts;
73 spinlock_t nsid_lock;
74 struct idr netns_ids;
76 struct ns_common ns;
78 struct proc_dir_entry *proc_net;
79 struct proc_dir_entry *proc_net_stat;
81 #ifdef CONFIG_SYSCTL
82 struct ctl_table_set sysctls;
83 #endif
85 struct sock *rtnl; /* rtnetlink socket */
86 struct sock *genl_sock;
88 struct uevent_sock *uevent_sock; /* uevent socket */
90 struct list_head dev_base_head;
91 struct hlist_head *dev_name_head;
92 struct hlist_head *dev_index_head;
93 unsigned int dev_base_seq; /* protected by rtnl_mutex */
94 int ifindex;
95 unsigned int dev_unreg_count;
97 /* core fib_rules */
98 struct list_head rules_ops;
100 struct list_head fib_notifier_ops; /* Populated by
101 * register_pernet_subsys()
103 struct net_device *loopback_dev; /* The loopback */
104 struct netns_core core;
105 struct netns_mib mib;
106 struct netns_packet packet;
107 struct netns_unix unx;
108 struct netns_ipv4 ipv4;
109 #if IS_ENABLED(CONFIG_IPV6)
110 struct netns_ipv6 ipv6;
111 #endif
112 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
113 struct netns_ieee802154_lowpan ieee802154_lowpan;
114 #endif
115 #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
116 struct netns_sctp sctp;
117 #endif
118 #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
119 struct netns_dccp dccp;
120 #endif
121 #ifdef CONFIG_NETFILTER
122 struct netns_nf nf;
123 struct netns_xt xt;
124 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
125 struct netns_ct ct;
126 #endif
127 #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
128 struct netns_nftables nft;
129 #endif
130 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
131 struct netns_nf_frag nf_frag;
132 struct ctl_table_header *nf_frag_frags_hdr;
133 #endif
134 struct sock *nfnl;
135 struct sock *nfnl_stash;
136 #if IS_ENABLED(CONFIG_NETFILTER_NETLINK_ACCT)
137 struct list_head nfnl_acct_list;
138 #endif
139 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
140 struct list_head nfct_timeout_list;
141 #endif
142 #endif
143 #ifdef CONFIG_WEXT_CORE
144 struct sk_buff_head wext_nlevents;
145 #endif
146 struct net_generic __rcu *gen;
148 /* Note : following structs are cache line aligned */
149 #ifdef CONFIG_XFRM
150 struct netns_xfrm xfrm;
151 #endif
152 #if IS_ENABLED(CONFIG_IP_VS)
153 struct netns_ipvs *ipvs;
154 #endif
155 #if IS_ENABLED(CONFIG_MPLS)
156 struct netns_mpls mpls;
157 #endif
158 #if IS_ENABLED(CONFIG_CAN)
159 struct netns_can can;
160 #endif
161 struct sock *diag_nlsk;
162 atomic_t fnhe_genid;
163 } __randomize_layout;
165 #include <linux/seq_file_net.h>
167 /* Init's network namespace */
168 extern struct net init_net;
170 #ifdef CONFIG_NET_NS
171 struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
172 struct net *old_net);
174 void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid);
176 void net_ns_barrier(void);
177 #else /* CONFIG_NET_NS */
178 #include <linux/sched.h>
179 #include <linux/nsproxy.h>
180 static inline struct net *copy_net_ns(unsigned long flags,
181 struct user_namespace *user_ns, struct net *old_net)
183 if (flags & CLONE_NEWNET)
184 return ERR_PTR(-EINVAL);
185 return old_net;
188 static inline void net_ns_get_ownership(const struct net *net,
189 kuid_t *uid, kgid_t *gid)
191 *uid = GLOBAL_ROOT_UID;
192 *gid = GLOBAL_ROOT_GID;
195 static inline void net_ns_barrier(void) {}
196 #endif /* CONFIG_NET_NS */
199 extern struct list_head net_namespace_list;
201 struct net *get_net_ns_by_pid(pid_t pid);
202 struct net *get_net_ns_by_fd(int fd);
204 #ifdef CONFIG_SYSCTL
205 void ipx_register_sysctl(void);
206 void ipx_unregister_sysctl(void);
207 #else
208 #define ipx_register_sysctl()
209 #define ipx_unregister_sysctl()
210 #endif
212 #ifdef CONFIG_NET_NS
213 void __put_net(struct net *net);
215 static inline struct net *get_net(struct net *net)
217 refcount_inc(&net->count);
218 return net;
221 static inline struct net *maybe_get_net(struct net *net)
223 /* Used when we know struct net exists but we
224 * aren't guaranteed a previous reference count
225 * exists. If the reference count is zero this
226 * function fails and returns NULL.
228 if (!refcount_inc_not_zero(&net->count))
229 net = NULL;
230 return net;
233 static inline void put_net(struct net *net)
235 if (refcount_dec_and_test(&net->count))
236 __put_net(net);
239 static inline
240 int net_eq(const struct net *net1, const struct net *net2)
242 return net1 == net2;
245 static inline int check_net(const struct net *net)
247 return refcount_read(&net->count) != 0;
250 void net_drop_ns(void *);
252 #else
254 static inline struct net *get_net(struct net *net)
256 return net;
259 static inline void put_net(struct net *net)
263 static inline struct net *maybe_get_net(struct net *net)
265 return net;
268 static inline
269 int net_eq(const struct net *net1, const struct net *net2)
271 return 1;
274 static inline int check_net(const struct net *net)
276 return 1;
279 #define net_drop_ns NULL
280 #endif
283 typedef struct {
284 #ifdef CONFIG_NET_NS
285 struct net *net;
286 #endif
287 } possible_net_t;
289 static inline void write_pnet(possible_net_t *pnet, struct net *net)
291 #ifdef CONFIG_NET_NS
292 pnet->net = net;
293 #endif
296 static inline struct net *read_pnet(const possible_net_t *pnet)
298 #ifdef CONFIG_NET_NS
299 return pnet->net;
300 #else
301 return &init_net;
302 #endif
305 /* Protected by net_rwsem */
306 #define for_each_net(VAR) \
307 list_for_each_entry(VAR, &net_namespace_list, list)
309 #define for_each_net_rcu(VAR) \
310 list_for_each_entry_rcu(VAR, &net_namespace_list, list)
312 #ifdef CONFIG_NET_NS
313 #define __net_init
314 #define __net_exit
315 #define __net_initdata
316 #define __net_initconst
317 #else
318 #define __net_init __init
319 #define __net_exit __ref
320 #define __net_initdata __initdata
321 #define __net_initconst __initconst
322 #endif
324 int peernet2id_alloc(struct net *net, struct net *peer);
325 int peernet2id(struct net *net, struct net *peer);
326 bool peernet_has_id(struct net *net, struct net *peer);
327 struct net *get_net_ns_by_id(struct net *net, int id);
329 struct pernet_operations {
330 struct list_head list;
332 * Below methods are called without any exclusive locks.
333 * More than one net may be constructed and destructed
334 * in parallel on several cpus. Every pernet_operations
335 * have to keep in mind all other pernet_operations and
336 * to introduce a locking, if they share common resources.
338 * The only time they are called with exclusive lock is
339 * from register_pernet_subsys(), unregister_pernet_subsys()
340 * register_pernet_device() and unregister_pernet_device().
342 * Exit methods using blocking RCU primitives, such as
343 * synchronize_rcu(), should be implemented via exit_batch.
344 * Then, destruction of a group of net requires single
345 * synchronize_rcu() related to these pernet_operations,
346 * instead of separate synchronize_rcu() for every net.
347 * Please, avoid synchronize_rcu() at all, where it's possible.
349 int (*init)(struct net *net);
350 void (*exit)(struct net *net);
351 void (*exit_batch)(struct list_head *net_exit_list);
352 unsigned int *id;
353 size_t size;
357 * Use these carefully. If you implement a network device and it
358 * needs per network namespace operations use device pernet operations,
359 * otherwise use pernet subsys operations.
361 * Network interfaces need to be removed from a dying netns _before_
362 * subsys notifiers can be called, as most of the network code cleanup
363 * (which is done from subsys notifiers) runs with the assumption that
364 * dev_remove_pack has been called so no new packets will arrive during
365 * and after the cleanup functions have been called. dev_remove_pack
366 * is not per namespace so instead the guarantee of no more packets
367 * arriving in a network namespace is provided by ensuring that all
368 * network devices and all sockets have left the network namespace
369 * before the cleanup methods are called.
371 * For the longest time the ipv4 icmp code was registered as a pernet
372 * device which caused kernel oops, and panics during network
373 * namespace cleanup. So please don't get this wrong.
375 int register_pernet_subsys(struct pernet_operations *);
376 void unregister_pernet_subsys(struct pernet_operations *);
377 int register_pernet_device(struct pernet_operations *);
378 void unregister_pernet_device(struct pernet_operations *);
380 struct ctl_table;
381 struct ctl_table_header;
383 #ifdef CONFIG_SYSCTL
384 int net_sysctl_init(void);
385 struct ctl_table_header *register_net_sysctl(struct net *net, const char *path,
386 struct ctl_table *table);
387 void unregister_net_sysctl_table(struct ctl_table_header *header);
388 #else
389 static inline int net_sysctl_init(void) { return 0; }
390 static inline struct ctl_table_header *register_net_sysctl(struct net *net,
391 const char *path, struct ctl_table *table)
393 return NULL;
395 static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
398 #endif
400 static inline int rt_genid_ipv4(struct net *net)
402 return atomic_read(&net->ipv4.rt_genid);
405 static inline void rt_genid_bump_ipv4(struct net *net)
407 atomic_inc(&net->ipv4.rt_genid);
410 extern void (*__fib6_flush_trees)(struct net *net);
411 static inline void rt_genid_bump_ipv6(struct net *net)
413 if (__fib6_flush_trees)
414 __fib6_flush_trees(net);
417 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
418 static inline struct netns_ieee802154_lowpan *
419 net_ieee802154_lowpan(struct net *net)
421 return &net->ieee802154_lowpan;
423 #endif
425 /* For callers who don't really care about whether it's IPv4 or IPv6 */
426 static inline void rt_genid_bump_all(struct net *net)
428 rt_genid_bump_ipv4(net);
429 rt_genid_bump_ipv6(net);
432 static inline int fnhe_genid(struct net *net)
434 return atomic_read(&net->fnhe_genid);
437 static inline void fnhe_genid_bump(struct net *net)
439 atomic_inc(&net->fnhe_genid);
442 #endif /* __NET_NET_NAMESPACE_H */