net_namespace.h 9.0 KB

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