net_namespace.h 8.2 KB

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