sta_info.c 38 KB

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