net_namespace.h 6.4 KB

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