sta_info.c 36 KB

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