| 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/netfilter.h> | 
|---|
| 26 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) | 
|---|
| 27 | #include <net/netns/conntrack.h> | 
|---|
| 28 | #endif | 
|---|
| 29 | #if IS_ENABLED(CONFIG_NF_FLOW_TABLE) | 
|---|
| 30 | #include <net/netns/flow_table.h> | 
|---|
| 31 | #endif | 
|---|
| 32 | #include <net/netns/nftables.h> | 
|---|
| 33 | #include <net/netns/xfrm.h> | 
|---|
| 34 | #include <net/netns/mpls.h> | 
|---|
| 35 | #include <net/netns/can.h> | 
|---|
| 36 | #include <net/netns/xdp.h> | 
|---|
| 37 | #include <net/netns/smc.h> | 
|---|
| 38 | #include <net/netns/bpf.h> | 
|---|
| 39 | #include <net/netns/mctp.h> | 
|---|
| 40 | #include <net/net_trackers.h> | 
|---|
| 41 | #include <linux/ns_common.h> | 
|---|
| 42 | #include <linux/idr.h> | 
|---|
| 43 | #include <linux/skbuff.h> | 
|---|
| 44 | #include <linux/notifier.h> | 
|---|
| 45 | #include <linux/xarray.h> | 
|---|
| 46 |  | 
|---|
| 47 | struct user_namespace; | 
|---|
| 48 | struct proc_dir_entry; | 
|---|
| 49 | struct net_device; | 
|---|
| 50 | struct sock; | 
|---|
| 51 | struct ; | 
|---|
| 52 | struct net_generic; | 
|---|
| 53 | struct uevent_sock; | 
|---|
| 54 | struct netns_ipvs; | 
|---|
| 55 | struct bpf_prog; | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | #define NETDEV_HASHBITS    8 | 
|---|
| 59 | #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS) | 
|---|
| 60 |  | 
|---|
| 61 | struct net { | 
|---|
| 62 | /* First cache line can be often dirtied. | 
|---|
| 63 | * Do not place here read-mostly fields. | 
|---|
| 64 | */ | 
|---|
| 65 | refcount_t		passive;	/* To decide when the network | 
|---|
| 66 | * namespace should be freed. | 
|---|
| 67 | */ | 
|---|
| 68 | spinlock_t		rules_mod_lock; | 
|---|
| 69 |  | 
|---|
| 70 | unsigned int		dev_base_seq;	/* protected by rtnl_mutex */ | 
|---|
| 71 | u32			ifindex; | 
|---|
| 72 |  | 
|---|
| 73 | spinlock_t		nsid_lock; | 
|---|
| 74 | atomic_t		fnhe_genid; | 
|---|
| 75 |  | 
|---|
| 76 | struct list_head	list;		/* list of network namespaces */ | 
|---|
| 77 | struct list_head	exit_list;	/* To linked to call pernet exit | 
|---|
| 78 | * methods on dead net ( | 
|---|
| 79 | * pernet_ops_rwsem read locked), | 
|---|
| 80 | * or to unregister pernet ops | 
|---|
| 81 | * (pernet_ops_rwsem write locked). | 
|---|
| 82 | */ | 
|---|
| 83 | struct llist_node	defer_free_list; | 
|---|
| 84 | struct llist_node	cleanup_list;	/* namespaces on death row */ | 
|---|
| 85 |  | 
|---|
| 86 | struct list_head ptype_all; | 
|---|
| 87 | struct list_head ptype_specific; | 
|---|
| 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 | struct ref_tracker_dir  refcnt_tracker; | 
|---|
| 98 | struct ref_tracker_dir  notrefcnt_tracker; /* tracker for objects not | 
|---|
| 99 | * refcounted against netns | 
|---|
| 100 | */ | 
|---|
| 101 | struct list_head 	dev_base_head; | 
|---|
| 102 | struct proc_dir_entry 	*proc_net; | 
|---|
| 103 | struct proc_dir_entry 	*proc_net_stat; | 
|---|
| 104 |  | 
|---|
| 105 | #ifdef CONFIG_SYSCTL | 
|---|
| 106 | struct ctl_table_set	sysctls; | 
|---|
| 107 | #endif | 
|---|
| 108 |  | 
|---|
| 109 | struct sock 		*rtnl;			/* rtnetlink socket */ | 
|---|
| 110 | struct sock		*genl_sock; | 
|---|
| 111 |  | 
|---|
| 112 | struct uevent_sock	*uevent_sock;		/* uevent socket */ | 
|---|
| 113 |  | 
|---|
| 114 | struct hlist_head 	*dev_name_head; | 
|---|
| 115 | struct hlist_head	*dev_index_head; | 
|---|
| 116 | struct xarray		dev_by_index; | 
|---|
| 117 | struct raw_notifier_head	netdev_chain; | 
|---|
| 118 |  | 
|---|
| 119 | /* Note that @hash_mix can be read millions times per second, | 
|---|
| 120 | * it is critical that it is on a read_mostly cache line. | 
|---|
| 121 | */ | 
|---|
| 122 | u32			hash_mix; | 
|---|
| 123 |  | 
|---|
| 124 | struct net_device       *loopback_dev;          /* The loopback */ | 
|---|
| 125 |  | 
|---|
| 126 | /* core fib_rules */ | 
|---|
| 127 | struct list_head	rules_ops; | 
|---|
| 128 |  | 
|---|
| 129 | struct netns_core	core; | 
|---|
| 130 | struct netns_mib	mib; | 
|---|
| 131 | struct netns_packet	packet; | 
|---|
| 132 | #if IS_ENABLED(CONFIG_UNIX) | 
|---|
| 133 | struct netns_unix	unx; | 
|---|
| 134 | #endif | 
|---|
| 135 | struct netns_nexthop	nexthop; | 
|---|
| 136 | struct netns_ipv4	ipv4; | 
|---|
| 137 | #if IS_ENABLED(CONFIG_IPV6) | 
|---|
| 138 | struct netns_ipv6	ipv6; | 
|---|
| 139 | #endif | 
|---|
| 140 | #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) | 
|---|
| 141 | struct netns_ieee802154_lowpan	ieee802154_lowpan; | 
|---|
| 142 | #endif | 
|---|
| 143 | #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE) | 
|---|
| 144 | struct netns_sctp	sctp; | 
|---|
| 145 | #endif | 
|---|
| 146 | #ifdef CONFIG_NETFILTER | 
|---|
| 147 | struct netns_nf		nf; | 
|---|
| 148 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) | 
|---|
| 149 | struct netns_ct		ct; | 
|---|
| 150 | #endif | 
|---|
| 151 | #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE) | 
|---|
| 152 | struct netns_nftables	nft; | 
|---|
| 153 | #endif | 
|---|
| 154 | #if IS_ENABLED(CONFIG_NF_FLOW_TABLE) | 
|---|
| 155 | struct netns_ft ft; | 
|---|
| 156 | #endif | 
|---|
| 157 | #endif | 
|---|
| 158 | #ifdef CONFIG_WEXT_CORE | 
|---|
| 159 | struct sk_buff_head	wext_nlevents; | 
|---|
| 160 | #endif | 
|---|
| 161 | struct net_generic __rcu	*gen; | 
|---|
| 162 |  | 
|---|
| 163 | /* Used to store attached BPF programs */ | 
|---|
| 164 | struct netns_bpf	bpf; | 
|---|
| 165 |  | 
|---|
| 166 | /* Note : following structs are cache line aligned */ | 
|---|
| 167 | #ifdef CONFIG_XFRM | 
|---|
| 168 | struct netns_xfrm	xfrm; | 
|---|
| 169 | #endif | 
|---|
| 170 |  | 
|---|
| 171 | u64			net_cookie; /* written once */ | 
|---|
| 172 |  | 
|---|
| 173 | #if IS_ENABLED(CONFIG_IP_VS) | 
|---|
| 174 | struct netns_ipvs	*ipvs; | 
|---|
| 175 | #endif | 
|---|
| 176 | #if IS_ENABLED(CONFIG_MPLS) | 
|---|
| 177 | struct netns_mpls	mpls; | 
|---|
| 178 | #endif | 
|---|
| 179 | #if IS_ENABLED(CONFIG_CAN) | 
|---|
| 180 | struct netns_can	can; | 
|---|
| 181 | #endif | 
|---|
| 182 | #ifdef CONFIG_XDP_SOCKETS | 
|---|
| 183 | struct netns_xdp	xdp; | 
|---|
| 184 | #endif | 
|---|
| 185 | #if IS_ENABLED(CONFIG_MCTP) | 
|---|
| 186 | struct netns_mctp	mctp; | 
|---|
| 187 | #endif | 
|---|
| 188 | #if IS_ENABLED(CONFIG_CRYPTO_USER) | 
|---|
| 189 | struct sock		*crypto_nlsk; | 
|---|
| 190 | #endif | 
|---|
| 191 | struct sock		*diag_nlsk; | 
|---|
| 192 | #if IS_ENABLED(CONFIG_SMC) | 
|---|
| 193 | struct netns_smc	smc; | 
|---|
| 194 | #endif | 
|---|
| 195 | #ifdef CONFIG_DEBUG_NET_SMALL_RTNL | 
|---|
| 196 | /* Move to a better place when the config guard is removed. */ | 
|---|
| 197 | struct mutex		rtnl_mutex; | 
|---|
| 198 | #endif | 
|---|
| 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(u64 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 | struct net *get_net_ns_by_fd(int fd); | 
|---|
| 216 | extern struct task_struct *cleanup_net_task; | 
|---|
| 217 |  | 
|---|
| 218 | #else /* CONFIG_NET_NS */ | 
|---|
| 219 | #include <linux/sched.h> | 
|---|
| 220 | #include <linux/nsproxy.h> | 
|---|
| 221 | static inline struct net *copy_net_ns(u64 flags, | 
|---|
| 222 | struct user_namespace *user_ns, struct net *old_net) | 
|---|
| 223 | { | 
|---|
| 224 | if (flags & CLONE_NEWNET) | 
|---|
| 225 | return ERR_PTR(-EINVAL); | 
|---|
| 226 | return old_net; | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | static inline void net_ns_get_ownership(const struct net *net, | 
|---|
| 230 | kuid_t *uid, kgid_t *gid) | 
|---|
| 231 | { | 
|---|
| 232 | *uid = GLOBAL_ROOT_UID; | 
|---|
| 233 | *gid = GLOBAL_ROOT_GID; | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | static inline void net_ns_barrier(void) {} | 
|---|
| 237 |  | 
|---|
| 238 | static inline struct ns_common *get_net_ns(struct ns_common *ns) | 
|---|
| 239 | { | 
|---|
| 240 | return ERR_PTR(-EINVAL); | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | static inline struct net *get_net_ns_by_fd(int fd) | 
|---|
| 244 | { | 
|---|
| 245 | return ERR_PTR(-EINVAL); | 
|---|
| 246 | } | 
|---|
| 247 | #endif /* CONFIG_NET_NS */ | 
|---|
| 248 |  | 
|---|
| 249 |  | 
|---|
| 250 | extern struct list_head net_namespace_list; | 
|---|
| 251 |  | 
|---|
| 252 | struct net *get_net_ns_by_pid(pid_t pid); | 
|---|
| 253 |  | 
|---|
| 254 | #ifdef CONFIG_SYSCTL | 
|---|
| 255 | void ipx_register_sysctl(void); | 
|---|
| 256 | void ipx_unregister_sysctl(void); | 
|---|
| 257 | #else | 
|---|
| 258 | #define ipx_register_sysctl() | 
|---|
| 259 | #define ipx_unregister_sysctl() | 
|---|
| 260 | #endif | 
|---|
| 261 |  | 
|---|
| 262 | #ifdef CONFIG_NET_NS | 
|---|
| 263 | void __put_net(struct net *net); | 
|---|
| 264 |  | 
|---|
| 265 | static inline struct net *to_net_ns(struct ns_common *ns) | 
|---|
| 266 | { | 
|---|
| 267 | return container_of(ns, struct net, ns); | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | /* Try using get_net_track() instead */ | 
|---|
| 271 | static inline struct net *get_net(struct net *net) | 
|---|
| 272 | { | 
|---|
| 273 | ns_ref_inc(net); | 
|---|
| 274 | return net; | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | static inline struct net *maybe_get_net(struct net *net) | 
|---|
| 278 | { | 
|---|
| 279 | /* Used when we know struct net exists but we | 
|---|
| 280 | * aren't guaranteed a previous reference count | 
|---|
| 281 | * exists.  If the reference count is zero this | 
|---|
| 282 | * function fails and returns NULL. | 
|---|
| 283 | */ | 
|---|
| 284 | if (!ns_ref_get(net)) | 
|---|
| 285 | net = NULL; | 
|---|
| 286 | return net; | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | /* Try using put_net_track() instead */ | 
|---|
| 290 | static inline void put_net(struct net *net) | 
|---|
| 291 | { | 
|---|
| 292 | if (ns_ref_put(net)) | 
|---|
| 293 | __put_net(net); | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 | static inline | 
|---|
| 297 | int net_eq(const struct net *net1, const struct net *net2) | 
|---|
| 298 | { | 
|---|
| 299 | return net1 == net2; | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 | static inline int check_net(const struct net *net) | 
|---|
| 303 | { | 
|---|
| 304 | return ns_ref_read(net) != 0; | 
|---|
| 305 | } | 
|---|
| 306 |  | 
|---|
| 307 | void net_drop_ns(void *); | 
|---|
| 308 | void net_passive_dec(struct net *net); | 
|---|
| 309 |  | 
|---|
| 310 | #else | 
|---|
| 311 |  | 
|---|
| 312 | static inline struct net *get_net(struct net *net) | 
|---|
| 313 | { | 
|---|
| 314 | return net; | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | static inline void put_net(struct net *net) | 
|---|
| 318 | { | 
|---|
| 319 | } | 
|---|
| 320 |  | 
|---|
| 321 | static inline struct net *maybe_get_net(struct net *net) | 
|---|
| 322 | { | 
|---|
| 323 | return net; | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 | static inline | 
|---|
| 327 | int net_eq(const struct net *net1, const struct net *net2) | 
|---|
| 328 | { | 
|---|
| 329 | return 1; | 
|---|
| 330 | } | 
|---|
| 331 |  | 
|---|
| 332 | static inline int check_net(const struct net *net) | 
|---|
| 333 | { | 
|---|
| 334 | return 1; | 
|---|
| 335 | } | 
|---|
| 336 |  | 
|---|
| 337 | #define net_drop_ns NULL | 
|---|
| 338 |  | 
|---|
| 339 | static inline void net_passive_dec(struct net *net) | 
|---|
| 340 | { | 
|---|
| 341 | refcount_dec(&net->passive); | 
|---|
| 342 | } | 
|---|
| 343 | #endif | 
|---|
| 344 |  | 
|---|
| 345 | static inline void net_passive_inc(struct net *net) | 
|---|
| 346 | { | 
|---|
| 347 | refcount_inc(r: &net->passive); | 
|---|
| 348 | } | 
|---|
| 349 |  | 
|---|
| 350 | /* Returns true if the netns initialization is completed successfully */ | 
|---|
| 351 | static inline bool net_initialized(const struct net *net) | 
|---|
| 352 | { | 
|---|
| 353 | return READ_ONCE(net->list.next); | 
|---|
| 354 | } | 
|---|
| 355 |  | 
|---|
| 356 | static inline void __netns_tracker_alloc(struct net *net, | 
|---|
| 357 | netns_tracker *tracker, | 
|---|
| 358 | bool refcounted, | 
|---|
| 359 | gfp_t gfp) | 
|---|
| 360 | { | 
|---|
| 361 | #ifdef CONFIG_NET_NS_REFCNT_TRACKER | 
|---|
| 362 | ref_tracker_alloc(refcounted ? &net->refcnt_tracker : | 
|---|
| 363 | &net->notrefcnt_tracker, | 
|---|
| 364 | tracker, gfp); | 
|---|
| 365 | #endif | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 | static inline void netns_tracker_alloc(struct net *net, netns_tracker *tracker, | 
|---|
| 369 | gfp_t gfp) | 
|---|
| 370 | { | 
|---|
| 371 | __netns_tracker_alloc(net, tracker, refcounted: true, gfp); | 
|---|
| 372 | } | 
|---|
| 373 |  | 
|---|
| 374 | static inline void __netns_tracker_free(struct net *net, | 
|---|
| 375 | netns_tracker *tracker, | 
|---|
| 376 | bool refcounted) | 
|---|
| 377 | { | 
|---|
| 378 | #ifdef CONFIG_NET_NS_REFCNT_TRACKER | 
|---|
| 379 | ref_tracker_free(refcounted ? &net->refcnt_tracker : | 
|---|
| 380 | &net->notrefcnt_tracker, tracker); | 
|---|
| 381 | #endif | 
|---|
| 382 | } | 
|---|
| 383 |  | 
|---|
| 384 | static inline struct net *get_net_track(struct net *net, | 
|---|
| 385 | netns_tracker *tracker, gfp_t gfp) | 
|---|
| 386 | { | 
|---|
| 387 | get_net(net); | 
|---|
| 388 | netns_tracker_alloc(net, tracker, gfp); | 
|---|
| 389 | return net; | 
|---|
| 390 | } | 
|---|
| 391 |  | 
|---|
| 392 | static inline void put_net_track(struct net *net, netns_tracker *tracker) | 
|---|
| 393 | { | 
|---|
| 394 | __netns_tracker_free(net, tracker, refcounted: true); | 
|---|
| 395 | put_net(net); | 
|---|
| 396 | } | 
|---|
| 397 |  | 
|---|
| 398 | typedef struct { | 
|---|
| 399 | #ifdef CONFIG_NET_NS | 
|---|
| 400 | struct net __rcu *net; | 
|---|
| 401 | #endif | 
|---|
| 402 | } possible_net_t; | 
|---|
| 403 |  | 
|---|
| 404 | static inline void write_pnet(possible_net_t *pnet, struct net *net) | 
|---|
| 405 | { | 
|---|
| 406 | #ifdef CONFIG_NET_NS | 
|---|
| 407 | rcu_assign_pointer(pnet->net, net); | 
|---|
| 408 | #endif | 
|---|
| 409 | } | 
|---|
| 410 |  | 
|---|
| 411 | static inline struct net *read_pnet(const possible_net_t *pnet) | 
|---|
| 412 | { | 
|---|
| 413 | #ifdef CONFIG_NET_NS | 
|---|
| 414 | return rcu_dereference_protected(pnet->net, true); | 
|---|
| 415 | #else | 
|---|
| 416 | return &init_net; | 
|---|
| 417 | #endif | 
|---|
| 418 | } | 
|---|
| 419 |  | 
|---|
| 420 | static inline struct net *read_pnet_rcu(const possible_net_t *pnet) | 
|---|
| 421 | { | 
|---|
| 422 | #ifdef CONFIG_NET_NS | 
|---|
| 423 | return rcu_dereference(pnet->net); | 
|---|
| 424 | #else | 
|---|
| 425 | return &init_net; | 
|---|
| 426 | #endif | 
|---|
| 427 | } | 
|---|
| 428 |  | 
|---|
| 429 | /* Protected by net_rwsem */ | 
|---|
| 430 | #define for_each_net(VAR)				\ | 
|---|
| 431 | list_for_each_entry(VAR, &net_namespace_list, list) | 
|---|
| 432 | #define for_each_net_continue_reverse(VAR)		\ | 
|---|
| 433 | list_for_each_entry_continue_reverse(VAR, &net_namespace_list, list) | 
|---|
| 434 | #define for_each_net_rcu(VAR)				\ | 
|---|
| 435 | list_for_each_entry_rcu(VAR, &net_namespace_list, list) | 
|---|
| 436 |  | 
|---|
| 437 | #ifdef CONFIG_NET_NS | 
|---|
| 438 | #define __net_init | 
|---|
| 439 | #define __net_exit | 
|---|
| 440 | #define __net_initdata | 
|---|
| 441 | #define __net_initconst | 
|---|
| 442 | #else | 
|---|
| 443 | #define __net_init	__init | 
|---|
| 444 | #define __net_exit	__ref | 
|---|
| 445 | #define __net_initdata	__initdata | 
|---|
| 446 | #define __net_initconst	__initconst | 
|---|
| 447 | #endif | 
|---|
| 448 |  | 
|---|
| 449 | int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp); | 
|---|
| 450 | int peernet2id(const struct net *net, struct net *peer); | 
|---|
| 451 | bool peernet_has_id(const struct net *net, struct net *peer); | 
|---|
| 452 | struct net *get_net_ns_by_id(const struct net *net, int id); | 
|---|
| 453 |  | 
|---|
| 454 | struct pernet_operations { | 
|---|
| 455 | struct list_head list; | 
|---|
| 456 | /* | 
|---|
| 457 | * Below methods are called without any exclusive locks. | 
|---|
| 458 | * More than one net may be constructed and destructed | 
|---|
| 459 | * in parallel on several cpus. Every pernet_operations | 
|---|
| 460 | * have to keep in mind all other pernet_operations and | 
|---|
| 461 | * to introduce a locking, if they share common resources. | 
|---|
| 462 | * | 
|---|
| 463 | * The only time they are called with exclusive lock is | 
|---|
| 464 | * from register_pernet_subsys(), unregister_pernet_subsys() | 
|---|
| 465 | * register_pernet_device() and unregister_pernet_device(). | 
|---|
| 466 | * | 
|---|
| 467 | * Exit methods using blocking RCU primitives, such as | 
|---|
| 468 | * synchronize_rcu(), should be implemented via exit_batch. | 
|---|
| 469 | * Then, destruction of a group of net requires single | 
|---|
| 470 | * synchronize_rcu() related to these pernet_operations, | 
|---|
| 471 | * instead of separate synchronize_rcu() for every net. | 
|---|
| 472 | * Please, avoid synchronize_rcu() at all, where it's possible. | 
|---|
| 473 | * | 
|---|
| 474 | * Note that a combination of pre_exit() and exit() can | 
|---|
| 475 | * be used, since a synchronize_rcu() is guaranteed between | 
|---|
| 476 | * the calls. | 
|---|
| 477 | */ | 
|---|
| 478 | int (*init)(struct net *net); | 
|---|
| 479 | void (*pre_exit)(struct net *net); | 
|---|
| 480 | void (*exit)(struct net *net); | 
|---|
| 481 | void (*exit_batch)(struct list_head *net_exit_list); | 
|---|
| 482 | /* Following method is called with RTNL held. */ | 
|---|
| 483 | void (*exit_rtnl)(struct net *net, | 
|---|
| 484 | struct list_head *dev_kill_list); | 
|---|
| 485 | unsigned int * const id; | 
|---|
| 486 | const size_t size; | 
|---|
| 487 | }; | 
|---|
| 488 |  | 
|---|
| 489 | /* | 
|---|
| 490 | * Use these carefully.  If you implement a network device and it | 
|---|
| 491 | * needs per network namespace operations use device pernet operations, | 
|---|
| 492 | * otherwise use pernet subsys operations. | 
|---|
| 493 | * | 
|---|
| 494 | * Network interfaces need to be removed from a dying netns _before_ | 
|---|
| 495 | * subsys notifiers can be called, as most of the network code cleanup | 
|---|
| 496 | * (which is done from subsys notifiers) runs with the assumption that | 
|---|
| 497 | * dev_remove_pack has been called so no new packets will arrive during | 
|---|
| 498 | * and after the cleanup functions have been called.  dev_remove_pack | 
|---|
| 499 | * is not per namespace so instead the guarantee of no more packets | 
|---|
| 500 | * arriving in a network namespace is provided by ensuring that all | 
|---|
| 501 | * network devices and all sockets have left the network namespace | 
|---|
| 502 | * before the cleanup methods are called. | 
|---|
| 503 | * | 
|---|
| 504 | * For the longest time the ipv4 icmp code was registered as a pernet | 
|---|
| 505 | * device which caused kernel oops, and panics during network | 
|---|
| 506 | * namespace cleanup.   So please don't get this wrong. | 
|---|
| 507 | */ | 
|---|
| 508 | int register_pernet_subsys(struct pernet_operations *); | 
|---|
| 509 | void unregister_pernet_subsys(struct pernet_operations *); | 
|---|
| 510 | int register_pernet_device(struct pernet_operations *); | 
|---|
| 511 | void unregister_pernet_device(struct pernet_operations *); | 
|---|
| 512 |  | 
|---|
| 513 | struct ctl_table; | 
|---|
| 514 |  | 
|---|
| 515 | #define register_net_sysctl(net, path, table)	\ | 
|---|
| 516 | register_net_sysctl_sz(net, path, table, ARRAY_SIZE(table)) | 
|---|
| 517 | #ifdef CONFIG_SYSCTL | 
|---|
| 518 | int net_sysctl_init(void); | 
|---|
| 519 | struct ctl_table_header *register_net_sysctl_sz(struct net *net, const char *path, | 
|---|
| 520 | struct ctl_table *table, size_t table_size); | 
|---|
| 521 | void unregister_net_sysctl_table(struct ctl_table_header *); | 
|---|
| 522 | #else | 
|---|
| 523 | static inline int net_sysctl_init(void) { return 0; } | 
|---|
| 524 | static inline struct ctl_table_header *register_net_sysctl_sz(struct net *net, | 
|---|
| 525 | const char *path, struct ctl_table *table, size_t table_size) | 
|---|
| 526 | { | 
|---|
| 527 | return NULL; | 
|---|
| 528 | } | 
|---|
| 529 | static inline void unregister_net_sysctl_table(struct ctl_table_header *header) | 
|---|
| 530 | { | 
|---|
| 531 | } | 
|---|
| 532 | #endif | 
|---|
| 533 |  | 
|---|
| 534 | static inline int rt_genid_ipv4(const struct net *net) | 
|---|
| 535 | { | 
|---|
| 536 | return atomic_read(v: &net->ipv4.rt_genid); | 
|---|
| 537 | } | 
|---|
| 538 |  | 
|---|
| 539 | #if IS_ENABLED(CONFIG_IPV6) | 
|---|
| 540 | static inline int rt_genid_ipv6(const struct net *net) | 
|---|
| 541 | { | 
|---|
| 542 | return atomic_read(v: &net->ipv6.fib6_sernum); | 
|---|
| 543 | } | 
|---|
| 544 | #endif | 
|---|
| 545 |  | 
|---|
| 546 | static inline void rt_genid_bump_ipv4(struct net *net) | 
|---|
| 547 | { | 
|---|
| 548 | atomic_inc(v: &net->ipv4.rt_genid); | 
|---|
| 549 | } | 
|---|
| 550 |  | 
|---|
| 551 | extern void (*__fib6_flush_trees)(struct net *net); | 
|---|
| 552 | static inline void rt_genid_bump_ipv6(struct net *net) | 
|---|
| 553 | { | 
|---|
| 554 | if (__fib6_flush_trees) | 
|---|
| 555 | __fib6_flush_trees(net); | 
|---|
| 556 | } | 
|---|
| 557 |  | 
|---|
| 558 | #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) | 
|---|
| 559 | static inline struct netns_ieee802154_lowpan * | 
|---|
| 560 | net_ieee802154_lowpan(struct net *net) | 
|---|
| 561 | { | 
|---|
| 562 | return &net->ieee802154_lowpan; | 
|---|
| 563 | } | 
|---|
| 564 | #endif | 
|---|
| 565 |  | 
|---|
| 566 | /* For callers who don't really care about whether it's IPv4 or IPv6 */ | 
|---|
| 567 | static inline void rt_genid_bump_all(struct net *net) | 
|---|
| 568 | { | 
|---|
| 569 | rt_genid_bump_ipv4(net); | 
|---|
| 570 | rt_genid_bump_ipv6(net); | 
|---|
| 571 | } | 
|---|
| 572 |  | 
|---|
| 573 | static inline int fnhe_genid(const struct net *net) | 
|---|
| 574 | { | 
|---|
| 575 | return atomic_read(v: &net->fnhe_genid); | 
|---|
| 576 | } | 
|---|
| 577 |  | 
|---|
| 578 | static inline void fnhe_genid_bump(struct net *net) | 
|---|
| 579 | { | 
|---|
| 580 | atomic_inc(v: &net->fnhe_genid); | 
|---|
| 581 | } | 
|---|
| 582 |  | 
|---|
| 583 | #ifdef CONFIG_NET | 
|---|
| 584 | void net_ns_init(void); | 
|---|
| 585 | #else | 
|---|
| 586 | static inline void net_ns_init(void) {} | 
|---|
| 587 | #endif | 
|---|
| 588 |  | 
|---|
| 589 | #endif /* __NET_NET_NAMESPACE_H */ | 
|---|
| 590 |  | 
|---|