1 /* Copyright (C) 2006, Red Hat, Inc. */
3 #include <linux/bitops.h>
4 #include <net/ieee80211.h>
13 static const u8 bssid_any[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
14 static const u8 bssid_off[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
16 static int assoc_helper_essid(wlan_private *priv,
17 struct assoc_request * assoc_req)
19 wlan_adapter *adapter = priv->adapter;
25 lbs_pr_debug(1, "New SSID requested: %s\n", assoc_req->ssid.ssid);
26 if (assoc_req->mode == IW_MODE_INFRA) {
27 if (adapter->prescan) {
28 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 1);
31 i = libertas_find_SSID_in_list(adapter, &assoc_req->ssid,
35 "SSID found in scan list ... associating...\n");
37 ret = wlan_associate(priv, &adapter->scantable[i]);
39 memcpy(&assoc_req->bssid,
40 &adapter->scantable[i].macaddress,
44 lbs_pr_debug(1, "SSID '%s' not found; cannot associate\n",
45 assoc_req->ssid.ssid);
47 } else if (assoc_req->mode == IW_MODE_ADHOC) {
48 /* Scan for the network, do not save previous results. Stale
49 * scan data will cause us to join a non-existant adhoc network
51 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 0);
53 /* Search for the requested SSID in the scan table */
54 i = libertas_find_SSID_in_list(adapter, &assoc_req->ssid, NULL,
57 lbs_pr_debug(1, "SSID found at %d in List, so join\n", ret);
58 libertas_join_adhoc_network(priv, &adapter->scantable[i]);
60 /* else send START command */
61 lbs_pr_debug(1, "SSID not found in list, so creating adhoc"
62 " with SSID '%s'\n", assoc_req->ssid.ssid);
63 libertas_start_adhoc_network(priv, &assoc_req->ssid);
65 memcpy(&assoc_req->bssid, &adapter->current_addr, ETH_ALEN);
73 static int assoc_helper_bssid(wlan_private *priv,
74 struct assoc_request * assoc_req)
76 wlan_adapter *adapter = priv->adapter;
81 lbs_pr_debug(1, "ASSOC: WAP: BSSID = " MAC_FMT "\n",
82 MAC_ARG(assoc_req->bssid));
84 /* Search for index position in list for requested MAC */
85 i = libertas_find_BSSID_in_list(adapter, assoc_req->bssid,
88 lbs_pr_debug(1, "ASSOC: WAP: BSSID " MAC_FMT " not found, "
89 "cannot associate.\n", MAC_ARG(assoc_req->bssid));
93 if (assoc_req->mode == IW_MODE_INFRA) {
94 ret = wlan_associate(priv, &adapter->scantable[i]);
95 lbs_pr_debug(1, "ASSOC: return from wlan_associate(bssd) was %d\n", ret);
96 } else if (assoc_req->mode == IW_MODE_ADHOC) {
97 libertas_join_adhoc_network(priv, &adapter->scantable[i]);
99 memcpy(&assoc_req->ssid, &adapter->scantable[i].ssid,
100 sizeof(struct WLAN_802_11_SSID));
108 static int assoc_helper_associate(wlan_private *priv,
109 struct assoc_request * assoc_req)
111 int ret = 0, done = 0;
113 /* If we're given and 'any' BSSID, try associating based on SSID */
115 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
116 if (memcmp(bssid_any, assoc_req->bssid, ETH_ALEN)
117 && memcmp(bssid_off, assoc_req->bssid, ETH_ALEN)) {
118 ret = assoc_helper_bssid(priv, assoc_req);
121 lbs_pr_debug(1, "ASSOC: bssid: ret = %d\n", ret);
126 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
127 ret = assoc_helper_essid(priv, assoc_req);
129 lbs_pr_debug(1, "ASSOC: bssid: ret = %d\n", ret);
137 static int assoc_helper_mode(wlan_private *priv,
138 struct assoc_request * assoc_req)
140 wlan_adapter *adapter = priv->adapter;
145 if (assoc_req->mode == adapter->mode) {
150 if (assoc_req->mode == IW_MODE_INFRA) {
151 if (adapter->psstate != PS_STATE_FULL_POWER)
152 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
153 adapter->psmode = wlan802_11powermodecam;
156 adapter->mode = assoc_req->mode;
157 ret = libertas_prepare_and_send_command(priv,
159 0, cmd_option_waitforrsp,
160 OID_802_11_INFRASTRUCTURE_MODE,
161 (void *) (size_t) assoc_req->mode);
168 static int assoc_helper_wep_keys(wlan_private *priv,
169 struct assoc_request * assoc_req)
171 wlan_adapter *adapter = priv->adapter;
177 /* Set or remove WEP keys */
178 if ( assoc_req->wep_keys[0].len
179 || assoc_req->wep_keys[1].len
180 || assoc_req->wep_keys[2].len
181 || assoc_req->wep_keys[3].len) {
182 ret = libertas_prepare_and_send_command(priv,
185 cmd_option_waitforrsp,
188 ret = libertas_prepare_and_send_command(priv,
191 cmd_option_waitforrsp,
198 /* enable/disable the MAC's WEP packet filter */
199 if (assoc_req->secinfo.wep_enabled)
200 adapter->currentpacketfilter |= cmd_act_mac_wep_enable;
202 adapter->currentpacketfilter &= ~cmd_act_mac_wep_enable;
203 ret = libertas_set_mac_packet_filter(priv);
207 mutex_lock(&adapter->lock);
209 /* Copy WEP keys into adapter wep key fields */
210 for (i = 0; i < 4; i++) {
211 memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
212 sizeof(struct WLAN_802_11_KEY));
214 adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
216 mutex_unlock(&adapter->lock);
223 static int assoc_helper_secinfo(wlan_private *priv,
224 struct assoc_request * assoc_req)
226 wlan_adapter *adapter = priv->adapter;
231 memcpy(&adapter->secinfo, &assoc_req->secinfo,
232 sizeof(struct wlan_802_11_security));
234 ret = libertas_set_mac_packet_filter(priv);
241 static int assoc_helper_wpa_keys(wlan_private *priv,
242 struct assoc_request * assoc_req)
248 /* enable/Disable RSN */
249 ret = libertas_prepare_and_send_command(priv,
250 cmd_802_11_enable_rsn,
252 cmd_option_waitforrsp,
257 ret = libertas_prepare_and_send_command(priv,
258 cmd_802_11_key_material,
260 cmd_option_waitforrsp,
269 static int assoc_helper_wpa_ie(wlan_private *priv,
270 struct assoc_request * assoc_req)
272 wlan_adapter *adapter = priv->adapter;
277 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
278 memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
279 adapter->wpa_ie_len = assoc_req->wpa_ie_len;
281 memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
282 adapter->wpa_ie_len = 0;
290 static int should_deauth_infrastructure(wlan_adapter *adapter,
291 struct assoc_request * assoc_req)
293 if (adapter->connect_status != libertas_connected)
296 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
297 lbs_pr_debug(1, "Deauthenticating due to new SSID in "
298 " configuration request.\n");
302 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
303 if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
304 lbs_pr_debug(1, "Deauthenticating due to updated security "
305 "info in configuration request.\n");
310 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
311 lbs_pr_debug(1, "Deauthenticating due to new BSSID in "
312 " configuration request.\n");
316 /* FIXME: deal with 'auto' mode somehow */
317 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
318 if (assoc_req->mode != IW_MODE_INFRA)
326 static int should_stop_adhoc(wlan_adapter *adapter,
327 struct assoc_request * assoc_req)
329 if (adapter->connect_status != libertas_connected)
332 if (adapter->curbssparams.ssid.ssidlength != assoc_req->ssid.ssidlength)
334 if (memcmp(adapter->curbssparams.ssid.ssid, assoc_req->ssid.ssid,
335 adapter->curbssparams.ssid.ssidlength))
338 /* FIXME: deal with 'auto' mode somehow */
339 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
340 if (assoc_req->mode != IW_MODE_ADHOC)
348 void wlan_association_worker(struct work_struct *work)
350 wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
351 wlan_adapter *adapter = priv->adapter;
352 struct assoc_request * assoc_req = NULL;
354 int find_any_ssid = 0;
358 mutex_lock(&adapter->lock);
359 assoc_req = adapter->assoc_req;
360 adapter->assoc_req = NULL;
361 mutex_unlock(&adapter->lock);
368 lbs_pr_debug(1, "ASSOC: starting new association request: flags = 0x%lX\n",
371 /* If 'any' SSID was specified, find an SSID to associate with */
372 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
373 && !assoc_req->ssid.ssidlength)
376 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
377 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
378 if (memcmp(&assoc_req->bssid, bssid_any, ETH_ALEN)
379 && memcmp(&assoc_req->bssid, bssid_off, ETH_ALEN))
386 ret = libertas_find_best_network_SSID(priv, &assoc_req->ssid,
387 assoc_req->mode, &new_mode);
389 lbs_pr_debug(1, "Could not find best network\n");
394 /* Ensure we switch to the mode of the AP */
395 if (assoc_req->mode == IW_MODE_AUTO) {
396 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
397 assoc_req->mode = new_mode;
402 * Check if the attributes being changing require deauthentication
403 * from the currently associated infrastructure access point.
405 if (adapter->mode == IW_MODE_INFRA) {
406 if (should_deauth_infrastructure(adapter, assoc_req)) {
407 ret = libertas_send_deauthentication(priv);
409 lbs_pr_debug(1, "Deauthentication due to new "
410 "configuration request failed: %d\n",
414 } else if (adapter->mode == IW_MODE_ADHOC) {
415 if (should_stop_adhoc(adapter, assoc_req)) {
416 ret = libertas_stop_adhoc_network(priv);
418 lbs_pr_debug(1, "Teardown of AdHoc network due to "
419 "new configuration request failed: %d\n",
426 /* Send the various configuration bits to the firmware */
427 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
428 ret = assoc_helper_mode(priv, assoc_req);
430 lbs_pr_debug(1, "ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
435 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
436 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
437 ret = assoc_helper_wep_keys(priv, assoc_req);
439 lbs_pr_debug(1, "ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
444 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
445 ret = assoc_helper_secinfo(priv, assoc_req);
447 lbs_pr_debug(1, "ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
452 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
453 ret = assoc_helper_wpa_ie(priv, assoc_req);
455 lbs_pr_debug(1, "ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
460 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
461 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
462 ret = assoc_helper_wpa_keys(priv, assoc_req);
464 lbs_pr_debug(1, "ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
469 /* SSID/BSSID should be the _last_ config option set, because they
470 * trigger the association attempt.
472 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
473 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
476 ret = assoc_helper_associate(priv, assoc_req);
478 lbs_pr_debug(1, "ASSOC: association attempt unsuccessful: %d\n",
483 if (adapter->connect_status != libertas_connected) {
484 lbs_pr_debug(1, "ASSOC: assoication attempt unsuccessful, "
490 lbs_pr_debug(1, "ASSOC: association attempt successful. "
491 "Associated to '%s' (" MAC_FMT ")\n",
492 assoc_req->ssid.ssid, MAC_ARG(assoc_req->bssid));
493 libertas_prepare_and_send_command(priv,
495 0, cmd_option_waitforrsp, 0, NULL);
497 libertas_prepare_and_send_command(priv,
499 0, cmd_option_waitforrsp, 0, NULL);
508 lbs_pr_debug(1, "ASSOC: reconfiguration attempt unsuccessful: %d\n",
517 * Caller MUST hold any necessary locks
519 struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
521 struct assoc_request * assoc_req;
523 if (!adapter->assoc_req) {
524 adapter->assoc_req = kzalloc(sizeof(struct assoc_request), GFP_KERNEL);
525 if (!adapter->assoc_req) {
526 lbs_pr_info("Not enough memory to allocate association"
532 /* Copy current configuration attributes to the association request,
533 * but don't overwrite any that are already set.
535 assoc_req = adapter->assoc_req;
536 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
537 memcpy(&assoc_req->ssid, adapter->curbssparams.ssid.ssid,
538 adapter->curbssparams.ssid.ssidlength);
541 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
542 assoc_req->channel = adapter->curbssparams.channel;
544 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
545 assoc_req->mode = adapter->mode;
547 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
548 memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
552 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
554 for (i = 0; i < 4; i++) {
555 memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
556 sizeof(struct WLAN_802_11_KEY));
560 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
561 assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
563 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
564 memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
565 sizeof(struct WLAN_802_11_KEY));
568 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
569 memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
570 sizeof(struct WLAN_802_11_KEY));
573 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
574 memcpy(&assoc_req->secinfo, &adapter->secinfo,
575 sizeof(struct wlan_802_11_security));
578 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
579 memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
581 assoc_req->wpa_ie_len = adapter->wpa_ie_len;