1 /* Copyright (C) 2006, Red Hat, Inc. */
3 #include <linux/bitops.h>
4 #include <net/ieee80211.h>
5 #include <linux/etherdevice.h>
15 static const u8 bssid_any[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
16 static const u8 bssid_off[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19 static int assoc_helper_essid(struct lbs_private *priv,
20 struct assoc_request * assoc_req)
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(priv, 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(priv, 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)
82 struct bss_descriptor * bss;
85 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %s",
86 print_mac(mac, assoc_req->bssid));
88 /* Search for index position in list for requested MAC */
89 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
92 lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
93 "cannot associate.\n", print_mac(mac, assoc_req->bssid));
97 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
98 if (assoc_req->mode == IW_MODE_INFRA) {
99 ret = lbs_associate(priv, assoc_req);
100 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
101 } else if (assoc_req->mode == IW_MODE_ADHOC) {
102 lbs_join_adhoc_network(priv, assoc_req);
106 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
111 static int assoc_helper_associate(struct lbs_private *priv,
112 struct assoc_request * assoc_req)
114 int ret = 0, done = 0;
116 lbs_deb_enter(LBS_DEB_ASSOC);
118 /* If we're given and 'any' BSSID, try associating based on SSID */
120 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
121 if (compare_ether_addr(bssid_any, assoc_req->bssid)
122 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
123 ret = assoc_helper_bssid(priv, assoc_req);
128 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
129 ret = assoc_helper_essid(priv, assoc_req);
132 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
137 static int assoc_helper_mode(struct lbs_private *priv,
138 struct assoc_request * assoc_req)
142 lbs_deb_enter(LBS_DEB_ASSOC);
144 if (assoc_req->mode == priv->mode)
147 if (assoc_req->mode == IW_MODE_INFRA) {
148 if (priv->psstate != PS_STATE_FULL_POWER)
149 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
150 priv->psmode = LBS802_11POWERMODECAM;
153 priv->mode = assoc_req->mode;
154 ret = lbs_prepare_and_send_command(priv,
156 0, CMD_OPTION_WAITFORRSP,
157 OID_802_11_INFRASTRUCTURE_MODE,
158 /* Shoot me now */ (void *) (size_t) assoc_req->mode);
161 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
166 static int update_channel(struct lbs_private *priv)
170 /* the channel in f/w could be out of sync, get the current channel */
171 lbs_deb_enter(LBS_DEB_ASSOC);
173 ret = lbs_get_channel(priv);
175 priv->curbssparams.channel = (u8) ret;
177 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
181 void lbs_sync_channel(struct work_struct *work)
183 struct lbs_private *priv = container_of(work, struct lbs_private,
186 lbs_deb_enter(LBS_DEB_ASSOC);
187 if (update_channel(priv) != 0)
188 lbs_pr_info("Channel synchronization failed.");
189 lbs_deb_leave(LBS_DEB_ASSOC);
192 static int assoc_helper_channel(struct lbs_private *priv,
193 struct assoc_request * assoc_req)
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 == priv->curbssparams.channel)
207 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
208 priv->curbssparams.channel, assoc_req->channel);
210 ret = lbs_set_channel(priv, assoc_req->channel);
212 lbs_deb_assoc("ASSOC: channel: error setting channel.");
214 /* FIXME: shouldn't need to grab the channel _again_ after setting
215 * it since the firmware is supposed to return the new channel, but
217 ret = update_channel(priv);
219 lbs_deb_assoc("ASSOC: channel: error getting channel.");
221 if (assoc_req->channel != priv->curbssparams.channel) {
222 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
227 if ( assoc_req->secinfo.wep_enabled
228 && (assoc_req->wep_keys[0].len
229 || assoc_req->wep_keys[1].len
230 || assoc_req->wep_keys[2].len
231 || assoc_req->wep_keys[3].len)) {
232 /* Make sure WEP keys are re-sent to firmware */
233 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
236 /* Must restart/rejoin adhoc networks after channel change */
237 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
240 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
245 static int assoc_helper_wep_keys(struct lbs_private *priv,
246 struct assoc_request * assoc_req)
251 lbs_deb_enter(LBS_DEB_ASSOC);
253 /* Set or remove WEP keys */
254 if ( assoc_req->wep_keys[0].len
255 || assoc_req->wep_keys[1].len
256 || assoc_req->wep_keys[2].len
257 || assoc_req->wep_keys[3].len) {
258 ret = lbs_prepare_and_send_command(priv,
261 CMD_OPTION_WAITFORRSP,
264 ret = lbs_prepare_and_send_command(priv,
267 CMD_OPTION_WAITFORRSP,
274 /* enable/disable the MAC's WEP packet filter */
275 if (assoc_req->secinfo.wep_enabled)
276 priv->currentpacketfilter |= CMD_ACT_MAC_WEP_ENABLE;
278 priv->currentpacketfilter &= ~CMD_ACT_MAC_WEP_ENABLE;
279 ret = lbs_set_mac_packet_filter(priv);
283 mutex_lock(&priv->lock);
285 /* Copy WEP keys into priv wep key fields */
286 for (i = 0; i < 4; i++) {
287 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
288 sizeof(struct enc_key));
290 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
292 mutex_unlock(&priv->lock);
295 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
299 static int assoc_helper_secinfo(struct lbs_private *priv,
300 struct assoc_request * assoc_req)
306 lbs_deb_enter(LBS_DEB_ASSOC);
308 memcpy(&priv->secinfo, &assoc_req->secinfo,
309 sizeof(struct lbs_802_11_security));
311 ret = lbs_set_mac_packet_filter(priv);
315 /* If RSN is already enabled, don't try to enable it again, since
316 * ENABLE_RSN resets internal state machines and will clobber the
317 * 4-way WPA handshake.
320 /* Get RSN enabled/disabled */
321 ret = lbs_prepare_and_send_command(priv,
322 CMD_802_11_ENABLE_RSN,
324 CMD_OPTION_WAITFORRSP,
327 lbs_deb_assoc("Failed to get RSN status: %d", ret);
331 /* Don't re-enable RSN if it's already enabled */
332 do_wpa = (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled);
336 /* Set RSN enabled/disabled */
338 ret = lbs_prepare_and_send_command(priv,
339 CMD_802_11_ENABLE_RSN,
341 CMD_OPTION_WAITFORRSP,
345 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
350 static int assoc_helper_wpa_keys(struct lbs_private *priv,
351 struct assoc_request * assoc_req)
354 unsigned int flags = assoc_req->flags;
356 lbs_deb_enter(LBS_DEB_ASSOC);
358 /* Work around older firmware bug where WPA unicast and multicast
359 * keys must be set independently. Seen in SDIO parts with firmware
363 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
364 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
365 ret = lbs_prepare_and_send_command(priv,
366 CMD_802_11_KEY_MATERIAL,
368 CMD_OPTION_WAITFORRSP,
370 assoc_req->flags = flags;
376 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
377 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
379 ret = lbs_prepare_and_send_command(priv,
380 CMD_802_11_KEY_MATERIAL,
382 CMD_OPTION_WAITFORRSP,
384 assoc_req->flags = flags;
388 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
393 static int assoc_helper_wpa_ie(struct lbs_private *priv,
394 struct assoc_request * assoc_req)
398 lbs_deb_enter(LBS_DEB_ASSOC);
400 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
401 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
402 priv->wpa_ie_len = assoc_req->wpa_ie_len;
404 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
405 priv->wpa_ie_len = 0;
408 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
413 static int should_deauth_infrastructure(struct lbs_private *priv,
414 struct assoc_request * assoc_req)
418 lbs_deb_enter(LBS_DEB_ASSOC);
420 if (priv->connect_status != LBS_CONNECTED)
423 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
424 lbs_deb_assoc("Deauthenticating due to new SSID\n");
429 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
430 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
431 lbs_deb_assoc("Deauthenticating due to new security\n");
437 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
438 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
443 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
444 lbs_deb_assoc("Deauthenticating due to channel switch\n");
449 /* FIXME: deal with 'auto' mode somehow */
450 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
451 if (assoc_req->mode != IW_MODE_INFRA) {
452 lbs_deb_assoc("Deauthenticating due to leaving "
460 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
465 static int should_stop_adhoc(struct lbs_private *priv,
466 struct assoc_request * assoc_req)
468 lbs_deb_enter(LBS_DEB_ASSOC);
470 if (priv->connect_status != LBS_CONNECTED)
473 if (lbs_ssid_cmp(priv->curbssparams.ssid,
474 priv->curbssparams.ssid_len,
475 assoc_req->ssid, assoc_req->ssid_len) != 0)
478 /* FIXME: deal with 'auto' mode somehow */
479 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
480 if (assoc_req->mode != IW_MODE_ADHOC)
484 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
485 if (assoc_req->channel != priv->curbssparams.channel)
489 lbs_deb_leave(LBS_DEB_ASSOC);
494 void lbs_association_worker(struct work_struct *work)
496 struct lbs_private *priv = container_of(work, struct lbs_private,
498 struct assoc_request * assoc_req = NULL;
500 int find_any_ssid = 0;
501 DECLARE_MAC_BUF(mac);
503 lbs_deb_enter(LBS_DEB_ASSOC);
505 mutex_lock(&priv->lock);
506 assoc_req = priv->pending_assoc_req;
507 priv->pending_assoc_req = NULL;
508 priv->in_progress_assoc_req = assoc_req;
509 mutex_unlock(&priv->lock);
515 "Association Request:\n"
525 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
526 assoc_req->channel, assoc_req->band, assoc_req->mode,
527 print_mac(mac, assoc_req->bssid),
528 assoc_req->secinfo.WPAenabled ? " WPA" : "",
529 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
530 assoc_req->secinfo.wep_enabled ? " WEP" : "",
531 assoc_req->secinfo.auth_mode);
533 /* If 'any' SSID was specified, find an SSID to associate with */
534 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
535 && !assoc_req->ssid_len)
538 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
539 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
540 if (compare_ether_addr(assoc_req->bssid, bssid_any)
541 && compare_ether_addr(assoc_req->bssid, bssid_off))
548 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
549 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
551 lbs_deb_assoc("Could not find best network\n");
556 /* Ensure we switch to the mode of the AP */
557 if (assoc_req->mode == IW_MODE_AUTO) {
558 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
559 assoc_req->mode = new_mode;
564 * Check if the attributes being changing require deauthentication
565 * from the currently associated infrastructure access point.
567 if (priv->mode == IW_MODE_INFRA) {
568 if (should_deauth_infrastructure(priv, assoc_req)) {
569 ret = lbs_send_deauthentication(priv);
571 lbs_deb_assoc("Deauthentication due to new "
572 "configuration request failed: %d\n",
576 } else if (priv->mode == IW_MODE_ADHOC) {
577 if (should_stop_adhoc(priv, assoc_req)) {
578 ret = lbs_stop_adhoc_network(priv);
580 lbs_deb_assoc("Teardown of AdHoc network due to "
581 "new configuration request failed: %d\n",
588 /* Send the various configuration bits to the firmware */
589 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
590 ret = assoc_helper_mode(priv, assoc_req);
595 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
596 ret = assoc_helper_channel(priv, assoc_req);
601 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
602 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
603 ret = assoc_helper_wep_keys(priv, assoc_req);
608 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
609 ret = assoc_helper_secinfo(priv, assoc_req);
614 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
615 ret = assoc_helper_wpa_ie(priv, assoc_req);
620 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
621 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
622 ret = assoc_helper_wpa_keys(priv, assoc_req);
627 /* SSID/BSSID should be the _last_ config option set, because they
628 * trigger the association attempt.
630 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
631 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
634 ret = assoc_helper_associate(priv, assoc_req);
636 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
641 if (priv->connect_status != LBS_CONNECTED) {
642 lbs_deb_assoc("ASSOC: association unsuccessful, "
648 lbs_deb_assoc("ASSOC: associated to '%s', %s\n",
649 escape_essid(priv->curbssparams.ssid,
650 priv->curbssparams.ssid_len),
651 print_mac(mac, priv->curbssparams.bssid));
652 lbs_prepare_and_send_command(priv,
654 0, CMD_OPTION_WAITFORRSP, 0, NULL);
656 lbs_prepare_and_send_command(priv,
658 0, CMD_OPTION_WAITFORRSP, 0, NULL);
666 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
670 mutex_lock(&priv->lock);
671 priv->in_progress_assoc_req = NULL;
672 mutex_unlock(&priv->lock);
676 lbs_deb_leave(LBS_DEB_ASSOC);
681 * Caller MUST hold any necessary locks
683 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
685 struct assoc_request * assoc_req;
687 lbs_deb_enter(LBS_DEB_ASSOC);
688 if (!priv->pending_assoc_req) {
689 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
691 if (!priv->pending_assoc_req) {
692 lbs_pr_info("Not enough memory to allocate association"
698 /* Copy current configuration attributes to the association request,
699 * but don't overwrite any that are already set.
701 assoc_req = priv->pending_assoc_req;
702 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
703 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
705 assoc_req->ssid_len = priv->curbssparams.ssid_len;
708 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
709 assoc_req->channel = priv->curbssparams.channel;
711 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
712 assoc_req->band = priv->curbssparams.band;
714 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
715 assoc_req->mode = priv->mode;
717 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
718 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
722 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
724 for (i = 0; i < 4; i++) {
725 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
726 sizeof(struct enc_key));
730 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
731 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
733 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
734 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
735 sizeof(struct enc_key));
738 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
739 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
740 sizeof(struct enc_key));
743 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
744 memcpy(&assoc_req->secinfo, &priv->secinfo,
745 sizeof(struct lbs_802_11_security));
748 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
749 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
751 assoc_req->wpa_ie_len = priv->wpa_ie_len;
754 lbs_deb_leave(LBS_DEB_ASSOC);