blob: 5f58ae33405b1767db2b67e60e27c83f7a7bf043 [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>
John W. Linville7e272fc2008-09-24 18:13:14 -04007#include <net/lib80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02008
9#include "assoc.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020010#include "decl.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020011#include "host.h"
Holger Schurig245bf202008-04-02 16:27:42 +020012#include "scan.h"
Dan Williams2dd4b262007-12-11 16:54:15 -050013#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014
Ihar Hrachyshka5a6e0432008-01-25 14:15:00 +010015static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
16 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
17static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
18 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020019
Dan Williamsbe0d76e2009-05-22 20:05:25 -040020/* The firmware needs the following bits masked out of the beacon-derived
21 * capability field when associating/joining to a BSS:
22 * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
Holger Schurig697900a2008-04-02 16:27:10 +020023 */
24#define CAPINFO_MASK (~(0xda00))
25
Holger Schurig2d465022009-10-22 15:30:48 +020026/**
27 * 802.11b/g supported bitrates (in 500Kb/s units)
28 */
29u8 lbs_bg_rates[MAX_RATES] =
30 { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
310x00, 0x00 };
32
Holger Schurig697900a2008-04-02 16:27:10 +020033
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040034/**
35 * @brief This function finds common rates between rates and card rates.
36 *
37 * It will fill common rates in rates as output if found.
38 *
39 * NOTE: Setting the MSB of the basic rates need to be taken
40 * care, either before or after calling this function
41 *
42 * @param priv A pointer to struct lbs_private structure
43 * @param rates the buffer which keeps input and output
Dan Williamsca4fe302009-08-21 09:35:20 -050044 * @param rates_size the size of rates buffer; new size of buffer on return,
45 * which will be less than or equal to original rates_size
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040046 *
47 * @return 0 on success, or -1 on error
48 */
49static int get_common_rates(struct lbs_private *priv,
50 u8 *rates,
51 u16 *rates_size)
52{
Roel Kluin1e3d31c2009-08-02 09:44:12 +020053 int i, j;
Dan Williamsca4fe302009-08-21 09:35:20 -050054 u8 intersection[MAX_RATES];
55 u16 intersection_size;
56 u16 num_rates = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040057
Dan Williamsca4fe302009-08-21 09:35:20 -050058 intersection_size = min_t(u16, *rates_size, ARRAY_SIZE(intersection));
Andrey Yurovsky6f632d52009-08-13 17:34:40 -070059
Dan Williamsca4fe302009-08-21 09:35:20 -050060 /* Allow each rate from 'rates' that is supported by the hardware */
61 for (i = 0; i < ARRAY_SIZE(lbs_bg_rates) && lbs_bg_rates[i]; i++) {
62 for (j = 0; j < intersection_size && rates[j]; j++) {
63 if (rates[j] == lbs_bg_rates[i])
64 intersection[num_rates++] = rates[j];
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040065 }
66 }
67
68 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
Dan Williamsca4fe302009-08-21 09:35:20 -050069 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", lbs_bg_rates,
70 ARRAY_SIZE(lbs_bg_rates));
71 lbs_deb_hex(LBS_DEB_JOIN, "common rates", intersection, num_rates);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040072 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
73
74 if (!priv->enablehwauto) {
Dan Williamsca4fe302009-08-21 09:35:20 -050075 for (i = 0; i < num_rates; i++) {
76 if (intersection[i] == priv->cur_rate)
77 goto done;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040078 }
Dan Williamsca4fe302009-08-21 09:35:20 -050079 lbs_pr_alert("Previously set fixed data rate %#x isn't "
80 "compatible with the network.\n", priv->cur_rate);
81 return -1;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040082 }
Dan Williamsca4fe302009-08-21 09:35:20 -050083
84done:
85 memset(rates, 0, *rates_size);
86 *rates_size = num_rates;
87 memcpy(rates, intersection, num_rates);
Roel Kluin1e3d31c2009-08-02 09:44:12 +020088 return 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040089}
90
91
92/**
93 * @brief Sets the MSB on basic rates as the firmware requires
94 *
95 * Scan through an array and set the MSB for basic data rates.
96 *
97 * @param rates buffer of data rates
98 * @param len size of buffer
99 */
100static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
101{
102 int i;
103
104 for (i = 0; i < len; i++) {
105 if (rates[i] == 0x02 || rates[i] == 0x04 ||
106 rates[i] == 0x0b || rates[i] == 0x16)
107 rates[i] |= 0x80;
108 }
109}
110
Holger Schurig697900a2008-04-02 16:27:10 +0200111
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400112static u8 iw_auth_to_ieee_auth(u8 auth)
113{
114 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
115 return 0x00;
116 else if (auth == IW_AUTH_ALG_SHARED_KEY)
117 return 0x01;
118 else if (auth == IW_AUTH_ALG_LEAP)
119 return 0x80;
120
121 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
122 return 0;
123}
124
125/**
126 * @brief This function prepares the authenticate command. AUTHENTICATE only
127 * sets the authentication suite for future associations, as the firmware
128 * handles authentication internally during the ASSOCIATE command.
129 *
130 * @param priv A pointer to struct lbs_private structure
131 * @param bssid The peer BSSID with which to authenticate
132 * @param auth The authentication mode to use (from wireless.h)
133 *
134 * @return 0 or -1
135 */
136static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
137{
138 struct cmd_ds_802_11_authenticate cmd;
139 int ret = -1;
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400140
141 lbs_deb_enter(LBS_DEB_JOIN);
142
143 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
144 memcpy(cmd.bssid, bssid, ETH_ALEN);
145
146 cmd.authtype = iw_auth_to_ieee_auth(auth);
147
Johannes Berge91d8332009-07-15 17:21:41 +0200148 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400149
150 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
151
152 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
153 return ret;
154}
155
Dan Williams822ac032009-05-22 20:07:14 -0400156
Holger Schurigd0de3742009-10-22 15:30:51 +0200157int lbs_cmd_802_11_set_wep(struct lbs_private *priv, uint16_t cmd_action,
158 struct assoc_request *assoc)
159{
160 struct cmd_ds_802_11_set_wep cmd;
161 int ret = 0;
162
163 lbs_deb_enter(LBS_DEB_CMD);
164
165 memset(&cmd, 0, sizeof(cmd));
166 cmd.hdr.command = cpu_to_le16(CMD_802_11_SET_WEP);
167 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
168
169 cmd.action = cpu_to_le16(cmd_action);
170
171 if (cmd_action == CMD_ACT_ADD) {
172 int i;
173
174 /* default tx key index */
175 cmd.keyindex = cpu_to_le16(assoc->wep_tx_keyidx &
176 CMD_WEP_KEY_INDEX_MASK);
177
178 /* Copy key types and material to host command structure */
179 for (i = 0; i < 4; i++) {
180 struct enc_key *pkey = &assoc->wep_keys[i];
181
182 switch (pkey->len) {
183 case KEY_LEN_WEP_40:
184 cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
185 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
186 lbs_deb_cmd("SET_WEP: add key %d (40 bit)\n", i);
187 break;
188 case KEY_LEN_WEP_104:
189 cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
190 memmove(cmd.keymaterial[i], pkey->key, pkey->len);
191 lbs_deb_cmd("SET_WEP: add key %d (104 bit)\n", i);
192 break;
193 case 0:
194 break;
195 default:
196 lbs_deb_cmd("SET_WEP: invalid key %d, length %d\n",
197 i, pkey->len);
198 ret = -1;
199 goto done;
200 break;
201 }
202 }
203 } else if (cmd_action == CMD_ACT_REMOVE) {
204 /* ACT_REMOVE clears _all_ WEP keys */
205
206 /* default tx key index */
207 cmd.keyindex = cpu_to_le16(priv->wep_tx_keyidx &
208 CMD_WEP_KEY_INDEX_MASK);
209 lbs_deb_cmd("SET_WEP: remove key %d\n", priv->wep_tx_keyidx);
210 }
211
212 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
213done:
214 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
215 return ret;
216}
217
218int lbs_cmd_802_11_enable_rsn(struct lbs_private *priv, uint16_t cmd_action,
219 uint16_t *enable)
220{
221 struct cmd_ds_802_11_enable_rsn cmd;
222 int ret;
223
224 lbs_deb_enter(LBS_DEB_CMD);
225
226 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
227 cmd.action = cpu_to_le16(cmd_action);
228
229 if (cmd_action == CMD_ACT_GET)
230 cmd.enable = 0;
231 else {
232 if (*enable)
233 cmd.enable = cpu_to_le16(CMD_ENABLE_RSN);
234 else
235 cmd.enable = cpu_to_le16(CMD_DISABLE_RSN);
236 lbs_deb_cmd("ENABLE_RSN: %d\n", *enable);
237 }
238
239 ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
240 if (!ret && cmd_action == CMD_ACT_GET)
241 *enable = le16_to_cpu(cmd.enable);
242
243 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
244 return ret;
245}
246
247static void set_one_wpa_key(struct MrvlIEtype_keyParamSet *keyparam,
248 struct enc_key *key)
249{
250 lbs_deb_enter(LBS_DEB_CMD);
251
252 if (key->flags & KEY_INFO_WPA_ENABLED)
253 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_ENABLED);
254 if (key->flags & KEY_INFO_WPA_UNICAST)
255 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_UNICAST);
256 if (key->flags & KEY_INFO_WPA_MCAST)
257 keyparam->keyinfo |= cpu_to_le16(KEY_INFO_WPA_MCAST);
258
259 keyparam->type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
260 keyparam->keytypeid = cpu_to_le16(key->type);
261 keyparam->keylen = cpu_to_le16(key->len);
262 memcpy(keyparam->key, key->key, key->len);
263
264 /* Length field doesn't include the {type,length} header */
265 keyparam->length = cpu_to_le16(sizeof(*keyparam) - 4);
266 lbs_deb_leave(LBS_DEB_CMD);
267}
268
269int lbs_cmd_802_11_key_material(struct lbs_private *priv, uint16_t cmd_action,
270 struct assoc_request *assoc)
271{
272 struct cmd_ds_802_11_key_material cmd;
273 int ret = 0;
274 int index = 0;
275
276 lbs_deb_enter(LBS_DEB_CMD);
277
278 cmd.action = cpu_to_le16(cmd_action);
279 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
280
281 if (cmd_action == CMD_ACT_GET) {
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200282 cmd.hdr.size = cpu_to_le16(sizeof(struct cmd_header) + 2);
Holger Schurigd0de3742009-10-22 15:30:51 +0200283 } else {
284 memset(cmd.keyParamSet, 0, sizeof(cmd.keyParamSet));
285
286 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc->flags)) {
287 set_one_wpa_key(&cmd.keyParamSet[index],
288 &assoc->wpa_unicast_key);
289 index++;
290 }
291
292 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc->flags)) {
293 set_one_wpa_key(&cmd.keyParamSet[index],
294 &assoc->wpa_mcast_key);
295 index++;
296 }
297
298 /* The common header and as many keys as we included */
299 cmd.hdr.size = cpu_to_le16(offsetof(typeof(cmd),
300 keyParamSet[index]));
301 }
302 ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
303 /* Copy the returned key to driver private data */
304 if (!ret && cmd_action == CMD_ACT_GET) {
305 void *buf_ptr = cmd.keyParamSet;
306 void *resp_end = &(&cmd)[1];
307
308 while (buf_ptr < resp_end) {
309 struct MrvlIEtype_keyParamSet *keyparam = buf_ptr;
310 struct enc_key *key;
311 uint16_t param_set_len = le16_to_cpu(keyparam->length);
312 uint16_t key_len = le16_to_cpu(keyparam->keylen);
313 uint16_t key_flags = le16_to_cpu(keyparam->keyinfo);
314 uint16_t key_type = le16_to_cpu(keyparam->keytypeid);
315 void *end;
316
317 end = (void *)keyparam + sizeof(keyparam->type)
318 + sizeof(keyparam->length) + param_set_len;
319
320 /* Make sure we don't access past the end of the IEs */
321 if (end > resp_end)
322 break;
323
324 if (key_flags & KEY_INFO_WPA_UNICAST)
325 key = &priv->wpa_unicast_key;
326 else if (key_flags & KEY_INFO_WPA_MCAST)
327 key = &priv->wpa_mcast_key;
328 else
329 break;
330
331 /* Copy returned key into driver */
332 memset(key, 0, sizeof(struct enc_key));
333 if (key_len > sizeof(key->key))
334 break;
335 key->type = key_type;
336 key->flags = key_flags;
337 key->len = key_len;
338 memcpy(key->key, keyparam->key, key->len);
339
340 buf_ptr = end + 1;
341 }
342 }
343
344 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
345 return ret;
346}
347
348static __le16 lbs_rate_to_fw_bitmap(int rate, int lower_rates_ok)
349{
350/* Bit Rate
351* 15:13 Reserved
352* 12 54 Mbps
353* 11 48 Mbps
354* 10 36 Mbps
355* 9 24 Mbps
356* 8 18 Mbps
357* 7 12 Mbps
358* 6 9 Mbps
359* 5 6 Mbps
360* 4 Reserved
361* 3 11 Mbps
362* 2 5.5 Mbps
363* 1 2 Mbps
364* 0 1 Mbps
365**/
366
367 uint16_t ratemask;
368 int i = lbs_data_rate_to_fw_index(rate);
369 if (lower_rates_ok)
370 ratemask = (0x1fef >> (12 - i));
371 else
372 ratemask = (1 << i);
373 return cpu_to_le16(ratemask);
374}
375
376int lbs_cmd_802_11_rate_adapt_rateset(struct lbs_private *priv,
377 uint16_t cmd_action)
378{
379 struct cmd_ds_802_11_rate_adapt_rateset cmd;
380 int ret;
381
382 lbs_deb_enter(LBS_DEB_CMD);
383
384 if (!priv->cur_rate && !priv->enablehwauto)
385 return -EINVAL;
386
387 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
388
389 cmd.action = cpu_to_le16(cmd_action);
390 cmd.enablehwauto = cpu_to_le16(priv->enablehwauto);
391 cmd.bitmap = lbs_rate_to_fw_bitmap(priv->cur_rate, priv->enablehwauto);
392 ret = lbs_cmd_with_response(priv, CMD_802_11_RATE_ADAPT_RATESET, &cmd);
393 if (!ret && cmd_action == CMD_ACT_GET) {
394 priv->ratebitmap = le16_to_cpu(cmd.bitmap);
395 priv->enablehwauto = le16_to_cpu(cmd.enablehwauto);
396 }
397
398 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
399 return ret;
400}
401
402/**
403 * @brief Set the data rate
404 *
405 * @param priv A pointer to struct lbs_private structure
406 * @param rate The desired data rate, or 0 to clear a locked rate
407 *
408 * @return 0 on success, error on failure
409 */
410int lbs_set_data_rate(struct lbs_private *priv, u8 rate)
411{
412 struct cmd_ds_802_11_data_rate cmd;
413 int ret = 0;
414
415 lbs_deb_enter(LBS_DEB_CMD);
416
417 memset(&cmd, 0, sizeof(cmd));
418 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
419
420 if (rate > 0) {
421 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_FIX_RATE);
422 cmd.rates[0] = lbs_data_rate_to_fw_index(rate);
423 if (cmd.rates[0] == 0) {
424 lbs_deb_cmd("DATA_RATE: invalid requested rate of"
425 " 0x%02X\n", rate);
426 ret = 0;
427 goto out;
428 }
429 lbs_deb_cmd("DATA_RATE: set fixed 0x%02X\n", cmd.rates[0]);
430 } else {
431 cmd.action = cpu_to_le16(CMD_ACT_SET_TX_AUTO);
432 lbs_deb_cmd("DATA_RATE: setting auto\n");
433 }
434
435 ret = lbs_cmd_with_response(priv, CMD_802_11_DATA_RATE, &cmd);
436 if (ret)
437 goto out;
438
439 lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) &cmd, sizeof(cmd));
440
441 /* FIXME: get actual rates FW can do if this command actually returns
442 * all data rates supported.
443 */
444 priv->cur_rate = lbs_fw_index_to_data_rate(cmd.rates[0]);
445 lbs_deb_cmd("DATA_RATE: current rate is 0x%02x\n", priv->cur_rate);
446
447out:
448 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
449 return ret;
450}
451
452
453int lbs_cmd_802_11_rssi(struct lbs_private *priv,
454 struct cmd_ds_command *cmd)
455{
456
457 lbs_deb_enter(LBS_DEB_CMD);
458 cmd->command = cpu_to_le16(CMD_802_11_RSSI);
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200459 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_rssi) +
460 sizeof(struct cmd_header));
Holger Schurigd0de3742009-10-22 15:30:51 +0200461 cmd->params.rssi.N = cpu_to_le16(DEFAULT_BCN_AVG_FACTOR);
462
463 /* reset Beacon SNR/NF/RSSI values */
464 priv->SNR[TYPE_BEACON][TYPE_NOAVG] = 0;
465 priv->SNR[TYPE_BEACON][TYPE_AVG] = 0;
466 priv->NF[TYPE_BEACON][TYPE_NOAVG] = 0;
467 priv->NF[TYPE_BEACON][TYPE_AVG] = 0;
468 priv->RSSI[TYPE_BEACON][TYPE_NOAVG] = 0;
469 priv->RSSI[TYPE_BEACON][TYPE_AVG] = 0;
470
471 lbs_deb_leave(LBS_DEB_CMD);
472 return 0;
473}
474
475int lbs_ret_802_11_rssi(struct lbs_private *priv,
476 struct cmd_ds_command *resp)
477{
478 struct cmd_ds_802_11_rssi_rsp *rssirsp = &resp->params.rssirsp;
479
480 lbs_deb_enter(LBS_DEB_CMD);
481
482 /* store the non average value */
483 priv->SNR[TYPE_BEACON][TYPE_NOAVG] = get_unaligned_le16(&rssirsp->SNR);
484 priv->NF[TYPE_BEACON][TYPE_NOAVG] =
485 get_unaligned_le16(&rssirsp->noisefloor);
486
487 priv->SNR[TYPE_BEACON][TYPE_AVG] = get_unaligned_le16(&rssirsp->avgSNR);
488 priv->NF[TYPE_BEACON][TYPE_AVG] =
489 get_unaligned_le16(&rssirsp->avgnoisefloor);
490
491 priv->RSSI[TYPE_BEACON][TYPE_NOAVG] =
492 CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_NOAVG],
493 priv->NF[TYPE_BEACON][TYPE_NOAVG]);
494
495 priv->RSSI[TYPE_BEACON][TYPE_AVG] =
496 CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_AVG] / AVG_SCALE,
497 priv->NF[TYPE_BEACON][TYPE_AVG] / AVG_SCALE);
498
499 lbs_deb_cmd("RSSI: beacon %d, avg %d\n",
500 priv->RSSI[TYPE_BEACON][TYPE_NOAVG],
501 priv->RSSI[TYPE_BEACON][TYPE_AVG]);
502
503 lbs_deb_leave(LBS_DEB_CMD);
504 return 0;
505}
506
507
508int lbs_cmd_bcn_ctrl(struct lbs_private *priv,
509 struct cmd_ds_command *cmd,
510 u16 cmd_action)
511{
512 struct cmd_ds_802_11_beacon_control
513 *bcn_ctrl = &cmd->params.bcn_ctrl;
514
515 lbs_deb_enter(LBS_DEB_CMD);
516 cmd->size =
517 cpu_to_le16(sizeof(struct cmd_ds_802_11_beacon_control)
Holger Schurig8ec97cc2009-10-22 15:30:55 +0200518 + sizeof(struct cmd_header));
Holger Schurigd0de3742009-10-22 15:30:51 +0200519 cmd->command = cpu_to_le16(CMD_802_11_BEACON_CTRL);
520
521 bcn_ctrl->action = cpu_to_le16(cmd_action);
522 bcn_ctrl->beacon_enable = cpu_to_le16(priv->beacon_enable);
523 bcn_ctrl->beacon_period = cpu_to_le16(priv->beacon_period);
524
525 lbs_deb_leave(LBS_DEB_CMD);
526 return 0;
527}
528
529int lbs_ret_802_11_bcn_ctrl(struct lbs_private *priv,
530 struct cmd_ds_command *resp)
531{
532 struct cmd_ds_802_11_beacon_control *bcn_ctrl =
533 &resp->params.bcn_ctrl;
534
535 lbs_deb_enter(LBS_DEB_CMD);
536
537 if (bcn_ctrl->action == CMD_ACT_GET) {
538 priv->beacon_enable = (u8) le16_to_cpu(bcn_ctrl->beacon_enable);
539 priv->beacon_period = le16_to_cpu(bcn_ctrl->beacon_period);
540 }
541
542 lbs_deb_enter(LBS_DEB_CMD);
543 return 0;
544}
545
546
547
Dan Williams822ac032009-05-22 20:07:14 -0400548static int lbs_assoc_post(struct lbs_private *priv,
549 struct cmd_ds_802_11_associate_response *resp)
550{
551 int ret = 0;
552 union iwreq_data wrqu;
553 struct bss_descriptor *bss;
554 u16 status_code;
555
556 lbs_deb_enter(LBS_DEB_ASSOC);
557
558 if (!priv->in_progress_assoc_req) {
559 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
560 ret = -1;
561 goto done;
562 }
563 bss = &priv->in_progress_assoc_req->bss;
564
565 /*
566 * Older FW versions map the IEEE 802.11 Status Code in the association
567 * response to the following values returned in resp->statuscode:
568 *
569 * IEEE Status Code Marvell Status Code
570 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
571 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
572 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
573 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
574 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
575 * others -> 0x0003 ASSOC_RESULT_REFUSED
576 *
577 * Other response codes:
578 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
579 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
580 * association response from the AP)
581 */
582
583 status_code = le16_to_cpu(resp->statuscode);
584 if (priv->fwrelease < 0x09000000) {
585 switch (status_code) {
586 case 0x00:
587 break;
588 case 0x01:
589 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
590 break;
591 case 0x02:
592 lbs_deb_assoc("ASSOC_RESP: internal timer "
593 "expired while waiting for the AP\n");
594 break;
595 case 0x03:
596 lbs_deb_assoc("ASSOC_RESP: association "
597 "refused by AP\n");
598 break;
599 case 0x04:
600 lbs_deb_assoc("ASSOC_RESP: authentication "
601 "refused by AP\n");
602 break;
603 default:
604 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
605 " unknown\n", status_code);
606 break;
607 }
608 } else {
609 /* v9+ returns the AP's association response */
610 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
611 }
612
613 if (status_code) {
614 lbs_mac_event_disconnected(priv);
615 ret = -1;
616 goto done;
617 }
618
619 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
620 (void *) (resp + sizeof (resp->hdr)),
621 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
622
623 /* Send a Media Connected event, according to the Spec */
624 priv->connect_status = LBS_CONNECTED;
625
626 /* Update current SSID and BSSID */
Holger Schurig243e84e2009-10-22 15:30:47 +0200627 memcpy(&priv->curbssparams.ssid, &bss->ssid, IEEE80211_MAX_SSID_LEN);
Dan Williams822ac032009-05-22 20:07:14 -0400628 priv->curbssparams.ssid_len = bss->ssid_len;
629 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
630
631 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
632 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
633
634 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
635 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
636 priv->nextSNRNF = 0;
637 priv->numSNRNF = 0;
638
639 netif_carrier_on(priv->dev);
640 if (!priv->tx_pending_len)
641 netif_wake_queue(priv->dev);
642
643 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
644 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
645 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
646
647done:
648 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
649 return ret;
650}
651
652/**
653 * @brief This function prepares an association-class command.
654 *
655 * @param priv A pointer to struct lbs_private structure
656 * @param assoc_req The association request describing the BSS to associate
657 * or reassociate with
658 * @param command The actual command, either CMD_802_11_ASSOCIATE or
659 * CMD_802_11_REASSOCIATE
660 *
661 * @return 0 or -1
662 */
663static int lbs_associate(struct lbs_private *priv,
664 struct assoc_request *assoc_req,
665 u16 command)
666{
667 struct cmd_ds_802_11_associate cmd;
668 int ret = 0;
669 struct bss_descriptor *bss = &assoc_req->bss;
670 u8 *pos = &(cmd.iebuf[0]);
671 u16 tmpcap, tmplen, tmpauth;
672 struct mrvl_ie_ssid_param_set *ssid;
673 struct mrvl_ie_ds_param_set *ds;
674 struct mrvl_ie_cf_param_set *cf;
675 struct mrvl_ie_rates_param_set *rates;
676 struct mrvl_ie_rsn_param_set *rsn;
677 struct mrvl_ie_auth_type *auth;
678
679 lbs_deb_enter(LBS_DEB_ASSOC);
680
681 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
682 (command != CMD_802_11_REASSOCIATE));
683
684 memset(&cmd, 0, sizeof(cmd));
685 cmd.hdr.command = cpu_to_le16(command);
686
687 /* Fill in static fields */
688 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
689 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
690
691 /* Capability info */
692 tmpcap = (bss->capability & CAPINFO_MASK);
693 if (bss->mode == IW_MODE_INFRA)
694 tmpcap |= WLAN_CAPABILITY_ESS;
695 cmd.capability = cpu_to_le16(tmpcap);
696 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
697
698 /* SSID */
699 ssid = (struct mrvl_ie_ssid_param_set *) pos;
700 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
701 tmplen = bss->ssid_len;
702 ssid->header.len = cpu_to_le16(tmplen);
703 memcpy(ssid->ssid, bss->ssid, tmplen);
704 pos += sizeof(ssid->header) + tmplen;
705
706 ds = (struct mrvl_ie_ds_param_set *) pos;
707 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
708 ds->header.len = cpu_to_le16(1);
709 ds->channel = bss->phy.ds.channel;
710 pos += sizeof(ds->header) + 1;
711
712 cf = (struct mrvl_ie_cf_param_set *) pos;
713 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
714 tmplen = sizeof(*cf) - sizeof (cf->header);
715 cf->header.len = cpu_to_le16(tmplen);
716 /* IE payload should be zeroed, firmware fills it in for us */
717 pos += sizeof(*cf);
718
719 rates = (struct mrvl_ie_rates_param_set *) pos;
720 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
Dan Williamsca4fe302009-08-21 09:35:20 -0500721 tmplen = min_t(u16, ARRAY_SIZE(bss->rates), MAX_RATES);
Roel Kluin1e3d31c2009-08-02 09:44:12 +0200722 memcpy(&rates->rates, &bss->rates, tmplen);
Dan Williams822ac032009-05-22 20:07:14 -0400723 if (get_common_rates(priv, rates->rates, &tmplen)) {
724 ret = -1;
725 goto done;
726 }
727 pos += sizeof(rates->header) + tmplen;
728 rates->header.len = cpu_to_le16(tmplen);
729 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
730
731 /* Copy the infra. association rates into Current BSS state structure */
732 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
733 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
734
735 /* Set MSB on basic rates as the firmware requires, but _after_
736 * copying to current bss rates.
737 */
738 lbs_set_basic_rate_flags(rates->rates, tmplen);
739
740 /* Firmware v9+ indicate authentication suites as a TLV */
741 if (priv->fwrelease >= 0x09000000) {
Dan Williams822ac032009-05-22 20:07:14 -0400742 auth = (struct mrvl_ie_auth_type *) pos;
743 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
744 auth->header.len = cpu_to_le16(2);
745 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
746 auth->auth = cpu_to_le16(tmpauth);
747 pos += sizeof(auth->header) + 2;
748
Johannes Berge91d8332009-07-15 17:21:41 +0200749 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
750 bss->bssid, priv->secinfo.auth_mode);
Dan Williams822ac032009-05-22 20:07:14 -0400751 }
752
753 /* WPA/WPA2 IEs */
754 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
755 rsn = (struct mrvl_ie_rsn_param_set *) pos;
756 /* WPA_IE or WPA2_IE */
757 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
758 tmplen = (u16) assoc_req->wpa_ie[1];
759 rsn->header.len = cpu_to_le16(tmplen);
760 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
761 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
762 sizeof(rsn->header) + tmplen);
763 pos += sizeof(rsn->header) + tmplen;
764 }
765
766 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
767 (u16)(pos - (u8 *) &cmd.iebuf));
768
769 /* update curbssparams */
Holger Schurigc14951f2009-10-22 15:30:50 +0200770 priv->channel = bss->phy.ds.channel;
Dan Williams822ac032009-05-22 20:07:14 -0400771
Dan Williams822ac032009-05-22 20:07:14 -0400772 ret = lbs_cmd_with_response(priv, command, &cmd);
773 if (ret == 0) {
774 ret = lbs_assoc_post(priv,
775 (struct cmd_ds_802_11_associate_response *) &cmd);
776 }
777
778done:
779 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
780 return ret;
781}
782
Holger Schurig697900a2008-04-02 16:27:10 +0200783/**
784 * @brief Associate to a specific BSS discovered in a scan
785 *
786 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400787 * @param assoc_req The association request describing the BSS to associate with
Holger Schurig697900a2008-04-02 16:27:10 +0200788 *
789 * @return 0-success, otherwise fail
790 */
Dan Williams822ac032009-05-22 20:07:14 -0400791static int lbs_try_associate(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200792 struct assoc_request *assoc_req)
793{
794 int ret;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400795 u8 preamble = RADIO_PREAMBLE_LONG;
Holger Schurig697900a2008-04-02 16:27:10 +0200796
797 lbs_deb_enter(LBS_DEB_ASSOC);
798
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400799 /* FW v9 and higher indicate authentication suites as a TLV in the
800 * association command, not as a separate authentication command.
801 */
802 if (priv->fwrelease < 0x09000000) {
803 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
804 priv->secinfo.auth_mode);
805 if (ret)
806 goto out;
807 }
Holger Schurig697900a2008-04-02 16:27:10 +0200808
Dan Williamsd5db2df2008-08-21 17:51:07 -0400809 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig0e78ff82009-12-02 15:26:03 +0100810 if (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Dan Williamsd5db2df2008-08-21 17:51:07 -0400811 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200812
Dan Williamsd5db2df2008-08-21 17:51:07 -0400813 ret = lbs_set_radio(priv, preamble, 1);
814 if (ret)
815 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200816
Dan Williams822ac032009-05-22 20:07:14 -0400817 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
Holger Schurig697900a2008-04-02 16:27:10 +0200818
Dan Williamsd5db2df2008-08-21 17:51:07 -0400819out:
Holger Schurig697900a2008-04-02 16:27:10 +0200820 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
821 return ret;
822}
823
Dan Williams822ac032009-05-22 20:07:14 -0400824static int lbs_adhoc_post(struct lbs_private *priv,
825 struct cmd_ds_802_11_ad_hoc_result *resp)
826{
827 int ret = 0;
828 u16 command = le16_to_cpu(resp->hdr.command);
829 u16 result = le16_to_cpu(resp->hdr.result);
830 union iwreq_data wrqu;
831 struct bss_descriptor *bss;
832 DECLARE_SSID_BUF(ssid);
833
834 lbs_deb_enter(LBS_DEB_JOIN);
835
836 if (!priv->in_progress_assoc_req) {
837 lbs_deb_join("ADHOC_RESP: no in-progress association "
838 "request\n");
839 ret = -1;
840 goto done;
841 }
842 bss = &priv->in_progress_assoc_req->bss;
843
844 /*
845 * Join result code 0 --> SUCCESS
846 */
847 if (result) {
848 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
849 if (priv->connect_status == LBS_CONNECTED)
850 lbs_mac_event_disconnected(priv);
851 ret = -1;
852 goto done;
853 }
854
855 /* Send a Media Connected event, according to the Spec */
856 priv->connect_status = LBS_CONNECTED;
857
858 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
859 /* Update the created network descriptor with the new BSSID */
860 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
861 }
862
863 /* Set the BSSID from the joined/started descriptor */
864 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
865
866 /* Set the new SSID to current SSID */
Holger Schurig243e84e2009-10-22 15:30:47 +0200867 memcpy(&priv->curbssparams.ssid, &bss->ssid, IEEE80211_MAX_SSID_LEN);
Dan Williams822ac032009-05-22 20:07:14 -0400868 priv->curbssparams.ssid_len = bss->ssid_len;
869
870 netif_carrier_on(priv->dev);
871 if (!priv->tx_pending_len)
872 netif_wake_queue(priv->dev);
873
874 memset(&wrqu, 0, sizeof(wrqu));
875 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
876 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
877 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
878
879 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
880 print_ssid(ssid, bss->ssid, bss->ssid_len),
881 priv->curbssparams.bssid,
Holger Schurigc14951f2009-10-22 15:30:50 +0200882 priv->channel);
Dan Williams822ac032009-05-22 20:07:14 -0400883
884done:
885 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
886 return ret;
887}
888
Holger Schurig697900a2008-04-02 16:27:10 +0200889/**
890 * @brief Join an adhoc network found in a previous scan
891 *
892 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400893 * @param assoc_req The association request describing the BSS to join
Holger Schurig697900a2008-04-02 16:27:10 +0200894 *
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400895 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200896 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400897static int lbs_adhoc_join(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200898 struct assoc_request *assoc_req)
899{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400900 struct cmd_ds_802_11_ad_hoc_join cmd;
Holger Schurig697900a2008-04-02 16:27:10 +0200901 struct bss_descriptor *bss = &assoc_req->bss;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400902 u8 preamble = RADIO_PREAMBLE_LONG;
John W. Linville9387b7c2008-09-30 20:59:05 -0400903 DECLARE_SSID_BUF(ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400904 u16 ratesize = 0;
905 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400906
907 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200908
909 lbs_deb_join("current SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400910 print_ssid(ssid, priv->curbssparams.ssid,
Holger Schurig697900a2008-04-02 16:27:10 +0200911 priv->curbssparams.ssid_len),
912 priv->curbssparams.ssid_len);
913 lbs_deb_join("requested ssid '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400914 print_ssid(ssid, bss->ssid, bss->ssid_len),
Holger Schurig697900a2008-04-02 16:27:10 +0200915 bss->ssid_len);
916
917 /* check if the requested SSID is already joined */
918 if (priv->curbssparams.ssid_len &&
919 !lbs_ssid_cmp(priv->curbssparams.ssid,
920 priv->curbssparams.ssid_len,
921 bss->ssid, bss->ssid_len) &&
922 (priv->mode == IW_MODE_ADHOC) &&
923 (priv->connect_status == LBS_CONNECTED)) {
924 union iwreq_data wrqu;
925
926 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
927 "current, not attempting to re-join");
928
929 /* Send the re-association event though, because the association
930 * request really was successful, even if just a null-op.
931 */
932 memset(&wrqu, 0, sizeof(wrqu));
933 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
934 ETH_ALEN);
935 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
936 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
937 goto out;
938 }
939
Dan Williamsd5db2df2008-08-21 17:51:07 -0400940 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig0e78ff82009-12-02 15:26:03 +0100941 if (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Holger Schurig697900a2008-04-02 16:27:10 +0200942 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400943 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200944 }
945
Dan Williamsd5db2df2008-08-21 17:51:07 -0400946 ret = lbs_set_radio(priv, preamble, 1);
947 if (ret)
948 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200949
950 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
951 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
952
953 priv->adhoccreate = 0;
Holger Schurigc14951f2009-10-22 15:30:50 +0200954 priv->channel = bss->channel;
Holger Schurig697900a2008-04-02 16:27:10 +0200955
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400956 /* Build the join command */
957 memset(&cmd, 0, sizeof(cmd));
958 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
959
960 cmd.bss.type = CMD_BSS_TYPE_IBSS;
961 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
962
963 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
964 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
965
Dan Williams5fd164e2009-05-22 20:01:21 -0400966 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400967
Dan Williams5fd164e2009-05-22 20:01:21 -0400968 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
969 sizeof(struct ieee_ie_ibss_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400970
971 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
972 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
973 bss->capability, CAPINFO_MASK);
974
975 /* information on BSSID descriptor passed to FW */
Johannes Berge1749612008-10-27 15:59:26 -0700976 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
977 cmd.bss.bssid, cmd.bss.ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400978
979 /* Only v8 and below support setting these */
980 if (priv->fwrelease < 0x09000000) {
981 /* failtimeout */
982 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
983 /* probedelay */
984 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
985 }
986
987 /* Copy Data rates from the rates recorded in scan response */
988 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
Dan Williamsca4fe302009-08-21 09:35:20 -0500989 ratesize = min_t(u16, ARRAY_SIZE(cmd.bss.rates), ARRAY_SIZE (bss->rates));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400990 memcpy(cmd.bss.rates, bss->rates, ratesize);
991 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
992 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
993 ret = -1;
994 goto out;
995 }
996
997 /* Copy the ad-hoc creation rates into Current BSS state structure */
998 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
999 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
1000
1001 /* Set MSB on basic rates as the firmware requires, but _after_
1002 * copying to current bss rates.
1003 */
1004 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
1005
Dan Williams5fd164e2009-05-22 20:01:21 -04001006 cmd.bss.ibss.atimwindow = bss->atimwindow;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001007
1008 if (assoc_req->secinfo.wep_enabled) {
1009 u16 tmp = le16_to_cpu(cmd.bss.capability);
1010 tmp |= WLAN_CAPABILITY_PRIVACY;
1011 cmd.bss.capability = cpu_to_le16(tmp);
1012 }
1013
1014 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1015 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
1016
1017 /* wake up first */
1018 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
1019 CMD_ACT_SET, 0, 0,
1020 &local_ps_mode);
1021 if (ret) {
1022 ret = -1;
1023 goto out;
1024 }
1025 }
1026
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001027 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
Dan Williams822ac032009-05-22 20:07:14 -04001028 if (ret == 0) {
1029 ret = lbs_adhoc_post(priv,
1030 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
1031 }
Holger Schurig697900a2008-04-02 16:27:10 +02001032
1033out:
Dan Williamsd5db2df2008-08-21 17:51:07 -04001034 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +02001035 return ret;
1036}
1037
1038/**
1039 * @brief Start an Adhoc Network
1040 *
1041 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -04001042 * @param assoc_req The association request describing the BSS to start
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001043 *
1044 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +02001045 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001046static int lbs_adhoc_start(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +02001047 struct assoc_request *assoc_req)
1048{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001049 struct cmd_ds_802_11_ad_hoc_start cmd;
Holger Schurig0e78ff82009-12-02 15:26:03 +01001050 u8 preamble = RADIO_PREAMBLE_SHORT;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001051 size_t ratesize = 0;
1052 u16 tmpcap = 0;
1053 int ret = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001054 DECLARE_SSID_BUF(ssid);
Dan Williamsd5db2df2008-08-21 17:51:07 -04001055
1056 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +02001057
Dan Williamsd5db2df2008-08-21 17:51:07 -04001058 ret = lbs_set_radio(priv, preamble, 1);
1059 if (ret)
1060 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +02001061
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001062 /* Build the start command */
1063 memset(&cmd, 0, sizeof(cmd));
1064 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurig697900a2008-04-02 16:27:10 +02001065
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001066 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
1067
1068 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001069 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001070 assoc_req->ssid_len);
1071
1072 cmd.bsstype = CMD_BSS_TYPE_IBSS;
1073
1074 if (priv->beacon_period == 0)
1075 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
1076 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
1077
1078 WARN_ON(!assoc_req->channel);
1079
1080 /* set Physical parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -04001081 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
1082 cmd.ds.header.len = 1;
Dan Williams5fd164e2009-05-22 20:01:21 -04001083 cmd.ds.channel = assoc_req->channel;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001084
1085 /* set IBSS parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -04001086 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1087 cmd.ibss.header.len = 2;
Dan Williams5fd164e2009-05-22 20:01:21 -04001088 cmd.ibss.atimwindow = cpu_to_le16(0);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001089
1090 /* set capability info */
1091 tmpcap = WLAN_CAPABILITY_IBSS;
Dan Williams2fa7a982009-05-22 20:09:58 -04001092 if (assoc_req->secinfo.wep_enabled ||
1093 assoc_req->secinfo.WPAenabled ||
1094 assoc_req->secinfo.WPA2enabled) {
1095 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001096 tmpcap |= WLAN_CAPABILITY_PRIVACY;
1097 } else
Dan Williams2fa7a982009-05-22 20:09:58 -04001098 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001099
1100 cmd.capability = cpu_to_le16(tmpcap);
1101
1102 /* Only v8 and below support setting probe delay */
1103 if (priv->fwrelease < 0x09000000)
1104 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1105
1106 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
1107 memcpy(cmd.rates, lbs_bg_rates, ratesize);
1108
1109 /* Copy the ad-hoc creating rates into Current BSS state structure */
1110 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1111 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
1112
1113 /* Set MSB on basic rates as the firmware requires, but _after_
1114 * copying to current bss rates.
1115 */
1116 lbs_set_basic_rate_flags(cmd.rates, ratesize);
1117
1118 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
1119 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
1120
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001121 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
1122 assoc_req->channel, assoc_req->band);
1123
1124 priv->adhoccreate = 1;
1125 priv->mode = IW_MODE_ADHOC;
1126
1127 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
1128 if (ret == 0)
Dan Williams822ac032009-05-22 20:07:14 -04001129 ret = lbs_adhoc_post(priv,
1130 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001131
Dan Williamsd5db2df2008-08-21 17:51:07 -04001132out:
1133 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +02001134 return ret;
1135}
1136
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001137/**
1138 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
1139 *
1140 * @param priv A pointer to struct lbs_private structure
1141 * @return 0 on success, or an error
1142 */
1143int lbs_adhoc_stop(struct lbs_private *priv)
Holger Schurig697900a2008-04-02 16:27:10 +02001144{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001145 struct cmd_ds_802_11_ad_hoc_stop cmd;
1146 int ret;
1147
1148 lbs_deb_enter(LBS_DEB_JOIN);
1149
1150 memset(&cmd, 0, sizeof (cmd));
1151 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
1152
1153 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
1154
1155 /* Clean up everything even if there was an error */
1156 lbs_mac_event_disconnected(priv);
1157
1158 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1159 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001160}
Dan Williamse76850d2007-05-25 17:09:41 -04001161
Holger Schurig245bf202008-04-02 16:27:42 +02001162static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
1163 struct bss_descriptor *match_bss)
1164{
1165 if (!secinfo->wep_enabled && !secinfo->WPAenabled
1166 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +01001167 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
1168 && match_bss->rsn_ie[0] != WLAN_EID_RSN
Holger Schurig245bf202008-04-02 16:27:42 +02001169 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
1170 return 1;
1171 else
1172 return 0;
1173}
1174
1175static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
1176 struct bss_descriptor *match_bss)
1177{
1178 if (secinfo->wep_enabled && !secinfo->WPAenabled
1179 && !secinfo->WPA2enabled
1180 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
1181 return 1;
1182 else
1183 return 0;
1184}
1185
1186static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
1187 struct bss_descriptor *match_bss)
1188{
1189 if (!secinfo->wep_enabled && secinfo->WPAenabled
Johannes Berg2c7060022008-10-30 22:09:54 +01001190 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
Holger Schurig245bf202008-04-02 16:27:42 +02001191 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
1192 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
1193 )
1194 return 1;
1195 else
1196 return 0;
1197}
1198
1199static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
1200 struct bss_descriptor *match_bss)
1201{
1202 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
Johannes Berg2c7060022008-10-30 22:09:54 +01001203 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +02001204 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
1205 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
1206 )
1207 return 1;
1208 else
1209 return 0;
1210}
1211
1212static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
1213 struct bss_descriptor *match_bss)
1214{
1215 if (!secinfo->wep_enabled && !secinfo->WPAenabled
1216 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +01001217 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
1218 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +02001219 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
1220 return 1;
1221 else
1222 return 0;
1223}
1224
1225/**
1226 * @brief Check if a scanned network compatible with the driver settings
1227 *
1228 * WEP WPA WPA2 ad-hoc encrypt Network
1229 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
1230 * 0 0 0 0 NONE 0 0 0 yes No security
1231 * 1 0 0 0 NONE 1 0 0 yes Static WEP
1232 * 0 1 0 0 x 1x 1 x yes WPA
1233 * 0 0 1 0 x 1x x 1 yes WPA2
1234 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
1235 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
1236 *
1237 *
1238 * @param priv A pointer to struct lbs_private
1239 * @param index Index in scantable to check against current driver settings
1240 * @param mode Network mode: Infrastructure or IBSS
1241 *
1242 * @return Index in scantable, or error code if negative
1243 */
1244static int is_network_compatible(struct lbs_private *priv,
1245 struct bss_descriptor *bss, uint8_t mode)
1246{
1247 int matched = 0;
1248
1249 lbs_deb_enter(LBS_DEB_SCAN);
1250
1251 if (bss->mode != mode)
1252 goto done;
1253
1254 matched = match_bss_no_security(&priv->secinfo, bss);
1255 if (matched)
1256 goto done;
1257 matched = match_bss_static_wep(&priv->secinfo, bss);
1258 if (matched)
1259 goto done;
1260 matched = match_bss_wpa(&priv->secinfo, bss);
1261 if (matched) {
1262 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
1263 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
1264 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
1265 priv->secinfo.wep_enabled ? "e" : "d",
1266 priv->secinfo.WPAenabled ? "e" : "d",
1267 priv->secinfo.WPA2enabled ? "e" : "d",
1268 (bss->capability & WLAN_CAPABILITY_PRIVACY));
1269 goto done;
1270 }
1271 matched = match_bss_wpa2(&priv->secinfo, bss);
1272 if (matched) {
1273 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
1274 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
1275 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
1276 priv->secinfo.wep_enabled ? "e" : "d",
1277 priv->secinfo.WPAenabled ? "e" : "d",
1278 priv->secinfo.WPA2enabled ? "e" : "d",
1279 (bss->capability & WLAN_CAPABILITY_PRIVACY));
1280 goto done;
1281 }
1282 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
1283 if (matched) {
1284 lbs_deb_scan("is_network_compatible() dynamic WEP: "
1285 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
1286 bss->wpa_ie[0], bss->rsn_ie[0],
1287 (bss->capability & WLAN_CAPABILITY_PRIVACY));
1288 goto done;
1289 }
1290
1291 /* bss security settings don't match those configured on card */
1292 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
1293 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
1294 bss->wpa_ie[0], bss->rsn_ie[0],
1295 priv->secinfo.wep_enabled ? "e" : "d",
1296 priv->secinfo.WPAenabled ? "e" : "d",
1297 priv->secinfo.WPA2enabled ? "e" : "d",
1298 (bss->capability & WLAN_CAPABILITY_PRIVACY));
1299
1300done:
1301 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
1302 return matched;
1303}
1304
1305/**
1306 * @brief This function finds a specific compatible BSSID in the scan list
1307 *
1308 * Used in association code
1309 *
1310 * @param priv A pointer to struct lbs_private
1311 * @param bssid BSSID to find in the scan list
1312 * @param mode Network mode: Infrastructure or IBSS
1313 *
1314 * @return index in BSSID list, or error return code (< 0)
1315 */
1316static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
1317 uint8_t *bssid, uint8_t mode)
1318{
1319 struct bss_descriptor *iter_bss;
1320 struct bss_descriptor *found_bss = NULL;
1321
1322 lbs_deb_enter(LBS_DEB_SCAN);
1323
1324 if (!bssid)
1325 goto out;
1326
1327 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
1328
1329 /* Look through the scan table for a compatible match. The loop will
1330 * continue past a matched bssid that is not compatible in case there
1331 * is an AP with multiple SSIDs assigned to the same BSSID
1332 */
1333 mutex_lock(&priv->lock);
1334 list_for_each_entry(iter_bss, &priv->network_list, list) {
1335 if (compare_ether_addr(iter_bss->bssid, bssid))
1336 continue; /* bssid doesn't match */
1337 switch (mode) {
1338 case IW_MODE_INFRA:
1339 case IW_MODE_ADHOC:
1340 if (!is_network_compatible(priv, iter_bss, mode))
1341 break;
1342 found_bss = iter_bss;
1343 break;
1344 default:
1345 found_bss = iter_bss;
1346 break;
1347 }
1348 }
1349 mutex_unlock(&priv->lock);
1350
1351out:
1352 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1353 return found_bss;
1354}
1355
1356/**
1357 * @brief This function finds ssid in ssid list.
1358 *
1359 * Used in association code
1360 *
1361 * @param priv A pointer to struct lbs_private
1362 * @param ssid SSID to find in the list
1363 * @param bssid BSSID to qualify the SSID selection (if provided)
1364 * @param mode Network mode: Infrastructure or IBSS
1365 *
1366 * @return index in BSSID list
1367 */
1368static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
1369 uint8_t *ssid, uint8_t ssid_len,
1370 uint8_t *bssid, uint8_t mode,
1371 int channel)
1372{
1373 u32 bestrssi = 0;
1374 struct bss_descriptor *iter_bss = NULL;
1375 struct bss_descriptor *found_bss = NULL;
1376 struct bss_descriptor *tmp_oldest = NULL;
1377
1378 lbs_deb_enter(LBS_DEB_SCAN);
1379
1380 mutex_lock(&priv->lock);
1381
1382 list_for_each_entry(iter_bss, &priv->network_list, list) {
1383 if (!tmp_oldest ||
1384 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1385 tmp_oldest = iter_bss;
1386
1387 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1388 ssid, ssid_len) != 0)
1389 continue; /* ssid doesn't match */
1390 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1391 continue; /* bssid doesn't match */
1392 if ((channel > 0) && (iter_bss->channel != channel))
1393 continue; /* channel doesn't match */
1394
1395 switch (mode) {
1396 case IW_MODE_INFRA:
1397 case IW_MODE_ADHOC:
1398 if (!is_network_compatible(priv, iter_bss, mode))
1399 break;
1400
1401 if (bssid) {
1402 /* Found requested BSSID */
1403 found_bss = iter_bss;
1404 goto out;
1405 }
1406
1407 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1408 bestrssi = SCAN_RSSI(iter_bss->rssi);
1409 found_bss = iter_bss;
1410 }
1411 break;
1412 case IW_MODE_AUTO:
1413 default:
1414 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1415 bestrssi = SCAN_RSSI(iter_bss->rssi);
1416 found_bss = iter_bss;
1417 }
1418 break;
1419 }
1420 }
1421
1422out:
1423 mutex_unlock(&priv->lock);
1424 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1425 return found_bss;
1426}
1427
Holger Schurig69f90322007-11-23 15:43:44 +01001428static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001429 struct assoc_request * assoc_req)
1430{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001431 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001432 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001433 int channel = -1;
John W. Linville9387b7c2008-09-30 20:59:05 -04001434 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001435
Holger Schurig9012b282007-05-25 11:27:16 -04001436 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001437
Dan Williamsef9a2642007-05-25 16:46:33 -04001438 /* FIXME: take channel into account when picking SSIDs if a channel
1439 * is set.
1440 */
1441
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001442 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1443 channel = assoc_req->channel;
1444
Holger Schurig0765af42007-10-15 12:55:56 +02001445 lbs_deb_assoc("SSID '%s' requested\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001446 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -04001447 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -05001448 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001449 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001450
David Woodhouseaa21c002007-12-08 20:04:36 +00001451 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001452 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001453 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -04001454 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams822ac032009-05-22 20:07:14 -04001455 ret = lbs_try_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001456 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -04001457 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001458 }
Dan Williams0dc5a292007-05-10 22:58:02 -04001459 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001460 /* Scan for the network, do not save previous results. Stale
1461 * scan data will cause us to join a non-existant adhoc network
1462 */
Holger Schurig10078322007-11-15 18:05:47 -05001463 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001464 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001465
1466 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001467 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001468 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001469 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -04001470 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001471 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001472 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001473 } else {
1474 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -04001475 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001476 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Holger Schurig243e84e2009-10-22 15:30:47 +02001477 IEEE80211_MAX_SSID_LEN);
Dan Williamsd8efea22007-05-28 23:54:55 -04001478 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001479 lbs_adhoc_start(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001480 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001481 }
1482
Holger Schurig9012b282007-05-25 11:27:16 -04001483 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001484 return ret;
1485}
1486
1487
Holger Schurig69f90322007-11-23 15:43:44 +01001488static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001489 struct assoc_request * assoc_req)
1490{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001491 int ret = 0;
1492 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001493
Johannes Berge1749612008-10-27 15:59:26 -07001494 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001495
1496 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +00001497 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001498 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001499 if (bss == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -07001500 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1501 "cannot associate.\n", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001502 goto out;
1503 }
1504
Dan Williamse76850d2007-05-25 17:09:41 -04001505 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -04001506 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williams822ac032009-05-22 20:07:14 -04001507 ret = lbs_try_associate(priv, assoc_req);
1508 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1509 ret);
Dan Williams0dc5a292007-05-10 22:58:02 -04001510 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001511 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001512 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001513
1514out:
Holger Schurig9012b282007-05-25 11:27:16 -04001515 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001516 return ret;
1517}
1518
1519
Holger Schurig69f90322007-11-23 15:43:44 +01001520static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001521 struct assoc_request * assoc_req)
1522{
1523 int ret = 0, done = 0;
1524
Holger Schurig0765af42007-10-15 12:55:56 +02001525 lbs_deb_enter(LBS_DEB_ASSOC);
1526
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001527 /* If we're given and 'any' BSSID, try associating based on SSID */
1528
1529 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -04001530 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1531 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001532 ret = assoc_helper_bssid(priv, assoc_req);
1533 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001534 }
1535 }
1536
1537 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1538 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001539 }
1540
Holger Schurig0765af42007-10-15 12:55:56 +02001541 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001542 return ret;
1543}
1544
1545
Holger Schurig69f90322007-11-23 15:43:44 +01001546static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001547 struct assoc_request * assoc_req)
1548{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001549 int ret = 0;
1550
Holger Schurig9012b282007-05-25 11:27:16 -04001551 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001552
David Woodhouseaa21c002007-12-08 20:04:36 +00001553 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -04001554 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001555
Dan Williams0dc5a292007-05-10 22:58:02 -04001556 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001557 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -05001558 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +00001559 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001560 }
1561
David Woodhouseaa21c002007-12-08 20:04:36 +00001562 priv->mode = assoc_req->mode;
Holger Schurigfef06402009-10-22 15:30:59 +02001563 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE,
1564 assoc_req->mode == IW_MODE_ADHOC ? 2 : 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001565
Holger Schurig9012b282007-05-25 11:27:16 -04001566done:
1567 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001568 return ret;
1569}
1570
Holger Schurig69f90322007-11-23 15:43:44 +01001571static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -04001572 struct assoc_request * assoc_req)
1573{
Dan Williamsef9a2642007-05-25 16:46:33 -04001574 int ret = 0;
1575
1576 lbs_deb_enter(LBS_DEB_ASSOC);
1577
David Woodhouse9f462572007-12-12 22:50:21 -05001578 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001579 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001580 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001581 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -04001582 }
1583
Holger Schurigc14951f2009-10-22 15:30:50 +02001584 if (assoc_req->channel == priv->channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001585 goto done;
1586
David Woodhouse8642f1f2007-12-11 20:03:01 -05001587 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -05001588 /* Change mesh channel first; 21.p21 firmware won't let
1589 you change channel otherwise (even though it'll return
1590 an error to this */
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001591 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1592 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001593 }
1594
Dan Williamsef9a2642007-05-25 16:46:33 -04001595 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
Holger Schurigc14951f2009-10-22 15:30:50 +02001596 priv->channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -04001597
Dan Williams2dd4b262007-12-11 16:54:15 -05001598 ret = lbs_set_channel(priv, assoc_req->channel);
1599 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -05001600 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -04001601
Dan Williams2dd4b262007-12-11 16:54:15 -05001602 /* FIXME: shouldn't need to grab the channel _again_ after setting
1603 * it since the firmware is supposed to return the new channel, but
1604 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -05001605 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001606 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001607 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001608 goto done;
1609 }
Dan Williamsef9a2642007-05-25 16:46:33 -04001610
Holger Schurigc14951f2009-10-22 15:30:50 +02001611 if (assoc_req->channel != priv->channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -05001612 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -04001613 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001614 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -04001615 }
1616
1617 if ( assoc_req->secinfo.wep_enabled
1618 && (assoc_req->wep_keys[0].len
1619 || assoc_req->wep_keys[1].len
1620 || assoc_req->wep_keys[2].len
1621 || assoc_req->wep_keys[3].len)) {
1622 /* Make sure WEP keys are re-sent to firmware */
1623 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1624 }
1625
1626 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -05001627 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -04001628
David Woodhouse8642f1f2007-12-11 20:03:01 -05001629 restore_mesh:
1630 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001631 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
Holger Schurigc14951f2009-10-22 15:30:50 +02001632 priv->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001633
1634 done:
Dan Williamsef9a2642007-05-25 16:46:33 -04001635 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1636 return ret;
1637}
1638
1639
Holger Schurig69f90322007-11-23 15:43:44 +01001640static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -05001641 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001642{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001643 int i;
1644 int ret = 0;
1645
Holger Schurig9012b282007-05-25 11:27:16 -04001646 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001647
1648 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -05001649 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1650 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1651 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1652 else
1653 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001654
1655 if (ret)
1656 goto out;
1657
1658 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -04001659 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +01001660 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001661 else
Holger Schurigd9e97782008-03-12 16:06:43 +01001662 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -05001663
Holger Schurigc97329e2008-03-18 11:20:21 +01001664 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001665
David Woodhouseaa21c002007-12-08 20:04:36 +00001666 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001667
David Woodhouseaa21c002007-12-08 20:04:36 +00001668 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001669 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001670 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -05001671 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001672 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001673 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001674
David Woodhouseaa21c002007-12-08 20:04:36 +00001675 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001676
1677out:
Holger Schurig9012b282007-05-25 11:27:16 -04001678 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001679 return ret;
1680}
1681
Holger Schurig69f90322007-11-23 15:43:44 +01001682static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001683 struct assoc_request * assoc_req)
1684{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001685 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -05001686 uint16_t do_wpa;
1687 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001688
Holger Schurig9012b282007-05-25 11:27:16 -04001689 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001690
David Woodhouseaa21c002007-12-08 20:04:36 +00001691 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001692 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001693
Holger Schurigc97329e2008-03-18 11:20:21 +01001694 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001695
Dan Williams18c96c342007-06-18 12:01:12 -04001696 /* If RSN is already enabled, don't try to enable it again, since
1697 * ENABLE_RSN resets internal state machines and will clobber the
1698 * 4-way WPA handshake.
1699 */
1700
1701 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001702 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -04001703 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001704 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -04001705 goto out;
1706 }
1707
1708 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001709 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -04001710 if (do_wpa == rsn)
1711 goto out;
1712
1713 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001714 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -04001715
1716out:
Holger Schurig9012b282007-05-25 11:27:16 -04001717 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001718 return ret;
1719}
1720
1721
Holger Schurig69f90322007-11-23 15:43:44 +01001722static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001723 struct assoc_request * assoc_req)
1724{
1725 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -04001726 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001727
Holger Schurig9012b282007-05-25 11:27:16 -04001728 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001729
Dan Williams2bcde512007-10-03 10:37:45 -04001730 /* Work around older firmware bug where WPA unicast and multicast
1731 * keys must be set independently. Seen in SDIO parts with firmware
1732 * version 5.0.11p0.
1733 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001734
Dan Williams2bcde512007-10-03 10:37:45 -04001735 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1736 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +01001737 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001738 assoc_req->flags = flags;
1739 }
1740
1741 if (ret)
1742 goto out;
1743
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001744 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1745 sizeof(struct enc_key));
1746
Dan Williams2bcde512007-10-03 10:37:45 -04001747 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1748 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1749
David Woodhouse9e1228d2008-03-03 12:15:39 +01001750 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001751 assoc_req->flags = flags;
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001752
1753 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1754 sizeof(struct enc_key));
Dan Williams2bcde512007-10-03 10:37:45 -04001755 }
1756
1757out:
Holger Schurig9012b282007-05-25 11:27:16 -04001758 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001759 return ret;
1760}
1761
1762
Holger Schurig69f90322007-11-23 15:43:44 +01001763static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001764 struct assoc_request * assoc_req)
1765{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001766 int ret = 0;
1767
Holger Schurig9012b282007-05-25 11:27:16 -04001768 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001769
1770 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001771 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1772 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001773 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +00001774 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1775 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001776 }
1777
Holger Schurig9012b282007-05-25 11:27:16 -04001778 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001779 return ret;
1780}
1781
1782
David Woodhouseaa21c002007-12-08 20:04:36 +00001783static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001784 struct assoc_request * assoc_req)
1785{
Holger Schurig0765af42007-10-15 12:55:56 +02001786 int ret = 0;
1787
David Woodhouseaa21c002007-12-08 20:04:36 +00001788 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001789 return 0;
1790
Holger Schurig52507c22008-01-28 17:25:53 +01001791 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001792 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001793 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1794 ret = 1;
1795 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001796 }
1797
1798 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001799 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +02001800 lbs_deb_assoc("Deauthenticating due to new security\n");
1801 ret = 1;
1802 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001803 }
1804 }
1805
1806 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001807 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1808 ret = 1;
1809 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001810 }
1811
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001812 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001813 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1814 ret = 1;
1815 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001816 }
1817
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001818 /* FIXME: deal with 'auto' mode somehow */
1819 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001820 if (assoc_req->mode != IW_MODE_INFRA) {
1821 lbs_deb_assoc("Deauthenticating due to leaving "
1822 "infra mode\n");
1823 ret = 1;
1824 goto out;
1825 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001826 }
1827
Holger Schurig0765af42007-10-15 12:55:56 +02001828out:
1829 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +01001830 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001831}
1832
1833
David Woodhouseaa21c002007-12-08 20:04:36 +00001834static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001835 struct assoc_request * assoc_req)
1836{
Holger Schurig0765af42007-10-15 12:55:56 +02001837 lbs_deb_enter(LBS_DEB_ASSOC);
1838
David Woodhouseaa21c002007-12-08 20:04:36 +00001839 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001840 return 0;
1841
David Woodhouseaa21c002007-12-08 20:04:36 +00001842 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1843 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001844 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001845 return 1;
1846
1847 /* FIXME: deal with 'auto' mode somehow */
1848 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001849 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001850 return 1;
1851 }
1852
Dan Williamsef9a2642007-05-25 16:46:33 -04001853 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurigc14951f2009-10-22 15:30:50 +02001854 if (assoc_req->channel != priv->channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001855 return 1;
1856 }
1857
Holger Schurig0765af42007-10-15 12:55:56 +02001858 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001859 return 0;
1860}
1861
1862
Holger Schurig245bf202008-04-02 16:27:42 +02001863/**
1864 * @brief This function finds the best SSID in the Scan List
1865 *
1866 * Search the scan table for the best SSID that also matches the current
1867 * adapter network preference (infrastructure or adhoc)
1868 *
1869 * @param priv A pointer to struct lbs_private
1870 *
1871 * @return index in BSSID list
1872 */
1873static struct bss_descriptor *lbs_find_best_ssid_in_list(
1874 struct lbs_private *priv, uint8_t mode)
1875{
1876 uint8_t bestrssi = 0;
1877 struct bss_descriptor *iter_bss;
1878 struct bss_descriptor *best_bss = NULL;
1879
1880 lbs_deb_enter(LBS_DEB_SCAN);
1881
1882 mutex_lock(&priv->lock);
1883
1884 list_for_each_entry(iter_bss, &priv->network_list, list) {
1885 switch (mode) {
1886 case IW_MODE_INFRA:
1887 case IW_MODE_ADHOC:
1888 if (!is_network_compatible(priv, iter_bss, mode))
1889 break;
1890 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1891 break;
1892 bestrssi = SCAN_RSSI(iter_bss->rssi);
1893 best_bss = iter_bss;
1894 break;
1895 case IW_MODE_AUTO:
1896 default:
1897 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1898 break;
1899 bestrssi = SCAN_RSSI(iter_bss->rssi);
1900 best_bss = iter_bss;
1901 break;
1902 }
1903 }
1904
1905 mutex_unlock(&priv->lock);
1906 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1907 return best_bss;
1908}
1909
1910/**
1911 * @brief Find the best AP
1912 *
1913 * Used from association worker.
1914 *
1915 * @param priv A pointer to struct lbs_private structure
1916 * @param pSSID A pointer to AP's ssid
1917 *
1918 * @return 0--success, otherwise--fail
1919 */
1920static int lbs_find_best_network_ssid(struct lbs_private *priv,
1921 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1922 uint8_t *out_mode)
1923{
1924 int ret = -1;
1925 struct bss_descriptor *found;
1926
1927 lbs_deb_enter(LBS_DEB_SCAN);
1928
1929 priv->scan_ssid_len = 0;
1930 lbs_scan_networks(priv, 1);
1931 if (priv->surpriseremoved)
1932 goto out;
1933
1934 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1935 if (found && (found->ssid_len > 0)) {
Holger Schurig243e84e2009-10-22 15:30:47 +02001936 memcpy(out_ssid, &found->ssid, IEEE80211_MAX_SSID_LEN);
Holger Schurig245bf202008-04-02 16:27:42 +02001937 *out_ssid_len = found->ssid_len;
1938 *out_mode = found->mode;
1939 ret = 0;
1940 }
1941
1942out:
1943 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1944 return ret;
1945}
1946
1947
Holger Schurig10078322007-11-15 18:05:47 -05001948void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001949{
Holger Schurig69f90322007-11-23 15:43:44 +01001950 struct lbs_private *priv = container_of(work, struct lbs_private,
1951 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001952 struct assoc_request * assoc_req = NULL;
1953 int ret = 0;
1954 int find_any_ssid = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001955 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001956
Holger Schurig9012b282007-05-25 11:27:16 -04001957 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001958
David Woodhouseaa21c002007-12-08 20:04:36 +00001959 mutex_lock(&priv->lock);
1960 assoc_req = priv->pending_assoc_req;
1961 priv->pending_assoc_req = NULL;
1962 priv->in_progress_assoc_req = assoc_req;
1963 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001964
Holger Schurig9012b282007-05-25 11:27:16 -04001965 if (!assoc_req)
1966 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001967
Holger Schurig0765af42007-10-15 12:55:56 +02001968 lbs_deb_assoc(
1969 "Association Request:\n"
1970 " flags: 0x%08lx\n"
1971 " SSID: '%s'\n"
1972 " chann: %d\n"
1973 " band: %d\n"
1974 " mode: %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001975 " BSSID: %pM\n"
Holger Schurig0765af42007-10-15 12:55:56 +02001976 " secinfo: %s%s%s\n"
1977 " auth_mode: %d\n",
1978 assoc_req->flags,
John W. Linville9387b7c2008-09-30 20:59:05 -04001979 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Holger Schurig0765af42007-10-15 12:55:56 +02001980 assoc_req->channel, assoc_req->band, assoc_req->mode,
Johannes Berge1749612008-10-27 15:59:26 -07001981 assoc_req->bssid,
Holger Schurig0765af42007-10-15 12:55:56 +02001982 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1983 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1984 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1985 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001986
1987 /* If 'any' SSID was specified, find an SSID to associate with */
1988 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001989 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001990 find_any_ssid = 1;
1991
1992 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1993 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -04001994 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1995 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001996 find_any_ssid = 0;
1997 }
1998
1999 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02002000 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002001
Holger Schurig10078322007-11-15 18:05:47 -05002002 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04002003 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002004 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04002005 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002006 ret = -ENETUNREACH;
2007 goto out;
2008 }
2009
2010 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04002011 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002012 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
2013 assoc_req->mode = new_mode;
2014 }
2015 }
2016
2017 /*
2018 * Check if the attributes being changing require deauthentication
2019 * from the currently associated infrastructure access point.
2020 */
David Woodhouseaa21c002007-12-08 20:04:36 +00002021 if (priv->mode == IW_MODE_INFRA) {
2022 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04002023 ret = lbs_cmd_80211_deauthenticate(priv,
2024 priv->curbssparams.bssid,
2025 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002026 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04002027 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002028 "configuration request failed: %d\n",
2029 ret);
2030 }
2031 }
David Woodhouseaa21c002007-12-08 20:04:36 +00002032 } else if (priv->mode == IW_MODE_ADHOC) {
2033 if (should_stop_adhoc(priv, assoc_req)) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04002034 ret = lbs_adhoc_stop(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002035 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04002036 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002037 "new configuration request failed: %d\n",
2038 ret);
2039 }
2040
2041 }
2042 }
2043
2044 /* Send the various configuration bits to the firmware */
2045 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
2046 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02002047 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002048 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002049 }
2050
Dan Williamsef9a2642007-05-25 16:46:33 -04002051 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
2052 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02002053 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04002054 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04002055 }
2056
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002057 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
2058 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
2059 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02002060 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002061 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002062 }
2063
2064 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
2065 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02002066 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002067 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002068 }
2069
2070 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
2071 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02002072 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002073 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002074 }
2075
2076 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
2077 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
2078 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02002079 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002080 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002081 }
2082
2083 /* SSID/BSSID should be the _last_ config option set, because they
2084 * trigger the association attempt.
2085 */
2086 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
2087 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
2088 int success = 1;
2089
2090 ret = assoc_helper_associate(priv, assoc_req);
2091 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01002092 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002093 ret);
2094 success = 0;
2095 }
2096
David Woodhouseaa21c002007-12-08 20:04:36 +00002097 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01002098 lbs_deb_assoc("ASSOC: association unsuccessful, "
2099 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002100 success = 0;
2101 }
2102
2103 if (success) {
Johannes Berge1749612008-10-27 15:59:26 -07002104 lbs_deb_assoc("associated to %pM\n",
2105 priv->curbssparams.bssid);
Holger Schurig10078322007-11-15 18:05:47 -05002106 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04002107 CMD_802_11_RSSI,
2108 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002109 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002110 ret = -1;
2111 }
2112 }
2113
2114out:
2115 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04002116 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002117 ret);
2118 }
Dan Williamse76850d2007-05-25 17:09:41 -04002119
David Woodhouseaa21c002007-12-08 20:04:36 +00002120 mutex_lock(&priv->lock);
2121 priv->in_progress_assoc_req = NULL;
2122 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002123 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04002124
2125done:
2126 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002127}
2128
2129
2130/*
2131 * Caller MUST hold any necessary locks
2132 */
David Woodhouseaa21c002007-12-08 20:04:36 +00002133struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002134{
2135 struct assoc_request * assoc_req;
2136
Holger Schurig0765af42007-10-15 12:55:56 +02002137 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00002138 if (!priv->pending_assoc_req) {
2139 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04002140 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00002141 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002142 lbs_pr_info("Not enough memory to allocate association"
2143 " request!\n");
2144 return NULL;
2145 }
2146 }
2147
2148 /* Copy current configuration attributes to the association request,
2149 * but don't overwrite any that are already set.
2150 */
David Woodhouseaa21c002007-12-08 20:04:36 +00002151 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002152 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002153 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Holger Schurig243e84e2009-10-22 15:30:47 +02002154 IEEE80211_MAX_SSID_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00002155 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002156 }
2157
2158 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
Holger Schurigc14951f2009-10-22 15:30:50 +02002159 assoc_req->channel = priv->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002160
Dan Williamse76850d2007-05-25 17:09:41 -04002161 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00002162 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04002163
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002164 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00002165 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002166
2167 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002168 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002169 ETH_ALEN);
2170 }
2171
2172 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
2173 int i;
2174 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002175 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04002176 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002177 }
2178 }
2179
2180 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00002181 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002182
2183 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002184 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04002185 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002186 }
2187
2188 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002189 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04002190 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002191 }
2192
2193 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002194 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05002195 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002196 }
2197
2198 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00002199 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002200 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00002201 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002202 }
2203
Holger Schurig0765af42007-10-15 12:55:56 +02002204 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02002205 return assoc_req;
2206}
Holger Schurig697900a2008-04-02 16:27:10 +02002207
2208
2209/**
Dan Williams191bb402008-08-21 17:46:18 -04002210 * @brief Deauthenticate from a specific BSS
2211 *
2212 * @param priv A pointer to struct lbs_private structure
2213 * @param bssid The specific BSS to deauthenticate from
2214 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
2215 *
2216 * @return 0 on success, error on failure
2217 */
2218int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
2219 u16 reason)
Holger Schurig697900a2008-04-02 16:27:10 +02002220{
Dan Williams191bb402008-08-21 17:46:18 -04002221 struct cmd_ds_802_11_deauthenticate cmd;
2222 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02002223
2224 lbs_deb_enter(LBS_DEB_JOIN);
2225
Dan Williams191bb402008-08-21 17:46:18 -04002226 memset(&cmd, 0, sizeof(cmd));
2227 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2228 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
2229 cmd.reasoncode = cpu_to_le16(reason);
Holger Schurig697900a2008-04-02 16:27:10 +02002230
Dan Williams191bb402008-08-21 17:46:18 -04002231 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02002232
Dan Williams191bb402008-08-21 17:46:18 -04002233 /* Clean up everything even if there was an error; can't assume that
2234 * we're still authenticated to the AP after trying to deauth.
2235 */
2236 lbs_mac_event_disconnected(priv);
Holger Schurig697900a2008-04-02 16:27:10 +02002237
2238 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04002239 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02002240}
2241