blob: fbf26499c9a91d039859035833157e9463dfa7ee [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 Williams3cf209312007-05-25 17:28:30 -04004#include <linux/etherdevice.h>
Johannes Berg2c7060022008-10-30 22:09:54 +01005#include <linux/ieee80211.h>
6#include <linux/if_arp.h>
John W. Linville7e272fc2008-09-24 18:13:14 -04007#include <net/lib80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02008
9#include "assoc.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020010#include "decl.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020011#include "host.h"
Holger Schurig245bf202008-04-02 16:27:42 +020012#include "scan.h"
Dan Williams2dd4b262007-12-11 16:54:15 -050013#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014
Ihar Hrachyshka5a6e0432008-01-25 14:15:00 +010015static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
16 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
17static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
18 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020019
Dan Williamsbe0d76e2009-05-22 20:05:25 -040020/* The firmware needs the following bits masked out of the beacon-derived
21 * capability field when associating/joining to a BSS:
22 * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
Holger Schurig697900a2008-04-02 16:27:10 +020023 */
24#define CAPINFO_MASK (~(0xda00))
25
26
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040027/**
28 * @brief This function finds common rates between rates and card rates.
29 *
30 * It will fill common rates in rates as output if found.
31 *
32 * NOTE: Setting the MSB of the basic rates need to be taken
33 * care, either before or after calling this function
34 *
35 * @param priv A pointer to struct lbs_private structure
36 * @param rates the buffer which keeps input and output
37 * @param rates_size the size of rate1 buffer; new size of buffer on return
38 *
39 * @return 0 on success, or -1 on error
40 */
41static int get_common_rates(struct lbs_private *priv,
42 u8 *rates,
43 u16 *rates_size)
44{
45 u8 *card_rates = lbs_bg_rates;
46 size_t num_card_rates = sizeof(lbs_bg_rates);
47 int ret = 0, i, j;
48 u8 tmp[30];
49 size_t tmp_size = 0;
50
51 /* For each rate in card_rates that exists in rate1, copy to tmp */
52 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
53 for (j = 0; rates[j] && (j < *rates_size); j++) {
54 if (rates[j] == card_rates[i])
55 tmp[tmp_size++] = card_rates[i];
56 }
57 }
58
59 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
60 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
61 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
62 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
63
64 if (!priv->enablehwauto) {
65 for (i = 0; i < tmp_size; i++) {
66 if (tmp[i] == priv->cur_rate)
67 goto done;
68 }
69 lbs_pr_alert("Previously set fixed data rate %#x isn't "
70 "compatible with the network.\n", priv->cur_rate);
71 ret = -1;
72 goto done;
73 }
74 ret = 0;
75
76done:
77 memset(rates, 0, *rates_size);
78 *rates_size = min_t(int, tmp_size, *rates_size);
79 memcpy(rates, tmp, *rates_size);
80 return ret;
81}
82
83
84/**
85 * @brief Sets the MSB on basic rates as the firmware requires
86 *
87 * Scan through an array and set the MSB for basic data rates.
88 *
89 * @param rates buffer of data rates
90 * @param len size of buffer
91 */
92static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
93{
94 int i;
95
96 for (i = 0; i < len; i++) {
97 if (rates[i] == 0x02 || rates[i] == 0x04 ||
98 rates[i] == 0x0b || rates[i] == 0x16)
99 rates[i] |= 0x80;
100 }
101}
102
Holger Schurig697900a2008-04-02 16:27:10 +0200103
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400104static u8 iw_auth_to_ieee_auth(u8 auth)
105{
106 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
107 return 0x00;
108 else if (auth == IW_AUTH_ALG_SHARED_KEY)
109 return 0x01;
110 else if (auth == IW_AUTH_ALG_LEAP)
111 return 0x80;
112
113 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
114 return 0;
115}
116
117/**
118 * @brief This function prepares the authenticate command. AUTHENTICATE only
119 * sets the authentication suite for future associations, as the firmware
120 * handles authentication internally during the ASSOCIATE command.
121 *
122 * @param priv A pointer to struct lbs_private structure
123 * @param bssid The peer BSSID with which to authenticate
124 * @param auth The authentication mode to use (from wireless.h)
125 *
126 * @return 0 or -1
127 */
128static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
129{
130 struct cmd_ds_802_11_authenticate cmd;
131 int ret = -1;
132 DECLARE_MAC_BUF(mac);
133
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
141 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
142 print_mac(mac, bssid), cmd.authtype);
143
144 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
145
146 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
147 return ret;
148}
149
Dan Williams822ac032009-05-22 20:07:14 -0400150
151static int lbs_assoc_post(struct lbs_private *priv,
152 struct cmd_ds_802_11_associate_response *resp)
153{
154 int ret = 0;
155 union iwreq_data wrqu;
156 struct bss_descriptor *bss;
157 u16 status_code;
158
159 lbs_deb_enter(LBS_DEB_ASSOC);
160
161 if (!priv->in_progress_assoc_req) {
162 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
163 ret = -1;
164 goto done;
165 }
166 bss = &priv->in_progress_assoc_req->bss;
167
168 /*
169 * Older FW versions map the IEEE 802.11 Status Code in the association
170 * response to the following values returned in resp->statuscode:
171 *
172 * IEEE Status Code Marvell Status Code
173 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
174 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
175 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
176 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
177 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
178 * others -> 0x0003 ASSOC_RESULT_REFUSED
179 *
180 * Other response codes:
181 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
182 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
183 * association response from the AP)
184 */
185
186 status_code = le16_to_cpu(resp->statuscode);
187 if (priv->fwrelease < 0x09000000) {
188 switch (status_code) {
189 case 0x00:
190 break;
191 case 0x01:
192 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
193 break;
194 case 0x02:
195 lbs_deb_assoc("ASSOC_RESP: internal timer "
196 "expired while waiting for the AP\n");
197 break;
198 case 0x03:
199 lbs_deb_assoc("ASSOC_RESP: association "
200 "refused by AP\n");
201 break;
202 case 0x04:
203 lbs_deb_assoc("ASSOC_RESP: authentication "
204 "refused by AP\n");
205 break;
206 default:
207 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
208 " unknown\n", status_code);
209 break;
210 }
211 } else {
212 /* v9+ returns the AP's association response */
213 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
214 }
215
216 if (status_code) {
217 lbs_mac_event_disconnected(priv);
218 ret = -1;
219 goto done;
220 }
221
222 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
223 (void *) (resp + sizeof (resp->hdr)),
224 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
225
226 /* Send a Media Connected event, according to the Spec */
227 priv->connect_status = LBS_CONNECTED;
228
229 /* Update current SSID and BSSID */
230 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
231 priv->curbssparams.ssid_len = bss->ssid_len;
232 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
233
234 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
235 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
236
237 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
238 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
239 priv->nextSNRNF = 0;
240 priv->numSNRNF = 0;
241
242 netif_carrier_on(priv->dev);
243 if (!priv->tx_pending_len)
244 netif_wake_queue(priv->dev);
245
246 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
247 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
248 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
249
250done:
251 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
252 return ret;
253}
254
255/**
256 * @brief This function prepares an association-class command.
257 *
258 * @param priv A pointer to struct lbs_private structure
259 * @param assoc_req The association request describing the BSS to associate
260 * or reassociate with
261 * @param command The actual command, either CMD_802_11_ASSOCIATE or
262 * CMD_802_11_REASSOCIATE
263 *
264 * @return 0 or -1
265 */
266static int lbs_associate(struct lbs_private *priv,
267 struct assoc_request *assoc_req,
268 u16 command)
269{
270 struct cmd_ds_802_11_associate cmd;
271 int ret = 0;
272 struct bss_descriptor *bss = &assoc_req->bss;
273 u8 *pos = &(cmd.iebuf[0]);
274 u16 tmpcap, tmplen, tmpauth;
275 struct mrvl_ie_ssid_param_set *ssid;
276 struct mrvl_ie_ds_param_set *ds;
277 struct mrvl_ie_cf_param_set *cf;
278 struct mrvl_ie_rates_param_set *rates;
279 struct mrvl_ie_rsn_param_set *rsn;
280 struct mrvl_ie_auth_type *auth;
281
282 lbs_deb_enter(LBS_DEB_ASSOC);
283
284 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
285 (command != CMD_802_11_REASSOCIATE));
286
287 memset(&cmd, 0, sizeof(cmd));
288 cmd.hdr.command = cpu_to_le16(command);
289
290 /* Fill in static fields */
291 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
292 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
293
294 /* Capability info */
295 tmpcap = (bss->capability & CAPINFO_MASK);
296 if (bss->mode == IW_MODE_INFRA)
297 tmpcap |= WLAN_CAPABILITY_ESS;
298 cmd.capability = cpu_to_le16(tmpcap);
299 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
300
301 /* SSID */
302 ssid = (struct mrvl_ie_ssid_param_set *) pos;
303 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
304 tmplen = bss->ssid_len;
305 ssid->header.len = cpu_to_le16(tmplen);
306 memcpy(ssid->ssid, bss->ssid, tmplen);
307 pos += sizeof(ssid->header) + tmplen;
308
309 ds = (struct mrvl_ie_ds_param_set *) pos;
310 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
311 ds->header.len = cpu_to_le16(1);
312 ds->channel = bss->phy.ds.channel;
313 pos += sizeof(ds->header) + 1;
314
315 cf = (struct mrvl_ie_cf_param_set *) pos;
316 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
317 tmplen = sizeof(*cf) - sizeof (cf->header);
318 cf->header.len = cpu_to_le16(tmplen);
319 /* IE payload should be zeroed, firmware fills it in for us */
320 pos += sizeof(*cf);
321
322 rates = (struct mrvl_ie_rates_param_set *) pos;
323 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
324 memcpy(&rates->rates, &bss->rates, MAX_RATES);
325 tmplen = MAX_RATES;
326 if (get_common_rates(priv, rates->rates, &tmplen)) {
327 ret = -1;
328 goto done;
329 }
330 pos += sizeof(rates->header) + tmplen;
331 rates->header.len = cpu_to_le16(tmplen);
332 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
333
334 /* Copy the infra. association rates into Current BSS state structure */
335 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
336 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
337
338 /* Set MSB on basic rates as the firmware requires, but _after_
339 * copying to current bss rates.
340 */
341 lbs_set_basic_rate_flags(rates->rates, tmplen);
342
343 /* Firmware v9+ indicate authentication suites as a TLV */
344 if (priv->fwrelease >= 0x09000000) {
345 DECLARE_MAC_BUF(mac);
346
347 auth = (struct mrvl_ie_auth_type *) pos;
348 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
349 auth->header.len = cpu_to_le16(2);
350 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
351 auth->auth = cpu_to_le16(tmpauth);
352 pos += sizeof(auth->header) + 2;
353
354 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
355 print_mac(mac, bss->bssid), priv->secinfo.auth_mode);
356 }
357
358 /* WPA/WPA2 IEs */
359 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
360 rsn = (struct mrvl_ie_rsn_param_set *) pos;
361 /* WPA_IE or WPA2_IE */
362 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
363 tmplen = (u16) assoc_req->wpa_ie[1];
364 rsn->header.len = cpu_to_le16(tmplen);
365 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
366 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
367 sizeof(rsn->header) + tmplen);
368 pos += sizeof(rsn->header) + tmplen;
369 }
370
371 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
372 (u16)(pos - (u8 *) &cmd.iebuf));
373
374 /* update curbssparams */
375 priv->curbssparams.channel = bss->phy.ds.channel;
376
377 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
378 ret = -1;
379 goto done;
380 }
381
382 ret = lbs_cmd_with_response(priv, command, &cmd);
383 if (ret == 0) {
384 ret = lbs_assoc_post(priv,
385 (struct cmd_ds_802_11_associate_response *) &cmd);
386 }
387
388done:
389 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
390 return ret;
391}
392
Holger Schurig697900a2008-04-02 16:27:10 +0200393/**
394 * @brief Associate to a specific BSS discovered in a scan
395 *
396 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400397 * @param assoc_req The association request describing the BSS to associate with
Holger Schurig697900a2008-04-02 16:27:10 +0200398 *
399 * @return 0-success, otherwise fail
400 */
Dan Williams822ac032009-05-22 20:07:14 -0400401static int lbs_try_associate(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200402 struct assoc_request *assoc_req)
403{
404 int ret;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400405 u8 preamble = RADIO_PREAMBLE_LONG;
Holger Schurig697900a2008-04-02 16:27:10 +0200406
407 lbs_deb_enter(LBS_DEB_ASSOC);
408
Dan Williamsbe0d76e2009-05-22 20:05:25 -0400409 /* FW v9 and higher indicate authentication suites as a TLV in the
410 * association command, not as a separate authentication command.
411 */
412 if (priv->fwrelease < 0x09000000) {
413 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
414 priv->secinfo.auth_mode);
415 if (ret)
416 goto out;
417 }
Holger Schurig697900a2008-04-02 16:27:10 +0200418
Dan Williamsd5db2df2008-08-21 17:51:07 -0400419 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig697900a2008-04-02 16:27:10 +0200420 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
421 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
Dan Williamsd5db2df2008-08-21 17:51:07 -0400422 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200423
Dan Williamsd5db2df2008-08-21 17:51:07 -0400424 ret = lbs_set_radio(priv, preamble, 1);
425 if (ret)
426 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200427
Dan Williams822ac032009-05-22 20:07:14 -0400428 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
Holger Schurig697900a2008-04-02 16:27:10 +0200429
Dan Williamsd5db2df2008-08-21 17:51:07 -0400430out:
Holger Schurig697900a2008-04-02 16:27:10 +0200431 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
432 return ret;
433}
434
Dan Williams822ac032009-05-22 20:07:14 -0400435static int lbs_adhoc_post(struct lbs_private *priv,
436 struct cmd_ds_802_11_ad_hoc_result *resp)
437{
438 int ret = 0;
439 u16 command = le16_to_cpu(resp->hdr.command);
440 u16 result = le16_to_cpu(resp->hdr.result);
441 union iwreq_data wrqu;
442 struct bss_descriptor *bss;
443 DECLARE_SSID_BUF(ssid);
444
445 lbs_deb_enter(LBS_DEB_JOIN);
446
447 if (!priv->in_progress_assoc_req) {
448 lbs_deb_join("ADHOC_RESP: no in-progress association "
449 "request\n");
450 ret = -1;
451 goto done;
452 }
453 bss = &priv->in_progress_assoc_req->bss;
454
455 /*
456 * Join result code 0 --> SUCCESS
457 */
458 if (result) {
459 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
460 if (priv->connect_status == LBS_CONNECTED)
461 lbs_mac_event_disconnected(priv);
462 ret = -1;
463 goto done;
464 }
465
466 /* Send a Media Connected event, according to the Spec */
467 priv->connect_status = LBS_CONNECTED;
468
469 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
470 /* Update the created network descriptor with the new BSSID */
471 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
472 }
473
474 /* Set the BSSID from the joined/started descriptor */
475 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
476
477 /* Set the new SSID to current SSID */
478 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
479 priv->curbssparams.ssid_len = bss->ssid_len;
480
481 netif_carrier_on(priv->dev);
482 if (!priv->tx_pending_len)
483 netif_wake_queue(priv->dev);
484
485 memset(&wrqu, 0, sizeof(wrqu));
486 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
487 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
488 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
489
490 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
491 print_ssid(ssid, bss->ssid, bss->ssid_len),
492 priv->curbssparams.bssid,
493 priv->curbssparams.channel);
494
495done:
496 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
497 return ret;
498}
499
Holger Schurig697900a2008-04-02 16:27:10 +0200500/**
501 * @brief Join an adhoc network found in a previous scan
502 *
503 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400504 * @param assoc_req The association request describing the BSS to join
Holger Schurig697900a2008-04-02 16:27:10 +0200505 *
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400506 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200507 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400508static int lbs_adhoc_join(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200509 struct assoc_request *assoc_req)
510{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400511 struct cmd_ds_802_11_ad_hoc_join cmd;
Holger Schurig697900a2008-04-02 16:27:10 +0200512 struct bss_descriptor *bss = &assoc_req->bss;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400513 u8 preamble = RADIO_PREAMBLE_LONG;
John W. Linville9387b7c2008-09-30 20:59:05 -0400514 DECLARE_SSID_BUF(ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400515 u16 ratesize = 0;
516 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400517
518 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200519
520 lbs_deb_join("current SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400521 print_ssid(ssid, priv->curbssparams.ssid,
Holger Schurig697900a2008-04-02 16:27:10 +0200522 priv->curbssparams.ssid_len),
523 priv->curbssparams.ssid_len);
524 lbs_deb_join("requested ssid '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400525 print_ssid(ssid, bss->ssid, bss->ssid_len),
Holger Schurig697900a2008-04-02 16:27:10 +0200526 bss->ssid_len);
527
528 /* check if the requested SSID is already joined */
529 if (priv->curbssparams.ssid_len &&
530 !lbs_ssid_cmp(priv->curbssparams.ssid,
531 priv->curbssparams.ssid_len,
532 bss->ssid, bss->ssid_len) &&
533 (priv->mode == IW_MODE_ADHOC) &&
534 (priv->connect_status == LBS_CONNECTED)) {
535 union iwreq_data wrqu;
536
537 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
538 "current, not attempting to re-join");
539
540 /* Send the re-association event though, because the association
541 * request really was successful, even if just a null-op.
542 */
543 memset(&wrqu, 0, sizeof(wrqu));
544 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
545 ETH_ALEN);
546 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
547 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
548 goto out;
549 }
550
Dan Williamsd5db2df2008-08-21 17:51:07 -0400551 /* Use short preamble only when both the BSS and firmware support it */
552 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
553 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig697900a2008-04-02 16:27:10 +0200554 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400555 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200556 }
557
Dan Williamsd5db2df2008-08-21 17:51:07 -0400558 ret = lbs_set_radio(priv, preamble, 1);
559 if (ret)
560 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200561
562 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
563 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
564
565 priv->adhoccreate = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400566 priv->curbssparams.channel = bss->channel;
Holger Schurig697900a2008-04-02 16:27:10 +0200567
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400568 /* Build the join command */
569 memset(&cmd, 0, sizeof(cmd));
570 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
571
572 cmd.bss.type = CMD_BSS_TYPE_IBSS;
573 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
574
575 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
576 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
577
Dan Williams5fd164e2009-05-22 20:01:21 -0400578 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400579
Dan Williams5fd164e2009-05-22 20:01:21 -0400580 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
581 sizeof(struct ieee_ie_ibss_param_set));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400582
583 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
584 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
585 bss->capability, CAPINFO_MASK);
586
587 /* information on BSSID descriptor passed to FW */
Johannes Berge1749612008-10-27 15:59:26 -0700588 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
589 cmd.bss.bssid, cmd.bss.ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400590
591 /* Only v8 and below support setting these */
592 if (priv->fwrelease < 0x09000000) {
593 /* failtimeout */
594 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
595 /* probedelay */
596 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
597 }
598
599 /* Copy Data rates from the rates recorded in scan response */
600 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
601 ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
602 memcpy(cmd.bss.rates, bss->rates, ratesize);
603 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
604 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
605 ret = -1;
606 goto out;
607 }
608
609 /* Copy the ad-hoc creation rates into Current BSS state structure */
610 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
611 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
612
613 /* Set MSB on basic rates as the firmware requires, but _after_
614 * copying to current bss rates.
615 */
616 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
617
Dan Williams5fd164e2009-05-22 20:01:21 -0400618 cmd.bss.ibss.atimwindow = bss->atimwindow;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400619
620 if (assoc_req->secinfo.wep_enabled) {
621 u16 tmp = le16_to_cpu(cmd.bss.capability);
622 tmp |= WLAN_CAPABILITY_PRIVACY;
623 cmd.bss.capability = cpu_to_le16(tmp);
624 }
625
626 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
627 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
628
629 /* wake up first */
630 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
631 CMD_ACT_SET, 0, 0,
632 &local_ps_mode);
633 if (ret) {
634 ret = -1;
635 goto out;
636 }
637 }
638
639 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
640 ret = -1;
641 goto out;
642 }
643
644 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
Dan Williams822ac032009-05-22 20:07:14 -0400645 if (ret == 0) {
646 ret = lbs_adhoc_post(priv,
647 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
648 }
Holger Schurig697900a2008-04-02 16:27:10 +0200649
650out:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400651 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200652 return ret;
653}
654
655/**
656 * @brief Start an Adhoc Network
657 *
658 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400659 * @param assoc_req The association request describing the BSS to start
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400660 *
661 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200662 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400663static int lbs_adhoc_start(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200664 struct assoc_request *assoc_req)
665{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400666 struct cmd_ds_802_11_ad_hoc_start cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400667 u8 preamble = RADIO_PREAMBLE_LONG;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400668 size_t ratesize = 0;
669 u16 tmpcap = 0;
670 int ret = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400671 DECLARE_SSID_BUF(ssid);
Dan Williamsd5db2df2008-08-21 17:51:07 -0400672
673 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200674
Holger Schurig697900a2008-04-02 16:27:10 +0200675 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400676 lbs_deb_join("ADHOC_START: Will use short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400677 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200678 }
679
Dan Williamsd5db2df2008-08-21 17:51:07 -0400680 ret = lbs_set_radio(priv, preamble, 1);
681 if (ret)
682 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200683
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400684 /* Build the start command */
685 memset(&cmd, 0, sizeof(cmd));
686 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurig697900a2008-04-02 16:27:10 +0200687
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400688 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
689
690 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400691 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400692 assoc_req->ssid_len);
693
694 cmd.bsstype = CMD_BSS_TYPE_IBSS;
695
696 if (priv->beacon_period == 0)
697 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
698 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
699
700 WARN_ON(!assoc_req->channel);
701
702 /* set Physical parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400703 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
704 cmd.ds.header.len = 1;
Dan Williams5fd164e2009-05-22 20:01:21 -0400705 cmd.ds.channel = assoc_req->channel;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400706
707 /* set IBSS parameter set */
Dan Williams75b6a612009-05-22 20:03:09 -0400708 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
709 cmd.ibss.header.len = 2;
Dan Williams5fd164e2009-05-22 20:01:21 -0400710 cmd.ibss.atimwindow = cpu_to_le16(0);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400711
712 /* set capability info */
713 tmpcap = WLAN_CAPABILITY_IBSS;
Dan Williams2fa7a982009-05-22 20:09:58 -0400714 if (assoc_req->secinfo.wep_enabled ||
715 assoc_req->secinfo.WPAenabled ||
716 assoc_req->secinfo.WPA2enabled) {
717 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400718 tmpcap |= WLAN_CAPABILITY_PRIVACY;
719 } else
Dan Williams2fa7a982009-05-22 20:09:58 -0400720 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400721
722 cmd.capability = cpu_to_le16(tmpcap);
723
724 /* Only v8 and below support setting probe delay */
725 if (priv->fwrelease < 0x09000000)
726 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
727
728 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
729 memcpy(cmd.rates, lbs_bg_rates, ratesize);
730
731 /* Copy the ad-hoc creating rates into Current BSS state structure */
732 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
733 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
734
735 /* Set MSB on basic rates as the firmware requires, but _after_
736 * copying to current bss rates.
737 */
738 lbs_set_basic_rate_flags(cmd.rates, ratesize);
739
740 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
741 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
742
743 if (lbs_create_dnld_countryinfo_11d(priv)) {
744 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
745 ret = -1;
746 goto out;
747 }
748
749 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
750 assoc_req->channel, assoc_req->band);
751
752 priv->adhoccreate = 1;
753 priv->mode = IW_MODE_ADHOC;
754
755 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
756 if (ret == 0)
Dan Williams822ac032009-05-22 20:07:14 -0400757 ret = lbs_adhoc_post(priv,
758 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200759
Dan Williamsd5db2df2008-08-21 17:51:07 -0400760out:
761 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200762 return ret;
763}
764
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400765/**
766 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
767 *
768 * @param priv A pointer to struct lbs_private structure
769 * @return 0 on success, or an error
770 */
771int lbs_adhoc_stop(struct lbs_private *priv)
Holger Schurig697900a2008-04-02 16:27:10 +0200772{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400773 struct cmd_ds_802_11_ad_hoc_stop cmd;
774 int ret;
775
776 lbs_deb_enter(LBS_DEB_JOIN);
777
778 memset(&cmd, 0, sizeof (cmd));
779 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
780
781 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
782
783 /* Clean up everything even if there was an error */
784 lbs_mac_event_disconnected(priv);
785
786 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
787 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +0200788}
Dan Williamse76850d2007-05-25 17:09:41 -0400789
Holger Schurig245bf202008-04-02 16:27:42 +0200790static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
791 struct bss_descriptor *match_bss)
792{
793 if (!secinfo->wep_enabled && !secinfo->WPAenabled
794 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100795 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
796 && match_bss->rsn_ie[0] != WLAN_EID_RSN
Holger Schurig245bf202008-04-02 16:27:42 +0200797 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
798 return 1;
799 else
800 return 0;
801}
802
803static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
804 struct bss_descriptor *match_bss)
805{
806 if (secinfo->wep_enabled && !secinfo->WPAenabled
807 && !secinfo->WPA2enabled
808 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
809 return 1;
810 else
811 return 0;
812}
813
814static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
815 struct bss_descriptor *match_bss)
816{
817 if (!secinfo->wep_enabled && secinfo->WPAenabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100818 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
Holger Schurig245bf202008-04-02 16:27:42 +0200819 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
820 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
821 )
822 return 1;
823 else
824 return 0;
825}
826
827static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
828 struct bss_descriptor *match_bss)
829{
830 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
Johannes Berg2c7060022008-10-30 22:09:54 +0100831 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200832 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
833 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
834 )
835 return 1;
836 else
837 return 0;
838}
839
840static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
841 struct bss_descriptor *match_bss)
842{
843 if (!secinfo->wep_enabled && !secinfo->WPAenabled
844 && !secinfo->WPA2enabled
Johannes Berg2c7060022008-10-30 22:09:54 +0100845 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
846 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
Holger Schurig245bf202008-04-02 16:27:42 +0200847 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
848 return 1;
849 else
850 return 0;
851}
852
853/**
854 * @brief Check if a scanned network compatible with the driver settings
855 *
856 * WEP WPA WPA2 ad-hoc encrypt Network
857 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
858 * 0 0 0 0 NONE 0 0 0 yes No security
859 * 1 0 0 0 NONE 1 0 0 yes Static WEP
860 * 0 1 0 0 x 1x 1 x yes WPA
861 * 0 0 1 0 x 1x x 1 yes WPA2
862 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
863 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
864 *
865 *
866 * @param priv A pointer to struct lbs_private
867 * @param index Index in scantable to check against current driver settings
868 * @param mode Network mode: Infrastructure or IBSS
869 *
870 * @return Index in scantable, or error code if negative
871 */
872static int is_network_compatible(struct lbs_private *priv,
873 struct bss_descriptor *bss, uint8_t mode)
874{
875 int matched = 0;
876
877 lbs_deb_enter(LBS_DEB_SCAN);
878
879 if (bss->mode != mode)
880 goto done;
881
882 matched = match_bss_no_security(&priv->secinfo, bss);
883 if (matched)
884 goto done;
885 matched = match_bss_static_wep(&priv->secinfo, bss);
886 if (matched)
887 goto done;
888 matched = match_bss_wpa(&priv->secinfo, bss);
889 if (matched) {
890 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
891 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
892 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
893 priv->secinfo.wep_enabled ? "e" : "d",
894 priv->secinfo.WPAenabled ? "e" : "d",
895 priv->secinfo.WPA2enabled ? "e" : "d",
896 (bss->capability & WLAN_CAPABILITY_PRIVACY));
897 goto done;
898 }
899 matched = match_bss_wpa2(&priv->secinfo, bss);
900 if (matched) {
901 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
902 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
903 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
904 priv->secinfo.wep_enabled ? "e" : "d",
905 priv->secinfo.WPAenabled ? "e" : "d",
906 priv->secinfo.WPA2enabled ? "e" : "d",
907 (bss->capability & WLAN_CAPABILITY_PRIVACY));
908 goto done;
909 }
910 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
911 if (matched) {
912 lbs_deb_scan("is_network_compatible() dynamic WEP: "
913 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
914 bss->wpa_ie[0], bss->rsn_ie[0],
915 (bss->capability & WLAN_CAPABILITY_PRIVACY));
916 goto done;
917 }
918
919 /* bss security settings don't match those configured on card */
920 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
921 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
922 bss->wpa_ie[0], bss->rsn_ie[0],
923 priv->secinfo.wep_enabled ? "e" : "d",
924 priv->secinfo.WPAenabled ? "e" : "d",
925 priv->secinfo.WPA2enabled ? "e" : "d",
926 (bss->capability & WLAN_CAPABILITY_PRIVACY));
927
928done:
929 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
930 return matched;
931}
932
933/**
934 * @brief This function finds a specific compatible BSSID in the scan list
935 *
936 * Used in association code
937 *
938 * @param priv A pointer to struct lbs_private
939 * @param bssid BSSID to find in the scan list
940 * @param mode Network mode: Infrastructure or IBSS
941 *
942 * @return index in BSSID list, or error return code (< 0)
943 */
944static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
945 uint8_t *bssid, uint8_t mode)
946{
947 struct bss_descriptor *iter_bss;
948 struct bss_descriptor *found_bss = NULL;
949
950 lbs_deb_enter(LBS_DEB_SCAN);
951
952 if (!bssid)
953 goto out;
954
955 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
956
957 /* Look through the scan table for a compatible match. The loop will
958 * continue past a matched bssid that is not compatible in case there
959 * is an AP with multiple SSIDs assigned to the same BSSID
960 */
961 mutex_lock(&priv->lock);
962 list_for_each_entry(iter_bss, &priv->network_list, list) {
963 if (compare_ether_addr(iter_bss->bssid, bssid))
964 continue; /* bssid doesn't match */
965 switch (mode) {
966 case IW_MODE_INFRA:
967 case IW_MODE_ADHOC:
968 if (!is_network_compatible(priv, iter_bss, mode))
969 break;
970 found_bss = iter_bss;
971 break;
972 default:
973 found_bss = iter_bss;
974 break;
975 }
976 }
977 mutex_unlock(&priv->lock);
978
979out:
980 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
981 return found_bss;
982}
983
984/**
985 * @brief This function finds ssid in ssid list.
986 *
987 * Used in association code
988 *
989 * @param priv A pointer to struct lbs_private
990 * @param ssid SSID to find in the list
991 * @param bssid BSSID to qualify the SSID selection (if provided)
992 * @param mode Network mode: Infrastructure or IBSS
993 *
994 * @return index in BSSID list
995 */
996static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
997 uint8_t *ssid, uint8_t ssid_len,
998 uint8_t *bssid, uint8_t mode,
999 int channel)
1000{
1001 u32 bestrssi = 0;
1002 struct bss_descriptor *iter_bss = NULL;
1003 struct bss_descriptor *found_bss = NULL;
1004 struct bss_descriptor *tmp_oldest = NULL;
1005
1006 lbs_deb_enter(LBS_DEB_SCAN);
1007
1008 mutex_lock(&priv->lock);
1009
1010 list_for_each_entry(iter_bss, &priv->network_list, list) {
1011 if (!tmp_oldest ||
1012 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1013 tmp_oldest = iter_bss;
1014
1015 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1016 ssid, ssid_len) != 0)
1017 continue; /* ssid doesn't match */
1018 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1019 continue; /* bssid doesn't match */
1020 if ((channel > 0) && (iter_bss->channel != channel))
1021 continue; /* channel doesn't match */
1022
1023 switch (mode) {
1024 case IW_MODE_INFRA:
1025 case IW_MODE_ADHOC:
1026 if (!is_network_compatible(priv, iter_bss, mode))
1027 break;
1028
1029 if (bssid) {
1030 /* Found requested BSSID */
1031 found_bss = iter_bss;
1032 goto out;
1033 }
1034
1035 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1036 bestrssi = SCAN_RSSI(iter_bss->rssi);
1037 found_bss = iter_bss;
1038 }
1039 break;
1040 case IW_MODE_AUTO:
1041 default:
1042 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1043 bestrssi = SCAN_RSSI(iter_bss->rssi);
1044 found_bss = iter_bss;
1045 }
1046 break;
1047 }
1048 }
1049
1050out:
1051 mutex_unlock(&priv->lock);
1052 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1053 return found_bss;
1054}
1055
Holger Schurig69f90322007-11-23 15:43:44 +01001056static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001057 struct assoc_request * assoc_req)
1058{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001059 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001060 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001061 int channel = -1;
John W. Linville9387b7c2008-09-30 20:59:05 -04001062 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001063
Holger Schurig9012b282007-05-25 11:27:16 -04001064 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001065
Dan Williamsef9a2642007-05-25 16:46:33 -04001066 /* FIXME: take channel into account when picking SSIDs if a channel
1067 * is set.
1068 */
1069
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001070 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1071 channel = assoc_req->channel;
1072
Holger Schurig0765af42007-10-15 12:55:56 +02001073 lbs_deb_assoc("SSID '%s' requested\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001074 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -04001075 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -05001076 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001077 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001078
David Woodhouseaa21c002007-12-08 20:04:36 +00001079 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001080 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001081 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -04001082 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams822ac032009-05-22 20:07:14 -04001083 ret = lbs_try_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001084 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -04001085 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001086 }
Dan Williams0dc5a292007-05-10 22:58:02 -04001087 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001088 /* Scan for the network, do not save previous results. Stale
1089 * scan data will cause us to join a non-existant adhoc network
1090 */
Holger Schurig10078322007-11-15 18:05:47 -05001091 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001092 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001093
1094 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001095 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001096 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001097 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -04001098 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001099 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001100 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001101 } else {
1102 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -04001103 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -04001104 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001105 IW_ESSID_MAX_SIZE);
1106 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001107 lbs_adhoc_start(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001108 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001109 }
1110
Holger Schurig9012b282007-05-25 11:27:16 -04001111 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001112 return ret;
1113}
1114
1115
Holger Schurig69f90322007-11-23 15:43:44 +01001116static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001117 struct assoc_request * assoc_req)
1118{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001119 int ret = 0;
1120 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001121
Johannes Berge1749612008-10-27 15:59:26 -07001122 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001123
1124 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +00001125 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001126 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001127 if (bss == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -07001128 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1129 "cannot associate.\n", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001130 goto out;
1131 }
1132
Dan Williamse76850d2007-05-25 17:09:41 -04001133 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -04001134 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williams822ac032009-05-22 20:07:14 -04001135 ret = lbs_try_associate(priv, assoc_req);
1136 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1137 ret);
Dan Williams0dc5a292007-05-10 22:58:02 -04001138 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001139 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001140 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001141
1142out:
Holger Schurig9012b282007-05-25 11:27:16 -04001143 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001144 return ret;
1145}
1146
1147
Holger Schurig69f90322007-11-23 15:43:44 +01001148static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001149 struct assoc_request * assoc_req)
1150{
1151 int ret = 0, done = 0;
1152
Holger Schurig0765af42007-10-15 12:55:56 +02001153 lbs_deb_enter(LBS_DEB_ASSOC);
1154
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001155 /* If we're given and 'any' BSSID, try associating based on SSID */
1156
1157 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -04001158 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1159 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001160 ret = assoc_helper_bssid(priv, assoc_req);
1161 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001162 }
1163 }
1164
1165 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1166 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001167 }
1168
Holger Schurig0765af42007-10-15 12:55:56 +02001169 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001170 return ret;
1171}
1172
1173
Holger Schurig69f90322007-11-23 15:43:44 +01001174static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001175 struct assoc_request * assoc_req)
1176{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001177 int ret = 0;
1178
Holger Schurig9012b282007-05-25 11:27:16 -04001179 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001180
David Woodhouseaa21c002007-12-08 20:04:36 +00001181 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -04001182 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001183
Dan Williams0dc5a292007-05-10 22:58:02 -04001184 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001185 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -05001186 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +00001187 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001188 }
1189
David Woodhouseaa21c002007-12-08 20:04:36 +00001190 priv->mode = assoc_req->mode;
Dan Williams39fcf7a2008-09-10 12:49:00 -04001191 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001192
Holger Schurig9012b282007-05-25 11:27:16 -04001193done:
1194 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001195 return ret;
1196}
1197
Holger Schurig69f90322007-11-23 15:43:44 +01001198static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -04001199 struct assoc_request * assoc_req)
1200{
Dan Williamsef9a2642007-05-25 16:46:33 -04001201 int ret = 0;
1202
1203 lbs_deb_enter(LBS_DEB_ASSOC);
1204
David Woodhouse9f462572007-12-12 22:50:21 -05001205 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001206 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001207 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001208 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -04001209 }
1210
David Woodhouseaa21c002007-12-08 20:04:36 +00001211 if (assoc_req->channel == priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001212 goto done;
1213
David Woodhouse8642f1f2007-12-11 20:03:01 -05001214 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -05001215 /* Change mesh channel first; 21.p21 firmware won't let
1216 you change channel otherwise (even though it'll return
1217 an error to this */
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001218 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1219 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001220 }
1221
Dan Williamsef9a2642007-05-25 16:46:33 -04001222 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
David Woodhouse86062132007-12-13 00:32:36 -05001223 priv->curbssparams.channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -04001224
Dan Williams2dd4b262007-12-11 16:54:15 -05001225 ret = lbs_set_channel(priv, assoc_req->channel);
1226 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -05001227 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -04001228
Dan Williams2dd4b262007-12-11 16:54:15 -05001229 /* FIXME: shouldn't need to grab the channel _again_ after setting
1230 * it since the firmware is supposed to return the new channel, but
1231 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -05001232 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -05001233 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001234 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -05001235 goto done;
1236 }
Dan Williamsef9a2642007-05-25 16:46:33 -04001237
David Woodhouseaa21c002007-12-08 20:04:36 +00001238 if (assoc_req->channel != priv->curbssparams.channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -05001239 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -04001240 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001241 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -04001242 }
1243
1244 if ( assoc_req->secinfo.wep_enabled
1245 && (assoc_req->wep_keys[0].len
1246 || assoc_req->wep_keys[1].len
1247 || assoc_req->wep_keys[2].len
1248 || assoc_req->wep_keys[3].len)) {
1249 /* Make sure WEP keys are re-sent to firmware */
1250 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1251 }
1252
1253 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -05001254 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -04001255
David Woodhouse8642f1f2007-12-11 20:03:01 -05001256 restore_mesh:
1257 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -07001258 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1259 priv->curbssparams.channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -05001260
1261 done:
Dan Williamsef9a2642007-05-25 16:46:33 -04001262 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1263 return ret;
1264}
1265
1266
Holger Schurig69f90322007-11-23 15:43:44 +01001267static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -05001268 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001269{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001270 int i;
1271 int ret = 0;
1272
Holger Schurig9012b282007-05-25 11:27:16 -04001273 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001274
1275 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -05001276 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1277 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1278 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1279 else
1280 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001281
1282 if (ret)
1283 goto out;
1284
1285 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -04001286 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +01001287 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001288 else
Holger Schurigd9e97782008-03-12 16:06:43 +01001289 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -05001290
Holger Schurigc97329e2008-03-18 11:20:21 +01001291 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001292
David Woodhouseaa21c002007-12-08 20:04:36 +00001293 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001294
David Woodhouseaa21c002007-12-08 20:04:36 +00001295 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001296 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001297 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -05001298 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001299 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001300 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001301
David Woodhouseaa21c002007-12-08 20:04:36 +00001302 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001303
1304out:
Holger Schurig9012b282007-05-25 11:27:16 -04001305 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001306 return ret;
1307}
1308
Holger Schurig69f90322007-11-23 15:43:44 +01001309static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001310 struct assoc_request * assoc_req)
1311{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001312 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -05001313 uint16_t do_wpa;
1314 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001315
Holger Schurig9012b282007-05-25 11:27:16 -04001316 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001317
David Woodhouseaa21c002007-12-08 20:04:36 +00001318 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001319 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001320
Holger Schurigc97329e2008-03-18 11:20:21 +01001321 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001322
Dan Williams18c96c342007-06-18 12:01:12 -04001323 /* If RSN is already enabled, don't try to enable it again, since
1324 * ENABLE_RSN resets internal state machines and will clobber the
1325 * 4-way WPA handshake.
1326 */
1327
1328 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001329 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -04001330 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -05001331 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -04001332 goto out;
1333 }
1334
1335 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001336 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -04001337 if (do_wpa == rsn)
1338 goto out;
1339
1340 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -05001341 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -04001342
1343out:
Holger Schurig9012b282007-05-25 11:27:16 -04001344 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001345 return ret;
1346}
1347
1348
Holger Schurig69f90322007-11-23 15:43:44 +01001349static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001350 struct assoc_request * assoc_req)
1351{
1352 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -04001353 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001354
Holger Schurig9012b282007-05-25 11:27:16 -04001355 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001356
Dan Williams2bcde512007-10-03 10:37:45 -04001357 /* Work around older firmware bug where WPA unicast and multicast
1358 * keys must be set independently. Seen in SDIO parts with firmware
1359 * version 5.0.11p0.
1360 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001361
Dan Williams2bcde512007-10-03 10:37:45 -04001362 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1363 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +01001364 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001365 assoc_req->flags = flags;
1366 }
1367
1368 if (ret)
1369 goto out;
1370
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001371 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1372 sizeof(struct enc_key));
1373
Dan Williams2bcde512007-10-03 10:37:45 -04001374 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1375 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1376
David Woodhouse9e1228d2008-03-03 12:15:39 +01001377 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001378 assoc_req->flags = flags;
Andrey Yurovskyce8d0962009-06-17 18:45:34 -07001379
1380 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1381 sizeof(struct enc_key));
Dan Williams2bcde512007-10-03 10:37:45 -04001382 }
1383
1384out:
Holger Schurig9012b282007-05-25 11:27:16 -04001385 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001386 return ret;
1387}
1388
1389
Holger Schurig69f90322007-11-23 15:43:44 +01001390static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001391 struct assoc_request * assoc_req)
1392{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001393 int ret = 0;
1394
Holger Schurig9012b282007-05-25 11:27:16 -04001395 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001396
1397 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001398 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1399 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001400 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +00001401 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1402 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001403 }
1404
Holger Schurig9012b282007-05-25 11:27:16 -04001405 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001406 return ret;
1407}
1408
1409
David Woodhouseaa21c002007-12-08 20:04:36 +00001410static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001411 struct assoc_request * assoc_req)
1412{
Holger Schurig0765af42007-10-15 12:55:56 +02001413 int ret = 0;
1414
David Woodhouseaa21c002007-12-08 20:04:36 +00001415 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001416 return 0;
1417
Holger Schurig52507c22008-01-28 17:25:53 +01001418 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001419 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001420 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1421 ret = 1;
1422 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001423 }
1424
1425 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001426 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +02001427 lbs_deb_assoc("Deauthenticating due to new security\n");
1428 ret = 1;
1429 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001430 }
1431 }
1432
1433 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001434 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1435 ret = 1;
1436 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001437 }
1438
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001439 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001440 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1441 ret = 1;
1442 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001443 }
1444
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001445 /* FIXME: deal with 'auto' mode somehow */
1446 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001447 if (assoc_req->mode != IW_MODE_INFRA) {
1448 lbs_deb_assoc("Deauthenticating due to leaving "
1449 "infra mode\n");
1450 ret = 1;
1451 goto out;
1452 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001453 }
1454
Holger Schurig0765af42007-10-15 12:55:56 +02001455out:
1456 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +01001457 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001458}
1459
1460
David Woodhouseaa21c002007-12-08 20:04:36 +00001461static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001462 struct assoc_request * assoc_req)
1463{
Holger Schurig0765af42007-10-15 12:55:56 +02001464 lbs_deb_enter(LBS_DEB_ASSOC);
1465
David Woodhouseaa21c002007-12-08 20:04:36 +00001466 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001467 return 0;
1468
David Woodhouseaa21c002007-12-08 20:04:36 +00001469 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1470 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001471 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001472 return 1;
1473
1474 /* FIXME: deal with 'auto' mode somehow */
1475 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001476 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001477 return 1;
1478 }
1479
Dan Williamsef9a2642007-05-25 16:46:33 -04001480 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001481 if (assoc_req->channel != priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001482 return 1;
1483 }
1484
Holger Schurig0765af42007-10-15 12:55:56 +02001485 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001486 return 0;
1487}
1488
1489
Holger Schurig245bf202008-04-02 16:27:42 +02001490/**
1491 * @brief This function finds the best SSID in the Scan List
1492 *
1493 * Search the scan table for the best SSID that also matches the current
1494 * adapter network preference (infrastructure or adhoc)
1495 *
1496 * @param priv A pointer to struct lbs_private
1497 *
1498 * @return index in BSSID list
1499 */
1500static struct bss_descriptor *lbs_find_best_ssid_in_list(
1501 struct lbs_private *priv, uint8_t mode)
1502{
1503 uint8_t bestrssi = 0;
1504 struct bss_descriptor *iter_bss;
1505 struct bss_descriptor *best_bss = NULL;
1506
1507 lbs_deb_enter(LBS_DEB_SCAN);
1508
1509 mutex_lock(&priv->lock);
1510
1511 list_for_each_entry(iter_bss, &priv->network_list, list) {
1512 switch (mode) {
1513 case IW_MODE_INFRA:
1514 case IW_MODE_ADHOC:
1515 if (!is_network_compatible(priv, iter_bss, mode))
1516 break;
1517 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1518 break;
1519 bestrssi = SCAN_RSSI(iter_bss->rssi);
1520 best_bss = iter_bss;
1521 break;
1522 case IW_MODE_AUTO:
1523 default:
1524 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1525 break;
1526 bestrssi = SCAN_RSSI(iter_bss->rssi);
1527 best_bss = iter_bss;
1528 break;
1529 }
1530 }
1531
1532 mutex_unlock(&priv->lock);
1533 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1534 return best_bss;
1535}
1536
1537/**
1538 * @brief Find the best AP
1539 *
1540 * Used from association worker.
1541 *
1542 * @param priv A pointer to struct lbs_private structure
1543 * @param pSSID A pointer to AP's ssid
1544 *
1545 * @return 0--success, otherwise--fail
1546 */
1547static int lbs_find_best_network_ssid(struct lbs_private *priv,
1548 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1549 uint8_t *out_mode)
1550{
1551 int ret = -1;
1552 struct bss_descriptor *found;
1553
1554 lbs_deb_enter(LBS_DEB_SCAN);
1555
1556 priv->scan_ssid_len = 0;
1557 lbs_scan_networks(priv, 1);
1558 if (priv->surpriseremoved)
1559 goto out;
1560
1561 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1562 if (found && (found->ssid_len > 0)) {
1563 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1564 *out_ssid_len = found->ssid_len;
1565 *out_mode = found->mode;
1566 ret = 0;
1567 }
1568
1569out:
1570 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1571 return ret;
1572}
1573
1574
Holger Schurig10078322007-11-15 18:05:47 -05001575void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001576{
Holger Schurig69f90322007-11-23 15:43:44 +01001577 struct lbs_private *priv = container_of(work, struct lbs_private,
1578 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001579 struct assoc_request * assoc_req = NULL;
1580 int ret = 0;
1581 int find_any_ssid = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001582 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001583
Holger Schurig9012b282007-05-25 11:27:16 -04001584 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001585
David Woodhouseaa21c002007-12-08 20:04:36 +00001586 mutex_lock(&priv->lock);
1587 assoc_req = priv->pending_assoc_req;
1588 priv->pending_assoc_req = NULL;
1589 priv->in_progress_assoc_req = assoc_req;
1590 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001591
Holger Schurig9012b282007-05-25 11:27:16 -04001592 if (!assoc_req)
1593 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001594
Holger Schurig0765af42007-10-15 12:55:56 +02001595 lbs_deb_assoc(
1596 "Association Request:\n"
1597 " flags: 0x%08lx\n"
1598 " SSID: '%s'\n"
1599 " chann: %d\n"
1600 " band: %d\n"
1601 " mode: %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001602 " BSSID: %pM\n"
Holger Schurig0765af42007-10-15 12:55:56 +02001603 " secinfo: %s%s%s\n"
1604 " auth_mode: %d\n",
1605 assoc_req->flags,
John W. Linville9387b7c2008-09-30 20:59:05 -04001606 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Holger Schurig0765af42007-10-15 12:55:56 +02001607 assoc_req->channel, assoc_req->band, assoc_req->mode,
Johannes Berge1749612008-10-27 15:59:26 -07001608 assoc_req->bssid,
Holger Schurig0765af42007-10-15 12:55:56 +02001609 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1610 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1611 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1612 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001613
1614 /* If 'any' SSID was specified, find an SSID to associate with */
1615 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001616 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001617 find_any_ssid = 1;
1618
1619 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1620 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -04001621 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1622 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001623 find_any_ssid = 0;
1624 }
1625
1626 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02001627 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001628
Holger Schurig10078322007-11-15 18:05:47 -05001629 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001630 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001631 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001632 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001633 ret = -ENETUNREACH;
1634 goto out;
1635 }
1636
1637 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04001638 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001639 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1640 assoc_req->mode = new_mode;
1641 }
1642 }
1643
1644 /*
1645 * Check if the attributes being changing require deauthentication
1646 * from the currently associated infrastructure access point.
1647 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001648 if (priv->mode == IW_MODE_INFRA) {
1649 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04001650 ret = lbs_cmd_80211_deauthenticate(priv,
1651 priv->curbssparams.bssid,
1652 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001653 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001654 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001655 "configuration request failed: %d\n",
1656 ret);
1657 }
1658 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001659 } else if (priv->mode == IW_MODE_ADHOC) {
1660 if (should_stop_adhoc(priv, assoc_req)) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001661 ret = lbs_adhoc_stop(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001662 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001663 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001664 "new configuration request failed: %d\n",
1665 ret);
1666 }
1667
1668 }
1669 }
1670
1671 /* Send the various configuration bits to the firmware */
1672 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1673 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001674 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001675 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001676 }
1677
Dan Williamsef9a2642007-05-25 16:46:33 -04001678 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1679 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001680 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04001681 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04001682 }
1683
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001684 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1685 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1686 ret = assoc_helper_wep_keys(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_SECINFO, &assoc_req->flags)) {
1692 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001693 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001694 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001695 }
1696
1697 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1698 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001699 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001700 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001701 }
1702
1703 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1704 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1705 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001706 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001707 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001708 }
1709
1710 /* SSID/BSSID should be the _last_ config option set, because they
1711 * trigger the association attempt.
1712 */
1713 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1714 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1715 int success = 1;
1716
1717 ret = assoc_helper_associate(priv, assoc_req);
1718 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01001719 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001720 ret);
1721 success = 0;
1722 }
1723
David Woodhouseaa21c002007-12-08 20:04:36 +00001724 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01001725 lbs_deb_assoc("ASSOC: association unsuccessful, "
1726 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001727 success = 0;
1728 }
1729
1730 if (success) {
Johannes Berge1749612008-10-27 15:59:26 -07001731 lbs_deb_assoc("associated to %pM\n",
1732 priv->curbssparams.bssid);
Holger Schurig10078322007-11-15 18:05:47 -05001733 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04001734 CMD_802_11_RSSI,
1735 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001736 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001737 ret = -1;
1738 }
1739 }
1740
1741out:
1742 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001743 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001744 ret);
1745 }
Dan Williamse76850d2007-05-25 17:09:41 -04001746
David Woodhouseaa21c002007-12-08 20:04:36 +00001747 mutex_lock(&priv->lock);
1748 priv->in_progress_assoc_req = NULL;
1749 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001750 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04001751
1752done:
1753 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001754}
1755
1756
1757/*
1758 * Caller MUST hold any necessary locks
1759 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001760struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001761{
1762 struct assoc_request * assoc_req;
1763
Holger Schurig0765af42007-10-15 12:55:56 +02001764 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00001765 if (!priv->pending_assoc_req) {
1766 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04001767 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00001768 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001769 lbs_pr_info("Not enough memory to allocate association"
1770 " request!\n");
1771 return NULL;
1772 }
1773 }
1774
1775 /* Copy current configuration attributes to the association request,
1776 * but don't overwrite any that are already set.
1777 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001778 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001779 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001780 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001781 IW_ESSID_MAX_SIZE);
David Woodhouseaa21c002007-12-08 20:04:36 +00001782 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001783 }
1784
1785 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001786 assoc_req->channel = priv->curbssparams.channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001787
Dan Williamse76850d2007-05-25 17:09:41 -04001788 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001789 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04001790
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001791 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001792 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001793
1794 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001795 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001796 ETH_ALEN);
1797 }
1798
1799 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1800 int i;
1801 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001802 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04001803 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001804 }
1805 }
1806
1807 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001808 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001809
1810 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001811 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001812 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001813 }
1814
1815 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001816 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001817 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001818 }
1819
1820 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001821 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001822 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001823 }
1824
1825 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001826 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001827 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001828 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001829 }
1830
Holger Schurig0765af42007-10-15 12:55:56 +02001831 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001832 return assoc_req;
1833}
Holger Schurig697900a2008-04-02 16:27:10 +02001834
1835
1836/**
Dan Williams191bb402008-08-21 17:46:18 -04001837 * @brief Deauthenticate from a specific BSS
1838 *
1839 * @param priv A pointer to struct lbs_private structure
1840 * @param bssid The specific BSS to deauthenticate from
1841 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1842 *
1843 * @return 0 on success, error on failure
1844 */
1845int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1846 u16 reason)
Holger Schurig697900a2008-04-02 16:27:10 +02001847{
Dan Williams191bb402008-08-21 17:46:18 -04001848 struct cmd_ds_802_11_deauthenticate cmd;
1849 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001850
1851 lbs_deb_enter(LBS_DEB_JOIN);
1852
Dan Williams191bb402008-08-21 17:46:18 -04001853 memset(&cmd, 0, sizeof(cmd));
1854 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1855 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1856 cmd.reasoncode = cpu_to_le16(reason);
Holger Schurig697900a2008-04-02 16:27:10 +02001857
Dan Williams191bb402008-08-21 17:46:18 -04001858 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001859
Dan Williams191bb402008-08-21 17:46:18 -04001860 /* Clean up everything even if there was an error; can't assume that
1861 * we're still authenticated to the AP after trying to deauth.
1862 */
1863 lbs_mac_event_disconnected(priv);
Holger Schurig697900a2008-04-02 16:27:10 +02001864
1865 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04001866 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001867}
1868