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(MAX_RATES * ARRAY_SIZE(lbs_bg_rates), GFP_KERNEL);
55 /* For each rate in card_rates that exists in rate1, copy to tmp */
56 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
57 for (j = 0; rates[j] && (j < *rates_size); j++) {
58 if (rates[j] == card_rates[i])
59 tmp[tmp_size++] = card_rates[i];
63 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
64 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
65 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
66 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
68 memset(rates, 0, *rates_size);
69 *rates_size = min_t(u16, tmp_size, *rates_size);
70 memcpy(rates, tmp, *rates_size);
72 if (!priv->enablehwauto) {
73 for (i = 0; i < tmp_size; i++) {
74 if (tmp[i] == priv->cur_rate)
78 lbs_pr_alert("Previously set fixed data rate %#x isn't "
79 "compatible with the network.\n",
90 * @brief Sets the MSB on basic rates as the firmware requires
92 * Scan through an array and set the MSB for basic data rates.
94 * @param rates buffer of data rates
95 * @param len size of buffer
97 static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
101 for (i = 0; i < len; i++) {
102 if (rates[i] == 0x02 || rates[i] == 0x04 ||
103 rates[i] == 0x0b || rates[i] == 0x16)
109 static u8 iw_auth_to_ieee_auth(u8 auth)
111 if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
113 else if (auth == IW_AUTH_ALG_SHARED_KEY)
115 else if (auth == IW_AUTH_ALG_LEAP)
118 lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
123 * @brief This function prepares the authenticate command. AUTHENTICATE only
124 * sets the authentication suite for future associations, as the firmware
125 * handles authentication internally during the ASSOCIATE command.
127 * @param priv A pointer to struct lbs_private structure
128 * @param bssid The peer BSSID with which to authenticate
129 * @param auth The authentication mode to use (from wireless.h)
133 static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
135 struct cmd_ds_802_11_authenticate cmd;
138 lbs_deb_enter(LBS_DEB_JOIN);
140 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
141 memcpy(cmd.bssid, bssid, ETH_ALEN);
143 cmd.authtype = iw_auth_to_ieee_auth(auth);
145 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
147 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
149 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
154 static int lbs_assoc_post(struct lbs_private *priv,
155 struct cmd_ds_802_11_associate_response *resp)
158 union iwreq_data wrqu;
159 struct bss_descriptor *bss;
162 lbs_deb_enter(LBS_DEB_ASSOC);
164 if (!priv->in_progress_assoc_req) {
165 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
169 bss = &priv->in_progress_assoc_req->bss;
172 * Older FW versions map the IEEE 802.11 Status Code in the association
173 * response to the following values returned in resp->statuscode:
175 * IEEE Status Code Marvell Status Code
176 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
177 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
178 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
179 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
180 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
181 * others -> 0x0003 ASSOC_RESULT_REFUSED
183 * Other response codes:
184 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
185 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
186 * association response from the AP)
189 status_code = le16_to_cpu(resp->statuscode);
190 if (priv->fwrelease < 0x09000000) {
191 switch (status_code) {
195 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
198 lbs_deb_assoc("ASSOC_RESP: internal timer "
199 "expired while waiting for the AP\n");
202 lbs_deb_assoc("ASSOC_RESP: association "
206 lbs_deb_assoc("ASSOC_RESP: authentication "
210 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
211 " unknown\n", status_code);
215 /* v9+ returns the AP's association response */
216 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
220 lbs_mac_event_disconnected(priv);
225 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
226 (void *) (resp + sizeof (resp->hdr)),
227 le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
229 /* Send a Media Connected event, according to the Spec */
230 priv->connect_status = LBS_CONNECTED;
232 /* Update current SSID and BSSID */
233 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
234 priv->curbssparams.ssid_len = bss->ssid_len;
235 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
237 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
238 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
240 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
241 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
245 netif_carrier_on(priv->dev);
246 if (!priv->tx_pending_len)
247 netif_wake_queue(priv->dev);
249 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
250 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
251 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
254 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
259 * @brief This function prepares an association-class command.
261 * @param priv A pointer to struct lbs_private structure
262 * @param assoc_req The association request describing the BSS to associate
263 * or reassociate with
264 * @param command The actual command, either CMD_802_11_ASSOCIATE or
265 * CMD_802_11_REASSOCIATE
269 static int lbs_associate(struct lbs_private *priv,
270 struct assoc_request *assoc_req,
273 struct cmd_ds_802_11_associate cmd;
275 struct bss_descriptor *bss = &assoc_req->bss;
276 u8 *pos = &(cmd.iebuf[0]);
277 u16 tmpcap, tmplen, tmpauth;
278 struct mrvl_ie_ssid_param_set *ssid;
279 struct mrvl_ie_ds_param_set *ds;
280 struct mrvl_ie_cf_param_set *cf;
281 struct mrvl_ie_rates_param_set *rates;
282 struct mrvl_ie_rsn_param_set *rsn;
283 struct mrvl_ie_auth_type *auth;
285 lbs_deb_enter(LBS_DEB_ASSOC);
287 BUG_ON((command != CMD_802_11_ASSOCIATE) &&
288 (command != CMD_802_11_REASSOCIATE));
290 memset(&cmd, 0, sizeof(cmd));
291 cmd.hdr.command = cpu_to_le16(command);
293 /* Fill in static fields */
294 memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
295 cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
297 /* Capability info */
298 tmpcap = (bss->capability & CAPINFO_MASK);
299 if (bss->mode == IW_MODE_INFRA)
300 tmpcap |= WLAN_CAPABILITY_ESS;
301 cmd.capability = cpu_to_le16(tmpcap);
302 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
305 ssid = (struct mrvl_ie_ssid_param_set *) pos;
306 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
307 tmplen = bss->ssid_len;
308 ssid->header.len = cpu_to_le16(tmplen);
309 memcpy(ssid->ssid, bss->ssid, tmplen);
310 pos += sizeof(ssid->header) + tmplen;
312 ds = (struct mrvl_ie_ds_param_set *) pos;
313 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
314 ds->header.len = cpu_to_le16(1);
315 ds->channel = bss->phy.ds.channel;
316 pos += sizeof(ds->header) + 1;
318 cf = (struct mrvl_ie_cf_param_set *) pos;
319 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
320 tmplen = sizeof(*cf) - sizeof (cf->header);
321 cf->header.len = cpu_to_le16(tmplen);
322 /* IE payload should be zeroed, firmware fills it in for us */
325 rates = (struct mrvl_ie_rates_param_set *) pos;
326 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
328 memcpy(&rates->rates, &bss->rates, tmplen);
329 if (get_common_rates(priv, rates->rates, &tmplen)) {
333 pos += sizeof(rates->header) + tmplen;
334 rates->header.len = cpu_to_le16(tmplen);
335 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
337 /* Copy the infra. association rates into Current BSS state structure */
338 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
339 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
341 /* Set MSB on basic rates as the firmware requires, but _after_
342 * copying to current bss rates.
344 lbs_set_basic_rate_flags(rates->rates, tmplen);
346 /* Firmware v9+ indicate authentication suites as a TLV */
347 if (priv->fwrelease >= 0x09000000) {
348 auth = (struct mrvl_ie_auth_type *) pos;
349 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
350 auth->header.len = cpu_to_le16(2);
351 tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
352 auth->auth = cpu_to_le16(tmpauth);
353 pos += sizeof(auth->header) + 2;
355 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
356 bss->bssid, priv->secinfo.auth_mode);
360 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
361 rsn = (struct mrvl_ie_rsn_param_set *) pos;
362 /* WPA_IE or WPA2_IE */
363 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
364 tmplen = (u16) assoc_req->wpa_ie[1];
365 rsn->header.len = cpu_to_le16(tmplen);
366 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
367 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
368 sizeof(rsn->header) + tmplen);
369 pos += sizeof(rsn->header) + tmplen;
372 cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
373 (u16)(pos - (u8 *) &cmd.iebuf));
375 /* update curbssparams */
376 priv->curbssparams.channel = bss->phy.ds.channel;
378 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
383 ret = lbs_cmd_with_response(priv, command, &cmd);
385 ret = lbs_assoc_post(priv,
386 (struct cmd_ds_802_11_associate_response *) &cmd);
390 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
395 * @brief Associate to a specific BSS discovered in a scan
397 * @param priv A pointer to struct lbs_private structure
398 * @param assoc_req The association request describing the BSS to associate with
400 * @return 0-success, otherwise fail
402 static int lbs_try_associate(struct lbs_private *priv,
403 struct assoc_request *assoc_req)
406 u8 preamble = RADIO_PREAMBLE_LONG;
408 lbs_deb_enter(LBS_DEB_ASSOC);
410 /* FW v9 and higher indicate authentication suites as a TLV in the
411 * association command, not as a separate authentication command.
413 if (priv->fwrelease < 0x09000000) {
414 ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
415 priv->secinfo.auth_mode);
420 /* Use short preamble only when both the BSS and firmware support it */
421 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
422 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
423 preamble = RADIO_PREAMBLE_SHORT;
425 ret = lbs_set_radio(priv, preamble, 1);
429 ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
432 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
436 static int lbs_adhoc_post(struct lbs_private *priv,
437 struct cmd_ds_802_11_ad_hoc_result *resp)
440 u16 command = le16_to_cpu(resp->hdr.command);
441 u16 result = le16_to_cpu(resp->hdr.result);
442 union iwreq_data wrqu;
443 struct bss_descriptor *bss;
444 DECLARE_SSID_BUF(ssid);
446 lbs_deb_enter(LBS_DEB_JOIN);
448 if (!priv->in_progress_assoc_req) {
449 lbs_deb_join("ADHOC_RESP: no in-progress association "
454 bss = &priv->in_progress_assoc_req->bss;
457 * Join result code 0 --> SUCCESS
460 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
461 if (priv->connect_status == LBS_CONNECTED)
462 lbs_mac_event_disconnected(priv);
467 /* Send a Media Connected event, according to the Spec */
468 priv->connect_status = LBS_CONNECTED;
470 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
471 /* Update the created network descriptor with the new BSSID */
472 memcpy(bss->bssid, resp->bssid, ETH_ALEN);
475 /* Set the BSSID from the joined/started descriptor */
476 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
478 /* Set the new SSID to current SSID */
479 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
480 priv->curbssparams.ssid_len = bss->ssid_len;
482 netif_carrier_on(priv->dev);
483 if (!priv->tx_pending_len)
484 netif_wake_queue(priv->dev);
486 memset(&wrqu, 0, sizeof(wrqu));
487 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
488 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
489 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
491 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
492 print_ssid(ssid, bss->ssid, bss->ssid_len),
493 priv->curbssparams.bssid,
494 priv->curbssparams.channel);
497 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
502 * @brief Join an adhoc network found in a previous scan
504 * @param priv A pointer to struct lbs_private structure
505 * @param assoc_req The association request describing the BSS to join
507 * @return 0 on success, error on failure
509 static int lbs_adhoc_join(struct lbs_private *priv,
510 struct assoc_request *assoc_req)
512 struct cmd_ds_802_11_ad_hoc_join cmd;
513 struct bss_descriptor *bss = &assoc_req->bss;
514 u8 preamble = RADIO_PREAMBLE_LONG;
515 DECLARE_SSID_BUF(ssid);
519 lbs_deb_enter(LBS_DEB_ASSOC);
521 lbs_deb_join("current SSID '%s', ssid length %u\n",
522 print_ssid(ssid, priv->curbssparams.ssid,
523 priv->curbssparams.ssid_len),
524 priv->curbssparams.ssid_len);
525 lbs_deb_join("requested ssid '%s', ssid length %u\n",
526 print_ssid(ssid, bss->ssid, bss->ssid_len),
529 /* check if the requested SSID is already joined */
530 if (priv->curbssparams.ssid_len &&
531 !lbs_ssid_cmp(priv->curbssparams.ssid,
532 priv->curbssparams.ssid_len,
533 bss->ssid, bss->ssid_len) &&
534 (priv->mode == IW_MODE_ADHOC) &&
535 (priv->connect_status == LBS_CONNECTED)) {
536 union iwreq_data wrqu;
538 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
539 "current, not attempting to re-join");
541 /* Send the re-association event though, because the association
542 * request really was successful, even if just a null-op.
544 memset(&wrqu, 0, sizeof(wrqu));
545 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
547 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
548 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
552 /* Use short preamble only when both the BSS and firmware support it */
553 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
554 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
555 lbs_deb_join("AdhocJoin: Short preamble\n");
556 preamble = RADIO_PREAMBLE_SHORT;
559 ret = lbs_set_radio(priv, preamble, 1);
563 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
564 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
566 priv->adhoccreate = 0;
567 priv->curbssparams.channel = bss->channel;
569 /* Build the join command */
570 memset(&cmd, 0, sizeof(cmd));
571 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
573 cmd.bss.type = CMD_BSS_TYPE_IBSS;
574 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
576 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
577 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
579 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
581 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
582 sizeof(struct ieee_ie_ibss_param_set));
584 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
585 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
586 bss->capability, CAPINFO_MASK);
588 /* information on BSSID descriptor passed to FW */
589 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
590 cmd.bss.bssid, cmd.bss.ssid);
592 /* Only v8 and below support setting these */
593 if (priv->fwrelease < 0x09000000) {
595 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
597 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
600 /* Copy Data rates from the rates recorded in scan response */
601 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
602 ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
603 memcpy(cmd.bss.rates, bss->rates, ratesize);
604 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
605 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
610 /* Copy the ad-hoc creation rates into Current BSS state structure */
611 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
612 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
614 /* Set MSB on basic rates as the firmware requires, but _after_
615 * copying to current bss rates.
617 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
619 cmd.bss.ibss.atimwindow = bss->atimwindow;
621 if (assoc_req->secinfo.wep_enabled) {
622 u16 tmp = le16_to_cpu(cmd.bss.capability);
623 tmp |= WLAN_CAPABILITY_PRIVACY;
624 cmd.bss.capability = cpu_to_le16(tmp);
627 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
628 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
631 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
640 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
645 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
647 ret = lbs_adhoc_post(priv,
648 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
652 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
657 * @brief Start an Adhoc Network
659 * @param priv A pointer to struct lbs_private structure
660 * @param assoc_req The association request describing the BSS to start
662 * @return 0 on success, error on failure
664 static int lbs_adhoc_start(struct lbs_private *priv,
665 struct assoc_request *assoc_req)
667 struct cmd_ds_802_11_ad_hoc_start cmd;
668 u8 preamble = RADIO_PREAMBLE_LONG;
672 DECLARE_SSID_BUF(ssid);
674 lbs_deb_enter(LBS_DEB_ASSOC);
676 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
677 lbs_deb_join("ADHOC_START: Will use short preamble\n");
678 preamble = RADIO_PREAMBLE_SHORT;
681 ret = lbs_set_radio(priv, preamble, 1);
685 /* Build the start command */
686 memset(&cmd, 0, sizeof(cmd));
687 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
689 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
691 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
692 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
693 assoc_req->ssid_len);
695 cmd.bsstype = CMD_BSS_TYPE_IBSS;
697 if (priv->beacon_period == 0)
698 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
699 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
701 WARN_ON(!assoc_req->channel);
703 /* set Physical parameter set */
704 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
705 cmd.ds.header.len = 1;
706 cmd.ds.channel = assoc_req->channel;
708 /* set IBSS parameter set */
709 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
710 cmd.ibss.header.len = 2;
711 cmd.ibss.atimwindow = cpu_to_le16(0);
713 /* set capability info */
714 tmpcap = WLAN_CAPABILITY_IBSS;
715 if (assoc_req->secinfo.wep_enabled ||
716 assoc_req->secinfo.WPAenabled ||
717 assoc_req->secinfo.WPA2enabled) {
718 lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
719 tmpcap |= WLAN_CAPABILITY_PRIVACY;
721 lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
723 cmd.capability = cpu_to_le16(tmpcap);
725 /* Only v8 and below support setting probe delay */
726 if (priv->fwrelease < 0x09000000)
727 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
729 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
730 memcpy(cmd.rates, lbs_bg_rates, ratesize);
732 /* Copy the ad-hoc creating rates into Current BSS state structure */
733 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
734 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
736 /* Set MSB on basic rates as the firmware requires, but _after_
737 * copying to current bss rates.
739 lbs_set_basic_rate_flags(cmd.rates, ratesize);
741 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
742 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
744 if (lbs_create_dnld_countryinfo_11d(priv)) {
745 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
750 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
751 assoc_req->channel, assoc_req->band);
753 priv->adhoccreate = 1;
754 priv->mode = IW_MODE_ADHOC;
756 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
758 ret = lbs_adhoc_post(priv,
759 (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
762 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
767 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
769 * @param priv A pointer to struct lbs_private structure
770 * @return 0 on success, or an error
772 int lbs_adhoc_stop(struct lbs_private *priv)
774 struct cmd_ds_802_11_ad_hoc_stop cmd;
777 lbs_deb_enter(LBS_DEB_JOIN);
779 memset(&cmd, 0, sizeof (cmd));
780 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
782 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
784 /* Clean up everything even if there was an error */
785 lbs_mac_event_disconnected(priv);
787 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
791 static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
792 struct bss_descriptor *match_bss)
794 if (!secinfo->wep_enabled && !secinfo->WPAenabled
795 && !secinfo->WPA2enabled
796 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
797 && match_bss->rsn_ie[0] != WLAN_EID_RSN
798 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
804 static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
805 struct bss_descriptor *match_bss)
807 if (secinfo->wep_enabled && !secinfo->WPAenabled
808 && !secinfo->WPA2enabled
809 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
815 static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
816 struct bss_descriptor *match_bss)
818 if (!secinfo->wep_enabled && secinfo->WPAenabled
819 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
820 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
821 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
828 static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
829 struct bss_descriptor *match_bss)
831 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
832 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
833 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
834 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
841 static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
842 struct bss_descriptor *match_bss)
844 if (!secinfo->wep_enabled && !secinfo->WPAenabled
845 && !secinfo->WPA2enabled
846 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
847 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
848 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
855 * @brief Check if a scanned network compatible with the driver settings
857 * WEP WPA WPA2 ad-hoc encrypt Network
858 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
859 * 0 0 0 0 NONE 0 0 0 yes No security
860 * 1 0 0 0 NONE 1 0 0 yes Static WEP
861 * 0 1 0 0 x 1x 1 x yes WPA
862 * 0 0 1 0 x 1x x 1 yes WPA2
863 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
864 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
867 * @param priv A pointer to struct lbs_private
868 * @param index Index in scantable to check against current driver settings
869 * @param mode Network mode: Infrastructure or IBSS
871 * @return Index in scantable, or error code if negative
873 static int is_network_compatible(struct lbs_private *priv,
874 struct bss_descriptor *bss, uint8_t mode)
878 lbs_deb_enter(LBS_DEB_SCAN);
880 if (bss->mode != mode)
883 matched = match_bss_no_security(&priv->secinfo, bss);
886 matched = match_bss_static_wep(&priv->secinfo, bss);
889 matched = match_bss_wpa(&priv->secinfo, bss);
891 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
892 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
893 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
894 priv->secinfo.wep_enabled ? "e" : "d",
895 priv->secinfo.WPAenabled ? "e" : "d",
896 priv->secinfo.WPA2enabled ? "e" : "d",
897 (bss->capability & WLAN_CAPABILITY_PRIVACY));
900 matched = match_bss_wpa2(&priv->secinfo, bss);
902 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
903 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
904 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
905 priv->secinfo.wep_enabled ? "e" : "d",
906 priv->secinfo.WPAenabled ? "e" : "d",
907 priv->secinfo.WPA2enabled ? "e" : "d",
908 (bss->capability & WLAN_CAPABILITY_PRIVACY));
911 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
913 lbs_deb_scan("is_network_compatible() dynamic WEP: "
914 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
915 bss->wpa_ie[0], bss->rsn_ie[0],
916 (bss->capability & WLAN_CAPABILITY_PRIVACY));
920 /* bss security settings don't match those configured on card */
921 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
922 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
923 bss->wpa_ie[0], bss->rsn_ie[0],
924 priv->secinfo.wep_enabled ? "e" : "d",
925 priv->secinfo.WPAenabled ? "e" : "d",
926 priv->secinfo.WPA2enabled ? "e" : "d",
927 (bss->capability & WLAN_CAPABILITY_PRIVACY));
930 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
935 * @brief This function finds a specific compatible BSSID in the scan list
937 * Used in association code
939 * @param priv A pointer to struct lbs_private
940 * @param bssid BSSID to find in the scan list
941 * @param mode Network mode: Infrastructure or IBSS
943 * @return index in BSSID list, or error return code (< 0)
945 static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
946 uint8_t *bssid, uint8_t mode)
948 struct bss_descriptor *iter_bss;
949 struct bss_descriptor *found_bss = NULL;
951 lbs_deb_enter(LBS_DEB_SCAN);
956 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
958 /* Look through the scan table for a compatible match. The loop will
959 * continue past a matched bssid that is not compatible in case there
960 * is an AP with multiple SSIDs assigned to the same BSSID
962 mutex_lock(&priv->lock);
963 list_for_each_entry(iter_bss, &priv->network_list, list) {
964 if (compare_ether_addr(iter_bss->bssid, bssid))
965 continue; /* bssid doesn't match */
969 if (!is_network_compatible(priv, iter_bss, mode))
971 found_bss = iter_bss;
974 found_bss = iter_bss;
978 mutex_unlock(&priv->lock);
981 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
986 * @brief This function finds ssid in ssid list.
988 * Used in association code
990 * @param priv A pointer to struct lbs_private
991 * @param ssid SSID to find in the list
992 * @param bssid BSSID to qualify the SSID selection (if provided)
993 * @param mode Network mode: Infrastructure or IBSS
995 * @return index in BSSID list
997 static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
998 uint8_t *ssid, uint8_t ssid_len,
999 uint8_t *bssid, uint8_t mode,
1003 struct bss_descriptor *iter_bss = NULL;
1004 struct bss_descriptor *found_bss = NULL;
1005 struct bss_descriptor *tmp_oldest = NULL;
1007 lbs_deb_enter(LBS_DEB_SCAN);
1009 mutex_lock(&priv->lock);
1011 list_for_each_entry(iter_bss, &priv->network_list, list) {
1013 (iter_bss->last_scanned < tmp_oldest->last_scanned))
1014 tmp_oldest = iter_bss;
1016 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1017 ssid, ssid_len) != 0)
1018 continue; /* ssid doesn't match */
1019 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1020 continue; /* bssid doesn't match */
1021 if ((channel > 0) && (iter_bss->channel != channel))
1022 continue; /* channel doesn't match */
1027 if (!is_network_compatible(priv, iter_bss, mode))
1031 /* Found requested BSSID */
1032 found_bss = iter_bss;
1036 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1037 bestrssi = SCAN_RSSI(iter_bss->rssi);
1038 found_bss = iter_bss;
1043 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1044 bestrssi = SCAN_RSSI(iter_bss->rssi);
1045 found_bss = iter_bss;
1052 mutex_unlock(&priv->lock);
1053 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
1057 static int assoc_helper_essid(struct lbs_private *priv,
1058 struct assoc_request * assoc_req)
1061 struct bss_descriptor * bss;
1063 DECLARE_SSID_BUF(ssid);
1065 lbs_deb_enter(LBS_DEB_ASSOC);
1067 /* FIXME: take channel into account when picking SSIDs if a channel
1071 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1072 channel = assoc_req->channel;
1074 lbs_deb_assoc("SSID '%s' requested\n",
1075 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
1076 if (assoc_req->mode == IW_MODE_INFRA) {
1077 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1078 assoc_req->ssid_len);
1080 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1081 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
1083 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1084 ret = lbs_try_associate(priv, assoc_req);
1086 lbs_deb_assoc("SSID not found; cannot associate\n");
1088 } else if (assoc_req->mode == IW_MODE_ADHOC) {
1089 /* Scan for the network, do not save previous results. Stale
1090 * scan data will cause us to join a non-existant adhoc network
1092 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1093 assoc_req->ssid_len);
1095 /* Search for the requested SSID in the scan table */
1096 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1097 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
1099 lbs_deb_assoc("SSID found, will join\n");
1100 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1101 lbs_adhoc_join(priv, assoc_req);
1103 /* else send START command */
1104 lbs_deb_assoc("SSID not found, creating adhoc network\n");
1105 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
1107 assoc_req->bss.ssid_len = assoc_req->ssid_len;
1108 lbs_adhoc_start(priv, assoc_req);
1112 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1117 static int assoc_helper_bssid(struct lbs_private *priv,
1118 struct assoc_request * assoc_req)
1121 struct bss_descriptor * bss;
1123 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
1125 /* Search for index position in list for requested MAC */
1126 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
1129 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
1130 "cannot associate.\n", assoc_req->bssid);
1134 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1135 if (assoc_req->mode == IW_MODE_INFRA) {
1136 ret = lbs_try_associate(priv, assoc_req);
1137 lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
1139 } else if (assoc_req->mode == IW_MODE_ADHOC) {
1140 lbs_adhoc_join(priv, assoc_req);
1144 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1149 static int assoc_helper_associate(struct lbs_private *priv,
1150 struct assoc_request * assoc_req)
1152 int ret = 0, done = 0;
1154 lbs_deb_enter(LBS_DEB_ASSOC);
1156 /* If we're given and 'any' BSSID, try associating based on SSID */
1158 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1159 if (compare_ether_addr(bssid_any, assoc_req->bssid)
1160 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
1161 ret = assoc_helper_bssid(priv, assoc_req);
1166 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1167 ret = assoc_helper_essid(priv, assoc_req);
1170 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1175 static int assoc_helper_mode(struct lbs_private *priv,
1176 struct assoc_request * assoc_req)
1180 lbs_deb_enter(LBS_DEB_ASSOC);
1182 if (assoc_req->mode == priv->mode)
1185 if (assoc_req->mode == IW_MODE_INFRA) {
1186 if (priv->psstate != PS_STATE_FULL_POWER)
1187 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1188 priv->psmode = LBS802_11POWERMODECAM;
1191 priv->mode = assoc_req->mode;
1192 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
1195 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1199 static int assoc_helper_channel(struct lbs_private *priv,
1200 struct assoc_request * assoc_req)
1204 lbs_deb_enter(LBS_DEB_ASSOC);
1206 ret = lbs_update_channel(priv);
1208 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1212 if (assoc_req->channel == priv->curbssparams.channel)
1215 if (priv->mesh_dev) {
1216 /* Change mesh channel first; 21.p21 firmware won't let
1217 you change channel otherwise (even though it'll return
1219 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
1220 assoc_req->channel);
1223 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
1224 priv->curbssparams.channel, assoc_req->channel);
1226 ret = lbs_set_channel(priv, assoc_req->channel);
1228 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
1230 /* FIXME: shouldn't need to grab the channel _again_ after setting
1231 * it since the firmware is supposed to return the new channel, but
1233 ret = lbs_update_channel(priv);
1235 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1239 if (assoc_req->channel != priv->curbssparams.channel) {
1240 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
1241 assoc_req->channel);
1245 if ( assoc_req->secinfo.wep_enabled
1246 && (assoc_req->wep_keys[0].len
1247 || assoc_req->wep_keys[1].len
1248 || assoc_req->wep_keys[2].len
1249 || assoc_req->wep_keys[3].len)) {
1250 /* Make sure WEP keys are re-sent to firmware */
1251 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
1254 /* Must restart/rejoin adhoc networks after channel change */
1255 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
1259 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1260 priv->curbssparams.channel);
1263 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1268 static int assoc_helper_wep_keys(struct lbs_private *priv,
1269 struct assoc_request *assoc_req)
1274 lbs_deb_enter(LBS_DEB_ASSOC);
1276 /* Set or remove WEP keys */
1277 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
1278 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
1279 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
1281 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
1286 /* enable/disable the MAC's WEP packet filter */
1287 if (assoc_req->secinfo.wep_enabled)
1288 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1290 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1292 lbs_set_mac_control(priv);
1294 mutex_lock(&priv->lock);
1296 /* Copy WEP keys into priv wep key fields */
1297 for (i = 0; i < 4; i++) {
1298 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
1299 sizeof(struct enc_key));
1301 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
1303 mutex_unlock(&priv->lock);
1306 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1310 static int assoc_helper_secinfo(struct lbs_private *priv,
1311 struct assoc_request * assoc_req)
1317 lbs_deb_enter(LBS_DEB_ASSOC);
1319 memcpy(&priv->secinfo, &assoc_req->secinfo,
1320 sizeof(struct lbs_802_11_security));
1322 lbs_set_mac_control(priv);
1324 /* If RSN is already enabled, don't try to enable it again, since
1325 * ENABLE_RSN resets internal state machines and will clobber the
1326 * 4-way WPA handshake.
1329 /* Get RSN enabled/disabled */
1330 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
1332 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
1336 /* Don't re-enable RSN if it's already enabled */
1337 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
1341 /* Set RSN enabled/disabled */
1342 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
1345 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1350 static int assoc_helper_wpa_keys(struct lbs_private *priv,
1351 struct assoc_request * assoc_req)
1354 unsigned int flags = assoc_req->flags;
1356 lbs_deb_enter(LBS_DEB_ASSOC);
1358 /* Work around older firmware bug where WPA unicast and multicast
1359 * keys must be set independently. Seen in SDIO parts with firmware
1363 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1364 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1365 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1366 assoc_req->flags = flags;
1372 memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
1373 sizeof(struct enc_key));
1375 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1376 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1378 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1379 assoc_req->flags = flags;
1381 memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
1382 sizeof(struct enc_key));
1386 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1391 static int assoc_helper_wpa_ie(struct lbs_private *priv,
1392 struct assoc_request * assoc_req)
1396 lbs_deb_enter(LBS_DEB_ASSOC);
1398 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1399 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1400 priv->wpa_ie_len = assoc_req->wpa_ie_len;
1402 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1403 priv->wpa_ie_len = 0;
1406 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1411 static int should_deauth_infrastructure(struct lbs_private *priv,
1412 struct assoc_request * assoc_req)
1416 if (priv->connect_status != LBS_CONNECTED)
1419 lbs_deb_enter(LBS_DEB_ASSOC);
1420 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1421 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1426 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1427 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
1428 lbs_deb_assoc("Deauthenticating due to new security\n");
1434 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1435 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1440 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1441 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1446 /* FIXME: deal with 'auto' mode somehow */
1447 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1448 if (assoc_req->mode != IW_MODE_INFRA) {
1449 lbs_deb_assoc("Deauthenticating due to leaving "
1457 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1462 static int should_stop_adhoc(struct lbs_private *priv,
1463 struct assoc_request * assoc_req)
1465 lbs_deb_enter(LBS_DEB_ASSOC);
1467 if (priv->connect_status != LBS_CONNECTED)
1470 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1471 priv->curbssparams.ssid_len,
1472 assoc_req->ssid, assoc_req->ssid_len) != 0)
1475 /* FIXME: deal with 'auto' mode somehow */
1476 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1477 if (assoc_req->mode != IW_MODE_ADHOC)
1481 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1482 if (assoc_req->channel != priv->curbssparams.channel)
1486 lbs_deb_leave(LBS_DEB_ASSOC);
1492 * @brief This function finds the best SSID in the Scan List
1494 * Search the scan table for the best SSID that also matches the current
1495 * adapter network preference (infrastructure or adhoc)
1497 * @param priv A pointer to struct lbs_private
1499 * @return index in BSSID list
1501 static struct bss_descriptor *lbs_find_best_ssid_in_list(
1502 struct lbs_private *priv, uint8_t mode)
1504 uint8_t bestrssi = 0;
1505 struct bss_descriptor *iter_bss;
1506 struct bss_descriptor *best_bss = NULL;
1508 lbs_deb_enter(LBS_DEB_SCAN);
1510 mutex_lock(&priv->lock);
1512 list_for_each_entry(iter_bss, &priv->network_list, list) {
1516 if (!is_network_compatible(priv, iter_bss, mode))
1518 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1520 bestrssi = SCAN_RSSI(iter_bss->rssi);
1521 best_bss = iter_bss;
1525 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1527 bestrssi = SCAN_RSSI(iter_bss->rssi);
1528 best_bss = iter_bss;
1533 mutex_unlock(&priv->lock);
1534 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1539 * @brief Find the best AP
1541 * Used from association worker.
1543 * @param priv A pointer to struct lbs_private structure
1544 * @param pSSID A pointer to AP's ssid
1546 * @return 0--success, otherwise--fail
1548 static int lbs_find_best_network_ssid(struct lbs_private *priv,
1549 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1553 struct bss_descriptor *found;
1555 lbs_deb_enter(LBS_DEB_SCAN);
1557 priv->scan_ssid_len = 0;
1558 lbs_scan_networks(priv, 1);
1559 if (priv->surpriseremoved)
1562 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1563 if (found && (found->ssid_len > 0)) {
1564 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1565 *out_ssid_len = found->ssid_len;
1566 *out_mode = found->mode;
1571 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1576 void lbs_association_worker(struct work_struct *work)
1578 struct lbs_private *priv = container_of(work, struct lbs_private,
1580 struct assoc_request * assoc_req = NULL;
1582 int find_any_ssid = 0;
1583 DECLARE_SSID_BUF(ssid);
1585 lbs_deb_enter(LBS_DEB_ASSOC);
1587 mutex_lock(&priv->lock);
1588 assoc_req = priv->pending_assoc_req;
1589 priv->pending_assoc_req = NULL;
1590 priv->in_progress_assoc_req = assoc_req;
1591 mutex_unlock(&priv->lock);
1597 "Association Request:\n"
1604 " secinfo: %s%s%s\n"
1607 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
1608 assoc_req->channel, assoc_req->band, assoc_req->mode,
1610 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1611 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1612 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1613 assoc_req->secinfo.auth_mode);
1615 /* If 'any' SSID was specified, find an SSID to associate with */
1616 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
1617 && !assoc_req->ssid_len)
1620 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1621 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1622 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1623 && compare_ether_addr(assoc_req->bssid, bssid_off))
1627 if (find_any_ssid) {
1628 u8 new_mode = assoc_req->mode;
1630 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
1631 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
1633 lbs_deb_assoc("Could not find best network\n");
1638 /* Ensure we switch to the mode of the AP */
1639 if (assoc_req->mode == IW_MODE_AUTO) {
1640 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1641 assoc_req->mode = new_mode;
1646 * Check if the attributes being changing require deauthentication
1647 * from the currently associated infrastructure access point.
1649 if (priv->mode == IW_MODE_INFRA) {
1650 if (should_deauth_infrastructure(priv, assoc_req)) {
1651 ret = lbs_cmd_80211_deauthenticate(priv,
1652 priv->curbssparams.bssid,
1653 WLAN_REASON_DEAUTH_LEAVING);
1655 lbs_deb_assoc("Deauthentication due to new "
1656 "configuration request failed: %d\n",
1660 } else if (priv->mode == IW_MODE_ADHOC) {
1661 if (should_stop_adhoc(priv, assoc_req)) {
1662 ret = lbs_adhoc_stop(priv);
1664 lbs_deb_assoc("Teardown of AdHoc network due to "
1665 "new configuration request failed: %d\n",
1672 /* Send the various configuration bits to the firmware */
1673 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1674 ret = assoc_helper_mode(priv, assoc_req);
1679 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1680 ret = assoc_helper_channel(priv, assoc_req);
1685 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1686 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1687 ret = assoc_helper_wep_keys(priv, assoc_req);
1692 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1693 ret = assoc_helper_secinfo(priv, assoc_req);
1698 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1699 ret = assoc_helper_wpa_ie(priv, assoc_req);
1704 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1705 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1706 ret = assoc_helper_wpa_keys(priv, assoc_req);
1711 /* SSID/BSSID should be the _last_ config option set, because they
1712 * trigger the association attempt.
1714 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1715 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1718 ret = assoc_helper_associate(priv, assoc_req);
1720 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
1725 if (priv->connect_status != LBS_CONNECTED) {
1726 lbs_deb_assoc("ASSOC: association unsuccessful, "
1732 lbs_deb_assoc("associated to %pM\n",
1733 priv->curbssparams.bssid);
1734 lbs_prepare_and_send_command(priv,
1736 0, CMD_OPTION_WAITFORRSP, 0, NULL);
1744 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
1748 mutex_lock(&priv->lock);
1749 priv->in_progress_assoc_req = NULL;
1750 mutex_unlock(&priv->lock);
1754 lbs_deb_leave(LBS_DEB_ASSOC);
1759 * Caller MUST hold any necessary locks
1761 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
1763 struct assoc_request * assoc_req;
1765 lbs_deb_enter(LBS_DEB_ASSOC);
1766 if (!priv->pending_assoc_req) {
1767 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
1769 if (!priv->pending_assoc_req) {
1770 lbs_pr_info("Not enough memory to allocate association"
1776 /* Copy current configuration attributes to the association request,
1777 * but don't overwrite any that are already set.
1779 assoc_req = priv->pending_assoc_req;
1780 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1781 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
1783 assoc_req->ssid_len = priv->curbssparams.ssid_len;
1786 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1787 assoc_req->channel = priv->curbssparams.channel;
1789 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
1790 assoc_req->band = priv->curbssparams.band;
1792 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
1793 assoc_req->mode = priv->mode;
1795 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1796 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
1800 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1802 for (i = 0; i < 4; i++) {
1803 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
1804 sizeof(struct enc_key));
1808 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
1809 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
1811 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1812 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
1813 sizeof(struct enc_key));
1816 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1817 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
1818 sizeof(struct enc_key));
1821 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1822 memcpy(&assoc_req->secinfo, &priv->secinfo,
1823 sizeof(struct lbs_802_11_security));
1826 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1827 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
1829 assoc_req->wpa_ie_len = priv->wpa_ie_len;
1832 lbs_deb_leave(LBS_DEB_ASSOC);
1838 * @brief Deauthenticate from a specific BSS
1840 * @param priv A pointer to struct lbs_private structure
1841 * @param bssid The specific BSS to deauthenticate from
1842 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1844 * @return 0 on success, error on failure
1846 int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1849 struct cmd_ds_802_11_deauthenticate cmd;
1852 lbs_deb_enter(LBS_DEB_JOIN);
1854 memset(&cmd, 0, sizeof(cmd));
1855 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1856 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1857 cmd.reasoncode = cpu_to_le16(reason);
1859 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1861 /* Clean up everything even if there was an error; can't assume that
1862 * we're still authenticated to the AP after trying to deauth.
1864 lbs_mac_event_disconnected(priv);
1866 lbs_deb_leave(LBS_DEB_JOIN);