cfg80211.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /*
  2. * Copyright (c) 2012-2012 Quantenna Communications, Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/ieee80211.h>
  20. #include <net/cfg80211.h>
  21. #include <net/netlink.h>
  22. #include "cfg80211.h"
  23. #include "commands.h"
  24. #include "core.h"
  25. #include "util.h"
  26. #include "bus.h"
  27. /* Supported rates to be advertised to the cfg80211 */
  28. static struct ieee80211_rate qtnf_rates_2g[] = {
  29. {.bitrate = 10, .hw_value = 2, },
  30. {.bitrate = 20, .hw_value = 4, },
  31. {.bitrate = 55, .hw_value = 11, },
  32. {.bitrate = 110, .hw_value = 22, },
  33. {.bitrate = 60, .hw_value = 12, },
  34. {.bitrate = 90, .hw_value = 18, },
  35. {.bitrate = 120, .hw_value = 24, },
  36. {.bitrate = 180, .hw_value = 36, },
  37. {.bitrate = 240, .hw_value = 48, },
  38. {.bitrate = 360, .hw_value = 72, },
  39. {.bitrate = 480, .hw_value = 96, },
  40. {.bitrate = 540, .hw_value = 108, },
  41. };
  42. /* Supported rates to be advertised to the cfg80211 */
  43. static struct ieee80211_rate qtnf_rates_5g[] = {
  44. {.bitrate = 60, .hw_value = 12, },
  45. {.bitrate = 90, .hw_value = 18, },
  46. {.bitrate = 120, .hw_value = 24, },
  47. {.bitrate = 180, .hw_value = 36, },
  48. {.bitrate = 240, .hw_value = 48, },
  49. {.bitrate = 360, .hw_value = 72, },
  50. {.bitrate = 480, .hw_value = 96, },
  51. {.bitrate = 540, .hw_value = 108, },
  52. };
  53. /* Supported crypto cipher suits to be advertised to cfg80211 */
  54. static const u32 qtnf_cipher_suites[] = {
  55. WLAN_CIPHER_SUITE_TKIP,
  56. WLAN_CIPHER_SUITE_CCMP,
  57. WLAN_CIPHER_SUITE_AES_CMAC,
  58. };
  59. /* Supported mgmt frame types to be advertised to cfg80211 */
  60. static const struct ieee80211_txrx_stypes
  61. qtnf_mgmt_stypes[NUM_NL80211_IFTYPES] = {
  62. [NL80211_IFTYPE_STATION] = {
  63. .tx = BIT(IEEE80211_STYPE_ACTION >> 4),
  64. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  65. BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
  66. },
  67. [NL80211_IFTYPE_AP] = {
  68. .tx = BIT(IEEE80211_STYPE_ACTION >> 4),
  69. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  70. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  71. BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  72. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  73. BIT(IEEE80211_STYPE_AUTH >> 4),
  74. },
  75. };
  76. static int
  77. qtnf_validate_iface_combinations(struct wiphy *wiphy,
  78. struct qtnf_vif *change_vif,
  79. enum nl80211_iftype new_type)
  80. {
  81. struct qtnf_wmac *mac;
  82. struct qtnf_vif *vif;
  83. int i;
  84. int ret = 0;
  85. struct iface_combination_params params = {
  86. .num_different_channels = 1,
  87. };
  88. mac = wiphy_priv(wiphy);
  89. if (!mac)
  90. return -EFAULT;
  91. for (i = 0; i < QTNF_MAX_INTF; i++) {
  92. vif = &mac->iflist[i];
  93. if (vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED)
  94. params.iftype_num[vif->wdev.iftype]++;
  95. }
  96. if (change_vif) {
  97. params.iftype_num[new_type]++;
  98. params.iftype_num[change_vif->wdev.iftype]--;
  99. } else {
  100. params.iftype_num[new_type]++;
  101. }
  102. ret = cfg80211_check_combinations(wiphy, &params);
  103. return ret;
  104. }
  105. static int
  106. qtnf_change_virtual_intf(struct wiphy *wiphy,
  107. struct net_device *dev,
  108. enum nl80211_iftype type,
  109. struct vif_params *params)
  110. {
  111. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  112. u8 *mac_addr;
  113. int ret;
  114. ret = qtnf_validate_iface_combinations(wiphy, vif, type);
  115. if (ret) {
  116. pr_err("VIF%u.%u combination check: failed to set type %d\n",
  117. vif->mac->macid, vif->vifid, type);
  118. return ret;
  119. }
  120. if (params)
  121. mac_addr = params->macaddr;
  122. else
  123. mac_addr = NULL;
  124. qtnf_scan_done(vif->mac, true);
  125. ret = qtnf_cmd_send_change_intf_type(vif, type, mac_addr);
  126. if (ret) {
  127. pr_err("VIF%u.%u: failed to change VIF type: %d\n",
  128. vif->mac->macid, vif->vifid, ret);
  129. return ret;
  130. }
  131. vif->wdev.iftype = type;
  132. return 0;
  133. }
  134. int qtnf_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
  135. {
  136. struct net_device *netdev = wdev->netdev;
  137. struct qtnf_vif *vif;
  138. if (WARN_ON(!netdev))
  139. return -EFAULT;
  140. vif = qtnf_netdev_get_priv(wdev->netdev);
  141. qtnf_scan_done(vif->mac, true);
  142. /* Stop data */
  143. netif_tx_stop_all_queues(netdev);
  144. if (netif_carrier_ok(netdev))
  145. netif_carrier_off(netdev);
  146. if (netdev->reg_state == NETREG_REGISTERED)
  147. unregister_netdevice(netdev);
  148. if (qtnf_cmd_send_del_intf(vif))
  149. pr_err("VIF%u.%u: failed to delete VIF\n", vif->mac->macid,
  150. vif->vifid);
  151. vif->netdev->ieee80211_ptr = NULL;
  152. vif->netdev = NULL;
  153. vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
  154. eth_zero_addr(vif->mac_addr);
  155. eth_zero_addr(vif->bssid);
  156. return 0;
  157. }
  158. static struct wireless_dev *qtnf_add_virtual_intf(struct wiphy *wiphy,
  159. const char *name,
  160. unsigned char name_assign_t,
  161. enum nl80211_iftype type,
  162. struct vif_params *params)
  163. {
  164. struct qtnf_wmac *mac;
  165. struct qtnf_vif *vif;
  166. u8 *mac_addr = NULL;
  167. int ret;
  168. mac = wiphy_priv(wiphy);
  169. if (!mac)
  170. return ERR_PTR(-EFAULT);
  171. ret = qtnf_validate_iface_combinations(wiphy, NULL, type);
  172. if (ret) {
  173. pr_err("MAC%u invalid combination: failed to add type %d\n",
  174. mac->macid, type);
  175. return ERR_PTR(ret);
  176. }
  177. switch (type) {
  178. case NL80211_IFTYPE_STATION:
  179. case NL80211_IFTYPE_AP:
  180. vif = qtnf_mac_get_free_vif(mac);
  181. if (!vif) {
  182. pr_err("MAC%u: no free VIF available\n", mac->macid);
  183. return ERR_PTR(-EFAULT);
  184. }
  185. eth_zero_addr(vif->mac_addr);
  186. vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
  187. vif->wdev.wiphy = wiphy;
  188. vif->wdev.iftype = type;
  189. vif->sta_state = QTNF_STA_DISCONNECTED;
  190. break;
  191. default:
  192. pr_err("MAC%u: unsupported IF type %d\n", mac->macid, type);
  193. return ERR_PTR(-ENOTSUPP);
  194. }
  195. if (params)
  196. mac_addr = params->macaddr;
  197. if (qtnf_cmd_send_add_intf(vif, type, mac_addr)) {
  198. pr_err("VIF%u.%u: failed to add VIF\n", mac->macid, vif->vifid);
  199. goto err_cmd;
  200. }
  201. if (!is_valid_ether_addr(vif->mac_addr)) {
  202. pr_err("VIF%u.%u: FW reported bad MAC: %pM\n",
  203. mac->macid, vif->vifid, vif->mac_addr);
  204. goto err_mac;
  205. }
  206. if (qtnf_core_net_attach(mac, vif, name, name_assign_t)) {
  207. pr_err("VIF%u.%u: failed to attach netdev\n", mac->macid,
  208. vif->vifid);
  209. goto err_net;
  210. }
  211. vif->wdev.netdev = vif->netdev;
  212. return &vif->wdev;
  213. err_net:
  214. vif->netdev = NULL;
  215. err_mac:
  216. qtnf_cmd_send_del_intf(vif);
  217. err_cmd:
  218. vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
  219. eth_zero_addr(vif->mac_addr);
  220. eth_zero_addr(vif->bssid);
  221. return ERR_PTR(-EFAULT);
  222. }
  223. static int qtnf_mgmt_set_appie(struct qtnf_vif *vif,
  224. const struct cfg80211_beacon_data *info)
  225. {
  226. int ret = 0;
  227. if (!info->beacon_ies || !info->beacon_ies_len) {
  228. ret = qtnf_cmd_send_mgmt_set_appie(vif, QLINK_IE_SET_BEACON_IES,
  229. NULL, 0);
  230. } else {
  231. ret = qtnf_cmd_send_mgmt_set_appie(vif, QLINK_IE_SET_BEACON_IES,
  232. info->beacon_ies,
  233. info->beacon_ies_len);
  234. }
  235. if (ret)
  236. goto out;
  237. if (!info->proberesp_ies || !info->proberesp_ies_len) {
  238. ret = qtnf_cmd_send_mgmt_set_appie(vif,
  239. QLINK_IE_SET_PROBE_RESP_IES,
  240. NULL, 0);
  241. } else {
  242. ret = qtnf_cmd_send_mgmt_set_appie(vif,
  243. QLINK_IE_SET_PROBE_RESP_IES,
  244. info->proberesp_ies,
  245. info->proberesp_ies_len);
  246. }
  247. if (ret)
  248. goto out;
  249. if (!info->assocresp_ies || !info->assocresp_ies_len) {
  250. ret = qtnf_cmd_send_mgmt_set_appie(vif,
  251. QLINK_IE_SET_ASSOC_RESP,
  252. NULL, 0);
  253. } else {
  254. ret = qtnf_cmd_send_mgmt_set_appie(vif,
  255. QLINK_IE_SET_ASSOC_RESP,
  256. info->assocresp_ies,
  257. info->assocresp_ies_len);
  258. }
  259. out:
  260. return ret;
  261. }
  262. static int qtnf_change_beacon(struct wiphy *wiphy, struct net_device *dev,
  263. struct cfg80211_beacon_data *info)
  264. {
  265. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  266. return qtnf_mgmt_set_appie(vif, info);
  267. }
  268. static int qtnf_start_ap(struct wiphy *wiphy, struct net_device *dev,
  269. struct cfg80211_ap_settings *settings)
  270. {
  271. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  272. int ret;
  273. ret = qtnf_cmd_send_start_ap(vif, settings);
  274. if (ret)
  275. pr_err("VIF%u.%u: failed to start AP\n", vif->mac->macid,
  276. vif->vifid);
  277. return ret;
  278. }
  279. static int qtnf_stop_ap(struct wiphy *wiphy, struct net_device *dev)
  280. {
  281. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  282. int ret;
  283. qtnf_scan_done(vif->mac, true);
  284. ret = qtnf_cmd_send_stop_ap(vif);
  285. if (ret) {
  286. pr_err("VIF%u.%u: failed to stop AP operation in FW\n",
  287. vif->mac->macid, vif->vifid);
  288. netif_carrier_off(vif->netdev);
  289. }
  290. return ret;
  291. }
  292. static int qtnf_set_wiphy_params(struct wiphy *wiphy, u32 changed)
  293. {
  294. struct qtnf_wmac *mac = wiphy_priv(wiphy);
  295. struct qtnf_vif *vif;
  296. int ret;
  297. vif = qtnf_mac_get_base_vif(mac);
  298. if (!vif) {
  299. pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
  300. return -EFAULT;
  301. }
  302. if (changed & (WIPHY_PARAM_RETRY_LONG | WIPHY_PARAM_RETRY_SHORT)) {
  303. pr_err("MAC%u: can't modify retry params\n", mac->macid);
  304. return -EOPNOTSUPP;
  305. }
  306. ret = qtnf_cmd_send_update_phy_params(mac, changed);
  307. if (ret)
  308. pr_err("MAC%u: failed to update PHY params\n", mac->macid);
  309. return ret;
  310. }
  311. static void
  312. qtnf_mgmt_frame_register(struct wiphy *wiphy, struct wireless_dev *wdev,
  313. u16 frame_type, bool reg)
  314. {
  315. struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev);
  316. u16 mgmt_type;
  317. u16 new_mask;
  318. u16 qlink_frame_type = 0;
  319. mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
  320. if (reg)
  321. new_mask = vif->mgmt_frames_bitmask | BIT(mgmt_type);
  322. else
  323. new_mask = vif->mgmt_frames_bitmask & ~BIT(mgmt_type);
  324. if (new_mask == vif->mgmt_frames_bitmask)
  325. return;
  326. switch (frame_type & IEEE80211_FCTL_STYPE) {
  327. case IEEE80211_STYPE_REASSOC_REQ:
  328. case IEEE80211_STYPE_ASSOC_REQ:
  329. qlink_frame_type = QLINK_MGMT_FRAME_ASSOC_REQ;
  330. break;
  331. case IEEE80211_STYPE_AUTH:
  332. qlink_frame_type = QLINK_MGMT_FRAME_AUTH;
  333. break;
  334. case IEEE80211_STYPE_PROBE_REQ:
  335. qlink_frame_type = QLINK_MGMT_FRAME_PROBE_REQ;
  336. break;
  337. case IEEE80211_STYPE_ACTION:
  338. qlink_frame_type = QLINK_MGMT_FRAME_ACTION;
  339. break;
  340. default:
  341. pr_warn("VIF%u.%u: unsupported frame type: %X\n",
  342. vif->mac->macid, vif->vifid,
  343. (frame_type & IEEE80211_FCTL_STYPE) >> 4);
  344. return;
  345. }
  346. if (qtnf_cmd_send_register_mgmt(vif, qlink_frame_type, reg)) {
  347. pr_warn("VIF%u.%u: failed to %sregister mgmt frame type 0x%x\n",
  348. vif->mac->macid, vif->vifid, reg ? "" : "un",
  349. frame_type);
  350. return;
  351. }
  352. vif->mgmt_frames_bitmask = new_mask;
  353. pr_debug("VIF%u.%u: %sregistered mgmt frame type 0x%x\n",
  354. vif->mac->macid, vif->vifid, reg ? "" : "un", frame_type);
  355. }
  356. static int
  357. qtnf_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
  358. struct cfg80211_mgmt_tx_params *params, u64 *cookie)
  359. {
  360. struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev);
  361. const struct ieee80211_mgmt *mgmt_frame = (void *)params->buf;
  362. u32 short_cookie = prandom_u32();
  363. u16 flags = 0;
  364. u16 freq;
  365. *cookie = short_cookie;
  366. if (params->offchan)
  367. flags |= QLINK_MGMT_FRAME_TX_FLAG_OFFCHAN;
  368. if (params->no_cck)
  369. flags |= QLINK_MGMT_FRAME_TX_FLAG_NO_CCK;
  370. if (params->dont_wait_for_ack)
  371. flags |= QLINK_MGMT_FRAME_TX_FLAG_ACK_NOWAIT;
  372. /* If channel is not specified, pass "freq = 0" to tell device
  373. * firmware to use current channel.
  374. */
  375. if (params->chan)
  376. freq = params->chan->center_freq;
  377. else
  378. freq = 0;
  379. pr_debug("%s freq:%u; FC:%.4X; DA:%pM; len:%zu; C:%.8X; FL:%.4X\n",
  380. wdev->netdev->name, freq,
  381. le16_to_cpu(mgmt_frame->frame_control), mgmt_frame->da,
  382. params->len, short_cookie, flags);
  383. return qtnf_cmd_send_mgmt_frame(vif, short_cookie, flags,
  384. freq,
  385. params->buf, params->len);
  386. }
  387. static int
  388. qtnf_get_station(struct wiphy *wiphy, struct net_device *dev,
  389. const u8 *mac, struct station_info *sinfo)
  390. {
  391. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  392. sinfo->generation = vif->generation;
  393. return qtnf_cmd_get_sta_info(vif, mac, sinfo);
  394. }
  395. static int
  396. qtnf_dump_station(struct wiphy *wiphy, struct net_device *dev,
  397. int idx, u8 *mac, struct station_info *sinfo)
  398. {
  399. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  400. const struct qtnf_sta_node *sta_node;
  401. int ret;
  402. sta_node = qtnf_sta_list_lookup_index(&vif->sta_list, idx);
  403. if (unlikely(!sta_node))
  404. return -ENOENT;
  405. ether_addr_copy(mac, sta_node->mac_addr);
  406. ret = qtnf_cmd_get_sta_info(vif, sta_node->mac_addr, sinfo);
  407. if (unlikely(ret == -ENOENT)) {
  408. qtnf_sta_list_del(vif, mac);
  409. cfg80211_del_sta(vif->netdev, mac, GFP_KERNEL);
  410. sinfo->filled = 0;
  411. }
  412. sinfo->generation = vif->generation;
  413. return ret;
  414. }
  415. static int qtnf_add_key(struct wiphy *wiphy, struct net_device *dev,
  416. u8 key_index, bool pairwise, const u8 *mac_addr,
  417. struct key_params *params)
  418. {
  419. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  420. int ret;
  421. ret = qtnf_cmd_send_add_key(vif, key_index, pairwise, mac_addr, params);
  422. if (ret)
  423. pr_err("VIF%u.%u: failed to add key: cipher=%x idx=%u pw=%u\n",
  424. vif->mac->macid, vif->vifid, params->cipher, key_index,
  425. pairwise);
  426. return ret;
  427. }
  428. static int qtnf_del_key(struct wiphy *wiphy, struct net_device *dev,
  429. u8 key_index, bool pairwise, const u8 *mac_addr)
  430. {
  431. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  432. int ret;
  433. ret = qtnf_cmd_send_del_key(vif, key_index, pairwise, mac_addr);
  434. if (ret)
  435. pr_err("VIF%u.%u: failed to delete key: idx=%u pw=%u\n",
  436. vif->mac->macid, vif->vifid, key_index, pairwise);
  437. return ret;
  438. }
  439. static int qtnf_set_default_key(struct wiphy *wiphy, struct net_device *dev,
  440. u8 key_index, bool unicast, bool multicast)
  441. {
  442. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  443. int ret;
  444. ret = qtnf_cmd_send_set_default_key(vif, key_index, unicast, multicast);
  445. if (ret)
  446. pr_err("VIF%u.%u: failed to set dflt key: idx=%u uc=%u mc=%u\n",
  447. vif->mac->macid, vif->vifid, key_index, unicast,
  448. multicast);
  449. return ret;
  450. }
  451. static int
  452. qtnf_set_default_mgmt_key(struct wiphy *wiphy, struct net_device *dev,
  453. u8 key_index)
  454. {
  455. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  456. int ret;
  457. ret = qtnf_cmd_send_set_default_mgmt_key(vif, key_index);
  458. if (ret)
  459. pr_err("VIF%u.%u: failed to set default MGMT key: idx=%u\n",
  460. vif->mac->macid, vif->vifid, key_index);
  461. return ret;
  462. }
  463. static int
  464. qtnf_change_station(struct wiphy *wiphy, struct net_device *dev,
  465. const u8 *mac, struct station_parameters *params)
  466. {
  467. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  468. int ret;
  469. ret = qtnf_cmd_send_change_sta(vif, mac, params);
  470. if (ret)
  471. pr_err("VIF%u.%u: failed to change STA %pM\n",
  472. vif->mac->macid, vif->vifid, mac);
  473. return ret;
  474. }
  475. static int
  476. qtnf_del_station(struct wiphy *wiphy, struct net_device *dev,
  477. struct station_del_parameters *params)
  478. {
  479. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  480. int ret;
  481. if (params->mac &&
  482. (vif->wdev.iftype == NL80211_IFTYPE_AP) &&
  483. !is_broadcast_ether_addr(params->mac) &&
  484. !qtnf_sta_list_lookup(&vif->sta_list, params->mac))
  485. return 0;
  486. ret = qtnf_cmd_send_del_sta(vif, params);
  487. if (ret)
  488. pr_err("VIF%u.%u: failed to delete STA %pM\n",
  489. vif->mac->macid, vif->vifid, params->mac);
  490. return ret;
  491. }
  492. static int
  493. qtnf_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
  494. {
  495. struct qtnf_wmac *mac = wiphy_priv(wiphy);
  496. cancel_delayed_work_sync(&mac->scan_timeout);
  497. mac->scan_req = request;
  498. if (qtnf_cmd_send_scan(mac)) {
  499. pr_err("MAC%u: failed to start scan\n", mac->macid);
  500. mac->scan_req = NULL;
  501. return -EFAULT;
  502. }
  503. queue_delayed_work(mac->bus->workqueue, &mac->scan_timeout,
  504. QTNF_SCAN_TIMEOUT_SEC * HZ);
  505. return 0;
  506. }
  507. static int
  508. qtnf_connect(struct wiphy *wiphy, struct net_device *dev,
  509. struct cfg80211_connect_params *sme)
  510. {
  511. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  512. int ret;
  513. if (vif->wdev.iftype != NL80211_IFTYPE_STATION)
  514. return -EOPNOTSUPP;
  515. if (vif->sta_state != QTNF_STA_DISCONNECTED)
  516. return -EBUSY;
  517. if (sme->bssid)
  518. ether_addr_copy(vif->bssid, sme->bssid);
  519. else
  520. eth_zero_addr(vif->bssid);
  521. ret = qtnf_cmd_send_connect(vif, sme);
  522. if (ret) {
  523. pr_err("VIF%u.%u: failed to connect\n", vif->mac->macid,
  524. vif->vifid);
  525. return ret;
  526. }
  527. vif->sta_state = QTNF_STA_CONNECTING;
  528. return 0;
  529. }
  530. static int
  531. qtnf_disconnect(struct wiphy *wiphy, struct net_device *dev,
  532. u16 reason_code)
  533. {
  534. struct qtnf_wmac *mac = wiphy_priv(wiphy);
  535. struct qtnf_vif *vif;
  536. int ret;
  537. vif = qtnf_mac_get_base_vif(mac);
  538. if (!vif) {
  539. pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
  540. return -EFAULT;
  541. }
  542. if (vif->wdev.iftype != NL80211_IFTYPE_STATION)
  543. return -EOPNOTSUPP;
  544. if (vif->sta_state == QTNF_STA_DISCONNECTED)
  545. return 0;
  546. ret = qtnf_cmd_send_disconnect(vif, reason_code);
  547. if (ret) {
  548. pr_err("VIF%u.%u: failed to disconnect\n", mac->macid,
  549. vif->vifid);
  550. return ret;
  551. }
  552. return 0;
  553. }
  554. static int
  555. qtnf_dump_survey(struct wiphy *wiphy, struct net_device *dev,
  556. int idx, struct survey_info *survey)
  557. {
  558. struct qtnf_wmac *mac = wiphy_priv(wiphy);
  559. struct wireless_dev *wdev = dev->ieee80211_ptr;
  560. struct ieee80211_supported_band *sband;
  561. const struct cfg80211_chan_def *chandef = &wdev->chandef;
  562. struct ieee80211_channel *chan;
  563. struct qtnf_chan_stats stats;
  564. struct qtnf_vif *vif;
  565. int ret;
  566. vif = qtnf_netdev_get_priv(dev);
  567. sband = wiphy->bands[NL80211_BAND_2GHZ];
  568. if (sband && idx >= sband->n_channels) {
  569. idx -= sband->n_channels;
  570. sband = NULL;
  571. }
  572. if (!sband)
  573. sband = wiphy->bands[NL80211_BAND_5GHZ];
  574. if (!sband || idx >= sband->n_channels)
  575. return -ENOENT;
  576. chan = &sband->channels[idx];
  577. memset(&stats, 0, sizeof(stats));
  578. survey->channel = chan;
  579. survey->filled = 0x0;
  580. if (chandef->chan) {
  581. if (chan->hw_value == chandef->chan->hw_value)
  582. survey->filled = SURVEY_INFO_IN_USE;
  583. }
  584. ret = qtnf_cmd_get_chan_stats(mac, chan->hw_value, &stats);
  585. switch (ret) {
  586. case 0:
  587. if (unlikely(stats.chan_num != chan->hw_value)) {
  588. pr_err("received stats for channel %d instead of %d\n",
  589. stats.chan_num, chan->hw_value);
  590. ret = -EINVAL;
  591. break;
  592. }
  593. survey->filled |= SURVEY_INFO_TIME |
  594. SURVEY_INFO_TIME_SCAN |
  595. SURVEY_INFO_TIME_BUSY |
  596. SURVEY_INFO_TIME_RX |
  597. SURVEY_INFO_TIME_TX |
  598. SURVEY_INFO_NOISE_DBM;
  599. survey->time_scan = stats.cca_try;
  600. survey->time = stats.cca_try;
  601. survey->time_tx = stats.cca_tx;
  602. survey->time_rx = stats.cca_rx;
  603. survey->time_busy = stats.cca_busy;
  604. survey->noise = stats.chan_noise;
  605. break;
  606. case -ENOENT:
  607. pr_debug("no stats for channel %u\n", chan->hw_value);
  608. ret = 0;
  609. break;
  610. default:
  611. pr_debug("failed to get chan(%d) stats from card\n",
  612. chan->hw_value);
  613. ret = -EINVAL;
  614. break;
  615. }
  616. return ret;
  617. }
  618. static int
  619. qtnf_get_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
  620. struct cfg80211_chan_def *chandef)
  621. {
  622. struct net_device *ndev = wdev->netdev;
  623. struct qtnf_vif *vif;
  624. int ret;
  625. if (!ndev)
  626. return -ENODEV;
  627. vif = qtnf_netdev_get_priv(wdev->netdev);
  628. ret = qtnf_cmd_get_channel(vif, chandef);
  629. if (ret) {
  630. pr_err("%s: failed to get channel: %d\n", ndev->name, ret);
  631. goto out;
  632. }
  633. if (!cfg80211_chandef_valid(chandef)) {
  634. pr_err("%s: bad channel freq=%u cf1=%u cf2=%u bw=%u\n",
  635. ndev->name, chandef->chan->center_freq,
  636. chandef->center_freq1, chandef->center_freq2,
  637. chandef->width);
  638. ret = -ENODATA;
  639. }
  640. out:
  641. return ret;
  642. }
  643. static int qtnf_channel_switch(struct wiphy *wiphy, struct net_device *dev,
  644. struct cfg80211_csa_settings *params)
  645. {
  646. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  647. int ret;
  648. pr_debug("%s: chan(%u) count(%u) radar(%u) block_tx(%u)\n", dev->name,
  649. params->chandef.chan->hw_value, params->count,
  650. params->radar_required, params->block_tx);
  651. if (!cfg80211_chandef_valid(&params->chandef)) {
  652. pr_err("%s: invalid channel\n", dev->name);
  653. return -EINVAL;
  654. }
  655. ret = qtnf_cmd_send_chan_switch(vif, params);
  656. if (ret)
  657. pr_warn("%s: failed to switch to channel (%u)\n",
  658. dev->name, params->chandef.chan->hw_value);
  659. return ret;
  660. }
  661. static int qtnf_start_radar_detection(struct wiphy *wiphy,
  662. struct net_device *ndev,
  663. struct cfg80211_chan_def *chandef,
  664. u32 cac_time_ms)
  665. {
  666. struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
  667. int ret;
  668. ret = qtnf_cmd_start_cac(vif, chandef, cac_time_ms);
  669. if (ret)
  670. pr_err("%s: failed to start CAC ret=%d\n", ndev->name, ret);
  671. return ret;
  672. }
  673. static int qtnf_set_mac_acl(struct wiphy *wiphy,
  674. struct net_device *dev,
  675. const struct cfg80211_acl_data *params)
  676. {
  677. struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
  678. int ret;
  679. ret = qtnf_cmd_set_mac_acl(vif, params);
  680. if (ret)
  681. pr_err("%s: failed to set mac ACL ret=%d\n", dev->name, ret);
  682. return ret;
  683. }
  684. static struct cfg80211_ops qtn_cfg80211_ops = {
  685. .add_virtual_intf = qtnf_add_virtual_intf,
  686. .change_virtual_intf = qtnf_change_virtual_intf,
  687. .del_virtual_intf = qtnf_del_virtual_intf,
  688. .start_ap = qtnf_start_ap,
  689. .change_beacon = qtnf_change_beacon,
  690. .stop_ap = qtnf_stop_ap,
  691. .set_wiphy_params = qtnf_set_wiphy_params,
  692. .mgmt_frame_register = qtnf_mgmt_frame_register,
  693. .mgmt_tx = qtnf_mgmt_tx,
  694. .change_station = qtnf_change_station,
  695. .del_station = qtnf_del_station,
  696. .get_station = qtnf_get_station,
  697. .dump_station = qtnf_dump_station,
  698. .add_key = qtnf_add_key,
  699. .del_key = qtnf_del_key,
  700. .set_default_key = qtnf_set_default_key,
  701. .set_default_mgmt_key = qtnf_set_default_mgmt_key,
  702. .scan = qtnf_scan,
  703. .connect = qtnf_connect,
  704. .disconnect = qtnf_disconnect,
  705. .dump_survey = qtnf_dump_survey,
  706. .get_channel = qtnf_get_channel,
  707. .channel_switch = qtnf_channel_switch,
  708. .start_radar_detection = qtnf_start_radar_detection,
  709. .set_mac_acl = qtnf_set_mac_acl,
  710. };
  711. static void qtnf_cfg80211_reg_notifier(struct wiphy *wiphy_in,
  712. struct regulatory_request *req)
  713. {
  714. struct qtnf_wmac *mac = wiphy_priv(wiphy_in);
  715. struct qtnf_bus *bus = mac->bus;
  716. struct wiphy *wiphy;
  717. unsigned int mac_idx;
  718. enum nl80211_band band;
  719. int ret;
  720. pr_debug("MAC%u: initiator=%d alpha=%c%c\n", mac->macid, req->initiator,
  721. req->alpha2[0], req->alpha2[1]);
  722. ret = qtnf_cmd_reg_notify(bus, req);
  723. if (ret) {
  724. if (ret != -EOPNOTSUPP && ret != -EALREADY)
  725. pr_err("failed to update reg domain to %c%c\n",
  726. req->alpha2[0], req->alpha2[1]);
  727. return;
  728. }
  729. for (mac_idx = 0; mac_idx < QTNF_MAX_MAC; ++mac_idx) {
  730. if (!(bus->hw_info.mac_bitmap & (1 << mac_idx)))
  731. continue;
  732. mac = bus->mac[mac_idx];
  733. if (!mac)
  734. continue;
  735. wiphy = priv_to_wiphy(mac);
  736. for (band = 0; band < NUM_NL80211_BANDS; ++band) {
  737. if (!wiphy->bands[band])
  738. continue;
  739. ret = qtnf_cmd_band_info_get(mac, wiphy->bands[band]);
  740. if (ret)
  741. pr_err("failed to get chan info for mac %u band %u\n",
  742. mac_idx, band);
  743. }
  744. }
  745. }
  746. struct wiphy *qtnf_wiphy_allocate(struct qtnf_bus *bus)
  747. {
  748. struct wiphy *wiphy;
  749. wiphy = wiphy_new(&qtn_cfg80211_ops, sizeof(struct qtnf_wmac));
  750. if (!wiphy)
  751. return NULL;
  752. set_wiphy_dev(wiphy, bus->dev);
  753. return wiphy;
  754. }
  755. static int
  756. qtnf_wiphy_setup_if_comb(struct wiphy *wiphy, struct qtnf_mac_info *mac_info)
  757. {
  758. struct ieee80211_iface_combination *if_comb;
  759. size_t n_if_comb;
  760. u16 interface_modes = 0;
  761. size_t i, j;
  762. if_comb = mac_info->if_comb;
  763. n_if_comb = mac_info->n_if_comb;
  764. if (!if_comb || !n_if_comb)
  765. return -ENOENT;
  766. for (i = 0; i < n_if_comb; i++) {
  767. if_comb[i].radar_detect_widths = mac_info->radar_detect_widths;
  768. for (j = 0; j < if_comb[i].n_limits; j++)
  769. interface_modes |= if_comb[i].limits[j].types;
  770. }
  771. wiphy->iface_combinations = if_comb;
  772. wiphy->n_iface_combinations = n_if_comb;
  773. wiphy->interface_modes = interface_modes;
  774. return 0;
  775. }
  776. int qtnf_wiphy_register(struct qtnf_hw_info *hw_info, struct qtnf_wmac *mac)
  777. {
  778. struct wiphy *wiphy = priv_to_wiphy(mac);
  779. int ret;
  780. if (!wiphy) {
  781. pr_err("invalid wiphy pointer\n");
  782. return -EFAULT;
  783. }
  784. wiphy->frag_threshold = mac->macinfo.frag_thr;
  785. wiphy->rts_threshold = mac->macinfo.rts_thr;
  786. wiphy->retry_short = mac->macinfo.sretry_limit;
  787. wiphy->retry_long = mac->macinfo.lretry_limit;
  788. wiphy->coverage_class = mac->macinfo.coverage_class;
  789. wiphy->max_scan_ssids = QTNF_MAX_SSID_LIST_LENGTH;
  790. wiphy->max_scan_ie_len = QTNF_MAX_VSIE_LEN;
  791. wiphy->mgmt_stypes = qtnf_mgmt_stypes;
  792. wiphy->max_remain_on_channel_duration = 5000;
  793. wiphy->max_acl_mac_addrs = mac->macinfo.max_acl_mac_addrs;
  794. wiphy->max_num_csa_counters = 2;
  795. ret = qtnf_wiphy_setup_if_comb(wiphy, &mac->macinfo);
  796. if (ret)
  797. goto out;
  798. /* Initialize cipher suits */
  799. wiphy->cipher_suites = qtnf_cipher_suites;
  800. wiphy->n_cipher_suites = ARRAY_SIZE(qtnf_cipher_suites);
  801. wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  802. wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
  803. WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
  804. WIPHY_FLAG_AP_UAPSD |
  805. WIPHY_FLAG_HAS_CHANNEL_SWITCH;
  806. wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
  807. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2;
  808. wiphy->available_antennas_tx = mac->macinfo.num_tx_chain;
  809. wiphy->available_antennas_rx = mac->macinfo.num_rx_chain;
  810. wiphy->max_ap_assoc_sta = mac->macinfo.max_ap_assoc_sta;
  811. wiphy->ht_capa_mod_mask = &mac->macinfo.ht_cap_mod_mask;
  812. wiphy->vht_capa_mod_mask = &mac->macinfo.vht_cap_mod_mask;
  813. ether_addr_copy(wiphy->perm_addr, mac->macaddr);
  814. if (hw_info->hw_capab & QLINK_HW_CAPAB_STA_INACT_TIMEOUT)
  815. wiphy->features |= NL80211_FEATURE_INACTIVITY_TIMER;
  816. if (hw_info->hw_capab & QLINK_HW_CAPAB_REG_UPDATE) {
  817. wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
  818. REGULATORY_CUSTOM_REG;
  819. wiphy->reg_notifier = qtnf_cfg80211_reg_notifier;
  820. wiphy_apply_custom_regulatory(wiphy, hw_info->rd);
  821. } else {
  822. wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
  823. }
  824. strlcpy(wiphy->fw_version, hw_info->fw_version,
  825. sizeof(wiphy->fw_version));
  826. wiphy->hw_version = hw_info->hw_version;
  827. ret = wiphy_register(wiphy);
  828. if (ret < 0)
  829. goto out;
  830. if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
  831. ret = regulatory_set_wiphy_regd(wiphy, hw_info->rd);
  832. else if (isalpha(hw_info->rd->alpha2[0]) &&
  833. isalpha(hw_info->rd->alpha2[1]))
  834. ret = regulatory_hint(wiphy, hw_info->rd->alpha2);
  835. out:
  836. return ret;
  837. }
  838. void qtnf_netdev_updown(struct net_device *ndev, bool up)
  839. {
  840. struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
  841. if (qtnf_cmd_send_updown_intf(vif, up))
  842. pr_err("failed to send up/down command to FW\n");
  843. }
  844. void qtnf_virtual_intf_cleanup(struct net_device *ndev)
  845. {
  846. struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
  847. struct qtnf_wmac *mac = wiphy_priv(vif->wdev.wiphy);
  848. if (vif->wdev.iftype == NL80211_IFTYPE_STATION) {
  849. switch (vif->sta_state) {
  850. case QTNF_STA_DISCONNECTED:
  851. break;
  852. case QTNF_STA_CONNECTING:
  853. cfg80211_connect_result(vif->netdev,
  854. vif->bssid, NULL, 0,
  855. NULL, 0,
  856. WLAN_STATUS_UNSPECIFIED_FAILURE,
  857. GFP_KERNEL);
  858. qtnf_disconnect(vif->wdev.wiphy, ndev,
  859. WLAN_REASON_DEAUTH_LEAVING);
  860. break;
  861. case QTNF_STA_CONNECTED:
  862. cfg80211_disconnected(vif->netdev,
  863. WLAN_REASON_DEAUTH_LEAVING,
  864. NULL, 0, 1, GFP_KERNEL);
  865. qtnf_disconnect(vif->wdev.wiphy, ndev,
  866. WLAN_REASON_DEAUTH_LEAVING);
  867. break;
  868. }
  869. vif->sta_state = QTNF_STA_DISCONNECTED;
  870. }
  871. qtnf_scan_done(mac, true);
  872. }
  873. void qtnf_cfg80211_vif_reset(struct qtnf_vif *vif)
  874. {
  875. if (vif->wdev.iftype == NL80211_IFTYPE_STATION) {
  876. switch (vif->sta_state) {
  877. case QTNF_STA_CONNECTING:
  878. cfg80211_connect_result(vif->netdev,
  879. vif->bssid, NULL, 0,
  880. NULL, 0,
  881. WLAN_STATUS_UNSPECIFIED_FAILURE,
  882. GFP_KERNEL);
  883. break;
  884. case QTNF_STA_CONNECTED:
  885. cfg80211_disconnected(vif->netdev,
  886. WLAN_REASON_DEAUTH_LEAVING,
  887. NULL, 0, 1, GFP_KERNEL);
  888. break;
  889. case QTNF_STA_DISCONNECTED:
  890. break;
  891. }
  892. }
  893. cfg80211_shutdown_all_interfaces(vif->wdev.wiphy);
  894. vif->sta_state = QTNF_STA_DISCONNECTED;
  895. }
  896. void qtnf_band_init_rates(struct ieee80211_supported_band *band)
  897. {
  898. switch (band->band) {
  899. case NL80211_BAND_2GHZ:
  900. band->bitrates = qtnf_rates_2g;
  901. band->n_bitrates = ARRAY_SIZE(qtnf_rates_2g);
  902. break;
  903. case NL80211_BAND_5GHZ:
  904. band->bitrates = qtnf_rates_5g;
  905. band->n_bitrates = ARRAY_SIZE(qtnf_rates_5g);
  906. break;
  907. default:
  908. band->bitrates = NULL;
  909. band->n_bitrates = 0;
  910. break;
  911. }
  912. }