blob: ee82413b426d2ee2a64d034a675b65a4911e25c7 [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 Williams3cf209312007-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 Williams3cf209312007-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
203static int assoc_helper_channel(wlan_private *priv,
204 struct assoc_request * assoc_req)
205{
206 wlan_adapter *adapter = priv->adapter;
207 int ret = 0;
208
209 lbs_deb_enter(LBS_DEB_ASSOC);
210
211 ret = update_channel(priv);
212 if (ret < 0) {
213 lbs_deb_assoc("ASSOC: channel: error getting channel.");
214 }
215
216 if (assoc_req->channel == adapter->curbssparams.channel)
217 goto done;
218
219 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
220 adapter->curbssparams.channel, assoc_req->channel);
221
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);
225 if (ret < 0) {
226 lbs_deb_assoc("ASSOC: channel: error setting channel.");
227 }
228
229 ret = update_channel(priv);
230 if (ret < 0) {
231 lbs_deb_assoc("ASSOC: channel: error getting channel.");
232 }
233
234 if (assoc_req->channel != adapter->curbssparams.channel) {
235 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
236 assoc_req->channel);
237 goto done;
238 }
239
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);
247 }
248
249 /* Must restart/rejoin adhoc networks after channel change */
250 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
251
252done:
253 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
254 return ret;
255}
256
257
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200258static int assoc_helper_wep_keys(wlan_private *priv,
259 struct assoc_request * assoc_req)
260{
261 wlan_adapter *adapter = priv->adapter;
262 int i;
263 int ret = 0;
264
Holger Schurig9012b282007-05-25 11:27:16 -0400265 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200266
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,
273 cmd_802_11_set_wep,
274 cmd_act_add,
275 cmd_option_waitforrsp,
276 0, assoc_req);
277 } else {
278 ret = libertas_prepare_and_send_command(priv,
279 cmd_802_11_set_wep,
280 cmd_act_remove,
281 cmd_option_waitforrsp,
282 0, NULL);
283 }
284
285 if (ret)
286 goto out;
287
288 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -0400289 if (assoc_req->secinfo.wep_enabled)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200290 adapter->currentpacketfilter |= cmd_act_mac_wep_enable;
291 else
292 adapter->currentpacketfilter &= ~cmd_act_mac_wep_enable;
293 ret = libertas_set_mac_packet_filter(priv);
294 if (ret)
295 goto out;
296
297 mutex_lock(&adapter->lock);
298
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));
303 }
304 adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
305
306 mutex_unlock(&adapter->lock);
307
308out:
Holger Schurig9012b282007-05-25 11:27:16 -0400309 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200310 return ret;
311}
312
313static int assoc_helper_secinfo(wlan_private *priv,
314 struct assoc_request * assoc_req)
315{
316 wlan_adapter *adapter = priv->adapter;
317 int ret = 0;
318
Holger Schurig9012b282007-05-25 11:27:16 -0400319 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200320
321 memcpy(&adapter->secinfo, &assoc_req->secinfo,
322 sizeof(struct wlan_802_11_security));
323
324 ret = libertas_set_mac_packet_filter(priv);
Dan Williams90a42212007-05-25 23:01:24 -0400325 if (ret)
326 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200327
Dan Williams90a42212007-05-25 23:01:24 -0400328 /* enable/disable RSN */
329 ret = libertas_prepare_and_send_command(priv,
330 cmd_802_11_enable_rsn,
331 cmd_act_set,
332 cmd_option_waitforrsp,
333 0, assoc_req);
334
335out:
Holger Schurig9012b282007-05-25 11:27:16 -0400336 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200337 return ret;
338}
339
340
341static int assoc_helper_wpa_keys(wlan_private *priv,
342 struct assoc_request * assoc_req)
343{
344 int ret = 0;
345
Holger Schurig9012b282007-05-25 11:27:16 -0400346 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200347
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200348 ret = libertas_prepare_and_send_command(priv,
349 cmd_802_11_key_material,
350 cmd_act_set,
351 cmd_option_waitforrsp,
352 0, assoc_req);
353
Holger Schurig9012b282007-05-25 11:27:16 -0400354 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200355 return ret;
356}
357
358
359static int assoc_helper_wpa_ie(wlan_private *priv,
360 struct assoc_request * assoc_req)
361{
362 wlan_adapter *adapter = priv->adapter;
363 int ret = 0;
364
Holger Schurig9012b282007-05-25 11:27:16 -0400365 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200366
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;
370 } else {
371 memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
372 adapter->wpa_ie_len = 0;
373 }
374
Holger Schurig9012b282007-05-25 11:27:16 -0400375 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200376 return ret;
377}
378
379
380static int should_deauth_infrastructure(wlan_adapter *adapter,
381 struct assoc_request * assoc_req)
382{
383 if (adapter->connect_status != libertas_connected)
384 return 0;
385
386 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400387 lbs_deb_assoc("Deauthenticating due to new SSID in "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200388 " configuration request.\n");
389 return 1;
390 }
391
392 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
Dan Williams6affe782007-05-10 22:56:42 -0400393 if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig9012b282007-05-25 11:27:16 -0400394 lbs_deb_assoc("Deauthenticating due to updated security "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200395 "info in configuration request.\n");
396 return 1;
397 }
398 }
399
400 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400401 lbs_deb_assoc("Deauthenticating due to new BSSID in "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200402 " configuration request.\n");
403 return 1;
404 }
405
406 /* FIXME: deal with 'auto' mode somehow */
407 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400408 if (assoc_req->mode != IW_MODE_INFRA)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200409 return 1;
410 }
411
412 return 0;
413}
414
415
416static int should_stop_adhoc(wlan_adapter *adapter,
417 struct assoc_request * assoc_req)
418{
419 if (adapter->connect_status != libertas_connected)
420 return 0;
421
Dan Williams717c9332007-05-29 00:03:31 -0400422 if (libertas_ssid_cmp(adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400423 adapter->curbssparams.ssid_len,
424 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200425 return 1;
426
427 /* FIXME: deal with 'auto' mode somehow */
428 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400429 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200430 return 1;
431 }
432
Dan Williamsef9a2642007-05-25 16:46:33 -0400433 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
434 if (assoc_req->channel != adapter->curbssparams.channel)
435 return 1;
436 }
437
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438 return 0;
439}
440
441
Holger Schurigeb3ce632007-05-24 23:41:15 -0400442void libertas_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200443{
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;
447 int ret = 0;
448 int find_any_ssid = 0;
449
Holger Schurig9012b282007-05-25 11:27:16 -0400450 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451
452 mutex_lock(&adapter->lock);
Dan Williamse76850d2007-05-25 17:09:41 -0400453 assoc_req = adapter->pending_assoc_req;
454 adapter->pending_assoc_req = NULL;
455 adapter->in_progress_assoc_req = assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456 mutex_unlock(&adapter->lock);
457
Holger Schurig9012b282007-05-25 11:27:16 -0400458 if (!assoc_req)
459 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200460
Dan Williamse76850d2007-05-25 17:09:41 -0400461 print_assoc_req(__func__, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200462
463 /* If 'any' SSID was specified, find an SSID to associate with */
464 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -0400465 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200466 find_any_ssid = 1;
467
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)) {
Dan Williams3cf209312007-05-25 17:28:30 -0400470 if (compare_ether_addr(assoc_req->bssid, bssid_any)
471 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200472 find_any_ssid = 0;
473 }
474
475 if (find_any_ssid) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400476 u8 new_mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200477
Dan Williams717c9332007-05-29 00:03:31 -0400478 ret = libertas_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400479 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200480 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400481 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200482 ret = -ENETUNREACH;
483 goto out;
484 }
485
486 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -0400487 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200488 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
489 assoc_req->mode = new_mode;
490 }
491 }
492
493 /*
494 * Check if the attributes being changing require deauthentication
495 * from the currently associated infrastructure access point.
496 */
Dan Williams0dc5a292007-05-10 22:58:02 -0400497 if (adapter->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200498 if (should_deauth_infrastructure(adapter, assoc_req)) {
499 ret = libertas_send_deauthentication(priv);
500 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400501 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200502 "configuration request failed: %d\n",
503 ret);
504 }
505 }
Dan Williams0dc5a292007-05-10 22:58:02 -0400506 } else if (adapter->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200507 if (should_stop_adhoc(adapter, assoc_req)) {
508 ret = libertas_stop_adhoc_network(priv);
509 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400510 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200511 "new configuration request failed: %d\n",
512 ret);
513 }
514
515 }
516 }
517
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);
521 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400522lbs_deb_assoc("ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200523 goto out;
524 }
525 }
526
Dan Williamsef9a2642007-05-25 16:46:33 -0400527 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
528 ret = assoc_helper_channel(priv, assoc_req);
529 if (ret) {
530 lbs_deb_assoc("ASSOC(:%d) channel: ret = %d\n",
531 __LINE__, ret);
532 goto out;
533 }
534 }
535
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200536 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);
539 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400540lbs_deb_assoc("ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200541 goto out;
542 }
543 }
544
545 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
546 ret = assoc_helper_secinfo(priv, assoc_req);
547 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400548lbs_deb_assoc("ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200549 goto out;
550 }
551 }
552
553 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
554 ret = assoc_helper_wpa_ie(priv, assoc_req);
555 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400556lbs_deb_assoc("ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200557 goto out;
558 }
559 }
560
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);
564 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400565lbs_deb_assoc("ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200566 goto out;
567 }
568 }
569
570 /* SSID/BSSID should be the _last_ config option set, because they
571 * trigger the association attempt.
572 */
573 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
574 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
575 int success = 1;
576
577 ret = assoc_helper_associate(priv, assoc_req);
578 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400579 lbs_deb_assoc("ASSOC: association attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200580 ret);
581 success = 0;
582 }
583
584 if (adapter->connect_status != libertas_connected) {
Holger Schurig9012b282007-05-25 11:27:16 -0400585 lbs_deb_assoc("ASSOC: assoication attempt unsuccessful, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200586 "not connected.\n");
587 success = 0;
588 }
589
590 if (success) {
Holger Schurig9012b282007-05-25 11:27:16 -0400591 lbs_deb_assoc("ASSOC: association attempt successful. "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200592 "Associated to '%s' (" MAC_FMT ")\n",
Dan Williamsd8efea22007-05-28 23:54:55 -0400593 escape_essid(adapter->curbssparams.ssid,
594 adapter->curbssparams.ssid_len),
Dan Williamse76850d2007-05-25 17:09:41 -0400595 MAC_ARG(adapter->curbssparams.bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200596 libertas_prepare_and_send_command(priv,
597 cmd_802_11_rssi,
598 0, cmd_option_waitforrsp, 0, NULL);
599
600 libertas_prepare_and_send_command(priv,
601 cmd_802_11_get_log,
602 0, cmd_option_waitforrsp, 0, NULL);
603 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200604 ret = -1;
605 }
606 }
607
608out:
609 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400610 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200611 ret);
612 }
Dan Williamse76850d2007-05-25 17:09:41 -0400613
614 mutex_lock(&adapter->lock);
615 adapter->in_progress_assoc_req = NULL;
616 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200617 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -0400618
619done:
620 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200621}
622
623
624/*
625 * Caller MUST hold any necessary locks
626 */
627struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
628{
629 struct assoc_request * assoc_req;
630
Dan Williamse76850d2007-05-25 17:09:41 -0400631 if (!adapter->pending_assoc_req) {
632 adapter->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
633 GFP_KERNEL);
634 if (!adapter->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200635 lbs_pr_info("Not enough memory to allocate association"
636 " request!\n");
637 return NULL;
638 }
639 }
640
641 /* Copy current configuration attributes to the association request,
642 * but don't overwrite any that are already set.
643 */
Dan Williamse76850d2007-05-25 17:09:41 -0400644 assoc_req = adapter->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200645 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Dan Williamse76850d2007-05-25 17:09:41 -0400646 memcpy(&assoc_req->ssid, &adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400647 IW_ESSID_MAX_SIZE);
648 assoc_req->ssid_len = adapter->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200649 }
650
651 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
652 assoc_req->channel = adapter->curbssparams.channel;
653
Dan Williamse76850d2007-05-25 17:09:41 -0400654 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
655 assoc_req->band = adapter->curbssparams.band;
656
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200657 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
Dan Williams0dc5a292007-05-10 22:58:02 -0400658 assoc_req->mode = adapter->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200659
660 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
661 memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
662 ETH_ALEN);
663 }
664
665 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
666 int i;
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));
670 }
671 }
672
673 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
674 assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
675
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));
679 }
680
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));
684 }
685
686 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
687 memcpy(&assoc_req->secinfo, &adapter->secinfo,
688 sizeof(struct wlan_802_11_security));
689 }
690
691 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
692 memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
693 MAX_WPA_IE_LEN);
694 assoc_req->wpa_ie_len = adapter->wpa_ie_len;
695 }
696
Dan Williamse76850d2007-05-25 17:09:41 -0400697 print_assoc_req(__func__, assoc_req);
698
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699 return assoc_req;
700}