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> 1867019cc9SPavel Emelyanov #include <net/netns/dccp.h> 198d870052SAlexey Dobriyan #include <net/netns/x_tables.h> 20dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 21dfdb8d79SAlexey Dobriyan #include <net/netns/conntrack.h> 22dfdb8d79SAlexey Dobriyan #endif 23d62ddc21SAlexey Dobriyan #include <net/netns/xfrm.h> 24a0a53c8bSDenis V. Lunev 25457c4cbcSEric W. Biederman struct proc_dir_entry; 262774c7abSEric W. Biederman struct net_device; 2797c53cacSDenis V. Lunev struct sock; 281597fbc0SPavel Emelyanov struct ctl_table_header; 29dec827d1SPavel Emelyanov struct net_generic; 30134e6375SJohannes Berg struct sock; 312553d064SJulian Anastasov struct netns_ipvs; 321597fbc0SPavel Emelyanov 337c28bd0bSEric Dumazet 347c28bd0bSEric Dumazet #define NETDEV_HASHBITS 8 357c28bd0bSEric Dumazet #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS) 367c28bd0bSEric Dumazet 375f256becSEric W. Biederman struct net { 38a685e089SAl Viro atomic_t passive; /* To decided when the network 395f256becSEric W. Biederman * namespace should be freed. 405f256becSEric W. Biederman */ 41a685e089SAl Viro atomic_t count; /* To decided when the network 42a685e089SAl Viro * namespace should be shut down. 43a685e089SAl Viro */ 445d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG 455f256becSEric W. Biederman atomic_t use_count; /* To track references we 465f256becSEric W. Biederman * destroy on demand 475f256becSEric W. Biederman */ 485d1e4468SDenis V. Lunev #endif 498e602ce2SEric Dumazet spinlock_t rules_mod_lock; 508e602ce2SEric Dumazet 515f256becSEric W. Biederman struct list_head list; /* list of network namespaces */ 522b035b39SEric W. Biederman struct list_head cleanup_list; /* namespaces on death row */ 5372ad937aSEric W. Biederman struct list_head exit_list; /* Use only net_mutex */ 54457c4cbcSEric W. Biederman 55457c4cbcSEric W. Biederman struct proc_dir_entry *proc_net; 56457c4cbcSEric W. Biederman struct proc_dir_entry *proc_net_stat; 57881d966bSEric W. Biederman 5873455092SAl Viro #ifdef CONFIG_SYSCTL 5973455092SAl Viro struct ctl_table_set sysctls; 6073455092SAl Viro #endif 6195bdfccbSEric W. Biederman 628e602ce2SEric Dumazet struct sock *rtnl; /* rtnetlink socket */ 638e602ce2SEric Dumazet struct sock *genl_sock; 642774c7abSEric W. Biederman 65881d966bSEric W. Biederman struct list_head dev_base_head; 66881d966bSEric W. Biederman struct hlist_head *dev_name_head; 67881d966bSEric W. Biederman struct hlist_head *dev_index_head; 684e985adaSThomas Graf unsigned int dev_base_seq; /* protected by rtnl_mutex */ 69aa79e66eSPavel Emelyanov int ifindex; 7097c53cacSDenis V. Lunev 715fd30ee7SDenis V. Lunev /* core fib_rules */ 725fd30ee7SDenis V. Lunev struct list_head rules_ops; 735fd30ee7SDenis V. Lunev 74d12d01d6SDenis V. Lunev 758e602ce2SEric Dumazet struct net_device *loopback_dev; /* The loopback */ 768efa6e93SPavel Emelyanov struct netns_core core; 77852566f5SPavel Emelyanov struct netns_mib mib; 782aaef4e4SDenis V. Lunev struct netns_packet packet; 79a0a53c8bSDenis V. Lunev struct netns_unix unx; 808afd351cSPavel Emelyanov struct netns_ipv4 ipv4; 81dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 82b0f159dbSDaniel Lezcano struct netns_ipv6 ipv6; 83b0f159dbSDaniel Lezcano #endif 8467019cc9SPavel Emelyanov #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE) 8567019cc9SPavel Emelyanov struct netns_dccp dccp; 8667019cc9SPavel Emelyanov #endif 878d870052SAlexey Dobriyan #ifdef CONFIG_NETFILTER 888d870052SAlexey Dobriyan struct netns_xt xt; 89dfdb8d79SAlexey Dobriyan #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 90dfdb8d79SAlexey Dobriyan struct netns_ct ct; 91dfdb8d79SAlexey Dobriyan #endif 92cd8c20b6SAlexey Dobriyan struct sock *nfnl; 93cd8c20b6SAlexey Dobriyan struct sock *nfnl_stash; 948d870052SAlexey Dobriyan #endif 953d23e349SJohannes Berg #ifdef CONFIG_WEXT_CORE 96b333b3d2SJohannes Berg struct sk_buff_head wext_nlevents; 97b333b3d2SJohannes Berg #endif 981c87733dSEric Dumazet struct net_generic __rcu *gen; 998e602ce2SEric Dumazet 1008e602ce2SEric Dumazet /* Note : following structs are cache line aligned */ 1018e602ce2SEric Dumazet #ifdef CONFIG_XFRM 1028e602ce2SEric Dumazet struct netns_xfrm xfrm; 1038e602ce2SEric Dumazet #endif 10461b1ab45SHans Schillstrom struct netns_ipvs *ipvs; 10551d7cccfSAndrey Vagin struct sock *diag_nlsk; 1065f256becSEric W. Biederman }; 1075f256becSEric W. Biederman 1081fb9489bSPavel Emelyanov /* 1091fb9489bSPavel Emelyanov * ifindex generation is per-net namespace, and loopback is 1101fb9489bSPavel Emelyanov * always the 1st device in ns (see net_dev_init), thus any 1111fb9489bSPavel Emelyanov * loopback device should get ifindex 1 1121fb9489bSPavel Emelyanov */ 1131fb9489bSPavel Emelyanov 1141fb9489bSPavel Emelyanov #define LOOPBACK_IFINDEX 1 115225c0a01SDenis V. Lunev 116c0f39322SDenis V. Lunev #include <linux/seq_file_net.h> 117c0f39322SDenis V. Lunev 1184fabcd71SDaniel Lezcano /* Init's network namespace */ 1195f256becSEric W. Biederman extern struct net init_net; 120a4aa834aSDenis V. Lunev 121a4aa834aSDenis V. Lunev #ifdef CONFIG_NET 1229dd776b6SEric W. Biederman extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns); 123225c0a01SDenis V. Lunev 124225c0a01SDenis V. Lunev #else /* CONFIG_NET */ 1259dd776b6SEric W. Biederman static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns) 1269dd776b6SEric W. Biederman { 1279dd776b6SEric W. Biederman /* There is nothing to copy so this is a noop */ 1289dd776b6SEric W. Biederman return net_ns; 1299dd776b6SEric W. Biederman } 130225c0a01SDenis V. Lunev #endif /* CONFIG_NET */ 131225c0a01SDenis V. Lunev 132225c0a01SDenis V. Lunev 133225c0a01SDenis V. Lunev extern struct list_head net_namespace_list; 1349dd776b6SEric W. Biederman 13530ffee84SJohannes Berg extern struct net *get_net_ns_by_pid(pid_t pid); 136f0630529SEric W. Biederman extern struct net *get_net_ns_by_fd(int pid); 13730ffee84SJohannes Berg 138d4655795SPavel Emelyanov #ifdef CONFIG_NET_NS 1395f256becSEric W. Biederman extern void __put_net(struct net *net); 1405f256becSEric W. Biederman 1415f256becSEric W. Biederman static inline struct net *get_net(struct net *net) 1425f256becSEric W. Biederman { 1435f256becSEric W. Biederman atomic_inc(&net->count); 1445f256becSEric W. Biederman return net; 1455f256becSEric W. Biederman } 1465f256becSEric W. Biederman 147077130c0SEric W. Biederman static inline struct net *maybe_get_net(struct net *net) 148077130c0SEric W. Biederman { 149077130c0SEric W. Biederman /* Used when we know struct net exists but we 150077130c0SEric W. Biederman * aren't guaranteed a previous reference count 151077130c0SEric W. Biederman * exists. If the reference count is zero this 152077130c0SEric W. Biederman * function fails and returns NULL. 153077130c0SEric W. Biederman */ 154077130c0SEric W. Biederman if (!atomic_inc_not_zero(&net->count)) 155077130c0SEric W. Biederman net = NULL; 156077130c0SEric W. Biederman return net; 157077130c0SEric W. Biederman } 158077130c0SEric W. Biederman 1595f256becSEric W. Biederman static inline void put_net(struct net *net) 1605f256becSEric W. Biederman { 1615f256becSEric W. Biederman if (atomic_dec_and_test(&net->count)) 1625f256becSEric W. Biederman __put_net(net); 1635f256becSEric W. Biederman } 1645f256becSEric W. Biederman 165878628fbSYOSHIFUJI Hideaki static inline 166878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2) 167878628fbSYOSHIFUJI Hideaki { 168878628fbSYOSHIFUJI Hideaki return net1 == net2; 169878628fbSYOSHIFUJI Hideaki } 170a685e089SAl Viro 171a685e089SAl Viro extern void net_drop_ns(void *); 172a685e089SAl Viro 173d4655795SPavel Emelyanov #else 174b9f75f45SEric W. Biederman 175d4655795SPavel Emelyanov static inline struct net *get_net(struct net *net) 176d4655795SPavel Emelyanov { 177d4655795SPavel Emelyanov return net; 178d4655795SPavel Emelyanov } 179d4655795SPavel Emelyanov 180d4655795SPavel Emelyanov static inline void put_net(struct net *net) 181d4655795SPavel Emelyanov { 182d4655795SPavel Emelyanov } 183d4655795SPavel Emelyanov 184d4655795SPavel Emelyanov static inline struct net *maybe_get_net(struct net *net) 185d4655795SPavel Emelyanov { 186d4655795SPavel Emelyanov return net; 187d4655795SPavel Emelyanov } 188878628fbSYOSHIFUJI Hideaki 189878628fbSYOSHIFUJI Hideaki static inline 190878628fbSYOSHIFUJI Hideaki int net_eq(const struct net *net1, const struct net *net2) 191878628fbSYOSHIFUJI Hideaki { 192878628fbSYOSHIFUJI Hideaki return 1; 193878628fbSYOSHIFUJI Hideaki } 194a685e089SAl Viro 195a685e089SAl Viro #define net_drop_ns NULL 196d4655795SPavel Emelyanov #endif 1975f256becSEric W. Biederman 1985d1e4468SDenis V. Lunev 1995d1e4468SDenis V. Lunev #ifdef NETNS_REFCNT_DEBUG 2005d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net) 2015d1e4468SDenis V. Lunev { 2025d1e4468SDenis V. Lunev if (net) 2035d1e4468SDenis V. Lunev atomic_inc(&net->use_count); 2045d1e4468SDenis V. Lunev return net; 2055d1e4468SDenis V. Lunev } 2065d1e4468SDenis V. Lunev 2075d1e4468SDenis V. Lunev static inline void release_net(struct net *net) 2085d1e4468SDenis V. Lunev { 2095d1e4468SDenis V. Lunev if (net) 2105d1e4468SDenis V. Lunev atomic_dec(&net->use_count); 2115d1e4468SDenis V. Lunev } 2125d1e4468SDenis V. Lunev #else 2135d1e4468SDenis V. Lunev static inline struct net *hold_net(struct net *net) 2145d1e4468SDenis V. Lunev { 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 } 2215d1e4468SDenis V. Lunev #endif 2225d1e4468SDenis V. Lunev 2238f424b5fSEric Dumazet #ifdef CONFIG_NET_NS 2248f424b5fSEric Dumazet 2258f424b5fSEric Dumazet static inline void write_pnet(struct net **pnet, struct net *net) 2268f424b5fSEric Dumazet { 2278f424b5fSEric Dumazet *pnet = net; 2288f424b5fSEric Dumazet } 2298f424b5fSEric Dumazet 2308f424b5fSEric Dumazet static inline struct net *read_pnet(struct net * const *pnet) 2318f424b5fSEric Dumazet { 2328f424b5fSEric Dumazet return *pnet; 2338f424b5fSEric Dumazet } 2348f424b5fSEric Dumazet 2358f424b5fSEric Dumazet #else 2368f424b5fSEric Dumazet 2378f424b5fSEric Dumazet #define write_pnet(pnet, net) do { (void)(net);} while (0) 2388f424b5fSEric Dumazet #define read_pnet(pnet) (&init_net) 2398f424b5fSEric Dumazet 2408f424b5fSEric Dumazet #endif 2415d1e4468SDenis V. Lunev 2425f256becSEric W. Biederman #define for_each_net(VAR) \ 2435f256becSEric W. Biederman list_for_each_entry(VAR, &net_namespace_list, list) 2445f256becSEric W. Biederman 24511a28d37SJohannes Berg #define for_each_net_rcu(VAR) \ 24611a28d37SJohannes Berg list_for_each_entry_rcu(VAR, &net_namespace_list, list) 24711a28d37SJohannes Berg 2484665079cSPavel Emelyanov #ifdef CONFIG_NET_NS 2494665079cSPavel Emelyanov #define __net_init 2504665079cSPavel Emelyanov #define __net_exit 251022cbae6SDenis V. Lunev #define __net_initdata 2524665079cSPavel Emelyanov #else 2534665079cSPavel Emelyanov #define __net_init __init 2544665079cSPavel Emelyanov #define __net_exit __exit_refok 255022cbae6SDenis V. Lunev #define __net_initdata __initdata 2564665079cSPavel Emelyanov #endif 2575f256becSEric W. Biederman 2585f256becSEric W. Biederman struct pernet_operations { 2595f256becSEric W. Biederman struct list_head list; 2605f256becSEric W. Biederman int (*init)(struct net *net); 2615f256becSEric W. Biederman void (*exit)(struct net *net); 26272ad937aSEric W. Biederman void (*exit_batch)(struct list_head *net_exit_list); 263f875bae0SEric W. Biederman int *id; 264f875bae0SEric W. Biederman size_t size; 2655f256becSEric W. Biederman }; 2665f256becSEric W. Biederman 26717edde52SEric W. Biederman /* 26817edde52SEric W. Biederman * Use these carefully. If you implement a network device and it 26917edde52SEric W. Biederman * needs per network namespace operations use device pernet operations, 27017edde52SEric W. Biederman * otherwise use pernet subsys operations. 27117edde52SEric W. Biederman * 2724edf547bSJohannes Berg * Network interfaces need to be removed from a dying netns _before_ 2734edf547bSJohannes Berg * subsys notifiers can be called, as most of the network code cleanup 2744edf547bSJohannes Berg * (which is done from subsys notifiers) runs with the assumption that 2754edf547bSJohannes Berg * dev_remove_pack has been called so no new packets will arrive during 2764edf547bSJohannes Berg * and after the cleanup functions have been called. dev_remove_pack 2774edf547bSJohannes Berg * is not per namespace so instead the guarantee of no more packets 2784edf547bSJohannes Berg * arriving in a network namespace is provided by ensuring that all 2794edf547bSJohannes Berg * network devices and all sockets have left the network namespace 2804edf547bSJohannes Berg * before the cleanup methods are called. 28117edde52SEric W. Biederman * 28217edde52SEric W. Biederman * For the longest time the ipv4 icmp code was registered as a pernet 28317edde52SEric W. Biederman * device which caused kernel oops, and panics during network 28417edde52SEric W. Biederman * namespace cleanup. So please don't get this wrong. 28517edde52SEric W. Biederman */ 2865f256becSEric W. Biederman extern int register_pernet_subsys(struct pernet_operations *); 2875f256becSEric W. Biederman extern void unregister_pernet_subsys(struct pernet_operations *); 2885f256becSEric W. Biederman extern int register_pernet_device(struct pernet_operations *); 2895f256becSEric W. Biederman extern void unregister_pernet_device(struct pernet_operations *); 290f875bae0SEric W. Biederman 29195bdfccbSEric W. Biederman struct ctl_table; 29295bdfccbSEric W. Biederman struct ctl_table_header; 293d62c612eSPavel Emelyanov 2942ca794e5SEric W. Biederman #ifdef CONFIG_SYSCTL 2952ca794e5SEric W. Biederman extern int net_sysctl_init(void); 296ab41a2caSEric W. Biederman extern struct ctl_table_header *register_net_sysctl(struct net *net, 297ab41a2caSEric W. Biederman const char *path, struct ctl_table *table); 29895bdfccbSEric W. Biederman extern void unregister_net_sysctl_table(struct ctl_table_header *header); 29948c74958SEric W. Biederman #else 30048c74958SEric W. Biederman static inline int net_sysctl_init(void) { return 0; } 30148c74958SEric W. Biederman static inline struct ctl_table_header *register_net_sysctl(struct net *net, 30248c74958SEric W. Biederman const char *path, struct ctl_table *table) 30348c74958SEric W. Biederman { 30448c74958SEric W. Biederman return NULL; 30548c74958SEric W. Biederman } 30648c74958SEric W. Biederman static inline void unregister_net_sysctl_table(struct ctl_table_header *header) 30748c74958SEric W. Biederman { 30848c74958SEric W. Biederman } 30948c74958SEric W. Biederman #endif 31048c74958SEric W. Biederman 31195bdfccbSEric W. Biederman 3125f256becSEric W. Biederman #endif /* __NET_NET_NAMESPACE_H */ 313