net_namespace.h 14 KB

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