OSDN Git Service

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