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 int lbs_adhoc_post(struct lbs_private *priv, struct cmd_header *resp);
17 static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
18 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
19 static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
20 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22 /* The firmware needs certain bits masked out of the beacon-derviced capability
23 * field when associating/joining to BSSs.
25 #define CAPINFO_MASK (~(0xda00))
29 * @brief This function finds common rates between rates and card rates.
31 * It will fill common rates in rates as output if found.
33 * NOTE: Setting the MSB of the basic rates need to be taken
34 * care, either before or after calling this function
36 * @param priv A pointer to struct lbs_private structure
37 * @param rates the buffer which keeps input and output
38 * @param rates_size the size of rate1 buffer; new size of buffer on return
40 * @return 0 on success, or -1 on error
42 static int get_common_rates(struct lbs_private *priv,
46 u8 *card_rates = lbs_bg_rates;
47 size_t num_card_rates = sizeof(lbs_bg_rates);
52 /* For each rate in card_rates that exists in rate1, copy to tmp */
53 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
54 for (j = 0; rates[j] && (j < *rates_size); j++) {
55 if (rates[j] == card_rates[i])
56 tmp[tmp_size++] = card_rates[i];
60 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
61 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
62 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
63 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
65 if (!priv->enablehwauto) {
66 for (i = 0; i < tmp_size; i++) {
67 if (tmp[i] == priv->cur_rate)
70 lbs_pr_alert("Previously set fixed data rate %#x isn't "
71 "compatible with the network.\n", priv->cur_rate);
78 memset(rates, 0, *rates_size);
79 *rates_size = min_t(int, tmp_size, *rates_size);
80 memcpy(rates, tmp, *rates_size);
86 * @brief Sets the MSB on basic rates as the firmware requires
88 * Scan through an array and set the MSB for basic data rates.
90 * @param rates buffer of data rates
91 * @param len size of buffer
93 static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
97 for (i = 0; i < len; i++) {
98 if (rates[i] == 0x02 || rates[i] == 0x04 ||
99 rates[i] == 0x0b || rates[i] == 0x16)
106 * @brief Associate to a specific BSS discovered in a scan
108 * @param priv A pointer to struct lbs_private structure
109 * @param assoc_req The association request describing the BSS to associate with
111 * @return 0-success, otherwise fail
113 static int lbs_associate(struct lbs_private *priv,
114 struct assoc_request *assoc_req)
117 u8 preamble = RADIO_PREAMBLE_LONG;
119 lbs_deb_enter(LBS_DEB_ASSOC);
121 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
122 0, CMD_OPTION_WAITFORRSP,
123 0, assoc_req->bss.bssid);
127 /* Use short preamble only when both the BSS and firmware support it */
128 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
129 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
130 preamble = RADIO_PREAMBLE_SHORT;
132 ret = lbs_set_radio(priv, preamble, 1);
136 ret = lbs_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
137 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
140 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
145 * @brief Join an adhoc network found in a previous scan
147 * @param priv A pointer to struct lbs_private structure
148 * @param assoc_req The association request describing the BSS to join
150 * @return 0 on success, error on failure
152 static int lbs_adhoc_join(struct lbs_private *priv,
153 struct assoc_request *assoc_req)
155 struct cmd_ds_802_11_ad_hoc_join cmd;
156 struct bss_descriptor *bss = &assoc_req->bss;
157 u8 preamble = RADIO_PREAMBLE_LONG;
158 DECLARE_SSID_BUF(ssid);
162 lbs_deb_enter(LBS_DEB_ASSOC);
164 lbs_deb_join("current SSID '%s', ssid length %u\n",
165 print_ssid(ssid, priv->curbssparams.ssid,
166 priv->curbssparams.ssid_len),
167 priv->curbssparams.ssid_len);
168 lbs_deb_join("requested ssid '%s', ssid length %u\n",
169 print_ssid(ssid, bss->ssid, bss->ssid_len),
172 /* check if the requested SSID is already joined */
173 if (priv->curbssparams.ssid_len &&
174 !lbs_ssid_cmp(priv->curbssparams.ssid,
175 priv->curbssparams.ssid_len,
176 bss->ssid, bss->ssid_len) &&
177 (priv->mode == IW_MODE_ADHOC) &&
178 (priv->connect_status == LBS_CONNECTED)) {
179 union iwreq_data wrqu;
181 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
182 "current, not attempting to re-join");
184 /* Send the re-association event though, because the association
185 * request really was successful, even if just a null-op.
187 memset(&wrqu, 0, sizeof(wrqu));
188 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
190 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
191 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
195 /* Use short preamble only when both the BSS and firmware support it */
196 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
197 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
198 lbs_deb_join("AdhocJoin: Short preamble\n");
199 preamble = RADIO_PREAMBLE_SHORT;
202 ret = lbs_set_radio(priv, preamble, 1);
206 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
207 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
209 priv->adhoccreate = 0;
210 priv->curbssparams.channel = bss->channel;
212 /* Build the join command */
213 memset(&cmd, 0, sizeof(cmd));
214 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
216 cmd.bss.type = CMD_BSS_TYPE_IBSS;
217 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
219 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
220 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
222 memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
224 memcpy(&cmd.bss.ibss, &bss->ss.ibss,
225 sizeof(struct ieee_ie_ibss_param_set));
227 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
228 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
229 bss->capability, CAPINFO_MASK);
231 /* information on BSSID descriptor passed to FW */
232 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
233 cmd.bss.bssid, cmd.bss.ssid);
235 /* Only v8 and below support setting these */
236 if (priv->fwrelease < 0x09000000) {
238 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
240 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
243 /* Copy Data rates from the rates recorded in scan response */
244 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
245 ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
246 memcpy(cmd.bss.rates, bss->rates, ratesize);
247 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
248 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
253 /* Copy the ad-hoc creation rates into Current BSS state structure */
254 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
255 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
257 /* Set MSB on basic rates as the firmware requires, but _after_
258 * copying to current bss rates.
260 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
262 cmd.bss.ibss.atimwindow = bss->atimwindow;
264 if (assoc_req->secinfo.wep_enabled) {
265 u16 tmp = le16_to_cpu(cmd.bss.capability);
266 tmp |= WLAN_CAPABILITY_PRIVACY;
267 cmd.bss.capability = cpu_to_le16(tmp);
270 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
271 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
274 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
283 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
288 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
290 ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
293 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
298 * @brief Start an Adhoc Network
300 * @param priv A pointer to struct lbs_private structure
301 * @param assoc_req The association request describing the BSS to start
303 * @return 0 on success, error on failure
305 static int lbs_adhoc_start(struct lbs_private *priv,
306 struct assoc_request *assoc_req)
308 struct cmd_ds_802_11_ad_hoc_start cmd;
309 u8 preamble = RADIO_PREAMBLE_LONG;
313 DECLARE_SSID_BUF(ssid);
315 lbs_deb_enter(LBS_DEB_ASSOC);
317 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
318 lbs_deb_join("ADHOC_START: Will use short preamble\n");
319 preamble = RADIO_PREAMBLE_SHORT;
322 ret = lbs_set_radio(priv, preamble, 1);
326 /* Build the start command */
327 memset(&cmd, 0, sizeof(cmd));
328 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
330 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
332 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
333 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
334 assoc_req->ssid_len);
336 cmd.bsstype = CMD_BSS_TYPE_IBSS;
338 if (priv->beacon_period == 0)
339 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
340 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
342 WARN_ON(!assoc_req->channel);
344 /* set Physical parameter set */
345 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
346 cmd.ds.header.len = 1;
347 cmd.ds.channel = assoc_req->channel;
349 /* set IBSS parameter set */
350 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
351 cmd.ibss.header.len = 2;
352 cmd.ibss.atimwindow = cpu_to_le16(0);
354 /* set capability info */
355 tmpcap = WLAN_CAPABILITY_IBSS;
356 if (assoc_req->secinfo.wep_enabled) {
357 lbs_deb_join("ADHOC_START: WEP enabled, setting privacy on\n");
358 tmpcap |= WLAN_CAPABILITY_PRIVACY;
360 lbs_deb_join("ADHOC_START: WEP disabled, setting privacy off\n");
362 cmd.capability = cpu_to_le16(tmpcap);
364 /* Only v8 and below support setting probe delay */
365 if (priv->fwrelease < 0x09000000)
366 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
368 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
369 memcpy(cmd.rates, lbs_bg_rates, ratesize);
371 /* Copy the ad-hoc creating rates into Current BSS state structure */
372 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
373 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
375 /* Set MSB on basic rates as the firmware requires, but _after_
376 * copying to current bss rates.
378 lbs_set_basic_rate_flags(cmd.rates, ratesize);
380 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
381 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
383 if (lbs_create_dnld_countryinfo_11d(priv)) {
384 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
389 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
390 assoc_req->channel, assoc_req->band);
392 priv->adhoccreate = 1;
393 priv->mode = IW_MODE_ADHOC;
395 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
397 ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
400 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
405 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
407 * @param priv A pointer to struct lbs_private structure
408 * @return 0 on success, or an error
410 int lbs_adhoc_stop(struct lbs_private *priv)
412 struct cmd_ds_802_11_ad_hoc_stop cmd;
415 lbs_deb_enter(LBS_DEB_JOIN);
417 memset(&cmd, 0, sizeof (cmd));
418 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
420 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
422 /* Clean up everything even if there was an error */
423 lbs_mac_event_disconnected(priv);
425 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
429 static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
430 struct bss_descriptor *match_bss)
432 if (!secinfo->wep_enabled && !secinfo->WPAenabled
433 && !secinfo->WPA2enabled
434 && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
435 && match_bss->rsn_ie[0] != WLAN_EID_RSN
436 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
442 static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
443 struct bss_descriptor *match_bss)
445 if (secinfo->wep_enabled && !secinfo->WPAenabled
446 && !secinfo->WPA2enabled
447 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
453 static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
454 struct bss_descriptor *match_bss)
456 if (!secinfo->wep_enabled && secinfo->WPAenabled
457 && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
458 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
459 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
466 static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
467 struct bss_descriptor *match_bss)
469 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
470 (match_bss->rsn_ie[0] == WLAN_EID_RSN)
471 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
472 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
479 static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
480 struct bss_descriptor *match_bss)
482 if (!secinfo->wep_enabled && !secinfo->WPAenabled
483 && !secinfo->WPA2enabled
484 && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
485 && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
486 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
493 * @brief Check if a scanned network compatible with the driver settings
495 * WEP WPA WPA2 ad-hoc encrypt Network
496 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
497 * 0 0 0 0 NONE 0 0 0 yes No security
498 * 1 0 0 0 NONE 1 0 0 yes Static WEP
499 * 0 1 0 0 x 1x 1 x yes WPA
500 * 0 0 1 0 x 1x x 1 yes WPA2
501 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
502 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
505 * @param priv A pointer to struct lbs_private
506 * @param index Index in scantable to check against current driver settings
507 * @param mode Network mode: Infrastructure or IBSS
509 * @return Index in scantable, or error code if negative
511 static int is_network_compatible(struct lbs_private *priv,
512 struct bss_descriptor *bss, uint8_t mode)
516 lbs_deb_enter(LBS_DEB_SCAN);
518 if (bss->mode != mode)
521 matched = match_bss_no_security(&priv->secinfo, bss);
524 matched = match_bss_static_wep(&priv->secinfo, bss);
527 matched = match_bss_wpa(&priv->secinfo, bss);
529 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
530 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
531 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
532 priv->secinfo.wep_enabled ? "e" : "d",
533 priv->secinfo.WPAenabled ? "e" : "d",
534 priv->secinfo.WPA2enabled ? "e" : "d",
535 (bss->capability & WLAN_CAPABILITY_PRIVACY));
538 matched = match_bss_wpa2(&priv->secinfo, bss);
540 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
541 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
542 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
543 priv->secinfo.wep_enabled ? "e" : "d",
544 priv->secinfo.WPAenabled ? "e" : "d",
545 priv->secinfo.WPA2enabled ? "e" : "d",
546 (bss->capability & WLAN_CAPABILITY_PRIVACY));
549 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
551 lbs_deb_scan("is_network_compatible() dynamic WEP: "
552 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
553 bss->wpa_ie[0], bss->rsn_ie[0],
554 (bss->capability & WLAN_CAPABILITY_PRIVACY));
558 /* bss security settings don't match those configured on card */
559 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
560 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
561 bss->wpa_ie[0], bss->rsn_ie[0],
562 priv->secinfo.wep_enabled ? "e" : "d",
563 priv->secinfo.WPAenabled ? "e" : "d",
564 priv->secinfo.WPA2enabled ? "e" : "d",
565 (bss->capability & WLAN_CAPABILITY_PRIVACY));
568 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
573 * @brief This function finds a specific compatible BSSID in the scan list
575 * Used in association code
577 * @param priv A pointer to struct lbs_private
578 * @param bssid BSSID to find in the scan list
579 * @param mode Network mode: Infrastructure or IBSS
581 * @return index in BSSID list, or error return code (< 0)
583 static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
584 uint8_t *bssid, uint8_t mode)
586 struct bss_descriptor *iter_bss;
587 struct bss_descriptor *found_bss = NULL;
589 lbs_deb_enter(LBS_DEB_SCAN);
594 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
596 /* Look through the scan table for a compatible match. The loop will
597 * continue past a matched bssid that is not compatible in case there
598 * is an AP with multiple SSIDs assigned to the same BSSID
600 mutex_lock(&priv->lock);
601 list_for_each_entry(iter_bss, &priv->network_list, list) {
602 if (compare_ether_addr(iter_bss->bssid, bssid))
603 continue; /* bssid doesn't match */
607 if (!is_network_compatible(priv, iter_bss, mode))
609 found_bss = iter_bss;
612 found_bss = iter_bss;
616 mutex_unlock(&priv->lock);
619 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
624 * @brief This function finds ssid in ssid list.
626 * Used in association code
628 * @param priv A pointer to struct lbs_private
629 * @param ssid SSID to find in the list
630 * @param bssid BSSID to qualify the SSID selection (if provided)
631 * @param mode Network mode: Infrastructure or IBSS
633 * @return index in BSSID list
635 static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
636 uint8_t *ssid, uint8_t ssid_len,
637 uint8_t *bssid, uint8_t mode,
641 struct bss_descriptor *iter_bss = NULL;
642 struct bss_descriptor *found_bss = NULL;
643 struct bss_descriptor *tmp_oldest = NULL;
645 lbs_deb_enter(LBS_DEB_SCAN);
647 mutex_lock(&priv->lock);
649 list_for_each_entry(iter_bss, &priv->network_list, list) {
651 (iter_bss->last_scanned < tmp_oldest->last_scanned))
652 tmp_oldest = iter_bss;
654 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
655 ssid, ssid_len) != 0)
656 continue; /* ssid doesn't match */
657 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
658 continue; /* bssid doesn't match */
659 if ((channel > 0) && (iter_bss->channel != channel))
660 continue; /* channel doesn't match */
665 if (!is_network_compatible(priv, iter_bss, mode))
669 /* Found requested BSSID */
670 found_bss = iter_bss;
674 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
675 bestrssi = SCAN_RSSI(iter_bss->rssi);
676 found_bss = iter_bss;
681 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
682 bestrssi = SCAN_RSSI(iter_bss->rssi);
683 found_bss = iter_bss;
690 mutex_unlock(&priv->lock);
691 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
695 static int assoc_helper_essid(struct lbs_private *priv,
696 struct assoc_request * assoc_req)
699 struct bss_descriptor * bss;
701 DECLARE_SSID_BUF(ssid);
703 lbs_deb_enter(LBS_DEB_ASSOC);
705 /* FIXME: take channel into account when picking SSIDs if a channel
709 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
710 channel = assoc_req->channel;
712 lbs_deb_assoc("SSID '%s' requested\n",
713 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
714 if (assoc_req->mode == IW_MODE_INFRA) {
715 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
716 assoc_req->ssid_len);
718 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
719 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
721 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
722 ret = lbs_associate(priv, assoc_req);
724 lbs_deb_assoc("SSID not found; cannot associate\n");
726 } else if (assoc_req->mode == IW_MODE_ADHOC) {
727 /* Scan for the network, do not save previous results. Stale
728 * scan data will cause us to join a non-existant adhoc network
730 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
731 assoc_req->ssid_len);
733 /* Search for the requested SSID in the scan table */
734 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
735 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
737 lbs_deb_assoc("SSID found, will join\n");
738 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
739 lbs_adhoc_join(priv, assoc_req);
741 /* else send START command */
742 lbs_deb_assoc("SSID not found, creating adhoc network\n");
743 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
745 assoc_req->bss.ssid_len = assoc_req->ssid_len;
746 lbs_adhoc_start(priv, assoc_req);
750 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
755 static int assoc_helper_bssid(struct lbs_private *priv,
756 struct assoc_request * assoc_req)
759 struct bss_descriptor * bss;
761 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
763 /* Search for index position in list for requested MAC */
764 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
767 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
768 "cannot associate.\n", assoc_req->bssid);
772 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
773 if (assoc_req->mode == IW_MODE_INFRA) {
774 ret = lbs_associate(priv, assoc_req);
775 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
776 } else if (assoc_req->mode == IW_MODE_ADHOC) {
777 lbs_adhoc_join(priv, assoc_req);
781 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
786 static int assoc_helper_associate(struct lbs_private *priv,
787 struct assoc_request * assoc_req)
789 int ret = 0, done = 0;
791 lbs_deb_enter(LBS_DEB_ASSOC);
793 /* If we're given and 'any' BSSID, try associating based on SSID */
795 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
796 if (compare_ether_addr(bssid_any, assoc_req->bssid)
797 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
798 ret = assoc_helper_bssid(priv, assoc_req);
803 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
804 ret = assoc_helper_essid(priv, assoc_req);
807 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
812 static int assoc_helper_mode(struct lbs_private *priv,
813 struct assoc_request * assoc_req)
817 lbs_deb_enter(LBS_DEB_ASSOC);
819 if (assoc_req->mode == priv->mode)
822 if (assoc_req->mode == IW_MODE_INFRA) {
823 if (priv->psstate != PS_STATE_FULL_POWER)
824 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
825 priv->psmode = LBS802_11POWERMODECAM;
828 priv->mode = assoc_req->mode;
829 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
832 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
836 static int assoc_helper_channel(struct lbs_private *priv,
837 struct assoc_request * assoc_req)
841 lbs_deb_enter(LBS_DEB_ASSOC);
843 ret = lbs_update_channel(priv);
845 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
849 if (assoc_req->channel == priv->curbssparams.channel)
852 if (priv->mesh_dev) {
853 /* Change mesh channel first; 21.p21 firmware won't let
854 you change channel otherwise (even though it'll return
856 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
860 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
861 priv->curbssparams.channel, assoc_req->channel);
863 ret = lbs_set_channel(priv, assoc_req->channel);
865 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
867 /* FIXME: shouldn't need to grab the channel _again_ after setting
868 * it since the firmware is supposed to return the new channel, but
870 ret = lbs_update_channel(priv);
872 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
876 if (assoc_req->channel != priv->curbssparams.channel) {
877 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
882 if ( assoc_req->secinfo.wep_enabled
883 && (assoc_req->wep_keys[0].len
884 || assoc_req->wep_keys[1].len
885 || assoc_req->wep_keys[2].len
886 || assoc_req->wep_keys[3].len)) {
887 /* Make sure WEP keys are re-sent to firmware */
888 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
891 /* Must restart/rejoin adhoc networks after channel change */
892 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
896 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
897 priv->curbssparams.channel);
900 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
905 static int assoc_helper_wep_keys(struct lbs_private *priv,
906 struct assoc_request *assoc_req)
911 lbs_deb_enter(LBS_DEB_ASSOC);
913 /* Set or remove WEP keys */
914 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
915 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
916 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
918 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
923 /* enable/disable the MAC's WEP packet filter */
924 if (assoc_req->secinfo.wep_enabled)
925 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
927 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
929 lbs_set_mac_control(priv);
931 mutex_lock(&priv->lock);
933 /* Copy WEP keys into priv wep key fields */
934 for (i = 0; i < 4; i++) {
935 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
936 sizeof(struct enc_key));
938 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
940 mutex_unlock(&priv->lock);
943 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
947 static int assoc_helper_secinfo(struct lbs_private *priv,
948 struct assoc_request * assoc_req)
954 lbs_deb_enter(LBS_DEB_ASSOC);
956 memcpy(&priv->secinfo, &assoc_req->secinfo,
957 sizeof(struct lbs_802_11_security));
959 lbs_set_mac_control(priv);
961 /* If RSN is already enabled, don't try to enable it again, since
962 * ENABLE_RSN resets internal state machines and will clobber the
963 * 4-way WPA handshake.
966 /* Get RSN enabled/disabled */
967 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
969 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
973 /* Don't re-enable RSN if it's already enabled */
974 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
978 /* Set RSN enabled/disabled */
979 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
982 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
987 static int assoc_helper_wpa_keys(struct lbs_private *priv,
988 struct assoc_request * assoc_req)
991 unsigned int flags = assoc_req->flags;
993 lbs_deb_enter(LBS_DEB_ASSOC);
995 /* Work around older firmware bug where WPA unicast and multicast
996 * keys must be set independently. Seen in SDIO parts with firmware
1000 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1001 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1002 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1003 assoc_req->flags = flags;
1009 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1010 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1012 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1013 assoc_req->flags = flags;
1017 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1022 static int assoc_helper_wpa_ie(struct lbs_private *priv,
1023 struct assoc_request * assoc_req)
1027 lbs_deb_enter(LBS_DEB_ASSOC);
1029 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1030 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1031 priv->wpa_ie_len = assoc_req->wpa_ie_len;
1033 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1034 priv->wpa_ie_len = 0;
1037 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1042 static int should_deauth_infrastructure(struct lbs_private *priv,
1043 struct assoc_request * assoc_req)
1047 if (priv->connect_status != LBS_CONNECTED)
1050 lbs_deb_enter(LBS_DEB_ASSOC);
1051 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1052 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1057 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1058 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
1059 lbs_deb_assoc("Deauthenticating due to new security\n");
1065 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1066 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1071 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1072 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1077 /* FIXME: deal with 'auto' mode somehow */
1078 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1079 if (assoc_req->mode != IW_MODE_INFRA) {
1080 lbs_deb_assoc("Deauthenticating due to leaving "
1088 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1093 static int should_stop_adhoc(struct lbs_private *priv,
1094 struct assoc_request * assoc_req)
1096 lbs_deb_enter(LBS_DEB_ASSOC);
1098 if (priv->connect_status != LBS_CONNECTED)
1101 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1102 priv->curbssparams.ssid_len,
1103 assoc_req->ssid, assoc_req->ssid_len) != 0)
1106 /* FIXME: deal with 'auto' mode somehow */
1107 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1108 if (assoc_req->mode != IW_MODE_ADHOC)
1112 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1113 if (assoc_req->channel != priv->curbssparams.channel)
1117 lbs_deb_leave(LBS_DEB_ASSOC);
1123 * @brief This function finds the best SSID in the Scan List
1125 * Search the scan table for the best SSID that also matches the current
1126 * adapter network preference (infrastructure or adhoc)
1128 * @param priv A pointer to struct lbs_private
1130 * @return index in BSSID list
1132 static struct bss_descriptor *lbs_find_best_ssid_in_list(
1133 struct lbs_private *priv, uint8_t mode)
1135 uint8_t bestrssi = 0;
1136 struct bss_descriptor *iter_bss;
1137 struct bss_descriptor *best_bss = NULL;
1139 lbs_deb_enter(LBS_DEB_SCAN);
1141 mutex_lock(&priv->lock);
1143 list_for_each_entry(iter_bss, &priv->network_list, list) {
1147 if (!is_network_compatible(priv, iter_bss, mode))
1149 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1151 bestrssi = SCAN_RSSI(iter_bss->rssi);
1152 best_bss = iter_bss;
1156 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1158 bestrssi = SCAN_RSSI(iter_bss->rssi);
1159 best_bss = iter_bss;
1164 mutex_unlock(&priv->lock);
1165 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1170 * @brief Find the best AP
1172 * Used from association worker.
1174 * @param priv A pointer to struct lbs_private structure
1175 * @param pSSID A pointer to AP's ssid
1177 * @return 0--success, otherwise--fail
1179 static int lbs_find_best_network_ssid(struct lbs_private *priv,
1180 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1184 struct bss_descriptor *found;
1186 lbs_deb_enter(LBS_DEB_SCAN);
1188 priv->scan_ssid_len = 0;
1189 lbs_scan_networks(priv, 1);
1190 if (priv->surpriseremoved)
1193 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1194 if (found && (found->ssid_len > 0)) {
1195 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1196 *out_ssid_len = found->ssid_len;
1197 *out_mode = found->mode;
1202 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1207 void lbs_association_worker(struct work_struct *work)
1209 struct lbs_private *priv = container_of(work, struct lbs_private,
1211 struct assoc_request * assoc_req = NULL;
1213 int find_any_ssid = 0;
1214 DECLARE_SSID_BUF(ssid);
1216 lbs_deb_enter(LBS_DEB_ASSOC);
1218 mutex_lock(&priv->lock);
1219 assoc_req = priv->pending_assoc_req;
1220 priv->pending_assoc_req = NULL;
1221 priv->in_progress_assoc_req = assoc_req;
1222 mutex_unlock(&priv->lock);
1228 "Association Request:\n"
1235 " secinfo: %s%s%s\n"
1238 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
1239 assoc_req->channel, assoc_req->band, assoc_req->mode,
1241 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1242 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1243 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1244 assoc_req->secinfo.auth_mode);
1246 /* If 'any' SSID was specified, find an SSID to associate with */
1247 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
1248 && !assoc_req->ssid_len)
1251 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1252 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1253 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1254 && compare_ether_addr(assoc_req->bssid, bssid_off))
1258 if (find_any_ssid) {
1259 u8 new_mode = assoc_req->mode;
1261 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
1262 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
1264 lbs_deb_assoc("Could not find best network\n");
1269 /* Ensure we switch to the mode of the AP */
1270 if (assoc_req->mode == IW_MODE_AUTO) {
1271 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1272 assoc_req->mode = new_mode;
1277 * Check if the attributes being changing require deauthentication
1278 * from the currently associated infrastructure access point.
1280 if (priv->mode == IW_MODE_INFRA) {
1281 if (should_deauth_infrastructure(priv, assoc_req)) {
1282 ret = lbs_cmd_80211_deauthenticate(priv,
1283 priv->curbssparams.bssid,
1284 WLAN_REASON_DEAUTH_LEAVING);
1286 lbs_deb_assoc("Deauthentication due to new "
1287 "configuration request failed: %d\n",
1291 } else if (priv->mode == IW_MODE_ADHOC) {
1292 if (should_stop_adhoc(priv, assoc_req)) {
1293 ret = lbs_adhoc_stop(priv);
1295 lbs_deb_assoc("Teardown of AdHoc network due to "
1296 "new configuration request failed: %d\n",
1303 /* Send the various configuration bits to the firmware */
1304 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1305 ret = assoc_helper_mode(priv, assoc_req);
1310 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1311 ret = assoc_helper_channel(priv, assoc_req);
1316 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1317 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1318 ret = assoc_helper_wep_keys(priv, assoc_req);
1323 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1324 ret = assoc_helper_secinfo(priv, assoc_req);
1329 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1330 ret = assoc_helper_wpa_ie(priv, assoc_req);
1335 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1336 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1337 ret = assoc_helper_wpa_keys(priv, assoc_req);
1342 /* SSID/BSSID should be the _last_ config option set, because they
1343 * trigger the association attempt.
1345 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1346 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1349 ret = assoc_helper_associate(priv, assoc_req);
1351 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
1356 if (priv->connect_status != LBS_CONNECTED) {
1357 lbs_deb_assoc("ASSOC: association unsuccessful, "
1363 lbs_deb_assoc("associated to %pM\n",
1364 priv->curbssparams.bssid);
1365 lbs_prepare_and_send_command(priv,
1367 0, CMD_OPTION_WAITFORRSP, 0, NULL);
1375 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
1379 mutex_lock(&priv->lock);
1380 priv->in_progress_assoc_req = NULL;
1381 mutex_unlock(&priv->lock);
1385 lbs_deb_leave(LBS_DEB_ASSOC);
1390 * Caller MUST hold any necessary locks
1392 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
1394 struct assoc_request * assoc_req;
1396 lbs_deb_enter(LBS_DEB_ASSOC);
1397 if (!priv->pending_assoc_req) {
1398 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
1400 if (!priv->pending_assoc_req) {
1401 lbs_pr_info("Not enough memory to allocate association"
1407 /* Copy current configuration attributes to the association request,
1408 * but don't overwrite any that are already set.
1410 assoc_req = priv->pending_assoc_req;
1411 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1412 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
1414 assoc_req->ssid_len = priv->curbssparams.ssid_len;
1417 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1418 assoc_req->channel = priv->curbssparams.channel;
1420 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
1421 assoc_req->band = priv->curbssparams.band;
1423 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
1424 assoc_req->mode = priv->mode;
1426 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1427 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
1431 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1433 for (i = 0; i < 4; i++) {
1434 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
1435 sizeof(struct enc_key));
1439 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
1440 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
1442 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1443 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
1444 sizeof(struct enc_key));
1447 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1448 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
1449 sizeof(struct enc_key));
1452 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1453 memcpy(&assoc_req->secinfo, &priv->secinfo,
1454 sizeof(struct lbs_802_11_security));
1457 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1458 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
1460 assoc_req->wpa_ie_len = priv->wpa_ie_len;
1463 lbs_deb_leave(LBS_DEB_ASSOC);
1469 * @brief This function prepares command of authenticate.
1471 * @param priv A pointer to struct lbs_private structure
1472 * @param cmd A pointer to cmd_ds_command structure
1473 * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
1477 int lbs_cmd_80211_authenticate(struct lbs_private *priv,
1478 struct cmd_ds_command *cmd,
1481 struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
1483 u8 *bssid = pdata_buf;
1485 lbs_deb_enter(LBS_DEB_JOIN);
1487 cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
1488 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
1491 /* translate auth mode to 802.11 defined wire value */
1492 switch (priv->secinfo.auth_mode) {
1493 case IW_AUTH_ALG_OPEN_SYSTEM:
1494 pauthenticate->authtype = 0x00;
1496 case IW_AUTH_ALG_SHARED_KEY:
1497 pauthenticate->authtype = 0x01;
1499 case IW_AUTH_ALG_LEAP:
1500 pauthenticate->authtype = 0x80;
1503 lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
1504 priv->secinfo.auth_mode);
1508 memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
1510 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
1511 bssid, pauthenticate->authtype);
1515 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1520 * @brief Deauthenticate from a specific BSS
1522 * @param priv A pointer to struct lbs_private structure
1523 * @param bssid The specific BSS to deauthenticate from
1524 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1526 * @return 0 on success, error on failure
1528 int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1531 struct cmd_ds_802_11_deauthenticate cmd;
1534 lbs_deb_enter(LBS_DEB_JOIN);
1536 memset(&cmd, 0, sizeof(cmd));
1537 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1538 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1539 cmd.reasoncode = cpu_to_le16(reason);
1541 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1543 /* Clean up everything even if there was an error; can't assume that
1544 * we're still authenticated to the AP after trying to deauth.
1546 lbs_mac_event_disconnected(priv);
1548 lbs_deb_leave(LBS_DEB_JOIN);
1552 int lbs_cmd_80211_associate(struct lbs_private *priv,
1553 struct cmd_ds_command *cmd, void *pdata_buf)
1555 struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
1557 struct assoc_request *assoc_req = pdata_buf;
1558 struct bss_descriptor *bss = &assoc_req->bss;
1561 struct mrvl_ie_ssid_param_set *ssid;
1562 struct mrvl_ie_ds_param_set *ds;
1563 struct mrvl_ie_cf_param_set *cf;
1564 struct mrvl_ie_rates_param_set *rates;
1565 struct mrvl_ie_rsn_param_set *rsn;
1567 lbs_deb_enter(LBS_DEB_ASSOC);
1576 cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1578 memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
1579 pos += sizeof(passo->peerstaaddr);
1581 /* set the listen interval */
1582 passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1584 pos += sizeof(passo->capability);
1585 pos += sizeof(passo->listeninterval);
1586 pos += sizeof(passo->bcnperiod);
1587 pos += sizeof(passo->dtimperiod);
1589 ssid = (struct mrvl_ie_ssid_param_set *) pos;
1590 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
1591 tmplen = bss->ssid_len;
1592 ssid->header.len = cpu_to_le16(tmplen);
1593 memcpy(ssid->ssid, bss->ssid, tmplen);
1594 pos += sizeof(ssid->header) + tmplen;
1596 ds = (struct mrvl_ie_ds_param_set *) pos;
1597 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
1598 ds->header.len = cpu_to_le16(1);
1599 ds->channel = bss->phy.ds.channel;
1600 pos += sizeof(ds->header) + 1;
1602 cf = (struct mrvl_ie_cf_param_set *) pos;
1603 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
1604 tmplen = sizeof(*cf) - sizeof (cf->header);
1605 cf->header.len = cpu_to_le16(tmplen);
1606 /* IE payload should be zeroed, firmware fills it in for us */
1609 rates = (struct mrvl_ie_rates_param_set *) pos;
1610 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
1611 memcpy(&rates->rates, &bss->rates, MAX_RATES);
1613 if (get_common_rates(priv, rates->rates, &tmplen)) {
1617 pos += sizeof(rates->header) + tmplen;
1618 rates->header.len = cpu_to_le16(tmplen);
1619 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
1621 /* Copy the infra. association rates into Current BSS state structure */
1622 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1623 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
1625 /* Set MSB on basic rates as the firmware requires, but _after_
1626 * copying to current bss rates.
1628 lbs_set_basic_rate_flags(rates->rates, tmplen);
1630 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1631 rsn = (struct mrvl_ie_rsn_param_set *) pos;
1632 /* WPA_IE or WPA2_IE */
1633 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
1634 tmplen = (u16) assoc_req->wpa_ie[1];
1635 rsn->header.len = cpu_to_le16(tmplen);
1636 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
1637 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
1638 sizeof(rsn->header) + tmplen);
1639 pos += sizeof(rsn->header) + tmplen;
1642 /* update curbssparams */
1643 priv->curbssparams.channel = bss->phy.ds.channel;
1645 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
1650 cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
1652 /* set the capability info */
1653 tmpcap = (bss->capability & CAPINFO_MASK);
1654 if (bss->mode == IW_MODE_INFRA)
1655 tmpcap |= WLAN_CAPABILITY_ESS;
1656 passo->capability = cpu_to_le16(tmpcap);
1657 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
1660 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1664 int lbs_ret_80211_associate(struct lbs_private *priv,
1665 struct cmd_ds_command *resp)
1668 union iwreq_data wrqu;
1669 struct ieee_assoc_response *passocrsp;
1670 struct bss_descriptor *bss;
1673 lbs_deb_enter(LBS_DEB_ASSOC);
1675 if (!priv->in_progress_assoc_req) {
1676 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
1680 bss = &priv->in_progress_assoc_req->bss;
1682 passocrsp = (struct ieee_assoc_response *) &resp->params;
1685 * Older FW versions map the IEEE 802.11 Status Code in the association
1686 * response to the following values returned in passocrsp->statuscode:
1688 * IEEE Status Code Marvell Status Code
1689 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1690 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1691 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1692 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1693 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1694 * others -> 0x0003 ASSOC_RESULT_REFUSED
1696 * Other response codes:
1697 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1698 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1699 * association response from the AP)
1702 status_code = le16_to_cpu(passocrsp->statuscode);
1703 switch (status_code) {
1707 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
1710 lbs_deb_assoc("ASSOC_RESP: internal timer "
1711 "expired while waiting for the AP\n");
1714 lbs_deb_assoc("ASSOC_RESP: association "
1718 lbs_deb_assoc("ASSOC_RESP: authentication "
1722 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
1723 " unknown\n", status_code);
1728 lbs_mac_event_disconnected(priv);
1733 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
1734 le16_to_cpu(resp->size) - S_DS_GEN);
1736 /* Send a Media Connected event, according to the Spec */
1737 priv->connect_status = LBS_CONNECTED;
1739 /* Update current SSID and BSSID */
1740 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1741 priv->curbssparams.ssid_len = bss->ssid_len;
1742 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1744 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
1745 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
1747 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
1748 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
1749 priv->nextSNRNF = 0;
1752 netif_carrier_on(priv->dev);
1753 if (!priv->tx_pending_len)
1754 netif_wake_queue(priv->dev);
1756 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1757 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1758 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1761 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1765 static int lbs_adhoc_post(struct lbs_private *priv, struct cmd_header *resp)
1768 u16 command = le16_to_cpu(resp->command);
1769 u16 result = le16_to_cpu(resp->result);
1770 struct cmd_ds_802_11_ad_hoc_result *adhoc_resp;
1771 union iwreq_data wrqu;
1772 struct bss_descriptor *bss;
1773 DECLARE_SSID_BUF(ssid);
1775 lbs_deb_enter(LBS_DEB_JOIN);
1777 adhoc_resp = (struct cmd_ds_802_11_ad_hoc_result *) resp;
1779 if (!priv->in_progress_assoc_req) {
1780 lbs_deb_join("ADHOC_RESP: no in-progress association "
1785 bss = &priv->in_progress_assoc_req->bss;
1788 * Join result code 0 --> SUCCESS
1791 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
1792 if (priv->connect_status == LBS_CONNECTED)
1793 lbs_mac_event_disconnected(priv);
1798 /* Send a Media Connected event, according to the Spec */
1799 priv->connect_status = LBS_CONNECTED;
1801 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
1802 /* Update the created network descriptor with the new BSSID */
1803 memcpy(bss->bssid, adhoc_resp->bssid, ETH_ALEN);
1806 /* Set the BSSID from the joined/started descriptor */
1807 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1809 /* Set the new SSID to current SSID */
1810 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1811 priv->curbssparams.ssid_len = bss->ssid_len;
1813 netif_carrier_on(priv->dev);
1814 if (!priv->tx_pending_len)
1815 netif_wake_queue(priv->dev);
1817 memset(&wrqu, 0, sizeof(wrqu));
1818 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1819 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1820 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1822 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
1823 print_ssid(ssid, bss->ssid, bss->ssid_len),
1824 priv->curbssparams.bssid,
1825 priv->curbssparams.channel);
1828 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);