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 == wlan802_11infrastructure) {
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,
32 NULL, wlan802_11infrastructure);
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 == wlan802_11ibss) {
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 == wlan802_11infrastructure) {
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 == wlan802_11ibss) {
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->inframode) {
150 if (assoc_req->mode == wlan802_11infrastructure) {
151 if (adapter->psstate != PS_STATE_FULL_POWER)
152 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
153 adapter->psmode = wlan802_11powermodecam;
156 adapter->inframode = assoc_req->mode;
157 ret = libertas_prepare_and_send_command(priv,
159 0, cmd_option_waitforrsp,
160 OID_802_11_INFRASTRUCTURE_MODE,
161 (void *) 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.WEPstatus == wlan802_11WEPenabled)
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.authmode !=
304 assoc_req->secinfo.authmode) {
305 lbs_pr_debug(1, "Deauthenticating due to updated security "
306 "info in configuration request.\n");
311 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
312 lbs_pr_debug(1, "Deauthenticating due to new BSSID in "
313 " configuration request.\n");
317 /* FIXME: deal with 'auto' mode somehow */
318 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
319 if (assoc_req->mode != wlan802_11infrastructure)
327 static int should_stop_adhoc(wlan_adapter *adapter,
328 struct assoc_request * assoc_req)
330 if (adapter->connect_status != libertas_connected)
333 if (adapter->curbssparams.ssid.ssidlength != assoc_req->ssid.ssidlength)
335 if (memcmp(adapter->curbssparams.ssid.ssid, assoc_req->ssid.ssid,
336 sizeof(struct WLAN_802_11_SSID)))
339 /* FIXME: deal with 'auto' mode somehow */
340 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
341 if (assoc_req->mode != wlan802_11ibss)
349 void wlan_association_worker(struct work_struct *work)
351 wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
352 wlan_adapter *adapter = priv->adapter;
353 struct assoc_request * assoc_req = NULL;
355 int find_any_ssid = 0;
359 mutex_lock(&adapter->lock);
360 assoc_req = adapter->assoc_req;
361 adapter->assoc_req = NULL;
362 mutex_unlock(&adapter->lock);
369 lbs_pr_debug(1, "ASSOC: starting new association request: flags = 0x%lX\n",
372 /* If 'any' SSID was specified, find an SSID to associate with */
373 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
374 && !assoc_req->ssid.ssidlength)
377 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
378 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
379 if (memcmp(&assoc_req->bssid, bssid_any, ETH_ALEN)
380 && memcmp(&assoc_req->bssid, bssid_off, ETH_ALEN))
385 enum WLAN_802_11_NETWORK_INFRASTRUCTURE new_mode;
387 ret = libertas_find_best_network_SSID(priv, &assoc_req->ssid,
388 assoc_req->mode, &new_mode);
390 lbs_pr_debug(1, "Could not find best network\n");
395 /* Ensure we switch to the mode of the AP */
396 if (assoc_req->mode == wlan802_11autounknown) {
397 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
398 assoc_req->mode = new_mode;
403 * Check if the attributes being changing require deauthentication
404 * from the currently associated infrastructure access point.
406 if (adapter->inframode == wlan802_11infrastructure) {
407 if (should_deauth_infrastructure(adapter, assoc_req)) {
408 ret = libertas_send_deauthentication(priv);
410 lbs_pr_debug(1, "Deauthentication due to new "
411 "configuration request failed: %d\n",
415 } else if (adapter->inframode == wlan802_11ibss) {
416 if (should_stop_adhoc(adapter, assoc_req)) {
417 ret = libertas_stop_adhoc_network(priv);
419 lbs_pr_debug(1, "Teardown of AdHoc network due to "
420 "new configuration request failed: %d\n",
427 /* Send the various configuration bits to the firmware */
428 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
429 ret = assoc_helper_mode(priv, assoc_req);
431 lbs_pr_debug(1, "ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
436 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
437 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
438 ret = assoc_helper_wep_keys(priv, assoc_req);
440 lbs_pr_debug(1, "ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
445 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
446 ret = assoc_helper_secinfo(priv, assoc_req);
448 lbs_pr_debug(1, "ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
453 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
454 ret = assoc_helper_wpa_ie(priv, assoc_req);
456 lbs_pr_debug(1, "ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
461 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
462 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
463 ret = assoc_helper_wpa_keys(priv, assoc_req);
465 lbs_pr_debug(1, "ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
470 /* SSID/BSSID should be the _last_ config option set, because they
471 * trigger the association attempt.
473 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
474 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
477 ret = assoc_helper_associate(priv, assoc_req);
479 lbs_pr_debug(1, "ASSOC: association attempt unsuccessful: %d\n",
484 if (adapter->connect_status != libertas_connected) {
485 lbs_pr_debug(1, "ASSOC: assoication attempt unsuccessful, "
491 lbs_pr_debug(1, "ASSOC: association attempt successful. "
492 "Associated to '%s' (" MAC_FMT ")\n",
493 assoc_req->ssid.ssid, MAC_ARG(assoc_req->bssid));
494 libertas_prepare_and_send_command(priv,
496 0, cmd_option_waitforrsp, 0, NULL);
498 libertas_prepare_and_send_command(priv,
500 0, cmd_option_waitforrsp, 0, NULL);
509 lbs_pr_debug(1, "ASSOC: reconfiguration attempt unsuccessful: %d\n",
518 * Caller MUST hold any necessary locks
520 struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
522 struct assoc_request * assoc_req;
524 if (!adapter->assoc_req) {
525 adapter->assoc_req = kzalloc(sizeof(struct assoc_request), GFP_KERNEL);
526 if (!adapter->assoc_req) {
527 lbs_pr_info("Not enough memory to allocate association"
533 /* Copy current configuration attributes to the association request,
534 * but don't overwrite any that are already set.
536 assoc_req = adapter->assoc_req;
537 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
538 memcpy(&assoc_req->ssid, adapter->curbssparams.ssid.ssid,
539 adapter->curbssparams.ssid.ssidlength);
542 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
543 assoc_req->channel = adapter->curbssparams.channel;
545 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
546 assoc_req->mode = adapter->inframode;
548 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
549 memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
553 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
555 for (i = 0; i < 4; i++) {
556 memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
557 sizeof(struct WLAN_802_11_KEY));
561 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
562 assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
564 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
565 memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
566 sizeof(struct WLAN_802_11_KEY));
569 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
570 memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
571 sizeof(struct WLAN_802_11_KEY));
574 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
575 memcpy(&assoc_req->secinfo, &adapter->secinfo,
576 sizeof(struct wlan_802_11_security));
579 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
580 memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
582 assoc_req->wpa_ie_len = adapter->wpa_ie_len;