2 * WPA Supplicant - Mac OS X Apple80211 driver interface
3 * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #define Boolean __DummyBoolean
17 #include <CoreFoundation/CoreFoundation.h>
24 #include "Apple80211.h"
26 struct wpa_driver_osx_data {
28 WirelessRef wireless_ctx;
29 CFArrayRef scan_results;
33 #ifndef CONFIG_NO_STDOUT_DEBUG
34 extern int wpa_debug_level;
36 static void dump_dict_cb(const void *key, const void *value, void *context)
38 if (MSG_DEBUG < wpa_debug_level)
41 wpa_printf(MSG_DEBUG, "Key:");
43 wpa_printf(MSG_DEBUG, "Value:");
46 #endif /* CONFIG_NO_STDOUT_DEBUG */
49 static void wpa_driver_osx_dump_dict(CFDictionaryRef dict, const char *title)
51 #ifndef CONFIG_NO_STDOUT_DEBUG
52 wpa_printf(MSG_DEBUG, "OSX: Dump dictionary %s - %u entries",
53 title, (unsigned int) CFDictionaryGetCount(dict));
54 CFDictionaryApplyFunction(dict, dump_dict_cb, NULL);
55 #endif /* CONFIG_NO_STDOUT_DEBUG */
59 static int wpa_driver_osx_get_ssid(void *priv, u8 *ssid)
61 struct wpa_driver_osx_data *drv = priv;
66 err = WirelessGetInfo(drv->wireless_ctx, &info);
68 wpa_printf(MSG_DEBUG, "OSX: WirelessGetInfo failed: %d",
73 wpa_printf(MSG_DEBUG, "OSX: Wireless device power off");
77 for (len = 0; len < 32; len++)
78 if (info.ssid[len] == 0)
81 os_memcpy(ssid, info.ssid, len);
86 static int wpa_driver_osx_get_bssid(void *priv, u8 *bssid)
88 struct wpa_driver_osx_data *drv = priv;
92 err = WirelessGetInfo(drv->wireless_ctx, &info);
94 wpa_printf(MSG_DEBUG, "OSX: WirelessGetInfo failed: %d",
99 wpa_printf(MSG_DEBUG, "OSX: Wireless device power off");
103 os_memcpy(bssid, info.bssID, ETH_ALEN);
108 static void wpa_driver_osx_scan_timeout(void *eloop_ctx, void *timeout_ctx)
110 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
114 static int wpa_driver_osx_scan(void *priv, const u8 *ssid, size_t ssid_len)
116 struct wpa_driver_osx_data *drv = priv;
119 if (drv->scan_results) {
120 CFRelease(drv->scan_results);
121 drv->scan_results = NULL;
126 data = CFStringCreateWithBytes(kCFAllocatorDefault,
128 kCFStringEncodingISOLatin1,
131 wpa_printf(MSG_DEBUG, "CFStringCreateWithBytes "
136 err = WirelessDirectedScan(drv->wireless_ctx,
137 &drv->scan_results, 0, data);
140 wpa_printf(MSG_DEBUG, "OSX: WirelessDirectedScan "
141 "failed: 0x%08x", (unsigned int) err);
145 err = WirelessScan(drv->wireless_ctx, &drv->scan_results, 0);
147 wpa_printf(MSG_DEBUG, "OSX: WirelessScan failed: "
148 "0x%08x", (unsigned int) err);
153 eloop_register_timeout(0, 0, wpa_driver_osx_scan_timeout, drv,
159 static int wpa_driver_osx_get_scan_results(void *priv,
160 struct wpa_scan_result *results,
163 struct wpa_driver_osx_data *drv = priv;
166 if (drv->scan_results == NULL)
169 num = CFArrayGetCount(drv->scan_results);
172 os_memset(results, 0, num * sizeof(struct wpa_scan_result));
174 for (i = 0; i < num; i++) {
175 struct wpa_scan_result *res = &results[i];
176 WirelessNetworkInfo *info;
177 info = (WirelessNetworkInfo *)
178 CFDataGetBytePtr(CFArrayGetValueAtIndex(
179 drv->scan_results, i));
181 os_memcpy(res->bssid, info->bssid, ETH_ALEN);
182 if (info->ssid_len > 32) {
183 wpa_printf(MSG_DEBUG, "OSX: Invalid SSID length %d in "
184 "scan results", (int) info->ssid_len);
187 os_memcpy(res->ssid, info->ssid, info->ssid_len);
188 res->ssid_len = info->ssid_len;
189 res->caps = info->capability;
190 res->freq = 2407 + info->channel * 5;
191 res->level = info->signal;
192 res->noise = info->noise;
199 static void wpa_driver_osx_assoc_timeout(void *eloop_ctx, void *timeout_ctx)
201 struct wpa_driver_osx_data *drv = eloop_ctx;
205 if (wpa_driver_osx_get_bssid(drv, bssid) != 0) {
206 eloop_register_timeout(1, 0, wpa_driver_osx_assoc_timeout,
211 ai = WirelessGetAssociationInfo(drv->wireless_ctx);
213 wpa_driver_osx_dump_dict(ai, "WirelessGetAssociationInfo");
216 wpa_printf(MSG_DEBUG, "OSX: Failed to get association info");
219 wpa_supplicant_event(timeout_ctx, EVENT_ASSOC, NULL);
223 static int wpa_driver_osx_associate(void *priv,
224 struct wpa_driver_associate_params *params)
226 struct wpa_driver_osx_data *drv = priv;
232 ssid = CFDataCreate(kCFAllocatorDefault, params->ssid,
237 /* TODO: support for WEP */
238 if (params->key_mgmt_suite == KEY_MGMT_PSK) {
239 if (params->passphrase == NULL)
241 key = CFStringCreateWithCString(kCFAllocatorDefault,
243 kCFStringEncodingISOLatin1);
251 if (params->key_mgmt_suite == KEY_MGMT_NONE)
256 wpa_printf(MSG_DEBUG, "OSX: WirelessAssociate(type=%d key=%p)",
258 err = WirelessAssociate(drv->wireless_ctx, assoc_type, ssid, key);
263 wpa_printf(MSG_DEBUG, "OSX: WirelessAssociate failed: 0x%08x",
269 * Driver is actually already associated; report association from an
272 eloop_cancel_timeout(wpa_driver_osx_assoc_timeout, drv, drv->ctx);
273 eloop_register_timeout(0, 0, wpa_driver_osx_assoc_timeout, drv,
280 static int wpa_driver_osx_set_key(void *priv, wpa_alg alg, const u8 *addr,
281 int key_idx, int set_tx, const u8 *seq,
282 size_t seq_len, const u8 *key,
285 struct wpa_driver_osx_data *drv = priv;
288 if (alg == WPA_ALG_WEP) {
289 err = WirelessSetKey(drv->wireless_ctx, 1, key_idx, key_len,
292 wpa_printf(MSG_DEBUG, "OSX: WirelessSetKey failed: "
293 "0x%08x", (unsigned int) err);
300 if (alg == WPA_ALG_PMK) {
301 err = WirelessSetWPAKey(drv->wireless_ctx, 1, key_len, key);
303 wpa_printf(MSG_DEBUG, "OSX: WirelessSetWPAKey failed: "
304 "0x%08x", (unsigned int) err);
310 wpa_printf(MSG_DEBUG, "OSX: Unsupported set_key alg %d", alg);
315 static int wpa_driver_osx_get_capa(void *priv, struct wpa_driver_capa *capa)
317 os_memset(capa, 0, sizeof(*capa));
319 capa->key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
320 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
321 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
322 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
323 capa->enc = WPA_DRIVER_CAPA_ENC_WEP40 | WPA_DRIVER_CAPA_ENC_WEP104 |
324 WPA_DRIVER_CAPA_ENC_TKIP | WPA_DRIVER_CAPA_ENC_CCMP;
325 capa->auth = WPA_DRIVER_AUTH_OPEN | WPA_DRIVER_AUTH_SHARED |
326 WPA_DRIVER_AUTH_LEAP;
327 capa->flags = WPA_DRIVER_FLAGS_4WAY_HANDSHAKE;
333 static void * wpa_driver_osx_init(void *ctx, const char *ifname)
335 struct wpa_driver_osx_data *drv;
339 if (!WirelessIsAvailable()) {
340 wpa_printf(MSG_ERROR, "OSX: No wireless interface available");
344 drv = os_zalloc(sizeof(*drv));
348 err = WirelessAttach(&drv->wireless_ctx, 0);
350 wpa_printf(MSG_ERROR, "OSX: WirelessAttach failed: %d",
356 err = WirelessGetEnabled(drv->wireless_ctx, &enabled);
358 wpa_printf(MSG_DEBUG, "OSX: WirelessGetEnabled failed: 0x%08x",
360 err = WirelessGetPower(drv->wireless_ctx, &power);
362 wpa_printf(MSG_DEBUG, "OSX: WirelessGetPower failed: 0x%08x",
365 wpa_printf(MSG_DEBUG, "OSX: Enabled=%d Power=%d", enabled, power);
368 err = WirelessSetEnabled(drv->wireless_ctx, 1);
370 wpa_printf(MSG_DEBUG, "OSX: WirelessSetEnabled failed:"
371 " 0x%08x", (unsigned int) err);
372 WirelessDetach(drv->wireless_ctx);
379 err = WirelessSetPower(drv->wireless_ctx, 1);
381 wpa_printf(MSG_DEBUG, "OSX: WirelessSetPower failed: "
382 "0x%08x", (unsigned int) err);
383 WirelessDetach(drv->wireless_ctx);
393 static void wpa_driver_osx_deinit(void *priv)
395 struct wpa_driver_osx_data *drv = priv;
398 eloop_cancel_timeout(wpa_driver_osx_scan_timeout, drv, drv->ctx);
399 eloop_cancel_timeout(wpa_driver_osx_assoc_timeout, drv, drv->ctx);
401 err = WirelessSetPower(drv->wireless_ctx, 0);
403 wpa_printf(MSG_DEBUG, "OSX: WirelessSetPower(0) failed: "
404 "0x%08x", (unsigned int) err);
407 err = WirelessDetach(drv->wireless_ctx);
409 wpa_printf(MSG_DEBUG, "OSX: WirelessDetach failed: 0x%08x",
413 if (drv->scan_results)
414 CFRelease(drv->scan_results);
420 const struct wpa_driver_ops wpa_driver_osx_ops = {
422 .desc = "Mac OS X Apple80211 driver",
423 .get_ssid = wpa_driver_osx_get_ssid,
424 .get_bssid = wpa_driver_osx_get_bssid,
425 .init = wpa_driver_osx_init,
426 .deinit = wpa_driver_osx_deinit,
427 .scan = wpa_driver_osx_scan,
428 .get_scan_results = wpa_driver_osx_get_scan_results,
429 .associate = wpa_driver_osx_associate,
430 .set_key = wpa_driver_osx_set_key,
431 .get_capa = wpa_driver_osx_get_capa,