xref: /linux/include/net/net_namespace.h (revision 8fa5723aa7e053d498336b48448b292fc2e0458b)
1 /*
2  * Operations on the network namespace
3  */
4 #ifndef __NET_NET_NAMESPACE_H
5 #define __NET_NET_NAMESPACE_H
6 
7 #include <asm/atomic.h>
8 #include <linux/workqueue.h>
9 #include <linux/list.h>
10 
11 #include <net/netns/core.h>
12 #include <net/netns/mib.h>
13 #include <net/netns/unix.h>
14 #include <net/netns/packet.h>
15 #include <net/netns/ipv4.h>
16 #include <net/netns/ipv6.h>
17 #include <net/netns/dccp.h>
18 #include <net/netns/x_tables.h>
19 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
20 #include <net/netns/conntrack.h>
21 #endif
22 
23 struct proc_dir_entry;
24 struct net_device;
25 struct sock;
26 struct ctl_table_header;
27 struct net_generic;
28 
29 struct net {
30 	atomic_t		count;		/* To decided when the network
31 						 *  namespace should be freed.
32 						 */
33 #ifdef NETNS_REFCNT_DEBUG
34 	atomic_t		use_count;	/* To track references we
35 						 * destroy on demand
36 						 */
37 #endif
38 	struct list_head	list;		/* list of network namespaces */
39 	struct work_struct	work;		/* work struct for freeing */
40 
41 	struct proc_dir_entry 	*proc_net;
42 	struct proc_dir_entry 	*proc_net_stat;
43 
44 #ifdef CONFIG_SYSCTL
45 	struct ctl_table_set	sysctls;
46 #endif
47 
48 	struct net_device       *loopback_dev;          /* The loopback */
49 
50 	struct list_head 	dev_base_head;
51 	struct hlist_head 	*dev_name_head;
52 	struct hlist_head	*dev_index_head;
53 
54 	/* core fib_rules */
55 	struct list_head	rules_ops;
56 	spinlock_t		rules_mod_lock;
57 
58 	struct sock 		*rtnl;			/* rtnetlink socket */
59 
60 	struct netns_core	core;
61 	struct netns_mib	mib;
62 	struct netns_packet	packet;
63 	struct netns_unix	unx;
64 	struct netns_ipv4	ipv4;
65 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
66 	struct netns_ipv6	ipv6;
67 #endif
68 #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
69 	struct netns_dccp	dccp;
70 #endif
71 #ifdef CONFIG_NETFILTER
72 	struct netns_xt		xt;
73 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
74 	struct netns_ct		ct;
75 #endif
76 #endif
77 	struct net_generic	*gen;
78 };
79 
80 
81 #include <linux/seq_file_net.h>
82 
83 /* Init's network namespace */
84 extern struct net init_net;
85 
86 #ifdef CONFIG_NET
87 #define INIT_NET_NS(net_ns) .net_ns = &init_net,
88 
89 extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
90 
91 #else /* CONFIG_NET */
92 
93 #define INIT_NET_NS(net_ns)
94 
95 static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
96 {
97 	/* There is nothing to copy so this is a noop */
98 	return net_ns;
99 }
100 #endif /* CONFIG_NET */
101 
102 
103 extern struct list_head net_namespace_list;
104 
105 #ifdef CONFIG_NET_NS
106 extern void __put_net(struct net *net);
107 
108 static inline int net_alive(struct net *net)
109 {
110 	return net && atomic_read(&net->count);
111 }
112 
113 static inline struct net *get_net(struct net *net)
114 {
115 	atomic_inc(&net->count);
116 	return net;
117 }
118 
119 static inline struct net *maybe_get_net(struct net *net)
120 {
121 	/* Used when we know struct net exists but we
122 	 * aren't guaranteed a previous reference count
123 	 * exists.  If the reference count is zero this
124 	 * function fails and returns NULL.
125 	 */
126 	if (!atomic_inc_not_zero(&net->count))
127 		net = NULL;
128 	return net;
129 }
130 
131 static inline void put_net(struct net *net)
132 {
133 	if (atomic_dec_and_test(&net->count))
134 		__put_net(net);
135 }
136 
137 static inline
138 int net_eq(const struct net *net1, const struct net *net2)
139 {
140 	return net1 == net2;
141 }
142 #else
143 
144 static inline int net_alive(struct net *net)
145 {
146 	return 1;
147 }
148 
149 static inline struct net *get_net(struct net *net)
150 {
151 	return net;
152 }
153 
154 static inline void put_net(struct net *net)
155 {
156 }
157 
158 static inline struct net *maybe_get_net(struct net *net)
159 {
160 	return net;
161 }
162 
163 static inline
164 int net_eq(const struct net *net1, const struct net *net2)
165 {
166 	return 1;
167 }
168 #endif
169 
170 
171 #ifdef NETNS_REFCNT_DEBUG
172 static inline struct net *hold_net(struct net *net)
173 {
174 	if (net)
175 		atomic_inc(&net->use_count);
176 	return net;
177 }
178 
179 static inline void release_net(struct net *net)
180 {
181 	if (net)
182 		atomic_dec(&net->use_count);
183 }
184 #else
185 static inline struct net *hold_net(struct net *net)
186 {
187 	return net;
188 }
189 
190 static inline void release_net(struct net *net)
191 {
192 }
193 #endif
194 
195 
196 #define for_each_net(VAR)				\
197 	list_for_each_entry(VAR, &net_namespace_list, list)
198 
199 #ifdef CONFIG_NET_NS
200 #define __net_init
201 #define __net_exit
202 #define __net_initdata
203 #else
204 #define __net_init	__init
205 #define __net_exit	__exit_refok
206 #define __net_initdata	__initdata
207 #endif
208 
209 struct pernet_operations {
210 	struct list_head list;
211 	int (*init)(struct net *net);
212 	void (*exit)(struct net *net);
213 };
214 
215 extern int register_pernet_subsys(struct pernet_operations *);
216 extern void unregister_pernet_subsys(struct pernet_operations *);
217 extern int register_pernet_gen_subsys(int *id, struct pernet_operations *);
218 extern void unregister_pernet_gen_subsys(int id, struct pernet_operations *);
219 extern int register_pernet_device(struct pernet_operations *);
220 extern void unregister_pernet_device(struct pernet_operations *);
221 extern int register_pernet_gen_device(int *id, struct pernet_operations *);
222 extern void unregister_pernet_gen_device(int id, struct pernet_operations *);
223 
224 struct ctl_path;
225 struct ctl_table;
226 struct ctl_table_header;
227 
228 extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
229 	const struct ctl_path *path, struct ctl_table *table);
230 extern struct ctl_table_header *register_net_sysctl_rotable(
231 	const struct ctl_path *path, struct ctl_table *table);
232 extern void unregister_net_sysctl_table(struct ctl_table_header *header);
233 
234 #endif /* __NET_NET_NAMESPACE_H */
235