blob: bf804d31142272cdfc42cce2069fa515f09f9f34 [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"
Dan Williams785e8f22007-05-25 23:51:12 -040051 " Encryption:%s%s%s\n"
52 " auth: %d\n",
Dan Williamse76850d2007-05-25 17:09:41 -040053 extra, assoc_req->flags,
54 libertas_escape_essid(assoc_req->ssid.ssid, assoc_req->ssid.ssidlength),
55 assoc_req->channel, assoc_req->band, assoc_req->mode,
Dan Williams785e8f22007-05-25 23:51:12 -040056 MAC_ARG(assoc_req->bssid),
57 assoc_req->secinfo.WPAenabled ? " WPA" : "",
58 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
59 assoc_req->secinfo.wep_enabled ? " WEP" : "",
60 assoc_req->secinfo.auth_mode);
Dan Williamse76850d2007-05-25 17:09:41 -040061}
62
63
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020064static int assoc_helper_essid(wlan_private *priv,
65 struct assoc_request * assoc_req)
66{
67 wlan_adapter *adapter = priv->adapter;
68 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -040069 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -040070 int channel = -1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020071
Holger Schurig9012b282007-05-25 11:27:16 -040072 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020073
Dan Williamsef9a2642007-05-25 16:46:33 -040074 /* FIXME: take channel into account when picking SSIDs if a channel
75 * is set.
76 */
77
Dan Williamsaeea0ab2007-05-25 22:30:48 -040078 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
79 channel = assoc_req->channel;
80
Holger Schurig9012b282007-05-25 11:27:16 -040081 lbs_deb_assoc("New SSID requested: %s\n", assoc_req->ssid.ssid);
Dan Williams0dc5a292007-05-10 22:58:02 -040082 if (assoc_req->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020083 if (adapter->prescan) {
Dan Williamseb8f7332007-05-25 16:25:21 -040084 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020085 }
86
Dan Williamsfcdb53d2007-05-25 16:15:56 -040087 bss = libertas_find_SSID_in_list(adapter, &assoc_req->ssid,
Dan Williamsaeea0ab2007-05-25 22:30:48 -040088 NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -040089 if (bss != NULL) {
90 lbs_deb_assoc("SSID found in scan list, associating\n");
Dan Williamse76850d2007-05-25 17:09:41 -040091 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
92 ret = wlan_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020093 } else {
Holger Schurig9012b282007-05-25 11:27:16 -040094 lbs_deb_assoc("SSID '%s' not found; cannot associate\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020095 assoc_req->ssid.ssid);
96 }
Dan Williams0dc5a292007-05-10 22:58:02 -040097 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020098 /* Scan for the network, do not save previous results. Stale
99 * scan data will cause us to join a non-existant adhoc network
100 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400101 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200102
103 /* Search for the requested SSID in the scan table */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400104 bss = libertas_find_SSID_in_list(adapter, &assoc_req->ssid, NULL,
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400105 IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400106 if (bss != NULL) {
107 lbs_deb_assoc("SSID found joining\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400108 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
109 libertas_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200110 } else {
111 /* else send START command */
Holger Schurig9012b282007-05-25 11:27:16 -0400112 lbs_deb_assoc("SSID not found in list, so creating adhoc"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200113 " with SSID '%s'\n", assoc_req->ssid.ssid);
Dan Williamse76850d2007-05-25 17:09:41 -0400114 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
115 sizeof(struct WLAN_802_11_SSID));
116 libertas_start_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200117 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200118 }
119
Holger Schurig9012b282007-05-25 11:27:16 -0400120 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200121 return ret;
122}
123
124
125static int assoc_helper_bssid(wlan_private *priv,
126 struct assoc_request * assoc_req)
127{
128 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400129 int ret = 0;
130 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200131
Dan Williams785e8f22007-05-25 23:51:12 -0400132 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID " MAC_FMT,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200133 MAC_ARG(assoc_req->bssid));
134
135 /* Search for index position in list for requested MAC */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400136 bss = libertas_find_BSSID_in_list(adapter, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200137 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400138 if (bss == NULL) {
Holger Schurig9012b282007-05-25 11:27:16 -0400139 lbs_deb_assoc("ASSOC: WAP: BSSID " MAC_FMT " not found, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200140 "cannot associate.\n", MAC_ARG(assoc_req->bssid));
141 goto out;
142 }
143
Dan Williamse76850d2007-05-25 17:09:41 -0400144 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -0400145 if (assoc_req->mode == IW_MODE_INFRA) {
Dan Williamse76850d2007-05-25 17:09:41 -0400146 ret = wlan_associate(priv, assoc_req);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400147 lbs_deb_assoc("ASSOC: wlan_associate(bssid) returned %d\n", ret);
Dan Williams0dc5a292007-05-10 22:58:02 -0400148 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamse76850d2007-05-25 17:09:41 -0400149 libertas_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200150 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200151
152out:
Holger Schurig9012b282007-05-25 11:27:16 -0400153 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200154 return ret;
155}
156
157
158static int assoc_helper_associate(wlan_private *priv,
159 struct assoc_request * assoc_req)
160{
161 int ret = 0, done = 0;
162
163 /* If we're given and 'any' BSSID, try associating based on SSID */
164
165 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -0400166 if (compare_ether_addr(bssid_any, assoc_req->bssid)
167 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200168 ret = assoc_helper_bssid(priv, assoc_req);
169 done = 1;
170 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400171 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200172 }
173 }
174 }
175
176 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
177 ret = assoc_helper_essid(priv, assoc_req);
178 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400179 lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200180 }
181 }
182
183 return ret;
184}
185
186
187static int assoc_helper_mode(wlan_private *priv,
188 struct assoc_request * assoc_req)
189{
190 wlan_adapter *adapter = priv->adapter;
191 int ret = 0;
192
Holger Schurig9012b282007-05-25 11:27:16 -0400193 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200194
Holger Schurig9012b282007-05-25 11:27:16 -0400195 if (assoc_req->mode == adapter->mode)
196 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200197
Dan Williams0dc5a292007-05-10 22:58:02 -0400198 if (assoc_req->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200199 if (adapter->psstate != PS_STATE_FULL_POWER)
200 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
201 adapter->psmode = wlan802_11powermodecam;
202 }
203
Dan Williams0dc5a292007-05-10 22:58:02 -0400204 adapter->mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200205 ret = libertas_prepare_and_send_command(priv,
206 cmd_802_11_snmp_mib,
207 0, cmd_option_waitforrsp,
208 OID_802_11_INFRASTRUCTURE_MODE,
David Woodhouse981f1872007-05-25 23:36:54 -0400209 /* Shoot me now */ (void *) (size_t) assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200210
Holger Schurig9012b282007-05-25 11:27:16 -0400211done:
212 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200213 return ret;
214}
215
216
Dan Williamsef9a2642007-05-25 16:46:33 -0400217static int update_channel(wlan_private * priv)
218{
219 /* the channel in f/w could be out of sync, get the current channel */
220 return libertas_prepare_and_send_command(priv, cmd_802_11_rf_channel,
221 cmd_opt_802_11_rf_channel_get,
222 cmd_option_waitforrsp, 0, NULL);
223}
224
225static int assoc_helper_channel(wlan_private *priv,
226 struct assoc_request * assoc_req)
227{
228 wlan_adapter *adapter = priv->adapter;
229 int ret = 0;
230
231 lbs_deb_enter(LBS_DEB_ASSOC);
232
233 ret = update_channel(priv);
234 if (ret < 0) {
235 lbs_deb_assoc("ASSOC: channel: error getting channel.");
236 }
237
238 if (assoc_req->channel == adapter->curbssparams.channel)
239 goto done;
240
241 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
242 adapter->curbssparams.channel, assoc_req->channel);
243
244 ret = libertas_prepare_and_send_command(priv, cmd_802_11_rf_channel,
245 cmd_opt_802_11_rf_channel_set,
246 cmd_option_waitforrsp, 0, &assoc_req->channel);
247 if (ret < 0) {
248 lbs_deb_assoc("ASSOC: channel: error setting channel.");
249 }
250
251 ret = update_channel(priv);
252 if (ret < 0) {
253 lbs_deb_assoc("ASSOC: channel: error getting channel.");
254 }
255
256 if (assoc_req->channel != adapter->curbssparams.channel) {
257 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
258 assoc_req->channel);
259 goto done;
260 }
261
262 if ( assoc_req->secinfo.wep_enabled
263 && (assoc_req->wep_keys[0].len
264 || assoc_req->wep_keys[1].len
265 || assoc_req->wep_keys[2].len
266 || assoc_req->wep_keys[3].len)) {
267 /* Make sure WEP keys are re-sent to firmware */
268 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
269 }
270
271 /* Must restart/rejoin adhoc networks after channel change */
272 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
273
274done:
275 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
276 return ret;
277}
278
279
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200280static int assoc_helper_wep_keys(wlan_private *priv,
281 struct assoc_request * assoc_req)
282{
283 wlan_adapter *adapter = priv->adapter;
284 int i;
285 int ret = 0;
286
Holger Schurig9012b282007-05-25 11:27:16 -0400287 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200288
289 /* Set or remove WEP keys */
290 if ( assoc_req->wep_keys[0].len
291 || assoc_req->wep_keys[1].len
292 || assoc_req->wep_keys[2].len
293 || assoc_req->wep_keys[3].len) {
294 ret = libertas_prepare_and_send_command(priv,
295 cmd_802_11_set_wep,
296 cmd_act_add,
297 cmd_option_waitforrsp,
298 0, assoc_req);
299 } else {
300 ret = libertas_prepare_and_send_command(priv,
301 cmd_802_11_set_wep,
302 cmd_act_remove,
303 cmd_option_waitforrsp,
304 0, NULL);
305 }
306
307 if (ret)
308 goto out;
309
310 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -0400311 if (assoc_req->secinfo.wep_enabled)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200312 adapter->currentpacketfilter |= cmd_act_mac_wep_enable;
313 else
314 adapter->currentpacketfilter &= ~cmd_act_mac_wep_enable;
315 ret = libertas_set_mac_packet_filter(priv);
316 if (ret)
317 goto out;
318
319 mutex_lock(&adapter->lock);
320
321 /* Copy WEP keys into adapter wep key fields */
322 for (i = 0; i < 4; i++) {
323 memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
324 sizeof(struct WLAN_802_11_KEY));
325 }
326 adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
327
328 mutex_unlock(&adapter->lock);
329
330out:
Holger Schurig9012b282007-05-25 11:27:16 -0400331 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200332 return ret;
333}
334
335static int assoc_helper_secinfo(wlan_private *priv,
336 struct assoc_request * assoc_req)
337{
338 wlan_adapter *adapter = priv->adapter;
339 int ret = 0;
340
Holger Schurig9012b282007-05-25 11:27:16 -0400341 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200342
343 memcpy(&adapter->secinfo, &assoc_req->secinfo,
344 sizeof(struct wlan_802_11_security));
345
346 ret = libertas_set_mac_packet_filter(priv);
Dan Williams90a42212007-05-25 23:01:24 -0400347 if (ret)
348 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200349
Dan Williams90a42212007-05-25 23:01:24 -0400350 /* enable/disable RSN */
351 ret = libertas_prepare_and_send_command(priv,
352 cmd_802_11_enable_rsn,
353 cmd_act_set,
354 cmd_option_waitforrsp,
355 0, assoc_req);
356
357out:
Holger Schurig9012b282007-05-25 11:27:16 -0400358 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200359 return ret;
360}
361
362
363static int assoc_helper_wpa_keys(wlan_private *priv,
364 struct assoc_request * assoc_req)
365{
366 int ret = 0;
367
Holger Schurig9012b282007-05-25 11:27:16 -0400368 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200369
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370 ret = libertas_prepare_and_send_command(priv,
371 cmd_802_11_key_material,
372 cmd_act_set,
373 cmd_option_waitforrsp,
374 0, assoc_req);
375
Holger Schurig9012b282007-05-25 11:27:16 -0400376 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200377 return ret;
378}
379
380
381static int assoc_helper_wpa_ie(wlan_private *priv,
382 struct assoc_request * assoc_req)
383{
384 wlan_adapter *adapter = priv->adapter;
385 int ret = 0;
386
Holger Schurig9012b282007-05-25 11:27:16 -0400387 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200388
389 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
390 memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
391 adapter->wpa_ie_len = assoc_req->wpa_ie_len;
392 } else {
393 memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
394 adapter->wpa_ie_len = 0;
395 }
396
Holger Schurig9012b282007-05-25 11:27:16 -0400397 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200398 return ret;
399}
400
401
402static int should_deauth_infrastructure(wlan_adapter *adapter,
403 struct assoc_request * assoc_req)
404{
405 if (adapter->connect_status != libertas_connected)
406 return 0;
407
408 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400409 lbs_deb_assoc("Deauthenticating due to new SSID in "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200410 " configuration request.\n");
411 return 1;
412 }
413
414 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
Dan Williams6affe782007-05-10 22:56:42 -0400415 if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig9012b282007-05-25 11:27:16 -0400416 lbs_deb_assoc("Deauthenticating due to updated security "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200417 "info in configuration request.\n");
418 return 1;
419 }
420 }
421
422 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig9012b282007-05-25 11:27:16 -0400423 lbs_deb_assoc("Deauthenticating due to new BSSID in "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200424 " configuration request.\n");
425 return 1;
426 }
427
428 /* FIXME: deal with 'auto' mode somehow */
429 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400430 if (assoc_req->mode != IW_MODE_INFRA)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200431 return 1;
432 }
433
434 return 0;
435}
436
437
438static int should_stop_adhoc(wlan_adapter *adapter,
439 struct assoc_request * assoc_req)
440{
441 if (adapter->connect_status != libertas_connected)
442 return 0;
443
444 if (adapter->curbssparams.ssid.ssidlength != assoc_req->ssid.ssidlength)
445 return 1;
446 if (memcmp(adapter->curbssparams.ssid.ssid, assoc_req->ssid.ssid,
Dan Williamsad1f3292007-05-10 22:52:37 -0400447 adapter->curbssparams.ssid.ssidlength))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200448 return 1;
449
450 /* FIXME: deal with 'auto' mode somehow */
451 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400452 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200453 return 1;
454 }
455
Dan Williamsef9a2642007-05-25 16:46:33 -0400456 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
457 if (assoc_req->channel != adapter->curbssparams.channel)
458 return 1;
459 }
460
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200461 return 0;
462}
463
464
Holger Schurigeb3ce632007-05-24 23:41:15 -0400465void libertas_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200466{
467 wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
468 wlan_adapter *adapter = priv->adapter;
469 struct assoc_request * assoc_req = NULL;
470 int ret = 0;
471 int find_any_ssid = 0;
472
Holger Schurig9012b282007-05-25 11:27:16 -0400473 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200474
475 mutex_lock(&adapter->lock);
Dan Williamse76850d2007-05-25 17:09:41 -0400476 assoc_req = adapter->pending_assoc_req;
477 adapter->pending_assoc_req = NULL;
478 adapter->in_progress_assoc_req = assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200479 mutex_unlock(&adapter->lock);
480
Holger Schurig9012b282007-05-25 11:27:16 -0400481 if (!assoc_req)
482 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200483
Dan Williamse76850d2007-05-25 17:09:41 -0400484 print_assoc_req(__func__, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200485
486 /* If 'any' SSID was specified, find an SSID to associate with */
487 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
488 && !assoc_req->ssid.ssidlength)
489 find_any_ssid = 1;
490
491 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
492 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -0400493 if (compare_ether_addr(assoc_req->bssid, bssid_any)
494 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200495 find_any_ssid = 0;
496 }
497
498 if (find_any_ssid) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400499 u8 new_mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500
501 ret = libertas_find_best_network_SSID(priv, &assoc_req->ssid,
502 assoc_req->mode, &new_mode);
503 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400504 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200505 ret = -ENETUNREACH;
506 goto out;
507 }
508
509 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -0400510 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200511 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
512 assoc_req->mode = new_mode;
513 }
514 }
515
516 /*
517 * Check if the attributes being changing require deauthentication
518 * from the currently associated infrastructure access point.
519 */
Dan Williams0dc5a292007-05-10 22:58:02 -0400520 if (adapter->mode == IW_MODE_INFRA) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200521 if (should_deauth_infrastructure(adapter, assoc_req)) {
522 ret = libertas_send_deauthentication(priv);
523 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400524 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200525 "configuration request failed: %d\n",
526 ret);
527 }
528 }
Dan Williams0dc5a292007-05-10 22:58:02 -0400529 } else if (adapter->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200530 if (should_stop_adhoc(adapter, assoc_req)) {
531 ret = libertas_stop_adhoc_network(priv);
532 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400533 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200534 "new configuration request failed: %d\n",
535 ret);
536 }
537
538 }
539 }
540
541 /* Send the various configuration bits to the firmware */
542 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
543 ret = assoc_helper_mode(priv, assoc_req);
544 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400545lbs_deb_assoc("ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200546 goto out;
547 }
548 }
549
Dan Williamsef9a2642007-05-25 16:46:33 -0400550 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
551 ret = assoc_helper_channel(priv, assoc_req);
552 if (ret) {
553 lbs_deb_assoc("ASSOC(:%d) channel: ret = %d\n",
554 __LINE__, ret);
555 goto out;
556 }
557 }
558
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
560 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
561 ret = assoc_helper_wep_keys(priv, assoc_req);
562 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400563lbs_deb_assoc("ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200564 goto out;
565 }
566 }
567
568 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
569 ret = assoc_helper_secinfo(priv, assoc_req);
570 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400571lbs_deb_assoc("ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200572 goto out;
573 }
574 }
575
576 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
577 ret = assoc_helper_wpa_ie(priv, assoc_req);
578 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400579lbs_deb_assoc("ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200580 goto out;
581 }
582 }
583
584 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
585 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
586 ret = assoc_helper_wpa_keys(priv, assoc_req);
587 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400588lbs_deb_assoc("ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200589 goto out;
590 }
591 }
592
593 /* SSID/BSSID should be the _last_ config option set, because they
594 * trigger the association attempt.
595 */
596 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
597 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
598 int success = 1;
599
600 ret = assoc_helper_associate(priv, assoc_req);
601 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400602 lbs_deb_assoc("ASSOC: association attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200603 ret);
604 success = 0;
605 }
606
607 if (adapter->connect_status != libertas_connected) {
Holger Schurig9012b282007-05-25 11:27:16 -0400608 lbs_deb_assoc("ASSOC: assoication attempt unsuccessful, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609 "not connected.\n");
610 success = 0;
611 }
612
613 if (success) {
Holger Schurig9012b282007-05-25 11:27:16 -0400614 lbs_deb_assoc("ASSOC: association attempt successful. "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200615 "Associated to '%s' (" MAC_FMT ")\n",
Dan Williamse76850d2007-05-25 17:09:41 -0400616 libertas_escape_essid(adapter->curbssparams.ssid.ssid,
617 adapter->curbssparams.ssid.ssidlength),
618 MAC_ARG(adapter->curbssparams.bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200619 libertas_prepare_and_send_command(priv,
620 cmd_802_11_rssi,
621 0, cmd_option_waitforrsp, 0, NULL);
622
623 libertas_prepare_and_send_command(priv,
624 cmd_802_11_get_log,
625 0, cmd_option_waitforrsp, 0, NULL);
626 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200627 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}