Commit | Line | Data |
---|---|---|
5f256bec EB |
1 | /* |
2 | * Operations on the network namespace | |
3 | */ | |
4 | #ifndef __NET_NET_NAMESPACE_H | |
5 | #define __NET_NET_NAMESPACE_H | |
6 | ||
60063497 | 7 | #include <linux/atomic.h> |
5f256bec EB |
8 | #include <linux/workqueue.h> |
9 | #include <linux/list.h> | |
bee95250 | 10 | #include <linux/sysctl.h> |
5f256bec | 11 | |
6a662719 | 12 | #include <net/flow.h> |
8efa6e93 | 13 | #include <net/netns/core.h> |
852566f5 | 14 | #include <net/netns/mib.h> |
a0a53c8b | 15 | #include <net/netns/unix.h> |
2aaef4e4 | 16 | #include <net/netns/packet.h> |
8afd351c | 17 | #include <net/netns/ipv4.h> |
b0f159db | 18 | #include <net/netns/ipv6.h> |
633fc86f | 19 | #include <net/netns/ieee802154_6lowpan.h> |
4db67e80 | 20 | #include <net/netns/sctp.h> |
67019cc9 | 21 | #include <net/netns/dccp.h> |
f3c1a44a | 22 | #include <net/netns/netfilter.h> |
8d870052 | 23 | #include <net/netns/x_tables.h> |
dfdb8d79 AD |
24 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
25 | #include <net/netns/conntrack.h> | |
26 | #endif | |
99633ab2 | 27 | #include <net/netns/nftables.h> |
d62ddc21 | 28 | #include <net/netns/xfrm.h> |
435d5f4b | 29 | #include <linux/ns_common.h> |
a0a53c8b | 30 | |
038e7332 | 31 | struct user_namespace; |
457c4cbc | 32 | struct proc_dir_entry; |
2774c7ab | 33 | struct net_device; |
97c53cac | 34 | struct sock; |
1597fbc0 | 35 | struct ctl_table_header; |
dec827d1 | 36 | struct net_generic; |
134e6375 | 37 | struct sock; |
2553d064 | 38 | struct netns_ipvs; |
1597fbc0 | 39 | |
7c28bd0b ED |
40 | |
41 | #define NETDEV_HASHBITS 8 | |
42 | #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS) | |
43 | ||
5f256bec | 44 | struct net { |
a685e089 AV |
45 | atomic_t passive; /* To decided when the network |
46 | * namespace should be freed. | |
47 | */ | |
5f256bec | 48 | atomic_t count; /* To decided when the network |
a685e089 | 49 | * namespace should be shut down. |
5f256bec | 50 | */ |
5d1e4468 | 51 | #ifdef NETNS_REFCNT_DEBUG |
5f256bec EB |
52 | atomic_t use_count; /* To track references we |
53 | * destroy on demand | |
54 | */ | |
5d1e4468 | 55 | #endif |
8e602ce2 ED |
56 | spinlock_t rules_mod_lock; |
57 | ||
5f256bec | 58 | struct list_head list; /* list of network namespaces */ |
2b035b39 | 59 | struct list_head cleanup_list; /* namespaces on death row */ |
72ad937a | 60 | struct list_head exit_list; /* Use only net_mutex */ |
457c4cbc | 61 | |
038e7332 EB |
62 | struct user_namespace *user_ns; /* Owning user namespace */ |
63 | ||
435d5f4b | 64 | struct ns_common ns; |
98f842e6 | 65 | |
457c4cbc EB |
66 | struct proc_dir_entry *proc_net; |
67 | struct proc_dir_entry *proc_net_stat; | |
881d966b | 68 | |
73455092 AV |
69 | #ifdef CONFIG_SYSCTL |
70 | struct ctl_table_set sysctls; | |
71 | #endif | |
95bdfccb | 72 | |
8e602ce2 ED |
73 | struct sock *rtnl; /* rtnetlink socket */ |
74 | struct sock *genl_sock; | |
2774c7ab | 75 | |
881d966b EB |
76 | struct list_head dev_base_head; |
77 | struct hlist_head *dev_name_head; | |
78 | struct hlist_head *dev_index_head; | |
4e985ada | 79 | unsigned int dev_base_seq; /* protected by rtnl_mutex */ |
aa79e66e | 80 | int ifindex; |
50624c93 | 81 | unsigned int dev_unreg_count; |
97c53cac | 82 | |
5fd30ee7 DL |
83 | /* core fib_rules */ |
84 | struct list_head rules_ops; | |
5fd30ee7 | 85 | |
d12d01d6 | 86 | |
8e602ce2 | 87 | struct net_device *loopback_dev; /* The loopback */ |
8efa6e93 | 88 | struct netns_core core; |
852566f5 | 89 | struct netns_mib mib; |
2aaef4e4 | 90 | struct netns_packet packet; |
a0a53c8b | 91 | struct netns_unix unx; |
8afd351c | 92 | struct netns_ipv4 ipv4; |
dfd56b8b | 93 | #if IS_ENABLED(CONFIG_IPV6) |
b0f159db DL |
94 | struct netns_ipv6 ipv6; |
95 | #endif | |
633fc86f AA |
96 | #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) |
97 | struct netns_ieee802154_lowpan ieee802154_lowpan; | |
98 | #endif | |
4db67e80 EB |
99 | #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE) |
100 | struct netns_sctp sctp; | |
101 | #endif | |
67019cc9 PE |
102 | #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE) |
103 | struct netns_dccp dccp; | |
104 | #endif | |
8d870052 | 105 | #ifdef CONFIG_NETFILTER |
f3c1a44a | 106 | struct netns_nf nf; |
8d870052 | 107 | struct netns_xt xt; |
dfdb8d79 AD |
108 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
109 | struct netns_ct ct; | |
c038a767 | 110 | #endif |
99633ab2 PNA |
111 | #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE) |
112 | struct netns_nftables nft; | |
113 | #endif | |
c038a767 AW |
114 | #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) |
115 | struct netns_nf_frag nf_frag; | |
dfdb8d79 | 116 | #endif |
cd8c20b6 AD |
117 | struct sock *nfnl; |
118 | struct sock *nfnl_stash; | |
d62ddc21 | 119 | #endif |
3d23e349 | 120 | #ifdef CONFIG_WEXT_CORE |
b333b3d2 | 121 | struct sk_buff_head wext_nlevents; |
8d870052 | 122 | #endif |
1c87733d | 123 | struct net_generic __rcu *gen; |
8e602ce2 ED |
124 | |
125 | /* Note : following structs are cache line aligned */ | |
126 | #ifdef CONFIG_XFRM | |
127 | struct netns_xfrm xfrm; | |
128 | #endif | |
8b4d14d8 | 129 | #if IS_ENABLED(CONFIG_IP_VS) |
61b1ab45 | 130 | struct netns_ipvs *ipvs; |
8b4d14d8 | 131 | #endif |
51d7cccf | 132 | struct sock *diag_nlsk; |
5aad1de5 | 133 | atomic_t fnhe_genid; |
5f256bec EB |
134 | }; |
135 | ||
c0f39322 DL |
136 | #include <linux/seq_file_net.h> |
137 | ||
4fabcd71 | 138 | /* Init's network namespace */ |
5f256bec | 139 | extern struct net init_net; |
a4aa834a | 140 | |
d727abcb | 141 | #ifdef CONFIG_NET_NS |
e67e16ea JP |
142 | struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns, |
143 | struct net *old_net); | |
225c0a01 | 144 | |
d727abcb EB |
145 | #else /* CONFIG_NET_NS */ |
146 | #include <linux/sched.h> | |
147 | #include <linux/nsproxy.h> | |
038e7332 EB |
148 | static inline struct net *copy_net_ns(unsigned long flags, |
149 | struct user_namespace *user_ns, struct net *old_net) | |
9dd776b6 | 150 | { |
d727abcb EB |
151 | if (flags & CLONE_NEWNET) |
152 | return ERR_PTR(-EINVAL); | |
153 | return old_net; | |
9dd776b6 | 154 | } |
d727abcb | 155 | #endif /* CONFIG_NET_NS */ |
225c0a01 DL |
156 | |
157 | ||
158 | extern struct list_head net_namespace_list; | |
9dd776b6 | 159 | |
e67e16ea JP |
160 | struct net *get_net_ns_by_pid(pid_t pid); |
161 | struct net *get_net_ns_by_fd(int pid); | |
30ffee84 | 162 | |
535d3ae9 RK |
163 | #ifdef CONFIG_SYSCTL |
164 | void ipx_register_sysctl(void); | |
165 | void ipx_unregister_sysctl(void); | |
166 | #else | |
167 | #define ipx_register_sysctl() | |
168 | #define ipx_unregister_sysctl() | |
169 | #endif | |
170 | ||
d4655795 | 171 | #ifdef CONFIG_NET_NS |
e67e16ea | 172 | void __put_net(struct net *net); |
5f256bec EB |
173 | |
174 | static inline struct net *get_net(struct net *net) | |
175 | { | |
176 | atomic_inc(&net->count); | |
177 | return net; | |
178 | } | |
179 | ||
077130c0 EB |
180 | static inline struct net *maybe_get_net(struct net *net) |
181 | { | |
182 | /* Used when we know struct net exists but we | |
183 | * aren't guaranteed a previous reference count | |
184 | * exists. If the reference count is zero this | |
185 | * function fails and returns NULL. | |
186 | */ | |
187 | if (!atomic_inc_not_zero(&net->count)) | |
188 | net = NULL; | |
189 | return net; | |
190 | } | |
191 | ||
5f256bec EB |
192 | static inline void put_net(struct net *net) |
193 | { | |
194 | if (atomic_dec_and_test(&net->count)) | |
195 | __put_net(net); | |
196 | } | |
197 | ||
878628fb YH |
198 | static inline |
199 | int net_eq(const struct net *net1, const struct net *net2) | |
200 | { | |
201 | return net1 == net2; | |
202 | } | |
a685e089 | 203 | |
e67e16ea | 204 | void net_drop_ns(void *); |
a685e089 | 205 | |
d4655795 | 206 | #else |
b9f75f45 | 207 | |
d4655795 PE |
208 | static inline struct net *get_net(struct net *net) |
209 | { | |
210 | return net; | |
211 | } | |
212 | ||
213 | static inline void put_net(struct net *net) | |
214 | { | |
215 | } | |
216 | ||
5d1e4468 DL |
217 | static inline struct net *maybe_get_net(struct net *net) |
218 | { | |
219 | return net; | |
220 | } | |
221 | ||
222 | static inline | |
223 | int net_eq(const struct net *net1, const struct net *net2) | |
224 | { | |
225 | return 1; | |
226 | } | |
a685e089 AV |
227 | |
228 | #define net_drop_ns NULL | |
5d1e4468 DL |
229 | #endif |
230 | ||
231 | ||
232 | #ifdef NETNS_REFCNT_DEBUG | |
d4655795 PE |
233 | static inline struct net *hold_net(struct net *net) |
234 | { | |
5d1e4468 DL |
235 | if (net) |
236 | atomic_inc(&net->use_count); | |
d4655795 PE |
237 | return net; |
238 | } | |
239 | ||
240 | static inline void release_net(struct net *net) | |
241 | { | |
5d1e4468 DL |
242 | if (net) |
243 | atomic_dec(&net->use_count); | |
d4655795 | 244 | } |
5d1e4468 DL |
245 | #else |
246 | static inline struct net *hold_net(struct net *net) | |
d4655795 PE |
247 | { |
248 | return net; | |
249 | } | |
878628fb | 250 | |
5d1e4468 | 251 | static inline void release_net(struct net *net) |
878628fb | 252 | { |
878628fb | 253 | } |
d4655795 | 254 | #endif |
5f256bec | 255 | |
8f424b5f ED |
256 | #ifdef CONFIG_NET_NS |
257 | ||
258 | static inline void write_pnet(struct net **pnet, struct net *net) | |
259 | { | |
260 | *pnet = net; | |
261 | } | |
262 | ||
263 | static inline struct net *read_pnet(struct net * const *pnet) | |
264 | { | |
265 | return *pnet; | |
266 | } | |
267 | ||
268 | #else | |
269 | ||
270 | #define write_pnet(pnet, net) do { (void)(net);} while (0) | |
271 | #define read_pnet(pnet) (&init_net) | |
272 | ||
273 | #endif | |
5d1e4468 | 274 | |
5f256bec EB |
275 | #define for_each_net(VAR) \ |
276 | list_for_each_entry(VAR, &net_namespace_list, list) | |
277 | ||
11a28d37 JB |
278 | #define for_each_net_rcu(VAR) \ |
279 | list_for_each_entry_rcu(VAR, &net_namespace_list, list) | |
280 | ||
4665079c PE |
281 | #ifdef CONFIG_NET_NS |
282 | #define __net_init | |
283 | #define __net_exit | |
022cbae6 | 284 | #define __net_initdata |
04a6f82c | 285 | #define __net_initconst |
4665079c PE |
286 | #else |
287 | #define __net_init __init | |
288 | #define __net_exit __exit_refok | |
022cbae6 | 289 | #define __net_initdata __initdata |
04a6f82c | 290 | #define __net_initconst __initconst |
4665079c | 291 | #endif |
5f256bec EB |
292 | |
293 | struct pernet_operations { | |
294 | struct list_head list; | |
295 | int (*init)(struct net *net); | |
296 | void (*exit)(struct net *net); | |
72ad937a | 297 | void (*exit_batch)(struct list_head *net_exit_list); |
f875bae0 EB |
298 | int *id; |
299 | size_t size; | |
5f256bec EB |
300 | }; |
301 | ||
17edde52 EB |
302 | /* |
303 | * Use these carefully. If you implement a network device and it | |
304 | * needs per network namespace operations use device pernet operations, | |
305 | * otherwise use pernet subsys operations. | |
306 | * | |
4edf547b JB |
307 | * Network interfaces need to be removed from a dying netns _before_ |
308 | * subsys notifiers can be called, as most of the network code cleanup | |
309 | * (which is done from subsys notifiers) runs with the assumption that | |
310 | * dev_remove_pack has been called so no new packets will arrive during | |
311 | * and after the cleanup functions have been called. dev_remove_pack | |
312 | * is not per namespace so instead the guarantee of no more packets | |
313 | * arriving in a network namespace is provided by ensuring that all | |
314 | * network devices and all sockets have left the network namespace | |
315 | * before the cleanup methods are called. | |
17edde52 EB |
316 | * |
317 | * For the longest time the ipv4 icmp code was registered as a pernet | |
318 | * device which caused kernel oops, and panics during network | |
319 | * namespace cleanup. So please don't get this wrong. | |
320 | */ | |
e67e16ea JP |
321 | int register_pernet_subsys(struct pernet_operations *); |
322 | void unregister_pernet_subsys(struct pernet_operations *); | |
323 | int register_pernet_device(struct pernet_operations *); | |
324 | void unregister_pernet_device(struct pernet_operations *); | |
f875bae0 | 325 | |
95bdfccb EB |
326 | struct ctl_table; |
327 | struct ctl_table_header; | |
d62c612e | 328 | |
2ca794e5 | 329 | #ifdef CONFIG_SYSCTL |
e67e16ea JP |
330 | int net_sysctl_init(void); |
331 | struct ctl_table_header *register_net_sysctl(struct net *net, const char *path, | |
332 | struct ctl_table *table); | |
333 | void unregister_net_sysctl_table(struct ctl_table_header *header); | |
48c74958 EB |
334 | #else |
335 | static inline int net_sysctl_init(void) { return 0; } | |
336 | static inline struct ctl_table_header *register_net_sysctl(struct net *net, | |
337 | const char *path, struct ctl_table *table) | |
338 | { | |
339 | return NULL; | |
340 | } | |
341 | static inline void unregister_net_sysctl_table(struct ctl_table_header *header) | |
342 | { | |
343 | } | |
344 | #endif | |
345 | ||
ca4c3fc2 | 346 | static inline int rt_genid_ipv4(struct net *net) |
b42664f8 | 347 | { |
ca4c3fc2 | 348 | return atomic_read(&net->ipv4.rt_genid); |
b42664f8 ND |
349 | } |
350 | ||
ca4c3fc2 | 351 | static inline void rt_genid_bump_ipv4(struct net *net) |
b42664f8 | 352 | { |
ca4c3fc2 | 353 | atomic_inc(&net->ipv4.rt_genid); |
354 | } | |
355 | ||
705f1c86 | 356 | extern void (*__fib6_flush_trees)(struct net *net); |
ca4c3fc2 | 357 | static inline void rt_genid_bump_ipv6(struct net *net) |
358 | { | |
705f1c86 HFS |
359 | if (__fib6_flush_trees) |
360 | __fib6_flush_trees(net); | |
ca4c3fc2 | 361 | } |
ca4c3fc2 | 362 | |
599018a7 LR |
363 | #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) |
364 | static inline struct netns_ieee802154_lowpan * | |
365 | net_ieee802154_lowpan(struct net *net) | |
366 | { | |
367 | return &net->ieee802154_lowpan; | |
368 | } | |
599018a7 LR |
369 | #endif |
370 | ||
ca4c3fc2 | 371 | /* For callers who don't really care about whether it's IPv4 or IPv6 */ |
372 | static inline void rt_genid_bump_all(struct net *net) | |
373 | { | |
374 | rt_genid_bump_ipv4(net); | |
375 | rt_genid_bump_ipv6(net); | |
b42664f8 | 376 | } |
95bdfccb | 377 | |
5aad1de5 TT |
378 | static inline int fnhe_genid(struct net *net) |
379 | { | |
380 | return atomic_read(&net->fnhe_genid); | |
381 | } | |
382 | ||
383 | static inline void fnhe_genid_bump(struct net *net) | |
384 | { | |
385 | atomic_inc(&net->fnhe_genid); | |
386 | } | |
387 | ||
5f256bec | 388 | #endif /* __NET_NET_NAMESPACE_H */ |