1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Operations on the network namespace
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>
15 #include <net/netns/core.h>
16 #include <net/netns/mib.h>
17 #include <net/netns/unix.h>
18 #include <net/netns/packet.h>
19 #include <net/netns/ipv4.h>
20 #include <net/netns/ipv6.h>
21 #include <net/netns/ieee802154_6lowpan.h>
22 #include <net/netns/sctp.h>
23 #include <net/netns/dccp.h>
24 #include <net/netns/netfilter.h>
25 #include <net/netns/x_tables.h>
26 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
27 #include <net/netns/conntrack.h>
29 #include <net/netns/nftables.h>
30 #include <net/netns/xfrm.h>
31 #include <net/netns/mpls.h>
32 #include <net/netns/can.h>
33 #include <linux/ns_common.h>
34 #include <linux/idr.h>
35 #include <linux/skbuff.h>
37 struct user_namespace;
38 struct proc_dir_entry;
41 struct ctl_table_header;
47 #define NETDEV_HASHBITS 8
48 #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
51 refcount_t passive; /* To decided when the network
52 * namespace should be freed.
54 refcount_t count; /* To decided when the network
55 * namespace should be shut down.
57 spinlock_t rules_mod_lock;
59 atomic64_t cookie_gen;
61 struct list_head list; /* list of network namespaces */
62 struct list_head exit_list; /* To linked to call pernet exit
63 * methods on dead net (net_sem
64 * read locked), or to unregister
65 * pernet ops (net_sem wr locked).
67 struct llist_node cleanup_list; /* namespaces on death row */
69 struct user_namespace *user_ns; /* Owning user namespace */
70 struct ucounts *ucounts;
76 struct proc_dir_entry *proc_net;
77 struct proc_dir_entry *proc_net_stat;
80 struct ctl_table_set sysctls;
83 struct sock *rtnl; /* rtnetlink socket */
84 struct sock *genl_sock;
86 struct uevent_sock *uevent_sock; /* uevent socket */
88 struct list_head dev_base_head;
89 struct hlist_head *dev_name_head;
90 struct hlist_head *dev_index_head;
91 unsigned int dev_base_seq; /* protected by rtnl_mutex */
93 unsigned int dev_unreg_count;
96 struct list_head rules_ops;
98 struct list_head fib_notifier_ops; /* protected by net_sem */
100 struct net_device *loopback_dev; /* The loopback */
101 struct netns_core core;
102 struct netns_mib mib;
103 struct netns_packet packet;
104 struct netns_unix unx;
105 struct netns_ipv4 ipv4;
106 #if IS_ENABLED(CONFIG_IPV6)
107 struct netns_ipv6 ipv6;
109 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
110 struct netns_ieee802154_lowpan ieee802154_lowpan;
112 #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
113 struct netns_sctp sctp;
115 #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
116 struct netns_dccp dccp;
118 #ifdef CONFIG_NETFILTER
121 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
124 #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
125 struct netns_nftables nft;
127 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
128 struct netns_nf_frag nf_frag;
131 struct sock *nfnl_stash;
132 #if IS_ENABLED(CONFIG_NETFILTER_NETLINK_ACCT)
133 struct list_head nfnl_acct_list;
135 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
136 struct list_head nfct_timeout_list;
139 #ifdef CONFIG_WEXT_CORE
140 struct sk_buff_head wext_nlevents;
142 struct net_generic __rcu *gen;
144 /* Note : following structs are cache line aligned */
146 struct netns_xfrm xfrm;
148 #if IS_ENABLED(CONFIG_IP_VS)
149 struct netns_ipvs *ipvs;
151 #if IS_ENABLED(CONFIG_MPLS)
152 struct netns_mpls mpls;
154 #if IS_ENABLED(CONFIG_CAN)
155 struct netns_can can;
157 struct sock *diag_nlsk;
159 } __randomize_layout;
161 #include <linux/seq_file_net.h>
163 /* Init's network namespace */
164 extern struct net init_net;
167 struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
168 struct net *old_net);
170 void net_ns_barrier(void);
171 #else /* CONFIG_NET_NS */
172 #include <linux/sched.h>
173 #include <linux/nsproxy.h>
174 static inline struct net *copy_net_ns(unsigned long flags,
175 struct user_namespace *user_ns, struct net *old_net)
177 if (flags & CLONE_NEWNET)
178 return ERR_PTR(-EINVAL);
182 static inline void net_ns_barrier(void) {}
183 #endif /* CONFIG_NET_NS */
186 extern struct list_head net_namespace_list;
188 struct net *get_net_ns_by_pid(pid_t pid);
189 struct net *get_net_ns_by_fd(int fd);
192 void ipx_register_sysctl(void);
193 void ipx_unregister_sysctl(void);
195 #define ipx_register_sysctl()
196 #define ipx_unregister_sysctl()
200 void __put_net(struct net *net);
202 static inline struct net *get_net(struct net *net)
204 refcount_inc(&net->count);
208 static inline struct net *maybe_get_net(struct net *net)
210 /* Used when we know struct net exists but we
211 * aren't guaranteed a previous reference count
212 * exists. If the reference count is zero this
213 * function fails and returns NULL.
215 if (!refcount_inc_not_zero(&net->count))
220 static inline void put_net(struct net *net)
222 if (refcount_dec_and_test(&net->count))
227 int net_eq(const struct net *net1, const struct net *net2)
232 static inline int check_net(const struct net *net)
234 return refcount_read(&net->count) != 0;
237 void net_drop_ns(void *);
241 static inline struct net *get_net(struct net *net)
246 static inline void put_net(struct net *net)
250 static inline struct net *maybe_get_net(struct net *net)
256 int net_eq(const struct net *net1, const struct net *net2)
261 static inline int check_net(const struct net *net)
266 #define net_drop_ns NULL
276 static inline void write_pnet(possible_net_t *pnet, struct net *net)
283 static inline struct net *read_pnet(const possible_net_t *pnet)
292 #define for_each_net(VAR) \
293 list_for_each_entry(VAR, &net_namespace_list, list)
295 #define for_each_net_rcu(VAR) \
296 list_for_each_entry_rcu(VAR, &net_namespace_list, list)
301 #define __net_initdata
302 #define __net_initconst
304 #define __net_init __init
305 #define __net_exit __ref
306 #define __net_initdata __initdata
307 #define __net_initconst __initconst
310 int peernet2id_alloc(struct net *net, struct net *peer);
311 int peernet2id(struct net *net, struct net *peer);
312 bool peernet_has_id(struct net *net, struct net *peer);
313 struct net *get_net_ns_by_id(struct net *net, int id);
315 struct pernet_operations {
316 struct list_head list;
318 * Below methods are called without any exclusive locks.
319 * More than one net may be constructed and destructed
320 * in parallel on several cpus. Every pernet_operations
321 * have to keep in mind all other pernet_operations and
322 * to introduce a locking, if they share common resources.
324 * Exit methods using blocking RCU primitives, such as
325 * synchronize_rcu(), should be implemented via exit_batch.
326 * Then, destruction of a group of net requires single
327 * synchronize_rcu() related to these pernet_operations,
328 * instead of separate synchronize_rcu() for every net.
329 * Please, avoid synchronize_rcu() at all, where it's possible.
331 int (*init)(struct net *net);
332 void (*exit)(struct net *net);
333 void (*exit_batch)(struct list_head *net_exit_list);
339 * Use these carefully. If you implement a network device and it
340 * needs per network namespace operations use device pernet operations,
341 * otherwise use pernet subsys operations.
343 * Network interfaces need to be removed from a dying netns _before_
344 * subsys notifiers can be called, as most of the network code cleanup
345 * (which is done from subsys notifiers) runs with the assumption that
346 * dev_remove_pack has been called so no new packets will arrive during
347 * and after the cleanup functions have been called. dev_remove_pack
348 * is not per namespace so instead the guarantee of no more packets
349 * arriving in a network namespace is provided by ensuring that all
350 * network devices and all sockets have left the network namespace
351 * before the cleanup methods are called.
353 * For the longest time the ipv4 icmp code was registered as a pernet
354 * device which caused kernel oops, and panics during network
355 * namespace cleanup. So please don't get this wrong.
357 int register_pernet_subsys(struct pernet_operations *);
358 void unregister_pernet_subsys(struct pernet_operations *);
359 int register_pernet_device(struct pernet_operations *);
360 void unregister_pernet_device(struct pernet_operations *);
363 struct ctl_table_header;
366 int net_sysctl_init(void);
367 struct ctl_table_header *register_net_sysctl(struct net *net, const char *path,
368 struct ctl_table *table);
369 void unregister_net_sysctl_table(struct ctl_table_header *header);
371 static inline int net_sysctl_init(void) { return 0; }
372 static inline struct ctl_table_header *register_net_sysctl(struct net *net,
373 const char *path, struct ctl_table *table)
377 static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
382 static inline int rt_genid_ipv4(struct net *net)
384 return atomic_read(&net->ipv4.rt_genid);
387 static inline void rt_genid_bump_ipv4(struct net *net)
389 atomic_inc(&net->ipv4.rt_genid);
392 extern void (*__fib6_flush_trees)(struct net *net);
393 static inline void rt_genid_bump_ipv6(struct net *net)
395 if (__fib6_flush_trees)
396 __fib6_flush_trees(net);
399 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
400 static inline struct netns_ieee802154_lowpan *
401 net_ieee802154_lowpan(struct net *net)
403 return &net->ieee802154_lowpan;
407 /* For callers who don't really care about whether it's IPv4 or IPv6 */
408 static inline void rt_genid_bump_all(struct net *net)
410 rt_genid_bump_ipv4(net);
411 rt_genid_bump_ipv6(net);
414 static inline int fnhe_genid(struct net *net)
416 return atomic_read(&net->fnhe_genid);
419 static inline void fnhe_genid_bump(struct net *net)
421 atomic_inc(&net->fnhe_genid);
424 #endif /* __NET_NET_NAMESPACE_H */