cfg80211.c 28 KB

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