1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Operations on the network namespace
4 */
5 #ifndef __NET_NET_NAMESPACE_H
6 #define __NET_NET_NAMESPACE_H
7
8 #include <linux/atomic.h>
9 #include <linux/refcount.h>
10 #include <linux/workqueue.h>
11 #include <linux/list.h>
12 #include <linux/sysctl.h>
13 #include <linux/uidgid.h>
14
15 #include <net/flow.h>
16 #include <net/netns/core.h>
17 #include <net/netns/mib.h>
18 #include <net/netns/unix.h>
19 #include <net/netns/packet.h>
20 #include <net/netns/ipv4.h>
21 #include <net/netns/ipv6.h>
22 #include <net/netns/nexthop.h>
23 #include <net/netns/ieee802154_6lowpan.h>
24 #include <net/netns/sctp.h>
25 #include <net/netns/netfilter.h>
26 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
27 #include <net/netns/conntrack.h>
28 #endif
29 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
30 #include <net/netns/flow_table.h>
31 #endif
32 #include <net/netns/nftables.h>
33 #include <net/netns/xfrm.h>
34 #include <net/netns/mpls.h>
35 #include <net/netns/can.h>
36 #include <net/netns/xdp.h>
37 #include <net/netns/smc.h>
38 #include <net/netns/bpf.h>
39 #include <net/netns/mctp.h>
40 #include <net/netns/vsock.h>
41 #include <net/net_trackers.h>
42 #include <linux/ns_common.h>
43 #include <linux/idr.h>
44 #include <linux/skbuff.h>
45 #include <linux/notifier.h>
46 #include <linux/xarray.h>
47
48 struct user_namespace;
49 struct proc_dir_entry;
50 struct net_device;
51 struct sock;
52 struct ctl_table_header;
53 struct net_generic;
54 struct uevent_sock;
55 struct netns_ipvs;
56 struct bpf_prog;
57
58
59 #define NETDEV_HASHBITS 8
60 #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
61
62 struct net {
63 /* First cache line can be often dirtied.
64 * Do not place here read-mostly fields.
65 */
66 refcount_t passive; /* To decide when the network
67 * namespace should be freed.
68 */
69 spinlock_t rules_mod_lock;
70
71 unsigned int dev_base_seq; /* protected by rtnl_mutex */
72 u32 ifindex;
73
74 spinlock_t nsid_lock;
75 atomic_t fnhe_genid;
76
77 struct list_head list; /* list of network namespaces */
78 struct list_head exit_list; /* To linked to call pernet exit
79 * methods on dead net (
80 * pernet_ops_rwsem read locked),
81 * or to unregister pernet ops
82 * (pernet_ops_rwsem write locked).
83 */
84 struct llist_node defer_free_list;
85 struct llist_node cleanup_list; /* namespaces on death row */
86
87 struct list_head ptype_all;
88 struct list_head ptype_specific;
89
90 #ifdef CONFIG_KEYS
91 struct key_tag *key_domain; /* Key domain of operation tag */
92 #endif
93 struct user_namespace *user_ns; /* Owning user namespace */
94 struct ucounts *ucounts;
95 struct idr netns_ids;
96
97 struct ns_common ns;
98 struct ref_tracker_dir refcnt_tracker;
99 struct ref_tracker_dir notrefcnt_tracker; /* tracker for objects not
100 * refcounted against netns
101 */
102 struct list_head dev_base_head;
103 struct proc_dir_entry *proc_net;
104 struct proc_dir_entry *proc_net_stat;
105
106 #ifdef CONFIG_SYSCTL
107 struct ctl_table_set sysctls;
108 #endif
109
110 struct sock *rtnl; /* rtnetlink socket */
111 struct sock *genl_sock;
112
113 struct uevent_sock *uevent_sock; /* uevent socket */
114
115 struct hlist_head *dev_name_head;
116 struct hlist_head *dev_index_head;
117 struct xarray dev_by_index;
118 struct raw_notifier_head netdev_chain;
119
120 /* Note that @hash_mix can be read millions times per second,
121 * it is critical that it is on a read_mostly cache line.
122 */
123 u32 hash_mix;
124 bool is_dying;
125
126 struct net_device *loopback_dev; /* The loopback */
127
128 /* core fib_rules */
129 struct list_head rules_ops;
130
131 struct netns_core core;
132 struct netns_mib mib;
133 struct netns_packet packet;
134 #if IS_ENABLED(CONFIG_UNIX)
135 struct netns_unix unx;
136 #endif
137 struct netns_nexthop nexthop;
138 struct netns_ipv4 ipv4;
139 #if IS_ENABLED(CONFIG_IPV6)
140 struct netns_ipv6 ipv6;
141 #endif
142 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
143 struct netns_ieee802154_lowpan ieee802154_lowpan;
144 #endif
145 #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
146 struct netns_sctp sctp;
147 #endif
148 #ifdef CONFIG_NETFILTER
149 struct netns_nf nf;
150 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
151 struct netns_ct ct;
152 #endif
153 #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
154 struct netns_nftables nft;
155 #endif
156 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
157 struct netns_ft ft;
158 #endif
159 #endif
160 #ifdef CONFIG_WEXT_CORE
161 struct sk_buff_head wext_nlevents;
162 #endif
163 struct net_generic __rcu *gen;
164
165 /* Used to store attached BPF programs */
166 struct netns_bpf bpf;
167
168 /* Note : following structs are cache line aligned */
169 #ifdef CONFIG_XFRM
170 struct netns_xfrm xfrm;
171 #endif
172
173 u64 net_cookie; /* written once */
174
175 #if IS_ENABLED(CONFIG_IP_VS)
176 struct netns_ipvs *ipvs;
177 #endif
178 #if IS_ENABLED(CONFIG_MPLS)
179 struct netns_mpls mpls;
180 #endif
181 #if IS_ENABLED(CONFIG_CAN)
182 struct netns_can can;
183 #endif
184 #ifdef CONFIG_XDP_SOCKETS
185 struct netns_xdp xdp;
186 #endif
187 #if IS_ENABLED(CONFIG_MCTP)
188 struct netns_mctp mctp;
189 #endif
190 #if IS_ENABLED(CONFIG_CRYPTO_USER)
191 struct sock *crypto_nlsk;
192 #endif
193 struct sock *diag_nlsk;
194 #if IS_ENABLED(CONFIG_SMC)
195 struct netns_smc smc;
196 #endif
197 #ifdef CONFIG_DEBUG_NET_SMALL_RTNL
198 /* Move to a better place when the config guard is removed. */
199 struct mutex rtnl_mutex;
200 struct work_struct rtnl_work;
201 struct list_head dev_unreg_head;
202 spinlock_t dev_unreg_lock;
203 #endif
204 #if IS_ENABLED(CONFIG_VSOCKETS)
205 struct netns_vsock vsock;
206 #endif
207 } __randomize_layout;
208
209 #include <linux/seq_file_net.h>
210
211 /* Init's network namespace */
212 extern struct net init_net;
213
214 #ifdef CONFIG_NET_NS
215 struct net *copy_net_ns(u64 flags, struct user_namespace *user_ns,
216 struct net *old_net);
217
218 void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid);
219
220 void net_ns_barrier(void);
221
222 struct ns_common *get_net_ns(struct ns_common *ns);
223 struct net *get_net_ns_by_fd(int fd);
224 extern struct task_struct *cleanup_net_task;
225
226 #else /* CONFIG_NET_NS */
227 #include <linux/sched.h>
228 #include <linux/nsproxy.h>
copy_net_ns(u64 flags,struct user_namespace * user_ns,struct net * old_net)229 static inline struct net *copy_net_ns(u64 flags,
230 struct user_namespace *user_ns, struct net *old_net)
231 {
232 if (flags & CLONE_NEWNET)
233 return ERR_PTR(-EINVAL);
234 return old_net;
235 }
236
net_ns_get_ownership(const struct net * net,kuid_t * uid,kgid_t * gid)237 static inline void net_ns_get_ownership(const struct net *net,
238 kuid_t *uid, kgid_t *gid)
239 {
240 *uid = GLOBAL_ROOT_UID;
241 *gid = GLOBAL_ROOT_GID;
242 }
243
net_ns_barrier(void)244 static inline void net_ns_barrier(void) {}
245
get_net_ns(struct ns_common * ns)246 static inline struct ns_common *get_net_ns(struct ns_common *ns)
247 {
248 return ERR_PTR(-EINVAL);
249 }
250
get_net_ns_by_fd(int fd)251 static inline struct net *get_net_ns_by_fd(int fd)
252 {
253 return ERR_PTR(-EINVAL);
254 }
255 #endif /* CONFIG_NET_NS */
256
257
258 extern struct list_head net_namespace_list;
259
260 struct net *get_net_ns_by_pid(pid_t pid);
261
262 #ifdef CONFIG_SYSCTL
263 void ipx_register_sysctl(void);
264 void ipx_unregister_sysctl(void);
265 #else
266 #define ipx_register_sysctl()
267 #define ipx_unregister_sysctl()
268 #endif
269
to_net_ns(struct ns_common * ns)270 static inline struct net *to_net_ns(struct ns_common *ns)
271 {
272 return container_of(ns, struct net, ns);
273 }
274
275 #ifdef CONFIG_NET_NS
276 void __put_net(struct net *net);
277
278 /* Try using get_net_track() instead */
get_net(struct net * net)279 static inline struct net *get_net(struct net *net)
280 {
281 ns_ref_inc(net);
282 return net;
283 }
284
maybe_get_net(struct net * net)285 static inline struct net *maybe_get_net(struct net *net)
286 {
287 /* Used when we know struct net exists but we
288 * aren't guaranteed a previous reference count
289 * exists. If the reference count is zero this
290 * function fails and returns NULL.
291 */
292 if (!ns_ref_get(net))
293 net = NULL;
294 return net;
295 }
296
297 /* Try using put_net_track() instead */
put_net(struct net * net)298 static inline void put_net(struct net *net)
299 {
300 if (ns_ref_put(net))
301 __put_net(net);
302 }
303
304 static inline
net_eq(const struct net * net1,const struct net * net2)305 int net_eq(const struct net *net1, const struct net *net2)
306 {
307 return net1 == net2;
308 }
309
check_net(const struct net * net)310 static inline int check_net(const struct net *net)
311 {
312 return ns_ref_read(net) != 0;
313 }
314
315 void net_drop_ns(struct ns_common *);
316 void net_passive_dec(struct net *net);
317
318 #else
319
get_net(struct net * net)320 static inline struct net *get_net(struct net *net)
321 {
322 return net;
323 }
324
put_net(struct net * net)325 static inline void put_net(struct net *net)
326 {
327 }
328
maybe_get_net(struct net * net)329 static inline struct net *maybe_get_net(struct net *net)
330 {
331 return net;
332 }
333
334 static inline
net_eq(const struct net * net1,const struct net * net2)335 int net_eq(const struct net *net1, const struct net *net2)
336 {
337 return 1;
338 }
339
check_net(const struct net * net)340 static inline int check_net(const struct net *net)
341 {
342 return 1;
343 }
344
345 #define net_drop_ns NULL
346
net_passive_dec(struct net * net)347 static inline void net_passive_dec(struct net *net)
348 {
349 refcount_dec(&net->passive);
350 }
351 #endif
352
net_passive_inc(struct net * net)353 static inline void net_passive_inc(struct net *net)
354 {
355 refcount_inc(&net->passive);
356 }
357
358 /* Returns true if the netns initialization is completed successfully */
net_initialized(const struct net * net)359 static inline bool net_initialized(const struct net *net)
360 {
361 return READ_ONCE(net->list.next);
362 }
363
__netns_tracker_alloc(struct net * net,netns_tracker * tracker,bool refcounted,gfp_t gfp)364 static inline void __netns_tracker_alloc(struct net *net,
365 netns_tracker *tracker,
366 bool refcounted,
367 gfp_t gfp)
368 {
369 #ifdef CONFIG_NET_NS_REFCNT_TRACKER
370 ref_tracker_alloc(refcounted ? &net->refcnt_tracker :
371 &net->notrefcnt_tracker,
372 tracker, gfp);
373 #endif
374 }
375
netns_tracker_alloc(struct net * net,netns_tracker * tracker,gfp_t gfp)376 static inline void netns_tracker_alloc(struct net *net, netns_tracker *tracker,
377 gfp_t gfp)
378 {
379 __netns_tracker_alloc(net, tracker, true, gfp);
380 }
381
__netns_tracker_free(struct net * net,netns_tracker * tracker,bool refcounted)382 static inline void __netns_tracker_free(struct net *net,
383 netns_tracker *tracker,
384 bool refcounted)
385 {
386 #ifdef CONFIG_NET_NS_REFCNT_TRACKER
387 ref_tracker_free(refcounted ? &net->refcnt_tracker :
388 &net->notrefcnt_tracker, tracker);
389 #endif
390 }
391
get_net_track(struct net * net,netns_tracker * tracker,gfp_t gfp)392 static inline struct net *get_net_track(struct net *net,
393 netns_tracker *tracker, gfp_t gfp)
394 {
395 get_net(net);
396 netns_tracker_alloc(net, tracker, gfp);
397 return net;
398 }
399
put_net_track(struct net * net,netns_tracker * tracker)400 static inline void put_net_track(struct net *net, netns_tracker *tracker)
401 {
402 __netns_tracker_free(net, tracker, true);
403 put_net(net);
404 }
405
406 typedef struct {
407 #ifdef CONFIG_NET_NS
408 struct net __rcu *net;
409 #endif
410 } possible_net_t;
411
write_pnet(possible_net_t * pnet,struct net * net)412 static inline void write_pnet(possible_net_t *pnet, struct net *net)
413 {
414 #ifdef CONFIG_NET_NS
415 rcu_assign_pointer(pnet->net, net);
416 #endif
417 }
418
read_pnet(const possible_net_t * pnet)419 static inline struct net *read_pnet(const possible_net_t *pnet)
420 {
421 #ifdef CONFIG_NET_NS
422 return rcu_dereference_protected(pnet->net, true);
423 #else
424 return &init_net;
425 #endif
426 }
427
read_pnet_rcu(const possible_net_t * pnet)428 static inline struct net *read_pnet_rcu(const possible_net_t *pnet)
429 {
430 #ifdef CONFIG_NET_NS
431 return rcu_dereference(pnet->net);
432 #else
433 return &init_net;
434 #endif
435 }
436
437 /* Protected by net_rwsem */
438 #define for_each_net(VAR) \
439 list_for_each_entry(VAR, &net_namespace_list, list)
440 #define for_each_net_continue_reverse(VAR) \
441 list_for_each_entry_continue_reverse(VAR, &net_namespace_list, list)
442 #define for_each_net_rcu(VAR) \
443 list_for_each_entry_rcu(VAR, &net_namespace_list, list)
444
445 #ifdef CONFIG_NET_NS
446 #define __net_init
447 #define __net_exit
448 #define __net_initdata
449 #define __net_initconst
450 #else
451 #define __net_init __init
452 #define __net_exit __ref
453 #define __net_initdata __initdata
454 #define __net_initconst __initconst
455 #endif
456
457 int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp);
458 int peernet2id(const struct net *net, struct net *peer);
459 bool peernet_has_id(const struct net *net, struct net *peer);
460 struct net *get_net_ns_by_id(const struct net *net, int id);
461
462 struct pernet_operations {
463 struct list_head list;
464 /*
465 * Below methods are called without any exclusive locks.
466 * More than one net may be constructed and destructed
467 * in parallel on several cpus. Every pernet_operations
468 * have to keep in mind all other pernet_operations and
469 * to introduce a locking, if they share common resources.
470 *
471 * The only time they are called with exclusive lock is
472 * from register_pernet_subsys(), unregister_pernet_subsys()
473 * register_pernet_device() and unregister_pernet_device().
474 *
475 * Exit methods using blocking RCU primitives, such as
476 * synchronize_rcu(), should be implemented via exit_batch.
477 * Then, destruction of a group of net requires single
478 * synchronize_rcu() related to these pernet_operations,
479 * instead of separate synchronize_rcu() for every net.
480 * Please, avoid synchronize_rcu() at all, where it's possible.
481 *
482 * Note that a combination of pre_exit() and exit() can
483 * be used, since a synchronize_rcu() is guaranteed between
484 * the calls.
485 */
486 int (*init)(struct net *net);
487 void (*pre_exit)(struct net *net);
488 void (*exit)(struct net *net);
489 void (*exit_batch)(struct list_head *net_exit_list);
490 /* Following method is called with RTNL held. */
491 void (*exit_rtnl)(struct net *net,
492 struct list_head *dev_kill_list);
493 unsigned int * const id;
494 const size_t size;
495 };
496
497 /*
498 * Use these carefully. If you implement a network device and it
499 * needs per network namespace operations use device pernet operations,
500 * otherwise use pernet subsys operations.
501 *
502 * Network interfaces need to be removed from a dying netns _before_
503 * subsys notifiers can be called, as most of the network code cleanup
504 * (which is done from subsys notifiers) runs with the assumption that
505 * dev_remove_pack has been called so no new packets will arrive during
506 * and after the cleanup functions have been called. dev_remove_pack
507 * is not per namespace so instead the guarantee of no more packets
508 * arriving in a network namespace is provided by ensuring that all
509 * network devices and all sockets have left the network namespace
510 * before the cleanup methods are called.
511 *
512 * For the longest time the ipv4 icmp code was registered as a pernet
513 * device which caused kernel oops, and panics during network
514 * namespace cleanup. So please don't get this wrong.
515 */
516 int register_pernet_subsys(struct pernet_operations *);
517 void unregister_pernet_subsys(struct pernet_operations *);
518 int register_pernet_device(struct pernet_operations *);
519 void unregister_pernet_device(struct pernet_operations *);
520
521 struct ctl_table;
522
523 #define register_net_sysctl(net, path, table) \
524 register_net_sysctl_sz(net, path, table, ARRAY_SIZE(table))
525 #ifdef CONFIG_SYSCTL
526 int net_sysctl_init(void);
527 struct ctl_table_header *register_net_sysctl_sz(struct net *net, const char *path,
528 const struct ctl_table *table,
529 size_t table_size);
530 void unregister_net_sysctl_table(struct ctl_table_header *header);
531 #else
net_sysctl_init(void)532 static inline int net_sysctl_init(void) { return 0; }
register_net_sysctl_sz(struct net * net,const char * path,const struct ctl_table * table,size_t table_size)533 static inline struct ctl_table_header *register_net_sysctl_sz(struct net *net,
534 const char *path, const struct ctl_table *table, size_t table_size)
535 {
536 return NULL;
537 }
unregister_net_sysctl_table(struct ctl_table_header * header)538 static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
539 {
540 }
541 #endif
542
rt_genid_ipv4(const struct net * net)543 static inline int rt_genid_ipv4(const struct net *net)
544 {
545 return atomic_read(&net->ipv4.rt_genid);
546 }
547
548 #if IS_ENABLED(CONFIG_IPV6)
rt_genid_ipv6(const struct net * net)549 static inline int rt_genid_ipv6(const struct net *net)
550 {
551 return atomic_read(&net->ipv6.fib6_sernum);
552 }
553 #endif
554
rt_genid_bump_ipv4(struct net * net)555 static inline void rt_genid_bump_ipv4(struct net *net)
556 {
557 atomic_inc(&net->ipv4.rt_genid);
558 }
559
560 extern void (*__fib6_flush_trees)(struct net *net);
rt_genid_bump_ipv6(struct net * net)561 static inline void rt_genid_bump_ipv6(struct net *net)
562 {
563 if (__fib6_flush_trees)
564 __fib6_flush_trees(net);
565 }
566
567 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
568 static inline struct netns_ieee802154_lowpan *
net_ieee802154_lowpan(struct net * net)569 net_ieee802154_lowpan(struct net *net)
570 {
571 return &net->ieee802154_lowpan;
572 }
573 #endif
574
575 /* For callers who don't really care about whether it's IPv4 or IPv6 */
rt_genid_bump_all(struct net * net)576 static inline void rt_genid_bump_all(struct net *net)
577 {
578 rt_genid_bump_ipv4(net);
579 rt_genid_bump_ipv6(net);
580 }
581
fnhe_genid(const struct net * net)582 static inline int fnhe_genid(const struct net *net)
583 {
584 return atomic_read(&net->fnhe_genid);
585 }
586
fnhe_genid_bump(struct net * net)587 static inline void fnhe_genid_bump(struct net *net)
588 {
589 atomic_inc(&net->fnhe_genid);
590 }
591
592 #ifdef CONFIG_NET
593 void net_ns_init(void);
594 #else
net_ns_init(void)595 static inline void net_ns_init(void) {}
596 #endif
597
598 #endif /* __NET_NET_NAMESPACE_H */
599