net_namespace.h 9.1 KB

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