]> Git Repo - J-linux.git/blob - include/net/net_namespace.h
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / include / net / net_namespace.h
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 #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)
218 {
219         if (flags & CLONE_NEWNET)
220                 return ERR_PTR(-EINVAL);
221         return old_net;
222 }
223
224 static inline void net_ns_get_ownership(const struct net *net,
225                                         kuid_t *uid, kgid_t *gid)
226 {
227         *uid = GLOBAL_ROOT_UID;
228         *gid = GLOBAL_ROOT_GID;
229 }
230
231 static inline void net_ns_barrier(void) {}
232
233 static inline struct ns_common *get_net_ns(struct ns_common *ns)
234 {
235         return ERR_PTR(-EINVAL);
236 }
237
238 static inline struct net *get_net_ns_by_fd(int fd)
239 {
240         return ERR_PTR(-EINVAL);
241 }
242 #endif /* CONFIG_NET_NS */
243
244
245 extern struct list_head net_namespace_list;
246
247 struct net *get_net_ns_by_pid(pid_t pid);
248
249 #ifdef CONFIG_SYSCTL
250 void ipx_register_sysctl(void);
251 void ipx_unregister_sysctl(void);
252 #else
253 #define ipx_register_sysctl()
254 #define ipx_unregister_sysctl()
255 #endif
256
257 #ifdef CONFIG_NET_NS
258 void __put_net(struct net *net);
259
260 /* Try using get_net_track() instead */
261 static inline struct net *get_net(struct net *net)
262 {
263         refcount_inc(&net->ns.count);
264         return net;
265 }
266
267 static inline struct net *maybe_get_net(struct net *net)
268 {
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.
273          */
274         if (!refcount_inc_not_zero(&net->ns.count))
275                 net = NULL;
276         return net;
277 }
278
279 /* Try using put_net_track() instead */
280 static inline void put_net(struct net *net)
281 {
282         if (refcount_dec_and_test(&net->ns.count))
283                 __put_net(net);
284 }
285
286 static inline
287 int net_eq(const struct net *net1, const struct net *net2)
288 {
289         return net1 == net2;
290 }
291
292 static inline int check_net(const struct net *net)
293 {
294         return refcount_read(&net->ns.count) != 0;
295 }
296
297 void net_drop_ns(void *);
298
299 #else
300
301 static inline struct net *get_net(struct net *net)
302 {
303         return net;
304 }
305
306 static inline void put_net(struct net *net)
307 {
308 }
309
310 static inline struct net *maybe_get_net(struct net *net)
311 {
312         return net;
313 }
314
315 static inline
316 int net_eq(const struct net *net1, const struct net *net2)
317 {
318         return 1;
319 }
320
321 static inline int check_net(const struct net *net)
322 {
323         return 1;
324 }
325
326 #define net_drop_ns NULL
327 #endif
328
329 /* Returns true if the netns initialization is completed successfully */
330 static inline bool net_initialized(const struct net *net)
331 {
332         return READ_ONCE(net->list.next);
333 }
334
335 static inline void __netns_tracker_alloc(struct net *net,
336                                          netns_tracker *tracker,
337                                          bool refcounted,
338                                          gfp_t gfp)
339 {
340 #ifdef CONFIG_NET_NS_REFCNT_TRACKER
341         ref_tracker_alloc(refcounted ? &net->refcnt_tracker :
342                                        &net->notrefcnt_tracker,
343                           tracker, gfp);
344 #endif
345 }
346
347 static inline void netns_tracker_alloc(struct net *net, netns_tracker *tracker,
348                                        gfp_t gfp)
349 {
350         __netns_tracker_alloc(net, tracker, true, gfp);
351 }
352
353 static inline void __netns_tracker_free(struct net *net,
354                                         netns_tracker *tracker,
355                                         bool refcounted)
356 {
357 #ifdef CONFIG_NET_NS_REFCNT_TRACKER
358        ref_tracker_free(refcounted ? &net->refcnt_tracker :
359                                      &net->notrefcnt_tracker, tracker);
360 #endif
361 }
362
363 static inline struct net *get_net_track(struct net *net,
364                                         netns_tracker *tracker, gfp_t gfp)
365 {
366         get_net(net);
367         netns_tracker_alloc(net, tracker, gfp);
368         return net;
369 }
370
371 static inline void put_net_track(struct net *net, netns_tracker *tracker)
372 {
373         __netns_tracker_free(net, tracker, true);
374         put_net(net);
375 }
376
377 typedef struct {
378 #ifdef CONFIG_NET_NS
379         struct net __rcu *net;
380 #endif
381 } possible_net_t;
382
383 static inline void write_pnet(possible_net_t *pnet, struct net *net)
384 {
385 #ifdef CONFIG_NET_NS
386         rcu_assign_pointer(pnet->net, net);
387 #endif
388 }
389
390 static inline struct net *read_pnet(const possible_net_t *pnet)
391 {
392 #ifdef CONFIG_NET_NS
393         return rcu_dereference_protected(pnet->net, true);
394 #else
395         return &init_net;
396 #endif
397 }
398
399 static inline struct net *read_pnet_rcu(possible_net_t *pnet)
400 {
401 #ifdef CONFIG_NET_NS
402         return rcu_dereference(pnet->net);
403 #else
404         return &init_net;
405 #endif
406 }
407
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)
415
416 #ifdef CONFIG_NET_NS
417 #define __net_init
418 #define __net_exit
419 #define __net_initdata
420 #define __net_initconst
421 #else
422 #define __net_init      __init
423 #define __net_exit      __ref
424 #define __net_initdata  __initdata
425 #define __net_initconst __initconst
426 #endif
427
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);
432
433 struct pernet_operations {
434         struct list_head list;
435         /*
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.
441          *
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().
445          *
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.
452          *
453          * Note that a combination of pre_exit() and exit() can
454          * be used, since a synchronize_rcu() is guaranteed between
455          * the calls.
456          */
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;
465         const size_t size;
466 };
467
468 /*
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.
472  *
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.
482  *
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.
486  */
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 *);
491
492 struct ctl_table;
493
494 #define register_net_sysctl(net, path, table)   \
495         register_net_sysctl_sz(net, path, table, ARRAY_SIZE(table))
496 #ifdef CONFIG_SYSCTL
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);
501 #else
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)
505 {
506         return NULL;
507 }
508 static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
509 {
510 }
511 #endif
512
513 static inline int rt_genid_ipv4(const struct net *net)
514 {
515         return atomic_read(&net->ipv4.rt_genid);
516 }
517
518 #if IS_ENABLED(CONFIG_IPV6)
519 static inline int rt_genid_ipv6(const struct net *net)
520 {
521         return atomic_read(&net->ipv6.fib6_sernum);
522 }
523 #endif
524
525 static inline void rt_genid_bump_ipv4(struct net *net)
526 {
527         atomic_inc(&net->ipv4.rt_genid);
528 }
529
530 extern void (*__fib6_flush_trees)(struct net *net);
531 static inline void rt_genid_bump_ipv6(struct net *net)
532 {
533         if (__fib6_flush_trees)
534                 __fib6_flush_trees(net);
535 }
536
537 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
538 static inline struct netns_ieee802154_lowpan *
539 net_ieee802154_lowpan(struct net *net)
540 {
541         return &net->ieee802154_lowpan;
542 }
543 #endif
544
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)
547 {
548         rt_genid_bump_ipv4(net);
549         rt_genid_bump_ipv6(net);
550 }
551
552 static inline int fnhe_genid(const struct net *net)
553 {
554         return atomic_read(&net->fnhe_genid);
555 }
556
557 static inline void fnhe_genid_bump(struct net *net)
558 {
559         atomic_inc(&net->fnhe_genid);
560 }
561
562 #ifdef CONFIG_NET
563 void net_ns_init(void);
564 #else
565 static inline void net_ns_init(void) {}
566 #endif
567
568 #endif /* __NET_NET_NAMESPACE_H */
This page took 0.055321 seconds and 4 git commands to generate.