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