blob: b8bdba67ad17c5a540b14a1333a735f486f25543 [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
Holger Schurig2d465022009-10-22 15:30:48 +020026/**
27 * 802.11b/g supported bitrates (in 500Kb/s units)
28 */
29u8 lbs_bg_rates[MAX_RATES] =
30 { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
310x00, 0x00 };
32
Holger Schurig697900a2008-04-02 16:27:10 +020033
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040034/**
35 * @brief This function finds common rates between rates and card rates.
36 *
37 * It will fill common rates in rates as output if found.
38 *
39 * NOTE: Setting the MSB of the basic rates need to be taken
40 * care, either before or after calling this function
41 *
42 * @param priv A pointer to struct lbs_private structure
43 * @param rates the buffer which keeps input and output
Dan Williamsca4fe302009-08-21 09:35:20 -050044 * @param rates_size the size of rates buffer; new size of buffer on return,
45 * which will be less than or equal to original rates_size
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040046 *
47 * @return 0 on success, or -1 on error
48 */
49static int get_common_rates(struct lbs_private *priv,
50 u8 *rates,
51 u16 *rates_size)
52{
Roel Kluin1e3d31c2009-08-02 09:44:12 +020053 int i, j;
Dan Williamsca4fe302009-08-21 09:35:20 -050054 u8 intersection[MAX_RATES];
55 u16 intersection_size;
56 u16 num_rates = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040057
Dan Williamsca4fe302009-08-21 09:35:20 -050058 intersection_size = min_t(u16, *rates_size, ARRAY_SIZE(intersection));
Andrey Yurovsky6f632d52009-08-13 17:34:40 -070059
Dan Williamsca4fe302009-08-21 09:35:20 -050060 /* Allow each rate from 'rates' that is supported by the hardware */
61 for (i = 0; i < ARRAY_SIZE(lbs_bg_rates) && lbs_bg_rates[i]; i++) {
62 for (j = 0; j < intersection_size && rates[j]; j++) {
63 if (rates[j] == lbs_bg_rates[i])
64 intersection[num_rates++] = rates[j];
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040065 }
66 }
67
68 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
Dan Williamsca4fe302009-08-21 09:35:20 -050069 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", lbs_bg_rates,
70 ARRAY_SIZE(lbs_bg_rates));
71 lbs_deb_hex(LBS_DEB_JOIN, "common rates", intersection, num_rates);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040072 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
73
74 if (!priv->enablehwauto) {
Dan Williamsca4fe302009-08-21 09:35:20 -050075 for (i = 0; i < num_rates; i++) {
76 if (intersection[i] == priv->cur_rate)
77 goto done;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040078 }
Dan Williamsca4fe302009-08-21 09:35:20 -050079 lbs_pr_alert("Previously set fixed data rate %#x isn't "
80 "compatible with the network.\n", priv->cur_rate);
81 return -1;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040082 }
Dan Williamsca4fe302009-08-21 09:35:20 -050083
84done:
85 memset(rates, 0, *rates_size);
86 *rates_size = num_rates;
87 memcpy(rates, intersection, num_rates);
Roel Kluin1e3d31c2009-08-02 09:44:12 +020088 return 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040089}
90
91
92/**
93 * @brief Sets the MSB on basic rates as the firmware requires
94 *
95 * Scan through an array and set the MSB for basic data rates.
96 *
97 * @param rates buffer of data rates
98 * @param len size of buffer
99 */
100static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
101{
102 int i;
103
104 for (i = 0; i < len; i++) {
105 if (rates[i] == 0x02 || rates[i] == 0x04 ||
106 rates[i] == 0x0b || rates[i] == 0x16)
107 rates[i] |= 0x80;
108 }
109}
110
Holger Schurig697900a2008-04-02 16:27:10 +0200111
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400112static u8 iw_auth_to_ieee_auth(u8 auth)
113{
114 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
115 return 0x00;
116 else if (auth == IW_AUTH_ALG_SHARED_KEY)
117 return 0x01;
118 else if (auth == IW_AUTH_ALG_LEAP)
119 return 0x80;
120
121 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
122 return 0;
123}
124
125/**
126 * @brief This function prepares the authenticate command. AUTHENTICATE only
127 * sets the authentication suite for future associations, as the firmware
128 * handles authentication internally during the ASSOCIATE command.
129 *
130 * @param priv A pointer to struct lbs_private structure
131 * @param bssid The peer BSSID with which to authenticate
132 * @param auth The authentication mode to use (from wireless.h)
133 *
134 * @return 0 or -1
135 */
136static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
137{
138 struct cmd_ds_802_11_authenticate cmd;
139 int ret = -1;
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400140
141 lbs_deb_enter(LBS_DEB_JOIN);
142
143 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
144 memcpy(cmd.bssid, bssid, ETH_ALEN);
145
146 cmd.authtype = iw_auth_to_ieee_auth(auth);
147
Johannes Berge91d8332009-07-15 17:21:41 +0200148 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400149
150 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
151
152 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
153 return ret;
154}
155
Dan Williams822ac032009-05-22 20:07:14 -0400156
157static int lbs_assoc_post(struct lbs_private *priv,
158 struct cmd_ds_802_11_associate_response *resp)
159{
160 int ret = 0;
161 union iwreq_data wrqu;
162 struct bss_descriptor *bss;
163 u16 status_code;
164
165 lbs_deb_enter(LBS_DEB_ASSOC);
166
167 if (!priv->in_progress_assoc_req) {
168 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
169 ret = -1;
170 goto done;
171 }
172 bss = &priv->in_progress_assoc_req->bss;
173
174 /*
175 * Older FW versions map the IEEE 802.11 Status Code in the association
176 * response to the following values returned in resp->statuscode:
177 *
178 * IEEE Status Code Marvell Status Code
179 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
180 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
181 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
182 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
183 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
184 * others -> 0x0003 ASSOC_RESULT_REFUSED
185 *
186 * Other response codes:
187 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
188 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
189 * association response from the AP)
190 */
191
192 status_code = le16_to_cpu(resp->statuscode);
193 if (priv->fwrelease < 0x09000000) {
194 switch (status_code) {
195 case 0x00:
196 break;
197 case 0x01:
198 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
199 break;
200 case 0x02:
201 lbs_deb_assoc("ASSOC_RESP: internal timer "
202 "expired while waiting for the AP\n");
203 break;
204 case 0x03:
205 lbs_deb_assoc("ASSOC_RESP: association "
206 "refused by AP\n");
207 break;
208 case 0x04:
209 lbs_deb_assoc("ASSOC_RESP: authentication "
210 "refused by AP\n");
211 break;
212 default:
213 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
214 " unknown\n", status_code);
215 break;
216 }
217 } else {
218 /* v9+ returns the AP's association response */
219 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
220 }
221
222 if (status_code) {
223 lbs_mac_event_disconnected(priv);
224 ret = -1;
225 goto done;
226 }
227
228 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
229 (void *) (resp + sizeof (resp->hdr)),
230 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
231
232 /* Send a Media Connected event, according to the Spec */
233 priv->connect_status = LBS_CONNECTED;
234
235 /* Update current SSID and BSSID */
Holger Schurig243e84e2009-10-22 15:30:47 +0200236 memcpy(&priv->curbssparams.ssid, &bss->ssid, IEEE80211_MAX_SSID_LEN);
Dan Williams822ac032009-05-22 20:07:14 -0400237 priv->curbssparams.ssid_len = bss->ssid_len;
238 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
239
240 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
241 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
242
243 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
244 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
245 priv->nextSNRNF = 0;
246 priv->numSNRNF = 0;
247
248 netif_carrier_on(priv->dev);
249 if (!priv->tx_pending_len)
250 netif_wake_queue(priv->dev);
251
252 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
253 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
254 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
255
256done:
257 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
258 return ret;
259}
260
261/**
262 * @brief This function prepares an association-class command.
263 *
264 * @param priv A pointer to struct lbs_private structure
265 * @param assoc_req The association request describing the BSS to associate
266 * or reassociate with
267 * @param command The actual command, either CMD_802_11_ASSOCIATE or
268 * CMD_802_11_REASSOCIATE
269 *
270 * @return 0 or -1
271 */
272static int lbs_associate(struct lbs_private *priv,
273 struct assoc_request *assoc_req,
274 u16 command)
275{
276 struct cmd_ds_802_11_associate cmd;
277 int ret = 0;
278 struct bss_descriptor *bss = &assoc_req->bss;
279 u8 *pos = &(cmd.iebuf[0]);
280 u16 tmpcap, tmplen, tmpauth;
281 struct mrvl_ie_ssid_param_set *ssid;
282 struct mrvl_ie_ds_param_set *ds;
283 struct mrvl_ie_cf_param_set *cf;
284 struct mrvl_ie_rates_param_set *rates;
285 struct mrvl_ie_rsn_param_set *rsn;
286 struct mrvl_ie_auth_type *auth;
287
288 lbs_deb_enter(LBS_DEB_ASSOC);
289
290 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
291 (command != CMD_802_11_REASSOCIATE));
292
293 memset(&cmd, 0, sizeof(cmd));
294 cmd.hdr.command = cpu_to_le16(command);
295
296 /* Fill in static fields */
297 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
298 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
299
300 /* Capability info */
301 tmpcap = (bss->capability & CAPINFO_MASK);
302 if (bss->mode == IW_MODE_INFRA)
303 tmpcap |= WLAN_CAPABILITY_ESS;
304 cmd.capability = cpu_to_le16(tmpcap);
305 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
306
307 /* SSID */
308 ssid = (struct mrvl_ie_ssid_param_set *) pos;
309 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
310 tmplen = bss->ssid_len;
311 ssid->header.len = cpu_to_le16(tmplen);
312 memcpy(ssid->ssid, bss->ssid, tmplen);
313 pos += sizeof(ssid->header) + tmplen;
314
315 ds = (struct mrvl_ie_ds_param_set *) pos;
316 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
317 ds->header.len = cpu_to_le16(1);
318 ds->channel = bss->phy.ds.channel;
319 pos += sizeof(ds->header) + 1;
320
321 cf = (struct mrvl_ie_cf_param_set *) pos;
322 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
323 tmplen = sizeof(*cf) - sizeof (cf->header);
324 cf->header.len = cpu_to_le16(tmplen);
325 /* IE payload should be zeroed, firmware fills it in for us */
326 pos += sizeof(*cf);
327
328 rates = (struct mrvl_ie_rates_param_set *) pos;
329 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
Dan Williamsca4fe302009-08-21 09:35:20 -0500330 tmplen = min_t(u16, ARRAY_SIZE(bss->rates), MAX_RATES);
Roel Kluin1e3d31c2009-08-02 09:44:12 +0200331 memcpy(&rates->rates, &bss->rates, tmplen);
Dan Williams822ac032009-05-22 20:07:14 -0400332 if (get_common_rates(priv, rates->rates, &tmplen)) {
333 ret = -1;
334 goto done;
335 }
336 pos += sizeof(rates->header) + tmplen;
337 rates->header.len = cpu_to_le16(tmplen);
338 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
339
340 /* Copy the infra. association rates into Current BSS state structure */
341 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
342 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
343
344 /* Set MSB on basic rates as the firmware requires, but _after_
345 * copying to current bss rates.
346 */
347 lbs_set_basic_rate_flags(rates->rates, tmplen);
348
349 /* Firmware v9+ indicate authentication suites as a TLV */
350 if (priv->fwrelease >= 0x09000000) {
Dan Williams822ac032009-05-22 20:07:14 -0400351 auth = (struct mrvl_ie_auth_type *) pos;
352 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
353 auth->header.len = cpu_to_le16(2);
354 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
355 auth->auth = cpu_to_le16(tmpauth);
356 pos += sizeof(auth->header) + 2;
357
Johannes Berge91d8332009-07-15 17:21:41 +0200358 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
359 bss->bssid, priv->secinfo.auth_mode);
Dan Williams822ac032009-05-22 20:07:14 -0400360 }
361
362 /* WPA/WPA2 IEs */
363 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
364 rsn = (struct mrvl_ie_rsn_param_set *) pos;
365 /* WPA_IE or WPA2_IE */
366 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
367 tmplen = (u16) assoc_req->wpa_ie[1];
368 rsn->header.len = cpu_to_le16(tmplen);
369 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
370 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
371 sizeof(rsn->header) + tmplen);
372 pos += sizeof(rsn->header) + tmplen;
373 }
374
375 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
376 (u16)(pos - (u8 *) &cmd.iebuf));
377
378 /* update curbssparams */
Holger Schurigc14951f2009-10-22 15:30:50 +0200379 priv->channel = bss->phy.ds.channel;
Dan Williams822ac032009-05-22 20:07:14 -0400380
Dan Williams822ac032009-05-22 20:07:14 -0400381 ret = lbs_cmd_with_response(priv, command, &cmd);
382 if (ret == 0) {
383 ret = lbs_assoc_post(priv,
384 (struct cmd_ds_802_11_associate_response *) &cmd);
385 }
386
387done:
388 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
389 return ret;
390}
391
Holger Schurig697900a2008-04-02 16:27:10 +0200392/**
393 * @brief Associate to a specific BSS discovered in a scan
394 *
395 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400396 * @param assoc_req The association request describing the BSS to associate with
Holger Schurig697900a2008-04-02 16:27:10 +0200397 *
398 * @return 0-success, otherwise fail
399 */
Dan Williams822ac032009-05-22 20:07:14 -0400400static int lbs_try_associate(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200401 struct assoc_request *assoc_req)
402{
403 int ret;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400404 u8 preamble = RADIO_PREAMBLE_LONG;
Holger Schurig697900a2008-04-02 16:27:10 +0200405
406 lbs_deb_enter(LBS_DEB_ASSOC);
407
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400408 /* FW v9 and higher indicate authentication suites as a TLV in the
409 * association command, not as a separate authentication command.
410 */
411 if (priv->fwrelease < 0x09000000) {
412 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
413 priv->secinfo.auth_mode);
414 if (ret)
415 goto out;
416 }
Holger Schurig697900a2008-04-02 16:27:10 +0200417
Dan Williamsd5db2df2008-08-21 17:51:07 -0400418 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig697900a2008-04-02 16:27:10 +0200419 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
420 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
Dan Williamsd5db2df2008-08-21 17:51:07 -0400421 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200422
Dan Williamsd5db2df2008-08-21 17:51:07 -0400423 ret = lbs_set_radio(priv, preamble, 1);
424 if (ret)
425 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200426
Dan Williams822ac032009-05-22 20:07:14 -0400427 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
Holger Schurig697900a2008-04-02 16:27:10 +0200428
Dan Williamsd5db2df2008-08-21 17:51:07 -0400429out:
Holger Schurig697900a2008-04-02 16:27:10 +0200430 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
431 return ret;
432}
433
Dan Williams822ac032009-05-22 20:07:14 -0400434static int lbs_adhoc_post(struct lbs_private *priv,
435 struct cmd_ds_802_11_ad_hoc_result *resp)
436{
437 int ret = 0;
438 u16 command = le16_to_cpu(resp->hdr.command);
439 u16 result = le16_to_cpu(resp->hdr.result);
440 union iwreq_data wrqu;
441 struct bss_descriptor *bss;
442 DECLARE_SSID_BUF(ssid);
443
444 lbs_deb_enter(LBS_DEB_JOIN);
445
446 if (!priv->in_progress_assoc_req) {
447 lbs_deb_join("ADHOC_RESP: no in-progress association "
448 "request\n");
449 ret = -1;
450 goto done;
451 }
452 bss = &priv->in_progress_assoc_req->bss;
453
454 /*
455 * Join result code 0 --> SUCCESS
456 */
457 if (result) {
458 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
459 if (priv->connect_status == LBS_CONNECTED)
460 lbs_mac_event_disconnected(priv);
461 ret = -1;
462 goto done;
463 }
464
465 /* Send a Media Connected event, according to the Spec */
466 priv->connect_status = LBS_CONNECTED;
467
468 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
469 /* Update the created network descriptor with the new BSSID */
470 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
471 }
472
473 /* Set the BSSID from the joined/started descriptor */
474 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
475
476 /* Set the new SSID to current SSID */
Holger Schurig243e84e2009-10-22 15:30:47 +0200477 memcpy(&priv->curbssparams.ssid, &bss->ssid, IEEE80211_MAX_SSID_LEN);
Dan Williams822ac032009-05-22 20:07:14 -0400478 priv->curbssparams.ssid_len = bss->ssid_len;
479
480 netif_carrier_on(priv->dev);
481 if (!priv->tx_pending_len)
482 netif_wake_queue(priv->dev);
483
484 memset(&wrqu, 0, sizeof(wrqu));
485 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
486 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
487 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
488
489 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
490 print_ssid(ssid, bss->ssid, bss->ssid_len),
491 priv->curbssparams.bssid,
Holger Schurigc14951f2009-10-22 15:30:50 +0200492 priv->channel);
Dan Williams822ac032009-05-22 20:07:14 -0400493
494done:
495 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
496 return ret;
497}
498
Holger Schurig697900a2008-04-02 16:27:10 +0200499/**
500 * @brief Join an adhoc network found in a previous scan
501 *
502 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400503 * @param assoc_req The association request describing the BSS to join
Holger Schurig697900a2008-04-02 16:27:10 +0200504 *
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400505 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200506 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400507static int lbs_adhoc_join(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200508 struct assoc_request *assoc_req)
509{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400510 struct cmd_ds_802_11_ad_hoc_join cmd;
Holger Schurig697900a2008-04-02 16:27:10 +0200511 struct bss_descriptor *bss = &assoc_req->bss;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400512 u8 preamble = RADIO_PREAMBLE_LONG;
John W. Linville9387b7c2008-09-30 20:59:05 -0400513 DECLARE_SSID_BUF(ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400514 u16 ratesize = 0;
515 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400516
517 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200518
519 lbs_deb_join("current SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400520 print_ssid(ssid, priv->curbssparams.ssid,
Holger Schurig697900a2008-04-02 16:27:10 +0200521 priv->curbssparams.ssid_len),
522 priv->curbssparams.ssid_len);
523 lbs_deb_join("requested ssid '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400524 print_ssid(ssid, bss->ssid, bss->ssid_len),
Holger Schurig697900a2008-04-02 16:27:10 +0200525 bss->ssid_len);
526
527 /* check if the requested SSID is already joined */
528 if (priv->curbssparams.ssid_len &&
529 !lbs_ssid_cmp(priv->curbssparams.ssid,
530 priv->curbssparams.ssid_len,
531 bss->ssid, bss->ssid_len) &&
532 (priv->mode == IW_MODE_ADHOC) &&
533 (priv->connect_status == LBS_CONNECTED)) {
534 union iwreq_data wrqu;
535
536 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
537 "current, not attempting to re-join");
538
539 /* Send the re-association event though, because the association
540 * request really was successful, even if just a null-op.
541 */
542 memset(&wrqu, 0, sizeof(wrqu));
543 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
544 ETH_ALEN);
545 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
546 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
547 goto out;
548 }
549
Dan Williamsd5db2df2008-08-21 17:51:07 -0400550 /* Use short preamble only when both the BSS and firmware support it */
551 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
552 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig697900a2008-04-02 16:27:10 +0200553 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400554 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200555 }
556
Dan Williamsd5db2df2008-08-21 17:51:07 -0400557 ret = lbs_set_radio(priv, preamble, 1);
558 if (ret)
559 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200560
561 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
562 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
563
564 priv->adhoccreate = 0;
Holger Schurigc14951f2009-10-22 15:30:50 +0200565 priv->channel = bss->channel;
Holger Schurig697900a2008-04-02 16:27:10 +0200566
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400567 /* Build the join command */
568 memset(&cmd, 0, sizeof(cmd));
569 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
570
571 cmd.bss.type = CMD_BSS_TYPE_IBSS;
572 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
573
574 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
575 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
576
Dan Williams5fd164e2009-05-22 20:01:21 -0400577 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400578
Dan Williams5fd164e2009-05-22 20:01:21 -0400579 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
580 sizeof(struct ieee_ie_ibss_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400581
582 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
583 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
584 bss->capability, CAPINFO_MASK);
585
586 /* information on BSSID descriptor passed to FW */
Johannes Berge1749612008-10-27 15:59:26 -0700587 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
588 cmd.bss.bssid, cmd.bss.ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400589
590 /* Only v8 and below support setting these */
591 if (priv->fwrelease < 0x09000000) {
592 /* failtimeout */
593 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
594 /* probedelay */
595 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
596 }
597
598 /* Copy Data rates from the rates recorded in scan response */
599 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
Dan Williamsca4fe302009-08-21 09:35:20 -0500600 ratesize = min_t(u16, ARRAY_SIZE(cmd.bss.rates), ARRAY_SIZE (bss->rates));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400601 memcpy(cmd.bss.rates, bss->rates, ratesize);
602 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
603 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
604 ret = -1;
605 goto out;
606 }
607
608 /* Copy the ad-hoc creation rates into Current BSS state structure */
609 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
610 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
611
612 /* Set MSB on basic rates as the firmware requires, but _after_
613 * copying to current bss rates.
614 */
615 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
616
Dan Williams5fd164e2009-05-22 20:01:21 -0400617 cmd.bss.ibss.atimwindow = bss->atimwindow;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400618
619 if (assoc_req->secinfo.wep_enabled) {
620 u16 tmp = le16_to_cpu(cmd.bss.capability);
621 tmp |= WLAN_CAPABILITY_PRIVACY;
622 cmd.bss.capability = cpu_to_le16(tmp);
623 }
624
625 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
626 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
627
628 /* wake up first */
629 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
630 CMD_ACT_SET, 0, 0,
631 &local_ps_mode);
632 if (ret) {
633 ret = -1;
634 goto out;
635 }
636 }
637
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400638 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
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400737 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
738 assoc_req->channel, assoc_req->band);
739
740 priv->adhoccreate = 1;
741 priv->mode = IW_MODE_ADHOC;
742
743 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
744 if (ret == 0)
Dan Williams822ac032009-05-22 20:07:14 -0400745 ret = lbs_adhoc_post(priv,
746 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200747
Dan Williamsd5db2df2008-08-21 17:51:07 -0400748out:
749 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200750 return ret;
751}
752
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400753/**
754 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
755 *
756 * @param priv A pointer to struct lbs_private structure
757 * @return 0 on success, or an error
758 */
759int lbs_adhoc_stop(struct lbs_private *priv)
Holger Schurig697900a2008-04-02 16:27:10 +0200760{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400761 struct cmd_ds_802_11_ad_hoc_stop cmd;
762 int ret;
763
764 lbs_deb_enter(LBS_DEB_JOIN);
765
766 memset(&cmd, 0, sizeof (cmd));
767 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
768
769 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
770
771 /* Clean up everything even if there was an error */
772 lbs_mac_event_disconnected(priv);
773
774 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
775 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +0200776}
Dan Williamse76850d2007-05-25 17:09:41 -0400777
Holger Schurig245bf202008-04-02 16:27:42 +0200778static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
779 struct bss_descriptor *match_bss)
780{
781 if (!secinfo->wep_enabled && !secinfo->WPAenabled
782 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100783 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
784 && match_bss->rsn_ie[0] != WLAN_EID_RSN
Holger Schurig245bf202008-04-02 16:27:42 +0200785 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
786 return 1;
787 else
788 return 0;
789}
790
791static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
792 struct bss_descriptor *match_bss)
793{
794 if (secinfo->wep_enabled && !secinfo->WPAenabled
795 && !secinfo->WPA2enabled
796 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
797 return 1;
798 else
799 return 0;
800}
801
802static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
803 struct bss_descriptor *match_bss)
804{
805 if (!secinfo->wep_enabled && secinfo->WPAenabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100806 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
Holger Schurig245bf202008-04-02 16:27:42 +0200807 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
808 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
809 )
810 return 1;
811 else
812 return 0;
813}
814
815static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
816 struct bss_descriptor *match_bss)
817{
818 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
Johannes Berg2c7060022008-10-30 22:09:54 +0100819 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200820 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
821 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
822 )
823 return 1;
824 else
825 return 0;
826}
827
828static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
829 struct bss_descriptor *match_bss)
830{
831 if (!secinfo->wep_enabled && !secinfo->WPAenabled
832 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100833 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
834 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200835 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
836 return 1;
837 else
838 return 0;
839}
840
841/**
842 * @brief Check if a scanned network compatible with the driver settings
843 *
844 * WEP WPA WPA2 ad-hoc encrypt Network
845 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
846 * 0 0 0 0 NONE 0 0 0 yes No security
847 * 1 0 0 0 NONE 1 0 0 yes Static WEP
848 * 0 1 0 0 x 1x 1 x yes WPA
849 * 0 0 1 0 x 1x x 1 yes WPA2
850 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
851 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
852 *
853 *
854 * @param priv A pointer to struct lbs_private
855 * @param index Index in scantable to check against current driver settings
856 * @param mode Network mode: Infrastructure or IBSS
857 *
858 * @return Index in scantable, or error code if negative
859 */
860static int is_network_compatible(struct lbs_private *priv,
861 struct bss_descriptor *bss, uint8_t mode)
862{
863 int matched = 0;
864
865 lbs_deb_enter(LBS_DEB_SCAN);
866
867 if (bss->mode != mode)
868 goto done;
869
870 matched = match_bss_no_security(&priv->secinfo, bss);
871 if (matched)
872 goto done;
873 matched = match_bss_static_wep(&priv->secinfo, bss);
874 if (matched)
875 goto done;
876 matched = match_bss_wpa(&priv->secinfo, bss);
877 if (matched) {
878 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
879 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
880 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
881 priv->secinfo.wep_enabled ? "e" : "d",
882 priv->secinfo.WPAenabled ? "e" : "d",
883 priv->secinfo.WPA2enabled ? "e" : "d",
884 (bss->capability & WLAN_CAPABILITY_PRIVACY));
885 goto done;
886 }
887 matched = match_bss_wpa2(&priv->secinfo, bss);
888 if (matched) {
889 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
890 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
891 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
892 priv->secinfo.wep_enabled ? "e" : "d",
893 priv->secinfo.WPAenabled ? "e" : "d",
894 priv->secinfo.WPA2enabled ? "e" : "d",
895 (bss->capability & WLAN_CAPABILITY_PRIVACY));
896 goto done;
897 }
898 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
899 if (matched) {
900 lbs_deb_scan("is_network_compatible() dynamic WEP: "
901 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
902 bss->wpa_ie[0], bss->rsn_ie[0],
903 (bss->capability & WLAN_CAPABILITY_PRIVACY));
904 goto done;
905 }
906
907 /* bss security settings don't match those configured on card */
908 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
909 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
910 bss->wpa_ie[0], bss->rsn_ie[0],
911 priv->secinfo.wep_enabled ? "e" : "d",
912 priv->secinfo.WPAenabled ? "e" : "d",
913 priv->secinfo.WPA2enabled ? "e" : "d",
914 (bss->capability & WLAN_CAPABILITY_PRIVACY));
915
916done:
917 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
918 return matched;
919}
920
921/**
922 * @brief This function finds a specific compatible BSSID in the scan list
923 *
924 * Used in association code
925 *
926 * @param priv A pointer to struct lbs_private
927 * @param bssid BSSID to find in the scan list
928 * @param mode Network mode: Infrastructure or IBSS
929 *
930 * @return index in BSSID list, or error return code (< 0)
931 */
932static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
933 uint8_t *bssid, uint8_t mode)
934{
935 struct bss_descriptor *iter_bss;
936 struct bss_descriptor *found_bss = NULL;
937
938 lbs_deb_enter(LBS_DEB_SCAN);
939
940 if (!bssid)
941 goto out;
942
943 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
944
945 /* Look through the scan table for a compatible match. The loop will
946 * continue past a matched bssid that is not compatible in case there
947 * is an AP with multiple SSIDs assigned to the same BSSID
948 */
949 mutex_lock(&priv->lock);
950 list_for_each_entry(iter_bss, &priv->network_list, list) {
951 if (compare_ether_addr(iter_bss->bssid, bssid))
952 continue; /* bssid doesn't match */
953 switch (mode) {
954 case IW_MODE_INFRA:
955 case IW_MODE_ADHOC:
956 if (!is_network_compatible(priv, iter_bss, mode))
957 break;
958 found_bss = iter_bss;
959 break;
960 default:
961 found_bss = iter_bss;
962 break;
963 }
964 }
965 mutex_unlock(&priv->lock);
966
967out:
968 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
969 return found_bss;
970}
971
972/**
973 * @brief This function finds ssid in ssid list.
974 *
975 * Used in association code
976 *
977 * @param priv A pointer to struct lbs_private
978 * @param ssid SSID to find in the list
979 * @param bssid BSSID to qualify the SSID selection (if provided)
980 * @param mode Network mode: Infrastructure or IBSS
981 *
982 * @return index in BSSID list
983 */
984static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
985 uint8_t *ssid, uint8_t ssid_len,
986 uint8_t *bssid, uint8_t mode,
987 int channel)
988{
989 u32 bestrssi = 0;
990 struct bss_descriptor *iter_bss = NULL;
991 struct bss_descriptor *found_bss = NULL;
992 struct bss_descriptor *tmp_oldest = NULL;
993
994 lbs_deb_enter(LBS_DEB_SCAN);
995
996 mutex_lock(&priv->lock);
997
998 list_for_each_entry(iter_bss, &priv->network_list, list) {
999 if (!tmp_oldest ||
1000 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1001 tmp_oldest = iter_bss;
1002
1003 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1004 ssid, ssid_len) != 0)
1005 continue; /* ssid doesn't match */
1006 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1007 continue; /* bssid doesn't match */
1008 if ((channel > 0) && (iter_bss->channel != channel))
1009 continue; /* channel doesn't match */
1010
1011 switch (mode) {
1012 case IW_MODE_INFRA:
1013 case IW_MODE_ADHOC:
1014 if (!is_network_compatible(priv, iter_bss, mode))
1015 break;
1016
1017 if (bssid) {
1018 /* Found requested BSSID */
1019 found_bss = iter_bss;
1020 goto out;
1021 }
1022
1023 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1024 bestrssi = SCAN_RSSI(iter_bss->rssi);
1025 found_bss = iter_bss;
1026 }
1027 break;
1028 case IW_MODE_AUTO:
1029 default:
1030 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1031 bestrssi = SCAN_RSSI(iter_bss->rssi);
1032 found_bss = iter_bss;
1033 }
1034 break;
1035 }
1036 }
1037
1038out:
1039 mutex_unlock(&priv->lock);
1040 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1041 return found_bss;
1042}
1043
Holger Schurig69f90322007-11-23 15:43:44 +01001044static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001045 struct assoc_request * assoc_req)
1046{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001047 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001048 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001049 int channel = -1;
John W. Linville9387b7c2008-09-30 20:59:05 -04001050 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051
Holger Schurig9012b282007-05-25 11:27:16 -04001052 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001053
Dan Williamsef9a2642007-05-25 16:46:33 -04001054 /* FIXME: take channel into account when picking SSIDs if a channel
1055 * is set.
1056 */
1057
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001058 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1059 channel = assoc_req->channel;
1060
Holger Schurig0765af42007-10-15 12:55:56 +02001061 lbs_deb_assoc("SSID '%s' requested\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001062 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -04001063 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -05001064 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001065 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001066
David Woodhouseaa21c002007-12-08 20:04:36 +00001067 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001068 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001069 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -04001070 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams822ac032009-05-22 20:07:14 -04001071 ret = lbs_try_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001072 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -04001073 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001074 }
Dan Williams0dc5a292007-05-10 22:58:02 -04001075 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076 /* Scan for the network, do not save previous results. Stale
1077 * scan data will cause us to join a non-existant adhoc network
1078 */
Holger Schurig10078322007-11-15 18:05:47 -05001079 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001080 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001081
1082 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001083 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001084 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001085 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -04001086 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001087 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001088 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001089 } else {
1090 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -04001091 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001092 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Holger Schurig243e84e2009-10-22 15:30:47 +02001093 IEEE80211_MAX_SSID_LEN);
Dan Williamsd8efea22007-05-28 23:54:55 -04001094 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001095 lbs_adhoc_start(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001096 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001097 }
1098
Holger Schurig9012b282007-05-25 11:27:16 -04001099 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001100 return ret;
1101}
1102
1103
Holger Schurig69f90322007-11-23 15:43:44 +01001104static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001105 struct assoc_request * assoc_req)
1106{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001107 int ret = 0;
1108 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001109
Johannes Berge1749612008-10-27 15:59:26 -07001110 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001111
1112 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +00001113 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001114 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001115 if (bss == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -07001116 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1117 "cannot associate.\n", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001118 goto out;
1119 }
1120
Dan Williamse76850d2007-05-25 17:09:41 -04001121 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -04001122 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williams822ac032009-05-22 20:07:14 -04001123 ret = lbs_try_associate(priv, assoc_req);
1124 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1125 ret);
Dan Williams0dc5a292007-05-10 22:58:02 -04001126 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001127 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001128 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001129
1130out:
Holger Schurig9012b282007-05-25 11:27:16 -04001131 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001132 return ret;
1133}
1134
1135
Holger Schurig69f90322007-11-23 15:43:44 +01001136static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001137 struct assoc_request * assoc_req)
1138{
1139 int ret = 0, done = 0;
1140
Holger Schurig0765af42007-10-15 12:55:56 +02001141 lbs_deb_enter(LBS_DEB_ASSOC);
1142
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001143 /* If we're given and 'any' BSSID, try associating based on SSID */
1144
1145 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001146 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1147 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001148 ret = assoc_helper_bssid(priv, assoc_req);
1149 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001150 }
1151 }
1152
1153 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1154 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001155 }
1156
Holger Schurig0765af42007-10-15 12:55:56 +02001157 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001158 return ret;
1159}
1160
1161
Holger Schurig69f90322007-11-23 15:43:44 +01001162static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001163 struct assoc_request * assoc_req)
1164{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001165 int ret = 0;
1166
Holger Schurig9012b282007-05-25 11:27:16 -04001167 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001168
David Woodhouseaa21c002007-12-08 20:04:36 +00001169 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -04001170 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171
Dan Williams0dc5a292007-05-10 22:58:02 -04001172 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001173 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -05001174 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +00001175 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001176 }
1177
David Woodhouseaa21c002007-12-08 20:04:36 +00001178 priv->mode = assoc_req->mode;
Dan Williams39fcf7a2008-09-10 12:49:00 -04001179 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001180
Holger Schurig9012b282007-05-25 11:27:16 -04001181done:
1182 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001183 return ret;
1184}
1185
Holger Schurig69f90322007-11-23 15:43:44 +01001186static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -04001187 struct assoc_request * assoc_req)
1188{
Dan Williamsef9a2642007-05-25 16:46:33 -04001189 int ret = 0;
1190
1191 lbs_deb_enter(LBS_DEB_ASSOC);
1192
David Woodhouse9f462572007-12-12 22:50:21 -05001193 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001194 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001195 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001196 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -04001197 }
1198
Holger Schurigc14951f2009-10-22 15:30:50 +02001199 if (assoc_req->channel == priv->channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001200 goto done;
1201
David Woodhouse8642f1f2007-12-11 20:03:01 -05001202 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -05001203 /* Change mesh channel first; 21.p21 firmware won't let
1204 you change channel otherwise (even though it'll return
1205 an error to this */
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001206 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1207 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001208 }
1209
Dan Williamsef9a2642007-05-25 16:46:33 -04001210 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
Holger Schurigc14951f2009-10-22 15:30:50 +02001211 priv->channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -04001212
Dan Williams2dd4b262007-12-11 16:54:15 -05001213 ret = lbs_set_channel(priv, assoc_req->channel);
1214 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -05001215 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -04001216
Dan Williams2dd4b262007-12-11 16:54:15 -05001217 /* FIXME: shouldn't need to grab the channel _again_ after setting
1218 * it since the firmware is supposed to return the new channel, but
1219 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -05001220 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001221 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001222 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001223 goto done;
1224 }
Dan Williamsef9a2642007-05-25 16:46:33 -04001225
Holger Schurigc14951f2009-10-22 15:30:50 +02001226 if (assoc_req->channel != priv->channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -05001227 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -04001228 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001229 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -04001230 }
1231
1232 if ( assoc_req->secinfo.wep_enabled
1233 && (assoc_req->wep_keys[0].len
1234 || assoc_req->wep_keys[1].len
1235 || assoc_req->wep_keys[2].len
1236 || assoc_req->wep_keys[3].len)) {
1237 /* Make sure WEP keys are re-sent to firmware */
1238 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1239 }
1240
1241 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -05001242 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -04001243
David Woodhouse8642f1f2007-12-11 20:03:01 -05001244 restore_mesh:
1245 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001246 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
Holger Schurigc14951f2009-10-22 15:30:50 +02001247 priv->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001248
1249 done:
Dan Williamsef9a2642007-05-25 16:46:33 -04001250 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1251 return ret;
1252}
1253
1254
Holger Schurig69f90322007-11-23 15:43:44 +01001255static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -05001256 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001257{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001258 int i;
1259 int ret = 0;
1260
Holger Schurig9012b282007-05-25 11:27:16 -04001261 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001262
1263 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -05001264 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1265 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1266 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1267 else
1268 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001269
1270 if (ret)
1271 goto out;
1272
1273 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -04001274 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +01001275 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001276 else
Holger Schurigd9e97782008-03-12 16:06:43 +01001277 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -05001278
Holger Schurigc97329e2008-03-18 11:20:21 +01001279 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001280
David Woodhouseaa21c002007-12-08 20:04:36 +00001281 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001282
David Woodhouseaa21c002007-12-08 20:04:36 +00001283 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001284 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001285 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -05001286 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001287 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001288 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001289
David Woodhouseaa21c002007-12-08 20:04:36 +00001290 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001291
1292out:
Holger Schurig9012b282007-05-25 11:27:16 -04001293 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001294 return ret;
1295}
1296
Holger Schurig69f90322007-11-23 15:43:44 +01001297static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001298 struct assoc_request * assoc_req)
1299{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001300 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -05001301 uint16_t do_wpa;
1302 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001303
Holger Schurig9012b282007-05-25 11:27:16 -04001304 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001305
David Woodhouseaa21c002007-12-08 20:04:36 +00001306 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001307 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001308
Holger Schurigc97329e2008-03-18 11:20:21 +01001309 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001310
Dan Williams18c96c342007-06-18 12:01:12 -04001311 /* If RSN is already enabled, don't try to enable it again, since
1312 * ENABLE_RSN resets internal state machines and will clobber the
1313 * 4-way WPA handshake.
1314 */
1315
1316 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001317 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -04001318 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001319 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -04001320 goto out;
1321 }
1322
1323 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001324 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -04001325 if (do_wpa == rsn)
1326 goto out;
1327
1328 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001329 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -04001330
1331out:
Holger Schurig9012b282007-05-25 11:27:16 -04001332 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001333 return ret;
1334}
1335
1336
Holger Schurig69f90322007-11-23 15:43:44 +01001337static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001338 struct assoc_request * assoc_req)
1339{
1340 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -04001341 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001342
Holger Schurig9012b282007-05-25 11:27:16 -04001343 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001344
Dan Williams2bcde512007-10-03 10:37:45 -04001345 /* Work around older firmware bug where WPA unicast and multicast
1346 * keys must be set independently. Seen in SDIO parts with firmware
1347 * version 5.0.11p0.
1348 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001349
Dan Williams2bcde512007-10-03 10:37:45 -04001350 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1351 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +01001352 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001353 assoc_req->flags = flags;
1354 }
1355
1356 if (ret)
1357 goto out;
1358
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001359 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1360 sizeof(struct enc_key));
1361
Dan Williams2bcde512007-10-03 10:37:45 -04001362 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1363 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1364
David Woodhouse9e1228d2008-03-03 12:15:39 +01001365 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001366 assoc_req->flags = flags;
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001367
1368 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1369 sizeof(struct enc_key));
Dan Williams2bcde512007-10-03 10:37:45 -04001370 }
1371
1372out:
Holger Schurig9012b282007-05-25 11:27:16 -04001373 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001374 return ret;
1375}
1376
1377
Holger Schurig69f90322007-11-23 15:43:44 +01001378static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001379 struct assoc_request * assoc_req)
1380{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001381 int ret = 0;
1382
Holger Schurig9012b282007-05-25 11:27:16 -04001383 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001384
1385 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001386 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1387 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001388 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +00001389 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1390 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001391 }
1392
Holger Schurig9012b282007-05-25 11:27:16 -04001393 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001394 return ret;
1395}
1396
1397
David Woodhouseaa21c002007-12-08 20:04:36 +00001398static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001399 struct assoc_request * assoc_req)
1400{
Holger Schurig0765af42007-10-15 12:55:56 +02001401 int ret = 0;
1402
David Woodhouseaa21c002007-12-08 20:04:36 +00001403 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001404 return 0;
1405
Holger Schurig52507c22008-01-28 17:25:53 +01001406 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001407 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001408 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1409 ret = 1;
1410 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001411 }
1412
1413 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001414 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +02001415 lbs_deb_assoc("Deauthenticating due to new security\n");
1416 ret = 1;
1417 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001418 }
1419 }
1420
1421 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001422 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1423 ret = 1;
1424 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001425 }
1426
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001427 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001428 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1429 ret = 1;
1430 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001431 }
1432
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001433 /* FIXME: deal with 'auto' mode somehow */
1434 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001435 if (assoc_req->mode != IW_MODE_INFRA) {
1436 lbs_deb_assoc("Deauthenticating due to leaving "
1437 "infra mode\n");
1438 ret = 1;
1439 goto out;
1440 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001441 }
1442
Holger Schurig0765af42007-10-15 12:55:56 +02001443out:
1444 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +01001445 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001446}
1447
1448
David Woodhouseaa21c002007-12-08 20:04:36 +00001449static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001450 struct assoc_request * assoc_req)
1451{
Holger Schurig0765af42007-10-15 12:55:56 +02001452 lbs_deb_enter(LBS_DEB_ASSOC);
1453
David Woodhouseaa21c002007-12-08 20:04:36 +00001454 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001455 return 0;
1456
David Woodhouseaa21c002007-12-08 20:04:36 +00001457 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1458 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001459 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001460 return 1;
1461
1462 /* FIXME: deal with 'auto' mode somehow */
1463 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001464 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001465 return 1;
1466 }
1467
Dan Williamsef9a2642007-05-25 16:46:33 -04001468 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurigc14951f2009-10-22 15:30:50 +02001469 if (assoc_req->channel != priv->channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001470 return 1;
1471 }
1472
Holger Schurig0765af42007-10-15 12:55:56 +02001473 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001474 return 0;
1475}
1476
1477
Holger Schurig245bf202008-04-02 16:27:42 +02001478/**
1479 * @brief This function finds the best SSID in the Scan List
1480 *
1481 * Search the scan table for the best SSID that also matches the current
1482 * adapter network preference (infrastructure or adhoc)
1483 *
1484 * @param priv A pointer to struct lbs_private
1485 *
1486 * @return index in BSSID list
1487 */
1488static struct bss_descriptor *lbs_find_best_ssid_in_list(
1489 struct lbs_private *priv, uint8_t mode)
1490{
1491 uint8_t bestrssi = 0;
1492 struct bss_descriptor *iter_bss;
1493 struct bss_descriptor *best_bss = NULL;
1494
1495 lbs_deb_enter(LBS_DEB_SCAN);
1496
1497 mutex_lock(&priv->lock);
1498
1499 list_for_each_entry(iter_bss, &priv->network_list, list) {
1500 switch (mode) {
1501 case IW_MODE_INFRA:
1502 case IW_MODE_ADHOC:
1503 if (!is_network_compatible(priv, iter_bss, mode))
1504 break;
1505 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1506 break;
1507 bestrssi = SCAN_RSSI(iter_bss->rssi);
1508 best_bss = iter_bss;
1509 break;
1510 case IW_MODE_AUTO:
1511 default:
1512 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1513 break;
1514 bestrssi = SCAN_RSSI(iter_bss->rssi);
1515 best_bss = iter_bss;
1516 break;
1517 }
1518 }
1519
1520 mutex_unlock(&priv->lock);
1521 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1522 return best_bss;
1523}
1524
1525/**
1526 * @brief Find the best AP
1527 *
1528 * Used from association worker.
1529 *
1530 * @param priv A pointer to struct lbs_private structure
1531 * @param pSSID A pointer to AP's ssid
1532 *
1533 * @return 0--success, otherwise--fail
1534 */
1535static int lbs_find_best_network_ssid(struct lbs_private *priv,
1536 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1537 uint8_t *out_mode)
1538{
1539 int ret = -1;
1540 struct bss_descriptor *found;
1541
1542 lbs_deb_enter(LBS_DEB_SCAN);
1543
1544 priv->scan_ssid_len = 0;
1545 lbs_scan_networks(priv, 1);
1546 if (priv->surpriseremoved)
1547 goto out;
1548
1549 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1550 if (found && (found->ssid_len > 0)) {
Holger Schurig243e84e2009-10-22 15:30:47 +02001551 memcpy(out_ssid, &found->ssid, IEEE80211_MAX_SSID_LEN);
Holger Schurig245bf202008-04-02 16:27:42 +02001552 *out_ssid_len = found->ssid_len;
1553 *out_mode = found->mode;
1554 ret = 0;
1555 }
1556
1557out:
1558 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1559 return ret;
1560}
1561
1562
Holger Schurig10078322007-11-15 18:05:47 -05001563void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001564{
Holger Schurig69f90322007-11-23 15:43:44 +01001565 struct lbs_private *priv = container_of(work, struct lbs_private,
1566 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001567 struct assoc_request * assoc_req = NULL;
1568 int ret = 0;
1569 int find_any_ssid = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001570 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001571
Holger Schurig9012b282007-05-25 11:27:16 -04001572 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001573
David Woodhouseaa21c002007-12-08 20:04:36 +00001574 mutex_lock(&priv->lock);
1575 assoc_req = priv->pending_assoc_req;
1576 priv->pending_assoc_req = NULL;
1577 priv->in_progress_assoc_req = assoc_req;
1578 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001579
Holger Schurig9012b282007-05-25 11:27:16 -04001580 if (!assoc_req)
1581 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001582
Holger Schurig0765af42007-10-15 12:55:56 +02001583 lbs_deb_assoc(
1584 "Association Request:\n"
1585 " flags: 0x%08lx\n"
1586 " SSID: '%s'\n"
1587 " chann: %d\n"
1588 " band: %d\n"
1589 " mode: %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001590 " BSSID: %pM\n"
Holger Schurig0765af42007-10-15 12:55:56 +02001591 " secinfo: %s%s%s\n"
1592 " auth_mode: %d\n",
1593 assoc_req->flags,
John W. Linville9387b7c2008-09-30 20:59:05 -04001594 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Holger Schurig0765af42007-10-15 12:55:56 +02001595 assoc_req->channel, assoc_req->band, assoc_req->mode,
Johannes Berge1749612008-10-27 15:59:26 -07001596 assoc_req->bssid,
Holger Schurig0765af42007-10-15 12:55:56 +02001597 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1598 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1599 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1600 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001601
1602 /* If 'any' SSID was specified, find an SSID to associate with */
1603 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001604 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001605 find_any_ssid = 1;
1606
1607 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1608 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001609 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1610 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001611 find_any_ssid = 0;
1612 }
1613
1614 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02001615 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001616
Holger Schurig10078322007-11-15 18:05:47 -05001617 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001618 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001619 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001620 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001621 ret = -ENETUNREACH;
1622 goto out;
1623 }
1624
1625 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04001626 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001627 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1628 assoc_req->mode = new_mode;
1629 }
1630 }
1631
1632 /*
1633 * Check if the attributes being changing require deauthentication
1634 * from the currently associated infrastructure access point.
1635 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001636 if (priv->mode == IW_MODE_INFRA) {
1637 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04001638 ret = lbs_cmd_80211_deauthenticate(priv,
1639 priv->curbssparams.bssid,
1640 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001641 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001642 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001643 "configuration request failed: %d\n",
1644 ret);
1645 }
1646 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001647 } else if (priv->mode == IW_MODE_ADHOC) {
1648 if (should_stop_adhoc(priv, assoc_req)) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001649 ret = lbs_adhoc_stop(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001650 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001651 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001652 "new configuration request failed: %d\n",
1653 ret);
1654 }
1655
1656 }
1657 }
1658
1659 /* Send the various configuration bits to the firmware */
1660 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1661 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001662 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001663 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001664 }
1665
Dan Williamsef9a2642007-05-25 16:46:33 -04001666 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1667 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001668 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04001669 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04001670 }
1671
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001672 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1673 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1674 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001675 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001676 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001677 }
1678
1679 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1680 ret = assoc_helper_secinfo(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_WPA_IE, &assoc_req->flags)) {
1686 ret = assoc_helper_wpa_ie(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_MCAST_KEY, &assoc_req->flags)
1692 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1693 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001694 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001695 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001696 }
1697
1698 /* SSID/BSSID should be the _last_ config option set, because they
1699 * trigger the association attempt.
1700 */
1701 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1702 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1703 int success = 1;
1704
1705 ret = assoc_helper_associate(priv, assoc_req);
1706 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01001707 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001708 ret);
1709 success = 0;
1710 }
1711
David Woodhouseaa21c002007-12-08 20:04:36 +00001712 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01001713 lbs_deb_assoc("ASSOC: association unsuccessful, "
1714 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001715 success = 0;
1716 }
1717
1718 if (success) {
Johannes Berge1749612008-10-27 15:59:26 -07001719 lbs_deb_assoc("associated to %pM\n",
1720 priv->curbssparams.bssid);
Holger Schurig10078322007-11-15 18:05:47 -05001721 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04001722 CMD_802_11_RSSI,
1723 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001724 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001725 ret = -1;
1726 }
1727 }
1728
1729out:
1730 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001731 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001732 ret);
1733 }
Dan Williamse76850d2007-05-25 17:09:41 -04001734
David Woodhouseaa21c002007-12-08 20:04:36 +00001735 mutex_lock(&priv->lock);
1736 priv->in_progress_assoc_req = NULL;
1737 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001738 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04001739
1740done:
1741 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001742}
1743
1744
1745/*
1746 * Caller MUST hold any necessary locks
1747 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001748struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001749{
1750 struct assoc_request * assoc_req;
1751
Holger Schurig0765af42007-10-15 12:55:56 +02001752 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00001753 if (!priv->pending_assoc_req) {
1754 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04001755 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00001756 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001757 lbs_pr_info("Not enough memory to allocate association"
1758 " request!\n");
1759 return NULL;
1760 }
1761 }
1762
1763 /* Copy current configuration attributes to the association request,
1764 * but don't overwrite any that are already set.
1765 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001766 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001767 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001768 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Holger Schurig243e84e2009-10-22 15:30:47 +02001769 IEEE80211_MAX_SSID_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001770 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001771 }
1772
1773 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
Holger Schurigc14951f2009-10-22 15:30:50 +02001774 assoc_req->channel = priv->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001775
Dan Williamse76850d2007-05-25 17:09:41 -04001776 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001777 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04001778
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001779 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001780 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001781
1782 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001783 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001784 ETH_ALEN);
1785 }
1786
1787 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1788 int i;
1789 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001790 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04001791 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001792 }
1793 }
1794
1795 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001796 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001797
1798 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001799 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001800 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001801 }
1802
1803 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001804 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001805 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001806 }
1807
1808 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001809 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001810 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001811 }
1812
1813 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001814 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001815 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001816 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001817 }
1818
Holger Schurig0765af42007-10-15 12:55:56 +02001819 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001820 return assoc_req;
1821}
Holger Schurig697900a2008-04-02 16:27:10 +02001822
1823
1824/**
Dan Williams191bb402008-08-21 17:46:18 -04001825 * @brief Deauthenticate from a specific BSS
1826 *
1827 * @param priv A pointer to struct lbs_private structure
1828 * @param bssid The specific BSS to deauthenticate from
1829 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1830 *
1831 * @return 0 on success, error on failure
1832 */
1833int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1834 u16 reason)
Holger Schurig697900a2008-04-02 16:27:10 +02001835{
Dan Williams191bb402008-08-21 17:46:18 -04001836 struct cmd_ds_802_11_deauthenticate cmd;
1837 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001838
1839 lbs_deb_enter(LBS_DEB_JOIN);
1840
Dan Williams191bb402008-08-21 17:46:18 -04001841 memset(&cmd, 0, sizeof(cmd));
1842 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1843 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1844 cmd.reasoncode = cpu_to_le16(reason);
Holger Schurig697900a2008-04-02 16:27:10 +02001845
Dan Williams191bb402008-08-21 17:46:18 -04001846 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001847
Dan Williams191bb402008-08-21 17:46:18 -04001848 /* Clean up everything even if there was an error; can't assume that
1849 * we're still authenticated to the AP after trying to deauth.
1850 */
1851 lbs_mac_event_disconnected(priv);
Holger Schurig697900a2008-04-02 16:27:10 +02001852
1853 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04001854 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001855}
1856