net_namespace.h 4.6 KB

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