blob: 3492e89d1dd525ce24e14a79eb72afb2ec82571d [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/* Copyright (C) 2006, Red Hat, Inc. */
2
John W. Linville7e272fc2008-09-24 18:13:14 -04003#include <linux/types.h>
Dan Williams3cf20932007-05-25 17:28:30 -04004#include <linux/etherdevice.h>
John W. Linville7e272fc2008-09-24 18:13:14 -04005#include <net/lib80211.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02006
7#include "assoc.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02008#include "decl.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02009#include "host.h"
Holger Schurig245bf202008-04-02 16:27:42 +020010#include "scan.h"
Dan Williams2dd4b262007-12-11 16:54:15 -050011#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020012
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040013static int lbs_adhoc_post(struct lbs_private *priv, struct cmd_header *resp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014
Ihar Hrachyshka5a6e0432008-01-25 14:15:00 +010015static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
16 { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
17static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
18 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020019
Holger Schurig697900a2008-04-02 16:27:10 +020020/* The firmware needs certain bits masked out of the beacon-derviced capability
21 * field when associating/joining to BSSs.
22 */
23#define CAPINFO_MASK (~(0xda00))
24
25
Dan Williamsf5fe1fd2008-08-21 21:46:59 -040026/**
27 * @brief This function finds common rates between rates and card rates.
28 *
29 * It will fill common rates in rates as output if found.
30 *
31 * NOTE: Setting the MSB of the basic rates need to be taken
32 * care, either before or after calling this function
33 *
34 * @param priv A pointer to struct lbs_private structure
35 * @param rates the buffer which keeps input and output
36 * @param rates_size the size of rate1 buffer; new size of buffer on return
37 *
38 * @return 0 on success, or -1 on error
39 */
40static int get_common_rates(struct lbs_private *priv,
41 u8 *rates,
42 u16 *rates_size)
43{
44 u8 *card_rates = lbs_bg_rates;
45 size_t num_card_rates = sizeof(lbs_bg_rates);
46 int ret = 0, i, j;
47 u8 tmp[30];
48 size_t tmp_size = 0;
49
50 /* For each rate in card_rates that exists in rate1, copy to tmp */
51 for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
52 for (j = 0; rates[j] && (j < *rates_size); j++) {
53 if (rates[j] == card_rates[i])
54 tmp[tmp_size++] = card_rates[i];
55 }
56 }
57
58 lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
59 lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
60 lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
61 lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
62
63 if (!priv->enablehwauto) {
64 for (i = 0; i < tmp_size; i++) {
65 if (tmp[i] == priv->cur_rate)
66 goto done;
67 }
68 lbs_pr_alert("Previously set fixed data rate %#x isn't "
69 "compatible with the network.\n", priv->cur_rate);
70 ret = -1;
71 goto done;
72 }
73 ret = 0;
74
75done:
76 memset(rates, 0, *rates_size);
77 *rates_size = min_t(int, tmp_size, *rates_size);
78 memcpy(rates, tmp, *rates_size);
79 return ret;
80}
81
82
83/**
84 * @brief Sets the MSB on basic rates as the firmware requires
85 *
86 * Scan through an array and set the MSB for basic data rates.
87 *
88 * @param rates buffer of data rates
89 * @param len size of buffer
90 */
91static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
92{
93 int i;
94
95 for (i = 0; i < len; i++) {
96 if (rates[i] == 0x02 || rates[i] == 0x04 ||
97 rates[i] == 0x0b || rates[i] == 0x16)
98 rates[i] |= 0x80;
99 }
100}
101
Holger Schurig697900a2008-04-02 16:27:10 +0200102
103/**
104 * @brief Associate to a specific BSS discovered in a scan
105 *
106 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400107 * @param assoc_req The association request describing the BSS to associate with
Holger Schurig697900a2008-04-02 16:27:10 +0200108 *
109 * @return 0-success, otherwise fail
110 */
111static int lbs_associate(struct lbs_private *priv,
112 struct assoc_request *assoc_req)
113{
114 int ret;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400115 u8 preamble = RADIO_PREAMBLE_LONG;
Holger Schurig697900a2008-04-02 16:27:10 +0200116
117 lbs_deb_enter(LBS_DEB_ASSOC);
118
119 ret = lbs_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
120 0, CMD_OPTION_WAITFORRSP,
121 0, assoc_req->bss.bssid);
Holger Schurig697900a2008-04-02 16:27:10 +0200122 if (ret)
Dan Williamsd5db2df2008-08-21 17:51:07 -0400123 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200124
Dan Williamsd5db2df2008-08-21 17:51:07 -0400125 /* Use short preamble only when both the BSS and firmware support it */
Holger Schurig697900a2008-04-02 16:27:10 +0200126 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
127 (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
Dan Williamsd5db2df2008-08-21 17:51:07 -0400128 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200129
Dan Williamsd5db2df2008-08-21 17:51:07 -0400130 ret = lbs_set_radio(priv, preamble, 1);
131 if (ret)
132 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200133
134 ret = lbs_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
135 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
136
Dan Williamsd5db2df2008-08-21 17:51:07 -0400137out:
Holger Schurig697900a2008-04-02 16:27:10 +0200138 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
139 return ret;
140}
141
142/**
143 * @brief Join an adhoc network found in a previous scan
144 *
145 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400146 * @param assoc_req The association request describing the BSS to join
Holger Schurig697900a2008-04-02 16:27:10 +0200147 *
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400148 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200149 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400150static int lbs_adhoc_join(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200151 struct assoc_request *assoc_req)
152{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400153 struct cmd_ds_802_11_ad_hoc_join cmd;
Holger Schurig697900a2008-04-02 16:27:10 +0200154 struct bss_descriptor *bss = &assoc_req->bss;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400155 u8 preamble = RADIO_PREAMBLE_LONG;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400156 u16 ratesize = 0;
157 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400158
159 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200160
161 lbs_deb_join("current SSID '%s', ssid length %u\n",
John W. Linville7e272fc2008-09-24 18:13:14 -0400162 escape_ssid(priv->curbssparams.ssid,
Holger Schurig697900a2008-04-02 16:27:10 +0200163 priv->curbssparams.ssid_len),
164 priv->curbssparams.ssid_len);
165 lbs_deb_join("requested ssid '%s', ssid length %u\n",
John W. Linville7e272fc2008-09-24 18:13:14 -0400166 escape_ssid(bss->ssid, bss->ssid_len),
Holger Schurig697900a2008-04-02 16:27:10 +0200167 bss->ssid_len);
168
169 /* check if the requested SSID is already joined */
170 if (priv->curbssparams.ssid_len &&
171 !lbs_ssid_cmp(priv->curbssparams.ssid,
172 priv->curbssparams.ssid_len,
173 bss->ssid, bss->ssid_len) &&
174 (priv->mode == IW_MODE_ADHOC) &&
175 (priv->connect_status == LBS_CONNECTED)) {
176 union iwreq_data wrqu;
177
178 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
179 "current, not attempting to re-join");
180
181 /* Send the re-association event though, because the association
182 * request really was successful, even if just a null-op.
183 */
184 memset(&wrqu, 0, sizeof(wrqu));
185 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
186 ETH_ALEN);
187 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
188 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
189 goto out;
190 }
191
Dan Williamsd5db2df2008-08-21 17:51:07 -0400192 /* Use short preamble only when both the BSS and firmware support it */
193 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
194 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig697900a2008-04-02 16:27:10 +0200195 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400196 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200197 }
198
Dan Williamsd5db2df2008-08-21 17:51:07 -0400199 ret = lbs_set_radio(priv, preamble, 1);
200 if (ret)
201 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200202
203 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
204 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
205
206 priv->adhoccreate = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400207 priv->curbssparams.channel = bss->channel;
Holger Schurig697900a2008-04-02 16:27:10 +0200208
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400209 /* Build the join command */
210 memset(&cmd, 0, sizeof(cmd));
211 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
212
213 cmd.bss.type = CMD_BSS_TYPE_IBSS;
214 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
215
216 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
217 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
218
219 memcpy(&cmd.bss.phyparamset, &bss->phyparamset,
220 sizeof(union ieeetypes_phyparamset));
221
222 memcpy(&cmd.bss.ssparamset, &bss->ssparamset,
223 sizeof(union IEEEtypes_ssparamset));
224
225 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
226 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
227 bss->capability, CAPINFO_MASK);
228
229 /* information on BSSID descriptor passed to FW */
Johannes Berge1749612008-10-27 15:59:26 -0700230 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
231 cmd.bss.bssid, cmd.bss.ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400232
233 /* Only v8 and below support setting these */
234 if (priv->fwrelease < 0x09000000) {
235 /* failtimeout */
236 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
237 /* probedelay */
238 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
239 }
240
241 /* Copy Data rates from the rates recorded in scan response */
242 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
243 ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
244 memcpy(cmd.bss.rates, bss->rates, ratesize);
245 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
246 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
247 ret = -1;
248 goto out;
249 }
250
251 /* Copy the ad-hoc creation rates into Current BSS state structure */
252 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
253 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
254
255 /* Set MSB on basic rates as the firmware requires, but _after_
256 * copying to current bss rates.
257 */
258 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
259
260 cmd.bss.ssparamset.ibssparamset.atimwindow = cpu_to_le16(bss->atimwindow);
261
262 if (assoc_req->secinfo.wep_enabled) {
263 u16 tmp = le16_to_cpu(cmd.bss.capability);
264 tmp |= WLAN_CAPABILITY_PRIVACY;
265 cmd.bss.capability = cpu_to_le16(tmp);
266 }
267
268 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
269 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
270
271 /* wake up first */
272 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
273 CMD_ACT_SET, 0, 0,
274 &local_ps_mode);
275 if (ret) {
276 ret = -1;
277 goto out;
278 }
279 }
280
281 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
282 ret = -1;
283 goto out;
284 }
285
286 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
287 if (ret == 0)
288 ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200289
290out:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400291 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200292 return ret;
293}
294
295/**
296 * @brief Start an Adhoc Network
297 *
298 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400299 * @param assoc_req The association request describing the BSS to start
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400300 *
301 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200302 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400303static int lbs_adhoc_start(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200304 struct assoc_request *assoc_req)
305{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400306 struct cmd_ds_802_11_ad_hoc_start cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400307 u8 preamble = RADIO_PREAMBLE_LONG;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400308 size_t ratesize = 0;
309 u16 tmpcap = 0;
310 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400311
312 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200313
Holger Schurig697900a2008-04-02 16:27:10 +0200314 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400315 lbs_deb_join("ADHOC_START: Will use short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400316 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200317 }
318
Dan Williamsd5db2df2008-08-21 17:51:07 -0400319 ret = lbs_set_radio(priv, preamble, 1);
320 if (ret)
321 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200322
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400323 /* Build the start command */
324 memset(&cmd, 0, sizeof(cmd));
325 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurig697900a2008-04-02 16:27:10 +0200326
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400327 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
328
329 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
John W. Linville7e272fc2008-09-24 18:13:14 -0400330 escape_ssid(assoc_req->ssid, assoc_req->ssid_len),
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400331 assoc_req->ssid_len);
332
333 cmd.bsstype = CMD_BSS_TYPE_IBSS;
334
335 if (priv->beacon_period == 0)
336 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
337 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
338
339 WARN_ON(!assoc_req->channel);
340
341 /* set Physical parameter set */
342 cmd.phyparamset.dsparamset.elementid = MFIE_TYPE_DS_SET;
343 cmd.phyparamset.dsparamset.len = 1;
344 cmd.phyparamset.dsparamset.currentchan = assoc_req->channel;
345
346 /* set IBSS parameter set */
347 cmd.ssparamset.ibssparamset.elementid = MFIE_TYPE_IBSS_SET;
348 cmd.ssparamset.ibssparamset.len = 2;
349 cmd.ssparamset.ibssparamset.atimwindow = 0;
350
351 /* set capability info */
352 tmpcap = WLAN_CAPABILITY_IBSS;
353 if (assoc_req->secinfo.wep_enabled) {
354 lbs_deb_join("ADHOC_START: WEP enabled, setting privacy on\n");
355 tmpcap |= WLAN_CAPABILITY_PRIVACY;
356 } else
357 lbs_deb_join("ADHOC_START: WEP disabled, setting privacy off\n");
358
359 cmd.capability = cpu_to_le16(tmpcap);
360
361 /* Only v8 and below support setting probe delay */
362 if (priv->fwrelease < 0x09000000)
363 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
364
365 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
366 memcpy(cmd.rates, lbs_bg_rates, ratesize);
367
368 /* Copy the ad-hoc creating rates into Current BSS state structure */
369 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
370 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
371
372 /* Set MSB on basic rates as the firmware requires, but _after_
373 * copying to current bss rates.
374 */
375 lbs_set_basic_rate_flags(cmd.rates, ratesize);
376
377 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
378 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
379
380 if (lbs_create_dnld_countryinfo_11d(priv)) {
381 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
382 ret = -1;
383 goto out;
384 }
385
386 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
387 assoc_req->channel, assoc_req->band);
388
389 priv->adhoccreate = 1;
390 priv->mode = IW_MODE_ADHOC;
391
392 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
393 if (ret == 0)
394 ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200395
Dan Williamsd5db2df2008-08-21 17:51:07 -0400396out:
397 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200398 return ret;
399}
400
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400401/**
402 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
403 *
404 * @param priv A pointer to struct lbs_private structure
405 * @return 0 on success, or an error
406 */
407int lbs_adhoc_stop(struct lbs_private *priv)
Holger Schurig697900a2008-04-02 16:27:10 +0200408{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400409 struct cmd_ds_802_11_ad_hoc_stop cmd;
410 int ret;
411
412 lbs_deb_enter(LBS_DEB_JOIN);
413
414 memset(&cmd, 0, sizeof (cmd));
415 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
416
417 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
418
419 /* Clean up everything even if there was an error */
420 lbs_mac_event_disconnected(priv);
421
422 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
423 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +0200424}
Dan Williamse76850d2007-05-25 17:09:41 -0400425
Holger Schurig245bf202008-04-02 16:27:42 +0200426static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
427 struct bss_descriptor *match_bss)
428{
429 if (!secinfo->wep_enabled && !secinfo->WPAenabled
430 && !secinfo->WPA2enabled
431 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
432 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
433 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
434 return 1;
435 else
436 return 0;
437}
438
439static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
440 struct bss_descriptor *match_bss)
441{
442 if (secinfo->wep_enabled && !secinfo->WPAenabled
443 && !secinfo->WPA2enabled
444 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
445 return 1;
446 else
447 return 0;
448}
449
450static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
451 struct bss_descriptor *match_bss)
452{
453 if (!secinfo->wep_enabled && secinfo->WPAenabled
454 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
455 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
456 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
457 )
458 return 1;
459 else
460 return 0;
461}
462
463static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
464 struct bss_descriptor *match_bss)
465{
466 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
467 (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
468 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
469 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
470 )
471 return 1;
472 else
473 return 0;
474}
475
476static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
477 struct bss_descriptor *match_bss)
478{
479 if (!secinfo->wep_enabled && !secinfo->WPAenabled
480 && !secinfo->WPA2enabled
481 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
482 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
483 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
484 return 1;
485 else
486 return 0;
487}
488
489/**
490 * @brief Check if a scanned network compatible with the driver settings
491 *
492 * WEP WPA WPA2 ad-hoc encrypt Network
493 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
494 * 0 0 0 0 NONE 0 0 0 yes No security
495 * 1 0 0 0 NONE 1 0 0 yes Static WEP
496 * 0 1 0 0 x 1x 1 x yes WPA
497 * 0 0 1 0 x 1x x 1 yes WPA2
498 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
499 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
500 *
501 *
502 * @param priv A pointer to struct lbs_private
503 * @param index Index in scantable to check against current driver settings
504 * @param mode Network mode: Infrastructure or IBSS
505 *
506 * @return Index in scantable, or error code if negative
507 */
508static int is_network_compatible(struct lbs_private *priv,
509 struct bss_descriptor *bss, uint8_t mode)
510{
511 int matched = 0;
512
513 lbs_deb_enter(LBS_DEB_SCAN);
514
515 if (bss->mode != mode)
516 goto done;
517
518 matched = match_bss_no_security(&priv->secinfo, bss);
519 if (matched)
520 goto done;
521 matched = match_bss_static_wep(&priv->secinfo, bss);
522 if (matched)
523 goto done;
524 matched = match_bss_wpa(&priv->secinfo, bss);
525 if (matched) {
526 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
527 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
528 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
529 priv->secinfo.wep_enabled ? "e" : "d",
530 priv->secinfo.WPAenabled ? "e" : "d",
531 priv->secinfo.WPA2enabled ? "e" : "d",
532 (bss->capability & WLAN_CAPABILITY_PRIVACY));
533 goto done;
534 }
535 matched = match_bss_wpa2(&priv->secinfo, bss);
536 if (matched) {
537 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
538 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
539 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
540 priv->secinfo.wep_enabled ? "e" : "d",
541 priv->secinfo.WPAenabled ? "e" : "d",
542 priv->secinfo.WPA2enabled ? "e" : "d",
543 (bss->capability & WLAN_CAPABILITY_PRIVACY));
544 goto done;
545 }
546 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
547 if (matched) {
548 lbs_deb_scan("is_network_compatible() dynamic WEP: "
549 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
550 bss->wpa_ie[0], bss->rsn_ie[0],
551 (bss->capability & WLAN_CAPABILITY_PRIVACY));
552 goto done;
553 }
554
555 /* bss security settings don't match those configured on card */
556 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
557 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
558 bss->wpa_ie[0], bss->rsn_ie[0],
559 priv->secinfo.wep_enabled ? "e" : "d",
560 priv->secinfo.WPAenabled ? "e" : "d",
561 priv->secinfo.WPA2enabled ? "e" : "d",
562 (bss->capability & WLAN_CAPABILITY_PRIVACY));
563
564done:
565 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
566 return matched;
567}
568
569/**
570 * @brief This function finds a specific compatible BSSID in the scan list
571 *
572 * Used in association code
573 *
574 * @param priv A pointer to struct lbs_private
575 * @param bssid BSSID to find in the scan list
576 * @param mode Network mode: Infrastructure or IBSS
577 *
578 * @return index in BSSID list, or error return code (< 0)
579 */
580static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
581 uint8_t *bssid, uint8_t mode)
582{
583 struct bss_descriptor *iter_bss;
584 struct bss_descriptor *found_bss = NULL;
585
586 lbs_deb_enter(LBS_DEB_SCAN);
587
588 if (!bssid)
589 goto out;
590
591 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
592
593 /* Look through the scan table for a compatible match. The loop will
594 * continue past a matched bssid that is not compatible in case there
595 * is an AP with multiple SSIDs assigned to the same BSSID
596 */
597 mutex_lock(&priv->lock);
598 list_for_each_entry(iter_bss, &priv->network_list, list) {
599 if (compare_ether_addr(iter_bss->bssid, bssid))
600 continue; /* bssid doesn't match */
601 switch (mode) {
602 case IW_MODE_INFRA:
603 case IW_MODE_ADHOC:
604 if (!is_network_compatible(priv, iter_bss, mode))
605 break;
606 found_bss = iter_bss;
607 break;
608 default:
609 found_bss = iter_bss;
610 break;
611 }
612 }
613 mutex_unlock(&priv->lock);
614
615out:
616 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
617 return found_bss;
618}
619
620/**
621 * @brief This function finds ssid in ssid list.
622 *
623 * Used in association code
624 *
625 * @param priv A pointer to struct lbs_private
626 * @param ssid SSID to find in the list
627 * @param bssid BSSID to qualify the SSID selection (if provided)
628 * @param mode Network mode: Infrastructure or IBSS
629 *
630 * @return index in BSSID list
631 */
632static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
633 uint8_t *ssid, uint8_t ssid_len,
634 uint8_t *bssid, uint8_t mode,
635 int channel)
636{
637 u32 bestrssi = 0;
638 struct bss_descriptor *iter_bss = NULL;
639 struct bss_descriptor *found_bss = NULL;
640 struct bss_descriptor *tmp_oldest = NULL;
641
642 lbs_deb_enter(LBS_DEB_SCAN);
643
644 mutex_lock(&priv->lock);
645
646 list_for_each_entry(iter_bss, &priv->network_list, list) {
647 if (!tmp_oldest ||
648 (iter_bss->last_scanned < tmp_oldest->last_scanned))
649 tmp_oldest = iter_bss;
650
651 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
652 ssid, ssid_len) != 0)
653 continue; /* ssid doesn't match */
654 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
655 continue; /* bssid doesn't match */
656 if ((channel > 0) && (iter_bss->channel != channel))
657 continue; /* channel doesn't match */
658
659 switch (mode) {
660 case IW_MODE_INFRA:
661 case IW_MODE_ADHOC:
662 if (!is_network_compatible(priv, iter_bss, mode))
663 break;
664
665 if (bssid) {
666 /* Found requested BSSID */
667 found_bss = iter_bss;
668 goto out;
669 }
670
671 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
672 bestrssi = SCAN_RSSI(iter_bss->rssi);
673 found_bss = iter_bss;
674 }
675 break;
676 case IW_MODE_AUTO:
677 default:
678 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
679 bestrssi = SCAN_RSSI(iter_bss->rssi);
680 found_bss = iter_bss;
681 }
682 break;
683 }
684 }
685
686out:
687 mutex_unlock(&priv->lock);
688 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
689 return found_bss;
690}
691
Holger Schurig69f90322007-11-23 15:43:44 +0100692static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200693 struct assoc_request * assoc_req)
694{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200695 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400696 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400697 int channel = -1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200698
Holger Schurig9012b282007-05-25 11:27:16 -0400699 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200700
Dan Williamsef9a2642007-05-25 16:46:33 -0400701 /* FIXME: take channel into account when picking SSIDs if a channel
702 * is set.
703 */
704
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400705 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
706 channel = assoc_req->channel;
707
Holger Schurig0765af42007-10-15 12:55:56 +0200708 lbs_deb_assoc("SSID '%s' requested\n",
John W. Linville7e272fc2008-09-24 18:13:14 -0400709 escape_ssid(assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -0400710 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -0500711 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100712 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200713
David Woodhouseaa21c002007-12-08 20:04:36 +0000714 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400715 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400716 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -0400717 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Holger Schurig10078322007-11-15 18:05:47 -0500718 ret = lbs_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -0400720 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200721 }
Dan Williams0dc5a292007-05-10 22:58:02 -0400722 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200723 /* Scan for the network, do not save previous results. Stale
724 * scan data will cause us to join a non-existant adhoc network
725 */
Holger Schurig10078322007-11-15 18:05:47 -0500726 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100727 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200728
729 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000730 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400731 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400732 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -0400733 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400734 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400735 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200736 } else {
737 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -0400738 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400739 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400740 IW_ESSID_MAX_SIZE);
741 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400742 lbs_adhoc_start(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200743 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200744 }
745
Holger Schurig9012b282007-05-25 11:27:16 -0400746 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200747 return ret;
748}
749
750
Holger Schurig69f90322007-11-23 15:43:44 +0100751static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200752 struct assoc_request * assoc_req)
753{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400754 int ret = 0;
755 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756
Johannes Berge1749612008-10-27 15:59:26 -0700757 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200758
759 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +0000760 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200761 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400762 if (bss == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -0700763 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
764 "cannot associate.\n", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200765 goto out;
766 }
767
Dan Williamse76850d2007-05-25 17:09:41 -0400768 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -0400769 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -0500770 ret = lbs_associate(priv, assoc_req);
771 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
Dan Williams0dc5a292007-05-10 22:58:02 -0400772 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400773 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200774 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200775
776out:
Holger Schurig9012b282007-05-25 11:27:16 -0400777 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200778 return ret;
779}
780
781
Holger Schurig69f90322007-11-23 15:43:44 +0100782static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200783 struct assoc_request * assoc_req)
784{
785 int ret = 0, done = 0;
786
Holger Schurig0765af42007-10-15 12:55:56 +0200787 lbs_deb_enter(LBS_DEB_ASSOC);
788
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200789 /* If we're given and 'any' BSSID, try associating based on SSID */
790
791 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -0400792 if (compare_ether_addr(bssid_any, assoc_req->bssid)
793 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200794 ret = assoc_helper_bssid(priv, assoc_req);
795 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200796 }
797 }
798
799 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
800 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200801 }
802
Holger Schurig0765af42007-10-15 12:55:56 +0200803 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200804 return ret;
805}
806
807
Holger Schurig69f90322007-11-23 15:43:44 +0100808static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200809 struct assoc_request * assoc_req)
810{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200811 int ret = 0;
812
Holger Schurig9012b282007-05-25 11:27:16 -0400813 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200814
David Woodhouseaa21c002007-12-08 20:04:36 +0000815 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -0400816 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200817
Dan Williams0dc5a292007-05-10 22:58:02 -0400818 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000819 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -0500820 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +0000821 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200822 }
823
David Woodhouseaa21c002007-12-08 20:04:36 +0000824 priv->mode = assoc_req->mode;
Dan Williams39fcf7a2008-09-10 12:49:00 -0400825 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200826
Holger Schurig9012b282007-05-25 11:27:16 -0400827done:
828 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200829 return ret;
830}
831
Holger Schurig69f90322007-11-23 15:43:44 +0100832static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -0400833 struct assoc_request * assoc_req)
834{
Dan Williamsef9a2642007-05-25 16:46:33 -0400835 int ret = 0;
836
837 lbs_deb_enter(LBS_DEB_ASSOC);
838
David Woodhouse9f462572007-12-12 22:50:21 -0500839 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500840 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500841 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -0500842 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -0400843 }
844
David Woodhouseaa21c002007-12-08 20:04:36 +0000845 if (assoc_req->channel == priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -0400846 goto done;
847
David Woodhouse8642f1f2007-12-11 20:03:01 -0500848 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -0500849 /* Change mesh channel first; 21.p21 firmware won't let
850 you change channel otherwise (even though it'll return
851 an error to this */
Javier Cardonaedaea5c2008-05-17 00:55:10 -0700852 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
853 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500854 }
855
Dan Williamsef9a2642007-05-25 16:46:33 -0400856 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
David Woodhouse86062132007-12-13 00:32:36 -0500857 priv->curbssparams.channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -0400858
Dan Williams2dd4b262007-12-11 16:54:15 -0500859 ret = lbs_set_channel(priv, assoc_req->channel);
860 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -0500861 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -0400862
Dan Williams2dd4b262007-12-11 16:54:15 -0500863 /* FIXME: shouldn't need to grab the channel _again_ after setting
864 * it since the firmware is supposed to return the new channel, but
865 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -0500866 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500867 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500868 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -0500869 goto done;
870 }
Dan Williamsef9a2642007-05-25 16:46:33 -0400871
David Woodhouseaa21c002007-12-08 20:04:36 +0000872 if (assoc_req->channel != priv->curbssparams.channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -0500873 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -0400874 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500875 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -0400876 }
877
878 if ( assoc_req->secinfo.wep_enabled
879 && (assoc_req->wep_keys[0].len
880 || assoc_req->wep_keys[1].len
881 || assoc_req->wep_keys[2].len
882 || assoc_req->wep_keys[3].len)) {
883 /* Make sure WEP keys are re-sent to firmware */
884 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
885 }
886
887 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -0500888 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -0400889
David Woodhouse8642f1f2007-12-11 20:03:01 -0500890 restore_mesh:
891 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -0700892 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
893 priv->curbssparams.channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500894
895 done:
Dan Williamsef9a2642007-05-25 16:46:33 -0400896 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
897 return ret;
898}
899
900
Holger Schurig69f90322007-11-23 15:43:44 +0100901static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -0500902 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200903{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200904 int i;
905 int ret = 0;
906
Holger Schurig9012b282007-05-25 11:27:16 -0400907 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200908
909 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -0500910 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
911 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
912 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
913 else
914 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200915
916 if (ret)
917 goto out;
918
919 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -0400920 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +0100921 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200922 else
Holger Schurigd9e97782008-03-12 16:06:43 +0100923 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -0500924
Holger Schurigc97329e2008-03-18 11:20:21 +0100925 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200926
David Woodhouseaa21c002007-12-08 20:04:36 +0000927 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200928
David Woodhouseaa21c002007-12-08 20:04:36 +0000929 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200930 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000931 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -0500932 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200933 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000934 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200935
David Woodhouseaa21c002007-12-08 20:04:36 +0000936 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200937
938out:
Holger Schurig9012b282007-05-25 11:27:16 -0400939 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200940 return ret;
941}
942
Holger Schurig69f90322007-11-23 15:43:44 +0100943static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200944 struct assoc_request * assoc_req)
945{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200946 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -0500947 uint16_t do_wpa;
948 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200949
Holger Schurig9012b282007-05-25 11:27:16 -0400950 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200951
David Woodhouseaa21c002007-12-08 20:04:36 +0000952 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -0500953 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200954
Holger Schurigc97329e2008-03-18 11:20:21 +0100955 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200956
Dan Williams18c96c342007-06-18 12:01:12 -0400957 /* If RSN is already enabled, don't try to enable it again, since
958 * ENABLE_RSN resets internal state machines and will clobber the
959 * 4-way WPA handshake.
960 */
961
962 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -0500963 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -0400964 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500965 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -0400966 goto out;
967 }
968
969 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -0500970 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -0400971 if (do_wpa == rsn)
972 goto out;
973
974 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -0500975 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -0400976
977out:
Holger Schurig9012b282007-05-25 11:27:16 -0400978 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200979 return ret;
980}
981
982
Holger Schurig69f90322007-11-23 15:43:44 +0100983static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200984 struct assoc_request * assoc_req)
985{
986 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -0400987 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200988
Holger Schurig9012b282007-05-25 11:27:16 -0400989 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200990
Dan Williams2bcde512007-10-03 10:37:45 -0400991 /* Work around older firmware bug where WPA unicast and multicast
992 * keys must be set independently. Seen in SDIO parts with firmware
993 * version 5.0.11p0.
994 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200995
Dan Williams2bcde512007-10-03 10:37:45 -0400996 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
997 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +0100998 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -0400999 assoc_req->flags = flags;
1000 }
1001
1002 if (ret)
1003 goto out;
1004
1005 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1006 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1007
David Woodhouse9e1228d2008-03-03 12:15:39 +01001008 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001009 assoc_req->flags = flags;
1010 }
1011
1012out:
Holger Schurig9012b282007-05-25 11:27:16 -04001013 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001014 return ret;
1015}
1016
1017
Holger Schurig69f90322007-11-23 15:43:44 +01001018static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001019 struct assoc_request * assoc_req)
1020{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001021 int ret = 0;
1022
Holger Schurig9012b282007-05-25 11:27:16 -04001023 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001024
1025 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001026 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1027 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001028 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +00001029 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1030 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001031 }
1032
Holger Schurig9012b282007-05-25 11:27:16 -04001033 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001034 return ret;
1035}
1036
1037
David Woodhouseaa21c002007-12-08 20:04:36 +00001038static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001039 struct assoc_request * assoc_req)
1040{
Holger Schurig0765af42007-10-15 12:55:56 +02001041 int ret = 0;
1042
David Woodhouseaa21c002007-12-08 20:04:36 +00001043 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001044 return 0;
1045
Holger Schurig52507c22008-01-28 17:25:53 +01001046 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001047 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001048 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1049 ret = 1;
1050 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051 }
1052
1053 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001054 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +02001055 lbs_deb_assoc("Deauthenticating due to new security\n");
1056 ret = 1;
1057 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001058 }
1059 }
1060
1061 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001062 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1063 ret = 1;
1064 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001065 }
1066
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001067 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001068 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1069 ret = 1;
1070 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001071 }
1072
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001073 /* FIXME: deal with 'auto' mode somehow */
1074 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001075 if (assoc_req->mode != IW_MODE_INFRA) {
1076 lbs_deb_assoc("Deauthenticating due to leaving "
1077 "infra mode\n");
1078 ret = 1;
1079 goto out;
1080 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001081 }
1082
Holger Schurig0765af42007-10-15 12:55:56 +02001083out:
1084 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +01001085 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001086}
1087
1088
David Woodhouseaa21c002007-12-08 20:04:36 +00001089static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001090 struct assoc_request * assoc_req)
1091{
Holger Schurig0765af42007-10-15 12:55:56 +02001092 lbs_deb_enter(LBS_DEB_ASSOC);
1093
David Woodhouseaa21c002007-12-08 20:04:36 +00001094 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001095 return 0;
1096
David Woodhouseaa21c002007-12-08 20:04:36 +00001097 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1098 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001099 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001100 return 1;
1101
1102 /* FIXME: deal with 'auto' mode somehow */
1103 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001104 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001105 return 1;
1106 }
1107
Dan Williamsef9a2642007-05-25 16:46:33 -04001108 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001109 if (assoc_req->channel != priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001110 return 1;
1111 }
1112
Holger Schurig0765af42007-10-15 12:55:56 +02001113 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001114 return 0;
1115}
1116
1117
Holger Schurig245bf202008-04-02 16:27:42 +02001118/**
1119 * @brief This function finds the best SSID in the Scan List
1120 *
1121 * Search the scan table for the best SSID that also matches the current
1122 * adapter network preference (infrastructure or adhoc)
1123 *
1124 * @param priv A pointer to struct lbs_private
1125 *
1126 * @return index in BSSID list
1127 */
1128static struct bss_descriptor *lbs_find_best_ssid_in_list(
1129 struct lbs_private *priv, uint8_t mode)
1130{
1131 uint8_t bestrssi = 0;
1132 struct bss_descriptor *iter_bss;
1133 struct bss_descriptor *best_bss = NULL;
1134
1135 lbs_deb_enter(LBS_DEB_SCAN);
1136
1137 mutex_lock(&priv->lock);
1138
1139 list_for_each_entry(iter_bss, &priv->network_list, list) {
1140 switch (mode) {
1141 case IW_MODE_INFRA:
1142 case IW_MODE_ADHOC:
1143 if (!is_network_compatible(priv, iter_bss, mode))
1144 break;
1145 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1146 break;
1147 bestrssi = SCAN_RSSI(iter_bss->rssi);
1148 best_bss = iter_bss;
1149 break;
1150 case IW_MODE_AUTO:
1151 default:
1152 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1153 break;
1154 bestrssi = SCAN_RSSI(iter_bss->rssi);
1155 best_bss = iter_bss;
1156 break;
1157 }
1158 }
1159
1160 mutex_unlock(&priv->lock);
1161 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1162 return best_bss;
1163}
1164
1165/**
1166 * @brief Find the best AP
1167 *
1168 * Used from association worker.
1169 *
1170 * @param priv A pointer to struct lbs_private structure
1171 * @param pSSID A pointer to AP's ssid
1172 *
1173 * @return 0--success, otherwise--fail
1174 */
1175static int lbs_find_best_network_ssid(struct lbs_private *priv,
1176 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1177 uint8_t *out_mode)
1178{
1179 int ret = -1;
1180 struct bss_descriptor *found;
1181
1182 lbs_deb_enter(LBS_DEB_SCAN);
1183
1184 priv->scan_ssid_len = 0;
1185 lbs_scan_networks(priv, 1);
1186 if (priv->surpriseremoved)
1187 goto out;
1188
1189 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1190 if (found && (found->ssid_len > 0)) {
1191 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1192 *out_ssid_len = found->ssid_len;
1193 *out_mode = found->mode;
1194 ret = 0;
1195 }
1196
1197out:
1198 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1199 return ret;
1200}
1201
1202
Holger Schurig10078322007-11-15 18:05:47 -05001203void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001204{
Holger Schurig69f90322007-11-23 15:43:44 +01001205 struct lbs_private *priv = container_of(work, struct lbs_private,
1206 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001207 struct assoc_request * assoc_req = NULL;
1208 int ret = 0;
1209 int find_any_ssid = 0;
1210
Holger Schurig9012b282007-05-25 11:27:16 -04001211 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001212
David Woodhouseaa21c002007-12-08 20:04:36 +00001213 mutex_lock(&priv->lock);
1214 assoc_req = priv->pending_assoc_req;
1215 priv->pending_assoc_req = NULL;
1216 priv->in_progress_assoc_req = assoc_req;
1217 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001218
Holger Schurig9012b282007-05-25 11:27:16 -04001219 if (!assoc_req)
1220 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001221
Holger Schurig0765af42007-10-15 12:55:56 +02001222 lbs_deb_assoc(
1223 "Association Request:\n"
1224 " flags: 0x%08lx\n"
1225 " SSID: '%s'\n"
1226 " chann: %d\n"
1227 " band: %d\n"
1228 " mode: %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001229 " BSSID: %pM\n"
Holger Schurig0765af42007-10-15 12:55:56 +02001230 " secinfo: %s%s%s\n"
1231 " auth_mode: %d\n",
1232 assoc_req->flags,
John W. Linville7e272fc2008-09-24 18:13:14 -04001233 escape_ssid(assoc_req->ssid, assoc_req->ssid_len),
Holger Schurig0765af42007-10-15 12:55:56 +02001234 assoc_req->channel, assoc_req->band, assoc_req->mode,
Johannes Berge1749612008-10-27 15:59:26 -07001235 assoc_req->bssid,
Holger Schurig0765af42007-10-15 12:55:56 +02001236 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1237 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1238 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1239 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001240
1241 /* If 'any' SSID was specified, find an SSID to associate with */
1242 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001243 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001244 find_any_ssid = 1;
1245
1246 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1247 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf20932007-05-25 17:28:30 -04001248 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1249 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001250 find_any_ssid = 0;
1251 }
1252
1253 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02001254 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001255
Holger Schurig10078322007-11-15 18:05:47 -05001256 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001257 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001258 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001259 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001260 ret = -ENETUNREACH;
1261 goto out;
1262 }
1263
1264 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04001265 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001266 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1267 assoc_req->mode = new_mode;
1268 }
1269 }
1270
1271 /*
1272 * Check if the attributes being changing require deauthentication
1273 * from the currently associated infrastructure access point.
1274 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001275 if (priv->mode == IW_MODE_INFRA) {
1276 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04001277 ret = lbs_cmd_80211_deauthenticate(priv,
1278 priv->curbssparams.bssid,
1279 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001280 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001281 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001282 "configuration request failed: %d\n",
1283 ret);
1284 }
1285 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001286 } else if (priv->mode == IW_MODE_ADHOC) {
1287 if (should_stop_adhoc(priv, assoc_req)) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001288 ret = lbs_adhoc_stop(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001289 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001290 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001291 "new configuration request failed: %d\n",
1292 ret);
1293 }
1294
1295 }
1296 }
1297
1298 /* Send the various configuration bits to the firmware */
1299 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1300 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001301 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001302 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001303 }
1304
Dan Williamsef9a2642007-05-25 16:46:33 -04001305 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1306 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001307 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04001308 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04001309 }
1310
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001311 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1312 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1313 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001314 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001315 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001316 }
1317
1318 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1319 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001320 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001321 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001322 }
1323
1324 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1325 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001326 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001327 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001328 }
1329
1330 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1331 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1332 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001333 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001334 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001335 }
1336
1337 /* SSID/BSSID should be the _last_ config option set, because they
1338 * trigger the association attempt.
1339 */
1340 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1341 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1342 int success = 1;
1343
1344 ret = assoc_helper_associate(priv, assoc_req);
1345 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01001346 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001347 ret);
1348 success = 0;
1349 }
1350
David Woodhouseaa21c002007-12-08 20:04:36 +00001351 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01001352 lbs_deb_assoc("ASSOC: association unsuccessful, "
1353 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001354 success = 0;
1355 }
1356
1357 if (success) {
Johannes Berge1749612008-10-27 15:59:26 -07001358 lbs_deb_assoc("associated to %pM\n",
1359 priv->curbssparams.bssid);
Holger Schurig10078322007-11-15 18:05:47 -05001360 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04001361 CMD_802_11_RSSI,
1362 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001363 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001364 ret = -1;
1365 }
1366 }
1367
1368out:
1369 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001370 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001371 ret);
1372 }
Dan Williamse76850d2007-05-25 17:09:41 -04001373
David Woodhouseaa21c002007-12-08 20:04:36 +00001374 mutex_lock(&priv->lock);
1375 priv->in_progress_assoc_req = NULL;
1376 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001377 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04001378
1379done:
1380 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001381}
1382
1383
1384/*
1385 * Caller MUST hold any necessary locks
1386 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001387struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001388{
1389 struct assoc_request * assoc_req;
1390
Holger Schurig0765af42007-10-15 12:55:56 +02001391 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00001392 if (!priv->pending_assoc_req) {
1393 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04001394 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00001395 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001396 lbs_pr_info("Not enough memory to allocate association"
1397 " request!\n");
1398 return NULL;
1399 }
1400 }
1401
1402 /* Copy current configuration attributes to the association request,
1403 * but don't overwrite any that are already set.
1404 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001405 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001406 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001407 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001408 IW_ESSID_MAX_SIZE);
David Woodhouseaa21c002007-12-08 20:04:36 +00001409 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001410 }
1411
1412 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001413 assoc_req->channel = priv->curbssparams.channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001414
Dan Williamse76850d2007-05-25 17:09:41 -04001415 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001416 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04001417
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001418 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001419 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001420
1421 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001422 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001423 ETH_ALEN);
1424 }
1425
1426 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1427 int i;
1428 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001429 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04001430 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001431 }
1432 }
1433
1434 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001435 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001436
1437 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001438 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001439 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001440 }
1441
1442 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001443 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001444 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001445 }
1446
1447 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001448 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001449 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001450 }
1451
1452 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001453 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001454 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001455 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001456 }
1457
Holger Schurig0765af42007-10-15 12:55:56 +02001458 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001459 return assoc_req;
1460}
Holger Schurig697900a2008-04-02 16:27:10 +02001461
1462
1463/**
Holger Schurig697900a2008-04-02 16:27:10 +02001464 * @brief This function prepares command of authenticate.
1465 *
1466 * @param priv A pointer to struct lbs_private structure
1467 * @param cmd A pointer to cmd_ds_command structure
1468 * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
1469 *
1470 * @return 0 or -1
1471 */
1472int lbs_cmd_80211_authenticate(struct lbs_private *priv,
1473 struct cmd_ds_command *cmd,
1474 void *pdata_buf)
1475{
1476 struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
1477 int ret = -1;
1478 u8 *bssid = pdata_buf;
Holger Schurig697900a2008-04-02 16:27:10 +02001479
1480 lbs_deb_enter(LBS_DEB_JOIN);
1481
1482 cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
1483 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
1484 + S_DS_GEN);
1485
1486 /* translate auth mode to 802.11 defined wire value */
1487 switch (priv->secinfo.auth_mode) {
1488 case IW_AUTH_ALG_OPEN_SYSTEM:
1489 pauthenticate->authtype = 0x00;
1490 break;
1491 case IW_AUTH_ALG_SHARED_KEY:
1492 pauthenticate->authtype = 0x01;
1493 break;
1494 case IW_AUTH_ALG_LEAP:
1495 pauthenticate->authtype = 0x80;
1496 break;
1497 default:
1498 lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
1499 priv->secinfo.auth_mode);
1500 goto out;
1501 }
1502
1503 memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
1504
Johannes Berge1749612008-10-27 15:59:26 -07001505 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
1506 bssid, pauthenticate->authtype);
Holger Schurig697900a2008-04-02 16:27:10 +02001507 ret = 0;
1508
1509out:
1510 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1511 return ret;
1512}
1513
Dan Williams191bb402008-08-21 17:46:18 -04001514/**
1515 * @brief Deauthenticate from a specific BSS
1516 *
1517 * @param priv A pointer to struct lbs_private structure
1518 * @param bssid The specific BSS to deauthenticate from
1519 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1520 *
1521 * @return 0 on success, error on failure
1522 */
1523int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1524 u16 reason)
Holger Schurig697900a2008-04-02 16:27:10 +02001525{
Dan Williams191bb402008-08-21 17:46:18 -04001526 struct cmd_ds_802_11_deauthenticate cmd;
1527 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001528
1529 lbs_deb_enter(LBS_DEB_JOIN);
1530
Dan Williams191bb402008-08-21 17:46:18 -04001531 memset(&cmd, 0, sizeof(cmd));
1532 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1533 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1534 cmd.reasoncode = cpu_to_le16(reason);
Holger Schurig697900a2008-04-02 16:27:10 +02001535
Dan Williams191bb402008-08-21 17:46:18 -04001536 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001537
Dan Williams191bb402008-08-21 17:46:18 -04001538 /* Clean up everything even if there was an error; can't assume that
1539 * we're still authenticated to the AP after trying to deauth.
1540 */
1541 lbs_mac_event_disconnected(priv);
Holger Schurig697900a2008-04-02 16:27:10 +02001542
1543 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04001544 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001545}
1546
1547int lbs_cmd_80211_associate(struct lbs_private *priv,
1548 struct cmd_ds_command *cmd, void *pdata_buf)
1549{
1550 struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
1551 int ret = 0;
1552 struct assoc_request *assoc_req = pdata_buf;
1553 struct bss_descriptor *bss = &assoc_req->bss;
1554 u8 *pos;
1555 u16 tmpcap, tmplen;
1556 struct mrvlietypes_ssidparamset *ssid;
1557 struct mrvlietypes_phyparamset *phy;
1558 struct mrvlietypes_ssparamset *ss;
1559 struct mrvlietypes_ratesparamset *rates;
1560 struct mrvlietypes_rsnparamset *rsn;
1561
1562 lbs_deb_enter(LBS_DEB_ASSOC);
1563
1564 pos = (u8 *) passo;
1565
1566 if (!priv) {
1567 ret = -1;
1568 goto done;
1569 }
1570
1571 cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1572
1573 memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
1574 pos += sizeof(passo->peerstaaddr);
1575
1576 /* set the listen interval */
1577 passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1578
1579 pos += sizeof(passo->capability);
1580 pos += sizeof(passo->listeninterval);
1581 pos += sizeof(passo->bcnperiod);
1582 pos += sizeof(passo->dtimperiod);
1583
1584 ssid = (struct mrvlietypes_ssidparamset *) pos;
1585 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
1586 tmplen = bss->ssid_len;
1587 ssid->header.len = cpu_to_le16(tmplen);
1588 memcpy(ssid->ssid, bss->ssid, tmplen);
1589 pos += sizeof(ssid->header) + tmplen;
1590
1591 phy = (struct mrvlietypes_phyparamset *) pos;
1592 phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
1593 tmplen = sizeof(phy->fh_ds.dsparamset);
1594 phy->header.len = cpu_to_le16(tmplen);
1595 memcpy(&phy->fh_ds.dsparamset,
1596 &bss->phyparamset.dsparamset.currentchan,
1597 tmplen);
1598 pos += sizeof(phy->header) + tmplen;
1599
1600 ss = (struct mrvlietypes_ssparamset *) pos;
1601 ss->header.type = cpu_to_le16(TLV_TYPE_CF);
1602 tmplen = sizeof(ss->cf_ibss.cfparamset);
1603 ss->header.len = cpu_to_le16(tmplen);
1604 pos += sizeof(ss->header) + tmplen;
1605
1606 rates = (struct mrvlietypes_ratesparamset *) pos;
1607 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
1608 memcpy(&rates->rates, &bss->rates, MAX_RATES);
1609 tmplen = MAX_RATES;
1610 if (get_common_rates(priv, rates->rates, &tmplen)) {
1611 ret = -1;
1612 goto done;
1613 }
1614 pos += sizeof(rates->header) + tmplen;
1615 rates->header.len = cpu_to_le16(tmplen);
1616 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
1617
1618 /* Copy the infra. association rates into Current BSS state structure */
1619 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1620 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
1621
1622 /* Set MSB on basic rates as the firmware requires, but _after_
1623 * copying to current bss rates.
1624 */
1625 lbs_set_basic_rate_flags(rates->rates, tmplen);
1626
1627 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1628 rsn = (struct mrvlietypes_rsnparamset *) pos;
1629 /* WPA_IE or WPA2_IE */
1630 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
1631 tmplen = (u16) assoc_req->wpa_ie[1];
1632 rsn->header.len = cpu_to_le16(tmplen);
1633 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
1634 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
1635 sizeof(rsn->header) + tmplen);
1636 pos += sizeof(rsn->header) + tmplen;
1637 }
1638
1639 /* update curbssparams */
1640 priv->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
1641
1642 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
1643 ret = -1;
1644 goto done;
1645 }
1646
1647 cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
1648
1649 /* set the capability info */
1650 tmpcap = (bss->capability & CAPINFO_MASK);
1651 if (bss->mode == IW_MODE_INFRA)
1652 tmpcap |= WLAN_CAPABILITY_ESS;
1653 passo->capability = cpu_to_le16(tmpcap);
1654 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
1655
1656done:
1657 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1658 return ret;
1659}
1660
Holger Schurig697900a2008-04-02 16:27:10 +02001661int lbs_ret_80211_associate(struct lbs_private *priv,
1662 struct cmd_ds_command *resp)
1663{
1664 int ret = 0;
1665 union iwreq_data wrqu;
1666 struct ieeetypes_assocrsp *passocrsp;
1667 struct bss_descriptor *bss;
1668 u16 status_code;
1669
1670 lbs_deb_enter(LBS_DEB_ASSOC);
1671
1672 if (!priv->in_progress_assoc_req) {
1673 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
1674 ret = -1;
1675 goto done;
1676 }
1677 bss = &priv->in_progress_assoc_req->bss;
1678
1679 passocrsp = (struct ieeetypes_assocrsp *) &resp->params;
1680
1681 /*
1682 * Older FW versions map the IEEE 802.11 Status Code in the association
1683 * response to the following values returned in passocrsp->statuscode:
1684 *
1685 * IEEE Status Code Marvell Status Code
1686 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1687 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1688 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1689 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1690 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1691 * others -> 0x0003 ASSOC_RESULT_REFUSED
1692 *
1693 * Other response codes:
1694 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1695 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1696 * association response from the AP)
1697 */
1698
1699 status_code = le16_to_cpu(passocrsp->statuscode);
1700 switch (status_code) {
1701 case 0x00:
1702 break;
1703 case 0x01:
1704 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
1705 break;
1706 case 0x02:
1707 lbs_deb_assoc("ASSOC_RESP: internal timer "
1708 "expired while waiting for the AP\n");
1709 break;
1710 case 0x03:
1711 lbs_deb_assoc("ASSOC_RESP: association "
1712 "refused by AP\n");
1713 break;
1714 case 0x04:
1715 lbs_deb_assoc("ASSOC_RESP: authentication "
1716 "refused by AP\n");
1717 break;
1718 default:
1719 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
1720 " unknown\n", status_code);
1721 break;
1722 }
1723
1724 if (status_code) {
1725 lbs_mac_event_disconnected(priv);
1726 ret = -1;
1727 goto done;
1728 }
1729
1730 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
1731 le16_to_cpu(resp->size) - S_DS_GEN);
1732
1733 /* Send a Media Connected event, according to the Spec */
1734 priv->connect_status = LBS_CONNECTED;
1735
1736 /* Update current SSID and BSSID */
1737 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1738 priv->curbssparams.ssid_len = bss->ssid_len;
1739 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1740
1741 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
1742 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
1743
1744 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
1745 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
1746 priv->nextSNRNF = 0;
1747 priv->numSNRNF = 0;
1748
1749 netif_carrier_on(priv->dev);
1750 if (!priv->tx_pending_len)
1751 netif_wake_queue(priv->dev);
1752
1753 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1754 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1755 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1756
1757done:
1758 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1759 return ret;
1760}
1761
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001762static int lbs_adhoc_post(struct lbs_private *priv, struct cmd_header *resp)
Holger Schurig697900a2008-04-02 16:27:10 +02001763{
1764 int ret = 0;
1765 u16 command = le16_to_cpu(resp->command);
1766 u16 result = le16_to_cpu(resp->result);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001767 struct cmd_ds_802_11_ad_hoc_result *adhoc_resp;
Holger Schurig697900a2008-04-02 16:27:10 +02001768 union iwreq_data wrqu;
1769 struct bss_descriptor *bss;
Holger Schurig697900a2008-04-02 16:27:10 +02001770
1771 lbs_deb_enter(LBS_DEB_JOIN);
1772
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001773 adhoc_resp = (struct cmd_ds_802_11_ad_hoc_result *) resp;
Holger Schurig697900a2008-04-02 16:27:10 +02001774
1775 if (!priv->in_progress_assoc_req) {
1776 lbs_deb_join("ADHOC_RESP: no in-progress association "
1777 "request\n");
1778 ret = -1;
1779 goto done;
1780 }
1781 bss = &priv->in_progress_assoc_req->bss;
1782
1783 /*
1784 * Join result code 0 --> SUCCESS
1785 */
1786 if (result) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001787 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
Holger Schurig697900a2008-04-02 16:27:10 +02001788 if (priv->connect_status == LBS_CONNECTED)
1789 lbs_mac_event_disconnected(priv);
1790 ret = -1;
1791 goto done;
1792 }
1793
Holger Schurig697900a2008-04-02 16:27:10 +02001794 /* Send a Media Connected event, according to the Spec */
1795 priv->connect_status = LBS_CONNECTED;
1796
1797 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
1798 /* Update the created network descriptor with the new BSSID */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001799 memcpy(bss->bssid, adhoc_resp->bssid, ETH_ALEN);
Holger Schurig697900a2008-04-02 16:27:10 +02001800 }
1801
1802 /* Set the BSSID from the joined/started descriptor */
1803 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1804
1805 /* Set the new SSID to current SSID */
1806 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1807 priv->curbssparams.ssid_len = bss->ssid_len;
1808
1809 netif_carrier_on(priv->dev);
1810 if (!priv->tx_pending_len)
1811 netif_wake_queue(priv->dev);
1812
1813 memset(&wrqu, 0, sizeof(wrqu));
1814 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1815 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1816 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1817
Johannes Berge1749612008-10-27 15:59:26 -07001818 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
John W. Linville7e272fc2008-09-24 18:13:14 -04001819 escape_ssid(bss->ssid, bss->ssid_len),
Johannes Berge1749612008-10-27 15:59:26 -07001820 priv->curbssparams.bssid,
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001821 priv->curbssparams.channel);
Holger Schurig697900a2008-04-02 16:27:10 +02001822
1823done:
1824 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1825 return ret;
1826}
1827