net_namespace.h 9.3 KB

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