address_tracker_linux.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_BASE_ADDRESS_TRACKER_LINUX_H_
  5. #define NET_BASE_ADDRESS_TRACKER_LINUX_H_
  6. #include <sys/socket.h> // Needed to include netlink.
  7. // Mask superfluous definition of |struct net|. This is fixed in Linux 2.6.38.
  8. #define net net_kernel
  9. #include <linux/rtnetlink.h>
  10. #undef net
  11. #include <stddef.h>
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include <unordered_set>
  16. #include "base/callback.h"
  17. #include "base/compiler_specific.h"
  18. #include "base/files/file_descriptor_watcher_posix.h"
  19. #include "base/files/scoped_file.h"
  20. #include "base/gtest_prod_util.h"
  21. #include "base/synchronization/condition_variable.h"
  22. #include "base/synchronization/lock.h"
  23. #include "base/threading/thread_checker.h"
  24. #include "net/base/ip_address.h"
  25. #include "net/base/net_export.h"
  26. #include "net/base/network_change_notifier.h"
  27. namespace net::internal {
  28. // Keeps track of network interface addresses using rtnetlink. Used by
  29. // NetworkChangeNotifier to provide signals to registered IPAddressObservers.
  30. class NET_EXPORT_PRIVATE AddressTrackerLinux {
  31. public:
  32. typedef std::map<IPAddress, struct ifaddrmsg> AddressMap;
  33. // Non-tracking version constructor: it takes a snapshot of the
  34. // current system configuration. Once Init() returns, the
  35. // configuration is available through GetOnlineLinks() and
  36. // GetAddressMap().
  37. AddressTrackerLinux();
  38. // Tracking version constructor: it will run |address_callback| when
  39. // the AddressMap changes, |link_callback| when the list of online
  40. // links changes, and |tunnel_callback| when the list of online
  41. // tunnels changes.
  42. // |ignored_interfaces| is the list of interfaces to ignore. Changes to an
  43. // ignored interface will not cause any callback to be run. An ignored
  44. // interface will not have entries in GetAddressMap() and GetOnlineLinks().
  45. // NOTE: Only ignore interfaces not used to connect to the internet. Adding
  46. // interfaces used to connect to the internet can cause critical network
  47. // changed signals to be lost allowing incorrect stale state to persist.
  48. AddressTrackerLinux(
  49. const base::RepeatingClosure& address_callback,
  50. const base::RepeatingClosure& link_callback,
  51. const base::RepeatingClosure& tunnel_callback,
  52. const std::unordered_set<std::string>& ignored_interfaces);
  53. virtual ~AddressTrackerLinux();
  54. // In tracking mode, it starts watching the system configuration for
  55. // changes. The current thread must have a MessageLoopForIO. In
  56. // non-tracking mode, once Init() returns, a snapshot of the system
  57. // configuration is available through GetOnlineLinks() and
  58. // GetAddressMap().
  59. void Init();
  60. AddressMap GetAddressMap() const;
  61. // Returns set of interface indices for online interfaces.
  62. std::unordered_set<int> GetOnlineLinks() const;
  63. // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType().
  64. // Safe to call from any thread, but will block until Init() has completed.
  65. NetworkChangeNotifier::ConnectionType GetCurrentConnectionType();
  66. // Returns the name for the interface with interface index |interface_index|.
  67. // |buf| should be a pointer to an array of size IFNAMSIZ. The returned
  68. // pointer will point to |buf|. This function acts like if_indextoname which
  69. // cannot be used as net/if.h cannot be mixed with linux/if.h. We'll stick
  70. // with exclusively talking to the kernel and not the C library.
  71. static char* GetInterfaceName(int interface_index, char* buf);
  72. // Does |name| refer to a tunnel interface?
  73. static bool IsTunnelInterfaceName(const char* name);
  74. private:
  75. friend class AddressTrackerLinuxTest;
  76. FRIEND_TEST_ALL_PREFIXES(AddressTrackerLinuxNetlinkTest,
  77. TestInitializeTwoTrackers);
  78. FRIEND_TEST_ALL_PREFIXES(AddressTrackerLinuxNetlinkTest,
  79. TestInitializeTwoTrackersInPidNamespaces);
  80. friend int ChildProcessInitializeTrackerForTesting();
  81. // In tracking mode, holds |lock| while alive. In non-tracking mode,
  82. // enforces single-threaded access.
  83. class AddressTrackerAutoLock {
  84. public:
  85. AddressTrackerAutoLock(const AddressTrackerLinux& tracker,
  86. base::Lock& lock);
  87. AddressTrackerAutoLock(const AddressTrackerAutoLock&) = delete;
  88. AddressTrackerAutoLock& operator=(const AddressTrackerAutoLock&) = delete;
  89. ~AddressTrackerAutoLock();
  90. private:
  91. const AddressTrackerLinux& tracker_;
  92. base::Lock& lock_;
  93. };
  94. // A function that returns the name of an interface given the interface index
  95. // in |interface_index|. |ifname| should be a buffer of size IFNAMSIZ. The
  96. // function should return a pointer to |ifname|.
  97. typedef char* (*GetInterfaceNameFunction)(int interface_index, char* ifname);
  98. // Sets |*address_changed| to indicate whether |address_map_| changed and
  99. // sets |*link_changed| to indicate if |online_links_| changed and sets
  100. // |*tunnel_changed| to indicate if |online_links_| changed with regards to a
  101. // tunnel interface while reading messages from |netlink_fd_|.
  102. void ReadMessages(bool* address_changed,
  103. bool* link_changed,
  104. bool* tunnel_changed);
  105. // Sets |*address_changed| to true if |address_map_| changed, sets
  106. // |*link_changed| to true if |online_links_| changed, sets |*tunnel_changed|
  107. // to true if |online_links_| changed with regards to a tunnel interface while
  108. // reading the message from |buffer|.
  109. void HandleMessage(const char* buffer,
  110. int length,
  111. bool* address_changed,
  112. bool* link_changed,
  113. bool* tunnel_changed);
  114. // Call when some part of initialization failed; forces online and unblocks.
  115. void AbortAndForceOnline();
  116. // Called by |watcher_| when |netlink_fd_| can be read without blocking.
  117. void OnFileCanReadWithoutBlocking();
  118. // Does |interface_index| refer to a tunnel interface?
  119. bool IsTunnelInterface(int interface_index) const;
  120. // Is interface with index |interface_index| in list of ignored interfaces?
  121. bool IsInterfaceIgnored(int interface_index) const;
  122. // Updates current_connection_type_ based on the network list.
  123. void UpdateCurrentConnectionType();
  124. // Used by AddressTrackerLinuxTest, returns the number of threads waiting
  125. // for |connection_type_initialized_cv_|.
  126. int GetThreadsWaitingForConnectionTypeInitForTesting();
  127. // Used by AddressTrackerLinuxNetlinkTest, returns true iff `Init` succeeded.
  128. // Undefined for non-tracking mode.
  129. bool DidTrackingInitSucceedForTesting() const;
  130. // Gets the name of an interface given the interface index |interface_index|.
  131. // May return empty string if it fails but should not return NULL. This is
  132. // overridden by tests.
  133. GetInterfaceNameFunction get_interface_name_;
  134. base::RepeatingClosure address_callback_;
  135. base::RepeatingClosure link_callback_;
  136. base::RepeatingClosure tunnel_callback_;
  137. // Note that |watcher_| must be inactive when |netlink_fd_| is closed.
  138. base::ScopedFD netlink_fd_;
  139. std::unique_ptr<base::FileDescriptorWatcher::Controller> watcher_;
  140. mutable base::Lock address_map_lock_;
  141. AddressMap address_map_;
  142. // Set of interface indices for links that are currently online.
  143. mutable base::Lock online_links_lock_;
  144. std::unordered_set<int> online_links_;
  145. // Set of interface names that should be ignored.
  146. const std::unordered_set<std::string> ignored_interfaces_;
  147. base::Lock connection_type_lock_;
  148. bool connection_type_initialized_ = false;
  149. base::ConditionVariable connection_type_initialized_cv_;
  150. NetworkChangeNotifier::ConnectionType current_connection_type_ =
  151. NetworkChangeNotifier::CONNECTION_NONE;
  152. bool tracking_;
  153. int threads_waiting_for_connection_type_initialization_ = 0;
  154. // Used to verify single-threaded access in non-tracking mode.
  155. base::ThreadChecker thread_checker_;
  156. };
  157. } // namespace net::internal
  158. #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_