PageRenderTime 149ms CodeModel.GetById 17ms app.highlight 116ms RepoModel.GetById 2ms app.codeStats 1ms

/drivers/net/wireless/rndis_wlan.c

http://github.com/mirrors/linux
C | 3774 lines | 2891 code | 650 blank | 233 comment | 404 complexity | 2bd64ccdafb1f4dfc0b593a998f806cc MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   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 */
  36static char modparam_country[4] = "EU";
  37module_param_string(country, modparam_country, 4, 0444);
  38MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
  39
  40static int modparam_frameburst = 1;
  41module_param_named(frameburst, modparam_frameburst, int, 0444);
  42MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
  43
  44static int modparam_afterburner = 0;
  45module_param_named(afterburner, modparam_afterburner, int, 0444);
  46MODULE_PARM_DESC(afterburner,
  47	"enable afterburner aka '125 High Speed Mode' (default: off)");
  48
  49static int modparam_power_save = 0;
  50module_param_named(power_save, modparam_power_save, int, 0444);
  51MODULE_PARM_DESC(power_save,
  52	"set power save mode: 0=off, 1=on, 2=fast (default: off)");
  53
  54static int modparam_power_output = 3;
  55module_param_named(power_output, modparam_power_output, int, 0444);
  56MODULE_PARM_DESC(power_output,
  57	"set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
  58
  59static int modparam_roamtrigger = -70;
  60module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
  61MODULE_PARM_DESC(roamtrigger,
  62	"set roaming dBm trigger: -80=optimize for distance, "
  63				"-60=bandwidth (default: -70)");
  64
  65static int modparam_roamdelta = 1;
  66module_param_named(roamdelta, modparam_roamdelta, int, 0444);
  67MODULE_PARM_DESC(roamdelta,
  68	"set roaming tendency: 0=aggressive, 1=moderate, "
  69				"2=conservative (default: moderate)");
  70
  71static int modparam_workaround_interval;
  72module_param_named(workaround_interval, modparam_workaround_interval,
  73							int, 0444);
  74MODULE_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
 108enum 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
 115enum ndis_80211_net_infra {
 116	NDIS_80211_INFRA_ADHOC,
 117	NDIS_80211_INFRA_INFRA,
 118	NDIS_80211_INFRA_AUTO_UNKNOWN
 119};
 120
 121enum 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
 132enum 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
 143enum ndis_80211_priv_filter {
 144	NDIS_80211_PRIV_ACCEPT_ALL,
 145	NDIS_80211_PRIV_8021X_WEP
 146};
 147
 148enum 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
 155enum ndis_80211_media_stream_mode {
 156	NDIS_80211_MEDIA_STREAM_OFF,
 157	NDIS_80211_MEDIA_STREAM_ON
 158};
 159
 160enum 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
 166enum 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
 173enum 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
 178enum 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
 184enum ndis_80211_pmkid_cand_list_flag_bits {
 185	NDIS_80211_PMKID_CAND_PREAUTH = cpu_to_le32(1 << 0)
 186};
 187
 188struct ndis_80211_auth_request {
 189	__le32 length;
 190	u8 bssid[ETH_ALEN];
 191	u8 padding[2];
 192	__le32 flags;
 193} __packed;
 194
 195struct ndis_80211_pmkid_candidate {
 196	u8 bssid[ETH_ALEN];
 197	u8 padding[2];
 198	__le32 flags;
 199} __packed;
 200
 201struct ndis_80211_pmkid_cand_list {
 202	__le32 version;
 203	__le32 num_candidates;
 204	struct ndis_80211_pmkid_candidate candidate_list[0];
 205} __packed;
 206
 207struct 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
 217struct ndis_80211_ssid {
 218	__le32 length;
 219	u8 essid[NDIS_802_11_LENGTH_SSID];
 220} __packed;
 221
 222struct ndis_80211_conf_freq_hop {
 223	__le32 length;
 224	__le32 hop_pattern;
 225	__le32 hop_set;
 226	__le32 dwell_time;
 227} __packed;
 228
 229struct 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
 237struct 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[0];
 250} __packed;
 251
 252struct ndis_80211_bssid_list_ex {
 253	__le32 num_items;
 254	struct ndis_80211_bssid_ex bssid[0];
 255} __packed;
 256
 257struct ndis_80211_fixed_ies {
 258	u8 timestamp[8];
 259	__le16 beacon_interval;
 260	__le16 capabilities;
 261} __packed;
 262
 263struct ndis_80211_wep_key {
 264	__le32 size;
 265	__le32 index;
 266	__le32 length;
 267	u8 material[32];
 268} __packed;
 269
 270struct 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
 280struct ndis_80211_remove_key {
 281	__le32 size;
 282	__le32 index;
 283	u8 bssid[ETH_ALEN];
 284	u8 padding[2];
 285} __packed;
 286
 287struct 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
 295struct 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
 315struct ndis_80211_auth_encr_pair {
 316	__le32 auth_mode;
 317	__le32 encr_mode;
 318} __packed;
 319
 320struct ndis_80211_capability {
 321	__le32 length;
 322	__le32 version;
 323	__le32 num_pmkids;
 324	__le32 num_auth_encr_pair;
 325	struct ndis_80211_auth_encr_pair auth_encr_pair[0];
 326} __packed;
 327
 328struct ndis_80211_bssid_info {
 329	u8 bssid[ETH_ALEN];
 330	u8 pmkid[16];
 331} __packed;
 332
 333struct ndis_80211_pmkid {
 334	__le32 length;
 335	__le32 bssid_info_count;
 336	struct ndis_80211_bssid_info bssid_info[0];
 337} __packed;
 338
 339/*
 340 *  private data
 341 */
 342#define CAP_MODE_80211A		1
 343#define CAP_MODE_80211B		2
 344#define CAP_MODE_80211G		4
 345#define CAP_MODE_MASK		7
 346
 347#define WORK_LINK_UP		0
 348#define WORK_LINK_DOWN		1
 349#define WORK_SET_MULTICAST_LIST	2
 350
 351#define RNDIS_WLAN_ALG_NONE	0
 352#define RNDIS_WLAN_ALG_WEP	(1<<0)
 353#define RNDIS_WLAN_ALG_TKIP	(1<<1)
 354#define RNDIS_WLAN_ALG_CCMP	(1<<2)
 355
 356#define RNDIS_WLAN_NUM_KEYS		4
 357#define RNDIS_WLAN_KEY_MGMT_NONE	0
 358#define RNDIS_WLAN_KEY_MGMT_802_1X	(1<<0)
 359#define RNDIS_WLAN_KEY_MGMT_PSK		(1<<1)
 360
 361#define COMMAND_BUFFER_SIZE	(CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
 362
 363static const struct ieee80211_channel rndis_channels[] = {
 364	{ .center_freq = 2412 },
 365	{ .center_freq = 2417 },
 366	{ .center_freq = 2422 },
 367	{ .center_freq = 2427 },
 368	{ .center_freq = 2432 },
 369	{ .center_freq = 2437 },
 370	{ .center_freq = 2442 },
 371	{ .center_freq = 2447 },
 372	{ .center_freq = 2452 },
 373	{ .center_freq = 2457 },
 374	{ .center_freq = 2462 },
 375	{ .center_freq = 2467 },
 376	{ .center_freq = 2472 },
 377	{ .center_freq = 2484 },
 378};
 379
 380static const struct ieee80211_rate rndis_rates[] = {
 381	{ .bitrate = 10 },
 382	{ .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 383	{ .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 384	{ .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 385	{ .bitrate = 60 },
 386	{ .bitrate = 90 },
 387	{ .bitrate = 120 },
 388	{ .bitrate = 180 },
 389	{ .bitrate = 240 },
 390	{ .bitrate = 360 },
 391	{ .bitrate = 480 },
 392	{ .bitrate = 540 }
 393};
 394
 395static const u32 rndis_cipher_suites[] = {
 396	WLAN_CIPHER_SUITE_WEP40,
 397	WLAN_CIPHER_SUITE_WEP104,
 398	WLAN_CIPHER_SUITE_TKIP,
 399	WLAN_CIPHER_SUITE_CCMP,
 400};
 401
 402struct rndis_wlan_encr_key {
 403	int len;
 404	u32 cipher;
 405	u8 material[32];
 406	u8 bssid[ETH_ALEN];
 407	bool pairwise;
 408	bool tx_key;
 409};
 410
 411/* RNDIS device private data */
 412struct rndis_wlan_private {
 413	struct usbnet *usbdev;
 414
 415	struct wireless_dev wdev;
 416
 417	struct cfg80211_scan_request *scan_request;
 418
 419	struct workqueue_struct *workqueue;
 420	struct delayed_work dev_poller_work;
 421	struct delayed_work scan_work;
 422	struct work_struct work;
 423	struct mutex command_lock;
 424	unsigned long work_pending;
 425	int last_qual;
 426	s32 cqm_rssi_thold;
 427	u32 cqm_rssi_hyst;
 428	int last_cqm_event_rssi;
 429
 430	struct ieee80211_supported_band band;
 431	struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
 432	struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
 433	u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
 434
 435	int device_type;
 436	int caps;
 437	int multicast_size;
 438
 439	/* module parameters */
 440	char param_country[4];
 441	int  param_frameburst;
 442	int  param_afterburner;
 443	int  param_power_save;
 444	int  param_power_output;
 445	int  param_roamtrigger;
 446	int  param_roamdelta;
 447	u32  param_workaround_interval;
 448
 449	/* hardware state */
 450	bool radio_on;
 451	int power_mode;
 452	int infra_mode;
 453	bool connected;
 454	u8 bssid[ETH_ALEN];
 455	u32 current_command_oid;
 456
 457	/* encryption stuff */
 458	u8 encr_tx_key_index;
 459	struct rndis_wlan_encr_key encr_keys[RNDIS_WLAN_NUM_KEYS];
 460	int  wpa_version;
 461
 462	u8 command_buffer[COMMAND_BUFFER_SIZE];
 463};
 464
 465/*
 466 * cfg80211 ops
 467 */
 468static int rndis_change_virtual_intf(struct wiphy *wiphy,
 469					struct net_device *dev,
 470					enum nl80211_iftype type,
 471					struct vif_params *params);
 472
 473static int rndis_scan(struct wiphy *wiphy,
 474			struct cfg80211_scan_request *request);
 475
 476static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
 477
 478static int rndis_set_tx_power(struct wiphy *wiphy,
 479			      struct wireless_dev *wdev,
 480			      enum nl80211_tx_power_setting type,
 481			      int mbm);
 482static int rndis_get_tx_power(struct wiphy *wiphy,
 483			      struct wireless_dev *wdev,
 484			      int *dbm);
 485
 486static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
 487				struct cfg80211_connect_params *sme);
 488
 489static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
 490				u16 reason_code);
 491
 492static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
 493					struct cfg80211_ibss_params *params);
 494
 495static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
 496
 497static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
 498			 u8 key_index, bool pairwise, const u8 *mac_addr,
 499			 struct key_params *params);
 500
 501static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
 502			 u8 key_index, bool pairwise, const u8 *mac_addr);
 503
 504static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
 505				 u8 key_index, bool unicast, bool multicast);
 506
 507static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
 508			     const u8 *mac, struct station_info *sinfo);
 509
 510static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
 511			       int idx, u8 *mac, struct station_info *sinfo);
 512
 513static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
 514				struct cfg80211_pmksa *pmksa);
 515
 516static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
 517				struct cfg80211_pmksa *pmksa);
 518
 519static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev);
 520
 521static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
 522				bool enabled, int timeout);
 523
 524static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
 525					struct net_device *dev,
 526					s32 rssi_thold, u32 rssi_hyst);
 527
 528static const struct cfg80211_ops rndis_config_ops = {
 529	.change_virtual_intf = rndis_change_virtual_intf,
 530	.scan = rndis_scan,
 531	.set_wiphy_params = rndis_set_wiphy_params,
 532	.set_tx_power = rndis_set_tx_power,
 533	.get_tx_power = rndis_get_tx_power,
 534	.connect = rndis_connect,
 535	.disconnect = rndis_disconnect,
 536	.join_ibss = rndis_join_ibss,
 537	.leave_ibss = rndis_leave_ibss,
 538	.add_key = rndis_add_key,
 539	.del_key = rndis_del_key,
 540	.set_default_key = rndis_set_default_key,
 541	.get_station = rndis_get_station,
 542	.dump_station = rndis_dump_station,
 543	.set_pmksa = rndis_set_pmksa,
 544	.del_pmksa = rndis_del_pmksa,
 545	.flush_pmksa = rndis_flush_pmksa,
 546	.set_power_mgmt = rndis_set_power_mgmt,
 547	.set_cqm_rssi_config = rndis_set_cqm_rssi_config,
 548};
 549
 550static void *rndis_wiphy_privid = &rndis_wiphy_privid;
 551
 552
 553static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
 554{
 555	return (struct rndis_wlan_private *)dev->driver_priv;
 556}
 557
 558static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
 559{
 560	switch (priv->param_power_output) {
 561	default:
 562	case 3:
 563		return BCM4320_DEFAULT_TXPOWER_DBM_100;
 564	case 2:
 565		return BCM4320_DEFAULT_TXPOWER_DBM_75;
 566	case 1:
 567		return BCM4320_DEFAULT_TXPOWER_DBM_50;
 568	case 0:
 569		return BCM4320_DEFAULT_TXPOWER_DBM_25;
 570	}
 571}
 572
 573static bool is_wpa_key(struct rndis_wlan_private *priv, u8 idx)
 574{
 575	int cipher = priv->encr_keys[idx].cipher;
 576
 577	return (cipher == WLAN_CIPHER_SUITE_CCMP ||
 578		cipher == WLAN_CIPHER_SUITE_TKIP);
 579}
 580
 581static int rndis_cipher_to_alg(u32 cipher)
 582{
 583	switch (cipher) {
 584	default:
 585		return RNDIS_WLAN_ALG_NONE;
 586	case WLAN_CIPHER_SUITE_WEP40:
 587	case WLAN_CIPHER_SUITE_WEP104:
 588		return RNDIS_WLAN_ALG_WEP;
 589	case WLAN_CIPHER_SUITE_TKIP:
 590		return RNDIS_WLAN_ALG_TKIP;
 591	case WLAN_CIPHER_SUITE_CCMP:
 592		return RNDIS_WLAN_ALG_CCMP;
 593	}
 594}
 595
 596static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
 597{
 598	switch (akm_suite) {
 599	default:
 600		return RNDIS_WLAN_KEY_MGMT_NONE;
 601	case WLAN_AKM_SUITE_8021X:
 602		return RNDIS_WLAN_KEY_MGMT_802_1X;
 603	case WLAN_AKM_SUITE_PSK:
 604		return RNDIS_WLAN_KEY_MGMT_PSK;
 605	}
 606}
 607
 608#ifdef DEBUG
 609static const char *oid_to_string(u32 oid)
 610{
 611	switch (oid) {
 612#define OID_STR(oid) case oid: return(#oid)
 613		/* from rndis_host.h */
 614		OID_STR(RNDIS_OID_802_3_PERMANENT_ADDRESS);
 615		OID_STR(RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE);
 616		OID_STR(RNDIS_OID_GEN_CURRENT_PACKET_FILTER);
 617		OID_STR(RNDIS_OID_GEN_PHYSICAL_MEDIUM);
 618
 619		/* from rndis_wlan.c */
 620		OID_STR(RNDIS_OID_GEN_LINK_SPEED);
 621		OID_STR(RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER);
 622
 623		OID_STR(RNDIS_OID_GEN_XMIT_OK);
 624		OID_STR(RNDIS_OID_GEN_RCV_OK);
 625		OID_STR(RNDIS_OID_GEN_XMIT_ERROR);
 626		OID_STR(RNDIS_OID_GEN_RCV_ERROR);
 627		OID_STR(RNDIS_OID_GEN_RCV_NO_BUFFER);
 628
 629		OID_STR(RNDIS_OID_802_3_CURRENT_ADDRESS);
 630		OID_STR(RNDIS_OID_802_3_MULTICAST_LIST);
 631		OID_STR(RNDIS_OID_802_3_MAXIMUM_LIST_SIZE);
 632
 633		OID_STR(RNDIS_OID_802_11_BSSID);
 634		OID_STR(RNDIS_OID_802_11_SSID);
 635		OID_STR(RNDIS_OID_802_11_INFRASTRUCTURE_MODE);
 636		OID_STR(RNDIS_OID_802_11_ADD_WEP);
 637		OID_STR(RNDIS_OID_802_11_REMOVE_WEP);
 638		OID_STR(RNDIS_OID_802_11_DISASSOCIATE);
 639		OID_STR(RNDIS_OID_802_11_AUTHENTICATION_MODE);
 640		OID_STR(RNDIS_OID_802_11_PRIVACY_FILTER);
 641		OID_STR(RNDIS_OID_802_11_BSSID_LIST_SCAN);
 642		OID_STR(RNDIS_OID_802_11_ENCRYPTION_STATUS);
 643		OID_STR(RNDIS_OID_802_11_ADD_KEY);
 644		OID_STR(RNDIS_OID_802_11_REMOVE_KEY);
 645		OID_STR(RNDIS_OID_802_11_ASSOCIATION_INFORMATION);
 646		OID_STR(RNDIS_OID_802_11_CAPABILITY);
 647		OID_STR(RNDIS_OID_802_11_PMKID);
 648		OID_STR(RNDIS_OID_802_11_NETWORK_TYPES_SUPPORTED);
 649		OID_STR(RNDIS_OID_802_11_NETWORK_TYPE_IN_USE);
 650		OID_STR(RNDIS_OID_802_11_TX_POWER_LEVEL);
 651		OID_STR(RNDIS_OID_802_11_RSSI);
 652		OID_STR(RNDIS_OID_802_11_RSSI_TRIGGER);
 653		OID_STR(RNDIS_OID_802_11_FRAGMENTATION_THRESHOLD);
 654		OID_STR(RNDIS_OID_802_11_RTS_THRESHOLD);
 655		OID_STR(RNDIS_OID_802_11_SUPPORTED_RATES);
 656		OID_STR(RNDIS_OID_802_11_CONFIGURATION);
 657		OID_STR(RNDIS_OID_802_11_POWER_MODE);
 658		OID_STR(RNDIS_OID_802_11_BSSID_LIST);
 659#undef OID_STR
 660	}
 661
 662	return "?";
 663}
 664#else
 665static const char *oid_to_string(u32 oid)
 666{
 667	return "?";
 668}
 669#endif
 670
 671/* translate error code */
 672static int rndis_error_status(__le32 rndis_status)
 673{
 674	int ret = -EINVAL;
 675	switch (le32_to_cpu(rndis_status)) {
 676	case RNDIS_STATUS_SUCCESS:
 677		ret = 0;
 678		break;
 679	case RNDIS_STATUS_FAILURE:
 680	case RNDIS_STATUS_INVALID_DATA:
 681		ret = -EINVAL;
 682		break;
 683	case RNDIS_STATUS_NOT_SUPPORTED:
 684		ret = -EOPNOTSUPP;
 685		break;
 686	case RNDIS_STATUS_ADAPTER_NOT_READY:
 687	case RNDIS_STATUS_ADAPTER_NOT_OPEN:
 688		ret = -EBUSY;
 689		break;
 690	}
 691	return ret;
 692}
 693
 694static int rndis_query_oid(struct usbnet *dev, u32 oid, void *data, int *len)
 695{
 696	struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
 697	union {
 698		void			*buf;
 699		struct rndis_msg_hdr	*header;
 700		struct rndis_query	*get;
 701		struct rndis_query_c	*get_c;
 702	} u;
 703	int ret, buflen;
 704	int resplen, respoffs, copylen;
 705
 706	buflen = *len + sizeof(*u.get);
 707	if (buflen < CONTROL_BUFFER_SIZE)
 708		buflen = CONTROL_BUFFER_SIZE;
 709
 710	if (buflen > COMMAND_BUFFER_SIZE) {
 711		u.buf = kmalloc(buflen, GFP_KERNEL);
 712		if (!u.buf)
 713			return -ENOMEM;
 714	} else {
 715		u.buf = priv->command_buffer;
 716	}
 717
 718	mutex_lock(&priv->command_lock);
 719
 720	memset(u.get, 0, sizeof *u.get);
 721	u.get->msg_type = cpu_to_le32(RNDIS_MSG_QUERY);
 722	u.get->msg_len = cpu_to_le32(sizeof *u.get);
 723	u.get->oid = cpu_to_le32(oid);
 724
 725	priv->current_command_oid = oid;
 726	ret = rndis_command(dev, u.header, buflen);
 727	priv->current_command_oid = 0;
 728	if (ret < 0)
 729		netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
 730			   __func__, oid_to_string(oid), ret,
 731			   le32_to_cpu(u.get_c->status));
 732
 733	if (ret == 0) {
 734		resplen = le32_to_cpu(u.get_c->len);
 735		respoffs = le32_to_cpu(u.get_c->offset) + 8;
 736
 737		if (respoffs > buflen) {
 738			/* Device returned data offset outside buffer, error. */
 739			netdev_dbg(dev->net, "%s(%s): received invalid "
 740				"data offset: %d > %d\n", __func__,
 741				oid_to_string(oid), respoffs, buflen);
 742
 743			ret = -EINVAL;
 744			goto exit_unlock;
 745		}
 746
 747		if ((resplen + respoffs) > buflen) {
 748			/* Device would have returned more data if buffer would
 749			 * have been big enough. Copy just the bits that we got.
 750			 */
 751			copylen = buflen - respoffs;
 752		} else {
 753			copylen = resplen;
 754		}
 755
 756		if (copylen > *len)
 757			copylen = *len;
 758
 759		memcpy(data, u.buf + respoffs, copylen);
 760
 761		*len = resplen;
 762
 763		ret = rndis_error_status(u.get_c->status);
 764		if (ret < 0)
 765			netdev_dbg(dev->net, "%s(%s): device returned error,  0x%08x (%d)\n",
 766				   __func__, oid_to_string(oid),
 767				   le32_to_cpu(u.get_c->status), ret);
 768	}
 769
 770exit_unlock:
 771	mutex_unlock(&priv->command_lock);
 772
 773	if (u.buf != priv->command_buffer)
 774		kfree(u.buf);
 775	return ret;
 776}
 777
 778static int rndis_set_oid(struct usbnet *dev, u32 oid, const void *data,
 779			 int len)
 780{
 781	struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
 782	union {
 783		void			*buf;
 784		struct rndis_msg_hdr	*header;
 785		struct rndis_set	*set;
 786		struct rndis_set_c	*set_c;
 787	} u;
 788	int ret, buflen;
 789
 790	buflen = len + sizeof(*u.set);
 791	if (buflen < CONTROL_BUFFER_SIZE)
 792		buflen = CONTROL_BUFFER_SIZE;
 793
 794	if (buflen > COMMAND_BUFFER_SIZE) {
 795		u.buf = kmalloc(buflen, GFP_KERNEL);
 796		if (!u.buf)
 797			return -ENOMEM;
 798	} else {
 799		u.buf = priv->command_buffer;
 800	}
 801
 802	mutex_lock(&priv->command_lock);
 803
 804	memset(u.set, 0, sizeof *u.set);
 805	u.set->msg_type = cpu_to_le32(RNDIS_MSG_SET);
 806	u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
 807	u.set->oid = cpu_to_le32(oid);
 808	u.set->len = cpu_to_le32(len);
 809	u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
 810	u.set->handle = cpu_to_le32(0);
 811	memcpy(u.buf + sizeof(*u.set), data, len);
 812
 813	priv->current_command_oid = oid;
 814	ret = rndis_command(dev, u.header, buflen);
 815	priv->current_command_oid = 0;
 816	if (ret < 0)
 817		netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
 818			   __func__, oid_to_string(oid), ret,
 819			   le32_to_cpu(u.set_c->status));
 820
 821	if (ret == 0) {
 822		ret = rndis_error_status(u.set_c->status);
 823
 824		if (ret < 0)
 825			netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
 826				   __func__, oid_to_string(oid),
 827				   le32_to_cpu(u.set_c->status), ret);
 828	}
 829
 830	mutex_unlock(&priv->command_lock);
 831
 832	if (u.buf != priv->command_buffer)
 833		kfree(u.buf);
 834	return ret;
 835}
 836
 837static int rndis_reset(struct usbnet *usbdev)
 838{
 839	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
 840	struct rndis_reset *reset;
 841	int ret;
 842
 843	mutex_lock(&priv->command_lock);
 844
 845	reset = (void *)priv->command_buffer;
 846	memset(reset, 0, sizeof(*reset));
 847	reset->msg_type = cpu_to_le32(RNDIS_MSG_RESET);
 848	reset->msg_len = cpu_to_le32(sizeof(*reset));
 849	priv->current_command_oid = 0;
 850	ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
 851
 852	mutex_unlock(&priv->command_lock);
 853
 854	if (ret < 0)
 855		return ret;
 856	return 0;
 857}
 858
 859/*
 860 * Specs say that we can only set config parameters only soon after device
 861 * initialization.
 862 *   value_type: 0 = u32, 2 = unicode string
 863 */
 864static int rndis_set_config_parameter(struct usbnet *dev, char *param,
 865						int value_type, void *value)
 866{
 867	struct ndis_config_param *infobuf;
 868	int value_len, info_len, param_len, ret, i;
 869	__le16 *unibuf;
 870	__le32 *dst_value;
 871
 872	if (value_type == 0)
 873		value_len = sizeof(__le32);
 874	else if (value_type == 2)
 875		value_len = strlen(value) * sizeof(__le16);
 876	else
 877		return -EINVAL;
 878
 879	param_len = strlen(param) * sizeof(__le16);
 880	info_len = sizeof(*infobuf) + param_len + value_len;
 881
 882#ifdef DEBUG
 883	info_len += 12;
 884#endif
 885	infobuf = kmalloc(info_len, GFP_KERNEL);
 886	if (!infobuf)
 887		return -ENOMEM;
 888
 889#ifdef DEBUG
 890	info_len -= 12;
 891	/* extra 12 bytes are for padding (debug output) */
 892	memset(infobuf, 0xCC, info_len + 12);
 893#endif
 894
 895	if (value_type == 2)
 896		netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
 897			   param, (u8 *)value);
 898	else
 899		netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
 900			   param, *(u32 *)value);
 901
 902	infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
 903	infobuf->name_length = cpu_to_le32(param_len);
 904	infobuf->type = cpu_to_le32(value_type);
 905	infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
 906	infobuf->value_length = cpu_to_le32(value_len);
 907
 908	/* simple string to unicode string conversion */
 909	unibuf = (void *)infobuf + sizeof(*infobuf);
 910	for (i = 0; i < param_len / sizeof(__le16); i++)
 911		unibuf[i] = cpu_to_le16(param[i]);
 912
 913	if (value_type == 2) {
 914		unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
 915		for (i = 0; i < value_len / sizeof(__le16); i++)
 916			unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
 917	} else {
 918		dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
 919		*dst_value = cpu_to_le32(*(u32 *)value);
 920	}
 921
 922#ifdef DEBUG
 923	netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
 924	for (i = 0; i < info_len; i += 12) {
 925		u32 *tmp = (u32 *)((u8 *)infobuf + i);
 926		netdev_dbg(dev->net, "%08X:%08X:%08X\n",
 927			   cpu_to_be32(tmp[0]),
 928			   cpu_to_be32(tmp[1]),
 929			   cpu_to_be32(tmp[2]));
 930	}
 931#endif
 932
 933	ret = rndis_set_oid(dev, RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER,
 934							infobuf, info_len);
 935	if (ret != 0)
 936		netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
 937			   ret);
 938
 939	kfree(infobuf);
 940	return ret;
 941}
 942
 943static int rndis_set_config_parameter_str(struct usbnet *dev,
 944						char *param, char *value)
 945{
 946	return rndis_set_config_parameter(dev, param, 2, value);
 947}
 948
 949/*
 950 * data conversion functions
 951 */
 952static int level_to_qual(int level)
 953{
 954	int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
 955	return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
 956}
 957
 958/*
 959 * common functions
 960 */
 961static int set_infra_mode(struct usbnet *usbdev, int mode);
 962static void restore_keys(struct usbnet *usbdev);
 963static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
 964					bool *matched);
 965
 966static int rndis_start_bssid_list_scan(struct usbnet *usbdev)
 967{
 968	__le32 tmp;
 969
 970	/* Note: RNDIS_OID_802_11_BSSID_LIST_SCAN clears internal BSS list. */
 971	tmp = cpu_to_le32(1);
 972	return rndis_set_oid(usbdev, RNDIS_OID_802_11_BSSID_LIST_SCAN, &tmp,
 973							sizeof(tmp));
 974}
 975
 976static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
 977{
 978	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
 979	int ret;
 980
 981	ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_SSID,
 982			    ssid, sizeof(*ssid));
 983	if (ret < 0) {
 984		netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
 985		return ret;
 986	}
 987	if (ret == 0) {
 988		priv->radio_on = true;
 989		netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
 990	}
 991
 992	return ret;
 993}
 994
 995static int set_bssid(struct usbnet *usbdev, const u8 *bssid)
 996{
 997	int ret;
 998
 999	ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_BSSID,
1000			    bssid, ETH_ALEN);
1001	if (ret < 0) {
1002		netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
1003			    bssid, ret);
1004		return ret;
1005	}
1006
1007	return ret;
1008}
1009
1010static int clear_bssid(struct usbnet *usbdev)
1011{
1012	static const u8 broadcast_mac[ETH_ALEN] = {
1013		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
1014	};
1015
1016	return set_bssid(usbdev, broadcast_mac);
1017}
1018
1019static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
1020{
1021	int ret, len;
1022
1023	len = ETH_ALEN;
1024	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_BSSID,
1025			      bssid, &len);
1026
1027	if (ret != 0)
1028		eth_zero_addr(bssid);
1029
1030	return ret;
1031}
1032
1033static int get_association_info(struct usbnet *usbdev,
1034			struct ndis_80211_assoc_info *info, int len)
1035{
1036	return rndis_query_oid(usbdev,
1037			RNDIS_OID_802_11_ASSOCIATION_INFORMATION,
1038			info, &len);
1039}
1040
1041static bool is_associated(struct usbnet *usbdev)
1042{
1043	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1044	u8 bssid[ETH_ALEN];
1045	int ret;
1046
1047	if (!priv->radio_on)
1048		return false;
1049
1050	ret = get_bssid(usbdev, bssid);
1051
1052	return (ret == 0 && !is_zero_ether_addr(bssid));
1053}
1054
1055static int disassociate(struct usbnet *usbdev, bool reset_ssid)
1056{
1057	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1058	struct ndis_80211_ssid ssid;
1059	int i, ret = 0;
1060
1061	if (priv->radio_on) {
1062		ret = rndis_set_oid(usbdev,
1063				RNDIS_OID_802_11_DISASSOCIATE,
1064				NULL, 0);
1065		if (ret == 0) {
1066			priv->radio_on = false;
1067			netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
1068				   __func__);
1069
1070			if (reset_ssid)
1071				msleep(100);
1072		}
1073	}
1074
1075	/* disassociate causes radio to be turned off; if reset_ssid
1076	 * is given, set random ssid to enable radio */
1077	if (reset_ssid) {
1078		/* Set device to infrastructure mode so we don't get ad-hoc
1079		 * 'media connect' indications with the random ssid.
1080		 */
1081		set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1082
1083		ssid.length = cpu_to_le32(sizeof(ssid.essid));
1084		get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
1085		ssid.essid[0] = 0x1;
1086		ssid.essid[1] = 0xff;
1087		for (i = 2; i < sizeof(ssid.essid); i++)
1088			ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
1089		ret = set_essid(usbdev, &ssid);
1090	}
1091	return ret;
1092}
1093
1094static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
1095				enum nl80211_auth_type auth_type, int keymgmt)
1096{
1097	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1098	__le32 tmp;
1099	int auth_mode, ret;
1100
1101	netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
1102		   __func__, wpa_version, auth_type, keymgmt);
1103
1104	if (wpa_version & NL80211_WPA_VERSION_2) {
1105		if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1106			auth_mode = NDIS_80211_AUTH_WPA2;
1107		else
1108			auth_mode = NDIS_80211_AUTH_WPA2_PSK;
1109	} else if (wpa_version & NL80211_WPA_VERSION_1) {
1110		if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
1111			auth_mode = NDIS_80211_AUTH_WPA;
1112		else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
1113			auth_mode = NDIS_80211_AUTH_WPA_PSK;
1114		else
1115			auth_mode = NDIS_80211_AUTH_WPA_NONE;
1116	} else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
1117		auth_mode = NDIS_80211_AUTH_SHARED;
1118	else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
1119		auth_mode = NDIS_80211_AUTH_OPEN;
1120	else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
1121		auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
1122	else
1123		return -ENOTSUPP;
1124
1125	tmp = cpu_to_le32(auth_mode);
1126	ret = rndis_set_oid(usbdev,
1127			    RNDIS_OID_802_11_AUTHENTICATION_MODE,
1128			    &tmp, sizeof(tmp));
1129	if (ret != 0) {
1130		netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
1131			    ret);
1132		return ret;
1133	}
1134
1135	priv->wpa_version = wpa_version;
1136
1137	return 0;
1138}
1139
1140static int set_priv_filter(struct usbnet *usbdev)
1141{
1142	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1143	__le32 tmp;
1144
1145	netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
1146		   __func__, priv->wpa_version);
1147
1148	if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
1149	    priv->wpa_version & NL80211_WPA_VERSION_1)
1150		tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
1151	else
1152		tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
1153
1154	return rndis_set_oid(usbdev,
1155			     RNDIS_OID_802_11_PRIVACY_FILTER, &tmp,
1156			     sizeof(tmp));
1157}
1158
1159static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
1160{
1161	__le32 tmp;
1162	int encr_mode, ret;
1163
1164	netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
1165		   __func__, pairwise, groupwise);
1166
1167	if (pairwise & RNDIS_WLAN_ALG_CCMP)
1168		encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1169	else if (pairwise & RNDIS_WLAN_ALG_TKIP)
1170		encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1171	else if (pairwise & RNDIS_WLAN_ALG_WEP)
1172		encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
1173	else if (groupwise & RNDIS_WLAN_ALG_CCMP)
1174		encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
1175	else if (groupwise & RNDIS_WLAN_ALG_TKIP)
1176		encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
1177	else
1178		encr_mode = NDIS_80211_ENCR_DISABLED;
1179
1180	tmp = cpu_to_le32(encr_mode);
1181	ret = rndis_set_oid(usbdev,
1182			RNDIS_OID_802_11_ENCRYPTION_STATUS, &tmp,
1183			sizeof(tmp));
1184	if (ret != 0) {
1185		netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
1186			    ret);
1187		return ret;
1188	}
1189
1190	return 0;
1191}
1192
1193static int set_infra_mode(struct usbnet *usbdev, int mode)
1194{
1195	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1196	__le32 tmp;
1197	int ret;
1198
1199	netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
1200		   __func__, priv->infra_mode);
1201
1202	tmp = cpu_to_le32(mode);
1203	ret = rndis_set_oid(usbdev,
1204			    RNDIS_OID_802_11_INFRASTRUCTURE_MODE,
1205			    &tmp, sizeof(tmp));
1206	if (ret != 0) {
1207		netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
1208			    ret);
1209		return ret;
1210	}
1211
1212	/* NDIS drivers clear keys when infrastructure mode is
1213	 * changed. But Linux tools assume otherwise. So set the
1214	 * keys */
1215	restore_keys(usbdev);
1216
1217	priv->infra_mode = mode;
1218	return 0;
1219}
1220
1221static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
1222{
1223	__le32 tmp;
1224
1225	netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
1226
1227	if (rts_threshold == -1 || rts_threshold > 2347)
1228		rts_threshold = 2347;
1229
1230	tmp = cpu_to_le32(rts_threshold);
1231	return rndis_set_oid(usbdev,
1232			     RNDIS_OID_802_11_RTS_THRESHOLD,
1233			     &tmp, sizeof(tmp));
1234}
1235
1236static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
1237{
1238	__le32 tmp;
1239
1240	netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
1241
1242	if (frag_threshold < 256 || frag_threshold > 2346)
1243		frag_threshold = 2346;
1244
1245	tmp = cpu_to_le32(frag_threshold);
1246	return rndis_set_oid(usbdev,
1247			RNDIS_OID_802_11_FRAGMENTATION_THRESHOLD,
1248			&tmp, sizeof(tmp));
1249}
1250
1251static void set_default_iw_params(struct usbnet *usbdev)
1252{
1253	set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
1254	set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
1255						RNDIS_WLAN_KEY_MGMT_NONE);
1256	set_priv_filter(usbdev);
1257	set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1258}
1259
1260static int deauthenticate(struct usbnet *usbdev)
1261{
1262	int ret;
1263
1264	ret = disassociate(usbdev, true);
1265	set_default_iw_params(usbdev);
1266	return ret;
1267}
1268
1269static int set_channel(struct usbnet *usbdev, int channel)
1270{
1271	struct ndis_80211_conf config;
1272	unsigned int dsconfig;
1273	int len, ret;
1274
1275	netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
1276
1277	/* this OID is valid only when not associated */
1278	if (is_associated(usbdev))
1279		return 0;
1280
1281	dsconfig = 1000 *
1282		ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
1283
1284	len = sizeof(config);
1285	ret = rndis_query_oid(usbdev,
1286			RNDIS_OID_802_11_CONFIGURATION,
1287			&config, &len);
1288	if (ret < 0) {
1289		netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
1290			   __func__);
1291		return ret;
1292	}
1293
1294	config.ds_config = cpu_to_le32(dsconfig);
1295	ret = rndis_set_oid(usbdev,
1296			RNDIS_OID_802_11_CONFIGURATION,
1297			&config, sizeof(config));
1298
1299	netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
1300
1301	return ret;
1302}
1303
1304static struct ieee80211_channel *get_current_channel(struct usbnet *usbdev,
1305						     u32 *beacon_period)
1306{
1307	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1308	struct ieee80211_channel *channel;
1309	struct ndis_80211_conf config;
1310	int len, ret;
1311
1312	/* Get channel and beacon interval */
1313	len = sizeof(config);
1314	ret = rndis_query_oid(usbdev,
1315			RNDIS_OID_802_11_CONFIGURATION,
1316			&config, &len);
1317	netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_CONFIGURATION -> %d\n",
1318				__func__, ret);
1319	if (ret < 0)
1320		return NULL;
1321
1322	channel = ieee80211_get_channel(priv->wdev.wiphy,
1323				KHZ_TO_MHZ(le32_to_cpu(config.ds_config)));
1324	if (!channel)
1325		return NULL;
1326
1327	if (beacon_period)
1328		*beacon_period = le32_to_cpu(config.beacon_period);
1329	return channel;
1330}
1331
1332/* index must be 0 - N, as per NDIS  */
1333static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
1334								u8 index)
1335{
1336	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1337	struct ndis_80211_wep_key ndis_key;
1338	u32 cipher;
1339	int ret;
1340
1341	netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
1342		   __func__, index, key_len);
1343
1344	if (index >= RNDIS_WLAN_NUM_KEYS)
1345		return -EINVAL;
1346
1347	if (key_len == 5)
1348		cipher = WLAN_CIPHER_SUITE_WEP40;
1349	else if (key_len == 13)
1350		cipher = WLAN_CIPHER_SUITE_WEP104;
1351	else
1352		return -EINVAL;
1353
1354	memset(&ndis_key, 0, sizeof(ndis_key));
1355
1356	ndis_key.size = cpu_to_le32(sizeof(ndis_key));
1357	ndis_key.length = cpu_to_le32(key_len);
1358	ndis_key.index = cpu_to_le32(index);
1359	memcpy(&ndis_key.material, key, key_len);
1360
1361	if (index == priv->encr_tx_key_index) {
1362		ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
1363		ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
1364							RNDIS_WLAN_ALG_NONE);
1365		if (ret)
1366			netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
1367				    ret);
1368	}
1369
1370	ret = rndis_set_oid(usbdev,
1371			RNDIS_OID_802_11_ADD_WEP, &ndis_key,
1372			sizeof(ndis_key));
1373	if (ret != 0) {
1374		netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
1375			    index + 1, ret);
1376		return ret;
1377	}
1378
1379	priv->encr_keys[index].len = key_len;
1380	priv->encr_keys[index].cipher = cipher;
1381	memcpy(&priv->encr_keys[index].material, key, key_len);
1382	eth_broadcast_addr(priv->encr_keys[index].bssid);
1383
1384	return 0;
1385}
1386
1387static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
1388			u8 index, const u8 *addr, const u8 *rx_seq,
1389			int seq_len, u32 cipher, __le32 flags)
1390{
1391	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1392	struct ndis_80211_key ndis_key;
1393	bool is_addr_ok;
1394	int ret;
1395
1396	if (index >= RNDIS_WLAN_NUM_KEYS) {
1397		netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
1398			   __func__, index);
1399		return -EINVAL;
1400	}
1401	if (key_len > sizeof(ndis_key.material) || key_len < 0) {
1402		netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
1403			   __func__, key_len);
1404		return -EINVAL;
1405	}
1406	if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
1407		if (!rx_seq || seq_len <= 0) {
1408			netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
1409				   __func__);
1410			return -EINVAL;
1411		}
1412		if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
1413			netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
1414			return -EINVAL;
1415		}
1416	}
1417
1418	is_addr_ok = addr && !is_zero_ether_addr(addr) &&
1419					!is_broadcast_ether_addr(addr);
1420	if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
1421		netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
1422			   __func__, addr);
1423		return -EINVAL;
1424	}
1425
1426	netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
1427		   __func__, index,
1428		   !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
1429		   !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
1430		   !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
1431
1432	memset(&ndis_key, 0, sizeof(ndis_key));
1433
1434	ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
1435				sizeof(ndis_key.material) + key_len);
1436	ndis_key.length = cpu_to_le32(key_len);
1437	ndis_key.index = cpu_to_le32(index) | flags;
1438
1439	if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
1440		/* wpa_supplicant gives us the Michael MIC RX/TX keys in
1441		 * different order than NDIS spec, so swap the order here. */
1442		memcpy(ndis_key.material, key, 16);
1443		memcpy(ndis_key.material + 16, key + 24, 8);
1444		memcpy(ndis_key.material + 24, key + 16, 8);
1445	} else
1446		memcpy(ndis_key.material, key, key_len);
1447
1448	if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
1449		memcpy(ndis_key.rsc, rx_seq, seq_len);
1450
1451	if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
1452		/* pairwise key */
1453		memcpy(ndis_key.bssid, addr, ETH_ALEN);
1454	} else {
1455		/* group key */
1456		if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
1457			eth_broadcast_addr(ndis_key.bssid);
1458		else
1459			get_bssid(usbdev, ndis_key.bssid);
1460	}
1461
1462	ret = rndis_set_oid(usbdev,
1463			RNDIS_OID_802_11_ADD_KEY, &ndis_key,
1464			le32_to_cpu(ndis_key.size));
1465	netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_ADD_KEY -> %08X\n",
1466		   __func__, ret);
1467	if (ret != 0)
1468		return ret;
1469
1470	memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
1471	priv->encr_keys[index].len = key_len;
1472	priv->encr_keys[index].cipher = cipher;
1473	memcpy(&priv->encr_keys[index].material, key, key_len);
1474	if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
1475		memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
1476	else
1477		eth_broadcast_addr(priv->encr_keys[index].bssid);
1478
1479	if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
1480		priv->encr_tx_key_index = index;
1481
1482	return 0;
1483}
1484
1485static int restore_key(struct usbnet *usbdev, u8 key_idx)
1486{
1487	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1488	struct rndis_wlan_encr_key key;
1489
1490	if (is_wpa_key(priv, key_idx))
1491		return 0;
1492
1493	key = priv->encr_keys[key_idx];
1494
1495	netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
1496
1497	if (key.len == 0)
1498		return 0;
1499
1500	return add_wep_key(usbdev, key.material, key.len, key_idx);
1501}
1502
1503static void restore_keys(struct usbnet *usbdev)
1504{
1505	int i;
1506
1507	for (i = 0; i < 4; i++)
1508		restore_key(usbdev, i);
1509}
1510
1511static void clear_key(struct rndis_wlan_private *priv, u8 idx)
1512{
1513	memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
1514}
1515
1516/* remove_key is for both wep and wpa */
1517static int remove_key(struct usbnet *usbdev, u8 index, const u8 *bssid)
1518{
1519	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1520	struct ndis_80211_remove_key remove_key;
1521	__le32 keyindex;
1522	bool is_wpa;
1523	int ret;
1524
1525	if (index >= RNDIS_WLAN_NUM_KEYS)
1526		return -ENOENT;
1527
1528	if (priv->encr_keys[index].len == 0)
1529		return 0;
1530
1531	is_wpa = is_wpa_key(priv, index);
1532
1533	netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
1534		   __func__, index, is_wpa ? "wpa" : "wep",
1535		   priv->encr_keys[index].len);
1536
1537	clear_key(priv, index);
1538
1539	if (is_wpa) {
1540		remove_key.size = cpu_to_le32(sizeof(remove_key));
1541		remove_key.index = cpu_to_le32(index);
1542		if (bssid) {
1543			/* pairwise key */
1544			if (!is_broadcast_ether_addr(bssid))
1545				remove_key.index |=
1546					NDIS_80211_ADDKEY_PAIRWISE_KEY;
1547			memcpy(remove_key.bssid, bssid,
1548					sizeof(remove_key.bssid));
1549		} else
1550			memset(remove_key.bssid, 0xff,
1551						sizeof(remove_key.bssid));
1552
1553		ret = rndis_set_oid(usbdev,
1554				RNDIS_OID_802_11_REMOVE_KEY,
1555				&remove_key, sizeof(remove_key));
1556		if (ret != 0)
1557			return ret;
1558	} else {
1559		keyindex = cpu_to_le32(index);
1560		ret = rndis_set_oid(usbdev,
1561				RNDIS_OID_802_11_REMOVE_WEP,
1562				&keyindex, sizeof(keyindex));
1563		if (ret != 0) {
1564			netdev_warn(usbdev->net,
1565				    "removing encryption key %d failed (%08X)\n",
1566				    index, ret);
1567			return ret;
1568		}
1569	}
1570
1571	/* if it is transmit key, disable encryption */
1572	if (index == priv->encr_tx_key_index)
1573		set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
1574
1575	return 0;
1576}
1577
1578static void set_multicast_list(struct usbnet *usbdev)
1579{
1580	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1581	struct netdev_hw_addr *ha;
1582	__le32 filter, basefilter;
1583	int ret;
1584	char *mc_addrs = NULL;
1585	int mc_count;
1586
1587	basefilter = filter = cpu_to_le32(RNDIS_PACKET_TYPE_DIRECTED |
1588					  RNDIS_PACKET_TYPE_BROADCAST);
1589
1590	if (usbdev->net->flags & IFF_PROMISC) {
1591		filter |= cpu_to_le32(RNDIS_PACKET_TYPE_PROMISCUOUS |
1592				      RNDIS_PACKET_TYPE_ALL_LOCAL);
1593	} else if (usbdev->net->flags & IFF_ALLMULTI) {
1594		filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
1595	}
1596
1597	if (filter != basefilter)
1598		goto set_filter;
1599
1600	/*
1601	 * mc_list should be accessed holding the lock, so copy addresses to
1602	 * local buffer first.
1603	 */
1604	netif_addr_lock_bh(usbdev->net);
1605	mc_count = netdev_mc_count(usbdev->net);
1606	if (mc_count > priv->multicast_size) {
1607		filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
1608	} else if (mc_count) {
1609		int i = 0;
1610
1611		mc_addrs = kmalloc_array(mc_count, ETH_ALEN, GFP_ATOMIC);
1612		if (!mc_addrs) {
1613			netif_addr_unlock_bh(usbdev->net);
1614			return;
1615		}
1616
1617		netdev_for_each_mc_addr(ha, usbdev->net)
1618			memcpy(mc_addrs + i++ * ETH_ALEN,
1619			       ha->addr, ETH_ALEN);
1620	}
1621	netif_addr_unlock_bh(usbdev->net);
1622
1623	if (filter != basefilter)
1624		goto set_filter;
1625
1626	if (mc_count) {
1627		ret = rndis_set_oid(usbdev,
1628				RNDIS_OID_802_3_MULTICAST_LIST,
1629				mc_addrs, mc_count * ETH_ALEN);
1630		kfree(mc_addrs);
1631		if (ret == 0)
1632			filter |= cpu_to_le32(RNDIS_PACKET_TYPE_MULTICAST);
1633		else
1634			filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
1635
1636		netdev_dbg(usbdev->net, "RNDIS_OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
1637			   mc_count, priv->multicast_size, ret);
1638	}
1639
1640set_filter:
1641	ret = rndis_set_oid(usbdev, RNDIS_OID_GEN_CURRENT_PACKET_FILTER, &filter,
1642							sizeof(filter));
1643	if (ret < 0) {
1644		netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
1645			    le32_to_cpu(filter));
1646	}
1647
1648	netdev_dbg(usbdev->net, "RNDIS_OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
1649		   le32_to_cpu(filter), ret);
1650}
1651
1652#ifdef DEBUG
1653static void debug_print_pmkids(struct usbnet *usbdev,
1654				struct ndis_80211_pmkid *pmkids,
1655				const char *func_str)
1656{
1657	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1658	int i, len, count, max_pmkids, entry_len;
1659
1660	max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1661	len = le32_to_cpu(pmkids->length);
1662	count = le32_to_cpu(pmkids->bssid_info_count);
1663
1664	entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
1665
1666	netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
1667				"%d)\n", func_str, count, len, entry_len);
1668
1669	if (count > max_pmkids)
1670		count = max_pmkids;
1671
1672	for (i = 0; i < count; i++) {
1673		u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
1674
1675		netdev_dbg(usbdev->net, "%s():  bssid: %pM, "
1676				"pmkid: %08X:%08X:%08X:%08X\n",
1677				func_str, pmkids->bssid_info[i].bssid,
1678				cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
1679				cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
1680	}
1681}
1682#else
1683static void debug_print_pmkids(struct usbnet *usbdev,
1684				struct ndis_80211_pmkid *pmkids,
1685				const char *func_str)
1686{
1687	return;
1688}
1689#endif
1690
1691static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
1692{
1693	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1694	struct ndis_80211_pmkid *pmkids;
1695	int len, ret, max_pmkids;
1696
1697	max_pmkids = priv->wdev.wiphy->max_num_pmkids;
1698	len = struct_size(pmkids, bssid_info, max_pmkids);
1699
1700	pmkids = kzalloc(len, GFP_KERNEL);
1701	if (!pmkids)
1702		return ERR_PTR(-ENOMEM);
1703
1704	pmkids->length = cpu_to_le32(len);
1705	pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1706
1707	ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_PMKID,
1708			pmkids, &len);
1709	if (ret < 0) {
1710		netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d)"
1711				" -> %d\n", __func__, len, max_pmkids, ret);
1712
1713		kfree(pmkids);
1714		return ERR_PTR(ret);
1715	}
1716
1717	if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
1718		pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
1719
1720	debug_print_pmkids(usbdev, pmkids, __func__);
1721
1722	return pmkids;
1723}
1724
1725static int set_device_pmkids(struct usbnet *usbdev,
1726				struct ndis_80211_pmkid *pmkids)
1727{
1728	int ret, len, num_pmkids;
1729
1730	num_pmkids = le32_to_cpu(pmkids->bssid_info_count);
1731	len = struct_size(pmkids, bssid_info, num_pmkids);
1732	pmkids->length = cpu_to_le32(len);
1733
1734	debug_print_pmkids(usbdev, pmkids, __func__);
1735
1736	ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_PMKID, pmkids,
1737			    le32_to_cpu(pmkids->length));
1738	if (ret < 0) {
1739		netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d) -> %d"
1740				"\n", __func__, len, num_pmkids, ret);
1741	}
1742
1743	kfree(pmkids);
1744	return ret;
1745}
1746
1747static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
1748						struct ndis_80211_pmkid *pmkids,
1749						struct cfg80211_pmksa *pmksa,
1750						int max_pmkids)
1751{
1752	int i, err;
1753	unsigned int count;
1754
1755	count = le32_to_cpu(pmkids->bssid_info_count);
1756
1757	if (count > max_pmkids)
1758		count = max_pmkids;
1759
1760	for (i = 0; i < count; i++)
1761		if (ether_addr_equal(pmkids->bssid_info[i].bssid,
1762				     pmksa->bssid))
1763			break;
1764
1765	/* pmkid not found */
1766	if (i == count) {
1767		netdev_dbg(usbdev->net, "%s(): bssid not found (%pM)\n",
1768					__func__, pmksa->bssid);
1769		err = -ENOENT;
1770		goto error;
1771	}
1772
1773	for (; i + 1 < count; i++)
1774		pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
1775
1776	count--;
1777	pmkids->length = cpu_to_le32(struct_size(pmkids, bssid_info, count));
1778	pmkids->bssid_info_count = cpu_to_le32(count);
1779
1780	return pmkids;
1781error:
1782	kfree(pmkids);
1783	return ERR_PTR(err);
1784}
1785
1786static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
1787						struct ndis_80211_pmkid *pmkids,
1788						struct cfg80211_pmksa *pmksa,
1789						int max_pmkids)
1790{
1791	struct ndis_80211_pmkid *new_pmkids;
1792	int i, err, newlen;
1793	unsigned int count;
1794
1795	count = le32_to_cpu(pmkids->bssid_info_count);
1796
1797	if (count > max_pmkids)
1798		count = max_pmkids;
1799
1800	/* update with new pmkid */
1801	for (i = 0; i < count; i++) {
1802		if (!ether_addr_equal(pmkids->bssid_info[i].bssid,
1803				      pmksa->bssid))
1804			continue;
1805
1806		memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
1807								WLAN_PMKID_LEN);
1808
1809		return pmkids;
1810	}
1811
1812	/* out of space, return error */
1813	if (i == max_pmkids) {
1814		netdev_dbg(usbdev->net, "%s(): out of space\n", __func__);
1815		err = -ENOSPC;
1816		goto error;
1817	}
1818
1819	/* add new pmkid */
1820	newlen = struct_size(pmkids, bssid_info, count + 1);
1821
1822	new_pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
1823	if (!new_pmkids) {
1824		err = -ENOMEM;
1825		goto error;
1826	}
1827	pmkids = new_pmkids;
1828
1829	pmkids->length = cpu_to_le32(newlen);
1830	pmkids->bssid_info_count = cpu_to_le32(count + 1);
1831
1832	memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
1833	memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
1834
1835	return pmkids;
1836error:
1837	kfree(pmkids);
1838	return ERR_PTR(err);
1839}
1840
1841/*
1842 * cfg80211 ops
1843 */
1844static int rndis_change_virtual_intf(struct wiphy *wiphy,
1845					struct net_device *dev,
1846					enum nl80211_iftype type,
1847					struct vif_params *params)
1848{
1849	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1850	struct usbnet *usbdev = priv->usbdev;
1851	int mode;
1852
1853	switch (type) {
1854	case NL80211_IFTYPE_ADHOC:
1855		mode = NDIS_80211_INFRA_ADHOC;
1856		break;
1857	case NL80211_IFTYPE_STATION:
1858		mode = NDIS_80211_INFRA_INFRA;
1859		break;
1860	default:
1861		return -EINVAL;
1862	}
1863
1864	priv->wdev.iftype = type;
1865
1866	return set_infra_mode(usbdev, mode);
1867}
1868
1869static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1870{
1871	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1872	struct usbnet *usbdev = priv->usbdev;
1873	int err;
1874
1875	if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1876		err = set_frag_threshold(usbdev, wiphy->frag_threshold);
1877		if (err < 0)
1878			return err;
1879	}
1880
1881	if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1882		err = set_rts_threshold(usbdev, wiphy->rts_threshold);
1883		if (err < 0)
1884			return err;
1885	}
1886
1887	return 0;
1888}
1889
1890static int rndis_set_tx_power(struct wiphy *wiphy,
1891			      struct wireless_dev *wdev,
1892			      enum nl80211_tx_power_setting type,
1893			      int mbm)
1894{
1895	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1896	struct usbnet *usbdev = priv->usbdev;
1897
1898	netdev_dbg(usbdev->net, "%s(): type:0x%x mbm:%i\n",
1899		   __func__, type, mbm);
1900
1901	if (mbm < 0 || (mbm % 100))
1902		return -ENOTSUPP;
1903
1904	/* Device doesn't support changing txpower after initialization, only
1905	 * turn off/on radio. Support 'auto' mode and setting same dBm that is
1906	 * currently used.
1907	 */
1908	if (type == NL80211_TX_POWER_AUTOMATIC ||
1909	    MBM_TO_DBM(mbm) == get_bcm4320_power_dbm(priv)) {
1910		if (!priv->radio_on)
1911			disassociate(usbdev, true); /* turn on radio */
1912
1913		return 0;
1914	}
1915
1916	return -ENOTSUPP;
1917}
1918
1919static int rndis_get_tx_power(struct wiphy *wiphy,
1920			      struct wireless_dev *wdev,
1921			      int *dbm)
1922{
1923	struct rndis_wlan_private *priv = wiphy_priv(wiphy);
1924	struct usbnet *usbdev = priv->usbdev;
1925
1926	*dbm = get_bcm4320_power_dbm(priv);
1927
1928	netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
1929
1930	return 0;
1931}
1932
1933#define SCAN_DELAY_JIFFIES (6 * HZ)
1934static int rndis_scan(struct wiphy *wiphy,
1935			struct cfg80211_scan_request *request)
1936{
1937	struct net_device *dev = request->wdev->netdev;
1938	struct usbnet *usbdev = netdev_priv(dev);
1939	struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
1940	int ret;
1941	int delay = SCAN_DE

Large files files are truncated, but you can click here to view the full file