libertas: convert SET_WEP to a direct command
[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 int lbs_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 = ret;
176                 ret = 0;
177         }
178         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
179         return ret;
180 }
181
182 void lbs_sync_channel(struct work_struct *work)
183 {
184         struct lbs_private *priv = container_of(work, struct lbs_private,
185                 sync_channel);
186
187         lbs_deb_enter(LBS_DEB_ASSOC);
188         if (lbs_update_channel(priv))
189                 lbs_pr_info("Channel synchronization failed.");
190         lbs_deb_leave(LBS_DEB_ASSOC);
191 }
192
193 static int assoc_helper_channel(struct lbs_private *priv,
194                                 struct assoc_request * assoc_req)
195 {
196         int ret = 0;
197
198         lbs_deb_enter(LBS_DEB_ASSOC);
199
200         ret = lbs_update_channel(priv);
201         if (ret) {
202                 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
203                 goto done;
204         }
205
206         if (assoc_req->channel == priv->curbssparams.channel)
207                 goto done;
208
209         if (priv->mesh_dev) {
210                 /* Change mesh channel first; 21.p21 firmware won't let
211                    you change channel otherwise (even though it'll return
212                    an error to this */
213                 lbs_mesh_config(priv, 0, assoc_req->channel);
214         }
215
216         lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
217                       priv->curbssparams.channel, assoc_req->channel);
218
219         ret = lbs_set_channel(priv, assoc_req->channel);
220         if (ret < 0)
221                 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
222
223         /* FIXME: shouldn't need to grab the channel _again_ after setting
224          * it since the firmware is supposed to return the new channel, but
225          * whatever... */
226         ret = lbs_update_channel(priv);
227         if (ret) {
228                 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
229                 goto done;
230         }
231
232         if (assoc_req->channel != priv->curbssparams.channel) {
233                 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
234                               assoc_req->channel);
235                 goto restore_mesh;
236         }
237
238         if (   assoc_req->secinfo.wep_enabled
239             &&   (assoc_req->wep_keys[0].len
240                || assoc_req->wep_keys[1].len
241                || assoc_req->wep_keys[2].len
242                || assoc_req->wep_keys[3].len)) {
243                 /* Make sure WEP keys are re-sent to firmware */
244                 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
245         }
246
247         /* Must restart/rejoin adhoc networks after channel change */
248         set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
249
250  restore_mesh:
251         if (priv->mesh_dev)
252                 lbs_mesh_config(priv, 1, priv->curbssparams.channel);
253
254  done:
255         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
256         return ret;
257 }
258
259
260 static int assoc_helper_wep_keys(struct lbs_private *priv,
261                                  struct assoc_request *assoc_req)
262 {
263         int i;
264         int ret = 0;
265
266         lbs_deb_enter(LBS_DEB_ASSOC);
267
268         /* Set or remove WEP keys */
269         if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
270             assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
271                 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
272         else
273                 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
274
275         if (ret)
276                 goto out;
277
278         /* enable/disable the MAC's WEP packet filter */
279         if (assoc_req->secinfo.wep_enabled)
280                 priv->currentpacketfilter |= CMD_ACT_MAC_WEP_ENABLE;
281         else
282                 priv->currentpacketfilter &= ~CMD_ACT_MAC_WEP_ENABLE;
283
284         ret = lbs_set_mac_packet_filter(priv);
285         if (ret)
286                 goto out;
287
288         mutex_lock(&priv->lock);
289
290         /* Copy WEP keys into priv wep key fields */
291         for (i = 0; i < 4; i++) {
292                 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
293                        sizeof(struct enc_key));
294         }
295         priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
296
297         mutex_unlock(&priv->lock);
298
299 out:
300         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
301         return ret;
302 }
303
304 static int assoc_helper_secinfo(struct lbs_private *priv,
305                                 struct assoc_request * assoc_req)
306 {
307         int ret = 0;
308         u32 do_wpa;
309         u32 rsn = 0;
310
311         lbs_deb_enter(LBS_DEB_ASSOC);
312
313         memcpy(&priv->secinfo, &assoc_req->secinfo,
314                 sizeof(struct lbs_802_11_security));
315
316         ret = lbs_set_mac_packet_filter(priv);
317         if (ret)
318                 goto out;
319
320         /* If RSN is already enabled, don't try to enable it again, since
321          * ENABLE_RSN resets internal state machines and will clobber the
322          * 4-way WPA handshake.
323          */
324
325         /* Get RSN enabled/disabled */
326         ret = lbs_prepare_and_send_command(priv,
327                                     CMD_802_11_ENABLE_RSN,
328                                     CMD_ACT_GET,
329                                     CMD_OPTION_WAITFORRSP,
330                                     0, &rsn);
331         if (ret) {
332                 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
333                 goto out;
334         }
335
336         /* Don't re-enable RSN if it's already enabled */
337         do_wpa = (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled);
338         if (do_wpa == rsn)
339                 goto out;
340
341         /* Set RSN enabled/disabled */
342         rsn = do_wpa;
343         ret = lbs_prepare_and_send_command(priv,
344                                     CMD_802_11_ENABLE_RSN,
345                                     CMD_ACT_SET,
346                                     CMD_OPTION_WAITFORRSP,
347                                     0, &rsn);
348
349 out:
350         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
351         return ret;
352 }
353
354
355 static int assoc_helper_wpa_keys(struct lbs_private *priv,
356                                  struct assoc_request * assoc_req)
357 {
358         int ret = 0;
359         unsigned int flags = assoc_req->flags;
360
361         lbs_deb_enter(LBS_DEB_ASSOC);
362
363         /* Work around older firmware bug where WPA unicast and multicast
364          * keys must be set independently.  Seen in SDIO parts with firmware
365          * version 5.0.11p0.
366          */
367
368         if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
369                 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
370                 ret = lbs_prepare_and_send_command(priv,
371                                         CMD_802_11_KEY_MATERIAL,
372                                         CMD_ACT_SET,
373                                         CMD_OPTION_WAITFORRSP,
374                                         0, assoc_req);
375                 assoc_req->flags = flags;
376         }
377
378         if (ret)
379                 goto out;
380
381         if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
382                 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
383
384                 ret = lbs_prepare_and_send_command(priv,
385                                         CMD_802_11_KEY_MATERIAL,
386                                         CMD_ACT_SET,
387                                         CMD_OPTION_WAITFORRSP,
388                                         0, assoc_req);
389                 assoc_req->flags = flags;
390         }
391
392 out:
393         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
394         return ret;
395 }
396
397
398 static int assoc_helper_wpa_ie(struct lbs_private *priv,
399                                struct assoc_request * assoc_req)
400 {
401         int ret = 0;
402
403         lbs_deb_enter(LBS_DEB_ASSOC);
404
405         if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
406                 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
407                 priv->wpa_ie_len = assoc_req->wpa_ie_len;
408         } else {
409                 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
410                 priv->wpa_ie_len = 0;
411         }
412
413         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
414         return ret;
415 }
416
417
418 static int should_deauth_infrastructure(struct lbs_private *priv,
419                                         struct assoc_request * assoc_req)
420 {
421         int ret = 0;
422
423         lbs_deb_enter(LBS_DEB_ASSOC);
424
425         if (priv->connect_status != LBS_CONNECTED)
426                 return 0;
427
428         if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
429                 lbs_deb_assoc("Deauthenticating due to new SSID\n");
430                 ret = 1;
431                 goto out;
432         }
433
434         if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
435                 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
436                         lbs_deb_assoc("Deauthenticating due to new security\n");
437                         ret = 1;
438                         goto out;
439                 }
440         }
441
442         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
443                 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
444                 ret = 1;
445                 goto out;
446         }
447
448         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
449                 lbs_deb_assoc("Deauthenticating due to channel switch\n");
450                 ret = 1;
451                 goto out;
452         }
453
454         /* FIXME: deal with 'auto' mode somehow */
455         if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
456                 if (assoc_req->mode != IW_MODE_INFRA) {
457                         lbs_deb_assoc("Deauthenticating due to leaving "
458                                 "infra mode\n");
459                         ret = 1;
460                         goto out;
461                 }
462         }
463
464 out:
465         lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
466         return 0;
467 }
468
469
470 static int should_stop_adhoc(struct lbs_private *priv,
471                              struct assoc_request * assoc_req)
472 {
473         lbs_deb_enter(LBS_DEB_ASSOC);
474
475         if (priv->connect_status != LBS_CONNECTED)
476                 return 0;
477
478         if (lbs_ssid_cmp(priv->curbssparams.ssid,
479                               priv->curbssparams.ssid_len,
480                               assoc_req->ssid, assoc_req->ssid_len) != 0)
481                 return 1;
482
483         /* FIXME: deal with 'auto' mode somehow */
484         if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
485                 if (assoc_req->mode != IW_MODE_ADHOC)
486                         return 1;
487         }
488
489         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
490                 if (assoc_req->channel != priv->curbssparams.channel)
491                         return 1;
492         }
493
494         lbs_deb_leave(LBS_DEB_ASSOC);
495         return 0;
496 }
497
498
499 void lbs_association_worker(struct work_struct *work)
500 {
501         struct lbs_private *priv = container_of(work, struct lbs_private,
502                 assoc_work.work);
503         struct assoc_request * assoc_req = NULL;
504         int ret = 0;
505         int find_any_ssid = 0;
506         DECLARE_MAC_BUF(mac);
507
508         lbs_deb_enter(LBS_DEB_ASSOC);
509
510         mutex_lock(&priv->lock);
511         assoc_req = priv->pending_assoc_req;
512         priv->pending_assoc_req = NULL;
513         priv->in_progress_assoc_req = assoc_req;
514         mutex_unlock(&priv->lock);
515
516         if (!assoc_req)
517                 goto done;
518
519         lbs_deb_assoc(
520                 "Association Request:\n"
521                 "    flags:     0x%08lx\n"
522                 "    SSID:      '%s'\n"
523                 "    chann:     %d\n"
524                 "    band:      %d\n"
525                 "    mode:      %d\n"
526                 "    BSSID:     %s\n"
527                 "    secinfo:  %s%s%s\n"
528                 "    auth_mode: %d\n",
529                 assoc_req->flags,
530                 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
531                 assoc_req->channel, assoc_req->band, assoc_req->mode,
532                 print_mac(mac, assoc_req->bssid),
533                 assoc_req->secinfo.WPAenabled ? " WPA" : "",
534                 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
535                 assoc_req->secinfo.wep_enabled ? " WEP" : "",
536                 assoc_req->secinfo.auth_mode);
537
538         /* If 'any' SSID was specified, find an SSID to associate with */
539         if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
540             && !assoc_req->ssid_len)
541                 find_any_ssid = 1;
542
543         /* But don't use 'any' SSID if there's a valid locked BSSID to use */
544         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
545                 if (compare_ether_addr(assoc_req->bssid, bssid_any)
546                     && compare_ether_addr(assoc_req->bssid, bssid_off))
547                         find_any_ssid = 0;
548         }
549
550         if (find_any_ssid) {
551                 u8 new_mode;
552
553                 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
554                                 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
555                 if (ret) {
556                         lbs_deb_assoc("Could not find best network\n");
557                         ret = -ENETUNREACH;
558                         goto out;
559                 }
560
561                 /* Ensure we switch to the mode of the AP */
562                 if (assoc_req->mode == IW_MODE_AUTO) {
563                         set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
564                         assoc_req->mode = new_mode;
565                 }
566         }
567
568         /*
569          * Check if the attributes being changing require deauthentication
570          * from the currently associated infrastructure access point.
571          */
572         if (priv->mode == IW_MODE_INFRA) {
573                 if (should_deauth_infrastructure(priv, assoc_req)) {
574                         ret = lbs_send_deauthentication(priv);
575                         if (ret) {
576                                 lbs_deb_assoc("Deauthentication due to new "
577                                         "configuration request failed: %d\n",
578                                         ret);
579                         }
580                 }
581         } else if (priv->mode == IW_MODE_ADHOC) {
582                 if (should_stop_adhoc(priv, assoc_req)) {
583                         ret = lbs_stop_adhoc_network(priv);
584                         if (ret) {
585                                 lbs_deb_assoc("Teardown of AdHoc network due to "
586                                         "new configuration request failed: %d\n",
587                                         ret);
588                         }
589
590                 }
591         }
592
593         /* Send the various configuration bits to the firmware */
594         if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
595                 ret = assoc_helper_mode(priv, assoc_req);
596                 if (ret)
597                         goto out;
598         }
599
600         if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
601                 ret = assoc_helper_channel(priv, assoc_req);
602                 if (ret)
603                         goto out;
604         }
605
606         if (   test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
607             || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
608                 ret = assoc_helper_wep_keys(priv, assoc_req);
609                 if (ret)
610                         goto out;
611         }
612
613         if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
614                 ret = assoc_helper_secinfo(priv, assoc_req);
615                 if (ret)
616                         goto out;
617         }
618
619         if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
620                 ret = assoc_helper_wpa_ie(priv, assoc_req);
621                 if (ret)
622                         goto out;
623         }
624
625         if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
626             || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
627                 ret = assoc_helper_wpa_keys(priv, assoc_req);
628                 if (ret)
629                         goto out;
630         }
631
632         /* SSID/BSSID should be the _last_ config option set, because they
633          * trigger the association attempt.
634          */
635         if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
636             || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
637                 int success = 1;
638
639                 ret = assoc_helper_associate(priv, assoc_req);
640                 if (ret) {
641                         lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
642                                 ret);
643                         success = 0;
644                 }
645
646                 if (priv->connect_status != LBS_CONNECTED) {
647                         lbs_deb_assoc("ASSOC: association unsuccessful, "
648                                 "not connected\n");
649                         success = 0;
650                 }
651
652                 if (success) {
653                         lbs_deb_assoc("ASSOC: associated to '%s', %s\n",
654                                 escape_essid(priv->curbssparams.ssid,
655                                              priv->curbssparams.ssid_len),
656                                 print_mac(mac, priv->curbssparams.bssid));
657                         lbs_prepare_and_send_command(priv,
658                                 CMD_802_11_RSSI,
659                                 0, CMD_OPTION_WAITFORRSP, 0, NULL);
660
661                         lbs_prepare_and_send_command(priv,
662                                 CMD_802_11_GET_LOG,
663                                 0, CMD_OPTION_WAITFORRSP, 0, NULL);
664                 } else {
665                         ret = -1;
666                 }
667         }
668
669 out:
670         if (ret) {
671                 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
672                         ret);
673         }
674
675         mutex_lock(&priv->lock);
676         priv->in_progress_assoc_req = NULL;
677         mutex_unlock(&priv->lock);
678         kfree(assoc_req);
679
680 done:
681         lbs_deb_leave(LBS_DEB_ASSOC);
682 }
683
684
685 /*
686  * Caller MUST hold any necessary locks
687  */
688 struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
689 {
690         struct assoc_request * assoc_req;
691
692         lbs_deb_enter(LBS_DEB_ASSOC);
693         if (!priv->pending_assoc_req) {
694                 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
695                                                      GFP_KERNEL);
696                 if (!priv->pending_assoc_req) {
697                         lbs_pr_info("Not enough memory to allocate association"
698                                 " request!\n");
699                         return NULL;
700                 }
701         }
702
703         /* Copy current configuration attributes to the association request,
704          * but don't overwrite any that are already set.
705          */
706         assoc_req = priv->pending_assoc_req;
707         if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
708                 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
709                        IW_ESSID_MAX_SIZE);
710                 assoc_req->ssid_len = priv->curbssparams.ssid_len;
711         }
712
713         if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
714                 assoc_req->channel = priv->curbssparams.channel;
715
716         if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
717                 assoc_req->band = priv->curbssparams.band;
718
719         if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
720                 assoc_req->mode = priv->mode;
721
722         if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
723                 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
724                         ETH_ALEN);
725         }
726
727         if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
728                 int i;
729                 for (i = 0; i < 4; i++) {
730                         memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
731                                 sizeof(struct enc_key));
732                 }
733         }
734
735         if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
736                 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
737
738         if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
739                 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
740                         sizeof(struct enc_key));
741         }
742
743         if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
744                 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
745                         sizeof(struct enc_key));
746         }
747
748         if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
749                 memcpy(&assoc_req->secinfo, &priv->secinfo,
750                         sizeof(struct lbs_802_11_security));
751         }
752
753         if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
754                 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
755                         MAX_WPA_IE_LEN);
756                 assoc_req->wpa_ie_len = priv->wpa_ie_len;
757         }
758
759         lbs_deb_leave(LBS_DEB_ASSOC);
760         return assoc_req;
761 }