cfg80211.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. /*
  2. * Copyright (c) 2004-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "core.h"
  17. #include "cfg80211.h"
  18. #include "debug.h"
  19. #define RATETAB_ENT(_rate, _rateid, _flags) { \
  20. .bitrate = (_rate), \
  21. .flags = (_flags), \
  22. .hw_value = (_rateid), \
  23. }
  24. #define CHAN2G(_channel, _freq, _flags) { \
  25. .band = IEEE80211_BAND_2GHZ, \
  26. .hw_value = (_channel), \
  27. .center_freq = (_freq), \
  28. .flags = (_flags), \
  29. .max_antenna_gain = 0, \
  30. .max_power = 30, \
  31. }
  32. #define CHAN5G(_channel, _flags) { \
  33. .band = IEEE80211_BAND_5GHZ, \
  34. .hw_value = (_channel), \
  35. .center_freq = 5000 + (5 * (_channel)), \
  36. .flags = (_flags), \
  37. .max_antenna_gain = 0, \
  38. .max_power = 30, \
  39. }
  40. static struct ieee80211_rate ath6kl_rates[] = {
  41. RATETAB_ENT(10, 0x1, 0),
  42. RATETAB_ENT(20, 0x2, 0),
  43. RATETAB_ENT(55, 0x4, 0),
  44. RATETAB_ENT(110, 0x8, 0),
  45. RATETAB_ENT(60, 0x10, 0),
  46. RATETAB_ENT(90, 0x20, 0),
  47. RATETAB_ENT(120, 0x40, 0),
  48. RATETAB_ENT(180, 0x80, 0),
  49. RATETAB_ENT(240, 0x100, 0),
  50. RATETAB_ENT(360, 0x200, 0),
  51. RATETAB_ENT(480, 0x400, 0),
  52. RATETAB_ENT(540, 0x800, 0),
  53. };
  54. #define ath6kl_a_rates (ath6kl_rates + 4)
  55. #define ath6kl_a_rates_size 8
  56. #define ath6kl_g_rates (ath6kl_rates + 0)
  57. #define ath6kl_g_rates_size 12
  58. static struct ieee80211_channel ath6kl_2ghz_channels[] = {
  59. CHAN2G(1, 2412, 0),
  60. CHAN2G(2, 2417, 0),
  61. CHAN2G(3, 2422, 0),
  62. CHAN2G(4, 2427, 0),
  63. CHAN2G(5, 2432, 0),
  64. CHAN2G(6, 2437, 0),
  65. CHAN2G(7, 2442, 0),
  66. CHAN2G(8, 2447, 0),
  67. CHAN2G(9, 2452, 0),
  68. CHAN2G(10, 2457, 0),
  69. CHAN2G(11, 2462, 0),
  70. CHAN2G(12, 2467, 0),
  71. CHAN2G(13, 2472, 0),
  72. CHAN2G(14, 2484, 0),
  73. };
  74. static struct ieee80211_channel ath6kl_5ghz_a_channels[] = {
  75. CHAN5G(34, 0), CHAN5G(36, 0),
  76. CHAN5G(38, 0), CHAN5G(40, 0),
  77. CHAN5G(42, 0), CHAN5G(44, 0),
  78. CHAN5G(46, 0), CHAN5G(48, 0),
  79. CHAN5G(52, 0), CHAN5G(56, 0),
  80. CHAN5G(60, 0), CHAN5G(64, 0),
  81. CHAN5G(100, 0), CHAN5G(104, 0),
  82. CHAN5G(108, 0), CHAN5G(112, 0),
  83. CHAN5G(116, 0), CHAN5G(120, 0),
  84. CHAN5G(124, 0), CHAN5G(128, 0),
  85. CHAN5G(132, 0), CHAN5G(136, 0),
  86. CHAN5G(140, 0), CHAN5G(149, 0),
  87. CHAN5G(153, 0), CHAN5G(157, 0),
  88. CHAN5G(161, 0), CHAN5G(165, 0),
  89. CHAN5G(184, 0), CHAN5G(188, 0),
  90. CHAN5G(192, 0), CHAN5G(196, 0),
  91. CHAN5G(200, 0), CHAN5G(204, 0),
  92. CHAN5G(208, 0), CHAN5G(212, 0),
  93. CHAN5G(216, 0),
  94. };
  95. static struct ieee80211_supported_band ath6kl_band_2ghz = {
  96. .n_channels = ARRAY_SIZE(ath6kl_2ghz_channels),
  97. .channels = ath6kl_2ghz_channels,
  98. .n_bitrates = ath6kl_g_rates_size,
  99. .bitrates = ath6kl_g_rates,
  100. };
  101. static struct ieee80211_supported_band ath6kl_band_5ghz = {
  102. .n_channels = ARRAY_SIZE(ath6kl_5ghz_a_channels),
  103. .channels = ath6kl_5ghz_a_channels,
  104. .n_bitrates = ath6kl_a_rates_size,
  105. .bitrates = ath6kl_a_rates,
  106. };
  107. static int ath6kl_set_wpa_version(struct ath6kl *ar,
  108. enum nl80211_wpa_versions wpa_version)
  109. {
  110. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: %u\n", __func__, wpa_version);
  111. if (!wpa_version) {
  112. ar->auth_mode = NONE_AUTH;
  113. } else if (wpa_version & NL80211_WPA_VERSION_2) {
  114. ar->auth_mode = WPA2_AUTH;
  115. } else if (wpa_version & NL80211_WPA_VERSION_1) {
  116. ar->auth_mode = WPA_AUTH;
  117. } else {
  118. ath6kl_err("%s: %u not supported\n", __func__, wpa_version);
  119. return -ENOTSUPP;
  120. }
  121. return 0;
  122. }
  123. static int ath6kl_set_auth_type(struct ath6kl *ar,
  124. enum nl80211_auth_type auth_type)
  125. {
  126. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: 0x%x\n", __func__, auth_type);
  127. switch (auth_type) {
  128. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  129. ar->dot11_auth_mode = OPEN_AUTH;
  130. break;
  131. case NL80211_AUTHTYPE_SHARED_KEY:
  132. ar->dot11_auth_mode = SHARED_AUTH;
  133. break;
  134. case NL80211_AUTHTYPE_NETWORK_EAP:
  135. ar->dot11_auth_mode = LEAP_AUTH;
  136. break;
  137. case NL80211_AUTHTYPE_AUTOMATIC:
  138. ar->dot11_auth_mode = OPEN_AUTH;
  139. ar->auto_auth_stage = AUTH_OPEN_IN_PROGRESS;
  140. break;
  141. default:
  142. ath6kl_err("%s: 0x%x not spported\n", __func__, auth_type);
  143. return -ENOTSUPP;
  144. }
  145. return 0;
  146. }
  147. static int ath6kl_set_cipher(struct ath6kl *ar, u32 cipher, bool ucast)
  148. {
  149. u8 *ar_cipher = ucast ? &ar->prwise_crypto : &ar->grp_crypto;
  150. u8 *ar_cipher_len = ucast ? &ar->prwise_crypto_len : &ar->grp_crpto_len;
  151. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: cipher 0x%x, ucast %u\n",
  152. __func__, cipher, ucast);
  153. switch (cipher) {
  154. case 0:
  155. /* our own hack to use value 0 as no crypto used */
  156. *ar_cipher = NONE_CRYPT;
  157. *ar_cipher_len = 0;
  158. break;
  159. case WLAN_CIPHER_SUITE_WEP40:
  160. *ar_cipher = WEP_CRYPT;
  161. *ar_cipher_len = 5;
  162. break;
  163. case WLAN_CIPHER_SUITE_WEP104:
  164. *ar_cipher = WEP_CRYPT;
  165. *ar_cipher_len = 13;
  166. break;
  167. case WLAN_CIPHER_SUITE_TKIP:
  168. *ar_cipher = TKIP_CRYPT;
  169. *ar_cipher_len = 0;
  170. break;
  171. case WLAN_CIPHER_SUITE_CCMP:
  172. *ar_cipher = AES_CRYPT;
  173. *ar_cipher_len = 0;
  174. break;
  175. default:
  176. ath6kl_err("cipher 0x%x not supported\n", cipher);
  177. return -ENOTSUPP;
  178. }
  179. return 0;
  180. }
  181. static void ath6kl_set_key_mgmt(struct ath6kl *ar, u32 key_mgmt)
  182. {
  183. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: 0x%x\n", __func__, key_mgmt);
  184. if (key_mgmt == WLAN_AKM_SUITE_PSK) {
  185. if (ar->auth_mode == WPA_AUTH)
  186. ar->auth_mode = WPA_PSK_AUTH;
  187. else if (ar->auth_mode == WPA2_AUTH)
  188. ar->auth_mode = WPA2_PSK_AUTH;
  189. } else if (key_mgmt != WLAN_AKM_SUITE_8021X) {
  190. ar->auth_mode = NONE_AUTH;
  191. }
  192. }
  193. static bool ath6kl_cfg80211_ready(struct ath6kl *ar)
  194. {
  195. if (!test_bit(WMI_READY, &ar->flag)) {
  196. ath6kl_err("wmi is not ready\n");
  197. return false;
  198. }
  199. if (!test_bit(WLAN_ENABLED, &ar->flag)) {
  200. ath6kl_err("wlan disabled\n");
  201. return false;
  202. }
  203. return true;
  204. }
  205. static int ath6kl_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
  206. struct cfg80211_connect_params *sme)
  207. {
  208. struct ath6kl *ar = ath6kl_priv(dev);
  209. int status;
  210. ar->sme_state = SME_CONNECTING;
  211. if (!ath6kl_cfg80211_ready(ar))
  212. return -EIO;
  213. if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
  214. ath6kl_err("destroy in progress\n");
  215. return -EBUSY;
  216. }
  217. if (test_bit(SKIP_SCAN, &ar->flag) &&
  218. ((sme->channel && sme->channel->center_freq == 0) ||
  219. (sme->bssid && is_zero_ether_addr(sme->bssid)))) {
  220. ath6kl_err("SkipScan: channel or bssid invalid\n");
  221. return -EINVAL;
  222. }
  223. if (down_interruptible(&ar->sem)) {
  224. ath6kl_err("busy, couldn't get access\n");
  225. return -ERESTARTSYS;
  226. }
  227. if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
  228. ath6kl_err("busy, destroy in progress\n");
  229. up(&ar->sem);
  230. return -EBUSY;
  231. }
  232. if (ar->tx_pending[ath6kl_wmi_get_control_ep(ar->wmi)]) {
  233. /*
  234. * sleep until the command queue drains
  235. */
  236. wait_event_interruptible_timeout(ar->event_wq,
  237. ar->tx_pending[ath6kl_wmi_get_control_ep(ar->wmi)] == 0,
  238. WMI_TIMEOUT);
  239. if (signal_pending(current)) {
  240. ath6kl_err("cmd queue drain timeout\n");
  241. up(&ar->sem);
  242. return -EINTR;
  243. }
  244. }
  245. if (test_bit(CONNECTED, &ar->flag) &&
  246. ar->ssid_len == sme->ssid_len &&
  247. !memcmp(ar->ssid, sme->ssid, ar->ssid_len)) {
  248. ar->reconnect_flag = true;
  249. status = ath6kl_wmi_reconnect_cmd(ar->wmi, ar->req_bssid,
  250. ar->ch_hint);
  251. up(&ar->sem);
  252. if (status) {
  253. ath6kl_err("wmi_reconnect_cmd failed\n");
  254. return -EIO;
  255. }
  256. return 0;
  257. } else if (ar->ssid_len == sme->ssid_len &&
  258. !memcmp(ar->ssid, sme->ssid, ar->ssid_len)) {
  259. ath6kl_disconnect(ar);
  260. }
  261. memset(ar->ssid, 0, sizeof(ar->ssid));
  262. ar->ssid_len = sme->ssid_len;
  263. memcpy(ar->ssid, sme->ssid, sme->ssid_len);
  264. if (sme->channel)
  265. ar->ch_hint = sme->channel->center_freq;
  266. memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
  267. if (sme->bssid && !is_broadcast_ether_addr(sme->bssid))
  268. memcpy(ar->req_bssid, sme->bssid, sizeof(ar->req_bssid));
  269. ath6kl_set_wpa_version(ar, sme->crypto.wpa_versions);
  270. status = ath6kl_set_auth_type(ar, sme->auth_type);
  271. if (status) {
  272. up(&ar->sem);
  273. return status;
  274. }
  275. if (sme->crypto.n_ciphers_pairwise)
  276. ath6kl_set_cipher(ar, sme->crypto.ciphers_pairwise[0], true);
  277. else
  278. ath6kl_set_cipher(ar, 0, true);
  279. ath6kl_set_cipher(ar, sme->crypto.cipher_group, false);
  280. if (sme->crypto.n_akm_suites)
  281. ath6kl_set_key_mgmt(ar, sme->crypto.akm_suites[0]);
  282. if ((sme->key_len) &&
  283. (ar->auth_mode == NONE_AUTH) && (ar->prwise_crypto == WEP_CRYPT)) {
  284. struct ath6kl_key *key = NULL;
  285. if (sme->key_idx < WMI_MIN_KEY_INDEX ||
  286. sme->key_idx > WMI_MAX_KEY_INDEX) {
  287. ath6kl_err("key index %d out of bounds\n",
  288. sme->key_idx);
  289. up(&ar->sem);
  290. return -ENOENT;
  291. }
  292. key = &ar->keys[sme->key_idx];
  293. key->key_len = sme->key_len;
  294. memcpy(key->key, sme->key, key->key_len);
  295. key->cipher = ar->prwise_crypto;
  296. ar->def_txkey_index = sme->key_idx;
  297. ath6kl_wmi_addkey_cmd(ar->wmi, sme->key_idx,
  298. ar->prwise_crypto,
  299. GROUP_USAGE | TX_USAGE,
  300. key->key_len,
  301. NULL,
  302. key->key, KEY_OP_INIT_VAL, NULL,
  303. NO_SYNC_WMIFLAG);
  304. }
  305. if (!ar->usr_bss_filter) {
  306. if (ath6kl_wmi_bssfilter_cmd(ar->wmi, ALL_BSS_FILTER, 0) != 0) {
  307. ath6kl_err("couldn't set bss filtering\n");
  308. up(&ar->sem);
  309. return -EIO;
  310. }
  311. }
  312. ar->nw_type = ar->next_mode;
  313. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  314. "%s: connect called with authmode %d dot11 auth %d"
  315. " PW crypto %d PW crypto len %d GRP crypto %d"
  316. " GRP crypto len %d channel hint %u\n",
  317. __func__,
  318. ar->auth_mode, ar->dot11_auth_mode, ar->prwise_crypto,
  319. ar->prwise_crypto_len, ar->grp_crypto,
  320. ar->grp_crpto_len, ar->ch_hint);
  321. ar->reconnect_flag = 0;
  322. status = ath6kl_wmi_connect_cmd(ar->wmi, ar->nw_type,
  323. ar->dot11_auth_mode, ar->auth_mode,
  324. ar->prwise_crypto,
  325. ar->prwise_crypto_len,
  326. ar->grp_crypto, ar->grp_crpto_len,
  327. ar->ssid_len, ar->ssid,
  328. ar->req_bssid, ar->ch_hint,
  329. ar->connect_ctrl_flags);
  330. up(&ar->sem);
  331. if (status == -EINVAL) {
  332. memset(ar->ssid, 0, sizeof(ar->ssid));
  333. ar->ssid_len = 0;
  334. ath6kl_err("invalid request\n");
  335. return -ENOENT;
  336. } else if (status) {
  337. ath6kl_err("ath6kl_wmi_connect_cmd failed\n");
  338. return -EIO;
  339. }
  340. if ((!(ar->connect_ctrl_flags & CONNECT_DO_WPA_OFFLOAD)) &&
  341. ((ar->auth_mode == WPA_PSK_AUTH)
  342. || (ar->auth_mode == WPA2_PSK_AUTH))) {
  343. mod_timer(&ar->disconnect_timer,
  344. jiffies + msecs_to_jiffies(DISCON_TIMER_INTVAL));
  345. }
  346. ar->connect_ctrl_flags &= ~CONNECT_DO_WPA_OFFLOAD;
  347. set_bit(CONNECT_PEND, &ar->flag);
  348. return 0;
  349. }
  350. void ath6kl_cfg80211_connect_event(struct ath6kl *ar, u16 channel,
  351. u8 *bssid, u16 listen_intvl,
  352. u16 beacon_intvl,
  353. enum network_type nw_type,
  354. u8 beacon_ie_len, u8 assoc_req_len,
  355. u8 assoc_resp_len, u8 *assoc_info)
  356. {
  357. u16 size = 0;
  358. u16 capability = 0;
  359. struct cfg80211_bss *bss = NULL;
  360. struct ieee80211_mgmt *mgmt = NULL;
  361. struct ieee80211_channel *ibss_ch = NULL;
  362. s32 signal = 50 * 100;
  363. u8 ie_buf_len = 0;
  364. unsigned char ie_buf[256];
  365. unsigned char *ptr_ie_buf = ie_buf;
  366. unsigned char *ieeemgmtbuf = NULL;
  367. u8 source_mac[ETH_ALEN];
  368. u16 capa_mask;
  369. u16 capa_val;
  370. /* capinfo + listen interval */
  371. u8 assoc_req_ie_offset = sizeof(u16) + sizeof(u16);
  372. /* capinfo + status code + associd */
  373. u8 assoc_resp_ie_offset = sizeof(u16) + sizeof(u16) + sizeof(u16);
  374. u8 *assoc_req_ie = assoc_info + beacon_ie_len + assoc_req_ie_offset;
  375. u8 *assoc_resp_ie = assoc_info + beacon_ie_len + assoc_req_len +
  376. assoc_resp_ie_offset;
  377. assoc_req_len -= assoc_req_ie_offset;
  378. assoc_resp_len -= assoc_resp_ie_offset;
  379. ar->auto_auth_stage = AUTH_IDLE;
  380. if (nw_type & ADHOC_NETWORK) {
  381. if (ar->wdev->iftype != NL80211_IFTYPE_ADHOC) {
  382. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  383. "%s: ath6k not in ibss mode\n", __func__);
  384. return;
  385. }
  386. }
  387. if (nw_type & INFRA_NETWORK) {
  388. if (ar->wdev->iftype != NL80211_IFTYPE_STATION) {
  389. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  390. "%s: ath6k not in station mode\n", __func__);
  391. return;
  392. }
  393. }
  394. if (nw_type & ADHOC_NETWORK) {
  395. capa_mask = WLAN_CAPABILITY_IBSS;
  396. capa_val = WLAN_CAPABILITY_IBSS;
  397. } else {
  398. capa_mask = WLAN_CAPABILITY_ESS;
  399. capa_val = WLAN_CAPABILITY_ESS;
  400. }
  401. /* Before informing the join/connect event, make sure that
  402. * bss entry is present in scan list, if it not present
  403. * construct and insert into scan list, otherwise that
  404. * event will be dropped on the way by cfg80211, due to
  405. * this keys will not be plumbed in case of WEP and
  406. * application will not be aware of join/connect status. */
  407. bss = cfg80211_get_bss(ar->wdev->wiphy, NULL, bssid,
  408. ar->wdev->ssid, ar->wdev->ssid_len,
  409. capa_mask, capa_val);
  410. /*
  411. * Earlier we were updating the cfg about bss by making a beacon frame
  412. * only if the entry for bss is not there. This can have some issue if
  413. * ROAM event is generated and a heavy traffic is ongoing. The ROAM
  414. * event is handled through a work queue and by the time it really gets
  415. * handled, BSS would have been aged out. So it is better to update the
  416. * cfg about BSS irrespective of its entry being present right now or
  417. * not.
  418. */
  419. if (nw_type & ADHOC_NETWORK) {
  420. /* construct 802.11 mgmt beacon */
  421. if (ptr_ie_buf) {
  422. *ptr_ie_buf++ = WLAN_EID_SSID;
  423. *ptr_ie_buf++ = ar->ssid_len;
  424. memcpy(ptr_ie_buf, ar->ssid, ar->ssid_len);
  425. ptr_ie_buf += ar->ssid_len;
  426. *ptr_ie_buf++ = WLAN_EID_IBSS_PARAMS;
  427. *ptr_ie_buf++ = 2; /* length */
  428. *ptr_ie_buf++ = 0; /* ATIM window */
  429. *ptr_ie_buf++ = 0; /* ATIM window */
  430. /* TODO: update ibss params and include supported rates,
  431. * DS param set, extened support rates, wmm. */
  432. ie_buf_len = ptr_ie_buf - ie_buf;
  433. }
  434. capability |= WLAN_CAPABILITY_IBSS;
  435. if (ar->prwise_crypto == WEP_CRYPT)
  436. capability |= WLAN_CAPABILITY_PRIVACY;
  437. memcpy(source_mac, ar->net_dev->dev_addr, ETH_ALEN);
  438. ptr_ie_buf = ie_buf;
  439. } else {
  440. capability = *(u16 *) (&assoc_info[beacon_ie_len]);
  441. memcpy(source_mac, bssid, ETH_ALEN);
  442. ptr_ie_buf = assoc_req_ie;
  443. ie_buf_len = assoc_req_len;
  444. }
  445. size = offsetof(struct ieee80211_mgmt, u)
  446. + sizeof(mgmt->u.beacon)
  447. + ie_buf_len;
  448. ieeemgmtbuf = kzalloc(size, GFP_ATOMIC);
  449. if (!ieeemgmtbuf) {
  450. ath6kl_err("ieee mgmt buf alloc error\n");
  451. cfg80211_put_bss(bss);
  452. return;
  453. }
  454. mgmt = (struct ieee80211_mgmt *)ieeemgmtbuf;
  455. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  456. IEEE80211_STYPE_BEACON);
  457. memset(mgmt->da, 0xff, ETH_ALEN); /* broadcast addr */
  458. memcpy(mgmt->sa, source_mac, ETH_ALEN);
  459. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  460. mgmt->u.beacon.beacon_int = cpu_to_le16(beacon_intvl);
  461. mgmt->u.beacon.capab_info = cpu_to_le16(capability);
  462. memcpy(mgmt->u.beacon.variable, ptr_ie_buf, ie_buf_len);
  463. ibss_ch = ieee80211_get_channel(ar->wdev->wiphy, (int)channel);
  464. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  465. "%s: inform bss with bssid %pM channel %d beacon_intvl %d capability 0x%x\n",
  466. __func__, mgmt->bssid, ibss_ch->hw_value,
  467. beacon_intvl, capability);
  468. bss = cfg80211_inform_bss_frame(ar->wdev->wiphy,
  469. ibss_ch, mgmt,
  470. size, signal, GFP_KERNEL);
  471. kfree(ieeemgmtbuf);
  472. cfg80211_put_bss(bss);
  473. if (nw_type & ADHOC_NETWORK) {
  474. cfg80211_ibss_joined(ar->net_dev, bssid, GFP_KERNEL);
  475. return;
  476. }
  477. if (ar->sme_state == SME_CONNECTING) {
  478. /* inform connect result to cfg80211 */
  479. ar->sme_state = SME_CONNECTED;
  480. cfg80211_connect_result(ar->net_dev, bssid,
  481. assoc_req_ie, assoc_req_len,
  482. assoc_resp_ie, assoc_resp_len,
  483. WLAN_STATUS_SUCCESS, GFP_KERNEL);
  484. } else if (ar->sme_state == SME_CONNECTED) {
  485. /* inform roam event to cfg80211 */
  486. cfg80211_roamed(ar->net_dev, ibss_ch, bssid,
  487. assoc_req_ie, assoc_req_len,
  488. assoc_resp_ie, assoc_resp_len, GFP_KERNEL);
  489. }
  490. }
  491. static int ath6kl_cfg80211_disconnect(struct wiphy *wiphy,
  492. struct net_device *dev, u16 reason_code)
  493. {
  494. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(dev);
  495. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: reason=%u\n", __func__,
  496. reason_code);
  497. if (!ath6kl_cfg80211_ready(ar))
  498. return -EIO;
  499. if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
  500. ath6kl_err("busy, destroy in progress\n");
  501. return -EBUSY;
  502. }
  503. if (down_interruptible(&ar->sem)) {
  504. ath6kl_err("busy, couldn't get access\n");
  505. return -ERESTARTSYS;
  506. }
  507. ar->reconnect_flag = 0;
  508. ath6kl_disconnect(ar);
  509. memset(ar->ssid, 0, sizeof(ar->ssid));
  510. ar->ssid_len = 0;
  511. if (!test_bit(SKIP_SCAN, &ar->flag))
  512. memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
  513. up(&ar->sem);
  514. return 0;
  515. }
  516. void ath6kl_cfg80211_disconnect_event(struct ath6kl *ar, u8 reason,
  517. u8 *bssid, u8 assoc_resp_len,
  518. u8 *assoc_info, u16 proto_reason)
  519. {
  520. struct ath6kl_key *key = NULL;
  521. u16 status;
  522. if (ar->scan_req) {
  523. cfg80211_scan_done(ar->scan_req, true);
  524. ar->scan_req = NULL;
  525. }
  526. if (ar->nw_type & ADHOC_NETWORK) {
  527. if (ar->wdev->iftype != NL80211_IFTYPE_ADHOC) {
  528. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  529. "%s: ath6k not in ibss mode\n", __func__);
  530. return;
  531. }
  532. memset(bssid, 0, ETH_ALEN);
  533. cfg80211_ibss_joined(ar->net_dev, bssid, GFP_KERNEL);
  534. return;
  535. }
  536. if (ar->nw_type & INFRA_NETWORK) {
  537. if (ar->wdev->iftype != NL80211_IFTYPE_STATION) {
  538. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  539. "%s: ath6k not in station mode\n", __func__);
  540. return;
  541. }
  542. }
  543. if (!test_bit(CONNECT_PEND, &ar->flag)) {
  544. if (reason != DISCONNECT_CMD)
  545. ath6kl_wmi_disconnect_cmd(ar->wmi);
  546. return;
  547. }
  548. if (reason == NO_NETWORK_AVAIL) {
  549. /* connect cmd failed */
  550. ath6kl_wmi_disconnect_cmd(ar->wmi);
  551. return;
  552. }
  553. if (reason != DISCONNECT_CMD)
  554. return;
  555. if (!ar->auto_auth_stage) {
  556. clear_bit(CONNECT_PEND, &ar->flag);
  557. if (ar->sme_state == SME_CONNECTING) {
  558. cfg80211_connect_result(ar->net_dev,
  559. bssid, NULL, 0,
  560. NULL, 0,
  561. WLAN_STATUS_UNSPECIFIED_FAILURE,
  562. GFP_KERNEL);
  563. } else {
  564. cfg80211_disconnected(ar->net_dev, reason,
  565. NULL, 0, GFP_KERNEL);
  566. }
  567. ar->sme_state = SME_DISCONNECTED;
  568. return;
  569. }
  570. if (ar->dot11_auth_mode != OPEN_AUTH)
  571. return;
  572. /*
  573. * If the current auth algorithm is open, try shared and
  574. * make autoAuthStage idle. We do not make it leap for now
  575. * being.
  576. */
  577. key = &ar->keys[ar->def_txkey_index];
  578. if (down_interruptible(&ar->sem)) {
  579. ath6kl_err("busy, couldn't get access\n");
  580. return;
  581. }
  582. ar->dot11_auth_mode = SHARED_AUTH;
  583. ar->auto_auth_stage = AUTH_IDLE;
  584. ath6kl_wmi_addkey_cmd(ar->wmi,
  585. ar->def_txkey_index,
  586. ar->prwise_crypto,
  587. GROUP_USAGE | TX_USAGE,
  588. key->key_len, NULL,
  589. key->key,
  590. KEY_OP_INIT_VAL, NULL,
  591. NO_SYNC_WMIFLAG);
  592. status = ath6kl_wmi_connect_cmd(ar->wmi,
  593. ar->nw_type,
  594. ar->dot11_auth_mode,
  595. ar->auth_mode,
  596. ar->prwise_crypto,
  597. ar->prwise_crypto_len,
  598. ar->grp_crypto,
  599. ar->grp_crpto_len,
  600. ar->ssid_len,
  601. ar->ssid,
  602. ar->req_bssid,
  603. ar->ch_hint,
  604. ar->connect_ctrl_flags);
  605. up(&ar->sem);
  606. }
  607. static inline bool is_ch_11a(u16 ch)
  608. {
  609. return (!((ch >= 2412) && (ch <= 2484)));
  610. }
  611. /* struct ath6kl_node_table::nt_nodelock is locked when calling this */
  612. void ath6kl_cfg80211_scan_node(struct wiphy *wiphy, struct bss *ni)
  613. {
  614. u16 size;
  615. unsigned char *ieeemgmtbuf = NULL;
  616. struct ieee80211_mgmt *mgmt;
  617. struct ieee80211_channel *channel;
  618. struct ieee80211_supported_band *band;
  619. struct ath6kl_common_ie *cie;
  620. s32 signal;
  621. int freq;
  622. cie = &ni->ni_cie;
  623. if (is_ch_11a(cie->ie_chan))
  624. band = wiphy->bands[IEEE80211_BAND_5GHZ]; /* 11a */
  625. else if ((cie->ie_erp) || (cie->ie_xrates))
  626. band = wiphy->bands[IEEE80211_BAND_2GHZ]; /* 11g */
  627. else
  628. band = wiphy->bands[IEEE80211_BAND_2GHZ]; /* 11b */
  629. size = ni->ni_framelen + offsetof(struct ieee80211_mgmt, u);
  630. ieeemgmtbuf = kmalloc(size, GFP_ATOMIC);
  631. if (!ieeemgmtbuf) {
  632. ath6kl_err("ieee mgmt buf alloc error\n");
  633. return;
  634. }
  635. /*
  636. * TODO: Update target to include 802.11 mac header while sending
  637. * bss info. Target removes 802.11 mac header while sending the bss
  638. * info to host, cfg80211 needs it, for time being just filling the
  639. * da, sa and bssid fields alone.
  640. */
  641. mgmt = (struct ieee80211_mgmt *)ieeemgmtbuf;
  642. memset(mgmt->da, 0xff, ETH_ALEN); /*broadcast addr */
  643. memcpy(mgmt->sa, ni->ni_macaddr, ETH_ALEN);
  644. memcpy(mgmt->bssid, ni->ni_macaddr, ETH_ALEN);
  645. memcpy(ieeemgmtbuf + offsetof(struct ieee80211_mgmt, u),
  646. ni->ni_buf, ni->ni_framelen);
  647. freq = cie->ie_chan;
  648. channel = ieee80211_get_channel(wiphy, freq);
  649. signal = ni->ni_snr * 100;
  650. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  651. "%s: bssid %pM ch %d freq %d size %d\n", __func__,
  652. mgmt->bssid, channel->hw_value, freq, size);
  653. cfg80211_inform_bss_frame(wiphy, channel, mgmt,
  654. size, signal, GFP_ATOMIC);
  655. kfree(ieeemgmtbuf);
  656. }
  657. static int ath6kl_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
  658. struct cfg80211_scan_request *request)
  659. {
  660. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
  661. int ret = 0;
  662. if (!ath6kl_cfg80211_ready(ar))
  663. return -EIO;
  664. if (!ar->usr_bss_filter) {
  665. if (ath6kl_wmi_bssfilter_cmd(ar->wmi,
  666. (test_bit(CONNECTED, &ar->flag) ?
  667. ALL_BUT_BSS_FILTER :
  668. ALL_BSS_FILTER), 0) != 0) {
  669. ath6kl_err("couldn't set bss filtering\n");
  670. return -EIO;
  671. }
  672. }
  673. if (request->n_ssids && request->ssids[0].ssid_len) {
  674. u8 i;
  675. if (request->n_ssids > (MAX_PROBED_SSID_INDEX - 1))
  676. request->n_ssids = MAX_PROBED_SSID_INDEX - 1;
  677. for (i = 0; i < request->n_ssids; i++)
  678. ath6kl_wmi_probedssid_cmd(ar->wmi, i + 1,
  679. SPECIFIC_SSID_FLAG,
  680. request->ssids[i].ssid_len,
  681. request->ssids[i].ssid);
  682. }
  683. if (ath6kl_wmi_startscan_cmd(ar->wmi, WMI_LONG_SCAN, 0,
  684. false, 0, 0, 0, NULL) != 0) {
  685. ath6kl_err("wmi_startscan_cmd failed\n");
  686. ret = -EIO;
  687. }
  688. ar->scan_req = request;
  689. return ret;
  690. }
  691. void ath6kl_cfg80211_scan_complete_event(struct ath6kl *ar, int status)
  692. {
  693. int i;
  694. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: status %d\n", __func__, status);
  695. if (!ar->scan_req)
  696. return;
  697. if ((status == -ECANCELED) || (status == -EBUSY)) {
  698. cfg80211_scan_done(ar->scan_req, true);
  699. goto out;
  700. }
  701. /* Translate data to cfg80211 mgmt format */
  702. wlan_iterate_nodes(&ar->scan_table, ar->wdev->wiphy);
  703. cfg80211_scan_done(ar->scan_req, false);
  704. if (ar->scan_req->n_ssids && ar->scan_req->ssids[0].ssid_len) {
  705. for (i = 0; i < ar->scan_req->n_ssids; i++) {
  706. ath6kl_wmi_probedssid_cmd(ar->wmi, i + 1,
  707. DISABLE_SSID_FLAG,
  708. 0, NULL);
  709. }
  710. }
  711. out:
  712. ar->scan_req = NULL;
  713. }
  714. static int ath6kl_cfg80211_add_key(struct wiphy *wiphy, struct net_device *ndev,
  715. u8 key_index, bool pairwise,
  716. const u8 *mac_addr,
  717. struct key_params *params)
  718. {
  719. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
  720. struct ath6kl_key *key = NULL;
  721. u8 key_usage;
  722. u8 key_type;
  723. int status = 0;
  724. if (!ath6kl_cfg80211_ready(ar))
  725. return -EIO;
  726. if (key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
  727. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  728. "%s: key index %d out of bounds\n", __func__,
  729. key_index);
  730. return -ENOENT;
  731. }
  732. key = &ar->keys[key_index];
  733. memset(key, 0, sizeof(struct ath6kl_key));
  734. if (pairwise)
  735. key_usage = PAIRWISE_USAGE;
  736. else
  737. key_usage = GROUP_USAGE;
  738. if (params) {
  739. if (params->key_len > WLAN_MAX_KEY_LEN ||
  740. params->seq_len > sizeof(key->seq))
  741. return -EINVAL;
  742. key->key_len = params->key_len;
  743. memcpy(key->key, params->key, key->key_len);
  744. key->seq_len = params->seq_len;
  745. memcpy(key->seq, params->seq, key->seq_len);
  746. key->cipher = params->cipher;
  747. }
  748. switch (key->cipher) {
  749. case WLAN_CIPHER_SUITE_WEP40:
  750. case WLAN_CIPHER_SUITE_WEP104:
  751. key_type = WEP_CRYPT;
  752. break;
  753. case WLAN_CIPHER_SUITE_TKIP:
  754. key_type = TKIP_CRYPT;
  755. break;
  756. case WLAN_CIPHER_SUITE_CCMP:
  757. key_type = AES_CRYPT;
  758. break;
  759. default:
  760. return -ENOTSUPP;
  761. }
  762. if (((ar->auth_mode == WPA_PSK_AUTH)
  763. || (ar->auth_mode == WPA2_PSK_AUTH))
  764. && (key_usage & GROUP_USAGE))
  765. del_timer(&ar->disconnect_timer);
  766. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  767. "%s: index %d, key_len %d, key_type 0x%x, key_usage 0x%x, seq_len %d\n",
  768. __func__, key_index, key->key_len, key_type,
  769. key_usage, key->seq_len);
  770. ar->def_txkey_index = key_index;
  771. status = ath6kl_wmi_addkey_cmd(ar->wmi, ar->def_txkey_index,
  772. key_type, key_usage, key->key_len,
  773. key->seq, key->key, KEY_OP_INIT_VAL,
  774. (u8 *) mac_addr, SYNC_BOTH_WMIFLAG);
  775. if (status)
  776. return -EIO;
  777. return 0;
  778. }
  779. static int ath6kl_cfg80211_del_key(struct wiphy *wiphy, struct net_device *ndev,
  780. u8 key_index, bool pairwise,
  781. const u8 *mac_addr)
  782. {
  783. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
  784. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
  785. if (!ath6kl_cfg80211_ready(ar))
  786. return -EIO;
  787. if (key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
  788. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  789. "%s: key index %d out of bounds\n", __func__,
  790. key_index);
  791. return -ENOENT;
  792. }
  793. if (!ar->keys[key_index].key_len) {
  794. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  795. "%s: index %d is empty\n", __func__, key_index);
  796. return 0;
  797. }
  798. ar->keys[key_index].key_len = 0;
  799. return ath6kl_wmi_deletekey_cmd(ar->wmi, key_index);
  800. }
  801. static int ath6kl_cfg80211_get_key(struct wiphy *wiphy, struct net_device *ndev,
  802. u8 key_index, bool pairwise,
  803. const u8 *mac_addr, void *cookie,
  804. void (*callback) (void *cookie,
  805. struct key_params *))
  806. {
  807. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
  808. struct ath6kl_key *key = NULL;
  809. struct key_params params;
  810. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
  811. if (!ath6kl_cfg80211_ready(ar))
  812. return -EIO;
  813. if (key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
  814. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  815. "%s: key index %d out of bounds\n", __func__,
  816. key_index);
  817. return -ENOENT;
  818. }
  819. key = &ar->keys[key_index];
  820. memset(&params, 0, sizeof(params));
  821. params.cipher = key->cipher;
  822. params.key_len = key->key_len;
  823. params.seq_len = key->seq_len;
  824. params.seq = key->seq;
  825. params.key = key->key;
  826. callback(cookie, &params);
  827. return key->key_len ? 0 : -ENOENT;
  828. }
  829. static int ath6kl_cfg80211_set_default_key(struct wiphy *wiphy,
  830. struct net_device *ndev,
  831. u8 key_index, bool unicast,
  832. bool multicast)
  833. {
  834. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(ndev);
  835. struct ath6kl_key *key = NULL;
  836. int status = 0;
  837. u8 key_usage;
  838. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
  839. if (!ath6kl_cfg80211_ready(ar))
  840. return -EIO;
  841. if (key_index < WMI_MIN_KEY_INDEX || key_index > WMI_MAX_KEY_INDEX) {
  842. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  843. "%s: key index %d out of bounds\n",
  844. __func__, key_index);
  845. return -ENOENT;
  846. }
  847. if (!ar->keys[key_index].key_len) {
  848. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: invalid key index %d\n",
  849. __func__, key_index);
  850. return -EINVAL;
  851. }
  852. ar->def_txkey_index = key_index;
  853. key = &ar->keys[ar->def_txkey_index];
  854. key_usage = GROUP_USAGE;
  855. if (ar->prwise_crypto == WEP_CRYPT)
  856. key_usage |= TX_USAGE;
  857. status = ath6kl_wmi_addkey_cmd(ar->wmi, ar->def_txkey_index,
  858. ar->prwise_crypto, key_usage,
  859. key->key_len, key->seq, key->key,
  860. KEY_OP_INIT_VAL, NULL,
  861. SYNC_BOTH_WMIFLAG);
  862. if (status)
  863. return -EIO;
  864. return 0;
  865. }
  866. void ath6kl_cfg80211_tkip_micerr_event(struct ath6kl *ar, u8 keyid,
  867. bool ismcast)
  868. {
  869. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  870. "%s: keyid %d, ismcast %d\n", __func__, keyid, ismcast);
  871. cfg80211_michael_mic_failure(ar->net_dev, ar->bssid,
  872. (ismcast ? NL80211_KEYTYPE_GROUP :
  873. NL80211_KEYTYPE_PAIRWISE), keyid, NULL,
  874. GFP_KERNEL);
  875. }
  876. static int ath6kl_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  877. {
  878. struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
  879. int ret;
  880. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: changed 0x%x\n", __func__,
  881. changed);
  882. if (!ath6kl_cfg80211_ready(ar))
  883. return -EIO;
  884. if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
  885. ret = ath6kl_wmi_set_rts_cmd(ar->wmi, wiphy->rts_threshold);
  886. if (ret != 0) {
  887. ath6kl_err("ath6kl_wmi_set_rts_cmd failed\n");
  888. return -EIO;
  889. }
  890. }
  891. return 0;
  892. }
  893. /*
  894. * The type nl80211_tx_power_setting replaces the following
  895. * data type from 2.6.36 onwards
  896. */
  897. static int ath6kl_cfg80211_set_txpower(struct wiphy *wiphy,
  898. enum nl80211_tx_power_setting type,
  899. int dbm)
  900. {
  901. struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
  902. u8 ath6kl_dbm;
  903. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type 0x%x, dbm %d\n", __func__,
  904. type, dbm);
  905. if (!ath6kl_cfg80211_ready(ar))
  906. return -EIO;
  907. switch (type) {
  908. case NL80211_TX_POWER_AUTOMATIC:
  909. return 0;
  910. case NL80211_TX_POWER_LIMITED:
  911. ar->tx_pwr = ath6kl_dbm = dbm;
  912. break;
  913. default:
  914. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type 0x%x not supported\n",
  915. __func__, type);
  916. return -EOPNOTSUPP;
  917. }
  918. ath6kl_wmi_set_tx_pwr_cmd(ar->wmi, ath6kl_dbm);
  919. return 0;
  920. }
  921. static int ath6kl_cfg80211_get_txpower(struct wiphy *wiphy, int *dbm)
  922. {
  923. struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
  924. if (!ath6kl_cfg80211_ready(ar))
  925. return -EIO;
  926. if (test_bit(CONNECTED, &ar->flag)) {
  927. ar->tx_pwr = 0;
  928. if (ath6kl_wmi_get_tx_pwr_cmd(ar->wmi) != 0) {
  929. ath6kl_err("ath6kl_wmi_get_tx_pwr_cmd failed\n");
  930. return -EIO;
  931. }
  932. wait_event_interruptible_timeout(ar->event_wq, ar->tx_pwr != 0,
  933. 5 * HZ);
  934. if (signal_pending(current)) {
  935. ath6kl_err("target did not respond\n");
  936. return -EINTR;
  937. }
  938. }
  939. *dbm = ar->tx_pwr;
  940. return 0;
  941. }
  942. static int ath6kl_cfg80211_set_power_mgmt(struct wiphy *wiphy,
  943. struct net_device *dev,
  944. bool pmgmt, int timeout)
  945. {
  946. struct ath6kl *ar = ath6kl_priv(dev);
  947. struct wmi_power_mode_cmd mode;
  948. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: pmgmt %d, timeout %d\n",
  949. __func__, pmgmt, timeout);
  950. if (!ath6kl_cfg80211_ready(ar))
  951. return -EIO;
  952. if (pmgmt) {
  953. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: max perf\n", __func__);
  954. mode.pwr_mode = REC_POWER;
  955. } else {
  956. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: rec power\n", __func__);
  957. mode.pwr_mode = MAX_PERF_POWER;
  958. }
  959. if (ath6kl_wmi_powermode_cmd(ar->wmi, mode.pwr_mode) != 0) {
  960. ath6kl_err("wmi_powermode_cmd failed\n");
  961. return -EIO;
  962. }
  963. return 0;
  964. }
  965. static int ath6kl_cfg80211_change_iface(struct wiphy *wiphy,
  966. struct net_device *ndev,
  967. enum nl80211_iftype type, u32 *flags,
  968. struct vif_params *params)
  969. {
  970. struct ath6kl *ar = ath6kl_priv(ndev);
  971. struct wireless_dev *wdev = ar->wdev;
  972. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type %u\n", __func__, type);
  973. if (!ath6kl_cfg80211_ready(ar))
  974. return -EIO;
  975. switch (type) {
  976. case NL80211_IFTYPE_STATION:
  977. ar->next_mode = INFRA_NETWORK;
  978. break;
  979. case NL80211_IFTYPE_ADHOC:
  980. ar->next_mode = ADHOC_NETWORK;
  981. break;
  982. default:
  983. ath6kl_err("invalid interface type %u\n", type);
  984. return -EOPNOTSUPP;
  985. }
  986. wdev->iftype = type;
  987. return 0;
  988. }
  989. static int ath6kl_cfg80211_join_ibss(struct wiphy *wiphy,
  990. struct net_device *dev,
  991. struct cfg80211_ibss_params *ibss_param)
  992. {
  993. struct ath6kl *ar = ath6kl_priv(dev);
  994. int status;
  995. if (!ath6kl_cfg80211_ready(ar))
  996. return -EIO;
  997. ar->ssid_len = ibss_param->ssid_len;
  998. memcpy(ar->ssid, ibss_param->ssid, ar->ssid_len);
  999. if (ibss_param->channel)
  1000. ar->ch_hint = ibss_param->channel->center_freq;
  1001. if (ibss_param->channel_fixed) {
  1002. /*
  1003. * TODO: channel_fixed: The channel should be fixed, do not
  1004. * search for IBSSs to join on other channels. Target
  1005. * firmware does not support this feature, needs to be
  1006. * updated.
  1007. */
  1008. return -EOPNOTSUPP;
  1009. }
  1010. memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
  1011. if (ibss_param->bssid && !is_broadcast_ether_addr(ibss_param->bssid))
  1012. memcpy(ar->req_bssid, ibss_param->bssid, sizeof(ar->req_bssid));
  1013. ath6kl_set_wpa_version(ar, 0);
  1014. status = ath6kl_set_auth_type(ar, NL80211_AUTHTYPE_OPEN_SYSTEM);
  1015. if (status)
  1016. return status;
  1017. if (ibss_param->privacy) {
  1018. ath6kl_set_cipher(ar, WLAN_CIPHER_SUITE_WEP40, true);
  1019. ath6kl_set_cipher(ar, WLAN_CIPHER_SUITE_WEP40, false);
  1020. } else {
  1021. ath6kl_set_cipher(ar, 0, true);
  1022. ath6kl_set_cipher(ar, 0, false);
  1023. }
  1024. ar->nw_type = ar->next_mode;
  1025. ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
  1026. "%s: connect called with authmode %d dot11 auth %d"
  1027. " PW crypto %d PW crypto len %d GRP crypto %d"
  1028. " GRP crypto len %d channel hint %u\n",
  1029. __func__,
  1030. ar->auth_mode, ar->dot11_auth_mode, ar->prwise_crypto,
  1031. ar->prwise_crypto_len, ar->grp_crypto,
  1032. ar->grp_crpto_len, ar->ch_hint);
  1033. status = ath6kl_wmi_connect_cmd(ar->wmi, ar->nw_type,
  1034. ar->dot11_auth_mode, ar->auth_mode,
  1035. ar->prwise_crypto,
  1036. ar->prwise_crypto_len,
  1037. ar->grp_crypto, ar->grp_crpto_len,
  1038. ar->ssid_len, ar->ssid,
  1039. ar->req_bssid, ar->ch_hint,
  1040. ar->connect_ctrl_flags);
  1041. set_bit(CONNECT_PEND, &ar->flag);
  1042. return 0;
  1043. }
  1044. static int ath6kl_cfg80211_leave_ibss(struct wiphy *wiphy,
  1045. struct net_device *dev)
  1046. {
  1047. struct ath6kl *ar = (struct ath6kl *)ath6kl_priv(dev);
  1048. if (!ath6kl_cfg80211_ready(ar))
  1049. return -EIO;
  1050. ath6kl_disconnect(ar);
  1051. memset(ar->ssid, 0, sizeof(ar->ssid));
  1052. ar->ssid_len = 0;
  1053. return 0;
  1054. }
  1055. static const u32 cipher_suites[] = {
  1056. WLAN_CIPHER_SUITE_WEP40,
  1057. WLAN_CIPHER_SUITE_WEP104,
  1058. WLAN_CIPHER_SUITE_TKIP,
  1059. WLAN_CIPHER_SUITE_CCMP,
  1060. };
  1061. static bool is_rate_legacy(s32 rate)
  1062. {
  1063. static const s32 legacy[] = { 1000, 2000, 5500, 11000,
  1064. 6000, 9000, 12000, 18000, 24000,
  1065. 36000, 48000, 54000
  1066. };
  1067. u8 i;
  1068. for (i = 0; i < ARRAY_SIZE(legacy); i++)
  1069. if (rate == legacy[i])
  1070. return true;
  1071. return false;
  1072. }
  1073. static bool is_rate_ht20(s32 rate, u8 *mcs, bool *sgi)
  1074. {
  1075. static const s32 ht20[] = { 6500, 13000, 19500, 26000, 39000,
  1076. 52000, 58500, 65000, 72200
  1077. };
  1078. u8 i;
  1079. for (i = 0; i < ARRAY_SIZE(ht20); i++) {
  1080. if (rate == ht20[i]) {
  1081. if (i == ARRAY_SIZE(ht20) - 1)
  1082. /* last rate uses sgi */
  1083. *sgi = true;
  1084. else
  1085. *sgi = false;
  1086. *mcs = i;
  1087. return true;
  1088. }
  1089. }
  1090. return false;
  1091. }
  1092. static bool is_rate_ht40(s32 rate, u8 *mcs, bool *sgi)
  1093. {
  1094. static const s32 ht40[] = { 13500, 27000, 40500, 54000,
  1095. 81000, 108000, 121500, 135000,
  1096. 150000
  1097. };
  1098. u8 i;
  1099. for (i = 0; i < ARRAY_SIZE(ht40); i++) {
  1100. if (rate == ht40[i]) {
  1101. if (i == ARRAY_SIZE(ht40) - 1)
  1102. /* last rate uses sgi */
  1103. *sgi = true;
  1104. else
  1105. *sgi = false;
  1106. *mcs = i;
  1107. return true;
  1108. }
  1109. }
  1110. return false;
  1111. }
  1112. static int ath6kl_get_station(struct wiphy *wiphy, struct net_device *dev,
  1113. u8 *mac, struct station_info *sinfo)
  1114. {
  1115. struct ath6kl *ar = ath6kl_priv(dev);
  1116. long left;
  1117. bool sgi;
  1118. s32 rate;
  1119. int ret;
  1120. u8 mcs;
  1121. if (memcmp(mac, ar->bssid, ETH_ALEN) != 0)
  1122. return -ENOENT;
  1123. if (down_interruptible(&ar->sem))
  1124. return -EBUSY;
  1125. set_bit(STATS_UPDATE_PEND, &ar->flag);
  1126. ret = ath6kl_wmi_get_stats_cmd(ar->wmi);
  1127. if (ret != 0) {
  1128. up(&ar->sem);
  1129. return -EIO;
  1130. }
  1131. left = wait_event_interruptible_timeout(ar->event_wq,
  1132. !test_bit(STATS_UPDATE_PEND,
  1133. &ar->flag),
  1134. WMI_TIMEOUT);
  1135. up(&ar->sem);
  1136. if (left == 0)
  1137. return -ETIMEDOUT;
  1138. else if (left < 0)
  1139. return left;
  1140. if (ar->target_stats.rx_byte) {
  1141. sinfo->rx_bytes = ar->target_stats.rx_byte;
  1142. sinfo->filled |= STATION_INFO_RX_BYTES;
  1143. sinfo->rx_packets = ar->target_stats.rx_pkt;
  1144. sinfo->filled |= STATION_INFO_RX_PACKETS;
  1145. }
  1146. if (ar->target_stats.tx_byte) {
  1147. sinfo->tx_bytes = ar->target_stats.tx_byte;
  1148. sinfo->filled |= STATION_INFO_TX_BYTES;
  1149. sinfo->tx_packets = ar->target_stats.tx_pkt;
  1150. sinfo->filled |= STATION_INFO_TX_PACKETS;
  1151. }
  1152. sinfo->signal = ar->target_stats.cs_rssi;
  1153. sinfo->filled |= STATION_INFO_SIGNAL;
  1154. rate = ar->target_stats.tx_ucast_rate;
  1155. if (is_rate_legacy(rate)) {
  1156. sinfo->txrate.legacy = rate / 100;
  1157. } else if (is_rate_ht20(rate, &mcs, &sgi)) {
  1158. if (sgi) {
  1159. sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1160. sinfo->txrate.mcs = mcs - 1;
  1161. } else {
  1162. sinfo->txrate.mcs = mcs;
  1163. }
  1164. sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
  1165. } else if (is_rate_ht40(rate, &mcs, &sgi)) {
  1166. if (sgi) {
  1167. sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1168. sinfo->txrate.mcs = mcs - 1;
  1169. } else {
  1170. sinfo->txrate.mcs = mcs;
  1171. }
  1172. sinfo->txrate.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
  1173. sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
  1174. } else {
  1175. ath6kl_warn("invalid rate: %d\n", rate);
  1176. return 0;
  1177. }
  1178. sinfo->filled |= STATION_INFO_TX_BITRATE;
  1179. return 0;
  1180. }
  1181. static int ath6kl_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  1182. struct cfg80211_pmksa *pmksa)
  1183. {
  1184. struct ath6kl *ar = ath6kl_priv(netdev);
  1185. return ath6kl_wmi_setpmkid_cmd(ar->wmi, pmksa->bssid,
  1186. pmksa->pmkid, true);
  1187. }
  1188. static int ath6kl_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
  1189. struct cfg80211_pmksa *pmksa)
  1190. {
  1191. struct ath6kl *ar = ath6kl_priv(netdev);
  1192. return ath6kl_wmi_setpmkid_cmd(ar->wmi, pmksa->bssid,
  1193. pmksa->pmkid, false);
  1194. }
  1195. static int ath6kl_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
  1196. {
  1197. struct ath6kl *ar = ath6kl_priv(netdev);
  1198. if (test_bit(CONNECTED, &ar->flag))
  1199. return ath6kl_wmi_setpmkid_cmd(ar->wmi, ar->bssid, NULL, false);
  1200. return 0;
  1201. }
  1202. static struct cfg80211_ops ath6kl_cfg80211_ops = {
  1203. .change_virtual_intf = ath6kl_cfg80211_change_iface,
  1204. .scan = ath6kl_cfg80211_scan,
  1205. .connect = ath6kl_cfg80211_connect,
  1206. .disconnect = ath6kl_cfg80211_disconnect,
  1207. .add_key = ath6kl_cfg80211_add_key,
  1208. .get_key = ath6kl_cfg80211_get_key,
  1209. .del_key = ath6kl_cfg80211_del_key,
  1210. .set_default_key = ath6kl_cfg80211_set_default_key,
  1211. .set_wiphy_params = ath6kl_cfg80211_set_wiphy_params,
  1212. .set_tx_power = ath6kl_cfg80211_set_txpower,
  1213. .get_tx_power = ath6kl_cfg80211_get_txpower,
  1214. .set_power_mgmt = ath6kl_cfg80211_set_power_mgmt,
  1215. .join_ibss = ath6kl_cfg80211_join_ibss,
  1216. .leave_ibss = ath6kl_cfg80211_leave_ibss,
  1217. .get_station = ath6kl_get_station,
  1218. .set_pmksa = ath6kl_set_pmksa,
  1219. .del_pmksa = ath6kl_del_pmksa,
  1220. .flush_pmksa = ath6kl_flush_pmksa,
  1221. };
  1222. struct wireless_dev *ath6kl_cfg80211_init(struct device *dev)
  1223. {
  1224. int ret = 0;
  1225. struct wireless_dev *wdev;
  1226. wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
  1227. if (!wdev) {
  1228. ath6kl_err("couldn't allocate wireless device\n");
  1229. return NULL;
  1230. }
  1231. /* create a new wiphy for use with cfg80211 */
  1232. wdev->wiphy = wiphy_new(&ath6kl_cfg80211_ops, sizeof(struct ath6kl));
  1233. if (!wdev->wiphy) {
  1234. ath6kl_err("couldn't allocate wiphy device\n");
  1235. kfree(wdev);
  1236. return NULL;
  1237. }
  1238. /* set device pointer for wiphy */
  1239. set_wiphy_dev(wdev->wiphy, dev);
  1240. wdev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1241. BIT(NL80211_IFTYPE_ADHOC);
  1242. /* max num of ssids that can be probed during scanning */
  1243. wdev->wiphy->max_scan_ssids = MAX_PROBED_SSID_INDEX;
  1244. wdev->wiphy->bands[IEEE80211_BAND_2GHZ] = &ath6kl_band_2ghz;
  1245. wdev->wiphy->bands[IEEE80211_BAND_5GHZ] = &ath6kl_band_5ghz;
  1246. wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  1247. wdev->wiphy->cipher_suites = cipher_suites;
  1248. wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  1249. ret = wiphy_register(wdev->wiphy);
  1250. if (ret < 0) {
  1251. ath6kl_err("couldn't register wiphy device\n");
  1252. wiphy_free(wdev->wiphy);
  1253. kfree(wdev);
  1254. return NULL;
  1255. }
  1256. return wdev;
  1257. }
  1258. void ath6kl_cfg80211_deinit(struct ath6kl *ar)
  1259. {
  1260. struct wireless_dev *wdev = ar->wdev;
  1261. if (ar->scan_req) {
  1262. cfg80211_scan_done(ar->scan_req, true);
  1263. ar->scan_req = NULL;
  1264. }
  1265. if (!wdev)
  1266. return;
  1267. wiphy_unregister(wdev->wiphy);
  1268. wiphy_free(wdev->wiphy);
  1269. kfree(wdev);
  1270. }