1 // Copyright 2014 The Chromium 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.
5 #include "components/wifi/wifi_service.h"
8 #import <CoreWLAN/CoreWLAN.h>
9 #import <SystemConfiguration/SystemConfiguration.h>
11 #include "base/bind.h"
12 #include "base/mac/foundation_util.h"
13 #include "base/mac/scoped_cftyperef.h"
14 #include "base/mac/scoped_nsobject.h"
15 #include "base/mac/sdk_forward_declarations.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/strings/sys_string_conversions.h"
18 #include "components/onc/onc_constants.h"
19 #include "components/wifi/network_properties.h"
20 #include "crypto/apple_keychain.h"
24 // Implementation of WiFiService for Mac OS X.
25 class WiFiServiceMac : public WiFiService {
28 ~WiFiServiceMac() override;
30 // WiFiService interface implementation.
32 scoped_refptr<base::SequencedTaskRunner> task_runner) override;
34 void UnInitialize() override;
36 void GetProperties(const std::string& network_guid,
37 base::DictionaryValue* properties,
38 std::string* error) override;
40 void GetManagedProperties(const std::string& network_guid,
41 base::DictionaryValue* managed_properties,
42 std::string* error) override;
44 void GetState(const std::string& network_guid,
45 base::DictionaryValue* properties,
46 std::string* error) override;
48 void SetProperties(const std::string& network_guid,
49 scoped_ptr<base::DictionaryValue> properties,
50 std::string* error) override;
52 void CreateNetwork(bool shared,
53 scoped_ptr<base::DictionaryValue> properties,
54 std::string* network_guid,
55 std::string* error) override;
57 void GetVisibleNetworks(const std::string& network_type,
58 base::ListValue* network_list,
59 bool include_details) override;
61 void RequestNetworkScan() override;
63 void StartConnect(const std::string& network_guid,
64 std::string* error) override;
66 void StartDisconnect(const std::string& network_guid,
67 std::string* error) override;
69 void GetKeyFromSystem(const std::string& network_guid,
70 std::string* key_data,
71 std::string* error) override;
73 void SetEventObservers(
74 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
75 const NetworkGuidListCallback& networks_changed_observer,
76 const NetworkGuidListCallback& network_list_changed_observer) override;
78 void RequestConnectedNetworkUpdate() override;
80 void GetConnectedNetworkSSID(std::string* ssid, std::string* error) override;
83 // Checks |ns_error| and if is not |nil|, then stores |error_name|
85 bool CheckError(NSError* ns_error,
86 const char* error_name,
87 std::string* error) const;
89 // Gets |ssid| from unique |network_guid|.
90 NSString* SSIDFromGUID(const std::string& network_guid) const {
91 return base::SysUTF8ToNSString(network_guid);
94 // Gets unique |network_guid| string based on |ssid|.
95 std::string GUIDFromSSID(NSString* ssid) const {
96 return base::SysNSStringToUTF8(ssid);
99 // Populates |properties| from |network|.
100 void NetworkPropertiesFromCWNetwork(const CWNetwork* network,
101 NetworkProperties* properties) const;
103 // Converts |CWSecurityMode| into onc::wifi::k{WPA|WEP}* security constant.
104 std::string SecurityFromCWSecurityMode(CWSecurityMode security) const;
106 // Returns onc::wifi::k{WPA|WEP}* security constant supported by the
108 std::string SecurityFromCWNetwork(const CWNetwork* network) const;
110 // Converts |CWChannelBand| into Frequency constant.
111 Frequency FrequencyFromCWChannelBand(CWChannelBand band) const;
113 // Gets current |onc::connection_state| for given |network_guid|.
114 std::string GetNetworkConnectionState(const std::string& network_guid) const;
116 // Updates |networks_| with the list of visible wireless networks.
117 void UpdateNetworks();
119 // Find network by |network_guid| and return iterator to its entry in
121 NetworkList::iterator FindNetwork(const std::string& network_guid);
123 // Handles notification from |wlan_observer_|.
124 void OnWlanObserverNotification();
126 // Notifies |network_list_changed_observer_| that list of visible networks has
127 // changed to |networks|.
128 void NotifyNetworkListChanged(const NetworkList& networks);
130 // Notifies |networks_changed_observer_| that network |network_guid|
131 // connection state has changed.
132 void NotifyNetworkChanged(const std::string& network_guid);
134 // Default interface.
135 base::scoped_nsobject<CWInterface> interface_;
136 // WLAN Notifications observer. |this| doesn't own this reference.
139 // Observer to get notified when network(s) have changed (e.g. connect).
140 NetworkGuidListCallback networks_changed_observer_;
141 // Observer to get notified when network list has changed.
142 NetworkGuidListCallback network_list_changed_observer_;
143 // MessageLoopProxy to which events should be posted.
144 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
145 // Task runner for worker tasks.
146 scoped_refptr<base::SequencedTaskRunner> task_runner_;
147 // Cached list of visible networks. Updated by |UpdateNetworks|.
148 NetworkList networks_;
149 // Guid of last known connected network.
150 std::string connected_network_guid_;
151 // Temporary storage of network properties indexed by |network_guid|.
152 base::DictionaryValue network_properties_;
154 DISALLOW_COPY_AND_ASSIGN(WiFiServiceMac);
157 WiFiServiceMac::WiFiServiceMac() : wlan_observer_(nil) {
160 WiFiServiceMac::~WiFiServiceMac() {
163 void WiFiServiceMac::Initialize(
164 scoped_refptr<base::SequencedTaskRunner> task_runner) {
165 task_runner_.swap(task_runner);
166 interface_.reset([[CWInterface interface] retain]);
168 DVLOG(1) << "Failed to initialize default interface.";
173 respondsToSelector:@selector(associateToNetwork:password:error:)]) {
174 DVLOG(1) << "CWInterface does not support associateToNetwork.";
180 void WiFiServiceMac::UnInitialize() {
182 [[NSNotificationCenter defaultCenter] removeObserver:wlan_observer_];
186 void WiFiServiceMac::GetProperties(const std::string& network_guid,
187 base::DictionaryValue* properties,
188 std::string* error) {
189 NetworkList::iterator it = FindNetwork(network_guid);
190 if (it == networks_.end()) {
191 DVLOG(1) << "Network not found:" << network_guid;
192 *error = kErrorNotFound;
196 it->connection_state = GetNetworkConnectionState(network_guid);
197 scoped_ptr<base::DictionaryValue> network(it->ToValue(false));
198 properties->Swap(network.get());
199 DVLOG(1) << *properties;
202 void WiFiServiceMac::GetManagedProperties(
203 const std::string& network_guid,
204 base::DictionaryValue* managed_properties,
205 std::string* error) {
206 *error = kErrorNotImplemented;
209 void WiFiServiceMac::GetState(const std::string& network_guid,
210 base::DictionaryValue* properties,
211 std::string* error) {
212 *error = kErrorNotImplemented;
215 void WiFiServiceMac::SetProperties(
216 const std::string& network_guid,
217 scoped_ptr<base::DictionaryValue> properties,
218 std::string* error) {
219 base::DictionaryValue* existing_properties;
220 // If the network properties already exist, don't override previously set
221 // properties, unless they are set in |properties|.
222 if (network_properties_.GetDictionaryWithoutPathExpansion(
223 network_guid, &existing_properties)) {
224 existing_properties->MergeDictionary(properties.get());
226 network_properties_.SetWithoutPathExpansion(network_guid,
227 properties.release());
231 void WiFiServiceMac::CreateNetwork(
233 scoped_ptr<base::DictionaryValue> properties,
234 std::string* network_guid,
235 std::string* error) {
236 NetworkProperties network_properties;
237 if (!network_properties.UpdateFromValue(*properties)) {
238 *error = kErrorInvalidData;
242 std::string guid = network_properties.ssid;
243 if (FindNetwork(guid) != networks_.end()) {
244 *error = kErrorInvalidData;
247 network_properties_.SetWithoutPathExpansion(guid,
248 properties.release());
249 *network_guid = guid;
252 void WiFiServiceMac::GetVisibleNetworks(const std::string& network_type,
253 base::ListValue* network_list,
254 bool include_details) {
255 if (!network_type.empty() &&
256 network_type != onc::network_type::kAllTypes &&
257 network_type != onc::network_type::kWiFi) {
261 if (networks_.empty())
264 for (NetworkList::const_iterator it = networks_.begin();
265 it != networks_.end();
267 scoped_ptr<base::DictionaryValue> network(it->ToValue(!include_details));
268 network_list->Append(network.release());
272 void WiFiServiceMac::RequestNetworkScan() {
273 DVLOG(1) << "*** RequestNetworkScan";
277 void WiFiServiceMac::StartConnect(const std::string& network_guid,
278 std::string* error) {
279 NSError* ns_error = nil;
281 DVLOG(1) << "*** StartConnect: " << network_guid;
282 // Remember previously connected network.
283 std::string connected_network_guid = GUIDFromSSID([interface_ ssid]);
284 // Check whether desired network is already connected.
285 if (network_guid == connected_network_guid)
288 NSSet* networks = [interface_
289 scanForNetworksWithName:SSIDFromGUID(network_guid)
292 if (CheckError(ns_error, kErrorScanForNetworksWithName, error))
295 CWNetwork* network = [networks anyObject];
296 if (network == nil) {
297 // System can't find the network, remove it from the |networks_| and notify
299 NetworkList::iterator it = FindNetwork(connected_network_guid);
300 if (it != networks_.end()) {
302 // Notify observers that list has changed.
303 NotifyNetworkListChanged(networks_);
306 *error = kErrorNotFound;
310 // Check whether WiFi Password is set in |network_properties_|.
311 base::DictionaryValue* properties;
312 base::DictionaryValue* wifi;
313 std::string passphrase;
314 NSString* ns_password = nil;
315 if (network_properties_.GetDictionaryWithoutPathExpansion(network_guid,
317 properties->GetDictionary(onc::network_type::kWiFi, &wifi) &&
318 wifi->GetString(onc::wifi::kPassphrase, &passphrase)) {
319 ns_password = base::SysUTF8ToNSString(passphrase);
322 // Number of attempts to associate to network.
323 static const int kMaxAssociationAttempts = 3;
324 // Try to associate to network several times if timeout or PMK error occurs.
325 for (int i = 0; i < kMaxAssociationAttempts; ++i) {
326 // Nil out the PMK to prevent stale data from causing invalid PMK error
327 // (CoreWLANTypes -3924).
328 [interface_ setPairwiseMasterKey:nil error:&ns_error];
329 if (![interface_ associateToNetwork:network
332 NSInteger error_code = [ns_error code];
333 if (error_code != kCWTimeoutErr && error_code != kCWInvalidPMKErr) {
338 CheckError(ns_error, kErrorAssociateToNetwork, error);
341 void WiFiServiceMac::StartDisconnect(const std::string& network_guid,
342 std::string* error) {
343 DVLOG(1) << "*** StartDisconnect: " << network_guid;
345 if (network_guid == GUIDFromSSID([interface_ ssid])) {
346 // Power-cycle the interface to disconnect from current network and connect
347 // to default network.
348 NSError* ns_error = nil;
349 [interface_ setPower:NO error:&ns_error];
350 CheckError(ns_error, kErrorAssociateToNetwork, error);
351 [interface_ setPower:YES error:&ns_error];
352 CheckError(ns_error, kErrorAssociateToNetwork, error);
354 *error = kErrorNotConnected;
358 void WiFiServiceMac::GetKeyFromSystem(const std::string& network_guid,
359 std::string* key_data,
360 std::string* error) {
361 static const char kAirPortServiceName[] = "AirPort";
363 UInt32 password_length = 0;
364 void *password_data = NULL;
365 crypto::AppleKeychain keychain;
366 OSStatus status = keychain.FindGenericPassword(NULL,
367 strlen(kAirPortServiceName),
369 network_guid.length(),
370 network_guid.c_str(),
374 if (status != errSecSuccess) {
375 *error = kErrorNotFound;
380 *key_data = std::string(reinterpret_cast<char*>(password_data),
382 keychain.ItemFreeContent(NULL, password_data);
386 void WiFiServiceMac::SetEventObservers(
387 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
388 const NetworkGuidListCallback& networks_changed_observer,
389 const NetworkGuidListCallback& network_list_changed_observer) {
390 message_loop_proxy_.swap(message_loop_proxy);
391 networks_changed_observer_ = networks_changed_observer;
392 network_list_changed_observer_ = network_list_changed_observer;
394 // Remove previous OS notifications observer.
395 if (wlan_observer_) {
396 [[NSNotificationCenter defaultCenter] removeObserver:wlan_observer_];
397 wlan_observer_ = nil;
400 // Subscribe to OS notifications.
401 if (!networks_changed_observer_.is_null()) {
402 void (^ns_observer) (NSNotification* notification) =
403 ^(NSNotification* notification) {
404 DVLOG(1) << "Received CWSSIDDidChangeNotification";
405 task_runner_->PostTask(
407 base::Bind(&WiFiServiceMac::OnWlanObserverNotification,
408 base::Unretained(this)));
411 wlan_observer_ = [[NSNotificationCenter defaultCenter]
412 addObserverForName:kCWSSIDDidChangeNotification
415 usingBlock:ns_observer];
419 void WiFiServiceMac::RequestConnectedNetworkUpdate() {
420 OnWlanObserverNotification();
423 void WiFiServiceMac::GetConnectedNetworkSSID(std::string* ssid,
424 std::string* error) {
425 *ssid = base::SysNSStringToUTF8([interface_ ssid]);
429 std::string WiFiServiceMac::GetNetworkConnectionState(
430 const std::string& network_guid) const {
431 if (network_guid != GUIDFromSSID([interface_ ssid]))
432 return onc::connection_state::kNotConnected;
434 // Check whether WiFi network is reachable.
435 struct sockaddr_in local_wifi_address;
436 bzero(&local_wifi_address, sizeof(local_wifi_address));
437 local_wifi_address.sin_len = sizeof(local_wifi_address);
438 local_wifi_address.sin_family = AF_INET;
439 local_wifi_address.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
440 base::ScopedCFTypeRef<SCNetworkReachabilityRef> reachability(
441 SCNetworkReachabilityCreateWithAddress(
443 reinterpret_cast<const struct sockaddr*>(&local_wifi_address)));
444 SCNetworkReachabilityFlags flags = 0u;
445 if (SCNetworkReachabilityGetFlags(reachability, &flags) &&
446 (flags & kSCNetworkReachabilityFlagsReachable) &&
447 (flags & kSCNetworkReachabilityFlagsIsDirect)) {
448 // Network is reachable, report is as |kConnected|.
449 return onc::connection_state::kConnected;
451 // Network is not reachable yet, so it must be |kConnecting|.
452 return onc::connection_state::kConnecting;
455 void WiFiServiceMac::UpdateNetworks() {
456 NSError* ns_error = nil;
457 NSSet* cw_networks = [interface_ scanForNetworksWithName:nil
462 std::string connected_bssid = base::SysNSStringToUTF8([interface_ bssid]);
463 std::map<std::string, NetworkProperties*> network_properties_map;
466 // There is one |cw_network| per BSS in |cw_networks|, so go through the set
467 // and combine them, paying attention to supported frequencies.
468 for (CWNetwork* cw_network in cw_networks) {
469 std::string network_guid = GUIDFromSSID([cw_network ssid]);
470 bool update_all_properties = false;
472 if (network_properties_map.find(network_guid) ==
473 network_properties_map.end()) {
474 networks_.push_back(NetworkProperties());
475 network_properties_map[network_guid] = &networks_.back();
476 update_all_properties = true;
478 // If current network is connected, use its properties for this network.
479 if (base::SysNSStringToUTF8([cw_network bssid]) == connected_bssid)
480 update_all_properties = true;
482 NetworkProperties* properties = network_properties_map.at(network_guid);
483 if (update_all_properties) {
484 NetworkPropertiesFromCWNetwork(cw_network, properties);
486 properties->frequency_set.insert(FrequencyFromCWChannelBand(
487 [[cw_network wlanChannel] channelBand]));
490 // Sort networks, so connected/connecting is up front.
491 networks_.sort(NetworkProperties::OrderByType);
492 // Notify observers that list has changed.
493 NotifyNetworkListChanged(networks_);
496 bool WiFiServiceMac::CheckError(NSError* ns_error,
497 const char* error_name,
498 std::string* error) const {
499 if (ns_error != nil) {
500 DLOG(ERROR) << "*** Error:" << error_name << ":" << [ns_error code];
507 void WiFiServiceMac::NetworkPropertiesFromCWNetwork(
508 const CWNetwork* network,
509 NetworkProperties* properties) const {
510 std::string network_guid = GUIDFromSSID([network ssid]);
512 properties->connection_state = GetNetworkConnectionState(network_guid);
513 properties->ssid = base::SysNSStringToUTF8([network ssid]);
514 properties->name = properties->ssid;
515 properties->guid = network_guid;
516 properties->type = onc::network_type::kWiFi;
518 properties->bssid = base::SysNSStringToUTF8([network bssid]);
519 properties->frequency = FrequencyFromCWChannelBand(
520 static_cast<CWChannelBand>([[network wlanChannel] channelBand]));
521 properties->frequency_set.insert(properties->frequency);
523 // -[CWNetwork supportsSecurity:] is available from 10.7 SDK while
524 // -[CWNetwork securityMode] is deprecated and hidden as private since
525 // 10.9 SDK. The latter is kept for now to support running on 10.6. It
526 // should be removed when 10.6 support is dropped.
527 if ([network respondsToSelector:@selector(supportsSecurity:)]) {
528 properties->security = SecurityFromCWNetwork(network);
530 properties->security = SecurityFromCWSecurityMode(
531 static_cast<CWSecurityMode>([[network securityMode] intValue]));
534 // rssiValue property of CWNetwork is available from 10.7 SDK while
535 // -[CWNetwork rssi] is deprecated and hidden as private since 10.9 SDK.
536 // The latter is kept for now to support running on 10.6. It should be
537 // removed when 10.6 support is dropped.
538 if ([network respondsToSelector:@selector(rssiValue)])
539 properties->signal_strength = [network rssiValue];
541 properties->signal_strength = [[network rssi] intValue];
544 std::string WiFiServiceMac::SecurityFromCWSecurityMode(
545 CWSecurityMode security) const {
547 case kCWSecurityModeWPA_Enterprise:
548 case kCWSecurityModeWPA2_Enterprise:
549 return onc::wifi::kWPA_EAP;
550 case kCWSecurityModeWPA_PSK:
551 case kCWSecurityModeWPA2_PSK:
552 return onc::wifi::kWPA_PSK;
553 case kCWSecurityModeWEP:
554 return onc::wifi::kWEP_PSK;
555 case kCWSecurityModeOpen:
556 return onc::wifi::kSecurityNone;
557 // TODO(mef): Figure out correct mapping.
558 case kCWSecurityModeWPS:
559 case kCWSecurityModeDynamicWEP:
560 return onc::wifi::kWPA_EAP;
562 return onc::wifi::kWPA_EAP;
565 std::string WiFiServiceMac::SecurityFromCWNetwork(
566 const CWNetwork* network) const {
567 if ([network supportsSecurity:kCWSecurityWPAEnterprise] ||
568 [network supportsSecurity:kCWSecurityWPA2Enterprise]) {
569 return onc::wifi::kWPA_EAP;
572 if ([network supportsSecurity:kCWSecurityWPAPersonal] ||
573 [network supportsSecurity:kCWSecurityWPA2Personal]) {
574 return onc::wifi::kWPA_PSK;
577 if ([network supportsSecurity:kCWSecurityWEP])
578 return onc::wifi::kWEP_PSK;
580 if ([network supportsSecurity:kCWSecurityNone])
581 return onc::wifi::kSecurityNone;
583 // TODO(mef): Figure out correct mapping.
584 if ([network supportsSecurity:kCWSecurityDynamicWEP])
585 return onc::wifi::kWPA_EAP;
587 return onc::wifi::kWPA_EAP;
590 Frequency WiFiServiceMac::FrequencyFromCWChannelBand(CWChannelBand band) const {
591 return band == kCWChannelBand2GHz ? kFrequency2400 : kFrequency5000;
594 NetworkList::iterator WiFiServiceMac::FindNetwork(
595 const std::string& network_guid) {
596 for (NetworkList::iterator it = networks_.begin();
597 it != networks_.end();
599 if (it->guid == network_guid)
602 return networks_.end();
605 void WiFiServiceMac::OnWlanObserverNotification() {
606 std::string connected_network_guid = GUIDFromSSID([interface_ ssid]);
607 DVLOG(1) << " *** Got Notification: " << connected_network_guid;
608 // Connected network has changed, mark previous one disconnected.
609 if (connected_network_guid != connected_network_guid_) {
610 // Update connection_state of newly connected network.
611 NetworkList::iterator it = FindNetwork(connected_network_guid_);
612 if (it != networks_.end()) {
613 it->connection_state = onc::connection_state::kNotConnected;
614 NotifyNetworkChanged(connected_network_guid_);
616 connected_network_guid_ = connected_network_guid;
619 if (!connected_network_guid.empty()) {
620 // Update connection_state of newly connected network.
621 NetworkList::iterator it = FindNetwork(connected_network_guid);
622 if (it != networks_.end()) {
623 it->connection_state = GetNetworkConnectionState(connected_network_guid);
625 // Can't find |connected_network_guid| in |networks_|, try to update it.
628 // Notify that network is connecting.
629 NotifyNetworkChanged(connected_network_guid);
630 // Further network change notification will be sent by detector.
634 void WiFiServiceMac::NotifyNetworkListChanged(const NetworkList& networks) {
635 if (network_list_changed_observer_.is_null())
638 NetworkGuidList current_networks;
639 for (NetworkList::const_iterator it = networks.begin();
640 it != networks.end();
642 current_networks.push_back(it->guid);
645 message_loop_proxy_->PostTask(
647 base::Bind(network_list_changed_observer_, current_networks));
650 void WiFiServiceMac::NotifyNetworkChanged(const std::string& network_guid) {
651 if (networks_changed_observer_.is_null())
654 DVLOG(1) << "NotifyNetworkChanged: " << network_guid;
655 NetworkGuidList changed_networks(1, network_guid);
656 message_loop_proxy_->PostTask(
658 base::Bind(networks_changed_observer_, changed_networks));
662 WiFiService* WiFiService::Create() { return new WiFiServiceMac(); }