net_namespace.h 11 KB

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