blob: 083b01120fdf8d1dd4c9d6c38765e383c4b29485 [file] [log] [blame]
Darin Petkovc64fe5e2012-01-11 12:46:13 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75897df2011-04-27 09:05:53 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Darin Petkov2b8e44e2012-06-25 15:13:26 +02005#ifndef SHILL_DEVICE_H_
6#define SHILL_DEVICE_H_
Paul Stewart75897df2011-04-27 09:05:53 -07007
Ben Chancd477322014-10-17 14:19:30 -07008#include <memory>
Chris Masone46eaaf52011-05-24 13:08:30 -07009#include <string>
Chris Masone9be4a9d2011-05-16 15:44:09 -070010#include <vector>
11
Ben Chancc67c522014-09-03 07:19:18 -070012#include <base/macros.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070013#include <base/memory/ref_counted.h>
Eric Shienbrood9a245532012-03-07 14:20:39 -050014#include <base/memory/weak_ptr.h>
Darin Petkovafa6fc42011-06-21 16:21:08 -070015#include <gtest/gtest_prod.h> // for FRIEND_TEST
Paul Stewartd5843772011-05-11 15:40:42 -070016
Eric Shienbrood9a245532012-03-07 14:20:39 -050017#include "shill/adaptor_interfaces.h"
18#include "shill/callbacks.h"
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -070019#include "shill/connection_tester.h"
Rebecca Silberstein3d49ea42014-08-21 11:20:50 -070020#include "shill/connectivity_trial.h"
Peter Qiud670d032014-06-03 15:04:43 -070021#include "shill/dns_server_tester.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070022#include "shill/event_dispatcher.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070023#include "shill/ipconfig.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070024#include "shill/net/ip_address.h"
25#include "shill/net/shill_time.h"
Paul Stewart20088d82012-02-16 06:58:55 -080026#include "shill/portal_detector.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070027#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070028#include "shill/refptr_types.h"
Paul Stewart03dba0b2011-08-22 16:32:45 -070029#include "shill/service.h"
Paul Stewartfdd16072011-09-16 12:41:35 -070030#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070031
32namespace shill {
33
Chris Masone9be4a9d2011-05-16 15:44:09 -070034class ControlInterface;
Darin Petkov77cb6812011-08-15 16:19:41 -070035class DHCPProvider;
Chris Masone9be4a9d2011-05-16 15:44:09 -070036class DeviceAdaptorInterface;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070037class Endpoint;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070038class Error;
39class EventDispatcher;
Gaurav Shah6d2c72d2012-10-16 16:30:44 -070040class GeolocationInfo;
Paul Stewart036dba02012-08-07 12:34:41 -070041class LinkMonitor;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070042class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080043class Metrics;
Thieu Lefb46caf2012-03-08 11:57:15 -080044class RTNLHandler;
Ben Chanb061f892013-02-27 17:46:55 -080045class TrafficMonitor;
Chris Masone9be4a9d2011-05-16 15:44:09 -070046
Paul Stewart75897df2011-04-27 09:05:53 -070047// Device superclass. Individual network interfaces types will inherit from
48// this class.
Chris Masone27c4aa52011-07-02 13:10:14 -070049class Device : public base::RefCounted<Device> {
Paul Stewart75897df2011-04-27 09:05:53 -070050 public:
Wade Guthrie68d41092013-04-02 12:56:02 -070051 // Progressively scanning for access points (APs) is done with multiple scans,
52 // each containing a group of channels. The scans are performed in order of
53 // decreasing likelihood of connecting on one of the channels in a group
54 // (the number of channels in a group is a matter for system tuning). Fully
55 // scanning for APs does a complete scan of all the channels in a single scan.
56 // Progressive scanning is supported for wifi devices; technologies that
57 // support scan but don't support progressive scan will always perform a full
58 // scan, regardless of the requested scan type.
59 enum ScanType { kProgressiveScan, kFullScan };
60
Chris Masone9be4a9d2011-05-16 15:44:09 -070061 // A constructor for the Device object
62 Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070063 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080064 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070065 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070066 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070067 const std::string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080068 int interface_index,
69 Technology::Identifier technology);
Chris Masone9be4a9d2011-05-16 15:44:09 -070070
mukesh agrawal28185512013-10-18 16:57:09 -070071 // Enable or disable the device. This is a convenience method for
72 // cases where we want to SetEnabledNonPersistent, but don't care
73 // about the results.
Eric Shienbrood9a245532012-03-07 14:20:39 -050074 virtual void SetEnabled(bool enable);
mukesh agrawalb1136662013-10-14 16:39:38 -070075 // Enable or disable the device. Unlike SetEnabledPersistent, it does not
Ben Chan9f3dcf82013-09-25 18:04:58 -070076 // save the setting in the profile.
77 //
mukesh agrawal28185512013-10-18 16:57:09 -070078 // TODO(quiche): Replace both of the next two methods with calls to
79 // SetEnabledChecked.
Ben Chan9f3dcf82013-09-25 18:04:58 -070080 virtual void SetEnabledNonPersistent(bool enable,
81 Error *error,
82 const ResultCallback &callback);
Eric Shienbrood9a245532012-03-07 14:20:39 -050083 // Enable or disable the device, and save the setting in the profile.
84 // The setting is persisted before the enable or disable operation
85 // starts, so that even if it fails, the user's intent is still recorded
86 // for the next time shill restarts.
Jason Glasgowdf7c5532012-05-14 14:41:45 -040087 virtual void SetEnabledPersistent(bool enable,
88 Error *error,
89 const ResultCallback &callback);
mukesh agrawal28185512013-10-18 16:57:09 -070090 // Enable or disable the Device, depending on |enable|.
91 // Save the new setting to the profile, if |persist| is true.
92 // Report synchronous errors using |error|, and asynchronous completion
93 // with |callback|.
94 virtual void SetEnabledChecked(bool enable,
95 bool persist,
96 Error *error,
97 const ResultCallback &callback);
98 // Similar to SetEnabledChecked, but without sanity checking, and
99 // without saving the new value of |enable| to the profile. If you
100 // are sane (i.e. not Cellular), you should use
101 // SetEnabledChecked instead.
102 virtual void SetEnabledUnchecked(bool enable,
103 Error *error,
104 const ResultCallback &callback);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800105
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400106 // Returns true if the underlying device reports that it is already enabled.
107 // Used when the device is registered with the Manager, so that shill can
108 // sync its state/ with the true state of the device. The default is to
109 // report false.
110 virtual bool IsUnderlyingDeviceEnabled() const;
111
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700112 virtual void LinkEvent(unsigned flags, unsigned change);
Paul Stewart75897df2011-04-27 09:05:53 -0700113
Darin Petkov9ae310f2011-08-30 15:41:13 -0700114 // The default implementation sets |error| to kNotSupported.
Wade Guthrie4823f4f2013-07-25 10:03:03 -0700115 virtual void Scan(ScanType scan_type, Error *error,
116 const std::string &reason);
Peter Qiud51b2442015-02-23 10:41:38 -0800117 // The default implementation sets |error| to kNotSupported.
118 virtual void SetSchedScan(bool enable, Error *error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500119 virtual void RegisterOnNetwork(const std::string &network_id, Error *error,
120 const ResultCallback &callback);
121 virtual void RequirePIN(const std::string &pin, bool require,
122 Error *error, const ResultCallback &callback);
123 virtual void EnterPIN(const std::string &pin,
124 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700125 virtual void UnblockPIN(const std::string &unblock_code,
126 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500127 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700128 virtual void ChangePIN(const std::string &old_pin,
129 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500130 Error *error, const ResultCallback &callback);
Ben Chanad663e12013-01-08 01:58:47 -0800131 virtual void Reset(Error *error, const ResultCallback &callback);
132
Darin Petkovc37a9c42012-09-06 15:28:22 +0200133 virtual void SetCarrier(const std::string &carrier,
134 Error *error, const ResultCallback &callback);
Ben Chanbcc6e012013-11-04 14:28:37 -0800135
136 // Returns true if IPv6 is allowed and should be enabled when the device
137 // tries to acquire an IP configuration. The default implementation allows
138 // IPv6, which can be overridden by a derived class.
139 virtual bool IsIPv6Allowed() const;
140
Paul Stewart2bf1d352011-12-06 15:02:55 -0800141 virtual void DisableIPv6();
142 virtual void EnableIPv6();
143 virtual void EnableIPv6Privacy();
Darin Petkov9ae310f2011-08-30 15:41:13 -0700144
Gaurav Shah435de2c2011-11-17 19:01:07 -0800145 // Returns true if the selected service on the device (if any) is connected.
146 // Returns false if there is no selected service, or if the selected service
147 // is not connected.
148 bool IsConnected() const;
149
Christopher Wiley5519e9e2013-01-08 16:55:56 -0800150 // Called by Device so that subclasses can run hooks on the selected service
151 // getting an IP. Subclasses should call up to the parent first.
152 virtual void OnConnected();
153
Paul Stewart8596f9f2013-03-14 07:58:26 -0700154 // Called by the Connection so that the Device can update the service sorting
155 // after one connection is bound to another.
156 virtual void OnConnectionUpdated();
157
Paul Stewartd215af62012-04-24 23:25:50 -0700158 // Returns true if the selected service on the device (if any) is connected
159 // and matches the passed-in argument |service|. Returns false if there is
160 // no connected service, or if it does not match |service|.
161 virtual bool IsConnectedToService(const ServiceRefPtr &service) const;
162
Paul Stewartfa11e282013-12-02 22:04:25 -0800163 // Returns true if the DHCP parameters provided indicate that we are tethered
164 // to a mobile device.
165 virtual bool IsConnectedViaTether() const;
166
Paul Stewartd215af62012-04-24 23:25:50 -0700167 // Restart the portal detection process on a connected device. This is
168 // useful if the properties on the connected service have changed in a
169 // way that may affect the decision to run portal detection at all.
170 // Returns true if portal detection was started.
171 virtual bool RestartPortalDetection();
172
Rebecca Silberstein6862b382014-09-11 08:24:51 -0700173 // Called by the manager to start a single connectivity test. This is used to
174 // log connection state triggered by a user feedback log request.
175 virtual bool StartConnectivityTest();
176
Ben Chanb061f892013-02-27 17:46:55 -0800177 // Get receive and transmit byte counters.
Ben Chan7fab8972014-08-10 17:14:46 -0700178 virtual uint64_t GetReceiveByteCount();
179 virtual uint64_t GetTransmitByteCount();
Ben Chanb061f892013-02-27 17:46:55 -0800180
Paul Stewartc6fbad92013-11-13 14:50:52 -0800181 // Perform a TDLS |operation| on the underlying device, with respect
182 // to a given |peer|. The string returned is empty for any operation
183 // other than kTDLSOperationStatus, which returns the state of the
184 // TDLS link with |peer|. This method is only valid for WiFi devices,
185 // but needs to be declared here since it is part of the Device RPC
186 // API.
187 virtual std::string PerformTDLSOperation(const std::string &operation,
188 const std::string &peer,
189 Error *error);
190
Paul Stewart6ff27f52012-07-11 06:51:41 -0700191 // Reset the persisted byte counters associated with the device.
192 void ResetByteCounters();
193
Paul Stewartc681fa02012-03-02 19:40:04 -0800194 // Requests that portal detection be done, if this device has the default
195 // connection. Returns true if portal detection was started.
196 virtual bool RequestPortalDetection();
197
mukesh agrawalf6b32092013-04-10 15:49:55 -0700198 std::string GetRpcIdentifier() const;
mukesh agrawal515873d2014-07-21 17:01:35 -0700199 std::string GetStorageIdentifier() const;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700200
Gaurav Shah6d2c72d2012-10-16 16:30:44 -0700201 // Returns a list of Geolocation objects. Each object is multiple
202 // key-value pairs representing one entity that can be used for
203 // Geolocation.
204 virtual std::vector<GeolocationInfo> GetGeolocationObjects() const;
205
Paul Stewart2cb3fa72014-11-13 01:43:12 -0800206 // Enable or disable this interface to receive packets even if it is not
207 // the default connection. This is useful in limited situations such as
208 // during portal detection.
209 virtual void SetLooseRouting(bool is_loose_routing);
210
211 // Enable or disable same-net multi-home support for this interface. When
212 // enabled, ARP filtering is enabled in order to avoid the "ARP Flux"
213 // effect where peers may end up with inaccurate IP address mappings due to
214 // the default Linux ARP transmit / reply behavior. See
215 // http://linux-ip.net/html/ether-arp.html for more details on this effect.
216 virtual void SetIsMultiHomed(bool is_multi_homed);
217
Chris Masone9d779932011-08-25 16:33:41 -0700218 const std::string &address() const { return hardware_address_; }
Darin Petkove0a312e2011-07-20 13:45:28 -0700219 const std::string &link_name() const { return link_name_; }
220 int interface_index() const { return interface_index_; }
Paul Stewart2cb3fa72014-11-13 01:43:12 -0800221 virtual const ConnectionRefPtr &connection() const { return connection_; }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500222 bool enabled() const { return enabled_; }
223 bool enabled_persistent() const { return enabled_persistent_; }
Gaurav Shah435de2c2011-11-17 19:01:07 -0800224 virtual Technology::Identifier technology() const { return technology_; }
Jason Glasgowb5790052012-01-27 01:03:52 -0500225 std::string GetTechnologyString(Error *error);
Darin Petkove0a312e2011-07-20 13:45:28 -0700226
Thieu Le03026662013-04-04 10:45:11 -0700227 virtual const IPConfigRefPtr &ipconfig() const { return ipconfig_; }
Samuel Tan787a1ce2014-11-11 17:17:27 -0800228 virtual const IPConfigRefPtr &ip6config() const { return ip6config_; }
Darin Petkov31a2eca2012-03-14 11:07:18 +0100229 void set_ipconfig(const IPConfigRefPtr &config) { ipconfig_ = config; }
230
Chris Masone19e30402011-07-19 15:48:47 -0700231 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -0700232
Darin Petkovafa6fc42011-06-21 16:21:08 -0700233 // Returns a string that is guaranteed to uniquely identify this Device
234 // instance.
235 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -0700236
mukesh agrawalde29fa82011-09-16 16:16:36 -0700237 PropertyStore *mutable_store() { return &store_; }
238 const PropertyStore &store() const { return store_; }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700239 RTNLHandler *rtnl_handler() { return rtnl_handler_; }
Ben Chan19f83972012-10-03 23:25:56 -0700240 bool running() const { return running_; }
Chris Masone19e30402011-07-19 15:48:47 -0700241
Darin Petkovb05315f2011-11-07 10:14:25 +0100242 EventDispatcher *dispatcher() const { return dispatcher_; }
243
Paul Stewarta41e38d2011-11-11 07:47:29 -0800244 // Load configuration for the device from |storage|. This may include
245 // instantiating non-visible services for which configuration has been
246 // stored.
247 virtual bool Load(StoreInterface *storage);
248
249 // Save configuration for the device to |storage|.
Chris Masone877ff982011-09-21 16:18:24 -0700250 virtual bool Save(StoreInterface *storage);
Chris Masone5dec5f42011-07-22 14:07:55 -0700251
Darin Petkov77cb6812011-08-15 16:19:41 -0700252 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
253
Darin Petkov3cfbf212011-11-21 16:02:09 +0100254 DeviceAdaptorInterface *adaptor() const { return adaptor_.get(); }
255
mukesh agrawal784566d2012-08-08 18:32:58 -0700256 // Suspend event handler. Called by Manager before the system
Samuel Tanfbe8d2b2014-09-15 20:23:59 -0700257 // suspends. This handler, along with any other suspend handlers,
mukesh agrawalb1136662013-10-14 16:39:38 -0700258 // will have Manager::kTerminationActionsTimeoutMilliseconds to
Samuel Tanfbe8d2b2014-09-15 20:23:59 -0700259 // execute before the system enters the suspend state. |callback|
260 // must be invoked after all synchronous and/or asynchronous actions
Samuel Tan787a1ce2014-11-11 17:17:27 -0800261 // this function performs complete. Code that needs to run on exit should use
mukesh agrawalb1136662013-10-14 16:39:38 -0700262 // Manager::AddTerminationAction, rather than OnBeforeSuspend.
Samuel Tan787a1ce2014-11-11 17:17:27 -0800263 //
264 // The default implementation invokes the |callback| immediately, since
265 // there is nothing to be done in the general case.
Samuel Tanfbe8d2b2014-09-15 20:23:59 -0700266 virtual void OnBeforeSuspend(const ResultCallback &callback);
mukesh agrawal784566d2012-08-08 18:32:58 -0700267
268 // Resume event handler. Called by Manager as the system resumes.
269 // The base class implementation takes care of renewing a DHCP lease
270 // (if necessary). Derived classes may implement any technology
271 // specific requirements by overriding, but should include a call to
272 // the base class implementation.
273 virtual void OnAfterResume();
274
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700275 // This method is invoked when the system resumes from suspend temporarily in
276 // the "dark resume" state. The system will reenter suspend in
Samuel Tan68b73d22014-10-28 17:00:56 -0700277 // Manager::kTerminationActionsTimeoutMilliseconds. |callback| must be invoked
278 // after all synchronous and/or asynchronous actions this function performs
279 // and/or posts complete.
Samuel Tan787a1ce2014-11-11 17:17:27 -0800280 //
281 // The default implementation invokes the |callback| immediately, since
282 // there is nothing to be done in the general case.
Samuel Tan68b73d22014-10-28 17:00:56 -0700283 virtual void OnDarkResume(const ResultCallback &callback);
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700284
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800285 // Destroy the lease, if any, with this |name|.
286 // Called by the service during Unload() as part of the cleanup sequence.
287 virtual void DestroyIPConfigLease(const std::string &name);
288
Paul Stewartd55f6ae2014-04-24 16:34:21 -0700289 // Called by DeviceInfo when the kernel adds or removes a globally-scoped
290 // IPv6 address from this interface.
Paul Stewartd4f26482014-04-25 19:12:03 -0700291 virtual void OnIPv6AddressChanged();
Paul Stewartd55f6ae2014-04-24 16:34:21 -0700292
Peter Qiu98551702014-07-28 13:28:53 -0700293 // Called by DeviceInfo when the kernel receives a update for IPv6 DNS server
294 // addresses from this interface.
295 virtual void OnIPv6DnsServerAddressesChanged();
296
Peter Qiu8e1ad162014-10-02 15:58:24 -0700297 // Called when link becomes unreliable (multiple link monitor failures
298 // detected in short period of time).
299 virtual void OnUnreliableLink();
300
Peter Qiua388fdb2015-04-03 10:31:22 -0700301 // Called when link becomes reliable (no link failures in a predefined period
302 // of time).
303 virtual void OnReliableLink();
304
Samuel Tan3a1bf992014-09-23 15:05:55 -0700305 // Program a rule into the NIC to wake the system from suspend upon receiving
306 // packets from |ip_endpoint|. |error| indicates the result of the
307 // operation.
Samuel Tan96bdaec2014-11-05 18:27:38 -0800308 virtual void AddWakeOnPacketConnection(const std::string &ip_endpoint,
Samuel Tanfe734672014-08-07 15:50:48 -0700309 Error *error);
Samuel Tan3a1bf992014-09-23 15:05:55 -0700310 // Removes a rule previously programmed into the NIC to wake the system from
311 // suspend upon receiving packets from |ip_endpoint|. |error| indicates the
312 // result of the operation.
Samuel Tan96bdaec2014-11-05 18:27:38 -0800313 virtual void RemoveWakeOnPacketConnection(const std::string &ip_endpoint,
Samuel Tanfe734672014-08-07 15:50:48 -0700314 Error *error);
Samuel Tan3a1bf992014-09-23 15:05:55 -0700315 // Removes all wake-on-packet rules programmed into the NIC. |error| indicates
316 // the result of the operation.
Samuel Tanfe734672014-08-07 15:50:48 -0700317 virtual void RemoveAllWakeOnPacketConnections(Error *error);
318
Samuel Tan787a1ce2014-11-11 17:17:27 -0800319 // Initiate renewal of existing DHCP lease.
320 void RenewDHCPLease();
321
Paul Stewartd5843772011-05-11 15:40:42 -0700322 protected:
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200323 friend class base::RefCounted<Device>;
Arman Ugurayf84a4242013-04-09 20:01:07 -0700324 friend class DeviceHealthCheckerTest;
Ben Chan19f83972012-10-03 23:25:56 -0700325 FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
Arman Ugurayd42d8ec2013-04-08 19:28:21 -0700326 FRIEND_TEST(CellularTest, EnableTrafficMonitor);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400327 FRIEND_TEST(CellularTest, ModemStateChangeDisable);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800328 FRIEND_TEST(CellularTest, UseNoArpGateway);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700329 FRIEND_TEST(DeviceHealthCheckerTest, HealthCheckerPersistsAcrossDeviceReset);
Arman Ugurayf84a4242013-04-09 20:01:07 -0700330 FRIEND_TEST(DeviceHealthCheckerTest, RequestConnectionHealthCheck);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700331 FRIEND_TEST(DeviceHealthCheckerTest, SetupHealthChecker);
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -0700332 FRIEND_TEST(DevicePortalDetectionTest, RequestStartConnectivityTest);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800333 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Paul Stewartd4f26482014-04-25 19:12:03 -0700334 FRIEND_TEST(DeviceTest, AvailableIPConfigs);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700335 FRIEND_TEST(DeviceTest, DestroyIPConfig);
336 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Garret Kelly782cdce2015-04-01 16:39:16 -0400337 FRIEND_TEST(DeviceTest, ConfigWithMinimumMTU);
Ben Chanbcc6e012013-11-04 14:28:37 -0800338 FRIEND_TEST(DeviceTest, EnableIPv6);
Chris Masone95207da2011-06-29 16:50:49 -0700339 FRIEND_TEST(DeviceTest, GetProperties);
Peter Qiub25083f2014-08-25 13:22:31 -0700340 FRIEND_TEST(DeviceTest, IPConfigUpdatedFailureWithIPv6Config);
Paul Stewartfa11e282013-12-02 22:04:25 -0800341 FRIEND_TEST(DeviceTest, IsConnectedViaTether);
Peter Qiua0572032014-09-26 10:07:37 -0700342 FRIEND_TEST(DeviceTest, LinkMonitorFailure);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700343 FRIEND_TEST(DeviceTest, Load);
Paul Stewartd4f26482014-04-25 19:12:03 -0700344 FRIEND_TEST(DeviceTest, OnIPv6AddressChanged);
Peter Qiub25083f2014-08-25 13:22:31 -0700345 FRIEND_TEST(DeviceTest, OnIPv6ConfigurationCompleted);
Peter Qiu25f1be62014-08-12 10:42:27 -0700346 FRIEND_TEST(DeviceTest, OnIPv6DnsServerAddressesChanged);
Samuel Tan815a6fb2014-10-23 16:53:59 -0700347 FRIEND_TEST(DeviceTest,
348 OnIPv6DnsServerAddressesChanged_LeaseExpirationUpdated);
Garret Kellyc5f89d12015-02-18 14:39:36 -0500349 FRIEND_TEST(DeviceTest, PrependIPv4DNSServers);
350 FRIEND_TEST(DeviceTest, PrependIPv6DNSServers);
Chris Masone5dec5f42011-07-22 14:07:55 -0700351 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700352 FRIEND_TEST(DeviceTest, SelectedService);
Ben Chan9f3dcf82013-09-25 18:04:58 -0700353 FRIEND_TEST(DeviceTest, SetEnabledNonPersistent);
Darin Petkove7c6ad32012-06-29 10:22:09 +0200354 FRIEND_TEST(DeviceTest, SetEnabledPersistent);
Paul Stewartc681fa02012-03-02 19:40:04 -0800355 FRIEND_TEST(DeviceTest, SetServiceConnectedState);
Paul Stewart75a68b92013-10-24 10:50:27 -0700356 FRIEND_TEST(DeviceTest, ShouldUseArpGateway);
Paul Stewart8c116a92012-05-02 18:30:03 -0700357 FRIEND_TEST(DeviceTest, Start);
Ben Chanb061f892013-02-27 17:46:55 -0800358 FRIEND_TEST(DeviceTest, StartTrafficMonitor);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700359 FRIEND_TEST(DeviceTest, Stop);
Ben Chanb061f892013-02-27 17:46:55 -0800360 FRIEND_TEST(DeviceTest, StopTrafficMonitor);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800361 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
362 FRIEND_TEST(ManagerTest, DefaultTechnology);
mukesh agrawala9f5dfe2014-12-16 12:56:36 -0800363 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
364 FRIEND_TEST(ManagerTest, GetEnabledDeviceWithTechnology);
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700365 FRIEND_TEST(ManagerTest, SetEnabledStateForTechnology);
Peter Qiua900f4a2015-03-19 09:36:40 -0700366 FRIEND_TEST(ManagerTest, GetEnabledDeviceByLinkName);
mukesh agrawalf407d592013-07-31 11:37:57 -0700367 FRIEND_TEST(PPPDeviceTest, UpdateIPConfigFromPPP);
mukesh agrawal32399322011-09-01 10:53:43 -0700368 FRIEND_TEST(WiFiMainTest, Connect);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800369 FRIEND_TEST(WiFiMainTest, UseArpGateway);
Darin Petkov3a4100c2012-06-14 11:36:59 +0200370 FRIEND_TEST(WiMaxTest, ConnectTimeout);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800371 FRIEND_TEST(WiMaxTest, UseNoArpGateway);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700372
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200373 virtual ~Device();
374
Eric Shienbrood9a245532012-03-07 14:20:39 -0500375 // Each device must implement this method to do the work needed to
376 // enable the device to operate for establishing network connections.
Ben Chancc225ef2014-09-30 13:26:51 -0700377 // The |error| argument, if not nullptr,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500378 // will refer to an Error that starts out with the value
379 // Error::kOperationInitiated. This reflects the assumption that
380 // enable (and disable) operations will usually be non-blocking,
381 // and their completion will be indicated by means of an asynchronous
382 // reply sometime later. There are two circumstances in which a
383 // device's Start() method may overwrite |error|:
384 //
385 // 1. If an early failure is detected, such that the non-blocking
386 // part of the operation never takes place, then |error| should
387 // be set to the appropriate value corresponding to the type
388 // of failure. This is the "immediate failure" case.
389 // 2. If the device is enabled without performing any non-blocking
390 // steps, then |error| should be Reset, i.e., its value set
391 // to Error::kSuccess. This is the "immediate success" case.
392 //
393 // In these two cases, because completion is immediate, |callback|
394 // is not used. If neither of these two conditions holds, then |error|
395 // should not be modified, and |callback| should be passed to the
396 // method that will initiate the non-blocking operation.
397 virtual void Start(Error *error,
398 const EnabledStateChangedCallback &callback) = 0;
399
400 // Each device must implement this method to do the work needed to
401 // disable the device, i.e., clear any running state, and make the
402 // device no longer capable of establishing network connections.
403 // The discussion for Start() regarding the use of |error| and
404 // |callback| apply to Stop() as well.
405 virtual void Stop(Error *error,
406 const EnabledStateChangedCallback &callback) = 0;
407
408 // The EnabledStateChangedCallback that gets passed to the device's
409 // Start() and Stop() methods is bound to this method. |callback|
410 // is the callback that was passed to SetEnabled().
411 void OnEnabledStateChanged(const ResultCallback &callback,
412 const Error &error);
413
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200414 // Drops the currently selected service along with its IP configuration and
415 // connection, if any.
mukesh agrawal5d851b12013-07-11 14:09:41 -0700416 virtual void DropConnection();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200417
Darin Petkovafa6fc42011-06-21 16:21:08 -0700418 // If there's an IP configuration in |ipconfig_|, releases the IP address and
419 // destroys the configuration instance.
420 void DestroyIPConfig();
421
422 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
Paul Stewartd408fdf2012-05-07 17:15:57 -0700423 // requests a new IP configuration. Saves the DHCP lease to the generic
424 // lease filename based on the interface name. Registers a callback to
Darin Petkovafa6fc42011-06-21 16:21:08 -0700425 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
426 // request was successfully sent.
Paul Stewart2bf1d352011-12-06 15:02:55 -0800427 bool AcquireIPConfig();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700428
Paul Stewartd408fdf2012-05-07 17:15:57 -0700429 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
430 // requests a new IP configuration. Saves the DHCP lease to a filename
431 // based on the passed-in |lease_name|. Registers a callback to
432 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
433 // request was successfully sent.
434 bool AcquireIPConfigWithLeaseName(const std::string &lease_name);
435
Ben Chan539ab022014-02-03 16:34:57 -0800436 // Assigns the IP configuration |properties| to |ipconfig_|.
437 void AssignIPConfig(const IPConfig::Properties &properties);
438
Paul Stewartc5099532013-12-12 07:53:15 -0800439 // Callback invoked on successful IP configuration updates.
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800440 virtual void OnIPConfigUpdated(const IPConfigRefPtr &ipconfig,
441 bool new_lease_acquired);
Samuel Tan787a1ce2014-11-11 17:17:27 -0800442
443 // Called when IPv6 configuration changes.
444 virtual void OnIPv6ConfigUpdated();
Paul Stewartc5099532013-12-12 07:53:15 -0800445
446 // Callback invoked on IP configuration failures.
447 void OnIPConfigFailed(const IPConfigRefPtr &ipconfig);
Darin Petkov79d74c92012-03-07 17:20:32 +0100448
Paul Stewart82236532013-12-10 15:33:11 -0800449 // Callback invoked when "Refresh" is invoked on an IPConfig. This usually
450 // signals a change in static IP parameters.
451 void OnIPConfigRefreshed(const IPConfigRefPtr &ipconfig);
452
Paul Stewart1f916e42013-12-23 09:52:54 -0800453 // Callback invoked when an IPConfig restarts due to lease expiry. This
454 // is advisory, since an "Updated" or "Failed" signal is guaranteed to
455 // follow.
456 void OnIPConfigExpired(const IPConfigRefPtr &ipconfig);
457
Paul Stewartf6f96482013-07-12 12:49:15 -0700458 // Called by Device so that subclasses can run hooks on the selected service
459 // failing to get an IP. The default implementation disconnects the selected
460 // service with Service::kFailureDHCP.
461 virtual void OnIPConfigFailure();
462
Paul Stewarte6132022011-08-16 09:11:02 -0700463 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
464 void CreateConnection();
465
466 // Remove connection state
467 void DestroyConnection();
468
Paul Stewart03dba0b2011-08-22 16:32:45 -0700469 // Selects a service to be "current" -- i.e. link-state or configuration
470 // events that happen to the device are attributed to this service.
471 void SelectService(const ServiceRefPtr &service);
472
mukesh agrawalfc362912013-08-06 18:10:07 -0700473 // Set the state of the |selected_service_|.
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700474 virtual void SetServiceState(Service::ConnectState state);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700475
476 // Set the failure of the selected service (implicitly sets the state to
mukesh agrawalfc362912013-08-06 18:10:07 -0700477 // "failure").
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700478 virtual void SetServiceFailure(Service::ConnectFailure failure_state);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700479
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400480 // Records the failure mode and time of the selected service, and
481 // sets the Service state of the selected service to "Idle".
482 // Avoids showing a failure mole in the UI.
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700483 virtual void SetServiceFailureSilent(Service::ConnectFailure failure_state);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400484
Paul Stewart20088d82012-02-16 06:58:55 -0800485 // Called by the Portal Detector whenever a trial completes. Device
486 // subclasses that choose unique mappings from portal results to connected
487 // states can override this method in order to do so.
488 virtual void PortalDetectorCallback(const PortalDetector::Result &result);
489
490 // Initiate portal detection, if enabled for this device type.
491 bool StartPortalDetection();
492
493 // Stop portal detection if it is running.
494 void StopPortalDetection();
495
Paul Stewarte8303eb2014-10-08 22:51:14 -0700496 // Stop connectivity tester if it exists.
497 void StopConnectivityTest();
498
Paul Stewart036dba02012-08-07 12:34:41 -0700499 // Initiate link monitoring, if enabled for this device type.
500 bool StartLinkMonitor();
501
502 // Stop link monitoring if it is running.
503 void StopLinkMonitor();
504
505 // Respond to a LinkMonitor failure in a Device-specific manner.
506 virtual void OnLinkMonitorFailure();
507
Peter Qiu9d581932014-04-14 16:37:37 -0700508 // Respond to a LinkMonitor gateway's MAC address found/change event.
509 virtual void OnLinkMonitorGatewayChange();
Peter Qiub5d124f2014-04-14 12:05:02 -0700510
Peter Qiudc335f82014-05-15 10:33:17 -0700511 // Returns true if traffic monitor is enabled on this device. The default
512 // implementation will return false, which can be overridden by a derived
513 // class.
514 virtual bool IsTrafficMonitorEnabled() const;
515
516 // Initiates traffic monitoring on the device if traffic monitor is enabled.
517 void StartTrafficMonitor();
518
519 // Stops traffic monitoring on the device if traffic monitor is enabled.
520 void StopTrafficMonitor();
521
Peter Qiu6f5618b2014-06-05 15:19:01 -0700522 // Start DNS test for the given servers. When retry_until_success is set,
523 // callback will only be invoke when the test succeed or the test failed to
524 // start (internal error). This function will return false if there is a test
525 // that's already running, and true otherwise.
526 virtual bool StartDNSTest(
527 const std::vector<std::string> &dns_servers,
528 const bool retry_until_success,
529 const base::Callback<void(const DNSServerTester::Status)> &callback);
530 // Stop DNS test if one is running.
531 virtual void StopDNSTest();
532
Peter Qiu25f1be62014-08-12 10:42:27 -0700533 // Timer function for monitoring IPv6 DNS server's lifetime.
534 void StartIPv6DNSServerTimer(uint32 lifetime_seconds);
535 void StopIPv6DNSServerTimer();
536
537 // Stop all monitoring/testing activities on this device. Called when tearing
538 // down or changing network connection on the device.
539 void StopAllActivities();
540
Peter Qiudc335f82014-05-15 10:33:17 -0700541 // Called by the Traffic Monitor when it detects a network problem. Device
542 // subclasses that want to roam to a different network when encountering
543 // network problems can override this method in order to do so. The parent
544 // implementation handles the metric reporting of the network problem.
545 virtual void OnEncounterNetworkProblem(int reason);
546
Paul Stewart20088d82012-02-16 06:58:55 -0800547 // Set the state of the selected service, with checks to make sure
548 // the service is already in a connected state before doing so.
549 void SetServiceConnectedState(Service::ConnectState state);
550
Arman Ugurayed8e6102012-11-29 14:47:20 -0800551 // Specifies whether an ARP gateway should be used for the
552 // device technology.
553 virtual bool ShouldUseArpGateway() const;
554
Paul Stewart316acef2014-05-29 18:40:48 -0700555 // Indicates if the selected service is configured with a static IP address.
556 bool IsUsingStaticIP() const;
557
Garret Kellyd01b5cc2015-03-12 16:20:55 -0400558 // Indicates if the selected service is configured with static nameservers.
559 bool IsUsingStaticNameServers() const;
560
Darin Petkov9893d9c2012-05-17 15:27:31 -0700561 const ServiceRefPtr &selected_service() const { return selected_service_; }
562
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700563 void HelpRegisterConstDerivedString(
Jason Glasgowb5790052012-01-27 01:03:52 -0500564 const std::string &name,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700565 std::string(Device::*get)(Error *));
Chris Masoneb925cc82011-06-22 15:39:57 -0700566
Christopher Wiley674598d2014-12-12 10:21:39 -0800567 void HelpRegisterConstDerivedRpcIdentifier(
568 const std::string &name,
569 RpcIdentifier(Device::*get)(Error *));
570
Jason Glasgow08afdff2012-04-03 10:22:26 -0400571 void HelpRegisterConstDerivedRpcIdentifiers(
572 const std::string &name,
573 RpcIdentifiers(Device::*get)(Error *));
574
Paul Stewart6ff27f52012-07-11 06:51:41 -0700575 void HelpRegisterConstDerivedUint64(
576 const std::string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700577 uint64_t(Device::*get)(Error *));
Paul Stewart6ff27f52012-07-11 06:51:41 -0700578
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -0700579 // Called by the ConnectionTester whenever a connectivity test completes.
580 virtual void ConnectionTesterCallback();
581
Paul Stewartac4ac002011-08-26 12:04:26 -0700582 // Property getters reserved for subclasses
583 ControlInterface *control_interface() const { return control_interface_; }
Thieu Le3426c8f2012-01-11 17:35:11 -0800584 Metrics *metrics() const { return metrics_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700585 Manager *manager() const { return manager_; }
Paul Stewart036dba02012-08-07 12:34:41 -0700586 const LinkMonitor *link_monitor() const { return link_monitor_.get(); }
587 void set_link_monitor(LinkMonitor *link_monitor);
Peter Qiudc335f82014-05-15 10:33:17 -0700588 // Use for unit test.
589 void set_traffic_monitor(TrafficMonitor *traffic_monitor);
Paul Stewartd5843772011-05-11 15:40:42 -0700590
Samuel Tan787a1ce2014-11-11 17:17:27 -0800591 // Calculates the time (in seconds) till a DHCP lease is due for renewal,
592 // and stores this value in |result|. Returns false is there is no upcoming
593 // DHCP lease renewal, true otherwise.
594 bool TimeToNextDHCPLeaseRenewal(uint32_t *result);
595
Paul Stewart75897df2011-04-27 09:05:53 -0700596 private:
Darin Petkov3189a472012-10-05 09:55:33 +0200597 friend class CellularCapabilityTest;
598 friend class CellularTest;
Chris Masone413a3192011-05-09 17:10:05 -0700599 friend class DeviceAdaptorInterface;
Paul Stewart6ff27f52012-07-11 06:51:41 -0700600 friend class DeviceByteCountTest;
Paul Stewartc681fa02012-03-02 19:40:04 -0800601 friend class DevicePortalDetectionTest;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700602 friend class DeviceTest;
Paul Stewartced3ad72013-04-03 13:39:25 -0700603 friend class EthernetTest;
Darin Petkov3189a472012-10-05 09:55:33 +0200604 friend class OpenVPNDriverTest;
Ben Chanbcc6e012013-11-04 14:28:37 -0800605 friend class TestDevice;
mukesh agrawal4a165582013-08-02 15:58:21 -0700606 friend class VirtualDeviceTest;
Paul Stewarte369ece2012-05-22 09:11:03 -0700607 friend class WiFiObjectTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700608
Paul Stewart2bf1d352011-12-06 15:02:55 -0800609 static const char kIPFlagTemplate[];
610 static const char kIPFlagVersion4[];
611 static const char kIPFlagVersion6[];
612 static const char kIPFlagDisableIPv6[];
613 static const char kIPFlagUseTempAddr[];
614 static const char kIPFlagUseTempAddrUsedAndDefault[];
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800615 static const char kIPFlagReversePathFilter[];
616 static const char kIPFlagReversePathFilterEnabled[];
617 static const char kIPFlagReversePathFilterLooseMode[];
Paul Stewart2cb3fa72014-11-13 01:43:12 -0800618 static const char kIPFlagArpAnnounce[];
619 static const char kIPFlagArpAnnounceDefault[];
620 static const char kIPFlagArpAnnounceBestLocal[];
621 static const char kIPFlagArpIgnore[];
622 static const char kIPFlagArpIgnoreDefault[];
623 static const char kIPFlagArpIgnoreLocalOnly[];
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700624 static const char kStoragePowered[];
Paul Stewart6ff27f52012-07-11 06:51:41 -0700625 static const char kStorageReceiveByteCount[];
626 static const char kStorageTransmitByteCount[];
Peter Qiub9256f32014-05-09 15:27:29 -0700627 static const char kFallbackDnsTestHostname[];
628 static const char* kFallbackDnsServers[];
629 static const int kDNSTimeoutMilliseconds;
Chris Masone5dec5f42011-07-22 14:07:55 -0700630
Peter Qiua0572032014-09-26 10:07:37 -0700631 // Maximum seconds between two link monitor failures to declare this link
632 // (network) as unreliable.
Peter Qiua388fdb2015-04-03 10:31:22 -0700633 static const int kLinkUnreliableThresholdSeconds;
Peter Qiua0572032014-09-26 10:07:37 -0700634
Paul Stewart1062d9d2012-04-27 10:42:27 -0700635 // Configure static IP address parameters if the service provides them.
636 void ConfigureStaticIPTask();
637
Chris Masone5dec5f42011-07-22 14:07:55 -0700638 // Right now, Devices reference IPConfigs directly when persisted to disk
639 // It's not clear that this makes sense long-term, but that's how it is now.
640 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700641 // |suffix| is injected into the storage identifier used for the configs.
642 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700643
Ben Chanbcc6e012013-11-04 14:28:37 -0800644 // Set an IP configuration flag on the device. |family| should be "ipv6" or
645 // "ipv4". |flag| should be the name of the flag to be set and |value| is
646 // what this flag should be set to. Overridden by unit tests to pretend
647 // writing to procfs.
648 virtual bool SetIPFlag(IPAddress::Family family,
649 const std::string &flag,
650 const std::string &value);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800651
Paul Stewart2cb3fa72014-11-13 01:43:12 -0800652 // Request the removal of reverse-path filtering for this interface.
653 // This will allow packets destined for this interface to be accepted,
654 // even if this is not the default route for such a packet to arrive.
655 void DisableReversePathFilter();
656
657 // Request reverse-path filtering for this interface.
658 void EnableReversePathFilter();
659
660 // Disable ARP filtering on the device. The interface will exhibit the
661 // default Linux behavior -- incoming ARP requests are responded to by all
662 // interfaces. Outgoing ARP requests can contain any local address.
663 void DisableArpFiltering();
664
665 // Enable ARP filtering on the device. Incoming ARP requests are responded
666 // to only by the interface(s) owning the address. Outgoing ARP requests
667 // will contain the best local address for the target.
668 void EnableArpFiltering();
669
Christopher Wiley674598d2014-12-12 10:21:39 -0800670 std::string GetSelectedServiceRpcIdentifier(Error *error);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800671 std::vector<std::string> AvailableIPConfigs(Error *error);
Chris Masone4e851612011-07-01 10:46:53 -0700672 std::string GetRpcConnectionIdentifier();
673
Paul Stewart036dba02012-08-07 12:34:41 -0700674 // Get the LinkMonitor's average response time.
Ben Chan7fab8972014-08-10 17:14:46 -0700675 uint64_t GetLinkMonitorResponseTime(Error *error);
Paul Stewart036dba02012-08-07 12:34:41 -0700676
Ben Chanb061f892013-02-27 17:46:55 -0800677 // Get receive and transmit byte counters. These methods simply wrap
678 // GetReceiveByteCount and GetTransmitByteCount in order to be used by
679 // HelpRegisterConstDerivedUint64.
Ben Chan7fab8972014-08-10 17:14:46 -0700680 uint64_t GetReceiveByteCountProperty(Error *error);
681 uint64_t GetTransmitByteCountProperty(Error *error);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700682
Paul Stewartd4f26482014-04-25 19:12:03 -0700683 // Emit a property change signal for the "IPConfigs" property of this device.
684 void UpdateIPConfigsProperty();
685
Peter Qiu6f5618b2014-06-05 15:19:01 -0700686 // Called by DNS server tester when the fallback DNS servers test completes.
Peter Qiud670d032014-06-03 15:04:43 -0700687 void FallbackDNSResultCallback(const DNSServerTester::Status status);
Peter Qiub9256f32014-05-09 15:27:29 -0700688
Peter Qiu6f5618b2014-06-05 15:19:01 -0700689 // Called by DNS server tester when the configured DNS servers test completes.
690 void ConfigDNSResultCallback(const DNSServerTester::Status status);
691
Peter Qiua89154b2014-05-23 15:45:42 -0700692 // Update DNS setting with the given DNS servers for the current connection.
693 void SwitchDNSServers(const std::vector<std::string> &dns_servers);
694
Peter Qiu25f1be62014-08-12 10:42:27 -0700695 // Called when the lifetime for IPv6 DNS server expires.
696 void IPv6DNSServerExpired();
697
Peter Qiub25083f2014-08-25 13:22:31 -0700698 // Return true if given IP configuration contain both IP address and DNS
699 // servers. Hence, ready to be used for network connection.
700 bool IPConfigCompleted(const IPConfigRefPtr &ipconfig);
701
702 // Setup network connection with given IP configuration, and start portal
703 // detection on that connection.
704 void SetupConnection(const IPConfigRefPtr &ipconfig);
705
Garret Kellyc5f89d12015-02-18 14:39:36 -0500706 // Prepend the Manager's configured list of DNS servers into |ipconfig|
707 // ensuring that only DNS servers of the same address family as |ipconfig| are
708 // included in the final list.
709 void PrependDNSServersIntoIPConfig(const IPConfigRefPtr &ipconfig);
710
711 // Mutate |servers| to include the Manager's prepended list of DNS servers for
712 // |family|. On return, it is guaranteed that there are no duplicate entries
713 // in |servers|.
714 void PrependDNSServers(const IPAddress::Family family,
715 std::vector<std::string> *servers);
716
Eric Shienbrood9a245532012-03-07 14:20:39 -0500717 // |enabled_persistent_| is the value of the Powered property, as
718 // read from the profile. If it is not found in the profile, it
719 // defaults to true. |enabled_| reflects the real-time state of
720 // the device, i.e., enabled or disabled. |enabled_pending_| reflects
721 // the target state of the device while an enable or disable operation
722 // is occurring.
723 //
724 // Some typical sequences for these state variables are shown below.
725 //
726 // Shill starts up, profile has been read:
727 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=false
728 //
729 // Shill acts on the value of |enabled_persistent_|, calls SetEnabled(true):
730 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=true
731 //
732 // SetEnabled completes successfully, device is enabled:
733 // |enabled_persistent_|=true |enabled_|=true |enabled_pending_|=true
734 //
735 // User presses "Disable" button, SetEnabled(false) is called:
736 // |enabled_persistent_|=false |enabled_|=true |enabled_pending_|=false
737 //
738 // SetEnabled completes successfully, device is disabled:
739 // |enabled_persistent_|=false |enabled_|=false |enabled_pending_|=false
740 bool enabled_;
741 bool enabled_persistent_;
742 bool enabled_pending_;
743
744 // Other properties
Paul Stewartac4ac002011-08-26 12:04:26 -0700745 bool reconnect_;
746 const std::string hardware_address_;
747
748 PropertyStore store_;
749
Paul Stewartac4ac002011-08-26 12:04:26 -0700750 const int interface_index_;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800751 bool running_; // indicates whether the device is actually in operation
Paul Stewartac4ac002011-08-26 12:04:26 -0700752 const std::string link_name_;
753 const std::string unique_id_;
754 ControlInterface *control_interface_;
755 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800756 Metrics *metrics_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700757 Manager *manager_;
758 IPConfigRefPtr ipconfig_;
Paul Stewartd4f26482014-04-25 19:12:03 -0700759 IPConfigRefPtr ip6config_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700760 ConnectionRefPtr connection_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500761 base::WeakPtrFactory<Device> weak_ptr_factory_;
Ben Chancd477322014-10-17 14:19:30 -0700762 std::unique_ptr<DeviceAdaptorInterface> adaptor_;
763 std::unique_ptr<PortalDetector> portal_detector_;
764 std::unique_ptr<LinkMonitor> link_monitor_;
Peter Qiu6f5618b2014-06-05 15:19:01 -0700765 // Used for verifying whether DNS server is functional.
Ben Chancd477322014-10-17 14:19:30 -0700766 std::unique_ptr<DNSServerTester> dns_server_tester_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500767 base::Callback<void(const PortalDetector::Result &)>
768 portal_detector_callback_;
Peter Qiu25f1be62014-08-12 10:42:27 -0700769 // Callback to invoke when IPv6 DNS servers lifetime expired.
770 base::CancelableClosure ipv6_dns_server_expired_callback_;
Ben Chancd477322014-10-17 14:19:30 -0700771 std::unique_ptr<TrafficMonitor> traffic_monitor_;
Peter Qiu6f5618b2014-06-05 15:19:01 -0700772 // DNS servers obtained from ipconfig (either from DHCP or static config)
773 // that are not working.
774 std::vector<std::string> config_dns_servers_;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800775 Technology::Identifier technology_;
Thieu Le85e050b2012-03-13 15:04:38 -0700776 // The number of portal detection attempts from Connected to Online state.
777 // This includes all failure/timeout attempts and the final successful
778 // attempt.
779 int portal_attempts_to_online_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700780
Paul Stewart6ff27f52012-07-11 06:51:41 -0700781 // Keep track of the offset between the interface-reported byte counters
782 // and our persisted value.
Ben Chan7fab8972014-08-10 17:14:46 -0700783 uint64_t receive_byte_offset_;
784 uint64_t transmit_byte_offset_;
Paul Stewart6ff27f52012-07-11 06:51:41 -0700785
Paul Stewart03dba0b2011-08-22 16:32:45 -0700786 // Maintain a reference to the connected / connecting service
787 ServiceRefPtr selected_service_;
788
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700789 // Cache singleton pointers for performance and test purposes.
Darin Petkov77cb6812011-08-15 16:19:41 -0700790 DHCPProvider *dhcp_provider_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700791 RTNLHandler *rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700792
Peter Qiua0572032014-09-26 10:07:37 -0700793 // Time when link monitor last failed.
794 Time *time_;
795 time_t last_link_monitor_failed_time_;
Peter Qiua388fdb2015-04-03 10:31:22 -0700796 // Callback to invoke when link becomes reliable again after it was previously
797 // unreliable.
798 base::CancelableClosure reliable_link_callback_;
Peter Qiua0572032014-09-26 10:07:37 -0700799
Ben Chancd477322014-10-17 14:19:30 -0700800 std::unique_ptr<ConnectionTester> connection_tester_;
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -0700801 base::Callback<void()> connection_tester_callback_;
802
Paul Stewart2cb3fa72014-11-13 01:43:12 -0800803 // Track whether packets from non-optimal routes will be accepted by this
804 // device. This is referred to as "loose mode" (see RFC3704).
805 bool is_loose_routing_;
806
807 // Track the current same-net multi-home state.
808 bool is_multi_homed_;
809
Chris Masone9be4a9d2011-05-16 15:44:09 -0700810 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700811};
812
Paul Stewart75897df2011-04-27 09:05:53 -0700813} // namespace shill
814
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200815#endif // SHILL_DEVICE_H_