net_namespace.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Operations on the network namespace
  3. */
  4. #ifndef __NET_NET_NAMESPACE_H
  5. #define __NET_NET_NAMESPACE_H
  6. #include <linux/atomic.h>
  7. #include <linux/workqueue.h>
  8. #include <linux/list.h>
  9. #include <linux/sysctl.h>
  10. #include <net/flow.h>
  11. #include <net/netns/core.h>
  12. #include <net/netns/mib.h>
  13. #include <net/netns/unix.h>
  14. #include <net/netns/packet.h>
  15. #include <net/netns/ipv4.h>
  16. #include <net/netns/ipv6.h>
  17. #include <net/netns/dccp.h>
  18. #include <net/netns/x_tables.h>
  19. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  20. #include <net/netns/conntrack.h>
  21. #endif
  22. #include <net/netns/xfrm.h>
  23. struct user_namespace;
  24. struct proc_dir_entry;
  25. struct net_device;
  26. struct sock;
  27. struct ctl_table_header;
  28. struct net_generic;
  29. struct sock;
  30. struct netns_ipvs;
  31. #define NETDEV_HASHBITS 8
  32. #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
  33. struct net {
  34. atomic_t passive; /* To decided when the network
  35. * namespace should be freed.
  36. */
  37. atomic_t count; /* To decided when the network
  38. * namespace should be shut down.
  39. */
  40. #ifdef NETNS_REFCNT_DEBUG
  41. atomic_t use_count; /* To track references we
  42. * destroy on demand
  43. */
  44. #endif
  45. spinlock_t rules_mod_lock;
  46. u32 hash_mix;
  47. struct list_head list; /* list of network namespaces */
  48. struct list_head cleanup_list; /* namespaces on death row */
  49. struct list_head exit_list; /* Use only net_mutex */
  50. struct user_namespace *user_ns; /* Owning user namespace */
  51. unsigned int proc_inum;
  52. struct proc_dir_entry *proc_net;
  53. struct proc_dir_entry *proc_net_stat;
  54. #ifdef CONFIG_SYSCTL
  55. struct ctl_table_set sysctls;
  56. #endif
  57. struct sock *rtnl; /* rtnetlink socket */
  58. struct sock *genl_sock;
  59. struct list_head dev_base_head;
  60. struct hlist_head *dev_name_head;
  61. struct hlist_head *dev_index_head;
  62. unsigned int dev_base_seq; /* protected by rtnl_mutex */
  63. /* core fib_rules */
  64. struct list_head rules_ops;
  65. struct net_device *loopback_dev; /* The loopback */
  66. struct netns_core core;
  67. struct netns_mib mib;
  68. struct netns_packet packet;
  69. struct netns_unix unx;
  70. struct netns_ipv4 ipv4;
  71. #if IS_ENABLED(CONFIG_IPV6)
  72. struct netns_ipv6 ipv6;
  73. #endif
  74. #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
  75. struct netns_dccp dccp;
  76. #endif
  77. #ifdef CONFIG_NETFILTER
  78. struct netns_xt xt;
  79. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  80. struct netns_ct ct;
  81. #endif
  82. struct sock *nfnl;
  83. struct sock *nfnl_stash;
  84. #endif
  85. #ifdef CONFIG_WEXT_CORE
  86. struct sk_buff_head wext_nlevents;
  87. #endif
  88. struct net_generic __rcu *gen;
  89. /* Note : following structs are cache line aligned */
  90. #ifdef CONFIG_XFRM
  91. struct netns_xfrm xfrm;
  92. #endif
  93. struct netns_ipvs *ipvs;
  94. };
  95. #include <linux/seq_file_net.h>
  96. /* Init's network namespace */
  97. extern struct net init_net;
  98. #ifdef CONFIG_NET
  99. extern struct net *copy_net_ns(unsigned long flags,
  100. struct user_namespace *user_ns, struct net *net_ns);
  101. #else /* CONFIG_NET */
  102. static inline struct net *copy_net_ns(unsigned long flags,
  103. struct user_namespace *user_ns, struct net *net_ns)
  104. {
  105. /* There is nothing to copy so this is a noop */
  106. return net_ns;
  107. }
  108. #endif /* CONFIG_NET */
  109. extern struct list_head net_namespace_list;
  110. extern struct net *get_net_ns_by_pid(pid_t pid);
  111. extern struct net *get_net_ns_by_fd(int pid);
  112. #ifdef CONFIG_NET_NS
  113. extern void __put_net(struct net *net);
  114. static inline struct net *get_net(struct net *net)
  115. {
  116. atomic_inc(&net->count);
  117. return net;
  118. }
  119. static inline struct net *maybe_get_net(struct net *net)
  120. {
  121. /* Used when we know struct net exists but we
  122. * aren't guaranteed a previous reference count
  123. * exists. If the reference count is zero this
  124. * function fails and returns NULL.
  125. */
  126. if (!atomic_inc_not_zero(&net->count))
  127. net = NULL;
  128. return net;
  129. }
  130. static inline void put_net(struct net *net)
  131. {
  132. if (atomic_dec_and_test(&net->count))
  133. __put_net(net);
  134. }
  135. static inline
  136. int net_eq(const struct net *net1, const struct net *net2)
  137. {
  138. return net1 == net2;
  139. }
  140. extern void net_drop_ns(void *);
  141. #else
  142. static inline struct net *get_net(struct net *net)
  143. {
  144. return net;
  145. }
  146. static inline void put_net(struct net *net)
  147. {
  148. }
  149. static inline struct net *maybe_get_net(struct net *net)
  150. {
  151. return net;
  152. }
  153. static inline
  154. int net_eq(const struct net *net1, const struct net *net2)
  155. {
  156. return 1;
  157. }
  158. #define net_drop_ns NULL
  159. #endif
  160. #ifdef NETNS_REFCNT_DEBUG
  161. static inline struct net *hold_net(struct net *net)
  162. {
  163. if (net)
  164. atomic_inc(&net->use_count);
  165. return net;
  166. }
  167. static inline void release_net(struct net *net)
  168. {
  169. if (net)
  170. atomic_dec(&net->use_count);
  171. }
  172. #else
  173. static inline struct net *hold_net(struct net *net)
  174. {
  175. return net;
  176. }
  177. static inline void release_net(struct net *net)
  178. {
  179. }
  180. #endif
  181. #ifdef CONFIG_NET_NS
  182. static inline void write_pnet(struct net **pnet, struct net *net)
  183. {
  184. *pnet = net;
  185. }
  186. static inline struct net *read_pnet(struct net * const *pnet)
  187. {
  188. return *pnet;
  189. }
  190. #else
  191. #define write_pnet(pnet, net) do { (void)(net);} while (0)
  192. #define read_pnet(pnet) (&init_net)
  193. #endif
  194. #define for_each_net(VAR) \
  195. list_for_each_entry(VAR, &net_namespace_list, list)
  196. #define for_each_net_rcu(VAR) \
  197. list_for_each_entry_rcu(VAR, &net_namespace_list, list)
  198. #ifdef CONFIG_NET_NS
  199. #define __net_init
  200. #define __net_exit
  201. #define __net_initdata
  202. #else
  203. #define __net_init __init
  204. #define __net_exit __exit_refok
  205. #define __net_initdata __initdata
  206. #endif
  207. struct pernet_operations {
  208. struct list_head list;
  209. int (*init)(struct net *net);
  210. void (*exit)(struct net *net);
  211. void (*exit_batch)(struct list_head *net_exit_list);
  212. int *id;
  213. size_t size;
  214. };
  215. /*
  216. * Use these carefully. If you implement a network device and it
  217. * needs per network namespace operations use device pernet operations,
  218. * otherwise use pernet subsys operations.
  219. *
  220. * Network interfaces need to be removed from a dying netns _before_
  221. * subsys notifiers can be called, as most of the network code cleanup
  222. * (which is done from subsys notifiers) runs with the assumption that
  223. * dev_remove_pack has been called so no new packets will arrive during
  224. * and after the cleanup functions have been called. dev_remove_pack
  225. * is not per namespace so instead the guarantee of no more packets
  226. * arriving in a network namespace is provided by ensuring that all
  227. * network devices and all sockets have left the network namespace
  228. * before the cleanup methods are called.
  229. *
  230. * For the longest time the ipv4 icmp code was registered as a pernet
  231. * device which caused kernel oops, and panics during network
  232. * namespace cleanup. So please don't get this wrong.
  233. */
  234. extern int register_pernet_subsys(struct pernet_operations *);
  235. extern void unregister_pernet_subsys(struct pernet_operations *);
  236. extern int register_pernet_device(struct pernet_operations *);
  237. extern void unregister_pernet_device(struct pernet_operations *);
  238. struct ctl_path;
  239. struct ctl_table;
  240. struct ctl_table_header;
  241. extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
  242. const struct ctl_path *path, struct ctl_table *table);
  243. extern struct ctl_table_header *register_net_sysctl_rotable(
  244. const struct ctl_path *path, struct ctl_table *table);
  245. extern void unregister_net_sysctl_table(struct ctl_table_header *header);
  246. #endif /* __NET_NET_NAMESPACE_H */