1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Operations on the network namespace
5 #ifndef __NET_NET_NAMESPACE_H
6 #define __NET_NET_NAMESPACE_H
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>
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>
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 <linux/ns_common.h>
35 #include <linux/idr.h>
36 #include <linux/skbuff.h>
38 struct user_namespace
;
39 struct proc_dir_entry
;
42 struct ctl_table_header
;
48 #define NETDEV_HASHBITS 8
49 #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
52 refcount_t passive
; /* To decided when the network
53 * namespace should be freed.
55 atomic_t count
; /* To decided when the network
56 * namespace should be shut down.
58 spinlock_t rules_mod_lock
;
61 atomic64_t cookie_gen
;
63 struct list_head list
; /* list of network namespaces */
64 struct list_head cleanup_list
; /* namespaces on death row */
65 struct list_head exit_list
; /* Use only net_mutex */
67 struct user_namespace
*user_ns
; /* Owning user namespace */
68 struct ucounts
*ucounts
;
74 struct proc_dir_entry
*proc_net
;
75 struct proc_dir_entry
*proc_net_stat
;
78 struct ctl_table_set sysctls
;
81 struct sock
*rtnl
; /* rtnetlink socket */
82 struct sock
*genl_sock
;
84 struct list_head dev_base_head
;
85 struct hlist_head
*dev_name_head
;
86 struct hlist_head
*dev_index_head
;
87 unsigned int dev_base_seq
; /* protected by rtnl_mutex */
89 unsigned int dev_unreg_count
;
92 struct list_head rules_ops
;
94 struct list_head fib_notifier_ops
; /* protected by net_mutex */
96 struct net_device
*loopback_dev
; /* The loopback */
97 struct netns_core core
;
99 struct netns_packet packet
;
100 struct netns_unix unx
;
101 struct netns_ipv4 ipv4
;
102 #if IS_ENABLED(CONFIG_IPV6)
103 struct netns_ipv6 ipv6
;
105 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
106 struct netns_ieee802154_lowpan ieee802154_lowpan
;
108 #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
109 struct netns_sctp sctp
;
111 #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
112 struct netns_dccp dccp
;
114 #ifdef CONFIG_NETFILTER
117 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
120 #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
121 struct netns_nftables nft
;
123 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
124 struct netns_nf_frag nf_frag
;
125 struct ctl_table_header
*nf_frag_frags_hdr
;
128 struct sock
*nfnl_stash
;
129 #if IS_ENABLED(CONFIG_NETFILTER_NETLINK_ACCT)
130 struct list_head nfnl_acct_list
;
132 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
133 struct list_head nfct_timeout_list
;
136 #ifdef CONFIG_WEXT_CORE
137 struct sk_buff_head wext_nlevents
;
139 struct net_generic __rcu
*gen
;
141 /* Note : following structs are cache line aligned */
143 struct netns_xfrm xfrm
;
145 #if IS_ENABLED(CONFIG_IP_VS)
146 struct netns_ipvs
*ipvs
;
148 #if IS_ENABLED(CONFIG_MPLS)
149 struct netns_mpls mpls
;
151 #if IS_ENABLED(CONFIG_CAN)
152 struct netns_can can
;
154 struct sock
*diag_nlsk
;
156 } __randomize_layout
;
158 #include <linux/seq_file_net.h>
160 /* Init's network namespace */
161 extern struct net init_net
;
164 struct net
*copy_net_ns(unsigned long flags
, struct user_namespace
*user_ns
,
165 struct net
*old_net
);
167 void net_ns_get_ownership(const struct net
*net
, kuid_t
*uid
, kgid_t
*gid
);
169 void net_ns_barrier(void);
170 #else /* CONFIG_NET_NS */
171 #include <linux/sched.h>
172 #include <linux/nsproxy.h>
173 static inline struct net
*copy_net_ns(unsigned long flags
,
174 struct user_namespace
*user_ns
, struct net
*old_net
)
176 if (flags
& CLONE_NEWNET
)
177 return ERR_PTR(-EINVAL
);
181 static inline void net_ns_get_ownership(const struct net
*net
,
182 kuid_t
*uid
, kgid_t
*gid
)
184 *uid
= GLOBAL_ROOT_UID
;
185 *gid
= GLOBAL_ROOT_GID
;
188 static inline void net_ns_barrier(void) {}
189 #endif /* CONFIG_NET_NS */
192 extern struct list_head net_namespace_list
;
194 struct net
*get_net_ns_by_pid(pid_t pid
);
195 struct net
*get_net_ns_by_fd(int fd
);
198 void ipx_register_sysctl(void);
199 void ipx_unregister_sysctl(void);
201 #define ipx_register_sysctl()
202 #define ipx_unregister_sysctl()
206 void __put_net(struct net
*net
);
208 static inline struct net
*get_net(struct net
*net
)
210 atomic_inc(&net
->count
);
214 static inline struct net
*maybe_get_net(struct net
*net
)
216 /* Used when we know struct net exists but we
217 * aren't guaranteed a previous reference count
218 * exists. If the reference count is zero this
219 * function fails and returns NULL.
221 if (!atomic_inc_not_zero(&net
->count
))
226 static inline void put_net(struct net
*net
)
228 if (atomic_dec_and_test(&net
->count
))
233 int net_eq(const struct net
*net1
, const struct net
*net2
)
238 static inline int check_net(const struct net
*net
)
240 return atomic_read(&net
->count
) != 0;
243 void net_drop_ns(void *);
247 static inline struct net
*get_net(struct net
*net
)
252 static inline void put_net(struct net
*net
)
256 static inline struct net
*maybe_get_net(struct net
*net
)
262 int net_eq(const struct net
*net1
, const struct net
*net2
)
267 static inline int check_net(const struct net
*net
)
272 #define net_drop_ns NULL
282 static inline void write_pnet(possible_net_t
*pnet
, struct net
*net
)
289 static inline struct net
*read_pnet(const possible_net_t
*pnet
)
298 #define for_each_net(VAR) \
299 list_for_each_entry(VAR, &net_namespace_list, list)
301 #define for_each_net_rcu(VAR) \
302 list_for_each_entry_rcu(VAR, &net_namespace_list, list)
307 #define __net_initdata
308 #define __net_initconst
310 #define __net_init __init
311 #define __net_exit __ref
312 #define __net_initdata __initdata
313 #define __net_initconst __initconst
316 int peernet2id_alloc(struct net
*net
, struct net
*peer
);
317 int peernet2id(struct net
*net
, struct net
*peer
);
318 bool peernet_has_id(struct net
*net
, struct net
*peer
);
319 struct net
*get_net_ns_by_id(struct net
*net
, int id
);
321 struct pernet_operations
{
322 struct list_head list
;
323 int (*init
)(struct net
*net
);
324 void (*exit
)(struct net
*net
);
325 void (*exit_batch
)(struct list_head
*net_exit_list
);
331 * Use these carefully. If you implement a network device and it
332 * needs per network namespace operations use device pernet operations,
333 * otherwise use pernet subsys operations.
335 * Network interfaces need to be removed from a dying netns _before_
336 * subsys notifiers can be called, as most of the network code cleanup
337 * (which is done from subsys notifiers) runs with the assumption that
338 * dev_remove_pack has been called so no new packets will arrive during
339 * and after the cleanup functions have been called. dev_remove_pack
340 * is not per namespace so instead the guarantee of no more packets
341 * arriving in a network namespace is provided by ensuring that all
342 * network devices and all sockets have left the network namespace
343 * before the cleanup methods are called.
345 * For the longest time the ipv4 icmp code was registered as a pernet
346 * device which caused kernel oops, and panics during network
347 * namespace cleanup. So please don't get this wrong.
349 int register_pernet_subsys(struct pernet_operations
*);
350 void unregister_pernet_subsys(struct pernet_operations
*);
351 int register_pernet_device(struct pernet_operations
*);
352 void unregister_pernet_device(struct pernet_operations
*);
355 struct ctl_table_header
;
358 int net_sysctl_init(void);
359 struct ctl_table_header
*register_net_sysctl(struct net
*net
, const char *path
,
360 struct ctl_table
*table
);
361 void unregister_net_sysctl_table(struct ctl_table_header
*header
);
363 static inline int net_sysctl_init(void) { return 0; }
364 static inline struct ctl_table_header
*register_net_sysctl(struct net
*net
,
365 const char *path
, struct ctl_table
*table
)
369 static inline void unregister_net_sysctl_table(struct ctl_table_header
*header
)
374 static inline int rt_genid_ipv4(struct net
*net
)
376 return atomic_read(&net
->ipv4
.rt_genid
);
379 static inline void rt_genid_bump_ipv4(struct net
*net
)
381 atomic_inc(&net
->ipv4
.rt_genid
);
384 extern void (*__fib6_flush_trees
)(struct net
*net
);
385 static inline void rt_genid_bump_ipv6(struct net
*net
)
387 if (__fib6_flush_trees
)
388 __fib6_flush_trees(net
);
391 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
392 static inline struct netns_ieee802154_lowpan
*
393 net_ieee802154_lowpan(struct net
*net
)
395 return &net
->ieee802154_lowpan
;
399 /* For callers who don't really care about whether it's IPv4 or IPv6 */
400 static inline void rt_genid_bump_all(struct net
*net
)
402 rt_genid_bump_ipv4(net
);
403 rt_genid_bump_ipv6(net
);
406 static inline int fnhe_genid(struct net
*net
)
408 return atomic_read(&net
->fnhe_genid
);
411 static inline void fnhe_genid_bump(struct net
*net
)
413 atomic_inc(&net
->fnhe_genid
);
416 #endif /* __NET_NET_NAMESPACE_H */