net_namespace.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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/nexthop.h>
  21. #include <net/netns/ieee802154_6lowpan.h>
  22. #include <net/netns/sctp.h>
  23. #include <net/netns/netfilter.h>
  24. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  25. #include <net/netns/conntrack.h>
  26. #endif
  27. #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
  28. #include <net/netns/flow_table.h>
  29. #endif
  30. #include <net/netns/nftables.h>
  31. #include <net/netns/xfrm.h>
  32. #include <net/netns/mpls.h>
  33. #include <net/netns/can.h>
  34. #include <net/netns/xdp.h>
  35. #include <net/netns/smc.h>
  36. #include <net/netns/bpf.h>
  37. #include <net/netns/mctp.h>
  38. #include <net/netns/vsock.h>
  39. #include <net/net_trackers.h>
  40. #include <linux/ns_common.h>
  41. #include <linux/idr.h>
  42. #include <linux/skbuff.h>
  43. #include <linux/notifier.h>
  44. #include <linux/xarray.h>
  45. struct user_namespace;
  46. struct proc_dir_entry;
  47. struct net_device;
  48. struct sock;
  49. struct ctl_table_header;
  50. struct net_generic;
  51. struct uevent_sock;
  52. struct netns_ipvs;
  53. struct bpf_prog;
  54. #define NETDEV_HASHBITS 8
  55. #define NETDEV_HASHENTRIES (1 << NETDEV_HASHBITS)
  56. struct net {
  57. /* First cache line can be often dirtied.
  58. * Do not place here read-mostly fields.
  59. */
  60. refcount_t passive; /* To decide when the network
  61. * namespace should be freed.
  62. */
  63. spinlock_t rules_mod_lock;
  64. unsigned int dev_base_seq; /* protected by rtnl_mutex */
  65. u32 ifindex;
  66. spinlock_t nsid_lock;
  67. atomic_t fnhe_genid;
  68. struct list_head list; /* list of network namespaces */
  69. struct list_head exit_list; /* To linked to call pernet exit
  70. * methods on dead net (
  71. * pernet_ops_rwsem read locked),
  72. * or to unregister pernet ops
  73. * (pernet_ops_rwsem write locked).
  74. */
  75. struct llist_node defer_free_list;
  76. struct llist_node cleanup_list; /* namespaces on death row */
  77. struct list_head ptype_all;
  78. struct list_head ptype_specific;
  79. #ifdef CONFIG_KEYS
  80. struct key_tag *key_domain; /* Key domain of operation tag */
  81. #endif
  82. struct user_namespace *user_ns; /* Owning user namespace */
  83. struct ucounts *ucounts;
  84. struct idr netns_ids;
  85. struct ns_common ns;
  86. struct ref_tracker_dir refcnt_tracker;
  87. struct ref_tracker_dir notrefcnt_tracker; /* tracker for objects not
  88. * refcounted against netns
  89. */
  90. struct list_head dev_base_head;
  91. struct proc_dir_entry *proc_net;
  92. struct proc_dir_entry *proc_net_stat;
  93. #ifdef CONFIG_SYSCTL
  94. struct ctl_table_set sysctls;
  95. #endif
  96. struct sock *rtnl; /* rtnetlink socket */
  97. struct sock *genl_sock;
  98. struct uevent_sock *uevent_sock; /* uevent socket */
  99. struct hlist_head *dev_name_head;
  100. struct hlist_head *dev_index_head;
  101. struct xarray dev_by_index;
  102. struct raw_notifier_head netdev_chain;
  103. /* Note that @hash_mix can be read millions times per second,
  104. * it is critical that it is on a read_mostly cache line.
  105. */
  106. u32 hash_mix;
  107. bool is_dying;
  108. struct net_device *loopback_dev; /* The loopback */
  109. /* core fib_rules */
  110. struct list_head rules_ops;
  111. struct netns_core core;
  112. struct netns_mib mib;
  113. struct netns_packet packet;
  114. #if IS_ENABLED(CONFIG_UNIX)
  115. struct netns_unix unx;
  116. #endif
  117. struct netns_nexthop nexthop;
  118. struct netns_ipv4 ipv4;
  119. #if IS_ENABLED(CONFIG_IPV6)
  120. struct netns_ipv6 ipv6;
  121. #endif
  122. #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
  123. struct netns_ieee802154_lowpan ieee802154_lowpan;
  124. #endif
  125. #if defined(CONFIG_IP_SCTP) || defined(CONFIG_IP_SCTP_MODULE)
  126. struct netns_sctp sctp;
  127. #endif
  128. #ifdef CONFIG_NETFILTER
  129. struct netns_nf nf;
  130. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  131. struct netns_ct ct;
  132. #endif
  133. #if defined(CONFIG_NF_TABLES) || defined(CONFIG_NF_TABLES_MODULE)
  134. struct netns_nftables nft;
  135. #endif
  136. #if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
  137. struct netns_ft ft;
  138. #endif
  139. #endif
  140. #ifdef CONFIG_WEXT_CORE
  141. struct sk_buff_head wext_nlevents;
  142. #endif
  143. struct net_generic __rcu *gen;
  144. /* Used to store attached BPF programs */
  145. struct netns_bpf bpf;
  146. /* Note : following structs are cache line aligned */
  147. #ifdef CONFIG_XFRM
  148. struct netns_xfrm xfrm;
  149. #endif
  150. u64 net_cookie; /* written once */
  151. #if IS_ENABLED(CONFIG_IP_VS)
  152. struct netns_ipvs *ipvs;
  153. #endif
  154. #if IS_ENABLED(CONFIG_MPLS)
  155. struct netns_mpls mpls;
  156. #endif
  157. #if IS_ENABLED(CONFIG_CAN)
  158. struct netns_can can;
  159. #endif
  160. #ifdef CONFIG_XDP_SOCKETS
  161. struct netns_xdp xdp;
  162. #endif
  163. #if IS_ENABLED(CONFIG_MCTP)
  164. struct netns_mctp mctp;
  165. #endif
  166. #if IS_ENABLED(CONFIG_CRYPTO_USER)
  167. struct sock *crypto_nlsk;
  168. #endif
  169. struct sock *diag_nlsk;
  170. #if IS_ENABLED(CONFIG_SMC)
  171. struct netns_smc smc;
  172. #endif
  173. #ifdef CONFIG_DEBUG_NET_SMALL_RTNL
  174. /* Move to a better place when the config guard is removed. */
  175. struct mutex rtnl_mutex;
  176. #endif
  177. #if IS_ENABLED(CONFIG_VSOCKETS)
  178. struct netns_vsock vsock;
  179. #endif
  180. } __randomize_layout;
  181. #include <linux/seq_file_net.h>
  182. /* Init's network namespace */
  183. extern struct net init_net;
  184. #ifdef CONFIG_NET_NS
  185. struct net *copy_net_ns(u64 flags, struct user_namespace *user_ns,
  186. struct net *old_net);
  187. void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid);
  188. void net_ns_barrier(void);
  189. struct ns_common *get_net_ns(struct ns_common *ns);
  190. struct net *get_net_ns_by_fd(int fd);
  191. extern struct task_struct *cleanup_net_task;
  192. #else /* CONFIG_NET_NS */
  193. #include <linux/sched.h>
  194. #include <linux/nsproxy.h>
  195. static inline struct net *copy_net_ns(u64 flags,
  196. struct user_namespace *user_ns, struct net *old_net)
  197. {
  198. if (flags & CLONE_NEWNET)
  199. return ERR_PTR(-EINVAL);
  200. return old_net;
  201. }
  202. static inline void net_ns_get_ownership(const struct net *net,
  203. kuid_t *uid, kgid_t *gid)
  204. {
  205. *uid = GLOBAL_ROOT_UID;
  206. *gid = GLOBAL_ROOT_GID;
  207. }
  208. static inline void net_ns_barrier(void) {}
  209. static inline struct ns_common *get_net_ns(struct ns_common *ns)
  210. {
  211. return ERR_PTR(-EINVAL);
  212. }
  213. static inline struct net *get_net_ns_by_fd(int fd)
  214. {
  215. return ERR_PTR(-EINVAL);
  216. }
  217. #endif /* CONFIG_NET_NS */
  218. extern struct list_head net_namespace_list;
  219. struct net *get_net_ns_by_pid(pid_t pid);
  220. #ifdef CONFIG_SYSCTL
  221. void ipx_register_sysctl(void);
  222. void ipx_unregister_sysctl(void);
  223. #else
  224. #define ipx_register_sysctl()
  225. #define ipx_unregister_sysctl()
  226. #endif
  227. static inline struct net *to_net_ns(struct ns_common *ns)
  228. {
  229. return container_of(ns, struct net, ns);
  230. }
  231. #ifdef CONFIG_NET_NS
  232. void __put_net(struct net *net);
  233. /* Try using get_net_track() instead */
  234. static inline struct net *get_net(struct net *net)
  235. {
  236. ns_ref_inc(net);
  237. return net;
  238. }
  239. static inline struct net *maybe_get_net(struct net *net)
  240. {
  241. /* Used when we know struct net exists but we
  242. * aren't guaranteed a previous reference count
  243. * exists. If the reference count is zero this
  244. * function fails and returns NULL.
  245. */
  246. if (!ns_ref_get(net))
  247. net = NULL;
  248. return net;
  249. }
  250. /* Try using put_net_track() instead */
  251. static inline void put_net(struct net *net)
  252. {
  253. if (ns_ref_put(net))
  254. __put_net(net);
  255. }
  256. static inline
  257. int net_eq(const struct net *net1, const struct net *net2)
  258. {
  259. return net1 == net2;
  260. }
  261. static inline int check_net(const struct net *net)
  262. {
  263. return ns_ref_read(net) != 0;
  264. }
  265. void net_drop_ns(struct ns_common *);
  266. void net_passive_dec(struct net *net);
  267. #else
  268. static inline struct net *get_net(struct net *net)
  269. {
  270. return net;
  271. }
  272. static inline void put_net(struct net *net)
  273. {
  274. }
  275. static inline struct net *maybe_get_net(struct net *net)
  276. {
  277. return net;
  278. }
  279. static inline
  280. int net_eq(const struct net *net1, const struct net *net2)
  281. {
  282. return 1;
  283. }
  284. static inline int check_net(const struct net *net)
  285. {
  286. return 1;
  287. }
  288. #define net_drop_ns NULL
  289. static inline void net_passive_dec(struct net *net)
  290. {
  291. refcount_dec(&net->passive);
  292. }
  293. #endif
  294. static inline void net_passive_inc(struct net *net)
  295. {
  296. refcount_inc(&net->passive);
  297. }
  298. /* Returns true if the netns initialization is completed successfully */
  299. static inline bool net_initialized(const struct net *net)
  300. {
  301. return READ_ONCE(net->list.next);
  302. }
  303. static inline void __netns_tracker_alloc(struct net *net,
  304. netns_tracker *tracker,
  305. bool refcounted,
  306. gfp_t gfp)
  307. {
  308. #ifdef CONFIG_NET_NS_REFCNT_TRACKER
  309. ref_tracker_alloc(refcounted ? &net->refcnt_tracker :
  310. &net->notrefcnt_tracker,
  311. tracker, gfp);
  312. #endif
  313. }
  314. static inline void netns_tracker_alloc(struct net *net, netns_tracker *tracker,
  315. gfp_t gfp)
  316. {
  317. __netns_tracker_alloc(net, tracker, true, gfp);
  318. }
  319. static inline void __netns_tracker_free(struct net *net,
  320. netns_tracker *tracker,
  321. bool refcounted)
  322. {
  323. #ifdef CONFIG_NET_NS_REFCNT_TRACKER
  324. ref_tracker_free(refcounted ? &net->refcnt_tracker :
  325. &net->notrefcnt_tracker, tracker);
  326. #endif
  327. }
  328. static inline struct net *get_net_track(struct net *net,
  329. netns_tracker *tracker, gfp_t gfp)
  330. {
  331. get_net(net);
  332. netns_tracker_alloc(net, tracker, gfp);
  333. return net;
  334. }
  335. static inline void put_net_track(struct net *net, netns_tracker *tracker)
  336. {
  337. __netns_tracker_free(net, tracker, true);
  338. put_net(net);
  339. }
  340. typedef struct {
  341. #ifdef CONFIG_NET_NS
  342. struct net __rcu *net;
  343. #endif
  344. } possible_net_t;
  345. static inline void write_pnet(possible_net_t *pnet, struct net *net)
  346. {
  347. #ifdef CONFIG_NET_NS
  348. rcu_assign_pointer(pnet->net, net);
  349. #endif
  350. }
  351. static inline struct net *read_pnet(const possible_net_t *pnet)
  352. {
  353. #ifdef CONFIG_NET_NS
  354. return rcu_dereference_protected(pnet->net, true);
  355. #else
  356. return &init_net;
  357. #endif
  358. }
  359. static inline struct net *read_pnet_rcu(const possible_net_t *pnet)
  360. {
  361. #ifdef CONFIG_NET_NS
  362. return rcu_dereference(pnet->net);
  363. #else
  364. return &init_net;
  365. #endif
  366. }
  367. /* Protected by net_rwsem */
  368. #define for_each_net(VAR) \
  369. list_for_each_entry(VAR, &net_namespace_list, list)
  370. #define for_each_net_continue_reverse(VAR) \
  371. list_for_each_entry_continue_reverse(VAR, &net_namespace_list, list)
  372. #define for_each_net_rcu(VAR) \
  373. list_for_each_entry_rcu(VAR, &net_namespace_list, list)
  374. #ifdef CONFIG_NET_NS
  375. #define __net_init
  376. #define __net_exit
  377. #define __net_initdata
  378. #define __net_initconst
  379. #else
  380. #define __net_init __init
  381. #define __net_exit __ref
  382. #define __net_initdata __initdata
  383. #define __net_initconst __initconst
  384. #endif
  385. int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp);
  386. int peernet2id(const struct net *net, struct net *peer);
  387. bool peernet_has_id(const struct net *net, struct net *peer);
  388. struct net *get_net_ns_by_id(const struct net *net, int id);
  389. struct pernet_operations {
  390. struct list_head list;
  391. /*
  392. * Below methods are called without any exclusive locks.
  393. * More than one net may be constructed and destructed
  394. * in parallel on several cpus. Every pernet_operations
  395. * have to keep in mind all other pernet_operations and
  396. * to introduce a locking, if they share common resources.
  397. *
  398. * The only time they are called with exclusive lock is
  399. * from register_pernet_subsys(), unregister_pernet_subsys()
  400. * register_pernet_device() and unregister_pernet_device().
  401. *
  402. * Exit methods using blocking RCU primitives, such as
  403. * synchronize_rcu(), should be implemented via exit_batch.
  404. * Then, destruction of a group of net requires single
  405. * synchronize_rcu() related to these pernet_operations,
  406. * instead of separate synchronize_rcu() for every net.
  407. * Please, avoid synchronize_rcu() at all, where it's possible.
  408. *
  409. * Note that a combination of pre_exit() and exit() can
  410. * be used, since a synchronize_rcu() is guaranteed between
  411. * the calls.
  412. */
  413. int (*init)(struct net *net);
  414. void (*pre_exit)(struct net *net);
  415. void (*exit)(struct net *net);
  416. void (*exit_batch)(struct list_head *net_exit_list);
  417. /* Following method is called with RTNL held. */
  418. void (*exit_rtnl)(struct net *net,
  419. struct list_head *dev_kill_list);
  420. unsigned int * const id;
  421. const size_t size;
  422. };
  423. /*
  424. * Use these carefully. If you implement a network device and it
  425. * needs per network namespace operations use device pernet operations,
  426. * otherwise use pernet subsys operations.
  427. *
  428. * Network interfaces need to be removed from a dying netns _before_
  429. * subsys notifiers can be called, as most of the network code cleanup
  430. * (which is done from subsys notifiers) runs with the assumption that
  431. * dev_remove_pack has been called so no new packets will arrive during
  432. * and after the cleanup functions have been called. dev_remove_pack
  433. * is not per namespace so instead the guarantee of no more packets
  434. * arriving in a network namespace is provided by ensuring that all
  435. * network devices and all sockets have left the network namespace
  436. * before the cleanup methods are called.
  437. *
  438. * For the longest time the ipv4 icmp code was registered as a pernet
  439. * device which caused kernel oops, and panics during network
  440. * namespace cleanup. So please don't get this wrong.
  441. */
  442. int register_pernet_subsys(struct pernet_operations *);
  443. void unregister_pernet_subsys(struct pernet_operations *);
  444. int register_pernet_device(struct pernet_operations *);
  445. void unregister_pernet_device(struct pernet_operations *);
  446. struct ctl_table;
  447. #define register_net_sysctl(net, path, table) \
  448. register_net_sysctl_sz(net, path, table, ARRAY_SIZE(table))
  449. #ifdef CONFIG_SYSCTL
  450. int net_sysctl_init(void);
  451. struct ctl_table_header *register_net_sysctl_sz(struct net *net, const char *path,
  452. struct ctl_table *table, size_t table_size);
  453. void unregister_net_sysctl_table(struct ctl_table_header *header);
  454. #else
  455. static inline int net_sysctl_init(void) { return 0; }
  456. static inline struct ctl_table_header *register_net_sysctl_sz(struct net *net,
  457. const char *path, struct ctl_table *table, size_t table_size)
  458. {
  459. return NULL;
  460. }
  461. static inline void unregister_net_sysctl_table(struct ctl_table_header *header)
  462. {
  463. }
  464. #endif
  465. static inline int rt_genid_ipv4(const struct net *net)
  466. {
  467. return atomic_read(&net->ipv4.rt_genid);
  468. }
  469. #if IS_ENABLED(CONFIG_IPV6)
  470. static inline int rt_genid_ipv6(const struct net *net)
  471. {
  472. return atomic_read(&net->ipv6.fib6_sernum);
  473. }
  474. #endif
  475. static inline void rt_genid_bump_ipv4(struct net *net)
  476. {
  477. atomic_inc(&net->ipv4.rt_genid);
  478. }
  479. extern void (*__fib6_flush_trees)(struct net *net);
  480. static inline void rt_genid_bump_ipv6(struct net *net)
  481. {
  482. if (__fib6_flush_trees)
  483. __fib6_flush_trees(net);
  484. }
  485. #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
  486. static inline struct netns_ieee802154_lowpan *
  487. net_ieee802154_lowpan(struct net *net)
  488. {
  489. return &net->ieee802154_lowpan;
  490. }
  491. #endif
  492. /* For callers who don't really care about whether it's IPv4 or IPv6 */
  493. static inline void rt_genid_bump_all(struct net *net)
  494. {
  495. rt_genid_bump_ipv4(net);
  496. rt_genid_bump_ipv6(net);
  497. }
  498. static inline int fnhe_genid(const struct net *net)
  499. {
  500. return atomic_read(&net->fnhe_genid);
  501. }
  502. static inline void fnhe_genid_bump(struct net *net)
  503. {
  504. atomic_inc(&net->fnhe_genid);
  505. }
  506. #ifdef CONFIG_NET
  507. void net_ns_init(void);
  508. #else
  509. static inline void net_ns_init(void) {}
  510. #endif
  511. #endif /* __NET_NET_NAMESPACE_H */