net_namespace.h 8.8 KB

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