blob: f67efa0815fe0f5b7fe5905f708eae0e8c2014da [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/* Copyright (C) 2006, Red Hat, Inc. */
2
3#include <linux/bitops.h>
4#include <net/ieee80211.h>
Dan Williams3cf20932007-05-25 17:28:30 -04005#include <linux/etherdevice.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02006
7#include "assoc.h"
8#include "join.h"
9#include "decl.h"
10#include "hostcmd.h"
11#include "host.h"
12
13
14static const u8 bssid_any[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
15static const u8 bssid_off[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
16
Dan Williamse76850d2007-05-25 17:09:41 -040017static void print_assoc_req(const char * extra, struct assoc_request * assoc_req)
18{
19 lbs_deb_assoc(
20 "#### Association Request: %s\n"
21 " flags: 0x%08lX\n"
22 " SSID: '%s'\n"
23 " channel: %d\n"
24 " band: %d\n"
25 " mode: %d\n"
26 " BSSID: " MAC_FMT "\n"
Dan Williams785e8f22007-05-25 23:51:12 -040027 " Encryption:%s%s%s\n"
28 " auth: %d\n",
Dan Williamse76850d2007-05-25 17:09:41 -040029 extra, assoc_req->flags,
Dan Williamsd8efea22007-05-28 23:54:55 -040030 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
Dan Williamse76850d2007-05-25 17:09:41 -040031 assoc_req->channel, assoc_req->band, assoc_req->mode,
Dan Williams785e8f22007-05-25 23:51:12 -040032 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);
Dan Williamse76850d2007-05-25 17:09:41 -040037}
38
39
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020040static int assoc_helper_essid(wlan_private *priv,
41 struct assoc_request * assoc_req)
42{
43 wlan_adapter *adapter = priv->adapter;
44 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -040045 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -040046 int channel = -1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020047
Holger Schurig9012b282007-05-25 11:27:16 -040048 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020049
Dan Williamsef9a2642007-05-25 16:46:33 -040050 /* FIXME: take channel into account when picking SSIDs if a channel
51 * is set.
52 */
53
Dan Williamsaeea0ab2007-05-25 22:30:48 -040054 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
55 channel = assoc_req->channel;
56
Dan Williamsd8efea22007-05-28 23:54:55 -040057 lbs_deb_assoc("New SSID requested: '%s'\n",
58 escape_essid(assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -040059 if (assoc_req->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020060 if (adapter->prescan) {
Dan Williams717c9332007-05-29 00:03:31 -040061 libertas_send_specific_ssid_scan(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -040062 assoc_req->ssid_len, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020063 }
64
Dan Williams717c9332007-05-29 00:03:31 -040065 bss = libertas_find_ssid_in_list(adapter, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -040066 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -040067 if (bss != NULL) {
68 lbs_deb_assoc("SSID found in scan list, associating\n");
Dan Williamse76850d2007-05-25 17:09:41 -040069 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
70 ret = wlan_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020071 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -040072 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020073 }
Dan Williams0dc5a292007-05-10 22:58:02 -040074 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020075 /* Scan for the network, do not save previous results. Stale
76 * scan data will cause us to join a non-existant adhoc network
77 */
Dan Williams717c9332007-05-29 00:03:31 -040078 libertas_send_specific_ssid_scan(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -040079 assoc_req->ssid_len, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020080
81 /* Search for the requested SSID in the scan table */
Dan Williams717c9332007-05-29 00:03:31 -040082 bss = libertas_find_ssid_in_list(adapter, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -040083 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -040084 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -040085 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -040086 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
87 libertas_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020088 } else {
89 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -040090 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -040091 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -040092 IW_ESSID_MAX_SIZE);
93 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamse76850d2007-05-25 17:09:41 -040094 libertas_start_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020095 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020096 }
97
Holger Schurig9012b282007-05-25 11:27:16 -040098 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020099 return ret;
100}
101
102
103static int assoc_helper_bssid(wlan_private *priv,
104 struct assoc_request * assoc_req)
105{
106 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400107 int ret = 0;
108 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200109
Dan Williams785e8f22007-05-25 23:51:12 -0400110 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID " MAC_FMT,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200111 MAC_ARG(assoc_req->bssid));
112
113 /* Search for index position in list for requested MAC */
Dan Williams717c9332007-05-29 00:03:31 -0400114 bss = libertas_find_bssid_in_list(adapter, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200115 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400116 if (bss == NULL) {
Holger Schurig9012b282007-05-25 11:27:16 -0400117 lbs_deb_assoc("ASSOC: WAP: BSSID " MAC_FMT " not found, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200118 "cannot associate.\n", MAC_ARG(assoc_req->bssid));
119 goto out;
120 }
121
Dan Williamse76850d2007-05-25 17:09:41 -0400122 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -0400123 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williamse76850d2007-05-25 17:09:41 -0400124 ret = wlan_associate(priv, assoc_req);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400125 lbs_deb_assoc("ASSOC: wlan_associate(bssid) returned %d\n", ret);
Dan Williams0dc5a292007-05-10 22:58:02 -0400126 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamse76850d2007-05-25 17:09:41 -0400127 libertas_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200128 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200129
130out:
Holger Schurig9012b282007-05-25 11:27:16 -0400131 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200132 return ret;
133}
134
135
136static int assoc_helper_associate(wlan_private *priv,
137 struct assoc_request * assoc_req)
138{
139 int ret = 0, done = 0;
140
141 /* If we're given and 'any' BSSID, try associating based on SSID */
142
143 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -0400144 if (compare_ether_addr(bssid_any, assoc_req->bssid)
145 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200146 ret = assoc_helper_bssid(priv, assoc_req);
147 done = 1;
148 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400149 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200150 }
151 }
152 }
153
154 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
155 ret = assoc_helper_essid(priv, assoc_req);
156 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400157 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200158 }
159 }
160
161 return ret;
162}
163
164
165static int assoc_helper_mode(wlan_private *priv,
166 struct assoc_request * assoc_req)
167{
168 wlan_adapter *adapter = priv->adapter;
169 int ret = 0;
170
Holger Schurig9012b282007-05-25 11:27:16 -0400171 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200172
Holger Schurig9012b282007-05-25 11:27:16 -0400173 if (assoc_req->mode == adapter->mode)
174 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200175
Dan Williams0dc5a292007-05-10 22:58:02 -0400176 if (assoc_req->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200177 if (adapter->psstate != PS_STATE_FULL_POWER)
178 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
179 adapter->psmode = wlan802_11powermodecam;
180 }
181
Dan Williams0dc5a292007-05-10 22:58:02 -0400182 adapter->mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200183 ret = libertas_prepare_and_send_command(priv,
184 cmd_802_11_snmp_mib,
185 0, cmd_option_waitforrsp,
186 OID_802_11_INFRASTRUCTURE_MODE,
David Woodhouse981f1872007-05-25 23:36:54 -0400187 /* Shoot me now */ (void *) (size_t) assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200188
Holger Schurig9012b282007-05-25 11:27:16 -0400189done:
190 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200191 return ret;
192}
193
194
Dan Williamsef9a2642007-05-25 16:46:33 -0400195static int update_channel(wlan_private * priv)
196{
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);
201}
202
Luis Carlos Cobo Rusb8bedef2007-05-30 12:14:34 -0400203void libertas_sync_channel(struct work_struct *work)
204{
205 wlan_private *priv = container_of(work, wlan_private, sync_channel);
206
207 if (update_channel(priv) != 0)
208 lbs_pr_info("Channel synchronization failed.");
209}
210
Dan Williamsef9a2642007-05-25 16:46:33 -0400211static int assoc_helper_channel(wlan_private *priv,
212 struct assoc_request * assoc_req)
213{
214 wlan_adapter *adapter = priv->adapter;
215 int ret = 0;
216
217 lbs_deb_enter(LBS_DEB_ASSOC);
218
219 ret = update_channel(priv);
220 if (ret < 0) {
221 lbs_deb_assoc("ASSOC: channel: error getting channel.");
222 }
223
224 if (assoc_req->channel == adapter->curbssparams.channel)
225 goto done;
226
227 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
228 adapter->curbssparams.channel, assoc_req->channel);
229
230 ret = libertas_prepare_and_send_command(priv, cmd_802_11_rf_channel,
231 cmd_opt_802_11_rf_channel_set,
232 cmd_option_waitforrsp, 0, &assoc_req->channel);
233 if (ret < 0) {
234 lbs_deb_assoc("ASSOC: channel: error setting channel.");
235 }
236
237 ret = update_channel(priv);
238 if (ret < 0) {
239 lbs_deb_assoc("ASSOC: channel: error getting channel.");
240 }
241
242 if (assoc_req->channel != adapter->curbssparams.channel) {
243 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
244 assoc_req->channel);
245 goto done;
246 }
247
248 if ( assoc_req->secinfo.wep_enabled
249 && (assoc_req->wep_keys[0].len
250 || assoc_req->wep_keys[1].len
251 || assoc_req->wep_keys[2].len
252 || assoc_req->wep_keys[3].len)) {
253 /* Make sure WEP keys are re-sent to firmware */
254 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
255 }
256
257 /* Must restart/rejoin adhoc networks after channel change */
258 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
259
260done:
261 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
262 return ret;
263}
264
265
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200266static int assoc_helper_wep_keys(wlan_private *priv,
267 struct assoc_request * assoc_req)
268{
269 wlan_adapter *adapter = priv->adapter;
270 int i;
271 int ret = 0;
272
Holger Schurig9012b282007-05-25 11:27:16 -0400273 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200274
275 /* Set or remove WEP keys */
276 if ( assoc_req->wep_keys[0].len
277 || assoc_req->wep_keys[1].len
278 || assoc_req->wep_keys[2].len
279 || assoc_req->wep_keys[3].len) {
280 ret = libertas_prepare_and_send_command(priv,
281 cmd_802_11_set_wep,
282 cmd_act_add,
283 cmd_option_waitforrsp,
284 0, assoc_req);
285 } else {
286 ret = libertas_prepare_and_send_command(priv,
287 cmd_802_11_set_wep,
288 cmd_act_remove,
289 cmd_option_waitforrsp,
290 0, NULL);
291 }
292
293 if (ret)
294 goto out;
295
296 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -0400297 if (assoc_req->secinfo.wep_enabled)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200298 adapter->currentpacketfilter |= cmd_act_mac_wep_enable;
299 else
300 adapter->currentpacketfilter &= ~cmd_act_mac_wep_enable;
301 ret = libertas_set_mac_packet_filter(priv);
302 if (ret)
303 goto out;
304
305 mutex_lock(&adapter->lock);
306
307 /* Copy WEP keys into adapter wep key fields */
308 for (i = 0; i < 4; i++) {
309 memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
310 sizeof(struct WLAN_802_11_KEY));
311 }
312 adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
313
314 mutex_unlock(&adapter->lock);
315
316out:
Holger Schurig9012b282007-05-25 11:27:16 -0400317 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200318 return ret;
319}
320
321static int assoc_helper_secinfo(wlan_private *priv,
322 struct assoc_request * assoc_req)
323{
324 wlan_adapter *adapter = priv->adapter;
325 int ret = 0;
326
Holger Schurig9012b282007-05-25 11:27:16 -0400327 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200328
329 memcpy(&adapter->secinfo, &assoc_req->secinfo,
330 sizeof(struct wlan_802_11_security));
331
332 ret = libertas_set_mac_packet_filter(priv);
Dan Williams90a42212007-05-25 23:01:24 -0400333 if (ret)
334 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200335
Dan Williams90a42212007-05-25 23:01:24 -0400336 /* enable/disable RSN */
337 ret = libertas_prepare_and_send_command(priv,
338 cmd_802_11_enable_rsn,
339 cmd_act_set,
340 cmd_option_waitforrsp,
341 0, assoc_req);
342
343out:
Holger Schurig9012b282007-05-25 11:27:16 -0400344 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200345 return ret;
346}
347
348
349static int assoc_helper_wpa_keys(wlan_private *priv,
350 struct assoc_request * assoc_req)
351{
352 int ret = 0;
353
Holger Schurig9012b282007-05-25 11:27:16 -0400354 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200355
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200356 ret = libertas_prepare_and_send_command(priv,
357 cmd_802_11_key_material,
358 cmd_act_set,
359 cmd_option_waitforrsp,
360 0, assoc_req);
361
Holger Schurig9012b282007-05-25 11:27:16 -0400362 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200363 return ret;
364}
365
366
367static int assoc_helper_wpa_ie(wlan_private *priv,
368 struct assoc_request * assoc_req)
369{
370 wlan_adapter *adapter = priv->adapter;
371 int ret = 0;
372
Holger Schurig9012b282007-05-25 11:27:16 -0400373 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200374
375 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
376 memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
377 adapter->wpa_ie_len = assoc_req->wpa_ie_len;
378 } else {
379 memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
380 adapter->wpa_ie_len = 0;
381 }
382
Holger Schurig9012b282007-05-25 11:27:16 -0400383 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200384 return ret;
385}
386
387
388static int should_deauth_infrastructure(wlan_adapter *adapter,
389 struct assoc_request * assoc_req)
390{
391 if (adapter->connect_status != libertas_connected)
392 return 0;
393
394 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400395 lbs_deb_assoc("Deauthenticating due to new SSID in "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200396 " configuration request.\n");
397 return 1;
398 }
399
400 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
Dan Williams6affe782007-05-10 22:56:42 -0400401 if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig9012b282007-05-25 11:27:16 -0400402 lbs_deb_assoc("Deauthenticating due to updated security "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200403 "info in configuration request.\n");
404 return 1;
405 }
406 }
407
408 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400409 lbs_deb_assoc("Deauthenticating due to new BSSID in "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200410 " configuration request.\n");
411 return 1;
412 }
413
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -0400414 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
415 lbs_deb_assoc("Deauthenticating due to channel switch.\n");
416 return 1;
417 }
418
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200419 /* FIXME: deal with 'auto' mode somehow */
420 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400421 if (assoc_req->mode != IW_MODE_INFRA)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200422 return 1;
423 }
424
425 return 0;
426}
427
428
429static int should_stop_adhoc(wlan_adapter *adapter,
430 struct assoc_request * assoc_req)
431{
432 if (adapter->connect_status != libertas_connected)
433 return 0;
434
Dan Williams717c9332007-05-29 00:03:31 -0400435 if (libertas_ssid_cmp(adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400436 adapter->curbssparams.ssid_len,
437 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438 return 1;
439
440 /* FIXME: deal with 'auto' mode somehow */
441 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400442 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200443 return 1;
444 }
445
Dan Williamsef9a2642007-05-25 16:46:33 -0400446 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
447 if (assoc_req->channel != adapter->curbssparams.channel)
448 return 1;
449 }
450
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451 return 0;
452}
453
454
Holger Schurigeb3ce632007-05-24 23:41:15 -0400455void libertas_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456{
457 wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
458 wlan_adapter *adapter = priv->adapter;
459 struct assoc_request * assoc_req = NULL;
460 int ret = 0;
461 int find_any_ssid = 0;
462
Holger Schurig9012b282007-05-25 11:27:16 -0400463 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200464
465 mutex_lock(&adapter->lock);
Dan Williamse76850d2007-05-25 17:09:41 -0400466 assoc_req = adapter->pending_assoc_req;
467 adapter->pending_assoc_req = NULL;
468 adapter->in_progress_assoc_req = assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469 mutex_unlock(&adapter->lock);
470
Holger Schurig9012b282007-05-25 11:27:16 -0400471 if (!assoc_req)
472 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200473
Dan Williamse76850d2007-05-25 17:09:41 -0400474 print_assoc_req(__func__, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200475
476 /* If 'any' SSID was specified, find an SSID to associate with */
477 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -0400478 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200479 find_any_ssid = 1;
480
481 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
482 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -0400483 if (compare_ether_addr(assoc_req->bssid, bssid_any)
484 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200485 find_any_ssid = 0;
486 }
487
488 if (find_any_ssid) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400489 u8 new_mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200490
Dan Williams717c9332007-05-29 00:03:31 -0400491 ret = libertas_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400492 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200493 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400494 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200495 ret = -ENETUNREACH;
496 goto out;
497 }
498
499 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -0400500 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200501 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
502 assoc_req->mode = new_mode;
503 }
504 }
505
506 /*
507 * Check if the attributes being changing require deauthentication
508 * from the currently associated infrastructure access point.
509 */
Dan Williams0dc5a292007-05-10 22:58:02 -0400510 if (adapter->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200511 if (should_deauth_infrastructure(adapter, assoc_req)) {
512 ret = libertas_send_deauthentication(priv);
513 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400514 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200515 "configuration request failed: %d\n",
516 ret);
517 }
518 }
Dan Williams0dc5a292007-05-10 22:58:02 -0400519 } else if (adapter->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200520 if (should_stop_adhoc(adapter, assoc_req)) {
521 ret = libertas_stop_adhoc_network(priv);
522 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400523 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200524 "new configuration request failed: %d\n",
525 ret);
526 }
527
528 }
529 }
530
531 /* Send the various configuration bits to the firmware */
532 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
533 ret = assoc_helper_mode(priv, assoc_req);
534 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400535lbs_deb_assoc("ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200536 goto out;
537 }
538 }
539
Dan Williamsef9a2642007-05-25 16:46:33 -0400540 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
541 ret = assoc_helper_channel(priv, assoc_req);
542 if (ret) {
543 lbs_deb_assoc("ASSOC(:%d) channel: ret = %d\n",
544 __LINE__, ret);
545 goto out;
546 }
547 }
548
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200549 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
550 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
551 ret = assoc_helper_wep_keys(priv, assoc_req);
552 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400553lbs_deb_assoc("ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200554 goto out;
555 }
556 }
557
558 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
559 ret = assoc_helper_secinfo(priv, assoc_req);
560 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400561lbs_deb_assoc("ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200562 goto out;
563 }
564 }
565
566 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
567 ret = assoc_helper_wpa_ie(priv, assoc_req);
568 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400569lbs_deb_assoc("ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200570 goto out;
571 }
572 }
573
574 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
575 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
576 ret = assoc_helper_wpa_keys(priv, assoc_req);
577 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400578lbs_deb_assoc("ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200579 goto out;
580 }
581 }
582
583 /* SSID/BSSID should be the _last_ config option set, because they
584 * trigger the association attempt.
585 */
586 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
587 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
588 int success = 1;
589
590 ret = assoc_helper_associate(priv, assoc_req);
591 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400592 lbs_deb_assoc("ASSOC: association attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200593 ret);
594 success = 0;
595 }
596
597 if (adapter->connect_status != libertas_connected) {
Holger Schurig9012b282007-05-25 11:27:16 -0400598 lbs_deb_assoc("ASSOC: assoication attempt unsuccessful, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200599 "not connected.\n");
600 success = 0;
601 }
602
603 if (success) {
Holger Schurig9012b282007-05-25 11:27:16 -0400604 lbs_deb_assoc("ASSOC: association attempt successful. "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200605 "Associated to '%s' (" MAC_FMT ")\n",
Dan Williamsd8efea22007-05-28 23:54:55 -0400606 escape_essid(adapter->curbssparams.ssid,
607 adapter->curbssparams.ssid_len),
Dan Williamse76850d2007-05-25 17:09:41 -0400608 MAC_ARG(adapter->curbssparams.bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609 libertas_prepare_and_send_command(priv,
610 cmd_802_11_rssi,
611 0, cmd_option_waitforrsp, 0, NULL);
612
613 libertas_prepare_and_send_command(priv,
614 cmd_802_11_get_log,
615 0, cmd_option_waitforrsp, 0, NULL);
616 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200617 ret = -1;
618 }
619 }
620
621out:
622 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400623 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200624 ret);
625 }
Dan Williamse76850d2007-05-25 17:09:41 -0400626
627 mutex_lock(&adapter->lock);
628 adapter->in_progress_assoc_req = NULL;
629 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200630 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -0400631
632done:
633 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634}
635
636
637/*
638 * Caller MUST hold any necessary locks
639 */
640struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
641{
642 struct assoc_request * assoc_req;
643
Dan Williamse76850d2007-05-25 17:09:41 -0400644 if (!adapter->pending_assoc_req) {
645 adapter->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
646 GFP_KERNEL);
647 if (!adapter->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200648 lbs_pr_info("Not enough memory to allocate association"
649 " request!\n");
650 return NULL;
651 }
652 }
653
654 /* Copy current configuration attributes to the association request,
655 * but don't overwrite any that are already set.
656 */
Dan Williamse76850d2007-05-25 17:09:41 -0400657 assoc_req = adapter->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200658 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Dan Williamse76850d2007-05-25 17:09:41 -0400659 memcpy(&assoc_req->ssid, &adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400660 IW_ESSID_MAX_SIZE);
661 assoc_req->ssid_len = adapter->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200662 }
663
664 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
665 assoc_req->channel = adapter->curbssparams.channel;
666
Dan Williamse76850d2007-05-25 17:09:41 -0400667 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
668 assoc_req->band = adapter->curbssparams.band;
669
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200670 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
Dan Williams0dc5a292007-05-10 22:58:02 -0400671 assoc_req->mode = adapter->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200672
673 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
674 memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
675 ETH_ALEN);
676 }
677
678 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
679 int i;
680 for (i = 0; i < 4; i++) {
681 memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
682 sizeof(struct WLAN_802_11_KEY));
683 }
684 }
685
686 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
687 assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
688
689 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
690 memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
691 sizeof(struct WLAN_802_11_KEY));
692 }
693
694 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
695 memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
696 sizeof(struct WLAN_802_11_KEY));
697 }
698
699 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
700 memcpy(&assoc_req->secinfo, &adapter->secinfo,
701 sizeof(struct wlan_802_11_security));
702 }
703
704 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
705 memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
706 MAX_WPA_IE_LEN);
707 assoc_req->wpa_ie_len = adapter->wpa_ie_len;
708 }
709
Dan Williamse76850d2007-05-25 17:09:41 -0400710 print_assoc_req(__func__, assoc_req);
711
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200712 return assoc_req;
713}