blob: e48a51e625a78fb4891daf4994f2d22453abbb02 [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>
Paul Stewart38fcf162015-06-12 09:52:17 -07009#include <set>
Chris Masone46eaaf52011-05-24 13:08:30 -070010#include <string>
Chris Masone9be4a9d2011-05-16 15:44:09 -070011#include <vector>
12
Ben Chancc67c522014-09-03 07:19:18 -070013#include <base/macros.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070014#include <base/memory/ref_counted.h>
Eric Shienbrood9a245532012-03-07 14:20:39 -050015#include <base/memory/weak_ptr.h>
Darin Petkovafa6fc42011-06-21 16:21:08 -070016#include <gtest/gtest_prod.h> // for FRIEND_TEST
Paul Stewartd5843772011-05-11 15:40:42 -070017
Eric Shienbrood9a245532012-03-07 14:20:39 -050018#include "shill/adaptor_interfaces.h"
19#include "shill/callbacks.h"
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -070020#include "shill/connection_tester.h"
Rebecca Silberstein3d49ea42014-08-21 11:20:50 -070021#include "shill/connectivity_trial.h"
Peter Qiud670d032014-06-03 15:04:43 -070022#include "shill/dns_server_tester.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070023#include "shill/event_dispatcher.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070024#include "shill/ipconfig.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070025#include "shill/net/ip_address.h"
26#include "shill/net/shill_time.h"
Paul Stewart20088d82012-02-16 06:58:55 -080027#include "shill/portal_detector.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070028#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070029#include "shill/refptr_types.h"
Paul Stewart03dba0b2011-08-22 16:32:45 -070030#include "shill/service.h"
Paul Stewartfdd16072011-09-16 12:41:35 -070031#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070032
33namespace shill {
34
Chris Masone9be4a9d2011-05-16 15:44:09 -070035class ControlInterface;
Darin Petkov77cb6812011-08-15 16:19:41 -070036class DHCPProvider;
Chris Masone9be4a9d2011-05-16 15:44:09 -070037class DeviceAdaptorInterface;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070038class Endpoint;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070039class Error;
40class EventDispatcher;
Gaurav Shah6d2c72d2012-10-16 16:30:44 -070041class GeolocationInfo;
Paul Stewart036dba02012-08-07 12:34:41 -070042class LinkMonitor;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070043class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080044class Metrics;
Thieu Lefb46caf2012-03-08 11:57:15 -080045class RTNLHandler;
Ben Chanb061f892013-02-27 17:46:55 -080046class TrafficMonitor;
Chris Masone9be4a9d2011-05-16 15:44:09 -070047
Paul Stewart75897df2011-04-27 09:05:53 -070048// Device superclass. Individual network interfaces types will inherit from
49// this class.
Chris Masone27c4aa52011-07-02 13:10:14 -070050class Device : public base::RefCounted<Device> {
Paul Stewart75897df2011-04-27 09:05:53 -070051 public:
Wade Guthrie68d41092013-04-02 12:56:02 -070052 // Progressively scanning for access points (APs) is done with multiple scans,
53 // each containing a group of channels. The scans are performed in order of
54 // decreasing likelihood of connecting on one of the channels in a group
55 // (the number of channels in a group is a matter for system tuning). Fully
56 // scanning for APs does a complete scan of all the channels in a single scan.
57 // Progressive scanning is supported for wifi devices; technologies that
58 // support scan but don't support progressive scan will always perform a full
59 // scan, regardless of the requested scan type.
60 enum ScanType { kProgressiveScan, kFullScan };
61
Chris Masone9be4a9d2011-05-16 15:44:09 -070062 // A constructor for the Device object
Paul Stewarta794cd62015-06-16 13:13:10 -070063 Device(ControlInterface* control_interface,
64 EventDispatcher* dispatcher,
65 Metrics* metrics,
66 Manager* manager,
67 const std::string& link_name,
68 const std::string& address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080069 int interface_index,
70 Technology::Identifier technology);
Chris Masone9be4a9d2011-05-16 15:44:09 -070071
mukesh agrawal28185512013-10-18 16:57:09 -070072 // Enable or disable the device. This is a convenience method for
73 // cases where we want to SetEnabledNonPersistent, but don't care
74 // about the results.
Eric Shienbrood9a245532012-03-07 14:20:39 -050075 virtual void SetEnabled(bool enable);
mukesh agrawalb1136662013-10-14 16:39:38 -070076 // Enable or disable the device. Unlike SetEnabledPersistent, it does not
Ben Chan9f3dcf82013-09-25 18:04:58 -070077 // save the setting in the profile.
78 //
mukesh agrawal28185512013-10-18 16:57:09 -070079 // TODO(quiche): Replace both of the next two methods with calls to
80 // SetEnabledChecked.
Ben Chan9f3dcf82013-09-25 18:04:58 -070081 virtual void SetEnabledNonPersistent(bool enable,
Paul Stewarta794cd62015-06-16 13:13:10 -070082 Error* error,
83 const ResultCallback& callback);
Eric Shienbrood9a245532012-03-07 14:20:39 -050084 // Enable or disable the device, and save the setting in the profile.
85 // The setting is persisted before the enable or disable operation
86 // starts, so that even if it fails, the user's intent is still recorded
87 // for the next time shill restarts.
Jason Glasgowdf7c5532012-05-14 14:41:45 -040088 virtual void SetEnabledPersistent(bool enable,
Paul Stewarta794cd62015-06-16 13:13:10 -070089 Error* error,
90 const ResultCallback& callback);
mukesh agrawal28185512013-10-18 16:57:09 -070091 // Enable or disable the Device, depending on |enable|.
92 // Save the new setting to the profile, if |persist| is true.
93 // Report synchronous errors using |error|, and asynchronous completion
94 // with |callback|.
95 virtual void SetEnabledChecked(bool enable,
96 bool persist,
Paul Stewarta794cd62015-06-16 13:13:10 -070097 Error* error,
98 const ResultCallback& callback);
mukesh agrawal28185512013-10-18 16:57:09 -070099 // Similar to SetEnabledChecked, but without sanity checking, and
100 // without saving the new value of |enable| to the profile. If you
101 // are sane (i.e. not Cellular), you should use
102 // SetEnabledChecked instead.
103 virtual void SetEnabledUnchecked(bool enable,
Paul Stewarta794cd62015-06-16 13:13:10 -0700104 Error* error,
105 const ResultCallback& callback);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800106
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400107 // Returns true if the underlying device reports that it is already enabled.
108 // Used when the device is registered with the Manager, so that shill can
109 // sync its state/ with the true state of the device. The default is to
110 // report false.
111 virtual bool IsUnderlyingDeviceEnabled() const;
112
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700113 virtual void LinkEvent(unsigned flags, unsigned change);
Paul Stewart75897df2011-04-27 09:05:53 -0700114
Darin Petkov9ae310f2011-08-30 15:41:13 -0700115 // The default implementation sets |error| to kNotSupported.
Paul Stewarta794cd62015-06-16 13:13:10 -0700116 virtual void Scan(ScanType scan_type, Error* error,
117 const std::string& reason);
Peter Qiud51b2442015-02-23 10:41:38 -0800118 // The default implementation sets |error| to kNotSupported.
Paul Stewarta794cd62015-06-16 13:13:10 -0700119 virtual void SetSchedScan(bool enable, Error* error);
120 virtual void RegisterOnNetwork(const std::string& network_id, Error* error,
121 const ResultCallback& callback);
122 virtual void RequirePIN(const std::string& pin, bool require,
123 Error* error, const ResultCallback& callback);
124 virtual void EnterPIN(const std::string& pin,
125 Error* error, const ResultCallback& callback);
126 virtual void UnblockPIN(const std::string& unblock_code,
127 const std::string& pin,
128 Error* error, const ResultCallback& callback);
129 virtual void ChangePIN(const std::string& old_pin,
130 const std::string& new_pin,
131 Error* error, const ResultCallback& callback);
132 virtual void Reset(Error* error, const ResultCallback& callback);
Ben Chanad663e12013-01-08 01:58:47 -0800133
Paul Stewarta794cd62015-06-16 13:13:10 -0700134 virtual void SetCarrier(const std::string& carrier,
135 Error* error, const ResultCallback& callback);
Ben Chanbcc6e012013-11-04 14:28:37 -0800136
137 // Returns true if IPv6 is allowed and should be enabled when the device
138 // tries to acquire an IP configuration. The default implementation allows
139 // IPv6, which can be overridden by a derived class.
140 virtual bool IsIPv6Allowed() const;
141
Paul Stewart2bf1d352011-12-06 15:02:55 -0800142 virtual void DisableIPv6();
143 virtual void EnableIPv6();
144 virtual void EnableIPv6Privacy();
Darin Petkov9ae310f2011-08-30 15:41:13 -0700145
Gaurav Shah435de2c2011-11-17 19:01:07 -0800146 // Returns true if the selected service on the device (if any) is connected.
147 // Returns false if there is no selected service, or if the selected service
148 // is not connected.
149 bool IsConnected() const;
150
Christopher Wiley5519e9e2013-01-08 16:55:56 -0800151 // Called by Device so that subclasses can run hooks on the selected service
152 // getting an IP. Subclasses should call up to the parent first.
153 virtual void OnConnected();
154
Paul Stewart8596f9f2013-03-14 07:58:26 -0700155 // Called by the Connection so that the Device can update the service sorting
156 // after one connection is bound to another.
157 virtual void OnConnectionUpdated();
158
Paul Stewartd215af62012-04-24 23:25:50 -0700159 // Returns true if the selected service on the device (if any) is connected
160 // and matches the passed-in argument |service|. Returns false if there is
161 // no connected service, or if it does not match |service|.
Paul Stewarta794cd62015-06-16 13:13:10 -0700162 virtual bool IsConnectedToService(const ServiceRefPtr& service) const;
Paul Stewartd215af62012-04-24 23:25:50 -0700163
Paul Stewartfa11e282013-12-02 22:04:25 -0800164 // Returns true if the DHCP parameters provided indicate that we are tethered
165 // to a mobile device.
166 virtual bool IsConnectedViaTether() const;
167
Paul Stewartd215af62012-04-24 23:25:50 -0700168 // Restart the portal detection process on a connected device. This is
169 // useful if the properties on the connected service have changed in a
170 // way that may affect the decision to run portal detection at all.
171 // Returns true if portal detection was started.
172 virtual bool RestartPortalDetection();
173
Rebecca Silberstein6862b382014-09-11 08:24:51 -0700174 // Called by the manager to start a single connectivity test. This is used to
175 // log connection state triggered by a user feedback log request.
176 virtual bool StartConnectivityTest();
177
Ben Chanb061f892013-02-27 17:46:55 -0800178 // Get receive and transmit byte counters.
Ben Chan7fab8972014-08-10 17:14:46 -0700179 virtual uint64_t GetReceiveByteCount();
180 virtual uint64_t GetTransmitByteCount();
Ben Chanb061f892013-02-27 17:46:55 -0800181
Paul Stewartc6fbad92013-11-13 14:50:52 -0800182 // Perform a TDLS |operation| on the underlying device, with respect
183 // to a given |peer|. The string returned is empty for any operation
184 // other than kTDLSOperationStatus, which returns the state of the
185 // TDLS link with |peer|. This method is only valid for WiFi devices,
186 // but needs to be declared here since it is part of the Device RPC
187 // API.
Paul Stewarta794cd62015-06-16 13:13:10 -0700188 virtual std::string PerformTDLSOperation(const std::string& operation,
189 const std::string& peer,
190 Error* error);
Paul Stewartc6fbad92013-11-13 14:50:52 -0800191
Paul Stewart6ff27f52012-07-11 06:51:41 -0700192 // Reset the persisted byte counters associated with the device.
193 void ResetByteCounters();
194
Paul Stewartc681fa02012-03-02 19:40:04 -0800195 // Requests that portal detection be done, if this device has the default
196 // connection. Returns true if portal detection was started.
197 virtual bool RequestPortalDetection();
198
mukesh agrawalf6b32092013-04-10 15:49:55 -0700199 std::string GetRpcIdentifier() const;
mukesh agrawal515873d2014-07-21 17:01:35 -0700200 std::string GetStorageIdentifier() const;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700201
Gaurav Shah6d2c72d2012-10-16 16:30:44 -0700202 // Returns a list of Geolocation objects. Each object is multiple
203 // key-value pairs representing one entity that can be used for
204 // Geolocation.
205 virtual std::vector<GeolocationInfo> GetGeolocationObjects() const;
206
Paul Stewart2cb3fa72014-11-13 01:43:12 -0800207 // Enable or disable this interface to receive packets even if it is not
208 // the default connection. This is useful in limited situations such as
209 // during portal detection.
210 virtual void SetLooseRouting(bool is_loose_routing);
211
212 // Enable or disable same-net multi-home support for this interface. When
213 // enabled, ARP filtering is enabled in order to avoid the "ARP Flux"
214 // effect where peers may end up with inaccurate IP address mappings due to
215 // the default Linux ARP transmit / reply behavior. See
216 // http://linux-ip.net/html/ether-arp.html for more details on this effect.
217 virtual void SetIsMultiHomed(bool is_multi_homed);
218
Paul Stewarta794cd62015-06-16 13:13:10 -0700219 const std::string& address() const { return hardware_address_; }
220 const std::string& link_name() const { return link_name_; }
Darin Petkove0a312e2011-07-20 13:45:28 -0700221 int interface_index() const { return interface_index_; }
Paul Stewarta794cd62015-06-16 13:13:10 -0700222 virtual const ConnectionRefPtr& connection() const { return connection_; }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500223 bool enabled() const { return enabled_; }
224 bool enabled_persistent() const { return enabled_persistent_; }
Gaurav Shah435de2c2011-11-17 19:01:07 -0800225 virtual Technology::Identifier technology() const { return technology_; }
Paul Stewarta794cd62015-06-16 13:13:10 -0700226 std::string GetTechnologyString(Error* error);
Darin Petkove0a312e2011-07-20 13:45:28 -0700227
Paul Stewarta794cd62015-06-16 13:13:10 -0700228 virtual const IPConfigRefPtr& ipconfig() const { return ipconfig_; }
229 virtual const IPConfigRefPtr& ip6config() const { return ip6config_; }
230 virtual const IPConfigRefPtr& dhcpv6_config() const { return dhcpv6_config_; }
231 void set_ipconfig(const IPConfigRefPtr& config) { ipconfig_ = config; }
Darin Petkov31a2eca2012-03-14 11:07:18 +0100232
Paul Stewarta794cd62015-06-16 13:13:10 -0700233 const std::string& FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -0700234
Darin Petkovafa6fc42011-06-21 16:21:08 -0700235 // Returns a string that is guaranteed to uniquely identify this Device
236 // instance.
Paul Stewarta794cd62015-06-16 13:13:10 -0700237 const std::string& UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -0700238
Paul Stewarta794cd62015-06-16 13:13:10 -0700239 PropertyStore* mutable_store() { return &store_; }
240 const PropertyStore& store() const { return store_; }
241 RTNLHandler* rtnl_handler() { return rtnl_handler_; }
Ben Chan19f83972012-10-03 23:25:56 -0700242 bool running() const { return running_; }
Chris Masone19e30402011-07-19 15:48:47 -0700243
Paul Stewarta794cd62015-06-16 13:13:10 -0700244 EventDispatcher* dispatcher() const { return dispatcher_; }
Darin Petkovb05315f2011-11-07 10:14:25 +0100245
Paul Stewarta41e38d2011-11-11 07:47:29 -0800246 // Load configuration for the device from |storage|. This may include
247 // instantiating non-visible services for which configuration has been
248 // stored.
Paul Stewarta794cd62015-06-16 13:13:10 -0700249 virtual bool Load(StoreInterface* storage);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800250
251 // Save configuration for the device to |storage|.
Paul Stewarta794cd62015-06-16 13:13:10 -0700252 virtual bool Save(StoreInterface* storage);
Chris Masone5dec5f42011-07-22 14:07:55 -0700253
Paul Stewarta794cd62015-06-16 13:13:10 -0700254 void set_dhcp_provider(DHCPProvider* provider) { dhcp_provider_ = provider; }
Darin Petkov77cb6812011-08-15 16:19:41 -0700255
Paul Stewarta794cd62015-06-16 13:13:10 -0700256 DeviceAdaptorInterface* adaptor() const { return adaptor_.get(); }
Darin Petkov3cfbf212011-11-21 16:02:09 +0100257
mukesh agrawal784566d2012-08-08 18:32:58 -0700258 // Suspend event handler. Called by Manager before the system
Samuel Tanfbe8d2b2014-09-15 20:23:59 -0700259 // suspends. This handler, along with any other suspend handlers,
mukesh agrawalb1136662013-10-14 16:39:38 -0700260 // will have Manager::kTerminationActionsTimeoutMilliseconds to
Samuel Tanfbe8d2b2014-09-15 20:23:59 -0700261 // execute before the system enters the suspend state. |callback|
262 // must be invoked after all synchronous and/or asynchronous actions
Samuel Tan787a1ce2014-11-11 17:17:27 -0800263 // this function performs complete. Code that needs to run on exit should use
mukesh agrawalb1136662013-10-14 16:39:38 -0700264 // Manager::AddTerminationAction, rather than OnBeforeSuspend.
Samuel Tan787a1ce2014-11-11 17:17:27 -0800265 //
266 // The default implementation invokes the |callback| immediately, since
267 // there is nothing to be done in the general case.
Paul Stewarta794cd62015-06-16 13:13:10 -0700268 virtual void OnBeforeSuspend(const ResultCallback& callback);
mukesh agrawal784566d2012-08-08 18:32:58 -0700269
270 // Resume event handler. Called by Manager as the system resumes.
271 // The base class implementation takes care of renewing a DHCP lease
272 // (if necessary). Derived classes may implement any technology
273 // specific requirements by overriding, but should include a call to
274 // the base class implementation.
275 virtual void OnAfterResume();
276
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700277 // This method is invoked when the system resumes from suspend temporarily in
278 // the "dark resume" state. The system will reenter suspend in
Samuel Tan68b73d22014-10-28 17:00:56 -0700279 // Manager::kTerminationActionsTimeoutMilliseconds. |callback| must be invoked
280 // after all synchronous and/or asynchronous actions this function performs
281 // and/or posts complete.
Samuel Tan787a1ce2014-11-11 17:17:27 -0800282 //
283 // The default implementation invokes the |callback| immediately, since
284 // there is nothing to be done in the general case.
Paul Stewarta794cd62015-06-16 13:13:10 -0700285 virtual void OnDarkResume(const ResultCallback& callback);
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700286
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800287 // Destroy the lease, if any, with this |name|.
288 // Called by the service during Unload() as part of the cleanup sequence.
Paul Stewarta794cd62015-06-16 13:13:10 -0700289 virtual void DestroyIPConfigLease(const std::string& name);
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800290
Paul Stewartd55f6ae2014-04-24 16:34:21 -0700291 // Called by DeviceInfo when the kernel adds or removes a globally-scoped
292 // IPv6 address from this interface.
Paul Stewartd4f26482014-04-25 19:12:03 -0700293 virtual void OnIPv6AddressChanged();
Paul Stewartd55f6ae2014-04-24 16:34:21 -0700294
Peter Qiu98551702014-07-28 13:28:53 -0700295 // Called by DeviceInfo when the kernel receives a update for IPv6 DNS server
296 // addresses from this interface.
297 virtual void OnIPv6DnsServerAddressesChanged();
298
Peter Qiu8e1ad162014-10-02 15:58:24 -0700299 // Called when link becomes unreliable (multiple link monitor failures
300 // detected in short period of time).
301 virtual void OnUnreliableLink();
302
Peter Qiua388fdb2015-04-03 10:31:22 -0700303 // Called when link becomes reliable (no link failures in a predefined period
304 // of time).
305 virtual void OnReliableLink();
306
Samuel Tan3a1bf992014-09-23 15:05:55 -0700307 // Program a rule into the NIC to wake the system from suspend upon receiving
308 // packets from |ip_endpoint|. |error| indicates the result of the
309 // operation.
Paul Stewarta794cd62015-06-16 13:13:10 -0700310 virtual void AddWakeOnPacketConnection(const std::string& ip_endpoint,
311 Error* error);
Samuel Tan3a1bf992014-09-23 15:05:55 -0700312 // Removes a rule previously programmed into the NIC to wake the system from
313 // suspend upon receiving packets from |ip_endpoint|. |error| indicates the
314 // result of the operation.
Paul Stewarta794cd62015-06-16 13:13:10 -0700315 virtual void RemoveWakeOnPacketConnection(const std::string& ip_endpoint,
316 Error* error);
Samuel Tan3a1bf992014-09-23 15:05:55 -0700317 // Removes all wake-on-packet rules programmed into the NIC. |error| indicates
318 // the result of the operation.
Paul Stewarta794cd62015-06-16 13:13:10 -0700319 virtual void RemoveAllWakeOnPacketConnections(Error* error);
Samuel Tanfe734672014-08-07 15:50:48 -0700320
Samuel Tan787a1ce2014-11-11 17:17:27 -0800321 // Initiate renewal of existing DHCP lease.
322 void RenewDHCPLease();
323
Peter Qiu62abf312015-05-05 12:58:05 -0700324 // Resolve the |input| string into a MAC address for a peer local to this
325 // device. This could be a trivial operation if the |input| is already a MAC
326 // address, or could involve an ARP table lookup. Returns true and populates
327 // |output| if the resolution completes, otherwise returns false and
328 // populates |error|.
Paul Stewarta794cd62015-06-16 13:13:10 -0700329 virtual bool ResolvePeerMacAddress(const std::string& input,
330 std::string* output,
331 Error* error);
Peter Qiu62abf312015-05-05 12:58:05 -0700332
333 // Creates a byte vector from a colon-separated hardware address string.
334 static std::vector<uint8_t> MakeHardwareAddressFromString(
Paul Stewarta794cd62015-06-16 13:13:10 -0700335 const std::string& address_string);
Peter Qiu62abf312015-05-05 12:58:05 -0700336
337 // Creates a colon-separated hardware address string from a byte vector.
338 static std::string MakeStringFromHardwareAddress(
Paul Stewarta794cd62015-06-16 13:13:10 -0700339 const std::vector<uint8_t>& address_data);
Peter Qiu62abf312015-05-05 12:58:05 -0700340
Paul Stewartd5843772011-05-11 15:40:42 -0700341 protected:
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200342 friend class base::RefCounted<Device>;
Arman Ugurayf84a4242013-04-09 20:01:07 -0700343 friend class DeviceHealthCheckerTest;
Ben Chan19f83972012-10-03 23:25:56 -0700344 FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
Arman Ugurayd42d8ec2013-04-08 19:28:21 -0700345 FRIEND_TEST(CellularTest, EnableTrafficMonitor);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400346 FRIEND_TEST(CellularTest, ModemStateChangeDisable);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800347 FRIEND_TEST(CellularTest, UseNoArpGateway);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700348 FRIEND_TEST(DeviceHealthCheckerTest, HealthCheckerPersistsAcrossDeviceReset);
Arman Ugurayf84a4242013-04-09 20:01:07 -0700349 FRIEND_TEST(DeviceHealthCheckerTest, RequestConnectionHealthCheck);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700350 FRIEND_TEST(DeviceHealthCheckerTest, SetupHealthChecker);
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -0700351 FRIEND_TEST(DevicePortalDetectionTest, RequestStartConnectivityTest);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800352 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Paul Stewartd4f26482014-04-25 19:12:03 -0700353 FRIEND_TEST(DeviceTest, AvailableIPConfigs);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700354 FRIEND_TEST(DeviceTest, DestroyIPConfig);
355 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Garret Kelly782cdce2015-04-01 16:39:16 -0400356 FRIEND_TEST(DeviceTest, ConfigWithMinimumMTU);
Ben Chanbcc6e012013-11-04 14:28:37 -0800357 FRIEND_TEST(DeviceTest, EnableIPv6);
Chris Masone95207da2011-06-29 16:50:49 -0700358 FRIEND_TEST(DeviceTest, GetProperties);
Peter Qiub25083f2014-08-25 13:22:31 -0700359 FRIEND_TEST(DeviceTest, IPConfigUpdatedFailureWithIPv6Config);
Paul Stewartfa11e282013-12-02 22:04:25 -0800360 FRIEND_TEST(DeviceTest, IsConnectedViaTether);
Peter Qiua0572032014-09-26 10:07:37 -0700361 FRIEND_TEST(DeviceTest, LinkMonitorFailure);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700362 FRIEND_TEST(DeviceTest, Load);
Peter Qiud48fa0c2015-06-10 12:20:48 -0700363 FRIEND_TEST(DeviceTest, OnDHCPv6ConfigExpired);
364 FRIEND_TEST(DeviceTest, OnDHCPv6ConfigFailed);
365 FRIEND_TEST(DeviceTest, OnDHCPv6ConfigUpdated);
Paul Stewartd4f26482014-04-25 19:12:03 -0700366 FRIEND_TEST(DeviceTest, OnIPv6AddressChanged);
Peter Qiub25083f2014-08-25 13:22:31 -0700367 FRIEND_TEST(DeviceTest, OnIPv6ConfigurationCompleted);
Peter Qiu25f1be62014-08-12 10:42:27 -0700368 FRIEND_TEST(DeviceTest, OnIPv6DnsServerAddressesChanged);
Samuel Tan815a6fb2014-10-23 16:53:59 -0700369 FRIEND_TEST(DeviceTest,
370 OnIPv6DnsServerAddressesChanged_LeaseExpirationUpdated);
Garret Kellyc5f89d12015-02-18 14:39:36 -0500371 FRIEND_TEST(DeviceTest, PrependIPv4DNSServers);
372 FRIEND_TEST(DeviceTest, PrependIPv6DNSServers);
Chris Masone5dec5f42011-07-22 14:07:55 -0700373 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700374 FRIEND_TEST(DeviceTest, SelectedService);
Ben Chan9f3dcf82013-09-25 18:04:58 -0700375 FRIEND_TEST(DeviceTest, SetEnabledNonPersistent);
Darin Petkove7c6ad32012-06-29 10:22:09 +0200376 FRIEND_TEST(DeviceTest, SetEnabledPersistent);
Paul Stewartc681fa02012-03-02 19:40:04 -0800377 FRIEND_TEST(DeviceTest, SetServiceConnectedState);
Paul Stewart75a68b92013-10-24 10:50:27 -0700378 FRIEND_TEST(DeviceTest, ShouldUseArpGateway);
Paul Stewart8c116a92012-05-02 18:30:03 -0700379 FRIEND_TEST(DeviceTest, Start);
Ben Chanb061f892013-02-27 17:46:55 -0800380 FRIEND_TEST(DeviceTest, StartTrafficMonitor);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700381 FRIEND_TEST(DeviceTest, Stop);
Ben Chanb061f892013-02-27 17:46:55 -0800382 FRIEND_TEST(DeviceTest, StopTrafficMonitor);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800383 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
384 FRIEND_TEST(ManagerTest, DefaultTechnology);
mukesh agrawala9f5dfe2014-12-16 12:56:36 -0800385 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
386 FRIEND_TEST(ManagerTest, GetEnabledDeviceWithTechnology);
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700387 FRIEND_TEST(ManagerTest, SetEnabledStateForTechnology);
Peter Qiua900f4a2015-03-19 09:36:40 -0700388 FRIEND_TEST(ManagerTest, GetEnabledDeviceByLinkName);
mukesh agrawalf407d592013-07-31 11:37:57 -0700389 FRIEND_TEST(PPPDeviceTest, UpdateIPConfigFromPPP);
mukesh agrawal32399322011-09-01 10:53:43 -0700390 FRIEND_TEST(WiFiMainTest, Connect);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800391 FRIEND_TEST(WiFiMainTest, UseArpGateway);
Darin Petkov3a4100c2012-06-14 11:36:59 +0200392 FRIEND_TEST(WiMaxTest, ConnectTimeout);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800393 FRIEND_TEST(WiMaxTest, UseNoArpGateway);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700394
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200395 virtual ~Device();
396
Eric Shienbrood9a245532012-03-07 14:20:39 -0500397 // Each device must implement this method to do the work needed to
398 // enable the device to operate for establishing network connections.
Ben Chancc225ef2014-09-30 13:26:51 -0700399 // The |error| argument, if not nullptr,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500400 // will refer to an Error that starts out with the value
401 // Error::kOperationInitiated. This reflects the assumption that
402 // enable (and disable) operations will usually be non-blocking,
403 // and their completion will be indicated by means of an asynchronous
404 // reply sometime later. There are two circumstances in which a
405 // device's Start() method may overwrite |error|:
406 //
407 // 1. If an early failure is detected, such that the non-blocking
408 // part of the operation never takes place, then |error| should
409 // be set to the appropriate value corresponding to the type
410 // of failure. This is the "immediate failure" case.
411 // 2. If the device is enabled without performing any non-blocking
412 // steps, then |error| should be Reset, i.e., its value set
413 // to Error::kSuccess. This is the "immediate success" case.
414 //
415 // In these two cases, because completion is immediate, |callback|
416 // is not used. If neither of these two conditions holds, then |error|
417 // should not be modified, and |callback| should be passed to the
418 // method that will initiate the non-blocking operation.
Paul Stewarta794cd62015-06-16 13:13:10 -0700419 virtual void Start(Error* error,
420 const EnabledStateChangedCallback& callback) = 0;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500421
422 // Each device must implement this method to do the work needed to
423 // disable the device, i.e., clear any running state, and make the
424 // device no longer capable of establishing network connections.
425 // The discussion for Start() regarding the use of |error| and
426 // |callback| apply to Stop() as well.
Paul Stewarta794cd62015-06-16 13:13:10 -0700427 virtual void Stop(Error* error,
428 const EnabledStateChangedCallback& callback) = 0;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500429
430 // The EnabledStateChangedCallback that gets passed to the device's
431 // Start() and Stop() methods is bound to this method. |callback|
432 // is the callback that was passed to SetEnabled().
Paul Stewarta794cd62015-06-16 13:13:10 -0700433 void OnEnabledStateChanged(const ResultCallback& callback,
434 const Error& error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500435
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200436 // Drops the currently selected service along with its IP configuration and
437 // connection, if any.
mukesh agrawal5d851b12013-07-11 14:09:41 -0700438 virtual void DropConnection();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200439
Darin Petkovafa6fc42011-06-21 16:21:08 -0700440 // If there's an IP configuration in |ipconfig_|, releases the IP address and
441 // destroys the configuration instance.
442 void DestroyIPConfig();
443
444 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
Paul Stewartd408fdf2012-05-07 17:15:57 -0700445 // requests a new IP configuration. Saves the DHCP lease to the generic
446 // lease filename based on the interface name. Registers a callback to
Darin Petkovafa6fc42011-06-21 16:21:08 -0700447 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
448 // request was successfully sent.
Paul Stewart2bf1d352011-12-06 15:02:55 -0800449 bool AcquireIPConfig();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700450
Paul Stewartd408fdf2012-05-07 17:15:57 -0700451 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
452 // requests a new IP configuration. Saves the DHCP lease to a filename
453 // based on the passed-in |lease_name|. Registers a callback to
454 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
455 // request was successfully sent.
Paul Stewarta794cd62015-06-16 13:13:10 -0700456 bool AcquireIPConfigWithLeaseName(const std::string& lease_name);
Paul Stewartd408fdf2012-05-07 17:15:57 -0700457
Ben Chan539ab022014-02-03 16:34:57 -0800458 // Assigns the IP configuration |properties| to |ipconfig_|.
Paul Stewarta794cd62015-06-16 13:13:10 -0700459 void AssignIPConfig(const IPConfig::Properties& properties);
Ben Chan539ab022014-02-03 16:34:57 -0800460
Paul Stewartc5099532013-12-12 07:53:15 -0800461 // Callback invoked on successful IP configuration updates.
Paul Stewarta794cd62015-06-16 13:13:10 -0700462 virtual void OnIPConfigUpdated(const IPConfigRefPtr& ipconfig,
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800463 bool new_lease_acquired);
Samuel Tan787a1ce2014-11-11 17:17:27 -0800464
465 // Called when IPv6 configuration changes.
466 virtual void OnIPv6ConfigUpdated();
Paul Stewartc5099532013-12-12 07:53:15 -0800467
468 // Callback invoked on IP configuration failures.
Paul Stewarta794cd62015-06-16 13:13:10 -0700469 void OnIPConfigFailed(const IPConfigRefPtr& ipconfig);
Darin Petkov79d74c92012-03-07 17:20:32 +0100470
Paul Stewart82236532013-12-10 15:33:11 -0800471 // Callback invoked when "Refresh" is invoked on an IPConfig. This usually
472 // signals a change in static IP parameters.
Paul Stewarta794cd62015-06-16 13:13:10 -0700473 void OnIPConfigRefreshed(const IPConfigRefPtr& ipconfig);
Paul Stewart82236532013-12-10 15:33:11 -0800474
Paul Stewart1f916e42013-12-23 09:52:54 -0800475 // Callback invoked when an IPConfig restarts due to lease expiry. This
476 // is advisory, since an "Updated" or "Failed" signal is guaranteed to
477 // follow.
Paul Stewarta794cd62015-06-16 13:13:10 -0700478 void OnIPConfigExpired(const IPConfigRefPtr& ipconfig);
Paul Stewart1f916e42013-12-23 09:52:54 -0800479
Paul Stewartf6f96482013-07-12 12:49:15 -0700480 // Called by Device so that subclasses can run hooks on the selected service
481 // failing to get an IP. The default implementation disconnects the selected
482 // service with Service::kFailureDHCP.
483 virtual void OnIPConfigFailure();
484
Peter Qiud48fa0c2015-06-10 12:20:48 -0700485 // Callback invoked on successful DHCPv6 configuration updates.
Paul Stewarta794cd62015-06-16 13:13:10 -0700486 void OnDHCPv6ConfigUpdated(const IPConfigRefPtr& ipconfig,
Peter Qiud48fa0c2015-06-10 12:20:48 -0700487 bool new_lease_acquired);
488
489 // Callback invoked on DHCPv6 configuration failures.
Paul Stewarta794cd62015-06-16 13:13:10 -0700490 void OnDHCPv6ConfigFailed(const IPConfigRefPtr& ipconfig);
Peter Qiud48fa0c2015-06-10 12:20:48 -0700491
492 // Callback invoked when an DHCPv6Config restarts due to lease expiry. This
493 // is advisory, since an "Updated" or "Failed" signal is guaranteed to
494 // follow.
Paul Stewarta794cd62015-06-16 13:13:10 -0700495 void OnDHCPv6ConfigExpired(const IPConfigRefPtr& ipconfig);
Peter Qiud48fa0c2015-06-10 12:20:48 -0700496
Paul Stewarte6132022011-08-16 09:11:02 -0700497 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
498 void CreateConnection();
499
500 // Remove connection state
501 void DestroyConnection();
502
Paul Stewart03dba0b2011-08-22 16:32:45 -0700503 // Selects a service to be "current" -- i.e. link-state or configuration
504 // events that happen to the device are attributed to this service.
Paul Stewarta794cd62015-06-16 13:13:10 -0700505 void SelectService(const ServiceRefPtr& service);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700506
mukesh agrawalfc362912013-08-06 18:10:07 -0700507 // Set the state of the |selected_service_|.
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700508 virtual void SetServiceState(Service::ConnectState state);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700509
510 // Set the failure of the selected service (implicitly sets the state to
mukesh agrawalfc362912013-08-06 18:10:07 -0700511 // "failure").
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700512 virtual void SetServiceFailure(Service::ConnectFailure failure_state);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700513
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400514 // Records the failure mode and time of the selected service, and
515 // sets the Service state of the selected service to "Idle".
516 // Avoids showing a failure mole in the UI.
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700517 virtual void SetServiceFailureSilent(Service::ConnectFailure failure_state);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400518
Paul Stewart20088d82012-02-16 06:58:55 -0800519 // Called by the Portal Detector whenever a trial completes. Device
520 // subclasses that choose unique mappings from portal results to connected
521 // states can override this method in order to do so.
Paul Stewarta794cd62015-06-16 13:13:10 -0700522 virtual void PortalDetectorCallback(const PortalDetector::Result& result);
Paul Stewart20088d82012-02-16 06:58:55 -0800523
524 // Initiate portal detection, if enabled for this device type.
525 bool StartPortalDetection();
526
527 // Stop portal detection if it is running.
528 void StopPortalDetection();
529
Paul Stewarte8303eb2014-10-08 22:51:14 -0700530 // Stop connectivity tester if it exists.
531 void StopConnectivityTest();
532
Paul Stewart036dba02012-08-07 12:34:41 -0700533 // Initiate link monitoring, if enabled for this device type.
534 bool StartLinkMonitor();
535
536 // Stop link monitoring if it is running.
537 void StopLinkMonitor();
538
539 // Respond to a LinkMonitor failure in a Device-specific manner.
540 virtual void OnLinkMonitorFailure();
541
Peter Qiu9d581932014-04-14 16:37:37 -0700542 // Respond to a LinkMonitor gateway's MAC address found/change event.
543 virtual void OnLinkMonitorGatewayChange();
Peter Qiub5d124f2014-04-14 12:05:02 -0700544
Peter Qiudc335f82014-05-15 10:33:17 -0700545 // Returns true if traffic monitor is enabled on this device. The default
546 // implementation will return false, which can be overridden by a derived
547 // class.
548 virtual bool IsTrafficMonitorEnabled() const;
549
550 // Initiates traffic monitoring on the device if traffic monitor is enabled.
551 void StartTrafficMonitor();
552
553 // Stops traffic monitoring on the device if traffic monitor is enabled.
554 void StopTrafficMonitor();
555
Peter Qiu6f5618b2014-06-05 15:19:01 -0700556 // Start DNS test for the given servers. When retry_until_success is set,
557 // callback will only be invoke when the test succeed or the test failed to
558 // start (internal error). This function will return false if there is a test
559 // that's already running, and true otherwise.
560 virtual bool StartDNSTest(
Paul Stewarta794cd62015-06-16 13:13:10 -0700561 const std::vector<std::string>& dns_servers,
Peter Qiu6f5618b2014-06-05 15:19:01 -0700562 const bool retry_until_success,
Paul Stewarta794cd62015-06-16 13:13:10 -0700563 const base::Callback<void(const DNSServerTester::Status)>& callback);
Peter Qiu6f5618b2014-06-05 15:19:01 -0700564 // Stop DNS test if one is running.
565 virtual void StopDNSTest();
566
Peter Qiu25f1be62014-08-12 10:42:27 -0700567 // Timer function for monitoring IPv6 DNS server's lifetime.
568 void StartIPv6DNSServerTimer(uint32 lifetime_seconds);
569 void StopIPv6DNSServerTimer();
570
571 // Stop all monitoring/testing activities on this device. Called when tearing
572 // down or changing network connection on the device.
573 void StopAllActivities();
574
Peter Qiudc335f82014-05-15 10:33:17 -0700575 // Called by the Traffic Monitor when it detects a network problem. Device
576 // subclasses that want to roam to a different network when encountering
577 // network problems can override this method in order to do so. The parent
578 // implementation handles the metric reporting of the network problem.
579 virtual void OnEncounterNetworkProblem(int reason);
580
Paul Stewart20088d82012-02-16 06:58:55 -0800581 // Set the state of the selected service, with checks to make sure
582 // the service is already in a connected state before doing so.
583 void SetServiceConnectedState(Service::ConnectState state);
584
Arman Ugurayed8e6102012-11-29 14:47:20 -0800585 // Specifies whether an ARP gateway should be used for the
586 // device technology.
587 virtual bool ShouldUseArpGateway() const;
588
Paul Stewart316acef2014-05-29 18:40:48 -0700589 // Indicates if the selected service is configured with a static IP address.
590 bool IsUsingStaticIP() const;
591
Garret Kellyd01b5cc2015-03-12 16:20:55 -0400592 // Indicates if the selected service is configured with static nameservers.
593 bool IsUsingStaticNameServers() const;
594
Paul Stewarta794cd62015-06-16 13:13:10 -0700595 const ServiceRefPtr& selected_service() const { return selected_service_; }
Darin Petkov9893d9c2012-05-17 15:27:31 -0700596
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700597 void HelpRegisterConstDerivedString(
Paul Stewarta794cd62015-06-16 13:13:10 -0700598 const std::string& name,
599 std::string(Device::*get)(Error*));
Chris Masoneb925cc82011-06-22 15:39:57 -0700600
Christopher Wiley674598d2014-12-12 10:21:39 -0800601 void HelpRegisterConstDerivedRpcIdentifier(
Paul Stewarta794cd62015-06-16 13:13:10 -0700602 const std::string& name,
603 RpcIdentifier(Device::*get)(Error*));
Christopher Wiley674598d2014-12-12 10:21:39 -0800604
Jason Glasgow08afdff2012-04-03 10:22:26 -0400605 void HelpRegisterConstDerivedRpcIdentifiers(
Paul Stewarta794cd62015-06-16 13:13:10 -0700606 const std::string& name,
607 RpcIdentifiers(Device::*get)(Error*));
Jason Glasgow08afdff2012-04-03 10:22:26 -0400608
Paul Stewart6ff27f52012-07-11 06:51:41 -0700609 void HelpRegisterConstDerivedUint64(
Paul Stewarta794cd62015-06-16 13:13:10 -0700610 const std::string& name,
611 uint64_t(Device::*get)(Error*));
Paul Stewart6ff27f52012-07-11 06:51:41 -0700612
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -0700613 // Called by the ConnectionTester whenever a connectivity test completes.
614 virtual void ConnectionTesterCallback();
615
Paul Stewartac4ac002011-08-26 12:04:26 -0700616 // Property getters reserved for subclasses
Paul Stewarta794cd62015-06-16 13:13:10 -0700617 ControlInterface* control_interface() const { return control_interface_; }
618 Metrics* metrics() const { return metrics_; }
619 Manager* manager() const { return manager_; }
620 const LinkMonitor* link_monitor() const { return link_monitor_.get(); }
621 void set_link_monitor(LinkMonitor* link_monitor);
Peter Qiudc335f82014-05-15 10:33:17 -0700622 // Use for unit test.
Paul Stewarta794cd62015-06-16 13:13:10 -0700623 void set_traffic_monitor(TrafficMonitor* traffic_monitor);
Paul Stewartd5843772011-05-11 15:40:42 -0700624
Samuel Tan787a1ce2014-11-11 17:17:27 -0800625 // Calculates the time (in seconds) till a DHCP lease is due for renewal,
626 // and stores this value in |result|. Returns false is there is no upcoming
627 // DHCP lease renewal, true otherwise.
Paul Stewarta794cd62015-06-16 13:13:10 -0700628 bool TimeToNextDHCPLeaseRenewal(uint32_t* result);
Samuel Tan787a1ce2014-11-11 17:17:27 -0800629
Paul Stewart75897df2011-04-27 09:05:53 -0700630 private:
Darin Petkov3189a472012-10-05 09:55:33 +0200631 friend class CellularCapabilityTest;
632 friend class CellularTest;
Chris Masone413a3192011-05-09 17:10:05 -0700633 friend class DeviceAdaptorInterface;
Paul Stewart6ff27f52012-07-11 06:51:41 -0700634 friend class DeviceByteCountTest;
Paul Stewartc681fa02012-03-02 19:40:04 -0800635 friend class DevicePortalDetectionTest;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700636 friend class DeviceTest;
Paul Stewartced3ad72013-04-03 13:39:25 -0700637 friend class EthernetTest;
Darin Petkov3189a472012-10-05 09:55:33 +0200638 friend class OpenVPNDriverTest;
Ben Chanbcc6e012013-11-04 14:28:37 -0800639 friend class TestDevice;
mukesh agrawal4a165582013-08-02 15:58:21 -0700640 friend class VirtualDeviceTest;
Paul Stewarte369ece2012-05-22 09:11:03 -0700641 friend class WiFiObjectTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700642
Paul Stewart2bf1d352011-12-06 15:02:55 -0800643 static const char kIPFlagTemplate[];
644 static const char kIPFlagVersion4[];
645 static const char kIPFlagVersion6[];
646 static const char kIPFlagDisableIPv6[];
647 static const char kIPFlagUseTempAddr[];
648 static const char kIPFlagUseTempAddrUsedAndDefault[];
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800649 static const char kIPFlagReversePathFilter[];
650 static const char kIPFlagReversePathFilterEnabled[];
651 static const char kIPFlagReversePathFilterLooseMode[];
Paul Stewart2cb3fa72014-11-13 01:43:12 -0800652 static const char kIPFlagArpAnnounce[];
653 static const char kIPFlagArpAnnounceDefault[];
654 static const char kIPFlagArpAnnounceBestLocal[];
655 static const char kIPFlagArpIgnore[];
656 static const char kIPFlagArpIgnoreDefault[];
657 static const char kIPFlagArpIgnoreLocalOnly[];
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700658 static const char kStoragePowered[];
Paul Stewart6ff27f52012-07-11 06:51:41 -0700659 static const char kStorageReceiveByteCount[];
660 static const char kStorageTransmitByteCount[];
Peter Qiub9256f32014-05-09 15:27:29 -0700661 static const char kFallbackDnsTestHostname[];
662 static const char* kFallbackDnsServers[];
663 static const int kDNSTimeoutMilliseconds;
Chris Masone5dec5f42011-07-22 14:07:55 -0700664
Peter Qiua0572032014-09-26 10:07:37 -0700665 // Maximum seconds between two link monitor failures to declare this link
666 // (network) as unreliable.
Peter Qiua388fdb2015-04-03 10:31:22 -0700667 static const int kLinkUnreliableThresholdSeconds;
Peter Qiua0572032014-09-26 10:07:37 -0700668
Peter Qiu62abf312015-05-05 12:58:05 -0700669 static const size_t kHardwareAddressLength;
670
Paul Stewart1062d9d2012-04-27 10:42:27 -0700671 // Configure static IP address parameters if the service provides them.
672 void ConfigureStaticIPTask();
673
Chris Masone5dec5f42011-07-22 14:07:55 -0700674 // Right now, Devices reference IPConfigs directly when persisted to disk
675 // It's not clear that this makes sense long-term, but that's how it is now.
676 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700677 // |suffix| is injected into the storage identifier used for the configs.
Paul Stewarta794cd62015-06-16 13:13:10 -0700678 std::string SerializeIPConfigs(const std::string& suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700679
Ben Chanbcc6e012013-11-04 14:28:37 -0800680 // Set an IP configuration flag on the device. |family| should be "ipv6" or
681 // "ipv4". |flag| should be the name of the flag to be set and |value| is
682 // what this flag should be set to. Overridden by unit tests to pretend
683 // writing to procfs.
684 virtual bool SetIPFlag(IPAddress::Family family,
Paul Stewarta794cd62015-06-16 13:13:10 -0700685 const std::string& flag,
686 const std::string& value);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800687
Paul Stewart2cb3fa72014-11-13 01:43:12 -0800688 // Request the removal of reverse-path filtering for this interface.
689 // This will allow packets destined for this interface to be accepted,
690 // even if this is not the default route for such a packet to arrive.
691 void DisableReversePathFilter();
692
693 // Request reverse-path filtering for this interface.
694 void EnableReversePathFilter();
695
696 // Disable ARP filtering on the device. The interface will exhibit the
697 // default Linux behavior -- incoming ARP requests are responded to by all
698 // interfaces. Outgoing ARP requests can contain any local address.
699 void DisableArpFiltering();
700
701 // Enable ARP filtering on the device. Incoming ARP requests are responded
702 // to only by the interface(s) owning the address. Outgoing ARP requests
703 // will contain the best local address for the target.
704 void EnableArpFiltering();
705
Paul Stewarta794cd62015-06-16 13:13:10 -0700706 std::string GetSelectedServiceRpcIdentifier(Error* error);
707 std::vector<std::string> AvailableIPConfigs(Error* error);
Chris Masone4e851612011-07-01 10:46:53 -0700708 std::string GetRpcConnectionIdentifier();
709
Paul Stewart036dba02012-08-07 12:34:41 -0700710 // Get the LinkMonitor's average response time.
Paul Stewarta794cd62015-06-16 13:13:10 -0700711 uint64_t GetLinkMonitorResponseTime(Error* error);
Paul Stewart036dba02012-08-07 12:34:41 -0700712
Ben Chanb061f892013-02-27 17:46:55 -0800713 // Get receive and transmit byte counters. These methods simply wrap
714 // GetReceiveByteCount and GetTransmitByteCount in order to be used by
715 // HelpRegisterConstDerivedUint64.
Paul Stewarta794cd62015-06-16 13:13:10 -0700716 uint64_t GetReceiveByteCountProperty(Error* error);
717 uint64_t GetTransmitByteCountProperty(Error* error);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700718
Paul Stewartd4f26482014-04-25 19:12:03 -0700719 // Emit a property change signal for the "IPConfigs" property of this device.
720 void UpdateIPConfigsProperty();
721
Peter Qiu6f5618b2014-06-05 15:19:01 -0700722 // Called by DNS server tester when the fallback DNS servers test completes.
Peter Qiud670d032014-06-03 15:04:43 -0700723 void FallbackDNSResultCallback(const DNSServerTester::Status status);
Peter Qiub9256f32014-05-09 15:27:29 -0700724
Peter Qiu6f5618b2014-06-05 15:19:01 -0700725 // Called by DNS server tester when the configured DNS servers test completes.
726 void ConfigDNSResultCallback(const DNSServerTester::Status status);
727
Peter Qiua89154b2014-05-23 15:45:42 -0700728 // Update DNS setting with the given DNS servers for the current connection.
Paul Stewarta794cd62015-06-16 13:13:10 -0700729 void SwitchDNSServers(const std::vector<std::string>& dns_servers);
Peter Qiua89154b2014-05-23 15:45:42 -0700730
Peter Qiu25f1be62014-08-12 10:42:27 -0700731 // Called when the lifetime for IPv6 DNS server expires.
732 void IPv6DNSServerExpired();
733
Peter Qiub25083f2014-08-25 13:22:31 -0700734 // Return true if given IP configuration contain both IP address and DNS
735 // servers. Hence, ready to be used for network connection.
Paul Stewarta794cd62015-06-16 13:13:10 -0700736 bool IPConfigCompleted(const IPConfigRefPtr& ipconfig);
Peter Qiub25083f2014-08-25 13:22:31 -0700737
738 // Setup network connection with given IP configuration, and start portal
739 // detection on that connection.
Paul Stewarta794cd62015-06-16 13:13:10 -0700740 void SetupConnection(const IPConfigRefPtr& ipconfig);
Peter Qiub25083f2014-08-25 13:22:31 -0700741
Paul Stewart208a97e2015-05-13 09:11:12 -0700742 // Set the system hostname to |hostname| if this device is configured to
743 // do so. If |hostname| is too long, truncate this parameter to fit within
744 // the maximum hostname size.
Paul Stewarta794cd62015-06-16 13:13:10 -0700745 bool SetHostname(const std::string& hostname);
Paul Stewart208a97e2015-05-13 09:11:12 -0700746
Garret Kellyc5f89d12015-02-18 14:39:36 -0500747 // Prepend the Manager's configured list of DNS servers into |ipconfig|
748 // ensuring that only DNS servers of the same address family as |ipconfig| are
749 // included in the final list.
Paul Stewarta794cd62015-06-16 13:13:10 -0700750 void PrependDNSServersIntoIPConfig(const IPConfigRefPtr& ipconfig);
Garret Kellyc5f89d12015-02-18 14:39:36 -0500751
752 // Mutate |servers| to include the Manager's prepended list of DNS servers for
753 // |family|. On return, it is guaranteed that there are no duplicate entries
754 // in |servers|.
755 void PrependDNSServers(const IPAddress::Family family,
Paul Stewarta794cd62015-06-16 13:13:10 -0700756 std::vector<std::string>* servers);
Garret Kellyc5f89d12015-02-18 14:39:36 -0500757
Eric Shienbrood9a245532012-03-07 14:20:39 -0500758 // |enabled_persistent_| is the value of the Powered property, as
759 // read from the profile. If it is not found in the profile, it
760 // defaults to true. |enabled_| reflects the real-time state of
761 // the device, i.e., enabled or disabled. |enabled_pending_| reflects
762 // the target state of the device while an enable or disable operation
763 // is occurring.
764 //
765 // Some typical sequences for these state variables are shown below.
766 //
767 // Shill starts up, profile has been read:
768 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=false
769 //
770 // Shill acts on the value of |enabled_persistent_|, calls SetEnabled(true):
771 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=true
772 //
773 // SetEnabled completes successfully, device is enabled:
774 // |enabled_persistent_|=true |enabled_|=true |enabled_pending_|=true
775 //
776 // User presses "Disable" button, SetEnabled(false) is called:
777 // |enabled_persistent_|=false |enabled_|=true |enabled_pending_|=false
778 //
779 // SetEnabled completes successfully, device is disabled:
780 // |enabled_persistent_|=false |enabled_|=false |enabled_pending_|=false
781 bool enabled_;
782 bool enabled_persistent_;
783 bool enabled_pending_;
784
785 // Other properties
Paul Stewartac4ac002011-08-26 12:04:26 -0700786 bool reconnect_;
787 const std::string hardware_address_;
788
789 PropertyStore store_;
790
Paul Stewartac4ac002011-08-26 12:04:26 -0700791 const int interface_index_;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800792 bool running_; // indicates whether the device is actually in operation
Paul Stewartac4ac002011-08-26 12:04:26 -0700793 const std::string link_name_;
794 const std::string unique_id_;
Paul Stewarta794cd62015-06-16 13:13:10 -0700795 ControlInterface* control_interface_;
796 EventDispatcher* dispatcher_;
797 Metrics* metrics_;
798 Manager* manager_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700799 IPConfigRefPtr ipconfig_;
Paul Stewartd4f26482014-04-25 19:12:03 -0700800 IPConfigRefPtr ip6config_;
Peter Qiud48fa0c2015-06-10 12:20:48 -0700801 IPConfigRefPtr dhcpv6_config_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700802 ConnectionRefPtr connection_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500803 base::WeakPtrFactory<Device> weak_ptr_factory_;
Ben Chancd477322014-10-17 14:19:30 -0700804 std::unique_ptr<DeviceAdaptorInterface> adaptor_;
805 std::unique_ptr<PortalDetector> portal_detector_;
806 std::unique_ptr<LinkMonitor> link_monitor_;
Peter Qiu6f5618b2014-06-05 15:19:01 -0700807 // Used for verifying whether DNS server is functional.
Ben Chancd477322014-10-17 14:19:30 -0700808 std::unique_ptr<DNSServerTester> dns_server_tester_;
Paul Stewarta794cd62015-06-16 13:13:10 -0700809 base::Callback<void(const PortalDetector::Result&)>
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500810 portal_detector_callback_;
Peter Qiu25f1be62014-08-12 10:42:27 -0700811 // Callback to invoke when IPv6 DNS servers lifetime expired.
812 base::CancelableClosure ipv6_dns_server_expired_callback_;
Ben Chancd477322014-10-17 14:19:30 -0700813 std::unique_ptr<TrafficMonitor> traffic_monitor_;
Peter Qiu6f5618b2014-06-05 15:19:01 -0700814 // DNS servers obtained from ipconfig (either from DHCP or static config)
815 // that are not working.
816 std::vector<std::string> config_dns_servers_;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800817 Technology::Identifier technology_;
Thieu Le85e050b2012-03-13 15:04:38 -0700818 // The number of portal detection attempts from Connected to Online state.
819 // This includes all failure/timeout attempts and the final successful
820 // attempt.
821 int portal_attempts_to_online_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700822
Paul Stewart6ff27f52012-07-11 06:51:41 -0700823 // Keep track of the offset between the interface-reported byte counters
824 // and our persisted value.
Ben Chan7fab8972014-08-10 17:14:46 -0700825 uint64_t receive_byte_offset_;
826 uint64_t transmit_byte_offset_;
Paul Stewart6ff27f52012-07-11 06:51:41 -0700827
Paul Stewart03dba0b2011-08-22 16:32:45 -0700828 // Maintain a reference to the connected / connecting service
829 ServiceRefPtr selected_service_;
830
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700831 // Cache singleton pointers for performance and test purposes.
Paul Stewarta794cd62015-06-16 13:13:10 -0700832 DHCPProvider* dhcp_provider_;
833 RTNLHandler* rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700834
Peter Qiua0572032014-09-26 10:07:37 -0700835 // Time when link monitor last failed.
Paul Stewarta794cd62015-06-16 13:13:10 -0700836 Time* time_;
Peter Qiua0572032014-09-26 10:07:37 -0700837 time_t last_link_monitor_failed_time_;
Peter Qiua388fdb2015-04-03 10:31:22 -0700838 // Callback to invoke when link becomes reliable again after it was previously
839 // unreliable.
840 base::CancelableClosure reliable_link_callback_;
Peter Qiua0572032014-09-26 10:07:37 -0700841
Ben Chancd477322014-10-17 14:19:30 -0700842 std::unique_ptr<ConnectionTester> connection_tester_;
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -0700843 base::Callback<void()> connection_tester_callback_;
844
Paul Stewart2cb3fa72014-11-13 01:43:12 -0800845 // Track whether packets from non-optimal routes will be accepted by this
846 // device. This is referred to as "loose mode" (see RFC3704).
847 bool is_loose_routing_;
848
849 // Track the current same-net multi-home state.
850 bool is_multi_homed_;
851
Paul Stewart38fcf162015-06-12 09:52:17 -0700852 // Remember which flag files were previously successfully written.
853 std::set<std::string> written_flags_;
854
Chris Masone9be4a9d2011-05-16 15:44:09 -0700855 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700856};
857
Paul Stewart75897df2011-04-27 09:05:53 -0700858} // namespace shill
859
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200860#endif // SHILL_DEVICE_H_