libertas: move lbs_update_channel out of assoc.c
[linux-2.6/mini2440.git] / drivers / net / wireless / libertas / assoc.c
blobf353419acd02a7fe096d0ff2a8c2fdbfc07c0726
1 /* Copyright (C) 2006, Red Hat, Inc. */
3 #include <linux/etherdevice.h>
5 #include "assoc.h"
6 #include "decl.h"
7 #include "host.h"
8 #include "scan.h"
9 #include "cmd.h"
12 static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
13 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
14 static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
15 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17 /* The firmware needs certain bits masked out of the beacon-derviced capability
18 * field when associating/joining to BSSs.
20 #define CAPINFO_MASK (~(0xda00))
24 /**
25 * @brief Associate to a specific BSS discovered in a scan
27 * @param priv A pointer to struct lbs_private structure
28 * @param pbssdesc Pointer to the BSS descriptor to associate with.
30 * @return 0-success, otherwise fail
32 static int lbs_associate(struct lbs_private *priv,
33 struct assoc_request *assoc_req)
35 int ret;
37 lbs_deb_enter(LBS_DEB_ASSOC);
39 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
40 0, CMD_OPTION_WAITFORRSP,
41 0, assoc_req->bss.bssid);
43 if (ret)
44 goto done;
46 /* set preamble to firmware */
47 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
48 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
49 priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
50 else
51 priv->preamble = CMD_TYPE_LONG_PREAMBLE;
53 lbs_set_radio_control(priv);
55 ret = lbs_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
56 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
58 done:
59 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
60 return ret;
63 /**
64 * @brief Join an adhoc network found in a previous scan
66 * @param priv A pointer to struct lbs_private structure
67 * @param pbssdesc Pointer to a BSS descriptor found in a previous scan
68 * to attempt to join
70 * @return 0--success, -1--fail
72 static int lbs_join_adhoc_network(struct lbs_private *priv,
73 struct assoc_request *assoc_req)
75 struct bss_descriptor *bss = &assoc_req->bss;
76 int ret = 0;
78 lbs_deb_join("current SSID '%s', ssid length %u\n",
79 escape_essid(priv->curbssparams.ssid,
80 priv->curbssparams.ssid_len),
81 priv->curbssparams.ssid_len);
82 lbs_deb_join("requested ssid '%s', ssid length %u\n",
83 escape_essid(bss->ssid, bss->ssid_len),
84 bss->ssid_len);
86 /* check if the requested SSID is already joined */
87 if (priv->curbssparams.ssid_len &&
88 !lbs_ssid_cmp(priv->curbssparams.ssid,
89 priv->curbssparams.ssid_len,
90 bss->ssid, bss->ssid_len) &&
91 (priv->mode == IW_MODE_ADHOC) &&
92 (priv->connect_status == LBS_CONNECTED)) {
93 union iwreq_data wrqu;
95 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
96 "current, not attempting to re-join");
98 /* Send the re-association event though, because the association
99 * request really was successful, even if just a null-op.
101 memset(&wrqu, 0, sizeof(wrqu));
102 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
103 ETH_ALEN);
104 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
105 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
106 goto out;
109 /* Use shortpreamble only when both creator and card supports
110 short preamble */
111 if (!(bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) ||
112 !(priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
113 lbs_deb_join("AdhocJoin: Long preamble\n");
114 priv->preamble = CMD_TYPE_LONG_PREAMBLE;
115 } else {
116 lbs_deb_join("AdhocJoin: Short preamble\n");
117 priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
120 lbs_set_radio_control(priv);
122 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
123 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
125 priv->adhoccreate = 0;
127 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_JOIN,
128 0, CMD_OPTION_WAITFORRSP,
129 OID_802_11_SSID, assoc_req);
131 out:
132 return ret;
136 * @brief Start an Adhoc Network
138 * @param priv A pointer to struct lbs_private structure
139 * @param adhocssid The ssid of the Adhoc Network
140 * @return 0--success, -1--fail
142 static int lbs_start_adhoc_network(struct lbs_private *priv,
143 struct assoc_request *assoc_req)
145 int ret = 0;
147 priv->adhoccreate = 1;
149 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
150 lbs_deb_join("AdhocStart: Short preamble\n");
151 priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
152 } else {
153 lbs_deb_join("AdhocStart: Long preamble\n");
154 priv->preamble = CMD_TYPE_LONG_PREAMBLE;
157 lbs_set_radio_control(priv);
159 lbs_deb_join("AdhocStart: channel = %d\n", assoc_req->channel);
160 lbs_deb_join("AdhocStart: band = %d\n", assoc_req->band);
162 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_START,
163 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
165 return ret;
168 int lbs_stop_adhoc_network(struct lbs_private *priv)
170 return lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_STOP,
171 0, CMD_OPTION_WAITFORRSP, 0, NULL);
174 static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
175 struct bss_descriptor *match_bss)
177 if (!secinfo->wep_enabled && !secinfo->WPAenabled
178 && !secinfo->WPA2enabled
179 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
180 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
181 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
182 return 1;
183 else
184 return 0;
187 static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
188 struct bss_descriptor *match_bss)
190 if (secinfo->wep_enabled && !secinfo->WPAenabled
191 && !secinfo->WPA2enabled
192 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
193 return 1;
194 else
195 return 0;
198 static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
199 struct bss_descriptor *match_bss)
201 if (!secinfo->wep_enabled && secinfo->WPAenabled
202 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
203 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
204 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
206 return 1;
207 else
208 return 0;
211 static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
212 struct bss_descriptor *match_bss)
214 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
215 (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
216 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
217 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
219 return 1;
220 else
221 return 0;
224 static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
225 struct bss_descriptor *match_bss)
227 if (!secinfo->wep_enabled && !secinfo->WPAenabled
228 && !secinfo->WPA2enabled
229 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
230 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
231 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
232 return 1;
233 else
234 return 0;
238 * @brief Check if a scanned network compatible with the driver settings
240 * WEP WPA WPA2 ad-hoc encrypt Network
241 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
242 * 0 0 0 0 NONE 0 0 0 yes No security
243 * 1 0 0 0 NONE 1 0 0 yes Static WEP
244 * 0 1 0 0 x 1x 1 x yes WPA
245 * 0 0 1 0 x 1x x 1 yes WPA2
246 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
247 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
250 * @param priv A pointer to struct lbs_private
251 * @param index Index in scantable to check against current driver settings
252 * @param mode Network mode: Infrastructure or IBSS
254 * @return Index in scantable, or error code if negative
256 static int is_network_compatible(struct lbs_private *priv,
257 struct bss_descriptor *bss, uint8_t mode)
259 int matched = 0;
261 lbs_deb_enter(LBS_DEB_SCAN);
263 if (bss->mode != mode)
264 goto done;
266 matched = match_bss_no_security(&priv->secinfo, bss);
267 if (matched)
268 goto done;
269 matched = match_bss_static_wep(&priv->secinfo, bss);
270 if (matched)
271 goto done;
272 matched = match_bss_wpa(&priv->secinfo, bss);
273 if (matched) {
274 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
275 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
276 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
277 priv->secinfo.wep_enabled ? "e" : "d",
278 priv->secinfo.WPAenabled ? "e" : "d",
279 priv->secinfo.WPA2enabled ? "e" : "d",
280 (bss->capability & WLAN_CAPABILITY_PRIVACY));
281 goto done;
283 matched = match_bss_wpa2(&priv->secinfo, bss);
284 if (matched) {
285 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
286 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
287 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
288 priv->secinfo.wep_enabled ? "e" : "d",
289 priv->secinfo.WPAenabled ? "e" : "d",
290 priv->secinfo.WPA2enabled ? "e" : "d",
291 (bss->capability & WLAN_CAPABILITY_PRIVACY));
292 goto done;
294 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
295 if (matched) {
296 lbs_deb_scan("is_network_compatible() dynamic WEP: "
297 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
298 bss->wpa_ie[0], bss->rsn_ie[0],
299 (bss->capability & WLAN_CAPABILITY_PRIVACY));
300 goto done;
303 /* bss security settings don't match those configured on card */
304 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
305 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
306 bss->wpa_ie[0], bss->rsn_ie[0],
307 priv->secinfo.wep_enabled ? "e" : "d",
308 priv->secinfo.WPAenabled ? "e" : "d",
309 priv->secinfo.WPA2enabled ? "e" : "d",
310 (bss->capability & WLAN_CAPABILITY_PRIVACY));
312 done:
313 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
314 return matched;
318 * @brief This function finds a specific compatible BSSID in the scan list
320 * Used in association code
322 * @param priv A pointer to struct lbs_private
323 * @param bssid BSSID to find in the scan list
324 * @param mode Network mode: Infrastructure or IBSS
326 * @return index in BSSID list, or error return code (< 0)
328 static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
329 uint8_t *bssid, uint8_t mode)
331 struct bss_descriptor *iter_bss;
332 struct bss_descriptor *found_bss = NULL;
334 lbs_deb_enter(LBS_DEB_SCAN);
336 if (!bssid)
337 goto out;
339 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
341 /* Look through the scan table for a compatible match. The loop will
342 * continue past a matched bssid that is not compatible in case there
343 * is an AP with multiple SSIDs assigned to the same BSSID
345 mutex_lock(&priv->lock);
346 list_for_each_entry(iter_bss, &priv->network_list, list) {
347 if (compare_ether_addr(iter_bss->bssid, bssid))
348 continue; /* bssid doesn't match */
349 switch (mode) {
350 case IW_MODE_INFRA:
351 case IW_MODE_ADHOC:
352 if (!is_network_compatible(priv, iter_bss, mode))
353 break;
354 found_bss = iter_bss;
355 break;
356 default:
357 found_bss = iter_bss;
358 break;
361 mutex_unlock(&priv->lock);
363 out:
364 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
365 return found_bss;
369 * @brief This function finds ssid in ssid list.
371 * Used in association code
373 * @param priv A pointer to struct lbs_private
374 * @param ssid SSID to find in the list
375 * @param bssid BSSID to qualify the SSID selection (if provided)
376 * @param mode Network mode: Infrastructure or IBSS
378 * @return index in BSSID list
380 static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
381 uint8_t *ssid, uint8_t ssid_len,
382 uint8_t *bssid, uint8_t mode,
383 int channel)
385 u32 bestrssi = 0;
386 struct bss_descriptor *iter_bss = NULL;
387 struct bss_descriptor *found_bss = NULL;
388 struct bss_descriptor *tmp_oldest = NULL;
390 lbs_deb_enter(LBS_DEB_SCAN);
392 mutex_lock(&priv->lock);
394 list_for_each_entry(iter_bss, &priv->network_list, list) {
395 if (!tmp_oldest ||
396 (iter_bss->last_scanned < tmp_oldest->last_scanned))
397 tmp_oldest = iter_bss;
399 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
400 ssid, ssid_len) != 0)
401 continue; /* ssid doesn't match */
402 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
403 continue; /* bssid doesn't match */
404 if ((channel > 0) && (iter_bss->channel != channel))
405 continue; /* channel doesn't match */
407 switch (mode) {
408 case IW_MODE_INFRA:
409 case IW_MODE_ADHOC:
410 if (!is_network_compatible(priv, iter_bss, mode))
411 break;
413 if (bssid) {
414 /* Found requested BSSID */
415 found_bss = iter_bss;
416 goto out;
419 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
420 bestrssi = SCAN_RSSI(iter_bss->rssi);
421 found_bss = iter_bss;
423 break;
424 case IW_MODE_AUTO:
425 default:
426 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
427 bestrssi = SCAN_RSSI(iter_bss->rssi);
428 found_bss = iter_bss;
430 break;
434 out:
435 mutex_unlock(&priv->lock);
436 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
437 return found_bss;
440 static int assoc_helper_essid(struct lbs_private *priv,
441 struct assoc_request * assoc_req)
443 int ret = 0;
444 struct bss_descriptor * bss;
445 int channel = -1;
447 lbs_deb_enter(LBS_DEB_ASSOC);
449 /* FIXME: take channel into account when picking SSIDs if a channel
450 * is set.
453 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
454 channel = assoc_req->channel;
456 lbs_deb_assoc("SSID '%s' requested\n",
457 escape_essid(assoc_req->ssid, assoc_req->ssid_len));
458 if (assoc_req->mode == IW_MODE_INFRA) {
459 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
460 assoc_req->ssid_len);
462 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
463 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
464 if (bss != NULL) {
465 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
466 ret = lbs_associate(priv, assoc_req);
467 } else {
468 lbs_deb_assoc("SSID not found; cannot associate\n");
470 } else if (assoc_req->mode == IW_MODE_ADHOC) {
471 /* Scan for the network, do not save previous results. Stale
472 * scan data will cause us to join a non-existant adhoc network
474 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
475 assoc_req->ssid_len);
477 /* Search for the requested SSID in the scan table */
478 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
479 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
480 if (bss != NULL) {
481 lbs_deb_assoc("SSID found, will join\n");
482 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
483 lbs_join_adhoc_network(priv, assoc_req);
484 } else {
485 /* else send START command */
486 lbs_deb_assoc("SSID not found, creating adhoc network\n");
487 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
488 IW_ESSID_MAX_SIZE);
489 assoc_req->bss.ssid_len = assoc_req->ssid_len;
490 lbs_start_adhoc_network(priv, assoc_req);
494 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
495 return ret;
499 static int assoc_helper_bssid(struct lbs_private *priv,
500 struct assoc_request * assoc_req)
502 int ret = 0;
503 struct bss_descriptor * bss;
504 DECLARE_MAC_BUF(mac);
506 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %s",
507 print_mac(mac, assoc_req->bssid));
509 /* Search for index position in list for requested MAC */
510 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
511 assoc_req->mode);
512 if (bss == NULL) {
513 lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
514 "cannot associate.\n", print_mac(mac, assoc_req->bssid));
515 goto out;
518 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
519 if (assoc_req->mode == IW_MODE_INFRA) {
520 ret = lbs_associate(priv, assoc_req);
521 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
522 } else if (assoc_req->mode == IW_MODE_ADHOC) {
523 lbs_join_adhoc_network(priv, assoc_req);
526 out:
527 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
528 return ret;
532 static int assoc_helper_associate(struct lbs_private *priv,
533 struct assoc_request * assoc_req)
535 int ret = 0, done = 0;
537 lbs_deb_enter(LBS_DEB_ASSOC);
539 /* If we're given and 'any' BSSID, try associating based on SSID */
541 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
542 if (compare_ether_addr(bssid_any, assoc_req->bssid)
543 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
544 ret = assoc_helper_bssid(priv, assoc_req);
545 done = 1;
549 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
550 ret = assoc_helper_essid(priv, assoc_req);
553 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
554 return ret;
558 static int assoc_helper_mode(struct lbs_private *priv,
559 struct assoc_request * assoc_req)
561 int ret = 0;
563 lbs_deb_enter(LBS_DEB_ASSOC);
565 if (assoc_req->mode == priv->mode)
566 goto done;
568 if (assoc_req->mode == IW_MODE_INFRA) {
569 if (priv->psstate != PS_STATE_FULL_POWER)
570 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
571 priv->psmode = LBS802_11POWERMODECAM;
574 priv->mode = assoc_req->mode;
575 ret = lbs_prepare_and_send_command(priv,
576 CMD_802_11_SNMP_MIB,
577 0, CMD_OPTION_WAITFORRSP,
578 OID_802_11_INFRASTRUCTURE_MODE,
579 /* Shoot me now */ (void *) (size_t) assoc_req->mode);
581 done:
582 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
583 return ret;
586 static int assoc_helper_channel(struct lbs_private *priv,
587 struct assoc_request * assoc_req)
589 int ret = 0;
591 lbs_deb_enter(LBS_DEB_ASSOC);
593 ret = lbs_update_channel(priv);
594 if (ret) {
595 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
596 goto done;
599 if (assoc_req->channel == priv->curbssparams.channel)
600 goto done;
602 if (priv->mesh_dev) {
603 /* Change mesh channel first; 21.p21 firmware won't let
604 you change channel otherwise (even though it'll return
605 an error to this */
606 lbs_mesh_config(priv, 0, assoc_req->channel);
609 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
610 priv->curbssparams.channel, assoc_req->channel);
612 ret = lbs_set_channel(priv, assoc_req->channel);
613 if (ret < 0)
614 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
616 /* FIXME: shouldn't need to grab the channel _again_ after setting
617 * it since the firmware is supposed to return the new channel, but
618 * whatever... */
619 ret = lbs_update_channel(priv);
620 if (ret) {
621 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
622 goto done;
625 if (assoc_req->channel != priv->curbssparams.channel) {
626 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
627 assoc_req->channel);
628 goto restore_mesh;
631 if ( assoc_req->secinfo.wep_enabled
632 && (assoc_req->wep_keys[0].len
633 || assoc_req->wep_keys[1].len
634 || assoc_req->wep_keys[2].len
635 || assoc_req->wep_keys[3].len)) {
636 /* Make sure WEP keys are re-sent to firmware */
637 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
640 /* Must restart/rejoin adhoc networks after channel change */
641 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
643 restore_mesh:
644 if (priv->mesh_dev)
645 lbs_mesh_config(priv, 1, priv->curbssparams.channel);
647 done:
648 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
649 return ret;
653 static int assoc_helper_wep_keys(struct lbs_private *priv,
654 struct assoc_request *assoc_req)
656 int i;
657 int ret = 0;
659 lbs_deb_enter(LBS_DEB_ASSOC);
661 /* Set or remove WEP keys */
662 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
663 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
664 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
665 else
666 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
668 if (ret)
669 goto out;
671 /* enable/disable the MAC's WEP packet filter */
672 if (assoc_req->secinfo.wep_enabled)
673 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
674 else
675 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
677 lbs_set_mac_control(priv);
679 mutex_lock(&priv->lock);
681 /* Copy WEP keys into priv wep key fields */
682 for (i = 0; i < 4; i++) {
683 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
684 sizeof(struct enc_key));
686 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
688 mutex_unlock(&priv->lock);
690 out:
691 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
692 return ret;
695 static int assoc_helper_secinfo(struct lbs_private *priv,
696 struct assoc_request * assoc_req)
698 int ret = 0;
699 uint16_t do_wpa;
700 uint16_t rsn = 0;
702 lbs_deb_enter(LBS_DEB_ASSOC);
704 memcpy(&priv->secinfo, &assoc_req->secinfo,
705 sizeof(struct lbs_802_11_security));
707 lbs_set_mac_control(priv);
709 /* If RSN is already enabled, don't try to enable it again, since
710 * ENABLE_RSN resets internal state machines and will clobber the
711 * 4-way WPA handshake.
714 /* Get RSN enabled/disabled */
715 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
716 if (ret) {
717 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
718 goto out;
721 /* Don't re-enable RSN if it's already enabled */
722 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
723 if (do_wpa == rsn)
724 goto out;
726 /* Set RSN enabled/disabled */
727 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
729 out:
730 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
731 return ret;
735 static int assoc_helper_wpa_keys(struct lbs_private *priv,
736 struct assoc_request * assoc_req)
738 int ret = 0;
739 unsigned int flags = assoc_req->flags;
741 lbs_deb_enter(LBS_DEB_ASSOC);
743 /* Work around older firmware bug where WPA unicast and multicast
744 * keys must be set independently. Seen in SDIO parts with firmware
745 * version 5.0.11p0.
748 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
749 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
750 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
751 assoc_req->flags = flags;
754 if (ret)
755 goto out;
757 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
758 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
760 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
761 assoc_req->flags = flags;
764 out:
765 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
766 return ret;
770 static int assoc_helper_wpa_ie(struct lbs_private *priv,
771 struct assoc_request * assoc_req)
773 int ret = 0;
775 lbs_deb_enter(LBS_DEB_ASSOC);
777 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
778 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
779 priv->wpa_ie_len = assoc_req->wpa_ie_len;
780 } else {
781 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
782 priv->wpa_ie_len = 0;
785 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
786 return ret;
790 static int should_deauth_infrastructure(struct lbs_private *priv,
791 struct assoc_request * assoc_req)
793 int ret = 0;
795 if (priv->connect_status != LBS_CONNECTED)
796 return 0;
798 lbs_deb_enter(LBS_DEB_ASSOC);
799 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
800 lbs_deb_assoc("Deauthenticating due to new SSID\n");
801 ret = 1;
802 goto out;
805 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
806 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
807 lbs_deb_assoc("Deauthenticating due to new security\n");
808 ret = 1;
809 goto out;
813 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
814 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
815 ret = 1;
816 goto out;
819 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
820 lbs_deb_assoc("Deauthenticating due to channel switch\n");
821 ret = 1;
822 goto out;
825 /* FIXME: deal with 'auto' mode somehow */
826 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
827 if (assoc_req->mode != IW_MODE_INFRA) {
828 lbs_deb_assoc("Deauthenticating due to leaving "
829 "infra mode\n");
830 ret = 1;
831 goto out;
835 out:
836 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
837 return ret;
841 static int should_stop_adhoc(struct lbs_private *priv,
842 struct assoc_request * assoc_req)
844 lbs_deb_enter(LBS_DEB_ASSOC);
846 if (priv->connect_status != LBS_CONNECTED)
847 return 0;
849 if (lbs_ssid_cmp(priv->curbssparams.ssid,
850 priv->curbssparams.ssid_len,
851 assoc_req->ssid, assoc_req->ssid_len) != 0)
852 return 1;
854 /* FIXME: deal with 'auto' mode somehow */
855 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
856 if (assoc_req->mode != IW_MODE_ADHOC)
857 return 1;
860 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
861 if (assoc_req->channel != priv->curbssparams.channel)
862 return 1;
865 lbs_deb_leave(LBS_DEB_ASSOC);
866 return 0;
871 * @brief This function finds the best SSID in the Scan List
873 * Search the scan table for the best SSID that also matches the current
874 * adapter network preference (infrastructure or adhoc)
876 * @param priv A pointer to struct lbs_private
878 * @return index in BSSID list
880 static struct bss_descriptor *lbs_find_best_ssid_in_list(
881 struct lbs_private *priv, uint8_t mode)
883 uint8_t bestrssi = 0;
884 struct bss_descriptor *iter_bss;
885 struct bss_descriptor *best_bss = NULL;
887 lbs_deb_enter(LBS_DEB_SCAN);
889 mutex_lock(&priv->lock);
891 list_for_each_entry(iter_bss, &priv->network_list, list) {
892 switch (mode) {
893 case IW_MODE_INFRA:
894 case IW_MODE_ADHOC:
895 if (!is_network_compatible(priv, iter_bss, mode))
896 break;
897 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
898 break;
899 bestrssi = SCAN_RSSI(iter_bss->rssi);
900 best_bss = iter_bss;
901 break;
902 case IW_MODE_AUTO:
903 default:
904 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
905 break;
906 bestrssi = SCAN_RSSI(iter_bss->rssi);
907 best_bss = iter_bss;
908 break;
912 mutex_unlock(&priv->lock);
913 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
914 return best_bss;
918 * @brief Find the best AP
920 * Used from association worker.
922 * @param priv A pointer to struct lbs_private structure
923 * @param pSSID A pointer to AP's ssid
925 * @return 0--success, otherwise--fail
927 static int lbs_find_best_network_ssid(struct lbs_private *priv,
928 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
929 uint8_t *out_mode)
931 int ret = -1;
932 struct bss_descriptor *found;
934 lbs_deb_enter(LBS_DEB_SCAN);
936 priv->scan_ssid_len = 0;
937 lbs_scan_networks(priv, 1);
938 if (priv->surpriseremoved)
939 goto out;
941 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
942 if (found && (found->ssid_len > 0)) {
943 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
944 *out_ssid_len = found->ssid_len;
945 *out_mode = found->mode;
946 ret = 0;
949 out:
950 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
951 return ret;
955 void lbs_association_worker(struct work_struct *work)
957 struct lbs_private *priv = container_of(work, struct lbs_private,
958 assoc_work.work);
959 struct assoc_request * assoc_req = NULL;
960 int ret = 0;
961 int find_any_ssid = 0;
962 DECLARE_MAC_BUF(mac);
964 lbs_deb_enter(LBS_DEB_ASSOC);
966 mutex_lock(&priv->lock);
967 assoc_req = priv->pending_assoc_req;
968 priv->pending_assoc_req = NULL;
969 priv->in_progress_assoc_req = assoc_req;
970 mutex_unlock(&priv->lock);
972 if (!assoc_req)
973 goto done;
975 lbs_deb_assoc(
976 "Association Request:\n"
977 " flags: 0x%08lx\n"
978 " SSID: '%s'\n"
979 " chann: %d\n"
980 " band: %d\n"
981 " mode: %d\n"
982 " BSSID: %s\n"
983 " secinfo: %s%s%s\n"
984 " auth_mode: %d\n",
985 assoc_req->flags,
986 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
987 assoc_req->channel, assoc_req->band, assoc_req->mode,
988 print_mac(mac, assoc_req->bssid),
989 assoc_req->secinfo.WPAenabled ? " WPA" : "",
990 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
991 assoc_req->secinfo.wep_enabled ? " WEP" : "",
992 assoc_req->secinfo.auth_mode);
994 /* If 'any' SSID was specified, find an SSID to associate with */
995 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
996 && !assoc_req->ssid_len)
997 find_any_ssid = 1;
999 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1000 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1001 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1002 && compare_ether_addr(assoc_req->bssid, bssid_off))
1003 find_any_ssid = 0;
1006 if (find_any_ssid) {
1007 u8 new_mode;
1009 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
1010 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
1011 if (ret) {
1012 lbs_deb_assoc("Could not find best network\n");
1013 ret = -ENETUNREACH;
1014 goto out;
1017 /* Ensure we switch to the mode of the AP */
1018 if (assoc_req->mode == IW_MODE_AUTO) {
1019 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1020 assoc_req->mode = new_mode;
1025 * Check if the attributes being changing require deauthentication
1026 * from the currently associated infrastructure access point.
1028 if (priv->mode == IW_MODE_INFRA) {
1029 if (should_deauth_infrastructure(priv, assoc_req)) {
1030 ret = lbs_send_deauthentication(priv);
1031 if (ret) {
1032 lbs_deb_assoc("Deauthentication due to new "
1033 "configuration request failed: %d\n",
1034 ret);
1037 } else if (priv->mode == IW_MODE_ADHOC) {
1038 if (should_stop_adhoc(priv, assoc_req)) {
1039 ret = lbs_stop_adhoc_network(priv);
1040 if (ret) {
1041 lbs_deb_assoc("Teardown of AdHoc network due to "
1042 "new configuration request failed: %d\n",
1043 ret);
1049 /* Send the various configuration bits to the firmware */
1050 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1051 ret = assoc_helper_mode(priv, assoc_req);
1052 if (ret)
1053 goto out;
1056 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1057 ret = assoc_helper_channel(priv, assoc_req);
1058 if (ret)
1059 goto out;
1062 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1063 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1064 ret = assoc_helper_wep_keys(priv, assoc_req);
1065 if (ret)
1066 goto out;
1069 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1070 ret = assoc_helper_secinfo(priv, assoc_req);
1071 if (ret)
1072 goto out;
1075 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1076 ret = assoc_helper_wpa_ie(priv, assoc_req);
1077 if (ret)
1078 goto out;
1081 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1082 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1083 ret = assoc_helper_wpa_keys(priv, assoc_req);
1084 if (ret)
1085 goto out;
1088 /* SSID/BSSID should be the _last_ config option set, because they
1089 * trigger the association attempt.
1091 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1092 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1093 int success = 1;
1095 ret = assoc_helper_associate(priv, assoc_req);
1096 if (ret) {
1097 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
1098 ret);
1099 success = 0;
1102 if (priv->connect_status != LBS_CONNECTED) {
1103 lbs_deb_assoc("ASSOC: association unsuccessful, "
1104 "not connected\n");
1105 success = 0;
1108 if (success) {
1109 lbs_deb_assoc("associated to %s\n",
1110 print_mac(mac, priv->curbssparams.bssid));
1111 lbs_prepare_and_send_command(priv,
1112 CMD_802_11_RSSI,
1113 0, CMD_OPTION_WAITFORRSP, 0, NULL);
1114 } else {
1115 ret = -1;
1119 out:
1120 if (ret) {
1121 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
1122 ret);
1125 mutex_lock(&priv->lock);
1126 priv->in_progress_assoc_req = NULL;
1127 mutex_unlock(&priv->lock);
1128 kfree(assoc_req);
1130 done:
1131 lbs_deb_leave(LBS_DEB_ASSOC);
1136 * Caller MUST hold any necessary locks
1138 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
1140 struct assoc_request * assoc_req;
1142 lbs_deb_enter(LBS_DEB_ASSOC);
1143 if (!priv->pending_assoc_req) {
1144 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
1145 GFP_KERNEL);
1146 if (!priv->pending_assoc_req) {
1147 lbs_pr_info("Not enough memory to allocate association"
1148 " request!\n");
1149 return NULL;
1153 /* Copy current configuration attributes to the association request,
1154 * but don't overwrite any that are already set.
1156 assoc_req = priv->pending_assoc_req;
1157 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1158 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
1159 IW_ESSID_MAX_SIZE);
1160 assoc_req->ssid_len = priv->curbssparams.ssid_len;
1163 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1164 assoc_req->channel = priv->curbssparams.channel;
1166 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
1167 assoc_req->band = priv->curbssparams.band;
1169 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
1170 assoc_req->mode = priv->mode;
1172 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1173 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
1174 ETH_ALEN);
1177 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1178 int i;
1179 for (i = 0; i < 4; i++) {
1180 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
1181 sizeof(struct enc_key));
1185 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
1186 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
1188 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1189 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
1190 sizeof(struct enc_key));
1193 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1194 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
1195 sizeof(struct enc_key));
1198 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1199 memcpy(&assoc_req->secinfo, &priv->secinfo,
1200 sizeof(struct lbs_802_11_security));
1203 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1204 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
1205 MAX_WPA_IE_LEN);
1206 assoc_req->wpa_ie_len = priv->wpa_ie_len;
1209 lbs_deb_leave(LBS_DEB_ASSOC);
1210 return assoc_req;
1215 * @brief This function finds common rates between rate1 and card rates.
1217 * It will fill common rates in rate1 as output if found.
1219 * NOTE: Setting the MSB of the basic rates need to be taken
1220 * care, either before or after calling this function
1222 * @param priv A pointer to struct lbs_private structure
1223 * @param rate1 the buffer which keeps input and output
1224 * @param rate1_size the size of rate1 buffer; new size of buffer on return
1226 * @return 0 or -1
1228 static int get_common_rates(struct lbs_private *priv,
1229 u8 *rates,
1230 u16 *rates_size)
1232 u8 *card_rates = lbs_bg_rates;
1233 size_t num_card_rates = sizeof(lbs_bg_rates);
1234 int ret = 0, i, j;
1235 u8 tmp[30];
1236 size_t tmp_size = 0;
1238 /* For each rate in card_rates that exists in rate1, copy to tmp */
1239 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
1240 for (j = 0; rates[j] && (j < *rates_size); j++) {
1241 if (rates[j] == card_rates[i])
1242 tmp[tmp_size++] = card_rates[i];
1246 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
1247 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
1248 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
1249 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
1251 if (!priv->auto_rate) {
1252 for (i = 0; i < tmp_size; i++) {
1253 if (tmp[i] == priv->cur_rate)
1254 goto done;
1256 lbs_pr_alert("Previously set fixed data rate %#x isn't "
1257 "compatible with the network.\n", priv->cur_rate);
1258 ret = -1;
1259 goto done;
1261 ret = 0;
1263 done:
1264 memset(rates, 0, *rates_size);
1265 *rates_size = min_t(int, tmp_size, *rates_size);
1266 memcpy(rates, tmp, *rates_size);
1267 return ret;
1272 * @brief Sets the MSB on basic rates as the firmware requires
1274 * Scan through an array and set the MSB for basic data rates.
1276 * @param rates buffer of data rates
1277 * @param len size of buffer
1279 static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
1281 int i;
1283 for (i = 0; i < len; i++) {
1284 if (rates[i] == 0x02 || rates[i] == 0x04 ||
1285 rates[i] == 0x0b || rates[i] == 0x16)
1286 rates[i] |= 0x80;
1291 * @brief Send Deauthentication Request
1293 * @param priv A pointer to struct lbs_private structure
1294 * @return 0--success, -1--fail
1296 int lbs_send_deauthentication(struct lbs_private *priv)
1298 return lbs_prepare_and_send_command(priv, CMD_802_11_DEAUTHENTICATE,
1299 0, CMD_OPTION_WAITFORRSP, 0, NULL);
1303 * @brief This function prepares command of authenticate.
1305 * @param priv A pointer to struct lbs_private structure
1306 * @param cmd A pointer to cmd_ds_command structure
1307 * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
1309 * @return 0 or -1
1311 int lbs_cmd_80211_authenticate(struct lbs_private *priv,
1312 struct cmd_ds_command *cmd,
1313 void *pdata_buf)
1315 struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
1316 int ret = -1;
1317 u8 *bssid = pdata_buf;
1318 DECLARE_MAC_BUF(mac);
1320 lbs_deb_enter(LBS_DEB_JOIN);
1322 cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
1323 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
1324 + S_DS_GEN);
1326 /* translate auth mode to 802.11 defined wire value */
1327 switch (priv->secinfo.auth_mode) {
1328 case IW_AUTH_ALG_OPEN_SYSTEM:
1329 pauthenticate->authtype = 0x00;
1330 break;
1331 case IW_AUTH_ALG_SHARED_KEY:
1332 pauthenticate->authtype = 0x01;
1333 break;
1334 case IW_AUTH_ALG_LEAP:
1335 pauthenticate->authtype = 0x80;
1336 break;
1337 default:
1338 lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
1339 priv->secinfo.auth_mode);
1340 goto out;
1343 memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
1345 lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
1346 print_mac(mac, bssid), pauthenticate->authtype);
1347 ret = 0;
1349 out:
1350 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1351 return ret;
1354 int lbs_cmd_80211_deauthenticate(struct lbs_private *priv,
1355 struct cmd_ds_command *cmd)
1357 struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
1359 lbs_deb_enter(LBS_DEB_JOIN);
1361 cmd->command = cpu_to_le16(CMD_802_11_DEAUTHENTICATE);
1362 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
1363 S_DS_GEN);
1365 /* set AP MAC address */
1366 memmove(dauth->macaddr, priv->curbssparams.bssid, ETH_ALEN);
1368 /* Reason code 3 = Station is leaving */
1369 #define REASON_CODE_STA_LEAVING 3
1370 dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
1372 lbs_deb_leave(LBS_DEB_JOIN);
1373 return 0;
1376 int lbs_cmd_80211_associate(struct lbs_private *priv,
1377 struct cmd_ds_command *cmd, void *pdata_buf)
1379 struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
1380 int ret = 0;
1381 struct assoc_request *assoc_req = pdata_buf;
1382 struct bss_descriptor *bss = &assoc_req->bss;
1383 u8 *pos;
1384 u16 tmpcap, tmplen;
1385 struct mrvlietypes_ssidparamset *ssid;
1386 struct mrvlietypes_phyparamset *phy;
1387 struct mrvlietypes_ssparamset *ss;
1388 struct mrvlietypes_ratesparamset *rates;
1389 struct mrvlietypes_rsnparamset *rsn;
1391 lbs_deb_enter(LBS_DEB_ASSOC);
1393 pos = (u8 *) passo;
1395 if (!priv) {
1396 ret = -1;
1397 goto done;
1400 cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1402 memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
1403 pos += sizeof(passo->peerstaaddr);
1405 /* set the listen interval */
1406 passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1408 pos += sizeof(passo->capability);
1409 pos += sizeof(passo->listeninterval);
1410 pos += sizeof(passo->bcnperiod);
1411 pos += sizeof(passo->dtimperiod);
1413 ssid = (struct mrvlietypes_ssidparamset *) pos;
1414 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
1415 tmplen = bss->ssid_len;
1416 ssid->header.len = cpu_to_le16(tmplen);
1417 memcpy(ssid->ssid, bss->ssid, tmplen);
1418 pos += sizeof(ssid->header) + tmplen;
1420 phy = (struct mrvlietypes_phyparamset *) pos;
1421 phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
1422 tmplen = sizeof(phy->fh_ds.dsparamset);
1423 phy->header.len = cpu_to_le16(tmplen);
1424 memcpy(&phy->fh_ds.dsparamset,
1425 &bss->phyparamset.dsparamset.currentchan,
1426 tmplen);
1427 pos += sizeof(phy->header) + tmplen;
1429 ss = (struct mrvlietypes_ssparamset *) pos;
1430 ss->header.type = cpu_to_le16(TLV_TYPE_CF);
1431 tmplen = sizeof(ss->cf_ibss.cfparamset);
1432 ss->header.len = cpu_to_le16(tmplen);
1433 pos += sizeof(ss->header) + tmplen;
1435 rates = (struct mrvlietypes_ratesparamset *) pos;
1436 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
1437 memcpy(&rates->rates, &bss->rates, MAX_RATES);
1438 tmplen = MAX_RATES;
1439 if (get_common_rates(priv, rates->rates, &tmplen)) {
1440 ret = -1;
1441 goto done;
1443 pos += sizeof(rates->header) + tmplen;
1444 rates->header.len = cpu_to_le16(tmplen);
1445 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
1447 /* Copy the infra. association rates into Current BSS state structure */
1448 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1449 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
1451 /* Set MSB on basic rates as the firmware requires, but _after_
1452 * copying to current bss rates.
1454 lbs_set_basic_rate_flags(rates->rates, tmplen);
1456 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1457 rsn = (struct mrvlietypes_rsnparamset *) pos;
1458 /* WPA_IE or WPA2_IE */
1459 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
1460 tmplen = (u16) assoc_req->wpa_ie[1];
1461 rsn->header.len = cpu_to_le16(tmplen);
1462 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
1463 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
1464 sizeof(rsn->header) + tmplen);
1465 pos += sizeof(rsn->header) + tmplen;
1468 /* update curbssparams */
1469 priv->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
1471 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
1472 ret = -1;
1473 goto done;
1476 cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
1478 /* set the capability info */
1479 tmpcap = (bss->capability & CAPINFO_MASK);
1480 if (bss->mode == IW_MODE_INFRA)
1481 tmpcap |= WLAN_CAPABILITY_ESS;
1482 passo->capability = cpu_to_le16(tmpcap);
1483 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
1485 done:
1486 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1487 return ret;
1490 int lbs_cmd_80211_ad_hoc_start(struct lbs_private *priv,
1491 struct cmd_ds_command *cmd, void *pdata_buf)
1493 struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
1494 int ret = 0;
1495 int cmdappendsize = 0;
1496 struct assoc_request *assoc_req = pdata_buf;
1497 u16 tmpcap = 0;
1498 size_t ratesize = 0;
1500 lbs_deb_enter(LBS_DEB_JOIN);
1502 if (!priv) {
1503 ret = -1;
1504 goto done;
1507 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
1510 * Fill in the parameters for 2 data structures:
1511 * 1. cmd_ds_802_11_ad_hoc_start command
1512 * 2. priv->scantable[i]
1514 * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
1515 * probe delay, and cap info.
1517 * Firmware will fill up beacon period, DTIM, Basic rates
1518 * and operational rates.
1521 memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
1522 memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
1524 lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
1525 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
1526 assoc_req->ssid_len);
1528 /* set the BSS type */
1529 adhs->bsstype = CMD_BSS_TYPE_IBSS;
1530 priv->mode = IW_MODE_ADHOC;
1531 if (priv->beacon_period == 0)
1532 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
1533 adhs->beaconperiod = cpu_to_le16(priv->beacon_period);
1535 /* set Physical param set */
1536 #define DS_PARA_IE_ID 3
1537 #define DS_PARA_IE_LEN 1
1539 adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
1540 adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
1542 WARN_ON(!assoc_req->channel);
1544 lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
1545 assoc_req->channel);
1547 adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
1549 /* set IBSS param set */
1550 #define IBSS_PARA_IE_ID 6
1551 #define IBSS_PARA_IE_LEN 2
1553 adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
1554 adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
1555 adhs->ssparamset.ibssparamset.atimwindow = 0;
1557 /* set capability info */
1558 tmpcap = WLAN_CAPABILITY_IBSS;
1559 if (assoc_req->secinfo.wep_enabled) {
1560 lbs_deb_join("ADHOC_S_CMD: WEP enabled, "
1561 "setting privacy on\n");
1562 tmpcap |= WLAN_CAPABILITY_PRIVACY;
1563 } else {
1564 lbs_deb_join("ADHOC_S_CMD: WEP disabled, "
1565 "setting privacy off\n");
1567 adhs->capability = cpu_to_le16(tmpcap);
1569 /* probedelay */
1570 adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1572 memset(adhs->rates, 0, sizeof(adhs->rates));
1573 ratesize = min(sizeof(adhs->rates), sizeof(lbs_bg_rates));
1574 memcpy(adhs->rates, lbs_bg_rates, ratesize);
1576 /* Copy the ad-hoc creating rates into Current BSS state structure */
1577 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1578 memcpy(&priv->curbssparams.rates, &adhs->rates, ratesize);
1580 /* Set MSB on basic rates as the firmware requires, but _after_
1581 * copying to current bss rates.
1583 lbs_set_basic_rate_flags(adhs->rates, ratesize);
1585 lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
1586 adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
1588 lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
1590 if (lbs_create_dnld_countryinfo_11d(priv)) {
1591 lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
1592 ret = -1;
1593 goto done;
1596 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
1597 S_DS_GEN + cmdappendsize);
1599 ret = 0;
1600 done:
1601 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1602 return ret;
1605 int lbs_cmd_80211_ad_hoc_stop(struct cmd_ds_command *cmd)
1607 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
1608 cmd->size = cpu_to_le16(S_DS_GEN);
1610 return 0;
1613 int lbs_cmd_80211_ad_hoc_join(struct lbs_private *priv,
1614 struct cmd_ds_command *cmd, void *pdata_buf)
1616 struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
1617 struct assoc_request *assoc_req = pdata_buf;
1618 struct bss_descriptor *bss = &assoc_req->bss;
1619 int cmdappendsize = 0;
1620 int ret = 0;
1621 u16 ratesize = 0;
1622 DECLARE_MAC_BUF(mac);
1624 lbs_deb_enter(LBS_DEB_JOIN);
1626 cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
1628 join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
1629 join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
1631 memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
1632 memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
1634 memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
1635 sizeof(union ieeetypes_phyparamset));
1637 memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
1638 sizeof(union IEEEtypes_ssparamset));
1640 join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
1641 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
1642 bss->capability, CAPINFO_MASK);
1644 /* information on BSSID descriptor passed to FW */
1645 lbs_deb_join(
1646 "ADHOC_J_CMD: BSSID = %s, SSID = '%s'\n",
1647 print_mac(mac, join_cmd->bss.bssid),
1648 join_cmd->bss.ssid);
1650 /* failtimeout */
1651 join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1653 /* probedelay */
1654 join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1656 priv->curbssparams.channel = bss->channel;
1658 /* Copy Data rates from the rates recorded in scan response */
1659 memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
1660 ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
1661 memcpy(join_cmd->bss.rates, bss->rates, ratesize);
1662 if (get_common_rates(priv, join_cmd->bss.rates, &ratesize)) {
1663 lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
1664 ret = -1;
1665 goto done;
1668 /* Copy the ad-hoc creating rates into Current BSS state structure */
1669 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1670 memcpy(&priv->curbssparams.rates, join_cmd->bss.rates, ratesize);
1672 /* Set MSB on basic rates as the firmware requires, but _after_
1673 * copying to current bss rates.
1675 lbs_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
1677 join_cmd->bss.ssparamset.ibssparamset.atimwindow =
1678 cpu_to_le16(bss->atimwindow);
1680 if (assoc_req->secinfo.wep_enabled) {
1681 u16 tmp = le16_to_cpu(join_cmd->bss.capability);
1682 tmp |= WLAN_CAPABILITY_PRIVACY;
1683 join_cmd->bss.capability = cpu_to_le16(tmp);
1686 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1687 /* wake up first */
1688 __le32 Localpsmode;
1690 Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
1691 ret = lbs_prepare_and_send_command(priv,
1692 CMD_802_11_PS_MODE,
1693 CMD_ACT_SET,
1694 0, 0, &Localpsmode);
1696 if (ret) {
1697 ret = -1;
1698 goto done;
1702 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
1703 ret = -1;
1704 goto done;
1707 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
1708 S_DS_GEN + cmdappendsize);
1710 done:
1711 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1712 return ret;
1715 int lbs_ret_80211_associate(struct lbs_private *priv,
1716 struct cmd_ds_command *resp)
1718 int ret = 0;
1719 union iwreq_data wrqu;
1720 struct ieeetypes_assocrsp *passocrsp;
1721 struct bss_descriptor *bss;
1722 u16 status_code;
1724 lbs_deb_enter(LBS_DEB_ASSOC);
1726 if (!priv->in_progress_assoc_req) {
1727 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
1728 ret = -1;
1729 goto done;
1731 bss = &priv->in_progress_assoc_req->bss;
1733 passocrsp = (struct ieeetypes_assocrsp *) &resp->params;
1736 * Older FW versions map the IEEE 802.11 Status Code in the association
1737 * response to the following values returned in passocrsp->statuscode:
1739 * IEEE Status Code Marvell Status Code
1740 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1741 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1742 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1743 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1744 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1745 * others -> 0x0003 ASSOC_RESULT_REFUSED
1747 * Other response codes:
1748 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1749 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1750 * association response from the AP)
1753 status_code = le16_to_cpu(passocrsp->statuscode);
1754 switch (status_code) {
1755 case 0x00:
1756 break;
1757 case 0x01:
1758 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
1759 break;
1760 case 0x02:
1761 lbs_deb_assoc("ASSOC_RESP: internal timer "
1762 "expired while waiting for the AP\n");
1763 break;
1764 case 0x03:
1765 lbs_deb_assoc("ASSOC_RESP: association "
1766 "refused by AP\n");
1767 break;
1768 case 0x04:
1769 lbs_deb_assoc("ASSOC_RESP: authentication "
1770 "refused by AP\n");
1771 break;
1772 default:
1773 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
1774 " unknown\n", status_code);
1775 break;
1778 if (status_code) {
1779 lbs_mac_event_disconnected(priv);
1780 ret = -1;
1781 goto done;
1784 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
1785 le16_to_cpu(resp->size) - S_DS_GEN);
1787 /* Send a Media Connected event, according to the Spec */
1788 priv->connect_status = LBS_CONNECTED;
1790 /* Update current SSID and BSSID */
1791 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1792 priv->curbssparams.ssid_len = bss->ssid_len;
1793 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1795 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
1796 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
1798 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
1799 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
1800 priv->nextSNRNF = 0;
1801 priv->numSNRNF = 0;
1803 netif_carrier_on(priv->dev);
1804 if (!priv->tx_pending_len)
1805 netif_wake_queue(priv->dev);
1807 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1808 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1809 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1811 done:
1812 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1813 return ret;
1816 int lbs_ret_80211_disassociate(struct lbs_private *priv)
1818 lbs_deb_enter(LBS_DEB_JOIN);
1820 lbs_mac_event_disconnected(priv);
1822 lbs_deb_leave(LBS_DEB_JOIN);
1823 return 0;
1826 int lbs_ret_80211_ad_hoc_start(struct lbs_private *priv,
1827 struct cmd_ds_command *resp)
1829 int ret = 0;
1830 u16 command = le16_to_cpu(resp->command);
1831 u16 result = le16_to_cpu(resp->result);
1832 struct cmd_ds_802_11_ad_hoc_result *padhocresult;
1833 union iwreq_data wrqu;
1834 struct bss_descriptor *bss;
1835 DECLARE_MAC_BUF(mac);
1837 lbs_deb_enter(LBS_DEB_JOIN);
1839 padhocresult = &resp->params.result;
1841 lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
1842 lbs_deb_join("ADHOC_RESP: command = %x\n", command);
1843 lbs_deb_join("ADHOC_RESP: result = %x\n", result);
1845 if (!priv->in_progress_assoc_req) {
1846 lbs_deb_join("ADHOC_RESP: no in-progress association "
1847 "request\n");
1848 ret = -1;
1849 goto done;
1851 bss = &priv->in_progress_assoc_req->bss;
1854 * Join result code 0 --> SUCCESS
1856 if (result) {
1857 lbs_deb_join("ADHOC_RESP: failed\n");
1858 if (priv->connect_status == LBS_CONNECTED)
1859 lbs_mac_event_disconnected(priv);
1860 ret = -1;
1861 goto done;
1865 * Now the join cmd should be successful
1866 * If BSSID has changed use SSID to compare instead of BSSID
1868 lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
1869 escape_essid(bss->ssid, bss->ssid_len));
1871 /* Send a Media Connected event, according to the Spec */
1872 priv->connect_status = LBS_CONNECTED;
1874 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
1875 /* Update the created network descriptor with the new BSSID */
1876 memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
1879 /* Set the BSSID from the joined/started descriptor */
1880 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1882 /* Set the new SSID to current SSID */
1883 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1884 priv->curbssparams.ssid_len = bss->ssid_len;
1886 netif_carrier_on(priv->dev);
1887 if (!priv->tx_pending_len)
1888 netif_wake_queue(priv->dev);
1890 memset(&wrqu, 0, sizeof(wrqu));
1891 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1892 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1893 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1895 lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
1896 lbs_deb_join("ADHOC_RESP: channel = %d\n", priv->curbssparams.channel);
1897 lbs_deb_join("ADHOC_RESP: BSSID = %s\n",
1898 print_mac(mac, padhocresult->bssid));
1900 done:
1901 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1902 return ret;
1905 int lbs_ret_80211_ad_hoc_stop(struct lbs_private *priv)
1907 lbs_deb_enter(LBS_DEB_JOIN);
1909 lbs_mac_event_disconnected(priv);
1911 lbs_deb_leave(LBS_DEB_JOIN);
1912 return 0;