iwlwifi: move agn module parameter structure to common place
[deliverable/linux.git] / drivers / net / wireless / libertas / assoc.c
CommitLineData
876c9d3a
MT
1/* Copyright (C) 2006, Red Hat, Inc. */
2
7e272fcf 3#include <linux/types.h>
3cf20931 4#include <linux/etherdevice.h>
2c706002
JB
5#include <linux/ieee80211.h>
6#include <linux/if_arp.h>
7e272fcf 7#include <net/lib80211.h>
876c9d3a
MT
8
9#include "assoc.h"
876c9d3a 10#include "decl.h"
876c9d3a 11#include "host.h"
245bf20f 12#include "scan.h"
2dd4b262 13#include "cmd.h"
876c9d3a 14
5a6e0434
IH
15static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
16 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
17static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
18 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
876c9d3a 19
be0d76e4
DW
20/* The firmware needs the following bits masked out of the beacon-derived
21 * capability field when associating/joining to a BSS:
22 * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
697900ac
HS
23 */
24#define CAPINFO_MASK (~(0xda00))
25
2d46502d
HS
26/**
27 * 802.11b/g supported bitrates (in 500Kb/s units)
28 */
29u8 lbs_bg_rates[MAX_RATES] =
30 { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
310x00, 0x00 };
32
697900ac 33
921ca03c
AK
34static int assoc_helper_wep_keys(struct lbs_private *priv,
35 struct assoc_request *assoc_req);
36
f5fe1fda
DW
37/**
38 * @brief This function finds common rates between rates and card rates.
39 *
40 * It will fill common rates in rates as output if found.
41 *
42 * NOTE: Setting the MSB of the basic rates need to be taken
43 * care, either before or after calling this function
44 *
45 * @param priv A pointer to struct lbs_private structure
46 * @param rates the buffer which keeps input and output
ca4fe300
DW
47 * @param rates_size the size of rates buffer; new size of buffer on return,
48 * which will be less than or equal to original rates_size
f5fe1fda
DW
49 *
50 * @return 0 on success, or -1 on error
51 */
52static int get_common_rates(struct lbs_private *priv,
53 u8 *rates,
54 u16 *rates_size)
55{
1e3d31c5 56 int i, j;
ca4fe300
DW
57 u8 intersection[MAX_RATES];
58 u16 intersection_size;
59 u16 num_rates = 0;
f5fe1fda 60
ca4fe300 61 intersection_size = min_t(u16, *rates_size, ARRAY_SIZE(intersection));
6f632d57 62
ca4fe300
DW
63 /* Allow each rate from 'rates' that is supported by the hardware */
64 for (i = 0; i < ARRAY_SIZE(lbs_bg_rates) && lbs_bg_rates[i]; i++) {
65 for (j = 0; j < intersection_size && rates[j]; j++) {
66 if (rates[j] == lbs_bg_rates[i])
67 intersection[num_rates++] = rates[j];
f5fe1fda
DW
68 }
69 }
70
71 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
ca4fe300
DW
72 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", lbs_bg_rates,
73 ARRAY_SIZE(lbs_bg_rates));
74 lbs_deb_hex(LBS_DEB_JOIN, "common rates", intersection, num_rates);
f5fe1fda
DW
75 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
76
77 if (!priv->enablehwauto) {
ca4fe300
DW
78 for (i = 0; i < num_rates; i++) {
79 if (intersection[i] == priv->cur_rate)
80 goto done;
f5fe1fda 81 }
ca4fe300
DW
82 lbs_pr_alert("Previously set fixed data rate %#x isn't "
83 "compatible with the network.\n", priv->cur_rate);
84 return -1;
f5fe1fda 85 }
ca4fe300
DW
86
87done:
88 memset(rates, 0, *rates_size);
89 *rates_size = num_rates;
90 memcpy(rates, intersection, num_rates);
1e3d31c5 91 return 0;
f5fe1fda
DW
92}
93
94
95/**
96 * @brief Sets the MSB on basic rates as the firmware requires
97 *
98 * Scan through an array and set the MSB for basic data rates.
99 *
100 * @param rates buffer of data rates
101 * @param len size of buffer
102 */
103static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
104{
105 int i;
106
107 for (i = 0; i < len; i++) {
108 if (rates[i] == 0x02 || rates[i] == 0x04 ||
109 rates[i] == 0x0b || rates[i] == 0x16)
110 rates[i] |= 0x80;
111 }
112}
113
697900ac 114
be0d76e4
DW
115static u8 iw_auth_to_ieee_auth(u8 auth)
116{
117 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
118 return 0x00;
119 else if (auth == IW_AUTH_ALG_SHARED_KEY)
120 return 0x01;
121 else if (auth == IW_AUTH_ALG_LEAP)
122 return 0x80;
123
124 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
125 return 0;
126}
127
128/**
129 * @brief This function prepares the authenticate command. AUTHENTICATE only
130 * sets the authentication suite for future associations, as the firmware
131 * handles authentication internally during the ASSOCIATE command.
132 *
133 * @param priv A pointer to struct lbs_private structure
134 * @param bssid The peer BSSID with which to authenticate
135 * @param auth The authentication mode to use (from wireless.h)
136 *
137 * @return 0 or -1
138 */
139static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
140{
141 struct cmd_ds_802_11_authenticate cmd;
142 int ret = -1;
be0d76e4
DW
143
144 lbs_deb_enter(LBS_DEB_JOIN);
145
146 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
147 memcpy(cmd.bssid, bssid, ETH_ALEN);
148
149 cmd.authtype = iw_auth_to_ieee_auth(auth);
150
e91d8334 151 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
be0d76e4
DW
152
153 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
154
155 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
156 return ret;
157}
158
822ac03a 159
d0de3741
HS
160int lbs_cmd_802_11_set_wep(struct lbs_private *priv, uint16_t cmd_action,
161 struct assoc_request *assoc)
162{
163 struct cmd_ds_802_11_set_wep cmd;
164 int ret = 0;
165
166 lbs_deb_enter(LBS_DEB_CMD);
167
168 memset(&cmd, 0, sizeof(cmd));
169 cmd.hdr.command = cpu_to_le16(CMD_802_11_SET_WEP);
170 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
171
172 cmd.action = cpu_to_le16(cmd_action);
173
174 if (cmd_action == CMD_ACT_ADD) {
175 int i;
176
177 /* default tx key index */
178 cmd.keyindex = cpu_to_le16(assoc->wep_tx_keyidx &
179 CMD_WEP_KEY_INDEX_MASK);
180
181 /* Copy key types and material to host command structure */
182 for (i = 0; i < 4; i++) {
183 struct enc_key *pkey = &assoc->wep_keys[i];
184
185 switch (pkey->len) {
186 case KEY_LEN_WEP_40:
187 cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
188 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
189 lbs_deb_cmd("SET_WEP: add key %d (40 bit)\n", i);
190 break;
191 case KEY_LEN_WEP_104:
192 cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
193 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
194 lbs_deb_cmd("SET_WEP: add key %d (104 bit)\n", i);
195 break;
196 case 0:
197 break;
198 default:
199 lbs_deb_cmd("SET_WEP: invalid key %d, length %d\n",
200 i, pkey->len);
201 ret = -1;
202 goto done;
203 break;
204 }
205 }
206 } else if (cmd_action == CMD_ACT_REMOVE) {
207 /* ACT_REMOVE clears _all_ WEP keys */
208
209 /* default tx key index */
210 cmd.keyindex = cpu_to_le16(priv->wep_tx_keyidx &
211 CMD_WEP_KEY_INDEX_MASK);
212 lbs_deb_cmd("SET_WEP: remove key %d\n", priv->wep_tx_keyidx);
213 }
214
215 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
216done:
217 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
218 return ret;
219}
220
221int lbs_cmd_802_11_enable_rsn(struct lbs_private *priv, uint16_t cmd_action,
222 uint16_t *enable)
223{
224 struct cmd_ds_802_11_enable_rsn cmd;
225 int ret;
226
227 lbs_deb_enter(LBS_DEB_CMD);
228
229 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
230 cmd.action = cpu_to_le16(cmd_action);
231
232 if (cmd_action == CMD_ACT_GET)
233 cmd.enable = 0;
234 else {
235 if (*enable)
236 cmd.enable = cpu_to_le16(CMD_ENABLE_RSN);
237 else
238 cmd.enable = cpu_to_le16(CMD_DISABLE_RSN);
239 lbs_deb_cmd("ENABLE_RSN: %d\n", *enable);
240 }
241
242 ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
243 if (!ret && cmd_action == CMD_ACT_GET)
244 *enable = le16_to_cpu(cmd.enable);
245
246 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
247 return ret;
248}
249
250static void set_one_wpa_key(struct MrvlIEtype_keyParamSet *keyparam,
251 struct enc_key *key)
252{
253 lbs_deb_enter(LBS_DEB_CMD);
254
255 if (key->flags & KEY_INFO_WPA_ENABLED)
256 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_ENABLED);
257 if (key->flags & KEY_INFO_WPA_UNICAST)
258 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
259 if (key->flags & KEY_INFO_WPA_MCAST)
260 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
261
262 keyparam->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
263 keyparam->keytypeid = cpu_to_le16(key->type);
264 keyparam->keylen = cpu_to_le16(key->len);
265 memcpy(keyparam->key, key->key, key->len);
266
267 /* Length field doesn't include the {type,length} header */
268 keyparam->length = cpu_to_le16(sizeof(*keyparam) - 4);
269 lbs_deb_leave(LBS_DEB_CMD);
270}
271
272int lbs_cmd_802_11_key_material(struct lbs_private *priv, uint16_t cmd_action,
273 struct assoc_request *assoc)
274{
275 struct cmd_ds_802_11_key_material cmd;
276 int ret = 0;
277 int index = 0;
278
279 lbs_deb_enter(LBS_DEB_CMD);
280
281 cmd.action = cpu_to_le16(cmd_action);
282 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
283
284 if (cmd_action == CMD_ACT_GET) {
8ec97cc8 285 cmd.hdr.size = cpu_to_le16(sizeof(struct cmd_header) + 2);
d0de3741
HS
286 } else {
287 memset(cmd.keyParamSet, 0, sizeof(cmd.keyParamSet));
288
289 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc->flags)) {
290 set_one_wpa_key(&cmd.keyParamSet[index],
291 &assoc->wpa_unicast_key);
292 index++;
293 }
294
295 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc->flags)) {
296 set_one_wpa_key(&cmd.keyParamSet[index],
297 &assoc->wpa_mcast_key);
298 index++;
299 }
300
301 /* The common header and as many keys as we included */
302 cmd.hdr.size = cpu_to_le16(offsetof(typeof(cmd),
303 keyParamSet[index]));
304 }
305 ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
306 /* Copy the returned key to driver private data */
307 if (!ret && cmd_action == CMD_ACT_GET) {
308 void *buf_ptr = cmd.keyParamSet;
309 void *resp_end = &(&cmd)[1];
310
311 while (buf_ptr < resp_end) {
312 struct MrvlIEtype_keyParamSet *keyparam = buf_ptr;
313 struct enc_key *key;
314 uint16_t param_set_len = le16_to_cpu(keyparam->length);
315 uint16_t key_len = le16_to_cpu(keyparam->keylen);
316 uint16_t key_flags = le16_to_cpu(keyparam->keyinfo);
317 uint16_t key_type = le16_to_cpu(keyparam->keytypeid);
318 void *end;
319
320 end = (void *)keyparam + sizeof(keyparam->type)
321 + sizeof(keyparam->length) + param_set_len;
322
323 /* Make sure we don't access past the end of the IEs */
324 if (end > resp_end)
325 break;
326
327 if (key_flags & KEY_INFO_WPA_UNICAST)
328 key = &priv->wpa_unicast_key;
329 else if (key_flags & KEY_INFO_WPA_MCAST)
330 key = &priv->wpa_mcast_key;
331 else
332 break;
333
334 /* Copy returned key into driver */
335 memset(key, 0, sizeof(struct enc_key));
336 if (key_len > sizeof(key->key))
337 break;
338 key->type = key_type;
339 key->flags = key_flags;
340 key->len = key_len;
341 memcpy(key->key, keyparam->key, key->len);
342
343 buf_ptr = end + 1;
344 }
345 }
346
347 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
348 return ret;
349}
350
351static __le16 lbs_rate_to_fw_bitmap(int rate, int lower_rates_ok)
352{
353/* Bit Rate
354* 15:13 Reserved
355* 12 54 Mbps
356* 11 48 Mbps
357* 10 36 Mbps
358* 9 24 Mbps
359* 8 18 Mbps
360* 7 12 Mbps
361* 6 9 Mbps
362* 5 6 Mbps
363* 4 Reserved
364* 3 11 Mbps
365* 2 5.5 Mbps
366* 1 2 Mbps
367* 0 1 Mbps
368**/
369
370 uint16_t ratemask;
371 int i = lbs_data_rate_to_fw_index(rate);
372 if (lower_rates_ok)
373 ratemask = (0x1fef >> (12 - i));
374 else
375 ratemask = (1 << i);
376 return cpu_to_le16(ratemask);
377}
378
379int lbs_cmd_802_11_rate_adapt_rateset(struct lbs_private *priv,
380 uint16_t cmd_action)
381{
382 struct cmd_ds_802_11_rate_adapt_rateset cmd;
383 int ret;
384
385 lbs_deb_enter(LBS_DEB_CMD);
386
387 if (!priv->cur_rate && !priv->enablehwauto)
388 return -EINVAL;
389
390 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
391
392 cmd.action = cpu_to_le16(cmd_action);
393 cmd.enablehwauto = cpu_to_le16(priv->enablehwauto);
394 cmd.bitmap = lbs_rate_to_fw_bitmap(priv->cur_rate, priv->enablehwauto);
395 ret = lbs_cmd_with_response(priv, CMD_802_11_RATE_ADAPT_RATESET, &cmd);
48631de9 396 if (!ret && cmd_action == CMD_ACT_GET)
d0de3741 397 priv->enablehwauto = le16_to_cpu(cmd.enablehwauto);
d0de3741
HS
398
399 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
400 return ret;
401}
402
403/**
404 * @brief Set the data rate
405 *
406 * @param priv A pointer to struct lbs_private structure
407 * @param rate The desired data rate, or 0 to clear a locked rate
408 *
409 * @return 0 on success, error on failure
410 */
411int lbs_set_data_rate(struct lbs_private *priv, u8 rate)
412{
413 struct cmd_ds_802_11_data_rate cmd;
414 int ret = 0;
415
416 lbs_deb_enter(LBS_DEB_CMD);
417
418 memset(&cmd, 0, sizeof(cmd));
419 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
420
421 if (rate > 0) {
422 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_FIX_RATE);
423 cmd.rates[0] = lbs_data_rate_to_fw_index(rate);
424 if (cmd.rates[0] == 0) {
425 lbs_deb_cmd("DATA_RATE: invalid requested rate of"
426 " 0x%02X\n", rate);
427 ret = 0;
428 goto out;
429 }
430 lbs_deb_cmd("DATA_RATE: set fixed 0x%02X\n", cmd.rates[0]);
431 } else {
432 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_AUTO);
433 lbs_deb_cmd("DATA_RATE: setting auto\n");
434 }
435
436 ret = lbs_cmd_with_response(priv, CMD_802_11_DATA_RATE, &cmd);
437 if (ret)
438 goto out;
439
440 lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) &cmd, sizeof(cmd));
441
442 /* FIXME: get actual rates FW can do if this command actually returns
443 * all data rates supported.
444 */
445 priv->cur_rate = lbs_fw_index_to_data_rate(cmd.rates[0]);
446 lbs_deb_cmd("DATA_RATE: current rate is 0x%02x\n", priv->cur_rate);
447
448out:
449 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
450 return ret;
451}
452
453
454int lbs_cmd_802_11_rssi(struct lbs_private *priv,
455 struct cmd_ds_command *cmd)
456{
457
458 lbs_deb_enter(LBS_DEB_CMD);
459 cmd->command = cpu_to_le16(CMD_802_11_RSSI);
8ec97cc8
HS
460 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) +
461 sizeof(struct cmd_header));
d0de3741
HS
462 cmd->params.rssi.N = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
463
464 /* reset Beacon SNR/NF/RSSI values */
465 priv->SNR[TYPE_BEACON][TYPE_NOAVG] = 0;
466 priv->SNR[TYPE_BEACON][TYPE_AVG] = 0;
467 priv->NF[TYPE_BEACON][TYPE_NOAVG] = 0;
468 priv->NF[TYPE_BEACON][TYPE_AVG] = 0;
469 priv->RSSI[TYPE_BEACON][TYPE_NOAVG] = 0;
470 priv->RSSI[TYPE_BEACON][TYPE_AVG] = 0;
471
472 lbs_deb_leave(LBS_DEB_CMD);
473 return 0;
474}
475
476int lbs_ret_802_11_rssi(struct lbs_private *priv,
477 struct cmd_ds_command *resp)
478{
479 struct cmd_ds_802_11_rssi_rsp *rssirsp = &resp->params.rssirsp;
480
481 lbs_deb_enter(LBS_DEB_CMD);
482
483 /* store the non average value */
484 priv->SNR[TYPE_BEACON][TYPE_NOAVG] = get_unaligned_le16(&rssirsp->SNR);
485 priv->NF[TYPE_BEACON][TYPE_NOAVG] =
486 get_unaligned_le16(&rssirsp->noisefloor);
487
488 priv->SNR[TYPE_BEACON][TYPE_AVG] = get_unaligned_le16(&rssirsp->avgSNR);
489 priv->NF[TYPE_BEACON][TYPE_AVG] =
490 get_unaligned_le16(&rssirsp->avgnoisefloor);
491
492 priv->RSSI[TYPE_BEACON][TYPE_NOAVG] =
493 CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_NOAVG],
494 priv->NF[TYPE_BEACON][TYPE_NOAVG]);
495
496 priv->RSSI[TYPE_BEACON][TYPE_AVG] =
497 CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_AVG] / AVG_SCALE,
498 priv->NF[TYPE_BEACON][TYPE_AVG] / AVG_SCALE);
499
500 lbs_deb_cmd("RSSI: beacon %d, avg %d\n",
501 priv->RSSI[TYPE_BEACON][TYPE_NOAVG],
502 priv->RSSI[TYPE_BEACON][TYPE_AVG]);
503
504 lbs_deb_leave(LBS_DEB_CMD);
505 return 0;
506}
507
508
509int lbs_cmd_bcn_ctrl(struct lbs_private *priv,
510 struct cmd_ds_command *cmd,
511 u16 cmd_action)
512{
513 struct cmd_ds_802_11_beacon_control
514 *bcn_ctrl = &cmd->params.bcn_ctrl;
515
516 lbs_deb_enter(LBS_DEB_CMD);
517 cmd->size =
518 cpu_to_le16(sizeof(struct cmd_ds_802_11_beacon_control)
8ec97cc8 519 + sizeof(struct cmd_header));
d0de3741
HS
520 cmd->command = cpu_to_le16(CMD_802_11_BEACON_CTRL);
521
522 bcn_ctrl->action = cpu_to_le16(cmd_action);
523 bcn_ctrl->beacon_enable = cpu_to_le16(priv->beacon_enable);
524 bcn_ctrl->beacon_period = cpu_to_le16(priv->beacon_period);
525
526 lbs_deb_leave(LBS_DEB_CMD);
527 return 0;
528}
529
530int lbs_ret_802_11_bcn_ctrl(struct lbs_private *priv,
531 struct cmd_ds_command *resp)
532{
533 struct cmd_ds_802_11_beacon_control *bcn_ctrl =
534 &resp->params.bcn_ctrl;
535
536 lbs_deb_enter(LBS_DEB_CMD);
537
538 if (bcn_ctrl->action == CMD_ACT_GET) {
539 priv->beacon_enable = (u8) le16_to_cpu(bcn_ctrl->beacon_enable);
540 priv->beacon_period = le16_to_cpu(bcn_ctrl->beacon_period);
541 }
542
543 lbs_deb_enter(LBS_DEB_CMD);
544 return 0;
545}
546
547
548
822ac03a
DW
549static int lbs_assoc_post(struct lbs_private *priv,
550 struct cmd_ds_802_11_associate_response *resp)
551{
552 int ret = 0;
553 union iwreq_data wrqu;
554 struct bss_descriptor *bss;
555 u16 status_code;
556
557 lbs_deb_enter(LBS_DEB_ASSOC);
558
559 if (!priv->in_progress_assoc_req) {
560 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
561 ret = -1;
562 goto done;
563 }
564 bss = &priv->in_progress_assoc_req->bss;
565
566 /*
567 * Older FW versions map the IEEE 802.11 Status Code in the association
568 * response to the following values returned in resp->statuscode:
569 *
570 * IEEE Status Code Marvell Status Code
571 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
572 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
573 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
574 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
575 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
576 * others -> 0x0003 ASSOC_RESULT_REFUSED
577 *
578 * Other response codes:
579 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
580 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
581 * association response from the AP)
582 */
583
584 status_code = le16_to_cpu(resp->statuscode);
585 if (priv->fwrelease < 0x09000000) {
586 switch (status_code) {
587 case 0x00:
588 break;
589 case 0x01:
590 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
591 break;
592 case 0x02:
593 lbs_deb_assoc("ASSOC_RESP: internal timer "
594 "expired while waiting for the AP\n");
595 break;
596 case 0x03:
597 lbs_deb_assoc("ASSOC_RESP: association "
598 "refused by AP\n");
599 break;
600 case 0x04:
601 lbs_deb_assoc("ASSOC_RESP: authentication "
602 "refused by AP\n");
603 break;
604 default:
605 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
606 " unknown\n", status_code);
607 break;
608 }
609 } else {
610 /* v9+ returns the AP's association response */
611 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
612 }
613
614 if (status_code) {
615 lbs_mac_event_disconnected(priv);
921ca03c 616 ret = status_code;
822ac03a
DW
617 goto done;
618 }
619
620 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
621 (void *) (resp + sizeof (resp->hdr)),
622 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
623
624 /* Send a Media Connected event, according to the Spec */
625 priv->connect_status = LBS_CONNECTED;
626
627 /* Update current SSID and BSSID */
243e84e9 628 memcpy(&priv->curbssparams.ssid, &bss->ssid, IEEE80211_MAX_SSID_LEN);
822ac03a
DW
629 priv->curbssparams.ssid_len = bss->ssid_len;
630 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
631
632 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
633 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
634
635 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
636 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
637 priv->nextSNRNF = 0;
638 priv->numSNRNF = 0;
639
640 netif_carrier_on(priv->dev);
641 if (!priv->tx_pending_len)
642 netif_wake_queue(priv->dev);
643
644 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
645 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
646 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
647
648done:
649 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
650 return ret;
651}
652
653/**
654 * @brief This function prepares an association-class command.
655 *
656 * @param priv A pointer to struct lbs_private structure
657 * @param assoc_req The association request describing the BSS to associate
658 * or reassociate with
659 * @param command The actual command, either CMD_802_11_ASSOCIATE or
660 * CMD_802_11_REASSOCIATE
661 *
662 * @return 0 or -1
663 */
664static int lbs_associate(struct lbs_private *priv,
665 struct assoc_request *assoc_req,
666 u16 command)
667{
668 struct cmd_ds_802_11_associate cmd;
669 int ret = 0;
670 struct bss_descriptor *bss = &assoc_req->bss;
671 u8 *pos = &(cmd.iebuf[0]);
672 u16 tmpcap, tmplen, tmpauth;
673 struct mrvl_ie_ssid_param_set *ssid;
674 struct mrvl_ie_ds_param_set *ds;
675 struct mrvl_ie_cf_param_set *cf;
676 struct mrvl_ie_rates_param_set *rates;
677 struct mrvl_ie_rsn_param_set *rsn;
678 struct mrvl_ie_auth_type *auth;
679
680 lbs_deb_enter(LBS_DEB_ASSOC);
681
682 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
683 (command != CMD_802_11_REASSOCIATE));
684
685 memset(&cmd, 0, sizeof(cmd));
686 cmd.hdr.command = cpu_to_le16(command);
687
688 /* Fill in static fields */
689 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
690 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
691
692 /* Capability info */
693 tmpcap = (bss->capability & CAPINFO_MASK);
694 if (bss->mode == IW_MODE_INFRA)
695 tmpcap |= WLAN_CAPABILITY_ESS;
696 cmd.capability = cpu_to_le16(tmpcap);
697 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
698
699 /* SSID */
700 ssid = (struct mrvl_ie_ssid_param_set *) pos;
701 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
702 tmplen = bss->ssid_len;
703 ssid->header.len = cpu_to_le16(tmplen);
704 memcpy(ssid->ssid, bss->ssid, tmplen);
705 pos += sizeof(ssid->header) + tmplen;
706
707 ds = (struct mrvl_ie_ds_param_set *) pos;
708 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
709 ds->header.len = cpu_to_le16(1);
710 ds->channel = bss->phy.ds.channel;
711 pos += sizeof(ds->header) + 1;
712
713 cf = (struct mrvl_ie_cf_param_set *) pos;
714 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
715 tmplen = sizeof(*cf) - sizeof (cf->header);
716 cf->header.len = cpu_to_le16(tmplen);
717 /* IE payload should be zeroed, firmware fills it in for us */
718 pos += sizeof(*cf);
719
720 rates = (struct mrvl_ie_rates_param_set *) pos;
721 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
ca4fe300 722 tmplen = min_t(u16, ARRAY_SIZE(bss->rates), MAX_RATES);
1e3d31c5 723 memcpy(&rates->rates, &bss->rates, tmplen);
822ac03a
DW
724 if (get_common_rates(priv, rates->rates, &tmplen)) {
725 ret = -1;
726 goto done;
727 }
728 pos += sizeof(rates->header) + tmplen;
729 rates->header.len = cpu_to_le16(tmplen);
730 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
731
732 /* Copy the infra. association rates into Current BSS state structure */
733 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
734 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
735
736 /* Set MSB on basic rates as the firmware requires, but _after_
737 * copying to current bss rates.
738 */
739 lbs_set_basic_rate_flags(rates->rates, tmplen);
740
741 /* Firmware v9+ indicate authentication suites as a TLV */
742 if (priv->fwrelease >= 0x09000000) {
822ac03a
DW
743 auth = (struct mrvl_ie_auth_type *) pos;
744 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
745 auth->header.len = cpu_to_le16(2);
746 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
747 auth->auth = cpu_to_le16(tmpauth);
748 pos += sizeof(auth->header) + 2;
749
e91d8334
JB
750 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
751 bss->bssid, priv->secinfo.auth_mode);
822ac03a
DW
752 }
753
754 /* WPA/WPA2 IEs */
755 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
756 rsn = (struct mrvl_ie_rsn_param_set *) pos;
757 /* WPA_IE or WPA2_IE */
758 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
759 tmplen = (u16) assoc_req->wpa_ie[1];
760 rsn->header.len = cpu_to_le16(tmplen);
761 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
762 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
763 sizeof(rsn->header) + tmplen);
764 pos += sizeof(rsn->header) + tmplen;
765 }
766
767 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
768 (u16)(pos - (u8 *) &cmd.iebuf));
769
770 /* update curbssparams */
c14951fe 771 priv->channel = bss->phy.ds.channel;
822ac03a 772
822ac03a
DW
773 ret = lbs_cmd_with_response(priv, command, &cmd);
774 if (ret == 0) {
775 ret = lbs_assoc_post(priv,
776 (struct cmd_ds_802_11_associate_response *) &cmd);
777 }
778
779done:
780 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
781 return ret;
782}
783
697900ac
HS
784/**
785 * @brief Associate to a specific BSS discovered in a scan
786 *
787 * @param priv A pointer to struct lbs_private structure
d5db2dfa 788 * @param assoc_req The association request describing the BSS to associate with
697900ac
HS
789 *
790 * @return 0-success, otherwise fail
791 */
822ac03a 792static int lbs_try_associate(struct lbs_private *priv,
697900ac
HS
793 struct assoc_request *assoc_req)
794{
795 int ret;
d5db2dfa 796 u8 preamble = RADIO_PREAMBLE_LONG;
697900ac
HS
797
798 lbs_deb_enter(LBS_DEB_ASSOC);
799
be0d76e4
DW
800 /* FW v9 and higher indicate authentication suites as a TLV in the
801 * association command, not as a separate authentication command.
802 */
803 if (priv->fwrelease < 0x09000000) {
804 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
805 priv->secinfo.auth_mode);
806 if (ret)
807 goto out;
808 }
697900ac 809
d5db2dfa 810 /* Use short preamble only when both the BSS and firmware support it */
0e78ff8f 811 if (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
d5db2dfa 812 preamble = RADIO_PREAMBLE_SHORT;
697900ac 813
d5db2dfa
DW
814 ret = lbs_set_radio(priv, preamble, 1);
815 if (ret)
816 goto out;
697900ac 817
822ac03a 818 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
921ca03c
AK
819 /* If the association fails with current auth mode, let's
820 * try by changing the auth mode
821 */
822 if ((priv->authtype_auto) &&
823 (ret == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) &&
824 (assoc_req->secinfo.wep_enabled) &&
825 (priv->connect_status != LBS_CONNECTED)) {
826 if (priv->secinfo.auth_mode == IW_AUTH_ALG_OPEN_SYSTEM)
827 priv->secinfo.auth_mode = IW_AUTH_ALG_SHARED_KEY;
828 else
829 priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
830 if (!assoc_helper_wep_keys(priv, assoc_req))
831 ret = lbs_associate(priv, assoc_req,
832 CMD_802_11_ASSOCIATE);
833 }
697900ac 834
921ca03c
AK
835 if (ret)
836 ret = -1;
d5db2dfa 837out:
697900ac
HS
838 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
839 return ret;
840}
841
822ac03a
DW
842static int lbs_adhoc_post(struct lbs_private *priv,
843 struct cmd_ds_802_11_ad_hoc_result *resp)
844{
845 int ret = 0;
846 u16 command = le16_to_cpu(resp->hdr.command);
847 u16 result = le16_to_cpu(resp->hdr.result);
848 union iwreq_data wrqu;
849 struct bss_descriptor *bss;
850 DECLARE_SSID_BUF(ssid);
851
852 lbs_deb_enter(LBS_DEB_JOIN);
853
854 if (!priv->in_progress_assoc_req) {
855 lbs_deb_join("ADHOC_RESP: no in-progress association "
856 "request\n");
857 ret = -1;
858 goto done;
859 }
860 bss = &priv->in_progress_assoc_req->bss;
861
862 /*
863 * Join result code 0 --> SUCCESS
864 */
865 if (result) {
866 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
867 if (priv->connect_status == LBS_CONNECTED)
868 lbs_mac_event_disconnected(priv);
869 ret = -1;
870 goto done;
871 }
872
873 /* Send a Media Connected event, according to the Spec */
874 priv->connect_status = LBS_CONNECTED;
875
876 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
877 /* Update the created network descriptor with the new BSSID */
878 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
879 }
880
881 /* Set the BSSID from the joined/started descriptor */
882 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
883
884 /* Set the new SSID to current SSID */
243e84e9 885 memcpy(&priv->curbssparams.ssid, &bss->ssid, IEEE80211_MAX_SSID_LEN);
822ac03a
DW
886 priv->curbssparams.ssid_len = bss->ssid_len;
887
888 netif_carrier_on(priv->dev);
889 if (!priv->tx_pending_len)
890 netif_wake_queue(priv->dev);
891
892 memset(&wrqu, 0, sizeof(wrqu));
893 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
894 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
895 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
896
897 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
898 print_ssid(ssid, bss->ssid, bss->ssid_len),
899 priv->curbssparams.bssid,
c14951fe 900 priv->channel);
822ac03a
DW
901
902done:
903 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
904 return ret;
905}
906
697900ac
HS
907/**
908 * @brief Join an adhoc network found in a previous scan
909 *
910 * @param priv A pointer to struct lbs_private structure
d5db2dfa 911 * @param assoc_req The association request describing the BSS to join
697900ac 912 *
f5fe1fda 913 * @return 0 on success, error on failure
697900ac 914 */
f5fe1fda 915static int lbs_adhoc_join(struct lbs_private *priv,
697900ac
HS
916 struct assoc_request *assoc_req)
917{
f5fe1fda 918 struct cmd_ds_802_11_ad_hoc_join cmd;
697900ac 919 struct bss_descriptor *bss = &assoc_req->bss;
d5db2dfa 920 u8 preamble = RADIO_PREAMBLE_LONG;
9387b7ca 921 DECLARE_SSID_BUF(ssid);
f5fe1fda
DW
922 u16 ratesize = 0;
923 int ret = 0;
d5db2dfa
DW
924
925 lbs_deb_enter(LBS_DEB_ASSOC);
697900ac
HS
926
927 lbs_deb_join("current SSID '%s', ssid length %u\n",
9387b7ca 928 print_ssid(ssid, priv->curbssparams.ssid,
697900ac
HS
929 priv->curbssparams.ssid_len),
930 priv->curbssparams.ssid_len);
931 lbs_deb_join("requested ssid '%s', ssid length %u\n",
9387b7ca 932 print_ssid(ssid, bss->ssid, bss->ssid_len),
697900ac
HS
933 bss->ssid_len);
934
935 /* check if the requested SSID is already joined */
936 if (priv->curbssparams.ssid_len &&
937 !lbs_ssid_cmp(priv->curbssparams.ssid,
938 priv->curbssparams.ssid_len,
939 bss->ssid, bss->ssid_len) &&
940 (priv->mode == IW_MODE_ADHOC) &&
941 (priv->connect_status == LBS_CONNECTED)) {
942 union iwreq_data wrqu;
943
944 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
945 "current, not attempting to re-join");
946
947 /* Send the re-association event though, because the association
948 * request really was successful, even if just a null-op.
949 */
950 memset(&wrqu, 0, sizeof(wrqu));
951 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
952 ETH_ALEN);
953 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
954 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
955 goto out;
956 }
957
d5db2dfa 958 /* Use short preamble only when both the BSS and firmware support it */
0e78ff8f 959 if (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
697900ac 960 lbs_deb_join("AdhocJoin: Short preamble\n");
d5db2dfa 961 preamble = RADIO_PREAMBLE_SHORT;
697900ac
HS
962 }
963
d5db2dfa
DW
964 ret = lbs_set_radio(priv, preamble, 1);
965 if (ret)
966 goto out;
697900ac
HS
967
968 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
969 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
970
971 priv->adhoccreate = 0;
c14951fe 972 priv->channel = bss->channel;
697900ac 973
f5fe1fda
DW
974 /* Build the join command */
975 memset(&cmd, 0, sizeof(cmd));
976 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
977
978 cmd.bss.type = CMD_BSS_TYPE_IBSS;
979 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
980
981 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
982 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
983
5fd164e9 984 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
f5fe1fda 985
5fd164e9
DW
986 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
987 sizeof(struct ieee_ie_ibss_param_set));
f5fe1fda
DW
988
989 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
990 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
991 bss->capability, CAPINFO_MASK);
992
993 /* information on BSSID descriptor passed to FW */
e174961c
JB
994 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
995 cmd.bss.bssid, cmd.bss.ssid);
f5fe1fda
DW
996
997 /* Only v8 and below support setting these */
998 if (priv->fwrelease < 0x09000000) {
999 /* failtimeout */
1000 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1001 /* probedelay */
1002 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1003 }
1004
1005 /* Copy Data rates from the rates recorded in scan response */
1006 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
ca4fe300 1007 ratesize = min_t(u16, ARRAY_SIZE(cmd.bss.rates), ARRAY_SIZE (bss->rates));
f5fe1fda
DW
1008 memcpy(cmd.bss.rates, bss->rates, ratesize);
1009 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
1010 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
1011 ret = -1;
1012 goto out;
1013 }
1014
1015 /* Copy the ad-hoc creation rates into Current BSS state structure */
1016 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1017 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
1018
1019 /* Set MSB on basic rates as the firmware requires, but _after_
1020 * copying to current bss rates.
1021 */
1022 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
1023
5fd164e9 1024 cmd.bss.ibss.atimwindow = bss->atimwindow;
f5fe1fda
DW
1025
1026 if (assoc_req->secinfo.wep_enabled) {
1027 u16 tmp = le16_to_cpu(cmd.bss.capability);
1028 tmp |= WLAN_CAPABILITY_PRIVACY;
1029 cmd.bss.capability = cpu_to_le16(tmp);
1030 }
1031
1032 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1033 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
1034
1035 /* wake up first */
1036 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1037 CMD_ACT_SET, 0, 0,
1038 &local_ps_mode);
1039 if (ret) {
1040 ret = -1;
1041 goto out;
1042 }
1043 }
1044
f5fe1fda 1045 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
822ac03a
DW
1046 if (ret == 0) {
1047 ret = lbs_adhoc_post(priv,
1048 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
1049 }
697900ac
HS
1050
1051out:
d5db2dfa 1052 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
697900ac
HS
1053 return ret;
1054}
1055
1056/**
1057 * @brief Start an Adhoc Network
1058 *
1059 * @param priv A pointer to struct lbs_private structure
d5db2dfa 1060 * @param assoc_req The association request describing the BSS to start
f5fe1fda
DW
1061 *
1062 * @return 0 on success, error on failure
697900ac 1063 */
f5fe1fda 1064static int lbs_adhoc_start(struct lbs_private *priv,
697900ac
HS
1065 struct assoc_request *assoc_req)
1066{
f5fe1fda 1067 struct cmd_ds_802_11_ad_hoc_start cmd;
0e78ff8f 1068 u8 preamble = RADIO_PREAMBLE_SHORT;
f5fe1fda
DW
1069 size_t ratesize = 0;
1070 u16 tmpcap = 0;
1071 int ret = 0;
9387b7ca 1072 DECLARE_SSID_BUF(ssid);
d5db2dfa
DW
1073
1074 lbs_deb_enter(LBS_DEB_ASSOC);
697900ac 1075
d5db2dfa
DW
1076 ret = lbs_set_radio(priv, preamble, 1);
1077 if (ret)
1078 goto out;
697900ac 1079
f5fe1fda
DW
1080 /* Build the start command */
1081 memset(&cmd, 0, sizeof(cmd));
1082 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
697900ac 1083
f5fe1fda
DW
1084 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
1085
1086 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
9387b7ca 1087 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
f5fe1fda
DW
1088 assoc_req->ssid_len);
1089
1090 cmd.bsstype = CMD_BSS_TYPE_IBSS;
1091
1092 if (priv->beacon_period == 0)
1093 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
1094 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
1095
1096 WARN_ON(!assoc_req->channel);
1097
1098 /* set Physical parameter set */
75b6a61a
DW
1099 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
1100 cmd.ds.header.len = 1;
5fd164e9 1101 cmd.ds.channel = assoc_req->channel;
f5fe1fda
DW
1102
1103 /* set IBSS parameter set */
75b6a61a
DW
1104 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1105 cmd.ibss.header.len = 2;
5fd164e9 1106 cmd.ibss.atimwindow = cpu_to_le16(0);
f5fe1fda
DW
1107
1108 /* set capability info */
1109 tmpcap = WLAN_CAPABILITY_IBSS;
2fa7a98f
DW
1110 if (assoc_req->secinfo.wep_enabled ||
1111 assoc_req->secinfo.WPAenabled ||
1112 assoc_req->secinfo.WPA2enabled) {
1113 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
f5fe1fda
DW
1114 tmpcap |= WLAN_CAPABILITY_PRIVACY;
1115 } else
2fa7a98f 1116 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
f5fe1fda
DW
1117
1118 cmd.capability = cpu_to_le16(tmpcap);
1119
1120 /* Only v8 and below support setting probe delay */
1121 if (priv->fwrelease < 0x09000000)
1122 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1123
1124 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
1125 memcpy(cmd.rates, lbs_bg_rates, ratesize);
1126
1127 /* Copy the ad-hoc creating rates into Current BSS state structure */
1128 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1129 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
1130
1131 /* Set MSB on basic rates as the firmware requires, but _after_
1132 * copying to current bss rates.
1133 */
1134 lbs_set_basic_rate_flags(cmd.rates, ratesize);
1135
1136 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
1137 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
1138
f5fe1fda
DW
1139 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
1140 assoc_req->channel, assoc_req->band);
1141
1142 priv->adhoccreate = 1;
1143 priv->mode = IW_MODE_ADHOC;
1144
1145 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
1146 if (ret == 0)
822ac03a
DW
1147 ret = lbs_adhoc_post(priv,
1148 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
697900ac 1149
d5db2dfa
DW
1150out:
1151 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
697900ac
HS
1152 return ret;
1153}
1154
f5fe1fda
DW
1155/**
1156 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
1157 *
1158 * @param priv A pointer to struct lbs_private structure
1159 * @return 0 on success, or an error
1160 */
1161int lbs_adhoc_stop(struct lbs_private *priv)
697900ac 1162{
f5fe1fda
DW
1163 struct cmd_ds_802_11_ad_hoc_stop cmd;
1164 int ret;
1165
1166 lbs_deb_enter(LBS_DEB_JOIN);
1167
1168 memset(&cmd, 0, sizeof (cmd));
1169 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
1170
1171 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
1172
1173 /* Clean up everything even if there was an error */
1174 lbs_mac_event_disconnected(priv);
1175
1176 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1177 return ret;
697900ac 1178}
e76850d6 1179
245bf20f
HS
1180static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
1181 struct bss_descriptor *match_bss)
1182{
64147c72
JL
1183 if (!secinfo->wep_enabled &&
1184 !secinfo->WPAenabled && !secinfo->WPA2enabled &&
1185 match_bss->wpa_ie[0] != WLAN_EID_GENERIC &&
1186 match_bss->rsn_ie[0] != WLAN_EID_RSN &&
1187 !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
245bf20f
HS
1188 return 1;
1189 else
1190 return 0;
1191}
1192
1193static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
1194 struct bss_descriptor *match_bss)
1195{
64147c72
JL
1196 if (secinfo->wep_enabled &&
1197 !secinfo->WPAenabled && !secinfo->WPA2enabled &&
1198 (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
245bf20f
HS
1199 return 1;
1200 else
1201 return 0;
1202}
1203
1204static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
1205 struct bss_descriptor *match_bss)
1206{
64147c72
JL
1207 if (!secinfo->wep_enabled && secinfo->WPAenabled &&
1208 (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
245bf20f
HS
1209 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
1210 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
1211 )
1212 return 1;
1213 else
1214 return 0;
1215}
1216
1217static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
1218 struct bss_descriptor *match_bss)
1219{
1220 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
2c706002 1221 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
245bf20f
HS
1222 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
1223 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
1224 )
1225 return 1;
1226 else
1227 return 0;
1228}
1229
1230static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
1231 struct bss_descriptor *match_bss)
1232{
64147c72
JL
1233 if (!secinfo->wep_enabled &&
1234 !secinfo->WPAenabled && !secinfo->WPA2enabled &&
1235 (match_bss->wpa_ie[0] != WLAN_EID_GENERIC) &&
1236 (match_bss->rsn_ie[0] != WLAN_EID_RSN) &&
1237 (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
245bf20f
HS
1238 return 1;
1239 else
1240 return 0;
1241}
1242
1243/**
1244 * @brief Check if a scanned network compatible with the driver settings
1245 *
1246 * WEP WPA WPA2 ad-hoc encrypt Network
1247 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
1248 * 0 0 0 0 NONE 0 0 0 yes No security
1249 * 1 0 0 0 NONE 1 0 0 yes Static WEP
1250 * 0 1 0 0 x 1x 1 x yes WPA
1251 * 0 0 1 0 x 1x x 1 yes WPA2
1252 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
1253 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
1254 *
1255 *
1256 * @param priv A pointer to struct lbs_private
1257 * @param index Index in scantable to check against current driver settings
1258 * @param mode Network mode: Infrastructure or IBSS
1259 *
1260 * @return Index in scantable, or error code if negative
1261 */
1262static int is_network_compatible(struct lbs_private *priv,
1263 struct bss_descriptor *bss, uint8_t mode)
1264{
1265 int matched = 0;
1266
1267 lbs_deb_enter(LBS_DEB_SCAN);
1268
1269 if (bss->mode != mode)
1270 goto done;
1271
1272 matched = match_bss_no_security(&priv->secinfo, bss);
1273 if (matched)
1274 goto done;
1275 matched = match_bss_static_wep(&priv->secinfo, bss);
1276 if (matched)
1277 goto done;
1278 matched = match_bss_wpa(&priv->secinfo, bss);
1279 if (matched) {
1280 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
1281 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
1282 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
1283 priv->secinfo.wep_enabled ? "e" : "d",
1284 priv->secinfo.WPAenabled ? "e" : "d",
1285 priv->secinfo.WPA2enabled ? "e" : "d",
1286 (bss->capability & WLAN_CAPABILITY_PRIVACY));
1287 goto done;
1288 }
1289 matched = match_bss_wpa2(&priv->secinfo, bss);
1290 if (matched) {
1291 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
1292 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
1293 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
1294 priv->secinfo.wep_enabled ? "e" : "d",
1295 priv->secinfo.WPAenabled ? "e" : "d",
1296 priv->secinfo.WPA2enabled ? "e" : "d",
1297 (bss->capability & WLAN_CAPABILITY_PRIVACY));
1298 goto done;
1299 }
1300 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
1301 if (matched) {
1302 lbs_deb_scan("is_network_compatible() dynamic WEP: "
1303 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
1304 bss->wpa_ie[0], bss->rsn_ie[0],
1305 (bss->capability & WLAN_CAPABILITY_PRIVACY));
1306 goto done;
1307 }
1308
1309 /* bss security settings don't match those configured on card */
1310 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
1311 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
1312 bss->wpa_ie[0], bss->rsn_ie[0],
1313 priv->secinfo.wep_enabled ? "e" : "d",
1314 priv->secinfo.WPAenabled ? "e" : "d",
1315 priv->secinfo.WPA2enabled ? "e" : "d",
1316 (bss->capability & WLAN_CAPABILITY_PRIVACY));
1317
1318done:
1319 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
1320 return matched;
1321}
1322
1323/**
1324 * @brief This function finds a specific compatible BSSID in the scan list
1325 *
1326 * Used in association code
1327 *
1328 * @param priv A pointer to struct lbs_private
1329 * @param bssid BSSID to find in the scan list
1330 * @param mode Network mode: Infrastructure or IBSS
1331 *
1332 * @return index in BSSID list, or error return code (< 0)
1333 */
1334static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
1335 uint8_t *bssid, uint8_t mode)
1336{
1337 struct bss_descriptor *iter_bss;
1338 struct bss_descriptor *found_bss = NULL;
1339
1340 lbs_deb_enter(LBS_DEB_SCAN);
1341
1342 if (!bssid)
1343 goto out;
1344
1345 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
1346
1347 /* Look through the scan table for a compatible match. The loop will
1348 * continue past a matched bssid that is not compatible in case there
1349 * is an AP with multiple SSIDs assigned to the same BSSID
1350 */
1351 mutex_lock(&priv->lock);
1352 list_for_each_entry(iter_bss, &priv->network_list, list) {
1353 if (compare_ether_addr(iter_bss->bssid, bssid))
1354 continue; /* bssid doesn't match */
1355 switch (mode) {
1356 case IW_MODE_INFRA:
1357 case IW_MODE_ADHOC:
1358 if (!is_network_compatible(priv, iter_bss, mode))
1359 break;
1360 found_bss = iter_bss;
1361 break;
1362 default:
1363 found_bss = iter_bss;
1364 break;
1365 }
1366 }
1367 mutex_unlock(&priv->lock);
1368
1369out:
1370 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1371 return found_bss;
1372}
1373
1374/**
1375 * @brief This function finds ssid in ssid list.
1376 *
1377 * Used in association code
1378 *
1379 * @param priv A pointer to struct lbs_private
1380 * @param ssid SSID to find in the list
1381 * @param bssid BSSID to qualify the SSID selection (if provided)
1382 * @param mode Network mode: Infrastructure or IBSS
1383 *
1384 * @return index in BSSID list
1385 */
1386static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
1387 uint8_t *ssid, uint8_t ssid_len,
1388 uint8_t *bssid, uint8_t mode,
1389 int channel)
1390{
1391 u32 bestrssi = 0;
1392 struct bss_descriptor *iter_bss = NULL;
1393 struct bss_descriptor *found_bss = NULL;
1394 struct bss_descriptor *tmp_oldest = NULL;
1395
1396 lbs_deb_enter(LBS_DEB_SCAN);
1397
1398 mutex_lock(&priv->lock);
1399
1400 list_for_each_entry(iter_bss, &priv->network_list, list) {
1401 if (!tmp_oldest ||
1402 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1403 tmp_oldest = iter_bss;
1404
1405 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1406 ssid, ssid_len) != 0)
1407 continue; /* ssid doesn't match */
1408 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1409 continue; /* bssid doesn't match */
1410 if ((channel > 0) && (iter_bss->channel != channel))
1411 continue; /* channel doesn't match */
1412
1413 switch (mode) {
1414 case IW_MODE_INFRA:
1415 case IW_MODE_ADHOC:
1416 if (!is_network_compatible(priv, iter_bss, mode))
1417 break;
1418
1419 if (bssid) {
1420 /* Found requested BSSID */
1421 found_bss = iter_bss;
1422 goto out;
1423 }
1424
1425 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1426 bestrssi = SCAN_RSSI(iter_bss->rssi);
1427 found_bss = iter_bss;
1428 }
1429 break;
1430 case IW_MODE_AUTO:
1431 default:
1432 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1433 bestrssi = SCAN_RSSI(iter_bss->rssi);
1434 found_bss = iter_bss;
1435 }
1436 break;
1437 }
1438 }
1439
1440out:
1441 mutex_unlock(&priv->lock);
1442 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1443 return found_bss;
1444}
1445
69f9032d 1446static int assoc_helper_essid(struct lbs_private *priv,
876c9d3a
MT
1447 struct assoc_request * assoc_req)
1448{
876c9d3a 1449 int ret = 0;
fcdb53db 1450 struct bss_descriptor * bss;
aeea0ab4 1451 int channel = -1;
9387b7ca 1452 DECLARE_SSID_BUF(ssid);
876c9d3a 1453
9012b28a 1454 lbs_deb_enter(LBS_DEB_ASSOC);
876c9d3a 1455
ef9a264b
DW
1456 /* FIXME: take channel into account when picking SSIDs if a channel
1457 * is set.
1458 */
1459
aeea0ab4
DW
1460 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1461 channel = assoc_req->channel;
1462
0765af44 1463 lbs_deb_assoc("SSID '%s' requested\n",
9387b7ca 1464 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
0dc5a290 1465 if (assoc_req->mode == IW_MODE_INFRA) {
10078321 1466 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
52933d81 1467 assoc_req->ssid_len);
876c9d3a 1468
aa21c004 1469 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
d8efea25 1470 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
fcdb53db 1471 if (bss != NULL) {
e76850d6 1472 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
822ac03a 1473 ret = lbs_try_associate(priv, assoc_req);
876c9d3a 1474 } else {
d8efea25 1475 lbs_deb_assoc("SSID not found; cannot associate\n");
876c9d3a 1476 }
0dc5a290 1477 } else if (assoc_req->mode == IW_MODE_ADHOC) {
876c9d3a
MT
1478 /* Scan for the network, do not save previous results. Stale
1479 * scan data will cause us to join a non-existant adhoc network
1480 */
10078321 1481 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
52933d81 1482 assoc_req->ssid_len);
876c9d3a
MT
1483
1484 /* Search for the requested SSID in the scan table */
aa21c004 1485 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
d8efea25 1486 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
fcdb53db 1487 if (bss != NULL) {
d8efea25 1488 lbs_deb_assoc("SSID found, will join\n");
e76850d6 1489 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
f5fe1fda 1490 lbs_adhoc_join(priv, assoc_req);
876c9d3a
MT
1491 } else {
1492 /* else send START command */
d8efea25 1493 lbs_deb_assoc("SSID not found, creating adhoc network\n");
e76850d6 1494 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
243e84e9 1495 IEEE80211_MAX_SSID_LEN);
d8efea25 1496 assoc_req->bss.ssid_len = assoc_req->ssid_len;
f5fe1fda 1497 lbs_adhoc_start(priv, assoc_req);
876c9d3a 1498 }
876c9d3a
MT
1499 }
1500
9012b28a 1501 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
876c9d3a
MT
1502 return ret;
1503}
1504
1505
69f9032d 1506static int assoc_helper_bssid(struct lbs_private *priv,
876c9d3a
MT
1507 struct assoc_request * assoc_req)
1508{
fcdb53db
DW
1509 int ret = 0;
1510 struct bss_descriptor * bss;
876c9d3a 1511
e174961c 1512 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
876c9d3a
MT
1513
1514 /* Search for index position in list for requested MAC */
aa21c004 1515 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
876c9d3a 1516 assoc_req->mode);
fcdb53db 1517 if (bss == NULL) {
e174961c
JB
1518 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1519 "cannot associate.\n", assoc_req->bssid);
876c9d3a
MT
1520 goto out;
1521 }
1522
e76850d6 1523 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
0dc5a290 1524 if (assoc_req->mode == IW_MODE_INFRA) {
822ac03a
DW
1525 ret = lbs_try_associate(priv, assoc_req);
1526 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1527 ret);
0dc5a290 1528 } else if (assoc_req->mode == IW_MODE_ADHOC) {
f5fe1fda 1529 lbs_adhoc_join(priv, assoc_req);
876c9d3a 1530 }
876c9d3a
MT
1531
1532out:
9012b28a 1533 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
876c9d3a
MT
1534 return ret;
1535}
1536
1537
69f9032d 1538static int assoc_helper_associate(struct lbs_private *priv,
876c9d3a
MT
1539 struct assoc_request * assoc_req)
1540{
1541 int ret = 0, done = 0;
1542
0765af44
HS
1543 lbs_deb_enter(LBS_DEB_ASSOC);
1544
876c9d3a
MT
1545 /* If we're given and 'any' BSSID, try associating based on SSID */
1546
1547 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
64147c72
JL
1548 if (compare_ether_addr(bssid_any, assoc_req->bssid) &&
1549 compare_ether_addr(bssid_off, assoc_req->bssid)) {
876c9d3a
MT
1550 ret = assoc_helper_bssid(priv, assoc_req);
1551 done = 1;
876c9d3a
MT
1552 }
1553 }
1554
1555 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1556 ret = assoc_helper_essid(priv, assoc_req);
876c9d3a
MT
1557 }
1558
0765af44 1559 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
876c9d3a
MT
1560 return ret;
1561}
1562
1563
69f9032d 1564static int assoc_helper_mode(struct lbs_private *priv,
876c9d3a
MT
1565 struct assoc_request * assoc_req)
1566{
876c9d3a
MT
1567 int ret = 0;
1568
9012b28a 1569 lbs_deb_enter(LBS_DEB_ASSOC);
876c9d3a 1570
aa21c004 1571 if (assoc_req->mode == priv->mode)
9012b28a 1572 goto done;
876c9d3a 1573
0dc5a290 1574 if (assoc_req->mode == IW_MODE_INFRA) {
aa21c004 1575 if (priv->psstate != PS_STATE_FULL_POWER)
10078321 1576 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
aa21c004 1577 priv->psmode = LBS802_11POWERMODECAM;
876c9d3a
MT
1578 }
1579
aa21c004 1580 priv->mode = assoc_req->mode;
fef0640e
HS
1581 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE,
1582 assoc_req->mode == IW_MODE_ADHOC ? 2 : 1);
876c9d3a 1583
9012b28a
HS
1584done:
1585 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
876c9d3a
MT
1586 return ret;
1587}
1588
69f9032d 1589static int assoc_helper_channel(struct lbs_private *priv,
ef9a264b
DW
1590 struct assoc_request * assoc_req)
1591{
ef9a264b
DW
1592 int ret = 0;
1593
1594 lbs_deb_enter(LBS_DEB_ASSOC);
1595
9f462577 1596 ret = lbs_update_channel(priv);
d1a469fd 1597 if (ret) {
23d36eec 1598 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
d1a469fd 1599 goto done;
ef9a264b
DW
1600 }
1601
c14951fe 1602 if (assoc_req->channel == priv->channel)
ef9a264b
DW
1603 goto done;
1604
8642f1f0 1605 if (priv->mesh_dev) {
86062134
DW
1606 /* Change mesh channel first; 21.p21 firmware won't let
1607 you change channel otherwise (even though it'll return
1608 an error to this */
edaea5ce
JC
1609 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1610 assoc_req->channel);
8642f1f0
DW
1611 }
1612
ef9a264b 1613 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
c14951fe 1614 priv->channel, assoc_req->channel);
ef9a264b 1615
2dd4b262
DW
1616 ret = lbs_set_channel(priv, assoc_req->channel);
1617 if (ret < 0)
23d36eec 1618 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
ef9a264b 1619
2dd4b262
DW
1620 /* FIXME: shouldn't need to grab the channel _again_ after setting
1621 * it since the firmware is supposed to return the new channel, but
1622 * whatever... */
9f462577 1623 ret = lbs_update_channel(priv);
d1a469fd 1624 if (ret) {
23d36eec 1625 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
d1a469fd
DW
1626 goto done;
1627 }
ef9a264b 1628
c14951fe 1629 if (assoc_req->channel != priv->channel) {
88ae2915 1630 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
ef9a264b 1631 assoc_req->channel);
8642f1f0 1632 goto restore_mesh;
ef9a264b
DW
1633 }
1634
64147c72
JL
1635 if (assoc_req->secinfo.wep_enabled &&
1636 (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1637 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)) {
ef9a264b
DW
1638 /* Make sure WEP keys are re-sent to firmware */
1639 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1640 }
1641
1642 /* Must restart/rejoin adhoc networks after channel change */
23d36eec 1643 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
ef9a264b 1644
8642f1f0
DW
1645 restore_mesh:
1646 if (priv->mesh_dev)
edaea5ce 1647 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
c14951fe 1648 priv->channel);
8642f1f0
DW
1649
1650 done:
ef9a264b
DW
1651 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1652 return ret;
1653}
1654
1655
69f9032d 1656static int assoc_helper_wep_keys(struct lbs_private *priv,
f70dd451 1657 struct assoc_request *assoc_req)
876c9d3a 1658{
876c9d3a
MT
1659 int i;
1660 int ret = 0;
1661
9012b28a 1662 lbs_deb_enter(LBS_DEB_ASSOC);
876c9d3a
MT
1663
1664 /* Set or remove WEP keys */
f70dd451
DW
1665 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1666 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1667 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1668 else
1669 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
876c9d3a
MT
1670
1671 if (ret)
1672 goto out;
1673
1674 /* enable/disable the MAC's WEP packet filter */
889c05bd 1675 if (assoc_req->secinfo.wep_enabled)
d9e9778c 1676 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
876c9d3a 1677 else
d9e9778c 1678 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
f70dd451 1679
c97329e2 1680 lbs_set_mac_control(priv);
876c9d3a 1681
aa21c004 1682 mutex_lock(&priv->lock);
876c9d3a 1683
aa21c004 1684 /* Copy WEP keys into priv wep key fields */
876c9d3a 1685 for (i = 0; i < 4; i++) {
aa21c004 1686 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
f70dd451 1687 sizeof(struct enc_key));
876c9d3a 1688 }
aa21c004 1689 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
876c9d3a 1690
aa21c004 1691 mutex_unlock(&priv->lock);
876c9d3a
MT
1692
1693out:
9012b28a 1694 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
876c9d3a
MT
1695 return ret;
1696}
1697
69f9032d 1698static int assoc_helper_secinfo(struct lbs_private *priv,
876c9d3a
MT
1699 struct assoc_request * assoc_req)
1700{
876c9d3a 1701 int ret = 0;
4f59abf1
DW
1702 uint16_t do_wpa;
1703 uint16_t rsn = 0;
876c9d3a 1704
9012b28a 1705 lbs_deb_enter(LBS_DEB_ASSOC);
876c9d3a 1706
aa21c004 1707 memcpy(&priv->secinfo, &assoc_req->secinfo,
10078321 1708 sizeof(struct lbs_802_11_security));
876c9d3a 1709
c97329e2 1710 lbs_set_mac_control(priv);
876c9d3a 1711
18c96c34
DW
1712 /* If RSN is already enabled, don't try to enable it again, since
1713 * ENABLE_RSN resets internal state machines and will clobber the
1714 * 4-way WPA handshake.
1715 */
1716
1717 /* Get RSN enabled/disabled */
4f59abf1 1718 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
18c96c34 1719 if (ret) {
23d36eec 1720 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
18c96c34
DW
1721 goto out;
1722 }
1723
1724 /* Don't re-enable RSN if it's already enabled */
4f59abf1 1725 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
18c96c34
DW
1726 if (do_wpa == rsn)
1727 goto out;
1728
1729 /* Set RSN enabled/disabled */
4f59abf1 1730 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
90a42210
DW
1731
1732out:
9012b28a 1733 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
876c9d3a
MT
1734 return ret;
1735}
1736
1737
69f9032d 1738static int assoc_helper_wpa_keys(struct lbs_private *priv,
876c9d3a
MT
1739 struct assoc_request * assoc_req)
1740{
1741 int ret = 0;
2bcde51d 1742 unsigned int flags = assoc_req->flags;
876c9d3a 1743
9012b28a 1744 lbs_deb_enter(LBS_DEB_ASSOC);
876c9d3a 1745
2bcde51d
DW
1746 /* Work around older firmware bug where WPA unicast and multicast
1747 * keys must be set independently. Seen in SDIO parts with firmware
1748 * version 5.0.11p0.
1749 */
876c9d3a 1750
2bcde51d
DW
1751 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1752 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
9e1228d0 1753 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
2bcde51d
DW
1754 assoc_req->flags = flags;
1755 }
1756
1757 if (ret)
1758 goto out;
1759
ce8d096d
AY
1760 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1761 sizeof(struct enc_key));
1762
2bcde51d
DW
1763 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1764 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1765
9e1228d0 1766 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
2bcde51d 1767 assoc_req->flags = flags;
ce8d096d
AY
1768
1769 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1770 sizeof(struct enc_key));
2bcde51d
DW
1771 }
1772
1773out:
9012b28a 1774 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
876c9d3a
MT
1775 return ret;
1776}
1777
1778
69f9032d 1779static int assoc_helper_wpa_ie(struct lbs_private *priv,
876c9d3a
MT
1780 struct assoc_request * assoc_req)
1781{
876c9d3a
MT
1782 int ret = 0;
1783
9012b28a 1784 lbs_deb_enter(LBS_DEB_ASSOC);
876c9d3a
MT
1785
1786 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
aa21c004
DW
1787 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1788 priv->wpa_ie_len = assoc_req->wpa_ie_len;
876c9d3a 1789 } else {
aa21c004
DW
1790 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1791 priv->wpa_ie_len = 0;
876c9d3a
MT
1792 }
1793
9012b28a 1794 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
876c9d3a
MT
1795 return ret;
1796}
1797
1798
aa21c004 1799static int should_deauth_infrastructure(struct lbs_private *priv,
876c9d3a
MT
1800 struct assoc_request * assoc_req)
1801{
0765af44
HS
1802 int ret = 0;
1803
aa21c004 1804 if (priv->connect_status != LBS_CONNECTED)
876c9d3a
MT
1805 return 0;
1806
52507c20 1807 lbs_deb_enter(LBS_DEB_ASSOC);
876c9d3a 1808 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
0765af44
HS
1809 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1810 ret = 1;
1811 goto out;
876c9d3a
MT
1812 }
1813
1814 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
aa21c004 1815 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
0765af44
HS
1816 lbs_deb_assoc("Deauthenticating due to new security\n");
1817 ret = 1;
1818 goto out;
876c9d3a
MT
1819 }
1820 }
1821
1822 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
0765af44
HS
1823 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1824 ret = 1;
1825 goto out;
876c9d3a
MT
1826 }
1827
fff47f10 1828 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
0765af44
HS
1829 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1830 ret = 1;
1831 goto out;
fff47f10
LCCR
1832 }
1833
876c9d3a
MT
1834 /* FIXME: deal with 'auto' mode somehow */
1835 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
0765af44
HS
1836 if (assoc_req->mode != IW_MODE_INFRA) {
1837 lbs_deb_assoc("Deauthenticating due to leaving "
1838 "infra mode\n");
1839 ret = 1;
1840 goto out;
1841 }
876c9d3a
MT
1842 }
1843
0765af44
HS
1844out:
1845 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
52507c20 1846 return ret;
876c9d3a
MT
1847}
1848
1849
aa21c004 1850static int should_stop_adhoc(struct lbs_private *priv,
876c9d3a
MT
1851 struct assoc_request * assoc_req)
1852{
0765af44
HS
1853 lbs_deb_enter(LBS_DEB_ASSOC);
1854
aa21c004 1855 if (priv->connect_status != LBS_CONNECTED)
876c9d3a
MT
1856 return 0;
1857
aa21c004
DW
1858 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1859 priv->curbssparams.ssid_len,
d8efea25 1860 assoc_req->ssid, assoc_req->ssid_len) != 0)
876c9d3a
MT
1861 return 1;
1862
1863 /* FIXME: deal with 'auto' mode somehow */
1864 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
0dc5a290 1865 if (assoc_req->mode != IW_MODE_ADHOC)
876c9d3a
MT
1866 return 1;
1867 }
1868
ef9a264b 1869 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
c14951fe 1870 if (assoc_req->channel != priv->channel)
ef9a264b
DW
1871 return 1;
1872 }
1873
0765af44 1874 lbs_deb_leave(LBS_DEB_ASSOC);
876c9d3a
MT
1875 return 0;
1876}
1877
1878
245bf20f
HS
1879/**
1880 * @brief This function finds the best SSID in the Scan List
1881 *
1882 * Search the scan table for the best SSID that also matches the current
1883 * adapter network preference (infrastructure or adhoc)
1884 *
1885 * @param priv A pointer to struct lbs_private
1886 *
1887 * @return index in BSSID list
1888 */
1889static struct bss_descriptor *lbs_find_best_ssid_in_list(
1890 struct lbs_private *priv, uint8_t mode)
1891{
1892 uint8_t bestrssi = 0;
1893 struct bss_descriptor *iter_bss;
1894 struct bss_descriptor *best_bss = NULL;
1895
1896 lbs_deb_enter(LBS_DEB_SCAN);
1897
1898 mutex_lock(&priv->lock);
1899
1900 list_for_each_entry(iter_bss, &priv->network_list, list) {
1901 switch (mode) {
1902 case IW_MODE_INFRA:
1903 case IW_MODE_ADHOC:
1904 if (!is_network_compatible(priv, iter_bss, mode))
1905 break;
1906 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1907 break;
1908 bestrssi = SCAN_RSSI(iter_bss->rssi);
1909 best_bss = iter_bss;
1910 break;
1911 case IW_MODE_AUTO:
1912 default:
1913 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1914 break;
1915 bestrssi = SCAN_RSSI(iter_bss->rssi);
1916 best_bss = iter_bss;
1917 break;
1918 }
1919 }
1920
1921 mutex_unlock(&priv->lock);
1922 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1923 return best_bss;
1924}
1925
1926/**
1927 * @brief Find the best AP
1928 *
1929 * Used from association worker.
1930 *
1931 * @param priv A pointer to struct lbs_private structure
1932 * @param pSSID A pointer to AP's ssid
1933 *
1934 * @return 0--success, otherwise--fail
1935 */
1936static int lbs_find_best_network_ssid(struct lbs_private *priv,
1937 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1938 uint8_t *out_mode)
1939{
1940 int ret = -1;
1941 struct bss_descriptor *found;
1942
1943 lbs_deb_enter(LBS_DEB_SCAN);
1944
1945 priv->scan_ssid_len = 0;
1946 lbs_scan_networks(priv, 1);
1947 if (priv->surpriseremoved)
1948 goto out;
1949
1950 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1951 if (found && (found->ssid_len > 0)) {
243e84e9 1952 memcpy(out_ssid, &found->ssid, IEEE80211_MAX_SSID_LEN);
245bf20f
HS
1953 *out_ssid_len = found->ssid_len;
1954 *out_mode = found->mode;
1955 ret = 0;
1956 }
1957
1958out:
1959 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1960 return ret;
1961}
1962
1963
10078321 1964void lbs_association_worker(struct work_struct *work)
876c9d3a 1965{
69f9032d
HS
1966 struct lbs_private *priv = container_of(work, struct lbs_private,
1967 assoc_work.work);
876c9d3a
MT
1968 struct assoc_request * assoc_req = NULL;
1969 int ret = 0;
1970 int find_any_ssid = 0;
9387b7ca 1971 DECLARE_SSID_BUF(ssid);
876c9d3a 1972
9012b28a 1973 lbs_deb_enter(LBS_DEB_ASSOC);
876c9d3a 1974
aa21c004
DW
1975 mutex_lock(&priv->lock);
1976 assoc_req = priv->pending_assoc_req;
1977 priv->pending_assoc_req = NULL;
1978 priv->in_progress_assoc_req = assoc_req;
1979 mutex_unlock(&priv->lock);
876c9d3a 1980
9012b28a
HS
1981 if (!assoc_req)
1982 goto done;
876c9d3a 1983
0765af44
HS
1984 lbs_deb_assoc(
1985 "Association Request:\n"
1986 " flags: 0x%08lx\n"
1987 " SSID: '%s'\n"
1988 " chann: %d\n"
1989 " band: %d\n"
1990 " mode: %d\n"
e174961c 1991 " BSSID: %pM\n"
0765af44
HS
1992 " secinfo: %s%s%s\n"
1993 " auth_mode: %d\n",
1994 assoc_req->flags,
9387b7ca 1995 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
0765af44 1996 assoc_req->channel, assoc_req->band, assoc_req->mode,
e174961c 1997 assoc_req->bssid,
0765af44
HS
1998 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1999 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
2000 assoc_req->secinfo.wep_enabled ? " WEP" : "",
2001 assoc_req->secinfo.auth_mode);
876c9d3a
MT
2002
2003 /* If 'any' SSID was specified, find an SSID to associate with */
64147c72
JL
2004 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags) &&
2005 !assoc_req->ssid_len)
876c9d3a
MT
2006 find_any_ssid = 1;
2007
2008 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
2009 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
64147c72
JL
2010 if (compare_ether_addr(assoc_req->bssid, bssid_any) &&
2011 compare_ether_addr(assoc_req->bssid, bssid_off))
876c9d3a
MT
2012 find_any_ssid = 0;
2013 }
2014
2015 if (find_any_ssid) {
877cb0d4 2016 u8 new_mode = assoc_req->mode;
876c9d3a 2017
10078321 2018 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
d8efea25 2019 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
876c9d3a 2020 if (ret) {
9012b28a 2021 lbs_deb_assoc("Could not find best network\n");
876c9d3a
MT
2022 ret = -ENETUNREACH;
2023 goto out;
2024 }
2025
2026 /* Ensure we switch to the mode of the AP */
0dc5a290 2027 if (assoc_req->mode == IW_MODE_AUTO) {
876c9d3a
MT
2028 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
2029 assoc_req->mode = new_mode;
2030 }
2031 }
2032
2033 /*
2034 * Check if the attributes being changing require deauthentication
2035 * from the currently associated infrastructure access point.
2036 */
aa21c004
DW
2037 if (priv->mode == IW_MODE_INFRA) {
2038 if (should_deauth_infrastructure(priv, assoc_req)) {
191bb40e
DW
2039 ret = lbs_cmd_80211_deauthenticate(priv,
2040 priv->curbssparams.bssid,
2041 WLAN_REASON_DEAUTH_LEAVING);
876c9d3a 2042 if (ret) {
9012b28a 2043 lbs_deb_assoc("Deauthentication due to new "
876c9d3a
MT
2044 "configuration request failed: %d\n",
2045 ret);
2046 }
2047 }
aa21c004
DW
2048 } else if (priv->mode == IW_MODE_ADHOC) {
2049 if (should_stop_adhoc(priv, assoc_req)) {
f5fe1fda 2050 ret = lbs_adhoc_stop(priv);
876c9d3a 2051 if (ret) {
9012b28a 2052 lbs_deb_assoc("Teardown of AdHoc network due to "
876c9d3a
MT
2053 "new configuration request failed: %d\n",
2054 ret);
2055 }
2056
2057 }
2058 }
2059
2060 /* Send the various configuration bits to the firmware */
2061 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
2062 ret = assoc_helper_mode(priv, assoc_req);
0765af44 2063 if (ret)
876c9d3a 2064 goto out;
876c9d3a
MT
2065 }
2066
ef9a264b
DW
2067 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
2068 ret = assoc_helper_channel(priv, assoc_req);
0765af44 2069 if (ret)
ef9a264b 2070 goto out;
ef9a264b
DW
2071 }
2072
876c9d3a
MT
2073 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
2074 ret = assoc_helper_secinfo(priv, assoc_req);
0765af44 2075 if (ret)
876c9d3a 2076 goto out;
876c9d3a
MT
2077 }
2078
2079 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
2080 ret = assoc_helper_wpa_ie(priv, assoc_req);
0765af44 2081 if (ret)
876c9d3a 2082 goto out;
876c9d3a
MT
2083 }
2084
d3d5621a
JL
2085 /*
2086 * v10 FW wants WPA keys to be set/cleared before WEP key operations,
2087 * otherwise it will fail to correctly associate to WEP networks.
2088 * Other firmware versions don't appear to care.
2089 */
64147c72
JL
2090 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags) ||
2091 test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
876c9d3a 2092 ret = assoc_helper_wpa_keys(priv, assoc_req);
0765af44 2093 if (ret)
876c9d3a 2094 goto out;
876c9d3a
MT
2095 }
2096
64147c72
JL
2097 if (test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags) ||
2098 test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1625c148
SO
2099 ret = assoc_helper_wep_keys(priv, assoc_req);
2100 if (ret)
2101 goto out;
2102 }
2103
2104
876c9d3a
MT
2105 /* SSID/BSSID should be the _last_ config option set, because they
2106 * trigger the association attempt.
2107 */
64147c72
JL
2108 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags) ||
2109 test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
876c9d3a
MT
2110 int success = 1;
2111
2112 ret = assoc_helper_associate(priv, assoc_req);
2113 if (ret) {
91843463 2114 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
876c9d3a
MT
2115 ret);
2116 success = 0;
2117 }
2118
aa21c004 2119 if (priv->connect_status != LBS_CONNECTED) {
91843463
HS
2120 lbs_deb_assoc("ASSOC: association unsuccessful, "
2121 "not connected\n");
876c9d3a
MT
2122 success = 0;
2123 }
2124
2125 if (success) {
e174961c
JB
2126 lbs_deb_assoc("associated to %pM\n",
2127 priv->curbssparams.bssid);
10078321 2128 lbs_prepare_and_send_command(priv,
0aef64d7
DW
2129 CMD_802_11_RSSI,
2130 0, CMD_OPTION_WAITFORRSP, 0, NULL);
876c9d3a 2131 } else {
876c9d3a
MT
2132 ret = -1;
2133 }
2134 }
2135
2136out:
2137 if (ret) {
9012b28a 2138 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
876c9d3a
MT
2139 ret);
2140 }
e76850d6 2141
aa21c004
DW
2142 mutex_lock(&priv->lock);
2143 priv->in_progress_assoc_req = NULL;
2144 mutex_unlock(&priv->lock);
876c9d3a 2145 kfree(assoc_req);
9012b28a
HS
2146
2147done:
2148 lbs_deb_leave(LBS_DEB_ASSOC);
876c9d3a
MT
2149}
2150
2151
2152/*
2153 * Caller MUST hold any necessary locks
2154 */
aa21c004 2155struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
876c9d3a
MT
2156{
2157 struct assoc_request * assoc_req;
2158
0765af44 2159 lbs_deb_enter(LBS_DEB_ASSOC);
aa21c004
DW
2160 if (!priv->pending_assoc_req) {
2161 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
e76850d6 2162 GFP_KERNEL);
aa21c004 2163 if (!priv->pending_assoc_req) {
876c9d3a
MT
2164 lbs_pr_info("Not enough memory to allocate association"
2165 " request!\n");
2166 return NULL;
2167 }
2168 }
2169
2170 /* Copy current configuration attributes to the association request,
2171 * but don't overwrite any that are already set.
2172 */
aa21c004 2173 assoc_req = priv->pending_assoc_req;
876c9d3a 2174 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
aa21c004 2175 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
243e84e9 2176 IEEE80211_MAX_SSID_LEN);
aa21c004 2177 assoc_req->ssid_len = priv->curbssparams.ssid_len;
876c9d3a
MT
2178 }
2179
2180 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
c14951fe 2181 assoc_req->channel = priv->channel;
876c9d3a 2182
e76850d6 2183 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
aa21c004 2184 assoc_req->band = priv->curbssparams.band;
e76850d6 2185
876c9d3a 2186 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
aa21c004 2187 assoc_req->mode = priv->mode;
876c9d3a
MT
2188
2189 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
aa21c004 2190 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
876c9d3a
MT
2191 ETH_ALEN);
2192 }
2193
2194 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
2195 int i;
2196 for (i = 0; i < 4; i++) {
aa21c004 2197 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
1443b653 2198 sizeof(struct enc_key));
876c9d3a
MT
2199 }
2200 }
2201
2202 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
aa21c004 2203 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
876c9d3a
MT
2204
2205 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
aa21c004 2206 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
1443b653 2207 sizeof(struct enc_key));
876c9d3a
MT
2208 }
2209
2210 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
aa21c004 2211 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
1443b653 2212 sizeof(struct enc_key));
876c9d3a
MT
2213 }
2214
2215 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
aa21c004 2216 memcpy(&assoc_req->secinfo, &priv->secinfo,
10078321 2217 sizeof(struct lbs_802_11_security));
876c9d3a
MT
2218 }
2219
2220 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
aa21c004 2221 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
876c9d3a 2222 MAX_WPA_IE_LEN);
aa21c004 2223 assoc_req->wpa_ie_len = priv->wpa_ie_len;
876c9d3a
MT
2224 }
2225
0765af44 2226 lbs_deb_leave(LBS_DEB_ASSOC);
876c9d3a
MT
2227 return assoc_req;
2228}
697900ac
HS
2229
2230
191bb40e
DW
2231/**
2232 * @brief Deauthenticate from a specific BSS
2233 *
2234 * @param priv A pointer to struct lbs_private structure
2235 * @param bssid The specific BSS to deauthenticate from
2236 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
2237 *
2238 * @return 0 on success, error on failure
2239 */
2240int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
2241 u16 reason)
697900ac 2242{
191bb40e
DW
2243 struct cmd_ds_802_11_deauthenticate cmd;
2244 int ret;
697900ac
HS
2245
2246 lbs_deb_enter(LBS_DEB_JOIN);
2247
191bb40e
DW
2248 memset(&cmd, 0, sizeof(cmd));
2249 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2250 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
2251 cmd.reasoncode = cpu_to_le16(reason);
697900ac 2252
191bb40e 2253 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
697900ac 2254
191bb40e
DW
2255 /* Clean up everything even if there was an error; can't assume that
2256 * we're still authenticated to the AP after trying to deauth.
2257 */
2258 lbs_mac_event_disconnected(priv);
697900ac
HS
2259
2260 lbs_deb_leave(LBS_DEB_JOIN);
191bb40e 2261 return ret;
697900ac
HS
2262}
2263
This page took 0.815714 seconds and 5 git commands to generate.