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)
20 "#### Association Request: %s\n"
26 " BSSID: " MAC_FMT "\n"
27 " Encryption:%s%s%s\n"
29 extra, assoc_req->flags,
30 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
31 assoc_req->channel, assoc_req->band, assoc_req->mode,
32 MAC_ARG(assoc_req->bssid),
33 assoc_req->secinfo.WPAenabled ? " WPA" : "",
34 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
35 assoc_req->secinfo.wep_enabled ? " WEP" : "",
36 assoc_req->secinfo.auth_mode);
40 static int assoc_helper_essid(wlan_private *priv,
41 struct assoc_request * assoc_req)
43 wlan_adapter *adapter = priv->adapter;
45 struct bss_descriptor * bss;
48 lbs_deb_enter(LBS_DEB_ASSOC);
50 /* FIXME: take channel into account when picking SSIDs if a channel
54 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
55 channel = assoc_req->channel;
57 lbs_deb_assoc("New SSID requested: '%s'\n",
58 escape_essid(assoc_req->ssid, assoc_req->ssid_len));
59 if (assoc_req->mode == IW_MODE_INFRA) {
60 if (adapter->prescan) {
61 libertas_send_specific_ssid_scan(priv, assoc_req->ssid,
62 assoc_req->ssid_len, 0);
65 bss = libertas_find_ssid_in_list(adapter, assoc_req->ssid,
66 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
68 lbs_deb_assoc("SSID found in scan list, associating\n");
69 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
70 ret = wlan_associate(priv, assoc_req);
72 lbs_deb_assoc("SSID not found; cannot associate\n");
74 } else if (assoc_req->mode == IW_MODE_ADHOC) {
75 /* Scan for the network, do not save previous results. Stale
76 * scan data will cause us to join a non-existant adhoc network
78 libertas_send_specific_ssid_scan(priv, assoc_req->ssid,
79 assoc_req->ssid_len, 1);
81 /* Search for the requested SSID in the scan table */
82 bss = libertas_find_ssid_in_list(adapter, assoc_req->ssid,
83 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
85 lbs_deb_assoc("SSID found, will join\n");
86 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
87 libertas_join_adhoc_network(priv, assoc_req);
89 /* else send START command */
90 lbs_deb_assoc("SSID not found, creating adhoc network\n");
91 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
93 assoc_req->bss.ssid_len = assoc_req->ssid_len;
94 libertas_start_adhoc_network(priv, assoc_req);
98 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
103 static int assoc_helper_bssid(wlan_private *priv,
104 struct assoc_request * assoc_req)
106 wlan_adapter *adapter = priv->adapter;
108 struct bss_descriptor * bss;
110 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID " MAC_FMT,
111 MAC_ARG(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 " MAC_FMT " not found, "
118 "cannot associate.\n", MAC_ARG(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 static int assoc_helper_channel(wlan_private *priv,
204 struct assoc_request * assoc_req)
206 wlan_adapter *adapter = priv->adapter;
209 lbs_deb_enter(LBS_DEB_ASSOC);
211 ret = update_channel(priv);
213 lbs_deb_assoc("ASSOC: channel: error getting channel.");
216 if (assoc_req->channel == adapter->curbssparams.channel)
219 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
220 adapter->curbssparams.channel, assoc_req->channel);
222 ret = libertas_prepare_and_send_command(priv, cmd_802_11_rf_channel,
223 cmd_opt_802_11_rf_channel_set,
224 cmd_option_waitforrsp, 0, &assoc_req->channel);
226 lbs_deb_assoc("ASSOC: channel: error setting channel.");
229 ret = update_channel(priv);
231 lbs_deb_assoc("ASSOC: channel: error getting channel.");
234 if (assoc_req->channel != adapter->curbssparams.channel) {
235 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
240 if ( assoc_req->secinfo.wep_enabled
241 && (assoc_req->wep_keys[0].len
242 || assoc_req->wep_keys[1].len
243 || assoc_req->wep_keys[2].len
244 || assoc_req->wep_keys[3].len)) {
245 /* Make sure WEP keys are re-sent to firmware */
246 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
249 /* Must restart/rejoin adhoc networks after channel change */
250 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
253 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
258 static int assoc_helper_wep_keys(wlan_private *priv,
259 struct assoc_request * assoc_req)
261 wlan_adapter *adapter = priv->adapter;
265 lbs_deb_enter(LBS_DEB_ASSOC);
267 /* Set or remove WEP keys */
268 if ( assoc_req->wep_keys[0].len
269 || assoc_req->wep_keys[1].len
270 || assoc_req->wep_keys[2].len
271 || assoc_req->wep_keys[3].len) {
272 ret = libertas_prepare_and_send_command(priv,
275 cmd_option_waitforrsp,
278 ret = libertas_prepare_and_send_command(priv,
281 cmd_option_waitforrsp,
288 /* enable/disable the MAC's WEP packet filter */
289 if (assoc_req->secinfo.wep_enabled)
290 adapter->currentpacketfilter |= cmd_act_mac_wep_enable;
292 adapter->currentpacketfilter &= ~cmd_act_mac_wep_enable;
293 ret = libertas_set_mac_packet_filter(priv);
297 mutex_lock(&adapter->lock);
299 /* Copy WEP keys into adapter wep key fields */
300 for (i = 0; i < 4; i++) {
301 memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
302 sizeof(struct WLAN_802_11_KEY));
304 adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
306 mutex_unlock(&adapter->lock);
309 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
313 static int assoc_helper_secinfo(wlan_private *priv,
314 struct assoc_request * assoc_req)
316 wlan_adapter *adapter = priv->adapter;
319 lbs_deb_enter(LBS_DEB_ASSOC);
321 memcpy(&adapter->secinfo, &assoc_req->secinfo,
322 sizeof(struct wlan_802_11_security));
324 ret = libertas_set_mac_packet_filter(priv);
328 /* enable/disable RSN */
329 ret = libertas_prepare_and_send_command(priv,
330 cmd_802_11_enable_rsn,
332 cmd_option_waitforrsp,
336 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
341 static int assoc_helper_wpa_keys(wlan_private *priv,
342 struct assoc_request * assoc_req)
346 lbs_deb_enter(LBS_DEB_ASSOC);
348 ret = libertas_prepare_and_send_command(priv,
349 cmd_802_11_key_material,
351 cmd_option_waitforrsp,
354 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
359 static int assoc_helper_wpa_ie(wlan_private *priv,
360 struct assoc_request * assoc_req)
362 wlan_adapter *adapter = priv->adapter;
365 lbs_deb_enter(LBS_DEB_ASSOC);
367 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
368 memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
369 adapter->wpa_ie_len = assoc_req->wpa_ie_len;
371 memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
372 adapter->wpa_ie_len = 0;
375 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
380 static int should_deauth_infrastructure(wlan_adapter *adapter,
381 struct assoc_request * assoc_req)
383 if (adapter->connect_status != libertas_connected)
386 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
387 lbs_deb_assoc("Deauthenticating due to new SSID in "
388 " configuration request.\n");
392 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
393 if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
394 lbs_deb_assoc("Deauthenticating due to updated security "
395 "info in configuration request.\n");
400 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
401 lbs_deb_assoc("Deauthenticating due to new BSSID in "
402 " configuration request.\n");
406 /* FIXME: deal with 'auto' mode somehow */
407 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
408 if (assoc_req->mode != IW_MODE_INFRA)
416 static int should_stop_adhoc(wlan_adapter *adapter,
417 struct assoc_request * assoc_req)
419 if (adapter->connect_status != libertas_connected)
422 if (libertas_ssid_cmp(adapter->curbssparams.ssid,
423 adapter->curbssparams.ssid_len,
424 assoc_req->ssid, assoc_req->ssid_len) != 0)
427 /* FIXME: deal with 'auto' mode somehow */
428 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
429 if (assoc_req->mode != IW_MODE_ADHOC)
433 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
434 if (assoc_req->channel != adapter->curbssparams.channel)
442 void libertas_association_worker(struct work_struct *work)
444 wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
445 wlan_adapter *adapter = priv->adapter;
446 struct assoc_request * assoc_req = NULL;
448 int find_any_ssid = 0;
450 lbs_deb_enter(LBS_DEB_ASSOC);
452 mutex_lock(&adapter->lock);
453 assoc_req = adapter->pending_assoc_req;
454 adapter->pending_assoc_req = NULL;
455 adapter->in_progress_assoc_req = assoc_req;
456 mutex_unlock(&adapter->lock);
461 print_assoc_req(__func__, assoc_req);
463 /* If 'any' SSID was specified, find an SSID to associate with */
464 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
465 && !assoc_req->ssid_len)
468 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
469 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
470 if (compare_ether_addr(assoc_req->bssid, bssid_any)
471 && compare_ether_addr(assoc_req->bssid, bssid_off))
478 ret = libertas_find_best_network_ssid(priv, assoc_req->ssid,
479 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
481 lbs_deb_assoc("Could not find best network\n");
486 /* Ensure we switch to the mode of the AP */
487 if (assoc_req->mode == IW_MODE_AUTO) {
488 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
489 assoc_req->mode = new_mode;
494 * Check if the attributes being changing require deauthentication
495 * from the currently associated infrastructure access point.
497 if (adapter->mode == IW_MODE_INFRA) {
498 if (should_deauth_infrastructure(adapter, assoc_req)) {
499 ret = libertas_send_deauthentication(priv);
501 lbs_deb_assoc("Deauthentication due to new "
502 "configuration request failed: %d\n",
506 } else if (adapter->mode == IW_MODE_ADHOC) {
507 if (should_stop_adhoc(adapter, assoc_req)) {
508 ret = libertas_stop_adhoc_network(priv);
510 lbs_deb_assoc("Teardown of AdHoc network due to "
511 "new configuration request failed: %d\n",
518 /* Send the various configuration bits to the firmware */
519 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
520 ret = assoc_helper_mode(priv, assoc_req);
522 lbs_deb_assoc("ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
527 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
528 ret = assoc_helper_channel(priv, assoc_req);
530 lbs_deb_assoc("ASSOC(:%d) channel: ret = %d\n",
536 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
537 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
538 ret = assoc_helper_wep_keys(priv, assoc_req);
540 lbs_deb_assoc("ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
545 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
546 ret = assoc_helper_secinfo(priv, assoc_req);
548 lbs_deb_assoc("ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
553 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
554 ret = assoc_helper_wpa_ie(priv, assoc_req);
556 lbs_deb_assoc("ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
561 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
562 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
563 ret = assoc_helper_wpa_keys(priv, assoc_req);
565 lbs_deb_assoc("ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
570 /* SSID/BSSID should be the _last_ config option set, because they
571 * trigger the association attempt.
573 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
574 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
577 ret = assoc_helper_associate(priv, assoc_req);
579 lbs_deb_assoc("ASSOC: association attempt unsuccessful: %d\n",
584 if (adapter->connect_status != libertas_connected) {
585 lbs_deb_assoc("ASSOC: assoication attempt unsuccessful, "
591 lbs_deb_assoc("ASSOC: association attempt successful. "
592 "Associated to '%s' (" MAC_FMT ")\n",
593 escape_essid(adapter->curbssparams.ssid,
594 adapter->curbssparams.ssid_len),
595 MAC_ARG(adapter->curbssparams.bssid));
596 libertas_prepare_and_send_command(priv,
598 0, cmd_option_waitforrsp, 0, NULL);
600 libertas_prepare_and_send_command(priv,
602 0, cmd_option_waitforrsp, 0, NULL);
610 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
614 mutex_lock(&adapter->lock);
615 adapter->in_progress_assoc_req = NULL;
616 mutex_unlock(&adapter->lock);
620 lbs_deb_leave(LBS_DEB_ASSOC);
625 * Caller MUST hold any necessary locks
627 struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
629 struct assoc_request * assoc_req;
631 if (!adapter->pending_assoc_req) {
632 adapter->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
634 if (!adapter->pending_assoc_req) {
635 lbs_pr_info("Not enough memory to allocate association"
641 /* Copy current configuration attributes to the association request,
642 * but don't overwrite any that are already set.
644 assoc_req = adapter->pending_assoc_req;
645 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
646 memcpy(&assoc_req->ssid, &adapter->curbssparams.ssid,
648 assoc_req->ssid_len = adapter->curbssparams.ssid_len;
651 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
652 assoc_req->channel = adapter->curbssparams.channel;
654 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
655 assoc_req->band = adapter->curbssparams.band;
657 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
658 assoc_req->mode = adapter->mode;
660 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
661 memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
665 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
667 for (i = 0; i < 4; i++) {
668 memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
669 sizeof(struct WLAN_802_11_KEY));
673 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
674 assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
676 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
677 memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
678 sizeof(struct WLAN_802_11_KEY));
681 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
682 memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
683 sizeof(struct WLAN_802_11_KEY));
686 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
687 memcpy(&assoc_req->secinfo, &adapter->secinfo,
688 sizeof(struct wlan_802_11_security));
691 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
692 memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
694 assoc_req->wpa_ie_len = adapter->wpa_ie_len;
697 print_assoc_req(__func__, assoc_req);