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 <asm/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 		pos += lbs_add_ssid_tlv(pos, ssid_eid + 2, ssid_eid[1]);
1155 	else
1156 		lbs_deb_assoc("no SSID\n");
1157 	rcu_read_unlock();
1158 
1159 	/* add DS param TLV */
1160 	if (bss->channel)
1161 		pos += lbs_add_channel_tlv(pos, bss->channel->hw_value);
1162 	else
1163 		lbs_deb_assoc("no channel\n");
1164 
1165 	/* add (empty) CF param TLV */
1166 	pos += lbs_add_cf_param_tlv(pos);
1167 
1168 	/* add rates TLV */
1169 	tmp = pos + 4; /* skip Marvell IE header */
1170 	pos += lbs_add_common_rates_tlv(pos, bss);
1171 	lbs_deb_hex(LBS_DEB_ASSOC, "Common Rates", tmp, pos - tmp);
1172 
1173 	/* add auth type TLV */
1174 	if (MRVL_FW_MAJOR_REV(priv->fwrelease) >= 9)
1175 		pos += lbs_add_auth_type_tlv(pos, sme->auth_type);
1176 
1177 	/* add WPA/WPA2 TLV */
1178 	if (sme->ie && sme->ie_len)
1179 		pos += lbs_add_wpa_tlv(pos, sme->ie, sme->ie_len);
1180 
1181 	len = sizeof(*cmd) + (u16)(pos - (u8 *) &cmd->iebuf);
1182 	cmd->hdr.size = cpu_to_le16(len);
1183 
1184 	lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_CMD", (u8 *) cmd,
1185 			le16_to_cpu(cmd->hdr.size));
1186 
1187 	/* store for later use */
1188 	memcpy(priv->assoc_bss, bss->bssid, ETH_ALEN);
1189 
1190 	ret = lbs_cmd_with_response(priv, CMD_802_11_ASSOCIATE, cmd);
1191 	if (ret)
1192 		goto done;
1193 
1194 	/* generate connect message to cfg80211 */
1195 
1196 	resp = (void *) cmd; /* recast for easier field access */
1197 	status = le16_to_cpu(resp->statuscode);
1198 
1199 	/* Older FW versions map the IEEE 802.11 Status Code in the association
1200 	 * response to the following values returned in resp->statuscode:
1201 	 *
1202 	 *    IEEE Status Code                Marvell Status Code
1203 	 *    0                       ->      0x0000 ASSOC_RESULT_SUCCESS
1204 	 *    13                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1205 	 *    14                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1206 	 *    15                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1207 	 *    16                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
1208 	 *    others                  ->      0x0003 ASSOC_RESULT_REFUSED
1209 	 *
1210 	 * Other response codes:
1211 	 *    0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
1212 	 *    0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
1213 	 *                                    association response from the AP)
1214 	 */
1215 	if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1216 		switch (status) {
1217 		case 0:
1218 			break;
1219 		case 1:
1220 			lbs_deb_assoc("invalid association parameters\n");
1221 			status = WLAN_STATUS_CAPS_UNSUPPORTED;
1222 			break;
1223 		case 2:
1224 			lbs_deb_assoc("timer expired while waiting for AP\n");
1225 			status = WLAN_STATUS_AUTH_TIMEOUT;
1226 			break;
1227 		case 3:
1228 			lbs_deb_assoc("association refused by AP\n");
1229 			status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1230 			break;
1231 		case 4:
1232 			lbs_deb_assoc("authentication refused by AP\n");
1233 			status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1234 			break;
1235 		default:
1236 			lbs_deb_assoc("association failure %d\n", status);
1237 			/* v5 OLPC firmware does return the AP status code if
1238 			 * it's not one of the values above.  Let that through.
1239 			 */
1240 			break;
1241 		}
1242 	}
1243 
1244 	lbs_deb_assoc("status %d, statuscode 0x%04x, capability 0x%04x, "
1245 		      "aid 0x%04x\n", status, le16_to_cpu(resp->statuscode),
1246 		      le16_to_cpu(resp->capability), le16_to_cpu(resp->aid));
1247 
1248 	resp_ie_len = le16_to_cpu(resp->hdr.size)
1249 		- sizeof(resp->hdr)
1250 		- 6;
1251 	cfg80211_connect_result(priv->dev,
1252 				priv->assoc_bss,
1253 				sme->ie, sme->ie_len,
1254 				resp->iebuf, resp_ie_len,
1255 				status,
1256 				GFP_KERNEL);
1257 
1258 	if (status == 0) {
1259 		/* TODO: get rid of priv->connect_status */
1260 		priv->connect_status = LBS_CONNECTED;
1261 		netif_carrier_on(priv->dev);
1262 		if (!priv->tx_pending_len)
1263 			netif_tx_wake_all_queues(priv->dev);
1264 	}
1265 
1266 	kfree(cmd);
1267 done:
1268 	return ret;
1269 }
1270 
1271 static struct cfg80211_scan_request *
_new_connect_scan_req(struct wiphy * wiphy,struct cfg80211_connect_params * sme)1272 _new_connect_scan_req(struct wiphy *wiphy, struct cfg80211_connect_params *sme)
1273 {
1274 	struct cfg80211_scan_request *creq = NULL;
1275 	int i, n_channels = ieee80211_get_num_supported_channels(wiphy);
1276 	enum nl80211_band band;
1277 
1278 	creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1279 		       n_channels * sizeof(void *),
1280 		       GFP_ATOMIC);
1281 	if (!creq)
1282 		return NULL;
1283 
1284 	/* SSIDs come after channels */
1285 	creq->ssids = (void *)&creq->channels[n_channels];
1286 	creq->n_channels = n_channels;
1287 	creq->n_ssids = 1;
1288 
1289 	/* Scan all available channels */
1290 	i = 0;
1291 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1292 		int j;
1293 
1294 		if (!wiphy->bands[band])
1295 			continue;
1296 
1297 		for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
1298 			/* ignore disabled channels */
1299 			if (wiphy->bands[band]->channels[j].flags &
1300 						IEEE80211_CHAN_DISABLED)
1301 				continue;
1302 
1303 			creq->channels[i] = &wiphy->bands[band]->channels[j];
1304 			i++;
1305 		}
1306 	}
1307 	if (i) {
1308 		/* Set real number of channels specified in creq->channels[] */
1309 		creq->n_channels = i;
1310 
1311 		/* Scan for the SSID we're going to connect to */
1312 		memcpy(creq->ssids[0].ssid, sme->ssid, sme->ssid_len);
1313 		creq->ssids[0].ssid_len = sme->ssid_len;
1314 	} else {
1315 		/* No channels found... */
1316 		kfree(creq);
1317 		creq = NULL;
1318 	}
1319 
1320 	return creq;
1321 }
1322 
lbs_cfg_connect(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_connect_params * sme)1323 static int lbs_cfg_connect(struct wiphy *wiphy, struct net_device *dev,
1324 			   struct cfg80211_connect_params *sme)
1325 {
1326 	struct lbs_private *priv = wiphy_priv(wiphy);
1327 	struct cfg80211_bss *bss = NULL;
1328 	int ret = 0;
1329 	u8 preamble = RADIO_PREAMBLE_SHORT;
1330 
1331 	if (dev == priv->mesh_dev)
1332 		return -EOPNOTSUPP;
1333 
1334 	if (!sme->bssid) {
1335 		struct cfg80211_scan_request *creq;
1336 
1337 		/*
1338 		 * Scan for the requested network after waiting for existing
1339 		 * scans to finish.
1340 		 */
1341 		lbs_deb_assoc("assoc: waiting for existing scans\n");
1342 		wait_event_interruptible_timeout(priv->scan_q,
1343 						 (priv->scan_req == NULL),
1344 						 (15 * HZ));
1345 
1346 		creq = _new_connect_scan_req(wiphy, sme);
1347 		if (!creq) {
1348 			ret = -EINVAL;
1349 			goto done;
1350 		}
1351 
1352 		lbs_deb_assoc("assoc: scanning for compatible AP\n");
1353 		_internal_start_scan(priv, true, creq);
1354 
1355 		lbs_deb_assoc("assoc: waiting for scan to complete\n");
1356 		wait_event_interruptible_timeout(priv->scan_q,
1357 						 (priv->scan_req == NULL),
1358 						 (15 * HZ));
1359 		lbs_deb_assoc("assoc: scanning completed\n");
1360 	}
1361 
1362 	/* Find the BSS we want using available scan results */
1363 	bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
1364 		sme->ssid, sme->ssid_len, IEEE80211_BSS_TYPE_ESS,
1365 		IEEE80211_PRIVACY_ANY);
1366 	if (!bss) {
1367 		wiphy_err(wiphy, "assoc: bss %pM not in scan results\n",
1368 			  sme->bssid);
1369 		ret = -ENOENT;
1370 		goto done;
1371 	}
1372 	lbs_deb_assoc("trying %pM\n", bss->bssid);
1373 	lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
1374 		      sme->crypto.cipher_group,
1375 		      sme->key_idx, sme->key_len);
1376 
1377 	/* As this is a new connection, clear locally stored WEP keys */
1378 	priv->wep_tx_key = 0;
1379 	memset(priv->wep_key, 0, sizeof(priv->wep_key));
1380 	memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));
1381 
1382 	/* set/remove WEP keys */
1383 	switch (sme->crypto.cipher_group) {
1384 	case WLAN_CIPHER_SUITE_WEP40:
1385 	case WLAN_CIPHER_SUITE_WEP104:
1386 		/* Store provided WEP keys in priv-> */
1387 		priv->wep_tx_key = sme->key_idx;
1388 		priv->wep_key_len[sme->key_idx] = sme->key_len;
1389 		memcpy(priv->wep_key[sme->key_idx], sme->key, sme->key_len);
1390 		/* Set WEP keys and WEP mode */
1391 		lbs_set_wep_keys(priv);
1392 		priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1393 		lbs_set_mac_control(priv);
1394 		/* No RSN mode for WEP */
1395 		lbs_enable_rsn(priv, 0);
1396 		break;
1397 	case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
1398 		/*
1399 		 * If we don't have no WEP, no WPA and no WPA2,
1400 		 * we remove all keys like in the WPA/WPA2 setup,
1401 		 * we just don't set RSN.
1402 		 *
1403 		 * Therefore: fall-through
1404 		 */
1405 	case WLAN_CIPHER_SUITE_TKIP:
1406 	case WLAN_CIPHER_SUITE_CCMP:
1407 		/* Remove WEP keys and WEP mode */
1408 		lbs_remove_wep_keys(priv);
1409 		priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1410 		lbs_set_mac_control(priv);
1411 
1412 		/* clear the WPA/WPA2 keys */
1413 		lbs_set_key_material(priv,
1414 			KEY_TYPE_ID_WEP, /* doesn't matter */
1415 			KEY_INFO_WPA_UNICAST,
1416 			NULL, 0);
1417 		lbs_set_key_material(priv,
1418 			KEY_TYPE_ID_WEP, /* doesn't matter */
1419 			KEY_INFO_WPA_MCAST,
1420 			NULL, 0);
1421 		/* RSN mode for WPA/WPA2 */
1422 		lbs_enable_rsn(priv, sme->crypto.cipher_group != 0);
1423 		break;
1424 	default:
1425 		wiphy_err(wiphy, "unsupported cipher group 0x%x\n",
1426 			  sme->crypto.cipher_group);
1427 		ret = -ENOTSUPP;
1428 		goto done;
1429 	}
1430 
1431 	ret = lbs_set_authtype(priv, sme);
1432 	if (ret == -ENOTSUPP) {
1433 		wiphy_err(wiphy, "unsupported authtype 0x%x\n", sme->auth_type);
1434 		goto done;
1435 	}
1436 
1437 	lbs_set_radio(priv, preamble, 1);
1438 
1439 	/* Do the actual association */
1440 	ret = lbs_associate(priv, bss, sme);
1441 
1442  done:
1443 	if (bss)
1444 		cfg80211_put_bss(wiphy, bss);
1445 	return ret;
1446 }
1447 
lbs_disconnect(struct lbs_private * priv,u16 reason)1448 int lbs_disconnect(struct lbs_private *priv, u16 reason)
1449 {
1450 	struct cmd_ds_802_11_deauthenticate cmd;
1451 	int ret;
1452 
1453 	memset(&cmd, 0, sizeof(cmd));
1454 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1455 	/* Mildly ugly to use a locally store my own BSSID ... */
1456 	memcpy(cmd.macaddr, &priv->assoc_bss, ETH_ALEN);
1457 	cmd.reasoncode = cpu_to_le16(reason);
1458 
1459 	ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1460 	if (ret)
1461 		return ret;
1462 
1463 	cfg80211_disconnected(priv->dev,
1464 			reason,
1465 			NULL, 0, true,
1466 			GFP_KERNEL);
1467 	priv->connect_status = LBS_DISCONNECTED;
1468 
1469 	return 0;
1470 }
1471 
lbs_cfg_disconnect(struct wiphy * wiphy,struct net_device * dev,u16 reason_code)1472 static int lbs_cfg_disconnect(struct wiphy *wiphy, struct net_device *dev,
1473 	u16 reason_code)
1474 {
1475 	struct lbs_private *priv = wiphy_priv(wiphy);
1476 
1477 	if (dev == priv->mesh_dev)
1478 		return -EOPNOTSUPP;
1479 
1480 	/* store for lbs_cfg_ret_disconnect() */
1481 	priv->disassoc_reason = reason_code;
1482 
1483 	return lbs_disconnect(priv, reason_code);
1484 }
1485 
lbs_cfg_set_default_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool unicast,bool multicast)1486 static int lbs_cfg_set_default_key(struct wiphy *wiphy,
1487 				   struct net_device *netdev, int link_id,
1488 				   u8 key_index, bool unicast,
1489 				   bool multicast)
1490 {
1491 	struct lbs_private *priv = wiphy_priv(wiphy);
1492 
1493 	if (netdev == priv->mesh_dev)
1494 		return -EOPNOTSUPP;
1495 
1496 	if (key_index != priv->wep_tx_key) {
1497 		lbs_deb_assoc("set_default_key: to %d\n", key_index);
1498 		priv->wep_tx_key = key_index;
1499 		lbs_set_wep_keys(priv);
1500 	}
1501 
1502 	return 0;
1503 }
1504 
1505 
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)1506 static int lbs_cfg_add_key(struct wiphy *wiphy, struct net_device *netdev,
1507 			   int link_id, u8 idx, bool pairwise,
1508 			   const u8 *mac_addr, struct key_params *params)
1509 {
1510 	struct lbs_private *priv = wiphy_priv(wiphy);
1511 	u16 key_info;
1512 	u16 key_type;
1513 	int ret = 0;
1514 
1515 	if (netdev == priv->mesh_dev)
1516 		return -EOPNOTSUPP;
1517 
1518 	lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
1519 		      params->cipher, mac_addr);
1520 	lbs_deb_assoc("add_key: key index %d, key len %d\n",
1521 		      idx, params->key_len);
1522 	if (params->key_len)
1523 		lbs_deb_hex(LBS_DEB_CFG80211, "KEY",
1524 			    params->key, params->key_len);
1525 
1526 	lbs_deb_assoc("add_key: seq len %d\n", params->seq_len);
1527 	if (params->seq_len)
1528 		lbs_deb_hex(LBS_DEB_CFG80211, "SEQ",
1529 			    params->seq, params->seq_len);
1530 
1531 	switch (params->cipher) {
1532 	case WLAN_CIPHER_SUITE_WEP40:
1533 	case WLAN_CIPHER_SUITE_WEP104:
1534 		/* actually compare if something has changed ... */
1535 		if ((priv->wep_key_len[idx] != params->key_len) ||
1536 			memcmp(priv->wep_key[idx],
1537 			       params->key, params->key_len) != 0) {
1538 			priv->wep_key_len[idx] = params->key_len;
1539 			memcpy(priv->wep_key[idx],
1540 			       params->key, params->key_len);
1541 			lbs_set_wep_keys(priv);
1542 		}
1543 		break;
1544 	case WLAN_CIPHER_SUITE_TKIP:
1545 	case WLAN_CIPHER_SUITE_CCMP:
1546 		key_info = KEY_INFO_WPA_ENABLED | ((idx == 0)
1547 						   ? KEY_INFO_WPA_UNICAST
1548 						   : KEY_INFO_WPA_MCAST);
1549 		key_type = (params->cipher == WLAN_CIPHER_SUITE_TKIP)
1550 			? KEY_TYPE_ID_TKIP
1551 			: KEY_TYPE_ID_AES;
1552 		lbs_set_key_material(priv,
1553 				     key_type,
1554 				     key_info,
1555 				     params->key, params->key_len);
1556 		break;
1557 	default:
1558 		wiphy_err(wiphy, "unhandled cipher 0x%x\n", params->cipher);
1559 		ret = -ENOTSUPP;
1560 		break;
1561 	}
1562 
1563 	return ret;
1564 }
1565 
1566 
lbs_cfg_del_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr)1567 static int lbs_cfg_del_key(struct wiphy *wiphy, struct net_device *netdev,
1568 			   int link_id, u8 key_index, bool pairwise,
1569 			   const u8 *mac_addr)
1570 {
1571 
1572 	lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
1573 		      key_index, mac_addr);
1574 
1575 #ifdef TODO
1576 	struct lbs_private *priv = wiphy_priv(wiphy);
1577 	/*
1578 	 * I think can keep this a NO-OP, because:
1579 
1580 	 * - we clear all keys whenever we do lbs_cfg_connect() anyway
1581 	 * - neither "iw" nor "wpa_supplicant" won't call this during
1582 	 *   an ongoing connection
1583 	 * - TODO: but I have to check if this is still true when
1584 	 *   I set the AP to periodic re-keying
1585 	 * - we've not kzallec() something when we've added a key at
1586 	 *   lbs_cfg_connect() or lbs_cfg_add_key().
1587 	 *
1588 	 * This causes lbs_cfg_del_key() only called at disconnect time,
1589 	 * where we'd just waste time deleting a key that is not going
1590 	 * to be used anyway.
1591 	 */
1592 	if (key_index < 3 && priv->wep_key_len[key_index]) {
1593 		priv->wep_key_len[key_index] = 0;
1594 		lbs_set_wep_keys(priv);
1595 	}
1596 #endif
1597 
1598 	return 0;
1599 }
1600 
1601 
1602 /*
1603  * Get station
1604  */
1605 
lbs_cfg_get_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_info * sinfo)1606 static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
1607 			       const u8 *mac, struct station_info *sinfo)
1608 {
1609 	struct lbs_private *priv = wiphy_priv(wiphy);
1610 	s8 signal, noise;
1611 	int ret;
1612 	size_t i;
1613 
1614 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
1615 			 BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
1616 			 BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
1617 			 BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
1618 	sinfo->tx_bytes = priv->dev->stats.tx_bytes;
1619 	sinfo->tx_packets = priv->dev->stats.tx_packets;
1620 	sinfo->rx_bytes = priv->dev->stats.rx_bytes;
1621 	sinfo->rx_packets = priv->dev->stats.rx_packets;
1622 
1623 	/* Get current RSSI */
1624 	ret = lbs_get_rssi(priv, &signal, &noise);
1625 	if (ret == 0) {
1626 		sinfo->signal = signal;
1627 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
1628 	}
1629 
1630 	/* Convert priv->cur_rate from hw_value to NL80211 value */
1631 	for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
1632 		if (priv->cur_rate == lbs_rates[i].hw_value) {
1633 			sinfo->txrate.legacy = lbs_rates[i].bitrate;
1634 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1635 			break;
1636 		}
1637 	}
1638 
1639 	return 0;
1640 }
1641 
1642 
1643 
1644 
1645 /*
1646  * Change interface
1647  */
1648 
lbs_change_intf(struct wiphy * wiphy,struct net_device * dev,enum nl80211_iftype type,struct vif_params * params)1649 static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
1650 	enum nl80211_iftype type,
1651 	       struct vif_params *params)
1652 {
1653 	struct lbs_private *priv = wiphy_priv(wiphy);
1654 	int ret = 0;
1655 
1656 	if (dev == priv->mesh_dev)
1657 		return -EOPNOTSUPP;
1658 
1659 	switch (type) {
1660 	case NL80211_IFTYPE_MONITOR:
1661 	case NL80211_IFTYPE_STATION:
1662 	case NL80211_IFTYPE_ADHOC:
1663 		break;
1664 	default:
1665 		return -EOPNOTSUPP;
1666 	}
1667 
1668 	if (priv->iface_running)
1669 		ret = lbs_set_iface_type(priv, type);
1670 
1671 	if (!ret)
1672 		priv->wdev->iftype = type;
1673 
1674 	return ret;
1675 }
1676 
1677 
1678 
1679 /*
1680  * IBSS (Ad-Hoc)
1681  */
1682 
1683 /*
1684  * The firmware needs the following bits masked out of the beacon-derived
1685  * capability field when associating/joining to a BSS:
1686  *  9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
1687  */
1688 #define CAPINFO_MASK (~(0xda00))
1689 
1690 
lbs_join_post(struct lbs_private * priv,struct cfg80211_ibss_params * params,u8 * bssid,u16 capability)1691 static void lbs_join_post(struct lbs_private *priv,
1692 			  struct cfg80211_ibss_params *params,
1693 			  u8 *bssid, u16 capability)
1694 {
1695 	u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN + /* ssid */
1696 		   2 + 4 +                      /* basic rates */
1697 		   2 + 1 +                      /* DS parameter */
1698 		   2 + 2 +                      /* atim */
1699 		   2 + 8];                      /* extended rates */
1700 	u8 *fake = fake_ie;
1701 	struct cfg80211_bss *bss;
1702 
1703 	/*
1704 	 * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
1705 	 * the real IE from the firmware. So we fabricate a fake IE based on
1706 	 * what the firmware actually sends (sniffed with wireshark).
1707 	 */
1708 	/* Fake SSID IE */
1709 	*fake++ = WLAN_EID_SSID;
1710 	*fake++ = params->ssid_len;
1711 	memcpy(fake, params->ssid, params->ssid_len);
1712 	fake += params->ssid_len;
1713 	/* Fake supported basic rates IE */
1714 	*fake++ = WLAN_EID_SUPP_RATES;
1715 	*fake++ = 4;
1716 	*fake++ = 0x82;
1717 	*fake++ = 0x84;
1718 	*fake++ = 0x8b;
1719 	*fake++ = 0x96;
1720 	/* Fake DS channel IE */
1721 	*fake++ = WLAN_EID_DS_PARAMS;
1722 	*fake++ = 1;
1723 	*fake++ = params->chandef.chan->hw_value;
1724 	/* Fake IBSS params IE */
1725 	*fake++ = WLAN_EID_IBSS_PARAMS;
1726 	*fake++ = 2;
1727 	*fake++ = 0; /* ATIM=0 */
1728 	*fake++ = 0;
1729 	/* Fake extended rates IE, TODO: don't add this for 802.11b only,
1730 	 * but I don't know how this could be checked */
1731 	*fake++ = WLAN_EID_EXT_SUPP_RATES;
1732 	*fake++ = 8;
1733 	*fake++ = 0x0c;
1734 	*fake++ = 0x12;
1735 	*fake++ = 0x18;
1736 	*fake++ = 0x24;
1737 	*fake++ = 0x30;
1738 	*fake++ = 0x48;
1739 	*fake++ = 0x60;
1740 	*fake++ = 0x6c;
1741 	lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
1742 
1743 	bss = cfg80211_inform_bss(priv->wdev->wiphy,
1744 				  params->chandef.chan,
1745 				  CFG80211_BSS_FTYPE_UNKNOWN,
1746 				  bssid,
1747 				  0,
1748 				  capability,
1749 				  params->beacon_interval,
1750 				  fake_ie, fake - fake_ie,
1751 				  0, GFP_KERNEL);
1752 	cfg80211_put_bss(priv->wdev->wiphy, bss);
1753 
1754 	cfg80211_ibss_joined(priv->dev, bssid, params->chandef.chan,
1755 			     GFP_KERNEL);
1756 
1757 	/* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
1758 	priv->connect_status = LBS_CONNECTED;
1759 	netif_carrier_on(priv->dev);
1760 	if (!priv->tx_pending_len)
1761 		netif_wake_queue(priv->dev);
1762 }
1763 
lbs_ibss_join_existing(struct lbs_private * priv,struct cfg80211_ibss_params * params,struct cfg80211_bss * bss)1764 static int lbs_ibss_join_existing(struct lbs_private *priv,
1765 	struct cfg80211_ibss_params *params,
1766 	struct cfg80211_bss *bss)
1767 {
1768 	const u8 *rates_eid;
1769 	struct cmd_ds_802_11_ad_hoc_join cmd;
1770 	u8 preamble = RADIO_PREAMBLE_SHORT;
1771 	int ret = 0;
1772 	int hw, i;
1773 	u8 rates_max;
1774 	u8 *rates;
1775 
1776 	/* TODO: set preamble based on scan result */
1777 	ret = lbs_set_radio(priv, preamble, 1);
1778 	if (ret)
1779 		goto out;
1780 
1781 	/*
1782 	 * Example CMD_802_11_AD_HOC_JOIN command:
1783 	 *
1784 	 * command         2c 00         CMD_802_11_AD_HOC_JOIN
1785 	 * size            65 00
1786 	 * sequence        xx xx
1787 	 * result          00 00
1788 	 * bssid           02 27 27 97 2f 96
1789 	 * ssid            49 42 53 53 00 00 00 00
1790 	 *                 00 00 00 00 00 00 00 00
1791 	 *                 00 00 00 00 00 00 00 00
1792 	 *                 00 00 00 00 00 00 00 00
1793 	 * type            02            CMD_BSS_TYPE_IBSS
1794 	 * beacon period   64 00
1795 	 * dtim period     00
1796 	 * timestamp       00 00 00 00 00 00 00 00
1797 	 * localtime       00 00 00 00 00 00 00 00
1798 	 * IE DS           03
1799 	 * IE DS len       01
1800 	 * IE DS channel   01
1801 	 * reserveed       00 00 00 00
1802 	 * IE IBSS         06
1803 	 * IE IBSS len     02
1804 	 * IE IBSS atim    00 00
1805 	 * reserved        00 00 00 00
1806 	 * capability      02 00
1807 	 * rates           82 84 8b 96 0c 12 18 24 30 48 60 6c 00
1808 	 * fail timeout    ff 00
1809 	 * probe delay     00 00
1810 	 */
1811 	memset(&cmd, 0, sizeof(cmd));
1812 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1813 
1814 	memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
1815 	memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
1816 	cmd.bss.type = CMD_BSS_TYPE_IBSS;
1817 	cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
1818 	cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
1819 	cmd.bss.ds.header.len = 1;
1820 	cmd.bss.ds.channel = params->chandef.chan->hw_value;
1821 	cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1822 	cmd.bss.ibss.header.len = 2;
1823 	cmd.bss.ibss.atimwindow = 0;
1824 	cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
1825 
1826 	/* set rates to the intersection of our rates and the rates in the
1827 	   bss */
1828 	rcu_read_lock();
1829 	rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1830 	if (!rates_eid) {
1831 		lbs_add_rates(cmd.bss.rates);
1832 	} else {
1833 		rates_max = rates_eid[1];
1834 		if (rates_max > MAX_RATES) {
1835 			lbs_deb_join("invalid rates");
1836 			rcu_read_unlock();
1837 			ret = -EINVAL;
1838 			goto out;
1839 		}
1840 		rates = cmd.bss.rates;
1841 		for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
1842 			u8 hw_rate = lbs_rates[hw].bitrate / 5;
1843 			for (i = 0; i < rates_max; i++) {
1844 				if (hw_rate == (rates_eid[i+2] & 0x7f)) {
1845 					u8 rate = rates_eid[i+2];
1846 					if (rate == 0x02 || rate == 0x04 ||
1847 					    rate == 0x0b || rate == 0x16)
1848 						rate |= 0x80;
1849 					*rates++ = rate;
1850 				}
1851 			}
1852 		}
1853 	}
1854 	rcu_read_unlock();
1855 
1856 	/* Only v8 and below support setting this */
1857 	if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1858 		cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1859 		cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1860 	}
1861 	ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
1862 	if (ret)
1863 		goto out;
1864 
1865 	/*
1866 	 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1867 	 *
1868 	 * response        2c 80
1869 	 * size            09 00
1870 	 * sequence        xx xx
1871 	 * result          00 00
1872 	 * reserved        00
1873 	 */
1874 	lbs_join_post(priv, params, bss->bssid, bss->capability);
1875 
1876  out:
1877 	return ret;
1878 }
1879 
1880 
1881 
lbs_ibss_start_new(struct lbs_private * priv,struct cfg80211_ibss_params * params)1882 static int lbs_ibss_start_new(struct lbs_private *priv,
1883 	struct cfg80211_ibss_params *params)
1884 {
1885 	struct cmd_ds_802_11_ad_hoc_start cmd;
1886 	struct cmd_ds_802_11_ad_hoc_result *resp =
1887 		(struct cmd_ds_802_11_ad_hoc_result *) &cmd;
1888 	u8 preamble = RADIO_PREAMBLE_SHORT;
1889 	int ret = 0;
1890 	u16 capability;
1891 
1892 	ret = lbs_set_radio(priv, preamble, 1);
1893 	if (ret)
1894 		goto out;
1895 
1896 	/*
1897 	 * Example CMD_802_11_AD_HOC_START command:
1898 	 *
1899 	 * command         2b 00         CMD_802_11_AD_HOC_START
1900 	 * size            b1 00
1901 	 * sequence        xx xx
1902 	 * result          00 00
1903 	 * ssid            54 45 53 54 00 00 00 00
1904 	 *                 00 00 00 00 00 00 00 00
1905 	 *                 00 00 00 00 00 00 00 00
1906 	 *                 00 00 00 00 00 00 00 00
1907 	 * bss type        02
1908 	 * beacon period   64 00
1909 	 * dtim period     00
1910 	 * IE IBSS         06
1911 	 * IE IBSS len     02
1912 	 * IE IBSS atim    00 00
1913 	 * reserved        00 00 00 00
1914 	 * IE DS           03
1915 	 * IE DS len       01
1916 	 * IE DS channel   01
1917 	 * reserved        00 00 00 00
1918 	 * probe delay     00 00
1919 	 * capability      02 00
1920 	 * rates           82 84 8b 96   (basic rates with have bit 7 set)
1921 	 *                 0c 12 18 24 30 48 60 6c
1922 	 * padding         100 bytes
1923 	 */
1924 	memset(&cmd, 0, sizeof(cmd));
1925 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1926 	memcpy(cmd.ssid, params->ssid, params->ssid_len);
1927 	cmd.bsstype = CMD_BSS_TYPE_IBSS;
1928 	cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
1929 	cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1930 	cmd.ibss.header.len = 2;
1931 	cmd.ibss.atimwindow = 0;
1932 	cmd.ds.header.id = WLAN_EID_DS_PARAMS;
1933 	cmd.ds.header.len = 1;
1934 	cmd.ds.channel = params->chandef.chan->hw_value;
1935 	/* Only v8 and below support setting probe delay */
1936 	if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
1937 		cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1938 	/* TODO: mix in WLAN_CAPABILITY_PRIVACY */
1939 	capability = WLAN_CAPABILITY_IBSS;
1940 	cmd.capability = cpu_to_le16(capability);
1941 	lbs_add_rates(cmd.rates);
1942 
1943 
1944 	ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
1945 	if (ret)
1946 		goto out;
1947 
1948 	/*
1949 	 * This is a sample response to CMD_802_11_AD_HOC_JOIN:
1950 	 *
1951 	 * response        2b 80
1952 	 * size            14 00
1953 	 * sequence        xx xx
1954 	 * result          00 00
1955 	 * reserved        00
1956 	 * bssid           02 2b 7b 0f 86 0e
1957 	 */
1958 	lbs_join_post(priv, params, resp->bssid, capability);
1959 
1960  out:
1961 	return ret;
1962 }
1963 
1964 
lbs_join_ibss(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_ibss_params * params)1965 static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1966 		struct cfg80211_ibss_params *params)
1967 {
1968 	struct lbs_private *priv = wiphy_priv(wiphy);
1969 	int ret = 0;
1970 	struct cfg80211_bss *bss;
1971 
1972 	if (dev == priv->mesh_dev)
1973 		return -EOPNOTSUPP;
1974 
1975 	if (!params->chandef.chan) {
1976 		ret = -ENOTSUPP;
1977 		goto out;
1978 	}
1979 
1980 	ret = lbs_set_channel(priv, params->chandef.chan->hw_value);
1981 	if (ret)
1982 		goto out;
1983 
1984 	/* Search if someone is beaconing. This assumes that the
1985 	 * bss list is populated already */
1986 	bss = cfg80211_get_bss(wiphy, params->chandef.chan, params->bssid,
1987 		params->ssid, params->ssid_len,
1988 		IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
1989 
1990 	if (bss) {
1991 		ret = lbs_ibss_join_existing(priv, params, bss);
1992 		cfg80211_put_bss(wiphy, bss);
1993 	} else
1994 		ret = lbs_ibss_start_new(priv, params);
1995 
1996 
1997  out:
1998 	return ret;
1999 }
2000 
2001 
lbs_leave_ibss(struct wiphy * wiphy,struct net_device * dev)2002 static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2003 {
2004 	struct lbs_private *priv = wiphy_priv(wiphy);
2005 	struct cmd_ds_802_11_ad_hoc_stop cmd;
2006 	int ret = 0;
2007 
2008 	if (dev == priv->mesh_dev)
2009 		return -EOPNOTSUPP;
2010 
2011 	memset(&cmd, 0, sizeof(cmd));
2012 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2013 	ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
2014 
2015 	/* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
2016 	lbs_mac_event_disconnected(priv, true);
2017 
2018 	return ret;
2019 }
2020 
2021 
2022 
lbs_set_power_mgmt(struct wiphy * wiphy,struct net_device * dev,bool enabled,int timeout)2023 static int lbs_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2024 			      bool enabled, int timeout)
2025 {
2026 	struct lbs_private *priv = wiphy_priv(wiphy);
2027 
2028 	if  (!(priv->fwcapinfo & FW_CAPINFO_PS)) {
2029 		if (!enabled)
2030 			return 0;
2031 		else
2032 			return -EINVAL;
2033 	}
2034 	/* firmware does not work well with too long latency with power saving
2035 	 * enabled, so do not enable it if there is only polling, no
2036 	 * interrupts (like in some sdio hosts which can only
2037 	 * poll for sdio irqs)
2038 	 */
2039 	if  (priv->is_polling) {
2040 		if (!enabled)
2041 			return 0;
2042 		else
2043 			return -EINVAL;
2044 	}
2045 	if (!enabled) {
2046 		priv->psmode = LBS802_11POWERMODECAM;
2047 		if (priv->psstate != PS_STATE_FULL_POWER)
2048 			lbs_set_ps_mode(priv,
2049 					PS_MODE_ACTION_EXIT_PS,
2050 					true);
2051 		return 0;
2052 	}
2053 	if (priv->psmode != LBS802_11POWERMODECAM)
2054 		return 0;
2055 	priv->psmode = LBS802_11POWERMODEMAX_PSP;
2056 	if (priv->connect_status == LBS_CONNECTED)
2057 		lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS, true);
2058 	return 0;
2059 }
2060 
2061 /*
2062  * Initialization
2063  */
2064 
2065 static const struct cfg80211_ops lbs_cfg80211_ops = {
2066 	.set_monitor_channel = lbs_cfg_set_monitor_channel,
2067 	.libertas_set_mesh_channel = lbs_cfg_set_mesh_channel,
2068 	.scan = lbs_cfg_scan,
2069 	.connect = lbs_cfg_connect,
2070 	.disconnect = lbs_cfg_disconnect,
2071 	.add_key = lbs_cfg_add_key,
2072 	.del_key = lbs_cfg_del_key,
2073 	.set_default_key = lbs_cfg_set_default_key,
2074 	.get_station = lbs_cfg_get_station,
2075 	.change_virtual_intf = lbs_change_intf,
2076 	.join_ibss = lbs_join_ibss,
2077 	.leave_ibss = lbs_leave_ibss,
2078 	.set_power_mgmt = lbs_set_power_mgmt,
2079 };
2080 
2081 
2082 /*
2083  * At this time lbs_private *priv doesn't even exist, so we just allocate
2084  * memory and don't initialize the wiphy further. This is postponed until we
2085  * can talk to the firmware and happens at registration time in
2086  * lbs_cfg_wiphy_register().
2087  */
lbs_cfg_alloc(struct device * dev)2088 struct wireless_dev *lbs_cfg_alloc(struct device *dev)
2089 {
2090 	int ret = 0;
2091 	struct wireless_dev *wdev;
2092 
2093 	wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
2094 	if (!wdev)
2095 		return ERR_PTR(-ENOMEM);
2096 
2097 	wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
2098 	if (!wdev->wiphy) {
2099 		dev_err(dev, "cannot allocate wiphy\n");
2100 		ret = -ENOMEM;
2101 		goto err_wiphy_new;
2102 	}
2103 
2104 	return wdev;
2105 
2106  err_wiphy_new:
2107 	kfree(wdev);
2108 	return ERR_PTR(ret);
2109 }
2110 
2111 
lbs_cfg_set_regulatory_hint(struct lbs_private * priv)2112 static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
2113 {
2114 	struct region_code_mapping {
2115 		const char *cn;
2116 		int code;
2117 	};
2118 
2119 	/* Section 5.17.2 */
2120 	static const struct region_code_mapping regmap[] = {
2121 		{"US ", 0x10}, /* US FCC */
2122 		{"CA ", 0x20}, /* Canada */
2123 		{"EU ", 0x30}, /* ETSI   */
2124 		{"ES ", 0x31}, /* Spain  */
2125 		{"FR ", 0x32}, /* France */
2126 		{"JP ", 0x40}, /* Japan  */
2127 	};
2128 	size_t i;
2129 
2130 	for (i = 0; i < ARRAY_SIZE(regmap); i++)
2131 		if (regmap[i].code == priv->regioncode) {
2132 			regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
2133 			break;
2134 		}
2135 }
2136 
lbs_reg_notifier(struct wiphy * wiphy,struct regulatory_request * request)2137 static void lbs_reg_notifier(struct wiphy *wiphy,
2138 			     struct regulatory_request *request)
2139 {
2140 	struct lbs_private *priv = wiphy_priv(wiphy);
2141 
2142 	memcpy(priv->country_code, request->alpha2, sizeof(request->alpha2));
2143 	if (lbs_iface_active(priv))
2144 		lbs_set_11d_domain_info(priv);
2145 }
2146 
2147 /*
2148  * This function get's called after lbs_setup_firmware() determined the
2149  * firmware capabities. So we can setup the wiphy according to our
2150  * hardware/firmware.
2151  */
lbs_cfg_register(struct lbs_private * priv)2152 int lbs_cfg_register(struct lbs_private *priv)
2153 {
2154 	struct wireless_dev *wdev = priv->wdev;
2155 	int ret;
2156 
2157 	wdev->wiphy->max_scan_ssids = 1;
2158 	wdev->wiphy->max_scan_ie_len = 256;
2159 	wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2160 
2161 	wdev->wiphy->interface_modes =
2162 			BIT(NL80211_IFTYPE_STATION) |
2163 			BIT(NL80211_IFTYPE_ADHOC);
2164 	if (lbs_rtap_supported(priv))
2165 		wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
2166 	if (lbs_mesh_activated(priv))
2167 		wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MESH_POINT);
2168 
2169 	wdev->wiphy->bands[NL80211_BAND_2GHZ] = &lbs_band_2ghz;
2170 
2171 	/*
2172 	 * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
2173 	 * never seen a firmware without WPA
2174 	 */
2175 	wdev->wiphy->cipher_suites = cipher_suites;
2176 	wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
2177 	wdev->wiphy->reg_notifier = lbs_reg_notifier;
2178 
2179 	ret = wiphy_register(wdev->wiphy);
2180 	if (ret < 0)
2181 		pr_err("cannot register wiphy device\n");
2182 
2183 	priv->wiphy_registered = true;
2184 
2185 	ret = register_netdev(priv->dev);
2186 	if (ret)
2187 		pr_err("cannot register network device\n");
2188 
2189 	INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
2190 
2191 	lbs_cfg_set_regulatory_hint(priv);
2192 
2193 	return ret;
2194 }
2195 
lbs_scan_deinit(struct lbs_private * priv)2196 void lbs_scan_deinit(struct lbs_private *priv)
2197 {
2198 	cancel_delayed_work_sync(&priv->scan_work);
2199 }
2200 
2201 
lbs_cfg_free(struct lbs_private * priv)2202 void lbs_cfg_free(struct lbs_private *priv)
2203 {
2204 	struct wireless_dev *wdev = priv->wdev;
2205 
2206 	if (!wdev)
2207 		return;
2208 
2209 	if (priv->wiphy_registered)
2210 		wiphy_unregister(wdev->wiphy);
2211 
2212 	if (wdev->wiphy)
2213 		wiphy_free(wdev->wiphy);
2214 
2215 	kfree(wdev);
2216 }
2217