blob: 882e51c16a7557619e4e4dd644c4ac16f41940cf [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/* Copyright (C) 2006, Red Hat, Inc. */
2
3#include <linux/bitops.h>
4#include <net/ieee80211.h>
Dan Williams3cf209312007-05-25 17:28:30 -04005#include <linux/etherdevice.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02006
7#include "assoc.h"
8#include "join.h"
9#include "decl.h"
10#include "hostcmd.h"
11#include "host.h"
Dan Williams2dd4b262007-12-11 16:54:15 -050012#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020013
14
15static const u8 bssid_any[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
16static const u8 bssid_off[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17
Dan Williamse76850d2007-05-25 17:09:41 -040018
Holger Schurig69f90322007-11-23 15:43:44 +010019static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020020 struct assoc_request * assoc_req)
21{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020022 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -040023 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -040024 int channel = -1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020025
Holger Schurig9012b282007-05-25 11:27:16 -040026 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020027
Dan Williamsef9a2642007-05-25 16:46:33 -040028 /* FIXME: take channel into account when picking SSIDs if a channel
29 * is set.
30 */
31
Dan Williamsaeea0ab2007-05-25 22:30:48 -040032 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
33 channel = assoc_req->channel;
34
Holger Schurig0765af42007-10-15 12:55:56 +020035 lbs_deb_assoc("SSID '%s' requested\n",
Dan Williamsd8efea22007-05-28 23:54:55 -040036 escape_essid(assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -040037 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -050038 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig6e22a852007-08-02 13:05:32 -040039 assoc_req->ssid_len, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020040
David Woodhouseaa21c002007-12-08 20:04:36 +000041 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -040042 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -040043 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -040044 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Holger Schurig10078322007-11-15 18:05:47 -050045 ret = lbs_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020046 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -040047 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020048 }
Dan Williams0dc5a292007-05-10 22:58:02 -040049 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020050 /* Scan for the network, do not save previous results. Stale
51 * scan data will cause us to join a non-existant adhoc network
52 */
Holger Schurig10078322007-11-15 18:05:47 -050053 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -040054 assoc_req->ssid_len, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020055
56 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +000057 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -040058 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -040059 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -040060 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -040061 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Holger Schurig10078322007-11-15 18:05:47 -050062 lbs_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020063 } else {
64 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -040065 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -040066 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -040067 IW_ESSID_MAX_SIZE);
68 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Holger Schurig10078322007-11-15 18:05:47 -050069 lbs_start_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020070 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020071 }
72
Holger Schurig9012b282007-05-25 11:27:16 -040073 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020074 return ret;
75}
76
77
Holger Schurig69f90322007-11-23 15:43:44 +010078static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020079 struct assoc_request * assoc_req)
80{
Dan Williamsfcdb53d2007-05-25 16:15:56 -040081 int ret = 0;
82 struct bss_descriptor * bss;
Joe Perches0795af52007-10-03 17:59:30 -070083 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020084
Joe Perches0795af52007-10-03 17:59:30 -070085 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %s",
86 print_mac(mac, assoc_req->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020087
88 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +000089 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020090 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -040091 if (bss == NULL) {
Joe Perches0795af52007-10-03 17:59:30 -070092 lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
93 "cannot associate.\n", print_mac(mac, assoc_req->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020094 goto out;
95 }
96
Dan Williamse76850d2007-05-25 17:09:41 -040097 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -040098 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -050099 ret = lbs_associate(priv, assoc_req);
100 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
Dan Williams0dc5a292007-05-10 22:58:02 -0400101 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Holger Schurig10078322007-11-15 18:05:47 -0500102 lbs_join_adhoc_network(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200103 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200104
105out:
Holger Schurig9012b282007-05-25 11:27:16 -0400106 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200107 return ret;
108}
109
110
Holger Schurig69f90322007-11-23 15:43:44 +0100111static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200112 struct assoc_request * assoc_req)
113{
114 int ret = 0, done = 0;
115
Holger Schurig0765af42007-10-15 12:55:56 +0200116 lbs_deb_enter(LBS_DEB_ASSOC);
117
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200118 /* If we're given and 'any' BSSID, try associating based on SSID */
119
120 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -0400121 if (compare_ether_addr(bssid_any, assoc_req->bssid)
122 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200123 ret = assoc_helper_bssid(priv, assoc_req);
124 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200125 }
126 }
127
128 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
129 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200130 }
131
Holger Schurig0765af42007-10-15 12:55:56 +0200132 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200133 return ret;
134}
135
136
Holger Schurig69f90322007-11-23 15:43:44 +0100137static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200138 struct assoc_request * assoc_req)
139{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200140 int ret = 0;
141
Holger Schurig9012b282007-05-25 11:27:16 -0400142 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200143
David Woodhouseaa21c002007-12-08 20:04:36 +0000144 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -0400145 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200146
Dan Williams0dc5a292007-05-10 22:58:02 -0400147 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000148 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -0500149 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +0000150 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200151 }
152
David Woodhouseaa21c002007-12-08 20:04:36 +0000153 priv->mode = assoc_req->mode;
Holger Schurig10078322007-11-15 18:05:47 -0500154 ret = lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -0400155 CMD_802_11_SNMP_MIB,
156 0, CMD_OPTION_WAITFORRSP,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200157 OID_802_11_INFRASTRUCTURE_MODE,
David Woodhouse981f1872007-05-25 23:36:54 -0400158 /* Shoot me now */ (void *) (size_t) assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200159
Holger Schurig9012b282007-05-25 11:27:16 -0400160done:
161 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200162 return ret;
163}
164
165
David Woodhouse9f462572007-12-12 22:50:21 -0500166int lbs_update_channel(struct lbs_private *priv)
Dan Williamsef9a2642007-05-25 16:46:33 -0400167{
Holger Schurig0765af42007-10-15 12:55:56 +0200168 int ret;
Dan Williams2dd4b262007-12-11 16:54:15 -0500169
David Woodhoused1a469f2007-12-16 17:21:00 -0500170 /* the channel in f/w could be out of sync; get the current channel */
Holger Schurig0765af42007-10-15 12:55:56 +0200171 lbs_deb_enter(LBS_DEB_ASSOC);
Dan Williams2dd4b262007-12-11 16:54:15 -0500172
173 ret = lbs_get_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500174 if (ret > 0) {
175 priv->curbssparams.channel = ret;
176 ret = 0;
177 }
Holger Schurig0765af42007-10-15 12:55:56 +0200178 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
179 return ret;
Dan Williamsef9a2642007-05-25 16:46:33 -0400180}
181
Holger Schurig10078322007-11-15 18:05:47 -0500182void lbs_sync_channel(struct work_struct *work)
Luis Carlos Cobo Rusb8bedef2007-05-30 12:14:34 -0400183{
Holger Schurig69f90322007-11-23 15:43:44 +0100184 struct lbs_private *priv = container_of(work, struct lbs_private,
185 sync_channel);
Luis Carlos Cobo Rusb8bedef2007-05-30 12:14:34 -0400186
Holger Schurig0765af42007-10-15 12:55:56 +0200187 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouse9f462572007-12-12 22:50:21 -0500188 if (lbs_update_channel(priv))
Luis Carlos Cobo Rusb8bedef2007-05-30 12:14:34 -0400189 lbs_pr_info("Channel synchronization failed.");
Holger Schurig0765af42007-10-15 12:55:56 +0200190 lbs_deb_leave(LBS_DEB_ASSOC);
Luis Carlos Cobo Rusb8bedef2007-05-30 12:14:34 -0400191}
192
Holger Schurig69f90322007-11-23 15:43:44 +0100193static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -0400194 struct assoc_request * assoc_req)
195{
Dan Williamsef9a2642007-05-25 16:46:33 -0400196 int ret = 0;
197
198 lbs_deb_enter(LBS_DEB_ASSOC);
199
David Woodhouse9f462572007-12-12 22:50:21 -0500200 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500201 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500202 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -0500203 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -0400204 }
205
David Woodhouseaa21c002007-12-08 20:04:36 +0000206 if (assoc_req->channel == priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -0400207 goto done;
208
David Woodhouse8642f1f2007-12-11 20:03:01 -0500209 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -0500210 /* 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);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500214 }
215
Dan Williamsef9a2642007-05-25 16:46:33 -0400216 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
David Woodhouse86062132007-12-13 00:32:36 -0500217 priv->curbssparams.channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -0400218
Dan Williams2dd4b262007-12-11 16:54:15 -0500219 ret = lbs_set_channel(priv, assoc_req->channel);
220 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -0500221 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -0400222
Dan Williams2dd4b262007-12-11 16:54:15 -0500223 /* 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... */
David Woodhouse9f462572007-12-12 22:50:21 -0500226 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500227 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500228 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -0500229 goto done;
230 }
Dan Williamsef9a2642007-05-25 16:46:33 -0400231
David Woodhouseaa21c002007-12-08 20:04:36 +0000232 if (assoc_req->channel != priv->curbssparams.channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -0500233 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -0400234 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500235 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -0400236 }
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 */
David Woodhouse23d36ee2007-12-12 00:14:21 -0500248 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -0400249
David Woodhouse8642f1f2007-12-11 20:03:01 -0500250 restore_mesh:
251 if (priv->mesh_dev)
David Woodhouse86062132007-12-13 00:32:36 -0500252 lbs_mesh_config(priv, 1, priv->curbssparams.channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500253
254 done:
Dan Williamsef9a2642007-05-25 16:46:33 -0400255 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
256 return ret;
257}
258
259
Holger Schurig69f90322007-11-23 15:43:44 +0100260static int assoc_helper_wep_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200261 struct assoc_request * assoc_req)
262{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200263 int i;
264 int ret = 0;
265
Holger Schurig9012b282007-05-25 11:27:16 -0400266 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200267
268 /* Set or remove WEP keys */
269 if ( assoc_req->wep_keys[0].len
270 || assoc_req->wep_keys[1].len
271 || assoc_req->wep_keys[2].len
272 || assoc_req->wep_keys[3].len) {
Holger Schurig10078322007-11-15 18:05:47 -0500273 ret = lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -0400274 CMD_802_11_SET_WEP,
275 CMD_ACT_ADD,
276 CMD_OPTION_WAITFORRSP,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200277 0, assoc_req);
278 } else {
Holger Schurig10078322007-11-15 18:05:47 -0500279 ret = lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -0400280 CMD_802_11_SET_WEP,
281 CMD_ACT_REMOVE,
282 CMD_OPTION_WAITFORRSP,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200283 0, NULL);
284 }
285
286 if (ret)
287 goto out;
288
289 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -0400290 if (assoc_req->secinfo.wep_enabled)
David Woodhouseaa21c002007-12-08 20:04:36 +0000291 priv->currentpacketfilter |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200292 else
David Woodhouseaa21c002007-12-08 20:04:36 +0000293 priv->currentpacketfilter &= ~CMD_ACT_MAC_WEP_ENABLE;
Holger Schurig10078322007-11-15 18:05:47 -0500294 ret = lbs_set_mac_packet_filter(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200295 if (ret)
296 goto out;
297
David Woodhouseaa21c002007-12-08 20:04:36 +0000298 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200299
David Woodhouseaa21c002007-12-08 20:04:36 +0000300 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200301 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000302 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -0400303 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200304 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000305 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200306
David Woodhouseaa21c002007-12-08 20:04:36 +0000307 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200308
309out:
Holger Schurig9012b282007-05-25 11:27:16 -0400310 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200311 return ret;
312}
313
Holger Schurig69f90322007-11-23 15:43:44 +0100314static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200315 struct assoc_request * assoc_req)
316{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200317 int ret = 0;
Dan Williams18c96c342007-06-18 12:01:12 -0400318 u32 do_wpa;
319 u32 rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200320
Holger Schurig9012b282007-05-25 11:27:16 -0400321 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200322
David Woodhouseaa21c002007-12-08 20:04:36 +0000323 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -0500324 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200325
Holger Schurig10078322007-11-15 18:05:47 -0500326 ret = lbs_set_mac_packet_filter(priv);
Dan Williams90a42212007-05-25 23:01:24 -0400327 if (ret)
328 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200329
Dan Williams18c96c342007-06-18 12:01:12 -0400330 /* If RSN is already enabled, don't try to enable it again, since
331 * ENABLE_RSN resets internal state machines and will clobber the
332 * 4-way WPA handshake.
333 */
334
335 /* Get RSN enabled/disabled */
Holger Schurig10078322007-11-15 18:05:47 -0500336 ret = lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -0400337 CMD_802_11_ENABLE_RSN,
338 CMD_ACT_GET,
339 CMD_OPTION_WAITFORRSP,
Dan Williams18c96c342007-06-18 12:01:12 -0400340 0, &rsn);
341 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500342 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -0400343 goto out;
344 }
345
346 /* Don't re-enable RSN if it's already enabled */
347 do_wpa = (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled);
348 if (do_wpa == rsn)
349 goto out;
350
351 /* Set RSN enabled/disabled */
352 rsn = do_wpa;
Holger Schurig10078322007-11-15 18:05:47 -0500353 ret = lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -0400354 CMD_802_11_ENABLE_RSN,
355 CMD_ACT_SET,
356 CMD_OPTION_WAITFORRSP,
Dan Williams18c96c342007-06-18 12:01:12 -0400357 0, &rsn);
Dan Williams90a42212007-05-25 23:01:24 -0400358
359out:
Holger Schurig9012b282007-05-25 11:27:16 -0400360 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200361 return ret;
362}
363
364
Holger Schurig69f90322007-11-23 15:43:44 +0100365static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200366 struct assoc_request * assoc_req)
367{
368 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -0400369 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370
Holger Schurig9012b282007-05-25 11:27:16 -0400371 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200372
Dan Williams2bcde512007-10-03 10:37:45 -0400373 /* Work around older firmware bug where WPA unicast and multicast
374 * keys must be set independently. Seen in SDIO parts with firmware
375 * version 5.0.11p0.
376 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200377
Dan Williams2bcde512007-10-03 10:37:45 -0400378 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
379 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
Holger Schurig10078322007-11-15 18:05:47 -0500380 ret = lbs_prepare_and_send_command(priv,
Dan Williams2bcde512007-10-03 10:37:45 -0400381 CMD_802_11_KEY_MATERIAL,
382 CMD_ACT_SET,
383 CMD_OPTION_WAITFORRSP,
384 0, assoc_req);
385 assoc_req->flags = flags;
386 }
387
388 if (ret)
389 goto out;
390
391 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
392 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
393
Holger Schurig10078322007-11-15 18:05:47 -0500394 ret = lbs_prepare_and_send_command(priv,
Dan Williams2bcde512007-10-03 10:37:45 -0400395 CMD_802_11_KEY_MATERIAL,
396 CMD_ACT_SET,
397 CMD_OPTION_WAITFORRSP,
398 0, assoc_req);
399 assoc_req->flags = flags;
400 }
401
402out:
Holger Schurig9012b282007-05-25 11:27:16 -0400403 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200404 return ret;
405}
406
407
Holger Schurig69f90322007-11-23 15:43:44 +0100408static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200409 struct assoc_request * assoc_req)
410{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200411 int ret = 0;
412
Holger Schurig9012b282007-05-25 11:27:16 -0400413 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200414
415 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000416 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
417 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200418 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +0000419 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
420 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200421 }
422
Holger Schurig9012b282007-05-25 11:27:16 -0400423 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200424 return ret;
425}
426
427
David Woodhouseaa21c002007-12-08 20:04:36 +0000428static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200429 struct assoc_request * assoc_req)
430{
Holger Schurig0765af42007-10-15 12:55:56 +0200431 int ret = 0;
432
433 lbs_deb_enter(LBS_DEB_ASSOC);
434
David Woodhouseaa21c002007-12-08 20:04:36 +0000435 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200436 return 0;
437
438 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +0200439 lbs_deb_assoc("Deauthenticating due to new SSID\n");
440 ret = 1;
441 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200442 }
443
444 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000445 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +0200446 lbs_deb_assoc("Deauthenticating due to new security\n");
447 ret = 1;
448 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200449 }
450 }
451
452 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +0200453 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
454 ret = 1;
455 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456 }
457
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -0400458 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +0200459 lbs_deb_assoc("Deauthenticating due to channel switch\n");
460 ret = 1;
461 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -0400462 }
463
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200464 /* FIXME: deal with 'auto' mode somehow */
465 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +0200466 if (assoc_req->mode != IW_MODE_INFRA) {
467 lbs_deb_assoc("Deauthenticating due to leaving "
468 "infra mode\n");
469 ret = 1;
470 goto out;
471 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200472 }
473
Holger Schurig0765af42007-10-15 12:55:56 +0200474out:
475 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476 return 0;
477}
478
479
David Woodhouseaa21c002007-12-08 20:04:36 +0000480static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200481 struct assoc_request * assoc_req)
482{
Holger Schurig0765af42007-10-15 12:55:56 +0200483 lbs_deb_enter(LBS_DEB_ASSOC);
484
David Woodhouseaa21c002007-12-08 20:04:36 +0000485 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486 return 0;
487
David Woodhouseaa21c002007-12-08 20:04:36 +0000488 if (lbs_ssid_cmp(priv->curbssparams.ssid,
489 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -0400490 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200491 return 1;
492
493 /* FIXME: deal with 'auto' mode somehow */
494 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400495 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200496 return 1;
497 }
498
Dan Williamsef9a2642007-05-25 16:46:33 -0400499 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000500 if (assoc_req->channel != priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -0400501 return 1;
502 }
503
Holger Schurig0765af42007-10-15 12:55:56 +0200504 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200505 return 0;
506}
507
508
Holger Schurig10078322007-11-15 18:05:47 -0500509void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510{
Holger Schurig69f90322007-11-23 15:43:44 +0100511 struct lbs_private *priv = container_of(work, struct lbs_private,
512 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513 struct assoc_request * assoc_req = NULL;
514 int ret = 0;
515 int find_any_ssid = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700516 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200517
Holger Schurig9012b282007-05-25 11:27:16 -0400518 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200519
David Woodhouseaa21c002007-12-08 20:04:36 +0000520 mutex_lock(&priv->lock);
521 assoc_req = priv->pending_assoc_req;
522 priv->pending_assoc_req = NULL;
523 priv->in_progress_assoc_req = assoc_req;
524 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200525
Holger Schurig9012b282007-05-25 11:27:16 -0400526 if (!assoc_req)
527 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200528
Holger Schurig0765af42007-10-15 12:55:56 +0200529 lbs_deb_assoc(
530 "Association Request:\n"
531 " flags: 0x%08lx\n"
532 " SSID: '%s'\n"
533 " chann: %d\n"
534 " band: %d\n"
535 " mode: %d\n"
536 " BSSID: %s\n"
537 " secinfo: %s%s%s\n"
538 " auth_mode: %d\n",
539 assoc_req->flags,
540 escape_essid(assoc_req->ssid, assoc_req->ssid_len),
541 assoc_req->channel, assoc_req->band, assoc_req->mode,
542 print_mac(mac, assoc_req->bssid),
543 assoc_req->secinfo.WPAenabled ? " WPA" : "",
544 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
545 assoc_req->secinfo.wep_enabled ? " WEP" : "",
546 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200547
548 /* If 'any' SSID was specified, find an SSID to associate with */
549 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -0400550 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200551 find_any_ssid = 1;
552
553 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
554 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -0400555 if (compare_ether_addr(assoc_req->bssid, bssid_any)
556 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200557 find_any_ssid = 0;
558 }
559
560 if (find_any_ssid) {
Dan Williams0dc5a292007-05-10 22:58:02 -0400561 u8 new_mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200562
Holger Schurig10078322007-11-15 18:05:47 -0500563 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400564 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200565 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400566 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200567 ret = -ENETUNREACH;
568 goto out;
569 }
570
571 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -0400572 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200573 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
574 assoc_req->mode = new_mode;
575 }
576 }
577
578 /*
579 * Check if the attributes being changing require deauthentication
580 * from the currently associated infrastructure access point.
581 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000582 if (priv->mode == IW_MODE_INFRA) {
583 if (should_deauth_infrastructure(priv, assoc_req)) {
Holger Schurig10078322007-11-15 18:05:47 -0500584 ret = lbs_send_deauthentication(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200585 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400586 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200587 "configuration request failed: %d\n",
588 ret);
589 }
590 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000591 } else if (priv->mode == IW_MODE_ADHOC) {
592 if (should_stop_adhoc(priv, assoc_req)) {
Holger Schurig10078322007-11-15 18:05:47 -0500593 ret = lbs_stop_adhoc_network(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200594 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400595 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200596 "new configuration request failed: %d\n",
597 ret);
598 }
599
600 }
601 }
602
603 /* Send the various configuration bits to the firmware */
604 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
605 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +0200606 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200607 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200608 }
609
Dan Williamsef9a2642007-05-25 16:46:33 -0400610 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
611 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +0200612 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -0400613 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -0400614 }
615
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200616 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
617 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
618 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +0200619 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200620 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200621 }
622
623 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
624 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +0200625 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200627 }
628
629 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
630 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +0200631 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200632 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200633 }
634
635 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
636 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
637 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +0200638 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200639 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200640 }
641
642 /* SSID/BSSID should be the _last_ config option set, because they
643 * trigger the association attempt.
644 */
645 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
646 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
647 int success = 1;
648
649 ret = assoc_helper_associate(priv, assoc_req);
650 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +0100651 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200652 ret);
653 success = 0;
654 }
655
David Woodhouseaa21c002007-12-08 20:04:36 +0000656 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +0100657 lbs_deb_assoc("ASSOC: association unsuccessful, "
658 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200659 success = 0;
660 }
661
662 if (success) {
Holger Schurig91843462007-11-28 14:05:02 +0100663 lbs_deb_assoc("ASSOC: associated to '%s', %s\n",
David Woodhouseaa21c002007-12-08 20:04:36 +0000664 escape_essid(priv->curbssparams.ssid,
665 priv->curbssparams.ssid_len),
666 print_mac(mac, priv->curbssparams.bssid));
Holger Schurig10078322007-11-15 18:05:47 -0500667 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -0400668 CMD_802_11_RSSI,
669 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200670
Holger Schurig10078322007-11-15 18:05:47 -0500671 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -0400672 CMD_802_11_GET_LOG,
673 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200674 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200675 ret = -1;
676 }
677 }
678
679out:
680 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -0400681 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200682 ret);
683 }
Dan Williamse76850d2007-05-25 17:09:41 -0400684
David Woodhouseaa21c002007-12-08 20:04:36 +0000685 mutex_lock(&priv->lock);
686 priv->in_progress_assoc_req = NULL;
687 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200688 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -0400689
690done:
691 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200692}
693
694
695/*
696 * Caller MUST hold any necessary locks
697 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000698struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699{
700 struct assoc_request * assoc_req;
701
Holger Schurig0765af42007-10-15 12:55:56 +0200702 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +0000703 if (!priv->pending_assoc_req) {
704 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -0400705 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +0000706 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200707 lbs_pr_info("Not enough memory to allocate association"
708 " request!\n");
709 return NULL;
710 }
711 }
712
713 /* Copy current configuration attributes to the association request,
714 * but don't overwrite any that are already set.
715 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000716 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200717 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000718 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400719 IW_ESSID_MAX_SIZE);
David Woodhouseaa21c002007-12-08 20:04:36 +0000720 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200721 }
722
723 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +0000724 assoc_req->channel = priv->curbssparams.channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200725
Dan Williamse76850d2007-05-25 17:09:41 -0400726 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +0000727 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -0400728
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +0000730 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200731
732 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000733 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200734 ETH_ALEN);
735 }
736
737 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
738 int i;
739 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000740 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -0400741 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200742 }
743 }
744
745 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +0000746 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200747
748 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000749 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -0400750 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200751 }
752
753 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000754 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -0400755 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756 }
757
758 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000759 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -0500760 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200761 }
762
763 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000764 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200765 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +0000766 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200767 }
768
Holger Schurig0765af42007-10-15 12:55:56 +0200769 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200770 return assoc_req;
771}