net_namespace.h 11 KB

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