1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Operations on the network namespace
5 #ifndef __NET_NET_NAMESPACE_H
6 #define __NET_NET_NAMESPACE_H
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>
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>
29 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
30 #include <net/netns/flow_table.h>
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>
47 struct user_namespace;
48 struct proc_dir_entry;
51 struct ctl_table_header;
58 #define NETDEV_HASHBITS 8
59 #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
62 /* First cache line can be often dirtied.
63 * Do not place here read-mostly fields.
65 refcount_t passive; /* To decide when the network
66 * namespace should be freed.
68 spinlock_t rules_mod_lock;
70 unsigned int dev_base_seq; /* protected by rtnl_mutex */
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).
83 struct llist_node defer_free_list;
84 struct llist_node cleanup_list; /* namespaces on death row */
87 struct key_tag *key_domain; /* Key domain of operation tag */
89 struct user_namespace *user_ns; /* Owning user namespace */
90 struct ucounts *ucounts;
94 struct ref_tracker_dir refcnt_tracker;
95 struct ref_tracker_dir notrefcnt_tracker; /* tracker for objects not
96 * refcounted against netns
98 struct list_head dev_base_head;
99 struct proc_dir_entry *proc_net;
100 struct proc_dir_entry *proc_net_stat;
103 struct ctl_table_set sysctls;
106 struct sock *rtnl; /* rtnetlink socket */
107 struct sock *genl_sock;
109 struct uevent_sock *uevent_sock; /* uevent socket */
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;
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.
121 struct net_device *loopback_dev; /* The loopback */
124 struct list_head rules_ops;
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;
132 struct netns_nexthop nexthop;
133 struct netns_ipv4 ipv4;
134 #if IS_ENABLED(CONFIG_IPV6)
135 struct netns_ipv6 ipv6;
137 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
138 struct netns_ieee802154_lowpan ieee802154_lowpan;
140 #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
141 struct netns_sctp sctp;
143 #ifdef CONFIG_NETFILTER
145 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
148 #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
149 struct netns_nftables nft;
151 #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
155 #ifdef CONFIG_WEXT_CORE
156 struct sk_buff_head wext_nlevents;
158 struct net_generic __rcu *gen;
160 /* Used to store attached BPF programs */
161 struct netns_bpf bpf;
163 /* Note : following structs are cache line aligned */
165 struct netns_xfrm xfrm;
168 u64 net_cookie; /* written once */
170 #if IS_ENABLED(CONFIG_IP_VS)
171 struct netns_ipvs *ipvs;
173 #if IS_ENABLED(CONFIG_MPLS)
174 struct netns_mpls mpls;
176 #if IS_ENABLED(CONFIG_CAN)
177 struct netns_can can;
179 #ifdef CONFIG_XDP_SOCKETS
180 struct netns_xdp xdp;
182 #if IS_ENABLED(CONFIG_MCTP)
183 struct netns_mctp mctp;
185 #if IS_ENABLED(CONFIG_CRYPTO_USER)
186 struct sock *crypto_nlsk;
188 struct sock *diag_nlsk;
189 #if IS_ENABLED(CONFIG_SMC)
190 struct netns_smc smc;
192 #ifdef CONFIG_DEBUG_NET_SMALL_RTNL
193 /* Move to a better place when the config guard is removed. */
194 struct mutex rtnl_mutex;
196 } __randomize_layout;
198 #include <linux/seq_file_net.h>
200 /* Init's network namespace */
201 extern struct net init_net;
204 struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns,
205 struct net *old_net);
207 void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid);
209 void net_ns_barrier(void);
211 struct ns_common *get_net_ns(struct ns_common *ns);
212 struct net *get_net_ns_by_fd(int fd);
213 #else /* CONFIG_NET_NS */
214 #include <linux/sched.h>
215 #include <linux/nsproxy.h>
216 static inline struct net *copy_net_ns(unsigned long flags,
217 struct user_namespace *user_ns, struct net *old_net)
219 if (flags & CLONE_NEWNET)
220 return ERR_PTR(-EINVAL);
224 static inline void net_ns_get_ownership(const struct net *net,
225 kuid_t *uid, kgid_t *gid)
227 *uid = GLOBAL_ROOT_UID;
228 *gid = GLOBAL_ROOT_GID;
231 static inline void net_ns_barrier(void) {}
233 static inline struct ns_common *get_net_ns(struct ns_common *ns)
235 return ERR_PTR(-EINVAL);
238 static inline struct net *get_net_ns_by_fd(int fd)
240 return ERR_PTR(-EINVAL);
242 #endif /* CONFIG_NET_NS */
245 extern struct list_head net_namespace_list;
247 struct net *get_net_ns_by_pid(pid_t pid);
250 void ipx_register_sysctl(void);
251 void ipx_unregister_sysctl(void);
253 #define ipx_register_sysctl()
254 #define ipx_unregister_sysctl()
258 void __put_net(struct net *net);
260 /* Try using get_net_track() instead */
261 static inline struct net *get_net(struct net *net)
263 refcount_inc(&net->ns.count);
267 static inline struct net *maybe_get_net(struct net *net)
269 /* Used when we know struct net exists but we
270 * aren't guaranteed a previous reference count
271 * exists. If the reference count is zero this
272 * function fails and returns NULL.
274 if (!refcount_inc_not_zero(&net->ns.count))
279 /* Try using put_net_track() instead */
280 static inline void put_net(struct net *net)
282 if (refcount_dec_and_test(&net->ns.count))
287 int net_eq(const struct net *net1, const struct net *net2)
292 static inline int check_net(const struct net *net)
294 return refcount_read(&net->ns.count) != 0;
297 void net_drop_ns(void *);
301 static inline struct net *get_net(struct net *net)
306 static inline void put_net(struct net *net)
310 static inline struct net *maybe_get_net(struct net *net)
316 int net_eq(const struct net *net1, const struct net *net2)
321 static inline int check_net(const struct net *net)
326 #define net_drop_ns NULL
329 /* Returns true if the netns initialization is completed successfully */
330 static inline bool net_initialized(const struct net *net)
332 return READ_ONCE(net->list.next);
335 static inline void __netns_tracker_alloc(struct net *net,
336 netns_tracker *tracker,
340 #ifdef CONFIG_NET_NS_REFCNT_TRACKER
341 ref_tracker_alloc(refcounted ? &net->refcnt_tracker :
342 &net->notrefcnt_tracker,
347 static inline void netns_tracker_alloc(struct net *net, netns_tracker *tracker,
350 __netns_tracker_alloc(net, tracker, true, gfp);
353 static inline void __netns_tracker_free(struct net *net,
354 netns_tracker *tracker,
357 #ifdef CONFIG_NET_NS_REFCNT_TRACKER
358 ref_tracker_free(refcounted ? &net->refcnt_tracker :
359 &net->notrefcnt_tracker, tracker);
363 static inline struct net *get_net_track(struct net *net,
364 netns_tracker *tracker, gfp_t gfp)
367 netns_tracker_alloc(net, tracker, gfp);
371 static inline void put_net_track(struct net *net, netns_tracker *tracker)
373 __netns_tracker_free(net, tracker, true);
379 struct net __rcu *net;
383 static inline void write_pnet(possible_net_t *pnet, struct net *net)
386 rcu_assign_pointer(pnet->net, net);
390 static inline struct net *read_pnet(const possible_net_t *pnet)
393 return rcu_dereference_protected(pnet->net, true);
399 static inline struct net *read_pnet_rcu(possible_net_t *pnet)
402 return rcu_dereference(pnet->net);
408 /* Protected by net_rwsem */
409 #define for_each_net(VAR) \
410 list_for_each_entry(VAR, &net_namespace_list, list)
411 #define for_each_net_continue_reverse(VAR) \
412 list_for_each_entry_continue_reverse(VAR, &net_namespace_list, list)
413 #define for_each_net_rcu(VAR) \
414 list_for_each_entry_rcu(VAR, &net_namespace_list, list)
419 #define __net_initdata
420 #define __net_initconst
422 #define __net_init __init
423 #define __net_exit __ref
424 #define __net_initdata __initdata
425 #define __net_initconst __initconst
428 int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp);
429 int peernet2id(const struct net *net, struct net *peer);
430 bool peernet_has_id(const struct net *net, struct net *peer);
431 struct net *get_net_ns_by_id(const struct net *net, int id);
433 struct pernet_operations {
434 struct list_head list;
436 * Below methods are called without any exclusive locks.
437 * More than one net may be constructed and destructed
438 * in parallel on several cpus. Every pernet_operations
439 * have to keep in mind all other pernet_operations and
440 * to introduce a locking, if they share common resources.
442 * The only time they are called with exclusive lock is
443 * from register_pernet_subsys(), unregister_pernet_subsys()
444 * register_pernet_device() and unregister_pernet_device().
446 * Exit methods using blocking RCU primitives, such as
447 * synchronize_rcu(), should be implemented via exit_batch.
448 * Then, destruction of a group of net requires single
449 * synchronize_rcu() related to these pernet_operations,
450 * instead of separate synchronize_rcu() for every net.
451 * Please, avoid synchronize_rcu() at all, where it's possible.
453 * Note that a combination of pre_exit() and exit() can
454 * be used, since a synchronize_rcu() is guaranteed between
457 int (*init)(struct net *net);
458 void (*pre_exit)(struct net *net);
459 void (*exit)(struct net *net);
460 void (*exit_batch)(struct list_head *net_exit_list);
461 /* Following method is called with RTNL held. */
462 void (*exit_batch_rtnl)(struct list_head *net_exit_list,
463 struct list_head *dev_kill_list);
464 unsigned int * const id;
469 * Use these carefully. If you implement a network device and it
470 * needs per network namespace operations use device pernet operations,
471 * otherwise use pernet subsys operations.
473 * Network interfaces need to be removed from a dying netns _before_
474 * subsys notifiers can be called, as most of the network code cleanup
475 * (which is done from subsys notifiers) runs with the assumption that
476 * dev_remove_pack has been called so no new packets will arrive during
477 * and after the cleanup functions have been called. dev_remove_pack
478 * is not per namespace so instead the guarantee of no more packets
479 * arriving in a network namespace is provided by ensuring that all
480 * network devices and all sockets have left the network namespace
481 * before the cleanup methods are called.
483 * For the longest time the ipv4 icmp code was registered as a pernet
484 * device which caused kernel oops, and panics during network
485 * namespace cleanup. So please don't get this wrong.
487 int register_pernet_subsys(struct pernet_operations *);
488 void unregister_pernet_subsys(struct pernet_operations *);
489 int register_pernet_device(struct pernet_operations *);
490 void unregister_pernet_device(struct pernet_operations *);
494 #define register_net_sysctl(net, path, table) \
495 register_net_sysctl_sz(net, path, table, ARRAY_SIZE(table))
497 int net_sysctl_init(void);
498 struct ctl_table_header *register_net_sysctl_sz(struct net *net, const char *path,
499 struct ctl_table *table, size_t table_size);
500 void unregister_net_sysctl_table(struct ctl_table_header *header);
502 static inline int net_sysctl_init(void) { return 0; }
503 static inline struct ctl_table_header *register_net_sysctl_sz(struct net *net,
504 const char *path, struct ctl_table *table, size_t table_size)
508 static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
513 static inline int rt_genid_ipv4(const struct net *net)
515 return atomic_read(&net->ipv4.rt_genid);
518 #if IS_ENABLED(CONFIG_IPV6)
519 static inline int rt_genid_ipv6(const struct net *net)
521 return atomic_read(&net->ipv6.fib6_sernum);
525 static inline void rt_genid_bump_ipv4(struct net *net)
527 atomic_inc(&net->ipv4.rt_genid);
530 extern void (*__fib6_flush_trees)(struct net *net);
531 static inline void rt_genid_bump_ipv6(struct net *net)
533 if (__fib6_flush_trees)
534 __fib6_flush_trees(net);
537 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
538 static inline struct netns_ieee802154_lowpan *
539 net_ieee802154_lowpan(struct net *net)
541 return &net->ieee802154_lowpan;
545 /* For callers who don't really care about whether it's IPv4 or IPv6 */
546 static inline void rt_genid_bump_all(struct net *net)
548 rt_genid_bump_ipv4(net);
549 rt_genid_bump_ipv6(net);
552 static inline int fnhe_genid(const struct net *net)
554 return atomic_read(&net->fnhe_genid);
557 static inline void fnhe_genid_bump(struct net *net)
559 atomic_inc(&net->fnhe_genid);
563 void net_ns_init(void);
565 static inline void net_ns_init(void) {}
568 #endif /* __NET_NET_NAMESPACE_H */