net_namespace.h 7.2 KB

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