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