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