net_namespace.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /* unix sockets */
  34. int sysctl_unix_max_dgram_qlen;
  35. };
  36. #ifdef CONFIG_NET
  37. /* Init's network namespace */
  38. extern struct net init_net;
  39. #define INIT_NET_NS(net_ns) .net_ns = &init_net,
  40. #else
  41. #define INIT_NET_NS(net_ns)
  42. #endif
  43. extern struct list_head net_namespace_list;
  44. #ifdef CONFIG_NET
  45. extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
  46. #else
  47. static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
  48. {
  49. /* There is nothing to copy so this is a noop */
  50. return net_ns;
  51. }
  52. #endif
  53. #ifdef CONFIG_NET_NS
  54. extern void __put_net(struct net *net);
  55. static inline struct net *get_net(struct net *net)
  56. {
  57. atomic_inc(&net->count);
  58. return net;
  59. }
  60. static inline struct net *maybe_get_net(struct net *net)
  61. {
  62. /* Used when we know struct net exists but we
  63. * aren't guaranteed a previous reference count
  64. * exists. If the reference count is zero this
  65. * function fails and returns NULL.
  66. */
  67. if (!atomic_inc_not_zero(&net->count))
  68. net = NULL;
  69. return net;
  70. }
  71. static inline void put_net(struct net *net)
  72. {
  73. if (atomic_dec_and_test(&net->count))
  74. __put_net(net);
  75. }
  76. static inline struct net *hold_net(struct net *net)
  77. {
  78. atomic_inc(&net->use_count);
  79. return net;
  80. }
  81. static inline void release_net(struct net *net)
  82. {
  83. atomic_dec(&net->use_count);
  84. }
  85. #else
  86. static inline struct net *get_net(struct net *net)
  87. {
  88. return net;
  89. }
  90. static inline void put_net(struct net *net)
  91. {
  92. }
  93. static inline struct net *hold_net(struct net *net)
  94. {
  95. return net;
  96. }
  97. static inline void release_net(struct net *net)
  98. {
  99. }
  100. static inline struct net *maybe_get_net(struct net *net)
  101. {
  102. return net;
  103. }
  104. #endif
  105. #define for_each_net(VAR) \
  106. list_for_each_entry(VAR, &net_namespace_list, list)
  107. #ifdef CONFIG_NET_NS
  108. #define __net_init
  109. #define __net_exit
  110. #define __net_initdata
  111. #else
  112. #define __net_init __init
  113. #define __net_exit __exit_refok
  114. #define __net_initdata __initdata
  115. #endif
  116. struct pernet_operations {
  117. struct list_head list;
  118. int (*init)(struct net *net);
  119. void (*exit)(struct net *net);
  120. };
  121. extern int register_pernet_subsys(struct pernet_operations *);
  122. extern void unregister_pernet_subsys(struct pernet_operations *);
  123. extern int register_pernet_device(struct pernet_operations *);
  124. extern void unregister_pernet_device(struct pernet_operations *);
  125. struct ctl_path;
  126. struct ctl_table;
  127. struct ctl_table_header;
  128. extern struct ctl_table_header *register_net_sysctl_table(struct net *net,
  129. const struct ctl_path *path, struct ctl_table *table);
  130. extern void unregister_net_sysctl_table(struct ctl_table_header *header);
  131. #endif /* __NET_NET_NAMESPACE_H */