net_namespace.h 6.7 KB

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