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