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