xref: /openbmc/linux/include/net/net_namespace.h (revision 1d0dc069)
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>
22633fc86fSAlexander Aring #include <net/netns/ieee802154_6lowpan.h>
234db67e80SEric W. Biederman #include <net/netns/sctp.h>
2467019cc9SPavel Emelyanov #include <net/netns/dccp.h>
25f3c1a44aSGao feng #include <net/netns/netfilter.h>
268d870052SAlexey Dobriyan #include <net/netns/x_tables.h>
27dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
28dfdb8d79SAlexey Dobriyan #include <net/netns/conntrack.h>
29dfdb8d79SAlexey Dobriyan #endif
3099633ab2SPablo Neira Ayuso #include <net/netns/nftables.h>
31d62ddc21SAlexey Dobriyan #include <net/netns/xfrm.h>
320189197fSEric W. Biederman #include <net/netns/mpls.h>
338e8cda6dSMario Kicherer #include <net/netns/can.h>
341d0dc069SBjörn Töpel #include <net/netns/xdp.h>
35435d5f4bSAl Viro #include <linux/ns_common.h>
3604c52decSPablo Neira Ayuso #include <linux/idr.h>
3704c52decSPablo Neira Ayuso #include <linux/skbuff.h>
38a0a53c8bSDenis V. Lunev 
39038e7332SEric W. Biederman struct user_namespace;
40457c4cbcSEric W. Biederman struct proc_dir_entry;
412774c7abSEric W. Biederman struct net_device;
4297c53cacSDenis V. Lunev struct sock;
431597fbc0SPavel Emelyanov struct ctl_table_header;
44dec827d1SPavel Emelyanov struct net_generic;
4594e5e308SChristian Brauner struct uevent_sock;
462553d064SJulian Anastasov struct netns_ipvs;
47d58e468bSPetar Penkov struct bpf_prog;
481597fbc0SPavel Emelyanov 
497c28bd0bSEric Dumazet 
507c28bd0bSEric Dumazet #define NETDEV_HASHBITS    8
517c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
527c28bd0bSEric Dumazet 
535f256becSEric W. Biederman struct net {
54c122e14dSReshetova, Elena 	refcount_t		passive;	/* To decided when the network
555f256becSEric W. Biederman 						 * namespace should be freed.
565f256becSEric W. Biederman 						 */
57273c28bcSKirill Tkhai 	refcount_t		count;		/* To decided when the network
58a685e089SAl Viro 						 *  namespace should be shut down.
59a685e089SAl Viro 						 */
608e602ce2SEric Dumazet 	spinlock_t		rules_mod_lock;
618e602ce2SEric Dumazet 
6233cf7c90SEric Dumazet 	atomic64_t		cookie_gen;
6333cf7c90SEric Dumazet 
645f256becSEric W. Biederman 	struct list_head	list;		/* list of network namespaces */
6519efbd93SKirill Tkhai 	struct list_head	exit_list;	/* To linked to call pernet exit
664420bf21SKirill Tkhai 						 * methods on dead net (
674420bf21SKirill Tkhai 						 * pernet_ops_rwsem read locked),
684420bf21SKirill Tkhai 						 * or to unregister pernet ops
694420bf21SKirill Tkhai 						 * (pernet_ops_rwsem write locked).
7019efbd93SKirill Tkhai 						 */
7165b7b5b9SKirill Tkhai 	struct llist_node	cleanup_list;	/* namespaces on death row */
7265b7b5b9SKirill Tkhai 
73038e7332SEric W. Biederman 	struct user_namespace   *user_ns;	/* Owning user namespace */
7470328660SEric W. Biederman 	struct ucounts		*ucounts;
75de133464SWANG Cong 	spinlock_t		nsid_lock;
760c7aecd4SNicolas Dichtel 	struct idr		netns_ids;
77038e7332SEric W. Biederman 
78435d5f4bSAl Viro 	struct ns_common	ns;
7998f842e6SEric W. Biederman 
80457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net;
81457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net_stat;
82881d966bSEric W. Biederman 
8373455092SAl Viro #ifdef CONFIG_SYSCTL
8473455092SAl Viro 	struct ctl_table_set	sysctls;
8573455092SAl Viro #endif
8695bdfccbSEric W. Biederman 
878e602ce2SEric Dumazet 	struct sock 		*rtnl;			/* rtnetlink socket */
888e602ce2SEric Dumazet 	struct sock		*genl_sock;
892774c7abSEric W. Biederman 
9094e5e308SChristian Brauner 	struct uevent_sock	*uevent_sock;		/* uevent socket */
9194e5e308SChristian Brauner 
92881d966bSEric W. Biederman 	struct list_head 	dev_base_head;
93881d966bSEric W. Biederman 	struct hlist_head 	*dev_name_head;
94881d966bSEric W. Biederman 	struct hlist_head	*dev_index_head;
954e985adaSThomas Graf 	unsigned int		dev_base_seq;	/* protected by rtnl_mutex */
96aa79e66eSPavel Emelyanov 	int			ifindex;
9750624c93SEric W. Biederman 	unsigned int		dev_unreg_count;
9897c53cacSDenis V. Lunev 
995fd30ee7SDenis V. Lunev 	/* core fib_rules */
1005fd30ee7SDenis V. Lunev 	struct list_head	rules_ops;
1015fd30ee7SDenis V. Lunev 
1024420bf21SKirill Tkhai 	struct list_head	fib_notifier_ops;  /* Populated by
1034420bf21SKirill Tkhai 						    * register_pernet_subsys()
1044420bf21SKirill Tkhai 						    */
1058e602ce2SEric Dumazet 	struct net_device       *loopback_dev;          /* The loopback */
1068efa6e93SPavel Emelyanov 	struct netns_core	core;
107852566f5SPavel Emelyanov 	struct netns_mib	mib;
1082aaef4e4SDenis V. Lunev 	struct netns_packet	packet;
109a0a53c8bSDenis V. Lunev 	struct netns_unix	unx;
1108afd351cSPavel Emelyanov 	struct netns_ipv4	ipv4;
111dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
112b0f159dbSDaniel Lezcano 	struct netns_ipv6	ipv6;
113b0f159dbSDaniel Lezcano #endif
114633fc86fSAlexander Aring #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
115633fc86fSAlexander Aring 	struct netns_ieee802154_lowpan	ieee802154_lowpan;
116633fc86fSAlexander Aring #endif
1174db67e80SEric W. Biederman #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
1184db67e80SEric W. Biederman 	struct netns_sctp	sctp;
1194db67e80SEric W. Biederman #endif
12067019cc9SPavel Emelyanov #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
12167019cc9SPavel Emelyanov 	struct netns_dccp	dccp;
12267019cc9SPavel Emelyanov #endif
1238d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER
124f3c1a44aSGao feng 	struct netns_nf		nf;
1258d870052SAlexey Dobriyan 	struct netns_xt		xt;
126dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
127dfdb8d79SAlexey Dobriyan 	struct netns_ct		ct;
128dfdb8d79SAlexey Dobriyan #endif
12999633ab2SPablo Neira Ayuso #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
13099633ab2SPablo Neira Ayuso 	struct netns_nftables	nft;
13199633ab2SPablo Neira Ayuso #endif
132c038a767SAmerigo Wang #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
133c038a767SAmerigo Wang 	struct netns_nf_frag	nf_frag;
1349ce7bc03SEric Dumazet 	struct ctl_table_header *nf_frag_frags_hdr;
135c038a767SAmerigo Wang #endif
136cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl;
137cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl_stash;
1383499abb2SAndreas Schultz #if IS_ENABLED(CONFIG_NETFILTER_NETLINK_ACCT)
1393499abb2SAndreas Schultz 	struct list_head        nfnl_acct_list;
1403499abb2SAndreas Schultz #endif
14119576c94SPablo Neira #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
14219576c94SPablo Neira 	struct list_head	nfct_timeout_list;
14319576c94SPablo Neira #endif
1448d870052SAlexey Dobriyan #endif
1453d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE
146b333b3d2SJohannes Berg 	struct sk_buff_head	wext_nlevents;
147b333b3d2SJohannes Berg #endif
1481c87733dSEric Dumazet 	struct net_generic __rcu	*gen;
1498e602ce2SEric Dumazet 
150d58e468bSPetar Penkov 	struct bpf_prog __rcu	*flow_dissector_prog;
151d58e468bSPetar Penkov 
1528e602ce2SEric Dumazet 	/* Note : following structs are cache line aligned */
1538e602ce2SEric Dumazet #ifdef CONFIG_XFRM
1548e602ce2SEric Dumazet 	struct netns_xfrm	xfrm;
1558e602ce2SEric Dumazet #endif
1568b4d14d8SJunweiZhang #if IS_ENABLED(CONFIG_IP_VS)
15761b1ab45SHans Schillstrom 	struct netns_ipvs	*ipvs;
1588b4d14d8SJunweiZhang #endif
1590189197fSEric W. Biederman #if IS_ENABLED(CONFIG_MPLS)
1600189197fSEric W. Biederman 	struct netns_mpls	mpls;
1610189197fSEric W. Biederman #endif
1628e8cda6dSMario Kicherer #if IS_ENABLED(CONFIG_CAN)
1638e8cda6dSMario Kicherer 	struct netns_can	can;
1648e8cda6dSMario Kicherer #endif
1651d0dc069SBjörn Töpel #ifdef CONFIG_XDP_SOCKETS
1661d0dc069SBjörn Töpel 	struct netns_xdp	xdp;
1671d0dc069SBjörn Töpel #endif
16851d7cccfSAndrey Vagin 	struct sock		*diag_nlsk;
1695aad1de5STimo Teräs 	atomic_t		fnhe_genid;
1703859a271SKees Cook } __randomize_layout;
1715f256becSEric W. Biederman 
172c0f39322SDenis V. Lunev #include <linux/seq_file_net.h>
173c0f39322SDenis V. Lunev 
1744fabcd71SDaniel Lezcano /* Init's network namespace */
1755f256becSEric W. Biederman extern struct net init_net;
176a4aa834aSDenis V. Lunev 
177d727abcbSEric W. Biederman #ifdef CONFIG_NET_NS
178e67e16eaSJoe Perches struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
179e67e16eaSJoe Perches 			struct net *old_net);
180225c0a01SDenis V. Lunev 
181fbdeaed4STyler Hicks void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid);
182fbdeaed4STyler Hicks 
1837866cc57SFlorian Westphal void net_ns_barrier(void);
184d727abcbSEric W. Biederman #else /* CONFIG_NET_NS */
185d727abcbSEric W. Biederman #include <linux/sched.h>
186d727abcbSEric W. Biederman #include <linux/nsproxy.h>
187038e7332SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags,
188038e7332SEric W. Biederman 	struct user_namespace *user_ns, struct net *old_net)
1899dd776b6SEric W. Biederman {
190d727abcbSEric W. Biederman 	if (flags & CLONE_NEWNET)
191d727abcbSEric W. Biederman 		return ERR_PTR(-EINVAL);
192d727abcbSEric W. Biederman 	return old_net;
1939dd776b6SEric W. Biederman }
1947866cc57SFlorian Westphal 
195fbdeaed4STyler Hicks static inline void net_ns_get_ownership(const struct net *net,
196fbdeaed4STyler Hicks 					kuid_t *uid, kgid_t *gid)
197fbdeaed4STyler Hicks {
198fbdeaed4STyler Hicks 	*uid = GLOBAL_ROOT_UID;
199fbdeaed4STyler Hicks 	*gid = GLOBAL_ROOT_GID;
200fbdeaed4STyler Hicks }
201fbdeaed4STyler Hicks 
2027866cc57SFlorian Westphal static inline void net_ns_barrier(void) {}
203d727abcbSEric W. Biederman #endif /* CONFIG_NET_NS */
204225c0a01SDenis V. Lunev 
205225c0a01SDenis V. Lunev 
206225c0a01SDenis V. Lunev extern struct list_head net_namespace_list;
2079dd776b6SEric W. Biederman 
208e67e16eaSJoe Perches struct net *get_net_ns_by_pid(pid_t pid);
2090f5258cdSStefan Hajnoczi struct net *get_net_ns_by_fd(int fd);
21030ffee84SJohannes Berg 
211535d3ae9SRashika Kheria #ifdef CONFIG_SYSCTL
212535d3ae9SRashika Kheria void ipx_register_sysctl(void);
213535d3ae9SRashika Kheria void ipx_unregister_sysctl(void);
214535d3ae9SRashika Kheria #else
215535d3ae9SRashika Kheria #define ipx_register_sysctl()
216535d3ae9SRashika Kheria #define ipx_unregister_sysctl()
217535d3ae9SRashika Kheria #endif
218535d3ae9SRashika Kheria 
219d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS
220e67e16eaSJoe Perches void __put_net(struct net *net);
2215f256becSEric W. Biederman 
2225f256becSEric W. Biederman static inline struct net *get_net(struct net *net)
2235f256becSEric W. Biederman {
224273c28bcSKirill Tkhai 	refcount_inc(&net->count);
2255f256becSEric W. Biederman 	return net;
2265f256becSEric W. Biederman }
2275f256becSEric W. Biederman 
228077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net)
229077130c0SEric W. Biederman {
230077130c0SEric W. Biederman 	/* Used when we know struct net exists but we
231077130c0SEric W. Biederman 	 * aren't guaranteed a previous reference count
232077130c0SEric W. Biederman 	 * exists.  If the reference count is zero this
233077130c0SEric W. Biederman 	 * function fails and returns NULL.
234077130c0SEric W. Biederman 	 */
235273c28bcSKirill Tkhai 	if (!refcount_inc_not_zero(&net->count))
236077130c0SEric W. Biederman 		net = NULL;
237077130c0SEric W. Biederman 	return net;
238077130c0SEric W. Biederman }
239077130c0SEric W. Biederman 
2405f256becSEric W. Biederman static inline void put_net(struct net *net)
2415f256becSEric W. Biederman {
242273c28bcSKirill Tkhai 	if (refcount_dec_and_test(&net->count))
2435f256becSEric W. Biederman 		__put_net(net);
2445f256becSEric W. Biederman }
2455f256becSEric W. Biederman 
246878628fbSYOSHIFUJI Hideaki static inline
247878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
248878628fbSYOSHIFUJI Hideaki {
249878628fbSYOSHIFUJI Hideaki 	return net1 == net2;
250878628fbSYOSHIFUJI Hideaki }
251a685e089SAl Viro 
2524ee806d5SDan Streetman static inline int check_net(const struct net *net)
2534ee806d5SDan Streetman {
2543e3ab9ccSDavid S. Miller 	return refcount_read(&net->count) != 0;
2554ee806d5SDan Streetman }
2564ee806d5SDan Streetman 
257e67e16eaSJoe Perches void net_drop_ns(void *);
258a685e089SAl Viro 
259d4655795SPavel Emelyanov #else
260b9f75f45SEric W. Biederman 
261d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net)
262d4655795SPavel Emelyanov {
263d4655795SPavel Emelyanov 	return net;
264d4655795SPavel Emelyanov }
265d4655795SPavel Emelyanov 
266d4655795SPavel Emelyanov static inline void put_net(struct net *net)
267d4655795SPavel Emelyanov {
268d4655795SPavel Emelyanov }
269d4655795SPavel Emelyanov 
270d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net)
271d4655795SPavel Emelyanov {
272d4655795SPavel Emelyanov 	return net;
273d4655795SPavel Emelyanov }
274878628fbSYOSHIFUJI Hideaki 
275878628fbSYOSHIFUJI Hideaki static inline
276878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
277878628fbSYOSHIFUJI Hideaki {
278878628fbSYOSHIFUJI Hideaki 	return 1;
279878628fbSYOSHIFUJI Hideaki }
280a685e089SAl Viro 
2814ee806d5SDan Streetman static inline int check_net(const struct net *net)
2824ee806d5SDan Streetman {
2834ee806d5SDan Streetman 	return 1;
2844ee806d5SDan Streetman }
2854ee806d5SDan Streetman 
286a685e089SAl Viro #define net_drop_ns NULL
287d4655795SPavel Emelyanov #endif
2885f256becSEric W. Biederman 
2895d1e4468SDenis V. Lunev 
2900c5c9fb5SEric W. Biederman typedef struct {
2918f424b5fSEric Dumazet #ifdef CONFIG_NET_NS
2920c5c9fb5SEric W. Biederman 	struct net *net;
2938f424b5fSEric Dumazet #endif
2940c5c9fb5SEric W. Biederman } possible_net_t;
2950c5c9fb5SEric W. Biederman 
2960c5c9fb5SEric W. Biederman static inline void write_pnet(possible_net_t *pnet, struct net *net)
2970c5c9fb5SEric W. Biederman {
2980c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS
2990c5c9fb5SEric W. Biederman 	pnet->net = net;
3000c5c9fb5SEric W. Biederman #endif
3010c5c9fb5SEric W. Biederman }
3020c5c9fb5SEric W. Biederman 
3030c5c9fb5SEric W. Biederman static inline struct net *read_pnet(const possible_net_t *pnet)
3040c5c9fb5SEric W. Biederman {
3050c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS
3060c5c9fb5SEric W. Biederman 	return pnet->net;
3070c5c9fb5SEric W. Biederman #else
3080c5c9fb5SEric W. Biederman 	return &init_net;
3090c5c9fb5SEric W. Biederman #endif
3100c5c9fb5SEric W. Biederman }
3115d1e4468SDenis V. Lunev 
312f0b07bb1SKirill Tkhai /* Protected by net_rwsem */
3135f256becSEric W. Biederman #define for_each_net(VAR)				\
3145f256becSEric W. Biederman 	list_for_each_entry(VAR, &net_namespace_list, list)
3155f256becSEric W. Biederman 
31611a28d37SJohannes Berg #define for_each_net_rcu(VAR)				\
31711a28d37SJohannes Berg 	list_for_each_entry_rcu(VAR, &net_namespace_list, list)
31811a28d37SJohannes Berg 
3194665079cSPavel Emelyanov #ifdef CONFIG_NET_NS
3204665079cSPavel Emelyanov #define __net_init
3214665079cSPavel Emelyanov #define __net_exit
322022cbae6SDenis V. Lunev #define __net_initdata
32304a6f82cSAndi Kleen #define __net_initconst
3244665079cSPavel Emelyanov #else
3254665079cSPavel Emelyanov #define __net_init	__init
326bd721ea7SFabian Frederick #define __net_exit	__ref
327022cbae6SDenis V. Lunev #define __net_initdata	__initdata
32804a6f82cSAndi Kleen #define __net_initconst	__initconst
3294665079cSPavel Emelyanov #endif
3305f256becSEric W. Biederman 
3317a0877d4SNicolas Dichtel int peernet2id_alloc(struct net *net, struct net *peer);
33259324cf3SNicolas Dichtel int peernet2id(struct net *net, struct net *peer);
33359324cf3SNicolas Dichtel bool peernet_has_id(struct net *net, struct net *peer);
3340c7aecd4SNicolas Dichtel struct net *get_net_ns_by_id(struct net *net, int id);
3350c7aecd4SNicolas Dichtel 
3365f256becSEric W. Biederman struct pernet_operations {
3375f256becSEric W. Biederman 	struct list_head list;
3386056415dSKirill Tkhai 	/*
3396056415dSKirill Tkhai 	 * Below methods are called without any exclusive locks.
3406056415dSKirill Tkhai 	 * More than one net may be constructed and destructed
3416056415dSKirill Tkhai 	 * in parallel on several cpus. Every pernet_operations
3426056415dSKirill Tkhai 	 * have to keep in mind all other pernet_operations and
3436056415dSKirill Tkhai 	 * to introduce a locking, if they share common resources.
3446056415dSKirill Tkhai 	 *
3458518e9bbSKirill Tkhai 	 * The only time they are called with exclusive lock is
3468518e9bbSKirill Tkhai 	 * from register_pernet_subsys(), unregister_pernet_subsys()
3478518e9bbSKirill Tkhai 	 * register_pernet_device() and unregister_pernet_device().
3488518e9bbSKirill Tkhai 	 *
3496056415dSKirill Tkhai 	 * Exit methods using blocking RCU primitives, such as
3506056415dSKirill Tkhai 	 * synchronize_rcu(), should be implemented via exit_batch.
3516056415dSKirill Tkhai 	 * Then, destruction of a group of net requires single
3526056415dSKirill Tkhai 	 * synchronize_rcu() related to these pernet_operations,
3536056415dSKirill Tkhai 	 * instead of separate synchronize_rcu() for every net.
3546056415dSKirill Tkhai 	 * Please, avoid synchronize_rcu() at all, where it's possible.
3556056415dSKirill Tkhai 	 */
3565f256becSEric W. Biederman 	int (*init)(struct net *net);
3575f256becSEric W. Biederman 	void (*exit)(struct net *net);
35872ad937aSEric W. Biederman 	void (*exit_batch)(struct list_head *net_exit_list);
359c7d03a00SAlexey Dobriyan 	unsigned int *id;
360f875bae0SEric W. Biederman 	size_t size;
3615f256becSEric W. Biederman };
3625f256becSEric W. Biederman 
36317edde52SEric W. Biederman /*
36417edde52SEric W. Biederman  * Use these carefully.  If you implement a network device and it
36517edde52SEric W. Biederman  * needs per network namespace operations use device pernet operations,
36617edde52SEric W. Biederman  * otherwise use pernet subsys operations.
36717edde52SEric W. Biederman  *
3684edf547bSJohannes Berg  * Network interfaces need to be removed from a dying netns _before_
3694edf547bSJohannes Berg  * subsys notifiers can be called, as most of the network code cleanup
3704edf547bSJohannes Berg  * (which is done from subsys notifiers) runs with the assumption that
3714edf547bSJohannes Berg  * dev_remove_pack has been called so no new packets will arrive during
3724edf547bSJohannes Berg  * and after the cleanup functions have been called.  dev_remove_pack
3734edf547bSJohannes Berg  * is not per namespace so instead the guarantee of no more packets
3744edf547bSJohannes Berg  * arriving in a network namespace is provided by ensuring that all
3754edf547bSJohannes Berg  * network devices and all sockets have left the network namespace
3764edf547bSJohannes Berg  * before the cleanup methods are called.
37717edde52SEric W. Biederman  *
37817edde52SEric W. Biederman  * For the longest time the ipv4 icmp code was registered as a pernet
37917edde52SEric W. Biederman  * device which caused kernel oops, and panics during network
38017edde52SEric W. Biederman  * namespace cleanup.   So please don't get this wrong.
38117edde52SEric W. Biederman  */
382e67e16eaSJoe Perches int register_pernet_subsys(struct pernet_operations *);
383e67e16eaSJoe Perches void unregister_pernet_subsys(struct pernet_operations *);
384e67e16eaSJoe Perches int register_pernet_device(struct pernet_operations *);
385e67e16eaSJoe Perches void unregister_pernet_device(struct pernet_operations *);
386f875bae0SEric W. Biederman 
38795bdfccbSEric W. Biederman struct ctl_table;
38895bdfccbSEric W. Biederman struct ctl_table_header;
389d62c612eSPavel Emelyanov 
3902ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL
391e67e16eaSJoe Perches int net_sysctl_init(void);
392e67e16eaSJoe Perches struct ctl_table_header *register_net_sysctl(struct net *net, const char *path,
393e67e16eaSJoe Perches 					     struct ctl_table *table);
394e67e16eaSJoe Perches void unregister_net_sysctl_table(struct ctl_table_header *header);
39548c74958SEric W. Biederman #else
39648c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; }
39748c74958SEric W. Biederman static inline struct ctl_table_header *register_net_sysctl(struct net *net,
39848c74958SEric W. Biederman 	const char *path, struct ctl_table *table)
39948c74958SEric W. Biederman {
40048c74958SEric W. Biederman 	return NULL;
40148c74958SEric W. Biederman }
40248c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
40348c74958SEric W. Biederman {
40448c74958SEric W. Biederman }
40548c74958SEric W. Biederman #endif
40648c74958SEric W. Biederman 
407ca4c3fc2Sfan.du static inline int rt_genid_ipv4(struct net *net)
408b42664f8SNicolas Dichtel {
409ca4c3fc2Sfan.du 	return atomic_read(&net->ipv4.rt_genid);
410b42664f8SNicolas Dichtel }
411b42664f8SNicolas Dichtel 
412ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv4(struct net *net)
413b42664f8SNicolas Dichtel {
414ca4c3fc2Sfan.du 	atomic_inc(&net->ipv4.rt_genid);
415ca4c3fc2Sfan.du }
416ca4c3fc2Sfan.du 
417705f1c86SHannes Frederic Sowa extern void (*__fib6_flush_trees)(struct net *net);
418ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv6(struct net *net)
419ca4c3fc2Sfan.du {
420705f1c86SHannes Frederic Sowa 	if (__fib6_flush_trees)
421705f1c86SHannes Frederic Sowa 		__fib6_flush_trees(net);
422ca4c3fc2Sfan.du }
423ca4c3fc2Sfan.du 
424599018a7SLuis R. Rodriguez #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
425599018a7SLuis R. Rodriguez static inline struct netns_ieee802154_lowpan *
426599018a7SLuis R. Rodriguez net_ieee802154_lowpan(struct net *net)
427599018a7SLuis R. Rodriguez {
428599018a7SLuis R. Rodriguez 	return &net->ieee802154_lowpan;
429599018a7SLuis R. Rodriguez }
430599018a7SLuis R. Rodriguez #endif
431599018a7SLuis R. Rodriguez 
432ca4c3fc2Sfan.du /* For callers who don't really care about whether it's IPv4 or IPv6 */
433ca4c3fc2Sfan.du static inline void rt_genid_bump_all(struct net *net)
434ca4c3fc2Sfan.du {
435ca4c3fc2Sfan.du 	rt_genid_bump_ipv4(net);
436ca4c3fc2Sfan.du 	rt_genid_bump_ipv6(net);
437b42664f8SNicolas Dichtel }
43895bdfccbSEric W. Biederman 
4395aad1de5STimo Teräs static inline int fnhe_genid(struct net *net)
4405aad1de5STimo Teräs {
4415aad1de5STimo Teräs 	return atomic_read(&net->fnhe_genid);
4425aad1de5STimo Teräs }
4435aad1de5STimo Teräs 
4445aad1de5STimo Teräs static inline void fnhe_genid_bump(struct net *net)
4455aad1de5STimo Teräs {
4465aad1de5STimo Teräs 	atomic_inc(&net->fnhe_genid);
4475aad1de5STimo Teräs }
4485aad1de5STimo Teräs 
4495f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */
450