blob: a9440a96b2e72e4e21c226f824692201efae1483 [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 Williams3cf20932007-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
26
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040027/**
28 * @brief This function finds common rates between rates and card rates.
29 *
30 * It will fill common rates in rates as output if found.
31 *
32 * NOTE: Setting the MSB of the basic rates need to be taken
33 * care, either before or after calling this function
34 *
35 * @param priv A pointer to struct lbs_private structure
36 * @param rates the buffer which keeps input and output
37 * @param rates_size the size of rate1 buffer; new size of buffer on return
38 *
39 * @return 0 on success, or -1 on error
40 */
41static int get_common_rates(struct lbs_private *priv,
42 u8 *rates,
43 u16 *rates_size)
44{
45 u8 *card_rates = lbs_bg_rates;
David S. Miller97992182009-08-12 17:37:52 -070046 size_t num_card_rates = sizeof(lbs_bg_rates);
Roel Kluin1e3d31c2009-08-02 09:44:12 +020047 int i, j;
Andrey Yurovsky6f632d52009-08-13 17:34:40 -070048 u8 *tmp;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040049 size_t tmp_size = 0;
50
Roel Kluin1e3d31c2009-08-02 09:44:12 +020051 tmp = kzalloc(MAX_RATES * ARRAY_SIZE(lbs_bg_rates), GFP_KERNEL);
Andrey Yurovsky6f632d52009-08-13 17:34:40 -070052 if (!tmp)
53 return -1;
54
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040055 /* For each rate in card_rates that exists in rate1, copy to tmp */
David S. Miller97992182009-08-12 17:37:52 -070056 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
57 for (j = 0; rates[j] && (j < *rates_size); j++) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040058 if (rates[j] == card_rates[i])
59 tmp[tmp_size++] = card_rates[i];
60 }
61 }
62
63 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
David S. Miller97992182009-08-12 17:37:52 -070064 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040065 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
66 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
67
Roel Kluin1e3d31c2009-08-02 09:44:12 +020068 memset(rates, 0, *rates_size);
69 *rates_size = min_t(u16, tmp_size, *rates_size);
70 memcpy(rates, tmp, *rates_size);
71
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040072 if (!priv->enablehwauto) {
73 for (i = 0; i < tmp_size; i++) {
74 if (tmp[i] == priv->cur_rate)
Roel Kluin1e3d31c2009-08-02 09:44:12 +020075 break;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040076 }
Roel Kluin1e3d31c2009-08-02 09:44:12 +020077 if (i == tmp_size) {
78 lbs_pr_alert("Previously set fixed data rate %#x isn't "
79 "compatible with the network.\n",
80 priv->cur_rate);
81 return -1;
82 }
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040083 }
Andrey Yurovsky6f632d52009-08-13 17:34:40 -070084 kfree(tmp);
Roel Kluin1e3d31c2009-08-02 09:44:12 +020085 return 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040086}
87
88
89/**
90 * @brief Sets the MSB on basic rates as the firmware requires
91 *
92 * Scan through an array and set the MSB for basic data rates.
93 *
94 * @param rates buffer of data rates
95 * @param len size of buffer
96 */
97static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
98{
99 int i;
100
101 for (i = 0; i < len; i++) {
102 if (rates[i] == 0x02 || rates[i] == 0x04 ||
103 rates[i] == 0x0b || rates[i] == 0x16)
104 rates[i] |= 0x80;
105 }
106}
107
Holger Schurig697900a2008-04-02 16:27:10 +0200108
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400109static u8 iw_auth_to_ieee_auth(u8 auth)
110{
111 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
112 return 0x00;
113 else if (auth == IW_AUTH_ALG_SHARED_KEY)
114 return 0x01;
115 else if (auth == IW_AUTH_ALG_LEAP)
116 return 0x80;
117
118 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
119 return 0;
120}
121
122/**
123 * @brief This function prepares the authenticate command. AUTHENTICATE only
124 * sets the authentication suite for future associations, as the firmware
125 * handles authentication internally during the ASSOCIATE command.
126 *
127 * @param priv A pointer to struct lbs_private structure
128 * @param bssid The peer BSSID with which to authenticate
129 * @param auth The authentication mode to use (from wireless.h)
130 *
131 * @return 0 or -1
132 */
133static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
134{
135 struct cmd_ds_802_11_authenticate cmd;
136 int ret = -1;
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400137
138 lbs_deb_enter(LBS_DEB_JOIN);
139
140 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
141 memcpy(cmd.bssid, bssid, ETH_ALEN);
142
143 cmd.authtype = iw_auth_to_ieee_auth(auth);
144
Johannes Berge91d8332009-07-15 17:21:41 +0200145 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400146
147 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
148
149 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
150 return ret;
151}
152
Dan Williams822ac032009-05-22 20:07:14 -0400153
154static int lbs_assoc_post(struct lbs_private *priv,
155 struct cmd_ds_802_11_associate_response *resp)
156{
157 int ret = 0;
158 union iwreq_data wrqu;
159 struct bss_descriptor *bss;
160 u16 status_code;
161
162 lbs_deb_enter(LBS_DEB_ASSOC);
163
164 if (!priv->in_progress_assoc_req) {
165 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
166 ret = -1;
167 goto done;
168 }
169 bss = &priv->in_progress_assoc_req->bss;
170
171 /*
172 * Older FW versions map the IEEE 802.11 Status Code in the association
173 * response to the following values returned in resp->statuscode:
174 *
175 * IEEE Status Code Marvell Status Code
176 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
177 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
178 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
179 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
180 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
181 * others -> 0x0003 ASSOC_RESULT_REFUSED
182 *
183 * Other response codes:
184 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
185 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
186 * association response from the AP)
187 */
188
189 status_code = le16_to_cpu(resp->statuscode);
190 if (priv->fwrelease < 0x09000000) {
191 switch (status_code) {
192 case 0x00:
193 break;
194 case 0x01:
195 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
196 break;
197 case 0x02:
198 lbs_deb_assoc("ASSOC_RESP: internal timer "
199 "expired while waiting for the AP\n");
200 break;
201 case 0x03:
202 lbs_deb_assoc("ASSOC_RESP: association "
203 "refused by AP\n");
204 break;
205 case 0x04:
206 lbs_deb_assoc("ASSOC_RESP: authentication "
207 "refused by AP\n");
208 break;
209 default:
210 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
211 " unknown\n", status_code);
212 break;
213 }
214 } else {
215 /* v9+ returns the AP's association response */
216 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
217 }
218
219 if (status_code) {
220 lbs_mac_event_disconnected(priv);
221 ret = -1;
222 goto done;
223 }
224
225 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
226 (void *) (resp + sizeof (resp->hdr)),
227 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
228
229 /* Send a Media Connected event, according to the Spec */
230 priv->connect_status = LBS_CONNECTED;
231
232 /* Update current SSID and BSSID */
233 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
234 priv->curbssparams.ssid_len = bss->ssid_len;
235 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
236
237 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
238 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
239
240 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
241 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
242 priv->nextSNRNF = 0;
243 priv->numSNRNF = 0;
244
245 netif_carrier_on(priv->dev);
246 if (!priv->tx_pending_len)
247 netif_wake_queue(priv->dev);
248
249 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
250 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
251 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
252
253done:
254 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
255 return ret;
256}
257
258/**
259 * @brief This function prepares an association-class command.
260 *
261 * @param priv A pointer to struct lbs_private structure
262 * @param assoc_req The association request describing the BSS to associate
263 * or reassociate with
264 * @param command The actual command, either CMD_802_11_ASSOCIATE or
265 * CMD_802_11_REASSOCIATE
266 *
267 * @return 0 or -1
268 */
269static int lbs_associate(struct lbs_private *priv,
270 struct assoc_request *assoc_req,
271 u16 command)
272{
273 struct cmd_ds_802_11_associate cmd;
274 int ret = 0;
275 struct bss_descriptor *bss = &assoc_req->bss;
276 u8 *pos = &(cmd.iebuf[0]);
277 u16 tmpcap, tmplen, tmpauth;
278 struct mrvl_ie_ssid_param_set *ssid;
279 struct mrvl_ie_ds_param_set *ds;
280 struct mrvl_ie_cf_param_set *cf;
281 struct mrvl_ie_rates_param_set *rates;
282 struct mrvl_ie_rsn_param_set *rsn;
283 struct mrvl_ie_auth_type *auth;
284
285 lbs_deb_enter(LBS_DEB_ASSOC);
286
287 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
288 (command != CMD_802_11_REASSOCIATE));
289
290 memset(&cmd, 0, sizeof(cmd));
291 cmd.hdr.command = cpu_to_le16(command);
292
293 /* Fill in static fields */
294 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
295 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
296
297 /* Capability info */
298 tmpcap = (bss->capability & CAPINFO_MASK);
299 if (bss->mode == IW_MODE_INFRA)
300 tmpcap |= WLAN_CAPABILITY_ESS;
301 cmd.capability = cpu_to_le16(tmpcap);
302 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
303
304 /* SSID */
305 ssid = (struct mrvl_ie_ssid_param_set *) pos;
306 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
307 tmplen = bss->ssid_len;
308 ssid->header.len = cpu_to_le16(tmplen);
309 memcpy(ssid->ssid, bss->ssid, tmplen);
310 pos += sizeof(ssid->header) + tmplen;
311
312 ds = (struct mrvl_ie_ds_param_set *) pos;
313 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
314 ds->header.len = cpu_to_le16(1);
315 ds->channel = bss->phy.ds.channel;
316 pos += sizeof(ds->header) + 1;
317
318 cf = (struct mrvl_ie_cf_param_set *) pos;
319 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
320 tmplen = sizeof(*cf) - sizeof (cf->header);
321 cf->header.len = cpu_to_le16(tmplen);
322 /* IE payload should be zeroed, firmware fills it in for us */
323 pos += sizeof(*cf);
324
325 rates = (struct mrvl_ie_rates_param_set *) pos;
326 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
David S. Miller97992182009-08-12 17:37:52 -0700327 tmplen = MAX_RATES;
Roel Kluin1e3d31c2009-08-02 09:44:12 +0200328 memcpy(&rates->rates, &bss->rates, tmplen);
Dan Williams822ac032009-05-22 20:07:14 -0400329 if (get_common_rates(priv, rates->rates, &tmplen)) {
330 ret = -1;
331 goto done;
332 }
333 pos += sizeof(rates->header) + tmplen;
334 rates->header.len = cpu_to_le16(tmplen);
335 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
336
337 /* Copy the infra. association rates into Current BSS state structure */
338 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
339 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
340
341 /* Set MSB on basic rates as the firmware requires, but _after_
342 * copying to current bss rates.
343 */
344 lbs_set_basic_rate_flags(rates->rates, tmplen);
345
346 /* Firmware v9+ indicate authentication suites as a TLV */
347 if (priv->fwrelease >= 0x09000000) {
Dan Williams822ac032009-05-22 20:07:14 -0400348 auth = (struct mrvl_ie_auth_type *) pos;
349 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
350 auth->header.len = cpu_to_le16(2);
351 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
352 auth->auth = cpu_to_le16(tmpauth);
353 pos += sizeof(auth->header) + 2;
354
Johannes Berge91d8332009-07-15 17:21:41 +0200355 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
356 bss->bssid, priv->secinfo.auth_mode);
Dan Williams822ac032009-05-22 20:07:14 -0400357 }
358
359 /* WPA/WPA2 IEs */
360 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
361 rsn = (struct mrvl_ie_rsn_param_set *) pos;
362 /* WPA_IE or WPA2_IE */
363 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
364 tmplen = (u16) assoc_req->wpa_ie[1];
365 rsn->header.len = cpu_to_le16(tmplen);
366 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
367 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
368 sizeof(rsn->header) + tmplen);
369 pos += sizeof(rsn->header) + tmplen;
370 }
371
372 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
373 (u16)(pos - (u8 *) &cmd.iebuf));
374
375 /* update curbssparams */
376 priv->curbssparams.channel = bss->phy.ds.channel;
377
378 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
379 ret = -1;
380 goto done;
381 }
382
383 ret = lbs_cmd_with_response(priv, command, &cmd);
384 if (ret == 0) {
385 ret = lbs_assoc_post(priv,
386 (struct cmd_ds_802_11_associate_response *) &cmd);
387 }
388
389done:
390 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
391 return ret;
392}
393
Holger Schurig697900a2008-04-02 16:27:10 +0200394/**
395 * @brief Associate to a specific BSS discovered in a scan
396 *
397 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400398 * @param assoc_req The association request describing the BSS to associate with
Holger Schurig697900a2008-04-02 16:27:10 +0200399 *
400 * @return 0-success, otherwise fail
401 */
Dan Williams822ac032009-05-22 20:07:14 -0400402static int lbs_try_associate(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200403 struct assoc_request *assoc_req)
404{
405 int ret;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400406 u8 preamble = RADIO_PREAMBLE_LONG;
Holger Schurig697900a2008-04-02 16:27:10 +0200407
408 lbs_deb_enter(LBS_DEB_ASSOC);
409
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400410 /* FW v9 and higher indicate authentication suites as a TLV in the
411 * association command, not as a separate authentication command.
412 */
413 if (priv->fwrelease < 0x09000000) {
414 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
415 priv->secinfo.auth_mode);
416 if (ret)
417 goto out;
418 }
Holger Schurig697900a2008-04-02 16:27:10 +0200419
Dan Williamsd5db2df2008-08-21 17:51:07 -0400420 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig697900a2008-04-02 16:27:10 +0200421 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
422 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
Dan Williamsd5db2df2008-08-21 17:51:07 -0400423 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200424
Dan Williamsd5db2df2008-08-21 17:51:07 -0400425 ret = lbs_set_radio(priv, preamble, 1);
426 if (ret)
427 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200428
Dan Williams822ac032009-05-22 20:07:14 -0400429 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
Holger Schurig697900a2008-04-02 16:27:10 +0200430
Dan Williamsd5db2df2008-08-21 17:51:07 -0400431out:
Holger Schurig697900a2008-04-02 16:27:10 +0200432 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
433 return ret;
434}
435
Dan Williams822ac032009-05-22 20:07:14 -0400436static int lbs_adhoc_post(struct lbs_private *priv,
437 struct cmd_ds_802_11_ad_hoc_result *resp)
438{
439 int ret = 0;
440 u16 command = le16_to_cpu(resp->hdr.command);
441 u16 result = le16_to_cpu(resp->hdr.result);
442 union iwreq_data wrqu;
443 struct bss_descriptor *bss;
444 DECLARE_SSID_BUF(ssid);
445
446 lbs_deb_enter(LBS_DEB_JOIN);
447
448 if (!priv->in_progress_assoc_req) {
449 lbs_deb_join("ADHOC_RESP: no in-progress association "
450 "request\n");
451 ret = -1;
452 goto done;
453 }
454 bss = &priv->in_progress_assoc_req->bss;
455
456 /*
457 * Join result code 0 --> SUCCESS
458 */
459 if (result) {
460 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
461 if (priv->connect_status == LBS_CONNECTED)
462 lbs_mac_event_disconnected(priv);
463 ret = -1;
464 goto done;
465 }
466
467 /* Send a Media Connected event, according to the Spec */
468 priv->connect_status = LBS_CONNECTED;
469
470 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
471 /* Update the created network descriptor with the new BSSID */
472 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
473 }
474
475 /* Set the BSSID from the joined/started descriptor */
476 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
477
478 /* Set the new SSID to current SSID */
479 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
480 priv->curbssparams.ssid_len = bss->ssid_len;
481
482 netif_carrier_on(priv->dev);
483 if (!priv->tx_pending_len)
484 netif_wake_queue(priv->dev);
485
486 memset(&wrqu, 0, sizeof(wrqu));
487 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
488 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
489 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
490
491 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
492 print_ssid(ssid, bss->ssid, bss->ssid_len),
493 priv->curbssparams.bssid,
494 priv->curbssparams.channel);
495
496done:
497 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
498 return ret;
499}
500
Holger Schurig697900a2008-04-02 16:27:10 +0200501/**
502 * @brief Join an adhoc network found in a previous scan
503 *
504 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400505 * @param assoc_req The association request describing the BSS to join
Holger Schurig697900a2008-04-02 16:27:10 +0200506 *
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400507 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200508 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400509static int lbs_adhoc_join(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200510 struct assoc_request *assoc_req)
511{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400512 struct cmd_ds_802_11_ad_hoc_join cmd;
Holger Schurig697900a2008-04-02 16:27:10 +0200513 struct bss_descriptor *bss = &assoc_req->bss;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400514 u8 preamble = RADIO_PREAMBLE_LONG;
John W. Linville9387b7c2008-09-30 20:59:05 -0400515 DECLARE_SSID_BUF(ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400516 u16 ratesize = 0;
517 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400518
519 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200520
521 lbs_deb_join("current SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400522 print_ssid(ssid, priv->curbssparams.ssid,
Holger Schurig697900a2008-04-02 16:27:10 +0200523 priv->curbssparams.ssid_len),
524 priv->curbssparams.ssid_len);
525 lbs_deb_join("requested ssid '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400526 print_ssid(ssid, bss->ssid, bss->ssid_len),
Holger Schurig697900a2008-04-02 16:27:10 +0200527 bss->ssid_len);
528
529 /* check if the requested SSID is already joined */
530 if (priv->curbssparams.ssid_len &&
531 !lbs_ssid_cmp(priv->curbssparams.ssid,
532 priv->curbssparams.ssid_len,
533 bss->ssid, bss->ssid_len) &&
534 (priv->mode == IW_MODE_ADHOC) &&
535 (priv->connect_status == LBS_CONNECTED)) {
536 union iwreq_data wrqu;
537
538 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
539 "current, not attempting to re-join");
540
541 /* Send the re-association event though, because the association
542 * request really was successful, even if just a null-op.
543 */
544 memset(&wrqu, 0, sizeof(wrqu));
545 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
546 ETH_ALEN);
547 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
548 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
549 goto out;
550 }
551
Dan Williamsd5db2df2008-08-21 17:51:07 -0400552 /* Use short preamble only when both the BSS and firmware support it */
553 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
554 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig697900a2008-04-02 16:27:10 +0200555 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400556 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200557 }
558
Dan Williamsd5db2df2008-08-21 17:51:07 -0400559 ret = lbs_set_radio(priv, preamble, 1);
560 if (ret)
561 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200562
563 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
564 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
565
566 priv->adhoccreate = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400567 priv->curbssparams.channel = bss->channel;
Holger Schurig697900a2008-04-02 16:27:10 +0200568
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400569 /* Build the join command */
570 memset(&cmd, 0, sizeof(cmd));
571 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
572
573 cmd.bss.type = CMD_BSS_TYPE_IBSS;
574 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
575
576 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
577 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
578
Dan Williams5fd164e2009-05-22 20:01:21 -0400579 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400580
Dan Williams5fd164e2009-05-22 20:01:21 -0400581 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
582 sizeof(struct ieee_ie_ibss_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400583
584 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
585 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
586 bss->capability, CAPINFO_MASK);
587
588 /* information on BSSID descriptor passed to FW */
Johannes Berge1749612008-10-27 15:59:26 -0700589 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
590 cmd.bss.bssid, cmd.bss.ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400591
592 /* Only v8 and below support setting these */
593 if (priv->fwrelease < 0x09000000) {
594 /* failtimeout */
595 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
596 /* probedelay */
597 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
598 }
599
600 /* Copy Data rates from the rates recorded in scan response */
601 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
David S. Miller97992182009-08-12 17:37:52 -0700602 ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400603 memcpy(cmd.bss.rates, bss->rates, ratesize);
604 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
605 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
606 ret = -1;
607 goto out;
608 }
609
610 /* Copy the ad-hoc creation rates into Current BSS state structure */
611 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
612 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
613
614 /* Set MSB on basic rates as the firmware requires, but _after_
615 * copying to current bss rates.
616 */
617 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
618
Dan Williams5fd164e2009-05-22 20:01:21 -0400619 cmd.bss.ibss.atimwindow = bss->atimwindow;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400620
621 if (assoc_req->secinfo.wep_enabled) {
622 u16 tmp = le16_to_cpu(cmd.bss.capability);
623 tmp |= WLAN_CAPABILITY_PRIVACY;
624 cmd.bss.capability = cpu_to_le16(tmp);
625 }
626
627 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
628 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
629
630 /* wake up first */
631 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
632 CMD_ACT_SET, 0, 0,
633 &local_ps_mode);
634 if (ret) {
635 ret = -1;
636 goto out;
637 }
638 }
639
640 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
641 ret = -1;
642 goto out;
643 }
644
645 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
Dan Williams822ac032009-05-22 20:07:14 -0400646 if (ret == 0) {
647 ret = lbs_adhoc_post(priv,
648 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
649 }
Holger Schurig697900a2008-04-02 16:27:10 +0200650
651out:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400652 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200653 return ret;
654}
655
656/**
657 * @brief Start an Adhoc Network
658 *
659 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400660 * @param assoc_req The association request describing the BSS to start
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400661 *
662 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200663 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400664static int lbs_adhoc_start(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200665 struct assoc_request *assoc_req)
666{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400667 struct cmd_ds_802_11_ad_hoc_start cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400668 u8 preamble = RADIO_PREAMBLE_LONG;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400669 size_t ratesize = 0;
670 u16 tmpcap = 0;
671 int ret = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400672 DECLARE_SSID_BUF(ssid);
Dan Williamsd5db2df2008-08-21 17:51:07 -0400673
674 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200675
Holger Schurig697900a2008-04-02 16:27:10 +0200676 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400677 lbs_deb_join("ADHOC_START: Will use short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400678 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200679 }
680
Dan Williamsd5db2df2008-08-21 17:51:07 -0400681 ret = lbs_set_radio(priv, preamble, 1);
682 if (ret)
683 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200684
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400685 /* Build the start command */
686 memset(&cmd, 0, sizeof(cmd));
687 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurig697900a2008-04-02 16:27:10 +0200688
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400689 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
690
691 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400692 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400693 assoc_req->ssid_len);
694
695 cmd.bsstype = CMD_BSS_TYPE_IBSS;
696
697 if (priv->beacon_period == 0)
698 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
699 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
700
701 WARN_ON(!assoc_req->channel);
702
703 /* set Physical parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400704 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
705 cmd.ds.header.len = 1;
Dan Williams5fd164e2009-05-22 20:01:21 -0400706 cmd.ds.channel = assoc_req->channel;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400707
708 /* set IBSS parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400709 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
710 cmd.ibss.header.len = 2;
Dan Williams5fd164e2009-05-22 20:01:21 -0400711 cmd.ibss.atimwindow = cpu_to_le16(0);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400712
713 /* set capability info */
714 tmpcap = WLAN_CAPABILITY_IBSS;
Dan Williams2fa7a982009-05-22 20:09:58 -0400715 if (assoc_req->secinfo.wep_enabled ||
716 assoc_req->secinfo.WPAenabled ||
717 assoc_req->secinfo.WPA2enabled) {
718 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400719 tmpcap |= WLAN_CAPABILITY_PRIVACY;
720 } else
Dan Williams2fa7a982009-05-22 20:09:58 -0400721 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400722
723 cmd.capability = cpu_to_le16(tmpcap);
724
725 /* Only v8 and below support setting probe delay */
726 if (priv->fwrelease < 0x09000000)
727 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
728
729 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
730 memcpy(cmd.rates, lbs_bg_rates, ratesize);
731
732 /* Copy the ad-hoc creating rates into Current BSS state structure */
733 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
734 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
735
736 /* Set MSB on basic rates as the firmware requires, but _after_
737 * copying to current bss rates.
738 */
739 lbs_set_basic_rate_flags(cmd.rates, ratesize);
740
741 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
742 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
743
744 if (lbs_create_dnld_countryinfo_11d(priv)) {
745 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
746 ret = -1;
747 goto out;
748 }
749
750 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
751 assoc_req->channel, assoc_req->band);
752
753 priv->adhoccreate = 1;
754 priv->mode = IW_MODE_ADHOC;
755
756 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
757 if (ret == 0)
Dan Williams822ac032009-05-22 20:07:14 -0400758 ret = lbs_adhoc_post(priv,
759 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200760
Dan Williamsd5db2df2008-08-21 17:51:07 -0400761out:
762 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200763 return ret;
764}
765
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400766/**
767 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
768 *
769 * @param priv A pointer to struct lbs_private structure
770 * @return 0 on success, or an error
771 */
772int lbs_adhoc_stop(struct lbs_private *priv)
Holger Schurig697900a2008-04-02 16:27:10 +0200773{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400774 struct cmd_ds_802_11_ad_hoc_stop cmd;
775 int ret;
776
777 lbs_deb_enter(LBS_DEB_JOIN);
778
779 memset(&cmd, 0, sizeof (cmd));
780 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
781
782 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
783
784 /* Clean up everything even if there was an error */
785 lbs_mac_event_disconnected(priv);
786
787 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
788 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +0200789}
Dan Williamse76850d2007-05-25 17:09:41 -0400790
Holger Schurig245bf202008-04-02 16:27:42 +0200791static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
792 struct bss_descriptor *match_bss)
793{
794 if (!secinfo->wep_enabled && !secinfo->WPAenabled
795 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100796 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
797 && match_bss->rsn_ie[0] != WLAN_EID_RSN
Holger Schurig245bf202008-04-02 16:27:42 +0200798 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
799 return 1;
800 else
801 return 0;
802}
803
804static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
805 struct bss_descriptor *match_bss)
806{
807 if (secinfo->wep_enabled && !secinfo->WPAenabled
808 && !secinfo->WPA2enabled
809 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
810 return 1;
811 else
812 return 0;
813}
814
815static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
816 struct bss_descriptor *match_bss)
817{
818 if (!secinfo->wep_enabled && secinfo->WPAenabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100819 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
Holger Schurig245bf202008-04-02 16:27:42 +0200820 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
821 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
822 )
823 return 1;
824 else
825 return 0;
826}
827
828static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
829 struct bss_descriptor *match_bss)
830{
831 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
Johannes Berg2c7060022008-10-30 22:09:54 +0100832 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200833 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
834 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
835 )
836 return 1;
837 else
838 return 0;
839}
840
841static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
842 struct bss_descriptor *match_bss)
843{
844 if (!secinfo->wep_enabled && !secinfo->WPAenabled
845 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100846 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
847 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200848 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
849 return 1;
850 else
851 return 0;
852}
853
854/**
855 * @brief Check if a scanned network compatible with the driver settings
856 *
857 * WEP WPA WPA2 ad-hoc encrypt Network
858 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
859 * 0 0 0 0 NONE 0 0 0 yes No security
860 * 1 0 0 0 NONE 1 0 0 yes Static WEP
861 * 0 1 0 0 x 1x 1 x yes WPA
862 * 0 0 1 0 x 1x x 1 yes WPA2
863 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
864 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
865 *
866 *
867 * @param priv A pointer to struct lbs_private
868 * @param index Index in scantable to check against current driver settings
869 * @param mode Network mode: Infrastructure or IBSS
870 *
871 * @return Index in scantable, or error code if negative
872 */
873static int is_network_compatible(struct lbs_private *priv,
874 struct bss_descriptor *bss, uint8_t mode)
875{
876 int matched = 0;
877
878 lbs_deb_enter(LBS_DEB_SCAN);
879
880 if (bss->mode != mode)
881 goto done;
882
883 matched = match_bss_no_security(&priv->secinfo, bss);
884 if (matched)
885 goto done;
886 matched = match_bss_static_wep(&priv->secinfo, bss);
887 if (matched)
888 goto done;
889 matched = match_bss_wpa(&priv->secinfo, bss);
890 if (matched) {
891 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
892 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
893 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
894 priv->secinfo.wep_enabled ? "e" : "d",
895 priv->secinfo.WPAenabled ? "e" : "d",
896 priv->secinfo.WPA2enabled ? "e" : "d",
897 (bss->capability & WLAN_CAPABILITY_PRIVACY));
898 goto done;
899 }
900 matched = match_bss_wpa2(&priv->secinfo, bss);
901 if (matched) {
902 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
903 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
904 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
905 priv->secinfo.wep_enabled ? "e" : "d",
906 priv->secinfo.WPAenabled ? "e" : "d",
907 priv->secinfo.WPA2enabled ? "e" : "d",
908 (bss->capability & WLAN_CAPABILITY_PRIVACY));
909 goto done;
910 }
911 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
912 if (matched) {
913 lbs_deb_scan("is_network_compatible() dynamic WEP: "
914 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
915 bss->wpa_ie[0], bss->rsn_ie[0],
916 (bss->capability & WLAN_CAPABILITY_PRIVACY));
917 goto done;
918 }
919
920 /* bss security settings don't match those configured on card */
921 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
922 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
923 bss->wpa_ie[0], bss->rsn_ie[0],
924 priv->secinfo.wep_enabled ? "e" : "d",
925 priv->secinfo.WPAenabled ? "e" : "d",
926 priv->secinfo.WPA2enabled ? "e" : "d",
927 (bss->capability & WLAN_CAPABILITY_PRIVACY));
928
929done:
930 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
931 return matched;
932}
933
934/**
935 * @brief This function finds a specific compatible BSSID in the scan list
936 *
937 * Used in association code
938 *
939 * @param priv A pointer to struct lbs_private
940 * @param bssid BSSID to find in the scan list
941 * @param mode Network mode: Infrastructure or IBSS
942 *
943 * @return index in BSSID list, or error return code (< 0)
944 */
945static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
946 uint8_t *bssid, uint8_t mode)
947{
948 struct bss_descriptor *iter_bss;
949 struct bss_descriptor *found_bss = NULL;
950
951 lbs_deb_enter(LBS_DEB_SCAN);
952
953 if (!bssid)
954 goto out;
955
956 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
957
958 /* Look through the scan table for a compatible match. The loop will
959 * continue past a matched bssid that is not compatible in case there
960 * is an AP with multiple SSIDs assigned to the same BSSID
961 */
962 mutex_lock(&priv->lock);
963 list_for_each_entry(iter_bss, &priv->network_list, list) {
964 if (compare_ether_addr(iter_bss->bssid, bssid))
965 continue; /* bssid doesn't match */
966 switch (mode) {
967 case IW_MODE_INFRA:
968 case IW_MODE_ADHOC:
969 if (!is_network_compatible(priv, iter_bss, mode))
970 break;
971 found_bss = iter_bss;
972 break;
973 default:
974 found_bss = iter_bss;
975 break;
976 }
977 }
978 mutex_unlock(&priv->lock);
979
980out:
981 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
982 return found_bss;
983}
984
985/**
986 * @brief This function finds ssid in ssid list.
987 *
988 * Used in association code
989 *
990 * @param priv A pointer to struct lbs_private
991 * @param ssid SSID to find in the list
992 * @param bssid BSSID to qualify the SSID selection (if provided)
993 * @param mode Network mode: Infrastructure or IBSS
994 *
995 * @return index in BSSID list
996 */
997static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
998 uint8_t *ssid, uint8_t ssid_len,
999 uint8_t *bssid, uint8_t mode,
1000 int channel)
1001{
1002 u32 bestrssi = 0;
1003 struct bss_descriptor *iter_bss = NULL;
1004 struct bss_descriptor *found_bss = NULL;
1005 struct bss_descriptor *tmp_oldest = NULL;
1006
1007 lbs_deb_enter(LBS_DEB_SCAN);
1008
1009 mutex_lock(&priv->lock);
1010
1011 list_for_each_entry(iter_bss, &priv->network_list, list) {
1012 if (!tmp_oldest ||
1013 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1014 tmp_oldest = iter_bss;
1015
1016 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1017 ssid, ssid_len) != 0)
1018 continue; /* ssid doesn't match */
1019 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1020 continue; /* bssid doesn't match */
1021 if ((channel > 0) && (iter_bss->channel != channel))
1022 continue; /* channel doesn't match */
1023
1024 switch (mode) {
1025 case IW_MODE_INFRA:
1026 case IW_MODE_ADHOC:
1027 if (!is_network_compatible(priv, iter_bss, mode))
1028 break;
1029
1030 if (bssid) {
1031 /* Found requested BSSID */
1032 found_bss = iter_bss;
1033 goto out;
1034 }
1035
1036 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1037 bestrssi = SCAN_RSSI(iter_bss->rssi);
1038 found_bss = iter_bss;
1039 }
1040 break;
1041 case IW_MODE_AUTO:
1042 default:
1043 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1044 bestrssi = SCAN_RSSI(iter_bss->rssi);
1045 found_bss = iter_bss;
1046 }
1047 break;
1048 }
1049 }
1050
1051out:
1052 mutex_unlock(&priv->lock);
1053 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1054 return found_bss;
1055}
1056
Holger Schurig69f90322007-11-23 15:43:44 +01001057static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001058 struct assoc_request * assoc_req)
1059{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001060 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001061 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001062 int channel = -1;
John W. Linville9387b7c2008-09-30 20:59:05 -04001063 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001064
Holger Schurig9012b282007-05-25 11:27:16 -04001065 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001066
Dan Williamsef9a2642007-05-25 16:46:33 -04001067 /* FIXME: take channel into account when picking SSIDs if a channel
1068 * is set.
1069 */
1070
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001071 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1072 channel = assoc_req->channel;
1073
Holger Schurig0765af42007-10-15 12:55:56 +02001074 lbs_deb_assoc("SSID '%s' requested\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001075 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -04001076 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -05001077 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001078 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001079
David Woodhouseaa21c002007-12-08 20:04:36 +00001080 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001081 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001082 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -04001083 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams822ac032009-05-22 20:07:14 -04001084 ret = lbs_try_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001085 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -04001086 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001087 }
Dan Williams0dc5a292007-05-10 22:58:02 -04001088 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001089 /* Scan for the network, do not save previous results. Stale
1090 * scan data will cause us to join a non-existant adhoc network
1091 */
Holger Schurig10078322007-11-15 18:05:47 -05001092 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001093 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001094
1095 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001096 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001097 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001098 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -04001099 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001100 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001101 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001102 } else {
1103 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -04001104 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001105 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001106 IW_ESSID_MAX_SIZE);
1107 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001108 lbs_adhoc_start(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001109 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001110 }
1111
Holger Schurig9012b282007-05-25 11:27:16 -04001112 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001113 return ret;
1114}
1115
1116
Holger Schurig69f90322007-11-23 15:43:44 +01001117static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001118 struct assoc_request * assoc_req)
1119{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001120 int ret = 0;
1121 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001122
Johannes Berge1749612008-10-27 15:59:26 -07001123 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001124
1125 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +00001126 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001127 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001128 if (bss == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -07001129 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1130 "cannot associate.\n", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001131 goto out;
1132 }
1133
Dan Williamse76850d2007-05-25 17:09:41 -04001134 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -04001135 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williams822ac032009-05-22 20:07:14 -04001136 ret = lbs_try_associate(priv, assoc_req);
1137 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1138 ret);
Dan Williams0dc5a292007-05-10 22:58:02 -04001139 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001140 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001141 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001142
1143out:
Holger Schurig9012b282007-05-25 11:27:16 -04001144 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001145 return ret;
1146}
1147
1148
Holger Schurig69f90322007-11-23 15:43:44 +01001149static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001150 struct assoc_request * assoc_req)
1151{
1152 int ret = 0, done = 0;
1153
Holger Schurig0765af42007-10-15 12:55:56 +02001154 lbs_deb_enter(LBS_DEB_ASSOC);
1155
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001156 /* If we're given and 'any' BSSID, try associating based on SSID */
1157
1158 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001159 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1160 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001161 ret = assoc_helper_bssid(priv, assoc_req);
1162 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001163 }
1164 }
1165
1166 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1167 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001168 }
1169
Holger Schurig0765af42007-10-15 12:55:56 +02001170 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171 return ret;
1172}
1173
1174
Holger Schurig69f90322007-11-23 15:43:44 +01001175static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001176 struct assoc_request * assoc_req)
1177{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001178 int ret = 0;
1179
Holger Schurig9012b282007-05-25 11:27:16 -04001180 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001181
David Woodhouseaa21c002007-12-08 20:04:36 +00001182 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -04001183 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001184
Dan Williams0dc5a292007-05-10 22:58:02 -04001185 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001186 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -05001187 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +00001188 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001189 }
1190
David Woodhouseaa21c002007-12-08 20:04:36 +00001191 priv->mode = assoc_req->mode;
Dan Williams39fcf7a2008-09-10 12:49:00 -04001192 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001193
Holger Schurig9012b282007-05-25 11:27:16 -04001194done:
1195 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001196 return ret;
1197}
1198
Holger Schurig69f90322007-11-23 15:43:44 +01001199static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -04001200 struct assoc_request * assoc_req)
1201{
Dan Williamsef9a2642007-05-25 16:46:33 -04001202 int ret = 0;
1203
1204 lbs_deb_enter(LBS_DEB_ASSOC);
1205
David Woodhouse9f462572007-12-12 22:50:21 -05001206 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001207 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001208 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001209 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -04001210 }
1211
David Woodhouseaa21c002007-12-08 20:04:36 +00001212 if (assoc_req->channel == priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001213 goto done;
1214
David Woodhouse8642f1f2007-12-11 20:03:01 -05001215 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -05001216 /* Change mesh channel first; 21.p21 firmware won't let
1217 you change channel otherwise (even though it'll return
1218 an error to this */
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001219 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1220 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001221 }
1222
Dan Williamsef9a2642007-05-25 16:46:33 -04001223 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
David Woodhouse86062132007-12-13 00:32:36 -05001224 priv->curbssparams.channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -04001225
Dan Williams2dd4b262007-12-11 16:54:15 -05001226 ret = lbs_set_channel(priv, assoc_req->channel);
1227 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -05001228 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -04001229
Dan Williams2dd4b262007-12-11 16:54:15 -05001230 /* FIXME: shouldn't need to grab the channel _again_ after setting
1231 * it since the firmware is supposed to return the new channel, but
1232 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -05001233 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001234 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001235 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001236 goto done;
1237 }
Dan Williamsef9a2642007-05-25 16:46:33 -04001238
David Woodhouseaa21c002007-12-08 20:04:36 +00001239 if (assoc_req->channel != priv->curbssparams.channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -05001240 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -04001241 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001242 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -04001243 }
1244
1245 if ( assoc_req->secinfo.wep_enabled
1246 && (assoc_req->wep_keys[0].len
1247 || assoc_req->wep_keys[1].len
1248 || assoc_req->wep_keys[2].len
1249 || assoc_req->wep_keys[3].len)) {
1250 /* Make sure WEP keys are re-sent to firmware */
1251 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1252 }
1253
1254 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -05001255 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -04001256
David Woodhouse8642f1f2007-12-11 20:03:01 -05001257 restore_mesh:
1258 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001259 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1260 priv->curbssparams.channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001261
1262 done:
Dan Williamsef9a2642007-05-25 16:46:33 -04001263 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1264 return ret;
1265}
1266
1267
Holger Schurig69f90322007-11-23 15:43:44 +01001268static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -05001269 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001270{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001271 int i;
1272 int ret = 0;
1273
Holger Schurig9012b282007-05-25 11:27:16 -04001274 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001275
1276 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -05001277 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1278 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1279 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1280 else
1281 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001282
1283 if (ret)
1284 goto out;
1285
1286 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -04001287 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +01001288 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001289 else
Holger Schurigd9e97782008-03-12 16:06:43 +01001290 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -05001291
Holger Schurigc97329e2008-03-18 11:20:21 +01001292 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001293
David Woodhouseaa21c002007-12-08 20:04:36 +00001294 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001295
David Woodhouseaa21c002007-12-08 20:04:36 +00001296 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001297 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001298 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -05001299 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001300 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001301 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001302
David Woodhouseaa21c002007-12-08 20:04:36 +00001303 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001304
1305out:
Holger Schurig9012b282007-05-25 11:27:16 -04001306 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001307 return ret;
1308}
1309
Holger Schurig69f90322007-11-23 15:43:44 +01001310static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001311 struct assoc_request * assoc_req)
1312{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001313 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -05001314 uint16_t do_wpa;
1315 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001316
Holger Schurig9012b282007-05-25 11:27:16 -04001317 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001318
David Woodhouseaa21c002007-12-08 20:04:36 +00001319 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001320 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001321
Holger Schurigc97329e2008-03-18 11:20:21 +01001322 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001323
Dan Williams18c96c342007-06-18 12:01:12 -04001324 /* If RSN is already enabled, don't try to enable it again, since
1325 * ENABLE_RSN resets internal state machines and will clobber the
1326 * 4-way WPA handshake.
1327 */
1328
1329 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001330 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -04001331 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001332 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -04001333 goto out;
1334 }
1335
1336 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001337 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -04001338 if (do_wpa == rsn)
1339 goto out;
1340
1341 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001342 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -04001343
1344out:
Holger Schurig9012b282007-05-25 11:27:16 -04001345 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001346 return ret;
1347}
1348
1349
Holger Schurig69f90322007-11-23 15:43:44 +01001350static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001351 struct assoc_request * assoc_req)
1352{
1353 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -04001354 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001355
Holger Schurig9012b282007-05-25 11:27:16 -04001356 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001357
Dan Williams2bcde512007-10-03 10:37:45 -04001358 /* Work around older firmware bug where WPA unicast and multicast
1359 * keys must be set independently. Seen in SDIO parts with firmware
1360 * version 5.0.11p0.
1361 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001362
Dan Williams2bcde512007-10-03 10:37:45 -04001363 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1364 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +01001365 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001366 assoc_req->flags = flags;
1367 }
1368
1369 if (ret)
1370 goto out;
1371
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001372 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1373 sizeof(struct enc_key));
1374
Dan Williams2bcde512007-10-03 10:37:45 -04001375 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1376 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1377
David Woodhouse9e1228d2008-03-03 12:15:39 +01001378 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001379 assoc_req->flags = flags;
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001380
1381 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1382 sizeof(struct enc_key));
Dan Williams2bcde512007-10-03 10:37:45 -04001383 }
1384
1385out:
Holger Schurig9012b282007-05-25 11:27:16 -04001386 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001387 return ret;
1388}
1389
1390
Holger Schurig69f90322007-11-23 15:43:44 +01001391static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001392 struct assoc_request * assoc_req)
1393{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001394 int ret = 0;
1395
Holger Schurig9012b282007-05-25 11:27:16 -04001396 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001397
1398 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001399 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1400 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001401 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +00001402 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1403 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001404 }
1405
Holger Schurig9012b282007-05-25 11:27:16 -04001406 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001407 return ret;
1408}
1409
1410
David Woodhouseaa21c002007-12-08 20:04:36 +00001411static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001412 struct assoc_request * assoc_req)
1413{
Holger Schurig0765af42007-10-15 12:55:56 +02001414 int ret = 0;
1415
David Woodhouseaa21c002007-12-08 20:04:36 +00001416 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001417 return 0;
1418
Holger Schurig52507c22008-01-28 17:25:53 +01001419 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001420 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001421 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1422 ret = 1;
1423 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001424 }
1425
1426 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001427 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +02001428 lbs_deb_assoc("Deauthenticating due to new security\n");
1429 ret = 1;
1430 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001431 }
1432 }
1433
1434 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001435 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1436 ret = 1;
1437 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001438 }
1439
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001440 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001441 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1442 ret = 1;
1443 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001444 }
1445
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001446 /* FIXME: deal with 'auto' mode somehow */
1447 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001448 if (assoc_req->mode != IW_MODE_INFRA) {
1449 lbs_deb_assoc("Deauthenticating due to leaving "
1450 "infra mode\n");
1451 ret = 1;
1452 goto out;
1453 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001454 }
1455
Holger Schurig0765af42007-10-15 12:55:56 +02001456out:
1457 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +01001458 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001459}
1460
1461
David Woodhouseaa21c002007-12-08 20:04:36 +00001462static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001463 struct assoc_request * assoc_req)
1464{
Holger Schurig0765af42007-10-15 12:55:56 +02001465 lbs_deb_enter(LBS_DEB_ASSOC);
1466
David Woodhouseaa21c002007-12-08 20:04:36 +00001467 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001468 return 0;
1469
David Woodhouseaa21c002007-12-08 20:04:36 +00001470 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1471 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001472 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001473 return 1;
1474
1475 /* FIXME: deal with 'auto' mode somehow */
1476 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001477 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001478 return 1;
1479 }
1480
Dan Williamsef9a2642007-05-25 16:46:33 -04001481 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001482 if (assoc_req->channel != priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001483 return 1;
1484 }
1485
Holger Schurig0765af42007-10-15 12:55:56 +02001486 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001487 return 0;
1488}
1489
1490
Holger Schurig245bf202008-04-02 16:27:42 +02001491/**
1492 * @brief This function finds the best SSID in the Scan List
1493 *
1494 * Search the scan table for the best SSID that also matches the current
1495 * adapter network preference (infrastructure or adhoc)
1496 *
1497 * @param priv A pointer to struct lbs_private
1498 *
1499 * @return index in BSSID list
1500 */
1501static struct bss_descriptor *lbs_find_best_ssid_in_list(
1502 struct lbs_private *priv, uint8_t mode)
1503{
1504 uint8_t bestrssi = 0;
1505 struct bss_descriptor *iter_bss;
1506 struct bss_descriptor *best_bss = NULL;
1507
1508 lbs_deb_enter(LBS_DEB_SCAN);
1509
1510 mutex_lock(&priv->lock);
1511
1512 list_for_each_entry(iter_bss, &priv->network_list, list) {
1513 switch (mode) {
1514 case IW_MODE_INFRA:
1515 case IW_MODE_ADHOC:
1516 if (!is_network_compatible(priv, iter_bss, mode))
1517 break;
1518 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1519 break;
1520 bestrssi = SCAN_RSSI(iter_bss->rssi);
1521 best_bss = iter_bss;
1522 break;
1523 case IW_MODE_AUTO:
1524 default:
1525 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1526 break;
1527 bestrssi = SCAN_RSSI(iter_bss->rssi);
1528 best_bss = iter_bss;
1529 break;
1530 }
1531 }
1532
1533 mutex_unlock(&priv->lock);
1534 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1535 return best_bss;
1536}
1537
1538/**
1539 * @brief Find the best AP
1540 *
1541 * Used from association worker.
1542 *
1543 * @param priv A pointer to struct lbs_private structure
1544 * @param pSSID A pointer to AP's ssid
1545 *
1546 * @return 0--success, otherwise--fail
1547 */
1548static int lbs_find_best_network_ssid(struct lbs_private *priv,
1549 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1550 uint8_t *out_mode)
1551{
1552 int ret = -1;
1553 struct bss_descriptor *found;
1554
1555 lbs_deb_enter(LBS_DEB_SCAN);
1556
1557 priv->scan_ssid_len = 0;
1558 lbs_scan_networks(priv, 1);
1559 if (priv->surpriseremoved)
1560 goto out;
1561
1562 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1563 if (found && (found->ssid_len > 0)) {
1564 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1565 *out_ssid_len = found->ssid_len;
1566 *out_mode = found->mode;
1567 ret = 0;
1568 }
1569
1570out:
1571 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1572 return ret;
1573}
1574
1575
Holger Schurig10078322007-11-15 18:05:47 -05001576void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001577{
Holger Schurig69f90322007-11-23 15:43:44 +01001578 struct lbs_private *priv = container_of(work, struct lbs_private,
1579 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001580 struct assoc_request * assoc_req = NULL;
1581 int ret = 0;
1582 int find_any_ssid = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001583 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001584
Holger Schurig9012b282007-05-25 11:27:16 -04001585 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001586
David Woodhouseaa21c002007-12-08 20:04:36 +00001587 mutex_lock(&priv->lock);
1588 assoc_req = priv->pending_assoc_req;
1589 priv->pending_assoc_req = NULL;
1590 priv->in_progress_assoc_req = assoc_req;
1591 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001592
Holger Schurig9012b282007-05-25 11:27:16 -04001593 if (!assoc_req)
1594 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001595
Holger Schurig0765af42007-10-15 12:55:56 +02001596 lbs_deb_assoc(
1597 "Association Request:\n"
1598 " flags: 0x%08lx\n"
1599 " SSID: '%s'\n"
1600 " chann: %d\n"
1601 " band: %d\n"
1602 " mode: %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001603 " BSSID: %pM\n"
Holger Schurig0765af42007-10-15 12:55:56 +02001604 " secinfo: %s%s%s\n"
1605 " auth_mode: %d\n",
1606 assoc_req->flags,
John W. Linville9387b7c2008-09-30 20:59:05 -04001607 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Holger Schurig0765af42007-10-15 12:55:56 +02001608 assoc_req->channel, assoc_req->band, assoc_req->mode,
Johannes Berge1749612008-10-27 15:59:26 -07001609 assoc_req->bssid,
Holger Schurig0765af42007-10-15 12:55:56 +02001610 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1611 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1612 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1613 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001614
1615 /* If 'any' SSID was specified, find an SSID to associate with */
1616 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001617 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001618 find_any_ssid = 1;
1619
1620 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1621 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001622 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1623 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001624 find_any_ssid = 0;
1625 }
1626
1627 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02001628 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001629
Holger Schurig10078322007-11-15 18:05:47 -05001630 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001631 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001632 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001633 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001634 ret = -ENETUNREACH;
1635 goto out;
1636 }
1637
1638 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04001639 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001640 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1641 assoc_req->mode = new_mode;
1642 }
1643 }
1644
1645 /*
1646 * Check if the attributes being changing require deauthentication
1647 * from the currently associated infrastructure access point.
1648 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001649 if (priv->mode == IW_MODE_INFRA) {
1650 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04001651 ret = lbs_cmd_80211_deauthenticate(priv,
1652 priv->curbssparams.bssid,
1653 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001654 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001655 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001656 "configuration request failed: %d\n",
1657 ret);
1658 }
1659 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001660 } else if (priv->mode == IW_MODE_ADHOC) {
1661 if (should_stop_adhoc(priv, assoc_req)) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001662 ret = lbs_adhoc_stop(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001663 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001664 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001665 "new configuration request failed: %d\n",
1666 ret);
1667 }
1668
1669 }
1670 }
1671
1672 /* Send the various configuration bits to the firmware */
1673 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1674 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001675 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001676 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001677 }
1678
Dan Williamsef9a2642007-05-25 16:46:33 -04001679 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1680 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001681 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04001682 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04001683 }
1684
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001685 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1686 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1687 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001688 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001689 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001690 }
1691
1692 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1693 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001694 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001695 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001696 }
1697
1698 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1699 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001700 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001701 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001702 }
1703
1704 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1705 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1706 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001707 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001708 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001709 }
1710
1711 /* SSID/BSSID should be the _last_ config option set, because they
1712 * trigger the association attempt.
1713 */
1714 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1715 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1716 int success = 1;
1717
1718 ret = assoc_helper_associate(priv, assoc_req);
1719 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01001720 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001721 ret);
1722 success = 0;
1723 }
1724
David Woodhouseaa21c002007-12-08 20:04:36 +00001725 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01001726 lbs_deb_assoc("ASSOC: association unsuccessful, "
1727 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001728 success = 0;
1729 }
1730
1731 if (success) {
Johannes Berge1749612008-10-27 15:59:26 -07001732 lbs_deb_assoc("associated to %pM\n",
1733 priv->curbssparams.bssid);
Holger Schurig10078322007-11-15 18:05:47 -05001734 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04001735 CMD_802_11_RSSI,
1736 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001737 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001738 ret = -1;
1739 }
1740 }
1741
1742out:
1743 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001744 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001745 ret);
1746 }
Dan Williamse76850d2007-05-25 17:09:41 -04001747
David Woodhouseaa21c002007-12-08 20:04:36 +00001748 mutex_lock(&priv->lock);
1749 priv->in_progress_assoc_req = NULL;
1750 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001751 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04001752
1753done:
1754 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001755}
1756
1757
1758/*
1759 * Caller MUST hold any necessary locks
1760 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001761struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001762{
1763 struct assoc_request * assoc_req;
1764
Holger Schurig0765af42007-10-15 12:55:56 +02001765 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00001766 if (!priv->pending_assoc_req) {
1767 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04001768 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00001769 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001770 lbs_pr_info("Not enough memory to allocate association"
1771 " request!\n");
1772 return NULL;
1773 }
1774 }
1775
1776 /* Copy current configuration attributes to the association request,
1777 * but don't overwrite any that are already set.
1778 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001779 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001780 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001781 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001782 IW_ESSID_MAX_SIZE);
David Woodhouseaa21c002007-12-08 20:04:36 +00001783 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001784 }
1785
1786 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001787 assoc_req->channel = priv->curbssparams.channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001788
Dan Williamse76850d2007-05-25 17:09:41 -04001789 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001790 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04001791
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001792 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001793 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001794
1795 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001796 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001797 ETH_ALEN);
1798 }
1799
1800 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1801 int i;
1802 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001803 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04001804 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001805 }
1806 }
1807
1808 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001809 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001810
1811 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001812 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001813 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001814 }
1815
1816 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001817 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001818 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001819 }
1820
1821 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001822 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001823 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001824 }
1825
1826 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001827 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001828 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001829 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001830 }
1831
Holger Schurig0765af42007-10-15 12:55:56 +02001832 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001833 return assoc_req;
1834}
Holger Schurig697900a2008-04-02 16:27:10 +02001835
1836
1837/**
Dan Williams191bb402008-08-21 17:46:18 -04001838 * @brief Deauthenticate from a specific BSS
1839 *
1840 * @param priv A pointer to struct lbs_private structure
1841 * @param bssid The specific BSS to deauthenticate from
1842 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1843 *
1844 * @return 0 on success, error on failure
1845 */
1846int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1847 u16 reason)
Holger Schurig697900a2008-04-02 16:27:10 +02001848{
Dan Williams191bb402008-08-21 17:46:18 -04001849 struct cmd_ds_802_11_deauthenticate cmd;
1850 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001851
1852 lbs_deb_enter(LBS_DEB_JOIN);
1853
Dan Williams191bb402008-08-21 17:46:18 -04001854 memset(&cmd, 0, sizeof(cmd));
1855 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1856 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1857 cmd.reasoncode = cpu_to_le16(reason);
Holger Schurig697900a2008-04-02 16:27:10 +02001858
Dan Williams191bb402008-08-21 17:46:18 -04001859 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001860
Dan Williams191bb402008-08-21 17:46:18 -04001861 /* Clean up everything even if there was an error; can't assume that
1862 * we're still authenticated to the AP after trying to deauth.
1863 */
1864 lbs_mac_event_disconnected(priv);
Holger Schurig697900a2008-04-02 16:27:10 +02001865
1866 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04001867 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001868}
1869