libertas: make worker thread not freezable
[linux-2.6] / drivers / net / wireless / libertas / assoc.c
1 /* Copyright (C) 2006, Red Hat, Inc. */
2
3 #include <linux/bitops.h>
4 #include <net/ieee80211.h>
5 #include <linux/etherdevice.h>
6
7 #include "assoc.h"
8 #include "join.h"
9 #include "decl.h"
10 #include "hostcmd.h"
11 #include "host.h"
12 #include "cmd.h"
13
14
15 static const u8 bssid_any[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
16 static const u8 bssid_off[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17
18
19 static int assoc_helper_essid(struct lbs_private *priv,
20                               struct assoc_request * assoc_req)
21 {
22         int ret = 0;
23         struct bss_descriptor * bss;
24         int channel = -1;
25
26         lbs_deb_enter(LBS_DEB_ASSOC);
27
28         /* FIXME: take channel into account when picking SSIDs if a channel
29          * is set.
30          */
31
32         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
33                 channel = assoc_req->channel;
34
35         lbs_deb_assoc("SSID '%s' requested\n",
36                       escape_essid(assoc_req->ssid, assoc_req->ssid_len));
37         if (assoc_req->mode == IW_MODE_INFRA) {
38                 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
39                         assoc_req->ssid_len, 0);
40
41                 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
42                                 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
43                 if (bss != NULL) {
44                         memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
45                         ret = lbs_associate(priv, assoc_req);
46                 } else {
47                         lbs_deb_assoc("SSID not found; cannot associate\n");
48                 }
49         } else if (assoc_req->mode == IW_MODE_ADHOC) {
50                 /* Scan for the network, do not save previous results.  Stale
51                  *   scan data will cause us to join a non-existant adhoc network
52                  */
53                 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
54                         assoc_req->ssid_len, 1);
55
56                 /* Search for the requested SSID in the scan table */
57                 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
58                                 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
59                 if (bss != NULL) {
60                         lbs_deb_assoc("SSID found, will join\n");
61                         memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
62                         lbs_join_adhoc_network(priv, assoc_req);
63                 } else {
64                         /* else send START command */
65                         lbs_deb_assoc("SSID not found, creating adhoc network\n");
66                         memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
67                                 IW_ESSID_MAX_SIZE);
68                         assoc_req->bss.ssid_len = assoc_req->ssid_len;
69                         lbs_start_adhoc_network(priv, assoc_req);
70                 }
71         }
72
73         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
74         return ret;
75 }
76
77
78 static int assoc_helper_bssid(struct lbs_private *priv,
79                               struct assoc_request * assoc_req)
80 {
81         int ret = 0;
82         struct bss_descriptor * bss;
83         DECLARE_MAC_BUF(mac);
84
85         lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %s",
86                 print_mac(mac, assoc_req->bssid));
87
88         /* Search for index position in list for requested MAC */
89         bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
90                             assoc_req->mode);
91         if (bss == NULL) {
92                 lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
93                         "cannot associate.\n", print_mac(mac, assoc_req->bssid));
94                 goto out;
95         }
96
97         memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
98         if (assoc_req->mode == IW_MODE_INFRA) {
99                 ret = lbs_associate(priv, assoc_req);
100                 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
101         } else if (assoc_req->mode == IW_MODE_ADHOC) {
102                 lbs_join_adhoc_network(priv, assoc_req);
103         }
104
105 out:
106         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
107         return ret;
108 }
109
110
111 static int assoc_helper_associate(struct lbs_private *priv,
112                                   struct assoc_request * assoc_req)
113 {
114         int ret = 0, done = 0;
115
116         lbs_deb_enter(LBS_DEB_ASSOC);
117
118         /* If we're given and 'any' BSSID, try associating based on SSID */
119
120         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
121                 if (compare_ether_addr(bssid_any, assoc_req->bssid)
122                     && compare_ether_addr(bssid_off, assoc_req->bssid)) {
123                         ret = assoc_helper_bssid(priv, assoc_req);
124                         done = 1;
125                 }
126         }
127
128         if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
129                 ret = assoc_helper_essid(priv, assoc_req);
130         }
131
132         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
133         return ret;
134 }
135
136
137 static int assoc_helper_mode(struct lbs_private *priv,
138                              struct assoc_request * assoc_req)
139 {
140         int ret = 0;
141
142         lbs_deb_enter(LBS_DEB_ASSOC);
143
144         if (assoc_req->mode == priv->mode)
145                 goto done;
146
147         if (assoc_req->mode == IW_MODE_INFRA) {
148                 if (priv->psstate != PS_STATE_FULL_POWER)
149                         lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
150                 priv->psmode = LBS802_11POWERMODECAM;
151         }
152
153         priv->mode = assoc_req->mode;
154         ret = lbs_prepare_and_send_command(priv,
155                                     CMD_802_11_SNMP_MIB,
156                                     0, CMD_OPTION_WAITFORRSP,
157                                     OID_802_11_INFRASTRUCTURE_MODE,
158                 /* Shoot me now */  (void *) (size_t) assoc_req->mode);
159
160 done:
161         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
162         return ret;
163 }
164
165
166 static int update_channel(struct lbs_private *priv)
167 {
168         int ret;
169
170         /* the channel in f/w could be out of sync, get the current channel */
171         lbs_deb_enter(LBS_DEB_ASSOC);
172
173         ret = lbs_get_channel(priv);
174         if (ret > 0)
175                 priv->curbssparams.channel = (u8) ret;
176
177         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
178         return ret;
179 }
180
181 void lbs_sync_channel(struct work_struct *work)
182 {
183         struct lbs_private *priv = container_of(work, struct lbs_private,
184                 sync_channel);
185
186         lbs_deb_enter(LBS_DEB_ASSOC);
187         if (update_channel(priv) != 0)
188                 lbs_pr_info("Channel synchronization failed.");
189         lbs_deb_leave(LBS_DEB_ASSOC);
190 }
191
192 static int assoc_helper_channel(struct lbs_private *priv,
193                                 struct assoc_request * assoc_req)
194 {
195         int ret = 0;
196
197         lbs_deb_enter(LBS_DEB_ASSOC);
198
199         ret = update_channel(priv);
200         if (ret < 0) {
201                 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
202         }
203
204         if (assoc_req->channel == priv->curbssparams.channel)
205                 goto done;
206
207         if (priv->mesh_dev) {
208                 /* Disconnect mesh while associating -- otherwise it
209                    won't let us change channels */
210                 lbs_mesh_config(priv, 0);
211         }
212
213         lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
214                priv->curbssparams.channel, assoc_req->channel);
215
216         ret = lbs_set_channel(priv, assoc_req->channel);
217         if (ret < 0)
218                 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
219
220         /* FIXME: shouldn't need to grab the channel _again_ after setting
221          * it since the firmware is supposed to return the new channel, but
222          * whatever... */
223         ret = update_channel(priv);
224         if (ret < 0)
225                 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
226
227         if (assoc_req->channel != priv->curbssparams.channel) {
228                 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
229                               assoc_req->channel);
230                 goto restore_mesh;
231         }
232
233         if (   assoc_req->secinfo.wep_enabled
234             &&   (assoc_req->wep_keys[0].len
235                || assoc_req->wep_keys[1].len
236                || assoc_req->wep_keys[2].len
237                || assoc_req->wep_keys[3].len)) {
238                 /* Make sure WEP keys are re-sent to firmware */
239                 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
240         }
241
242         /* Must restart/rejoin adhoc networks after channel change */
243         set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
244
245  restore_mesh:
246         if (priv->mesh_dev)
247                 lbs_mesh_config(priv, 1);
248
249  done:
250         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
251         return ret;
252 }
253
254
255 static int assoc_helper_wep_keys(struct lbs_private *priv,
256                                  struct assoc_request * assoc_req)
257 {
258         int i;
259         int ret = 0;
260
261         lbs_deb_enter(LBS_DEB_ASSOC);
262
263         /* Set or remove WEP keys */
264         if (   assoc_req->wep_keys[0].len
265             || assoc_req->wep_keys[1].len
266             || assoc_req->wep_keys[2].len
267             || assoc_req->wep_keys[3].len) {
268                 ret = lbs_prepare_and_send_command(priv,
269                                             CMD_802_11_SET_WEP,
270                                             CMD_ACT_ADD,
271                                             CMD_OPTION_WAITFORRSP,
272                                             0, assoc_req);
273         } else {
274                 ret = lbs_prepare_and_send_command(priv,
275                                             CMD_802_11_SET_WEP,
276                                             CMD_ACT_REMOVE,
277                                             CMD_OPTION_WAITFORRSP,
278                                             0, NULL);
279         }
280
281         if (ret)
282                 goto out;
283
284         /* enable/disable the MAC's WEP packet filter */
285         if (assoc_req->secinfo.wep_enabled)
286                 priv->currentpacketfilter |= CMD_ACT_MAC_WEP_ENABLE;
287         else
288                 priv->currentpacketfilter &= ~CMD_ACT_MAC_WEP_ENABLE;
289         ret = lbs_set_mac_packet_filter(priv);
290         if (ret)
291                 goto out;
292
293         mutex_lock(&priv->lock);
294
295         /* Copy WEP keys into priv wep key fields */
296         for (i = 0; i < 4; i++) {
297                 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
298                         sizeof(struct enc_key));
299         }
300         priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
301
302         mutex_unlock(&priv->lock);
303
304 out:
305         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
306         return ret;
307 }
308
309 static int assoc_helper_secinfo(struct lbs_private *priv,
310                                 struct assoc_request * assoc_req)
311 {
312         int ret = 0;
313         u32 do_wpa;
314         u32 rsn = 0;
315
316         lbs_deb_enter(LBS_DEB_ASSOC);
317
318         memcpy(&priv->secinfo, &assoc_req->secinfo,
319                 sizeof(struct lbs_802_11_security));
320
321         ret = lbs_set_mac_packet_filter(priv);
322         if (ret)
323                 goto out;
324
325         /* If RSN is already enabled, don't try to enable it again, since
326          * ENABLE_RSN resets internal state machines and will clobber the
327          * 4-way WPA handshake.
328          */
329
330         /* Get RSN enabled/disabled */
331         ret = lbs_prepare_and_send_command(priv,
332                                     CMD_802_11_ENABLE_RSN,
333                                     CMD_ACT_GET,
334                                     CMD_OPTION_WAITFORRSP,
335                                     0, &rsn);
336         if (ret) {
337                 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
338                 goto out;
339         }
340
341         /* Don't re-enable RSN if it's already enabled */
342         do_wpa = (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled);
343         if (do_wpa == rsn)
344                 goto out;
345
346         /* Set RSN enabled/disabled */
347         rsn = do_wpa;
348         ret = lbs_prepare_and_send_command(priv,
349                                     CMD_802_11_ENABLE_RSN,
350                                     CMD_ACT_SET,
351                                     CMD_OPTION_WAITFORRSP,
352                                     0, &rsn);
353
354 out:
355         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
356         return ret;
357 }
358
359
360 static int assoc_helper_wpa_keys(struct lbs_private *priv,
361                                  struct assoc_request * assoc_req)
362 {
363         int ret = 0;
364         unsigned int flags = assoc_req->flags;
365
366         lbs_deb_enter(LBS_DEB_ASSOC);
367
368         /* Work around older firmware bug where WPA unicast and multicast
369          * keys must be set independently.  Seen in SDIO parts with firmware
370          * version 5.0.11p0.
371          */
372
373         if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
374                 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
375                 ret = lbs_prepare_and_send_command(priv,
376                                         CMD_802_11_KEY_MATERIAL,
377                                         CMD_ACT_SET,
378                                         CMD_OPTION_WAITFORRSP,
379                                         0, assoc_req);
380                 assoc_req->flags = flags;
381         }
382
383         if (ret)
384                 goto out;
385
386         if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
387                 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
388
389                 ret = lbs_prepare_and_send_command(priv,
390                                         CMD_802_11_KEY_MATERIAL,
391                                         CMD_ACT_SET,
392                                         CMD_OPTION_WAITFORRSP,
393                                         0, assoc_req);
394                 assoc_req->flags = flags;
395         }
396
397 out:
398         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
399         return ret;
400 }
401
402
403 static int assoc_helper_wpa_ie(struct lbs_private *priv,
404                                struct assoc_request * assoc_req)
405 {
406         int ret = 0;
407
408         lbs_deb_enter(LBS_DEB_ASSOC);
409
410         if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
411                 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
412                 priv->wpa_ie_len = assoc_req->wpa_ie_len;
413         } else {
414                 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
415                 priv->wpa_ie_len = 0;
416         }
417
418         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
419         return ret;
420 }
421
422
423 static int should_deauth_infrastructure(struct lbs_private *priv,
424                                         struct assoc_request * assoc_req)
425 {
426         int ret = 0;
427
428         lbs_deb_enter(LBS_DEB_ASSOC);
429
430         if (priv->connect_status != LBS_CONNECTED)
431                 return 0;
432
433         if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
434                 lbs_deb_assoc("Deauthenticating due to new SSID\n");
435                 ret = 1;
436                 goto out;
437         }
438
439         if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
440                 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
441                         lbs_deb_assoc("Deauthenticating due to new security\n");
442                         ret = 1;
443                         goto out;
444                 }
445         }
446
447         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
448                 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
449                 ret = 1;
450                 goto out;
451         }
452
453         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
454                 lbs_deb_assoc("Deauthenticating due to channel switch\n");
455                 ret = 1;
456                 goto out;
457         }
458
459         /* FIXME: deal with 'auto' mode somehow */
460         if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
461                 if (assoc_req->mode != IW_MODE_INFRA) {
462                         lbs_deb_assoc("Deauthenticating due to leaving "
463                                 "infra mode\n");
464                         ret = 1;
465                         goto out;
466                 }
467         }
468
469 out:
470         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
471         return 0;
472 }
473
474
475 static int should_stop_adhoc(struct lbs_private *priv,
476                              struct assoc_request * assoc_req)
477 {
478         lbs_deb_enter(LBS_DEB_ASSOC);
479
480         if (priv->connect_status != LBS_CONNECTED)
481                 return 0;
482
483         if (lbs_ssid_cmp(priv->curbssparams.ssid,
484                               priv->curbssparams.ssid_len,
485                               assoc_req->ssid, assoc_req->ssid_len) != 0)
486                 return 1;
487
488         /* FIXME: deal with 'auto' mode somehow */
489         if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
490                 if (assoc_req->mode != IW_MODE_ADHOC)
491                         return 1;
492         }
493
494         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
495                 if (assoc_req->channel != priv->curbssparams.channel)
496                         return 1;
497         }
498
499         lbs_deb_leave(LBS_DEB_ASSOC);
500         return 0;
501 }
502
503
504 void lbs_association_worker(struct work_struct *work)
505 {
506         struct lbs_private *priv = container_of(work, struct lbs_private,
507                 assoc_work.work);
508         struct assoc_request * assoc_req = NULL;
509         int ret = 0;
510         int find_any_ssid = 0;
511         DECLARE_MAC_BUF(mac);
512
513         lbs_deb_enter(LBS_DEB_ASSOC);
514
515         mutex_lock(&priv->lock);
516         assoc_req = priv->pending_assoc_req;
517         priv->pending_assoc_req = NULL;
518         priv->in_progress_assoc_req = assoc_req;
519         mutex_unlock(&priv->lock);
520
521         if (!assoc_req)
522                 goto done;
523
524         lbs_deb_assoc(
525                 "Association Request:\n"
526                 "    flags:     0x%08lx\n"
527                 "    SSID:      '%s'\n"
528                 "    chann:     %d\n"
529                 "    band:      %d\n"
530                 "    mode:      %d\n"
531                 "    BSSID:     %s\n"
532                 "    secinfo:  %s%s%s\n"
533                 "    auth_mode: %d\n",
534                 assoc_req->flags,
535                 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
536                 assoc_req->channel, assoc_req->band, assoc_req->mode,
537                 print_mac(mac, assoc_req->bssid),
538                 assoc_req->secinfo.WPAenabled ? " WPA" : "",
539                 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
540                 assoc_req->secinfo.wep_enabled ? " WEP" : "",
541                 assoc_req->secinfo.auth_mode);
542
543         /* If 'any' SSID was specified, find an SSID to associate with */
544         if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
545             && !assoc_req->ssid_len)
546                 find_any_ssid = 1;
547
548         /* But don't use 'any' SSID if there's a valid locked BSSID to use */
549         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
550                 if (compare_ether_addr(assoc_req->bssid, bssid_any)
551                     && compare_ether_addr(assoc_req->bssid, bssid_off))
552                         find_any_ssid = 0;
553         }
554
555         if (find_any_ssid) {
556                 u8 new_mode;
557
558                 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
559                                 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
560                 if (ret) {
561                         lbs_deb_assoc("Could not find best network\n");
562                         ret = -ENETUNREACH;
563                         goto out;
564                 }
565
566                 /* Ensure we switch to the mode of the AP */
567                 if (assoc_req->mode == IW_MODE_AUTO) {
568                         set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
569                         assoc_req->mode = new_mode;
570                 }
571         }
572
573         /*
574          * Check if the attributes being changing require deauthentication
575          * from the currently associated infrastructure access point.
576          */
577         if (priv->mode == IW_MODE_INFRA) {
578                 if (should_deauth_infrastructure(priv, assoc_req)) {
579                         ret = lbs_send_deauthentication(priv);
580                         if (ret) {
581                                 lbs_deb_assoc("Deauthentication due to new "
582                                         "configuration request failed: %d\n",
583                                         ret);
584                         }
585                 }
586         } else if (priv->mode == IW_MODE_ADHOC) {
587                 if (should_stop_adhoc(priv, assoc_req)) {
588                         ret = lbs_stop_adhoc_network(priv);
589                         if (ret) {
590                                 lbs_deb_assoc("Teardown of AdHoc network due to "
591                                         "new configuration request failed: %d\n",
592                                         ret);
593                         }
594
595                 }
596         }
597
598         /* Send the various configuration bits to the firmware */
599         if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
600                 ret = assoc_helper_mode(priv, assoc_req);
601                 if (ret)
602                         goto out;
603         }
604
605         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
606                 ret = assoc_helper_channel(priv, assoc_req);
607                 if (ret)
608                         goto out;
609         }
610
611         if (   test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
612             || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
613                 ret = assoc_helper_wep_keys(priv, assoc_req);
614                 if (ret)
615                         goto out;
616         }
617
618         if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
619                 ret = assoc_helper_secinfo(priv, assoc_req);
620                 if (ret)
621                         goto out;
622         }
623
624         if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
625                 ret = assoc_helper_wpa_ie(priv, assoc_req);
626                 if (ret)
627                         goto out;
628         }
629
630         if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
631             || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
632                 ret = assoc_helper_wpa_keys(priv, assoc_req);
633                 if (ret)
634                         goto out;
635         }
636
637         /* SSID/BSSID should be the _last_ config option set, because they
638          * trigger the association attempt.
639          */
640         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
641             || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
642                 int success = 1;
643
644                 ret = assoc_helper_associate(priv, assoc_req);
645                 if (ret) {
646                         lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
647                                 ret);
648                         success = 0;
649                 }
650
651                 if (priv->connect_status != LBS_CONNECTED) {
652                         lbs_deb_assoc("ASSOC: association unsuccessful, "
653                                 "not connected\n");
654                         success = 0;
655                 }
656
657                 if (success) {
658                         lbs_deb_assoc("ASSOC: associated to '%s', %s\n",
659                                 escape_essid(priv->curbssparams.ssid,
660                                              priv->curbssparams.ssid_len),
661                                 print_mac(mac, priv->curbssparams.bssid));
662                         lbs_prepare_and_send_command(priv,
663                                 CMD_802_11_RSSI,
664                                 0, CMD_OPTION_WAITFORRSP, 0, NULL);
665
666                         lbs_prepare_and_send_command(priv,
667                                 CMD_802_11_GET_LOG,
668                                 0, CMD_OPTION_WAITFORRSP, 0, NULL);
669                 } else {
670                         ret = -1;
671                 }
672         }
673
674 out:
675         if (ret) {
676                 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
677                         ret);
678         }
679
680         mutex_lock(&priv->lock);
681         priv->in_progress_assoc_req = NULL;
682         mutex_unlock(&priv->lock);
683         kfree(assoc_req);
684
685 done:
686         lbs_deb_leave(LBS_DEB_ASSOC);
687 }
688
689
690 /*
691  * Caller MUST hold any necessary locks
692  */
693 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
694 {
695         struct assoc_request * assoc_req;
696
697         lbs_deb_enter(LBS_DEB_ASSOC);
698         if (!priv->pending_assoc_req) {
699                 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
700                                                      GFP_KERNEL);
701                 if (!priv->pending_assoc_req) {
702                         lbs_pr_info("Not enough memory to allocate association"
703                                 " request!\n");
704                         return NULL;
705                 }
706         }
707
708         /* Copy current configuration attributes to the association request,
709          * but don't overwrite any that are already set.
710          */
711         assoc_req = priv->pending_assoc_req;
712         if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
713                 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
714                        IW_ESSID_MAX_SIZE);
715                 assoc_req->ssid_len = priv->curbssparams.ssid_len;
716         }
717
718         if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
719                 assoc_req->channel = priv->curbssparams.channel;
720
721         if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
722                 assoc_req->band = priv->curbssparams.band;
723
724         if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
725                 assoc_req->mode = priv->mode;
726
727         if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
728                 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
729                         ETH_ALEN);
730         }
731
732         if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
733                 int i;
734                 for (i = 0; i < 4; i++) {
735                         memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
736                                 sizeof(struct enc_key));
737                 }
738         }
739
740         if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
741                 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
742
743         if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
744                 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
745                         sizeof(struct enc_key));
746         }
747
748         if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
749                 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
750                         sizeof(struct enc_key));
751         }
752
753         if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
754                 memcpy(&assoc_req->secinfo, &priv->secinfo,
755                         sizeof(struct lbs_802_11_security));
756         }
757
758         if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
759                 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
760                         MAX_WPA_IE_LEN);
761                 assoc_req->wpa_ie_len = priv->wpa_ie_len;
762         }
763
764         lbs_deb_leave(LBS_DEB_ASSOC);
765         return assoc_req;
766 }