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/message_loop/message_loop.h"
16 #include "base/strings/sys_string_conversions.h"
17 #include "components/onc/onc_constants.h"
19 #if !defined(MAC_OS_X_VERSION_10_7) || \
20 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
22 // Local definitions of API added in Mac OS X 10.7
24 @interface CWInterface (LionAPI)
25 - (BOOL)associateToNetwork:(CWNetwork*)network
26 password:(NSString*)password
27 error:(NSError**)error;
28 - (NSSet*)scanForNetworksWithName:(NSString*)networkName
29 error:(NSError**)error;
33 kCWChannelBandUnknown = 0,
34 kCWChannelBand2GHz = 1,
35 kCWChannelBand5GHz = 2,
38 @interface CWChannel : NSObject
39 @property(readonly) CWChannelBand channelBand;
42 @interface CWNetwork (LionAPI)
43 @property(readonly) CWChannel* wlanChannel;
50 const char kErrorAssociateToNetwork[] = "Error.AssociateToNetwork";
51 const char kErrorInvalidData[] = "Error.InvalidData";
52 const char kErrorNotConnected[] = "Error.NotConnected";
53 const char kErrorNotFound[] = "Error.NotFound";
54 const char kErrorNotImplemented[] = "Error.NotImplemented";
55 const char kErrorScanForNetworksWithName[] = "Error.ScanForNetworksWithName";
57 // Implementation of WiFiService for Mac OS X.
58 class WiFiServiceMac : public WiFiService {
61 virtual ~WiFiServiceMac();
63 // WiFiService interface implementation.
64 virtual void Initialize(
65 scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE;
67 virtual void UnInitialize() OVERRIDE;
69 virtual void GetProperties(const std::string& network_guid,
70 base::DictionaryValue* properties,
71 std::string* error) OVERRIDE;
73 virtual void GetManagedProperties(const std::string& network_guid,
74 base::DictionaryValue* managed_properties,
75 std::string* error) OVERRIDE;
77 virtual void GetState(const std::string& network_guid,
78 base::DictionaryValue* properties,
79 std::string* error) OVERRIDE;
81 virtual void SetProperties(const std::string& network_guid,
82 scoped_ptr<base::DictionaryValue> properties,
83 std::string* error) OVERRIDE;
85 virtual void CreateNetwork(bool shared,
86 scoped_ptr<base::DictionaryValue> properties,
87 std::string* network_guid,
88 std::string* error) OVERRIDE;
90 virtual void GetVisibleNetworks(const std::string& network_type,
91 base::ListValue* network_list) OVERRIDE;
93 virtual void RequestNetworkScan() OVERRIDE;
95 virtual void StartConnect(const std::string& network_guid,
96 std::string* error) OVERRIDE;
98 virtual void StartDisconnect(const std::string& network_guid,
99 std::string* error) OVERRIDE;
101 virtual void GetKeyFromSystem(const std::string& network_guid,
102 std::string* key_data,
103 std::string* error) OVERRIDE;
105 virtual void SetEventObservers(
106 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
107 const NetworkGuidListCallback& networks_changed_observer,
108 const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE;
110 virtual void RequestConnectedNetworkUpdate() OVERRIDE;
113 // Checks |ns_error| and if is not |nil|, then stores |error_name|
115 bool CheckError(NSError* ns_error,
116 const char* error_name,
117 std::string* error) const;
119 // Gets |ssid| from unique |network_guid|.
120 NSString* SSIDFromGUID(const std::string& network_guid) const {
121 return base::SysUTF8ToNSString(network_guid);
124 // Gets unique |network_guid| string based on |ssid|.
125 std::string GUIDFromSSID(NSString* ssid) const {
126 return base::SysNSStringToUTF8(ssid);
129 // Populates |properties| from |network|.
130 void NetworkPropertiesFromCWNetwork(const CWNetwork* network,
131 NetworkProperties* properties) const;
133 // Converts |CWSecurityMode| into onc::wifi::k{WPA|WEP}* security constant.
134 std::string SecurityFromCWSecurityMode(CWSecurityMode security) const;
136 // Converts |CWChannelBand| into WiFiService::Frequency constant.
137 Frequency FrequencyFromCWChannelBand(CWChannelBand band) const;
139 // Gets current |onc::connection_state| for given |network_guid|.
140 std::string GetNetworkConnectionState(const std::string& network_guid) const;
142 // Updates |networks_| with the list of visible wireless networks.
143 void UpdateNetworks();
145 // Find network by |network_guid| and return iterator to its entry in
147 NetworkList::iterator FindNetwork(const std::string& network_guid);
149 // Handles notification from |wlan_observer_|.
150 void OnWlanObserverNotification();
152 // Notifies |network_list_changed_observer_| that list of visible networks has
153 // changed to |networks|.
154 void NotifyNetworkListChanged(const NetworkList& networks);
156 // Notifies |networks_changed_observer_| that network |network_guid|
157 // connection state has changed.
158 void NotifyNetworkChanged(const std::string& network_guid);
160 // Default interface.
161 base::scoped_nsobject<CWInterface> interface_;
162 // WLAN Notifications observer. |this| doesn't own this reference.
165 // Observer to get notified when network(s) have changed (e.g. connect).
166 NetworkGuidListCallback networks_changed_observer_;
167 // Observer to get notified when network list has changed.
168 NetworkGuidListCallback network_list_changed_observer_;
169 // MessageLoopProxy to which events should be posted.
170 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
171 // Task runner for worker tasks.
172 scoped_refptr<base::SequencedTaskRunner> task_runner_;
173 // Cached list of visible networks. Updated by |UpdateNetworks|.
174 NetworkList networks_;
175 // Guid of last known connected network.
176 std::string connected_network_guid_;
177 // Temporary storage of network properties indexed by |network_guid|.
178 base::DictionaryValue network_properties_;
180 DISALLOW_COPY_AND_ASSIGN(WiFiServiceMac);
183 WiFiServiceMac::WiFiServiceMac() : wlan_observer_(nil) {
186 WiFiServiceMac::~WiFiServiceMac() {
189 void WiFiServiceMac::Initialize(
190 scoped_refptr<base::SequencedTaskRunner> task_runner) {
191 task_runner_.swap(task_runner);
192 interface_.reset([[CWInterface interface] retain]);
194 DVLOG(1) << "Failed to initialize default interface.";
199 respondsToSelector:@selector(associateToNetwork:password:error:)]) {
200 DVLOG(1) << "CWInterface does not support associateToNetwork.";
206 void WiFiServiceMac::UnInitialize() {
208 [[NSNotificationCenter defaultCenter] removeObserver:wlan_observer_];
212 void WiFiServiceMac::GetProperties(const std::string& network_guid,
213 base::DictionaryValue* properties,
214 std::string* error) {
215 NetworkList::iterator it = FindNetwork(network_guid);
216 if (it == networks_.end()) {
217 DVLOG(1) << "Network not found:" << network_guid;
218 *error = kErrorNotFound;
222 it->connection_state = GetNetworkConnectionState(network_guid);
223 scoped_ptr<base::DictionaryValue> network(it->ToValue(false));
224 properties->Swap(network.get());
225 DVLOG(1) << *properties;
228 void WiFiServiceMac::GetManagedProperties(
229 const std::string& network_guid,
230 base::DictionaryValue* managed_properties,
231 std::string* error) {
232 *error = kErrorNotImplemented;
235 void WiFiServiceMac::GetState(const std::string& network_guid,
236 base::DictionaryValue* properties,
237 std::string* error) {
238 *error = kErrorNotImplemented;
241 void WiFiServiceMac::SetProperties(
242 const std::string& network_guid,
243 scoped_ptr<base::DictionaryValue> properties,
244 std::string* error) {
245 base::DictionaryValue* existing_properties;
246 // If the network properties already exist, don't override previously set
247 // properties, unless they are set in |properties|.
248 if (network_properties_.GetDictionaryWithoutPathExpansion(
249 network_guid, &existing_properties)) {
250 existing_properties->MergeDictionary(properties.get());
252 network_properties_.SetWithoutPathExpansion(network_guid,
253 properties.release());
257 void WiFiServiceMac::CreateNetwork(
259 scoped_ptr<base::DictionaryValue> properties,
260 std::string* network_guid,
261 std::string* error) {
262 WiFiService::NetworkProperties network_properties;
263 if (!network_properties.UpdateFromValue(*properties)) {
264 *error = kErrorInvalidData;
268 std::string guid = network_properties.ssid;
269 if (FindNetwork(guid) != networks_.end()) {
270 *error = kErrorInvalidData;
273 network_properties_.SetWithoutPathExpansion(guid,
274 properties.release());
275 *network_guid = guid;
278 void WiFiServiceMac::GetVisibleNetworks(const std::string& network_type,
279 base::ListValue* network_list) {
280 if (!network_type.empty() &&
281 network_type != onc::network_type::kAllTypes &&
282 network_type != onc::network_type::kWiFi) {
286 if (networks_.empty())
289 for (WiFiService::NetworkList::const_iterator it = networks_.begin();
290 it != networks_.end();
292 scoped_ptr<base::DictionaryValue> network(it->ToValue(true));
293 network_list->Append(network.release());
297 void WiFiServiceMac::RequestNetworkScan() {
298 DVLOG(1) << "*** RequestNetworkScan";
302 void WiFiServiceMac::StartConnect(const std::string& network_guid,
303 std::string* error) {
304 NSError* ns_error = nil;
306 DVLOG(1) << "*** StartConnect: " << network_guid;
307 // Remember previously connected network.
308 std::string connected_network_guid = GUIDFromSSID([interface_ ssid]);
309 // Check whether desired network is already connected.
310 if (network_guid == connected_network_guid)
313 NSSet* networks = [interface_
314 scanForNetworksWithName:SSIDFromGUID(network_guid)
317 if (CheckError(ns_error, kErrorScanForNetworksWithName, error))
320 CWNetwork* network = [networks anyObject];
321 if (network == nil) {
322 // System can't find the network, remove it from the |networks_| and notify
324 NetworkList::iterator it = FindNetwork(connected_network_guid);
325 if (it != networks_.end()) {
327 // Notify observers that list has changed.
328 NotifyNetworkListChanged(networks_);
331 *error = kErrorNotFound;
335 // Check whether WiFi Password is set in |network_properties_|.
336 base::DictionaryValue* properties;
337 base::DictionaryValue* wifi;
338 std::string passphrase;
339 NSString* ns_password = nil;
340 if (network_properties_.GetDictionaryWithoutPathExpansion(network_guid,
342 properties->GetDictionary(onc::network_type::kWiFi, &wifi) &&
343 wifi->GetString(onc::wifi::kPassphrase, &passphrase)) {
344 ns_password = base::SysUTF8ToNSString(passphrase);
347 // Number of attempts to associate to network.
348 static const int kMaxAssociationAttempts = 3;
349 // Try to associate to network several times if timeout or PMK error occurs.
350 for (int i = 0; i < kMaxAssociationAttempts; ++i) {
351 // Nil out the PMK to prevent stale data from causing invalid PMK error
352 // (CoreWLANTypes -3924).
353 [interface_ setPairwiseMasterKey:nil error:&ns_error];
354 if (![interface_ associateToNetwork:network
357 NSInteger error_code = [ns_error code];
358 if (error_code != kCWTimeoutErr && error_code != kCWInvalidPMKErr) {
363 CheckError(ns_error, kErrorAssociateToNetwork, error);
366 void WiFiServiceMac::StartDisconnect(const std::string& network_guid,
367 std::string* error) {
368 DVLOG(1) << "*** StartDisconnect: " << network_guid;
370 if (network_guid == GUIDFromSSID([interface_ ssid])) {
371 // Power-cycle the interface to disconnect from current network and connect
372 // to default network.
373 NSError* ns_error = nil;
374 [interface_ setPower:NO error:&ns_error];
375 CheckError(ns_error, kErrorAssociateToNetwork, error);
376 [interface_ setPower:YES error:&ns_error];
377 CheckError(ns_error, kErrorAssociateToNetwork, error);
379 *error = kErrorNotConnected;
383 void WiFiServiceMac::GetKeyFromSystem(const std::string& network_guid,
384 std::string* key_data,
385 std::string* error) {
386 static const char kAirPortServiceName[] = "AirPort";
388 UInt32 password_length = 0;
389 void *password_data = NULL;
390 OSStatus status = SecKeychainFindGenericPassword(NULL,
391 strlen(kAirPortServiceName),
393 network_guid.length(),
394 network_guid.c_str(),
398 if (status != errSecSuccess) {
399 *error = kErrorNotFound;
404 *key_data = std::string(reinterpret_cast<char*>(password_data),
406 SecKeychainItemFreeContent(NULL, password_data);
410 void WiFiServiceMac::SetEventObservers(
411 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
412 const NetworkGuidListCallback& networks_changed_observer,
413 const NetworkGuidListCallback& network_list_changed_observer) {
414 message_loop_proxy_.swap(message_loop_proxy);
415 networks_changed_observer_ = networks_changed_observer;
416 network_list_changed_observer_ = network_list_changed_observer;
418 // Remove previous OS notifications observer.
419 if (wlan_observer_) {
420 [[NSNotificationCenter defaultCenter] removeObserver:wlan_observer_];
421 wlan_observer_ = nil;
424 // Subscribe to OS notifications.
425 if (!networks_changed_observer_.is_null()) {
426 void (^ns_observer) (NSNotification* notification) =
427 ^(NSNotification* notification) {
428 DVLOG(1) << "Received CWSSIDDidChangeNotification";
429 task_runner_->PostTask(
431 base::Bind(&WiFiServiceMac::OnWlanObserverNotification,
432 base::Unretained(this)));
435 wlan_observer_ = [[NSNotificationCenter defaultCenter]
436 addObserverForName:kCWSSIDDidChangeNotification
439 usingBlock:ns_observer];
443 void WiFiServiceMac::RequestConnectedNetworkUpdate() {
444 OnWlanObserverNotification();
447 std::string WiFiServiceMac::GetNetworkConnectionState(
448 const std::string& network_guid) const {
449 if (network_guid != GUIDFromSSID([interface_ ssid]))
450 return onc::connection_state::kNotConnected;
452 // Check whether WiFi network is reachable.
453 struct sockaddr_in local_wifi_address;
454 bzero(&local_wifi_address, sizeof(local_wifi_address));
455 local_wifi_address.sin_len = sizeof(local_wifi_address);
456 local_wifi_address.sin_family = AF_INET;
457 local_wifi_address.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
458 base::ScopedCFTypeRef<SCNetworkReachabilityRef> reachability(
459 SCNetworkReachabilityCreateWithAddress(
461 reinterpret_cast<const struct sockaddr*>(&local_wifi_address)));
462 SCNetworkReachabilityFlags flags = 0u;
463 if (SCNetworkReachabilityGetFlags(reachability, &flags) &&
464 (flags & kSCNetworkReachabilityFlagsReachable) &&
465 (flags & kSCNetworkReachabilityFlagsIsDirect)) {
466 // Network is reachable, report is as |kConnected|.
467 return onc::connection_state::kConnected;
469 // Network is not reachable yet, so it must be |kConnecting|.
470 return onc::connection_state::kConnecting;
473 void WiFiServiceMac::UpdateNetworks() {
474 NSError* ns_error = nil;
475 NSSet* cw_networks = [interface_ scanForNetworksWithName:nil
480 std::string connected_bssid = base::SysNSStringToUTF8([interface_ bssid]);
481 std::map<std::string, NetworkProperties*> network_properties_map;
484 // There is one |cw_network| per BSS in |cw_networks|, so go through the set
485 // and combine them, paying attention to supported frequencies.
486 for (CWNetwork* cw_network in cw_networks) {
487 std::string network_guid = GUIDFromSSID([cw_network ssid]);
488 bool update_all_properties = false;
490 if (network_properties_map.find(network_guid) ==
491 network_properties_map.end()) {
492 networks_.push_back(NetworkProperties());
493 network_properties_map[network_guid] = &networks_.back();
494 update_all_properties = true;
496 // If current network is connected, use its properties for this network.
497 if (base::SysNSStringToUTF8([cw_network bssid]) == connected_bssid)
498 update_all_properties = true;
500 NetworkProperties* properties = network_properties_map.at(network_guid);
501 if (update_all_properties) {
502 NetworkPropertiesFromCWNetwork(cw_network, properties);
504 properties->frequency_set.insert(FrequencyFromCWChannelBand(
505 [[cw_network wlanChannel] channelBand]));
508 // Sort networks, so connected/connecting is up front.
509 networks_.sort(NetworkProperties::OrderByType);
510 // Notify observers that list has changed.
511 NotifyNetworkListChanged(networks_);
514 bool WiFiServiceMac::CheckError(NSError* ns_error,
515 const char* error_name,
516 std::string* error) const {
517 if (ns_error != nil) {
518 DLOG(ERROR) << "*** Error:" << error_name << ":" << [ns_error code];
525 void WiFiServiceMac::NetworkPropertiesFromCWNetwork(
526 const CWNetwork* network,
527 NetworkProperties* properties) const {
528 std::string network_guid = GUIDFromSSID([network ssid]);
530 properties->connection_state = GetNetworkConnectionState(network_guid);
531 properties->ssid = base::SysNSStringToUTF8([network ssid]);
532 properties->name = properties->ssid;
533 properties->guid = network_guid;
534 properties->type = onc::network_type::kWiFi;
536 properties->bssid = base::SysNSStringToUTF8([network bssid]);
537 properties->frequency = FrequencyFromCWChannelBand(
538 static_cast<CWChannelBand>([[network wlanChannel] channelBand]));
539 properties->frequency_set.insert(properties->frequency);
540 properties->security = SecurityFromCWSecurityMode(
541 static_cast<CWSecurityMode>([[network securityMode] intValue]));
543 properties->signal_strength = [[network rssi] intValue];
546 std::string WiFiServiceMac::SecurityFromCWSecurityMode(
547 CWSecurityMode security) const {
549 case kCWSecurityModeWPA_Enterprise:
550 case kCWSecurityModeWPA2_Enterprise:
551 return onc::wifi::kWPA_EAP;
552 case kCWSecurityModeWPA_PSK:
553 case kCWSecurityModeWPA2_PSK:
554 return onc::wifi::kWPA_PSK;
555 case kCWSecurityModeWEP:
556 return onc::wifi::kWEP_PSK;
557 case kCWSecurityModeOpen:
558 return onc::wifi::kNone;
559 // TODO(mef): Figure out correct mapping.
560 case kCWSecurityModeWPS:
561 case kCWSecurityModeDynamicWEP:
562 return onc::wifi::kWPA_EAP;
564 return onc::wifi::kWPA_EAP;
568 WiFiService::Frequency WiFiServiceMac::FrequencyFromCWChannelBand(
569 CWChannelBand band) const {
570 return band == kCWChannelBand2GHz ? kFrequency2400 : kFrequency5000;
573 WiFiService::NetworkList::iterator WiFiServiceMac::FindNetwork(
574 const std::string& network_guid) {
575 for (NetworkList::iterator it = networks_.begin();
576 it != networks_.end();
578 if (it->guid == network_guid)
581 return networks_.end();
584 void WiFiServiceMac::OnWlanObserverNotification() {
585 std::string connected_network_guid = GUIDFromSSID([interface_ ssid]);
586 DVLOG(1) << " *** Got Notification: " << connected_network_guid;
587 // Connected network has changed, mark previous one disconnected.
588 if (connected_network_guid != connected_network_guid_) {
589 // Update connection_state of newly connected network.
590 NetworkList::iterator it = FindNetwork(connected_network_guid_);
591 if (it != networks_.end()) {
592 it->connection_state = onc::connection_state::kNotConnected;
593 NotifyNetworkChanged(connected_network_guid_);
595 connected_network_guid_ = connected_network_guid;
598 if (!connected_network_guid.empty()) {
599 // Update connection_state of newly connected network.
600 NetworkList::iterator it = FindNetwork(connected_network_guid);
601 if (it != networks_.end()) {
602 it->connection_state = GetNetworkConnectionState(connected_network_guid);
604 // Can't find |connected_network_guid| in |networks_|, try to update it.
607 // Notify that network is connecting.
608 NotifyNetworkChanged(connected_network_guid);
609 // Further network change notification will be sent by detector.
613 void WiFiServiceMac::NotifyNetworkListChanged(const NetworkList& networks) {
614 if (network_list_changed_observer_.is_null())
617 NetworkGuidList current_networks;
618 for (NetworkList::const_iterator it = networks.begin();
619 it != networks.end();
621 current_networks.push_back(it->guid);
624 message_loop_proxy_->PostTask(
626 base::Bind(network_list_changed_observer_, current_networks));
629 void WiFiServiceMac::NotifyNetworkChanged(const std::string& network_guid) {
630 if (networks_changed_observer_.is_null())
633 DVLOG(1) << "NotifyNetworkChanged: " << network_guid;
634 NetworkGuidList changed_networks(1, network_guid);
635 message_loop_proxy_->PostTask(
637 base::Bind(networks_changed_observer_, changed_networks));
641 WiFiService* WiFiService::Create() { return new WiFiServiceMac(); }