blob: dd8732611ba92d06ac94a15443dcc2d884a2a4ef [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/* Copyright (C) 2006, Red Hat, Inc. */
2
John W. Linville7e272fc2008-09-24 18:13:14 -04003#include <linux/types.h>
Dan Williams3cf20932007-05-25 17:28:30 -04004#include <linux/etherdevice.h>
Johannes Berg2c7060022008-10-30 22:09:54 +01005#include <linux/ieee80211.h>
6#include <linux/if_arp.h>
John W. Linville7e272fc2008-09-24 18:13:14 -04007#include <net/lib80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02008
9#include "assoc.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020010#include "decl.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020011#include "host.h"
Holger Schurig245bf202008-04-02 16:27:42 +020012#include "scan.h"
Dan Williams2dd4b262007-12-11 16:54:15 -050013#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014
Ihar Hrachyshka5a6e0432008-01-25 14:15:00 +010015static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
16 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
17static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
18 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020019
Dan Williamsbe0d76e2009-05-22 20:05:25 -040020/* The firmware needs the following bits masked out of the beacon-derived
21 * capability field when associating/joining to a BSS:
22 * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
Holger Schurig697900a2008-04-02 16:27:10 +020023 */
24#define CAPINFO_MASK (~(0xda00))
25
26
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040027/**
28 * @brief This function finds common rates between rates and card rates.
29 *
30 * It will fill common rates in rates as output if found.
31 *
32 * NOTE: Setting the MSB of the basic rates need to be taken
33 * care, either before or after calling this function
34 *
35 * @param priv A pointer to struct lbs_private structure
36 * @param rates the buffer which keeps input and output
Dan Williamsca4fe302009-08-21 09:35:20 -050037 * @param rates_size the size of rates buffer; new size of buffer on return,
38 * which will be less than or equal to original rates_size
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040039 *
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{
Roel Kluin1e3d31c2009-08-02 09:44:12 +020046 int i, j;
Dan Williamsca4fe302009-08-21 09:35:20 -050047 u8 intersection[MAX_RATES];
48 u16 intersection_size;
49 u16 num_rates = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040050
Dan Williamsca4fe302009-08-21 09:35:20 -050051 intersection_size = min_t(u16, *rates_size, ARRAY_SIZE(intersection));
Andrey Yurovsky6f632d52009-08-13 17:34:40 -070052
Dan Williamsca4fe302009-08-21 09:35:20 -050053 /* Allow each rate from 'rates' that is supported by the hardware */
54 for (i = 0; i < ARRAY_SIZE(lbs_bg_rates) && lbs_bg_rates[i]; i++) {
55 for (j = 0; j < intersection_size && rates[j]; j++) {
56 if (rates[j] == lbs_bg_rates[i])
57 intersection[num_rates++] = rates[j];
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040058 }
59 }
60
61 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
Dan Williamsca4fe302009-08-21 09:35:20 -050062 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", lbs_bg_rates,
63 ARRAY_SIZE(lbs_bg_rates));
64 lbs_deb_hex(LBS_DEB_JOIN, "common rates", intersection, num_rates);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040065 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
66
67 if (!priv->enablehwauto) {
Dan Williamsca4fe302009-08-21 09:35:20 -050068 for (i = 0; i < num_rates; i++) {
69 if (intersection[i] == priv->cur_rate)
70 goto done;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040071 }
Dan Williamsca4fe302009-08-21 09:35:20 -050072 lbs_pr_alert("Previously set fixed data rate %#x isn't "
73 "compatible with the network.\n", priv->cur_rate);
74 return -1;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040075 }
Dan Williamsca4fe302009-08-21 09:35:20 -050076
77done:
78 memset(rates, 0, *rates_size);
79 *rates_size = num_rates;
80 memcpy(rates, intersection, num_rates);
Roel Kluin1e3d31c2009-08-02 09:44:12 +020081 return 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040082}
83
84
85/**
86 * @brief Sets the MSB on basic rates as the firmware requires
87 *
88 * Scan through an array and set the MSB for basic data rates.
89 *
90 * @param rates buffer of data rates
91 * @param len size of buffer
92 */
93static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
94{
95 int i;
96
97 for (i = 0; i < len; i++) {
98 if (rates[i] == 0x02 || rates[i] == 0x04 ||
99 rates[i] == 0x0b || rates[i] == 0x16)
100 rates[i] |= 0x80;
101 }
102}
103
Holger Schurig697900a2008-04-02 16:27:10 +0200104
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400105static u8 iw_auth_to_ieee_auth(u8 auth)
106{
107 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
108 return 0x00;
109 else if (auth == IW_AUTH_ALG_SHARED_KEY)
110 return 0x01;
111 else if (auth == IW_AUTH_ALG_LEAP)
112 return 0x80;
113
114 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
115 return 0;
116}
117
118/**
119 * @brief This function prepares the authenticate command. AUTHENTICATE only
120 * sets the authentication suite for future associations, as the firmware
121 * handles authentication internally during the ASSOCIATE command.
122 *
123 * @param priv A pointer to struct lbs_private structure
124 * @param bssid The peer BSSID with which to authenticate
125 * @param auth The authentication mode to use (from wireless.h)
126 *
127 * @return 0 or -1
128 */
129static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
130{
131 struct cmd_ds_802_11_authenticate cmd;
132 int ret = -1;
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400133
134 lbs_deb_enter(LBS_DEB_JOIN);
135
136 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
137 memcpy(cmd.bssid, bssid, ETH_ALEN);
138
139 cmd.authtype = iw_auth_to_ieee_auth(auth);
140
Johannes Berge91d8332009-07-15 17:21:41 +0200141 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400142
143 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
144
145 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
146 return ret;
147}
148
Dan Williams822ac032009-05-22 20:07:14 -0400149
150static int lbs_assoc_post(struct lbs_private *priv,
151 struct cmd_ds_802_11_associate_response *resp)
152{
153 int ret = 0;
154 union iwreq_data wrqu;
155 struct bss_descriptor *bss;
156 u16 status_code;
157
158 lbs_deb_enter(LBS_DEB_ASSOC);
159
160 if (!priv->in_progress_assoc_req) {
161 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
162 ret = -1;
163 goto done;
164 }
165 bss = &priv->in_progress_assoc_req->bss;
166
167 /*
168 * Older FW versions map the IEEE 802.11 Status Code in the association
169 * response to the following values returned in resp->statuscode:
170 *
171 * IEEE Status Code Marvell Status Code
172 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
173 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
174 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
175 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
176 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
177 * others -> 0x0003 ASSOC_RESULT_REFUSED
178 *
179 * Other response codes:
180 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
181 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
182 * association response from the AP)
183 */
184
185 status_code = le16_to_cpu(resp->statuscode);
186 if (priv->fwrelease < 0x09000000) {
187 switch (status_code) {
188 case 0x00:
189 break;
190 case 0x01:
191 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
192 break;
193 case 0x02:
194 lbs_deb_assoc("ASSOC_RESP: internal timer "
195 "expired while waiting for the AP\n");
196 break;
197 case 0x03:
198 lbs_deb_assoc("ASSOC_RESP: association "
199 "refused by AP\n");
200 break;
201 case 0x04:
202 lbs_deb_assoc("ASSOC_RESP: authentication "
203 "refused by AP\n");
204 break;
205 default:
206 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
207 " unknown\n", status_code);
208 break;
209 }
210 } else {
211 /* v9+ returns the AP's association response */
212 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
213 }
214
215 if (status_code) {
216 lbs_mac_event_disconnected(priv);
217 ret = -1;
218 goto done;
219 }
220
221 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
222 (void *) (resp + sizeof (resp->hdr)),
223 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
224
225 /* Send a Media Connected event, according to the Spec */
226 priv->connect_status = LBS_CONNECTED;
227
228 /* Update current SSID and BSSID */
229 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
230 priv->curbssparams.ssid_len = bss->ssid_len;
231 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
232
233 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
234 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
235
236 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
237 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
238 priv->nextSNRNF = 0;
239 priv->numSNRNF = 0;
240
241 netif_carrier_on(priv->dev);
242 if (!priv->tx_pending_len)
243 netif_wake_queue(priv->dev);
244
245 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
246 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
247 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
248
249done:
250 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
251 return ret;
252}
253
254/**
255 * @brief This function prepares an association-class command.
256 *
257 * @param priv A pointer to struct lbs_private structure
258 * @param assoc_req The association request describing the BSS to associate
259 * or reassociate with
260 * @param command The actual command, either CMD_802_11_ASSOCIATE or
261 * CMD_802_11_REASSOCIATE
262 *
263 * @return 0 or -1
264 */
265static int lbs_associate(struct lbs_private *priv,
266 struct assoc_request *assoc_req,
267 u16 command)
268{
269 struct cmd_ds_802_11_associate cmd;
270 int ret = 0;
271 struct bss_descriptor *bss = &assoc_req->bss;
272 u8 *pos = &(cmd.iebuf[0]);
273 u16 tmpcap, tmplen, tmpauth;
274 struct mrvl_ie_ssid_param_set *ssid;
275 struct mrvl_ie_ds_param_set *ds;
276 struct mrvl_ie_cf_param_set *cf;
277 struct mrvl_ie_rates_param_set *rates;
278 struct mrvl_ie_rsn_param_set *rsn;
279 struct mrvl_ie_auth_type *auth;
280
281 lbs_deb_enter(LBS_DEB_ASSOC);
282
283 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
284 (command != CMD_802_11_REASSOCIATE));
285
286 memset(&cmd, 0, sizeof(cmd));
287 cmd.hdr.command = cpu_to_le16(command);
288
289 /* Fill in static fields */
290 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
291 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
292
293 /* Capability info */
294 tmpcap = (bss->capability & CAPINFO_MASK);
295 if (bss->mode == IW_MODE_INFRA)
296 tmpcap |= WLAN_CAPABILITY_ESS;
297 cmd.capability = cpu_to_le16(tmpcap);
298 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
299
300 /* SSID */
301 ssid = (struct mrvl_ie_ssid_param_set *) pos;
302 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
303 tmplen = bss->ssid_len;
304 ssid->header.len = cpu_to_le16(tmplen);
305 memcpy(ssid->ssid, bss->ssid, tmplen);
306 pos += sizeof(ssid->header) + tmplen;
307
308 ds = (struct mrvl_ie_ds_param_set *) pos;
309 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
310 ds->header.len = cpu_to_le16(1);
311 ds->channel = bss->phy.ds.channel;
312 pos += sizeof(ds->header) + 1;
313
314 cf = (struct mrvl_ie_cf_param_set *) pos;
315 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
316 tmplen = sizeof(*cf) - sizeof (cf->header);
317 cf->header.len = cpu_to_le16(tmplen);
318 /* IE payload should be zeroed, firmware fills it in for us */
319 pos += sizeof(*cf);
320
321 rates = (struct mrvl_ie_rates_param_set *) pos;
322 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
Dan Williamsca4fe302009-08-21 09:35:20 -0500323 tmplen = min_t(u16, ARRAY_SIZE(bss->rates), MAX_RATES);
Roel Kluin1e3d31c2009-08-02 09:44:12 +0200324 memcpy(&rates->rates, &bss->rates, tmplen);
Dan Williams822ac032009-05-22 20:07:14 -0400325 if (get_common_rates(priv, rates->rates, &tmplen)) {
326 ret = -1;
327 goto done;
328 }
329 pos += sizeof(rates->header) + tmplen;
330 rates->header.len = cpu_to_le16(tmplen);
331 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
332
333 /* Copy the infra. association rates into Current BSS state structure */
334 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
335 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
336
337 /* Set MSB on basic rates as the firmware requires, but _after_
338 * copying to current bss rates.
339 */
340 lbs_set_basic_rate_flags(rates->rates, tmplen);
341
342 /* Firmware v9+ indicate authentication suites as a TLV */
343 if (priv->fwrelease >= 0x09000000) {
Dan Williams822ac032009-05-22 20:07:14 -0400344 auth = (struct mrvl_ie_auth_type *) pos;
345 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
346 auth->header.len = cpu_to_le16(2);
347 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
348 auth->auth = cpu_to_le16(tmpauth);
349 pos += sizeof(auth->header) + 2;
350
Johannes Berge91d8332009-07-15 17:21:41 +0200351 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
352 bss->bssid, priv->secinfo.auth_mode);
Dan Williams822ac032009-05-22 20:07:14 -0400353 }
354
355 /* WPA/WPA2 IEs */
356 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
357 rsn = (struct mrvl_ie_rsn_param_set *) pos;
358 /* WPA_IE or WPA2_IE */
359 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
360 tmplen = (u16) assoc_req->wpa_ie[1];
361 rsn->header.len = cpu_to_le16(tmplen);
362 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
363 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
364 sizeof(rsn->header) + tmplen);
365 pos += sizeof(rsn->header) + tmplen;
366 }
367
368 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
369 (u16)(pos - (u8 *) &cmd.iebuf));
370
371 /* update curbssparams */
372 priv->curbssparams.channel = bss->phy.ds.channel;
373
374 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
375 ret = -1;
376 goto done;
377 }
378
379 ret = lbs_cmd_with_response(priv, command, &cmd);
380 if (ret == 0) {
381 ret = lbs_assoc_post(priv,
382 (struct cmd_ds_802_11_associate_response *) &cmd);
383 }
384
385done:
386 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
387 return ret;
388}
389
Holger Schurig697900a2008-04-02 16:27:10 +0200390/**
391 * @brief Associate to a specific BSS discovered in a scan
392 *
393 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400394 * @param assoc_req The association request describing the BSS to associate with
Holger Schurig697900a2008-04-02 16:27:10 +0200395 *
396 * @return 0-success, otherwise fail
397 */
Dan Williams822ac032009-05-22 20:07:14 -0400398static int lbs_try_associate(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200399 struct assoc_request *assoc_req)
400{
401 int ret;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400402 u8 preamble = RADIO_PREAMBLE_LONG;
Holger Schurig697900a2008-04-02 16:27:10 +0200403
404 lbs_deb_enter(LBS_DEB_ASSOC);
405
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400406 /* FW v9 and higher indicate authentication suites as a TLV in the
407 * association command, not as a separate authentication command.
408 */
409 if (priv->fwrelease < 0x09000000) {
410 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
411 priv->secinfo.auth_mode);
412 if (ret)
413 goto out;
414 }
Holger Schurig697900a2008-04-02 16:27:10 +0200415
Dan Williamsd5db2df2008-08-21 17:51:07 -0400416 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig697900a2008-04-02 16:27:10 +0200417 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
418 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
Dan Williamsd5db2df2008-08-21 17:51:07 -0400419 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200420
Dan Williamsd5db2df2008-08-21 17:51:07 -0400421 ret = lbs_set_radio(priv, preamble, 1);
422 if (ret)
423 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200424
Dan Williams822ac032009-05-22 20:07:14 -0400425 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
Holger Schurig697900a2008-04-02 16:27:10 +0200426
Dan Williamsd5db2df2008-08-21 17:51:07 -0400427out:
Holger Schurig697900a2008-04-02 16:27:10 +0200428 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
429 return ret;
430}
431
Dan Williams822ac032009-05-22 20:07:14 -0400432static int lbs_adhoc_post(struct lbs_private *priv,
433 struct cmd_ds_802_11_ad_hoc_result *resp)
434{
435 int ret = 0;
436 u16 command = le16_to_cpu(resp->hdr.command);
437 u16 result = le16_to_cpu(resp->hdr.result);
438 union iwreq_data wrqu;
439 struct bss_descriptor *bss;
440 DECLARE_SSID_BUF(ssid);
441
442 lbs_deb_enter(LBS_DEB_JOIN);
443
444 if (!priv->in_progress_assoc_req) {
445 lbs_deb_join("ADHOC_RESP: no in-progress association "
446 "request\n");
447 ret = -1;
448 goto done;
449 }
450 bss = &priv->in_progress_assoc_req->bss;
451
452 /*
453 * Join result code 0 --> SUCCESS
454 */
455 if (result) {
456 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
457 if (priv->connect_status == LBS_CONNECTED)
458 lbs_mac_event_disconnected(priv);
459 ret = -1;
460 goto done;
461 }
462
463 /* Send a Media Connected event, according to the Spec */
464 priv->connect_status = LBS_CONNECTED;
465
466 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
467 /* Update the created network descriptor with the new BSSID */
468 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
469 }
470
471 /* Set the BSSID from the joined/started descriptor */
472 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
473
474 /* Set the new SSID to current SSID */
475 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
476 priv->curbssparams.ssid_len = bss->ssid_len;
477
478 netif_carrier_on(priv->dev);
479 if (!priv->tx_pending_len)
480 netif_wake_queue(priv->dev);
481
482 memset(&wrqu, 0, sizeof(wrqu));
483 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
484 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
485 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
486
487 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
488 print_ssid(ssid, bss->ssid, bss->ssid_len),
489 priv->curbssparams.bssid,
490 priv->curbssparams.channel);
491
492done:
493 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
494 return ret;
495}
496
Holger Schurig697900a2008-04-02 16:27:10 +0200497/**
498 * @brief Join an adhoc network found in a previous scan
499 *
500 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400501 * @param assoc_req The association request describing the BSS to join
Holger Schurig697900a2008-04-02 16:27:10 +0200502 *
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400503 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200504 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400505static int lbs_adhoc_join(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200506 struct assoc_request *assoc_req)
507{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400508 struct cmd_ds_802_11_ad_hoc_join cmd;
Holger Schurig697900a2008-04-02 16:27:10 +0200509 struct bss_descriptor *bss = &assoc_req->bss;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400510 u8 preamble = RADIO_PREAMBLE_LONG;
John W. Linville9387b7c2008-09-30 20:59:05 -0400511 DECLARE_SSID_BUF(ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400512 u16 ratesize = 0;
513 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400514
515 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200516
517 lbs_deb_join("current SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400518 print_ssid(ssid, priv->curbssparams.ssid,
Holger Schurig697900a2008-04-02 16:27:10 +0200519 priv->curbssparams.ssid_len),
520 priv->curbssparams.ssid_len);
521 lbs_deb_join("requested ssid '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400522 print_ssid(ssid, bss->ssid, bss->ssid_len),
Holger Schurig697900a2008-04-02 16:27:10 +0200523 bss->ssid_len);
524
525 /* check if the requested SSID is already joined */
526 if (priv->curbssparams.ssid_len &&
527 !lbs_ssid_cmp(priv->curbssparams.ssid,
528 priv->curbssparams.ssid_len,
529 bss->ssid, bss->ssid_len) &&
530 (priv->mode == IW_MODE_ADHOC) &&
531 (priv->connect_status == LBS_CONNECTED)) {
532 union iwreq_data wrqu;
533
534 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
535 "current, not attempting to re-join");
536
537 /* Send the re-association event though, because the association
538 * request really was successful, even if just a null-op.
539 */
540 memset(&wrqu, 0, sizeof(wrqu));
541 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
542 ETH_ALEN);
543 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
544 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
545 goto out;
546 }
547
Dan Williamsd5db2df2008-08-21 17:51:07 -0400548 /* Use short preamble only when both the BSS and firmware support it */
549 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
550 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig697900a2008-04-02 16:27:10 +0200551 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400552 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200553 }
554
Dan Williamsd5db2df2008-08-21 17:51:07 -0400555 ret = lbs_set_radio(priv, preamble, 1);
556 if (ret)
557 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200558
559 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
560 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
561
562 priv->adhoccreate = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400563 priv->curbssparams.channel = bss->channel;
Holger Schurig697900a2008-04-02 16:27:10 +0200564
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400565 /* Build the join command */
566 memset(&cmd, 0, sizeof(cmd));
567 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
568
569 cmd.bss.type = CMD_BSS_TYPE_IBSS;
570 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
571
572 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
573 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
574
Dan Williams5fd164e2009-05-22 20:01:21 -0400575 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400576
Dan Williams5fd164e2009-05-22 20:01:21 -0400577 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
578 sizeof(struct ieee_ie_ibss_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400579
580 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
581 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
582 bss->capability, CAPINFO_MASK);
583
584 /* information on BSSID descriptor passed to FW */
Johannes Berge1749612008-10-27 15:59:26 -0700585 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
586 cmd.bss.bssid, cmd.bss.ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400587
588 /* Only v8 and below support setting these */
589 if (priv->fwrelease < 0x09000000) {
590 /* failtimeout */
591 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
592 /* probedelay */
593 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
594 }
595
596 /* Copy Data rates from the rates recorded in scan response */
597 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
Dan Williamsca4fe302009-08-21 09:35:20 -0500598 ratesize = min_t(u16, ARRAY_SIZE(cmd.bss.rates), ARRAY_SIZE (bss->rates));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400599 memcpy(cmd.bss.rates, bss->rates, ratesize);
600 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
601 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
602 ret = -1;
603 goto out;
604 }
605
606 /* Copy the ad-hoc creation rates into Current BSS state structure */
607 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
608 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
609
610 /* Set MSB on basic rates as the firmware requires, but _after_
611 * copying to current bss rates.
612 */
613 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
614
Dan Williams5fd164e2009-05-22 20:01:21 -0400615 cmd.bss.ibss.atimwindow = bss->atimwindow;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400616
617 if (assoc_req->secinfo.wep_enabled) {
618 u16 tmp = le16_to_cpu(cmd.bss.capability);
619 tmp |= WLAN_CAPABILITY_PRIVACY;
620 cmd.bss.capability = cpu_to_le16(tmp);
621 }
622
623 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
624 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
625
626 /* wake up first */
627 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
628 CMD_ACT_SET, 0, 0,
629 &local_ps_mode);
630 if (ret) {
631 ret = -1;
632 goto out;
633 }
634 }
635
636 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
637 ret = -1;
638 goto out;
639 }
640
641 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
Dan Williams822ac032009-05-22 20:07:14 -0400642 if (ret == 0) {
643 ret = lbs_adhoc_post(priv,
644 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
645 }
Holger Schurig697900a2008-04-02 16:27:10 +0200646
647out:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400648 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200649 return ret;
650}
651
652/**
653 * @brief Start an Adhoc Network
654 *
655 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400656 * @param assoc_req The association request describing the BSS to start
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400657 *
658 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200659 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400660static int lbs_adhoc_start(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200661 struct assoc_request *assoc_req)
662{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400663 struct cmd_ds_802_11_ad_hoc_start cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400664 u8 preamble = RADIO_PREAMBLE_LONG;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400665 size_t ratesize = 0;
666 u16 tmpcap = 0;
667 int ret = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400668 DECLARE_SSID_BUF(ssid);
Dan Williamsd5db2df2008-08-21 17:51:07 -0400669
670 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200671
Holger Schurig697900a2008-04-02 16:27:10 +0200672 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400673 lbs_deb_join("ADHOC_START: Will use short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400674 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200675 }
676
Dan Williamsd5db2df2008-08-21 17:51:07 -0400677 ret = lbs_set_radio(priv, preamble, 1);
678 if (ret)
679 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200680
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400681 /* Build the start command */
682 memset(&cmd, 0, sizeof(cmd));
683 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurig697900a2008-04-02 16:27:10 +0200684
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400685 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
686
687 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400688 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400689 assoc_req->ssid_len);
690
691 cmd.bsstype = CMD_BSS_TYPE_IBSS;
692
693 if (priv->beacon_period == 0)
694 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
695 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
696
697 WARN_ON(!assoc_req->channel);
698
699 /* set Physical parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400700 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
701 cmd.ds.header.len = 1;
Dan Williams5fd164e2009-05-22 20:01:21 -0400702 cmd.ds.channel = assoc_req->channel;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400703
704 /* set IBSS parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400705 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
706 cmd.ibss.header.len = 2;
Dan Williams5fd164e2009-05-22 20:01:21 -0400707 cmd.ibss.atimwindow = cpu_to_le16(0);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400708
709 /* set capability info */
710 tmpcap = WLAN_CAPABILITY_IBSS;
Dan Williams2fa7a982009-05-22 20:09:58 -0400711 if (assoc_req->secinfo.wep_enabled ||
712 assoc_req->secinfo.WPAenabled ||
713 assoc_req->secinfo.WPA2enabled) {
714 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400715 tmpcap |= WLAN_CAPABILITY_PRIVACY;
716 } else
Dan Williams2fa7a982009-05-22 20:09:58 -0400717 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400718
719 cmd.capability = cpu_to_le16(tmpcap);
720
721 /* Only v8 and below support setting probe delay */
722 if (priv->fwrelease < 0x09000000)
723 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
724
725 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
726 memcpy(cmd.rates, lbs_bg_rates, ratesize);
727
728 /* Copy the ad-hoc creating rates into Current BSS state structure */
729 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
730 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
731
732 /* Set MSB on basic rates as the firmware requires, but _after_
733 * copying to current bss rates.
734 */
735 lbs_set_basic_rate_flags(cmd.rates, ratesize);
736
737 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
738 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
739
740 if (lbs_create_dnld_countryinfo_11d(priv)) {
741 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
742 ret = -1;
743 goto out;
744 }
745
746 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
747 assoc_req->channel, assoc_req->band);
748
749 priv->adhoccreate = 1;
750 priv->mode = IW_MODE_ADHOC;
751
752 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
753 if (ret == 0)
Dan Williams822ac032009-05-22 20:07:14 -0400754 ret = lbs_adhoc_post(priv,
755 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200756
Dan Williamsd5db2df2008-08-21 17:51:07 -0400757out:
758 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200759 return ret;
760}
761
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400762/**
763 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
764 *
765 * @param priv A pointer to struct lbs_private structure
766 * @return 0 on success, or an error
767 */
768int lbs_adhoc_stop(struct lbs_private *priv)
Holger Schurig697900a2008-04-02 16:27:10 +0200769{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400770 struct cmd_ds_802_11_ad_hoc_stop cmd;
771 int ret;
772
773 lbs_deb_enter(LBS_DEB_JOIN);
774
775 memset(&cmd, 0, sizeof (cmd));
776 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
777
778 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
779
780 /* Clean up everything even if there was an error */
781 lbs_mac_event_disconnected(priv);
782
783 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
784 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +0200785}
Dan Williamse76850d2007-05-25 17:09:41 -0400786
Holger Schurig245bf202008-04-02 16:27:42 +0200787static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
788 struct bss_descriptor *match_bss)
789{
790 if (!secinfo->wep_enabled && !secinfo->WPAenabled
791 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100792 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
793 && match_bss->rsn_ie[0] != WLAN_EID_RSN
Holger Schurig245bf202008-04-02 16:27:42 +0200794 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
795 return 1;
796 else
797 return 0;
798}
799
800static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
801 struct bss_descriptor *match_bss)
802{
803 if (secinfo->wep_enabled && !secinfo->WPAenabled
804 && !secinfo->WPA2enabled
805 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
806 return 1;
807 else
808 return 0;
809}
810
811static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
812 struct bss_descriptor *match_bss)
813{
814 if (!secinfo->wep_enabled && secinfo->WPAenabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100815 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
Holger Schurig245bf202008-04-02 16:27:42 +0200816 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
817 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
818 )
819 return 1;
820 else
821 return 0;
822}
823
824static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
825 struct bss_descriptor *match_bss)
826{
827 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
Johannes Berg2c7060022008-10-30 22:09:54 +0100828 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200829 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
830 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
831 )
832 return 1;
833 else
834 return 0;
835}
836
837static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
838 struct bss_descriptor *match_bss)
839{
840 if (!secinfo->wep_enabled && !secinfo->WPAenabled
841 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100842 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
843 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200844 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
845 return 1;
846 else
847 return 0;
848}
849
850/**
851 * @brief Check if a scanned network compatible with the driver settings
852 *
853 * WEP WPA WPA2 ad-hoc encrypt Network
854 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
855 * 0 0 0 0 NONE 0 0 0 yes No security
856 * 1 0 0 0 NONE 1 0 0 yes Static WEP
857 * 0 1 0 0 x 1x 1 x yes WPA
858 * 0 0 1 0 x 1x x 1 yes WPA2
859 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
860 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
861 *
862 *
863 * @param priv A pointer to struct lbs_private
864 * @param index Index in scantable to check against current driver settings
865 * @param mode Network mode: Infrastructure or IBSS
866 *
867 * @return Index in scantable, or error code if negative
868 */
869static int is_network_compatible(struct lbs_private *priv,
870 struct bss_descriptor *bss, uint8_t mode)
871{
872 int matched = 0;
873
874 lbs_deb_enter(LBS_DEB_SCAN);
875
876 if (bss->mode != mode)
877 goto done;
878
879 matched = match_bss_no_security(&priv->secinfo, bss);
880 if (matched)
881 goto done;
882 matched = match_bss_static_wep(&priv->secinfo, bss);
883 if (matched)
884 goto done;
885 matched = match_bss_wpa(&priv->secinfo, bss);
886 if (matched) {
887 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
888 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
889 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
890 priv->secinfo.wep_enabled ? "e" : "d",
891 priv->secinfo.WPAenabled ? "e" : "d",
892 priv->secinfo.WPA2enabled ? "e" : "d",
893 (bss->capability & WLAN_CAPABILITY_PRIVACY));
894 goto done;
895 }
896 matched = match_bss_wpa2(&priv->secinfo, bss);
897 if (matched) {
898 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
899 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
900 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
901 priv->secinfo.wep_enabled ? "e" : "d",
902 priv->secinfo.WPAenabled ? "e" : "d",
903 priv->secinfo.WPA2enabled ? "e" : "d",
904 (bss->capability & WLAN_CAPABILITY_PRIVACY));
905 goto done;
906 }
907 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
908 if (matched) {
909 lbs_deb_scan("is_network_compatible() dynamic WEP: "
910 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
911 bss->wpa_ie[0], bss->rsn_ie[0],
912 (bss->capability & WLAN_CAPABILITY_PRIVACY));
913 goto done;
914 }
915
916 /* bss security settings don't match those configured on card */
917 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
918 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
919 bss->wpa_ie[0], bss->rsn_ie[0],
920 priv->secinfo.wep_enabled ? "e" : "d",
921 priv->secinfo.WPAenabled ? "e" : "d",
922 priv->secinfo.WPA2enabled ? "e" : "d",
923 (bss->capability & WLAN_CAPABILITY_PRIVACY));
924
925done:
926 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
927 return matched;
928}
929
930/**
931 * @brief This function finds a specific compatible BSSID in the scan list
932 *
933 * Used in association code
934 *
935 * @param priv A pointer to struct lbs_private
936 * @param bssid BSSID to find in the scan list
937 * @param mode Network mode: Infrastructure or IBSS
938 *
939 * @return index in BSSID list, or error return code (< 0)
940 */
941static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
942 uint8_t *bssid, uint8_t mode)
943{
944 struct bss_descriptor *iter_bss;
945 struct bss_descriptor *found_bss = NULL;
946
947 lbs_deb_enter(LBS_DEB_SCAN);
948
949 if (!bssid)
950 goto out;
951
952 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
953
954 /* Look through the scan table for a compatible match. The loop will
955 * continue past a matched bssid that is not compatible in case there
956 * is an AP with multiple SSIDs assigned to the same BSSID
957 */
958 mutex_lock(&priv->lock);
959 list_for_each_entry(iter_bss, &priv->network_list, list) {
960 if (compare_ether_addr(iter_bss->bssid, bssid))
961 continue; /* bssid doesn't match */
962 switch (mode) {
963 case IW_MODE_INFRA:
964 case IW_MODE_ADHOC:
965 if (!is_network_compatible(priv, iter_bss, mode))
966 break;
967 found_bss = iter_bss;
968 break;
969 default:
970 found_bss = iter_bss;
971 break;
972 }
973 }
974 mutex_unlock(&priv->lock);
975
976out:
977 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
978 return found_bss;
979}
980
981/**
982 * @brief This function finds ssid in ssid list.
983 *
984 * Used in association code
985 *
986 * @param priv A pointer to struct lbs_private
987 * @param ssid SSID to find in the list
988 * @param bssid BSSID to qualify the SSID selection (if provided)
989 * @param mode Network mode: Infrastructure or IBSS
990 *
991 * @return index in BSSID list
992 */
993static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
994 uint8_t *ssid, uint8_t ssid_len,
995 uint8_t *bssid, uint8_t mode,
996 int channel)
997{
998 u32 bestrssi = 0;
999 struct bss_descriptor *iter_bss = NULL;
1000 struct bss_descriptor *found_bss = NULL;
1001 struct bss_descriptor *tmp_oldest = NULL;
1002
1003 lbs_deb_enter(LBS_DEB_SCAN);
1004
1005 mutex_lock(&priv->lock);
1006
1007 list_for_each_entry(iter_bss, &priv->network_list, list) {
1008 if (!tmp_oldest ||
1009 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1010 tmp_oldest = iter_bss;
1011
1012 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1013 ssid, ssid_len) != 0)
1014 continue; /* ssid doesn't match */
1015 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1016 continue; /* bssid doesn't match */
1017 if ((channel > 0) && (iter_bss->channel != channel))
1018 continue; /* channel doesn't match */
1019
1020 switch (mode) {
1021 case IW_MODE_INFRA:
1022 case IW_MODE_ADHOC:
1023 if (!is_network_compatible(priv, iter_bss, mode))
1024 break;
1025
1026 if (bssid) {
1027 /* Found requested BSSID */
1028 found_bss = iter_bss;
1029 goto out;
1030 }
1031
1032 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1033 bestrssi = SCAN_RSSI(iter_bss->rssi);
1034 found_bss = iter_bss;
1035 }
1036 break;
1037 case IW_MODE_AUTO:
1038 default:
1039 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1040 bestrssi = SCAN_RSSI(iter_bss->rssi);
1041 found_bss = iter_bss;
1042 }
1043 break;
1044 }
1045 }
1046
1047out:
1048 mutex_unlock(&priv->lock);
1049 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1050 return found_bss;
1051}
1052
Holger Schurig69f90322007-11-23 15:43:44 +01001053static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001054 struct assoc_request * assoc_req)
1055{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001056 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001057 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001058 int channel = -1;
John W. Linville9387b7c2008-09-30 20:59:05 -04001059 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001060
Holger Schurig9012b282007-05-25 11:27:16 -04001061 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001062
Dan Williamsef9a2642007-05-25 16:46:33 -04001063 /* FIXME: take channel into account when picking SSIDs if a channel
1064 * is set.
1065 */
1066
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001067 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1068 channel = assoc_req->channel;
1069
Holger Schurig0765af42007-10-15 12:55:56 +02001070 lbs_deb_assoc("SSID '%s' requested\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001071 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -04001072 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -05001073 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001074 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001075
David Woodhouseaa21c002007-12-08 20:04:36 +00001076 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001077 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001078 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -04001079 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams822ac032009-05-22 20:07:14 -04001080 ret = lbs_try_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001081 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -04001082 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001083 }
Dan Williams0dc5a292007-05-10 22:58:02 -04001084 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001085 /* Scan for the network, do not save previous results. Stale
1086 * scan data will cause us to join a non-existant adhoc network
1087 */
Holger Schurig10078322007-11-15 18:05:47 -05001088 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001089 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001090
1091 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001092 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001093 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001094 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -04001095 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001096 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001097 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001098 } else {
1099 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -04001100 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001101 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001102 IW_ESSID_MAX_SIZE);
1103 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001104 lbs_adhoc_start(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001105 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001106 }
1107
Holger Schurig9012b282007-05-25 11:27:16 -04001108 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001109 return ret;
1110}
1111
1112
Holger Schurig69f90322007-11-23 15:43:44 +01001113static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001114 struct assoc_request * assoc_req)
1115{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001116 int ret = 0;
1117 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001118
Johannes Berge1749612008-10-27 15:59:26 -07001119 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001120
1121 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +00001122 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001123 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001124 if (bss == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -07001125 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1126 "cannot associate.\n", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001127 goto out;
1128 }
1129
Dan Williamse76850d2007-05-25 17:09:41 -04001130 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -04001131 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williams822ac032009-05-22 20:07:14 -04001132 ret = lbs_try_associate(priv, assoc_req);
1133 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1134 ret);
Dan Williams0dc5a292007-05-10 22:58:02 -04001135 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001136 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001137 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001138
1139out:
Holger Schurig9012b282007-05-25 11:27:16 -04001140 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001141 return ret;
1142}
1143
1144
Holger Schurig69f90322007-11-23 15:43:44 +01001145static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001146 struct assoc_request * assoc_req)
1147{
1148 int ret = 0, done = 0;
1149
Holger Schurig0765af42007-10-15 12:55:56 +02001150 lbs_deb_enter(LBS_DEB_ASSOC);
1151
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001152 /* If we're given and 'any' BSSID, try associating based on SSID */
1153
1154 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001155 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1156 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001157 ret = assoc_helper_bssid(priv, assoc_req);
1158 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001159 }
1160 }
1161
1162 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1163 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001164 }
1165
Holger Schurig0765af42007-10-15 12:55:56 +02001166 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001167 return ret;
1168}
1169
1170
Holger Schurig69f90322007-11-23 15:43:44 +01001171static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001172 struct assoc_request * assoc_req)
1173{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001174 int ret = 0;
1175
Holger Schurig9012b282007-05-25 11:27:16 -04001176 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001177
David Woodhouseaa21c002007-12-08 20:04:36 +00001178 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -04001179 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001180
Dan Williams0dc5a292007-05-10 22:58:02 -04001181 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001182 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -05001183 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +00001184 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001185 }
1186
David Woodhouseaa21c002007-12-08 20:04:36 +00001187 priv->mode = assoc_req->mode;
Dan Williams39fcf7a2008-09-10 12:49:00 -04001188 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001189
Holger Schurig9012b282007-05-25 11:27:16 -04001190done:
1191 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001192 return ret;
1193}
1194
Holger Schurig69f90322007-11-23 15:43:44 +01001195static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -04001196 struct assoc_request * assoc_req)
1197{
Dan Williamsef9a2642007-05-25 16:46:33 -04001198 int ret = 0;
1199
1200 lbs_deb_enter(LBS_DEB_ASSOC);
1201
David Woodhouse9f462572007-12-12 22:50:21 -05001202 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001203 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001204 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001205 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -04001206 }
1207
David Woodhouseaa21c002007-12-08 20:04:36 +00001208 if (assoc_req->channel == priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001209 goto done;
1210
David Woodhouse8642f1f2007-12-11 20:03:01 -05001211 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -05001212 /* Change mesh channel first; 21.p21 firmware won't let
1213 you change channel otherwise (even though it'll return
1214 an error to this */
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001215 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1216 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001217 }
1218
Dan Williamsef9a2642007-05-25 16:46:33 -04001219 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
David Woodhouse86062132007-12-13 00:32:36 -05001220 priv->curbssparams.channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -04001221
Dan Williams2dd4b262007-12-11 16:54:15 -05001222 ret = lbs_set_channel(priv, assoc_req->channel);
1223 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -05001224 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -04001225
Dan Williams2dd4b262007-12-11 16:54:15 -05001226 /* FIXME: shouldn't need to grab the channel _again_ after setting
1227 * it since the firmware is supposed to return the new channel, but
1228 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -05001229 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001230 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001231 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001232 goto done;
1233 }
Dan Williamsef9a2642007-05-25 16:46:33 -04001234
David Woodhouseaa21c002007-12-08 20:04:36 +00001235 if (assoc_req->channel != priv->curbssparams.channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -05001236 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -04001237 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001238 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -04001239 }
1240
1241 if ( assoc_req->secinfo.wep_enabled
1242 && (assoc_req->wep_keys[0].len
1243 || assoc_req->wep_keys[1].len
1244 || assoc_req->wep_keys[2].len
1245 || assoc_req->wep_keys[3].len)) {
1246 /* Make sure WEP keys are re-sent to firmware */
1247 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1248 }
1249
1250 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -05001251 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -04001252
David Woodhouse8642f1f2007-12-11 20:03:01 -05001253 restore_mesh:
1254 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001255 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1256 priv->curbssparams.channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001257
1258 done:
Dan Williamsef9a2642007-05-25 16:46:33 -04001259 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1260 return ret;
1261}
1262
1263
Holger Schurig69f90322007-11-23 15:43:44 +01001264static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -05001265 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001266{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001267 int i;
1268 int ret = 0;
1269
Holger Schurig9012b282007-05-25 11:27:16 -04001270 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001271
1272 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -05001273 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1274 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1275 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1276 else
1277 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001278
1279 if (ret)
1280 goto out;
1281
1282 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -04001283 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +01001284 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001285 else
Holger Schurigd9e97782008-03-12 16:06:43 +01001286 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -05001287
Holger Schurigc97329e2008-03-18 11:20:21 +01001288 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001289
David Woodhouseaa21c002007-12-08 20:04:36 +00001290 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001291
David Woodhouseaa21c002007-12-08 20:04:36 +00001292 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001293 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001294 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -05001295 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001296 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001297 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001298
David Woodhouseaa21c002007-12-08 20:04:36 +00001299 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001300
1301out:
Holger Schurig9012b282007-05-25 11:27:16 -04001302 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001303 return ret;
1304}
1305
Holger Schurig69f90322007-11-23 15:43:44 +01001306static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001307 struct assoc_request * assoc_req)
1308{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001309 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -05001310 uint16_t do_wpa;
1311 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001312
Holger Schurig9012b282007-05-25 11:27:16 -04001313 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001314
David Woodhouseaa21c002007-12-08 20:04:36 +00001315 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001316 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001317
Holger Schurigc97329e2008-03-18 11:20:21 +01001318 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001319
Dan Williams18c96c342007-06-18 12:01:12 -04001320 /* If RSN is already enabled, don't try to enable it again, since
1321 * ENABLE_RSN resets internal state machines and will clobber the
1322 * 4-way WPA handshake.
1323 */
1324
1325 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001326 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -04001327 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001328 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -04001329 goto out;
1330 }
1331
1332 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001333 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -04001334 if (do_wpa == rsn)
1335 goto out;
1336
1337 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001338 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -04001339
1340out:
Holger Schurig9012b282007-05-25 11:27:16 -04001341 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001342 return ret;
1343}
1344
1345
Holger Schurig69f90322007-11-23 15:43:44 +01001346static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001347 struct assoc_request * assoc_req)
1348{
1349 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -04001350 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001351
Holger Schurig9012b282007-05-25 11:27:16 -04001352 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001353
Dan Williams2bcde512007-10-03 10:37:45 -04001354 /* Work around older firmware bug where WPA unicast and multicast
1355 * keys must be set independently. Seen in SDIO parts with firmware
1356 * version 5.0.11p0.
1357 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001358
Dan Williams2bcde512007-10-03 10:37:45 -04001359 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1360 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +01001361 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001362 assoc_req->flags = flags;
1363 }
1364
1365 if (ret)
1366 goto out;
1367
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001368 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1369 sizeof(struct enc_key));
1370
Dan Williams2bcde512007-10-03 10:37:45 -04001371 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1372 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1373
David Woodhouse9e1228d2008-03-03 12:15:39 +01001374 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001375 assoc_req->flags = flags;
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001376
1377 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1378 sizeof(struct enc_key));
Dan Williams2bcde512007-10-03 10:37:45 -04001379 }
1380
1381out:
Holger Schurig9012b282007-05-25 11:27:16 -04001382 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001383 return ret;
1384}
1385
1386
Holger Schurig69f90322007-11-23 15:43:44 +01001387static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001388 struct assoc_request * assoc_req)
1389{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001390 int ret = 0;
1391
Holger Schurig9012b282007-05-25 11:27:16 -04001392 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001393
1394 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001395 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1396 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001397 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +00001398 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1399 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001400 }
1401
Holger Schurig9012b282007-05-25 11:27:16 -04001402 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001403 return ret;
1404}
1405
1406
David Woodhouseaa21c002007-12-08 20:04:36 +00001407static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001408 struct assoc_request * assoc_req)
1409{
Holger Schurig0765af42007-10-15 12:55:56 +02001410 int ret = 0;
1411
David Woodhouseaa21c002007-12-08 20:04:36 +00001412 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001413 return 0;
1414
Holger Schurig52507c22008-01-28 17:25:53 +01001415 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001416 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001417 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1418 ret = 1;
1419 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001420 }
1421
1422 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001423 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +02001424 lbs_deb_assoc("Deauthenticating due to new security\n");
1425 ret = 1;
1426 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001427 }
1428 }
1429
1430 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001431 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1432 ret = 1;
1433 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001434 }
1435
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001436 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001437 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1438 ret = 1;
1439 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001440 }
1441
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001442 /* FIXME: deal with 'auto' mode somehow */
1443 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001444 if (assoc_req->mode != IW_MODE_INFRA) {
1445 lbs_deb_assoc("Deauthenticating due to leaving "
1446 "infra mode\n");
1447 ret = 1;
1448 goto out;
1449 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001450 }
1451
Holger Schurig0765af42007-10-15 12:55:56 +02001452out:
1453 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +01001454 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001455}
1456
1457
David Woodhouseaa21c002007-12-08 20:04:36 +00001458static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001459 struct assoc_request * assoc_req)
1460{
Holger Schurig0765af42007-10-15 12:55:56 +02001461 lbs_deb_enter(LBS_DEB_ASSOC);
1462
David Woodhouseaa21c002007-12-08 20:04:36 +00001463 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001464 return 0;
1465
David Woodhouseaa21c002007-12-08 20:04:36 +00001466 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1467 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001468 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001469 return 1;
1470
1471 /* FIXME: deal with 'auto' mode somehow */
1472 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001473 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001474 return 1;
1475 }
1476
Dan Williamsef9a2642007-05-25 16:46:33 -04001477 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001478 if (assoc_req->channel != priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001479 return 1;
1480 }
1481
Holger Schurig0765af42007-10-15 12:55:56 +02001482 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001483 return 0;
1484}
1485
1486
Holger Schurig245bf202008-04-02 16:27:42 +02001487/**
1488 * @brief This function finds the best SSID in the Scan List
1489 *
1490 * Search the scan table for the best SSID that also matches the current
1491 * adapter network preference (infrastructure or adhoc)
1492 *
1493 * @param priv A pointer to struct lbs_private
1494 *
1495 * @return index in BSSID list
1496 */
1497static struct bss_descriptor *lbs_find_best_ssid_in_list(
1498 struct lbs_private *priv, uint8_t mode)
1499{
1500 uint8_t bestrssi = 0;
1501 struct bss_descriptor *iter_bss;
1502 struct bss_descriptor *best_bss = NULL;
1503
1504 lbs_deb_enter(LBS_DEB_SCAN);
1505
1506 mutex_lock(&priv->lock);
1507
1508 list_for_each_entry(iter_bss, &priv->network_list, list) {
1509 switch (mode) {
1510 case IW_MODE_INFRA:
1511 case IW_MODE_ADHOC:
1512 if (!is_network_compatible(priv, iter_bss, mode))
1513 break;
1514 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1515 break;
1516 bestrssi = SCAN_RSSI(iter_bss->rssi);
1517 best_bss = iter_bss;
1518 break;
1519 case IW_MODE_AUTO:
1520 default:
1521 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1522 break;
1523 bestrssi = SCAN_RSSI(iter_bss->rssi);
1524 best_bss = iter_bss;
1525 break;
1526 }
1527 }
1528
1529 mutex_unlock(&priv->lock);
1530 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1531 return best_bss;
1532}
1533
1534/**
1535 * @brief Find the best AP
1536 *
1537 * Used from association worker.
1538 *
1539 * @param priv A pointer to struct lbs_private structure
1540 * @param pSSID A pointer to AP's ssid
1541 *
1542 * @return 0--success, otherwise--fail
1543 */
1544static int lbs_find_best_network_ssid(struct lbs_private *priv,
1545 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1546 uint8_t *out_mode)
1547{
1548 int ret = -1;
1549 struct bss_descriptor *found;
1550
1551 lbs_deb_enter(LBS_DEB_SCAN);
1552
1553 priv->scan_ssid_len = 0;
1554 lbs_scan_networks(priv, 1);
1555 if (priv->surpriseremoved)
1556 goto out;
1557
1558 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1559 if (found && (found->ssid_len > 0)) {
1560 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1561 *out_ssid_len = found->ssid_len;
1562 *out_mode = found->mode;
1563 ret = 0;
1564 }
1565
1566out:
1567 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1568 return ret;
1569}
1570
1571
Holger Schurig10078322007-11-15 18:05:47 -05001572void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001573{
Holger Schurig69f90322007-11-23 15:43:44 +01001574 struct lbs_private *priv = container_of(work, struct lbs_private,
1575 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001576 struct assoc_request * assoc_req = NULL;
1577 int ret = 0;
1578 int find_any_ssid = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001579 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001580
Holger Schurig9012b282007-05-25 11:27:16 -04001581 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001582
David Woodhouseaa21c002007-12-08 20:04:36 +00001583 mutex_lock(&priv->lock);
1584 assoc_req = priv->pending_assoc_req;
1585 priv->pending_assoc_req = NULL;
1586 priv->in_progress_assoc_req = assoc_req;
1587 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001588
Holger Schurig9012b282007-05-25 11:27:16 -04001589 if (!assoc_req)
1590 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001591
Holger Schurig0765af42007-10-15 12:55:56 +02001592 lbs_deb_assoc(
1593 "Association Request:\n"
1594 " flags: 0x%08lx\n"
1595 " SSID: '%s'\n"
1596 " chann: %d\n"
1597 " band: %d\n"
1598 " mode: %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001599 " BSSID: %pM\n"
Holger Schurig0765af42007-10-15 12:55:56 +02001600 " secinfo: %s%s%s\n"
1601 " auth_mode: %d\n",
1602 assoc_req->flags,
John W. Linville9387b7c2008-09-30 20:59:05 -04001603 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Holger Schurig0765af42007-10-15 12:55:56 +02001604 assoc_req->channel, assoc_req->band, assoc_req->mode,
Johannes Berge1749612008-10-27 15:59:26 -07001605 assoc_req->bssid,
Holger Schurig0765af42007-10-15 12:55:56 +02001606 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1607 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1608 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1609 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001610
1611 /* If 'any' SSID was specified, find an SSID to associate with */
1612 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001613 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001614 find_any_ssid = 1;
1615
1616 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1617 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001618 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1619 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001620 find_any_ssid = 0;
1621 }
1622
1623 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02001624 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001625
Holger Schurig10078322007-11-15 18:05:47 -05001626 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001627 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001628 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001629 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001630 ret = -ENETUNREACH;
1631 goto out;
1632 }
1633
1634 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04001635 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001636 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1637 assoc_req->mode = new_mode;
1638 }
1639 }
1640
1641 /*
1642 * Check if the attributes being changing require deauthentication
1643 * from the currently associated infrastructure access point.
1644 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001645 if (priv->mode == IW_MODE_INFRA) {
1646 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04001647 ret = lbs_cmd_80211_deauthenticate(priv,
1648 priv->curbssparams.bssid,
1649 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001650 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001651 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001652 "configuration request failed: %d\n",
1653 ret);
1654 }
1655 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001656 } else if (priv->mode == IW_MODE_ADHOC) {
1657 if (should_stop_adhoc(priv, assoc_req)) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001658 ret = lbs_adhoc_stop(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001659 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001660 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001661 "new configuration request failed: %d\n",
1662 ret);
1663 }
1664
1665 }
1666 }
1667
1668 /* Send the various configuration bits to the firmware */
1669 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1670 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001671 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001672 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001673 }
1674
Dan Williamsef9a2642007-05-25 16:46:33 -04001675 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1676 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001677 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04001678 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04001679 }
1680
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001681 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1682 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1683 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001684 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001685 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001686 }
1687
1688 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1689 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001690 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001691 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001692 }
1693
1694 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1695 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001696 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001697 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001698 }
1699
1700 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1701 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1702 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001703 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001704 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001705 }
1706
1707 /* SSID/BSSID should be the _last_ config option set, because they
1708 * trigger the association attempt.
1709 */
1710 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1711 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1712 int success = 1;
1713
1714 ret = assoc_helper_associate(priv, assoc_req);
1715 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01001716 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001717 ret);
1718 success = 0;
1719 }
1720
David Woodhouseaa21c002007-12-08 20:04:36 +00001721 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01001722 lbs_deb_assoc("ASSOC: association unsuccessful, "
1723 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001724 success = 0;
1725 }
1726
1727 if (success) {
Johannes Berge1749612008-10-27 15:59:26 -07001728 lbs_deb_assoc("associated to %pM\n",
1729 priv->curbssparams.bssid);
Holger Schurig10078322007-11-15 18:05:47 -05001730 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04001731 CMD_802_11_RSSI,
1732 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001733 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001734 ret = -1;
1735 }
1736 }
1737
1738out:
1739 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001740 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001741 ret);
1742 }
Dan Williamse76850d2007-05-25 17:09:41 -04001743
David Woodhouseaa21c002007-12-08 20:04:36 +00001744 mutex_lock(&priv->lock);
1745 priv->in_progress_assoc_req = NULL;
1746 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001747 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04001748
1749done:
1750 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001751}
1752
1753
1754/*
1755 * Caller MUST hold any necessary locks
1756 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001757struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001758{
1759 struct assoc_request * assoc_req;
1760
Holger Schurig0765af42007-10-15 12:55:56 +02001761 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00001762 if (!priv->pending_assoc_req) {
1763 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04001764 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00001765 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001766 lbs_pr_info("Not enough memory to allocate association"
1767 " request!\n");
1768 return NULL;
1769 }
1770 }
1771
1772 /* Copy current configuration attributes to the association request,
1773 * but don't overwrite any that are already set.
1774 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001775 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001776 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001777 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001778 IW_ESSID_MAX_SIZE);
David Woodhouseaa21c002007-12-08 20:04:36 +00001779 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001780 }
1781
1782 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001783 assoc_req->channel = priv->curbssparams.channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001784
Dan Williamse76850d2007-05-25 17:09:41 -04001785 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001786 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04001787
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001788 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001789 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001790
1791 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001792 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001793 ETH_ALEN);
1794 }
1795
1796 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1797 int i;
1798 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001799 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04001800 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001801 }
1802 }
1803
1804 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001805 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001806
1807 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001808 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001809 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001810 }
1811
1812 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001813 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001814 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001815 }
1816
1817 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001818 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001819 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001820 }
1821
1822 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001823 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001824 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001825 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001826 }
1827
Holger Schurig0765af42007-10-15 12:55:56 +02001828 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001829 return assoc_req;
1830}
Holger Schurig697900a2008-04-02 16:27:10 +02001831
1832
1833/**
Dan Williams191bb402008-08-21 17:46:18 -04001834 * @brief Deauthenticate from a specific BSS
1835 *
1836 * @param priv A pointer to struct lbs_private structure
1837 * @param bssid The specific BSS to deauthenticate from
1838 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1839 *
1840 * @return 0 on success, error on failure
1841 */
1842int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1843 u16 reason)
Holger Schurig697900a2008-04-02 16:27:10 +02001844{
Dan Williams191bb402008-08-21 17:46:18 -04001845 struct cmd_ds_802_11_deauthenticate cmd;
1846 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001847
1848 lbs_deb_enter(LBS_DEB_JOIN);
1849
Dan Williams191bb402008-08-21 17:46:18 -04001850 memset(&cmd, 0, sizeof(cmd));
1851 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1852 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1853 cmd.reasoncode = cpu_to_le16(reason);
Holger Schurig697900a2008-04-02 16:27:10 +02001854
Dan Williams191bb402008-08-21 17:46:18 -04001855 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001856
Dan Williams191bb402008-08-21 17:46:18 -04001857 /* Clean up everything even if there was an error; can't assume that
1858 * we're still authenticated to the AP after trying to deauth.
1859 */
1860 lbs_mac_event_disconnected(priv);
Holger Schurig697900a2008-04-02 16:27:10 +02001861
1862 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04001863 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001864}
1865