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