1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 25f256becSEric W. Biederman /* 35f256becSEric W. Biederman * Operations on the network namespace 45f256becSEric W. Biederman */ 55f256becSEric W. Biederman #ifndef __NET_NET_NAMESPACE_H 65f256becSEric W. Biederman #define __NET_NET_NAMESPACE_H 75f256becSEric W. Biederman 860063497SArun Sharma #include <linux/atomic.h> 9c122e14dSReshetova, Elena #include <linux/refcount.h> 105f256becSEric W. Biederman #include <linux/workqueue.h> 115f256becSEric W. Biederman #include <linux/list.h> 12bee95250SDavid S. Miller #include <linux/sysctl.h> 13fbdeaed4STyler Hicks #include <linux/uidgid.h> 145f256becSEric W. Biederman 156a662719SCong Wang #include <net/flow.h> 168efa6e93SPavel Emelyanov #include <net/netns/core.h> 17852566f5SPavel Emelyanov #include <net/netns/mib.h> 18a0a53c8bSDenis V. Lunev #include <net/netns/unix.h> 192aaef4e4SDenis V. Lunev #include <net/netns/packet.h> 208afd351cSPavel Emelyanov #include <net/netns/ipv4.h> 21b0f159dbSDaniel Lezcano #include <net/netns/ipv6.h> 22ab84be7eSDavid Ahern #include <net/netns/nexthop.h> 23633fc86fSAlexander Aring #include <net/netns/ieee802154_6lowpan.h> 244db67e80SEric W. Biederman #include <net/netns/sctp.h> 25f3c1a44aSGao feng #include <net/netns/netfilter.h> 268d870052SAlexey Dobriyan #include <net/netns/x_tables.h> 27dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 28dfdb8d79SAlexey Dobriyan #include <net/netns/conntrack.h> 29dfdb8d79SAlexey Dobriyan #endif 3099633ab2SPablo Neira Ayuso #include <net/netns/nftables.h> 31d62ddc21SAlexey Dobriyan #include <net/netns/xfrm.h> 320189197fSEric W. Biederman #include <net/netns/mpls.h> 338e8cda6dSMario Kicherer #include <net/netns/can.h> 341d0dc069SBjörn Töpel #include <net/netns/xdp.h> 35a3fd7ceeSJakub Sitnicki #include <net/netns/bpf.h> 36435d5f4bSAl Viro #include <linux/ns_common.h> 3704c52decSPablo Neira Ayuso #include <linux/idr.h> 3804c52decSPablo Neira Ayuso #include <linux/skbuff.h> 39a30c7b42SJiri Pirko #include <linux/notifier.h> 40a0a53c8bSDenis V. Lunev 41038e7332SEric W. Biederman struct user_namespace; 42457c4cbcSEric W. Biederman struct proc_dir_entry; 432774c7abSEric W. Biederman struct net_device; 4497c53cacSDenis V. Lunev struct sock; 451597fbc0SPavel Emelyanov struct ctl_table_header; 46dec827d1SPavel Emelyanov struct net_generic; 4794e5e308SChristian Brauner struct uevent_sock; 482553d064SJulian Anastasov struct netns_ipvs; 49d58e468bSPetar Penkov struct bpf_prog; 501597fbc0SPavel Emelyanov 517c28bd0bSEric Dumazet 527c28bd0bSEric Dumazet #define NETDEV_HASHBITS 8 537c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS) 547c28bd0bSEric Dumazet 555f256becSEric W. Biederman struct net { 562a06b898SEric Dumazet /* First cache line can be often dirtied. 572a06b898SEric Dumazet * Do not place here read-mostly fields. 582a06b898SEric Dumazet */ 59aad12c23SMike Rapoport refcount_t passive; /* To decide when the network 605f256becSEric W. Biederman * namespace should be freed. 615f256becSEric W. Biederman */ 628e602ce2SEric Dumazet spinlock_t rules_mod_lock; 638e602ce2SEric Dumazet 642a06b898SEric Dumazet unsigned int dev_unreg_count; 652a06b898SEric Dumazet 662a06b898SEric Dumazet unsigned int dev_base_seq; /* protected by rtnl_mutex */ 672a06b898SEric Dumazet int ifindex; 682a06b898SEric Dumazet 692a06b898SEric Dumazet spinlock_t nsid_lock; 702a06b898SEric Dumazet atomic_t fnhe_genid; 7133cf7c90SEric Dumazet 725f256becSEric W. Biederman struct list_head list; /* list of network namespaces */ 7319efbd93SKirill Tkhai struct list_head exit_list; /* To linked to call pernet exit 744420bf21SKirill Tkhai * methods on dead net ( 754420bf21SKirill Tkhai * pernet_ops_rwsem read locked), 764420bf21SKirill Tkhai * or to unregister pernet ops 774420bf21SKirill Tkhai * (pernet_ops_rwsem write locked). 7819efbd93SKirill Tkhai */ 7965b7b5b9SKirill Tkhai struct llist_node cleanup_list; /* namespaces on death row */ 8065b7b5b9SKirill Tkhai 819b242610SDavid Howells #ifdef CONFIG_KEYS 829b242610SDavid Howells struct key_tag *key_domain; /* Key domain of operation tag */ 839b242610SDavid Howells #endif 84038e7332SEric W. Biederman struct user_namespace *user_ns; /* Owning user namespace */ 8570328660SEric W. Biederman struct ucounts *ucounts; 860c7aecd4SNicolas Dichtel struct idr netns_ids; 87038e7332SEric W. Biederman 88435d5f4bSAl Viro struct ns_common ns; 8998f842e6SEric W. Biederman 902a06b898SEric Dumazet struct list_head dev_base_head; 91457c4cbcSEric W. Biederman struct proc_dir_entry *proc_net; 92457c4cbcSEric W. Biederman struct proc_dir_entry *proc_net_stat; 93881d966bSEric W. Biederman 9473455092SAl Viro #ifdef CONFIG_SYSCTL 9573455092SAl Viro struct ctl_table_set sysctls; 9673455092SAl Viro #endif 9795bdfccbSEric W. Biederman 988e602ce2SEric Dumazet struct sock *rtnl; /* rtnetlink socket */ 998e602ce2SEric Dumazet struct sock *genl_sock; 1002774c7abSEric W. Biederman 10194e5e308SChristian Brauner struct uevent_sock *uevent_sock; /* uevent socket */ 10294e5e308SChristian Brauner 103881d966bSEric W. Biederman struct hlist_head *dev_name_head; 104881d966bSEric W. Biederman struct hlist_head *dev_index_head; 105a30c7b42SJiri Pirko struct raw_notifier_head netdev_chain; 106a30c7b42SJiri Pirko 1072a06b898SEric Dumazet /* Note that @hash_mix can be read millions times per second, 1082a06b898SEric Dumazet * it is critical that it is on a read_mostly cache line. 1092a06b898SEric Dumazet */ 1102a06b898SEric Dumazet u32 hash_mix; 1112a06b898SEric Dumazet 1122a06b898SEric Dumazet struct net_device *loopback_dev; /* The loopback */ 11397c53cacSDenis V. Lunev 1145fd30ee7SDenis V. Lunev /* core fib_rules */ 1155fd30ee7SDenis V. Lunev struct list_head rules_ops; 1165fd30ee7SDenis V. Lunev 1178efa6e93SPavel Emelyanov struct netns_core core; 118852566f5SPavel Emelyanov struct netns_mib mib; 1192aaef4e4SDenis V. Lunev struct netns_packet packet; 120a0a53c8bSDenis V. Lunev struct netns_unix unx; 121ab84be7eSDavid Ahern struct netns_nexthop nexthop; 1228afd351cSPavel Emelyanov struct netns_ipv4 ipv4; 123dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 124b0f159dbSDaniel Lezcano struct netns_ipv6 ipv6; 125b0f159dbSDaniel Lezcano #endif 126633fc86fSAlexander Aring #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) 127633fc86fSAlexander Aring struct netns_ieee802154_lowpan ieee802154_lowpan; 128633fc86fSAlexander Aring #endif 1294db67e80SEric W. Biederman #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE) 1304db67e80SEric W. Biederman struct netns_sctp sctp; 1314db67e80SEric W. Biederman #endif 1328d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER 133f3c1a44aSGao feng struct netns_nf nf; 1348d870052SAlexey Dobriyan struct netns_xt xt; 135dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 136dfdb8d79SAlexey Dobriyan struct netns_ct ct; 137dfdb8d79SAlexey Dobriyan #endif 13899633ab2SPablo Neira Ayuso #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE) 13999633ab2SPablo Neira Ayuso struct netns_nftables nft; 14099633ab2SPablo Neira Ayuso #endif 1418d870052SAlexey Dobriyan #endif 1423d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE 143b333b3d2SJohannes Berg struct sk_buff_head wext_nlevents; 144b333b3d2SJohannes Berg #endif 1451c87733dSEric Dumazet struct net_generic __rcu *gen; 1468e602ce2SEric Dumazet 147a3fd7ceeSJakub Sitnicki /* Used to store attached BPF programs */ 148a3fd7ceeSJakub Sitnicki struct netns_bpf bpf; 149d58e468bSPetar Penkov 1508e602ce2SEric Dumazet /* Note : following structs are cache line aligned */ 1518e602ce2SEric Dumazet #ifdef CONFIG_XFRM 1528e602ce2SEric Dumazet struct netns_xfrm xfrm; 1538e602ce2SEric Dumazet #endif 154f318903cSDaniel Borkmann 1553d368ab8SEric Dumazet u64 net_cookie; /* written once */ 156f318903cSDaniel Borkmann 1578b4d14d8SJunweiZhang #if IS_ENABLED(CONFIG_IP_VS) 15861b1ab45SHans Schillstrom struct netns_ipvs *ipvs; 1598b4d14d8SJunweiZhang #endif 1600189197fSEric W. Biederman #if IS_ENABLED(CONFIG_MPLS) 1610189197fSEric W. Biederman struct netns_mpls mpls; 1620189197fSEric W. Biederman #endif 1638e8cda6dSMario Kicherer #if IS_ENABLED(CONFIG_CAN) 1648e8cda6dSMario Kicherer struct netns_can can; 1658e8cda6dSMario Kicherer #endif 1661d0dc069SBjörn Töpel #ifdef CONFIG_XDP_SOCKETS 1671d0dc069SBjörn Töpel struct netns_xdp xdp; 1681d0dc069SBjörn Töpel #endif 16991b05a7eSOndrej Mosnacek #if IS_ENABLED(CONFIG_CRYPTO_USER) 17091b05a7eSOndrej Mosnacek struct sock *crypto_nlsk; 17191b05a7eSOndrej Mosnacek #endif 17251d7cccfSAndrey Vagin struct sock *diag_nlsk; 1733859a271SKees Cook } __randomize_layout; 1745f256becSEric W. Biederman 175c0f39322SDenis V. Lunev #include <linux/seq_file_net.h> 176c0f39322SDenis V. Lunev 1774fabcd71SDaniel Lezcano /* Init's network namespace */ 1785f256becSEric W. Biederman extern struct net init_net; 179a4aa834aSDenis V. Lunev 180d727abcbSEric W. Biederman #ifdef CONFIG_NET_NS 181e67e16eaSJoe Perches struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns, 182e67e16eaSJoe Perches struct net *old_net); 183225c0a01SDenis V. Lunev 184fbdeaed4STyler Hicks void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid); 185fbdeaed4STyler Hicks 1867866cc57SFlorian Westphal void net_ns_barrier(void); 187ea6932d7SChangbin Du 188ea6932d7SChangbin Du struct ns_common *get_net_ns(struct ns_common *ns); 189*e34492deSChangbin Du struct net *get_net_ns_by_fd(int fd); 190d727abcbSEric W. Biederman #else /* CONFIG_NET_NS */ 191d727abcbSEric W. Biederman #include <linux/sched.h> 192d727abcbSEric W. Biederman #include <linux/nsproxy.h> 193038e7332SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags, 194038e7332SEric W. Biederman struct user_namespace *user_ns, struct net *old_net) 1959dd776b6SEric W. Biederman { 196d727abcbSEric W. Biederman if (flags & CLONE_NEWNET) 197d727abcbSEric W. Biederman return ERR_PTR(-EINVAL); 198d727abcbSEric W. Biederman return old_net; 1999dd776b6SEric W. Biederman } 2007866cc57SFlorian Westphal 201fbdeaed4STyler Hicks static inline void net_ns_get_ownership(const struct net *net, 202fbdeaed4STyler Hicks kuid_t *uid, kgid_t *gid) 203fbdeaed4STyler Hicks { 204fbdeaed4STyler Hicks *uid = GLOBAL_ROOT_UID; 205fbdeaed4STyler Hicks *gid = GLOBAL_ROOT_GID; 206fbdeaed4STyler Hicks } 207fbdeaed4STyler Hicks 2087866cc57SFlorian Westphal static inline void net_ns_barrier(void) {} 209ea6932d7SChangbin Du 210ea6932d7SChangbin Du static inline struct ns_common *get_net_ns(struct ns_common *ns) 211ea6932d7SChangbin Du { 212ea6932d7SChangbin Du return ERR_PTR(-EINVAL); 213ea6932d7SChangbin Du } 214*e34492deSChangbin Du 215*e34492deSChangbin Du static inline struct net *get_net_ns_by_fd(int fd) 216*e34492deSChangbin Du { 217*e34492deSChangbin Du return ERR_PTR(-EINVAL); 218*e34492deSChangbin Du } 219d727abcbSEric W. Biederman #endif /* CONFIG_NET_NS */ 220225c0a01SDenis V. Lunev 221225c0a01SDenis V. Lunev 222225c0a01SDenis V. Lunev extern struct list_head net_namespace_list; 2239dd776b6SEric W. Biederman 224e67e16eaSJoe Perches struct net *get_net_ns_by_pid(pid_t pid); 22530ffee84SJohannes Berg 226535d3ae9SRashika Kheria #ifdef CONFIG_SYSCTL 227535d3ae9SRashika Kheria void ipx_register_sysctl(void); 228535d3ae9SRashika Kheria void ipx_unregister_sysctl(void); 229535d3ae9SRashika Kheria #else 230535d3ae9SRashika Kheria #define ipx_register_sysctl() 231535d3ae9SRashika Kheria #define ipx_unregister_sysctl() 232535d3ae9SRashika Kheria #endif 233535d3ae9SRashika Kheria 234d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS 235e67e16eaSJoe Perches void __put_net(struct net *net); 2365f256becSEric W. Biederman 2375f256becSEric W. Biederman static inline struct net *get_net(struct net *net) 2385f256becSEric W. Biederman { 2398b8f3e66SChristian Brauner refcount_inc(&net->ns.count); 2405f256becSEric W. Biederman return net; 2415f256becSEric W. Biederman } 2425f256becSEric W. Biederman 243077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net) 244077130c0SEric W. Biederman { 245077130c0SEric W. Biederman /* Used when we know struct net exists but we 246077130c0SEric W. Biederman * aren't guaranteed a previous reference count 247077130c0SEric W. Biederman * exists. If the reference count is zero this 248077130c0SEric W. Biederman * function fails and returns NULL. 249077130c0SEric W. Biederman */ 2508b8f3e66SChristian Brauner if (!refcount_inc_not_zero(&net->ns.count)) 251077130c0SEric W. Biederman net = NULL; 252077130c0SEric W. Biederman return net; 253077130c0SEric W. Biederman } 254077130c0SEric W. Biederman 2555f256becSEric W. Biederman static inline void put_net(struct net *net) 2565f256becSEric W. Biederman { 2578b8f3e66SChristian Brauner if (refcount_dec_and_test(&net->ns.count)) 2585f256becSEric W. Biederman __put_net(net); 2595f256becSEric W. Biederman } 2605f256becSEric W. Biederman 261878628fbSYOSHIFUJI Hideaki static inline 262878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2) 263878628fbSYOSHIFUJI Hideaki { 264878628fbSYOSHIFUJI Hideaki return net1 == net2; 265878628fbSYOSHIFUJI Hideaki } 266a685e089SAl Viro 2674ee806d5SDan Streetman static inline int check_net(const struct net *net) 2684ee806d5SDan Streetman { 2698b8f3e66SChristian Brauner return refcount_read(&net->ns.count) != 0; 2704ee806d5SDan Streetman } 2714ee806d5SDan Streetman 272e67e16eaSJoe Perches void net_drop_ns(void *); 273a685e089SAl Viro 274d4655795SPavel Emelyanov #else 275b9f75f45SEric W. Biederman 276d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net) 277d4655795SPavel Emelyanov { 278d4655795SPavel Emelyanov return net; 279d4655795SPavel Emelyanov } 280d4655795SPavel Emelyanov 281d4655795SPavel Emelyanov static inline void put_net(struct net *net) 282d4655795SPavel Emelyanov { 283d4655795SPavel Emelyanov } 284d4655795SPavel Emelyanov 285d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net) 286d4655795SPavel Emelyanov { 287d4655795SPavel Emelyanov return net; 288d4655795SPavel Emelyanov } 289878628fbSYOSHIFUJI Hideaki 290878628fbSYOSHIFUJI Hideaki static inline 291878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2) 292878628fbSYOSHIFUJI Hideaki { 293878628fbSYOSHIFUJI Hideaki return 1; 294878628fbSYOSHIFUJI Hideaki } 295a685e089SAl Viro 2964ee806d5SDan Streetman static inline int check_net(const struct net *net) 2974ee806d5SDan Streetman { 2984ee806d5SDan Streetman return 1; 2994ee806d5SDan Streetman } 3004ee806d5SDan Streetman 301a685e089SAl Viro #define net_drop_ns NULL 302d4655795SPavel Emelyanov #endif 3035f256becSEric W. Biederman 3045d1e4468SDenis V. Lunev 3050c5c9fb5SEric W. Biederman typedef struct { 3068f424b5fSEric Dumazet #ifdef CONFIG_NET_NS 3070c5c9fb5SEric W. Biederman struct net *net; 3088f424b5fSEric Dumazet #endif 3090c5c9fb5SEric W. Biederman } possible_net_t; 3100c5c9fb5SEric W. Biederman 3110c5c9fb5SEric W. Biederman static inline void write_pnet(possible_net_t *pnet, struct net *net) 3120c5c9fb5SEric W. Biederman { 3130c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS 3140c5c9fb5SEric W. Biederman pnet->net = net; 3150c5c9fb5SEric W. Biederman #endif 3160c5c9fb5SEric W. Biederman } 3170c5c9fb5SEric W. Biederman 3180c5c9fb5SEric W. Biederman static inline struct net *read_pnet(const possible_net_t *pnet) 3190c5c9fb5SEric W. Biederman { 3200c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS 3210c5c9fb5SEric W. Biederman return pnet->net; 3220c5c9fb5SEric W. Biederman #else 3230c5c9fb5SEric W. Biederman return &init_net; 3240c5c9fb5SEric W. Biederman #endif 3250c5c9fb5SEric W. Biederman } 3265d1e4468SDenis V. Lunev 327f0b07bb1SKirill Tkhai /* Protected by net_rwsem */ 3285f256becSEric W. Biederman #define for_each_net(VAR) \ 3295f256becSEric W. Biederman list_for_each_entry(VAR, &net_namespace_list, list) 330afa0df59SJiri Pirko #define for_each_net_continue_reverse(VAR) \ 331afa0df59SJiri Pirko list_for_each_entry_continue_reverse(VAR, &net_namespace_list, list) 33211a28d37SJohannes Berg #define for_each_net_rcu(VAR) \ 33311a28d37SJohannes Berg list_for_each_entry_rcu(VAR, &net_namespace_list, list) 33411a28d37SJohannes Berg 3354665079cSPavel Emelyanov #ifdef CONFIG_NET_NS 3364665079cSPavel Emelyanov #define __net_init 3374665079cSPavel Emelyanov #define __net_exit 338022cbae6SDenis V. Lunev #define __net_initdata 33904a6f82cSAndi Kleen #define __net_initconst 3404665079cSPavel Emelyanov #else 3414665079cSPavel Emelyanov #define __net_init __init 342bd721ea7SFabian Frederick #define __net_exit __ref 343022cbae6SDenis V. Lunev #define __net_initdata __initdata 34404a6f82cSAndi Kleen #define __net_initconst __initconst 3454665079cSPavel Emelyanov #endif 3465f256becSEric W. Biederman 347d4e4fdf9SGuillaume Nault int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp); 34856f200c7SGuillaume Nault int peernet2id(const struct net *net, struct net *peer); 34956f200c7SGuillaume Nault bool peernet_has_id(const struct net *net, struct net *peer); 35056f200c7SGuillaume Nault struct net *get_net_ns_by_id(const struct net *net, int id); 3510c7aecd4SNicolas Dichtel 3525f256becSEric W. Biederman struct pernet_operations { 3535f256becSEric W. Biederman struct list_head list; 3546056415dSKirill Tkhai /* 3556056415dSKirill Tkhai * Below methods are called without any exclusive locks. 3566056415dSKirill Tkhai * More than one net may be constructed and destructed 3576056415dSKirill Tkhai * in parallel on several cpus. Every pernet_operations 3586056415dSKirill Tkhai * have to keep in mind all other pernet_operations and 3596056415dSKirill Tkhai * to introduce a locking, if they share common resources. 3606056415dSKirill Tkhai * 3618518e9bbSKirill Tkhai * The only time they are called with exclusive lock is 3628518e9bbSKirill Tkhai * from register_pernet_subsys(), unregister_pernet_subsys() 3638518e9bbSKirill Tkhai * register_pernet_device() and unregister_pernet_device(). 3648518e9bbSKirill Tkhai * 3656056415dSKirill Tkhai * Exit methods using blocking RCU primitives, such as 3666056415dSKirill Tkhai * synchronize_rcu(), should be implemented via exit_batch. 3676056415dSKirill Tkhai * Then, destruction of a group of net requires single 3686056415dSKirill Tkhai * synchronize_rcu() related to these pernet_operations, 3696056415dSKirill Tkhai * instead of separate synchronize_rcu() for every net. 3706056415dSKirill Tkhai * Please, avoid synchronize_rcu() at all, where it's possible. 371d7d99872SEric Dumazet * 372d7d99872SEric Dumazet * Note that a combination of pre_exit() and exit() can 373d7d99872SEric Dumazet * be used, since a synchronize_rcu() is guaranteed between 374d7d99872SEric Dumazet * the calls. 3756056415dSKirill Tkhai */ 3765f256becSEric W. Biederman int (*init)(struct net *net); 377d7d99872SEric Dumazet void (*pre_exit)(struct net *net); 3785f256becSEric W. Biederman void (*exit)(struct net *net); 37972ad937aSEric W. Biederman void (*exit_batch)(struct list_head *net_exit_list); 380c7d03a00SAlexey Dobriyan unsigned int *id; 381f875bae0SEric W. Biederman size_t size; 3825f256becSEric W. Biederman }; 3835f256becSEric W. Biederman 38417edde52SEric W. Biederman /* 38517edde52SEric W. Biederman * Use these carefully. If you implement a network device and it 38617edde52SEric W. Biederman * needs per network namespace operations use device pernet operations, 38717edde52SEric W. Biederman * otherwise use pernet subsys operations. 38817edde52SEric W. Biederman * 3894edf547bSJohannes Berg * Network interfaces need to be removed from a dying netns _before_ 3904edf547bSJohannes Berg * subsys notifiers can be called, as most of the network code cleanup 3914edf547bSJohannes Berg * (which is done from subsys notifiers) runs with the assumption that 3924edf547bSJohannes Berg * dev_remove_pack has been called so no new packets will arrive during 3934edf547bSJohannes Berg * and after the cleanup functions have been called. dev_remove_pack 3944edf547bSJohannes Berg * is not per namespace so instead the guarantee of no more packets 3954edf547bSJohannes Berg * arriving in a network namespace is provided by ensuring that all 3964edf547bSJohannes Berg * network devices and all sockets have left the network namespace 3974edf547bSJohannes Berg * before the cleanup methods are called. 39817edde52SEric W. Biederman * 39917edde52SEric W. Biederman * For the longest time the ipv4 icmp code was registered as a pernet 40017edde52SEric W. Biederman * device which caused kernel oops, and panics during network 40117edde52SEric W. Biederman * namespace cleanup. So please don't get this wrong. 40217edde52SEric W. Biederman */ 403e67e16eaSJoe Perches int register_pernet_subsys(struct pernet_operations *); 404e67e16eaSJoe Perches void unregister_pernet_subsys(struct pernet_operations *); 405e67e16eaSJoe Perches int register_pernet_device(struct pernet_operations *); 406e67e16eaSJoe Perches void unregister_pernet_device(struct pernet_operations *); 407f875bae0SEric W. Biederman 40895bdfccbSEric W. Biederman struct ctl_table; 409d62c612eSPavel Emelyanov 4102ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL 411e67e16eaSJoe Perches int net_sysctl_init(void); 412e67e16eaSJoe Perches struct ctl_table_header *register_net_sysctl(struct net *net, const char *path, 413e67e16eaSJoe Perches struct ctl_table *table); 414e67e16eaSJoe Perches void unregister_net_sysctl_table(struct ctl_table_header *header); 41548c74958SEric W. Biederman #else 41648c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; } 41748c74958SEric W. Biederman static inline struct ctl_table_header *register_net_sysctl(struct net *net, 41848c74958SEric W. Biederman const char *path, struct ctl_table *table) 41948c74958SEric W. Biederman { 42048c74958SEric W. Biederman return NULL; 42148c74958SEric W. Biederman } 42248c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header) 42348c74958SEric W. Biederman { 42448c74958SEric W. Biederman } 42548c74958SEric W. Biederman #endif 42648c74958SEric W. Biederman 42756f200c7SGuillaume Nault static inline int rt_genid_ipv4(const struct net *net) 428b42664f8SNicolas Dichtel { 429ca4c3fc2Sfan.du return atomic_read(&net->ipv4.rt_genid); 430b42664f8SNicolas Dichtel } 431b42664f8SNicolas Dichtel 4328f34e53bSDavid Ahern #if IS_ENABLED(CONFIG_IPV6) 4338f34e53bSDavid Ahern static inline int rt_genid_ipv6(const struct net *net) 4348f34e53bSDavid Ahern { 4358f34e53bSDavid Ahern return atomic_read(&net->ipv6.fib6_sernum); 4368f34e53bSDavid Ahern } 4378f34e53bSDavid Ahern #endif 4388f34e53bSDavid Ahern 439ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv4(struct net *net) 440b42664f8SNicolas Dichtel { 441ca4c3fc2Sfan.du atomic_inc(&net->ipv4.rt_genid); 442ca4c3fc2Sfan.du } 443ca4c3fc2Sfan.du 444705f1c86SHannes Frederic Sowa extern void (*__fib6_flush_trees)(struct net *net); 445ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv6(struct net *net) 446ca4c3fc2Sfan.du { 447705f1c86SHannes Frederic Sowa if (__fib6_flush_trees) 448705f1c86SHannes Frederic Sowa __fib6_flush_trees(net); 449ca4c3fc2Sfan.du } 450ca4c3fc2Sfan.du 451599018a7SLuis R. Rodriguez #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) 452599018a7SLuis R. Rodriguez static inline struct netns_ieee802154_lowpan * 453599018a7SLuis R. Rodriguez net_ieee802154_lowpan(struct net *net) 454599018a7SLuis R. Rodriguez { 455599018a7SLuis R. Rodriguez return &net->ieee802154_lowpan; 456599018a7SLuis R. Rodriguez } 457599018a7SLuis R. Rodriguez #endif 458599018a7SLuis R. Rodriguez 459ca4c3fc2Sfan.du /* For callers who don't really care about whether it's IPv4 or IPv6 */ 460ca4c3fc2Sfan.du static inline void rt_genid_bump_all(struct net *net) 461ca4c3fc2Sfan.du { 462ca4c3fc2Sfan.du rt_genid_bump_ipv4(net); 463ca4c3fc2Sfan.du rt_genid_bump_ipv6(net); 464b42664f8SNicolas Dichtel } 46595bdfccbSEric W. Biederman 46656f200c7SGuillaume Nault static inline int fnhe_genid(const struct net *net) 4675aad1de5STimo Teräs { 4685aad1de5STimo Teräs return atomic_read(&net->fnhe_genid); 4695aad1de5STimo Teräs } 4705aad1de5STimo Teräs 4715aad1de5STimo Teräs static inline void fnhe_genid_bump(struct net *net) 4725aad1de5STimo Teräs { 4735aad1de5STimo Teräs atomic_inc(&net->fnhe_genid); 4745aad1de5STimo Teräs } 4755aad1de5STimo Teräs 4765f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */ 477