net_namespace.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #ifdef CONFIG_WIRELESS_EXT
  73. struct sk_buff_head wext_nlevents;
  74. #endif
  75. struct net_generic *gen;
  76. };
  77. #include <linux/seq_file_net.h>
  78. /* Init's network namespace */
  79. extern struct net init_net;
  80. #ifdef CONFIG_NET
  81. #define INIT_NET_NS(net_ns) .net_ns = &init_net,
  82. extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
  83. #else /* CONFIG_NET */
  84. #define INIT_NET_NS(net_ns)
  85. static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
  86. {
  87. /* There is nothing to copy so this is a noop */
  88. return net_ns;
  89. }
  90. #endif /* CONFIG_NET */
  91. extern struct list_head net_namespace_list;
  92. extern struct net *get_net_ns_by_pid(pid_t pid);
  93. #ifdef CONFIG_NET_NS
  94. extern void __put_net(struct net *net);
  95. static inline struct net *get_net(struct net *net)
  96. {
  97. atomic_inc(&net->count);
  98. return net;
  99. }
  100. static inline struct net *maybe_get_net(struct net *net)
  101. {
  102. /* Used when we know struct net exists but we
  103. * aren't guaranteed a previous reference count
  104. * exists. If the reference count is zero this
  105. * function fails and returns NULL.
  106. */
  107. if (!atomic_inc_not_zero(&net->count))
  108. net = NULL;
  109. return net;
  110. }
  111. static inline void put_net(struct net *net)
  112. {
  113. if (atomic_dec_and_test(&net->count))
  114. __put_net(net);
  115. }
  116. static inline
  117. int net_eq(const struct net *net1, const struct net *net2)
  118. {
  119. return net1 == net2;
  120. }
  121. #else
  122. static inline struct net *get_net(struct net *net)
  123. {
  124. return net;
  125. }
  126. static inline void put_net(struct net *net)
  127. {
  128. }
  129. static inline struct net *maybe_get_net(struct net *net)
  130. {
  131. return net;
  132. }
  133. static inline
  134. int net_eq(const struct net *net1, const struct net *net2)
  135. {
  136. return 1;
  137. }
  138. #endif
  139. #ifdef NETNS_REFCNT_DEBUG
  140. static inline struct net *hold_net(struct net *net)
  141. {
  142. if (net)
  143. atomic_inc(&net->use_count);
  144. return net;
  145. }
  146. static inline void release_net(struct net *net)
  147. {
  148. if (net)
  149. atomic_dec(&net->use_count);
  150. }
  151. #else
  152. static inline struct net *hold_net(struct net *net)
  153. {
  154. return net;
  155. }
  156. static inline void release_net(struct net *net)
  157. {
  158. }
  159. #endif
  160. #ifdef CONFIG_NET_NS
  161. static inline void write_pnet(struct net **pnet, struct net *net)
  162. {
  163. *pnet = net;
  164. }
  165. static inline struct net *read_pnet(struct net * const *pnet)
  166. {
  167. return *pnet;
  168. }
  169. #else
  170. #define write_pnet(pnet, net) do { (void)(net);} while (0)
  171. #define read_pnet(pnet) (&init_net)
  172. #endif
  173. #define for_each_net(VAR) \
  174. list_for_each_entry(VAR, &net_namespace_list, list)
  175. #define for_each_net_rcu(VAR) \
  176. list_for_each_entry_rcu(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. /*
  192. * Use these carefully. If you implement a network device and it
  193. * needs per network namespace operations use device pernet operations,
  194. * otherwise use pernet subsys operations.
  195. *
  196. * Network interfaces need to be removed from a dying netns _before_
  197. * subsys notifiers can be called, as most of the network code cleanup
  198. * (which is done from subsys notifiers) runs with the assumption that
  199. * dev_remove_pack has been called so no new packets will arrive during
  200. * and after the cleanup functions have been called. dev_remove_pack
  201. * is not per namespace so instead the guarantee of no more packets
  202. * arriving in a network namespace is provided by ensuring that all
  203. * network devices and all sockets have left the network namespace
  204. * before the cleanup methods are called.
  205. *
  206. * For the longest time the ipv4 icmp code was registered as a pernet
  207. * device which caused kernel oops, and panics during network
  208. * namespace cleanup. So please don't get this wrong.
  209. */
  210. extern int register_pernet_subsys(struct pernet_operations *);
  211. extern void unregister_pernet_subsys(struct pernet_operations *);
  212. extern int register_pernet_gen_subsys(int *id, struct pernet_operations *);
  213. extern void unregister_pernet_gen_subsys(int id, struct pernet_operations *);
  214. extern int register_pernet_device(struct pernet_operations *);
  215. extern void unregister_pernet_device(struct pernet_operations *);
  216. extern int register_pernet_gen_device(int *id, struct pernet_operations *);
  217. extern void unregister_pernet_gen_device(int id, struct pernet_operations *);
  218. struct ctl_path;
  219. struct ctl_table;
  220. struct ctl_table_header;
  221. extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
  222. const struct ctl_path *path, struct ctl_table *table);
  223. extern struct ctl_table_header *register_net_sysctl_rotable(
  224. const struct ctl_path *path, struct ctl_table *table);
  225. extern void unregister_net_sysctl_table(struct ctl_table_header *header);
  226. #endif /* __NET_NET_NAMESPACE_H */