net_namespace.h 3.5 KB

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