/drivers/net/wireless/rndis_wlan.c

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

Large files are truncated click here to view the full file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for RNDIS based wireless USB devices.
  4. *
  5. * Copyright (C) 2007 by Bjorge Dijkstra <bjd@jooz.net>
  6. * Copyright (C) 2008-2009 by Jussi Kivilinna <jussi.kivilinna@iki.fi>
  7. *
  8. * Portions of this file are based on NDISwrapper project,
  9. * Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani
  10. * http://ndiswrapper.sourceforge.net/
  11. */
  12. // #define DEBUG // error path messages, extra info
  13. // #define VERBOSE // more; success messages
  14. #include <linux/module.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/mutex.h>
  20. #include <linux/mii.h>
  21. #include <linux/usb.h>
  22. #include <linux/usb/cdc.h>
  23. #include <linux/ieee80211.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/ctype.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/slab.h>
  28. #include <net/cfg80211.h>
  29. #include <linux/usb/usbnet.h>
  30. #include <linux/usb/rndis_host.h>
  31. /* NOTE: All these are settings for Broadcom chipset */
  32. static char modparam_country[4] = "EU";
  33. module_param_string(country, modparam_country, 4, 0444);
  34. MODULE_PARM_DESC(country, "Country code (ISO 3166-1 alpha-2), default: EU");
  35. static int modparam_frameburst = 1;
  36. module_param_named(frameburst, modparam_frameburst, int, 0444);
  37. MODULE_PARM_DESC(frameburst, "enable frame bursting (default: on)");
  38. static int modparam_afterburner = 0;
  39. module_param_named(afterburner, modparam_afterburner, int, 0444);
  40. MODULE_PARM_DESC(afterburner,
  41. "enable afterburner aka '125 High Speed Mode' (default: off)");
  42. static int modparam_power_save = 0;
  43. module_param_named(power_save, modparam_power_save, int, 0444);
  44. MODULE_PARM_DESC(power_save,
  45. "set power save mode: 0=off, 1=on, 2=fast (default: off)");
  46. static int modparam_power_output = 3;
  47. module_param_named(power_output, modparam_power_output, int, 0444);
  48. MODULE_PARM_DESC(power_output,
  49. "set power output: 0=25%, 1=50%, 2=75%, 3=100% (default: 100%)");
  50. static int modparam_roamtrigger = -70;
  51. module_param_named(roamtrigger, modparam_roamtrigger, int, 0444);
  52. MODULE_PARM_DESC(roamtrigger,
  53. "set roaming dBm trigger: -80=optimize for distance, "
  54. "-60=bandwidth (default: -70)");
  55. static int modparam_roamdelta = 1;
  56. module_param_named(roamdelta, modparam_roamdelta, int, 0444);
  57. MODULE_PARM_DESC(roamdelta,
  58. "set roaming tendency: 0=aggressive, 1=moderate, "
  59. "2=conservative (default: moderate)");
  60. static int modparam_workaround_interval;
  61. module_param_named(workaround_interval, modparam_workaround_interval,
  62. int, 0444);
  63. MODULE_PARM_DESC(workaround_interval,
  64. "set stall workaround interval in msecs (0=disabled) (default: 0)");
  65. /* Typical noise/maximum signal level values taken from ndiswrapper iw_ndis.h */
  66. #define WL_NOISE -96 /* typical noise level in dBm */
  67. #define WL_SIGMAX -32 /* typical maximum signal level in dBm */
  68. /* Assume that Broadcom 4320 (only chipset at time of writing known to be
  69. * based on wireless rndis) has default txpower of 13dBm.
  70. * This value is from Linksys WUSB54GSC User Guide, Appendix F: Specifications.
  71. * 100% : 20 mW ~ 13dBm
  72. * 75% : 15 mW ~ 12dBm
  73. * 50% : 10 mW ~ 10dBm
  74. * 25% : 5 mW ~ 7dBm
  75. */
  76. #define BCM4320_DEFAULT_TXPOWER_DBM_100 13
  77. #define BCM4320_DEFAULT_TXPOWER_DBM_75 12
  78. #define BCM4320_DEFAULT_TXPOWER_DBM_50 10
  79. #define BCM4320_DEFAULT_TXPOWER_DBM_25 7
  80. /* Known device types */
  81. #define RNDIS_UNKNOWN 0
  82. #define RNDIS_BCM4320A 1
  83. #define RNDIS_BCM4320B 2
  84. /* NDIS data structures. Taken from wpa_supplicant driver_ndis.c
  85. * slightly modified for datatype endianess, etc
  86. */
  87. #define NDIS_802_11_LENGTH_SSID 32
  88. #define NDIS_802_11_LENGTH_RATES 8
  89. #define NDIS_802_11_LENGTH_RATES_EX 16
  90. enum ndis_80211_net_type {
  91. NDIS_80211_TYPE_FREQ_HOP,
  92. NDIS_80211_TYPE_DIRECT_SEQ,
  93. NDIS_80211_TYPE_OFDM_A,
  94. NDIS_80211_TYPE_OFDM_G
  95. };
  96. enum ndis_80211_net_infra {
  97. NDIS_80211_INFRA_ADHOC,
  98. NDIS_80211_INFRA_INFRA,
  99. NDIS_80211_INFRA_AUTO_UNKNOWN
  100. };
  101. enum ndis_80211_auth_mode {
  102. NDIS_80211_AUTH_OPEN,
  103. NDIS_80211_AUTH_SHARED,
  104. NDIS_80211_AUTH_AUTO_SWITCH,
  105. NDIS_80211_AUTH_WPA,
  106. NDIS_80211_AUTH_WPA_PSK,
  107. NDIS_80211_AUTH_WPA_NONE,
  108. NDIS_80211_AUTH_WPA2,
  109. NDIS_80211_AUTH_WPA2_PSK
  110. };
  111. enum ndis_80211_encr_status {
  112. NDIS_80211_ENCR_WEP_ENABLED,
  113. NDIS_80211_ENCR_DISABLED,
  114. NDIS_80211_ENCR_WEP_KEY_ABSENT,
  115. NDIS_80211_ENCR_NOT_SUPPORTED,
  116. NDIS_80211_ENCR_TKIP_ENABLED,
  117. NDIS_80211_ENCR_TKIP_KEY_ABSENT,
  118. NDIS_80211_ENCR_CCMP_ENABLED,
  119. NDIS_80211_ENCR_CCMP_KEY_ABSENT
  120. };
  121. enum ndis_80211_priv_filter {
  122. NDIS_80211_PRIV_ACCEPT_ALL,
  123. NDIS_80211_PRIV_8021X_WEP
  124. };
  125. enum ndis_80211_status_type {
  126. NDIS_80211_STATUSTYPE_AUTHENTICATION,
  127. NDIS_80211_STATUSTYPE_MEDIASTREAMMODE,
  128. NDIS_80211_STATUSTYPE_PMKID_CANDIDATELIST,
  129. NDIS_80211_STATUSTYPE_RADIOSTATE,
  130. };
  131. enum ndis_80211_media_stream_mode {
  132. NDIS_80211_MEDIA_STREAM_OFF,
  133. NDIS_80211_MEDIA_STREAM_ON
  134. };
  135. enum ndis_80211_radio_status {
  136. NDIS_80211_RADIO_STATUS_ON,
  137. NDIS_80211_RADIO_STATUS_HARDWARE_OFF,
  138. NDIS_80211_RADIO_STATUS_SOFTWARE_OFF,
  139. };
  140. enum ndis_80211_addkey_bits {
  141. NDIS_80211_ADDKEY_8021X_AUTH = cpu_to_le32(1 << 28),
  142. NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ = cpu_to_le32(1 << 29),
  143. NDIS_80211_ADDKEY_PAIRWISE_KEY = cpu_to_le32(1 << 30),
  144. NDIS_80211_ADDKEY_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  145. };
  146. enum ndis_80211_addwep_bits {
  147. NDIS_80211_ADDWEP_PERCLIENT_KEY = cpu_to_le32(1 << 30),
  148. NDIS_80211_ADDWEP_TRANSMIT_KEY = cpu_to_le32(1 << 31)
  149. };
  150. enum ndis_80211_power_mode {
  151. NDIS_80211_POWER_MODE_CAM,
  152. NDIS_80211_POWER_MODE_MAX_PSP,
  153. NDIS_80211_POWER_MODE_FAST_PSP,
  154. };
  155. enum ndis_80211_pmkid_cand_list_flag_bits {
  156. NDIS_80211_PMKID_CAND_PREAUTH = cpu_to_le32(1 << 0)
  157. };
  158. struct ndis_80211_auth_request {
  159. __le32 length;
  160. u8 bssid[ETH_ALEN];
  161. u8 padding[2];
  162. __le32 flags;
  163. } __packed;
  164. struct ndis_80211_pmkid_candidate {
  165. u8 bssid[ETH_ALEN];
  166. u8 padding[2];
  167. __le32 flags;
  168. } __packed;
  169. struct ndis_80211_pmkid_cand_list {
  170. __le32 version;
  171. __le32 num_candidates;
  172. struct ndis_80211_pmkid_candidate candidate_list[0];
  173. } __packed;
  174. struct ndis_80211_status_indication {
  175. __le32 status_type;
  176. union {
  177. __le32 media_stream_mode;
  178. __le32 radio_status;
  179. struct ndis_80211_auth_request auth_request[0];
  180. struct ndis_80211_pmkid_cand_list cand_list;
  181. } u;
  182. } __packed;
  183. struct ndis_80211_ssid {
  184. __le32 length;
  185. u8 essid[NDIS_802_11_LENGTH_SSID];
  186. } __packed;
  187. struct ndis_80211_conf_freq_hop {
  188. __le32 length;
  189. __le32 hop_pattern;
  190. __le32 hop_set;
  191. __le32 dwell_time;
  192. } __packed;
  193. struct ndis_80211_conf {
  194. __le32 length;
  195. __le32 beacon_period;
  196. __le32 atim_window;
  197. __le32 ds_config;
  198. struct ndis_80211_conf_freq_hop fh_config;
  199. } __packed;
  200. struct ndis_80211_bssid_ex {
  201. __le32 length;
  202. u8 mac[ETH_ALEN];
  203. u8 padding[2];
  204. struct ndis_80211_ssid ssid;
  205. __le32 privacy;
  206. __le32 rssi;
  207. __le32 net_type;
  208. struct ndis_80211_conf config;
  209. __le32 net_infra;
  210. u8 rates[NDIS_802_11_LENGTH_RATES_EX];
  211. __le32 ie_length;
  212. u8 ies[0];
  213. } __packed;
  214. struct ndis_80211_bssid_list_ex {
  215. __le32 num_items;
  216. struct ndis_80211_bssid_ex bssid[0];
  217. } __packed;
  218. struct ndis_80211_fixed_ies {
  219. u8 timestamp[8];
  220. __le16 beacon_interval;
  221. __le16 capabilities;
  222. } __packed;
  223. struct ndis_80211_wep_key {
  224. __le32 size;
  225. __le32 index;
  226. __le32 length;
  227. u8 material[32];
  228. } __packed;
  229. struct ndis_80211_key {
  230. __le32 size;
  231. __le32 index;
  232. __le32 length;
  233. u8 bssid[ETH_ALEN];
  234. u8 padding[6];
  235. u8 rsc[8];
  236. u8 material[32];
  237. } __packed;
  238. struct ndis_80211_remove_key {
  239. __le32 size;
  240. __le32 index;
  241. u8 bssid[ETH_ALEN];
  242. u8 padding[2];
  243. } __packed;
  244. struct ndis_config_param {
  245. __le32 name_offs;
  246. __le32 name_length;
  247. __le32 type;
  248. __le32 value_offs;
  249. __le32 value_length;
  250. } __packed;
  251. struct ndis_80211_assoc_info {
  252. __le32 length;
  253. __le16 req_ies;
  254. struct req_ie {
  255. __le16 capa;
  256. __le16 listen_interval;
  257. u8 cur_ap_address[ETH_ALEN];
  258. } req_ie;
  259. __le32 req_ie_length;
  260. __le32 offset_req_ies;
  261. __le16 resp_ies;
  262. struct resp_ie {
  263. __le16 capa;
  264. __le16 status_code;
  265. __le16 assoc_id;
  266. } resp_ie;
  267. __le32 resp_ie_length;
  268. __le32 offset_resp_ies;
  269. } __packed;
  270. struct ndis_80211_auth_encr_pair {
  271. __le32 auth_mode;
  272. __le32 encr_mode;
  273. } __packed;
  274. struct ndis_80211_capability {
  275. __le32 length;
  276. __le32 version;
  277. __le32 num_pmkids;
  278. __le32 num_auth_encr_pair;
  279. struct ndis_80211_auth_encr_pair auth_encr_pair[0];
  280. } __packed;
  281. struct ndis_80211_bssid_info {
  282. u8 bssid[ETH_ALEN];
  283. u8 pmkid[16];
  284. } __packed;
  285. struct ndis_80211_pmkid {
  286. __le32 length;
  287. __le32 bssid_info_count;
  288. struct ndis_80211_bssid_info bssid_info[0];
  289. } __packed;
  290. /*
  291. * private data
  292. */
  293. #define CAP_MODE_80211A 1
  294. #define CAP_MODE_80211B 2
  295. #define CAP_MODE_80211G 4
  296. #define CAP_MODE_MASK 7
  297. #define WORK_LINK_UP 0
  298. #define WORK_LINK_DOWN 1
  299. #define WORK_SET_MULTICAST_LIST 2
  300. #define RNDIS_WLAN_ALG_NONE 0
  301. #define RNDIS_WLAN_ALG_WEP (1<<0)
  302. #define RNDIS_WLAN_ALG_TKIP (1<<1)
  303. #define RNDIS_WLAN_ALG_CCMP (1<<2)
  304. #define RNDIS_WLAN_NUM_KEYS 4
  305. #define RNDIS_WLAN_KEY_MGMT_NONE 0
  306. #define RNDIS_WLAN_KEY_MGMT_802_1X (1<<0)
  307. #define RNDIS_WLAN_KEY_MGMT_PSK (1<<1)
  308. #define COMMAND_BUFFER_SIZE (CONTROL_BUFFER_SIZE + sizeof(struct rndis_set))
  309. static const struct ieee80211_channel rndis_channels[] = {
  310. { .center_freq = 2412 },
  311. { .center_freq = 2417 },
  312. { .center_freq = 2422 },
  313. { .center_freq = 2427 },
  314. { .center_freq = 2432 },
  315. { .center_freq = 2437 },
  316. { .center_freq = 2442 },
  317. { .center_freq = 2447 },
  318. { .center_freq = 2452 },
  319. { .center_freq = 2457 },
  320. { .center_freq = 2462 },
  321. { .center_freq = 2467 },
  322. { .center_freq = 2472 },
  323. { .center_freq = 2484 },
  324. };
  325. static const struct ieee80211_rate rndis_rates[] = {
  326. { .bitrate = 10 },
  327. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  328. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  329. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  330. { .bitrate = 60 },
  331. { .bitrate = 90 },
  332. { .bitrate = 120 },
  333. { .bitrate = 180 },
  334. { .bitrate = 240 },
  335. { .bitrate = 360 },
  336. { .bitrate = 480 },
  337. { .bitrate = 540 }
  338. };
  339. static const u32 rndis_cipher_suites[] = {
  340. WLAN_CIPHER_SUITE_WEP40,
  341. WLAN_CIPHER_SUITE_WEP104,
  342. WLAN_CIPHER_SUITE_TKIP,
  343. WLAN_CIPHER_SUITE_CCMP,
  344. };
  345. struct rndis_wlan_encr_key {
  346. int len;
  347. u32 cipher;
  348. u8 material[32];
  349. u8 bssid[ETH_ALEN];
  350. bool pairwise;
  351. bool tx_key;
  352. };
  353. /* RNDIS device private data */
  354. struct rndis_wlan_private {
  355. struct usbnet *usbdev;
  356. struct wireless_dev wdev;
  357. struct cfg80211_scan_request *scan_request;
  358. struct workqueue_struct *workqueue;
  359. struct delayed_work dev_poller_work;
  360. struct delayed_work scan_work;
  361. struct work_struct work;
  362. struct mutex command_lock;
  363. unsigned long work_pending;
  364. int last_qual;
  365. s32 cqm_rssi_thold;
  366. u32 cqm_rssi_hyst;
  367. int last_cqm_event_rssi;
  368. struct ieee80211_supported_band band;
  369. struct ieee80211_channel channels[ARRAY_SIZE(rndis_channels)];
  370. struct ieee80211_rate rates[ARRAY_SIZE(rndis_rates)];
  371. u32 cipher_suites[ARRAY_SIZE(rndis_cipher_suites)];
  372. int device_type;
  373. int caps;
  374. int multicast_size;
  375. /* module parameters */
  376. char param_country[4];
  377. int param_frameburst;
  378. int param_afterburner;
  379. int param_power_save;
  380. int param_power_output;
  381. int param_roamtrigger;
  382. int param_roamdelta;
  383. u32 param_workaround_interval;
  384. /* hardware state */
  385. bool radio_on;
  386. int power_mode;
  387. int infra_mode;
  388. bool connected;
  389. u8 bssid[ETH_ALEN];
  390. u32 current_command_oid;
  391. /* encryption stuff */
  392. u8 encr_tx_key_index;
  393. struct rndis_wlan_encr_key encr_keys[RNDIS_WLAN_NUM_KEYS];
  394. int wpa_version;
  395. u8 command_buffer[COMMAND_BUFFER_SIZE];
  396. };
  397. /*
  398. * cfg80211 ops
  399. */
  400. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  401. struct net_device *dev,
  402. enum nl80211_iftype type,
  403. struct vif_params *params);
  404. static int rndis_scan(struct wiphy *wiphy,
  405. struct cfg80211_scan_request *request);
  406. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed);
  407. static int rndis_set_tx_power(struct wiphy *wiphy,
  408. struct wireless_dev *wdev,
  409. enum nl80211_tx_power_setting type,
  410. int mbm);
  411. static int rndis_get_tx_power(struct wiphy *wiphy,
  412. struct wireless_dev *wdev,
  413. int *dbm);
  414. static int rndis_connect(struct wiphy *wiphy, struct net_device *dev,
  415. struct cfg80211_connect_params *sme);
  416. static int rndis_disconnect(struct wiphy *wiphy, struct net_device *dev,
  417. u16 reason_code);
  418. static int rndis_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  419. struct cfg80211_ibss_params *params);
  420. static int rndis_leave_ibss(struct wiphy *wiphy, struct net_device *dev);
  421. static int rndis_add_key(struct wiphy *wiphy, struct net_device *netdev,
  422. u8 key_index, bool pairwise, const u8 *mac_addr,
  423. struct key_params *params);
  424. static int rndis_del_key(struct wiphy *wiphy, struct net_device *netdev,
  425. u8 key_index, bool pairwise, const u8 *mac_addr);
  426. static int rndis_set_default_key(struct wiphy *wiphy, struct net_device *netdev,
  427. u8 key_index, bool unicast, bool multicast);
  428. static int rndis_get_station(struct wiphy *wiphy, struct net_device *dev,
  429. const u8 *mac, struct station_info *sinfo);
  430. static int rndis_dump_station(struct wiphy *wiphy, struct net_device *dev,
  431. int idx, u8 *mac, struct station_info *sinfo);
  432. static int rndis_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  433. struct cfg80211_pmksa *pmksa);
  434. static int rndis_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  435. struct cfg80211_pmksa *pmksa);
  436. static int rndis_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev);
  437. static int rndis_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
  438. bool enabled, int timeout);
  439. static int rndis_set_cqm_rssi_config(struct wiphy *wiphy,
  440. struct net_device *dev,
  441. s32 rssi_thold, u32 rssi_hyst);
  442. static const struct cfg80211_ops rndis_config_ops = {
  443. .change_virtual_intf = rndis_change_virtual_intf,
  444. .scan = rndis_scan,
  445. .set_wiphy_params = rndis_set_wiphy_params,
  446. .set_tx_power = rndis_set_tx_power,
  447. .get_tx_power = rndis_get_tx_power,
  448. .connect = rndis_connect,
  449. .disconnect = rndis_disconnect,
  450. .join_ibss = rndis_join_ibss,
  451. .leave_ibss = rndis_leave_ibss,
  452. .add_key = rndis_add_key,
  453. .del_key = rndis_del_key,
  454. .set_default_key = rndis_set_default_key,
  455. .get_station = rndis_get_station,
  456. .dump_station = rndis_dump_station,
  457. .set_pmksa = rndis_set_pmksa,
  458. .del_pmksa = rndis_del_pmksa,
  459. .flush_pmksa = rndis_flush_pmksa,
  460. .set_power_mgmt = rndis_set_power_mgmt,
  461. .set_cqm_rssi_config = rndis_set_cqm_rssi_config,
  462. };
  463. static void *rndis_wiphy_privid = &rndis_wiphy_privid;
  464. static struct rndis_wlan_private *get_rndis_wlan_priv(struct usbnet *dev)
  465. {
  466. return (struct rndis_wlan_private *)dev->driver_priv;
  467. }
  468. static u32 get_bcm4320_power_dbm(struct rndis_wlan_private *priv)
  469. {
  470. switch (priv->param_power_output) {
  471. default:
  472. case 3:
  473. return BCM4320_DEFAULT_TXPOWER_DBM_100;
  474. case 2:
  475. return BCM4320_DEFAULT_TXPOWER_DBM_75;
  476. case 1:
  477. return BCM4320_DEFAULT_TXPOWER_DBM_50;
  478. case 0:
  479. return BCM4320_DEFAULT_TXPOWER_DBM_25;
  480. }
  481. }
  482. static bool is_wpa_key(struct rndis_wlan_private *priv, u8 idx)
  483. {
  484. int cipher = priv->encr_keys[idx].cipher;
  485. return (cipher == WLAN_CIPHER_SUITE_CCMP ||
  486. cipher == WLAN_CIPHER_SUITE_TKIP);
  487. }
  488. static int rndis_cipher_to_alg(u32 cipher)
  489. {
  490. switch (cipher) {
  491. default:
  492. return RNDIS_WLAN_ALG_NONE;
  493. case WLAN_CIPHER_SUITE_WEP40:
  494. case WLAN_CIPHER_SUITE_WEP104:
  495. return RNDIS_WLAN_ALG_WEP;
  496. case WLAN_CIPHER_SUITE_TKIP:
  497. return RNDIS_WLAN_ALG_TKIP;
  498. case WLAN_CIPHER_SUITE_CCMP:
  499. return RNDIS_WLAN_ALG_CCMP;
  500. }
  501. }
  502. static int rndis_akm_suite_to_key_mgmt(u32 akm_suite)
  503. {
  504. switch (akm_suite) {
  505. default:
  506. return RNDIS_WLAN_KEY_MGMT_NONE;
  507. case WLAN_AKM_SUITE_8021X:
  508. return RNDIS_WLAN_KEY_MGMT_802_1X;
  509. case WLAN_AKM_SUITE_PSK:
  510. return RNDIS_WLAN_KEY_MGMT_PSK;
  511. }
  512. }
  513. #ifdef DEBUG
  514. static const char *oid_to_string(u32 oid)
  515. {
  516. switch (oid) {
  517. #define OID_STR(oid) case oid: return(#oid)
  518. /* from rndis_host.h */
  519. OID_STR(RNDIS_OID_802_3_PERMANENT_ADDRESS);
  520. OID_STR(RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE);
  521. OID_STR(RNDIS_OID_GEN_CURRENT_PACKET_FILTER);
  522. OID_STR(RNDIS_OID_GEN_PHYSICAL_MEDIUM);
  523. /* from rndis_wlan.c */
  524. OID_STR(RNDIS_OID_GEN_LINK_SPEED);
  525. OID_STR(RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER);
  526. OID_STR(RNDIS_OID_GEN_XMIT_OK);
  527. OID_STR(RNDIS_OID_GEN_RCV_OK);
  528. OID_STR(RNDIS_OID_GEN_XMIT_ERROR);
  529. OID_STR(RNDIS_OID_GEN_RCV_ERROR);
  530. OID_STR(RNDIS_OID_GEN_RCV_NO_BUFFER);
  531. OID_STR(RNDIS_OID_802_3_CURRENT_ADDRESS);
  532. OID_STR(RNDIS_OID_802_3_MULTICAST_LIST);
  533. OID_STR(RNDIS_OID_802_3_MAXIMUM_LIST_SIZE);
  534. OID_STR(RNDIS_OID_802_11_BSSID);
  535. OID_STR(RNDIS_OID_802_11_SSID);
  536. OID_STR(RNDIS_OID_802_11_INFRASTRUCTURE_MODE);
  537. OID_STR(RNDIS_OID_802_11_ADD_WEP);
  538. OID_STR(RNDIS_OID_802_11_REMOVE_WEP);
  539. OID_STR(RNDIS_OID_802_11_DISASSOCIATE);
  540. OID_STR(RNDIS_OID_802_11_AUTHENTICATION_MODE);
  541. OID_STR(RNDIS_OID_802_11_PRIVACY_FILTER);
  542. OID_STR(RNDIS_OID_802_11_BSSID_LIST_SCAN);
  543. OID_STR(RNDIS_OID_802_11_ENCRYPTION_STATUS);
  544. OID_STR(RNDIS_OID_802_11_ADD_KEY);
  545. OID_STR(RNDIS_OID_802_11_REMOVE_KEY);
  546. OID_STR(RNDIS_OID_802_11_ASSOCIATION_INFORMATION);
  547. OID_STR(RNDIS_OID_802_11_CAPABILITY);
  548. OID_STR(RNDIS_OID_802_11_PMKID);
  549. OID_STR(RNDIS_OID_802_11_NETWORK_TYPES_SUPPORTED);
  550. OID_STR(RNDIS_OID_802_11_NETWORK_TYPE_IN_USE);
  551. OID_STR(RNDIS_OID_802_11_TX_POWER_LEVEL);
  552. OID_STR(RNDIS_OID_802_11_RSSI);
  553. OID_STR(RNDIS_OID_802_11_RSSI_TRIGGER);
  554. OID_STR(RNDIS_OID_802_11_FRAGMENTATION_THRESHOLD);
  555. OID_STR(RNDIS_OID_802_11_RTS_THRESHOLD);
  556. OID_STR(RNDIS_OID_802_11_SUPPORTED_RATES);
  557. OID_STR(RNDIS_OID_802_11_CONFIGURATION);
  558. OID_STR(RNDIS_OID_802_11_POWER_MODE);
  559. OID_STR(RNDIS_OID_802_11_BSSID_LIST);
  560. #undef OID_STR
  561. }
  562. return "?";
  563. }
  564. #else
  565. static const char *oid_to_string(u32 oid)
  566. {
  567. return "?";
  568. }
  569. #endif
  570. /* translate error code */
  571. static int rndis_error_status(__le32 rndis_status)
  572. {
  573. int ret = -EINVAL;
  574. switch (le32_to_cpu(rndis_status)) {
  575. case RNDIS_STATUS_SUCCESS:
  576. ret = 0;
  577. break;
  578. case RNDIS_STATUS_FAILURE:
  579. case RNDIS_STATUS_INVALID_DATA:
  580. ret = -EINVAL;
  581. break;
  582. case RNDIS_STATUS_NOT_SUPPORTED:
  583. ret = -EOPNOTSUPP;
  584. break;
  585. case RNDIS_STATUS_ADAPTER_NOT_READY:
  586. case RNDIS_STATUS_ADAPTER_NOT_OPEN:
  587. ret = -EBUSY;
  588. break;
  589. }
  590. return ret;
  591. }
  592. static int rndis_query_oid(struct usbnet *dev, u32 oid, void *data, int *len)
  593. {
  594. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  595. union {
  596. void *buf;
  597. struct rndis_msg_hdr *header;
  598. struct rndis_query *get;
  599. struct rndis_query_c *get_c;
  600. } u;
  601. int ret, buflen;
  602. int resplen, respoffs, copylen;
  603. buflen = *len + sizeof(*u.get);
  604. if (buflen < CONTROL_BUFFER_SIZE)
  605. buflen = CONTROL_BUFFER_SIZE;
  606. if (buflen > COMMAND_BUFFER_SIZE) {
  607. u.buf = kmalloc(buflen, GFP_KERNEL);
  608. if (!u.buf)
  609. return -ENOMEM;
  610. } else {
  611. u.buf = priv->command_buffer;
  612. }
  613. mutex_lock(&priv->command_lock);
  614. memset(u.get, 0, sizeof *u.get);
  615. u.get->msg_type = cpu_to_le32(RNDIS_MSG_QUERY);
  616. u.get->msg_len = cpu_to_le32(sizeof *u.get);
  617. u.get->oid = cpu_to_le32(oid);
  618. priv->current_command_oid = oid;
  619. ret = rndis_command(dev, u.header, buflen);
  620. priv->current_command_oid = 0;
  621. if (ret < 0)
  622. netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
  623. __func__, oid_to_string(oid), ret,
  624. le32_to_cpu(u.get_c->status));
  625. if (ret == 0) {
  626. resplen = le32_to_cpu(u.get_c->len);
  627. respoffs = le32_to_cpu(u.get_c->offset) + 8;
  628. if (respoffs > buflen) {
  629. /* Device returned data offset outside buffer, error. */
  630. netdev_dbg(dev->net, "%s(%s): received invalid "
  631. "data offset: %d > %d\n", __func__,
  632. oid_to_string(oid), respoffs, buflen);
  633. ret = -EINVAL;
  634. goto exit_unlock;
  635. }
  636. if ((resplen + respoffs) > buflen) {
  637. /* Device would have returned more data if buffer would
  638. * have been big enough. Copy just the bits that we got.
  639. */
  640. copylen = buflen - respoffs;
  641. } else {
  642. copylen = resplen;
  643. }
  644. if (copylen > *len)
  645. copylen = *len;
  646. memcpy(data, u.buf + respoffs, copylen);
  647. *len = resplen;
  648. ret = rndis_error_status(u.get_c->status);
  649. if (ret < 0)
  650. netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
  651. __func__, oid_to_string(oid),
  652. le32_to_cpu(u.get_c->status), ret);
  653. }
  654. exit_unlock:
  655. mutex_unlock(&priv->command_lock);
  656. if (u.buf != priv->command_buffer)
  657. kfree(u.buf);
  658. return ret;
  659. }
  660. static int rndis_set_oid(struct usbnet *dev, u32 oid, const void *data,
  661. int len)
  662. {
  663. struct rndis_wlan_private *priv = get_rndis_wlan_priv(dev);
  664. union {
  665. void *buf;
  666. struct rndis_msg_hdr *header;
  667. struct rndis_set *set;
  668. struct rndis_set_c *set_c;
  669. } u;
  670. int ret, buflen;
  671. buflen = len + sizeof(*u.set);
  672. if (buflen < CONTROL_BUFFER_SIZE)
  673. buflen = CONTROL_BUFFER_SIZE;
  674. if (buflen > COMMAND_BUFFER_SIZE) {
  675. u.buf = kmalloc(buflen, GFP_KERNEL);
  676. if (!u.buf)
  677. return -ENOMEM;
  678. } else {
  679. u.buf = priv->command_buffer;
  680. }
  681. mutex_lock(&priv->command_lock);
  682. memset(u.set, 0, sizeof *u.set);
  683. u.set->msg_type = cpu_to_le32(RNDIS_MSG_SET);
  684. u.set->msg_len = cpu_to_le32(sizeof(*u.set) + len);
  685. u.set->oid = cpu_to_le32(oid);
  686. u.set->len = cpu_to_le32(len);
  687. u.set->offset = cpu_to_le32(sizeof(*u.set) - 8);
  688. u.set->handle = cpu_to_le32(0);
  689. memcpy(u.buf + sizeof(*u.set), data, len);
  690. priv->current_command_oid = oid;
  691. ret = rndis_command(dev, u.header, buflen);
  692. priv->current_command_oid = 0;
  693. if (ret < 0)
  694. netdev_dbg(dev->net, "%s(%s): rndis_command() failed, %d (%08x)\n",
  695. __func__, oid_to_string(oid), ret,
  696. le32_to_cpu(u.set_c->status));
  697. if (ret == 0) {
  698. ret = rndis_error_status(u.set_c->status);
  699. if (ret < 0)
  700. netdev_dbg(dev->net, "%s(%s): device returned error, 0x%08x (%d)\n",
  701. __func__, oid_to_string(oid),
  702. le32_to_cpu(u.set_c->status), ret);
  703. }
  704. mutex_unlock(&priv->command_lock);
  705. if (u.buf != priv->command_buffer)
  706. kfree(u.buf);
  707. return ret;
  708. }
  709. static int rndis_reset(struct usbnet *usbdev)
  710. {
  711. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  712. struct rndis_reset *reset;
  713. int ret;
  714. mutex_lock(&priv->command_lock);
  715. reset = (void *)priv->command_buffer;
  716. memset(reset, 0, sizeof(*reset));
  717. reset->msg_type = cpu_to_le32(RNDIS_MSG_RESET);
  718. reset->msg_len = cpu_to_le32(sizeof(*reset));
  719. priv->current_command_oid = 0;
  720. ret = rndis_command(usbdev, (void *)reset, CONTROL_BUFFER_SIZE);
  721. mutex_unlock(&priv->command_lock);
  722. if (ret < 0)
  723. return ret;
  724. return 0;
  725. }
  726. /*
  727. * Specs say that we can only set config parameters only soon after device
  728. * initialization.
  729. * value_type: 0 = u32, 2 = unicode string
  730. */
  731. static int rndis_set_config_parameter(struct usbnet *dev, char *param,
  732. int value_type, void *value)
  733. {
  734. struct ndis_config_param *infobuf;
  735. int value_len, info_len, param_len, ret, i;
  736. __le16 *unibuf;
  737. __le32 *dst_value;
  738. if (value_type == 0)
  739. value_len = sizeof(__le32);
  740. else if (value_type == 2)
  741. value_len = strlen(value) * sizeof(__le16);
  742. else
  743. return -EINVAL;
  744. param_len = strlen(param) * sizeof(__le16);
  745. info_len = sizeof(*infobuf) + param_len + value_len;
  746. #ifdef DEBUG
  747. info_len += 12;
  748. #endif
  749. infobuf = kmalloc(info_len, GFP_KERNEL);
  750. if (!infobuf)
  751. return -ENOMEM;
  752. #ifdef DEBUG
  753. info_len -= 12;
  754. /* extra 12 bytes are for padding (debug output) */
  755. memset(infobuf, 0xCC, info_len + 12);
  756. #endif
  757. if (value_type == 2)
  758. netdev_dbg(dev->net, "setting config parameter: %s, value: %s\n",
  759. param, (u8 *)value);
  760. else
  761. netdev_dbg(dev->net, "setting config parameter: %s, value: %d\n",
  762. param, *(u32 *)value);
  763. infobuf->name_offs = cpu_to_le32(sizeof(*infobuf));
  764. infobuf->name_length = cpu_to_le32(param_len);
  765. infobuf->type = cpu_to_le32(value_type);
  766. infobuf->value_offs = cpu_to_le32(sizeof(*infobuf) + param_len);
  767. infobuf->value_length = cpu_to_le32(value_len);
  768. /* simple string to unicode string conversion */
  769. unibuf = (void *)infobuf + sizeof(*infobuf);
  770. for (i = 0; i < param_len / sizeof(__le16); i++)
  771. unibuf[i] = cpu_to_le16(param[i]);
  772. if (value_type == 2) {
  773. unibuf = (void *)infobuf + sizeof(*infobuf) + param_len;
  774. for (i = 0; i < value_len / sizeof(__le16); i++)
  775. unibuf[i] = cpu_to_le16(((u8 *)value)[i]);
  776. } else {
  777. dst_value = (void *)infobuf + sizeof(*infobuf) + param_len;
  778. *dst_value = cpu_to_le32(*(u32 *)value);
  779. }
  780. #ifdef DEBUG
  781. netdev_dbg(dev->net, "info buffer (len: %d)\n", info_len);
  782. for (i = 0; i < info_len; i += 12) {
  783. u32 *tmp = (u32 *)((u8 *)infobuf + i);
  784. netdev_dbg(dev->net, "%08X:%08X:%08X\n",
  785. cpu_to_be32(tmp[0]),
  786. cpu_to_be32(tmp[1]),
  787. cpu_to_be32(tmp[2]));
  788. }
  789. #endif
  790. ret = rndis_set_oid(dev, RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER,
  791. infobuf, info_len);
  792. if (ret != 0)
  793. netdev_dbg(dev->net, "setting rndis config parameter failed, %d\n",
  794. ret);
  795. kfree(infobuf);
  796. return ret;
  797. }
  798. static int rndis_set_config_parameter_str(struct usbnet *dev,
  799. char *param, char *value)
  800. {
  801. return rndis_set_config_parameter(dev, param, 2, value);
  802. }
  803. /*
  804. * data conversion functions
  805. */
  806. static int level_to_qual(int level)
  807. {
  808. int qual = 100 * (level - WL_NOISE) / (WL_SIGMAX - WL_NOISE);
  809. return qual >= 0 ? (qual <= 100 ? qual : 100) : 0;
  810. }
  811. /*
  812. * common functions
  813. */
  814. static int set_infra_mode(struct usbnet *usbdev, int mode);
  815. static void restore_keys(struct usbnet *usbdev);
  816. static int rndis_check_bssid_list(struct usbnet *usbdev, u8 *match_bssid,
  817. bool *matched);
  818. static int rndis_start_bssid_list_scan(struct usbnet *usbdev)
  819. {
  820. __le32 tmp;
  821. /* Note: RNDIS_OID_802_11_BSSID_LIST_SCAN clears internal BSS list. */
  822. tmp = cpu_to_le32(1);
  823. return rndis_set_oid(usbdev, RNDIS_OID_802_11_BSSID_LIST_SCAN, &tmp,
  824. sizeof(tmp));
  825. }
  826. static int set_essid(struct usbnet *usbdev, struct ndis_80211_ssid *ssid)
  827. {
  828. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  829. int ret;
  830. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_SSID,
  831. ssid, sizeof(*ssid));
  832. if (ret < 0) {
  833. netdev_warn(usbdev->net, "setting SSID failed (%08X)\n", ret);
  834. return ret;
  835. }
  836. if (ret == 0) {
  837. priv->radio_on = true;
  838. netdev_dbg(usbdev->net, "%s(): radio_on = true\n", __func__);
  839. }
  840. return ret;
  841. }
  842. static int set_bssid(struct usbnet *usbdev, const u8 *bssid)
  843. {
  844. int ret;
  845. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_BSSID,
  846. bssid, ETH_ALEN);
  847. if (ret < 0) {
  848. netdev_warn(usbdev->net, "setting BSSID[%pM] failed (%08X)\n",
  849. bssid, ret);
  850. return ret;
  851. }
  852. return ret;
  853. }
  854. static int clear_bssid(struct usbnet *usbdev)
  855. {
  856. static const u8 broadcast_mac[ETH_ALEN] = {
  857. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  858. };
  859. return set_bssid(usbdev, broadcast_mac);
  860. }
  861. static int get_bssid(struct usbnet *usbdev, u8 bssid[ETH_ALEN])
  862. {
  863. int ret, len;
  864. len = ETH_ALEN;
  865. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_BSSID,
  866. bssid, &len);
  867. if (ret != 0)
  868. eth_zero_addr(bssid);
  869. return ret;
  870. }
  871. static int get_association_info(struct usbnet *usbdev,
  872. struct ndis_80211_assoc_info *info, int len)
  873. {
  874. return rndis_query_oid(usbdev,
  875. RNDIS_OID_802_11_ASSOCIATION_INFORMATION,
  876. info, &len);
  877. }
  878. static bool is_associated(struct usbnet *usbdev)
  879. {
  880. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  881. u8 bssid[ETH_ALEN];
  882. int ret;
  883. if (!priv->radio_on)
  884. return false;
  885. ret = get_bssid(usbdev, bssid);
  886. return (ret == 0 && !is_zero_ether_addr(bssid));
  887. }
  888. static int disassociate(struct usbnet *usbdev, bool reset_ssid)
  889. {
  890. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  891. struct ndis_80211_ssid ssid;
  892. int i, ret = 0;
  893. if (priv->radio_on) {
  894. ret = rndis_set_oid(usbdev,
  895. RNDIS_OID_802_11_DISASSOCIATE,
  896. NULL, 0);
  897. if (ret == 0) {
  898. priv->radio_on = false;
  899. netdev_dbg(usbdev->net, "%s(): radio_on = false\n",
  900. __func__);
  901. if (reset_ssid)
  902. msleep(100);
  903. }
  904. }
  905. /* disassociate causes radio to be turned off; if reset_ssid
  906. * is given, set random ssid to enable radio */
  907. if (reset_ssid) {
  908. /* Set device to infrastructure mode so we don't get ad-hoc
  909. * 'media connect' indications with the random ssid.
  910. */
  911. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  912. ssid.length = cpu_to_le32(sizeof(ssid.essid));
  913. get_random_bytes(&ssid.essid[2], sizeof(ssid.essid)-2);
  914. ssid.essid[0] = 0x1;
  915. ssid.essid[1] = 0xff;
  916. for (i = 2; i < sizeof(ssid.essid); i++)
  917. ssid.essid[i] = 0x1 + (ssid.essid[i] * 0xfe / 0xff);
  918. ret = set_essid(usbdev, &ssid);
  919. }
  920. return ret;
  921. }
  922. static int set_auth_mode(struct usbnet *usbdev, u32 wpa_version,
  923. enum nl80211_auth_type auth_type, int keymgmt)
  924. {
  925. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  926. __le32 tmp;
  927. int auth_mode, ret;
  928. netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x authalg=0x%x keymgmt=0x%x\n",
  929. __func__, wpa_version, auth_type, keymgmt);
  930. if (wpa_version & NL80211_WPA_VERSION_2) {
  931. if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
  932. auth_mode = NDIS_80211_AUTH_WPA2;
  933. else
  934. auth_mode = NDIS_80211_AUTH_WPA2_PSK;
  935. } else if (wpa_version & NL80211_WPA_VERSION_1) {
  936. if (keymgmt & RNDIS_WLAN_KEY_MGMT_802_1X)
  937. auth_mode = NDIS_80211_AUTH_WPA;
  938. else if (keymgmt & RNDIS_WLAN_KEY_MGMT_PSK)
  939. auth_mode = NDIS_80211_AUTH_WPA_PSK;
  940. else
  941. auth_mode = NDIS_80211_AUTH_WPA_NONE;
  942. } else if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
  943. auth_mode = NDIS_80211_AUTH_SHARED;
  944. else if (auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM)
  945. auth_mode = NDIS_80211_AUTH_OPEN;
  946. else if (auth_type == NL80211_AUTHTYPE_AUTOMATIC)
  947. auth_mode = NDIS_80211_AUTH_AUTO_SWITCH;
  948. else
  949. return -ENOTSUPP;
  950. tmp = cpu_to_le32(auth_mode);
  951. ret = rndis_set_oid(usbdev,
  952. RNDIS_OID_802_11_AUTHENTICATION_MODE,
  953. &tmp, sizeof(tmp));
  954. if (ret != 0) {
  955. netdev_warn(usbdev->net, "setting auth mode failed (%08X)\n",
  956. ret);
  957. return ret;
  958. }
  959. priv->wpa_version = wpa_version;
  960. return 0;
  961. }
  962. static int set_priv_filter(struct usbnet *usbdev)
  963. {
  964. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  965. __le32 tmp;
  966. netdev_dbg(usbdev->net, "%s(): wpa_version=0x%x\n",
  967. __func__, priv->wpa_version);
  968. if (priv->wpa_version & NL80211_WPA_VERSION_2 ||
  969. priv->wpa_version & NL80211_WPA_VERSION_1)
  970. tmp = cpu_to_le32(NDIS_80211_PRIV_8021X_WEP);
  971. else
  972. tmp = cpu_to_le32(NDIS_80211_PRIV_ACCEPT_ALL);
  973. return rndis_set_oid(usbdev,
  974. RNDIS_OID_802_11_PRIVACY_FILTER, &tmp,
  975. sizeof(tmp));
  976. }
  977. static int set_encr_mode(struct usbnet *usbdev, int pairwise, int groupwise)
  978. {
  979. __le32 tmp;
  980. int encr_mode, ret;
  981. netdev_dbg(usbdev->net, "%s(): cipher_pair=0x%x cipher_group=0x%x\n",
  982. __func__, pairwise, groupwise);
  983. if (pairwise & RNDIS_WLAN_ALG_CCMP)
  984. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  985. else if (pairwise & RNDIS_WLAN_ALG_TKIP)
  986. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  987. else if (pairwise & RNDIS_WLAN_ALG_WEP)
  988. encr_mode = NDIS_80211_ENCR_WEP_ENABLED;
  989. else if (groupwise & RNDIS_WLAN_ALG_CCMP)
  990. encr_mode = NDIS_80211_ENCR_CCMP_ENABLED;
  991. else if (groupwise & RNDIS_WLAN_ALG_TKIP)
  992. encr_mode = NDIS_80211_ENCR_TKIP_ENABLED;
  993. else
  994. encr_mode = NDIS_80211_ENCR_DISABLED;
  995. tmp = cpu_to_le32(encr_mode);
  996. ret = rndis_set_oid(usbdev,
  997. RNDIS_OID_802_11_ENCRYPTION_STATUS, &tmp,
  998. sizeof(tmp));
  999. if (ret != 0) {
  1000. netdev_warn(usbdev->net, "setting encr mode failed (%08X)\n",
  1001. ret);
  1002. return ret;
  1003. }
  1004. return 0;
  1005. }
  1006. static int set_infra_mode(struct usbnet *usbdev, int mode)
  1007. {
  1008. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1009. __le32 tmp;
  1010. int ret;
  1011. netdev_dbg(usbdev->net, "%s(): infra_mode=0x%x\n",
  1012. __func__, priv->infra_mode);
  1013. tmp = cpu_to_le32(mode);
  1014. ret = rndis_set_oid(usbdev,
  1015. RNDIS_OID_802_11_INFRASTRUCTURE_MODE,
  1016. &tmp, sizeof(tmp));
  1017. if (ret != 0) {
  1018. netdev_warn(usbdev->net, "setting infra mode failed (%08X)\n",
  1019. ret);
  1020. return ret;
  1021. }
  1022. /* NDIS drivers clear keys when infrastructure mode is
  1023. * changed. But Linux tools assume otherwise. So set the
  1024. * keys */
  1025. restore_keys(usbdev);
  1026. priv->infra_mode = mode;
  1027. return 0;
  1028. }
  1029. static int set_rts_threshold(struct usbnet *usbdev, u32 rts_threshold)
  1030. {
  1031. __le32 tmp;
  1032. netdev_dbg(usbdev->net, "%s(): %i\n", __func__, rts_threshold);
  1033. if (rts_threshold == -1 || rts_threshold > 2347)
  1034. rts_threshold = 2347;
  1035. tmp = cpu_to_le32(rts_threshold);
  1036. return rndis_set_oid(usbdev,
  1037. RNDIS_OID_802_11_RTS_THRESHOLD,
  1038. &tmp, sizeof(tmp));
  1039. }
  1040. static int set_frag_threshold(struct usbnet *usbdev, u32 frag_threshold)
  1041. {
  1042. __le32 tmp;
  1043. netdev_dbg(usbdev->net, "%s(): %i\n", __func__, frag_threshold);
  1044. if (frag_threshold < 256 || frag_threshold > 2346)
  1045. frag_threshold = 2346;
  1046. tmp = cpu_to_le32(frag_threshold);
  1047. return rndis_set_oid(usbdev,
  1048. RNDIS_OID_802_11_FRAGMENTATION_THRESHOLD,
  1049. &tmp, sizeof(tmp));
  1050. }
  1051. static void set_default_iw_params(struct usbnet *usbdev)
  1052. {
  1053. set_infra_mode(usbdev, NDIS_80211_INFRA_INFRA);
  1054. set_auth_mode(usbdev, 0, NL80211_AUTHTYPE_OPEN_SYSTEM,
  1055. RNDIS_WLAN_KEY_MGMT_NONE);
  1056. set_priv_filter(usbdev);
  1057. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1058. }
  1059. static int deauthenticate(struct usbnet *usbdev)
  1060. {
  1061. int ret;
  1062. ret = disassociate(usbdev, true);
  1063. set_default_iw_params(usbdev);
  1064. return ret;
  1065. }
  1066. static int set_channel(struct usbnet *usbdev, int channel)
  1067. {
  1068. struct ndis_80211_conf config;
  1069. unsigned int dsconfig;
  1070. int len, ret;
  1071. netdev_dbg(usbdev->net, "%s(%d)\n", __func__, channel);
  1072. /* this OID is valid only when not associated */
  1073. if (is_associated(usbdev))
  1074. return 0;
  1075. dsconfig = 1000 *
  1076. ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
  1077. len = sizeof(config);
  1078. ret = rndis_query_oid(usbdev,
  1079. RNDIS_OID_802_11_CONFIGURATION,
  1080. &config, &len);
  1081. if (ret < 0) {
  1082. netdev_dbg(usbdev->net, "%s(): querying configuration failed\n",
  1083. __func__);
  1084. return ret;
  1085. }
  1086. config.ds_config = cpu_to_le32(dsconfig);
  1087. ret = rndis_set_oid(usbdev,
  1088. RNDIS_OID_802_11_CONFIGURATION,
  1089. &config, sizeof(config));
  1090. netdev_dbg(usbdev->net, "%s(): %d -> %d\n", __func__, channel, ret);
  1091. return ret;
  1092. }
  1093. static struct ieee80211_channel *get_current_channel(struct usbnet *usbdev,
  1094. u32 *beacon_period)
  1095. {
  1096. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1097. struct ieee80211_channel *channel;
  1098. struct ndis_80211_conf config;
  1099. int len, ret;
  1100. /* Get channel and beacon interval */
  1101. len = sizeof(config);
  1102. ret = rndis_query_oid(usbdev,
  1103. RNDIS_OID_802_11_CONFIGURATION,
  1104. &config, &len);
  1105. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_CONFIGURATION -> %d\n",
  1106. __func__, ret);
  1107. if (ret < 0)
  1108. return NULL;
  1109. channel = ieee80211_get_channel(priv->wdev.wiphy,
  1110. KHZ_TO_MHZ(le32_to_cpu(config.ds_config)));
  1111. if (!channel)
  1112. return NULL;
  1113. if (beacon_period)
  1114. *beacon_period = le32_to_cpu(config.beacon_period);
  1115. return channel;
  1116. }
  1117. /* index must be 0 - N, as per NDIS */
  1118. static int add_wep_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1119. u8 index)
  1120. {
  1121. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1122. struct ndis_80211_wep_key ndis_key;
  1123. u32 cipher;
  1124. int ret;
  1125. netdev_dbg(usbdev->net, "%s(idx: %d, len: %d)\n",
  1126. __func__, index, key_len);
  1127. if (index >= RNDIS_WLAN_NUM_KEYS)
  1128. return -EINVAL;
  1129. if (key_len == 5)
  1130. cipher = WLAN_CIPHER_SUITE_WEP40;
  1131. else if (key_len == 13)
  1132. cipher = WLAN_CIPHER_SUITE_WEP104;
  1133. else
  1134. return -EINVAL;
  1135. memset(&ndis_key, 0, sizeof(ndis_key));
  1136. ndis_key.size = cpu_to_le32(sizeof(ndis_key));
  1137. ndis_key.length = cpu_to_le32(key_len);
  1138. ndis_key.index = cpu_to_le32(index);
  1139. memcpy(&ndis_key.material, key, key_len);
  1140. if (index == priv->encr_tx_key_index) {
  1141. ndis_key.index |= NDIS_80211_ADDWEP_TRANSMIT_KEY;
  1142. ret = set_encr_mode(usbdev, RNDIS_WLAN_ALG_WEP,
  1143. RNDIS_WLAN_ALG_NONE);
  1144. if (ret)
  1145. netdev_warn(usbdev->net, "encryption couldn't be enabled (%08X)\n",
  1146. ret);
  1147. }
  1148. ret = rndis_set_oid(usbdev,
  1149. RNDIS_OID_802_11_ADD_WEP, &ndis_key,
  1150. sizeof(ndis_key));
  1151. if (ret != 0) {
  1152. netdev_warn(usbdev->net, "adding encryption key %d failed (%08X)\n",
  1153. index + 1, ret);
  1154. return ret;
  1155. }
  1156. priv->encr_keys[index].len = key_len;
  1157. priv->encr_keys[index].cipher = cipher;
  1158. memcpy(&priv->encr_keys[index].material, key, key_len);
  1159. eth_broadcast_addr(priv->encr_keys[index].bssid);
  1160. return 0;
  1161. }
  1162. static int add_wpa_key(struct usbnet *usbdev, const u8 *key, int key_len,
  1163. u8 index, const u8 *addr, const u8 *rx_seq,
  1164. int seq_len, u32 cipher, __le32 flags)
  1165. {
  1166. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1167. struct ndis_80211_key ndis_key;
  1168. bool is_addr_ok;
  1169. int ret;
  1170. if (index >= RNDIS_WLAN_NUM_KEYS) {
  1171. netdev_dbg(usbdev->net, "%s(): index out of range (%i)\n",
  1172. __func__, index);
  1173. return -EINVAL;
  1174. }
  1175. if (key_len > sizeof(ndis_key.material) || key_len < 0) {
  1176. netdev_dbg(usbdev->net, "%s(): key length out of range (%i)\n",
  1177. __func__, key_len);
  1178. return -EINVAL;
  1179. }
  1180. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ) {
  1181. if (!rx_seq || seq_len <= 0) {
  1182. netdev_dbg(usbdev->net, "%s(): recv seq flag without buffer\n",
  1183. __func__);
  1184. return -EINVAL;
  1185. }
  1186. if (rx_seq && seq_len > sizeof(ndis_key.rsc)) {
  1187. netdev_dbg(usbdev->net, "%s(): too big recv seq buffer\n", __func__);
  1188. return -EINVAL;
  1189. }
  1190. }
  1191. is_addr_ok = addr && !is_zero_ether_addr(addr) &&
  1192. !is_broadcast_ether_addr(addr);
  1193. if ((flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) && !is_addr_ok) {
  1194. netdev_dbg(usbdev->net, "%s(): pairwise but bssid invalid (%pM)\n",
  1195. __func__, addr);
  1196. return -EINVAL;
  1197. }
  1198. netdev_dbg(usbdev->net, "%s(%i): flags:%i%i%i\n",
  1199. __func__, index,
  1200. !!(flags & NDIS_80211_ADDKEY_TRANSMIT_KEY),
  1201. !!(flags & NDIS_80211_ADDKEY_PAIRWISE_KEY),
  1202. !!(flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ));
  1203. memset(&ndis_key, 0, sizeof(ndis_key));
  1204. ndis_key.size = cpu_to_le32(sizeof(ndis_key) -
  1205. sizeof(ndis_key.material) + key_len);
  1206. ndis_key.length = cpu_to_le32(key_len);
  1207. ndis_key.index = cpu_to_le32(index) | flags;
  1208. if (cipher == WLAN_CIPHER_SUITE_TKIP && key_len == 32) {
  1209. /* wpa_supplicant gives us the Michael MIC RX/TX keys in
  1210. * different order than NDIS spec, so swap the order here. */
  1211. memcpy(ndis_key.material, key, 16);
  1212. memcpy(ndis_key.material + 16, key + 24, 8);
  1213. memcpy(ndis_key.material + 24, key + 16, 8);
  1214. } else
  1215. memcpy(ndis_key.material, key, key_len);
  1216. if (flags & NDIS_80211_ADDKEY_SET_INIT_RECV_SEQ)
  1217. memcpy(ndis_key.rsc, rx_seq, seq_len);
  1218. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY) {
  1219. /* pairwise key */
  1220. memcpy(ndis_key.bssid, addr, ETH_ALEN);
  1221. } else {
  1222. /* group key */
  1223. if (priv->infra_mode == NDIS_80211_INFRA_ADHOC)
  1224. eth_broadcast_addr(ndis_key.bssid);
  1225. else
  1226. get_bssid(usbdev, ndis_key.bssid);
  1227. }
  1228. ret = rndis_set_oid(usbdev,
  1229. RNDIS_OID_802_11_ADD_KEY, &ndis_key,
  1230. le32_to_cpu(ndis_key.size));
  1231. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_ADD_KEY -> %08X\n",
  1232. __func__, ret);
  1233. if (ret != 0)
  1234. return ret;
  1235. memset(&priv->encr_keys[index], 0, sizeof(priv->encr_keys[index]));
  1236. priv->encr_keys[index].len = key_len;
  1237. priv->encr_keys[index].cipher = cipher;
  1238. memcpy(&priv->encr_keys[index].material, key, key_len);
  1239. if (flags & NDIS_80211_ADDKEY_PAIRWISE_KEY)
  1240. memcpy(&priv->encr_keys[index].bssid, ndis_key.bssid, ETH_ALEN);
  1241. else
  1242. eth_broadcast_addr(priv->encr_keys[index].bssid);
  1243. if (flags & NDIS_80211_ADDKEY_TRANSMIT_KEY)
  1244. priv->encr_tx_key_index = index;
  1245. return 0;
  1246. }
  1247. static int restore_key(struct usbnet *usbdev, u8 key_idx)
  1248. {
  1249. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1250. struct rndis_wlan_encr_key key;
  1251. if (is_wpa_key(priv, key_idx))
  1252. return 0;
  1253. key = priv->encr_keys[key_idx];
  1254. netdev_dbg(usbdev->net, "%s(): %i:%i\n", __func__, key_idx, key.len);
  1255. if (key.len == 0)
  1256. return 0;
  1257. return add_wep_key(usbdev, key.material, key.len, key_idx);
  1258. }
  1259. static void restore_keys(struct usbnet *usbdev)
  1260. {
  1261. int i;
  1262. for (i = 0; i < 4; i++)
  1263. restore_key(usbdev, i);
  1264. }
  1265. static void clear_key(struct rndis_wlan_private *priv, u8 idx)
  1266. {
  1267. memset(&priv->encr_keys[idx], 0, sizeof(priv->encr_keys[idx]));
  1268. }
  1269. /* remove_key is for both wep and wpa */
  1270. static int remove_key(struct usbnet *usbdev, u8 index, const u8 *bssid)
  1271. {
  1272. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1273. struct ndis_80211_remove_key remove_key;
  1274. __le32 keyindex;
  1275. bool is_wpa;
  1276. int ret;
  1277. if (index >= RNDIS_WLAN_NUM_KEYS)
  1278. return -ENOENT;
  1279. if (priv->encr_keys[index].len == 0)
  1280. return 0;
  1281. is_wpa = is_wpa_key(priv, index);
  1282. netdev_dbg(usbdev->net, "%s(): %i:%s:%i\n",
  1283. __func__, index, is_wpa ? "wpa" : "wep",
  1284. priv->encr_keys[index].len);
  1285. clear_key(priv, index);
  1286. if (is_wpa) {
  1287. remove_key.size = cpu_to_le32(sizeof(remove_key));
  1288. remove_key.index = cpu_to_le32(index);
  1289. if (bssid) {
  1290. /* pairwise key */
  1291. if (!is_broadcast_ether_addr(bssid))
  1292. remove_key.index |=
  1293. NDIS_80211_ADDKEY_PAIRWISE_KEY;
  1294. memcpy(remove_key.bssid, bssid,
  1295. sizeof(remove_key.bssid));
  1296. } else
  1297. memset(remove_key.bssid, 0xff,
  1298. sizeof(remove_key.bssid));
  1299. ret = rndis_set_oid(usbdev,
  1300. RNDIS_OID_802_11_REMOVE_KEY,
  1301. &remove_key, sizeof(remove_key));
  1302. if (ret != 0)
  1303. return ret;
  1304. } else {
  1305. keyindex = cpu_to_le32(index);
  1306. ret = rndis_set_oid(usbdev,
  1307. RNDIS_OID_802_11_REMOVE_WEP,
  1308. &keyindex, sizeof(keyindex));
  1309. if (ret != 0) {
  1310. netdev_warn(usbdev->net,
  1311. "removing encryption key %d failed (%08X)\n",
  1312. index, ret);
  1313. return ret;
  1314. }
  1315. }
  1316. /* if it is transmit key, disable encryption */
  1317. if (index == priv->encr_tx_key_index)
  1318. set_encr_mode(usbdev, RNDIS_WLAN_ALG_NONE, RNDIS_WLAN_ALG_NONE);
  1319. return 0;
  1320. }
  1321. static void set_multicast_list(struct usbnet *usbdev)
  1322. {
  1323. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1324. struct netdev_hw_addr *ha;
  1325. __le32 filter, basefilter;
  1326. int ret;
  1327. char *mc_addrs = NULL;
  1328. int mc_count;
  1329. basefilter = filter = cpu_to_le32(RNDIS_PACKET_TYPE_DIRECTED |
  1330. RNDIS_PACKET_TYPE_BROADCAST);
  1331. if (usbdev->net->flags & IFF_PROMISC) {
  1332. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_PROMISCUOUS |
  1333. RNDIS_PACKET_TYPE_ALL_LOCAL);
  1334. } else if (usbdev->net->flags & IFF_ALLMULTI) {
  1335. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
  1336. }
  1337. if (filter != basefilter)
  1338. goto set_filter;
  1339. /*
  1340. * mc_list should be accessed holding the lock, so copy addresses to
  1341. * local buffer first.
  1342. */
  1343. netif_addr_lock_bh(usbdev->net);
  1344. mc_count = netdev_mc_count(usbdev->net);
  1345. if (mc_count > priv->multicast_size) {
  1346. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
  1347. } else if (mc_count) {
  1348. int i = 0;
  1349. mc_addrs = kmalloc_array(mc_count, ETH_ALEN, GFP_ATOMIC);
  1350. if (!mc_addrs) {
  1351. netif_addr_unlock_bh(usbdev->net);
  1352. return;
  1353. }
  1354. netdev_for_each_mc_addr(ha, usbdev->net)
  1355. memcpy(mc_addrs + i++ * ETH_ALEN,
  1356. ha->addr, ETH_ALEN);
  1357. }
  1358. netif_addr_unlock_bh(usbdev->net);
  1359. if (filter != basefilter)
  1360. goto set_filter;
  1361. if (mc_count) {
  1362. ret = rndis_set_oid(usbdev,
  1363. RNDIS_OID_802_3_MULTICAST_LIST,
  1364. mc_addrs, mc_count * ETH_ALEN);
  1365. kfree(mc_addrs);
  1366. if (ret == 0)
  1367. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_MULTICAST);
  1368. else
  1369. filter |= cpu_to_le32(RNDIS_PACKET_TYPE_ALL_MULTICAST);
  1370. netdev_dbg(usbdev->net, "RNDIS_OID_802_3_MULTICAST_LIST(%d, max: %d) -> %d\n",
  1371. mc_count, priv->multicast_size, ret);
  1372. }
  1373. set_filter:
  1374. ret = rndis_set_oid(usbdev, RNDIS_OID_GEN_CURRENT_PACKET_FILTER, &filter,
  1375. sizeof(filter));
  1376. if (ret < 0) {
  1377. netdev_warn(usbdev->net, "couldn't set packet filter: %08x\n",
  1378. le32_to_cpu(filter));
  1379. }
  1380. netdev_dbg(usbdev->net, "RNDIS_OID_GEN_CURRENT_PACKET_FILTER(%08x) -> %d\n",
  1381. le32_to_cpu(filter), ret);
  1382. }
  1383. #ifdef DEBUG
  1384. static void debug_print_pmkids(struct usbnet *usbdev,
  1385. struct ndis_80211_pmkid *pmkids,
  1386. const char *func_str)
  1387. {
  1388. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1389. int i, len, count, max_pmkids, entry_len;
  1390. max_pmkids = priv->wdev.wiphy->max_num_pmkids;
  1391. len = le32_to_cpu(pmkids->length);
  1392. count = le32_to_cpu(pmkids->bssid_info_count);
  1393. entry_len = (count > 0) ? (len - sizeof(*pmkids)) / count : -1;
  1394. netdev_dbg(usbdev->net, "%s(): %d PMKIDs (data len: %d, entry len: "
  1395. "%d)\n", func_str, count, len, entry_len);
  1396. if (count > max_pmkids)
  1397. count = max_pmkids;
  1398. for (i = 0; i < count; i++) {
  1399. u32 *tmp = (u32 *)pmkids->bssid_info[i].pmkid;
  1400. netdev_dbg(usbdev->net, "%s(): bssid: %pM, "
  1401. "pmkid: %08X:%08X:%08X:%08X\n",
  1402. func_str, pmkids->bssid_info[i].bssid,
  1403. cpu_to_be32(tmp[0]), cpu_to_be32(tmp[1]),
  1404. cpu_to_be32(tmp[2]), cpu_to_be32(tmp[3]));
  1405. }
  1406. }
  1407. #else
  1408. static void debug_print_pmkids(struct usbnet *usbdev,
  1409. struct ndis_80211_pmkid *pmkids,
  1410. const char *func_str)
  1411. {
  1412. return;
  1413. }
  1414. #endif
  1415. static struct ndis_80211_pmkid *get_device_pmkids(struct usbnet *usbdev)
  1416. {
  1417. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1418. struct ndis_80211_pmkid *pmkids;
  1419. int len, ret, max_pmkids;
  1420. max_pmkids = priv->wdev.wiphy->max_num_pmkids;
  1421. len = struct_size(pmkids, bssid_info, max_pmkids);
  1422. pmkids = kzalloc(len, GFP_KERNEL);
  1423. if (!pmkids)
  1424. return ERR_PTR(-ENOMEM);
  1425. pmkids->length = cpu_to_le32(len);
  1426. pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
  1427. ret = rndis_query_oid(usbdev, RNDIS_OID_802_11_PMKID,
  1428. pmkids, &len);
  1429. if (ret < 0) {
  1430. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d)"
  1431. " -> %d\n", __func__, len, max_pmkids, ret);
  1432. kfree(pmkids);
  1433. return ERR_PTR(ret);
  1434. }
  1435. if (le32_to_cpu(pmkids->bssid_info_count) > max_pmkids)
  1436. pmkids->bssid_info_count = cpu_to_le32(max_pmkids);
  1437. debug_print_pmkids(usbdev, pmkids, __func__);
  1438. return pmkids;
  1439. }
  1440. static int set_device_pmkids(struct usbnet *usbdev,
  1441. struct ndis_80211_pmkid *pmkids)
  1442. {
  1443. int ret, len, num_pmkids;
  1444. num_pmkids = le32_to_cpu(pmkids->bssid_info_count);
  1445. len = struct_size(pmkids, bssid_info, num_pmkids);
  1446. pmkids->length = cpu_to_le32(len);
  1447. debug_print_pmkids(usbdev, pmkids, __func__);
  1448. ret = rndis_set_oid(usbdev, RNDIS_OID_802_11_PMKID, pmkids,
  1449. le32_to_cpu(pmkids->length));
  1450. if (ret < 0) {
  1451. netdev_dbg(usbdev->net, "%s(): RNDIS_OID_802_11_PMKID(%d, %d) -> %d"
  1452. "\n", __func__, len, num_pmkids, ret);
  1453. }
  1454. kfree(pmkids);
  1455. return ret;
  1456. }
  1457. static struct ndis_80211_pmkid *remove_pmkid(struct usbnet *usbdev,
  1458. struct ndis_80211_pmkid *pmkids,
  1459. struct cfg80211_pmksa *pmksa,
  1460. int max_pmkids)
  1461. {
  1462. int i, err;
  1463. unsigned int count;
  1464. count = le32_to_cpu(pmkids->bssid_info_count);
  1465. if (count > max_pmkids)
  1466. count = max_pmkids;
  1467. for (i = 0; i < count; i++)
  1468. if (ether_addr_equal(pmkids->bssid_info[i].bssid,
  1469. pmksa->bssid))
  1470. break;
  1471. /* pmkid not found */
  1472. if (i == count) {
  1473. netdev_dbg(usbdev->net, "%s(): bssid not found (%pM)\n",
  1474. __func__, pmksa->bssid);
  1475. err = -ENOENT;
  1476. goto error;
  1477. }
  1478. for (; i + 1 < count; i++)
  1479. pmkids->bssid_info[i] = pmkids->bssid_info[i + 1];
  1480. count--;
  1481. pmkids->length = cpu_to_le32(struct_size(pmkids, bssid_info, count));
  1482. pmkids->bssid_info_count = cpu_to_le32(count);
  1483. return pmkids;
  1484. error:
  1485. kfree(pmkids);
  1486. return ERR_PTR(err);
  1487. }
  1488. static struct ndis_80211_pmkid *update_pmkid(struct usbnet *usbdev,
  1489. struct ndis_80211_pmkid *pmkids,
  1490. struct cfg80211_pmksa *pmksa,
  1491. int max_pmkids)
  1492. {
  1493. struct ndis_80211_pmkid *new_pmkids;
  1494. int i, err, newlen;
  1495. unsigned int count;
  1496. count = le32_to_cpu(pmkids->bssid_info_count);
  1497. if (count > max_pmkids)
  1498. count = max_pmkids;
  1499. /* update with new pmkid */
  1500. for (i = 0; i < count; i++) {
  1501. if (!ether_addr_equal(pmkids->bssid_info[i].bssid,
  1502. pmksa->bssid))
  1503. continue;
  1504. memcpy(pmkids->bssid_info[i].pmkid, pmksa->pmkid,
  1505. WLAN_PMKID_LEN);
  1506. return pmkids;
  1507. }
  1508. /* out of space, return error */
  1509. if (i == max_pmkids) {
  1510. netdev_dbg(usbdev->net, "%s(): out of space\n", __func__);
  1511. err = -ENOSPC;
  1512. goto error;
  1513. }
  1514. /* add new pmkid */
  1515. newlen = struct_size(pmkids, bssid_info, count + 1);
  1516. new_pmkids = krealloc(pmkids, newlen, GFP_KERNEL);
  1517. if (!new_pmkids) {
  1518. err = -ENOMEM;
  1519. goto error;
  1520. }
  1521. pmkids = new_pmkids;
  1522. pmkids->length = cpu_to_le32(newlen);
  1523. pmkids->bssid_info_count = cpu_to_le32(count + 1);
  1524. memcpy(pmkids->bssid_info[count].bssid, pmksa->bssid, ETH_ALEN);
  1525. memcpy(pmkids->bssid_info[count].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
  1526. return pmkids;
  1527. error:
  1528. kfree(pmkids);
  1529. return ERR_PTR(err);
  1530. }
  1531. /*
  1532. * cfg80211 ops
  1533. */
  1534. static int rndis_change_virtual_intf(struct wiphy *wiphy,
  1535. struct net_device *dev,
  1536. enum nl80211_iftype type,
  1537. struct vif_params *params)
  1538. {
  1539. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1540. struct usbnet *usbdev = priv->usbdev;
  1541. int mode;
  1542. switch (type) {
  1543. case NL80211_IFTYPE_ADHOC:
  1544. mode = NDIS_80211_INFRA_ADHOC;
  1545. break;
  1546. case NL80211_IFTYPE_STATION:
  1547. mode = NDIS_80211_INFRA_INFRA;
  1548. break;
  1549. default:
  1550. return -EINVAL;
  1551. }
  1552. priv->wdev.iftype = type;
  1553. return set_infra_mode(usbdev, mode);
  1554. }
  1555. static int rndis_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  1556. {
  1557. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1558. struct usbnet *usbdev = priv->usbdev;
  1559. int err;
  1560. if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
  1561. err = set_frag_threshold(usbdev, wiphy->frag_threshold);
  1562. if (err < 0)
  1563. return err;
  1564. }
  1565. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  1566. err = set_rts_threshold(usbdev, wiphy->rts_threshold);
  1567. if (err < 0)
  1568. return err;
  1569. }
  1570. return 0;
  1571. }
  1572. static int rndis_set_tx_power(struct wiphy *wiphy,
  1573. struct wireless_dev *wdev,
  1574. enum nl80211_tx_power_setting type,
  1575. int mbm)
  1576. {
  1577. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1578. struct usbnet *usbdev = priv->usbdev;
  1579. netdev_dbg(usbdev->net, "%s(): type:0x%x mbm:%i\n",
  1580. __func__, type, mbm);
  1581. if (mbm < 0 || (mbm % 100))
  1582. return -ENOTSUPP;
  1583. /* Device doesn't support changing txpower after initialization, only
  1584. * turn off/on radio. Support 'auto' mode and setting same dBm that is
  1585. * currently used.
  1586. */
  1587. if (type == NL80211_TX_POWER_AUTOMATIC ||
  1588. MBM_TO_DBM(mbm) == get_bcm4320_power_dbm(priv)) {
  1589. if (!priv->radio_on)
  1590. disassociate(usbdev, true); /* turn on radio */
  1591. return 0;
  1592. }
  1593. return -ENOTSUPP;
  1594. }
  1595. static int rndis_get_tx_power(struct wiphy *wiphy,
  1596. struct wireless_dev *wdev,
  1597. int *dbm)
  1598. {
  1599. struct rndis_wlan_private *priv = wiphy_priv(wiphy);
  1600. struct usbnet *usbdev = priv->usbdev;
  1601. *dbm = get_bcm4320_power_dbm(priv);
  1602. netdev_dbg(usbdev->net, "%s(): dbm:%i\n", __func__, *dbm);
  1603. return 0;
  1604. }
  1605. #define SCAN_DELAY_JIFFIES (6 * HZ)
  1606. static int rndis_scan(struct wiphy *wiphy,
  1607. struct cfg80211_scan_request *request)
  1608. {
  1609. struct net_device *dev = request->wdev->netdev;
  1610. struct usbnet *usbdev = netdev_priv(dev);
  1611. struct rndis_wlan_private *priv = get_rndis_wlan_priv(usbdev);
  1612. int ret;
  1613. int delay = SCAN_DE