net_namespace.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Operations on the network namespace
  3. */
  4. #ifndef __NET_NET_NAMESPACE_H
  5. #define __NET_NET_NAMESPACE_H
  6. #include <asm/atomic.h>
  7. #include <linux/workqueue.h>
  8. #include <linux/list.h>
  9. #include <net/netns/core.h>
  10. #include <net/netns/unix.h>
  11. #include <net/netns/packet.h>
  12. #include <net/netns/ipv4.h>
  13. #include <net/netns/ipv6.h>
  14. #include <net/netns/dccp.h>
  15. #include <net/netns/x_tables.h>
  16. struct proc_dir_entry;
  17. struct net_device;
  18. struct sock;
  19. struct ctl_table_header;
  20. struct net {
  21. atomic_t count; /* To decided when the network
  22. * namespace should be freed.
  23. */
  24. atomic_t use_count; /* To track references we
  25. * destroy on demand
  26. */
  27. struct list_head list; /* list of network namespaces */
  28. struct work_struct work; /* work struct for freeing */
  29. struct proc_dir_entry *proc_net;
  30. struct proc_dir_entry *proc_net_stat;
  31. struct list_head sysctl_table_headers;
  32. struct net_device *loopback_dev; /* The loopback */
  33. struct list_head dev_base_head;
  34. struct hlist_head *dev_name_head;
  35. struct hlist_head *dev_index_head;
  36. /* core fib_rules */
  37. struct list_head rules_ops;
  38. spinlock_t rules_mod_lock;
  39. struct sock *rtnl; /* rtnetlink socket */
  40. struct netns_core core;
  41. struct netns_packet packet;
  42. struct netns_unix unx;
  43. struct netns_ipv4 ipv4;
  44. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  45. struct netns_ipv6 ipv6;
  46. #endif
  47. #if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)
  48. struct netns_dccp dccp;
  49. #endif
  50. #ifdef CONFIG_NETFILTER
  51. struct netns_xt xt;
  52. #endif
  53. };
  54. #include <linux/seq_file_net.h>
  55. /* Init's network namespace */
  56. extern struct net init_net;
  57. #ifdef CONFIG_NET
  58. #define INIT_NET_NS(net_ns) .net_ns = &init_net,
  59. extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
  60. #else /* CONFIG_NET */
  61. #define INIT_NET_NS(net_ns)
  62. static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
  63. {
  64. /* There is nothing to copy so this is a noop */
  65. return net_ns;
  66. }
  67. #endif /* CONFIG_NET */
  68. extern struct list_head net_namespace_list;
  69. #ifdef CONFIG_NET_NS
  70. extern void __put_net(struct net *net);
  71. static inline struct net *get_net(struct net *net)
  72. {
  73. atomic_inc(&net->count);
  74. return net;
  75. }
  76. static inline struct net *maybe_get_net(struct net *net)
  77. {
  78. /* Used when we know struct net exists but we
  79. * aren't guaranteed a previous reference count
  80. * exists. If the reference count is zero this
  81. * function fails and returns NULL.
  82. */
  83. if (!atomic_inc_not_zero(&net->count))
  84. net = NULL;
  85. return net;
  86. }
  87. static inline void put_net(struct net *net)
  88. {
  89. if (atomic_dec_and_test(&net->count))
  90. __put_net(net);
  91. }
  92. static inline struct net *hold_net(struct net *net)
  93. {
  94. atomic_inc(&net->use_count);
  95. return net;
  96. }
  97. static inline void release_net(struct net *net)
  98. {
  99. atomic_dec(&net->use_count);
  100. }
  101. static inline
  102. int net_eq(const struct net *net1, const struct net *net2)
  103. {
  104. return net1 == net2;
  105. }
  106. #else
  107. static inline struct net *get_net(struct net *net)
  108. {
  109. return net;
  110. }
  111. static inline void put_net(struct net *net)
  112. {
  113. }
  114. static inline struct net *hold_net(struct net *net)
  115. {
  116. return net;
  117. }
  118. static inline void release_net(struct net *net)
  119. {
  120. }
  121. static inline struct net *maybe_get_net(struct net *net)
  122. {
  123. return net;
  124. }
  125. static inline
  126. int net_eq(const struct net *net1, const struct net *net2)
  127. {
  128. return 1;
  129. }
  130. #endif
  131. #define for_each_net(VAR) \
  132. list_for_each_entry(VAR, &net_namespace_list, list)
  133. #ifdef CONFIG_NET_NS
  134. #define __net_init
  135. #define __net_exit
  136. #define __net_initdata
  137. #else
  138. #define __net_init __init
  139. #define __net_exit __exit_refok
  140. #define __net_initdata __initdata
  141. #endif
  142. struct pernet_operations {
  143. struct list_head list;
  144. int (*init)(struct net *net);
  145. void (*exit)(struct net *net);
  146. };
  147. extern int register_pernet_subsys(struct pernet_operations *);
  148. extern void unregister_pernet_subsys(struct pernet_operations *);
  149. extern int register_pernet_device(struct pernet_operations *);
  150. extern void unregister_pernet_device(struct pernet_operations *);
  151. struct ctl_path;
  152. struct ctl_table;
  153. struct ctl_table_header;
  154. extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
  155. const struct ctl_path *path, struct ctl_table *table);
  156. extern void unregister_net_sysctl_table(struct ctl_table_header *header);
  157. #endif /* __NET_NET_NAMESPACE_H */