xref: /openbmc/linux/include/net/net_namespace.h (revision b0381776)
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>
25f3c1a44aSGao feng #include <net/netns/netfilter.h>
26dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
27dfdb8d79SAlexey Dobriyan #include <net/netns/conntrack.h>
28dfdb8d79SAlexey Dobriyan #endif
29*b0381776SVlad Buslov #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
30*b0381776SVlad Buslov #include <net/netns/flow_table.h>
31*b0381776SVlad Buslov #endif
3299633ab2SPablo Neira Ayuso #include <net/netns/nftables.h>
33d62ddc21SAlexey Dobriyan #include <net/netns/xfrm.h>
340189197fSEric W. Biederman #include <net/netns/mpls.h>
358e8cda6dSMario Kicherer #include <net/netns/can.h>
361d0dc069SBjörn Töpel #include <net/netns/xdp.h>
37194730a9SGuvenc Gulce #include <net/netns/smc.h>
38a3fd7ceeSJakub Sitnicki #include <net/netns/bpf.h>
39889b7da2SJeremy Kerr #include <net/netns/mctp.h>
409ba74e6cSEric Dumazet #include <net/net_trackers.h>
41435d5f4bSAl Viro #include <linux/ns_common.h>
4204c52decSPablo Neira Ayuso #include <linux/idr.h>
4304c52decSPablo Neira Ayuso #include <linux/skbuff.h>
44a30c7b42SJiri Pirko #include <linux/notifier.h>
45a0a53c8bSDenis V. Lunev 
46038e7332SEric W. Biederman struct user_namespace;
47457c4cbcSEric W. Biederman struct proc_dir_entry;
482774c7abSEric W. Biederman struct net_device;
4997c53cacSDenis V. Lunev struct sock;
501597fbc0SPavel Emelyanov struct ctl_table_header;
51dec827d1SPavel Emelyanov struct net_generic;
5294e5e308SChristian Brauner struct uevent_sock;
532553d064SJulian Anastasov struct netns_ipvs;
54d58e468bSPetar Penkov struct bpf_prog;
551597fbc0SPavel Emelyanov 
567c28bd0bSEric Dumazet 
577c28bd0bSEric Dumazet #define NETDEV_HASHBITS    8
587c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
597c28bd0bSEric Dumazet 
605f256becSEric W. Biederman struct net {
612a06b898SEric Dumazet 	/* First cache line can be often dirtied.
622a06b898SEric Dumazet 	 * Do not place here read-mostly fields.
632a06b898SEric Dumazet 	 */
64aad12c23SMike Rapoport 	refcount_t		passive;	/* To decide when the network
655f256becSEric W. Biederman 						 * namespace should be freed.
665f256becSEric W. Biederman 						 */
678e602ce2SEric Dumazet 	spinlock_t		rules_mod_lock;
688e602ce2SEric Dumazet 
69ede6c39cSEric Dumazet 	atomic_t		dev_unreg_count;
702a06b898SEric Dumazet 
712a06b898SEric Dumazet 	unsigned int		dev_base_seq;	/* protected by rtnl_mutex */
722a06b898SEric Dumazet 	int			ifindex;
732a06b898SEric Dumazet 
742a06b898SEric Dumazet 	spinlock_t		nsid_lock;
752a06b898SEric Dumazet 	atomic_t		fnhe_genid;
7633cf7c90SEric Dumazet 
775f256becSEric W. Biederman 	struct list_head	list;		/* list of network namespaces */
7819efbd93SKirill Tkhai 	struct list_head	exit_list;	/* To linked to call pernet exit
794420bf21SKirill Tkhai 						 * methods on dead net (
804420bf21SKirill Tkhai 						 * pernet_ops_rwsem read locked),
814420bf21SKirill Tkhai 						 * or to unregister pernet ops
824420bf21SKirill Tkhai 						 * (pernet_ops_rwsem write locked).
8319efbd93SKirill Tkhai 						 */
8465b7b5b9SKirill Tkhai 	struct llist_node	cleanup_list;	/* namespaces on death row */
8565b7b5b9SKirill Tkhai 
869b242610SDavid Howells #ifdef CONFIG_KEYS
879b242610SDavid Howells 	struct key_tag		*key_domain;	/* Key domain of operation tag */
889b242610SDavid Howells #endif
89038e7332SEric W. Biederman 	struct user_namespace   *user_ns;	/* Owning user namespace */
9070328660SEric W. Biederman 	struct ucounts		*ucounts;
910c7aecd4SNicolas Dichtel 	struct idr		netns_ids;
92038e7332SEric W. Biederman 
93435d5f4bSAl Viro 	struct ns_common	ns;
949ba74e6cSEric Dumazet 	struct ref_tracker_dir  refcnt_tracker;
9598f842e6SEric W. Biederman 
962a06b898SEric Dumazet 	struct list_head 	dev_base_head;
97457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net;
98457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net_stat;
99881d966bSEric W. Biederman 
10073455092SAl Viro #ifdef CONFIG_SYSCTL
10173455092SAl Viro 	struct ctl_table_set	sysctls;
10273455092SAl Viro #endif
10395bdfccbSEric W. Biederman 
1048e602ce2SEric Dumazet 	struct sock 		*rtnl;			/* rtnetlink socket */
1058e602ce2SEric Dumazet 	struct sock		*genl_sock;
1062774c7abSEric W. Biederman 
10794e5e308SChristian Brauner 	struct uevent_sock	*uevent_sock;		/* uevent socket */
10894e5e308SChristian Brauner 
109881d966bSEric W. Biederman 	struct hlist_head 	*dev_name_head;
110881d966bSEric W. Biederman 	struct hlist_head	*dev_index_head;
111a30c7b42SJiri Pirko 	struct raw_notifier_head	netdev_chain;
112a30c7b42SJiri Pirko 
1132a06b898SEric Dumazet 	/* Note that @hash_mix can be read millions times per second,
1142a06b898SEric Dumazet 	 * it is critical that it is on a read_mostly cache line.
1152a06b898SEric Dumazet 	 */
1162a06b898SEric Dumazet 	u32			hash_mix;
1172a06b898SEric Dumazet 
1182a06b898SEric Dumazet 	struct net_device       *loopback_dev;          /* The loopback */
11997c53cacSDenis V. Lunev 
1205fd30ee7SDenis V. Lunev 	/* core fib_rules */
1215fd30ee7SDenis V. Lunev 	struct list_head	rules_ops;
1225fd30ee7SDenis V. Lunev 
1238efa6e93SPavel Emelyanov 	struct netns_core	core;
124852566f5SPavel Emelyanov 	struct netns_mib	mib;
1252aaef4e4SDenis V. Lunev 	struct netns_packet	packet;
126b6e81138SKuniyuki Iwashima #if IS_ENABLED(CONFIG_UNIX)
127a0a53c8bSDenis V. Lunev 	struct netns_unix	unx;
128b6e81138SKuniyuki Iwashima #endif
129ab84be7eSDavid Ahern 	struct netns_nexthop	nexthop;
1308afd351cSPavel Emelyanov 	struct netns_ipv4	ipv4;
131dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
132b0f159dbSDaniel Lezcano 	struct netns_ipv6	ipv6;
133b0f159dbSDaniel Lezcano #endif
134633fc86fSAlexander Aring #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
135633fc86fSAlexander Aring 	struct netns_ieee802154_lowpan	ieee802154_lowpan;
136633fc86fSAlexander Aring #endif
1374db67e80SEric W. Biederman #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
1384db67e80SEric W. Biederman 	struct netns_sctp	sctp;
1394db67e80SEric W. Biederman #endif
1408d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER
141f3c1a44aSGao feng 	struct netns_nf		nf;
142dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
143dfdb8d79SAlexey Dobriyan 	struct netns_ct		ct;
144dfdb8d79SAlexey Dobriyan #endif
14599633ab2SPablo Neira Ayuso #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
14699633ab2SPablo Neira Ayuso 	struct netns_nftables	nft;
14799633ab2SPablo Neira Ayuso #endif
148*b0381776SVlad Buslov #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
149*b0381776SVlad Buslov 	struct netns_ft ft;
150*b0381776SVlad Buslov #endif
1518d870052SAlexey Dobriyan #endif
1523d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE
153b333b3d2SJohannes Berg 	struct sk_buff_head	wext_nlevents;
154b333b3d2SJohannes Berg #endif
1551c87733dSEric Dumazet 	struct net_generic __rcu	*gen;
1568e602ce2SEric Dumazet 
157a3fd7ceeSJakub Sitnicki 	/* Used to store attached BPF programs */
158a3fd7ceeSJakub Sitnicki 	struct netns_bpf	bpf;
159d58e468bSPetar Penkov 
1608e602ce2SEric Dumazet 	/* Note : following structs are cache line aligned */
1618e602ce2SEric Dumazet #ifdef CONFIG_XFRM
1628e602ce2SEric Dumazet 	struct netns_xfrm	xfrm;
1638e602ce2SEric Dumazet #endif
164f318903cSDaniel Borkmann 
1653d368ab8SEric Dumazet 	u64			net_cookie; /* written once */
166f318903cSDaniel Borkmann 
1678b4d14d8SJunweiZhang #if IS_ENABLED(CONFIG_IP_VS)
16861b1ab45SHans Schillstrom 	struct netns_ipvs	*ipvs;
1698b4d14d8SJunweiZhang #endif
1700189197fSEric W. Biederman #if IS_ENABLED(CONFIG_MPLS)
1710189197fSEric W. Biederman 	struct netns_mpls	mpls;
1720189197fSEric W. Biederman #endif
1738e8cda6dSMario Kicherer #if IS_ENABLED(CONFIG_CAN)
1748e8cda6dSMario Kicherer 	struct netns_can	can;
1758e8cda6dSMario Kicherer #endif
1761d0dc069SBjörn Töpel #ifdef CONFIG_XDP_SOCKETS
1771d0dc069SBjörn Töpel 	struct netns_xdp	xdp;
1781d0dc069SBjörn Töpel #endif
179889b7da2SJeremy Kerr #if IS_ENABLED(CONFIG_MCTP)
180889b7da2SJeremy Kerr 	struct netns_mctp	mctp;
181889b7da2SJeremy Kerr #endif
18291b05a7eSOndrej Mosnacek #if IS_ENABLED(CONFIG_CRYPTO_USER)
18391b05a7eSOndrej Mosnacek 	struct sock		*crypto_nlsk;
18491b05a7eSOndrej Mosnacek #endif
18551d7cccfSAndrey Vagin 	struct sock		*diag_nlsk;
186194730a9SGuvenc Gulce #if IS_ENABLED(CONFIG_SMC)
187194730a9SGuvenc Gulce 	struct netns_smc	smc;
188194730a9SGuvenc Gulce #endif
1893859a271SKees Cook } __randomize_layout;
1905f256becSEric W. Biederman 
191c0f39322SDenis V. Lunev #include <linux/seq_file_net.h>
192c0f39322SDenis V. Lunev 
1934fabcd71SDaniel Lezcano /* Init's network namespace */
1945f256becSEric W. Biederman extern struct net init_net;
195a4aa834aSDenis V. Lunev 
196d727abcbSEric W. Biederman #ifdef CONFIG_NET_NS
197e67e16eaSJoe Perches struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
198e67e16eaSJoe Perches 			struct net *old_net);
199225c0a01SDenis V. Lunev 
200fbdeaed4STyler Hicks void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid);
201fbdeaed4STyler Hicks 
2027866cc57SFlorian Westphal void net_ns_barrier(void);
203ea6932d7SChangbin Du 
204ea6932d7SChangbin Du struct ns_common *get_net_ns(struct ns_common *ns);
205e34492deSChangbin Du struct net *get_net_ns_by_fd(int fd);
206d727abcbSEric W. Biederman #else /* CONFIG_NET_NS */
207d727abcbSEric W. Biederman #include <linux/sched.h>
208d727abcbSEric W. Biederman #include <linux/nsproxy.h>
209038e7332SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags,
210038e7332SEric W. Biederman 	struct user_namespace *user_ns, struct net *old_net)
2119dd776b6SEric W. Biederman {
212d727abcbSEric W. Biederman 	if (flags & CLONE_NEWNET)
213d727abcbSEric W. Biederman 		return ERR_PTR(-EINVAL);
214d727abcbSEric W. Biederman 	return old_net;
2159dd776b6SEric W. Biederman }
2167866cc57SFlorian Westphal 
217fbdeaed4STyler Hicks static inline void net_ns_get_ownership(const struct net *net,
218fbdeaed4STyler Hicks 					kuid_t *uid, kgid_t *gid)
219fbdeaed4STyler Hicks {
220fbdeaed4STyler Hicks 	*uid = GLOBAL_ROOT_UID;
221fbdeaed4STyler Hicks 	*gid = GLOBAL_ROOT_GID;
222fbdeaed4STyler Hicks }
223fbdeaed4STyler Hicks 
2247866cc57SFlorian Westphal static inline void net_ns_barrier(void) {}
225ea6932d7SChangbin Du 
226ea6932d7SChangbin Du static inline struct ns_common *get_net_ns(struct ns_common *ns)
227ea6932d7SChangbin Du {
228ea6932d7SChangbin Du 	return ERR_PTR(-EINVAL);
229ea6932d7SChangbin Du }
230e34492deSChangbin Du 
231e34492deSChangbin Du static inline struct net *get_net_ns_by_fd(int fd)
232e34492deSChangbin Du {
233e34492deSChangbin Du 	return ERR_PTR(-EINVAL);
234e34492deSChangbin Du }
235d727abcbSEric W. Biederman #endif /* CONFIG_NET_NS */
236225c0a01SDenis V. Lunev 
237225c0a01SDenis V. Lunev 
238225c0a01SDenis V. Lunev extern struct list_head net_namespace_list;
2399dd776b6SEric W. Biederman 
240e67e16eaSJoe Perches struct net *get_net_ns_by_pid(pid_t pid);
24130ffee84SJohannes Berg 
242535d3ae9SRashika Kheria #ifdef CONFIG_SYSCTL
243535d3ae9SRashika Kheria void ipx_register_sysctl(void);
244535d3ae9SRashika Kheria void ipx_unregister_sysctl(void);
245535d3ae9SRashika Kheria #else
246535d3ae9SRashika Kheria #define ipx_register_sysctl()
247535d3ae9SRashika Kheria #define ipx_unregister_sysctl()
248535d3ae9SRashika Kheria #endif
249535d3ae9SRashika Kheria 
250d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS
251e67e16eaSJoe Perches void __put_net(struct net *net);
2525f256becSEric W. Biederman 
2539ba74e6cSEric Dumazet /* Try using get_net_track() instead */
2545f256becSEric W. Biederman static inline struct net *get_net(struct net *net)
2555f256becSEric W. Biederman {
2568b8f3e66SChristian Brauner 	refcount_inc(&net->ns.count);
2575f256becSEric W. Biederman 	return net;
2585f256becSEric W. Biederman }
2595f256becSEric W. Biederman 
260077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net)
261077130c0SEric W. Biederman {
262077130c0SEric W. Biederman 	/* Used when we know struct net exists but we
263077130c0SEric W. Biederman 	 * aren't guaranteed a previous reference count
264077130c0SEric W. Biederman 	 * exists.  If the reference count is zero this
265077130c0SEric W. Biederman 	 * function fails and returns NULL.
266077130c0SEric W. Biederman 	 */
2678b8f3e66SChristian Brauner 	if (!refcount_inc_not_zero(&net->ns.count))
268077130c0SEric W. Biederman 		net = NULL;
269077130c0SEric W. Biederman 	return net;
270077130c0SEric W. Biederman }
271077130c0SEric W. Biederman 
2729ba74e6cSEric Dumazet /* Try using put_net_track() instead */
2735f256becSEric W. Biederman static inline void put_net(struct net *net)
2745f256becSEric W. Biederman {
2758b8f3e66SChristian Brauner 	if (refcount_dec_and_test(&net->ns.count))
2765f256becSEric W. Biederman 		__put_net(net);
2775f256becSEric W. Biederman }
2785f256becSEric W. Biederman 
279878628fbSYOSHIFUJI Hideaki static inline
280878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
281878628fbSYOSHIFUJI Hideaki {
282878628fbSYOSHIFUJI Hideaki 	return net1 == net2;
283878628fbSYOSHIFUJI Hideaki }
284a685e089SAl Viro 
2854ee806d5SDan Streetman static inline int check_net(const struct net *net)
2864ee806d5SDan Streetman {
2878b8f3e66SChristian Brauner 	return refcount_read(&net->ns.count) != 0;
2884ee806d5SDan Streetman }
2894ee806d5SDan Streetman 
290e67e16eaSJoe Perches void net_drop_ns(void *);
291a685e089SAl Viro 
292d4655795SPavel Emelyanov #else
293b9f75f45SEric W. Biederman 
294d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net)
295d4655795SPavel Emelyanov {
296d4655795SPavel Emelyanov 	return net;
297d4655795SPavel Emelyanov }
298d4655795SPavel Emelyanov 
299d4655795SPavel Emelyanov static inline void put_net(struct net *net)
300d4655795SPavel Emelyanov {
301d4655795SPavel Emelyanov }
302d4655795SPavel Emelyanov 
303d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net)
304d4655795SPavel Emelyanov {
305d4655795SPavel Emelyanov 	return net;
306d4655795SPavel Emelyanov }
307878628fbSYOSHIFUJI Hideaki 
308878628fbSYOSHIFUJI Hideaki static inline
309878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
310878628fbSYOSHIFUJI Hideaki {
311878628fbSYOSHIFUJI Hideaki 	return 1;
312878628fbSYOSHIFUJI Hideaki }
313a685e089SAl Viro 
3144ee806d5SDan Streetman static inline int check_net(const struct net *net)
3154ee806d5SDan Streetman {
3164ee806d5SDan Streetman 	return 1;
3174ee806d5SDan Streetman }
3184ee806d5SDan Streetman 
319a685e089SAl Viro #define net_drop_ns NULL
320d4655795SPavel Emelyanov #endif
3215f256becSEric W. Biederman 
3225d1e4468SDenis V. Lunev 
3239ba74e6cSEric Dumazet static inline void netns_tracker_alloc(struct net *net,
3249ba74e6cSEric Dumazet 				       netns_tracker *tracker, gfp_t gfp)
3259ba74e6cSEric Dumazet {
3269ba74e6cSEric Dumazet #ifdef CONFIG_NET_NS_REFCNT_TRACKER
3279ba74e6cSEric Dumazet 	ref_tracker_alloc(&net->refcnt_tracker, tracker, gfp);
3289ba74e6cSEric Dumazet #endif
3299ba74e6cSEric Dumazet }
3309ba74e6cSEric Dumazet 
3319ba74e6cSEric Dumazet static inline void netns_tracker_free(struct net *net,
3329ba74e6cSEric Dumazet 				      netns_tracker *tracker)
3339ba74e6cSEric Dumazet {
3349ba74e6cSEric Dumazet #ifdef CONFIG_NET_NS_REFCNT_TRACKER
3359ba74e6cSEric Dumazet        ref_tracker_free(&net->refcnt_tracker, tracker);
3369ba74e6cSEric Dumazet #endif
3379ba74e6cSEric Dumazet }
3389ba74e6cSEric Dumazet 
3399ba74e6cSEric Dumazet static inline struct net *get_net_track(struct net *net,
3409ba74e6cSEric Dumazet 					netns_tracker *tracker, gfp_t gfp)
3419ba74e6cSEric Dumazet {
3429ba74e6cSEric Dumazet 	get_net(net);
3439ba74e6cSEric Dumazet 	netns_tracker_alloc(net, tracker, gfp);
3449ba74e6cSEric Dumazet 	return net;
3459ba74e6cSEric Dumazet }
3469ba74e6cSEric Dumazet 
3479ba74e6cSEric Dumazet static inline void put_net_track(struct net *net, netns_tracker *tracker)
3489ba74e6cSEric Dumazet {
3499ba74e6cSEric Dumazet 	netns_tracker_free(net, tracker);
3509ba74e6cSEric Dumazet 	put_net(net);
3519ba74e6cSEric Dumazet }
3529ba74e6cSEric Dumazet 
3530c5c9fb5SEric W. Biederman typedef struct {
3548f424b5fSEric Dumazet #ifdef CONFIG_NET_NS
3550c5c9fb5SEric W. Biederman 	struct net *net;
3568f424b5fSEric Dumazet #endif
3570c5c9fb5SEric W. Biederman } possible_net_t;
3580c5c9fb5SEric W. Biederman 
3590c5c9fb5SEric W. Biederman static inline void write_pnet(possible_net_t *pnet, struct net *net)
3600c5c9fb5SEric W. Biederman {
3610c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS
3620c5c9fb5SEric W. Biederman 	pnet->net = net;
3630c5c9fb5SEric W. Biederman #endif
3640c5c9fb5SEric W. Biederman }
3650c5c9fb5SEric W. Biederman 
3660c5c9fb5SEric W. Biederman static inline struct net *read_pnet(const possible_net_t *pnet)
3670c5c9fb5SEric W. Biederman {
3680c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS
3690c5c9fb5SEric W. Biederman 	return pnet->net;
3700c5c9fb5SEric W. Biederman #else
3710c5c9fb5SEric W. Biederman 	return &init_net;
3720c5c9fb5SEric W. Biederman #endif
3730c5c9fb5SEric W. Biederman }
3745d1e4468SDenis V. Lunev 
375f0b07bb1SKirill Tkhai /* Protected by net_rwsem */
3765f256becSEric W. Biederman #define for_each_net(VAR)				\
3775f256becSEric W. Biederman 	list_for_each_entry(VAR, &net_namespace_list, list)
378afa0df59SJiri Pirko #define for_each_net_continue_reverse(VAR)		\
379afa0df59SJiri Pirko 	list_for_each_entry_continue_reverse(VAR, &net_namespace_list, list)
38011a28d37SJohannes Berg #define for_each_net_rcu(VAR)				\
38111a28d37SJohannes Berg 	list_for_each_entry_rcu(VAR, &net_namespace_list, list)
38211a28d37SJohannes Berg 
3834665079cSPavel Emelyanov #ifdef CONFIG_NET_NS
3844665079cSPavel Emelyanov #define __net_init
3854665079cSPavel Emelyanov #define __net_exit
386022cbae6SDenis V. Lunev #define __net_initdata
38704a6f82cSAndi Kleen #define __net_initconst
3884665079cSPavel Emelyanov #else
3894665079cSPavel Emelyanov #define __net_init	__init
390bd721ea7SFabian Frederick #define __net_exit	__ref
391022cbae6SDenis V. Lunev #define __net_initdata	__initdata
39204a6f82cSAndi Kleen #define __net_initconst	__initconst
3934665079cSPavel Emelyanov #endif
3945f256becSEric W. Biederman 
395d4e4fdf9SGuillaume Nault int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp);
39656f200c7SGuillaume Nault int peernet2id(const struct net *net, struct net *peer);
39756f200c7SGuillaume Nault bool peernet_has_id(const struct net *net, struct net *peer);
39856f200c7SGuillaume Nault struct net *get_net_ns_by_id(const struct net *net, int id);
3990c7aecd4SNicolas Dichtel 
4005f256becSEric W. Biederman struct pernet_operations {
4015f256becSEric W. Biederman 	struct list_head list;
4026056415dSKirill Tkhai 	/*
4036056415dSKirill Tkhai 	 * Below methods are called without any exclusive locks.
4046056415dSKirill Tkhai 	 * More than one net may be constructed and destructed
4056056415dSKirill Tkhai 	 * in parallel on several cpus. Every pernet_operations
4066056415dSKirill Tkhai 	 * have to keep in mind all other pernet_operations and
4076056415dSKirill Tkhai 	 * to introduce a locking, if they share common resources.
4086056415dSKirill Tkhai 	 *
4098518e9bbSKirill Tkhai 	 * The only time they are called with exclusive lock is
4108518e9bbSKirill Tkhai 	 * from register_pernet_subsys(), unregister_pernet_subsys()
4118518e9bbSKirill Tkhai 	 * register_pernet_device() and unregister_pernet_device().
4128518e9bbSKirill Tkhai 	 *
4136056415dSKirill Tkhai 	 * Exit methods using blocking RCU primitives, such as
4146056415dSKirill Tkhai 	 * synchronize_rcu(), should be implemented via exit_batch.
4156056415dSKirill Tkhai 	 * Then, destruction of a group of net requires single
4166056415dSKirill Tkhai 	 * synchronize_rcu() related to these pernet_operations,
4176056415dSKirill Tkhai 	 * instead of separate synchronize_rcu() for every net.
4186056415dSKirill Tkhai 	 * Please, avoid synchronize_rcu() at all, where it's possible.
419d7d99872SEric Dumazet 	 *
420d7d99872SEric Dumazet 	 * Note that a combination of pre_exit() and exit() can
421d7d99872SEric Dumazet 	 * be used, since a synchronize_rcu() is guaranteed between
422d7d99872SEric Dumazet 	 * the calls.
4236056415dSKirill Tkhai 	 */
4245f256becSEric W. Biederman 	int (*init)(struct net *net);
425d7d99872SEric Dumazet 	void (*pre_exit)(struct net *net);
4265f256becSEric W. Biederman 	void (*exit)(struct net *net);
42772ad937aSEric W. Biederman 	void (*exit_batch)(struct list_head *net_exit_list);
428c7d03a00SAlexey Dobriyan 	unsigned int *id;
429f875bae0SEric W. Biederman 	size_t size;
4305f256becSEric W. Biederman };
4315f256becSEric W. Biederman 
43217edde52SEric W. Biederman /*
43317edde52SEric W. Biederman  * Use these carefully.  If you implement a network device and it
43417edde52SEric W. Biederman  * needs per network namespace operations use device pernet operations,
43517edde52SEric W. Biederman  * otherwise use pernet subsys operations.
43617edde52SEric W. Biederman  *
4374edf547bSJohannes Berg  * Network interfaces need to be removed from a dying netns _before_
4384edf547bSJohannes Berg  * subsys notifiers can be called, as most of the network code cleanup
4394edf547bSJohannes Berg  * (which is done from subsys notifiers) runs with the assumption that
4404edf547bSJohannes Berg  * dev_remove_pack has been called so no new packets will arrive during
4414edf547bSJohannes Berg  * and after the cleanup functions have been called.  dev_remove_pack
4424edf547bSJohannes Berg  * is not per namespace so instead the guarantee of no more packets
4434edf547bSJohannes Berg  * arriving in a network namespace is provided by ensuring that all
4444edf547bSJohannes Berg  * network devices and all sockets have left the network namespace
4454edf547bSJohannes Berg  * before the cleanup methods are called.
44617edde52SEric W. Biederman  *
44717edde52SEric W. Biederman  * For the longest time the ipv4 icmp code was registered as a pernet
44817edde52SEric W. Biederman  * device which caused kernel oops, and panics during network
44917edde52SEric W. Biederman  * namespace cleanup.   So please don't get this wrong.
45017edde52SEric W. Biederman  */
451e67e16eaSJoe Perches int register_pernet_subsys(struct pernet_operations *);
452e67e16eaSJoe Perches void unregister_pernet_subsys(struct pernet_operations *);
453e67e16eaSJoe Perches int register_pernet_device(struct pernet_operations *);
454e67e16eaSJoe Perches void unregister_pernet_device(struct pernet_operations *);
455f875bae0SEric W. Biederman 
45695bdfccbSEric W. Biederman struct ctl_table;
457d62c612eSPavel Emelyanov 
4582ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL
459e67e16eaSJoe Perches int net_sysctl_init(void);
460e67e16eaSJoe Perches struct ctl_table_header *register_net_sysctl(struct net *net, const char *path,
461e67e16eaSJoe Perches 					     struct ctl_table *table);
462e67e16eaSJoe Perches void unregister_net_sysctl_table(struct ctl_table_header *header);
46348c74958SEric W. Biederman #else
46448c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; }
46548c74958SEric W. Biederman static inline struct ctl_table_header *register_net_sysctl(struct net *net,
46648c74958SEric W. Biederman 	const char *path, struct ctl_table *table)
46748c74958SEric W. Biederman {
46848c74958SEric W. Biederman 	return NULL;
46948c74958SEric W. Biederman }
47048c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
47148c74958SEric W. Biederman {
47248c74958SEric W. Biederman }
47348c74958SEric W. Biederman #endif
47448c74958SEric W. Biederman 
47556f200c7SGuillaume Nault static inline int rt_genid_ipv4(const struct net *net)
476b42664f8SNicolas Dichtel {
477ca4c3fc2Sfan.du 	return atomic_read(&net->ipv4.rt_genid);
478b42664f8SNicolas Dichtel }
479b42664f8SNicolas Dichtel 
4808f34e53bSDavid Ahern #if IS_ENABLED(CONFIG_IPV6)
4818f34e53bSDavid Ahern static inline int rt_genid_ipv6(const struct net *net)
4828f34e53bSDavid Ahern {
4838f34e53bSDavid Ahern 	return atomic_read(&net->ipv6.fib6_sernum);
4848f34e53bSDavid Ahern }
4858f34e53bSDavid Ahern #endif
4868f34e53bSDavid Ahern 
487ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv4(struct net *net)
488b42664f8SNicolas Dichtel {
489ca4c3fc2Sfan.du 	atomic_inc(&net->ipv4.rt_genid);
490ca4c3fc2Sfan.du }
491ca4c3fc2Sfan.du 
492705f1c86SHannes Frederic Sowa extern void (*__fib6_flush_trees)(struct net *net);
493ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv6(struct net *net)
494ca4c3fc2Sfan.du {
495705f1c86SHannes Frederic Sowa 	if (__fib6_flush_trees)
496705f1c86SHannes Frederic Sowa 		__fib6_flush_trees(net);
497ca4c3fc2Sfan.du }
498ca4c3fc2Sfan.du 
499599018a7SLuis R. Rodriguez #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
500599018a7SLuis R. Rodriguez static inline struct netns_ieee802154_lowpan *
501599018a7SLuis R. Rodriguez net_ieee802154_lowpan(struct net *net)
502599018a7SLuis R. Rodriguez {
503599018a7SLuis R. Rodriguez 	return &net->ieee802154_lowpan;
504599018a7SLuis R. Rodriguez }
505599018a7SLuis R. Rodriguez #endif
506599018a7SLuis R. Rodriguez 
507ca4c3fc2Sfan.du /* For callers who don't really care about whether it's IPv4 or IPv6 */
508ca4c3fc2Sfan.du static inline void rt_genid_bump_all(struct net *net)
509ca4c3fc2Sfan.du {
510ca4c3fc2Sfan.du 	rt_genid_bump_ipv4(net);
511ca4c3fc2Sfan.du 	rt_genid_bump_ipv6(net);
512b42664f8SNicolas Dichtel }
51395bdfccbSEric W. Biederman 
51456f200c7SGuillaume Nault static inline int fnhe_genid(const struct net *net)
5155aad1de5STimo Teräs {
5165aad1de5STimo Teräs 	return atomic_read(&net->fnhe_genid);
5175aad1de5STimo Teräs }
5185aad1de5STimo Teräs 
5195aad1de5STimo Teräs static inline void fnhe_genid_bump(struct net *net)
5205aad1de5STimo Teräs {
5215aad1de5STimo Teräs 	atomic_inc(&net->fnhe_genid);
5225aad1de5STimo Teräs }
5235aad1de5STimo Teräs 
5249c1be193SEric Dumazet #ifdef CONFIG_NET
5259c1be193SEric Dumazet void net_ns_init(void);
5269c1be193SEric Dumazet #else
5279c1be193SEric Dumazet static inline void net_ns_init(void) {}
5289c1be193SEric Dumazet #endif
5299c1be193SEric Dumazet 
5305f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */
531