sta_info.c 38 KB

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