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