xref: /openbmc/linux/include/net/net_namespace.h (revision 04a6f82c)
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 
26457c4cbcSEric W. Biederman struct proc_dir_entry;
272774c7abSEric W. Biederman struct net_device;
2897c53cacSDenis V. Lunev struct sock;
291597fbc0SPavel Emelyanov struct ctl_table_header;
30dec827d1SPavel Emelyanov struct net_generic;
31134e6375SJohannes Berg struct sock;
322553d064SJulian Anastasov struct netns_ipvs;
331597fbc0SPavel Emelyanov 
347c28bd0bSEric Dumazet 
357c28bd0bSEric Dumazet #define NETDEV_HASHBITS    8
367c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
377c28bd0bSEric Dumazet 
385f256becSEric W. Biederman struct net {
39a685e089SAl Viro 	atomic_t		passive;	/* To decided when the network
405f256becSEric W. Biederman 						 * namespace should be freed.
415f256becSEric W. Biederman 						 */
42a685e089SAl Viro 	atomic_t		count;		/* To decided when the network
43a685e089SAl Viro 						 *  namespace should be shut down.
44a685e089SAl Viro 						 */
455d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG
465f256becSEric W. Biederman 	atomic_t		use_count;	/* To track references we
475f256becSEric W. Biederman 						 * destroy on demand
485f256becSEric W. Biederman 						 */
495d1e4468SDenis V. Lunev #endif
508e602ce2SEric Dumazet 	spinlock_t		rules_mod_lock;
518e602ce2SEric Dumazet 
525f256becSEric W. Biederman 	struct list_head	list;		/* list of network namespaces */
532b035b39SEric W. Biederman 	struct list_head	cleanup_list;	/* namespaces on death row */
5472ad937aSEric W. Biederman 	struct list_head	exit_list;	/* Use only net_mutex */
55457c4cbcSEric W. Biederman 
56457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net;
57457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net_stat;
58881d966bSEric W. Biederman 
5973455092SAl Viro #ifdef CONFIG_SYSCTL
6073455092SAl Viro 	struct ctl_table_set	sysctls;
6173455092SAl Viro #endif
6295bdfccbSEric W. Biederman 
638e602ce2SEric Dumazet 	struct sock 		*rtnl;			/* rtnetlink socket */
648e602ce2SEric Dumazet 	struct sock		*genl_sock;
652774c7abSEric W. Biederman 
66881d966bSEric W. Biederman 	struct list_head 	dev_base_head;
67881d966bSEric W. Biederman 	struct hlist_head 	*dev_name_head;
68881d966bSEric W. Biederman 	struct hlist_head	*dev_index_head;
694e985adaSThomas Graf 	unsigned int		dev_base_seq;	/* protected by rtnl_mutex */
70aa79e66eSPavel Emelyanov 	int			ifindex;
7197c53cacSDenis V. Lunev 
725fd30ee7SDenis V. Lunev 	/* core fib_rules */
735fd30ee7SDenis V. Lunev 	struct list_head	rules_ops;
745fd30ee7SDenis V. Lunev 
75d12d01d6SDenis V. Lunev 
768e602ce2SEric Dumazet 	struct net_device       *loopback_dev;          /* The loopback */
778efa6e93SPavel Emelyanov 	struct netns_core	core;
78852566f5SPavel Emelyanov 	struct netns_mib	mib;
792aaef4e4SDenis V. Lunev 	struct netns_packet	packet;
80a0a53c8bSDenis V. Lunev 	struct netns_unix	unx;
818afd351cSPavel Emelyanov 	struct netns_ipv4	ipv4;
82dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
83b0f159dbSDaniel Lezcano 	struct netns_ipv6	ipv6;
84b0f159dbSDaniel Lezcano #endif
854db67e80SEric W. Biederman #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
864db67e80SEric W. Biederman 	struct netns_sctp	sctp;
874db67e80SEric W. Biederman #endif
8867019cc9SPavel Emelyanov #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
8967019cc9SPavel Emelyanov 	struct netns_dccp	dccp;
9067019cc9SPavel Emelyanov #endif
918d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER
928d870052SAlexey Dobriyan 	struct netns_xt		xt;
93dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
94dfdb8d79SAlexey Dobriyan 	struct netns_ct		ct;
95dfdb8d79SAlexey Dobriyan #endif
96c038a767SAmerigo Wang #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
97c038a767SAmerigo Wang 	struct netns_nf_frag	nf_frag;
98c038a767SAmerigo Wang #endif
99cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl;
100cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl_stash;
1018d870052SAlexey Dobriyan #endif
1023d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE
103b333b3d2SJohannes Berg 	struct sk_buff_head	wext_nlevents;
104b333b3d2SJohannes Berg #endif
1051c87733dSEric Dumazet 	struct net_generic __rcu	*gen;
1068e602ce2SEric Dumazet 
1078e602ce2SEric Dumazet 	/* Note : following structs are cache line aligned */
1088e602ce2SEric Dumazet #ifdef CONFIG_XFRM
1098e602ce2SEric Dumazet 	struct netns_xfrm	xfrm;
1108e602ce2SEric Dumazet #endif
11161b1ab45SHans Schillstrom 	struct netns_ipvs	*ipvs;
11251d7cccfSAndrey Vagin 	struct sock		*diag_nlsk;
113b42664f8SNicolas Dichtel 	atomic_t		rt_genid;
1145f256becSEric W. Biederman };
1155f256becSEric W. Biederman 
1161fb9489bSPavel Emelyanov /*
1171fb9489bSPavel Emelyanov  * ifindex generation is per-net namespace, and loopback is
1181fb9489bSPavel Emelyanov  * always the 1st device in ns (see net_dev_init), thus any
1191fb9489bSPavel Emelyanov  * loopback device should get ifindex 1
1201fb9489bSPavel Emelyanov  */
1211fb9489bSPavel Emelyanov 
1221fb9489bSPavel Emelyanov #define LOOPBACK_IFINDEX	1
123225c0a01SDenis V. Lunev 
124c0f39322SDenis V. Lunev #include <linux/seq_file_net.h>
125c0f39322SDenis V. Lunev 
1264fabcd71SDaniel Lezcano /* Init's network namespace */
1275f256becSEric W. Biederman extern struct net init_net;
128a4aa834aSDenis V. Lunev 
129a4aa834aSDenis V. Lunev #ifdef CONFIG_NET
1309dd776b6SEric W. Biederman extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
131225c0a01SDenis V. Lunev 
132225c0a01SDenis V. Lunev #else /* CONFIG_NET */
1339dd776b6SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
1349dd776b6SEric W. Biederman {
1359dd776b6SEric W. Biederman 	/* There is nothing to copy so this is a noop */
1369dd776b6SEric W. Biederman 	return net_ns;
1379dd776b6SEric W. Biederman }
138225c0a01SDenis V. Lunev #endif /* CONFIG_NET */
139225c0a01SDenis V. Lunev 
140225c0a01SDenis V. Lunev 
141225c0a01SDenis V. Lunev extern struct list_head net_namespace_list;
1429dd776b6SEric W. Biederman 
14330ffee84SJohannes Berg extern struct net *get_net_ns_by_pid(pid_t pid);
144f0630529SEric W. Biederman extern struct net *get_net_ns_by_fd(int pid);
14530ffee84SJohannes Berg 
146d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS
1475f256becSEric W. Biederman extern void __put_net(struct net *net);
1485f256becSEric W. Biederman 
1495f256becSEric W. Biederman static inline struct net *get_net(struct net *net)
1505f256becSEric W. Biederman {
1515f256becSEric W. Biederman 	atomic_inc(&net->count);
1525f256becSEric W. Biederman 	return net;
1535f256becSEric W. Biederman }
1545f256becSEric W. Biederman 
155077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net)
156077130c0SEric W. Biederman {
157077130c0SEric W. Biederman 	/* Used when we know struct net exists but we
158077130c0SEric W. Biederman 	 * aren't guaranteed a previous reference count
159077130c0SEric W. Biederman 	 * exists.  If the reference count is zero this
160077130c0SEric W. Biederman 	 * function fails and returns NULL.
161077130c0SEric W. Biederman 	 */
162077130c0SEric W. Biederman 	if (!atomic_inc_not_zero(&net->count))
163077130c0SEric W. Biederman 		net = NULL;
164077130c0SEric W. Biederman 	return net;
165077130c0SEric W. Biederman }
166077130c0SEric W. Biederman 
1675f256becSEric W. Biederman static inline void put_net(struct net *net)
1685f256becSEric W. Biederman {
1695f256becSEric W. Biederman 	if (atomic_dec_and_test(&net->count))
1705f256becSEric W. Biederman 		__put_net(net);
1715f256becSEric W. Biederman }
1725f256becSEric W. Biederman 
173878628fbSYOSHIFUJI Hideaki static inline
174878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
175878628fbSYOSHIFUJI Hideaki {
176878628fbSYOSHIFUJI Hideaki 	return net1 == net2;
177878628fbSYOSHIFUJI Hideaki }
178a685e089SAl Viro 
179a685e089SAl Viro extern void net_drop_ns(void *);
180a685e089SAl Viro 
181d4655795SPavel Emelyanov #else
182b9f75f45SEric W. Biederman 
183d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net)
184d4655795SPavel Emelyanov {
185d4655795SPavel Emelyanov 	return net;
186d4655795SPavel Emelyanov }
187d4655795SPavel Emelyanov 
188d4655795SPavel Emelyanov static inline void put_net(struct net *net)
189d4655795SPavel Emelyanov {
190d4655795SPavel Emelyanov }
191d4655795SPavel Emelyanov 
192d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net)
193d4655795SPavel Emelyanov {
194d4655795SPavel Emelyanov 	return net;
195d4655795SPavel Emelyanov }
196878628fbSYOSHIFUJI Hideaki 
197878628fbSYOSHIFUJI Hideaki static inline
198878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
199878628fbSYOSHIFUJI Hideaki {
200878628fbSYOSHIFUJI Hideaki 	return 1;
201878628fbSYOSHIFUJI Hideaki }
202a685e089SAl Viro 
203a685e089SAl Viro #define net_drop_ns NULL
204d4655795SPavel Emelyanov #endif
2055f256becSEric W. Biederman 
2065d1e4468SDenis V. Lunev 
2075d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG
2085d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net)
2095d1e4468SDenis V. Lunev {
2105d1e4468SDenis V. Lunev 	if (net)
2115d1e4468SDenis V. Lunev 		atomic_inc(&net->use_count);
2125d1e4468SDenis V. Lunev 	return net;
2135d1e4468SDenis V. Lunev }
2145d1e4468SDenis V. Lunev 
2155d1e4468SDenis V. Lunev static inline void release_net(struct net *net)
2165d1e4468SDenis V. Lunev {
2175d1e4468SDenis V. Lunev 	if (net)
2185d1e4468SDenis V. Lunev 		atomic_dec(&net->use_count);
2195d1e4468SDenis V. Lunev }
2205d1e4468SDenis V. Lunev #else
2215d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net)
2225d1e4468SDenis V. Lunev {
2235d1e4468SDenis V. Lunev 	return net;
2245d1e4468SDenis V. Lunev }
2255d1e4468SDenis V. Lunev 
2265d1e4468SDenis V. Lunev static inline void release_net(struct net *net)
2275d1e4468SDenis V. Lunev {
2285d1e4468SDenis V. Lunev }
2295d1e4468SDenis V. Lunev #endif
2305d1e4468SDenis V. Lunev 
2318f424b5fSEric Dumazet #ifdef CONFIG_NET_NS
2328f424b5fSEric Dumazet 
2338f424b5fSEric Dumazet static inline void write_pnet(struct net **pnet, struct net *net)
2348f424b5fSEric Dumazet {
2358f424b5fSEric Dumazet 	*pnet = net;
2368f424b5fSEric Dumazet }
2378f424b5fSEric Dumazet 
2388f424b5fSEric Dumazet static inline struct net *read_pnet(struct net * const *pnet)
2398f424b5fSEric Dumazet {
2408f424b5fSEric Dumazet 	return *pnet;
2418f424b5fSEric Dumazet }
2428f424b5fSEric Dumazet 
2438f424b5fSEric Dumazet #else
2448f424b5fSEric Dumazet 
2458f424b5fSEric Dumazet #define write_pnet(pnet, net)	do { (void)(net);} while (0)
2468f424b5fSEric Dumazet #define read_pnet(pnet)		(&init_net)
2478f424b5fSEric Dumazet 
2488f424b5fSEric Dumazet #endif
2495d1e4468SDenis V. Lunev 
2505f256becSEric W. Biederman #define for_each_net(VAR)				\
2515f256becSEric W. Biederman 	list_for_each_entry(VAR, &net_namespace_list, list)
2525f256becSEric W. Biederman 
25311a28d37SJohannes Berg #define for_each_net_rcu(VAR)				\
25411a28d37SJohannes Berg 	list_for_each_entry_rcu(VAR, &net_namespace_list, list)
25511a28d37SJohannes Berg 
2564665079cSPavel Emelyanov #ifdef CONFIG_NET_NS
2574665079cSPavel Emelyanov #define __net_init
2584665079cSPavel Emelyanov #define __net_exit
259022cbae6SDenis V. Lunev #define __net_initdata
26004a6f82cSAndi Kleen #define __net_initconst
2614665079cSPavel Emelyanov #else
2624665079cSPavel Emelyanov #define __net_init	__init
2634665079cSPavel Emelyanov #define __net_exit	__exit_refok
264022cbae6SDenis V. Lunev #define __net_initdata	__initdata
26504a6f82cSAndi Kleen #define __net_initconst	__initconst
2664665079cSPavel Emelyanov #endif
2675f256becSEric W. Biederman 
2685f256becSEric W. Biederman struct pernet_operations {
2695f256becSEric W. Biederman 	struct list_head list;
2705f256becSEric W. Biederman 	int (*init)(struct net *net);
2715f256becSEric W. Biederman 	void (*exit)(struct net *net);
27272ad937aSEric W. Biederman 	void (*exit_batch)(struct list_head *net_exit_list);
273f875bae0SEric W. Biederman 	int *id;
274f875bae0SEric W. Biederman 	size_t size;
2755f256becSEric W. Biederman };
2765f256becSEric W. Biederman 
27717edde52SEric W. Biederman /*
27817edde52SEric W. Biederman  * Use these carefully.  If you implement a network device and it
27917edde52SEric W. Biederman  * needs per network namespace operations use device pernet operations,
28017edde52SEric W. Biederman  * otherwise use pernet subsys operations.
28117edde52SEric W. Biederman  *
2824edf547bSJohannes Berg  * Network interfaces need to be removed from a dying netns _before_
2834edf547bSJohannes Berg  * subsys notifiers can be called, as most of the network code cleanup
2844edf547bSJohannes Berg  * (which is done from subsys notifiers) runs with the assumption that
2854edf547bSJohannes Berg  * dev_remove_pack has been called so no new packets will arrive during
2864edf547bSJohannes Berg  * and after the cleanup functions have been called.  dev_remove_pack
2874edf547bSJohannes Berg  * is not per namespace so instead the guarantee of no more packets
2884edf547bSJohannes Berg  * arriving in a network namespace is provided by ensuring that all
2894edf547bSJohannes Berg  * network devices and all sockets have left the network namespace
2904edf547bSJohannes Berg  * before the cleanup methods are called.
29117edde52SEric W. Biederman  *
29217edde52SEric W. Biederman  * For the longest time the ipv4 icmp code was registered as a pernet
29317edde52SEric W. Biederman  * device which caused kernel oops, and panics during network
29417edde52SEric W. Biederman  * namespace cleanup.   So please don't get this wrong.
29517edde52SEric W. Biederman  */
2965f256becSEric W. Biederman extern int register_pernet_subsys(struct pernet_operations *);
2975f256becSEric W. Biederman extern void unregister_pernet_subsys(struct pernet_operations *);
2985f256becSEric W. Biederman extern int register_pernet_device(struct pernet_operations *);
2995f256becSEric W. Biederman extern void unregister_pernet_device(struct pernet_operations *);
300f875bae0SEric W. Biederman 
30195bdfccbSEric W. Biederman struct ctl_table;
30295bdfccbSEric W. Biederman struct ctl_table_header;
303d62c612eSPavel Emelyanov 
3042ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL
3052ca794e5SEric W. Biederman extern int net_sysctl_init(void);
306ab41a2caSEric W. Biederman extern struct ctl_table_header *register_net_sysctl(struct net *net,
307ab41a2caSEric W. Biederman 	const char *path, struct ctl_table *table);
30895bdfccbSEric W. Biederman extern void unregister_net_sysctl_table(struct ctl_table_header *header);
30948c74958SEric W. Biederman #else
31048c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; }
31148c74958SEric W. Biederman static inline struct ctl_table_header *register_net_sysctl(struct net *net,
31248c74958SEric W. Biederman 	const char *path, struct ctl_table *table)
31348c74958SEric W. Biederman {
31448c74958SEric W. Biederman 	return NULL;
31548c74958SEric W. Biederman }
31648c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
31748c74958SEric W. Biederman {
31848c74958SEric W. Biederman }
31948c74958SEric W. Biederman #endif
32048c74958SEric W. Biederman 
321b42664f8SNicolas Dichtel static inline int rt_genid(struct net *net)
322b42664f8SNicolas Dichtel {
323b42664f8SNicolas Dichtel 	return atomic_read(&net->rt_genid);
324b42664f8SNicolas Dichtel }
325b42664f8SNicolas Dichtel 
326b42664f8SNicolas Dichtel static inline void rt_genid_bump(struct net *net)
327b42664f8SNicolas Dichtel {
328b42664f8SNicolas Dichtel 	atomic_inc(&net->rt_genid);
329b42664f8SNicolas Dichtel }
33095bdfccbSEric W. Biederman 
3315f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */
332