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;
21 struct bss_descriptor * bss;
23 lbs_deb_enter(LBS_DEB_ASSOC);
25 lbs_deb_assoc("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 bss = libertas_find_SSID_in_list(adapter, &assoc_req->ssid,
34 lbs_deb_assoc("SSID found in scan list, associating\n");
35 ret = wlan_associate(priv, bss);
37 memcpy(&assoc_req->bssid, bss->bssid, ETH_ALEN);
40 lbs_deb_assoc("SSID '%s' not found; cannot associate\n",
41 assoc_req->ssid.ssid);
43 } else if (assoc_req->mode == IW_MODE_ADHOC) {
44 /* Scan for the network, do not save previous results. Stale
45 * scan data will cause us to join a non-existant adhoc network
47 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 0);
49 /* Search for the requested SSID in the scan table */
50 bss = libertas_find_SSID_in_list(adapter, &assoc_req->ssid, NULL,
53 lbs_deb_assoc("SSID found joining\n");
54 libertas_join_adhoc_network(priv, bss);
56 /* else send START command */
57 lbs_deb_assoc("SSID not found in list, so creating adhoc"
58 " with SSID '%s'\n", assoc_req->ssid.ssid);
59 libertas_start_adhoc_network(priv, &assoc_req->ssid);
61 memcpy(&assoc_req->bssid, &adapter->current_addr, ETH_ALEN);
64 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
69 static int assoc_helper_bssid(wlan_private *priv,
70 struct assoc_request * assoc_req)
72 wlan_adapter *adapter = priv->adapter;
74 struct bss_descriptor * bss;
76 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID" MAC_FMT "\n",
77 MAC_ARG(assoc_req->bssid));
79 /* Search for index position in list for requested MAC */
80 bss = libertas_find_BSSID_in_list(adapter, assoc_req->bssid,
83 lbs_deb_assoc("ASSOC: WAP: BSSID " MAC_FMT " not found, "
84 "cannot associate.\n", MAC_ARG(assoc_req->bssid));
88 if (assoc_req->mode == IW_MODE_INFRA) {
89 ret = wlan_associate(priv, bss);
90 lbs_deb_assoc("ASSOC: wlan_associate(bssid) returned %d\n", ret);
91 } else if (assoc_req->mode == IW_MODE_ADHOC) {
92 libertas_join_adhoc_network(priv, bss);
94 memcpy(&assoc_req->ssid, &bss->ssid, sizeof(struct WLAN_802_11_SSID));
97 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
102 static int assoc_helper_associate(wlan_private *priv,
103 struct assoc_request * assoc_req)
105 int ret = 0, done = 0;
107 /* If we're given and 'any' BSSID, try associating based on SSID */
109 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
110 if (memcmp(bssid_any, assoc_req->bssid, ETH_ALEN)
111 && memcmp(bssid_off, assoc_req->bssid, ETH_ALEN)) {
112 ret = assoc_helper_bssid(priv, assoc_req);
115 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
120 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
121 ret = assoc_helper_essid(priv, assoc_req);
123 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
131 static int assoc_helper_mode(wlan_private *priv,
132 struct assoc_request * assoc_req)
134 wlan_adapter *adapter = priv->adapter;
137 lbs_deb_enter(LBS_DEB_ASSOC);
139 if (assoc_req->mode == adapter->mode)
142 if (assoc_req->mode == IW_MODE_INFRA) {
143 if (adapter->psstate != PS_STATE_FULL_POWER)
144 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
145 adapter->psmode = wlan802_11powermodecam;
148 adapter->mode = assoc_req->mode;
149 ret = libertas_prepare_and_send_command(priv,
151 0, cmd_option_waitforrsp,
152 OID_802_11_INFRASTRUCTURE_MODE,
153 (void *) (size_t) assoc_req->mode);
156 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
161 static int assoc_helper_wep_keys(wlan_private *priv,
162 struct assoc_request * assoc_req)
164 wlan_adapter *adapter = priv->adapter;
168 lbs_deb_enter(LBS_DEB_ASSOC);
170 /* Set or remove WEP keys */
171 if ( assoc_req->wep_keys[0].len
172 || assoc_req->wep_keys[1].len
173 || assoc_req->wep_keys[2].len
174 || assoc_req->wep_keys[3].len) {
175 ret = libertas_prepare_and_send_command(priv,
178 cmd_option_waitforrsp,
181 ret = libertas_prepare_and_send_command(priv,
184 cmd_option_waitforrsp,
191 /* enable/disable the MAC's WEP packet filter */
192 if (assoc_req->secinfo.wep_enabled)
193 adapter->currentpacketfilter |= cmd_act_mac_wep_enable;
195 adapter->currentpacketfilter &= ~cmd_act_mac_wep_enable;
196 ret = libertas_set_mac_packet_filter(priv);
200 mutex_lock(&adapter->lock);
202 /* Copy WEP keys into adapter wep key fields */
203 for (i = 0; i < 4; i++) {
204 memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
205 sizeof(struct WLAN_802_11_KEY));
207 adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
209 mutex_unlock(&adapter->lock);
212 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
216 static int assoc_helper_secinfo(wlan_private *priv,
217 struct assoc_request * assoc_req)
219 wlan_adapter *adapter = priv->adapter;
222 lbs_deb_enter(LBS_DEB_ASSOC);
224 memcpy(&adapter->secinfo, &assoc_req->secinfo,
225 sizeof(struct wlan_802_11_security));
227 ret = libertas_set_mac_packet_filter(priv);
229 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
234 static int assoc_helper_wpa_keys(wlan_private *priv,
235 struct assoc_request * assoc_req)
239 lbs_deb_enter(LBS_DEB_ASSOC);
241 /* enable/Disable RSN */
242 ret = libertas_prepare_and_send_command(priv,
243 cmd_802_11_enable_rsn,
245 cmd_option_waitforrsp,
250 ret = libertas_prepare_and_send_command(priv,
251 cmd_802_11_key_material,
253 cmd_option_waitforrsp,
257 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
262 static int assoc_helper_wpa_ie(wlan_private *priv,
263 struct assoc_request * assoc_req)
265 wlan_adapter *adapter = priv->adapter;
268 lbs_deb_enter(LBS_DEB_ASSOC);
270 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
271 memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
272 adapter->wpa_ie_len = assoc_req->wpa_ie_len;
274 memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
275 adapter->wpa_ie_len = 0;
278 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
283 static int should_deauth_infrastructure(wlan_adapter *adapter,
284 struct assoc_request * assoc_req)
286 if (adapter->connect_status != libertas_connected)
289 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
290 lbs_deb_assoc("Deauthenticating due to new SSID in "
291 " configuration request.\n");
295 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
296 if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
297 lbs_deb_assoc("Deauthenticating due to updated security "
298 "info in configuration request.\n");
303 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
304 lbs_deb_assoc("Deauthenticating due to new BSSID in "
305 " configuration request.\n");
309 /* FIXME: deal with 'auto' mode somehow */
310 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
311 if (assoc_req->mode != IW_MODE_INFRA)
319 static int should_stop_adhoc(wlan_adapter *adapter,
320 struct assoc_request * assoc_req)
322 if (adapter->connect_status != libertas_connected)
325 if (adapter->curbssparams.ssid.ssidlength != assoc_req->ssid.ssidlength)
327 if (memcmp(adapter->curbssparams.ssid.ssid, assoc_req->ssid.ssid,
328 adapter->curbssparams.ssid.ssidlength))
331 /* FIXME: deal with 'auto' mode somehow */
332 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
333 if (assoc_req->mode != IW_MODE_ADHOC)
341 void libertas_association_worker(struct work_struct *work)
343 wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
344 wlan_adapter *adapter = priv->adapter;
345 struct assoc_request * assoc_req = NULL;
347 int find_any_ssid = 0;
349 lbs_deb_enter(LBS_DEB_ASSOC);
351 mutex_lock(&adapter->lock);
352 assoc_req = adapter->assoc_req;
353 adapter->assoc_req = NULL;
354 mutex_unlock(&adapter->lock);
359 lbs_deb_assoc("ASSOC: starting new association request: flags = 0x%lX\n",
362 /* If 'any' SSID was specified, find an SSID to associate with */
363 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
364 && !assoc_req->ssid.ssidlength)
367 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
368 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
369 if (memcmp(&assoc_req->bssid, bssid_any, ETH_ALEN)
370 && memcmp(&assoc_req->bssid, bssid_off, ETH_ALEN))
377 ret = libertas_find_best_network_SSID(priv, &assoc_req->ssid,
378 assoc_req->mode, &new_mode);
380 lbs_deb_assoc("Could not find best network\n");
385 /* Ensure we switch to the mode of the AP */
386 if (assoc_req->mode == IW_MODE_AUTO) {
387 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
388 assoc_req->mode = new_mode;
393 * Check if the attributes being changing require deauthentication
394 * from the currently associated infrastructure access point.
396 if (adapter->mode == IW_MODE_INFRA) {
397 if (should_deauth_infrastructure(adapter, assoc_req)) {
398 ret = libertas_send_deauthentication(priv);
400 lbs_deb_assoc("Deauthentication due to new "
401 "configuration request failed: %d\n",
405 } else if (adapter->mode == IW_MODE_ADHOC) {
406 if (should_stop_adhoc(adapter, assoc_req)) {
407 ret = libertas_stop_adhoc_network(priv);
409 lbs_deb_assoc("Teardown of AdHoc network due to "
410 "new configuration request failed: %d\n",
417 /* Send the various configuration bits to the firmware */
418 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
419 ret = assoc_helper_mode(priv, assoc_req);
421 lbs_deb_assoc("ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
426 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
427 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
428 ret = assoc_helper_wep_keys(priv, assoc_req);
430 lbs_deb_assoc("ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
435 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
436 ret = assoc_helper_secinfo(priv, assoc_req);
438 lbs_deb_assoc("ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
443 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
444 ret = assoc_helper_wpa_ie(priv, assoc_req);
446 lbs_deb_assoc("ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
451 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
452 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
453 ret = assoc_helper_wpa_keys(priv, assoc_req);
455 lbs_deb_assoc("ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
460 /* SSID/BSSID should be the _last_ config option set, because they
461 * trigger the association attempt.
463 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
464 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
467 ret = assoc_helper_associate(priv, assoc_req);
469 lbs_deb_assoc("ASSOC: association attempt unsuccessful: %d\n",
474 if (adapter->connect_status != libertas_connected) {
475 lbs_deb_assoc("ASSOC: assoication attempt unsuccessful, "
481 lbs_deb_assoc("ASSOC: association attempt successful. "
482 "Associated to '%s' (" MAC_FMT ")\n",
483 assoc_req->ssid.ssid, MAC_ARG(assoc_req->bssid));
484 libertas_prepare_and_send_command(priv,
486 0, cmd_option_waitforrsp, 0, NULL);
488 libertas_prepare_and_send_command(priv,
490 0, cmd_option_waitforrsp, 0, NULL);
499 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
505 lbs_deb_leave(LBS_DEB_ASSOC);
510 * Caller MUST hold any necessary locks
512 struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
514 struct assoc_request * assoc_req;
516 if (!adapter->assoc_req) {
517 adapter->assoc_req = kzalloc(sizeof(struct assoc_request), GFP_KERNEL);
518 if (!adapter->assoc_req) {
519 lbs_pr_info("Not enough memory to allocate association"
525 /* Copy current configuration attributes to the association request,
526 * but don't overwrite any that are already set.
528 assoc_req = adapter->assoc_req;
529 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
530 memcpy(&assoc_req->ssid, adapter->curbssparams.ssid.ssid,
531 adapter->curbssparams.ssid.ssidlength);
534 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
535 assoc_req->channel = adapter->curbssparams.channel;
537 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
538 assoc_req->mode = adapter->mode;
540 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
541 memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
545 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
547 for (i = 0; i < 4; i++) {
548 memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
549 sizeof(struct WLAN_802_11_KEY));
553 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
554 assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
556 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
557 memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
558 sizeof(struct WLAN_802_11_KEY));
561 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
562 memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
563 sizeof(struct WLAN_802_11_KEY));
566 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
567 memcpy(&assoc_req->secinfo, &adapter->secinfo,
568 sizeof(struct wlan_802_11_security));
571 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
572 memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
574 assoc_req->wpa_ie_len = adapter->wpa_ie_len;