net_namespace.h 3.6 KB

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