1 /* Copyright (C) 2006, Red Hat, Inc. */
3 #include <linux/bitops.h>
4 #include <net/ieee80211.h>
5 #include <linux/etherdevice.h>
14 static const u8 bssid_any[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
15 static const u8 bssid_off[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
18 static int assoc_helper_essid(struct lbs_private *priv,
19 struct assoc_request * assoc_req)
21 struct lbs_adapter *adapter = priv->adapter;
23 struct bss_descriptor * bss;
26 lbs_deb_enter(LBS_DEB_ASSOC);
28 /* FIXME: take channel into account when picking SSIDs if a channel
32 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
33 channel = assoc_req->channel;
35 lbs_deb_assoc("SSID '%s' requested\n",
36 escape_essid(assoc_req->ssid, assoc_req->ssid_len));
37 if (assoc_req->mode == IW_MODE_INFRA) {
38 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
39 assoc_req->ssid_len, 0);
41 bss = lbs_find_ssid_in_list(adapter, assoc_req->ssid,
42 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
44 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
45 ret = lbs_associate(priv, assoc_req);
47 lbs_deb_assoc("SSID not found; cannot associate\n");
49 } else if (assoc_req->mode == IW_MODE_ADHOC) {
50 /* Scan for the network, do not save previous results. Stale
51 * scan data will cause us to join a non-existant adhoc network
53 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
54 assoc_req->ssid_len, 1);
56 /* Search for the requested SSID in the scan table */
57 bss = lbs_find_ssid_in_list(adapter, assoc_req->ssid,
58 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
60 lbs_deb_assoc("SSID found, will join\n");
61 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
62 lbs_join_adhoc_network(priv, assoc_req);
64 /* else send START command */
65 lbs_deb_assoc("SSID not found, creating adhoc network\n");
66 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
68 assoc_req->bss.ssid_len = assoc_req->ssid_len;
69 lbs_start_adhoc_network(priv, assoc_req);
73 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
78 static int assoc_helper_bssid(struct lbs_private *priv,
79 struct assoc_request * assoc_req)
81 struct lbs_adapter *adapter = priv->adapter;
83 struct bss_descriptor * bss;
86 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %s",
87 print_mac(mac, assoc_req->bssid));
89 /* Search for index position in list for requested MAC */
90 bss = lbs_find_bssid_in_list(adapter, assoc_req->bssid,
93 lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
94 "cannot associate.\n", print_mac(mac, assoc_req->bssid));
98 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
99 if (assoc_req->mode == IW_MODE_INFRA) {
100 ret = lbs_associate(priv, assoc_req);
101 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
102 } else if (assoc_req->mode == IW_MODE_ADHOC) {
103 lbs_join_adhoc_network(priv, assoc_req);
107 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
112 static int assoc_helper_associate(struct lbs_private *priv,
113 struct assoc_request * assoc_req)
115 int ret = 0, done = 0;
117 lbs_deb_enter(LBS_DEB_ASSOC);
119 /* If we're given and 'any' BSSID, try associating based on SSID */
121 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
122 if (compare_ether_addr(bssid_any, assoc_req->bssid)
123 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
124 ret = assoc_helper_bssid(priv, assoc_req);
129 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
130 ret = assoc_helper_essid(priv, assoc_req);
133 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
138 static int assoc_helper_mode(struct lbs_private *priv,
139 struct assoc_request * assoc_req)
141 struct lbs_adapter *adapter = priv->adapter;
144 lbs_deb_enter(LBS_DEB_ASSOC);
146 if (assoc_req->mode == adapter->mode)
149 if (assoc_req->mode == IW_MODE_INFRA) {
150 if (adapter->psstate != PS_STATE_FULL_POWER)
151 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
152 adapter->psmode = LBS802_11POWERMODECAM;
155 adapter->mode = assoc_req->mode;
156 ret = lbs_prepare_and_send_command(priv,
158 0, CMD_OPTION_WAITFORRSP,
159 OID_802_11_INFRASTRUCTURE_MODE,
160 /* Shoot me now */ (void *) (size_t) assoc_req->mode);
163 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
168 static int update_channel(struct lbs_private *priv)
171 /* the channel in f/w could be out of sync, get the current channel */
172 lbs_deb_enter(LBS_DEB_ASSOC);
173 ret = lbs_prepare_and_send_command(priv, CMD_802_11_RF_CHANNEL,
174 CMD_OPT_802_11_RF_CHANNEL_GET,
175 CMD_OPTION_WAITFORRSP, 0, NULL);
176 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
180 void lbs_sync_channel(struct work_struct *work)
182 struct lbs_private *priv = container_of(work, struct lbs_private,
185 lbs_deb_enter(LBS_DEB_ASSOC);
186 if (update_channel(priv) != 0)
187 lbs_pr_info("Channel synchronization failed.");
188 lbs_deb_leave(LBS_DEB_ASSOC);
191 static int assoc_helper_channel(struct lbs_private *priv,
192 struct assoc_request * assoc_req)
194 struct lbs_adapter *adapter = priv->adapter;
197 lbs_deb_enter(LBS_DEB_ASSOC);
199 ret = update_channel(priv);
201 lbs_deb_assoc("ASSOC: channel: error getting channel.");
204 if (assoc_req->channel == adapter->curbssparams.channel)
207 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
208 adapter->curbssparams.channel, assoc_req->channel);
210 ret = lbs_prepare_and_send_command(priv, CMD_802_11_RF_CHANNEL,
211 CMD_OPT_802_11_RF_CHANNEL_SET,
212 CMD_OPTION_WAITFORRSP, 0, &assoc_req->channel);
214 lbs_deb_assoc("ASSOC: channel: error setting channel.");
217 ret = update_channel(priv);
219 lbs_deb_assoc("ASSOC: channel: error getting channel.");
222 if (assoc_req->channel != adapter->curbssparams.channel) {
223 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
228 if ( assoc_req->secinfo.wep_enabled
229 && (assoc_req->wep_keys[0].len
230 || assoc_req->wep_keys[1].len
231 || assoc_req->wep_keys[2].len
232 || assoc_req->wep_keys[3].len)) {
233 /* Make sure WEP keys are re-sent to firmware */
234 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
237 /* Must restart/rejoin adhoc networks after channel change */
238 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
241 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
246 static int assoc_helper_wep_keys(struct lbs_private *priv,
247 struct assoc_request * assoc_req)
249 struct lbs_adapter *adapter = priv->adapter;
253 lbs_deb_enter(LBS_DEB_ASSOC);
255 /* Set or remove WEP keys */
256 if ( assoc_req->wep_keys[0].len
257 || assoc_req->wep_keys[1].len
258 || assoc_req->wep_keys[2].len
259 || assoc_req->wep_keys[3].len) {
260 ret = lbs_prepare_and_send_command(priv,
263 CMD_OPTION_WAITFORRSP,
266 ret = lbs_prepare_and_send_command(priv,
269 CMD_OPTION_WAITFORRSP,
276 /* enable/disable the MAC's WEP packet filter */
277 if (assoc_req->secinfo.wep_enabled)
278 adapter->currentpacketfilter |= CMD_ACT_MAC_WEP_ENABLE;
280 adapter->currentpacketfilter &= ~CMD_ACT_MAC_WEP_ENABLE;
281 ret = lbs_set_mac_packet_filter(priv);
285 mutex_lock(&adapter->lock);
287 /* Copy WEP keys into adapter wep key fields */
288 for (i = 0; i < 4; i++) {
289 memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
290 sizeof(struct enc_key));
292 adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
294 mutex_unlock(&adapter->lock);
297 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
301 static int assoc_helper_secinfo(struct lbs_private *priv,
302 struct assoc_request * assoc_req)
304 struct lbs_adapter *adapter = priv->adapter;
309 lbs_deb_enter(LBS_DEB_ASSOC);
311 memcpy(&adapter->secinfo, &assoc_req->secinfo,
312 sizeof(struct lbs_802_11_security));
314 ret = lbs_set_mac_packet_filter(priv);
318 /* If RSN is already enabled, don't try to enable it again, since
319 * ENABLE_RSN resets internal state machines and will clobber the
320 * 4-way WPA handshake.
323 /* Get RSN enabled/disabled */
324 ret = lbs_prepare_and_send_command(priv,
325 CMD_802_11_ENABLE_RSN,
327 CMD_OPTION_WAITFORRSP,
330 lbs_deb_assoc("Failed to get RSN status: %d", ret);
334 /* Don't re-enable RSN if it's already enabled */
335 do_wpa = (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled);
339 /* Set RSN enabled/disabled */
341 ret = lbs_prepare_and_send_command(priv,
342 CMD_802_11_ENABLE_RSN,
344 CMD_OPTION_WAITFORRSP,
348 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
353 static int assoc_helper_wpa_keys(struct lbs_private *priv,
354 struct assoc_request * assoc_req)
357 unsigned int flags = assoc_req->flags;
359 lbs_deb_enter(LBS_DEB_ASSOC);
361 /* Work around older firmware bug where WPA unicast and multicast
362 * keys must be set independently. Seen in SDIO parts with firmware
366 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
367 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
368 ret = lbs_prepare_and_send_command(priv,
369 CMD_802_11_KEY_MATERIAL,
371 CMD_OPTION_WAITFORRSP,
373 assoc_req->flags = flags;
379 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
380 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
382 ret = lbs_prepare_and_send_command(priv,
383 CMD_802_11_KEY_MATERIAL,
385 CMD_OPTION_WAITFORRSP,
387 assoc_req->flags = flags;
391 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
396 static int assoc_helper_wpa_ie(struct lbs_private *priv,
397 struct assoc_request * assoc_req)
399 struct lbs_adapter *adapter = priv->adapter;
402 lbs_deb_enter(LBS_DEB_ASSOC);
404 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
405 memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
406 adapter->wpa_ie_len = assoc_req->wpa_ie_len;
408 memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
409 adapter->wpa_ie_len = 0;
412 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
417 static int should_deauth_infrastructure(struct lbs_adapter *adapter,
418 struct assoc_request * assoc_req)
422 lbs_deb_enter(LBS_DEB_ASSOC);
424 if (adapter->connect_status != LBS_CONNECTED)
427 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
428 lbs_deb_assoc("Deauthenticating due to new SSID\n");
433 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
434 if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
435 lbs_deb_assoc("Deauthenticating due to new security\n");
441 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
442 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
447 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
448 lbs_deb_assoc("Deauthenticating due to channel switch\n");
453 /* FIXME: deal with 'auto' mode somehow */
454 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
455 if (assoc_req->mode != IW_MODE_INFRA) {
456 lbs_deb_assoc("Deauthenticating due to leaving "
464 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
469 static int should_stop_adhoc(struct lbs_adapter *adapter,
470 struct assoc_request * assoc_req)
472 lbs_deb_enter(LBS_DEB_ASSOC);
474 if (adapter->connect_status != LBS_CONNECTED)
477 if (lbs_ssid_cmp(adapter->curbssparams.ssid,
478 adapter->curbssparams.ssid_len,
479 assoc_req->ssid, assoc_req->ssid_len) != 0)
482 /* FIXME: deal with 'auto' mode somehow */
483 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
484 if (assoc_req->mode != IW_MODE_ADHOC)
488 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
489 if (assoc_req->channel != adapter->curbssparams.channel)
493 lbs_deb_leave(LBS_DEB_ASSOC);
498 void lbs_association_worker(struct work_struct *work)
500 struct lbs_private *priv = container_of(work, struct lbs_private,
502 struct lbs_adapter *adapter = priv->adapter;
503 struct assoc_request * assoc_req = NULL;
505 int find_any_ssid = 0;
506 DECLARE_MAC_BUF(mac);
508 lbs_deb_enter(LBS_DEB_ASSOC);
510 mutex_lock(&adapter->lock);
511 assoc_req = adapter->pending_assoc_req;
512 adapter->pending_assoc_req = NULL;
513 adapter->in_progress_assoc_req = assoc_req;
514 mutex_unlock(&adapter->lock);
520 "Association Request:\n"
530 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
531 assoc_req->channel, assoc_req->band, assoc_req->mode,
532 print_mac(mac, assoc_req->bssid),
533 assoc_req->secinfo.WPAenabled ? " WPA" : "",
534 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
535 assoc_req->secinfo.wep_enabled ? " WEP" : "",
536 assoc_req->secinfo.auth_mode);
538 /* If 'any' SSID was specified, find an SSID to associate with */
539 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
540 && !assoc_req->ssid_len)
543 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
544 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
545 if (compare_ether_addr(assoc_req->bssid, bssid_any)
546 && compare_ether_addr(assoc_req->bssid, bssid_off))
553 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
554 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
556 lbs_deb_assoc("Could not find best network\n");
561 /* Ensure we switch to the mode of the AP */
562 if (assoc_req->mode == IW_MODE_AUTO) {
563 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
564 assoc_req->mode = new_mode;
569 * Check if the attributes being changing require deauthentication
570 * from the currently associated infrastructure access point.
572 if (adapter->mode == IW_MODE_INFRA) {
573 if (should_deauth_infrastructure(adapter, assoc_req)) {
574 ret = lbs_send_deauthentication(priv);
576 lbs_deb_assoc("Deauthentication due to new "
577 "configuration request failed: %d\n",
581 } else if (adapter->mode == IW_MODE_ADHOC) {
582 if (should_stop_adhoc(adapter, assoc_req)) {
583 ret = lbs_stop_adhoc_network(priv);
585 lbs_deb_assoc("Teardown of AdHoc network due to "
586 "new configuration request failed: %d\n",
593 /* Send the various configuration bits to the firmware */
594 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
595 ret = assoc_helper_mode(priv, assoc_req);
600 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
601 ret = assoc_helper_channel(priv, assoc_req);
606 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
607 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
608 ret = assoc_helper_wep_keys(priv, assoc_req);
613 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
614 ret = assoc_helper_secinfo(priv, assoc_req);
619 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
620 ret = assoc_helper_wpa_ie(priv, assoc_req);
625 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
626 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
627 ret = assoc_helper_wpa_keys(priv, assoc_req);
632 /* SSID/BSSID should be the _last_ config option set, because they
633 * trigger the association attempt.
635 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
636 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
639 ret = assoc_helper_associate(priv, assoc_req);
641 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
646 if (adapter->connect_status != LBS_CONNECTED) {
647 lbs_deb_assoc("ASSOC: association unsuccessful, "
653 lbs_deb_assoc("ASSOC: associated to '%s', %s\n",
654 escape_essid(adapter->curbssparams.ssid,
655 adapter->curbssparams.ssid_len),
656 print_mac(mac, adapter->curbssparams.bssid));
657 lbs_prepare_and_send_command(priv,
659 0, CMD_OPTION_WAITFORRSP, 0, NULL);
661 lbs_prepare_and_send_command(priv,
663 0, CMD_OPTION_WAITFORRSP, 0, NULL);
671 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
675 mutex_lock(&adapter->lock);
676 adapter->in_progress_assoc_req = NULL;
677 mutex_unlock(&adapter->lock);
681 lbs_deb_leave(LBS_DEB_ASSOC);
686 * Caller MUST hold any necessary locks
688 struct assoc_request *lbs_get_association_request(struct lbs_adapter *adapter)
690 struct assoc_request * assoc_req;
692 lbs_deb_enter(LBS_DEB_ASSOC);
693 if (!adapter->pending_assoc_req) {
694 adapter->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
696 if (!adapter->pending_assoc_req) {
697 lbs_pr_info("Not enough memory to allocate association"
703 /* Copy current configuration attributes to the association request,
704 * but don't overwrite any that are already set.
706 assoc_req = adapter->pending_assoc_req;
707 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
708 memcpy(&assoc_req->ssid, &adapter->curbssparams.ssid,
710 assoc_req->ssid_len = adapter->curbssparams.ssid_len;
713 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
714 assoc_req->channel = adapter->curbssparams.channel;
716 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
717 assoc_req->band = adapter->curbssparams.band;
719 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
720 assoc_req->mode = adapter->mode;
722 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
723 memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
727 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
729 for (i = 0; i < 4; i++) {
730 memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
731 sizeof(struct enc_key));
735 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
736 assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
738 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
739 memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
740 sizeof(struct enc_key));
743 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
744 memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
745 sizeof(struct enc_key));
748 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
749 memcpy(&assoc_req->secinfo, &adapter->secinfo,
750 sizeof(struct lbs_802_11_security));
753 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
754 memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
756 assoc_req->wpa_ie_len = adapter->wpa_ie_len;
759 lbs_deb_leave(LBS_DEB_ASSOC);