net_namespace.h 5.2 KB

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