blob: 12a2ef9dacea9aca87c59f0cd8d41b2d8732d6c1 [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/* Copyright (C) 2006, Red Hat, Inc. */
2
John W. Linville7e272fc2008-09-24 18:13:14 -04003#include <linux/types.h>
Dan Williams3cf209312007-05-25 17:28:30 -04004#include <linux/etherdevice.h>
Johannes Berg2c7060022008-10-30 22:09:54 +01005#include <linux/ieee80211.h>
6#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
John W. Linville7e272fc2008-09-24 18:13:14 -04008#include <net/lib80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02009
10#include "assoc.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020011#include "decl.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020012#include "host.h"
Holger Schurig245bf202008-04-02 16:27:42 +020013#include "scan.h"
Dan Williams2dd4b262007-12-11 16:54:15 -050014#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020015
Ihar Hrachyshka5a6e0432008-01-25 14:15:00 +010016static 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 };
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020020
Dan Williamsbe0d76e2009-05-22 20:05:25 -040021/* 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)
Holger Schurig697900a2008-04-02 16:27:10 +020024 */
25#define CAPINFO_MASK (~(0xda00))
26
Holger Schurig2d465022009-10-22 15:30:48 +020027/**
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
Holger Schurig697900a2008-04-02 16:27:10 +020034
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040035/**
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
Dan Williamsca4fe302009-08-21 09:35:20 -050045 * @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
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040047 *
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{
Roel Kluin1e3d31c2009-08-02 09:44:12 +020054 int i, j;
Dan Williamsca4fe302009-08-21 09:35:20 -050055 u8 intersection[MAX_RATES];
56 u16 intersection_size;
57 u16 num_rates = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040058
Dan Williamsca4fe302009-08-21 09:35:20 -050059 intersection_size = min_t(u16, *rates_size, ARRAY_SIZE(intersection));
Andrey Yurovsky6f632d572009-08-13 17:34:40 -070060
Dan Williamsca4fe302009-08-21 09:35:20 -050061 /* 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];
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040066 }
67 }
68
69 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
Dan Williamsca4fe302009-08-21 09:35:20 -050070 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);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040073 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
74
75 if (!priv->enablehwauto) {
Dan Williamsca4fe302009-08-21 09:35:20 -050076 for (i = 0; i < num_rates; i++) {
77 if (intersection[i] == priv->cur_rate)
78 goto done;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040079 }
Dan Williamsca4fe302009-08-21 09:35:20 -050080 lbs_pr_alert("Previously set fixed data rate %#x isn't "
81 "compatible with the network.\n", priv->cur_rate);
82 return -1;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040083 }
Dan Williamsca4fe302009-08-21 09:35:20 -050084
85done:
86 memset(rates, 0, *rates_size);
87 *rates_size = num_rates;
88 memcpy(rates, intersection, num_rates);
Roel Kluin1e3d31c2009-08-02 09:44:12 +020089 return 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040090}
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
Holger Schurig697900a2008-04-02 16:27:10 +0200112
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400113static 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;
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400141
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
Johannes Berge91d8332009-07-15 17:21:41 +0200149 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400150
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
Dan Williams822ac03a2009-05-22 20:07:14 -0400157
Holger Schurigd0de3742009-10-22 15:30:51 +0200158int 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) {
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200283 cmd.hdr.size = cpu_to_le16(sizeof(struct cmd_header) + 2);
Holger Schurigd0de3742009-10-22 15:30:51 +0200284 } 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);
Holger Schurig48631de2009-12-02 15:26:04 +0100394 if (!ret && cmd_action == CMD_ACT_GET)
Holger Schurigd0de3742009-10-22 15:30:51 +0200395 priv->enablehwauto = le16_to_cpu(cmd.enablehwauto);
Holger Schurigd0de3742009-10-22 15:30:51 +0200396
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);
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200458 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) +
459 sizeof(struct cmd_header));
Holger Schurigd0de3742009-10-22 15:30:51 +0200460 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)
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200517 + sizeof(struct cmd_header));
Holger Schurigd0de3742009-10-22 15:30:51 +0200518 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
Dan Williams822ac03a2009-05-22 20:07:14 -0400547static 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 */
Holger Schurig243e84e2009-10-22 15:30:47 +0200626 memcpy(&priv->curbssparams.ssid, &bss->ssid, IEEE80211_MAX_SSID_LEN);
Dan Williams822ac03a2009-05-22 20:07:14 -0400627 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);
Dan Williamsca4fe302009-08-21 09:35:20 -0500720 tmplen = min_t(u16, ARRAY_SIZE(bss->rates), MAX_RATES);
Roel Kluin1e3d31c2009-08-02 09:44:12 +0200721 memcpy(&rates->rates, &bss->rates, tmplen);
Dan Williams822ac03a2009-05-22 20:07:14 -0400722 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) {
Dan Williams822ac03a2009-05-22 20:07:14 -0400741 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
Johannes Berge91d8332009-07-15 17:21:41 +0200748 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
749 bss->bssid, priv->secinfo.auth_mode);
Dan Williams822ac03a2009-05-22 20:07:14 -0400750 }
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 */
Holger Schurigc14951f2009-10-22 15:30:50 +0200769 priv->channel = bss->phy.ds.channel;
Dan Williams822ac03a2009-05-22 20:07:14 -0400770
Dan Williams822ac03a2009-05-22 20:07:14 -0400771 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
Holger Schurig697900a2008-04-02 16:27:10 +0200782/**
783 * @brief Associate to a specific BSS discovered in a scan
784 *
785 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400786 * @param assoc_req The association request describing the BSS to associate with
Holger Schurig697900a2008-04-02 16:27:10 +0200787 *
788 * @return 0-success, otherwise fail
789 */
Dan Williams822ac03a2009-05-22 20:07:14 -0400790static int lbs_try_associate(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200791 struct assoc_request *assoc_req)
792{
793 int ret;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400794 u8 preamble = RADIO_PREAMBLE_LONG;
Holger Schurig697900a2008-04-02 16:27:10 +0200795
796 lbs_deb_enter(LBS_DEB_ASSOC);
797
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400798 /* 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 }
Holger Schurig697900a2008-04-02 16:27:10 +0200807
Dan Williamsd5db2df2008-08-21 17:51:07 -0400808 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig0e78ff82009-12-02 15:26:03 +0100809 if (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Dan Williamsd5db2df2008-08-21 17:51:07 -0400810 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200811
Dan Williamsd5db2df2008-08-21 17:51:07 -0400812 ret = lbs_set_radio(priv, preamble, 1);
813 if (ret)
814 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200815
Dan Williams822ac03a2009-05-22 20:07:14 -0400816 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
Holger Schurig697900a2008-04-02 16:27:10 +0200817
Dan Williamsd5db2df2008-08-21 17:51:07 -0400818out:
Holger Schurig697900a2008-04-02 16:27:10 +0200819 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
820 return ret;
821}
822
Dan Williams822ac03a2009-05-22 20:07:14 -0400823static 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 */
Holger Schurig243e84e2009-10-22 15:30:47 +0200866 memcpy(&priv->curbssparams.ssid, &bss->ssid, IEEE80211_MAX_SSID_LEN);
Dan Williams822ac03a2009-05-22 20:07:14 -0400867 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,
Holger Schurigc14951f2009-10-22 15:30:50 +0200881 priv->channel);
Dan Williams822ac03a2009-05-22 20:07:14 -0400882
883done:
884 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
885 return ret;
886}
887
Holger Schurig697900a2008-04-02 16:27:10 +0200888/**
889 * @brief Join an adhoc network found in a previous scan
890 *
891 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400892 * @param assoc_req The association request describing the BSS to join
Holger Schurig697900a2008-04-02 16:27:10 +0200893 *
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400894 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200895 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400896static int lbs_adhoc_join(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200897 struct assoc_request *assoc_req)
898{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400899 struct cmd_ds_802_11_ad_hoc_join cmd;
Holger Schurig697900a2008-04-02 16:27:10 +0200900 struct bss_descriptor *bss = &assoc_req->bss;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400901 u8 preamble = RADIO_PREAMBLE_LONG;
John W. Linville9387b7c2008-09-30 20:59:05 -0400902 DECLARE_SSID_BUF(ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400903 u16 ratesize = 0;
904 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400905
906 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200907
908 lbs_deb_join("current SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400909 print_ssid(ssid, priv->curbssparams.ssid,
Holger Schurig697900a2008-04-02 16:27:10 +0200910 priv->curbssparams.ssid_len),
911 priv->curbssparams.ssid_len);
912 lbs_deb_join("requested ssid '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400913 print_ssid(ssid, bss->ssid, bss->ssid_len),
Holger Schurig697900a2008-04-02 16:27:10 +0200914 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
Dan Williamsd5db2df2008-08-21 17:51:07 -0400939 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig0e78ff82009-12-02 15:26:03 +0100940 if (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Holger Schurig697900a2008-04-02 16:27:10 +0200941 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400942 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200943 }
944
Dan Williamsd5db2df2008-08-21 17:51:07 -0400945 ret = lbs_set_radio(priv, preamble, 1);
946 if (ret)
947 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200948
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;
Holger Schurigc14951f2009-10-22 15:30:50 +0200953 priv->channel = bss->channel;
Holger Schurig697900a2008-04-02 16:27:10 +0200954
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400955 /* 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
Dan Williams5fd164e2009-05-22 20:01:21 -0400965 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400966
Dan Williams5fd164e2009-05-22 20:01:21 -0400967 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
968 sizeof(struct ieee_ie_ibss_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400969
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 */
Johannes Berge1749612008-10-27 15:59:26 -0700975 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
976 cmd.bss.bssid, cmd.bss.ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400977
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));
Dan Williamsca4fe302009-08-21 09:35:20 -0500988 ratesize = min_t(u16, ARRAY_SIZE(cmd.bss.rates), ARRAY_SIZE (bss->rates));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400989 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
Dan Williams5fd164e2009-05-22 20:01:21 -04001005 cmd.bss.ibss.atimwindow = bss->atimwindow;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001006
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
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001026 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
Dan Williams822ac03a2009-05-22 20:07:14 -04001027 if (ret == 0) {
1028 ret = lbs_adhoc_post(priv,
1029 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
1030 }
Holger Schurig697900a2008-04-02 16:27:10 +02001031
1032out:
Dan Williamsd5db2df2008-08-21 17:51:07 -04001033 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +02001034 return ret;
1035}
1036
1037/**
1038 * @brief Start an Adhoc Network
1039 *
1040 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -04001041 * @param assoc_req The association request describing the BSS to start
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001042 *
1043 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +02001044 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001045static int lbs_adhoc_start(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +02001046 struct assoc_request *assoc_req)
1047{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001048 struct cmd_ds_802_11_ad_hoc_start cmd;
Holger Schurig0e78ff82009-12-02 15:26:03 +01001049 u8 preamble = RADIO_PREAMBLE_SHORT;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001050 size_t ratesize = 0;
1051 u16 tmpcap = 0;
1052 int ret = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001053 DECLARE_SSID_BUF(ssid);
Dan Williamsd5db2df2008-08-21 17:51:07 -04001054
1055 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +02001056
Dan Williamsd5db2df2008-08-21 17:51:07 -04001057 ret = lbs_set_radio(priv, preamble, 1);
1058 if (ret)
1059 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +02001060
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001061 /* Build the start command */
1062 memset(&cmd, 0, sizeof(cmd));
1063 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurig697900a2008-04-02 16:27:10 +02001064
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001065 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
1066
1067 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001068 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001069 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 */
Dan Williams75b6a612009-05-22 20:03:09 -04001080 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
1081 cmd.ds.header.len = 1;
Dan Williams5fd164e2009-05-22 20:01:21 -04001082 cmd.ds.channel = assoc_req->channel;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001083
1084 /* set IBSS parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -04001085 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1086 cmd.ibss.header.len = 2;
Dan Williams5fd164e2009-05-22 20:01:21 -04001087 cmd.ibss.atimwindow = cpu_to_le16(0);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001088
1089 /* set capability info */
1090 tmpcap = WLAN_CAPABILITY_IBSS;
Dan Williams2fa7a982009-05-22 20:09:58 -04001091 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");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001095 tmpcap |= WLAN_CAPABILITY_PRIVACY;
1096 } else
Dan Williams2fa7a982009-05-22 20:09:58 -04001097 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001098
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
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001120 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)
Dan Williams822ac03a2009-05-22 20:07:14 -04001128 ret = lbs_adhoc_post(priv,
1129 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001130
Dan Williamsd5db2df2008-08-21 17:51:07 -04001131out:
1132 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +02001133 return ret;
1134}
1135
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001136/**
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)
Holger Schurig697900a2008-04-02 16:27:10 +02001143{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001144 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;
Holger Schurig697900a2008-04-02 16:27:10 +02001159}
Dan Williamse76850d2007-05-25 17:09:41 -04001160
Holger Schurig245bf202008-04-02 16:27:42 +02001161static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
1162 struct bss_descriptor *match_bss)
1163{
John W. Linville64147c72010-01-19 17:07:41 -05001164 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))
Holger Schurig245bf202008-04-02 16:27:42 +02001169 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{
John W. Linville64147c72010-01-19 17:07:41 -05001177 if (secinfo->wep_enabled &&
1178 !secinfo->WPAenabled && !secinfo->WPA2enabled &&
1179 (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
Holger Schurig245bf202008-04-02 16:27:42 +02001180 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{
John W. Linville64147c72010-01-19 17:07:41 -05001188 if (!secinfo->wep_enabled && secinfo->WPAenabled &&
1189 (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
Holger Schurig245bf202008-04-02 16:27:42 +02001190 /* 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 &&
Johannes Berg2c7060022008-10-30 22:09:54 +01001202 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +02001203 /* 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{
John W. Linville64147c72010-01-19 17:07:41 -05001214 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))
Holger Schurig245bf202008-04-02 16:27:42 +02001219 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
Holger Schurig69f90322007-11-23 15:43:44 +01001427static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001428 struct assoc_request * assoc_req)
1429{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001430 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001431 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001432 int channel = -1;
John W. Linville9387b7c2008-09-30 20:59:05 -04001433 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001434
Holger Schurig9012b282007-05-25 11:27:16 -04001435 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001436
Dan Williamsef9a2642007-05-25 16:46:33 -04001437 /* FIXME: take channel into account when picking SSIDs if a channel
1438 * is set.
1439 */
1440
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001441 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1442 channel = assoc_req->channel;
1443
Holger Schurig0765af42007-10-15 12:55:56 +02001444 lbs_deb_assoc("SSID '%s' requested\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001445 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -04001446 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -05001447 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001448 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001449
David Woodhouseaa21c002007-12-08 20:04:36 +00001450 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001451 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001452 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -04001453 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams822ac03a2009-05-22 20:07:14 -04001454 ret = lbs_try_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001455 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -04001456 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001457 }
Dan Williams0dc5a292007-05-10 22:58:02 -04001458 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001459 /* Scan for the network, do not save previous results. Stale
1460 * scan data will cause us to join a non-existant adhoc network
1461 */
Holger Schurig10078322007-11-15 18:05:47 -05001462 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001463 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001464
1465 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001466 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001467 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001468 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -04001469 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001470 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001471 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001472 } else {
1473 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -04001474 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001475 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Holger Schurig243e84e2009-10-22 15:30:47 +02001476 IEEE80211_MAX_SSID_LEN);
Dan Williamsd8efea22007-05-28 23:54:55 -04001477 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001478 lbs_adhoc_start(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001479 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001480 }
1481
Holger Schurig9012b282007-05-25 11:27:16 -04001482 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001483 return ret;
1484}
1485
1486
Holger Schurig69f90322007-11-23 15:43:44 +01001487static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001488 struct assoc_request * assoc_req)
1489{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001490 int ret = 0;
1491 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001492
Johannes Berge1749612008-10-27 15:59:26 -07001493 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001494
1495 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +00001496 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001497 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001498 if (bss == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -07001499 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1500 "cannot associate.\n", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001501 goto out;
1502 }
1503
Dan Williamse76850d2007-05-25 17:09:41 -04001504 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -04001505 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williams822ac03a2009-05-22 20:07:14 -04001506 ret = lbs_try_associate(priv, assoc_req);
1507 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1508 ret);
Dan Williams0dc5a292007-05-10 22:58:02 -04001509 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001510 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001511 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001512
1513out:
Holger Schurig9012b282007-05-25 11:27:16 -04001514 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001515 return ret;
1516}
1517
1518
Holger Schurig69f90322007-11-23 15:43:44 +01001519static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001520 struct assoc_request * assoc_req)
1521{
1522 int ret = 0, done = 0;
1523
Holger Schurig0765af42007-10-15 12:55:56 +02001524 lbs_deb_enter(LBS_DEB_ASSOC);
1525
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001526 /* If we're given and 'any' BSSID, try associating based on SSID */
1527
1528 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
John W. Linville64147c72010-01-19 17:07:41 -05001529 if (compare_ether_addr(bssid_any, assoc_req->bssid) &&
1530 compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001531 ret = assoc_helper_bssid(priv, assoc_req);
1532 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001533 }
1534 }
1535
1536 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1537 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001538 }
1539
Holger Schurig0765af42007-10-15 12:55:56 +02001540 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001541 return ret;
1542}
1543
1544
Holger Schurig69f90322007-11-23 15:43:44 +01001545static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001546 struct assoc_request * assoc_req)
1547{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001548 int ret = 0;
1549
Holger Schurig9012b282007-05-25 11:27:16 -04001550 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001551
David Woodhouseaa21c002007-12-08 20:04:36 +00001552 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -04001553 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001554
Dan Williams0dc5a292007-05-10 22:58:02 -04001555 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001556 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -05001557 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +00001558 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001559 }
1560
David Woodhouseaa21c002007-12-08 20:04:36 +00001561 priv->mode = assoc_req->mode;
Holger Schurigfef06402009-10-22 15:30:59 +02001562 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE,
1563 assoc_req->mode == IW_MODE_ADHOC ? 2 : 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001564
Holger Schurig9012b282007-05-25 11:27:16 -04001565done:
1566 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001567 return ret;
1568}
1569
Holger Schurig69f90322007-11-23 15:43:44 +01001570static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -04001571 struct assoc_request * assoc_req)
1572{
Dan Williamsef9a2642007-05-25 16:46:33 -04001573 int ret = 0;
1574
1575 lbs_deb_enter(LBS_DEB_ASSOC);
1576
David Woodhouse9f462572007-12-12 22:50:21 -05001577 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001578 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001579 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001580 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -04001581 }
1582
Holger Schurigc14951f2009-10-22 15:30:50 +02001583 if (assoc_req->channel == priv->channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001584 goto done;
1585
David Woodhouse8642f1f2007-12-11 20:03:01 -05001586 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -05001587 /* 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 */
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001590 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1591 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001592 }
1593
Dan Williamsef9a2642007-05-25 16:46:33 -04001594 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
Holger Schurigc14951f2009-10-22 15:30:50 +02001595 priv->channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -04001596
Dan Williams2dd4b262007-12-11 16:54:15 -05001597 ret = lbs_set_channel(priv, assoc_req->channel);
1598 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -05001599 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -04001600
Dan Williams2dd4b262007-12-11 16:54:15 -05001601 /* 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... */
David Woodhouse9f462572007-12-12 22:50:21 -05001604 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001605 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001606 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001607 goto done;
1608 }
Dan Williamsef9a2642007-05-25 16:46:33 -04001609
Holger Schurigc14951f2009-10-22 15:30:50 +02001610 if (assoc_req->channel != priv->channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -05001611 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -04001612 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001613 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -04001614 }
1615
John W. Linville64147c72010-01-19 17:07:41 -05001616 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)) {
Dan Williamsef9a2642007-05-25 16:46:33 -04001619 /* 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 */
David Woodhouse23d36ee2007-12-12 00:14:21 -05001624 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -04001625
David Woodhouse8642f1f2007-12-11 20:03:01 -05001626 restore_mesh:
1627 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001628 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
Holger Schurigc14951f2009-10-22 15:30:50 +02001629 priv->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001630
1631 done:
Dan Williamsef9a2642007-05-25 16:46:33 -04001632 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1633 return ret;
1634}
1635
1636
Holger Schurig69f90322007-11-23 15:43:44 +01001637static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -05001638 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001639{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001640 int i;
1641 int ret = 0;
1642
Holger Schurig9012b282007-05-25 11:27:16 -04001643 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001644
1645 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -05001646 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);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001651
1652 if (ret)
1653 goto out;
1654
1655 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -04001656 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +01001657 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001658 else
Holger Schurigd9e97782008-03-12 16:06:43 +01001659 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -05001660
Holger Schurigc97329e2008-03-18 11:20:21 +01001661 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001662
David Woodhouseaa21c002007-12-08 20:04:36 +00001663 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001664
David Woodhouseaa21c002007-12-08 20:04:36 +00001665 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001666 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001667 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -05001668 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001669 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001670 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001671
David Woodhouseaa21c002007-12-08 20:04:36 +00001672 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001673
1674out:
Holger Schurig9012b282007-05-25 11:27:16 -04001675 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001676 return ret;
1677}
1678
Holger Schurig69f90322007-11-23 15:43:44 +01001679static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001680 struct assoc_request * assoc_req)
1681{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001682 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -05001683 uint16_t do_wpa;
1684 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001685
Holger Schurig9012b282007-05-25 11:27:16 -04001686 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001687
David Woodhouseaa21c002007-12-08 20:04:36 +00001688 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001689 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001690
Holger Schurigc97329e2008-03-18 11:20:21 +01001691 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001692
Dan Williams18c96c342007-06-18 12:01:12 -04001693 /* 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 */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001699 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -04001700 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001701 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -04001702 goto out;
1703 }
1704
1705 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001706 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -04001707 if (do_wpa == rsn)
1708 goto out;
1709
1710 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001711 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -04001712
1713out:
Holger Schurig9012b282007-05-25 11:27:16 -04001714 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001715 return ret;
1716}
1717
1718
Holger Schurig69f90322007-11-23 15:43:44 +01001719static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001720 struct assoc_request * assoc_req)
1721{
1722 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -04001723 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001724
Holger Schurig9012b282007-05-25 11:27:16 -04001725 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001726
Dan Williams2bcde512007-10-03 10:37:45 -04001727 /* 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 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001731
Dan Williams2bcde512007-10-03 10:37:45 -04001732 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1733 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +01001734 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001735 assoc_req->flags = flags;
1736 }
1737
1738 if (ret)
1739 goto out;
1740
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001741 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1742 sizeof(struct enc_key));
1743
Dan Williams2bcde512007-10-03 10:37:45 -04001744 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1745 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1746
David Woodhouse9e1228d2008-03-03 12:15:39 +01001747 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001748 assoc_req->flags = flags;
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001749
1750 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1751 sizeof(struct enc_key));
Dan Williams2bcde512007-10-03 10:37:45 -04001752 }
1753
1754out:
Holger Schurig9012b282007-05-25 11:27:16 -04001755 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001756 return ret;
1757}
1758
1759
Holger Schurig69f90322007-11-23 15:43:44 +01001760static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001761 struct assoc_request * assoc_req)
1762{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001763 int ret = 0;
1764
Holger Schurig9012b282007-05-25 11:27:16 -04001765 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001766
1767 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001768 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1769 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001770 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +00001771 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1772 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001773 }
1774
Holger Schurig9012b282007-05-25 11:27:16 -04001775 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001776 return ret;
1777}
1778
1779
David Woodhouseaa21c002007-12-08 20:04:36 +00001780static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001781 struct assoc_request * assoc_req)
1782{
Holger Schurig0765af42007-10-15 12:55:56 +02001783 int ret = 0;
1784
David Woodhouseaa21c002007-12-08 20:04:36 +00001785 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001786 return 0;
1787
Holger Schurig52507c22008-01-28 17:25:53 +01001788 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001789 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001790 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1791 ret = 1;
1792 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001793 }
1794
1795 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001796 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +02001797 lbs_deb_assoc("Deauthenticating due to new security\n");
1798 ret = 1;
1799 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001800 }
1801 }
1802
1803 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001804 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1805 ret = 1;
1806 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001807 }
1808
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001809 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001810 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1811 ret = 1;
1812 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001813 }
1814
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001815 /* FIXME: deal with 'auto' mode somehow */
1816 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001817 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 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001823 }
1824
Holger Schurig0765af42007-10-15 12:55:56 +02001825out:
1826 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +01001827 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001828}
1829
1830
David Woodhouseaa21c002007-12-08 20:04:36 +00001831static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001832 struct assoc_request * assoc_req)
1833{
Holger Schurig0765af42007-10-15 12:55:56 +02001834 lbs_deb_enter(LBS_DEB_ASSOC);
1835
David Woodhouseaa21c002007-12-08 20:04:36 +00001836 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001837 return 0;
1838
David Woodhouseaa21c002007-12-08 20:04:36 +00001839 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1840 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001841 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001842 return 1;
1843
1844 /* FIXME: deal with 'auto' mode somehow */
1845 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001846 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001847 return 1;
1848 }
1849
Dan Williamsef9a2642007-05-25 16:46:33 -04001850 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurigc14951f2009-10-22 15:30:50 +02001851 if (assoc_req->channel != priv->channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001852 return 1;
1853 }
1854
Holger Schurig0765af42007-10-15 12:55:56 +02001855 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001856 return 0;
1857}
1858
1859
Holger Schurig245bf202008-04-02 16:27:42 +02001860/**
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)) {
Holger Schurig243e84e2009-10-22 15:30:47 +02001933 memcpy(out_ssid, &found->ssid, IEEE80211_MAX_SSID_LEN);
Holger Schurig245bf202008-04-02 16:27:42 +02001934 *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
Holger Schurig10078322007-11-15 18:05:47 -05001945void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001946{
Holger Schurig69f90322007-11-23 15:43:44 +01001947 struct lbs_private *priv = container_of(work, struct lbs_private,
1948 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001949 struct assoc_request * assoc_req = NULL;
1950 int ret = 0;
1951 int find_any_ssid = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001952 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001953
Holger Schurig9012b282007-05-25 11:27:16 -04001954 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001955
David Woodhouseaa21c002007-12-08 20:04:36 +00001956 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);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001961
Holger Schurig9012b282007-05-25 11:27:16 -04001962 if (!assoc_req)
1963 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001964
Holger Schurig0765af42007-10-15 12:55:56 +02001965 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"
Johannes Berge1749612008-10-27 15:59:26 -07001972 " BSSID: %pM\n"
Holger Schurig0765af42007-10-15 12:55:56 +02001973 " secinfo: %s%s%s\n"
1974 " auth_mode: %d\n",
1975 assoc_req->flags,
John W. Linville9387b7c2008-09-30 20:59:05 -04001976 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Holger Schurig0765af42007-10-15 12:55:56 +02001977 assoc_req->channel, assoc_req->band, assoc_req->mode,
Johannes Berge1749612008-10-27 15:59:26 -07001978 assoc_req->bssid,
Holger Schurig0765af42007-10-15 12:55:56 +02001979 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1980 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1981 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1982 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001983
1984 /* If 'any' SSID was specified, find an SSID to associate with */
John W. Linville64147c72010-01-19 17:07:41 -05001985 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags) &&
1986 !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001987 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)) {
John W. Linville64147c72010-01-19 17:07:41 -05001991 if (compare_ether_addr(assoc_req->bssid, bssid_any) &&
1992 compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001993 find_any_ssid = 0;
1994 }
1995
1996 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02001997 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001998
Holger Schurig10078322007-11-15 18:05:47 -05001999 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04002000 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002001 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04002002 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002003 ret = -ENETUNREACH;
2004 goto out;
2005 }
2006
2007 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04002008 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002009 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 */
David Woodhouseaa21c002007-12-08 20:04:36 +00002018 if (priv->mode == IW_MODE_INFRA) {
2019 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04002020 ret = lbs_cmd_80211_deauthenticate(priv,
2021 priv->curbssparams.bssid,
2022 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002023 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04002024 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002025 "configuration request failed: %d\n",
2026 ret);
2027 }
2028 }
David Woodhouseaa21c002007-12-08 20:04:36 +00002029 } else if (priv->mode == IW_MODE_ADHOC) {
2030 if (should_stop_adhoc(priv, assoc_req)) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04002031 ret = lbs_adhoc_stop(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002032 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04002033 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002034 "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);
Holger Schurig0765af42007-10-15 12:55:56 +02002044 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002045 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002046 }
2047
Dan Williamsef9a2642007-05-25 16:46:33 -04002048 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
2049 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02002050 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04002051 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04002052 }
2053
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002054 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
2055 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02002056 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002057 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002058 }
2059
2060 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
2061 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02002062 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002063 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002064 }
2065
John W. Linvilled3d56212010-01-27 14:23:17 -05002066 /*
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 */
John W. Linville64147c72010-01-19 17:07:41 -05002071 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags) ||
2072 test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002073 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02002074 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002075 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002076 }
2077
John W. Linville64147c72010-01-19 17:07:41 -05002078 if (test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags) ||
2079 test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
Samuel Ortiz1625c142010-01-19 00:19:21 +01002080 ret = assoc_helper_wep_keys(priv, assoc_req);
2081 if (ret)
2082 goto out;
2083 }
2084
2085
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002086 /* SSID/BSSID should be the _last_ config option set, because they
2087 * trigger the association attempt.
2088 */
John W. Linville64147c72010-01-19 17:07:41 -05002089 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags) ||
2090 test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002091 int success = 1;
2092
2093 ret = assoc_helper_associate(priv, assoc_req);
2094 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01002095 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002096 ret);
2097 success = 0;
2098 }
2099
David Woodhouseaa21c002007-12-08 20:04:36 +00002100 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01002101 lbs_deb_assoc("ASSOC: association unsuccessful, "
2102 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002103 success = 0;
2104 }
2105
2106 if (success) {
Johannes Berge1749612008-10-27 15:59:26 -07002107 lbs_deb_assoc("associated to %pM\n",
2108 priv->curbssparams.bssid);
Holger Schurig10078322007-11-15 18:05:47 -05002109 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04002110 CMD_802_11_RSSI,
2111 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002112 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002113 ret = -1;
2114 }
2115 }
2116
2117out:
2118 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04002119 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002120 ret);
2121 }
Dan Williamse76850d2007-05-25 17:09:41 -04002122
David Woodhouseaa21c002007-12-08 20:04:36 +00002123 mutex_lock(&priv->lock);
2124 priv->in_progress_assoc_req = NULL;
2125 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002126 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04002127
2128done:
2129 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002130}
2131
2132
2133/*
2134 * Caller MUST hold any necessary locks
2135 */
David Woodhouseaa21c002007-12-08 20:04:36 +00002136struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002137{
2138 struct assoc_request * assoc_req;
2139
Holger Schurig0765af42007-10-15 12:55:56 +02002140 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00002141 if (!priv->pending_assoc_req) {
2142 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04002143 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00002144 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002145 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 */
David Woodhouseaa21c002007-12-08 20:04:36 +00002154 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002155 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002156 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Holger Schurig243e84e2009-10-22 15:30:47 +02002157 IEEE80211_MAX_SSID_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00002158 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002159 }
2160
2161 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
Holger Schurigc14951f2009-10-22 15:30:50 +02002162 assoc_req->channel = priv->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002163
Dan Williamse76850d2007-05-25 17:09:41 -04002164 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00002165 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04002166
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002167 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00002168 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002169
2170 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002171 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002172 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++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002178 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04002179 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002180 }
2181 }
2182
2183 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00002184 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002185
2186 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002187 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04002188 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002189 }
2190
2191 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002192 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04002193 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002194 }
2195
2196 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002197 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05002198 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002199 }
2200
2201 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002202 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002203 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00002204 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002205 }
2206
Holger Schurig0765af42007-10-15 12:55:56 +02002207 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002208 return assoc_req;
2209}
Holger Schurig697900a2008-04-02 16:27:10 +02002210
2211
2212/**
Dan Williams191bb402008-08-21 17:46:18 -04002213 * @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)
Holger Schurig697900a2008-04-02 16:27:10 +02002223{
Dan Williams191bb402008-08-21 17:46:18 -04002224 struct cmd_ds_802_11_deauthenticate cmd;
2225 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02002226
2227 lbs_deb_enter(LBS_DEB_JOIN);
2228
Dan Williams191bb402008-08-21 17:46:18 -04002229 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);
Holger Schurig697900a2008-04-02 16:27:10 +02002233
Dan Williams191bb402008-08-21 17:46:18 -04002234 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02002235
Dan Williams191bb402008-08-21 17:46:18 -04002236 /* 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);
Holger Schurig697900a2008-04-02 16:27:10 +02002240
2241 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04002242 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02002243}
2244