1 /* Copyright (C) 2006, Red Hat, Inc. */
3 #include <linux/etherdevice.h>
11 static int lbs_adhoc_post(struct lbs_private *priv, struct cmd_header *resp);
13 static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
14 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
15 static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
16 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
18 /* The firmware needs certain bits masked out of the beacon-derviced capability
19 * field when associating/joining to BSSs.
21 #define CAPINFO_MASK (~(0xda00))
25 * @brief This function finds common rates between rates and card rates.
27 * It will fill common rates in rates as output if found.
29 * NOTE: Setting the MSB of the basic rates need to be taken
30 * care, either before or after calling this function
32 * @param priv A pointer to struct lbs_private structure
33 * @param rates the buffer which keeps input and output
34 * @param rates_size the size of rate1 buffer; new size of buffer on return
36 * @return 0 on success, or -1 on error
38 static int get_common_rates(struct lbs_private *priv,
42 u8 *card_rates = lbs_bg_rates;
43 size_t num_card_rates = sizeof(lbs_bg_rates);
48 /* For each rate in card_rates that exists in rate1, copy to tmp */
49 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
50 for (j = 0; rates[j] && (j < *rates_size); j++) {
51 if (rates[j] == card_rates[i])
52 tmp[tmp_size++] = card_rates[i];
56 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
57 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
58 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
59 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
61 if (!priv->enablehwauto) {
62 for (i = 0; i < tmp_size; i++) {
63 if (tmp[i] == priv->cur_rate)
66 lbs_pr_alert("Previously set fixed data rate %#x isn't "
67 "compatible with the network.\n", priv->cur_rate);
74 memset(rates, 0, *rates_size);
75 *rates_size = min_t(int, tmp_size, *rates_size);
76 memcpy(rates, tmp, *rates_size);
82 * @brief Sets the MSB on basic rates as the firmware requires
84 * Scan through an array and set the MSB for basic data rates.
86 * @param rates buffer of data rates
87 * @param len size of buffer
89 static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
93 for (i = 0; i < len; i++) {
94 if (rates[i] == 0x02 || rates[i] == 0x04 ||
95 rates[i] == 0x0b || rates[i] == 0x16)
102 * @brief Associate to a specific BSS discovered in a scan
104 * @param priv A pointer to struct lbs_private structure
105 * @param assoc_req The association request describing the BSS to associate with
107 * @return 0-success, otherwise fail
109 static int lbs_associate(struct lbs_private *priv,
110 struct assoc_request *assoc_req)
113 u8 preamble = RADIO_PREAMBLE_LONG;
115 lbs_deb_enter(LBS_DEB_ASSOC);
117 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
118 0, CMD_OPTION_WAITFORRSP,
119 0, assoc_req->bss.bssid);
123 /* Use short preamble only when both the BSS and firmware support it */
124 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
125 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
126 preamble = RADIO_PREAMBLE_SHORT;
128 ret = lbs_set_radio(priv, preamble, 1);
132 ret = lbs_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
133 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
136 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
141 * @brief Join an adhoc network found in a previous scan
143 * @param priv A pointer to struct lbs_private structure
144 * @param assoc_req The association request describing the BSS to join
146 * @return 0 on success, error on failure
148 static int lbs_adhoc_join(struct lbs_private *priv,
149 struct assoc_request *assoc_req)
151 struct cmd_ds_802_11_ad_hoc_join cmd;
152 struct bss_descriptor *bss = &assoc_req->bss;
153 u8 preamble = RADIO_PREAMBLE_LONG;
154 DECLARE_MAC_BUF(mac);
158 lbs_deb_enter(LBS_DEB_ASSOC);
160 lbs_deb_join("current SSID '%s', ssid length %u\n",
161 escape_essid(priv->curbssparams.ssid,
162 priv->curbssparams.ssid_len),
163 priv->curbssparams.ssid_len);
164 lbs_deb_join("requested ssid '%s', ssid length %u\n",
165 escape_essid(bss->ssid, bss->ssid_len),
168 /* check if the requested SSID is already joined */
169 if (priv->curbssparams.ssid_len &&
170 !lbs_ssid_cmp(priv->curbssparams.ssid,
171 priv->curbssparams.ssid_len,
172 bss->ssid, bss->ssid_len) &&
173 (priv->mode == IW_MODE_ADHOC) &&
174 (priv->connect_status == LBS_CONNECTED)) {
175 union iwreq_data wrqu;
177 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
178 "current, not attempting to re-join");
180 /* Send the re-association event though, because the association
181 * request really was successful, even if just a null-op.
183 memset(&wrqu, 0, sizeof(wrqu));
184 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
186 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
187 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
191 /* Use short preamble only when both the BSS and firmware support it */
192 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
193 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
194 lbs_deb_join("AdhocJoin: Short preamble\n");
195 preamble = RADIO_PREAMBLE_SHORT;
198 ret = lbs_set_radio(priv, preamble, 1);
202 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
203 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
205 priv->adhoccreate = 0;
206 priv->curbssparams.channel = bss->channel;
208 /* Build the join command */
209 memset(&cmd, 0, sizeof(cmd));
210 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
212 cmd.bss.type = CMD_BSS_TYPE_IBSS;
213 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
215 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
216 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
218 memcpy(&cmd.bss.phyparamset, &bss->phyparamset,
219 sizeof(union ieeetypes_phyparamset));
221 memcpy(&cmd.bss.ssparamset, &bss->ssparamset,
222 sizeof(union IEEEtypes_ssparamset));
224 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
225 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
226 bss->capability, CAPINFO_MASK);
228 /* information on BSSID descriptor passed to FW */
229 lbs_deb_join("ADHOC_J_CMD: BSSID = %s, SSID = '%s'\n",
230 print_mac(mac, cmd.bss.bssid), cmd.bss.ssid);
232 /* Only v8 and below support setting these */
233 if (priv->fwrelease < 0x09000000) {
235 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
237 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
240 /* Copy Data rates from the rates recorded in scan response */
241 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
242 ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
243 memcpy(cmd.bss.rates, bss->rates, ratesize);
244 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
245 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
250 /* Copy the ad-hoc creation rates into Current BSS state structure */
251 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
252 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
254 /* Set MSB on basic rates as the firmware requires, but _after_
255 * copying to current bss rates.
257 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
259 cmd.bss.ssparamset.ibssparamset.atimwindow = cpu_to_le16(bss->atimwindow);
261 if (assoc_req->secinfo.wep_enabled) {
262 u16 tmp = le16_to_cpu(cmd.bss.capability);
263 tmp |= WLAN_CAPABILITY_PRIVACY;
264 cmd.bss.capability = cpu_to_le16(tmp);
267 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
268 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
271 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
280 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
285 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
287 ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
290 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
295 * @brief Start an Adhoc Network
297 * @param priv A pointer to struct lbs_private structure
298 * @param assoc_req The association request describing the BSS to start
300 * @return 0 on success, error on failure
302 static int lbs_adhoc_start(struct lbs_private *priv,
303 struct assoc_request *assoc_req)
305 struct cmd_ds_802_11_ad_hoc_start cmd;
306 u8 preamble = RADIO_PREAMBLE_LONG;
311 lbs_deb_enter(LBS_DEB_ASSOC);
313 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
314 lbs_deb_join("ADHOC_START: Will use short preamble\n");
315 preamble = RADIO_PREAMBLE_SHORT;
318 ret = lbs_set_radio(priv, preamble, 1);
322 /* Build the start command */
323 memset(&cmd, 0, sizeof(cmd));
324 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
326 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
328 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
329 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
330 assoc_req->ssid_len);
332 cmd.bsstype = CMD_BSS_TYPE_IBSS;
334 if (priv->beacon_period == 0)
335 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
336 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
338 WARN_ON(!assoc_req->channel);
340 /* set Physical parameter set */
341 cmd.phyparamset.dsparamset.elementid = MFIE_TYPE_DS_SET;
342 cmd.phyparamset.dsparamset.len = 1;
343 cmd.phyparamset.dsparamset.currentchan = assoc_req->channel;
345 /* set IBSS parameter set */
346 cmd.ssparamset.ibssparamset.elementid = MFIE_TYPE_IBSS_SET;
347 cmd.ssparamset.ibssparamset.len = 2;
348 cmd.ssparamset.ibssparamset.atimwindow = 0;
350 /* set capability info */
351 tmpcap = WLAN_CAPABILITY_IBSS;
352 if (assoc_req->secinfo.wep_enabled) {
353 lbs_deb_join("ADHOC_START: WEP enabled, setting privacy on\n");
354 tmpcap |= WLAN_CAPABILITY_PRIVACY;
356 lbs_deb_join("ADHOC_START: WEP disabled, setting privacy off\n");
358 cmd.capability = cpu_to_le16(tmpcap);
360 /* Only v8 and below support setting probe delay */
361 if (priv->fwrelease < 0x09000000)
362 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
364 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
365 memcpy(cmd.rates, lbs_bg_rates, ratesize);
367 /* Copy the ad-hoc creating rates into Current BSS state structure */
368 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
369 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
371 /* Set MSB on basic rates as the firmware requires, but _after_
372 * copying to current bss rates.
374 lbs_set_basic_rate_flags(cmd.rates, ratesize);
376 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
377 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
379 if (lbs_create_dnld_countryinfo_11d(priv)) {
380 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
385 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
386 assoc_req->channel, assoc_req->band);
388 priv->adhoccreate = 1;
389 priv->mode = IW_MODE_ADHOC;
391 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
393 ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
396 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
401 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
403 * @param priv A pointer to struct lbs_private structure
404 * @return 0 on success, or an error
406 int lbs_adhoc_stop(struct lbs_private *priv)
408 struct cmd_ds_802_11_ad_hoc_stop cmd;
411 lbs_deb_enter(LBS_DEB_JOIN);
413 memset(&cmd, 0, sizeof (cmd));
414 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
416 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
418 /* Clean up everything even if there was an error */
419 lbs_mac_event_disconnected(priv);
421 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
425 static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
426 struct bss_descriptor *match_bss)
428 if (!secinfo->wep_enabled && !secinfo->WPAenabled
429 && !secinfo->WPA2enabled
430 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
431 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
432 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
438 static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
439 struct bss_descriptor *match_bss)
441 if (secinfo->wep_enabled && !secinfo->WPAenabled
442 && !secinfo->WPA2enabled
443 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
449 static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
450 struct bss_descriptor *match_bss)
452 if (!secinfo->wep_enabled && secinfo->WPAenabled
453 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
454 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
455 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
462 static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
463 struct bss_descriptor *match_bss)
465 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
466 (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
467 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
468 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
475 static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
476 struct bss_descriptor *match_bss)
478 if (!secinfo->wep_enabled && !secinfo->WPAenabled
479 && !secinfo->WPA2enabled
480 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
481 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
482 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
489 * @brief Check if a scanned network compatible with the driver settings
491 * WEP WPA WPA2 ad-hoc encrypt Network
492 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
493 * 0 0 0 0 NONE 0 0 0 yes No security
494 * 1 0 0 0 NONE 1 0 0 yes Static WEP
495 * 0 1 0 0 x 1x 1 x yes WPA
496 * 0 0 1 0 x 1x x 1 yes WPA2
497 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
498 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
501 * @param priv A pointer to struct lbs_private
502 * @param index Index in scantable to check against current driver settings
503 * @param mode Network mode: Infrastructure or IBSS
505 * @return Index in scantable, or error code if negative
507 static int is_network_compatible(struct lbs_private *priv,
508 struct bss_descriptor *bss, uint8_t mode)
512 lbs_deb_enter(LBS_DEB_SCAN);
514 if (bss->mode != mode)
517 matched = match_bss_no_security(&priv->secinfo, bss);
520 matched = match_bss_static_wep(&priv->secinfo, bss);
523 matched = match_bss_wpa(&priv->secinfo, bss);
525 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
526 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
527 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
528 priv->secinfo.wep_enabled ? "e" : "d",
529 priv->secinfo.WPAenabled ? "e" : "d",
530 priv->secinfo.WPA2enabled ? "e" : "d",
531 (bss->capability & WLAN_CAPABILITY_PRIVACY));
534 matched = match_bss_wpa2(&priv->secinfo, bss);
536 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
537 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
538 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
539 priv->secinfo.wep_enabled ? "e" : "d",
540 priv->secinfo.WPAenabled ? "e" : "d",
541 priv->secinfo.WPA2enabled ? "e" : "d",
542 (bss->capability & WLAN_CAPABILITY_PRIVACY));
545 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
547 lbs_deb_scan("is_network_compatible() dynamic WEP: "
548 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
549 bss->wpa_ie[0], bss->rsn_ie[0],
550 (bss->capability & WLAN_CAPABILITY_PRIVACY));
554 /* bss security settings don't match those configured on card */
555 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
556 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
557 bss->wpa_ie[0], bss->rsn_ie[0],
558 priv->secinfo.wep_enabled ? "e" : "d",
559 priv->secinfo.WPAenabled ? "e" : "d",
560 priv->secinfo.WPA2enabled ? "e" : "d",
561 (bss->capability & WLAN_CAPABILITY_PRIVACY));
564 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
569 * @brief This function finds a specific compatible BSSID in the scan list
571 * Used in association code
573 * @param priv A pointer to struct lbs_private
574 * @param bssid BSSID to find in the scan list
575 * @param mode Network mode: Infrastructure or IBSS
577 * @return index in BSSID list, or error return code (< 0)
579 static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
580 uint8_t *bssid, uint8_t mode)
582 struct bss_descriptor *iter_bss;
583 struct bss_descriptor *found_bss = NULL;
585 lbs_deb_enter(LBS_DEB_SCAN);
590 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
592 /* Look through the scan table for a compatible match. The loop will
593 * continue past a matched bssid that is not compatible in case there
594 * is an AP with multiple SSIDs assigned to the same BSSID
596 mutex_lock(&priv->lock);
597 list_for_each_entry(iter_bss, &priv->network_list, list) {
598 if (compare_ether_addr(iter_bss->bssid, bssid))
599 continue; /* bssid doesn't match */
603 if (!is_network_compatible(priv, iter_bss, mode))
605 found_bss = iter_bss;
608 found_bss = iter_bss;
612 mutex_unlock(&priv->lock);
615 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
620 * @brief This function finds ssid in ssid list.
622 * Used in association code
624 * @param priv A pointer to struct lbs_private
625 * @param ssid SSID to find in the list
626 * @param bssid BSSID to qualify the SSID selection (if provided)
627 * @param mode Network mode: Infrastructure or IBSS
629 * @return index in BSSID list
631 static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
632 uint8_t *ssid, uint8_t ssid_len,
633 uint8_t *bssid, uint8_t mode,
637 struct bss_descriptor *iter_bss = NULL;
638 struct bss_descriptor *found_bss = NULL;
639 struct bss_descriptor *tmp_oldest = NULL;
641 lbs_deb_enter(LBS_DEB_SCAN);
643 mutex_lock(&priv->lock);
645 list_for_each_entry(iter_bss, &priv->network_list, list) {
647 (iter_bss->last_scanned < tmp_oldest->last_scanned))
648 tmp_oldest = iter_bss;
650 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
651 ssid, ssid_len) != 0)
652 continue; /* ssid doesn't match */
653 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
654 continue; /* bssid doesn't match */
655 if ((channel > 0) && (iter_bss->channel != channel))
656 continue; /* channel doesn't match */
661 if (!is_network_compatible(priv, iter_bss, mode))
665 /* Found requested BSSID */
666 found_bss = iter_bss;
670 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
671 bestrssi = SCAN_RSSI(iter_bss->rssi);
672 found_bss = iter_bss;
677 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
678 bestrssi = SCAN_RSSI(iter_bss->rssi);
679 found_bss = iter_bss;
686 mutex_unlock(&priv->lock);
687 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
691 static int assoc_helper_essid(struct lbs_private *priv,
692 struct assoc_request * assoc_req)
695 struct bss_descriptor * bss;
698 lbs_deb_enter(LBS_DEB_ASSOC);
700 /* FIXME: take channel into account when picking SSIDs if a channel
704 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
705 channel = assoc_req->channel;
707 lbs_deb_assoc("SSID '%s' requested\n",
708 escape_essid(assoc_req->ssid, assoc_req->ssid_len));
709 if (assoc_req->mode == IW_MODE_INFRA) {
710 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
711 assoc_req->ssid_len);
713 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
714 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
716 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
717 ret = lbs_associate(priv, assoc_req);
719 lbs_deb_assoc("SSID not found; cannot associate\n");
721 } else if (assoc_req->mode == IW_MODE_ADHOC) {
722 /* Scan for the network, do not save previous results. Stale
723 * scan data will cause us to join a non-existant adhoc network
725 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
726 assoc_req->ssid_len);
728 /* Search for the requested SSID in the scan table */
729 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
730 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
732 lbs_deb_assoc("SSID found, will join\n");
733 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
734 lbs_adhoc_join(priv, assoc_req);
736 /* else send START command */
737 lbs_deb_assoc("SSID not found, creating adhoc network\n");
738 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
740 assoc_req->bss.ssid_len = assoc_req->ssid_len;
741 lbs_adhoc_start(priv, assoc_req);
745 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
750 static int assoc_helper_bssid(struct lbs_private *priv,
751 struct assoc_request * assoc_req)
754 struct bss_descriptor * bss;
755 DECLARE_MAC_BUF(mac);
757 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %s",
758 print_mac(mac, assoc_req->bssid));
760 /* Search for index position in list for requested MAC */
761 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
764 lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
765 "cannot associate.\n", print_mac(mac, assoc_req->bssid));
769 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
770 if (assoc_req->mode == IW_MODE_INFRA) {
771 ret = lbs_associate(priv, assoc_req);
772 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
773 } else if (assoc_req->mode == IW_MODE_ADHOC) {
774 lbs_adhoc_join(priv, assoc_req);
778 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
783 static int assoc_helper_associate(struct lbs_private *priv,
784 struct assoc_request * assoc_req)
786 int ret = 0, done = 0;
788 lbs_deb_enter(LBS_DEB_ASSOC);
790 /* If we're given and 'any' BSSID, try associating based on SSID */
792 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
793 if (compare_ether_addr(bssid_any, assoc_req->bssid)
794 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
795 ret = assoc_helper_bssid(priv, assoc_req);
800 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
801 ret = assoc_helper_essid(priv, assoc_req);
804 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
809 static int assoc_helper_mode(struct lbs_private *priv,
810 struct assoc_request * assoc_req)
814 lbs_deb_enter(LBS_DEB_ASSOC);
816 if (assoc_req->mode == priv->mode)
819 if (assoc_req->mode == IW_MODE_INFRA) {
820 if (priv->psstate != PS_STATE_FULL_POWER)
821 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
822 priv->psmode = LBS802_11POWERMODECAM;
825 priv->mode = assoc_req->mode;
826 ret = lbs_prepare_and_send_command(priv,
828 0, CMD_OPTION_WAITFORRSP,
829 OID_802_11_INFRASTRUCTURE_MODE,
830 /* Shoot me now */ (void *) (size_t) assoc_req->mode);
833 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
837 static int assoc_helper_channel(struct lbs_private *priv,
838 struct assoc_request * assoc_req)
842 lbs_deb_enter(LBS_DEB_ASSOC);
844 ret = lbs_update_channel(priv);
846 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
850 if (assoc_req->channel == priv->curbssparams.channel)
853 if (priv->mesh_dev) {
854 /* Change mesh channel first; 21.p21 firmware won't let
855 you change channel otherwise (even though it'll return
857 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
861 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
862 priv->curbssparams.channel, assoc_req->channel);
864 ret = lbs_set_channel(priv, assoc_req->channel);
866 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
868 /* FIXME: shouldn't need to grab the channel _again_ after setting
869 * it since the firmware is supposed to return the new channel, but
871 ret = lbs_update_channel(priv);
873 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
877 if (assoc_req->channel != priv->curbssparams.channel) {
878 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
883 if ( assoc_req->secinfo.wep_enabled
884 && (assoc_req->wep_keys[0].len
885 || assoc_req->wep_keys[1].len
886 || assoc_req->wep_keys[2].len
887 || assoc_req->wep_keys[3].len)) {
888 /* Make sure WEP keys are re-sent to firmware */
889 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
892 /* Must restart/rejoin adhoc networks after channel change */
893 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
897 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
898 priv->curbssparams.channel);
901 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
906 static int assoc_helper_wep_keys(struct lbs_private *priv,
907 struct assoc_request *assoc_req)
912 lbs_deb_enter(LBS_DEB_ASSOC);
914 /* Set or remove WEP keys */
915 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
916 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
917 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
919 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
924 /* enable/disable the MAC's WEP packet filter */
925 if (assoc_req->secinfo.wep_enabled)
926 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
928 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
930 lbs_set_mac_control(priv);
932 mutex_lock(&priv->lock);
934 /* Copy WEP keys into priv wep key fields */
935 for (i = 0; i < 4; i++) {
936 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
937 sizeof(struct enc_key));
939 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
941 mutex_unlock(&priv->lock);
944 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
948 static int assoc_helper_secinfo(struct lbs_private *priv,
949 struct assoc_request * assoc_req)
955 lbs_deb_enter(LBS_DEB_ASSOC);
957 memcpy(&priv->secinfo, &assoc_req->secinfo,
958 sizeof(struct lbs_802_11_security));
960 lbs_set_mac_control(priv);
962 /* If RSN is already enabled, don't try to enable it again, since
963 * ENABLE_RSN resets internal state machines and will clobber the
964 * 4-way WPA handshake.
967 /* Get RSN enabled/disabled */
968 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
970 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
974 /* Don't re-enable RSN if it's already enabled */
975 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
979 /* Set RSN enabled/disabled */
980 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
983 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
988 static int assoc_helper_wpa_keys(struct lbs_private *priv,
989 struct assoc_request * assoc_req)
992 unsigned int flags = assoc_req->flags;
994 lbs_deb_enter(LBS_DEB_ASSOC);
996 /* Work around older firmware bug where WPA unicast and multicast
997 * keys must be set independently. Seen in SDIO parts with firmware
1001 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1002 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1003 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1004 assoc_req->flags = flags;
1010 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1011 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1013 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1014 assoc_req->flags = flags;
1018 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1023 static int assoc_helper_wpa_ie(struct lbs_private *priv,
1024 struct assoc_request * assoc_req)
1028 lbs_deb_enter(LBS_DEB_ASSOC);
1030 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1031 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1032 priv->wpa_ie_len = assoc_req->wpa_ie_len;
1034 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1035 priv->wpa_ie_len = 0;
1038 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1043 static int should_deauth_infrastructure(struct lbs_private *priv,
1044 struct assoc_request * assoc_req)
1048 if (priv->connect_status != LBS_CONNECTED)
1051 lbs_deb_enter(LBS_DEB_ASSOC);
1052 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1053 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1058 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1059 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
1060 lbs_deb_assoc("Deauthenticating due to new security\n");
1066 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1067 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1072 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1073 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1078 /* FIXME: deal with 'auto' mode somehow */
1079 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1080 if (assoc_req->mode != IW_MODE_INFRA) {
1081 lbs_deb_assoc("Deauthenticating due to leaving "
1089 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1094 static int should_stop_adhoc(struct lbs_private *priv,
1095 struct assoc_request * assoc_req)
1097 lbs_deb_enter(LBS_DEB_ASSOC);
1099 if (priv->connect_status != LBS_CONNECTED)
1102 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1103 priv->curbssparams.ssid_len,
1104 assoc_req->ssid, assoc_req->ssid_len) != 0)
1107 /* FIXME: deal with 'auto' mode somehow */
1108 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1109 if (assoc_req->mode != IW_MODE_ADHOC)
1113 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1114 if (assoc_req->channel != priv->curbssparams.channel)
1118 lbs_deb_leave(LBS_DEB_ASSOC);
1124 * @brief This function finds the best SSID in the Scan List
1126 * Search the scan table for the best SSID that also matches the current
1127 * adapter network preference (infrastructure or adhoc)
1129 * @param priv A pointer to struct lbs_private
1131 * @return index in BSSID list
1133 static struct bss_descriptor *lbs_find_best_ssid_in_list(
1134 struct lbs_private *priv, uint8_t mode)
1136 uint8_t bestrssi = 0;
1137 struct bss_descriptor *iter_bss;
1138 struct bss_descriptor *best_bss = NULL;
1140 lbs_deb_enter(LBS_DEB_SCAN);
1142 mutex_lock(&priv->lock);
1144 list_for_each_entry(iter_bss, &priv->network_list, list) {
1148 if (!is_network_compatible(priv, iter_bss, mode))
1150 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1152 bestrssi = SCAN_RSSI(iter_bss->rssi);
1153 best_bss = iter_bss;
1157 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1159 bestrssi = SCAN_RSSI(iter_bss->rssi);
1160 best_bss = iter_bss;
1165 mutex_unlock(&priv->lock);
1166 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1171 * @brief Find the best AP
1173 * Used from association worker.
1175 * @param priv A pointer to struct lbs_private structure
1176 * @param pSSID A pointer to AP's ssid
1178 * @return 0--success, otherwise--fail
1180 static int lbs_find_best_network_ssid(struct lbs_private *priv,
1181 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1185 struct bss_descriptor *found;
1187 lbs_deb_enter(LBS_DEB_SCAN);
1189 priv->scan_ssid_len = 0;
1190 lbs_scan_networks(priv, 1);
1191 if (priv->surpriseremoved)
1194 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1195 if (found && (found->ssid_len > 0)) {
1196 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1197 *out_ssid_len = found->ssid_len;
1198 *out_mode = found->mode;
1203 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1208 void lbs_association_worker(struct work_struct *work)
1210 struct lbs_private *priv = container_of(work, struct lbs_private,
1212 struct assoc_request * assoc_req = NULL;
1214 int find_any_ssid = 0;
1215 DECLARE_MAC_BUF(mac);
1217 lbs_deb_enter(LBS_DEB_ASSOC);
1219 mutex_lock(&priv->lock);
1220 assoc_req = priv->pending_assoc_req;
1221 priv->pending_assoc_req = NULL;
1222 priv->in_progress_assoc_req = assoc_req;
1223 mutex_unlock(&priv->lock);
1229 "Association Request:\n"
1236 " secinfo: %s%s%s\n"
1239 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
1240 assoc_req->channel, assoc_req->band, assoc_req->mode,
1241 print_mac(mac, assoc_req->bssid),
1242 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1243 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1244 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1245 assoc_req->secinfo.auth_mode);
1247 /* If 'any' SSID was specified, find an SSID to associate with */
1248 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
1249 && !assoc_req->ssid_len)
1252 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1253 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1254 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1255 && compare_ether_addr(assoc_req->bssid, bssid_off))
1259 if (find_any_ssid) {
1260 u8 new_mode = assoc_req->mode;
1262 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
1263 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
1265 lbs_deb_assoc("Could not find best network\n");
1270 /* Ensure we switch to the mode of the AP */
1271 if (assoc_req->mode == IW_MODE_AUTO) {
1272 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1273 assoc_req->mode = new_mode;
1278 * Check if the attributes being changing require deauthentication
1279 * from the currently associated infrastructure access point.
1281 if (priv->mode == IW_MODE_INFRA) {
1282 if (should_deauth_infrastructure(priv, assoc_req)) {
1283 ret = lbs_cmd_80211_deauthenticate(priv,
1284 priv->curbssparams.bssid,
1285 WLAN_REASON_DEAUTH_LEAVING);
1287 lbs_deb_assoc("Deauthentication due to new "
1288 "configuration request failed: %d\n",
1292 } else if (priv->mode == IW_MODE_ADHOC) {
1293 if (should_stop_adhoc(priv, assoc_req)) {
1294 ret = lbs_adhoc_stop(priv);
1296 lbs_deb_assoc("Teardown of AdHoc network due to "
1297 "new configuration request failed: %d\n",
1304 /* Send the various configuration bits to the firmware */
1305 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1306 ret = assoc_helper_mode(priv, assoc_req);
1311 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1312 ret = assoc_helper_channel(priv, assoc_req);
1317 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1318 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1319 ret = assoc_helper_wep_keys(priv, assoc_req);
1324 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1325 ret = assoc_helper_secinfo(priv, assoc_req);
1330 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1331 ret = assoc_helper_wpa_ie(priv, assoc_req);
1336 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1337 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1338 ret = assoc_helper_wpa_keys(priv, assoc_req);
1343 /* SSID/BSSID should be the _last_ config option set, because they
1344 * trigger the association attempt.
1346 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1347 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1350 ret = assoc_helper_associate(priv, assoc_req);
1352 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
1357 if (priv->connect_status != LBS_CONNECTED) {
1358 lbs_deb_assoc("ASSOC: association unsuccessful, "
1364 lbs_deb_assoc("associated to %s\n",
1365 print_mac(mac, priv->curbssparams.bssid));
1366 lbs_prepare_and_send_command(priv,
1368 0, CMD_OPTION_WAITFORRSP, 0, NULL);
1376 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
1380 mutex_lock(&priv->lock);
1381 priv->in_progress_assoc_req = NULL;
1382 mutex_unlock(&priv->lock);
1386 lbs_deb_leave(LBS_DEB_ASSOC);
1391 * Caller MUST hold any necessary locks
1393 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
1395 struct assoc_request * assoc_req;
1397 lbs_deb_enter(LBS_DEB_ASSOC);
1398 if (!priv->pending_assoc_req) {
1399 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
1401 if (!priv->pending_assoc_req) {
1402 lbs_pr_info("Not enough memory to allocate association"
1408 /* Copy current configuration attributes to the association request,
1409 * but don't overwrite any that are already set.
1411 assoc_req = priv->pending_assoc_req;
1412 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1413 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
1415 assoc_req->ssid_len = priv->curbssparams.ssid_len;
1418 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1419 assoc_req->channel = priv->curbssparams.channel;
1421 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
1422 assoc_req->band = priv->curbssparams.band;
1424 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
1425 assoc_req->mode = priv->mode;
1427 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1428 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
1432 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1434 for (i = 0; i < 4; i++) {
1435 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
1436 sizeof(struct enc_key));
1440 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
1441 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
1443 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1444 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
1445 sizeof(struct enc_key));
1448 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1449 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
1450 sizeof(struct enc_key));
1453 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1454 memcpy(&assoc_req->secinfo, &priv->secinfo,
1455 sizeof(struct lbs_802_11_security));
1458 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1459 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
1461 assoc_req->wpa_ie_len = priv->wpa_ie_len;
1464 lbs_deb_leave(LBS_DEB_ASSOC);
1470 * @brief This function prepares command of authenticate.
1472 * @param priv A pointer to struct lbs_private structure
1473 * @param cmd A pointer to cmd_ds_command structure
1474 * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
1478 int lbs_cmd_80211_authenticate(struct lbs_private *priv,
1479 struct cmd_ds_command *cmd,
1482 struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
1484 u8 *bssid = pdata_buf;
1485 DECLARE_MAC_BUF(mac);
1487 lbs_deb_enter(LBS_DEB_JOIN);
1489 cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
1490 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
1493 /* translate auth mode to 802.11 defined wire value */
1494 switch (priv->secinfo.auth_mode) {
1495 case IW_AUTH_ALG_OPEN_SYSTEM:
1496 pauthenticate->authtype = 0x00;
1498 case IW_AUTH_ALG_SHARED_KEY:
1499 pauthenticate->authtype = 0x01;
1501 case IW_AUTH_ALG_LEAP:
1502 pauthenticate->authtype = 0x80;
1505 lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
1506 priv->secinfo.auth_mode);
1510 memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
1512 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
1513 print_mac(mac, bssid), pauthenticate->authtype);
1517 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1522 * @brief Deauthenticate from a specific BSS
1524 * @param priv A pointer to struct lbs_private structure
1525 * @param bssid The specific BSS to deauthenticate from
1526 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1528 * @return 0 on success, error on failure
1530 int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1533 struct cmd_ds_802_11_deauthenticate cmd;
1536 lbs_deb_enter(LBS_DEB_JOIN);
1538 memset(&cmd, 0, sizeof(cmd));
1539 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1540 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1541 cmd.reasoncode = cpu_to_le16(reason);
1543 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1545 /* Clean up everything even if there was an error; can't assume that
1546 * we're still authenticated to the AP after trying to deauth.
1548 lbs_mac_event_disconnected(priv);
1550 lbs_deb_leave(LBS_DEB_JOIN);
1554 int lbs_cmd_80211_associate(struct lbs_private *priv,
1555 struct cmd_ds_command *cmd, void *pdata_buf)
1557 struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
1559 struct assoc_request *assoc_req = pdata_buf;
1560 struct bss_descriptor *bss = &assoc_req->bss;
1563 struct mrvlietypes_ssidparamset *ssid;
1564 struct mrvlietypes_phyparamset *phy;
1565 struct mrvlietypes_ssparamset *ss;
1566 struct mrvlietypes_ratesparamset *rates;
1567 struct mrvlietypes_rsnparamset *rsn;
1569 lbs_deb_enter(LBS_DEB_ASSOC);
1578 cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1580 memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
1581 pos += sizeof(passo->peerstaaddr);
1583 /* set the listen interval */
1584 passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1586 pos += sizeof(passo->capability);
1587 pos += sizeof(passo->listeninterval);
1588 pos += sizeof(passo->bcnperiod);
1589 pos += sizeof(passo->dtimperiod);
1591 ssid = (struct mrvlietypes_ssidparamset *) pos;
1592 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
1593 tmplen = bss->ssid_len;
1594 ssid->header.len = cpu_to_le16(tmplen);
1595 memcpy(ssid->ssid, bss->ssid, tmplen);
1596 pos += sizeof(ssid->header) + tmplen;
1598 phy = (struct mrvlietypes_phyparamset *) pos;
1599 phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
1600 tmplen = sizeof(phy->fh_ds.dsparamset);
1601 phy->header.len = cpu_to_le16(tmplen);
1602 memcpy(&phy->fh_ds.dsparamset,
1603 &bss->phyparamset.dsparamset.currentchan,
1605 pos += sizeof(phy->header) + tmplen;
1607 ss = (struct mrvlietypes_ssparamset *) pos;
1608 ss->header.type = cpu_to_le16(TLV_TYPE_CF);
1609 tmplen = sizeof(ss->cf_ibss.cfparamset);
1610 ss->header.len = cpu_to_le16(tmplen);
1611 pos += sizeof(ss->header) + tmplen;
1613 rates = (struct mrvlietypes_ratesparamset *) pos;
1614 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
1615 memcpy(&rates->rates, &bss->rates, MAX_RATES);
1617 if (get_common_rates(priv, rates->rates, &tmplen)) {
1621 pos += sizeof(rates->header) + tmplen;
1622 rates->header.len = cpu_to_le16(tmplen);
1623 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
1625 /* Copy the infra. association rates into Current BSS state structure */
1626 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1627 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
1629 /* Set MSB on basic rates as the firmware requires, but _after_
1630 * copying to current bss rates.
1632 lbs_set_basic_rate_flags(rates->rates, tmplen);
1634 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1635 rsn = (struct mrvlietypes_rsnparamset *) pos;
1636 /* WPA_IE or WPA2_IE */
1637 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
1638 tmplen = (u16) assoc_req->wpa_ie[1];
1639 rsn->header.len = cpu_to_le16(tmplen);
1640 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
1641 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
1642 sizeof(rsn->header) + tmplen);
1643 pos += sizeof(rsn->header) + tmplen;
1646 /* update curbssparams */
1647 priv->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
1649 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
1654 cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
1656 /* set the capability info */
1657 tmpcap = (bss->capability & CAPINFO_MASK);
1658 if (bss->mode == IW_MODE_INFRA)
1659 tmpcap |= WLAN_CAPABILITY_ESS;
1660 passo->capability = cpu_to_le16(tmpcap);
1661 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
1664 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1668 int lbs_ret_80211_associate(struct lbs_private *priv,
1669 struct cmd_ds_command *resp)
1672 union iwreq_data wrqu;
1673 struct ieeetypes_assocrsp *passocrsp;
1674 struct bss_descriptor *bss;
1677 lbs_deb_enter(LBS_DEB_ASSOC);
1679 if (!priv->in_progress_assoc_req) {
1680 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
1684 bss = &priv->in_progress_assoc_req->bss;
1686 passocrsp = (struct ieeetypes_assocrsp *) &resp->params;
1689 * Older FW versions map the IEEE 802.11 Status Code in the association
1690 * response to the following values returned in passocrsp->statuscode:
1692 * IEEE Status Code Marvell Status Code
1693 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1694 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1695 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1696 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1697 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1698 * others -> 0x0003 ASSOC_RESULT_REFUSED
1700 * Other response codes:
1701 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1702 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1703 * association response from the AP)
1706 status_code = le16_to_cpu(passocrsp->statuscode);
1707 switch (status_code) {
1711 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
1714 lbs_deb_assoc("ASSOC_RESP: internal timer "
1715 "expired while waiting for the AP\n");
1718 lbs_deb_assoc("ASSOC_RESP: association "
1722 lbs_deb_assoc("ASSOC_RESP: authentication "
1726 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
1727 " unknown\n", status_code);
1732 lbs_mac_event_disconnected(priv);
1737 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
1738 le16_to_cpu(resp->size) - S_DS_GEN);
1740 /* Send a Media Connected event, according to the Spec */
1741 priv->connect_status = LBS_CONNECTED;
1743 /* Update current SSID and BSSID */
1744 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1745 priv->curbssparams.ssid_len = bss->ssid_len;
1746 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1748 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
1749 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
1751 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
1752 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
1753 priv->nextSNRNF = 0;
1756 netif_carrier_on(priv->dev);
1757 if (!priv->tx_pending_len)
1758 netif_wake_queue(priv->dev);
1760 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1761 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1762 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1765 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1769 static int lbs_adhoc_post(struct lbs_private *priv, struct cmd_header *resp)
1772 u16 command = le16_to_cpu(resp->command);
1773 u16 result = le16_to_cpu(resp->result);
1774 struct cmd_ds_802_11_ad_hoc_result *adhoc_resp;
1775 union iwreq_data wrqu;
1776 struct bss_descriptor *bss;
1777 DECLARE_MAC_BUF(mac);
1779 lbs_deb_enter(LBS_DEB_JOIN);
1781 adhoc_resp = (struct cmd_ds_802_11_ad_hoc_result *) resp;
1783 if (!priv->in_progress_assoc_req) {
1784 lbs_deb_join("ADHOC_RESP: no in-progress association "
1789 bss = &priv->in_progress_assoc_req->bss;
1792 * Join result code 0 --> SUCCESS
1795 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
1796 if (priv->connect_status == LBS_CONNECTED)
1797 lbs_mac_event_disconnected(priv);
1802 /* Send a Media Connected event, according to the Spec */
1803 priv->connect_status = LBS_CONNECTED;
1805 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
1806 /* Update the created network descriptor with the new BSSID */
1807 memcpy(bss->bssid, adhoc_resp->bssid, ETH_ALEN);
1810 /* Set the BSSID from the joined/started descriptor */
1811 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1813 /* Set the new SSID to current SSID */
1814 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1815 priv->curbssparams.ssid_len = bss->ssid_len;
1817 netif_carrier_on(priv->dev);
1818 if (!priv->tx_pending_len)
1819 netif_wake_queue(priv->dev);
1821 memset(&wrqu, 0, sizeof(wrqu));
1822 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1823 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1824 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1826 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %s, channel %d\n",
1827 escape_essid(bss->ssid, bss->ssid_len),
1828 print_mac(mac, priv->curbssparams.bssid),
1829 priv->curbssparams.channel);
1832 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);