net_namespace.h 4.1 KB

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