net_namespace.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 proc_dir_entry *proc_net_root;
  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. /* core sysctls */
  40. struct ctl_table_header *sysctl_core_hdr;
  41. int sysctl_somaxconn;
  42. struct netns_packet packet;
  43. struct netns_unix unx;
  44. struct netns_ipv4 ipv4;
  45. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  46. struct netns_ipv6 ipv6;
  47. #endif
  48. #ifdef CONFIG_NETFILTER
  49. struct netns_xt xt;
  50. #endif
  51. };
  52. #ifdef CONFIG_NET
  53. /* Init's network namespace */
  54. extern struct net init_net;
  55. #define INIT_NET_NS(net_ns) .net_ns = &init_net,
  56. #else
  57. #define INIT_NET_NS(net_ns)
  58. #endif
  59. extern struct list_head net_namespace_list;
  60. #ifdef CONFIG_NET
  61. extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
  62. #else
  63. static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
  64. {
  65. /* There is nothing to copy so this is a noop */
  66. return net_ns;
  67. }
  68. #endif
  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. #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. #endif
  121. #define for_each_net(VAR) \
  122. list_for_each_entry(VAR, &net_namespace_list, list)
  123. #ifdef CONFIG_NET_NS
  124. #define __net_init
  125. #define __net_exit
  126. #define __net_initdata
  127. #else
  128. #define __net_init __init
  129. #define __net_exit __exit_refok
  130. #define __net_initdata __initdata
  131. #endif
  132. struct pernet_operations {
  133. struct list_head list;
  134. int (*init)(struct net *net);
  135. void (*exit)(struct net *net);
  136. };
  137. extern int register_pernet_subsys(struct pernet_operations *);
  138. extern void unregister_pernet_subsys(struct pernet_operations *);
  139. extern int register_pernet_device(struct pernet_operations *);
  140. extern void unregister_pernet_device(struct pernet_operations *);
  141. struct ctl_path;
  142. struct ctl_table;
  143. struct ctl_table_header;
  144. extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
  145. const struct ctl_path *path, struct ctl_table *table);
  146. extern void unregister_net_sysctl_table(struct ctl_table_header *header);
  147. #endif /* __NET_NET_NAMESPACE_H */