xref: /openbmc/linux/include/net/net_namespace.h (revision 98f842e6)
15f256becSEric W. Biederman /*
25f256becSEric W. Biederman  * Operations on the network namespace
35f256becSEric W. Biederman  */
45f256becSEric W. Biederman #ifndef __NET_NET_NAMESPACE_H
55f256becSEric W. Biederman #define __NET_NET_NAMESPACE_H
65f256becSEric W. Biederman 
760063497SArun Sharma #include <linux/atomic.h>
85f256becSEric W. Biederman #include <linux/workqueue.h>
95f256becSEric W. Biederman #include <linux/list.h>
10bee95250SDavid S. Miller #include <linux/sysctl.h>
115f256becSEric W. Biederman 
128efa6e93SPavel Emelyanov #include <net/netns/core.h>
13852566f5SPavel Emelyanov #include <net/netns/mib.h>
14a0a53c8bSDenis V. Lunev #include <net/netns/unix.h>
152aaef4e4SDenis V. Lunev #include <net/netns/packet.h>
168afd351cSPavel Emelyanov #include <net/netns/ipv4.h>
17b0f159dbSDaniel Lezcano #include <net/netns/ipv6.h>
184db67e80SEric W. Biederman #include <net/netns/sctp.h>
1967019cc9SPavel Emelyanov #include <net/netns/dccp.h>
208d870052SAlexey Dobriyan #include <net/netns/x_tables.h>
21dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
22dfdb8d79SAlexey Dobriyan #include <net/netns/conntrack.h>
23dfdb8d79SAlexey Dobriyan #endif
24d62ddc21SAlexey Dobriyan #include <net/netns/xfrm.h>
25a0a53c8bSDenis V. Lunev 
26038e7332SEric W. Biederman struct user_namespace;
27457c4cbcSEric W. Biederman struct proc_dir_entry;
282774c7abSEric W. Biederman struct net_device;
2997c53cacSDenis V. Lunev struct sock;
301597fbc0SPavel Emelyanov struct ctl_table_header;
31dec827d1SPavel Emelyanov struct net_generic;
32134e6375SJohannes Berg struct sock;
332553d064SJulian Anastasov struct netns_ipvs;
341597fbc0SPavel Emelyanov 
357c28bd0bSEric Dumazet 
367c28bd0bSEric Dumazet #define NETDEV_HASHBITS    8
377c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
387c28bd0bSEric Dumazet 
395f256becSEric W. Biederman struct net {
40a685e089SAl Viro 	atomic_t		passive;	/* To decided when the network
415f256becSEric W. Biederman 						 * namespace should be freed.
425f256becSEric W. Biederman 						 */
43a685e089SAl Viro 	atomic_t		count;		/* To decided when the network
44a685e089SAl Viro 						 *  namespace should be shut down.
45a685e089SAl Viro 						 */
465d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG
475f256becSEric W. Biederman 	atomic_t		use_count;	/* To track references we
485f256becSEric W. Biederman 						 * destroy on demand
495f256becSEric W. Biederman 						 */
505d1e4468SDenis V. Lunev #endif
518e602ce2SEric Dumazet 	spinlock_t		rules_mod_lock;
528e602ce2SEric Dumazet 
535f256becSEric W. Biederman 	struct list_head	list;		/* list of network namespaces */
542b035b39SEric W. Biederman 	struct list_head	cleanup_list;	/* namespaces on death row */
5572ad937aSEric W. Biederman 	struct list_head	exit_list;	/* Use only net_mutex */
56457c4cbcSEric W. Biederman 
57038e7332SEric W. Biederman 	struct user_namespace   *user_ns;	/* Owning user namespace */
58038e7332SEric W. Biederman 
5998f842e6SEric W. Biederman 	unsigned int		proc_inum;
6098f842e6SEric W. Biederman 
61457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net;
62457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net_stat;
63881d966bSEric W. Biederman 
6473455092SAl Viro #ifdef CONFIG_SYSCTL
6573455092SAl Viro 	struct ctl_table_set	sysctls;
6673455092SAl Viro #endif
6795bdfccbSEric W. Biederman 
688e602ce2SEric Dumazet 	struct sock 		*rtnl;			/* rtnetlink socket */
698e602ce2SEric Dumazet 	struct sock		*genl_sock;
702774c7abSEric W. Biederman 
71881d966bSEric W. Biederman 	struct list_head 	dev_base_head;
72881d966bSEric W. Biederman 	struct hlist_head 	*dev_name_head;
73881d966bSEric W. Biederman 	struct hlist_head	*dev_index_head;
744e985adaSThomas Graf 	unsigned int		dev_base_seq;	/* protected by rtnl_mutex */
75aa79e66eSPavel Emelyanov 	int			ifindex;
7697c53cacSDenis V. Lunev 
775fd30ee7SDenis V. Lunev 	/* core fib_rules */
785fd30ee7SDenis V. Lunev 	struct list_head	rules_ops;
795fd30ee7SDenis V. Lunev 
80d12d01d6SDenis V. Lunev 
818e602ce2SEric Dumazet 	struct net_device       *loopback_dev;          /* The loopback */
828efa6e93SPavel Emelyanov 	struct netns_core	core;
83852566f5SPavel Emelyanov 	struct netns_mib	mib;
842aaef4e4SDenis V. Lunev 	struct netns_packet	packet;
85a0a53c8bSDenis V. Lunev 	struct netns_unix	unx;
868afd351cSPavel Emelyanov 	struct netns_ipv4	ipv4;
87dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
88b0f159dbSDaniel Lezcano 	struct netns_ipv6	ipv6;
89b0f159dbSDaniel Lezcano #endif
904db67e80SEric W. Biederman #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
914db67e80SEric W. Biederman 	struct netns_sctp	sctp;
924db67e80SEric W. Biederman #endif
9367019cc9SPavel Emelyanov #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
9467019cc9SPavel Emelyanov 	struct netns_dccp	dccp;
9567019cc9SPavel Emelyanov #endif
968d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER
978d870052SAlexey Dobriyan 	struct netns_xt		xt;
98dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
99dfdb8d79SAlexey Dobriyan 	struct netns_ct		ct;
100dfdb8d79SAlexey Dobriyan #endif
101c038a767SAmerigo Wang #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
102c038a767SAmerigo Wang 	struct netns_nf_frag	nf_frag;
103c038a767SAmerigo Wang #endif
104cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl;
105cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl_stash;
1068d870052SAlexey Dobriyan #endif
1073d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE
108b333b3d2SJohannes Berg 	struct sk_buff_head	wext_nlevents;
109b333b3d2SJohannes Berg #endif
1101c87733dSEric Dumazet 	struct net_generic __rcu	*gen;
1118e602ce2SEric Dumazet 
1128e602ce2SEric Dumazet 	/* Note : following structs are cache line aligned */
1138e602ce2SEric Dumazet #ifdef CONFIG_XFRM
1148e602ce2SEric Dumazet 	struct netns_xfrm	xfrm;
1158e602ce2SEric Dumazet #endif
11661b1ab45SHans Schillstrom 	struct netns_ipvs	*ipvs;
11751d7cccfSAndrey Vagin 	struct sock		*diag_nlsk;
118b42664f8SNicolas Dichtel 	atomic_t		rt_genid;
1195f256becSEric W. Biederman };
1205f256becSEric W. Biederman 
1211fb9489bSPavel Emelyanov /*
1221fb9489bSPavel Emelyanov  * ifindex generation is per-net namespace, and loopback is
1231fb9489bSPavel Emelyanov  * always the 1st device in ns (see net_dev_init), thus any
1241fb9489bSPavel Emelyanov  * loopback device should get ifindex 1
1251fb9489bSPavel Emelyanov  */
1261fb9489bSPavel Emelyanov 
1271fb9489bSPavel Emelyanov #define LOOPBACK_IFINDEX	1
128225c0a01SDenis V. Lunev 
129c0f39322SDenis V. Lunev #include <linux/seq_file_net.h>
130c0f39322SDenis V. Lunev 
1314fabcd71SDaniel Lezcano /* Init's network namespace */
1325f256becSEric W. Biederman extern struct net init_net;
133a4aa834aSDenis V. Lunev 
134d727abcbSEric W. Biederman #ifdef CONFIG_NET_NS
135038e7332SEric W. Biederman extern struct net *copy_net_ns(unsigned long flags,
136038e7332SEric W. Biederman 	struct user_namespace *user_ns, struct net *old_net);
137225c0a01SDenis V. Lunev 
138d727abcbSEric W. Biederman #else /* CONFIG_NET_NS */
139d727abcbSEric W. Biederman #include <linux/sched.h>
140d727abcbSEric W. Biederman #include <linux/nsproxy.h>
141038e7332SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags,
142038e7332SEric W. Biederman 	struct user_namespace *user_ns, struct net *old_net)
1439dd776b6SEric W. Biederman {
144d727abcbSEric W. Biederman 	if (flags & CLONE_NEWNET)
145d727abcbSEric W. Biederman 		return ERR_PTR(-EINVAL);
146d727abcbSEric W. Biederman 	return old_net;
1479dd776b6SEric W. Biederman }
148d727abcbSEric W. Biederman #endif /* CONFIG_NET_NS */
149225c0a01SDenis V. Lunev 
150225c0a01SDenis V. Lunev 
151225c0a01SDenis V. Lunev extern struct list_head net_namespace_list;
1529dd776b6SEric W. Biederman 
15330ffee84SJohannes Berg extern struct net *get_net_ns_by_pid(pid_t pid);
154f0630529SEric W. Biederman extern struct net *get_net_ns_by_fd(int pid);
15530ffee84SJohannes Berg 
156d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS
1575f256becSEric W. Biederman extern void __put_net(struct net *net);
1585f256becSEric W. Biederman 
1595f256becSEric W. Biederman static inline struct net *get_net(struct net *net)
1605f256becSEric W. Biederman {
1615f256becSEric W. Biederman 	atomic_inc(&net->count);
1625f256becSEric W. Biederman 	return net;
1635f256becSEric W. Biederman }
1645f256becSEric W. Biederman 
165077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net)
166077130c0SEric W. Biederman {
167077130c0SEric W. Biederman 	/* Used when we know struct net exists but we
168077130c0SEric W. Biederman 	 * aren't guaranteed a previous reference count
169077130c0SEric W. Biederman 	 * exists.  If the reference count is zero this
170077130c0SEric W. Biederman 	 * function fails and returns NULL.
171077130c0SEric W. Biederman 	 */
172077130c0SEric W. Biederman 	if (!atomic_inc_not_zero(&net->count))
173077130c0SEric W. Biederman 		net = NULL;
174077130c0SEric W. Biederman 	return net;
175077130c0SEric W. Biederman }
176077130c0SEric W. Biederman 
1775f256becSEric W. Biederman static inline void put_net(struct net *net)
1785f256becSEric W. Biederman {
1795f256becSEric W. Biederman 	if (atomic_dec_and_test(&net->count))
1805f256becSEric W. Biederman 		__put_net(net);
1815f256becSEric W. Biederman }
1825f256becSEric W. Biederman 
183878628fbSYOSHIFUJI Hideaki static inline
184878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
185878628fbSYOSHIFUJI Hideaki {
186878628fbSYOSHIFUJI Hideaki 	return net1 == net2;
187878628fbSYOSHIFUJI Hideaki }
188a685e089SAl Viro 
189a685e089SAl Viro extern void net_drop_ns(void *);
190a685e089SAl Viro 
191d4655795SPavel Emelyanov #else
192b9f75f45SEric W. Biederman 
193d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net)
194d4655795SPavel Emelyanov {
195d4655795SPavel Emelyanov 	return net;
196d4655795SPavel Emelyanov }
197d4655795SPavel Emelyanov 
198d4655795SPavel Emelyanov static inline void put_net(struct net *net)
199d4655795SPavel Emelyanov {
200d4655795SPavel Emelyanov }
201d4655795SPavel Emelyanov 
202d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net)
203d4655795SPavel Emelyanov {
204d4655795SPavel Emelyanov 	return net;
205d4655795SPavel Emelyanov }
206878628fbSYOSHIFUJI Hideaki 
207878628fbSYOSHIFUJI Hideaki static inline
208878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
209878628fbSYOSHIFUJI Hideaki {
210878628fbSYOSHIFUJI Hideaki 	return 1;
211878628fbSYOSHIFUJI Hideaki }
212a685e089SAl Viro 
213a685e089SAl Viro #define net_drop_ns NULL
214d4655795SPavel Emelyanov #endif
2155f256becSEric W. Biederman 
2165d1e4468SDenis V. Lunev 
2175d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG
2185d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net)
2195d1e4468SDenis V. Lunev {
2205d1e4468SDenis V. Lunev 	if (net)
2215d1e4468SDenis V. Lunev 		atomic_inc(&net->use_count);
2225d1e4468SDenis V. Lunev 	return net;
2235d1e4468SDenis V. Lunev }
2245d1e4468SDenis V. Lunev 
2255d1e4468SDenis V. Lunev static inline void release_net(struct net *net)
2265d1e4468SDenis V. Lunev {
2275d1e4468SDenis V. Lunev 	if (net)
2285d1e4468SDenis V. Lunev 		atomic_dec(&net->use_count);
2295d1e4468SDenis V. Lunev }
2305d1e4468SDenis V. Lunev #else
2315d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net)
2325d1e4468SDenis V. Lunev {
2335d1e4468SDenis V. Lunev 	return net;
2345d1e4468SDenis V. Lunev }
2355d1e4468SDenis V. Lunev 
2365d1e4468SDenis V. Lunev static inline void release_net(struct net *net)
2375d1e4468SDenis V. Lunev {
2385d1e4468SDenis V. Lunev }
2395d1e4468SDenis V. Lunev #endif
2405d1e4468SDenis V. Lunev 
2418f424b5fSEric Dumazet #ifdef CONFIG_NET_NS
2428f424b5fSEric Dumazet 
2438f424b5fSEric Dumazet static inline void write_pnet(struct net **pnet, struct net *net)
2448f424b5fSEric Dumazet {
2458f424b5fSEric Dumazet 	*pnet = net;
2468f424b5fSEric Dumazet }
2478f424b5fSEric Dumazet 
2488f424b5fSEric Dumazet static inline struct net *read_pnet(struct net * const *pnet)
2498f424b5fSEric Dumazet {
2508f424b5fSEric Dumazet 	return *pnet;
2518f424b5fSEric Dumazet }
2528f424b5fSEric Dumazet 
2538f424b5fSEric Dumazet #else
2548f424b5fSEric Dumazet 
2558f424b5fSEric Dumazet #define write_pnet(pnet, net)	do { (void)(net);} while (0)
2568f424b5fSEric Dumazet #define read_pnet(pnet)		(&init_net)
2578f424b5fSEric Dumazet 
2588f424b5fSEric Dumazet #endif
2595d1e4468SDenis V. Lunev 
2605f256becSEric W. Biederman #define for_each_net(VAR)				\
2615f256becSEric W. Biederman 	list_for_each_entry(VAR, &net_namespace_list, list)
2625f256becSEric W. Biederman 
26311a28d37SJohannes Berg #define for_each_net_rcu(VAR)				\
26411a28d37SJohannes Berg 	list_for_each_entry_rcu(VAR, &net_namespace_list, list)
26511a28d37SJohannes Berg 
2664665079cSPavel Emelyanov #ifdef CONFIG_NET_NS
2674665079cSPavel Emelyanov #define __net_init
2684665079cSPavel Emelyanov #define __net_exit
269022cbae6SDenis V. Lunev #define __net_initdata
27004a6f82cSAndi Kleen #define __net_initconst
2714665079cSPavel Emelyanov #else
2724665079cSPavel Emelyanov #define __net_init	__init
2734665079cSPavel Emelyanov #define __net_exit	__exit_refok
274022cbae6SDenis V. Lunev #define __net_initdata	__initdata
27504a6f82cSAndi Kleen #define __net_initconst	__initconst
2764665079cSPavel Emelyanov #endif
2775f256becSEric W. Biederman 
2785f256becSEric W. Biederman struct pernet_operations {
2795f256becSEric W. Biederman 	struct list_head list;
2805f256becSEric W. Biederman 	int (*init)(struct net *net);
2815f256becSEric W. Biederman 	void (*exit)(struct net *net);
28272ad937aSEric W. Biederman 	void (*exit_batch)(struct list_head *net_exit_list);
283f875bae0SEric W. Biederman 	int *id;
284f875bae0SEric W. Biederman 	size_t size;
2855f256becSEric W. Biederman };
2865f256becSEric W. Biederman 
28717edde52SEric W. Biederman /*
28817edde52SEric W. Biederman  * Use these carefully.  If you implement a network device and it
28917edde52SEric W. Biederman  * needs per network namespace operations use device pernet operations,
29017edde52SEric W. Biederman  * otherwise use pernet subsys operations.
29117edde52SEric W. Biederman  *
2924edf547bSJohannes Berg  * Network interfaces need to be removed from a dying netns _before_
2934edf547bSJohannes Berg  * subsys notifiers can be called, as most of the network code cleanup
2944edf547bSJohannes Berg  * (which is done from subsys notifiers) runs with the assumption that
2954edf547bSJohannes Berg  * dev_remove_pack has been called so no new packets will arrive during
2964edf547bSJohannes Berg  * and after the cleanup functions have been called.  dev_remove_pack
2974edf547bSJohannes Berg  * is not per namespace so instead the guarantee of no more packets
2984edf547bSJohannes Berg  * arriving in a network namespace is provided by ensuring that all
2994edf547bSJohannes Berg  * network devices and all sockets have left the network namespace
3004edf547bSJohannes Berg  * before the cleanup methods are called.
30117edde52SEric W. Biederman  *
30217edde52SEric W. Biederman  * For the longest time the ipv4 icmp code was registered as a pernet
30317edde52SEric W. Biederman  * device which caused kernel oops, and panics during network
30417edde52SEric W. Biederman  * namespace cleanup.   So please don't get this wrong.
30517edde52SEric W. Biederman  */
3065f256becSEric W. Biederman extern int register_pernet_subsys(struct pernet_operations *);
3075f256becSEric W. Biederman extern void unregister_pernet_subsys(struct pernet_operations *);
3085f256becSEric W. Biederman extern int register_pernet_device(struct pernet_operations *);
3095f256becSEric W. Biederman extern void unregister_pernet_device(struct pernet_operations *);
310f875bae0SEric W. Biederman 
31195bdfccbSEric W. Biederman struct ctl_table;
31295bdfccbSEric W. Biederman struct ctl_table_header;
313d62c612eSPavel Emelyanov 
3142ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL
3152ca794e5SEric W. Biederman extern int net_sysctl_init(void);
316ab41a2caSEric W. Biederman extern struct ctl_table_header *register_net_sysctl(struct net *net,
317ab41a2caSEric W. Biederman 	const char *path, struct ctl_table *table);
31895bdfccbSEric W. Biederman extern void unregister_net_sysctl_table(struct ctl_table_header *header);
31948c74958SEric W. Biederman #else
32048c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; }
32148c74958SEric W. Biederman static inline struct ctl_table_header *register_net_sysctl(struct net *net,
32248c74958SEric W. Biederman 	const char *path, struct ctl_table *table)
32348c74958SEric W. Biederman {
32448c74958SEric W. Biederman 	return NULL;
32548c74958SEric W. Biederman }
32648c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
32748c74958SEric W. Biederman {
32848c74958SEric W. Biederman }
32948c74958SEric W. Biederman #endif
33048c74958SEric W. Biederman 
331b42664f8SNicolas Dichtel static inline int rt_genid(struct net *net)
332b42664f8SNicolas Dichtel {
333b42664f8SNicolas Dichtel 	return atomic_read(&net->rt_genid);
334b42664f8SNicolas Dichtel }
335b42664f8SNicolas Dichtel 
336b42664f8SNicolas Dichtel static inline void rt_genid_bump(struct net *net)
337b42664f8SNicolas Dichtel {
338b42664f8SNicolas Dichtel 	atomic_inc(&net->rt_genid);
339b42664f8SNicolas Dichtel }
34095bdfccbSEric W. Biederman 
3415f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */
342