-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.h
550 lines (463 loc) · 23.7 KB
/
device.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// Copyright 2018 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SHILL_DEVICE_H_
#define SHILL_DEVICE_H_
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <base/memory/ref_counted.h>
#include <base/memory/weak_ptr.h>
#include <base/time/time.h>
#include <chromeos/patchpanel/dbus/client.h>
#include <gtest/gtest_prod.h> // for FRIEND_TEST
#include <net-base/ip_address.h>
#include <net-base/rtnl_handler.h>
#include "shill/adaptor_interfaces.h"
#include "shill/callbacks.h"
#include "shill/connection_diagnostics.h"
#include "shill/event_dispatcher.h"
#include "shill/geolocation_info.h"
#include "shill/ipconfig.h"
#include "shill/metrics.h"
#include "shill/network/dhcp_controller.h"
#include "shill/network/network.h"
#include "shill/portal_detector.h"
#include "shill/refptr_types.h"
#include "shill/service.h"
#include "shill/store/property_store.h"
#include "shill/technology.h"
namespace shill {
class ControlInterface;
class DHCPProvider;
class DeviceAdaptorInterface;
class Error;
class EventDispatcher;
class Manager;
// Device superclass. Individual network interfaces types will inherit from
// this class.
class Device : public base::RefCounted<Device>, public Network::EventHandler {
public:
// A constructor for the Device object
Device(Manager* manager,
const std::string& link_name,
const std::string& mac_address,
int interface_index,
Technology technology,
bool fixed_ip_params = false);
Device(const Device&) = delete;
Device& operator=(const Device&) = delete;
// Initialize type-specific network interface properties.
virtual void Initialize();
// Enable or disable the device. This is a convenience method for
// cases where we want to SetEnabledNonPersistent, but don't care
// about the results.
virtual void SetEnabled(bool enable);
// Enable or disable the device. Unlike SetEnabledPersistent, it does not
// save the setting in the profile.
//
// TODO(quiche): Replace both of the next two methods with calls to
// SetEnabledChecked.
void SetEnabledNonPersistent(bool enable, ResultCallback callback);
// Enable or disable the device, and save the setting in the profile.
// The setting is persisted before the enable or disable operation
// starts, so that even if it fails, the user's intent is still recorded
// for the next time shill restarts.
void SetEnabledPersistent(bool enable, ResultCallback callback);
// Enable or disable the Device, depending on |enable|.
// Save the new setting to the profile, if |persist| is true.
// Report synchronous errors using |error|, and asynchronous completion
// with |callback|.
mockable void SetEnabledChecked(bool enable,
bool persist,
ResultCallback callback);
// Similar to SetEnabledChecked, but without coherence checking, and
// without saving the new value of |enable| to the profile. If you
// are rational (i.e. not Cellular), you should use
// SetEnabledChecked instead.
void SetEnabledUnchecked(bool enable, ResultCallback callback);
// Returns true if the underlying device reports that it is already enabled.
// Used when the device is registered with the Manager, so that shill can
// sync its state/ with the true state of the device. The default is to
// report false.
virtual bool IsUnderlyingDeviceEnabled() const;
virtual void LinkEvent(unsigned flags, unsigned change);
// The default implementation sets |error| to kNotSupported.
virtual void Scan(Error* error, const std::string& reason, bool is_dbus_call);
virtual void RegisterOnNetwork(const std::string& network_id,
ResultCallback callback);
virtual void RequirePin(const std::string& pin,
bool require,
ResultCallback callback);
virtual void EnterPin(const std::string& pin, ResultCallback callback);
virtual void UnblockPin(const std::string& unblock_code,
const std::string& pin,
ResultCallback callback);
virtual void ChangePin(const std::string& old_pin,
const std::string& new_pin,
ResultCallback callback);
virtual void Reset(ResultCallback callback);
// Returns true if the selected service on the device (if any) is connected.
// Returns false if there is no selected service, or if the selected service
// is not connected.
bool IsConnected() const;
// Called by Device so that subclasses can run hooks on the selected service
// getting an IP. Subclasses should call up to the parent first.
virtual void OnConnected();
// Returns true if the selected service on the device (if any) is connected
// and matches the passed-in argument |service|. Returns false if there is
// no connected service, or if it does not match |service|.
mockable bool IsConnectedToService(const ServiceRefPtr& service) const;
// Called by Device so that subclasses can run hooks on the selected service
// changed. This function is called after the |selected_service_| changed so
// the subclasses can call the getter to retrieve the new selected service.
// Note that the base class does nothing here so the subclasses don't need to
// call up to the parent.
virtual void OnSelectedServiceChanged(const ServiceRefPtr& old_service);
// Initiate or restart network validation if all the following conditions are
// met:
// - There is currently a |selected_service_| for this Device.
// - The Device has an active Network connection and |selected_service_| is
// in a connected state.
// - Network validation is not disabled (Service::IsPortalDetectioDisabled):
// - The network is not a network defined by policies.
// - Network validation is enabled for the |selected_service_| itself or
// for its link technology.
// - Network validation was not already running. If |reason| requires that
// network validation should be restarted, this check is ignored. For
// details see Network::StartPortalDetection.
// If the Service is connected and network validation should not be running,
// it is stopped and the connection state of the Service is set to "online".
mockable bool UpdatePortalDetector(Network::ValidationReason reason);
const RpcIdentifier& GetRpcIdentifier() const;
std::string GetStorageIdentifier() const;
// Update the Geolocation objects. Each object is multiple
// key-value pairs representing one entity that can be used for
// Geolocation.
virtual void UpdateGeolocationObjects(
std::vector<GeolocationInfo>* geolocation_infos) const;
const std::string& mac_address() const { return mac_address_; }
const std::string& link_name() const { return link_name_; }
int interface_index() const { return interface_index_; }
bool enabled() const { return enabled_; }
bool enabled_persistent() const { return enabled_persistent_; }
mockable Technology technology() const { return technology_; }
std::string GetTechnologyName() const;
// In WiFi, Ethernet and all other device types except for Cellular, this
// method is guaranteed to return always a valid Network, so it is safe to
// dereference the returned value.
//
// In Cellular devices, where ephemeral multiplexed network interfaces are
// supported, this method is not guaranteed to always return a valid Network.
// The Network lifecycle will be bound to the connection state of the device,
// and therefore this method will return nullptr when disconnected.
virtual Network* GetPrimaryNetwork() const;
// Returns a string that is guaranteed to uniquely identify this Device
// instance.
const std::string& UniqueName() const;
// Returns a WeakPtr of the Device.
base::WeakPtr<Device> AsWeakPtr() { return weak_ptr_factory_.GetWeakPtr(); }
PropertyStore* mutable_store() { return &store_; }
const PropertyStore& store() const { return store_; }
net_base::RTNLHandler* rtnl_handler() { return rtnl_handler_; }
EventDispatcher* dispatcher() const;
// Load configuration for the device from |storage|. This may include
// instantiating non-visible services for which configuration has been
// stored.
virtual bool Load(const StoreInterface* storage);
// Save configuration for the device to |storage|.
virtual bool Save(StoreInterface* storage);
DeviceAdaptorInterface* adaptor() const { return adaptor_.get(); }
// Suspend event handler. Called by Manager before the system
// suspends. This handler, along with any other suspend handlers,
// will have Manager::kTerminationActionsTimeoutMilliseconds to
// execute before the system enters the suspend state. |callback|
// must be invoked after all synchronous and/or asynchronous actions
// this function performs complete. Code that needs to run on exit should use
// Manager::AddTerminationAction, rather than OnBeforeSuspend.
//
// The default implementation invokes the |callback| immediately, since
// there is nothing to be done in the general case.
virtual void OnBeforeSuspend(ResultCallback callback);
// Resume event handler. Called by Manager as the system resumes.
// The base class implementation takes care of renewing a DHCP lease
// (if necessary). Derived classes may implement any technology
// specific requirements by overriding, but should include a call to
// the base class implementation.
virtual void OnAfterResume();
// This method is invoked when the system resumes from suspend temporarily in
// the "dark resume" state. The system will reenter suspend in
// Manager::kTerminationActionsTimeoutMilliseconds. |callback| must be invoked
// after all synchronous and/or asynchronous actions this function performs
// and/or posts complete.
//
// The default implementation invokes the |callback| immediately, since
// there is nothing to be done in the general case.
virtual void OnDarkResume(ResultCallback callback);
// Sets MAC address source for USB Ethernet device.
virtual void SetUsbEthernetMacAddressSource(const std::string& source,
ResultCallback callback);
// Renew DHCPv4 lease and invalidate the IPv6 config kept in shill.
void ForceIPConfigUpdate();
// Creates a byte vector from a colon-separated hardware address string.
static std::vector<uint8_t> MakeHardwareAddressFromString(
const std::string& address_string);
// Creates a colon-separated hardware address string from a byte vector.
static std::string MakeStringFromHardwareAddress(
const std::vector<uint8_t>& address_data);
// Request the WiFi device to roam to AP with |addr|.
// This call will send Roam command to wpa_supplicant.
virtual bool RequestRoam(const std::string& addr, Error* error);
const ServiceRefPtr& selected_service() const { return selected_service_; }
// Responds to a neighbor reachability event from patchpanel. The base class
// does nothing here so the derived class doesn't need to call this.
void OnNeighborReachabilityEvent(
int interface_index,
const net_base::IPAddress& ip_address,
patchpanel::Client::NeighborRole role,
patchpanel::Client::NeighborStatus status) override;
// Returns a string formatted as "$ifname $service_log_name", or
// "$ifname no_service" if |selected_service_| is currently not defined.
std::string LoggingTag() const;
// Overrides for Network::EventHandler. See the comments for
// Network::EventHandler for more details.
void OnConnectionUpdated(int interface_index) override;
void OnNetworkStopped(int interface_index, bool is_failure) override;
// Emit a property change signal for the "IPConfigs" property of this device.
void OnIPConfigsPropertyUpdated(int interface_index) override;
// Derived class should implement this function to listen to this event. Base
// class does nothing.
void OnGetDHCPLease(int interface_index) override;
// Derived class should implement this function to listen to this event. Base
// class does nothing.
void OnGetDHCPFailure(int interface_index) override;
// Derived class should implement this function to listen to this event. Base
// class does nothing.
void OnGetSLAACAddress(int interface_index) override;
// Derived class should implement this function to listen to this event. Base
// class does nothing.
void OnNetworkValidationStart(int interface_index) override;
// Derived class should implement this function to listen to this event. Base
// class does nothing.
void OnNetworkValidationStop(int interface_index) override;
void OnNetworkValidationResult(int interface_index,
const PortalDetector::Result& result) override;
void OnNetworkDestroyed(int interface_index) override;
void set_enabled_for_testing(bool enabled) { enabled_ = enabled; }
void set_selected_service_for_testing(ServiceRefPtr service) {
selected_service_ = service;
}
void set_network_for_testing(std::unique_ptr<Network> network) {
implicit_network_ = std::move(network);
}
protected:
friend class base::RefCounted<Device>;
FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
FRIEND_TEST(CellularTest, DefaultLinkDeleted);
FRIEND_TEST(DeviceTest, AvailableIPConfigs);
FRIEND_TEST(DeviceTest, FetchTrafficCounters);
FRIEND_TEST(DeviceTest, GetProperties);
FRIEND_TEST(DeviceTest, Load);
FRIEND_TEST(DeviceTest, Save);
FRIEND_TEST(DeviceTest, SelectedService);
FRIEND_TEST(DeviceTest, SetEnabledNonPersistent);
FRIEND_TEST(DeviceTest, SetEnabledPersistent);
FRIEND_TEST(DeviceTest, Start);
FRIEND_TEST(DeviceTest, StartFailure);
FRIEND_TEST(DeviceTest, StartProhibited);
FRIEND_TEST(DeviceTest, Stop);
FRIEND_TEST(DeviceTest, StopWithFixedIpParams);
FRIEND_TEST(DeviceTest, StopWithNetworkInterfaceDisabledAfterward);
FRIEND_TEST(ManagerTest, ConnectedTechnologies);
FRIEND_TEST(ManagerTest, DefaultTechnology);
FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
FRIEND_TEST(ManagerTest, GetEnabledDeviceWithTechnology);
FRIEND_TEST(ManagerTest, ConnectToMostSecureWiFi);
FRIEND_TEST(ManagerTest, RefreshAllTrafficCountersTask);
FRIEND_TEST(ManagerTest, SetEnabledStateForTechnology);
FRIEND_TEST(MetricsTest, WiFiServicePostReadySameBSSIDLB);
FRIEND_TEST(MetricsTest, WiFiServicePostReadySameBSSIDHB);
FRIEND_TEST(MetricsTest, WiFiServicePostReadySameBSSIDUHB);
FRIEND_TEST(MetricsTest, WiFiServicePostReadySameBSSIDUndef);
FRIEND_TEST(VirtualDeviceTest, ResetConnection);
virtual ~Device();
// Each device must implement this method to do the work needed to
// enable the device to operate for establishing network connections.
virtual void Start(EnabledStateChangedCallback callback) = 0;
// Each device must implement this method to do the work needed to
// disable the device, i.e., clear any running state, and make the
// device no longer capable of establishing network connections.
virtual void Stop(EnabledStateChangedCallback callback) = 0;
// Returns true if the associated network interface should be brought down
// after the device is disabled, or false if that should be done before the
// device is disabled.
virtual bool ShouldBringNetworkInterfaceDownAfterDisabled() const;
// The EnabledStateChangedCallback that gets passed to the device's
// Start() and Stop() methods is bound to this method. |callback|
// is the callback that was passed to SetEnabled().
void OnEnabledStateChanged(ResultCallback callback, const Error& error);
// Update the device state to the pending state.
void UpdateEnabledState();
// Create the implicit Network object. Must be reimplemented by classes that
// don't require it (e.g. Cellular) so that it's a no-op.
virtual void CreateImplicitNetwork(bool fixed_ip_params);
// Drops the currently selected service along with its IP configuration and
// implicit Network connection, if any. Must be reimplemented by classes (e.g.
// Cellular) that don't require the implicit network.
virtual void DropConnection();
// Brings the network interface associated to the implicit Network down. Must
/// be reimplemented by classes (e.g. Cellular) that don't require the
// implicit network.
virtual void BringNetworkInterfaceDown();
// Called when a PortalDetector trial completes.
// Called every time PortalDetector finishes and Internet connectivity is
// validated.
virtual void OnNetworkValidationSuccess();
// Called every time PortalDetector finishes and Internet connectivity is not
// validated. In that case a new validation attempt is scheduled to run at a
// later time.
virtual void OnNetworkValidationFailure();
// This is currently only used in the WiFi child class for triggering
// WakeOnWiFi::OnConnectedAndReachable().
void OnIPv4ConfiguredWithDHCPLease(int interface_index) override;
// This is currently only used in the WiFi child class for triggering
// WakeOnWiFi::OnConnectedAndReachable().
void OnIPv6ConfiguredWithSLAACAddress(int interface_index) override;
// Called by Device so that subclasses can run hooks on the selected service
// failing to get an IP. The default implementation disconnects the selected
// service with Service::kFailureDHCP.
virtual void OnIPConfigFailure();
// Check if the interface index provided corresponds to the index of the
// network interface associated to the primary network. Network events
// reported in other interfaces will be ignored by the Device class.
bool IsEventOnPrimaryNetwork(int interface_index);
// Selects a service to be "current" -- i.e. link-state or configuration
// events that happen to the device are attributed to this service. Also reset
// old service state to Idle if its current state is not Failure and
// |reset_old_service_state| is true.
void SelectService(const ServiceRefPtr& service,
bool reset_old_service_state = true);
// Reset the Network currently used in the selected service by reloading the
// one considered primary. This will typically be run during SelectService()
// but may also happen if technologies silently change the Network used
// without performing service selection.
void ResetServiceAttachedNetwork();
// Set the state of the |selected_service_|.
virtual void SetServiceState(Service::ConnectState state);
// Set the failure of the selected service (implicitly sets the state to
// "failure").
virtual void SetServiceFailure(Service::ConnectFailure failure_state);
// Records the failure mode and time of the selected service, and
// sets the Service state of the selected service to "Idle".
// Avoids showing a failure mole in the UI.
virtual void SetServiceFailureSilent(Service::ConnectFailure failure_state);
void HelpRegisterConstDerivedString(std::string_view name,
std::string (Device::*get)(Error*));
void HelpRegisterConstDerivedRpcIdentifier(
std::string_view name, RpcIdentifier (Device::*get)(Error*));
void HelpRegisterConstDerivedRpcIdentifiers(
std::string_view name, RpcIdentifiers (Device::*get)(Error*));
void HelpRegisterConstDerivedUint64(std::string_view name,
uint64_t (Device::*get)(Error*));
// By default StorageId is equal to: "device_" + DeviceStorageSuffix()
// where the latter returns MAC address. This can be overridden in
// subclasses.
virtual std::string DeviceStorageSuffix() const { return mac_address(); }
// Property getters reserved for subclasses
ControlInterface* control_interface() const;
bool enabled_pending() const { return enabled_pending_; }
Metrics* metrics() const;
Manager* manager() const { return manager_; }
virtual void set_mac_address(const std::string& mac_address);
// Emit a given MAC Address via dbus. If empty or bad string is provided,
// emit the hardware MAC address of the device.
void EmitMACAddress(const std::string& mac_address = std::string());
private:
friend class CellularTest;
friend class DeviceAdaptorInterface;
friend class DeviceByteCountTest;
friend class DevicePortalDetectionTest;
friend class DeviceTest;
friend class DevicePortalDetectorTest;
friend class EthernetTest;
friend class OpenVPNDriverTest;
friend class TestDevice;
friend class VirtualDeviceTest;
friend class WiFiObjectTest;
static const char kStoragePowered[];
RpcIdentifier GetSelectedServiceRpcIdentifier(Error* error);
RpcIdentifiers AvailableIPConfigs(Error* error);
// Atomically update the counters of the old service and the snapshot of the
// new service. |GetTrafficCountersPatchpanelCallback| calls
// |GetTrafficCountersCallback| using the |get_traffic_counters_callback_|
// callback below. This is necessary because the callback that holds a
// reference to the ServiceRefPtrs needs to be reset to release the
// references. We can't directly cancel the callback we give to patchpanel
// client since it expects a OnceCallback.
void GetTrafficCountersCallback(
const ServiceRefPtr& old_service,
const ServiceRefPtr& new_service,
const std::vector<patchpanel::Client::TrafficCounter>& counters);
void GetTrafficCountersPatchpanelCallback(
unsigned int id,
const std::vector<patchpanel::Client::TrafficCounter>& counters);
// Asynchronously get all the traffic counters for this device during a
// selected_service_ change and update the counters and snapshots for the old
// and new selected_service_ respectively.
void FetchTrafficCounters(const ServiceRefPtr& old_service,
const ServiceRefPtr& new_service);
// Necessary getter signature for kTypeProperty. Cannot be const.
std::string GetTechnologyString(Error* error);
// |enabled_persistent_| is the value of the Powered property, as
// read from the profile. If it is not found in the profile, it
// defaults to true. |enabled_| reflects the real-time state of
// the device, i.e., enabled or disabled. |enabled_pending_| reflects
// the target state of the device while an enable or disable operation
// is occurring.
//
// Some typical sequences for these state variables are shown below.
//
// Shill starts up, profile has been read:
// |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=false
//
// Shill acts on the value of |enabled_persistent_|, calls SetEnabled(true):
// |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=true
//
// SetEnabled completes successfully, device is enabled:
// |enabled_persistent_|=true |enabled_|=true |enabled_pending_|=true
//
// User presses "Disable" button, SetEnabled(false) is called:
// |enabled_persistent_|=false |enabled_|=true |enabled_pending_|=false
//
// SetEnabled completes successfully, device is disabled:
// |enabled_persistent_|=false |enabled_|=false |enabled_pending_|=false
bool enabled_;
bool enabled_persistent_;
bool enabled_pending_;
std::string mac_address_;
PropertyStore store_;
const int interface_index_;
const std::string link_name_;
Manager* manager_;
std::unique_ptr<Network> implicit_network_;
std::unique_ptr<DeviceAdaptorInterface> adaptor_;
Technology technology_;
// Maintain a reference to the connected / connecting service
ServiceRefPtr selected_service_;
// Cache singleton pointers for performance and test purposes.
net_base::RTNLHandler* rtnl_handler_;
// See GetTrafficCountersCallback.
unsigned int traffic_counter_callback_id_;
// Maps the callback ID, created when FetchTrafficCounters is called, to the
// corresponding callback.
std::map<unsigned int,
base::OnceCallback<void(
const std::vector<patchpanel::Client::TrafficCounter>&)>>
traffic_counters_callback_map_;
base::WeakPtrFactory<Device> weak_ptr_factory_;
};
} // namespace shill
#endif // SHILL_DEVICE_H_