• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * WPA Supplicant - Mac OS X Apple80211 driver interface
3 * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
4 *
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.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16#define Boolean __DummyBoolean
17#include <CoreFoundation/CoreFoundation.h>
18#undef Boolean
19
20#include "common.h"
21#include "driver.h"
22#include "eloop.h"
23
24#include "Apple80211.h"
25
26struct wpa_driver_osx_data {
27	void *ctx;
28	WirelessRef wireless_ctx;
29	CFArrayRef scan_results;
30};
31
32
33#ifndef CONFIG_NO_STDOUT_DEBUG
34extern int wpa_debug_level;
35
36static void dump_dict_cb(const void *key, const void *value, void *context)
37{
38        if (MSG_DEBUG < wpa_debug_level)
39                return;
40
41	wpa_printf(MSG_DEBUG, "Key:");
42	CFShow(key);
43	wpa_printf(MSG_DEBUG, "Value:");
44	CFShow(value);
45}
46#endif /* CONFIG_NO_STDOUT_DEBUG */
47
48
49static void wpa_driver_osx_dump_dict(CFDictionaryRef dict, const char *title)
50{
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 */
56}
57
58
59static int wpa_driver_osx_get_ssid(void *priv, u8 *ssid)
60{
61	struct wpa_driver_osx_data *drv = priv;
62	WirelessError err;
63	WirelessInfo info;
64	int len;
65
66	err = WirelessGetInfo(drv->wireless_ctx, &info);
67	if (err) {
68		wpa_printf(MSG_DEBUG, "OSX: WirelessGetInfo failed: %d",
69			   (int) err);
70		return -1;
71	}
72	if (!info.power) {
73		wpa_printf(MSG_DEBUG, "OSX: Wireless device power off");
74		return -1;
75	}
76
77	for (len = 0; len < 32; len++)
78		if (info.ssid[len] == 0)
79			break;
80
81	os_memcpy(ssid, info.ssid, len);
82	return len;
83}
84
85
86static int wpa_driver_osx_get_bssid(void *priv, u8 *bssid)
87{
88	struct wpa_driver_osx_data *drv = priv;
89	WirelessError err;
90	WirelessInfo info;
91
92	err = WirelessGetInfo(drv->wireless_ctx, &info);
93	if (err) {
94		wpa_printf(MSG_DEBUG, "OSX: WirelessGetInfo failed: %d",
95			   (int) err);
96		return -1;
97	}
98	if (!info.power) {
99		wpa_printf(MSG_DEBUG, "OSX: Wireless device power off");
100		return -1;
101	}
102
103	os_memcpy(bssid, info.bssID, ETH_ALEN);
104	return 0;
105}
106
107
108static void wpa_driver_osx_scan_timeout(void *eloop_ctx, void *timeout_ctx)
109{
110	wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
111}
112
113
114static int wpa_driver_osx_scan(void *priv, const u8 *ssid, size_t ssid_len)
115{
116	struct wpa_driver_osx_data *drv = priv;
117	WirelessError err;
118
119	if (drv->scan_results) {
120		CFRelease(drv->scan_results);
121		drv->scan_results = NULL;
122	}
123
124	if (ssid) {
125		CFStringRef data;
126		data = CFStringCreateWithBytes(kCFAllocatorDefault,
127					       ssid, ssid_len,
128					       kCFStringEncodingISOLatin1,
129					       FALSE);
130		if (data == NULL) {
131			wpa_printf(MSG_DEBUG, "CFStringCreateWithBytes "
132				   "failed");
133			return -1;
134		}
135
136		err = WirelessDirectedScan(drv->wireless_ctx,
137					   &drv->scan_results, 0, data);
138		CFRelease(data);
139		if (err) {
140			wpa_printf(MSG_DEBUG, "OSX: WirelessDirectedScan "
141				   "failed: 0x%08x", (unsigned int) err);
142			return -1;
143		}
144	} else {
145		err = WirelessScan(drv->wireless_ctx, &drv->scan_results, 0);
146		if (err) {
147			wpa_printf(MSG_DEBUG, "OSX: WirelessScan failed: "
148				   "0x%08x", (unsigned int) err);
149			return -1;
150		}
151	}
152
153	eloop_register_timeout(0, 0, wpa_driver_osx_scan_timeout, drv,
154			       drv->ctx);
155	return 0;
156}
157
158
159static int wpa_driver_osx_get_scan_results(void *priv,
160					   struct wpa_scan_result *results,
161					   size_t max_size)
162{
163	struct wpa_driver_osx_data *drv = priv;
164	size_t i, num;
165
166	if (drv->scan_results == NULL)
167		return 0;
168
169	num = CFArrayGetCount(drv->scan_results);
170	if (num > max_size)
171		num = max_size;
172	os_memset(results, 0, num * sizeof(struct wpa_scan_result));
173
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));
180
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);
185			continue;
186		}
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;
193	}
194
195	return num;
196}
197
198
199static void wpa_driver_osx_assoc_timeout(void *eloop_ctx, void *timeout_ctx)
200{
201	struct wpa_driver_osx_data *drv = eloop_ctx;
202	u8 bssid[ETH_ALEN];
203	CFDictionaryRef ai;
204
205	if (wpa_driver_osx_get_bssid(drv, bssid) != 0) {
206		eloop_register_timeout(1, 0, wpa_driver_osx_assoc_timeout,
207				       drv, drv->ctx);
208		return;
209	}
210
211	ai = WirelessGetAssociationInfo(drv->wireless_ctx);
212	if (ai) {
213		wpa_driver_osx_dump_dict(ai, "WirelessGetAssociationInfo");
214		CFRelease(ai);
215	} else {
216		wpa_printf(MSG_DEBUG, "OSX: Failed to get association info");
217	}
218
219	wpa_supplicant_event(timeout_ctx, EVENT_ASSOC, NULL);
220}
221
222
223static int wpa_driver_osx_associate(void *priv,
224				    struct wpa_driver_associate_params *params)
225{
226	struct wpa_driver_osx_data *drv = priv;
227	WirelessError err;
228	CFDataRef ssid;
229	CFStringRef key;
230	int assoc_type;
231
232	ssid = CFDataCreate(kCFAllocatorDefault, params->ssid,
233			    params->ssid_len);
234	if (ssid == NULL)
235		return -1;
236
237	/* TODO: support for WEP */
238	if (params->key_mgmt_suite == KEY_MGMT_PSK) {
239		if (params->passphrase == NULL)
240			return -1;
241		key = CFStringCreateWithCString(kCFAllocatorDefault,
242						params->passphrase,
243						kCFStringEncodingISOLatin1);
244		if (key == NULL) {
245			CFRelease(ssid);
246			return -1;
247		}
248	} else
249		key = NULL;
250
251	if (params->key_mgmt_suite == KEY_MGMT_NONE)
252		assoc_type = 0;
253	else
254		assoc_type = 4;
255
256	wpa_printf(MSG_DEBUG, "OSX: WirelessAssociate(type=%d key=%p)",
257		   assoc_type, key);
258	err = WirelessAssociate(drv->wireless_ctx, assoc_type, ssid, key);
259	CFRelease(ssid);
260	if (key)
261		CFRelease(key);
262	if (err) {
263		wpa_printf(MSG_DEBUG, "OSX: WirelessAssociate failed: 0x%08x",
264			   (unsigned int) err);
265		return -1;
266	}
267
268	/*
269	 * Driver is actually already associated; report association from an
270	 * eloop callback.
271	 */
272	eloop_cancel_timeout(wpa_driver_osx_assoc_timeout, drv, drv->ctx);
273	eloop_register_timeout(0, 0, wpa_driver_osx_assoc_timeout, drv,
274			       drv->ctx);
275
276	return 0;
277}
278
279
280static 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,
283				  size_t key_len)
284{
285	struct wpa_driver_osx_data *drv = priv;
286	WirelessError err;
287
288	if (alg == WPA_ALG_WEP) {
289		err = WirelessSetKey(drv->wireless_ctx, 1, key_idx, key_len,
290				     key);
291		if (err != 0) {
292			wpa_printf(MSG_DEBUG, "OSX: WirelessSetKey failed: "
293				   "0x%08x", (unsigned int) err);
294			return -1;
295		}
296
297		return 0;
298	}
299
300	if (alg == WPA_ALG_PMK) {
301		err = WirelessSetWPAKey(drv->wireless_ctx, 1, key_len, key);
302		if (err != 0) {
303			wpa_printf(MSG_DEBUG, "OSX: WirelessSetWPAKey failed: "
304				   "0x%08x", (unsigned int) err);
305			return -1;
306		}
307		return 0;
308	}
309
310	wpa_printf(MSG_DEBUG, "OSX: Unsupported set_key alg %d", alg);
311	return -1;
312}
313
314
315static int wpa_driver_osx_get_capa(void *priv, struct wpa_driver_capa *capa)
316{
317	os_memset(capa, 0, sizeof(*capa));
318
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;
328
329	return 0;
330}
331
332
333static void * wpa_driver_osx_init(void *ctx, const char *ifname)
334{
335	struct wpa_driver_osx_data *drv;
336	WirelessError err;
337	u8 enabled, power;
338
339	if (!WirelessIsAvailable()) {
340		wpa_printf(MSG_ERROR, "OSX: No wireless interface available");
341		return NULL;
342	}
343
344	drv = os_zalloc(sizeof(*drv));
345	if (drv == NULL)
346		return NULL;
347	drv->ctx = ctx;
348	err = WirelessAttach(&drv->wireless_ctx, 0);
349	if (err) {
350		wpa_printf(MSG_ERROR, "OSX: WirelessAttach failed: %d",
351			   (int) err);
352		os_free(drv);
353		return NULL;
354	}
355
356	err = WirelessGetEnabled(drv->wireless_ctx, &enabled);
357	if (err)
358		wpa_printf(MSG_DEBUG, "OSX: WirelessGetEnabled failed: 0x%08x",
359			   (unsigned int) err);
360	err = WirelessGetPower(drv->wireless_ctx, &power);
361	if (err)
362		wpa_printf(MSG_DEBUG, "OSX: WirelessGetPower failed: 0x%08x",
363			   (unsigned int) err);
364
365	wpa_printf(MSG_DEBUG, "OSX: Enabled=%d Power=%d", enabled, power);
366
367	if (!enabled) {
368		err = WirelessSetEnabled(drv->wireless_ctx, 1);
369		if (err) {
370			wpa_printf(MSG_DEBUG, "OSX: WirelessSetEnabled failed:"
371				   " 0x%08x", (unsigned int) err);
372			WirelessDetach(drv->wireless_ctx);
373			os_free(drv);
374			return NULL;
375		}
376	}
377
378	if (!power) {
379		err = WirelessSetPower(drv->wireless_ctx, 1);
380		if (err) {
381			wpa_printf(MSG_DEBUG, "OSX: WirelessSetPower failed: "
382				   "0x%08x", (unsigned int) err);
383			WirelessDetach(drv->wireless_ctx);
384			os_free(drv);
385			return NULL;
386		}
387	}
388
389	return drv;
390}
391
392
393static void wpa_driver_osx_deinit(void *priv)
394{
395	struct wpa_driver_osx_data *drv = priv;
396	WirelessError err;
397
398	eloop_cancel_timeout(wpa_driver_osx_scan_timeout, drv, drv->ctx);
399	eloop_cancel_timeout(wpa_driver_osx_assoc_timeout, drv, drv->ctx);
400
401	err = WirelessSetPower(drv->wireless_ctx, 0);
402	if (err) {
403		wpa_printf(MSG_DEBUG, "OSX: WirelessSetPower(0) failed: "
404			   "0x%08x", (unsigned int) err);
405	}
406
407	err = WirelessDetach(drv->wireless_ctx);
408	if (err) {
409		wpa_printf(MSG_DEBUG, "OSX: WirelessDetach failed: 0x%08x",
410			   (unsigned int) err);
411	}
412
413	if (drv->scan_results)
414		CFRelease(drv->scan_results);
415
416	os_free(drv);
417}
418
419
420const struct wpa_driver_ops wpa_driver_osx_ops = {
421	.name = "osx",
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,
432};
433