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 128efa6e93SPavel Emelyanov #include <net/netns/core.h> 13852566f5SPavel Emelyanov #include <net/netns/mib.h> 14a0a53c8bSDenis V. Lunev #include <net/netns/unix.h> 152aaef4e4SDenis V. Lunev #include <net/netns/packet.h> 168afd351cSPavel Emelyanov #include <net/netns/ipv4.h> 17b0f159dbSDaniel Lezcano #include <net/netns/ipv6.h> 184db67e80SEric W. Biederman #include <net/netns/sctp.h> 1967019cc9SPavel Emelyanov #include <net/netns/dccp.h> 208d870052SAlexey Dobriyan #include <net/netns/x_tables.h> 21dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 22dfdb8d79SAlexey Dobriyan #include <net/netns/conntrack.h> 23dfdb8d79SAlexey Dobriyan #endif 24d62ddc21SAlexey Dobriyan #include <net/netns/xfrm.h> 25a0a53c8bSDenis V. Lunev 26457c4cbcSEric W. Biederman struct proc_dir_entry; 272774c7abSEric W. Biederman struct net_device; 2897c53cacSDenis V. Lunev struct sock; 291597fbc0SPavel Emelyanov struct ctl_table_header; 30dec827d1SPavel Emelyanov struct net_generic; 31134e6375SJohannes Berg struct sock; 322553d064SJulian Anastasov struct netns_ipvs; 331597fbc0SPavel Emelyanov 347c28bd0bSEric Dumazet 357c28bd0bSEric Dumazet #define NETDEV_HASHBITS 8 367c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS) 377c28bd0bSEric Dumazet 385f256becSEric W. Biederman struct net { 39a685e089SAl Viro atomic_t passive; /* To decided when the network 405f256becSEric W. Biederman * namespace should be freed. 415f256becSEric W. Biederman */ 42a685e089SAl Viro atomic_t count; /* To decided when the network 43a685e089SAl Viro * namespace should be shut down. 44a685e089SAl Viro */ 455d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG 465f256becSEric W. Biederman atomic_t use_count; /* To track references we 475f256becSEric W. Biederman * destroy on demand 485f256becSEric W. Biederman */ 495d1e4468SDenis V. Lunev #endif 508e602ce2SEric Dumazet spinlock_t rules_mod_lock; 518e602ce2SEric Dumazet 525f256becSEric W. Biederman struct list_head list; /* list of network namespaces */ 532b035b39SEric W. Biederman struct list_head cleanup_list; /* namespaces on death row */ 5472ad937aSEric W. Biederman struct list_head exit_list; /* Use only net_mutex */ 55457c4cbcSEric W. Biederman 56457c4cbcSEric W. Biederman struct proc_dir_entry *proc_net; 57457c4cbcSEric W. Biederman struct proc_dir_entry *proc_net_stat; 58881d966bSEric W. Biederman 5973455092SAl Viro #ifdef CONFIG_SYSCTL 6073455092SAl Viro struct ctl_table_set sysctls; 6173455092SAl Viro #endif 6295bdfccbSEric W. Biederman 638e602ce2SEric Dumazet struct sock *rtnl; /* rtnetlink socket */ 648e602ce2SEric Dumazet struct sock *genl_sock; 652774c7abSEric W. Biederman 66881d966bSEric W. Biederman struct list_head dev_base_head; 67881d966bSEric W. Biederman struct hlist_head *dev_name_head; 68881d966bSEric W. Biederman struct hlist_head *dev_index_head; 694e985adaSThomas Graf unsigned int dev_base_seq; /* protected by rtnl_mutex */ 70aa79e66eSPavel Emelyanov int ifindex; 7197c53cacSDenis V. Lunev 725fd30ee7SDenis V. Lunev /* core fib_rules */ 735fd30ee7SDenis V. Lunev struct list_head rules_ops; 745fd30ee7SDenis V. Lunev 75d12d01d6SDenis V. Lunev 768e602ce2SEric Dumazet struct net_device *loopback_dev; /* The loopback */ 778efa6e93SPavel Emelyanov struct netns_core core; 78852566f5SPavel Emelyanov struct netns_mib mib; 792aaef4e4SDenis V. Lunev struct netns_packet packet; 80a0a53c8bSDenis V. Lunev struct netns_unix unx; 818afd351cSPavel Emelyanov struct netns_ipv4 ipv4; 82dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 83b0f159dbSDaniel Lezcano struct netns_ipv6 ipv6; 84b0f159dbSDaniel Lezcano #endif 854db67e80SEric W. Biederman #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE) 864db67e80SEric W. Biederman struct netns_sctp sctp; 874db67e80SEric W. Biederman #endif 8867019cc9SPavel Emelyanov #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE) 8967019cc9SPavel Emelyanov struct netns_dccp dccp; 9067019cc9SPavel Emelyanov #endif 918d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER 928d870052SAlexey Dobriyan struct netns_xt xt; 93dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 94dfdb8d79SAlexey Dobriyan struct netns_ct ct; 95dfdb8d79SAlexey Dobriyan #endif 96c038a767SAmerigo Wang #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) 97c038a767SAmerigo Wang struct netns_nf_frag nf_frag; 98c038a767SAmerigo Wang #endif 99cd8c20b6SAlexey Dobriyan struct sock *nfnl; 100cd8c20b6SAlexey Dobriyan struct sock *nfnl_stash; 1018d870052SAlexey Dobriyan #endif 1023d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE 103b333b3d2SJohannes Berg struct sk_buff_head wext_nlevents; 104b333b3d2SJohannes Berg #endif 1051c87733dSEric Dumazet struct net_generic __rcu *gen; 1068e602ce2SEric Dumazet 1078e602ce2SEric Dumazet /* Note : following structs are cache line aligned */ 1088e602ce2SEric Dumazet #ifdef CONFIG_XFRM 1098e602ce2SEric Dumazet struct netns_xfrm xfrm; 1108e602ce2SEric Dumazet #endif 11161b1ab45SHans Schillstrom struct netns_ipvs *ipvs; 11251d7cccfSAndrey Vagin struct sock *diag_nlsk; 113b42664f8SNicolas Dichtel atomic_t rt_genid; 1145f256becSEric W. Biederman }; 1155f256becSEric W. Biederman 1161fb9489bSPavel Emelyanov /* 1171fb9489bSPavel Emelyanov * ifindex generation is per-net namespace, and loopback is 1181fb9489bSPavel Emelyanov * always the 1st device in ns (see net_dev_init), thus any 1191fb9489bSPavel Emelyanov * loopback device should get ifindex 1 1201fb9489bSPavel Emelyanov */ 1211fb9489bSPavel Emelyanov 1221fb9489bSPavel Emelyanov #define LOOPBACK_IFINDEX 1 123225c0a01SDenis V. Lunev 124c0f39322SDenis V. Lunev #include <linux/seq_file_net.h> 125c0f39322SDenis V. Lunev 1264fabcd71SDaniel Lezcano /* Init's network namespace */ 1275f256becSEric W. Biederman extern struct net init_net; 128a4aa834aSDenis V. Lunev 129d727abcbSEric W. Biederman #ifdef CONFIG_NET_NS 1309dd776b6SEric W. Biederman extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns); 131225c0a01SDenis V. Lunev 132d727abcbSEric W. Biederman #else /* CONFIG_NET_NS */ 133d727abcbSEric W. Biederman #include <linux/sched.h> 134d727abcbSEric W. Biederman #include <linux/nsproxy.h> 135d727abcbSEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags, struct net *old_net) 1369dd776b6SEric W. Biederman { 137d727abcbSEric W. Biederman if (flags & CLONE_NEWNET) 138d727abcbSEric W. Biederman return ERR_PTR(-EINVAL); 139d727abcbSEric W. Biederman return old_net; 1409dd776b6SEric W. Biederman } 141d727abcbSEric W. Biederman #endif /* CONFIG_NET_NS */ 142225c0a01SDenis V. Lunev 143225c0a01SDenis V. Lunev 144225c0a01SDenis V. Lunev extern struct list_head net_namespace_list; 1459dd776b6SEric W. Biederman 14630ffee84SJohannes Berg extern struct net *get_net_ns_by_pid(pid_t pid); 147f0630529SEric W. Biederman extern struct net *get_net_ns_by_fd(int pid); 14830ffee84SJohannes Berg 149d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS 1505f256becSEric W. Biederman extern void __put_net(struct net *net); 1515f256becSEric W. Biederman 1525f256becSEric W. Biederman static inline struct net *get_net(struct net *net) 1535f256becSEric W. Biederman { 1545f256becSEric W. Biederman atomic_inc(&net->count); 1555f256becSEric W. Biederman return net; 1565f256becSEric W. Biederman } 1575f256becSEric W. Biederman 158077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net) 159077130c0SEric W. Biederman { 160077130c0SEric W. Biederman /* Used when we know struct net exists but we 161077130c0SEric W. Biederman * aren't guaranteed a previous reference count 162077130c0SEric W. Biederman * exists. If the reference count is zero this 163077130c0SEric W. Biederman * function fails and returns NULL. 164077130c0SEric W. Biederman */ 165077130c0SEric W. Biederman if (!atomic_inc_not_zero(&net->count)) 166077130c0SEric W. Biederman net = NULL; 167077130c0SEric W. Biederman return net; 168077130c0SEric W. Biederman } 169077130c0SEric W. Biederman 1705f256becSEric W. Biederman static inline void put_net(struct net *net) 1715f256becSEric W. Biederman { 1725f256becSEric W. Biederman if (atomic_dec_and_test(&net->count)) 1735f256becSEric W. Biederman __put_net(net); 1745f256becSEric W. Biederman } 1755f256becSEric W. Biederman 176878628fbSYOSHIFUJI Hideaki static inline 177878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2) 178878628fbSYOSHIFUJI Hideaki { 179878628fbSYOSHIFUJI Hideaki return net1 == net2; 180878628fbSYOSHIFUJI Hideaki } 181a685e089SAl Viro 182a685e089SAl Viro extern void net_drop_ns(void *); 183a685e089SAl Viro 184d4655795SPavel Emelyanov #else 185b9f75f45SEric W. Biederman 186d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net) 187d4655795SPavel Emelyanov { 188d4655795SPavel Emelyanov return net; 189d4655795SPavel Emelyanov } 190d4655795SPavel Emelyanov 191d4655795SPavel Emelyanov static inline void put_net(struct net *net) 192d4655795SPavel Emelyanov { 193d4655795SPavel Emelyanov } 194d4655795SPavel Emelyanov 195d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net) 196d4655795SPavel Emelyanov { 197d4655795SPavel Emelyanov return net; 198d4655795SPavel Emelyanov } 199878628fbSYOSHIFUJI Hideaki 200878628fbSYOSHIFUJI Hideaki static inline 201878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2) 202878628fbSYOSHIFUJI Hideaki { 203878628fbSYOSHIFUJI Hideaki return 1; 204878628fbSYOSHIFUJI Hideaki } 205a685e089SAl Viro 206a685e089SAl Viro #define net_drop_ns NULL 207d4655795SPavel Emelyanov #endif 2085f256becSEric W. Biederman 2095d1e4468SDenis V. Lunev 2105d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG 2115d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net) 2125d1e4468SDenis V. Lunev { 2135d1e4468SDenis V. Lunev if (net) 2145d1e4468SDenis V. Lunev atomic_inc(&net->use_count); 2155d1e4468SDenis V. Lunev return net; 2165d1e4468SDenis V. Lunev } 2175d1e4468SDenis V. Lunev 2185d1e4468SDenis V. Lunev static inline void release_net(struct net *net) 2195d1e4468SDenis V. Lunev { 2205d1e4468SDenis V. Lunev if (net) 2215d1e4468SDenis V. Lunev atomic_dec(&net->use_count); 2225d1e4468SDenis V. Lunev } 2235d1e4468SDenis V. Lunev #else 2245d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net) 2255d1e4468SDenis V. Lunev { 2265d1e4468SDenis V. Lunev return net; 2275d1e4468SDenis V. Lunev } 2285d1e4468SDenis V. Lunev 2295d1e4468SDenis V. Lunev static inline void release_net(struct net *net) 2305d1e4468SDenis V. Lunev { 2315d1e4468SDenis V. Lunev } 2325d1e4468SDenis V. Lunev #endif 2335d1e4468SDenis V. Lunev 2348f424b5fSEric Dumazet #ifdef CONFIG_NET_NS 2358f424b5fSEric Dumazet 2368f424b5fSEric Dumazet static inline void write_pnet(struct net **pnet, struct net *net) 2378f424b5fSEric Dumazet { 2388f424b5fSEric Dumazet *pnet = net; 2398f424b5fSEric Dumazet } 2408f424b5fSEric Dumazet 2418f424b5fSEric Dumazet static inline struct net *read_pnet(struct net * const *pnet) 2428f424b5fSEric Dumazet { 2438f424b5fSEric Dumazet return *pnet; 2448f424b5fSEric Dumazet } 2458f424b5fSEric Dumazet 2468f424b5fSEric Dumazet #else 2478f424b5fSEric Dumazet 2488f424b5fSEric Dumazet #define write_pnet(pnet, net) do { (void)(net);} while (0) 2498f424b5fSEric Dumazet #define read_pnet(pnet) (&init_net) 2508f424b5fSEric Dumazet 2518f424b5fSEric Dumazet #endif 2525d1e4468SDenis V. Lunev 2535f256becSEric W. Biederman #define for_each_net(VAR) \ 2545f256becSEric W. Biederman list_for_each_entry(VAR, &net_namespace_list, list) 2555f256becSEric W. Biederman 25611a28d37SJohannes Berg #define for_each_net_rcu(VAR) \ 25711a28d37SJohannes Berg list_for_each_entry_rcu(VAR, &net_namespace_list, list) 25811a28d37SJohannes Berg 2594665079cSPavel Emelyanov #ifdef CONFIG_NET_NS 2604665079cSPavel Emelyanov #define __net_init 2614665079cSPavel Emelyanov #define __net_exit 262022cbae6SDenis V. Lunev #define __net_initdata 26304a6f82cSAndi Kleen #define __net_initconst 2644665079cSPavel Emelyanov #else 2654665079cSPavel Emelyanov #define __net_init __init 2664665079cSPavel Emelyanov #define __net_exit __exit_refok 267022cbae6SDenis V. Lunev #define __net_initdata __initdata 26804a6f82cSAndi Kleen #define __net_initconst __initconst 2694665079cSPavel Emelyanov #endif 2705f256becSEric W. Biederman 2715f256becSEric W. Biederman struct pernet_operations { 2725f256becSEric W. Biederman struct list_head list; 2735f256becSEric W. Biederman int (*init)(struct net *net); 2745f256becSEric W. Biederman void (*exit)(struct net *net); 27572ad937aSEric W. Biederman void (*exit_batch)(struct list_head *net_exit_list); 276f875bae0SEric W. Biederman int *id; 277f875bae0SEric W. Biederman size_t size; 2785f256becSEric W. Biederman }; 2795f256becSEric W. Biederman 28017edde52SEric W. Biederman /* 28117edde52SEric W. Biederman * Use these carefully. If you implement a network device and it 28217edde52SEric W. Biederman * needs per network namespace operations use device pernet operations, 28317edde52SEric W. Biederman * otherwise use pernet subsys operations. 28417edde52SEric W. Biederman * 2854edf547bSJohannes Berg * Network interfaces need to be removed from a dying netns _before_ 2864edf547bSJohannes Berg * subsys notifiers can be called, as most of the network code cleanup 2874edf547bSJohannes Berg * (which is done from subsys notifiers) runs with the assumption that 2884edf547bSJohannes Berg * dev_remove_pack has been called so no new packets will arrive during 2894edf547bSJohannes Berg * and after the cleanup functions have been called. dev_remove_pack 2904edf547bSJohannes Berg * is not per namespace so instead the guarantee of no more packets 2914edf547bSJohannes Berg * arriving in a network namespace is provided by ensuring that all 2924edf547bSJohannes Berg * network devices and all sockets have left the network namespace 2934edf547bSJohannes Berg * before the cleanup methods are called. 29417edde52SEric W. Biederman * 29517edde52SEric W. Biederman * For the longest time the ipv4 icmp code was registered as a pernet 29617edde52SEric W. Biederman * device which caused kernel oops, and panics during network 29717edde52SEric W. Biederman * namespace cleanup. So please don't get this wrong. 29817edde52SEric W. Biederman */ 2995f256becSEric W. Biederman extern int register_pernet_subsys(struct pernet_operations *); 3005f256becSEric W. Biederman extern void unregister_pernet_subsys(struct pernet_operations *); 3015f256becSEric W. Biederman extern int register_pernet_device(struct pernet_operations *); 3025f256becSEric W. Biederman extern void unregister_pernet_device(struct pernet_operations *); 303f875bae0SEric W. Biederman 30495bdfccbSEric W. Biederman struct ctl_table; 30595bdfccbSEric W. Biederman struct ctl_table_header; 306d62c612eSPavel Emelyanov 3072ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL 3082ca794e5SEric W. Biederman extern int net_sysctl_init(void); 309ab41a2caSEric W. Biederman extern struct ctl_table_header *register_net_sysctl(struct net *net, 310ab41a2caSEric W. Biederman const char *path, struct ctl_table *table); 31195bdfccbSEric W. Biederman extern void unregister_net_sysctl_table(struct ctl_table_header *header); 31248c74958SEric W. Biederman #else 31348c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; } 31448c74958SEric W. Biederman static inline struct ctl_table_header *register_net_sysctl(struct net *net, 31548c74958SEric W. Biederman const char *path, struct ctl_table *table) 31648c74958SEric W. Biederman { 31748c74958SEric W. Biederman return NULL; 31848c74958SEric W. Biederman } 31948c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header) 32048c74958SEric W. Biederman { 32148c74958SEric W. Biederman } 32248c74958SEric W. Biederman #endif 32348c74958SEric W. Biederman 324b42664f8SNicolas Dichtel static inline int rt_genid(struct net *net) 325b42664f8SNicolas Dichtel { 326b42664f8SNicolas Dichtel return atomic_read(&net->rt_genid); 327b42664f8SNicolas Dichtel } 328b42664f8SNicolas Dichtel 329b42664f8SNicolas Dichtel static inline void rt_genid_bump(struct net *net) 330b42664f8SNicolas Dichtel { 331b42664f8SNicolas Dichtel atomic_inc(&net->rt_genid); 332b42664f8SNicolas Dichtel } 33395bdfccbSEric W. Biederman 3345f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */ 335