net_namespace.h 3.6 KB

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