xref: /openbmc/linux/include/net/net_namespace.h (revision f318903c)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
25f256becSEric W. Biederman /*
35f256becSEric W. Biederman  * Operations on the network namespace
45f256becSEric W. Biederman  */
55f256becSEric W. Biederman #ifndef __NET_NET_NAMESPACE_H
65f256becSEric W. Biederman #define __NET_NET_NAMESPACE_H
75f256becSEric W. Biederman 
860063497SArun Sharma #include <linux/atomic.h>
9c122e14dSReshetova, Elena #include <linux/refcount.h>
105f256becSEric W. Biederman #include <linux/workqueue.h>
115f256becSEric W. Biederman #include <linux/list.h>
12bee95250SDavid S. Miller #include <linux/sysctl.h>
13fbdeaed4STyler Hicks #include <linux/uidgid.h>
145f256becSEric W. Biederman 
156a662719SCong Wang #include <net/flow.h>
168efa6e93SPavel Emelyanov #include <net/netns/core.h>
17852566f5SPavel Emelyanov #include <net/netns/mib.h>
18a0a53c8bSDenis V. Lunev #include <net/netns/unix.h>
192aaef4e4SDenis V. Lunev #include <net/netns/packet.h>
208afd351cSPavel Emelyanov #include <net/netns/ipv4.h>
21b0f159dbSDaniel Lezcano #include <net/netns/ipv6.h>
22ab84be7eSDavid Ahern #include <net/netns/nexthop.h>
23633fc86fSAlexander Aring #include <net/netns/ieee802154_6lowpan.h>
244db67e80SEric W. Biederman #include <net/netns/sctp.h>
2567019cc9SPavel Emelyanov #include <net/netns/dccp.h>
26f3c1a44aSGao feng #include <net/netns/netfilter.h>
278d870052SAlexey Dobriyan #include <net/netns/x_tables.h>
28dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
29dfdb8d79SAlexey Dobriyan #include <net/netns/conntrack.h>
30dfdb8d79SAlexey Dobriyan #endif
3199633ab2SPablo Neira Ayuso #include <net/netns/nftables.h>
32d62ddc21SAlexey Dobriyan #include <net/netns/xfrm.h>
330189197fSEric W. Biederman #include <net/netns/mpls.h>
348e8cda6dSMario Kicherer #include <net/netns/can.h>
351d0dc069SBjörn Töpel #include <net/netns/xdp.h>
36435d5f4bSAl Viro #include <linux/ns_common.h>
3704c52decSPablo Neira Ayuso #include <linux/idr.h>
3804c52decSPablo Neira Ayuso #include <linux/skbuff.h>
39a30c7b42SJiri Pirko #include <linux/notifier.h>
40a0a53c8bSDenis V. Lunev 
41038e7332SEric W. Biederman struct user_namespace;
42457c4cbcSEric W. Biederman struct proc_dir_entry;
432774c7abSEric W. Biederman struct net_device;
4497c53cacSDenis V. Lunev struct sock;
451597fbc0SPavel Emelyanov struct ctl_table_header;
46dec827d1SPavel Emelyanov struct net_generic;
4794e5e308SChristian Brauner struct uevent_sock;
482553d064SJulian Anastasov struct netns_ipvs;
49d58e468bSPetar Penkov struct bpf_prog;
501597fbc0SPavel Emelyanov 
517c28bd0bSEric Dumazet 
527c28bd0bSEric Dumazet #define NETDEV_HASHBITS    8
537c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
547c28bd0bSEric Dumazet 
555f256becSEric W. Biederman struct net {
562a06b898SEric Dumazet 	/* First cache line can be often dirtied.
572a06b898SEric Dumazet 	 * Do not place here read-mostly fields.
582a06b898SEric Dumazet 	 */
59aad12c23SMike Rapoport 	refcount_t		passive;	/* To decide when the network
605f256becSEric W. Biederman 						 * namespace should be freed.
615f256becSEric W. Biederman 						 */
62273c28bcSKirill Tkhai 	refcount_t		count;		/* To decided when the network
63a685e089SAl Viro 						 *  namespace should be shut down.
64a685e089SAl Viro 						 */
658e602ce2SEric Dumazet 	spinlock_t		rules_mod_lock;
668e602ce2SEric Dumazet 
672a06b898SEric Dumazet 	unsigned int		dev_unreg_count;
682a06b898SEric Dumazet 
692a06b898SEric Dumazet 	unsigned int		dev_base_seq;	/* protected by rtnl_mutex */
702a06b898SEric Dumazet 	int			ifindex;
712a06b898SEric Dumazet 
722a06b898SEric Dumazet 	spinlock_t		nsid_lock;
732a06b898SEric Dumazet 	atomic_t		fnhe_genid;
7433cf7c90SEric Dumazet 
755f256becSEric W. Biederman 	struct list_head	list;		/* list of network namespaces */
7619efbd93SKirill Tkhai 	struct list_head	exit_list;	/* To linked to call pernet exit
774420bf21SKirill Tkhai 						 * methods on dead net (
784420bf21SKirill Tkhai 						 * pernet_ops_rwsem read locked),
794420bf21SKirill Tkhai 						 * or to unregister pernet ops
804420bf21SKirill Tkhai 						 * (pernet_ops_rwsem write locked).
8119efbd93SKirill Tkhai 						 */
8265b7b5b9SKirill Tkhai 	struct llist_node	cleanup_list;	/* namespaces on death row */
8365b7b5b9SKirill Tkhai 
849b242610SDavid Howells #ifdef CONFIG_KEYS
859b242610SDavid Howells 	struct key_tag		*key_domain;	/* Key domain of operation tag */
869b242610SDavid Howells #endif
87038e7332SEric W. Biederman 	struct user_namespace   *user_ns;	/* Owning user namespace */
8870328660SEric W. Biederman 	struct ucounts		*ucounts;
890c7aecd4SNicolas Dichtel 	struct idr		netns_ids;
90038e7332SEric W. Biederman 
91435d5f4bSAl Viro 	struct ns_common	ns;
9298f842e6SEric W. Biederman 
932a06b898SEric Dumazet 	struct list_head 	dev_base_head;
94457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net;
95457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net_stat;
96881d966bSEric W. Biederman 
9773455092SAl Viro #ifdef CONFIG_SYSCTL
9873455092SAl Viro 	struct ctl_table_set	sysctls;
9973455092SAl Viro #endif
10095bdfccbSEric W. Biederman 
1018e602ce2SEric Dumazet 	struct sock 		*rtnl;			/* rtnetlink socket */
1028e602ce2SEric Dumazet 	struct sock		*genl_sock;
1032774c7abSEric W. Biederman 
10494e5e308SChristian Brauner 	struct uevent_sock	*uevent_sock;		/* uevent socket */
10594e5e308SChristian Brauner 
106881d966bSEric W. Biederman 	struct hlist_head 	*dev_name_head;
107881d966bSEric W. Biederman 	struct hlist_head	*dev_index_head;
108a30c7b42SJiri Pirko 	struct raw_notifier_head	netdev_chain;
109a30c7b42SJiri Pirko 
1102a06b898SEric Dumazet 	/* Note that @hash_mix can be read millions times per second,
1112a06b898SEric Dumazet 	 * it is critical that it is on a read_mostly cache line.
1122a06b898SEric Dumazet 	 */
1132a06b898SEric Dumazet 	u32			hash_mix;
1142a06b898SEric Dumazet 
1152a06b898SEric Dumazet 	struct net_device       *loopback_dev;          /* The loopback */
11697c53cacSDenis V. Lunev 
1175fd30ee7SDenis V. Lunev 	/* core fib_rules */
1185fd30ee7SDenis V. Lunev 	struct list_head	rules_ops;
1195fd30ee7SDenis V. Lunev 
1208efa6e93SPavel Emelyanov 	struct netns_core	core;
121852566f5SPavel Emelyanov 	struct netns_mib	mib;
1222aaef4e4SDenis V. Lunev 	struct netns_packet	packet;
123a0a53c8bSDenis V. Lunev 	struct netns_unix	unx;
124ab84be7eSDavid Ahern 	struct netns_nexthop	nexthop;
1258afd351cSPavel Emelyanov 	struct netns_ipv4	ipv4;
126dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
127b0f159dbSDaniel Lezcano 	struct netns_ipv6	ipv6;
128b0f159dbSDaniel Lezcano #endif
129633fc86fSAlexander Aring #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
130633fc86fSAlexander Aring 	struct netns_ieee802154_lowpan	ieee802154_lowpan;
131633fc86fSAlexander Aring #endif
1324db67e80SEric W. Biederman #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
1334db67e80SEric W. Biederman 	struct netns_sctp	sctp;
1344db67e80SEric W. Biederman #endif
13567019cc9SPavel Emelyanov #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
13667019cc9SPavel Emelyanov 	struct netns_dccp	dccp;
13767019cc9SPavel Emelyanov #endif
1388d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER
139f3c1a44aSGao feng 	struct netns_nf		nf;
1408d870052SAlexey Dobriyan 	struct netns_xt		xt;
141dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
142dfdb8d79SAlexey Dobriyan 	struct netns_ct		ct;
143dfdb8d79SAlexey Dobriyan #endif
14499633ab2SPablo Neira Ayuso #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
14599633ab2SPablo Neira Ayuso 	struct netns_nftables	nft;
14699633ab2SPablo Neira Ayuso #endif
147c038a767SAmerigo Wang #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
148c038a767SAmerigo Wang 	struct netns_nf_frag	nf_frag;
1499ce7bc03SEric Dumazet 	struct ctl_table_header *nf_frag_frags_hdr;
150c038a767SAmerigo Wang #endif
151cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl;
152cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl_stash;
1533499abb2SAndreas Schultz #if IS_ENABLED(CONFIG_NETFILTER_NETLINK_ACCT)
1543499abb2SAndreas Schultz 	struct list_head        nfnl_acct_list;
1553499abb2SAndreas Schultz #endif
15619576c94SPablo Neira #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
15719576c94SPablo Neira 	struct list_head	nfct_timeout_list;
15819576c94SPablo Neira #endif
1598d870052SAlexey Dobriyan #endif
1603d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE
161b333b3d2SJohannes Berg 	struct sk_buff_head	wext_nlevents;
162b333b3d2SJohannes Berg #endif
1631c87733dSEric Dumazet 	struct net_generic __rcu	*gen;
1648e602ce2SEric Dumazet 
165d58e468bSPetar Penkov 	struct bpf_prog __rcu	*flow_dissector_prog;
166d58e468bSPetar Penkov 
1678e602ce2SEric Dumazet 	/* Note : following structs are cache line aligned */
1688e602ce2SEric Dumazet #ifdef CONFIG_XFRM
1698e602ce2SEric Dumazet 	struct netns_xfrm	xfrm;
1708e602ce2SEric Dumazet #endif
171f318903cSDaniel Borkmann 
172f318903cSDaniel Borkmann 	atomic64_t		net_cookie; /* written once */
173f318903cSDaniel Borkmann 
1748b4d14d8SJunweiZhang #if IS_ENABLED(CONFIG_IP_VS)
17561b1ab45SHans Schillstrom 	struct netns_ipvs	*ipvs;
1768b4d14d8SJunweiZhang #endif
1770189197fSEric W. Biederman #if IS_ENABLED(CONFIG_MPLS)
1780189197fSEric W. Biederman 	struct netns_mpls	mpls;
1790189197fSEric W. Biederman #endif
1808e8cda6dSMario Kicherer #if IS_ENABLED(CONFIG_CAN)
1818e8cda6dSMario Kicherer 	struct netns_can	can;
1828e8cda6dSMario Kicherer #endif
1831d0dc069SBjörn Töpel #ifdef CONFIG_XDP_SOCKETS
1841d0dc069SBjörn Töpel 	struct netns_xdp	xdp;
1851d0dc069SBjörn Töpel #endif
18691b05a7eSOndrej Mosnacek #if IS_ENABLED(CONFIG_CRYPTO_USER)
18791b05a7eSOndrej Mosnacek 	struct sock		*crypto_nlsk;
18891b05a7eSOndrej Mosnacek #endif
18951d7cccfSAndrey Vagin 	struct sock		*diag_nlsk;
1903859a271SKees Cook } __randomize_layout;
1915f256becSEric W. Biederman 
192c0f39322SDenis V. Lunev #include <linux/seq_file_net.h>
193c0f39322SDenis V. Lunev 
1944fabcd71SDaniel Lezcano /* Init's network namespace */
1955f256becSEric W. Biederman extern struct net init_net;
196a4aa834aSDenis V. Lunev 
197d727abcbSEric W. Biederman #ifdef CONFIG_NET_NS
198e67e16eaSJoe Perches struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
199e67e16eaSJoe Perches 			struct net *old_net);
200225c0a01SDenis V. Lunev 
201fbdeaed4STyler Hicks void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid);
202fbdeaed4STyler Hicks 
2037866cc57SFlorian Westphal void net_ns_barrier(void);
204d727abcbSEric W. Biederman #else /* CONFIG_NET_NS */
205d727abcbSEric W. Biederman #include <linux/sched.h>
206d727abcbSEric W. Biederman #include <linux/nsproxy.h>
207038e7332SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags,
208038e7332SEric W. Biederman 	struct user_namespace *user_ns, struct net *old_net)
2099dd776b6SEric W. Biederman {
210d727abcbSEric W. Biederman 	if (flags & CLONE_NEWNET)
211d727abcbSEric W. Biederman 		return ERR_PTR(-EINVAL);
212d727abcbSEric W. Biederman 	return old_net;
2139dd776b6SEric W. Biederman }
2147866cc57SFlorian Westphal 
215fbdeaed4STyler Hicks static inline void net_ns_get_ownership(const struct net *net,
216fbdeaed4STyler Hicks 					kuid_t *uid, kgid_t *gid)
217fbdeaed4STyler Hicks {
218fbdeaed4STyler Hicks 	*uid = GLOBAL_ROOT_UID;
219fbdeaed4STyler Hicks 	*gid = GLOBAL_ROOT_GID;
220fbdeaed4STyler Hicks }
221fbdeaed4STyler Hicks 
2227866cc57SFlorian Westphal static inline void net_ns_barrier(void) {}
223d727abcbSEric W. Biederman #endif /* CONFIG_NET_NS */
224225c0a01SDenis V. Lunev 
225225c0a01SDenis V. Lunev 
226225c0a01SDenis V. Lunev extern struct list_head net_namespace_list;
2279dd776b6SEric W. Biederman 
228e67e16eaSJoe Perches struct net *get_net_ns_by_pid(pid_t pid);
2290f5258cdSStefan Hajnoczi struct net *get_net_ns_by_fd(int fd);
23030ffee84SJohannes Berg 
231535d3ae9SRashika Kheria #ifdef CONFIG_SYSCTL
232535d3ae9SRashika Kheria void ipx_register_sysctl(void);
233535d3ae9SRashika Kheria void ipx_unregister_sysctl(void);
234535d3ae9SRashika Kheria #else
235535d3ae9SRashika Kheria #define ipx_register_sysctl()
236535d3ae9SRashika Kheria #define ipx_unregister_sysctl()
237535d3ae9SRashika Kheria #endif
238535d3ae9SRashika Kheria 
239d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS
240e67e16eaSJoe Perches void __put_net(struct net *net);
2415f256becSEric W. Biederman 
2425f256becSEric W. Biederman static inline struct net *get_net(struct net *net)
2435f256becSEric W. Biederman {
244273c28bcSKirill Tkhai 	refcount_inc(&net->count);
2455f256becSEric W. Biederman 	return net;
2465f256becSEric W. Biederman }
2475f256becSEric W. Biederman 
248077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net)
249077130c0SEric W. Biederman {
250077130c0SEric W. Biederman 	/* Used when we know struct net exists but we
251077130c0SEric W. Biederman 	 * aren't guaranteed a previous reference count
252077130c0SEric W. Biederman 	 * exists.  If the reference count is zero this
253077130c0SEric W. Biederman 	 * function fails and returns NULL.
254077130c0SEric W. Biederman 	 */
255273c28bcSKirill Tkhai 	if (!refcount_inc_not_zero(&net->count))
256077130c0SEric W. Biederman 		net = NULL;
257077130c0SEric W. Biederman 	return net;
258077130c0SEric W. Biederman }
259077130c0SEric W. Biederman 
2605f256becSEric W. Biederman static inline void put_net(struct net *net)
2615f256becSEric W. Biederman {
262273c28bcSKirill Tkhai 	if (refcount_dec_and_test(&net->count))
2635f256becSEric W. Biederman 		__put_net(net);
2645f256becSEric W. Biederman }
2655f256becSEric W. Biederman 
266878628fbSYOSHIFUJI Hideaki static inline
267878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
268878628fbSYOSHIFUJI Hideaki {
269878628fbSYOSHIFUJI Hideaki 	return net1 == net2;
270878628fbSYOSHIFUJI Hideaki }
271a685e089SAl Viro 
2724ee806d5SDan Streetman static inline int check_net(const struct net *net)
2734ee806d5SDan Streetman {
2743e3ab9ccSDavid S. Miller 	return refcount_read(&net->count) != 0;
2754ee806d5SDan Streetman }
2764ee806d5SDan Streetman 
277e67e16eaSJoe Perches void net_drop_ns(void *);
278a685e089SAl Viro 
279f318903cSDaniel Borkmann u64 net_gen_cookie(struct net *net);
280f318903cSDaniel Borkmann 
281d4655795SPavel Emelyanov #else
282b9f75f45SEric W. Biederman 
283d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net)
284d4655795SPavel Emelyanov {
285d4655795SPavel Emelyanov 	return net;
286d4655795SPavel Emelyanov }
287d4655795SPavel Emelyanov 
288d4655795SPavel Emelyanov static inline void put_net(struct net *net)
289d4655795SPavel Emelyanov {
290d4655795SPavel Emelyanov }
291d4655795SPavel Emelyanov 
292d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net)
293d4655795SPavel Emelyanov {
294d4655795SPavel Emelyanov 	return net;
295d4655795SPavel Emelyanov }
296878628fbSYOSHIFUJI Hideaki 
297878628fbSYOSHIFUJI Hideaki static inline
298878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
299878628fbSYOSHIFUJI Hideaki {
300878628fbSYOSHIFUJI Hideaki 	return 1;
301878628fbSYOSHIFUJI Hideaki }
302a685e089SAl Viro 
3034ee806d5SDan Streetman static inline int check_net(const struct net *net)
3044ee806d5SDan Streetman {
3054ee806d5SDan Streetman 	return 1;
3064ee806d5SDan Streetman }
3074ee806d5SDan Streetman 
308f318903cSDaniel Borkmann static inline u64 net_gen_cookie(struct net *net)
309f318903cSDaniel Borkmann {
310f318903cSDaniel Borkmann 	return 0;
311f318903cSDaniel Borkmann }
312f318903cSDaniel Borkmann 
313a685e089SAl Viro #define net_drop_ns NULL
314d4655795SPavel Emelyanov #endif
3155f256becSEric W. Biederman 
3165d1e4468SDenis V. Lunev 
3170c5c9fb5SEric W. Biederman typedef struct {
3188f424b5fSEric Dumazet #ifdef CONFIG_NET_NS
3190c5c9fb5SEric W. Biederman 	struct net *net;
3208f424b5fSEric Dumazet #endif
3210c5c9fb5SEric W. Biederman } possible_net_t;
3220c5c9fb5SEric W. Biederman 
3230c5c9fb5SEric W. Biederman static inline void write_pnet(possible_net_t *pnet, struct net *net)
3240c5c9fb5SEric W. Biederman {
3250c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS
3260c5c9fb5SEric W. Biederman 	pnet->net = net;
3270c5c9fb5SEric W. Biederman #endif
3280c5c9fb5SEric W. Biederman }
3290c5c9fb5SEric W. Biederman 
3300c5c9fb5SEric W. Biederman static inline struct net *read_pnet(const possible_net_t *pnet)
3310c5c9fb5SEric W. Biederman {
3320c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS
3330c5c9fb5SEric W. Biederman 	return pnet->net;
3340c5c9fb5SEric W. Biederman #else
3350c5c9fb5SEric W. Biederman 	return &init_net;
3360c5c9fb5SEric W. Biederman #endif
3370c5c9fb5SEric W. Biederman }
3385d1e4468SDenis V. Lunev 
339f0b07bb1SKirill Tkhai /* Protected by net_rwsem */
3405f256becSEric W. Biederman #define for_each_net(VAR)				\
3415f256becSEric W. Biederman 	list_for_each_entry(VAR, &net_namespace_list, list)
342afa0df59SJiri Pirko #define for_each_net_continue_reverse(VAR)		\
343afa0df59SJiri Pirko 	list_for_each_entry_continue_reverse(VAR, &net_namespace_list, list)
34411a28d37SJohannes Berg #define for_each_net_rcu(VAR)				\
34511a28d37SJohannes Berg 	list_for_each_entry_rcu(VAR, &net_namespace_list, list)
34611a28d37SJohannes Berg 
3474665079cSPavel Emelyanov #ifdef CONFIG_NET_NS
3484665079cSPavel Emelyanov #define __net_init
3494665079cSPavel Emelyanov #define __net_exit
350022cbae6SDenis V. Lunev #define __net_initdata
35104a6f82cSAndi Kleen #define __net_initconst
3524665079cSPavel Emelyanov #else
3534665079cSPavel Emelyanov #define __net_init	__init
354bd721ea7SFabian Frederick #define __net_exit	__ref
355022cbae6SDenis V. Lunev #define __net_initdata	__initdata
35604a6f82cSAndi Kleen #define __net_initconst	__initconst
3574665079cSPavel Emelyanov #endif
3585f256becSEric W. Biederman 
359d4e4fdf9SGuillaume Nault int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp);
36056f200c7SGuillaume Nault int peernet2id(const struct net *net, struct net *peer);
36156f200c7SGuillaume Nault bool peernet_has_id(const struct net *net, struct net *peer);
36256f200c7SGuillaume Nault struct net *get_net_ns_by_id(const struct net *net, int id);
3630c7aecd4SNicolas Dichtel 
3645f256becSEric W. Biederman struct pernet_operations {
3655f256becSEric W. Biederman 	struct list_head list;
3666056415dSKirill Tkhai 	/*
3676056415dSKirill Tkhai 	 * Below methods are called without any exclusive locks.
3686056415dSKirill Tkhai 	 * More than one net may be constructed and destructed
3696056415dSKirill Tkhai 	 * in parallel on several cpus. Every pernet_operations
3706056415dSKirill Tkhai 	 * have to keep in mind all other pernet_operations and
3716056415dSKirill Tkhai 	 * to introduce a locking, if they share common resources.
3726056415dSKirill Tkhai 	 *
3738518e9bbSKirill Tkhai 	 * The only time they are called with exclusive lock is
3748518e9bbSKirill Tkhai 	 * from register_pernet_subsys(), unregister_pernet_subsys()
3758518e9bbSKirill Tkhai 	 * register_pernet_device() and unregister_pernet_device().
3768518e9bbSKirill Tkhai 	 *
3776056415dSKirill Tkhai 	 * Exit methods using blocking RCU primitives, such as
3786056415dSKirill Tkhai 	 * synchronize_rcu(), should be implemented via exit_batch.
3796056415dSKirill Tkhai 	 * Then, destruction of a group of net requires single
3806056415dSKirill Tkhai 	 * synchronize_rcu() related to these pernet_operations,
3816056415dSKirill Tkhai 	 * instead of separate synchronize_rcu() for every net.
3826056415dSKirill Tkhai 	 * Please, avoid synchronize_rcu() at all, where it's possible.
383d7d99872SEric Dumazet 	 *
384d7d99872SEric Dumazet 	 * Note that a combination of pre_exit() and exit() can
385d7d99872SEric Dumazet 	 * be used, since a synchronize_rcu() is guaranteed between
386d7d99872SEric Dumazet 	 * the calls.
3876056415dSKirill Tkhai 	 */
3885f256becSEric W. Biederman 	int (*init)(struct net *net);
389d7d99872SEric Dumazet 	void (*pre_exit)(struct net *net);
3905f256becSEric W. Biederman 	void (*exit)(struct net *net);
39172ad937aSEric W. Biederman 	void (*exit_batch)(struct list_head *net_exit_list);
392c7d03a00SAlexey Dobriyan 	unsigned int *id;
393f875bae0SEric W. Biederman 	size_t size;
3945f256becSEric W. Biederman };
3955f256becSEric W. Biederman 
39617edde52SEric W. Biederman /*
39717edde52SEric W. Biederman  * Use these carefully.  If you implement a network device and it
39817edde52SEric W. Biederman  * needs per network namespace operations use device pernet operations,
39917edde52SEric W. Biederman  * otherwise use pernet subsys operations.
40017edde52SEric W. Biederman  *
4014edf547bSJohannes Berg  * Network interfaces need to be removed from a dying netns _before_
4024edf547bSJohannes Berg  * subsys notifiers can be called, as most of the network code cleanup
4034edf547bSJohannes Berg  * (which is done from subsys notifiers) runs with the assumption that
4044edf547bSJohannes Berg  * dev_remove_pack has been called so no new packets will arrive during
4054edf547bSJohannes Berg  * and after the cleanup functions have been called.  dev_remove_pack
4064edf547bSJohannes Berg  * is not per namespace so instead the guarantee of no more packets
4074edf547bSJohannes Berg  * arriving in a network namespace is provided by ensuring that all
4084edf547bSJohannes Berg  * network devices and all sockets have left the network namespace
4094edf547bSJohannes Berg  * before the cleanup methods are called.
41017edde52SEric W. Biederman  *
41117edde52SEric W. Biederman  * For the longest time the ipv4 icmp code was registered as a pernet
41217edde52SEric W. Biederman  * device which caused kernel oops, and panics during network
41317edde52SEric W. Biederman  * namespace cleanup.   So please don't get this wrong.
41417edde52SEric W. Biederman  */
415e67e16eaSJoe Perches int register_pernet_subsys(struct pernet_operations *);
416e67e16eaSJoe Perches void unregister_pernet_subsys(struct pernet_operations *);
417e67e16eaSJoe Perches int register_pernet_device(struct pernet_operations *);
418e67e16eaSJoe Perches void unregister_pernet_device(struct pernet_operations *);
419f875bae0SEric W. Biederman 
42095bdfccbSEric W. Biederman struct ctl_table;
42195bdfccbSEric W. Biederman struct ctl_table_header;
422d62c612eSPavel Emelyanov 
4232ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL
424e67e16eaSJoe Perches int net_sysctl_init(void);
425e67e16eaSJoe Perches struct ctl_table_header *register_net_sysctl(struct net *net, const char *path,
426e67e16eaSJoe Perches 					     struct ctl_table *table);
427e67e16eaSJoe Perches void unregister_net_sysctl_table(struct ctl_table_header *header);
42848c74958SEric W. Biederman #else
42948c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; }
43048c74958SEric W. Biederman static inline struct ctl_table_header *register_net_sysctl(struct net *net,
43148c74958SEric W. Biederman 	const char *path, struct ctl_table *table)
43248c74958SEric W. Biederman {
43348c74958SEric W. Biederman 	return NULL;
43448c74958SEric W. Biederman }
43548c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
43648c74958SEric W. Biederman {
43748c74958SEric W. Biederman }
43848c74958SEric W. Biederman #endif
43948c74958SEric W. Biederman 
44056f200c7SGuillaume Nault static inline int rt_genid_ipv4(const struct net *net)
441b42664f8SNicolas Dichtel {
442ca4c3fc2Sfan.du 	return atomic_read(&net->ipv4.rt_genid);
443b42664f8SNicolas Dichtel }
444b42664f8SNicolas Dichtel 
445ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv4(struct net *net)
446b42664f8SNicolas Dichtel {
447ca4c3fc2Sfan.du 	atomic_inc(&net->ipv4.rt_genid);
448ca4c3fc2Sfan.du }
449ca4c3fc2Sfan.du 
450705f1c86SHannes Frederic Sowa extern void (*__fib6_flush_trees)(struct net *net);
451ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv6(struct net *net)
452ca4c3fc2Sfan.du {
453705f1c86SHannes Frederic Sowa 	if (__fib6_flush_trees)
454705f1c86SHannes Frederic Sowa 		__fib6_flush_trees(net);
455ca4c3fc2Sfan.du }
456ca4c3fc2Sfan.du 
457599018a7SLuis R. Rodriguez #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
458599018a7SLuis R. Rodriguez static inline struct netns_ieee802154_lowpan *
459599018a7SLuis R. Rodriguez net_ieee802154_lowpan(struct net *net)
460599018a7SLuis R. Rodriguez {
461599018a7SLuis R. Rodriguez 	return &net->ieee802154_lowpan;
462599018a7SLuis R. Rodriguez }
463599018a7SLuis R. Rodriguez #endif
464599018a7SLuis R. Rodriguez 
465ca4c3fc2Sfan.du /* For callers who don't really care about whether it's IPv4 or IPv6 */
466ca4c3fc2Sfan.du static inline void rt_genid_bump_all(struct net *net)
467ca4c3fc2Sfan.du {
468ca4c3fc2Sfan.du 	rt_genid_bump_ipv4(net);
469ca4c3fc2Sfan.du 	rt_genid_bump_ipv6(net);
470b42664f8SNicolas Dichtel }
47195bdfccbSEric W. Biederman 
47256f200c7SGuillaume Nault static inline int fnhe_genid(const struct net *net)
4735aad1de5STimo Teräs {
4745aad1de5STimo Teräs 	return atomic_read(&net->fnhe_genid);
4755aad1de5STimo Teräs }
4765aad1de5STimo Teräs 
4775aad1de5STimo Teräs static inline void fnhe_genid_bump(struct net *net)
4785aad1de5STimo Teräs {
4795aad1de5STimo Teräs 	atomic_inc(&net->fnhe_genid);
4805aad1de5STimo Teräs }
4815aad1de5STimo Teräs 
4825f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */
483