aboutsummaryrefslogtreecommitdiffstats
path: root/src/drivers/driver_iphone.m
blob: 8213fdacc32e8482487193c04e174103c08c9f07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * WPA Supplicant - iPhone/iPod touch Apple80211 driver interface
 * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Alternatively, this software may be distributed under the terms of BSD
 * license.
 *
 * See README and COPYING for more details.
 */

#include "includes.h"
#define Boolean __DummyBoolean
#include <CoreFoundation/CoreFoundation.h>
#undef Boolean

#include "common.h"
#include "driver.h"
#include "eloop.h"
#include "common/ieee802_11_defs.h"

#include "MobileApple80211.h"

struct wpa_driver_iphone_data {
	void *ctx;
	Apple80211Ref wireless_ctx;
	CFArrayRef scan_results;
	int ctrl_power;
};


static const void * cfdict_get_key_str(CFDictionaryRef dict, const char *key)
{
	const void *res;
	CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, key,
						    kCFStringEncodingMacRoman);
	if (str == NULL)
		return NULL;

	res = CFDictionaryGetValue(dict, str);
	CFRelease(str);
	return res;
}


static int wpa_driver_iphone_get_ssid(void *priv, u8 *ssid)
{
	struct wpa_driver_iphone_data *drv = priv;
	CFDataRef data;
	int err, len;

	err = Apple80211CopyValue(drv->wireless_ctx, APPLE80211_VALUE_SSID, 0,
				  &data);
	if (err != 0) {
		wpa_printf(MSG_DEBUG, "iPhone: Apple80211CopyValue(SSID) "
			   "failed: %d", err);
		return -1;
	}

	len = CFDataGetLength(data);
	if (len > 32) {
		CFRelease(data);
		return -1;
	}
	os_memcpy(ssid, CFDataGetBytePtr(data), len);
	CFRelease(data);

	return len;
}


