xref: /openbmc/linux/include/net/net_namespace.h (revision 2b035b39)
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 
75f256becSEric W. Biederman #include <asm/atomic.h>
85f256becSEric W. Biederman #include <linux/workqueue.h>
95f256becSEric W. Biederman #include <linux/list.h>
105f256becSEric W. Biederman 
118efa6e93SPavel Emelyanov #include <net/netns/core.h>
12852566f5SPavel Emelyanov #include <net/netns/mib.h>
13a0a53c8bSDenis V. Lunev #include <net/netns/unix.h>
142aaef4e4SDenis V. Lunev #include <net/netns/packet.h>
158afd351cSPavel Emelyanov #include <net/netns/ipv4.h>
16b0f159dbSDaniel Lezcano #include <net/netns/ipv6.h>
1767019cc9SPavel Emelyanov #include <net/netns/dccp.h>
188d870052SAlexey Dobriyan #include <net/netns/x_tables.h>
19dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
20dfdb8d79SAlexey Dobriyan #include <net/netns/conntrack.h>
21dfdb8d79SAlexey Dobriyan #endif
22d62ddc21SAlexey Dobriyan #include <net/netns/xfrm.h>
23a0a53c8bSDenis V. Lunev 
24457c4cbcSEric W. Biederman struct proc_dir_entry;
252774c7abSEric W. Biederman struct net_device;
2697c53cacSDenis V. Lunev struct sock;
271597fbc0SPavel Emelyanov struct ctl_table_header;
28dec827d1SPavel Emelyanov struct net_generic;
29134e6375SJohannes Berg struct sock;
301597fbc0SPavel Emelyanov 
317c28bd0bSEric Dumazet 
327c28bd0bSEric Dumazet #define NETDEV_HASHBITS    8
337c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
347c28bd0bSEric Dumazet 
355f256becSEric W. Biederman struct net {
365f256becSEric W. Biederman 	atomic_t		count;		/* To decided when the network
375f256becSEric W. Biederman 						 *  namespace should be freed.
385f256becSEric W. Biederman 						 */
395d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG
405f256becSEric W. Biederman 	atomic_t		use_count;	/* To track references we
415f256becSEric W. Biederman 						 * destroy on demand
425f256becSEric W. Biederman 						 */
435d1e4468SDenis V. Lunev #endif
445f256becSEric W. Biederman 	struct list_head	list;		/* list of network namespaces */
452b035b39SEric W. Biederman 	struct list_head	cleanup_list;	/* namespaces on death row */
46457c4cbcSEric W. Biederman 
47457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net;
48457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net_stat;
49881d966bSEric W. Biederman 
5073455092SAl Viro #ifdef CONFIG_SYSCTL
5173455092SAl Viro 	struct ctl_table_set	sysctls;
5273455092SAl Viro #endif
5395bdfccbSEric W. Biederman 
542774c7abSEric W. Biederman 	struct net_device       *loopback_dev;          /* The loopback */
552774c7abSEric W. Biederman 
56881d966bSEric W. Biederman 	struct list_head 	dev_base_head;
57881d966bSEric W. Biederman 	struct hlist_head 	*dev_name_head;
58881d966bSEric W. Biederman 	struct hlist_head	*dev_index_head;
5997c53cacSDenis V. Lunev 
605fd30ee7SDenis V. Lunev 	/* core fib_rules */
615fd30ee7SDenis V. Lunev 	struct list_head	rules_ops;
625fd30ee7SDenis V. Lunev 	spinlock_t		rules_mod_lock;
635fd30ee7SDenis V. Lunev 
6497c53cacSDenis V. Lunev 	struct sock 		*rtnl;			/* rtnetlink socket */
65134e6375SJohannes Berg 	struct sock		*genl_sock;
66d12d01d6SDenis V. Lunev 
678efa6e93SPavel Emelyanov 	struct netns_core	core;
68852566f5SPavel Emelyanov 	struct netns_mib	mib;
692aaef4e4SDenis V. Lunev 	struct netns_packet	packet;
70a0a53c8bSDenis V. Lunev 	struct netns_unix	unx;
718afd351cSPavel Emelyanov 	struct netns_ipv4	ipv4;
72b0f159dbSDaniel Lezcano #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
73b0f159dbSDaniel Lezcano 	struct netns_ipv6	ipv6;
74b0f159dbSDaniel Lezcano #endif
7567019cc9SPavel Emelyanov #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
7667019cc9SPavel Emelyanov 	struct netns_dccp	dccp;
7767019cc9SPavel Emelyanov #endif
788d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER
798d870052SAlexey Dobriyan 	struct netns_xt		xt;
80dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
81dfdb8d79SAlexey Dobriyan 	struct netns_ct		ct;
82dfdb8d79SAlexey Dobriyan #endif
838d870052SAlexey Dobriyan #endif
84d62ddc21SAlexey Dobriyan #ifdef CONFIG_XFRM
85d62ddc21SAlexey Dobriyan 	struct netns_xfrm	xfrm;
86d62ddc21SAlexey Dobriyan #endif
873d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE
88b333b3d2SJohannes Berg 	struct sk_buff_head	wext_nlevents;
89b333b3d2SJohannes Berg #endif
90dec827d1SPavel Emelyanov 	struct net_generic	*gen;
915f256becSEric W. Biederman };
925f256becSEric W. Biederman 
93225c0a01SDenis V. Lunev 
94c0f39322SDenis V. Lunev #include <linux/seq_file_net.h>
95c0f39322SDenis V. Lunev 
964fabcd71SDaniel Lezcano /* Init's network namespace */
975f256becSEric W. Biederman extern struct net init_net;
98a4aa834aSDenis V. Lunev 
99a4aa834aSDenis V. Lunev #ifdef CONFIG_NET
1004fabcd71SDaniel Lezcano #define INIT_NET_NS(net_ns) .net_ns = &init_net,
1014fabcd71SDaniel Lezcano 
1029dd776b6SEric W. Biederman extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
103225c0a01SDenis V. Lunev 
104225c0a01SDenis V. Lunev #else /* CONFIG_NET */
105225c0a01SDenis V. Lunev 
106225c0a01SDenis V. Lunev #define INIT_NET_NS(net_ns)
107225c0a01SDenis V. Lunev 
1089dd776b6SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
1099dd776b6SEric W. Biederman {
1109dd776b6SEric W. Biederman 	/* There is nothing to copy so this is a noop */
1119dd776b6SEric W. Biederman 	return net_ns;
1129dd776b6SEric W. Biederman }
113225c0a01SDenis V. Lunev #endif /* CONFIG_NET */
114225c0a01SDenis V. Lunev 
115225c0a01SDenis V. Lunev 
116225c0a01SDenis V. Lunev extern struct list_head net_namespace_list;
1179dd776b6SEric W. Biederman 
11830ffee84SJohannes Berg extern struct net *get_net_ns_by_pid(pid_t pid);
11930ffee84SJohannes Berg 
120d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS
1215f256becSEric W. Biederman extern void __put_net(struct net *net);
1225f256becSEric W. Biederman 
1235f256becSEric W. Biederman static inline struct net *get_net(struct net *net)
1245f256becSEric W. Biederman {
1255f256becSEric W. Biederman 	atomic_inc(&net->count);
1265f256becSEric W. Biederman 	return net;
1275f256becSEric W. Biederman }
1285f256becSEric W. Biederman 
129077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net)
130077130c0SEric W. Biederman {
131077130c0SEric W. Biederman 	/* Used when we know struct net exists but we
132077130c0SEric W. Biederman 	 * aren't guaranteed a previous reference count
133077130c0SEric W. Biederman 	 * exists.  If the reference count is zero this
134077130c0SEric W. Biederman 	 * function fails and returns NULL.
135077130c0SEric W. Biederman 	 */
136077130c0SEric W. Biederman 	if (!atomic_inc_not_zero(&net->count))
137077130c0SEric W. Biederman 		net = NULL;
138077130c0SEric W. Biederman 	return net;
139077130c0SEric W. Biederman }
140077130c0SEric W. Biederman 
1415f256becSEric W. Biederman static inline void put_net(struct net *net)
1425f256becSEric W. Biederman {
1435f256becSEric W. Biederman 	if (atomic_dec_and_test(&net->count))
1445f256becSEric W. Biederman 		__put_net(net);
1455f256becSEric W. Biederman }
1465f256becSEric W. Biederman 
147878628fbSYOSHIFUJI Hideaki static inline
148878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
149878628fbSYOSHIFUJI Hideaki {
150878628fbSYOSHIFUJI Hideaki 	return net1 == net2;
151878628fbSYOSHIFUJI Hideaki }
152d4655795SPavel Emelyanov #else
153b9f75f45SEric W. Biederman 
154d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net)
155d4655795SPavel Emelyanov {
156d4655795SPavel Emelyanov 	return net;
157d4655795SPavel Emelyanov }
158d4655795SPavel Emelyanov 
159d4655795SPavel Emelyanov static inline void put_net(struct net *net)
160d4655795SPavel Emelyanov {
161d4655795SPavel Emelyanov }
162d4655795SPavel Emelyanov 
163d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net)
164d4655795SPavel Emelyanov {
165d4655795SPavel Emelyanov 	return net;
166d4655795SPavel Emelyanov }
167878628fbSYOSHIFUJI Hideaki 
168878628fbSYOSHIFUJI Hideaki static inline
169878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
170878628fbSYOSHIFUJI Hideaki {
171878628fbSYOSHIFUJI Hideaki 	return 1;
172878628fbSYOSHIFUJI Hideaki }
173d4655795SPavel Emelyanov #endif
1745f256becSEric W. Biederman 
1755d1e4468SDenis V. Lunev 
1765d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG
1775d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net)
1785d1e4468SDenis V. Lunev {
1795d1e4468SDenis V. Lunev 	if (net)
1805d1e4468SDenis V. Lunev 		atomic_inc(&net->use_count);
1815d1e4468SDenis V. Lunev 	return net;
1825d1e4468SDenis V. Lunev }
1835d1e4468SDenis V. Lunev 
1845d1e4468SDenis V. Lunev static inline void release_net(struct net *net)
1855d1e4468SDenis V. Lunev {
1865d1e4468SDenis V. Lunev 	if (net)
1875d1e4468SDenis V. Lunev 		atomic_dec(&net->use_count);
1885d1e4468SDenis V. Lunev }
1895d1e4468SDenis V. Lunev #else
1905d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net)
1915d1e4468SDenis V. Lunev {
1925d1e4468SDenis V. Lunev 	return net;
1935d1e4468SDenis V. Lunev }
1945d1e4468SDenis V. Lunev 
1955d1e4468SDenis V. Lunev static inline void release_net(struct net *net)
1965d1e4468SDenis V. Lunev {
1975d1e4468SDenis V. Lunev }
1985d1e4468SDenis V. Lunev #endif
1995d1e4468SDenis V. Lunev 
2008f424b5fSEric Dumazet #ifdef CONFIG_NET_NS
2018f424b5fSEric Dumazet 
2028f424b5fSEric Dumazet static inline void write_pnet(struct net **pnet, struct net *net)
2038f424b5fSEric Dumazet {
2048f424b5fSEric Dumazet 	*pnet = net;
2058f424b5fSEric Dumazet }
2068f424b5fSEric Dumazet 
2078f424b5fSEric Dumazet static inline struct net *read_pnet(struct net * const *pnet)
2088f424b5fSEric Dumazet {
2098f424b5fSEric Dumazet 	return *pnet;
2108f424b5fSEric Dumazet }
2118f424b5fSEric Dumazet 
2128f424b5fSEric Dumazet #else
2138f424b5fSEric Dumazet 
2148f424b5fSEric Dumazet #define write_pnet(pnet, net)	do { (void)(net);} while (0)
2158f424b5fSEric Dumazet #define read_pnet(pnet)		(&init_net)
2168f424b5fSEric Dumazet 
2178f424b5fSEric Dumazet #endif
2185d1e4468SDenis V. Lunev 
2195f256becSEric W. Biederman #define for_each_net(VAR)				\
2205f256becSEric W. Biederman 	list_for_each_entry(VAR, &net_namespace_list, list)
2215f256becSEric W. Biederman 
22211a28d37SJohannes Berg #define for_each_net_rcu(VAR)				\
22311a28d37SJohannes Berg 	list_for_each_entry_rcu(VAR, &net_namespace_list, list)
22411a28d37SJohannes Berg 
2254665079cSPavel Emelyanov #ifdef CONFIG_NET_NS
2264665079cSPavel Emelyanov #define __net_init
2274665079cSPavel Emelyanov #define __net_exit
228022cbae6SDenis V. Lunev #define __net_initdata
2294665079cSPavel Emelyanov #else
2304665079cSPavel Emelyanov #define __net_init	__init
2314665079cSPavel Emelyanov #define __net_exit	__exit_refok
232022cbae6SDenis V. Lunev #define __net_initdata	__initdata
2334665079cSPavel Emelyanov #endif
2345f256becSEric W. Biederman 
2355f256becSEric W. Biederman struct pernet_operations {
2365f256becSEric W. Biederman 	struct list_head list;
2375f256becSEric W. Biederman 	int (*init)(struct net *net);
2385f256becSEric W. Biederman 	void (*exit)(struct net *net);
2395f256becSEric W. Biederman };
2405f256becSEric W. Biederman 
24117edde52SEric W. Biederman /*
24217edde52SEric W. Biederman  * Use these carefully.  If you implement a network device and it
24317edde52SEric W. Biederman  * needs per network namespace operations use device pernet operations,
24417edde52SEric W. Biederman  * otherwise use pernet subsys operations.
24517edde52SEric W. Biederman  *
2464edf547bSJohannes Berg  * Network interfaces need to be removed from a dying netns _before_
2474edf547bSJohannes Berg  * subsys notifiers can be called, as most of the network code cleanup
2484edf547bSJohannes Berg  * (which is done from subsys notifiers) runs with the assumption that
2494edf547bSJohannes Berg  * dev_remove_pack has been called so no new packets will arrive during
2504edf547bSJohannes Berg  * and after the cleanup functions have been called.  dev_remove_pack
2514edf547bSJohannes Berg  * is not per namespace so instead the guarantee of no more packets
2524edf547bSJohannes Berg  * arriving in a network namespace is provided by ensuring that all
2534edf547bSJohannes Berg  * network devices and all sockets have left the network namespace
2544edf547bSJohannes Berg  * before the cleanup methods are called.
25517edde52SEric W. Biederman  *
25617edde52SEric W. Biederman  * For the longest time the ipv4 icmp code was registered as a pernet
25717edde52SEric W. Biederman  * device which caused kernel oops, and panics during network
25817edde52SEric W. Biederman  * namespace cleanup.   So please don't get this wrong.
25917edde52SEric W. Biederman  */
2605f256becSEric W. Biederman extern int register_pernet_subsys(struct pernet_operations *);
2615f256becSEric W. Biederman extern void unregister_pernet_subsys(struct pernet_operations *);
262485ac57bSAlexey Dobriyan extern int register_pernet_gen_subsys(int *id, struct pernet_operations *);
263485ac57bSAlexey Dobriyan extern void unregister_pernet_gen_subsys(int id, struct pernet_operations *);
2645f256becSEric W. Biederman extern int register_pernet_device(struct pernet_operations *);
2655f256becSEric W. Biederman extern void unregister_pernet_device(struct pernet_operations *);
266c93cf61fSPavel Emelyanov extern int register_pernet_gen_device(int *id, struct pernet_operations *);
267c93cf61fSPavel Emelyanov extern void unregister_pernet_gen_device(int id, struct pernet_operations *);
2685f256becSEric W. Biederman 
26995bdfccbSEric W. Biederman struct ctl_path;
27095bdfccbSEric W. Biederman struct ctl_table;
27195bdfccbSEric W. Biederman struct ctl_table_header;
272d62c612eSPavel Emelyanov 
27395bdfccbSEric W. Biederman extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
27495bdfccbSEric W. Biederman 	const struct ctl_path *path, struct ctl_table *table);
275d62c612eSPavel Emelyanov extern struct ctl_table_header *register_net_sysctl_rotable(
276d62c612eSPavel Emelyanov 	const struct ctl_path *path, struct ctl_table *table);
27795bdfccbSEric W. Biederman extern void unregister_net_sysctl_table(struct ctl_table_header *header);
27895bdfccbSEric W. Biederman 
2795f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */
280