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