net_namespace.h 10 KB

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