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)
22 struct bss_descriptor * bss;
25 lbs_deb_enter(LBS_DEB_ASSOC);
27 /* FIXME: take channel into account when picking SSIDs if a channel
31 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
32 channel = assoc_req->channel;
34 lbs_deb_assoc("SSID '%s' requested\n",
35 escape_essid(assoc_req->ssid, assoc_req->ssid_len));
36 if (assoc_req->mode == IW_MODE_INFRA) {
37 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
38 assoc_req->ssid_len, 0);
40 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
41 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
43 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
44 ret = lbs_associate(priv, assoc_req);
46 lbs_deb_assoc("SSID not found; cannot associate\n");
48 } else if (assoc_req->mode == IW_MODE_ADHOC) {
49 /* Scan for the network, do not save previous results. Stale
50 * scan data will cause us to join a non-existant adhoc network
52 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
53 assoc_req->ssid_len, 1);
55 /* Search for the requested SSID in the scan table */
56 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
57 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
59 lbs_deb_assoc("SSID found, will join\n");
60 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
61 lbs_join_adhoc_network(priv, assoc_req);
63 /* else send START command */
64 lbs_deb_assoc("SSID not found, creating adhoc network\n");
65 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
67 assoc_req->bss.ssid_len = assoc_req->ssid_len;
68 lbs_start_adhoc_network(priv, assoc_req);
72 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
77 static int assoc_helper_bssid(struct lbs_private *priv,
78 struct assoc_request * assoc_req)
81 struct bss_descriptor * bss;
84 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %s",
85 print_mac(mac, assoc_req->bssid));
87 /* Search for index position in list for requested MAC */
88 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
91 lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
92 "cannot associate.\n", print_mac(mac, assoc_req->bssid));
96 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
97 if (assoc_req->mode == IW_MODE_INFRA) {
98 ret = lbs_associate(priv, assoc_req);
99 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
100 } else if (assoc_req->mode == IW_MODE_ADHOC) {
101 lbs_join_adhoc_network(priv, assoc_req);
105 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
110 static int assoc_helper_associate(struct lbs_private *priv,
111 struct assoc_request * assoc_req)
113 int ret = 0, done = 0;
115 lbs_deb_enter(LBS_DEB_ASSOC);
117 /* If we're given and 'any' BSSID, try associating based on SSID */
119 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
120 if (compare_ether_addr(bssid_any, assoc_req->bssid)
121 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
122 ret = assoc_helper_bssid(priv, assoc_req);
127 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
128 ret = assoc_helper_essid(priv, assoc_req);
131 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
136 static int assoc_helper_mode(struct lbs_private *priv,
137 struct assoc_request * assoc_req)
141 lbs_deb_enter(LBS_DEB_ASSOC);
143 if (assoc_req->mode == priv->mode)
146 if (assoc_req->mode == IW_MODE_INFRA) {
147 if (priv->psstate != PS_STATE_FULL_POWER)
148 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
149 priv->psmode = LBS802_11POWERMODECAM;
152 priv->mode = assoc_req->mode;
153 ret = lbs_prepare_and_send_command(priv,
155 0, CMD_OPTION_WAITFORRSP,
156 OID_802_11_INFRASTRUCTURE_MODE,
157 /* Shoot me now */ (void *) (size_t) assoc_req->mode);
160 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
165 static int update_channel(struct lbs_private *priv)
168 /* the channel in f/w could be out of sync, get the current channel */
169 lbs_deb_enter(LBS_DEB_ASSOC);
170 ret = lbs_prepare_and_send_command(priv, CMD_802_11_RF_CHANNEL,
171 CMD_OPT_802_11_RF_CHANNEL_GET,
172 CMD_OPTION_WAITFORRSP, 0, NULL);
173 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
177 void lbs_sync_channel(struct work_struct *work)
179 struct lbs_private *priv = container_of(work, struct lbs_private,
182 lbs_deb_enter(LBS_DEB_ASSOC);
183 if (update_channel(priv) != 0)
184 lbs_pr_info("Channel synchronization failed.");
185 lbs_deb_leave(LBS_DEB_ASSOC);
188 static int assoc_helper_channel(struct lbs_private *priv,
189 struct assoc_request * assoc_req)
193 lbs_deb_enter(LBS_DEB_ASSOC);
195 ret = update_channel(priv);
197 lbs_deb_assoc("ASSOC: channel: error getting channel.");
200 if (assoc_req->channel == priv->curbssparams.channel)
203 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
204 priv->curbssparams.channel, assoc_req->channel);
206 ret = lbs_prepare_and_send_command(priv, CMD_802_11_RF_CHANNEL,
207 CMD_OPT_802_11_RF_CHANNEL_SET,
208 CMD_OPTION_WAITFORRSP, 0, &assoc_req->channel);
210 lbs_deb_assoc("ASSOC: channel: error setting channel.");
213 ret = update_channel(priv);
215 lbs_deb_assoc("ASSOC: channel: error getting channel.");
218 if (assoc_req->channel != priv->curbssparams.channel) {
219 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
224 if ( assoc_req->secinfo.wep_enabled
225 && (assoc_req->wep_keys[0].len
226 || assoc_req->wep_keys[1].len
227 || assoc_req->wep_keys[2].len
228 || assoc_req->wep_keys[3].len)) {
229 /* Make sure WEP keys are re-sent to firmware */
230 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
233 /* Must restart/rejoin adhoc networks after channel change */
234 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
237 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
242 static int assoc_helper_wep_keys(struct lbs_private *priv,
243 struct assoc_request * assoc_req)
248 lbs_deb_enter(LBS_DEB_ASSOC);
250 /* Set or remove WEP keys */
251 if ( assoc_req->wep_keys[0].len
252 || assoc_req->wep_keys[1].len
253 || assoc_req->wep_keys[2].len
254 || assoc_req->wep_keys[3].len) {
255 ret = lbs_prepare_and_send_command(priv,
258 CMD_OPTION_WAITFORRSP,
261 ret = lbs_prepare_and_send_command(priv,
264 CMD_OPTION_WAITFORRSP,
271 /* enable/disable the MAC's WEP packet filter */
272 if (assoc_req->secinfo.wep_enabled)
273 priv->currentpacketfilter |= CMD_ACT_MAC_WEP_ENABLE;
275 priv->currentpacketfilter &= ~CMD_ACT_MAC_WEP_ENABLE;
276 ret = lbs_set_mac_packet_filter(priv);
280 mutex_lock(&priv->lock);
282 /* Copy WEP keys into priv wep key fields */
283 for (i = 0; i < 4; i++) {
284 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
285 sizeof(struct enc_key));
287 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
289 mutex_unlock(&priv->lock);
292 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
296 static int assoc_helper_secinfo(struct lbs_private *priv,
297 struct assoc_request * assoc_req)
303 lbs_deb_enter(LBS_DEB_ASSOC);
305 memcpy(&priv->secinfo, &assoc_req->secinfo,
306 sizeof(struct lbs_802_11_security));
308 ret = lbs_set_mac_packet_filter(priv);
312 /* If RSN is already enabled, don't try to enable it again, since
313 * ENABLE_RSN resets internal state machines and will clobber the
314 * 4-way WPA handshake.
317 /* Get RSN enabled/disabled */
318 ret = lbs_prepare_and_send_command(priv,
319 CMD_802_11_ENABLE_RSN,
321 CMD_OPTION_WAITFORRSP,
324 lbs_deb_assoc("Failed to get RSN status: %d", ret);
328 /* Don't re-enable RSN if it's already enabled */
329 do_wpa = (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled);
333 /* Set RSN enabled/disabled */
335 ret = lbs_prepare_and_send_command(priv,
336 CMD_802_11_ENABLE_RSN,
338 CMD_OPTION_WAITFORRSP,
342 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
347 static int assoc_helper_wpa_keys(struct lbs_private *priv,
348 struct assoc_request * assoc_req)
351 unsigned int flags = assoc_req->flags;
353 lbs_deb_enter(LBS_DEB_ASSOC);
355 /* Work around older firmware bug where WPA unicast and multicast
356 * keys must be set independently. Seen in SDIO parts with firmware
360 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
361 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
362 ret = lbs_prepare_and_send_command(priv,
363 CMD_802_11_KEY_MATERIAL,
365 CMD_OPTION_WAITFORRSP,
367 assoc_req->flags = flags;
373 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
374 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
376 ret = lbs_prepare_and_send_command(priv,
377 CMD_802_11_KEY_MATERIAL,
379 CMD_OPTION_WAITFORRSP,
381 assoc_req->flags = flags;
385 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
390 static int assoc_helper_wpa_ie(struct lbs_private *priv,
391 struct assoc_request * assoc_req)
395 lbs_deb_enter(LBS_DEB_ASSOC);
397 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
398 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
399 priv->wpa_ie_len = assoc_req->wpa_ie_len;
401 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
402 priv->wpa_ie_len = 0;
405 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
410 static int should_deauth_infrastructure(struct lbs_private *priv,
411 struct assoc_request * assoc_req)
415 lbs_deb_enter(LBS_DEB_ASSOC);
417 if (priv->connect_status != LBS_CONNECTED)
420 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
421 lbs_deb_assoc("Deauthenticating due to new SSID\n");
426 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
427 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
428 lbs_deb_assoc("Deauthenticating due to new security\n");
434 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
435 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
440 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
441 lbs_deb_assoc("Deauthenticating due to channel switch\n");
446 /* FIXME: deal with 'auto' mode somehow */
447 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
448 if (assoc_req->mode != IW_MODE_INFRA) {
449 lbs_deb_assoc("Deauthenticating due to leaving "
457 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
462 static int should_stop_adhoc(struct lbs_private *priv,
463 struct assoc_request * assoc_req)
465 lbs_deb_enter(LBS_DEB_ASSOC);
467 if (priv->connect_status != LBS_CONNECTED)
470 if (lbs_ssid_cmp(priv->curbssparams.ssid,
471 priv->curbssparams.ssid_len,
472 assoc_req->ssid, assoc_req->ssid_len) != 0)
475 /* FIXME: deal with 'auto' mode somehow */
476 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
477 if (assoc_req->mode != IW_MODE_ADHOC)
481 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
482 if (assoc_req->channel != priv->curbssparams.channel)
486 lbs_deb_leave(LBS_DEB_ASSOC);
491 void lbs_association_worker(struct work_struct *work)
493 struct lbs_private *priv = container_of(work, struct lbs_private,
495 struct assoc_request * assoc_req = NULL;
497 int find_any_ssid = 0;
498 DECLARE_MAC_BUF(mac);
500 lbs_deb_enter(LBS_DEB_ASSOC);
502 mutex_lock(&priv->lock);
503 assoc_req = priv->pending_assoc_req;
504 priv->pending_assoc_req = NULL;
505 priv->in_progress_assoc_req = assoc_req;
506 mutex_unlock(&priv->lock);
512 "Association Request:\n"
522 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
523 assoc_req->channel, assoc_req->band, assoc_req->mode,
524 print_mac(mac, assoc_req->bssid),
525 assoc_req->secinfo.WPAenabled ? " WPA" : "",
526 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
527 assoc_req->secinfo.wep_enabled ? " WEP" : "",
528 assoc_req->secinfo.auth_mode);
530 /* If 'any' SSID was specified, find an SSID to associate with */
531 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
532 && !assoc_req->ssid_len)
535 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
536 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
537 if (compare_ether_addr(assoc_req->bssid, bssid_any)
538 && compare_ether_addr(assoc_req->bssid, bssid_off))
545 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
546 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
548 lbs_deb_assoc("Could not find best network\n");
553 /* Ensure we switch to the mode of the AP */
554 if (assoc_req->mode == IW_MODE_AUTO) {
555 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
556 assoc_req->mode = new_mode;
561 * Check if the attributes being changing require deauthentication
562 * from the currently associated infrastructure access point.
564 if (priv->mode == IW_MODE_INFRA) {
565 if (should_deauth_infrastructure(priv, assoc_req)) {
566 ret = lbs_send_deauthentication(priv);
568 lbs_deb_assoc("Deauthentication due to new "
569 "configuration request failed: %d\n",
573 } else if (priv->mode == IW_MODE_ADHOC) {
574 if (should_stop_adhoc(priv, assoc_req)) {
575 ret = lbs_stop_adhoc_network(priv);
577 lbs_deb_assoc("Teardown of AdHoc network due to "
578 "new configuration request failed: %d\n",
585 /* Send the various configuration bits to the firmware */
586 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
587 ret = assoc_helper_mode(priv, assoc_req);
592 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
593 ret = assoc_helper_channel(priv, assoc_req);
598 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
599 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
600 ret = assoc_helper_wep_keys(priv, assoc_req);
605 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
606 ret = assoc_helper_secinfo(priv, assoc_req);
611 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
612 ret = assoc_helper_wpa_ie(priv, assoc_req);
617 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
618 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
619 ret = assoc_helper_wpa_keys(priv, assoc_req);
624 /* SSID/BSSID should be the _last_ config option set, because they
625 * trigger the association attempt.
627 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
628 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
631 ret = assoc_helper_associate(priv, assoc_req);
633 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
638 if (priv->connect_status != LBS_CONNECTED) {
639 lbs_deb_assoc("ASSOC: association unsuccessful, "
645 lbs_deb_assoc("ASSOC: associated to '%s', %s\n",
646 escape_essid(priv->curbssparams.ssid,
647 priv->curbssparams.ssid_len),
648 print_mac(mac, priv->curbssparams.bssid));
649 lbs_prepare_and_send_command(priv,
651 0, CMD_OPTION_WAITFORRSP, 0, NULL);
653 lbs_prepare_and_send_command(priv,
655 0, CMD_OPTION_WAITFORRSP, 0, NULL);
663 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
667 mutex_lock(&priv->lock);
668 priv->in_progress_assoc_req = NULL;
669 mutex_unlock(&priv->lock);
673 lbs_deb_leave(LBS_DEB_ASSOC);
678 * Caller MUST hold any necessary locks
680 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
682 struct assoc_request * assoc_req;
684 lbs_deb_enter(LBS_DEB_ASSOC);
685 if (!priv->pending_assoc_req) {
686 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
688 if (!priv->pending_assoc_req) {
689 lbs_pr_info("Not enough memory to allocate association"
695 /* Copy current configuration attributes to the association request,
696 * but don't overwrite any that are already set.
698 assoc_req = priv->pending_assoc_req;
699 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
700 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
702 assoc_req->ssid_len = priv->curbssparams.ssid_len;
705 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
706 assoc_req->channel = priv->curbssparams.channel;
708 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
709 assoc_req->band = priv->curbssparams.band;
711 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
712 assoc_req->mode = priv->mode;
714 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
715 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
719 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
721 for (i = 0; i < 4; i++) {
722 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
723 sizeof(struct enc_key));
727 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
728 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
730 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
731 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
732 sizeof(struct enc_key));
735 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
736 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
737 sizeof(struct enc_key));
740 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
741 memcpy(&assoc_req->secinfo, &priv->secinfo,
742 sizeof(struct lbs_802_11_security));
745 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
746 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
748 assoc_req->wpa_ie_len = priv->wpa_ie_len;
751 lbs_deb_leave(LBS_DEB_ASSOC);