Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / libertas / assoc.c
blob1902b6f0b78cb5a32c57e2ed4be050dbcf115a97
1 /* Copyright (C) 2006, Red Hat, Inc. */
3 #include <linux/types.h>
4 #include <linux/kernel.h>
5 #include <linux/etherdevice.h>
6 #include <linux/ieee80211.h>
7 #include <linux/if_arp.h>
8 #include <net/lib80211.h>
10 #include "assoc.h"
11 #include "decl.h"
12 #include "host.h"
13 #include "scan.h"
14 #include "cmd.h"
16 static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
17 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
18 static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
19 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
21 /* The firmware needs the following bits masked out of the beacon-derived
22 * capability field when associating/joining to a BSS:
23 * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
25 #define CAPINFO_MASK (~(0xda00))
28 /**
29 * @brief This function finds common rates between rates and card rates.
31 * It will fill common rates in rates as output if found.
33 * NOTE: Setting the MSB of the basic rates need to be taken
34 * care, either before or after calling this function
36 * @param priv A pointer to struct lbs_private structure
37 * @param rates the buffer which keeps input and output
38 * @param rates_size the size of rate1 buffer; new size of buffer on return
40 * @return 0 on success, or -1 on error
42 static int get_common_rates(struct lbs_private *priv,
43 u8 *rates,
44 u16 *rates_size)
46 u8 *card_rates = lbs_bg_rates;
47 int ret = 0, i, j;
48 u8 tmp[(ARRAY_SIZE(lbs_bg_rates) - 1) * (*rates_size - 1)];
49 size_t tmp_size = 0;
51 /* For each rate in card_rates that exists in rate1, copy to tmp */
52 for (i = 0; i < ARRAY_SIZE(lbs_bg_rates) && card_rates[i]; i++) {
53 for (j = 0; j < *rates_size && rates[j]; j++) {
54 if (rates[j] == card_rates[i])
55 tmp[tmp_size++] = card_rates[i];
59 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
60 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates,
61 ARRAY_SIZE(lbs_bg_rates));
62 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
63 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
65 if (!priv->enablehwauto) {
66 for (i = 0; i < tmp_size; i++) {
67 if (tmp[i] == priv->cur_rate)
68 goto done;
70 lbs_pr_alert("Previously set fixed data rate %#x isn't "
71 "compatible with the network.\n", priv->cur_rate);
72 ret = -1;
74 done:
75 memset(rates, 0, *rates_size);
76 *rates_size = min_t(int, tmp_size, *rates_size);
77 memcpy(rates, tmp, *rates_size);
78 return ret;
82 /**
83 * @brief Sets the MSB on basic rates as the firmware requires
85 * Scan through an array and set the MSB for basic data rates.
87 * @param rates buffer of data rates
88 * @param len size of buffer
90 static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
92 int i;
94 for (i = 0; i < len; i++) {
95 if (rates[i] == 0x02 || rates[i] == 0x04 ||
96 rates[i] == 0x0b || rates[i] == 0x16)
97 rates[i] |= 0x80;
102 static u8 iw_auth_to_ieee_auth(u8 auth)
104 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
105 return 0x00;
106 else if (auth == IW_AUTH_ALG_SHARED_KEY)
107 return 0x01;
108 else if (auth == IW_AUTH_ALG_LEAP)
109 return 0x80;
111 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
112 return 0;
116 * @brief This function prepares the authenticate command. AUTHENTICATE only
117 * sets the authentication suite for future associations, as the firmware
118 * handles authentication internally during the ASSOCIATE command.
120 * @param priv A pointer to struct lbs_private structure
121 * @param bssid The peer BSSID with which to authenticate
122 * @param auth The authentication mode to use (from wireless.h)
124 * @return 0 or -1
126 static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
128 struct cmd_ds_802_11_authenticate cmd;
129 int ret = -1;
131 lbs_deb_enter(LBS_DEB_JOIN);
133 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
134 memcpy(cmd.bssid, bssid, ETH_ALEN);
136 cmd.authtype = iw_auth_to_ieee_auth(auth);
138 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
140 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
142 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
143 return ret;
147 static int lbs_assoc_post(struct lbs_private *priv,
148 struct cmd_ds_802_11_associate_response *resp)
150 int ret = 0;
151 union iwreq_data wrqu;
152 struct bss_descriptor *bss;
153 u16 status_code;
155 lbs_deb_enter(LBS_DEB_ASSOC);
157 if (!priv->in_progress_assoc_req) {
158 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
159 ret = -1;
160 goto done;
162 bss = &priv->in_progress_assoc_req->bss;
165 * Older FW versions map the IEEE 802.11 Status Code in the association
166 * response to the following values returned in resp->statuscode:
168 * IEEE Status Code Marvell Status Code
169 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
170 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
171 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
172 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
173 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
174 * others -> 0x0003 ASSOC_RESULT_REFUSED
176 * Other response codes:
177 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
178 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
179 * association response from the AP)
182 status_code = le16_to_cpu(resp->statuscode);
183 if (priv->fwrelease < 0x09000000) {
184 switch (status_code) {
185 case 0x00:
186 break;
187 case 0x01:
188 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
189 break;
190 case 0x02:
191 lbs_deb_assoc("ASSOC_RESP: internal timer "
192 "expired while waiting for the AP\n");
193 break;
194 case 0x03:
195 lbs_deb_assoc("ASSOC_RESP: association "
196 "refused by AP\n");
197 break;
198 case 0x04:
199 lbs_deb_assoc("ASSOC_RESP: authentication "
200 "refused by AP\n");
201 break;
202 default:
203 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
204 " unknown\n", status_code);
205 break;
207 } else {
208 /* v9+ returns the AP's association response */
209 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
212 if (status_code) {
213 lbs_mac_event_disconnected(priv);
214 ret = -1;
215 goto done;
218 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
219 (void *) (resp + sizeof (resp->hdr)),
220 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
222 /* Send a Media Connected event, according to the Spec */
223 priv->connect_status = LBS_CONNECTED;
225 /* Update current SSID and BSSID */
226 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
227 priv->curbssparams.ssid_len = bss->ssid_len;
228 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
230 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
231 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
233 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
234 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
235 priv->nextSNRNF = 0;
236 priv->numSNRNF = 0;
238 netif_carrier_on(priv->dev);
239 if (!priv->tx_pending_len)
240 netif_wake_queue(priv->dev);
242 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
243 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
244 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
246 done:
247 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
248 return ret;
252 * @brief This function prepares an association-class command.
254 * @param priv A pointer to struct lbs_private structure
255 * @param assoc_req The association request describing the BSS to associate
256 * or reassociate with
257 * @param command The actual command, either CMD_802_11_ASSOCIATE or
258 * CMD_802_11_REASSOCIATE
260 * @return 0 or -1
262 static int lbs_associate(struct lbs_private *priv,
263 struct assoc_request *assoc_req,
264 u16 command)
266 struct cmd_ds_802_11_associate cmd;
267 int ret = 0;
268 struct bss_descriptor *bss = &assoc_req->bss;
269 u8 *pos = &(cmd.iebuf[0]);
270 u16 tmpcap, tmplen, tmpauth;
271 struct mrvl_ie_ssid_param_set *ssid;
272 struct mrvl_ie_ds_param_set *ds;
273 struct mrvl_ie_cf_param_set *cf;
274 struct mrvl_ie_rates_param_set *rates;
275 struct mrvl_ie_rsn_param_set *rsn;
276 struct mrvl_ie_auth_type *auth;
278 lbs_deb_enter(LBS_DEB_ASSOC);
280 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
281 (command != CMD_802_11_REASSOCIATE));
283 memset(&cmd, 0, sizeof(cmd));
284 cmd.hdr.command = cpu_to_le16(command);
286 /* Fill in static fields */
287 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
288 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
290 /* Capability info */
291 tmpcap = (bss->capability & CAPINFO_MASK);
292 if (bss->mode == IW_MODE_INFRA)
293 tmpcap |= WLAN_CAPABILITY_ESS;
294 cmd.capability = cpu_to_le16(tmpcap);
295 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
297 /* SSID */
298 ssid = (struct mrvl_ie_ssid_param_set *) pos;
299 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
300 tmplen = bss->ssid_len;
301 ssid->header.len = cpu_to_le16(tmplen);
302 memcpy(ssid->ssid, bss->ssid, tmplen);
303 pos += sizeof(ssid->header) + tmplen;
305 ds = (struct mrvl_ie_ds_param_set *) pos;
306 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
307 ds->header.len = cpu_to_le16(1);
308 ds->channel = bss->phy.ds.channel;
309 pos += sizeof(ds->header) + 1;
311 cf = (struct mrvl_ie_cf_param_set *) pos;
312 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
313 tmplen = sizeof(*cf) - sizeof (cf->header);
314 cf->header.len = cpu_to_le16(tmplen);
315 /* IE payload should be zeroed, firmware fills it in for us */
316 pos += sizeof(*cf);
318 rates = (struct mrvl_ie_rates_param_set *) pos;
319 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
320 memcpy(&rates->rates, &bss->rates, MAX_RATES);
321 tmplen = min_t(u16, ARRAY_SIZE(rates->rates), MAX_RATES);
322 if (get_common_rates(priv, rates->rates, &tmplen)) {
323 ret = -1;
324 goto done;
326 pos += sizeof(rates->header) + tmplen;
327 rates->header.len = cpu_to_le16(tmplen);
328 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
330 /* Copy the infra. association rates into Current BSS state structure */
331 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
332 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
334 /* Set MSB on basic rates as the firmware requires, but _after_
335 * copying to current bss rates.
337 lbs_set_basic_rate_flags(rates->rates, tmplen);
339 /* Firmware v9+ indicate authentication suites as a TLV */
340 if (priv->fwrelease >= 0x09000000) {
341 auth = (struct mrvl_ie_auth_type *) pos;
342 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
343 auth->header.len = cpu_to_le16(2);
344 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
345 auth->auth = cpu_to_le16(tmpauth);
346 pos += sizeof(auth->header) + 2;
348 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
349 bss->bssid, priv->secinfo.auth_mode);
352 /* WPA/WPA2 IEs */
353 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
354 rsn = (struct mrvl_ie_rsn_param_set *) pos;
355 /* WPA_IE or WPA2_IE */
356 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
357 tmplen = (u16) assoc_req->wpa_ie[1];
358 rsn->header.len = cpu_to_le16(tmplen);
359 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
360 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
361 sizeof(rsn->header) + tmplen);
362 pos += sizeof(rsn->header) + tmplen;
365 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
366 (u16)(pos - (u8 *) &cmd.iebuf));
368 /* update curbssparams */
369 priv->curbssparams.channel = bss->phy.ds.channel;
371 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
372 ret = -1;
373 goto done;
376 ret = lbs_cmd_with_response(priv, command, &cmd);
377 if (ret == 0) {
378 ret = lbs_assoc_post(priv,
379 (struct cmd_ds_802_11_associate_response *) &cmd);
382 done:
383 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
384 return ret;
388 * @brief Associate to a specific BSS discovered in a scan
390 * @param priv A pointer to struct lbs_private structure
391 * @param assoc_req The association request describing the BSS to associate with
393 * @return 0-success, otherwise fail
395 static int lbs_try_associate(struct lbs_private *priv,
396 struct assoc_request *assoc_req)
398 int ret;
399 u8 preamble = RADIO_PREAMBLE_LONG;
401 lbs_deb_enter(LBS_DEB_ASSOC);
403 /* FW v9 and higher indicate authentication suites as a TLV in the
404 * association command, not as a separate authentication command.
406 if (priv->fwrelease < 0x09000000) {
407 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
408 priv->secinfo.auth_mode);
409 if (ret)
410 goto out;
413 /* Use short preamble only when both the BSS and firmware support it */
414 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
415 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
416 preamble = RADIO_PREAMBLE_SHORT;
418 ret = lbs_set_radio(priv, preamble, 1);
419 if (ret)
420 goto out;
422 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
424 out:
425 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
426 return ret;
429 static int lbs_adhoc_post(struct lbs_private *priv,
430 struct cmd_ds_802_11_ad_hoc_result *resp)
432 int ret = 0;
433 u16 command = le16_to_cpu(resp->hdr.command);
434 u16 result = le16_to_cpu(resp->hdr.result);
435 union iwreq_data wrqu;
436 struct bss_descriptor *bss;
437 DECLARE_SSID_BUF(ssid);
439 lbs_deb_enter(LBS_DEB_JOIN);
441 if (!priv->in_progress_assoc_req) {
442 lbs_deb_join("ADHOC_RESP: no in-progress association "
443 "request\n");
444 ret = -1;
445 goto done;
447 bss = &priv->in_progress_assoc_req->bss;
450 * Join result code 0 --> SUCCESS
452 if (result) {
453 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
454 if (priv->connect_status == LBS_CONNECTED)
455 lbs_mac_event_disconnected(priv);
456 ret = -1;
457 goto done;
460 /* Send a Media Connected event, according to the Spec */
461 priv->connect_status = LBS_CONNECTED;
463 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
464 /* Update the created network descriptor with the new BSSID */
465 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
468 /* Set the BSSID from the joined/started descriptor */
469 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
471 /* Set the new SSID to current SSID */
472 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
473 priv->curbssparams.ssid_len = bss->ssid_len;
475 netif_carrier_on(priv->dev);
476 if (!priv->tx_pending_len)
477 netif_wake_queue(priv->dev);
479 memset(&wrqu, 0, sizeof(wrqu));
480 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
481 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
482 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
484 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
485 print_ssid(ssid, bss->ssid, bss->ssid_len),
486 priv->curbssparams.bssid,
487 priv->curbssparams.channel);
489 done:
490 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
491 return ret;
495 * @brief Join an adhoc network found in a previous scan
497 * @param priv A pointer to struct lbs_private structure
498 * @param assoc_req The association request describing the BSS to join
500 * @return 0 on success, error on failure
502 static int lbs_adhoc_join(struct lbs_private *priv,
503 struct assoc_request *assoc_req)
505 struct cmd_ds_802_11_ad_hoc_join cmd;
506 struct bss_descriptor *bss = &assoc_req->bss;
507 u8 preamble = RADIO_PREAMBLE_LONG;
508 DECLARE_SSID_BUF(ssid);
509 u16 ratesize = 0;
510 int ret = 0;
512 lbs_deb_enter(LBS_DEB_ASSOC);
514 lbs_deb_join("current SSID '%s', ssid length %u\n",
515 print_ssid(ssid, priv->curbssparams.ssid,
516 priv->curbssparams.ssid_len),
517 priv->curbssparams.ssid_len);
518 lbs_deb_join("requested ssid '%s', ssid length %u\n",
519 print_ssid(ssid, bss->ssid, bss->ssid_len),
520 bss->ssid_len);
522 /* check if the requested SSID is already joined */
523 if (priv->curbssparams.ssid_len &&
524 !lbs_ssid_cmp(priv->curbssparams.ssid,
525 priv->curbssparams.ssid_len,
526 bss->ssid, bss->ssid_len) &&
527 (priv->mode == IW_MODE_ADHOC) &&
528 (priv->connect_status == LBS_CONNECTED)) {
529 union iwreq_data wrqu;
531 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
532 "current, not attempting to re-join");
534 /* Send the re-association event though, because the association
535 * request really was successful, even if just a null-op.
537 memset(&wrqu, 0, sizeof(wrqu));
538 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
539 ETH_ALEN);
540 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
541 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
542 goto out;
545 /* Use short preamble only when both the BSS and firmware support it */
546 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
547 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
548 lbs_deb_join("AdhocJoin: Short preamble\n");
549 preamble = RADIO_PREAMBLE_SHORT;
552 ret = lbs_set_radio(priv, preamble, 1);
553 if (ret)
554 goto out;
556 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
557 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
559 priv->adhoccreate = 0;
560 priv->curbssparams.channel = bss->channel;
562 /* Build the join command */
563 memset(&cmd, 0, sizeof(cmd));
564 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
566 cmd.bss.type = CMD_BSS_TYPE_IBSS;
567 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
569 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
570 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
572 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
574 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
575 sizeof(struct ieee_ie_ibss_param_set));
577 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
578 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
579 bss->capability, CAPINFO_MASK);
581 /* information on BSSID descriptor passed to FW */
582 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
583 cmd.bss.bssid, cmd.bss.ssid);
585 /* Only v8 and below support setting these */
586 if (priv->fwrelease < 0x09000000) {
587 /* failtimeout */
588 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
589 /* probedelay */
590 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
593 /* Copy Data rates from the rates recorded in scan response */
594 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
595 ratesize = min_t(u16, ARRAY_SIZE(cmd.bss.rates), MAX_RATES);
596 memcpy(cmd.bss.rates, bss->rates, ratesize);
597 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
598 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
599 ret = -1;
600 goto out;
603 /* Copy the ad-hoc creation rates into Current BSS state structure */
604 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
605 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
607 /* Set MSB on basic rates as the firmware requires, but _after_
608 * copying to current bss rates.
610 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
612 cmd.bss.ibss.atimwindow = bss->atimwindow;
614 if (assoc_req->secinfo.wep_enabled) {
615 u16 tmp = le16_to_cpu(cmd.bss.capability);
616 tmp |= WLAN_CAPABILITY_PRIVACY;
617 cmd.bss.capability = cpu_to_le16(tmp);
620 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
621 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
623 /* wake up first */
624 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
625 CMD_ACT_SET, 0, 0,
626 &local_ps_mode);
627 if (ret) {
628 ret = -1;
629 goto out;
633 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
634 ret = -1;
635 goto out;
638 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
639 if (ret == 0) {
640 ret = lbs_adhoc_post(priv,
641 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
644 out:
645 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
646 return ret;
650 * @brief Start an Adhoc Network
652 * @param priv A pointer to struct lbs_private structure
653 * @param assoc_req The association request describing the BSS to start
655 * @return 0 on success, error on failure
657 static int lbs_adhoc_start(struct lbs_private *priv,
658 struct assoc_request *assoc_req)
660 struct cmd_ds_802_11_ad_hoc_start cmd;
661 u8 preamble = RADIO_PREAMBLE_LONG;
662 size_t ratesize = 0;
663 u16 tmpcap = 0;
664 int ret = 0;
665 DECLARE_SSID_BUF(ssid);
667 lbs_deb_enter(LBS_DEB_ASSOC);
669 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
670 lbs_deb_join("ADHOC_START: Will use short preamble\n");
671 preamble = RADIO_PREAMBLE_SHORT;
674 ret = lbs_set_radio(priv, preamble, 1);
675 if (ret)
676 goto out;
678 /* Build the start command */
679 memset(&cmd, 0, sizeof(cmd));
680 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
682 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
684 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
685 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
686 assoc_req->ssid_len);
688 cmd.bsstype = CMD_BSS_TYPE_IBSS;
690 if (priv->beacon_period == 0)
691 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
692 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
694 WARN_ON(!assoc_req->channel);
696 /* set Physical parameter set */
697 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
698 cmd.ds.header.len = 1;
699 cmd.ds.channel = assoc_req->channel;
701 /* set IBSS parameter set */
702 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
703 cmd.ibss.header.len = 2;
704 cmd.ibss.atimwindow = cpu_to_le16(0);
706 /* set capability info */
707 tmpcap = WLAN_CAPABILITY_IBSS;
708 if (assoc_req->secinfo.wep_enabled ||
709 assoc_req->secinfo.WPAenabled ||
710 assoc_req->secinfo.WPA2enabled) {
711 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
712 tmpcap |= WLAN_CAPABILITY_PRIVACY;
713 } else
714 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
716 cmd.capability = cpu_to_le16(tmpcap);
718 /* Only v8 and below support setting probe delay */
719 if (priv->fwrelease < 0x09000000)
720 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
722 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
723 memcpy(cmd.rates, lbs_bg_rates, ratesize);
725 /* Copy the ad-hoc creating rates into Current BSS state structure */
726 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
727 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
729 /* Set MSB on basic rates as the firmware requires, but _after_
730 * copying to current bss rates.
732 lbs_set_basic_rate_flags(cmd.rates, ratesize);
734 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
735 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
737 if (lbs_create_dnld_countryinfo_11d(priv)) {
738 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
739 ret = -1;
740 goto out;
743 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
744 assoc_req->channel, assoc_req->band);
746 priv->adhoccreate = 1;
747 priv->mode = IW_MODE_ADHOC;
749 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
750 if (ret == 0)
751 ret = lbs_adhoc_post(priv,
752 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
754 out:
755 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
756 return ret;
760 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
762 * @param priv A pointer to struct lbs_private structure
763 * @return 0 on success, or an error
765 int lbs_adhoc_stop(struct lbs_private *priv)
767 struct cmd_ds_802_11_ad_hoc_stop cmd;
768 int ret;
770 lbs_deb_enter(LBS_DEB_JOIN);
772 memset(&cmd, 0, sizeof (cmd));
773 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
775 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
777 /* Clean up everything even if there was an error */
778 lbs_mac_event_disconnected(priv);
780 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
781 return ret;
784 static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
785 struct bss_descriptor *match_bss)
787 if (!secinfo->wep_enabled && !secinfo->WPAenabled
788 && !secinfo->WPA2enabled
789 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
790 && match_bss->rsn_ie[0] != WLAN_EID_RSN
791 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
792 return 1;
793 else
794 return 0;
797 static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
798 struct bss_descriptor *match_bss)
800 if (secinfo->wep_enabled && !secinfo->WPAenabled
801 && !secinfo->WPA2enabled
802 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
803 return 1;
804 else
805 return 0;
808 static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
809 struct bss_descriptor *match_bss)
811 if (!secinfo->wep_enabled && secinfo->WPAenabled
812 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
813 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
814 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
816 return 1;
817 else
818 return 0;
821 static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
822 struct bss_descriptor *match_bss)
824 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
825 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
826 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
827 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
829 return 1;
830 else
831 return 0;
834 static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
835 struct bss_descriptor *match_bss)
837 if (!secinfo->wep_enabled && !secinfo->WPAenabled
838 && !secinfo->WPA2enabled
839 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
840 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
841 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
842 return 1;
843 else
844 return 0;
848 * @brief Check if a scanned network compatible with the driver settings
850 * WEP WPA WPA2 ad-hoc encrypt Network
851 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
852 * 0 0 0 0 NONE 0 0 0 yes No security
853 * 1 0 0 0 NONE 1 0 0 yes Static WEP
854 * 0 1 0 0 x 1x 1 x yes WPA
855 * 0 0 1 0 x 1x x 1 yes WPA2
856 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
857 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
860 * @param priv A pointer to struct lbs_private
861 * @param index Index in scantable to check against current driver settings
862 * @param mode Network mode: Infrastructure or IBSS
864 * @return Index in scantable, or error code if negative
866 static int is_network_compatible(struct lbs_private *priv,
867 struct bss_descriptor *bss, uint8_t mode)
869 int matched = 0;
871 lbs_deb_enter(LBS_DEB_SCAN);
873 if (bss->mode != mode)
874 goto done;
876 matched = match_bss_no_security(&priv->secinfo, bss);
877 if (matched)
878 goto done;
879 matched = match_bss_static_wep(&priv->secinfo, bss);
880 if (matched)
881 goto done;
882 matched = match_bss_wpa(&priv->secinfo, bss);
883 if (matched) {
884 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
885 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
886 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
887 priv->secinfo.wep_enabled ? "e" : "d",
888 priv->secinfo.WPAenabled ? "e" : "d",
889 priv->secinfo.WPA2enabled ? "e" : "d",
890 (bss->capability & WLAN_CAPABILITY_PRIVACY));
891 goto done;
893 matched = match_bss_wpa2(&priv->secinfo, bss);
894 if (matched) {
895 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
896 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
897 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
898 priv->secinfo.wep_enabled ? "e" : "d",
899 priv->secinfo.WPAenabled ? "e" : "d",
900 priv->secinfo.WPA2enabled ? "e" : "d",
901 (bss->capability & WLAN_CAPABILITY_PRIVACY));
902 goto done;
904 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
905 if (matched) {
906 lbs_deb_scan("is_network_compatible() dynamic WEP: "
907 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
908 bss->wpa_ie[0], bss->rsn_ie[0],
909 (bss->capability & WLAN_CAPABILITY_PRIVACY));
910 goto done;
913 /* bss security settings don't match those configured on card */
914 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
915 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
916 bss->wpa_ie[0], bss->rsn_ie[0],
917 priv->secinfo.wep_enabled ? "e" : "d",
918 priv->secinfo.WPAenabled ? "e" : "d",
919 priv->secinfo.WPA2enabled ? "e" : "d",
920 (bss->capability & WLAN_CAPABILITY_PRIVACY));
922 done:
923 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
924 return matched;
928 * @brief This function finds a specific compatible BSSID in the scan list
930 * Used in association code
932 * @param priv A pointer to struct lbs_private
933 * @param bssid BSSID to find in the scan list
934 * @param mode Network mode: Infrastructure or IBSS
936 * @return index in BSSID list, or error return code (< 0)
938 static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
939 uint8_t *bssid, uint8_t mode)
941 struct bss_descriptor *iter_bss;
942 struct bss_descriptor *found_bss = NULL;
944 lbs_deb_enter(LBS_DEB_SCAN);
946 if (!bssid)
947 goto out;
949 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
951 /* Look through the scan table for a compatible match. The loop will
952 * continue past a matched bssid that is not compatible in case there
953 * is an AP with multiple SSIDs assigned to the same BSSID
955 mutex_lock(&priv->lock);
956 list_for_each_entry(iter_bss, &priv->network_list, list) {
957 if (compare_ether_addr(iter_bss->bssid, bssid))
958 continue; /* bssid doesn't match */
959 switch (mode) {
960 case IW_MODE_INFRA:
961 case IW_MODE_ADHOC:
962 if (!is_network_compatible(priv, iter_bss, mode))
963 break;
964 found_bss = iter_bss;
965 break;
966 default:
967 found_bss = iter_bss;
968 break;
971 mutex_unlock(&priv->lock);
973 out:
974 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
975 return found_bss;
979 * @brief This function finds ssid in ssid list.
981 * Used in association code
983 * @param priv A pointer to struct lbs_private
984 * @param ssid SSID to find in the list
985 * @param bssid BSSID to qualify the SSID selection (if provided)
986 * @param mode Network mode: Infrastructure or IBSS
988 * @return index in BSSID list
990 static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
991 uint8_t *ssid, uint8_t ssid_len,
992 uint8_t *bssid, uint8_t mode,
993 int channel)
995 u32 bestrssi = 0;
996 struct bss_descriptor *iter_bss = NULL;
997 struct bss_descriptor *found_bss = NULL;
998 struct bss_descriptor *tmp_oldest = NULL;
1000 lbs_deb_enter(LBS_DEB_SCAN);
1002 mutex_lock(&priv->lock);
1004 list_for_each_entry(iter_bss, &priv->network_list, list) {
1005 if (!tmp_oldest ||
1006 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1007 tmp_oldest = iter_bss;
1009 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1010 ssid, ssid_len) != 0)
1011 continue; /* ssid doesn't match */
1012 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1013 continue; /* bssid doesn't match */
1014 if ((channel > 0) && (iter_bss->channel != channel))
1015 continue; /* channel doesn't match */
1017 switch (mode) {
1018 case IW_MODE_INFRA:
1019 case IW_MODE_ADHOC:
1020 if (!is_network_compatible(priv, iter_bss, mode))
1021 break;
1023 if (bssid) {
1024 /* Found requested BSSID */
1025 found_bss = iter_bss;
1026 goto out;
1029 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1030 bestrssi = SCAN_RSSI(iter_bss->rssi);
1031 found_bss = iter_bss;
1033 break;
1034 case IW_MODE_AUTO:
1035 default:
1036 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1037 bestrssi = SCAN_RSSI(iter_bss->rssi);
1038 found_bss = iter_bss;
1040 break;
1044 out:
1045 mutex_unlock(&priv->lock);
1046 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1047 return found_bss;
1050 static int assoc_helper_essid(struct lbs_private *priv,
1051 struct assoc_request * assoc_req)
1053 int ret = 0;
1054 struct bss_descriptor * bss;
1055 int channel = -1;
1056 DECLARE_SSID_BUF(ssid);
1058 lbs_deb_enter(LBS_DEB_ASSOC);
1060 /* FIXME: take channel into account when picking SSIDs if a channel
1061 * is set.
1064 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1065 channel = assoc_req->channel;
1067 lbs_deb_assoc("SSID '%s' requested\n",
1068 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
1069 if (assoc_req->mode == IW_MODE_INFRA) {
1070 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1071 assoc_req->ssid_len);
1073 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1074 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
1075 if (bss != NULL) {
1076 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1077 ret = lbs_try_associate(priv, assoc_req);
1078 } else {
1079 lbs_deb_assoc("SSID not found; cannot associate\n");
1081 } else if (assoc_req->mode == IW_MODE_ADHOC) {
1082 /* Scan for the network, do not save previous results. Stale
1083 * scan data will cause us to join a non-existant adhoc network
1085 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1086 assoc_req->ssid_len);
1088 /* Search for the requested SSID in the scan table */
1089 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1090 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
1091 if (bss != NULL) {
1092 lbs_deb_assoc("SSID found, will join\n");
1093 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1094 lbs_adhoc_join(priv, assoc_req);
1095 } else {
1096 /* else send START command */
1097 lbs_deb_assoc("SSID not found, creating adhoc network\n");
1098 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
1099 IW_ESSID_MAX_SIZE);
1100 assoc_req->bss.ssid_len = assoc_req->ssid_len;
1101 lbs_adhoc_start(priv, assoc_req);
1105 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1106 return ret;
1110 static int assoc_helper_bssid(struct lbs_private *priv,
1111 struct assoc_request * assoc_req)
1113 int ret = 0;
1114 struct bss_descriptor * bss;
1116 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
1118 /* Search for index position in list for requested MAC */
1119 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
1120 assoc_req->mode);
1121 if (bss == NULL) {
1122 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1123 "cannot associate.\n", assoc_req->bssid);
1124 goto out;
1127 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1128 if (assoc_req->mode == IW_MODE_INFRA) {
1129 ret = lbs_try_associate(priv, assoc_req);
1130 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1131 ret);
1132 } else if (assoc_req->mode == IW_MODE_ADHOC) {
1133 lbs_adhoc_join(priv, assoc_req);
1136 out:
1137 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1138 return ret;
1142 static int assoc_helper_associate(struct lbs_private *priv,
1143 struct assoc_request * assoc_req)
1145 int ret = 0, done = 0;
1147 lbs_deb_enter(LBS_DEB_ASSOC);
1149 /* If we're given and 'any' BSSID, try associating based on SSID */
1151 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1152 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1153 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
1154 ret = assoc_helper_bssid(priv, assoc_req);
1155 done = 1;
1159 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1160 ret = assoc_helper_essid(priv, assoc_req);
1163 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1164 return ret;
1168 static int assoc_helper_mode(struct lbs_private *priv,
1169 struct assoc_request * assoc_req)
1171 int ret = 0;
1173 lbs_deb_enter(LBS_DEB_ASSOC);
1175 if (assoc_req->mode == priv->mode)
1176 goto done;
1178 if (assoc_req->mode == IW_MODE_INFRA) {
1179 if (priv->psstate != PS_STATE_FULL_POWER)
1180 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1181 priv->psmode = LBS802_11POWERMODECAM;
1184 priv->mode = assoc_req->mode;
1185 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
1187 done:
1188 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1189 return ret;
1192 static int assoc_helper_channel(struct lbs_private *priv,
1193 struct assoc_request * assoc_req)
1195 int ret = 0;
1197 lbs_deb_enter(LBS_DEB_ASSOC);
1199 ret = lbs_update_channel(priv);
1200 if (ret) {
1201 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1202 goto done;
1205 if (assoc_req->channel == priv->curbssparams.channel)
1206 goto done;
1208 if (priv->mesh_dev) {
1209 /* Change mesh channel first; 21.p21 firmware won't let
1210 you change channel otherwise (even though it'll return
1211 an error to this */
1212 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1213 assoc_req->channel);
1216 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
1217 priv->curbssparams.channel, assoc_req->channel);
1219 ret = lbs_set_channel(priv, assoc_req->channel);
1220 if (ret < 0)
1221 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
1223 /* FIXME: shouldn't need to grab the channel _again_ after setting
1224 * it since the firmware is supposed to return the new channel, but
1225 * whatever... */
1226 ret = lbs_update_channel(priv);
1227 if (ret) {
1228 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1229 goto done;
1232 if (assoc_req->channel != priv->curbssparams.channel) {
1233 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
1234 assoc_req->channel);
1235 goto restore_mesh;
1238 if ( assoc_req->secinfo.wep_enabled
1239 && (assoc_req->wep_keys[0].len
1240 || assoc_req->wep_keys[1].len
1241 || assoc_req->wep_keys[2].len
1242 || assoc_req->wep_keys[3].len)) {
1243 /* Make sure WEP keys are re-sent to firmware */
1244 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1247 /* Must restart/rejoin adhoc networks after channel change */
1248 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
1250 restore_mesh:
1251 if (priv->mesh_dev)
1252 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1253 priv->curbssparams.channel);
1255 done:
1256 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1257 return ret;
1261 static int assoc_helper_wep_keys(struct lbs_private *priv,
1262 struct assoc_request *assoc_req)
1264 int i;
1265 int ret = 0;
1267 lbs_deb_enter(LBS_DEB_ASSOC);
1269 /* Set or remove WEP keys */
1270 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1271 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1272 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1273 else
1274 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
1276 if (ret)
1277 goto out;
1279 /* enable/disable the MAC's WEP packet filter */
1280 if (assoc_req->secinfo.wep_enabled)
1281 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1282 else
1283 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1285 lbs_set_mac_control(priv);
1287 mutex_lock(&priv->lock);
1289 /* Copy WEP keys into priv wep key fields */
1290 for (i = 0; i < 4; i++) {
1291 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
1292 sizeof(struct enc_key));
1294 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
1296 mutex_unlock(&priv->lock);
1298 out:
1299 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1300 return ret;
1303 static int assoc_helper_secinfo(struct lbs_private *priv,
1304 struct assoc_request * assoc_req)
1306 int ret = 0;
1307 uint16_t do_wpa;
1308 uint16_t rsn = 0;
1310 lbs_deb_enter(LBS_DEB_ASSOC);
1312 memcpy(&priv->secinfo, &assoc_req->secinfo,
1313 sizeof(struct lbs_802_11_security));
1315 lbs_set_mac_control(priv);
1317 /* If RSN is already enabled, don't try to enable it again, since
1318 * ENABLE_RSN resets internal state machines and will clobber the
1319 * 4-way WPA handshake.
1322 /* Get RSN enabled/disabled */
1323 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
1324 if (ret) {
1325 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
1326 goto out;
1329 /* Don't re-enable RSN if it's already enabled */
1330 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
1331 if (do_wpa == rsn)
1332 goto out;
1334 /* Set RSN enabled/disabled */
1335 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
1337 out:
1338 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1339 return ret;
1343 static int assoc_helper_wpa_keys(struct lbs_private *priv,
1344 struct assoc_request * assoc_req)
1346 int ret = 0;
1347 unsigned int flags = assoc_req->flags;
1349 lbs_deb_enter(LBS_DEB_ASSOC);
1351 /* Work around older firmware bug where WPA unicast and multicast
1352 * keys must be set independently. Seen in SDIO parts with firmware
1353 * version 5.0.11p0.
1356 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1357 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1358 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1359 assoc_req->flags = flags;
1362 if (ret)
1363 goto out;
1365 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1366 sizeof(struct enc_key));
1368 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1369 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1371 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1372 assoc_req->flags = flags;
1374 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1375 sizeof(struct enc_key));
1378 out:
1379 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1380 return ret;
1384 static int assoc_helper_wpa_ie(struct lbs_private *priv,
1385 struct assoc_request * assoc_req)
1387 int ret = 0;
1389 lbs_deb_enter(LBS_DEB_ASSOC);
1391 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1392 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1393 priv->wpa_ie_len = assoc_req->wpa_ie_len;
1394 } else {
1395 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1396 priv->wpa_ie_len = 0;
1399 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1400 return ret;
1404 static int should_deauth_infrastructure(struct lbs_private *priv,
1405 struct assoc_request * assoc_req)
1407 int ret = 0;
1409 if (priv->connect_status != LBS_CONNECTED)
1410 return 0;
1412 lbs_deb_enter(LBS_DEB_ASSOC);
1413 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1414 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1415 ret = 1;
1416 goto out;
1419 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1420 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
1421 lbs_deb_assoc("Deauthenticating due to new security\n");
1422 ret = 1;
1423 goto out;
1427 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1428 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1429 ret = 1;
1430 goto out;
1433 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1434 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1435 ret = 1;
1436 goto out;
1439 /* FIXME: deal with 'auto' mode somehow */
1440 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1441 if (assoc_req->mode != IW_MODE_INFRA) {
1442 lbs_deb_assoc("Deauthenticating due to leaving "
1443 "infra mode\n");
1444 ret = 1;
1445 goto out;
1449 out:
1450 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1451 return ret;
1455 static int should_stop_adhoc(struct lbs_private *priv,
1456 struct assoc_request * assoc_req)
1458 lbs_deb_enter(LBS_DEB_ASSOC);
1460 if (priv->connect_status != LBS_CONNECTED)
1461 return 0;
1463 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1464 priv->curbssparams.ssid_len,
1465 assoc_req->ssid, assoc_req->ssid_len) != 0)
1466 return 1;
1468 /* FIXME: deal with 'auto' mode somehow */
1469 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1470 if (assoc_req->mode != IW_MODE_ADHOC)
1471 return 1;
1474 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1475 if (assoc_req->channel != priv->curbssparams.channel)
1476 return 1;
1479 lbs_deb_leave(LBS_DEB_ASSOC);
1480 return 0;
1485 * @brief This function finds the best SSID in the Scan List
1487 * Search the scan table for the best SSID that also matches the current
1488 * adapter network preference (infrastructure or adhoc)
1490 * @param priv A pointer to struct lbs_private
1492 * @return index in BSSID list
1494 static struct bss_descriptor *lbs_find_best_ssid_in_list(
1495 struct lbs_private *priv, uint8_t mode)
1497 uint8_t bestrssi = 0;
1498 struct bss_descriptor *iter_bss;
1499 struct bss_descriptor *best_bss = NULL;
1501 lbs_deb_enter(LBS_DEB_SCAN);
1503 mutex_lock(&priv->lock);
1505 list_for_each_entry(iter_bss, &priv->network_list, list) {
1506 switch (mode) {
1507 case IW_MODE_INFRA:
1508 case IW_MODE_ADHOC:
1509 if (!is_network_compatible(priv, iter_bss, mode))
1510 break;
1511 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1512 break;
1513 bestrssi = SCAN_RSSI(iter_bss->rssi);
1514 best_bss = iter_bss;
1515 break;
1516 case IW_MODE_AUTO:
1517 default:
1518 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1519 break;
1520 bestrssi = SCAN_RSSI(iter_bss->rssi);
1521 best_bss = iter_bss;
1522 break;
1526 mutex_unlock(&priv->lock);
1527 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1528 return best_bss;
1532 * @brief Find the best AP
1534 * Used from association worker.
1536 * @param priv A pointer to struct lbs_private structure
1537 * @param pSSID A pointer to AP's ssid
1539 * @return 0--success, otherwise--fail
1541 static int lbs_find_best_network_ssid(struct lbs_private *priv,
1542 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1543 uint8_t *out_mode)
1545 int ret = -1;
1546 struct bss_descriptor *found;
1548 lbs_deb_enter(LBS_DEB_SCAN);
1550 priv->scan_ssid_len = 0;
1551 lbs_scan_networks(priv, 1);
1552 if (priv->surpriseremoved)
1553 goto out;
1555 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1556 if (found && (found->ssid_len > 0)) {
1557 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1558 *out_ssid_len = found->ssid_len;
1559 *out_mode = found->mode;
1560 ret = 0;
1563 out:
1564 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1565 return ret;
1569 void lbs_association_worker(struct work_struct *work)
1571 struct lbs_private *priv = container_of(work, struct lbs_private,
1572 assoc_work.work);
1573 struct assoc_request * assoc_req = NULL;
1574 int ret = 0;
1575 int find_any_ssid = 0;
1576 DECLARE_SSID_BUF(ssid);
1578 lbs_deb_enter(LBS_DEB_ASSOC);
1580 mutex_lock(&priv->lock);
1581 assoc_req = priv->pending_assoc_req;
1582 priv->pending_assoc_req = NULL;
1583 priv->in_progress_assoc_req = assoc_req;
1584 mutex_unlock(&priv->lock);
1586 if (!assoc_req)
1587 goto done;
1589 lbs_deb_assoc(
1590 "Association Request:\n"
1591 " flags: 0x%08lx\n"
1592 " SSID: '%s'\n"
1593 " chann: %d\n"
1594 " band: %d\n"
1595 " mode: %d\n"
1596 " BSSID: %pM\n"
1597 " secinfo: %s%s%s\n"
1598 " auth_mode: %d\n",
1599 assoc_req->flags,
1600 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
1601 assoc_req->channel, assoc_req->band, assoc_req->mode,
1602 assoc_req->bssid,
1603 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1604 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1605 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1606 assoc_req->secinfo.auth_mode);
1608 /* If 'any' SSID was specified, find an SSID to associate with */
1609 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
1610 && !assoc_req->ssid_len)
1611 find_any_ssid = 1;
1613 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1614 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1615 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1616 && compare_ether_addr(assoc_req->bssid, bssid_off))
1617 find_any_ssid = 0;
1620 if (find_any_ssid) {
1621 u8 new_mode = assoc_req->mode;
1623 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
1624 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
1625 if (ret) {
1626 lbs_deb_assoc("Could not find best network\n");
1627 ret = -ENETUNREACH;
1628 goto out;
1631 /* Ensure we switch to the mode of the AP */
1632 if (assoc_req->mode == IW_MODE_AUTO) {
1633 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1634 assoc_req->mode = new_mode;
1639 * Check if the attributes being changing require deauthentication
1640 * from the currently associated infrastructure access point.
1642 if (priv->mode == IW_MODE_INFRA) {
1643 if (should_deauth_infrastructure(priv, assoc_req)) {
1644 ret = lbs_cmd_80211_deauthenticate(priv,
1645 priv->curbssparams.bssid,
1646 WLAN_REASON_DEAUTH_LEAVING);
1647 if (ret) {
1648 lbs_deb_assoc("Deauthentication due to new "
1649 "configuration request failed: %d\n",
1650 ret);
1653 } else if (priv->mode == IW_MODE_ADHOC) {
1654 if (should_stop_adhoc(priv, assoc_req)) {
1655 ret = lbs_adhoc_stop(priv);
1656 if (ret) {
1657 lbs_deb_assoc("Teardown of AdHoc network due to "
1658 "new configuration request failed: %d\n",
1659 ret);
1665 /* Send the various configuration bits to the firmware */
1666 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1667 ret = assoc_helper_mode(priv, assoc_req);
1668 if (ret)
1669 goto out;
1672 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1673 ret = assoc_helper_channel(priv, assoc_req);
1674 if (ret)
1675 goto out;
1678 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1679 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1680 ret = assoc_helper_wep_keys(priv, assoc_req);
1681 if (ret)
1682 goto out;
1685 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1686 ret = assoc_helper_secinfo(priv, assoc_req);
1687 if (ret)
1688 goto out;
1691 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1692 ret = assoc_helper_wpa_ie(priv, assoc_req);
1693 if (ret)
1694 goto out;
1697 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1698 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1699 ret = assoc_helper_wpa_keys(priv, assoc_req);
1700 if (ret)
1701 goto out;
1704 /* SSID/BSSID should be the _last_ config option set, because they
1705 * trigger the association attempt.
1707 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1708 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1709 int success = 1;
1711 ret = assoc_helper_associate(priv, assoc_req);
1712 if (ret) {
1713 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
1714 ret);
1715 success = 0;
1718 if (priv->connect_status != LBS_CONNECTED) {
1719 lbs_deb_assoc("ASSOC: association unsuccessful, "
1720 "not connected\n");
1721 success = 0;
1724 if (success) {
1725 lbs_deb_assoc("associated to %pM\n",
1726 priv->curbssparams.bssid);
1727 lbs_prepare_and_send_command(priv,
1728 CMD_802_11_RSSI,
1729 0, CMD_OPTION_WAITFORRSP, 0, NULL);
1730 } else {
1731 ret = -1;
1735 out:
1736 if (ret) {
1737 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
1738 ret);
1741 mutex_lock(&priv->lock);
1742 priv->in_progress_assoc_req = NULL;
1743 mutex_unlock(&priv->lock);
1744 kfree(assoc_req);
1746 done:
1747 lbs_deb_leave(LBS_DEB_ASSOC);
1752 * Caller MUST hold any necessary locks
1754 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
1756 struct assoc_request * assoc_req;
1758 lbs_deb_enter(LBS_DEB_ASSOC);
1759 if (!priv->pending_assoc_req) {
1760 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
1761 GFP_KERNEL);
1762 if (!priv->pending_assoc_req) {
1763 lbs_pr_info("Not enough memory to allocate association"
1764 " request!\n");
1765 return NULL;
1769 /* Copy current configuration attributes to the association request,
1770 * but don't overwrite any that are already set.
1772 assoc_req = priv->pending_assoc_req;
1773 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1774 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
1775 IW_ESSID_MAX_SIZE);
1776 assoc_req->ssid_len = priv->curbssparams.ssid_len;
1779 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1780 assoc_req->channel = priv->curbssparams.channel;
1782 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
1783 assoc_req->band = priv->curbssparams.band;
1785 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
1786 assoc_req->mode = priv->mode;
1788 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1789 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
1790 ETH_ALEN);
1793 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1794 int i;
1795 for (i = 0; i < 4; i++) {
1796 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
1797 sizeof(struct enc_key));
1801 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
1802 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
1804 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1805 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
1806 sizeof(struct enc_key));
1809 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1810 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
1811 sizeof(struct enc_key));
1814 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1815 memcpy(&assoc_req->secinfo, &priv->secinfo,
1816 sizeof(struct lbs_802_11_security));
1819 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1820 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
1821 MAX_WPA_IE_LEN);
1822 assoc_req->wpa_ie_len = priv->wpa_ie_len;
1825 lbs_deb_leave(LBS_DEB_ASSOC);
1826 return assoc_req;
1831 * @brief Deauthenticate from a specific BSS
1833 * @param priv A pointer to struct lbs_private structure
1834 * @param bssid The specific BSS to deauthenticate from
1835 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1837 * @return 0 on success, error on failure
1839 int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1840 u16 reason)
1842 struct cmd_ds_802_11_deauthenticate cmd;
1843 int ret;
1845 lbs_deb_enter(LBS_DEB_JOIN);
1847 memset(&cmd, 0, sizeof(cmd));
1848 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1849 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1850 cmd.reasoncode = cpu_to_le16(reason);
1852 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1854 /* Clean up everything even if there was an error; can't assume that
1855 * we're still authenticated to the AP after trying to deauth.
1857 lbs_mac_event_disconnected(priv);
1859 lbs_deb_leave(LBS_DEB_JOIN);
1860 return ret;