sta_info.c 38 KB

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