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> 290189197fSEric W. Biederman #include <net/netns/mpls.h> 30435d5f4bSAl Viro #include <linux/ns_common.h> 3104c52decSPablo Neira Ayuso #include <linux/idr.h> 3204c52decSPablo Neira Ayuso #include <linux/skbuff.h> 33a0a53c8bSDenis V. Lunev 34038e7332SEric W. Biederman struct user_namespace; 35457c4cbcSEric W. Biederman struct proc_dir_entry; 362774c7abSEric W. Biederman struct net_device; 3797c53cacSDenis V. Lunev struct sock; 381597fbc0SPavel Emelyanov struct ctl_table_header; 39dec827d1SPavel Emelyanov struct net_generic; 40134e6375SJohannes Berg struct sock; 412553d064SJulian Anastasov struct netns_ipvs; 421597fbc0SPavel Emelyanov 437c28bd0bSEric Dumazet 447c28bd0bSEric Dumazet #define NETDEV_HASHBITS 8 457c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS) 467c28bd0bSEric Dumazet 475f256becSEric W. Biederman struct net { 48a685e089SAl Viro atomic_t passive; /* To decided when the network 495f256becSEric W. Biederman * namespace should be freed. 505f256becSEric W. Biederman */ 51a685e089SAl Viro atomic_t count; /* To decided when the network 52a685e089SAl Viro * namespace should be shut down. 53a685e089SAl Viro */ 548e602ce2SEric Dumazet spinlock_t rules_mod_lock; 558e602ce2SEric Dumazet 5633cf7c90SEric Dumazet atomic64_t cookie_gen; 5733cf7c90SEric 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 */ 63de133464SWANG Cong spinlock_t nsid_lock; 640c7aecd4SNicolas Dichtel struct idr netns_ids; 65038e7332SEric W. Biederman 66435d5f4bSAl Viro struct ns_common ns; 6798f842e6SEric W. Biederman 68457c4cbcSEric W. Biederman struct proc_dir_entry *proc_net; 69457c4cbcSEric W. Biederman struct proc_dir_entry *proc_net_stat; 70881d966bSEric W. Biederman 7173455092SAl Viro #ifdef CONFIG_SYSCTL 7273455092SAl Viro struct ctl_table_set sysctls; 7373455092SAl Viro #endif 7495bdfccbSEric W. Biederman 758e602ce2SEric Dumazet struct sock *rtnl; /* rtnetlink socket */ 768e602ce2SEric Dumazet struct sock *genl_sock; 772774c7abSEric W. Biederman 78881d966bSEric W. Biederman struct list_head dev_base_head; 79881d966bSEric W. Biederman struct hlist_head *dev_name_head; 80881d966bSEric W. Biederman struct hlist_head *dev_index_head; 814e985adaSThomas Graf unsigned int dev_base_seq; /* protected by rtnl_mutex */ 82aa79e66eSPavel Emelyanov int ifindex; 8350624c93SEric W. Biederman unsigned int dev_unreg_count; 8497c53cacSDenis V. Lunev 855fd30ee7SDenis V. Lunev /* core fib_rules */ 865fd30ee7SDenis V. Lunev struct list_head rules_ops; 875fd30ee7SDenis V. Lunev 88d12d01d6SDenis V. Lunev 898e602ce2SEric Dumazet struct net_device *loopback_dev; /* The loopback */ 908efa6e93SPavel Emelyanov struct netns_core core; 91852566f5SPavel Emelyanov struct netns_mib mib; 922aaef4e4SDenis V. Lunev struct netns_packet packet; 93a0a53c8bSDenis V. Lunev struct netns_unix unx; 948afd351cSPavel Emelyanov struct netns_ipv4 ipv4; 95dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 96b0f159dbSDaniel Lezcano struct netns_ipv6 ipv6; 97b0f159dbSDaniel Lezcano #endif 98633fc86fSAlexander Aring #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) 99633fc86fSAlexander Aring struct netns_ieee802154_lowpan ieee802154_lowpan; 100633fc86fSAlexander Aring #endif 1014db67e80SEric W. Biederman #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE) 1024db67e80SEric W. Biederman struct netns_sctp sctp; 1034db67e80SEric W. Biederman #endif 10467019cc9SPavel Emelyanov #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE) 10567019cc9SPavel Emelyanov struct netns_dccp dccp; 10667019cc9SPavel Emelyanov #endif 1078d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER 108f3c1a44aSGao feng struct netns_nf nf; 1098d870052SAlexey Dobriyan struct netns_xt xt; 110dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 111dfdb8d79SAlexey Dobriyan struct netns_ct ct; 112dfdb8d79SAlexey Dobriyan #endif 11399633ab2SPablo Neira Ayuso #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE) 11499633ab2SPablo Neira Ayuso struct netns_nftables nft; 11599633ab2SPablo Neira Ayuso #endif 116c038a767SAmerigo Wang #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) 117c038a767SAmerigo Wang struct netns_nf_frag nf_frag; 118c038a767SAmerigo Wang #endif 119cd8c20b6SAlexey Dobriyan struct sock *nfnl; 120cd8c20b6SAlexey Dobriyan struct sock *nfnl_stash; 1213499abb2SAndreas Schultz #if IS_ENABLED(CONFIG_NETFILTER_NETLINK_ACCT) 1223499abb2SAndreas Schultz struct list_head nfnl_acct_list; 1233499abb2SAndreas Schultz #endif 1248d870052SAlexey Dobriyan #endif 1253d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE 126b333b3d2SJohannes Berg struct sk_buff_head wext_nlevents; 127b333b3d2SJohannes Berg #endif 1281c87733dSEric Dumazet struct net_generic __rcu *gen; 1298e602ce2SEric Dumazet 1308e602ce2SEric Dumazet /* Note : following structs are cache line aligned */ 1318e602ce2SEric Dumazet #ifdef CONFIG_XFRM 1328e602ce2SEric Dumazet struct netns_xfrm xfrm; 1338e602ce2SEric Dumazet #endif 1348b4d14d8SJunweiZhang #if IS_ENABLED(CONFIG_IP_VS) 13561b1ab45SHans Schillstrom struct netns_ipvs *ipvs; 1368b4d14d8SJunweiZhang #endif 1370189197fSEric W. Biederman #if IS_ENABLED(CONFIG_MPLS) 1380189197fSEric W. Biederman struct netns_mpls mpls; 1390189197fSEric W. Biederman #endif 14051d7cccfSAndrey Vagin struct sock *diag_nlsk; 1415aad1de5STimo Teräs atomic_t fnhe_genid; 1425f256becSEric W. Biederman }; 1435f256becSEric W. Biederman 144c0f39322SDenis V. Lunev #include <linux/seq_file_net.h> 145c0f39322SDenis V. Lunev 1464fabcd71SDaniel Lezcano /* Init's network namespace */ 1475f256becSEric W. Biederman extern struct net init_net; 148a4aa834aSDenis V. Lunev 149d727abcbSEric W. Biederman #ifdef CONFIG_NET_NS 150e67e16eaSJoe Perches struct net *copy_net_ns(unsigned long flags, struct user_namespace *user_ns, 151e67e16eaSJoe Perches struct net *old_net); 152225c0a01SDenis V. Lunev 153d727abcbSEric W. Biederman #else /* CONFIG_NET_NS */ 154d727abcbSEric W. Biederman #include <linux/sched.h> 155d727abcbSEric W. Biederman #include <linux/nsproxy.h> 156038e7332SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags, 157038e7332SEric W. Biederman struct user_namespace *user_ns, struct net *old_net) 1589dd776b6SEric W. Biederman { 159d727abcbSEric W. Biederman if (flags & CLONE_NEWNET) 160d727abcbSEric W. Biederman return ERR_PTR(-EINVAL); 161d727abcbSEric W. Biederman return old_net; 1629dd776b6SEric W. Biederman } 163d727abcbSEric W. Biederman #endif /* CONFIG_NET_NS */ 164225c0a01SDenis V. Lunev 165225c0a01SDenis V. Lunev 166225c0a01SDenis V. Lunev extern struct list_head net_namespace_list; 1679dd776b6SEric W. Biederman 168e67e16eaSJoe Perches struct net *get_net_ns_by_pid(pid_t pid); 169e67e16eaSJoe Perches struct net *get_net_ns_by_fd(int pid); 17030ffee84SJohannes Berg 171535d3ae9SRashika Kheria #ifdef CONFIG_SYSCTL 172535d3ae9SRashika Kheria void ipx_register_sysctl(void); 173535d3ae9SRashika Kheria void ipx_unregister_sysctl(void); 174535d3ae9SRashika Kheria #else 175535d3ae9SRashika Kheria #define ipx_register_sysctl() 176535d3ae9SRashika Kheria #define ipx_unregister_sysctl() 177535d3ae9SRashika Kheria #endif 178535d3ae9SRashika Kheria 179d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS 180e67e16eaSJoe Perches void __put_net(struct net *net); 1815f256becSEric W. Biederman 1825f256becSEric W. Biederman static inline struct net *get_net(struct net *net) 1835f256becSEric W. Biederman { 1845f256becSEric W. Biederman atomic_inc(&net->count); 1855f256becSEric W. Biederman return net; 1865f256becSEric W. Biederman } 1875f256becSEric W. Biederman 188077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net) 189077130c0SEric W. Biederman { 190077130c0SEric W. Biederman /* Used when we know struct net exists but we 191077130c0SEric W. Biederman * aren't guaranteed a previous reference count 192077130c0SEric W. Biederman * exists. If the reference count is zero this 193077130c0SEric W. Biederman * function fails and returns NULL. 194077130c0SEric W. Biederman */ 195077130c0SEric W. Biederman if (!atomic_inc_not_zero(&net->count)) 196077130c0SEric W. Biederman net = NULL; 197077130c0SEric W. Biederman return net; 198077130c0SEric W. Biederman } 199077130c0SEric W. Biederman 2005f256becSEric W. Biederman static inline void put_net(struct net *net) 2015f256becSEric W. Biederman { 2025f256becSEric W. Biederman if (atomic_dec_and_test(&net->count)) 2035f256becSEric W. Biederman __put_net(net); 2045f256becSEric W. Biederman } 2055f256becSEric W. Biederman 206878628fbSYOSHIFUJI Hideaki static inline 207878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2) 208878628fbSYOSHIFUJI Hideaki { 209878628fbSYOSHIFUJI Hideaki return net1 == net2; 210878628fbSYOSHIFUJI Hideaki } 211a685e089SAl Viro 212e67e16eaSJoe Perches void net_drop_ns(void *); 213a685e089SAl Viro 214d4655795SPavel Emelyanov #else 215b9f75f45SEric W. Biederman 216d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net) 217d4655795SPavel Emelyanov { 218d4655795SPavel Emelyanov return net; 219d4655795SPavel Emelyanov } 220d4655795SPavel Emelyanov 221d4655795SPavel Emelyanov static inline void put_net(struct net *net) 222d4655795SPavel Emelyanov { 223d4655795SPavel Emelyanov } 224d4655795SPavel Emelyanov 225d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net) 226d4655795SPavel Emelyanov { 227d4655795SPavel Emelyanov return net; 228d4655795SPavel Emelyanov } 229878628fbSYOSHIFUJI Hideaki 230878628fbSYOSHIFUJI Hideaki static inline 231878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2) 232878628fbSYOSHIFUJI Hideaki { 233878628fbSYOSHIFUJI Hideaki return 1; 234878628fbSYOSHIFUJI Hideaki } 235a685e089SAl Viro 236a685e089SAl Viro #define net_drop_ns NULL 237d4655795SPavel Emelyanov #endif 2385f256becSEric W. Biederman 2395d1e4468SDenis V. Lunev 2400c5c9fb5SEric W. Biederman typedef struct { 2418f424b5fSEric Dumazet #ifdef CONFIG_NET_NS 2420c5c9fb5SEric W. Biederman struct net *net; 2438f424b5fSEric Dumazet #endif 2440c5c9fb5SEric W. Biederman } possible_net_t; 2450c5c9fb5SEric W. Biederman 2460c5c9fb5SEric W. Biederman static inline void write_pnet(possible_net_t *pnet, struct net *net) 2470c5c9fb5SEric W. Biederman { 2480c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS 2490c5c9fb5SEric W. Biederman pnet->net = net; 2500c5c9fb5SEric W. Biederman #endif 2510c5c9fb5SEric W. Biederman } 2520c5c9fb5SEric W. Biederman 2530c5c9fb5SEric W. Biederman static inline struct net *read_pnet(const possible_net_t *pnet) 2540c5c9fb5SEric W. Biederman { 2550c5c9fb5SEric W. Biederman #ifdef CONFIG_NET_NS 2560c5c9fb5SEric W. Biederman return pnet->net; 2570c5c9fb5SEric W. Biederman #else 2580c5c9fb5SEric W. Biederman return &init_net; 2590c5c9fb5SEric W. Biederman #endif 2600c5c9fb5SEric W. Biederman } 2615d1e4468SDenis V. Lunev 2625f256becSEric W. Biederman #define for_each_net(VAR) \ 2635f256becSEric W. Biederman list_for_each_entry(VAR, &net_namespace_list, list) 2645f256becSEric W. Biederman 26511a28d37SJohannes Berg #define for_each_net_rcu(VAR) \ 26611a28d37SJohannes Berg list_for_each_entry_rcu(VAR, &net_namespace_list, list) 26711a28d37SJohannes Berg 2684665079cSPavel Emelyanov #ifdef CONFIG_NET_NS 2694665079cSPavel Emelyanov #define __net_init 2704665079cSPavel Emelyanov #define __net_exit 271022cbae6SDenis V. Lunev #define __net_initdata 27204a6f82cSAndi Kleen #define __net_initconst 2734665079cSPavel Emelyanov #else 2744665079cSPavel Emelyanov #define __net_init __init 2754665079cSPavel Emelyanov #define __net_exit __exit_refok 276022cbae6SDenis V. Lunev #define __net_initdata __initdata 27704a6f82cSAndi Kleen #define __net_initconst __initconst 2784665079cSPavel Emelyanov #endif 2795f256becSEric W. Biederman 2807a0877d4SNicolas Dichtel int peernet2id_alloc(struct net *net, struct net *peer); 28159324cf3SNicolas Dichtel int peernet2id(struct net *net, struct net *peer); 28259324cf3SNicolas Dichtel bool peernet_has_id(struct net *net, struct net *peer); 2830c7aecd4SNicolas Dichtel struct net *get_net_ns_by_id(struct net *net, int id); 2840c7aecd4SNicolas Dichtel 2855f256becSEric W. Biederman struct pernet_operations { 2865f256becSEric W. Biederman struct list_head list; 2875f256becSEric W. Biederman int (*init)(struct net *net); 2885f256becSEric W. Biederman void (*exit)(struct net *net); 28972ad937aSEric W. Biederman void (*exit_batch)(struct list_head *net_exit_list); 290f875bae0SEric W. Biederman int *id; 291f875bae0SEric W. Biederman size_t size; 2925f256becSEric W. Biederman }; 2935f256becSEric W. Biederman 29417edde52SEric W. Biederman /* 29517edde52SEric W. Biederman * Use these carefully. If you implement a network device and it 29617edde52SEric W. Biederman * needs per network namespace operations use device pernet operations, 29717edde52SEric W. Biederman * otherwise use pernet subsys operations. 29817edde52SEric W. Biederman * 2994edf547bSJohannes Berg * Network interfaces need to be removed from a dying netns _before_ 3004edf547bSJohannes Berg * subsys notifiers can be called, as most of the network code cleanup 3014edf547bSJohannes Berg * (which is done from subsys notifiers) runs with the assumption that 3024edf547bSJohannes Berg * dev_remove_pack has been called so no new packets will arrive during 3034edf547bSJohannes Berg * and after the cleanup functions have been called. dev_remove_pack 3044edf547bSJohannes Berg * is not per namespace so instead the guarantee of no more packets 3054edf547bSJohannes Berg * arriving in a network namespace is provided by ensuring that all 3064edf547bSJohannes Berg * network devices and all sockets have left the network namespace 3074edf547bSJohannes Berg * before the cleanup methods are called. 30817edde52SEric W. Biederman * 30917edde52SEric W. Biederman * For the longest time the ipv4 icmp code was registered as a pernet 31017edde52SEric W. Biederman * device which caused kernel oops, and panics during network 31117edde52SEric W. Biederman * namespace cleanup. So please don't get this wrong. 31217edde52SEric W. Biederman */ 313e67e16eaSJoe Perches int register_pernet_subsys(struct pernet_operations *); 314e67e16eaSJoe Perches void unregister_pernet_subsys(struct pernet_operations *); 315e67e16eaSJoe Perches int register_pernet_device(struct pernet_operations *); 316e67e16eaSJoe Perches void unregister_pernet_device(struct pernet_operations *); 317f875bae0SEric W. Biederman 31895bdfccbSEric W. Biederman struct ctl_table; 31995bdfccbSEric W. Biederman struct ctl_table_header; 320d62c612eSPavel Emelyanov 3212ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL 322e67e16eaSJoe Perches int net_sysctl_init(void); 323e67e16eaSJoe Perches struct ctl_table_header *register_net_sysctl(struct net *net, const char *path, 324e67e16eaSJoe Perches struct ctl_table *table); 325e67e16eaSJoe Perches void unregister_net_sysctl_table(struct ctl_table_header *header); 32648c74958SEric W. Biederman #else 32748c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; } 32848c74958SEric W. Biederman static inline struct ctl_table_header *register_net_sysctl(struct net *net, 32948c74958SEric W. Biederman const char *path, struct ctl_table *table) 33048c74958SEric W. Biederman { 33148c74958SEric W. Biederman return NULL; 33248c74958SEric W. Biederman } 33348c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header) 33448c74958SEric W. Biederman { 33548c74958SEric W. Biederman } 33648c74958SEric W. Biederman #endif 33748c74958SEric W. Biederman 338ca4c3fc2Sfan.du static inline int rt_genid_ipv4(struct net *net) 339b42664f8SNicolas Dichtel { 340ca4c3fc2Sfan.du return atomic_read(&net->ipv4.rt_genid); 341b42664f8SNicolas Dichtel } 342b42664f8SNicolas Dichtel 343ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv4(struct net *net) 344b42664f8SNicolas Dichtel { 345ca4c3fc2Sfan.du atomic_inc(&net->ipv4.rt_genid); 346ca4c3fc2Sfan.du } 347ca4c3fc2Sfan.du 348705f1c86SHannes Frederic Sowa extern void (*__fib6_flush_trees)(struct net *net); 349ca4c3fc2Sfan.du static inline void rt_genid_bump_ipv6(struct net *net) 350ca4c3fc2Sfan.du { 351705f1c86SHannes Frederic Sowa if (__fib6_flush_trees) 352705f1c86SHannes Frederic Sowa __fib6_flush_trees(net); 353ca4c3fc2Sfan.du } 354ca4c3fc2Sfan.du 355599018a7SLuis R. Rodriguez #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) 356599018a7SLuis R. Rodriguez static inline struct netns_ieee802154_lowpan * 357599018a7SLuis R. Rodriguez net_ieee802154_lowpan(struct net *net) 358599018a7SLuis R. Rodriguez { 359599018a7SLuis R. Rodriguez return &net->ieee802154_lowpan; 360599018a7SLuis R. Rodriguez } 361599018a7SLuis R. Rodriguez #endif 362599018a7SLuis R. Rodriguez 363ca4c3fc2Sfan.du /* For callers who don't really care about whether it's IPv4 or IPv6 */ 364ca4c3fc2Sfan.du static inline void rt_genid_bump_all(struct net *net) 365ca4c3fc2Sfan.du { 366ca4c3fc2Sfan.du rt_genid_bump_ipv4(net); 367ca4c3fc2Sfan.du rt_genid_bump_ipv6(net); 368b42664f8SNicolas Dichtel } 36995bdfccbSEric W. Biederman 3705aad1de5STimo Teräs static inline int fnhe_genid(struct net *net) 3715aad1de5STimo Teräs { 3725aad1de5STimo Teräs return atomic_read(&net->fnhe_genid); 3735aad1de5STimo Teräs } 3745aad1de5STimo Teräs 3755aad1de5STimo Teräs static inline void fnhe_genid_bump(struct net *net) 3765aad1de5STimo Teräs { 3775aad1de5STimo Teräs atomic_inc(&net->fnhe_genid); 3785aad1de5STimo Teräs } 3795aad1de5STimo Teräs 3805f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */ 381