blob: de86f0e3274e1614f05dff65a4d1ebe72ee68d6c [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
Chris Masone46eaaf52011-05-24 13:08:30 -07008#include <string>
Chris Masone9be4a9d2011-05-16 15:44:09 -07009#include <vector>
10
Chris Masone8fe2c7e2011-06-09 15:51:19 -070011#include <base/basictypes.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070012#include <base/memory/ref_counted.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -070013#include <base/memory/scoped_ptr.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"
Peter Qiud670d032014-06-03 15:04:43 -070019#include "shill/dns_server_tester.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070020#include "shill/event_dispatcher.h"
Paul Stewart2bf1d352011-12-06 15:02:55 -080021#include "shill/ip_address.h"
Samuel Tanfe734672014-08-07 15:50:48 -070022#include "shill/ip_address_store.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070023#include "shill/ipconfig.h"
Paul Stewart20088d82012-02-16 06:58:55 -080024#include "shill/portal_detector.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070025#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070026#include "shill/refptr_types.h"
Paul Stewart03dba0b2011-08-22 16:32:45 -070027#include "shill/service.h"
Paul Stewartfdd16072011-09-16 12:41:35 -070028#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070029
30namespace shill {
31
Chris Masone9be4a9d2011-05-16 15:44:09 -070032class ControlInterface;
Darin Petkov77cb6812011-08-15 16:19:41 -070033class DHCPProvider;
Chris Masone9be4a9d2011-05-16 15:44:09 -070034class DeviceAdaptorInterface;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070035class Endpoint;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070036class Error;
37class EventDispatcher;
Gaurav Shah6d2c72d2012-10-16 16:30:44 -070038class GeolocationInfo;
Paul Stewart036dba02012-08-07 12:34:41 -070039class LinkMonitor;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070040class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080041class Metrics;
Thieu Lefb46caf2012-03-08 11:57:15 -080042class RTNLHandler;
Ben Chanb061f892013-02-27 17:46:55 -080043class TrafficMonitor;
Chris Masone9be4a9d2011-05-16 15:44:09 -070044
Paul Stewart75897df2011-04-27 09:05:53 -070045// Device superclass. Individual network interfaces types will inherit from
46// this class.
Chris Masone27c4aa52011-07-02 13:10:14 -070047class Device : public base::RefCounted<Device> {
Paul Stewart75897df2011-04-27 09:05:53 -070048 public:
Wade Guthrie68d41092013-04-02 12:56:02 -070049 // Progressively scanning for access points (APs) is done with multiple scans,
50 // each containing a group of channels. The scans are performed in order of
51 // decreasing likelihood of connecting on one of the channels in a group
52 // (the number of channels in a group is a matter for system tuning). Fully
53 // scanning for APs does a complete scan of all the channels in a single scan.
54 // Progressive scanning is supported for wifi devices; technologies that
55 // support scan but don't support progressive scan will always perform a full
56 // scan, regardless of the requested scan type.
57 enum ScanType { kProgressiveScan, kFullScan };
58
Chris Masone9be4a9d2011-05-16 15:44:09 -070059 // A constructor for the Device object
60 Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070061 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080062 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070063 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070064 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070065 const std::string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080066 int interface_index,
67 Technology::Identifier technology);
Chris Masone9be4a9d2011-05-16 15:44:09 -070068
mukesh agrawal28185512013-10-18 16:57:09 -070069 // Enable or disable the device. This is a convenience method for
70 // cases where we want to SetEnabledNonPersistent, but don't care
71 // about the results.
Eric Shienbrood9a245532012-03-07 14:20:39 -050072 virtual void SetEnabled(bool enable);
mukesh agrawalb1136662013-10-14 16:39:38 -070073 // Enable or disable the device. Unlike SetEnabledPersistent, it does not
Ben Chan9f3dcf82013-09-25 18:04:58 -070074 // save the setting in the profile.
75 //
mukesh agrawal28185512013-10-18 16:57:09 -070076 // TODO(quiche): Replace both of the next two methods with calls to
77 // SetEnabledChecked.
Ben Chan9f3dcf82013-09-25 18:04:58 -070078 virtual void SetEnabledNonPersistent(bool enable,
79 Error *error,
80 const ResultCallback &callback);
Eric Shienbrood9a245532012-03-07 14:20:39 -050081 // Enable or disable the device, and save the setting in the profile.
82 // The setting is persisted before the enable or disable operation
83 // starts, so that even if it fails, the user's intent is still recorded
84 // for the next time shill restarts.
Jason Glasgowdf7c5532012-05-14 14:41:45 -040085 virtual void SetEnabledPersistent(bool enable,
86 Error *error,
87 const ResultCallback &callback);
mukesh agrawal28185512013-10-18 16:57:09 -070088 // Enable or disable the Device, depending on |enable|.
89 // Save the new setting to the profile, if |persist| is true.
90 // Report synchronous errors using |error|, and asynchronous completion
91 // with |callback|.
92 virtual void SetEnabledChecked(bool enable,
93 bool persist,
94 Error *error,
95 const ResultCallback &callback);
96 // Similar to SetEnabledChecked, but without sanity checking, and
97 // without saving the new value of |enable| to the profile. If you
98 // are sane (i.e. not Cellular), you should use
99 // SetEnabledChecked instead.
100 virtual void SetEnabledUnchecked(bool enable,
101 Error *error,
102 const ResultCallback &callback);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800103
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400104 // Returns true if the underlying device reports that it is already enabled.
105 // Used when the device is registered with the Manager, so that shill can
106 // sync its state/ with the true state of the device. The default is to
107 // report false.
108 virtual bool IsUnderlyingDeviceEnabled() const;
109
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700110 virtual void LinkEvent(unsigned flags, unsigned change);
Paul Stewart75897df2011-04-27 09:05:53 -0700111
Darin Petkov9ae310f2011-08-30 15:41:13 -0700112 // The default implementation sets |error| to kNotSupported.
Wade Guthrie4823f4f2013-07-25 10:03:03 -0700113 virtual void Scan(ScanType scan_type, Error *error,
114 const std::string &reason);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500115 virtual void RegisterOnNetwork(const std::string &network_id, Error *error,
116 const ResultCallback &callback);
117 virtual void RequirePIN(const std::string &pin, bool require,
118 Error *error, const ResultCallback &callback);
119 virtual void EnterPIN(const std::string &pin,
120 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700121 virtual void UnblockPIN(const std::string &unblock_code,
122 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500123 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700124 virtual void ChangePIN(const std::string &old_pin,
125 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500126 Error *error, const ResultCallback &callback);
Ben Chanad663e12013-01-08 01:58:47 -0800127 virtual void Reset(Error *error, const ResultCallback &callback);
128
Darin Petkovc37a9c42012-09-06 15:28:22 +0200129 virtual void SetCarrier(const std::string &carrier,
130 Error *error, const ResultCallback &callback);
Ben Chanbcc6e012013-11-04 14:28:37 -0800131
132 // Returns true if IPv6 is allowed and should be enabled when the device
133 // tries to acquire an IP configuration. The default implementation allows
134 // IPv6, which can be overridden by a derived class.
135 virtual bool IsIPv6Allowed() const;
136
Paul Stewart2bf1d352011-12-06 15:02:55 -0800137 virtual void DisableIPv6();
138 virtual void EnableIPv6();
139 virtual void EnableIPv6Privacy();
Darin Petkov9ae310f2011-08-30 15:41:13 -0700140
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800141 // Request the removal of reverse-path filtering for this interface.
142 // This will allow packets destined for this interface to be accepted,
143 // even if this is not the default route for such a packet to arrive.
144 virtual void DisableReversePathFilter();
145
146 // Request reverse-path filtering for this interface.
147 virtual void EnableReversePathFilter();
148
Gaurav Shah435de2c2011-11-17 19:01:07 -0800149 // Returns true if the selected service on the device (if any) is connected.
150 // Returns false if there is no selected service, or if the selected service
151 // is not connected.
152 bool IsConnected() const;
153
Christopher Wiley5519e9e2013-01-08 16:55:56 -0800154 // Called by Device so that subclasses can run hooks on the selected service
155 // getting an IP. Subclasses should call up to the parent first.
156 virtual void OnConnected();
157
Paul Stewart8596f9f2013-03-14 07:58:26 -0700158 // Called by the Connection so that the Device can update the service sorting
159 // after one connection is bound to another.
160 virtual void OnConnectionUpdated();
161
Paul Stewartd215af62012-04-24 23:25:50 -0700162 // Returns true if the selected service on the device (if any) is connected
163 // and matches the passed-in argument |service|. Returns false if there is
164 // no connected service, or if it does not match |service|.
165 virtual bool IsConnectedToService(const ServiceRefPtr &service) const;
166
Paul Stewartfa11e282013-12-02 22:04:25 -0800167 // Returns true if the DHCP parameters provided indicate that we are tethered
168 // to a mobile device.
169 virtual bool IsConnectedViaTether() const;
170
Paul Stewartd215af62012-04-24 23:25:50 -0700171 // Restart the portal detection process on a connected device. This is
172 // useful if the properties on the connected service have changed in a
173 // way that may affect the decision to run portal detection at all.
174 // Returns true if portal detection was started.
175 virtual bool RestartPortalDetection();
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
Chris Masone9d779932011-08-25 16:33:41 -0700206 const std::string &address() const { return hardware_address_; }
Darin Petkove0a312e2011-07-20 13:45:28 -0700207 const std::string &link_name() const { return link_name_; }
208 int interface_index() const { return interface_index_; }
Paul Stewarte6132022011-08-16 09:11:02 -0700209 const ConnectionRefPtr &connection() const { return connection_; }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500210 bool enabled() const { return enabled_; }
211 bool enabled_persistent() const { return enabled_persistent_; }
Gaurav Shah435de2c2011-11-17 19:01:07 -0800212 virtual Technology::Identifier technology() const { return technology_; }
Jason Glasgowb5790052012-01-27 01:03:52 -0500213 std::string GetTechnologyString(Error *error);
Darin Petkove0a312e2011-07-20 13:45:28 -0700214
Thieu Le03026662013-04-04 10:45:11 -0700215 virtual const IPConfigRefPtr &ipconfig() const { return ipconfig_; }
Darin Petkov31a2eca2012-03-14 11:07:18 +0100216 void set_ipconfig(const IPConfigRefPtr &config) { ipconfig_ = config; }
217
Chris Masone19e30402011-07-19 15:48:47 -0700218 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -0700219
Darin Petkovafa6fc42011-06-21 16:21:08 -0700220 // Returns a string that is guaranteed to uniquely identify this Device
221 // instance.
222 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -0700223
mukesh agrawalde29fa82011-09-16 16:16:36 -0700224 PropertyStore *mutable_store() { return &store_; }
225 const PropertyStore &store() const { return store_; }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700226 RTNLHandler *rtnl_handler() { return rtnl_handler_; }
Ben Chan19f83972012-10-03 23:25:56 -0700227 bool running() const { return running_; }
Chris Masone19e30402011-07-19 15:48:47 -0700228
Darin Petkovb05315f2011-11-07 10:14:25 +0100229 EventDispatcher *dispatcher() const { return dispatcher_; }
230
Paul Stewarta41e38d2011-11-11 07:47:29 -0800231 // Load configuration for the device from |storage|. This may include
232 // instantiating non-visible services for which configuration has been
233 // stored.
234 virtual bool Load(StoreInterface *storage);
235
236 // Save configuration for the device to |storage|.
Chris Masone877ff982011-09-21 16:18:24 -0700237 virtual bool Save(StoreInterface *storage);
Chris Masone5dec5f42011-07-22 14:07:55 -0700238
Darin Petkov77cb6812011-08-15 16:19:41 -0700239 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
240
Darin Petkov3cfbf212011-11-21 16:02:09 +0100241 DeviceAdaptorInterface *adaptor() const { return adaptor_.get(); }
242
mukesh agrawal784566d2012-08-08 18:32:58 -0700243 // Suspend event handler. Called by Manager before the system
mukesh agrawalb1136662013-10-14 16:39:38 -0700244 // suspends. This handler, along with any other suspect handlers,
245 // will have Manager::kTerminationActionsTimeoutMilliseconds to
246 // execute before the system enters the suspend state.
mukesh agrawal784566d2012-08-08 18:32:58 -0700247 //
mukesh agrawalb1136662013-10-14 16:39:38 -0700248 // Code that needs to run on exit, as well as on suspend, should use
249 // Manager::AddTerminationAction, rather than OnBeforeSuspend.
mukesh agrawal784566d2012-08-08 18:32:58 -0700250 virtual void OnBeforeSuspend();
251
252 // Resume event handler. Called by Manager as the system resumes.
253 // The base class implementation takes care of renewing a DHCP lease
254 // (if necessary). Derived classes may implement any technology
255 // specific requirements by overriding, but should include a call to
256 // the base class implementation.
257 virtual void OnAfterResume();
258
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800259 // Destroy the lease, if any, with this |name|.
260 // Called by the service during Unload() as part of the cleanup sequence.
261 virtual void DestroyIPConfigLease(const std::string &name);
262
Paul Stewartd55f6ae2014-04-24 16:34:21 -0700263 // Called by DeviceInfo when the kernel adds or removes a globally-scoped
264 // IPv6 address from this interface.
Paul Stewartd4f26482014-04-25 19:12:03 -0700265 virtual void OnIPv6AddressChanged();
Paul Stewartd55f6ae2014-04-24 16:34:21 -0700266
Peter Qiu98551702014-07-28 13:28:53 -0700267 // Called by DeviceInfo when the kernel receives a update for IPv6 DNS server
268 // addresses from this interface.
269 virtual void OnIPv6DnsServerAddressesChanged();
270
Samuel Tanfe734672014-08-07 15:50:48 -0700271 // Requests that NICs be programmed to wake up from suspend on the arrival of
272 // packets on the TCP connection specified by the string argument.
273 virtual void AddWakeOnPacketConnection(const IPAddress &ip_endpoint,
274 Error *error);
275 // Removes a NIC programming request established by AddWakeOnPacketConnection.
276 virtual void RemoveWakeOnPacketConnection(const IPAddress &ip_endpoint,
277 Error *error);
278 // Removes all NIC programming requests.
279 virtual void RemoveAllWakeOnPacketConnections(Error *error);
280
281 // Get all registered wake-on-packet connections
282 const IPAddressStore &GetAllWakeOnPacketConnections() const {
283 return wake_on_packet_connections_;
284 }
285
286 // Adds wake-on-packet connections specified by the IP addresses in
287 // |ip_addresses|
288 void AddWakeOnPacketConnections(const IPAddressStore &ip_addresses);
289
Paul Stewartd5843772011-05-11 15:40:42 -0700290 protected:
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200291 friend class base::RefCounted<Device>;
Arman Ugurayf84a4242013-04-09 20:01:07 -0700292 friend class DeviceHealthCheckerTest;
Ben Chan19f83972012-10-03 23:25:56 -0700293 FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
Arman Ugurayd42d8ec2013-04-08 19:28:21 -0700294 FRIEND_TEST(CellularTest, EnableTrafficMonitor);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400295 FRIEND_TEST(CellularTest, ModemStateChangeDisable);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800296 FRIEND_TEST(CellularTest, UseNoArpGateway);
Darin Petkov13e6d552012-05-09 14:22:23 +0200297 FRIEND_TEST(ConnectionTest, OnRouteQueryResponse);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700298 FRIEND_TEST(DeviceHealthCheckerTest, HealthCheckerPersistsAcrossDeviceReset);
Arman Ugurayf84a4242013-04-09 20:01:07 -0700299 FRIEND_TEST(DeviceHealthCheckerTest, RequestConnectionHealthCheck);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700300 FRIEND_TEST(DeviceHealthCheckerTest, SetupHealthChecker);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800301 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Paul Stewartd4f26482014-04-25 19:12:03 -0700302 FRIEND_TEST(DeviceTest, AvailableIPConfigs);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700303 FRIEND_TEST(DeviceTest, DestroyIPConfig);
304 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Ben Chanbcc6e012013-11-04 14:28:37 -0800305 FRIEND_TEST(DeviceTest, EnableIPv6);
Chris Masone95207da2011-06-29 16:50:49 -0700306 FRIEND_TEST(DeviceTest, GetProperties);
Paul Stewartfa11e282013-12-02 22:04:25 -0800307 FRIEND_TEST(DeviceTest, IsConnectedViaTether);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700308 FRIEND_TEST(DeviceTest, Load);
Paul Stewartd4f26482014-04-25 19:12:03 -0700309 FRIEND_TEST(DeviceTest, OnIPv6AddressChanged);
Peter Qiu25f1be62014-08-12 10:42:27 -0700310 FRIEND_TEST(DeviceTest, OnIPv6DnsServerAddressesChanged);
Chris Masone5dec5f42011-07-22 14:07:55 -0700311 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700312 FRIEND_TEST(DeviceTest, SelectedService);
Ben Chan9f3dcf82013-09-25 18:04:58 -0700313 FRIEND_TEST(DeviceTest, SetEnabledNonPersistent);
Darin Petkove7c6ad32012-06-29 10:22:09 +0200314 FRIEND_TEST(DeviceTest, SetEnabledPersistent);
Paul Stewartc681fa02012-03-02 19:40:04 -0800315 FRIEND_TEST(DeviceTest, SetServiceConnectedState);
Paul Stewart75a68b92013-10-24 10:50:27 -0700316 FRIEND_TEST(DeviceTest, ShouldUseArpGateway);
Paul Stewart8c116a92012-05-02 18:30:03 -0700317 FRIEND_TEST(DeviceTest, Start);
Ben Chanb061f892013-02-27 17:46:55 -0800318 FRIEND_TEST(DeviceTest, StartTrafficMonitor);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700319 FRIEND_TEST(DeviceTest, Stop);
Ben Chanb061f892013-02-27 17:46:55 -0800320 FRIEND_TEST(DeviceTest, StopTrafficMonitor);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800321 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800322 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
323 FRIEND_TEST(ManagerTest, DefaultTechnology);
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700324 FRIEND_TEST(ManagerTest, SetEnabledStateForTechnology);
mukesh agrawalf407d592013-07-31 11:37:57 -0700325 FRIEND_TEST(PPPDeviceTest, UpdateIPConfigFromPPP);
mukesh agrawal32399322011-09-01 10:53:43 -0700326 FRIEND_TEST(WiFiMainTest, Connect);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800327 FRIEND_TEST(WiFiMainTest, UseArpGateway);
Darin Petkov3a4100c2012-06-14 11:36:59 +0200328 FRIEND_TEST(WiMaxTest, ConnectTimeout);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800329 FRIEND_TEST(WiMaxTest, UseNoArpGateway);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700330
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200331 virtual ~Device();
332
Eric Shienbrood9a245532012-03-07 14:20:39 -0500333 // Each device must implement this method to do the work needed to
334 // enable the device to operate for establishing network connections.
335 // The |error| argument, if not NULL,
336 // will refer to an Error that starts out with the value
337 // Error::kOperationInitiated. This reflects the assumption that
338 // enable (and disable) operations will usually be non-blocking,
339 // and their completion will be indicated by means of an asynchronous
340 // reply sometime later. There are two circumstances in which a
341 // device's Start() method may overwrite |error|:
342 //
343 // 1. If an early failure is detected, such that the non-blocking
344 // part of the operation never takes place, then |error| should
345 // be set to the appropriate value corresponding to the type
346 // of failure. This is the "immediate failure" case.
347 // 2. If the device is enabled without performing any non-blocking
348 // steps, then |error| should be Reset, i.e., its value set
349 // to Error::kSuccess. This is the "immediate success" case.
350 //
351 // In these two cases, because completion is immediate, |callback|
352 // is not used. If neither of these two conditions holds, then |error|
353 // should not be modified, and |callback| should be passed to the
354 // method that will initiate the non-blocking operation.
355 virtual void Start(Error *error,
356 const EnabledStateChangedCallback &callback) = 0;
357
358 // Each device must implement this method to do the work needed to
359 // disable the device, i.e., clear any running state, and make the
360 // device no longer capable of establishing network connections.
361 // The discussion for Start() regarding the use of |error| and
362 // |callback| apply to Stop() as well.
363 virtual void Stop(Error *error,
364 const EnabledStateChangedCallback &callback) = 0;
365
366 // The EnabledStateChangedCallback that gets passed to the device's
367 // Start() and Stop() methods is bound to this method. |callback|
368 // is the callback that was passed to SetEnabled().
369 void OnEnabledStateChanged(const ResultCallback &callback,
370 const Error &error);
371
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200372 // Drops the currently selected service along with its IP configuration and
373 // connection, if any.
mukesh agrawal5d851b12013-07-11 14:09:41 -0700374 virtual void DropConnection();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200375
Darin Petkovafa6fc42011-06-21 16:21:08 -0700376 // If there's an IP configuration in |ipconfig_|, releases the IP address and
377 // destroys the configuration instance.
378 void DestroyIPConfig();
379
380 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
Paul Stewartd408fdf2012-05-07 17:15:57 -0700381 // requests a new IP configuration. Saves the DHCP lease to the generic
382 // lease filename based on the interface name. Registers a callback to
Darin Petkovafa6fc42011-06-21 16:21:08 -0700383 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
384 // request was successfully sent.
Paul Stewart2bf1d352011-12-06 15:02:55 -0800385 bool AcquireIPConfig();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700386
Paul Stewartd408fdf2012-05-07 17:15:57 -0700387 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
388 // requests a new IP configuration. Saves the DHCP lease to a filename
389 // based on the passed-in |lease_name|. Registers a callback to
390 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
391 // request was successfully sent.
392 bool AcquireIPConfigWithLeaseName(const std::string &lease_name);
393
Ben Chan539ab022014-02-03 16:34:57 -0800394 // Assigns the IP configuration |properties| to |ipconfig_|.
395 void AssignIPConfig(const IPConfig::Properties &properties);
396
Paul Stewartc5099532013-12-12 07:53:15 -0800397 // Callback invoked on successful IP configuration updates.
398 void OnIPConfigUpdated(const IPConfigRefPtr &ipconfig);
399
400 // Callback invoked on IP configuration failures.
401 void OnIPConfigFailed(const IPConfigRefPtr &ipconfig);
Darin Petkov79d74c92012-03-07 17:20:32 +0100402
Paul Stewart82236532013-12-10 15:33:11 -0800403 // Callback invoked when "Refresh" is invoked on an IPConfig. This usually
404 // signals a change in static IP parameters.
405 void OnIPConfigRefreshed(const IPConfigRefPtr &ipconfig);
406
Paul Stewart1f916e42013-12-23 09:52:54 -0800407 // Callback invoked when an IPConfig restarts due to lease expiry. This
408 // is advisory, since an "Updated" or "Failed" signal is guaranteed to
409 // follow.
410 void OnIPConfigExpired(const IPConfigRefPtr &ipconfig);
411
Paul Stewartf6f96482013-07-12 12:49:15 -0700412 // Called by Device so that subclasses can run hooks on the selected service
413 // failing to get an IP. The default implementation disconnects the selected
414 // service with Service::kFailureDHCP.
415 virtual void OnIPConfigFailure();
416
Paul Stewarte6132022011-08-16 09:11:02 -0700417 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
418 void CreateConnection();
419
420 // Remove connection state
421 void DestroyConnection();
422
Paul Stewart03dba0b2011-08-22 16:32:45 -0700423 // Selects a service to be "current" -- i.e. link-state or configuration
424 // events that happen to the device are attributed to this service.
425 void SelectService(const ServiceRefPtr &service);
426
mukesh agrawalfc362912013-08-06 18:10:07 -0700427 // Set the state of the |selected_service_|.
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700428 virtual void SetServiceState(Service::ConnectState state);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700429
430 // Set the failure of the selected service (implicitly sets the state to
mukesh agrawalfc362912013-08-06 18:10:07 -0700431 // "failure").
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700432 virtual void SetServiceFailure(Service::ConnectFailure failure_state);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700433
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400434 // Records the failure mode and time of the selected service, and
435 // sets the Service state of the selected service to "Idle".
436 // Avoids showing a failure mole in the UI.
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700437 virtual void SetServiceFailureSilent(Service::ConnectFailure failure_state);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400438
Paul Stewart20088d82012-02-16 06:58:55 -0800439 // Called by the Portal Detector whenever a trial completes. Device
440 // subclasses that choose unique mappings from portal results to connected
441 // states can override this method in order to do so.
442 virtual void PortalDetectorCallback(const PortalDetector::Result &result);
443
444 // Initiate portal detection, if enabled for this device type.
445 bool StartPortalDetection();
446
447 // Stop portal detection if it is running.
448 void StopPortalDetection();
449
Paul Stewart036dba02012-08-07 12:34:41 -0700450 // Initiate link monitoring, if enabled for this device type.
451 bool StartLinkMonitor();
452
453 // Stop link monitoring if it is running.
454 void StopLinkMonitor();
455
456 // Respond to a LinkMonitor failure in a Device-specific manner.
457 virtual void OnLinkMonitorFailure();
458
Peter Qiu9d581932014-04-14 16:37:37 -0700459 // Respond to a LinkMonitor gateway's MAC address found/change event.
460 virtual void OnLinkMonitorGatewayChange();
Peter Qiub5d124f2014-04-14 12:05:02 -0700461
Peter Qiudc335f82014-05-15 10:33:17 -0700462 // Returns true if traffic monitor is enabled on this device. The default
463 // implementation will return false, which can be overridden by a derived
464 // class.
465 virtual bool IsTrafficMonitorEnabled() const;
466
467 // Initiates traffic monitoring on the device if traffic monitor is enabled.
468 void StartTrafficMonitor();
469
470 // Stops traffic monitoring on the device if traffic monitor is enabled.
471 void StopTrafficMonitor();
472
Peter Qiu6f5618b2014-06-05 15:19:01 -0700473 // Start DNS test for the given servers. When retry_until_success is set,
474 // callback will only be invoke when the test succeed or the test failed to
475 // start (internal error). This function will return false if there is a test
476 // that's already running, and true otherwise.
477 virtual bool StartDNSTest(
478 const std::vector<std::string> &dns_servers,
479 const bool retry_until_success,
480 const base::Callback<void(const DNSServerTester::Status)> &callback);
481 // Stop DNS test if one is running.
482 virtual void StopDNSTest();
483
Peter Qiu25f1be62014-08-12 10:42:27 -0700484 // Timer function for monitoring IPv6 DNS server's lifetime.
485 void StartIPv6DNSServerTimer(uint32 lifetime_seconds);
486 void StopIPv6DNSServerTimer();
487
488 // Stop all monitoring/testing activities on this device. Called when tearing
489 // down or changing network connection on the device.
490 void StopAllActivities();
491
Peter Qiudc335f82014-05-15 10:33:17 -0700492 // Called by the Traffic Monitor when it detects a network problem. Device
493 // subclasses that want to roam to a different network when encountering
494 // network problems can override this method in order to do so. The parent
495 // implementation handles the metric reporting of the network problem.
496 virtual void OnEncounterNetworkProblem(int reason);
497
Paul Stewart20088d82012-02-16 06:58:55 -0800498 // Set the state of the selected service, with checks to make sure
499 // the service is already in a connected state before doing so.
500 void SetServiceConnectedState(Service::ConnectState state);
501
Arman Ugurayed8e6102012-11-29 14:47:20 -0800502 // Specifies whether an ARP gateway should be used for the
503 // device technology.
504 virtual bool ShouldUseArpGateway() const;
505
Paul Stewart316acef2014-05-29 18:40:48 -0700506 // Indicates if the selected service is configured with a static IP address.
507 bool IsUsingStaticIP() const;
508
Darin Petkov9893d9c2012-05-17 15:27:31 -0700509 const ServiceRefPtr &selected_service() const { return selected_service_; }
510
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700511 void HelpRegisterConstDerivedString(
Jason Glasgowb5790052012-01-27 01:03:52 -0500512 const std::string &name,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700513 std::string(Device::*get)(Error *));
Chris Masoneb925cc82011-06-22 15:39:57 -0700514
Jason Glasgow08afdff2012-04-03 10:22:26 -0400515 void HelpRegisterConstDerivedRpcIdentifiers(
516 const std::string &name,
517 RpcIdentifiers(Device::*get)(Error *));
518
Paul Stewart6ff27f52012-07-11 06:51:41 -0700519 void HelpRegisterConstDerivedUint64(
520 const std::string &name,
Ben Chan7fab8972014-08-10 17:14:46 -0700521 uint64_t(Device::*get)(Error *));
Paul Stewart6ff27f52012-07-11 06:51:41 -0700522
Paul Stewartac4ac002011-08-26 12:04:26 -0700523 // Property getters reserved for subclasses
524 ControlInterface *control_interface() const { return control_interface_; }
Thieu Le3426c8f2012-01-11 17:35:11 -0800525 Metrics *metrics() const { return metrics_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700526 Manager *manager() const { return manager_; }
Paul Stewart036dba02012-08-07 12:34:41 -0700527 const LinkMonitor *link_monitor() const { return link_monitor_.get(); }
528 void set_link_monitor(LinkMonitor *link_monitor);
Peter Qiudc335f82014-05-15 10:33:17 -0700529 // Use for unit test.
530 void set_traffic_monitor(TrafficMonitor *traffic_monitor);
Paul Stewartd5843772011-05-11 15:40:42 -0700531
Paul Stewart75897df2011-04-27 09:05:53 -0700532 private:
Darin Petkov3189a472012-10-05 09:55:33 +0200533 friend class CellularCapabilityTest;
534 friend class CellularTest;
Chris Masone413a3192011-05-09 17:10:05 -0700535 friend class DeviceAdaptorInterface;
Paul Stewart6ff27f52012-07-11 06:51:41 -0700536 friend class DeviceByteCountTest;
Paul Stewartc681fa02012-03-02 19:40:04 -0800537 friend class DevicePortalDetectionTest;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700538 friend class DeviceTest;
Paul Stewartced3ad72013-04-03 13:39:25 -0700539 friend class EthernetTest;
Darin Petkov3189a472012-10-05 09:55:33 +0200540 friend class OpenVPNDriverTest;
Ben Chanbcc6e012013-11-04 14:28:37 -0800541 friend class TestDevice;
mukesh agrawal4a165582013-08-02 15:58:21 -0700542 friend class VirtualDeviceTest;
Paul Stewarte369ece2012-05-22 09:11:03 -0700543 friend class WiFiObjectTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700544
Paul Stewart2bf1d352011-12-06 15:02:55 -0800545 static const char kIPFlagTemplate[];
546 static const char kIPFlagVersion4[];
547 static const char kIPFlagVersion6[];
548 static const char kIPFlagDisableIPv6[];
549 static const char kIPFlagUseTempAddr[];
550 static const char kIPFlagUseTempAddrUsedAndDefault[];
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800551 static const char kIPFlagReversePathFilter[];
552 static const char kIPFlagReversePathFilterEnabled[];
553 static const char kIPFlagReversePathFilterLooseMode[];
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700554 static const char kStoragePowered[];
Paul Stewart6ff27f52012-07-11 06:51:41 -0700555 static const char kStorageReceiveByteCount[];
556 static const char kStorageTransmitByteCount[];
Peter Qiub9256f32014-05-09 15:27:29 -0700557 static const char kFallbackDnsTestHostname[];
558 static const char* kFallbackDnsServers[];
559 static const int kDNSTimeoutMilliseconds;
Chris Masone5dec5f42011-07-22 14:07:55 -0700560
Paul Stewart1062d9d2012-04-27 10:42:27 -0700561 // Configure static IP address parameters if the service provides them.
562 void ConfigureStaticIPTask();
563
Chris Masone5dec5f42011-07-22 14:07:55 -0700564 // Right now, Devices reference IPConfigs directly when persisted to disk
565 // It's not clear that this makes sense long-term, but that's how it is now.
566 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700567 // |suffix| is injected into the storage identifier used for the configs.
568 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700569
Ben Chanbcc6e012013-11-04 14:28:37 -0800570 // Set an IP configuration flag on the device. |family| should be "ipv6" or
571 // "ipv4". |flag| should be the name of the flag to be set and |value| is
572 // what this flag should be set to. Overridden by unit tests to pretend
573 // writing to procfs.
574 virtual bool SetIPFlag(IPAddress::Family family,
575 const std::string &flag,
576 const std::string &value);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800577
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800578 std::vector<std::string> AvailableIPConfigs(Error *error);
Chris Masone4e851612011-07-01 10:46:53 -0700579 std::string GetRpcConnectionIdentifier();
580
Paul Stewart036dba02012-08-07 12:34:41 -0700581 // Get the LinkMonitor's average response time.
Ben Chan7fab8972014-08-10 17:14:46 -0700582 uint64_t GetLinkMonitorResponseTime(Error *error);
Paul Stewart036dba02012-08-07 12:34:41 -0700583
Ben Chanb061f892013-02-27 17:46:55 -0800584 // Get receive and transmit byte counters. These methods simply wrap
585 // GetReceiveByteCount and GetTransmitByteCount in order to be used by
586 // HelpRegisterConstDerivedUint64.
Ben Chan7fab8972014-08-10 17:14:46 -0700587 uint64_t GetReceiveByteCountProperty(Error *error);
588 uint64_t GetTransmitByteCountProperty(Error *error);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700589
Paul Stewartd4f26482014-04-25 19:12:03 -0700590 // Emit a property change signal for the "IPConfigs" property of this device.
591 void UpdateIPConfigsProperty();
592
Peter Qiu6f5618b2014-06-05 15:19:01 -0700593 // Called by DNS server tester when the fallback DNS servers test completes.
Peter Qiud670d032014-06-03 15:04:43 -0700594 void FallbackDNSResultCallback(const DNSServerTester::Status status);
Peter Qiub9256f32014-05-09 15:27:29 -0700595
Peter Qiu6f5618b2014-06-05 15:19:01 -0700596 // Called by DNS server tester when the configured DNS servers test completes.
597 void ConfigDNSResultCallback(const DNSServerTester::Status status);
598
Peter Qiua89154b2014-05-23 15:45:42 -0700599 // Update DNS setting with the given DNS servers for the current connection.
600 void SwitchDNSServers(const std::vector<std::string> &dns_servers);
601
Peter Qiu25f1be62014-08-12 10:42:27 -0700602 // Called when the lifetime for IPv6 DNS server expires.
603 void IPv6DNSServerExpired();
604
Samuel Tanfe734672014-08-07 15:50:48 -0700605 // Modify the internal records of registered wake-on-packet connections
606 void AddWakeOnPacketConnectionInternal(const IPAddress &ip);
607 void RemoveWakeOnPacketConnectionInternal(const IPAddress &ip);
608 void RemoveAllWakeOnPacketConnectionsInternal();
609
Eric Shienbrood9a245532012-03-07 14:20:39 -0500610 // |enabled_persistent_| is the value of the Powered property, as
611 // read from the profile. If it is not found in the profile, it
612 // defaults to true. |enabled_| reflects the real-time state of
613 // the device, i.e., enabled or disabled. |enabled_pending_| reflects
614 // the target state of the device while an enable or disable operation
615 // is occurring.
616 //
617 // Some typical sequences for these state variables are shown below.
618 //
619 // Shill starts up, profile has been read:
620 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=false
621 //
622 // Shill acts on the value of |enabled_persistent_|, calls SetEnabled(true):
623 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=true
624 //
625 // SetEnabled completes successfully, device is enabled:
626 // |enabled_persistent_|=true |enabled_|=true |enabled_pending_|=true
627 //
628 // User presses "Disable" button, SetEnabled(false) is called:
629 // |enabled_persistent_|=false |enabled_|=true |enabled_pending_|=false
630 //
631 // SetEnabled completes successfully, device is disabled:
632 // |enabled_persistent_|=false |enabled_|=false |enabled_pending_|=false
633 bool enabled_;
634 bool enabled_persistent_;
635 bool enabled_pending_;
636
637 // Other properties
Paul Stewartac4ac002011-08-26 12:04:26 -0700638 bool reconnect_;
639 const std::string hardware_address_;
640
641 PropertyStore store_;
642
Paul Stewartac4ac002011-08-26 12:04:26 -0700643 const int interface_index_;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800644 bool running_; // indicates whether the device is actually in operation
Paul Stewartac4ac002011-08-26 12:04:26 -0700645 const std::string link_name_;
646 const std::string unique_id_;
647 ControlInterface *control_interface_;
648 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800649 Metrics *metrics_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700650 Manager *manager_;
651 IPConfigRefPtr ipconfig_;
Paul Stewartd4f26482014-04-25 19:12:03 -0700652 IPConfigRefPtr ip6config_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700653 ConnectionRefPtr connection_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500654 base::WeakPtrFactory<Device> weak_ptr_factory_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700655 scoped_ptr<DeviceAdaptorInterface> adaptor_;
Paul Stewart20088d82012-02-16 06:58:55 -0800656 scoped_ptr<PortalDetector> portal_detector_;
Paul Stewart036dba02012-08-07 12:34:41 -0700657 scoped_ptr<LinkMonitor> link_monitor_;
Peter Qiu6f5618b2014-06-05 15:19:01 -0700658 // Used for verifying whether DNS server is functional.
659 scoped_ptr<DNSServerTester> dns_server_tester_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500660 base::Callback<void(const PortalDetector::Result &)>
661 portal_detector_callback_;
Peter Qiu25f1be62014-08-12 10:42:27 -0700662 // Callback to invoke when IPv6 DNS servers lifetime expired.
663 base::CancelableClosure ipv6_dns_server_expired_callback_;
Peter Qiudc335f82014-05-15 10:33:17 -0700664 scoped_ptr<TrafficMonitor> traffic_monitor_;
Peter Qiu6f5618b2014-06-05 15:19:01 -0700665 // DNS servers obtained from ipconfig (either from DHCP or static config)
666 // that are not working.
667 std::vector<std::string> config_dns_servers_;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800668 Technology::Identifier technology_;
Thieu Le85e050b2012-03-13 15:04:38 -0700669 // The number of portal detection attempts from Connected to Online state.
670 // This includes all failure/timeout attempts and the final successful
671 // attempt.
672 int portal_attempts_to_online_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700673
Paul Stewart6ff27f52012-07-11 06:51:41 -0700674 // Keep track of the offset between the interface-reported byte counters
675 // and our persisted value.
Ben Chan7fab8972014-08-10 17:14:46 -0700676 uint64_t receive_byte_offset_;
677 uint64_t transmit_byte_offset_;
Paul Stewart6ff27f52012-07-11 06:51:41 -0700678
Paul Stewart03dba0b2011-08-22 16:32:45 -0700679 // Maintain a reference to the connected / connecting service
680 ServiceRefPtr selected_service_;
681
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700682 // Cache singleton pointers for performance and test purposes.
Darin Petkov77cb6812011-08-15 16:19:41 -0700683 DHCPProvider *dhcp_provider_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700684 RTNLHandler *rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700685
Samuel Tanfe734672014-08-07 15:50:48 -0700686 // Keep track of IP addresses that device will wake upon receiving packets
687 // from
688 IPAddressStore wake_on_packet_connections_;
689
Chris Masone9be4a9d2011-05-16 15:44:09 -0700690 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700691};
692
Paul Stewart75897df2011-04-27 09:05:53 -0700693} // namespace shill
694
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200695#endif // SHILL_DEVICE_H_