-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver_madwifi.c
567 lines (496 loc) · 15.5 KB
/
driver_madwifi.c
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
* WPA Supplicant - driver interaction with MADWIFI 802.11 driver
* Copyright (c) 2004, Sam Leffler <[email protected]>
* Copyright (c) 2004-2005, Jouni Malinen <[email protected]>
*
* 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"
#include <sys/ioctl.h>
#include "common.h"
#include "driver.h"
#include "driver_wext.h"
#include "eloop.h"
#include "wpa_supplicant.h"
#include "wpa.h"
#include "wireless_copy.h"
#include <include/compat.h>
#include <net80211/ieee80211.h>
#ifdef WME_NUM_AC
/* Assume this is built against BSD branch of madwifi driver. */
#define MADWIFI_BSD
#include <net80211/_ieee80211.h>
#endif /* WME_NUM_AC */
#include <net80211/ieee80211_crypto.h>
#include <net80211/ieee80211_ioctl.h>
#ifdef IEEE80211_IOCTL_SETWMMPARAMS
/* Assume this is built against madwifi-ng */
#define MADWIFI_NG
#endif /* IEEE80211_IOCTL_SETWMMPARAMS */
struct wpa_driver_madwifi_data {
void *wext; /* private data for driver_wext */
void *ctx;
char ifname[IFNAMSIZ + 1];
int sock;
};
static int
set80211priv(struct wpa_driver_madwifi_data *drv, int op, void *data, int len,
int show_err)
{
struct iwreq iwr;
os_memset(&iwr, 0, sizeof(iwr));
os_strncpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
if (len < IFNAMSIZ) {
/*
* Argument data fits inline; put it there.
*/
os_memcpy(iwr.u.name, data, len);
} else {
/*
* Argument data too big for inline transfer; setup a
* parameter block instead; the kernel will transfer
* the data for the driver.
*/
iwr.u.data.pointer = data;
iwr.u.data.length = len;
}
if (ioctl(drv->sock, op, &iwr) < 0) {
if (show_err) {
#ifdef MADWIFI_NG
int first = IEEE80211_IOCTL_SETPARAM;
int last = IEEE80211_IOCTL_KICKMAC;
static const char *opnames[] = {
"ioctl[IEEE80211_IOCTL_SETPARAM]",
"ioctl[IEEE80211_IOCTL_GETPARAM]",
"ioctl[IEEE80211_IOCTL_SETMODE]",
"ioctl[IEEE80211_IOCTL_GETMODE]",
"ioctl[IEEE80211_IOCTL_SETWMMPARAMS]",
"ioctl[IEEE80211_IOCTL_GETWMMPARAMS]",
"ioctl[IEEE80211_IOCTL_SETCHANLIST]",
"ioctl[IEEE80211_IOCTL_GETCHANLIST]",
"ioctl[IEEE80211_IOCTL_CHANSWITCH]",
NULL,
NULL,
"ioctl[IEEE80211_IOCTL_GETSCANRESULTS]",
NULL,
"ioctl[IEEE80211_IOCTL_GETCHANINFO]",
"ioctl[IEEE80211_IOCTL_SETOPTIE]",
"ioctl[IEEE80211_IOCTL_GETOPTIE]",
"ioctl[IEEE80211_IOCTL_SETMLME]",
NULL,
"ioctl[IEEE80211_IOCTL_SETKEY]",
NULL,
"ioctl[IEEE80211_IOCTL_DELKEY]",
NULL,
"ioctl[IEEE80211_IOCTL_ADDMAC]",
NULL,
"ioctl[IEEE80211_IOCTL_DELMAC]",
NULL,
"ioctl[IEEE80211_IOCTL_WDSMAC]",
NULL,
"ioctl[IEEE80211_IOCTL_WDSDELMAC]",
NULL,
"ioctl[IEEE80211_IOCTL_KICKMAC]",
};
#else /* MADWIFI_NG */
int first = IEEE80211_IOCTL_SETPARAM;
int last = IEEE80211_IOCTL_CHANLIST;
static const char *opnames[] = {
"ioctl[IEEE80211_IOCTL_SETPARAM]",
"ioctl[IEEE80211_IOCTL_GETPARAM]",
"ioctl[IEEE80211_IOCTL_SETKEY]",
"ioctl[IEEE80211_IOCTL_GETKEY]",
"ioctl[IEEE80211_IOCTL_DELKEY]",
NULL,
"ioctl[IEEE80211_IOCTL_SETMLME]",
NULL,
"ioctl[IEEE80211_IOCTL_SETOPTIE]",
"ioctl[IEEE80211_IOCTL_GETOPTIE]",
"ioctl[IEEE80211_IOCTL_ADDMAC]",
NULL,
"ioctl[IEEE80211_IOCTL_DELMAC]",
NULL,
"ioctl[IEEE80211_IOCTL_CHANLIST]",
};
#endif /* MADWIFI_NG */
int idx = op - first;
if (first <= op && op <= last &&
idx < (int) (sizeof(opnames) / sizeof(opnames[0]))
&& opnames[idx])
perror(opnames[idx]);
else
perror("ioctl[unknown???]");
}
return -1;
}
return 0;
}
static int
set80211param(struct wpa_driver_madwifi_data *drv, int op, int arg,
int show_err)
{
struct iwreq iwr;
os_memset(&iwr, 0, sizeof(iwr));
os_strncpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
iwr.u.mode = op;
os_memcpy(iwr.u.name+sizeof(__u32), &arg, sizeof(arg));
if (ioctl(drv->sock, IEEE80211_IOCTL_SETPARAM, &iwr) < 0) {
if (show_err)
perror("ioctl[IEEE80211_IOCTL_SETPARAM]");
return -1;
}
return 0;
}
static int
wpa_driver_madwifi_set_wpa_ie(struct wpa_driver_madwifi_data *drv,
const u8 *wpa_ie, size_t wpa_ie_len)
{
struct iwreq iwr;
os_memset(&iwr, 0, sizeof(iwr));
os_strncpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
/* NB: SETOPTIE is not fixed-size so must not be inlined */
iwr.u.data.pointer = (void *) wpa_ie;
iwr.u.data.length = wpa_ie_len;
if (ioctl(drv->sock, IEEE80211_IOCTL_SETOPTIE, &iwr) < 0) {
perror("ioctl[IEEE80211_IOCTL_SETOPTIE]");
return -1;
}
return 0;
}
static int
wpa_driver_madwifi_del_key(struct wpa_driver_madwifi_data *drv, int key_idx,
const u8 *addr)
{
struct ieee80211req_del_key wk;
wpa_printf(MSG_DEBUG, "%s: keyidx=%d", __FUNCTION__, key_idx);
os_memset(&wk, 0, sizeof(wk));
wk.idk_keyix = key_idx;
if (addr != NULL)
os_memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
return set80211priv(drv, IEEE80211_IOCTL_DELKEY, &wk, sizeof(wk), 1);
}
static int
wpa_driver_madwifi_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)
{
struct wpa_driver_madwifi_data *drv = priv;
struct ieee80211req_key wk;
char *alg_name;
u_int8_t cipher;
if (alg == WPA_ALG_NONE)
return wpa_driver_madwifi_del_key(drv, key_idx, addr);
switch (alg) {
case WPA_ALG_WEP:
if (addr == NULL || os_memcmp(addr, "\xff\xff\xff\xff\xff\xff",
ETH_ALEN) == 0) {
/*
* madwifi did not seem to like static WEP key
* configuration with IEEE80211_IOCTL_SETKEY, so use
* Linux wireless extensions ioctl for this.
*/
return wpa_driver_wext_set_key(drv->wext, alg, addr,
key_idx, set_tx,
seq, seq_len,
key, key_len);
}
alg_name = "WEP";
cipher = IEEE80211_CIPHER_WEP;
break;
case WPA_ALG_TKIP:
alg_name = "TKIP";
cipher = IEEE80211_CIPHER_TKIP;
break;
case WPA_ALG_CCMP:
alg_name = "CCMP";
cipher = IEEE80211_CIPHER_AES_CCM;
break;
default:
wpa_printf(MSG_DEBUG, "%s: unknown/unsupported algorithm %d",
__FUNCTION__, alg);
return -1;
}
wpa_printf(MSG_DEBUG, "%s: alg=%s key_idx=%d set_tx=%d seq_len=%lu "
"key_len=%lu", __FUNCTION__, alg_name, key_idx, set_tx,
(unsigned long) seq_len, (unsigned long) key_len);
if (seq_len > sizeof(u_int64_t)) {
wpa_printf(MSG_DEBUG, "%s: seq_len %lu too big",
__FUNCTION__, (unsigned long) seq_len);
return -2;
}
if (key_len > sizeof(wk.ik_keydata)) {
wpa_printf(MSG_DEBUG, "%s: key length %lu too big",
__FUNCTION__, (unsigned long) key_len);
return -3;
}
os_memset(&wk, 0, sizeof(wk));
wk.ik_type = cipher;
wk.ik_flags = IEEE80211_KEY_RECV;
if (addr == NULL ||
os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0)
wk.ik_flags |= IEEE80211_KEY_GROUP;
if (set_tx) {
wk.ik_flags |= IEEE80211_KEY_XMIT | IEEE80211_KEY_DEFAULT;
os_memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN);
} else
os_memset(wk.ik_macaddr, 0, IEEE80211_ADDR_LEN);
wk.ik_keyix = key_idx;
wk.ik_keylen = key_len;
#ifdef WORDS_BIGENDIAN
#define WPA_KEY_RSC_LEN 8
{
size_t i;
u8 tmp[WPA_KEY_RSC_LEN];
os_memset(tmp, 0, sizeof(tmp));
for (i = 0; i < seq_len; i++)
tmp[WPA_KEY_RSC_LEN - i - 1] = seq[i];
os_memcpy(&wk.ik_keyrsc, tmp, WPA_KEY_RSC_LEN);
}
#else /* WORDS_BIGENDIAN */
os_memcpy(&wk.ik_keyrsc, seq, seq_len);
#endif /* WORDS_BIGENDIAN */
os_memcpy(wk.ik_keydata, key, key_len);
return set80211priv(drv, IEEE80211_IOCTL_SETKEY, &wk, sizeof(wk), 1);
}
static int
wpa_driver_madwifi_set_countermeasures(void *priv, int enabled)
{
struct wpa_driver_madwifi_data *drv = priv;
wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
return set80211param(drv, IEEE80211_PARAM_COUNTERMEASURES, enabled, 1);
}
static int
wpa_driver_madwifi_set_drop_unencrypted(void *priv, int enabled)
{
struct wpa_driver_madwifi_data *drv = priv;
wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled);
return set80211param(drv, IEEE80211_PARAM_DROPUNENCRYPTED, enabled, 1);
}
static int
wpa_driver_madwifi_deauthenticate(void *priv, const u8 *addr, int reason_code)
{
struct wpa_driver_madwifi_data *drv = priv;
struct ieee80211req_mlme mlme;
wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
mlme.im_op = IEEE80211_MLME_DEAUTH;
mlme.im_reason = reason_code;
os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
return set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme), 1);
}
static int
wpa_driver_madwifi_disassociate(void *priv, const u8 *addr, int reason_code)
{
struct wpa_driver_madwifi_data *drv = priv;
struct ieee80211req_mlme mlme;
wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
mlme.im_op = IEEE80211_MLME_DISASSOC;
mlme.im_reason = reason_code;
os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN);
return set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme, sizeof(mlme), 1);
}
static int
wpa_driver_madwifi_associate(void *priv,
struct wpa_driver_associate_params *params)
{
struct wpa_driver_madwifi_data *drv = priv;
struct ieee80211req_mlme mlme;
int ret = 0, privacy = 1;
wpa_printf(MSG_DEBUG, "%s", __FUNCTION__);
/*
* NB: Don't need to set the freq or cipher-related state as
* this is implied by the bssid which is used to locate
* the scanned node state which holds it. The ssid is
* needed to disambiguate an AP that broadcasts multiple
* ssid's but uses the same bssid.
*/
/* XXX error handling is wrong but unclear what to do... */
if (wpa_driver_madwifi_set_wpa_ie(drv, params->wpa_ie,
params->wpa_ie_len) < 0)
ret = -1;
if (params->pairwise_suite == CIPHER_NONE &&
params->group_suite == CIPHER_NONE &&
params->key_mgmt_suite == KEY_MGMT_NONE &&
params->wpa_ie_len == 0)
privacy = 0;
if (set80211param(drv, IEEE80211_PARAM_PRIVACY, privacy, 1) < 0)
ret = -1;
if (params->wpa_ie_len &&
set80211param(drv, IEEE80211_PARAM_WPA,
params->wpa_ie[0] == RSN_INFO_ELEM ? 2 : 1, 1) < 0)
ret = -1;
if (params->bssid == NULL) {
/* ap_scan=2 mode - driver takes care of AP selection and
* roaming */
/* FIX: this does not seem to work; would probably need to
* change something in the driver */
if (set80211param(drv, IEEE80211_PARAM_ROAMING, 0, 1) < 0)
ret = -1;
if (wpa_driver_wext_set_ssid(drv->wext, params->ssid,
params->ssid_len) < 0)
ret = -1;
} else {
if (set80211param(drv, IEEE80211_PARAM_ROAMING, 2, 1) < 0)
ret = -1;
if (wpa_driver_wext_set_ssid(drv->wext, params->ssid,
params->ssid_len) < 0)
ret = -1;
os_memset(&mlme, 0, sizeof(mlme));
mlme.im_op = IEEE80211_MLME_ASSOC;
os_memcpy(mlme.im_macaddr, params->bssid, IEEE80211_ADDR_LEN);
if (set80211priv(drv, IEEE80211_IOCTL_SETMLME, &mlme,
sizeof(mlme), 1) < 0) {
wpa_printf(MSG_DEBUG, "%s: SETMLME[ASSOC] failed",
__func__);
ret = -1;
}
}
return ret;
}
static int
wpa_driver_madwifi_set_auth_alg(void *priv, int auth_alg)
{
struct wpa_driver_madwifi_data *drv = priv;
int authmode;
if ((auth_alg & AUTH_ALG_OPEN_SYSTEM) &&
(auth_alg & AUTH_ALG_SHARED_KEY))
authmode = IEEE80211_AUTH_AUTO;
else if (auth_alg & AUTH_ALG_SHARED_KEY)
authmode = IEEE80211_AUTH_SHARED;
else
authmode = IEEE80211_AUTH_OPEN;
return set80211param(drv, IEEE80211_PARAM_AUTHMODE, authmode, 1);
}
static int
wpa_driver_madwifi_scan(void *priv, const u8 *ssid, size_t ssid_len)
{
struct wpa_driver_madwifi_data *drv = priv;
struct iwreq iwr;
int ret = 0;
os_memset(&iwr, 0, sizeof(iwr));
os_strncpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
/* set desired ssid before scan */
/* FIX: scan should not break the current association, so using
* set_ssid may not be the best way of doing this.. */
if (wpa_driver_wext_set_ssid(drv->wext, ssid, ssid_len) < 0)
ret = -1;
if (ioctl(drv->sock, SIOCSIWSCAN, &iwr) < 0) {
perror("ioctl[SIOCSIWSCAN]");
ret = -1;
}
/*
* madwifi delivers a scan complete event so no need to poll, but
* register a backup timeout anyway to make sure that we recover even
* if the driver does not send this event for any reason. This timeout
* will only be used if the event is not delivered (event handler will
* cancel the timeout).
*/
eloop_cancel_timeout(wpa_driver_wext_scan_timeout, drv->wext,
drv->ctx);
eloop_register_timeout(30, 0, wpa_driver_wext_scan_timeout, drv->wext,
drv->ctx);
return ret;
}
static int wpa_driver_madwifi_get_bssid(void *priv, u8 *bssid)
{
struct wpa_driver_madwifi_data *drv = priv;
return wpa_driver_wext_get_bssid(drv->wext, bssid);
}
static int wpa_driver_madwifi_get_ssid(void *priv, u8 *ssid)
{
struct wpa_driver_madwifi_data *drv = priv;
return wpa_driver_wext_get_ssid(drv->wext, ssid);
}
static int wpa_driver_madwifi_get_scan_results(void *priv,
struct wpa_scan_result *results,
size_t max_size)
{
struct wpa_driver_madwifi_data *drv = priv;
return wpa_driver_wext_get_scan_results(drv->wext, results, max_size);
}
static int wpa_driver_madwifi_set_operstate(void *priv, int state)
{
struct wpa_driver_madwifi_data *drv = priv;
return wpa_driver_wext_set_operstate(drv->wext, state);
}
static void * wpa_driver_madwifi_init(void *ctx, const char *ifname)
{
struct wpa_driver_madwifi_data *drv;
drv = os_zalloc(sizeof(*drv));
if (drv == NULL)
return NULL;
drv->wext = wpa_driver_wext_init(ctx, ifname);
if (drv->wext == NULL)
goto fail;
drv->ctx = ctx;
os_strncpy(drv->ifname, ifname, sizeof(drv->ifname));
drv->sock = socket(PF_INET, SOCK_DGRAM, 0);
if (drv->sock < 0)
goto fail2;
if (set80211param(drv, IEEE80211_PARAM_ROAMING, 2, 1) < 0) {
wpa_printf(MSG_DEBUG, "%s: failed to set wpa_supplicant-based "
"roaming", __FUNCTION__);
goto fail3;
}
if (set80211param(drv, IEEE80211_PARAM_WPA, 3, 1) < 0) {
wpa_printf(MSG_DEBUG, "%s: failed to enable WPA support",
__FUNCTION__);
goto fail3;
}
return drv;
fail3:
close(drv->sock);
fail2:
wpa_driver_wext_deinit(drv->wext);
fail:
os_free(drv);
return NULL;
}
static void wpa_driver_madwifi_deinit(void *priv)
{
struct wpa_driver_madwifi_data *drv = priv;
if (wpa_driver_madwifi_set_wpa_ie(drv, NULL, 0) < 0) {
wpa_printf(MSG_DEBUG, "%s: failed to clear WPA IE",
__FUNCTION__);
}
if (set80211param(drv, IEEE80211_PARAM_ROAMING, 0, 1) < 0) {
wpa_printf(MSG_DEBUG, "%s: failed to enable driver-based "
"roaming", __FUNCTION__);
}
if (set80211param(drv, IEEE80211_PARAM_PRIVACY, 0, 1) < 0) {
wpa_printf(MSG_DEBUG, "%s: failed to disable forced Privacy "
"flag", __FUNCTION__);
}
if (set80211param(drv, IEEE80211_PARAM_WPA, 0, 1) < 0) {
wpa_printf(MSG_DEBUG, "%s: failed to disable WPA",
__FUNCTION__);
}
wpa_driver_wext_deinit(drv->wext);
close(drv->sock);
os_free(drv);
}
const struct wpa_driver_ops wpa_driver_madwifi_ops = {
.name = "madwifi",
.desc = "MADWIFI 802.11 support (Atheros, etc.)",
.get_bssid = wpa_driver_madwifi_get_bssid,
.get_ssid = wpa_driver_madwifi_get_ssid,
.set_key = wpa_driver_madwifi_set_key,
.init = wpa_driver_madwifi_init,
.deinit = wpa_driver_madwifi_deinit,
.set_countermeasures = wpa_driver_madwifi_set_countermeasures,
.set_drop_unencrypted = wpa_driver_madwifi_set_drop_unencrypted,
.scan = wpa_driver_madwifi_scan,
.get_scan_results = wpa_driver_madwifi_get_scan_results,
.deauthenticate = wpa_driver_madwifi_deauthenticate,
.disassociate = wpa_driver_madwifi_disassociate,
.associate = wpa_driver_madwifi_associate,
.set_auth_alg = wpa_driver_madwifi_set_auth_alg,
.set_operstate = wpa_driver_madwifi_set_operstate,
};