blob: cc6352ee43854cb709a885851a0e96b2b86069db [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>
5
6#include "assoc.h"
7#include "join.h"
8#include "decl.h"
9#include "hostcmd.h"
10#include "host.h"
11
12
13static const u8 bssid_any[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
14static const u8 bssid_off[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
15
16static int assoc_helper_essid(wlan_private *priv,
17 struct assoc_request * assoc_req)
18{
19 wlan_adapter *adapter = priv->adapter;
20 int ret = 0;
21 int i;
22
23 ENTER();
24
25 lbs_pr_debug(1, "New SSID requested: %s\n", assoc_req->ssid.ssid);
26 if (assoc_req->mode == wlan802_11infrastructure) {
27 if (adapter->prescan) {
28 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 1);
29 }
30
31 i = libertas_find_SSID_in_list(adapter, &assoc_req->ssid,
32 NULL, wlan802_11infrastructure);
33 if (i >= 0) {
34 lbs_pr_debug(1,
35 "SSID found in scan list ... associating...\n");
36
37 ret = wlan_associate(priv, &adapter->scantable[i]);
38 if (ret == 0) {
39 memcpy(&assoc_req->bssid,
40 &adapter->scantable[i].macaddress,
41 ETH_ALEN);
42 }
43 } else {
44 lbs_pr_debug(1, "SSID '%s' not found; cannot associate\n",
45 assoc_req->ssid.ssid);
46 }
47 } else if (assoc_req->mode == wlan802_11ibss) {
48 /* Scan for the network, do not save previous results. Stale
49 * scan data will cause us to join a non-existant adhoc network
50 */
51 libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 0);
52
53 /* Search for the requested SSID in the scan table */
54 i = libertas_find_SSID_in_list(adapter, &assoc_req->ssid, NULL,
55 wlan802_11ibss);
56 if (i >= 0) {
57 lbs_pr_debug(1, "SSID found at %d in List, so join\n", ret);
58 libertas_join_adhoc_network(priv, &adapter->scantable[i]);
59 } else {
60 /* else send START command */
61 lbs_pr_debug(1, "SSID not found in list, so creating adhoc"
62 " with SSID '%s'\n", assoc_req->ssid.ssid);
63 libertas_start_adhoc_network(priv, &assoc_req->ssid);
64 }
65 memcpy(&assoc_req->bssid, &adapter->current_addr, ETH_ALEN);
66 }
67
68 LEAVE();
69 return ret;
70}
71
72
73static int assoc_helper_bssid(wlan_private *priv,
74 struct assoc_request * assoc_req)
75{
76 wlan_adapter *adapter = priv->adapter;
77 int i, ret = 0;
78
79 ENTER();
80
81 lbs_pr_debug(1, "ASSOC: WAP: BSSID = " MAC_FMT "\n",
82 MAC_ARG(assoc_req->bssid));
83
84 /* Search for index position in list for requested MAC */
85 i = libertas_find_BSSID_in_list(adapter, assoc_req->bssid,
86 assoc_req->mode);
87 if (i < 0) {
88 lbs_pr_debug(1, "ASSOC: WAP: BSSID " MAC_FMT " not found, "
89 "cannot associate.\n", MAC_ARG(assoc_req->bssid));
90 goto out;
91 }
92
93 if (assoc_req->mode == wlan802_11infrastructure) {
94 ret = wlan_associate(priv, &adapter->scantable[i]);
95 lbs_pr_debug(1, "ASSOC: return from wlan_associate(bssd) was %d\n", ret);
96 } else if (assoc_req->mode == wlan802_11ibss) {
97 libertas_join_adhoc_network(priv, &adapter->scantable[i]);
98 }
99 memcpy(&assoc_req->ssid, &adapter->scantable[i].ssid,
100 sizeof(struct WLAN_802_11_SSID));
101
102out:
103 LEAVE();
104 return ret;
105}
106
107
108static int assoc_helper_associate(wlan_private *priv,
109 struct assoc_request * assoc_req)
110{
111 int ret = 0, done = 0;
112
113 /* If we're given and 'any' BSSID, try associating based on SSID */
114
115 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
116 if (memcmp(bssid_any, assoc_req->bssid, ETH_ALEN)
117 && memcmp(bssid_off, assoc_req->bssid, ETH_ALEN)) {
118 ret = assoc_helper_bssid(priv, assoc_req);
119 done = 1;
120 if (ret) {
121 lbs_pr_debug(1, "ASSOC: bssid: ret = %d\n", ret);
122 }
123 }
124 }
125
126 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
127 ret = assoc_helper_essid(priv, assoc_req);
128 if (ret) {
129 lbs_pr_debug(1, "ASSOC: bssid: ret = %d\n", ret);
130 }
131 }
132
133 return ret;
134}
135
136
137static int assoc_helper_mode(wlan_private *priv,
138 struct assoc_request * assoc_req)
139{
140 wlan_adapter *adapter = priv->adapter;
141 int ret = 0;
142
143 ENTER();
144
145 if (assoc_req->mode == adapter->inframode) {
146 LEAVE();
147 return 0;
148 }
149
150 if (assoc_req->mode == wlan802_11infrastructure) {
151 if (adapter->psstate != PS_STATE_FULL_POWER)
152 libertas_ps_wakeup(priv, cmd_option_waitforrsp);
153 adapter->psmode = wlan802_11powermodecam;
154 }
155
156 adapter->inframode = assoc_req->mode;
157 ret = libertas_prepare_and_send_command(priv,
158 cmd_802_11_snmp_mib,
159 0, cmd_option_waitforrsp,
160 OID_802_11_INFRASTRUCTURE_MODE,
161 (void *) assoc_req->mode);
162
163 LEAVE();
164 return ret;
165}
166
167
168static int assoc_helper_wep_keys(wlan_private *priv,
169 struct assoc_request * assoc_req)
170{
171 wlan_adapter *adapter = priv->adapter;
172 int i;
173 int ret = 0;
174
175 ENTER();
176
177 /* Set or remove WEP keys */
178 if ( assoc_req->wep_keys[0].len
179 || assoc_req->wep_keys[1].len
180 || assoc_req->wep_keys[2].len
181 || assoc_req->wep_keys[3].len) {
182 ret = libertas_prepare_and_send_command(priv,
183 cmd_802_11_set_wep,
184 cmd_act_add,
185 cmd_option_waitforrsp,
186 0, assoc_req);
187 } else {
188 ret = libertas_prepare_and_send_command(priv,
189 cmd_802_11_set_wep,
190 cmd_act_remove,
191 cmd_option_waitforrsp,
192 0, NULL);
193 }
194
195 if (ret)
196 goto out;
197
198 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -0400199 if (assoc_req->secinfo.wep_enabled)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200200 adapter->currentpacketfilter |= cmd_act_mac_wep_enable;
201 else
202 adapter->currentpacketfilter &= ~cmd_act_mac_wep_enable;
203 ret = libertas_set_mac_packet_filter(priv);
204 if (ret)
205 goto out;
206
207 mutex_lock(&adapter->lock);
208
209 /* Copy WEP keys into adapter wep key fields */
210 for (i = 0; i < 4; i++) {
211 memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
212 sizeof(struct WLAN_802_11_KEY));
213 }
214 adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
215
216 mutex_unlock(&adapter->lock);
217
218out:
219 LEAVE();
220 return ret;
221}
222
223static int assoc_helper_secinfo(wlan_private *priv,
224 struct assoc_request * assoc_req)
225{
226 wlan_adapter *adapter = priv->adapter;
227 int ret = 0;
228
229 ENTER();
230
231 memcpy(&adapter->secinfo, &assoc_req->secinfo,
232 sizeof(struct wlan_802_11_security));
233
234 ret = libertas_set_mac_packet_filter(priv);
235
236 LEAVE();
237 return ret;
238}
239
240
241static int assoc_helper_wpa_keys(wlan_private *priv,
242 struct assoc_request * assoc_req)
243{
244 int ret = 0;
245
246 ENTER();
247
248 /* enable/Disable RSN */
249 ret = libertas_prepare_and_send_command(priv,
250 cmd_802_11_enable_rsn,
251 cmd_act_set,
252 cmd_option_waitforrsp,
253 0, assoc_req);
254 if (ret)
255 goto out;
256
257 ret = libertas_prepare_and_send_command(priv,
258 cmd_802_11_key_material,
259 cmd_act_set,
260 cmd_option_waitforrsp,
261 0, assoc_req);
262
263out:
264 LEAVE();
265 return ret;
266}
267
268
269static int assoc_helper_wpa_ie(wlan_private *priv,
270 struct assoc_request * assoc_req)
271{
272 wlan_adapter *adapter = priv->adapter;
273 int ret = 0;
274
275 ENTER();
276
277 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
278 memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
279 adapter->wpa_ie_len = assoc_req->wpa_ie_len;
280 } else {
281 memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
282 adapter->wpa_ie_len = 0;
283 }
284
285 LEAVE();
286 return ret;
287}
288
289
290static int should_deauth_infrastructure(wlan_adapter *adapter,
291 struct assoc_request * assoc_req)
292{
293 if (adapter->connect_status != libertas_connected)
294 return 0;
295
296 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
297 lbs_pr_debug(1, "Deauthenticating due to new SSID in "
298 " configuration request.\n");
299 return 1;
300 }
301
302 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
Dan Williams6affe782007-05-10 22:56:42 -0400303 if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200304 lbs_pr_debug(1, "Deauthenticating due to updated security "
305 "info in configuration request.\n");
306 return 1;
307 }
308 }
309
310 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
311 lbs_pr_debug(1, "Deauthenticating due to new BSSID in "
312 " configuration request.\n");
313 return 1;
314 }
315
316 /* FIXME: deal with 'auto' mode somehow */
317 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
318 if (assoc_req->mode != wlan802_11infrastructure)
319 return 1;
320 }
321
322 return 0;
323}
324
325
326static int should_stop_adhoc(wlan_adapter *adapter,
327 struct assoc_request * assoc_req)
328{
329 if (adapter->connect_status != libertas_connected)
330 return 0;
331
332 if (adapter->curbssparams.ssid.ssidlength != assoc_req->ssid.ssidlength)
333 return 1;
334 if (memcmp(adapter->curbssparams.ssid.ssid, assoc_req->ssid.ssid,
Dan Williamsad1f3292007-05-10 22:52:37 -0400335 adapter->curbssparams.ssid.ssidlength))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200336 return 1;
337
338 /* FIXME: deal with 'auto' mode somehow */
339 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
340 if (assoc_req->mode != wlan802_11ibss)
341 return 1;
342 }
343
344 return 0;
345}
346
347
348void wlan_association_worker(struct work_struct *work)
349{
350 wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
351 wlan_adapter *adapter = priv->adapter;
352 struct assoc_request * assoc_req = NULL;
353 int ret = 0;
354 int find_any_ssid = 0;
355
356 ENTER();
357
358 mutex_lock(&adapter->lock);
359 assoc_req = adapter->assoc_req;
360 adapter->assoc_req = NULL;
361 mutex_unlock(&adapter->lock);
362
363 if (!assoc_req) {
364 LEAVE();
365 return;
366 }
367
368 lbs_pr_debug(1, "ASSOC: starting new association request: flags = 0x%lX\n",
369 assoc_req->flags);
370
371 /* If 'any' SSID was specified, find an SSID to associate with */
372 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
373 && !assoc_req->ssid.ssidlength)
374 find_any_ssid = 1;
375
376 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
377 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
378 if (memcmp(&assoc_req->bssid, bssid_any, ETH_ALEN)
379 && memcmp(&assoc_req->bssid, bssid_off, ETH_ALEN))
380 find_any_ssid = 0;
381 }
382
383 if (find_any_ssid) {
384 enum WLAN_802_11_NETWORK_INFRASTRUCTURE new_mode;
385
386 ret = libertas_find_best_network_SSID(priv, &assoc_req->ssid,
387 assoc_req->mode, &new_mode);
388 if (ret) {
389 lbs_pr_debug(1, "Could not find best network\n");
390 ret = -ENETUNREACH;
391 goto out;
392 }
393
394 /* Ensure we switch to the mode of the AP */
395 if (assoc_req->mode == wlan802_11autounknown) {
396 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
397 assoc_req->mode = new_mode;
398 }
399 }
400
401 /*
402 * Check if the attributes being changing require deauthentication
403 * from the currently associated infrastructure access point.
404 */
405 if (adapter->inframode == wlan802_11infrastructure) {
406 if (should_deauth_infrastructure(adapter, assoc_req)) {
407 ret = libertas_send_deauthentication(priv);
408 if (ret) {
409 lbs_pr_debug(1, "Deauthentication due to new "
410 "configuration request failed: %d\n",
411 ret);
412 }
413 }
414 } else if (adapter->inframode == wlan802_11ibss) {
415 if (should_stop_adhoc(adapter, assoc_req)) {
416 ret = libertas_stop_adhoc_network(priv);
417 if (ret) {
418 lbs_pr_debug(1, "Teardown of AdHoc network due to "
419 "new configuration request failed: %d\n",
420 ret);
421 }
422
423 }
424 }
425
426 /* Send the various configuration bits to the firmware */
427 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
428 ret = assoc_helper_mode(priv, assoc_req);
429 if (ret) {
430lbs_pr_debug(1, "ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
431 goto out;
432 }
433 }
434
435 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
436 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
437 ret = assoc_helper_wep_keys(priv, assoc_req);
438 if (ret) {
439lbs_pr_debug(1, "ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
440 goto out;
441 }
442 }
443
444 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
445 ret = assoc_helper_secinfo(priv, assoc_req);
446 if (ret) {
447lbs_pr_debug(1, "ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
448 goto out;
449 }
450 }
451
452 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
453 ret = assoc_helper_wpa_ie(priv, assoc_req);
454 if (ret) {
455lbs_pr_debug(1, "ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
456 goto out;
457 }
458 }
459
460 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
461 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
462 ret = assoc_helper_wpa_keys(priv, assoc_req);
463 if (ret) {
464lbs_pr_debug(1, "ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
465 goto out;
466 }
467 }
468
469 /* SSID/BSSID should be the _last_ config option set, because they
470 * trigger the association attempt.
471 */
472 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
473 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
474 int success = 1;
475
476 ret = assoc_helper_associate(priv, assoc_req);
477 if (ret) {
478 lbs_pr_debug(1, "ASSOC: association attempt unsuccessful: %d\n",
479 ret);
480 success = 0;
481 }
482
483 if (adapter->connect_status != libertas_connected) {
484 lbs_pr_debug(1, "ASSOC: assoication attempt unsuccessful, "
485 "not connected.\n");
486 success = 0;
487 }
488
489 if (success) {
490 lbs_pr_debug(1, "ASSOC: association attempt successful. "
491 "Associated to '%s' (" MAC_FMT ")\n",
492 assoc_req->ssid.ssid, MAC_ARG(assoc_req->bssid));
493 libertas_prepare_and_send_command(priv,
494 cmd_802_11_rssi,
495 0, cmd_option_waitforrsp, 0, NULL);
496
497 libertas_prepare_and_send_command(priv,
498 cmd_802_11_get_log,
499 0, cmd_option_waitforrsp, 0, NULL);
500 } else {
501
502 ret = -1;
503 }
504 }
505
506out:
507 if (ret) {
508 lbs_pr_debug(1, "ASSOC: reconfiguration attempt unsuccessful: %d\n",
509 ret);
510 }
511 kfree(assoc_req);
512 LEAVE();
513}
514
515
516/*
517 * Caller MUST hold any necessary locks
518 */
519struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
520{
521 struct assoc_request * assoc_req;
522
523 if (!adapter->assoc_req) {
524 adapter->assoc_req = kzalloc(sizeof(struct assoc_request), GFP_KERNEL);
525 if (!adapter->assoc_req) {
526 lbs_pr_info("Not enough memory to allocate association"
527 " request!\n");
528 return NULL;
529 }
530 }
531
532 /* Copy current configuration attributes to the association request,
533 * but don't overwrite any that are already set.
534 */
535 assoc_req = adapter->assoc_req;
536 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
537 memcpy(&assoc_req->ssid, adapter->curbssparams.ssid.ssid,
538 adapter->curbssparams.ssid.ssidlength);
539 }
540
541 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
542 assoc_req->channel = adapter->curbssparams.channel;
543
544 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
545 assoc_req->mode = adapter->inframode;
546
547 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
548 memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
549 ETH_ALEN);
550 }
551
552 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
553 int i;
554 for (i = 0; i < 4; i++) {
555 memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
556 sizeof(struct WLAN_802_11_KEY));
557 }
558 }
559
560 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
561 assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
562
563 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
564 memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
565 sizeof(struct WLAN_802_11_KEY));
566 }
567
568 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
569 memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
570 sizeof(struct WLAN_802_11_KEY));
571 }
572
573 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
574 memcpy(&assoc_req->secinfo, &adapter->secinfo,
575 sizeof(struct wlan_802_11_security));
576 }
577
578 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
579 memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
580 MAX_WPA_IE_LEN);
581 assoc_req->wpa_ie_len = adapter->wpa_ie_len;
582 }
583
584 return assoc_req;
585}
586
587