net_namespace.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 <net/netns/core.h>
  10. #include <net/netns/mib.h>
  11. #include <net/netns/unix.h>
  12. #include <net/netns/packet.h>
  13. #include <net/netns/ipv4.h>
  14. #include <net/netns/ipv6.h>
  15. #include <net/netns/dccp.h>
  16. #include <net/netns/x_tables.h>
  17. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  18. #include <net/netns/conntrack.h>
  19. #endif
  20. #include <net/netns/xfrm.h>
  21. struct proc_dir_entry;
  22. struct net_device;
  23. struct sock;
  24. struct ctl_table_header;
  25. struct net_generic;
  26. struct net {
  27. atomic_t count; /* To decided when the network
  28. * namespace should be freed.
  29. */
  30. #ifdef NETNS_REFCNT_DEBUG
  31. atomic_t use_count; /* To track references we
  32. * destroy on demand
  33. */
  34. #endif
  35. struct list_head list; /* list of network namespaces */
  36. struct work_struct work; /* work struct for freeing */
  37. struct proc_dir_entry *proc_net;
  38. struct proc_dir_entry *proc_net_stat;
  39. #ifdef CONFIG_SYSCTL
  40. struct ctl_table_set sysctls;
  41. #endif
  42. struct net_device *loopback_dev; /* The loopback */
  43. struct list_head dev_base_head;
  44. struct hlist_head *dev_name_head;
  45. struct hlist_head *dev_index_head;
  46. /* core fib_rules */
  47. struct list_head rules_ops;
  48. spinlock_t rules_mod_lock;
  49. struct sock *rtnl; /* rtnetlink socket */
  50. struct netns_core core;
  51. struct netns_mib mib;
  52. struct netns_packet packet;
  53. struct netns_unix unx;
  54. struct netns_ipv4 ipv4;
  55. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  56. struct netns_ipv6 ipv6;
  57. #endif
  58. #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
  59. struct netns_dccp dccp;
  60. #endif
  61. #ifdef CONFIG_NETFILTER
  62. struct netns_xt xt;
  63. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  64. struct netns_ct ct;
  65. #endif
  66. #endif
  67. #ifdef CONFIG_XFRM
  68. struct netns_xfrm xfrm;
  69. #endif
  70. struct net_generic *gen;
  71. };
  72. #include <linux/seq_file_net.h>
  73. /* Init's network namespace */
  74. extern struct net init_net;
  75. #ifdef CONFIG_NET
  76. #define INIT_NET_NS(net_ns) .net_ns = &init_net,
  77. extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
  78. #else /* CONFIG_NET */
  79. #define INIT_NET_NS(net_ns)
  80. static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
  81. {
  82. /* There is nothing to copy so this is a noop */
  83. return net_ns;
  84. }
  85. #endif /* CONFIG_NET */
  86. extern struct list_head net_namespace_list;
  87. #ifdef CONFIG_NET_NS
  88. extern void __put_net(struct net *net);
  89. static inline int net_alive(struct net *net)
  90. {
  91. return net && atomic_read(&net->count);
  92. }
  93. static inline struct net *get_net(struct net *net)
  94. {
  95. atomic_inc(&net->count);
  96. return net;
  97. }
  98. static inline struct net *maybe_get_net(struct net *net)
  99. {
  100. /* Used when we know struct net exists but we
  101. * aren't guaranteed a previous reference count
  102. * exists. If the reference count is zero this
  103. * function fails and returns NULL.
  104. */
  105. if (!atomic_inc_not_zero(&net->count))
  106. net = NULL;
  107. return net;
  108. }
  109. static inline void put_net(struct net *net)
  110. {
  111. if (atomic_dec_and_test(&net->count))
  112. __put_net(net);
  113. }
  114. static inline
  115. int net_eq(const struct net *net1, const struct net *net2)
  116. {
  117. return net1 == net2;
  118. }
  119. #else
  120. static inline int net_alive(struct net *net)
  121. {
  122. return 1;
  123. }
  124. static inline struct net *get_net(struct net *net)
  125. {
  126. return net;
  127. }
  128. static inline void put_net(struct net *net)
  129. {
  130. }
  131. static inline struct net *maybe_get_net(struct net *net)
  132. {
  133. return net;
  134. }
  135. static inline
  136. int net_eq(const struct net *net1, const struct net *net2)
  137. {
  138. return 1;
  139. }
  140. #endif
  141. #ifdef NETNS_REFCNT_DEBUG
  142. static inline struct net *hold_net(struct net *net)
  143. {
  144. if (net)
  145. atomic_inc(&net->use_count);
  146. return net;
  147. }
  148. static inline void release_net(struct net *net)
  149. {
  150. if (net)
  151. atomic_dec(&net->use_count);
  152. }
  153. #else
  154. static inline struct net *hold_net(struct net *net)
  155. {
  156. return net;
  157. }
  158. static inline void release_net(struct net *net)
  159. {
  160. }
  161. #endif
  162. #ifdef CONFIG_NET_NS
  163. static inline void write_pnet(struct net **pnet, struct net *net)
  164. {
  165. *pnet = net;
  166. }
  167. static inline struct net *read_pnet(struct net * const *pnet)
  168. {
  169. return *pnet;
  170. }
  171. #else
  172. #define write_pnet(pnet, net) do { (void)(net);} while (0)
  173. #define read_pnet(pnet) (&init_net)
  174. #endif
  175. #define for_each_net(VAR) \
  176. list_for_each_entry(VAR, &net_namespace_list, list)
  177. #ifdef CONFIG_NET_NS
  178. #define __net_init
  179. #define __net_exit
  180. #define __net_initdata
  181. #else
  182. #define __net_init __init
  183. #define __net_exit __exit_refok
  184. #define __net_initdata __initdata
  185. #endif
  186. struct pernet_operations {
  187. struct list_head list;
  188. int (*init)(struct net *net);
  189. void (*exit)(struct net *net);
  190. };
  191. extern int register_pernet_subsys(struct pernet_operations *);
  192. extern void unregister_pernet_subsys(struct pernet_operations *);
  193. extern int register_pernet_gen_subsys(int *id, struct pernet_operations *);
  194. extern void unregister_pernet_gen_subsys(int id, struct pernet_operations *);
  195. extern int register_pernet_device(struct pernet_operations *);
  196. extern void unregister_pernet_device(struct pernet_operations *);
  197. extern int register_pernet_gen_device(int *id, struct pernet_operations *);
  198. extern void unregister_pernet_gen_device(int id, struct pernet_operations *);
  199. struct ctl_path;
  200. struct ctl_table;
  201. struct ctl_table_header;
  202. extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
  203. const struct ctl_path *path, struct ctl_table *table);
  204. extern struct ctl_table_header *register_net_sysctl_rotable(
  205. const struct ctl_path *path, struct ctl_table *table);
  206. extern void unregister_net_sysctl_table(struct ctl_table_header *header);
  207. #endif /* __NET_NET_NAMESPACE_H */