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