net_namespace.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. struct proc_dir_entry;
  14. struct net_device;
  15. struct sock;
  16. struct ctl_table_header;
  17. struct net {
  18. atomic_t count; /* To decided when the network
  19. * namespace should be freed.
  20. */
  21. atomic_t use_count; /* To track references we
  22. * destroy on demand
  23. */
  24. struct list_head list; /* list of network namespaces */
  25. struct work_struct work; /* work struct for freeing */
  26. struct proc_dir_entry *proc_net;
  27. struct proc_dir_entry *proc_net_stat;
  28. struct proc_dir_entry *proc_net_root;
  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. };
  48. #ifdef CONFIG_NET
  49. /* Init's network namespace */
  50. extern struct net init_net;
  51. #define INIT_NET_NS(net_ns) .net_ns = &init_net,
  52. #else
  53. #define INIT_NET_NS(net_ns)
  54. #endif
  55. extern struct list_head net_namespace_list;
  56. #ifdef CONFIG_NET
  57. extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
  58. #else
  59. static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
  60. {
  61. /* There is nothing to copy so this is a noop */
  62. return net_ns;
  63. }
  64. #endif
  65. #ifdef CONFIG_NET_NS
  66. extern void __put_net(struct net *net);
  67. static inline struct net *get_net(struct net *net)
  68. {
  69. atomic_inc(&net->count);
  70. return net;
  71. }
  72. static inline struct net *maybe_get_net(struct net *net)
  73. {
  74. /* Used when we know struct net exists but we
  75. * aren't guaranteed a previous reference count
  76. * exists. If the reference count is zero this
  77. * function fails and returns NULL.
  78. */
  79. if (!atomic_inc_not_zero(&net->count))
  80. net = NULL;
  81. return net;
  82. }
  83. static inline void put_net(struct net *net)
  84. {
  85. if (atomic_dec_and_test(&net->count))
  86. __put_net(net);
  87. }
  88. static inline struct net *hold_net(struct net *net)
  89. {
  90. atomic_inc(&net->use_count);
  91. return net;
  92. }
  93. static inline void release_net(struct net *net)
  94. {
  95. atomic_dec(&net->use_count);
  96. }
  97. #else
  98. static inline struct net *get_net(struct net *net)
  99. {
  100. return net;
  101. }
  102. static inline void put_net(struct net *net)
  103. {
  104. }
  105. static inline struct net *hold_net(struct net *net)
  106. {
  107. return net;
  108. }
  109. static inline void release_net(struct net *net)
  110. {
  111. }
  112. static inline struct net *maybe_get_net(struct net *net)
  113. {
  114. return net;
  115. }
  116. #endif
  117. #define for_each_net(VAR) \
  118. list_for_each_entry(VAR, &net_namespace_list, list)
  119. #ifdef CONFIG_NET_NS
  120. #define __net_init
  121. #define __net_exit
  122. #define __net_initdata
  123. #else
  124. #define __net_init __init
  125. #define __net_exit __exit_refok
  126. #define __net_initdata __initdata
  127. #endif
  128. struct pernet_operations {
  129. struct list_head list;
  130. int (*init)(struct net *net);
  131. void (*exit)(struct net *net);
  132. };
  133. extern int register_pernet_subsys(struct pernet_operations *);
  134. extern void unregister_pernet_subsys(struct pernet_operations *);
  135. extern int register_pernet_device(struct pernet_operations *);
  136. extern void unregister_pernet_device(struct pernet_operations *);
  137. struct ctl_path;
  138. struct ctl_table;
  139. struct ctl_table_header;
  140. extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
  141. const struct ctl_path *path, struct ctl_table *table);
  142. extern void unregister_net_sysctl_table(struct ctl_table_header *header);
  143. #endif /* __NET_NET_NAMESPACE_H */