blob: a02f39aa5fcfe0da13f51ff0963fac3558b176f6 [file] [log] [blame]
Kevin Cernekee95d4ae92016-06-19 10:26:29 -07001// Copyright 2016 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Garrick Evans3388a032020-03-24 11:25:55 +09005#include "patchpanel/shill_client.h"
Kevin Cernekee95d4ae92016-06-19 10:26:29 -07006
Jie Jiang850a4712020-04-08 21:06:36 +09007#include <utility>
Garrick Evans49879532018-12-03 13:15:36 +09008#include <vector>
9
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070010#include <base/bind.h>
11#include <base/logging.h>
Garrick Evanse87bb4f2020-02-18 10:27:37 +090012#include <base/strings/string_util.h>
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070013#include <chromeos/dbus/service_constants.h>
14
Garrick Evans3388a032020-03-24 11:25:55 +090015namespace patchpanel {
Hugo Benichid17aa592019-04-26 15:15:01 +090016
Jie Jiang48c99ce2020-06-08 15:18:23 +090017namespace {
18
19ShillClient::Device::Type ParseDeviceType(const std::string& type_str) {
20 static const std::map<std::string, ShillClient::Device::Type> str2enum{
21 {shill::kTypeCellular, ShillClient::Device::Type::kCellular},
22 {shill::kTypeEthernet, ShillClient::Device::Type::kEthernet},
23 {shill::kTypeEthernetEap, ShillClient::Device::Type::kEthernetEap},
24 {shill::kTypeGuestInterface, ShillClient::Device::Type::kGuestInterface},
25 {shill::kTypeLoopback, ShillClient::Device::Type::kLoopback},
26 {shill::kTypePPP, ShillClient::Device::Type::kPPP},
27 {shill::kTypePPPoE, ShillClient::Device::Type::kPPPoE},
28 {shill::kTypeTunnel, ShillClient::Device::Type::kTunnel},
29 {shill::kTypeWifi, ShillClient::Device::Type::kWifi},
30 {shill::kTypeVPN, ShillClient::Device::Type::kVPN},
31 };
32
33 const auto it = str2enum.find(type_str);
34 return it != str2enum.end() ? it->second
35 : ShillClient::Device::Type::kUnknown;
36}
37
38} // namespace
39
Garrick Evans08843932019-09-17 14:41:08 +090040ShillClient::ShillClient(const scoped_refptr<dbus::Bus>& bus) : bus_(bus) {
Hidehiko Abe3a7e5132018-02-15 13:07:50 +090041 manager_proxy_.reset(new org::chromium::flimflam::ManagerProxy(bus_));
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070042 manager_proxy_->RegisterPropertyChangedSignalHandler(
43 base::Bind(&ShillClient::OnManagerPropertyChange,
44 weak_factory_.GetWeakPtr()),
45 base::Bind(&ShillClient::OnManagerPropertyChangeRegistration,
46 weak_factory_.GetWeakPtr()));
47}
48
Garrick Evans1b1f67c2020-02-04 16:21:25 +090049const std::string& ShillClient::default_interface() const {
50 return default_interface_;
51}
52
Hugo Benichicc6850f2020-01-17 13:26:06 +090053const std::set<std::string> ShillClient::get_devices() const {
54 return devices_;
55}
56
57bool ShillClient::has_device(const std::string& ifname) const {
58 return devices_.find(ifname) != devices_.end();
59}
60
Jie Jiang84c76a12020-04-17 16:45:20 +090061void ShillClient::ScanDevices() {
Garrick Evans49879532018-12-03 13:15:36 +090062 brillo::VariantDictionary props;
63 if (!manager_proxy_->GetProperties(&props, nullptr)) {
64 LOG(ERROR) << "Unable to get manager properties";
65 return;
66 }
67 const auto it = props.find(shill::kDevicesProperty);
68 if (it == props.end()) {
69 LOG(WARNING) << "Manager properties is missing devices";
70 return;
71 }
Garrick Evans139708f2020-02-06 14:38:59 +090072 UpdateDevices(it->second);
Garrick Evans49879532018-12-03 13:15:36 +090073}
74
Hugo Benichiddee2812019-05-10 16:03:43 +090075std::string ShillClient::GetDefaultInterface() {
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070076 brillo::VariantDictionary manager_props;
77
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070078 if (!manager_proxy_->GetProperties(&manager_props, nullptr)) {
79 LOG(ERROR) << "Unable to get manager properties";
Hugo Benichiddee2812019-05-10 16:03:43 +090080 return "";
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070081 }
82
83 auto it = manager_props.find(shill::kDefaultServiceProperty);
84 if (it == manager_props.end()) {
85 LOG(WARNING) << "Manager properties is missing default service";
Hugo Benichiddee2812019-05-10 16:03:43 +090086 return "";
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070087 }
88
89 dbus::ObjectPath service_path = it->second.TryGet<dbus::ObjectPath>();
90 if (!service_path.IsValid() || service_path.value() == "/") {
Hugo Benichiddee2812019-05-10 16:03:43 +090091 return "";
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070092 }
93
94 org::chromium::flimflam::ServiceProxy service_proxy(bus_, service_path);
95 brillo::VariantDictionary service_props;
96 if (!service_proxy.GetProperties(&service_props, nullptr)) {
97 LOG(ERROR) << "Can't retrieve properties for service";
Hugo Benichiddee2812019-05-10 16:03:43 +090098 return "";
Kevin Cernekee95d4ae92016-06-19 10:26:29 -070099 }
100
Alex Khouderchah05a8b5b2019-11-20 12:18:51 -0800101 it = service_props.find(shill::kIsConnectedProperty);
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700102 if (it == service_props.end()) {
Alex Khouderchah05a8b5b2019-11-20 12:18:51 -0800103 LOG(WARNING) << "Service properties is missing \"IsConnected\"";
Hugo Benichiddee2812019-05-10 16:03:43 +0900104 return "";
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700105 }
Alex Khouderchah05a8b5b2019-11-20 12:18:51 -0800106 if (!it->second.TryGet<bool>()) {
107 LOG(INFO) << "Ignoring non-connected service";
Hugo Benichiddee2812019-05-10 16:03:43 +0900108 return "";
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700109 }
110
111 it = service_props.find(shill::kDeviceProperty);
112 if (it == service_props.end()) {
113 LOG(WARNING) << "Service properties is missing device path";
Hugo Benichiddee2812019-05-10 16:03:43 +0900114 return "";
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700115 }
116
117 dbus::ObjectPath device_path = it->second.TryGet<dbus::ObjectPath>();
118 if (!device_path.IsValid()) {
119 LOG(WARNING) << "Invalid device path";
Hugo Benichiddee2812019-05-10 16:03:43 +0900120 return "";
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700121 }
122
123 org::chromium::flimflam::DeviceProxy device_proxy(bus_, device_path);
124 brillo::VariantDictionary device_props;
125 if (!device_proxy.GetProperties(&device_props, nullptr)) {
126 LOG(ERROR) << "Can't retrieve properties for device";
Hugo Benichiddee2812019-05-10 16:03:43 +0900127 return "";
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700128 }
129
130 it = device_props.find(shill::kInterfaceProperty);
131 if (it == device_props.end()) {
132 LOG(WARNING) << "Device properties is missing interface name";
Hugo Benichiddee2812019-05-10 16:03:43 +0900133 return "";
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700134 }
135
136 std::string interface = it->second.TryGet<std::string>();
137 if (interface.empty()) {
138 LOG(WARNING) << "Device interface name is empty";
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700139 }
140
Hugo Benichiddee2812019-05-10 16:03:43 +0900141 return interface;
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700142}
143
144void ShillClient::OnManagerPropertyChangeRegistration(
145 const std::string& interface,
146 const std::string& signal_name,
147 bool success) {
148 if (!success)
149 LOG(FATAL) << "Unable to register for interface change events";
150}
151
152void ShillClient::OnManagerPropertyChange(const std::string& property_name,
153 const brillo::Any& property_value) {
Hugo Benichiddee2812019-05-10 16:03:43 +0900154 if (property_name == shill::kDevicesProperty) {
Garrick Evans139708f2020-02-06 14:38:59 +0900155 UpdateDevices(property_value);
Hugo Benichiddee2812019-05-10 16:03:43 +0900156
157 // Choose a fallback interface when any network device exist. Update the
158 // fallback interface if it that device does not exist anymore.
Garrick Evansd4742832019-05-20 11:51:40 +0900159 if (!devices_.empty() &&
160 devices_.find(fallback_default_interface_) == devices_.end()) {
Hugo Benichiddee2812019-05-10 16:03:43 +0900161 fallback_default_interface_ = *devices_.begin();
162 // When the system appears to have no default interface, use the fallback
163 // interface instead.
Garrick Evansd4742832019-05-20 11:51:40 +0900164 if (default_interface_.empty() ||
165 default_interface_ != fallback_default_interface_)
Hugo Benichiddee2812019-05-10 16:03:43 +0900166 SetDefaultInterface(fallback_default_interface_);
167 }
168
Hugo Benichiddee2812019-05-10 16:03:43 +0900169 // Remove the fallback interface when no network device is managed by shill.
170 if (!fallback_default_interface_.empty() && devices_.empty()) {
171 fallback_default_interface_ = "";
172 SetDefaultInterface("");
173 }
174
Garrick Evans49879532018-12-03 13:15:36 +0900175 return;
176 }
177
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700178 if (property_name != shill::kDefaultServiceProperty &&
179 property_name != shill::kConnectionStateProperty)
180 return;
181
Hugo Benichiddee2812019-05-10 16:03:43 +0900182 SetDefaultInterface(GetDefaultInterface());
183}
184
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900185std::string ShillClient::SetDefaultInterface(std::string new_default) {
Hugo Benichiddee2812019-05-10 16:03:43 +0900186 // When the system default is lost, use the fallback interface instead.
187 if (new_default.empty())
188 new_default = fallback_default_interface_;
189
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700190 if (default_interface_ == new_default)
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900191 return default_interface_;
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700192
Garrick Evans139708f2020-02-06 14:38:59 +0900193 LOG(INFO) << "Default interface changed from [" << default_interface_
194 << "] to [" << new_default << "]";
195
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900196 const std::string prev_default = default_interface_;
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700197 default_interface_ = new_default;
Garrick Evans139708f2020-02-06 14:38:59 +0900198 for (const auto& h : default_interface_handlers_) {
199 if (!h.is_null())
200 h.Run(default_interface_, prev_default);
Garrick Evansc7fea0a2020-02-04 10:46:42 +0900201 }
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900202 return prev_default;
Kevin Cernekee95d4ae92016-06-19 10:26:29 -0700203}
204
205void ShillClient::RegisterDefaultInterfaceChangedHandler(
Garrick Evans139708f2020-02-06 14:38:59 +0900206 const DefaultInterfaceChangeHandler& handler) {
207 default_interface_handlers_.emplace_back(handler);
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900208 const auto prev_default = SetDefaultInterface(GetDefaultInterface());
Garrick Evans139708f2020-02-06 14:38:59 +0900209 handler.Run(default_interface_, prev_default);
Garrick Evans49879532018-12-03 13:15:36 +0900210}
211
212void ShillClient::RegisterDevicesChangedHandler(
Garrick Evans139708f2020-02-06 14:38:59 +0900213 const DevicesChangeHandler& handler) {
214 device_handlers_.emplace_back(handler);
Garrick Evans49879532018-12-03 13:15:36 +0900215}
216
Jie Jiang850a4712020-04-08 21:06:36 +0900217void ShillClient::RegisterIPConfigsChangedHandler(
218 const IPConfigsChangeHandler& handler) {
219 ipconfigs_handlers_.emplace_back(handler);
220}
221
Garrick Evans139708f2020-02-06 14:38:59 +0900222void ShillClient::UpdateDevices(const brillo::Any& property_value) {
223 std::set<std::string> new_devices, added, removed;
224 for (const auto& path :
225 property_value.TryGet<std::vector<dbus::ObjectPath>>()) {
226 std::string device = path.value();
227 // Strip "/device/" prefix.
228 device = device.substr(device.find_last_of('/') + 1);
Garrick Evanse87bb4f2020-02-18 10:27:37 +0900229
Garrick Evans139708f2020-02-06 14:38:59 +0900230 new_devices.emplace(device);
231 if (devices_.find(device) == devices_.end())
232 added.insert(device);
Jie Jiang850a4712020-04-08 21:06:36 +0900233
234 // Registers handler if we see this device for the first time.
235 if (known_device_paths_.insert(std::make_pair(device, path)).second) {
236 org::chromium::flimflam::DeviceProxy proxy(bus_, path);
237 proxy.RegisterPropertyChangedSignalHandler(
238 base::Bind(&ShillClient::OnDevicePropertyChange,
239 weak_factory_.GetWeakPtr(), device),
240 base::Bind(&ShillClient::OnDevicePropertyChangeRegistration,
241 weak_factory_.GetWeakPtr()));
Jie Jiang01c1a2e2020-04-08 20:58:30 +0900242 known_device_paths_[device] = path;
Jie Jiang850a4712020-04-08 21:06:36 +0900243 }
Garrick Evans139708f2020-02-06 14:38:59 +0900244 }
245
246 for (const auto& d : devices_) {
247 if (new_devices.find(d) == new_devices.end())
248 removed.insert(d);
249 }
250
251 devices_ = new_devices;
252
253 for (const auto& h : device_handlers_)
254 h.Run(added, removed);
Garrick Evans49879532018-12-03 13:15:36 +0900255}
256
Jie Jiang850a4712020-04-08 21:06:36 +0900257ShillClient::IPConfig ShillClient::ParseIPConfigsProperty(
258 const std::string& device, const brillo::Any& property_value) {
259 IPConfig ipconfig;
260 for (const auto& path :
261 property_value.TryGet<std::vector<dbus::ObjectPath>>()) {
262 std::unique_ptr<org::chromium::flimflam::IPConfigProxy> ipconfig_proxy(
263 new org::chromium::flimflam::IPConfigProxy(bus_, path));
264 brillo::VariantDictionary ipconfig_props;
265
266 if (!ipconfig_proxy->GetProperties(&ipconfig_props, nullptr)) {
267 // It is possible that an IPConfig object is removed after we know its
268 // path, especially when the interface is going down.
269 LOG(WARNING) << "[" << device << "]: "
270 << "Unable to get properties for " << path.value();
271 continue;
272 }
273
274 // Detects the type of IPConfig. For ipv4 and ipv6 configurations, there
275 // should be at most one for each type.
276 auto it = ipconfig_props.find(shill::kMethodProperty);
277 if (it == ipconfig_props.end()) {
278 LOG(WARNING) << "[" << device << "]: "
279 << "IPConfig properties is missing Method";
280 continue;
281 }
282 const std::string& method = it->second.TryGet<std::string>();
283 const bool is_ipv4_type =
284 (method == shill::kTypeIPv4 || method == shill::kTypeDHCP ||
285 method == shill::kTypeBOOTP || method == shill::kTypeZeroConf);
286 const bool is_ipv6_type = (method == shill::kTypeIPv6);
287 if (!is_ipv4_type && !is_ipv6_type) {
288 LOG(WARNING) << "[" << device << "]: "
289 << "unknown type \"" << method << "\" for " << path.value();
290 continue;
291 }
292 if ((is_ipv4_type && !ipconfig.ipv4_address.empty()) ||
293 (is_ipv6_type && !ipconfig.ipv6_address.empty())) {
294 LOG(WARNING) << "[" << device << "]: "
295 << "Duplicated ipconfig for " << method;
296 continue;
297 }
298
299 // Gets the value of address, prefix_length, gateway, and dns_servers.
300 it = ipconfig_props.find(shill::kAddressProperty);
301 if (it == ipconfig_props.end()) {
302 LOG(WARNING) << "[" << device << "]: "
303 << "IPConfig properties is missing Address";
304 continue;
305 }
306 const std::string& address = it->second.TryGet<std::string>();
307
308 it = ipconfig_props.find(shill::kPrefixlenProperty);
309 if (it == ipconfig_props.end()) {
310 LOG(WARNING) << "[" << device << "]: "
311 << "IPConfig properties is missing Prefixlen";
312 continue;
313 }
314 int prefix_length = it->second.TryGet<int>();
315
316 it = ipconfig_props.find(shill::kGatewayProperty);
317 if (it == ipconfig_props.end()) {
318 LOG(WARNING) << "[" << device << "]: "
319 << "IPConfig properties is missing Gateway";
320 continue;
321 }
322 const std::string& gateway = it->second.TryGet<std::string>();
323
324 it = ipconfig_props.find(shill::kNameServersProperty);
325 if (it == ipconfig_props.end()) {
326 LOG(WARNING) << "[" << device << "]: "
327 << "IPConfig properties is missing NameServers";
328 // Shill will emit this property with empty value if it has no dns for
329 // this device, so missing this property indicates an error.
330 continue;
331 }
332 const std::vector<std::string>& dns_addresses =
333 it->second.TryGet<std::vector<std::string>>();
334
335 // Checks if this ipconfig is valid: address, gateway, and prefix_length
336 // should not be empty.
337 if (address.empty() || gateway.empty() || prefix_length == 0) {
338 LOG(WARNING) << "[" << device << "]: "
339 << "Skipped invalid ipconfig: "
340 << "address.length()=" << address.length()
341 << ", gateway.length()=" << gateway.length()
342 << ", prefix_length=" << prefix_length;
343 continue;
344 }
345
346 // Fills the IPConfig struct according to the type.
347 if (is_ipv4_type) {
348 ipconfig.ipv4_prefix_length = prefix_length;
349 ipconfig.ipv4_address = address;
350 ipconfig.ipv4_gateway = gateway;
351 ipconfig.ipv4_dns_addresses = dns_addresses;
352 } else { // is_ipv6_type
353 ipconfig.ipv6_prefix_length = prefix_length;
354 ipconfig.ipv6_address = address;
355 ipconfig.ipv6_gateway = gateway;
356 ipconfig.ipv6_dns_addresses = dns_addresses;
357 }
358 }
359
360 return ipconfig;
361}
362
Jie Jiang01c1a2e2020-04-08 20:58:30 +0900363bool ShillClient::GetDeviceProperties(const std::string& device,
364 Device* output) {
365 DCHECK(output);
366 const auto& device_it = known_device_paths_.find(device);
367 if (device_it == known_device_paths_.end()) {
368 LOG(ERROR) << "Unknown device " << device;
369 return false;
370 }
371
372 org::chromium::flimflam::DeviceProxy proxy(bus_, device_it->second);
373 brillo::VariantDictionary props;
374 if (!proxy.GetProperties(&props, nullptr)) {
375 LOG(WARNING) << "Unable to get device properties for " << device;
376 return false;
377 }
378
379 const auto& type_it = props.find(shill::kTypeProperty);
380 if (type_it == props.end()) {
381 LOG(WARNING) << "Device properties is missing Type for " << device;
382 return false;
383 }
Jie Jiang48c99ce2020-06-08 15:18:23 +0900384 const std::string& type_str = type_it->second.TryGet<std::string>();
385 output->type = ParseDeviceType(type_str);
386 if (output->type == Device::Type::kUnknown)
387 LOG(WARNING) << "Unknown device type " << type_str << " for " << device;
Jie Jiang01c1a2e2020-04-08 20:58:30 +0900388
389 const auto& interface_it = props.find(shill::kInterfaceProperty);
390 if (interface_it == props.end()) {
391 LOG(WARNING) << "Device properties is missing Interface for " << device;
392 return false;
393 }
394 output->ifname = interface_it->second.TryGet<std::string>();
395
396 const auto& ipconfigs_it = props.find(shill::kIPConfigsProperty);
397 if (ipconfigs_it == props.end()) {
398 LOG(WARNING) << "Device properties is missing IPConfigs for " << device;
399 return false;
400 }
401 output->ipconfig = ParseIPConfigsProperty(device, ipconfigs_it->second);
402
403 return true;
404}
405
Jie Jiang850a4712020-04-08 21:06:36 +0900406void ShillClient::OnDevicePropertyChangeRegistration(
407 const std::string& interface,
408 const std::string& signal_name,
409 bool success) {
410 if (!success)
411 LOG(ERROR) << "[" << interface << "]: "
412 << "Unable to register listener for " << signal_name;
413}
414
415void ShillClient::OnDevicePropertyChange(const std::string& device,
416 const std::string& property_name,
417 const brillo::Any& property_value) {
418 if (property_name != shill::kIPConfigsProperty)
419 return;
420
421 const IPConfig& ipconfig = ParseIPConfigsProperty(device, property_value);
422 // TODO(jiejiang): Keep a cache of the last parsed IPConfig, and only
423 // trigger handlers if there is an actual change.
424 for (const auto& handler : ipconfigs_handlers_)
425 handler.Run(device, ipconfig);
426}
427
Garrick Evans3388a032020-03-24 11:25:55 +0900428} // namespace patchpanel