xref: /openbmc/linux/include/net/net_namespace.h (revision 60063497)
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>
1867019cc9SPavel Emelyanov #include <net/netns/dccp.h>
198d870052SAlexey Dobriyan #include <net/netns/x_tables.h>
20dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
21dfdb8d79SAlexey Dobriyan #include <net/netns/conntrack.h>
22dfdb8d79SAlexey Dobriyan #endif
23d62ddc21SAlexey Dobriyan #include <net/netns/xfrm.h>
24a0a53c8bSDenis V. Lunev 
25457c4cbcSEric W. Biederman struct proc_dir_entry;
262774c7abSEric W. Biederman struct net_device;
2797c53cacSDenis V. Lunev struct sock;
281597fbc0SPavel Emelyanov struct ctl_table_header;
29dec827d1SPavel Emelyanov struct net_generic;
30134e6375SJohannes Berg struct sock;
312553d064SJulian Anastasov struct netns_ipvs;
321597fbc0SPavel Emelyanov 
337c28bd0bSEric Dumazet 
347c28bd0bSEric Dumazet #define NETDEV_HASHBITS    8
357c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
367c28bd0bSEric Dumazet 
375f256becSEric W. Biederman struct net {
38a685e089SAl Viro 	atomic_t		passive;	/* To decided when the network
395f256becSEric W. Biederman 						 * namespace should be freed.
405f256becSEric W. Biederman 						 */
41a685e089SAl Viro 	atomic_t		count;		/* To decided when the network
42a685e089SAl Viro 						 *  namespace should be shut down.
43a685e089SAl Viro 						 */
445d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG
455f256becSEric W. Biederman 	atomic_t		use_count;	/* To track references we
465f256becSEric W. Biederman 						 * destroy on demand
475f256becSEric W. Biederman 						 */
485d1e4468SDenis V. Lunev #endif
498e602ce2SEric Dumazet 	spinlock_t		rules_mod_lock;
508e602ce2SEric Dumazet 
515f256becSEric W. Biederman 	struct list_head	list;		/* list of network namespaces */
522b035b39SEric W. Biederman 	struct list_head	cleanup_list;	/* namespaces on death row */
5372ad937aSEric W. Biederman 	struct list_head	exit_list;	/* Use only net_mutex */
54457c4cbcSEric W. Biederman 
55457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net;
56457c4cbcSEric W. Biederman 	struct proc_dir_entry 	*proc_net_stat;
57881d966bSEric W. Biederman 
5873455092SAl Viro #ifdef CONFIG_SYSCTL
5973455092SAl Viro 	struct ctl_table_set	sysctls;
6073455092SAl Viro #endif
6195bdfccbSEric W. Biederman 
628e602ce2SEric Dumazet 	struct sock 		*rtnl;			/* rtnetlink socket */
638e602ce2SEric Dumazet 	struct sock		*genl_sock;
642774c7abSEric W. Biederman 
65881d966bSEric W. Biederman 	struct list_head 	dev_base_head;
66881d966bSEric W. Biederman 	struct hlist_head 	*dev_name_head;
67881d966bSEric W. Biederman 	struct hlist_head	*dev_index_head;
684e985adaSThomas Graf 	unsigned int		dev_base_seq;	/* protected by rtnl_mutex */
6997c53cacSDenis V. Lunev 
705fd30ee7SDenis V. Lunev 	/* core fib_rules */
715fd30ee7SDenis V. Lunev 	struct list_head	rules_ops;
725fd30ee7SDenis V. Lunev 
73d12d01d6SDenis V. Lunev 
748e602ce2SEric Dumazet 	struct net_device       *loopback_dev;          /* The loopback */
758efa6e93SPavel Emelyanov 	struct netns_core	core;
76852566f5SPavel Emelyanov 	struct netns_mib	mib;
772aaef4e4SDenis V. Lunev 	struct netns_packet	packet;
78a0a53c8bSDenis V. Lunev 	struct netns_unix	unx;
798afd351cSPavel Emelyanov 	struct netns_ipv4	ipv4;
80b0f159dbSDaniel Lezcano #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
81b0f159dbSDaniel Lezcano 	struct netns_ipv6	ipv6;
82b0f159dbSDaniel Lezcano #endif
8367019cc9SPavel Emelyanov #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
8467019cc9SPavel Emelyanov 	struct netns_dccp	dccp;
8567019cc9SPavel Emelyanov #endif
868d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER
878d870052SAlexey Dobriyan 	struct netns_xt		xt;
88dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
89dfdb8d79SAlexey Dobriyan 	struct netns_ct		ct;
90dfdb8d79SAlexey Dobriyan #endif
91cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl;
92cd8c20b6SAlexey Dobriyan 	struct sock		*nfnl_stash;
938d870052SAlexey Dobriyan #endif
943d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE
95b333b3d2SJohannes Berg 	struct sk_buff_head	wext_nlevents;
96b333b3d2SJohannes Berg #endif
971c87733dSEric Dumazet 	struct net_generic __rcu	*gen;
988e602ce2SEric Dumazet 
998e602ce2SEric Dumazet 	/* Note : following structs are cache line aligned */
1008e602ce2SEric Dumazet #ifdef CONFIG_XFRM
1018e602ce2SEric Dumazet 	struct netns_xfrm	xfrm;
1028e602ce2SEric Dumazet #endif
10361b1ab45SHans Schillstrom 	struct netns_ipvs	*ipvs;
1045f256becSEric W. Biederman };
1055f256becSEric W. Biederman 
106225c0a01SDenis V. Lunev 
107c0f39322SDenis V. Lunev #include <linux/seq_file_net.h>
108c0f39322SDenis V. Lunev 
1094fabcd71SDaniel Lezcano /* Init's network namespace */
1105f256becSEric W. Biederman extern struct net init_net;
111a4aa834aSDenis V. Lunev 
112a4aa834aSDenis V. Lunev #ifdef CONFIG_NET
1139dd776b6SEric W. Biederman extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
114225c0a01SDenis V. Lunev 
115225c0a01SDenis V. Lunev #else /* CONFIG_NET */
1169dd776b6SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
1179dd776b6SEric W. Biederman {
1189dd776b6SEric W. Biederman 	/* There is nothing to copy so this is a noop */
1199dd776b6SEric W. Biederman 	return net_ns;
1209dd776b6SEric W. Biederman }
121225c0a01SDenis V. Lunev #endif /* CONFIG_NET */
122225c0a01SDenis V. Lunev 
123225c0a01SDenis V. Lunev 
124225c0a01SDenis V. Lunev extern struct list_head net_namespace_list;
1259dd776b6SEric W. Biederman 
12630ffee84SJohannes Berg extern struct net *get_net_ns_by_pid(pid_t pid);
127f0630529SEric W. Biederman extern struct net *get_net_ns_by_fd(int pid);
12830ffee84SJohannes Berg 
129d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS
1305f256becSEric W. Biederman extern void __put_net(struct net *net);
1315f256becSEric W. Biederman 
1325f256becSEric W. Biederman static inline struct net *get_net(struct net *net)
1335f256becSEric W. Biederman {
1345f256becSEric W. Biederman 	atomic_inc(&net->count);
1355f256becSEric W. Biederman 	return net;
1365f256becSEric W. Biederman }
1375f256becSEric W. Biederman 
138077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net)
139077130c0SEric W. Biederman {
140077130c0SEric W. Biederman 	/* Used when we know struct net exists but we
141077130c0SEric W. Biederman 	 * aren't guaranteed a previous reference count
142077130c0SEric W. Biederman 	 * exists.  If the reference count is zero this
143077130c0SEric W. Biederman 	 * function fails and returns NULL.
144077130c0SEric W. Biederman 	 */
145077130c0SEric W. Biederman 	if (!atomic_inc_not_zero(&net->count))
146077130c0SEric W. Biederman 		net = NULL;
147077130c0SEric W. Biederman 	return net;
148077130c0SEric W. Biederman }
149077130c0SEric W. Biederman 
1505f256becSEric W. Biederman static inline void put_net(struct net *net)
1515f256becSEric W. Biederman {
1525f256becSEric W. Biederman 	if (atomic_dec_and_test(&net->count))
1535f256becSEric W. Biederman 		__put_net(net);
1545f256becSEric W. Biederman }
1555f256becSEric W. Biederman 
156878628fbSYOSHIFUJI Hideaki static inline
157878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
158878628fbSYOSHIFUJI Hideaki {
159878628fbSYOSHIFUJI Hideaki 	return net1 == net2;
160878628fbSYOSHIFUJI Hideaki }
161a685e089SAl Viro 
162a685e089SAl Viro extern void net_drop_ns(void *);
163a685e089SAl Viro 
164d4655795SPavel Emelyanov #else
165b9f75f45SEric W. Biederman 
166d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net)
167d4655795SPavel Emelyanov {
168d4655795SPavel Emelyanov 	return net;
169d4655795SPavel Emelyanov }
170d4655795SPavel Emelyanov 
171d4655795SPavel Emelyanov static inline void put_net(struct net *net)
172d4655795SPavel Emelyanov {
173d4655795SPavel Emelyanov }
174d4655795SPavel Emelyanov 
175d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net)
176d4655795SPavel Emelyanov {
177d4655795SPavel Emelyanov 	return net;
178d4655795SPavel Emelyanov }
179878628fbSYOSHIFUJI Hideaki 
180878628fbSYOSHIFUJI Hideaki static inline
181878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2)
182878628fbSYOSHIFUJI Hideaki {
183878628fbSYOSHIFUJI Hideaki 	return 1;
184878628fbSYOSHIFUJI Hideaki }
185a685e089SAl Viro 
186a685e089SAl Viro #define net_drop_ns NULL
187d4655795SPavel Emelyanov #endif
1885f256becSEric W. Biederman 
1895d1e4468SDenis V. Lunev 
1905d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG
1915d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net)
1925d1e4468SDenis V. Lunev {
1935d1e4468SDenis V. Lunev 	if (net)
1945d1e4468SDenis V. Lunev 		atomic_inc(&net->use_count);
1955d1e4468SDenis V. Lunev 	return net;
1965d1e4468SDenis V. Lunev }
1975d1e4468SDenis V. Lunev 
1985d1e4468SDenis V. Lunev static inline void release_net(struct net *net)
1995d1e4468SDenis V. Lunev {
2005d1e4468SDenis V. Lunev 	if (net)
2015d1e4468SDenis V. Lunev 		atomic_dec(&net->use_count);
2025d1e4468SDenis V. Lunev }
2035d1e4468SDenis V. Lunev #else
2045d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net)
2055d1e4468SDenis V. Lunev {
2065d1e4468SDenis V. Lunev 	return net;
2075d1e4468SDenis V. Lunev }
2085d1e4468SDenis V. Lunev 
2095d1e4468SDenis V. Lunev static inline void release_net(struct net *net)
2105d1e4468SDenis V. Lunev {
2115d1e4468SDenis V. Lunev }
2125d1e4468SDenis V. Lunev #endif
2135d1e4468SDenis V. Lunev 
2148f424b5fSEric Dumazet #ifdef CONFIG_NET_NS
2158f424b5fSEric Dumazet 
2168f424b5fSEric Dumazet static inline void write_pnet(struct net **pnet, struct net *net)
2178f424b5fSEric Dumazet {
2188f424b5fSEric Dumazet 	*pnet = net;
2198f424b5fSEric Dumazet }
2208f424b5fSEric Dumazet 
2218f424b5fSEric Dumazet static inline struct net *read_pnet(struct net * const *pnet)
2228f424b5fSEric Dumazet {
2238f424b5fSEric Dumazet 	return *pnet;
2248f424b5fSEric Dumazet }
2258f424b5fSEric Dumazet 
2268f424b5fSEric Dumazet #else
2278f424b5fSEric Dumazet 
2288f424b5fSEric Dumazet #define write_pnet(pnet, net)	do { (void)(net);} while (0)
2298f424b5fSEric Dumazet #define read_pnet(pnet)		(&init_net)
2308f424b5fSEric Dumazet 
2318f424b5fSEric Dumazet #endif
2325d1e4468SDenis V. Lunev 
2335f256becSEric W. Biederman #define for_each_net(VAR)				\
2345f256becSEric W. Biederman 	list_for_each_entry(VAR, &net_namespace_list, list)
2355f256becSEric W. Biederman 
23611a28d37SJohannes Berg #define for_each_net_rcu(VAR)				\
23711a28d37SJohannes Berg 	list_for_each_entry_rcu(VAR, &net_namespace_list, list)
23811a28d37SJohannes Berg 
2394665079cSPavel Emelyanov #ifdef CONFIG_NET_NS
2404665079cSPavel Emelyanov #define __net_init
2414665079cSPavel Emelyanov #define __net_exit
242022cbae6SDenis V. Lunev #define __net_initdata
2434665079cSPavel Emelyanov #else
2444665079cSPavel Emelyanov #define __net_init	__init
2454665079cSPavel Emelyanov #define __net_exit	__exit_refok
246022cbae6SDenis V. Lunev #define __net_initdata	__initdata
2474665079cSPavel Emelyanov #endif
2485f256becSEric W. Biederman 
2495f256becSEric W. Biederman struct pernet_operations {
2505f256becSEric W. Biederman 	struct list_head list;
2515f256becSEric W. Biederman 	int (*init)(struct net *net);
2525f256becSEric W. Biederman 	void (*exit)(struct net *net);
25372ad937aSEric W. Biederman 	void (*exit_batch)(struct list_head *net_exit_list);
254f875bae0SEric W. Biederman 	int *id;
255f875bae0SEric W. Biederman 	size_t size;
2565f256becSEric W. Biederman };
2575f256becSEric W. Biederman 
25817edde52SEric W. Biederman /*
25917edde52SEric W. Biederman  * Use these carefully.  If you implement a network device and it
26017edde52SEric W. Biederman  * needs per network namespace operations use device pernet operations,
26117edde52SEric W. Biederman  * otherwise use pernet subsys operations.
26217edde52SEric W. Biederman  *
2634edf547bSJohannes Berg  * Network interfaces need to be removed from a dying netns _before_
2644edf547bSJohannes Berg  * subsys notifiers can be called, as most of the network code cleanup
2654edf547bSJohannes Berg  * (which is done from subsys notifiers) runs with the assumption that
2664edf547bSJohannes Berg  * dev_remove_pack has been called so no new packets will arrive during
2674edf547bSJohannes Berg  * and after the cleanup functions have been called.  dev_remove_pack
2684edf547bSJohannes Berg  * is not per namespace so instead the guarantee of no more packets
2694edf547bSJohannes Berg  * arriving in a network namespace is provided by ensuring that all
2704edf547bSJohannes Berg  * network devices and all sockets have left the network namespace
2714edf547bSJohannes Berg  * before the cleanup methods are called.
27217edde52SEric W. Biederman  *
27317edde52SEric W. Biederman  * For the longest time the ipv4 icmp code was registered as a pernet
27417edde52SEric W. Biederman  * device which caused kernel oops, and panics during network
27517edde52SEric W. Biederman  * namespace cleanup.   So please don't get this wrong.
27617edde52SEric W. Biederman  */
2775f256becSEric W. Biederman extern int register_pernet_subsys(struct pernet_operations *);
2785f256becSEric W. Biederman extern void unregister_pernet_subsys(struct pernet_operations *);
2795f256becSEric W. Biederman extern int register_pernet_device(struct pernet_operations *);
2805f256becSEric W. Biederman extern void unregister_pernet_device(struct pernet_operations *);
281f875bae0SEric W. Biederman 
28295bdfccbSEric W. Biederman struct ctl_path;
28395bdfccbSEric W. Biederman struct ctl_table;
28495bdfccbSEric W. Biederman struct ctl_table_header;
285d62c612eSPavel Emelyanov 
28695bdfccbSEric W. Biederman extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
28795bdfccbSEric W. Biederman 	const struct ctl_path *path, struct ctl_table *table);
288d62c612eSPavel Emelyanov extern struct ctl_table_header *register_net_sysctl_rotable(
289d62c612eSPavel Emelyanov 	const struct ctl_path *path, struct ctl_table *table);
29095bdfccbSEric W. Biederman extern void unregister_net_sysctl_table(struct ctl_table_header *header);
29195bdfccbSEric W. Biederman 
2925f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */
293