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"
23 // Implementation of WiFiService for Mac OS X.
24 class WiFiServiceMac : public WiFiService {
27 virtual ~WiFiServiceMac();
29 // WiFiService interface implementation.
30 virtual void Initialize(
31 scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE;
33 virtual void UnInitialize() OVERRIDE;
35 virtual void GetProperties(const std::string& network_guid,
36 base::DictionaryValue* properties,
37 std::string* error) OVERRIDE;
39 virtual void GetManagedProperties(const std::string& network_guid,
40 base::DictionaryValue* managed_properties,
41 std::string* error) OVERRIDE;
43 virtual void GetState(const std::string& network_guid,
44 base::DictionaryValue* properties,
45 std::string* error) OVERRIDE;
47 virtual void SetProperties(const std::string& network_guid,
48 scoped_ptr<base::DictionaryValue> properties,
49 std::string* error) OVERRIDE;
51 virtual void CreateNetwork(bool shared,
52 scoped_ptr<base::DictionaryValue> properties,
53 std::string* network_guid,
54 std::string* error) OVERRIDE;
56 virtual void GetVisibleNetworks(const std::string& network_type,
57 base::ListValue* network_list,
58 bool include_details) OVERRIDE;
60 virtual void RequestNetworkScan() OVERRIDE;
62 virtual void StartConnect(const std::string& network_guid,
63 std::string* error) OVERRIDE;
65 virtual void StartDisconnect(const std::string& network_guid,
66 std::string* error) OVERRIDE;
68 virtual void GetKeyFromSystem(const std::string& network_guid,
69 std::string* key_data,
70 std::string* error) OVERRIDE;
72 virtual void SetEventObservers(
73 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
74 const NetworkGuidListCallback& networks_changed_observer,
75 const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE;
77 virtual void RequestConnectedNetworkUpdate() OVERRIDE;
80 // Checks |ns_error| and if is not |nil|, then stores |error_name|
82 bool CheckError(NSError* ns_error,
83 const char* error_name,
84 std::string* error) const;
86 // Gets |ssid| from unique |network_guid|.
87 NSString* SSIDFromGUID(const std::string& network_guid) const {
88 return base::SysUTF8ToNSString(network_guid);
91 // Gets unique |network_guid| string based on |ssid|.
92 std::string GUIDFromSSID(NSString* ssid) const {
93 return base::SysNSStringToUTF8(ssid);
96 // Populates |properties| from |network|.
97 void NetworkPropertiesFromCWNetwork(const CWNetwork* network,
98 NetworkProperties* properties) const;
100 // Converts |CWSecurityMode| into onc::wifi::k{WPA|WEP}* security constant.
101 std::string SecurityFromCWSecurityMode(CWSecurityMode security) const;
103 // Converts |CWChannelBand| into Frequency constant.
104 Frequency FrequencyFromCWChannelBand(CWChannelBand band) const;
106 // Gets current |onc::connection_state| for given |network_guid|.
107 std::string GetNetworkConnectionState(const std::string& network_guid) const;
109 // Updates |networks_| with the list of visible wireless networks.
110 void UpdateNetworks();
112 // Find network by |network_guid| and return iterator to its entry in
114 NetworkList::iterator FindNetwork(const std::string& network_guid);
116 // Handles notification from |wlan_observer_|.
117 void OnWlanObserverNotification();
119 // Notifies |network_list_changed_observer_| that list of visible networks has
120 // changed to |networks|.
121 void NotifyNetworkListChanged(const NetworkList& networks);
123 // Notifies |networks_changed_observer_| that network |network_guid|
124 // connection state has changed.
125 void NotifyNetworkChanged(const std::string& network_guid);
127 // Default interface.
128 base::scoped_nsobject<CWInterface> interface_;
129 // WLAN Notifications observer. |this| doesn't own this reference.
132 // Observer to get notified when network(s) have changed (e.g. connect).
133 NetworkGuidListCallback networks_changed_observer_;
134 // Observer to get notified when network list has changed.
135 NetworkGuidListCallback network_list_changed_observer_;
136 // MessageLoopProxy to which events should be posted.
137 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
138 // Task runner for worker tasks.
139 scoped_refptr<base::SequencedTaskRunner> task_runner_;
140 // Cached list of visible networks. Updated by |UpdateNetworks|.
141 NetworkList networks_;
142 // Guid of last known connected network.
143 std::string connected_network_guid_;
144 // Temporary storage of network properties indexed by |network_guid|.
145 base::DictionaryValue network_properties_;
147 DISALLOW_COPY_AND_ASSIGN(WiFiServiceMac);
150 WiFiServiceMac::WiFiServiceMac() : wlan_observer_(nil) {
153 WiFiServiceMac::~WiFiServiceMac() {
156 void WiFiServiceMac::Initialize(
157 scoped_refptr<base::SequencedTaskRunner> task_runner) {
158 task_runner_.swap(task_runner);
159 interface_.reset([[CWInterface interface] retain]);
161 DVLOG(1) << "Failed to initialize default interface.";
166 respondsToSelector:@selector(associateToNetwork:password:error:)]) {
167 DVLOG(1) << "CWInterface does not support associateToNetwork.";
173 void WiFiServiceMac::UnInitialize() {
175 [[NSNotificationCenter defaultCenter] removeObserver:wlan_observer_];
179 void WiFiServiceMac::GetProperties(const std::string& network_guid,
180 base::DictionaryValue* properties,
181 std::string* error) {
182 NetworkList::iterator it = FindNetwork(network_guid);
183 if (it == networks_.end()) {
184 DVLOG(1) << "Network not found:" << network_guid;
185 *error = kErrorNotFound;
189 it->connection_state = GetNetworkConnectionState(network_guid);
190 scoped_ptr<base::DictionaryValue> network(it->ToValue(false));
191 properties->Swap(network.get());
192 DVLOG(1) << *properties;
195 void WiFiServiceMac::GetManagedProperties(
196 const std::string& network_guid,
197 base::DictionaryValue* managed_properties,
198 std::string* error) {
199 *error = kErrorNotImplemented;
202 void WiFiServiceMac::GetState(const std::string& network_guid,
203 base::DictionaryValue* properties,
204 std::string* error) {
205 *error = kErrorNotImplemented;
208 void WiFiServiceMac::SetProperties(
209 const std::string& network_guid,
210 scoped_ptr<base::DictionaryValue> properties,
211 std::string* error) {
212 base::DictionaryValue* existing_properties;
213 // If the network properties already exist, don't override previously set
214 // properties, unless they are set in |properties|.
215 if (network_properties_.GetDictionaryWithoutPathExpansion(
216 network_guid, &existing_properties)) {
217 existing_properties->MergeDictionary(properties.get());
219 network_properties_.SetWithoutPathExpansion(network_guid,
220 properties.release());
224 void WiFiServiceMac::CreateNetwork(
226 scoped_ptr<base::DictionaryValue> properties,
227 std::string* network_guid,
228 std::string* error) {
229 NetworkProperties network_properties;
230 if (!network_properties.UpdateFromValue(*properties)) {
231 *error = kErrorInvalidData;
235 std::string guid = network_properties.ssid;
236 if (FindNetwork(guid) != networks_.end()) {
237 *error = kErrorInvalidData;
240 network_properties_.SetWithoutPathExpansion(guid,
241 properties.release());
242 *network_guid = guid;
245 void WiFiServiceMac::GetVisibleNetworks(const std::string& network_type,
246 base::ListValue* network_list,
247 bool include_details) {
248 if (!network_type.empty() &&
249 network_type != onc::network_type::kAllTypes &&
250 network_type != onc::network_type::kWiFi) {
254 if (networks_.empty())
257 for (NetworkList::const_iterator it = networks_.begin();
258 it != networks_.end();
260 scoped_ptr<base::DictionaryValue> network(it->ToValue(!include_details));
261 network_list->Append(network.release());
265 void WiFiServiceMac::RequestNetworkScan() {
266 DVLOG(1) << "*** RequestNetworkScan";
270 void WiFiServiceMac::StartConnect(const std::string& network_guid,
271 std::string* error) {
272 NSError* ns_error = nil;
274 DVLOG(1) << "*** StartConnect: " << network_guid;
275 // Remember previously connected network.
276 std::string connected_network_guid = GUIDFromSSID([interface_ ssid]);
277 // Check whether desired network is already connected.
278 if (network_guid == connected_network_guid)
281 NSSet* networks = [interface_
282 scanForNetworksWithName:SSIDFromGUID(network_guid)
285 if (CheckError(ns_error, kErrorScanForNetworksWithName, error))
288 CWNetwork* network = [networks anyObject];
289 if (network == nil) {
290 // System can't find the network, remove it from the |networks_| and notify
292 NetworkList::iterator it = FindNetwork(connected_network_guid);
293 if (it != networks_.end()) {
295 // Notify observers that list has changed.
296 NotifyNetworkListChanged(networks_);
299 *error = kErrorNotFound;
303 // Check whether WiFi Password is set in |network_properties_|.
304 base::DictionaryValue* properties;
305 base::DictionaryValue* wifi;
306 std::string passphrase;
307 NSString* ns_password = nil;
308 if (network_properties_.GetDictionaryWithoutPathExpansion(network_guid,
310 properties->GetDictionary(onc::network_type::kWiFi, &wifi) &&
311 wifi->GetString(onc::wifi::kPassphrase, &passphrase)) {
312 ns_password = base::SysUTF8ToNSString(passphrase);
315 // Number of attempts to associate to network.
316 static const int kMaxAssociationAttempts = 3;
317 // Try to associate to network several times if timeout or PMK error occurs.
318 for (int i = 0; i < kMaxAssociationAttempts; ++i) {
319 // Nil out the PMK to prevent stale data from causing invalid PMK error
320 // (CoreWLANTypes -3924).
321 [interface_ setPairwiseMasterKey:nil error:&ns_error];
322 if (![interface_ associateToNetwork:network
325 NSInteger error_code = [ns_error code];
326 if (error_code != kCWTimeoutErr && error_code != kCWInvalidPMKErr) {
331 CheckError(ns_error, kErrorAssociateToNetwork, error);
334 void WiFiServiceMac::StartDisconnect(const std::string& network_guid,
335 std::string* error) {
336 DVLOG(1) << "*** StartDisconnect: " << network_guid;
338 if (network_guid == GUIDFromSSID([interface_ ssid])) {
339 // Power-cycle the interface to disconnect from current network and connect
340 // to default network.
341 NSError* ns_error = nil;
342 [interface_ setPower:NO error:&ns_error];
343 CheckError(ns_error, kErrorAssociateToNetwork, error);
344 [interface_ setPower:YES error:&ns_error];
345 CheckError(ns_error, kErrorAssociateToNetwork, error);
347 *error = kErrorNotConnected;
351 void WiFiServiceMac::GetKeyFromSystem(const std::string& network_guid,
352 std::string* key_data,
353 std::string* error) {
354 static const char kAirPortServiceName[] = "AirPort";
356 UInt32 password_length = 0;
357 void *password_data = NULL;
358 OSStatus status = SecKeychainFindGenericPassword(NULL,
359 strlen(kAirPortServiceName),
361 network_guid.length(),
362 network_guid.c_str(),
366 if (status != errSecSuccess) {
367 *error = kErrorNotFound;
372 *key_data = std::string(reinterpret_cast<char*>(password_data),
374 SecKeychainItemFreeContent(NULL, password_data);
378 void WiFiServiceMac::SetEventObservers(
379 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
380 const NetworkGuidListCallback& networks_changed_observer,
381 const NetworkGuidListCallback& network_list_changed_observer) {
382 message_loop_proxy_.swap(message_loop_proxy);
383 networks_changed_observer_ = networks_changed_observer;
384 network_list_changed_observer_ = network_list_changed_observer;
386 // Remove previous OS notifications observer.
387 if (wlan_observer_) {
388 [[NSNotificationCenter defaultCenter] removeObserver:wlan_observer_];
389 wlan_observer_ = nil;
392 // Subscribe to OS notifications.
393 if (!networks_changed_observer_.is_null()) {
394 void (^ns_observer) (NSNotification* notification) =
395 ^(NSNotification* notification) {
396 DVLOG(1) << "Received CWSSIDDidChangeNotification";
397 task_runner_->PostTask(
399 base::Bind(&WiFiServiceMac::OnWlanObserverNotification,
400 base::Unretained(this)));
403 wlan_observer_ = [[NSNotificationCenter defaultCenter]
404 addObserverForName:kCWSSIDDidChangeNotification
407 usingBlock:ns_observer];
411 void WiFiServiceMac::RequestConnectedNetworkUpdate() {
412 OnWlanObserverNotification();
415 std::string WiFiServiceMac::GetNetworkConnectionState(
416 const std::string& network_guid) const {
417 if (network_guid != GUIDFromSSID([interface_ ssid]))
418 return onc::connection_state::kNotConnected;
420 // Check whether WiFi network is reachable.
421 struct sockaddr_in local_wifi_address;
422 bzero(&local_wifi_address, sizeof(local_wifi_address));
423 local_wifi_address.sin_len = sizeof(local_wifi_address);
424 local_wifi_address.sin_family = AF_INET;
425 local_wifi_address.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
426 base::ScopedCFTypeRef<SCNetworkReachabilityRef> reachability(
427 SCNetworkReachabilityCreateWithAddress(
429 reinterpret_cast<const struct sockaddr*>(&local_wifi_address)));
430 SCNetworkReachabilityFlags flags = 0u;
431 if (SCNetworkReachabilityGetFlags(reachability, &flags) &&
432 (flags & kSCNetworkReachabilityFlagsReachable) &&
433 (flags & kSCNetworkReachabilityFlagsIsDirect)) {
434 // Network is reachable, report is as |kConnected|.
435 return onc::connection_state::kConnected;
437 // Network is not reachable yet, so it must be |kConnecting|.
438 return onc::connection_state::kConnecting;
441 void WiFiServiceMac::UpdateNetworks() {
442 NSError* ns_error = nil;
443 NSSet* cw_networks = [interface_ scanForNetworksWithName:nil
448 std::string connected_bssid = base::SysNSStringToUTF8([interface_ bssid]);
449 std::map<std::string, NetworkProperties*> network_properties_map;
452 // There is one |cw_network| per BSS in |cw_networks|, so go through the set
453 // and combine them, paying attention to supported frequencies.
454 for (CWNetwork* cw_network in cw_networks) {
455 std::string network_guid = GUIDFromSSID([cw_network ssid]);
456 bool update_all_properties = false;
458 if (network_properties_map.find(network_guid) ==
459 network_properties_map.end()) {
460 networks_.push_back(NetworkProperties());
461 network_properties_map[network_guid] = &networks_.back();
462 update_all_properties = true;
464 // If current network is connected, use its properties for this network.
465 if (base::SysNSStringToUTF8([cw_network bssid]) == connected_bssid)
466 update_all_properties = true;
468 NetworkProperties* properties = network_properties_map.at(network_guid);
469 if (update_all_properties) {
470 NetworkPropertiesFromCWNetwork(cw_network, properties);
472 properties->frequency_set.insert(FrequencyFromCWChannelBand(
473 [[cw_network wlanChannel] channelBand]));
476 // Sort networks, so connected/connecting is up front.
477 networks_.sort(NetworkProperties::OrderByType);
478 // Notify observers that list has changed.
479 NotifyNetworkListChanged(networks_);
482 bool WiFiServiceMac::CheckError(NSError* ns_error,
483 const char* error_name,
484 std::string* error) const {
485 if (ns_error != nil) {
486 DLOG(ERROR) << "*** Error:" << error_name << ":" << [ns_error code];
493 void WiFiServiceMac::NetworkPropertiesFromCWNetwork(
494 const CWNetwork* network,
495 NetworkProperties* properties) const {
496 std::string network_guid = GUIDFromSSID([network ssid]);
498 properties->connection_state = GetNetworkConnectionState(network_guid);
499 properties->ssid = base::SysNSStringToUTF8([network ssid]);
500 properties->name = properties->ssid;
501 properties->guid = network_guid;
502 properties->type = onc::network_type::kWiFi;
504 properties->bssid = base::SysNSStringToUTF8([network bssid]);
505 properties->frequency = FrequencyFromCWChannelBand(
506 static_cast<CWChannelBand>([[network wlanChannel] channelBand]));
507 properties->frequency_set.insert(properties->frequency);
508 properties->security = SecurityFromCWSecurityMode(
509 static_cast<CWSecurityMode>([[network securityMode] intValue]));
511 properties->signal_strength = [[network rssi] intValue];
514 std::string WiFiServiceMac::SecurityFromCWSecurityMode(
515 CWSecurityMode security) const {
517 case kCWSecurityModeWPA_Enterprise:
518 case kCWSecurityModeWPA2_Enterprise:
519 return onc::wifi::kWPA_EAP;
520 case kCWSecurityModeWPA_PSK:
521 case kCWSecurityModeWPA2_PSK:
522 return onc::wifi::kWPA_PSK;
523 case kCWSecurityModeWEP:
524 return onc::wifi::kWEP_PSK;
525 case kCWSecurityModeOpen:
526 return onc::wifi::kSecurityNone;
527 // TODO(mef): Figure out correct mapping.
528 case kCWSecurityModeWPS:
529 case kCWSecurityModeDynamicWEP:
530 return onc::wifi::kWPA_EAP;
532 return onc::wifi::kWPA_EAP;
535 Frequency WiFiServiceMac::FrequencyFromCWChannelBand(CWChannelBand band) const {
536 return band == kCWChannelBand2GHz ? kFrequency2400 : kFrequency5000;
539 NetworkList::iterator WiFiServiceMac::FindNetwork(
540 const std::string& network_guid) {
541 for (NetworkList::iterator it = networks_.begin();
542 it != networks_.end();
544 if (it->guid == network_guid)
547 return networks_.end();
550 void WiFiServiceMac::OnWlanObserverNotification() {
551 std::string connected_network_guid = GUIDFromSSID([interface_ ssid]);
552 DVLOG(1) << " *** Got Notification: " << connected_network_guid;
553 // Connected network has changed, mark previous one disconnected.
554 if (connected_network_guid != connected_network_guid_) {
555 // Update connection_state of newly connected network.
556 NetworkList::iterator it = FindNetwork(connected_network_guid_);
557 if (it != networks_.end()) {
558 it->connection_state = onc::connection_state::kNotConnected;
559 NotifyNetworkChanged(connected_network_guid_);
561 connected_network_guid_ = connected_network_guid;
564 if (!connected_network_guid.empty()) {
565 // Update connection_state of newly connected network.
566 NetworkList::iterator it = FindNetwork(connected_network_guid);
567 if (it != networks_.end()) {
568 it->connection_state = GetNetworkConnectionState(connected_network_guid);
570 // Can't find |connected_network_guid| in |networks_|, try to update it.
573 // Notify that network is connecting.
574 NotifyNetworkChanged(connected_network_guid);
575 // Further network change notification will be sent by detector.
579 void WiFiServiceMac::NotifyNetworkListChanged(const NetworkList& networks) {
580 if (network_list_changed_observer_.is_null())
583 NetworkGuidList current_networks;
584 for (NetworkList::const_iterator it = networks.begin();
585 it != networks.end();
587 current_networks.push_back(it->guid);
590 message_loop_proxy_->PostTask(
592 base::Bind(network_list_changed_observer_, current_networks));
595 void WiFiServiceMac::NotifyNetworkChanged(const std::string& network_guid) {
596 if (networks_changed_observer_.is_null())
599 DVLOG(1) << "NotifyNetworkChanged: " << network_guid;
600 NetworkGuidList changed_networks(1, network_guid);
601 message_loop_proxy_->PostTask(
603 base::Bind(networks_changed_observer_, changed_networks));
607 WiFiService* WiFiService::Create() { return new WiFiServiceMac(); }