sta_info.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/types.h>
  14. #include <linux/slab.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/timer.h>
  18. #include <linux/rtnetlink.h>
  19. #include <net/mac80211.h>
  20. #include "ieee80211_i.h"
  21. #include "driver-ops.h"
  22. #include "rate.h"
  23. #include "sta_info.h"
  24. #include "debugfs_sta.h"
  25. #include "mesh.h"
  26. #include "wme.h"
  27. /**
  28. * DOC: STA information lifetime rules
  29. *
  30. * STA info structures (&struct sta_info) are managed in a hash table
  31. * for faster lookup and a list for iteration. They are managed using
  32. * RCU, i.e. access to the list and hash table is protected by RCU.
  33. *
  34. * Upon allocating a STA info structure with sta_info_alloc(), the caller
  35. * owns that structure. It must then insert it into the hash table using
  36. * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
  37. * case (which acquires an rcu read section but must not be called from
  38. * within one) will the pointer still be valid after the call. Note that
  39. * the caller may not do much with the STA info before inserting it, in
  40. * particular, it may not start any mesh peer link management or add
  41. * encryption keys.
  42. *
  43. * When the insertion fails (sta_info_insert()) returns non-zero), the
  44. * structure will have been freed by sta_info_insert()!
  45. *
  46. * Station entries are added by mac80211 when you establish a link with a
  47. * peer. This means different things for the different type of interfaces
  48. * we support. For a regular station this mean we add the AP sta when we
  49. * receive an association response from the AP. For IBSS this occurs when
  50. * get to know about a peer on the same IBSS. For WDS we add the sta for
  51. * the peer immediately upon device open. When using AP mode we add stations
  52. * for each respective station upon request from userspace through nl80211.
  53. *
  54. * In order to remove a STA info structure, various sta_info_destroy_*()
  55. * calls are available.
  56. *
  57. * There is no concept of ownership on a STA entry, each structure is
  58. * owned by the global hash table/list until it is removed. All users of
  59. * the structure need to be RCU protected so that the structure won't be
  60. * freed before they are done using it.
  61. */
  62. /* Caller must hold local->sta_mtx */
  63. static int sta_info_hash_del(struct ieee80211_local *local,
  64. struct sta_info *sta)
  65. {
  66. struct sta_info *s;
  67. s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
  68. lockdep_is_held(&local->sta_mtx));
  69. if (!s)
  70. return -ENOENT;
  71. if (s == sta) {
  72. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
  73. s->hnext);
  74. return 0;
  75. }
  76. while (rcu_access_pointer(s->hnext) &&
  77. rcu_access_pointer(s->hnext) != sta)
  78. s = rcu_dereference_protected(s->hnext,
  79. lockdep_is_held(&local->sta_mtx));
  80. if (rcu_access_pointer(s->hnext)) {
  81. rcu_assign_pointer(s->hnext, sta->hnext);
  82. return 0;
  83. }
  84. return -ENOENT;
  85. }
  86. static void __cleanup_single_sta(struct sta_info *sta)
  87. {
  88. int ac, i;
  89. struct tid_ampdu_tx *tid_tx;
  90. struct ieee80211_sub_if_data *sdata = sta->sdata;
  91. struct ieee80211_local *local = sdata->local;
  92. struct ps_data *ps;
  93. if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
  94. test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
  95. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  96. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  97. ps = &sdata->bss->ps;
  98. else if (ieee80211_vif_is_mesh(&sdata->vif))
  99. ps = &sdata->u.mesh.ps;
  100. else
  101. return;
  102. clear_sta_flag(sta, WLAN_STA_PS_STA);
  103. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  104. atomic_dec(&ps->num_sta_ps);
  105. sta_info_recalc_tim(sta);
  106. }
  107. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  108. local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
  109. ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
  110. ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
  111. }
  112. if (ieee80211_vif_is_mesh(&sdata->vif))
  113. mesh_sta_cleanup(sta);
  114. cancel_work_sync(&sta->drv_unblock_wk);
  115. /*
  116. * Destroy aggregation state here. It would be nice to wait for the
  117. * driver to finish aggregation stop and then clean up, but for now
  118. * drivers have to handle aggregation stop being requested, followed
  119. * directly by station destruction.
  120. */
  121. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  122. kfree(sta->ampdu_mlme.tid_start_tx[i]);
  123. tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
  124. if (!tid_tx)
  125. continue;
  126. ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
  127. kfree(tid_tx);
  128. }
  129. }
  130. static void cleanup_single_sta(struct sta_info *sta)
  131. {
  132. struct ieee80211_sub_if_data *sdata = sta->sdata;
  133. struct ieee80211_local *local = sdata->local;
  134. __cleanup_single_sta(sta);
  135. sta_info_free(local, sta);
  136. }
  137. /* protected by RCU */
  138. struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
  139. const u8 *addr)
  140. {
  141. struct ieee80211_local *local = sdata->local;
  142. struct sta_info *sta;
  143. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  144. lockdep_is_held(&local->sta_mtx));
  145. while (sta) {
  146. if (sta->sdata == sdata &&
  147. ether_addr_equal(sta->sta.addr, addr))
  148. break;
  149. sta = rcu_dereference_check(sta->hnext,
  150. lockdep_is_held(&local->sta_mtx));
  151. }
  152. return sta;
  153. }
  154. /*
  155. * Get sta info either from the specified interface
  156. * or from one of its vlans
  157. */
  158. struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
  159. const u8 *addr)
  160. {
  161. struct ieee80211_local *local = sdata->local;
  162. struct sta_info *sta;
  163. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  164. lockdep_is_held(&local->sta_mtx));
  165. while (sta) {
  166. if ((sta->sdata == sdata ||
  167. (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
  168. ether_addr_equal(sta->sta.addr, addr))
  169. break;
  170. sta = rcu_dereference_check(sta->hnext,
  171. lockdep_is_held(&local->sta_mtx));
  172. }
  173. return sta;
  174. }
  175. struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
  176. int idx)
  177. {
  178. struct ieee80211_local *local = sdata->local;
  179. struct sta_info *sta;
  180. int i = 0;
  181. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  182. if (sdata != sta->sdata)
  183. continue;
  184. if (i < idx) {
  185. ++i;
  186. continue;
  187. }
  188. return sta;
  189. }
  190. return NULL;
  191. }
  192. /**
  193. * sta_info_free - free STA
  194. *
  195. * @local: pointer to the global information
  196. * @sta: STA info to free
  197. *
  198. * This function must undo everything done by sta_info_alloc()
  199. * that may happen before sta_info_insert(). It may only be
  200. * called when sta_info_insert() has not been attempted (and
  201. * if that fails, the station is freed anyway.)
  202. */
  203. void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
  204. {
  205. int i;
  206. if (sta->rate_ctrl)
  207. rate_control_free_sta(sta);
  208. if (sta->tx_lat) {
  209. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  210. kfree(sta->tx_lat[i].bins);
  211. kfree(sta->tx_lat);
  212. }
  213. sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
  214. kfree(rcu_dereference_raw(sta->sta.rates));
  215. kfree(sta);
  216. }
  217. /* Caller must hold local->sta_mtx */
  218. static void sta_info_hash_add(struct ieee80211_local *local,
  219. struct sta_info *sta)
  220. {
  221. lockdep_assert_held(&local->sta_mtx);
  222. sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
  223. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
  224. }
  225. static void sta_unblock(struct work_struct *wk)
  226. {
  227. struct sta_info *sta;
  228. sta = container_of(wk, struct sta_info, drv_unblock_wk);
  229. if (sta->dead)
  230. return;
  231. if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
  232. local_bh_disable();
  233. ieee80211_sta_ps_deliver_wakeup(sta);
  234. local_bh_enable();
  235. } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
  236. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  237. local_bh_disable();
  238. ieee80211_sta_ps_deliver_poll_response(sta);
  239. local_bh_enable();
  240. } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
  241. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  242. local_bh_disable();
  243. ieee80211_sta_ps_deliver_uapsd(sta);
  244. local_bh_enable();
  245. } else
  246. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  247. }
  248. static int sta_prepare_rate_control(struct ieee80211_local *local,
  249. struct sta_info *sta, gfp_t gfp)
  250. {
  251. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  252. return 0;
  253. sta->rate_ctrl = local->rate_ctrl;
  254. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  255. &sta->sta, gfp);
  256. if (!sta->rate_ctrl_priv)
  257. return -ENOMEM;
  258. return 0;
  259. }
  260. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  261. const u8 *addr, gfp_t gfp)
  262. {
  263. struct ieee80211_local *local = sdata->local;
  264. struct sta_info *sta;
  265. struct timespec uptime;
  266. struct ieee80211_tx_latency_bin_ranges *tx_latency;
  267. int i;
  268. sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
  269. if (!sta)
  270. return NULL;
  271. rcu_read_lock();
  272. tx_latency = rcu_dereference(local->tx_latency);
  273. /* init stations Tx latency statistics && TID bins */
  274. if (tx_latency) {
  275. sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS *
  276. sizeof(struct ieee80211_tx_latency_stat),
  277. GFP_ATOMIC);
  278. if (!sta->tx_lat) {
  279. rcu_read_unlock();
  280. goto free;
  281. }
  282. if (tx_latency->n_ranges) {
  283. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  284. /* size of bins is size of the ranges +1 */
  285. sta->tx_lat[i].bin_count =
  286. tx_latency->n_ranges + 1;
  287. sta->tx_lat[i].bins =
  288. kcalloc(sta->tx_lat[i].bin_count,
  289. sizeof(u32), GFP_ATOMIC);
  290. if (!sta->tx_lat[i].bins) {
  291. rcu_read_unlock();
  292. goto free;
  293. }
  294. }
  295. }
  296. }
  297. rcu_read_unlock();
  298. spin_lock_init(&sta->lock);
  299. spin_lock_init(&sta->ps_lock);
  300. INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
  301. INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
  302. mutex_init(&sta->ampdu_mlme.mtx);
  303. #ifdef CONFIG_MAC80211_MESH
  304. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  305. !sdata->u.mesh.user_mpm)
  306. init_timer(&sta->plink_timer);
  307. sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
  308. #endif
  309. memcpy(sta->sta.addr, addr, ETH_ALEN);
  310. sta->local = local;
  311. sta->sdata = sdata;
  312. sta->last_rx = jiffies;
  313. sta->sta_state = IEEE80211_STA_NONE;
  314. do_posix_clock_monotonic_gettime(&uptime);
  315. sta->last_connected = uptime.tv_sec;
  316. ewma_init(&sta->avg_signal, 1024, 8);
  317. for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
  318. ewma_init(&sta->chain_signal_avg[i], 1024, 8);
  319. if (sta_prepare_rate_control(local, sta, gfp))
  320. goto free;
  321. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  322. /*
  323. * timer_to_tid must be initialized with identity mapping
  324. * to enable session_timer's data differentiation. See
  325. * sta_rx_agg_session_timer_expired for usage.
  326. */
  327. sta->timer_to_tid[i] = i;
  328. }
  329. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  330. skb_queue_head_init(&sta->ps_tx_buf[i]);
  331. skb_queue_head_init(&sta->tx_filtered[i]);
  332. }
  333. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  334. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  335. sta->sta.smps_mode = IEEE80211_SMPS_OFF;
  336. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  337. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  338. struct ieee80211_supported_band *sband =
  339. local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
  340. u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
  341. IEEE80211_HT_CAP_SM_PS_SHIFT;
  342. /*
  343. * Assume that hostapd advertises our caps in the beacon and
  344. * this is the known_smps_mode for a station that just assciated
  345. */
  346. switch (smps) {
  347. case WLAN_HT_SMPS_CONTROL_DISABLED:
  348. sta->known_smps_mode = IEEE80211_SMPS_OFF;
  349. break;
  350. case WLAN_HT_SMPS_CONTROL_STATIC:
  351. sta->known_smps_mode = IEEE80211_SMPS_STATIC;
  352. break;
  353. case WLAN_HT_SMPS_CONTROL_DYNAMIC:
  354. sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
  355. break;
  356. default:
  357. WARN_ON(1);
  358. }
  359. }
  360. sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
  361. return sta;
  362. free:
  363. if (sta->tx_lat) {
  364. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  365. kfree(sta->tx_lat[i].bins);
  366. kfree(sta->tx_lat);
  367. }
  368. kfree(sta);
  369. return NULL;
  370. }
  371. static int sta_info_insert_check(struct sta_info *sta)
  372. {
  373. struct ieee80211_sub_if_data *sdata = sta->sdata;
  374. /*
  375. * Can't be a WARN_ON because it can be triggered through a race:
  376. * something inserts a STA (on one CPU) without holding the RTNL
  377. * and another CPU turns off the net device.
  378. */
  379. if (unlikely(!ieee80211_sdata_running(sdata)))
  380. return -ENETDOWN;
  381. if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
  382. is_multicast_ether_addr(sta->sta.addr)))
  383. return -EINVAL;
  384. return 0;
  385. }
  386. static int sta_info_insert_drv_state(struct ieee80211_local *local,
  387. struct ieee80211_sub_if_data *sdata,
  388. struct sta_info *sta)
  389. {
  390. enum ieee80211_sta_state state;
  391. int err = 0;
  392. for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
  393. err = drv_sta_state(local, sdata, sta, state, state + 1);
  394. if (err)
  395. break;
  396. }
  397. if (!err) {
  398. /*
  399. * Drivers using legacy sta_add/sta_remove callbacks only
  400. * get uploaded set to true after sta_add is called.
  401. */
  402. if (!local->ops->sta_add)
  403. sta->uploaded = true;
  404. return 0;
  405. }
  406. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  407. sdata_info(sdata,
  408. "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
  409. sta->sta.addr, state + 1, err);
  410. err = 0;
  411. }
  412. /* unwind on error */
  413. for (; state > IEEE80211_STA_NOTEXIST; state--)
  414. WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
  415. return err;
  416. }
  417. /*
  418. * should be called with sta_mtx locked
  419. * this function replaces the mutex lock
  420. * with a RCU lock
  421. */
  422. static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
  423. {
  424. struct ieee80211_local *local = sta->local;
  425. struct ieee80211_sub_if_data *sdata = sta->sdata;
  426. struct station_info sinfo;
  427. int err = 0;
  428. lockdep_assert_held(&local->sta_mtx);
  429. /* check if STA exists already */
  430. if (sta_info_get_bss(sdata, sta->sta.addr)) {
  431. err = -EEXIST;
  432. goto out_err;
  433. }
  434. local->num_sta++;
  435. local->sta_generation++;
  436. smp_mb();
  437. /* simplify things and don't accept BA sessions yet */
  438. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  439. /* make the station visible */
  440. sta_info_hash_add(local, sta);
  441. list_add_rcu(&sta->list, &local->sta_list);
  442. /* notify driver */
  443. err = sta_info_insert_drv_state(local, sdata, sta);
  444. if (err)
  445. goto out_remove;
  446. set_sta_flag(sta, WLAN_STA_INSERTED);
  447. /* accept BA sessions now */
  448. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  449. ieee80211_recalc_min_chandef(sdata);
  450. ieee80211_sta_debugfs_add(sta);
  451. rate_control_add_sta_debugfs(sta);
  452. memset(&sinfo, 0, sizeof(sinfo));
  453. sinfo.filled = 0;
  454. sinfo.generation = local->sta_generation;
  455. cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
  456. sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
  457. /* move reference to rcu-protected */
  458. rcu_read_lock();
  459. mutex_unlock(&local->sta_mtx);
  460. if (ieee80211_vif_is_mesh(&sdata->vif))
  461. mesh_accept_plinks_update(sdata);
  462. return 0;
  463. out_remove:
  464. sta_info_hash_del(local, sta);
  465. list_del_rcu(&sta->list);
  466. local->num_sta--;
  467. synchronize_net();
  468. __cleanup_single_sta(sta);
  469. out_err:
  470. mutex_unlock(&local->sta_mtx);
  471. rcu_read_lock();
  472. return err;
  473. }
  474. int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
  475. {
  476. struct ieee80211_local *local = sta->local;
  477. int err;
  478. might_sleep();
  479. err = sta_info_insert_check(sta);
  480. if (err) {
  481. rcu_read_lock();
  482. goto out_free;
  483. }
  484. mutex_lock(&local->sta_mtx);
  485. err = sta_info_insert_finish(sta);
  486. if (err)
  487. goto out_free;
  488. return 0;
  489. out_free:
  490. sta_info_free(local, sta);
  491. return err;
  492. }
  493. int sta_info_insert(struct sta_info *sta)
  494. {
  495. int err = sta_info_insert_rcu(sta);
  496. rcu_read_unlock();
  497. return err;
  498. }
  499. static inline void __bss_tim_set(u8 *tim, u16 id)
  500. {
  501. /*
  502. * This format has been mandated by the IEEE specifications,
  503. * so this line may not be changed to use the __set_bit() format.
  504. */
  505. tim[id / 8] |= (1 << (id % 8));
  506. }
  507. static inline void __bss_tim_clear(u8 *tim, u16 id)
  508. {
  509. /*
  510. * This format has been mandated by the IEEE specifications,
  511. * so this line may not be changed to use the __clear_bit() format.
  512. */
  513. tim[id / 8] &= ~(1 << (id % 8));
  514. }
  515. static inline bool __bss_tim_get(u8 *tim, u16 id)
  516. {
  517. /*
  518. * This format has been mandated by the IEEE specifications,
  519. * so this line may not be changed to use the test_bit() format.
  520. */
  521. return tim[id / 8] & (1 << (id % 8));
  522. }
  523. static unsigned long ieee80211_tids_for_ac(int ac)
  524. {
  525. /* If we ever support TIDs > 7, this obviously needs to be adjusted */
  526. switch (ac) {
  527. case IEEE80211_AC_VO:
  528. return BIT(6) | BIT(7);
  529. case IEEE80211_AC_VI:
  530. return BIT(4) | BIT(5);
  531. case IEEE80211_AC_BE:
  532. return BIT(0) | BIT(3);
  533. case IEEE80211_AC_BK:
  534. return BIT(1) | BIT(2);
  535. default:
  536. WARN_ON(1);
  537. return 0;
  538. }
  539. }
  540. void sta_info_recalc_tim(struct sta_info *sta)
  541. {
  542. struct ieee80211_local *local = sta->local;
  543. struct ps_data *ps;
  544. bool indicate_tim = false;
  545. u8 ignore_for_tim = sta->sta.uapsd_queues;
  546. int ac;
  547. u16 id;
  548. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  549. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  550. if (WARN_ON_ONCE(!sta->sdata->bss))
  551. return;
  552. ps = &sta->sdata->bss->ps;
  553. id = sta->sta.aid;
  554. #ifdef CONFIG_MAC80211_MESH
  555. } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
  556. ps = &sta->sdata->u.mesh.ps;
  557. /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
  558. id = sta->plid % (IEEE80211_MAX_AID + 1);
  559. #endif
  560. } else {
  561. return;
  562. }
  563. /* No need to do anything if the driver does all */
  564. if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
  565. return;
  566. if (sta->dead)
  567. goto done;
  568. /*
  569. * If all ACs are delivery-enabled then we should build
  570. * the TIM bit for all ACs anyway; if only some are then
  571. * we ignore those and build the TIM bit using only the
  572. * non-enabled ones.
  573. */
  574. if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
  575. ignore_for_tim = 0;
  576. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  577. unsigned long tids;
  578. if (ignore_for_tim & BIT(ac))
  579. continue;
  580. indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
  581. !skb_queue_empty(&sta->ps_tx_buf[ac]);
  582. if (indicate_tim)
  583. break;
  584. tids = ieee80211_tids_for_ac(ac);
  585. indicate_tim |=
  586. sta->driver_buffered_tids & tids;
  587. }
  588. done:
  589. spin_lock_bh(&local->tim_lock);
  590. if (indicate_tim == __bss_tim_get(ps->tim, id))
  591. goto out_unlock;
  592. if (indicate_tim)
  593. __bss_tim_set(ps->tim, id);
  594. else
  595. __bss_tim_clear(ps->tim, id);
  596. if (local->ops->set_tim) {
  597. local->tim_in_locked_section = true;
  598. drv_set_tim(local, &sta->sta, indicate_tim);
  599. local->tim_in_locked_section = false;
  600. }
  601. out_unlock:
  602. spin_unlock_bh(&local->tim_lock);
  603. }
  604. static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
  605. {
  606. struct ieee80211_tx_info *info;
  607. int timeout;
  608. if (!skb)
  609. return false;
  610. info = IEEE80211_SKB_CB(skb);
  611. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  612. timeout = (sta->listen_interval *
  613. sta->sdata->vif.bss_conf.beacon_int *
  614. 32 / 15625) * HZ;
  615. if (timeout < STA_TX_BUFFER_EXPIRE)
  616. timeout = STA_TX_BUFFER_EXPIRE;
  617. return time_after(jiffies, info->control.jiffies + timeout);
  618. }
  619. static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
  620. struct sta_info *sta, int ac)
  621. {
  622. unsigned long flags;
  623. struct sk_buff *skb;
  624. /*
  625. * First check for frames that should expire on the filtered
  626. * queue. Frames here were rejected by the driver and are on
  627. * a separate queue to avoid reordering with normal PS-buffered
  628. * frames. They also aren't accounted for right now in the
  629. * total_ps_buffered counter.
  630. */
  631. for (;;) {
  632. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  633. skb = skb_peek(&sta->tx_filtered[ac]);
  634. if (sta_info_buffer_expired(sta, skb))
  635. skb = __skb_dequeue(&sta->tx_filtered[ac]);
  636. else
  637. skb = NULL;
  638. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  639. /*
  640. * Frames are queued in order, so if this one
  641. * hasn't expired yet we can stop testing. If
  642. * we actually reached the end of the queue we
  643. * also need to stop, of course.
  644. */
  645. if (!skb)
  646. break;
  647. ieee80211_free_txskb(&local->hw, skb);
  648. }
  649. /*
  650. * Now also check the normal PS-buffered queue, this will
  651. * only find something if the filtered queue was emptied
  652. * since the filtered frames are all before the normal PS
  653. * buffered frames.
  654. */
  655. for (;;) {
  656. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  657. skb = skb_peek(&sta->ps_tx_buf[ac]);
  658. if (sta_info_buffer_expired(sta, skb))
  659. skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
  660. else
  661. skb = NULL;
  662. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  663. /*
  664. * frames are queued in order, so if this one
  665. * hasn't expired yet (or we reached the end of
  666. * the queue) we can stop testing
  667. */
  668. if (!skb)
  669. break;
  670. local->total_ps_buffered--;
  671. ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
  672. sta->sta.addr);
  673. ieee80211_free_txskb(&local->hw, skb);
  674. }
  675. /*
  676. * Finally, recalculate the TIM bit for this station -- it might
  677. * now be clear because the station was too slow to retrieve its
  678. * frames.
  679. */
  680. sta_info_recalc_tim(sta);
  681. /*
  682. * Return whether there are any frames still buffered, this is
  683. * used to check whether the cleanup timer still needs to run,
  684. * if there are no frames we don't need to rearm the timer.
  685. */
  686. return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
  687. skb_queue_empty(&sta->tx_filtered[ac]));
  688. }
  689. static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  690. struct sta_info *sta)
  691. {
  692. bool have_buffered = false;
  693. int ac;
  694. /* This is only necessary for stations on BSS/MBSS interfaces */
  695. if (!sta->sdata->bss &&
  696. !ieee80211_vif_is_mesh(&sta->sdata->vif))
  697. return false;
  698. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  699. have_buffered |=
  700. sta_info_cleanup_expire_buffered_ac(local, sta, ac);
  701. return have_buffered;
  702. }
  703. static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
  704. {
  705. struct ieee80211_local *local;
  706. struct ieee80211_sub_if_data *sdata;
  707. int ret;
  708. might_sleep();
  709. if (!sta)
  710. return -ENOENT;
  711. local = sta->local;
  712. sdata = sta->sdata;
  713. lockdep_assert_held(&local->sta_mtx);
  714. /*
  715. * Before removing the station from the driver and
  716. * rate control, it might still start new aggregation
  717. * sessions -- block that to make sure the tear-down
  718. * will be sufficient.
  719. */
  720. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  721. ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
  722. ret = sta_info_hash_del(local, sta);
  723. if (WARN_ON(ret))
  724. return ret;
  725. list_del_rcu(&sta->list);
  726. drv_sta_pre_rcu_remove(local, sta->sdata, sta);
  727. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  728. rcu_access_pointer(sdata->u.vlan.sta) == sta)
  729. RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
  730. return 0;
  731. }
  732. static void __sta_info_destroy_part2(struct sta_info *sta)
  733. {
  734. struct ieee80211_local *local = sta->local;
  735. struct ieee80211_sub_if_data *sdata = sta->sdata;
  736. int ret;
  737. /*
  738. * NOTE: This assumes at least synchronize_net() was done
  739. * after _part1 and before _part2!
  740. */
  741. might_sleep();
  742. lockdep_assert_held(&local->sta_mtx);
  743. /* now keys can no longer be reached */
  744. ieee80211_free_sta_keys(local, sta);
  745. sta->dead = true;
  746. local->num_sta--;
  747. local->sta_generation++;
  748. while (sta->sta_state > IEEE80211_STA_NONE) {
  749. ret = sta_info_move_state(sta, sta->sta_state - 1);
  750. if (ret) {
  751. WARN_ON_ONCE(1);
  752. break;
  753. }
  754. }
  755. if (sta->uploaded) {
  756. ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
  757. IEEE80211_STA_NOTEXIST);
  758. WARN_ON_ONCE(ret != 0);
  759. }
  760. sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
  761. cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
  762. rate_control_remove_sta_debugfs(sta);
  763. ieee80211_sta_debugfs_remove(sta);
  764. ieee80211_recalc_min_chandef(sdata);
  765. cleanup_single_sta(sta);
  766. }
  767. int __must_check __sta_info_destroy(struct sta_info *sta)
  768. {
  769. int err = __sta_info_destroy_part1(sta);
  770. if (err)
  771. return err;
  772. synchronize_net();
  773. __sta_info_destroy_part2(sta);
  774. return 0;
  775. }
  776. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  777. {
  778. struct sta_info *sta;
  779. int ret;
  780. mutex_lock(&sdata->local->sta_mtx);
  781. sta = sta_info_get(sdata, addr);
  782. ret = __sta_info_destroy(sta);
  783. mutex_unlock(&sdata->local->sta_mtx);
  784. return ret;
  785. }
  786. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  787. const u8 *addr)
  788. {
  789. struct sta_info *sta;
  790. int ret;
  791. mutex_lock(&sdata->local->sta_mtx);
  792. sta = sta_info_get_bss(sdata, addr);
  793. ret = __sta_info_destroy(sta);
  794. mutex_unlock(&sdata->local->sta_mtx);
  795. return ret;
  796. }
  797. static void sta_info_cleanup(unsigned long data)
  798. {
  799. struct ieee80211_local *local = (struct ieee80211_local *) data;
  800. struct sta_info *sta;
  801. bool timer_needed = false;
  802. rcu_read_lock();
  803. list_for_each_entry_rcu(sta, &local->sta_list, list)
  804. if (sta_info_cleanup_expire_buffered(local, sta))
  805. timer_needed = true;
  806. rcu_read_unlock();
  807. if (local->quiescing)
  808. return;
  809. if (!timer_needed)
  810. return;
  811. mod_timer(&local->sta_cleanup,
  812. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  813. }
  814. void sta_info_init(struct ieee80211_local *local)
  815. {
  816. spin_lock_init(&local->tim_lock);
  817. mutex_init(&local->sta_mtx);
  818. INIT_LIST_HEAD(&local->sta_list);
  819. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  820. (unsigned long)local);
  821. }
  822. void sta_info_stop(struct ieee80211_local *local)
  823. {
  824. del_timer_sync(&local->sta_cleanup);
  825. }
  826. int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
  827. {
  828. struct ieee80211_local *local = sdata->local;
  829. struct sta_info *sta, *tmp;
  830. LIST_HEAD(free_list);
  831. int ret = 0;
  832. might_sleep();
  833. WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
  834. WARN_ON(vlans && !sdata->bss);
  835. mutex_lock(&local->sta_mtx);
  836. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  837. if (sdata == sta->sdata ||
  838. (vlans && sdata->bss == sta->sdata->bss)) {
  839. if (!WARN_ON(__sta_info_destroy_part1(sta)))
  840. list_add(&sta->free_list, &free_list);
  841. ret++;
  842. }
  843. }
  844. if (!list_empty(&free_list)) {
  845. synchronize_net();
  846. list_for_each_entry_safe(sta, tmp, &free_list, free_list)
  847. __sta_info_destroy_part2(sta);
  848. }
  849. mutex_unlock(&local->sta_mtx);
  850. return ret;
  851. }
  852. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  853. unsigned long exp_time)
  854. {
  855. struct ieee80211_local *local = sdata->local;
  856. struct sta_info *sta, *tmp;
  857. mutex_lock(&local->sta_mtx);
  858. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  859. if (sdata != sta->sdata)
  860. continue;
  861. if (time_after(jiffies, sta->last_rx + exp_time)) {
  862. sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
  863. sta->sta.addr);
  864. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  865. test_sta_flag(sta, WLAN_STA_PS_STA))
  866. atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
  867. WARN_ON(__sta_info_destroy(sta));
  868. }
  869. }
  870. mutex_unlock(&local->sta_mtx);
  871. }
  872. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  873. const u8 *addr,
  874. const u8 *localaddr)
  875. {
  876. struct sta_info *sta, *nxt;
  877. /*
  878. * Just return a random station if localaddr is NULL
  879. * ... first in list.
  880. */
  881. for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
  882. if (localaddr &&
  883. !ether_addr_equal(sta->sdata->vif.addr, localaddr))
  884. continue;
  885. if (!sta->uploaded)
  886. return NULL;
  887. return &sta->sta;
  888. }
  889. return NULL;
  890. }
  891. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  892. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  893. const u8 *addr)
  894. {
  895. struct sta_info *sta;
  896. if (!vif)
  897. return NULL;
  898. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  899. if (!sta)
  900. return NULL;
  901. if (!sta->uploaded)
  902. return NULL;
  903. return &sta->sta;
  904. }
  905. EXPORT_SYMBOL(ieee80211_find_sta);
  906. /* powersave support code */
  907. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  908. {
  909. struct ieee80211_sub_if_data *sdata = sta->sdata;
  910. struct ieee80211_local *local = sdata->local;
  911. struct sk_buff_head pending;
  912. int filtered = 0, buffered = 0, ac;
  913. unsigned long flags;
  914. struct ps_data *ps;
  915. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  916. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  917. ps = &sdata->bss->ps;
  918. else if (ieee80211_vif_is_mesh(&sdata->vif))
  919. ps = &sdata->u.mesh.ps;
  920. else
  921. return;
  922. clear_sta_flag(sta, WLAN_STA_SP);
  923. BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
  924. sta->driver_buffered_tids = 0;
  925. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  926. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  927. skb_queue_head_init(&pending);
  928. /* sync with ieee80211_tx_h_unicast_ps_buf */
  929. spin_lock(&sta->ps_lock);
  930. /* Send all buffered frames to the station */
  931. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  932. int count = skb_queue_len(&pending), tmp;
  933. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  934. skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
  935. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  936. tmp = skb_queue_len(&pending);
  937. filtered += tmp - count;
  938. count = tmp;
  939. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  940. skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
  941. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  942. tmp = skb_queue_len(&pending);
  943. buffered += tmp - count;
  944. }
  945. ieee80211_add_pending_skbs(local, &pending);
  946. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  947. clear_sta_flag(sta, WLAN_STA_PS_STA);
  948. spin_unlock(&sta->ps_lock);
  949. atomic_dec(&ps->num_sta_ps);
  950. /* This station just woke up and isn't aware of our SMPS state */
  951. if (!ieee80211_vif_is_mesh(&sdata->vif) &&
  952. !ieee80211_smps_is_restrictive(sta->known_smps_mode,
  953. sdata->smps_mode) &&
  954. sta->known_smps_mode != sdata->bss->req_smps &&
  955. sta_info_tx_streams(sta) != 1) {
  956. ht_dbg(sdata,
  957. "%pM just woke up and MIMO capable - update SMPS\n",
  958. sta->sta.addr);
  959. ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
  960. sta->sta.addr,
  961. sdata->vif.bss_conf.bssid);
  962. }
  963. local->total_ps_buffered -= buffered;
  964. sta_info_recalc_tim(sta);
  965. ps_dbg(sdata,
  966. "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
  967. sta->sta.addr, sta->sta.aid, filtered, buffered);
  968. }
  969. static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
  970. struct sta_info *sta, int tid,
  971. enum ieee80211_frame_release_type reason,
  972. bool call_driver)
  973. {
  974. struct ieee80211_local *local = sdata->local;
  975. struct ieee80211_qos_hdr *nullfunc;
  976. struct sk_buff *skb;
  977. int size = sizeof(*nullfunc);
  978. __le16 fc;
  979. bool qos = test_sta_flag(sta, WLAN_STA_WME);
  980. struct ieee80211_tx_info *info;
  981. struct ieee80211_chanctx_conf *chanctx_conf;
  982. if (qos) {
  983. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  984. IEEE80211_STYPE_QOS_NULLFUNC |
  985. IEEE80211_FCTL_FROMDS);
  986. } else {
  987. size -= 2;
  988. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  989. IEEE80211_STYPE_NULLFUNC |
  990. IEEE80211_FCTL_FROMDS);
  991. }
  992. skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
  993. if (!skb)
  994. return;
  995. skb_reserve(skb, local->hw.extra_tx_headroom);
  996. nullfunc = (void *) skb_put(skb, size);
  997. nullfunc->frame_control = fc;
  998. nullfunc->duration_id = 0;
  999. memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
  1000. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  1001. memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
  1002. nullfunc->seq_ctrl = 0;
  1003. skb->priority = tid;
  1004. skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
  1005. if (qos) {
  1006. nullfunc->qos_ctrl = cpu_to_le16(tid);
  1007. if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
  1008. nullfunc->qos_ctrl |=
  1009. cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
  1010. }
  1011. info = IEEE80211_SKB_CB(skb);
  1012. /*
  1013. * Tell TX path to send this frame even though the
  1014. * STA may still remain is PS mode after this frame
  1015. * exchange. Also set EOSP to indicate this packet
  1016. * ends the poll/service period.
  1017. */
  1018. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
  1019. IEEE80211_TX_CTL_PS_RESPONSE |
  1020. IEEE80211_TX_STATUS_EOSP |
  1021. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1022. if (call_driver)
  1023. drv_allow_buffered_frames(local, sta, BIT(tid), 1,
  1024. reason, false);
  1025. skb->dev = sdata->dev;
  1026. rcu_read_lock();
  1027. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  1028. if (WARN_ON(!chanctx_conf)) {
  1029. rcu_read_unlock();
  1030. kfree_skb(skb);
  1031. return;
  1032. }
  1033. ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band);
  1034. rcu_read_unlock();
  1035. }
  1036. static int find_highest_prio_tid(unsigned long tids)
  1037. {
  1038. /* lower 3 TIDs aren't ordered perfectly */
  1039. if (tids & 0xF8)
  1040. return fls(tids) - 1;
  1041. /* TID 0 is BE just like TID 3 */
  1042. if (tids & BIT(0))
  1043. return 0;
  1044. return fls(tids) - 1;
  1045. }
  1046. static void
  1047. ieee80211_sta_ps_deliver_response(struct sta_info *sta,
  1048. int n_frames, u8 ignored_acs,
  1049. enum ieee80211_frame_release_type reason)
  1050. {
  1051. struct ieee80211_sub_if_data *sdata = sta->sdata;
  1052. struct ieee80211_local *local = sdata->local;
  1053. bool more_data = false;
  1054. int ac;
  1055. unsigned long driver_release_tids = 0;
  1056. struct sk_buff_head frames;
  1057. /* Service or PS-Poll period starts */
  1058. set_sta_flag(sta, WLAN_STA_SP);
  1059. __skb_queue_head_init(&frames);
  1060. /* Get response frame(s) and more data bit for the last one. */
  1061. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  1062. unsigned long tids;
  1063. if (ignored_acs & BIT(ac))
  1064. continue;
  1065. tids = ieee80211_tids_for_ac(ac);
  1066. /* if we already have frames from software, then we can't also
  1067. * release from hardware queues
  1068. */
  1069. if (skb_queue_empty(&frames))
  1070. driver_release_tids |= sta->driver_buffered_tids & tids;
  1071. if (driver_release_tids) {
  1072. /* If the driver has data on more than one TID then
  1073. * certainly there's more data if we release just a
  1074. * single frame now (from a single TID). This will
  1075. * only happen for PS-Poll.
  1076. */
  1077. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
  1078. hweight16(driver_release_tids) > 1) {
  1079. more_data = true;
  1080. driver_release_tids =
  1081. BIT(find_highest_prio_tid(
  1082. driver_release_tids));
  1083. break;
  1084. }
  1085. } else {
  1086. struct sk_buff *skb;
  1087. while (n_frames > 0) {
  1088. skb = skb_dequeue(&sta->tx_filtered[ac]);
  1089. if (!skb) {
  1090. skb = skb_dequeue(
  1091. &sta->ps_tx_buf[ac]);
  1092. if (skb)
  1093. local->total_ps_buffered--;
  1094. }
  1095. if (!skb)
  1096. break;
  1097. n_frames--;
  1098. __skb_queue_tail(&frames, skb);
  1099. }
  1100. }
  1101. /* If we have more frames buffered on this AC, then set the
  1102. * more-data bit and abort the loop since we can't send more
  1103. * data from other ACs before the buffered frames from this.
  1104. */
  1105. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  1106. !skb_queue_empty(&sta->ps_tx_buf[ac])) {
  1107. more_data = true;
  1108. break;
  1109. }
  1110. }
  1111. if (skb_queue_empty(&frames) && !driver_release_tids) {
  1112. int tid;
  1113. /*
  1114. * For PS-Poll, this can only happen due to a race condition
  1115. * when we set the TIM bit and the station notices it, but
  1116. * before it can poll for the frame we expire it.
  1117. *
  1118. * For uAPSD, this is said in the standard (11.2.1.5 h):
  1119. * At each unscheduled SP for a non-AP STA, the AP shall
  1120. * attempt to transmit at least one MSDU or MMPDU, but no
  1121. * more than the value specified in the Max SP Length field
  1122. * in the QoS Capability element from delivery-enabled ACs,
  1123. * that are destined for the non-AP STA.
  1124. *
  1125. * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
  1126. */
  1127. /* This will evaluate to 1, 3, 5 or 7. */
  1128. tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
  1129. ieee80211_send_null_response(sdata, sta, tid, reason, true);
  1130. } else if (!driver_release_tids) {
  1131. struct sk_buff_head pending;
  1132. struct sk_buff *skb;
  1133. int num = 0;
  1134. u16 tids = 0;
  1135. bool need_null = false;
  1136. skb_queue_head_init(&pending);
  1137. while ((skb = __skb_dequeue(&frames))) {
  1138. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1139. struct ieee80211_hdr *hdr = (void *) skb->data;
  1140. u8 *qoshdr = NULL;
  1141. num++;
  1142. /*
  1143. * Tell TX path to send this frame even though the
  1144. * STA may still remain is PS mode after this frame
  1145. * exchange.
  1146. */
  1147. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
  1148. IEEE80211_TX_CTL_PS_RESPONSE;
  1149. /*
  1150. * Use MoreData flag to indicate whether there are
  1151. * more buffered frames for this STA
  1152. */
  1153. if (more_data || !skb_queue_empty(&frames))
  1154. hdr->frame_control |=
  1155. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1156. else
  1157. hdr->frame_control &=
  1158. cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  1159. if (ieee80211_is_data_qos(hdr->frame_control) ||
  1160. ieee80211_is_qos_nullfunc(hdr->frame_control))
  1161. qoshdr = ieee80211_get_qos_ctl(hdr);
  1162. tids |= BIT(skb->priority);
  1163. __skb_queue_tail(&pending, skb);
  1164. /* end service period after last frame or add one */
  1165. if (!skb_queue_empty(&frames))
  1166. continue;
  1167. if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
  1168. /* for PS-Poll, there's only one frame */
  1169. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1170. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1171. break;
  1172. }
  1173. /* For uAPSD, things are a bit more complicated. If the
  1174. * last frame has a QoS header (i.e. is a QoS-data or
  1175. * QoS-nulldata frame) then just set the EOSP bit there
  1176. * and be done.
  1177. * If the frame doesn't have a QoS header (which means
  1178. * it should be a bufferable MMPDU) then we can't set
  1179. * the EOSP bit in the QoS header; add a QoS-nulldata
  1180. * frame to the list to send it after the MMPDU.
  1181. *
  1182. * Note that this code is only in the mac80211-release
  1183. * code path, we assume that the driver will not buffer
  1184. * anything but QoS-data frames, or if it does, will
  1185. * create the QoS-nulldata frame by itself if needed.
  1186. *
  1187. * Cf. 802.11-2012 10.2.1.10 (c).
  1188. */
  1189. if (qoshdr) {
  1190. *qoshdr |= IEEE80211_QOS_CTL_EOSP;
  1191. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1192. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1193. } else {
  1194. /* The standard isn't completely clear on this
  1195. * as it says the more-data bit should be set
  1196. * if there are more BUs. The QoS-Null frame
  1197. * we're about to send isn't buffered yet, we
  1198. * only create it below, but let's pretend it
  1199. * was buffered just in case some clients only
  1200. * expect more-data=0 when eosp=1.
  1201. */
  1202. hdr->frame_control |=
  1203. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1204. need_null = true;
  1205. num++;
  1206. }
  1207. break;
  1208. }
  1209. drv_allow_buffered_frames(local, sta, tids, num,
  1210. reason, more_data);
  1211. ieee80211_add_pending_skbs(local, &pending);
  1212. if (need_null)
  1213. ieee80211_send_null_response(
  1214. sdata, sta, find_highest_prio_tid(tids),
  1215. reason, false);
  1216. sta_info_recalc_tim(sta);
  1217. } else {
  1218. /*
  1219. * We need to release a frame that is buffered somewhere in the
  1220. * driver ... it'll have to handle that.
  1221. * Note that the driver also has to check the number of frames
  1222. * on the TIDs we're releasing from - if there are more than
  1223. * n_frames it has to set the more-data bit (if we didn't ask
  1224. * it to set it anyway due to other buffered frames); if there
  1225. * are fewer than n_frames it has to make sure to adjust that
  1226. * to allow the service period to end properly.
  1227. */
  1228. drv_release_buffered_frames(local, sta, driver_release_tids,
  1229. n_frames, reason, more_data);
  1230. /*
  1231. * Note that we don't recalculate the TIM bit here as it would
  1232. * most likely have no effect at all unless the driver told us
  1233. * that the TID(s) became empty before returning here from the
  1234. * release function.
  1235. * Either way, however, when the driver tells us that the TID(s)
  1236. * became empty we'll do the TIM recalculation.
  1237. */
  1238. }
  1239. }
  1240. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  1241. {
  1242. u8 ignore_for_response = sta->sta.uapsd_queues;
  1243. /*
  1244. * If all ACs are delivery-enabled then we should reply
  1245. * from any of them, if only some are enabled we reply
  1246. * only from the non-enabled ones.
  1247. */
  1248. if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
  1249. ignore_for_response = 0;
  1250. ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
  1251. IEEE80211_FRAME_RELEASE_PSPOLL);
  1252. }
  1253. void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
  1254. {
  1255. int n_frames = sta->sta.max_sp;
  1256. u8 delivery_enabled = sta->sta.uapsd_queues;
  1257. /*
  1258. * If we ever grow support for TSPEC this might happen if
  1259. * the TSPEC update from hostapd comes in between a trigger
  1260. * frame setting WLAN_STA_UAPSD in the RX path and this
  1261. * actually getting called.
  1262. */
  1263. if (!delivery_enabled)
  1264. return;
  1265. switch (sta->sta.max_sp) {
  1266. case 1:
  1267. n_frames = 2;
  1268. break;
  1269. case 2:
  1270. n_frames = 4;
  1271. break;
  1272. case 3:
  1273. n_frames = 6;
  1274. break;
  1275. case 0:
  1276. /* XXX: what is a good value? */
  1277. n_frames = 8;
  1278. break;
  1279. }
  1280. ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
  1281. IEEE80211_FRAME_RELEASE_UAPSD);
  1282. }
  1283. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  1284. struct ieee80211_sta *pubsta, bool block)
  1285. {
  1286. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1287. trace_api_sta_block_awake(sta->local, pubsta, block);
  1288. if (block)
  1289. set_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1290. else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1291. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  1292. }
  1293. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  1294. void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
  1295. {
  1296. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1297. struct ieee80211_local *local = sta->local;
  1298. trace_api_eosp(local, pubsta);
  1299. clear_sta_flag(sta, WLAN_STA_SP);
  1300. }
  1301. EXPORT_SYMBOL(ieee80211_sta_eosp);
  1302. void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
  1303. u8 tid, bool buffered)
  1304. {
  1305. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1306. if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
  1307. return;
  1308. trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
  1309. if (buffered)
  1310. set_bit(tid, &sta->driver_buffered_tids);
  1311. else
  1312. clear_bit(tid, &sta->driver_buffered_tids);
  1313. sta_info_recalc_tim(sta);
  1314. }
  1315. EXPORT_SYMBOL(ieee80211_sta_set_buffered);
  1316. int sta_info_move_state(struct sta_info *sta,
  1317. enum ieee80211_sta_state new_state)
  1318. {
  1319. might_sleep();
  1320. if (sta->sta_state == new_state)
  1321. return 0;
  1322. /* check allowed transitions first */
  1323. switch (new_state) {
  1324. case IEEE80211_STA_NONE:
  1325. if (sta->sta_state != IEEE80211_STA_AUTH)
  1326. return -EINVAL;
  1327. break;
  1328. case IEEE80211_STA_AUTH:
  1329. if (sta->sta_state != IEEE80211_STA_NONE &&
  1330. sta->sta_state != IEEE80211_STA_ASSOC)
  1331. return -EINVAL;
  1332. break;
  1333. case IEEE80211_STA_ASSOC:
  1334. if (sta->sta_state != IEEE80211_STA_AUTH &&
  1335. sta->sta_state != IEEE80211_STA_AUTHORIZED)
  1336. return -EINVAL;
  1337. break;
  1338. case IEEE80211_STA_AUTHORIZED:
  1339. if (sta->sta_state != IEEE80211_STA_ASSOC)
  1340. return -EINVAL;
  1341. break;
  1342. default:
  1343. WARN(1, "invalid state %d", new_state);
  1344. return -EINVAL;
  1345. }
  1346. sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
  1347. sta->sta.addr, new_state);
  1348. /*
  1349. * notify the driver before the actual changes so it can
  1350. * fail the transition
  1351. */
  1352. if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
  1353. int err = drv_sta_state(sta->local, sta->sdata, sta,
  1354. sta->sta_state, new_state);
  1355. if (err)
  1356. return err;
  1357. }
  1358. /* reflect the change in all state variables */
  1359. switch (new_state) {
  1360. case IEEE80211_STA_NONE:
  1361. if (sta->sta_state == IEEE80211_STA_AUTH)
  1362. clear_bit(WLAN_STA_AUTH, &sta->_flags);
  1363. break;
  1364. case IEEE80211_STA_AUTH:
  1365. if (sta->sta_state == IEEE80211_STA_NONE)
  1366. set_bit(WLAN_STA_AUTH, &sta->_flags);
  1367. else if (sta->sta_state == IEEE80211_STA_ASSOC)
  1368. clear_bit(WLAN_STA_ASSOC, &sta->_flags);
  1369. break;
  1370. case IEEE80211_STA_ASSOC:
  1371. if (sta->sta_state == IEEE80211_STA_AUTH) {
  1372. set_bit(WLAN_STA_ASSOC, &sta->_flags);
  1373. } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
  1374. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1375. (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1376. !sta->sdata->u.vlan.sta))
  1377. atomic_dec(&sta->sdata->bss->num_mcast_sta);
  1378. clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1379. }
  1380. break;
  1381. case IEEE80211_STA_AUTHORIZED:
  1382. if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1383. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1384. (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1385. !sta->sdata->u.vlan.sta))
  1386. atomic_inc(&sta->sdata->bss->num_mcast_sta);
  1387. set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1388. }
  1389. break;
  1390. default:
  1391. break;
  1392. }
  1393. sta->sta_state = new_state;
  1394. return 0;
  1395. }
  1396. u8 sta_info_tx_streams(struct sta_info *sta)
  1397. {
  1398. struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
  1399. u8 rx_streams;
  1400. if (!sta->sta.ht_cap.ht_supported)
  1401. return 1;
  1402. if (sta->sta.vht_cap.vht_supported) {
  1403. int i;
  1404. u16 tx_mcs_map =
  1405. le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
  1406. for (i = 7; i >= 0; i--)
  1407. if ((tx_mcs_map & (0x3 << (i * 2))) !=
  1408. IEEE80211_VHT_MCS_NOT_SUPPORTED)
  1409. return i + 1;
  1410. }
  1411. if (ht_cap->mcs.rx_mask[3])
  1412. rx_streams = 4;
  1413. else if (ht_cap->mcs.rx_mask[2])
  1414. rx_streams = 3;
  1415. else if (ht_cap->mcs.rx_mask[1])
  1416. rx_streams = 2;
  1417. else
  1418. rx_streams = 1;
  1419. if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
  1420. return rx_streams;
  1421. return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
  1422. >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
  1423. }