sta_info.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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. /*
  94. * At this point, when being called as call_rcu callback,
  95. * neither mac80211 nor the driver can reference this
  96. * sta struct any more except by still existing timers
  97. * associated with this station that we clean up below.
  98. *
  99. * Note though that this still uses the sdata and even
  100. * calls the driver in AP and mesh mode, so interfaces
  101. * of those types mush use call sta_info_flush_cleanup()
  102. * (typically via sta_info_flush()) before deconfiguring
  103. * the driver.
  104. *
  105. * In station mode, nothing happens here so it doesn't
  106. * have to (and doesn't) do that, this is intentional to
  107. * speed up roaming.
  108. */
  109. if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
  110. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  111. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  112. ps = &sdata->bss->ps;
  113. else if (ieee80211_vif_is_mesh(&sdata->vif))
  114. ps = &sdata->u.mesh.ps;
  115. else
  116. return;
  117. clear_sta_flag(sta, WLAN_STA_PS_STA);
  118. atomic_dec(&ps->num_sta_ps);
  119. sta_info_recalc_tim(sta);
  120. }
  121. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  122. local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
  123. ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
  124. ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
  125. }
  126. if (ieee80211_vif_is_mesh(&sdata->vif))
  127. mesh_sta_cleanup(sta);
  128. cancel_work_sync(&sta->drv_unblock_wk);
  129. /*
  130. * Destroy aggregation state here. It would be nice to wait for the
  131. * driver to finish aggregation stop and then clean up, but for now
  132. * drivers have to handle aggregation stop being requested, followed
  133. * directly by station destruction.
  134. */
  135. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  136. tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
  137. if (!tid_tx)
  138. continue;
  139. ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
  140. kfree(tid_tx);
  141. }
  142. sta_info_free(local, sta);
  143. }
  144. void ieee80211_cleanup_sdata_stas(struct ieee80211_sub_if_data *sdata)
  145. {
  146. struct sta_info *sta;
  147. spin_lock_bh(&sdata->cleanup_stations_lock);
  148. while (!list_empty(&sdata->cleanup_stations)) {
  149. sta = list_first_entry(&sdata->cleanup_stations,
  150. struct sta_info, list);
  151. list_del(&sta->list);
  152. spin_unlock_bh(&sdata->cleanup_stations_lock);
  153. cleanup_single_sta(sta);
  154. spin_lock_bh(&sdata->cleanup_stations_lock);
  155. }
  156. spin_unlock_bh(&sdata->cleanup_stations_lock);
  157. }
  158. static void free_sta_rcu(struct rcu_head *h)
  159. {
  160. struct sta_info *sta = container_of(h, struct sta_info, rcu_head);
  161. struct ieee80211_sub_if_data *sdata = sta->sdata;
  162. spin_lock(&sdata->cleanup_stations_lock);
  163. list_add_tail(&sta->list, &sdata->cleanup_stations);
  164. spin_unlock(&sdata->cleanup_stations_lock);
  165. ieee80211_queue_work(&sdata->local->hw, &sdata->cleanup_stations_wk);
  166. }
  167. /* protected by RCU */
  168. struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
  169. const u8 *addr)
  170. {
  171. struct ieee80211_local *local = sdata->local;
  172. struct sta_info *sta;
  173. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  174. lockdep_is_held(&local->sta_mtx));
  175. while (sta) {
  176. if (sta->sdata == sdata &&
  177. ether_addr_equal(sta->sta.addr, addr))
  178. break;
  179. sta = rcu_dereference_check(sta->hnext,
  180. lockdep_is_held(&local->sta_mtx));
  181. }
  182. return sta;
  183. }
  184. /*
  185. * Get sta info either from the specified interface
  186. * or from one of its vlans
  187. */
  188. struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
  189. const u8 *addr)
  190. {
  191. struct ieee80211_local *local = sdata->local;
  192. struct sta_info *sta;
  193. sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
  194. lockdep_is_held(&local->sta_mtx));
  195. while (sta) {
  196. if ((sta->sdata == sdata ||
  197. (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
  198. ether_addr_equal(sta->sta.addr, addr))
  199. break;
  200. sta = rcu_dereference_check(sta->hnext,
  201. lockdep_is_held(&local->sta_mtx));
  202. }
  203. return sta;
  204. }
  205. struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
  206. int idx)
  207. {
  208. struct ieee80211_local *local = sdata->local;
  209. struct sta_info *sta;
  210. int i = 0;
  211. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  212. if (sdata != sta->sdata)
  213. continue;
  214. if (i < idx) {
  215. ++i;
  216. continue;
  217. }
  218. return sta;
  219. }
  220. return NULL;
  221. }
  222. /**
  223. * sta_info_free - free STA
  224. *
  225. * @local: pointer to the global information
  226. * @sta: STA info to free
  227. *
  228. * This function must undo everything done by sta_info_alloc()
  229. * that may happen before sta_info_insert(). It may only be
  230. * called when sta_info_insert() has not been attempted (and
  231. * if that fails, the station is freed anyway.)
  232. */
  233. void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
  234. {
  235. if (sta->rate_ctrl)
  236. rate_control_free_sta(sta);
  237. sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
  238. kfree(sta);
  239. }
  240. /* Caller must hold local->sta_mtx */
  241. static void sta_info_hash_add(struct ieee80211_local *local,
  242. struct sta_info *sta)
  243. {
  244. lockdep_assert_held(&local->sta_mtx);
  245. sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
  246. rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
  247. }
  248. static void sta_unblock(struct work_struct *wk)
  249. {
  250. struct sta_info *sta;
  251. sta = container_of(wk, struct sta_info, drv_unblock_wk);
  252. if (sta->dead)
  253. return;
  254. if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
  255. local_bh_disable();
  256. ieee80211_sta_ps_deliver_wakeup(sta);
  257. local_bh_enable();
  258. } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
  259. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  260. local_bh_disable();
  261. ieee80211_sta_ps_deliver_poll_response(sta);
  262. local_bh_enable();
  263. } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
  264. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  265. local_bh_disable();
  266. ieee80211_sta_ps_deliver_uapsd(sta);
  267. local_bh_enable();
  268. } else
  269. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  270. }
  271. static int sta_prepare_rate_control(struct ieee80211_local *local,
  272. struct sta_info *sta, gfp_t gfp)
  273. {
  274. if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
  275. return 0;
  276. sta->rate_ctrl = local->rate_ctrl;
  277. sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
  278. &sta->sta, gfp);
  279. if (!sta->rate_ctrl_priv)
  280. return -ENOMEM;
  281. return 0;
  282. }
  283. struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
  284. const u8 *addr, gfp_t gfp)
  285. {
  286. struct ieee80211_local *local = sdata->local;
  287. struct sta_info *sta;
  288. struct timespec uptime;
  289. int i;
  290. sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
  291. if (!sta)
  292. return NULL;
  293. spin_lock_init(&sta->lock);
  294. INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
  295. INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
  296. mutex_init(&sta->ampdu_mlme.mtx);
  297. #ifdef CONFIG_MAC80211_MESH
  298. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  299. !sdata->u.mesh.user_mpm)
  300. init_timer(&sta->plink_timer);
  301. #endif
  302. memcpy(sta->sta.addr, addr, ETH_ALEN);
  303. sta->local = local;
  304. sta->sdata = sdata;
  305. sta->last_rx = jiffies;
  306. sta->sta_state = IEEE80211_STA_NONE;
  307. do_posix_clock_monotonic_gettime(&uptime);
  308. sta->last_connected = uptime.tv_sec;
  309. ewma_init(&sta->avg_signal, 1024, 8);
  310. for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
  311. ewma_init(&sta->chain_signal_avg[i], 1024, 8);
  312. if (sta_prepare_rate_control(local, sta, gfp)) {
  313. kfree(sta);
  314. return NULL;
  315. }
  316. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  317. /*
  318. * timer_to_tid must be initialized with identity mapping
  319. * to enable session_timer's data differentiation. See
  320. * sta_rx_agg_session_timer_expired for usage.
  321. */
  322. sta->timer_to_tid[i] = i;
  323. }
  324. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  325. skb_queue_head_init(&sta->ps_tx_buf[i]);
  326. skb_queue_head_init(&sta->tx_filtered[i]);
  327. }
  328. for (i = 0; i < IEEE80211_NUM_TIDS; i++)
  329. sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
  330. sta->sta.smps_mode = IEEE80211_SMPS_OFF;
  331. sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
  332. return sta;
  333. }
  334. static int sta_info_insert_check(struct sta_info *sta)
  335. {
  336. struct ieee80211_sub_if_data *sdata = sta->sdata;
  337. /*
  338. * Can't be a WARN_ON because it can be triggered through a race:
  339. * something inserts a STA (on one CPU) without holding the RTNL
  340. * and another CPU turns off the net device.
  341. */
  342. if (unlikely(!ieee80211_sdata_running(sdata)))
  343. return -ENETDOWN;
  344. if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
  345. is_multicast_ether_addr(sta->sta.addr)))
  346. return -EINVAL;
  347. return 0;
  348. }
  349. static int sta_info_insert_drv_state(struct ieee80211_local *local,
  350. struct ieee80211_sub_if_data *sdata,
  351. struct sta_info *sta)
  352. {
  353. enum ieee80211_sta_state state;
  354. int err = 0;
  355. for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
  356. err = drv_sta_state(local, sdata, sta, state, state + 1);
  357. if (err)
  358. break;
  359. }
  360. if (!err) {
  361. /*
  362. * Drivers using legacy sta_add/sta_remove callbacks only
  363. * get uploaded set to true after sta_add is called.
  364. */
  365. if (!local->ops->sta_add)
  366. sta->uploaded = true;
  367. return 0;
  368. }
  369. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  370. sdata_info(sdata,
  371. "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
  372. sta->sta.addr, state + 1, err);
  373. err = 0;
  374. }
  375. /* unwind on error */
  376. for (; state > IEEE80211_STA_NOTEXIST; state--)
  377. WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
  378. return err;
  379. }
  380. /*
  381. * should be called with sta_mtx locked
  382. * this function replaces the mutex lock
  383. * with a RCU lock
  384. */
  385. static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
  386. {
  387. struct ieee80211_local *local = sta->local;
  388. struct ieee80211_sub_if_data *sdata = sta->sdata;
  389. struct station_info sinfo;
  390. int err = 0;
  391. lockdep_assert_held(&local->sta_mtx);
  392. /* check if STA exists already */
  393. if (sta_info_get_bss(sdata, sta->sta.addr)) {
  394. err = -EEXIST;
  395. goto out_err;
  396. }
  397. /* notify driver */
  398. err = sta_info_insert_drv_state(local, sdata, sta);
  399. if (err)
  400. goto out_err;
  401. local->num_sta++;
  402. local->sta_generation++;
  403. smp_mb();
  404. /* make the station visible */
  405. sta_info_hash_add(local, sta);
  406. list_add_rcu(&sta->list, &local->sta_list);
  407. set_sta_flag(sta, WLAN_STA_INSERTED);
  408. ieee80211_sta_debugfs_add(sta);
  409. rate_control_add_sta_debugfs(sta);
  410. memset(&sinfo, 0, sizeof(sinfo));
  411. sinfo.filled = 0;
  412. sinfo.generation = local->sta_generation;
  413. cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
  414. sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
  415. /* move reference to rcu-protected */
  416. rcu_read_lock();
  417. mutex_unlock(&local->sta_mtx);
  418. if (ieee80211_vif_is_mesh(&sdata->vif))
  419. mesh_accept_plinks_update(sdata);
  420. return 0;
  421. out_err:
  422. mutex_unlock(&local->sta_mtx);
  423. rcu_read_lock();
  424. return err;
  425. }
  426. int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
  427. {
  428. struct ieee80211_local *local = sta->local;
  429. int err = 0;
  430. might_sleep();
  431. err = sta_info_insert_check(sta);
  432. if (err) {
  433. rcu_read_lock();
  434. goto out_free;
  435. }
  436. mutex_lock(&local->sta_mtx);
  437. err = sta_info_insert_finish(sta);
  438. if (err)
  439. goto out_free;
  440. return 0;
  441. out_free:
  442. BUG_ON(!err);
  443. sta_info_free(local, sta);
  444. return err;
  445. }
  446. int sta_info_insert(struct sta_info *sta)
  447. {
  448. int err = sta_info_insert_rcu(sta);
  449. rcu_read_unlock();
  450. return err;
  451. }
  452. static inline void __bss_tim_set(u8 *tim, u16 id)
  453. {
  454. /*
  455. * This format has been mandated by the IEEE specifications,
  456. * so this line may not be changed to use the __set_bit() format.
  457. */
  458. tim[id / 8] |= (1 << (id % 8));
  459. }
  460. static inline void __bss_tim_clear(u8 *tim, u16 id)
  461. {
  462. /*
  463. * This format has been mandated by the IEEE specifications,
  464. * so this line may not be changed to use the __clear_bit() format.
  465. */
  466. tim[id / 8] &= ~(1 << (id % 8));
  467. }
  468. static inline bool __bss_tim_get(u8 *tim, u16 id)
  469. {
  470. /*
  471. * This format has been mandated by the IEEE specifications,
  472. * so this line may not be changed to use the test_bit() format.
  473. */
  474. return tim[id / 8] & (1 << (id % 8));
  475. }
  476. static unsigned long ieee80211_tids_for_ac(int ac)
  477. {
  478. /* If we ever support TIDs > 7, this obviously needs to be adjusted */
  479. switch (ac) {
  480. case IEEE80211_AC_VO:
  481. return BIT(6) | BIT(7);
  482. case IEEE80211_AC_VI:
  483. return BIT(4) | BIT(5);
  484. case IEEE80211_AC_BE:
  485. return BIT(0) | BIT(3);
  486. case IEEE80211_AC_BK:
  487. return BIT(1) | BIT(2);
  488. default:
  489. WARN_ON(1);
  490. return 0;
  491. }
  492. }
  493. void sta_info_recalc_tim(struct sta_info *sta)
  494. {
  495. struct ieee80211_local *local = sta->local;
  496. struct ps_data *ps;
  497. bool indicate_tim = false;
  498. u8 ignore_for_tim = sta->sta.uapsd_queues;
  499. int ac;
  500. u16 id;
  501. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  502. sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  503. if (WARN_ON_ONCE(!sta->sdata->bss))
  504. return;
  505. ps = &sta->sdata->bss->ps;
  506. id = sta->sta.aid;
  507. #ifdef CONFIG_MAC80211_MESH
  508. } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
  509. ps = &sta->sdata->u.mesh.ps;
  510. /* TIM map only for PLID <= IEEE80211_MAX_AID */
  511. id = le16_to_cpu(sta->plid) % IEEE80211_MAX_AID;
  512. #endif
  513. } else {
  514. return;
  515. }
  516. /* No need to do anything if the driver does all */
  517. if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
  518. return;
  519. if (sta->dead)
  520. goto done;
  521. /*
  522. * If all ACs are delivery-enabled then we should build
  523. * the TIM bit for all ACs anyway; if only some are then
  524. * we ignore those and build the TIM bit using only the
  525. * non-enabled ones.
  526. */
  527. if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
  528. ignore_for_tim = 0;
  529. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  530. unsigned long tids;
  531. if (ignore_for_tim & BIT(ac))
  532. continue;
  533. indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
  534. !skb_queue_empty(&sta->ps_tx_buf[ac]);
  535. if (indicate_tim)
  536. break;
  537. tids = ieee80211_tids_for_ac(ac);
  538. indicate_tim |=
  539. sta->driver_buffered_tids & tids;
  540. }
  541. done:
  542. spin_lock_bh(&local->tim_lock);
  543. if (indicate_tim == __bss_tim_get(ps->tim, id))
  544. goto out_unlock;
  545. if (indicate_tim)
  546. __bss_tim_set(ps->tim, id);
  547. else
  548. __bss_tim_clear(ps->tim, id);
  549. if (local->ops->set_tim) {
  550. local->tim_in_locked_section = true;
  551. drv_set_tim(local, &sta->sta, indicate_tim);
  552. local->tim_in_locked_section = false;
  553. }
  554. out_unlock:
  555. spin_unlock_bh(&local->tim_lock);
  556. }
  557. static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
  558. {
  559. struct ieee80211_tx_info *info;
  560. int timeout;
  561. if (!skb)
  562. return false;
  563. info = IEEE80211_SKB_CB(skb);
  564. /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
  565. timeout = (sta->listen_interval *
  566. sta->sdata->vif.bss_conf.beacon_int *
  567. 32 / 15625) * HZ;
  568. if (timeout < STA_TX_BUFFER_EXPIRE)
  569. timeout = STA_TX_BUFFER_EXPIRE;
  570. return time_after(jiffies, info->control.jiffies + timeout);
  571. }
  572. static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
  573. struct sta_info *sta, int ac)
  574. {
  575. unsigned long flags;
  576. struct sk_buff *skb;
  577. /*
  578. * First check for frames that should expire on the filtered
  579. * queue. Frames here were rejected by the driver and are on
  580. * a separate queue to avoid reordering with normal PS-buffered
  581. * frames. They also aren't accounted for right now in the
  582. * total_ps_buffered counter.
  583. */
  584. for (;;) {
  585. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  586. skb = skb_peek(&sta->tx_filtered[ac]);
  587. if (sta_info_buffer_expired(sta, skb))
  588. skb = __skb_dequeue(&sta->tx_filtered[ac]);
  589. else
  590. skb = NULL;
  591. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  592. /*
  593. * Frames are queued in order, so if this one
  594. * hasn't expired yet we can stop testing. If
  595. * we actually reached the end of the queue we
  596. * also need to stop, of course.
  597. */
  598. if (!skb)
  599. break;
  600. ieee80211_free_txskb(&local->hw, skb);
  601. }
  602. /*
  603. * Now also check the normal PS-buffered queue, this will
  604. * only find something if the filtered queue was emptied
  605. * since the filtered frames are all before the normal PS
  606. * buffered frames.
  607. */
  608. for (;;) {
  609. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  610. skb = skb_peek(&sta->ps_tx_buf[ac]);
  611. if (sta_info_buffer_expired(sta, skb))
  612. skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
  613. else
  614. skb = NULL;
  615. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  616. /*
  617. * frames are queued in order, so if this one
  618. * hasn't expired yet (or we reached the end of
  619. * the queue) we can stop testing
  620. */
  621. if (!skb)
  622. break;
  623. local->total_ps_buffered--;
  624. ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
  625. sta->sta.addr);
  626. ieee80211_free_txskb(&local->hw, skb);
  627. }
  628. /*
  629. * Finally, recalculate the TIM bit for this station -- it might
  630. * now be clear because the station was too slow to retrieve its
  631. * frames.
  632. */
  633. sta_info_recalc_tim(sta);
  634. /*
  635. * Return whether there are any frames still buffered, this is
  636. * used to check whether the cleanup timer still needs to run,
  637. * if there are no frames we don't need to rearm the timer.
  638. */
  639. return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
  640. skb_queue_empty(&sta->tx_filtered[ac]));
  641. }
  642. static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
  643. struct sta_info *sta)
  644. {
  645. bool have_buffered = false;
  646. int ac;
  647. /* This is only necessary for stations on BSS/MBSS interfaces */
  648. if (!sta->sdata->bss &&
  649. !ieee80211_vif_is_mesh(&sta->sdata->vif))
  650. return false;
  651. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  652. have_buffered |=
  653. sta_info_cleanup_expire_buffered_ac(local, sta, ac);
  654. return have_buffered;
  655. }
  656. int __must_check __sta_info_destroy(struct sta_info *sta)
  657. {
  658. struct ieee80211_local *local;
  659. struct ieee80211_sub_if_data *sdata;
  660. int ret;
  661. might_sleep();
  662. if (!sta)
  663. return -ENOENT;
  664. local = sta->local;
  665. sdata = sta->sdata;
  666. lockdep_assert_held(&local->sta_mtx);
  667. /*
  668. * Before removing the station from the driver and
  669. * rate control, it might still start new aggregation
  670. * sessions -- block that to make sure the tear-down
  671. * will be sufficient.
  672. */
  673. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  674. ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
  675. ret = sta_info_hash_del(local, sta);
  676. if (ret)
  677. return ret;
  678. list_del_rcu(&sta->list);
  679. /* this always calls synchronize_net() */
  680. ieee80211_free_sta_keys(local, sta);
  681. sta->dead = true;
  682. local->num_sta--;
  683. local->sta_generation++;
  684. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  685. RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
  686. while (sta->sta_state > IEEE80211_STA_NONE) {
  687. ret = sta_info_move_state(sta, sta->sta_state - 1);
  688. if (ret) {
  689. WARN_ON_ONCE(1);
  690. break;
  691. }
  692. }
  693. if (sta->uploaded) {
  694. ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
  695. IEEE80211_STA_NOTEXIST);
  696. WARN_ON_ONCE(ret != 0);
  697. }
  698. sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
  699. cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
  700. rate_control_remove_sta_debugfs(sta);
  701. ieee80211_sta_debugfs_remove(sta);
  702. call_rcu(&sta->rcu_head, free_sta_rcu);
  703. return 0;
  704. }
  705. int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
  706. {
  707. struct sta_info *sta;
  708. int ret;
  709. mutex_lock(&sdata->local->sta_mtx);
  710. sta = sta_info_get(sdata, addr);
  711. ret = __sta_info_destroy(sta);
  712. mutex_unlock(&sdata->local->sta_mtx);
  713. return ret;
  714. }
  715. int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
  716. const u8 *addr)
  717. {
  718. struct sta_info *sta;
  719. int ret;
  720. mutex_lock(&sdata->local->sta_mtx);
  721. sta = sta_info_get_bss(sdata, addr);
  722. ret = __sta_info_destroy(sta);
  723. mutex_unlock(&sdata->local->sta_mtx);
  724. return ret;
  725. }
  726. static void sta_info_cleanup(unsigned long data)
  727. {
  728. struct ieee80211_local *local = (struct ieee80211_local *) data;
  729. struct sta_info *sta;
  730. bool timer_needed = false;
  731. rcu_read_lock();
  732. list_for_each_entry_rcu(sta, &local->sta_list, list)
  733. if (sta_info_cleanup_expire_buffered(local, sta))
  734. timer_needed = true;
  735. rcu_read_unlock();
  736. if (local->quiescing)
  737. return;
  738. if (!timer_needed)
  739. return;
  740. mod_timer(&local->sta_cleanup,
  741. round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
  742. }
  743. void sta_info_init(struct ieee80211_local *local)
  744. {
  745. spin_lock_init(&local->tim_lock);
  746. mutex_init(&local->sta_mtx);
  747. INIT_LIST_HEAD(&local->sta_list);
  748. setup_timer(&local->sta_cleanup, sta_info_cleanup,
  749. (unsigned long)local);
  750. }
  751. void sta_info_stop(struct ieee80211_local *local)
  752. {
  753. del_timer_sync(&local->sta_cleanup);
  754. }
  755. int sta_info_flush_defer(struct ieee80211_sub_if_data *sdata)
  756. {
  757. struct ieee80211_local *local = sdata->local;
  758. struct sta_info *sta, *tmp;
  759. int ret = 0;
  760. might_sleep();
  761. mutex_lock(&local->sta_mtx);
  762. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  763. if (sdata == sta->sdata) {
  764. WARN_ON(__sta_info_destroy(sta));
  765. ret++;
  766. }
  767. }
  768. mutex_unlock(&local->sta_mtx);
  769. return ret;
  770. }
  771. void sta_info_flush_cleanup(struct ieee80211_sub_if_data *sdata)
  772. {
  773. ieee80211_cleanup_sdata_stas(sdata);
  774. cancel_work_sync(&sdata->cleanup_stations_wk);
  775. }
  776. void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
  777. unsigned long exp_time)
  778. {
  779. struct ieee80211_local *local = sdata->local;
  780. struct sta_info *sta, *tmp;
  781. mutex_lock(&local->sta_mtx);
  782. list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
  783. if (sdata != sta->sdata)
  784. continue;
  785. if (time_after(jiffies, sta->last_rx + exp_time)) {
  786. sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
  787. sta->sta.addr);
  788. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  789. test_sta_flag(sta, WLAN_STA_PS_STA))
  790. atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
  791. WARN_ON(__sta_info_destroy(sta));
  792. }
  793. }
  794. mutex_unlock(&local->sta_mtx);
  795. }
  796. struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
  797. const u8 *addr,
  798. const u8 *localaddr)
  799. {
  800. struct sta_info *sta, *nxt;
  801. /*
  802. * Just return a random station if localaddr is NULL
  803. * ... first in list.
  804. */
  805. for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
  806. if (localaddr &&
  807. !ether_addr_equal(sta->sdata->vif.addr, localaddr))
  808. continue;
  809. if (!sta->uploaded)
  810. return NULL;
  811. return &sta->sta;
  812. }
  813. return NULL;
  814. }
  815. EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
  816. struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
  817. const u8 *addr)
  818. {
  819. struct sta_info *sta;
  820. if (!vif)
  821. return NULL;
  822. sta = sta_info_get_bss(vif_to_sdata(vif), addr);
  823. if (!sta)
  824. return NULL;
  825. if (!sta->uploaded)
  826. return NULL;
  827. return &sta->sta;
  828. }
  829. EXPORT_SYMBOL(ieee80211_find_sta);
  830. static void clear_sta_ps_flags(void *_sta)
  831. {
  832. struct sta_info *sta = _sta;
  833. struct ieee80211_sub_if_data *sdata = sta->sdata;
  834. struct ps_data *ps;
  835. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  836. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  837. ps = &sdata->bss->ps;
  838. else if (ieee80211_vif_is_mesh(&sdata->vif))
  839. ps = &sdata->u.mesh.ps;
  840. else
  841. return;
  842. clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
  843. if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
  844. atomic_dec(&ps->num_sta_ps);
  845. }
  846. /* powersave support code */
  847. void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
  848. {
  849. struct ieee80211_sub_if_data *sdata = sta->sdata;
  850. struct ieee80211_local *local = sdata->local;
  851. struct sk_buff_head pending;
  852. int filtered = 0, buffered = 0, ac;
  853. unsigned long flags;
  854. clear_sta_flag(sta, WLAN_STA_SP);
  855. BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
  856. sta->driver_buffered_tids = 0;
  857. if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
  858. drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
  859. skb_queue_head_init(&pending);
  860. /* Send all buffered frames to the station */
  861. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  862. int count = skb_queue_len(&pending), tmp;
  863. spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
  864. skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
  865. spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
  866. tmp = skb_queue_len(&pending);
  867. filtered += tmp - count;
  868. count = tmp;
  869. spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
  870. skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
  871. spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
  872. tmp = skb_queue_len(&pending);
  873. buffered += tmp - count;
  874. }
  875. ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
  876. local->total_ps_buffered -= buffered;
  877. sta_info_recalc_tim(sta);
  878. ps_dbg(sdata,
  879. "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
  880. sta->sta.addr, sta->sta.aid, filtered, buffered);
  881. }
  882. static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
  883. struct sta_info *sta, int tid,
  884. enum ieee80211_frame_release_type reason)
  885. {
  886. struct ieee80211_local *local = sdata->local;
  887. struct ieee80211_qos_hdr *nullfunc;
  888. struct sk_buff *skb;
  889. int size = sizeof(*nullfunc);
  890. __le16 fc;
  891. bool qos = test_sta_flag(sta, WLAN_STA_WME);
  892. struct ieee80211_tx_info *info;
  893. struct ieee80211_chanctx_conf *chanctx_conf;
  894. if (qos) {
  895. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  896. IEEE80211_STYPE_QOS_NULLFUNC |
  897. IEEE80211_FCTL_FROMDS);
  898. } else {
  899. size -= 2;
  900. fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
  901. IEEE80211_STYPE_NULLFUNC |
  902. IEEE80211_FCTL_FROMDS);
  903. }
  904. skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
  905. if (!skb)
  906. return;
  907. skb_reserve(skb, local->hw.extra_tx_headroom);
  908. nullfunc = (void *) skb_put(skb, size);
  909. nullfunc->frame_control = fc;
  910. nullfunc->duration_id = 0;
  911. memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
  912. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  913. memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
  914. skb->priority = tid;
  915. skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
  916. if (qos) {
  917. nullfunc->qos_ctrl = cpu_to_le16(tid);
  918. if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
  919. nullfunc->qos_ctrl |=
  920. cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
  921. }
  922. info = IEEE80211_SKB_CB(skb);
  923. /*
  924. * Tell TX path to send this frame even though the
  925. * STA may still remain is PS mode after this frame
  926. * exchange. Also set EOSP to indicate this packet
  927. * ends the poll/service period.
  928. */
  929. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
  930. IEEE80211_TX_CTL_PS_RESPONSE |
  931. IEEE80211_TX_STATUS_EOSP |
  932. IEEE80211_TX_CTL_REQ_TX_STATUS;
  933. drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
  934. skb->dev = sdata->dev;
  935. rcu_read_lock();
  936. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  937. if (WARN_ON(!chanctx_conf)) {
  938. rcu_read_unlock();
  939. kfree_skb(skb);
  940. return;
  941. }
  942. ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band);
  943. rcu_read_unlock();
  944. }
  945. static void
  946. ieee80211_sta_ps_deliver_response(struct sta_info *sta,
  947. int n_frames, u8 ignored_acs,
  948. enum ieee80211_frame_release_type reason)
  949. {
  950. struct ieee80211_sub_if_data *sdata = sta->sdata;
  951. struct ieee80211_local *local = sdata->local;
  952. bool found = false;
  953. bool more_data = false;
  954. int ac;
  955. unsigned long driver_release_tids = 0;
  956. struct sk_buff_head frames;
  957. /* Service or PS-Poll period starts */
  958. set_sta_flag(sta, WLAN_STA_SP);
  959. __skb_queue_head_init(&frames);
  960. /*
  961. * Get response frame(s) and more data bit for it.
  962. */
  963. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  964. unsigned long tids;
  965. if (ignored_acs & BIT(ac))
  966. continue;
  967. tids = ieee80211_tids_for_ac(ac);
  968. if (!found) {
  969. driver_release_tids = sta->driver_buffered_tids & tids;
  970. if (driver_release_tids) {
  971. found = true;
  972. } else {
  973. struct sk_buff *skb;
  974. while (n_frames > 0) {
  975. skb = skb_dequeue(&sta->tx_filtered[ac]);
  976. if (!skb) {
  977. skb = skb_dequeue(
  978. &sta->ps_tx_buf[ac]);
  979. if (skb)
  980. local->total_ps_buffered--;
  981. }
  982. if (!skb)
  983. break;
  984. n_frames--;
  985. found = true;
  986. __skb_queue_tail(&frames, skb);
  987. }
  988. }
  989. /*
  990. * If the driver has data on more than one TID then
  991. * certainly there's more data if we release just a
  992. * single frame now (from a single TID).
  993. */
  994. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
  995. hweight16(driver_release_tids) > 1) {
  996. more_data = true;
  997. driver_release_tids =
  998. BIT(ffs(driver_release_tids) - 1);
  999. break;
  1000. }
  1001. }
  1002. if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
  1003. !skb_queue_empty(&sta->ps_tx_buf[ac])) {
  1004. more_data = true;
  1005. break;
  1006. }
  1007. }
  1008. if (!found) {
  1009. int tid;
  1010. /*
  1011. * For PS-Poll, this can only happen due to a race condition
  1012. * when we set the TIM bit and the station notices it, but
  1013. * before it can poll for the frame we expire it.
  1014. *
  1015. * For uAPSD, this is said in the standard (11.2.1.5 h):
  1016. * At each unscheduled SP for a non-AP STA, the AP shall
  1017. * attempt to transmit at least one MSDU or MMPDU, but no
  1018. * more than the value specified in the Max SP Length field
  1019. * in the QoS Capability element from delivery-enabled ACs,
  1020. * that are destined for the non-AP STA.
  1021. *
  1022. * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
  1023. */
  1024. /* This will evaluate to 1, 3, 5 or 7. */
  1025. tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
  1026. ieee80211_send_null_response(sdata, sta, tid, reason);
  1027. return;
  1028. }
  1029. if (!driver_release_tids) {
  1030. struct sk_buff_head pending;
  1031. struct sk_buff *skb;
  1032. int num = 0;
  1033. u16 tids = 0;
  1034. skb_queue_head_init(&pending);
  1035. while ((skb = __skb_dequeue(&frames))) {
  1036. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1037. struct ieee80211_hdr *hdr = (void *) skb->data;
  1038. u8 *qoshdr = NULL;
  1039. num++;
  1040. /*
  1041. * Tell TX path to send this frame even though the
  1042. * STA may still remain is PS mode after this frame
  1043. * exchange.
  1044. */
  1045. info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
  1046. IEEE80211_TX_CTL_PS_RESPONSE;
  1047. /*
  1048. * Use MoreData flag to indicate whether there are
  1049. * more buffered frames for this STA
  1050. */
  1051. if (more_data || !skb_queue_empty(&frames))
  1052. hdr->frame_control |=
  1053. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1054. else
  1055. hdr->frame_control &=
  1056. cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
  1057. if (ieee80211_is_data_qos(hdr->frame_control) ||
  1058. ieee80211_is_qos_nullfunc(hdr->frame_control))
  1059. qoshdr = ieee80211_get_qos_ctl(hdr);
  1060. /* end service period after last frame */
  1061. if (skb_queue_empty(&frames)) {
  1062. if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
  1063. qoshdr)
  1064. *qoshdr |= IEEE80211_QOS_CTL_EOSP;
  1065. info->flags |= IEEE80211_TX_STATUS_EOSP |
  1066. IEEE80211_TX_CTL_REQ_TX_STATUS;
  1067. }
  1068. if (qoshdr)
  1069. tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
  1070. else
  1071. tids |= BIT(0);
  1072. __skb_queue_tail(&pending, skb);
  1073. }
  1074. drv_allow_buffered_frames(local, sta, tids, num,
  1075. reason, more_data);
  1076. ieee80211_add_pending_skbs(local, &pending);
  1077. sta_info_recalc_tim(sta);
  1078. } else {
  1079. /*
  1080. * We need to release a frame that is buffered somewhere in the
  1081. * driver ... it'll have to handle that.
  1082. * Note that, as per the comment above, it'll also have to see
  1083. * if there is more than just one frame on the specific TID that
  1084. * we're releasing from, and it needs to set the more-data bit
  1085. * accordingly if we tell it that there's no more data. If we do
  1086. * tell it there's more data, then of course the more-data bit
  1087. * needs to be set anyway.
  1088. */
  1089. drv_release_buffered_frames(local, sta, driver_release_tids,
  1090. n_frames, reason, more_data);
  1091. /*
  1092. * Note that we don't recalculate the TIM bit here as it would
  1093. * most likely have no effect at all unless the driver told us
  1094. * that the TID became empty before returning here from the
  1095. * release function.
  1096. * Either way, however, when the driver tells us that the TID
  1097. * became empty we'll do the TIM recalculation.
  1098. */
  1099. }
  1100. }
  1101. void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
  1102. {
  1103. u8 ignore_for_response = sta->sta.uapsd_queues;
  1104. /*
  1105. * If all ACs are delivery-enabled then we should reply
  1106. * from any of them, if only some are enabled we reply
  1107. * only from the non-enabled ones.
  1108. */
  1109. if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
  1110. ignore_for_response = 0;
  1111. ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
  1112. IEEE80211_FRAME_RELEASE_PSPOLL);
  1113. }
  1114. void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
  1115. {
  1116. int n_frames = sta->sta.max_sp;
  1117. u8 delivery_enabled = sta->sta.uapsd_queues;
  1118. /*
  1119. * If we ever grow support for TSPEC this might happen if
  1120. * the TSPEC update from hostapd comes in between a trigger
  1121. * frame setting WLAN_STA_UAPSD in the RX path and this
  1122. * actually getting called.
  1123. */
  1124. if (!delivery_enabled)
  1125. return;
  1126. switch (sta->sta.max_sp) {
  1127. case 1:
  1128. n_frames = 2;
  1129. break;
  1130. case 2:
  1131. n_frames = 4;
  1132. break;
  1133. case 3:
  1134. n_frames = 6;
  1135. break;
  1136. case 0:
  1137. /* XXX: what is a good value? */
  1138. n_frames = 8;
  1139. break;
  1140. }
  1141. ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
  1142. IEEE80211_FRAME_RELEASE_UAPSD);
  1143. }
  1144. void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
  1145. struct ieee80211_sta *pubsta, bool block)
  1146. {
  1147. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1148. trace_api_sta_block_awake(sta->local, pubsta, block);
  1149. if (block)
  1150. set_sta_flag(sta, WLAN_STA_PS_DRIVER);
  1151. else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
  1152. ieee80211_queue_work(hw, &sta->drv_unblock_wk);
  1153. }
  1154. EXPORT_SYMBOL(ieee80211_sta_block_awake);
  1155. void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
  1156. {
  1157. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1158. struct ieee80211_local *local = sta->local;
  1159. trace_api_eosp(local, pubsta);
  1160. clear_sta_flag(sta, WLAN_STA_SP);
  1161. }
  1162. EXPORT_SYMBOL(ieee80211_sta_eosp);
  1163. void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
  1164. u8 tid, bool buffered)
  1165. {
  1166. struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
  1167. if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
  1168. return;
  1169. if (buffered)
  1170. set_bit(tid, &sta->driver_buffered_tids);
  1171. else
  1172. clear_bit(tid, &sta->driver_buffered_tids);
  1173. sta_info_recalc_tim(sta);
  1174. }
  1175. EXPORT_SYMBOL(ieee80211_sta_set_buffered);
  1176. int sta_info_move_state(struct sta_info *sta,
  1177. enum ieee80211_sta_state new_state)
  1178. {
  1179. might_sleep();
  1180. if (sta->sta_state == new_state)
  1181. return 0;
  1182. /* check allowed transitions first */
  1183. switch (new_state) {
  1184. case IEEE80211_STA_NONE:
  1185. if (sta->sta_state != IEEE80211_STA_AUTH)
  1186. return -EINVAL;
  1187. break;
  1188. case IEEE80211_STA_AUTH:
  1189. if (sta->sta_state != IEEE80211_STA_NONE &&
  1190. sta->sta_state != IEEE80211_STA_ASSOC)
  1191. return -EINVAL;
  1192. break;
  1193. case IEEE80211_STA_ASSOC:
  1194. if (sta->sta_state != IEEE80211_STA_AUTH &&
  1195. sta->sta_state != IEEE80211_STA_AUTHORIZED)
  1196. return -EINVAL;
  1197. break;
  1198. case IEEE80211_STA_AUTHORIZED:
  1199. if (sta->sta_state != IEEE80211_STA_ASSOC)
  1200. return -EINVAL;
  1201. break;
  1202. default:
  1203. WARN(1, "invalid state %d", new_state);
  1204. return -EINVAL;
  1205. }
  1206. sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
  1207. sta->sta.addr, new_state);
  1208. /*
  1209. * notify the driver before the actual changes so it can
  1210. * fail the transition
  1211. */
  1212. if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
  1213. int err = drv_sta_state(sta->local, sta->sdata, sta,
  1214. sta->sta_state, new_state);
  1215. if (err)
  1216. return err;
  1217. }
  1218. /* reflect the change in all state variables */
  1219. switch (new_state) {
  1220. case IEEE80211_STA_NONE:
  1221. if (sta->sta_state == IEEE80211_STA_AUTH)
  1222. clear_bit(WLAN_STA_AUTH, &sta->_flags);
  1223. break;
  1224. case IEEE80211_STA_AUTH:
  1225. if (sta->sta_state == IEEE80211_STA_NONE)
  1226. set_bit(WLAN_STA_AUTH, &sta->_flags);
  1227. else if (sta->sta_state == IEEE80211_STA_ASSOC)
  1228. clear_bit(WLAN_STA_ASSOC, &sta->_flags);
  1229. break;
  1230. case IEEE80211_STA_ASSOC:
  1231. if (sta->sta_state == IEEE80211_STA_AUTH) {
  1232. set_bit(WLAN_STA_ASSOC, &sta->_flags);
  1233. } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
  1234. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1235. (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1236. !sta->sdata->u.vlan.sta))
  1237. atomic_dec(&sta->sdata->bss->num_mcast_sta);
  1238. clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1239. }
  1240. break;
  1241. case IEEE80211_STA_AUTHORIZED:
  1242. if (sta->sta_state == IEEE80211_STA_ASSOC) {
  1243. if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
  1244. (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  1245. !sta->sdata->u.vlan.sta))
  1246. atomic_inc(&sta->sdata->bss->num_mcast_sta);
  1247. set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
  1248. }
  1249. break;
  1250. default:
  1251. break;
  1252. }
  1253. sta->sta_state = new_state;
  1254. return 0;
  1255. }