blob: 4fcc6a653d997864f60d5ca0c67a8251c6196bf9 [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;
Dan Williamsaeea0ab2007-05-25 22:30:48 -040073 int channel = -1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020074
Holger Schurig9012b282007-05-25 11:27:16 -040075 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020076
Dan Williamsef9a2642007-05-25 16:46:33 -040077 /* FIXME: take channel into account when picking SSIDs if a channel
78 * is set.
79 */
80
Dan Williamsaeea0ab2007-05-25 22:30:48 -040081 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
82 channel = assoc_req->channel;
83
Holger Schurig9012b282007-05-25 11:27:16 -040084 lbs_deb_assoc("New SSID requested: %s\n", assoc_req->ssid.ssid);
Dan Williams0dc5a292007-05-10 22:58:02 -040085 if (assoc_req->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020086 if (adapter->prescan) {
Dan Williamseb8f7332007-05-25 16:25:21 -040087 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020088 }
89
Dan Williamsfcdb53d2007-05-25 16:15:56 -040090 bss = libertas_find_SSID_in_list(adapter, &assoc_req->ssid,
Dan Williamsaeea0ab2007-05-25 22:30:48 -040091 NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -040092 if (bss != NULL) {
93 lbs_deb_assoc("SSID found in scan list, associating\n");
Dan Williamse76850d2007-05-25 17:09:41 -040094 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
95 ret = wlan_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020096 } else {
Holger Schurig9012b282007-05-25 11:27:16 -040097 lbs_deb_assoc("SSID '%s' not found; cannot associate\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020098 assoc_req->ssid.ssid);
99 }
Dan Williams0dc5a292007-05-10 22:58:02 -0400100 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200101 /* Scan for the network, do not save previous results. Stale
102 * scan data will cause us to join a non-existant adhoc network
103 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400104 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200105
106 /* Search for the requested SSID in the scan table */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400107 bss = libertas_find_SSID_in_list(adapter, &assoc_req->ssid, NULL,
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400108 IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400109 if (bss != NULL) {
110 lbs_deb_assoc("SSID found joining\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400111 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
112 libertas_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200113 } else {
114 /* else send START command */
Holger Schurig9012b282007-05-25 11:27:16 -0400115 lbs_deb_assoc("SSID not found in list, so creating adhoc"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200116 " with SSID '%s'\n", assoc_req->ssid.ssid);
Dan Williamse76850d2007-05-25 17:09:41 -0400117 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
118 sizeof(struct WLAN_802_11_SSID));
119 libertas_start_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200120 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200121 }
122
Holger Schurig9012b282007-05-25 11:27:16 -0400123 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200124 return ret;
125}
126
127
128static int assoc_helper_bssid(wlan_private *priv,
129 struct assoc_request * assoc_req)
130{
131 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400132 int ret = 0;
133 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200134
Holger Schurig9012b282007-05-25 11:27:16 -0400135 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID" MAC_FMT "\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200136 MAC_ARG(assoc_req->bssid));
137
138 /* Search for index position in list for requested MAC */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400139 bss = libertas_find_BSSID_in_list(adapter, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200140 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400141 if (bss == NULL) {
Holger Schurig9012b282007-05-25 11:27:16 -0400142 lbs_deb_assoc("ASSOC: WAP: BSSID " MAC_FMT " not found, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200143 "cannot associate.\n", MAC_ARG(assoc_req->bssid));
144 goto out;
145 }
146
Dan Williamse76850d2007-05-25 17:09:41 -0400147 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -0400148 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williamse76850d2007-05-25 17:09:41 -0400149 ret = wlan_associate(priv, assoc_req);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400150 lbs_deb_assoc("ASSOC: wlan_associate(bssid) returned %d\n", ret);
Dan Williams0dc5a292007-05-10 22:58:02 -0400151 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamse76850d2007-05-25 17:09:41 -0400152 libertas_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200153 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200154
155out:
Holger Schurig9012b282007-05-25 11:27:16 -0400156 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200157 return ret;
158}
159
160
161static int assoc_helper_associate(wlan_private *priv,
162 struct assoc_request * assoc_req)
163{
164 int ret = 0, done = 0;
165
166 /* If we're given and 'any' BSSID, try associating based on SSID */
167
168 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -0400169 if (compare_ether_addr(bssid_any, assoc_req->bssid)
170 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200171 ret = assoc_helper_bssid(priv, assoc_req);
172 done = 1;
173 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400174 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200175 }
176 }
177 }
178
179 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
180 ret = assoc_helper_essid(priv, assoc_req);
181 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400182 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200183 }
184 }
185
186 return ret;
187}
188
189
190static int assoc_helper_mode(wlan_private *priv,
191 struct assoc_request * assoc_req)
192{
193 wlan_adapter *adapter = priv->adapter;
194 int ret = 0;
195
Holger Schurig9012b282007-05-25 11:27:16 -0400196 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200197
Holger Schurig9012b282007-05-25 11:27:16 -0400198 if (assoc_req->mode == adapter->mode)
199 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200200
Dan Williams0dc5a292007-05-10 22:58:02 -0400201 if (assoc_req->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200202 if (adapter->psstate != PS_STATE_FULL_POWER)
203 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
204 adapter->psmode = wlan802_11powermodecam;
205 }
206
Dan Williams0dc5a292007-05-10 22:58:02 -0400207 adapter->mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200208 ret = libertas_prepare_and_send_command(priv,
209 cmd_802_11_snmp_mib,
210 0, cmd_option_waitforrsp,
211 OID_802_11_INFRASTRUCTURE_MODE,
Dan Williams0dc5a292007-05-10 22:58:02 -0400212 (void *) (size_t) assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200213
Holger Schurig9012b282007-05-25 11:27:16 -0400214done:
215 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200216 return ret;
217}
218
219
Dan Williamsef9a2642007-05-25 16:46:33 -0400220static int update_channel(wlan_private * priv)
221{
222 /* the channel in f/w could be out of sync, get the current channel */
223 return libertas_prepare_and_send_command(priv, cmd_802_11_rf_channel,
224 cmd_opt_802_11_rf_channel_get,
225 cmd_option_waitforrsp, 0, NULL);
226}
227
228static int assoc_helper_channel(wlan_private *priv,
229 struct assoc_request * assoc_req)
230{
231 wlan_adapter *adapter = priv->adapter;
232 int ret = 0;
233
234 lbs_deb_enter(LBS_DEB_ASSOC);
235
236 ret = update_channel(priv);
237 if (ret < 0) {
238 lbs_deb_assoc("ASSOC: channel: error getting channel.");
239 }
240
241 if (assoc_req->channel == adapter->curbssparams.channel)
242 goto done;
243
244 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
245 adapter->curbssparams.channel, assoc_req->channel);
246
247 ret = libertas_prepare_and_send_command(priv, cmd_802_11_rf_channel,
248 cmd_opt_802_11_rf_channel_set,
249 cmd_option_waitforrsp, 0, &assoc_req->channel);
250 if (ret < 0) {
251 lbs_deb_assoc("ASSOC: channel: error setting channel.");
252 }
253
254 ret = update_channel(priv);
255 if (ret < 0) {
256 lbs_deb_assoc("ASSOC: channel: error getting channel.");
257 }
258
259 if (assoc_req->channel != adapter->curbssparams.channel) {
260 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
261 assoc_req->channel);
262 goto done;
263 }
264
265 if ( assoc_req->secinfo.wep_enabled
266 && (assoc_req->wep_keys[0].len
267 || assoc_req->wep_keys[1].len
268 || assoc_req->wep_keys[2].len
269 || assoc_req->wep_keys[3].len)) {
270 /* Make sure WEP keys are re-sent to firmware */
271 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
272 }
273
274 /* Must restart/rejoin adhoc networks after channel change */
275 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
276
277done:
278 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
279 return ret;
280}
281
282
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200283static int assoc_helper_wep_keys(wlan_private *priv,
284 struct assoc_request * assoc_req)
285{
286 wlan_adapter *adapter = priv->adapter;
287 int i;
288 int ret = 0;
289
Holger Schurig9012b282007-05-25 11:27:16 -0400290 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200291
292 /* Set or remove WEP keys */
293 if ( assoc_req->wep_keys[0].len
294 || assoc_req->wep_keys[1].len
295 || assoc_req->wep_keys[2].len
296 || assoc_req->wep_keys[3].len) {
297 ret = libertas_prepare_and_send_command(priv,
298 cmd_802_11_set_wep,
299 cmd_act_add,
300 cmd_option_waitforrsp,
301 0, assoc_req);
302 } else {
303 ret = libertas_prepare_and_send_command(priv,
304 cmd_802_11_set_wep,
305 cmd_act_remove,
306 cmd_option_waitforrsp,
307 0, NULL);
308 }
309
310 if (ret)
311 goto out;
312
313 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -0400314 if (assoc_req->secinfo.wep_enabled)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200315 adapter->currentpacketfilter |= cmd_act_mac_wep_enable;
316 else
317 adapter->currentpacketfilter &= ~cmd_act_mac_wep_enable;
318 ret = libertas_set_mac_packet_filter(priv);
319 if (ret)
320 goto out;
321
322 mutex_lock(&adapter->lock);
323
324 /* Copy WEP keys into adapter wep key fields */
325 for (i = 0; i < 4; i++) {
326 memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
327 sizeof(struct WLAN_802_11_KEY));
328 }
329 adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
330
331 mutex_unlock(&adapter->lock);
332
333out:
Holger Schurig9012b282007-05-25 11:27:16 -0400334 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200335 return ret;
336}
337
338static int assoc_helper_secinfo(wlan_private *priv,
339 struct assoc_request * assoc_req)
340{
341 wlan_adapter *adapter = priv->adapter;
342 int ret = 0;
343
Holger Schurig9012b282007-05-25 11:27:16 -0400344 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200345
346 memcpy(&adapter->secinfo, &assoc_req->secinfo,
347 sizeof(struct wlan_802_11_security));
348
349 ret = libertas_set_mac_packet_filter(priv);
350
Holger Schurig9012b282007-05-25 11:27:16 -0400351 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200352 return ret;
353}
354
355
356static int assoc_helper_wpa_keys(wlan_private *priv,
357 struct assoc_request * assoc_req)
358{
359 int ret = 0;
360
Holger Schurig9012b282007-05-25 11:27:16 -0400361 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200362
363 /* enable/Disable RSN */
364 ret = libertas_prepare_and_send_command(priv,
365 cmd_802_11_enable_rsn,
366 cmd_act_set,
367 cmd_option_waitforrsp,
368 0, assoc_req);
369 if (ret)
370 goto out;
371
372 ret = libertas_prepare_and_send_command(priv,
373 cmd_802_11_key_material,
374 cmd_act_set,
375 cmd_option_waitforrsp,
376 0, assoc_req);
377
378out:
Holger Schurig9012b282007-05-25 11:27:16 -0400379 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200380 return ret;
381}
382
383
384static int assoc_helper_wpa_ie(wlan_private *priv,
385 struct assoc_request * assoc_req)
386{
387 wlan_adapter *adapter = priv->adapter;
388 int ret = 0;
389
Holger Schurig9012b282007-05-25 11:27:16 -0400390 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200391
392 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
393 memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
394 adapter->wpa_ie_len = assoc_req->wpa_ie_len;
395 } else {
396 memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
397 adapter->wpa_ie_len = 0;
398 }
399
Holger Schurig9012b282007-05-25 11:27:16 -0400400 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200401 return ret;
402}
403
404
405static int should_deauth_infrastructure(wlan_adapter *adapter,
406 struct assoc_request * assoc_req)
407{
408 if (adapter->connect_status != libertas_connected)
409 return 0;
410
411 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400412 lbs_deb_assoc("Deauthenticating due to new SSID in "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200413 " configuration request.\n");
414 return 1;
415 }
416
417 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
Dan Williams6affe782007-05-10 22:56:42 -0400418 if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig9012b282007-05-25 11:27:16 -0400419 lbs_deb_assoc("Deauthenticating due to updated security "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200420 "info in configuration request.\n");
421 return 1;
422 }
423 }
424
425 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400426 lbs_deb_assoc("Deauthenticating due to new BSSID in "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200427 " configuration request.\n");
428 return 1;
429 }
430
431 /* FIXME: deal with 'auto' mode somehow */
432 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400433 if (assoc_req->mode != IW_MODE_INFRA)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200434 return 1;
435 }
436
437 return 0;
438}
439
440
441static int should_stop_adhoc(wlan_adapter *adapter,
442 struct assoc_request * assoc_req)
443{
444 if (adapter->connect_status != libertas_connected)
445 return 0;
446
447 if (adapter->curbssparams.ssid.ssidlength != assoc_req->ssid.ssidlength)
448 return 1;
449 if (memcmp(adapter->curbssparams.ssid.ssid, assoc_req->ssid.ssid,
Dan Williamsad1f3292007-05-10 22:52:37 -0400450 adapter->curbssparams.ssid.ssidlength))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200451 return 1;
452
453 /* FIXME: deal with 'auto' mode somehow */
454 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400455 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456 return 1;
457 }
458
Dan Williamsef9a2642007-05-25 16:46:33 -0400459 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
460 if (assoc_req->channel != adapter->curbssparams.channel)
461 return 1;
462 }
463
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200464 return 0;
465}
466
467
Holger Schurigeb3ce632007-05-24 23:41:15 -0400468void libertas_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469{
470 wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
471 wlan_adapter *adapter = priv->adapter;
472 struct assoc_request * assoc_req = NULL;
473 int ret = 0;
474 int find_any_ssid = 0;
475
Holger Schurig9012b282007-05-25 11:27:16 -0400476 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200477
478 mutex_lock(&adapter->lock);
Dan Williamse76850d2007-05-25 17:09:41 -0400479 assoc_req = adapter->pending_assoc_req;
480 adapter->pending_assoc_req = NULL;
481 adapter->in_progress_assoc_req = assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200482 mutex_unlock(&adapter->lock);
483
Holger Schurig9012b282007-05-25 11:27:16 -0400484 if (!assoc_req)
485 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486
Dan Williamse76850d2007-05-25 17:09:41 -0400487 print_assoc_req(__func__, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200488
489 /* If 'any' SSID was specified, find an SSID to associate with */
490 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
491 && !assoc_req->ssid.ssidlength)
492 find_any_ssid = 1;
493
494 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
495 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -0400496 if (compare_ether_addr(assoc_req->bssid, bssid_any)
497 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200498 find_any_ssid = 0;
499 }
500
501 if (find_any_ssid) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400502 u8 new_mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200503
504 ret = libertas_find_best_network_SSID(priv, &assoc_req->ssid,
505 assoc_req->mode, &new_mode);
506 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400507 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200508 ret = -ENETUNREACH;
509 goto out;
510 }
511
512 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -0400513 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200514 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
515 assoc_req->mode = new_mode;
516 }
517 }
518
519 /*
520 * Check if the attributes being changing require deauthentication
521 * from the currently associated infrastructure access point.
522 */
Dan Williams0dc5a292007-05-10 22:58:02 -0400523 if (adapter->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200524 if (should_deauth_infrastructure(adapter, assoc_req)) {
525 ret = libertas_send_deauthentication(priv);
526 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400527 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200528 "configuration request failed: %d\n",
529 ret);
530 }
531 }
Dan Williams0dc5a292007-05-10 22:58:02 -0400532 } else if (adapter->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533 if (should_stop_adhoc(adapter, assoc_req)) {
534 ret = libertas_stop_adhoc_network(priv);
535 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400536 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200537 "new configuration request failed: %d\n",
538 ret);
539 }
540
541 }
542 }
543
544 /* Send the various configuration bits to the firmware */
545 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
546 ret = assoc_helper_mode(priv, assoc_req);
547 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400548lbs_deb_assoc("ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200549 goto out;
550 }
551 }
552
Dan Williamsef9a2642007-05-25 16:46:33 -0400553 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
554 ret = assoc_helper_channel(priv, assoc_req);
555 if (ret) {
556 lbs_deb_assoc("ASSOC(:%d) channel: ret = %d\n",
557 __LINE__, ret);
558 goto out;
559 }
560 }
561
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200562 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
563 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
564 ret = assoc_helper_wep_keys(priv, assoc_req);
565 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400566lbs_deb_assoc("ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200567 goto out;
568 }
569 }
570
571 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
572 ret = assoc_helper_secinfo(priv, assoc_req);
573 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400574lbs_deb_assoc("ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200575 goto out;
576 }
577 }
578
579 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
580 ret = assoc_helper_wpa_ie(priv, assoc_req);
581 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400582lbs_deb_assoc("ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200583 goto out;
584 }
585 }
586
587 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
588 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
589 ret = assoc_helper_wpa_keys(priv, assoc_req);
590 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400591lbs_deb_assoc("ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200592 goto out;
593 }
594 }
595
596 /* SSID/BSSID should be the _last_ config option set, because they
597 * trigger the association attempt.
598 */
599 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
600 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
601 int success = 1;
602
603 ret = assoc_helper_associate(priv, assoc_req);
604 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400605 lbs_deb_assoc("ASSOC: association attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200606 ret);
607 success = 0;
608 }
609
610 if (adapter->connect_status != libertas_connected) {
Holger Schurig9012b282007-05-25 11:27:16 -0400611 lbs_deb_assoc("ASSOC: assoication attempt unsuccessful, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200612 "not connected.\n");
613 success = 0;
614 }
615
616 if (success) {
Holger Schurig9012b282007-05-25 11:27:16 -0400617 lbs_deb_assoc("ASSOC: association attempt successful. "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200618 "Associated to '%s' (" MAC_FMT ")\n",
Dan Williamse76850d2007-05-25 17:09:41 -0400619 libertas_escape_essid(adapter->curbssparams.ssid.ssid,
620 adapter->curbssparams.ssid.ssidlength),
621 MAC_ARG(adapter->curbssparams.bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200622 libertas_prepare_and_send_command(priv,
623 cmd_802_11_rssi,
624 0, cmd_option_waitforrsp, 0, NULL);
625
626 libertas_prepare_and_send_command(priv,
627 cmd_802_11_get_log,
628 0, cmd_option_waitforrsp, 0, NULL);
629 } else {
630
631 ret = -1;
632 }
633 }
634
635out:
636 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400637 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200638 ret);
639 }
Dan Williamse76850d2007-05-25 17:09:41 -0400640
641 mutex_lock(&adapter->lock);
642 adapter->in_progress_assoc_req = NULL;
643 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200644 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -0400645
646done:
647 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200648}
649
650
651/*
652 * Caller MUST hold any necessary locks
653 */
654struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
655{
656 struct assoc_request * assoc_req;
657
Dan Williamse76850d2007-05-25 17:09:41 -0400658 if (!adapter->pending_assoc_req) {
659 adapter->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
660 GFP_KERNEL);
661 if (!adapter->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200662 lbs_pr_info("Not enough memory to allocate association"
663 " request!\n");
664 return NULL;
665 }
666 }
667
668 /* Copy current configuration attributes to the association request,
669 * but don't overwrite any that are already set.
670 */
Dan Williamse76850d2007-05-25 17:09:41 -0400671 assoc_req = adapter->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200672 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Dan Williamse76850d2007-05-25 17:09:41 -0400673 memcpy(&assoc_req->ssid, &adapter->curbssparams.ssid,
674 sizeof(struct WLAN_802_11_SSID));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200675 }
676
677 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
678 assoc_req->channel = adapter->curbssparams.channel;
679
Dan Williamse76850d2007-05-25 17:09:41 -0400680 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
681 assoc_req->band = adapter->curbssparams.band;
682
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
Dan Williams0dc5a292007-05-10 22:58:02 -0400684 assoc_req->mode = adapter->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200685
686 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
687 memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
688 ETH_ALEN);
689 }
690
691 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
692 int i;
693 for (i = 0; i < 4; i++) {
694 memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
695 sizeof(struct WLAN_802_11_KEY));
696 }
697 }
698
699 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
700 assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
701
702 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
703 memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
704 sizeof(struct WLAN_802_11_KEY));
705 }
706
707 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
708 memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
709 sizeof(struct WLAN_802_11_KEY));
710 }
711
712 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
713 memcpy(&assoc_req->secinfo, &adapter->secinfo,
714 sizeof(struct wlan_802_11_security));
715 }
716
717 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
718 memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
719 MAX_WPA_IE_LEN);
720 assoc_req->wpa_ie_len = adapter->wpa_ie_len;
721 }
722
Dan Williamse76850d2007-05-25 17:09:41 -0400723 print_assoc_req(__func__, assoc_req);
724
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200725 return assoc_req;
726}