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>
23 #include "common/ieee802_11_defs.h"
25 #include "Apple80211.h"
27 struct wpa_driver_osx_data {
29 WirelessRef wireless_ctx;
30 CFArrayRef scan_results;
34 #ifndef CONFIG_NO_STDOUT_DEBUG
35 extern int wpa_debug_level;
37 static void dump_dict_cb(const void *key, const void *value, void *context)
39 if (MSG_DEBUG < wpa_debug_level)
42 wpa_printf(MSG_DEBUG, "Key:");
44 wpa_printf(MSG_DEBUG, "Value:");
47 #endif /* CONFIG_NO_STDOUT_DEBUG */
50 static void wpa_driver_osx_dump_dict(CFDictionaryRef dict, const char *title)
52 #ifndef CONFIG_NO_STDOUT_DEBUG
53 wpa_printf(MSG_DEBUG, "OSX: Dump dictionary %s - %u entries",
54 title, (unsigned int) CFDictionaryGetCount(dict));
55 CFDictionaryApplyFunction(dict, dump_dict_cb, NULL);
56 #endif /* CONFIG_NO_STDOUT_DEBUG */
60 static int wpa_driver_osx_get_ssid(void *priv, u8 *ssid)
62 struct wpa_driver_osx_data *drv = priv;
67 err = WirelessGetInfo(drv->wireless_ctx, &info);
69 wpa_printf(MSG_DEBUG, "OSX: WirelessGetInfo failed: %d",
74 wpa_printf(MSG_DEBUG, "OSX: Wireless device power off");
78 for (len = 0; len < 32; len++)
79 if (info.ssid[len] == 0)
82 os_memcpy(ssid, info.ssid, len);
87 static int wpa_driver_osx_get_bssid(void *priv, u8 *bssid)
89 struct wpa_driver_osx_data *drv = priv;
93 err = WirelessGetInfo(drv->wireless_ctx, &info);
95 wpa_printf(MSG_DEBUG, "OSX: WirelessGetInfo failed: %d",
100 wpa_printf(MSG_DEBUG, "OSX: Wireless device power off");
104 os_memcpy(bssid, info.bssID, ETH_ALEN);
109 static void wpa_driver_osx_scan_timeout(void *eloop_ctx, void *timeout_ctx)
111 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
115 static int wpa_driver_osx_scan(void *priv, struct wpa_driver_scan_params *params)
117 struct wpa_driver_osx_data *drv = priv;
119 const u8 *ssid = params->ssids[0].ssid;
120 size_t ssid_len = params->ssids[0].ssid_len;
122 if (drv->scan_results) {
123 CFRelease(drv->scan_results);
124 drv->scan_results = NULL;
129 data = CFStringCreateWithBytes(kCFAllocatorDefault,
131 kCFStringEncodingISOLatin1,
134 wpa_printf(MSG_DEBUG, "CFStringCreateWithBytes "
139 err = WirelessDirectedScan(drv->wireless_ctx,
140 &drv->scan_results, 0, data);
143 wpa_printf(MSG_DEBUG, "OSX: WirelessDirectedScan "
144 "failed: 0x%08x", (unsigned int) err);
148 err = WirelessScan(drv->wireless_ctx, &drv->scan_results, 0);
150 wpa_printf(MSG_DEBUG, "OSX: WirelessScan failed: "
151 "0x%08x", (unsigned int) err);
156 eloop_register_timeout(0, 0, wpa_driver_osx_scan_timeout, drv,
162 static void wpa_driver_osx_add_scan_entry(struct wpa_scan_results *res,
163 WirelessNetworkInfo *info)
165 struct wpa_scan_res *result, **tmp;
169 extra_len = 2 + info->ssid_len;
171 result = os_zalloc(sizeof(*result) + extra_len);
174 os_memcpy(result->bssid, info->bssid, ETH_ALEN);
175 result->freq = 2407 + info->channel * 5;
176 //result->beacon_int =;
177 result->caps = info->capability;
178 //result->qual = info->signal;
179 result->noise = info->noise;
181 pos = (u8 *)(result + 1);
183 *pos++ = WLAN_EID_SSID;
184 *pos++ = info->ssid_len;
185 os_memcpy(pos, info->ssid, info->ssid_len);
186 pos += info->ssid_len;
188 result->ie_len = pos - (u8 *)(result + 1);
190 tmp = os_realloc(res->res,
191 (res->num + 1) * sizeof(struct wpa_scan_res *));
196 tmp[res->num++] = result;
201 static struct wpa_scan_results * wpa_driver_osx_get_scan_results(void *priv)
203 struct wpa_driver_osx_data *drv = priv;
204 struct wpa_scan_results *res;
207 if (drv->scan_results == NULL)
210 num = CFArrayGetCount(drv->scan_results);
212 res = os_zalloc(sizeof(*res));
216 for (i = 0; i < num; i++)
217 wpa_driver_osx_add_scan_entry(res, (WirelessNetworkInfo *)
218 CFDataGetBytePtr(CFArrayGetValueAtIndex(
219 drv->scan_results, i)));
225 static void wpa_driver_osx_assoc_timeout(void *eloop_ctx, void *timeout_ctx)
227 struct wpa_driver_osx_data *drv = eloop_ctx;
231 if (wpa_driver_osx_get_bssid(drv, bssid) != 0) {
232 eloop_register_timeout(1, 0, wpa_driver_osx_assoc_timeout,
237 ai = WirelessGetAssociationInfo(drv->wireless_ctx);
239 wpa_driver_osx_dump_dict(ai, "WirelessGetAssociationInfo");
242 wpa_printf(MSG_DEBUG, "OSX: Failed to get association info");
245 wpa_supplicant_event(timeout_ctx, EVENT_ASSOC, NULL);
249 static int wpa_driver_osx_associate(void *priv,
250 struct wpa_driver_associate_params *params)
252 struct wpa_driver_osx_data *drv = priv;
258 ssid = CFDataCreate(kCFAllocatorDefault, params->ssid,
263 /* TODO: support for WEP */
264 if (params->key_mgmt_suite == KEY_MGMT_PSK) {
265 if (params->passphrase == NULL)
267 key = CFStringCreateWithCString(kCFAllocatorDefault,
269 kCFStringEncodingISOLatin1);
277 if (params->key_mgmt_suite == KEY_MGMT_NONE)
282 wpa_printf(MSG_DEBUG, "OSX: WirelessAssociate(type=%d key=%p)",
284 err = WirelessAssociate(drv->wireless_ctx, assoc_type, ssid, key);
289 wpa_printf(MSG_DEBUG, "OSX: WirelessAssociate failed: 0x%08x",
295 * Driver is actually already associated; report association from an
298 eloop_cancel_timeout(wpa_driver_osx_assoc_timeout, drv, drv->ctx);
299 eloop_register_timeout(0, 0, wpa_driver_osx_assoc_timeout, drv,
306 static int wpa_driver_osx_set_key(const char *ifname, void *priv,
307 enum wpa_alg alg, const u8 *addr,
308 int key_idx, int set_tx, const u8 *seq,
309 size_t seq_len, const u8 *key,
312 struct wpa_driver_osx_data *drv = priv;
315 if (alg == WPA_ALG_WEP) {
316 err = WirelessSetKey(drv->wireless_ctx, 1, key_idx, key_len,
319 wpa_printf(MSG_DEBUG, "OSX: WirelessSetKey failed: "
320 "0x%08x", (unsigned int) err);
327 if (alg == WPA_ALG_PMK) {
328 err = WirelessSetWPAKey(drv->wireless_ctx, 1, key_len, key);
330 wpa_printf(MSG_DEBUG, "OSX: WirelessSetWPAKey failed: "
331 "0x%08x", (unsigned int) err);
337 wpa_printf(MSG_DEBUG, "OSX: Unsupported set_key alg %d", alg);
342 static int wpa_driver_osx_get_capa(void *priv, struct wpa_driver_capa *capa)
344 os_memset(capa, 0, sizeof(*capa));
346 capa->key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
347 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
348 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
349 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
350 capa->enc = WPA_DRIVER_CAPA_ENC_WEP40 | WPA_DRIVER_CAPA_ENC_WEP104 |
351 WPA_DRIVER_CAPA_ENC_TKIP | WPA_DRIVER_CAPA_ENC_CCMP;
352 capa->auth = WPA_DRIVER_AUTH_OPEN | WPA_DRIVER_AUTH_SHARED |
353 WPA_DRIVER_AUTH_LEAP;
354 capa->flags = WPA_DRIVER_FLAGS_4WAY_HANDSHAKE;
360 static void * wpa_driver_osx_init(void *ctx, const char *ifname)
362 struct wpa_driver_osx_data *drv;
366 if (!WirelessIsAvailable()) {
367 wpa_printf(MSG_ERROR, "OSX: No wireless interface available");
371 drv = os_zalloc(sizeof(*drv));
375 err = WirelessAttach(&drv->wireless_ctx, 0);
377 wpa_printf(MSG_ERROR, "OSX: WirelessAttach failed: %d",
383 err = WirelessGetEnabled(drv->wireless_ctx, &enabled);
385 wpa_printf(MSG_DEBUG, "OSX: WirelessGetEnabled failed: 0x%08x",
387 err = WirelessGetPower(drv->wireless_ctx, &power);
389 wpa_printf(MSG_DEBUG, "OSX: WirelessGetPower failed: 0x%08x",
392 wpa_printf(MSG_DEBUG, "OSX: Enabled=%d Power=%d", enabled, power);
395 err = WirelessSetEnabled(drv->wireless_ctx, 1);
397 wpa_printf(MSG_DEBUG, "OSX: WirelessSetEnabled failed:"
398 " 0x%08x", (unsigned int) err);
399 WirelessDetach(drv->wireless_ctx);
406 err = WirelessSetPower(drv->wireless_ctx, 1);
408 wpa_printf(MSG_DEBUG, "OSX: WirelessSetPower failed: "
409 "0x%08x", (unsigned int) err);
410 WirelessDetach(drv->wireless_ctx);
420 static void wpa_driver_osx_deinit(void *priv)
422 struct wpa_driver_osx_data *drv = priv;
425 eloop_cancel_timeout(wpa_driver_osx_scan_timeout, drv, drv->ctx);
426 eloop_cancel_timeout(wpa_driver_osx_assoc_timeout, drv, drv->ctx);
428 err = WirelessSetPower(drv->wireless_ctx, 0);
430 wpa_printf(MSG_DEBUG, "OSX: WirelessSetPower(0) failed: "
431 "0x%08x", (unsigned int) err);
434 err = WirelessDetach(drv->wireless_ctx);
436 wpa_printf(MSG_DEBUG, "OSX: WirelessDetach failed: 0x%08x",
440 if (drv->scan_results)
441 CFRelease(drv->scan_results);
447 const struct wpa_driver_ops wpa_driver_osx_ops = {
449 .desc = "Mac OS X Apple80211 driver",
450 .get_ssid = wpa_driver_osx_get_ssid,
451 .get_bssid = wpa_driver_osx_get_bssid,
452 .init = wpa_driver_osx_init,
453 .deinit = wpa_driver_osx_deinit,
454 .scan2 = wpa_driver_osx_scan,
455 .get_scan_results2 = wpa_driver_osx_get_scan_results,
456 .associate = wpa_driver_osx_associate,
457 .set_key = wpa_driver_osx_set_key,
458 .get_capa = wpa_driver_osx_get_capa,