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