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