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.
5 #include "net/base/address_tracker_linux.h"
11 #include "base/logging.h"
12 #include "base/posix/eintr_wrapper.h"
13 #include "base/threading/thread_restrictions.h"
20 // Retrieves address from NETLINK address message.
21 // Sets |really_deprecated| for IPv6 addresses with preferred lifetimes of 0.
22 bool GetAddress(const struct nlmsghdr
* header
,
24 bool* really_deprecated
) {
25 if (really_deprecated
)
26 *really_deprecated
= false;
27 const struct ifaddrmsg
* msg
=
28 reinterpret_cast<struct ifaddrmsg
*>(NLMSG_DATA(header
));
29 size_t address_length
= 0;
30 switch (msg
->ifa_family
) {
32 address_length
= kIPv4AddressSize
;
35 address_length
= kIPv6AddressSize
;
41 // Use IFA_ADDRESS unless IFA_LOCAL is present. This behavior here is based on
42 // getaddrinfo in glibc (check_pf.c). Judging from kernel implementation of
43 // NETLINK, IPv4 addresses have only the IFA_ADDRESS attribute, while IPv6
44 // have the IFA_LOCAL attribute.
45 unsigned char* address
= NULL
;
46 unsigned char* local
= NULL
;
47 size_t length
= IFA_PAYLOAD(header
);
48 for (const struct rtattr
* attr
=
49 reinterpret_cast<const struct rtattr
*>(IFA_RTA(msg
));
51 attr
= RTA_NEXT(attr
, length
)) {
52 switch (attr
->rta_type
) {
54 DCHECK_GE(RTA_PAYLOAD(attr
), address_length
);
55 address
= reinterpret_cast<unsigned char*>(RTA_DATA(attr
));
58 DCHECK_GE(RTA_PAYLOAD(attr
), address_length
);
59 local
= reinterpret_cast<unsigned char*>(RTA_DATA(attr
));
62 const struct ifa_cacheinfo
*cache_info
=
63 reinterpret_cast<const struct ifa_cacheinfo
*>(RTA_DATA(attr
));
64 if (really_deprecated
)
65 *really_deprecated
= (cache_info
->ifa_prefered
== 0);
75 out
->assign(address
, address
+ address_length
);
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);
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
);
100 AddressTrackerLinux::AddressTrackerLinux(const base::Closure
& address_callback
,
101 const base::Closure
& link_callback
,
102 const base::Closure
& tunnel_callback
)
103 : get_interface_name_(GetInterfaceName
),
104 address_callback_(address_callback
),
105 link_callback_(link_callback
),
106 tunnel_callback_(tunnel_callback
),
109 is_offline_initialized_(false),
110 is_offline_initialized_cv_(&is_offline_lock_
) {
111 DCHECK(!address_callback
.is_null());
112 DCHECK(!link_callback
.is_null());
115 AddressTrackerLinux::~AddressTrackerLinux() {
119 void AddressTrackerLinux::Init() {
120 netlink_fd_
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
121 if (netlink_fd_
< 0) {
122 PLOG(ERROR
) << "Could not create NETLINK socket";
123 AbortAndForceOnline();
127 // Request notifications.
128 struct sockaddr_nl addr
= {};
129 addr
.nl_family
= AF_NETLINK
;
130 addr
.nl_pid
= getpid();
131 // TODO(szym): Track RTMGRP_LINK as well for ifi_type, http://crbug.com/113993
132 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
| RTMGRP_IPV6_IFADDR
| RTMGRP_NOTIFY
|
134 int rv
= bind(netlink_fd_
,
135 reinterpret_cast<struct sockaddr
*>(&addr
),
138 PLOG(ERROR
) << "Could not bind NETLINK socket";
139 AbortAndForceOnline();
143 // Request dump of addresses.
144 struct sockaddr_nl peer
= {};
145 peer
.nl_family
= AF_NETLINK
;
148 struct nlmsghdr header
;
152 request
.header
.nlmsg_len
= NLMSG_LENGTH(sizeof(request
.msg
));
153 request
.header
.nlmsg_type
= RTM_GETADDR
;
154 request
.header
.nlmsg_flags
= NLM_F_REQUEST
| NLM_F_DUMP
;
155 request
.header
.nlmsg_pid
= getpid();
156 request
.msg
.rtgen_family
= AF_UNSPEC
;
158 rv
= HANDLE_EINTR(sendto(netlink_fd_
, &request
, request
.header
.nlmsg_len
,
159 0, reinterpret_cast<struct sockaddr
*>(&peer
),
162 PLOG(ERROR
) << "Could not send NETLINK request";
163 AbortAndForceOnline();
167 // Consume pending message to populate the AddressMap, but don't notify.
168 // Sending another request without first reading responses results in EBUSY.
169 bool address_changed
;
172 ReadMessages(&address_changed
, &link_changed
, &tunnel_changed
);
174 // Request dump of link state
175 request
.header
.nlmsg_type
= RTM_GETLINK
;
177 rv
= HANDLE_EINTR(sendto(netlink_fd_
, &request
, request
.header
.nlmsg_len
, 0,
178 reinterpret_cast<struct sockaddr
*>(&peer
),
181 PLOG(ERROR
) << "Could not send NETLINK request";
182 AbortAndForceOnline();
186 // Consume pending message to populate links_online_, but don't notify.
187 ReadMessages(&address_changed
, &link_changed
, &tunnel_changed
);
189 base::AutoLock
lock(is_offline_lock_
);
190 is_offline_initialized_
= true;
191 is_offline_initialized_cv_
.Signal();
194 rv
= base::MessageLoopForIO::current()->WatchFileDescriptor(
195 netlink_fd_
, true, base::MessageLoopForIO::WATCH_READ
, &watcher_
, this);
197 PLOG(ERROR
) << "Could not watch NETLINK socket";
198 AbortAndForceOnline();
203 void AddressTrackerLinux::AbortAndForceOnline() {
205 base::AutoLock
lock(is_offline_lock_
);
207 is_offline_initialized_
= true;
208 is_offline_initialized_cv_
.Signal();
211 AddressTrackerLinux::AddressMap
AddressTrackerLinux::GetAddressMap() const {
212 base::AutoLock
lock(address_map_lock_
);
216 NetworkChangeNotifier::ConnectionType
217 AddressTrackerLinux::GetCurrentConnectionType() {
218 // http://crbug.com/125097
219 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
220 base::AutoLock
lock(is_offline_lock_
);
221 // Make sure the initial offline state is set before returning.
222 while (!is_offline_initialized_
) {
223 is_offline_initialized_cv_
.Wait();
225 // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN.
226 // http://crbug.com/160537
227 return is_offline_
? NetworkChangeNotifier::CONNECTION_NONE
:
228 NetworkChangeNotifier::CONNECTION_UNKNOWN
;
231 void AddressTrackerLinux::ReadMessages(bool* address_changed
,
233 bool* tunnel_changed
) {
234 *address_changed
= false;
235 *link_changed
= false;
236 *tunnel_changed
= false;
238 bool first_loop
= true;
240 int rv
= HANDLE_EINTR(recv(netlink_fd_
,
243 // Block the first time through loop.
244 first_loop
? 0 : MSG_DONTWAIT
));
247 LOG(ERROR
) << "Unexpected shutdown of NETLINK socket.";
251 if ((errno
== EAGAIN
) || (errno
== EWOULDBLOCK
))
253 PLOG(ERROR
) << "Failed to recv from netlink socket";
256 HandleMessage(buffer
, rv
, address_changed
, link_changed
, tunnel_changed
);
259 base::AutoLock
lock(is_offline_lock_
);
260 is_offline_
= online_links_
.empty();
264 void AddressTrackerLinux::HandleMessage(char* buffer
,
266 bool* address_changed
,
268 bool* tunnel_changed
) {
270 for (struct nlmsghdr
* header
= reinterpret_cast<struct nlmsghdr
*>(buffer
);
271 NLMSG_OK(header
, length
);
272 header
= NLMSG_NEXT(header
, length
)) {
273 switch (header
->nlmsg_type
) {
277 const struct nlmsgerr
* msg
=
278 reinterpret_cast<struct nlmsgerr
*>(NLMSG_DATA(header
));
279 LOG(ERROR
) << "Unexpected netlink error " << msg
->error
<< ".";
282 IPAddressNumber address
;
283 bool really_deprecated
;
284 if (GetAddress(header
, &address
, &really_deprecated
)) {
285 base::AutoLock
lock(address_map_lock_
);
286 struct ifaddrmsg
* msg
=
287 reinterpret_cast<struct ifaddrmsg
*>(NLMSG_DATA(header
));
288 // Routers may frequently (every few seconds) output the IPv6 ULA
289 // prefix which can cause the linux kernel to frequently output two
290 // back-to-back messages, one without the deprecated flag and one with
291 // the deprecated flag but both with preferred lifetimes of 0. Avoid
292 // interpretting this as an actual change by canonicalizing the two
293 // messages by setting the deprecated flag based on the preferred
294 // lifetime also. http://crbug.com/268042
295 if (really_deprecated
)
296 msg
->ifa_flags
|= IFA_F_DEPRECATED
;
297 // Only indicate change if the address is new or ifaddrmsg info has
299 AddressMap::iterator it
= address_map_
.find(address
);
300 if (it
== address_map_
.end()) {
301 address_map_
.insert(it
, std::make_pair(address
, *msg
));
302 *address_changed
= true;
303 } else if (memcmp(&it
->second
, msg
, sizeof(*msg
))) {
305 *address_changed
= true;
310 IPAddressNumber address
;
311 if (GetAddress(header
, &address
, NULL
)) {
312 base::AutoLock
lock(address_map_lock_
);
313 if (address_map_
.erase(address
))
314 *address_changed
= true;
318 const struct ifinfomsg
* msg
=
319 reinterpret_cast<struct ifinfomsg
*>(NLMSG_DATA(header
));
320 if (!(msg
->ifi_flags
& IFF_LOOPBACK
) && (msg
->ifi_flags
& IFF_UP
) &&
321 (msg
->ifi_flags
& IFF_LOWER_UP
) && (msg
->ifi_flags
& IFF_RUNNING
)) {
322 if (online_links_
.insert(msg
->ifi_index
).second
) {
323 *link_changed
= true;
324 if (IsTunnelInterface(msg
))
325 *tunnel_changed
= true;
328 if (online_links_
.erase(msg
->ifi_index
)) {
329 *link_changed
= true;
330 if (IsTunnelInterface(msg
))
331 *tunnel_changed
= true;
336 const struct ifinfomsg
* msg
=
337 reinterpret_cast<struct ifinfomsg
*>(NLMSG_DATA(header
));
338 if (online_links_
.erase(msg
->ifi_index
)) {
339 *link_changed
= true;
340 if (IsTunnelInterface(msg
))
341 *tunnel_changed
= true;
350 void AddressTrackerLinux::OnFileCanReadWithoutBlocking(int fd
) {
351 DCHECK_EQ(netlink_fd_
, fd
);
352 bool address_changed
;
355 ReadMessages(&address_changed
, &link_changed
, &tunnel_changed
);
357 address_callback_
.Run();
359 link_callback_
.Run();
361 tunnel_callback_
.Run();
364 void AddressTrackerLinux::OnFileCanWriteWithoutBlocking(int /* fd */) {}
366 void AddressTrackerLinux::CloseSocket() {
367 if (netlink_fd_
>= 0 && IGNORE_EINTR(close(netlink_fd_
)) < 0)
368 PLOG(ERROR
) << "Could not close NETLINK socket.";
372 bool AddressTrackerLinux::IsTunnelInterface(const struct ifinfomsg
* msg
) const {
373 // Linux kernel drivers/net/tun.c uses "tun" name prefix.
374 return strncmp(get_interface_name_(msg
->ifi_index
), "tun", 3) == 0;
377 } // namespace internal