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>
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))
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,
46 u8 *card_rates = lbs_bg_rates;
48 u8 tmp[(ARRAY_SIZE(lbs_bg_rates) - 1) * (*rates_size - 1)];
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)
70 lbs_pr_alert("Previously set fixed data rate %#x isn't "
71 "compatible with the network.\n", priv->cur_rate);
75 memset(rates, 0, *rates_size);
76 *rates_size = min_t(int, tmp_size, *rates_size);
77 memcpy(rates, tmp, *rates_size);
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)
94 for (i = 0; i < len; i++) {
95 if (rates[i] == 0x02 || rates[i] == 0x04 ||
96 rates[i] == 0x0b || rates[i] == 0x16)
102 static u8 iw_auth_to_ieee_auth(u8 auth)
104 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
106 else if (auth == IW_AUTH_ALG_SHARED_KEY)
108 else if (auth == IW_AUTH_ALG_LEAP)
111 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
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)
126 static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
128 struct cmd_ds_802_11_authenticate cmd;
130 DECLARE_MAC_BUF(mac);
132 lbs_deb_enter(LBS_DEB_JOIN);
134 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
135 memcpy(cmd.bssid, bssid, ETH_ALEN);
137 cmd.authtype = iw_auth_to_ieee_auth(auth);
139 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
140 print_mac(mac, bssid), cmd.authtype);
142 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
144 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
149 static int lbs_assoc_post(struct lbs_private *priv,
150 struct cmd_ds_802_11_associate_response *resp)
153 union iwreq_data wrqu;
154 struct bss_descriptor *bss;
157 lbs_deb_enter(LBS_DEB_ASSOC);
159 if (!priv->in_progress_assoc_req) {
160 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
164 bss = &priv->in_progress_assoc_req->bss;
167 * Older FW versions map the IEEE 802.11 Status Code in the association
168 * response to the following values returned in resp->statuscode:
170 * IEEE Status Code Marvell Status Code
171 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
172 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
173 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
174 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
175 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
176 * others -> 0x0003 ASSOC_RESULT_REFUSED
178 * Other response codes:
179 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
180 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
181 * association response from the AP)
184 status_code = le16_to_cpu(resp->statuscode);
185 if (priv->fwrelease < 0x09000000) {
186 switch (status_code) {
190 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
193 lbs_deb_assoc("ASSOC_RESP: internal timer "
194 "expired while waiting for the AP\n");
197 lbs_deb_assoc("ASSOC_RESP: association "
201 lbs_deb_assoc("ASSOC_RESP: authentication "
205 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
206 " unknown\n", status_code);
210 /* v9+ returns the AP's association response */
211 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
215 lbs_mac_event_disconnected(priv);
220 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
221 (void *) (resp + sizeof (resp->hdr)),
222 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
224 /* Send a Media Connected event, according to the Spec */
225 priv->connect_status = LBS_CONNECTED;
227 /* Update current SSID and BSSID */
228 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
229 priv->curbssparams.ssid_len = bss->ssid_len;
230 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
232 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
233 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
235 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
236 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
240 netif_carrier_on(priv->dev);
241 if (!priv->tx_pending_len)
242 netif_wake_queue(priv->dev);
244 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
245 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
246 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
249 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
254 * @brief This function prepares an association-class command.
256 * @param priv A pointer to struct lbs_private structure
257 * @param assoc_req The association request describing the BSS to associate
258 * or reassociate with
259 * @param command The actual command, either CMD_802_11_ASSOCIATE or
260 * CMD_802_11_REASSOCIATE
264 static int lbs_associate(struct lbs_private *priv,
265 struct assoc_request *assoc_req,
268 struct cmd_ds_802_11_associate cmd;
270 struct bss_descriptor *bss = &assoc_req->bss;
271 u8 *pos = &(cmd.iebuf[0]);
272 u16 tmpcap, tmplen, tmpauth;
273 struct mrvl_ie_ssid_param_set *ssid;
274 struct mrvl_ie_ds_param_set *ds;
275 struct mrvl_ie_cf_param_set *cf;
276 struct mrvl_ie_rates_param_set *rates;
277 struct mrvl_ie_rsn_param_set *rsn;
278 struct mrvl_ie_auth_type *auth;
280 lbs_deb_enter(LBS_DEB_ASSOC);
282 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
283 (command != CMD_802_11_REASSOCIATE));
285 memset(&cmd, 0, sizeof(cmd));
286 cmd.hdr.command = cpu_to_le16(command);
288 /* Fill in static fields */
289 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
290 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
292 /* Capability info */
293 tmpcap = (bss->capability & CAPINFO_MASK);
294 if (bss->mode == IW_MODE_INFRA)
295 tmpcap |= WLAN_CAPABILITY_ESS;
296 cmd.capability = cpu_to_le16(tmpcap);
297 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
300 ssid = (struct mrvl_ie_ssid_param_set *) pos;
301 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
302 tmplen = bss->ssid_len;
303 ssid->header.len = cpu_to_le16(tmplen);
304 memcpy(ssid->ssid, bss->ssid, tmplen);
305 pos += sizeof(ssid->header) + tmplen;
307 ds = (struct mrvl_ie_ds_param_set *) pos;
308 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
309 ds->header.len = cpu_to_le16(1);
310 ds->channel = bss->phy.ds.channel;
311 pos += sizeof(ds->header) + 1;
313 cf = (struct mrvl_ie_cf_param_set *) pos;
314 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
315 tmplen = sizeof(*cf) - sizeof (cf->header);
316 cf->header.len = cpu_to_le16(tmplen);
317 /* IE payload should be zeroed, firmware fills it in for us */
320 rates = (struct mrvl_ie_rates_param_set *) pos;
321 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
322 memcpy(&rates->rates, &bss->rates, MAX_RATES);
323 tmplen = min_t(u16, ARRAY_SIZE(rates->rates), MAX_RATES);
324 if (get_common_rates(priv, rates->rates, &tmplen)) {
328 pos += sizeof(rates->header) + tmplen;
329 rates->header.len = cpu_to_le16(tmplen);
330 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
332 /* Copy the infra. association rates into Current BSS state structure */
333 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
334 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
336 /* Set MSB on basic rates as the firmware requires, but _after_
337 * copying to current bss rates.
339 lbs_set_basic_rate_flags(rates->rates, tmplen);
341 /* Firmware v9+ indicate authentication suites as a TLV */
342 if (priv->fwrelease >= 0x09000000) {
343 DECLARE_MAC_BUF(mac);
345 auth = (struct mrvl_ie_auth_type *) pos;
346 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
347 auth->header.len = cpu_to_le16(2);
348 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
349 auth->auth = cpu_to_le16(tmpauth);
350 pos += sizeof(auth->header) + 2;
352 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
353 print_mac(mac, bss->bssid), priv->secinfo.auth_mode);
357 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
358 rsn = (struct mrvl_ie_rsn_param_set *) pos;
359 /* WPA_IE or WPA2_IE */
360 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
361 tmplen = (u16) assoc_req->wpa_ie[1];
362 rsn->header.len = cpu_to_le16(tmplen);
363 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
364 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
365 sizeof(rsn->header) + tmplen);
366 pos += sizeof(rsn->header) + tmplen;
369 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
370 (u16)(pos - (u8 *) &cmd.iebuf));
372 /* update curbssparams */
373 priv->curbssparams.channel = bss->phy.ds.channel;
375 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
380 ret = lbs_cmd_with_response(priv, command, &cmd);
382 ret = lbs_assoc_post(priv,
383 (struct cmd_ds_802_11_associate_response *) &cmd);
387 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
392 * @brief Associate to a specific BSS discovered in a scan
394 * @param priv A pointer to struct lbs_private structure
395 * @param assoc_req The association request describing the BSS to associate with
397 * @return 0-success, otherwise fail
399 static int lbs_try_associate(struct lbs_private *priv,
400 struct assoc_request *assoc_req)
403 u8 preamble = RADIO_PREAMBLE_LONG;
405 lbs_deb_enter(LBS_DEB_ASSOC);
407 /* FW v9 and higher indicate authentication suites as a TLV in the
408 * association command, not as a separate authentication command.
410 if (priv->fwrelease < 0x09000000) {
411 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
412 priv->secinfo.auth_mode);
417 /* Use short preamble only when both the BSS and firmware support it */
418 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
419 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
420 preamble = RADIO_PREAMBLE_SHORT;
422 ret = lbs_set_radio(priv, preamble, 1);
426 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
429 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
433 static int lbs_adhoc_post(struct lbs_private *priv,
434 struct cmd_ds_802_11_ad_hoc_result *resp)
437 u16 command = le16_to_cpu(resp->hdr.command);
438 u16 result = le16_to_cpu(resp->hdr.result);
439 union iwreq_data wrqu;
440 struct bss_descriptor *bss;
441 DECLARE_SSID_BUF(ssid);
443 lbs_deb_enter(LBS_DEB_JOIN);
445 if (!priv->in_progress_assoc_req) {
446 lbs_deb_join("ADHOC_RESP: no in-progress association "
451 bss = &priv->in_progress_assoc_req->bss;
454 * Join result code 0 --> SUCCESS
457 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
458 if (priv->connect_status == LBS_CONNECTED)
459 lbs_mac_event_disconnected(priv);
464 /* Send a Media Connected event, according to the Spec */
465 priv->connect_status = LBS_CONNECTED;
467 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
468 /* Update the created network descriptor with the new BSSID */
469 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
472 /* Set the BSSID from the joined/started descriptor */
473 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
475 /* Set the new SSID to current SSID */
476 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
477 priv->curbssparams.ssid_len = bss->ssid_len;
479 netif_carrier_on(priv->dev);
480 if (!priv->tx_pending_len)
481 netif_wake_queue(priv->dev);
483 memset(&wrqu, 0, sizeof(wrqu));
484 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
485 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
486 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
488 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
489 print_ssid(ssid, bss->ssid, bss->ssid_len),
490 priv->curbssparams.bssid,
491 priv->curbssparams.channel);
494 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
499 * @brief Join an adhoc network found in a previous scan
501 * @param priv A pointer to struct lbs_private structure
502 * @param assoc_req The association request describing the BSS to join
504 * @return 0 on success, error on failure
506 static int lbs_adhoc_join(struct lbs_private *priv,
507 struct assoc_request *assoc_req)
509 struct cmd_ds_802_11_ad_hoc_join cmd;
510 struct bss_descriptor *bss = &assoc_req->bss;
511 u8 preamble = RADIO_PREAMBLE_LONG;
512 DECLARE_SSID_BUF(ssid);
516 lbs_deb_enter(LBS_DEB_ASSOC);
518 lbs_deb_join("current SSID '%s', ssid length %u\n",
519 print_ssid(ssid, priv->curbssparams.ssid,
520 priv->curbssparams.ssid_len),
521 priv->curbssparams.ssid_len);
522 lbs_deb_join("requested ssid '%s', ssid length %u\n",
523 print_ssid(ssid, bss->ssid, bss->ssid_len),
526 /* check if the requested SSID is already joined */
527 if (priv->curbssparams.ssid_len &&
528 !lbs_ssid_cmp(priv->curbssparams.ssid,
529 priv->curbssparams.ssid_len,
530 bss->ssid, bss->ssid_len) &&
531 (priv->mode == IW_MODE_ADHOC) &&
532 (priv->connect_status == LBS_CONNECTED)) {
533 union iwreq_data wrqu;
535 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
536 "current, not attempting to re-join");
538 /* Send the re-association event though, because the association
539 * request really was successful, even if just a null-op.
541 memset(&wrqu, 0, sizeof(wrqu));
542 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
544 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
545 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
549 /* Use short preamble only when both the BSS and firmware support it */
550 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
551 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
552 lbs_deb_join("AdhocJoin: Short preamble\n");
553 preamble = RADIO_PREAMBLE_SHORT;
556 ret = lbs_set_radio(priv, preamble, 1);
560 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
561 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
563 priv->adhoccreate = 0;
564 priv->curbssparams.channel = bss->channel;
566 /* Build the join command */
567 memset(&cmd, 0, sizeof(cmd));
568 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
570 cmd.bss.type = CMD_BSS_TYPE_IBSS;
571 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
573 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
574 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
576 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
578 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
579 sizeof(struct ieee_ie_ibss_param_set));
581 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
582 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
583 bss->capability, CAPINFO_MASK);
585 /* information on BSSID descriptor passed to FW */
586 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
587 cmd.bss.bssid, cmd.bss.ssid);
589 /* Only v8 and below support setting these */
590 if (priv->fwrelease < 0x09000000) {
592 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
594 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
597 /* Copy Data rates from the rates recorded in scan response */
598 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
599 ratesize = min_t(u16, ARRAY_SIZE(cmd.bss.rates), MAX_RATES);
600 memcpy(cmd.bss.rates, bss->rates, ratesize);
601 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
602 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
607 /* Copy the ad-hoc creation rates into Current BSS state structure */
608 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
609 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
611 /* Set MSB on basic rates as the firmware requires, but _after_
612 * copying to current bss rates.
614 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
616 cmd.bss.ibss.atimwindow = bss->atimwindow;
618 if (assoc_req->secinfo.wep_enabled) {
619 u16 tmp = le16_to_cpu(cmd.bss.capability);
620 tmp |= WLAN_CAPABILITY_PRIVACY;
621 cmd.bss.capability = cpu_to_le16(tmp);
624 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
625 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
628 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
637 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
642 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
644 ret = lbs_adhoc_post(priv,
645 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
649 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
654 * @brief Start an Adhoc Network
656 * @param priv A pointer to struct lbs_private structure
657 * @param assoc_req The association request describing the BSS to start
659 * @return 0 on success, error on failure
661 static int lbs_adhoc_start(struct lbs_private *priv,
662 struct assoc_request *assoc_req)
664 struct cmd_ds_802_11_ad_hoc_start cmd;
665 u8 preamble = RADIO_PREAMBLE_LONG;
669 DECLARE_SSID_BUF(ssid);
671 lbs_deb_enter(LBS_DEB_ASSOC);
673 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
674 lbs_deb_join("ADHOC_START: Will use short preamble\n");
675 preamble = RADIO_PREAMBLE_SHORT;
678 ret = lbs_set_radio(priv, preamble, 1);
682 /* Build the start command */
683 memset(&cmd, 0, sizeof(cmd));
684 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
686 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
688 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
689 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
690 assoc_req->ssid_len);
692 cmd.bsstype = CMD_BSS_TYPE_IBSS;
694 if (priv->beacon_period == 0)
695 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
696 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
698 WARN_ON(!assoc_req->channel);
700 /* set Physical parameter set */
701 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
702 cmd.ds.header.len = 1;
703 cmd.ds.channel = assoc_req->channel;
705 /* set IBSS parameter set */
706 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
707 cmd.ibss.header.len = 2;
708 cmd.ibss.atimwindow = cpu_to_le16(0);
710 /* set capability info */
711 tmpcap = WLAN_CAPABILITY_IBSS;
712 if (assoc_req->secinfo.wep_enabled ||
713 assoc_req->secinfo.WPAenabled ||
714 assoc_req->secinfo.WPA2enabled) {
715 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
716 tmpcap |= WLAN_CAPABILITY_PRIVACY;
718 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
720 cmd.capability = cpu_to_le16(tmpcap);
722 /* Only v8 and below support setting probe delay */
723 if (priv->fwrelease < 0x09000000)
724 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
726 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
727 memcpy(cmd.rates, lbs_bg_rates, ratesize);
729 /* Copy the ad-hoc creating rates into Current BSS state structure */
730 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
731 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
733 /* Set MSB on basic rates as the firmware requires, but _after_
734 * copying to current bss rates.
736 lbs_set_basic_rate_flags(cmd.rates, ratesize);
738 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
739 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
741 if (lbs_create_dnld_countryinfo_11d(priv)) {
742 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
747 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
748 assoc_req->channel, assoc_req->band);
750 priv->adhoccreate = 1;
751 priv->mode = IW_MODE_ADHOC;
753 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
755 ret = lbs_adhoc_post(priv,
756 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
759 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
764 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
766 * @param priv A pointer to struct lbs_private structure
767 * @return 0 on success, or an error
769 int lbs_adhoc_stop(struct lbs_private *priv)
771 struct cmd_ds_802_11_ad_hoc_stop cmd;
774 lbs_deb_enter(LBS_DEB_JOIN);
776 memset(&cmd, 0, sizeof (cmd));
777 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
779 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
781 /* Clean up everything even if there was an error */
782 lbs_mac_event_disconnected(priv);
784 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
788 static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
789 struct bss_descriptor *match_bss)
791 if (!secinfo->wep_enabled && !secinfo->WPAenabled
792 && !secinfo->WPA2enabled
793 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
794 && match_bss->rsn_ie[0] != WLAN_EID_RSN
795 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
801 static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
802 struct bss_descriptor *match_bss)
804 if (secinfo->wep_enabled && !secinfo->WPAenabled
805 && !secinfo->WPA2enabled
806 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
812 static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
813 struct bss_descriptor *match_bss)
815 if (!secinfo->wep_enabled && secinfo->WPAenabled
816 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
817 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
818 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
825 static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
826 struct bss_descriptor *match_bss)
828 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
829 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
830 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
831 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
838 static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
839 struct bss_descriptor *match_bss)
841 if (!secinfo->wep_enabled && !secinfo->WPAenabled
842 && !secinfo->WPA2enabled
843 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
844 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
845 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
852 * @brief Check if a scanned network compatible with the driver settings
854 * WEP WPA WPA2 ad-hoc encrypt Network
855 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
856 * 0 0 0 0 NONE 0 0 0 yes No security
857 * 1 0 0 0 NONE 1 0 0 yes Static WEP
858 * 0 1 0 0 x 1x 1 x yes WPA
859 * 0 0 1 0 x 1x x 1 yes WPA2
860 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
861 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
864 * @param priv A pointer to struct lbs_private
865 * @param index Index in scantable to check against current driver settings
866 * @param mode Network mode: Infrastructure or IBSS
868 * @return Index in scantable, or error code if negative
870 static int is_network_compatible(struct lbs_private *priv,
871 struct bss_descriptor *bss, uint8_t mode)
875 lbs_deb_enter(LBS_DEB_SCAN);
877 if (bss->mode != mode)
880 matched = match_bss_no_security(&priv->secinfo, bss);
883 matched = match_bss_static_wep(&priv->secinfo, bss);
886 matched = match_bss_wpa(&priv->secinfo, bss);
888 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
889 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
890 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
891 priv->secinfo.wep_enabled ? "e" : "d",
892 priv->secinfo.WPAenabled ? "e" : "d",
893 priv->secinfo.WPA2enabled ? "e" : "d",
894 (bss->capability & WLAN_CAPABILITY_PRIVACY));
897 matched = match_bss_wpa2(&priv->secinfo, bss);
899 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
900 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
901 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
902 priv->secinfo.wep_enabled ? "e" : "d",
903 priv->secinfo.WPAenabled ? "e" : "d",
904 priv->secinfo.WPA2enabled ? "e" : "d",
905 (bss->capability & WLAN_CAPABILITY_PRIVACY));
908 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
910 lbs_deb_scan("is_network_compatible() dynamic WEP: "
911 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
912 bss->wpa_ie[0], bss->rsn_ie[0],
913 (bss->capability & WLAN_CAPABILITY_PRIVACY));
917 /* bss security settings don't match those configured on card */
918 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
919 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
920 bss->wpa_ie[0], bss->rsn_ie[0],
921 priv->secinfo.wep_enabled ? "e" : "d",
922 priv->secinfo.WPAenabled ? "e" : "d",
923 priv->secinfo.WPA2enabled ? "e" : "d",
924 (bss->capability & WLAN_CAPABILITY_PRIVACY));
927 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
932 * @brief This function finds a specific compatible BSSID in the scan list
934 * Used in association code
936 * @param priv A pointer to struct lbs_private
937 * @param bssid BSSID to find in the scan list
938 * @param mode Network mode: Infrastructure or IBSS
940 * @return index in BSSID list, or error return code (< 0)
942 static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
943 uint8_t *bssid, uint8_t mode)
945 struct bss_descriptor *iter_bss;
946 struct bss_descriptor *found_bss = NULL;
948 lbs_deb_enter(LBS_DEB_SCAN);
953 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
955 /* Look through the scan table for a compatible match. The loop will
956 * continue past a matched bssid that is not compatible in case there
957 * is an AP with multiple SSIDs assigned to the same BSSID
959 mutex_lock(&priv->lock);
960 list_for_each_entry(iter_bss, &priv->network_list, list) {
961 if (compare_ether_addr(iter_bss->bssid, bssid))
962 continue; /* bssid doesn't match */
966 if (!is_network_compatible(priv, iter_bss, mode))
968 found_bss = iter_bss;
971 found_bss = iter_bss;
975 mutex_unlock(&priv->lock);
978 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
983 * @brief This function finds ssid in ssid list.
985 * Used in association code
987 * @param priv A pointer to struct lbs_private
988 * @param ssid SSID to find in the list
989 * @param bssid BSSID to qualify the SSID selection (if provided)
990 * @param mode Network mode: Infrastructure or IBSS
992 * @return index in BSSID list
994 static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
995 uint8_t *ssid, uint8_t ssid_len,
996 uint8_t *bssid, uint8_t mode,
1000 struct bss_descriptor *iter_bss = NULL;
1001 struct bss_descriptor *found_bss = NULL;
1002 struct bss_descriptor *tmp_oldest = NULL;
1004 lbs_deb_enter(LBS_DEB_SCAN);
1006 mutex_lock(&priv->lock);
1008 list_for_each_entry(iter_bss, &priv->network_list, list) {
1010 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1011 tmp_oldest = iter_bss;
1013 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1014 ssid, ssid_len) != 0)
1015 continue; /* ssid doesn't match */
1016 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1017 continue; /* bssid doesn't match */
1018 if ((channel > 0) && (iter_bss->channel != channel))
1019 continue; /* channel doesn't match */
1024 if (!is_network_compatible(priv, iter_bss, mode))
1028 /* Found requested BSSID */
1029 found_bss = iter_bss;
1033 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1034 bestrssi = SCAN_RSSI(iter_bss->rssi);
1035 found_bss = iter_bss;
1040 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1041 bestrssi = SCAN_RSSI(iter_bss->rssi);
1042 found_bss = iter_bss;
1049 mutex_unlock(&priv->lock);
1050 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1054 static int assoc_helper_essid(struct lbs_private *priv,
1055 struct assoc_request * assoc_req)
1058 struct bss_descriptor * bss;
1060 DECLARE_SSID_BUF(ssid);
1062 lbs_deb_enter(LBS_DEB_ASSOC);
1064 /* FIXME: take channel into account when picking SSIDs if a channel
1068 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1069 channel = assoc_req->channel;
1071 lbs_deb_assoc("SSID '%s' requested\n",
1072 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
1073 if (assoc_req->mode == IW_MODE_INFRA) {
1074 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1075 assoc_req->ssid_len);
1077 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1078 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
1080 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1081 ret = lbs_try_associate(priv, assoc_req);
1083 lbs_deb_assoc("SSID not found; cannot associate\n");
1085 } else if (assoc_req->mode == IW_MODE_ADHOC) {
1086 /* Scan for the network, do not save previous results. Stale
1087 * scan data will cause us to join a non-existant adhoc network
1089 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1090 assoc_req->ssid_len);
1092 /* Search for the requested SSID in the scan table */
1093 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1094 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
1096 lbs_deb_assoc("SSID found, will join\n");
1097 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1098 lbs_adhoc_join(priv, assoc_req);
1100 /* else send START command */
1101 lbs_deb_assoc("SSID not found, creating adhoc network\n");
1102 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
1104 assoc_req->bss.ssid_len = assoc_req->ssid_len;
1105 lbs_adhoc_start(priv, assoc_req);
1109 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1114 static int assoc_helper_bssid(struct lbs_private *priv,
1115 struct assoc_request * assoc_req)
1118 struct bss_descriptor * bss;
1120 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
1122 /* Search for index position in list for requested MAC */
1123 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
1126 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1127 "cannot associate.\n", assoc_req->bssid);
1131 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1132 if (assoc_req->mode == IW_MODE_INFRA) {
1133 ret = lbs_try_associate(priv, assoc_req);
1134 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1136 } else if (assoc_req->mode == IW_MODE_ADHOC) {
1137 lbs_adhoc_join(priv, assoc_req);
1141 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1146 static int assoc_helper_associate(struct lbs_private *priv,
1147 struct assoc_request * assoc_req)
1149 int ret = 0, done = 0;
1151 lbs_deb_enter(LBS_DEB_ASSOC);
1153 /* If we're given and 'any' BSSID, try associating based on SSID */
1155 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1156 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1157 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
1158 ret = assoc_helper_bssid(priv, assoc_req);
1163 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1164 ret = assoc_helper_essid(priv, assoc_req);
1167 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1172 static int assoc_helper_mode(struct lbs_private *priv,
1173 struct assoc_request * assoc_req)
1177 lbs_deb_enter(LBS_DEB_ASSOC);
1179 if (assoc_req->mode == priv->mode)
1182 if (assoc_req->mode == IW_MODE_INFRA) {
1183 if (priv->psstate != PS_STATE_FULL_POWER)
1184 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1185 priv->psmode = LBS802_11POWERMODECAM;
1188 priv->mode = assoc_req->mode;
1189 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
1192 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1196 static int assoc_helper_channel(struct lbs_private *priv,
1197 struct assoc_request * assoc_req)
1201 lbs_deb_enter(LBS_DEB_ASSOC);
1203 ret = lbs_update_channel(priv);
1205 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1209 if (assoc_req->channel == priv->curbssparams.channel)
1212 if (priv->mesh_dev) {
1213 /* Change mesh channel first; 21.p21 firmware won't let
1214 you change channel otherwise (even though it'll return
1216 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1217 assoc_req->channel);
1220 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
1221 priv->curbssparams.channel, assoc_req->channel);
1223 ret = lbs_set_channel(priv, assoc_req->channel);
1225 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
1227 /* FIXME: shouldn't need to grab the channel _again_ after setting
1228 * it since the firmware is supposed to return the new channel, but
1230 ret = lbs_update_channel(priv);
1232 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1236 if (assoc_req->channel != priv->curbssparams.channel) {
1237 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
1238 assoc_req->channel);
1242 if ( assoc_req->secinfo.wep_enabled
1243 && (assoc_req->wep_keys[0].len
1244 || assoc_req->wep_keys[1].len
1245 || assoc_req->wep_keys[2].len
1246 || assoc_req->wep_keys[3].len)) {
1247 /* Make sure WEP keys are re-sent to firmware */
1248 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1251 /* Must restart/rejoin adhoc networks after channel change */
1252 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
1256 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1257 priv->curbssparams.channel);
1260 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1265 static int assoc_helper_wep_keys(struct lbs_private *priv,
1266 struct assoc_request *assoc_req)
1271 lbs_deb_enter(LBS_DEB_ASSOC);
1273 /* Set or remove WEP keys */
1274 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1275 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1276 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1278 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
1283 /* enable/disable the MAC's WEP packet filter */
1284 if (assoc_req->secinfo.wep_enabled)
1285 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1287 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1289 lbs_set_mac_control(priv);
1291 mutex_lock(&priv->lock);
1293 /* Copy WEP keys into priv wep key fields */
1294 for (i = 0; i < 4; i++) {
1295 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
1296 sizeof(struct enc_key));
1298 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
1300 mutex_unlock(&priv->lock);
1303 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1307 static int assoc_helper_secinfo(struct lbs_private *priv,
1308 struct assoc_request * assoc_req)
1314 lbs_deb_enter(LBS_DEB_ASSOC);
1316 memcpy(&priv->secinfo, &assoc_req->secinfo,
1317 sizeof(struct lbs_802_11_security));
1319 lbs_set_mac_control(priv);
1321 /* If RSN is already enabled, don't try to enable it again, since
1322 * ENABLE_RSN resets internal state machines and will clobber the
1323 * 4-way WPA handshake.
1326 /* Get RSN enabled/disabled */
1327 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
1329 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
1333 /* Don't re-enable RSN if it's already enabled */
1334 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
1338 /* Set RSN enabled/disabled */
1339 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
1342 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1347 static int assoc_helper_wpa_keys(struct lbs_private *priv,
1348 struct assoc_request * assoc_req)
1351 unsigned int flags = assoc_req->flags;
1353 lbs_deb_enter(LBS_DEB_ASSOC);
1355 /* Work around older firmware bug where WPA unicast and multicast
1356 * keys must be set independently. Seen in SDIO parts with firmware
1360 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1361 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1362 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1363 assoc_req->flags = flags;
1369 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1370 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1372 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1373 assoc_req->flags = flags;
1377 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1382 static int assoc_helper_wpa_ie(struct lbs_private *priv,
1383 struct assoc_request * assoc_req)
1387 lbs_deb_enter(LBS_DEB_ASSOC);
1389 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1390 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1391 priv->wpa_ie_len = assoc_req->wpa_ie_len;
1393 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1394 priv->wpa_ie_len = 0;
1397 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1402 static int should_deauth_infrastructure(struct lbs_private *priv,
1403 struct assoc_request * assoc_req)
1407 if (priv->connect_status != LBS_CONNECTED)
1410 lbs_deb_enter(LBS_DEB_ASSOC);
1411 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1412 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1417 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1418 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
1419 lbs_deb_assoc("Deauthenticating due to new security\n");
1425 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1426 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1431 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1432 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1437 /* FIXME: deal with 'auto' mode somehow */
1438 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1439 if (assoc_req->mode != IW_MODE_INFRA) {
1440 lbs_deb_assoc("Deauthenticating due to leaving "
1448 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1453 static int should_stop_adhoc(struct lbs_private *priv,
1454 struct assoc_request * assoc_req)
1456 lbs_deb_enter(LBS_DEB_ASSOC);
1458 if (priv->connect_status != LBS_CONNECTED)
1461 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1462 priv->curbssparams.ssid_len,
1463 assoc_req->ssid, assoc_req->ssid_len) != 0)
1466 /* FIXME: deal with 'auto' mode somehow */
1467 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1468 if (assoc_req->mode != IW_MODE_ADHOC)
1472 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1473 if (assoc_req->channel != priv->curbssparams.channel)
1477 lbs_deb_leave(LBS_DEB_ASSOC);
1483 * @brief This function finds the best SSID in the Scan List
1485 * Search the scan table for the best SSID that also matches the current
1486 * adapter network preference (infrastructure or adhoc)
1488 * @param priv A pointer to struct lbs_private
1490 * @return index in BSSID list
1492 static struct bss_descriptor *lbs_find_best_ssid_in_list(
1493 struct lbs_private *priv, uint8_t mode)
1495 uint8_t bestrssi = 0;
1496 struct bss_descriptor *iter_bss;
1497 struct bss_descriptor *best_bss = NULL;
1499 lbs_deb_enter(LBS_DEB_SCAN);
1501 mutex_lock(&priv->lock);
1503 list_for_each_entry(iter_bss, &priv->network_list, list) {
1507 if (!is_network_compatible(priv, iter_bss, mode))
1509 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1511 bestrssi = SCAN_RSSI(iter_bss->rssi);
1512 best_bss = iter_bss;
1516 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1518 bestrssi = SCAN_RSSI(iter_bss->rssi);
1519 best_bss = iter_bss;
1524 mutex_unlock(&priv->lock);
1525 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1530 * @brief Find the best AP
1532 * Used from association worker.
1534 * @param priv A pointer to struct lbs_private structure
1535 * @param pSSID A pointer to AP's ssid
1537 * @return 0--success, otherwise--fail
1539 static int lbs_find_best_network_ssid(struct lbs_private *priv,
1540 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1544 struct bss_descriptor *found;
1546 lbs_deb_enter(LBS_DEB_SCAN);
1548 priv->scan_ssid_len = 0;
1549 lbs_scan_networks(priv, 1);
1550 if (priv->surpriseremoved)
1553 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1554 if (found && (found->ssid_len > 0)) {
1555 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1556 *out_ssid_len = found->ssid_len;
1557 *out_mode = found->mode;
1562 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1567 void lbs_association_worker(struct work_struct *work)
1569 struct lbs_private *priv = container_of(work, struct lbs_private,
1571 struct assoc_request * assoc_req = NULL;
1573 int find_any_ssid = 0;
1574 DECLARE_SSID_BUF(ssid);
1576 lbs_deb_enter(LBS_DEB_ASSOC);
1578 mutex_lock(&priv->lock);
1579 assoc_req = priv->pending_assoc_req;
1580 priv->pending_assoc_req = NULL;
1581 priv->in_progress_assoc_req = assoc_req;
1582 mutex_unlock(&priv->lock);
1588 "Association Request:\n"
1595 " secinfo: %s%s%s\n"
1598 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
1599 assoc_req->channel, assoc_req->band, assoc_req->mode,
1601 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1602 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1603 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1604 assoc_req->secinfo.auth_mode);
1606 /* If 'any' SSID was specified, find an SSID to associate with */
1607 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
1608 && !assoc_req->ssid_len)
1611 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1612 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1613 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1614 && compare_ether_addr(assoc_req->bssid, bssid_off))
1618 if (find_any_ssid) {
1619 u8 new_mode = assoc_req->mode;
1621 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
1622 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
1624 lbs_deb_assoc("Could not find best network\n");
1629 /* Ensure we switch to the mode of the AP */
1630 if (assoc_req->mode == IW_MODE_AUTO) {
1631 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1632 assoc_req->mode = new_mode;
1637 * Check if the attributes being changing require deauthentication
1638 * from the currently associated infrastructure access point.
1640 if (priv->mode == IW_MODE_INFRA) {
1641 if (should_deauth_infrastructure(priv, assoc_req)) {
1642 ret = lbs_cmd_80211_deauthenticate(priv,
1643 priv->curbssparams.bssid,
1644 WLAN_REASON_DEAUTH_LEAVING);
1646 lbs_deb_assoc("Deauthentication due to new "
1647 "configuration request failed: %d\n",
1651 } else if (priv->mode == IW_MODE_ADHOC) {
1652 if (should_stop_adhoc(priv, assoc_req)) {
1653 ret = lbs_adhoc_stop(priv);
1655 lbs_deb_assoc("Teardown of AdHoc network due to "
1656 "new configuration request failed: %d\n",
1663 /* Send the various configuration bits to the firmware */
1664 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1665 ret = assoc_helper_mode(priv, assoc_req);
1670 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1671 ret = assoc_helper_channel(priv, assoc_req);
1676 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1677 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1678 ret = assoc_helper_wep_keys(priv, assoc_req);
1683 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1684 ret = assoc_helper_secinfo(priv, assoc_req);
1689 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1690 ret = assoc_helper_wpa_ie(priv, assoc_req);
1695 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1696 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1697 ret = assoc_helper_wpa_keys(priv, assoc_req);
1702 /* SSID/BSSID should be the _last_ config option set, because they
1703 * trigger the association attempt.
1705 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1706 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1709 ret = assoc_helper_associate(priv, assoc_req);
1711 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
1716 if (priv->connect_status != LBS_CONNECTED) {
1717 lbs_deb_assoc("ASSOC: association unsuccessful, "
1723 lbs_deb_assoc("associated to %pM\n",
1724 priv->curbssparams.bssid);
1725 lbs_prepare_and_send_command(priv,
1727 0, CMD_OPTION_WAITFORRSP, 0, NULL);
1735 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
1739 mutex_lock(&priv->lock);
1740 priv->in_progress_assoc_req = NULL;
1741 mutex_unlock(&priv->lock);
1745 lbs_deb_leave(LBS_DEB_ASSOC);
1750 * Caller MUST hold any necessary locks
1752 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
1754 struct assoc_request * assoc_req;
1756 lbs_deb_enter(LBS_DEB_ASSOC);
1757 if (!priv->pending_assoc_req) {
1758 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
1760 if (!priv->pending_assoc_req) {
1761 lbs_pr_info("Not enough memory to allocate association"
1767 /* Copy current configuration attributes to the association request,
1768 * but don't overwrite any that are already set.
1770 assoc_req = priv->pending_assoc_req;
1771 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1772 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
1774 assoc_req->ssid_len = priv->curbssparams.ssid_len;
1777 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1778 assoc_req->channel = priv->curbssparams.channel;
1780 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
1781 assoc_req->band = priv->curbssparams.band;
1783 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
1784 assoc_req->mode = priv->mode;
1786 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1787 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
1791 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1793 for (i = 0; i < 4; i++) {
1794 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
1795 sizeof(struct enc_key));
1799 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
1800 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
1802 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1803 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
1804 sizeof(struct enc_key));
1807 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1808 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
1809 sizeof(struct enc_key));
1812 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1813 memcpy(&assoc_req->secinfo, &priv->secinfo,
1814 sizeof(struct lbs_802_11_security));
1817 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1818 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
1820 assoc_req->wpa_ie_len = priv->wpa_ie_len;
1823 lbs_deb_leave(LBS_DEB_ASSOC);
1829 * @brief Deauthenticate from a specific BSS
1831 * @param priv A pointer to struct lbs_private structure
1832 * @param bssid The specific BSS to deauthenticate from
1833 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1835 * @return 0 on success, error on failure
1837 int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1840 struct cmd_ds_802_11_deauthenticate cmd;
1843 lbs_deb_enter(LBS_DEB_JOIN);
1845 memset(&cmd, 0, sizeof(cmd));
1846 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1847 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1848 cmd.reasoncode = cpu_to_le16(reason);
1850 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1852 /* Clean up everything even if there was an error; can't assume that
1853 * we're still authenticated to the AP after trying to deauth.
1855 lbs_mac_event_disconnected(priv);
1857 lbs_deb_leave(LBS_DEB_JOIN);