net_namespace.h 14 KB

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