blob: 385b50f4b1057d35812fdc4d544fabd765c4e7d6 [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;
46 size_t num_card_rates = sizeof(lbs_bg_rates);
47 int ret = 0, i, j;
48 u8 tmp[30];
49 size_t tmp_size = 0;
50
51 /* For each rate in card_rates that exists in rate1, copy to tmp */
52 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
53 for (j = 0; rates[j] && (j < *rates_size); j++) {
54 if (rates[j] == card_rates[i])
55 tmp[tmp_size++] = card_rates[i];
56 }
57 }
58
59 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
60 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
61 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
62 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
63
64 if (!priv->enablehwauto) {
65 for (i = 0; i < tmp_size; i++) {
66 if (tmp[i] == priv->cur_rate)
67 goto done;
68 }
69 lbs_pr_alert("Previously set fixed data rate %#x isn't "
70 "compatible with the network.\n", priv->cur_rate);
71 ret = -1;
72 goto done;
73 }
74 ret = 0;
75
76done:
77 memset(rates, 0, *rates_size);
78 *rates_size = min_t(int, tmp_size, *rates_size);
79 memcpy(rates, tmp, *rates_size);
80 return ret;
81}
82
83
84/**
85 * @brief Sets the MSB on basic rates as the firmware requires
86 *
87 * Scan through an array and set the MSB for basic data rates.
88 *
89 * @param rates buffer of data rates
90 * @param len size of buffer
91 */
92static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
93{
94 int i;
95
96 for (i = 0; i < len; i++) {
97 if (rates[i] == 0x02 || rates[i] == 0x04 ||
98 rates[i] == 0x0b || rates[i] == 0x16)
99 rates[i] |= 0x80;
100 }
101}
102
Holger Schurig697900a2008-04-02 16:27:10 +0200103
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400104static u8 iw_auth_to_ieee_auth(u8 auth)
105{
106 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
107 return 0x00;
108 else if (auth == IW_AUTH_ALG_SHARED_KEY)
109 return 0x01;
110 else if (auth == IW_AUTH_ALG_LEAP)
111 return 0x80;
112
113 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
114 return 0;
115}
116
117/**
118 * @brief This function prepares the authenticate command. AUTHENTICATE only
119 * sets the authentication suite for future associations, as the firmware
120 * handles authentication internally during the ASSOCIATE command.
121 *
122 * @param priv A pointer to struct lbs_private structure
123 * @param bssid The peer BSSID with which to authenticate
124 * @param auth The authentication mode to use (from wireless.h)
125 *
126 * @return 0 or -1
127 */
128static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
129{
130 struct cmd_ds_802_11_authenticate cmd;
131 int ret = -1;
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400132
133 lbs_deb_enter(LBS_DEB_JOIN);
134
135 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
136 memcpy(cmd.bssid, bssid, ETH_ALEN);
137
138 cmd.authtype = iw_auth_to_ieee_auth(auth);
139
Johannes Berge91d8332009-07-15 17:21:41 +0200140 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400141
142 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
143
144 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
145 return ret;
146}
147
Dan Williams822ac032009-05-22 20:07:14 -0400148
149static int lbs_assoc_post(struct lbs_private *priv,
150 struct cmd_ds_802_11_associate_response *resp)
151{
152 int ret = 0;
153 union iwreq_data wrqu;
154 struct bss_descriptor *bss;
155 u16 status_code;
156
157 lbs_deb_enter(LBS_DEB_ASSOC);
158
159 if (!priv->in_progress_assoc_req) {
160 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
161 ret = -1;
162 goto done;
163 }
164 bss = &priv->in_progress_assoc_req->bss;
165
166 /*
167 * Older FW versions map the IEEE 802.11 Status Code in the association
168 * response to the following values returned in resp->statuscode:
169 *
170 * IEEE Status Code Marvell Status Code
171 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
172 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
173 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
174 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
175 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
176 * others -> 0x0003 ASSOC_RESULT_REFUSED
177 *
178 * Other response codes:
179 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
180 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
181 * association response from the AP)
182 */
183
184 status_code = le16_to_cpu(resp->statuscode);
185 if (priv->fwrelease < 0x09000000) {
186 switch (status_code) {
187 case 0x00:
188 break;
189 case 0x01:
190 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
191 break;
192 case 0x02:
193 lbs_deb_assoc("ASSOC_RESP: internal timer "
194 "expired while waiting for the AP\n");
195 break;
196 case 0x03:
197 lbs_deb_assoc("ASSOC_RESP: association "
198 "refused by AP\n");
199 break;
200 case 0x04:
201 lbs_deb_assoc("ASSOC_RESP: authentication "
202 "refused by AP\n");
203 break;
204 default:
205 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
206 " unknown\n", status_code);
207 break;
208 }
209 } else {
210 /* v9+ returns the AP's association response */
211 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
212 }
213
214 if (status_code) {
215 lbs_mac_event_disconnected(priv);
216 ret = -1;
217 goto done;
218 }
219
220 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
221 (void *) (resp + sizeof (resp->hdr)),
222 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
223
224 /* Send a Media Connected event, according to the Spec */
225 priv->connect_status = LBS_CONNECTED;
226
227 /* Update current SSID and BSSID */
228 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
229 priv->curbssparams.ssid_len = bss->ssid_len;
230 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
231
232 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
233 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
234
235 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
236 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
237 priv->nextSNRNF = 0;
238 priv->numSNRNF = 0;
239
240 netif_carrier_on(priv->dev);
241 if (!priv->tx_pending_len)
242 netif_wake_queue(priv->dev);
243
244 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
245 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
246 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
247
248done:
249 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
250 return ret;
251}
252
253/**
254 * @brief This function prepares an association-class command.
255 *
256 * @param priv A pointer to struct lbs_private structure
257 * @param assoc_req The association request describing the BSS to associate
258 * or reassociate with
259 * @param command The actual command, either CMD_802_11_ASSOCIATE or
260 * CMD_802_11_REASSOCIATE
261 *
262 * @return 0 or -1
263 */
264static int lbs_associate(struct lbs_private *priv,
265 struct assoc_request *assoc_req,
266 u16 command)
267{
268 struct cmd_ds_802_11_associate cmd;
269 int ret = 0;
270 struct bss_descriptor *bss = &assoc_req->bss;
271 u8 *pos = &(cmd.iebuf[0]);
272 u16 tmpcap, tmplen, tmpauth;
273 struct mrvl_ie_ssid_param_set *ssid;
274 struct mrvl_ie_ds_param_set *ds;
275 struct mrvl_ie_cf_param_set *cf;
276 struct mrvl_ie_rates_param_set *rates;
277 struct mrvl_ie_rsn_param_set *rsn;
278 struct mrvl_ie_auth_type *auth;
279
280 lbs_deb_enter(LBS_DEB_ASSOC);
281
282 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
283 (command != CMD_802_11_REASSOCIATE));
284
285 memset(&cmd, 0, sizeof(cmd));
286 cmd.hdr.command = cpu_to_le16(command);
287
288 /* Fill in static fields */
289 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
290 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
291
292 /* Capability info */
293 tmpcap = (bss->capability & CAPINFO_MASK);
294 if (bss->mode == IW_MODE_INFRA)
295 tmpcap |= WLAN_CAPABILITY_ESS;
296 cmd.capability = cpu_to_le16(tmpcap);
297 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
298
299 /* SSID */
300 ssid = (struct mrvl_ie_ssid_param_set *) pos;
301 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
302 tmplen = bss->ssid_len;
303 ssid->header.len = cpu_to_le16(tmplen);
304 memcpy(ssid->ssid, bss->ssid, tmplen);
305 pos += sizeof(ssid->header) + tmplen;
306
307 ds = (struct mrvl_ie_ds_param_set *) pos;
308 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
309 ds->header.len = cpu_to_le16(1);
310 ds->channel = bss->phy.ds.channel;
311 pos += sizeof(ds->header) + 1;
312
313 cf = (struct mrvl_ie_cf_param_set *) pos;
314 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
315 tmplen = sizeof(*cf) - sizeof (cf->header);
316 cf->header.len = cpu_to_le16(tmplen);
317 /* IE payload should be zeroed, firmware fills it in for us */
318 pos += sizeof(*cf);
319
320 rates = (struct mrvl_ie_rates_param_set *) pos;
321 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
322 memcpy(&rates->rates, &bss->rates, MAX_RATES);
323 tmplen = MAX_RATES;
324 if (get_common_rates(priv, rates->rates, &tmplen)) {
325 ret = -1;
326 goto done;
327 }
328 pos += sizeof(rates->header) + tmplen;
329 rates->header.len = cpu_to_le16(tmplen);
330 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
331
332 /* Copy the infra. association rates into Current BSS state structure */
333 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
334 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
335
336 /* Set MSB on basic rates as the firmware requires, but _after_
337 * copying to current bss rates.
338 */
339 lbs_set_basic_rate_flags(rates->rates, tmplen);
340
341 /* Firmware v9+ indicate authentication suites as a TLV */
342 if (priv->fwrelease >= 0x09000000) {
Dan Williams822ac032009-05-22 20:07:14 -0400343 auth = (struct mrvl_ie_auth_type *) pos;
344 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
345 auth->header.len = cpu_to_le16(2);
346 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
347 auth->auth = cpu_to_le16(tmpauth);
348 pos += sizeof(auth->header) + 2;
349
Johannes Berge91d8332009-07-15 17:21:41 +0200350 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
351 bss->bssid, priv->secinfo.auth_mode);
Dan Williams822ac032009-05-22 20:07:14 -0400352 }
353
354 /* WPA/WPA2 IEs */
355 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
356 rsn = (struct mrvl_ie_rsn_param_set *) pos;
357 /* WPA_IE or WPA2_IE */
358 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
359 tmplen = (u16) assoc_req->wpa_ie[1];
360 rsn->header.len = cpu_to_le16(tmplen);
361 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
362 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
363 sizeof(rsn->header) + tmplen);
364 pos += sizeof(rsn->header) + tmplen;
365 }
366
367 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
368 (u16)(pos - (u8 *) &cmd.iebuf));
369
370 /* update curbssparams */
371 priv->curbssparams.channel = bss->phy.ds.channel;
372
373 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
374 ret = -1;
375 goto done;
376 }
377
378 ret = lbs_cmd_with_response(priv, command, &cmd);
379 if (ret == 0) {
380 ret = lbs_assoc_post(priv,
381 (struct cmd_ds_802_11_associate_response *) &cmd);
382 }
383
384done:
385 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
386 return ret;
387}
388
Holger Schurig697900a2008-04-02 16:27:10 +0200389/**
390 * @brief Associate to a specific BSS discovered in a scan
391 *
392 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400393 * @param assoc_req The association request describing the BSS to associate with
Holger Schurig697900a2008-04-02 16:27:10 +0200394 *
395 * @return 0-success, otherwise fail
396 */
Dan Williams822ac032009-05-22 20:07:14 -0400397static int lbs_try_associate(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200398 struct assoc_request *assoc_req)
399{
400 int ret;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400401 u8 preamble = RADIO_PREAMBLE_LONG;
Holger Schurig697900a2008-04-02 16:27:10 +0200402
403 lbs_deb_enter(LBS_DEB_ASSOC);
404
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400405 /* FW v9 and higher indicate authentication suites as a TLV in the
406 * association command, not as a separate authentication command.
407 */
408 if (priv->fwrelease < 0x09000000) {
409 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
410 priv->secinfo.auth_mode);
411 if (ret)
412 goto out;
413 }
Holger Schurig697900a2008-04-02 16:27:10 +0200414
Dan Williamsd5db2df2008-08-21 17:51:07 -0400415 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig697900a2008-04-02 16:27:10 +0200416 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
417 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
Dan Williamsd5db2df2008-08-21 17:51:07 -0400418 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200419
Dan Williamsd5db2df2008-08-21 17:51:07 -0400420 ret = lbs_set_radio(priv, preamble, 1);
421 if (ret)
422 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200423
Dan Williams822ac032009-05-22 20:07:14 -0400424 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
Holger Schurig697900a2008-04-02 16:27:10 +0200425
Dan Williamsd5db2df2008-08-21 17:51:07 -0400426out:
Holger Schurig697900a2008-04-02 16:27:10 +0200427 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
428 return ret;
429}
430
Dan Williams822ac032009-05-22 20:07:14 -0400431static int lbs_adhoc_post(struct lbs_private *priv,
432 struct cmd_ds_802_11_ad_hoc_result *resp)
433{
434 int ret = 0;
435 u16 command = le16_to_cpu(resp->hdr.command);
436 u16 result = le16_to_cpu(resp->hdr.result);
437 union iwreq_data wrqu;
438 struct bss_descriptor *bss;
439 DECLARE_SSID_BUF(ssid);
440
441 lbs_deb_enter(LBS_DEB_JOIN);
442
443 if (!priv->in_progress_assoc_req) {
444 lbs_deb_join("ADHOC_RESP: no in-progress association "
445 "request\n");
446 ret = -1;
447 goto done;
448 }
449 bss = &priv->in_progress_assoc_req->bss;
450
451 /*
452 * Join result code 0 --> SUCCESS
453 */
454 if (result) {
455 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
456 if (priv->connect_status == LBS_CONNECTED)
457 lbs_mac_event_disconnected(priv);
458 ret = -1;
459 goto done;
460 }
461
462 /* Send a Media Connected event, according to the Spec */
463 priv->connect_status = LBS_CONNECTED;
464
465 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
466 /* Update the created network descriptor with the new BSSID */
467 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
468 }
469
470 /* Set the BSSID from the joined/started descriptor */
471 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
472
473 /* Set the new SSID to current SSID */
474 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
475 priv->curbssparams.ssid_len = bss->ssid_len;
476
477 netif_carrier_on(priv->dev);
478 if (!priv->tx_pending_len)
479 netif_wake_queue(priv->dev);
480
481 memset(&wrqu, 0, sizeof(wrqu));
482 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
483 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
484 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
485
486 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
487 print_ssid(ssid, bss->ssid, bss->ssid_len),
488 priv->curbssparams.bssid,
489 priv->curbssparams.channel);
490
491done:
492 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
493 return ret;
494}
495
Holger Schurig697900a2008-04-02 16:27:10 +0200496/**
497 * @brief Join an adhoc network found in a previous scan
498 *
499 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400500 * @param assoc_req The association request describing the BSS to join
Holger Schurig697900a2008-04-02 16:27:10 +0200501 *
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400502 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200503 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400504static int lbs_adhoc_join(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200505 struct assoc_request *assoc_req)
506{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400507 struct cmd_ds_802_11_ad_hoc_join cmd;
Holger Schurig697900a2008-04-02 16:27:10 +0200508 struct bss_descriptor *bss = &assoc_req->bss;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400509 u8 preamble = RADIO_PREAMBLE_LONG;
John W. Linville9387b7c2008-09-30 20:59:05 -0400510 DECLARE_SSID_BUF(ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400511 u16 ratesize = 0;
512 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400513
514 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200515
516 lbs_deb_join("current SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400517 print_ssid(ssid, priv->curbssparams.ssid,
Holger Schurig697900a2008-04-02 16:27:10 +0200518 priv->curbssparams.ssid_len),
519 priv->curbssparams.ssid_len);
520 lbs_deb_join("requested ssid '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400521 print_ssid(ssid, bss->ssid, bss->ssid_len),
Holger Schurig697900a2008-04-02 16:27:10 +0200522 bss->ssid_len);
523
524 /* check if the requested SSID is already joined */
525 if (priv->curbssparams.ssid_len &&
526 !lbs_ssid_cmp(priv->curbssparams.ssid,
527 priv->curbssparams.ssid_len,
528 bss->ssid, bss->ssid_len) &&
529 (priv->mode == IW_MODE_ADHOC) &&
530 (priv->connect_status == LBS_CONNECTED)) {
531 union iwreq_data wrqu;
532
533 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
534 "current, not attempting to re-join");
535
536 /* Send the re-association event though, because the association
537 * request really was successful, even if just a null-op.
538 */
539 memset(&wrqu, 0, sizeof(wrqu));
540 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
541 ETH_ALEN);
542 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
543 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
544 goto out;
545 }
546
Dan Williamsd5db2df2008-08-21 17:51:07 -0400547 /* Use short preamble only when both the BSS and firmware support it */
548 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
549 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig697900a2008-04-02 16:27:10 +0200550 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400551 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200552 }
553
Dan Williamsd5db2df2008-08-21 17:51:07 -0400554 ret = lbs_set_radio(priv, preamble, 1);
555 if (ret)
556 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200557
558 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
559 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
560
561 priv->adhoccreate = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400562 priv->curbssparams.channel = bss->channel;
Holger Schurig697900a2008-04-02 16:27:10 +0200563
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400564 /* Build the join command */
565 memset(&cmd, 0, sizeof(cmd));
566 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
567
568 cmd.bss.type = CMD_BSS_TYPE_IBSS;
569 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
570
571 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
572 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
573
Dan Williams5fd164e2009-05-22 20:01:21 -0400574 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400575
Dan Williams5fd164e2009-05-22 20:01:21 -0400576 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
577 sizeof(struct ieee_ie_ibss_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400578
579 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
580 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
581 bss->capability, CAPINFO_MASK);
582
583 /* information on BSSID descriptor passed to FW */
Johannes Berge1749612008-10-27 15:59:26 -0700584 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
585 cmd.bss.bssid, cmd.bss.ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400586
587 /* Only v8 and below support setting these */
588 if (priv->fwrelease < 0x09000000) {
589 /* failtimeout */
590 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
591 /* probedelay */
592 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
593 }
594
595 /* Copy Data rates from the rates recorded in scan response */
596 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
597 ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
598 memcpy(cmd.bss.rates, bss->rates, ratesize);
599 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
600 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
601 ret = -1;
602 goto out;
603 }
604
605 /* Copy the ad-hoc creation rates into Current BSS state structure */
606 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
607 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
608
609 /* Set MSB on basic rates as the firmware requires, but _after_
610 * copying to current bss rates.
611 */
612 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
613
Dan Williams5fd164e2009-05-22 20:01:21 -0400614 cmd.bss.ibss.atimwindow = bss->atimwindow;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400615
616 if (assoc_req->secinfo.wep_enabled) {
617 u16 tmp = le16_to_cpu(cmd.bss.capability);
618 tmp |= WLAN_CAPABILITY_PRIVACY;
619 cmd.bss.capability = cpu_to_le16(tmp);
620 }
621
622 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
623 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
624
625 /* wake up first */
626 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
627 CMD_ACT_SET, 0, 0,
628 &local_ps_mode);
629 if (ret) {
630 ret = -1;
631 goto out;
632 }
633 }
634
635 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
636 ret = -1;
637 goto out;
638 }
639
640 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
Dan Williams822ac032009-05-22 20:07:14 -0400641 if (ret == 0) {
642 ret = lbs_adhoc_post(priv,
643 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
644 }
Holger Schurig697900a2008-04-02 16:27:10 +0200645
646out:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400647 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200648 return ret;
649}
650
651/**
652 * @brief Start an Adhoc Network
653 *
654 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400655 * @param assoc_req The association request describing the BSS to start
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400656 *
657 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200658 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400659static int lbs_adhoc_start(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200660 struct assoc_request *assoc_req)
661{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400662 struct cmd_ds_802_11_ad_hoc_start cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400663 u8 preamble = RADIO_PREAMBLE_LONG;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400664 size_t ratesize = 0;
665 u16 tmpcap = 0;
666 int ret = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400667 DECLARE_SSID_BUF(ssid);
Dan Williamsd5db2df2008-08-21 17:51:07 -0400668
669 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200670
Holger Schurig697900a2008-04-02 16:27:10 +0200671 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400672 lbs_deb_join("ADHOC_START: Will use short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400673 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200674 }
675
Dan Williamsd5db2df2008-08-21 17:51:07 -0400676 ret = lbs_set_radio(priv, preamble, 1);
677 if (ret)
678 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200679
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400680 /* Build the start command */
681 memset(&cmd, 0, sizeof(cmd));
682 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurig697900a2008-04-02 16:27:10 +0200683
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400684 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
685
686 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400687 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400688 assoc_req->ssid_len);
689
690 cmd.bsstype = CMD_BSS_TYPE_IBSS;
691
692 if (priv->beacon_period == 0)
693 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
694 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
695
696 WARN_ON(!assoc_req->channel);
697
698 /* set Physical parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400699 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
700 cmd.ds.header.len = 1;
Dan Williams5fd164e2009-05-22 20:01:21 -0400701 cmd.ds.channel = assoc_req->channel;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400702
703 /* set IBSS parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400704 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
705 cmd.ibss.header.len = 2;
Dan Williams5fd164e2009-05-22 20:01:21 -0400706 cmd.ibss.atimwindow = cpu_to_le16(0);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400707
708 /* set capability info */
709 tmpcap = WLAN_CAPABILITY_IBSS;
Dan Williams2fa7a982009-05-22 20:09:58 -0400710 if (assoc_req->secinfo.wep_enabled ||
711 assoc_req->secinfo.WPAenabled ||
712 assoc_req->secinfo.WPA2enabled) {
713 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400714 tmpcap |= WLAN_CAPABILITY_PRIVACY;
715 } else
Dan Williams2fa7a982009-05-22 20:09:58 -0400716 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400717
718 cmd.capability = cpu_to_le16(tmpcap);
719
720 /* Only v8 and below support setting probe delay */
721 if (priv->fwrelease < 0x09000000)
722 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
723
724 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
725 memcpy(cmd.rates, lbs_bg_rates, ratesize);
726
727 /* Copy the ad-hoc creating rates into Current BSS state structure */
728 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
729 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
730
731 /* Set MSB on basic rates as the firmware requires, but _after_
732 * copying to current bss rates.
733 */
734 lbs_set_basic_rate_flags(cmd.rates, ratesize);
735
736 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
737 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
738
739 if (lbs_create_dnld_countryinfo_11d(priv)) {
740 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
741 ret = -1;
742 goto out;
743 }
744
745 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
746 assoc_req->channel, assoc_req->band);
747
748 priv->adhoccreate = 1;
749 priv->mode = IW_MODE_ADHOC;
750
751 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
752 if (ret == 0)
Dan Williams822ac032009-05-22 20:07:14 -0400753 ret = lbs_adhoc_post(priv,
754 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200755
Dan Williamsd5db2df2008-08-21 17:51:07 -0400756out:
757 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200758 return ret;
759}
760
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400761/**
762 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
763 *
764 * @param priv A pointer to struct lbs_private structure
765 * @return 0 on success, or an error
766 */
767int lbs_adhoc_stop(struct lbs_private *priv)
Holger Schurig697900a2008-04-02 16:27:10 +0200768{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400769 struct cmd_ds_802_11_ad_hoc_stop cmd;
770 int ret;
771
772 lbs_deb_enter(LBS_DEB_JOIN);
773
774 memset(&cmd, 0, sizeof (cmd));
775 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
776
777 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
778
779 /* Clean up everything even if there was an error */
780 lbs_mac_event_disconnected(priv);
781
782 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
783 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +0200784}
Dan Williamse76850d2007-05-25 17:09:41 -0400785
Holger Schurig245bf202008-04-02 16:27:42 +0200786static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
787 struct bss_descriptor *match_bss)
788{
789 if (!secinfo->wep_enabled && !secinfo->WPAenabled
790 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100791 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
792 && match_bss->rsn_ie[0] != WLAN_EID_RSN
Holger Schurig245bf202008-04-02 16:27:42 +0200793 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
794 return 1;
795 else
796 return 0;
797}
798
799static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
800 struct bss_descriptor *match_bss)
801{
802 if (secinfo->wep_enabled && !secinfo->WPAenabled
803 && !secinfo->WPA2enabled
804 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
805 return 1;
806 else
807 return 0;
808}
809
810static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
811 struct bss_descriptor *match_bss)
812{
813 if (!secinfo->wep_enabled && secinfo->WPAenabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100814 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
Holger Schurig245bf202008-04-02 16:27:42 +0200815 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
816 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
817 )
818 return 1;
819 else
820 return 0;
821}
822
823static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
824 struct bss_descriptor *match_bss)
825{
826 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
Johannes Berg2c7060022008-10-30 22:09:54 +0100827 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200828 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
829 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
830 )
831 return 1;
832 else
833 return 0;
834}
835
836static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
837 struct bss_descriptor *match_bss)
838{
839 if (!secinfo->wep_enabled && !secinfo->WPAenabled
840 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100841 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
842 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200843 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
844 return 1;
845 else
846 return 0;
847}
848
849/**
850 * @brief Check if a scanned network compatible with the driver settings
851 *
852 * WEP WPA WPA2 ad-hoc encrypt Network
853 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
854 * 0 0 0 0 NONE 0 0 0 yes No security
855 * 1 0 0 0 NONE 1 0 0 yes Static WEP
856 * 0 1 0 0 x 1x 1 x yes WPA
857 * 0 0 1 0 x 1x x 1 yes WPA2
858 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
859 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
860 *
861 *
862 * @param priv A pointer to struct lbs_private
863 * @param index Index in scantable to check against current driver settings
864 * @param mode Network mode: Infrastructure or IBSS
865 *
866 * @return Index in scantable, or error code if negative
867 */
868static int is_network_compatible(struct lbs_private *priv,
869 struct bss_descriptor *bss, uint8_t mode)
870{
871 int matched = 0;
872
873 lbs_deb_enter(LBS_DEB_SCAN);
874
875 if (bss->mode != mode)
876 goto done;
877
878 matched = match_bss_no_security(&priv->secinfo, bss);
879 if (matched)
880 goto done;
881 matched = match_bss_static_wep(&priv->secinfo, bss);
882 if (matched)
883 goto done;
884 matched = match_bss_wpa(&priv->secinfo, bss);
885 if (matched) {
886 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
887 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
888 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
889 priv->secinfo.wep_enabled ? "e" : "d",
890 priv->secinfo.WPAenabled ? "e" : "d",
891 priv->secinfo.WPA2enabled ? "e" : "d",
892 (bss->capability & WLAN_CAPABILITY_PRIVACY));
893 goto done;
894 }
895 matched = match_bss_wpa2(&priv->secinfo, bss);
896 if (matched) {
897 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
898 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
899 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
900 priv->secinfo.wep_enabled ? "e" : "d",
901 priv->secinfo.WPAenabled ? "e" : "d",
902 priv->secinfo.WPA2enabled ? "e" : "d",
903 (bss->capability & WLAN_CAPABILITY_PRIVACY));
904 goto done;
905 }
906 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
907 if (matched) {
908 lbs_deb_scan("is_network_compatible() dynamic WEP: "
909 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
910 bss->wpa_ie[0], bss->rsn_ie[0],
911 (bss->capability & WLAN_CAPABILITY_PRIVACY));
912 goto done;
913 }
914
915 /* bss security settings don't match those configured on card */
916 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
917 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
918 bss->wpa_ie[0], bss->rsn_ie[0],
919 priv->secinfo.wep_enabled ? "e" : "d",
920 priv->secinfo.WPAenabled ? "e" : "d",
921 priv->secinfo.WPA2enabled ? "e" : "d",
922 (bss->capability & WLAN_CAPABILITY_PRIVACY));
923
924done:
925 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
926 return matched;
927}
928
929/**
930 * @brief This function finds a specific compatible BSSID in the scan list
931 *
932 * Used in association code
933 *
934 * @param priv A pointer to struct lbs_private
935 * @param bssid BSSID to find in the scan list
936 * @param mode Network mode: Infrastructure or IBSS
937 *
938 * @return index in BSSID list, or error return code (< 0)
939 */
940static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
941 uint8_t *bssid, uint8_t mode)
942{
943 struct bss_descriptor *iter_bss;
944 struct bss_descriptor *found_bss = NULL;
945
946 lbs_deb_enter(LBS_DEB_SCAN);
947
948 if (!bssid)
949 goto out;
950
951 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
952
953 /* Look through the scan table for a compatible match. The loop will
954 * continue past a matched bssid that is not compatible in case there
955 * is an AP with multiple SSIDs assigned to the same BSSID
956 */
957 mutex_lock(&priv->lock);
958 list_for_each_entry(iter_bss, &priv->network_list, list) {
959 if (compare_ether_addr(iter_bss->bssid, bssid))
960 continue; /* bssid doesn't match */
961 switch (mode) {
962 case IW_MODE_INFRA:
963 case IW_MODE_ADHOC:
964 if (!is_network_compatible(priv, iter_bss, mode))
965 break;
966 found_bss = iter_bss;
967 break;
968 default:
969 found_bss = iter_bss;
970 break;
971 }
972 }
973 mutex_unlock(&priv->lock);
974
975out:
976 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
977 return found_bss;
978}
979
980/**
981 * @brief This function finds ssid in ssid list.
982 *
983 * Used in association code
984 *
985 * @param priv A pointer to struct lbs_private
986 * @param ssid SSID to find in the list
987 * @param bssid BSSID to qualify the SSID selection (if provided)
988 * @param mode Network mode: Infrastructure or IBSS
989 *
990 * @return index in BSSID list
991 */
992static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
993 uint8_t *ssid, uint8_t ssid_len,
994 uint8_t *bssid, uint8_t mode,
995 int channel)
996{
997 u32 bestrssi = 0;
998 struct bss_descriptor *iter_bss = NULL;
999 struct bss_descriptor *found_bss = NULL;
1000 struct bss_descriptor *tmp_oldest = NULL;
1001
1002 lbs_deb_enter(LBS_DEB_SCAN);
1003
1004 mutex_lock(&priv->lock);
1005
1006 list_for_each_entry(iter_bss, &priv->network_list, list) {
1007 if (!tmp_oldest ||
1008 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1009 tmp_oldest = iter_bss;
1010
1011 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1012 ssid, ssid_len) != 0)
1013 continue; /* ssid doesn't match */
1014 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1015 continue; /* bssid doesn't match */
1016 if ((channel > 0) && (iter_bss->channel != channel))
1017 continue; /* channel doesn't match */
1018
1019 switch (mode) {
1020 case IW_MODE_INFRA:
1021 case IW_MODE_ADHOC:
1022 if (!is_network_compatible(priv, iter_bss, mode))
1023 break;
1024
1025 if (bssid) {
1026 /* Found requested BSSID */
1027 found_bss = iter_bss;
1028 goto out;
1029 }
1030
1031 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1032 bestrssi = SCAN_RSSI(iter_bss->rssi);
1033 found_bss = iter_bss;
1034 }
1035 break;
1036 case IW_MODE_AUTO:
1037 default:
1038 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1039 bestrssi = SCAN_RSSI(iter_bss->rssi);
1040 found_bss = iter_bss;
1041 }
1042 break;
1043 }
1044 }
1045
1046out:
1047 mutex_unlock(&priv->lock);
1048 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1049 return found_bss;
1050}
1051
Holger Schurig69f90322007-11-23 15:43:44 +01001052static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001053 struct assoc_request * assoc_req)
1054{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001055 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001056 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001057 int channel = -1;
John W. Linville9387b7c2008-09-30 20:59:05 -04001058 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001059
Holger Schurig9012b282007-05-25 11:27:16 -04001060 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001061
Dan Williamsef9a2642007-05-25 16:46:33 -04001062 /* FIXME: take channel into account when picking SSIDs if a channel
1063 * is set.
1064 */
1065
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001066 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1067 channel = assoc_req->channel;
1068
Holger Schurig0765af42007-10-15 12:55:56 +02001069 lbs_deb_assoc("SSID '%s' requested\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001070 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -04001071 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -05001072 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001073 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001074
David Woodhouseaa21c002007-12-08 20:04:36 +00001075 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001076 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001077 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -04001078 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams822ac032009-05-22 20:07:14 -04001079 ret = lbs_try_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001080 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -04001081 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001082 }
Dan Williams0dc5a292007-05-10 22:58:02 -04001083 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001084 /* Scan for the network, do not save previous results. Stale
1085 * scan data will cause us to join a non-existant adhoc network
1086 */
Holger Schurig10078322007-11-15 18:05:47 -05001087 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001088 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001089
1090 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001091 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001092 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001093 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -04001094 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001095 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001096 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001097 } else {
1098 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -04001099 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001100 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001101 IW_ESSID_MAX_SIZE);
1102 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001103 lbs_adhoc_start(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001104 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001105 }
1106
Holger Schurig9012b282007-05-25 11:27:16 -04001107 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001108 return ret;
1109}
1110
1111
Holger Schurig69f90322007-11-23 15:43:44 +01001112static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001113 struct assoc_request * assoc_req)
1114{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001115 int ret = 0;
1116 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001117
Johannes Berge1749612008-10-27 15:59:26 -07001118 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001119
1120 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +00001121 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001122 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001123 if (bss == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -07001124 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1125 "cannot associate.\n", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001126 goto out;
1127 }
1128
Dan Williamse76850d2007-05-25 17:09:41 -04001129 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -04001130 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williams822ac032009-05-22 20:07:14 -04001131 ret = lbs_try_associate(priv, assoc_req);
1132 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1133 ret);
Dan Williams0dc5a292007-05-10 22:58:02 -04001134 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001135 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001136 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001137
1138out:
Holger Schurig9012b282007-05-25 11:27:16 -04001139 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001140 return ret;
1141}
1142
1143
Holger Schurig69f90322007-11-23 15:43:44 +01001144static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001145 struct assoc_request * assoc_req)
1146{
1147 int ret = 0, done = 0;
1148
Holger Schurig0765af42007-10-15 12:55:56 +02001149 lbs_deb_enter(LBS_DEB_ASSOC);
1150
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001151 /* If we're given and 'any' BSSID, try associating based on SSID */
1152
1153 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001154 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1155 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001156 ret = assoc_helper_bssid(priv, assoc_req);
1157 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001158 }
1159 }
1160
1161 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1162 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001163 }
1164
Holger Schurig0765af42007-10-15 12:55:56 +02001165 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001166 return ret;
1167}
1168
1169
Holger Schurig69f90322007-11-23 15:43:44 +01001170static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171 struct assoc_request * assoc_req)
1172{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001173 int ret = 0;
1174
Holger Schurig9012b282007-05-25 11:27:16 -04001175 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001176
David Woodhouseaa21c002007-12-08 20:04:36 +00001177 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -04001178 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001179
Dan Williams0dc5a292007-05-10 22:58:02 -04001180 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001181 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -05001182 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +00001183 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001184 }
1185
David Woodhouseaa21c002007-12-08 20:04:36 +00001186 priv->mode = assoc_req->mode;
Dan Williams39fcf7a2008-09-10 12:49:00 -04001187 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001188
Holger Schurig9012b282007-05-25 11:27:16 -04001189done:
1190 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001191 return ret;
1192}
1193
Holger Schurig69f90322007-11-23 15:43:44 +01001194static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -04001195 struct assoc_request * assoc_req)
1196{
Dan Williamsef9a2642007-05-25 16:46:33 -04001197 int ret = 0;
1198
1199 lbs_deb_enter(LBS_DEB_ASSOC);
1200
David Woodhouse9f462572007-12-12 22:50:21 -05001201 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001202 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001203 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001204 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -04001205 }
1206
David Woodhouseaa21c002007-12-08 20:04:36 +00001207 if (assoc_req->channel == priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001208 goto done;
1209
David Woodhouse8642f1f2007-12-11 20:03:01 -05001210 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -05001211 /* Change mesh channel first; 21.p21 firmware won't let
1212 you change channel otherwise (even though it'll return
1213 an error to this */
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001214 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1215 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001216 }
1217
Dan Williamsef9a2642007-05-25 16:46:33 -04001218 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
David Woodhouse86062132007-12-13 00:32:36 -05001219 priv->curbssparams.channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -04001220
Dan Williams2dd4b262007-12-11 16:54:15 -05001221 ret = lbs_set_channel(priv, assoc_req->channel);
1222 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -05001223 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -04001224
Dan Williams2dd4b262007-12-11 16:54:15 -05001225 /* FIXME: shouldn't need to grab the channel _again_ after setting
1226 * it since the firmware is supposed to return the new channel, but
1227 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -05001228 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001229 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001230 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001231 goto done;
1232 }
Dan Williamsef9a2642007-05-25 16:46:33 -04001233
David Woodhouseaa21c002007-12-08 20:04:36 +00001234 if (assoc_req->channel != priv->curbssparams.channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -05001235 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -04001236 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001237 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -04001238 }
1239
1240 if ( assoc_req->secinfo.wep_enabled
1241 && (assoc_req->wep_keys[0].len
1242 || assoc_req->wep_keys[1].len
1243 || assoc_req->wep_keys[2].len
1244 || assoc_req->wep_keys[3].len)) {
1245 /* Make sure WEP keys are re-sent to firmware */
1246 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1247 }
1248
1249 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -05001250 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -04001251
David Woodhouse8642f1f2007-12-11 20:03:01 -05001252 restore_mesh:
1253 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001254 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1255 priv->curbssparams.channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001256
1257 done:
Dan Williamsef9a2642007-05-25 16:46:33 -04001258 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1259 return ret;
1260}
1261
1262
Holger Schurig69f90322007-11-23 15:43:44 +01001263static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -05001264 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001265{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001266 int i;
1267 int ret = 0;
1268
Holger Schurig9012b282007-05-25 11:27:16 -04001269 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001270
1271 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -05001272 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1273 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1274 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1275 else
1276 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001277
1278 if (ret)
1279 goto out;
1280
1281 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -04001282 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +01001283 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001284 else
Holger Schurigd9e97782008-03-12 16:06:43 +01001285 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -05001286
Holger Schurigc97329e2008-03-18 11:20:21 +01001287 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001288
David Woodhouseaa21c002007-12-08 20:04:36 +00001289 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001290
David Woodhouseaa21c002007-12-08 20:04:36 +00001291 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001292 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001293 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -05001294 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001295 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001296 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001297
David Woodhouseaa21c002007-12-08 20:04:36 +00001298 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001299
1300out:
Holger Schurig9012b282007-05-25 11:27:16 -04001301 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001302 return ret;
1303}
1304
Holger Schurig69f90322007-11-23 15:43:44 +01001305static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001306 struct assoc_request * assoc_req)
1307{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001308 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -05001309 uint16_t do_wpa;
1310 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001311
Holger Schurig9012b282007-05-25 11:27:16 -04001312 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001313
David Woodhouseaa21c002007-12-08 20:04:36 +00001314 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001315 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001316
Holger Schurigc97329e2008-03-18 11:20:21 +01001317 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001318
Dan Williams18c96c342007-06-18 12:01:12 -04001319 /* If RSN is already enabled, don't try to enable it again, since
1320 * ENABLE_RSN resets internal state machines and will clobber the
1321 * 4-way WPA handshake.
1322 */
1323
1324 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001325 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -04001326 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001327 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -04001328 goto out;
1329 }
1330
1331 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001332 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -04001333 if (do_wpa == rsn)
1334 goto out;
1335
1336 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001337 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -04001338
1339out:
Holger Schurig9012b282007-05-25 11:27:16 -04001340 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001341 return ret;
1342}
1343
1344
Holger Schurig69f90322007-11-23 15:43:44 +01001345static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001346 struct assoc_request * assoc_req)
1347{
1348 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -04001349 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001350
Holger Schurig9012b282007-05-25 11:27:16 -04001351 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001352
Dan Williams2bcde512007-10-03 10:37:45 -04001353 /* Work around older firmware bug where WPA unicast and multicast
1354 * keys must be set independently. Seen in SDIO parts with firmware
1355 * version 5.0.11p0.
1356 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001357
Dan Williams2bcde512007-10-03 10:37:45 -04001358 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1359 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +01001360 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001361 assoc_req->flags = flags;
1362 }
1363
1364 if (ret)
1365 goto out;
1366
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001367 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1368 sizeof(struct enc_key));
1369
Dan Williams2bcde512007-10-03 10:37:45 -04001370 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1371 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1372
David Woodhouse9e1228d2008-03-03 12:15:39 +01001373 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001374 assoc_req->flags = flags;
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001375
1376 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1377 sizeof(struct enc_key));
Dan Williams2bcde512007-10-03 10:37:45 -04001378 }
1379
1380out:
Holger Schurig9012b282007-05-25 11:27:16 -04001381 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001382 return ret;
1383}
1384
1385
Holger Schurig69f90322007-11-23 15:43:44 +01001386static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001387 struct assoc_request * assoc_req)
1388{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001389 int ret = 0;
1390
Holger Schurig9012b282007-05-25 11:27:16 -04001391 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001392
1393 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001394 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1395 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001396 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +00001397 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1398 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001399 }
1400
Holger Schurig9012b282007-05-25 11:27:16 -04001401 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001402 return ret;
1403}
1404
1405
David Woodhouseaa21c002007-12-08 20:04:36 +00001406static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001407 struct assoc_request * assoc_req)
1408{
Holger Schurig0765af42007-10-15 12:55:56 +02001409 int ret = 0;
1410
David Woodhouseaa21c002007-12-08 20:04:36 +00001411 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001412 return 0;
1413
Holger Schurig52507c22008-01-28 17:25:53 +01001414 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001415 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001416 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1417 ret = 1;
1418 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001419 }
1420
1421 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001422 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +02001423 lbs_deb_assoc("Deauthenticating due to new security\n");
1424 ret = 1;
1425 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001426 }
1427 }
1428
1429 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001430 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1431 ret = 1;
1432 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001433 }
1434
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001435 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001436 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1437 ret = 1;
1438 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001439 }
1440
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001441 /* FIXME: deal with 'auto' mode somehow */
1442 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001443 if (assoc_req->mode != IW_MODE_INFRA) {
1444 lbs_deb_assoc("Deauthenticating due to leaving "
1445 "infra mode\n");
1446 ret = 1;
1447 goto out;
1448 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001449 }
1450
Holger Schurig0765af42007-10-15 12:55:56 +02001451out:
1452 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +01001453 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001454}
1455
1456
David Woodhouseaa21c002007-12-08 20:04:36 +00001457static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001458 struct assoc_request * assoc_req)
1459{
Holger Schurig0765af42007-10-15 12:55:56 +02001460 lbs_deb_enter(LBS_DEB_ASSOC);
1461
David Woodhouseaa21c002007-12-08 20:04:36 +00001462 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001463 return 0;
1464
David Woodhouseaa21c002007-12-08 20:04:36 +00001465 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1466 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001467 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001468 return 1;
1469
1470 /* FIXME: deal with 'auto' mode somehow */
1471 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001472 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001473 return 1;
1474 }
1475
Dan Williamsef9a2642007-05-25 16:46:33 -04001476 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001477 if (assoc_req->channel != priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001478 return 1;
1479 }
1480
Holger Schurig0765af42007-10-15 12:55:56 +02001481 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001482 return 0;
1483}
1484
1485
Holger Schurig245bf202008-04-02 16:27:42 +02001486/**
1487 * @brief This function finds the best SSID in the Scan List
1488 *
1489 * Search the scan table for the best SSID that also matches the current
1490 * adapter network preference (infrastructure or adhoc)
1491 *
1492 * @param priv A pointer to struct lbs_private
1493 *
1494 * @return index in BSSID list
1495 */
1496static struct bss_descriptor *lbs_find_best_ssid_in_list(
1497 struct lbs_private *priv, uint8_t mode)
1498{
1499 uint8_t bestrssi = 0;
1500 struct bss_descriptor *iter_bss;
1501 struct bss_descriptor *best_bss = NULL;
1502
1503 lbs_deb_enter(LBS_DEB_SCAN);
1504
1505 mutex_lock(&priv->lock);
1506
1507 list_for_each_entry(iter_bss, &priv->network_list, list) {
1508 switch (mode) {
1509 case IW_MODE_INFRA:
1510 case IW_MODE_ADHOC:
1511 if (!is_network_compatible(priv, iter_bss, mode))
1512 break;
1513 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1514 break;
1515 bestrssi = SCAN_RSSI(iter_bss->rssi);
1516 best_bss = iter_bss;
1517 break;
1518 case IW_MODE_AUTO:
1519 default:
1520 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1521 break;
1522 bestrssi = SCAN_RSSI(iter_bss->rssi);
1523 best_bss = iter_bss;
1524 break;
1525 }
1526 }
1527
1528 mutex_unlock(&priv->lock);
1529 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1530 return best_bss;
1531}
1532
1533/**
1534 * @brief Find the best AP
1535 *
1536 * Used from association worker.
1537 *
1538 * @param priv A pointer to struct lbs_private structure
1539 * @param pSSID A pointer to AP's ssid
1540 *
1541 * @return 0--success, otherwise--fail
1542 */
1543static int lbs_find_best_network_ssid(struct lbs_private *priv,
1544 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1545 uint8_t *out_mode)
1546{
1547 int ret = -1;
1548 struct bss_descriptor *found;
1549
1550 lbs_deb_enter(LBS_DEB_SCAN);
1551
1552 priv->scan_ssid_len = 0;
1553 lbs_scan_networks(priv, 1);
1554 if (priv->surpriseremoved)
1555 goto out;
1556
1557 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1558 if (found && (found->ssid_len > 0)) {
1559 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1560 *out_ssid_len = found->ssid_len;
1561 *out_mode = found->mode;
1562 ret = 0;
1563 }
1564
1565out:
1566 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1567 return ret;
1568}
1569
1570
Holger Schurig10078322007-11-15 18:05:47 -05001571void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001572{
Holger Schurig69f90322007-11-23 15:43:44 +01001573 struct lbs_private *priv = container_of(work, struct lbs_private,
1574 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001575 struct assoc_request * assoc_req = NULL;
1576 int ret = 0;
1577 int find_any_ssid = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001578 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001579
Holger Schurig9012b282007-05-25 11:27:16 -04001580 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001581
David Woodhouseaa21c002007-12-08 20:04:36 +00001582 mutex_lock(&priv->lock);
1583 assoc_req = priv->pending_assoc_req;
1584 priv->pending_assoc_req = NULL;
1585 priv->in_progress_assoc_req = assoc_req;
1586 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001587
Holger Schurig9012b282007-05-25 11:27:16 -04001588 if (!assoc_req)
1589 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001590
Holger Schurig0765af42007-10-15 12:55:56 +02001591 lbs_deb_assoc(
1592 "Association Request:\n"
1593 " flags: 0x%08lx\n"
1594 " SSID: '%s'\n"
1595 " chann: %d\n"
1596 " band: %d\n"
1597 " mode: %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001598 " BSSID: %pM\n"
Holger Schurig0765af42007-10-15 12:55:56 +02001599 " secinfo: %s%s%s\n"
1600 " auth_mode: %d\n",
1601 assoc_req->flags,
John W. Linville9387b7c2008-09-30 20:59:05 -04001602 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Holger Schurig0765af42007-10-15 12:55:56 +02001603 assoc_req->channel, assoc_req->band, assoc_req->mode,
Johannes Berge1749612008-10-27 15:59:26 -07001604 assoc_req->bssid,
Holger Schurig0765af42007-10-15 12:55:56 +02001605 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1606 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1607 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1608 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001609
1610 /* If 'any' SSID was specified, find an SSID to associate with */
1611 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001612 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001613 find_any_ssid = 1;
1614
1615 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1616 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001617 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1618 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001619 find_any_ssid = 0;
1620 }
1621
1622 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02001623 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001624
Holger Schurig10078322007-11-15 18:05:47 -05001625 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001626 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001627 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001628 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001629 ret = -ENETUNREACH;
1630 goto out;
1631 }
1632
1633 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04001634 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001635 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1636 assoc_req->mode = new_mode;
1637 }
1638 }
1639
1640 /*
1641 * Check if the attributes being changing require deauthentication
1642 * from the currently associated infrastructure access point.
1643 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001644 if (priv->mode == IW_MODE_INFRA) {
1645 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04001646 ret = lbs_cmd_80211_deauthenticate(priv,
1647 priv->curbssparams.bssid,
1648 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001649 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001650 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001651 "configuration request failed: %d\n",
1652 ret);
1653 }
1654 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001655 } else if (priv->mode == IW_MODE_ADHOC) {
1656 if (should_stop_adhoc(priv, assoc_req)) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001657 ret = lbs_adhoc_stop(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001658 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001659 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001660 "new configuration request failed: %d\n",
1661 ret);
1662 }
1663
1664 }
1665 }
1666
1667 /* Send the various configuration bits to the firmware */
1668 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1669 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001670 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001671 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001672 }
1673
Dan Williamsef9a2642007-05-25 16:46:33 -04001674 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1675 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001676 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04001677 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04001678 }
1679
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001680 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1681 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1682 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001683 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001684 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001685 }
1686
1687 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1688 ret = assoc_helper_secinfo(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_WPA_IE, &assoc_req->flags)) {
1694 ret = assoc_helper_wpa_ie(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_MCAST_KEY, &assoc_req->flags)
1700 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1701 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001702 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001703 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001704 }
1705
1706 /* SSID/BSSID should be the _last_ config option set, because they
1707 * trigger the association attempt.
1708 */
1709 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1710 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1711 int success = 1;
1712
1713 ret = assoc_helper_associate(priv, assoc_req);
1714 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01001715 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001716 ret);
1717 success = 0;
1718 }
1719
David Woodhouseaa21c002007-12-08 20:04:36 +00001720 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01001721 lbs_deb_assoc("ASSOC: association unsuccessful, "
1722 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001723 success = 0;
1724 }
1725
1726 if (success) {
Johannes Berge1749612008-10-27 15:59:26 -07001727 lbs_deb_assoc("associated to %pM\n",
1728 priv->curbssparams.bssid);
Holger Schurig10078322007-11-15 18:05:47 -05001729 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04001730 CMD_802_11_RSSI,
1731 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001732 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001733 ret = -1;
1734 }
1735 }
1736
1737out:
1738 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001739 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001740 ret);
1741 }
Dan Williamse76850d2007-05-25 17:09:41 -04001742
David Woodhouseaa21c002007-12-08 20:04:36 +00001743 mutex_lock(&priv->lock);
1744 priv->in_progress_assoc_req = NULL;
1745 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001746 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04001747
1748done:
1749 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001750}
1751
1752
1753/*
1754 * Caller MUST hold any necessary locks
1755 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001756struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001757{
1758 struct assoc_request * assoc_req;
1759
Holger Schurig0765af42007-10-15 12:55:56 +02001760 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00001761 if (!priv->pending_assoc_req) {
1762 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04001763 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00001764 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001765 lbs_pr_info("Not enough memory to allocate association"
1766 " request!\n");
1767 return NULL;
1768 }
1769 }
1770
1771 /* Copy current configuration attributes to the association request,
1772 * but don't overwrite any that are already set.
1773 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001774 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001775 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001776 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001777 IW_ESSID_MAX_SIZE);
David Woodhouseaa21c002007-12-08 20:04:36 +00001778 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001779 }
1780
1781 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001782 assoc_req->channel = priv->curbssparams.channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001783
Dan Williamse76850d2007-05-25 17:09:41 -04001784 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001785 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04001786
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001787 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001788 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001789
1790 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001791 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001792 ETH_ALEN);
1793 }
1794
1795 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1796 int i;
1797 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001798 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04001799 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001800 }
1801 }
1802
1803 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001804 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001805
1806 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001807 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001808 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001809 }
1810
1811 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001812 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_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_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001817 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001818 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001819 }
1820
1821 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001822 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001823 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001824 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001825 }
1826
Holger Schurig0765af42007-10-15 12:55:56 +02001827 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001828 return assoc_req;
1829}
Holger Schurig697900a2008-04-02 16:27:10 +02001830
1831
1832/**
Dan Williams191bb402008-08-21 17:46:18 -04001833 * @brief Deauthenticate from a specific BSS
1834 *
1835 * @param priv A pointer to struct lbs_private structure
1836 * @param bssid The specific BSS to deauthenticate from
1837 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1838 *
1839 * @return 0 on success, error on failure
1840 */
1841int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1842 u16 reason)
Holger Schurig697900a2008-04-02 16:27:10 +02001843{
Dan Williams191bb402008-08-21 17:46:18 -04001844 struct cmd_ds_802_11_deauthenticate cmd;
1845 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001846
1847 lbs_deb_enter(LBS_DEB_JOIN);
1848
Dan Williams191bb402008-08-21 17:46:18 -04001849 memset(&cmd, 0, sizeof(cmd));
1850 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1851 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1852 cmd.reasoncode = cpu_to_le16(reason);
Holger Schurig697900a2008-04-02 16:27:10 +02001853
Dan Williams191bb402008-08-21 17:46:18 -04001854 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001855
Dan Williams191bb402008-08-21 17:46:18 -04001856 /* Clean up everything even if there was an error; can't assume that
1857 * we're still authenticated to the AP after trying to deauth.
1858 */
1859 lbs_mac_event_disconnected(priv);
Holger Schurig697900a2008-04-02 16:27:10 +02001860
1861 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04001862 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001863}
1864