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 /* From ieee80211_module.c */
18 static const char *libertas_escape_essid(const char *essid, u8 essid_len)
20 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
21 const char *s = essid;
24 if (ieee80211_is_empty_essid(essid, essid_len))
27 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
41 static void print_assoc_req(const char * extra, struct assoc_request * assoc_req)
44 "#### Association Request: %s\n"
50 " BSSID: " MAC_FMT "\n"
57 extra, assoc_req->flags,
58 libertas_escape_essid(assoc_req->ssid.ssid, assoc_req->ssid.ssidlength),
59 assoc_req->channel, assoc_req->band, assoc_req->mode,
60 MAC_ARG(assoc_req->bssid), assoc_req->secinfo.WPAenabled,
61 assoc_req->secinfo.WPA2enabled, assoc_req->secinfo.WEPstatus,
62 assoc_req->secinfo.authmode, assoc_req->secinfo.auth1xalg,
63 assoc_req->secinfo.Encryptionmode);
67 static int assoc_helper_essid(wlan_private *priv,
68 struct assoc_request * assoc_req)
70 wlan_adapter *adapter = priv->adapter;
72 struct bss_descriptor * bss;
75 lbs_deb_enter(LBS_DEB_ASSOC);
77 /* FIXME: take channel into account when picking SSIDs if a channel
81 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
82 channel = assoc_req->channel;
84 lbs_deb_assoc("New SSID requested: %s\n", assoc_req->ssid.ssid);
85 if (assoc_req->mode == IW_MODE_INFRA) {
86 if (adapter->prescan) {
87 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 0);
90 bss = libertas_find_SSID_in_list(adapter, &assoc_req->ssid,
91 NULL, IW_MODE_INFRA, channel);
93 lbs_deb_assoc("SSID found in scan list, associating\n");
94 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
95 ret = wlan_associate(priv, assoc_req);
97 lbs_deb_assoc("SSID '%s' not found; cannot associate\n",
98 assoc_req->ssid.ssid);
100 } else if (assoc_req->mode == IW_MODE_ADHOC) {
101 /* Scan for the network, do not save previous results. Stale
102 * scan data will cause us to join a non-existant adhoc network
104 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 1);
106 /* Search for the requested SSID in the scan table */
107 bss = libertas_find_SSID_in_list(adapter, &assoc_req->ssid, NULL,
108 IW_MODE_ADHOC, channel);
110 lbs_deb_assoc("SSID found joining\n");
111 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
112 libertas_join_adhoc_network(priv, assoc_req);
114 /* else send START command */
115 lbs_deb_assoc("SSID not found in list, so creating adhoc"
116 " with SSID '%s'\n", assoc_req->ssid.ssid);
117 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
118 sizeof(struct WLAN_802_11_SSID));
119 libertas_start_adhoc_network(priv, assoc_req);
123 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
128 static int assoc_helper_bssid(wlan_private *priv,
129 struct assoc_request * assoc_req)
131 wlan_adapter *adapter = priv->adapter;
133 struct bss_descriptor * bss;
135 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID" MAC_FMT "\n",
136 MAC_ARG(assoc_req->bssid));
138 /* Search for index position in list for requested MAC */
139 bss = libertas_find_BSSID_in_list(adapter, assoc_req->bssid,
142 lbs_deb_assoc("ASSOC: WAP: BSSID " MAC_FMT " not found, "
143 "cannot associate.\n", MAC_ARG(assoc_req->bssid));
147 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
148 if (assoc_req->mode == IW_MODE_INFRA) {
149 ret = wlan_associate(priv, assoc_req);
150 lbs_deb_assoc("ASSOC: wlan_associate(bssid) returned %d\n", ret);
151 } else if (assoc_req->mode == IW_MODE_ADHOC) {
152 libertas_join_adhoc_network(priv, assoc_req);
156 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
161 static int assoc_helper_associate(wlan_private *priv,
162 struct assoc_request * assoc_req)
164 int ret = 0, done = 0;
166 /* If we're given and 'any' BSSID, try associating based on SSID */
168 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
169 if (compare_ether_addr(bssid_any, assoc_req->bssid)
170 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
171 ret = assoc_helper_bssid(priv, assoc_req);
174 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
179 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
180 ret = assoc_helper_essid(priv, assoc_req);
182 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
190 static int assoc_helper_mode(wlan_private *priv,
191 struct assoc_request * assoc_req)
193 wlan_adapter *adapter = priv->adapter;
196 lbs_deb_enter(LBS_DEB_ASSOC);
198 if (assoc_req->mode == adapter->mode)
201 if (assoc_req->mode == IW_MODE_INFRA) {
202 if (adapter->psstate != PS_STATE_FULL_POWER)
203 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
204 adapter->psmode = wlan802_11powermodecam;
207 adapter->mode = assoc_req->mode;
208 ret = libertas_prepare_and_send_command(priv,
210 0, cmd_option_waitforrsp,
211 OID_802_11_INFRASTRUCTURE_MODE,
212 (void *) (size_t) assoc_req->mode);
215 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
220 static int update_channel(wlan_private * priv)
222 /* the channel in f/w could be out of sync, get the current channel */
223 return libertas_prepare_and_send_command(priv, cmd_802_11_rf_channel,
224 cmd_opt_802_11_rf_channel_get,
225 cmd_option_waitforrsp, 0, NULL);
228 static int assoc_helper_channel(wlan_private *priv,
229 struct assoc_request * assoc_req)
231 wlan_adapter *adapter = priv->adapter;
234 lbs_deb_enter(LBS_DEB_ASSOC);
236 ret = update_channel(priv);
238 lbs_deb_assoc("ASSOC: channel: error getting channel.");
241 if (assoc_req->channel == adapter->curbssparams.channel)
244 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
245 adapter->curbssparams.channel, assoc_req->channel);
247 ret = libertas_prepare_and_send_command(priv, cmd_802_11_rf_channel,
248 cmd_opt_802_11_rf_channel_set,
249 cmd_option_waitforrsp, 0, &assoc_req->channel);
251 lbs_deb_assoc("ASSOC: channel: error setting channel.");
254 ret = update_channel(priv);
256 lbs_deb_assoc("ASSOC: channel: error getting channel.");
259 if (assoc_req->channel != adapter->curbssparams.channel) {
260 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
265 if ( assoc_req->secinfo.wep_enabled
266 && (assoc_req->wep_keys[0].len
267 || assoc_req->wep_keys[1].len
268 || assoc_req->wep_keys[2].len
269 || assoc_req->wep_keys[3].len)) {
270 /* Make sure WEP keys are re-sent to firmware */
271 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
274 /* Must restart/rejoin adhoc networks after channel change */
275 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
278 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
283 static int assoc_helper_wep_keys(wlan_private *priv,
284 struct assoc_request * assoc_req)
286 wlan_adapter *adapter = priv->adapter;
290 lbs_deb_enter(LBS_DEB_ASSOC);
292 /* Set or remove WEP keys */
293 if ( assoc_req->wep_keys[0].len
294 || assoc_req->wep_keys[1].len
295 || assoc_req->wep_keys[2].len
296 || assoc_req->wep_keys[3].len) {
297 ret = libertas_prepare_and_send_command(priv,
300 cmd_option_waitforrsp,
303 ret = libertas_prepare_and_send_command(priv,
306 cmd_option_waitforrsp,
313 /* enable/disable the MAC's WEP packet filter */
314 if (assoc_req->secinfo.wep_enabled)
315 adapter->currentpacketfilter |= cmd_act_mac_wep_enable;
317 adapter->currentpacketfilter &= ~cmd_act_mac_wep_enable;
318 ret = libertas_set_mac_packet_filter(priv);
322 mutex_lock(&adapter->lock);
324 /* Copy WEP keys into adapter wep key fields */
325 for (i = 0; i < 4; i++) {
326 memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
327 sizeof(struct WLAN_802_11_KEY));
329 adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
331 mutex_unlock(&adapter->lock);
334 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
338 static int assoc_helper_secinfo(wlan_private *priv,
339 struct assoc_request * assoc_req)
341 wlan_adapter *adapter = priv->adapter;
344 lbs_deb_enter(LBS_DEB_ASSOC);
346 memcpy(&adapter->secinfo, &assoc_req->secinfo,
347 sizeof(struct wlan_802_11_security));
349 ret = libertas_set_mac_packet_filter(priv);
351 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
356 static int assoc_helper_wpa_keys(wlan_private *priv,
357 struct assoc_request * assoc_req)
361 lbs_deb_enter(LBS_DEB_ASSOC);
363 /* enable/Disable RSN */
364 ret = libertas_prepare_and_send_command(priv,
365 cmd_802_11_enable_rsn,
367 cmd_option_waitforrsp,
372 ret = libertas_prepare_and_send_command(priv,
373 cmd_802_11_key_material,
375 cmd_option_waitforrsp,
379 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
384 static int assoc_helper_wpa_ie(wlan_private *priv,
385 struct assoc_request * assoc_req)
387 wlan_adapter *adapter = priv->adapter;
390 lbs_deb_enter(LBS_DEB_ASSOC);
392 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
393 memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
394 adapter->wpa_ie_len = assoc_req->wpa_ie_len;
396 memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
397 adapter->wpa_ie_len = 0;
400 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
405 static int should_deauth_infrastructure(wlan_adapter *adapter,
406 struct assoc_request * assoc_req)
408 if (adapter->connect_status != libertas_connected)
411 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
412 lbs_deb_assoc("Deauthenticating due to new SSID in "
413 " configuration request.\n");
417 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
418 if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
419 lbs_deb_assoc("Deauthenticating due to updated security "
420 "info in configuration request.\n");
425 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
426 lbs_deb_assoc("Deauthenticating due to new BSSID in "
427 " configuration request.\n");
431 /* FIXME: deal with 'auto' mode somehow */
432 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
433 if (assoc_req->mode != IW_MODE_INFRA)
441 static int should_stop_adhoc(wlan_adapter *adapter,
442 struct assoc_request * assoc_req)
444 if (adapter->connect_status != libertas_connected)
447 if (adapter->curbssparams.ssid.ssidlength != assoc_req->ssid.ssidlength)
449 if (memcmp(adapter->curbssparams.ssid.ssid, assoc_req->ssid.ssid,
450 adapter->curbssparams.ssid.ssidlength))
453 /* FIXME: deal with 'auto' mode somehow */
454 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
455 if (assoc_req->mode != IW_MODE_ADHOC)
459 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
460 if (assoc_req->channel != adapter->curbssparams.channel)
468 void libertas_association_worker(struct work_struct *work)
470 wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
471 wlan_adapter *adapter = priv->adapter;
472 struct assoc_request * assoc_req = NULL;
474 int find_any_ssid = 0;
476 lbs_deb_enter(LBS_DEB_ASSOC);
478 mutex_lock(&adapter->lock);
479 assoc_req = adapter->pending_assoc_req;
480 adapter->pending_assoc_req = NULL;
481 adapter->in_progress_assoc_req = assoc_req;
482 mutex_unlock(&adapter->lock);
487 print_assoc_req(__func__, assoc_req);
489 /* If 'any' SSID was specified, find an SSID to associate with */
490 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
491 && !assoc_req->ssid.ssidlength)
494 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
495 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
496 if (compare_ether_addr(assoc_req->bssid, bssid_any)
497 && compare_ether_addr(assoc_req->bssid, bssid_off))
504 ret = libertas_find_best_network_SSID(priv, &assoc_req->ssid,
505 assoc_req->mode, &new_mode);
507 lbs_deb_assoc("Could not find best network\n");
512 /* Ensure we switch to the mode of the AP */
513 if (assoc_req->mode == IW_MODE_AUTO) {
514 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
515 assoc_req->mode = new_mode;
520 * Check if the attributes being changing require deauthentication
521 * from the currently associated infrastructure access point.
523 if (adapter->mode == IW_MODE_INFRA) {
524 if (should_deauth_infrastructure(adapter, assoc_req)) {
525 ret = libertas_send_deauthentication(priv);
527 lbs_deb_assoc("Deauthentication due to new "
528 "configuration request failed: %d\n",
532 } else if (adapter->mode == IW_MODE_ADHOC) {
533 if (should_stop_adhoc(adapter, assoc_req)) {
534 ret = libertas_stop_adhoc_network(priv);
536 lbs_deb_assoc("Teardown of AdHoc network due to "
537 "new configuration request failed: %d\n",
544 /* Send the various configuration bits to the firmware */
545 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
546 ret = assoc_helper_mode(priv, assoc_req);
548 lbs_deb_assoc("ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
553 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
554 ret = assoc_helper_channel(priv, assoc_req);
556 lbs_deb_assoc("ASSOC(:%d) channel: ret = %d\n",
562 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
563 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
564 ret = assoc_helper_wep_keys(priv, assoc_req);
566 lbs_deb_assoc("ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
571 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
572 ret = assoc_helper_secinfo(priv, assoc_req);
574 lbs_deb_assoc("ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
579 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
580 ret = assoc_helper_wpa_ie(priv, assoc_req);
582 lbs_deb_assoc("ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
587 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
588 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
589 ret = assoc_helper_wpa_keys(priv, assoc_req);
591 lbs_deb_assoc("ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
596 /* SSID/BSSID should be the _last_ config option set, because they
597 * trigger the association attempt.
599 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
600 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
603 ret = assoc_helper_associate(priv, assoc_req);
605 lbs_deb_assoc("ASSOC: association attempt unsuccessful: %d\n",
610 if (adapter->connect_status != libertas_connected) {
611 lbs_deb_assoc("ASSOC: assoication attempt unsuccessful, "
617 lbs_deb_assoc("ASSOC: association attempt successful. "
618 "Associated to '%s' (" MAC_FMT ")\n",
619 libertas_escape_essid(adapter->curbssparams.ssid.ssid,
620 adapter->curbssparams.ssid.ssidlength),
621 MAC_ARG(adapter->curbssparams.bssid));
622 libertas_prepare_and_send_command(priv,
624 0, cmd_option_waitforrsp, 0, NULL);
626 libertas_prepare_and_send_command(priv,
628 0, cmd_option_waitforrsp, 0, NULL);
637 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
641 mutex_lock(&adapter->lock);
642 adapter->in_progress_assoc_req = NULL;
643 mutex_unlock(&adapter->lock);
647 lbs_deb_leave(LBS_DEB_ASSOC);
652 * Caller MUST hold any necessary locks
654 struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
656 struct assoc_request * assoc_req;
658 if (!adapter->pending_assoc_req) {
659 adapter->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
661 if (!adapter->pending_assoc_req) {
662 lbs_pr_info("Not enough memory to allocate association"
668 /* Copy current configuration attributes to the association request,
669 * but don't overwrite any that are already set.
671 assoc_req = adapter->pending_assoc_req;
672 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
673 memcpy(&assoc_req->ssid, &adapter->curbssparams.ssid,
674 sizeof(struct WLAN_802_11_SSID));
677 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
678 assoc_req->channel = adapter->curbssparams.channel;
680 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
681 assoc_req->band = adapter->curbssparams.band;
683 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
684 assoc_req->mode = adapter->mode;
686 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
687 memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
691 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
693 for (i = 0; i < 4; i++) {
694 memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
695 sizeof(struct WLAN_802_11_KEY));
699 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
700 assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
702 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
703 memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
704 sizeof(struct WLAN_802_11_KEY));
707 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
708 memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
709 sizeof(struct WLAN_802_11_KEY));
712 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
713 memcpy(&assoc_req->secinfo, &adapter->secinfo,
714 sizeof(struct wlan_802_11_security));
717 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
718 memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
720 assoc_req->wpa_ie_len = adapter->wpa_ie_len;
723 print_assoc_req(__func__, assoc_req);