net_namespace.h 9.2 KB

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