blob: 81f86ef26990be2f703555916dee55bd0c7ffaad [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);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040047 int ret = 0, 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
Andrey Yurovsky6f632d52009-08-13 17:34:40 -070051 tmp = kzalloc((ARRAY_SIZE(lbs_bg_rates) - 1) * (*rates_size - 1),
52 GFP_KERNEL);
53 if (!tmp)
54 return -1;
55
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040056 /* For each rate in card_rates that exists in rate1, copy to tmp */
David S. Miller97992182009-08-12 17:37:52 -070057 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
58 for (j = 0; rates[j] && (j < *rates_size); j++) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040059 if (rates[j] == card_rates[i])
60 tmp[tmp_size++] = card_rates[i];
61 }
62 }
63
64 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
David S. Miller97992182009-08-12 17:37:52 -070065 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040066 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
67 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
68
69 if (!priv->enablehwauto) {
70 for (i = 0; i < tmp_size; i++) {
71 if (tmp[i] == priv->cur_rate)
72 goto done;
73 }
74 lbs_pr_alert("Previously set fixed data rate %#x isn't "
75 "compatible with the network.\n", priv->cur_rate);
76 ret = -1;
David S. Miller97992182009-08-12 17:37:52 -070077 goto done;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040078 }
David S. Miller97992182009-08-12 17:37:52 -070079 ret = 0;
80
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040081done:
82 memset(rates, 0, *rates_size);
83 *rates_size = min_t(int, tmp_size, *rates_size);
84 memcpy(rates, tmp, *rates_size);
Andrey Yurovsky6f632d52009-08-13 17:34:40 -070085 kfree(tmp);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040086 return ret;
87}
88
89
90/**
91 * @brief Sets the MSB on basic rates as the firmware requires
92 *
93 * Scan through an array and set the MSB for basic data rates.
94 *
95 * @param rates buffer of data rates
96 * @param len size of buffer
97 */
98static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
99{
100 int i;
101
102 for (i = 0; i < len; i++) {
103 if (rates[i] == 0x02 || rates[i] == 0x04 ||
104 rates[i] == 0x0b || rates[i] == 0x16)
105 rates[i] |= 0x80;
106 }
107}
108
Holger Schurig697900a2008-04-02 16:27:10 +0200109
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400110static u8 iw_auth_to_ieee_auth(u8 auth)
111{
112 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
113 return 0x00;
114 else if (auth == IW_AUTH_ALG_SHARED_KEY)
115 return 0x01;
116 else if (auth == IW_AUTH_ALG_LEAP)
117 return 0x80;
118
119 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
120 return 0;
121}
122
123/**
124 * @brief This function prepares the authenticate command. AUTHENTICATE only
125 * sets the authentication suite for future associations, as the firmware
126 * handles authentication internally during the ASSOCIATE command.
127 *
128 * @param priv A pointer to struct lbs_private structure
129 * @param bssid The peer BSSID with which to authenticate
130 * @param auth The authentication mode to use (from wireless.h)
131 *
132 * @return 0 or -1
133 */
134static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
135{
136 struct cmd_ds_802_11_authenticate cmd;
137 int ret = -1;
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400138
139 lbs_deb_enter(LBS_DEB_JOIN);
140
141 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
142 memcpy(cmd.bssid, bssid, ETH_ALEN);
143
144 cmd.authtype = iw_auth_to_ieee_auth(auth);
145
Johannes Berge91d8332009-07-15 17:21:41 +0200146 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400147
148 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
149
150 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
151 return ret;
152}
153
Dan Williams822ac032009-05-22 20:07:14 -0400154
155static int lbs_assoc_post(struct lbs_private *priv,
156 struct cmd_ds_802_11_associate_response *resp)
157{
158 int ret = 0;
159 union iwreq_data wrqu;
160 struct bss_descriptor *bss;
161 u16 status_code;
162
163 lbs_deb_enter(LBS_DEB_ASSOC);
164
165 if (!priv->in_progress_assoc_req) {
166 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
167 ret = -1;
168 goto done;
169 }
170 bss = &priv->in_progress_assoc_req->bss;
171
172 /*
173 * Older FW versions map the IEEE 802.11 Status Code in the association
174 * response to the following values returned in resp->statuscode:
175 *
176 * IEEE Status Code Marvell Status Code
177 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
178 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
179 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
180 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
181 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
182 * others -> 0x0003 ASSOC_RESULT_REFUSED
183 *
184 * Other response codes:
185 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
186 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
187 * association response from the AP)
188 */
189
190 status_code = le16_to_cpu(resp->statuscode);
191 if (priv->fwrelease < 0x09000000) {
192 switch (status_code) {
193 case 0x00:
194 break;
195 case 0x01:
196 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
197 break;
198 case 0x02:
199 lbs_deb_assoc("ASSOC_RESP: internal timer "
200 "expired while waiting for the AP\n");
201 break;
202 case 0x03:
203 lbs_deb_assoc("ASSOC_RESP: association "
204 "refused by AP\n");
205 break;
206 case 0x04:
207 lbs_deb_assoc("ASSOC_RESP: authentication "
208 "refused by AP\n");
209 break;
210 default:
211 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
212 " unknown\n", status_code);
213 break;
214 }
215 } else {
216 /* v9+ returns the AP's association response */
217 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
218 }
219
220 if (status_code) {
221 lbs_mac_event_disconnected(priv);
222 ret = -1;
223 goto done;
224 }
225
226 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
227 (void *) (resp + sizeof (resp->hdr)),
228 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
229
230 /* Send a Media Connected event, according to the Spec */
231 priv->connect_status = LBS_CONNECTED;
232
233 /* Update current SSID and BSSID */
234 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
235 priv->curbssparams.ssid_len = bss->ssid_len;
236 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
237
238 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
239 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
240
241 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
242 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
243 priv->nextSNRNF = 0;
244 priv->numSNRNF = 0;
245
246 netif_carrier_on(priv->dev);
247 if (!priv->tx_pending_len)
248 netif_wake_queue(priv->dev);
249
250 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
251 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
252 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
253
254done:
255 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
256 return ret;
257}
258
259/**
260 * @brief This function prepares an association-class command.
261 *
262 * @param priv A pointer to struct lbs_private structure
263 * @param assoc_req The association request describing the BSS to associate
264 * or reassociate with
265 * @param command The actual command, either CMD_802_11_ASSOCIATE or
266 * CMD_802_11_REASSOCIATE
267 *
268 * @return 0 or -1
269 */
270static int lbs_associate(struct lbs_private *priv,
271 struct assoc_request *assoc_req,
272 u16 command)
273{
274 struct cmd_ds_802_11_associate cmd;
275 int ret = 0;
276 struct bss_descriptor *bss = &assoc_req->bss;
277 u8 *pos = &(cmd.iebuf[0]);
278 u16 tmpcap, tmplen, tmpauth;
279 struct mrvl_ie_ssid_param_set *ssid;
280 struct mrvl_ie_ds_param_set *ds;
281 struct mrvl_ie_cf_param_set *cf;
282 struct mrvl_ie_rates_param_set *rates;
283 struct mrvl_ie_rsn_param_set *rsn;
284 struct mrvl_ie_auth_type *auth;
285
286 lbs_deb_enter(LBS_DEB_ASSOC);
287
288 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
289 (command != CMD_802_11_REASSOCIATE));
290
291 memset(&cmd, 0, sizeof(cmd));
292 cmd.hdr.command = cpu_to_le16(command);
293
294 /* Fill in static fields */
295 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
296 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
297
298 /* Capability info */
299 tmpcap = (bss->capability & CAPINFO_MASK);
300 if (bss->mode == IW_MODE_INFRA)
301 tmpcap |= WLAN_CAPABILITY_ESS;
302 cmd.capability = cpu_to_le16(tmpcap);
303 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
304
305 /* SSID */
306 ssid = (struct mrvl_ie_ssid_param_set *) pos;
307 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
308 tmplen = bss->ssid_len;
309 ssid->header.len = cpu_to_le16(tmplen);
310 memcpy(ssid->ssid, bss->ssid, tmplen);
311 pos += sizeof(ssid->header) + tmplen;
312
313 ds = (struct mrvl_ie_ds_param_set *) pos;
314 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
315 ds->header.len = cpu_to_le16(1);
316 ds->channel = bss->phy.ds.channel;
317 pos += sizeof(ds->header) + 1;
318
319 cf = (struct mrvl_ie_cf_param_set *) pos;
320 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
321 tmplen = sizeof(*cf) - sizeof (cf->header);
322 cf->header.len = cpu_to_le16(tmplen);
323 /* IE payload should be zeroed, firmware fills it in for us */
324 pos += sizeof(*cf);
325
326 rates = (struct mrvl_ie_rates_param_set *) pos;
327 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
328 memcpy(&rates->rates, &bss->rates, MAX_RATES);
David S. Miller97992182009-08-12 17:37:52 -0700329 tmplen = MAX_RATES;
Dan Williams822ac032009-05-22 20:07:14 -0400330 if (get_common_rates(priv, rates->rates, &tmplen)) {
331 ret = -1;
332 goto done;
333 }
334 pos += sizeof(rates->header) + tmplen;
335 rates->header.len = cpu_to_le16(tmplen);
336 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
337
338 /* Copy the infra. association rates into Current BSS state structure */
339 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
340 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
341
342 /* Set MSB on basic rates as the firmware requires, but _after_
343 * copying to current bss rates.
344 */
345 lbs_set_basic_rate_flags(rates->rates, tmplen);
346
347 /* Firmware v9+ indicate authentication suites as a TLV */
348 if (priv->fwrelease >= 0x09000000) {
Dan Williams822ac032009-05-22 20:07:14 -0400349 auth = (struct mrvl_ie_auth_type *) pos;
350 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
351 auth->header.len = cpu_to_le16(2);
352 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
353 auth->auth = cpu_to_le16(tmpauth);
354 pos += sizeof(auth->header) + 2;
355
Johannes Berge91d8332009-07-15 17:21:41 +0200356 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
357 bss->bssid, priv->secinfo.auth_mode);
Dan Williams822ac032009-05-22 20:07:14 -0400358 }
359
360 /* WPA/WPA2 IEs */
361 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
362 rsn = (struct mrvl_ie_rsn_param_set *) pos;
363 /* WPA_IE or WPA2_IE */
364 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
365 tmplen = (u16) assoc_req->wpa_ie[1];
366 rsn->header.len = cpu_to_le16(tmplen);
367 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
368 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
369 sizeof(rsn->header) + tmplen);
370 pos += sizeof(rsn->header) + tmplen;
371 }
372
373 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
374 (u16)(pos - (u8 *) &cmd.iebuf));
375
376 /* update curbssparams */
377 priv->curbssparams.channel = bss->phy.ds.channel;
378
379 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
380 ret = -1;
381 goto done;
382 }
383
384 ret = lbs_cmd_with_response(priv, command, &cmd);
385 if (ret == 0) {
386 ret = lbs_assoc_post(priv,
387 (struct cmd_ds_802_11_associate_response *) &cmd);
388 }
389
390done:
391 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
392 return ret;
393}
394
Holger Schurig697900a2008-04-02 16:27:10 +0200395/**
396 * @brief Associate to a specific BSS discovered in a scan
397 *
398 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400399 * @param assoc_req The association request describing the BSS to associate with
Holger Schurig697900a2008-04-02 16:27:10 +0200400 *
401 * @return 0-success, otherwise fail
402 */
Dan Williams822ac032009-05-22 20:07:14 -0400403static int lbs_try_associate(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200404 struct assoc_request *assoc_req)
405{
406 int ret;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400407 u8 preamble = RADIO_PREAMBLE_LONG;
Holger Schurig697900a2008-04-02 16:27:10 +0200408
409 lbs_deb_enter(LBS_DEB_ASSOC);
410
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400411 /* FW v9 and higher indicate authentication suites as a TLV in the
412 * association command, not as a separate authentication command.
413 */
414 if (priv->fwrelease < 0x09000000) {
415 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
416 priv->secinfo.auth_mode);
417 if (ret)
418 goto out;
419 }
Holger Schurig697900a2008-04-02 16:27:10 +0200420
Dan Williamsd5db2df2008-08-21 17:51:07 -0400421 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig697900a2008-04-02 16:27:10 +0200422 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
423 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
Dan Williamsd5db2df2008-08-21 17:51:07 -0400424 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200425
Dan Williamsd5db2df2008-08-21 17:51:07 -0400426 ret = lbs_set_radio(priv, preamble, 1);
427 if (ret)
428 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200429
Dan Williams822ac032009-05-22 20:07:14 -0400430 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
Holger Schurig697900a2008-04-02 16:27:10 +0200431
Dan Williamsd5db2df2008-08-21 17:51:07 -0400432out:
Holger Schurig697900a2008-04-02 16:27:10 +0200433 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
434 return ret;
435}
436
Dan Williams822ac032009-05-22 20:07:14 -0400437static int lbs_adhoc_post(struct lbs_private *priv,
438 struct cmd_ds_802_11_ad_hoc_result *resp)
439{
440 int ret = 0;
441 u16 command = le16_to_cpu(resp->hdr.command);
442 u16 result = le16_to_cpu(resp->hdr.result);
443 union iwreq_data wrqu;
444 struct bss_descriptor *bss;
445 DECLARE_SSID_BUF(ssid);
446
447 lbs_deb_enter(LBS_DEB_JOIN);
448
449 if (!priv->in_progress_assoc_req) {
450 lbs_deb_join("ADHOC_RESP: no in-progress association "
451 "request\n");
452 ret = -1;
453 goto done;
454 }
455 bss = &priv->in_progress_assoc_req->bss;
456
457 /*
458 * Join result code 0 --> SUCCESS
459 */
460 if (result) {
461 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
462 if (priv->connect_status == LBS_CONNECTED)
463 lbs_mac_event_disconnected(priv);
464 ret = -1;
465 goto done;
466 }
467
468 /* Send a Media Connected event, according to the Spec */
469 priv->connect_status = LBS_CONNECTED;
470
471 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
472 /* Update the created network descriptor with the new BSSID */
473 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
474 }
475
476 /* Set the BSSID from the joined/started descriptor */
477 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
478
479 /* Set the new SSID to current SSID */
480 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
481 priv->curbssparams.ssid_len = bss->ssid_len;
482
483 netif_carrier_on(priv->dev);
484 if (!priv->tx_pending_len)
485 netif_wake_queue(priv->dev);
486
487 memset(&wrqu, 0, sizeof(wrqu));
488 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
489 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
490 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
491
492 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
493 print_ssid(ssid, bss->ssid, bss->ssid_len),
494 priv->curbssparams.bssid,
495 priv->curbssparams.channel);
496
497done:
498 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
499 return ret;
500}
501
Holger Schurig697900a2008-04-02 16:27:10 +0200502/**
503 * @brief Join an adhoc network found in a previous scan
504 *
505 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400506 * @param assoc_req The association request describing the BSS to join
Holger Schurig697900a2008-04-02 16:27:10 +0200507 *
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400508 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200509 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400510static int lbs_adhoc_join(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200511 struct assoc_request *assoc_req)
512{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400513 struct cmd_ds_802_11_ad_hoc_join cmd;
Holger Schurig697900a2008-04-02 16:27:10 +0200514 struct bss_descriptor *bss = &assoc_req->bss;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400515 u8 preamble = RADIO_PREAMBLE_LONG;
John W. Linville9387b7c2008-09-30 20:59:05 -0400516 DECLARE_SSID_BUF(ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400517 u16 ratesize = 0;
518 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400519
520 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200521
522 lbs_deb_join("current SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400523 print_ssid(ssid, priv->curbssparams.ssid,
Holger Schurig697900a2008-04-02 16:27:10 +0200524 priv->curbssparams.ssid_len),
525 priv->curbssparams.ssid_len);
526 lbs_deb_join("requested ssid '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400527 print_ssid(ssid, bss->ssid, bss->ssid_len),
Holger Schurig697900a2008-04-02 16:27:10 +0200528 bss->ssid_len);
529
530 /* check if the requested SSID is already joined */
531 if (priv->curbssparams.ssid_len &&
532 !lbs_ssid_cmp(priv->curbssparams.ssid,
533 priv->curbssparams.ssid_len,
534 bss->ssid, bss->ssid_len) &&
535 (priv->mode == IW_MODE_ADHOC) &&
536 (priv->connect_status == LBS_CONNECTED)) {
537 union iwreq_data wrqu;
538
539 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
540 "current, not attempting to re-join");
541
542 /* Send the re-association event though, because the association
543 * request really was successful, even if just a null-op.
544 */
545 memset(&wrqu, 0, sizeof(wrqu));
546 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
547 ETH_ALEN);
548 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
549 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
550 goto out;
551 }
552
Dan Williamsd5db2df2008-08-21 17:51:07 -0400553 /* Use short preamble only when both the BSS and firmware support it */
554 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
555 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig697900a2008-04-02 16:27:10 +0200556 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400557 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200558 }
559
Dan Williamsd5db2df2008-08-21 17:51:07 -0400560 ret = lbs_set_radio(priv, preamble, 1);
561 if (ret)
562 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200563
564 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
565 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
566
567 priv->adhoccreate = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400568 priv->curbssparams.channel = bss->channel;
Holger Schurig697900a2008-04-02 16:27:10 +0200569
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400570 /* Build the join command */
571 memset(&cmd, 0, sizeof(cmd));
572 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
573
574 cmd.bss.type = CMD_BSS_TYPE_IBSS;
575 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
576
577 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
578 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
579
Dan Williams5fd164e2009-05-22 20:01:21 -0400580 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400581
Dan Williams5fd164e2009-05-22 20:01:21 -0400582 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
583 sizeof(struct ieee_ie_ibss_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400584
585 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
586 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
587 bss->capability, CAPINFO_MASK);
588
589 /* information on BSSID descriptor passed to FW */
Johannes Berge1749612008-10-27 15:59:26 -0700590 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
591 cmd.bss.bssid, cmd.bss.ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400592
593 /* Only v8 and below support setting these */
594 if (priv->fwrelease < 0x09000000) {
595 /* failtimeout */
596 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
597 /* probedelay */
598 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
599 }
600
601 /* Copy Data rates from the rates recorded in scan response */
602 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
David S. Miller97992182009-08-12 17:37:52 -0700603 ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400604 memcpy(cmd.bss.rates, bss->rates, ratesize);
605 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
606 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
607 ret = -1;
608 goto out;
609 }
610
611 /* Copy the ad-hoc creation rates into Current BSS state structure */
612 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
613 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
614
615 /* Set MSB on basic rates as the firmware requires, but _after_
616 * copying to current bss rates.
617 */
618 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
619
Dan Williams5fd164e2009-05-22 20:01:21 -0400620 cmd.bss.ibss.atimwindow = bss->atimwindow;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400621
622 if (assoc_req->secinfo.wep_enabled) {
623 u16 tmp = le16_to_cpu(cmd.bss.capability);
624 tmp |= WLAN_CAPABILITY_PRIVACY;
625 cmd.bss.capability = cpu_to_le16(tmp);
626 }
627
628 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
629 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
630
631 /* wake up first */
632 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
633 CMD_ACT_SET, 0, 0,
634 &local_ps_mode);
635 if (ret) {
636 ret = -1;
637 goto out;
638 }
639 }
640
641 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
642 ret = -1;
643 goto out;
644 }
645
646 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
Dan Williams822ac032009-05-22 20:07:14 -0400647 if (ret == 0) {
648 ret = lbs_adhoc_post(priv,
649 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
650 }
Holger Schurig697900a2008-04-02 16:27:10 +0200651
652out:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400653 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200654 return ret;
655}
656
657/**
658 * @brief Start an Adhoc Network
659 *
660 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400661 * @param assoc_req The association request describing the BSS to start
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400662 *
663 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200664 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400665static int lbs_adhoc_start(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200666 struct assoc_request *assoc_req)
667{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400668 struct cmd_ds_802_11_ad_hoc_start cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400669 u8 preamble = RADIO_PREAMBLE_LONG;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400670 size_t ratesize = 0;
671 u16 tmpcap = 0;
672 int ret = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400673 DECLARE_SSID_BUF(ssid);
Dan Williamsd5db2df2008-08-21 17:51:07 -0400674
675 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200676
Holger Schurig697900a2008-04-02 16:27:10 +0200677 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400678 lbs_deb_join("ADHOC_START: Will use short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400679 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200680 }
681
Dan Williamsd5db2df2008-08-21 17:51:07 -0400682 ret = lbs_set_radio(priv, preamble, 1);
683 if (ret)
684 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200685
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400686 /* Build the start command */
687 memset(&cmd, 0, sizeof(cmd));
688 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurig697900a2008-04-02 16:27:10 +0200689
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400690 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
691
692 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400693 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400694 assoc_req->ssid_len);
695
696 cmd.bsstype = CMD_BSS_TYPE_IBSS;
697
698 if (priv->beacon_period == 0)
699 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
700 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
701
702 WARN_ON(!assoc_req->channel);
703
704 /* set Physical parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400705 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
706 cmd.ds.header.len = 1;
Dan Williams5fd164e2009-05-22 20:01:21 -0400707 cmd.ds.channel = assoc_req->channel;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400708
709 /* set IBSS parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400710 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
711 cmd.ibss.header.len = 2;
Dan Williams5fd164e2009-05-22 20:01:21 -0400712 cmd.ibss.atimwindow = cpu_to_le16(0);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400713
714 /* set capability info */
715 tmpcap = WLAN_CAPABILITY_IBSS;
Dan Williams2fa7a982009-05-22 20:09:58 -0400716 if (assoc_req->secinfo.wep_enabled ||
717 assoc_req->secinfo.WPAenabled ||
718 assoc_req->secinfo.WPA2enabled) {
719 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400720 tmpcap |= WLAN_CAPABILITY_PRIVACY;
721 } else
Dan Williams2fa7a982009-05-22 20:09:58 -0400722 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400723
724 cmd.capability = cpu_to_le16(tmpcap);
725
726 /* Only v8 and below support setting probe delay */
727 if (priv->fwrelease < 0x09000000)
728 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
729
730 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
731 memcpy(cmd.rates, lbs_bg_rates, ratesize);
732
733 /* Copy the ad-hoc creating rates into Current BSS state structure */
734 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
735 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
736
737 /* Set MSB on basic rates as the firmware requires, but _after_
738 * copying to current bss rates.
739 */
740 lbs_set_basic_rate_flags(cmd.rates, ratesize);
741
742 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
743 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
744
745 if (lbs_create_dnld_countryinfo_11d(priv)) {
746 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
747 ret = -1;
748 goto out;
749 }
750
751 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
752 assoc_req->channel, assoc_req->band);
753
754 priv->adhoccreate = 1;
755 priv->mode = IW_MODE_ADHOC;
756
757 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
758 if (ret == 0)
Dan Williams822ac032009-05-22 20:07:14 -0400759 ret = lbs_adhoc_post(priv,
760 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200761
Dan Williamsd5db2df2008-08-21 17:51:07 -0400762out:
763 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200764 return ret;
765}
766
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400767/**
768 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
769 *
770 * @param priv A pointer to struct lbs_private structure
771 * @return 0 on success, or an error
772 */
773int lbs_adhoc_stop(struct lbs_private *priv)
Holger Schurig697900a2008-04-02 16:27:10 +0200774{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400775 struct cmd_ds_802_11_ad_hoc_stop cmd;
776 int ret;
777
778 lbs_deb_enter(LBS_DEB_JOIN);
779
780 memset(&cmd, 0, sizeof (cmd));
781 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
782
783 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
784
785 /* Clean up everything even if there was an error */
786 lbs_mac_event_disconnected(priv);
787
788 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
789 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +0200790}
Dan Williamse76850d2007-05-25 17:09:41 -0400791
Holger Schurig245bf202008-04-02 16:27:42 +0200792static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
793 struct bss_descriptor *match_bss)
794{
795 if (!secinfo->wep_enabled && !secinfo->WPAenabled
796 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100797 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
798 && match_bss->rsn_ie[0] != WLAN_EID_RSN
Holger Schurig245bf202008-04-02 16:27:42 +0200799 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
800 return 1;
801 else
802 return 0;
803}
804
805static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
806 struct bss_descriptor *match_bss)
807{
808 if (secinfo->wep_enabled && !secinfo->WPAenabled
809 && !secinfo->WPA2enabled
810 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
811 return 1;
812 else
813 return 0;
814}
815
816static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
817 struct bss_descriptor *match_bss)
818{
819 if (!secinfo->wep_enabled && secinfo->WPAenabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100820 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
Holger Schurig245bf202008-04-02 16:27:42 +0200821 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
822 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
823 )
824 return 1;
825 else
826 return 0;
827}
828
829static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
830 struct bss_descriptor *match_bss)
831{
832 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
Johannes Berg2c7060022008-10-30 22:09:54 +0100833 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200834 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
835 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
836 )
837 return 1;
838 else
839 return 0;
840}
841
842static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
843 struct bss_descriptor *match_bss)
844{
845 if (!secinfo->wep_enabled && !secinfo->WPAenabled
846 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100847 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
848 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200849 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
850 return 1;
851 else
852 return 0;
853}
854
855/**
856 * @brief Check if a scanned network compatible with the driver settings
857 *
858 * WEP WPA WPA2 ad-hoc encrypt Network
859 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
860 * 0 0 0 0 NONE 0 0 0 yes No security
861 * 1 0 0 0 NONE 1 0 0 yes Static WEP
862 * 0 1 0 0 x 1x 1 x yes WPA
863 * 0 0 1 0 x 1x x 1 yes WPA2
864 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
865 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
866 *
867 *
868 * @param priv A pointer to struct lbs_private
869 * @param index Index in scantable to check against current driver settings
870 * @param mode Network mode: Infrastructure or IBSS
871 *
872 * @return Index in scantable, or error code if negative
873 */
874static int is_network_compatible(struct lbs_private *priv,
875 struct bss_descriptor *bss, uint8_t mode)
876{
877 int matched = 0;
878
879 lbs_deb_enter(LBS_DEB_SCAN);
880
881 if (bss->mode != mode)
882 goto done;
883
884 matched = match_bss_no_security(&priv->secinfo, bss);
885 if (matched)
886 goto done;
887 matched = match_bss_static_wep(&priv->secinfo, bss);
888 if (matched)
889 goto done;
890 matched = match_bss_wpa(&priv->secinfo, bss);
891 if (matched) {
892 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
893 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
894 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
895 priv->secinfo.wep_enabled ? "e" : "d",
896 priv->secinfo.WPAenabled ? "e" : "d",
897 priv->secinfo.WPA2enabled ? "e" : "d",
898 (bss->capability & WLAN_CAPABILITY_PRIVACY));
899 goto done;
900 }
901 matched = match_bss_wpa2(&priv->secinfo, bss);
902 if (matched) {
903 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
904 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
905 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
906 priv->secinfo.wep_enabled ? "e" : "d",
907 priv->secinfo.WPAenabled ? "e" : "d",
908 priv->secinfo.WPA2enabled ? "e" : "d",
909 (bss->capability & WLAN_CAPABILITY_PRIVACY));
910 goto done;
911 }
912 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
913 if (matched) {
914 lbs_deb_scan("is_network_compatible() dynamic WEP: "
915 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
916 bss->wpa_ie[0], bss->rsn_ie[0],
917 (bss->capability & WLAN_CAPABILITY_PRIVACY));
918 goto done;
919 }
920
921 /* bss security settings don't match those configured on card */
922 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
923 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
924 bss->wpa_ie[0], bss->rsn_ie[0],
925 priv->secinfo.wep_enabled ? "e" : "d",
926 priv->secinfo.WPAenabled ? "e" : "d",
927 priv->secinfo.WPA2enabled ? "e" : "d",
928 (bss->capability & WLAN_CAPABILITY_PRIVACY));
929
930done:
931 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
932 return matched;
933}
934
935/**
936 * @brief This function finds a specific compatible BSSID in the scan list
937 *
938 * Used in association code
939 *
940 * @param priv A pointer to struct lbs_private
941 * @param bssid BSSID to find in the scan list
942 * @param mode Network mode: Infrastructure or IBSS
943 *
944 * @return index in BSSID list, or error return code (< 0)
945 */
946static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
947 uint8_t *bssid, uint8_t mode)
948{
949 struct bss_descriptor *iter_bss;
950 struct bss_descriptor *found_bss = NULL;
951
952 lbs_deb_enter(LBS_DEB_SCAN);
953
954 if (!bssid)
955 goto out;
956
957 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
958
959 /* Look through the scan table for a compatible match. The loop will
960 * continue past a matched bssid that is not compatible in case there
961 * is an AP with multiple SSIDs assigned to the same BSSID
962 */
963 mutex_lock(&priv->lock);
964 list_for_each_entry(iter_bss, &priv->network_list, list) {
965 if (compare_ether_addr(iter_bss->bssid, bssid))
966 continue; /* bssid doesn't match */
967 switch (mode) {
968 case IW_MODE_INFRA:
969 case IW_MODE_ADHOC:
970 if (!is_network_compatible(priv, iter_bss, mode))
971 break;
972 found_bss = iter_bss;
973 break;
974 default:
975 found_bss = iter_bss;
976 break;
977 }
978 }
979 mutex_unlock(&priv->lock);
980
981out:
982 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
983 return found_bss;
984}
985
986/**
987 * @brief This function finds ssid in ssid list.
988 *
989 * Used in association code
990 *
991 * @param priv A pointer to struct lbs_private
992 * @param ssid SSID to find in the list
993 * @param bssid BSSID to qualify the SSID selection (if provided)
994 * @param mode Network mode: Infrastructure or IBSS
995 *
996 * @return index in BSSID list
997 */
998static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
999 uint8_t *ssid, uint8_t ssid_len,
1000 uint8_t *bssid, uint8_t mode,
1001 int channel)
1002{
1003 u32 bestrssi = 0;
1004 struct bss_descriptor *iter_bss = NULL;
1005 struct bss_descriptor *found_bss = NULL;
1006 struct bss_descriptor *tmp_oldest = NULL;
1007
1008 lbs_deb_enter(LBS_DEB_SCAN);
1009
1010 mutex_lock(&priv->lock);
1011
1012 list_for_each_entry(iter_bss, &priv->network_list, list) {
1013 if (!tmp_oldest ||
1014 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1015 tmp_oldest = iter_bss;
1016
1017 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1018 ssid, ssid_len) != 0)
1019 continue; /* ssid doesn't match */
1020 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1021 continue; /* bssid doesn't match */
1022 if ((channel > 0) && (iter_bss->channel != channel))
1023 continue; /* channel doesn't match */
1024
1025 switch (mode) {
1026 case IW_MODE_INFRA:
1027 case IW_MODE_ADHOC:
1028 if (!is_network_compatible(priv, iter_bss, mode))
1029 break;
1030
1031 if (bssid) {
1032 /* Found requested BSSID */
1033 found_bss = iter_bss;
1034 goto out;
1035 }
1036
1037 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1038 bestrssi = SCAN_RSSI(iter_bss->rssi);
1039 found_bss = iter_bss;
1040 }
1041 break;
1042 case IW_MODE_AUTO:
1043 default:
1044 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1045 bestrssi = SCAN_RSSI(iter_bss->rssi);
1046 found_bss = iter_bss;
1047 }
1048 break;
1049 }
1050 }
1051
1052out:
1053 mutex_unlock(&priv->lock);
1054 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1055 return found_bss;
1056}
1057
Holger Schurig69f90322007-11-23 15:43:44 +01001058static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001059 struct assoc_request * assoc_req)
1060{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001061 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001062 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001063 int channel = -1;
John W. Linville9387b7c2008-09-30 20:59:05 -04001064 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001065
Holger Schurig9012b282007-05-25 11:27:16 -04001066 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001067
Dan Williamsef9a2642007-05-25 16:46:33 -04001068 /* FIXME: take channel into account when picking SSIDs if a channel
1069 * is set.
1070 */
1071
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001072 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1073 channel = assoc_req->channel;
1074
Holger Schurig0765af42007-10-15 12:55:56 +02001075 lbs_deb_assoc("SSID '%s' requested\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001076 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -04001077 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -05001078 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001079 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001080
David Woodhouseaa21c002007-12-08 20:04:36 +00001081 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001082 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001083 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -04001084 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams822ac032009-05-22 20:07:14 -04001085 ret = lbs_try_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001086 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -04001087 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001088 }
Dan Williams0dc5a292007-05-10 22:58:02 -04001089 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001090 /* Scan for the network, do not save previous results. Stale
1091 * scan data will cause us to join a non-existant adhoc network
1092 */
Holger Schurig10078322007-11-15 18:05:47 -05001093 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001094 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001095
1096 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001097 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001098 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001099 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -04001100 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001101 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001102 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103 } else {
1104 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -04001105 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001106 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001107 IW_ESSID_MAX_SIZE);
1108 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001109 lbs_adhoc_start(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001110 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001111 }
1112
Holger Schurig9012b282007-05-25 11:27:16 -04001113 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001114 return ret;
1115}
1116
1117
Holger Schurig69f90322007-11-23 15:43:44 +01001118static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001119 struct assoc_request * assoc_req)
1120{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001121 int ret = 0;
1122 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001123
Johannes Berge1749612008-10-27 15:59:26 -07001124 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001125
1126 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +00001127 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001128 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001129 if (bss == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -07001130 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1131 "cannot associate.\n", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001132 goto out;
1133 }
1134
Dan Williamse76850d2007-05-25 17:09:41 -04001135 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -04001136 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williams822ac032009-05-22 20:07:14 -04001137 ret = lbs_try_associate(priv, assoc_req);
1138 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1139 ret);
Dan Williams0dc5a292007-05-10 22:58:02 -04001140 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001141 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001142 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001143
1144out:
Holger Schurig9012b282007-05-25 11:27:16 -04001145 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001146 return ret;
1147}
1148
1149
Holger Schurig69f90322007-11-23 15:43:44 +01001150static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001151 struct assoc_request * assoc_req)
1152{
1153 int ret = 0, done = 0;
1154
Holger Schurig0765af42007-10-15 12:55:56 +02001155 lbs_deb_enter(LBS_DEB_ASSOC);
1156
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001157 /* If we're given and 'any' BSSID, try associating based on SSID */
1158
1159 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001160 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1161 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001162 ret = assoc_helper_bssid(priv, assoc_req);
1163 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001164 }
1165 }
1166
1167 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1168 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001169 }
1170
Holger Schurig0765af42007-10-15 12:55:56 +02001171 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001172 return ret;
1173}
1174
1175
Holger Schurig69f90322007-11-23 15:43:44 +01001176static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001177 struct assoc_request * assoc_req)
1178{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001179 int ret = 0;
1180
Holger Schurig9012b282007-05-25 11:27:16 -04001181 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001182
David Woodhouseaa21c002007-12-08 20:04:36 +00001183 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -04001184 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001185
Dan Williams0dc5a292007-05-10 22:58:02 -04001186 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001187 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -05001188 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +00001189 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001190 }
1191
David Woodhouseaa21c002007-12-08 20:04:36 +00001192 priv->mode = assoc_req->mode;
Dan Williams39fcf7a2008-09-10 12:49:00 -04001193 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001194
Holger Schurig9012b282007-05-25 11:27:16 -04001195done:
1196 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001197 return ret;
1198}
1199
Holger Schurig69f90322007-11-23 15:43:44 +01001200static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -04001201 struct assoc_request * assoc_req)
1202{
Dan Williamsef9a2642007-05-25 16:46:33 -04001203 int ret = 0;
1204
1205 lbs_deb_enter(LBS_DEB_ASSOC);
1206
David Woodhouse9f462572007-12-12 22:50:21 -05001207 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001208 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001209 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001210 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -04001211 }
1212
David Woodhouseaa21c002007-12-08 20:04:36 +00001213 if (assoc_req->channel == priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001214 goto done;
1215
David Woodhouse8642f1f2007-12-11 20:03:01 -05001216 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -05001217 /* Change mesh channel first; 21.p21 firmware won't let
1218 you change channel otherwise (even though it'll return
1219 an error to this */
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001220 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1221 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001222 }
1223
Dan Williamsef9a2642007-05-25 16:46:33 -04001224 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
David Woodhouse86062132007-12-13 00:32:36 -05001225 priv->curbssparams.channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -04001226
Dan Williams2dd4b262007-12-11 16:54:15 -05001227 ret = lbs_set_channel(priv, assoc_req->channel);
1228 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -05001229 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -04001230
Dan Williams2dd4b262007-12-11 16:54:15 -05001231 /* FIXME: shouldn't need to grab the channel _again_ after setting
1232 * it since the firmware is supposed to return the new channel, but
1233 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -05001234 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001235 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001236 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001237 goto done;
1238 }
Dan Williamsef9a2642007-05-25 16:46:33 -04001239
David Woodhouseaa21c002007-12-08 20:04:36 +00001240 if (assoc_req->channel != priv->curbssparams.channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -05001241 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -04001242 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001243 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -04001244 }
1245
1246 if ( assoc_req->secinfo.wep_enabled
1247 && (assoc_req->wep_keys[0].len
1248 || assoc_req->wep_keys[1].len
1249 || assoc_req->wep_keys[2].len
1250 || assoc_req->wep_keys[3].len)) {
1251 /* Make sure WEP keys are re-sent to firmware */
1252 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1253 }
1254
1255 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -05001256 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -04001257
David Woodhouse8642f1f2007-12-11 20:03:01 -05001258 restore_mesh:
1259 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001260 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1261 priv->curbssparams.channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001262
1263 done:
Dan Williamsef9a2642007-05-25 16:46:33 -04001264 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1265 return ret;
1266}
1267
1268
Holger Schurig69f90322007-11-23 15:43:44 +01001269static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -05001270 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001271{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001272 int i;
1273 int ret = 0;
1274
Holger Schurig9012b282007-05-25 11:27:16 -04001275 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001276
1277 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -05001278 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1279 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1280 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1281 else
1282 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001283
1284 if (ret)
1285 goto out;
1286
1287 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -04001288 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +01001289 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001290 else
Holger Schurigd9e97782008-03-12 16:06:43 +01001291 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -05001292
Holger Schurigc97329e2008-03-18 11:20:21 +01001293 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001294
David Woodhouseaa21c002007-12-08 20:04:36 +00001295 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001296
David Woodhouseaa21c002007-12-08 20:04:36 +00001297 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001298 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001299 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -05001300 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001301 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001302 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001303
David Woodhouseaa21c002007-12-08 20:04:36 +00001304 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001305
1306out:
Holger Schurig9012b282007-05-25 11:27:16 -04001307 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001308 return ret;
1309}
1310
Holger Schurig69f90322007-11-23 15:43:44 +01001311static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001312 struct assoc_request * assoc_req)
1313{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001314 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -05001315 uint16_t do_wpa;
1316 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001317
Holger Schurig9012b282007-05-25 11:27:16 -04001318 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001319
David Woodhouseaa21c002007-12-08 20:04:36 +00001320 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001321 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001322
Holger Schurigc97329e2008-03-18 11:20:21 +01001323 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001324
Dan Williams18c96c342007-06-18 12:01:12 -04001325 /* If RSN is already enabled, don't try to enable it again, since
1326 * ENABLE_RSN resets internal state machines and will clobber the
1327 * 4-way WPA handshake.
1328 */
1329
1330 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001331 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -04001332 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001333 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -04001334 goto out;
1335 }
1336
1337 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001338 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -04001339 if (do_wpa == rsn)
1340 goto out;
1341
1342 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001343 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -04001344
1345out:
Holger Schurig9012b282007-05-25 11:27:16 -04001346 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001347 return ret;
1348}
1349
1350
Holger Schurig69f90322007-11-23 15:43:44 +01001351static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001352 struct assoc_request * assoc_req)
1353{
1354 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -04001355 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001356
Holger Schurig9012b282007-05-25 11:27:16 -04001357 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001358
Dan Williams2bcde512007-10-03 10:37:45 -04001359 /* Work around older firmware bug where WPA unicast and multicast
1360 * keys must be set independently. Seen in SDIO parts with firmware
1361 * version 5.0.11p0.
1362 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001363
Dan Williams2bcde512007-10-03 10:37:45 -04001364 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1365 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +01001366 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001367 assoc_req->flags = flags;
1368 }
1369
1370 if (ret)
1371 goto out;
1372
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001373 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1374 sizeof(struct enc_key));
1375
Dan Williams2bcde512007-10-03 10:37:45 -04001376 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1377 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1378
David Woodhouse9e1228d2008-03-03 12:15:39 +01001379 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001380 assoc_req->flags = flags;
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001381
1382 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1383 sizeof(struct enc_key));
Dan Williams2bcde512007-10-03 10:37:45 -04001384 }
1385
1386out:
Holger Schurig9012b282007-05-25 11:27:16 -04001387 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001388 return ret;
1389}
1390
1391
Holger Schurig69f90322007-11-23 15:43:44 +01001392static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001393 struct assoc_request * assoc_req)
1394{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001395 int ret = 0;
1396
Holger Schurig9012b282007-05-25 11:27:16 -04001397 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001398
1399 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001400 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1401 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001402 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +00001403 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1404 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001405 }
1406
Holger Schurig9012b282007-05-25 11:27:16 -04001407 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001408 return ret;
1409}
1410
1411
David Woodhouseaa21c002007-12-08 20:04:36 +00001412static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001413 struct assoc_request * assoc_req)
1414{
Holger Schurig0765af42007-10-15 12:55:56 +02001415 int ret = 0;
1416
David Woodhouseaa21c002007-12-08 20:04:36 +00001417 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001418 return 0;
1419
Holger Schurig52507c22008-01-28 17:25:53 +01001420 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001421 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001422 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1423 ret = 1;
1424 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001425 }
1426
1427 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001428 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +02001429 lbs_deb_assoc("Deauthenticating due to new security\n");
1430 ret = 1;
1431 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001432 }
1433 }
1434
1435 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001436 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1437 ret = 1;
1438 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001439 }
1440
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001441 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001442 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1443 ret = 1;
1444 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001445 }
1446
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001447 /* FIXME: deal with 'auto' mode somehow */
1448 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001449 if (assoc_req->mode != IW_MODE_INFRA) {
1450 lbs_deb_assoc("Deauthenticating due to leaving "
1451 "infra mode\n");
1452 ret = 1;
1453 goto out;
1454 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001455 }
1456
Holger Schurig0765af42007-10-15 12:55:56 +02001457out:
1458 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +01001459 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001460}
1461
1462
David Woodhouseaa21c002007-12-08 20:04:36 +00001463static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001464 struct assoc_request * assoc_req)
1465{
Holger Schurig0765af42007-10-15 12:55:56 +02001466 lbs_deb_enter(LBS_DEB_ASSOC);
1467
David Woodhouseaa21c002007-12-08 20:04:36 +00001468 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001469 return 0;
1470
David Woodhouseaa21c002007-12-08 20:04:36 +00001471 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1472 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001473 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001474 return 1;
1475
1476 /* FIXME: deal with 'auto' mode somehow */
1477 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001478 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001479 return 1;
1480 }
1481
Dan Williamsef9a2642007-05-25 16:46:33 -04001482 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001483 if (assoc_req->channel != priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001484 return 1;
1485 }
1486
Holger Schurig0765af42007-10-15 12:55:56 +02001487 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001488 return 0;
1489}
1490
1491
Holger Schurig245bf202008-04-02 16:27:42 +02001492/**
1493 * @brief This function finds the best SSID in the Scan List
1494 *
1495 * Search the scan table for the best SSID that also matches the current
1496 * adapter network preference (infrastructure or adhoc)
1497 *
1498 * @param priv A pointer to struct lbs_private
1499 *
1500 * @return index in BSSID list
1501 */
1502static struct bss_descriptor *lbs_find_best_ssid_in_list(
1503 struct lbs_private *priv, uint8_t mode)
1504{
1505 uint8_t bestrssi = 0;
1506 struct bss_descriptor *iter_bss;
1507 struct bss_descriptor *best_bss = NULL;
1508
1509 lbs_deb_enter(LBS_DEB_SCAN);
1510
1511 mutex_lock(&priv->lock);
1512
1513 list_for_each_entry(iter_bss, &priv->network_list, list) {
1514 switch (mode) {
1515 case IW_MODE_INFRA:
1516 case IW_MODE_ADHOC:
1517 if (!is_network_compatible(priv, iter_bss, mode))
1518 break;
1519 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1520 break;
1521 bestrssi = SCAN_RSSI(iter_bss->rssi);
1522 best_bss = iter_bss;
1523 break;
1524 case IW_MODE_AUTO:
1525 default:
1526 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1527 break;
1528 bestrssi = SCAN_RSSI(iter_bss->rssi);
1529 best_bss = iter_bss;
1530 break;
1531 }
1532 }
1533
1534 mutex_unlock(&priv->lock);
1535 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1536 return best_bss;
1537}
1538
1539/**
1540 * @brief Find the best AP
1541 *
1542 * Used from association worker.
1543 *
1544 * @param priv A pointer to struct lbs_private structure
1545 * @param pSSID A pointer to AP's ssid
1546 *
1547 * @return 0--success, otherwise--fail
1548 */
1549static int lbs_find_best_network_ssid(struct lbs_private *priv,
1550 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1551 uint8_t *out_mode)
1552{
1553 int ret = -1;
1554 struct bss_descriptor *found;
1555
1556 lbs_deb_enter(LBS_DEB_SCAN);
1557
1558 priv->scan_ssid_len = 0;
1559 lbs_scan_networks(priv, 1);
1560 if (priv->surpriseremoved)
1561 goto out;
1562
1563 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1564 if (found && (found->ssid_len > 0)) {
1565 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1566 *out_ssid_len = found->ssid_len;
1567 *out_mode = found->mode;
1568 ret = 0;
1569 }
1570
1571out:
1572 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1573 return ret;
1574}
1575
1576
Holger Schurig10078322007-11-15 18:05:47 -05001577void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001578{
Holger Schurig69f90322007-11-23 15:43:44 +01001579 struct lbs_private *priv = container_of(work, struct lbs_private,
1580 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001581 struct assoc_request * assoc_req = NULL;
1582 int ret = 0;
1583 int find_any_ssid = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001584 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001585
Holger Schurig9012b282007-05-25 11:27:16 -04001586 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001587
David Woodhouseaa21c002007-12-08 20:04:36 +00001588 mutex_lock(&priv->lock);
1589 assoc_req = priv->pending_assoc_req;
1590 priv->pending_assoc_req = NULL;
1591 priv->in_progress_assoc_req = assoc_req;
1592 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001593
Holger Schurig9012b282007-05-25 11:27:16 -04001594 if (!assoc_req)
1595 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001596
Holger Schurig0765af42007-10-15 12:55:56 +02001597 lbs_deb_assoc(
1598 "Association Request:\n"
1599 " flags: 0x%08lx\n"
1600 " SSID: '%s'\n"
1601 " chann: %d\n"
1602 " band: %d\n"
1603 " mode: %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001604 " BSSID: %pM\n"
Holger Schurig0765af42007-10-15 12:55:56 +02001605 " secinfo: %s%s%s\n"
1606 " auth_mode: %d\n",
1607 assoc_req->flags,
John W. Linville9387b7c2008-09-30 20:59:05 -04001608 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Holger Schurig0765af42007-10-15 12:55:56 +02001609 assoc_req->channel, assoc_req->band, assoc_req->mode,
Johannes Berge1749612008-10-27 15:59:26 -07001610 assoc_req->bssid,
Holger Schurig0765af42007-10-15 12:55:56 +02001611 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1612 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1613 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1614 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001615
1616 /* If 'any' SSID was specified, find an SSID to associate with */
1617 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001618 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001619 find_any_ssid = 1;
1620
1621 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1622 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001623 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1624 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001625 find_any_ssid = 0;
1626 }
1627
1628 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02001629 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001630
Holger Schurig10078322007-11-15 18:05:47 -05001631 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001632 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001633 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001634 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001635 ret = -ENETUNREACH;
1636 goto out;
1637 }
1638
1639 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04001640 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001641 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1642 assoc_req->mode = new_mode;
1643 }
1644 }
1645
1646 /*
1647 * Check if the attributes being changing require deauthentication
1648 * from the currently associated infrastructure access point.
1649 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001650 if (priv->mode == IW_MODE_INFRA) {
1651 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04001652 ret = lbs_cmd_80211_deauthenticate(priv,
1653 priv->curbssparams.bssid,
1654 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001655 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001656 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001657 "configuration request failed: %d\n",
1658 ret);
1659 }
1660 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001661 } else if (priv->mode == IW_MODE_ADHOC) {
1662 if (should_stop_adhoc(priv, assoc_req)) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001663 ret = lbs_adhoc_stop(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001664 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001665 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001666 "new configuration request failed: %d\n",
1667 ret);
1668 }
1669
1670 }
1671 }
1672
1673 /* Send the various configuration bits to the firmware */
1674 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1675 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001676 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001677 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001678 }
1679
Dan Williamsef9a2642007-05-25 16:46:33 -04001680 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1681 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001682 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04001683 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04001684 }
1685
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001686 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1687 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1688 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001689 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001690 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001691 }
1692
1693 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1694 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001695 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001696 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001697 }
1698
1699 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1700 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001701 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001702 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001703 }
1704
1705 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1706 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1707 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001708 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001709 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001710 }
1711
1712 /* SSID/BSSID should be the _last_ config option set, because they
1713 * trigger the association attempt.
1714 */
1715 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1716 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1717 int success = 1;
1718
1719 ret = assoc_helper_associate(priv, assoc_req);
1720 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01001721 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001722 ret);
1723 success = 0;
1724 }
1725
David Woodhouseaa21c002007-12-08 20:04:36 +00001726 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01001727 lbs_deb_assoc("ASSOC: association unsuccessful, "
1728 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001729 success = 0;
1730 }
1731
1732 if (success) {
Johannes Berge1749612008-10-27 15:59:26 -07001733 lbs_deb_assoc("associated to %pM\n",
1734 priv->curbssparams.bssid);
Holger Schurig10078322007-11-15 18:05:47 -05001735 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04001736 CMD_802_11_RSSI,
1737 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001738 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001739 ret = -1;
1740 }
1741 }
1742
1743out:
1744 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001745 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001746 ret);
1747 }
Dan Williamse76850d2007-05-25 17:09:41 -04001748
David Woodhouseaa21c002007-12-08 20:04:36 +00001749 mutex_lock(&priv->lock);
1750 priv->in_progress_assoc_req = NULL;
1751 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001752 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04001753
1754done:
1755 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001756}
1757
1758
1759/*
1760 * Caller MUST hold any necessary locks
1761 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001762struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001763{
1764 struct assoc_request * assoc_req;
1765
Holger Schurig0765af42007-10-15 12:55:56 +02001766 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00001767 if (!priv->pending_assoc_req) {
1768 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04001769 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00001770 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001771 lbs_pr_info("Not enough memory to allocate association"
1772 " request!\n");
1773 return NULL;
1774 }
1775 }
1776
1777 /* Copy current configuration attributes to the association request,
1778 * but don't overwrite any that are already set.
1779 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001780 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001781 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001782 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001783 IW_ESSID_MAX_SIZE);
David Woodhouseaa21c002007-12-08 20:04:36 +00001784 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001785 }
1786
1787 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001788 assoc_req->channel = priv->curbssparams.channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001789
Dan Williamse76850d2007-05-25 17:09:41 -04001790 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001791 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04001792
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001793 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001794 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001795
1796 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001797 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001798 ETH_ALEN);
1799 }
1800
1801 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1802 int i;
1803 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001804 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04001805 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001806 }
1807 }
1808
1809 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001810 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001811
1812 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001813 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001814 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001815 }
1816
1817 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001818 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001819 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001820 }
1821
1822 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001823 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001824 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001825 }
1826
1827 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001828 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001829 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001830 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001831 }
1832
Holger Schurig0765af42007-10-15 12:55:56 +02001833 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001834 return assoc_req;
1835}
Holger Schurig697900a2008-04-02 16:27:10 +02001836
1837
1838/**
Dan Williams191bb402008-08-21 17:46:18 -04001839 * @brief Deauthenticate from a specific BSS
1840 *
1841 * @param priv A pointer to struct lbs_private structure
1842 * @param bssid The specific BSS to deauthenticate from
1843 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1844 *
1845 * @return 0 on success, error on failure
1846 */
1847int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1848 u16 reason)
Holger Schurig697900a2008-04-02 16:27:10 +02001849{
Dan Williams191bb402008-08-21 17:46:18 -04001850 struct cmd_ds_802_11_deauthenticate cmd;
1851 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001852
1853 lbs_deb_enter(LBS_DEB_JOIN);
1854
Dan Williams191bb402008-08-21 17:46:18 -04001855 memset(&cmd, 0, sizeof(cmd));
1856 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1857 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1858 cmd.reasoncode = cpu_to_le16(reason);
Holger Schurig697900a2008-04-02 16:27:10 +02001859
Dan Williams191bb402008-08-21 17:46:18 -04001860 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001861
Dan Williams191bb402008-08-21 17:46:18 -04001862 /* Clean up everything even if there was an error; can't assume that
1863 * we're still authenticated to the AP after trying to deauth.
1864 */
1865 lbs_mac_event_disconnected(priv);
Holger Schurig697900a2008-04-02 16:27:10 +02001866
1867 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04001868 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001869}
1870