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