net_namespace.h 7.7 KB

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