15f256becSEric W. Biederman /* 25f256becSEric W. Biederman * Operations on the network namespace 35f256becSEric W. Biederman */ 45f256becSEric W. Biederman #ifndef __NET_NET_NAMESPACE_H 55f256becSEric W. Biederman #define __NET_NET_NAMESPACE_H 65f256becSEric W. Biederman 760063497SArun Sharma #include <linux/atomic.h> 85f256becSEric W. Biederman #include <linux/workqueue.h> 95f256becSEric W. Biederman #include <linux/list.h> 10bee95250SDavid S. Miller #include <linux/sysctl.h> 115f256becSEric W. Biederman 126a662719SCong Wang #include <net/flow.h> 138efa6e93SPavel Emelyanov #include <net/netns/core.h> 14852566f5SPavel Emelyanov #include <net/netns/mib.h> 15a0a53c8bSDenis V. Lunev #include <net/netns/unix.h> 162aaef4e4SDenis V. Lunev #include <net/netns/packet.h> 178afd351cSPavel Emelyanov #include <net/netns/ipv4.h> 18b0f159dbSDaniel Lezcano #include <net/netns/ipv6.h> 19633fc86fSAlexander Aring #include <net/netns/ieee802154_6lowpan.h> 204db67e80SEric W. Biederman #include <net/netns/sctp.h> 2167019cc9SPavel Emelyanov #include <net/netns/dccp.h> 22f3c1a44aSGao feng #include <net/netns/netfilter.h> 238d870052SAlexey Dobriyan #include <net/netns/x_tables.h> 24dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 25dfdb8d79SAlexey Dobriyan #include <net/netns/conntrack.h> 26dfdb8d79SAlexey Dobriyan #endif 2799633ab2SPablo Neira Ayuso #include <net/netns/nftables.h> 28d62ddc21SAlexey Dobriyan #include <net/netns/xfrm.h> 29435d5f4bSAl Viro #include <linux/ns_common.h> 30a0a53c8bSDenis V. Lunev 31038e7332SEric W. Biederman struct user_namespace; 32457c4cbcSEric W. Biederman struct proc_dir_entry; 332774c7abSEric W. Biederman struct net_device; 3497c53cacSDenis V. Lunev struct sock; 351597fbc0SPavel Emelyanov struct ctl_table_header; 36dec827d1SPavel Emelyanov struct net_generic; 37134e6375SJohannes Berg struct sock; 382553d064SJulian Anastasov struct netns_ipvs; 391597fbc0SPavel Emelyanov 407c28bd0bSEric Dumazet 417c28bd0bSEric Dumazet #define NETDEV_HASHBITS 8 427c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS) 437c28bd0bSEric Dumazet 445f256becSEric W. Biederman struct net { 45a685e089SAl Viro atomic_t passive; /* To decided when the network 465f256becSEric W. Biederman * namespace should be freed. 475f256becSEric W. Biederman */ 48a685e089SAl Viro atomic_t count; /* To decided when the network 49a685e089SAl Viro * namespace should be shut down. 50a685e089SAl Viro */ 515d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG 525f256becSEric W. Biederman atomic_t use_count; /* To track references we 535f256becSEric W. Biederman * destroy on demand 545f256becSEric W. Biederman */ 555d1e4468SDenis V. Lunev #endif 568e602ce2SEric Dumazet spinlock_t rules_mod_lock; 578e602ce2SEric Dumazet 585f256becSEric W. Biederman struct list_head list; /* list of network namespaces */ 592b035b39SEric W. Biederman struct list_head cleanup_list; /* namespaces on death row */ 6072ad937aSEric W. Biederman struct list_head exit_list; /* Use only net_mutex */ 61457c4cbcSEric W. Biederman 62038e7332SEric W. Biederman struct user_namespace *user_ns; /* Owning user namespace */ 63038e7332SEric W. Biederman 64435d5f4bSAl Viro struct ns_common ns; 6598f842e6SEric W. Biederman 66457c4cbcSEric W. Biederman struct proc_dir_entry *proc_net; 67457c4cbcSEric W. Biederman struct proc_dir_entry *proc_net_stat; 68881d966bSEric W. Biederman 6973455092SAl Viro #ifdef CONFIG_SYSCTL 7073455092SAl Viro struct ctl_table_set sysctls; 7173455092SAl Viro #endif 7295bdfccbSEric W. Biederman 738e602ce2SEric Dumazet struct sock *rtnl; /* rtnetlink socket */ 748e602ce2SEric Dumazet struct sock *genl_sock; 752774c7abSEric W. Biederman 76881d966bSEric W. Biederman struct list_head dev_base_head; 77881d966bSEric W. Biederman struct hlist_head *dev_name_head; 78881d966bSEric W. Biederman struct hlist_head *dev_index_head; 794e985adaSThomas Graf unsigned int dev_base_seq; /* protected by rtnl_mutex */ 80aa79e66eSPavel Emelyanov int ifindex; 8150624c93SEric W. Biederman unsigned int dev_unreg_count; 8297c53cacSDenis V. Lunev 835fd30ee7SDenis V. Lunev /* core fib_rules */ 845fd30ee7SDenis V. Lunev struct list_head rules_ops; 855fd30ee7SDenis V. Lunev 86d12d01d6SDenis V. Lunev 878e602ce2SEric Dumazet struct net_device *loopback_dev; /* The loopback */ 888efa6e93SPavel Emelyanov struct netns_core core; 89852566f5SPavel Emelyanov struct netns_mib mib; 902aaef4e4SDenis V. Lunev struct netns_packet packet; 91a0a53c8bSDenis V. Lunev struct netns_unix unx; 928afd351cSPavel Emelyanov struct netns_ipv4 ipv4; 93dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 94b0f159dbSDaniel Lezcano struct netns_ipv6 ipv6; 95b0f159dbSDaniel Lezcano #endif 96633fc86fSAlexander Aring #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) 97633fc86fSAlexander Aring struct netns_ieee802154_lowpan ieee802154_lowpan; 98633fc86fSAlexander Aring #endif 994db67e80SEric W. Biederman #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE) 1004db67e80SEric W. Biederman struct netns_sctp sctp; 1014db67e80SEric W. Biederman #endif 10267019cc9SPavel Emelyanov #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE) 10367019cc9SPavel Emelyanov struct netns_dccp dccp; 10467019cc9SPavel Emelyanov #endif 1058d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER 106f3c1a44aSGao feng struct netns_nf nf; 1078d870052SAlexey Dobriyan struct netns_xt xt; 108dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 109dfdb8d79SAlexey Dobriyan struct netns_ct ct; 110dfdb8d79SAlexey Dobriyan #endif 11199633ab2SPablo Neira Ayuso #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE) 11299633ab2SPablo Neira Ayuso struct netns_nftables nft; 11399633ab2SPablo Neira Ayuso #endif 114c038a767SAmerigo Wang #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) 115c038a767SAmerigo Wang struct netns_nf_frag nf_frag; 116c038a767SAmerigo Wang #endif 117cd8c20b6SAlexey Dobriyan struct sock *nfnl; 118cd8c20b6SAlexey Dobriyan struct sock *nfnl_stash; 1198d870052SAlexey Dobriyan #endif 1203d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE 121b333b3d2SJohannes Berg struct sk_buff_head wext_nlevents; 122b333b3d2SJohannes Berg #endif 1231c87733dSEric Dumazet struct net_generic __rcu *gen; 1248e602ce2SEric Dumazet 1258e602ce2SEric Dumazet /* Note : following structs are cache line aligned */ 1268e602ce2SEric Dumazet #ifdef CONFIG_XFRM 1278e602ce2SEric Dumazet struct netns_xfrm xfrm; 1288e602ce2SEric Dumazet #endif 1298b4d14d8SJunweiZhang #if IS_ENABLED(CONFIG_IP_VS) 13061b1ab45SHans Schillstrom struct netns_ipvs *ipvs; 1318b4d14d8SJunweiZhang #endif 13251d7cccfSAndrey Vagin struct sock *diag_nlsk; 1335aad1de5STimo Teräs atomic_t fnhe_genid; 1345f256becSEric W. Biederman }; 1355f256becSEric W. Biederman 136c0f39322SDenis V. Lunev #include <linux/seq_file_net.h> 137c0f39322SDenis V. Lunev 1384fabcd71SDaniel Lezcano /* Init's network namespace */ 1395f256becSEric W. Biederman extern struct net init_net; 140a4aa834aSDenis V. Lunev 141d727abcbSEric W. Biederman #ifdef CONFIG_NET_NS 142e67e16eaSJoe Perches struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns, 143e67e16eaSJoe Perches struct net *old_net); 144225c0a01SDenis V. Lunev 145d727abcbSEric W. Biederman #else /* CONFIG_NET_NS */ 146d727abcbSEric W. Biederman #include <linux/sched.h> 147d727abcbSEric W. Biederman #include <linux/nsproxy.h> 148038e7332SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags, 149038e7332SEric W. Biederman struct user_namespace *user_ns, struct net *old_net) 1509dd776b6SEric W. Biederman { 151d727abcbSEric W. Biederman if (flags & CLONE_NEWNET) 152d727abcbSEric W. Biederman return ERR_PTR(-EINVAL); 153d727abcbSEric W. Biederman return old_net; 1549dd776b6SEric W. Biederman } 155d727abcbSEric W. Biederman #endif /* CONFIG_NET_NS */ 156225c0a01SDenis V. Lunev 157225c0a01SDenis V. Lunev 158225c0a01SDenis V. Lunev extern struct list_head net_namespace_list; 1599dd776b6SEric W. Biederman 160e67e16eaSJoe Perches struct net *get_net_ns_by_pid(pid_t pid); 161e67e16eaSJoe Perches struct net *get_net_ns_by_fd(int pid); 16230ffee84SJohannes Berg 163535d3ae9SRashika Kheria #ifdef CONFIG_SYSCTL 164535d3ae9SRashika Kheria void ipx_register_sysctl(void); 165535d3ae9SRashika Kheria void ipx_unregister_sysctl(void); 166535d3ae9SRashika Kheria #else 167535d3ae9SRashika Kheria #define ipx_register_sysctl() 168535d3ae9SRashika Kheria #define ipx_unregister_sysctl() 169535d3ae9SRashika Kheria #endif 170535d3ae9SRashika Kheria 171d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS 172e67e16eaSJoe Perches void __put_net(struct net *net); 1735f256becSEric W. Biederman 1745f256becSEric W. Biederman static inline struct net *get_net(struct net *net) 1755f256becSEric W. Biederman { 1765f256becSEric W. Biederman atomic_inc(&net->count); 1775f256becSEric W. Biederman return net; 1785f256becSEric W. Biederman } 1795f256becSEric W. Biederman 180077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net) 181077130c0SEric W. Biederman { 182077130c0SEric W. Biederman /* Used when we know struct net exists but we 183077130c0SEric W. Biederman * aren't guaranteed a previous reference count 184077130c0SEric W. Biederman * exists. If the reference count is zero this 185077130c0SEric W. Biederman * function fails and returns NULL. 186077130c0SEric W. Biederman */ 187077130c0SEric W. Biederman if (!atomic_inc_not_zero(&net->count)) 188077130c0SEric W. Biederman net = NULL; 189077130c0SEric W. Biederman return net; 190077130c0SEric W. Biederman } 191077130c0SEric W. Biederman 1925f256becSEric W. Biederman static inline void put_net(struct net *net) 1935f256becSEric W. Biederman { 1945f256becSEric W. Biederman if (atomic_dec_and_test(&net->count)) 1955f256becSEric W. Biederman __put_net(net); 1965f256becSEric W. Biederman } 1975f256becSEric W. Biederman 198878628fbSYOSHIFUJI Hideaki static inline 199878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2) 200878628fbSYOSHIFUJI Hideaki { 201878628fbSYOSHIFUJI Hideaki return net1 == net2; 202878628fbSYOSHIFUJI Hideaki } 203a685e089SAl Viro 204e67e16eaSJoe Perches void net_drop_ns(void *); 205a685e089SAl Viro 206d4655795SPavel Emelyanov #else 207b9f75f45SEric W. Biederman 208d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net) 209d4655795SPavel Emelyanov { 210d4655795SPavel Emelyanov return net; 211d4655795SPavel Emelyanov } 212d4655795SPavel Emelyanov 213d4655795SPavel Emelyanov static inline void put_net(struct net *net) 214d4655795SPavel Emelyanov { 215d4655795SPavel Emelyanov } 216d4655795SPavel Emelyanov 217d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net) 218d4655795SPavel Emelyanov { 219d4655795SPavel Emelyanov return net; 220d4655795SPavel Emelyanov } 221878628fbSYOSHIFUJI Hideaki 222878628fbSYOSHIFUJI Hideaki static inline 223878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2) 224878628fbSYOSHIFUJI Hideaki { 225878628fbSYOSHIFUJI Hideaki return 1; 226878628fbSYOSHIFUJI Hideaki } 227a685e089SAl Viro 228a685e089SAl Viro #define net_drop_ns NULL 229d4655795SPavel Emelyanov #endif 2305f256becSEric W. Biederman 2315d1e4468SDenis V. Lunev 2325d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG 2335d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net) 2345d1e4468SDenis V. Lunev { 2355d1e4468SDenis V. Lunev if (net) 2365d1e4468SDenis V. Lunev atomic_inc(&net->use_count); 2375d1e4468SDenis V. Lunev return net; 2385d1e4468SDenis V. Lunev } 2395d1e4468SDenis V. Lunev 2405d1e4468SDenis V. Lunev static inline void release_net(struct net *net) 2415d1e4468SDenis V. Lunev { 2425d1e4468SDenis V. Lunev if (net) 2435d1e4468SDenis V. Lunev atomic_dec(&net->use_count); 2445d1e4468SDenis V. Lunev } 2455d1e4468SDenis V. Lunev #else 2465d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net) 2475d1e4468SDenis V. Lunev { 2485d1e4468SDenis V. Lunev return net; 2495d1e4468SDenis V. Lunev } 2505d1e4468SDenis V. Lunev 2515d1e4468SDenis V. Lunev static inline void release_net(struct net *net) 2525d1e4468SDenis V. Lunev { 2535d1e4468SDenis V. Lunev } 2545d1e4468SDenis V. Lunev #endif 2555d1e4468SDenis V. Lunev 2568f424b5fSEric Dumazet #ifdef CONFIG_NET_NS 2578f424b5fSEric Dumazet 2588f424b5fSEric Dumazet static inline void write_pnet(struct net **pnet, struct net *net) 2598f424b5fSEric Dumazet { 2608f424b5fSEric Dumazet *pnet = net; 2618f424b5fSEric Dumazet } 2628f424b5fSEric Dumazet 2638f424b5fSEric Dumazet static inline struct net *read_pnet(struct net * const *pnet) 2648f424b5fSEric Dumazet { 2658f424b5fSEric Dumazet return *pnet; 2668f424b5fSEric Dumazet } 2678f424b5fSEric Dumazet 2688f424b5fSEric Dumazet #else 2698f424b5fSEric Dumazet 2708f424b5fSEric Dumazet #define write_pnet(pnet, net) do { (void)(net);} while (0) 2718f424b5fSEric Dumazet #define read_pnet(pnet) (&init_net) 2728f424b5fSEric Dumazet 2738f424b5fSEric Dumazet #endif 2745d1e4468SDenis V. Lunev 2755f256becSEric W. Biederman #define for_each_net(VAR) \ 2765f256becSEric W. Biederman list_for_each_entry(VAR, &net_namespace_list, list) 2775f256becSEric W. Biederman 27811a28d37SJohannes Berg #define for_each_net_rcu(VAR) \ 27911a28d37SJohannes Berg list_for_each_entry_rcu(VAR, &net_namespace_list, list) 28011a28d37SJohannes Berg 2814665079cSPavel Emelyanov #ifdef CONFIG_NET_NS 2824665079cSPavel Emelyanov #define __net_init 2834665079cSPavel Emelyanov #define __net_exit 284022cbae6SDenis V. Lunev #define __net_initdata 28504a6f82cSAndi Kleen #define __net_initconst 2864665079cSPavel Emelyanov #else 2874665079cSPavel Emelyanov #define __net_init __init 2884665079cSPavel Emelyanov #define __net_exit __exit_refok 289022cbae6SDenis V. Lunev #define __net_initdata __initdata 29004a6f82cSAndi Kleen #define __net_initconst __initconst 2914665079cSPavel Emelyanov #endif 2925f256becSEric W. Biederman 2935f256becSEric W. Biederman struct pernet_operations { 2945f256becSEric W. Biederman struct list_head list; 2955f256becSEric W. Biederman int (*init)(struct net *net); 2965f256becSEric W. Biederman void (*exit)(struct net *net); 29772ad937aSEric W. Biederman void (*exit_batch)(struct list_head *net_exit_list); 298f875bae0SEric W. Biederman int *id; 299f875bae0SEric W. Biederman size_t size; 3005f256becSEric W. Biederman }; 3015f256becSEric W. Biederman 30217edde52SEric W. Biederman /* 30317edde52SEric W. Biederman * Use these carefully. If you implement a network device and it 30417edde52SEric W. Biederman * needs per network namespace operations use device pernet operations, 30517edde52SEric W. Biederman * otherwise use pernet subsys operations. 30617edde52SEric W. Biederman * 3074edf547bSJohannes Berg * Network interfaces need to be removed from a dying netns _before_ 3084edf547bSJohannes Berg * subsys notifiers can be called, as most of the network code cleanup 3094edf547bSJohannes Berg * (which is done from subsys notifiers) runs with the assumption that 3104edf547bSJohannes Berg * dev_remove_pack has been called so no new packets will arrive during 3114edf547bSJohannes Berg * and after the cleanup functions have been called. dev_remove_pack 3124edf547bSJohannes Berg * is not per namespace so instead the guarantee of no more packets 3134edf547bSJohannes Berg * arriving in a network namespace is provided by ensuring that all 3144edf547bSJohannes Berg * network devices and all sockets have left the network namespace 3154edf547bSJohannes Berg * before the cleanup methods are called. 31617edde52SEric W. Biederman * 31717edde52SEric W. Biederman * For the longest time the ipv4 icmp code was registered as a pernet 31817edde52SEric W. Biederman * device which caused kernel oops, and panics during network 31917edde52SEric W. Biederman * namespace cleanup. So please don't get this wrong. 32017edde52SEric W. Biederman */ 321e67e16eaSJoe Perches int register_pernet_subsys(struct pernet_operations *); 322e67e16eaSJoe Perches void unregister_pernet_subsys(struct pernet_operations *); 323e67e16eaSJoe Perches int register_pernet_device(struct pernet_operations *); 324e67e16eaSJoe Perches void unregister_pernet_device(struct pernet_operations *); 325f875bae0SEric W. Biederman 32695bdfccbSEric W. Biederman struct ctl_table; 32795bdfccbSEric W. Biederman struct ctl_table_header; 328d62c612eSPavel Emelyanov 3292ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL 330e67e16eaSJoe Perches int net_sysctl_init(void); 331e67e16eaSJoe Perches struct ctl_table_header *register_net_sysctl(struct net *net, const char *path, 332e67e16eaSJoe Perches struct ctl_table *table); 333e67e16eaSJoe Perches void unregister_net_sysctl_table(struct ctl_table_header *header); 33448c74958SEric W. Biederman #else 33548c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; } 33648c74958SEric W. Biederman static inline struct ctl_table_header *register_net_sysctl(struct net *net, 33748c74958SEric W. Biederman const char *path, struct ctl_table *table) 33848c74958SEric W. Biederman { 33948c74958SEric W. Biederman return NULL; 34048c74958SEric W. Biederman } 34148c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header) 34248c74958SEric W. Biederman { 34348c74958SEric W. Biederman } 34448c74958SEric W. Biederman #endif 34548c74958SEric W. Biederman 346ca4c3fc2Sfan.du static inline int rt_genid_ipv4(struct net *net) 347b42664f8SNicolas Dichtel { 348ca4c3fc2Sfan.du return atomic_read(&net->ipv4.rt_genid); 349b42664f8SNicolas Dichtel } 350b42664f8SNicolas Dichtel 351ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv4(struct net *net) 352b42664f8SNicolas Dichtel { 353ca4c3fc2Sfan.du atomic_inc(&net->ipv4.rt_genid); 354ca4c3fc2Sfan.du } 355ca4c3fc2Sfan.du 356705f1c86SHannes Frederic Sowa extern void (*__fib6_flush_trees)(struct net *net); 357ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv6(struct net *net) 358ca4c3fc2Sfan.du { 359705f1c86SHannes Frederic Sowa if (__fib6_flush_trees) 360705f1c86SHannes Frederic Sowa __fib6_flush_trees(net); 361ca4c3fc2Sfan.du } 362ca4c3fc2Sfan.du 363599018a7SLuis R. Rodriguez #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) 364599018a7SLuis R. Rodriguez static inline struct netns_ieee802154_lowpan * 365599018a7SLuis R. Rodriguez net_ieee802154_lowpan(struct net *net) 366599018a7SLuis R. Rodriguez { 367599018a7SLuis R. Rodriguez return &net->ieee802154_lowpan; 368599018a7SLuis R. Rodriguez } 369599018a7SLuis R. Rodriguez #endif 370599018a7SLuis R. Rodriguez 371ca4c3fc2Sfan.du /* For callers who don't really care about whether it's IPv4 or IPv6 */ 372ca4c3fc2Sfan.du static inline void rt_genid_bump_all(struct net *net) 373ca4c3fc2Sfan.du { 374ca4c3fc2Sfan.du rt_genid_bump_ipv4(net); 375ca4c3fc2Sfan.du rt_genid_bump_ipv6(net); 376b42664f8SNicolas Dichtel } 37795bdfccbSEric W. Biederman 3785aad1de5STimo Teräs static inline int fnhe_genid(struct net *net) 3795aad1de5STimo Teräs { 3805aad1de5STimo Teräs return atomic_read(&net->fnhe_genid); 3815aad1de5STimo Teräs } 3825aad1de5STimo Teräs 3835aad1de5STimo Teräs static inline void fnhe_genid_bump(struct net *net) 3845aad1de5STimo Teräs { 3855aad1de5STimo Teräs atomic_inc(&net->fnhe_genid); 3865aad1de5STimo Teräs } 3875aad1de5STimo Teräs 3885f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */ 389