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 };
17 static void print_assoc_req(const char * extra, struct assoc_request * assoc_req)
21 "#### Association Request: %s\n"
28 " Encryption:%s%s%s\n"
30 extra, assoc_req->flags,
31 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
32 assoc_req->channel, assoc_req->band, assoc_req->mode,
33 print_mac(mac, assoc_req->bssid),
34 assoc_req->secinfo.WPAenabled ? " WPA" : "",
35 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
36 assoc_req->secinfo.wep_enabled ? " WEP" : "",
37 assoc_req->secinfo.auth_mode);
41 static int assoc_helper_essid(wlan_private *priv,
42 struct assoc_request * assoc_req)
44 wlan_adapter *adapter = priv->adapter;
46 struct bss_descriptor * bss;
49 lbs_deb_enter(LBS_DEB_ASSOC);
51 /* FIXME: take channel into account when picking SSIDs if a channel
55 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
56 channel = assoc_req->channel;
58 lbs_deb_assoc("New SSID requested: '%s'\n",
59 escape_essid(assoc_req->ssid, assoc_req->ssid_len));
60 if (assoc_req->mode == IW_MODE_INFRA) {
61 libertas_send_specific_ssid_scan(priv, assoc_req->ssid,
62 assoc_req->ssid_len, 0);
64 bss = libertas_find_ssid_in_list(adapter, assoc_req->ssid,
65 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
67 lbs_deb_assoc("SSID found in scan list, associating\n");
68 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
69 ret = wlan_associate(priv, assoc_req);
71 lbs_deb_assoc("SSID not found; cannot associate\n");
73 } else if (assoc_req->mode == IW_MODE_ADHOC) {
74 /* Scan for the network, do not save previous results. Stale
75 * scan data will cause us to join a non-existant adhoc network
77 libertas_send_specific_ssid_scan(priv, assoc_req->ssid,
78 assoc_req->ssid_len, 1);
80 /* Search for the requested SSID in the scan table */
81 bss = libertas_find_ssid_in_list(adapter, assoc_req->ssid,
82 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
84 lbs_deb_assoc("SSID found, will join\n");
85 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
86 libertas_join_adhoc_network(priv, assoc_req);
88 /* else send START command */
89 lbs_deb_assoc("SSID not found, creating adhoc network\n");
90 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
92 assoc_req->bss.ssid_len = assoc_req->ssid_len;
93 libertas_start_adhoc_network(priv, assoc_req);
97 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
102 static int assoc_helper_bssid(wlan_private *priv,
103 struct assoc_request * assoc_req)
105 wlan_adapter *adapter = priv->adapter;
107 struct bss_descriptor * bss;
108 DECLARE_MAC_BUF(mac);
110 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %s",
111 print_mac(mac, assoc_req->bssid));
113 /* Search for index position in list for requested MAC */
114 bss = libertas_find_bssid_in_list(adapter, assoc_req->bssid,
117 lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
118 "cannot associate.\n", print_mac(mac, assoc_req->bssid));
122 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
123 if (assoc_req->mode == IW_MODE_INFRA) {
124 ret = wlan_associate(priv, assoc_req);
125 lbs_deb_assoc("ASSOC: wlan_associate(bssid) returned %d\n", ret);
126 } else if (assoc_req->mode == IW_MODE_ADHOC) {
127 libertas_join_adhoc_network(priv, assoc_req);
131 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
136 static int assoc_helper_associate(wlan_private *priv,
137 struct assoc_request * assoc_req)
139 int ret = 0, done = 0;
141 /* If we're given and 'any' BSSID, try associating based on SSID */
143 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
144 if (compare_ether_addr(bssid_any, assoc_req->bssid)
145 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
146 ret = assoc_helper_bssid(priv, assoc_req);
149 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
154 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
155 ret = assoc_helper_essid(priv, assoc_req);
157 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
165 static int assoc_helper_mode(wlan_private *priv,
166 struct assoc_request * assoc_req)
168 wlan_adapter *adapter = priv->adapter;
171 lbs_deb_enter(LBS_DEB_ASSOC);
173 if (assoc_req->mode == adapter->mode)
176 if (assoc_req->mode == IW_MODE_INFRA) {
177 if (adapter->psstate != PS_STATE_FULL_POWER)
178 libertas_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
179 adapter->psmode = WLAN802_11POWERMODECAM;
182 adapter->mode = assoc_req->mode;
183 ret = libertas_prepare_and_send_command(priv,
185 0, CMD_OPTION_WAITFORRSP,
186 OID_802_11_INFRASTRUCTURE_MODE,
187 /* Shoot me now */ (void *) (size_t) assoc_req->mode);
190 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
195 static int update_channel(wlan_private * priv)
197 /* the channel in f/w could be out of sync, get the current channel */
198 return libertas_prepare_and_send_command(priv, CMD_802_11_RF_CHANNEL,
199 CMD_OPT_802_11_RF_CHANNEL_GET,
200 CMD_OPTION_WAITFORRSP, 0, NULL);
203 void libertas_sync_channel(struct work_struct *work)
205 wlan_private *priv = container_of(work, wlan_private, sync_channel);
207 if (update_channel(priv) != 0)
208 lbs_pr_info("Channel synchronization failed.");
211 static int assoc_helper_channel(wlan_private *priv,
212 struct assoc_request * assoc_req)
214 wlan_adapter *adapter = priv->adapter;
217 lbs_deb_enter(LBS_DEB_ASSOC);
219 ret = update_channel(priv);
221 lbs_deb_assoc("ASSOC: channel: error getting channel.");
224 if (assoc_req->channel == adapter->curbssparams.channel)
227 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
228 adapter->curbssparams.channel, assoc_req->channel);
230 ret = libertas_prepare_and_send_command(priv, CMD_802_11_RF_CHANNEL,
231 CMD_OPT_802_11_RF_CHANNEL_SET,
232 CMD_OPTION_WAITFORRSP, 0, &assoc_req->channel);
234 lbs_deb_assoc("ASSOC: channel: error setting channel.");
237 ret = update_channel(priv);
239 lbs_deb_assoc("ASSOC: channel: error getting channel.");
242 if (assoc_req->channel != adapter->curbssparams.channel) {
243 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
248 if ( assoc_req->secinfo.wep_enabled
249 && (assoc_req->wep_keys[0].len
250 || assoc_req->wep_keys[1].len
251 || assoc_req->wep_keys[2].len
252 || assoc_req->wep_keys[3].len)) {
253 /* Make sure WEP keys are re-sent to firmware */
254 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
257 /* Must restart/rejoin adhoc networks after channel change */
258 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
261 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
266 static int assoc_helper_wep_keys(wlan_private *priv,
267 struct assoc_request * assoc_req)
269 wlan_adapter *adapter = priv->adapter;
273 lbs_deb_enter(LBS_DEB_ASSOC);
275 /* Set or remove WEP keys */
276 if ( assoc_req->wep_keys[0].len
277 || assoc_req->wep_keys[1].len
278 || assoc_req->wep_keys[2].len
279 || assoc_req->wep_keys[3].len) {
280 ret = libertas_prepare_and_send_command(priv,
283 CMD_OPTION_WAITFORRSP,
286 ret = libertas_prepare_and_send_command(priv,
289 CMD_OPTION_WAITFORRSP,
296 /* enable/disable the MAC's WEP packet filter */
297 if (assoc_req->secinfo.wep_enabled)
298 adapter->currentpacketfilter |= CMD_ACT_MAC_WEP_ENABLE;
300 adapter->currentpacketfilter &= ~CMD_ACT_MAC_WEP_ENABLE;
301 ret = libertas_set_mac_packet_filter(priv);
305 mutex_lock(&adapter->lock);
307 /* Copy WEP keys into adapter wep key fields */
308 for (i = 0; i < 4; i++) {
309 memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
310 sizeof(struct enc_key));
312 adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
314 mutex_unlock(&adapter->lock);
317 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
321 static int assoc_helper_secinfo(wlan_private *priv,
322 struct assoc_request * assoc_req)
324 wlan_adapter *adapter = priv->adapter;
329 lbs_deb_enter(LBS_DEB_ASSOC);
331 memcpy(&adapter->secinfo, &assoc_req->secinfo,
332 sizeof(struct wlan_802_11_security));
334 ret = libertas_set_mac_packet_filter(priv);
338 /* If RSN is already enabled, don't try to enable it again, since
339 * ENABLE_RSN resets internal state machines and will clobber the
340 * 4-way WPA handshake.
343 /* Get RSN enabled/disabled */
344 ret = libertas_prepare_and_send_command(priv,
345 CMD_802_11_ENABLE_RSN,
347 CMD_OPTION_WAITFORRSP,
350 lbs_deb_assoc("Failed to get RSN status: %d", ret);
354 /* Don't re-enable RSN if it's already enabled */
355 do_wpa = (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled);
359 /* Set RSN enabled/disabled */
361 ret = libertas_prepare_and_send_command(priv,
362 CMD_802_11_ENABLE_RSN,
364 CMD_OPTION_WAITFORRSP,
368 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
373 static int assoc_helper_wpa_keys(wlan_private *priv,
374 struct assoc_request * assoc_req)
377 unsigned int flags = assoc_req->flags;
379 lbs_deb_enter(LBS_DEB_ASSOC);
381 /* Work around older firmware bug where WPA unicast and multicast
382 * keys must be set independently. Seen in SDIO parts with firmware
386 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
387 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
388 ret = libertas_prepare_and_send_command(priv,
389 CMD_802_11_KEY_MATERIAL,
391 CMD_OPTION_WAITFORRSP,
393 assoc_req->flags = flags;
399 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
400 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
402 ret = libertas_prepare_and_send_command(priv,
403 CMD_802_11_KEY_MATERIAL,
405 CMD_OPTION_WAITFORRSP,
407 assoc_req->flags = flags;
411 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
416 static int assoc_helper_wpa_ie(wlan_private *priv,
417 struct assoc_request * assoc_req)
419 wlan_adapter *adapter = priv->adapter;
422 lbs_deb_enter(LBS_DEB_ASSOC);
424 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
425 memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
426 adapter->wpa_ie_len = assoc_req->wpa_ie_len;
428 memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
429 adapter->wpa_ie_len = 0;
432 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
437 static int should_deauth_infrastructure(wlan_adapter *adapter,
438 struct assoc_request * assoc_req)
440 if (adapter->connect_status != LIBERTAS_CONNECTED)
443 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
444 lbs_deb_assoc("Deauthenticating due to new SSID in "
445 " configuration request.\n");
449 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
450 if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
451 lbs_deb_assoc("Deauthenticating due to updated security "
452 "info in configuration request.\n");
457 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
458 lbs_deb_assoc("Deauthenticating due to new BSSID in "
459 " configuration request.\n");
463 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
464 lbs_deb_assoc("Deauthenticating due to channel switch.\n");
468 /* FIXME: deal with 'auto' mode somehow */
469 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
470 if (assoc_req->mode != IW_MODE_INFRA)
478 static int should_stop_adhoc(wlan_adapter *adapter,
479 struct assoc_request * assoc_req)
481 if (adapter->connect_status != LIBERTAS_CONNECTED)
484 if (libertas_ssid_cmp(adapter->curbssparams.ssid,
485 adapter->curbssparams.ssid_len,
486 assoc_req->ssid, assoc_req->ssid_len) != 0)
489 /* FIXME: deal with 'auto' mode somehow */
490 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
491 if (assoc_req->mode != IW_MODE_ADHOC)
495 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
496 if (assoc_req->channel != adapter->curbssparams.channel)
504 void libertas_association_worker(struct work_struct *work)
506 wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
507 wlan_adapter *adapter = priv->adapter;
508 struct assoc_request * assoc_req = NULL;
510 int find_any_ssid = 0;
511 DECLARE_MAC_BUF(mac);
513 lbs_deb_enter(LBS_DEB_ASSOC);
515 mutex_lock(&adapter->lock);
516 assoc_req = adapter->pending_assoc_req;
517 adapter->pending_assoc_req = NULL;
518 adapter->in_progress_assoc_req = assoc_req;
519 mutex_unlock(&adapter->lock);
524 print_assoc_req(__func__, assoc_req);
526 /* If 'any' SSID was specified, find an SSID to associate with */
527 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
528 && !assoc_req->ssid_len)
531 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
532 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
533 if (compare_ether_addr(assoc_req->bssid, bssid_any)
534 && compare_ether_addr(assoc_req->bssid, bssid_off))
541 ret = libertas_find_best_network_ssid(priv, assoc_req->ssid,
542 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
544 lbs_deb_assoc("Could not find best network\n");
549 /* Ensure we switch to the mode of the AP */
550 if (assoc_req->mode == IW_MODE_AUTO) {
551 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
552 assoc_req->mode = new_mode;
557 * Check if the attributes being changing require deauthentication
558 * from the currently associated infrastructure access point.
560 if (adapter->mode == IW_MODE_INFRA) {
561 if (should_deauth_infrastructure(adapter, assoc_req)) {
562 ret = libertas_send_deauthentication(priv);
564 lbs_deb_assoc("Deauthentication due to new "
565 "configuration request failed: %d\n",
569 } else if (adapter->mode == IW_MODE_ADHOC) {
570 if (should_stop_adhoc(adapter, assoc_req)) {
571 ret = libertas_stop_adhoc_network(priv);
573 lbs_deb_assoc("Teardown of AdHoc network due to "
574 "new configuration request failed: %d\n",
581 /* Send the various configuration bits to the firmware */
582 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
583 ret = assoc_helper_mode(priv, assoc_req);
585 lbs_deb_assoc("ASSOC(:%d) mode: ret = %d\n",
591 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
592 ret = assoc_helper_channel(priv, assoc_req);
594 lbs_deb_assoc("ASSOC(:%d) channel: ret = %d\n",
600 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
601 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
602 ret = assoc_helper_wep_keys(priv, assoc_req);
604 lbs_deb_assoc("ASSOC(:%d) wep_keys: ret = %d\n",
610 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
611 ret = assoc_helper_secinfo(priv, assoc_req);
613 lbs_deb_assoc("ASSOC(:%d) secinfo: ret = %d\n",
619 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
620 ret = assoc_helper_wpa_ie(priv, assoc_req);
622 lbs_deb_assoc("ASSOC(:%d) wpa_ie: ret = %d\n",
628 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
629 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
630 ret = assoc_helper_wpa_keys(priv, assoc_req);
632 lbs_deb_assoc("ASSOC(:%d) wpa_keys: ret = %d\n",
638 /* SSID/BSSID should be the _last_ config option set, because they
639 * trigger the association attempt.
641 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
642 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
645 ret = assoc_helper_associate(priv, assoc_req);
647 lbs_deb_assoc("ASSOC: association attempt unsuccessful: %d\n",
652 if (adapter->connect_status != LIBERTAS_CONNECTED) {
653 lbs_deb_assoc("ASSOC: association attempt unsuccessful, "
659 lbs_deb_assoc("ASSOC: association attempt successful. "
660 "Associated to '%s' (%s)\n",
661 escape_essid(adapter->curbssparams.ssid,
662 adapter->curbssparams.ssid_len),
663 print_mac(mac, adapter->curbssparams.bssid));
664 libertas_prepare_and_send_command(priv,
666 0, CMD_OPTION_WAITFORRSP, 0, NULL);
668 libertas_prepare_and_send_command(priv,
670 0, CMD_OPTION_WAITFORRSP, 0, NULL);
678 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
682 mutex_lock(&adapter->lock);
683 adapter->in_progress_assoc_req = NULL;
684 mutex_unlock(&adapter->lock);
688 lbs_deb_leave(LBS_DEB_ASSOC);
693 * Caller MUST hold any necessary locks
695 struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
697 struct assoc_request * assoc_req;
699 if (!adapter->pending_assoc_req) {
700 adapter->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
702 if (!adapter->pending_assoc_req) {
703 lbs_pr_info("Not enough memory to allocate association"
709 /* Copy current configuration attributes to the association request,
710 * but don't overwrite any that are already set.
712 assoc_req = adapter->pending_assoc_req;
713 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
714 memcpy(&assoc_req->ssid, &adapter->curbssparams.ssid,
716 assoc_req->ssid_len = adapter->curbssparams.ssid_len;
719 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
720 assoc_req->channel = adapter->curbssparams.channel;
722 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
723 assoc_req->band = adapter->curbssparams.band;
725 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
726 assoc_req->mode = adapter->mode;
728 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
729 memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
733 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
735 for (i = 0; i < 4; i++) {
736 memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
737 sizeof(struct enc_key));
741 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
742 assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
744 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
745 memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
746 sizeof(struct enc_key));
749 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
750 memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
751 sizeof(struct enc_key));
754 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
755 memcpy(&assoc_req->secinfo, &adapter->secinfo,
756 sizeof(struct wlan_802_11_security));
759 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
760 memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
762 assoc_req->wpa_ie_len = adapter->wpa_ie_len;
765 print_assoc_req(__func__, assoc_req);