cfg80211.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * Intel Wireless Multicomm 3200 WiFi driver
  3. *
  4. * Copyright (C) 2009 Intel Corporation <ilw@linux.intel.com>
  5. * Samuel Ortiz <samuel.ortiz@intel.com>
  6. * Zhu Yi <yi.zhu@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/sched.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/wireless.h>
  28. #include <linux/ieee80211.h>
  29. #include <linux/slab.h>
  30. #include <net/cfg80211.h>
  31. #include "iwm.h"
  32. #include "commands.h"
  33. #include "cfg80211.h"
  34. #include "debug.h"
  35. #define RATETAB_ENT(_rate, _rateid, _flags) \
  36. { \
  37. .bitrate = (_rate), \
  38. .hw_value = (_rateid), \
  39. .flags = (_flags), \
  40. }
  41. #define CHAN2G(_channel, _freq, _flags) { \
  42. .band = IEEE80211_BAND_2GHZ, \
  43. .center_freq = (_freq), \
  44. .hw_value = (_channel), \
  45. .flags = (_flags), \
  46. .max_antenna_gain = 0, \
  47. .max_power = 30, \
  48. }
  49. #define CHAN5G(_channel, _flags) { \
  50. .band = IEEE80211_BAND_5GHZ, \
  51. .center_freq = 5000 + (5 * (_channel)), \
  52. .hw_value = (_channel), \
  53. .flags = (_flags), \
  54. .max_antenna_gain = 0, \
  55. .max_power = 30, \
  56. }
  57. static struct ieee80211_rate iwm_rates[] = {
  58. RATETAB_ENT(10, 0x1, 0),
  59. RATETAB_ENT(20, 0x2, 0),
  60. RATETAB_ENT(55, 0x4, 0),
  61. RATETAB_ENT(110, 0x8, 0),
  62. RATETAB_ENT(60, 0x10, 0),
  63. RATETAB_ENT(90, 0x20, 0),
  64. RATETAB_ENT(120, 0x40, 0),
  65. RATETAB_ENT(180, 0x80, 0),
  66. RATETAB_ENT(240, 0x100, 0),
  67. RATETAB_ENT(360, 0x200, 0),
  68. RATETAB_ENT(480, 0x400, 0),
  69. RATETAB_ENT(540, 0x800, 0),
  70. };
  71. #define iwm_a_rates (iwm_rates + 4)
  72. #define iwm_a_rates_size 8
  73. #define iwm_g_rates (iwm_rates + 0)
  74. #define iwm_g_rates_size 12
  75. static struct ieee80211_channel iwm_2ghz_channels[] = {
  76. CHAN2G(1, 2412, 0),
  77. CHAN2G(2, 2417, 0),
  78. CHAN2G(3, 2422, 0),
  79. CHAN2G(4, 2427, 0),
  80. CHAN2G(5, 2432, 0),
  81. CHAN2G(6, 2437, 0),
  82. CHAN2G(7, 2442, 0),
  83. CHAN2G(8, 2447, 0),
  84. CHAN2G(9, 2452, 0),
  85. CHAN2G(10, 2457, 0),
  86. CHAN2G(11, 2462, 0),
  87. CHAN2G(12, 2467, 0),
  88. CHAN2G(13, 2472, 0),
  89. CHAN2G(14, 2484, 0),
  90. };
  91. static struct ieee80211_channel iwm_5ghz_a_channels[] = {
  92. CHAN5G(34, 0), CHAN5G(36, 0),
  93. CHAN5G(38, 0), CHAN5G(40, 0),
  94. CHAN5G(42, 0), CHAN5G(44, 0),
  95. CHAN5G(46, 0), CHAN5G(48, 0),
  96. CHAN5G(52, 0), CHAN5G(56, 0),
  97. CHAN5G(60, 0), CHAN5G(64, 0),
  98. CHAN5G(100, 0), CHAN5G(104, 0),
  99. CHAN5G(108, 0), CHAN5G(112, 0),
  100. CHAN5G(116, 0), CHAN5G(120, 0),
  101. CHAN5G(124, 0), CHAN5G(128, 0),
  102. CHAN5G(132, 0), CHAN5G(136, 0),
  103. CHAN5G(140, 0), CHAN5G(149, 0),
  104. CHAN5G(153, 0), CHAN5G(157, 0),
  105. CHAN5G(161, 0), CHAN5G(165, 0),
  106. CHAN5G(184, 0), CHAN5G(188, 0),
  107. CHAN5G(192, 0), CHAN5G(196, 0),
  108. CHAN5G(200, 0), CHAN5G(204, 0),
  109. CHAN5G(208, 0), CHAN5G(212, 0),
  110. CHAN5G(216, 0),
  111. };
  112. static struct ieee80211_supported_band iwm_band_2ghz = {
  113. .channels = iwm_2ghz_channels,
  114. .n_channels = ARRAY_SIZE(iwm_2ghz_channels),
  115. .bitrates = iwm_g_rates,
  116. .n_bitrates = iwm_g_rates_size,
  117. };
  118. static struct ieee80211_supported_band iwm_band_5ghz = {
  119. .channels = iwm_5ghz_a_channels,
  120. .n_channels = ARRAY_SIZE(iwm_5ghz_a_channels),
  121. .bitrates = iwm_a_rates,
  122. .n_bitrates = iwm_a_rates_size,
  123. };
  124. static int iwm_key_init(struct iwm_key *key, u8 key_index,
  125. const u8 *mac_addr, struct key_params *params)
  126. {
  127. key->hdr.key_idx = key_index;
  128. if (!mac_addr || is_broadcast_ether_addr(mac_addr)) {
  129. key->hdr.multicast = 1;
  130. memset(key->hdr.mac, 0xff, ETH_ALEN);
  131. } else {
  132. key->hdr.multicast = 0;
  133. memcpy(key->hdr.mac, mac_addr, ETH_ALEN);
  134. }
  135. if (params) {
  136. if (params->key_len > WLAN_MAX_KEY_LEN ||
  137. params->seq_len > IW_ENCODE_SEQ_MAX_SIZE)
  138. return -EINVAL;
  139. key->cipher = params->cipher;
  140. key->key_len = params->key_len;
  141. key->seq_len = params->seq_len;
  142. memcpy(key->key, params->key, key->key_len);
  143. memcpy(key->seq, params->seq, key->seq_len);
  144. }
  145. return 0;
  146. }
  147. static int iwm_cfg80211_add_key(struct wiphy *wiphy, struct net_device *ndev,
  148. u8 key_index, bool pairwise, const u8 *mac_addr,
  149. struct key_params *params)
  150. {
  151. struct iwm_priv *iwm = ndev_to_iwm(ndev);
  152. struct iwm_key *key = &iwm->keys[key_index];
  153. int ret;
  154. IWM_DBG_WEXT(iwm, DBG, "Adding key for %pM\n", mac_addr);
  155. memset(key, 0, sizeof(struct iwm_key));
  156. ret = iwm_key_init(key, key_index, mac_addr, params);
  157. if (ret < 0) {
  158. IWM_ERR(iwm, "Invalid key_params\n");
  159. return ret;
  160. }
  161. return iwm_set_key(iwm, 0, key);
  162. }
  163. static int iwm_cfg80211_get_key(struct wiphy *wiphy, struct net_device *ndev,
  164. u8 key_index, bool pairwise, const u8 *mac_addr,
  165. void *cookie,
  166. void (*callback)(void *cookie,
  167. struct key_params*))
  168. {
  169. struct iwm_priv *iwm = ndev_to_iwm(ndev);
  170. struct iwm_key *key;
  171. struct key_params params;
  172. IWM_DBG_WEXT(iwm, DBG, "Getting key %d\n", key_index);
  173. if (key_index >= IWM_NUM_KEYS)
  174. return -ENOENT;
  175. memset(&params, 0, sizeof(params));
  176. key = &iwm->keys[key_index];
  177. params.cipher = key->cipher;
  178. params.key_len = key->key_len;
  179. params.seq_len = key->seq_len;
  180. params.seq = key->seq;
  181. params.key = key->key;
  182. callback(cookie, &params);
  183. return key->key_len ? 0 : -ENOENT;
  184. }
  185. static int iwm_cfg80211_del_key(struct wiphy *wiphy, struct net_device *ndev,
  186. u8 key_index, bool pairwise, const u8 *mac_addr)
  187. {
  188. struct iwm_priv *iwm = ndev_to_iwm(ndev);
  189. struct iwm_key *key = &iwm->keys[key_index];
  190. if (!iwm->keys[key_index].key_len) {
  191. IWM_DBG_WEXT(iwm, DBG, "Key %d not used\n", key_index);
  192. return 0;
  193. }
  194. if (key_index == iwm->default_key)
  195. iwm->default_key = -1;
  196. return iwm_set_key(iwm, 1, key);
  197. }
  198. static int iwm_cfg80211_set_default_key(struct wiphy *wiphy,
  199. struct net_device *ndev,
  200. u8 key_index, bool unicast,
  201. bool multicast)
  202. {
  203. struct iwm_priv *iwm = ndev_to_iwm(ndev);
  204. IWM_DBG_WEXT(iwm, DBG, "Default key index is: %d\n", key_index);
  205. if (!iwm->keys[key_index].key_len) {
  206. IWM_ERR(iwm, "Key %d not used\n", key_index);
  207. return -EINVAL;
  208. }
  209. iwm->default_key = key_index;
  210. return iwm_set_tx_key(iwm, key_index);
  211. }
  212. static int iwm_cfg80211_get_station(struct wiphy *wiphy,
  213. struct net_device *ndev,
  214. u8 *mac, struct station_info *sinfo)
  215. {
  216. struct iwm_priv *iwm = ndev_to_iwm(ndev);
  217. if (memcmp(mac, iwm->bssid, ETH_ALEN))
  218. return -ENOENT;
  219. sinfo->filled |= STATION_INFO_TX_BITRATE;
  220. sinfo->txrate.legacy = iwm->rate * 10;
  221. if (test_bit(IWM_STATUS_ASSOCIATED, &iwm->status)) {
  222. sinfo->filled |= STATION_INFO_SIGNAL;
  223. sinfo->signal = iwm->wstats.qual.level;
  224. }
  225. return 0;
  226. }
  227. int iwm_cfg80211_inform_bss(struct iwm_priv *iwm)
  228. {
  229. struct wiphy *wiphy = iwm_to_wiphy(iwm);
  230. struct iwm_bss_info *bss;
  231. struct iwm_umac_notif_bss_info *umac_bss;
  232. struct ieee80211_mgmt *mgmt;
  233. struct ieee80211_channel *channel;
  234. struct ieee80211_supported_band *band;
  235. s32 signal;
  236. int freq;
  237. list_for_each_entry(bss, &iwm->bss_list, node) {
  238. umac_bss = bss->bss;
  239. mgmt = (struct ieee80211_mgmt *)(umac_bss->frame_buf);
  240. if (umac_bss->band == UMAC_BAND_2GHZ)
  241. band = wiphy->bands[IEEE80211_BAND_2GHZ];
  242. else if (umac_bss->band == UMAC_BAND_5GHZ)
  243. band = wiphy->bands[IEEE80211_BAND_5GHZ];
  244. else {
  245. IWM_ERR(iwm, "Invalid band: %d\n", umac_bss->band);
  246. return -EINVAL;
  247. }
  248. freq = ieee80211_channel_to_frequency(umac_bss->channel,
  249. band->band);
  250. channel = ieee80211_get_channel(wiphy, freq);
  251. signal = umac_bss->rssi * 100;
  252. if (!cfg80211_inform_bss_frame(wiphy, channel, mgmt,
  253. le16_to_cpu(umac_bss->frame_len),
  254. signal, GFP_KERNEL))
  255. return -EINVAL;
  256. }
  257. return 0;
  258. }
  259. static int iwm_cfg80211_change_iface(struct wiphy *wiphy,
  260. struct net_device *ndev,
  261. enum nl80211_iftype type, u32 *flags,
  262. struct vif_params *params)
  263. {
  264. struct wireless_dev *wdev;
  265. struct iwm_priv *iwm;
  266. u32 old_mode;
  267. wdev = ndev->ieee80211_ptr;
  268. iwm = ndev_to_iwm(ndev);
  269. old_mode = iwm->conf.mode;
  270. switch (type) {
  271. case NL80211_IFTYPE_STATION:
  272. iwm->conf.mode = UMAC_MODE_BSS;
  273. break;
  274. case NL80211_IFTYPE_ADHOC:
  275. iwm->conf.mode = UMAC_MODE_IBSS;
  276. break;
  277. default:
  278. return -EOPNOTSUPP;
  279. }
  280. wdev->iftype = type;
  281. if ((old_mode == iwm->conf.mode) || !iwm->umac_profile)
  282. return 0;
  283. iwm->umac_profile->mode = cpu_to_le32(iwm->conf.mode);
  284. if (iwm->umac_profile_active)
  285. iwm_invalidate_mlme_profile(iwm);
  286. return 0;
  287. }
  288. static int iwm_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
  289. struct cfg80211_scan_request *request)
  290. {
  291. struct iwm_priv *iwm = ndev_to_iwm(ndev);
  292. int ret;
  293. if (!test_bit(IWM_STATUS_READY, &iwm->status)) {
  294. IWM_ERR(iwm, "Scan while device is not ready\n");
  295. return -EIO;
  296. }
  297. if (test_bit(IWM_STATUS_SCANNING, &iwm->status)) {
  298. IWM_ERR(iwm, "Scanning already\n");
  299. return -EAGAIN;
  300. }
  301. if (test_bit(IWM_STATUS_SCAN_ABORTING, &iwm->status)) {
  302. IWM_ERR(iwm, "Scanning being aborted\n");
  303. return -EAGAIN;
  304. }
  305. set_bit(IWM_STATUS_SCANNING, &iwm->status);
  306. ret = iwm_scan_ssids(iwm, request->ssids, request->n_ssids);
  307. if (ret) {
  308. clear_bit(IWM_STATUS_SCANNING, &iwm->status);
  309. return ret;
  310. }
  311. iwm->scan_request = request;
  312. return 0;
  313. }
  314. static int iwm_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  315. {
  316. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  317. if (changed & WIPHY_PARAM_RTS_THRESHOLD &&
  318. (iwm->conf.rts_threshold != wiphy->rts_threshold)) {
  319. int ret;
  320. iwm->conf.rts_threshold = wiphy->rts_threshold;
  321. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  322. CFG_RTS_THRESHOLD,
  323. iwm->conf.rts_threshold);
  324. if (ret < 0)
  325. return ret;
  326. }
  327. if (changed & WIPHY_PARAM_FRAG_THRESHOLD &&
  328. (iwm->conf.frag_threshold != wiphy->frag_threshold)) {
  329. int ret;
  330. iwm->conf.frag_threshold = wiphy->frag_threshold;
  331. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_FA_CFG_FIX,
  332. CFG_FRAG_THRESHOLD,
  333. iwm->conf.frag_threshold);
  334. if (ret < 0)
  335. return ret;
  336. }
  337. return 0;
  338. }
  339. static int iwm_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  340. struct cfg80211_ibss_params *params)
  341. {
  342. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  343. struct ieee80211_channel *chan = params->channel;
  344. if (!test_bit(IWM_STATUS_READY, &iwm->status))
  345. return -EIO;
  346. /* UMAC doesn't support creating or joining an IBSS network
  347. * with specified bssid. */
  348. if (params->bssid)
  349. return -EOPNOTSUPP;
  350. iwm->channel = ieee80211_frequency_to_channel(chan->center_freq);
  351. iwm->umac_profile->ibss.band = chan->band;
  352. iwm->umac_profile->ibss.channel = iwm->channel;
  353. iwm->umac_profile->ssid.ssid_len = params->ssid_len;
  354. memcpy(iwm->umac_profile->ssid.ssid, params->ssid, params->ssid_len);
  355. return iwm_send_mlme_profile(iwm);
  356. }
  357. static int iwm_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  358. {
  359. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  360. if (iwm->umac_profile_active)
  361. return iwm_invalidate_mlme_profile(iwm);
  362. return 0;
  363. }
  364. static int iwm_set_auth_type(struct iwm_priv *iwm,
  365. enum nl80211_auth_type sme_auth_type)
  366. {
  367. u8 *auth_type = &iwm->umac_profile->sec.auth_type;
  368. switch (sme_auth_type) {
  369. case NL80211_AUTHTYPE_AUTOMATIC:
  370. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  371. IWM_DBG_WEXT(iwm, DBG, "OPEN auth\n");
  372. *auth_type = UMAC_AUTH_TYPE_OPEN;
  373. break;
  374. case NL80211_AUTHTYPE_SHARED_KEY:
  375. if (iwm->umac_profile->sec.flags &
  376. (UMAC_SEC_FLG_WPA_ON_MSK | UMAC_SEC_FLG_RSNA_ON_MSK)) {
  377. IWM_DBG_WEXT(iwm, DBG, "WPA auth alg\n");
  378. *auth_type = UMAC_AUTH_TYPE_RSNA_PSK;
  379. } else {
  380. IWM_DBG_WEXT(iwm, DBG, "WEP shared key auth alg\n");
  381. *auth_type = UMAC_AUTH_TYPE_LEGACY_PSK;
  382. }
  383. break;
  384. default:
  385. IWM_ERR(iwm, "Unsupported auth alg: 0x%x\n", sme_auth_type);
  386. return -ENOTSUPP;
  387. }
  388. return 0;
  389. }
  390. static int iwm_set_wpa_version(struct iwm_priv *iwm, u32 wpa_version)
  391. {
  392. IWM_DBG_WEXT(iwm, DBG, "wpa_version: %d\n", wpa_version);
  393. if (!wpa_version) {
  394. iwm->umac_profile->sec.flags = UMAC_SEC_FLG_LEGACY_PROFILE;
  395. return 0;
  396. }
  397. if (wpa_version & NL80211_WPA_VERSION_1)
  398. iwm->umac_profile->sec.flags = UMAC_SEC_FLG_WPA_ON_MSK;
  399. if (wpa_version & NL80211_WPA_VERSION_2)
  400. iwm->umac_profile->sec.flags = UMAC_SEC_FLG_RSNA_ON_MSK;
  401. return 0;
  402. }
  403. static int iwm_set_cipher(struct iwm_priv *iwm, u32 cipher, bool ucast)
  404. {
  405. u8 *profile_cipher = ucast ? &iwm->umac_profile->sec.ucast_cipher :
  406. &iwm->umac_profile->sec.mcast_cipher;
  407. if (!cipher) {
  408. *profile_cipher = UMAC_CIPHER_TYPE_NONE;
  409. return 0;
  410. }
  411. IWM_DBG_WEXT(iwm, DBG, "%ccast cipher is 0x%x\n", ucast ? 'u' : 'm',
  412. cipher);
  413. switch (cipher) {
  414. case IW_AUTH_CIPHER_NONE:
  415. *profile_cipher = UMAC_CIPHER_TYPE_NONE;
  416. break;
  417. case WLAN_CIPHER_SUITE_WEP40:
  418. *profile_cipher = UMAC_CIPHER_TYPE_WEP_40;
  419. break;
  420. case WLAN_CIPHER_SUITE_WEP104:
  421. *profile_cipher = UMAC_CIPHER_TYPE_WEP_104;
  422. break;
  423. case WLAN_CIPHER_SUITE_TKIP:
  424. *profile_cipher = UMAC_CIPHER_TYPE_TKIP;
  425. break;
  426. case WLAN_CIPHER_SUITE_CCMP:
  427. *profile_cipher = UMAC_CIPHER_TYPE_CCMP;
  428. break;
  429. default:
  430. IWM_ERR(iwm, "Unsupported cipher: 0x%x\n", cipher);
  431. return -ENOTSUPP;
  432. }
  433. return 0;
  434. }
  435. static int iwm_set_key_mgt(struct iwm_priv *iwm, u32 key_mgt)
  436. {
  437. u8 *auth_type = &iwm->umac_profile->sec.auth_type;
  438. IWM_DBG_WEXT(iwm, DBG, "key_mgt: 0x%x\n", key_mgt);
  439. if (key_mgt == WLAN_AKM_SUITE_8021X)
  440. *auth_type = UMAC_AUTH_TYPE_8021X;
  441. else if (key_mgt == WLAN_AKM_SUITE_PSK) {
  442. if (iwm->umac_profile->sec.flags &
  443. (UMAC_SEC_FLG_WPA_ON_MSK | UMAC_SEC_FLG_RSNA_ON_MSK))
  444. *auth_type = UMAC_AUTH_TYPE_RSNA_PSK;
  445. else
  446. *auth_type = UMAC_AUTH_TYPE_LEGACY_PSK;
  447. } else {
  448. IWM_ERR(iwm, "Invalid key mgt: 0x%x\n", key_mgt);
  449. return -EINVAL;
  450. }
  451. return 0;
  452. }
  453. static int iwm_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
  454. struct cfg80211_connect_params *sme)
  455. {
  456. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  457. struct ieee80211_channel *chan = sme->channel;
  458. struct key_params key_param;
  459. int ret;
  460. if (!test_bit(IWM_STATUS_READY, &iwm->status))
  461. return -EIO;
  462. if (!sme->ssid)
  463. return -EINVAL;
  464. if (iwm->umac_profile_active) {
  465. ret = iwm_invalidate_mlme_profile(iwm);
  466. if (ret) {
  467. IWM_ERR(iwm, "Couldn't invalidate profile\n");
  468. return ret;
  469. }
  470. }
  471. if (chan)
  472. iwm->channel =
  473. ieee80211_frequency_to_channel(chan->center_freq);
  474. iwm->umac_profile->ssid.ssid_len = sme->ssid_len;
  475. memcpy(iwm->umac_profile->ssid.ssid, sme->ssid, sme->ssid_len);
  476. if (sme->bssid) {
  477. IWM_DBG_WEXT(iwm, DBG, "BSSID: %pM\n", sme->bssid);
  478. memcpy(&iwm->umac_profile->bssid[0], sme->bssid, ETH_ALEN);
  479. iwm->umac_profile->bss_num = 1;
  480. } else {
  481. memset(&iwm->umac_profile->bssid[0], 0, ETH_ALEN);
  482. iwm->umac_profile->bss_num = 0;
  483. }
  484. ret = iwm_set_wpa_version(iwm, sme->crypto.wpa_versions);
  485. if (ret < 0)
  486. return ret;
  487. ret = iwm_set_auth_type(iwm, sme->auth_type);
  488. if (ret < 0)
  489. return ret;
  490. if (sme->crypto.n_ciphers_pairwise) {
  491. ret = iwm_set_cipher(iwm, sme->crypto.ciphers_pairwise[0],
  492. true);
  493. if (ret < 0)
  494. return ret;
  495. }
  496. ret = iwm_set_cipher(iwm, sme->crypto.cipher_group, false);
  497. if (ret < 0)
  498. return ret;
  499. if (sme->crypto.n_akm_suites) {
  500. ret = iwm_set_key_mgt(iwm, sme->crypto.akm_suites[0]);
  501. if (ret < 0)
  502. return ret;
  503. }
  504. /*
  505. * We save the WEP key in case we want to do shared authentication.
  506. * We have to do it so because UMAC will assert whenever it gets a
  507. * key before a profile.
  508. */
  509. if (sme->key) {
  510. key_param.key = kmemdup(sme->key, sme->key_len, GFP_KERNEL);
  511. if (key_param.key == NULL)
  512. return -ENOMEM;
  513. key_param.key_len = sme->key_len;
  514. key_param.seq_len = 0;
  515. key_param.cipher = sme->crypto.ciphers_pairwise[0];
  516. ret = iwm_key_init(&iwm->keys[sme->key_idx], sme->key_idx,
  517. NULL, &key_param);
  518. kfree(key_param.key);
  519. if (ret < 0) {
  520. IWM_ERR(iwm, "Invalid key_params\n");
  521. return ret;
  522. }
  523. iwm->default_key = sme->key_idx;
  524. }
  525. /* WPA and open AUTH type from wpa_s means WPS (a.k.a. WSC) */
  526. if ((iwm->umac_profile->sec.flags &
  527. (UMAC_SEC_FLG_WPA_ON_MSK | UMAC_SEC_FLG_RSNA_ON_MSK)) &&
  528. iwm->umac_profile->sec.auth_type == UMAC_AUTH_TYPE_OPEN) {
  529. iwm->umac_profile->sec.flags = UMAC_SEC_FLG_WSC_ON_MSK;
  530. }
  531. ret = iwm_send_mlme_profile(iwm);
  532. if (iwm->umac_profile->sec.auth_type != UMAC_AUTH_TYPE_LEGACY_PSK ||
  533. sme->key == NULL)
  534. return ret;
  535. /*
  536. * We want to do shared auth.
  537. * We need to actually set the key we previously cached,
  538. * and then tell the UMAC it's the default one.
  539. * That will trigger the auth+assoc UMAC machinery, and again,
  540. * this must be done after setting the profile.
  541. */
  542. ret = iwm_set_key(iwm, 0, &iwm->keys[sme->key_idx]);
  543. if (ret < 0)
  544. return ret;
  545. return iwm_set_tx_key(iwm, iwm->default_key);
  546. }
  547. static int iwm_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *dev,
  548. u16 reason_code)
  549. {
  550. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  551. IWM_DBG_WEXT(iwm, DBG, "Active: %d\n", iwm->umac_profile_active);
  552. if (iwm->umac_profile_active)
  553. iwm_invalidate_mlme_profile(iwm);
  554. return 0;
  555. }
  556. static int iwm_cfg80211_set_txpower(struct wiphy *wiphy,
  557. enum nl80211_tx_power_setting type, int mbm)
  558. {
  559. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  560. int ret;
  561. switch (type) {
  562. case NL80211_TX_POWER_AUTOMATIC:
  563. return 0;
  564. case NL80211_TX_POWER_FIXED:
  565. if (mbm < 0 || (mbm % 100))
  566. return -EOPNOTSUPP;
  567. if (!test_bit(IWM_STATUS_READY, &iwm->status))
  568. return 0;
  569. ret = iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  570. CFG_TX_PWR_LIMIT_USR,
  571. MBM_TO_DBM(mbm) * 2);
  572. if (ret < 0)
  573. return ret;
  574. return iwm_tx_power_trigger(iwm);
  575. default:
  576. IWM_ERR(iwm, "Unsupported power type: %d\n", type);
  577. return -EOPNOTSUPP;
  578. }
  579. return 0;
  580. }
  581. static int iwm_cfg80211_get_txpower(struct wiphy *wiphy, int *dbm)
  582. {
  583. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  584. *dbm = iwm->txpower >> 1;
  585. return 0;
  586. }
  587. static int iwm_cfg80211_set_power_mgmt(struct wiphy *wiphy,
  588. struct net_device *dev,
  589. bool enabled, int timeout)
  590. {
  591. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  592. u32 power_index;
  593. if (enabled)
  594. power_index = IWM_POWER_INDEX_DEFAULT;
  595. else
  596. power_index = IWM_POWER_INDEX_MIN;
  597. if (power_index == iwm->conf.power_index)
  598. return 0;
  599. iwm->conf.power_index = power_index;
  600. return iwm_umac_set_config_fix(iwm, UMAC_PARAM_TBL_CFG_FIX,
  601. CFG_POWER_INDEX, iwm->conf.power_index);
  602. }
  603. static int iwm_cfg80211_set_pmksa(struct wiphy *wiphy,
  604. struct net_device *netdev,
  605. struct cfg80211_pmksa *pmksa)
  606. {
  607. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  608. return iwm_send_pmkid_update(iwm, pmksa, IWM_CMD_PMKID_ADD);
  609. }
  610. static int iwm_cfg80211_del_pmksa(struct wiphy *wiphy,
  611. struct net_device *netdev,
  612. struct cfg80211_pmksa *pmksa)
  613. {
  614. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  615. return iwm_send_pmkid_update(iwm, pmksa, IWM_CMD_PMKID_DEL);
  616. }
  617. static int iwm_cfg80211_flush_pmksa(struct wiphy *wiphy,
  618. struct net_device *netdev)
  619. {
  620. struct iwm_priv *iwm = wiphy_to_iwm(wiphy);
  621. struct cfg80211_pmksa pmksa;
  622. memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
  623. return iwm_send_pmkid_update(iwm, &pmksa, IWM_CMD_PMKID_FLUSH);
  624. }
  625. static struct cfg80211_ops iwm_cfg80211_ops = {
  626. .change_virtual_intf = iwm_cfg80211_change_iface,
  627. .add_key = iwm_cfg80211_add_key,
  628. .get_key = iwm_cfg80211_get_key,
  629. .del_key = iwm_cfg80211_del_key,
  630. .set_default_key = iwm_cfg80211_set_default_key,
  631. .get_station = iwm_cfg80211_get_station,
  632. .scan = iwm_cfg80211_scan,
  633. .set_wiphy_params = iwm_cfg80211_set_wiphy_params,
  634. .connect = iwm_cfg80211_connect,
  635. .disconnect = iwm_cfg80211_disconnect,
  636. .join_ibss = iwm_cfg80211_join_ibss,
  637. .leave_ibss = iwm_cfg80211_leave_ibss,
  638. .set_tx_power = iwm_cfg80211_set_txpower,
  639. .get_tx_power = iwm_cfg80211_get_txpower,
  640. .set_power_mgmt = iwm_cfg80211_set_power_mgmt,
  641. .set_pmksa = iwm_cfg80211_set_pmksa,
  642. .del_pmksa = iwm_cfg80211_del_pmksa,
  643. .flush_pmksa = iwm_cfg80211_flush_pmksa,
  644. };
  645. static const u32 cipher_suites[] = {
  646. WLAN_CIPHER_SUITE_WEP40,
  647. WLAN_CIPHER_SUITE_WEP104,
  648. WLAN_CIPHER_SUITE_TKIP,
  649. WLAN_CIPHER_SUITE_CCMP,
  650. };
  651. struct wireless_dev *iwm_wdev_alloc(int sizeof_bus, struct device *dev)
  652. {
  653. int ret = 0;
  654. struct wireless_dev *wdev;
  655. /*
  656. * We're trying to have the following memory
  657. * layout:
  658. *
  659. * +-------------------------+
  660. * | struct wiphy |
  661. * +-------------------------+
  662. * | struct iwm_priv |
  663. * +-------------------------+
  664. * | bus private data |
  665. * | (e.g. iwm_priv_sdio) |
  666. * +-------------------------+
  667. *
  668. */
  669. wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
  670. if (!wdev) {
  671. dev_err(dev, "Couldn't allocate wireless device\n");
  672. return ERR_PTR(-ENOMEM);
  673. }
  674. wdev->wiphy = wiphy_new(&iwm_cfg80211_ops,
  675. sizeof(struct iwm_priv) + sizeof_bus);
  676. if (!wdev->wiphy) {
  677. dev_err(dev, "Couldn't allocate wiphy device\n");
  678. ret = -ENOMEM;
  679. goto out_err_new;
  680. }
  681. set_wiphy_dev(wdev->wiphy, dev);
  682. wdev->wiphy->max_scan_ssids = UMAC_WIFI_IF_PROBE_OPTION_MAX;
  683. wdev->wiphy->max_num_pmkids = UMAC_MAX_NUM_PMKIDS;
  684. wdev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  685. BIT(NL80211_IFTYPE_ADHOC);
  686. wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &iwm_band_2ghz;
  687. wdev->wiphy->bands[IEEE80211_BAND_5GHZ] = &iwm_band_5ghz;
  688. wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  689. wdev->wiphy->cipher_suites = cipher_suites;
  690. wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  691. ret = wiphy_register(wdev->wiphy);
  692. if (ret < 0) {
  693. dev_err(dev, "Couldn't register wiphy device\n");
  694. goto out_err_register;
  695. }
  696. return wdev;
  697. out_err_register:
  698. wiphy_free(wdev->wiphy);
  699. out_err_new:
  700. kfree(wdev);
  701. return ERR_PTR(ret);
  702. }
  703. void iwm_wdev_free(struct iwm_priv *iwm)
  704. {
  705. struct wireless_dev *wdev = iwm_to_wdev(iwm);
  706. if (!wdev)
  707. return;
  708. wiphy_unregister(wdev->wiphy);
  709. wiphy_free(wdev->wiphy);
  710. kfree(wdev);
  711. }