net_namespace.h 4.8 KB

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