net_namespace.h 6.6 KB

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