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