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