net_namespace.h 6.5 KB

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