• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Implement cfg80211 ("iw") support.
4  *
5  * Copyright (C) 2009 M&N Solutions GmbH, 61191 Rosbach, Germany
6  * Holger Schurig <hs4233@mail.mn-solutions.de>
7  *
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/hardirq.h>
13 #include <linux/sched.h>
14 #include <linux/wait.h>
15 #include <linux/slab.h>
16 #include <linux/ieee80211.h>
17 #include <net/cfg80211.h>
18 #include <linux/unaligned.h>
19 
20 #include "decl.h"
21 #include "cfg.h"
22 #include "cmd.h"
23 #include "mesh.h"
24 
25 
26 #define CHAN2G(_channel, _freq, _flags) {        \
27 	.band             = NL80211_BAND_2GHZ, \
28 	.center_freq      = (_freq),             \
29 	.hw_value         = (_channel),          \
30 	.flags            = (_flags),            \
31 	.max_antenna_gain = 0,                   \
32 	.max_power        = 30,                  \
33 }
34 
35 static struct ieee80211_channel lbs_2ghz_channels[] = {
36 	CHAN2G(1,  2412, 0),
37 	CHAN2G(2,  2417, 0),
38 	CHAN2G(3,  2422, 0),
39 	CHAN2G(4,  2427, 0),
40 	CHAN2G(5,  2432, 0),
41 	CHAN2G(6,  2437, 0),
42 	CHAN2G(7,  2442, 0),
43 	CHAN2G(8,  2447, 0),
44 	CHAN2G(9,  2452, 0),
45 	CHAN2G(10, 2457, 0),
46 	CHAN2G(11, 2462, 0),
47 	CHAN2G(12, 2467, 0),
48 	CHAN2G(13, 2472, 0),
49 	CHAN2G(14, 2484, 0),
50 };
51 
52 #define RATETAB_ENT(_rate, _hw_value, _flags) { \
53 	.bitrate  = (_rate),                    \
54 	.hw_value = (_hw_value),                \
55 	.flags    = (_flags),                   \
56 }
57 
58 
59 /* Table 6 in section 3.2.1.1 */
60 static struct ieee80211_rate lbs_rates[] = {
61 	RATETAB_ENT(10,  0,  0),
62 	RATETAB_ENT(20,  1,  0),
63 	RATETAB_ENT(55,  2,  0),
64 	RATETAB_ENT(110, 3,  0),
65 	RATETAB_ENT(60,  9,  0),
66 	RATETAB_ENT(90,  6,  0),
67 	RATETAB_ENT(120, 7,  0),
68 	RATETAB_ENT(180, 8,  0),
69 	RATETAB_ENT(240, 9,  0),
70 	RATETAB_ENT(360, 10, 0),
71 	RATETAB_ENT(480, 11, 0),
72 	RATETAB_ENT(540, 12, 0),
73 };
74 
75 static struct ieee80211_supported_band lbs_band_2ghz = {
76 	.channels = lbs_2ghz_channels,
77 	.n_channels = ARRAY_SIZE(lbs_2ghz_channels),
78 	.bitrates = lbs_rates,
79 	.n_bitrates = ARRAY_SIZE(lbs_rates),
80 };
81 
82 
83 static const u32 cipher_suites[] = {
84 	WLAN_CIPHER_SUITE_WEP40,
85 	WLAN_CIPHER_SUITE_WEP104,
86 	WLAN_CIPHER_SUITE_TKIP,
87 	WLAN_CIPHER_SUITE_CCMP,
88 };
89 
90 /* Time to stay on the channel */
91 #define LBS_DWELL_PASSIVE 100
92 #define LBS_DWELL_ACTIVE  40
93 
94 
95 /***************************************************************************
96  * Misc utility functions
97  *
98  * TLVs are Marvell specific. They are very similar to IEs, they have the
99  * same structure: type, length, data*. The only difference: for IEs, the
100  * type and length are u8, but for TLVs they're __le16.
101  */
102 
103 /*
104  * Convert NL80211's auth_type to the one from Libertas, see chapter 5.9.1
105  * in the firmware spec
106  */
lbs_auth_to_authtype(enum nl80211_auth_type auth_type)107 static int lbs_auth_to_authtype(enum nl80211_auth_type auth_type)
108 {
109 	int ret = -ENOTSUPP;
110 
111 	switch (auth_type) {
112 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
113 	case NL80211_AUTHTYPE_SHARED_KEY:
114 		ret = auth_type;
115 		break;
116 	case NL80211_AUTHTYPE_AUTOMATIC:
117 		ret = NL80211_AUTHTYPE_OPEN_SYSTEM;
118 		break;
119 	case NL80211_AUTHTYPE_NETWORK_EAP:
120 		ret = 0x80;
121 		break;
122 	default:
123 		/* silence compiler */
124 		break;
125 	}
126 	return ret;
127 }
128 
129 
130 /*
131  * Various firmware commands need the list of supported rates, but with
132  * the hight-bit set for basic rates
133  */
lbs_add_rates(u8 * rates)134 static int lbs_add_rates(u8 *rates)
135 {
136 	size_t i;
137 
138 	for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
139 		u8 rate = lbs_rates[i].bitrate / 5;
140 		if (rate == 0x02 || rate == 0x04 ||
141 		    rate == 0x0b || rate == 0x16)
142 			rate |= 0x80;
143 		rates[i] = rate;
144 	}
145 	return ARRAY_SIZE(lbs_rates);
146 }
147 
148 
149 /***************************************************************************
150  * TLV utility functions
151  *
152  * TLVs are Marvell specific. They are very similar to IEs, they have the
153  * same structure: type, length, data*. The only difference: for IEs, the
154  * type and length are u8, but for TLVs they're __le16.
155  */
156 
157 
158 /*
159  * Add ssid TLV
160  */
161 #define LBS_MAX_SSID_TLV_SIZE			\
162 	(sizeof(struct mrvl_ie_header)		\
163 	 + IEEE80211_MAX_SSID_LEN)
164 
lbs_add_ssid_tlv(u8 * tlv,const u8 * ssid,int ssid_len)165 static int lbs_add_ssid_tlv(u8 *tlv, const u8 *ssid, int ssid_len)
166 {
167 	struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
168 
169 	/*
170 	 * TLV-ID SSID  00 00
171 	 * length       06 00
172 	 * ssid         4d 4e 54 45 53 54
173 	 */
174 	ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
175 	ssid_tlv->header.len = cpu_to_le16(ssid_len);
176 	memcpy(ssid_tlv->ssid, ssid, ssid_len);
177 	return sizeof(ssid_tlv->header) + ssid_len;
178 }
179 
180 
181 /*
182  * Add channel list TLV (section 8.4.2)
183  *
184  * Actual channel data comes from priv->wdev->wiphy->channels.
185  */
186 #define LBS_MAX_CHANNEL_LIST_TLV_SIZE					\
187 	(sizeof(struct mrvl_ie_header)					\
188 	 + (LBS_SCAN_BEFORE_NAP * sizeof(struct chanscanparamset)))
189 
lbs_add_channel_list_tlv(struct lbs_private * priv,u8 * tlv,int last_channel,int active_scan)190 static int lbs_add_channel_list_tlv(struct lbs_private *priv, u8 *tlv,
191 				    int last_channel, int active_scan)
192 {
193 	int chanscanparamsize = sizeof(struct chanscanparamset) *
194 		(last_channel - priv->scan_channel);
195 
196 	struct mrvl_ie_header *header = (void *) tlv;
197 
198 	/*
199 	 * TLV-ID CHANLIST  01 01
200 	 * length           0e 00
201 	 * channel          00 01 00 00 00 64 00
202 	 *   radio type     00
203 	 *   channel           01
204 	 *   scan type            00
205 	 *   min scan time           00 00
206 	 *   max scan time                 64 00
207 	 * channel 2        00 02 00 00 00 64 00
208 	 *
209 	 */
210 
211 	header->type = cpu_to_le16(TLV_TYPE_CHANLIST);
212 	header->len  = cpu_to_le16(chanscanparamsize);
213 	tlv += sizeof(struct mrvl_ie_header);
214 
215 	/* lbs_deb_scan("scan: channels %d to %d\n", priv->scan_channel,
216 		     last_channel); */
217 	memset(tlv, 0, chanscanparamsize);
218 
219 	while (priv->scan_channel < last_channel) {
220 		struct chanscanparamset *param = (void *) tlv;
221 
222 		param->radiotype = CMD_SCAN_RADIO_TYPE_BG;
223 		param->channumber =
224 			priv->scan_req->channels[priv->scan_channel]->hw_value;
225 		if (active_scan) {
226 			param->maxscantime = cpu_to_le16(LBS_DWELL_ACTIVE);
227 		} else {
228 			param->chanscanmode.passivescan = 1;
229 			param->maxscantime = cpu_to_le16(LBS_DWELL_PASSIVE);
230 		}
231 		tlv += sizeof(struct chanscanparamset);
232 		priv->scan_channel++;
233 	}
234 	return sizeof(struct mrvl_ie_header) + chanscanparamsize;
235 }
236 
237 
238 /*
239  * Add rates TLV
240  *
241  * The rates are in lbs_bg_rates[], but for the 802.11b
242  * rates the high bit is set. We add this TLV only because
243  * there's a firmware which otherwise doesn't report all
244  * APs in range.
245  */
246 #define LBS_MAX_RATES_TLV_SIZE			\
247 	(sizeof(struct mrvl_ie_header)		\
248 	 + (ARRAY_SIZE(lbs_rates)))
249 
250 /* Adds a TLV with all rates the hardware supports */
lbs_add_supported_rates_tlv(u8 * tlv)251 static int lbs_add_supported_rates_tlv(u8 *tlv)
252 {
253 	size_t i;
254 	struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
255 
256 	/*
257 	 * TLV-ID RATES  01 00
258 	 * length        0e 00
259 	 * rates         82 84 8b 96 0c 12 18 24 30 48 60 6c
260 	 */
261 	rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
262 	tlv += sizeof(rate_tlv->header);
263 	i = lbs_add_rates(tlv);
264 	tlv += i;
265 	rate_tlv->header.len = cpu_to_le16(i);
266 	return sizeof(rate_tlv->header) + i;
267 }
268 
269 /* Add common rates from a TLV and return the new end of the TLV */
270 static u8 *
add_ie_rates(u8 * tlv,const u8 * ie,int * nrates)271 add_ie_rates(u8 *tlv, const u8 *ie, int *nrates)
272 {
273 	int hw, ap, ap_max = ie[1];
274 	u8 hw_rate;
275 
276 	if (ap_max > MAX_RATES) {
277 		lbs_deb_assoc("invalid rates\n");
278 		return tlv;
279 	}
280 	/* Advance past IE header */
281 	ie += 2;
282 
283 	lbs_deb_hex(LBS_DEB_ASSOC, "AP IE Rates", (u8 *) ie, ap_max);
284 
285 	for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
286 		hw_rate = lbs_rates[hw].bitrate / 5;
287 		for (ap = 0; ap < ap_max; ap++) {
288 			if (hw_rate == (ie[ap] & 0x7f)) {
289 				*tlv++ = ie[ap];
290 				*nrates = *nrates + 1;
291 			}
292 		}
293 	}
294 	return tlv;
295 }
296 
297 /*
298  * Adds a TLV with all rates the hardware *and* BSS supports.
299  */
lbs_add_common_rates_tlv(u8 * tlv,struct cfg80211_bss * bss)300 static int lbs_add_common_rates_tlv(u8 *tlv, struct cfg80211_bss *bss)
301 {
302 	struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
303 	const u8 *rates_eid, *ext_rates_eid;
304 	int n = 0;
305 
306 	rcu_read_lock();
307 	rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
308 	ext_rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
309 
310 	/*
311 	 * 01 00                   TLV_TYPE_RATES
312 	 * 04 00                   len
313 	 * 82 84 8b 96             rates
314 	 */
315 	rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
316 	tlv += sizeof(rate_tlv->header);
317 
318 	/* Add basic rates */
319 	if (rates_eid) {
320 		tlv = add_ie_rates(tlv, rates_eid, &n);
321 
322 		/* Add extended rates, if any */
323 		if (ext_rates_eid)
324 			tlv = add_ie_rates(tlv, ext_rates_eid, &n);
325 	} else {
326 		lbs_deb_assoc("assoc: bss had no basic rate IE\n");
327 		/* Fallback: add basic 802.11b rates */
328 		*tlv++ = 0x82;
329 		*tlv++ = 0x84;
330 		*tlv++ = 0x8b;
331 		*tlv++ = 0x96;
332 		n = 4;
333 	}
334 	rcu_read_unlock();
335 
336 	rate_tlv->header.len = cpu_to_le16(n);
337 	return sizeof(rate_tlv->header) + n;
338 }
339 
340 
341 /*
342  * Add auth type TLV.
343  *
344  * This is only needed for newer firmware (V9 and up).
345  */
346 #define LBS_MAX_AUTH_TYPE_TLV_SIZE \
347 	sizeof(struct mrvl_ie_auth_type)
348 
lbs_add_auth_type_tlv(u8 * tlv,enum nl80211_auth_type auth_type)349 static int lbs_add_auth_type_tlv(u8 *tlv, enum nl80211_auth_type auth_type)
350 {
351 	struct mrvl_ie_auth_type *auth = (void *) tlv;
352 
353 	/*
354 	 * 1f 01  TLV_TYPE_AUTH_TYPE
355 	 * 01 00  len
356 	 * 01     auth type
357 	 */
358 	auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
359 	auth->header.len = cpu_to_le16(sizeof(*auth)-sizeof(auth->header));
360 	auth->auth = cpu_to_le16(lbs_auth_to_authtype(auth_type));
361 	return sizeof(*auth);
362 }
363 
364 
365 /*
366  * Add channel (phy ds) TLV
367  */
368 #define LBS_MAX_CHANNEL_TLV_SIZE \
369 	sizeof(struct mrvl_ie_header)
370 
lbs_add_channel_tlv(u8 * tlv,u8 channel)371 static int lbs_add_channel_tlv(u8 *tlv, u8 channel)
372 {
373 	struct mrvl_ie_ds_param_set *ds = (void *) tlv;
374 
375 	/*
376 	 * 03 00  TLV_TYPE_PHY_DS
377 	 * 01 00  len
378 	 * 06     channel
379 	 */
380 	ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
381 	ds->header.len = cpu_to_le16(sizeof(*ds)-sizeof(ds->header));
382 	ds->channel = channel;
383 	return sizeof(*ds);
384 }
385 
386 
387 /*
388  * Add (empty) CF param TLV of the form:
389  */
390 #define LBS_MAX_CF_PARAM_TLV_SIZE		\
391 	sizeof(struct mrvl_ie_header)
392 
lbs_add_cf_param_tlv(u8 * tlv)393 static int lbs_add_cf_param_tlv(u8 *tlv)
394 {
395 	struct mrvl_ie_cf_param_set *cf = (void *)tlv;
396 
397 	/*
398 	 * 04 00  TLV_TYPE_CF
399 	 * 06 00  len
400 	 * 00     cfpcnt
401 	 * 00     cfpperiod
402 	 * 00 00  cfpmaxduration
403 	 * 00 00  cfpdurationremaining
404 	 */
405 	cf->header.type = cpu_to_le16(TLV_TYPE_CF);
406 	cf->header.len = cpu_to_le16(sizeof(*cf)-sizeof(cf->header));
407 	return sizeof(*cf);
408 }
409 
410 /*
411  * Add WPA TLV
412  */
413 #define LBS_MAX_WPA_TLV_SIZE			\
414 	(sizeof(struct mrvl_ie_header)		\
415 	 + 128 /* TODO: I guessed the size */)
416 
lbs_add_wpa_tlv(u8 * tlv,const u8 * ie,u8 ie_len)417 static int lbs_add_wpa_tlv(u8 *tlv, const u8 *ie, u8 ie_len)
418 {
419 	struct mrvl_ie_data *wpatlv = (struct mrvl_ie_data *)tlv;
420 	const struct element *wpaie;
421 
422 	/* Find the first RSN or WPA IE to use */
423 	wpaie = cfg80211_find_elem(WLAN_EID_RSN, ie, ie_len);
424 	if (!wpaie)
425 		wpaie = cfg80211_find_vendor_elem(WLAN_OUI_MICROSOFT,
426 						  WLAN_OUI_TYPE_MICROSOFT_WPA,
427 						  ie, ie_len);
428 	if (!wpaie || wpaie->datalen > 128)
429 		return 0;
430 
431 	/*
432 	 * Convert the found IE to a TLV. IEs use u8 for the header,
433 	 *   u8      type
434 	 *   u8      len
435 	 *   u8[]    data
436 	 * but TLVs use __le16 instead:
437 	 *   __le16  type
438 	 *   __le16  len
439 	 *   u8[]    data
440 	 */
441 	wpatlv->header.type = cpu_to_le16(wpaie->id);
442 	wpatlv->header.len = cpu_to_le16(wpaie->datalen);
443 	memcpy(wpatlv->data, wpaie->data, wpaie->datalen);
444 
445 	/* Return the total number of bytes added to the TLV buffer */
446 	return sizeof(struct mrvl_ie_header) + wpaie->datalen;
447 }
448 
449 /* Add WPS enrollee TLV
450  */
451 #define LBS_MAX_WPS_ENROLLEE_TLV_SIZE		\
452 	(sizeof(struct mrvl_ie_header)		\
453 	 + 256)
454 
lbs_add_wps_enrollee_tlv(u8 * tlv,const u8 * ie,size_t ie_len)455 static int lbs_add_wps_enrollee_tlv(u8 *tlv, const u8 *ie, size_t ie_len)
456 {
457 	struct mrvl_ie_data *wpstlv = (struct mrvl_ie_data *)tlv;
458 	const struct element *wpsie;
459 
460 	/* Look for a WPS IE and add it to the probe request */
461 	wpsie = cfg80211_find_vendor_elem(WLAN_OUI_MICROSOFT,
462 					  WLAN_OUI_TYPE_MICROSOFT_WPS,
463 					  ie, ie_len);
464 	if (!wpsie)
465 		return 0;
466 
467 	/* Convert the WPS IE to a TLV. The IE looks like this:
468 	 *   u8      type (WLAN_EID_VENDOR_SPECIFIC)
469 	 *   u8      len
470 	 *   u8[]    data
471 	 * but the TLV will look like this instead:
472 	 *   __le16  type (TLV_TYPE_WPS_ENROLLEE)
473 	 *   __le16  len
474 	 *   u8[]    data
475 	 */
476 	wpstlv->header.type = cpu_to_le16(TLV_TYPE_WPS_ENROLLEE);
477 	wpstlv->header.len = cpu_to_le16(wpsie->datalen);
478 	memcpy(wpstlv->data, wpsie->data, wpsie->datalen);
479 
480 	/* Return the total number of bytes added to the TLV buffer */
481 	return sizeof(struct mrvl_ie_header) + wpsie->datalen;
482 }
483 
484 /*
485  * Set Channel
486  */
487 
lbs_cfg_set_monitor_channel(struct wiphy * wiphy,struct cfg80211_chan_def * chandef)488 static int lbs_cfg_set_monitor_channel(struct wiphy *wiphy,
489 				       struct cfg80211_chan_def *chandef)
490 {
491 	struct lbs_private *priv = wiphy_priv(wiphy);
492 	int ret = -ENOTSUPP;
493 
494 	if (cfg80211_get_chandef_type(chandef) != NL80211_CHAN_NO_HT)
495 		goto out;
496 
497 	ret = lbs_set_channel(priv, chandef->chan->hw_value);
498 
499  out:
500 	return ret;
501 }
502 
lbs_cfg_set_mesh_channel(struct wiphy * wiphy,struct net_device * netdev,struct ieee80211_channel * channel)503 static int lbs_cfg_set_mesh_channel(struct wiphy *wiphy,
504 				    struct net_device *netdev,
505 				    struct ieee80211_channel *channel)
506 {
507 	struct lbs_private *priv = wiphy_priv(wiphy);
508 	int ret = -ENOTSUPP;
509 
510 	if (netdev != priv->mesh_dev)
511 		goto out;
512 
513 	ret = lbs_mesh_set_channel(priv, channel->hw_value);
514 
515  out:
516 	return ret;
517 }
518 
519 
520 
521 /*
522  * Scanning
523  */
524 
525 /*
526  * When scanning, the firmware doesn't send a nul packet with the power-safe
527  * bit to the AP. So we cannot stay away from our current channel too long,
528  * otherwise we loose data. So take a "nap" while scanning every other
529  * while.
530  */
531 #define LBS_SCAN_BEFORE_NAP 4
532 
533 
534 /*
535  * When the firmware reports back a scan-result, it gives us an "u8 rssi",
536  * which isn't really an RSSI, as it becomes larger when moving away from
537  * the AP. Anyway, we need to convert that into mBm.
538  */
539 #define LBS_SCAN_RSSI_TO_MBM(rssi) \
540 	((-(int)rssi + 3)*100)
541 
lbs_ret_scan(struct lbs_private * priv,unsigned long dummy,struct cmd_header * resp)542 static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
543 	struct cmd_header *resp)
544 {
545 	struct cfg80211_bss *bss;
546 	struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
547 	int bsssize;
548 	const u8 *pos;
549 	const u8 *tsfdesc;
550 	int tsfsize;
551 	int i;
552 	int ret = -EILSEQ;
553 
554 	bsssize = get_unaligned_le16(&scanresp->bssdescriptsize);
555 
556 	lbs_deb_scan("scan response: %d BSSs (%d bytes); resp size %d bytes\n",
557 			scanresp->nr_sets, bsssize, le16_to_cpu(resp->size));
558 
559 	if (scanresp->nr_sets == 0) {
560 		ret = 0;
561 		goto done;
562 	}
563 
564 	/*
565 	 * The general layout of the scan response is described in chapter
566 	 * 5.7.1. Basically we have a common part, then any number of BSS
567 	 * descriptor sections. Finally we have section with the same number
568 	 * of TSFs.
569 	 *
570 	 * cmd_ds_802_11_scan_rsp
571 	 *   cmd_header
572 	 *   pos_size
573 	 *   nr_sets
574 	 *   bssdesc 1
575 	 *     bssid
576 	 *     rssi
577 	 *     timestamp
578 	 *     intvl
579 	 *     capa
580 	 *     IEs
581 	 *   bssdesc 2
582 	 *   bssdesc n
583 	 *   MrvlIEtypes_TsfFimestamp_t
584 	 *     TSF for BSS 1
585 	 *     TSF for BSS 2
586 	 *     TSF for BSS n
587 	 */
588 
589 	pos = scanresp->bssdesc_and_tlvbuffer;
590 
591 	lbs_deb_hex(LBS_DEB_SCAN, "SCAN_RSP", scanresp->bssdesc_and_tlvbuffer,
592 		    bsssize);
593 
594 	tsfdesc = pos + bsssize;
595 	tsfsize = 4 + 8 * scanresp->nr_sets;
596 	lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TSF", (u8 *) tsfdesc, tsfsize);
597 
598 	/* Validity check: we expect a Marvell-Local TLV */
599 	i = get_unaligned_le16(tsfdesc);
600 	tsfdesc += 2;
601 	if (i != TLV_TYPE_TSFTIMESTAMP) {
602 		lbs_deb_scan("scan response: invalid TSF Timestamp %d\n", i);
603 		goto done;
604 	}
605 
606 	/*
607 	 * Validity check: the TLV holds TSF values with 8 bytes each, so
608 	 * the size in the TLV must match the nr_sets value
609 	 */
610 	i = get_unaligned_le16(tsfdesc);
611 	tsfdesc += 2;
612 	if (i / 8 != scanresp->nr_sets) {
613 		lbs_deb_scan("scan response: invalid number of TSF timestamp "
614 			     "sets (expected %d got %d)\n", scanresp->nr_sets,
615 			     i / 8);
616 		goto done;
617 	}
618 
619 	for (i = 0; i < scanresp->nr_sets; i++) {
620 		const u8 *bssid;
621 		const u8 *ie;
622 		int left;
623 		int ielen;
624 		int rssi;
625 		u16 intvl;
626 		u16 capa;
627 		int chan_no = -1;
628 		const u8 *ssid = NULL;
629 		u8 ssid_len = 0;
630 
631 		int len = get_unaligned_le16(pos);
632 		pos += 2;
633 
634 		/* BSSID */
635 		bssid = pos;
636 		pos += ETH_ALEN;
637 		/* RSSI */
638 		rssi = *pos++;
639 		/* Packet time stamp */
640 		pos += 8;
641 		/* Beacon interval */
642 		intvl = get_unaligned_le16(pos);
643 		pos += 2;
644 		/* Capabilities */
645 		capa = get_unaligned_le16(pos);
646 		pos += 2;
647 
648 		/* To find out the channel, we must parse the IEs */
649 		ie = pos;
650 		/*
651 		 * 6+1+8+2+2: size of BSSID, RSSI, time stamp, beacon
652 		 * interval, capabilities
653 		 */
654 		ielen = left = len - (6 + 1 + 8 + 2 + 2);
655 		while (left >= 2) {
656 			u8 id, elen;
657 			id = *pos++;
658 			elen = *pos++;
659 			left -= 2;
660 			if (elen > left) {
661 				lbs_deb_scan("scan response: invalid IE fmt\n");
662 				goto done;
663 			}
664 
665 			if (id == WLAN_EID_DS_PARAMS)
666 				chan_no = *pos;
667 			if (id == WLAN_EID_SSID) {
668 				ssid = pos;
669 				ssid_len = elen;
670 			}
671 			left -= elen;
672 			pos += elen;
673 		}
674 
675 		/* No channel, no luck */
676 		if (chan_no != -1) {
677 			struct wiphy *wiphy = priv->wdev->wiphy;
678 			int freq = ieee80211_channel_to_frequency(chan_no,
679 							NL80211_BAND_2GHZ);
680 			struct ieee80211_channel *channel =
681 				ieee80211_get_channel(wiphy, freq);
682 
683 			lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %*pE, %d dBm\n",
684 				     bssid, capa, chan_no, ssid_len, ssid,
685 				     LBS_SCAN_RSSI_TO_MBM(rssi)/100);
686 
687 			if (channel &&
688 			    !(channel->flags & IEEE80211_CHAN_DISABLED)) {
689 				bss = cfg80211_inform_bss(wiphy, channel,
690 					CFG80211_BSS_FTYPE_UNKNOWN,
691 					bssid, get_unaligned_le64(tsfdesc),
692 					capa, intvl, ie, ielen,
693 					LBS_SCAN_RSSI_TO_MBM(rssi),
694 					GFP_KERNEL);
695 				cfg80211_put_bss(wiphy, bss);
696 			}
697 		} else
698 			lbs_deb_scan("scan response: missing BSS channel IE\n");
699 
700 		tsfdesc += 8;
701 	}
702 	ret = 0;
703 
704  done:
705 	return ret;
706 }
707 
708 
709 /*
710  * Our scan command contains a TLV, consisting of a SSID TLV, a channel list
711  * TLV, a rates TLV, and an optional WPS IE. Determine the maximum size of them:
712  */
713 #define LBS_SCAN_MAX_CMD_SIZE			\
714 	(sizeof(struct cmd_ds_802_11_scan)	\
715 	 + LBS_MAX_SSID_TLV_SIZE		\
716 	 + LBS_MAX_CHANNEL_LIST_TLV_SIZE	\
717 	 + LBS_MAX_RATES_TLV_SIZE		\
718 	 + LBS_MAX_WPS_ENROLLEE_TLV_SIZE)
719 
720 /*
721  * Assumes priv->scan_req is initialized and valid
722  * Assumes priv->scan_channel is initialized
723  */
lbs_scan_worker(struct work_struct * work)724 static void lbs_scan_worker(struct work_struct *work)
725 {
726 	struct lbs_private *priv =
727 		container_of(work, struct lbs_private, scan_work.work);
728 	struct cmd_ds_802_11_scan *scan_cmd;
729 	u8 *tlv; /* pointer into our current, growing TLV storage area */
730 	int last_channel;
731 	int running, carrier;
732 
733 	scan_cmd = kzalloc(LBS_SCAN_MAX_CMD_SIZE, GFP_KERNEL);
734 	if (scan_cmd == NULL)
735 		return;
736 
737 	/* prepare fixed part of scan command */
738 	scan_cmd->bsstype = CMD_BSS_TYPE_ANY;
739 
740 	/* stop network while we're away from our main channel */
741 	running = !netif_queue_stopped(priv->dev);
742 	carrier = netif_carrier_ok(priv->dev);
743 	if (running)
744 		netif_stop_queue(priv->dev);
745 	if (carrier)
746 		netif_carrier_off(priv->dev);
747 
748 	/* prepare fixed part of scan command */
749 	tlv = scan_cmd->tlvbuffer;
750 
751 	/* add SSID TLV */
752 	if (priv->scan_req->n_ssids && priv->scan_req->ssids[0].ssid_len > 0)
753 		tlv += lbs_add_ssid_tlv(tlv,
754 					priv->scan_req->ssids[0].ssid,
755 					priv->scan_req->ssids[0].ssid_len);
756 
757 	/* add channel TLVs */
758 	last_channel = priv->scan_channel + LBS_SCAN_BEFORE_NAP;
759 	if (last_channel > priv->scan_req->n_channels)
760 		last_channel = priv->scan_req->n_channels;
761 	tlv += lbs_add_channel_list_tlv(priv, tlv, last_channel,
762 		priv->scan_req->n_ssids);
763 
764 	/* add rates TLV */
765 	tlv += lbs_add_supported_rates_tlv(tlv);
766 
767 	/* add optional WPS enrollee TLV */
768 	if (priv->scan_req->ie && priv->scan_req->ie_len)
769 		tlv += lbs_add_wps_enrollee_tlv(tlv, priv->scan_req->ie,
770 						priv->scan_req->ie_len);
771 
772 	if (priv->scan_channel < priv->scan_req->n_channels) {
773 		cancel_delayed_work(&priv->scan_work);
774 		if (netif_running(priv->dev))
775 			queue_delayed_work(priv->work_thread, &priv->scan_work,
776 				msecs_to_jiffies(300));
777 	}
778 
779 	/* This is the final data we are about to send */
780 	scan_cmd->hdr.size = cpu_to_le16(tlv - (u8 *)scan_cmd);
781 	lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
782 		    sizeof(*scan_cmd));
783 	lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
784 		    tlv - scan_cmd->tlvbuffer);
785 
786 	__lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
787 		le16_to_cpu(scan_cmd->hdr.size),
788 		lbs_ret_scan, 0);
789 
790 	if (priv->scan_channel >= priv->scan_req->n_channels) {
791 		/* Mark scan done */
792 		cancel_delayed_work(&priv->scan_work);
793 		lbs_scan_done(priv);
794 	}
795 
796 	/* Restart network */
797 	if (carrier)
798 		netif_carrier_on(priv->dev);
799 	if (running && !priv->tx_pending_len)
800 		netif_wake_queue(priv->dev);
801 
802 	kfree(scan_cmd);
803 
804 	/* Wake up anything waiting on scan completion */
805 	if (priv->scan_req == NULL) {
806 		lbs_deb_scan("scan: waking up waiters\n");
807 		wake_up_all(&priv->scan_q);
808 	}
809 }
810 
_internal_start_scan(struct lbs_private * priv,bool internal,struct cfg80211_scan_request * request)811 static void _internal_start_scan(struct lbs_private *priv, bool internal,
812 	struct cfg80211_scan_request *request)
813 {
814 	lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
815 		request->n_ssids, request->n_channels, request->ie_len);
816 
817 	priv->scan_channel = 0;
818 	priv->scan_req = request;
819 	priv->internal_scan = internal;
820 
821 	queue_delayed_work(priv->work_thread, &priv->scan_work,
822 		msecs_to_jiffies(50));
823 }
824 
825 /*
826  * Clean up priv->scan_req.  Should be used to handle the allocation details.
827  */
lbs_scan_done(struct lbs_private * priv)828 void lbs_scan_done(struct lbs_private *priv)
829 {
830 	WARN_ON(!priv->scan_req);
831 
832 	if (priv->internal_scan) {
833 		kfree(priv->scan_req);
834 	} else {
835 		struct cfg80211_scan_info info = {
836 			.aborted = false,
837 		};
838 
839 		cfg80211_scan_done(priv->scan_req, &info);
840 	}
841 
842 	priv->scan_req = NULL;
843 }
844 
lbs_cfg_scan(struct wiphy * wiphy,struct cfg80211_scan_request * request)845 static int lbs_cfg_scan(struct wiphy *wiphy,
846 	struct cfg80211_scan_request *request)
847 {
848 	struct lbs_private *priv = wiphy_priv(wiphy);
849 	int ret = 0;
850 
851 	if (priv->scan_req || delayed_work_pending(&priv->scan_work)) {
852 		/* old scan request not yet processed */
853 		ret = -EAGAIN;
854 		goto out;
855 	}
856 
857 	_internal_start_scan(priv, false, request);
858 
859 	if (priv->surpriseremoved)
860 		ret = -EIO;
861 
862  out:
863 	return ret;
864 }
865 
866 
867 
868 
869 /*
870  * Events
871  */
872 
lbs_send_disconnect_notification(struct lbs_private * priv,bool locally_generated)873 void lbs_send_disconnect_notification(struct lbs_private *priv,
874 				      bool locally_generated)
875 {
876 	cfg80211_disconnected(priv->dev, 0, NULL, 0, locally_generated,
877 			      GFP_KERNEL);
878 }
879 
lbs_send_mic_failureevent(struct lbs_private * priv,u32 event)880 void lbs_send_mic_failureevent(struct lbs_private *priv, u32 event)
881 {
882 	cfg80211_michael_mic_failure(priv->dev,
883 		priv->assoc_bss,
884 		event == MACREG_INT_CODE_MIC_ERR_MULTICAST ?
885 			NL80211_KEYTYPE_GROUP :
886 			NL80211_KEYTYPE_PAIRWISE,
887 		-1,
888 		NULL,
889 		GFP_KERNEL);
890 }
891 
892 
893 
894 
895 /*
896  * Connect/disconnect
897  */
898 
899 
900 /*
901  * This removes all WEP keys
902  */
lbs_remove_wep_keys(struct lbs_private * priv)903 static int lbs_remove_wep_keys(struct lbs_private *priv)
904 {
905 	struct cmd_ds_802_11_set_wep cmd;
906 	int ret;
907 
908 	memset(&cmd, 0, sizeof(cmd));
909 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
910 	cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
911 	cmd.action = cpu_to_le16(CMD_ACT_REMOVE);
912 
913 	ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
914 
915 	return ret;
916 }
917 
918 /*
919  * Set WEP keys
920  */
lbs_set_wep_keys(struct lbs_private * priv)921 static int lbs_set_wep_keys(struct lbs_private *priv)
922 {
923 	struct cmd_ds_802_11_set_wep cmd;
924 	int i;
925 	int ret;
926 
927 	/*
928 	 * command         13 00
929 	 * size            50 00
930 	 * sequence        xx xx
931 	 * result          00 00
932 	 * action          02 00     ACT_ADD
933 	 * transmit key    00 00
934 	 * type for key 1  01        WEP40
935 	 * type for key 2  00
936 	 * type for key 3  00
937 	 * type for key 4  00
938 	 * key 1           39 39 39 39 39 00 00 00
939 	 *                 00 00 00 00 00 00 00 00
940 	 * key 2           00 00 00 00 00 00 00 00
941 	 *                 00 00 00 00 00 00 00 00
942 	 * key 3           00 00 00 00 00 00 00 00
943 	 *                 00 00 00 00 00 00 00 00
944 	 * key 4           00 00 00 00 00 00 00 00
945 	 */
946 	if (priv->wep_key_len[0] || priv->wep_key_len[1] ||
947 	    priv->wep_key_len[2] || priv->wep_key_len[3]) {
948 		/* Only set wep keys if we have at least one of them */
949 		memset(&cmd, 0, sizeof(cmd));
950 		cmd.hdr.size = cpu_to_le16(sizeof(cmd));
951 		cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
952 		cmd.action = cpu_to_le16(CMD_ACT_ADD);
953 
954 		for (i = 0; i < 4; i++) {
955 			switch (priv->wep_key_len[i]) {
956 			case WLAN_KEY_LEN_WEP40:
957 				cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
958 				break;
959 			case WLAN_KEY_LEN_WEP104:
960 				cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
961 				break;
962 			default:
963 				cmd.keytype[i] = 0;
964 				break;
965 			}
966 			memcpy(cmd.keymaterial[i], priv->wep_key[i],
967 			       priv->wep_key_len[i]);
968 		}
969 
970 		ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
971 	} else {
972 		/* Otherwise remove all wep keys */
973 		ret = lbs_remove_wep_keys(priv);
974 	}
975 
976 	return ret;
977 }
978 
979 
980 /*
981  * Enable/Disable RSN status
982  */
lbs_enable_rsn(struct lbs_private * priv,int enable)983 static int lbs_enable_rsn(struct lbs_private *priv, int enable)
984 {
985 	struct cmd_ds_802_11_enable_rsn cmd;
986 	int ret;
987 
988 	/*
989 	 * cmd       2f 00
990 	 * size      0c 00
991 	 * sequence  xx xx
992 	 * result    00 00
993 	 * action    01 00    ACT_SET
994 	 * enable    01 00
995 	 */
996 	memset(&cmd, 0, sizeof(cmd));
997 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
998 	cmd.action = cpu_to_le16(CMD_ACT_SET);
999 	cmd.enable = cpu_to_le16(enable);
1000 
1001 	ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
1002 
1003 	return ret;
1004 }
1005 
1006 
1007 /*
1008  * Set WPA/WPA key material
1009  */
1010 
1011 /*
1012  * like "struct cmd_ds_802_11_key_material", but with cmd_header. Once we
1013  * get rid of WEXT, this should go into host.h
1014  */
1015 
1016 struct cmd_key_material {
1017 	struct cmd_header hdr;
1018 
1019 	__le16 action;
1020 	struct MrvlIEtype_keyParamSet param;
1021 } __packed;
1022 
lbs_set_key_material(struct lbs_private * priv,int key_type,int key_info,const u8 * key,u16 key_len)1023 static int lbs_set_key_material(struct lbs_private *priv,
1024 				int key_type, int key_info,
1025 				const u8 *key, u16 key_len)
1026 {
1027 	struct cmd_key_material cmd;
1028 	int ret;
1029 
1030 	/*
1031 	 * Example for WPA (TKIP):
1032 	 *
1033 	 * cmd       5e 00
1034 	 * size      34 00
1035 	 * sequence  xx xx
1036 	 * result    00 00
1037 	 * action    01 00
1038 	 * TLV type  00 01    key param
1039 	 * length    00 26
1040 	 * key type  01 00    TKIP
1041 	 * key info  06 00    UNICAST | ENABLED
1042 	 * key len   20 00
1043 	 * key       32 bytes
1044 	 */
1045 	memset(&cmd, 0, sizeof(cmd));
1046 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1047 	cmd.action = cpu_to_le16(CMD_ACT_SET);
1048 	cmd.param.type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
1049 	cmd.param.length = cpu_to_le16(sizeof(cmd.param) - 4);
1050 	cmd.param.keytypeid = cpu_to_le16(key_type);
1051 	cmd.param.keyinfo = cpu_to_le16(key_info);
1052 	cmd.param.keylen = cpu_to_le16(key_len);
1053 	if (key && key_len)
1054 		memcpy(cmd.param.key, key, key_len);
1055 
1056 	ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
1057 
1058 	return ret;
1059 }
1060 
1061 
1062 /*
1063  * Sets the auth type (open, shared, etc) in the firmware. That
1064  * we use CMD_802_11_AUTHENTICATE is misleading, this firmware
1065  * command doesn't send an authentication frame at all, it just
1066  * stores the auth_type.
1067  */
lbs_set_authtype(struct lbs_private * priv,struct cfg80211_connect_params * sme)1068 static int lbs_set_authtype(struct lbs_private *priv,
1069 			    struct cfg80211_connect_params *sme)
1070 {
1071 	struct cmd_ds_802_11_authenticate cmd;
1072 	int ret;
1073 
1074 	/*
1075 	 * cmd        11 00
1076 	 * size       19 00
1077 	 * sequence   xx xx
1078 	 * result     00 00
1079 	 * BSS id     00 13 19 80 da 30
1080 	 * auth type  00
1081 	 * reserved   00 00 00 00 00 00 00 00 00 00
1082 	 */
1083 	memset(&cmd, 0, sizeof(cmd));
1084 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1085 	if (sme->bssid)
1086 		memcpy(cmd.bssid, sme->bssid, ETH_ALEN);
1087 	/* convert auth_type */
1088 	ret = lbs_auth_to_authtype(sme->auth_type);
1089 	if (ret < 0)
1090 		goto done;
1091 
1092 	cmd.authtype = ret;
1093 	ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
1094 
1095  done:
1096 	return ret;
1097 }
1098 
1099 
1100 /*
1101  * Create association request
1102  */
1103 #define LBS_ASSOC_MAX_CMD_SIZE                     \
1104 	(sizeof(struct cmd_ds_802_11_associate)    \
1105 	 + LBS_MAX_SSID_TLV_SIZE                   \
1106 	 + LBS_MAX_CHANNEL_TLV_SIZE                \
1107 	 + LBS_MAX_CF_PARAM_TLV_SIZE               \
1108 	 + LBS_MAX_AUTH_TYPE_TLV_SIZE              \
1109 	 + LBS_MAX_WPA_TLV_SIZE)
1110 
lbs_associate(struct lbs_private * priv,struct cfg80211_bss * bss,struct cfg80211_connect_params * sme)1111 static int lbs_associate(struct lbs_private *priv,
1112 		struct cfg80211_bss *bss,
1113 		struct cfg80211_connect_params *sme)
1114 {
1115 	struct cmd_ds_802_11_associate_response *resp;
1116 	struct cmd_ds_802_11_associate *cmd = kzalloc(LBS_ASSOC_MAX_CMD_SIZE,
1117 						      GFP_KERNEL);
1118 	const u8 *ssid_eid;
1119 	size_t len, resp_ie_len;
1120 	int status;
1121 	int ret;
1122 	u8 *pos;
1123 	u8 *tmp;
1124 
1125 	if (!cmd) {
1126 		ret = -ENOMEM;
1127 		goto done;
1128 	}
1129 	pos = &cmd->iebuf[0];
1130 
1131 	/*
1132 	 * cmd              50 00
1133 	 * length           34 00
1134 	 * sequence         xx xx
1135 	 * result           00 00
1136 	 * BSS id           00 13 19 80 da 30
1137 	 * capabilities     11 00
1138 	 * listen interval  0a 00
1139 	 * beacon interval  00 00
1140 	 * DTIM period      00
1141 	 * TLVs             xx   (up to 512 bytes)
1142 	 */
1143 	cmd->hdr.command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1144 
1145 	/* Fill in static fields */
1146 	memcpy(cmd->bssid, bss->bssid, ETH_ALEN);
1147 	cmd->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1148 	cmd->capability = cpu_to_le16(bss->capability);
1149 
1150 	/* add SSID TLV */
1151 	rcu_read_lock();
1152 	ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
1153 	if (ssid_eid) {
1154 		u32 ssid_len = min(ssid_eid[1], IEEE80211_MAX_SSID_LEN);
1155 
1156 		pos += lbs_add_ssid_tlv(pos, ssid_eid + 2, ssid_len);
1157 	} else {
1158 		lbs_deb_assoc("no SSID\n");
1159 	}
1160 	rcu_read_unlock();
1161 
1162 	/* add DS param TLV */
1163 	if (bss->channel)
1164 		pos += lbs_add_channel_tlv(pos, bss->channel->hw_value);
1165 	else
1166 		lbs_deb_assoc("no channel\n");
1167 
1168 	/* add (empty) CF param TLV */
1169 	pos += lbs_add_cf_param_tlv(pos);
1170 
1171 	/* add rates TLV */
1172 	tmp = pos + 4; /* skip Marvell IE header */
1173 	pos += lbs_add_common_rates_tlv(pos, bss);
1174 	lbs_deb_hex(LBS_DEB_ASSOC, "Common Rates", tmp, pos - tmp);
1175 
1176 	/* add auth type TLV */
1177 	if (MRVL_FW_MAJOR_REV(priv->fwrelease) >= 9)
1178 		pos += lbs_add_auth_type_tlv(pos, sme->auth_type);
1179 
1180 	/* add WPA/WPA2 TLV */
1181 	if (sme->ie && sme->ie_len)
1182 		pos += lbs_add_wpa_tlv(pos, sme->ie, sme->ie_len);
1183 
1184 	len = sizeof(*cmd) + (u16)(pos - (u8 *) &cmd->iebuf);
1185 	cmd->hdr.size = cpu_to_le16(len);
1186 
1187 	lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_CMD", (u8 *) cmd,
1188 			le16_to_cpu(cmd->hdr.size));
1189 
1190 	/* store for later use */
1191 	memcpy(priv->assoc_bss, bss->bssid, ETH_ALEN);
1192 
1193 	ret = lbs_cmd_with_response(priv, CMD_802_11_ASSOCIATE, cmd);
1194 	if (ret)
1195 		goto done;
1196 
1197 	/* generate connect message to cfg80211 */
1198 
1199 	resp = (void *) cmd; /* recast for easier field access */
1200 	status = le16_to_cpu(resp->statuscode);
1201 
1202 	/* Older FW versions map the IEEE 802.11 Status Code in the association
1203 	 * response to the following values returned in resp->statuscode:
1204 	 *
1205 	 *    IEEE Status Code                Marvell Status Code
1206 	 *    0                       ->      0x0000 ASSOC_RESULT_SUCCESS
1207 	 *    13                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1208 	 *    14                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1209 	 *    15                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1210 	 *    16                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1211 	 *    others                  ->      0x0003 ASSOC_RESULT_REFUSED
1212 	 *
1213 	 * Other response codes:
1214 	 *    0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1215 	 *    0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1216 	 *                                    association response from the AP)
1217 	 */
1218 	if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1219 		switch (status) {
1220 		case 0:
1221 			break;
1222 		case 1:
1223 			lbs_deb_assoc("invalid association parameters\n");
1224 			status = WLAN_STATUS_CAPS_UNSUPPORTED;
1225 			break;
1226 		case 2:
1227 			lbs_deb_assoc("timer expired while waiting for AP\n");
1228 			status = WLAN_STATUS_AUTH_TIMEOUT;
1229 			break;
1230 		case 3:
1231 			lbs_deb_assoc("association refused by AP\n");
1232 			status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1233 			break;
1234 		case 4:
1235 			lbs_deb_assoc("authentication refused by AP\n");
1236 			status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1237 			break;
1238 		default:
1239 			lbs_deb_assoc("association failure %d\n", status);
1240 			/* v5 OLPC firmware does return the AP status code if
1241 			 * it's not one of the values above.  Let that through.
1242 			 */
1243 			break;
1244 		}
1245 	}
1246 
1247 	lbs_deb_assoc("status %d, statuscode 0x%04x, capability 0x%04x, "
1248 		      "aid 0x%04x\n", status, le16_to_cpu(resp->statuscode),
1249 		      le16_to_cpu(resp->capability), le16_to_cpu(resp->aid));
1250 
1251 	resp_ie_len = le16_to_cpu(resp->hdr.size)
1252 		- sizeof(resp->hdr)
1253 		- 6;
1254 	cfg80211_connect_result(priv->dev,
1255 				priv->assoc_bss,
1256 				sme->ie, sme->ie_len,
1257 				resp->iebuf, resp_ie_len,
1258 				status,
1259 				GFP_KERNEL);
1260 
1261 	if (status == 0) {
1262 		/* TODO: get rid of priv->connect_status */
1263 		priv->connect_status = LBS_CONNECTED;
1264 		netif_carrier_on(priv->dev);
1265 		if (!priv->tx_pending_len)
1266 			netif_tx_wake_all_queues(priv->dev);
1267 	}
1268 
1269 	kfree(cmd);
1270 done:
1271 	return ret;
1272 }
1273 
1274 static struct cfg80211_scan_request *
_new_connect_scan_req(struct wiphy * wiphy,struct cfg80211_connect_params * sme)1275 _new_connect_scan_req(struct wiphy *wiphy, struct cfg80211_connect_params *sme)
1276 {
1277 	struct cfg80211_scan_request *creq = NULL;
1278 	int i, n_channels = ieee80211_get_num_supported_channels(wiphy);
1279 	enum nl80211_band band;
1280 
1281 	creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1282 		       n_channels * sizeof(void *),
1283 		       GFP_ATOMIC);
1284 	if (!creq)
1285 		return NULL;
1286 
1287 	/* SSIDs come after channels */
1288 	creq->ssids = (void *)&creq->channels[n_channels];
1289 	creq->n_channels = n_channels;
1290 	creq->n_ssids = 1;
1291 
1292 	/* Scan all available channels */
1293 	i = 0;
1294 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1295 		int j;
1296 
1297 		if (!wiphy->bands[band])
1298 			continue;
1299 
1300 		for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
1301 			/* ignore disabled channels */
1302 			if (wiphy->bands[band]->channels[j].flags &
1303 						IEEE80211_CHAN_DISABLED)
1304 				continue;
1305 
1306 			creq->channels[i] = &wiphy->bands[band]->channels[j];
1307 			i++;
1308 		}
1309 	}
1310 	if (i) {
1311 		/* Set real number of channels specified in creq->channels[] */
1312 		creq->n_channels = i;
1313 
1314 		/* Scan for the SSID we're going to connect to */
1315 		memcpy(creq->ssids[0].ssid, sme->ssid, sme->ssid_len);
1316 		creq->ssids[0].ssid_len = sme->ssid_len;
1317 	} else {
1318 		/* No channels found... */
1319 		kfree(creq);
1320 		creq = NULL;
1321 	}
1322 
1323 	return creq;
1324 }
1325 
lbs_cfg_connect(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_connect_params * sme)1326 static int lbs_cfg_connect(struct wiphy *wiphy, struct net_device *dev,
1327 			   struct cfg80211_connect_params *sme)
1328 {
1329 	struct lbs_private *priv = wiphy_priv(wiphy);
1330 	struct cfg80211_bss *bss = NULL;
1331 	int ret = 0;
1332 	u8 preamble = RADIO_PREAMBLE_SHORT;
1333 
1334 	if (dev == priv->mesh_dev)
1335 		return -EOPNOTSUPP;
1336 
1337 	if (!sme->bssid) {
1338 		struct cfg80211_scan_request *creq;
1339 
1340 		/*
1341 		 * Scan for the requested network after waiting for existing
1342 		 * scans to finish.
1343 		 */
1344 		lbs_deb_assoc("assoc: waiting for existing scans\n");
1345 		wait_event_interruptible_timeout(priv->scan_q,
1346 						 (priv->scan_req == NULL),
1347 						 (15 * HZ));
1348 
1349 		creq = _new_connect_scan_req(wiphy, sme);
1350 		if (!creq) {
1351 			ret = -EINVAL;
1352 			goto done;
1353 		}
1354 
1355 		lbs_deb_assoc("assoc: scanning for compatible AP\n");
1356 		_internal_start_scan(priv, true, creq);
1357 
1358 		lbs_deb_assoc("assoc: waiting for scan to complete\n");
1359 		wait_event_interruptible_timeout(priv->scan_q,
1360 						 (priv->scan_req == NULL),
1361 						 (15 * HZ));
1362 		lbs_deb_assoc("assoc: scanning completed\n");
1363 	}
1364 
1365 	/* Find the BSS we want using available scan results */
1366 	bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
1367 		sme->ssid, sme->ssid_len, IEEE80211_BSS_TYPE_ESS,
1368 		IEEE80211_PRIVACY_ANY);
1369 	if (!bss) {
1370 		wiphy_err(wiphy, "assoc: bss %pM not in scan results\n",
1371 			  sme->bssid);
1372 		ret = -ENOENT;
1373 		goto done;
1374 	}
1375 	lbs_deb_assoc("trying %pM\n", bss->bssid);
1376 	lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
1377 		      sme->crypto.cipher_group,
1378 		      sme->key_idx, sme->key_len);
1379 
1380 	/* As this is a new connection, clear locally stored WEP keys */
1381 	priv->wep_tx_key = 0;
1382 	memset(priv->wep_key, 0, sizeof(priv->wep_key));
1383 	memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));
1384 
1385 	/* set/remove WEP keys */
1386 	switch (sme->crypto.cipher_group) {
1387 	case WLAN_CIPHER_SUITE_WEP40:
1388 	case WLAN_CIPHER_SUITE_WEP104:
1389 		/* Store provided WEP keys in priv-> */
1390 		priv->wep_tx_key = sme->key_idx;
1391 		priv->wep_key_len[sme->key_idx] = sme->key_len;
1392 		memcpy(priv->wep_key[sme->key_idx], sme->key, sme->key_len);
1393 		/* Set WEP keys and WEP mode */
1394 		lbs_set_wep_keys(priv);
1395 		priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1396 		lbs_set_mac_control(priv);
1397 		/* No RSN mode for WEP */
1398 		lbs_enable_rsn(priv, 0);
1399 		break;
1400 	case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
1401 		/*
1402 		 * If we don't have no WEP, no WPA and no WPA2,
1403 		 * we remove all keys like in the WPA/WPA2 setup,
1404 		 * we just don't set RSN.
1405 		 *
1406 		 * Therefore: fall-through
1407 		 */
1408 	case WLAN_CIPHER_SUITE_TKIP:
1409 	case WLAN_CIPHER_SUITE_CCMP:
1410 		/* Remove WEP keys and WEP mode */
1411 		lbs_remove_wep_keys(priv);
1412 		priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1413 		lbs_set_mac_control(priv);
1414 
1415 		/* clear the WPA/WPA2 keys */
1416 		lbs_set_key_material(priv,
1417 			KEY_TYPE_ID_WEP, /* doesn't matter */
1418 			KEY_INFO_WPA_UNICAST,
1419 			NULL, 0);
1420 		lbs_set_key_material(priv,
1421 			KEY_TYPE_ID_WEP, /* doesn't matter */
1422 			KEY_INFO_WPA_MCAST,
1423 			NULL, 0);
1424 		/* RSN mode for WPA/WPA2 */
1425 		lbs_enable_rsn(priv, sme->crypto.cipher_group != 0);
1426 		break;
1427 	default:
1428 		wiphy_err(wiphy, "unsupported cipher group 0x%x\n",
1429 			  sme->crypto.cipher_group);
1430 		ret = -ENOTSUPP;
1431 		goto done;
1432 	}
1433 
1434 	ret = lbs_set_authtype(priv, sme);
1435 	if (ret == -ENOTSUPP) {
1436 		wiphy_err(wiphy, "unsupported authtype 0x%x\n", sme->auth_type);
1437 		goto done;
1438 	}
1439 
1440 	lbs_set_radio(priv, preamble, 1);
1441 
1442 	/* Do the actual association */
1443 	ret = lbs_associate(priv, bss, sme);
1444 
1445  done:
1446 	if (bss)
1447 		cfg80211_put_bss(wiphy, bss);
1448 	return ret;
1449 }
1450 
lbs_disconnect(struct lbs_private * priv,u16 reason)1451 int lbs_disconnect(struct lbs_private *priv, u16 reason)
1452 {
1453 	struct cmd_ds_802_11_deauthenticate cmd;
1454 	int ret;
1455 
1456 	memset(&cmd, 0, sizeof(cmd));
1457 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1458 	/* Mildly ugly to use a locally store my own BSSID ... */
1459 	memcpy(cmd.macaddr, &priv->assoc_bss, ETH_ALEN);
1460 	cmd.reasoncode = cpu_to_le16(reason);
1461 
1462 	ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1463 	if (ret)
1464 		return ret;
1465 
1466 	cfg80211_disconnected(priv->dev,
1467 			reason,
1468 			NULL, 0, true,
1469 			GFP_KERNEL);
1470 	priv->connect_status = LBS_DISCONNECTED;
1471 
1472 	return 0;
1473 }
1474 
lbs_cfg_disconnect(struct wiphy * wiphy,struct net_device * dev,u16 reason_code)1475 static int lbs_cfg_disconnect(struct wiphy *wiphy, struct net_device *dev,
1476 	u16 reason_code)
1477 {
1478 	struct lbs_private *priv = wiphy_priv(wiphy);
1479 
1480 	if (dev == priv->mesh_dev)
1481 		return -EOPNOTSUPP;
1482 
1483 	/* store for lbs_cfg_ret_disconnect() */
1484 	priv->disassoc_reason = reason_code;
1485 
1486 	return lbs_disconnect(priv, reason_code);
1487 }
1488 
lbs_cfg_set_default_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool unicast,bool multicast)1489 static int lbs_cfg_set_default_key(struct wiphy *wiphy,
1490 				   struct net_device *netdev, int link_id,
1491 				   u8 key_index, bool unicast,
1492 				   bool multicast)
1493 {
1494 	struct lbs_private *priv = wiphy_priv(wiphy);
1495 
1496 	if (netdev == priv->mesh_dev)
1497 		return -EOPNOTSUPP;
1498 
1499 	if (key_index != priv->wep_tx_key) {
1500 		lbs_deb_assoc("set_default_key: to %d\n", key_index);
1501 		priv->wep_tx_key = key_index;
1502 		lbs_set_wep_keys(priv);
1503 	}
1504 
1505 	return 0;
1506 }
1507 
1508 
lbs_cfg_add_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 idx,bool pairwise,const u8 * mac_addr,struct key_params * params)1509 static int lbs_cfg_add_key(struct wiphy *wiphy, struct net_device *netdev,
1510 			   int link_id, u8 idx, bool pairwise,
1511 			   const u8 *mac_addr, struct key_params *params)
1512 {
1513 	struct lbs_private *priv = wiphy_priv(wiphy);
1514 	u16 key_info;
1515 	u16 key_type;
1516 	int ret = 0;
1517 
1518 	if (netdev == priv->mesh_dev)
1519 		return -EOPNOTSUPP;
1520 
1521 	lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
1522 		      params->cipher, mac_addr);
1523 	lbs_deb_assoc("add_key: key index %d, key len %d\n",
1524 		      idx, params->key_len);
1525 	if (params->key_len)
1526 		lbs_deb_hex(LBS_DEB_CFG80211, "KEY",
1527 			    params->key, params->key_len);
1528 
1529 	lbs_deb_assoc("add_key: seq len %d\n", params->seq_len);
1530 	if (params->seq_len)
1531 		lbs_deb_hex(LBS_DEB_CFG80211, "SEQ",
1532 			    params->seq, params->seq_len);
1533 
1534 	switch (params->cipher) {
1535 	case WLAN_CIPHER_SUITE_WEP40:
1536 	case WLAN_CIPHER_SUITE_WEP104:
1537 		/* actually compare if something has changed ... */
1538 		if ((priv->wep_key_len[idx] != params->key_len) ||
1539 			memcmp(priv->wep_key[idx],
1540 			       params->key, params->key_len) != 0) {
1541 			priv->wep_key_len[idx] = params->key_len;
1542 			memcpy(priv->wep_key[idx],
1543 			       params->key, params->key_len);
1544 			lbs_set_wep_keys(priv);
1545 		}
1546 		break;
1547 	case WLAN_CIPHER_SUITE_TKIP:
1548 	case WLAN_CIPHER_SUITE_CCMP:
1549 		key_info = KEY_INFO_WPA_ENABLED | ((idx == 0)
1550 						   ? KEY_INFO_WPA_UNICAST
1551 						   : KEY_INFO_WPA_MCAST);
1552 		key_type = (params->cipher == WLAN_CIPHER_SUITE_TKIP)
1553 			? KEY_TYPE_ID_TKIP
1554 			: KEY_TYPE_ID_AES;
1555 		lbs_set_key_material(priv,
1556 				     key_type,
1557 				     key_info,
1558 				     params->key, params->key_len);
1559 		break;
1560 	default:
1561 		wiphy_err(wiphy, "unhandled cipher 0x%x\n", params->cipher);
1562 		ret = -ENOTSUPP;
1563 		break;
1564 	}
1565 
1566 	return ret;
1567 }
1568 
1569 
lbs_cfg_del_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr)1570 static int lbs_cfg_del_key(struct wiphy *wiphy, struct net_device *netdev,
1571 			   int link_id, u8 key_index, bool pairwise,
1572 			   const u8 *mac_addr)
1573 {
1574 
1575 	lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
1576 		      key_index, mac_addr);
1577 
1578 #ifdef TODO
1579 	struct lbs_private *priv = wiphy_priv(wiphy);
1580 	/*
1581 	 * I think can keep this a NO-OP, because:
1582 
1583 	 * - we clear all keys whenever we do lbs_cfg_connect() anyway
1584 	 * - neither "iw" nor "wpa_supplicant" won't call this during
1585 	 *   an ongoing connection
1586 	 * - TODO: but I have to check if this is still true when
1587 	 *   I set the AP to periodic re-keying
1588 	 * - we've not kzallec() something when we've added a key at
1589 	 *   lbs_cfg_connect() or lbs_cfg_add_key().
1590 	 *
1591 	 * This causes lbs_cfg_del_key() only called at disconnect time,
1592 	 * where we'd just waste time deleting a key that is not going
1593 	 * to be used anyway.
1594 	 */
1595 	if (key_index < 3 && priv->wep_key_len[key_index]) {
1596 		priv->wep_key_len[key_index] = 0;
1597 		lbs_set_wep_keys(priv);
1598 	}
1599 #endif
1600 
1601 	return 0;
1602 }
1603 
1604 
1605 /*
1606  * Get station
1607  */
1608 
lbs_cfg_get_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_info * sinfo)1609 static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
1610 			       const u8 *mac, struct station_info *sinfo)
1611 {
1612 	struct lbs_private *priv = wiphy_priv(wiphy);
1613 	s8 signal, noise;
1614 	int ret;
1615 	size_t i;
1616 
1617 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
1618 			 BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
1619 			 BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
1620 			 BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
1621 	sinfo->tx_bytes = priv->dev->stats.tx_bytes;
1622 	sinfo->tx_packets = priv->dev->stats.tx_packets;
1623 	sinfo->rx_bytes = priv->dev->stats.rx_bytes;
1624 	sinfo->rx_packets = priv->dev->stats.rx_packets;
1625 
1626 	/* Get current RSSI */
1627 	ret = lbs_get_rssi(priv, &signal, &noise);
1628 	if (ret == 0) {
1629 		sinfo->signal = signal;
1630 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
1631 	}
1632 
1633 	/* Convert priv->cur_rate from hw_value to NL80211 value */
1634 	for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
1635 		if (priv->cur_rate == lbs_rates[i].hw_value) {
1636 			sinfo->txrate.legacy = lbs_rates[i].bitrate;
1637 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1638 			break;
1639 		}
1640 	}
1641 
1642 	return 0;
1643 }
1644 
1645 
1646 
1647 
1648 /*
1649  * Change interface
1650  */
1651 
lbs_change_intf(struct wiphy * wiphy,struct net_device * dev,enum nl80211_iftype type,struct vif_params * params)1652 static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
1653 	enum nl80211_iftype type,
1654 	       struct vif_params *params)
1655 {
1656 	struct lbs_private *priv = wiphy_priv(wiphy);
1657 	int ret = 0;
1658 
1659 	if (dev == priv->mesh_dev)
1660 		return -EOPNOTSUPP;
1661 
1662 	switch (type) {
1663 	case NL80211_IFTYPE_MONITOR:
1664 	case NL80211_IFTYPE_STATION:
1665 	case NL80211_IFTYPE_ADHOC:
1666 		break;
1667 	default:
1668 		return -EOPNOTSUPP;
1669 	}
1670 
1671 	if (priv->iface_running)
1672 		ret = lbs_set_iface_type(priv, type);
1673 
1674 	if (!ret)
1675 		priv->wdev->iftype = type;
1676 
1677 	return ret;
1678 }
1679 
1680 
1681 
1682 /*
1683  * IBSS (Ad-Hoc)
1684  */
1685 
1686 /*
1687  * The firmware needs the following bits masked out of the beacon-derived
1688  * capability field when associating/joining to a BSS:
1689  *  9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
1690  */
1691 #define CAPINFO_MASK (~(0xda00))
1692 
1693 
lbs_join_post(struct lbs_private * priv,struct cfg80211_ibss_params * params,u8 * bssid,u16 capability)1694 static void lbs_join_post(struct lbs_private *priv,
1695 			  struct cfg80211_ibss_params *params,
1696 			  u8 *bssid, u16 capability)
1697 {
1698 	u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN + /* ssid */
1699 		   2 + 4 +                      /* basic rates */
1700 		   2 + 1 +                      /* DS parameter */
1701 		   2 + 2 +                      /* atim */
1702 		   2 + 8];                      /* extended rates */
1703 	u8 *fake = fake_ie;
1704 	struct cfg80211_bss *bss;
1705 
1706 	/*
1707 	 * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
1708 	 * the real IE from the firmware. So we fabricate a fake IE based on
1709 	 * what the firmware actually sends (sniffed with wireshark).
1710 	 */
1711 	/* Fake SSID IE */
1712 	*fake++ = WLAN_EID_SSID;
1713 	*fake++ = params->ssid_len;
1714 	memcpy(fake, params->ssid, params->ssid_len);
1715 	fake += params->ssid_len;
1716 	/* Fake supported basic rates IE */
1717 	*fake++ = WLAN_EID_SUPP_RATES;
1718 	*fake++ = 4;
1719 	*fake++ = 0x82;
1720 	*fake++ = 0x84;
1721 	*fake++ = 0x8b;
1722 	*fake++ = 0x96;
1723 	/* Fake DS channel IE */
1724 	*fake++ = WLAN_EID_DS_PARAMS;
1725 	*fake++ = 1;
1726 	*fake++ = params->chandef.chan->hw_value;
1727 	/* Fake IBSS params IE */
1728 	*fake++ = WLAN_EID_IBSS_PARAMS;
1729 	*fake++ = 2;
1730 	*fake++ = 0; /* ATIM=0 */
1731 	*fake++ = 0;
1732 	/* Fake extended rates IE, TODO: don't add this for 802.11b only,
1733 	 * but I don't know how this could be checked */
1734 	*fake++ = WLAN_EID_EXT_SUPP_RATES;
1735 	*fake++ = 8;
1736 	*fake++ = 0x0c;
1737 	*fake++ = 0x12;
1738 	*fake++ = 0x18;
1739 	*fake++ = 0x24;
1740 	*fake++ = 0x30;
1741 	*fake++ = 0x48;
1742 	*fake++ = 0x60;
1743 	*fake++ = 0x6c;
1744 	lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
1745 
1746 	bss = cfg80211_inform_bss(priv->wdev->wiphy,
1747 				  params->chandef.chan,
1748 				  CFG80211_BSS_FTYPE_UNKNOWN,
1749 				  bssid,
1750 				  0,
1751 				  capability,
1752 				  params->beacon_interval,
1753 				  fake_ie, fake - fake_ie,
1754 				  0, GFP_KERNEL);
1755 	cfg80211_put_bss(priv->wdev->wiphy, bss);
1756 
1757 	cfg80211_ibss_joined(priv->dev, bssid, params->chandef.chan,
1758 			     GFP_KERNEL);
1759 
1760 	/* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
1761 	priv->connect_status = LBS_CONNECTED;
1762 	netif_carrier_on(priv->dev);
1763 	if (!priv->tx_pending_len)
1764 		netif_wake_queue(priv->dev);
1765 }
1766 
lbs_ibss_join_existing(struct lbs_private * priv,struct cfg80211_ibss_params * params,struct cfg80211_bss * bss)1767 static int lbs_ibss_join_existing(struct lbs_private *priv,
1768 	struct cfg80211_ibss_params *params,
1769 	struct cfg80211_bss *bss)
1770 {
1771 	const u8 *rates_eid;
1772 	struct cmd_ds_802_11_ad_hoc_join cmd;
1773 	u8 preamble = RADIO_PREAMBLE_SHORT;
1774 	int ret = 0;
1775 	int hw, i;
1776 	u8 rates_max;
1777 	u8 *rates;
1778 
1779 	/* TODO: set preamble based on scan result */
1780 	ret = lbs_set_radio(priv, preamble, 1);
1781 	if (ret)
1782 		goto out;
1783 
1784 	/*
1785 	 * Example CMD_802_11_AD_HOC_JOIN command:
1786 	 *
1787 	 * command         2c 00         CMD_802_11_AD_HOC_JOIN
1788 	 * size            65 00
1789 	 * sequence        xx xx
1790 	 * result          00 00
1791 	 * bssid           02 27 27 97 2f 96
1792 	 * ssid            49 42 53 53 00 00 00 00
1793 	 *                 00 00 00 00 00 00 00 00
1794 	 *                 00 00 00 00 00 00 00 00
1795 	 *                 00 00 00 00 00 00 00 00
1796 	 * type            02            CMD_BSS_TYPE_IBSS
1797 	 * beacon period   64 00
1798 	 * dtim period     00
1799 	 * timestamp       00 00 00 00 00 00 00 00
1800 	 * localtime       00 00 00 00 00 00 00 00
1801 	 * IE DS           03
1802 	 * IE DS len       01
1803 	 * IE DS channel   01
1804 	 * reserveed       00 00 00 00
1805 	 * IE IBSS         06
1806 	 * IE IBSS len     02
1807 	 * IE IBSS atim    00 00
1808 	 * reserved        00 00 00 00
1809 	 * capability      02 00
1810 	 * rates           82 84 8b 96 0c 12 18 24 30 48 60 6c 00
1811 	 * fail timeout    ff 00
1812 	 * probe delay     00 00
1813 	 */
1814 	memset(&cmd, 0, sizeof(cmd));
1815 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1816 
1817 	memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
1818 	memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
1819 	cmd.bss.type = CMD_BSS_TYPE_IBSS;
1820 	cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
1821 	cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
1822 	cmd.bss.ds.header.len = 1;
1823 	cmd.bss.ds.channel = params->chandef.chan->hw_value;
1824 	cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1825 	cmd.bss.ibss.header.len = 2;
1826 	cmd.bss.ibss.atimwindow = 0;
1827 	cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
1828 
1829 	/* set rates to the intersection of our rates and the rates in the
1830 	   bss */
1831 	rcu_read_lock();
1832 	rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1833 	if (!rates_eid) {
1834 		lbs_add_rates(cmd.bss.rates);
1835 	} else {
1836 		rates_max = rates_eid[1];
1837 		if (rates_max > MAX_RATES) {
1838 			lbs_deb_join("invalid rates");
1839 			rcu_read_unlock();
1840 			ret = -EINVAL;
1841 			goto out;
1842 		}
1843 		rates = cmd.bss.rates;
1844 		for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
1845 			u8 hw_rate = lbs_rates[hw].bitrate / 5;
1846 			for (i = 0; i < rates_max; i++) {
1847 				if (hw_rate == (rates_eid[i+2] & 0x7f)) {
1848 					u8 rate = rates_eid[i+2];
1849 					if (rate == 0x02 || rate == 0x04 ||
1850 					    rate == 0x0b || rate == 0x16)
1851 						rate |= 0x80;
1852 					*rates++ = rate;
1853 				}
1854 			}
1855 		}
1856 	}
1857 	rcu_read_unlock();
1858 
1859 	/* Only v8 and below support setting this */
1860 	if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1861 		cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1862 		cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1863 	}
1864 	ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
1865 	if (ret)
1866 		goto out;
1867 
1868 	/*
1869 	 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1870 	 *
1871 	 * response        2c 80
1872 	 * size            09 00
1873 	 * sequence        xx xx
1874 	 * result          00 00
1875 	 * reserved        00
1876 	 */
1877 	lbs_join_post(priv, params, bss->bssid, bss->capability);
1878 
1879  out:
1880 	return ret;
1881 }
1882 
1883 
1884 
lbs_ibss_start_new(struct lbs_private * priv,struct cfg80211_ibss_params * params)1885 static int lbs_ibss_start_new(struct lbs_private *priv,
1886 	struct cfg80211_ibss_params *params)
1887 {
1888 	struct cmd_ds_802_11_ad_hoc_start cmd;
1889 	struct cmd_ds_802_11_ad_hoc_result *resp =
1890 		(struct cmd_ds_802_11_ad_hoc_result *) &cmd;
1891 	u8 preamble = RADIO_PREAMBLE_SHORT;
1892 	int ret = 0;
1893 	u16 capability;
1894 
1895 	ret = lbs_set_radio(priv, preamble, 1);
1896 	if (ret)
1897 		goto out;
1898 
1899 	/*
1900 	 * Example CMD_802_11_AD_HOC_START command:
1901 	 *
1902 	 * command         2b 00         CMD_802_11_AD_HOC_START
1903 	 * size            b1 00
1904 	 * sequence        xx xx
1905 	 * result          00 00
1906 	 * ssid            54 45 53 54 00 00 00 00
1907 	 *                 00 00 00 00 00 00 00 00
1908 	 *                 00 00 00 00 00 00 00 00
1909 	 *                 00 00 00 00 00 00 00 00
1910 	 * bss type        02
1911 	 * beacon period   64 00
1912 	 * dtim period     00
1913 	 * IE IBSS         06
1914 	 * IE IBSS len     02
1915 	 * IE IBSS atim    00 00
1916 	 * reserved        00 00 00 00
1917 	 * IE DS           03
1918 	 * IE DS len       01
1919 	 * IE DS channel   01
1920 	 * reserved        00 00 00 00
1921 	 * probe delay     00 00
1922 	 * capability      02 00
1923 	 * rates           82 84 8b 96   (basic rates with have bit 7 set)
1924 	 *                 0c 12 18 24 30 48 60 6c
1925 	 * padding         100 bytes
1926 	 */
1927 	memset(&cmd, 0, sizeof(cmd));
1928 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1929 	memcpy(cmd.ssid, params->ssid, params->ssid_len);
1930 	cmd.bsstype = CMD_BSS_TYPE_IBSS;
1931 	cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
1932 	cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1933 	cmd.ibss.header.len = 2;
1934 	cmd.ibss.atimwindow = 0;
1935 	cmd.ds.header.id = WLAN_EID_DS_PARAMS;
1936 	cmd.ds.header.len = 1;
1937 	cmd.ds.channel = params->chandef.chan->hw_value;
1938 	/* Only v8 and below support setting probe delay */
1939 	if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
1940 		cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1941 	/* TODO: mix in WLAN_CAPABILITY_PRIVACY */
1942 	capability = WLAN_CAPABILITY_IBSS;
1943 	cmd.capability = cpu_to_le16(capability);
1944 	lbs_add_rates(cmd.rates);
1945 
1946 
1947 	ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
1948 	if (ret)
1949 		goto out;
1950 
1951 	/*
1952 	 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1953 	 *
1954 	 * response        2b 80
1955 	 * size            14 00
1956 	 * sequence        xx xx
1957 	 * result          00 00
1958 	 * reserved        00
1959 	 * bssid           02 2b 7b 0f 86 0e
1960 	 */
1961 	lbs_join_post(priv, params, resp->bssid, capability);
1962 
1963  out:
1964 	return ret;
1965 }
1966 
1967 
lbs_join_ibss(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_ibss_params * params)1968 static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1969 		struct cfg80211_ibss_params *params)
1970 {
1971 	struct lbs_private *priv = wiphy_priv(wiphy);
1972 	int ret = 0;
1973 	struct cfg80211_bss *bss;
1974 
1975 	if (dev == priv->mesh_dev)
1976 		return -EOPNOTSUPP;
1977 
1978 	if (!params->chandef.chan) {
1979 		ret = -ENOTSUPP;
1980 		goto out;
1981 	}
1982 
1983 	ret = lbs_set_channel(priv, params->chandef.chan->hw_value);
1984 	if (ret)
1985 		goto out;
1986 
1987 	/* Search if someone is beaconing. This assumes that the
1988 	 * bss list is populated already */
1989 	bss = cfg80211_get_bss(wiphy, params->chandef.chan, params->bssid,
1990 		params->ssid, params->ssid_len,
1991 		IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
1992 
1993 	if (bss) {
1994 		ret = lbs_ibss_join_existing(priv, params, bss);
1995 		cfg80211_put_bss(wiphy, bss);
1996 	} else
1997 		ret = lbs_ibss_start_new(priv, params);
1998 
1999 
2000  out:
2001 	return ret;
2002 }
2003 
2004 
lbs_leave_ibss(struct wiphy * wiphy,struct net_device * dev)2005 static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2006 {
2007 	struct lbs_private *priv = wiphy_priv(wiphy);
2008 	struct cmd_ds_802_11_ad_hoc_stop cmd;
2009 	int ret = 0;
2010 
2011 	if (dev == priv->mesh_dev)
2012 		return -EOPNOTSUPP;
2013 
2014 	memset(&cmd, 0, sizeof(cmd));
2015 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2016 	ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
2017 
2018 	/* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
2019 	lbs_mac_event_disconnected(priv, true);
2020 
2021 	return ret;
2022 }
2023 
2024 
2025 
lbs_set_power_mgmt(struct wiphy * wiphy,struct net_device * dev,bool enabled,int timeout)2026 static int lbs_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2027 			      bool enabled, int timeout)
2028 {
2029 	struct lbs_private *priv = wiphy_priv(wiphy);
2030 
2031 	if  (!(priv->fwcapinfo & FW_CAPINFO_PS)) {
2032 		if (!enabled)
2033 			return 0;
2034 		else
2035 			return -EINVAL;
2036 	}
2037 	/* firmware does not work well with too long latency with power saving
2038 	 * enabled, so do not enable it if there is only polling, no
2039 	 * interrupts (like in some sdio hosts which can only
2040 	 * poll for sdio irqs)
2041 	 */
2042 	if  (priv->is_polling) {
2043 		if (!enabled)
2044 			return 0;
2045 		else
2046 			return -EINVAL;
2047 	}
2048 	if (!enabled) {
2049 		priv->psmode = LBS802_11POWERMODECAM;
2050 		if (priv->psstate != PS_STATE_FULL_POWER)
2051 			lbs_set_ps_mode(priv,
2052 					PS_MODE_ACTION_EXIT_PS,
2053 					true);
2054 		return 0;
2055 	}
2056 	if (priv->psmode != LBS802_11POWERMODECAM)
2057 		return 0;
2058 	priv->psmode = LBS802_11POWERMODEMAX_PSP;
2059 	if (priv->connect_status == LBS_CONNECTED)
2060 		lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS, true);
2061 	return 0;
2062 }
2063 
2064 /*
2065  * Initialization
2066  */
2067 
2068 static const struct cfg80211_ops lbs_cfg80211_ops = {
2069 	.set_monitor_channel = lbs_cfg_set_monitor_channel,
2070 	.libertas_set_mesh_channel = lbs_cfg_set_mesh_channel,
2071 	.scan = lbs_cfg_scan,
2072 	.connect = lbs_cfg_connect,
2073 	.disconnect = lbs_cfg_disconnect,
2074 	.add_key = lbs_cfg_add_key,
2075 	.del_key = lbs_cfg_del_key,
2076 	.set_default_key = lbs_cfg_set_default_key,
2077 	.get_station = lbs_cfg_get_station,
2078 	.change_virtual_intf = lbs_change_intf,
2079 	.join_ibss = lbs_join_ibss,
2080 	.leave_ibss = lbs_leave_ibss,
2081 	.set_power_mgmt = lbs_set_power_mgmt,
2082 };
2083 
2084 
2085 /*
2086  * At this time lbs_private *priv doesn't even exist, so we just allocate
2087  * memory and don't initialize the wiphy further. This is postponed until we
2088  * can talk to the firmware and happens at registration time in
2089  * lbs_cfg_wiphy_register().
2090  */
lbs_cfg_alloc(struct device * dev)2091 struct wireless_dev *lbs_cfg_alloc(struct device *dev)
2092 {
2093 	int ret = 0;
2094 	struct wireless_dev *wdev;
2095 
2096 	wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
2097 	if (!wdev)
2098 		return ERR_PTR(-ENOMEM);
2099 
2100 	wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
2101 	if (!wdev->wiphy) {
2102 		dev_err(dev, "cannot allocate wiphy\n");
2103 		ret = -ENOMEM;
2104 		goto err_wiphy_new;
2105 	}
2106 
2107 	return wdev;
2108 
2109  err_wiphy_new:
2110 	kfree(wdev);
2111 	return ERR_PTR(ret);
2112 }
2113 
2114 
lbs_cfg_set_regulatory_hint(struct lbs_private * priv)2115 static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
2116 {
2117 	struct region_code_mapping {
2118 		const char *cn;
2119 		int code;
2120 	};
2121 
2122 	/* Section 5.17.2 */
2123 	static const struct region_code_mapping regmap[] = {
2124 		{"US ", 0x10}, /* US FCC */
2125 		{"CA ", 0x20}, /* Canada */
2126 		{"EU ", 0x30}, /* ETSI   */
2127 		{"ES ", 0x31}, /* Spain  */
2128 		{"FR ", 0x32}, /* France */
2129 		{"JP ", 0x40}, /* Japan  */
2130 	};
2131 	size_t i;
2132 
2133 	for (i = 0; i < ARRAY_SIZE(regmap); i++)
2134 		if (regmap[i].code == priv->regioncode) {
2135 			regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
2136 			break;
2137 		}
2138 }
2139 
lbs_reg_notifier(struct wiphy * wiphy,struct regulatory_request * request)2140 static void lbs_reg_notifier(struct wiphy *wiphy,
2141 			     struct regulatory_request *request)
2142 {
2143 	struct lbs_private *priv = wiphy_priv(wiphy);
2144 
2145 	memcpy(priv->country_code, request->alpha2, sizeof(request->alpha2));
2146 	if (lbs_iface_active(priv))
2147 		lbs_set_11d_domain_info(priv);
2148 }
2149 
2150 /*
2151  * This function get's called after lbs_setup_firmware() determined the
2152  * firmware capabities. So we can setup the wiphy according to our
2153  * hardware/firmware.
2154  */
lbs_cfg_register(struct lbs_private * priv)2155 int lbs_cfg_register(struct lbs_private *priv)
2156 {
2157 	struct wireless_dev *wdev = priv->wdev;
2158 	int ret;
2159 
2160 	wdev->wiphy->max_scan_ssids = 1;
2161 	wdev->wiphy->max_scan_ie_len = 256;
2162 	wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2163 
2164 	wdev->wiphy->interface_modes =
2165 			BIT(NL80211_IFTYPE_STATION) |
2166 			BIT(NL80211_IFTYPE_ADHOC);
2167 	if (lbs_rtap_supported(priv))
2168 		wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
2169 	if (lbs_mesh_activated(priv))
2170 		wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MESH_POINT);
2171 
2172 	wdev->wiphy->bands[NL80211_BAND_2GHZ] = &lbs_band_2ghz;
2173 
2174 	/*
2175 	 * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
2176 	 * never seen a firmware without WPA
2177 	 */
2178 	wdev->wiphy->cipher_suites = cipher_suites;
2179 	wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
2180 	wdev->wiphy->reg_notifier = lbs_reg_notifier;
2181 
2182 	ret = wiphy_register(wdev->wiphy);
2183 	if (ret < 0)
2184 		pr_err("cannot register wiphy device\n");
2185 
2186 	priv->wiphy_registered = true;
2187 
2188 	ret = register_netdev(priv->dev);
2189 	if (ret)
2190 		pr_err("cannot register network device\n");
2191 
2192 	INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
2193 
2194 	lbs_cfg_set_regulatory_hint(priv);
2195 
2196 	return ret;
2197 }
2198 
lbs_scan_deinit(struct lbs_private * priv)2199 void lbs_scan_deinit(struct lbs_private *priv)
2200 {
2201 	cancel_delayed_work_sync(&priv->scan_work);
2202 }
2203 
2204 
lbs_cfg_free(struct lbs_private * priv)2205 void lbs_cfg_free(struct lbs_private *priv)
2206 {
2207 	struct wireless_dev *wdev = priv->wdev;
2208 
2209 	if (!wdev)
2210 		return;
2211 
2212 	if (priv->wiphy_registered)
2213 		wiphy_unregister(wdev->wiphy);
2214 
2215 	if (wdev->wiphy)
2216 		wiphy_free(wdev->wiphy);
2217 
2218 	kfree(wdev);
2219 }
2220