cfg80211.c 26 KB

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