1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for RNDIS based wireless USB devices.
4  *
5  * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
6  * Copyright (C) 2008-2009 by Jussi Kivilinna <jussi.kivilinna@iki.fi>
7  *
8  *  Portions of this file are based on NDISwrapper project,
9  *  Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani
10  *  http://ndiswrapper.sourceforge.net/
11  */
12 
13 // #define	DEBUG			// error path messages, extra info
14 // #define	VERBOSE			// more; success messages
15 
16 #include <linux/module.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/ethtool.h>
20 #include <linux/workqueue.h>
21 #include <linux/mutex.h>
22 #include <linux/mii.h>
23 #include <linux/usb.h>
24 #include <linux/usb/cdc.h>
25 #include <linux/ieee80211.h>
26 #include <linux/if_arp.h>
27 #include <linux/ctype.h>
28 #include <linux/spinlock.h>
29 #include <linux/slab.h>
30 #include <net/cfg80211.h>
31 #include <linux/usb/usbnet.h>
32 #include <linux/usb/rndis_host.h>
33 
34 
35 /* NOTE: All these are settings for Broadcom chipset */
36 static char modparam_country[4] = "EU";
37 module_param_string(country, modparam_country, 4, 0444);
38 MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
39 
40 static int modparam_frameburst = 1;
41 module_param_named(frameburst, modparam_frameburst, int, 0444);
42 MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
43 
44 static int modparam_afterburner = 0;
45 module_param_named(afterburner, modparam_afterburner, int, 0444);
46 MODULE_PARM_DESC(afterburner,
47 	"enable afterburner aka '125 High Speed Mode' (default: off)");
48 
49 static int modparam_power_save = 0;
50 module_param_named(power_save, modparam_power_save, int, 0444);
51 MODULE_PARM_DESC(power_save,
52 	"set power save mode: 0=off, 1=on, 2=fast (default: off)");
53 
54 static int modparam_power_output = 3;
55 module_param_named(power_output, modparam_power_output, int, 0444);
56 MODULE_PARM_DESC(power_output,
57 	"set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
58 
59 static int modparam_roamtrigger = -70;
60 module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
61 MODULE_PARM_DESC(roamtrigger,
62 	"set roaming dBm trigger: -80=optimize for distance, "
63 				"-60=bandwidth (default: -70)");
64 
65 static int modparam_roamdelta = 1;
66 module_param_named(roamdelta, modparam_roamdelta, int, 0444);
67 MODULE_PARM_DESC(roamdelta,
68 	"set roaming tendency: 0=aggressive, 1=moderate, "
69 				"2=conservative (default: moderate)");
70 
71 static int modparam_workaround_interval;
72 module_param_named(workaround_interval, modparam_workaround_interval,
73 							int, 0444);
74 MODULE_PARM_DESC(workaround_interval,
75 	"set stall workaround interval in msecs (0=disabled) (default: 0)");
76 
77 /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
78 #define	WL_NOISE	-96	/* typical noise level in dBm */
79 #define	WL_SIGMAX	-32	/* typical maximum signal level in dBm */
80 
81 
82 /* Assume that Broadcom 4320 (only chipset at time of writing known to be
83  * based on wireless rndis) has default txpower of 13dBm.
84  * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
85  *  100% : 20 mW ~ 13dBm
86  *   75% : 15 mW ~ 12dBm
87  *   50% : 10 mW ~ 10dBm
88  *   25% :  5 mW ~  7dBm
89  */
90 #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
91 #define BCM4320_DEFAULT_TXPOWER_DBM_75  12
92 #define BCM4320_DEFAULT_TXPOWER_DBM_50  10
93 #define BCM4320_DEFAULT_TXPOWER_DBM_25  7
94 
95 /* Known device types */
96 #define RNDIS_UNKNOWN	0
97 #define RNDIS_BCM4320A	1
98 #define RNDIS_BCM4320B	2
99 
100 
101 /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
102  * slightly modified for datatype endianess, etc
103  */
104 #define NDIS_802_11_LENGTH_SSID 32
105 #define NDIS_802_11_LENGTH_RATES 8
106 #define NDIS_802_11_LENGTH_RATES_EX 16
107 
108 enum ndis_80211_net_type {
109 	NDIS_80211_TYPE_FREQ_HOP,
110 	NDIS_80211_TYPE_DIRECT_SEQ,
111 	NDIS_80211_TYPE_OFDM_A,
112 	NDIS_80211_TYPE_OFDM_G
113 };
114 
115 enum ndis_80211_net_infra {
116 	NDIS_80211_INFRA_ADHOC,
117 	NDIS_80211_INFRA_INFRA,
118 	NDIS_80211_INFRA_AUTO_UNKNOWN
119 };
120 
121 enum ndis_80211_auth_mode {
122 	NDIS_80211_AUTH_OPEN,
123 	NDIS_80211_AUTH_SHARED,
124 	NDIS_80211_AUTH_AUTO_SWITCH,
125 	NDIS_80211_AUTH_WPA,
126 	NDIS_80211_AUTH_WPA_PSK,
127 	NDIS_80211_AUTH_WPA_NONE,
128 	NDIS_80211_AUTH_WPA2,
129 	NDIS_80211_AUTH_WPA2_PSK
130 };
131 
132 enum ndis_80211_encr_status {
133 	NDIS_80211_ENCR_WEP_ENABLED,
134 	NDIS_80211_ENCR_DISABLED,
135 	NDIS_80211_ENCR_WEP_KEY_ABSENT,
136 	NDIS_80211_ENCR_NOT_SUPPORTED,
137 	NDIS_80211_ENCR_TKIP_ENABLED,
138 	NDIS_80211_ENCR_TKIP_KEY_ABSENT,
139 	NDIS_80211_ENCR_CCMP_ENABLED,
140 	NDIS_80211_ENCR_CCMP_KEY_ABSENT
141 };
142 
143 enum ndis_80211_priv_filter {
144 	NDIS_80211_PRIV_ACCEPT_ALL,
145 	NDIS_80211_PRIV_8021X_WEP
146 };
147 
148 enum ndis_80211_status_type {
149 	NDIS_80211_STATUSTYPE_AUTHENTICATION,
150 	NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
151 	NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
152 	NDIS_80211_STATUSTYPE_RADIOSTATE,
153 };
154 
155 enum ndis_80211_media_stream_mode {
156 	NDIS_80211_MEDIA_STREAM_OFF,
157 	NDIS_80211_MEDIA_STREAM_ON
158 };
159 
160 enum ndis_80211_radio_status {
161 	NDIS_80211_RADIO_STATUS_ON,
162 	NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
163 	NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
164 };
165 
166 enum ndis_80211_addkey_bits {
167 	NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
168 	NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
169 	NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
170 	NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
171 };
172 
173 enum ndis_80211_addwep_bits {
174 	NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
175 	NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
176 };
177 
178 enum ndis_80211_power_mode {
179 	NDIS_80211_POWER_MODE_CAM,
180 	NDIS_80211_POWER_MODE_MAX_PSP,
181 	NDIS_80211_POWER_MODE_FAST_PSP,
182 };
183 
184 enum ndis_80211_pmkid_cand_list_flag_bits {
185 	NDIS_80211_PMKID_CAND_PREAUTH = cpu_to_le32(1 << 0)
186 };
187 
188 struct ndis_80211_auth_request {
189 	__le32 length;
190 	u8 bssid[ETH_ALEN];
191 	u8 padding[2];
192 	__le32 flags;
193 } __packed;
194 
195 struct ndis_80211_pmkid_candidate {
196 	u8 bssid[ETH_ALEN];
197 	u8 padding[2];
198 	__le32 flags;
199 } __packed;
200 
201 struct ndis_80211_pmkid_cand_list {
202 	__le32 version;
203 	__le32 num_candidates;
204 	struct ndis_80211_pmkid_candidate candidate_list[];
205 } __packed;
206 
207 struct ndis_80211_status_indication {
208 	__le32 status_type;
209 	union {
210 		__le32					media_stream_mode;
211 		__le32					radio_status;
212 		struct ndis_80211_auth_request		auth_request[0];
213 		struct ndis_80211_pmkid_cand_list	cand_list;
214 	} u;
215 } __packed;
216 
217 struct ndis_80211_ssid {
218 	__le32 length;
219 	u8 essid[NDIS_802_11_LENGTH_SSID];
220 } __packed;
221 
222 struct ndis_80211_conf_freq_hop {
223 	__le32 length;
224 	__le32 hop_pattern;
225 	__le32 hop_set;
226 	__le32 dwell_time;
227 } __packed;
228 
229 struct ndis_80211_conf {
230 	__le32 length;
231 	__le32 beacon_period;
232 	__le32 atim_window;
233 	__le32 ds_config;
234 	struct ndis_80211_conf_freq_hop fh_config;
235 } __packed;
236 
237 struct ndis_80211_bssid_ex {
238 	__le32 length;
239 	u8 mac[ETH_ALEN];
240 	u8 padding[2];
241 	struct ndis_80211_ssid ssid;
242 	__le32 privacy;
243 	__le32 rssi;
244 	__le32 net_type;
245 	struct ndis_80211_conf config;
246 	__le32 net_infra;
247 	u8 rates[NDIS_802_11_LENGTH_RATES_EX];
248 	__le32 ie_length;
249 	u8 ies[];
250 } __packed;
251 
252 struct ndis_80211_bssid_list_ex {
253 	__le32 num_items;
254 	u8 bssid_data[];
255 } __packed;
256 
257 struct ndis_80211_fixed_ies {
258 	u8 timestamp[8];
259 	__le16 beacon_interval;
260 	__le16 capabilities;
261 } __packed;
262 
263 struct ndis_80211_wep_key {
264 	__le32 size;
265 	__le32 index;
266 	__le32 length;
267 	u8 material[32];
268 } __packed;
269 
270 struct ndis_80211_key {
271 	__le32 size;
272 	__le32 index;
273 	__le32 length;
274 	u8 bssid[ETH_ALEN];
275 	u8 padding[6];
276 	u8 rsc[8];
277 	u8 material[32];
278 } __packed;
279 
280 struct ndis_80211_remove_key {
281 	__le32 size;
282 	__le32 index;
283 	u8 bssid[ETH_ALEN];
284 	u8 padding[2];
285 } __packed;
286 
287 struct ndis_config_param {
288 	__le32 name_offs;
289 	__le32 name_length;
290 	__le32 type;
291 	__le32 value_offs;
292 	__le32 value_length;
293 } __packed;
294 
295 struct ndis_80211_assoc_info {
296 	__le32 length;
297 	__le16 req_ies;
298 	struct req_ie {
299 		__le16 capa;
300 		__le16 listen_interval;
301 		u8 cur_ap_address[ETH_ALEN];
302 	} req_ie;
303 	__le32 req_ie_length;
304 	__le32 offset_req_ies;
305 	__le16 resp_ies;
306 	struct resp_ie {
307 		__le16 capa;
308 		__le16 status_code;
309 		__le16 assoc_id;
310 	} resp_ie;
311 	__le32 resp_ie_length;
312 	__le32 offset_resp_ies;
313 } __packed;
314 
315 struct ndis_80211_capability {
316 	__le32 length;
317 	__le32 version;
318 	__le32 num_pmkids;
319 	__le32 num_auth_encr_pair;
320 } __packed;
321 
322 struct ndis_80211_bssid_info {
323 	u8 bssid[ETH_ALEN];
324 	u8 pmkid[16];
325 } __packed;
326 
327 struct ndis_80211_pmkid {
328 	__le32 length;
329 	__le32 bssid_info_count;
330 	struct ndis_80211_bssid_info bssid_info[];
331 } __packed;
332 
333 /*
334  *  private data
335  */
336 #define CAP_MODE_80211A		1
337 #define CAP_MODE_80211B		2
338 #define CAP_MODE_80211G		4
339 #define CAP_MODE_MASK		7
340 
341 #define WORK_LINK_UP		0
342 #define WORK_LINK_DOWN		1
343 #define WORK_SET_MULTICAST_LIST	2
344 
345 #define RNDIS_WLAN_ALG_NONE	0
346 #define RNDIS_WLAN_ALG_WEP	(1<<0)
347 #define RNDIS_WLAN_ALG_TKIP	(1<<1)
348 #define RNDIS_WLAN_ALG_CCMP	(1<<2)
349 
350 #define RNDIS_WLAN_NUM_KEYS		4
351 #define RNDIS_WLAN_KEY_MGMT_NONE	0
352 #define RNDIS_WLAN_KEY_MGMT_802_1X	(1<<0)
353 #define RNDIS_WLAN_KEY_MGMT_PSK		(1<<1)
354 
355 #define COMMAND_BUFFER_SIZE	(CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
356 
357 static const struct ieee80211_channel rndis_channels[] = {
358 	{ .center_freq = 2412 },
359 	{ .center_freq = 2417 },
360 	{ .center_freq = 2422 },
361 	{ .center_freq = 2427 },
362 	{ .center_freq = 2432 },
363 	{ .center_freq = 2437 },
364 	{ .center_freq = 2442 },
365 	{ .center_freq = 2447 },
366 	{ .center_freq = 2452 },
367 	{ .center_freq = 2457 },
368 	{ .center_freq = 2462 },
369 	{ .center_freq = 2467 },
370 	{ .center_freq = 2472 },
371 	{ .center_freq = 2484 },
372 };
373 
374 static const struct ieee80211_rate rndis_rates[] = {
375 	{ .bitrate = 10 },
376 	{ .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
377 	{ .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
378 	{ .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
379 	{ .bitrate = 60 },
380 	{ .bitrate = 90 },
381 	{ .bitrate = 120 },
382 	{ .bitrate = 180 },
383 	{ .bitrate = 240 },
384 	{ .bitrate = 360 },
385 	{ .bitrate = 480 },
386 	{ .bitrate = 540 }
387 };
388 
389 static const u32 rndis_cipher_suites[] = {
390 	WLAN_CIPHER_SUITE_WEP40,
391 	WLAN_CIPHER_SUITE_WEP104,
392 	WLAN_CIPHER_SUITE_TKIP,
393 	WLAN_CIPHER_SUITE_CCMP,
394 };
395 
396 struct rndis_wlan_encr_key {
397 	int len;
398 	u32 cipher;
399 	u8 material[32];
400 	u8 bssid[ETH_ALEN];
401 	bool pairwise;
402 	bool tx_key;
403 };
404 
405 /* RNDIS device private data */
406 struct rndis_wlan_private {
407 	struct usbnet *usbdev;
408 
409 	struct wireless_dev wdev;
410 
411 	struct cfg80211_scan_request *scan_request;
412 
413 	struct workqueue_struct *workqueue;
414 	struct delayed_work dev_poller_work;
415 	struct delayed_work scan_work;
416 	struct work_struct work;
417 	struct mutex command_lock;
418 	unsigned long work_pending;
419 	int last_qual;
420 	s32 cqm_rssi_thold;
421 	u32 cqm_rssi_hyst;
422 	int last_cqm_event_rssi;
423 
424 	struct ieee80211_supported_band band;
425 	struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
426 	struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
427 	u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
428 
429 	int device_type;
430 	int caps;
431 	int multicast_size;
432 
433 	/* module parameters */
434 	char param_country[4];
435 	int  param_frameburst;
436 	int  param_afterburner;
437 	int  param_power_save;
438 	int  param_power_output;
439 	int  param_roamtrigger;
440 	int  param_roamdelta;
441 	u32  param_workaround_interval;
442 
443 	/* hardware state */
444 	bool radio_on;
445 	int power_mode;
446 	int infra_mode;
447 	bool connected;
448 	u8 bssid[ETH_ALEN];
449 	u32 current_command_oid;
450 
451 	/* encryption stuff */
452 	u8 encr_tx_key_index;
453 	struct rndis_wlan_encr_key encr_keys[RNDIS_WLAN_NUM_KEYS];
454 	int  wpa_version;
455 
456 	u8 command_buffer[COMMAND_BUFFER_SIZE];
457 };
458 
459 /*
460  * cfg80211 ops
461  */
462 static int rndis_change_virtual_intf(struct wiphy *wiphy,
463 					struct net_device *dev,
464 					enum nl80211_iftype type,
465 					struct vif_params *params);
466 
467 static int rndis_scan(struct wiphy *wiphy,
468 			struct cfg80211_scan_request *request);
469 
470 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
471 
472 static int rndis_set_tx_power(struct wiphy *wiphy,
473 			      struct wireless_dev *wdev,
474 			      enum nl80211_tx_power_setting type,
475 			      int mbm);
476 static int rndis_get_tx_power(struct wiphy *wiphy,
477 			      struct wireless_dev *wdev,
478 			      int *dbm);
479 
480 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
481 				struct cfg80211_connect_params *sme);
482 
483 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
484 				u16 reason_code);
485 
486 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
487 					struct cfg80211_ibss_params *params);
488 
489 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
490 
491 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
492 			 int link_id,  u8 key_index, bool pairwise,
493 			 const u8 *mac_addr, struct key_params *params);
494 
495 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
496 			 int link_id, u8 key_index, bool pairwise,
497 			 const u8 *mac_addr);
498 
499 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
500 				 int link_id, u8 key_index, bool unicast,
501 				 bool multicast);
502 
503 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
504 			     const u8 *mac, struct station_info *sinfo);
505 
506 static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
507 			       int idx, u8 *mac, struct station_info *sinfo);
508 
509 static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
510 				struct cfg80211_pmksa *pmksa);
511 
512 static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
513 				struct cfg80211_pmksa *pmksa);
514 
515 static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev);
516 
517 static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
518 				bool enabled, int timeout);
519 
520 static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
521 					struct net_device *dev,
522 					s32 rssi_thold, u32 rssi_hyst);
523 
524 static const struct cfg80211_ops rndis_config_ops = {
525 	.change_virtual_intf = rndis_change_virtual_intf,
526 	.scan = rndis_scan,
527 	.set_wiphy_params = rndis_set_wiphy_params,
528 	.set_tx_power = rndis_set_tx_power,
529 	.get_tx_power = rndis_get_tx_power,
530 	.connect = rndis_connect,
531 	.disconnect = rndis_disconnect,
532 	.join_ibss = rndis_join_ibss,
533 	.leave_ibss = rndis_leave_ibss,
534 	.add_key = rndis_add_key,
535 	.del_key = rndis_del_key,
536 	.set_default_key = rndis_set_default_key,
537 	.get_station = rndis_get_station,
538 	.dump_station = rndis_dump_station,
539 	.set_pmksa = rndis_set_pmksa,
540 	.del_pmksa = rndis_del_pmksa,
541 	.flush_pmksa = rndis_flush_pmksa,
542 	.set_power_mgmt = rndis_set_power_mgmt,
543 	.set_cqm_rssi_config = rndis_set_cqm_rssi_config,
544 };
545 
546 static void *rndis_wiphy_privid = &rndis_wiphy_privid;
547 
548 
get_rndis_wlan_priv(struct usbnet * dev)549 static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
550 {
551 	return (struct rndis_wlan_private *)dev->driver_priv;
552 }
553 
get_bcm4320_power_dbm(struct rndis_wlan_private * priv)554 static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
555 {
556 	switch (priv->param_power_output) {
557 	default:
558 	case 3:
559 		return BCM4320_DEFAULT_TXPOWER_DBM_100;
560 	case 2:
561 		return BCM4320_DEFAULT_TXPOWER_DBM_75;
562 	case 1:
563 		return BCM4320_DEFAULT_TXPOWER_DBM_50;
564 	case 0:
565 		return BCM4320_DEFAULT_TXPOWER_DBM_25;
566 	}
567 }
568 
is_wpa_key(struct rndis_wlan_private * priv,u8 idx)569 static bool is_wpa_key(struct rndis_wlan_private *priv, u8 idx)
570 {
571 	int cipher = priv->encr_keys[idx].cipher;
572 
573 	return (cipher == WLAN_CIPHER_SUITE_CCMP ||
574 		cipher == WLAN_CIPHER_SUITE_TKIP);
575 }
576 
rndis_cipher_to_alg(u32 cipher)577 static int rndis_cipher_to_alg(u32 cipher)
578 {
579 	switch (cipher) {
580 	default:
581 		return RNDIS_WLAN_ALG_NONE;
582 	case WLAN_CIPHER_SUITE_WEP40:
583 	case WLAN_CIPHER_SUITE_WEP104:
584 		return RNDIS_WLAN_ALG_WEP;
585 	case WLAN_CIPHER_SUITE_TKIP:
586 		return RNDIS_WLAN_ALG_TKIP;
587 	case WLAN_CIPHER_SUITE_CCMP:
588 		return RNDIS_WLAN_ALG_CCMP;
589 	}
590 }
591 
rndis_akm_suite_to_key_mgmt(u32 akm_suite)592 static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
593 {
594 	switch (akm_suite) {
595 	default:
596 		return RNDIS_WLAN_KEY_MGMT_NONE;
597 	case WLAN_AKM_SUITE_8021X:
598 		return RNDIS_WLAN_KEY_MGMT_802_1X;
599 	case WLAN_AKM_SUITE_PSK:
600 		return RNDIS_WLAN_KEY_MGMT_PSK;
601 	}
602 }
603 
604 #ifdef DEBUG
oid_to_string(u32 oid)605 static const char *oid_to_string(u32 oid)
606 {
607 	switch (oid) {
608 #define OID_STR(oid) case oid: return(#oid)
609 		/* from rndis_host.h */
610 		OID_STR(RNDIS_OID_802_3_PERMANENT_ADDRESS);
611 		OID_STR(RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE);
612 		OID_STR(RNDIS_OID_GEN_CURRENT_PACKET_FILTER);
613 		OID_STR(RNDIS_OID_GEN_PHYSICAL_MEDIUM);
614 
615 		/* from rndis_wlan.c */
616 		OID_STR(RNDIS_OID_GEN_LINK_SPEED);
617 		OID_STR(RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER);
618 
619 		OID_STR(RNDIS_OID_GEN_XMIT_OK);
620 		OID_STR(RNDIS_OID_GEN_RCV_OK);
621 		OID_STR(RNDIS_OID_GEN_XMIT_ERROR);
622 		OID_STR(RNDIS_OID_GEN_RCV_ERROR);
623 		OID_STR(RNDIS_OID_GEN_RCV_NO_BUFFER);
624 
625 		OID_STR(RNDIS_OID_802_3_CURRENT_ADDRESS);
626 		OID_STR(RNDIS_OID_802_3_MULTICAST_LIST);
627 		OID_STR(RNDIS_OID_802_3_MAXIMUM_LIST_SIZE);
628 
629 		OID_STR(RNDIS_OID_802_11_BSSID);
630 		OID_STR(RNDIS_OID_802_11_SSID);
631 		OID_STR(RNDIS_OID_802_11_INFRASTRUCTURE_MODE);
632 		OID_STR(RNDIS_OID_802_11_ADD_WEP);
633 		OID_STR(RNDIS_OID_802_11_REMOVE_WEP);
634 		OID_STR(RNDIS_OID_802_11_DISASSOCIATE);
635 		OID_STR(RNDIS_OID_802_11_AUTHENTICATION_MODE);
636 		OID_STR(RNDIS_OID_802_11_PRIVACY_FILTER);
637 		OID_STR(RNDIS_OID_802_11_BSSID_LIST_SCAN);
638 		OID_STR(RNDIS_OID_802_11_ENCRYPTION_STATUS);
639 		OID_STR(RNDIS_OID_802_11_ADD_KEY);
640 		OID_STR(RNDIS_OID_802_11_REMOVE_KEY);
641 		OID_STR(RNDIS_OID_802_11_ASSOCIATION_INFORMATION);
642 		OID_STR(RNDIS_OID_802_11_CAPABILITY);
643 		OID_STR(RNDIS_OID_802_11_PMKID);
644 		OID_STR(RNDIS_OID_802_11_NETWORK_TYPES_SUPPORTED);
645 		OID_STR(RNDIS_OID_802_11_NETWORK_TYPE_IN_USE);
646 		OID_STR(RNDIS_OID_802_11_TX_POWER_LEVEL);
647 		OID_STR(RNDIS_OID_802_11_RSSI);
648 		OID_STR(RNDIS_OID_802_11_RSSI_TRIGGER);
649 		OID_STR(RNDIS_OID_802_11_FRAGMENTATION_THRESHOLD);
650 		OID_STR(RNDIS_OID_802_11_RTS_THRESHOLD);
651 		OID_STR(RNDIS_OID_802_11_SUPPORTED_RATES);
652 		OID_STR(RNDIS_OID_802_11_CONFIGURATION);
653 		OID_STR(RNDIS_OID_802_11_POWER_MODE);
654 		OID_STR(RNDIS_OID_802_11_BSSID_LIST);
655 #undef OID_STR
656 	}
657 
658 	return "?";
659 }
660 #else
oid_to_string(u32 oid)661 static const char *oid_to_string(u32 oid)
662 {
663 	return "?";
664 }
665 #endif
666 
667 /* translate error code */
rndis_error_status(__le32 rndis_status)668 static int rndis_error_status(__le32 rndis_status)
669 {
670 	int ret = -EINVAL;
671 	switch (le32_to_cpu(rndis_status)) {
672 	case RNDIS_STATUS_SUCCESS:
673 		ret = 0;
674 		break;
675 	case RNDIS_STATUS_FAILURE:
676 	case RNDIS_STATUS_INVALID_DATA:
677 		ret = -EINVAL;
678 		break;
679 	case RNDIS_STATUS_NOT_SUPPORTED:
680 		ret = -EOPNOTSUPP;
681 		break;
682 	case RNDIS_STATUS_ADAPTER_NOT_READY:
683 	case RNDIS_STATUS_ADAPTER_NOT_OPEN:
684 		ret = -EBUSY;
685 		break;
686 	}
687 	return ret;
688 }
689 
rndis_query_oid(struct usbnet * dev,u32 oid,void * data,int * len)690 static int rndis_query_oid(struct usbnet *dev, u32 oid, void *data, int *len)
691 {
692 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
693 	union {
694 		void			*buf;
695 		struct rndis_msg_hdr	*header;
696 		struct rndis_query	*get;
697 		struct rndis_query_c	*get_c;
698 	} u;
699 	int ret, buflen;
700 	int resplen, respoffs, copylen;
701 
702 	buflen = *len + sizeof(*u.get);
703 	if (buflen < CONTROL_BUFFER_SIZE)
704 		buflen = CONTROL_BUFFER_SIZE;
705 
706 	if (buflen > COMMAND_BUFFER_SIZE) {
707 		u.buf = kmalloc(buflen, GFP_KERNEL);
708 		if (!u.buf)
709 			return -ENOMEM;
710 	} else {
711 		u.buf = priv->command_buffer;
712 	}
713 
714 	mutex_lock(&priv->command_lock);
715 
716 	memset(u.get, 0, sizeof *u.get);
717 	u.get->msg_type = cpu_to_le32(RNDIS_MSG_QUERY);
718 	u.get->msg_len = cpu_to_le32(sizeof *u.get);
719 	u.get->oid = cpu_to_le32(oid);
720 
721 	priv->current_command_oid = oid;
722 	ret = rndis_command(dev, u.header, buflen);
723 	priv->current_command_oid = 0;
724 	if (ret < 0)
725 		netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
726 			   __func__, oid_to_string(oid), ret,
727 			   le32_to_cpu(u.get_c->status));
728 
729 	if (ret == 0) {
730 		resplen = le32_to_cpu(u.get_c->len);
731 		respoffs = le32_to_cpu(u.get_c->offset) + 8;
732 
733 		if (respoffs > buflen) {
734 			/* Device returned data offset outside buffer, error. */
735 			netdev_dbg(dev->net, "%s(%s): received invalid "
736 				"data offset: %d > %d\n", __func__,
737 				oid_to_string(oid), respoffs, buflen);
738 
739 			ret = -EINVAL;
740 			goto exit_unlock;
741 		}
742 
743 		if ((resplen + respoffs) > buflen) {
744 			/* Device would have returned more data if buffer would
745 			 * have been big enough. Copy just the bits that we got.
746 			 */
747 			copylen = buflen - respoffs;
748 		} else {
749 			copylen = resplen;
750 		}
751 
752 		if (copylen > *len)
753 			copylen = *len;
754 
755 		memcpy(data, u.buf + respoffs, copylen);
756 
757 		*len = resplen;
758 
759 		ret = rndis_error_status(u.get_c->status);
760 		if (ret < 0)
761 			netdev_dbg(dev->net, "%s(%s): device returned error,  0x%08x (%d)\n",
762 				   __func__, oid_to_string(oid),
763 				   le32_to_cpu(u.get_c->status), ret);
764 	}
765 
766 exit_unlock:
767 	mutex_unlock(&priv->command_lock);
768 
769 	if (u.buf != priv->command_buffer)
770 		kfree(u.buf);
771 	return ret;
772 }
773 
rndis_set_oid(struct usbnet * dev,u32 oid,const void * data,int len)774 static int rndis_set_oid(struct usbnet *dev, u32 oid, const void *data,
775 			 int len)
776 {
777 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
778 	union {
779 		void			*buf;
780 		struct rndis_msg_hdr	*header;
781 		struct rndis_set	*set;
782 		struct rndis_set_c	*set_c;
783 	} u;
784 	int ret, buflen;
785 
786 	buflen = len + sizeof(*u.set);
787 	if (buflen < CONTROL_BUFFER_SIZE)
788 		buflen = CONTROL_BUFFER_SIZE;
789 
790 	if (buflen > COMMAND_BUFFER_SIZE) {
791 		u.buf = kmalloc(buflen, GFP_KERNEL);
792 		if (!u.buf)
793 			return -ENOMEM;
794 	} else {
795 		u.buf = priv->command_buffer;
796 	}
797 
798 	mutex_lock(&priv->command_lock);
799 
800 	memset(u.set, 0, sizeof *u.set);
801 	u.set->msg_type = cpu_to_le32(RNDIS_MSG_SET);
802 	u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
803 	u.set->oid = cpu_to_le32(oid);
804 	u.set->len = cpu_to_le32(len);
805 	u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
806 	u.set->handle = cpu_to_le32(0);
807 	memcpy(u.buf + sizeof(*u.set), data, len);
808 
809 	priv->current_command_oid = oid;
810 	ret = rndis_command(dev, u.header, buflen);
811 	priv->current_command_oid = 0;
812 	if (ret < 0)
813 		netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
814 			   __func__, oid_to_string(oid), ret,
815 			   le32_to_cpu(u.set_c->status));
816 
817 	if (ret == 0) {
818 		ret = rndis_error_status(u.set_c->status);
819 
820 		if (ret < 0)
821 			netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
822 				   __func__, oid_to_string(oid),
823 				   le32_to_cpu(u.set_c->status), ret);
824 	}
825 
826 	mutex_unlock(&priv->command_lock);
827 
828 	if (u.buf != priv->command_buffer)
829 		kfree(u.buf);
830 	return ret;
831 }
832 
rndis_reset(struct usbnet * usbdev)833 static int rndis_reset(struct usbnet *usbdev)
834 {
835 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
836 	struct rndis_reset *reset;
837 	int ret;
838 
839 	mutex_lock(&priv->command_lock);
840 
841 	reset = (void *)priv->command_buffer;
842 	memset(reset, 0, sizeof(*reset));
843 	reset->msg_type = cpu_to_le32(RNDIS_MSG_RESET);
844 	reset->msg_len = cpu_to_le32(sizeof(*reset));
845 	priv->current_command_oid = 0;
846 	ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
847 
848 	mutex_unlock(&priv->command_lock);
849 
850 	if (ret < 0)
851 		return ret;
852 	return 0;
853 }
854 
855 /*
856  * Specs say that we can only set config parameters only soon after device
857  * initialization.
858  *   value_type: 0 = u32, 2 = unicode string
859  */
rndis_set_config_parameter(struct usbnet * dev,char * param,int value_type,void * value)860 static int rndis_set_config_parameter(struct usbnet *dev, char *param,
861 						int value_type, void *value)
862 {
863 	struct ndis_config_param *infobuf;
864 	int value_len, info_len, param_len, ret, i;
865 	__le16 *unibuf;
866 	__le32 *dst_value;
867 
868 	if (value_type == 0)
869 		value_len = sizeof(__le32);
870 	else if (value_type == 2)
871 		value_len = strlen(value) * sizeof(__le16);
872 	else
873 		return -EINVAL;
874 
875 	param_len = strlen(param) * sizeof(__le16);
876 	info_len = sizeof(*infobuf) + param_len + value_len;
877 
878 #ifdef DEBUG
879 	info_len += 12;
880 #endif
881 	infobuf = kmalloc(info_len, GFP_KERNEL);
882 	if (!infobuf)
883 		return -ENOMEM;
884 
885 #ifdef DEBUG
886 	info_len -= 12;
887 	/* extra 12 bytes are for padding (debug output) */
888 	memset(infobuf, 0xCC, info_len + 12);
889 #endif
890 
891 	if (value_type == 2)
892 		netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
893 			   param, (u8 *)value);
894 	else
895 		netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
896 			   param, *(u32 *)value);
897 
898 	infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
899 	infobuf->name_length = cpu_to_le32(param_len);
900 	infobuf->type = cpu_to_le32(value_type);
901 	infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
902 	infobuf->value_length = cpu_to_le32(value_len);
903 
904 	/* simple string to unicode string conversion */
905 	unibuf = (void *)infobuf + sizeof(*infobuf);
906 	for (i = 0; i < param_len / sizeof(__le16); i++)
907 		unibuf[i] = cpu_to_le16(param[i]);
908 
909 	if (value_type == 2) {
910 		unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
911 		for (i = 0; i < value_len / sizeof(__le16); i++)
912 			unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
913 	} else {
914 		dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
915 		*dst_value = cpu_to_le32(*(u32 *)value);
916 	}
917 
918 #ifdef DEBUG
919 	netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
920 	for (i = 0; i < info_len; i += 12) {
921 		u32 *tmp = (u32 *)((u8 *)infobuf + i);
922 		netdev_dbg(dev->net, "%08X:%08X:%08X\n",
923 			   cpu_to_be32(tmp[0]),
924 			   cpu_to_be32(tmp[1]),
925 			   cpu_to_be32(tmp[2]));
926 	}
927 #endif
928 
929 	ret = rndis_set_oid(dev, RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER,
930 							infobuf, info_len);
931 	if (ret != 0)
932 		netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
933 			   ret);
934 
935 	kfree(infobuf);
936 	return ret;
937 }
938 
rndis_set_config_parameter_str(struct usbnet * dev,char * param,char * value)939 static int rndis_set_config_parameter_str(struct usbnet *dev,
940 						char *param, char *value)
941 {
942 	return rndis_set_config_parameter(dev, param, 2, value);
943 }
944 
945 /*
946  * data conversion functions
947  */
level_to_qual(int level)948 static int level_to_qual(int level)
949 {
950 	int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
951 	return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
952 }
953 
954 /*
955  * common functions
956  */
957 static int set_infra_mode(struct usbnet *usbdev, int mode);
958 static void restore_keys(struct usbnet *usbdev);
959 static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
960 					bool *matched);
961 
rndis_start_bssid_list_scan(struct usbnet * usbdev)962 static int rndis_start_bssid_list_scan(struct usbnet *usbdev)
963 {
964 	__le32 tmp;
965 
966 	/* Note: RNDIS_OID_802_11_BSSID_LIST_SCAN clears internal BSS list. */
967 	tmp = cpu_to_le32(1);
968 	return rndis_set_oid(usbdev, RNDIS_OID_802_11_BSSID_LIST_SCAN, &tmp,
969 							sizeof(tmp));
970 }
971 
set_essid(struct usbnet * usbdev,struct ndis_80211_ssid * ssid)972 static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
973 {
974 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
975 	int ret;
976 
977 	ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_SSID,
978 			    ssid, sizeof(*ssid));
979 	if (ret < 0) {
980 		netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
981 		return ret;
982 	}
983 	if (ret == 0) {
984 		priv->radio_on = true;
985 		netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
986 	}
987 
988 	return ret;
989 }
990 
set_bssid(struct usbnet * usbdev,const u8 * bssid)991 static int set_bssid(struct usbnet *usbdev, const u8 *bssid)
992 {
993 	int ret;
994 
995 	ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_BSSID,
996 			    bssid, ETH_ALEN);
997 	if (ret < 0) {
998 		netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
999 			    bssid, ret);
1000 		return ret;
1001 	}
1002 
1003 	return ret;
1004 }
1005 
clear_bssid(struct usbnet * usbdev)1006 static int clear_bssid(struct usbnet *usbdev)
1007 {
1008 	static const u8 broadcast_mac[ETH_ALEN] = {
1009 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
1010 	};
1011 
1012 	return set_bssid(usbdev, broadcast_mac);
1013 }
1014 
get_bssid(struct usbnet * usbdev,u8 bssid[ETH_ALEN])1015 static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1016 {
1017 	int ret, len;
1018 
1019 	len = ETH_ALEN;
1020 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_BSSID,
1021 			      bssid, &len);
1022 
1023 	if (ret != 0)
1024 		eth_zero_addr(bssid);
1025 
1026 	return ret;
1027 }
1028 
get_association_info(struct usbnet * usbdev,struct ndis_80211_assoc_info * info,int len)1029 static int get_association_info(struct usbnet *usbdev,
1030 			struct ndis_80211_assoc_info *info, int len)
1031 {
1032 	return rndis_query_oid(usbdev,
1033 			RNDIS_OID_802_11_ASSOCIATION_INFORMATION,
1034 			info, &len);
1035 }
1036 
is_associated(struct usbnet * usbdev)1037 static bool is_associated(struct usbnet *usbdev)
1038 {
1039 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1040 	u8 bssid[ETH_ALEN];
1041 
1042 	if (!priv->radio_on)
1043 		return false;
1044 
1045 	return (get_bssid(usbdev, bssid) == 0 && !is_zero_ether_addr(bssid));
1046 }
1047 
disassociate(struct usbnet * usbdev,bool reset_ssid)1048 static int disassociate(struct usbnet *usbdev, bool reset_ssid)
1049 {
1050 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1051 	struct ndis_80211_ssid ssid;
1052 	int i, ret = 0;
1053 
1054 	if (priv->radio_on) {
1055 		ret = rndis_set_oid(usbdev,
1056 				RNDIS_OID_802_11_DISASSOCIATE,
1057 				NULL, 0);
1058 		if (ret == 0) {
1059 			priv->radio_on = false;
1060 			netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
1061 				   __func__);
1062 
1063 			if (reset_ssid)
1064 				msleep(100);
1065 		}
1066 	}
1067 
1068 	/* disassociate causes radio to be turned off; if reset_ssid
1069 	 * is given, set random ssid to enable radio */
1070 	if (reset_ssid) {
1071 		/* Set device to infrastructure mode so we don't get ad-hoc
1072 		 * 'media connect' indications with the random ssid.
1073 		 */
1074 		set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1075 
1076 		ssid.length = cpu_to_le32(sizeof(ssid.essid));
1077 		get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
1078 		ssid.essid[0] = 0x1;
1079 		ssid.essid[1] = 0xff;
1080 		for (i = 2; i < sizeof(ssid.essid); i++)
1081 			ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
1082 		ret = set_essid(usbdev, &ssid);
1083 	}
1084 	return ret;
1085 }
1086 
set_auth_mode(struct usbnet * usbdev,u32 wpa_version,enum nl80211_auth_type auth_type,int keymgmt)1087 static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
1088 				enum nl80211_auth_type auth_type, int keymgmt)
1089 {
1090 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1091 	__le32 tmp;
1092 	int auth_mode, ret;
1093 
1094 	netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
1095 		   __func__, wpa_version, auth_type, keymgmt);
1096 
1097 	if (wpa_version & NL80211_WPA_VERSION_2) {
1098 		if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1099 			auth_mode = NDIS_80211_AUTH_WPA2;
1100 		else
1101 			auth_mode = NDIS_80211_AUTH_WPA2_PSK;
1102 	} else if (wpa_version & NL80211_WPA_VERSION_1) {
1103 		if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1104 			auth_mode = NDIS_80211_AUTH_WPA;
1105 		else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
1106 			auth_mode = NDIS_80211_AUTH_WPA_PSK;
1107 		else
1108 			auth_mode = NDIS_80211_AUTH_WPA_NONE;
1109 	} else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
1110 		auth_mode = NDIS_80211_AUTH_SHARED;
1111 	else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
1112 		auth_mode = NDIS_80211_AUTH_OPEN;
1113 	else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
1114 		auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
1115 	else
1116 		return -ENOTSUPP;
1117 
1118 	tmp = cpu_to_le32(auth_mode);
1119 	ret = rndis_set_oid(usbdev,
1120 			    RNDIS_OID_802_11_AUTHENTICATION_MODE,
1121 			    &tmp, sizeof(tmp));
1122 	if (ret != 0) {
1123 		netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
1124 			    ret);
1125 		return ret;
1126 	}
1127 
1128 	priv->wpa_version = wpa_version;
1129 
1130 	return 0;
1131 }
1132 
set_priv_filter(struct usbnet * usbdev)1133 static int set_priv_filter(struct usbnet *usbdev)
1134 {
1135 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1136 	__le32 tmp;
1137 
1138 	netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
1139 		   __func__, priv->wpa_version);
1140 
1141 	if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
1142 	    priv->wpa_version & NL80211_WPA_VERSION_1)
1143 		tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
1144 	else
1145 		tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
1146 
1147 	return rndis_set_oid(usbdev,
1148 			     RNDIS_OID_802_11_PRIVACY_FILTER, &tmp,
1149 			     sizeof(tmp));
1150 }
1151 
set_encr_mode(struct usbnet * usbdev,int pairwise,int groupwise)1152 static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
1153 {
1154 	__le32 tmp;
1155 	int encr_mode, ret;
1156 
1157 	netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
1158 		   __func__, pairwise, groupwise);
1159 
1160 	if (pairwise & RNDIS_WLAN_ALG_CCMP)
1161 		encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1162 	else if (pairwise & RNDIS_WLAN_ALG_TKIP)
1163 		encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1164 	else if (pairwise & RNDIS_WLAN_ALG_WEP)
1165 		encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
1166 	else if (groupwise & RNDIS_WLAN_ALG_CCMP)
1167 		encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1168 	else if (groupwise & RNDIS_WLAN_ALG_TKIP)
1169 		encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1170 	else
1171 		encr_mode = NDIS_80211_ENCR_DISABLED;
1172 
1173 	tmp = cpu_to_le32(encr_mode);
1174 	ret = rndis_set_oid(usbdev,
1175 			RNDIS_OID_802_11_ENCRYPTION_STATUS, &tmp,
1176 			sizeof(tmp));
1177 	if (ret != 0) {
1178 		netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
1179 			    ret);
1180 		return ret;
1181 	}
1182 
1183 	return 0;
1184 }
1185 
set_infra_mode(struct usbnet * usbdev,int mode)1186 static int set_infra_mode(struct usbnet *usbdev, int mode)
1187 {
1188 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1189 	__le32 tmp;
1190 	int ret;
1191 
1192 	netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
1193 		   __func__, priv->infra_mode);
1194 
1195 	tmp = cpu_to_le32(mode);
1196 	ret = rndis_set_oid(usbdev,
1197 			    RNDIS_OID_802_11_INFRASTRUCTURE_MODE,
1198 			    &tmp, sizeof(tmp));
1199 	if (ret != 0) {
1200 		netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
1201 			    ret);
1202 		return ret;
1203 	}
1204 
1205 	/* NDIS drivers clear keys when infrastructure mode is
1206 	 * changed. But Linux tools assume otherwise. So set the
1207 	 * keys */
1208 	restore_keys(usbdev);
1209 
1210 	priv->infra_mode = mode;
1211 	return 0;
1212 }
1213 
set_rts_threshold(struct usbnet * usbdev,u32 rts_threshold)1214 static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
1215 {
1216 	__le32 tmp;
1217 
1218 	netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
1219 
1220 	if (rts_threshold == -1 || rts_threshold > 2347)
1221 		rts_threshold = 2347;
1222 
1223 	tmp = cpu_to_le32(rts_threshold);
1224 	return rndis_set_oid(usbdev,
1225 			     RNDIS_OID_802_11_RTS_THRESHOLD,
1226 			     &tmp, sizeof(tmp));
1227 }
1228 
set_frag_threshold(struct usbnet * usbdev,u32 frag_threshold)1229 static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
1230 {
1231 	__le32 tmp;
1232 
1233 	netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
1234 
1235 	if (frag_threshold < 256 || frag_threshold > 2346)
1236 		frag_threshold = 2346;
1237 
1238 	tmp = cpu_to_le32(frag_threshold);
1239 	return rndis_set_oid(usbdev,
1240 			RNDIS_OID_802_11_FRAGMENTATION_THRESHOLD,
1241 			&tmp, sizeof(tmp));
1242 }
1243 
set_default_iw_params(struct usbnet * usbdev)1244 static void set_default_iw_params(struct usbnet *usbdev)
1245 {
1246 	set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1247 	set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
1248 						RNDIS_WLAN_KEY_MGMT_NONE);
1249 	set_priv_filter(usbdev);
1250 	set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1251 }
1252 
deauthenticate(struct usbnet * usbdev)1253 static int deauthenticate(struct usbnet *usbdev)
1254 {
1255 	int ret;
1256 
1257 	ret = disassociate(usbdev, true);
1258 	set_default_iw_params(usbdev);
1259 	return ret;
1260 }
1261 
set_channel(struct usbnet * usbdev,int channel)1262 static int set_channel(struct usbnet *usbdev, int channel)
1263 {
1264 	struct ndis_80211_conf config;
1265 	unsigned int dsconfig;
1266 	int len, ret;
1267 
1268 	netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
1269 
1270 	/* this OID is valid only when not associated */
1271 	if (is_associated(usbdev))
1272 		return 0;
1273 
1274 	dsconfig = 1000 *
1275 		ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
1276 
1277 	len = sizeof(config);
1278 	ret = rndis_query_oid(usbdev,
1279 			RNDIS_OID_802_11_CONFIGURATION,
1280 			&config, &len);
1281 	if (ret < 0) {
1282 		netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
1283 			   __func__);
1284 		return ret;
1285 	}
1286 
1287 	config.ds_config = cpu_to_le32(dsconfig);
1288 	ret = rndis_set_oid(usbdev,
1289 			RNDIS_OID_802_11_CONFIGURATION,
1290 			&config, sizeof(config));
1291 
1292 	netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
1293 
1294 	return ret;
1295 }
1296 
get_current_channel(struct usbnet * usbdev,u32 * beacon_period)1297 static struct ieee80211_channel *get_current_channel(struct usbnet *usbdev,
1298 						     u32 *beacon_period)
1299 {
1300 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1301 	struct ieee80211_channel *channel;
1302 	struct ndis_80211_conf config;
1303 	int len, ret;
1304 
1305 	/* Get channel and beacon interval */
1306 	len = sizeof(config);
1307 	ret = rndis_query_oid(usbdev,
1308 			RNDIS_OID_802_11_CONFIGURATION,
1309 			&config, &len);
1310 	netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_CONFIGURATION -> %d\n",
1311 				__func__, ret);
1312 	if (ret < 0)
1313 		return NULL;
1314 
1315 	channel = ieee80211_get_channel(priv->wdev.wiphy,
1316 				KHZ_TO_MHZ(le32_to_cpu(config.ds_config)));
1317 	if (!channel)
1318 		return NULL;
1319 
1320 	if (beacon_period)
1321 		*beacon_period = le32_to_cpu(config.beacon_period);
1322 	return channel;
1323 }
1324 
1325 /* index must be 0 - N, as per NDIS  */
add_wep_key(struct usbnet * usbdev,const u8 * key,int key_len,u8 index)1326 static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
1327 								u8 index)
1328 {
1329 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1330 	struct ndis_80211_wep_key ndis_key;
1331 	u32 cipher;
1332 	int ret;
1333 
1334 	netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
1335 		   __func__, index, key_len);
1336 
1337 	if (index >= RNDIS_WLAN_NUM_KEYS)
1338 		return -EINVAL;
1339 
1340 	if (key_len == 5)
1341 		cipher = WLAN_CIPHER_SUITE_WEP40;
1342 	else if (key_len == 13)
1343 		cipher = WLAN_CIPHER_SUITE_WEP104;
1344 	else
1345 		return -EINVAL;
1346 
1347 	memset(&ndis_key, 0, sizeof(ndis_key));
1348 
1349 	ndis_key.size = cpu_to_le32(sizeof(ndis_key));
1350 	ndis_key.length = cpu_to_le32(key_len);
1351 	ndis_key.index = cpu_to_le32(index);
1352 	memcpy(&ndis_key.material, key, key_len);
1353 
1354 	if (index == priv->encr_tx_key_index) {
1355 		ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
1356 		ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
1357 							RNDIS_WLAN_ALG_NONE);
1358 		if (ret)
1359 			netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
1360 				    ret);
1361 	}
1362 
1363 	ret = rndis_set_oid(usbdev,
1364 			RNDIS_OID_802_11_ADD_WEP, &ndis_key,
1365 			sizeof(ndis_key));
1366 	if (ret != 0) {
1367 		netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
1368 			    index + 1, ret);
1369 		return ret;
1370 	}
1371 
1372 	priv->encr_keys[index].len = key_len;
1373 	priv->encr_keys[index].cipher = cipher;
1374 	memcpy(&priv->encr_keys[index].material, key, key_len);
1375 	eth_broadcast_addr(priv->encr_keys[index].bssid);
1376 
1377 	return 0;
1378 }
1379 
add_wpa_key(struct usbnet * usbdev,const u8 * key,int key_len,u8 index,const u8 * addr,const u8 * rx_seq,int seq_len,u32 cipher,__le32 flags)1380 static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
1381 			u8 index, const u8 *addr, const u8 *rx_seq,
1382 			int seq_len, u32 cipher, __le32 flags)
1383 {
1384 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1385 	struct ndis_80211_key ndis_key;
1386 	bool is_addr_ok;
1387 	int ret;
1388 
1389 	if (index >= RNDIS_WLAN_NUM_KEYS) {
1390 		netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
1391 			   __func__, index);
1392 		return -EINVAL;
1393 	}
1394 	if (key_len > sizeof(ndis_key.material) || key_len < 0) {
1395 		netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
1396 			   __func__, key_len);
1397 		return -EINVAL;
1398 	}
1399 	if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
1400 		if (!rx_seq || seq_len <= 0) {
1401 			netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
1402 				   __func__);
1403 			return -EINVAL;
1404 		}
1405 		if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
1406 			netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
1407 			return -EINVAL;
1408 		}
1409 	}
1410 
1411 	is_addr_ok = addr && !is_zero_ether_addr(addr) &&
1412 					!is_broadcast_ether_addr(addr);
1413 	if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
1414 		netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
1415 			   __func__, addr);
1416 		return -EINVAL;
1417 	}
1418 
1419 	netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
1420 		   __func__, index,
1421 		   !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
1422 		   !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
1423 		   !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
1424 
1425 	memset(&ndis_key, 0, sizeof(ndis_key));
1426 
1427 	ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1428 				sizeof(ndis_key.material) + key_len);
1429 	ndis_key.length = cpu_to_le32(key_len);
1430 	ndis_key.index = cpu_to_le32(index) | flags;
1431 
1432 	if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
1433 		/* wpa_supplicant gives us the Michael MIC RX/TX keys in
1434 		 * different order than NDIS spec, so swap the order here. */
1435 		memcpy(ndis_key.material, key, 16);
1436 		memcpy(ndis_key.material + 16, key + 24, 8);
1437 		memcpy(ndis_key.material + 24, key + 16, 8);
1438 	} else
1439 		memcpy(ndis_key.material, key, key_len);
1440 
1441 	if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
1442 		memcpy(ndis_key.rsc, rx_seq, seq_len);
1443 
1444 	if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
1445 		/* pairwise key */
1446 		memcpy(ndis_key.bssid, addr, ETH_ALEN);
1447 	} else {
1448 		/* group key */
1449 		if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
1450 			eth_broadcast_addr(ndis_key.bssid);
1451 		else
1452 			get_bssid(usbdev, ndis_key.bssid);
1453 	}
1454 
1455 	ret = rndis_set_oid(usbdev,
1456 			RNDIS_OID_802_11_ADD_KEY, &ndis_key,
1457 			le32_to_cpu(ndis_key.size));
1458 	netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_ADD_KEY -> %08X\n",
1459 		   __func__, ret);
1460 	if (ret != 0)
1461 		return ret;
1462 
1463 	memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
1464 	priv->encr_keys[index].len = key_len;
1465 	priv->encr_keys[index].cipher = cipher;
1466 	memcpy(&priv->encr_keys[index].material, key, key_len);
1467 	if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
1468 		memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
1469 	else
1470 		eth_broadcast_addr(priv->encr_keys[index].bssid);
1471 
1472 	if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
1473 		priv->encr_tx_key_index = index;
1474 
1475 	return 0;
1476 }
1477 
restore_key(struct usbnet * usbdev,u8 key_idx)1478 static int restore_key(struct usbnet *usbdev, u8 key_idx)
1479 {
1480 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1481 	struct rndis_wlan_encr_key key;
1482 
1483 	if (is_wpa_key(priv, key_idx))
1484 		return 0;
1485 
1486 	key = priv->encr_keys[key_idx];
1487 
1488 	netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
1489 
1490 	if (key.len == 0)
1491 		return 0;
1492 
1493 	return add_wep_key(usbdev, key.material, key.len, key_idx);
1494 }
1495 
restore_keys(struct usbnet * usbdev)1496 static void restore_keys(struct usbnet *usbdev)
1497 {
1498 	int i;
1499 
1500 	for (i = 0; i < 4; i++)
1501 		restore_key(usbdev, i);
1502 }
1503 
clear_key(struct rndis_wlan_private * priv,u8 idx)1504 static void clear_key(struct rndis_wlan_private *priv, u8 idx)
1505 {
1506 	memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
1507 }
1508 
1509 /* remove_key is for both wep and wpa */
remove_key(struct usbnet * usbdev,u8 index,const u8 * bssid)1510 static int remove_key(struct usbnet *usbdev, u8 index, const u8 *bssid)
1511 {
1512 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1513 	struct ndis_80211_remove_key remove_key;
1514 	__le32 keyindex;
1515 	bool is_wpa;
1516 	int ret;
1517 
1518 	if (index >= RNDIS_WLAN_NUM_KEYS)
1519 		return -ENOENT;
1520 
1521 	if (priv->encr_keys[index].len == 0)
1522 		return 0;
1523 
1524 	is_wpa = is_wpa_key(priv, index);
1525 
1526 	netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
1527 		   __func__, index, is_wpa ? "wpa" : "wep",
1528 		   priv->encr_keys[index].len);
1529 
1530 	clear_key(priv, index);
1531 
1532 	if (is_wpa) {
1533 		remove_key.size = cpu_to_le32(sizeof(remove_key));
1534 		remove_key.index = cpu_to_le32(index);
1535 		if (bssid) {
1536 			/* pairwise key */
1537 			if (!is_broadcast_ether_addr(bssid))
1538 				remove_key.index |=
1539 					NDIS_80211_ADDKEY_PAIRWISE_KEY;
1540 			memcpy(remove_key.bssid, bssid,
1541 					sizeof(remove_key.bssid));
1542 		} else
1543 			memset(remove_key.bssid, 0xff,
1544 						sizeof(remove_key.bssid));
1545 
1546 		ret = rndis_set_oid(usbdev,
1547 				RNDIS_OID_802_11_REMOVE_KEY,
1548 				&remove_key, sizeof(remove_key));
1549 		if (ret != 0)
1550 			return ret;
1551 	} else {
1552 		keyindex = cpu_to_le32(index);
1553 		ret = rndis_set_oid(usbdev,
1554 				RNDIS_OID_802_11_REMOVE_WEP,
1555 				&keyindex, sizeof(keyindex));
1556 		if (ret != 0) {
1557 			netdev_warn(usbdev->net,
1558 				    "removing encryption key %d failed (%08X)\n",
1559 				    index, ret);
1560 			return ret;
1561 		}
1562 	}
1563 
1564 	/* if it is transmit key, disable encryption */
1565 	if (index == priv->encr_tx_key_index)
1566 		set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1567 
1568 	return 0;
1569 }
1570 
set_multicast_list(struct usbnet * usbdev)1571 static void set_multicast_list(struct usbnet *usbdev)
1572 {
1573 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1574 	struct netdev_hw_addr *ha;
1575 	__le32 filter, basefilter;
1576 	int ret;
1577 	char *mc_addrs = NULL;
1578 	int mc_count;
1579 
1580 	basefilter = filter = cpu_to_le32(RNDIS_PACKET_TYPE_DIRECTED |
1581 					  RNDIS_PACKET_TYPE_BROADCAST);
1582 
1583 	if (usbdev->net->flags & IFF_PROMISC) {
1584 		filter |= cpu_to_le32(RNDIS_PACKET_TYPE_PROMISCUOUS |
1585 				      RNDIS_PACKET_TYPE_ALL_LOCAL);
1586 	} else if (usbdev->net->flags & IFF_ALLMULTI) {
1587 		filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
1588 	}
1589 
1590 	if (filter != basefilter)
1591 		goto set_filter;
1592 
1593 	/*
1594 	 * mc_list should be accessed holding the lock, so copy addresses to
1595 	 * local buffer first.
1596 	 */
1597 	netif_addr_lock_bh(usbdev->net);
1598 	mc_count = netdev_mc_count(usbdev->net);
1599 	if (mc_count > priv->multicast_size) {
1600 		filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
1601 	} else if (mc_count) {
1602 		int i = 0;
1603 
1604 		mc_addrs = kmalloc_array(mc_count, ETH_ALEN, GFP_ATOMIC);
1605 		if (!mc_addrs) {
1606 			netif_addr_unlock_bh(usbdev->net);
1607 			return;
1608 		}
1609 
1610 		netdev_for_each_mc_addr(ha, usbdev->net)
1611 			memcpy(mc_addrs + i++ * ETH_ALEN,
1612 			       ha->addr, ETH_ALEN);
1613 	}
1614 	netif_addr_unlock_bh(usbdev->net);
1615 
1616 	if (filter != basefilter)
1617 		goto set_filter;
1618 
1619 	if (mc_count) {
1620 		ret = rndis_set_oid(usbdev,
1621 				RNDIS_OID_802_3_MULTICAST_LIST,
1622 				mc_addrs, mc_count * ETH_ALEN);
1623 		kfree(mc_addrs);
1624 		if (ret == 0)
1625 			filter |= cpu_to_le32(RNDIS_PACKET_TYPE_MULTICAST);
1626 		else
1627 			filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
1628 
1629 		netdev_dbg(usbdev->net, "RNDIS_OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
1630 			   mc_count, priv->multicast_size, ret);
1631 	}
1632 
1633 set_filter:
1634 	ret = rndis_set_oid(usbdev, RNDIS_OID_GEN_CURRENT_PACKET_FILTER, &filter,
1635 							sizeof(filter));
1636 	if (ret < 0) {
1637 		netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
1638 			    le32_to_cpu(filter));
1639 	}
1640 
1641 	netdev_dbg(usbdev->net, "RNDIS_OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
1642 		   le32_to_cpu(filter), ret);
1643 }
1644 
1645 #ifdef DEBUG
debug_print_pmkids(struct usbnet * usbdev,struct ndis_80211_pmkid * pmkids,const char * func_str)1646 static void debug_print_pmkids(struct usbnet *usbdev,
1647 				struct ndis_80211_pmkid *pmkids,
1648 				const char *func_str)
1649 {
1650 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1651 	int i, len, count, max_pmkids, entry_len;
1652 
1653 	max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1654 	len = le32_to_cpu(pmkids->length);
1655 	count = le32_to_cpu(pmkids->bssid_info_count);
1656 
1657 	entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
1658 
1659 	netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
1660 				"%d)\n", func_str, count, len, entry_len);
1661 
1662 	if (count > max_pmkids)
1663 		count = max_pmkids;
1664 
1665 	for (i = 0; i < count; i++) {
1666 		u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
1667 
1668 		netdev_dbg(usbdev->net, "%s():  bssid: %pM, "
1669 				"pmkid: %08X:%08X:%08X:%08X\n",
1670 				func_str, pmkids->bssid_info[i].bssid,
1671 				cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
1672 				cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
1673 	}
1674 }
1675 #else
debug_print_pmkids(struct usbnet * usbdev,struct ndis_80211_pmkid * pmkids,const char * func_str)1676 static void debug_print_pmkids(struct usbnet *usbdev,
1677 				struct ndis_80211_pmkid *pmkids,
1678 				const char *func_str)
1679 {
1680 	return;
1681 }
1682 #endif
1683 
get_device_pmkids(struct usbnet * usbdev)1684 static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
1685 {
1686 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1687 	struct ndis_80211_pmkid *pmkids;
1688 	int len, ret, max_pmkids;
1689 
1690 	max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1691 	len = struct_size(pmkids, bssid_info, max_pmkids);
1692 
1693 	pmkids = kzalloc(len, GFP_KERNEL);
1694 	if (!pmkids)
1695 		return ERR_PTR(-ENOMEM);
1696 
1697 	pmkids->length = cpu_to_le32(len);
1698 	pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1699 
1700 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_PMKID,
1701 			pmkids, &len);
1702 	if (ret < 0) {
1703 		netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d)"
1704 				" -> %d\n", __func__, len, max_pmkids, ret);
1705 
1706 		kfree(pmkids);
1707 		return ERR_PTR(ret);
1708 	}
1709 
1710 	if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
1711 		pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1712 
1713 	debug_print_pmkids(usbdev, pmkids, __func__);
1714 
1715 	return pmkids;
1716 }
1717 
set_device_pmkids(struct usbnet * usbdev,struct ndis_80211_pmkid * pmkids)1718 static int set_device_pmkids(struct usbnet *usbdev,
1719 				struct ndis_80211_pmkid *pmkids)
1720 {
1721 	int ret, len, num_pmkids;
1722 
1723 	num_pmkids = le32_to_cpu(pmkids->bssid_info_count);
1724 	len = struct_size(pmkids, bssid_info, num_pmkids);
1725 	pmkids->length = cpu_to_le32(len);
1726 
1727 	debug_print_pmkids(usbdev, pmkids, __func__);
1728 
1729 	ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_PMKID, pmkids,
1730 			    le32_to_cpu(pmkids->length));
1731 	if (ret < 0) {
1732 		netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d) -> %d"
1733 				"\n", __func__, len, num_pmkids, ret);
1734 	}
1735 
1736 	kfree(pmkids);
1737 	return ret;
1738 }
1739 
remove_pmkid(struct usbnet * usbdev,struct ndis_80211_pmkid * pmkids,struct cfg80211_pmksa * pmksa,int max_pmkids)1740 static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
1741 						struct ndis_80211_pmkid *pmkids,
1742 						struct cfg80211_pmksa *pmksa,
1743 						int max_pmkids)
1744 {
1745 	int i, err;
1746 	unsigned int count;
1747 
1748 	count = le32_to_cpu(pmkids->bssid_info_count);
1749 
1750 	if (count > max_pmkids)
1751 		count = max_pmkids;
1752 
1753 	for (i = 0; i < count; i++)
1754 		if (ether_addr_equal(pmkids->bssid_info[i].bssid,
1755 				     pmksa->bssid))
1756 			break;
1757 
1758 	/* pmkid not found */
1759 	if (i == count) {
1760 		netdev_dbg(usbdev->net, "%s(): bssid not found (%pM)\n",
1761 					__func__, pmksa->bssid);
1762 		err = -ENOENT;
1763 		goto error;
1764 	}
1765 
1766 	for (; i + 1 < count; i++)
1767 		pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
1768 
1769 	count--;
1770 	pmkids->length = cpu_to_le32(struct_size(pmkids, bssid_info, count));
1771 	pmkids->bssid_info_count = cpu_to_le32(count);
1772 
1773 	return pmkids;
1774 error:
1775 	kfree(pmkids);
1776 	return ERR_PTR(err);
1777 }
1778 
update_pmkid(struct usbnet * usbdev,struct ndis_80211_pmkid * pmkids,struct cfg80211_pmksa * pmksa,int max_pmkids)1779 static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
1780 						struct ndis_80211_pmkid *pmkids,
1781 						struct cfg80211_pmksa *pmksa,
1782 						int max_pmkids)
1783 {
1784 	struct ndis_80211_pmkid *new_pmkids;
1785 	int i, err, newlen;
1786 	unsigned int count;
1787 
1788 	count = le32_to_cpu(pmkids->bssid_info_count);
1789 
1790 	if (count > max_pmkids)
1791 		count = max_pmkids;
1792 
1793 	/* update with new pmkid */
1794 	for (i = 0; i < count; i++) {
1795 		if (!ether_addr_equal(pmkids->bssid_info[i].bssid,
1796 				      pmksa->bssid))
1797 			continue;
1798 
1799 		memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
1800 								WLAN_PMKID_LEN);
1801 
1802 		return pmkids;
1803 	}
1804 
1805 	/* out of space, return error */
1806 	if (i == max_pmkids) {
1807 		netdev_dbg(usbdev->net, "%s(): out of space\n", __func__);
1808 		err = -ENOSPC;
1809 		goto error;
1810 	}
1811 
1812 	/* add new pmkid */
1813 	newlen = struct_size(pmkids, bssid_info, count + 1);
1814 
1815 	new_pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
1816 	if (!new_pmkids) {
1817 		err = -ENOMEM;
1818 		goto error;
1819 	}
1820 	pmkids = new_pmkids;
1821 
1822 	pmkids->length = cpu_to_le32(newlen);
1823 	pmkids->bssid_info_count = cpu_to_le32(count + 1);
1824 
1825 	memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
1826 	memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
1827 
1828 	return pmkids;
1829 error:
1830 	kfree(pmkids);
1831 	return ERR_PTR(err);
1832 }
1833 
1834 /*
1835  * cfg80211 ops
1836  */
rndis_change_virtual_intf(struct wiphy * wiphy,struct net_device * dev,enum nl80211_iftype type,struct vif_params * params)1837 static int rndis_change_virtual_intf(struct wiphy *wiphy,
1838 					struct net_device *dev,
1839 					enum nl80211_iftype type,
1840 					struct vif_params *params)
1841 {
1842 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1843 	struct usbnet *usbdev = priv->usbdev;
1844 	int mode;
1845 
1846 	switch (type) {
1847 	case NL80211_IFTYPE_ADHOC:
1848 		mode = NDIS_80211_INFRA_ADHOC;
1849 		break;
1850 	case NL80211_IFTYPE_STATION:
1851 		mode = NDIS_80211_INFRA_INFRA;
1852 		break;
1853 	default:
1854 		return -EINVAL;
1855 	}
1856 
1857 	priv->wdev.iftype = type;
1858 
1859 	return set_infra_mode(usbdev, mode);
1860 }
1861 
rndis_set_wiphy_params(struct wiphy * wiphy,u32 changed)1862 static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1863 {
1864 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1865 	struct usbnet *usbdev = priv->usbdev;
1866 	int err;
1867 
1868 	if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1869 		err = set_frag_threshold(usbdev, wiphy->frag_threshold);
1870 		if (err < 0)
1871 			return err;
1872 	}
1873 
1874 	if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1875 		err = set_rts_threshold(usbdev, wiphy->rts_threshold);
1876 		if (err < 0)
1877 			return err;
1878 	}
1879 
1880 	return 0;
1881 }
1882 
rndis_set_tx_power(struct wiphy * wiphy,struct wireless_dev * wdev,enum nl80211_tx_power_setting type,int mbm)1883 static int rndis_set_tx_power(struct wiphy *wiphy,
1884 			      struct wireless_dev *wdev,
1885 			      enum nl80211_tx_power_setting type,
1886 			      int mbm)
1887 {
1888 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1889 	struct usbnet *usbdev = priv->usbdev;
1890 
1891 	netdev_dbg(usbdev->net, "%s(): type:0x%x mbm:%i\n",
1892 		   __func__, type, mbm);
1893 
1894 	if (mbm < 0 || (mbm % 100))
1895 		return -ENOTSUPP;
1896 
1897 	/* Device doesn't support changing txpower after initialization, only
1898 	 * turn off/on radio. Support 'auto' mode and setting same dBm that is
1899 	 * currently used.
1900 	 */
1901 	if (type == NL80211_TX_POWER_AUTOMATIC ||
1902 	    MBM_TO_DBM(mbm) == get_bcm4320_power_dbm(priv)) {
1903 		if (!priv->radio_on)
1904 			disassociate(usbdev, true); /* turn on radio */
1905 
1906 		return 0;
1907 	}
1908 
1909 	return -ENOTSUPP;
1910 }
1911 
rndis_get_tx_power(struct wiphy * wiphy,struct wireless_dev * wdev,int * dbm)1912 static int rndis_get_tx_power(struct wiphy *wiphy,
1913 			      struct wireless_dev *wdev,
1914 			      int *dbm)
1915 {
1916 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1917 	struct usbnet *usbdev = priv->usbdev;
1918 
1919 	*dbm = get_bcm4320_power_dbm(priv);
1920 
1921 	netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
1922 
1923 	return 0;
1924 }
1925 
1926 #define SCAN_DELAY_JIFFIES (6 * HZ)
rndis_scan(struct wiphy * wiphy,struct cfg80211_scan_request * request)1927 static int rndis_scan(struct wiphy *wiphy,
1928 			struct cfg80211_scan_request *request)
1929 {
1930 	struct net_device *dev = request->wdev->netdev;
1931 	struct usbnet *usbdev = netdev_priv(dev);
1932 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1933 	int ret;
1934 	int delay = SCAN_DELAY_JIFFIES;
1935 
1936 	netdev_dbg(usbdev->net, "cfg80211.scan\n");
1937 
1938 	/* Get current bssid list from device before new scan, as new scan
1939 	 * clears internal bssid list.
1940 	 */
1941 	rndis_check_bssid_list(usbdev, NULL, NULL);
1942 
1943 	if (priv->scan_request && priv->scan_request != request)
1944 		return -EBUSY;
1945 
1946 	priv->scan_request = request;
1947 
1948 	ret = rndis_start_bssid_list_scan(usbdev);
1949 	if (ret == 0) {
1950 		if (priv->device_type == RNDIS_BCM4320A)
1951 			delay = HZ;
1952 
1953 		/* Wait before retrieving scan results from device */
1954 		queue_delayed_work(priv->workqueue, &priv->scan_work, delay);
1955 	}
1956 
1957 	return ret;
1958 }
1959 
rndis_bss_info_update(struct usbnet * usbdev,struct ndis_80211_bssid_ex * bssid)1960 static bool rndis_bss_info_update(struct usbnet *usbdev,
1961 				  struct ndis_80211_bssid_ex *bssid)
1962 {
1963 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1964 	struct ieee80211_channel *channel;
1965 	struct cfg80211_bss *bss;
1966 	s32 signal;
1967 	u64 timestamp;
1968 	u16 capability;
1969 	u16 beacon_interval;
1970 	struct ndis_80211_fixed_ies *fixed;
1971 	int ie_len, bssid_len;
1972 	u8 *ie;
1973 
1974 	netdev_dbg(usbdev->net, " found bssid: '%.32s' [%pM], len: %d\n",
1975 		   bssid->ssid.essid, bssid->mac, le32_to_cpu(bssid->length));
1976 
1977 	/* parse bssid structure */
1978 	bssid_len = le32_to_cpu(bssid->length);
1979 
1980 	if (bssid_len < sizeof(struct ndis_80211_bssid_ex) +
1981 			sizeof(struct ndis_80211_fixed_ies))
1982 		return NULL;
1983 
1984 	fixed = (struct ndis_80211_fixed_ies *)bssid->ies;
1985 
1986 	ie = (void *)(bssid->ies + sizeof(struct ndis_80211_fixed_ies));
1987 	ie_len = min(bssid_len - (int)sizeof(*bssid),
1988 					(int)le32_to_cpu(bssid->ie_length));
1989 	ie_len -= sizeof(struct ndis_80211_fixed_ies);
1990 	if (ie_len < 0)
1991 		return NULL;
1992 
1993 	/* extract data for cfg80211_inform_bss */
1994 	channel = ieee80211_get_channel(priv->wdev.wiphy,
1995 			KHZ_TO_MHZ(le32_to_cpu(bssid->config.ds_config)));
1996 	if (!channel)
1997 		return NULL;
1998 
1999 	signal = level_to_qual(le32_to_cpu(bssid->rssi));
2000 	timestamp = le64_to_cpu(*(__le64 *)fixed->timestamp);
2001 	capability = le16_to_cpu(fixed->capabilities);
2002 	beacon_interval = le16_to_cpu(fixed->beacon_interval);
2003 
2004 	bss = cfg80211_inform_bss(priv->wdev.wiphy, channel,
2005 				  CFG80211_BSS_FTYPE_UNKNOWN, bssid->mac,
2006 				  timestamp, capability, beacon_interval,
2007 				  ie, ie_len, signal, GFP_KERNEL);
2008 	cfg80211_put_bss(priv->wdev.wiphy, bss);
2009 
2010 	return (bss != NULL);
2011 }
2012 
next_bssid_list_item(struct ndis_80211_bssid_ex * bssid,int * bssid_len,void * buf,int len)2013 static struct ndis_80211_bssid_ex *next_bssid_list_item(
2014 					struct ndis_80211_bssid_ex *bssid,
2015 					int *bssid_len, void *buf, int len)
2016 {
2017 	void *buf_end, *bssid_end;
2018 
2019 	buf_end = (char *)buf + len;
2020 	bssid_end = (char *)bssid + *bssid_len;
2021 
2022 	if ((int)(buf_end - bssid_end) < sizeof(bssid->length)) {
2023 		*bssid_len = 0;
2024 		return NULL;
2025 	} else {
2026 		bssid = (void *)((char *)bssid + *bssid_len);
2027 		*bssid_len = le32_to_cpu(bssid->length);
2028 		return bssid;
2029 	}
2030 }
2031 
check_bssid_list_item(struct ndis_80211_bssid_ex * bssid,int bssid_len,void * buf,int len)2032 static bool check_bssid_list_item(struct ndis_80211_bssid_ex *bssid,
2033 				  int bssid_len, void *buf, int len)
2034 {
2035 	void *buf_end, *bssid_end;
2036 
2037 	if (!bssid || bssid_len <= 0 || bssid_len > len)
2038 		return false;
2039 
2040 	buf_end = (char *)buf + len;
2041 	bssid_end = (char *)bssid + bssid_len;
2042 
2043 	return (int)(buf_end - bssid_end) >= 0 && (int)(bssid_end - buf) >= 0;
2044 }
2045 
rndis_check_bssid_list(struct usbnet * usbdev,u8 * match_bssid,bool * matched)2046 static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
2047 					bool *matched)
2048 {
2049 	void *buf = NULL;
2050 	struct ndis_80211_bssid_list_ex *bssid_list;
2051 	struct ndis_80211_bssid_ex *bssid;
2052 	int ret = -EINVAL, len, count, bssid_len, real_count, new_len;
2053 
2054 	netdev_dbg(usbdev->net, "%s()\n", __func__);
2055 
2056 	len = CONTROL_BUFFER_SIZE;
2057 resize_buf:
2058 	buf = kzalloc(len, GFP_KERNEL);
2059 	if (!buf) {
2060 		ret = -ENOMEM;
2061 		goto out;
2062 	}
2063 
2064 	/* BSSID-list might have got bigger last time we checked, keep
2065 	 * resizing until it won't get any bigger.
2066 	 */
2067 	new_len = len;
2068 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_BSSID_LIST,
2069 			      buf, &new_len);
2070 	if (ret != 0 || new_len < sizeof(struct ndis_80211_bssid_list_ex))
2071 		goto out;
2072 
2073 	if (new_len > len) {
2074 		len = new_len;
2075 		kfree(buf);
2076 		goto resize_buf;
2077 	}
2078 
2079 	len = new_len;
2080 
2081 	bssid_list = buf;
2082 	count = le32_to_cpu(bssid_list->num_items);
2083 	real_count = 0;
2084 	netdev_dbg(usbdev->net, "%s(): buflen: %d\n", __func__, len);
2085 
2086 	bssid_len = 0;
2087 	bssid = next_bssid_list_item((void *)bssid_list->bssid_data,
2088 				     &bssid_len, buf, len);
2089 
2090 	/* Device returns incorrect 'num_items'. Workaround by ignoring the
2091 	 * received 'num_items' and walking through full bssid buffer instead.
2092 	 */
2093 	while (check_bssid_list_item(bssid, bssid_len, buf, len)) {
2094 		if (rndis_bss_info_update(usbdev, bssid) && match_bssid &&
2095 		    matched) {
2096 			if (ether_addr_equal(bssid->mac, match_bssid))
2097 				*matched = true;
2098 		}
2099 
2100 		real_count++;
2101 		bssid = next_bssid_list_item(bssid, &bssid_len, buf, len);
2102 	}
2103 
2104 	netdev_dbg(usbdev->net, "%s(): num_items from device: %d, really found:"
2105 				" %d\n", __func__, count, real_count);
2106 
2107 out:
2108 	kfree(buf);
2109 	return ret;
2110 }
2111 
rndis_get_scan_results(struct work_struct * work)2112 static void rndis_get_scan_results(struct work_struct *work)
2113 {
2114 	struct rndis_wlan_private *priv =
2115 		container_of(work, struct rndis_wlan_private, scan_work.work);
2116 	struct usbnet *usbdev = priv->usbdev;
2117 	struct cfg80211_scan_info info = {};
2118 	int ret;
2119 
2120 	netdev_dbg(usbdev->net, "get_scan_results\n");
2121 
2122 	if (!priv->scan_request)
2123 		return;
2124 
2125 	ret = rndis_check_bssid_list(usbdev, NULL, NULL);
2126 
2127 	info.aborted = ret < 0;
2128 	cfg80211_scan_done(priv->scan_request, &info);
2129 
2130 	priv->scan_request = NULL;
2131 }
2132 
rndis_connect(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_connect_params * sme)2133 static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
2134 					struct cfg80211_connect_params *sme)
2135 {
2136 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2137 	struct usbnet *usbdev = priv->usbdev;
2138 	struct ieee80211_channel *channel = sme->channel;
2139 	struct ndis_80211_ssid ssid;
2140 	int pairwise = RNDIS_WLAN_ALG_NONE;
2141 	int groupwise = RNDIS_WLAN_ALG_NONE;
2142 	int keymgmt = RNDIS_WLAN_KEY_MGMT_NONE;
2143 	int length, i, ret, chan = -1;
2144 
2145 	if (channel)
2146 		chan = ieee80211_frequency_to_channel(channel->center_freq);
2147 
2148 	groupwise = rndis_cipher_to_alg(sme->crypto.cipher_group);
2149 	for (i = 0; i < sme->crypto.n_ciphers_pairwise; i++)
2150 		pairwise |=
2151 			rndis_cipher_to_alg(sme->crypto.ciphers_pairwise[i]);
2152 
2153 	if (sme->crypto.n_ciphers_pairwise > 0 &&
2154 			pairwise == RNDIS_WLAN_ALG_NONE) {
2155 		netdev_err(usbdev->net, "Unsupported pairwise cipher\n");
2156 		return -ENOTSUPP;
2157 	}
2158 
2159 	for (i = 0; i < sme->crypto.n_akm_suites; i++)
2160 		keymgmt |=
2161 			rndis_akm_suite_to_key_mgmt(sme->crypto.akm_suites[i]);
2162 
2163 	if (sme->crypto.n_akm_suites > 0 &&
2164 			keymgmt == RNDIS_WLAN_KEY_MGMT_NONE) {
2165 		netdev_err(usbdev->net, "Invalid keymgmt\n");
2166 		return -ENOTSUPP;
2167 	}
2168 
2169 	netdev_dbg(usbdev->net, "cfg80211.connect('%.32s':[%pM]:%d:[%d,0x%x:0x%x]:[0x%x:0x%x]:0x%x)\n",
2170 		   sme->ssid, sme->bssid, chan,
2171 		   sme->privacy, sme->crypto.wpa_versions, sme->auth_type,
2172 		   groupwise, pairwise, keymgmt);
2173 
2174 	if (is_associated(usbdev))
2175 		disassociate(usbdev, false);
2176 
2177 	ret = set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
2178 	if (ret < 0) {
2179 		netdev_dbg(usbdev->net, "connect: set_infra_mode failed, %d\n",
2180 			   ret);
2181 		goto err_turn_radio_on;
2182 	}
2183 
2184 	ret = set_auth_mode(usbdev, sme->crypto.wpa_versions, sme->auth_type,
2185 								keymgmt);
2186 	if (ret < 0) {
2187 		netdev_dbg(usbdev->net, "connect: set_auth_mode failed, %d\n",
2188 			   ret);
2189 		goto err_turn_radio_on;
2190 	}
2191 
2192 	set_priv_filter(usbdev);
2193 
2194 	ret = set_encr_mode(usbdev, pairwise, groupwise);
2195 	if (ret < 0) {
2196 		netdev_dbg(usbdev->net, "connect: set_encr_mode failed, %d\n",
2197 			   ret);
2198 		goto err_turn_radio_on;
2199 	}
2200 
2201 	if (channel) {
2202 		ret = set_channel(usbdev, chan);
2203 		if (ret < 0) {
2204 			netdev_dbg(usbdev->net, "connect: set_channel failed, %d\n",
2205 				   ret);
2206 			goto err_turn_radio_on;
2207 		}
2208 	}
2209 
2210 	if (sme->key && ((groupwise | pairwise) & RNDIS_WLAN_ALG_WEP)) {
2211 		priv->encr_tx_key_index = sme->key_idx;
2212 		ret = add_wep_key(usbdev, sme->key, sme->key_len, sme->key_idx);
2213 		if (ret < 0) {
2214 			netdev_dbg(usbdev->net, "connect: add_wep_key failed, %d (%d, %d)\n",
2215 				   ret, sme->key_len, sme->key_idx);
2216 			goto err_turn_radio_on;
2217 		}
2218 	}
2219 
2220 	if (sme->bssid && !is_zero_ether_addr(sme->bssid) &&
2221 				!is_broadcast_ether_addr(sme->bssid)) {
2222 		ret = set_bssid(usbdev, sme->bssid);
2223 		if (ret < 0) {
2224 			netdev_dbg(usbdev->net, "connect: set_bssid failed, %d\n",
2225 				   ret);
2226 			goto err_turn_radio_on;
2227 		}
2228 	} else
2229 		clear_bssid(usbdev);
2230 
2231 	length = sme->ssid_len;
2232 	if (length > NDIS_802_11_LENGTH_SSID)
2233 		length = NDIS_802_11_LENGTH_SSID;
2234 
2235 	memset(&ssid, 0, sizeof(ssid));
2236 	ssid.length = cpu_to_le32(length);
2237 	memcpy(ssid.essid, sme->ssid, length);
2238 
2239 	/* Pause and purge rx queue, so we don't pass packets before
2240 	 * 'media connect'-indication.
2241 	 */
2242 	usbnet_pause_rx(usbdev);
2243 	usbnet_purge_paused_rxq(usbdev);
2244 
2245 	ret = set_essid(usbdev, &ssid);
2246 	if (ret < 0)
2247 		netdev_dbg(usbdev->net, "connect: set_essid failed, %d\n", ret);
2248 	return ret;
2249 
2250 err_turn_radio_on:
2251 	disassociate(usbdev, true);
2252 
2253 	return ret;
2254 }
2255 
rndis_disconnect(struct wiphy * wiphy,struct net_device * dev,u16 reason_code)2256 static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
2257 								u16 reason_code)
2258 {
2259 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2260 	struct usbnet *usbdev = priv->usbdev;
2261 
2262 	netdev_dbg(usbdev->net, "cfg80211.disconnect(%d)\n", reason_code);
2263 
2264 	priv->connected = false;
2265 	eth_zero_addr(priv->bssid);
2266 
2267 	return deauthenticate(usbdev);
2268 }
2269 
rndis_join_ibss(struct wiphy * wiphy,struct net_device * dev,struct cfg80211_ibss_params * params)2270 static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
2271 					struct cfg80211_ibss_params *params)
2272 {
2273 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2274 	struct usbnet *usbdev = priv->usbdev;
2275 	struct ieee80211_channel *channel = params->chandef.chan;
2276 	struct ndis_80211_ssid ssid;
2277 	enum nl80211_auth_type auth_type;
2278 	int ret, alg, length, chan = -1;
2279 
2280 	if (channel)
2281 		chan = ieee80211_frequency_to_channel(channel->center_freq);
2282 
2283 	/* TODO: How to handle ad-hoc encryption?
2284 	 * connect() has *key, join_ibss() doesn't. RNDIS requires key to be
2285 	 * pre-shared for encryption (open/shared/wpa), is key set before
2286 	 * join_ibss? Which auth_type to use (not in params)? What about WPA?
2287 	 */
2288 	if (params->privacy) {
2289 		auth_type = NL80211_AUTHTYPE_SHARED_KEY;
2290 		alg = RNDIS_WLAN_ALG_WEP;
2291 	} else {
2292 		auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2293 		alg = RNDIS_WLAN_ALG_NONE;
2294 	}
2295 
2296 	netdev_dbg(usbdev->net, "cfg80211.join_ibss('%.32s':[%pM]:%d:%d)\n",
2297 		   params->ssid, params->bssid, chan, params->privacy);
2298 
2299 	if (is_associated(usbdev))
2300 		disassociate(usbdev, false);
2301 
2302 	ret = set_infra_mode(usbdev, NDIS_80211_INFRA_ADHOC);
2303 	if (ret < 0) {
2304 		netdev_dbg(usbdev->net, "join_ibss: set_infra_mode failed, %d\n",
2305 			   ret);
2306 		goto err_turn_radio_on;
2307 	}
2308 
2309 	ret = set_auth_mode(usbdev, 0, auth_type, RNDIS_WLAN_KEY_MGMT_NONE);
2310 	if (ret < 0) {
2311 		netdev_dbg(usbdev->net, "join_ibss: set_auth_mode failed, %d\n",
2312 			   ret);
2313 		goto err_turn_radio_on;
2314 	}
2315 
2316 	set_priv_filter(usbdev);
2317 
2318 	ret = set_encr_mode(usbdev, alg, RNDIS_WLAN_ALG_NONE);
2319 	if (ret < 0) {
2320 		netdev_dbg(usbdev->net, "join_ibss: set_encr_mode failed, %d\n",
2321 			   ret);
2322 		goto err_turn_radio_on;
2323 	}
2324 
2325 	if (channel) {
2326 		ret = set_channel(usbdev, chan);
2327 		if (ret < 0) {
2328 			netdev_dbg(usbdev->net, "join_ibss: set_channel failed, %d\n",
2329 				   ret);
2330 			goto err_turn_radio_on;
2331 		}
2332 	}
2333 
2334 	if (params->bssid && !is_zero_ether_addr(params->bssid) &&
2335 				!is_broadcast_ether_addr(params->bssid)) {
2336 		ret = set_bssid(usbdev, params->bssid);
2337 		if (ret < 0) {
2338 			netdev_dbg(usbdev->net, "join_ibss: set_bssid failed, %d\n",
2339 				   ret);
2340 			goto err_turn_radio_on;
2341 		}
2342 	} else
2343 		clear_bssid(usbdev);
2344 
2345 	length = params->ssid_len;
2346 	if (length > NDIS_802_11_LENGTH_SSID)
2347 		length = NDIS_802_11_LENGTH_SSID;
2348 
2349 	memset(&ssid, 0, sizeof(ssid));
2350 	ssid.length = cpu_to_le32(length);
2351 	memcpy(ssid.essid, params->ssid, length);
2352 
2353 	/* Don't need to pause rx queue for ad-hoc. */
2354 	usbnet_purge_paused_rxq(usbdev);
2355 	usbnet_resume_rx(usbdev);
2356 
2357 	ret = set_essid(usbdev, &ssid);
2358 	if (ret < 0)
2359 		netdev_dbg(usbdev->net, "join_ibss: set_essid failed, %d\n",
2360 			   ret);
2361 	return ret;
2362 
2363 err_turn_radio_on:
2364 	disassociate(usbdev, true);
2365 
2366 	return ret;
2367 }
2368 
rndis_leave_ibss(struct wiphy * wiphy,struct net_device * dev)2369 static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2370 {
2371 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2372 	struct usbnet *usbdev = priv->usbdev;
2373 
2374 	netdev_dbg(usbdev->net, "cfg80211.leave_ibss()\n");
2375 
2376 	priv->connected = false;
2377 	eth_zero_addr(priv->bssid);
2378 
2379 	return deauthenticate(usbdev);
2380 }
2381 
rndis_add_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr,struct key_params * params)2382 static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
2383 			 int link_id,  u8 key_index, bool pairwise,
2384 			 const u8 *mac_addr, struct key_params *params)
2385 {
2386 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2387 	struct usbnet *usbdev = priv->usbdev;
2388 	__le32 flags;
2389 
2390 	netdev_dbg(usbdev->net, "%s(%i, %pM, %08x)\n",
2391 		   __func__, key_index, mac_addr, params->cipher);
2392 
2393 	switch (params->cipher) {
2394 	case WLAN_CIPHER_SUITE_WEP40:
2395 	case WLAN_CIPHER_SUITE_WEP104:
2396 		return add_wep_key(usbdev, params->key, params->key_len,
2397 								key_index);
2398 	case WLAN_CIPHER_SUITE_TKIP:
2399 	case WLAN_CIPHER_SUITE_CCMP:
2400 		flags = 0;
2401 
2402 		if (params->seq && params->seq_len > 0)
2403 			flags |= NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ;
2404 		if (mac_addr)
2405 			flags |= NDIS_80211_ADDKEY_PAIRWISE_KEY |
2406 					NDIS_80211_ADDKEY_TRANSMIT_KEY;
2407 
2408 		return add_wpa_key(usbdev, params->key, params->key_len,
2409 				key_index, mac_addr, params->seq,
2410 				params->seq_len, params->cipher, flags);
2411 	default:
2412 		netdev_dbg(usbdev->net, "%s(): unsupported cipher %08x\n",
2413 			   __func__, params->cipher);
2414 		return -ENOTSUPP;
2415 	}
2416 }
2417 
rndis_del_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr)2418 static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
2419 			 int link_id, u8 key_index, bool pairwise,
2420 			 const u8 *mac_addr)
2421 {
2422 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2423 	struct usbnet *usbdev = priv->usbdev;
2424 
2425 	netdev_dbg(usbdev->net, "%s(%i, %pM)\n", __func__, key_index, mac_addr);
2426 
2427 	return remove_key(usbdev, key_index, mac_addr);
2428 }
2429 
rndis_set_default_key(struct wiphy * wiphy,struct net_device * netdev,int link_id,u8 key_index,bool unicast,bool multicast)2430 static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
2431 				 int link_id, u8 key_index, bool unicast,
2432 				 bool multicast)
2433 {
2434 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2435 	struct usbnet *usbdev = priv->usbdev;
2436 	struct rndis_wlan_encr_key key;
2437 
2438 	netdev_dbg(usbdev->net, "%s(%i)\n", __func__, key_index);
2439 
2440 	if (key_index >= RNDIS_WLAN_NUM_KEYS)
2441 		return -ENOENT;
2442 
2443 	priv->encr_tx_key_index = key_index;
2444 
2445 	if (is_wpa_key(priv, key_index))
2446 		return 0;
2447 
2448 	key = priv->encr_keys[key_index];
2449 
2450 	return add_wep_key(usbdev, key.material, key.len, key_index);
2451 }
2452 
rndis_fill_station_info(struct usbnet * usbdev,struct station_info * sinfo)2453 static void rndis_fill_station_info(struct usbnet *usbdev,
2454 						struct station_info *sinfo)
2455 {
2456 	__le32 linkspeed, rssi;
2457 	int ret, len;
2458 
2459 	memset(sinfo, 0, sizeof(*sinfo));
2460 
2461 	len = sizeof(linkspeed);
2462 	ret = rndis_query_oid(usbdev, RNDIS_OID_GEN_LINK_SPEED, &linkspeed, &len);
2463 	if (ret == 0) {
2464 		sinfo->txrate.legacy = le32_to_cpu(linkspeed) / 1000;
2465 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2466 	}
2467 
2468 	len = sizeof(rssi);
2469 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
2470 			      &rssi, &len);
2471 	if (ret == 0) {
2472 		sinfo->signal = level_to_qual(le32_to_cpu(rssi));
2473 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2474 	}
2475 }
2476 
rndis_get_station(struct wiphy * wiphy,struct net_device * dev,const u8 * mac,struct station_info * sinfo)2477 static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
2478 			     const u8 *mac, struct station_info *sinfo)
2479 {
2480 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2481 	struct usbnet *usbdev = priv->usbdev;
2482 
2483 	if (!ether_addr_equal(priv->bssid, mac))
2484 		return -ENOENT;
2485 
2486 	rndis_fill_station_info(usbdev, sinfo);
2487 
2488 	return 0;
2489 }
2490 
rndis_dump_station(struct wiphy * wiphy,struct net_device * dev,int idx,u8 * mac,struct station_info * sinfo)2491 static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
2492 			       int idx, u8 *mac, struct station_info *sinfo)
2493 {
2494 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2495 	struct usbnet *usbdev = priv->usbdev;
2496 
2497 	if (idx != 0)
2498 		return -ENOENT;
2499 
2500 	memcpy(mac, priv->bssid, ETH_ALEN);
2501 
2502 	rndis_fill_station_info(usbdev, sinfo);
2503 
2504 	return 0;
2505 }
2506 
rndis_set_pmksa(struct wiphy * wiphy,struct net_device * netdev,struct cfg80211_pmksa * pmksa)2507 static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2508 				struct cfg80211_pmksa *pmksa)
2509 {
2510 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2511 	struct usbnet *usbdev = priv->usbdev;
2512 	struct ndis_80211_pmkid *pmkids;
2513 	u32 *tmp = (u32 *)pmksa->pmkid;
2514 
2515 	netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
2516 			pmksa->bssid,
2517 			cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
2518 			cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
2519 
2520 	pmkids = get_device_pmkids(usbdev);
2521 	if (IS_ERR(pmkids)) {
2522 		/* couldn't read PMKID cache from device */
2523 		return PTR_ERR(pmkids);
2524 	}
2525 
2526 	pmkids = update_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
2527 	if (IS_ERR(pmkids)) {
2528 		/* not found, list full, etc */
2529 		return PTR_ERR(pmkids);
2530 	}
2531 
2532 	return set_device_pmkids(usbdev, pmkids);
2533 }
2534 
rndis_del_pmksa(struct wiphy * wiphy,struct net_device * netdev,struct cfg80211_pmksa * pmksa)2535 static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
2536 				struct cfg80211_pmksa *pmksa)
2537 {
2538 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2539 	struct usbnet *usbdev = priv->usbdev;
2540 	struct ndis_80211_pmkid *pmkids;
2541 	u32 *tmp = (u32 *)pmksa->pmkid;
2542 
2543 	netdev_dbg(usbdev->net, "%s(%pM, %08X:%08X:%08X:%08X)\n", __func__,
2544 			pmksa->bssid,
2545 			cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
2546 			cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
2547 
2548 	pmkids = get_device_pmkids(usbdev);
2549 	if (IS_ERR(pmkids)) {
2550 		/* Couldn't read PMKID cache from device */
2551 		return PTR_ERR(pmkids);
2552 	}
2553 
2554 	pmkids = remove_pmkid(usbdev, pmkids, pmksa, wiphy->max_num_pmkids);
2555 	if (IS_ERR(pmkids)) {
2556 		/* not found, etc */
2557 		return PTR_ERR(pmkids);
2558 	}
2559 
2560 	return set_device_pmkids(usbdev, pmkids);
2561 }
2562 
rndis_flush_pmksa(struct wiphy * wiphy,struct net_device * netdev)2563 static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
2564 {
2565 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2566 	struct usbnet *usbdev = priv->usbdev;
2567 	struct ndis_80211_pmkid pmkid;
2568 
2569 	netdev_dbg(usbdev->net, "%s()\n", __func__);
2570 
2571 	memset(&pmkid, 0, sizeof(pmkid));
2572 
2573 	pmkid.length = cpu_to_le32(sizeof(pmkid));
2574 	pmkid.bssid_info_count = cpu_to_le32(0);
2575 
2576 	return rndis_set_oid(usbdev, RNDIS_OID_802_11_PMKID,
2577 			     &pmkid, sizeof(pmkid));
2578 }
2579 
rndis_set_power_mgmt(struct wiphy * wiphy,struct net_device * dev,bool enabled,int timeout)2580 static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2581 				bool enabled, int timeout)
2582 {
2583 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2584 	struct usbnet *usbdev = priv->usbdev;
2585 	int power_mode;
2586 	__le32 mode;
2587 	int ret;
2588 
2589 	if (priv->device_type != RNDIS_BCM4320B)
2590 		return -ENOTSUPP;
2591 
2592 	netdev_dbg(usbdev->net, "%s(): %s, %d\n", __func__,
2593 				enabled ? "enabled" : "disabled",
2594 				timeout);
2595 
2596 	if (enabled)
2597 		power_mode = NDIS_80211_POWER_MODE_FAST_PSP;
2598 	else
2599 		power_mode = NDIS_80211_POWER_MODE_CAM;
2600 
2601 	if (power_mode == priv->power_mode)
2602 		return 0;
2603 
2604 	priv->power_mode = power_mode;
2605 
2606 	mode = cpu_to_le32(power_mode);
2607 	ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_POWER_MODE,
2608 			    &mode, sizeof(mode));
2609 
2610 	netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_POWER_MODE -> %d\n",
2611 				__func__, ret);
2612 
2613 	return ret;
2614 }
2615 
rndis_set_cqm_rssi_config(struct wiphy * wiphy,struct net_device * dev,s32 rssi_thold,u32 rssi_hyst)2616 static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
2617 					struct net_device *dev,
2618 					s32 rssi_thold, u32 rssi_hyst)
2619 {
2620 	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
2621 
2622 	priv->cqm_rssi_thold = rssi_thold;
2623 	priv->cqm_rssi_hyst = rssi_hyst;
2624 	priv->last_cqm_event_rssi = 0;
2625 
2626 	return 0;
2627 }
2628 
rndis_wlan_craft_connected_bss(struct usbnet * usbdev,u8 * bssid,struct ndis_80211_assoc_info * info)2629 static void rndis_wlan_craft_connected_bss(struct usbnet *usbdev, u8 *bssid,
2630 					   struct ndis_80211_assoc_info *info)
2631 {
2632 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2633 	struct ieee80211_channel *channel;
2634 	struct ndis_80211_ssid ssid;
2635 	struct cfg80211_bss *bss;
2636 	s32 signal;
2637 	u64 timestamp;
2638 	u16 capability;
2639 	u32 beacon_period = 0;
2640 	__le32 rssi;
2641 	u8 ie_buf[34];
2642 	int len, ret, ie_len;
2643 
2644 	/* Get signal quality, in case of error use rssi=0 and ignore error. */
2645 	len = sizeof(rssi);
2646 	rssi = 0;
2647 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
2648 			      &rssi, &len);
2649 	signal = level_to_qual(le32_to_cpu(rssi));
2650 
2651 	netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_RSSI -> %d, "
2652 		   "rssi:%d, qual: %d\n", __func__, ret, le32_to_cpu(rssi),
2653 		   level_to_qual(le32_to_cpu(rssi)));
2654 
2655 	/* Get AP capabilities */
2656 	if (info) {
2657 		capability = le16_to_cpu(info->resp_ie.capa);
2658 	} else {
2659 		/* Set atleast ESS/IBSS capability */
2660 		capability = (priv->infra_mode == NDIS_80211_INFRA_INFRA) ?
2661 				WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS;
2662 	}
2663 
2664 	/* Get channel and beacon interval */
2665 	channel = get_current_channel(usbdev, &beacon_period);
2666 	if (!channel) {
2667 		netdev_warn(usbdev->net, "%s(): could not get channel.\n",
2668 					__func__);
2669 		return;
2670 	}
2671 
2672 	/* Get SSID, in case of error, use zero length SSID and ignore error. */
2673 	len = sizeof(ssid);
2674 	memset(&ssid, 0, sizeof(ssid));
2675 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_SSID,
2676 			      &ssid, &len);
2677 	netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_SSID -> %d, len: %d, ssid: "
2678 				"'%.32s'\n", __func__, ret,
2679 				le32_to_cpu(ssid.length), ssid.essid);
2680 
2681 	if (le32_to_cpu(ssid.length) > 32)
2682 		ssid.length = cpu_to_le32(32);
2683 
2684 	ie_buf[0] = WLAN_EID_SSID;
2685 	ie_buf[1] = le32_to_cpu(ssid.length);
2686 	memcpy(&ie_buf[2], ssid.essid, le32_to_cpu(ssid.length));
2687 
2688 	ie_len = le32_to_cpu(ssid.length) + 2;
2689 
2690 	/* no tsf */
2691 	timestamp = 0;
2692 
2693 	netdev_dbg(usbdev->net, "%s(): channel:%d(freq), bssid:[%pM], tsf:%d, "
2694 		"capa:%x, beacon int:%d, resp_ie(len:%d, essid:'%.32s'), "
2695 		"signal:%d\n", __func__, (channel ? channel->center_freq : -1),
2696 		bssid, (u32)timestamp, capability, beacon_period, ie_len,
2697 		ssid.essid, signal);
2698 
2699 	bss = cfg80211_inform_bss(priv->wdev.wiphy, channel,
2700 				  CFG80211_BSS_FTYPE_UNKNOWN, bssid,
2701 				  timestamp, capability, beacon_period,
2702 				  ie_buf, ie_len, signal, GFP_KERNEL);
2703 	cfg80211_put_bss(priv->wdev.wiphy, bss);
2704 }
2705 
2706 /*
2707  * workers, indication handlers, device poller
2708  */
rndis_wlan_do_link_up_work(struct usbnet * usbdev)2709 static void rndis_wlan_do_link_up_work(struct usbnet *usbdev)
2710 {
2711 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2712 	struct ndis_80211_assoc_info *info = NULL;
2713 	u8 bssid[ETH_ALEN];
2714 	unsigned int resp_ie_len, req_ie_len;
2715 	unsigned int offset;
2716 	u8 *req_ie, *resp_ie;
2717 	int ret;
2718 	bool roamed = false;
2719 	bool match_bss;
2720 
2721 	if (priv->infra_mode == NDIS_80211_INFRA_INFRA && priv->connected) {
2722 		/* received media connect indication while connected, either
2723 		 * device reassociated with same AP or roamed to new. */
2724 		roamed = true;
2725 	}
2726 
2727 	req_ie_len = 0;
2728 	resp_ie_len = 0;
2729 	req_ie = NULL;
2730 	resp_ie = NULL;
2731 
2732 	if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2733 		info = kzalloc(CONTROL_BUFFER_SIZE, GFP_KERNEL);
2734 		if (!info) {
2735 			/* No memory? Try resume work later */
2736 			set_bit(WORK_LINK_UP, &priv->work_pending);
2737 			queue_work(priv->workqueue, &priv->work);
2738 			return;
2739 		}
2740 
2741 		/* Get association info IEs from device. */
2742 		ret = get_association_info(usbdev, info, CONTROL_BUFFER_SIZE);
2743 		if (!ret) {
2744 			req_ie_len = le32_to_cpu(info->req_ie_length);
2745 			if (req_ie_len > CONTROL_BUFFER_SIZE)
2746 				req_ie_len = CONTROL_BUFFER_SIZE;
2747 			if (req_ie_len != 0) {
2748 				offset = le32_to_cpu(info->offset_req_ies);
2749 
2750 				if (offset > CONTROL_BUFFER_SIZE)
2751 					offset = CONTROL_BUFFER_SIZE;
2752 
2753 				req_ie = (u8 *)info + offset;
2754 
2755 				if (offset + req_ie_len > CONTROL_BUFFER_SIZE)
2756 					req_ie_len =
2757 						CONTROL_BUFFER_SIZE - offset;
2758 			}
2759 
2760 			resp_ie_len = le32_to_cpu(info->resp_ie_length);
2761 			if (resp_ie_len > CONTROL_BUFFER_SIZE)
2762 				resp_ie_len = CONTROL_BUFFER_SIZE;
2763 			if (resp_ie_len != 0) {
2764 				offset = le32_to_cpu(info->offset_resp_ies);
2765 
2766 				if (offset > CONTROL_BUFFER_SIZE)
2767 					offset = CONTROL_BUFFER_SIZE;
2768 
2769 				resp_ie = (u8 *)info + offset;
2770 
2771 				if (offset + resp_ie_len > CONTROL_BUFFER_SIZE)
2772 					resp_ie_len =
2773 						CONTROL_BUFFER_SIZE - offset;
2774 			}
2775 		} else {
2776 			/* Since rndis_wlan_craft_connected_bss() might use info
2777 			 * later and expects info to contain valid data if
2778 			 * non-null, free info and set NULL here.
2779 			 */
2780 			kfree(info);
2781 			info = NULL;
2782 		}
2783 	} else if (WARN_ON(priv->infra_mode != NDIS_80211_INFRA_ADHOC))
2784 		return;
2785 
2786 	ret = get_bssid(usbdev, bssid);
2787 	if (ret < 0)
2788 		memset(bssid, 0, sizeof(bssid));
2789 
2790 	netdev_dbg(usbdev->net, "link up work: [%pM]%s\n",
2791 		   bssid, roamed ? " roamed" : "");
2792 
2793 	/* Internal bss list in device should contain at least the currently
2794 	 * connected bss and we can get it to cfg80211 with
2795 	 * rndis_check_bssid_list().
2796 	 *
2797 	 * NDIS spec says: "If the device is associated, but the associated
2798 	 *  BSSID is not in its BSSID scan list, then the driver must add an
2799 	 *  entry for the BSSID at the end of the data that it returns in
2800 	 *  response to query of RNDIS_OID_802_11_BSSID_LIST."
2801 	 *
2802 	 * NOTE: Seems to be true for BCM4320b variant, but not BCM4320a.
2803 	 */
2804 	match_bss = false;
2805 	rndis_check_bssid_list(usbdev, bssid, &match_bss);
2806 
2807 	if (!is_zero_ether_addr(bssid) && !match_bss) {
2808 		/* Couldn't get bss from device, we need to manually craft bss
2809 		 * for cfg80211.
2810 		 */
2811 		rndis_wlan_craft_connected_bss(usbdev, bssid, info);
2812 	}
2813 
2814 	if (priv->infra_mode == NDIS_80211_INFRA_INFRA) {
2815 		if (!roamed) {
2816 			cfg80211_connect_result(usbdev->net, bssid, req_ie,
2817 						req_ie_len, resp_ie,
2818 						resp_ie_len, 0, GFP_KERNEL);
2819 		} else {
2820 			struct cfg80211_roam_info roam_info = {
2821 				.links[0].channel =
2822 					get_current_channel(usbdev, NULL),
2823 				.links[0].bssid = bssid,
2824 				.req_ie = req_ie,
2825 				.req_ie_len = req_ie_len,
2826 				.resp_ie = resp_ie,
2827 				.resp_ie_len = resp_ie_len,
2828 			};
2829 
2830 			cfg80211_roamed(usbdev->net, &roam_info, GFP_KERNEL);
2831 		}
2832 	} else if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
2833 		cfg80211_ibss_joined(usbdev->net, bssid,
2834 				     get_current_channel(usbdev, NULL),
2835 				     GFP_KERNEL);
2836 
2837 	kfree(info);
2838 
2839 	priv->connected = true;
2840 	memcpy(priv->bssid, bssid, ETH_ALEN);
2841 
2842 	usbnet_resume_rx(usbdev);
2843 	netif_carrier_on(usbdev->net);
2844 }
2845 
rndis_wlan_do_link_down_work(struct usbnet * usbdev)2846 static void rndis_wlan_do_link_down_work(struct usbnet *usbdev)
2847 {
2848 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2849 
2850 	if (priv->connected) {
2851 		priv->connected = false;
2852 		eth_zero_addr(priv->bssid);
2853 
2854 		deauthenticate(usbdev);
2855 
2856 		cfg80211_disconnected(usbdev->net, 0, NULL, 0, true, GFP_KERNEL);
2857 	}
2858 
2859 	netif_carrier_off(usbdev->net);
2860 }
2861 
rndis_wlan_worker(struct work_struct * work)2862 static void rndis_wlan_worker(struct work_struct *work)
2863 {
2864 	struct rndis_wlan_private *priv =
2865 		container_of(work, struct rndis_wlan_private, work);
2866 	struct usbnet *usbdev = priv->usbdev;
2867 
2868 	if (test_and_clear_bit(WORK_LINK_UP, &priv->work_pending))
2869 		rndis_wlan_do_link_up_work(usbdev);
2870 
2871 	if (test_and_clear_bit(WORK_LINK_DOWN, &priv->work_pending))
2872 		rndis_wlan_do_link_down_work(usbdev);
2873 
2874 	if (test_and_clear_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2875 		set_multicast_list(usbdev);
2876 }
2877 
rndis_wlan_set_multicast_list(struct net_device * dev)2878 static void rndis_wlan_set_multicast_list(struct net_device *dev)
2879 {
2880 	struct usbnet *usbdev = netdev_priv(dev);
2881 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
2882 
2883 	if (test_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending))
2884 		return;
2885 
2886 	set_bit(WORK_SET_MULTICAST_LIST, &priv->work_pending);
2887 	queue_work(priv->workqueue, &priv->work);
2888 }
2889 
rndis_wlan_auth_indication(struct usbnet * usbdev,struct ndis_80211_status_indication * indication,int len)2890 static void rndis_wlan_auth_indication(struct usbnet *usbdev,
2891 				struct ndis_80211_status_indication *indication,
2892 				int len)
2893 {
2894 	u8 *buf;
2895 	const char *type;
2896 	int flags, buflen, key_id;
2897 	bool pairwise_error, group_error;
2898 	struct ndis_80211_auth_request *auth_req;
2899 	enum nl80211_key_type key_type;
2900 
2901 	/* must have at least one array entry */
2902 	if (len < offsetof(struct ndis_80211_status_indication, u) +
2903 				sizeof(struct ndis_80211_auth_request)) {
2904 		netdev_info(usbdev->net, "authentication indication: too short message (%i)\n",
2905 			    len);
2906 		return;
2907 	}
2908 
2909 	buf = (void *)&indication->u.auth_request[0];
2910 	buflen = len - offsetof(struct ndis_80211_status_indication, u);
2911 
2912 	while (buflen >= sizeof(*auth_req)) {
2913 		auth_req = (void *)buf;
2914 		if (buflen < le32_to_cpu(auth_req->length))
2915 			return;
2916 		type = "unknown";
2917 		flags = le32_to_cpu(auth_req->flags);
2918 		pairwise_error = false;
2919 		group_error = false;
2920 
2921 		if (flags & 0x1)
2922 			type = "reauth request";
2923 		if (flags & 0x2)
2924 			type = "key update request";
2925 		if (flags & 0x6) {
2926 			pairwise_error = true;
2927 			type = "pairwise_error";
2928 		}
2929 		if (flags & 0xe) {
2930 			group_error = true;
2931 			type = "group_error";
2932 		}
2933 
2934 		netdev_info(usbdev->net, "authentication indication: %s (0x%08x)\n",
2935 			    type, le32_to_cpu(auth_req->flags));
2936 
2937 		if (pairwise_error) {
2938 			key_type = NL80211_KEYTYPE_PAIRWISE;
2939 			key_id = -1;
2940 
2941 			cfg80211_michael_mic_failure(usbdev->net,
2942 							auth_req->bssid,
2943 							key_type, key_id, NULL,
2944 							GFP_KERNEL);
2945 		}
2946 
2947 		if (group_error) {
2948 			key_type = NL80211_KEYTYPE_GROUP;
2949 			key_id = -1;
2950 
2951 			cfg80211_michael_mic_failure(usbdev->net,
2952 							auth_req->bssid,
2953 							key_type, key_id, NULL,
2954 							GFP_KERNEL);
2955 		}
2956 
2957 		buflen -= le32_to_cpu(auth_req->length);
2958 		buf += le32_to_cpu(auth_req->length);
2959 	}
2960 }
2961 
rndis_wlan_pmkid_cand_list_indication(struct usbnet * usbdev,struct ndis_80211_status_indication * indication,int len)2962 static void rndis_wlan_pmkid_cand_list_indication(struct usbnet *usbdev,
2963 				struct ndis_80211_status_indication *indication,
2964 				int len)
2965 {
2966 	struct ndis_80211_pmkid_cand_list *cand_list;
2967 	int list_len, expected_len, i;
2968 
2969 	if (len < offsetof(struct ndis_80211_status_indication, u) +
2970 				sizeof(struct ndis_80211_pmkid_cand_list)) {
2971 		netdev_info(usbdev->net, "pmkid candidate list indication: too short message (%i)\n",
2972 			    len);
2973 		return;
2974 	}
2975 
2976 	list_len = le32_to_cpu(indication->u.cand_list.num_candidates) *
2977 			sizeof(struct ndis_80211_pmkid_candidate);
2978 	expected_len = sizeof(struct ndis_80211_pmkid_cand_list) + list_len +
2979 			offsetof(struct ndis_80211_status_indication, u);
2980 
2981 	if (len < expected_len) {
2982 		netdev_info(usbdev->net, "pmkid candidate list indication: list larger than buffer (%i < %i)\n",
2983 			    len, expected_len);
2984 		return;
2985 	}
2986 
2987 	cand_list = &indication->u.cand_list;
2988 
2989 	netdev_info(usbdev->net, "pmkid candidate list indication: version %i, candidates %i\n",
2990 		    le32_to_cpu(cand_list->version),
2991 		    le32_to_cpu(cand_list->num_candidates));
2992 
2993 	if (le32_to_cpu(cand_list->version) != 1)
2994 		return;
2995 
2996 	for (i = 0; i < le32_to_cpu(cand_list->num_candidates); i++) {
2997 		struct ndis_80211_pmkid_candidate *cand =
2998 						&cand_list->candidate_list[i];
2999 		bool preauth = !!(cand->flags & NDIS_80211_PMKID_CAND_PREAUTH);
3000 
3001 		netdev_dbg(usbdev->net, "cand[%i]: flags: 0x%08x, preauth: %d, bssid: %pM\n",
3002 			   i, le32_to_cpu(cand->flags), preauth, cand->bssid);
3003 
3004 		cfg80211_pmksa_candidate_notify(usbdev->net, i, cand->bssid,
3005 						preauth, GFP_ATOMIC);
3006 	}
3007 }
3008 
rndis_wlan_media_specific_indication(struct usbnet * usbdev,struct rndis_indicate * msg,int buflen)3009 static void rndis_wlan_media_specific_indication(struct usbnet *usbdev,
3010 			struct rndis_indicate *msg, int buflen)
3011 {
3012 	struct ndis_80211_status_indication *indication;
3013 	unsigned int len, offset;
3014 
3015 	offset = offsetof(struct rndis_indicate, status) +
3016 			le32_to_cpu(msg->offset);
3017 	len = le32_to_cpu(msg->length);
3018 
3019 	if (len < 8) {
3020 		netdev_info(usbdev->net, "media specific indication, ignore too short message (%i < 8)\n",
3021 			    len);
3022 		return;
3023 	}
3024 
3025 	if (len > buflen || offset > buflen || offset + len > buflen) {
3026 		netdev_info(usbdev->net, "media specific indication, too large to fit to buffer (%i > %i)\n",
3027 			    offset + len, buflen);
3028 		return;
3029 	}
3030 
3031 	indication = (void *)((u8 *)msg + offset);
3032 
3033 	switch (le32_to_cpu(indication->status_type)) {
3034 	case NDIS_80211_STATUSTYPE_RADIOSTATE:
3035 		netdev_info(usbdev->net, "radio state indication: %i\n",
3036 			    le32_to_cpu(indication->u.radio_status));
3037 		return;
3038 
3039 	case NDIS_80211_STATUSTYPE_MEDIASTREAMMODE:
3040 		netdev_info(usbdev->net, "media stream mode indication: %i\n",
3041 			    le32_to_cpu(indication->u.media_stream_mode));
3042 		return;
3043 
3044 	case NDIS_80211_STATUSTYPE_AUTHENTICATION:
3045 		rndis_wlan_auth_indication(usbdev, indication, len);
3046 		return;
3047 
3048 	case NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST:
3049 		rndis_wlan_pmkid_cand_list_indication(usbdev, indication, len);
3050 		return;
3051 
3052 	default:
3053 		netdev_info(usbdev->net, "media specific indication: unknown status type 0x%08x\n",
3054 			    le32_to_cpu(indication->status_type));
3055 	}
3056 }
3057 
rndis_wlan_indication(struct usbnet * usbdev,void * ind,int buflen)3058 static void rndis_wlan_indication(struct usbnet *usbdev, void *ind, int buflen)
3059 {
3060 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3061 	struct rndis_indicate *msg = ind;
3062 
3063 	switch (le32_to_cpu(msg->status)) {
3064 	case RNDIS_STATUS_MEDIA_CONNECT:
3065 		if (priv->current_command_oid == RNDIS_OID_802_11_ADD_KEY) {
3066 			/* RNDIS_OID_802_11_ADD_KEY causes sometimes extra
3067 			 * "media connect" indications which confuses driver
3068 			 * and userspace to think that device is
3069 			 * roaming/reassociating when it isn't.
3070 			 */
3071 			netdev_dbg(usbdev->net, "ignored RNDIS_OID_802_11_ADD_KEY triggered 'media connect'\n");
3072 			return;
3073 		}
3074 
3075 		usbnet_pause_rx(usbdev);
3076 
3077 		netdev_info(usbdev->net, "media connect\n");
3078 
3079 		/* queue work to avoid recursive calls into rndis_command */
3080 		set_bit(WORK_LINK_UP, &priv->work_pending);
3081 		queue_work(priv->workqueue, &priv->work);
3082 		break;
3083 
3084 	case RNDIS_STATUS_MEDIA_DISCONNECT:
3085 		netdev_info(usbdev->net, "media disconnect\n");
3086 
3087 		/* queue work to avoid recursive calls into rndis_command */
3088 		set_bit(WORK_LINK_DOWN, &priv->work_pending);
3089 		queue_work(priv->workqueue, &priv->work);
3090 		break;
3091 
3092 	case RNDIS_STATUS_MEDIA_SPECIFIC_INDICATION:
3093 		rndis_wlan_media_specific_indication(usbdev, msg, buflen);
3094 		break;
3095 
3096 	default:
3097 		netdev_info(usbdev->net, "indication: 0x%08x\n",
3098 			    le32_to_cpu(msg->status));
3099 		break;
3100 	}
3101 }
3102 
rndis_wlan_get_caps(struct usbnet * usbdev,struct wiphy * wiphy)3103 static int rndis_wlan_get_caps(struct usbnet *usbdev, struct wiphy *wiphy)
3104 {
3105 	struct {
3106 		__le32	num_items;
3107 		__le32	items[8];
3108 	} networks_supported;
3109 	struct ndis_80211_capability caps;
3110 	int len, retval, i, n;
3111 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3112 
3113 	/* determine supported modes */
3114 	len = sizeof(networks_supported);
3115 	retval = rndis_query_oid(usbdev,
3116 				 RNDIS_OID_802_11_NETWORK_TYPES_SUPPORTED,
3117 				 &networks_supported, &len);
3118 	if (!retval) {
3119 		n = le32_to_cpu(networks_supported.num_items);
3120 		if (n > 8)
3121 			n = 8;
3122 		for (i = 0; i < n; i++) {
3123 			switch (le32_to_cpu(networks_supported.items[i])) {
3124 			case NDIS_80211_TYPE_FREQ_HOP:
3125 			case NDIS_80211_TYPE_DIRECT_SEQ:
3126 				priv->caps |= CAP_MODE_80211B;
3127 				break;
3128 			case NDIS_80211_TYPE_OFDM_A:
3129 				priv->caps |= CAP_MODE_80211A;
3130 				break;
3131 			case NDIS_80211_TYPE_OFDM_G:
3132 				priv->caps |= CAP_MODE_80211G;
3133 				break;
3134 			}
3135 		}
3136 	}
3137 
3138 	/* get device 802.11 capabilities, number of PMKIDs */
3139 	len = sizeof(caps);
3140 	retval = rndis_query_oid(usbdev,
3141 				 RNDIS_OID_802_11_CAPABILITY,
3142 				 &caps, &len);
3143 	if (!retval) {
3144 		netdev_dbg(usbdev->net, "RNDIS_OID_802_11_CAPABILITY -> len %d, "
3145 				"ver %d, pmkids %d, auth-encr-pairs %d\n",
3146 				le32_to_cpu(caps.length),
3147 				le32_to_cpu(caps.version),
3148 				le32_to_cpu(caps.num_pmkids),
3149 				le32_to_cpu(caps.num_auth_encr_pair));
3150 		wiphy->max_num_pmkids = le32_to_cpu(caps.num_pmkids);
3151 	} else
3152 		wiphy->max_num_pmkids = 0;
3153 
3154 	return retval;
3155 }
3156 
rndis_do_cqm(struct usbnet * usbdev,s32 rssi)3157 static void rndis_do_cqm(struct usbnet *usbdev, s32 rssi)
3158 {
3159 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3160 	enum nl80211_cqm_rssi_threshold_event event;
3161 	int thold, hyst, last_event;
3162 
3163 	if (priv->cqm_rssi_thold >= 0 || rssi >= 0)
3164 		return;
3165 	if (priv->infra_mode != NDIS_80211_INFRA_INFRA)
3166 		return;
3167 
3168 	last_event = priv->last_cqm_event_rssi;
3169 	thold = priv->cqm_rssi_thold;
3170 	hyst = priv->cqm_rssi_hyst;
3171 
3172 	if (rssi < thold && (last_event == 0 || rssi < last_event - hyst))
3173 		event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
3174 	else if (rssi > thold && (last_event == 0 || rssi > last_event + hyst))
3175 		event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
3176 	else
3177 		return;
3178 
3179 	priv->last_cqm_event_rssi = rssi;
3180 	cfg80211_cqm_rssi_notify(usbdev->net, event, rssi, GFP_KERNEL);
3181 }
3182 
3183 #define DEVICE_POLLER_JIFFIES (HZ)
rndis_device_poller(struct work_struct * work)3184 static void rndis_device_poller(struct work_struct *work)
3185 {
3186 	struct rndis_wlan_private *priv =
3187 		container_of(work, struct rndis_wlan_private,
3188 							dev_poller_work.work);
3189 	struct usbnet *usbdev = priv->usbdev;
3190 	__le32 rssi, tmp;
3191 	int len, ret, j;
3192 	int update_jiffies = DEVICE_POLLER_JIFFIES;
3193 	void *buf;
3194 
3195 	/* Only check/do workaround when connected. Calling is_associated()
3196 	 * also polls device with rndis_command() and catches for media link
3197 	 * indications.
3198 	 */
3199 	if (!is_associated(usbdev)) {
3200 		/* Workaround bad scanning in BCM4320a devices with active
3201 		 * background scanning when not associated.
3202 		 */
3203 		if (priv->device_type == RNDIS_BCM4320A && priv->radio_on &&
3204 		    !priv->scan_request) {
3205 			/* Get previous scan results */
3206 			rndis_check_bssid_list(usbdev, NULL, NULL);
3207 
3208 			/* Initiate new scan */
3209 			rndis_start_bssid_list_scan(usbdev);
3210 		}
3211 
3212 		goto end;
3213 	}
3214 
3215 	len = sizeof(rssi);
3216 	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_RSSI,
3217 			      &rssi, &len);
3218 	if (ret == 0) {
3219 		priv->last_qual = level_to_qual(le32_to_cpu(rssi));
3220 		rndis_do_cqm(usbdev, le32_to_cpu(rssi));
3221 	}
3222 
3223 	netdev_dbg(usbdev->net, "dev-poller: RNDIS_OID_802_11_RSSI -> %d, rssi:%d, qual: %d\n",
3224 		   ret, le32_to_cpu(rssi), level_to_qual(le32_to_cpu(rssi)));
3225 
3226 	/* Workaround transfer stalls on poor quality links.
3227 	 * TODO: find right way to fix these stalls (as stalls do not happen
3228 	 * with ndiswrapper/windows driver). */
3229 	if (priv->param_workaround_interval > 0 && priv->last_qual <= 25) {
3230 		/* Decrease stats worker interval to catch stalls.
3231 		 * faster. Faster than 400-500ms causes packet loss,
3232 		 * Slower doesn't catch stalls fast enough.
3233 		 */
3234 		j = msecs_to_jiffies(priv->param_workaround_interval);
3235 		if (j > DEVICE_POLLER_JIFFIES)
3236 			j = DEVICE_POLLER_JIFFIES;
3237 		else if (j <= 0)
3238 			j = 1;
3239 		update_jiffies = j;
3240 
3241 		/* Send scan OID. Use of both OIDs is required to get device
3242 		 * working.
3243 		 */
3244 		tmp = cpu_to_le32(1);
3245 		rndis_set_oid(usbdev,
3246 			      RNDIS_OID_802_11_BSSID_LIST_SCAN,
3247 			      &tmp, sizeof(tmp));
3248 
3249 		len = CONTROL_BUFFER_SIZE;
3250 		buf = kmalloc(len, GFP_KERNEL);
3251 		if (!buf)
3252 			goto end;
3253 
3254 		rndis_query_oid(usbdev,
3255 				RNDIS_OID_802_11_BSSID_LIST,
3256 				buf, &len);
3257 		kfree(buf);
3258 	}
3259 
3260 end:
3261 	if (update_jiffies >= HZ)
3262 		update_jiffies = round_jiffies_relative(update_jiffies);
3263 	else {
3264 		j = round_jiffies_relative(update_jiffies);
3265 		if (abs(j - update_jiffies) <= 10)
3266 			update_jiffies = j;
3267 	}
3268 
3269 	queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
3270 								update_jiffies);
3271 }
3272 
3273 /*
3274  * driver/device initialization
3275  */
rndis_copy_module_params(struct usbnet * usbdev,int device_type)3276 static void rndis_copy_module_params(struct usbnet *usbdev, int device_type)
3277 {
3278 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3279 
3280 	priv->device_type = device_type;
3281 
3282 	priv->param_country[0] = modparam_country[0];
3283 	priv->param_country[1] = modparam_country[1];
3284 	priv->param_country[2] = 0;
3285 	priv->param_frameburst   = modparam_frameburst;
3286 	priv->param_afterburner  = modparam_afterburner;
3287 	priv->param_power_save   = modparam_power_save;
3288 	priv->param_power_output = modparam_power_output;
3289 	priv->param_roamtrigger  = modparam_roamtrigger;
3290 	priv->param_roamdelta    = modparam_roamdelta;
3291 
3292 	priv->param_country[0] = toupper(priv->param_country[0]);
3293 	priv->param_country[1] = toupper(priv->param_country[1]);
3294 	/* doesn't support EU as country code, use FI instead */
3295 	if (!strcmp(priv->param_country, "EU"))
3296 		strcpy(priv->param_country, "FI");
3297 
3298 	if (priv->param_power_save < 0)
3299 		priv->param_power_save = 0;
3300 	else if (priv->param_power_save > 2)
3301 		priv->param_power_save = 2;
3302 
3303 	if (priv->param_power_output < 0)
3304 		priv->param_power_output = 0;
3305 	else if (priv->param_power_output > 3)
3306 		priv->param_power_output = 3;
3307 
3308 	if (priv->param_roamtrigger < -80)
3309 		priv->param_roamtrigger = -80;
3310 	else if (priv->param_roamtrigger > -60)
3311 		priv->param_roamtrigger = -60;
3312 
3313 	if (priv->param_roamdelta < 0)
3314 		priv->param_roamdelta = 0;
3315 	else if (priv->param_roamdelta > 2)
3316 		priv->param_roamdelta = 2;
3317 
3318 	if (modparam_workaround_interval < 0)
3319 		priv->param_workaround_interval = 500;
3320 	else
3321 		priv->param_workaround_interval = modparam_workaround_interval;
3322 }
3323 
unknown_early_init(struct usbnet * usbdev)3324 static int unknown_early_init(struct usbnet *usbdev)
3325 {
3326 	/* copy module parameters for unknown so that iwconfig reports txpower
3327 	 * and workaround parameter is copied to private structure correctly.
3328 	 */
3329 	rndis_copy_module_params(usbdev, RNDIS_UNKNOWN);
3330 
3331 	/* This is unknown device, so do not try set configuration parameters.
3332 	 */
3333 
3334 	return 0;
3335 }
3336 
bcm4320a_early_init(struct usbnet * usbdev)3337 static int bcm4320a_early_init(struct usbnet *usbdev)
3338 {
3339 	/* copy module parameters for bcm4320a so that iwconfig reports txpower
3340 	 * and workaround parameter is copied to private structure correctly.
3341 	 */
3342 	rndis_copy_module_params(usbdev, RNDIS_BCM4320A);
3343 
3344 	/* bcm4320a doesn't handle configuration parameters well. Try
3345 	 * set any and you get partially zeroed mac and broken device.
3346 	 */
3347 
3348 	return 0;
3349 }
3350 
bcm4320b_early_init(struct usbnet * usbdev)3351 static int bcm4320b_early_init(struct usbnet *usbdev)
3352 {
3353 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3354 	char buf[8];
3355 
3356 	rndis_copy_module_params(usbdev, RNDIS_BCM4320B);
3357 
3358 	/* Early initialization settings, setting these won't have effect
3359 	 * if called after generic_rndis_bind().
3360 	 */
3361 
3362 	rndis_set_config_parameter_str(usbdev, "Country", priv->param_country);
3363 	rndis_set_config_parameter_str(usbdev, "FrameBursting",
3364 					priv->param_frameburst ? "1" : "0");
3365 	rndis_set_config_parameter_str(usbdev, "Afterburner",
3366 					priv->param_afterburner ? "1" : "0");
3367 	sprintf(buf, "%d", priv->param_power_save);
3368 	rndis_set_config_parameter_str(usbdev, "PowerSaveMode", buf);
3369 	sprintf(buf, "%d", priv->param_power_output);
3370 	rndis_set_config_parameter_str(usbdev, "PwrOut", buf);
3371 	sprintf(buf, "%d", priv->param_roamtrigger);
3372 	rndis_set_config_parameter_str(usbdev, "RoamTrigger", buf);
3373 	sprintf(buf, "%d", priv->param_roamdelta);
3374 	rndis_set_config_parameter_str(usbdev, "RoamDelta", buf);
3375 
3376 	return 0;
3377 }
3378 
3379 /* same as rndis_netdev_ops but with local multicast handler */
3380 static const struct net_device_ops rndis_wlan_netdev_ops = {
3381 	.ndo_open		= usbnet_open,
3382 	.ndo_stop		= usbnet_stop,
3383 	.ndo_start_xmit		= usbnet_start_xmit,
3384 	.ndo_tx_timeout		= usbnet_tx_timeout,
3385 	.ndo_get_stats64	= dev_get_tstats64,
3386 	.ndo_set_mac_address 	= eth_mac_addr,
3387 	.ndo_validate_addr	= eth_validate_addr,
3388 	.ndo_set_rx_mode	= rndis_wlan_set_multicast_list,
3389 };
3390 
rndis_wlan_bind(struct usbnet * usbdev,struct usb_interface * intf)3391 static int rndis_wlan_bind(struct usbnet *usbdev, struct usb_interface *intf)
3392 {
3393 	struct wiphy *wiphy;
3394 	struct rndis_wlan_private *priv;
3395 	int retval, len;
3396 	__le32 tmp;
3397 
3398 	/* allocate wiphy and rndis private data
3399 	 * NOTE: We only support a single virtual interface, so wiphy
3400 	 * and wireless_dev are somewhat synonymous for this device.
3401 	 */
3402 	wiphy = wiphy_new(&rndis_config_ops, sizeof(struct rndis_wlan_private));
3403 	if (!wiphy)
3404 		return -ENOMEM;
3405 
3406 	priv = wiphy_priv(wiphy);
3407 	usbdev->net->ieee80211_ptr = &priv->wdev;
3408 	priv->wdev.wiphy = wiphy;
3409 	priv->wdev.iftype = NL80211_IFTYPE_STATION;
3410 
3411 	/* These have to be initialized before calling generic_rndis_bind().
3412 	 * Otherwise we'll be in big trouble in rndis_wlan_early_init().
3413 	 */
3414 	usbdev->driver_priv = priv;
3415 	priv->usbdev = usbdev;
3416 
3417 	mutex_init(&priv->command_lock);
3418 
3419 	/* because rndis_command() sleeps we need to use workqueue */
3420 	priv->workqueue = create_singlethread_workqueue("rndis_wlan");
3421 	if (!priv->workqueue) {
3422 		wiphy_free(wiphy);
3423 		return -ENOMEM;
3424 	}
3425 	INIT_WORK(&priv->work, rndis_wlan_worker);
3426 	INIT_DELAYED_WORK(&priv->dev_poller_work, rndis_device_poller);
3427 	INIT_DELAYED_WORK(&priv->scan_work, rndis_get_scan_results);
3428 
3429 	/* try bind rndis_host */
3430 	retval = generic_rndis_bind(usbdev, intf, FLAG_RNDIS_PHYM_WIRELESS);
3431 	if (retval < 0)
3432 		goto fail;
3433 
3434 	/* generic_rndis_bind set packet filter to multicast_all+
3435 	 * promisc mode which doesn't work well for our devices (device
3436 	 * picks up rssi to closest station instead of to access point).
3437 	 *
3438 	 * rndis_host wants to avoid all OID as much as possible
3439 	 * so do promisc/multicast handling in rndis_wlan.
3440 	 */
3441 	usbdev->net->netdev_ops = &rndis_wlan_netdev_ops;
3442 
3443 	tmp = cpu_to_le32(RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_BROADCAST);
3444 	retval = rndis_set_oid(usbdev,
3445 			       RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
3446 			       &tmp, sizeof(tmp));
3447 
3448 	len = sizeof(tmp);
3449 	retval = rndis_query_oid(usbdev,
3450 				 RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
3451 				 &tmp, &len);
3452 	priv->multicast_size = le32_to_cpu(tmp);
3453 	if (retval < 0 || priv->multicast_size < 0)
3454 		priv->multicast_size = 0;
3455 	if (priv->multicast_size > 0)
3456 		usbdev->net->flags |= IFF_MULTICAST;
3457 	else
3458 		usbdev->net->flags &= ~IFF_MULTICAST;
3459 
3460 	/* fill-out wiphy structure and register w/ cfg80211 */
3461 	memcpy(wiphy->perm_addr, usbdev->net->dev_addr, ETH_ALEN);
3462 	wiphy->privid = rndis_wiphy_privid;
3463 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION)
3464 					| BIT(NL80211_IFTYPE_ADHOC);
3465 	wiphy->max_scan_ssids = 1;
3466 
3467 	/* TODO: fill-out band/encr information based on priv->caps */
3468 	rndis_wlan_get_caps(usbdev, wiphy);
3469 
3470 	memcpy(priv->channels, rndis_channels, sizeof(rndis_channels));
3471 	memcpy(priv->rates, rndis_rates, sizeof(rndis_rates));
3472 	priv->band.channels = priv->channels;
3473 	priv->band.n_channels = ARRAY_SIZE(rndis_channels);
3474 	priv->band.bitrates = priv->rates;
3475 	priv->band.n_bitrates = ARRAY_SIZE(rndis_rates);
3476 	wiphy->bands[NL80211_BAND_2GHZ] = &priv->band;
3477 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
3478 
3479 	memcpy(priv->cipher_suites, rndis_cipher_suites,
3480 						sizeof(rndis_cipher_suites));
3481 	wiphy->cipher_suites = priv->cipher_suites;
3482 	wiphy->n_cipher_suites = ARRAY_SIZE(rndis_cipher_suites);
3483 
3484 	set_wiphy_dev(wiphy, &usbdev->udev->dev);
3485 
3486 	if (wiphy_register(wiphy)) {
3487 		retval = -ENODEV;
3488 		goto fail;
3489 	}
3490 
3491 	set_default_iw_params(usbdev);
3492 
3493 	priv->power_mode = -1;
3494 
3495 	/* set default rts/frag */
3496 	rndis_set_wiphy_params(wiphy,
3497 			WIPHY_PARAM_FRAG_THRESHOLD | WIPHY_PARAM_RTS_THRESHOLD);
3498 
3499 	/* turn radio off on init */
3500 	priv->radio_on = false;
3501 	disassociate(usbdev, false);
3502 	netif_carrier_off(usbdev->net);
3503 
3504 	return 0;
3505 
3506 fail:
3507 	cancel_delayed_work_sync(&priv->dev_poller_work);
3508 	cancel_delayed_work_sync(&priv->scan_work);
3509 	cancel_work_sync(&priv->work);
3510 	destroy_workqueue(priv->workqueue);
3511 
3512 	wiphy_free(wiphy);
3513 	return retval;
3514 }
3515 
rndis_wlan_unbind(struct usbnet * usbdev,struct usb_interface * intf)3516 static void rndis_wlan_unbind(struct usbnet *usbdev, struct usb_interface *intf)
3517 {
3518 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3519 
3520 	/* turn radio off */
3521 	disassociate(usbdev, false);
3522 
3523 	cancel_delayed_work_sync(&priv->dev_poller_work);
3524 	cancel_delayed_work_sync(&priv->scan_work);
3525 	cancel_work_sync(&priv->work);
3526 	destroy_workqueue(priv->workqueue);
3527 
3528 	rndis_unbind(usbdev, intf);
3529 
3530 	wiphy_unregister(priv->wdev.wiphy);
3531 	wiphy_free(priv->wdev.wiphy);
3532 }
3533 
rndis_wlan_reset(struct usbnet * usbdev)3534 static int rndis_wlan_reset(struct usbnet *usbdev)
3535 {
3536 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3537 	int retval;
3538 
3539 	netdev_dbg(usbdev->net, "%s()\n", __func__);
3540 
3541 	retval = rndis_reset(usbdev);
3542 	if (retval)
3543 		netdev_warn(usbdev->net, "rndis_reset failed: %d\n", retval);
3544 
3545 	/* rndis_reset cleared multicast list, so restore here.
3546 	   (set_multicast_list() also turns on current packet filter) */
3547 	set_multicast_list(usbdev);
3548 
3549 	queue_delayed_work(priv->workqueue, &priv->dev_poller_work,
3550 		round_jiffies_relative(DEVICE_POLLER_JIFFIES));
3551 
3552 	return deauthenticate(usbdev);
3553 }
3554 
rndis_wlan_stop(struct usbnet * usbdev)3555 static int rndis_wlan_stop(struct usbnet *usbdev)
3556 {
3557 	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
3558 	int retval;
3559 	__le32 filter;
3560 
3561 	netdev_dbg(usbdev->net, "%s()\n", __func__);
3562 
3563 	retval = disassociate(usbdev, false);
3564 
3565 	priv->work_pending = 0;
3566 	cancel_delayed_work_sync(&priv->dev_poller_work);
3567 	cancel_delayed_work_sync(&priv->scan_work);
3568 	cancel_work_sync(&priv->work);
3569 	flush_workqueue(priv->workqueue);
3570 
3571 	if (priv->scan_request) {
3572 		struct cfg80211_scan_info info = {
3573 			.aborted = true,
3574 		};
3575 
3576 		cfg80211_scan_done(priv->scan_request, &info);
3577 		priv->scan_request = NULL;
3578 	}
3579 
3580 	/* Set current packet filter zero to block receiving data packets from
3581 	   device. */
3582 	filter = 0;
3583 	rndis_set_oid(usbdev, RNDIS_OID_GEN_CURRENT_PACKET_FILTER, &filter,
3584 								sizeof(filter));
3585 
3586 	return retval;
3587 }
3588 
3589 static const struct driver_info	bcm4320b_info = {
3590 	.description =	"Wireless RNDIS device, BCM4320b based",
3591 	.flags =	FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3592 				FLAG_AVOID_UNLINK_URBS,
3593 	.bind =		rndis_wlan_bind,
3594 	.unbind =	rndis_wlan_unbind,
3595 	.status =	rndis_status,
3596 	.rx_fixup =	rndis_rx_fixup,
3597 	.tx_fixup =	rndis_tx_fixup,
3598 	.reset =	rndis_wlan_reset,
3599 	.stop =		rndis_wlan_stop,
3600 	.early_init =	bcm4320b_early_init,
3601 	.indication =	rndis_wlan_indication,
3602 };
3603 
3604 static const struct driver_info	bcm4320a_info = {
3605 	.description =	"Wireless RNDIS device, BCM4320a based",
3606 	.flags =	FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3607 				FLAG_AVOID_UNLINK_URBS,
3608 	.bind =		rndis_wlan_bind,
3609 	.unbind =	rndis_wlan_unbind,
3610 	.status =	rndis_status,
3611 	.rx_fixup =	rndis_rx_fixup,
3612 	.tx_fixup =	rndis_tx_fixup,
3613 	.reset =	rndis_wlan_reset,
3614 	.stop =		rndis_wlan_stop,
3615 	.early_init =	bcm4320a_early_init,
3616 	.indication =	rndis_wlan_indication,
3617 };
3618 
3619 static const struct driver_info rndis_wlan_info = {
3620 	.description =	"Wireless RNDIS device",
3621 	.flags =	FLAG_WLAN | FLAG_FRAMING_RN | FLAG_NO_SETINT |
3622 				FLAG_AVOID_UNLINK_URBS,
3623 	.bind =		rndis_wlan_bind,
3624 	.unbind =	rndis_wlan_unbind,
3625 	.status =	rndis_status,
3626 	.rx_fixup =	rndis_rx_fixup,
3627 	.tx_fixup =	rndis_tx_fixup,
3628 	.reset =	rndis_wlan_reset,
3629 	.stop =		rndis_wlan_stop,
3630 	.early_init =	unknown_early_init,
3631 	.indication =	rndis_wlan_indication,
3632 };
3633 
3634 /*-------------------------------------------------------------------------*/
3635 
3636 static const struct usb_device_id products [] = {
3637 #define	RNDIS_MASTER_INTERFACE \
3638 	.bInterfaceClass	= USB_CLASS_COMM, \
3639 	.bInterfaceSubClass	= 2 /* ACM */, \
3640 	.bInterfaceProtocol	= 0x0ff
3641 
3642 /* INF driver for these devices have DriverVer >= 4.xx.xx.xx and many custom
3643  * parameters available. Chipset marked as 'BCM4320SKFBG' in NDISwrapper-wiki.
3644  */
3645 {
3646 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3647 			  | USB_DEVICE_ID_MATCH_DEVICE,
3648 	.idVendor		= 0x0411,
3649 	.idProduct		= 0x00bc,	/* Buffalo WLI-U2-KG125S */
3650 	RNDIS_MASTER_INTERFACE,
3651 	.driver_info		= (unsigned long) &bcm4320b_info,
3652 }, {
3653 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3654 			  | USB_DEVICE_ID_MATCH_DEVICE,
3655 	.idVendor		= 0x0baf,
3656 	.idProduct		= 0x011b,	/* U.S. Robotics USR5421 */
3657 	RNDIS_MASTER_INTERFACE,
3658 	.driver_info		= (unsigned long) &bcm4320b_info,
3659 }, {
3660 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3661 			  | USB_DEVICE_ID_MATCH_DEVICE,
3662 	.idVendor		= 0x050d,
3663 	.idProduct		= 0x011b,	/* Belkin F5D7051 */
3664 	RNDIS_MASTER_INTERFACE,
3665 	.driver_info		= (unsigned long) &bcm4320b_info,
3666 }, {
3667 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3668 			  | USB_DEVICE_ID_MATCH_DEVICE,
3669 	.idVendor		= 0x1799,	/* Belkin has two vendor ids */
3670 	.idProduct		= 0x011b,	/* Belkin F5D7051 */
3671 	RNDIS_MASTER_INTERFACE,
3672 	.driver_info		= (unsigned long) &bcm4320b_info,
3673 }, {
3674 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3675 			  | USB_DEVICE_ID_MATCH_DEVICE,
3676 	.idVendor		= 0x13b1,
3677 	.idProduct		= 0x0014,	/* Linksys WUSB54GSv2 */
3678 	RNDIS_MASTER_INTERFACE,
3679 	.driver_info		= (unsigned long) &bcm4320b_info,
3680 }, {
3681 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3682 			  | USB_DEVICE_ID_MATCH_DEVICE,
3683 	.idVendor		= 0x13b1,
3684 	.idProduct		= 0x0026,	/* Linksys WUSB54GSC */
3685 	RNDIS_MASTER_INTERFACE,
3686 	.driver_info		= (unsigned long) &bcm4320b_info,
3687 }, {
3688 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3689 			  | USB_DEVICE_ID_MATCH_DEVICE,
3690 	.idVendor		= 0x0b05,
3691 	.idProduct		= 0x1717,	/* Asus WL169gE */
3692 	RNDIS_MASTER_INTERFACE,
3693 	.driver_info		= (unsigned long) &bcm4320b_info,
3694 }, {
3695 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3696 			  | USB_DEVICE_ID_MATCH_DEVICE,
3697 	.idVendor		= 0x0a5c,
3698 	.idProduct		= 0xd11b,	/* Eminent EM4045 */
3699 	RNDIS_MASTER_INTERFACE,
3700 	.driver_info		= (unsigned long) &bcm4320b_info,
3701 }, {
3702 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3703 			  | USB_DEVICE_ID_MATCH_DEVICE,
3704 	.idVendor		= 0x1690,
3705 	.idProduct		= 0x0715,	/* BT Voyager 1055 */
3706 	RNDIS_MASTER_INTERFACE,
3707 	.driver_info		= (unsigned long) &bcm4320b_info,
3708 },
3709 /* These devices have DriverVer < 4.xx.xx.xx and do not have any custom
3710  * parameters available, hardware probably contain older firmware version with
3711  * no way of updating. Chipset marked as 'BCM4320????' in NDISwrapper-wiki.
3712  */
3713 {
3714 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3715 			  | USB_DEVICE_ID_MATCH_DEVICE,
3716 	.idVendor		= 0x13b1,
3717 	.idProduct		= 0x000e,	/* Linksys WUSB54GSv1 */
3718 	RNDIS_MASTER_INTERFACE,
3719 	.driver_info		= (unsigned long) &bcm4320a_info,
3720 }, {
3721 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3722 			  | USB_DEVICE_ID_MATCH_DEVICE,
3723 	.idVendor		= 0x0baf,
3724 	.idProduct		= 0x0111,	/* U.S. Robotics USR5420 */
3725 	RNDIS_MASTER_INTERFACE,
3726 	.driver_info		= (unsigned long) &bcm4320a_info,
3727 }, {
3728 	.match_flags	=   USB_DEVICE_ID_MATCH_INT_INFO
3729 			  | USB_DEVICE_ID_MATCH_DEVICE,
3730 	.idVendor		= 0x0411,
3731 	.idProduct		= 0x004b,	/* BUFFALO WLI-USB-G54 */
3732 	RNDIS_MASTER_INTERFACE,
3733 	.driver_info		= (unsigned long) &bcm4320a_info,
3734 },
3735 /* Generic Wireless RNDIS devices that we don't have exact
3736  * idVendor/idProduct/chip yet.
3737  */
3738 {
3739 	/* RNDIS is MSFT's un-official variant of CDC ACM */
3740 	USB_INTERFACE_INFO(USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
3741 	.driver_info = (unsigned long) &rndis_wlan_info,
3742 }, {
3743 	/* "ActiveSync" is an undocumented variant of RNDIS, used in WM5 */
3744 	USB_INTERFACE_INFO(USB_CLASS_MISC, 1, 1),
3745 	.driver_info = (unsigned long) &rndis_wlan_info,
3746 },
3747 	{ },		// END
3748 };
3749 MODULE_DEVICE_TABLE(usb, products);
3750 
3751 static struct usb_driver rndis_wlan_driver = {
3752 	.name =		"rndis_wlan",
3753 	.id_table =	products,
3754 	.probe =	usbnet_probe,
3755 	.disconnect =	usbnet_disconnect,
3756 	.suspend =	usbnet_suspend,
3757 	.resume =	usbnet_resume,
3758 	.disable_hub_initiated_lpm = 1,
3759 };
3760 
3761 module_usb_driver(rndis_wlan_driver);
3762 
3763 MODULE_AUTHOR("Bjorge Dijkstra");
3764 MODULE_AUTHOR("Jussi Kivilinna");
3765 MODULE_DESCRIPTION("Driver for RNDIS based USB Wireless adapters");
3766 MODULE_LICENSE("GPL");
3767 
3768