blob: 92863780286fb8569e4738ec112f13f6c06cf9be [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 Williams3cf209312007-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;
John W. Linville9387b7c2008-09-30 20:59:05 -0400156 DECLARE_SSID_BUF(ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400157 u16 ratesize = 0;
158 int ret = 0;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400159
160 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200161
162 lbs_deb_join("current SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400163 print_ssid(ssid, priv->curbssparams.ssid,
Holger Schurig697900a2008-04-02 16:27:10 +0200164 priv->curbssparams.ssid_len),
165 priv->curbssparams.ssid_len);
166 lbs_deb_join("requested ssid '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400167 print_ssid(ssid, bss->ssid, bss->ssid_len),
Holger Schurig697900a2008-04-02 16:27:10 +0200168 bss->ssid_len);
169
170 /* check if the requested SSID is already joined */
171 if (priv->curbssparams.ssid_len &&
172 !lbs_ssid_cmp(priv->curbssparams.ssid,
173 priv->curbssparams.ssid_len,
174 bss->ssid, bss->ssid_len) &&
175 (priv->mode == IW_MODE_ADHOC) &&
176 (priv->connect_status == LBS_CONNECTED)) {
177 union iwreq_data wrqu;
178
179 lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
180 "current, not attempting to re-join");
181
182 /* Send the re-association event though, because the association
183 * request really was successful, even if just a null-op.
184 */
185 memset(&wrqu, 0, sizeof(wrqu));
186 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
187 ETH_ALEN);
188 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
189 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
190 goto out;
191 }
192
Dan Williamsd5db2df2008-08-21 17:51:07 -0400193 /* Use short preamble only when both the BSS and firmware support it */
194 if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
195 (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
Holger Schurig697900a2008-04-02 16:27:10 +0200196 lbs_deb_join("AdhocJoin: Short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400197 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200198 }
199
Dan Williamsd5db2df2008-08-21 17:51:07 -0400200 ret = lbs_set_radio(priv, preamble, 1);
201 if (ret)
202 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200203
204 lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
205 lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
206
207 priv->adhoccreate = 0;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400208 priv->curbssparams.channel = bss->channel;
Holger Schurig697900a2008-04-02 16:27:10 +0200209
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400210 /* Build the join command */
211 memset(&cmd, 0, sizeof(cmd));
212 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
213
214 cmd.bss.type = CMD_BSS_TYPE_IBSS;
215 cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
216
217 memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
218 memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
219
220 memcpy(&cmd.bss.phyparamset, &bss->phyparamset,
221 sizeof(union ieeetypes_phyparamset));
222
223 memcpy(&cmd.bss.ssparamset, &bss->ssparamset,
224 sizeof(union IEEEtypes_ssparamset));
225
226 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
227 lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
228 bss->capability, CAPINFO_MASK);
229
230 /* information on BSSID descriptor passed to FW */
Johannes Berge1749612008-10-27 15:59:26 -0700231 lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
232 cmd.bss.bssid, cmd.bss.ssid);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400233
234 /* Only v8 and below support setting these */
235 if (priv->fwrelease < 0x09000000) {
236 /* failtimeout */
237 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
238 /* probedelay */
239 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
240 }
241
242 /* Copy Data rates from the rates recorded in scan response */
243 memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
244 ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
245 memcpy(cmd.bss.rates, bss->rates, ratesize);
246 if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
247 lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
248 ret = -1;
249 goto out;
250 }
251
252 /* Copy the ad-hoc creation rates into Current BSS state structure */
253 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
254 memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
255
256 /* Set MSB on basic rates as the firmware requires, but _after_
257 * copying to current bss rates.
258 */
259 lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
260
261 cmd.bss.ssparamset.ibssparamset.atimwindow = cpu_to_le16(bss->atimwindow);
262
263 if (assoc_req->secinfo.wep_enabled) {
264 u16 tmp = le16_to_cpu(cmd.bss.capability);
265 tmp |= WLAN_CAPABILITY_PRIVACY;
266 cmd.bss.capability = cpu_to_le16(tmp);
267 }
268
269 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
270 __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
271
272 /* wake up first */
273 ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
274 CMD_ACT_SET, 0, 0,
275 &local_ps_mode);
276 if (ret) {
277 ret = -1;
278 goto out;
279 }
280 }
281
282 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
283 ret = -1;
284 goto out;
285 }
286
287 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
288 if (ret == 0)
289 ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200290
291out:
Dan Williamsd5db2df2008-08-21 17:51:07 -0400292 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200293 return ret;
294}
295
296/**
297 * @brief Start an Adhoc Network
298 *
299 * @param priv A pointer to struct lbs_private structure
Dan Williamsd5db2df2008-08-21 17:51:07 -0400300 * @param assoc_req The association request describing the BSS to start
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400301 *
302 * @return 0 on success, error on failure
Holger Schurig697900a2008-04-02 16:27:10 +0200303 */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400304static int lbs_adhoc_start(struct lbs_private *priv,
Holger Schurig697900a2008-04-02 16:27:10 +0200305 struct assoc_request *assoc_req)
306{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400307 struct cmd_ds_802_11_ad_hoc_start cmd;
Dan Williamsd5db2df2008-08-21 17:51:07 -0400308 u8 preamble = RADIO_PREAMBLE_LONG;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400309 size_t ratesize = 0;
310 u16 tmpcap = 0;
311 int ret = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400312 DECLARE_SSID_BUF(ssid);
Dan Williamsd5db2df2008-08-21 17:51:07 -0400313
314 lbs_deb_enter(LBS_DEB_ASSOC);
Holger Schurig697900a2008-04-02 16:27:10 +0200315
Holger Schurig697900a2008-04-02 16:27:10 +0200316 if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400317 lbs_deb_join("ADHOC_START: Will use short preamble\n");
Dan Williamsd5db2df2008-08-21 17:51:07 -0400318 preamble = RADIO_PREAMBLE_SHORT;
Holger Schurig697900a2008-04-02 16:27:10 +0200319 }
320
Dan Williamsd5db2df2008-08-21 17:51:07 -0400321 ret = lbs_set_radio(priv, preamble, 1);
322 if (ret)
323 goto out;
Holger Schurig697900a2008-04-02 16:27:10 +0200324
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400325 /* Build the start command */
326 memset(&cmd, 0, sizeof(cmd));
327 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
Holger Schurig697900a2008-04-02 16:27:10 +0200328
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400329 memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
330
331 lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400332 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400333 assoc_req->ssid_len);
334
335 cmd.bsstype = CMD_BSS_TYPE_IBSS;
336
337 if (priv->beacon_period == 0)
338 priv->beacon_period = MRVDRV_BEACON_INTERVAL;
339 cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
340
341 WARN_ON(!assoc_req->channel);
342
343 /* set Physical parameter set */
344 cmd.phyparamset.dsparamset.elementid = MFIE_TYPE_DS_SET;
345 cmd.phyparamset.dsparamset.len = 1;
346 cmd.phyparamset.dsparamset.currentchan = assoc_req->channel;
347
348 /* set IBSS parameter set */
349 cmd.ssparamset.ibssparamset.elementid = MFIE_TYPE_IBSS_SET;
350 cmd.ssparamset.ibssparamset.len = 2;
351 cmd.ssparamset.ibssparamset.atimwindow = 0;
352
353 /* set capability info */
354 tmpcap = WLAN_CAPABILITY_IBSS;
355 if (assoc_req->secinfo.wep_enabled) {
356 lbs_deb_join("ADHOC_START: WEP enabled, setting privacy on\n");
357 tmpcap |= WLAN_CAPABILITY_PRIVACY;
358 } else
359 lbs_deb_join("ADHOC_START: WEP disabled, setting privacy off\n");
360
361 cmd.capability = cpu_to_le16(tmpcap);
362
363 /* Only v8 and below support setting probe delay */
364 if (priv->fwrelease < 0x09000000)
365 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
366
367 ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
368 memcpy(cmd.rates, lbs_bg_rates, ratesize);
369
370 /* Copy the ad-hoc creating rates into Current BSS state structure */
371 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
372 memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
373
374 /* Set MSB on basic rates as the firmware requires, but _after_
375 * copying to current bss rates.
376 */
377 lbs_set_basic_rate_flags(cmd.rates, ratesize);
378
379 lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
380 cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
381
382 if (lbs_create_dnld_countryinfo_11d(priv)) {
383 lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
384 ret = -1;
385 goto out;
386 }
387
388 lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
389 assoc_req->channel, assoc_req->band);
390
391 priv->adhoccreate = 1;
392 priv->mode = IW_MODE_ADHOC;
393
394 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
395 if (ret == 0)
396 ret = lbs_adhoc_post(priv, (struct cmd_header *) &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +0200397
Dan Williamsd5db2df2008-08-21 17:51:07 -0400398out:
399 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig697900a2008-04-02 16:27:10 +0200400 return ret;
401}
402
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400403/**
404 * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
405 *
406 * @param priv A pointer to struct lbs_private structure
407 * @return 0 on success, or an error
408 */
409int lbs_adhoc_stop(struct lbs_private *priv)
Holger Schurig697900a2008-04-02 16:27:10 +0200410{
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400411 struct cmd_ds_802_11_ad_hoc_stop cmd;
412 int ret;
413
414 lbs_deb_enter(LBS_DEB_JOIN);
415
416 memset(&cmd, 0, sizeof (cmd));
417 cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
418
419 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
420
421 /* Clean up everything even if there was an error */
422 lbs_mac_event_disconnected(priv);
423
424 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
425 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +0200426}
Dan Williamse76850d2007-05-25 17:09:41 -0400427
Holger Schurig245bf202008-04-02 16:27:42 +0200428static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
429 struct bss_descriptor *match_bss)
430{
431 if (!secinfo->wep_enabled && !secinfo->WPAenabled
432 && !secinfo->WPA2enabled
433 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
434 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
435 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
436 return 1;
437 else
438 return 0;
439}
440
441static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
442 struct bss_descriptor *match_bss)
443{
444 if (secinfo->wep_enabled && !secinfo->WPAenabled
445 && !secinfo->WPA2enabled
446 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
447 return 1;
448 else
449 return 0;
450}
451
452static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
453 struct bss_descriptor *match_bss)
454{
455 if (!secinfo->wep_enabled && secinfo->WPAenabled
456 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
457 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
458 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
459 )
460 return 1;
461 else
462 return 0;
463}
464
465static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
466 struct bss_descriptor *match_bss)
467{
468 if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
469 (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
470 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
471 (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
472 )
473 return 1;
474 else
475 return 0;
476}
477
478static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
479 struct bss_descriptor *match_bss)
480{
481 if (!secinfo->wep_enabled && !secinfo->WPAenabled
482 && !secinfo->WPA2enabled
483 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
484 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
485 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
486 return 1;
487 else
488 return 0;
489}
490
491/**
492 * @brief Check if a scanned network compatible with the driver settings
493 *
494 * WEP WPA WPA2 ad-hoc encrypt Network
495 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
496 * 0 0 0 0 NONE 0 0 0 yes No security
497 * 1 0 0 0 NONE 1 0 0 yes Static WEP
498 * 0 1 0 0 x 1x 1 x yes WPA
499 * 0 0 1 0 x 1x x 1 yes WPA2
500 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
501 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
502 *
503 *
504 * @param priv A pointer to struct lbs_private
505 * @param index Index in scantable to check against current driver settings
506 * @param mode Network mode: Infrastructure or IBSS
507 *
508 * @return Index in scantable, or error code if negative
509 */
510static int is_network_compatible(struct lbs_private *priv,
511 struct bss_descriptor *bss, uint8_t mode)
512{
513 int matched = 0;
514
515 lbs_deb_enter(LBS_DEB_SCAN);
516
517 if (bss->mode != mode)
518 goto done;
519
520 matched = match_bss_no_security(&priv->secinfo, bss);
521 if (matched)
522 goto done;
523 matched = match_bss_static_wep(&priv->secinfo, bss);
524 if (matched)
525 goto done;
526 matched = match_bss_wpa(&priv->secinfo, bss);
527 if (matched) {
528 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
529 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
530 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
531 priv->secinfo.wep_enabled ? "e" : "d",
532 priv->secinfo.WPAenabled ? "e" : "d",
533 priv->secinfo.WPA2enabled ? "e" : "d",
534 (bss->capability & WLAN_CAPABILITY_PRIVACY));
535 goto done;
536 }
537 matched = match_bss_wpa2(&priv->secinfo, bss);
538 if (matched) {
539 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
540 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
541 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
542 priv->secinfo.wep_enabled ? "e" : "d",
543 priv->secinfo.WPAenabled ? "e" : "d",
544 priv->secinfo.WPA2enabled ? "e" : "d",
545 (bss->capability & WLAN_CAPABILITY_PRIVACY));
546 goto done;
547 }
548 matched = match_bss_dynamic_wep(&priv->secinfo, bss);
549 if (matched) {
550 lbs_deb_scan("is_network_compatible() dynamic WEP: "
551 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
552 bss->wpa_ie[0], bss->rsn_ie[0],
553 (bss->capability & WLAN_CAPABILITY_PRIVACY));
554 goto done;
555 }
556
557 /* bss security settings don't match those configured on card */
558 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
559 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
560 bss->wpa_ie[0], bss->rsn_ie[0],
561 priv->secinfo.wep_enabled ? "e" : "d",
562 priv->secinfo.WPAenabled ? "e" : "d",
563 priv->secinfo.WPA2enabled ? "e" : "d",
564 (bss->capability & WLAN_CAPABILITY_PRIVACY));
565
566done:
567 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
568 return matched;
569}
570
571/**
572 * @brief This function finds a specific compatible BSSID in the scan list
573 *
574 * Used in association code
575 *
576 * @param priv A pointer to struct lbs_private
577 * @param bssid BSSID to find in the scan list
578 * @param mode Network mode: Infrastructure or IBSS
579 *
580 * @return index in BSSID list, or error return code (< 0)
581 */
582static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
583 uint8_t *bssid, uint8_t mode)
584{
585 struct bss_descriptor *iter_bss;
586 struct bss_descriptor *found_bss = NULL;
587
588 lbs_deb_enter(LBS_DEB_SCAN);
589
590 if (!bssid)
591 goto out;
592
593 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
594
595 /* Look through the scan table for a compatible match. The loop will
596 * continue past a matched bssid that is not compatible in case there
597 * is an AP with multiple SSIDs assigned to the same BSSID
598 */
599 mutex_lock(&priv->lock);
600 list_for_each_entry(iter_bss, &priv->network_list, list) {
601 if (compare_ether_addr(iter_bss->bssid, bssid))
602 continue; /* bssid doesn't match */
603 switch (mode) {
604 case IW_MODE_INFRA:
605 case IW_MODE_ADHOC:
606 if (!is_network_compatible(priv, iter_bss, mode))
607 break;
608 found_bss = iter_bss;
609 break;
610 default:
611 found_bss = iter_bss;
612 break;
613 }
614 }
615 mutex_unlock(&priv->lock);
616
617out:
618 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
619 return found_bss;
620}
621
622/**
623 * @brief This function finds ssid in ssid list.
624 *
625 * Used in association code
626 *
627 * @param priv A pointer to struct lbs_private
628 * @param ssid SSID to find in the list
629 * @param bssid BSSID to qualify the SSID selection (if provided)
630 * @param mode Network mode: Infrastructure or IBSS
631 *
632 * @return index in BSSID list
633 */
634static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
635 uint8_t *ssid, uint8_t ssid_len,
636 uint8_t *bssid, uint8_t mode,
637 int channel)
638{
639 u32 bestrssi = 0;
640 struct bss_descriptor *iter_bss = NULL;
641 struct bss_descriptor *found_bss = NULL;
642 struct bss_descriptor *tmp_oldest = NULL;
643
644 lbs_deb_enter(LBS_DEB_SCAN);
645
646 mutex_lock(&priv->lock);
647
648 list_for_each_entry(iter_bss, &priv->network_list, list) {
649 if (!tmp_oldest ||
650 (iter_bss->last_scanned < tmp_oldest->last_scanned))
651 tmp_oldest = iter_bss;
652
653 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
654 ssid, ssid_len) != 0)
655 continue; /* ssid doesn't match */
656 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
657 continue; /* bssid doesn't match */
658 if ((channel > 0) && (iter_bss->channel != channel))
659 continue; /* channel doesn't match */
660
661 switch (mode) {
662 case IW_MODE_INFRA:
663 case IW_MODE_ADHOC:
664 if (!is_network_compatible(priv, iter_bss, mode))
665 break;
666
667 if (bssid) {
668 /* Found requested BSSID */
669 found_bss = iter_bss;
670 goto out;
671 }
672
673 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
674 bestrssi = SCAN_RSSI(iter_bss->rssi);
675 found_bss = iter_bss;
676 }
677 break;
678 case IW_MODE_AUTO:
679 default:
680 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
681 bestrssi = SCAN_RSSI(iter_bss->rssi);
682 found_bss = iter_bss;
683 }
684 break;
685 }
686 }
687
688out:
689 mutex_unlock(&priv->lock);
690 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
691 return found_bss;
692}
693
Holger Schurig69f90322007-11-23 15:43:44 +0100694static int assoc_helper_essid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200695 struct assoc_request * assoc_req)
696{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697 int ret = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400698 struct bss_descriptor * bss;
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400699 int channel = -1;
John W. Linville9387b7c2008-09-30 20:59:05 -0400700 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200701
Holger Schurig9012b282007-05-25 11:27:16 -0400702 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703
Dan Williamsef9a2642007-05-25 16:46:33 -0400704 /* FIXME: take channel into account when picking SSIDs if a channel
705 * is set.
706 */
707
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400708 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
709 channel = assoc_req->channel;
710
Holger Schurig0765af42007-10-15 12:55:56 +0200711 lbs_deb_assoc("SSID '%s' requested\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400712 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
Dan Williams0dc5a292007-05-10 22:58:02 -0400713 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -0500714 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100715 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716
David Woodhouseaa21c002007-12-08 20:04:36 +0000717 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400718 assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400719 if (bss != NULL) {
Dan Williamse76850d2007-05-25 17:09:41 -0400720 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Holger Schurig10078322007-11-15 18:05:47 -0500721 ret = lbs_associate(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722 } else {
Dan Williamsd8efea22007-05-28 23:54:55 -0400723 lbs_deb_assoc("SSID not found; cannot associate\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200724 }
Dan Williams0dc5a292007-05-10 22:58:02 -0400725 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200726 /* Scan for the network, do not save previous results. Stale
727 * scan data will cause us to join a non-existant adhoc network
728 */
Holger Schurig10078322007-11-15 18:05:47 -0500729 lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100730 assoc_req->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200731
732 /* Search for the requested SSID in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000733 bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400734 assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400735 if (bss != NULL) {
Dan Williamsd8efea22007-05-28 23:54:55 -0400736 lbs_deb_assoc("SSID found, will join\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400737 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400738 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200739 } else {
740 /* else send START command */
Dan Williamsd8efea22007-05-28 23:54:55 -0400741 lbs_deb_assoc("SSID not found, creating adhoc network\n");
Dan Williamse76850d2007-05-25 17:09:41 -0400742 memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -0400743 IW_ESSID_MAX_SIZE);
744 assoc_req->bss.ssid_len = assoc_req->ssid_len;
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400745 lbs_adhoc_start(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200747 }
748
Holger Schurig9012b282007-05-25 11:27:16 -0400749 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200750 return ret;
751}
752
753
Holger Schurig69f90322007-11-23 15:43:44 +0100754static int assoc_helper_bssid(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200755 struct assoc_request * assoc_req)
756{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400757 int ret = 0;
758 struct bss_descriptor * bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759
Johannes Berge1749612008-10-27 15:59:26 -0700760 lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200761
762 /* Search for index position in list for requested MAC */
David Woodhouseaa21c002007-12-08 20:04:36 +0000763 bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200764 assoc_req->mode);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400765 if (bss == NULL) {
Johannes Berge1749612008-10-27 15:59:26 -0700766 lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
767 "cannot associate.\n", assoc_req->bssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200768 goto out;
769 }
770
Dan Williamse76850d2007-05-25 17:09:41 -0400771 memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
Dan Williams0dc5a292007-05-10 22:58:02 -0400772 if (assoc_req->mode == IW_MODE_INFRA) {
Holger Schurig10078322007-11-15 18:05:47 -0500773 ret = lbs_associate(priv, assoc_req);
774 lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
Dan Williams0dc5a292007-05-10 22:58:02 -0400775 } else if (assoc_req->mode == IW_MODE_ADHOC) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -0400776 lbs_adhoc_join(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200777 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200778
779out:
Holger Schurig9012b282007-05-25 11:27:16 -0400780 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200781 return ret;
782}
783
784
Holger Schurig69f90322007-11-23 15:43:44 +0100785static int assoc_helper_associate(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200786 struct assoc_request * assoc_req)
787{
788 int ret = 0, done = 0;
789
Holger Schurig0765af42007-10-15 12:55:56 +0200790 lbs_deb_enter(LBS_DEB_ASSOC);
791
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200792 /* If we're given and 'any' BSSID, try associating based on SSID */
793
794 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -0400795 if (compare_ether_addr(bssid_any, assoc_req->bssid)
796 && compare_ether_addr(bssid_off, assoc_req->bssid)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200797 ret = assoc_helper_bssid(priv, assoc_req);
798 done = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200799 }
800 }
801
802 if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
803 ret = assoc_helper_essid(priv, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200804 }
805
Holger Schurig0765af42007-10-15 12:55:56 +0200806 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200807 return ret;
808}
809
810
Holger Schurig69f90322007-11-23 15:43:44 +0100811static int assoc_helper_mode(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200812 struct assoc_request * assoc_req)
813{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200814 int ret = 0;
815
Holger Schurig9012b282007-05-25 11:27:16 -0400816 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200817
David Woodhouseaa21c002007-12-08 20:04:36 +0000818 if (assoc_req->mode == priv->mode)
Holger Schurig9012b282007-05-25 11:27:16 -0400819 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200820
Dan Williams0dc5a292007-05-10 22:58:02 -0400821 if (assoc_req->mode == IW_MODE_INFRA) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000822 if (priv->psstate != PS_STATE_FULL_POWER)
Holger Schurig10078322007-11-15 18:05:47 -0500823 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
David Woodhouseaa21c002007-12-08 20:04:36 +0000824 priv->psmode = LBS802_11POWERMODECAM;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200825 }
826
David Woodhouseaa21c002007-12-08 20:04:36 +0000827 priv->mode = assoc_req->mode;
Dan Williams39fcf7a2008-09-10 12:49:00 -0400828 ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200829
Holger Schurig9012b282007-05-25 11:27:16 -0400830done:
831 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200832 return ret;
833}
834
Holger Schurig69f90322007-11-23 15:43:44 +0100835static int assoc_helper_channel(struct lbs_private *priv,
Dan Williamsef9a2642007-05-25 16:46:33 -0400836 struct assoc_request * assoc_req)
837{
Dan Williamsef9a2642007-05-25 16:46:33 -0400838 int ret = 0;
839
840 lbs_deb_enter(LBS_DEB_ASSOC);
841
David Woodhouse9f462572007-12-12 22:50:21 -0500842 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500843 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500844 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -0500845 goto done;
Dan Williamsef9a2642007-05-25 16:46:33 -0400846 }
847
David Woodhouseaa21c002007-12-08 20:04:36 +0000848 if (assoc_req->channel == priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -0400849 goto done;
850
David Woodhouse8642f1f2007-12-11 20:03:01 -0500851 if (priv->mesh_dev) {
David Woodhouse86062132007-12-13 00:32:36 -0500852 /* Change mesh channel first; 21.p21 firmware won't let
853 you change channel otherwise (even though it'll return
854 an error to this */
Javier Cardonaedaea5c2008-05-17 00:55:10 -0700855 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
856 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500857 }
858
Dan Williamsef9a2642007-05-25 16:46:33 -0400859 lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
David Woodhouse86062132007-12-13 00:32:36 -0500860 priv->curbssparams.channel, assoc_req->channel);
Dan Williamsef9a2642007-05-25 16:46:33 -0400861
Dan Williams2dd4b262007-12-11 16:54:15 -0500862 ret = lbs_set_channel(priv, assoc_req->channel);
863 if (ret < 0)
David Woodhouse23d36ee2007-12-12 00:14:21 -0500864 lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
Dan Williamsef9a2642007-05-25 16:46:33 -0400865
Dan Williams2dd4b262007-12-11 16:54:15 -0500866 /* FIXME: shouldn't need to grab the channel _again_ after setting
867 * it since the firmware is supposed to return the new channel, but
868 * whatever... */
David Woodhouse9f462572007-12-12 22:50:21 -0500869 ret = lbs_update_channel(priv);
David Woodhoused1a469f2007-12-16 17:21:00 -0500870 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500871 lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
David Woodhoused1a469f2007-12-16 17:21:00 -0500872 goto done;
873 }
Dan Williamsef9a2642007-05-25 16:46:33 -0400874
David Woodhouseaa21c002007-12-08 20:04:36 +0000875 if (assoc_req->channel != priv->curbssparams.channel) {
David Woodhouse88ae2912007-12-11 19:57:05 -0500876 lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
Dan Williamsef9a2642007-05-25 16:46:33 -0400877 assoc_req->channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500878 goto restore_mesh;
Dan Williamsef9a2642007-05-25 16:46:33 -0400879 }
880
881 if ( assoc_req->secinfo.wep_enabled
882 && (assoc_req->wep_keys[0].len
883 || assoc_req->wep_keys[1].len
884 || assoc_req->wep_keys[2].len
885 || assoc_req->wep_keys[3].len)) {
886 /* Make sure WEP keys are re-sent to firmware */
887 set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
888 }
889
890 /* Must restart/rejoin adhoc networks after channel change */
David Woodhouse23d36ee2007-12-12 00:14:21 -0500891 set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
Dan Williamsef9a2642007-05-25 16:46:33 -0400892
David Woodhouse8642f1f2007-12-11 20:03:01 -0500893 restore_mesh:
894 if (priv->mesh_dev)
Javier Cardonaedaea5c2008-05-17 00:55:10 -0700895 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
896 priv->curbssparams.channel);
David Woodhouse8642f1f2007-12-11 20:03:01 -0500897
898 done:
Dan Williamsef9a2642007-05-25 16:46:33 -0400899 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
900 return ret;
901}
902
903
Holger Schurig69f90322007-11-23 15:43:44 +0100904static int assoc_helper_wep_keys(struct lbs_private *priv,
David Woodhousef70dd452007-12-18 00:18:05 -0500905 struct assoc_request *assoc_req)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200906{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200907 int i;
908 int ret = 0;
909
Holger Schurig9012b282007-05-25 11:27:16 -0400910 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200911
912 /* Set or remove WEP keys */
David Woodhousef70dd452007-12-18 00:18:05 -0500913 if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
914 assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
915 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
916 else
917 ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200918
919 if (ret)
920 goto out;
921
922 /* enable/disable the MAC's WEP packet filter */
Dan Williams889c05b2007-05-10 22:57:23 -0400923 if (assoc_req->secinfo.wep_enabled)
Holger Schurigd9e97782008-03-12 16:06:43 +0100924 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200925 else
Holger Schurigd9e97782008-03-12 16:06:43 +0100926 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
David Woodhousef70dd452007-12-18 00:18:05 -0500927
Holger Schurigc97329e2008-03-18 11:20:21 +0100928 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200929
David Woodhouseaa21c002007-12-08 20:04:36 +0000930 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200931
David Woodhouseaa21c002007-12-08 20:04:36 +0000932 /* Copy WEP keys into priv wep key fields */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200933 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000934 memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
David Woodhousef70dd452007-12-18 00:18:05 -0500935 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200936 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000937 priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200938
David Woodhouseaa21c002007-12-08 20:04:36 +0000939 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200940
941out:
Holger Schurig9012b282007-05-25 11:27:16 -0400942 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200943 return ret;
944}
945
Holger Schurig69f90322007-11-23 15:43:44 +0100946static int assoc_helper_secinfo(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200947 struct assoc_request * assoc_req)
948{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200949 int ret = 0;
David Woodhouse4f59abf2007-12-18 00:47:17 -0500950 uint16_t do_wpa;
951 uint16_t rsn = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200952
Holger Schurig9012b282007-05-25 11:27:16 -0400953 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200954
David Woodhouseaa21c002007-12-08 20:04:36 +0000955 memcpy(&priv->secinfo, &assoc_req->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -0500956 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200957
Holger Schurigc97329e2008-03-18 11:20:21 +0100958 lbs_set_mac_control(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200959
Dan Williams18c96c342007-06-18 12:01:12 -0400960 /* If RSN is already enabled, don't try to enable it again, since
961 * ENABLE_RSN resets internal state machines and will clobber the
962 * 4-way WPA handshake.
963 */
964
965 /* Get RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -0500966 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
Dan Williams18c96c342007-06-18 12:01:12 -0400967 if (ret) {
David Woodhouse23d36ee2007-12-12 00:14:21 -0500968 lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
Dan Williams18c96c342007-06-18 12:01:12 -0400969 goto out;
970 }
971
972 /* Don't re-enable RSN if it's already enabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -0500973 do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
Dan Williams18c96c342007-06-18 12:01:12 -0400974 if (do_wpa == rsn)
975 goto out;
976
977 /* Set RSN enabled/disabled */
David Woodhouse4f59abf2007-12-18 00:47:17 -0500978 ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
Dan Williams90a42212007-05-25 23:01:24 -0400979
980out:
Holger Schurig9012b282007-05-25 11:27:16 -0400981 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200982 return ret;
983}
984
985
Holger Schurig69f90322007-11-23 15:43:44 +0100986static int assoc_helper_wpa_keys(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200987 struct assoc_request * assoc_req)
988{
989 int ret = 0;
Dan Williams2bcde512007-10-03 10:37:45 -0400990 unsigned int flags = assoc_req->flags;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200991
Holger Schurig9012b282007-05-25 11:27:16 -0400992 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200993
Dan Williams2bcde512007-10-03 10:37:45 -0400994 /* Work around older firmware bug where WPA unicast and multicast
995 * keys must be set independently. Seen in SDIO parts with firmware
996 * version 5.0.11p0.
997 */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200998
Dan Williams2bcde512007-10-03 10:37:45 -0400999 if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1000 clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
David Woodhouse9e1228d2008-03-03 12:15:39 +01001001 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001002 assoc_req->flags = flags;
1003 }
1004
1005 if (ret)
1006 goto out;
1007
1008 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1009 clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
1010
David Woodhouse9e1228d2008-03-03 12:15:39 +01001011 ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
Dan Williams2bcde512007-10-03 10:37:45 -04001012 assoc_req->flags = flags;
1013 }
1014
1015out:
Holger Schurig9012b282007-05-25 11:27:16 -04001016 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001017 return ret;
1018}
1019
1020
Holger Schurig69f90322007-11-23 15:43:44 +01001021static int assoc_helper_wpa_ie(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001022 struct assoc_request * assoc_req)
1023{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001024 int ret = 0;
1025
Holger Schurig9012b282007-05-25 11:27:16 -04001026 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001027
1028 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001029 memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
1030 priv->wpa_ie_len = assoc_req->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001031 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +00001032 memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
1033 priv->wpa_ie_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001034 }
1035
Holger Schurig9012b282007-05-25 11:27:16 -04001036 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001037 return ret;
1038}
1039
1040
David Woodhouseaa21c002007-12-08 20:04:36 +00001041static int should_deauth_infrastructure(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001042 struct assoc_request * assoc_req)
1043{
Holger Schurig0765af42007-10-15 12:55:56 +02001044 int ret = 0;
1045
David Woodhouseaa21c002007-12-08 20:04:36 +00001046 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001047 return 0;
1048
Holger Schurig52507c22008-01-28 17:25:53 +01001049 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001051 lbs_deb_assoc("Deauthenticating due to new SSID\n");
1052 ret = 1;
1053 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001054 }
1055
1056 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001057 if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
Holger Schurig0765af42007-10-15 12:55:56 +02001058 lbs_deb_assoc("Deauthenticating due to new security\n");
1059 ret = 1;
1060 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001061 }
1062 }
1063
1064 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001065 lbs_deb_assoc("Deauthenticating due to new BSSID\n");
1066 ret = 1;
1067 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001068 }
1069
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001070 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001071 lbs_deb_assoc("Deauthenticating due to channel switch\n");
1072 ret = 1;
1073 goto out;
Luis Carlos Cobo Rusfff47f12007-05-30 12:16:13 -04001074 }
1075
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076 /* FIXME: deal with 'auto' mode somehow */
1077 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Holger Schurig0765af42007-10-15 12:55:56 +02001078 if (assoc_req->mode != IW_MODE_INFRA) {
1079 lbs_deb_assoc("Deauthenticating due to leaving "
1080 "infra mode\n");
1081 ret = 1;
1082 goto out;
1083 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001084 }
1085
Holger Schurig0765af42007-10-15 12:55:56 +02001086out:
1087 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
Holger Schurig52507c22008-01-28 17:25:53 +01001088 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001089}
1090
1091
David Woodhouseaa21c002007-12-08 20:04:36 +00001092static int should_stop_adhoc(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001093 struct assoc_request * assoc_req)
1094{
Holger Schurig0765af42007-10-15 12:55:56 +02001095 lbs_deb_enter(LBS_DEB_ASSOC);
1096
David Woodhouseaa21c002007-12-08 20:04:36 +00001097 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001098 return 0;
1099
David Woodhouseaa21c002007-12-08 20:04:36 +00001100 if (lbs_ssid_cmp(priv->curbssparams.ssid,
1101 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001102 assoc_req->ssid, assoc_req->ssid_len) != 0)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103 return 1;
1104
1105 /* FIXME: deal with 'auto' mode somehow */
1106 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001107 if (assoc_req->mode != IW_MODE_ADHOC)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001108 return 1;
1109 }
1110
Dan Williamsef9a2642007-05-25 16:46:33 -04001111 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001112 if (assoc_req->channel != priv->curbssparams.channel)
Dan Williamsef9a2642007-05-25 16:46:33 -04001113 return 1;
1114 }
1115
Holger Schurig0765af42007-10-15 12:55:56 +02001116 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001117 return 0;
1118}
1119
1120
Holger Schurig245bf202008-04-02 16:27:42 +02001121/**
1122 * @brief This function finds the best SSID in the Scan List
1123 *
1124 * Search the scan table for the best SSID that also matches the current
1125 * adapter network preference (infrastructure or adhoc)
1126 *
1127 * @param priv A pointer to struct lbs_private
1128 *
1129 * @return index in BSSID list
1130 */
1131static struct bss_descriptor *lbs_find_best_ssid_in_list(
1132 struct lbs_private *priv, uint8_t mode)
1133{
1134 uint8_t bestrssi = 0;
1135 struct bss_descriptor *iter_bss;
1136 struct bss_descriptor *best_bss = NULL;
1137
1138 lbs_deb_enter(LBS_DEB_SCAN);
1139
1140 mutex_lock(&priv->lock);
1141
1142 list_for_each_entry(iter_bss, &priv->network_list, list) {
1143 switch (mode) {
1144 case IW_MODE_INFRA:
1145 case IW_MODE_ADHOC:
1146 if (!is_network_compatible(priv, iter_bss, mode))
1147 break;
1148 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1149 break;
1150 bestrssi = SCAN_RSSI(iter_bss->rssi);
1151 best_bss = iter_bss;
1152 break;
1153 case IW_MODE_AUTO:
1154 default:
1155 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1156 break;
1157 bestrssi = SCAN_RSSI(iter_bss->rssi);
1158 best_bss = iter_bss;
1159 break;
1160 }
1161 }
1162
1163 mutex_unlock(&priv->lock);
1164 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
1165 return best_bss;
1166}
1167
1168/**
1169 * @brief Find the best AP
1170 *
1171 * Used from association worker.
1172 *
1173 * @param priv A pointer to struct lbs_private structure
1174 * @param pSSID A pointer to AP's ssid
1175 *
1176 * @return 0--success, otherwise--fail
1177 */
1178static int lbs_find_best_network_ssid(struct lbs_private *priv,
1179 uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
1180 uint8_t *out_mode)
1181{
1182 int ret = -1;
1183 struct bss_descriptor *found;
1184
1185 lbs_deb_enter(LBS_DEB_SCAN);
1186
1187 priv->scan_ssid_len = 0;
1188 lbs_scan_networks(priv, 1);
1189 if (priv->surpriseremoved)
1190 goto out;
1191
1192 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
1193 if (found && (found->ssid_len > 0)) {
1194 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1195 *out_ssid_len = found->ssid_len;
1196 *out_mode = found->mode;
1197 ret = 0;
1198 }
1199
1200out:
1201 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1202 return ret;
1203}
1204
1205
Holger Schurig10078322007-11-15 18:05:47 -05001206void lbs_association_worker(struct work_struct *work)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001207{
Holger Schurig69f90322007-11-23 15:43:44 +01001208 struct lbs_private *priv = container_of(work, struct lbs_private,
1209 assoc_work.work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001210 struct assoc_request * assoc_req = NULL;
1211 int ret = 0;
1212 int find_any_ssid = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -04001213 DECLARE_SSID_BUF(ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001214
Holger Schurig9012b282007-05-25 11:27:16 -04001215 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001216
David Woodhouseaa21c002007-12-08 20:04:36 +00001217 mutex_lock(&priv->lock);
1218 assoc_req = priv->pending_assoc_req;
1219 priv->pending_assoc_req = NULL;
1220 priv->in_progress_assoc_req = assoc_req;
1221 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001222
Holger Schurig9012b282007-05-25 11:27:16 -04001223 if (!assoc_req)
1224 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001225
Holger Schurig0765af42007-10-15 12:55:56 +02001226 lbs_deb_assoc(
1227 "Association Request:\n"
1228 " flags: 0x%08lx\n"
1229 " SSID: '%s'\n"
1230 " chann: %d\n"
1231 " band: %d\n"
1232 " mode: %d\n"
Johannes Berge1749612008-10-27 15:59:26 -07001233 " BSSID: %pM\n"
Holger Schurig0765af42007-10-15 12:55:56 +02001234 " secinfo: %s%s%s\n"
1235 " auth_mode: %d\n",
1236 assoc_req->flags,
John W. Linville9387b7c2008-09-30 20:59:05 -04001237 print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
Holger Schurig0765af42007-10-15 12:55:56 +02001238 assoc_req->channel, assoc_req->band, assoc_req->mode,
Johannes Berge1749612008-10-27 15:59:26 -07001239 assoc_req->bssid,
Holger Schurig0765af42007-10-15 12:55:56 +02001240 assoc_req->secinfo.WPAenabled ? " WPA" : "",
1241 assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
1242 assoc_req->secinfo.wep_enabled ? " WEP" : "",
1243 assoc_req->secinfo.auth_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001244
1245 /* If 'any' SSID was specified, find an SSID to associate with */
1246 if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
Dan Williamsd8efea22007-05-28 23:54:55 -04001247 && !assoc_req->ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001248 find_any_ssid = 1;
1249
1250 /* But don't use 'any' SSID if there's a valid locked BSSID to use */
1251 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
Dan Williams3cf209312007-05-25 17:28:30 -04001252 if (compare_ether_addr(assoc_req->bssid, bssid_any)
1253 && compare_ether_addr(assoc_req->bssid, bssid_off))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001254 find_any_ssid = 0;
1255 }
1256
1257 if (find_any_ssid) {
Holger Schurig877cb0d2008-04-02 16:34:51 +02001258 u8 new_mode = assoc_req->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001259
Holger Schurig10078322007-11-15 18:05:47 -05001260 ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001261 &assoc_req->ssid_len, assoc_req->mode, &new_mode);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001262 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001263 lbs_deb_assoc("Could not find best network\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001264 ret = -ENETUNREACH;
1265 goto out;
1266 }
1267
1268 /* Ensure we switch to the mode of the AP */
Dan Williams0dc5a292007-05-10 22:58:02 -04001269 if (assoc_req->mode == IW_MODE_AUTO) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001270 set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
1271 assoc_req->mode = new_mode;
1272 }
1273 }
1274
1275 /*
1276 * Check if the attributes being changing require deauthentication
1277 * from the currently associated infrastructure access point.
1278 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001279 if (priv->mode == IW_MODE_INFRA) {
1280 if (should_deauth_infrastructure(priv, assoc_req)) {
Dan Williams191bb402008-08-21 17:46:18 -04001281 ret = lbs_cmd_80211_deauthenticate(priv,
1282 priv->curbssparams.bssid,
1283 WLAN_REASON_DEAUTH_LEAVING);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001284 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001285 lbs_deb_assoc("Deauthentication due to new "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001286 "configuration request failed: %d\n",
1287 ret);
1288 }
1289 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001290 } else if (priv->mode == IW_MODE_ADHOC) {
1291 if (should_stop_adhoc(priv, assoc_req)) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001292 ret = lbs_adhoc_stop(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001293 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001294 lbs_deb_assoc("Teardown of AdHoc network due to "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001295 "new configuration request failed: %d\n",
1296 ret);
1297 }
1298
1299 }
1300 }
1301
1302 /* Send the various configuration bits to the firmware */
1303 if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1304 ret = assoc_helper_mode(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001305 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001306 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001307 }
1308
Dan Williamsef9a2642007-05-25 16:46:33 -04001309 if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1310 ret = assoc_helper_channel(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001311 if (ret)
Dan Williamsef9a2642007-05-25 16:46:33 -04001312 goto out;
Dan Williamsef9a2642007-05-25 16:46:33 -04001313 }
1314
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001315 if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
1316 || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
1317 ret = assoc_helper_wep_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001318 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001319 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001320 }
1321
1322 if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1323 ret = assoc_helper_secinfo(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001324 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001325 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001326 }
1327
1328 if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1329 ret = assoc_helper_wpa_ie(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001330 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001331 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001332 }
1333
1334 if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
1335 || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1336 ret = assoc_helper_wpa_keys(priv, assoc_req);
Holger Schurig0765af42007-10-15 12:55:56 +02001337 if (ret)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001338 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001339 }
1340
1341 /* SSID/BSSID should be the _last_ config option set, because they
1342 * trigger the association attempt.
1343 */
1344 if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
1345 || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1346 int success = 1;
1347
1348 ret = assoc_helper_associate(priv, assoc_req);
1349 if (ret) {
Holger Schurig91843462007-11-28 14:05:02 +01001350 lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001351 ret);
1352 success = 0;
1353 }
1354
David Woodhouseaa21c002007-12-08 20:04:36 +00001355 if (priv->connect_status != LBS_CONNECTED) {
Holger Schurig91843462007-11-28 14:05:02 +01001356 lbs_deb_assoc("ASSOC: association unsuccessful, "
1357 "not connected\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001358 success = 0;
1359 }
1360
1361 if (success) {
Johannes Berge1749612008-10-27 15:59:26 -07001362 lbs_deb_assoc("associated to %pM\n",
1363 priv->curbssparams.bssid);
Holger Schurig10078322007-11-15 18:05:47 -05001364 lbs_prepare_and_send_command(priv,
Dan Williams0aef64d2007-08-02 11:31:18 -04001365 CMD_802_11_RSSI,
1366 0, CMD_OPTION_WAITFORRSP, 0, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001367 } else {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001368 ret = -1;
1369 }
1370 }
1371
1372out:
1373 if (ret) {
Holger Schurig9012b282007-05-25 11:27:16 -04001374 lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001375 ret);
1376 }
Dan Williamse76850d2007-05-25 17:09:41 -04001377
David Woodhouseaa21c002007-12-08 20:04:36 +00001378 mutex_lock(&priv->lock);
1379 priv->in_progress_assoc_req = NULL;
1380 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001381 kfree(assoc_req);
Holger Schurig9012b282007-05-25 11:27:16 -04001382
1383done:
1384 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001385}
1386
1387
1388/*
1389 * Caller MUST hold any necessary locks
1390 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001391struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001392{
1393 struct assoc_request * assoc_req;
1394
Holger Schurig0765af42007-10-15 12:55:56 +02001395 lbs_deb_enter(LBS_DEB_ASSOC);
David Woodhouseaa21c002007-12-08 20:04:36 +00001396 if (!priv->pending_assoc_req) {
1397 priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
Dan Williamse76850d2007-05-25 17:09:41 -04001398 GFP_KERNEL);
David Woodhouseaa21c002007-12-08 20:04:36 +00001399 if (!priv->pending_assoc_req) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001400 lbs_pr_info("Not enough memory to allocate association"
1401 " request!\n");
1402 return NULL;
1403 }
1404 }
1405
1406 /* Copy current configuration attributes to the association request,
1407 * but don't overwrite any that are already set.
1408 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001409 assoc_req = priv->pending_assoc_req;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001410 if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001411 memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001412 IW_ESSID_MAX_SIZE);
David Woodhouseaa21c002007-12-08 20:04:36 +00001413 assoc_req->ssid_len = priv->curbssparams.ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001414 }
1415
1416 if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001417 assoc_req->channel = priv->curbssparams.channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001418
Dan Williamse76850d2007-05-25 17:09:41 -04001419 if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001420 assoc_req->band = priv->curbssparams.band;
Dan Williamse76850d2007-05-25 17:09:41 -04001421
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001422 if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001423 assoc_req->mode = priv->mode;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001424
1425 if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001426 memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001427 ETH_ALEN);
1428 }
1429
1430 if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
1431 int i;
1432 for (i = 0; i < 4; i++) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001433 memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
Dan Williams1443b652007-08-02 10:45:55 -04001434 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001435 }
1436 }
1437
1438 if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
David Woodhouseaa21c002007-12-08 20:04:36 +00001439 assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001440
1441 if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001442 memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001443 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001444 }
1445
1446 if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001447 memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
Dan Williams1443b652007-08-02 10:45:55 -04001448 sizeof(struct enc_key));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001449 }
1450
1451 if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001452 memcpy(&assoc_req->secinfo, &priv->secinfo,
Holger Schurig10078322007-11-15 18:05:47 -05001453 sizeof(struct lbs_802_11_security));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001454 }
1455
1456 if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
David Woodhouseaa21c002007-12-08 20:04:36 +00001457 memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001458 MAX_WPA_IE_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +00001459 assoc_req->wpa_ie_len = priv->wpa_ie_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001460 }
1461
Holger Schurig0765af42007-10-15 12:55:56 +02001462 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001463 return assoc_req;
1464}
Holger Schurig697900a2008-04-02 16:27:10 +02001465
1466
1467/**
Holger Schurig697900a2008-04-02 16:27:10 +02001468 * @brief This function prepares command of authenticate.
1469 *
1470 * @param priv A pointer to struct lbs_private structure
1471 * @param cmd A pointer to cmd_ds_command structure
1472 * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
1473 *
1474 * @return 0 or -1
1475 */
1476int lbs_cmd_80211_authenticate(struct lbs_private *priv,
1477 struct cmd_ds_command *cmd,
1478 void *pdata_buf)
1479{
1480 struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
1481 int ret = -1;
1482 u8 *bssid = pdata_buf;
Holger Schurig697900a2008-04-02 16:27:10 +02001483
1484 lbs_deb_enter(LBS_DEB_JOIN);
1485
1486 cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
1487 cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
1488 + S_DS_GEN);
1489
1490 /* translate auth mode to 802.11 defined wire value */
1491 switch (priv->secinfo.auth_mode) {
1492 case IW_AUTH_ALG_OPEN_SYSTEM:
1493 pauthenticate->authtype = 0x00;
1494 break;
1495 case IW_AUTH_ALG_SHARED_KEY:
1496 pauthenticate->authtype = 0x01;
1497 break;
1498 case IW_AUTH_ALG_LEAP:
1499 pauthenticate->authtype = 0x80;
1500 break;
1501 default:
1502 lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
1503 priv->secinfo.auth_mode);
1504 goto out;
1505 }
1506
1507 memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
1508
Johannes Berge1749612008-10-27 15:59:26 -07001509 lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
1510 bssid, pauthenticate->authtype);
Holger Schurig697900a2008-04-02 16:27:10 +02001511 ret = 0;
1512
1513out:
1514 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1515 return ret;
1516}
1517
Dan Williams191bb402008-08-21 17:46:18 -04001518/**
1519 * @brief Deauthenticate from a specific BSS
1520 *
1521 * @param priv A pointer to struct lbs_private structure
1522 * @param bssid The specific BSS to deauthenticate from
1523 * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
1524 *
1525 * @return 0 on success, error on failure
1526 */
1527int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
1528 u16 reason)
Holger Schurig697900a2008-04-02 16:27:10 +02001529{
Dan Williams191bb402008-08-21 17:46:18 -04001530 struct cmd_ds_802_11_deauthenticate cmd;
1531 int ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001532
1533 lbs_deb_enter(LBS_DEB_JOIN);
1534
Dan Williams191bb402008-08-21 17:46:18 -04001535 memset(&cmd, 0, sizeof(cmd));
1536 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1537 memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
1538 cmd.reasoncode = cpu_to_le16(reason);
Holger Schurig697900a2008-04-02 16:27:10 +02001539
Dan Williams191bb402008-08-21 17:46:18 -04001540 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
Holger Schurig697900a2008-04-02 16:27:10 +02001541
Dan Williams191bb402008-08-21 17:46:18 -04001542 /* Clean up everything even if there was an error; can't assume that
1543 * we're still authenticated to the AP after trying to deauth.
1544 */
1545 lbs_mac_event_disconnected(priv);
Holger Schurig697900a2008-04-02 16:27:10 +02001546
1547 lbs_deb_leave(LBS_DEB_JOIN);
Dan Williams191bb402008-08-21 17:46:18 -04001548 return ret;
Holger Schurig697900a2008-04-02 16:27:10 +02001549}
1550
1551int lbs_cmd_80211_associate(struct lbs_private *priv,
1552 struct cmd_ds_command *cmd, void *pdata_buf)
1553{
1554 struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
1555 int ret = 0;
1556 struct assoc_request *assoc_req = pdata_buf;
1557 struct bss_descriptor *bss = &assoc_req->bss;
1558 u8 *pos;
1559 u16 tmpcap, tmplen;
1560 struct mrvlietypes_ssidparamset *ssid;
1561 struct mrvlietypes_phyparamset *phy;
1562 struct mrvlietypes_ssparamset *ss;
1563 struct mrvlietypes_ratesparamset *rates;
1564 struct mrvlietypes_rsnparamset *rsn;
1565
1566 lbs_deb_enter(LBS_DEB_ASSOC);
1567
1568 pos = (u8 *) passo;
1569
1570 if (!priv) {
1571 ret = -1;
1572 goto done;
1573 }
1574
1575 cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1576
1577 memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
1578 pos += sizeof(passo->peerstaaddr);
1579
1580 /* set the listen interval */
1581 passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1582
1583 pos += sizeof(passo->capability);
1584 pos += sizeof(passo->listeninterval);
1585 pos += sizeof(passo->bcnperiod);
1586 pos += sizeof(passo->dtimperiod);
1587
1588 ssid = (struct mrvlietypes_ssidparamset *) pos;
1589 ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
1590 tmplen = bss->ssid_len;
1591 ssid->header.len = cpu_to_le16(tmplen);
1592 memcpy(ssid->ssid, bss->ssid, tmplen);
1593 pos += sizeof(ssid->header) + tmplen;
1594
1595 phy = (struct mrvlietypes_phyparamset *) pos;
1596 phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
1597 tmplen = sizeof(phy->fh_ds.dsparamset);
1598 phy->header.len = cpu_to_le16(tmplen);
1599 memcpy(&phy->fh_ds.dsparamset,
1600 &bss->phyparamset.dsparamset.currentchan,
1601 tmplen);
1602 pos += sizeof(phy->header) + tmplen;
1603
1604 ss = (struct mrvlietypes_ssparamset *) pos;
1605 ss->header.type = cpu_to_le16(TLV_TYPE_CF);
1606 tmplen = sizeof(ss->cf_ibss.cfparamset);
1607 ss->header.len = cpu_to_le16(tmplen);
1608 pos += sizeof(ss->header) + tmplen;
1609
1610 rates = (struct mrvlietypes_ratesparamset *) pos;
1611 rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
1612 memcpy(&rates->rates, &bss->rates, MAX_RATES);
1613 tmplen = MAX_RATES;
1614 if (get_common_rates(priv, rates->rates, &tmplen)) {
1615 ret = -1;
1616 goto done;
1617 }
1618 pos += sizeof(rates->header) + tmplen;
1619 rates->header.len = cpu_to_le16(tmplen);
1620 lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
1621
1622 /* Copy the infra. association rates into Current BSS state structure */
1623 memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
1624 memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
1625
1626 /* Set MSB on basic rates as the firmware requires, but _after_
1627 * copying to current bss rates.
1628 */
1629 lbs_set_basic_rate_flags(rates->rates, tmplen);
1630
1631 if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1632 rsn = (struct mrvlietypes_rsnparamset *) pos;
1633 /* WPA_IE or WPA2_IE */
1634 rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
1635 tmplen = (u16) assoc_req->wpa_ie[1];
1636 rsn->header.len = cpu_to_le16(tmplen);
1637 memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
1638 lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
1639 sizeof(rsn->header) + tmplen);
1640 pos += sizeof(rsn->header) + tmplen;
1641 }
1642
1643 /* update curbssparams */
1644 priv->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
1645
1646 if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
1647 ret = -1;
1648 goto done;
1649 }
1650
1651 cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
1652
1653 /* set the capability info */
1654 tmpcap = (bss->capability & CAPINFO_MASK);
1655 if (bss->mode == IW_MODE_INFRA)
1656 tmpcap |= WLAN_CAPABILITY_ESS;
1657 passo->capability = cpu_to_le16(tmpcap);
1658 lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
1659
1660done:
1661 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1662 return ret;
1663}
1664
Holger Schurig697900a2008-04-02 16:27:10 +02001665int lbs_ret_80211_associate(struct lbs_private *priv,
1666 struct cmd_ds_command *resp)
1667{
1668 int ret = 0;
1669 union iwreq_data wrqu;
1670 struct ieeetypes_assocrsp *passocrsp;
1671 struct bss_descriptor *bss;
1672 u16 status_code;
1673
1674 lbs_deb_enter(LBS_DEB_ASSOC);
1675
1676 if (!priv->in_progress_assoc_req) {
1677 lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
1678 ret = -1;
1679 goto done;
1680 }
1681 bss = &priv->in_progress_assoc_req->bss;
1682
1683 passocrsp = (struct ieeetypes_assocrsp *) &resp->params;
1684
1685 /*
1686 * Older FW versions map the IEEE 802.11 Status Code in the association
1687 * response to the following values returned in passocrsp->statuscode:
1688 *
1689 * IEEE Status Code Marvell Status Code
1690 * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
1691 * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1692 * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1693 * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1694 * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
1695 * others -> 0x0003 ASSOC_RESULT_REFUSED
1696 *
1697 * Other response codes:
1698 * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1699 * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1700 * association response from the AP)
1701 */
1702
1703 status_code = le16_to_cpu(passocrsp->statuscode);
1704 switch (status_code) {
1705 case 0x00:
1706 break;
1707 case 0x01:
1708 lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
1709 break;
1710 case 0x02:
1711 lbs_deb_assoc("ASSOC_RESP: internal timer "
1712 "expired while waiting for the AP\n");
1713 break;
1714 case 0x03:
1715 lbs_deb_assoc("ASSOC_RESP: association "
1716 "refused by AP\n");
1717 break;
1718 case 0x04:
1719 lbs_deb_assoc("ASSOC_RESP: authentication "
1720 "refused by AP\n");
1721 break;
1722 default:
1723 lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
1724 " unknown\n", status_code);
1725 break;
1726 }
1727
1728 if (status_code) {
1729 lbs_mac_event_disconnected(priv);
1730 ret = -1;
1731 goto done;
1732 }
1733
1734 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
1735 le16_to_cpu(resp->size) - S_DS_GEN);
1736
1737 /* Send a Media Connected event, according to the Spec */
1738 priv->connect_status = LBS_CONNECTED;
1739
1740 /* Update current SSID and BSSID */
1741 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1742 priv->curbssparams.ssid_len = bss->ssid_len;
1743 memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1744
1745 priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
1746 priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
1747
1748 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
1749 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
1750 priv->nextSNRNF = 0;
1751 priv->numSNRNF = 0;
1752
1753 netif_carrier_on(priv->dev);
1754 if (!priv->tx_pending_len)
1755 netif_wake_queue(priv->dev);
1756
1757 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1758 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1759 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1760
1761done:
1762 lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1763 return ret;
1764}
1765
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001766static int lbs_adhoc_post(struct lbs_private *priv, struct cmd_header *resp)
Holger Schurig697900a2008-04-02 16:27:10 +02001767{
1768 int ret = 0;
1769 u16 command = le16_to_cpu(resp->command);
1770 u16 result = le16_to_cpu(resp->result);
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001771 struct cmd_ds_802_11_ad_hoc_result *adhoc_resp;
Holger Schurig697900a2008-04-02 16:27:10 +02001772 union iwreq_data wrqu;
1773 struct bss_descriptor *bss;
John W. Linville9387b7c2008-09-30 20:59:05 -04001774 DECLARE_SSID_BUF(ssid);
Holger Schurig697900a2008-04-02 16:27:10 +02001775
1776 lbs_deb_enter(LBS_DEB_JOIN);
1777
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001778 adhoc_resp = (struct cmd_ds_802_11_ad_hoc_result *) resp;
Holger Schurig697900a2008-04-02 16:27:10 +02001779
1780 if (!priv->in_progress_assoc_req) {
1781 lbs_deb_join("ADHOC_RESP: no in-progress association "
1782 "request\n");
1783 ret = -1;
1784 goto done;
1785 }
1786 bss = &priv->in_progress_assoc_req->bss;
1787
1788 /*
1789 * Join result code 0 --> SUCCESS
1790 */
1791 if (result) {
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001792 lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
Holger Schurig697900a2008-04-02 16:27:10 +02001793 if (priv->connect_status == LBS_CONNECTED)
1794 lbs_mac_event_disconnected(priv);
1795 ret = -1;
1796 goto done;
1797 }
1798
Holger Schurig697900a2008-04-02 16:27:10 +02001799 /* Send a Media Connected event, according to the Spec */
1800 priv->connect_status = LBS_CONNECTED;
1801
1802 if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
1803 /* Update the created network descriptor with the new BSSID */
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001804 memcpy(bss->bssid, adhoc_resp->bssid, ETH_ALEN);
Holger Schurig697900a2008-04-02 16:27:10 +02001805 }
1806
1807 /* Set the BSSID from the joined/started descriptor */
1808 memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
1809
1810 /* Set the new SSID to current SSID */
1811 memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
1812 priv->curbssparams.ssid_len = bss->ssid_len;
1813
1814 netif_carrier_on(priv->dev);
1815 if (!priv->tx_pending_len)
1816 netif_wake_queue(priv->dev);
1817
1818 memset(&wrqu, 0, sizeof(wrqu));
1819 memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
1820 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1821 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1822
Johannes Berge1749612008-10-27 15:59:26 -07001823 lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001824 print_ssid(ssid, bss->ssid, bss->ssid_len),
Johannes Berge1749612008-10-27 15:59:26 -07001825 priv->curbssparams.bssid,
Dan Williamsf5fe1fd2008-08-21 21:46:59 -04001826 priv->curbssparams.channel);
Holger Schurig697900a2008-04-02 16:27:10 +02001827
1828done:
1829 lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
1830 return ret;
1831}
1832