xref: /openbmc/linux/include/net/net_namespace.h (revision 2a06b898)
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>
39a0a53c8bSDenis V. Lunev 
40038e7332SEric W. Biederman struct user_namespace;
41457c4cbcSEric W. Biederman struct proc_dir_entry;
422774c7abSEric W. Biederman struct net_device;
4397c53cacSDenis V. Lunev struct sock;
441597fbc0SPavel Emelyanov struct ctl_table_header;
45dec827d1SPavel Emelyanov struct net_generic;
4694e5e308SChristian Brauner struct uevent_sock;
472553d064SJulian Anastasov struct netns_ipvs;
48d58e468bSPetar Penkov struct bpf_prog;
491597fbc0SPavel Emelyanov 
507c28bd0bSEric Dumazet 
517c28bd0bSEric Dumazet #define NETDEV_HASHBITS    8
527c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
537c28bd0bSEric Dumazet 
545f256becSEric W. Biederman struct net {
552a06b898SEric Dumazet 	/* First cache line can be often dirtied.
562a06b898SEric Dumazet 	 * Do not place here read-mostly fields.
572a06b898SEric Dumazet 	 */
58aad12c23SMike Rapoport 	refcount_t		passive;	/* To decide when the network
595f256becSEric W. Biederman 						 * namespace should be freed.
605f256becSEric W. Biederman 						 */
61273c28bcSKirill Tkhai 	refcount_t		count;		/* To decided when the network
62a685e089SAl Viro 						 *  namespace should be shut down.
63a685e089SAl Viro 						 */
648e602ce2SEric Dumazet 	spinlock_t		rules_mod_lock;
658e602ce2SEric Dumazet 
662a06b898SEric Dumazet 	unsigned int		dev_unreg_count;
672a06b898SEric Dumazet 
682a06b898SEric Dumazet 	unsigned int		dev_base_seq;	/* protected by rtnl_mutex */
692a06b898SEric Dumazet 	int			ifindex;
702a06b898SEric Dumazet 
712a06b898SEric Dumazet 	spinlock_t		nsid_lock;
722a06b898SEric Dumazet 	atomic_t		fnhe_genid;
7333cf7c90SEric Dumazet 
745f256becSEric W. Biederman 	struct list_head	list;		/* list of network namespaces */
7519efbd93SKirill Tkhai 	struct list_head	exit_list;	/* To linked to call pernet exit
764420bf21SKirill Tkhai 						 * methods on dead net (
774420bf21SKirill Tkhai 						 * pernet_ops_rwsem read locked),
784420bf21SKirill Tkhai 						 * or to unregister pernet ops
794420bf21SKirill Tkhai 						 * (pernet_ops_rwsem write locked).
8019efbd93SKirill Tkhai 						 */
8165b7b5b9SKirill Tkhai 	struct llist_node	cleanup_list;	/* namespaces on death row */
8265b7b5b9SKirill Tkhai 
839b242610SDavid Howells #ifdef CONFIG_KEYS
849b242610SDavid Howells 	struct key_tag		*key_domain;	/* Key domain of operation tag */
859b242610SDavid Howells #endif
86038e7332SEric W. Biederman 	struct user_namespace   *user_ns;	/* Owning user namespace */
8770328660SEric W. Biederman 	struct ucounts		*ucounts;
880c7aecd4SNicolas Dichtel 	struct idr		netns_ids;
89038e7332SEric W. Biederman 
90435d5f4bSAl Viro 	struct ns_common	ns;
9198f842e6SEric W. Biederman 
922a06b898SEric Dumazet 	struct list_head 	dev_base_head;
93457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net;
94457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net_stat;
95881d966bSEric W. Biederman 
9673455092SAl Viro #ifdef CONFIG_SYSCTL
9773455092SAl Viro 	struct ctl_table_set	sysctls;
9873455092SAl Viro #endif
9995bdfccbSEric W. Biederman 
1008e602ce2SEric Dumazet 	struct sock 		*rtnl;			/* rtnetlink socket */
1018e602ce2SEric Dumazet 	struct sock		*genl_sock;
1022774c7abSEric W. Biederman 
10394e5e308SChristian Brauner 	struct uevent_sock	*uevent_sock;		/* uevent socket */
10494e5e308SChristian Brauner 
105881d966bSEric W. Biederman 	struct hlist_head 	*dev_name_head;
106881d966bSEric W. Biederman 	struct hlist_head	*dev_index_head;
1072a06b898SEric Dumazet 	/* Note that @hash_mix can be read millions times per second,
1082a06b898SEric Dumazet 	 * it is critical that it is on a read_mostly cache line.
1092a06b898SEric Dumazet 	 */
1102a06b898SEric Dumazet 	u32			hash_mix;
1112a06b898SEric Dumazet 
1122a06b898SEric Dumazet 	struct net_device       *loopback_dev;          /* The loopback */
11397c53cacSDenis V. Lunev 
1145fd30ee7SDenis V. Lunev 	/* core fib_rules */
1155fd30ee7SDenis V. Lunev 	struct list_head	rules_ops;
1165fd30ee7SDenis V. Lunev 
1178efa6e93SPavel Emelyanov 	struct netns_core	core;
118852566f5SPavel Emelyanov 	struct netns_mib	mib;
1192aaef4e4SDenis V. Lunev 	struct netns_packet	packet;
120a0a53c8bSDenis V. Lunev 	struct netns_unix	unx;
121ab84be7eSDavid Ahern 	struct netns_nexthop	nexthop;
1228afd351cSPavel Emelyanov 	struct netns_ipv4	ipv4;
123dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
124b0f159dbSDaniel Lezcano 	struct netns_ipv6	ipv6;
125b0f159dbSDaniel Lezcano #endif
126633fc86fSAlexander Aring #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
127633fc86fSAlexander Aring 	struct netns_ieee802154_lowpan	ieee802154_lowpan;
128633fc86fSAlexander Aring #endif
1294db67e80SEric W. Biederman #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
1304db67e80SEric W. Biederman 	struct netns_sctp	sctp;
1314db67e80SEric W. Biederman #endif
13267019cc9SPavel Emelyanov #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
13367019cc9SPavel Emelyanov 	struct netns_dccp	dccp;
13467019cc9SPavel Emelyanov #endif
1358d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER
136f3c1a44aSGao feng 	struct netns_nf		nf;
1378d870052SAlexey Dobriyan 	struct netns_xt		xt;
138dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
139dfdb8d79SAlexey Dobriyan 	struct netns_ct		ct;
140dfdb8d79SAlexey Dobriyan #endif
14199633ab2SPablo Neira Ayuso #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
14299633ab2SPablo Neira Ayuso 	struct netns_nftables	nft;
14399633ab2SPablo Neira Ayuso #endif
144c038a767SAmerigo Wang #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
145c038a767SAmerigo Wang 	struct netns_nf_frag	nf_frag;
1469ce7bc03SEric Dumazet 	struct ctl_table_header *nf_frag_frags_hdr;
147c038a767SAmerigo Wang #endif
148cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl;
149cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl_stash;
1503499abb2SAndreas Schultz #if IS_ENABLED(CONFIG_NETFILTER_NETLINK_ACCT)
1513499abb2SAndreas Schultz 	struct list_head        nfnl_acct_list;
1523499abb2SAndreas Schultz #endif
15319576c94SPablo Neira #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
15419576c94SPablo Neira 	struct list_head	nfct_timeout_list;
15519576c94SPablo Neira #endif
1568d870052SAlexey Dobriyan #endif
1573d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE
158b333b3d2SJohannes Berg 	struct sk_buff_head	wext_nlevents;
159b333b3d2SJohannes Berg #endif
1601c87733dSEric Dumazet 	struct net_generic __rcu	*gen;
1618e602ce2SEric Dumazet 
162d58e468bSPetar Penkov 	struct bpf_prog __rcu	*flow_dissector_prog;
163d58e468bSPetar Penkov 
1648e602ce2SEric Dumazet 	/* Note : following structs are cache line aligned */
1658e602ce2SEric Dumazet #ifdef CONFIG_XFRM
1668e602ce2SEric Dumazet 	struct netns_xfrm	xfrm;
1678e602ce2SEric Dumazet #endif
1688b4d14d8SJunweiZhang #if IS_ENABLED(CONFIG_IP_VS)
16961b1ab45SHans Schillstrom 	struct netns_ipvs	*ipvs;
1708b4d14d8SJunweiZhang #endif
1710189197fSEric W. Biederman #if IS_ENABLED(CONFIG_MPLS)
1720189197fSEric W. Biederman 	struct netns_mpls	mpls;
1730189197fSEric W. Biederman #endif
1748e8cda6dSMario Kicherer #if IS_ENABLED(CONFIG_CAN)
1758e8cda6dSMario Kicherer 	struct netns_can	can;
1768e8cda6dSMario Kicherer #endif
1771d0dc069SBjörn Töpel #ifdef CONFIG_XDP_SOCKETS
1781d0dc069SBjörn Töpel 	struct netns_xdp	xdp;
1791d0dc069SBjörn Töpel #endif
18091b05a7eSOndrej Mosnacek #if IS_ENABLED(CONFIG_CRYPTO_USER)
18191b05a7eSOndrej Mosnacek 	struct sock		*crypto_nlsk;
18291b05a7eSOndrej Mosnacek #endif
18351d7cccfSAndrey Vagin 	struct sock		*diag_nlsk;
1843859a271SKees Cook } __randomize_layout;
1855f256becSEric W. Biederman 
186c0f39322SDenis V. Lunev #include <linux/seq_file_net.h>
187c0f39322SDenis V. Lunev 
1884fabcd71SDaniel Lezcano /* Init's network namespace */
1895f256becSEric W. Biederman extern struct net init_net;
190a4aa834aSDenis V. Lunev 
191d727abcbSEric W. Biederman #ifdef CONFIG_NET_NS
192e67e16eaSJoe Perches struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
193e67e16eaSJoe Perches 			struct net *old_net);
194225c0a01SDenis V. Lunev 
195fbdeaed4STyler Hicks void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid);
196fbdeaed4STyler Hicks 
1977866cc57SFlorian Westphal void net_ns_barrier(void);
198d727abcbSEric W. Biederman #else /* CONFIG_NET_NS */
199d727abcbSEric W. Biederman #include <linux/sched.h>
200d727abcbSEric W. Biederman #include <linux/nsproxy.h>
201038e7332SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags,
202038e7332SEric W. Biederman 	struct user_namespace *user_ns, struct net *old_net)
2039dd776b6SEric W. Biederman {
204d727abcbSEric W. Biederman 	if (flags & CLONE_NEWNET)
205d727abcbSEric W. Biederman 		return ERR_PTR(-EINVAL);
206d727abcbSEric W. Biederman 	return old_net;
2079dd776b6SEric W. Biederman }
2087866cc57SFlorian Westphal 
209fbdeaed4STyler Hicks static inline void net_ns_get_ownership(const struct net *net,
210fbdeaed4STyler Hicks 					kuid_t *uid, kgid_t *gid)
211fbdeaed4STyler Hicks {
212fbdeaed4STyler Hicks 	*uid = GLOBAL_ROOT_UID;
213fbdeaed4STyler Hicks 	*gid = GLOBAL_ROOT_GID;
214fbdeaed4STyler Hicks }
215fbdeaed4STyler Hicks 
2167866cc57SFlorian Westphal static inline void net_ns_barrier(void) {}
217d727abcbSEric W. Biederman #endif /* CONFIG_NET_NS */
218225c0a01SDenis V. Lunev 
219225c0a01SDenis V. Lunev 
220225c0a01SDenis V. Lunev extern struct list_head net_namespace_list;
2219dd776b6SEric W. Biederman 
222e67e16eaSJoe Perches struct net *get_net_ns_by_pid(pid_t pid);
2230f5258cdSStefan Hajnoczi struct net *get_net_ns_by_fd(int fd);
22430ffee84SJohannes Berg 
225535d3ae9SRashika Kheria #ifdef CONFIG_SYSCTL
226535d3ae9SRashika Kheria void ipx_register_sysctl(void);
227535d3ae9SRashika Kheria void ipx_unregister_sysctl(void);
228535d3ae9SRashika Kheria #else
229535d3ae9SRashika Kheria #define ipx_register_sysctl()
230535d3ae9SRashika Kheria #define ipx_unregister_sysctl()
231535d3ae9SRashika Kheria #endif
232535d3ae9SRashika Kheria 
233d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS
234e67e16eaSJoe Perches void __put_net(struct net *net);
2355f256becSEric W. Biederman 
2365f256becSEric W. Biederman static inline struct net *get_net(struct net *net)
2375f256becSEric W. Biederman {
238273c28bcSKirill Tkhai 	refcount_inc(&net->count);
2395f256becSEric W. Biederman 	return net;
2405f256becSEric W. Biederman }
2415f256becSEric W. Biederman 
242077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net)
243077130c0SEric W. Biederman {
244077130c0SEric W. Biederman 	/* Used when we know struct net exists but we
245077130c0SEric W. Biederman 	 * aren't guaranteed a previous reference count
246077130c0SEric W. Biederman 	 * exists.  If the reference count is zero this
247077130c0SEric W. Biederman 	 * function fails and returns NULL.
248077130c0SEric W. Biederman 	 */
249273c28bcSKirill Tkhai 	if (!refcount_inc_not_zero(&net->count))
250077130c0SEric W. Biederman 		net = NULL;
251077130c0SEric W. Biederman 	return net;
252077130c0SEric W. Biederman }
253077130c0SEric W. Biederman 
2545f256becSEric W. Biederman static inline void put_net(struct net *net)
2555f256becSEric W. Biederman {
256273c28bcSKirill Tkhai 	if (refcount_dec_and_test(&net->count))
2575f256becSEric W. Biederman 		__put_net(net);
2585f256becSEric W. Biederman }
2595f256becSEric W. Biederman 
260878628fbSYOSHIFUJI Hideaki static inline
261878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
262878628fbSYOSHIFUJI Hideaki {
263878628fbSYOSHIFUJI Hideaki 	return net1 == net2;
264878628fbSYOSHIFUJI Hideaki }
265a685e089SAl Viro 
2664ee806d5SDan Streetman static inline int check_net(const struct net *net)
2674ee806d5SDan Streetman {
2683e3ab9ccSDavid S. Miller 	return refcount_read(&net->count) != 0;
2694ee806d5SDan Streetman }
2704ee806d5SDan Streetman 
271e67e16eaSJoe Perches void net_drop_ns(void *);
272a685e089SAl Viro 
273d4655795SPavel Emelyanov #else
274b9f75f45SEric W. Biederman 
275d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net)
276d4655795SPavel Emelyanov {
277d4655795SPavel Emelyanov 	return net;
278d4655795SPavel Emelyanov }
279d4655795SPavel Emelyanov 
280d4655795SPavel Emelyanov static inline void put_net(struct net *net)
281d4655795SPavel Emelyanov {
282d4655795SPavel Emelyanov }
283d4655795SPavel Emelyanov 
284d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net)
285d4655795SPavel Emelyanov {
286d4655795SPavel Emelyanov 	return net;
287d4655795SPavel Emelyanov }
288878628fbSYOSHIFUJI Hideaki 
289878628fbSYOSHIFUJI Hideaki static inline
290878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
291878628fbSYOSHIFUJI Hideaki {
292878628fbSYOSHIFUJI Hideaki 	return 1;
293878628fbSYOSHIFUJI Hideaki }
294a685e089SAl Viro 
2954ee806d5SDan Streetman static inline int check_net(const struct net *net)
2964ee806d5SDan Streetman {
2974ee806d5SDan Streetman 	return 1;
2984ee806d5SDan Streetman }
2994ee806d5SDan Streetman 
300a685e089SAl Viro #define net_drop_ns NULL
301d4655795SPavel Emelyanov #endif
3025f256becSEric W. Biederman 
3035d1e4468SDenis V. Lunev 
3040c5c9fb5SEric W. Biederman typedef struct {
3058f424b5fSEric Dumazet #ifdef CONFIG_NET_NS
3060c5c9fb5SEric W. Biederman 	struct net *net;
3078f424b5fSEric Dumazet #endif
3080c5c9fb5SEric W. Biederman } possible_net_t;
3090c5c9fb5SEric W. Biederman 
3100c5c9fb5SEric W. Biederman static inline void write_pnet(possible_net_t *pnet, struct net *net)
3110c5c9fb5SEric W. Biederman {
3120c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS
3130c5c9fb5SEric W. Biederman 	pnet->net = net;
3140c5c9fb5SEric W. Biederman #endif
3150c5c9fb5SEric W. Biederman }
3160c5c9fb5SEric W. Biederman 
3170c5c9fb5SEric W. Biederman static inline struct net *read_pnet(const possible_net_t *pnet)
3180c5c9fb5SEric W. Biederman {
3190c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS
3200c5c9fb5SEric W. Biederman 	return pnet->net;
3210c5c9fb5SEric W. Biederman #else
3220c5c9fb5SEric W. Biederman 	return &init_net;
3230c5c9fb5SEric W. Biederman #endif
3240c5c9fb5SEric W. Biederman }
3255d1e4468SDenis V. Lunev 
326f0b07bb1SKirill Tkhai /* Protected by net_rwsem */
3275f256becSEric W. Biederman #define for_each_net(VAR)				\
3285f256becSEric W. Biederman 	list_for_each_entry(VAR, &net_namespace_list, list)
3295f256becSEric W. Biederman 
33011a28d37SJohannes Berg #define for_each_net_rcu(VAR)				\
33111a28d37SJohannes Berg 	list_for_each_entry_rcu(VAR, &net_namespace_list, list)
33211a28d37SJohannes Berg 
3334665079cSPavel Emelyanov #ifdef CONFIG_NET_NS
3344665079cSPavel Emelyanov #define __net_init
3354665079cSPavel Emelyanov #define __net_exit
336022cbae6SDenis V. Lunev #define __net_initdata
33704a6f82cSAndi Kleen #define __net_initconst
3384665079cSPavel Emelyanov #else
3394665079cSPavel Emelyanov #define __net_init	__init
340bd721ea7SFabian Frederick #define __net_exit	__ref
341022cbae6SDenis V. Lunev #define __net_initdata	__initdata
34204a6f82cSAndi Kleen #define __net_initconst	__initconst
3434665079cSPavel Emelyanov #endif
3445f256becSEric W. Biederman 
3457a0877d4SNicolas Dichtel int peernet2id_alloc(struct net *net, struct net *peer);
34659324cf3SNicolas Dichtel int peernet2id(struct net *net, struct net *peer);
34759324cf3SNicolas Dichtel bool peernet_has_id(struct net *net, struct net *peer);
3480c7aecd4SNicolas Dichtel struct net *get_net_ns_by_id(struct net *net, int id);
3490c7aecd4SNicolas Dichtel 
3505f256becSEric W. Biederman struct pernet_operations {
3515f256becSEric W. Biederman 	struct list_head list;
3526056415dSKirill Tkhai 	/*
3536056415dSKirill Tkhai 	 * Below methods are called without any exclusive locks.
3546056415dSKirill Tkhai 	 * More than one net may be constructed and destructed
3556056415dSKirill Tkhai 	 * in parallel on several cpus. Every pernet_operations
3566056415dSKirill Tkhai 	 * have to keep in mind all other pernet_operations and
3576056415dSKirill Tkhai 	 * to introduce a locking, if they share common resources.
3586056415dSKirill Tkhai 	 *
3598518e9bbSKirill Tkhai 	 * The only time they are called with exclusive lock is
3608518e9bbSKirill Tkhai 	 * from register_pernet_subsys(), unregister_pernet_subsys()
3618518e9bbSKirill Tkhai 	 * register_pernet_device() and unregister_pernet_device().
3628518e9bbSKirill Tkhai 	 *
3636056415dSKirill Tkhai 	 * Exit methods using blocking RCU primitives, such as
3646056415dSKirill Tkhai 	 * synchronize_rcu(), should be implemented via exit_batch.
3656056415dSKirill Tkhai 	 * Then, destruction of a group of net requires single
3666056415dSKirill Tkhai 	 * synchronize_rcu() related to these pernet_operations,
3676056415dSKirill Tkhai 	 * instead of separate synchronize_rcu() for every net.
3686056415dSKirill Tkhai 	 * Please, avoid synchronize_rcu() at all, where it's possible.
369d7d99872SEric Dumazet 	 *
370d7d99872SEric Dumazet 	 * Note that a combination of pre_exit() and exit() can
371d7d99872SEric Dumazet 	 * be used, since a synchronize_rcu() is guaranteed between
372d7d99872SEric Dumazet 	 * the calls.
3736056415dSKirill Tkhai 	 */
3745f256becSEric W. Biederman 	int (*init)(struct net *net);
375d7d99872SEric Dumazet 	void (*pre_exit)(struct net *net);
3765f256becSEric W. Biederman 	void (*exit)(struct net *net);
37772ad937aSEric W. Biederman 	void (*exit_batch)(struct list_head *net_exit_list);
378c7d03a00SAlexey Dobriyan 	unsigned int *id;
379f875bae0SEric W. Biederman 	size_t size;
3805f256becSEric W. Biederman };
3815f256becSEric W. Biederman 
38217edde52SEric W. Biederman /*
38317edde52SEric W. Biederman  * Use these carefully.  If you implement a network device and it
38417edde52SEric W. Biederman  * needs per network namespace operations use device pernet operations,
38517edde52SEric W. Biederman  * otherwise use pernet subsys operations.
38617edde52SEric W. Biederman  *
3874edf547bSJohannes Berg  * Network interfaces need to be removed from a dying netns _before_
3884edf547bSJohannes Berg  * subsys notifiers can be called, as most of the network code cleanup
3894edf547bSJohannes Berg  * (which is done from subsys notifiers) runs with the assumption that
3904edf547bSJohannes Berg  * dev_remove_pack has been called so no new packets will arrive during
3914edf547bSJohannes Berg  * and after the cleanup functions have been called.  dev_remove_pack
3924edf547bSJohannes Berg  * is not per namespace so instead the guarantee of no more packets
3934edf547bSJohannes Berg  * arriving in a network namespace is provided by ensuring that all
3944edf547bSJohannes Berg  * network devices and all sockets have left the network namespace
3954edf547bSJohannes Berg  * before the cleanup methods are called.
39617edde52SEric W. Biederman  *
39717edde52SEric W. Biederman  * For the longest time the ipv4 icmp code was registered as a pernet
39817edde52SEric W. Biederman  * device which caused kernel oops, and panics during network
39917edde52SEric W. Biederman  * namespace cleanup.   So please don't get this wrong.
40017edde52SEric W. Biederman  */
401e67e16eaSJoe Perches int register_pernet_subsys(struct pernet_operations *);
402e67e16eaSJoe Perches void unregister_pernet_subsys(struct pernet_operations *);
403e67e16eaSJoe Perches int register_pernet_device(struct pernet_operations *);
404e67e16eaSJoe Perches void unregister_pernet_device(struct pernet_operations *);
405f875bae0SEric W. Biederman 
40695bdfccbSEric W. Biederman struct ctl_table;
40795bdfccbSEric W. Biederman struct ctl_table_header;
408d62c612eSPavel Emelyanov 
4092ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL
410e67e16eaSJoe Perches int net_sysctl_init(void);
411e67e16eaSJoe Perches struct ctl_table_header *register_net_sysctl(struct net *net, const char *path,
412e67e16eaSJoe Perches 					     struct ctl_table *table);
413e67e16eaSJoe Perches void unregister_net_sysctl_table(struct ctl_table_header *header);
41448c74958SEric W. Biederman #else
41548c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; }
41648c74958SEric W. Biederman static inline struct ctl_table_header *register_net_sysctl(struct net *net,
41748c74958SEric W. Biederman 	const char *path, struct ctl_table *table)
41848c74958SEric W. Biederman {
41948c74958SEric W. Biederman 	return NULL;
42048c74958SEric W. Biederman }
42148c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
42248c74958SEric W. Biederman {
42348c74958SEric W. Biederman }
42448c74958SEric W. Biederman #endif
42548c74958SEric W. Biederman 
426ca4c3fc2Sfan.du static inline int rt_genid_ipv4(struct net *net)
427b42664f8SNicolas Dichtel {
428ca4c3fc2Sfan.du 	return atomic_read(&net->ipv4.rt_genid);
429b42664f8SNicolas Dichtel }
430b42664f8SNicolas Dichtel 
431ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv4(struct net *net)
432b42664f8SNicolas Dichtel {
433ca4c3fc2Sfan.du 	atomic_inc(&net->ipv4.rt_genid);
434ca4c3fc2Sfan.du }
435ca4c3fc2Sfan.du 
436705f1c86SHannes Frederic Sowa extern void (*__fib6_flush_trees)(struct net *net);
437ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv6(struct net *net)
438ca4c3fc2Sfan.du {
439705f1c86SHannes Frederic Sowa 	if (__fib6_flush_trees)
440705f1c86SHannes Frederic Sowa 		__fib6_flush_trees(net);
441ca4c3fc2Sfan.du }
442ca4c3fc2Sfan.du 
443599018a7SLuis R. Rodriguez #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
444599018a7SLuis R. Rodriguez static inline struct netns_ieee802154_lowpan *
445599018a7SLuis R. Rodriguez net_ieee802154_lowpan(struct net *net)
446599018a7SLuis R. Rodriguez {
447599018a7SLuis R. Rodriguez 	return &net->ieee802154_lowpan;
448599018a7SLuis R. Rodriguez }
449599018a7SLuis R. Rodriguez #endif
450599018a7SLuis R. Rodriguez 
451ca4c3fc2Sfan.du /* For callers who don't really care about whether it's IPv4 or IPv6 */
452ca4c3fc2Sfan.du static inline void rt_genid_bump_all(struct net *net)
453ca4c3fc2Sfan.du {
454ca4c3fc2Sfan.du 	rt_genid_bump_ipv4(net);
455ca4c3fc2Sfan.du 	rt_genid_bump_ipv6(net);
456b42664f8SNicolas Dichtel }
45795bdfccbSEric W. Biederman 
4585aad1de5STimo Teräs static inline int fnhe_genid(struct net *net)
4595aad1de5STimo Teräs {
4605aad1de5STimo Teräs 	return atomic_read(&net->fnhe_genid);
4615aad1de5STimo Teräs }
4625aad1de5STimo Teräs 
4635aad1de5STimo Teräs static inline void fnhe_genid_bump(struct net *net)
4645aad1de5STimo Teräs {
4655aad1de5STimo Teräs 	atomic_inc(&net->fnhe_genid);
4665aad1de5STimo Teräs }
4675aad1de5STimo Teräs 
4685f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */
469