1 /* Copyright (C) 2006, Red Hat, Inc. */
3 #include <linux/types.h>
4 #include <linux/etherdevice.h>
5 #include <net/lib80211.h>
13 static int lbs_adhoc_post(struct lbs_private *priv, struct cmd_header *resp);
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 certain bits masked out of the beacon-derviced capability
21 * field when associating/joining to BSSs.
23 #define CAPINFO_MASK (~(0xda00))
27 * @brief This function finds common rates between rates and card rates.
29 * It will fill common rates in rates as output if found.
31 * NOTE: Setting the MSB of the basic rates need to be taken
32 * care, either before or after calling this function
34 * @param priv A pointer to struct lbs_private structure
35 * @param rates the buffer which keeps input and output
36 * @param rates_size the size of rate1 buffer; new size of buffer on return
38 * @return 0 on success, or -1 on error
40 static int get_common_rates(struct lbs_private *priv,
44 u8 *card_rates = lbs_bg_rates;
45 size_t num_card_rates = sizeof(lbs_bg_rates);
50 /* For each rate in card_rates that exists in rate1, copy to tmp */
51 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
52 for (j = 0; rates[j] && (j < *rates_size); j++) {
53 if (rates[j] == card_rates[i])
54 tmp[tmp_size++] = card_rates[i];
58 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
59 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
60 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
61 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
63 if (!priv->enablehwauto) {
64 for (i = 0; i < tmp_size; i++) {
65 if (tmp[i] == priv->cur_rate)
68 lbs_pr_alert("Previously set fixed data rate %#x isn't "
69 "compatible with the network.\n", priv->cur_rate);
76 memset(rates, 0, *rates_size);
77 *rates_size = min_t(int, tmp_size, *rates_size);
78 memcpy(rates, tmp, *rates_size);
84 * @brief Sets the MSB on basic rates as the firmware requires
86 * Scan through an array and set the MSB for basic data rates.
88 * @param rates buffer of data rates
89 * @param len size of buffer
91 static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
95 for (i = 0; i < len; i++) {
96 if (rates[i] == 0x02 || rates[i] == 0x04 ||
97 rates[i] == 0x0b || rates[i] == 0x16)
104 * @brief Associate to a specific BSS discovered in a scan
106 * @param priv A pointer to struct lbs_private structure
107 * @param assoc_req The association request describing the BSS to associate with
109 * @return 0-success, otherwise fail
111 static int lbs_associate(struct lbs_private *priv,
112 struct assoc_request *assoc_req)
115 u8 preamble = RADIO_PREAMBLE_LONG;
117 lbs_deb_enter(LBS_DEB_ASSOC);
119 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
120 0, CMD_OPTION_WAITFORRSP,
121 0, assoc_req->bss.bssid);
125 /* Use short preamble only when both the BSS and firmware support it */
126 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
127 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
128 preamble = RADIO_PREAMBLE_SHORT;
130 ret = lbs_set_radio(priv, preamble, 1);
134 ret = lbs_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
135 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
138 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
143 * @brief Join an adhoc network found in a previous scan
145 * @param priv A pointer to struct lbs_private structure
146 * @param assoc_req The association request describing the BSS to join
148 * @return 0 on success, error on failure
150 static int lbs_adhoc_join(struct lbs_private *priv,
151 struct assoc_request *assoc_req)
153 struct cmd_ds_802_11_ad_hoc_join cmd;
154 struct bss_descriptor *bss = &assoc_req->bss;
155 u8 preamble = RADIO_PREAMBLE_LONG;
156 DECLARE_SSID_BUF(ssid);
160 lbs_deb_enter(LBS_DEB_ASSOC);
162 lbs_deb_join("current SSID '%s', ssid length %u\n",
163 print_ssid(ssid, priv->curbssparams.ssid,
164 priv->curbssparams.ssid_len),
165 priv->curbssparams.ssid_len);
166 lbs_deb_join("requested ssid '%s', ssid length %u\n",
167 print_ssid(ssid, bss->ssid, bss->ssid_len),
170 /* check if the requested SSID is already joined */
171 if (priv->curbssparams.ssid_len &&
172 !lbs_ssid_cmp(priv->curbssparams.ssid,
173 priv->curbssparams.ssid_len,
174 bss->ssid, bss->ssid_len) &&
175 (priv->mode == IW_MODE_ADHOC) &&
176 (priv->connect_status == LBS_CONNECTED)) {
177 union iwreq_data wrqu;
179 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
180 "current, not attempting to re-join");
182 /* Send the re-association event though, because the association
183 * request really was successful, even if just a null-op.
185 memset(&wrqu, 0, sizeof(wrqu));
186 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
188 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
189 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
193 /* Use short preamble only when both the BSS and firmware support it */
194 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
195 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
196 lbs_deb_join("AdhocJoin: Short preamble\n");
197 preamble = RADIO_PREAMBLE_SHORT;
200 ret = lbs_set_radio(priv, preamble, 1);
204 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
205 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
207 priv->adhoccreate = 0;
208 priv->curbssparams.channel = bss->channel;
210 /* Build the join command */
211 memset(&cmd, 0, sizeof(cmd));
212 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
214 cmd.bss.type = CMD_BSS_TYPE_IBSS;
215 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
217 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
218 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
220 memcpy(&cmd.bss.phyparamset, &bss->phyparamset,
221 sizeof(union ieeetypes_phyparamset));
223 memcpy(&cmd.bss.ssparamset, &bss->ssparamset,
224 sizeof(union IEEEtypes_ssparamset));
226 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
227 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
228 bss->capability, CAPINFO_MASK);
230 /* information on BSSID descriptor passed to FW */
231 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
232 cmd.bss.bssid, cmd.bss.ssid);
234 /* Only v8 and below support setting these */
235 if (priv->fwrelease < 0x09000000) {
237 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
239 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
242 /* Copy Data rates from the rates recorded in scan response */
243 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
244 ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
245 memcpy(cmd.bss.rates, bss->rates, ratesize);
246 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
247 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
252 /* Copy the ad-hoc creation rates into Current BSS state structure */
253 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
254 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
256 /* Set MSB on basic rates as the firmware requires, but _after_
257 * copying to current bss rates.
259 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
261 cmd.bss.ssparamset.ibssparamset.atimwindow = cpu_to_le16(bss->atimwindow);
263 if (assoc_req->secinfo.wep_enabled) {
264 u16 tmp = le16_to_cpu(cmd.bss.capability);
265 tmp |= WLAN_CAPABILITY_PRIVACY;
266 cmd.bss.capability = cpu_to_le16(tmp);
269 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
270 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
273 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
282 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
287 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
289 ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
292 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
297 * @brief Start an Adhoc Network
299 * @param priv A pointer to struct lbs_private structure
300 * @param assoc_req The association request describing the BSS to start
302 * @return 0 on success, error on failure
304 static int lbs_adhoc_start(struct lbs_private *priv,
305 struct assoc_request *assoc_req)
307 struct cmd_ds_802_11_ad_hoc_start cmd;
308 u8 preamble = RADIO_PREAMBLE_LONG;
312 DECLARE_SSID_BUF(ssid);
314 lbs_deb_enter(LBS_DEB_ASSOC);
316 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
317 lbs_deb_join("ADHOC_START: Will use short preamble\n");
318 preamble = RADIO_PREAMBLE_SHORT;
321 ret = lbs_set_radio(priv, preamble, 1);
325 /* Build the start command */
326 memset(&cmd, 0, sizeof(cmd));
327 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
329 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
331 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
332 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
333 assoc_req->ssid_len);
335 cmd.bsstype = CMD_BSS_TYPE_IBSS;
337 if (priv->beacon_period == 0)
338 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
339 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
341 WARN_ON(!assoc_req->channel);
343 /* set Physical parameter set */
344 cmd.phyparamset.dsparamset.elementid = MFIE_TYPE_DS_SET;
345 cmd.phyparamset.dsparamset.len = 1;
346 cmd.phyparamset.dsparamset.currentchan = assoc_req->channel;
348 /* set IBSS parameter set */
349 cmd.ssparamset.ibssparamset.elementid = MFIE_TYPE_IBSS_SET;
350 cmd.ssparamset.ibssparamset.len = 2;
351 cmd.ssparamset.ibssparamset.atimwindow = 0;
353 /* set capability info */
354 tmpcap = WLAN_CAPABILITY_IBSS;
355 if (assoc_req->secinfo.wep_enabled) {
356 lbs_deb_join("ADHOC_START: WEP enabled, setting privacy on\n");
357 tmpcap |= WLAN_CAPABILITY_PRIVACY;
359 lbs_deb_join("ADHOC_START: WEP disabled, setting privacy off\n");
361 cmd.capability = cpu_to_le16(tmpcap);
363 /* Only v8 and below support setting probe delay */
364 if (priv->fwrelease < 0x09000000)
365 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
367 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
368 memcpy(cmd.rates, lbs_bg_rates, ratesize);
370 /* Copy the ad-hoc creating rates into Current BSS state structure */
371 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
372 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
374 /* Set MSB on basic rates as the firmware requires, but _after_
375 * copying to current bss rates.
377 lbs_set_basic_rate_flags(cmd.rates, ratesize);
379 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
380 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
382 if (lbs_create_dnld_countryinfo_11d(priv)) {
383 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
388 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
389 assoc_req->channel, assoc_req->band);
391 priv->adhoccreate = 1;
392 priv->mode = IW_MODE_ADHOC;
394 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
396 ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
399 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
404 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
406 * @param priv A pointer to struct lbs_private structure
407 * @return 0 on success, or an error
409 int lbs_adhoc_stop(struct lbs_private *priv)
411 struct cmd_ds_802_11_ad_hoc_stop cmd;
414 lbs_deb_enter(LBS_DEB_JOIN);
416 memset(&cmd, 0, sizeof (cmd));
417 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
419 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
421 /* Clean up everything even if there was an error */
422 lbs_mac_event_disconnected(priv);
424 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
428 static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
429 struct bss_descriptor *match_bss)
431 if (!secinfo->wep_enabled && !secinfo->WPAenabled
432 && !secinfo->WPA2enabled
433 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
434 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
435 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
441 static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
442 struct bss_descriptor *match_bss)
444 if (secinfo->wep_enabled && !secinfo->WPAenabled
445 && !secinfo->WPA2enabled
446 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
452 static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
453 struct bss_descriptor *match_bss)
455 if (!secinfo->wep_enabled && secinfo->WPAenabled
456 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
457 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
458 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
465 static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
466 struct bss_descriptor *match_bss)
468 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
469 (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
470 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
471 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
478 static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
479 struct bss_descriptor *match_bss)
481 if (!secinfo->wep_enabled && !secinfo->WPAenabled
482 && !secinfo->WPA2enabled
483 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
484 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
485 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
492 * @brief Check if a scanned network compatible with the driver settings
494 * WEP WPA WPA2 ad-hoc encrypt Network
495 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
496 * 0 0 0 0 NONE 0 0 0 yes No security
497 * 1 0 0 0 NONE 1 0 0 yes Static WEP
498 * 0 1 0 0 x 1x 1 x yes WPA
499 * 0 0 1 0 x 1x x 1 yes WPA2
500 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
501 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
504 * @param priv A pointer to struct lbs_private
505 * @param index Index in scantable to check against current driver settings
506 * @param mode Network mode: Infrastructure or IBSS
508 * @return Index in scantable, or error code if negative
510 static int is_network_compatible(struct lbs_private *priv,
511 struct bss_descriptor *bss, uint8_t mode)
515 lbs_deb_enter(LBS_DEB_SCAN);
517 if (bss->mode != mode)
520 matched = match_bss_no_security(&priv->secinfo, bss);
523 matched = match_bss_static_wep(&priv->secinfo, bss);
526 matched = match_bss_wpa(&priv->secinfo, bss);
528 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
529 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
530 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
531 priv->secinfo.wep_enabled ? "e" : "d",
532 priv->secinfo.WPAenabled ? "e" : "d",
533 priv->secinfo.WPA2enabled ? "e" : "d",
534 (bss->capability & WLAN_CAPABILITY_PRIVACY));
537 matched = match_bss_wpa2(&priv->secinfo, bss);
539 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
540 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
541 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
542 priv->secinfo.wep_enabled ? "e" : "d",
543 priv->secinfo.WPAenabled ? "e" : "d",
544 priv->secinfo.WPA2enabled ? "e" : "d",
545 (bss->capability & WLAN_CAPABILITY_PRIVACY));
548 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
550 lbs_deb_scan("is_network_compatible() dynamic WEP: "
551 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
552 bss->wpa_ie[0], bss->rsn_ie[0],
553 (bss->capability & WLAN_CAPABILITY_PRIVACY));
557 /* bss security settings don't match those configured on card */
558 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
559 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
560 bss->wpa_ie[0], bss->rsn_ie[0],
561 priv->secinfo.wep_enabled ? "e" : "d",
562 priv->secinfo.WPAenabled ? "e" : "d",
563 priv->secinfo.WPA2enabled ? "e" : "d",
564 (bss->capability & WLAN_CAPABILITY_PRIVACY));
567 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
572 * @brief This function finds a specific compatible BSSID in the scan list
574 * Used in association code
576 * @param priv A pointer to struct lbs_private
577 * @param bssid BSSID to find in the scan list
578 * @param mode Network mode: Infrastructure or IBSS
580 * @return index in BSSID list, or error return code (< 0)
582 static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
583 uint8_t *bssid, uint8_t mode)
585 struct bss_descriptor *iter_bss;
586 struct bss_descriptor *found_bss = NULL;
588 lbs_deb_enter(LBS_DEB_SCAN);
593 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
595 /* Look through the scan table for a compatible match. The loop will
596 * continue past a matched bssid that is not compatible in case there
597 * is an AP with multiple SSIDs assigned to the same BSSID
599 mutex_lock(&priv->lock);
600 list_for_each_entry(iter_bss, &priv->network_list, list) {
601 if (compare_ether_addr(iter_bss->bssid, bssid))
602 continue; /* bssid doesn't match */
606 if (!is_network_compatible(priv, iter_bss, mode))
608 found_bss = iter_bss;
611 found_bss = iter_bss;
615 mutex_unlock(&priv->lock);
618 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
623 * @brief This function finds ssid in ssid list.
625 * Used in association code
627 * @param priv A pointer to struct lbs_private
628 * @param ssid SSID to find in the list
629 * @param bssid BSSID to qualify the SSID selection (if provided)
630 * @param mode Network mode: Infrastructure or IBSS
632 * @return index in BSSID list
634 static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
635 uint8_t *ssid, uint8_t ssid_len,
636 uint8_t *bssid, uint8_t mode,
640 struct bss_descriptor *iter_bss = NULL;
641 struct bss_descriptor *found_bss = NULL;
642 struct bss_descriptor *tmp_oldest = NULL;
644 lbs_deb_enter(LBS_DEB_SCAN);
646 mutex_lock(&priv->lock);
648 list_for_each_entry(iter_bss, &priv->network_list, list) {
650 (iter_bss->last_scanned < tmp_oldest->last_scanned))
651 tmp_oldest = iter_bss;
653 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
654 ssid, ssid_len) != 0)
655 continue; /* ssid doesn't match */
656 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
657 continue; /* bssid doesn't match */
658 if ((channel > 0) && (iter_bss->channel != channel))
659 continue; /* channel doesn't match */
664 if (!is_network_compatible(priv, iter_bss, mode))
668 /* Found requested BSSID */
669 found_bss = iter_bss;
673 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
674 bestrssi = SCAN_RSSI(iter_bss->rssi);
675 found_bss = iter_bss;
680 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
681 bestrssi = SCAN_RSSI(iter_bss->rssi);
682 found_bss = iter_bss;
689 mutex_unlock(&priv->lock);
690 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
694 static int assoc_helper_essid(struct lbs_private *priv,
695 struct assoc_request * assoc_req)
698 struct bss_descriptor * bss;
700 DECLARE_SSID_BUF(ssid);
702 lbs_deb_enter(LBS_DEB_ASSOC);
704 /* FIXME: take channel into account when picking SSIDs if a channel
708 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
709 channel = assoc_req->channel;
711 lbs_deb_assoc("SSID '%s' requested\n",
712 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
713 if (assoc_req->mode == IW_MODE_INFRA) {
714 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
715 assoc_req->ssid_len);
717 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
718 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
720 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
721 ret = lbs_associate(priv, assoc_req);
723 lbs_deb_assoc("SSID not found; cannot associate\n");
725 } else if (assoc_req->mode == IW_MODE_ADHOC) {
726 /* Scan for the network, do not save previous results. Stale
727 * scan data will cause us to join a non-existant adhoc network
729 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
730 assoc_req->ssid_len);
732 /* Search for the requested SSID in the scan table */
733 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
734 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
736 lbs_deb_assoc("SSID found, will join\n");
737 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
738 lbs_adhoc_join(priv, assoc_req);
740 /* else send START command */
741 lbs_deb_assoc("SSID not found, creating adhoc network\n");
742 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
744 assoc_req->bss.ssid_len = assoc_req->ssid_len;
745 lbs_adhoc_start(priv, assoc_req);
749 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
754 static int assoc_helper_bssid(struct lbs_private *priv,
755 struct assoc_request * assoc_req)
758 struct bss_descriptor * bss;
760 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
762 /* Search for index position in list for requested MAC */
763 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
766 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
767 "cannot associate.\n", assoc_req->bssid);
771 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
772 if (assoc_req->mode == IW_MODE_INFRA) {
773 ret = lbs_associate(priv, assoc_req);
774 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
775 } else if (assoc_req->mode == IW_MODE_ADHOC) {
776 lbs_adhoc_join(priv, assoc_req);
780 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
785 static int assoc_helper_associate(struct lbs_private *priv,
786 struct assoc_request * assoc_req)
788 int ret = 0, done = 0;
790 lbs_deb_enter(LBS_DEB_ASSOC);
792 /* If we're given and 'any' BSSID, try associating based on SSID */
794 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
795 if (compare_ether_addr(bssid_any, assoc_req->bssid)
796 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
797 ret = assoc_helper_bssid(priv, assoc_req);
802 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
803 ret = assoc_helper_essid(priv, assoc_req);
806 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
811 static int assoc_helper_mode(struct lbs_private *priv,
812 struct assoc_request * assoc_req)
816 lbs_deb_enter(LBS_DEB_ASSOC);
818 if (assoc_req->mode == priv->mode)
821 if (assoc_req->mode == IW_MODE_INFRA) {
822 if (priv->psstate != PS_STATE_FULL_POWER)
823 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
824 priv->psmode = LBS802_11POWERMODECAM;
827 priv->mode = assoc_req->mode;
828 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
831 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
835 static int assoc_helper_channel(struct lbs_private *priv,
836 struct assoc_request * assoc_req)
840 lbs_deb_enter(LBS_DEB_ASSOC);
842 ret = lbs_update_channel(priv);
844 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
848 if (assoc_req->channel == priv->curbssparams.channel)
851 if (priv->mesh_dev) {
852 /* Change mesh channel first; 21.p21 firmware won't let
853 you change channel otherwise (even though it'll return
855 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
859 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
860 priv->curbssparams.channel, assoc_req->channel);
862 ret = lbs_set_channel(priv, assoc_req->channel);
864 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
866 /* FIXME: shouldn't need to grab the channel _again_ after setting
867 * it since the firmware is supposed to return the new channel, but
869 ret = lbs_update_channel(priv);
871 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
875 if (assoc_req->channel != priv->curbssparams.channel) {
876 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
881 if ( assoc_req->secinfo.wep_enabled
882 && (assoc_req->wep_keys[0].len
883 || assoc_req->wep_keys[1].len
884 || assoc_req->wep_keys[2].len
885 || assoc_req->wep_keys[3].len)) {
886 /* Make sure WEP keys are re-sent to firmware */
887 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
890 /* Must restart/rejoin adhoc networks after channel change */
891 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
895 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
896 priv->curbssparams.channel);
899 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
904 static int assoc_helper_wep_keys(struct lbs_private *priv,
905 struct assoc_request *assoc_req)
910 lbs_deb_enter(LBS_DEB_ASSOC);
912 /* Set or remove WEP keys */
913 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
914 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
915 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
917 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
922 /* enable/disable the MAC's WEP packet filter */
923 if (assoc_req->secinfo.wep_enabled)
924 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
926 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
928 lbs_set_mac_control(priv);
930 mutex_lock(&priv->lock);
932 /* Copy WEP keys into priv wep key fields */
933 for (i = 0; i < 4; i++) {
934 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
935 sizeof(struct enc_key));
937 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
939 mutex_unlock(&priv->lock);
942 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
946 static int assoc_helper_secinfo(struct lbs_private *priv,
947 struct assoc_request * assoc_req)
953 lbs_deb_enter(LBS_DEB_ASSOC);
955 memcpy(&priv->secinfo, &assoc_req->secinfo,
956 sizeof(struct lbs_802_11_security));
958 lbs_set_mac_control(priv);
960 /* If RSN is already enabled, don't try to enable it again, since
961 * ENABLE_RSN resets internal state machines and will clobber the
962 * 4-way WPA handshake.
965 /* Get RSN enabled/disabled */
966 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
968 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
972 /* Don't re-enable RSN if it's already enabled */
973 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
977 /* Set RSN enabled/disabled */
978 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
981 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
986 static int assoc_helper_wpa_keys(struct lbs_private *priv,
987 struct assoc_request * assoc_req)
990 unsigned int flags = assoc_req->flags;
992 lbs_deb_enter(LBS_DEB_ASSOC);
994 /* Work around older firmware bug where WPA unicast and multicast
995 * keys must be set independently. Seen in SDIO parts with firmware
999 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1000 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1001 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1002 assoc_req->flags = flags;
1008 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1009 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1011 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1012 assoc_req->flags = flags;
1016 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1021 static int assoc_helper_wpa_ie(struct lbs_private *priv,
1022 struct assoc_request * assoc_req)
1026 lbs_deb_enter(LBS_DEB_ASSOC);
1028 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1029 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1030 priv->wpa_ie_len = assoc_req->wpa_ie_len;
1032 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1033 priv->wpa_ie_len = 0;
1036 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1041 static int should_deauth_infrastructure(struct lbs_private *priv,
1042 struct assoc_request * assoc_req)
1046 if (priv->connect_status != LBS_CONNECTED)
1049 lbs_deb_enter(LBS_DEB_ASSOC);
1050 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1051 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1056 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1057 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
1058 lbs_deb_assoc("Deauthenticating due to new security\n");
1064 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1065 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1070 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1071 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1076 /* FIXME: deal with 'auto' mode somehow */
1077 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1078 if (assoc_req->mode != IW_MODE_INFRA) {
1079 lbs_deb_assoc("Deauthenticating due to leaving "
1087 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1092 static int should_stop_adhoc(struct lbs_private *priv,
1093 struct assoc_request * assoc_req)
1095 lbs_deb_enter(LBS_DEB_ASSOC);
1097 if (priv->connect_status != LBS_CONNECTED)
1100 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1101 priv->curbssparams.ssid_len,
1102 assoc_req->ssid, assoc_req->ssid_len) != 0)
1105 /* FIXME: deal with 'auto' mode somehow */
1106 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1107 if (assoc_req->mode != IW_MODE_ADHOC)
1111 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1112 if (assoc_req->channel != priv->curbssparams.channel)
1116 lbs_deb_leave(LBS_DEB_ASSOC);
1122 * @brief This function finds the best SSID in the Scan List
1124 * Search the scan table for the best SSID that also matches the current
1125 * adapter network preference (infrastructure or adhoc)
1127 * @param priv A pointer to struct lbs_private
1129 * @return index in BSSID list
1131 static struct bss_descriptor *lbs_find_best_ssid_in_list(
1132 struct lbs_private *priv, uint8_t mode)
1134 uint8_t bestrssi = 0;
1135 struct bss_descriptor *iter_bss;
1136 struct bss_descriptor *best_bss = NULL;
1138 lbs_deb_enter(LBS_DEB_SCAN);
1140 mutex_lock(&priv->lock);
1142 list_for_each_entry(iter_bss, &priv->network_list, list) {
1146 if (!is_network_compatible(priv, iter_bss, mode))
1148 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1150 bestrssi = SCAN_RSSI(iter_bss->rssi);
1151 best_bss = iter_bss;
1155 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1157 bestrssi = SCAN_RSSI(iter_bss->rssi);
1158 best_bss = iter_bss;
1163 mutex_unlock(&priv->lock);
1164 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1169 * @brief Find the best AP
1171 * Used from association worker.
1173 * @param priv A pointer to struct lbs_private structure
1174 * @param pSSID A pointer to AP's ssid
1176 * @return 0--success, otherwise--fail
1178 static int lbs_find_best_network_ssid(struct lbs_private *priv,
1179 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1183 struct bss_descriptor *found;
1185 lbs_deb_enter(LBS_DEB_SCAN);
1187 priv->scan_ssid_len = 0;
1188 lbs_scan_networks(priv, 1);
1189 if (priv->surpriseremoved)
1192 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1193 if (found && (found->ssid_len > 0)) {
1194 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1195 *out_ssid_len = found->ssid_len;
1196 *out_mode = found->mode;
1201 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1206 void lbs_association_worker(struct work_struct *work)
1208 struct lbs_private *priv = container_of(work, struct lbs_private,
1210 struct assoc_request * assoc_req = NULL;
1212 int find_any_ssid = 0;
1213 DECLARE_SSID_BUF(ssid);
1215 lbs_deb_enter(LBS_DEB_ASSOC);
1217 mutex_lock(&priv->lock);
1218 assoc_req = priv->pending_assoc_req;
1219 priv->pending_assoc_req = NULL;
1220 priv->in_progress_assoc_req = assoc_req;
1221 mutex_unlock(&priv->lock);
1227 "Association Request:\n"
1234 " secinfo: %s%s%s\n"
1237 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
1238 assoc_req->channel, assoc_req->band, assoc_req->mode,
1240 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1241 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1242 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1243 assoc_req->secinfo.auth_mode);
1245 /* If 'any' SSID was specified, find an SSID to associate with */
1246 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
1247 && !assoc_req->ssid_len)
1250 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1251 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1252 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1253 && compare_ether_addr(assoc_req->bssid, bssid_off))
1257 if (find_any_ssid) {
1258 u8 new_mode = assoc_req->mode;
1260 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
1261 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
1263 lbs_deb_assoc("Could not find best network\n");
1268 /* Ensure we switch to the mode of the AP */
1269 if (assoc_req->mode == IW_MODE_AUTO) {
1270 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1271 assoc_req->mode = new_mode;
1276 * Check if the attributes being changing require deauthentication
1277 * from the currently associated infrastructure access point.
1279 if (priv->mode == IW_MODE_INFRA) {
1280 if (should_deauth_infrastructure(priv, assoc_req)) {
1281 ret = lbs_cmd_80211_deauthenticate(priv,
1282 priv->curbssparams.bssid,
1283 WLAN_REASON_DEAUTH_LEAVING);
1285 lbs_deb_assoc("Deauthentication due to new "
1286 "configuration request failed: %d\n",
1290 } else if (priv->mode == IW_MODE_ADHOC) {
1291 if (should_stop_adhoc(priv, assoc_req)) {
1292 ret = lbs_adhoc_stop(priv);
1294 lbs_deb_assoc("Teardown of AdHoc network due to "
1295 "new configuration request failed: %d\n",
1302 /* Send the various configuration bits to the firmware */
1303 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1304 ret = assoc_helper_mode(priv, assoc_req);
1309 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1310 ret = assoc_helper_channel(priv, assoc_req);
1315 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1316 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1317 ret = assoc_helper_wep_keys(priv, assoc_req);
1322 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1323 ret = assoc_helper_secinfo(priv, assoc_req);
1328 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1329 ret = assoc_helper_wpa_ie(priv, assoc_req);
1334 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1335 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1336 ret = assoc_helper_wpa_keys(priv, assoc_req);
1341 /* SSID/BSSID should be the _last_ config option set, because they
1342 * trigger the association attempt.
1344 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1345 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1348 ret = assoc_helper_associate(priv, assoc_req);
1350 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
1355 if (priv->connect_status != LBS_CONNECTED) {
1356 lbs_deb_assoc("ASSOC: association unsuccessful, "
1362 lbs_deb_assoc("associated to %pM\n",
1363 priv->curbssparams.bssid);
1364 lbs_prepare_and_send_command(priv,
1366 0, CMD_OPTION_WAITFORRSP, 0, NULL);
1374 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
1378 mutex_lock(&priv->lock);
1379 priv->in_progress_assoc_req = NULL;
1380 mutex_unlock(&priv->lock);
1384 lbs_deb_leave(LBS_DEB_ASSOC);
1389 * Caller MUST hold any necessary locks
1391 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
1393 struct assoc_request * assoc_req;
1395 lbs_deb_enter(LBS_DEB_ASSOC);
1396 if (!priv->pending_assoc_req) {
1397 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
1399 if (!priv->pending_assoc_req) {
1400 lbs_pr_info("Not enough memory to allocate association"
1406 /* Copy current configuration attributes to the association request,
1407 * but don't overwrite any that are already set.
1409 assoc_req = priv->pending_assoc_req;
1410 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1411 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
1413 assoc_req->ssid_len = priv->curbssparams.ssid_len;
1416 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1417 assoc_req->channel = priv->curbssparams.channel;
1419 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
1420 assoc_req->band = priv->curbssparams.band;
1422 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
1423 assoc_req->mode = priv->mode;
1425 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1426 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
1430 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1432 for (i = 0; i < 4; i++) {
1433 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
1434 sizeof(struct enc_key));
1438 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
1439 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
1441 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1442 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
1443 sizeof(struct enc_key));
1446 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1447 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
1448 sizeof(struct enc_key));
1451 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1452 memcpy(&assoc_req->secinfo, &priv->secinfo,
1453 sizeof(struct lbs_802_11_security));
1456 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1457 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
1459 assoc_req->wpa_ie_len = priv->wpa_ie_len;
1462 lbs_deb_leave(LBS_DEB_ASSOC);
1468 * @brief This function prepares command of authenticate.
1470 * @param priv A pointer to struct lbs_private structure
1471 * @param cmd A pointer to cmd_ds_command structure
1472 * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
1476 int lbs_cmd_80211_authenticate(struct lbs_private *priv,
1477 struct cmd_ds_command *cmd,
1480 struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
1482 u8 *bssid = pdata_buf;
1484 lbs_deb_enter(LBS_DEB_JOIN);
1486 cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
1487 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
1490 /* translate auth mode to 802.11 defined wire value */
1491 switch (priv->secinfo.auth_mode) {
1492 case IW_AUTH_ALG_OPEN_SYSTEM:
1493 pauthenticate->authtype = 0x00;
1495 case IW_AUTH_ALG_SHARED_KEY:
1496 pauthenticate->authtype = 0x01;
1498 case IW_AUTH_ALG_LEAP:
1499 pauthenticate->authtype = 0x80;
1502 lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
1503 priv->secinfo.auth_mode);
1507 memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
1509 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
1510 bssid, pauthenticate->authtype);
1514 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1519 * @brief Deauthenticate from a specific BSS
1521 * @param priv A pointer to struct lbs_private structure
1522 * @param bssid The specific BSS to deauthenticate from
1523 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1525 * @return 0 on success, error on failure
1527 int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1530 struct cmd_ds_802_11_deauthenticate cmd;
1533 lbs_deb_enter(LBS_DEB_JOIN);
1535 memset(&cmd, 0, sizeof(cmd));
1536 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1537 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1538 cmd.reasoncode = cpu_to_le16(reason);
1540 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1542 /* Clean up everything even if there was an error; can't assume that
1543 * we're still authenticated to the AP after trying to deauth.
1545 lbs_mac_event_disconnected(priv);
1547 lbs_deb_leave(LBS_DEB_JOIN);
1551 int lbs_cmd_80211_associate(struct lbs_private *priv,
1552 struct cmd_ds_command *cmd, void *pdata_buf)
1554 struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
1556 struct assoc_request *assoc_req = pdata_buf;
1557 struct bss_descriptor *bss = &assoc_req->bss;
1560 struct mrvlietypes_ssidparamset *ssid;
1561 struct mrvlietypes_phyparamset *phy;
1562 struct mrvlietypes_ssparamset *ss;
1563 struct mrvlietypes_ratesparamset *rates;
1564 struct mrvlietypes_rsnparamset *rsn;
1566 lbs_deb_enter(LBS_DEB_ASSOC);
1575 cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1577 memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
1578 pos += sizeof(passo->peerstaaddr);
1580 /* set the listen interval */
1581 passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1583 pos += sizeof(passo->capability);
1584 pos += sizeof(passo->listeninterval);
1585 pos += sizeof(passo->bcnperiod);
1586 pos += sizeof(passo->dtimperiod);
1588 ssid = (struct mrvlietypes_ssidparamset *) pos;
1589 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
1590 tmplen = bss->ssid_len;
1591 ssid->header.len = cpu_to_le16(tmplen);
1592 memcpy(ssid->ssid, bss->ssid, tmplen);
1593 pos += sizeof(ssid->header) + tmplen;
1595 phy = (struct mrvlietypes_phyparamset *) pos;
1596 phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
1597 tmplen = sizeof(phy->fh_ds.dsparamset);
1598 phy->header.len = cpu_to_le16(tmplen);
1599 memcpy(&phy->fh_ds.dsparamset,
1600 &bss->phyparamset.dsparamset.currentchan,
1602 pos += sizeof(phy->header) + tmplen;
1604 ss = (struct mrvlietypes_ssparamset *) pos;
1605 ss->header.type = cpu_to_le16(TLV_TYPE_CF);
1606 tmplen = sizeof(ss->cf_ibss.cfparamset);
1607 ss->header.len = cpu_to_le16(tmplen);
1608 pos += sizeof(ss->header) + tmplen;
1610 rates = (struct mrvlietypes_ratesparamset *) pos;
1611 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
1612 memcpy(&rates->rates, &bss->rates, MAX_RATES);
1614 if (get_common_rates(priv, rates->rates, &tmplen)) {
1618 pos += sizeof(rates->header) + tmplen;
1619 rates->header.len = cpu_to_le16(tmplen);
1620 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
1622 /* Copy the infra. association rates into Current BSS state structure */
1623 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1624 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
1626 /* Set MSB on basic rates as the firmware requires, but _after_
1627 * copying to current bss rates.
1629 lbs_set_basic_rate_flags(rates->rates, tmplen);
1631 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1632 rsn = (struct mrvlietypes_rsnparamset *) pos;
1633 /* WPA_IE or WPA2_IE */
1634 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
1635 tmplen = (u16) assoc_req->wpa_ie[1];
1636 rsn->header.len = cpu_to_le16(tmplen);
1637 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
1638 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
1639 sizeof(rsn->header) + tmplen);
1640 pos += sizeof(rsn->header) + tmplen;
1643 /* update curbssparams */
1644 priv->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
1646 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
1651 cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
1653 /* set the capability info */
1654 tmpcap = (bss->capability & CAPINFO_MASK);
1655 if (bss->mode == IW_MODE_INFRA)
1656 tmpcap |= WLAN_CAPABILITY_ESS;
1657 passo->capability = cpu_to_le16(tmpcap);
1658 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
1661 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1665 int lbs_ret_80211_associate(struct lbs_private *priv,
1666 struct cmd_ds_command *resp)
1669 union iwreq_data wrqu;
1670 struct ieeetypes_assocrsp *passocrsp;
1671 struct bss_descriptor *bss;
1674 lbs_deb_enter(LBS_DEB_ASSOC);
1676 if (!priv->in_progress_assoc_req) {
1677 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
1681 bss = &priv->in_progress_assoc_req->bss;
1683 passocrsp = (struct ieeetypes_assocrsp *) &resp->params;
1686 * Older FW versions map the IEEE 802.11 Status Code in the association
1687 * response to the following values returned in passocrsp->statuscode:
1689 * IEEE Status Code Marvell Status Code
1690 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1691 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1692 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1693 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1694 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1695 * others -> 0x0003 ASSOC_RESULT_REFUSED
1697 * Other response codes:
1698 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1699 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1700 * association response from the AP)
1703 status_code = le16_to_cpu(passocrsp->statuscode);
1704 switch (status_code) {
1708 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
1711 lbs_deb_assoc("ASSOC_RESP: internal timer "
1712 "expired while waiting for the AP\n");
1715 lbs_deb_assoc("ASSOC_RESP: association "
1719 lbs_deb_assoc("ASSOC_RESP: authentication "
1723 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
1724 " unknown\n", status_code);
1729 lbs_mac_event_disconnected(priv);
1734 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
1735 le16_to_cpu(resp->size) - S_DS_GEN);
1737 /* Send a Media Connected event, according to the Spec */
1738 priv->connect_status = LBS_CONNECTED;
1740 /* Update current SSID and BSSID */
1741 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1742 priv->curbssparams.ssid_len = bss->ssid_len;
1743 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1745 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
1746 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
1748 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
1749 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
1750 priv->nextSNRNF = 0;
1753 netif_carrier_on(priv->dev);
1754 if (!priv->tx_pending_len)
1755 netif_wake_queue(priv->dev);
1757 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1758 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1759 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1762 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1766 static int lbs_adhoc_post(struct lbs_private *priv, struct cmd_header *resp)
1769 u16 command = le16_to_cpu(resp->command);
1770 u16 result = le16_to_cpu(resp->result);
1771 struct cmd_ds_802_11_ad_hoc_result *adhoc_resp;
1772 union iwreq_data wrqu;
1773 struct bss_descriptor *bss;
1774 DECLARE_SSID_BUF(ssid);
1776 lbs_deb_enter(LBS_DEB_JOIN);
1778 adhoc_resp = (struct cmd_ds_802_11_ad_hoc_result *) resp;
1780 if (!priv->in_progress_assoc_req) {
1781 lbs_deb_join("ADHOC_RESP: no in-progress association "
1786 bss = &priv->in_progress_assoc_req->bss;
1789 * Join result code 0 --> SUCCESS
1792 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
1793 if (priv->connect_status == LBS_CONNECTED)
1794 lbs_mac_event_disconnected(priv);
1799 /* Send a Media Connected event, according to the Spec */
1800 priv->connect_status = LBS_CONNECTED;
1802 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
1803 /* Update the created network descriptor with the new BSSID */
1804 memcpy(bss->bssid, adhoc_resp->bssid, ETH_ALEN);
1807 /* Set the BSSID from the joined/started descriptor */
1808 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1810 /* Set the new SSID to current SSID */
1811 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1812 priv->curbssparams.ssid_len = bss->ssid_len;
1814 netif_carrier_on(priv->dev);
1815 if (!priv->tx_pending_len)
1816 netif_wake_queue(priv->dev);
1818 memset(&wrqu, 0, sizeof(wrqu));
1819 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1820 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1821 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1823 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
1824 print_ssid(ssid, bss->ssid, bss->ssid_len),
1825 priv->curbssparams.bssid,
1826 priv->curbssparams.channel);
1829 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);