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 int lbs_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 = ret;
178 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
182 void lbs_sync_channel(struct work_struct *work)
184 struct lbs_private *priv = container_of(work, struct lbs_private,
187 lbs_deb_enter(LBS_DEB_ASSOC);
188 if (lbs_update_channel(priv))
189 lbs_pr_info("Channel synchronization failed.");
190 lbs_deb_leave(LBS_DEB_ASSOC);
193 static int assoc_helper_channel(struct lbs_private *priv,
194 struct assoc_request * assoc_req)
198 lbs_deb_enter(LBS_DEB_ASSOC);
200 ret = lbs_update_channel(priv);
202 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
206 if (assoc_req->channel == priv->curbssparams.channel)
209 if (priv->mesh_dev) {
210 /* Change mesh channel first; 21.p21 firmware won't let
211 you change channel otherwise (even though it'll return
213 lbs_mesh_config(priv, 0, assoc_req->channel);
216 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
217 priv->curbssparams.channel, assoc_req->channel);
219 ret = lbs_set_channel(priv, assoc_req->channel);
221 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
223 /* FIXME: shouldn't need to grab the channel _again_ after setting
224 * it since the firmware is supposed to return the new channel, but
226 ret = lbs_update_channel(priv);
228 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
232 if (assoc_req->channel != priv->curbssparams.channel) {
233 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
238 if ( assoc_req->secinfo.wep_enabled
239 && (assoc_req->wep_keys[0].len
240 || assoc_req->wep_keys[1].len
241 || assoc_req->wep_keys[2].len
242 || assoc_req->wep_keys[3].len)) {
243 /* Make sure WEP keys are re-sent to firmware */
244 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
247 /* Must restart/rejoin adhoc networks after channel change */
248 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
252 lbs_mesh_config(priv, 1, priv->curbssparams.channel);
255 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
260 static int assoc_helper_wep_keys(struct lbs_private *priv,
261 struct assoc_request *assoc_req)
266 lbs_deb_enter(LBS_DEB_ASSOC);
268 /* Set or remove WEP keys */
269 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
270 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
271 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
273 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
278 /* enable/disable the MAC's WEP packet filter */
279 if (assoc_req->secinfo.wep_enabled)
280 priv->currentpacketfilter |= CMD_ACT_MAC_WEP_ENABLE;
282 priv->currentpacketfilter &= ~CMD_ACT_MAC_WEP_ENABLE;
284 ret = lbs_set_mac_packet_filter(priv);
288 mutex_lock(&priv->lock);
290 /* Copy WEP keys into priv wep key fields */
291 for (i = 0; i < 4; i++) {
292 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
293 sizeof(struct enc_key));
295 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
297 mutex_unlock(&priv->lock);
300 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
304 static int assoc_helper_secinfo(struct lbs_private *priv,
305 struct assoc_request * assoc_req)
311 lbs_deb_enter(LBS_DEB_ASSOC);
313 memcpy(&priv->secinfo, &assoc_req->secinfo,
314 sizeof(struct lbs_802_11_security));
316 ret = lbs_set_mac_packet_filter(priv);
320 /* If RSN is already enabled, don't try to enable it again, since
321 * ENABLE_RSN resets internal state machines and will clobber the
322 * 4-way WPA handshake.
325 /* Get RSN enabled/disabled */
326 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
328 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
332 /* Don't re-enable RSN if it's already enabled */
333 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
337 /* Set RSN enabled/disabled */
338 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
341 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
346 static int assoc_helper_wpa_keys(struct lbs_private *priv,
347 struct assoc_request * assoc_req)
350 unsigned int flags = assoc_req->flags;
352 lbs_deb_enter(LBS_DEB_ASSOC);
354 /* Work around older firmware bug where WPA unicast and multicast
355 * keys must be set independently. Seen in SDIO parts with firmware
359 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
360 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
361 ret = lbs_prepare_and_send_command(priv,
362 CMD_802_11_KEY_MATERIAL,
364 CMD_OPTION_WAITFORRSP,
366 assoc_req->flags = flags;
372 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
373 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
375 ret = lbs_prepare_and_send_command(priv,
376 CMD_802_11_KEY_MATERIAL,
378 CMD_OPTION_WAITFORRSP,
380 assoc_req->flags = flags;
384 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
389 static int assoc_helper_wpa_ie(struct lbs_private *priv,
390 struct assoc_request * assoc_req)
394 lbs_deb_enter(LBS_DEB_ASSOC);
396 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
397 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
398 priv->wpa_ie_len = assoc_req->wpa_ie_len;
400 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
401 priv->wpa_ie_len = 0;
404 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
409 static int should_deauth_infrastructure(struct lbs_private *priv,
410 struct assoc_request * assoc_req)
414 lbs_deb_enter(LBS_DEB_ASSOC);
416 if (priv->connect_status != LBS_CONNECTED)
419 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
420 lbs_deb_assoc("Deauthenticating due to new SSID\n");
425 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
426 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
427 lbs_deb_assoc("Deauthenticating due to new security\n");
433 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
434 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
439 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
440 lbs_deb_assoc("Deauthenticating due to channel switch\n");
445 /* FIXME: deal with 'auto' mode somehow */
446 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
447 if (assoc_req->mode != IW_MODE_INFRA) {
448 lbs_deb_assoc("Deauthenticating due to leaving "
456 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
461 static int should_stop_adhoc(struct lbs_private *priv,
462 struct assoc_request * assoc_req)
464 lbs_deb_enter(LBS_DEB_ASSOC);
466 if (priv->connect_status != LBS_CONNECTED)
469 if (lbs_ssid_cmp(priv->curbssparams.ssid,
470 priv->curbssparams.ssid_len,
471 assoc_req->ssid, assoc_req->ssid_len) != 0)
474 /* FIXME: deal with 'auto' mode somehow */
475 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
476 if (assoc_req->mode != IW_MODE_ADHOC)
480 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
481 if (assoc_req->channel != priv->curbssparams.channel)
485 lbs_deb_leave(LBS_DEB_ASSOC);
490 void lbs_association_worker(struct work_struct *work)
492 struct lbs_private *priv = container_of(work, struct lbs_private,
494 struct assoc_request * assoc_req = NULL;
496 int find_any_ssid = 0;
497 DECLARE_MAC_BUF(mac);
499 lbs_deb_enter(LBS_DEB_ASSOC);
501 mutex_lock(&priv->lock);
502 assoc_req = priv->pending_assoc_req;
503 priv->pending_assoc_req = NULL;
504 priv->in_progress_assoc_req = assoc_req;
505 mutex_unlock(&priv->lock);
511 "Association Request:\n"
521 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
522 assoc_req->channel, assoc_req->band, assoc_req->mode,
523 print_mac(mac, assoc_req->bssid),
524 assoc_req->secinfo.WPAenabled ? " WPA" : "",
525 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
526 assoc_req->secinfo.wep_enabled ? " WEP" : "",
527 assoc_req->secinfo.auth_mode);
529 /* If 'any' SSID was specified, find an SSID to associate with */
530 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
531 && !assoc_req->ssid_len)
534 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
535 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
536 if (compare_ether_addr(assoc_req->bssid, bssid_any)
537 && compare_ether_addr(assoc_req->bssid, bssid_off))
544 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
545 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
547 lbs_deb_assoc("Could not find best network\n");
552 /* Ensure we switch to the mode of the AP */
553 if (assoc_req->mode == IW_MODE_AUTO) {
554 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
555 assoc_req->mode = new_mode;
560 * Check if the attributes being changing require deauthentication
561 * from the currently associated infrastructure access point.
563 if (priv->mode == IW_MODE_INFRA) {
564 if (should_deauth_infrastructure(priv, assoc_req)) {
565 ret = lbs_send_deauthentication(priv);
567 lbs_deb_assoc("Deauthentication due to new "
568 "configuration request failed: %d\n",
572 } else if (priv->mode == IW_MODE_ADHOC) {
573 if (should_stop_adhoc(priv, assoc_req)) {
574 ret = lbs_stop_adhoc_network(priv);
576 lbs_deb_assoc("Teardown of AdHoc network due to "
577 "new configuration request failed: %d\n",
584 /* Send the various configuration bits to the firmware */
585 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
586 ret = assoc_helper_mode(priv, assoc_req);
591 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
592 ret = assoc_helper_channel(priv, assoc_req);
597 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
598 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
599 ret = assoc_helper_wep_keys(priv, assoc_req);
604 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
605 ret = assoc_helper_secinfo(priv, assoc_req);
610 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
611 ret = assoc_helper_wpa_ie(priv, assoc_req);
616 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
617 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
618 ret = assoc_helper_wpa_keys(priv, assoc_req);
623 /* SSID/BSSID should be the _last_ config option set, because they
624 * trigger the association attempt.
626 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
627 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
630 ret = assoc_helper_associate(priv, assoc_req);
632 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
637 if (priv->connect_status != LBS_CONNECTED) {
638 lbs_deb_assoc("ASSOC: association unsuccessful, "
644 lbs_deb_assoc("ASSOC: associated to '%s', %s\n",
645 escape_essid(priv->curbssparams.ssid,
646 priv->curbssparams.ssid_len),
647 print_mac(mac, priv->curbssparams.bssid));
648 lbs_prepare_and_send_command(priv,
650 0, CMD_OPTION_WAITFORRSP, 0, NULL);
652 lbs_prepare_and_send_command(priv,
654 0, CMD_OPTION_WAITFORRSP, 0, NULL);
662 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
666 mutex_lock(&priv->lock);
667 priv->in_progress_assoc_req = NULL;
668 mutex_unlock(&priv->lock);
672 lbs_deb_leave(LBS_DEB_ASSOC);
677 * Caller MUST hold any necessary locks
679 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
681 struct assoc_request * assoc_req;
683 lbs_deb_enter(LBS_DEB_ASSOC);
684 if (!priv->pending_assoc_req) {
685 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
687 if (!priv->pending_assoc_req) {
688 lbs_pr_info("Not enough memory to allocate association"
694 /* Copy current configuration attributes to the association request,
695 * but don't overwrite any that are already set.
697 assoc_req = priv->pending_assoc_req;
698 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
699 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
701 assoc_req->ssid_len = priv->curbssparams.ssid_len;
704 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
705 assoc_req->channel = priv->curbssparams.channel;
707 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
708 assoc_req->band = priv->curbssparams.band;
710 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
711 assoc_req->mode = priv->mode;
713 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
714 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
718 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
720 for (i = 0; i < 4; i++) {
721 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
722 sizeof(struct enc_key));
726 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
727 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
729 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
730 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
731 sizeof(struct enc_key));
734 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
735 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
736 sizeof(struct enc_key));
739 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
740 memcpy(&assoc_req->secinfo, &priv->secinfo,
741 sizeof(struct lbs_802_11_security));
744 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
745 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
747 assoc_req->wpa_ie_len = priv->wpa_ie_len;
750 lbs_deb_leave(LBS_DEB_ASSOC);