1 /* Copyright (C) 2006, Red Hat, Inc. */
3 #include <linux/types.h>
4 #include <linux/etherdevice.h>
5 #include <linux/ieee80211.h>
6 #include <linux/if_arp.h>
7 #include <net/lib80211.h>
15 static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
16 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
17 static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
18 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
20 /* 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)
24 #define CAPINFO_MASK (~(0xda00))
28 * @brief This function finds common rates between rates and card rates.
30 * It will fill common rates in rates as output if found.
32 * NOTE: Setting the MSB of the basic rates need to be taken
33 * care, either before or after calling this function
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
39 * @return 0 on success, or -1 on error
41 static int get_common_rates(struct lbs_private *priv,
45 u8 *card_rates = lbs_bg_rates;
46 size_t num_card_rates = sizeof(lbs_bg_rates);
51 tmp = kzalloc((ARRAY_SIZE(lbs_bg_rates) - 1) * (*rates_size - 1),
56 /* For each rate in card_rates that exists in rate1, copy to tmp */
57 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
58 for (j = 0; rates[j] && (j < *rates_size); j++) {
59 if (rates[j] == card_rates[i])
60 tmp[tmp_size++] = card_rates[i];
64 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
65 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
66 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
67 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
69 if (!priv->enablehwauto) {
70 for (i = 0; i < tmp_size; i++) {
71 if (tmp[i] == priv->cur_rate)
74 lbs_pr_alert("Previously set fixed data rate %#x isn't "
75 "compatible with the network.\n", priv->cur_rate);
82 memset(rates, 0, *rates_size);
83 *rates_size = min_t(int, tmp_size, *rates_size);
84 memcpy(rates, tmp, *rates_size);
91 * @brief Sets the MSB on basic rates as the firmware requires
93 * Scan through an array and set the MSB for basic data rates.
95 * @param rates buffer of data rates
96 * @param len size of buffer
98 static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
102 for (i = 0; i < len; i++) {
103 if (rates[i] == 0x02 || rates[i] == 0x04 ||
104 rates[i] == 0x0b || rates[i] == 0x16)
110 static u8 iw_auth_to_ieee_auth(u8 auth)
112 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
114 else if (auth == IW_AUTH_ALG_SHARED_KEY)
116 else if (auth == IW_AUTH_ALG_LEAP)
119 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
124 * @brief This function prepares the authenticate command. AUTHENTICATE only
125 * sets the authentication suite for future associations, as the firmware
126 * handles authentication internally during the ASSOCIATE command.
128 * @param priv A pointer to struct lbs_private structure
129 * @param bssid The peer BSSID with which to authenticate
130 * @param auth The authentication mode to use (from wireless.h)
134 static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
136 struct cmd_ds_802_11_authenticate cmd;
139 lbs_deb_enter(LBS_DEB_JOIN);
141 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
142 memcpy(cmd.bssid, bssid, ETH_ALEN);
144 cmd.authtype = iw_auth_to_ieee_auth(auth);
146 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
148 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
150 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
155 static int lbs_assoc_post(struct lbs_private *priv,
156 struct cmd_ds_802_11_associate_response *resp)
159 union iwreq_data wrqu;
160 struct bss_descriptor *bss;
163 lbs_deb_enter(LBS_DEB_ASSOC);
165 if (!priv->in_progress_assoc_req) {
166 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
170 bss = &priv->in_progress_assoc_req->bss;
173 * Older FW versions map the IEEE 802.11 Status Code in the association
174 * response to the following values returned in resp->statuscode:
176 * IEEE Status Code Marvell Status Code
177 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
178 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
179 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
180 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
181 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
182 * others -> 0x0003 ASSOC_RESULT_REFUSED
184 * Other response codes:
185 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
186 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
187 * association response from the AP)
190 status_code = le16_to_cpu(resp->statuscode);
191 if (priv->fwrelease < 0x09000000) {
192 switch (status_code) {
196 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
199 lbs_deb_assoc("ASSOC_RESP: internal timer "
200 "expired while waiting for the AP\n");
203 lbs_deb_assoc("ASSOC_RESP: association "
207 lbs_deb_assoc("ASSOC_RESP: authentication "
211 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
212 " unknown\n", status_code);
216 /* v9+ returns the AP's association response */
217 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
221 lbs_mac_event_disconnected(priv);
226 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
227 (void *) (resp + sizeof (resp->hdr)),
228 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
230 /* Send a Media Connected event, according to the Spec */
231 priv->connect_status = LBS_CONNECTED;
233 /* Update current SSID and BSSID */
234 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
235 priv->curbssparams.ssid_len = bss->ssid_len;
236 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
238 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
239 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
241 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
242 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
246 netif_carrier_on(priv->dev);
247 if (!priv->tx_pending_len)
248 netif_wake_queue(priv->dev);
250 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
251 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
252 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
255 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
260 * @brief This function prepares an association-class command.
262 * @param priv A pointer to struct lbs_private structure
263 * @param assoc_req The association request describing the BSS to associate
264 * or reassociate with
265 * @param command The actual command, either CMD_802_11_ASSOCIATE or
266 * CMD_802_11_REASSOCIATE
270 static int lbs_associate(struct lbs_private *priv,
271 struct assoc_request *assoc_req,
274 struct cmd_ds_802_11_associate cmd;
276 struct bss_descriptor *bss = &assoc_req->bss;
277 u8 *pos = &(cmd.iebuf[0]);
278 u16 tmpcap, tmplen, tmpauth;
279 struct mrvl_ie_ssid_param_set *ssid;
280 struct mrvl_ie_ds_param_set *ds;
281 struct mrvl_ie_cf_param_set *cf;
282 struct mrvl_ie_rates_param_set *rates;
283 struct mrvl_ie_rsn_param_set *rsn;
284 struct mrvl_ie_auth_type *auth;
286 lbs_deb_enter(LBS_DEB_ASSOC);
288 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
289 (command != CMD_802_11_REASSOCIATE));
291 memset(&cmd, 0, sizeof(cmd));
292 cmd.hdr.command = cpu_to_le16(command);
294 /* Fill in static fields */
295 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
296 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
298 /* Capability info */
299 tmpcap = (bss->capability & CAPINFO_MASK);
300 if (bss->mode == IW_MODE_INFRA)
301 tmpcap |= WLAN_CAPABILITY_ESS;
302 cmd.capability = cpu_to_le16(tmpcap);
303 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
306 ssid = (struct mrvl_ie_ssid_param_set *) pos;
307 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
308 tmplen = bss->ssid_len;
309 ssid->header.len = cpu_to_le16(tmplen);
310 memcpy(ssid->ssid, bss->ssid, tmplen);
311 pos += sizeof(ssid->header) + tmplen;
313 ds = (struct mrvl_ie_ds_param_set *) pos;
314 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
315 ds->header.len = cpu_to_le16(1);
316 ds->channel = bss->phy.ds.channel;
317 pos += sizeof(ds->header) + 1;
319 cf = (struct mrvl_ie_cf_param_set *) pos;
320 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
321 tmplen = sizeof(*cf) - sizeof (cf->header);
322 cf->header.len = cpu_to_le16(tmplen);
323 /* IE payload should be zeroed, firmware fills it in for us */
326 rates = (struct mrvl_ie_rates_param_set *) pos;
327 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
328 memcpy(&rates->rates, &bss->rates, MAX_RATES);
330 if (get_common_rates(priv, rates->rates, &tmplen)) {
334 pos += sizeof(rates->header) + tmplen;
335 rates->header.len = cpu_to_le16(tmplen);
336 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
338 /* Copy the infra. association rates into Current BSS state structure */
339 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
340 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
342 /* Set MSB on basic rates as the firmware requires, but _after_
343 * copying to current bss rates.
345 lbs_set_basic_rate_flags(rates->rates, tmplen);
347 /* Firmware v9+ indicate authentication suites as a TLV */
348 if (priv->fwrelease >= 0x09000000) {
349 auth = (struct mrvl_ie_auth_type *) pos;
350 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
351 auth->header.len = cpu_to_le16(2);
352 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
353 auth->auth = cpu_to_le16(tmpauth);
354 pos += sizeof(auth->header) + 2;
356 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
357 bss->bssid, priv->secinfo.auth_mode);
361 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
362 rsn = (struct mrvl_ie_rsn_param_set *) pos;
363 /* WPA_IE or WPA2_IE */
364 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
365 tmplen = (u16) assoc_req->wpa_ie[1];
366 rsn->header.len = cpu_to_le16(tmplen);
367 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
368 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
369 sizeof(rsn->header) + tmplen);
370 pos += sizeof(rsn->header) + tmplen;
373 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
374 (u16)(pos - (u8 *) &cmd.iebuf));
376 /* update curbssparams */
377 priv->curbssparams.channel = bss->phy.ds.channel;
379 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
384 ret = lbs_cmd_with_response(priv, command, &cmd);
386 ret = lbs_assoc_post(priv,
387 (struct cmd_ds_802_11_associate_response *) &cmd);
391 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
396 * @brief Associate to a specific BSS discovered in a scan
398 * @param priv A pointer to struct lbs_private structure
399 * @param assoc_req The association request describing the BSS to associate with
401 * @return 0-success, otherwise fail
403 static int lbs_try_associate(struct lbs_private *priv,
404 struct assoc_request *assoc_req)
407 u8 preamble = RADIO_PREAMBLE_LONG;
409 lbs_deb_enter(LBS_DEB_ASSOC);
411 /* FW v9 and higher indicate authentication suites as a TLV in the
412 * association command, not as a separate authentication command.
414 if (priv->fwrelease < 0x09000000) {
415 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
416 priv->secinfo.auth_mode);
421 /* Use short preamble only when both the BSS and firmware support it */
422 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
423 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
424 preamble = RADIO_PREAMBLE_SHORT;
426 ret = lbs_set_radio(priv, preamble, 1);
430 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
433 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
437 static int lbs_adhoc_post(struct lbs_private *priv,
438 struct cmd_ds_802_11_ad_hoc_result *resp)
441 u16 command = le16_to_cpu(resp->hdr.command);
442 u16 result = le16_to_cpu(resp->hdr.result);
443 union iwreq_data wrqu;
444 struct bss_descriptor *bss;
445 DECLARE_SSID_BUF(ssid);
447 lbs_deb_enter(LBS_DEB_JOIN);
449 if (!priv->in_progress_assoc_req) {
450 lbs_deb_join("ADHOC_RESP: no in-progress association "
455 bss = &priv->in_progress_assoc_req->bss;
458 * Join result code 0 --> SUCCESS
461 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
462 if (priv->connect_status == LBS_CONNECTED)
463 lbs_mac_event_disconnected(priv);
468 /* Send a Media Connected event, according to the Spec */
469 priv->connect_status = LBS_CONNECTED;
471 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
472 /* Update the created network descriptor with the new BSSID */
473 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
476 /* Set the BSSID from the joined/started descriptor */
477 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
479 /* Set the new SSID to current SSID */
480 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
481 priv->curbssparams.ssid_len = bss->ssid_len;
483 netif_carrier_on(priv->dev);
484 if (!priv->tx_pending_len)
485 netif_wake_queue(priv->dev);
487 memset(&wrqu, 0, sizeof(wrqu));
488 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
489 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
490 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
492 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
493 print_ssid(ssid, bss->ssid, bss->ssid_len),
494 priv->curbssparams.bssid,
495 priv->curbssparams.channel);
498 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
503 * @brief Join an adhoc network found in a previous scan
505 * @param priv A pointer to struct lbs_private structure
506 * @param assoc_req The association request describing the BSS to join
508 * @return 0 on success, error on failure
510 static int lbs_adhoc_join(struct lbs_private *priv,
511 struct assoc_request *assoc_req)
513 struct cmd_ds_802_11_ad_hoc_join cmd;
514 struct bss_descriptor *bss = &assoc_req->bss;
515 u8 preamble = RADIO_PREAMBLE_LONG;
516 DECLARE_SSID_BUF(ssid);
520 lbs_deb_enter(LBS_DEB_ASSOC);
522 lbs_deb_join("current SSID '%s', ssid length %u\n",
523 print_ssid(ssid, priv->curbssparams.ssid,
524 priv->curbssparams.ssid_len),
525 priv->curbssparams.ssid_len);
526 lbs_deb_join("requested ssid '%s', ssid length %u\n",
527 print_ssid(ssid, bss->ssid, bss->ssid_len),
530 /* check if the requested SSID is already joined */
531 if (priv->curbssparams.ssid_len &&
532 !lbs_ssid_cmp(priv->curbssparams.ssid,
533 priv->curbssparams.ssid_len,
534 bss->ssid, bss->ssid_len) &&
535 (priv->mode == IW_MODE_ADHOC) &&
536 (priv->connect_status == LBS_CONNECTED)) {
537 union iwreq_data wrqu;
539 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
540 "current, not attempting to re-join");
542 /* Send the re-association event though, because the association
543 * request really was successful, even if just a null-op.
545 memset(&wrqu, 0, sizeof(wrqu));
546 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
548 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
549 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
553 /* Use short preamble only when both the BSS and firmware support it */
554 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
555 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
556 lbs_deb_join("AdhocJoin: Short preamble\n");
557 preamble = RADIO_PREAMBLE_SHORT;
560 ret = lbs_set_radio(priv, preamble, 1);
564 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
565 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
567 priv->adhoccreate = 0;
568 priv->curbssparams.channel = bss->channel;
570 /* Build the join command */
571 memset(&cmd, 0, sizeof(cmd));
572 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
574 cmd.bss.type = CMD_BSS_TYPE_IBSS;
575 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
577 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
578 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
580 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
582 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
583 sizeof(struct ieee_ie_ibss_param_set));
585 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
586 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
587 bss->capability, CAPINFO_MASK);
589 /* information on BSSID descriptor passed to FW */
590 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
591 cmd.bss.bssid, cmd.bss.ssid);
593 /* Only v8 and below support setting these */
594 if (priv->fwrelease < 0x09000000) {
596 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
598 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
601 /* Copy Data rates from the rates recorded in scan response */
602 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
603 ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
604 memcpy(cmd.bss.rates, bss->rates, ratesize);
605 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
606 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
611 /* Copy the ad-hoc creation rates into Current BSS state structure */
612 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
613 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
615 /* Set MSB on basic rates as the firmware requires, but _after_
616 * copying to current bss rates.
618 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
620 cmd.bss.ibss.atimwindow = bss->atimwindow;
622 if (assoc_req->secinfo.wep_enabled) {
623 u16 tmp = le16_to_cpu(cmd.bss.capability);
624 tmp |= WLAN_CAPABILITY_PRIVACY;
625 cmd.bss.capability = cpu_to_le16(tmp);
628 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
629 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
632 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
641 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
646 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
648 ret = lbs_adhoc_post(priv,
649 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
653 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
658 * @brief Start an Adhoc Network
660 * @param priv A pointer to struct lbs_private structure
661 * @param assoc_req The association request describing the BSS to start
663 * @return 0 on success, error on failure
665 static int lbs_adhoc_start(struct lbs_private *priv,
666 struct assoc_request *assoc_req)
668 struct cmd_ds_802_11_ad_hoc_start cmd;
669 u8 preamble = RADIO_PREAMBLE_LONG;
673 DECLARE_SSID_BUF(ssid);
675 lbs_deb_enter(LBS_DEB_ASSOC);
677 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
678 lbs_deb_join("ADHOC_START: Will use short preamble\n");
679 preamble = RADIO_PREAMBLE_SHORT;
682 ret = lbs_set_radio(priv, preamble, 1);
686 /* Build the start command */
687 memset(&cmd, 0, sizeof(cmd));
688 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
690 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
692 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
693 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
694 assoc_req->ssid_len);
696 cmd.bsstype = CMD_BSS_TYPE_IBSS;
698 if (priv->beacon_period == 0)
699 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
700 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
702 WARN_ON(!assoc_req->channel);
704 /* set Physical parameter set */
705 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
706 cmd.ds.header.len = 1;
707 cmd.ds.channel = assoc_req->channel;
709 /* set IBSS parameter set */
710 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
711 cmd.ibss.header.len = 2;
712 cmd.ibss.atimwindow = cpu_to_le16(0);
714 /* set capability info */
715 tmpcap = WLAN_CAPABILITY_IBSS;
716 if (assoc_req->secinfo.wep_enabled ||
717 assoc_req->secinfo.WPAenabled ||
718 assoc_req->secinfo.WPA2enabled) {
719 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
720 tmpcap |= WLAN_CAPABILITY_PRIVACY;
722 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
724 cmd.capability = cpu_to_le16(tmpcap);
726 /* Only v8 and below support setting probe delay */
727 if (priv->fwrelease < 0x09000000)
728 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
730 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
731 memcpy(cmd.rates, lbs_bg_rates, ratesize);
733 /* Copy the ad-hoc creating rates into Current BSS state structure */
734 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
735 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
737 /* Set MSB on basic rates as the firmware requires, but _after_
738 * copying to current bss rates.
740 lbs_set_basic_rate_flags(cmd.rates, ratesize);
742 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
743 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
745 if (lbs_create_dnld_countryinfo_11d(priv)) {
746 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
751 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
752 assoc_req->channel, assoc_req->band);
754 priv->adhoccreate = 1;
755 priv->mode = IW_MODE_ADHOC;
757 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
759 ret = lbs_adhoc_post(priv,
760 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
763 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
768 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
770 * @param priv A pointer to struct lbs_private structure
771 * @return 0 on success, or an error
773 int lbs_adhoc_stop(struct lbs_private *priv)
775 struct cmd_ds_802_11_ad_hoc_stop cmd;
778 lbs_deb_enter(LBS_DEB_JOIN);
780 memset(&cmd, 0, sizeof (cmd));
781 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
783 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
785 /* Clean up everything even if there was an error */
786 lbs_mac_event_disconnected(priv);
788 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
792 static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
793 struct bss_descriptor *match_bss)
795 if (!secinfo->wep_enabled && !secinfo->WPAenabled
796 && !secinfo->WPA2enabled
797 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
798 && match_bss->rsn_ie[0] != WLAN_EID_RSN
799 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
805 static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
806 struct bss_descriptor *match_bss)
808 if (secinfo->wep_enabled && !secinfo->WPAenabled
809 && !secinfo->WPA2enabled
810 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
816 static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
817 struct bss_descriptor *match_bss)
819 if (!secinfo->wep_enabled && secinfo->WPAenabled
820 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
821 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
822 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
829 static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
830 struct bss_descriptor *match_bss)
832 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
833 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
834 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
835 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
842 static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
843 struct bss_descriptor *match_bss)
845 if (!secinfo->wep_enabled && !secinfo->WPAenabled
846 && !secinfo->WPA2enabled
847 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
848 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
849 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
856 * @brief Check if a scanned network compatible with the driver settings
858 * WEP WPA WPA2 ad-hoc encrypt Network
859 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
860 * 0 0 0 0 NONE 0 0 0 yes No security
861 * 1 0 0 0 NONE 1 0 0 yes Static WEP
862 * 0 1 0 0 x 1x 1 x yes WPA
863 * 0 0 1 0 x 1x x 1 yes WPA2
864 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
865 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
868 * @param priv A pointer to struct lbs_private
869 * @param index Index in scantable to check against current driver settings
870 * @param mode Network mode: Infrastructure or IBSS
872 * @return Index in scantable, or error code if negative
874 static int is_network_compatible(struct lbs_private *priv,
875 struct bss_descriptor *bss, uint8_t mode)
879 lbs_deb_enter(LBS_DEB_SCAN);
881 if (bss->mode != mode)
884 matched = match_bss_no_security(&priv->secinfo, bss);
887 matched = match_bss_static_wep(&priv->secinfo, bss);
890 matched = match_bss_wpa(&priv->secinfo, bss);
892 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
893 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
894 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
895 priv->secinfo.wep_enabled ? "e" : "d",
896 priv->secinfo.WPAenabled ? "e" : "d",
897 priv->secinfo.WPA2enabled ? "e" : "d",
898 (bss->capability & WLAN_CAPABILITY_PRIVACY));
901 matched = match_bss_wpa2(&priv->secinfo, bss);
903 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
904 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
905 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
906 priv->secinfo.wep_enabled ? "e" : "d",
907 priv->secinfo.WPAenabled ? "e" : "d",
908 priv->secinfo.WPA2enabled ? "e" : "d",
909 (bss->capability & WLAN_CAPABILITY_PRIVACY));
912 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
914 lbs_deb_scan("is_network_compatible() dynamic WEP: "
915 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
916 bss->wpa_ie[0], bss->rsn_ie[0],
917 (bss->capability & WLAN_CAPABILITY_PRIVACY));
921 /* bss security settings don't match those configured on card */
922 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
923 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
924 bss->wpa_ie[0], bss->rsn_ie[0],
925 priv->secinfo.wep_enabled ? "e" : "d",
926 priv->secinfo.WPAenabled ? "e" : "d",
927 priv->secinfo.WPA2enabled ? "e" : "d",
928 (bss->capability & WLAN_CAPABILITY_PRIVACY));
931 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
936 * @brief This function finds a specific compatible BSSID in the scan list
938 * Used in association code
940 * @param priv A pointer to struct lbs_private
941 * @param bssid BSSID to find in the scan list
942 * @param mode Network mode: Infrastructure or IBSS
944 * @return index in BSSID list, or error return code (< 0)
946 static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
947 uint8_t *bssid, uint8_t mode)
949 struct bss_descriptor *iter_bss;
950 struct bss_descriptor *found_bss = NULL;
952 lbs_deb_enter(LBS_DEB_SCAN);
957 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
959 /* Look through the scan table for a compatible match. The loop will
960 * continue past a matched bssid that is not compatible in case there
961 * is an AP with multiple SSIDs assigned to the same BSSID
963 mutex_lock(&priv->lock);
964 list_for_each_entry(iter_bss, &priv->network_list, list) {
965 if (compare_ether_addr(iter_bss->bssid, bssid))
966 continue; /* bssid doesn't match */
970 if (!is_network_compatible(priv, iter_bss, mode))
972 found_bss = iter_bss;
975 found_bss = iter_bss;
979 mutex_unlock(&priv->lock);
982 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
987 * @brief This function finds ssid in ssid list.
989 * Used in association code
991 * @param priv A pointer to struct lbs_private
992 * @param ssid SSID to find in the list
993 * @param bssid BSSID to qualify the SSID selection (if provided)
994 * @param mode Network mode: Infrastructure or IBSS
996 * @return index in BSSID list
998 static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
999 uint8_t *ssid, uint8_t ssid_len,
1000 uint8_t *bssid, uint8_t mode,
1004 struct bss_descriptor *iter_bss = NULL;
1005 struct bss_descriptor *found_bss = NULL;
1006 struct bss_descriptor *tmp_oldest = NULL;
1008 lbs_deb_enter(LBS_DEB_SCAN);
1010 mutex_lock(&priv->lock);
1012 list_for_each_entry(iter_bss, &priv->network_list, list) {
1014 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1015 tmp_oldest = iter_bss;
1017 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1018 ssid, ssid_len) != 0)
1019 continue; /* ssid doesn't match */
1020 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1021 continue; /* bssid doesn't match */
1022 if ((channel > 0) && (iter_bss->channel != channel))
1023 continue; /* channel doesn't match */
1028 if (!is_network_compatible(priv, iter_bss, mode))
1032 /* Found requested BSSID */
1033 found_bss = iter_bss;
1037 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1038 bestrssi = SCAN_RSSI(iter_bss->rssi);
1039 found_bss = iter_bss;
1044 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1045 bestrssi = SCAN_RSSI(iter_bss->rssi);
1046 found_bss = iter_bss;
1053 mutex_unlock(&priv->lock);
1054 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1058 static int assoc_helper_essid(struct lbs_private *priv,
1059 struct assoc_request * assoc_req)
1062 struct bss_descriptor * bss;
1064 DECLARE_SSID_BUF(ssid);
1066 lbs_deb_enter(LBS_DEB_ASSOC);
1068 /* FIXME: take channel into account when picking SSIDs if a channel
1072 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1073 channel = assoc_req->channel;
1075 lbs_deb_assoc("SSID '%s' requested\n",
1076 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
1077 if (assoc_req->mode == IW_MODE_INFRA) {
1078 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1079 assoc_req->ssid_len);
1081 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1082 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
1084 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1085 ret = lbs_try_associate(priv, assoc_req);
1087 lbs_deb_assoc("SSID not found; cannot associate\n");
1089 } else if (assoc_req->mode == IW_MODE_ADHOC) {
1090 /* Scan for the network, do not save previous results. Stale
1091 * scan data will cause us to join a non-existant adhoc network
1093 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1094 assoc_req->ssid_len);
1096 /* Search for the requested SSID in the scan table */
1097 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1098 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
1100 lbs_deb_assoc("SSID found, will join\n");
1101 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1102 lbs_adhoc_join(priv, assoc_req);
1104 /* else send START command */
1105 lbs_deb_assoc("SSID not found, creating adhoc network\n");
1106 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
1108 assoc_req->bss.ssid_len = assoc_req->ssid_len;
1109 lbs_adhoc_start(priv, assoc_req);
1113 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1118 static int assoc_helper_bssid(struct lbs_private *priv,
1119 struct assoc_request * assoc_req)
1122 struct bss_descriptor * bss;
1124 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
1126 /* Search for index position in list for requested MAC */
1127 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
1130 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1131 "cannot associate.\n", assoc_req->bssid);
1135 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1136 if (assoc_req->mode == IW_MODE_INFRA) {
1137 ret = lbs_try_associate(priv, assoc_req);
1138 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1140 } else if (assoc_req->mode == IW_MODE_ADHOC) {
1141 lbs_adhoc_join(priv, assoc_req);
1145 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1150 static int assoc_helper_associate(struct lbs_private *priv,
1151 struct assoc_request * assoc_req)
1153 int ret = 0, done = 0;
1155 lbs_deb_enter(LBS_DEB_ASSOC);
1157 /* If we're given and 'any' BSSID, try associating based on SSID */
1159 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1160 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1161 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
1162 ret = assoc_helper_bssid(priv, assoc_req);
1167 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1168 ret = assoc_helper_essid(priv, assoc_req);
1171 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1176 static int assoc_helper_mode(struct lbs_private *priv,
1177 struct assoc_request * assoc_req)
1181 lbs_deb_enter(LBS_DEB_ASSOC);
1183 if (assoc_req->mode == priv->mode)
1186 if (assoc_req->mode == IW_MODE_INFRA) {
1187 if (priv->psstate != PS_STATE_FULL_POWER)
1188 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1189 priv->psmode = LBS802_11POWERMODECAM;
1192 priv->mode = assoc_req->mode;
1193 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
1196 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1200 static int assoc_helper_channel(struct lbs_private *priv,
1201 struct assoc_request * assoc_req)
1205 lbs_deb_enter(LBS_DEB_ASSOC);
1207 ret = lbs_update_channel(priv);
1209 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1213 if (assoc_req->channel == priv->curbssparams.channel)
1216 if (priv->mesh_dev) {
1217 /* Change mesh channel first; 21.p21 firmware won't let
1218 you change channel otherwise (even though it'll return
1220 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1221 assoc_req->channel);
1224 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
1225 priv->curbssparams.channel, assoc_req->channel);
1227 ret = lbs_set_channel(priv, assoc_req->channel);
1229 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
1231 /* FIXME: shouldn't need to grab the channel _again_ after setting
1232 * it since the firmware is supposed to return the new channel, but
1234 ret = lbs_update_channel(priv);
1236 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1240 if (assoc_req->channel != priv->curbssparams.channel) {
1241 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
1242 assoc_req->channel);
1246 if ( assoc_req->secinfo.wep_enabled
1247 && (assoc_req->wep_keys[0].len
1248 || assoc_req->wep_keys[1].len
1249 || assoc_req->wep_keys[2].len
1250 || assoc_req->wep_keys[3].len)) {
1251 /* Make sure WEP keys are re-sent to firmware */
1252 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1255 /* Must restart/rejoin adhoc networks after channel change */
1256 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
1260 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1261 priv->curbssparams.channel);
1264 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1269 static int assoc_helper_wep_keys(struct lbs_private *priv,
1270 struct assoc_request *assoc_req)
1275 lbs_deb_enter(LBS_DEB_ASSOC);
1277 /* Set or remove WEP keys */
1278 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1279 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1280 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1282 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
1287 /* enable/disable the MAC's WEP packet filter */
1288 if (assoc_req->secinfo.wep_enabled)
1289 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1291 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1293 lbs_set_mac_control(priv);
1295 mutex_lock(&priv->lock);
1297 /* Copy WEP keys into priv wep key fields */
1298 for (i = 0; i < 4; i++) {
1299 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
1300 sizeof(struct enc_key));
1302 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
1304 mutex_unlock(&priv->lock);
1307 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1311 static int assoc_helper_secinfo(struct lbs_private *priv,
1312 struct assoc_request * assoc_req)
1318 lbs_deb_enter(LBS_DEB_ASSOC);
1320 memcpy(&priv->secinfo, &assoc_req->secinfo,
1321 sizeof(struct lbs_802_11_security));
1323 lbs_set_mac_control(priv);
1325 /* If RSN is already enabled, don't try to enable it again, since
1326 * ENABLE_RSN resets internal state machines and will clobber the
1327 * 4-way WPA handshake.
1330 /* Get RSN enabled/disabled */
1331 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
1333 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
1337 /* Don't re-enable RSN if it's already enabled */
1338 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
1342 /* Set RSN enabled/disabled */
1343 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
1346 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1351 static int assoc_helper_wpa_keys(struct lbs_private *priv,
1352 struct assoc_request * assoc_req)
1355 unsigned int flags = assoc_req->flags;
1357 lbs_deb_enter(LBS_DEB_ASSOC);
1359 /* Work around older firmware bug where WPA unicast and multicast
1360 * keys must be set independently. Seen in SDIO parts with firmware
1364 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1365 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1366 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1367 assoc_req->flags = flags;
1373 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1374 sizeof(struct enc_key));
1376 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1377 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1379 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1380 assoc_req->flags = flags;
1382 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1383 sizeof(struct enc_key));
1387 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1392 static int assoc_helper_wpa_ie(struct lbs_private *priv,
1393 struct assoc_request * assoc_req)
1397 lbs_deb_enter(LBS_DEB_ASSOC);
1399 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1400 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1401 priv->wpa_ie_len = assoc_req->wpa_ie_len;
1403 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1404 priv->wpa_ie_len = 0;
1407 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1412 static int should_deauth_infrastructure(struct lbs_private *priv,
1413 struct assoc_request * assoc_req)
1417 if (priv->connect_status != LBS_CONNECTED)
1420 lbs_deb_enter(LBS_DEB_ASSOC);
1421 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1422 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1427 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1428 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
1429 lbs_deb_assoc("Deauthenticating due to new security\n");
1435 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1436 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1441 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1442 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1447 /* FIXME: deal with 'auto' mode somehow */
1448 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1449 if (assoc_req->mode != IW_MODE_INFRA) {
1450 lbs_deb_assoc("Deauthenticating due to leaving "
1458 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1463 static int should_stop_adhoc(struct lbs_private *priv,
1464 struct assoc_request * assoc_req)
1466 lbs_deb_enter(LBS_DEB_ASSOC);
1468 if (priv->connect_status != LBS_CONNECTED)
1471 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1472 priv->curbssparams.ssid_len,
1473 assoc_req->ssid, assoc_req->ssid_len) != 0)
1476 /* FIXME: deal with 'auto' mode somehow */
1477 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1478 if (assoc_req->mode != IW_MODE_ADHOC)
1482 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1483 if (assoc_req->channel != priv->curbssparams.channel)
1487 lbs_deb_leave(LBS_DEB_ASSOC);
1493 * @brief This function finds the best SSID in the Scan List
1495 * Search the scan table for the best SSID that also matches the current
1496 * adapter network preference (infrastructure or adhoc)
1498 * @param priv A pointer to struct lbs_private
1500 * @return index in BSSID list
1502 static struct bss_descriptor *lbs_find_best_ssid_in_list(
1503 struct lbs_private *priv, uint8_t mode)
1505 uint8_t bestrssi = 0;
1506 struct bss_descriptor *iter_bss;
1507 struct bss_descriptor *best_bss = NULL;
1509 lbs_deb_enter(LBS_DEB_SCAN);
1511 mutex_lock(&priv->lock);
1513 list_for_each_entry(iter_bss, &priv->network_list, list) {
1517 if (!is_network_compatible(priv, iter_bss, mode))
1519 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1521 bestrssi = SCAN_RSSI(iter_bss->rssi);
1522 best_bss = iter_bss;
1526 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1528 bestrssi = SCAN_RSSI(iter_bss->rssi);
1529 best_bss = iter_bss;
1534 mutex_unlock(&priv->lock);
1535 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1540 * @brief Find the best AP
1542 * Used from association worker.
1544 * @param priv A pointer to struct lbs_private structure
1545 * @param pSSID A pointer to AP's ssid
1547 * @return 0--success, otherwise--fail
1549 static int lbs_find_best_network_ssid(struct lbs_private *priv,
1550 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1554 struct bss_descriptor *found;
1556 lbs_deb_enter(LBS_DEB_SCAN);
1558 priv->scan_ssid_len = 0;
1559 lbs_scan_networks(priv, 1);
1560 if (priv->surpriseremoved)
1563 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1564 if (found && (found->ssid_len > 0)) {
1565 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1566 *out_ssid_len = found->ssid_len;
1567 *out_mode = found->mode;
1572 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1577 void lbs_association_worker(struct work_struct *work)
1579 struct lbs_private *priv = container_of(work, struct lbs_private,
1581 struct assoc_request * assoc_req = NULL;
1583 int find_any_ssid = 0;
1584 DECLARE_SSID_BUF(ssid);
1586 lbs_deb_enter(LBS_DEB_ASSOC);
1588 mutex_lock(&priv->lock);
1589 assoc_req = priv->pending_assoc_req;
1590 priv->pending_assoc_req = NULL;
1591 priv->in_progress_assoc_req = assoc_req;
1592 mutex_unlock(&priv->lock);
1598 "Association Request:\n"
1605 " secinfo: %s%s%s\n"
1608 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
1609 assoc_req->channel, assoc_req->band, assoc_req->mode,
1611 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1612 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1613 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1614 assoc_req->secinfo.auth_mode);
1616 /* If 'any' SSID was specified, find an SSID to associate with */
1617 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
1618 && !assoc_req->ssid_len)
1621 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1622 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1623 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1624 && compare_ether_addr(assoc_req->bssid, bssid_off))
1628 if (find_any_ssid) {
1629 u8 new_mode = assoc_req->mode;
1631 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
1632 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
1634 lbs_deb_assoc("Could not find best network\n");
1639 /* Ensure we switch to the mode of the AP */
1640 if (assoc_req->mode == IW_MODE_AUTO) {
1641 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1642 assoc_req->mode = new_mode;
1647 * Check if the attributes being changing require deauthentication
1648 * from the currently associated infrastructure access point.
1650 if (priv->mode == IW_MODE_INFRA) {
1651 if (should_deauth_infrastructure(priv, assoc_req)) {
1652 ret = lbs_cmd_80211_deauthenticate(priv,
1653 priv->curbssparams.bssid,
1654 WLAN_REASON_DEAUTH_LEAVING);
1656 lbs_deb_assoc("Deauthentication due to new "
1657 "configuration request failed: %d\n",
1661 } else if (priv->mode == IW_MODE_ADHOC) {
1662 if (should_stop_adhoc(priv, assoc_req)) {
1663 ret = lbs_adhoc_stop(priv);
1665 lbs_deb_assoc("Teardown of AdHoc network due to "
1666 "new configuration request failed: %d\n",
1673 /* Send the various configuration bits to the firmware */
1674 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1675 ret = assoc_helper_mode(priv, assoc_req);
1680 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1681 ret = assoc_helper_channel(priv, assoc_req);
1686 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1687 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1688 ret = assoc_helper_wep_keys(priv, assoc_req);
1693 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1694 ret = assoc_helper_secinfo(priv, assoc_req);
1699 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1700 ret = assoc_helper_wpa_ie(priv, assoc_req);
1705 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1706 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1707 ret = assoc_helper_wpa_keys(priv, assoc_req);
1712 /* SSID/BSSID should be the _last_ config option set, because they
1713 * trigger the association attempt.
1715 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1716 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1719 ret = assoc_helper_associate(priv, assoc_req);
1721 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
1726 if (priv->connect_status != LBS_CONNECTED) {
1727 lbs_deb_assoc("ASSOC: association unsuccessful, "
1733 lbs_deb_assoc("associated to %pM\n",
1734 priv->curbssparams.bssid);
1735 lbs_prepare_and_send_command(priv,
1737 0, CMD_OPTION_WAITFORRSP, 0, NULL);
1745 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
1749 mutex_lock(&priv->lock);
1750 priv->in_progress_assoc_req = NULL;
1751 mutex_unlock(&priv->lock);
1755 lbs_deb_leave(LBS_DEB_ASSOC);
1760 * Caller MUST hold any necessary locks
1762 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
1764 struct assoc_request * assoc_req;
1766 lbs_deb_enter(LBS_DEB_ASSOC);
1767 if (!priv->pending_assoc_req) {
1768 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
1770 if (!priv->pending_assoc_req) {
1771 lbs_pr_info("Not enough memory to allocate association"
1777 /* Copy current configuration attributes to the association request,
1778 * but don't overwrite any that are already set.
1780 assoc_req = priv->pending_assoc_req;
1781 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1782 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
1784 assoc_req->ssid_len = priv->curbssparams.ssid_len;
1787 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1788 assoc_req->channel = priv->curbssparams.channel;
1790 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
1791 assoc_req->band = priv->curbssparams.band;
1793 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
1794 assoc_req->mode = priv->mode;
1796 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1797 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
1801 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1803 for (i = 0; i < 4; i++) {
1804 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
1805 sizeof(struct enc_key));
1809 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
1810 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
1812 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1813 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
1814 sizeof(struct enc_key));
1817 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1818 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
1819 sizeof(struct enc_key));
1822 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1823 memcpy(&assoc_req->secinfo, &priv->secinfo,
1824 sizeof(struct lbs_802_11_security));
1827 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1828 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
1830 assoc_req->wpa_ie_len = priv->wpa_ie_len;
1833 lbs_deb_leave(LBS_DEB_ASSOC);
1839 * @brief Deauthenticate from a specific BSS
1841 * @param priv A pointer to struct lbs_private structure
1842 * @param bssid The specific BSS to deauthenticate from
1843 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1845 * @return 0 on success, error on failure
1847 int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1850 struct cmd_ds_802_11_deauthenticate cmd;
1853 lbs_deb_enter(LBS_DEB_JOIN);
1855 memset(&cmd, 0, sizeof(cmd));
1856 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1857 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1858 cmd.reasoncode = cpu_to_le16(reason);
1860 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1862 /* Clean up everything even if there was an error; can't assume that
1863 * we're still authenticated to the AP after trying to deauth.
1865 lbs_mac_event_disconnected(priv);
1867 lbs_deb_leave(LBS_DEB_JOIN);