static int wpa_driver_iphone_get_bssid(void *priv, u8 *bssid)
{
	struct wpa_driver_iphone_data *drv = priv;
	CFStringRef data;
	int err;
	int a1, a2, a3, a4, a5, a6;

	err = Apple80211CopyValue(drv->wireless_ctx, APPLE80211_VALUE_BSSID, 0,
				  &data);
	if (err != 0) {
		wpa_printf(MSG_DEBUG, "iPhone: Apple80211CopyValue(BSSID) "
			   "failed: %d", err);
		return -1;
	}

	sscanf(CFStringGetCStringPtr(data, kCFStringEncodingMacRoman),
	       "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6);
	bssid[0] = a1;
	bssid[1] = a2;
	bssid[2] = a3;
	bssid[3] = a4;
	bssid[4] = a5;
	bssid[5] = a6;

	CFRelease(data);

	return 0;
}


static void wpa_driver_iphone_scan_timeout(void *eloop_ctx, void *timeout_ctx)
{
	wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
}


static int wpa_driver_iphone_scan(void *priv, const u8 *ssid, size_t ssid_len)
{
	struct wpa_driver_iphone_data *drv = priv;
	int err;

	if (drv->scan_results) {
		CFRelease(drv->scan_results);
		drv->scan_results = NULL;
	}

	err = Apple80211Scan(drv->wireless_ctx, &drv->scan_results, NULL);
	if (err) {
		wpa_printf(MSG_DEBUG, "iPhone: Apple80211Scan failed: %d",
			   err);
		return -1;
	}

	eloop_register_timeout(0, 0, wpa_driver_iphone_scan_timeout, drv,
			       drv->ctx);
	return 0;
}


static int wpa_driver_iphone_get_scan_results(void *priv,
					      struct wpa_scan_result *results,
					      size_t max_size)
{
	struct wpa_driver_iphone_data *drv = priv;
	size_t i, num;

	if (drv->scan_results == NULL)
		return 0;

	num = CFArrayGetCount(drv->scan_results);
	if (num > max_size)
		num = max_size;
	os_memset(results, 0, num * sizeof(struct wpa_scan_result));

	for (i = 0; i < num; i++) {
		struct wpa_scan_result *res = &results[i];
		CFDictionaryRef dict =
			CFArrayGetValueAtIndex(drv->scan_results, i);
		CFDataRef data;
		CFStringRef str;
		CFNumberRef num;
		int val;

		data = cfdict_get_key_str(dict, "SSID");
		if (data) {
			res->ssid_len = CFDataGetLength(data);
			if (res->ssid_len > 32)
				res->ssid_len = 32;
			os_memcpy(res->ssid, CFDataGetBytePtr(data),
				  res->ssid_len);
		}

		str = cfdict_get_key_str(dict, "BSSID");
		if (str) {
			int a1, a2, a3, a4, a5, a6;
			sscanf(CFStringGetCStringPtr(
				       str, kCFStringEncodingMacRoman),
			       "%x:%x:%x:%x:%x:%x",
			       &a1, &a2, &a3, &a4, &a5, &a6);
			res->bssid[0] = a1;
			res->bssid[1] = a2;
			res->bssid[2] = a3;
			res->bssid[3] = a4;
			res->bssid[4] = a5;
			res->bssid[5] = a6;
		}

		num = cfdict_get_key_str(dict, "CAPABILITIES");
		if (num) {
			if (CFNumberGetValue(num, kCFNumberSInt32Type, &val))
				res->caps = val;
		}

		num = cfdict_get_key_str(dict, "CHANNEL");
		if (num) {
			if (CFNumberGetValue(num, kCFNumberSInt32Type, &val))
				res->freq = 2407 + val * 5;
		}

		num = cfdict_get_key_str(dict, "RSSI");
		if (num) {
			if (CFNumberGetValue(num, kCFNumberSInt32Type, &val))
				res->level = val;
		}

		num = cfdict_get_key_str(dict, "NOISE");
		if (num) {
			if (CFNumberGetValue(num, kCFNumberSInt32Type, &val))
				res->noise = val;
		}

		data = cfdict_get_key_str(dict, "IE");
		if (data) {
			u8 *ptr = (u8 *) CFDataGetBytePtr(data);
			int len = CFDataGetLength(data);
			u8 *pos = ptr, *end = ptr + len;

			while (pos + 2 < end) {
				if (pos + 2 + pos[1] > end)
					break;
				if (pos[0] == WLAN_EID_RSN &&
				    pos[1] <= SSID_MAX_WPA_IE_LEN) {
					os_memcpy(res->rsn_ie, pos,
						  2 + pos[1]);
					res->rsn_ie_len = 2 + pos[1];
				}
				if (pos[0] == WLAN_EID_VENDOR_SPECIFIC &&
				    pos[1] > 4 && pos[2] == 0x00 &&
				    pos[3] == 0x50 && pos[4] == 0xf2 &&
				    pos[5] == 0x01) {
					os_memcpy(res->wpa_ie, pos,
						  2 + pos[1]);
					res->wpa_ie_len = 2 + pos[1];
				}

				pos = pos + 2 + pos[1];
			}
		}
	}

	return num;
}


static void wpa_driver_iphone_assoc_timeout(void *eloop_ctx, void *timeout_ctx)
{
	struct wpa_driver_iphone_data *drv = eloop_ctx;
	u8 bssid[ETH_ALEN];

	if (wpa_driver_iphone_get_bssid(drv, bssid) != 0) {
		eloop_register_timeout(1, 0, wpa_driver_iphone_assoc_timeout,
				       drv, drv->ctx);
		return;
	}

	wpa_supplicant_event(timeout_ctx, EVENT_ASSOC, NULL);
}


static int wpa_driver_iphone_associate(
	void *priv, struct wpa_driver_associate_params *params)
{
	struct wpa_driver_iphone_data *drv = priv;
	int i, num, err;
	size_t ssid_len;
	CFDictionaryRef bss = NULL;

	/*
	 * TODO: Consider generating parameters instead of just using an entry
	 * from scan results in order to support ap_scan=2.
	 */

	if (drv->scan_results == NULL) {
		wpa_printf(MSG_DEBUG, "iPhone: No scan results - cannot "
			   "associate");
		return -1;
	}

	num = CFArrayGetCount(drv->scan_results);

	for (i = 0; i < num; i++) {
		CFDictionaryRef dict =
			CFArrayGetValueAtIndex(drv->scan_results, i);
		CFDataRef data;

		data = cfdict_get_key_str(dict, "SSID");
		if (data == NULL)
			continue;

		ssid_len = CFDataGetLength(data);
		if (ssid_len != params->ssid_len ||
		    os_memcmp(CFDataGetBytePtr(data), params->ssid, ssid_len)
		    != 0)
			continue;

		bss = dict;
		break;
	}

	if (bss == NULL) {
		wpa_printf(MSG_DEBUG, "iPhone: Could not find SSID from scan "
			   "results - cannot associate");
		return -1;
	}

	wpa_printf(MSG_DEBUG, "iPhone: Trying to associate with a BSS found "
		   "from scan results");

	err = Apple80211Associate(drv->wireless_ctx, bss, NULL);
	if (err) {
		wpa_printf(MSG_DEBUG, "iPhone: Apple80211Associate() failed: "
			   "%d", err);
		return -1;
	}

	/*
	 * Driver is actually already associated; report association from an
	 * eloop callback.
	 */
	eloop_cancel_timeout(wpa_driver_iphone_assoc_timeout, drv, drv->ctx);
	eloop_register_timeout(0, 0, wpa_driver_iphone_assoc_timeout, drv,
			       drv->ctx);

	return 0;
}


static int wpa_driver_iphone_set_key(void *priv, wpa_alg alg, const u8 *addr,
				     int key_idx, int set_tx, const u8 *seq,
				     size_t seq_len, const u8 *key,
				     size_t key_len)
{
	/*
	 * TODO: Need to either support configuring PMK for 4-way handshake or
	 * PTK for TKIP/CCMP.
	 */
	return -1;
}


static int wpa_driver_iphone_get_capa(void *priv, struct wpa_driver_capa *capa)
{
	os_memset(capa, 0, sizeof(*capa));

	capa->key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
		WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
		WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
		WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
	capa->enc = WPA_DRIVER_CAPA_ENC_WEP40 | WPA_DRIVER_CAPA_ENC_WEP104 |
		WPA_DRIVER_CAPA_ENC_TKIP | WPA_DRIVER_CAPA_ENC_CCMP;
	capa->auth = WPA_DRIVER_AUTH_OPEN | WPA_DRIVER_AUTH_SHARED |
		WPA_DRIVER_AUTH_LEAP;
	capa->flags = WPA_DRIVER_FLAGS_4WAY_HANDSHAKE;

	return 0;
}


static void * wpa_driver_iphone_init(void *ctx, const char *ifname)
{
	struct wpa_driver_iphone_data *drv;
	int err;
	char power;
	CFStringRef name;
	CFDictionaryRef dict;

	drv = os_zalloc(sizeof(*drv));
	if (drv == NULL)
		return NULL;
	drv->ctx = ctx;
	err = Apple80211Open(&drv->wireless_ctx);
	if (err) {
		wpa_printf(MSG_ERROR, "iPhone: Apple80211Open failed: %d",
			   err);
		os_free(drv);
		return NULL;
	}

	name = CFStringCreateWithCString(kCFAllocatorDefault, ifname,
					 kCFStringEncodingISOLatin1);
	if (name == NULL) {
		wpa_printf(MSG_ERROR, "iPhone: ifname -> CFString failed");
		Apple80211Close(drv->wireless_ctx);
		os_free(drv);
		return NULL;
	}

	err = Apple80211BindToInterface(drv->wireless_ctx, name);
	CFRelease(name);

	if (err) {
		wpa_printf(MSG_ERROR, "iPhone: Apple80211BindToInterface "
			   "failed: %d", err);
		Apple80211Close(drv->wireless_ctx);
		os_free(drv);
		return NULL;
	}

	err = Apple80211GetPower(drv->wireless_ctx, &power);
	if (err)
		wpa_printf(MSG_DEBUG, "iPhone: Apple80211GetPower failed: %d",
			   err);

	wpa_printf(MSG_DEBUG, "iPhone: Power=%d", power);

	if (!power) {
		drv->ctrl_power = 1;
		err = Apple80211SetPower(drv->wireless_ctx, 1);
		if (err) {
			wpa_printf(MSG_DEBUG, "iPhone: Apple80211SetPower "
				   "failed: %d", err);
			Apple80211Close(drv->wireless_ctx);
			os_free(drv);
			return NULL;
		}
	}

	err = Apple80211GetInfoCopy(drv->wireless_ctx, &dict);
	if (err == 0) {
		CFShow(dict);
		CFRelease(dict);
	} else {
		printf("Apple80211GetInfoCopy: %d\n", err);
	}

	return drv;
}


static void wpa_driver_iphone_deinit(void *priv)
{
	struct wpa_driver_iphone_data *drv = priv;
	int err;

	eloop_cancel_timeout(wpa_driver_iphone_scan_timeout, drv, drv->ctx);
	eloop_cancel_timeout(wpa_driver_iphone_assoc_timeout, drv, drv->ctx);

	if (drv->ctrl_power) {
		wpa_printf(MSG_DEBUG, "iPhone: Power down the interface");
		err = Apple80211SetPower(drv->wireless_ctx, 0);
		if (err) {
			wpa_printf(MSG_DEBUG, "iPhone: Apple80211SetPower(0) "
				   "failed: %d", err);
		}
	}

	err = Apple80211Close(drv->wireless_ctx);
	if (err) {
		wpa_printf(MSG_DEBUG, "iPhone: Apple80211Close failed: %d",
			   err);
	}

	if (drv->scan_results)
		CFRelease(drv->scan_results);

	os_free(drv);
}


const struct wpa_driver_ops wpa_driver_iphone_ops = {
	.name = "iphone",
	.desc = "iPhone/iPod touch Apple80211 driver",
	.get_ssid = wpa_driver_iphone_get_ssid,
	.get_bssid = wpa_driver_iphone_get_bssid,
	.init = wpa_driver_iphone_init,
	.deinit = wpa_driver_iphone_deinit,
	.scan = wpa_driver_iphone_scan,
	.get_scan_results = wpa_driver_iphone_get_scan_results,
	.associate = wpa_driver_iphone_associate,
	.set_key = wpa_driver_iphone_set_key,
	.get_capa = wpa_driver_iphone_get_capa,
};