OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/address_tracker_linux.h" | 5 #include "net/base/address_tracker_linux.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <linux/if.h> | 8 #include <linux/if.h> |
9 #include <sys/ioctl.h> | 9 #include <sys/ioctl.h> |
10 | 10 |
11 #include "base/files/scoped_file.h" | |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/posix/eintr_wrapper.h" | 13 #include "base/posix/eintr_wrapper.h" |
13 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
15 #include "net/base/net_util_linux.h" | |
14 | 16 |
15 namespace net { | 17 namespace net { |
16 namespace internal { | 18 namespace internal { |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 // Retrieves address from NETLINK address message. | 22 // Retrieves address from NETLINK address message. |
21 // Sets |really_deprecated| for IPv6 addresses with preferred lifetimes of 0. | 23 // Sets |really_deprecated| for IPv6 addresses with preferred lifetimes of 0. |
22 bool GetAddress(const struct nlmsghdr* header, | 24 bool GetAddress(const struct nlmsghdr* header, |
23 IPAddressNumber* out, | 25 IPAddressNumber* out, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 } | 71 } |
70 } | 72 } |
71 if (local) | 73 if (local) |
72 address = local; | 74 address = local; |
73 if (!address) | 75 if (!address) |
74 return false; | 76 return false; |
75 out->assign(address, address + address_length); | 77 out->assign(address, address + address_length); |
76 return true; | 78 return true; |
77 } | 79 } |
78 | 80 |
79 // Returns the name for the interface with interface index |interface_index|. | |
80 // The return value points to a function-scoped static so it may be changed by | |
81 // subsequent calls. This function could be replaced with if_indextoname() but | |
82 // net/if.h cannot be mixed with linux/if.h so we'll stick with exclusively | |
83 // talking to the kernel and not the C library. | |
84 const char* GetInterfaceName(int interface_index) { | |
85 int ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0); | |
86 if (ioctl_socket < 0) | |
87 return ""; | |
88 static struct ifreq ifr; | |
89 memset(&ifr, 0, sizeof(ifr)); | |
90 ifr.ifr_ifindex = interface_index; | |
91 int rv = ioctl(ioctl_socket, SIOCGIFNAME, &ifr); | |
92 close(ioctl_socket); | |
93 if (rv != 0) | |
94 return ""; | |
95 return ifr.ifr_name; | |
96 } | |
97 | |
98 } // namespace | 81 } // namespace |
99 | 82 |
83 // static | |
84 char* AddressTrackerLinux::GetInterfaceName(int interface_index, char* buf) { | |
85 memset(buf, 0, IFNAMSIZ); | |
86 base::ScopedFD ioctl_socket(socket(AF_INET, SOCK_DGRAM, 0)); | |
87 if (!ioctl_socket.is_valid()) | |
88 return buf; | |
89 | |
90 static struct ifreq ifr; | |
pauljensen
2015/01/28 21:15:55
um this shouldn't be static...that would make this
derekjchow1
2015/01/29 00:35:04
Done.
| |
91 memset(&ifr, 0, sizeof(ifr)); | |
pauljensen
2015/01/28 21:15:55
could we combine this line into the above line usi
derekjchow1
2015/01/29 00:35:04
Done.
| |
92 ifr.ifr_ifindex = interface_index; | |
pauljensen
2015/01/28 21:15:55
could we combine this line into the above declarat
derekjchow1
2015/01/29 00:35:04
Done.
| |
93 int rv = ioctl(ioctl_socket.get(), SIOCGIFNAME, &ifr); | |
94 if (rv != 0) | |
95 return buf; | |
96 | |
97 strncpy(buf, ifr.ifr_name, IFNAMSIZ - 1); | |
98 return buf; | |
pauljensen
2015/01/28 21:15:55
could we combine the last 6 lines of this function
derekjchow1
2015/01/29 00:35:04
Done.
| |
99 } | |
100 | |
100 AddressTrackerLinux::AddressTrackerLinux() | 101 AddressTrackerLinux::AddressTrackerLinux() |
101 : get_interface_name_(GetInterfaceName), | 102 : get_interface_name_(GetInterfaceName), |
102 address_callback_(base::Bind(&base::DoNothing)), | 103 address_callback_(base::Bind(&base::DoNothing)), |
103 link_callback_(base::Bind(&base::DoNothing)), | 104 link_callback_(base::Bind(&base::DoNothing)), |
104 tunnel_callback_(base::Bind(&base::DoNothing)), | 105 tunnel_callback_(base::Bind(&base::DoNothing)), |
105 netlink_fd_(-1), | 106 netlink_fd_(-1), |
106 is_offline_(true), | 107 connection_type_initialized_(false), |
107 is_offline_initialized_(false), | 108 connection_type_initialized_cv_(&connection_type_lock_), |
108 is_offline_initialized_cv_(&is_offline_lock_), | 109 current_connection_type_(NetworkChangeNotifier::CONNECTION_NONE), |
109 tracking_(false) { | 110 tracking_(false) { |
110 } | 111 } |
111 | 112 |
112 AddressTrackerLinux::AddressTrackerLinux(const base::Closure& address_callback, | 113 AddressTrackerLinux::AddressTrackerLinux(const base::Closure& address_callback, |
113 const base::Closure& link_callback, | 114 const base::Closure& link_callback, |
114 const base::Closure& tunnel_callback) | 115 const base::Closure& tunnel_callback) |
115 : get_interface_name_(GetInterfaceName), | 116 : get_interface_name_(GetInterfaceName), |
116 address_callback_(address_callback), | 117 address_callback_(address_callback), |
117 link_callback_(link_callback), | 118 link_callback_(link_callback), |
118 tunnel_callback_(tunnel_callback), | 119 tunnel_callback_(tunnel_callback), |
119 netlink_fd_(-1), | 120 netlink_fd_(-1), |
120 is_offline_(true), | 121 connection_type_initialized_(false), |
121 is_offline_initialized_(false), | 122 connection_type_initialized_cv_(&connection_type_lock_), |
122 is_offline_initialized_cv_(&is_offline_lock_), | 123 current_connection_type_(NetworkChangeNotifier::CONNECTION_NONE), |
123 tracking_(true) { | 124 tracking_(true) { |
124 DCHECK(!address_callback.is_null()); | 125 DCHECK(!address_callback.is_null()); |
125 DCHECK(!link_callback.is_null()); | 126 DCHECK(!link_callback.is_null()); |
126 } | 127 } |
127 | 128 |
128 AddressTrackerLinux::~AddressTrackerLinux() { | 129 AddressTrackerLinux::~AddressTrackerLinux() { |
129 CloseSocket(); | 130 CloseSocket(); |
130 } | 131 } |
131 | 132 |
132 void AddressTrackerLinux::Init() { | 133 void AddressTrackerLinux::Init() { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
196 sizeof(peer))); | 197 sizeof(peer))); |
197 if (rv < 0) { | 198 if (rv < 0) { |
198 PLOG(ERROR) << "Could not send NETLINK request"; | 199 PLOG(ERROR) << "Could not send NETLINK request"; |
199 AbortAndForceOnline(); | 200 AbortAndForceOnline(); |
200 return; | 201 return; |
201 } | 202 } |
202 | 203 |
203 // Consume pending message to populate links_online_, but don't notify. | 204 // Consume pending message to populate links_online_, but don't notify. |
204 ReadMessages(&address_changed, &link_changed, &tunnel_changed); | 205 ReadMessages(&address_changed, &link_changed, &tunnel_changed); |
205 { | 206 { |
206 AddressTrackerAutoLock lock(*this, is_offline_lock_); | 207 AddressTrackerAutoLock lock(*this, connection_type_lock_); |
207 is_offline_initialized_ = true; | 208 connection_type_initialized_ = true; |
208 is_offline_initialized_cv_.Signal(); | 209 connection_type_initialized_cv_.Signal(); |
209 } | 210 } |
210 | 211 |
211 if (tracking_) { | 212 if (tracking_) { |
212 rv = base::MessageLoopForIO::current()->WatchFileDescriptor( | 213 rv = base::MessageLoopForIO::current()->WatchFileDescriptor( |
213 netlink_fd_, true, base::MessageLoopForIO::WATCH_READ, &watcher_, this); | 214 netlink_fd_, true, base::MessageLoopForIO::WATCH_READ, &watcher_, this); |
214 if (rv < 0) { | 215 if (rv < 0) { |
215 PLOG(ERROR) << "Could not watch NETLINK socket"; | 216 PLOG(ERROR) << "Could not watch NETLINK socket"; |
216 AbortAndForceOnline(); | 217 AbortAndForceOnline(); |
217 return; | 218 return; |
218 } | 219 } |
219 } | 220 } |
220 } | 221 } |
221 | 222 |
222 void AddressTrackerLinux::AbortAndForceOnline() { | 223 void AddressTrackerLinux::AbortAndForceOnline() { |
223 CloseSocket(); | 224 CloseSocket(); |
224 AddressTrackerAutoLock lock(*this, is_offline_lock_); | 225 AddressTrackerAutoLock lock(*this, connection_type_lock_); |
225 is_offline_ = false; | 226 current_connection_type_ = NetworkChangeNotifier::CONNECTION_UNKNOWN; |
226 is_offline_initialized_ = true; | 227 connection_type_initialized_ = true; |
227 is_offline_initialized_cv_.Signal(); | 228 connection_type_initialized_cv_.Signal(); |
228 } | 229 } |
229 | 230 |
230 AddressTrackerLinux::AddressMap AddressTrackerLinux::GetAddressMap() const { | 231 AddressTrackerLinux::AddressMap AddressTrackerLinux::GetAddressMap() const { |
231 AddressTrackerAutoLock lock(*this, address_map_lock_); | 232 AddressTrackerAutoLock lock(*this, address_map_lock_); |
232 return address_map_; | 233 return address_map_; |
233 } | 234 } |
234 | 235 |
235 base::hash_set<int> AddressTrackerLinux::GetOnlineLinks() const { | 236 base::hash_set<int> AddressTrackerLinux::GetOnlineLinks() const { |
236 AddressTrackerAutoLock lock(*this, online_links_lock_); | 237 AddressTrackerAutoLock lock(*this, online_links_lock_); |
237 return online_links_; | 238 return online_links_; |
238 } | 239 } |
239 | 240 |
240 NetworkChangeNotifier::ConnectionType | 241 NetworkChangeNotifier::ConnectionType |
241 AddressTrackerLinux::GetCurrentConnectionType() { | 242 AddressTrackerLinux::GetCurrentConnectionType() { |
242 // http://crbug.com/125097 | 243 // http://crbug.com/125097 |
243 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 244 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
244 AddressTrackerAutoLock lock(*this, is_offline_lock_); | 245 AddressTrackerAutoLock lock(*this, connection_type_lock_); |
245 // Make sure the initial offline state is set before returning. | 246 // Make sure the initial connection type is set before returning. |
246 while (!is_offline_initialized_) { | 247 while (!connection_type_initialized_) { |
247 is_offline_initialized_cv_.Wait(); | 248 connection_type_initialized_cv_.Wait(); |
248 } | 249 } |
249 // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN. | 250 return current_connection_type_; |
250 // http://crbug.com/160537 | |
251 return is_offline_ ? NetworkChangeNotifier::CONNECTION_NONE : | |
252 NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
253 } | 251 } |
254 | 252 |
255 void AddressTrackerLinux::ReadMessages(bool* address_changed, | 253 void AddressTrackerLinux::ReadMessages(bool* address_changed, |
256 bool* link_changed, | 254 bool* link_changed, |
257 bool* tunnel_changed) { | 255 bool* tunnel_changed) { |
258 *address_changed = false; | 256 *address_changed = false; |
259 *link_changed = false; | 257 *link_changed = false; |
260 *tunnel_changed = false; | 258 *tunnel_changed = false; |
261 char buffer[4096]; | 259 char buffer[4096]; |
262 bool first_loop = true; | 260 bool first_loop = true; |
263 for (;;) { | 261 for (;;) { |
264 int rv = HANDLE_EINTR(recv(netlink_fd_, | 262 int rv = HANDLE_EINTR(recv(netlink_fd_, |
265 buffer, | 263 buffer, |
266 sizeof(buffer), | 264 sizeof(buffer), |
267 // Block the first time through loop. | 265 // Block the first time through loop. |
268 first_loop ? 0 : MSG_DONTWAIT)); | 266 first_loop ? 0 : MSG_DONTWAIT)); |
269 first_loop = false; | 267 first_loop = false; |
270 if (rv == 0) { | 268 if (rv == 0) { |
271 LOG(ERROR) << "Unexpected shutdown of NETLINK socket."; | 269 LOG(ERROR) << "Unexpected shutdown of NETLINK socket."; |
272 return; | 270 return; |
273 } | 271 } |
274 if (rv < 0) { | 272 if (rv < 0) { |
275 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) | 273 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) |
276 break; | 274 break; |
277 PLOG(ERROR) << "Failed to recv from netlink socket"; | 275 PLOG(ERROR) << "Failed to recv from netlink socket"; |
278 return; | 276 return; |
279 } | 277 } |
280 HandleMessage(buffer, rv, address_changed, link_changed, tunnel_changed); | 278 HandleMessage(buffer, rv, address_changed, link_changed, tunnel_changed); |
281 } | 279 } |
282 if (*link_changed) { | 280 if (*link_changed || *address_changed) |
283 bool is_offline; | 281 UpdateCurrentConnectionType(); |
284 { | |
285 AddressTrackerAutoLock lock(*this, online_links_lock_); | |
286 is_offline = online_links_.empty(); | |
287 } | |
288 AddressTrackerAutoLock lock(*this, is_offline_lock_); | |
289 is_offline_ = is_offline; | |
290 } | |
291 } | 282 } |
292 | 283 |
293 void AddressTrackerLinux::HandleMessage(char* buffer, | 284 void AddressTrackerLinux::HandleMessage(char* buffer, |
294 size_t length, | 285 size_t length, |
295 bool* address_changed, | 286 bool* address_changed, |
296 bool* link_changed, | 287 bool* link_changed, |
297 bool* tunnel_changed) { | 288 bool* tunnel_changed) { |
298 DCHECK(buffer); | 289 DCHECK(buffer); |
299 for (struct nlmsghdr* header = reinterpret_cast<struct nlmsghdr*>(buffer); | 290 for (struct nlmsghdr* header = reinterpret_cast<struct nlmsghdr*>(buffer); |
300 NLMSG_OK(header, length); | 291 NLMSG_OK(header, length); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
344 } | 335 } |
345 } break; | 336 } break; |
346 case RTM_NEWLINK: { | 337 case RTM_NEWLINK: { |
347 const struct ifinfomsg* msg = | 338 const struct ifinfomsg* msg = |
348 reinterpret_cast<struct ifinfomsg*>(NLMSG_DATA(header)); | 339 reinterpret_cast<struct ifinfomsg*>(NLMSG_DATA(header)); |
349 if (!(msg->ifi_flags & IFF_LOOPBACK) && (msg->ifi_flags & IFF_UP) && | 340 if (!(msg->ifi_flags & IFF_LOOPBACK) && (msg->ifi_flags & IFF_UP) && |
350 (msg->ifi_flags & IFF_LOWER_UP) && (msg->ifi_flags & IFF_RUNNING)) { | 341 (msg->ifi_flags & IFF_LOWER_UP) && (msg->ifi_flags & IFF_RUNNING)) { |
351 AddressTrackerAutoLock lock(*this, online_links_lock_); | 342 AddressTrackerAutoLock lock(*this, online_links_lock_); |
352 if (online_links_.insert(msg->ifi_index).second) { | 343 if (online_links_.insert(msg->ifi_index).second) { |
353 *link_changed = true; | 344 *link_changed = true; |
354 if (IsTunnelInterface(msg)) | 345 if (IsTunnelInterface(msg->ifi_index)) |
355 *tunnel_changed = true; | 346 *tunnel_changed = true; |
356 } | 347 } |
357 } else { | 348 } else { |
358 AddressTrackerAutoLock lock(*this, online_links_lock_); | 349 AddressTrackerAutoLock lock(*this, online_links_lock_); |
359 if (online_links_.erase(msg->ifi_index)) { | 350 if (online_links_.erase(msg->ifi_index)) { |
360 *link_changed = true; | 351 *link_changed = true; |
361 if (IsTunnelInterface(msg)) | 352 if (IsTunnelInterface(msg->ifi_index)) |
362 *tunnel_changed = true; | 353 *tunnel_changed = true; |
363 } | 354 } |
364 } | 355 } |
365 } break; | 356 } break; |
366 case RTM_DELLINK: { | 357 case RTM_DELLINK: { |
367 const struct ifinfomsg* msg = | 358 const struct ifinfomsg* msg = |
368 reinterpret_cast<struct ifinfomsg*>(NLMSG_DATA(header)); | 359 reinterpret_cast<struct ifinfomsg*>(NLMSG_DATA(header)); |
369 AddressTrackerAutoLock lock(*this, online_links_lock_); | 360 AddressTrackerAutoLock lock(*this, online_links_lock_); |
370 if (online_links_.erase(msg->ifi_index)) { | 361 if (online_links_.erase(msg->ifi_index)) { |
371 *link_changed = true; | 362 *link_changed = true; |
372 if (IsTunnelInterface(msg)) | 363 if (IsTunnelInterface(msg->ifi_index)) |
373 *tunnel_changed = true; | 364 *tunnel_changed = true; |
374 } | 365 } |
375 } break; | 366 } break; |
376 default: | 367 default: |
377 break; | 368 break; |
378 } | 369 } |
379 } | 370 } |
380 } | 371 } |
381 | 372 |
382 void AddressTrackerLinux::OnFileCanReadWithoutBlocking(int fd) { | 373 void AddressTrackerLinux::OnFileCanReadWithoutBlocking(int fd) { |
(...skipping 11 matching lines...) Expand all Loading... | |
394 } | 385 } |
395 | 386 |
396 void AddressTrackerLinux::OnFileCanWriteWithoutBlocking(int /* fd */) {} | 387 void AddressTrackerLinux::OnFileCanWriteWithoutBlocking(int /* fd */) {} |
397 | 388 |
398 void AddressTrackerLinux::CloseSocket() { | 389 void AddressTrackerLinux::CloseSocket() { |
399 if (netlink_fd_ >= 0 && IGNORE_EINTR(close(netlink_fd_)) < 0) | 390 if (netlink_fd_ >= 0 && IGNORE_EINTR(close(netlink_fd_)) < 0) |
400 PLOG(ERROR) << "Could not close NETLINK socket."; | 391 PLOG(ERROR) << "Could not close NETLINK socket."; |
401 netlink_fd_ = -1; | 392 netlink_fd_ = -1; |
402 } | 393 } |
403 | 394 |
404 bool AddressTrackerLinux::IsTunnelInterface(const struct ifinfomsg* msg) const { | 395 bool AddressTrackerLinux::IsTunnelInterface(int interface_index) const { |
405 // Linux kernel drivers/net/tun.c uses "tun" name prefix. | 396 // Linux kernel drivers/net/tun.c uses "tun" name prefix. |
406 return strncmp(get_interface_name_(msg->ifi_index), "tun", 3) == 0; | 397 char buf[IFNAMSIZ] = {0}; |
398 return strncmp(get_interface_name_(interface_index, buf), "tun", 3) == 0; | |
399 } | |
400 | |
401 void AddressTrackerLinux::UpdateCurrentConnectionType() { | |
402 NetworkChangeNotifier::ConnectionType type = | |
403 NetworkChangeNotifier::CONNECTION_NONE; | |
404 AddressTrackerLinux::AddressMap address_map = GetAddressMap(); | |
405 base::hash_set<int> online_links = GetOnlineLinks(); | |
406 | |
407 // Strip out tunnel interfaces from online_links | |
408 for (base::hash_set<int>::const_iterator it = online_links.begin(); | |
409 it != online_links.end(); ++it) { | |
410 if (IsTunnelInterface(*it)) | |
411 online_links.erase(it); | |
pauljensen
2015/01/28 21:15:55
this won't work: |it| becomes invalid after erase(
derekjchow1
2015/01/29 00:35:04
https://code.google.com/p/chromium/codesearch#chro
| |
412 } | |
413 | |
414 NetworkInterfaceList networks; | |
415 if (GetNetworkListImpl(&networks, 0, online_links, address_map, | |
416 get_interface_name_)) { | |
417 type = NetworkChangeNotifier::ConnectionTypeFromInterfaceList(networks); | |
418 } else { | |
419 type = online_links.empty() ? NetworkChangeNotifier::CONNECTION_NONE | |
420 : NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
421 } | |
422 | |
423 AddressTrackerAutoLock lock(*this, connection_type_lock_); | |
424 current_connection_type_ = type; | |
407 } | 425 } |
408 | 426 |
409 AddressTrackerLinux::AddressTrackerAutoLock::AddressTrackerAutoLock( | 427 AddressTrackerLinux::AddressTrackerAutoLock::AddressTrackerAutoLock( |
410 const AddressTrackerLinux& tracker, | 428 const AddressTrackerLinux& tracker, |
411 base::Lock& lock) | 429 base::Lock& lock) |
412 : tracker_(tracker), lock_(lock) { | 430 : tracker_(tracker), lock_(lock) { |
413 if (tracker_.tracking_) { | 431 if (tracker_.tracking_) { |
414 lock_.Acquire(); | 432 lock_.Acquire(); |
415 } else { | 433 } else { |
416 DCHECK(tracker_.thread_checker_.CalledOnValidThread()); | 434 DCHECK(tracker_.thread_checker_.CalledOnValidThread()); |
417 } | 435 } |
418 } | 436 } |
419 | 437 |
420 AddressTrackerLinux::AddressTrackerAutoLock::~AddressTrackerAutoLock() { | 438 AddressTrackerLinux::AddressTrackerAutoLock::~AddressTrackerAutoLock() { |
421 if (tracker_.tracking_) { | 439 if (tracker_.tracking_) { |
422 lock_.AssertAcquired(); | 440 lock_.AssertAcquired(); |
423 lock_.Release(); | 441 lock_.Release(); |
424 } | 442 } |
425 } | 443 } |
426 | 444 |
427 } // namespace internal | 445 } // namespace internal |
428 } // namespace net | 446 } // namespace net |
OLD | NEW |