blob: 1902b6f0b78cb5a32c57e2ed4be050dbcf115a97 [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>
Roel Kluin57921c32009-07-28 12:05:00 +02004#include <linux/kernel.h>
Dan Williams3cf20932007-05-25 17:28:30 -04005#include <linux/etherdevice.h>
Johannes Berg2c7060022008-10-30 22:09:54 +01006#include <linux/ieee80211.h>
7#include <linux/if_arp.h>
John W. Linville7e272fc2008-09-24 18:13:14 -04008#include <net/lib80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02009
10#include "assoc.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020011#include "decl.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020012#include "host.h"
Holger Schurig245bf202008-04-02 16:27:42 +020013#include "scan.h"
Dan Williams2dd4b262007-12-11 16:54:15 -050014#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020015
Ihar Hrachyshka5a6e0432008-01-25 14:15:00 +010016static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
17 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
18static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
19 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020020
Dan Williamsbe0d76e2009-05-22 20:05:25 -040021/* The firmware needs the following bits masked out of the beacon-derived
22 * capability field when associating/joining to a BSS:
23 * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
Holger Schurig697900a2008-04-02 16:27:10 +020024 */
25#define CAPINFO_MASK (~(0xda00))
26
27
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040028/**
29 * @brief This function finds common rates between rates and card rates.
30 *
31 * It will fill common rates in rates as output if found.
32 *
33 * NOTE: Setting the MSB of the basic rates need to be taken
34 * care, either before or after calling this function
35 *
36 * @param priv A pointer to struct lbs_private structure
37 * @param rates the buffer which keeps input and output
38 * @param rates_size the size of rate1 buffer; new size of buffer on return
39 *
40 * @return 0 on success, or -1 on error
41 */
42static int get_common_rates(struct lbs_private *priv,
43 u8 *rates,
44 u16 *rates_size)
45{
46 u8 *card_rates = lbs_bg_rates;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040047 int ret = 0, i, j;
Roel Kluin57921c32009-07-28 12:05:00 +020048 u8 tmp[(ARRAY_SIZE(lbs_bg_rates) - 1) * (*rates_size - 1)];
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040049 size_t tmp_size = 0;
50
51 /* For each rate in card_rates that exists in rate1, copy to tmp */
Roel Kluin57921c32009-07-28 12:05:00 +020052 for (i = 0; i < ARRAY_SIZE(lbs_bg_rates) && card_rates[i]; i++) {
53 for (j = 0; j < *rates_size && rates[j]; j++) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040054 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);
Roel Kluin57921c32009-07-28 12:05:00 +020060 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates,
61 ARRAY_SIZE(lbs_bg_rates));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040062 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
63 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
64
65 if (!priv->enablehwauto) {
66 for (i = 0; i < tmp_size; i++) {
67 if (tmp[i] == priv->cur_rate)
68 goto done;
69 }
70 lbs_pr_alert("Previously set fixed data rate %#x isn't "
71 "compatible with the network.\n", priv->cur_rate);
72 ret = -1;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040073 }
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040074done:
75 memset(rates, 0, *rates_size);
76 *rates_size = min_t(int, tmp_size, *rates_size);
77 memcpy(rates, tmp, *rates_size);
78 return ret;
79}
80
81
82/**
83 * @brief Sets the MSB on basic rates as the firmware requires
84 *
85 * Scan through an array and set the MSB for basic data rates.
86 *
87 * @param rates buffer of data rates
88 * @param len size of buffer
89 */
90static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
91{
92 int i;
93
94 for (i = 0; i < len; i++) {
95 if (rates[i] == 0x02 || rates[i] == 0x04 ||
96 rates[i] == 0x0b || rates[i] == 0x16)
97 rates[i] |= 0x80;
98 }
99}
100
Holger Schurig697900a2008-04-02 16:27:10 +0200101
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400102static u8 iw_auth_to_ieee_auth(u8 auth)
103{
104 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
105 return 0x00;
106 else if (auth == IW_AUTH_ALG_SHARED_KEY)
107 return 0x01;
108 else if (auth == IW_AUTH_ALG_LEAP)
109 return 0x80;
110
111 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
112 return 0;
113}
114
115/**
116 * @brief This function prepares the authenticate command. AUTHENTICATE only
117 * sets the authentication suite for future associations, as the firmware
118 * handles authentication internally during the ASSOCIATE command.
119 *
120 * @param priv A pointer to struct lbs_private structure
121 * @param bssid The peer BSSID with which to authenticate
122 * @param auth The authentication mode to use (from wireless.h)
123 *
124 * @return 0 or -1
125 */
126static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
127{
128 struct cmd_ds_802_11_authenticate cmd;
129 int ret = -1;
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400130
131 lbs_deb_enter(LBS_DEB_JOIN);
132
133 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
134 memcpy(cmd.bssid, bssid, ETH_ALEN);
135
136 cmd.authtype = iw_auth_to_ieee_auth(auth);
137
Johannes Berge91d8332009-07-15 17:21:41 +0200138 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400139
140 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
141
142 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
143 return ret;
144}
145
Dan Williams822ac032009-05-22 20:07:14 -0400146
147static int lbs_assoc_post(struct lbs_private *priv,
148 struct cmd_ds_802_11_associate_response *resp)
149{
150 int ret = 0;
151 union iwreq_data wrqu;
152 struct bss_descriptor *bss;
153 u16 status_code;
154
155 lbs_deb_enter(LBS_DEB_ASSOC);
156
157 if (!priv->in_progress_assoc_req) {
158 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
159 ret = -1;
160 goto done;
161 }
162 bss = &priv->in_progress_assoc_req->bss;
163
164 /*
165 * Older FW versions map the IEEE 802.11 Status Code in the association
166 * response to the following values returned in resp->statuscode:
167 *
168 * IEEE Status Code Marvell Status Code
169 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
170 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
171 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
172 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
173 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
174 * others -> 0x0003 ASSOC_RESULT_REFUSED
175 *
176 * Other response codes:
177 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
178 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
179 * association response from the AP)
180 */
181
182 status_code = le16_to_cpu(resp->statuscode);
183 if (priv->fwrelease < 0x09000000) {
184 switch (status_code) {
185 case 0x00:
186 break;
187 case 0x01:
188 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
189 break;
190 case 0x02:
191 lbs_deb_assoc("ASSOC_RESP: internal timer "
192 "expired while waiting for the AP\n");
193 break;
194 case 0x03:
195 lbs_deb_assoc("ASSOC_RESP: association "
196 "refused by AP\n");
197 break;
198 case 0x04:
199 lbs_deb_assoc("ASSOC_RESP: authentication "
200 "refused by AP\n");
201 break;
202 default:
203 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
204 " unknown\n", status_code);
205 break;
206 }
207 } else {
208 /* v9+ returns the AP's association response */
209 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
210 }
211
212 if (status_code) {
213 lbs_mac_event_disconnected(priv);
214 ret = -1;
215 goto done;
216 }
217
218 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
219 (void *) (resp + sizeof (resp->hdr)),
220 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
221
222 /* Send a Media Connected event, according to the Spec */
223 priv->connect_status = LBS_CONNECTED;
224
225 /* Update current SSID and BSSID */
226 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
227 priv->curbssparams.ssid_len = bss->ssid_len;
228 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
229
230 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
231 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
232
233 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
234 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
235 priv->nextSNRNF = 0;
236 priv->numSNRNF = 0;
237
238 netif_carrier_on(priv->dev);
239 if (!priv->tx_pending_len)
240 netif_wake_queue(priv->dev);
241
242 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
243 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
244 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
245
246done:
247 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
248 return ret;
249}
250
251/**
252 * @brief This function prepares an association-class command.
253 *
254 * @param priv A pointer to struct lbs_private structure
255 * @param assoc_req The association request describing the BSS to associate
256 * or reassociate with
257 * @param command The actual command, either CMD_802_11_ASSOCIATE or
258 * CMD_802_11_REASSOCIATE
259 *
260 * @return 0 or -1
261 */
262static int lbs_associate(struct lbs_private *priv,
263 struct assoc_request *assoc_req,
264 u16 command)
265{
266 struct cmd_ds_802_11_associate cmd;
267 int ret = 0;
268 struct bss_descriptor *bss = &assoc_req->bss;
269 u8 *pos = &(cmd.iebuf[0]);
270 u16 tmpcap, tmplen, tmpauth;
271 struct mrvl_ie_ssid_param_set *ssid;
272 struct mrvl_ie_ds_param_set *ds;
273 struct mrvl_ie_cf_param_set *cf;
274 struct mrvl_ie_rates_param_set *rates;
275 struct mrvl_ie_rsn_param_set *rsn;
276 struct mrvl_ie_auth_type *auth;
277
278 lbs_deb_enter(LBS_DEB_ASSOC);
279
280 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
281 (command != CMD_802_11_REASSOCIATE));
282
283 memset(&cmd, 0, sizeof(cmd));
284 cmd.hdr.command = cpu_to_le16(command);
285
286 /* Fill in static fields */
287 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
288 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
289
290 /* Capability info */
291 tmpcap = (bss->capability & CAPINFO_MASK);
292 if (bss->mode == IW_MODE_INFRA)
293 tmpcap |= WLAN_CAPABILITY_ESS;
294 cmd.capability = cpu_to_le16(tmpcap);
295 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
296
297 /* SSID */
298 ssid = (struct mrvl_ie_ssid_param_set *) pos;
299 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
300 tmplen = bss->ssid_len;
301 ssid->header.len = cpu_to_le16(tmplen);
302 memcpy(ssid->ssid, bss->ssid, tmplen);
303 pos += sizeof(ssid->header) + tmplen;
304
305 ds = (struct mrvl_ie_ds_param_set *) pos;
306 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
307 ds->header.len = cpu_to_le16(1);
308 ds->channel = bss->phy.ds.channel;
309 pos += sizeof(ds->header) + 1;
310
311 cf = (struct mrvl_ie_cf_param_set *) pos;
312 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
313 tmplen = sizeof(*cf) - sizeof (cf->header);
314 cf->header.len = cpu_to_le16(tmplen);
315 /* IE payload should be zeroed, firmware fills it in for us */
316 pos += sizeof(*cf);
317
318 rates = (struct mrvl_ie_rates_param_set *) pos;
319 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
320 memcpy(&rates->rates, &bss->rates, MAX_RATES);
Roel Kluin57921c32009-07-28 12:05:00 +0200321 tmplen = min_t(u16, ARRAY_SIZE(rates->rates), MAX_RATES);
Dan Williams822ac032009-05-22 20:07:14 -0400322 if (get_common_rates(priv, rates->rates, &tmplen)) {
323 ret = -1;
324 goto done;
325 }
326 pos += sizeof(rates->header) + tmplen;
327 rates->header.len = cpu_to_le16(tmplen);
328 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
329
330 /* Copy the infra. association rates into Current BSS state structure */
331 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
332 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
333
334 /* Set MSB on basic rates as the firmware requires, but _after_
335 * copying to current bss rates.
336 */
337 lbs_set_basic_rate_flags(rates->rates, tmplen);
338
339 /* Firmware v9+ indicate authentication suites as a TLV */
340 if (priv->fwrelease >= 0x09000000) {
Dan Williams822ac032009-05-22 20:07:14 -0400341 auth = (struct mrvl_ie_auth_type *) pos;
342 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
343 auth->header.len = cpu_to_le16(2);
344 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
345 auth->auth = cpu_to_le16(tmpauth);
346 pos += sizeof(auth->header) + 2;
347
Johannes Berge91d8332009-07-15 17:21:41 +0200348 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
349 bss->bssid, priv->secinfo.auth_mode);
Dan Williams822ac032009-05-22 20:07:14 -0400350 }
351
352 /* WPA/WPA2 IEs */
353 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
354 rsn = (struct mrvl_ie_rsn_param_set *) pos;
355 /* WPA_IE or WPA2_IE */
356 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
357 tmplen = (u16) assoc_req->wpa_ie[1];
358 rsn->header.len = cpu_to_le16(tmplen);
359 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
360 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
361 sizeof(rsn->header) + tmplen);
362 pos += sizeof(rsn->header) + tmplen;
363 }
364
365 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
366 (u16)(pos - (u8 *) &cmd.iebuf));
367
368 /* update curbssparams */
369 priv->curbssparams.channel = bss->phy.ds.channel;
370
371 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
372 ret = -1;
373 goto done;
374 }
375
376 ret = lbs_cmd_with_response(priv, command, &cmd);
377 if (ret == 0) {
378 ret = lbs_assoc_post(priv,
379 (struct cmd_ds_802_11_associate_response *) &cmd);
380 }
381
382done:
383 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
384 return ret;
385}
386
Holger Schurig697900a2008-04-02 16:27:10 +0200387/**
388 * @brief Associate to a specific BSS discovered in a scan
389 *
390 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400391 * @param assoc_req The association request describing the BSS to associate with
Holger Schurig697900a2008-04-02 16:27:10 +0200392 *
393 * @return 0-success, otherwise fail
394 */
Dan Williams822ac032009-05-22 20:07:14 -0400395static int lbs_try_associate(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200396 struct assoc_request *assoc_req)
397{
398 int ret;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400399 u8 preamble = RADIO_PREAMBLE_LONG;
Holger Schurig697900a2008-04-02 16:27:10 +0200400
401 lbs_deb_enter(LBS_DEB_ASSOC);
402
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400403 /* FW v9 and higher indicate authentication suites as a TLV in the
404 * association command, not as a separate authentication command.
405 */
406 if (priv->fwrelease < 0x09000000) {
407 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
408 priv->secinfo.auth_mode);
409 if (ret)
410 goto out;
411 }
Holger Schurig697900a2008-04-02 16:27:10 +0200412
Dan Williamsd5db2df2008-08-21 17:51:07 -0400413 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig697900a2008-04-02 16:27:10 +0200414 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
415 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
Dan Williamsd5db2df2008-08-21 17:51:07 -0400416 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200417
Dan Williamsd5db2df2008-08-21 17:51:07 -0400418 ret = lbs_set_radio(priv, preamble, 1);
419 if (ret)
420 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200421
Dan Williams822ac032009-05-22 20:07:14 -0400422 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
Holger Schurig697900a2008-04-02 16:27:10 +0200423
Dan Williamsd5db2df2008-08-21 17:51:07 -0400424out:
Holger Schurig697900a2008-04-02 16:27:10 +0200425 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
426 return ret;
427}
428
Dan Williams822ac032009-05-22 20:07:14 -0400429static int lbs_adhoc_post(struct lbs_private *priv,
430 struct cmd_ds_802_11_ad_hoc_result *resp)
431{
432 int ret = 0;
433 u16 command = le16_to_cpu(resp->hdr.command);
434 u16 result = le16_to_cpu(resp->hdr.result);
435 union iwreq_data wrqu;
436 struct bss_descriptor *bss;
437 DECLARE_SSID_BUF(ssid);
438
439 lbs_deb_enter(LBS_DEB_JOIN);
440
441 if (!priv->in_progress_assoc_req) {
442 lbs_deb_join("ADHOC_RESP: no in-progress association "
443 "request\n");
444 ret = -1;
445 goto done;
446 }
447 bss = &priv->in_progress_assoc_req->bss;
448
449 /*
450 * Join result code 0 --> SUCCESS
451 */
452 if (result) {
453 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
454 if (priv->connect_status == LBS_CONNECTED)
455 lbs_mac_event_disconnected(priv);
456 ret = -1;
457 goto done;
458 }
459
460 /* Send a Media Connected event, according to the Spec */
461 priv->connect_status = LBS_CONNECTED;
462
463 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
464 /* Update the created network descriptor with the new BSSID */
465 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
466 }
467
468 /* Set the BSSID from the joined/started descriptor */
469 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
470
471 /* Set the new SSID to current SSID */
472 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
473 priv->curbssparams.ssid_len = bss->ssid_len;
474
475 netif_carrier_on(priv->dev);
476 if (!priv->tx_pending_len)
477 netif_wake_queue(priv->dev);
478
479 memset(&wrqu, 0, sizeof(wrqu));
480 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
481 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
482 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
483
484 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
485 print_ssid(ssid, bss->ssid, bss->ssid_len),
486 priv->curbssparams.bssid,
487 priv->curbssparams.channel);
488
489done:
490 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
491 return ret;
492}
493
Holger Schurig697900a2008-04-02 16:27:10 +0200494/**
495 * @brief Join an adhoc network found in a previous scan
496 *
497 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400498 * @param assoc_req The association request describing the BSS to join
Holger Schurig697900a2008-04-02 16:27:10 +0200499 *
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400500 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200501 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400502static int lbs_adhoc_join(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200503 struct assoc_request *assoc_req)
504{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400505 struct cmd_ds_802_11_ad_hoc_join cmd;
Holger Schurig697900a2008-04-02 16:27:10 +0200506 struct bss_descriptor *bss = &assoc_req->bss;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400507 u8 preamble = RADIO_PREAMBLE_LONG;
John W. Linville9387b7c2008-09-30 20:59:05 -0400508 DECLARE_SSID_BUF(ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400509 u16 ratesize = 0;
510 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400511
512 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200513
514 lbs_deb_join("current SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400515 print_ssid(ssid, priv->curbssparams.ssid,
Holger Schurig697900a2008-04-02 16:27:10 +0200516 priv->curbssparams.ssid_len),
517 priv->curbssparams.ssid_len);
518 lbs_deb_join("requested ssid '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400519 print_ssid(ssid, bss->ssid, bss->ssid_len),
Holger Schurig697900a2008-04-02 16:27:10 +0200520 bss->ssid_len);
521
522 /* check if the requested SSID is already joined */
523 if (priv->curbssparams.ssid_len &&
524 !lbs_ssid_cmp(priv->curbssparams.ssid,
525 priv->curbssparams.ssid_len,
526 bss->ssid, bss->ssid_len) &&
527 (priv->mode == IW_MODE_ADHOC) &&
528 (priv->connect_status == LBS_CONNECTED)) {
529 union iwreq_data wrqu;
530
531 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
532 "current, not attempting to re-join");
533
534 /* Send the re-association event though, because the association
535 * request really was successful, even if just a null-op.
536 */
537 memset(&wrqu, 0, sizeof(wrqu));
538 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
539 ETH_ALEN);
540 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
541 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
542 goto out;
543 }
544
Dan Williamsd5db2df2008-08-21 17:51:07 -0400545 /* Use short preamble only when both the BSS and firmware support it */
546 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
547 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig697900a2008-04-02 16:27:10 +0200548 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400549 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200550 }
551
Dan Williamsd5db2df2008-08-21 17:51:07 -0400552 ret = lbs_set_radio(priv, preamble, 1);
553 if (ret)
554 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200555
556 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
557 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
558
559 priv->adhoccreate = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400560 priv->curbssparams.channel = bss->channel;
Holger Schurig697900a2008-04-02 16:27:10 +0200561
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400562 /* Build the join command */
563 memset(&cmd, 0, sizeof(cmd));
564 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
565
566 cmd.bss.type = CMD_BSS_TYPE_IBSS;
567 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
568
569 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
570 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
571
Dan Williams5fd164e2009-05-22 20:01:21 -0400572 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400573
Dan Williams5fd164e2009-05-22 20:01:21 -0400574 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
575 sizeof(struct ieee_ie_ibss_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400576
577 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
578 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
579 bss->capability, CAPINFO_MASK);
580
581 /* information on BSSID descriptor passed to FW */
Johannes Berge1749612008-10-27 15:59:26 -0700582 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
583 cmd.bss.bssid, cmd.bss.ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400584
585 /* Only v8 and below support setting these */
586 if (priv->fwrelease < 0x09000000) {
587 /* failtimeout */
588 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
589 /* probedelay */
590 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
591 }
592
593 /* Copy Data rates from the rates recorded in scan response */
594 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
Roel Kluin57921c32009-07-28 12:05:00 +0200595 ratesize = min_t(u16, ARRAY_SIZE(cmd.bss.rates), MAX_RATES);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400596 memcpy(cmd.bss.rates, bss->rates, ratesize);
597 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
598 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
599 ret = -1;
600 goto out;
601 }
602
603 /* Copy the ad-hoc creation rates into Current BSS state structure */
604 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
605 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
606
607 /* Set MSB on basic rates as the firmware requires, but _after_
608 * copying to current bss rates.
609 */
610 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
611
Dan Williams5fd164e2009-05-22 20:01:21 -0400612 cmd.bss.ibss.atimwindow = bss->atimwindow;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400613
614 if (assoc_req->secinfo.wep_enabled) {
615 u16 tmp = le16_to_cpu(cmd.bss.capability);
616 tmp |= WLAN_CAPABILITY_PRIVACY;
617 cmd.bss.capability = cpu_to_le16(tmp);
618 }
619
620 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
621 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
622
623 /* wake up first */
624 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
625 CMD_ACT_SET, 0, 0,
626 &local_ps_mode);
627 if (ret) {
628 ret = -1;
629 goto out;
630 }
631 }
632
633 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
634 ret = -1;
635 goto out;
636 }
637
638 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
Dan Williams822ac032009-05-22 20:07:14 -0400639 if (ret == 0) {
640 ret = lbs_adhoc_post(priv,
641 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
642 }
Holger Schurig697900a2008-04-02 16:27:10 +0200643
644out:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400645 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200646 return ret;
647}
648
649/**
650 * @brief Start an Adhoc Network
651 *
652 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400653 * @param assoc_req The association request describing the BSS to start
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400654 *
655 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200656 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400657static int lbs_adhoc_start(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200658 struct assoc_request *assoc_req)
659{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400660 struct cmd_ds_802_11_ad_hoc_start cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400661 u8 preamble = RADIO_PREAMBLE_LONG;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400662 size_t ratesize = 0;
663 u16 tmpcap = 0;
664 int ret = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400665 DECLARE_SSID_BUF(ssid);
Dan Williamsd5db2df2008-08-21 17:51:07 -0400666
667 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200668
Holger Schurig697900a2008-04-02 16:27:10 +0200669 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400670 lbs_deb_join("ADHOC_START: Will use short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400671 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200672 }
673
Dan Williamsd5db2df2008-08-21 17:51:07 -0400674 ret = lbs_set_radio(priv, preamble, 1);
675 if (ret)
676 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200677
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400678 /* Build the start command */
679 memset(&cmd, 0, sizeof(cmd));
680 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurig697900a2008-04-02 16:27:10 +0200681
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400682 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
683
684 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400685 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400686 assoc_req->ssid_len);
687
688 cmd.bsstype = CMD_BSS_TYPE_IBSS;
689
690 if (priv->beacon_period == 0)
691 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
692 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
693
694 WARN_ON(!assoc_req->channel);
695
696 /* set Physical parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400697 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
698 cmd.ds.header.len = 1;
Dan Williams5fd164e2009-05-22 20:01:21 -0400699 cmd.ds.channel = assoc_req->channel;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400700
701 /* set IBSS parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400702 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
703 cmd.ibss.header.len = 2;
Dan Williams5fd164e2009-05-22 20:01:21 -0400704 cmd.ibss.atimwindow = cpu_to_le16(0);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400705
706 /* set capability info */
707 tmpcap = WLAN_CAPABILITY_IBSS;
Dan Williams2fa7a982009-05-22 20:09:58 -0400708 if (assoc_req->secinfo.wep_enabled ||
709 assoc_req->secinfo.WPAenabled ||
710 assoc_req->secinfo.WPA2enabled) {
711 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400712 tmpcap |= WLAN_CAPABILITY_PRIVACY;
713 } else
Dan Williams2fa7a982009-05-22 20:09:58 -0400714 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400715
716 cmd.capability = cpu_to_le16(tmpcap);
717
718 /* Only v8 and below support setting probe delay */
719 if (priv->fwrelease < 0x09000000)
720 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
721
722 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
723 memcpy(cmd.rates, lbs_bg_rates, ratesize);
724
725 /* Copy the ad-hoc creating rates into Current BSS state structure */
726 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
727 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
728
729 /* Set MSB on basic rates as the firmware requires, but _after_
730 * copying to current bss rates.
731 */
732 lbs_set_basic_rate_flags(cmd.rates, ratesize);
733
734 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
735 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
736
737 if (lbs_create_dnld_countryinfo_11d(priv)) {
738 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
739 ret = -1;
740 goto out;
741 }
742
743 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
744 assoc_req->channel, assoc_req->band);
745
746 priv->adhoccreate = 1;
747 priv->mode = IW_MODE_ADHOC;
748
749 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
750 if (ret == 0)
Dan Williams822ac032009-05-22 20:07:14 -0400751 ret = lbs_adhoc_post(priv,
752 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200753
Dan Williamsd5db2df2008-08-21 17:51:07 -0400754out:
755 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200756 return ret;
757}
758
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400759/**
760 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
761 *
762 * @param priv A pointer to struct lbs_private structure
763 * @return 0 on success, or an error
764 */
765int lbs_adhoc_stop(struct lbs_private *priv)
Holger Schurig697900a2008-04-02 16:27:10 +0200766{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400767 struct cmd_ds_802_11_ad_hoc_stop cmd;
768 int ret;
769
770 lbs_deb_enter(LBS_DEB_JOIN);
771
772 memset(&cmd, 0, sizeof (cmd));
773 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
774
775 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
776
777 /* Clean up everything even if there was an error */
778 lbs_mac_event_disconnected(priv);
779
780 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
781 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +0200782}
Dan Williamse76850d2007-05-25 17:09:41 -0400783
Holger Schurig245bf202008-04-02 16:27:42 +0200784static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
785 struct bss_descriptor *match_bss)
786{
787 if (!secinfo->wep_enabled && !secinfo->WPAenabled
788 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100789 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
790 && match_bss->rsn_ie[0] != WLAN_EID_RSN
Holger Schurig245bf202008-04-02 16:27:42 +0200791 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
792 return 1;
793 else
794 return 0;
795}
796
797static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
798 struct bss_descriptor *match_bss)
799{
800 if (secinfo->wep_enabled && !secinfo->WPAenabled
801 && !secinfo->WPA2enabled
802 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
803 return 1;
804 else
805 return 0;
806}
807
808static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
809 struct bss_descriptor *match_bss)
810{
811 if (!secinfo->wep_enabled && secinfo->WPAenabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100812 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
Holger Schurig245bf202008-04-02 16:27:42 +0200813 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
814 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
815 )
816 return 1;
817 else
818 return 0;
819}
820
821static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
822 struct bss_descriptor *match_bss)
823{
824 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
Johannes Berg2c7060022008-10-30 22:09:54 +0100825 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200826 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
827 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
828 )
829 return 1;
830 else
831 return 0;
832}
833
834static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
835 struct bss_descriptor *match_bss)
836{
837 if (!secinfo->wep_enabled && !secinfo->WPAenabled
838 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100839 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
840 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200841 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
842 return 1;
843 else
844 return 0;
845}
846
847/**
848 * @brief Check if a scanned network compatible with the driver settings
849 *
850 * WEP WPA WPA2 ad-hoc encrypt Network
851 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
852 * 0 0 0 0 NONE 0 0 0 yes No security
853 * 1 0 0 0 NONE 1 0 0 yes Static WEP
854 * 0 1 0 0 x 1x 1 x yes WPA
855 * 0 0 1 0 x 1x x 1 yes WPA2
856 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
857 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
858 *
859 *
860 * @param priv A pointer to struct lbs_private
861 * @param index Index in scantable to check against current driver settings
862 * @param mode Network mode: Infrastructure or IBSS
863 *
864 * @return Index in scantable, or error code if negative
865 */
866static int is_network_compatible(struct lbs_private *priv,
867 struct bss_descriptor *bss, uint8_t mode)
868{
869 int matched = 0;
870
871 lbs_deb_enter(LBS_DEB_SCAN);
872
873 if (bss->mode != mode)
874 goto done;
875
876 matched = match_bss_no_security(&priv->secinfo, bss);
877 if (matched)
878 goto done;
879 matched = match_bss_static_wep(&priv->secinfo, bss);
880 if (matched)
881 goto done;
882 matched = match_bss_wpa(&priv->secinfo, bss);
883 if (matched) {
884 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
885 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
886 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
887 priv->secinfo.wep_enabled ? "e" : "d",
888 priv->secinfo.WPAenabled ? "e" : "d",
889 priv->secinfo.WPA2enabled ? "e" : "d",
890 (bss->capability & WLAN_CAPABILITY_PRIVACY));
891 goto done;
892 }
893 matched = match_bss_wpa2(&priv->secinfo, bss);
894 if (matched) {
895 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
896 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
897 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
898 priv->secinfo.wep_enabled ? "e" : "d",
899 priv->secinfo.WPAenabled ? "e" : "d",
900 priv->secinfo.WPA2enabled ? "e" : "d",
901 (bss->capability & WLAN_CAPABILITY_PRIVACY));
902 goto done;
903 }
904 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
905 if (matched) {
906 lbs_deb_scan("is_network_compatible() dynamic WEP: "
907 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
908 bss->wpa_ie[0], bss->rsn_ie[0],
909 (bss->capability & WLAN_CAPABILITY_PRIVACY));
910 goto done;
911 }
912
913 /* bss security settings don't match those configured on card */
914 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
915 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
916 bss->wpa_ie[0], bss->rsn_ie[0],
917 priv->secinfo.wep_enabled ? "e" : "d",
918 priv->secinfo.WPAenabled ? "e" : "d",
919 priv->secinfo.WPA2enabled ? "e" : "d",
920 (bss->capability & WLAN_CAPABILITY_PRIVACY));
921
922done:
923 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
924 return matched;
925}
926
927/**
928 * @brief This function finds a specific compatible BSSID in the scan list
929 *
930 * Used in association code
931 *
932 * @param priv A pointer to struct lbs_private
933 * @param bssid BSSID to find in the scan list
934 * @param mode Network mode: Infrastructure or IBSS
935 *
936 * @return index in BSSID list, or error return code (< 0)
937 */
938static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
939 uint8_t *bssid, uint8_t mode)
940{
941 struct bss_descriptor *iter_bss;
942 struct bss_descriptor *found_bss = NULL;
943
944 lbs_deb_enter(LBS_DEB_SCAN);
945
946 if (!bssid)
947 goto out;
948
949 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
950
951 /* Look through the scan table for a compatible match. The loop will
952 * continue past a matched bssid that is not compatible in case there
953 * is an AP with multiple SSIDs assigned to the same BSSID
954 */
955 mutex_lock(&priv->lock);
956 list_for_each_entry(iter_bss, &priv->network_list, list) {
957 if (compare_ether_addr(iter_bss->bssid, bssid))
958 continue; /* bssid doesn't match */
959 switch (mode) {
960 case IW_MODE_INFRA:
961 case IW_MODE_ADHOC:
962 if (!is_network_compatible(priv, iter_bss, mode))
963 break;
964 found_bss = iter_bss;
965 break;
966 default:
967 found_bss = iter_bss;
968 break;
969 }
970 }
971 mutex_unlock(&priv->lock);
972
973out:
974 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
975 return found_bss;
976}
977
978/**
979 * @brief This function finds ssid in ssid list.
980 *
981 * Used in association code
982 *
983 * @param priv A pointer to struct lbs_private
984 * @param ssid SSID to find in the list
985 * @param bssid BSSID to qualify the SSID selection (if provided)
986 * @param mode Network mode: Infrastructure or IBSS
987 *
988 * @return index in BSSID list
989 */
990static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
991 uint8_t *ssid, uint8_t ssid_len,
992 uint8_t *bssid, uint8_t mode,
993 int channel)
994{
995 u32 bestrssi = 0;
996 struct bss_descriptor *iter_bss = NULL;
997 struct bss_descriptor *found_bss = NULL;
998 struct bss_descriptor *tmp_oldest = NULL;
999
1000 lbs_deb_enter(LBS_DEB_SCAN);
1001
1002 mutex_lock(&priv->lock);
1003
1004 list_for_each_entry(iter_bss, &priv->network_list, list) {
1005 if (!tmp_oldest ||
1006 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1007 tmp_oldest = iter_bss;
1008
1009 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1010 ssid, ssid_len) != 0)
1011 continue; /* ssid doesn't match */
1012 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1013 continue; /* bssid doesn't match */
1014 if ((channel > 0) && (iter_bss->channel != channel))
1015 continue; /* channel doesn't match */
1016
1017 switch (mode) {
1018 case IW_MODE_INFRA:
1019 case IW_MODE_ADHOC:
1020 if (!is_network_compatible(priv, iter_bss, mode))
1021 break;
1022
1023 if (bssid) {
1024 /* Found requested BSSID */
1025 found_bss = iter_bss;
1026 goto out;
1027 }
1028
1029 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1030 bestrssi = SCAN_RSSI(iter_bss->rssi);
1031 found_bss = iter_bss;
1032 }
1033 break;
1034 case IW_MODE_AUTO:
1035 default:
1036 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1037 bestrssi = SCAN_RSSI(iter_bss->rssi);
1038 found_bss = iter_bss;
1039 }
1040 break;
1041 }
1042 }
1043
1044out:
1045 mutex_unlock(&priv->lock);
1046 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1047 return found_bss;
1048}
1049
Holger Schurig69f90322007-11-23 15:43:44 +01001050static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051 struct assoc_request * assoc_req)
1052{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001053 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001054 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001055 int channel = -1;
John W. Linville9387b7c2008-09-30 20:59:05 -04001056 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001057
Holger Schurig9012b282007-05-25 11:27:16 -04001058 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001059
Dan Williamsef9a2642007-05-25 16:46:33 -04001060 /* FIXME: take channel into account when picking SSIDs if a channel
1061 * is set.
1062 */
1063
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001064 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1065 channel = assoc_req->channel;
1066
Holger Schurig0765af42007-10-15 12:55:56 +02001067 lbs_deb_assoc("SSID '%s' requested\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001068 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -04001069 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -05001070 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001071 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001072
David Woodhouseaa21c002007-12-08 20:04:36 +00001073 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001074 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001075 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -04001076 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams822ac032009-05-22 20:07:14 -04001077 ret = lbs_try_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001078 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -04001079 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001080 }
Dan Williams0dc5a292007-05-10 22:58:02 -04001081 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001082 /* Scan for the network, do not save previous results. Stale
1083 * scan data will cause us to join a non-existant adhoc network
1084 */
Holger Schurig10078322007-11-15 18:05:47 -05001085 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001086 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001087
1088 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001089 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001090 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001091 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -04001092 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001093 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001094 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001095 } else {
1096 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -04001097 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001098 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001099 IW_ESSID_MAX_SIZE);
1100 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001101 lbs_adhoc_start(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001102 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103 }
1104
Holger Schurig9012b282007-05-25 11:27:16 -04001105 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001106 return ret;
1107}
1108
1109
Holger Schurig69f90322007-11-23 15:43:44 +01001110static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001111 struct assoc_request * assoc_req)
1112{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001113 int ret = 0;
1114 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001115
Johannes Berge1749612008-10-27 15:59:26 -07001116 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001117
1118 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +00001119 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001120 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001121 if (bss == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -07001122 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1123 "cannot associate.\n", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001124 goto out;
1125 }
1126
Dan Williamse76850d2007-05-25 17:09:41 -04001127 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -04001128 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williams822ac032009-05-22 20:07:14 -04001129 ret = lbs_try_associate(priv, assoc_req);
1130 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1131 ret);
Dan Williams0dc5a292007-05-10 22:58:02 -04001132 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001133 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001134 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001135
1136out:
Holger Schurig9012b282007-05-25 11:27:16 -04001137 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001138 return ret;
1139}
1140
1141
Holger Schurig69f90322007-11-23 15:43:44 +01001142static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001143 struct assoc_request * assoc_req)
1144{
1145 int ret = 0, done = 0;
1146
Holger Schurig0765af42007-10-15 12:55:56 +02001147 lbs_deb_enter(LBS_DEB_ASSOC);
1148
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001149 /* If we're given and 'any' BSSID, try associating based on SSID */
1150
1151 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001152 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1153 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001154 ret = assoc_helper_bssid(priv, assoc_req);
1155 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001156 }
1157 }
1158
1159 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1160 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001161 }
1162
Holger Schurig0765af42007-10-15 12:55:56 +02001163 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001164 return ret;
1165}
1166
1167
Holger Schurig69f90322007-11-23 15:43:44 +01001168static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001169 struct assoc_request * assoc_req)
1170{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171 int ret = 0;
1172
Holger Schurig9012b282007-05-25 11:27:16 -04001173 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001174
David Woodhouseaa21c002007-12-08 20:04:36 +00001175 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -04001176 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001177
Dan Williams0dc5a292007-05-10 22:58:02 -04001178 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001179 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -05001180 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +00001181 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001182 }
1183
David Woodhouseaa21c002007-12-08 20:04:36 +00001184 priv->mode = assoc_req->mode;
Dan Williams39fcf7a2008-09-10 12:49:00 -04001185 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001186
Holger Schurig9012b282007-05-25 11:27:16 -04001187done:
1188 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001189 return ret;
1190}
1191
Holger Schurig69f90322007-11-23 15:43:44 +01001192static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -04001193 struct assoc_request * assoc_req)
1194{
Dan Williamsef9a2642007-05-25 16:46:33 -04001195 int ret = 0;
1196
1197 lbs_deb_enter(LBS_DEB_ASSOC);
1198
David Woodhouse9f462572007-12-12 22:50:21 -05001199 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001200 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001201 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001202 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -04001203 }
1204
David Woodhouseaa21c002007-12-08 20:04:36 +00001205 if (assoc_req->channel == priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001206 goto done;
1207
David Woodhouse8642f1f2007-12-11 20:03:01 -05001208 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -05001209 /* Change mesh channel first; 21.p21 firmware won't let
1210 you change channel otherwise (even though it'll return
1211 an error to this */
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001212 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1213 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001214 }
1215
Dan Williamsef9a2642007-05-25 16:46:33 -04001216 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
David Woodhouse86062132007-12-13 00:32:36 -05001217 priv->curbssparams.channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -04001218
Dan Williams2dd4b262007-12-11 16:54:15 -05001219 ret = lbs_set_channel(priv, assoc_req->channel);
1220 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -05001221 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -04001222
Dan Williams2dd4b262007-12-11 16:54:15 -05001223 /* FIXME: shouldn't need to grab the channel _again_ after setting
1224 * it since the firmware is supposed to return the new channel, but
1225 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -05001226 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001227 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001228 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001229 goto done;
1230 }
Dan Williamsef9a2642007-05-25 16:46:33 -04001231
David Woodhouseaa21c002007-12-08 20:04:36 +00001232 if (assoc_req->channel != priv->curbssparams.channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -05001233 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -04001234 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001235 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -04001236 }
1237
1238 if ( assoc_req->secinfo.wep_enabled
1239 && (assoc_req->wep_keys[0].len
1240 || assoc_req->wep_keys[1].len
1241 || assoc_req->wep_keys[2].len
1242 || assoc_req->wep_keys[3].len)) {
1243 /* Make sure WEP keys are re-sent to firmware */
1244 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1245 }
1246
1247 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -05001248 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -04001249
David Woodhouse8642f1f2007-12-11 20:03:01 -05001250 restore_mesh:
1251 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001252 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1253 priv->curbssparams.channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001254
1255 done:
Dan Williamsef9a2642007-05-25 16:46:33 -04001256 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1257 return ret;
1258}
1259
1260
Holger Schurig69f90322007-11-23 15:43:44 +01001261static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -05001262 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001263{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001264 int i;
1265 int ret = 0;
1266
Holger Schurig9012b282007-05-25 11:27:16 -04001267 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001268
1269 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -05001270 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1271 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1272 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1273 else
1274 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001275
1276 if (ret)
1277 goto out;
1278
1279 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -04001280 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +01001281 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001282 else
Holger Schurigd9e97782008-03-12 16:06:43 +01001283 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -05001284
Holger Schurigc97329e2008-03-18 11:20:21 +01001285 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001286
David Woodhouseaa21c002007-12-08 20:04:36 +00001287 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001288
David Woodhouseaa21c002007-12-08 20:04:36 +00001289 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001290 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001291 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -05001292 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001293 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001294 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001295
David Woodhouseaa21c002007-12-08 20:04:36 +00001296 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001297
1298out:
Holger Schurig9012b282007-05-25 11:27:16 -04001299 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001300 return ret;
1301}
1302
Holger Schurig69f90322007-11-23 15:43:44 +01001303static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001304 struct assoc_request * assoc_req)
1305{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001306 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -05001307 uint16_t do_wpa;
1308 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001309
Holger Schurig9012b282007-05-25 11:27:16 -04001310 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001311
David Woodhouseaa21c002007-12-08 20:04:36 +00001312 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001313 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001314
Holger Schurigc97329e2008-03-18 11:20:21 +01001315 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001316
Dan Williams18c96c342007-06-18 12:01:12 -04001317 /* If RSN is already enabled, don't try to enable it again, since
1318 * ENABLE_RSN resets internal state machines and will clobber the
1319 * 4-way WPA handshake.
1320 */
1321
1322 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001323 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -04001324 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001325 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -04001326 goto out;
1327 }
1328
1329 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001330 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -04001331 if (do_wpa == rsn)
1332 goto out;
1333
1334 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001335 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -04001336
1337out:
Holger Schurig9012b282007-05-25 11:27:16 -04001338 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001339 return ret;
1340}
1341
1342
Holger Schurig69f90322007-11-23 15:43:44 +01001343static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001344 struct assoc_request * assoc_req)
1345{
1346 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -04001347 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001348
Holger Schurig9012b282007-05-25 11:27:16 -04001349 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001350
Dan Williams2bcde512007-10-03 10:37:45 -04001351 /* Work around older firmware bug where WPA unicast and multicast
1352 * keys must be set independently. Seen in SDIO parts with firmware
1353 * version 5.0.11p0.
1354 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001355
Dan Williams2bcde512007-10-03 10:37:45 -04001356 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1357 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +01001358 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001359 assoc_req->flags = flags;
1360 }
1361
1362 if (ret)
1363 goto out;
1364
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001365 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1366 sizeof(struct enc_key));
1367
Dan Williams2bcde512007-10-03 10:37:45 -04001368 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1369 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1370
David Woodhouse9e1228d2008-03-03 12:15:39 +01001371 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001372 assoc_req->flags = flags;
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001373
1374 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1375 sizeof(struct enc_key));
Dan Williams2bcde512007-10-03 10:37:45 -04001376 }
1377
1378out:
Holger Schurig9012b282007-05-25 11:27:16 -04001379 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001380 return ret;
1381}
1382
1383
Holger Schurig69f90322007-11-23 15:43:44 +01001384static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001385 struct assoc_request * assoc_req)
1386{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001387 int ret = 0;
1388
Holger Schurig9012b282007-05-25 11:27:16 -04001389 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001390
1391 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001392 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1393 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001394 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +00001395 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1396 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001397 }
1398
Holger Schurig9012b282007-05-25 11:27:16 -04001399 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001400 return ret;
1401}
1402
1403
David Woodhouseaa21c002007-12-08 20:04:36 +00001404static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001405 struct assoc_request * assoc_req)
1406{
Holger Schurig0765af42007-10-15 12:55:56 +02001407 int ret = 0;
1408
David Woodhouseaa21c002007-12-08 20:04:36 +00001409 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001410 return 0;
1411
Holger Schurig52507c22008-01-28 17:25:53 +01001412 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001413 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001414 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1415 ret = 1;
1416 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001417 }
1418
1419 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001420 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +02001421 lbs_deb_assoc("Deauthenticating due to new security\n");
1422 ret = 1;
1423 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001424 }
1425 }
1426
1427 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001428 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1429 ret = 1;
1430 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001431 }
1432
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001433 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001434 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1435 ret = 1;
1436 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001437 }
1438
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001439 /* FIXME: deal with 'auto' mode somehow */
1440 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001441 if (assoc_req->mode != IW_MODE_INFRA) {
1442 lbs_deb_assoc("Deauthenticating due to leaving "
1443 "infra mode\n");
1444 ret = 1;
1445 goto out;
1446 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001447 }
1448
Holger Schurig0765af42007-10-15 12:55:56 +02001449out:
1450 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +01001451 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001452}
1453
1454
David Woodhouseaa21c002007-12-08 20:04:36 +00001455static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001456 struct assoc_request * assoc_req)
1457{
Holger Schurig0765af42007-10-15 12:55:56 +02001458 lbs_deb_enter(LBS_DEB_ASSOC);
1459
David Woodhouseaa21c002007-12-08 20:04:36 +00001460 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001461 return 0;
1462
David Woodhouseaa21c002007-12-08 20:04:36 +00001463 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1464 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001465 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001466 return 1;
1467
1468 /* FIXME: deal with 'auto' mode somehow */
1469 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001470 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001471 return 1;
1472 }
1473
Dan Williamsef9a2642007-05-25 16:46:33 -04001474 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001475 if (assoc_req->channel != priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001476 return 1;
1477 }
1478
Holger Schurig0765af42007-10-15 12:55:56 +02001479 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001480 return 0;
1481}
1482
1483
Holger Schurig245bf202008-04-02 16:27:42 +02001484/**
1485 * @brief This function finds the best SSID in the Scan List
1486 *
1487 * Search the scan table for the best SSID that also matches the current
1488 * adapter network preference (infrastructure or adhoc)
1489 *
1490 * @param priv A pointer to struct lbs_private
1491 *
1492 * @return index in BSSID list
1493 */
1494static struct bss_descriptor *lbs_find_best_ssid_in_list(
1495 struct lbs_private *priv, uint8_t mode)
1496{
1497 uint8_t bestrssi = 0;
1498 struct bss_descriptor *iter_bss;
1499 struct bss_descriptor *best_bss = NULL;
1500
1501 lbs_deb_enter(LBS_DEB_SCAN);
1502
1503 mutex_lock(&priv->lock);
1504
1505 list_for_each_entry(iter_bss, &priv->network_list, list) {
1506 switch (mode) {
1507 case IW_MODE_INFRA:
1508 case IW_MODE_ADHOC:
1509 if (!is_network_compatible(priv, iter_bss, mode))
1510 break;
1511 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1512 break;
1513 bestrssi = SCAN_RSSI(iter_bss->rssi);
1514 best_bss = iter_bss;
1515 break;
1516 case IW_MODE_AUTO:
1517 default:
1518 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1519 break;
1520 bestrssi = SCAN_RSSI(iter_bss->rssi);
1521 best_bss = iter_bss;
1522 break;
1523 }
1524 }
1525
1526 mutex_unlock(&priv->lock);
1527 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1528 return best_bss;
1529}
1530
1531/**
1532 * @brief Find the best AP
1533 *
1534 * Used from association worker.
1535 *
1536 * @param priv A pointer to struct lbs_private structure
1537 * @param pSSID A pointer to AP's ssid
1538 *
1539 * @return 0--success, otherwise--fail
1540 */
1541static int lbs_find_best_network_ssid(struct lbs_private *priv,
1542 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1543 uint8_t *out_mode)
1544{
1545 int ret = -1;
1546 struct bss_descriptor *found;
1547
1548 lbs_deb_enter(LBS_DEB_SCAN);
1549
1550 priv->scan_ssid_len = 0;
1551 lbs_scan_networks(priv, 1);
1552 if (priv->surpriseremoved)
1553 goto out;
1554
1555 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1556 if (found && (found->ssid_len > 0)) {
1557 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1558 *out_ssid_len = found->ssid_len;
1559 *out_mode = found->mode;
1560 ret = 0;
1561 }
1562
1563out:
1564 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1565 return ret;
1566}
1567
1568
Holger Schurig10078322007-11-15 18:05:47 -05001569void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001570{
Holger Schurig69f90322007-11-23 15:43:44 +01001571 struct lbs_private *priv = container_of(work, struct lbs_private,
1572 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001573 struct assoc_request * assoc_req = NULL;
1574 int ret = 0;
1575 int find_any_ssid = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001576 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001577
Holger Schurig9012b282007-05-25 11:27:16 -04001578 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001579
David Woodhouseaa21c002007-12-08 20:04:36 +00001580 mutex_lock(&priv->lock);
1581 assoc_req = priv->pending_assoc_req;
1582 priv->pending_assoc_req = NULL;
1583 priv->in_progress_assoc_req = assoc_req;
1584 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001585
Holger Schurig9012b282007-05-25 11:27:16 -04001586 if (!assoc_req)
1587 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001588
Holger Schurig0765af42007-10-15 12:55:56 +02001589 lbs_deb_assoc(
1590 "Association Request:\n"
1591 " flags: 0x%08lx\n"
1592 " SSID: '%s'\n"
1593 " chann: %d\n"
1594 " band: %d\n"
1595 " mode: %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001596 " BSSID: %pM\n"
Holger Schurig0765af42007-10-15 12:55:56 +02001597 " secinfo: %s%s%s\n"
1598 " auth_mode: %d\n",
1599 assoc_req->flags,
John W. Linville9387b7c2008-09-30 20:59:05 -04001600 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Holger Schurig0765af42007-10-15 12:55:56 +02001601 assoc_req->channel, assoc_req->band, assoc_req->mode,
Johannes Berge1749612008-10-27 15:59:26 -07001602 assoc_req->bssid,
Holger Schurig0765af42007-10-15 12:55:56 +02001603 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1604 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1605 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1606 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001607
1608 /* If 'any' SSID was specified, find an SSID to associate with */
1609 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001610 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001611 find_any_ssid = 1;
1612
1613 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1614 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001615 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1616 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001617 find_any_ssid = 0;
1618 }
1619
1620 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02001621 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001622
Holger Schurig10078322007-11-15 18:05:47 -05001623 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001624 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001625 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001626 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001627 ret = -ENETUNREACH;
1628 goto out;
1629 }
1630
1631 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04001632 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001633 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1634 assoc_req->mode = new_mode;
1635 }
1636 }
1637
1638 /*
1639 * Check if the attributes being changing require deauthentication
1640 * from the currently associated infrastructure access point.
1641 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001642 if (priv->mode == IW_MODE_INFRA) {
1643 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04001644 ret = lbs_cmd_80211_deauthenticate(priv,
1645 priv->curbssparams.bssid,
1646 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001647 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001648 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001649 "configuration request failed: %d\n",
1650 ret);
1651 }
1652 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001653 } else if (priv->mode == IW_MODE_ADHOC) {
1654 if (should_stop_adhoc(priv, assoc_req)) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001655 ret = lbs_adhoc_stop(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001656 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001657 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001658 "new configuration request failed: %d\n",
1659 ret);
1660 }
1661
1662 }
1663 }
1664
1665 /* Send the various configuration bits to the firmware */
1666 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1667 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001668 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001669 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001670 }
1671
Dan Williamsef9a2642007-05-25 16:46:33 -04001672 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1673 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001674 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04001675 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04001676 }
1677
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001678 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1679 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1680 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001681 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001682 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001683 }
1684
1685 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1686 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001687 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001688 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001689 }
1690
1691 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1692 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001693 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001694 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001695 }
1696
1697 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1698 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1699 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001700 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001701 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001702 }
1703
1704 /* SSID/BSSID should be the _last_ config option set, because they
1705 * trigger the association attempt.
1706 */
1707 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1708 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1709 int success = 1;
1710
1711 ret = assoc_helper_associate(priv, assoc_req);
1712 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01001713 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001714 ret);
1715 success = 0;
1716 }
1717
David Woodhouseaa21c002007-12-08 20:04:36 +00001718 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01001719 lbs_deb_assoc("ASSOC: association unsuccessful, "
1720 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001721 success = 0;
1722 }
1723
1724 if (success) {
Johannes Berge1749612008-10-27 15:59:26 -07001725 lbs_deb_assoc("associated to %pM\n",
1726 priv->curbssparams.bssid);
Holger Schurig10078322007-11-15 18:05:47 -05001727 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04001728 CMD_802_11_RSSI,
1729 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001730 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001731 ret = -1;
1732 }
1733 }
1734
1735out:
1736 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001737 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001738 ret);
1739 }
Dan Williamse76850d2007-05-25 17:09:41 -04001740
David Woodhouseaa21c002007-12-08 20:04:36 +00001741 mutex_lock(&priv->lock);
1742 priv->in_progress_assoc_req = NULL;
1743 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001744 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04001745
1746done:
1747 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001748}
1749
1750
1751/*
1752 * Caller MUST hold any necessary locks
1753 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001754struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001755{
1756 struct assoc_request * assoc_req;
1757
Holger Schurig0765af42007-10-15 12:55:56 +02001758 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00001759 if (!priv->pending_assoc_req) {
1760 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04001761 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00001762 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001763 lbs_pr_info("Not enough memory to allocate association"
1764 " request!\n");
1765 return NULL;
1766 }
1767 }
1768
1769 /* Copy current configuration attributes to the association request,
1770 * but don't overwrite any that are already set.
1771 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001772 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001773 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001774 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001775 IW_ESSID_MAX_SIZE);
David Woodhouseaa21c002007-12-08 20:04:36 +00001776 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001777 }
1778
1779 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001780 assoc_req->channel = priv->curbssparams.channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001781
Dan Williamse76850d2007-05-25 17:09:41 -04001782 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001783 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04001784
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001785 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001786 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001787
1788 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001789 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001790 ETH_ALEN);
1791 }
1792
1793 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1794 int i;
1795 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001796 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04001797 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001798 }
1799 }
1800
1801 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001802 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001803
1804 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001805 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001806 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001807 }
1808
1809 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001810 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001811 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001812 }
1813
1814 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001815 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001816 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001817 }
1818
1819 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001820 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001821 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001822 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001823 }
1824
Holger Schurig0765af42007-10-15 12:55:56 +02001825 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001826 return assoc_req;
1827}
Holger Schurig697900a2008-04-02 16:27:10 +02001828
1829
1830/**
Dan Williams191bb402008-08-21 17:46:18 -04001831 * @brief Deauthenticate from a specific BSS
1832 *
1833 * @param priv A pointer to struct lbs_private structure
1834 * @param bssid The specific BSS to deauthenticate from
1835 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1836 *
1837 * @return 0 on success, error on failure
1838 */
1839int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1840 u16 reason)
Holger Schurig697900a2008-04-02 16:27:10 +02001841{
Dan Williams191bb402008-08-21 17:46:18 -04001842 struct cmd_ds_802_11_deauthenticate cmd;
1843 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001844
1845 lbs_deb_enter(LBS_DEB_JOIN);
1846
Dan Williams191bb402008-08-21 17:46:18 -04001847 memset(&cmd, 0, sizeof(cmd));
1848 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1849 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1850 cmd.reasoncode = cpu_to_le16(reason);
Holger Schurig697900a2008-04-02 16:27:10 +02001851
Dan Williams191bb402008-08-21 17:46:18 -04001852 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001853
Dan Williams191bb402008-08-21 17:46:18 -04001854 /* Clean up everything even if there was an error; can't assume that
1855 * we're still authenticated to the AP after trying to deauth.
1856 */
1857 lbs_mac_event_disconnected(priv);
Holger Schurig697900a2008-04-02 16:27:10 +02001858
1859 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04001860 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001861}
1862