blob: afce32761bfd1b0106ca4118eff48a5ef5442160 [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 -040017/* From ieee80211_module.c */
18static const char *libertas_escape_essid(const char *essid, u8 essid_len)
19{
20 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
21 const char *s = essid;
22 char *d = escaped;
23
24 if (ieee80211_is_empty_essid(essid, essid_len))
25 return "";
26
27 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
28 while (essid_len--) {
29 if (*s == '\0') {
30 *d++ = '\\';
31 *d++ = '0';
32 s++;
33 } else {
34 *d++ = *s++;
35 }
36 }
37 *d = '\0';
38 return escaped;
39}
40
41static void print_assoc_req(const char * extra, struct assoc_request * assoc_req)
42{
43 lbs_deb_assoc(
44 "#### Association Request: %s\n"
45 " flags: 0x%08lX\n"
46 " SSID: '%s'\n"
47 " channel: %d\n"
48 " band: %d\n"
49 " mode: %d\n"
50 " BSSID: " MAC_FMT "\n"
51 " WPA: %d\n"
52 " WPA2: %d\n"
53 " WEP status: %d\n"
54 " auth: %d\n"
55 " auth_alg: %d\n"
56 " encmode: %d\n",
57 extra, assoc_req->flags,
58 libertas_escape_essid(assoc_req->ssid.ssid, assoc_req->ssid.ssidlength),
59 assoc_req->channel, assoc_req->band, assoc_req->mode,
60 MAC_ARG(assoc_req->bssid), assoc_req->secinfo.WPAenabled,
61 assoc_req->secinfo.WPA2enabled, assoc_req->secinfo.WEPstatus,
62 assoc_req->secinfo.authmode, assoc_req->secinfo.auth1xalg,
63 assoc_req->secinfo.Encryptionmode);
64}
65
66
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020067static int assoc_helper_essid(wlan_private *priv,
68 struct assoc_request * assoc_req)
69{
70 wlan_adapter *adapter = priv->adapter;
71 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -040072 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020073
Holger Schurig9012b282007-05-25 11:27:16 -040074 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020075
Dan Williamsef9a2642007-05-25 16:46:33 -040076 /* FIXME: take channel into account when picking SSIDs if a channel
77 * is set.
78 */
79
Holger Schurig9012b282007-05-25 11:27:16 -040080 lbs_deb_assoc("New SSID requested: %s\n", assoc_req->ssid.ssid);
Dan Williams0dc5a292007-05-10 22:58:02 -040081 if (assoc_req->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020082 if (adapter->prescan) {
Dan Williamseb8f7332007-05-25 16:25:21 -040083 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020084 }
85
Dan Williamsfcdb53d2007-05-25 16:15:56 -040086 bss = libertas_find_SSID_in_list(adapter, &assoc_req->ssid,
Dan Williams0dc5a292007-05-10 22:58:02 -040087 NULL, IW_MODE_INFRA);
Dan Williamsfcdb53d2007-05-25 16:15:56 -040088 if (bss != NULL) {
89 lbs_deb_assoc("SSID found in scan list, associating\n");
Dan Williamse76850d2007-05-25 17:09:41 -040090 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
91 ret = wlan_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020092 } else {
Holger Schurig9012b282007-05-25 11:27:16 -040093 lbs_deb_assoc("SSID '%s' not found; cannot associate\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020094 assoc_req->ssid.ssid);
95 }
Dan Williams0dc5a292007-05-10 22:58:02 -040096 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020097 /* Scan for the network, do not save previous results. Stale
98 * scan data will cause us to join a non-existant adhoc network
99 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400100 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200101
102 /* Search for the requested SSID in the scan table */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400103 bss = libertas_find_SSID_in_list(adapter, &assoc_req->ssid, NULL,
Dan Williams0dc5a292007-05-10 22:58:02 -0400104 IW_MODE_ADHOC);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400105 if (bss != NULL) {
106 lbs_deb_assoc("SSID found joining\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400107 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
108 libertas_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200109 } else {
110 /* else send START command */
Holger Schurig9012b282007-05-25 11:27:16 -0400111 lbs_deb_assoc("SSID not found in list, so creating adhoc"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200112 " with SSID '%s'\n", assoc_req->ssid.ssid);
Dan Williamse76850d2007-05-25 17:09:41 -0400113 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
114 sizeof(struct WLAN_802_11_SSID));
115 libertas_start_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200116 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200117 }
118
Holger Schurig9012b282007-05-25 11:27:16 -0400119 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200120 return ret;
121}
122
123
124static int assoc_helper_bssid(wlan_private *priv,
125 struct assoc_request * assoc_req)
126{
127 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400128 int ret = 0;
129 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200130
Holger Schurig9012b282007-05-25 11:27:16 -0400131 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID" MAC_FMT "\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200132 MAC_ARG(assoc_req->bssid));
133
134 /* Search for index position in list for requested MAC */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400135 bss = libertas_find_BSSID_in_list(adapter, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200136 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400137 if (bss == NULL) {
Holger Schurig9012b282007-05-25 11:27:16 -0400138 lbs_deb_assoc("ASSOC: WAP: BSSID " MAC_FMT " not found, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200139 "cannot associate.\n", MAC_ARG(assoc_req->bssid));
140 goto out;
141 }
142
Dan Williamse76850d2007-05-25 17:09:41 -0400143 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -0400144 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williamse76850d2007-05-25 17:09:41 -0400145 ret = wlan_associate(priv, assoc_req);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400146 lbs_deb_assoc("ASSOC: wlan_associate(bssid) returned %d\n", ret);
Dan Williams0dc5a292007-05-10 22:58:02 -0400147 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamse76850d2007-05-25 17:09:41 -0400148 libertas_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200149 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200150
151out:
Holger Schurig9012b282007-05-25 11:27:16 -0400152 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200153 return ret;
154}
155
156
157static int assoc_helper_associate(wlan_private *priv,
158 struct assoc_request * assoc_req)
159{
160 int ret = 0, done = 0;
161
162 /* If we're given and 'any' BSSID, try associating based on SSID */
163
164 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -0400165 if (compare_ether_addr(bssid_any, assoc_req->bssid)
166 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167 ret = assoc_helper_bssid(priv, assoc_req);
168 done = 1;
169 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400170 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200171 }
172 }
173 }
174
175 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
176 ret = assoc_helper_essid(priv, assoc_req);
177 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400178 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200179 }
180 }
181
182 return ret;
183}
184
185
186static int assoc_helper_mode(wlan_private *priv,
187 struct assoc_request * assoc_req)
188{
189 wlan_adapter *adapter = priv->adapter;
190 int ret = 0;
191
Holger Schurig9012b282007-05-25 11:27:16 -0400192 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200193
Holger Schurig9012b282007-05-25 11:27:16 -0400194 if (assoc_req->mode == adapter->mode)
195 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200196
Dan Williams0dc5a292007-05-10 22:58:02 -0400197 if (assoc_req->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200198 if (adapter->psstate != PS_STATE_FULL_POWER)
199 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
200 adapter->psmode = wlan802_11powermodecam;
201 }
202
Dan Williams0dc5a292007-05-10 22:58:02 -0400203 adapter->mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200204 ret = libertas_prepare_and_send_command(priv,
205 cmd_802_11_snmp_mib,
206 0, cmd_option_waitforrsp,
207 OID_802_11_INFRASTRUCTURE_MODE,
Dan Williams0dc5a292007-05-10 22:58:02 -0400208 (void *) (size_t) assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200209
Holger Schurig9012b282007-05-25 11:27:16 -0400210done:
211 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200212 return ret;
213}
214
215
Dan Williamsef9a2642007-05-25 16:46:33 -0400216static int update_channel(wlan_private * priv)
217{
218 /* the channel in f/w could be out of sync, get the current channel */
219 return libertas_prepare_and_send_command(priv, cmd_802_11_rf_channel,
220 cmd_opt_802_11_rf_channel_get,
221 cmd_option_waitforrsp, 0, NULL);
222}
223
224static int assoc_helper_channel(wlan_private *priv,
225 struct assoc_request * assoc_req)
226{
227 wlan_adapter *adapter = priv->adapter;
228 int ret = 0;
229
230 lbs_deb_enter(LBS_DEB_ASSOC);
231
232 ret = update_channel(priv);
233 if (ret < 0) {
234 lbs_deb_assoc("ASSOC: channel: error getting channel.");
235 }
236
237 if (assoc_req->channel == adapter->curbssparams.channel)
238 goto done;
239
240 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
241 adapter->curbssparams.channel, assoc_req->channel);
242
243 ret = libertas_prepare_and_send_command(priv, cmd_802_11_rf_channel,
244 cmd_opt_802_11_rf_channel_set,
245 cmd_option_waitforrsp, 0, &assoc_req->channel);
246 if (ret < 0) {
247 lbs_deb_assoc("ASSOC: channel: error setting channel.");
248 }
249
250 ret = update_channel(priv);
251 if (ret < 0) {
252 lbs_deb_assoc("ASSOC: channel: error getting channel.");
253 }
254
255 if (assoc_req->channel != adapter->curbssparams.channel) {
256 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
257 assoc_req->channel);
258 goto done;
259 }
260
261 if ( assoc_req->secinfo.wep_enabled
262 && (assoc_req->wep_keys[0].len
263 || assoc_req->wep_keys[1].len
264 || assoc_req->wep_keys[2].len
265 || assoc_req->wep_keys[3].len)) {
266 /* Make sure WEP keys are re-sent to firmware */
267 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
268 }
269
270 /* Must restart/rejoin adhoc networks after channel change */
271 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
272
273done:
274 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
275 return ret;
276}
277
278
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200279static int assoc_helper_wep_keys(wlan_private *priv,
280 struct assoc_request * assoc_req)
281{
282 wlan_adapter *adapter = priv->adapter;
283 int i;
284 int ret = 0;
285
Holger Schurig9012b282007-05-25 11:27:16 -0400286 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200287
288 /* Set or remove WEP keys */
289 if ( assoc_req->wep_keys[0].len
290 || assoc_req->wep_keys[1].len
291 || assoc_req->wep_keys[2].len
292 || assoc_req->wep_keys[3].len) {
293 ret = libertas_prepare_and_send_command(priv,
294 cmd_802_11_set_wep,
295 cmd_act_add,
296 cmd_option_waitforrsp,
297 0, assoc_req);
298 } else {
299 ret = libertas_prepare_and_send_command(priv,
300 cmd_802_11_set_wep,
301 cmd_act_remove,
302 cmd_option_waitforrsp,
303 0, NULL);
304 }
305
306 if (ret)
307 goto out;
308
309 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -0400310 if (assoc_req->secinfo.wep_enabled)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200311 adapter->currentpacketfilter |= cmd_act_mac_wep_enable;
312 else
313 adapter->currentpacketfilter &= ~cmd_act_mac_wep_enable;
314 ret = libertas_set_mac_packet_filter(priv);
315 if (ret)
316 goto out;
317
318 mutex_lock(&adapter->lock);
319
320 /* Copy WEP keys into adapter wep key fields */
321 for (i = 0; i < 4; i++) {
322 memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
323 sizeof(struct WLAN_802_11_KEY));
324 }
325 adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
326
327 mutex_unlock(&adapter->lock);
328
329out:
Holger Schurig9012b282007-05-25 11:27:16 -0400330 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200331 return ret;
332}
333
334static int assoc_helper_secinfo(wlan_private *priv,
335 struct assoc_request * assoc_req)
336{
337 wlan_adapter *adapter = priv->adapter;
338 int ret = 0;
339
Holger Schurig9012b282007-05-25 11:27:16 -0400340 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200341
342 memcpy(&adapter->secinfo, &assoc_req->secinfo,
343 sizeof(struct wlan_802_11_security));
344
345 ret = libertas_set_mac_packet_filter(priv);
346
Holger Schurig9012b282007-05-25 11:27:16 -0400347 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200348 return ret;
349}
350
351
352static int assoc_helper_wpa_keys(wlan_private *priv,
353 struct assoc_request * assoc_req)
354{
355 int ret = 0;
356
Holger Schurig9012b282007-05-25 11:27:16 -0400357 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200358
359 /* enable/Disable RSN */
360 ret = libertas_prepare_and_send_command(priv,
361 cmd_802_11_enable_rsn,
362 cmd_act_set,
363 cmd_option_waitforrsp,
364 0, assoc_req);
365 if (ret)
366 goto out;
367
368 ret = libertas_prepare_and_send_command(priv,
369 cmd_802_11_key_material,
370 cmd_act_set,
371 cmd_option_waitforrsp,
372 0, assoc_req);
373
374out:
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 assoc_helper_wpa_ie(wlan_private *priv,
381 struct assoc_request * assoc_req)
382{
383 wlan_adapter *adapter = priv->adapter;
384 int ret = 0;
385
Holger Schurig9012b282007-05-25 11:27:16 -0400386 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200387
388 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
389 memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
390 adapter->wpa_ie_len = assoc_req->wpa_ie_len;
391 } else {
392 memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
393 adapter->wpa_ie_len = 0;
394 }
395
Holger Schurig9012b282007-05-25 11:27:16 -0400396 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200397 return ret;
398}
399
400
401static int should_deauth_infrastructure(wlan_adapter *adapter,
402 struct assoc_request * assoc_req)
403{
404 if (adapter->connect_status != libertas_connected)
405 return 0;
406
407 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400408 lbs_deb_assoc("Deauthenticating due to new SSID in "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200409 " configuration request.\n");
410 return 1;
411 }
412
413 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
Dan Williams6affe782007-05-10 22:56:42 -0400414 if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig9012b282007-05-25 11:27:16 -0400415 lbs_deb_assoc("Deauthenticating due to updated security "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200416 "info in configuration request.\n");
417 return 1;
418 }
419 }
420
421 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400422 lbs_deb_assoc("Deauthenticating due to new BSSID in "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200423 " configuration request.\n");
424 return 1;
425 }
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_INFRA)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200430 return 1;
431 }
432
433 return 0;
434}
435
436
437static int should_stop_adhoc(wlan_adapter *adapter,
438 struct assoc_request * assoc_req)
439{
440 if (adapter->connect_status != libertas_connected)
441 return 0;
442
443 if (adapter->curbssparams.ssid.ssidlength != assoc_req->ssid.ssidlength)
444 return 1;
445 if (memcmp(adapter->curbssparams.ssid.ssid, assoc_req->ssid.ssid,
Dan Williamsad1f3292007-05-10 22:52:37 -0400446 adapter->curbssparams.ssid.ssidlength))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200447 return 1;
448
449 /* FIXME: deal with 'auto' mode somehow */
450 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400451 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200452 return 1;
453 }
454
Dan Williamsef9a2642007-05-25 16:46:33 -0400455 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
456 if (assoc_req->channel != adapter->curbssparams.channel)
457 return 1;
458 }
459
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200460 return 0;
461}
462
463
Holger Schurigeb3ce632007-05-24 23:41:15 -0400464void libertas_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200465{
466 wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
467 wlan_adapter *adapter = priv->adapter;
468 struct assoc_request * assoc_req = NULL;
469 int ret = 0;
470 int find_any_ssid = 0;
471
Holger Schurig9012b282007-05-25 11:27:16 -0400472 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200473
474 mutex_lock(&adapter->lock);
Dan Williamse76850d2007-05-25 17:09:41 -0400475 assoc_req = adapter->pending_assoc_req;
476 adapter->pending_assoc_req = NULL;
477 adapter->in_progress_assoc_req = assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200478 mutex_unlock(&adapter->lock);
479
Holger Schurig9012b282007-05-25 11:27:16 -0400480 if (!assoc_req)
481 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200482
Dan Williamse76850d2007-05-25 17:09:41 -0400483 print_assoc_req(__func__, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200484
485 /* If 'any' SSID was specified, find an SSID to associate with */
486 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
487 && !assoc_req->ssid.ssidlength)
488 find_any_ssid = 1;
489
490 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
491 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -0400492 if (compare_ether_addr(assoc_req->bssid, bssid_any)
493 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200494 find_any_ssid = 0;
495 }
496
497 if (find_any_ssid) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400498 u8 new_mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200499
500 ret = libertas_find_best_network_SSID(priv, &assoc_req->ssid,
501 assoc_req->mode, &new_mode);
502 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400503 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200504 ret = -ENETUNREACH;
505 goto out;
506 }
507
508 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -0400509 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
511 assoc_req->mode = new_mode;
512 }
513 }
514
515 /*
516 * Check if the attributes being changing require deauthentication
517 * from the currently associated infrastructure access point.
518 */
Dan Williams0dc5a292007-05-10 22:58:02 -0400519 if (adapter->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200520 if (should_deauth_infrastructure(adapter, assoc_req)) {
521 ret = libertas_send_deauthentication(priv);
522 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400523 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200524 "configuration request failed: %d\n",
525 ret);
526 }
527 }
Dan Williams0dc5a292007-05-10 22:58:02 -0400528 } else if (adapter->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200529 if (should_stop_adhoc(adapter, assoc_req)) {
530 ret = libertas_stop_adhoc_network(priv);
531 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400532 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533 "new configuration request failed: %d\n",
534 ret);
535 }
536
537 }
538 }
539
540 /* Send the various configuration bits to the firmware */
541 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
542 ret = assoc_helper_mode(priv, assoc_req);
543 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400544lbs_deb_assoc("ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200545 goto out;
546 }
547 }
548
Dan Williamsef9a2642007-05-25 16:46:33 -0400549 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
550 ret = assoc_helper_channel(priv, assoc_req);
551 if (ret) {
552 lbs_deb_assoc("ASSOC(:%d) channel: ret = %d\n",
553 __LINE__, ret);
554 goto out;
555 }
556 }
557
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200558 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
559 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
560 ret = assoc_helper_wep_keys(priv, assoc_req);
561 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400562lbs_deb_assoc("ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200563 goto out;
564 }
565 }
566
567 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
568 ret = assoc_helper_secinfo(priv, assoc_req);
569 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400570lbs_deb_assoc("ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200571 goto out;
572 }
573 }
574
575 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
576 ret = assoc_helper_wpa_ie(priv, assoc_req);
577 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400578lbs_deb_assoc("ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200579 goto out;
580 }
581 }
582
583 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
584 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
585 ret = assoc_helper_wpa_keys(priv, assoc_req);
586 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400587lbs_deb_assoc("ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200588 goto out;
589 }
590 }
591
592 /* SSID/BSSID should be the _last_ config option set, because they
593 * trigger the association attempt.
594 */
595 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
596 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
597 int success = 1;
598
599 ret = assoc_helper_associate(priv, assoc_req);
600 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400601 lbs_deb_assoc("ASSOC: association attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200602 ret);
603 success = 0;
604 }
605
606 if (adapter->connect_status != libertas_connected) {
Holger Schurig9012b282007-05-25 11:27:16 -0400607 lbs_deb_assoc("ASSOC: assoication attempt unsuccessful, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200608 "not connected.\n");
609 success = 0;
610 }
611
612 if (success) {
Holger Schurig9012b282007-05-25 11:27:16 -0400613 lbs_deb_assoc("ASSOC: association attempt successful. "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200614 "Associated to '%s' (" MAC_FMT ")\n",
Dan Williamse76850d2007-05-25 17:09:41 -0400615 libertas_escape_essid(adapter->curbssparams.ssid.ssid,
616 adapter->curbssparams.ssid.ssidlength),
617 MAC_ARG(adapter->curbssparams.bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200618 libertas_prepare_and_send_command(priv,
619 cmd_802_11_rssi,
620 0, cmd_option_waitforrsp, 0, NULL);
621
622 libertas_prepare_and_send_command(priv,
623 cmd_802_11_get_log,
624 0, cmd_option_waitforrsp, 0, NULL);
625 } else {
626
627 ret = -1;
628 }
629 }
630
631out:
632 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400633 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200634 ret);
635 }
Dan Williamse76850d2007-05-25 17:09:41 -0400636
637 mutex_lock(&adapter->lock);
638 adapter->in_progress_assoc_req = NULL;
639 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200640 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -0400641
642done:
643 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200644}
645
646
647/*
648 * Caller MUST hold any necessary locks
649 */
650struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
651{
652 struct assoc_request * assoc_req;
653
Dan Williamse76850d2007-05-25 17:09:41 -0400654 if (!adapter->pending_assoc_req) {
655 adapter->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
656 GFP_KERNEL);
657 if (!adapter->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200658 lbs_pr_info("Not enough memory to allocate association"
659 " request!\n");
660 return NULL;
661 }
662 }
663
664 /* Copy current configuration attributes to the association request,
665 * but don't overwrite any that are already set.
666 */
Dan Williamse76850d2007-05-25 17:09:41 -0400667 assoc_req = adapter->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200668 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Dan Williamse76850d2007-05-25 17:09:41 -0400669 memcpy(&assoc_req->ssid, &adapter->curbssparams.ssid,
670 sizeof(struct WLAN_802_11_SSID));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200671 }
672
673 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
674 assoc_req->channel = adapter->curbssparams.channel;
675
Dan Williamse76850d2007-05-25 17:09:41 -0400676 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
677 assoc_req->band = adapter->curbssparams.band;
678
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200679 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
Dan Williams0dc5a292007-05-10 22:58:02 -0400680 assoc_req->mode = adapter->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200681
682 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
683 memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
684 ETH_ALEN);
685 }
686
687 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
688 int i;
689 for (i = 0; i < 4; i++) {
690 memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
691 sizeof(struct WLAN_802_11_KEY));
692 }
693 }
694
695 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
696 assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
697
698 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
699 memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
700 sizeof(struct WLAN_802_11_KEY));
701 }
702
703 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
704 memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
705 sizeof(struct WLAN_802_11_KEY));
706 }
707
708 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
709 memcpy(&assoc_req->secinfo, &adapter->secinfo,
710 sizeof(struct wlan_802_11_security));
711 }
712
713 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
714 memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
715 MAX_WPA_IE_LEN);
716 assoc_req->wpa_ie_len = adapter->wpa_ie_len;
717 }
718
Dan Williamse76850d2007-05-25 17:09:41 -0400719 print_assoc_req(__func__, assoc_req);
720
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200721 return assoc_req;
722}