xref: /openbmc/linux/include/net/net_namespace.h (revision ab84be7e)
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 {
55c122e14dSReshetova, Elena 	refcount_t		passive;	/* To decided when the network
565f256becSEric W. Biederman 						 * namespace should be freed.
575f256becSEric W. Biederman 						 */
58273c28bcSKirill Tkhai 	refcount_t		count;		/* To decided when the network
59a685e089SAl Viro 						 *  namespace should be shut down.
60a685e089SAl Viro 						 */
618e602ce2SEric Dumazet 	spinlock_t		rules_mod_lock;
628e602ce2SEric Dumazet 
63355b9855SEric Dumazet 	u32			hash_mix;
6433cf7c90SEric Dumazet 	atomic64_t		cookie_gen;
6533cf7c90SEric Dumazet 
665f256becSEric W. Biederman 	struct list_head	list;		/* list of network namespaces */
6719efbd93SKirill Tkhai 	struct list_head	exit_list;	/* To linked to call pernet exit
684420bf21SKirill Tkhai 						 * methods on dead net (
694420bf21SKirill Tkhai 						 * pernet_ops_rwsem read locked),
704420bf21SKirill Tkhai 						 * or to unregister pernet ops
714420bf21SKirill Tkhai 						 * (pernet_ops_rwsem write locked).
7219efbd93SKirill Tkhai 						 */
7365b7b5b9SKirill Tkhai 	struct llist_node	cleanup_list;	/* namespaces on death row */
7465b7b5b9SKirill Tkhai 
75038e7332SEric W. Biederman 	struct user_namespace   *user_ns;	/* Owning user namespace */
7670328660SEric W. Biederman 	struct ucounts		*ucounts;
77de133464SWANG Cong 	spinlock_t		nsid_lock;
780c7aecd4SNicolas Dichtel 	struct idr		netns_ids;
79038e7332SEric W. Biederman 
80435d5f4bSAl Viro 	struct ns_common	ns;
8198f842e6SEric W. Biederman 
82457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net;
83457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net_stat;
84881d966bSEric W. Biederman 
8573455092SAl Viro #ifdef CONFIG_SYSCTL
8673455092SAl Viro 	struct ctl_table_set	sysctls;
8773455092SAl Viro #endif
8895bdfccbSEric W. Biederman 
898e602ce2SEric Dumazet 	struct sock 		*rtnl;			/* rtnetlink socket */
908e602ce2SEric Dumazet 	struct sock		*genl_sock;
912774c7abSEric W. Biederman 
9294e5e308SChristian Brauner 	struct uevent_sock	*uevent_sock;		/* uevent socket */
9394e5e308SChristian Brauner 
94881d966bSEric W. Biederman 	struct list_head 	dev_base_head;
95881d966bSEric W. Biederman 	struct hlist_head 	*dev_name_head;
96881d966bSEric W. Biederman 	struct hlist_head	*dev_index_head;
974e985adaSThomas Graf 	unsigned int		dev_base_seq;	/* protected by rtnl_mutex */
98aa79e66eSPavel Emelyanov 	int			ifindex;
9950624c93SEric W. Biederman 	unsigned int		dev_unreg_count;
10097c53cacSDenis V. Lunev 
1015fd30ee7SDenis V. Lunev 	/* core fib_rules */
1025fd30ee7SDenis V. Lunev 	struct list_head	rules_ops;
1035fd30ee7SDenis V. Lunev 
1044420bf21SKirill Tkhai 	struct list_head	fib_notifier_ops;  /* Populated by
1054420bf21SKirill Tkhai 						    * register_pernet_subsys()
1064420bf21SKirill Tkhai 						    */
1078e602ce2SEric Dumazet 	struct net_device       *loopback_dev;          /* The loopback */
1088efa6e93SPavel Emelyanov 	struct netns_core	core;
109852566f5SPavel Emelyanov 	struct netns_mib	mib;
1102aaef4e4SDenis V. Lunev 	struct netns_packet	packet;
111a0a53c8bSDenis V. Lunev 	struct netns_unix	unx;
112ab84be7eSDavid Ahern 	struct netns_nexthop	nexthop;
1138afd351cSPavel Emelyanov 	struct netns_ipv4	ipv4;
114dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
115b0f159dbSDaniel Lezcano 	struct netns_ipv6	ipv6;
116b0f159dbSDaniel Lezcano #endif
117633fc86fSAlexander Aring #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
118633fc86fSAlexander Aring 	struct netns_ieee802154_lowpan	ieee802154_lowpan;
119633fc86fSAlexander Aring #endif
1204db67e80SEric W. Biederman #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
1214db67e80SEric W. Biederman 	struct netns_sctp	sctp;
1224db67e80SEric W. Biederman #endif
12367019cc9SPavel Emelyanov #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
12467019cc9SPavel Emelyanov 	struct netns_dccp	dccp;
12567019cc9SPavel Emelyanov #endif
1268d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER
127f3c1a44aSGao feng 	struct netns_nf		nf;
1288d870052SAlexey Dobriyan 	struct netns_xt		xt;
129dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
130dfdb8d79SAlexey Dobriyan 	struct netns_ct		ct;
131dfdb8d79SAlexey Dobriyan #endif
13299633ab2SPablo Neira Ayuso #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
13399633ab2SPablo Neira Ayuso 	struct netns_nftables	nft;
13499633ab2SPablo Neira Ayuso #endif
135c038a767SAmerigo Wang #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
136c038a767SAmerigo Wang 	struct netns_nf_frag	nf_frag;
1379ce7bc03SEric Dumazet 	struct ctl_table_header *nf_frag_frags_hdr;
138c038a767SAmerigo Wang #endif
139cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl;
140cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl_stash;
1413499abb2SAndreas Schultz #if IS_ENABLED(CONFIG_NETFILTER_NETLINK_ACCT)
1423499abb2SAndreas Schultz 	struct list_head        nfnl_acct_list;
1433499abb2SAndreas Schultz #endif
14419576c94SPablo Neira #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
14519576c94SPablo Neira 	struct list_head	nfct_timeout_list;
14619576c94SPablo Neira #endif
1478d870052SAlexey Dobriyan #endif
1483d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE
149b333b3d2SJohannes Berg 	struct sk_buff_head	wext_nlevents;
150b333b3d2SJohannes Berg #endif
1511c87733dSEric Dumazet 	struct net_generic __rcu	*gen;
1528e602ce2SEric Dumazet 
153d58e468bSPetar Penkov 	struct bpf_prog __rcu	*flow_dissector_prog;
154d58e468bSPetar Penkov 
1558e602ce2SEric Dumazet 	/* Note : following structs are cache line aligned */
1568e602ce2SEric Dumazet #ifdef CONFIG_XFRM
1578e602ce2SEric Dumazet 	struct netns_xfrm	xfrm;
1588e602ce2SEric Dumazet #endif
1598b4d14d8SJunweiZhang #if IS_ENABLED(CONFIG_IP_VS)
16061b1ab45SHans Schillstrom 	struct netns_ipvs	*ipvs;
1618b4d14d8SJunweiZhang #endif
1620189197fSEric W. Biederman #if IS_ENABLED(CONFIG_MPLS)
1630189197fSEric W. Biederman 	struct netns_mpls	mpls;
1640189197fSEric W. Biederman #endif
1658e8cda6dSMario Kicherer #if IS_ENABLED(CONFIG_CAN)
1668e8cda6dSMario Kicherer 	struct netns_can	can;
1678e8cda6dSMario Kicherer #endif
1681d0dc069SBjörn Töpel #ifdef CONFIG_XDP_SOCKETS
1691d0dc069SBjörn Töpel 	struct netns_xdp	xdp;
1701d0dc069SBjörn Töpel #endif
17151d7cccfSAndrey Vagin 	struct sock		*diag_nlsk;
1725aad1de5STimo Teräs 	atomic_t		fnhe_genid;
1733859a271SKees Cook } __randomize_layout;
1745f256becSEric W. Biederman 
175c0f39322SDenis V. Lunev #include <linux/seq_file_net.h>
176c0f39322SDenis V. Lunev 
1774fabcd71SDaniel Lezcano /* Init's network namespace */
1785f256becSEric W. Biederman extern struct net init_net;
179a4aa834aSDenis V. Lunev 
180d727abcbSEric W. Biederman #ifdef CONFIG_NET_NS
181e67e16eaSJoe Perches struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
182e67e16eaSJoe Perches 			struct net *old_net);
183225c0a01SDenis V. Lunev 
184fbdeaed4STyler Hicks void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid);
185fbdeaed4STyler Hicks 
1867866cc57SFlorian Westphal void net_ns_barrier(void);
187d727abcbSEric W. Biederman #else /* CONFIG_NET_NS */
188d727abcbSEric W. Biederman #include <linux/sched.h>
189d727abcbSEric W. Biederman #include <linux/nsproxy.h>
190038e7332SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags,
191038e7332SEric W. Biederman 	struct user_namespace *user_ns, struct net *old_net)
1929dd776b6SEric W. Biederman {
193d727abcbSEric W. Biederman 	if (flags & CLONE_NEWNET)
194d727abcbSEric W. Biederman 		return ERR_PTR(-EINVAL);
195d727abcbSEric W. Biederman 	return old_net;
1969dd776b6SEric W. Biederman }
1977866cc57SFlorian Westphal 
198fbdeaed4STyler Hicks static inline void net_ns_get_ownership(const struct net *net,
199fbdeaed4STyler Hicks 					kuid_t *uid, kgid_t *gid)
200fbdeaed4STyler Hicks {
201fbdeaed4STyler Hicks 	*uid = GLOBAL_ROOT_UID;
202fbdeaed4STyler Hicks 	*gid = GLOBAL_ROOT_GID;
203fbdeaed4STyler Hicks }
204fbdeaed4STyler Hicks 
2057866cc57SFlorian Westphal static inline void net_ns_barrier(void) {}
206d727abcbSEric W. Biederman #endif /* CONFIG_NET_NS */
207225c0a01SDenis V. Lunev 
208225c0a01SDenis V. Lunev 
209225c0a01SDenis V. Lunev extern struct list_head net_namespace_list;
2109dd776b6SEric W. Biederman 
211e67e16eaSJoe Perches struct net *get_net_ns_by_pid(pid_t pid);
2120f5258cdSStefan Hajnoczi struct net *get_net_ns_by_fd(int fd);
21330ffee84SJohannes Berg 
214535d3ae9SRashika Kheria #ifdef CONFIG_SYSCTL
215535d3ae9SRashika Kheria void ipx_register_sysctl(void);
216535d3ae9SRashika Kheria void ipx_unregister_sysctl(void);
217535d3ae9SRashika Kheria #else
218535d3ae9SRashika Kheria #define ipx_register_sysctl()
219535d3ae9SRashika Kheria #define ipx_unregister_sysctl()
220535d3ae9SRashika Kheria #endif
221535d3ae9SRashika Kheria 
222d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS
223e67e16eaSJoe Perches void __put_net(struct net *net);
2245f256becSEric W. Biederman 
2255f256becSEric W. Biederman static inline struct net *get_net(struct net *net)
2265f256becSEric W. Biederman {
227273c28bcSKirill Tkhai 	refcount_inc(&net->count);
2285f256becSEric W. Biederman 	return net;
2295f256becSEric W. Biederman }
2305f256becSEric W. Biederman 
231077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net)
232077130c0SEric W. Biederman {
233077130c0SEric W. Biederman 	/* Used when we know struct net exists but we
234077130c0SEric W. Biederman 	 * aren't guaranteed a previous reference count
235077130c0SEric W. Biederman 	 * exists.  If the reference count is zero this
236077130c0SEric W. Biederman 	 * function fails and returns NULL.
237077130c0SEric W. Biederman 	 */
238273c28bcSKirill Tkhai 	if (!refcount_inc_not_zero(&net->count))
239077130c0SEric W. Biederman 		net = NULL;
240077130c0SEric W. Biederman 	return net;
241077130c0SEric W. Biederman }
242077130c0SEric W. Biederman 
2435f256becSEric W. Biederman static inline void put_net(struct net *net)
2445f256becSEric W. Biederman {
245273c28bcSKirill Tkhai 	if (refcount_dec_and_test(&net->count))
2465f256becSEric W. Biederman 		__put_net(net);
2475f256becSEric W. Biederman }
2485f256becSEric W. Biederman 
249878628fbSYOSHIFUJI Hideaki static inline
250878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
251878628fbSYOSHIFUJI Hideaki {
252878628fbSYOSHIFUJI Hideaki 	return net1 == net2;
253878628fbSYOSHIFUJI Hideaki }
254a685e089SAl Viro 
2554ee806d5SDan Streetman static inline int check_net(const struct net *net)
2564ee806d5SDan Streetman {
2573e3ab9ccSDavid S. Miller 	return refcount_read(&net->count) != 0;
2584ee806d5SDan Streetman }
2594ee806d5SDan Streetman 
260e67e16eaSJoe Perches void net_drop_ns(void *);
261a685e089SAl Viro 
262d4655795SPavel Emelyanov #else
263b9f75f45SEric W. Biederman 
264d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net)
265d4655795SPavel Emelyanov {
266d4655795SPavel Emelyanov 	return net;
267d4655795SPavel Emelyanov }
268d4655795SPavel Emelyanov 
269d4655795SPavel Emelyanov static inline void put_net(struct net *net)
270d4655795SPavel Emelyanov {
271d4655795SPavel Emelyanov }
272d4655795SPavel Emelyanov 
273d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net)
274d4655795SPavel Emelyanov {
275d4655795SPavel Emelyanov 	return net;
276d4655795SPavel Emelyanov }
277878628fbSYOSHIFUJI Hideaki 
278878628fbSYOSHIFUJI Hideaki static inline
279878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
280878628fbSYOSHIFUJI Hideaki {
281878628fbSYOSHIFUJI Hideaki 	return 1;
282878628fbSYOSHIFUJI Hideaki }
283a685e089SAl Viro 
2844ee806d5SDan Streetman static inline int check_net(const struct net *net)
2854ee806d5SDan Streetman {
2864ee806d5SDan Streetman 	return 1;
2874ee806d5SDan Streetman }
2884ee806d5SDan Streetman 
289a685e089SAl Viro #define net_drop_ns NULL
290d4655795SPavel Emelyanov #endif
2915f256becSEric W. Biederman 
2925d1e4468SDenis V. Lunev 
2930c5c9fb5SEric W. Biederman typedef struct {
2948f424b5fSEric Dumazet #ifdef CONFIG_NET_NS
2950c5c9fb5SEric W. Biederman 	struct net *net;
2968f424b5fSEric Dumazet #endif
2970c5c9fb5SEric W. Biederman } possible_net_t;
2980c5c9fb5SEric W. Biederman 
2990c5c9fb5SEric W. Biederman static inline void write_pnet(possible_net_t *pnet, struct net *net)
3000c5c9fb5SEric W. Biederman {
3010c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS
3020c5c9fb5SEric W. Biederman 	pnet->net = net;
3030c5c9fb5SEric W. Biederman #endif
3040c5c9fb5SEric W. Biederman }
3050c5c9fb5SEric W. Biederman 
3060c5c9fb5SEric W. Biederman static inline struct net *read_pnet(const possible_net_t *pnet)
3070c5c9fb5SEric W. Biederman {
3080c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS
3090c5c9fb5SEric W. Biederman 	return pnet->net;
3100c5c9fb5SEric W. Biederman #else
3110c5c9fb5SEric W. Biederman 	return &init_net;
3120c5c9fb5SEric W. Biederman #endif
3130c5c9fb5SEric W. Biederman }
3145d1e4468SDenis V. Lunev 
315f0b07bb1SKirill Tkhai /* Protected by net_rwsem */
3165f256becSEric W. Biederman #define for_each_net(VAR)				\
3175f256becSEric W. Biederman 	list_for_each_entry(VAR, &net_namespace_list, list)
3185f256becSEric W. Biederman 
31911a28d37SJohannes Berg #define for_each_net_rcu(VAR)				\
32011a28d37SJohannes Berg 	list_for_each_entry_rcu(VAR, &net_namespace_list, list)
32111a28d37SJohannes Berg 
3224665079cSPavel Emelyanov #ifdef CONFIG_NET_NS
3234665079cSPavel Emelyanov #define __net_init
3244665079cSPavel Emelyanov #define __net_exit
325022cbae6SDenis V. Lunev #define __net_initdata
32604a6f82cSAndi Kleen #define __net_initconst
3274665079cSPavel Emelyanov #else
3284665079cSPavel Emelyanov #define __net_init	__init
329bd721ea7SFabian Frederick #define __net_exit	__ref
330022cbae6SDenis V. Lunev #define __net_initdata	__initdata
33104a6f82cSAndi Kleen #define __net_initconst	__initconst
3324665079cSPavel Emelyanov #endif
3335f256becSEric W. Biederman 
3347a0877d4SNicolas Dichtel int peernet2id_alloc(struct net *net, struct net *peer);
33559324cf3SNicolas Dichtel int peernet2id(struct net *net, struct net *peer);
33659324cf3SNicolas Dichtel bool peernet_has_id(struct net *net, struct net *peer);
3370c7aecd4SNicolas Dichtel struct net *get_net_ns_by_id(struct net *net, int id);
3380c7aecd4SNicolas Dichtel 
3395f256becSEric W. Biederman struct pernet_operations {
3405f256becSEric W. Biederman 	struct list_head list;
3416056415dSKirill Tkhai 	/*
3426056415dSKirill Tkhai 	 * Below methods are called without any exclusive locks.
3436056415dSKirill Tkhai 	 * More than one net may be constructed and destructed
3446056415dSKirill Tkhai 	 * in parallel on several cpus. Every pernet_operations
3456056415dSKirill Tkhai 	 * have to keep in mind all other pernet_operations and
3466056415dSKirill Tkhai 	 * to introduce a locking, if they share common resources.
3476056415dSKirill Tkhai 	 *
3488518e9bbSKirill Tkhai 	 * The only time they are called with exclusive lock is
3498518e9bbSKirill Tkhai 	 * from register_pernet_subsys(), unregister_pernet_subsys()
3508518e9bbSKirill Tkhai 	 * register_pernet_device() and unregister_pernet_device().
3518518e9bbSKirill Tkhai 	 *
3526056415dSKirill Tkhai 	 * Exit methods using blocking RCU primitives, such as
3536056415dSKirill Tkhai 	 * synchronize_rcu(), should be implemented via exit_batch.
3546056415dSKirill Tkhai 	 * Then, destruction of a group of net requires single
3556056415dSKirill Tkhai 	 * synchronize_rcu() related to these pernet_operations,
3566056415dSKirill Tkhai 	 * instead of separate synchronize_rcu() for every net.
3576056415dSKirill Tkhai 	 * Please, avoid synchronize_rcu() at all, where it's possible.
3586056415dSKirill Tkhai 	 */
3595f256becSEric W. Biederman 	int (*init)(struct net *net);
3605f256becSEric W. Biederman 	void (*exit)(struct net *net);
36172ad937aSEric W. Biederman 	void (*exit_batch)(struct list_head *net_exit_list);
362c7d03a00SAlexey Dobriyan 	unsigned int *id;
363f875bae0SEric W. Biederman 	size_t size;
3645f256becSEric W. Biederman };
3655f256becSEric W. Biederman 
36617edde52SEric W. Biederman /*
36717edde52SEric W. Biederman  * Use these carefully.  If you implement a network device and it
36817edde52SEric W. Biederman  * needs per network namespace operations use device pernet operations,
36917edde52SEric W. Biederman  * otherwise use pernet subsys operations.
37017edde52SEric W. Biederman  *
3714edf547bSJohannes Berg  * Network interfaces need to be removed from a dying netns _before_
3724edf547bSJohannes Berg  * subsys notifiers can be called, as most of the network code cleanup
3734edf547bSJohannes Berg  * (which is done from subsys notifiers) runs with the assumption that
3744edf547bSJohannes Berg  * dev_remove_pack has been called so no new packets will arrive during
3754edf547bSJohannes Berg  * and after the cleanup functions have been called.  dev_remove_pack
3764edf547bSJohannes Berg  * is not per namespace so instead the guarantee of no more packets
3774edf547bSJohannes Berg  * arriving in a network namespace is provided by ensuring that all
3784edf547bSJohannes Berg  * network devices and all sockets have left the network namespace
3794edf547bSJohannes Berg  * before the cleanup methods are called.
38017edde52SEric W. Biederman  *
38117edde52SEric W. Biederman  * For the longest time the ipv4 icmp code was registered as a pernet
38217edde52SEric W. Biederman  * device which caused kernel oops, and panics during network
38317edde52SEric W. Biederman  * namespace cleanup.   So please don't get this wrong.
38417edde52SEric W. Biederman  */
385e67e16eaSJoe Perches int register_pernet_subsys(struct pernet_operations *);
386e67e16eaSJoe Perches void unregister_pernet_subsys(struct pernet_operations *);
387e67e16eaSJoe Perches int register_pernet_device(struct pernet_operations *);
388e67e16eaSJoe Perches void unregister_pernet_device(struct pernet_operations *);
389f875bae0SEric W. Biederman 
39095bdfccbSEric W. Biederman struct ctl_table;
39195bdfccbSEric W. Biederman struct ctl_table_header;
392d62c612eSPavel Emelyanov 
3932ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL
394e67e16eaSJoe Perches int net_sysctl_init(void);
395e67e16eaSJoe Perches struct ctl_table_header *register_net_sysctl(struct net *net, const char *path,
396e67e16eaSJoe Perches 					     struct ctl_table *table);
397e67e16eaSJoe Perches void unregister_net_sysctl_table(struct ctl_table_header *header);
39848c74958SEric W. Biederman #else
39948c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; }
40048c74958SEric W. Biederman static inline struct ctl_table_header *register_net_sysctl(struct net *net,
40148c74958SEric W. Biederman 	const char *path, struct ctl_table *table)
40248c74958SEric W. Biederman {
40348c74958SEric W. Biederman 	return NULL;
40448c74958SEric W. Biederman }
40548c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
40648c74958SEric W. Biederman {
40748c74958SEric W. Biederman }
40848c74958SEric W. Biederman #endif
40948c74958SEric W. Biederman 
410ca4c3fc2Sfan.du static inline int rt_genid_ipv4(struct net *net)
411b42664f8SNicolas Dichtel {
412ca4c3fc2Sfan.du 	return atomic_read(&net->ipv4.rt_genid);
413b42664f8SNicolas Dichtel }
414b42664f8SNicolas Dichtel 
415ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv4(struct net *net)
416b42664f8SNicolas Dichtel {
417ca4c3fc2Sfan.du 	atomic_inc(&net->ipv4.rt_genid);
418ca4c3fc2Sfan.du }
419ca4c3fc2Sfan.du 
420705f1c86SHannes Frederic Sowa extern void (*__fib6_flush_trees)(struct net *net);
421ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv6(struct net *net)
422ca4c3fc2Sfan.du {
423705f1c86SHannes Frederic Sowa 	if (__fib6_flush_trees)
424705f1c86SHannes Frederic Sowa 		__fib6_flush_trees(net);
425ca4c3fc2Sfan.du }
426ca4c3fc2Sfan.du 
427599018a7SLuis R. Rodriguez #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
428599018a7SLuis R. Rodriguez static inline struct netns_ieee802154_lowpan *
429599018a7SLuis R. Rodriguez net_ieee802154_lowpan(struct net *net)
430599018a7SLuis R. Rodriguez {
431599018a7SLuis R. Rodriguez 	return &net->ieee802154_lowpan;
432599018a7SLuis R. Rodriguez }
433599018a7SLuis R. Rodriguez #endif
434599018a7SLuis R. Rodriguez 
435ca4c3fc2Sfan.du /* For callers who don't really care about whether it's IPv4 or IPv6 */
436ca4c3fc2Sfan.du static inline void rt_genid_bump_all(struct net *net)
437ca4c3fc2Sfan.du {
438ca4c3fc2Sfan.du 	rt_genid_bump_ipv4(net);
439ca4c3fc2Sfan.du 	rt_genid_bump_ipv6(net);
440b42664f8SNicolas Dichtel }
44195bdfccbSEric W. Biederman 
4425aad1de5STimo Teräs static inline int fnhe_genid(struct net *net)
4435aad1de5STimo Teräs {
4445aad1de5STimo Teräs 	return atomic_read(&net->fnhe_genid);
4455aad1de5STimo Teräs }
4465aad1de5STimo Teräs 
4475aad1de5STimo Teräs static inline void fnhe_genid_bump(struct net *net)
4485aad1de5STimo Teräs {
4495aad1de5STimo Teräs 	atomic_inc(&net->fnhe_genid);
4505aad1de5STimo Teräs }
4515aad1de5STimo Teräs 
4525f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */
453