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